i8042.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. /*
  435. * keyboard interrupt
  436. */
  437. static void
  438. i8042intr(Ureg* u, void* v)
  439. {
  440. int s, c, i;
  441. int keyup;
  442. /*
  443. * get status
  444. */
  445. ilock(&i8042lock);
  446. s = inb(Status);
  447. if((s&Inready) == 0){
  448. iunlock(&i8042lock);
  449. return;
  450. }
  451. /*
  452. * get the character
  453. */
  454. c = inb(Data);
  455. iunlock(&i8042lock);
  456. /*
  457. * if it's the aux port...
  458. */
  459. if(s & Minready){
  460. if(mouseq != nil)
  461. qiwrite(mouseq, &c, 1);
  462. return;
  463. }
  464. /*
  465. * e0's is the first of a 2 character sequence, e1 the first
  466. * of a 3 character sequence (on the safari)
  467. */
  468. if(c == 0xe0){
  469. kbscan.esc1 = 1;
  470. return;
  471. } else if(c == 0xe1){
  472. kbscan.esc2 = 2;
  473. return;
  474. }
  475. keyup = c&0x80;
  476. c &= 0x7f;
  477. if(c > sizeof kbtab){
  478. c |= keyup;
  479. if(c != 0xFF) /* these come fairly often: CAPSLOCK U Y */
  480. print("unknown key %ux\n", c);
  481. return;
  482. }
  483. if(kbscan.esc1){
  484. c = kbtabesc1[c];
  485. kbscan.esc1 = 0;
  486. } else if(kbscan.esc2){
  487. kbscan.esc2--;
  488. return;
  489. } else if(kbscan.shift)
  490. c = kbtabshift[c];
  491. else if(kbscan.altgr)
  492. c = kbtabaltgr[c];
  493. else if(kbscan.ctl)
  494. c = kbtabctrl[c];
  495. else
  496. c = kbtab[c];
  497. if(kbscan.caps && c<='z' && c>='a')
  498. c += 'A' - 'a';
  499. /*
  500. * Process dead keys
  501. */
  502. if (hasdeadkeys() && !keyup){
  503. c = processdeadkey(c);
  504. }
  505. /*
  506. * keyup only important for shifts
  507. */
  508. if(keyup){
  509. switch(c){
  510. case Latin:
  511. kbscan.alt = 0;
  512. break;
  513. case Shift:
  514. kbscan.shift = 0;
  515. mouseshifted = 0;
  516. break;
  517. case Ctrl:
  518. kbscan.ctl = 0;
  519. break;
  520. case Altgr:
  521. kbscan.altgr = 0;
  522. break;
  523. case Kmouse|1:
  524. case Kmouse|2:
  525. case Kmouse|3:
  526. case Kmouse|4:
  527. case Kmouse|5:
  528. kbscan.buttons &= ~(1<<(c-Kmouse-1));
  529. if(kbdmouse)
  530. kbdmouse(kbscan.buttons);
  531. break;
  532. }
  533. return;
  534. }
  535. /*
  536. * normal character
  537. */
  538. if(!(c & (Spec|KF))){
  539. if(kbscan.ctl)
  540. if(kbscan.alt && c == Del)
  541. exit(0);
  542. if(!kbscan.collecting){
  543. kbdputc(keybq, c);
  544. return;
  545. }
  546. kbscan.kc[kbscan.nk++] = c;
  547. c = latin1(kbscan.kc, kbscan.nk);
  548. if(c < -1) /* need more keystrokes */
  549. return;
  550. if(c != -1) /* valid sequence */
  551. kbdputc(keybq, c);
  552. else /* dump characters */
  553. for(i=0; i<kbscan.nk; i++)
  554. kbdputc(keybq, kbscan.kc[i]);
  555. kbscan.nk = 0;
  556. kbscan.collecting = 0;
  557. return;
  558. } else {
  559. switch(c){
  560. case Caps:
  561. kbscan.caps ^= 1;
  562. return;
  563. case Num:
  564. kbscan.num ^= 1;
  565. return;
  566. case Shift:
  567. kbscan.shift = 1;
  568. mouseshifted = 1;
  569. return;
  570. case Latin:
  571. kbscan.alt = 1;
  572. /*
  573. * VMware and Qemu use Ctl-Alt as the key combination
  574. * to make the VM give up keyboard and mouse focus.
  575. * This has the unfortunate side effect that when you
  576. * come back into focus, Plan 9 thinks you want to type
  577. * a compose sequence (you just typed alt).
  578. *
  579. * As a clumsy hack around this, we look for ctl-alt
  580. * and don't treat it as the start of a compose sequence.
  581. */
  582. if(!kbscan.ctl){
  583. kbscan.collecting = 1;
  584. kbscan.nk = 0;
  585. }
  586. return;
  587. case Ctrl:
  588. kbscan.ctl = 1;
  589. return;
  590. case Altgr:
  591. kbscan.altgr = 1;
  592. return;
  593. case Kmouse|1:
  594. case Kmouse|2:
  595. case Kmouse|3:
  596. case Kmouse|4:
  597. case Kmouse|5:
  598. kbscan.buttons |= 1<<(c-Kmouse-1);
  599. if(kbdmouse)
  600. kbdmouse(kbscan.buttons);
  601. return;
  602. }
  603. }
  604. kbdputc(keybq, c);
  605. }
  606. void
  607. i8042auxenable(void (*putc)(int, int))
  608. {
  609. char *err = "i8042: aux init failed\n";
  610. /* enable kbd/aux xfers and interrupts */
  611. ccc &= ~Cauxdis;
  612. ccc |= Cauxint;
  613. ilock(&i8042lock);
  614. if(outready() < 0)
  615. print(err);
  616. outb(Cmd, 0x60); /* write control register */
  617. if(outready() < 0)
  618. print(err);
  619. outb(Data, ccc);
  620. if(outready() < 0)
  621. print(err);
  622. outb(Cmd, 0xA8); /* auxilliary device enable */
  623. if(outready() < 0){
  624. iunlock(&i8042lock);
  625. return;
  626. }
  627. auxputc = putc;
  628. intrenable(IrqAUX, i8042intr, 0, BUSUNKNOWN, "kbdaux");
  629. iunlock(&i8042lock);
  630. }
  631. int
  632. mousecmd(int cmd)
  633. {
  634. unsigned int c;
  635. int tries;
  636. static int badkbd;
  637. if(badkbd)
  638. return -1;
  639. c = 0;
  640. tries = 0;
  641. ilock(&i8042lock);
  642. do{
  643. if(tries++ > 2)
  644. break;
  645. if(outready() < 0)
  646. break;
  647. outb(Cmd, 0xD4);
  648. if(outready() < 0)
  649. break;
  650. outb(Data, cmd);
  651. if(outready() < 0)
  652. break;
  653. if(inready() < 0)
  654. break;
  655. c = inb(Data);
  656. } while(c == 0xFE || c == 0);
  657. iunlock(&i8042lock);
  658. if(c != 0xFA){
  659. print("mousecmd: %2.2x returned to the %2.2x command\n", c, cmd);
  660. badkbd = 1; /* don't keep trying; there might not be one */
  661. return -1;
  662. }
  663. return 0;
  664. }
  665. static int
  666. mousecmds(uint8_t *cmd, int ncmd)
  667. {
  668. int i;
  669. ilock(&i8042lock);
  670. for(i=0; i<ncmd; i++){
  671. if(outready() == -1)
  672. break;
  673. outb(Cmd, 0xD4);
  674. if(outready() == -1)
  675. break;
  676. outb(Data, cmd[i]);
  677. }
  678. iunlock(&i8042lock);
  679. return i;
  680. }
  681. void
  682. kbdputsc(int data, int _)
  683. {
  684. qiwrite(keybq, &data, 1);
  685. }
  686. static char *initfailed = "i8042: keybinit failed\n";
  687. static int
  688. outbyte(int port, int c)
  689. {
  690. outb(port, c);
  691. if(outready() < 0) {
  692. print(initfailed);
  693. return -1;
  694. }
  695. return 0;
  696. }
  697. static int32_t
  698. mouserwrite(Chan* c, void *vbuf, int32_t len, int64_t off64)
  699. {
  700. return mousecmds(vbuf, len);
  701. }
  702. static int32_t
  703. mouseread(Chan* c, void *vbuf, int32_t len, int64_t off64)
  704. {
  705. return qread(mouseq, vbuf, len);
  706. }
  707. void
  708. mouseenable(void)
  709. {
  710. mouseq = qopen(32, 0, 0, 0);
  711. if(mouseq == nil)
  712. panic("mouseenable");
  713. qnoblock(mouseq, 1);
  714. ccc &= ~Cauxdis;
  715. ccc |= Cauxint;
  716. ilock(&i8042lock);
  717. if(outready() == -1)
  718. iprint("mouseenable: failed 0\n");
  719. outb(Cmd, 0x60); /* write control register */
  720. if(outready() == -1)
  721. iprint("mouseenable: failed 1\n");
  722. outb(Data, ccc);
  723. if(outready() == -1)
  724. iprint("mouseenable: failed 2\n");
  725. outb(Cmd, 0xA8); /* auxilliary device enable */
  726. if(outready() == -1){
  727. iprint("mouseenable: failed 3\n");
  728. iunlock(&i8042lock);
  729. return;
  730. }
  731. iunlock(&i8042lock);
  732. intrenable(IrqAUX, i8042intr, 0, BUSUNKNOWN, "mouse");
  733. addarchfile("ps2mouse", 0666, mouseread, mouserwrite);
  734. }
  735. void
  736. keybinit(void)
  737. {
  738. int c, try;
  739. /* wait for a quiescent controller */
  740. ilock(&i8042lock);
  741. try = 1000;
  742. while(try-- > 0 && (c = inb(Status)) & (Outbusy | Inready)) {
  743. if(c & Inready)
  744. inb(Data);
  745. delay(1);
  746. }
  747. if (try <= 0) {
  748. iunlock(&i8042lock);
  749. print("keybinit failed 0\n");
  750. return;
  751. }
  752. /* get current controller command byte */
  753. outb(Cmd, 0x20);
  754. if(inready() == -1){
  755. iunlock(&i8042lock);
  756. print("keybinit failed 1\n");
  757. ccc = 0;
  758. } else
  759. ccc = inb(Data);
  760. /* enable keyb xfers and interrupts */
  761. ccc &= ~(Ckeybdis);
  762. ccc |= Csf | Ckeybint | Cscs1;
  763. if(outready() == -1) {
  764. iunlock(&i8042lock);
  765. print("keybinit failed 2\n");
  766. return;
  767. }
  768. if (outbyte(Cmd, 0x60) == -1){
  769. iunlock(&i8042lock);
  770. print("keybinit failed 3\n");
  771. return;
  772. }
  773. if (outbyte(Data, ccc) == -1){
  774. iunlock(&i8042lock);
  775. print("keybinit failed 4\n");
  776. return;
  777. }
  778. nokeyb = 0;
  779. initDeadKeys();
  780. iunlock(&i8042lock);
  781. }
  782. static int32_t
  783. keybread(Chan* c, void *vbuf, int32_t len, int64_t off64)
  784. {
  785. return qread(keybq, vbuf, len);
  786. }
  787. void
  788. keybenable(void)
  789. {
  790. keybq = qopen(32, 0, 0, 0);
  791. if(keybq == nil)
  792. panic("keybinit");
  793. qnoblock(keybq, 1);
  794. addkbdq(keybq, -1);
  795. ioalloc(Data, 1, 0, "keyb");
  796. ioalloc(Cmd, 1, 0, "keyb");
  797. intrenable(IrqKBD, i8042intr, 0, MKBUS(BusISA, 0xff, 0xff, 0xff), "keyb");
  798. addarchfile("ps2keyb", 0666, keybread, nil);
  799. }
  800. void
  801. kbdputmap(uint16_t m, uint16_t scanc, Rune r)
  802. {
  803. if(scanc >= Nscan)
  804. error(Ebadarg);
  805. switch(m) {
  806. default:
  807. error(Ebadarg);
  808. case 0:
  809. kbtab[scanc] = r;
  810. break;
  811. case 1:
  812. kbtabshift[scanc] = r;
  813. break;
  814. case 2:
  815. kbtabesc1[scanc] = r;
  816. break;
  817. case 3:
  818. kbtabaltgr[scanc] = r;
  819. break;
  820. case 4:
  821. kbtabctrl[scanc] = r;
  822. break;
  823. }
  824. }
  825. int
  826. kbdgetmap(int offset, int *t, int *sc, Rune *r)
  827. {
  828. *t = offset/Nscan;
  829. *sc = offset%Nscan;
  830. if(*t < 0 || *sc < 0)
  831. error(Ebadarg);
  832. switch(*t) {
  833. default:
  834. return 0;
  835. case 0:
  836. *r = kbtab[*sc];
  837. return 1;
  838. case 1:
  839. *r = kbtabshift[*sc];
  840. return 1;
  841. case 2:
  842. *r = kbtabesc1[*sc];
  843. return 1;
  844. case 3:
  845. *r = kbtabaltgr[*sc];
  846. return 1;
  847. case 4:
  848. *r = kbtabctrl[*sc];
  849. return 1;
  850. }
  851. }