kb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * USB Human Interaction Device: keyboard and mouse.
  3. *
  4. * If there's no usb keyboard, it tries to setup the mouse, if any.
  5. * It should be started at boot time.
  6. *
  7. * Mouse events are converted to the format of mouse(3)'s mousein file.
  8. * Keyboard keycodes are translated to scan codes and sent to kbin(3).
  9. *
  10. * If there is no keyboard, it tries to setup the mouse properly, else it falls
  11. * back to boot protocol.
  12. */
  13. #include <u.h>
  14. #include <libc.h>
  15. #include <thread.h>
  16. #include "usb.h"
  17. #include "hid.h"
  18. enum
  19. {
  20. Awakemsg= 0xdeaddead,
  21. Diemsg = 0xbeefbeef,
  22. Dwcidle = 8,
  23. };
  24. typedef struct KDev KDev;
  25. typedef struct Kin Kin;
  26. struct KDev
  27. {
  28. Dev* dev; /* usb device*/
  29. Dev* ep; /* endpoint to get events */
  30. Kin* in; /* used to send events to kernel */
  31. int idle; /* min time between reports (× 4ms) */
  32. Channel*repeatc; /* only for keyboard */
  33. int accel; /* only for mouse */
  34. int bootp; /* has associated keyboard */
  35. int debug;
  36. HidRepTempl templ;
  37. int (*ptrvals)(KDev *kd, Chain *ch, int *px, int *py, int *pb);
  38. };
  39. /*
  40. * Kbdin and mousein files must be shared among all instances.
  41. */
  42. struct Kin
  43. {
  44. int ref;
  45. int fd;
  46. char* name;
  47. };
  48. /*
  49. * Map for the logitech bluetooth mouse with 8 buttons and wheels.
  50. * { ptr ->mouse}
  51. * { 0x01, 0x01 }, // left
  52. * { 0x04, 0x02 }, // middle
  53. * { 0x02, 0x04 }, // right
  54. * { 0x40, 0x08 }, // up
  55. * { 0x80, 0x10 }, // down
  56. * { 0x10, 0x08 }, // side up
  57. * { 0x08, 0x10 }, // side down
  58. * { 0x20, 0x02 }, // page
  59. * besides wheel and regular up/down report the 4th byte as 1/-1
  60. */
  61. /*
  62. * key code to scan code; for the page table used by
  63. * the logitech bluetooth keyboard.
  64. */
  65. static char sctab[256] =
  66. {
  67. [0x00] 0x0, 0x0, 0x0, 0x0, 0x1e, 0x30, 0x2e, 0x20,
  68. [0x08] 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26,
  69. [0x10] 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14,
  70. [0x18] 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x2, 0x3,
  71. [0x20] 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb,
  72. [0x28] 0x1c, 0x1, 0xe, 0xf, 0x39, 0xc, 0xd, 0x1a,
  73. [0x30] 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34,
  74. [0x38] 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
  75. [0x40] 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0x63, 0x46,
  76. [0x48] 0x77, 0x52, 0x47, 0x49, 0x53, 0x4f, 0x51, 0x4d,
  77. [0x50] 0x4b, 0x50, 0x48, 0x45, 0x35, 0x37, 0x4a, 0x4e,
  78. [0x58] 0x1c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47,
  79. [0x60] 0x48, 0x49, 0x52, 0x53, 0x56, 0x7f, 0x74, 0x75,
  80. [0x68] 0x55, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
  81. [0x70] 0x78, 0x79, 0x7a, 0x7b, 0x0, 0x0, 0x0, 0x0,
  82. [0x78] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x71,
  83. [0x80] 0x73, 0x72, 0x0, 0x0, 0x0, 0x7c, 0x0, 0x0,
  84. [0x88] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  85. [0x90] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  86. [0x98] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  87. [0xa0] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  88. [0xa8] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  89. [0xb0] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  90. [0xb8] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  91. [0xc0] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  92. [0xc8] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  93. [0xd0] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  94. [0xd8] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  95. [0xe0] 0x1d, 0x2a, 0x38, 0x7d, 0x61, 0x36, 0x64, 0x7e,
  96. [0xe8] 0x0, 0x0, 0x0, 0x0, 0x0, 0x73, 0x72, 0x71,
  97. [0xf0] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  98. [0xf8] 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  99. };
  100. static QLock inlck;
  101. static Kin kbdin =
  102. {
  103. .ref = 0,
  104. .name = "#Ι/kbin",
  105. .fd = -1,
  106. };
  107. static Kin ptrin =
  108. {
  109. .ref = 0,
  110. .name = "#m/mousein",
  111. .fd = -1,
  112. };
  113. static int ptrbootpvals(KDev *kd, Chain *ch, int *px, int *py, int *pb);
  114. static int ptrrepvals(KDev *kd, Chain *ch, int *px, int *py, int *pb);
  115. static int
  116. setbootproto(KDev* f, int eid, uchar *, int)
  117. {
  118. int nr, r, id;
  119. f->ptrvals = ptrbootpvals;
  120. r = Rh2d|Rclass|Riface;
  121. dprint(2, "setting boot protocol\n");
  122. id = f->dev->usb->ep[eid]->iface->id;
  123. nr = usbcmd(f->dev, r, Setproto, Bootproto, id, nil, 0);
  124. if(nr < 0)
  125. return -1;
  126. usbcmd(f->dev, r, Setidle, f->idle<<8, id, nil, 0);
  127. return nr;
  128. }
  129. static uchar ignoredesc[128];
  130. static int
  131. setfirstconfig(KDev* f, int eid, uchar *desc, int descsz)
  132. {
  133. int nr, r, id, i;
  134. dprint(2, "setting first config\n");
  135. if(desc == nil){
  136. descsz = sizeof ignoredesc;
  137. desc = ignoredesc;
  138. }
  139. id = f->dev->usb->ep[eid]->iface->id;
  140. r = Rh2d | Rstd | Rdev;
  141. nr = usbcmd(f->dev, r, Rsetconf, 1, 0, nil, 0);
  142. if(nr < 0)
  143. return -1;
  144. r = Rh2d | Rclass | Riface;
  145. nr = usbcmd(f->dev, r, Setidle, f->idle<<8, id, nil, 0);
  146. if(nr < 0)
  147. return -1;
  148. r = Rd2h | Rstd | Riface;
  149. nr=usbcmd(f->dev, r, Rgetdesc, Dreport<<8, id, desc, descsz);
  150. if(nr <= 0)
  151. return -1;
  152. if(f->debug){
  153. fprint(2, "report descriptor:");
  154. for(i = 0; i < nr; i++){
  155. if(i%8 == 0)
  156. fprint(2, "\n\t");
  157. fprint(2, "%#2.2ux ", desc[i]);
  158. }
  159. fprint(2, "\n");
  160. }
  161. f->ptrvals = ptrrepvals;
  162. return nr;
  163. }
  164. /*
  165. * Try to recover from a babble error. A port reset is the only way out.
  166. * BUG: we should be careful not to reset a bundle with several devices.
  167. */
  168. static void
  169. recoverkb(KDev *f)
  170. {
  171. int i;
  172. close(f->dev->dfd); /* it's for usbd now */
  173. devctl(f->dev, "reset");
  174. for(i = 0; i < 10; i++){
  175. if(i == 5)
  176. f->bootp++;
  177. sleep(500);
  178. if(opendevdata(f->dev, ORDWR) >= 0){
  179. if(f->bootp)
  180. /* TODO func pointer */
  181. setbootproto(f, f->ep->id, nil, 0);
  182. else
  183. setfirstconfig(f, f->ep->id, nil, 0);
  184. break;
  185. }
  186. /* else usbd still working... */
  187. }
  188. }
  189. static void
  190. kbfatal(KDev *kd, char *sts)
  191. {
  192. Dev *dev;
  193. if(sts != nil)
  194. fprint(2, "kb: fatal: %s\n", sts);
  195. else
  196. fprint(2, "kb: exiting\n");
  197. if(kd->repeatc != nil)
  198. nbsendul(kd->repeatc, Diemsg);
  199. dev = kd->dev;
  200. kd->dev = nil;
  201. if(kd->ep != nil)
  202. closedev(kd->ep);
  203. kd->ep = nil;
  204. devctl(dev, "detach");
  205. closedev(dev);
  206. /*
  207. * free(kd); done by closedev.
  208. */
  209. threadexits(sts);
  210. }
  211. static int
  212. scale(KDev *f, int x)
  213. {
  214. int sign = 1;
  215. if(x < 0){
  216. sign = -1;
  217. x = -x;
  218. }
  219. switch(x){
  220. case 0:
  221. case 1:
  222. case 2:
  223. case 3:
  224. break;
  225. case 4:
  226. x = 6 + (f->accel>>2);
  227. break;
  228. case 5:
  229. x = 9 + (f->accel>>1);
  230. break;
  231. default:
  232. x *= MaxAcc;
  233. break;
  234. }
  235. return sign*x;
  236. }
  237. /*
  238. * ps2 mouse is processed mostly at interrupt time.
  239. * for usb we do what we can.
  240. */
  241. static void
  242. sethipri(void)
  243. {
  244. char fn[30];
  245. int fd;
  246. snprint(fn, sizeof fn, "/proc/%d/ctl", getpid());
  247. fd = open(fn, OWRITE);
  248. if(fd >= 0) {
  249. fprint(fd, "pri 13");
  250. close(fd);
  251. }
  252. }
  253. static int
  254. ptrrepvals(KDev *kd, Chain *ch, int *px, int *py, int *pb)
  255. {
  256. int i, x, y, b, c;
  257. static char buts[] = {0x0, 0x2, 0x1};
  258. c = ch->e / 8;
  259. /* sometimes there is a report id, sometimes not */
  260. if(c == kd->templ.sz + 1)
  261. if(ch->buf[0] == kd->templ.id)
  262. ch->b += 8;
  263. else
  264. return -1;
  265. parsereport(&kd->templ, ch);
  266. if(kd->debug > 1)
  267. dumpreport(&kd->templ);
  268. if(c < 3)
  269. return -1;
  270. x = hidifcval(&kd->templ, KindX, 0);
  271. y = hidifcval(&kd->templ, KindY, 0);
  272. b = 0;
  273. for(i = 0; i<sizeof buts; i++)
  274. b |= (hidifcval(&kd->templ, KindButtons, i) & 1) << buts[i];
  275. if(c > 3 && hidifcval(&kd->templ, KindWheel, 0) > 0) /* up */
  276. b |= 0x10;
  277. if(c > 3 && hidifcval(&kd->templ, KindWheel, 0) < 0) /* down */
  278. b |= 0x08;
  279. *px = x;
  280. *py = y;
  281. *pb = b;
  282. return 0;
  283. }
  284. static int
  285. ptrbootpvals(KDev *kd, Chain *ch, int *px, int *py, int *pb)
  286. {
  287. int b, c;
  288. char x, y;
  289. static char maptab[] = {0x0, 0x1, 0x4, 0x5, 0x2, 0x3, 0x6, 0x7};
  290. c = ch->e / 8;
  291. if(c < 3)
  292. return -1;
  293. if(kd->templ.nifcs){
  294. x = hidifcval(&kd->templ, KindX, 0);
  295. y = hidifcval(&kd->templ, KindY, 0);
  296. }else{
  297. /* no report descriptor for boot protocol */
  298. x = ((signed char*)ch->buf)[1];
  299. y = ((signed char*)ch->buf)[2];
  300. }
  301. b = maptab[ch->buf[0] & 0x7];
  302. if(c > 3 && ch->buf[3] == 1) /* up */
  303. b |= 0x08;
  304. if(c > 3 && ch->buf[3] == 0xff) /* down */
  305. b |= 0x10;
  306. *px = x;
  307. *py = y;
  308. *pb = b;
  309. return 0;
  310. }
  311. static void
  312. ptrwork(void* a)
  313. {
  314. int hipri, mfd, nerrs, x, y, b, c, ptrfd;
  315. char mbuf[80];
  316. Chain ch;
  317. KDev* f = a;
  318. threadsetname("ptr %s", f->ep->dir);
  319. hipri = nerrs = 0;
  320. ptrfd = f->ep->dfd;
  321. mfd = f->in->fd;
  322. if(f->ep->maxpkt < 3 || f->ep->maxpkt > MaxChLen)
  323. kbfatal(f, "weird mouse maxpkt");
  324. for(;;){
  325. memset(ch.buf, 0, MaxChLen);
  326. if(f->ep == nil)
  327. kbfatal(f, nil);
  328. c = read(ptrfd, ch.buf, f->ep->maxpkt);
  329. assert(f->dev != nil);
  330. assert(f->ep != nil);
  331. if(c < 0){
  332. dprint(2, "kb: mouse: %s: read: %r\n", f->ep->dir);
  333. if(++nerrs < 3){
  334. recoverkb(f);
  335. continue;
  336. }
  337. }
  338. if(c <= 0)
  339. kbfatal(f, nil);
  340. ch.b = 0;
  341. ch.e = 8 * c;
  342. if(f->ptrvals(f, &ch, &x, &y, &b) < 0)
  343. continue;
  344. if(f->accel){
  345. x = scale(f, x);
  346. y = scale(f, y);
  347. }
  348. if(f->debug > 1)
  349. fprint(2, "kb: m%11d %11d %11d\n", x, y, b);
  350. seprint(mbuf, mbuf+sizeof(mbuf), "m%11d %11d %11d", x, y,b);
  351. if(write(mfd, mbuf, strlen(mbuf)) < 0)
  352. kbfatal(f, "mousein i/o");
  353. if(hipri == 0){
  354. sethipri();
  355. hipri = 1;
  356. }
  357. }
  358. }
  359. static void
  360. stoprepeat(KDev *f)
  361. {
  362. sendul(f->repeatc, Awakemsg);
  363. }
  364. static void
  365. startrepeat(KDev *f, uchar esc1, uchar sc)
  366. {
  367. ulong c;
  368. if(esc1)
  369. c = SCesc1 << 8 | (sc & 0xff);
  370. else
  371. c = sc;
  372. sendul(f->repeatc, c);
  373. }
  374. static void
  375. putscan(KDev *f, uchar esc, uchar sc)
  376. {
  377. int kbinfd;
  378. uchar s[2] = {SCesc1, 0};
  379. kbinfd = f->in->fd;
  380. if(sc == 0x41){
  381. f->debug += 2;
  382. return;
  383. }
  384. if(sc == 0x42){
  385. f->debug = 0;
  386. return;
  387. }
  388. if(f->debug > 1)
  389. fprint(2, "sc: %x %x\n", (esc? SCesc1: 0), sc);
  390. s[1] = sc;
  391. if(esc && sc != 0)
  392. write(kbinfd, s, 2);
  393. else if(sc != 0)
  394. write(kbinfd, s+1, 1);
  395. }
  396. static void
  397. repeatproc(void* a)
  398. {
  399. KDev *f;
  400. Channel *repeatc;
  401. ulong l, t, i;
  402. uchar esc1, sc;
  403. threadsetname("kbd repeat");
  404. /*
  405. * too many jumps here.
  406. * Rewrite instead of debug, if needed.
  407. */
  408. f = a;
  409. repeatc = f->repeatc;
  410. l = Awakemsg;
  411. Repeat:
  412. if(l == Diemsg)
  413. goto Abort;
  414. while(l == Awakemsg)
  415. l = recvul(repeatc);
  416. if(l == Diemsg)
  417. goto Abort;
  418. esc1 = l >> 8;
  419. sc = l;
  420. t = 160;
  421. for(;;){
  422. for(i = 0; i < t; i += 5){
  423. if(l = nbrecvul(repeatc))
  424. goto Repeat;
  425. sleep(5);
  426. }
  427. putscan(f, esc1, sc);
  428. t = 30;
  429. }
  430. Abort:
  431. chanfree(repeatc);
  432. threadexits("aborted");
  433. }
  434. #define hasesc1(sc) (((sc) > 0x47) || ((sc) == 0x38))
  435. static void
  436. putmod(KDev *f, uchar mods, uchar omods, uchar mask, uchar esc, uchar sc)
  437. {
  438. /* BUG: Should be a single write */
  439. if((mods&mask) && !(omods&mask))
  440. putscan(f, esc, sc);
  441. if(!(mods&mask) && (omods&mask))
  442. putscan(f, esc, Keyup|sc);
  443. }
  444. /*
  445. * This routine diffs the state with the last known state
  446. * and invents the scan codes that would have been sent
  447. * by a non-usb keyboard in that case. This also requires supplying
  448. * the extra esc1 byte as well as keyup flags.
  449. * The aim is to allow future addition of other keycode pages
  450. * for other keyboards.
  451. */
  452. static uchar
  453. putkeys(KDev *f, uchar buf[], uchar obuf[], int n, uchar dk)
  454. {
  455. int i, j;
  456. uchar uk;
  457. putmod(f, buf[0], obuf[0], Mctrl, 0, SCctrl);
  458. putmod(f, buf[0], obuf[0], (1<<Mlshift), 0, SClshift);
  459. putmod(f, buf[0], obuf[0], (1<<Mrshift), 0, SCrshift);
  460. putmod(f, buf[0], obuf[0], Mcompose, 0, SCcompose);
  461. putmod(f, buf[0], obuf[0], Maltgr, 1, SCcompose);
  462. /* Report key downs */
  463. for(i = 2; i < n; i++){
  464. for(j = 2; j < n; j++)
  465. if(buf[i] == obuf[j])
  466. break;
  467. if(j == n && buf[i] != 0){
  468. dk = sctab[buf[i]];
  469. putscan(f, hasesc1(dk), dk);
  470. startrepeat(f, hasesc1(dk), dk);
  471. }
  472. }
  473. /* Report key ups */
  474. uk = 0;
  475. for(i = 2; i < n; i++){
  476. for(j = 2; j < n; j++)
  477. if(obuf[i] == buf[j])
  478. break;
  479. if(j == n && obuf[i] != 0){
  480. uk = sctab[obuf[i]];
  481. putscan(f, hasesc1(uk), uk|Keyup);
  482. }
  483. }
  484. if(uk && (dk == 0 || dk == uk)){
  485. stoprepeat(f);
  486. dk = 0;
  487. }
  488. return dk;
  489. }
  490. static int
  491. kbdbusy(uchar* buf, int n)
  492. {
  493. int i;
  494. for(i = 1; i < n; i++)
  495. if(buf[i] == 0 || buf[i] != buf[0])
  496. return 0;
  497. return 1;
  498. }
  499. static void
  500. kbdwork(void *a)
  501. {
  502. int c, i, kbdfd, nerrs;
  503. uchar dk, buf[64], lbuf[64];
  504. char err[128];
  505. KDev *f = a;
  506. threadsetname("kbd %s", f->ep->dir);
  507. kbdfd = f->ep->dfd;
  508. if(f->ep->maxpkt < 3 || f->ep->maxpkt > sizeof buf)
  509. kbfatal(f, "weird maxpkt");
  510. f->repeatc = chancreate(sizeof(ulong), 0);
  511. if(f->repeatc == nil)
  512. kbfatal(f, "chancreate failed");
  513. proccreate(repeatproc, f, Stack);
  514. memset(lbuf, 0, sizeof lbuf);
  515. dk = nerrs = 0;
  516. for(;;){
  517. memset(buf, 0, sizeof buf);
  518. c = read(kbdfd, buf, f->ep->maxpkt);
  519. assert(f->dev != nil);
  520. assert(f->ep != nil);
  521. if(c < 0){
  522. rerrstr(err, sizeof(err));
  523. fprint(2, "kb: %s: read: %s\n", f->ep->dir, err);
  524. if(strstr(err, "babble") != 0 && ++nerrs < 3){
  525. recoverkb(f);
  526. continue;
  527. }
  528. }
  529. if(c <= 0)
  530. kbfatal(f, nil);
  531. if(c < 3)
  532. continue;
  533. if(kbdbusy(buf + 2, c - 2))
  534. continue;
  535. if(usbdebug > 2 || f->debug > 1){
  536. fprint(2, "kbd mod %x: ", buf[0]);
  537. for(i = 2; i < c; i++)
  538. fprint(2, "kc %x ", buf[i]);
  539. fprint(2, "\n");
  540. }
  541. dk = putkeys(f, buf, lbuf, f->ep->maxpkt, dk);
  542. memmove(lbuf, buf, c);
  543. nerrs = 0;
  544. }
  545. }
  546. static void
  547. freekdev(void *a)
  548. {
  549. KDev *kd;
  550. kd = a;
  551. if(kd->in != nil){
  552. qlock(&inlck);
  553. if(--kd->in->ref == 0){
  554. close(kd->in->fd);
  555. kd->in->fd = -1;
  556. }
  557. qunlock(&inlck);
  558. }
  559. dprint(2, "freekdev\n");
  560. free(kd);
  561. }
  562. static void
  563. kbstart(Dev *d, Ep *ep, Kin *in, void (*f)(void*), KDev *kd)
  564. {
  565. uchar desc[128];
  566. int n, res;
  567. qlock(&inlck);
  568. if(in->fd < 0){
  569. in->fd = open(in->name, OWRITE);
  570. if(in->fd < 0){
  571. fprint(2, "kb: %s: %r\n", in->name);
  572. qunlock(&inlck);
  573. return;
  574. }
  575. }
  576. in->ref++; /* for kd->in = in */
  577. qunlock(&inlck);
  578. d->free = freekdev;
  579. kd->in = in;
  580. kd->dev = d;
  581. res = -1;
  582. kd->ep = openep(d, ep->id);
  583. if(kd->ep == nil){
  584. fprint(2, "kb: %s: openep %d: %r\n", d->dir, ep->id);
  585. return;
  586. }
  587. if(in == &kbdin){
  588. /*
  589. * DWC OTG controller misses some split transaction inputs.
  590. * Set nonzero idle time to return more frequent reports
  591. * of keyboard state, to avoid losing key up/down events.
  592. */
  593. n = read(d->cfd, desc, sizeof desc - 1);
  594. if(n > 0){
  595. desc[n] = 0;
  596. if(strstr((char*)desc, "dwcotg") != nil)
  597. kd->idle = Dwcidle;
  598. }
  599. }
  600. if(!kd->bootp)
  601. res= setfirstconfig(kd, ep->id, desc, sizeof desc);
  602. if(res > 0)
  603. res = parsereportdesc(&kd->templ, desc, sizeof desc);
  604. /* if we could not set the first config, we give up */
  605. if(kd->bootp || res < 0){
  606. kd->bootp = 1;
  607. if(setbootproto(kd, ep->id, nil, 0) < 0){
  608. fprint(2, "kb: %s: bootproto: %r\n", d->dir);
  609. return;
  610. }
  611. }else if(kd->debug)
  612. dumpreport(&kd->templ);
  613. if(opendevdata(kd->ep, OREAD) < 0){
  614. fprint(2, "kb: %s: opendevdata: %r\n", kd->ep->dir);
  615. closedev(kd->ep);
  616. kd->ep = nil;
  617. return;
  618. }
  619. incref(d);
  620. proccreate(f, kd, Stack);
  621. }
  622. static int
  623. usage(void)
  624. {
  625. werrstr("usage: usb/kb [-bdkm] [-a n] [-N nb]");
  626. return -1;
  627. }
  628. int
  629. kbmain(Dev *d, int argc, char* argv[])
  630. {
  631. int bootp, i, kena, pena, accel, devid, debug;
  632. Ep *ep;
  633. KDev *kd;
  634. Usbdev *ud;
  635. kena = pena = 1;
  636. bootp = 0;
  637. accel = 0;
  638. debug = 0;
  639. devid = d->id;
  640. ARGBEGIN{
  641. case 'a':
  642. accel = strtol(EARGF(usage()), nil, 0);
  643. break;
  644. case 'd':
  645. debug++;
  646. break;
  647. case 'k':
  648. kena = 1;
  649. pena = 0;
  650. break;
  651. case 'm':
  652. kena = 0;
  653. pena = 1;
  654. break;
  655. case 'N':
  656. devid = atoi(EARGF(usage())); /* ignore dev number */
  657. break;
  658. case 'b':
  659. bootp++;
  660. break;
  661. default:
  662. return usage();
  663. }ARGEND;
  664. if(argc != 0)
  665. return usage();
  666. USED(devid);
  667. ud = d->usb;
  668. d->aux = nil;
  669. dprint(2, "kb: main: dev %s ref %ld\n", d->dir, d->ref);
  670. if(kena)
  671. for(i = 0; i < nelem(ud->ep); i++)
  672. if((ep = ud->ep[i]) == nil)
  673. break;
  674. else if(ep->iface->csp == KbdCSP)
  675. bootp = 1;
  676. for(i = 0; i < nelem(ud->ep); i++){
  677. if((ep = ud->ep[i]) == nil)
  678. break;
  679. if(kena && ep->type == Eintr && ep->dir == Ein &&
  680. ep->iface->csp == KbdCSP){
  681. kd = d->aux = emallocz(sizeof(KDev), 1);
  682. kd->accel = 0;
  683. kd->bootp = 1;
  684. kd->debug = debug;
  685. kbstart(d, ep, &kbdin, kbdwork, kd);
  686. }
  687. if(pena && ep->type == Eintr && ep->dir == Ein &&
  688. ep->iface->csp == PtrCSP){
  689. kd = d->aux = emallocz(sizeof(KDev), 1);
  690. kd->accel = accel;
  691. kd->bootp = bootp;
  692. kd->debug = debug;
  693. kbstart(d, ep, &ptrin, ptrwork, kd);
  694. }
  695. }
  696. return 0;
  697. }