From 890f126abc5c82c3a86bc20e84f54dbb2ee8208e Mon Sep 17 00:00:00 2001 From: Giacomo Tesio Date: Tue, 3 Jan 2017 00:42:37 +0100 Subject: [PATCH] kernel: fix usb after changing OREAD/OWRITE values OREAD and OWRITE are used as array indexes assuming that OREAD was zero and OWRITE was one. Thus each related allocation reserved just 2 slot and even Ep struct in usb.h reserved just 2 int for toggles. Since OREAD is now 1 and OWRITE is 2 we have to allocate/reserve 3 slot as long as we use them as array indexes (which we could change in the future). Unfortunately this means we waste the index zero in those arrays that will always be unused. This also means that, to loop in such arrays we must begin with OREAD as index zero is always empty. PRO-MEMORIA: if/when we introduce the walk() syscall, OSTAT might turn useless. In that case we might remove it and thus consider to move back OREAD/OWRITE to 0/1 respectively (which might or might not be a good idea, to be analyzed). --- sys/src/kern/amd64/usbohci.c | 2 +- sys/src/kern/amd64/usbuhci.c | 2 +- sys/src/kern/port/devusb.c | 8 ++++---- sys/src/kern/port/usb.h | 5 +++-- sys/src/kern/port/usbehci.c | 2 +- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/sys/src/kern/amd64/usbohci.c b/sys/src/kern/amd64/usbohci.c index 0d08b6d..ed87e80 100644 --- a/sys/src/kern/amd64/usbohci.c +++ b/sys/src/kern/amd64/usbohci.c @@ -2059,7 +2059,7 @@ epopen(Ep *ep) ep->pollival = 1; /* assume this; doesn't really matter */ /* and fall... */ case Tintr: - io = ep->aux = smalloc(sizeof(Qio)*2); + io = ep->aux = smalloc(sizeof(Qio)*3); io[OREAD].debug = io[OWRITE].debug = ep->debug; usbid = (ep->nb&Epmax)<<7 | (ep->dev->nb&Devmax); if(ep->mode != OREAD){ diff --git a/sys/src/kern/amd64/usbuhci.c b/sys/src/kern/amd64/usbuhci.c index b8c9ed2..2e9f3a4 100644 --- a/sys/src/kern/amd64/usbuhci.c +++ b/sys/src/kern/amd64/usbuhci.c @@ -1785,7 +1785,7 @@ epopen(Ep *ep) break; case Tbulk: case Tintr: - io = ep->aux = smalloc(sizeof(Qio)*2); + io = ep->aux = smalloc(sizeof(Qio)*3); // allocate 3 Qio because OREAD == 1 and OWRITE == 2; index 0 is wasted; TODO: fix this io[OREAD].debug = io[OWRITE].debug = ep->debug; usbid = ((ep->nb&Epmax)<<7)|(ep->dev->nb&Devmax); if(ep->mode != OREAD){ diff --git a/sys/src/kern/port/devusb.c b/sys/src/kern/port/devusb.c index feff38d..87c82cd 100644 --- a/sys/src/kern/port/devusb.c +++ b/sys/src/kern/port/devusb.c @@ -144,9 +144,9 @@ static Dirtab usbdir[] = char *usbmodename[] = { - [OREAD] "r", + [OREAD] "r", [OWRITE] "w", - [ORDWR] "rw", + [ORDWR] "rw", }; static char *ttname[] = @@ -223,7 +223,7 @@ name2mode(char *mode) { int i; - for(i = 0; i < nelem(usbmodename); i++) + for(i = OREAD; i < nelem(usbmodename); i++) if(strcmp(mode, usbmodename[i]) == 0) return i; return -1; @@ -821,7 +821,7 @@ usbopen(Chan *c, unsigned long omode) { int q; Ep *ep; - int mode; + unsigned long mode; mode = openmode(omode); q = QID(c->qid); diff --git a/sys/src/kern/port/usb.h b/sys/src/kern/port/usb.h index b7a3395..2800fe0 100644 --- a/sys/src/kern/port/usb.h +++ b/sys/src/kern/port/usb.h @@ -163,8 +163,9 @@ struct Ep uint32_t load; /* in microseconds, for a fransfer of maxpkt bytes */ void* aux; /* for controller specific info */ int rhrepl; /* fake root hub replies */ - int toggle[2]; /* saved toggles (while ep is not in use) */ - int32_t pollival; /* poll interval ([µ]frames; intr/iso) */ + int toggle[3]; /* saved toggles (while ep is not in use) + NOTE: 3 because OREAD == 1 and OWRITE == 2; index 0 is wasted; TODO fix this */ + int32_t pollival; /* poll interval ([µ]frames; intr/iso) */ int32_t hz; /* poll frequency (iso) */ int32_t samplesz; /* sample size (iso) */ int ntds; /* nb. of Tds per µframe */ diff --git a/sys/src/kern/port/usbehci.c b/sys/src/kern/port/usbehci.c index f602cc0..b5f217a 100644 --- a/sys/src/kern/port/usbehci.c +++ b/sys/src/kern/port/usbehci.c @@ -2923,7 +2923,7 @@ epopen(Ep *ep) ep->pollival = 1; /* assume this; doesn't really matter */ /* and fall... */ case Tintr: - io = ep->aux = smalloc(sizeof(Qio)*2); + io = ep->aux = smalloc(sizeof(Qio)*3); io[OREAD].debug = io[OWRITE].debug = ep->debug; usbid = (ep->nb&Epmax) << 7 | ep->dev->nb &Devmax; assert(ep->pollival != 0);