]> git.lizzy.rs Git - plan9front.git/commitdiff
nusb/lib: make usbcmd() return value symmetic; returning size of data phase (if any...
authorcinap_lenrek <cinap_lenrek@felloff.net>
Sat, 1 Apr 2017 20:19:58 +0000 (22:19 +0200)
committercinap_lenrek <cinap_lenrek@felloff.net>
Sat, 1 Apr 2017 20:19:58 +0000 (22:19 +0200)
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.

sys/src/cmd/nusb/lib/dev.c
sys/src/cmd/nusb/serial/ftdi.c

index c556bcfa6fae17d7ae18e2e0be46ce3050447e10..cd9d15a3856ec6d5cb78d2d89e89e595a6f96f5c 100644 (file)
@@ -396,14 +396,13 @@ cmdreq(Dev *d, int type, int req, int value, int index, uchar *data, int count)
        }else{
                ndata = count;
                wp = emallocz(8+ndata, 0);
+               memmove(wp+8, data, ndata);
        }
        wp[0] = type;
        wp[1] = req;
        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 @@ cmdreq(Dev *d, int type, int req, int value, int index, uchar *data, int count)
                dprint(2, "%s: cmd: short write: %d\n", argv0, n);
                return -1;
        }
-       return n;
+       return ndata;
 }
 
 static int
@@ -430,7 +429,7 @@ cmdrep(Dev *d, void *buf, int nb)
        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 @@ usbcmd(Dev *d, int type, int req, int value, int index, uchar *data, int count)
                        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 @@ usbcmd(Dev *d, int type, int req, int value, int index, uchar *data, int count)
                        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);
index 3affcf39f6def63e78cfae82896c3b968636cbe7..cbe0d035ecf8e0b2f68d6b92676df77c68af64a4 100644 (file)
@@ -858,7 +858,7 @@ ftdiread(Serialport *p, int index, int req, uchar *buf, int len)
                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;
 }