io.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. #define KBD_BUF_SIZE 16
  45. static volatile unsigned char kbd_buf[KBD_BUF_SIZE];
  46. static volatile unsigned int kbd_head, kbd_tail;
  47. /* Fallback screen for slot 0 / boot / exception output (not rendered by WM). */
  48. static struct text_screen io_fallback;
  49. static struct text_screen *io_current_screen(void)
  50. {
  51. struct text_screen *s = procs[current_pid].screen;
  52. return s ? s : &io_fallback;
  53. }
  54. /* IRQ1: read the controller status first and only read data when the output
  55. * buffer is full. Some USB-legacy SMM implementations keep raising IRQ1:
  56. * reading 0x60 with no byte present does not acknowledge anything, and a
  57. * level-triggered line then storms. The loop is bounded, and if the rate
  58. * gets absurd we mask IRQ1 and let the WM fall back to polling. */
  59. static unsigned int kbd_win_start;
  60. static unsigned int kbd_win_count;
  61. static volatile int kbd_irq_masked;
  62. int io_keyboard_irq_masked(void)
  63. {
  64. return kbd_irq_masked;
  65. }
  66. void kbd_irq(void)
  67. {
  68. unsigned int guard = 0;
  69. unsigned int now = get_ticks_ms();
  70. unsigned char st;
  71. if (now - kbd_win_start >= 1000) {
  72. kbd_win_start = now;
  73. kbd_win_count = 0;
  74. }
  75. kbd_win_count++;
  76. if (!kbd_irq_masked && kbd_win_count > 200) {
  77. /* IRQ1 storm; mask it (master PIC) and poll instead. */
  78. outb(0x21, (unsigned char)(inb(0x21) | 0x02));
  79. kbd_irq_masked = 1;
  80. }
  81. while (guard++ < 32) {
  82. st = inb(KB_STATUS);
  83. if (!(st & 0x01))
  84. break;
  85. {
  86. unsigned char sc = inb(KB_DATA);
  87. if (st & 0x20) {
  88. /* AUX (mouse) byte: not ours, drop it. */
  89. continue;
  90. }
  91. {
  92. unsigned int next = (kbd_head + 1) % KBD_BUF_SIZE;
  93. if (next != kbd_tail) {
  94. kbd_buf[kbd_head] = sc;
  95. kbd_head = next;
  96. }
  97. }
  98. }
  99. }
  100. outb(0x20, 0x20);
  101. }
  102. static int kbd_buf_get(unsigned char *sc)
  103. {
  104. if (kbd_tail == kbd_head)
  105. return 0;
  106. *sc = kbd_buf[kbd_tail];
  107. kbd_tail = (kbd_tail + 1) % KBD_BUF_SIZE;
  108. return 1;
  109. }
  110. static char apply_shift(char c)
  111. {
  112. if (!shift_pressed) return c;
  113. if (c >= 'a' && c <= 'z') return (char)(c - 32);
  114. switch ((unsigned char)c) {
  115. case '1': return '!'; case '2': return '@'; case '3': return '#';
  116. case '4': return '$'; case '5': return '%'; case '6': return '^';
  117. case '7': return '&'; case '8': return '*'; case '9': return '(';
  118. case '0': return ')'; case '-': return '_'; case '=': return '+';
  119. case '[': return '{'; case ']': return '}'; case ';': return ':';
  120. case '\'': return '"'; case '`': return '~'; case '\\': return '|';
  121. case ',': return '<'; case '.': return '>'; case '/': return '?';
  122. default: return c;
  123. }
  124. }
  125. unsigned int io_get_cols(void)
  126. {
  127. return io_cols;
  128. }
  129. unsigned int io_get_rows(void)
  130. {
  131. return io_rows;
  132. }
  133. void io_init(void)
  134. {
  135. fbtext_init();
  136. io_cols = fbtext_get_cols();
  137. io_rows = fbtext_get_rows();
  138. screen_init(&io_fallback);
  139. io_drain_keyboard();
  140. }
  141. /* A process with a tty writes there; the kernel terminal/boot fallback keeps
  142. * the old cell-screen path until the ring-3 terminal is running. */
  143. static int io_tty(void)
  144. {
  145. int id = procs[current_pid].tty;
  146. return tty_valid(id) ? id : 0;
  147. }
  148. void io_clear_screen(void)
  149. {
  150. int id = io_tty();
  151. if (id) {
  152. tty_write_str(id, "\x1b[2J\x1b[H");
  153. return;
  154. }
  155. screen_clear(io_current_screen());
  156. }
  157. int io_did_scroll(void)
  158. {
  159. if (io_tty())
  160. return 0;
  161. return screen_did_scroll(io_current_screen());
  162. }
  163. void io_putchar(char c)
  164. {
  165. int id = io_tty();
  166. if (id) {
  167. tty_write(id, &c, 1);
  168. return;
  169. }
  170. screen_putchar(io_current_screen(), c);
  171. }
  172. void io_putstr(const char *s)
  173. {
  174. int id = io_tty();
  175. if (id) {
  176. tty_write_str(id, s);
  177. return;
  178. }
  179. screen_putstr(io_current_screen(), s);
  180. }
  181. void io_puthex(unsigned int n)
  182. {
  183. if (io_tty()) {
  184. static const char hex[] = "0123456789ABCDEF";
  185. char buf[9];
  186. unsigned int i = 8;
  187. buf[8] = '\0';
  188. do {
  189. buf[--i] = hex[n & 0xF];
  190. n >>= 4;
  191. } while (i > 0);
  192. io_putstr(buf);
  193. return;
  194. }
  195. screen_puthex(io_current_screen(), n);
  196. }
  197. unsigned int io_get_cursor(void)
  198. {
  199. if (io_tty())
  200. return 0;
  201. return screen_get_cursor(io_current_screen());
  202. }
  203. void io_set_cursor(unsigned int pos)
  204. {
  205. if (io_tty()) {
  206. char buf[16];
  207. unsigned int row = pos / 64 + 1, col = pos % 64 + 1, i = 0;
  208. buf[i++] = 0x1B; buf[i++] = '[';
  209. buf[i++] = (char)('0' + row / 10);
  210. buf[i++] = (char)('0' + row % 10);
  211. buf[i++] = ';';
  212. buf[i++] = (char)('0' + col / 10);
  213. buf[i++] = (char)('0' + col % 10);
  214. buf[i++] = 'H'; buf[i] = '\0';
  215. io_putstr(buf);
  216. return;
  217. }
  218. screen_set_cursor(io_current_screen(), pos);
  219. }
  220. void io_cursor_show_block(void)
  221. {
  222. struct text_screen *s;
  223. if (io_tty())
  224. return;
  225. s = io_current_screen();
  226. s->cursor_visible = 1;
  227. screen_damage_cursor(s);
  228. }
  229. void io_cursor_hide_block(void)
  230. {
  231. struct text_screen *s;
  232. if (io_tty())
  233. return;
  234. s = io_current_screen();
  235. s->cursor_visible = 0;
  236. screen_damage_cursor(s);
  237. }
  238. void io_vga_write(unsigned int pos, unsigned short value)
  239. {
  240. if (io_tty())
  241. return;
  242. screen_put_at(io_current_screen(), pos, value);
  243. }
  244. void io_drain_keyboard(void)
  245. {
  246. while (inb(KB_STATUS) & 1)
  247. (void)inb(KB_DATA);
  248. kbd_head = kbd_tail = 0;
  249. kbd_extended = 0;
  250. }
  251. /* Process one scancode byte; shared by io_poll_key and io_poll_key_with_hw. */
  252. static int kbd_process_byte(unsigned char sc)
  253. {
  254. if (sc == 0xE0) {
  255. kbd_extended = 1;
  256. return 0;
  257. }
  258. if (kbd_extended) {
  259. kbd_extended = 0;
  260. if (sc & 0x80)
  261. return 0; /* key release */
  262. if (sc == 0x4B) return IO_KEY_LEFT;
  263. if (sc == 0x4D) return IO_KEY_RIGHT;
  264. if (sc == 0x48) return IO_KEY_UP;
  265. if (sc == 0x50) return IO_KEY_DOWN;
  266. return 0;
  267. }
  268. if (sc & 0x80) {
  269. if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
  270. if (sc == 0x9D) ctrl_pressed = 0;
  271. return 0;
  272. }
  273. if (sc == 0x2A || sc == 0x36) {
  274. shift_pressed = 1;
  275. return 0;
  276. }
  277. if (sc == 0x1D) {
  278. ctrl_pressed = 1;
  279. return 0;
  280. }
  281. {
  282. char c = (char)scancode_to_ascii[sc];
  283. if (c) {
  284. c = apply_shift(c);
  285. if (ctrl_pressed) {
  286. /* Ctrl+letter -> control character (e.g. Ctrl+C -> 0x03). */
  287. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
  288. return (unsigned char)(c & 0x1F);
  289. return 0;
  290. }
  291. return (unsigned char)c;
  292. }
  293. }
  294. return 0;
  295. }
  296. int io_poll_key(void)
  297. {
  298. unsigned char sc;
  299. if (!kbd_buf_get(&sc))
  300. return 0;
  301. return kbd_process_byte(sc);
  302. }
  303. /* Like io_poll_key but also reads from port 0x60 when buffer is empty.
  304. For use by full-screen programs (e.g. space.bin) where IRQ may be missed on
  305. real HW. Only reads when a keyboard byte is actually pending (OBF set, AUX
  306. clear); the status+data pair is read atomically wrt IRQ1. */
  307. int io_poll_key_with_hw(void)
  308. {
  309. unsigned char sc = 0;
  310. unsigned int flags;
  311. int got = 0;
  312. if (kbd_tail != kbd_head) {
  313. if (!kbd_buf_get(&sc))
  314. return 0;
  315. return kbd_process_byte(sc);
  316. }
  317. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  318. {
  319. unsigned char st = inb(KB_STATUS);
  320. if ((st & 0x01) && !(st & 0x20)) {
  321. sc = inb(KB_DATA);
  322. got = 1;
  323. }
  324. }
  325. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  326. if (!got)
  327. return 0;
  328. return kbd_process_byte(sc);
  329. }
  330. /* Queue a scancode from a non-IRQ source (the USB keyboard backend); the
  331. normal io_poll_key/io_poll_key_with_hw path then decodes it. Interrupts
  332. are masked while touching the ring so an IRQ1 producer cannot corrupt it. */
  333. void io_inject_scancode(unsigned char sc)
  334. {
  335. unsigned int flags;
  336. __asm__ volatile ("pushfl; popl %0; cli" : "=r"(flags));
  337. {
  338. unsigned int next = (kbd_head + 1) % KBD_BUF_SIZE;
  339. if (next != kbd_tail) {
  340. kbd_buf[kbd_head] = sc;
  341. kbd_head = next;
  342. }
  343. }
  344. __asm__ volatile ("pushl %0; popfl" : : "r"(flags));
  345. }
  346. static int kbd_raw;
  347. void io_set_raw_keyboard(int on)
  348. {
  349. kbd_raw = on;
  350. }
  351. int io_keyboard_raw(void)
  352. {
  353. return kbd_raw;
  354. }