io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. return tty_valid(id) ? id : 0;
  211. }
  212. void io_putchar(char c)
  213. {
  214. int id = io_tty();
  215. if (id) {
  216. tty_write(id, &c, 1);
  217. return;
  218. }
  219. screen_putchar(io_current_screen(), c);
  220. }
  221. void io_putstr(const char *s)
  222. {
  223. int id = io_tty();
  224. if (id) {
  225. tty_write_str(id, s);
  226. return;
  227. }
  228. screen_putstr(io_current_screen(), s);
  229. }
  230. void io_puthex(unsigned int n)
  231. {
  232. if (io_tty()) {
  233. static const char hex[] = "0123456789ABCDEF";
  234. char buf[9];
  235. unsigned int i = 8;
  236. buf[8] = '\0';
  237. do {
  238. buf[--i] = hex[n & 0xF];
  239. n >>= 4;
  240. } while (i > 0);
  241. io_putstr(buf);
  242. return;
  243. }
  244. screen_puthex(io_current_screen(), n);
  245. }
  246. void io_drain_keyboard(void)
  247. {
  248. while (inb(KB_STATUS) & 1)
  249. (void)inb(KB_DATA);
  250. kbd_head = kbd_tail = 0;
  251. kbd_extended = 0;
  252. repeat_active = 0;
  253. }
  254. static void kbd_repeat_start(int key, unsigned int ext, unsigned int sc)
  255. {
  256. repeat_key = key;
  257. repeat_ext = ext;
  258. repeat_sc = sc;
  259. repeat_at = get_ticks_ms() + KBD_REPEAT_DELAY_MS;
  260. repeat_active = 1;
  261. }
  262. static void kbd_repeat_stop(unsigned int ext, unsigned int sc)
  263. {
  264. if (repeat_active && repeat_ext == ext && repeat_sc == sc)
  265. repeat_active = 0;
  266. }
  267. /* Return a synthetic repeat event when the held key is due. Called only when
  268. * the real scancode queue is empty, so a pending break code is decoded first
  269. * and cancels the repeat before it can leak through. */
  270. static int kbd_repeat_poll(void)
  271. {
  272. unsigned int now;
  273. if (!repeat_active)
  274. return 0;
  275. now = get_ticks_ms();
  276. if (now - repeat_at > 0x7FFFFFFFu) /* signed deadline comparison */
  277. return 0;
  278. repeat_at = now + KBD_REPEAT_RATE_MS;
  279. return repeat_key;
  280. }
  281. /* Decode one scancode byte; shared by io_poll_key and io_poll_key_with_hw. */
  282. static int kbd_process_byte_inner(unsigned char sc)
  283. {
  284. int key;
  285. if (sc == 0xE0) {
  286. kbd_extended = 1;
  287. return 0;
  288. }
  289. if (kbd_extended) {
  290. kbd_extended = 0;
  291. if (sc & 0x80) {
  292. kbd_repeat_stop(1, sc & 0x7F);
  293. if (sc == 0x9D) /* right Ctrl release */
  294. ctrl_pressed = 0;
  295. return 0; /* key release */
  296. }
  297. if (sc == 0x1D) { /* right Ctrl */
  298. ctrl_pressed = 1;
  299. return 0;
  300. }
  301. if (sc == 0x14 || sc == 0x15) {
  302. /* Standard AT brightness down/up (E0 14 / E0 15);
  303. * ChromeOS EC action-key boards send these. */
  304. if (backlight_present())
  305. backlight_step(sc == 0x14 ? -1 : 1);
  306. return 0;
  307. }
  308. if (sc == 0x4B)
  309. key = ctrl_pressed ? IO_KEY_CTRL_LEFT : IO_KEY_LEFT;
  310. else if (sc == 0x4D)
  311. key = ctrl_pressed ? IO_KEY_CTRL_RIGHT : IO_KEY_RIGHT;
  312. else if (sc == 0x48)
  313. key = IO_KEY_UP;
  314. else if (sc == 0x50)
  315. key = IO_KEY_DOWN;
  316. else if (sc == 0x49)
  317. key = IO_KEY_PAGE_UP;
  318. else if (sc == 0x51)
  319. key = IO_KEY_PAGE_DOWN;
  320. else if (sc == 0x47)
  321. key = IO_KEY_HOME;
  322. else if (sc == 0x4F)
  323. key = IO_KEY_END;
  324. else if (sc == 0x53)
  325. key = ctrl_pressed ? IO_KEY_CTRL_DELETE : IO_KEY_DELETE;
  326. else
  327. return 0;
  328. kbd_repeat_start(key, 1, sc);
  329. return key;
  330. }
  331. if (sc & 0x80) {
  332. if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
  333. if (sc == 0x9D) ctrl_pressed = 0;
  334. kbd_repeat_stop(0, sc & 0x7F);
  335. return 0;
  336. }
  337. /* A ChromeOS EC keyboard sends the action-row brightness keys as plain
  338. * F6/F7 (the older standard table). Only claim them when a Chrome EC
  339. * is present, so a regular Braswell laptop keeps its function keys. */
  340. if ((sc == 0x40 || sc == 0x41) && backlight_present() &&
  341. cros_ec_present()) {
  342. backlight_step(sc == 0x40 ? -1 : 1);
  343. return 0;
  344. }
  345. if (sc == 0x2A || sc == 0x36) {
  346. shift_pressed = 1;
  347. return 0;
  348. }
  349. if (sc == 0x1D) {
  350. ctrl_pressed = 1;
  351. return 0;
  352. }
  353. if (sc == 0x0E && ctrl_pressed) {
  354. /* Ctrl+Backspace: delete the previous word. */
  355. kbd_repeat_start(IO_KEY_CTRL_BACKSPACE, 0, sc);
  356. return IO_KEY_CTRL_BACKSPACE;
  357. }
  358. {
  359. char c = (char)scancode_to_ascii[sc];
  360. if (c) {
  361. c = apply_shift(c);
  362. if (ctrl_pressed) {
  363. /* Ctrl+letter -> control character (e.g. Ctrl+C -> 0x03). */
  364. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
  365. key = (unsigned char)(c & 0x1F);
  366. else
  367. return 0;
  368. } else {
  369. key = (unsigned char)c;
  370. }
  371. kbd_repeat_start(key, 0, sc);
  372. return key;
  373. }
  374. }
  375. return 0;
  376. }
  377. /* Tap every byte after decoding, so `keytest` sees the raw wire sequence
  378. * (including E0 prefixes, releases and keys the decoder ignores). */
  379. static int kbd_process_byte(unsigned char sc)
  380. {
  381. unsigned char ext = (unsigned char)kbd_extended;
  382. unsigned char kind;
  383. int key;
  384. key = kbd_process_byte_inner(sc);
  385. if (sc == 0xE0)
  386. kind = 2;
  387. else if (sc & 0x80)
  388. kind = 1;
  389. else
  390. kind = 0;
  391. kbd_tap_push(sc, ext, kind, key);
  392. return key;
  393. }
  394. int io_poll_key(void)
  395. {
  396. unsigned char sc;
  397. if (!kbd_buf_get(&sc))
  398. return kbd_repeat_poll();
  399. return kbd_process_byte(sc);
  400. }
  401. /* Like io_poll_key but also reads from port 0x60 when buffer is empty.
  402. For use by callers that poll the keyboard without an IRQ path (the WM's
  403. boot-console fallback and format's confirmation prompt) where an IRQ may be
  404. missed on real HW. Only reads when a keyboard byte is actually pending
  405. (OBF set, AUX clear); the status+data pair is read atomically wrt IRQ1. */
  406. int io_poll_key_with_hw(void)
  407. {
  408. unsigned char sc = 0;
  409. unsigned int flags;
  410. int got = 0;
  411. if (kbd_tail != kbd_head) {
  412. if (!kbd_buf_get(&sc))
  413. return 0;
  414. return kbd_process_byte(sc);
  415. }
  416. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  417. {
  418. unsigned char st = inb(KB_STATUS);
  419. if ((st & 0x01) && !(st & 0x20)) {
  420. sc = inb(KB_DATA);
  421. got = 1;
  422. }
  423. }
  424. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  425. if (!got)
  426. return kbd_repeat_poll();
  427. return kbd_process_byte(sc);
  428. }
  429. /* Queue a scancode from a non-IRQ source (the USB keyboard backend); the
  430. normal io_poll_key/io_poll_key_with_hw path then decodes it. Interrupts
  431. are masked while touching the ring so an IRQ1 producer cannot corrupt it. */
  432. void io_inject_scancode(unsigned char sc)
  433. {
  434. unsigned int flags;
  435. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  436. {
  437. unsigned int next = (kbd_head + 1) % KBD_BUF_SIZE;
  438. if (next != kbd_tail) {
  439. kbd_buf[kbd_head] = sc;
  440. kbd_head = next;
  441. }
  442. }
  443. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  444. }
  445. static int kbd_raw;
  446. void io_set_raw_keyboard(int on)
  447. {
  448. kbd_raw = on;
  449. }
  450. int io_keyboard_raw(void)
  451. {
  452. return kbd_raw;
  453. }