Get packet size and poll interval from usb device config instead of guessing. Reference: /n/sources/patch/applied/usbmouse-config Date: Fri Feb 8 12:47:11 CET 2008 Signed-off-by: miller@hamnavoe.com --- /sys/src/cmd/usb/misc/usbmouse.c Fri Feb 8 12:45:46 2008 +++ /sys/src/cmd/usb/misc/usbmouse.c Fri Feb 8 12:45:43 2008 @@ -6,6 +6,13 @@ #include #include #include +#include "usb.h" + +void (*dprinter[])(Device *, int, ulong, void *b, int n) = { + [STRING] pstring, + [DEVICE] pdevice, + [HID] phid, +}; int mousefd, ctlfd, mousein; @@ -16,7 +23,7 @@ char *ctlfmt = "/dev/usb%d/%d/ctl"; char *msefmt = "/dev/usb%d/%d/ep%ddata"; -char *ctlmsgfmt = "ep %d 10 r %d"; +char *ctlmsgfmt = "ep %d %d r %d"; char ctlfile[32]; char msefile[32]; @@ -32,6 +39,35 @@ void work(void *); int +findendpoint(int ctlr, int id, int *epno, int *maxpkt, int *pollms) { + Device *d; + Endpt *ep; + int i; + + d = opendev(ctlr, id); + d->config[0] = emallocz(sizeof(*d->config[0]), 1); + if (describedevice(d) < 0 || loadconfig(d, 0) < 0) { + closedev(d); + return -1; + } + for (i = 1; i < Nendpt; i++) { + if ((ep = d->ep[i]) == nil) + continue; + if (ep->csp == 0x020103 && ep->type == Eintr && ep->dir != Eout) { + if (ep->iface == nil || ep->iface->dalt[0] == nil) + continue; + *epno = i; + *maxpkt = ep->maxpkt; + *pollms = ep->iface->dalt[0]->interval; + closedev(d); + return 0; + } + } + closedev(d); + return -1; +} + +int robusthandler(void*, char *s) { if (debug) fprint(2, "inthandler: %s\n", s); @@ -95,11 +131,15 @@ threadmain(int argc, char *argv[]) { int ctlrno, i, ep = 0; + int maxpkt, pollms; char *p; char buf[256]; Biobuf *f; ARGBEGIN{ + case 'd': + debug=1; + break; case 's': scroll=1; break; @@ -132,11 +172,8 @@ if (strncmp(p, "Enabled ", 8) == 0) continue; if (strstr(p, hbm) != nil) { - while(*p == ' ') - p++; - ep = atoi(p); - if(ep) // ep0data is no use - goto found; + Bterm(f); + goto found; } } Bterm(f); @@ -146,10 +183,11 @@ case 2: ctlrno = atoi(argv[0]); i = atoi(argv[1]); - ep = 1; /* a guess */ - if (verbose) - fprint(2, "assuming endpoint %d\n", ep); found: + if(findendpoint(ctlrno, i, &ep, &maxpkt, &pollms) < 0) { + fprint(2, "%s: invalid usb device configuration\n", argv0); + threadexitsall("no mouse"); + } snprint(ctlfile, sizeof ctlfile, ctlfmt, ctlrno, i); snprint(msefile, sizeof msefile, msefmt, ctlrno, i, ep); break; @@ -158,11 +196,13 @@ } nbuts = (scroll? 5: 3); + if (nbuts > maxpkt) + nbuts = maxpkt; if ((ctlfd = open(ctlfile, OWRITE)) < 0) sysfatal("%s: %r", ctlfile); if (verbose) - fprint(2, "Send ep %d 10 r %d to %s\n", ep, nbuts, ctlfile); - fprint(ctlfd, ctlmsgfmt, ep, nbuts); + fprint(2, "Send ep %d %d r %d to %s\n", ep, pollms, maxpkt, ctlfile); + fprint(ctlfd, ctlmsgfmt, ep, pollms, maxpkt); close(ctlfd); if ((mousefd = open(msefile, OREAD)) < 0)