io.c 13 KB

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