kbd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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= 0xF800, /* Unicode private space */
  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. Altgr= Spec|0x67,
  32. Kmouse= Spec|0x100,
  33. No= 0x00, /* peter */
  34. Home= KF|13,
  35. Up= KF|14,
  36. Pgup= KF|15,
  37. Print= KF|16,
  38. Left= KF|17,
  39. Right= KF|18,
  40. End= KF|24,
  41. Down= View,
  42. Pgdown= KF|19,
  43. Ins= KF|20,
  44. Del= 0x7F,
  45. Scroll= KF|21,
  46. Nscan= 128,
  47. };
  48. /*
  49. * The codes at 0x79 and 0x81 are produed by the PFU Happy Hacking keyboard.
  50. * A 'standard' keyboard doesn't produce anything above 0x58.
  51. */
  52. Rune kbtab[Nscan] =
  53. {
  54. [0x00] No, 0x1b, '1', '2', '3', '4', '5', '6',
  55. [0x08] '7', '8', '9', '0', '-', '=', '\b', '\t',
  56. [0x10] 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  57. [0x18] 'o', 'p', '[', ']', '\n', Ctrl, 'a', 's',
  58. [0x20] 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
  59. [0x28] '\'', '`', Shift, '\\', 'z', 'x', 'c', 'v',
  60. [0x30] 'b', 'n', 'm', ',', '.', '/', Shift, '*',
  61. [0x38] Latin, ' ', Ctrl, KF|1, KF|2, KF|3, KF|4, KF|5,
  62. [0x40] KF|6, KF|7, KF|8, KF|9, KF|10, Num, Scroll, '7',
  63. [0x48] '8', '9', '-', '4', '5', '6', '+', '1',
  64. [0x50] '2', '3', '0', '.', No, No, No, KF|11,
  65. [0x58] KF|12, No, No, No, No, No, No, No,
  66. [0x60] No, No, No, No, No, No, No, No,
  67. [0x68] No, No, No, No, No, No, No, No,
  68. [0x70] No, No, No, No, No, No, No, No,
  69. [0x78] No, View, No, Up, No, No, No, No,
  70. };
  71. Rune kbtabshift[Nscan] =
  72. {
  73. [0x00] No, 0x1b, '!', '@', '#', '$', '%', '^',
  74. [0x08] '&', '*', '(', ')', '_', '+', '\b', '\t',
  75. [0x10] 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
  76. [0x18] 'O', 'P', '{', '}', '\n', Ctrl, 'A', 'S',
  77. [0x20] 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
  78. [0x28] '"', '~', Shift, '|', 'Z', 'X', 'C', 'V',
  79. [0x30] 'B', 'N', 'M', '<', '>', '?', Shift, '*',
  80. [0x38] Latin, ' ', Ctrl, KF|1, KF|2, KF|3, KF|4, KF|5,
  81. [0x40] KF|6, KF|7, KF|8, KF|9, KF|10, Num, Scroll, '7',
  82. [0x48] '8', '9', '-', '4', '5', '6', '+', '1',
  83. [0x50] '2', '3', '0', '.', No, No, No, KF|11,
  84. [0x58] KF|12, No, No, No, No, No, No, No,
  85. [0x60] No, No, No, No, No, No, No, No,
  86. [0x68] No, No, No, No, No, No, No, No,
  87. [0x70] No, No, No, No, No, No, No, No,
  88. [0x78] No, Up, No, Up, No, No, No, No,
  89. };
  90. Rune kbtabesc1[Nscan] =
  91. {
  92. [0x00] No, No, No, No, No, No, No, No,
  93. [0x08] No, No, No, No, No, No, No, No,
  94. [0x10] No, No, No, No, No, No, No, No,
  95. [0x18] No, No, No, No, '\n', Ctrl, No, No,
  96. [0x20] No, No, No, No, No, No, No, No,
  97. [0x28] No, No, Shift, No, No, No, No, No,
  98. [0x30] No, No, No, No, No, '/', No, Print,
  99. [0x38] Altgr, No, No, No, No, No, No, No,
  100. [0x40] No, No, No, No, No, No, Break, Home,
  101. [0x48] Up, Pgup, No, Left, No, Right, No, End,
  102. [0x50] Down, Pgdown, Ins, Del, No, No, No, No,
  103. [0x58] No, No, No, No, No, No, No, No,
  104. [0x60] No, No, No, No, No, No, No, No,
  105. [0x68] No, No, No, No, No, No, No, No,
  106. [0x70] No, No, No, No, No, No, No, No,
  107. [0x78] No, Up, No, No, No, No, No, No,
  108. };
  109. Rune kbtabaltgr[Nscan] =
  110. {
  111. [0x00] No, No, No, No, No, No, No, No,
  112. [0x08] No, No, No, No, No, No, No, No,
  113. [0x10] No, No, No, No, No, No, No, No,
  114. [0x18] No, No, No, No, '\n', Ctrl, No, No,
  115. [0x20] No, No, No, No, No, No, No, No,
  116. [0x28] No, No, Shift, No, No, No, No, No,
  117. [0x30] No, No, No, No, No, '/', No, Print,
  118. [0x38] Altgr, No, No, No, No, No, No, No,
  119. [0x40] No, No, No, No, No, No, Break, Home,
  120. [0x48] Up, Pgup, No, Left, No, Right, No, End,
  121. [0x50] Down, Pgdown, Ins, Del, No, No, No, No,
  122. [0x58] No, No, No, No, No, No, No, No,
  123. [0x60] No, No, No, No, No, No, No, No,
  124. [0x68] No, No, No, No, No, No, No, No,
  125. [0x70] No, No, No, No, No, No, No, No,
  126. [0x78] No, Up, No, No, No, No, No, No,
  127. };
  128. Rune kbtabctrl[] =
  129. {
  130. [0x00] No, '', '', '', '', '', '', '',
  131. [0x08] '', '', '', '', ' ', '', '\b', '\t',
  132. [0x10] '', '', '', '', '', '', '', '\t',
  133. [0x18] '', '', '', '', '\n', Ctrl, '', '',
  134. [0x20] '', '', '', '\b', '\n', ' ', ' ', '',
  135. [0x28] '', No, Shift, '', '', '', '', '',
  136. [0x30] '', '', ' ', ' ', '', '', Shift, '\n',
  137. [0x38] Latin, No, Ctrl, '', '', '', '', '',
  138. [0x40] '', '', ' ', ' ', '', '', '', '',
  139. [0x48] '', '', ' ', '', '', '', ' ', '',
  140. [0x50] '', '', '', '', No, No, No, '',
  141. [0x58] ' ', No, No, No, No, No, No, No,
  142. [0x60] No, No, No, No, No, No, No, No,
  143. [0x68] No, No, No, No, No, No, No, No,
  144. [0x70] No, No, No, No, No, No, No, No,
  145. [0x78] No, '', No, '\b', No, No, No, No,
  146. };
  147. enum
  148. {
  149. /* controller command byte */
  150. Cscs1= (1<<6), /* scan code set 1 */
  151. Cauxdis= (1<<5), /* mouse disable */
  152. Ckbddis= (1<<4), /* kbd disable */
  153. Csf= (1<<2), /* system flag */
  154. Cauxint= (1<<1), /* mouse interrupt enable */
  155. Ckbdint= (1<<0), /* kbd interrupt enable */
  156. };
  157. int mouseshifted;
  158. void (*kbdmouse)(int);
  159. static Lock i8042lock;
  160. static uchar ccc;
  161. static void (*auxputc)(int, int);
  162. /*
  163. * wait for output no longer busy
  164. */
  165. static int
  166. outready(void)
  167. {
  168. int tries;
  169. for(tries = 0; (inb(Status) & Outbusy); tries++){
  170. if(tries > 500)
  171. return -1;
  172. delay(2);
  173. }
  174. return 0;
  175. }
  176. /*
  177. * wait for input
  178. */
  179. static int
  180. inready(void)
  181. {
  182. int tries;
  183. for(tries = 0; !(inb(Status) & Inready); tries++){
  184. if(tries > 500)
  185. return -1;
  186. delay(2);
  187. }
  188. return 0;
  189. }
  190. /*
  191. * ask 8042 to reset the machine
  192. */
  193. void
  194. i8042reset(void)
  195. {
  196. ushort *s = KADDR(0x472);
  197. int i, x;
  198. *s = 0x1234; /* BIOS warm-boot flag */
  199. /*
  200. * newer reset the machine command
  201. */
  202. outready();
  203. outb(Cmd, 0xFE);
  204. outready();
  205. /*
  206. * Pulse it by hand (old somewhat reliable)
  207. */
  208. x = 0xDF;
  209. for(i = 0; i < 5; i++){
  210. x ^= 1;
  211. outready();
  212. outb(Cmd, 0xD1);
  213. outready();
  214. outb(Data, x); /* toggle reset */
  215. delay(100);
  216. }
  217. }
  218. int
  219. i8042auxcmd(int cmd)
  220. {
  221. unsigned int c;
  222. int tries;
  223. c = 0;
  224. tries = 0;
  225. ilock(&i8042lock);
  226. do{
  227. if(tries++ > 2)
  228. break;
  229. if(outready() < 0)
  230. break;
  231. outb(Cmd, 0xD4);
  232. if(outready() < 0)
  233. break;
  234. outb(Data, cmd);
  235. if(outready() < 0)
  236. break;
  237. if(inready() < 0)
  238. break;
  239. c = inb(Data);
  240. } while(c == 0xFE || c == 0);
  241. iunlock(&i8042lock);
  242. if(c != 0xFA){
  243. print("i8042: %2.2ux returned to the %2.2ux command\n", c, cmd);
  244. return -1;
  245. }
  246. return 0;
  247. }
  248. int
  249. i8042auxcmds(uchar *cmd, int ncmd)
  250. {
  251. int i;
  252. ilock(&i8042lock);
  253. for(i=0; i<ncmd; i++){
  254. if(outready() < 0)
  255. break;
  256. outb(Cmd, 0xD4);
  257. if(outready() < 0)
  258. break;
  259. outb(Data, cmd[i]);
  260. }
  261. iunlock(&i8042lock);
  262. return i;
  263. }
  264. struct {
  265. int esc1;
  266. int esc2;
  267. int alt;
  268. int altgr;
  269. int caps;
  270. int ctl;
  271. int num;
  272. int shift;
  273. int collecting;
  274. int nk;
  275. Rune kc[5];
  276. int buttons;
  277. } kbscan;
  278. /*
  279. * keyboard interrupt
  280. */
  281. static void
  282. i8042intr(Ureg*, void*)
  283. {
  284. int s, c, i;
  285. int keyup;
  286. /*
  287. * get status
  288. */
  289. ilock(&i8042lock);
  290. s = inb(Status);
  291. if(!(s&Inready)){
  292. iunlock(&i8042lock);
  293. return;
  294. }
  295. /*
  296. * get the character
  297. */
  298. c = inb(Data);
  299. iunlock(&i8042lock);
  300. /*
  301. * if it's the aux port...
  302. */
  303. if(s & Minready){
  304. if(auxputc != nil)
  305. auxputc(c, kbscan.shift);
  306. return;
  307. }
  308. /*
  309. * e0's is the first of a 2 character sequence, e1 the first
  310. * of a 3 character sequence (on the safari)
  311. */
  312. if(c == 0xe0){
  313. kbscan.esc1 = 1;
  314. return;
  315. } else if(c == 0xe1){
  316. kbscan.esc2 = 2;
  317. return;
  318. }
  319. keyup = c&0x80;
  320. c &= 0x7f;
  321. if(c > sizeof kbtab){
  322. c |= keyup;
  323. if(c != 0xFF) /* these come fairly often: CAPSLOCK U Y */
  324. print("unknown key %ux\n", c);
  325. return;
  326. }
  327. if(kbscan.esc1){
  328. c = kbtabesc1[c];
  329. kbscan.esc1 = 0;
  330. } else if(kbscan.esc2){
  331. kbscan.esc2--;
  332. return;
  333. } else if(kbscan.shift)
  334. c = kbtabshift[c];
  335. else if(kbscan.altgr)
  336. c = kbtabaltgr[c];
  337. else if(kbscan.ctl)
  338. c = kbtabctrl[c];
  339. else
  340. c = kbtab[c];
  341. if(kbscan.caps && c<='z' && c>='a')
  342. c += 'A' - 'a';
  343. /*
  344. * keyup only important for shifts
  345. */
  346. if(keyup){
  347. switch(c){
  348. case Latin:
  349. kbscan.alt = 0;
  350. break;
  351. case Shift:
  352. kbscan.shift = 0;
  353. mouseshifted = 0;
  354. break;
  355. case Ctrl:
  356. kbscan.ctl = 0;
  357. break;
  358. case Altgr:
  359. kbscan.altgr = 0;
  360. break;
  361. case Kmouse|1:
  362. case Kmouse|2:
  363. case Kmouse|3:
  364. case Kmouse|4:
  365. case Kmouse|5:
  366. kbscan.buttons &= ~(1<<(c-Kmouse-1));
  367. if(kbdmouse)
  368. kbdmouse(kbscan.buttons);
  369. break;
  370. }
  371. return;
  372. }
  373. /*
  374. * normal character
  375. */
  376. if(!(c & (Spec|KF))){
  377. if(kbscan.ctl)
  378. if(kbscan.alt && c == Del)
  379. exit(0);
  380. if(!kbscan.collecting){
  381. kbdputc(kbdq, c);
  382. return;
  383. }
  384. kbscan.kc[kbscan.nk++] = c;
  385. c = latin1(kbscan.kc, kbscan.nk);
  386. if(c < -1) /* need more keystrokes */
  387. return;
  388. if(c != -1) /* valid sequence */
  389. kbdputc(kbdq, c);
  390. else /* dump characters */
  391. for(i=0; i<kbscan.nk; i++)
  392. kbdputc(kbdq, kbscan.kc[i]);
  393. kbscan.nk = 0;
  394. kbscan.collecting = 0;
  395. return;
  396. } else {
  397. switch(c){
  398. case Caps:
  399. kbscan.caps ^= 1;
  400. return;
  401. case Num:
  402. kbscan.num ^= 1;
  403. return;
  404. case Shift:
  405. kbscan.shift = 1;
  406. mouseshifted = 1;
  407. return;
  408. case Latin:
  409. kbscan.alt = 1;
  410. /*
  411. * VMware and Qemu use Ctl-Alt as the key combination
  412. * to make the VM give up keyboard and mouse focus.
  413. * This has the unfortunate side effect that when you
  414. * come back into focus, Plan 9 thinks you want to type
  415. * a compose sequence (you just typed alt).
  416. *
  417. * As a clumsy hack around this, we look for ctl-alt
  418. * and don't treat it as the start of a compose sequence.
  419. */
  420. if(!kbscan.ctl){
  421. kbscan.collecting = 1;
  422. kbscan.nk = 0;
  423. }
  424. return;
  425. case Ctrl:
  426. kbscan.ctl = 1;
  427. return;
  428. case Altgr:
  429. kbscan.altgr = 1;
  430. return;
  431. case Kmouse|1:
  432. case Kmouse|2:
  433. case Kmouse|3:
  434. case Kmouse|4:
  435. case Kmouse|5:
  436. kbscan.buttons |= 1<<(c-Kmouse-1);
  437. if(kbdmouse)
  438. kbdmouse(kbscan.buttons);
  439. return;
  440. }
  441. }
  442. kbdputc(kbdq, c);
  443. }
  444. void
  445. i8042auxenable(void (*putc)(int, int))
  446. {
  447. char *err = "i8042: aux init failed\n";
  448. /* enable kbd/aux xfers and interrupts */
  449. ccc &= ~Cauxdis;
  450. ccc |= Cauxint;
  451. ilock(&i8042lock);
  452. if(outready() < 0)
  453. print(err);
  454. outb(Cmd, 0x60); /* write control register */
  455. if(outready() < 0)
  456. print(err);
  457. outb(Data, ccc);
  458. if(outready() < 0)
  459. print(err);
  460. outb(Cmd, 0xA8); /* auxilliary device enable */
  461. if(outready() < 0){
  462. iunlock(&i8042lock);
  463. return;
  464. }
  465. auxputc = putc;
  466. intrenable(IrqAUX, i8042intr, 0, BUSUNKNOWN, "kbdaux");
  467. iunlock(&i8042lock);
  468. }
  469. void
  470. kbdinit(void)
  471. {
  472. int c;
  473. /* wait for a quiescent controller */
  474. while((c = inb(Status)) & (Outbusy | Inready))
  475. if(c & Inready)
  476. inb(Data);
  477. /* get current controller command byte */
  478. outb(Cmd, 0x20);
  479. if(inready() < 0){
  480. print("kbdinit: can't read ccc\n");
  481. ccc = 0;
  482. } else
  483. ccc = inb(Data);
  484. /* enable kbd xfers and interrupts */
  485. /* disable mouse */
  486. ccc &= ~Ckbddis;
  487. ccc |= Csf | Ckbdint | Cscs1;
  488. if(outready() < 0)
  489. print("kbd init failed\n");
  490. outb(Cmd, 0x60);
  491. if(outready() < 0)
  492. print("kbd init failed\n");
  493. outb(Data, ccc);
  494. outready();
  495. }
  496. void
  497. kbdenable(void)
  498. {
  499. kbdq = qopen(4*1024, 0, 0, 0);
  500. if(kbdq == nil)
  501. panic("kbdinit");
  502. qnoblock(kbdq, 1);
  503. ioalloc(Data, 1, 0, "kbd");
  504. ioalloc(Cmd, 1, 0, "kbd");
  505. intrenable(IrqKBD, i8042intr, 0, BUSUNKNOWN, "kbd");
  506. }
  507. void
  508. kbdputmap(ushort m, ushort scanc, Rune r)
  509. {
  510. if(scanc >= Nscan)
  511. error(Ebadarg);
  512. switch(m) {
  513. default:
  514. error(Ebadarg);
  515. case 0:
  516. kbtab[scanc] = r;
  517. break;
  518. case 1:
  519. kbtabshift[scanc] = r;
  520. break;
  521. case 2:
  522. kbtabesc1[scanc] = r;
  523. break;
  524. case 3:
  525. kbtabaltgr[scanc] = r;
  526. break;
  527. case 4:
  528. kbtabctrl[scanc] = r;
  529. break;
  530. }
  531. }
  532. int
  533. kbdgetmap(int offset, int *t, int *sc, Rune *r)
  534. {
  535. *t = offset/Nscan;
  536. *sc = offset%Nscan;
  537. if(*t < 0 || *sc < 0)
  538. error(Ebadarg);
  539. switch(*t) {
  540. default:
  541. return 0;
  542. case 0:
  543. *r = kbtab[*sc];
  544. return 1;
  545. case 1:
  546. *r = kbtabshift[*sc];
  547. return 1;
  548. case 2:
  549. *r = kbtabesc1[*sc];
  550. return 1;
  551. case 3:
  552. *r = kbtabaltgr[*sc];
  553. return 1;
  554. case 4:
  555. *r = kbtabctrl[*sc];
  556. return 1;
  557. }
  558. }