xref: /openbsd-src/sys/dev/usb/uhub.c (revision 9f77a00801e3f67f7dca9c0a48a1d9e64f7540a8)
1 /*	$OpenBSD: uhub.c,v 1.71 2014/08/09 09:48:32 mpi Exp $ */
2 /*	$NetBSD: uhub.c,v 1.64 2003/02/08 03:32:51 ichiro Exp $	*/
3 /*	$FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma Exp $	*/
4 
5 /*
6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Lennart Augustsson (lennart@augustsson.net) at
11  * Carlstedt Research & Technology.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/device.h>
40 
41 #include <machine/bus.h>
42 
43 #include <dev/usb/usb.h>
44 #include <dev/usb/usbdi.h>
45 #include <dev/usb/usbdi_util.h>
46 #include <dev/usb/usbdivar.h>
47 
48 #define UHUB_INTR_INTERVAL 255	/* ms */
49 
50 #ifdef UHUB_DEBUG
51 #define DPRINTF(x...)	do { printf(x); } while (0)
52 #else
53 #define DPRINTF(x...)
54 #endif
55 
56 struct uhub_softc {
57 	struct device		sc_dev;		/* base device */
58 	struct usbd_device	*sc_hub;	/* USB device */
59 	struct usbd_pipe	*sc_ipipe;	/* interrupt pipe */
60 	u_int8_t		*sc_statusbuf;	/* per port status buffer */
61 	size_t			sc_statuslen;	/* status bufferlen */
62 	u_char			sc_running;
63 };
64 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol)
65 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
66 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
67 
68 int uhub_explore(struct usbd_device *hub);
69 void uhub_intr(struct usbd_xfer *, void *, usbd_status);
70 
71 /*
72  * We need two attachment points:
73  * hub to usb and hub to hub
74  * Every other driver only connects to hubs
75  */
76 
77 int uhub_match(struct device *, void *, void *);
78 void uhub_attach(struct device *, struct device *, void *);
79 int uhub_detach(struct device *, int);
80 
81 struct cfdriver uhub_cd = {
82 	NULL, "uhub", DV_DULL
83 };
84 
85 const struct cfattach uhub_ca = {
86 	sizeof(struct uhub_softc), uhub_match, uhub_attach,  uhub_detach
87 };
88 
89 const struct cfattach uhub_uhub_ca = {
90 	sizeof(struct uhub_softc), uhub_match, uhub_attach,  uhub_detach
91 };
92 
93 int
94 uhub_match(struct device *parent, void *match, void *aux)
95 {
96 	struct usb_attach_arg *uaa = aux;
97 	usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
98 
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 == NULL && dd->bDeviceClass == UDCLASS_HUB)
104 		return (UMATCH_DEVCLASS_DEVSUBCLASS);
105 	return (UMATCH_NONE);
106 }
107 
108 void
109 uhub_attach(struct device *parent, struct device *self, void *aux)
110 {
111 	struct uhub_softc *sc = (struct uhub_softc *)self;
112 	struct usb_attach_arg *uaa = aux;
113 	struct usbd_device *dev = uaa->device;
114 	struct usbd_hub *hub = NULL;
115 	union {
116 		usb_hub_descriptor_t	hs;
117 		usb_hub_ss_descriptor_t	ss;
118 	} hd;
119 	int p, port, nports, powerdelay;
120 	struct usbd_interface *iface;
121 	usb_endpoint_descriptor_t *ed;
122 	struct usbd_tt *tts = NULL;
123 	usbd_status err;
124 #ifdef UHUB_DEBUG
125 	int nremov;
126 #endif
127 
128 	sc->sc_hub = dev;
129 
130 	err = usbd_set_config_index(dev, 0, 1);
131 	if (err) {
132 		DPRINTF("%s: configuration failed, error=%s\n",
133 			 sc->sc_dev.dv_xname, usbd_errstr(err));
134 		return;
135 	}
136 
137 	if (dev->depth > USB_HUB_MAX_DEPTH) {
138 		printf("%s: hub depth (%d) exceeded, hub ignored\n",
139 		       sc->sc_dev.dv_xname, USB_HUB_MAX_DEPTH);
140 		return;
141 	}
142 
143 	/* Get hub descriptor. */
144 	if (dev->speed == USB_SPEED_SUPER) {
145 		err = usbd_get_hub_ss_descriptor(dev, &hd.ss, 1);
146 		if (!err && nports > 7)
147 			usbd_get_hub_ss_descriptor(dev, &hd.ss, nports);
148 	} else {
149 		err = usbd_get_hub_descriptor(dev, &hd.hs, 1);
150 		if (!err && nports > 7)
151 			usbd_get_hub_descriptor(dev, &hd.hs, nports);
152 	}
153 
154 	nports = hd.hs.bNbrPorts;
155 	powerdelay = (hd.hs.bPwrOn2PwrGood * UHD_PWRON_FACTOR);
156 
157 	if (err) {
158 		DPRINTF("%s: getting hub descriptor failed, error=%s\n",
159 			 sc->sc_dev.dv_xname, usbd_errstr(err));
160 		return;
161 	}
162 
163 #ifdef UHUB_DEBUG
164 	for (nremov = 0, port = 1; port <= nports; port++) {
165 		if (dev->speed == USB_SPEED_SUPER) {
166 			if (!UHD_NOT_REMOV(&hd.ss, port))
167 				nremov++;
168 		} else {
169 			if (!UHD_NOT_REMOV(&hd.hs, port))
170 				nremov++;
171 		}
172 	}
173 
174 	printf("%s: %d port%s with %d removable, %s powered",
175 	       sc->sc_dev.dv_xname, nports, nports != 1 ? "s" : "",
176 	       nremov, dev->self_powered ? "self" : "bus");
177 
178 	if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
179 		printf(", %s transaction translator%s",
180 		    UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
181 		    UHUB_IS_SINGLE_TT(sc) ? "" : "s");
182 	}
183 
184 	printf("\n");
185 #endif
186 
187 	if (nports == 0) {
188 		printf("%s: no ports, hub ignored\n", sc->sc_dev.dv_xname);
189 		goto bad;
190 	}
191 
192 	hub = malloc(sizeof(*hub), M_USBDEV, M_NOWAIT);
193 	if (hub == NULL)
194 		return;
195 	hub->ports = malloc(sizeof(struct usbd_port) * nports,
196 	    M_USBDEV, M_NOWAIT);
197 	if (hub->ports == NULL) {
198 		free(hub, M_USBDEV, 0);
199 		return;
200 	}
201 	dev->hub = hub;
202 	dev->hub->hubsoftc = sc;
203 	hub->explore = uhub_explore;
204 	hub->nports = nports;
205 	hub->powerdelay = powerdelay;
206 
207 	if (!dev->self_powered && dev->powersrc->parent != NULL &&
208 	    !dev->powersrc->parent->self_powered) {
209 		printf("%s: bus powered hub connected to bus powered hub, "
210 		       "ignored\n", sc->sc_dev.dv_xname);
211 		goto bad;
212 	}
213 
214 	/* Set up interrupt pipe. */
215 	err = usbd_device2interface_handle(dev, 0, &iface);
216 	if (err) {
217 		printf("%s: no interface handle\n", sc->sc_dev.dv_xname);
218 		goto bad;
219 	}
220 	ed = usbd_interface2endpoint_descriptor(iface, 0);
221 	if (ed == NULL) {
222 		printf("%s: no endpoint descriptor\n", sc->sc_dev.dv_xname);
223 		goto bad;
224 	}
225 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
226 		printf("%s: bad interrupt endpoint\n", sc->sc_dev.dv_xname);
227 		goto bad;
228 	}
229 
230 	sc->sc_statuslen = (nports + 1 + 7) / 8;
231 	sc->sc_statusbuf = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT);
232 	if (!sc->sc_statusbuf)
233 		goto bad;
234 
235 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
236 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_statusbuf,
237 		  sc->sc_statuslen, uhub_intr, UHUB_INTR_INTERVAL);
238 	if (err) {
239 		printf("%s: cannot open interrupt pipe\n",
240 		       sc->sc_dev.dv_xname);
241 		goto bad;
242 	}
243 
244 	/* Wait with power off for a while. */
245 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
246 
247 	/*
248 	 * To have the best chance of success we do things in the exact same
249 	 * order as Windoze98.  This should not be necessary, but some
250 	 * devices do not follow the USB specs to the letter.
251 	 *
252 	 * These are the events on the bus when a hub is attached:
253 	 *  Get device and config descriptors (see attach code)
254 	 *  Get hub descriptor (see above)
255 	 *  For all ports
256 	 *     turn on power
257 	 *     wait for power to become stable
258 	 * (all below happens in explore code)
259 	 *  For all ports
260 	 *     clear C_PORT_CONNECTION
261 	 *  For all ports
262 	 *     get port status
263 	 *     if device connected
264 	 *        wait 100 ms
265 	 *        turn on reset
266 	 *        wait
267 	 *        clear C_PORT_RESET
268 	 *        get port status
269 	 *        proceed with device attachment
270 	 */
271 
272 	if (UHUB_IS_HIGH_SPEED(sc)) {
273 		tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
274 		    sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
275 		if (!tts)
276 			goto bad;
277 	}
278 	/* Set up data structures */
279 	for (p = 0; p < nports; p++) {
280 		struct usbd_port *up = &hub->ports[p];
281 		up->device = NULL;
282 		up->parent = dev;
283 		up->portno = p+1;
284 		if (dev->self_powered)
285 			/* Self powered hub, give ports maximum current. */
286 			up->power = USB_MAX_POWER;
287 		else
288 			up->power = USB_MIN_POWER;
289 		up->restartcnt = 0;
290 		up->reattach = 0;
291 		if (UHUB_IS_HIGH_SPEED(sc)) {
292 			up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
293 			up->tt->hub = hub;
294 		} else {
295 			up->tt = NULL;
296 		}
297 	}
298 
299 	for (port = 1; port <= nports; port++) {
300 		/* Turn the power on. */
301 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
302 		if (err)
303 			printf("%s: port %d power on failed, %s\n",
304 			       sc->sc_dev.dv_xname, port,
305 			       usbd_errstr(err));
306 	}
307 
308 	/* Wait for stable power. */
309         if (dev->powersrc->parent != NULL)
310 		usbd_delay_ms(dev, powerdelay + USB_EXTRA_POWER_UP_TIME);
311 
312 	/* The usual exploration will finish the setup. */
313 
314 	sc->sc_running = 1;
315 
316 	return;
317 
318  bad:
319 	if (sc->sc_statusbuf)
320 		free(sc->sc_statusbuf, M_USBDEV, 0);
321 	if (hub) {
322 		if (hub->ports)
323 			free(hub->ports, M_USBDEV, 0);
324 		free(hub, M_USBDEV, 0);
325 	}
326 	dev->hub = NULL;
327 }
328 
329 int
330 uhub_explore(struct usbd_device *dev)
331 {
332 	struct uhub_softc *sc = dev->hub->hubsoftc;
333 	struct usbd_port *up;
334 	usbd_status err;
335 	int speed;
336 	int port;
337 	int change, status, reconnect;
338 
339 	if (usbd_is_dying(dev))
340 		return (EIO);
341 
342 	if (!sc->sc_running)
343 		return (ENXIO);
344 
345 	/* Ignore hubs that are too deep. */
346 	if (dev->depth > USB_HUB_MAX_DEPTH)
347 		return (EOPNOTSUPP);
348 
349 	for (port = 1; port <= dev->hub->nports; port++) {
350 		up = &dev->hub->ports[port-1];
351 		err = usbd_get_port_status(dev, port, &up->status);
352 		if (err) {
353 			DPRINTF("%s: get port %d status failed, error=%s\n",
354 			    sc->sc_dev.dv_xname, port, usbd_errstr(err));
355 			continue;
356 		}
357 		status = UGETW(up->status.wPortStatus);
358 		change = UGETW(up->status.wPortChange);
359 		reconnect = up->reattach;
360 		up->reattach = 0;
361 		DPRINTF("%s: port %d status=0x%04x change=0x%04x\n",
362 		    sc->sc_dev.dv_xname, port, status, change);
363 		if (change & UPS_C_PORT_ENABLED) {
364 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
365 			if (change & UPS_C_CONNECT_STATUS) {
366 				/* Ignore the port error if the device
367 				   vanished. */
368 			} else if (status & UPS_PORT_ENABLED) {
369 				printf("%s: illegal enable change, port %d\n",
370 				       sc->sc_dev.dv_xname, port);
371 			} else {
372 				/* Port error condition. */
373 				if (up->restartcnt) /* no message first time */
374 					printf("%s: port error, restarting "
375 					       "port %d\n",
376 					       sc->sc_dev.dv_xname, port);
377 
378 				if (up->restartcnt++ < USBD_RESTART_MAX)
379 					goto disco;
380 				else
381 					printf("%s: port error, giving up "
382 					       "port %d\n",
383 					       sc->sc_dev.dv_xname, port);
384 			}
385 		}
386 		if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
387 			/* No status change, just do recursive explore. */
388 			if (up->device != NULL && up->device->hub != NULL)
389 				up->device->hub->explore(up->device);
390 			continue;
391 		}
392 
393 		/* We have a connect status change, handle it. */
394 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
395 		/*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
396 		/*
397 		 * If there is already a device on the port the change status
398 		 * must mean that is has disconnected.  Looking at the
399 		 * current connect status is not enough to figure this out
400 		 * since a new unit may have been connected before we handle
401 		 * the disconnect.
402 		 */
403 	disco:
404 		if (up->device != NULL) {
405 			/* Disconnected */
406 			usbd_detach(up->device, &sc->sc_dev);
407 			up->device = NULL;
408 			usbd_clear_port_feature(dev, port,
409 						UHF_C_PORT_CONNECTION);
410 		}
411 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
412 			/* Nothing connected, just ignore it. */
413 			continue;
414 		}
415 
416 		/* Connected */
417 		if (!(status & UPS_PORT_POWER))
418 			printf("%s: strange, connected port %d has no power\n",
419 			       sc->sc_dev.dv_xname, port);
420 
421 		/* Wait for maximum device power up time. */
422 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
423 
424 		/* Reset port, which implies enabling it. */
425 		if (usbd_reset_port(dev, port, &up->status)) {
426 			printf("%s: port %d reset failed\n",
427 			       sc->sc_dev.dv_xname, port);
428 			continue;
429 		}
430 		/* Get port status again, it might have changed during reset */
431 		err = usbd_get_port_status(dev, port, &up->status);
432 		if (err) {
433 			DPRINTF("%s: get port %d status failed, error=%s\n",
434 			    sc->sc_dev.dv_xname, port, usbd_errstr(err));
435 			continue;
436 		}
437 		status = UGETW(up->status.wPortStatus);
438 		change = UGETW(up->status.wPortChange);
439 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
440 			/* Nothing connected, just ignore it. */
441 			DPRINTF("%s: port %d, device disappeared after reset\n",
442 			    sc->sc_dev.dv_xname, port);
443 			continue;
444 		}
445 
446 		/* Figure out device speed */
447 		if (status & UPS_SUPER_SPEED)
448 			speed = USB_SPEED_SUPER;
449 		else if (status & UPS_HIGH_SPEED)
450 			speed = USB_SPEED_HIGH;
451 		else if (status & UPS_LOW_SPEED)
452 			speed = USB_SPEED_LOW;
453 		else
454 			speed = USB_SPEED_FULL;
455 		/* Get device info and set its address. */
456 		err = usbd_new_device(&sc->sc_dev, dev->bus,
457 			  dev->depth + 1, speed, port, up);
458 		/* XXX retry a few times? */
459 		if (err) {
460 			DPRINTF("%s: usbd_new_device failed, error=%s\n",
461 			    sc->sc_dev.dv_xname, usbd_errstr(err));
462 			/* Avoid addressing problems by disabling. */
463 			/* usbd_reset_port(dev, port, &up->status); */
464 
465 			/*
466 			 * The unit refused to accept a new address, or had
467 			 * some other serious problem.  Since we cannot leave
468 			 * at 0 we have to disable the port instead.
469 			 */
470 			printf("%s: device problem, disabling port %d\n",
471 			       sc->sc_dev.dv_xname, port);
472 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
473 		} else {
474 			/* The port set up succeeded, reset error count. */
475 			up->restartcnt = 0;
476 
477 			if (up->device->hub)
478 				up->device->hub->explore(up->device);
479 		}
480 	}
481 
482 	return (0);
483 }
484 
485 /*
486  * Called from process context when the hub is gone.
487  * Detach all devices on active ports.
488  */
489 int
490 uhub_detach(struct device *self, int flags)
491 {
492 	struct uhub_softc *sc = (struct uhub_softc *)self;
493 	struct usbd_hub *hub = sc->sc_hub->hub;
494 	struct usbd_port *rup;
495 	int port;
496 
497 	if (hub == NULL)		/* Must be partially working */
498 		return (0);
499 
500 	usbd_abort_pipe(sc->sc_ipipe);
501 	usbd_close_pipe(sc->sc_ipipe);
502 
503 	for (port = 0; port < hub->nports; port++) {
504 		rup = &hub->ports[port];
505 		if (rup->device != NULL) {
506 			usbd_detach(rup->device, self);
507 			rup->device = NULL;
508 		}
509 	}
510 
511 	if (hub->ports[0].tt)
512 		free(hub->ports[0].tt, M_USBDEV, 0);
513 	if (sc->sc_statusbuf)
514 		free(sc->sc_statusbuf, M_USBDEV, 0);
515 	if (hub->ports)
516 		free(hub->ports, M_USBDEV, 0);
517 	free(hub, M_USBDEV, 0);
518 	sc->sc_hub->hub = NULL;
519 
520 	return (0);
521 }
522 
523 /*
524  * Hub interrupt.
525  * This an indication that some port has changed status.
526  * Notify the bus event handler thread that we need
527  * to be explored again.
528  */
529 void
530 uhub_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
531 {
532 	struct uhub_softc *sc = addr;
533 
534 	if (usbd_is_dying(sc->sc_hub))
535 		return;
536 
537 	DPRINTF("%s: intr status=%d\n", sc->sc_dev.dv_xname, status);
538 	if (status == USBD_STALLED)
539 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
540 	else if (status == USBD_NORMAL_COMPLETION)
541 		usb_needs_explore(sc->sc_hub, 0);
542 }
543