Make the usb/kb debug flag local to each device instead of global, to make it easier to debug multiple devices, and prevent inadvertent promotion of -d to -dd when extra devices are started. Also fallback to boot protocol if fetching the report descriptor returns zero bytes (should be impossible but it's been observed). Reference: /n/sources/patch/applied/usb-kb-debug Date: Tue Dec 18 20:52:44 CET 2012 Signed-off-by: miller@hamnavoe.com --- /sys/src/cmd/usb/kb/kb.c Thu Dec 13 21:34:14 2012 +++ /sys/src/cmd/usb/kb/kb.c Tue Dec 18 20:51:13 2012 @@ -36,6 +36,7 @@ Channel*repeatc; /* only for keyboard */ int accel; /* only for mouse */ int bootp; /* has associated keyboard */ + int debug; HidRepTempl templ; int (*ptrvals)(KDev *kd, Chain *ch, int *px, int *py, int *pb); }; @@ -118,8 +119,6 @@ .fd = -1, }; -static int kbdebug; - static int ptrbootpvals(KDev *kd, Chain *ch, int *px, int *py, int *pb); static int ptrrepvals(KDev *kd, Chain *ch, int *px, int *py, int *pb); @@ -153,7 +152,7 @@ } id = f->dev->usb->ep[eid]->iface->id; r = Rh2d | Rstd | Rdev; - nr =usbcmd(f->dev, r, Rsetconf, 1, id, nil, 0); + nr = usbcmd(f->dev, r, Rsetconf, 1, 0, nil, 0); if(nr < 0) return -1; r = Rh2d | Rclass | Riface; @@ -162,9 +161,9 @@ return -1; r = Rd2h | Rstd | Riface; nr=usbcmd(f->dev, r, Rgetdesc, Dreport<<8, id, desc, descsz); - if(nr < 0) + if(nr <= 0) return -1; - if(kbdebug && nr > 0){ + if(f->debug){ fprint(2, "report descriptor:"); for(i = 0; i < nr; i++){ if(i%8 == 0) @@ -290,7 +289,7 @@ return -1; parsereport(&kd->templ, ch); - if(kbdebug) + if(kd->debug > 1) dumpreport(&kd->templ); if(c < 3) return -1; @@ -378,7 +377,7 @@ x = scale(f, x); y = scale(f, y); } - if(kbdebug > 1) + if(f->debug > 1) fprint(2, "kb: m%11d %11d %11d\n", x, y, b); seprint(mbuf, mbuf+sizeof(mbuf), "m%11d %11d %11d", x, y,b); if(write(mfd, mbuf, strlen(mbuf)) < 0) @@ -409,19 +408,21 @@ } static void -putscan(int kbinfd, uchar esc, uchar sc) +putscan(KDev *f, uchar esc, uchar sc) { + int kbinfd; uchar s[2] = {SCesc1, 0}; + kbinfd = f->in->fd; if(sc == 0x41){ - kbdebug += 2; + f->debug += 2; return; } if(sc == 0x42){ - kbdebug = 0; + f->debug = 0; return; } - if(kbdebug) + if(f->debug > 1) fprint(2, "sc: %x %x\n", (esc? SCesc1: 0), sc); s[1] = sc; if(esc && sc != 0) @@ -435,7 +436,6 @@ { KDev *f; Channel *repeatc; - int kbdinfd; ulong l, t, i; uchar esc1, sc; @@ -446,7 +446,6 @@ */ f = a; repeatc = f->repeatc; - kbdinfd = f->in->fd; l = Awakemsg; Repeat: if(l == Diemsg) @@ -464,7 +463,7 @@ goto Repeat; sleep(5); } - putscan(kbdinfd, esc1, sc); + putscan(f, esc1, sc); t = 30; } Abort: @@ -477,13 +476,13 @@ #define hasesc1(sc) (((sc) > 0x47) || ((sc) == 0x38)) static void -putmod(int fd, uchar mods, uchar omods, uchar mask, uchar esc, uchar sc) +putmod(KDev *f, uchar mods, uchar omods, uchar mask, uchar esc, uchar sc) { /* BUG: Should be a single write */ if((mods&mask) && !(omods&mask)) - putscan(fd, esc, sc); + putscan(f, esc, sc); if(!(mods&mask) && (omods&mask)) - putscan(fd, esc, Keyup|sc); + putscan(f, esc, Keyup|sc); } /* @@ -499,14 +498,12 @@ { int i, j; uchar uk; - int fd; - fd = f->in->fd; - putmod(fd, buf[0], obuf[0], Mctrl, 0, SCctrl); - putmod(fd, buf[0], obuf[0], (1< 2 || kbdebug > 1){ + if(usbdebug > 2 || f->debug > 1){ fprint(2, "kbd mod %x: ", buf[0]); for(i = 2; i < c; i++) fprint(2, "kc %x ", buf[i]); @@ -669,7 +666,7 @@ fprint(2, "kb: %s: bootproto: %r\n", d->dir); return; } - }else if(kbdebug) + }else if(kd->debug) dumpreport(&kd->templ); if(opendevdata(kd->ep, OREAD) < 0){ fprint(2, "kb: %s: opendevdata: %r\n", kd->ep->dir); @@ -692,7 +689,7 @@ int kbmain(Dev *d, int argc, char* argv[]) { - int bootp, i, kena, pena, accel, devid; + int bootp, i, kena, pena, accel, devid, debug; Ep *ep; KDev *kd; Usbdev *ud; @@ -700,13 +697,14 @@ kena = pena = 1; bootp = 0; accel = 0; + debug = 0; devid = d->id; ARGBEGIN{ case 'a': accel = strtol(EARGF(usage()), nil, 0); break; case 'd': - kbdebug++; + debug++; break; case 'k': kena = 1; @@ -747,6 +745,7 @@ kd = d->aux = emallocz(sizeof(KDev), 1); kd->accel = 0; kd->bootp = 1; + kd->debug = debug; kbstart(d, ep, &kbdin, kbdwork, kd); } if(pena && ep->type == Eintr && ep->dir == Ein && @@ -754,6 +753,7 @@ kd = d->aux = emallocz(sizeof(KDev), 1); kd->accel = accel; kd->bootp = bootp; + kd->debug = debug; kbstart(d, ep, &ptrin, ptrwork, kd); } }