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 Hiddev Hiddev;
35 Dev* dev; /* usb device*/
36 Dev* ep; /* endpoint to get events */
41 Channel *repeatc; /* only for keyboard */
43 /* report descriptor */
48 typedef struct Hidreport Hidreport;
71 * Plan 9 keyboard driver constants.
74 /* Scan codes (see kbd.c) */
75 SCesc1 = 0xe0, /* first of a 2-character sequence */
77 Keyup = 0x80, /* flag bit */
78 Keymask = 0x7f, /* regular scan code bits */
82 * scan codes >= 0x80 are extended (E0 XX)
84 #define isext(sc) ((sc) >= 0x80)
87 * key code to scan code; for the page table used by
88 * the logitech bluetooth keyboard.
90 static char sctab[256] =
92 [0x00] 0x0, 0x0, 0x0, 0x0, 0x1e, 0x30, 0x2e, 0x20,
93 [0x08] 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26,
94 [0x10] 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14,
95 [0x18] 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x2, 0x3,
96 [0x20] 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb,
97 [0x28] 0x1c, 0x1, 0xe, 0xf, 0x39, 0xc, 0xd, 0x1a,
98 [0x30] 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34,
99 [0x38] 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
100 [0x40] 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xe3, 0x46,
101 [0x48] 0xf7, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd,
102 [0x50] 0xcb, 0xd0, 0xc8, 0x45, 0x35, 0x37, 0x4a, 0x4e,
103 [0x58] 0x1c, 0xcf, 0xd0, 0xd1,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,
126 static uchar kbdbootrep[] = {
127 0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, 0x07,
128 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, 0x25, 0x01,
129 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x95, 0x01,
130 0x75, 0x08, 0x81, 0x01, 0x95, 0x05, 0x75, 0x01,
131 0x05, 0x08, 0x19, 0x01, 0x29, 0x05, 0x91, 0x02,
132 0x95, 0x01, 0x75, 0x03, 0x91, 0x01, 0x95, 0x06,
133 0x75, 0x08, 0x15, 0x00, 0x25, 0x65, 0x05, 0x07,
134 0x19, 0x00, 0x29, 0x65, 0x81, 0x00, 0xc0,
137 static uchar ptrbootrep[] = {
138 0x05, 0x01, 0x09, 0x02, 0xa1, 0x01, 0x09, 0x01,
139 0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03,
140 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01,
141 0x81, 0x02, 0x95, 0x01, 0x75, 0x05, 0x81, 0x01,
142 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38,
143 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95, 0x03,
144 0x81, 0x06, 0xc0, 0x09, 0x3c, 0x15, 0x00, 0x25,
145 0x01, 0x75, 0x01, 0x95, 0x01, 0xb1, 0x22, 0x95,
146 0x07, 0xb1, 0x01, 0xc0,
149 static int debug = 0;
152 signext(int v, int bits)
156 s = sizeof(v)*8 - bits;
163 getbits(uchar *p, uchar *e, int bits, int off)
188 UsgCnt = Delim+1, /* fake */
194 repparse1(uchar *d, uchar *e, int g[], int l[], int c,
195 void (*f)(int t, int v, int g[], int l[], int c, void *a), void *a)
208 v = d[-4] | d[-3]<<8 | d[-2]<<16 | d[-1]<<24;
213 v = d[-2] | d[-1]<<8;
222 case 0: /* main item*/
225 memset(l, 0, Nl*sizeof(l[0]));
226 d = repparse1(d, e, g, l, v, f, a);
233 if(l[UsgCnt] == 0 && l[UsagMin] != 0 && l[UsagMin] < l[UsagMax])
234 for(i=l[UsagMin]; i<=l[UsagMax] && l[UsgCnt] < Nu; i++)
235 l[Nl + l[UsgCnt]++] = i;
236 for(i=0; i<g[RepCnt]; i++){
238 l[Usage] = l[Nl + i];
239 (*f)(t, v, g, l, c, a);
243 memset(l, 0, Nl*sizeof(l[0]));
245 case 1: /* global item */
248 memmove(w, g, sizeof(w));
249 d = repparse1(d, e, w, l, c, f, a);
257 else if(t != RepSize && t != RepCnt){
258 v = signext(v, (z == 3) ? 32 : 8*z);
263 case 2: /* local item */
268 } else if(t < Delim){
269 if(z != 3 && (t == Usage || t == UsagMin || t == UsagMax))
270 v = (v & 0xFFFF) | (g[UsagPg] << 16);
272 if(t == Usage && l[UsgCnt] < Nu)
273 l[Nl + l[UsgCnt]++] = v;
276 case 3: /* long item */
286 * parse the report descriptor and call f for every (Input, Output
287 * and Feature) main item as often as it would appear in the report
291 repparse(uchar *d, uchar *e,
292 void (*f)(int t, int v, int g[], int l[], int c, void *a), void *a)
296 memset(l, 0, sizeof(l));
297 memset(g, 0, sizeof(g));
298 repparse1(d, e, g, l, 0, f, a);
302 setproto(Hiddev *f, int eid)
307 iface = f->dev->usb->ep[eid]->iface;
309 f->nrep = usbcmd(f->dev, Rd2h|Rstd|Riface, Rgetdesc, Dreport<<8, id,
310 f->rep, sizeof(f->rep));
315 fprint(2, "report descriptor:");
316 for(i = 0; i < f->nrep; i++){
319 fprint(2, "%#2.2ux ", f->rep[i]);
327 f->nrep = sizeof(kbdbootrep);
328 memmove(f->rep, kbdbootrep, f->nrep);
331 f->nrep = sizeof(ptrbootrep);
332 memmove(f->rep, ptrbootrep, f->nrep);
335 werrstr("no report descriptor");
340 return usbcmd(f->dev, Rh2d|Rclass|Riface, Setproto, proto, id, nil, 0);
344 setleds(Hiddev* f, int, uchar leds)
346 return usbcmd(f->dev, Rh2d|Rclass|Riface, Setreport, Reportout, 0, &leds, 1);
364 hdfatal(Hiddev *f, char *sts)
367 fprint(2, "%s: fatal: %s\n", argv0, sts);
369 fprint(2, "%s: exiting\n", argv0);
370 if(f->repeatc != nil)
371 sendul(f->repeatc, Diemsg);
385 devctl(f->dev, "reset");
388 if(opendevdata(f->dev, ORDWR) >= 0)
393 /* wait for reset to complete */
397 if(setproto(f, f->ep->id) < 0){
398 rerrstr(err, sizeof(err));
406 putscan(Hiddev *f, uchar sc, uchar up)
408 uchar s[2] = {SCesc1, 0};
412 s[1] = up | sc&Keymask;
414 write(f->kinfd, s, 2);
416 write(f->kinfd, s+1, 1);
425 threadsetname("sleepproc");
426 while((ms = recvul(c)) > 0)
432 repeatproc(void* arg)
435 Channel *repeatc, *sleepc;
440 repeatc = f->repeatc;
441 threadsetname("repeatproc");
443 sleepc = chancreate(sizeof(ulong), 0);
445 proccreate(sleepproc, sleepc, Stack);
452 a[1].op = sleepc!=nil ? CHANSND : CHANNOP;
478 stoprepeat(Hiddev *f)
480 sendul(f->repeatc, Awakemsg);
484 startrepeat(Hiddev *f, uchar sc)
486 sendul(f->repeatc, sc);
490 hidparse(int t, int f, int g[], int l[], int, void *a)
498 if(p->p[0] != g[RepId]){
503 p->o = 8; /* skip report id byte */
505 v = getbits(p->p, p->e, g[RepSize], p->o);
508 if((f & (Fconst|Fdata)) != Fdata)
512 fprint(2, "hidparse: t=%x f=%x usage=%x v=%x\n", t, f, l[Usage], v);
514 if((l[Usage]>>16) == 0x07){ /* keycode */
515 if((f & (Fvar|Farray)) == Fvar)
516 if(v != 0) v = l[Usage] & 0xFF;
517 if(p->nk < nelem(p->k) && v != 0)
523 v = signext(v, g[RepSize]);
524 if((f & (Fvar|Farray)) == Fvar && v >= g[LogiMin] && v <= g[LogiMax]){
526 * we use logical units below, but need the
527 * sign to be correct for mouse deltas.
528 * so if physical unit is signed but logical
529 * is unsigned, convert to signed but in logical
532 if((f & (Fabs|Frel)) == Frel
533 && g[PhysMin] < 0 && g[PhysMax] > 0
534 && g[LogiMin] >= 0 && g[LogiMin] < g[LogiMax])
535 v -= (g[PhysMax] * (g[LogiMax] - g[LogiMin])) / (g[PhysMax] - g[PhysMin]);
546 m = 1<<(l[Usage] - 0x090001);
553 if((f & (Fabs|Frel)) == Fabs){
554 p->x = (v - p->absx);
562 if((f & (Fabs|Frel)) == Fabs){
563 p->y = (v - p->absy);
571 if((f & (Fabs|Frel)) == Fabs){
572 p->z = (v - p->absz);
589 snprint(fn, sizeof(fn), "/proc/%d/ctl", getpid());
590 fd = open(fn, OWRITE);
593 fprint(fd, "pri 13");
600 char err[ERRMAX], mbuf[80];
601 uchar lastk[64], uk, dk;
602 int i, c, b, nerrs, lastb, nlastk;
606 threadsetname("readerproc %s", f->ep->dir);
609 memset(&p, 0, sizeof(p));
610 lastb = nlastk = nerrs = 0;
615 if(f->ep->maxpkt < 1 || f->ep->maxpkt > sizeof(p.p))
616 hdfatal(f, "hid: weird maxpkt");
618 memset(p.p, 0, sizeof(p.p));
619 c = read(f->ep->dfd, p.p, f->ep->maxpkt);
622 rerrstr(err, sizeof(err));
624 strcpy(err, "zero read");
625 fprint(2, "%s: hid: %s: read: %s\n", argv0, f->ep->dir, err);
636 repparse(f->rep, f->rep+f->nrep, hidparse, &p);
638 if(p.nk != 0 || nlastk != 0){
641 for(i = 0; i < p.nk; i++)
642 fprint(2, "%#2.2ux ", p.k[i]);
647 f->kinfd = open("/dev/kbin", OWRITE);
649 hdfatal(f, "open /dev/kbin");
651 f->repeatc = chancreate(sizeof(ulong), 0);
652 if(f->repeatc == nil)
653 hdfatal(f, "chancreate failed");
654 proccreate(repeatproc, f, Stack);
658 for(i=0; i<nlastk; i++){
659 if(memchr(p.k, lastk[i], p.nk) == nil){
660 uk = sctab[lastk[i]];
661 putscan(f, uk, Keyup);
664 for(i=0; i<p.nk; i++){
665 if(memchr(lastk, p.k[i], nlastk) == nil){
670 if(uk != 0 && (dk == 0 || dk == uk))
675 memmove(lastk, p.k, nlastk = p.nk);
679 /* map mouse buttons */
686 b |= (p.z > 0) ? 8 : 16;
688 if(p.x != 0 || p.y != 0 || p.z != 0 || b != lastb){
690 fprint(2, "ptr: b=%x m=%x x=%d y=%d z=%d\n", p.b, p.m, p.x, p.y, p.z);
693 f->minfd = open("/dev/mousein", OWRITE);
695 hdfatal(f, "open /dev/mousein");
698 seprint(mbuf, mbuf+sizeof(mbuf), "m%11d %11d %11d", p.x, p.y, b);
699 write(f->minfd, mbuf, strlen(mbuf));
707 hdsetup(Dev *d, Ep *ep)
711 f = emallocz(sizeof(Hiddev), 1);
716 if(setproto(f, ep->id) < 0){
717 fprint(2, "%s: %s: setproto: %r\n", argv0, d->dir);
720 f->ep = openep(f->dev, ep->id);
722 fprint(2, "%s: %s: openep %d: %r\n", argv0, d->dir, ep->id);
725 if(opendevdata(f->ep, OREAD) < 0){
726 fprint(2, "%s: %s: opendevdata: %r\n", argv0, f->ep->dir);
729 procrfork(readerproc, f, Stack, RFNOTEG);
738 fprint(2, "usage: %s [-d] devid\n", argv0);
739 threadexits("usage");
743 threadmain(int argc, char* argv[])
761 sysfatal("getdev: %r");
763 for(i = 0; i < nelem(ud->ep); i++){
764 if((ep = ud->ep[i]) == nil)
766 if(ep->type != Eintr || ep->dir != Ein)
768 switch(ep->iface->csp){