io.c 11 KB

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