Home | History | Annotate | Line # | Download | only in usb
uhub.c revision 1.8
      1 /*	$NetBSD: uhub.c,v 1.8 1998/12/08 15:48:18 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 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/malloc.h>
     45 #include <sys/device.h>
     46 #include <sys/proc.h>
     47 #include <sys/device.h>
     48 
     49 #include <dev/usb/usb.h>
     50 
     51 #include <dev/usb/usbdi.h>
     52 #include <dev/usb/usbdi_util.h>
     53 #include <dev/usb/usbdivar.h>
     54 
     55 #ifdef USB_DEBUG
     56 #define DPRINTF(x)	if (usbdebug) printf x
     57 #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
     58 extern int	usbdebug;
     59 extern char 	*usbd_error_strs[];
     60 #else
     61 #define DPRINTF(x)
     62 #define DPRINTFN(n,x)
     63 #endif
     64 
     65 struct uhub_softc {
     66 	struct device sc_dev;		/* base device */
     67 	usbd_device_handle sc_hub;	/* USB device */
     68 	usbd_pipe_handle sc_ipipe;      /* interrupt pipe */
     69 	u_int8_t sc_status[1];	/* XXX more ports */
     70 	u_char sc_running;
     71 };
     72 
     73 int uhub_match __P((struct device *, struct cfdata *, void *));
     74 void uhub_attach __P((struct device *, struct device *, void *));
     75 
     76 usbd_status uhub_init_port __P((int, struct usbd_port *, usbd_device_handle));
     77 void uhub_disconnect __P((struct uhub_softc *sc, struct device *parent,
     78 			  struct usbd_port *up, int portno));
     79 usbd_status uhub_explore __P((struct device *parent, usbd_device_handle hub));
     80 void uhub_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
     81 
     82 /*void uhub_disco __P((void *));*/
     83 
     84 extern struct cfdriver uhub_cd;
     85 
     86 struct cfattach uhub_ca = {
     87 	sizeof(struct uhub_softc), uhub_match, uhub_attach
     88 };
     89 
     90 struct cfattach uhub_uhub_ca = {
     91 	sizeof(struct uhub_softc), uhub_match, uhub_attach
     92 };
     93 
     94 int
     95 uhub_match(parent, match, aux)
     96 	struct device *parent;
     97 	struct cfdata *match;
     98 	void *aux;
     99 {
    100 	struct usb_attach_arg *uaa = aux;
    101 	usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
    102 
    103 	DPRINTFN(1,("uhub_match, dd=%p\n", dd));
    104 	/*
    105 	 * The subclass for hubs seems to be 0 for some and 1 for others,
    106 	 * so we just ignore the subclass.
    107 	 */
    108 	if (uaa->iface == 0 && dd->bDeviceClass == UCLASS_HUB)
    109 		return (UMATCH_DEVCLASS_DEVSUBCLASS);
    110 	return (UMATCH_NONE);
    111 }
    112 
    113 void
    114 uhub_attach(parent, self, aux)
    115 	struct device *parent;
    116 	struct device *self;
    117 	void *aux;
    118 {
    119 	struct uhub_softc *sc = (struct uhub_softc *)self;
    120 	struct usb_attach_arg *uaa = aux;
    121 	usbd_device_handle dev = uaa->device;
    122 	char devinfo[1024];
    123 	usbd_status r;
    124 	struct usbd_hub *hub;
    125 	usb_device_request_t req;
    126 	usb_hub_descriptor_t hubdesc;
    127 	int port, nports;
    128 	usbd_interface_handle iface;
    129 	usb_endpoint_descriptor_t *ed;
    130 
    131 	DPRINTFN(1,("uhub_attach\n"));
    132 	sc->sc_hub = dev;
    133 	usbd_devinfo(dev, 1, devinfo);
    134 	printf("\n%s: %s\n", sc->sc_dev.dv_xname, devinfo);
    135 
    136 	r = usbd_set_config_index(dev, 0, 1);
    137 	if (r != USBD_NORMAL_COMPLETION) {
    138 		printf("%s: configuration failed, error=%d(%s)\n",
    139 		       sc->sc_dev.dv_xname, r, usbd_error_strs[r]);
    140 		return;
    141 	}
    142 
    143 	if (dev->depth > USB_HUB_MAX_DEPTH) {
    144 		printf("%s: hub depth (%d) exceeded, hub ignored\n",
    145 		       sc->sc_dev.dv_xname, USB_HUB_MAX_DEPTH);
    146 		return;
    147 	}
    148 
    149 	/* Get hub descriptor. */
    150 	req.bmRequestType = UT_READ_CLASS_DEVICE;
    151 	req.bRequest = UR_GET_DESCRIPTOR;
    152 	USETW(req.wValue, 0);
    153 	USETW(req.wIndex, 0);
    154 	USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
    155 	DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
    156 	/* XXX not correct for hubs with >7 ports */
    157 	r = usbd_do_request(dev, &req, &hubdesc);
    158 	if (r != USBD_NORMAL_COMPLETION) {
    159 		printf("%s: getting hub descriptor failed, error=%d(%s)\n",
    160 		       sc->sc_dev.dv_xname, r, usbd_error_strs[r]);
    161 		return;
    162 	}
    163 
    164 	nports = hubdesc.bNbrPorts;
    165 	hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
    166 		     M_USB, M_NOWAIT);
    167 	if (hub == 0)
    168 		return;
    169 	dev->hub = hub;
    170 	dev->hub->hubdata = sc;
    171 	hub->explore = uhub_explore;
    172 	hub->hubdesc = hubdesc;
    173 
    174 	DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, parent->selfpowered=%d\n",
    175 		 dev->self_powered, dev->powersrc->parent,
    176 		 dev->powersrc->parent ?
    177 		 dev->powersrc->parent->self_powered : 0));
    178 	if (!dev->self_powered && dev->powersrc->parent &&
    179 	    !dev->powersrc->parent->self_powered) {
    180 		printf("%s: bus powered hub connected to bus powered hub, ignored\n",
    181 		       sc->sc_dev.dv_xname);
    182 		return;
    183 	}
    184 
    185 	/* Set up interrupt pipe. */
    186 	r = usbd_device2interface_handle(dev, 0, &iface);
    187 	if (r != USBD_NORMAL_COMPLETION) {
    188 		printf("%s: no interface handle\n", sc->sc_dev.dv_xname);
    189 		return;
    190 	}
    191 	ed = usbd_interface2endpoint_descriptor(iface, 0);
    192 	if (ed == 0) {
    193 		printf("%s: no endpoint descriptor\n", sc->sc_dev.dv_xname);
    194 		return;
    195 	}
    196 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
    197 		printf("%s: bad interrupt endpoint\n", sc->sc_dev.dv_xname);
    198 		return;
    199 	}
    200 
    201 	r = usbd_open_pipe_intr(iface, ed->bEndpointAddress,USBD_SHORT_XFER_OK,
    202 				&sc->sc_ipipe, sc, sc->sc_status,
    203 				sizeof(sc->sc_status),
    204 				uhub_intr);
    205 	if (r != USBD_NORMAL_COMPLETION) {
    206 		printf("%s: cannot open interrupt pipe\n",sc->sc_dev.dv_xname);
    207 		return;
    208 	}
    209 
    210 	for (port = 1; port <= nports; port++) {
    211 		r = uhub_init_port(port, &hub->ports[port-1], dev);
    212 		if (r != USBD_NORMAL_COMPLETION)
    213 			printf("%s: init of port %d failed\n",
    214 			       sc->sc_dev.dv_xname, port);
    215 	}
    216 	sc->sc_running = 1;
    217 }
    218 
    219 usbd_status
    220 uhub_init_port(port, uport, dev)
    221 	int port;
    222 	struct usbd_port *uport;
    223 	usbd_device_handle dev;
    224 {
    225 	usbd_status r;
    226 	u_int16_t pstatus;
    227 
    228 	uport->device = 0;
    229 	uport->parent = dev;
    230 	r = usbd_get_port_status(dev, port, &uport->status);
    231 	if (r != USBD_NORMAL_COMPLETION)
    232 		return r;
    233 	pstatus = UGETW(uport->status.wPortStatus);
    234 	DPRINTF(("usbd_init_port: adding hub port=%d status=0x%04x change=0x%04x\n",
    235 		 port, pstatus, UGETW(uport->status.wPortChange)));
    236 	if ((pstatus & UPS_PORT_POWER) == 0) {
    237 		/* Port lacks power, turn it on */
    238 #if 0
    239 		/* First let the device go through a good power cycle, */
    240 		usbd_delay_ms(dev->bus, USB_POWER_DOWN_TIME);
    241 #endif
    242 		/* then turn the power on. */
    243 		r = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
    244 		if (r != USBD_NORMAL_COMPLETION)
    245 			return (r);
    246 		r = usbd_get_port_status(dev, port, &uport->status);
    247 		if (r != USBD_NORMAL_COMPLETION)
    248 			return (r);
    249 		DPRINTF(("usb_init_port: turn on port %d power status=0x%04x change=0x%04x\n",
    250 			 port, UGETW(uport->status.wPortStatus),
    251 			 UGETW(uport->status.wPortChange)));
    252 		/* Wait for stable power. */
    253 		usbd_delay_ms(dev->bus, dev->hub->hubdesc.bPwrOn2PwrGood *
    254 			                UHD_PWRON_FACTOR);
    255 	}
    256 	if (dev->self_powered)
    257 		/* Self powered hub, give ports maximum current. */
    258 		uport->power = USB_MAX_POWER;
    259 	else
    260 		uport->power = USB_MIN_POWER;
    261 	return (USBD_NORMAL_COMPLETION);
    262 }
    263 
    264 usbd_status
    265 uhub_explore(parent, dev)
    266 	struct device *parent;
    267 	usbd_device_handle dev;
    268 {
    269 	usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
    270 	struct uhub_softc *sc = dev->hub->hubdata;
    271 	struct usbd_port *up;
    272 	usbd_status r;
    273 	int port;
    274 	int change, status;
    275 
    276 	DPRINTF(("uhub_explore dev=%p addr=%d\n", dev, dev->address));
    277 
    278 	if (!sc->sc_running)
    279 		return (USBD_NOT_STARTED);
    280 
    281 	/* Ignore hubs that are too deep. */
    282 	if (dev->depth > USB_HUB_MAX_DEPTH)
    283 		return (USBD_TOO_DEEP);
    284 
    285 	for(port = 1; port <= hd->bNbrPorts; port++) {
    286 		up = &dev->hub->ports[port-1];
    287 		r = usbd_get_port_status(dev, port, &up->status);
    288 		if (r != USBD_NORMAL_COMPLETION) {
    289 			DPRINTF(("uhub_explore: get port status failed, error=%d(%s)\n",
    290 				 r, usbd_error_strs[r]));
    291 			continue;
    292 		}
    293 		status = UGETW(up->status.wPortStatus);
    294 		change = UGETW(up->status.wPortChange);
    295 		DPRINTFN(5, ("uhub_explore: port %d status 0x%04x 0x%04x\n",
    296 			     port, status, change));
    297 		if (!(change & UPS_CURRENT_CONNECT_STATUS)) {
    298 			/* No status change, just do recursive explore. */
    299 			if (up->device && up->device->hub)
    300 				up->device->hub->explore(parent, up->device);
    301 			continue;
    302 		}
    303 		DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
    304 			 dev->address, port));
    305 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
    306 		usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
    307 		/*
    308 		 * If there is already a device on the port the change status
    309 		 * must mean that is has disconnected.  Looking at the
    310 		 * current connect status is not enough to figure this out
    311 		 * since a new unit may have been connected before we handle
    312 		 * the disconnect.
    313 		 */
    314 		if (up->device) {
    315 			/* Disconnected */
    316 			DPRINTF(("uhub_explore: device %d disappeared on port %d\n",
    317 				 up->device->address, port));
    318 			uhub_disconnect(sc, parent, up, port);
    319 			usbd_clear_port_feature(dev, port,
    320 						UHF_C_PORT_CONNECTION);
    321 		}
    322 		if (!(status & UPS_CURRENT_CONNECT_STATUS))
    323 			continue;
    324 
    325 		/* Connected */
    326 		/* Wait for maximum device power up time. */
    327 		usbd_delay_ms(dev->bus, USB_PORT_POWERUP_DELAY);
    328 		/* Reset port, which implies enabling it. */
    329 		if (usbd_reset_port(dev, port, &up->status) !=
    330 		    USBD_NORMAL_COMPLETION)
    331 			continue;
    332 
    333 		/* Wait for power to settle in device. */
    334 		usbd_delay_ms(dev->bus, USB_POWER_SETTLE);
    335 
    336 		/* Get device info and set its address. */
    337 		r = usbd_new_device((struct device *)sc, dev->bus,
    338 				    dev->depth + 1, status & UPS_LOW_SPEED,
    339 				    port, up);
    340 		/* XXX retry a few times? */
    341 		if (r != USBD_NORMAL_COMPLETION) {
    342 			DPRINTFN(-1,("uhub_explore: usb_new_device failed, error=%d(%s)\n", r, usbd_error_strs[r]));
    343 			/* Avoid addressing problems by disabling. */
    344 			/* usbd_reset_port(dev, port, &up->status); */
    345 /* XXX
    346  * What should we do.  The device may or may not be at its
    347  * assigned address.  In any case we'd like to ignore it.
    348  * Maybe the port should be disabled until the device is
    349  * disconnected.
    350  */
    351 			if (r == USBD_SET_ADDR_FAILED || 1) {/* XXX */
    352 				/* The unit refused to accept a new
    353 				 * address, and since we cannot leave
    354 				 * at 0 we have to disable the port
    355 				 * instead. */
    356 				printf("%s: device problem, disabling port %d\n",
    357 				       parent->dv_xname, port);
    358 				usbd_clear_port_feature(dev, port,
    359 							UHF_PORT_ENABLE);
    360 			}
    361 		} else {
    362 			if (up->device->hub)
    363 				up->device->hub->explore(parent, up->device);
    364 		}
    365 	}
    366 	return (USBD_NORMAL_COMPLETION);
    367 }
    368 
    369 void
    370 uhub_disconnect(sc, parent, up, portno)
    371 	struct uhub_softc *sc;
    372 	struct device *parent;
    373 	struct usbd_port *up;
    374 	int portno;
    375 {
    376 	usbd_device_handle dev = up->device;
    377 	usbd_pipe_handle p, n;
    378 	usb_hub_descriptor_t *hd;
    379 	struct usbd_port *spi;
    380 	int i, port;
    381 
    382 	DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
    383 		    up, dev, portno));
    384 
    385 	printf("%s: device addr %d%s on hub addr %d, port %d disconnected\n",
    386 	       parent->dv_xname, dev->address, dev->hub ? " (hub)" : "",
    387 	       up->parent->address, portno);
    388 
    389 	for (i = 0; i < dev->cdesc->bNumInterface; i++) {
    390 		for (p = LIST_FIRST(&dev->ifaces[i].pipes); p; p = n) {
    391 			n = LIST_NEXT(p, next);
    392 			if (p->disco)
    393 				p->disco(p->discoarg);
    394 			usbd_abort_pipe(p);
    395 			usbd_close_pipe(p);
    396 		}
    397 	}
    398 
    399 	/* XXX Free all data structures and disable further I/O. */
    400 
    401 
    402 	if (dev->hub) {
    403 		DPRINTFN(3,("usb_disconnect: hub, recursing\n"));
    404 		hd = &dev->hub->hubdesc;
    405 		for(port = 1; port <= hd->bNbrPorts; port++) {
    406 			spi = &dev->hub->ports[port-1];
    407 			if (spi->device)
    408 				uhub_disconnect(sc, parent, spi, port);
    409 		}
    410 	}
    411 
    412 	dev->bus->devices[dev->address] = 0;
    413 	up->device = 0;
    414 	/* XXX free */
    415 }
    416 
    417 void
    418 uhub_intr(reqh, addr, status)
    419 	usbd_request_handle reqh;
    420 	usbd_private_handle addr;
    421 	usbd_status status;
    422 {
    423 	struct uhub_softc *sc = addr;
    424 
    425 	DPRINTFN(1,("uhub_intr: sc=%p\n", sc));
    426 #if 0
    427 	if (status != USBD_NORMAL_COMPLETION)
    428 		usbd_clear_endpoint_stall(sc->sc_ipipe);
    429 	else
    430 #endif
    431 		usb_needs_explore(sc->sc_hub->bus);
    432 }
    433