kbd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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[] =
  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. /*
  284. * Scan code processing
  285. */
  286. void
  287. kbdputsc(int c, int external)
  288. {
  289. int i, keyup;
  290. Kbscan *kbscan;
  291. if(external)
  292. kbscan = &kbscans[1];
  293. else
  294. kbscan = &kbscans[0];
  295. /*
  296. * e0's is the first of a 2 character sequence, e1 the first
  297. * of a 3 character sequence (on the safari)
  298. */
  299. if(c == 0xe0){
  300. kbscan->esc1 = 1;
  301. return;
  302. } else if(c == 0xe1){
  303. kbscan->esc2 = 2;
  304. return;
  305. }
  306. keyup = c & 0x80;
  307. c &= 0x7f;
  308. if(c > sizeof kbtab){
  309. c |= keyup;
  310. if(c != 0xFF) /* these come fairly often: CAPSLOCK U Y */
  311. print("unknown key %ux\n", c);
  312. return;
  313. }
  314. if(kbscan->esc1){
  315. c = kbtabesc1[c];
  316. kbscan->esc1 = 0;
  317. } else if(kbscan->esc2){
  318. kbscan->esc2--;
  319. return;
  320. } else if(kbscan->shift)
  321. c = kbtabshift[c];
  322. else if(kbscan->altgr)
  323. c = kbtabaltgr[c];
  324. else if(kbscan->ctl)
  325. c = kbtabctrl[c];
  326. else
  327. c = kbtab[c];
  328. if(kbscan->caps && c<='z' && c>='a')
  329. c += 'A' - 'a';
  330. /*
  331. * keyup only important for shifts
  332. */
  333. if(keyup){
  334. switch(c){
  335. case Latin:
  336. kbscan->alt = 0;
  337. break;
  338. case Shift:
  339. kbscan->shift = 0;
  340. mouseshifted = 0;
  341. break;
  342. case Ctrl:
  343. kbscan->ctl = 0;
  344. break;
  345. case Altgr:
  346. kbscan->altgr = 0;
  347. break;
  348. case Kmouse|1:
  349. case Kmouse|2:
  350. case Kmouse|3:
  351. case Kmouse|4:
  352. case Kmouse|5:
  353. kbscan->buttons &= ~(1<<(c-Kmouse-1));
  354. if(kbdmouse)
  355. kbdmouse(kbscan->buttons);
  356. break;
  357. }
  358. return;
  359. }
  360. /*
  361. * normal character
  362. */
  363. if(!(c & (Spec|KF))){
  364. if(kbscan->ctl)
  365. if(kbscan->alt && c == Del)
  366. exit(0);
  367. if(!kbscan->collecting){
  368. kbdputc(kbdq, c);
  369. return;
  370. }
  371. kbscan->kc[kbscan->nk++] = c;
  372. c = latin1(kbscan->kc, kbscan->nk);
  373. if(c < -1) /* need more keystrokes */
  374. return;
  375. if(c != -1) /* valid sequence */
  376. kbdputc(kbdq, c);
  377. else /* dump characters */
  378. for(i=0; i<kbscan->nk; i++)
  379. kbdputc(kbdq, kbscan->kc[i]);
  380. kbscan->nk = 0;
  381. kbscan->collecting = 0;
  382. return;
  383. } else {
  384. switch(c){
  385. case Caps:
  386. kbscan->caps ^= 1;
  387. return;
  388. case Num:
  389. kbscan->num ^= 1;
  390. return;
  391. case Shift:
  392. kbscan->shift = 1;
  393. mouseshifted = 1;
  394. return;
  395. case Latin:
  396. kbscan->alt = 1;
  397. /*
  398. * VMware and Qemu use Ctl-Alt as the key combination
  399. * to make the VM give up keyboard and mouse focus.
  400. * This has the unfortunate side effect that when you
  401. * come back into focus, Plan 9 thinks you want to type
  402. * a compose sequence (you just typed alt).
  403. *
  404. * As a clumsy hack around this, we look for ctl-alt
  405. * and don't treat it as the start of a compose sequence.
  406. */
  407. if(!kbscan->ctl){
  408. kbscan->collecting = 1;
  409. kbscan->nk = 0;
  410. }
  411. return;
  412. case Ctrl:
  413. kbscan->ctl = 1;
  414. return;
  415. case Altgr:
  416. kbscan->altgr = 1;
  417. return;
  418. case Kmouse|1:
  419. case Kmouse|2:
  420. case Kmouse|3:
  421. case Kmouse|4:
  422. case Kmouse|5:
  423. kbscan->buttons |= 1<<(c-Kmouse-1);
  424. if(kbdmouse)
  425. kbdmouse(kbscan->buttons);
  426. return;
  427. }
  428. }
  429. kbdputc(kbdq, c);
  430. }
  431. /*
  432. * keyboard interrupt
  433. */
  434. static void
  435. i8042intr(Ureg*, void*)
  436. {
  437. int s, c;
  438. /*
  439. * get status
  440. */
  441. ilock(&i8042lock);
  442. s = inb(Status);
  443. if(!(s&Inready)){
  444. iunlock(&i8042lock);
  445. return;
  446. }
  447. /*
  448. * get the character
  449. */
  450. c = inb(Data);
  451. iunlock(&i8042lock);
  452. /*
  453. * if it's the aux port...
  454. */
  455. if(s & Minready){
  456. if(auxputc != nil)
  457. auxputc(c, kbscans[0].shift); /* internal source */
  458. return;
  459. }
  460. kbdputsc(c, 0); /* internal source */
  461. }
  462. void
  463. i8042auxenable(void (*putc)(int, int))
  464. {
  465. char *err = "i8042: aux init failed\n";
  466. /* enable kbd/aux xfers and interrupts */
  467. ccc &= ~Cauxdis;
  468. ccc |= Cauxint;
  469. ilock(&i8042lock);
  470. if(outready() < 0)
  471. print(err);
  472. outb(Cmd, 0x60); /* write control register */
  473. if(outready() < 0)
  474. print(err);
  475. outb(Data, ccc);
  476. if(outready() < 0)
  477. print(err);
  478. outb(Cmd, 0xA8); /* auxilliary device enable */
  479. if(outready() < 0){
  480. iunlock(&i8042lock);
  481. return;
  482. }
  483. auxputc = putc;
  484. intrenable(IrqAUX, i8042intr, 0, BUSUNKNOWN, "kbdaux");
  485. iunlock(&i8042lock);
  486. }
  487. void
  488. kbdinit(void)
  489. {
  490. int c;
  491. /* wait for a quiescent controller */
  492. while((c = inb(Status)) & (Outbusy | Inready))
  493. if(c & Inready)
  494. inb(Data);
  495. /* get current controller command byte */
  496. outb(Cmd, 0x20);
  497. if(inready() < 0){
  498. print("kbdinit: can't read ccc\n");
  499. ccc = 0;
  500. } else
  501. ccc = inb(Data);
  502. /* enable kbd xfers and interrupts */
  503. /* disable mouse */
  504. ccc &= ~Ckbddis;
  505. ccc |= Csf | Ckbdint | Cscs1;
  506. if(outready() < 0)
  507. print("kbd init failed\n");
  508. outb(Cmd, 0x60);
  509. if(outready() < 0)
  510. print("kbd init failed\n");
  511. outb(Data, ccc);
  512. outready();
  513. }
  514. void
  515. kbdenable(void)
  516. {
  517. kbdq = qopen(4*1024, 0, 0, 0);
  518. if(kbdq == nil)
  519. panic("kbdinit");
  520. qnoblock(kbdq, 1);
  521. ioalloc(Data, 1, 0, "kbd");
  522. ioalloc(Cmd, 1, 0, "kbd");
  523. intrenable(IrqKBD, i8042intr, 0, BUSUNKNOWN, "kbd");
  524. }
  525. void
  526. kbdputmap(ushort m, ushort scanc, Rune r)
  527. {
  528. if(scanc >= Nscan)
  529. error(Ebadarg);
  530. switch(m) {
  531. default:
  532. error(Ebadarg);
  533. case 0:
  534. kbtab[scanc] = r;
  535. break;
  536. case 1:
  537. kbtabshift[scanc] = r;
  538. break;
  539. case 2:
  540. kbtabesc1[scanc] = r;
  541. break;
  542. case 3:
  543. kbtabaltgr[scanc] = r;
  544. break;
  545. case 4:
  546. kbtabctrl[scanc] = r;
  547. break;
  548. }
  549. }
  550. int
  551. kbdgetmap(uint offset, int *t, int *sc, Rune *r)
  552. {
  553. if ((int)offset < 0)
  554. error(Ebadarg);
  555. *t = offset/Nscan;
  556. *sc = offset%Nscan;
  557. switch(*t) {
  558. default:
  559. return 0;
  560. case 0:
  561. *r = kbtab[*sc];
  562. return 1;
  563. case 1:
  564. *r = kbtabshift[*sc];
  565. return 1;
  566. case 2:
  567. *r = kbtabesc1[*sc];
  568. return 1;
  569. case 3:
  570. *r = kbtabaltgr[*sc];
  571. return 1;
  572. case 4:
  573. *r = kbtabctrl[*sc];
  574. return 1;
  575. }
  576. }