kbd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. static int nokbd = 1;
  166. /*
  167. * wait for output no longer busy
  168. */
  169. static int
  170. outready(void)
  171. {
  172. int tries;
  173. for(tries = 0; (inb(Status) & Outbusy); tries++){
  174. if(tries > 500)
  175. return -1;
  176. delay(2);
  177. }
  178. return 0;
  179. }
  180. /*
  181. * wait for input
  182. */
  183. static int
  184. inready(void)
  185. {
  186. int tries;
  187. for(tries = 0; !(inb(Status) & Inready); tries++){
  188. if(tries > 500)
  189. return -1;
  190. delay(2);
  191. }
  192. return 0;
  193. }
  194. /*
  195. * ask 8042 to reset the machine
  196. */
  197. void
  198. i8042reset(void)
  199. {
  200. int i, x;
  201. if(nokbd)
  202. return;
  203. *((ushort*)KADDR(0x472)) = 0x1234; /* BIOS warm-boot flag */
  204. /*
  205. * newer reset the machine command
  206. */
  207. outready();
  208. outb(Cmd, 0xFE);
  209. outready();
  210. /*
  211. * Pulse it by hand (old somewhat reliable)
  212. */
  213. x = 0xDF;
  214. for(i = 0; i < 5; i++){
  215. x ^= 1;
  216. outready();
  217. outb(Cmd, 0xD1);
  218. outready();
  219. outb(Data, x); /* toggle reset */
  220. delay(100);
  221. }
  222. }
  223. int
  224. i8042auxcmd(int cmd)
  225. {
  226. unsigned int c;
  227. int tries;
  228. c = 0;
  229. tries = 0;
  230. ilock(&i8042lock);
  231. do{
  232. if(tries++ > 2)
  233. break;
  234. if(outready() < 0)
  235. break;
  236. outb(Cmd, 0xD4);
  237. if(outready() < 0)
  238. break;
  239. outb(Data, cmd);
  240. if(outready() < 0)
  241. break;
  242. if(inready() < 0)
  243. break;
  244. c = inb(Data);
  245. } while(c == 0xFE || c == 0);
  246. iunlock(&i8042lock);
  247. if(c != 0xFA){
  248. print("i8042: %2.2ux returned to the %2.2ux command\n", c, cmd);
  249. return -1;
  250. }
  251. return 0;
  252. }
  253. int
  254. i8042auxcmds(uchar *cmd, int ncmd)
  255. {
  256. int i;
  257. ilock(&i8042lock);
  258. for(i=0; i<ncmd; i++){
  259. if(outready() < 0)
  260. break;
  261. outb(Cmd, 0xD4);
  262. if(outready() < 0)
  263. break;
  264. outb(Data, cmd[i]);
  265. }
  266. iunlock(&i8042lock);
  267. return i;
  268. }
  269. typedef struct Kbscan Kbscan;
  270. struct Kbscan {
  271. int esc1;
  272. int esc2;
  273. int alt;
  274. int altgr;
  275. int caps;
  276. int ctl;
  277. int num;
  278. int shift;
  279. int collecting;
  280. int nk;
  281. Rune kc[5];
  282. int buttons;
  283. };
  284. Kbscan kbscans[2]; /* kernel and external scan code state */
  285. static int kdebug;
  286. /*
  287. * Scan code processing
  288. */
  289. void
  290. kbdputsc(int c, int external)
  291. {
  292. int i, keyup;
  293. Kbscan *kbscan;
  294. if(external)
  295. kbscan = &kbscans[1];
  296. else
  297. kbscan = &kbscans[0];
  298. if(kdebug)
  299. print("sc %x ms %d\n", c, mouseshifted);
  300. /*
  301. * e0's is the first of a 2 character sequence, e1 the first
  302. * of a 3 character sequence (on the safari)
  303. */
  304. if(c == 0xe0){
  305. kbscan->esc1 = 1;
  306. return;
  307. } else if(c == 0xe1){
  308. kbscan->esc2 = 2;
  309. return;
  310. }
  311. keyup = c & 0x80;
  312. c &= 0x7f;
  313. if(c > sizeof kbtab){
  314. c |= keyup;
  315. if(c != 0xFF) /* these come fairly often: CAPSLOCK U Y */
  316. print("unknown key %ux\n", c);
  317. return;
  318. }
  319. if(kbscan->esc1){
  320. c = kbtabesc1[c];
  321. kbscan->esc1 = 0;
  322. } else if(kbscan->esc2){
  323. kbscan->esc2--;
  324. return;
  325. } else if(kbscan->shift)
  326. c = kbtabshift[c];
  327. else if(kbscan->altgr)
  328. c = kbtabaltgr[c];
  329. else if(kbscan->ctl)
  330. c = kbtabctrl[c];
  331. else
  332. c = kbtab[c];
  333. if(kbscan->caps && c<='z' && c>='a')
  334. c += 'A' - 'a';
  335. /*
  336. * keyup only important for shifts
  337. */
  338. if(keyup){
  339. switch(c){
  340. case Latin:
  341. kbscan->alt = 0;
  342. break;
  343. case Shift:
  344. kbscan->shift = 0;
  345. mouseshifted = 0;
  346. if(kdebug)
  347. print("shiftclr\n");
  348. break;
  349. case Ctrl:
  350. kbscan->ctl = 0;
  351. break;
  352. case Altgr:
  353. kbscan->altgr = 0;
  354. break;
  355. case Kmouse|1:
  356. case Kmouse|2:
  357. case Kmouse|3:
  358. case Kmouse|4:
  359. case Kmouse|5:
  360. kbscan->buttons &= ~(1<<(c-Kmouse-1));
  361. if(kbdmouse)
  362. kbdmouse(kbscan->buttons);
  363. break;
  364. }
  365. return;
  366. }
  367. /*
  368. * normal character
  369. */
  370. if(!(c & (Spec|KF))){
  371. if(kbscan->ctl)
  372. if(kbscan->alt && c == Del)
  373. exit(0);
  374. if(!kbscan->collecting){
  375. kbdputc(kbdq, c);
  376. return;
  377. }
  378. kbscan->kc[kbscan->nk++] = c;
  379. c = latin1(kbscan->kc, kbscan->nk);
  380. if(c < -1) /* need more keystrokes */
  381. return;
  382. if(c != -1) /* valid sequence */
  383. kbdputc(kbdq, c);
  384. else /* dump characters */
  385. for(i=0; i<kbscan->nk; i++)
  386. kbdputc(kbdq, kbscan->kc[i]);
  387. kbscan->nk = 0;
  388. kbscan->collecting = 0;
  389. return;
  390. } else {
  391. switch(c){
  392. case Caps:
  393. kbscan->caps ^= 1;
  394. return;
  395. case Num:
  396. kbscan->num ^= 1;
  397. return;
  398. case Shift:
  399. kbscan->shift = 1;
  400. if(kdebug)
  401. print("shift\n");
  402. mouseshifted = 1;
  403. return;
  404. case Latin:
  405. kbscan->alt = 1;
  406. /*
  407. * VMware and Qemu use Ctl-Alt as the key combination
  408. * to make the VM give up keyboard and mouse focus.
  409. * This has the unfortunate side effect that when you
  410. * come back into focus, Plan 9 thinks you want to type
  411. * a compose sequence (you just typed alt).
  412. *
  413. * As a clumsy hack around this, we look for ctl-alt
  414. * and don't treat it as the start of a compose sequence.
  415. */
  416. if(!kbscan->ctl){
  417. kbscan->collecting = 1;
  418. kbscan->nk = 0;
  419. }
  420. return;
  421. case Ctrl:
  422. kbscan->ctl = 1;
  423. return;
  424. case Altgr:
  425. kbscan->altgr = 1;
  426. return;
  427. case Kmouse|1:
  428. case Kmouse|2:
  429. case Kmouse|3:
  430. case Kmouse|4:
  431. case Kmouse|5:
  432. kbscan->buttons |= 1<<(c-Kmouse-1);
  433. if(kbdmouse)
  434. kbdmouse(kbscan->buttons);
  435. return;
  436. case KF|11:
  437. kdebug = 1;
  438. break;
  439. case KF|12:
  440. kdebug = 0;
  441. break;
  442. }
  443. }
  444. kbdputc(kbdq, c);
  445. }
  446. /*
  447. * keyboard interrupt
  448. */
  449. static void
  450. i8042intr(Ureg*, void*)
  451. {
  452. int s, c;
  453. /*
  454. * get status
  455. */
  456. ilock(&i8042lock);
  457. s = inb(Status);
  458. if(!(s&Inready)){
  459. iunlock(&i8042lock);
  460. return;
  461. }
  462. /*
  463. * get the character
  464. */
  465. c = inb(Data);
  466. iunlock(&i8042lock);
  467. /*
  468. * if it's the aux port...
  469. */
  470. if(s & Minready){
  471. if(auxputc != nil)
  472. auxputc(c, kbscans[0].shift); /* internal source */
  473. return;
  474. }
  475. kbdputsc(c, 0); /* internal source */
  476. }
  477. void
  478. i8042auxenable(void (*putc)(int, int))
  479. {
  480. char *err = "i8042: aux init failed\n";
  481. /* enable kbd/aux xfers and interrupts */
  482. ccc &= ~Cauxdis;
  483. ccc |= Cauxint;
  484. ilock(&i8042lock);
  485. if(outready() < 0)
  486. print(err);
  487. outb(Cmd, 0x60); /* write control register */
  488. if(outready() < 0)
  489. print(err);
  490. outb(Data, ccc);
  491. if(outready() < 0)
  492. print(err);
  493. outb(Cmd, 0xA8); /* auxilliary device enable */
  494. if(outready() < 0){
  495. iunlock(&i8042lock);
  496. return;
  497. }
  498. auxputc = putc;
  499. intrenable(IrqAUX, i8042intr, 0, BUSUNKNOWN, "kbdaux");
  500. iunlock(&i8042lock);
  501. }
  502. static char *initfailed = "i8042: kbdinit failed\n";
  503. static int
  504. outbyte(int port, int c)
  505. {
  506. outb(port, c);
  507. if(outready() < 0) {
  508. print(initfailed);
  509. return -1;
  510. }
  511. return 0;
  512. }
  513. void
  514. kbdinit(void)
  515. {
  516. int c, try;
  517. /* wait for a quiescent controller */
  518. try = 1000;
  519. while(try-- > 0 && (c = inb(Status)) & (Outbusy | Inready)) {
  520. if(c & Inready)
  521. inb(Data);
  522. delay(1);
  523. }
  524. if (try <= 0) {
  525. print(initfailed);
  526. return;
  527. }
  528. /* get current controller command byte */
  529. outb(Cmd, 0x20);
  530. if(inready() < 0){
  531. print("i8042: kbdinit can't read ccc\n");
  532. ccc = 0;
  533. } else
  534. ccc = inb(Data);
  535. /* enable kbd xfers and interrupts */
  536. ccc &= ~Ckbddis;
  537. ccc |= Csf | Ckbdint | Cscs1;
  538. if(outready() < 0) {
  539. print(initfailed);
  540. return;
  541. }
  542. nokbd = 0;
  543. /* disable mouse */
  544. if (outbyte(Cmd, 0x60) < 0 || outbyte(Data, ccc) < 0)
  545. print("i8042: kbdinit mouse disable failed\n");
  546. }
  547. void
  548. kbdenable(void)
  549. {
  550. kbdq = qopen(4*1024, 0, 0, 0);
  551. if(kbdq == nil)
  552. panic("kbdinit");
  553. qnoblock(kbdq, 1);
  554. ioalloc(Data, 1, 0, "kbd");
  555. ioalloc(Cmd, 1, 0, "kbd");
  556. intrenable(IrqKBD, i8042intr, 0, BUSUNKNOWN, "kbd");
  557. }
  558. void
  559. kbdputmap(ushort m, ushort scanc, Rune r)
  560. {
  561. if(scanc >= Nscan)
  562. error(Ebadarg);
  563. switch(m) {
  564. default:
  565. error(Ebadarg);
  566. case 0:
  567. kbtab[scanc] = r;
  568. break;
  569. case 1:
  570. kbtabshift[scanc] = r;
  571. break;
  572. case 2:
  573. kbtabesc1[scanc] = r;
  574. break;
  575. case 3:
  576. kbtabaltgr[scanc] = r;
  577. break;
  578. case 4:
  579. kbtabctrl[scanc] = r;
  580. break;
  581. }
  582. }
  583. int
  584. kbdgetmap(uint offset, int *t, int *sc, Rune *r)
  585. {
  586. if ((int)offset < 0)
  587. error(Ebadarg);
  588. *t = offset/Nscan;
  589. *sc = offset%Nscan;
  590. switch(*t) {
  591. default:
  592. return 0;
  593. case 0:
  594. *r = kbtab[*sc];
  595. return 1;
  596. case 1:
  597. *r = kbtabshift[*sc];
  598. return 1;
  599. case 2:
  600. *r = kbtabesc1[*sc];
  601. return 1;
  602. case 3:
  603. *r = kbtabaltgr[*sc];
  604. return 1;
  605. case 4:
  606. *r = kbtabctrl[*sc];
  607. return 1;
  608. }
  609. }