io.c 14 KB

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