Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/nick-nhusb]: src/sys/dev/usb Improve handling of roothub device and free...



details:   https://anonhg.NetBSD.org/src/rev/3f2c897329ba
branches:  nick-nhusb
changeset: 804749:3f2c897329ba
user:      skrll <skrll%NetBSD.org@localhost>
date:      Tue Jan 03 12:50:50 2017 +0000

description:
Improve handling of roothub device and free up a bus address for LS/FS/HS
controllers.

diffstat:

 sys/dev/usb/usb.c      |  20 +++++++++++++-------
 sys/dev/usb/usb.h      |   8 ++++----
 sys/dev/usb/usb_subr.c |  16 +++++++++-------
 sys/dev/usb/usbdivar.h |  14 ++++++++++++--
 sys/dev/usb/xhci.c     |  15 +++++++--------
 5 files changed, 45 insertions(+), 28 deletions(-)

diffs (231 lines):

diff -r 428a3cac62a6 -r 3f2c897329ba sys/dev/usb/usb.c
--- a/sys/dev/usb/usb.c Mon Jan 02 16:55:50 2017 +0000
+++ b/sys/dev/usb/usb.c Tue Jan 03 12:50:50 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: usb.c,v 1.156.2.16 2017/01/01 14:56:25 skrll Exp $     */
+/*     $NetBSD: usb.c,v 1.156.2.17 2017/01/03 12:50:50 skrll Exp $     */
 
 /*
  * Copyright (c) 1998, 2002, 2008, 2012 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.156.2.16 2017/01/01 14:56:25 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.156.2.17 2017/01/03 12:50:50 skrll Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -727,8 +727,12 @@
                        error = EINVAL;
                        goto fail;
                }
-               if (addr < 0 || addr >= USB_MAX_DEVICES ||
-                   sc->sc_bus->ub_devices[addr] == NULL) {
+               if (addr < 0 || addr >= USB_MAX_DEVICES) {
+                       error = EINVAL;
+                       goto fail;
+               }
+               size_t dindex = usb_addr2dindex(addr);
+               if (sc->sc_bus->ub_devices[dindex] == NULL) {
                        error = EINVAL;
                        goto fail;
                }
@@ -750,7 +754,7 @@
                                        goto ret;
                        }
                }
-               err = usbd_do_request_flags(sc->sc_bus->ub_devices[addr],
+               err = usbd_do_request_flags(sc->sc_bus->ub_devices[dindex],
                          &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
                          USBD_DEFAULT_TIMEOUT);
                if (err) {
@@ -783,7 +787,8 @@
                        error = EINVAL;
                        goto fail;
                }
-               if ((dev = sc->sc_bus->ub_devices[addr]) == NULL) {
+               size_t dindex = usb_addr2dindex(addr);
+               if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
                        error = ENXIO;
                        goto fail;
                }
@@ -802,7 +807,8 @@
                        error = EINVAL;
                        goto fail;
                }
-               if ((dev = sc->sc_bus->ub_devices[addr]) == NULL) {
+               size_t dindex = usb_addr2dindex(addr);
+               if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
                        error = ENXIO;
                        goto fail;
                }
diff -r 428a3cac62a6 -r 3f2c897329ba sys/dev/usb/usb.h
--- a/sys/dev/usb/usb.h Mon Jan 02 16:55:50 2017 +0000
+++ b/sys/dev/usb/usb.h Tue Jan 03 12:50:50 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: usb.h,v 1.111.2.11 2017/01/02 16:55:50 skrll Exp $     */
+/*     $NetBSD: usb.h,v 1.111.2.12 2017/01/03 12:50:50 skrll Exp $     */
 
 /*
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -53,9 +53,9 @@
 
 #define USB_STACK_VERSION 2
 
-#define USB_MAX_DEVICES        (128 + 1)       /* 0, root, and 1->127 */
-#define USB_MIN_DEVICES        2               /* unused + root HUB */
-#define USB_START_ADDR 0
+#define USB_MAX_DEVICES                128             /* 0, 1-127 */
+#define USB_MIN_DEVICES                2               /* unused + root HUB */
+#define USB_START_ADDR         0
 
 #define USB_CONTROL_ENDPOINT 0
 #define USB_MAX_ENDPOINTS 16
diff -r 428a3cac62a6 -r 3f2c897329ba sys/dev/usb/usb_subr.c
--- a/sys/dev/usb/usb_subr.c    Mon Jan 02 16:55:50 2017 +0000
+++ b/sys/dev/usb/usb_subr.c    Tue Jan 03 12:50:50 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: usb_subr.c,v 1.198.2.38 2016/12/29 08:40:27 skrll Exp $        */
+/*     $NetBSD: usb_subr.c,v 1.198.2.39 2017/01/03 12:50:50 skrll Exp $        */
 /*     $FreeBSD: src/sys/dev/usb/usb_subr.c,v 1.18 1999/11/17 22:33:47 n_hibma Exp $   */
 
 /*
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.198.2.38 2016/12/29 08:40:27 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: usb_subr.c,v 1.198.2.39 2017/01/03 12:50:50 skrll Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -832,9 +832,11 @@
 {
        int addr;
 
-       for (addr = 1; addr < USB_MAX_DEVICES; addr++)
-               if (bus->ub_devices[addr] == NULL)
+       for (addr = 1; addr < USB_MAX_DEVICES; addr++) {
+               size_t dindex = usb_addr2dindex(addr);
+               if (bus->ub_devices[dindex] == NULL)
                        return addr;
+       }
        return -1;
 }
 
@@ -1336,7 +1338,7 @@
        /* Allow device time to set new address */
        usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
        dev->ud_addr = addr;    /* new device address now */
