shithub: riscv

Download patch

ref: 56ac6ea29e1b9dfdad8c7b1875f81c242362da2a
parent: b1d4e860642d13805e47ee5cf9f373d8c97d0bc4
author: cinap_lenrek <[email protected]>
date: Mon Jul 31 11:29:08 EDT 2017

nusb/*: cleanup

--- a/sys/src/cmd/nusb/disk/disk.c
+++ b/sys/src/cmd/nusb/disk/disk.c
@@ -1002,20 +1002,6 @@
 	exits("usage");
 }
 
-static void
-umsdevfree(void *a)
-{
-	Ums *ums = a;
-
-	if(ums == nil)
-		return;
-	closedev(ums->epin);
-	closedev(ums->epout);
-	ums->epin = ums->epout = nil;
-	free(ums->lun);
-	free(ums);
-}
-
 /*
  * some devices like usb modems appear as mass storage
  * for windows driver installation. switch mode here
@@ -1068,7 +1054,6 @@
 	notreallyums(dev);
 	ums = dev->aux = emallocz(sizeof(Ums), 1);
 	ums->maxlun = -1;
-	dev->free = umsdevfree;
 	if(findendpoints(ums) < 0)
 		sysfatal("endpoints not found");
 
--- a/sys/src/cmd/nusb/lib/dev.c
+++ b/sys/src/cmd/nusb/lib/dev.c
@@ -310,8 +310,6 @@
 	if(d==nil || decref(d) != 0)
 		return;
 	dprint(2, "%s: closedev %#p %s\n", argv0, d, d->dir);
-	if(d->free != nil)
-		d->free(d->aux);
 	if(d->cfd >= 0)
 		close(d->cfd);
 	if(d->dfd >= 0)
--- a/sys/src/cmd/nusb/lib/usb.h
+++ b/sys/src/cmd/nusb/lib/usb.h
@@ -174,11 +174,10 @@
 	int	dfd;		/* descriptor for the data file */
 	int	cfd;		/* descriptor for the control file */
 	int	isusb3;		/* this is a usb3 device */
+	int	depth;		/* hub depth for usb3 hubs */
 	int	maxpkt;		/* cached from usb description */
-	Ref	nerrs;		/* number of errors in requests */
 	Usbdev*	usb;		/* USB description */
 	void*	aux;		/* for the device driver */
-	void	(*free)(void*);	/* idem. to release aux */
 	char*	hname;		/* hash name, unique for device */
 };
 
--- a/sys/src/cmd/nusb/usbd/dat.h
+++ b/sys/src/cmd/nusb/usbd/dat.h
@@ -84,7 +84,6 @@
 	Port	*port;
 	int	failed;		/* I/O error while enumerating */
 	int	isroot;		/* set if root hub */
-	int	depth;		/* hub depth */
 	Dev	*dev;		/* for this hub */
 	Hub	*next;		/* in list of hubs */
 };
--- a/sys/src/cmd/nusb/usbd/fns.h
+++ b/sys/src/cmd/nusb/usbd/fns.h
@@ -1,6 +1,6 @@
-int	attachdev(Hub*, Port*);
-void	detachdev(Hub*, Port*);
+int	attachdev(Port*);
+void	detachdev(Port*);
 void	work(void);
-Hub*	newhub(char *, Dev*, Hub*);
+Hub*	newhub(char *, Dev*);
 void	hname(char *);
 void	checkidle(void);
--- a/sys/src/cmd/nusb/usbd/hub.c
+++ b/sys/src/cmd/nusb/usbd/hub.c
@@ -83,7 +83,7 @@
 		mask = 1<<(i%8);
 		pp->removable = (dd->DeviceRemovable[offset] & mask) != 0;
 	}
-	if(usbcmd(h->dev, Rh2d|Rclass|Rdev, Rsethubdepth, h->depth, 0, nil, 0) < 0){
+	if(usbcmd(h->dev, Rh2d|Rclass|Rdev, Rsethubdepth, h->dev->depth, 0, nil, 0) < 0){
 		fprint(2, "%s: %s: sethubdepth: %r\n", argv0, h->dev->dir);
 		return -1;
 	}
@@ -106,21 +106,26 @@
 		dt = Dhub;
 		dl = Dhublen;
 	}
