keep descriptor sizes to a minimum disambiguate error messages use pread not seek+read eliminate unused and misleading setting of bMaxPacketSize0 before a call to read on top of that data. Reference: /n/atom/patch/applied/usbdsmalldesc Date: Tue Apr 1 04:17:13 CES 2014 Signed-off-by: quanstro@quanstro.net --- /sys/src/cmd/usb/usbd/usbd.c Tue Apr 1 04:15:47 2014 +++ /sys/src/cmd/usb/usbd/usbd.c Tue Apr 1 04:15:49 2014 @@ -53,14 +53,14 @@ confighub(Hub *h) { int type; - uchar buf[128]; /* room for extra descriptors */ + uchar buf[64]; /* room for extra descriptors */ int i; Usbdev *d; DHub *dd; Port *pp; int nr; int nmap; - uchar *PortPwrCtrlMask; + uchar *pwrctlmask; int offset; int mask; @@ -93,15 +93,15 @@ h->pwrms = Powerdelay; h->maxcurrent = dd->bHubContrCurrent; h->pwrmode = dd->wHubCharacteristics[0] & 3; - h->compound = (dd->wHubCharacteristics[0] & (1<<2))!=0; + h->compound = (dd->wHubCharacteristics[0] & (1<<2)) != 0; h->leds = (dd->wHubCharacteristics[0] & (1<<7)) != 0; - PortPwrCtrlMask = dd->DeviceRemovable + nmap; + pwrctlmask = dd->DeviceRemovable + nmap; for(i = 1; i <= h->nport; i++){ pp = &h->port[i]; offset = i/8; mask = 1<<(i%8); pp->removable = (dd->DeviceRemovable[offset] & mask) != 0; - pp->pwrctl = (PortPwrCtrlMask[offset] & mask) != 0; + pp->pwrctl = (pwrctlmask[offset] & mask) != 0; } return 0; } @@ -117,8 +117,7 @@ d = h->dev; h->nport = 2; h->maxpkt = 8; - seek(d->cfd, 0, 0); - nr = read(d->cfd, buf, sizeof(buf)-1); + nr = pread(d->cfd, buf, sizeof(buf)-1, 0); if(nr < 0) goto Done; buf[nr] = 0; @@ -296,16 +295,12 @@ } static int -getmaxpkt(Dev *d, int islow) +getmaxpkt(Dev *d) { - uchar buf[64]; /* More room to try to get device-specific descriptors */ + uchar buf[Ddevlen]; DDev *dd; dd = (DDev*)buf; - if(islow) - dd->bMaxPacketSize0 = 8; - else - dd->bMaxPacketSize0 = 64; if(usbcmd(d, Rd2h|Rstd|Rdev, Rgetdesc, Ddev<<8|0, 0, buf, sizeof(buf)) < 0) return -1; return dd->bMaxPacketSize0; @@ -361,8 +356,7 @@ fprint(2, "%s: %s: port %d: newdev: %r\n", argv0, d->dir, p); goto Fail; } - seek(d->cfd, 0, 0); - nr = read(d->cfd, buf, sizeof(buf)-1); + nr = pread(d->cfd, buf, sizeof(buf)-1, 0); if(nr == 0){ fprint(2, "%s: %s: port %d: newdev: eof\n", argv0, d->dir, p); goto Fail; @@ -385,15 +379,15 @@ goto Fail; } if(usbcmd(nd, Rh2d|Rstd|Rdev, Rsetaddress, nd->id, 0, nil, 0) < 0){ - dprint(2, "%s: %s: port %d: setaddress: %r\n", argv0, d->dir, p); + dprint(2, "%s: %s: port %d: attach setaddress: %r\n", argv0, d->dir, p); goto Fail; } if(devctl(nd, "address") < 0){ - dprint(2, "%s: %s: port %d: set address: %r\n", argv0, d->dir, p); + dprint(2, "%s: %s: port %d: attach set address: %r\n", argv0, d->dir, p); goto Fail; } - mp=getmaxpkt(nd, strcmp(sp, "low") == 0); + mp = getmaxpkt(nd); if(mp < 0){ dprint(2, "%s: %s: port %d: getmaxpkt: %r\n", argv0, d->dir, p); goto Fail; @@ -530,11 +524,11 @@ nd = pp->dev; opendevdata(nd, ORDWR); if(usbcmd(nd, Rh2d|Rstd|Rdev, Rsetaddress, nd->id, 0, nil, 0) < 0){ - dprint(2, "%s: %s: port %d: setaddress: %r\n", argv0, d->dir, p); + dprint(2, "%s: %s: rst port %d: setaddress: %r\n", argv0, d->dir, p); goto Fail; } if(devctl(nd, "address") < 0){ - dprint(2, "%s: %s: port %d: set address: %r\n", argv0, d->dir, p); + dprint(2, "%s: %s: rst port %d: set address: %r\n", argv0, d->dir, p); goto Fail; } if(usbcmd(nd, Rh2d|Rstd|Rdev, Rsetconf, 1, 0, nil, 0) < 0){ @@ -873,6 +867,7 @@ args(); fmtinstall('U', Ufmt); + fmtinstall('H', encodefmt); quotefmtinstall(); rfork(RFNOTEG); portc = chancreate(sizeof(char *), 0); --- /sys/src/cmd/usb/usbd/usbd.h Tue Apr 1 04:15:50 2014 +++ /sys/src/cmd/usb/usbd/usbd.h Tue Apr 1 04:15:51 2014 @@ -52,9 +52,7 @@ Pconfiged, /* Delays, timeouts (ms) */ -// Spawndelay = 1000, /* how often may we re-spawn a driver */ Spawndelay = 250, /* how often may we re-spawn a driver */ -// Connectdelay = 1000, /* how much to wait after a connect */ Connectdelay = 500, /* how much to wait after a connect */ Resetdelay = 20, /* how much to wait after a reset */ Enabledelay = 20, /* how much to wait after an enable */