kbd.c 11 KB

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