xref: /openbsd-src/sys/dev/usb/uhub.c (revision eff17a6a55d0fce39da7e589cb0eb3147404c33e)
1 /*	$OpenBSD: uhub.c,v 1.70 2014/08/09 09:45:14 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 	usb_hub_descriptor_t hubdesc;
116 	int p, port, nports, powerdelay;
117 	struct usbd_interface *iface;
118 	usb_endpoint_descriptor_t *ed;
119 	struct usbd_tt *tts = NULL;
120 	usbd_status err;
121 #ifdef UHUB_DEBUG
122 	int nremov;
123 #endif
124 
125 	sc->sc_hub = dev;
126 
127 	err = usbd_set_config_index(dev, 0, 1);
128 	if (err) {
129 		DPRINTF("%s: configuration failed, error=%s\n",
130 			 sc->sc_dev.dv_xname, usbd_errstr(err));
131 		return;
132 	}
133 
134 	if (dev->depth > USB_HUB_MAX_DEPTH) {
135 		printf("%s: hub depth (%d) exceeded, hub ignored\n",
136 		       sc->sc_dev.dv_xname, USB_HUB_MAX_DEPTH);
137 		return;
138 	}
139 
140 	/* Get hub descriptor. */
141 	err = usbd_get_hub_descriptor(dev, &hubdesc, 1);
142 	nports = hubdesc.bNbrPorts;
143 	powerdelay = (hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR);
144 	if (!err && nports > 7)
145 		usbd_get_hub_descriptor(dev, &hubdesc, nports);
146 	if (err) {
147 		DPRINTF("%s: getting hub descriptor failed, error=%s\n",
148 			 sc->sc_dev.dv_xname, usbd_errstr(err));
149 		return;
150 	}
151 
152 #ifdef UHUB_DEBUG
153 	for (nremov = 0, port = 1; port <= nports; port++)
154 		if (!UHD_NOT_REMOV(&hubdesc, port))
155 			nremov++;
156 
157 	printf("%s: %d port%s with %d removable, %s powered",
158 	       sc->sc_dev.dv_xname, nports, nports != 1 ? "s" : "",
159 	       nremov, dev->self_powered ? "self" : "bus");
160 
161 	if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
162 		printf(", %s transaction translator%s",
163 		    UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
164 		    UHUB_IS_SINGLE_TT(sc) ? "" : "s");
165 	}
166 
167 	printf("\n");
168 #endif
169 
170 	if (nports == 0) {
171 		printf("%s: no ports, hub ignored\n", sc->sc_dev.dv_xname);
172 		goto bad;
173 	}
174 
175 	hub = malloc(sizeof(*hub), M_USBDEV, M_NOWAIT);
176 	if (hub == NULL)
177 		return;
178 	hub->ports = malloc(sizeof(struct usbd_port) * nports,
179 	    M_USBDEV, M_NOWAIT);
180 	if (hub->ports == NULL) {
181 		free(hub, M_USBDEV, 0);
182 		return;
183 	}
184 	dev->hub = hub;
185 	dev->hub->hubsoftc = sc;
186 	hub->explore = uhub_explore;
187 	hub->nports = nports;
188 	hub->powerdelay = powerdelay;
189 
190 	if (!dev->self_powered && dev->powersrc->parent != NULL &&
191 	    !dev->powersrc->parent->self_powered) {
192 		printf("%s: bus powered hub connected to bus powered hub, "
193 		       "ignored\n", sc->sc_dev.dv_xname);
194 		goto bad;
195 	}
196 
197 	/* Set up interrupt pipe. */
198 	err = usbd_device2interface_handle(dev, 0, &iface);
199 	if (err) {
200 		printf("%s: no interface handle\n", sc->sc_dev.dv_xname);
201 		goto bad;
202 	}
203 	ed = usbd_interface2endpoint_descriptor(iface, 0);
204 	if (ed == NULL) {
205 		printf("%s: no endpoint descriptor\n", sc->sc_dev.dv_xname);
206 		goto bad;
207 	}
208 	if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
209 		printf("%s: bad interrupt endpoint\n", sc->sc_dev.dv_xname);
210 		goto bad;
211 	}
212 
213 	sc->sc_statuslen = (nports + 1 + 7) / 8;
214 	sc->sc_statusbuf = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT);
215 	if (!sc->sc_statusbuf)
216 		goto bad;
217 
218 	err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
219 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_statusbuf,
220 		  sc->sc_statuslen, uhub_intr, UHUB_INTR_INTERVAL);
221 	if (err) {
222 		printf("%s: cannot open interrupt pipe\n",
223 		       sc->sc_dev.dv_xname);
224 		goto bad;
225 	}
226 
227 	/* Wait with power off for a while. */
228 	usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
229 
230 	/*
231 	 * To have the best chance of success we do things in the exact same
232 	 * order as Windoze98.  This should not be necessary, but some
233 	 * devices do not follow the USB specs to the letter.
234 	 *
235 	 * These are the events on the bus when a hub is attached:
236 	 *  Get device and config descriptors (see attach code)
237 	 *  Get hub descriptor (see above)
238 	 *  For all ports
239 	 *     turn on power
240 	 *     wait for power to become stable
241 	 * (all below happens in explore code)
242 	 *  For all ports
243 	 *     clear C_PORT_CONNECTION
244 	 *  For all ports
245 	 *     get port status
246 	 *     if device connected
247 	 *        wait 100 ms
248 	 *        turn on reset
249 	 *        wait
250 	 *        clear C_PORT_RESET
251 	 *        get port status
252 	 *        proceed with device attachment
253 	 */
254 
255 	if (UHUB_IS_HIGH_SPEED(sc)) {
256 		tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
257 		    sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
258 		if (!tts)
259 			goto bad;
260 	}
261 	/* Set up data structures */
262 	for (p = 0; p < nports; p++) {
263 		struct usbd_port *up = &hub->ports[p];
264 		up->device = NULL;
265 		up->parent = dev;
266 		up->portno = p+1;
267 		if (dev->self_powered)
268 			/* Self powered hub, give ports maximum current. */
269 			up->power = USB_MAX_POWER;
270 		else
271 			up->power = USB_MIN_POWER;
272 		up->restartcnt = 0;
273 		up->reattach = 0;
274 		if (UHUB_IS_HIGH_SPEED(sc)) {
275 			up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
276 			up->tt->hub = hub;
277 		} else {
278 			up->tt = NULL;
279 		}
280 	}
281 
282 	for (port = 1; port <= nports; port++) {
283 		/* Turn the power on. */
284 		err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
285 		if (err)
286 			printf("%s: port %d power on failed, %s\n",
287 			       sc->sc_dev.dv_xname, port,
288 			       usbd_errstr(err));
289 	}
290 
291 	/* Wait for stable power. */
292         if (dev->powersrc->parent != NULL)
293 		usbd_delay_ms(dev, powerdelay + USB_EXTRA_POWER_UP_TIME);
294 
295 	/* The usual exploration will finish the setup. */
296 
297 	sc->sc_running = 1;
298 
299 	return;
300 
301  bad:
302 	if (sc->sc_statusbuf)
303 		free(sc->sc_statusbuf, M_USBDEV, 0);
304 	if (hub) {
305 		if (hub->ports)
306 			free(hub->ports, M_USBDEV, 0);
307 		free(hub, M_USBDEV, 0);
308 	}
309 	dev->hub = NULL;
310 }
311 
312 int
313 uhub_explore(struct usbd_device *dev)
314 {
315 	struct uhub_softc *sc = dev->hub->hubsoftc;
316 	struct usbd_port *up;
317 	usbd_status err;
318 	int speed;
319 	int port;
320 	int change, status, reconnect;
321 
322 	if (usbd_is_dying(dev))
323 		return (EIO);
324 
325 	if (!sc->sc_running)
326 		return (ENXIO);
327 
328 	/* Ignore hubs that are too deep. */
329 	if (dev->depth > USB_HUB_MAX_DEPTH)
330 		return (EOPNOTSUPP);
331 
332 	for (port = 1; port <= dev->hub->nports; port++) {
333 		up = &dev->hub->ports[port-1];
334 		err = usbd_get_port_status(dev, port, &up->status);
335 		if (err) {
336 			DPRINTF("%s: get port %d status failed, error=%s\n",
337 			    sc->sc_dev.dv_xname, port, usbd_errstr(err));
338 			continue;
339 		}
340 		status = UGETW(up->status.wPortStatus);
341 		change = UGETW(up->status.wPortChange);
342 		reconnect = up->reattach;
343 		up->reattach = 0;
344 		DPRINTF("%s: port %d status=0x%04x change=0x%04x\n",
345 		    sc->sc_dev.dv_xname, port, status, change);
346 		if (change & UPS_C_PORT_ENABLED) {
347 			usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
348 			if (change & UPS_C_CONNECT_STATUS) {
349 				/* Ignore the port error if the device
350 				   vanished. */
351 			} else if (status & UPS_PORT_ENABLED) {
352 				printf("%s: illegal enable change, port %d\n",
353 				       sc->sc_dev.dv_xname, port);
354 			} else {
355 				/* Port error condition. */
356 				if (up->restartcnt) /* no message first time */
357 					printf("%s: port error, restarting "
358 					       "port %d\n",
359 					       sc->sc_dev.dv_xname, port);
360 
361 				if (up->restartcnt++ < USBD_RESTART_MAX)
362 					goto disco;
363 				else
364 					printf("%s: port error, giving up "
365 					       "port %d\n",
366 					       sc->sc_dev.dv_xname, port);
367 			}
368 		}
369 		if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
370 			/* No status change, just do recursive explore. */
371 			if (up->device != NULL && up->device->hub != NULL)
372 				up->device->hub->explore(up->device);
373 			continue;
374 		}
375 
376 		/* We have a connect status change, handle it. */
377 		usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
378 		/*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
379 		/*
380 		 * If there is already a device on the port the change status
381 		 * must mean that is has disconnected.  Looking at the
382 		 * current connect status is not enough to figure this out
383 		 * since a new unit may have been connected before we handle
384 		 * the disconnect.
385 		 */
386 	disco:
387 		if (up->device != NULL) {
388 			/* Disconnected */
389 			usbd_detach(up->device, &sc->sc_dev);
390 			up->device = NULL;
391 			usbd_clear_port_feature(dev, port,
392 						UHF_C_PORT_CONNECTION);
393 		}
394 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
395 			/* Nothing connected, just ignore it. */
396 			continue;
397 		}
398 
399 		/* Connected */
400 		if (!(status & UPS_PORT_POWER))
401 			printf("%s: strange, connected port %d has no power\n",
402 			       sc->sc_dev.dv_xname, port);
403 
404 		/* Wait for maximum device power up time. */
405 		usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
406 
407 		/* Reset port, which implies enabling it. */
408 		if (usbd_reset_port(dev, port, &up->status)) {
409 			printf("%s: port %d reset failed\n",
410 			       sc->sc_dev.dv_xname, port);
411 			continue;
412 		}
413 		/* Get port status again, it might have changed during reset */
414 		err = usbd_get_port_status(dev, port, &up->status);
415 		if (err) {
416 			DPRINTF("%s: get port %d status failed, error=%s\n",
417 			    sc->sc_dev.dv_xname, port, usbd_errstr(err));
418 			continue;
419 		}
420 		status = UGETW(up->status.wPortStatus);
421 		change = UGETW(up->status.wPortChange);
422 		if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
423 			/* Nothing connected, just ignore it. */
424 			DPRINTF("%s: port %d, device disappeared after reset\n",
425 			    sc->sc_dev.dv_xname, port);
426 			continue;
427 		}
428 
429 		/* Figure out device speed */
430 		if (status & UPS_SUPER_SPEED)
431 			speed = USB_SPEED_SUPER;
432 		else if (status & UPS_HIGH_SPEED)
433 			speed = USB_SPEED_HIGH;
434 		else if (status & UPS_LOW_SPEED)
435 			speed = USB_SPEED_LOW;
436 		else
437 			speed = USB_SPEED_FULL;
438 		/* Get device info and set its address. */
439 		err = usbd_new_device(&sc->sc_dev, dev->bus,
440 			  dev->depth + 1, speed, port, up);
441 		/* XXX retry a few times? */
442 		if (err) {
443 			DPRINTF("%s: usbd_new_device failed, error=%s\n",
444 			    sc->sc_dev.dv_xname, usbd_errstr(err));
445 			/* Avoid addressing problems by disabling. */
446 			/* usbd_reset_port(dev, port, &up->status); */
447 
448 			/*
449 			 * The unit refused to accept a new address, or had
450 			 * some other serious problem.  Since we cannot leave
451 			 * at 0 we have to disable the port instead.
452 			 */
453 			printf("%s: device problem, disabling port %d\n",
454 			       sc->sc_dev.dv_xname, port);
455 			usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
456 		} else {
457 			/* The port set up succeeded, reset error count. */
458 			up->restartcnt = 0;
459 
460 			if (up->device->hub)
461 				up->device->hub->explore(up->device);
462 		}
463 	}
464 
465 	return (0);
466 }
467 
468 /*
469  * Called from process context when the hub is gone.
470  * Detach all devices on active ports.
471  */
472 int
473 uhub_detach(struct device *self, int flags)
474 {
475 	struct uhub_softc *sc = (struct uhub_softc *)self;
476 	struct usbd_hub *hub = sc->sc_hub->hub;
477 	struct usbd_port *rup;
478 	int port;
479 
480 	if (hub == NULL)		/* Must be partially working */
481 		return (0);
482 
483 	usbd_abort_pipe(sc->sc_ipipe);
484 	usbd_close_pipe(sc->sc_ipipe);
485 
486 	for (port = 0; port < hub->nports; port++) {
487 		rup = &hub->ports[port];
488 		if (rup->device != NULL) {
489 			usbd_detach(rup->device, self);
490 			rup->device = NULL;
491 		}
492 	}
493 
494 	if (hub->ports[0].tt)
495 		free(hub->ports[0].tt, M_USBDEV, 0);
496 	if (sc->sc_statusbuf)
497 		free(sc->sc_statusbuf, M_USBDEV, 0);
498 	if (hub->ports)
499 		free(hub->ports, M_USBDEV, 0);
500 	free(hub, M_USBDEV, 0);
501 	sc->sc_hub->hub = NULL;
502 
503 	return (0);
504 }
505 
506 /*
507  * Hub interrupt.
508  * This an indication that some port has changed status.
509  * Notify the bus event handler thread that we need
510  * to be explored again.
511  */
512 void
513 uhub_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
514 {
515 	struct uhub_softc *sc = addr;
516 
517 	if (usbd_is_dying(sc->sc_hub))
518 		return;
519 
520 	DPRINTF("%s: intr status=%d\n", sc->sc_dev.dv_xname, status);
521 	if (status == USBD_STALLED)
522 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
523 	else if (status == USBD_NORMAL_COMPLETION)
524 		usb_needs_explore(sc->sc_hub, 0);
525 }
526