io.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 "mouse.h"
  7. #include "process.h"
  8. #include "screen.h"
  9. #include "timer.h"
  10. #include "tty.h"
  11. #define VGA_ATTR 0x0A
  12. static unsigned int io_cols = 80;
  13. static unsigned int io_rows = 25;
  14. #define KB_DATA 0x60
  15. #define KB_STATUS 0x64
  16. static inline unsigned char inb(unsigned short port)
  17. {
  18. unsigned char v;
  19. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  20. return v;
  21. }
  22. static inline void outb(unsigned short port, unsigned char v)
  23. {
  24. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  25. }
  26. /* Scancode set 1 (XT) -> ASCII. 0 = ignore. */
  27. static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodata"))) = {
  28. [0x01] = 0x1B, /* Escape */
  29. [0x02] = '1', [0x03] = '2', [0x04] = '3', [0x05] = '4', [0x06] = '5',
  30. [0x07] = '6', [0x08] = '7', [0x09] = '8', [0x0A] = '9', [0x0B] = '0',
  31. [0x0C] = '-', [0x0D] = '=', [0x0E] = '\b', [0x0F] = '\t',
  32. [0x10] = 'q', [0x11] = 'w', [0x12] = 'e', [0x13] = 'r', [0x14] = 't',
  33. [0x15] = 'y', [0x16] = 'u', [0x17] = 'i', [0x18] = 'o', [0x19] = 'p',
  34. [0x1A] = '[', [0x1B] = ']', [0x1C] = '\n',
  35. [0x1E] = 'a', [0x1F] = 's', [0x20] = 'd', [0x21] = 'f', [0x22] = 'g',
  36. [0x23] = 'h', [0x24] = 'j', [0x25] = 'k', [0x26] = 'l', [0x27] = ';',
  37. [0x28] = '\'', [0x29] = '`',
  38. [0x2B] = '\\', [0x2C] = 'z', [0x2D] = 'x', [0x2E] = 'c', [0x2F] = 'v',
  39. [0x30] = 'b', [0x31] = 'n', [0x32] = 'm', [0x33] = ',', [0x34] = '.',
  40. [0x35] = '/', [0x39] = ' ',
  41. };
  42. static int shift_pressed;
  43. static int ctrl_pressed;
  44. static int kbd_extended; /* 1 after 0xE0, next byte is extended scancode */
  45. /* Software typematic repeat. PS/2 keyboards repeat in hardware; the USB HID
  46. * backend only reports changes, so a held key would otherwise produce exactly
  47. * one event. Track the last make code here (both backends end up in this
  48. * decoder) and re-deliver it after the usual delay/rate. */
  49. #define KBD_REPEAT_DELAY_MS 500
  50. #define KBD_REPEAT_RATE_MS 33
  51. static int repeat_key;
  52. static int repeat_active;
  53. static unsigned int repeat_ext;
  54. static unsigned int repeat_sc;
  55. static unsigned int repeat_at;
  56. #define KBD_BUF_SIZE 16
  57. static volatile unsigned char kbd_buf[KBD_BUF_SIZE];
  58. static volatile unsigned int kbd_head, kbd_tail;
  59. /* Fallback screen for slot 0 / boot / exception output (not rendered by WM). */
  60. static struct text_screen io_fallback;
  61. static struct text_screen *io_current_screen(void)
  62. {
  63. struct process *p = current_proc();
  64. struct text_screen *s = p ? p->screen : 0;
  65. return s ? s : &io_fallback;
  66. }
  67. /* IRQ1: read the controller status first and only read data when the output
  68. * buffer is full. Some USB-legacy SMM implementations keep raising IRQ1:
  69. * reading 0x60 with no byte present does not acknowledge anything, and a
  70. * level-triggered line then storms. The loop is bounded, and if the rate
  71. * gets absurd we mask IRQ1 and let the WM fall back to polling. */
  72. static unsigned int kbd_win_start;
  73. static unsigned int kbd_win_count;
  74. static volatile int kbd_irq_masked;
  75. int io_keyboard_irq_masked(void)
  76. {
  77. return kbd_irq_masked;
  78. }
  79. void kbd_irq(void)
  80. {
  81. unsigned int guard = 0;
  82. unsigned int now = get_ticks_ms();
  83. unsigned char st;
  84. if (now - kbd_win_start >= 1000) {
  85. kbd_win_start = now;
  86. kbd_win_count = 0;
  87. }
  88. kbd_win_count++;
  89. if (!kbd_irq_masked && kbd_win_count > 200) {
  90. /* IRQ1 storm; mask it (master PIC) and poll instead. */
  91. outb(0x21, (unsigned char)(inb(0x21) | 0x02));
  92. kbd_irq_masked = 1;
  93. }
  94. while (guard++ < 32) {
  95. st = inb(KB_STATUS);
  96. if (!(st & 0x01))
  97. break;
  98. if (st & 0x20) {
  99. /* AUX (mouse) byte: the IRQ12 handler or the mouse
  100. * poll owns it while streaming is enabled. With no
  101. * owner, discard it so it cannot block the keyboard. */
  102. if (mouse_streaming_enabled())
  103. break;
  104. (void)inb(KB_DATA);
  105. continue;
  106. }
  107. {
  108. unsigned char sc = inb(KB_DATA);
  109. {
  110. unsigned int next = (kbd_head + 1) % KBD_BUF_SIZE;
  111. if (next != kbd_tail) {
  112. kbd_buf[kbd_head] = sc;
  113. kbd_head = next;
  114. }
  115. }
  116. }
  117. }
  118. outb(0x20, 0x20);
  119. }
  120. static int kbd_buf_get(unsigned char *sc)
  121. {
  122. if (kbd_tail == kbd_head)
  123. return 0;
  124. *sc = kbd_buf[kbd_tail];
  125. kbd_tail = (kbd_tail + 1) % KBD_BUF_SIZE;
  126. return 1;
  127. }
  128. static char apply_shift(char c)
  129. {
  130. if (!shift_pressed) return c;
  131. if (c >= 'a' && c <= 'z') return (char)(c - 32);
  132. switch ((unsigned char)c) {
  133. case '1': return '!'; case '2': return '@'; case '3': return '#';
  134. case '4': return '$'; case '5': return '%'; case '6': return '^';
  135. case '7': return '&'; case '8': return '*'; case '9': return '(';
  136. case '0': return ')'; case '-': return '_'; case '=': return '+';
  137. case '[': return '{'; case ']': return '}'; case ';': return ':';
  138. case '\'': return '"'; case '`': return '~'; case '\\': return '|';
  139. case ',': return '<'; case '.': return '>'; case '/': return '?';
  140. default: return c;
  141. }
  142. }
  143. unsigned int io_get_cols(void)
  144. {
  145. return io_cols;
  146. }
  147. unsigned int io_get_rows(void)
  148. {
  149. return io_rows;
  150. }
  151. void io_init(void)
  152. {
  153. fbtext_init();
  154. io_cols = fbtext_get_cols();
  155. io_rows = fbtext_get_rows();
  156. screen_init(&io_fallback);
  157. io_drain_keyboard();
  158. }
  159. /* A process with a tty writes there; the kernel terminal/boot fallback keeps
  160. * the old cell-screen path until the ring-3 terminal is running. */
  161. static int io_tty(void)
  162. {
  163. struct process *p = current_proc();
  164. int id = p ? p->tty : 0;
  165. return tty_valid(id) ? id : 0;
  166. }
  167. void io_clear_screen(void)
  168. {
  169. int id = io_tty();
  170. if (id) {
  171. tty_write_str(id, "\x1b[2J\x1b[H");
  172. return;
  173. }
  174. screen_clear(io_current_screen());
  175. }
  176. int io_did_scroll(void)
  177. {
  178. if (io_tty())
  179. return 0;
  180. return screen_did_scroll(io_current_screen());
  181. }
  182. void io_putchar(char c)
  183. {
  184. int id = io_tty();
  185. if (id) {
  186. tty_write(id, &c, 1);
  187. return;
  188. }
  189. screen_putchar(io_current_screen(), c);
  190. }
  191. void io_putstr(const char *s)
  192. {
  193. int id = io_tty();
  194. if (id) {
  195. tty_write_str(id, s);
  196. return;
  197. }
  198. screen_putstr(io_current_screen(), s);
  199. }
  200. void io_puthex(unsigned int n)
  201. {
  202. if (io_tty()) {
  203. static const char hex[] = "0123456789ABCDEF";
  204. char buf[9];
  205. unsigned int i = 8;
  206. buf[8] = '\0';
  207. do {
  208. buf[--i] = hex[n & 0xF];
  209. n >>= 4;
  210. } while (i > 0);
  211. io_putstr(buf);
  212. return;
  213. }
  214. screen_puthex(io_current_screen(), n);
  215. }
  216. unsigned int io_get_cursor(void)
  217. {
  218. if (io_tty())
  219. return 0;
  220. return screen_get_cursor(io_current_screen());
  221. }
  222. void io_set_cursor(unsigned int pos)
  223. {
  224. if (io_tty()) {
  225. char buf[16];
  226. unsigned int row = pos / 64 + 1, col = pos % 64 + 1, i = 0;
  227. buf[i++] = 0x1B; buf[i++] = '[';
  228. buf[i++] = (char)('0' + row / 10);
  229. buf[i++] = (char)('0' + row % 10);
  230. buf[i++] = ';';
  231. buf[i++] = (char)('0' + col / 10);
  232. buf[i++] = (char)('0' + col % 10);
  233. buf[i++] = 'H'; buf[i] = '\0';
  234. io_putstr(buf);
  235. return;
  236. }
  237. screen_set_cursor(io_current_screen(), pos);
  238. }
  239. void io_cursor_show_block(void)
  240. {
  241. struct text_screen *s;
  242. if (io_tty())
  243. return;
  244. s = io_current_screen();
  245. s->cursor_visible = 1;
  246. screen_damage_cursor(s);
  247. }
  248. void io_cursor_hide_block(void)
  249. {
  250. struct text_screen *s;
  251. if (io_tty())
  252. return;
  253. s = io_current_screen();
  254. s->cursor_visible = 0;
  255. screen_damage_cursor(s);
  256. }
  257. void io_vga_write(unsigned int pos, unsigned short value)
  258. {
  259. if (io_tty())
  260. return;
  261. screen_put_at(io_current_screen(), pos, value);
  262. }
  263. void io_drain_keyboard(void)
  264. {
  265. while (inb(KB_STATUS) & 1)
  266. (void)inb(KB_DATA);
  267. kbd_head = kbd_tail = 0;
  268. kbd_extended = 0;
  269. repeat_active = 0;
  270. }
  271. static void kbd_repeat_start(int key, unsigned int ext, unsigned int sc)
  272. {
  273. repeat_key = key;
  274. repeat_ext = ext;
  275. repeat_sc = sc;
  276. repeat_at = get_ticks_ms() + KBD_REPEAT_DELAY_MS;
  277. repeat_active = 1;
  278. }
  279. static void kbd_repeat_stop(unsigned int ext, unsigned int sc)
  280. {
  281. if (repeat_active && repeat_ext == ext && repeat_sc == sc)
  282. repeat_active = 0;
  283. }
  284. /* Return a synthetic repeat event when the held key is due. Called only when
  285. * the real scancode queue is empty, so a pending break code is decoded first
  286. * and cancels the repeat before it can leak through. */
  287. static int kbd_repeat_poll(void)
  288. {
  289. unsigned int now;
  290. if (!repeat_active)
  291. return 0;
  292. now = get_ticks_ms();
  293. if (now - repeat_at > 0x7FFFFFFFu) /* signed deadline comparison */
  294. return 0;
  295. repeat_at = now + KBD_REPEAT_RATE_MS;
  296. return repeat_key;
  297. }
  298. /* Process one scancode byte; shared by io_poll_key and io_poll_key_with_hw. */
  299. static int kbd_process_byte(unsigned char sc)
  300. {
  301. int key;
  302. if (sc == 0xE0) {
  303. kbd_extended = 1;
  304. return 0;
  305. }
  306. if (kbd_extended) {
  307. kbd_extended = 0;
  308. if (sc & 0x80) {
  309. kbd_repeat_stop(1, sc & 0x7F);
  310. if (sc == 0x9D) /* right Ctrl release */
  311. ctrl_pressed = 0;
  312. return 0; /* key release */
  313. }
  314. if (sc == 0x1D) { /* right Ctrl */
  315. ctrl_pressed = 1;
  316. return 0;
  317. }
  318. if (sc == 0x4B)
  319. key = ctrl_pressed ? IO_KEY_CTRL_LEFT : IO_KEY_LEFT;
  320. else if (sc == 0x4D)
  321. key = ctrl_pressed ? IO_KEY_CTRL_RIGHT : IO_KEY_RIGHT;
  322. else if (sc == 0x48)
  323. key = IO_KEY_UP;
  324. else if (sc == 0x50)
  325. key = IO_KEY_DOWN;
  326. else if (sc == 0x49)
  327. key = IO_KEY_PAGE_UP;
  328. else if (sc == 0x51)
  329. key = IO_KEY_PAGE_DOWN;
  330. else if (sc == 0x47)
  331. key = IO_KEY_HOME;
  332. else if (sc == 0x4F)
  333. key = IO_KEY_END;
  334. else if (sc == 0x53)
  335. key = ctrl_pressed ? IO_KEY_CTRL_DELETE : IO_KEY_DELETE;
  336. else
  337. return 0;
  338. kbd_repeat_start(key, 1, sc);
  339. return key;
  340. }
  341. if (sc & 0x80) {
  342. if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
  343. if (sc == 0x9D) ctrl_pressed = 0;
  344. kbd_repeat_stop(0, sc & 0x7F);
  345. return 0;
  346. }
  347. if (sc == 0x2A || sc == 0x36) {
  348. shift_pressed = 1;
  349. return 0;
  350. }
  351. if (sc == 0x1D) {
  352. ctrl_pressed = 1;
  353. return 0;
  354. }
  355. if (sc == 0x0E && ctrl_pressed) {
  356. /* Ctrl+Backspace: delete the previous word. */
  357. kbd_repeat_start(IO_KEY_CTRL_BACKSPACE, 0, sc);
  358. return IO_KEY_CTRL_BACKSPACE;
  359. }
  360. {
  361. char c = (char)scancode_to_ascii[sc];
  362. if (c) {
  363. c = apply_shift(c);
  364. if (ctrl_pressed) {
  365. /* Ctrl+letter -> control character (e.g. Ctrl+C -> 0x03). */
  366. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
  367. key = (unsigned char)(c & 0x1F);
  368. else
  369. return 0;
  370. } else {
  371. key = (unsigned char)c;
  372. }
  373. kbd_repeat_start(key, 0, sc);
  374. return key;
  375. }
  376. }
  377. return 0;
  378. }
  379. int io_poll_key(void)
  380. {
  381. unsigned char sc;
  382. if (!kbd_buf_get(&sc))
  383. return kbd_repeat_poll();
  384. return kbd_process_byte(sc);
  385. }
  386. /* Like io_poll_key but also reads from port 0x60 when buffer is empty.
  387. For use by full-screen programs (e.g. space.bin) where IRQ may be missed on
  388. real HW. Only reads when a keyboard byte is actually pending (OBF set, AUX
  389. clear); the status+data pair is read atomically wrt IRQ1. */
  390. int io_poll_key_with_hw(void)
  391. {
  392. unsigned char sc = 0;
  393. unsigned int flags;
  394. int got = 0;
  395. if (kbd_tail != kbd_head) {
  396. if (!kbd_buf_get(&sc))
  397. return 0;
  398. return kbd_process_byte(sc);
  399. }
  400. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  401. {
  402. unsigned char st = inb(KB_STATUS);
  403. if ((st & 0x01) && !(st & 0x20)) {
  404. sc = inb(KB_DATA);
  405. got = 1;
  406. }
  407. }
  408. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  409. if (!got)
  410. return kbd_repeat_poll();
  411. return kbd_process_byte(sc);
  412. }
  413. /* Queue a scancode from a non-IRQ source (the USB keyboard backend); the
  414. normal io_poll_key/io_poll_key_with_hw path then decodes it. Interrupts
  415. are masked while touching the ring so an IRQ1 producer cannot corrupt it. */
  416. void io_inject_scancode(unsigned char sc)
  417. {
  418. unsigned int flags;
  419. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  420. {
  421. unsigned int next = (kbd_head + 1) % KBD_BUF_SIZE;
  422. if (next != kbd_tail) {
  423. kbd_buf[kbd_head] = sc;
  424. kbd_head = next;
  425. }
  426. }
  427. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  428. }
  429. static int kbd_raw;
  430. void io_set_raw_keyboard(int on)
  431. {
  432. kbd_raw = on;
  433. }
  434. int io_keyboard_raw(void)
  435. {
  436. return kbd_raw;
  437. }