-	for(i = 0; i < nelem(d->ddesc); i++)
+	for(i = 0; i < nelem(d->ddesc); i++){
 		if(d->ddesc[i] == nil)
 			break;
-		else if(d->ddesc[i]->data.bDescriptorType == dt){
+		if(d->ddesc[i]->data.bDescriptorType == dt){
 			dd = &d->ddesc[i]->data;
-			nr = dl;
+			nr = d->ddesc[i]->data.bLength;
 			goto Config;
 		}
+	}
 	nr = usbcmd(h->dev, Rd2h|Rclass|Rdev, Rgetdesc, dt<<8|0, 0, buf, sizeof buf);
-	if(nr < dl){
+	if(nr < 0){
 		fprint(2, "%s: %s: getdesc hub: %r\n", argv0, h->dev->dir);
 		return -1;
 	}
 	dd = buf;
 Config:
+	if(nr < dl){
+		fprint(2, "%s: %s: hub descriptor too small (%d < %d)\n", argv0, h->dev->dir, nr, dl);
+		return -1;
+	}
 	if(h->dev->isusb3)
 		return configusb3hub(h, dd, nr);
 	else
@@ -160,7 +165,7 @@
 }
 
 Hub*
-newhub(char *fn, Dev *d, Hub *ph)
+newhub(char *fn, Dev *d)
 {
 	Hub *h;
 	int i;
@@ -169,12 +174,12 @@
 	h = emallocz(sizeof(Hub), 1);
 	h->isroot = (d == nil);
 	if(h->isroot){
-		h->depth = -1;
 		h->dev = opendev(fn);
 		if(h->dev == nil){
 			fprint(2, "%s: opendev: %s: %r", argv0, fn);
 			goto Fail;
 		}
+		h->dev->depth = -1;
 		configroothub(h);	/* never fails */
 		if(opendevdata(h->dev, ORDWR) < 0){
 			fprint(2, "%s: opendevdata: %s: %r\n", argv0, fn);
@@ -182,7 +187,6 @@
 			goto Fail;
 		}
 	}else{
-		h->depth = ph->depth+1;
 		h->dev = d;
 		if(confighub(h) < 0){
 			fprint(2, "%s: %s: config: %r\n", argv0, fn);
@@ -416,6 +420,8 @@
 		fprint(2, "%s: %s: port %d: opendev: %r\n", argv0, d->dir, p);
 		goto Fail;
 	}
+	nd->depth = h->dev->depth+1;
+	nd->isusb3 = h->dev->isusb3;
 	if(usbdebug > 2)
 		devctl(nd, "debug 1");
 	if(opendevdata(nd, ORDWR) < 0){
@@ -430,7 +436,6 @@
 		dprint(2, "%s: %s: port %d: set address: %r\n", argv0, d->dir, p);
 		goto Fail;
 	}
-	nd->isusb3 = h->dev->isusb3;
 	mp=getmaxpkt(nd, strcmp(sp, "low") == 0);
 	if(mp < 0){
 		dprint(2, "%s: %s: port %d: getmaxpkt: %r\n", argv0, d->dir, p);
@@ -491,7 +496,7 @@
 		pp->hub = nil;
 	}
 	if(pp->dev != nil){
-		detachdev(h, pp);
+		detachdev(pp);
 
 		devctl(pp->dev, "detach");
 		closedev(pp->dev);
@@ -639,7 +644,7 @@
 	}
 	if((pp->sts & PSpresent) == 0 && (sts & PSpresent) != 0){
 		if(portattach(h, p, sts) != nil)
-			if(attachdev(h, pp) < 0)
+			if(attachdev(pp) < 0)
 				portdetach(h, p);
 	}else if(portgone(pp, sts)){
 		portdetach(h, p);
@@ -680,7 +685,7 @@
 	hubs = nil;
 	while((fn = rendezvous(work, nil)) != nil){
 		dprint(2, "%s: %s starting\n", argv0, fn);
-		h = newhub(fn, nil, nil);
+		h = newhub(fn, nil);
 		if(h == nil)
 			fprint(2, "%s: %s: newhub failed: %r\n", argv0, fn);
 		free(fn);
--- a/sys/src/cmd/nusb/usbd/usbd.c
+++ b/sys/src/cmd/nusb/usbd/usbd.c
@@ -373,7 +373,7 @@
 }
 
 int
-attachdev(Hub *h, Port *p)
+attachdev(Port *p)
 {
 	Dev *d = p->dev;
 	int id;
@@ -385,7 +385,7 @@
 		 * has the config address in use.
 		 * We cancel kernel debug for these eps. too chatty.
 		 */
-		if((p->hub = newhub(d->dir, d, h)) == nil)
+		if((p->hub = newhub(d->dir, d)) == nil)
 			return -1;
 		return 0;
 	}
@@ -417,7 +417,7 @@
 }
 
 void
-detachdev(Hub *, Port *p)
+detachdev(Port *p)
 {
 	Dev *d = p->dev;