ref: adfb8dff26976e201208f3f8ddc3cdc1d34cd8cb
parent: fcd45e0cdd0591e11b876da4f378c0474c21475f
author: cinap_lenrek <[email protected]>
date: Wed Apr 8 14:07:16 EDT 2015
nusb/kb: always try to recover on error, fix recover for multi-function devices when we get an i/o error, always call hdrecover() which will reset the port and reinitialize the interface of the calling processes endpoint. handle the case when we have multi-function device with multiple reader procs in hdrecover(). the sequence is as follows: 1) any of the reader procs encounters i/o error and calls hdrecover(), acquires qlock and initiates port reset. 2) any other readerprocs will now encounter i/o error (due to reset) and also call hdrecover() but will be waiting on the qlock for reset to complete. 3) first process completes reset and reinitializes its interface with setproto() and then releases the qlock for the other readers todo the same.
--- a/sys/src/cmd/nusb/kb/kb.c
+++ b/sys/src/cmd/nusb/kb/kb.c
@@ -346,28 +346,7 @@
return usbcmd(f->dev, Rh2d|Rclass|Riface, Setreport, Reportout, 0, &leds, 1);
}
-/*
- * Try to recover from a babble error. A port reset is the only way out.
- * BUG: we should be careful not to reset a bundle with several devices.
- */
static void
-hdrecover(Hiddev *f)
-{
- int i;
-
- close(f->dev->dfd); /* it's for usbd now */
- devctl(f->dev, "reset");
- for(i = 0; i < 10; i++){
- sleep(500);
- if(opendevdata(f->dev, ORDWR) >= 0){
- setproto(f, f->ep->id);
- break;
- }
- /* else usbd still working... */
- }
-}
-
-static void
hdfree(Hiddev *f)
{
if(f->kinfd >= 0)
@@ -395,6 +374,35 @@
}
static void
+hdrecover(Hiddev *f)
+{
+ char err[ERRMAX];
+ static QLock l;
+ int i;
+
+ if(canqlock(&l)){
+ close(f->dev->dfd);
+ devctl(f->dev, "reset");
+ for(i=0; i<4; i++){
+ sleep(500);
+ if(opendevdata(f->dev, ORDWR) >= 0)
+ goto Resetdone;
+ }
+ threadexitsall(err);
+ } else {
+ /* wait for reset to complete */
+ qlock(&l);
+ }
+Resetdone:
+ if(setproto(f, f->ep->id) < 0){
+ rerrstr(err, sizeof(err));
+ qunlock(&l);
+ hdfatal(f, err);
+ }
+ qunlock(&l);
+}
+
+static void
putscan(Hiddev *f, uchar sc, uchar up)
{
uchar s[2] = {SCesc1, 0};
@@ -614,10 +622,9 @@
rerrstr(err, sizeof(err));
else
strcpy(err, "zero read");
- if(++nerrs < 3){
- fprint(2, "%s: hid: %s: read: %s\n", argv0, f->ep->dir, err);
- if(strstr(err, "babble") != 0)
- hdrecover(f);
+ fprint(2, "%s: hid: %s: read: %s\n", argv0, f->ep->dir, err);
+ if(++nerrs <= 3){
+ hdrecover(f);
continue;
}
hdfatal(f, err);