code: plan9front

Download patch

ref: e0087b2a78d7229d8d2b148b8def688be6639797
parent: 25725eb0adc6902318e63da0eca9123be4d4f93d
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sat Apr 1 18:19:58 EDT 2017

nusb/lib: make usbcmd() return value symmetic; returning size of data phase (if any) (thanks aiju)

usbcmd() with Rh2d used to return the command size (8+ndata) wile returning
only ndata for Rd2h. this changes it to always return ndata for Rh2d. it
mostly doesnt matter as Rh2d callers only check r < 0 for error, but this
makes the interface symmetic.

--- a/sys/src/cmd/nusb/lib/dev.c
+++ b/sys/src/cmd/nusb/lib/dev.c
@@ -396,6 +396,7 @@
 	}else{
 		ndata = count;
 		wp = emallocz(8+ndata, 0);
+		memmove(wp+8, data, ndata);
 	}
 	wp[0] = type;
 	wp[1] = req;
@@ -402,8 +403,6 @@
 	PUT2(wp+2, value);
 	PUT2(wp+4, index);
 	PUT2(wp+6, count);
-	if(data != nil)
-		memmove(wp+8, data, ndata);
 	if(usbdebug>2){
 		hd = hexstr(wp, ndata+8);
 		rs = reqstr(type, req);
@@ -421,7 +420,7 @@
 		dprint(2, "%s: cmd: short write: %d\n", argv0, n);
 		return -1;
 	}
-	return n;
+	return ndata;
 }
 
 static int
@@ -430,7 +429,7 @@
 	char *hd;
 
 	nb = read(d->dfd, buf, nb);
-	if(nb >0 && usbdebug > 2){
+	if(nb > 0 && usbdebug > 2){
 		hd = hexstr(buf, nb);
 		fprint(2, "%s: in[%d] %s\n", d->dir, nb, hd);
 		free(hd);
@@ -455,7 +454,7 @@
 			r = cmdreq(d, type, req, value, index, nil, count);
 		else
 			r = cmdreq(d, type, req, value, index, data, count);
-		if(r > 0){
+		if(r >= 0){
 			if((type & Rd2h) == 0)
 				break;
 			r = cmdrep(d, data, count);
@@ -469,7 +468,7 @@
 			rerrstr(err, sizeof(err));
 		sleep(Ucdelay);
 	}
-	if(r > 0 && i >= 2)
+	if(r >= 0 && i >= 2)
 		/* let the user know the device is not in good shape */
 		fprint(2, "%s: usbcmd: %s: required %d attempts (%s)\n",
 			argv0, d->dir, i, err);
--- a/sys/src/cmd/nusb/serial/ftdi.c
+++ b/sys/src/cmd/nusb/serial/ftdi.c
@@ -858,7 +858,7 @@
 		index |= p->interfc + 1;
 	dsprint(2, "serial: ftdiread %#p [%d] req: %#x val: %#x idx:%d buf:%p len:%d\n",
 		p, p->interfc, req, 0, index, buf, len);
-	res = usbcmd(ser->dev,  Rd2h | Rftdireq | Rdev, req, 0, index, buf, len);
+	res = usbcmd(ser->dev, Rd2h | Rftdireq | Rdev, req, 0, index, buf, len);
 	dsprint(2, "serial: ftdiread res:%d\n", res);
 	return res;
 }