io.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* I/O library: PS/2 keyboard and text output.
  2. Text output is routed to the current process's screen (see process.c);
  3. slot 0 / boot fall back to a static screen the WM does not render. */
  4. #include "io.h"
  5. #include "fbtext.h"
  6. #include "process.h"
  7. #include "screen.h"
  8. #include "timer.h"
  9. #define VGA_ATTR 0x0A
  10. static unsigned int io_cols = 80;
  11. static unsigned int io_rows = 25;
  12. #define KB_DATA 0x60
  13. #define KB_STATUS 0x64
  14. static inline unsigned char inb(unsigned short port)
  15. {
  16. unsigned char v;
  17. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  18. return v;
  19. }
  20. static inline void outb(unsigned short port, unsigned char v)
  21. {
  22. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  23. }
  24. /* Scancode set 1 (XT) -> ASCII. 0 = ignore. */
  25. static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodata"))) = {
  26. [0x01] = 0x1B, /* Escape */
  27. [0x02] = '1', [0x03] = '2', [0x04] = '3', [0x05] = '4', [0x06] = '5',
  28. [0x07] = '6', [0x08] = '7', [0x09] = '8', [0x0A] = '9', [0x0B] = '0',
  29. [0x0C] = '-', [0x0D] = '=', [0x0E] = '\b', [0x0F] = '\t',
  30. [0x10] = 'q', [0x11] = 'w', [0x12] = 'e', [0x13] = 'r', [0x14] = 't',
  31. [0x15] = 'y', [0x16] = 'u', [0x17] = 'i', [0x18] = 'o', [0x19] = 'p',
  32. [0x1A] = '[', [0x1B] = ']', [0x1C] = '\n',
  33. [0x1E] = 'a', [0x1F] = 's', [0x20] = 'd', [0x21] = 'f', [0x22] = 'g',
  34. [0x23] = 'h', [0x24] = 'j', [0x25] = 'k', [0x26] = 'l', [0x27] = ';',
  35. [0x28] = '\'', [0x29] = '`',
  36. [0x2B] = '\\', [0x2C] = 'z', [0x2D] = 'x', [0x2E] = 'c', [0x2F] = 'v',
  37. [0x30] = 'b', [0x31] = 'n', [0x32] = 'm', [0x33] = ',', [0x34] = '.',
  38. [0x35] = '/', [0x39] = ' ',
  39. };
  40. static int shift_pressed;
  41. static int ctrl_pressed;
  42. static int kbd_extended; /* 1 after 0xE0, next byte is extended scancode */
  43. #define KBD_BUF_SIZE 16
  44. static volatile unsigned char kbd_buf[KBD_BUF_SIZE];
  45. static volatile unsigned int kbd_head, kbd_tail;
  46. /* Fallback screen for slot 0 / boot / exception output (not rendered by WM). */
  47. static struct text_screen io_fallback;
  48. static struct text_screen *io_current_screen(void)
  49. {
  50. struct text_screen *s = procs[current_pid].screen;
  51. return s ? s : &io_fallback;
  52. }
  53. /* IRQ1: read the controller status first and only read data when the output
  54. * buffer is full. Some USB-legacy SMM implementations keep raising IRQ1:
  55. * reading 0x60 with no byte present does not acknowledge anything, and a
  56. * level-triggered line then storms. The loop is bounded, and if the rate
  57. * gets absurd we mask IRQ1 and let the WM fall back to polling. */
  58. static unsigned int kbd_win_start;
  59. static unsigned int kbd_win_count;
  60. static volatile int kbd_irq_masked;
  61. int io_keyboard_irq_masked(void)
  62. {
  63. return kbd_irq_masked;
  64. }
  65. void kbd_irq(void)
  66. {
  67. unsigned int guard = 0;
  68. unsigned int now = get_ticks_ms();
  69. unsigned char st;
  70. if (now - kbd_win_start >= 1000) {
  71. kbd_win_start = now;
  72. kbd_win_count = 0;
  73. }
  74. kbd_win_count++;
  75. if (!kbd_irq_masked && kbd_win_count > 200) {
  76. /* IRQ1 storm; mask it (master PIC) and poll instead. */
  77. outb(0x21, (unsigned char)(inb(0x21) | 0x02));
  78. kbd_irq_masked = 1;
  79. }
  80. while (guard++ < 32) {
  81. st = inb(KB_STATUS);
  82. if (!(st & 0x01))
  83. break;
  84. {
  85. unsigned char sc = inb(KB_DATA);
  86. if (st & 0x20) {
  87. /* AUX (mouse) byte: not ours, drop it. */
  88. continue;
  89. }
  90. {
  91. unsigned int next = (kbd_head + 1) % KBD_BUF_SIZE;
  92. if (next != kbd_tail) {
  93. kbd_buf[kbd_head] = sc;
  94. kbd_head = next;
  95. }
  96. }
  97. }
  98. }
  99. outb(0x20, 0x20);
  100. }
  101. static int kbd_buf_get(unsigned char *sc)
  102. {
  103. if (kbd_tail == kbd_head)
  104. return 0;
  105. *sc = kbd_buf[kbd_tail];
  106. kbd_tail = (kbd_tail + 1) % KBD_BUF_SIZE;
  107. return 1;
  108. }
  109. static char apply_shift(char c)
  110. {
  111. if (!shift_pressed) return c;
  112. if (c >= 'a' && c <= 'z') return (char)(c - 32);
  113. switch ((unsigned char)c) {
  114. case '1': return '!'; case '2': return '@'; case '3': return '#';
  115. case '4': return '$'; case '5': return '%'; case '6': return '^';
  116. case '7': return '&'; case '8': return '*'; case '9': return '(';
  117. case '0': return ')'; case '-': return '_'; case '=': return '+';
  118. case '[': return '{'; case ']': return '}'; case ';': return ':';
  119. case '\'': return '"'; case '`': return '~'; case '\\': return '|';
  120. case ',': return '<'; case '.': return '>'; case '/': return '?';
  121. default: return c;
  122. }
  123. }
  124. unsigned int io_get_cols(void)
  125. {
  126. return io_cols;
  127. }
  128. unsigned int io_get_rows(void)
  129. {
  130. return io_rows;
  131. }
  132. void io_init(void)
  133. {
  134. fbtext_init();
  135. io_cols = fbtext_get_cols();
  136. io_rows = fbtext_get_rows();
  137. screen_init(&io_fallback);
  138. io_drain_keyboard();
  139. }
  140. void io_clear_screen(void)
  141. {
  142. screen_clear(io_current_screen());
  143. }
  144. int io_did_scroll(void)
  145. {
  146. return screen_did_scroll(io_current_screen());
  147. }
  148. void io_putchar(char c)
  149. {
  150. screen_putchar(io_current_screen(), c);
  151. }
  152. void io_putstr(const char *s)
  153. {
  154. screen_putstr(io_current_screen(), s);
  155. }
  156. void io_puthex(unsigned int n)
  157. {
  158. screen_puthex(io_current_screen(), n);
  159. }
  160. unsigned int io_get_cursor(void)
  161. {
  162. return screen_get_cursor(io_current_screen());
  163. }
  164. void io_set_cursor(unsigned int pos)
  165. {
  166. screen_set_cursor(io_current_screen(), pos);
  167. }
  168. void io_cursor_show_block(void)
  169. {
  170. struct text_screen *s = io_current_screen();
  171. s->cursor_visible = 1;
  172. screen_damage_cursor(s);
  173. }
  174. void io_cursor_hide_block(void)
  175. {
  176. struct text_screen *s = io_current_screen();
  177. s->cursor_visible = 0;
  178. screen_damage_cursor(s);
  179. }
  180. void io_vga_write(unsigned int pos, unsigned short value)
  181. {
  182. screen_put_at(io_current_screen(), pos, value);
  183. }
  184. void io_drain_keyboard(void)
  185. {
  186. while (inb(KB_STATUS) & 1)
  187. (void)inb(KB_DATA);
  188. kbd_head = kbd_tail = 0;
  189. kbd_extended = 0;
  190. }
  191. /* Process one scancode byte; shared by io_poll_key and io_poll_key_with_hw. */
  192. static int kbd_process_byte(unsigned char sc)
  193. {
  194. if (sc == 0xE0) {
  195. kbd_extended = 1;
  196. return 0;
  197. }
  198. if (kbd_extended) {
  199. kbd_extended = 0;
  200. if (sc & 0x80)
  201. return 0; /* key release */
  202. if (sc == 0x4B) return IO_KEY_LEFT;
  203. if (sc == 0x4D) return IO_KEY_RIGHT;
  204. if (sc == 0x48) return IO_KEY_UP;
  205. if (sc == 0x50) return IO_KEY_DOWN;
  206. return 0;
  207. }
  208. if (sc & 0x80) {
  209. if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
  210. if (sc == 0x9D) ctrl_pressed = 0;
  211. return 0;
  212. }
  213. if (sc == 0x2A || sc == 0x36) {
  214. shift_pressed = 1;
  215. return 0;
  216. }
  217. if (sc == 0x1D) {
  218. ctrl_pressed = 1;
  219. return 0;
  220. }
  221. {
  222. char c = (char)scancode_to_ascii[sc];
  223. if (c) {
  224. c = apply_shift(c);
  225. if (ctrl_pressed) {
  226. /* Ctrl+letter -> control character (e.g. Ctrl+C -> 0x03). */
  227. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
  228. return (unsigned char)(c & 0x1F);
  229. return 0;
  230. }
  231. return (unsigned char)c;
  232. }
  233. }
  234. return 0;
  235. }
  236. int io_poll_key(void)
  237. {
  238. unsigned char sc;
  239. if (!kbd_buf_get(&sc))
  240. return 0;
  241. return kbd_process_byte(sc);
  242. }
  243. /* Like io_poll_key but also reads from port 0x60 when buffer is empty.
  244. For use by full-screen programs (e.g. space.bin) where IRQ may be missed on
  245. real HW. Only reads when a keyboard byte is actually pending (OBF set, AUX
  246. clear); the status+data pair is read atomically wrt IRQ1. */
  247. int io_poll_key_with_hw(void)
  248. {
  249. unsigned char sc = 0;
  250. unsigned int flags;
  251. int got = 0;
  252. if (kbd_tail != kbd_head) {
  253. if (!kbd_buf_get(&sc))
  254. return 0;
  255. return kbd_process_byte(sc);
  256. }
  257. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  258. {
  259. unsigned char st = inb(KB_STATUS);
  260. if ((st & 0x01) && !(st & 0x20)) {
  261. sc = inb(KB_DATA);
  262. got = 1;
  263. }
  264. }
  265. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  266. if (!got)
  267. return 0;
  268. return kbd_process_byte(sc);
  269. }
  270. /* Queue a scancode from a non-IRQ source (the USB keyboard backend); the
  271. normal io_poll_key/io_poll_key_with_hw path then decodes it. Interrupts
  272. are masked while touching the ring so an IRQ1 producer cannot corrupt it. */
  273. void io_inject_scancode(unsigned char sc)
  274. {
  275. unsigned int flags;
  276. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  277. {
  278. unsigned int next = (kbd_head + 1) % KBD_BUF_SIZE;
  279. if (next != kbd_tail) {
  280. kbd_buf[kbd_head] = sc;
  281. kbd_head = next;
  282. }
  283. }
  284. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  285. }
  286. static int kbd_raw;
  287. void io_set_raw_keyboard(int on)
  288. {
  289. kbd_raw = on;
  290. }
  291. int io_keyboard_raw(void)
  292. {
  293. return kbd_raw;
  294. }