kbd.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "../port/error.h"
  8. enum {
  9. Data= 0x60, /* data port */
  10. Status= 0x64, /* status port */
  11. Inready= 0x01, /* input character ready */
  12. Outbusy= 0x02, /* output busy */
  13. Sysflag= 0x04, /* system flag */
  14. Cmddata= 0x08, /* cmd==0, data==1 */
  15. Inhibit= 0x10, /* keyboard/mouse inhibited */
  16. Minready= 0x20, /* mouse character ready */
  17. Rtimeout= 0x40, /* general timeout */
  18. Parity= 0x80,
  19. Cmd= 0x64, /* command port (write only) */
  20. Spec= 0x80,
  21. PF= Spec|0x20, /* num pad function key */
  22. View= Spec|0x00, /* view (shift window up) */
  23. KF= 0xF000, /* function key (begin Unicode private space) */
  24. Shift= Spec|0x60,
  25. Break= Spec|0x61,
  26. Ctrl= Spec|0x62,
  27. Latin= Spec|0x63,
  28. Caps= Spec|0x64,
  29. Num= Spec|0x65,
  30. Middle= Spec|0x66,
  31. No= 0x00, /* peter */
  32. Home= KF|13,
  33. Up= KF|14,
  34. Pgup= KF|15,
  35. Print= KF|16,
  36. Left= KF|17,
  37. Right= KF|18,
  38. End= '\r',
  39. Down= View,
  40. Pgdown= KF|19,
  41. Ins= KF|20,
  42. Scroll= KF|21,
  43. Del= 0x7F,
  44. };
  45. /*
  46. * The codes at 0x79 and 0x81 are produed by the PFU Happy Hacking keyboard.
  47. * A 'standard' keyboard doesn't produce anything above 0x58.
  48. */
  49. Rune kbtab[] =
  50. {
  51. [0x00] No, 0x1b, '1', '2', '3', '4', '5', '6',
  52. [0x08] '7', '8', '9', '0', '-', '=', '\b', '\t',
  53. [0x10] 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  54. [0x18] 'o', 'p', '[', ']', '\n', Ctrl, 'a', 's',
  55. [0x20] 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
  56. [0x28] '\'', '`', Shift, '\\', 'z', 'x', 'c', 'v',
  57. [0x30] 'b', 'n', 'm', ',', '.', '/', Shift, '*',
  58. [0x38] Latin, ' ', Ctrl, KF|1, KF|2, KF|3, KF|4, KF|5,
  59. [0x40] KF|6, KF|7, KF|8, KF|9, KF|10, Num, Scroll, '7',
  60. [0x48] '8', '9', '-', '4', '5', '6', '+', '1',
  61. [0x50] '2', '3', '0', '.', No, No, No, KF|11,
  62. [0x58] KF|12, No, No, No, No, No, No, No,
  63. [0x60] No, No, No, No, No, No, No, No,
  64. [0x68] No, No, No, No, No, No, No, No,
  65. [0x70] No, No, No, No, No, No, No, No,
  66. [0x78] No, View, No, Up, No, No, No, No,
  67. };
  68. Rune kbtabshift[] =
  69. {
  70. [0x00] No, 0x1b, '!', '@', '#', '$', '%', '^',
  71. [0x08] '&', '*', '(', ')', '_', '+', '\b', '\t',
  72. [0x10] 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
  73. [0x18] 'O', 'P', '{', '}', '\n', Ctrl, 'A', 'S',
  74. [0x20] 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
  75. [0x28] '"', '~', Shift, '|', 'Z', 'X', 'C', 'V',
  76. [0x30] 'B', 'N', 'M', '<', '>', '?', Shift, '*',
  77. [0x38] Latin, ' ', Ctrl, KF|1, KF|2, KF|3, KF|4, KF|5,
  78. [0x40] KF|6, KF|7, KF|8, KF|9, KF|10, Num, Scroll, '7',
  79. [0x48] '8', '9', '-', '4', '5', '6', '+', '1',
  80. [0x50] '2', '3', '0', '.', No, No, No, KF|11,
  81. [0x58] KF|12, No, No, No, No, No, No, No,
  82. [0x60] No, No, No, No, No, No, No, No,
  83. [0x68] No, No, No, No, No, No, No, No,
  84. [0x70] No, No, No, No, No, No, No, No,
  85. [0x78] No, Up, No, Up, No, No, No, No,
  86. };
  87. Rune kbtabesc1[] =
  88. {
  89. [0x00] No, No, No, No, No, No, No, No,
  90. [0x08] No, No, No, No, No, No, No, No,
  91. [0x10] No, No, No, No, No, No, No, No,
  92. [0x18] No, No, No, No, '\n', Ctrl, No, No,
  93. [0x20] No, No, No, No, No, No, No, No,
  94. [0x28] No, No, Shift, No, No, No, No, No,
  95. [0x30] No, No, No, No, No, '/', No, Print,
  96. [0x38] Latin, No, No, No, No, No, No, No,
  97. [0x40] No, No, No, No, No, No, Break, Home,
  98. [0x48] Up, Pgup, No, Left, No, Right, No, End,
  99. [0x50] Down, Pgdown, Ins, Del, No, No, No, No,
  100. [0x58] No, No, No, No, No, No, No, No,
  101. [0x60] No, No, No, No, No, No, No, No,
  102. [0x68] No, No, No, No, No, No, No, No,
  103. [0x70] No, No, No, No, No, No, No, No,
  104. [0x78] No, Up, No, No, No, No, No, No,
  105. };
  106. enum
  107. {
  108. /* controller command byte */
  109. Cscs1= (1<<6), /* scan code set 1 */
  110. Cauxdis= (1<<5), /* mouse disable */
  111. Ckbddis= (1<<4), /* kbd disable */
  112. Csf= (1<<2), /* system flag */
  113. Cauxint= (1<<1), /* mouse interrupt enable */
  114. Ckbdint= (1<<0), /* kbd interrupt enable */
  115. };
  116. static Lock i8042lock;
  117. static uchar ccc;
  118. static void (*auxputc)(int, int);
  119. /*
  120. * wait for output no longer busy
  121. */
  122. static int
  123. outready(void)
  124. {
  125. int tries;
  126. for(tries = 0; (inb(Status) & Outbusy); tries++){
  127. if(tries > 500)
  128. return -1;
  129. delay(2);
  130. }
  131. return 0;
  132. }
  133. /*
  134. * wait for input
  135. */
  136. static int
  137. inready(void)
  138. {
  139. int tries;
  140. for(tries = 0; !(inb(Status) & Inready); tries++){
  141. if(tries > 500)
  142. return -1;
  143. delay(2);
  144. }
  145. return 0;
  146. }
  147. /*
  148. * ask 8042 to reset the machine
  149. */
  150. void
  151. i8042reset(void)
  152. {
  153. ushort *s = KADDR(0x472);
  154. int i, x;
  155. *s = 0x1234; /* BIOS warm-boot flag */
  156. /*
  157. * newer reset the machine command
  158. */
  159. outready();
  160. outb(Cmd, 0xFE);
  161. outready();
  162. /*
  163. * Pulse it by hand (old somewhat reliable)
  164. */
  165. x = 0xDF;
  166. for(i = 0; i < 5; i++){
  167. x ^= 1;
  168. outready();
  169. outb(Cmd, 0xD1);
  170. outready();
  171. outb(Data, x); /* toggle reset */
  172. delay(100);
  173. }
  174. }
  175. int
  176. i8042auxcmd(int cmd)
  177. {
  178. unsigned int c;
  179. int tries;
  180. c = 0;
  181. tries = 0;
  182. ilock(&i8042lock);
  183. do{
  184. if(tries++ > 2)
  185. break;
  186. if(outready() < 0)
  187. break;
  188. outb(Cmd, 0xD4);
  189. if(outready() < 0)
  190. break;
  191. outb(Data, cmd);
  192. if(outready() < 0)
  193. break;
  194. if(inready() < 0)
  195. break;
  196. c = inb(Data);
  197. } while(c == 0xFE || c == 0);
  198. iunlock(&i8042lock);
  199. if(c != 0xFA){
  200. print("i8042: %2.2ux returned to the %2.2ux command\n", c, cmd);
  201. return -1;
  202. }
  203. return 0;
  204. }
  205. /*
  206. * keyboard interrupt
  207. */
  208. static void
  209. i8042intr(Ureg*, void*)
  210. {
  211. int s, c, i;
  212. static int esc1, esc2;
  213. static int alt, caps, ctl, num, shift;
  214. static int collecting, nk;
  215. static Rune kc[5];
  216. int keyup;
  217. /*
  218. * get status
  219. */
  220. lock(&i8042lock);
  221. s = inb(Status);
  222. if(!(s&Inready)){
  223. unlock(&i8042lock);
  224. return;
  225. }
  226. /*
  227. * get the character
  228. */
  229. c = inb(Data);
  230. unlock(&i8042lock);
  231. /*
  232. * if it's the aux port...
  233. */
  234. if(s & Minready){
  235. if(auxputc != nil)
  236. auxputc(c, shift);
  237. return;
  238. }
  239. /*
  240. * e0's is the first of a 2 character sequence
  241. */
  242. if(c == 0xe0){
  243. esc1 = 1;
  244. return;
  245. } else if(c == 0xe1){
  246. esc2 = 2;
  247. return;
  248. }
  249. keyup = c&0x80;
  250. c &= 0x7f;
  251. if(c > sizeof kbtab){
  252. c |= keyup;
  253. if(c != 0xFF) /* these come fairly often: CAPSLOCK U Y */
  254. print("unknown key %ux\n", c);
  255. return;
  256. }
  257. if(esc1){
  258. c = kbtabesc1[c];
  259. esc1 = 0;
  260. } else if(esc2){
  261. esc2--;
  262. return;
  263. } else if(shift)
  264. c = kbtabshift[c];
  265. else
  266. c = kbtab[c];
  267. if(caps && c<='z' && c>='a')
  268. c += 'A' - 'a';
  269. /*
  270. * keyup only important for shifts
  271. */
  272. if(keyup){
  273. switch(c){
  274. case Latin:
  275. alt = 0;
  276. break;
  277. case Shift:
  278. shift = 0;
  279. break;
  280. case Ctrl:
  281. ctl = 0;
  282. break;
  283. }
  284. return;
  285. }
  286. /*
  287. * normal character
  288. */
  289. if(!(c & (Spec|KF))){
  290. if(ctl){
  291. if(alt && c == Del)
  292. exit(0);
  293. c &= 0x1f;
  294. }
  295. if(!collecting){
  296. kbdputc(kbdq, c);
  297. return;
  298. }
  299. kc[nk++] = c;
  300. c = latin1(kc, nk);
  301. if(c < -1) /* need more keystrokes */
  302. return;
  303. if(c != -1) /* valid sequence */
  304. kbdputc(kbdq, c);
  305. else /* dump characters */
  306. for(i=0; i<nk; i++)
  307. kbdputc(kbdq, kc[i]);
  308. nk = 0;
  309. collecting = 0;
  310. return;
  311. } else {
  312. switch(c){
  313. case Caps:
  314. caps ^= 1;
  315. return;
  316. case Num:
  317. num ^= 1;
  318. return;
  319. case Shift:
  320. shift = 1;
  321. return;
  322. case Latin:
  323. alt = 1;
  324. collecting = 1;
  325. nk = 0;
  326. return;
  327. case Ctrl:
  328. ctl = 1;
  329. return;
  330. }
  331. }
  332. kbdputc(kbdq, c);
  333. }
  334. void
  335. i8042auxenable(void (*putc)(int, int))
  336. {
  337. char *err = "i8042: aux init failed\n";
  338. /* enable kbd/aux xfers and interrupts */
  339. ccc &= ~Cauxdis;
  340. ccc |= Cauxint;
  341. ilock(&i8042lock);
  342. if(outready() < 0)
  343. print(err);
  344. outb(Cmd, 0x60); /* write control register */
  345. if(outready() < 0)
  346. print(err);
  347. outb(Data, ccc);
  348. if(outready() < 0)
  349. print(err);
  350. outb(Cmd, 0xA8); /* auxilliary device enable */
  351. if(outready() < 0){
  352. iunlock(&i8042lock);
  353. return;
  354. }
  355. auxputc = putc;
  356. intrenable(IrqAUX, i8042intr, 0, BUSUNKNOWN, "kbdaux");
  357. iunlock(&i8042lock);
  358. }
  359. static void
  360. setscan(int code)
  361. {
  362. char *err = "setscan: set scan code failed\n";
  363. outb(Data, 0xF0);
  364. if(inready() < 0 || inb(Data) != 0xFA || outready() < 0) {
  365. print(err);
  366. return;
  367. }
  368. outb(Data, code);
  369. if(inready() < 0) {
  370. print(err);
  371. return;
  372. }
  373. inb(Data);
  374. if(outready() < 0)
  375. print(err);
  376. }
  377. void
  378. kbdinit(void)
  379. {
  380. int c;
  381. kbdq = qopen(4*1024, 0, 0, 0);
  382. if(kbdq == nil)
  383. panic("kbdinit");
  384. qnoblock(kbdq, 1);
  385. ioalloc(Data, 1, 0, "kbd");
  386. ioalloc(Cmd, 1, 0, "kbd");
  387. intrenable(IrqKBD, i8042intr, 0, BUSUNKNOWN, "kbd");
  388. /* wait for a quiescent controller */
  389. while((c = inb(Status)) & (Outbusy | Inready))
  390. if(c & Inready)
  391. inb(Data);
  392. /* get current controller command byte */
  393. outb(Cmd, 0x20);
  394. if(inready() < 0){
  395. print("kbdinit: can't read ccc\n");
  396. ccc = 0;
  397. } else
  398. ccc = inb(Data);
  399. /* enable kbd xfers and interrupts */
  400. /* disable mouse */
  401. ccc &= ~Ckbddis;
  402. ccc |= Csf | Ckbdint | Cscs1;
  403. if(outready() < 0)
  404. print("kbd init failed\n");
  405. outb(Cmd, 0x60);
  406. if(outready() < 0)
  407. print("kbd init failed\n");
  408. outb(Data, ccc);
  409. outready();
  410. setscan(0x02);
  411. }