io.c 14 KB

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