-       bus->ub_devices[addr] = dev;
+       bus->ub_devices[usb_addr2dindex(addr)] = dev;
 
        /* Re-establish the default pipe with the new address. */
        usbd_kill_pipe(dev->ud_pipe0);
@@ -1428,7 +1430,7 @@
        if (dev->ud_pipe0 != NULL)
                usbd_kill_pipe(dev->ud_pipe0);
        up->up_dev = NULL;
-       dev->ud_bus->ub_devices[dev->ud_addr] = NULL;
+       dev->ud_bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = NULL;
 
        kmem_free(dev, sizeof(*dev));
 }
@@ -1737,7 +1739,7 @@
        }
 
        mutex_enter(dev->ud_bus->ub_lock);
-       dev->ud_bus->ub_devices[dev->ud_addr] = NULL;
+       dev->ud_bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = NULL;
        up->up_dev = NULL;
        mutex_exit(dev->ud_bus->ub_lock);
 
diff -r 428a3cac62a6 -r 3f2c897329ba sys/dev/usb/usbdivar.h
--- a/sys/dev/usb/usbdivar.h    Mon Jan 02 16:55:50 2017 +0000
+++ b/sys/dev/usb/usbdivar.h    Tue Jan 03 12:50:50 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: usbdivar.h,v 1.109.2.27 2016/04/30 10:34:14 skrll Exp $        */
+/*     $NetBSD: usbdivar.h,v 1.109.2.28 2017/01/03 12:50:50 skrll Exp $        */
 
 /*
  * Copyright (c) 1998, 2012 The NetBSD Foundation, Inc.
@@ -139,6 +139,9 @@
 };
 
 /*****/
+/* 0, root, and 1->127 */
+#define USB_ROOTHUB_INDEX      1
+#define USB_TOTAL_DEVICES      (USB_MAX_DEVICES + 1)
 
 struct usbd_bus {
        /* Filled by HC driver */
@@ -164,7 +167,7 @@
        struct usbd_device     *ub_roothub;
        uint8_t                 ub_rhaddr;      /* roothub address */
        uint8_t                 ub_rhconf;      /* roothub configuration */
-       struct usbd_device     *ub_devices[USB_MAX_DEVICES];
+       struct usbd_device     *ub_devices[USB_TOTAL_DEVICES];
        kcondvar_t              ub_needsexplore_cv;
        char                    ub_needsexplore;/* a hub a signalled a change */
        char                    ub_usepolling;
@@ -353,6 +356,13 @@
           UE_DIR_IN;
 }
 
+static inline size_t
+usb_addr2dindex(int addr)
+{
+
+       return USB_ROOTHUB_INDEX + addr;
+}
+
 /*
  * These macros reflect the current locking scheme.  They might change.
  */
diff -r 428a3cac62a6 -r 3f2c897329ba sys/dev/usb/xhci.c
--- a/sys/dev/usb/xhci.c        Mon Jan 02 16:55:50 2017 +0000
+++ b/sys/dev/usb/xhci.c        Tue Jan 03 12:50:50 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: xhci.c,v 1.28.2.83 2017/01/02 16:55:50 skrll Exp $     */
+/*     $NetBSD: xhci.c,v 1.28.2.84 2017/01/03 12:50:50 skrll Exp $     */
 
 /*
  * Copyright (c) 2013 Jonathan A. Kollasch
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.28.2.83 2017/01/02 16:55:50 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.28.2.84 2017/01/03 12:50:50 skrll Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -2292,9 +2292,8 @@
        dd = &dev->ud_ddesc;
 
        if (depth == 0 && port == 0) {
-#define XHCI_ROOTHUB_INDEX 128
-               KASSERT(bus->ub_devices[XHCI_ROOTHUB_INDEX] == NULL);
-               bus->ub_devices[XHCI_ROOTHUB_INDEX] = dev;
+               KASSERT(bus->ub_devices[USB_ROOTHUB_INDEX] == NULL);
+               bus->ub_devices[USB_ROOTHUB_INDEX] = dev;
                err = usbd_get_initial_ddesc(dev, dd);
                if (err) {
                        DPRINTFN(1, "get_initial_ddesc %u", err, 0, 0, 0);
@@ -2356,12 +2355,12 @@
                KASSERTMSG(addr >= 1 && addr <= 127, "addr %d", addr);
                dev->ud_addr = addr;
 
-               KASSERTMSG(bus->ub_devices[dev->ud_addr] == NULL,
+               KASSERTMSG(bus->ub_devices[usb_addr2dindex(dev->ud_addr)] == NULL,
                    "addr %d already allocated", dev->ud_addr);
                /*
-                * The root hub is given a slot
+                * The root hub is given its own slot
                 */
-               bus->ub_devices[dev->ud_addr] = dev;
+               bus->ub_devices[usb_addr2dindex(dev->ud_addr)] = dev;
 
                err = usbd_get_initial_ddesc(dev, dd);
                if (err) {



Home | Main Index | Thread Index | Old Index