kbd.c 13 KB

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