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