Home | History | Annotate | Line # | Download | only in usb
uhub.c revision 1.31
      1 /*	$NetBSD: uhub.c,v 1.31 1999/10/12 20:02:47 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (augustss (at) carlstedt.se) at
      9  * Carlstedt Research & Technology.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * USB spec: http://www.usb.org/cgi-usb/mailmerge.cgi/home/usb/docs/developers/cgiform.tpl
     42  */
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/malloc.h>
     48 #if defined(__NetBSD__) || defined(__OpenBSD__)
     49 #include <sys/device.h>
     50 #elif defined(__FreeBSD__)
     51 #include <sys/module.h>
     52 #include <sys/bus.h>
     53 #endif
     54 #include <sys/proc.h>
     55 
     56 #include <machine/bus.h>
     57 
     58 #include <dev/usb/usb.h>
     59 #include <dev/usb/usbdi.h>
     60 #include <dev/usb/usbdi_util.h>
     61 #include <dev/usb/usbdivar.h>
     62 
     63 #ifdef USB_DEBUG
     64 #define DPRINTF(x)	if (usbdebug) logprintf x
     65 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     66 extern int	usbdebug;
     67 #else
     68 #define DPRINTF(x)
     69 #define DPRINTFN(n,x)
     70 #endif
     71 
     72 struct uhub_softc {
     73 	USBBASEDEVICE		sc_dev;		/* base device */
     74 	usbd_device_handle	sc_hub;		/* USB device */
     75 	usbd_pipe_handle	sc_ipipe;	/* interrupt pipe */
     76 	u_int8_t		sc_status[1];	/* XXX more ports */
     77 	u_char			sc_running;
     78 };
     79 
     80 usbd_status uhub_init_port __P((struct usbd_port *));
     81 usbd_status uhub_explore __P((usbd_device_handle hub));
     82 void uhub_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
     83 
     84 USB_DECLARE_DRIVER_NAME(usb, uhub);
     85 
     86 #if defined(__NetBSD__) || defined(__OpenBSD__)
     87 struct cfattach uhub_uhub_ca = {
     88 	sizeof(struct uhub_softc), uhub_match, uhub_attach,
     89 	uhub_detach, uhub_activate
     90 };
     91 #endif
     92 
     93 USB_MATCH(uhub)
     94 {
     95 	USB_MATCH_START(uhub, uaa);
     96 	usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
     97 
     98 	DPRINTFN(5,("uhub_match, dd=%p\n", dd));
     99 	/*
    100 	 * The subclass for hubs seems to be 0 for some and 1 for others,
    101 	 * so we just ignore the subclass.
    102 	 */
    103 	if (uaa->iface == 0 && dd->bDeviceClass == UCLASS_HUB)
    104 		return (UMATCH_DEVCLASS_DEVSUBCLASS);
    105 	return (UMATCH_NONE);
    106 }
    107 
    108 USB_ATTACH(uhub)
    109 {
    110 	USB_ATTACH_START(uhub, sc, uaa);
    111 	usbd_device_handle dev = uaa->device;
    112 	char devinfo[1024];
    113 	usbd_status r;
    114 	struct usbd_hub *hub;
    115 	usb_device_request_t req;
    116 	usb_hub_descriptor_t hubdesc;
    117 	int p, port, nports, nremov;
    118 	usbd_interface_handle iface;
    119 	usb_endpoint_descriptor_t *ed;
    120 
    121 	DPRINTFN(1,("uhub_attach\n"));
    122 	sc->sc_hub = dev;
    123 	usbd_devinfo(dev, 1, devinfo);
    124 	USB_ATTACH_SETUP;
    125 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
    126 
    127 	r = usbd_set_config_index(dev, 0, 1);
    128 	if (r != USBD_NORMAL_COMPLETION) {
    129 		DPRINTF(("%s: configuration failed, error=%s\n",
    130 			 USBDEVNAME(sc->sc_dev), usbd_errstr(r)));
    131 		USB_ATTACH_ERROR_RETURN;
    132 	}
    133 
    134 	if (dev->depth > USB_HUB_MAX_DEPTH) {
    135 		printf("%s: hub depth (%d) exceeded, hub ignored\n",
    136 		       USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
    137 		USB_ATTACH_ERROR_RETURN;
    138 	}
    139 
    140 	/* Get hub descriptor. */
    141 	req.bmRequestType = UT_READ_CLASS_DEVICE;
    142 	req.bRequest = UR_GET_DESCRIPTOR;
    143 	USETW(req.wValue, 0);
    144 	USETW(req.wIndex, 0);
    145 	USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
    146 	DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
    147 	r = usbd_do_request(dev, &req, &hubdesc);
    148 	nports = hubdesc.bNbrPorts;
    149 	if (r == USBD_NORMAL_COMPLETION && nports > 7) {
    150 		USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
    151 		r = usbd_do_request(dev, &req, &hubdesc);
    152 	}
    153 	if (r != USBD_NORMAL_COMPLETION) {
    154 		DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
    155 			 USBDEVNAME(sc->sc_dev), usbd_errstr(r)));
    156 		USB_ATTACH_ERROR_RETURN;
    157 	}
    158 
    159 	for (nremov = 0, port = 1; port <= nports; port++)
    160 		if (!UHD_NOT_REMOV(&hubdesc, port))
    161 			nremov++;
    162 	printf("%s: %d port%s with %d removable, %s powered\n",
    163 	       USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
    164 	       nremov, dev->self_powered ? "self" : "bus");
    165 
    166 
    167 	hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
    168 		     M_USBDEV, M_NOWAIT);
    169 	if (hub == 0)
    170 		USB_ATTACH_ERROR_RETURN;
    171 	dev->hub = hub;
    172 	dev->hub->hubsoftc = sc;
    173 	hub->explore = uhub_explore;
    174 	hub->hubdesc = hubdesc;
    175 
    176 	DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
    177 		    "parent->selfpowered=%d\n",
    178 		 dev->self_powered, dev->powersrc->parent,
    179 		 dev->powersrc->parent ?
    180 		 dev->powersrc->parent->self_powered : 0));
    181 	if (!dev->self_powered && dev->powersrc->parent &&
    182 	    !dev->powersrc->parent->self_powered) {
    183 		printf("%s: bus powered hub connected to bus powered hub, "
    184 		       "ignored\n", USBDEVNAME(sc->sc_dev));
    185 		goto bad;
    186 	}
    187 
    188 	/* Set up interrupt pipe. */
    189 	r = usbd_device2interface_handle(dev, 0, &iface);
    190 	if (r != USBD_NORMAL_COMPLETION) {
    191 		printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
    192 		goto bad;
    193 	}
    194 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    195 	if (ed == 0) {
    196 		printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
    197 		goto bad;
    198 	}
    199 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    200 		printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
    201 		goto bad;
    202 	}
    203 
    204 	r = usbd_open_pipe_intr(iface, ed->bEndpointAddress,USBD_SHORT_XFER_OK,
    205 				&sc->sc_ipipe, sc, sc->sc_status,
    206 				sizeof(sc->sc_status),
    207 				uhub_intr);
    208 	if (r != USBD_NORMAL_COMPLETION) {
    209 		printf("%s: cannot open interrupt pipe\n",
    210 		       USBDEVNAME(sc->sc_dev));
    211 		goto bad;
    212 	}
    213 
    214 	/* Wait with power off for a while. */
    215 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
    216 
    217 	for (p = 0; p < nports; p++) {
    218 		struct usbd_port *up = &hub->ports[p];
    219 		up->device = 0;
    220 		up->parent = dev;
    221 		up->portno = p+1;
    222 		r = uhub_init_port(up);
    223 		if (r != USBD_NORMAL_COMPLETION)
    224 			printf("%s: init of port %d failed\n",
    225 			       USBDEVNAME(sc->sc_dev), up->portno);
    226 	}
    227 	sc->sc_running = 1;
    228 
    229 	USB_ATTACH_SUCCESS_RETURN;
    230 
    231  bad:
    232 	free(hub, M_USBDEV);
    233 	dev->hub = 0;
    234 	USB_ATTACH_ERROR_RETURN;
    235 }
    236 
    237 usbd_status
    238 uhub_init_port(up)
    239 	struct usbd_port *up;
    240 {
    241 	int port = up->portno;
    242 	usbd_device_handle dev = up->parent;
    243 	usbd_status r;
    244 	u_int16_t pstatus;
    245 
    246 	r = usbd_get_port_status(dev, port, &up->status);
    247 	if (r != USBD_NORMAL_COMPLETION)
    248 		return (r);
    249 	pstatus = UGETW(up->status.wPortStatus);
    250 	DPRINTF(("usbd_init_port: adding hub port=%d status=0x%04x "
    251 		 "change=0x%04x\n",
    252 		 port, pstatus, UGETW(up->status.wPortChange)));
    253 	if ((pstatus & UPS_PORT_POWER) == 0) {
    254 		/* Port lacks power, turn it on */
    255 
    256 		/* First let the device go through a good power cycle, */
    257 		usbd_delay_ms(dev, USB_PORT_POWER_DOWN_TIME);
    258 
    259 #if 0
    260 usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT);
    261 usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT);
    262 #endif
    263 
    264 		/* then turn the power on. */
    265 		r = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
    266 		if (r != USBD_NORMAL_COMPLETION)
    267 			return (r);
    268 		DPRINTF(("usb_init_port: turn on port %d power status=0x%04x "
    269 			 "change=0x%04x\n",
    270 			 port, UGETW(up->status.wPortStatus),
    271 			 UGETW(up->status.wPortChange)));
    272 		/* Wait for stable power. */
    273 		usbd_delay_ms(dev, dev->hub->hubdesc.bPwrOn2PwrGood *
    274 			           UHD_PWRON_FACTOR);
    275 		/* Get the port status again. */
    276 		r = usbd_get_port_status(dev, port, &up->status);
    277 		if (r != USBD_NORMAL_COMPLETION)
    278 			return (r);
    279 		DPRINTF(("usb_init_port: after power on status=0x%04x "
    280 			 "change=0x%04x\n",
    281 			 UGETW(up->status.wPortStatus),
    282 			 UGETW(up->status.wPortChange)));
    283 
    284 #if 0
    285 usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT);
    286 usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT);
    287 usbd_get_port_status(dev, port, &up->status);
    288 #endif
    289 
    290 		pstatus = UGETW(up->status.wPortStatus);
    291 		if ((pstatus & UPS_PORT_POWER) == 0)
    292 			printf("%s: port %d did not power up\n",
    293  USBDEVNAME(((struct uhub_softc *)dev->hub->hubsoftc)->sc_dev), port);
    294 
    295 	}
    296 	if (dev->self_powered)
    297 		/* Self powered hub, give ports maximum current. */
    298 		up->power = USB_MAX_POWER;
    299 	else
    300 		up->power = USB_MIN_POWER;
    301 	return (USBD_NORMAL_COMPLETION);
    302 }
    303 
    304 usbd_status
    305 uhub_explore(dev)
    306 	usbd_device_handle dev;
    307 {
    308 	usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
    309 	struct uhub_softc *sc = dev->hub->hubsoftc;
    310 	struct usbd_port *up;
    311 	usbd_status r;
    312 	int port;
    313 	int change, status;
    314 
    315 	DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
    316 
    317 	if (!sc->sc_running)
    318 		return (USBD_NOT_STARTED);
    319 
    320 	/* Ignore hubs that are too deep. */
    321 	if (dev->depth > USB_HUB_MAX_DEPTH)
    322 		return (USBD_TOO_DEEP);
    323 
    324 	for(port = 1; port <= hd->bNbrPorts; port++) {
    325 		up = &dev->hub->ports[port-1];
    326 		r = usbd_get_port_status(dev, port, &up->status);
    327 		if (r != USBD_NORMAL_COMPLETION) {
    328 			DPRINTF(("uhub_explore: get port status failed, "
    329 				 "error=%s\n",
    330 				 usbd_errstr(r)));
    331 			continue;
    332 		}
    333 		status = UGETW(up->status.wPortStatus);
    334 		change = UGETW(up->status.wPortChange);
    335 		DPRINTFN(5, ("uhub_explore: port %d status 0x%04x 0x%04x\n",
    336 			     port, status, change));
    337 		if (change & UPS_C_PORT_ENABLED) {
    338 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
    339 			if (status & UPS_PORT_ENABLED) {
    340 				printf("%s: illegal enable change, port %d\n",
    341 				       USBDEVNAME(sc->sc_dev), port);
    342 			} else {
    343 				/* Port error condition. */
    344 				if (up->restartcnt++ < USBD_RESTART_MAX) {
    345 					printf("%s: port error, restarting "
    346 					       "port %d\n",
    347 					       USBDEVNAME(sc->sc_dev), port);
    348 					goto disco;
    349 				} else {
    350 					printf("%s: port error, giving up "
    351 					       "port %d\n",
    352 					       USBDEVNAME(sc->sc_dev), port);
    353 				}
    354 			}
    355 		}
    356 		if (!(change & UPS_C_CONNECT_STATUS)) {
    357 			/* No status change, just do recursive explore. */
    358 			if (up->device && up->device->hub)
    359 				up->device->hub->explore(up->device);
    360 			continue;
    361 		}
    362 		DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
    363 			 dev->address, port));
    364 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
    365 		usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
    366 		/*
    367 		 * If there is already a device on the port the change status
    368 		 * must mean that is has disconnected.  Looking at the
    369 		 * current connect status is not enough to figure this out
    370 		 * since a new unit may have been connected before we handle
    371 		 * the disconnect.
    372 		 */
    373 	disco:
    374 		if (up->device) {
    375 			/* Disconnected */
    376 			DPRINTF(("uhub_explore: device %d disappeared "
    377 				 "on port %d\n", up->device->address, port));
    378 			usb_disconnect_port(up, USBDEV(sc->sc_dev));
    379 			usbd_clear_port_feature(dev, port,
    380 						UHF_C_PORT_CONNECTION);
    381 		}
    382 		if (!(status & UPS_CURRENT_CONNECT_STATUS))
    383 			continue;
    384 
    385 		/* Connected */
    386 		up->restartcnt = 0;
    387 
    388 		/* Wait for maximum device power up time. */
    389 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
    390 
    391 		/* Reset port, which implies enabling it. */
    392 		if (usbd_reset_port(dev, port, &up->status) !=
    393 		    USBD_NORMAL_COMPLETION)
    394 			continue;
    395 
    396 		/* Get device info and set its address. */
    397 		r = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
    398 				    dev->depth + 1, status & UPS_LOW_SPEED,
    399 				    port, up);
    400 		/* XXX retry a few times? */
    401 		if (r != USBD_NORMAL_COMPLETION) {
    402 			DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
    403 				     "error=%s\n", usbd_errstr(r)));
    404 			/* Avoid addressing problems by disabling. */
    405 			/* usbd_reset_port(dev, port, &up->status); */
    406 
    407 			/*
    408 			 * The unit refused to accept a new address, or had
    409 			 * some other serious problem.  Since we cannot leave
    410 			 * at 0 we have to disable the port instead.
    411 			 */
    412 			printf("%s: device problem, disabling port %d\n",
    413 			       USBDEVNAME(sc->sc_dev), port);
    414 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
    415 			/* Make sure we don't try to restart it infinitely. */
    416 			up->restartcnt = USBD_RESTART_MAX;
    417 		} else {
    418 			if (up->device->hub)
    419 				up->device->hub->explore(up->device);
    420 		}
    421 	}
    422 	return (USBD_NORMAL_COMPLETION);
    423 }
    424 
    425 int
    426 uhub_activate(self, act)
    427 	device_ptr_t self;
    428 	enum devact act;
    429 {
    430 	struct uhub_softc *sc = (struct uhub_softc *)self;
    431 	usbd_device_handle devhub = sc->sc_hub;
    432 	int nports, p, i;
    433 
    434 	switch (act) {
    435 	case DVACT_ACTIVATE:
    436 		return (EOPNOTSUPP);
    437 		break;
    438 
    439 	case DVACT_DEACTIVATE:
    440 		nports = devhub->hub->hubdesc.bNbrPorts;
    441 		for(p = 0; p < nports; p++) {
    442 			usbd_device_handle dev = devhub->hub->ports[p].device;
    443 			if (dev) {
    444 				for (i = 0; dev->subdevs[i]; i++)
    445 					config_deactivate(dev->subdevs[i]);
    446 			}
    447 		}
    448 		break;
    449 	}
    450 	return (0);
    451 }
    452 
    453 /*
    454  * Called from process context when the hub is gone.
    455  * Detach all devices on active ports.
    456  */
    457 int
    458 uhub_detach(self, flags)
    459 	device_ptr_t self;
    460 	int flags;
    461 {
    462 	struct uhub_softc *sc = (struct uhub_softc *)self;
    463 	usbd_device_handle dev = sc->sc_hub;
    464 	struct usbd_port *rup;
    465 	int p, nports;
    466 
    467 	DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
    468 
    469 	if (!dev->hub) {
    470 		/* Must be partially working */
    471 		return (0);
    472 	}
    473 
    474 	usbd_abort_pipe(sc->sc_ipipe);
    475 	usbd_close_pipe(sc->sc_ipipe);
    476 
    477 	nports = dev->hub->hubdesc.bNbrPorts;
    478 	for(p = 0; p < nports; p++) {
    479 		rup = &dev->hub->ports[p];
    480 		if (rup->device)
    481 			usb_disconnect_port(rup, self);
    482 	}
    483 
    484 	free(dev->hub, M_USBDEV);
    485 	dev->hub = 0;
    486 
    487 	return (0);
    488 }
    489 
    490 /*
    491  * Hub interrupt.
    492  * This an indication that some port has changed status.
    493  * Notify the bus event handler thread that we need
    494  * to be explored again.
    495  */
    496 void
    497 uhub_intr(reqh, addr, status)
    498 	usbd_request_handle reqh;
    499 	usbd_private_handle addr;
    500 	usbd_status status;
    501 {
    502 	struct uhub_softc *sc = addr;
    503 
    504 	DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
    505 	if (status != USBD_NORMAL_COMPLETION)
    506 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    507 
    508 	usb_needs_explore(sc->sc_hub->bus);
    509 }
    510 
    511 #if defined(__FreeBSD__)
    512 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
    513 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
    514 #endif
    515