portio.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * provides /dev/port
  7. *
  8. */
  9. #include <kernel/system.h>
  10. #include <kernel/fs.h>
  11. #include <kernel/module.h>
  12. static uint32_t read_port(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
  13. switch (size) {
  14. case 1:
  15. buffer[0] = inportb(offset);
  16. break;
  17. case 2:
  18. ((uint16_t *)buffer)[0] = inports(offset);
  19. break;
  20. case 4:
  21. ((uint32_t *)buffer)[0] = inportl(offset);
  22. break;
  23. default:
  24. for (unsigned int i = 0; i < size; ++i) {
  25. buffer[i] = inportb(offset + i);
  26. }
  27. break;
  28. }
  29. return size;
  30. }
  31. static uint32_t write_port(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
  32. switch (size) {
  33. case 1:
  34. outportb(offset, buffer[0]);
  35. break;
  36. case 2:
  37. outports(offset, ((uint16_t*)buffer)[0]);
  38. break;
  39. case 4:
  40. outportl(offset, ((uint32_t*)buffer)[0]);
  41. break;
  42. default:
  43. for (unsigned int i = 0; i < size; ++i) {
  44. outportb(offset +i, buffer[i]);
  45. }
  46. break;
  47. }
  48. return size;
  49. }
  50. static fs_node_t * port_device_create(void) {
  51. fs_node_t * fnode = malloc(sizeof(fs_node_t));
  52. memset(fnode, 0x00, sizeof(fs_node_t));
  53. fnode->inode = 0;
  54. strcpy(fnode->name, "port");
  55. fnode->uid = 0;
  56. fnode->gid = 0;
  57. fnode->mask = 0660;
  58. fnode->flags = FS_CHARDEVICE;
  59. fnode->read = read_port;
  60. fnode->write = write_port;
  61. fnode->open = NULL;
  62. fnode->close = NULL;
  63. fnode->readdir = NULL;
  64. fnode->finddir = NULL;
  65. fnode->ioctl = NULL;
  66. return fnode;
  67. }
  68. static int port_initialize(void) {
  69. vfs_mount("/dev/port", port_device_create());
  70. return 0;
  71. }
  72. static int port_finalize(void) {
  73. return 0;
  74. }
  75. MODULE_DEF(portio, port_initialize, port_finalize);