Module Name: src Committed By: skrll Date: Tue Jan 3 12:50:51 UTC 2017
Modified Files: src/sys/dev/usb [nick-nhusb]: usb.c usb.h usb_subr.c usbdivar.h xhci.c Log Message: Improve handling of roothub device and free up a bus address for LS/FS/HS controllers. To generate a diff of this commit: cvs rdiff -u -r1.156.2.16 -r1.156.2.17 src/sys/dev/usb/usb.c cvs rdiff -u -r1.111.2.11 -r1.111.2.12 src/sys/dev/usb/usb.h cvs rdiff -u -r1.198.2.38 -r1.198.2.39 src/sys/dev/usb/usb_subr.c cvs rdiff -u -r1.109.2.27 -r1.109.2.28 src/sys/dev/usb/usbdivar.h cvs rdiff -u -r1.28.2.83 -r1.28.2.84 src/sys/dev/usb/xhci.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/dev/usb/usb.c diff -u src/sys/dev/usb/usb.c:1.156.2.16 src/sys/dev/usb/usb.c:1.156.2.17 --- src/sys/dev/usb/usb.c:1.156.2.16 Sun Jan 1 14:56:25 2017 +++ src/sys/dev/usb/usb.c Tue Jan 3 12:50:50 2017 @@ -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 @@ usbioctl(dev_t devt, u_long cmd, void *d 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 @@ usbioctl(dev_t devt, u_long cmd, void *d 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 @@ usbioctl(dev_t devt, u_long cmd, void *d 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 @@ usbioctl(dev_t devt, u_long cmd, void *d 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; } Index: src/sys/dev/usb/usb.h diff -u src/sys/dev/usb/usb.h:1.111.2.11 src/sys/dev/usb/usb.h:1.111.2.12 --- src/sys/dev/usb/usb.h:1.111.2.11 Mon Jan 2 16:55:50 2017 +++ src/sys/dev/usb/usb.h Tue Jan 3 12:50:50 2017 @@ -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 Index: src/sys/dev/usb/usb_subr.c diff -u src/sys/dev/usb/usb_subr.c:1.198.2.38 src/sys/dev/usb/usb_subr.c:1.198.2.39 --- src/sys/dev/usb/usb_subr.c:1.198.2.38 Thu Dec 29 08:40:27 2016 +++ src/sys/dev/usb/usb_subr.c Tue Jan 3 12:50:50 2017 @@ -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 @@ usbd_getnewaddr(struct usbd_bus *bus) { 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 @@ usbd_new_device(device_t parent, struct /* 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 @@ usbd_remove_device(struct usbd_device *d 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 @@ usb_disconnect_port(struct usbd_port *up } 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); Index: src/sys/dev/usb/usbdivar.h diff -u src/sys/dev/usb/usbdivar.h:1.109.2.27 src/sys/dev/usb/usbdivar.h:1.109.2.28 --- src/sys/dev/usb/usbdivar.h:1.109.2.27 Sat Apr 30 10:34:14 2016 +++ src/sys/dev/usb/usbdivar.h Tue Jan 3 12:50:50 2017 @@ -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 @@ struct usbd_hub { }; /*****/ +/* 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_bus { 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 @@ usbd_xfer_isread(struct usbd_xfer *xfer) 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. */ Index: src/sys/dev/usb/xhci.c diff -u src/sys/dev/usb/xhci.c:1.28.2.83 src/sys/dev/usb/xhci.c:1.28.2.84 --- src/sys/dev/usb/xhci.c:1.28.2.83 Mon Jan 2 16:55:50 2017 +++ src/sys/dev/usb/xhci.c Tue Jan 3 12:50:50 2017 @@ -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 @@ xhci_new_device(device_t parent, struct 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 @@ xhci_new_device(device_t parent, struct 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) {