serial.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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) 2014-2018 K. Lange
  5. *
  6. * Serial communication device
  7. *
  8. */
  9. #include <kernel/system.h>
  10. #include <kernel/fs.h>
  11. #include <kernel/pipe.h>
  12. #include <kernel/logging.h>
  13. #include <kernel/args.h>
  14. #include <kernel/module.h>
  15. #define SERIAL_PORT_A 0x3F8
  16. #define SERIAL_PORT_B 0x2F8
  17. #define SERIAL_PORT_C 0x3E8
  18. #define SERIAL_PORT_D 0x2E8
  19. #define SERIAL_IRQ_AC 4
  20. #define SERIAL_IRQ_BD 3
  21. static char serial_recv(int device);
  22. static fs_node_t * _serial_port_a = NULL;
  23. static fs_node_t * _serial_port_b = NULL;
  24. static fs_node_t * _serial_port_c = NULL;
  25. static fs_node_t * _serial_port_d = NULL;
  26. static fs_node_t ** pipe_for_port(int port) {
  27. switch (port) {
  28. case SERIAL_PORT_A: return &_serial_port_a;
  29. case SERIAL_PORT_B: return &_serial_port_b;
  30. case SERIAL_PORT_C: return &_serial_port_c;
  31. case SERIAL_PORT_D: return &_serial_port_d;
  32. }
  33. __builtin_unreachable();
  34. }
  35. static int serial_handler_ac(struct regs *r) {
  36. char serial;
  37. int port = 0;
  38. if (inportb(SERIAL_PORT_A+1) & 0x01) {
  39. port = SERIAL_PORT_A;
  40. } else {
  41. port = SERIAL_PORT_C;
  42. }
  43. serial = serial_recv(port);
  44. irq_ack(SERIAL_IRQ_AC);
  45. write_fs(*pipe_for_port(port), 0, 1, (uint8_t*)&serial);
  46. return 1;
  47. }
  48. static int serial_handler_bd(struct regs *r) {
  49. char serial;
  50. int port = 0;
  51. debug_print(NOTICE, "Received something on secondary port");
  52. if (inportb(SERIAL_PORT_B+1) & 0x01) {
  53. port = SERIAL_PORT_B;
  54. } else {
  55. port = SERIAL_PORT_D;
  56. }
  57. serial = serial_recv(port);
  58. irq_ack(SERIAL_IRQ_BD);
  59. write_fs(*pipe_for_port(port), 0, 1, (uint8_t*)&serial);
  60. return 1;
  61. }
  62. static void serial_enable(int port) {
  63. outportb(port + 1, 0x00); /* Disable interrupts */
  64. outportb(port + 3, 0x80); /* Enable divisor mode */
  65. outportb(port + 0, 0x01); /* Div Low: 01 Set the port to 115200 bps */
  66. outportb(port + 1, 0x00); /* Div High: 00 */
  67. outportb(port + 3, 0x03); /* Disable divisor mode, set parity */
  68. outportb(port + 2, 0xC7); /* Enable FIFO and clear */
  69. outportb(port + 4, 0x0B); /* Enable interrupts */
  70. outportb(port + 1, 0x01); /* Enable interrupts */
  71. }
  72. static int serial_rcvd(int device) {
  73. return inportb(device + 5) & 1;
  74. }
  75. static char serial_recv(int device) {
  76. while (serial_rcvd(device) == 0) ;
  77. return inportb(device);
  78. }
  79. static int serial_transmit_empty(int device) {
  80. return inportb(device + 5) & 0x20;
  81. }
  82. static void serial_send(int device, char out) {
  83. while (serial_transmit_empty(device) == 0);
  84. outportb(device, out);
  85. }
  86. static void serial_string(char * out) {
  87. for (uint32_t i = 0; i < strlen(out); ++i) {
  88. serial_send(SERIAL_PORT_A, out[i]);
  89. }
  90. }
  91. static uint32_t read_serial(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer);
  92. static uint32_t write_serial(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer);
  93. static void open_serial(fs_node_t *node, unsigned int flags);
  94. static void close_serial(fs_node_t *node);
  95. static uint32_t read_serial(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
  96. return read_fs(*pipe_for_port((int)node->device), offset, size, buffer);
  97. }
  98. static uint32_t write_serial(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
  99. uint32_t sent = 0;
  100. while (sent < size) {
  101. serial_send((int)node->device, buffer[sent]);
  102. sent++;
  103. }
  104. return size;
  105. }
  106. static void open_serial(fs_node_t * node, unsigned int flags) {
  107. return;
  108. }
  109. static void close_serial(fs_node_t * node) {
  110. return;
  111. }
  112. static int wait_serial(fs_node_t * node, void * process) {
  113. return selectwait_fs(*pipe_for_port((int)node->device), process);
  114. }
  115. static int check_serial(fs_node_t * node) {
  116. return selectcheck_fs(*pipe_for_port((int)node->device));
  117. }
  118. static int have_installed_ac = 0;
  119. static int have_installed_bd = 0;
  120. static fs_node_t * serial_device_create(int device) {
  121. fs_node_t * fnode = malloc(sizeof(fs_node_t));
  122. memset(fnode, 0x00, sizeof(fs_node_t));
  123. fnode->device= (void *)device;
  124. strcpy(fnode->name, "serial");
  125. fnode->uid = 0;
  126. fnode->gid = 0;
  127. fnode->mask = 0660;
  128. fnode->flags = FS_CHARDEVICE;
  129. fnode->read = read_serial;
  130. fnode->write = write_serial;
  131. fnode->open = open_serial;
  132. fnode->close = close_serial;
  133. fnode->readdir = NULL;
  134. fnode->finddir = NULL;
  135. fnode->ioctl = NULL; /* TODO ioctls for raw serial devices */
  136. fnode->selectcheck = check_serial;
  137. fnode->selectwait = wait_serial;
  138. fnode->atime = now();
  139. fnode->mtime = fnode->atime;
  140. fnode->ctime = fnode->atime;
  141. serial_enable(device);
  142. if (device == SERIAL_PORT_A || device == SERIAL_PORT_C) {
  143. if (!have_installed_ac) {
  144. irq_install_handler(SERIAL_IRQ_AC, serial_handler_ac, "serial ac");
  145. have_installed_ac = 1;
  146. }
  147. } else {
  148. if (!have_installed_bd) {
  149. irq_install_handler(SERIAL_IRQ_BD, serial_handler_bd, "serial bd");
  150. have_installed_bd = 1;
  151. }
  152. }
  153. *pipe_for_port(device) = make_pipe(128);
  154. return fnode;
  155. }
  156. static int serial_mount_devices(void) {
  157. fs_node_t * ttyS0 = serial_device_create(SERIAL_PORT_A);
  158. vfs_mount("/dev/ttyS0", ttyS0);
  159. fs_node_t * ttyS1 = serial_device_create(SERIAL_PORT_B);
  160. vfs_mount("/dev/ttyS1", ttyS1);
  161. fs_node_t * ttyS2 = serial_device_create(SERIAL_PORT_C);
  162. vfs_mount("/dev/ttyS2", ttyS2);
  163. fs_node_t * ttyS3 = serial_device_create(SERIAL_PORT_D);
  164. vfs_mount("/dev/ttyS3", ttyS3);
  165. char * c;
  166. if ((c = args_value("logtoserial"))) {
  167. debug_file = ttyS0;
  168. if (!strcmp(c,"INFO") || !strcmp(c,"info")) {
  169. debug_level = INFO;
  170. } else if (!strcmp(c,"NOTICE") || !strcmp(c,"notice")) {
  171. debug_level = NOTICE;
  172. } else if (!strcmp(c,"WARNING") || !strcmp(c,"warning")) {
  173. debug_level = WARNING;
  174. } else if (!strcmp(c,"ERROR") || !strcmp(c,"error")) {
  175. debug_level = ERROR;
  176. } else if (!strcmp(c,"CRITICAL") || !strcmp(c,"critical")) {
  177. debug_level = CRITICAL;
  178. } else if (!strcmp(c,"INSANE") || !strcmp(c,"insane")) {
  179. debug_level = INSANE;
  180. } else {
  181. debug_level = atoi(c);
  182. }
  183. debug_print(NOTICE, "Serial logging enabled at level %d.", debug_level);
  184. }
  185. return 0;
  186. }
  187. static int serial_finalize(void) {
  188. return 0;
  189. }
  190. MODULE_DEF(serial, serial_mount_devices, serial_finalize);