io.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #define VGA_ATTR 0x0A
  9. static unsigned int io_cols = 80;
  10. static unsigned int io_rows = 25;
  11. #define KB_DATA 0x60
  12. #define KB_STATUS 0x64
  13. static inline unsigned char inb(unsigned short port)
  14. {
  15. unsigned char v;
  16. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  17. return v;
  18. }
  19. static inline void outb(unsigned short port, unsigned char v)
  20. {
  21. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  22. }
  23. /* Scancode set 1 (XT) -> ASCII. 0 = ignore. */
  24. static const unsigned char scancode_to_ascii[256] __attribute__((section(".rodata"))) = {
  25. [0x01] = 0x1B, /* Escape */
  26. [0x02] = '1', [0x03] = '2', [0x04] = '3', [0x05] = '4', [0x06] = '5',
  27. [0x07] = '6', [0x08] = '7', [0x09] = '8', [0x0A] = '9', [0x0B] = '0',
  28. [0x0C] = '-', [0x0D] = '=', [0x0E] = '\b', [0x0F] = '\t',
  29. [0x10] = 'q', [0x11] = 'w', [0x12] = 'e', [0x13] = 'r', [0x14] = 't',
  30. [0x15] = 'y', [0x16] = 'u', [0x17] = 'i', [0x18] = 'o', [0x19] = 'p',
  31. [0x1A] = '[', [0x1B] = ']', [0x1C] = '\n',
  32. [0x1E] = 'a', [0x1F] = 's', [0x20] = 'd', [0x21] = 'f', [0x22] = 'g',
  33. [0x23] = 'h', [0x24] = 'j', [0x25] = 'k', [0x26] = 'l', [0x27] = ';',
  34. [0x28] = '\'', [0x29] = '`',
  35. [0x2B] = '\\', [0x2C] = 'z', [0x2D] = 'x', [0x2E] = 'c', [0x2F] = 'v',
  36. [0x30] = 'b', [0x31] = 'n', [0x32] = 'm', [0x33] = ',', [0x34] = '.',
  37. [0x35] = '/', [0x39] = ' ',
  38. };
  39. static int shift_pressed;
  40. static unsigned char last_scancode;
  41. static int kbd_extended; /* 1 after 0xE0, next byte is extended scancode */
  42. #define KBD_BUF_SIZE 16
  43. static volatile unsigned char kbd_buf[KBD_BUF_SIZE];
  44. static volatile unsigned int kbd_head, kbd_tail;
  45. /* Fallback screen for slot 0 / boot / exception output (not rendered by WM). */
  46. static struct text_screen io_fallback;
  47. static struct text_screen *io_current_screen(void)
  48. {
  49. struct text_screen *s = procs[current_pid].screen;
  50. return s ? s : &io_fallback;
  51. }
  52. void kbd_irq(void)
  53. {
  54. unsigned char sc = inb(KB_DATA);
  55. unsigned int next = (kbd_head + 1) % KBD_BUF_SIZE;
  56. if (next != kbd_tail) {
  57. kbd_buf[kbd_head] = sc;
  58. kbd_head = next;
  59. }
  60. outb(0x20, 0x20);
  61. }
  62. static int kbd_buf_get(unsigned char *sc)
  63. {
  64. if (kbd_tail == kbd_head)
  65. return 0;
  66. *sc = kbd_buf[kbd_tail];
  67. kbd_tail = (kbd_tail + 1) % KBD_BUF_SIZE;
  68. return 1;
  69. }
  70. static char apply_shift(char c)
  71. {
  72. if (!shift_pressed) return c;
  73. if (c >= 'a' && c <= 'z') return (char)(c - 32);
  74. switch ((unsigned char)c) {
  75. case '1': return '!'; case '2': return '@'; case '3': return '#';
  76. case '4': return '$'; case '5': return '%'; case '6': return '^';
  77. case '7': return '&'; case '8': return '*'; case '9': return '(';
  78. case '0': return ')'; case '-': return '_'; case '=': return '+';
  79. case '[': return '{'; case ']': return '}'; case ';': return ':';
  80. case '\'': return '"'; case '`': return '~'; case '\\': return '|';
  81. case ',': return '<'; case '.': return '>'; case '/': return '?';
  82. default: return c;
  83. }
  84. }
  85. unsigned int io_get_cols(void)
  86. {
  87. return io_cols;
  88. }
  89. unsigned int io_get_rows(void)
  90. {
  91. return io_rows;
  92. }
  93. void io_init(void)
  94. {
  95. fbtext_init();
  96. io_cols = fbtext_get_cols();
  97. io_rows = fbtext_get_rows();
  98. screen_init(&io_fallback);
  99. io_drain_keyboard();
  100. }
  101. void io_clear_screen(void)
  102. {
  103. screen_clear(io_current_screen());
  104. }
  105. int io_did_scroll(void)
  106. {
  107. return screen_did_scroll(io_current_screen());
  108. }
  109. void io_putchar(char c)
  110. {
  111. screen_putchar(io_current_screen(), c);
  112. }
  113. void io_putstr(const char *s)
  114. {
  115. screen_putstr(io_current_screen(), s);
  116. }
  117. void io_puthex(unsigned int n)
  118. {
  119. screen_puthex(io_current_screen(), n);
  120. }
  121. unsigned int io_get_cursor(void)
  122. {
  123. return screen_get_cursor(io_current_screen());
  124. }
  125. void io_set_cursor(unsigned int pos)
  126. {
  127. screen_set_cursor(io_current_screen(), pos);
  128. }
  129. void io_cursor_show_block(void)
  130. {
  131. struct text_screen *s = io_current_screen();
  132. s->cursor_visible = 1;
  133. s->dirty = 1;
  134. }
  135. void io_cursor_hide_block(void)
  136. {
  137. struct text_screen *s = io_current_screen();
  138. s->cursor_visible = 0;
  139. s->dirty = 1;
  140. }
  141. void io_vga_write(unsigned int pos, unsigned short value)
  142. {
  143. screen_put_at(io_current_screen(), pos, value);
  144. }
  145. void io_drain_keyboard(void)
  146. {
  147. while (inb(KB_STATUS) & 1)
  148. (void)inb(KB_DATA);
  149. kbd_head = kbd_tail = 0;
  150. kbd_extended = 0;
  151. }
  152. /* Process one scancode byte; shared by io_poll_key and io_poll_key_with_hw. */
  153. static int kbd_process_byte(unsigned char sc)
  154. {
  155. if (sc == 0xE0) {
  156. kbd_extended = 1;
  157. return 0;
  158. }
  159. if (kbd_extended) {
  160. kbd_extended = 0;
  161. if (sc & 0x80)
  162. return 0; /* key release */
  163. if (sc == 0x4B) return IO_KEY_LEFT;
  164. if (sc == 0x4D) return IO_KEY_RIGHT;
  165. if (sc == 0x48) return IO_KEY_UP;
  166. if (sc == 0x50) return IO_KEY_DOWN;
  167. return 0;
  168. }
  169. if (sc & 0x80) {
  170. if (sc == 0xAA || sc == 0xB6) shift_pressed = 0;
  171. return 0;
  172. }
  173. if (sc == 0x2A || sc == 0x36) {
  174. shift_pressed = 1;
  175. return 0;
  176. }
  177. {
  178. char c = (char)scancode_to_ascii[sc];
  179. if (c) {
  180. last_scancode = sc;
  181. return (unsigned char)apply_shift(c);
  182. }
  183. }
  184. return 0;
  185. }
  186. int io_poll_key(void)
  187. {
  188. unsigned char sc;
  189. if (!kbd_buf_get(&sc))
  190. return 0;
  191. return kbd_process_byte(sc);
  192. }
  193. /* Like io_poll_key but also reads from port 0x60 when buffer is empty.
  194. For use by full-screen programs (e.g. space.bin) where IRQ may be missed on real HW. */
  195. int io_poll_key_with_hw(void)
  196. {
  197. unsigned char sc;
  198. if (kbd_tail == kbd_head) {
  199. if (!(inb(KB_STATUS) & 1))
  200. return 0;
  201. sc = inb(KB_DATA);
  202. } else {
  203. if (!kbd_buf_get(&sc))
  204. return 0;
  205. }
  206. return kbd_process_byte(sc);
  207. }
  208. unsigned char io_last_scancode(void)
  209. {
  210. return last_scancode;
  211. }