2 * USB Human Interaction Device: keyboard and mouse.
4 * If there's no usb keyboard, it tries to setup the mouse, if any.
5 * It should be started at boot time.
7 * Mouse events are converted to the format of mouse(3)
9 * Keyboard keycodes are translated to scan codes and sent to kbdfs(8)
32 typedef struct KDev KDev;
35 Dev* dev; /* usb device*/
36 Dev* ep; /* endpoint to get events */
37 int infd; /* used to send events to kernel */
38 Channel *repeatc; /* only for keyboard */
40 /* report descriptor */
46 * scan codes >= 0x80 are extended (E0 XX)
48 #define isext(sc) ((sc) >= 0x80)
51 * key code to scan code; for the page table used by
52 * the logitech bluetooth keyboard.
54 static char sctab[256] =
56 [0x00] 0x0, 0x0, 0x0, 0x0, 0x1e, 0x30, 0x2e, 0x20,
57 [0x08] 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26,
58 [0x10] 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14,
59 [0x18] 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x2, 0x3,
60 [0x20] 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb,
61 [0x28] 0x1c, 0x1, 0xe, 0xf, 0x39, 0xc, 0xd, 0x1a,
62 [0x30] 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34,
63 [0x38] 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
64 [0x40] 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xe3, 0x46,
65 [0x48] 0xf7, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd,
66 [0x50] 0xcb, 0xd0, 0xc8, 0x45, 0x35, 0x37, 0x4a, 0x4e,
67 [0x58] 0x1c, 0xcf, 0xd0, 0xd1, 0xcb, 0xcc, 0xcd, 0xc7,
68 [0x60] 0xc8, 0xc9, 0xd2, 0xd3, 0x56, 0xff, 0xf4, 0xf5,
69 [0x68] 0xd5, 0xd9, 0xda, 0xdb, 0xdc, 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00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01,
94 0x81, 0x02, 0x95, 0x01, 0x75, 0x05, 0x81, 0x01,
95 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38,
96 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95, 0x03,
97 0x81, 0x06, 0xc0, 0x09, 0x3c, 0x15, 0x00, 0x25,
98 0x01, 0x75, 0x01, 0x95, 0x01, 0xb1, 0x22, 0x95,
99 0x07, 0xb1, 0x01, 0xc0,
105 signext(int v, int bits)
109 s = sizeof(v)*8 - bits;
116 getbits(uchar *p, uchar *e, int bits, int off)
141 UsgCnt = Delim+1, /* fake */
147 repparse1(uchar *d, uchar *e, int g[], int l[], int c,
148 void (*f)(int t, int v, int g[], int l[], int c, void *a), void *a)
161 v = d[-4] | d[-3]<<8 | d[-2]<<16 | d[-1]<<24;
166 v = d[-2] | d[-1]<<8;
175 case 0: /* main item*/
178 memset(l, 0, Nl*sizeof(l[0]));
179 d = repparse1(d, e, g, l, v, f, a);
186 if(l[UsgCnt] == 0 && l[UsagMin] != 0 && l[UsagMin] < l[UsagMax])
187 for(i=l[UsagMin]; i<=l[UsagMax] && l[UsgCnt] < Nu; i++)
188 l[Nl + l[UsgCnt]++] = i;
189 for(i=0; i<g[RepCnt]; i++){
191 l[Usage] = l[Nl + i];
192 (*f)(t, v, g, l, c, a);
196 memset(l, 0, Nl*sizeof(l[0]));
198 case 1: /* global item */
201 memmove(w, g, sizeof(w));
202 d = repparse1(d, e, w, l, c, f, a);
210 else if(t != RepSize && t != RepCnt){
211 v = signext(v, (z == 3) ? 32 : 8*z);
216 case 2: /* local item */
221 } else if(t < Delim){
222 if(z != 3 && (t == Usage || t == UsagMin || t == UsagMax))
223 v = (v & 0xFFFF) | (g[UsagPg] << 16);
225 if(t == Usage && l[UsgCnt] < Nu)
226 l[Nl + l[UsgCnt]++] = v;
229 case 3: /* long item */
239 * parse the report descriptor and call f for every (Input, Output
240 * and Feature) main item as often as it would appear in the report
244 repparse(uchar *d, uchar *e,
245 void (*f)(int t, int v, int g[], int l[], int c, void *a), void *a)
249 memset(l, 0, sizeof(l));
250 memset(g, 0, sizeof(g));
251 repparse1(d, e, g, l, 0, f, a);
255 setproto(KDev *f, int eid)
261 iface = f->dev->usb->ep[eid]->iface;
263 if(iface->csp == PtrCSP || iface->csp == Ptr2CSP){
264 f->nrep = usbcmd(f->dev, Rd2h|Rstd|Riface, Rgetdesc, Dreport<<8, id,
265 f->rep, sizeof(f->rep));
270 fprint(2, "report descriptor:");
271 for(i = 0; i < f->nrep; i++){
274 fprint(2, "%#2.2ux ", f->rep[i]);
280 f->nrep = sizeof(ptrbootrep);
281 memmove(f->rep, ptrbootrep, f->nrep);
284 return usbcmd(f->dev, Rh2d|Rclass|Riface, Setproto, proto, id, nil, 0);
288 setleds(KDev* f, int, uchar leds)
290 return usbcmd(f->dev, Rh2d|Rclass|Riface, Setreport, Reportout, 0, &leds, 1);
294 * Try to recover from a babble error. A port reset is the only way out.
295 * BUG: we should be careful not to reset a bundle with several devices.
302 close(f->dev->dfd); /* it's for usbd now */
303 devctl(f->dev, "reset");
304 for(i = 0; i < 10; i++){
306 if(opendevdata(f->dev, ORDWR) >= 0){
307 setproto(f, f->ep->id);
310 /* else usbd still working... */
327 kbfatal(KDev *kd, char *sts)
330 fprint(2, "%s: fatal: %s\n", argv0, sts);
332 fprint(2, "%s: exiting\n", argv0);
333 if(kd->repeatc != nil)
334 sendul(kd->repeatc, Diemsg);
340 kbprocname(KDev *kd, char *name)
343 snprint(buf, sizeof(buf), "%s %s", name, kd->ep->dir);
353 snprint(fn, sizeof(fn), "/proc/%d/ctl", getpid());
354 fd = open(fn, OWRITE);
357 fprint(fd, "pri 13");
361 typedef struct Ptr Ptr;
381 ptrparse(int t, int f, int g[], int l[], int, void *a)
389 if(p->p[0] != g[RepId]){
394 p->o = 8; /* skip report id byte */
396 v = getbits(p->p, p->e, g[RepSize], p->o);
398 v = signext(v, g[RepSize]);
399 if((f & (Fvar|Farray)) == Fvar && v >= g[LogiMin] && v <= g[LogiMax]){
401 * we use logical units below, but need the
402 * sign to be correct for mouse deltas.
403 * so if physical unit is signed but logical
404 * is unsigned, convert to signed but in logical
407 if((f & (Fabs|Frel)) == Frel
408 && g[PhysMin] < 0 && g[PhysMax] > 0
409 && g[LogiMin] >= 0 && g[LogiMin] < g[LogiMax])
410 v -= (g[PhysMax] * (g[LogiMax] - g[LogiMin])) / (g[PhysMax] - g[PhysMin]);
421 m = 1<<(l[Usage] - 0x090001);
428 if((f & (Fabs|Frel)) == Fabs){
429 p->x = (v - p->absx);
437 if((f & (Fabs|Frel)) == Fabs){
438 p->y = (v - p->absy);
446 if((f & (Fabs|Frel)) == Fabs){
447 p->z = (v - p->absz);
464 int c, b, nerrs, lastb;
468 kbprocname(f, "ptr");
471 memset(&p, 0, sizeof(p));
478 if(f->ep->maxpkt < 1 || f->ep->maxpkt > sizeof(p.p))
479 kbfatal(f, "ptr: weird mouse maxpkt");
481 memset(p.p, 0, sizeof(p.p));
482 c = read(f->ep->dfd, p.p, f->ep->maxpkt);
485 rerrstr(err, sizeof(err));
487 strcpy(err, "zero read");
489 fprint(2, "%s: ptr: %s: read: %s\n", argv0, f->ep->dir, err);
490 if(strstr(err, "babble") != 0)
500 repparse(f->rep, f->rep+f->nrep, ptrparse, &p);
503 fprint(2, "ptr: b=%x m=%x x=%d y=%d z=%d\n", p.b, p.m, p.x, p.y, p.z);
512 b |= (p.z > 0) ? 8 : 16;
514 if(p.x == 0 && p.y == 0 && p.z == 0 && b == lastb)
518 seprint(mbuf, mbuf+sizeof(mbuf), "m%11d %11d %11d", p.x, p.y, b);
519 if(write(f->infd, mbuf, strlen(mbuf)) < 0)
520 kbfatal(f, "mousein i/o");
525 putscan(int fd, uchar sc, uchar up)
527 uchar s[2] = {SCesc1, 0};
531 s[1] = up | sc&Keymask;
539 putmod(int fd, uchar mods, uchar omods, uchar mask, uchar esc, uchar sc)
544 if((mods&mask) && !(omods&mask)){
549 if(!(mods&mask) && (omods&mask)){
564 threadsetname("sleepproc");
565 while((ms = recvul(c)) > 0)
571 repeatproc(void* arg)
574 Channel *repeatc, *sleepc;
580 repeatc = f->repeatc;
582 threadsetname("repeatproc");
584 sleepc = chancreate(sizeof(ulong), 0);
586 proccreate(sleepproc, sleepc, Stack);
593 a[1].op = sleepc!=nil ? CHANSND : CHANNOP;
609 putscan(kbdinfd, sc, 0);
621 sendul(f->repeatc, Awakemsg);
625 startrepeat(KDev *f, uchar sc)
627 sendul(f->repeatc, sc);
631 * This routine diffs the state with the last known state
632 * and invents the scan codes that would have been sent
633 * by a non-usb keyboard in that case. This also requires supplying
634 * the extra esc1 byte as well as keyup flags.
635 * The aim is to allow future addition of other keycode pages
636 * for other keyboards.
639 putkeys(KDev *f, uchar buf[], uchar obuf[], int n, uchar dk)
646 putmod(fd, buf[0], obuf[0], Mctrl, 0, SCctrl);
647 putmod(fd, buf[0], obuf[0], (1<<Mlshift), 0, SClshift);
648 putmod(fd, buf[0], obuf[0], (1<<Mrshift), 0, SCrshift);
649 putmod(fd, buf[0], obuf[0], Mcompose, 0, SCcompose);
650 putmod(fd, buf[0], obuf[0], Maltgr, 1, SCcompose);
652 /* Report key downs */
653 for(i = 2; i < n; i++){
654 for(j = 2; j < n; j++)
655 if(buf[i] == obuf[j])
657 if(j == n && buf[i] != 0){
666 for(i = 2; i < n; i++){
667 for(j = 2; j < n; j++)
668 if(obuf[i] == buf[j])
670 if(j == n && obuf[i] != 0){
672 putscan(fd, uk, Keyup);
675 if(uk && (dk == 0 || dk == uk)){
683 kbdbusy(uchar* buf, int n)
687 for(i = 1; i < n; i++)
688 if(buf[i] == 0 || buf[i] != buf[0])
696 uchar dk, buf[64], lbuf[64];
701 kbprocname(f, "kbd");
703 f->repeatc = chancreate(sizeof(ulong), 0);
704 if(f->repeatc == nil)
705 kbfatal(f, "chancreate failed");
706 proccreate(repeatproc, f, Stack);
708 setleds(f, f->ep->id, 0);
711 memset(lbuf, 0, sizeof lbuf);
716 if(f->ep->maxpkt < 3 || f->ep->maxpkt > sizeof buf)
717 kbfatal(f, "kbd: weird maxpkt");
718 memset(buf, 0, sizeof buf);
719 c = read(f->ep->dfd, buf, f->ep->maxpkt);
722 rerrstr(err, sizeof(err));
724 strcpy(err, "zero read");
726 fprint(2, "%s: kbd: %s: read: %s\n", argv0, f->ep->dir, err);
727 if(strstr(err, "babble") != 0)
736 if(kbdbusy(buf + 2, c - 2))
738 if(usbdebug > 2 || debug > 1){
739 fprint(2, "kbd mod %x: ", buf[0]);
740 for(i = 2; i < c; i++)
741 fprint(2, "kc %x ", buf[i]);
744 dk = putkeys(f, buf, lbuf, f->ep->maxpkt, dk);
745 memmove(lbuf, buf, c);
750 kbstart(Dev *d, Ep *ep, char *infile, void (*f)(void*))
754 kd = emallocz(sizeof(KDev), 1);
755 kd->infd = open(infile, OWRITE);
757 fprint(2, "%s: %s: open: %r\n", argv0, d->dir);
762 if(setproto(kd, ep->id) < 0){
763 fprint(2, "%s: %s: setproto: %r\n", argv0, d->dir);
766 kd->ep = openep(kd->dev, ep->id);
768 fprint(2, "%s: %s: openep %d: %r\n", argv0, d->dir, ep->id);
771 if(opendevdata(kd->ep, OREAD) < 0){
772 fprint(2, "%s: %s: opendevdata: %r\n", argv0, kd->ep->dir);
775 procrfork(f, kd, Stack, RFNOTEG);
784 fprint(2, "usage: %s [-d] devid\n", argv0);
785 threadexits("usage");
789 threadmain(int argc, char* argv[])
805 d = getdev(atoi(*argv));
807 sysfatal("getdev: %r");
809 for(i = 0; i < nelem(ud->ep); i++){
810 if((ep = ud->ep[i]) == nil)
812 if(ep->type == Eintr && ep->dir == Ein && ep->iface->csp == KbdCSP)
813 kbstart(d, ep, "/dev/kbin", kbdwork);
814 if(ep->type == Eintr && ep->dir == Ein && (ep->iface->csp == PtrCSP || ep->iface->csp == Ptr2CSP))
815 kbstart(d, ep, "/dev/mousein", ptrwork);