shithub: front

Download patch

ref: d5938c597328adb8abb8785b4ac820ca0f094b6b
parent: 33a0d0599932e4d7190b06a4fc94c0b9ef031d9a
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Wed Jan 15 12:38:34 EST 2025

nusb/lib: remove GET_STATUS special case in usbcmd(), cleanup

With the latest change to usbd, the altered retry-count
for GET_STATUS commands is not needed anymore.

Also, improve the code avoiding unneccessary sleep in
the the last retry iteration and print request type.

--- a/sys/src/cmd/nusb/lib/dev.c
+++ b/sys/src/cmd/nusb/lib/dev.c
@@ -426,16 +426,15 @@
 int
 usbcmd(Dev *d, int type, int req, int value, int index, uchar *data, int count)
 {
-	int i, r, nerr;
-	char err[64];
+	char err[ERRMAX];
+	int r, tries;
 
 	/*
 	 * Some devices do not respond to commands some times.
 	 * Others even report errors but later work just fine. Retry.
 	 */
-	r = -1;
 	*err = 0;
-	for(i = nerr = 0; i < Uctries; i++){
+	for(tries = 1;; tries++){
 		if(type & Rd2h)
 			r = cmdreq(d, type, req, value, index, nil, count);
 		else
@@ -449,22 +448,16 @@
 			if(r == 0)
 				werrstr("no data from device");
 		}
-
-		/* don't retry GET_STATUS requests */
-		if(type == (Rd2h|Rclass|Rother)
-		&& req == Rgetstatus
-		&& value == 0)
-			break;
-
-		nerr++;
 		if(*err == 0)
 			rerrstr(err, sizeof(err));
+		if(tries >= Uctries)
+			break;
 		sleep(Ucdelay);
 	}
-	if(r >= 0 && i >= 2)
+	if(r >= 0 && tries > 1)
 		/* 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);
+		fprint(2, "%s: usbcmd: %s: %s required %d attempts (%s)\n",
+			argv0, d->dir, reqstr(type, req), tries, err);
 	return r;
 }
 
--