i8042.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "../port/lib.h"
  11. #include "mem.h"
  12. #include "dat.h"
  13. #include "fns.h"
  14. #include "../port/error.h"
  15. #include "io.h"
  16. enum {
  17. Data= 0x60, /* data port */
  18. Status= 0x64, /* status port */
  19. Inready= 0x01, /* input character ready */
  20. Outbusy= 0x02, /* output busy */
  21. Sysflag= 0x04, /* system flag */
  22. Cmddata= 0x08, /* cmd==0, data==1 */
  23. Inhibit= 0x10, /* keyboard/mouse inhibited */
  24. Minready= 0x20, /* mouse character ready */
  25. Rtimeout= 0x40, /* general timeout */
  26. Parity= 0x80,
  27. Cmd= 0x64, /* command port (write only) */
  28. Spec= 0xF800, /* Unicode private space */
  29. PF= Spec|0x20, /* num pad function key */
  30. View= Spec|0x00, /* view (shift window up) */
  31. KF= 0xF000, /* function key (begin Unicode private space) */
  32. Shift= Spec|0x60,
  33. Break= Spec|0x61,
  34. Ctrl= Spec|0x62,
  35. Latin= Spec|0x63,
  36. Caps= Spec|0x64,
  37. Num= Spec|0x65,
  38. Middle= Spec|0x66,
  39. Altgr= Spec|0x67,
  40. Kmouse= Spec|0x100,
  41. No= 0x00, /* peter */
  42. Home= KF|13,
  43. Up= KF|14,
  44. Pgup= KF|15,
  45. Print= KF|16,
  46. Left= KF|17,
  47. Right= KF|18,
  48. End= KF|24,
  49. Down= View,
  50. Pgdown= KF|19,
  51. Ins= KF|20,
  52. Del= 0x7F,
  53. Scroll= KF|21,
  54. Nscan= 128,
  55. };
  56. /*
  57. * The codes at 0x79 and 0x81 are produed by the PFU Happy Hacking keyboard.
  58. * A 'standard' keyboard doesn't produce anything above 0x58.
  59. */
  60. Rune kbtab[Nscan] =
  61. {
  62. [0x00] = No, 0x1b, '1', '2', '3', '4', '5', '6',
  63. [0x08] = '7', '8', '9', '0', '-', '=', '\b', '\t',
  64. [0x10] = 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  65. [0x18] = 'o', 'p', '[', ']', '\n', Ctrl, 'a', 's',
  66. [0x20] = 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
  67. [0x28] = '\'', '`', Shift, '\\', 'z', 'x', 'c', 'v',
  68. [0x30] = 'b', 'n', 'm', ',', '.', '/', Shift, '*',
  69. [0x38] = Latin, ' ', Ctrl, KF|1, KF|2, KF|3, KF|4, KF|5,
  70. [0x40] = KF|6, KF|7, KF|8, KF|9, KF|10, Num, Scroll, '7',
  71. [0x48] = '8', '9', '-', '4', '5', '6', '+', '1',
  72. [0x50] = '2', '3', '0', '.', No, No, No, KF|11,
  73. [0x58] = KF|12, No, No, No, No, No, No, No,
  74. [0x60] = No, No, No, No, No, No, No, No,
  75. [0x68] = No, No, No, No, No, No, No, No,
  76. [0x70] = No, No, No, No, No, No, No, No,
  77. [0x78] = No, View, No, Up, No, No, No, No,
  78. };
  79. Rune kbtabshift[Nscan] =
  80. {
  81. [0x00] = No, 0x1b, '!', '@', '#', '$', '%', '^',
  82. [0x08] = '&', '*', '(', ')', '_', '+', '\b', '\t',
  83. [0x10] = 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
  84. [0x18] = 'O', 'P', '{', '}', '\n', Ctrl, 'A', 'S',
  85. [0x20] = 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
  86. [0x28] = '"', '~', Shift, '|', 'Z', 'X', 'C', 'V',
  87. [0x30] = 'B', 'N', 'M', '<', '>', '?', Shift, '*',
  88. [0x38] = Latin, ' ', Ctrl, KF|1, KF|2, KF|3, KF|4, KF|5,
  89. [0x40] = KF|6, KF|7, KF|8, KF|9, KF|10, Num, Scroll, '7',
  90. [0x48] = '8', '9', '-', '4', '5', '6', '+', '1',
  91. [0x50] = '2', '3', '0', '.', No, No, No, KF|11,
  92. [0x58] = KF|12, No, No, No, No, No, No, No,
  93. [0x60] = No, No, No, No, No, No, No, No,
  94. [0x68] = No, No, No, No, No, No, No, No,
  95. [0x70] = No, No, No, No, No, No, No, No,
  96. [0x78] = No, Up, No, Up, No, No, No, No,
  97. };
  98. Rune kbtabesc1[Nscan] =
  99. {
  100. [0x00] = No, No, No, No, No, No, No, No,
  101. [0x08] = No, No, No, No, No, No, No, No,
  102. [0x10] = No, No, No, No, No, No, No, No,
  103. [0x18] = No, No, No, No, '\n', Ctrl, No, No,
  104. [0x20] = No, No, No, No, No, No, No, No,
  105. [0x28] = No, No, Shift, No, No, No, No, No,
  106. [0x30] = No, No, No, No, No, '/', No, Print,
  107. [0x38] = Altgr, No, No, No, No, No, No, No,
  108. [0x40] = No, No, No, No, No, No, Break, Home,
  109. [0x48] = Up, Pgup, No, Left, No, Right, No, End,
  110. [0x50] = Down, Pgdown, Ins, Del, No, No, No, No,
  111. [0x58] = No, No, No, No, No, No, No, No,
  112. [0x60] = No, No, No, No, No, No, No, No,
  113. [0x68] = No, No, No, No, No, No, No, No,
  114. [0x70] = No, No, No, No, No, No, No, No,
  115. [0x78] = No, Up, No, No, No, No, No, No,
  116. };
  117. Rune kbtabaltgr[Nscan] =
  118. {
  119. [0x00] = No, No, No, No, No, No, No, No,
  120. [0x08] = No, No, No, No, No, No, No, No,
  121. [0x10] = No, No, No, No, No, No, No, No,
  122. [0x18] = No, No, No, No, '\n', Ctrl, No, No,
  123. [0x20] = No, No, No, No, No, No, No, No,
  124. [0x28] = No, No, Shift, No, No, No, No, No,
  125. [0x30] = No, No, No, No, No, '/', No, Print,
  126. [0x38] = Altgr, No, No, No, No, No, No, No,
  127. [0x40] = No, No, No, No, No, No, Break, Home,
  128. [0x48] = Up, Pgup, No, Left, No, Right, No, End,
  129. [0x50] = Down, Pgdown, Ins, Del, No, No, No, No,
  130. [0x58] = No, No, No, No, No, No, No, No,
  131. [0x60] = No, No, No, No, No, No, No, No,
  132. [0x68] = No, No, No, No, No, No, No, No,
  133. [0x70] = No, No, No, No, No, No, No, No,
  134. [0x78] = No, Up, No, No, No, No, No, No,
  135. };
  136. /*
  137. Rune kbtabctrl[] =
  138. {
  139. [0x00] No, '', '', '', '', '', '', '',
  140. [0x08] '', '', '', '', '
  141. ', '', '\b', '\t',
  142. [0x10] '', '', '', '', '', '', '', '\t',
  143. [0x18] '', '', '', '', '\n', Ctrl, '', '',
  144. [0x20] '', '', '', '\b', '\n', ' ', ' ', '',
  145. [0x28] '', No, Shift, '', '', '', '', '',
  146. [0x30] '', '', '
  147. ', ' ', '', '', Shift, '\n',
  148. [0x38] Latin, No, Ctrl, '', '', '', '', '',
  149. [0x40] '', '', ' ', '
  150. ', '', '', '', '',
  151. [0x48] '', '', '
  152. ', '', '', '', ' ', '',
  153. [0x50] '', '', '', '', No, No, No, '',
  154. [0x58] ' ', No, No, No, No, No, No, No,
  155. [0x60] No, No, No, No, No, No, No, No,
  156. [0x68] No, No, No, No, No, No, No, No,
  157. [0x70] No, No, No, No, No, No, No, No,
  158. [0x78] No, '', No, '\b', No, No, No, No,
  159. };
  160. */
  161. static Rune kbtabctrl[256] =
  162. {
  163. [0x00] = No, '\x1b', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16',
  164. [0x08] = '\x17', '\x18', '\x19', '\x10', '\n', '\x1d', '\b', '\t',
  165. [0x10] = '\x11', '\x17', '\x05', '\x12', '\x14', '\x19', '\x15', '\t',
  166. [0x18] = '\x0f', '\x10', '\x1b', '\x1d', '\n', Ctrl, '\x01', '\x13',
  167. [0x20] = '\x04', '\x06', '\x07', '\b', '\n', '\x0b', '\x0c', '\x1b',
  168. [0x28] = '\x07', No, Shift, '\x1c', '\x1a', '\x18', '\x03', '\x16',
  169. [0x30] = '\x02', '\x0e', '\n', '\x0c', '\x0e', '\x0f', Shift, '\n',
  170. [0x38] = Latin, No, Ctrl, '\x05', '\x06', '\x07', '\x04', '\x05',
  171. [0x40] = '\x06', '\x07', '\x0c', '\n', '\x0e', '\x05', '\x06', '\x17',
  172. [0x48] = '\x18', '\x19', '\n', '\x14', '\x15', '\x16', '\x0b', '\x11',
  173. [0x50] = '\x12', '\x13', '\x10', '\x0e', No, No, No, '\x0f',
  174. [0x58] = '\x0c', No, No, No, No, No, No, No,
  175. [0x60] = No, No, No, No, No, No, No, No,
  176. [0x68] = No, No, No, No, No, No, No, No,
  177. [0x70] = No, No, No, No, No, No, No, No,
  178. [0x78] = No, '\x07', No, '\b', No, No, No, No,
  179. };
  180. enum
  181. {
  182. /* controller command byte */
  183. Cscs1= (1<<6), /* scan code set 1 */
  184. Cauxdis= (1<<5), /* mouse disable */
  185. Ckeybdis= (1<<4), /* kbd disable */
  186. Csf= (1<<2), /* system flag */
  187. Cauxint= (1<<1), /* mouse interrupt enable */
  188. Ckeybint= (1<<0), /* kbd interrupt enable */
  189. };
  190. static Queue *keybq;
  191. static Queue *mouseq;
  192. int mouseshifted;
  193. void (*kbdmouse)(int);
  194. static int nokeyb = 1;
  195. static Lock i8042lock;
  196. static uint8_t ccc;
  197. static void (*auxputc)(int, int);
  198. enum {
  199. MaxDKResults = 28,
  200. MaxDK = 10,
  201. };
  202. typedef struct DeadKey DeadKey;
  203. typedef struct DeadKeys DeadKeys;
  204. struct DeadKey {
  205. Rune key; //Rune that represents the dead key (i.e. ^)
  206. Rune baseKey[MaxDKResults]; //Rune that represents the base letter (i.e. a)
  207. Rune resultKey[MaxDKResults]; //Sum of both keys (i.e. â)
  208. };
  209. struct DeadKeys {
  210. Rune max;
  211. Rune min;
  212. DeadKey deadKey[MaxDK];
  213. };
  214. static DeadKeys kbdeadkeys;
  215. /*
  216. * Allow to reset dead keys when we load a non deadkeys kbmap
  217. */
  218. void
  219. initDeadKeys(){
  220. DeadKey k;
  221. k.key = No;
  222. memset(k.baseKey, No, sizeof(k.baseKey));
  223. for (int i=0; i<MaxDK;i++){
  224. kbdeadkeys.deadKey[i] = k;
  225. }
  226. kbdeadkeys.max = kbdeadkeys.min = 0;
  227. }
  228. /*
  229. * Adds new element to dead keys table.
  230. */
  231. void
  232. kdbputdeadkey(Rune deadkey, Rune basekey, Rune finalkey){
  233. DeadKey *k=nil;
  234. for (int i=0; i<MaxDK; i++){
  235. if (kbdeadkeys.deadKey[i].key == deadkey || kbdeadkeys.deadKey[i].key == No){
  236. k=&kbdeadkeys.deadKey[i];
  237. break;
  238. }
  239. }
  240. if (k==nil){
  241. print("No more space on kbdeadkeys, MaxDK is too small\n");
  242. error(Ebadarg);
  243. return;
  244. }
  245. k->key=deadkey;
  246. int i;
  247. for(i=0; i<MaxDKResults;i++){
  248. if(k->baseKey[i] == basekey || k->baseKey[i] == No){
  249. k->baseKey[i] = basekey;
  250. k->resultKey[i] = finalkey;
  251. break;
  252. }
  253. }
  254. if(i==MaxDKResults){
  255. print("No more space on DeadKey %d, MaxDKResults is too small\n", deadkey);
  256. error(Ebadarg);
  257. return;
  258. }
  259. if(deadkey>kbdeadkeys.max){
  260. kbdeadkeys.max=deadkey;
  261. }
  262. if(deadkey<kbdeadkeys.min || kbdeadkeys.min==0){
  263. kbdeadkeys.min=deadkey;
  264. }
  265. }
  266. /*
  267. * Process dead keys
  268. */
  269. static Rune
  270. processdeadkey(Rune c){
  271. static DeadKey *k = nil;
  272. // shift must be ignored
  273. if (c == Shift){
  274. return c;
  275. }
  276. if (k != nil) {
  277. // We pressed a dead key before this
  278. if (k->key == c){
  279. // Press two times the same DeadKey = print the dead key
  280. k=nil;
  281. return c;
  282. }
  283. for (int i=0; i<MaxDKResults && k->baseKey[i]!=No; i++){
  284. if(k->baseKey[i] == c){
  285. c = k->resultKey[i];
  286. k=nil;
  287. return c;
  288. }
  289. }
  290. /*
  291. * If we reach this point it is because we pressed a key that can't be
  292. * combined with last dead key or two differents dead keys.
  293. * Both cases are illegal and anulate last dead key.
  294. */
  295. k=nil;
  296. c=No;
  297. }
  298. if (c <= kbdeadkeys.max && c >= kbdeadkeys.min){
  299. // It could be a dead key, let's search it
  300. for(int i=0; i<MaxDK && kbdeadkeys.deadKey[i].key != No; i++){
  301. if(kbdeadkeys.deadKey[i].key == c){
  302. k = &kbdeadkeys.deadKey[i];
  303. c = No;
  304. }
  305. }
  306. }
  307. return c;
  308. }
  309. /*
  310. * true if the kbmap has dead keys
  311. */
  312. static int
  313. hasdeadkeys(){
  314. return !(kbdeadkeys.max==0 && kbdeadkeys.min==0);
  315. }
  316. /*
  317. * wait for output no longer busy
  318. */
  319. static int
  320. outready(void)
  321. {
  322. int tries;
  323. for(tries = 0; (inb(Status) & Outbusy); tries++){
  324. if(tries > 500)
  325. return -1;
  326. delay(2);
  327. }
  328. return 0;
  329. }
  330. /*
  331. * wait for input
  332. */
  333. static int
  334. inready(void)
  335. {
  336. int tries;
  337. for(tries = 0; !(inb(Status) & Inready); tries++){
  338. if(tries > 500)
  339. return -1;
  340. delay(2);
  341. }
  342. return 0;
  343. }
  344. /*
  345. * ask 8042 to reset the machine
  346. */
  347. void
  348. i8042systemreset(void)
  349. {
  350. uint16_t *s = KADDR(0x472);
  351. int i, x;
  352. if(nokeyb)
  353. return;
  354. *s = 0x1234; /* BIOS warm-boot flag */
  355. /*
  356. * newer reset the machine command
  357. */
  358. outready();
  359. outb(Cmd, 0xFE);
  360. outready();
  361. /*
  362. * Pulse it by hand (old somewhat reliable)
  363. */
  364. x = 0xDF;
  365. for(i = 0; i < 5; i++){
  366. x ^= 1;
  367. outready();
  368. outb(Cmd, 0xD1);
  369. outready();
  370. outb(Data, x); /* toggle reset */
  371. delay(100);
  372. }
  373. }
  374. int
  375. i8042auxcmd(int cmd)
  376. {
  377. unsigned int c;
  378. int tries;
  379. c = 0;
  380. tries = 0;
  381. ilock(&i8042lock);
  382. do{
  383. if(tries++ > 2)
  384. break;
  385. if(outready() < 0)
  386. break;
  387. outb(Cmd, 0xD4);
  388. if(outready() < 0)
  389. break;
  390. outb(Data, cmd);
  391. if(outready() < 0)
  392. break;
  393. if(inready() < 0)
  394. break;
  395. c = inb(Data);
  396. } while(c == 0xFE || c == 0);
  397. iunlock(&i8042lock);
  398. if(c != 0xFA){
  399. print("i8042: %2.2ux returned to the %2.2ux command\n", c, cmd);
  400. return -1;
  401. }
  402. return 0;
  403. }
  404. int
  405. i8042auxcmds(uint8_t *cmd, int ncmd)
  406. {
  407. int i;
  408. ilock(&i8042lock);
  409. for(i=0; i<ncmd; i++){
  410. if(outready() < 0)
  411. break;
  412. outb(Cmd, 0xD4);
  413. if(outready() < 0)
  414. break;
  415. outb(Data, cmd[i]);
  416. }
  417. iunlock(&i8042lock);
  418. return i;
  419. }
  420. struct {
  421. int esc1;
  422. int esc2;
  423. int alt;
  424. int altgr;
  425. int caps;
  426. int ctl;
  427. int num;
  428. int shift;
  429. int collecting;
  430. int nk;
  431. Rune kc[5];
  432. int buttons;
  433. } kbscan;
  434. static void
  435. processKey(int c){
  436. int keyup, i;
  437. /*
  438. * e0's is the first of a 2 character sequence, e1 the first
  439. * of a 3 character sequence (on the safari)
  440. */
  441. if(c == 0xe0){
  442. kbscan.esc1 = 1;
  443. return;
  444. } else if(c == 0xe1){
  445. kbscan.esc2 = 2;
  446. return;
  447. }
  448. keyup = c&0x80;
  449. c &= 0x7f;
  450. if(c > sizeof kbtab){
  451. c |= keyup;
  452. if(c != 0xFF) /* these come fairly often: CAPSLOCK U Y */
  453. print("unknown key %ux\n", c);
  454. return;
  455. }
  456. if(kbscan.esc1){
  457. c = kbtabesc1[c];
  458. kbscan.esc1 = 0;
  459. } else if(kbscan.esc2){
  460. kbscan.esc2--;
  461. return;
  462. } else if(kbscan.shift)
  463. c = kbtabshift[c];
  464. else if(kbscan.altgr)
  465. c = kbtabaltgr[c];
  466. else if(kbscan.ctl)
  467. c = kbtabctrl[c];
  468. else
  469. c = kbtab[c];
  470. if(kbscan.caps && c<='z' && c>='a')
  471. c += 'A' - 'a';
  472. /*
  473. * Process dead keys
  474. */
  475. if (hasdeadkeys() && !keyup){
  476. c = processdeadkey(c);
  477. }
  478. /*
  479. * keyup only important for shifts
  480. */
  481. if(keyup){
  482. switch(c){
  483. case Latin:
  484. kbscan.alt = 0;
  485. break;
  486. case Shift:
  487. kbscan.shift = 0;
  488. mouseshifted = 0;
  489. break;
  490. case Ctrl:
  491. kbscan.ctl = 0;
  492. break;
  493. case Altgr:
  494. kbscan.altgr = 0;
  495. break;
  496. case Kmouse|1:
  497. case Kmouse|2:
  498. case Kmouse|3:
  499. case Kmouse|4:
  500. case Kmouse|5:
  501. kbscan.buttons &= ~(1<<(c-Kmouse-1));
  502. if(kbdmouse)
  503. kbdmouse(kbscan.buttons);
  504. break;
  505. }
  506. return;
  507. }
  508. /*
  509. * normal character
  510. */
  511. if(!(c & (Spec|KF))){
  512. if(kbscan.ctl)
  513. if(kbscan.alt && c == Del)
  514. exit(0);
  515. if(!kbscan.collecting){
  516. kbdputc(keybq, c);
  517. return;
  518. }
  519. kbscan.kc[kbscan.nk++] = c;
  520. c = latin1(kbscan.kc, kbscan.nk);
  521. if(c < -1) /* need more keystrokes */
  522. return;
  523. if(c != -1) /* valid sequence */
  524. kbdputc(keybq, c);
  525. else /* dump characters */
  526. for(i=0; i<kbscan.nk; i++)
  527. kbdputc(keybq, kbscan.kc[i]);
  528. kbscan.nk = 0;
  529. kbscan.collecting = 0;
  530. return;
  531. } else {
  532. switch(c){
  533. case Caps:
  534. kbscan.caps ^= 1;
  535. return;
  536. case Num:
  537. kbscan.num ^= 1;
  538. return;
  539. case Shift:
  540. kbscan.shift = 1;
  541. mouseshifted = 1;
  542. return;
  543. case Latin:
  544. kbscan.alt = 1;
  545. /*
  546. * VMware and Qemu use Ctl-Alt as the key combination
  547. * to make the VM give up keyboard and mouse focus.
  548. * This has the unfortunate side effect that when you
  549. * come back into focus, Plan 9 thinks you want to type
  550. * a compose sequence (you just typed alt).
  551. *
  552. * As a clumsy hack around this, we look for ctl-alt
  553. * and don't treat it as the start of a compose sequence.
  554. */
  555. if(!kbscan.ctl){
  556. kbscan.collecting = 1;
  557. kbscan.nk = 0;
  558. }
  559. return;
  560. case Ctrl:
  561. kbscan.ctl = 1;
  562. return;
  563. case Altgr:
  564. kbscan.altgr = 1;
  565. return;
  566. case Kmouse|1:
  567. case Kmouse|2:
  568. case Kmouse|3:
  569. case Kmouse|4:
  570. case Kmouse|5:
  571. kbscan.buttons |= 1<<(c-Kmouse-1);
  572. if(kbdmouse)
  573. kbdmouse(kbscan.buttons);
  574. return;
  575. }
  576. }
  577. kbdputc(keybq, c);
  578. }
  579. /*
  580. * keyboard interrupt
  581. */
  582. static void
  583. i8042intr(Ureg* u, void* v)
  584. {
  585. int s, c;
  586. /*
  587. * get status
  588. */
  589. ilock(&i8042lock);
  590. s = inb(Status);
  591. if((s&Inready) == 0){
  592. iunlock(&i8042lock);
  593. return;
  594. }
  595. /*
  596. * get the character
  597. */
  598. c = inb(Data);
  599. iunlock(&i8042lock);
  600. /*
  601. * if it's the aux port...
  602. */
  603. if(s & Minready){
  604. if(mouseq != nil)
  605. qiwrite(mouseq, &c, 1);
  606. return;
  607. }
  608. processKey(c);
  609. }
  610. void
  611. i8042auxenable(void (*putc)(int, int))
  612. {
  613. char *err = "i8042: aux init failed\n";
  614. /* enable kbd/aux xfers and interrupts */
  615. ccc &= ~Cauxdis;
  616. ccc |= Cauxint;
  617. ilock(&i8042lock);
  618. if(outready() < 0)
  619. print(err);
  620. outb(Cmd, 0x60); /* write control register */
  621. if(outready() < 0)
  622. print(err);
  623. outb(Data, ccc);
  624. if(outready() < 0)
  625. print(err);
  626. outb(Cmd, 0xA8); /* auxilliary device enable */
  627. if(outready() < 0){
  628. iunlock(&i8042lock);
  629. return;
  630. }
  631. auxputc = putc;
  632. intrenable(IrqAUX, i8042intr, 0, BUSUNKNOWN, "kbdaux");
  633. iunlock(&i8042lock);
  634. }
  635. int
  636. mousecmd(int cmd)
  637. {
  638. unsigned int c;
  639. int tries;
  640. static int badkbd;
  641. if(badkbd)
  642. return -1;
  643. c = 0;
  644. tries = 0;
  645. ilock(&i8042lock);
  646. do{
  647. if(tries++ > 2)
  648. break;
  649. if(outready() < 0)
  650. break;
  651. outb(Cmd, 0xD4);
  652. if(outready() < 0)
  653. break;
  654. outb(Data, cmd);
  655. if(outready() < 0)
  656. break;
  657. if(inready() < 0)
  658. break;
  659. c = inb(Data);
  660. } while(c == 0xFE || c == 0);
  661. iunlock(&i8042lock);
  662. if(c != 0xFA){
  663. print("mousecmd: %2.2x returned to the %2.2x command\n", c, cmd);
  664. badkbd = 1; /* don't keep trying; there might not be one */
  665. return -1;
  666. }
  667. return 0;
  668. }
  669. static int
  670. mousecmds(uint8_t *cmd, int ncmd)
  671. {
  672. int i;
  673. ilock(&i8042lock);
  674. for(i=0; i<ncmd; i++){
  675. if(outready() == -1)
  676. break;
  677. outb(Cmd, 0xD4);
  678. if(outready() == -1)
  679. break;
  680. outb(Data, cmd[i]);
  681. }
  682. iunlock(&i8042lock);
  683. return i;
  684. }
  685. void
  686. kbdputsc(int data, int _)
  687. {
  688. processKey(data);
  689. }
  690. static char *initfailed = "i8042: keybinit failed\n";
  691. static int
  692. outbyte(int port, int c)
  693. {
  694. outb(port, c);
  695. if(outready() < 0) {
  696. print(initfailed);
  697. return -1;
  698. }
  699. return 0;
  700. }
  701. static int32_t
  702. mouserwrite(Chan* c, void *vbuf, int32_t len, int64_t off64)
  703. {
  704. return mousecmds(vbuf, len);
  705. }
  706. static int32_t
  707. mouseread(Chan* c, void *vbuf, int32_t len, int64_t off64)
  708. {
  709. return qread(mouseq, vbuf, len);
  710. }
  711. void
  712. mouseenable(void)
  713. {
  714. mouseq = qopen(32, 0, 0, 0);
  715. if(mouseq == nil)
  716. panic("mouseenable");
  717. qnoblock(mouseq, 1);
  718. ccc &= ~Cauxdis;
  719. ccc |= Cauxint;
  720. ilock(&i8042lock);
  721. if(outready() == -1)
  722. iprint("mouseenable: failed 0\n");
  723. outb(Cmd, 0x60); /* write control register */
  724. if(outready() == -1)
  725. iprint("mouseenable: failed 1\n");
  726. outb(Data, ccc);
  727. if(outready() == -1)
  728. iprint("mouseenable: failed 2\n");
  729. outb(Cmd, 0xA8); /* auxilliary device enable */
  730. if(outready() == -1){
  731. iprint("mouseenable: failed 3\n");
  732. iunlock(&i8042lock);
  733. return;
  734. }
  735. iunlock(&i8042lock);
  736. intrenable(IrqAUX, i8042intr, 0, BUSUNKNOWN, "mouse");
  737. addarchfile("ps2mouse", 0666, mouseread, mouserwrite);
  738. }
  739. void
  740. keybinit(void)
  741. {
  742. int c, try;
  743. /* wait for a quiescent controller */
  744. ilock(&i8042lock);
  745. try = 1000;
  746. while(try-- > 0 && (c = inb(Status)) & (Outbusy | Inready)) {
  747. if(c & Inready)
  748. inb(Data);
  749. delay(1);
  750. }
  751. if (try <= 0) {
  752. iunlock(&i8042lock);
  753. print("keybinit failed 0\n");
  754. return;
  755. }
  756. /* get current controller command byte */
  757. outb(Cmd, 0x20);
  758. if(inready() == -1){
  759. iunlock(&i8042lock);
  760. print("keybinit failed 1\n");
  761. ccc = 0;
  762. } else
  763. ccc = inb(Data);
  764. /* enable keyb xfers and interrupts */
  765. ccc &= ~(Ckeybdis);
  766. ccc |= Csf | Ckeybint | Cscs1;
  767. if(outready() == -1) {
  768. iunlock(&i8042lock);
  769. print("keybinit failed 2\n");
  770. return;
  771. }
  772. if (outbyte(Cmd, 0x60) == -1){
  773. iunlock(&i8042lock);
  774. print("keybinit failed 3\n");
  775. return;
  776. }
  777. if (outbyte(Data, ccc) == -1){
  778. iunlock(&i8042lock);
  779. print("keybinit failed 4\n");
  780. return;
  781. }
  782. nokeyb = 0;
  783. initDeadKeys();
  784. iunlock(&i8042lock);
  785. }
  786. static int32_t
  787. keybread(Chan* c, void *vbuf, int32_t len, int64_t off64)
  788. {
  789. return qread(keybq, vbuf, len);
  790. }
  791. void
  792. keybenable(void)
  793. {
  794. keybq = qopen(32, 0, 0, 0);
  795. if(keybq == nil)
  796. panic("keybinit");
  797. qnoblock(keybq, 1);
  798. addkbdq(keybq, -1);
  799. ioalloc(Data, 1, 0, "keyb");
  800. ioalloc(Cmd, 1, 0, "keyb");
  801. intrenable(IrqKBD, i8042intr, 0, MKBUS(BusISA, 0xff, 0xff, 0xff), "keyb");
  802. addarchfile("ps2keyb", 0666, keybread, nil);
  803. }
  804. void
  805. kbdputmap(uint16_t m, uint16_t scanc, Rune r)
  806. {
  807. if(scanc >= Nscan)
  808. error(Ebadarg);
  809. switch(m) {
  810. default:
  811. error(Ebadarg);
  812. case 0:
  813. kbtab[scanc] = r;
  814. break;
  815. case 1:
  816. kbtabshift[scanc] = r;
  817. break;
  818. case 2:
  819. kbtabesc1[scanc] = r;
  820. break;
  821. case 3:
  822. kbtabaltgr[scanc] = r;
  823. break;
  824. case 4:
  825. kbtabctrl[scanc] = r;
  826. break;
  827. }
  828. }
  829. int
  830. kbdgetmap(int offset, int *t, int *sc, Rune *r)
  831. {
  832. *t = offset/Nscan;
  833. *sc = offset%Nscan;
  834. if(*t < 0 || *sc < 0)
  835. error(Ebadarg);
  836. switch(*t) {
  837. default:
  838. return 0;
  839. case 0:
  840. *r = kbtab[*sc];
  841. return 1;
  842. case 1:
  843. *r = kbtabshift[*sc];
  844. return 1;
  845. case 2:
  846. *r = kbtabesc1[*sc];
  847. return 1;
  848. case 3:
  849. *r = kbtabaltgr[*sc];
  850. return 1;
  851. case 4:
  852. *r = kbtabctrl[*sc];
  853. return 1;
  854. }
  855. }