uhub.c revision 1.57.10.1 1 /* $NetBSD: uhub.c,v 1.57.10.1 2004/03/15 04:37:39 jmc Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma Exp $ */
3
4 /*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart (at) augustsson.net) at
10 * Carlstedt Research & Technology.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 /*
42 * USB spec: http://www.usb.org/developers/data/usbspec.zip
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.57.10.1 2004/03/15 04:37:39 jmc Exp $");
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #if defined(__NetBSD__) || defined(__OpenBSD__)
53 #include <sys/device.h>
54 #include <sys/proc.h>
55 #elif defined(__FreeBSD__)
56 #include <sys/module.h>
57 #include <sys/bus.h>
58 #include "bus_if.h"
59 #endif
60
61 #include <machine/bus.h>
62
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usbdi_util.h>
66 #include <dev/usb/usbdivar.h>
67
68 #define UHUB_INTR_INTERVAL 255 /* ms */
69
70 #ifdef UHUB_DEBUG
71 #define DPRINTF(x) if (uhubdebug) logprintf x
72 #define DPRINTFN(n,x) if (uhubdebug>(n)) logprintf x
73 int uhubdebug;
74 #else
75 #define DPRINTF(x)
76 #define DPRINTFN(n,x)
77 #endif
78
79 struct uhub_softc {
80 USBBASEDEVICE sc_dev; /* base device */
81 usbd_device_handle sc_hub; /* USB device */
82 usbd_pipe_handle sc_ipipe; /* interrupt pipe */
83 u_int8_t sc_status[1]; /* XXX more ports */
84 u_char sc_running;
85 };
86
87 Static usbd_status uhub_explore(usbd_device_handle hub);
88 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
89
90 #if defined(__FreeBSD__)
91 Static bus_child_detached_t uhub_child_detached;
92 #endif
93
94
95 /*
96 * We need two attachment points:
97 * hub to usb and hub to hub
98 * Every other driver only connects to hubs
99 */
100
101 #if defined(__NetBSD__) || defined(__OpenBSD__)
102 USB_DECLARE_DRIVER(uhub);
103
104 /* Create the driver instance for the hub connected to hub case */
105 struct cfattach uhub_uhub_ca = {
106 sizeof(struct uhub_softc), uhub_match, uhub_attach,
107 uhub_detach, uhub_activate
108 };
109 #elif defined(__FreeBSD__)
110 USB_DECLARE_DRIVER_INIT(uhub,
111 DEVMETHOD(bus_child_detached, uhub_child_detached));
112
113 /* Create the driver instance for the hub connected to usb case. */
114 devclass_t uhubroot_devclass;
115
116 Static device_method_t uhubroot_methods[] = {
117 DEVMETHOD(device_probe, uhub_match),
118 DEVMETHOD(device_attach, uhub_attach),
119
120 /* detach is not allowed for a root hub */
121 {0,0}
122 };
123
124 Static driver_t uhubroot_driver = {
125 "uhub",
126 uhubroot_methods,
127 sizeof(struct uhub_softc)
128 };
129 #endif
130
131 USB_MATCH(uhub)
132 {
133 USB_MATCH_START(uhub, uaa);
134 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
135
136 DPRINTFN(5,("uhub_match, dd=%p\n", dd));
137 /*
138 * The subclass for hubs seems to be 0 for some and 1 for others,
139 * so we just ignore the subclass.
140 */
141 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
142 return (UMATCH_DEVCLASS_DEVSUBCLASS);
143 return (UMATCH_NONE);
144 }
145
146 USB_ATTACH(uhub)
147 {
148 USB_ATTACH_START(uhub, sc, uaa);
149 usbd_device_handle dev = uaa->device;
150 char devinfo[1024];
151 usbd_status err;
152 struct usbd_hub *hub;
153 usb_device_request_t req;
154 usb_hub_descriptor_t hubdesc;
155 int p, port, nports, nremov, pwrdly;
156 usbd_interface_handle iface;
157 usb_endpoint_descriptor_t *ed;
158
159 DPRINTFN(1,("uhub_attach\n"));
160 sc->sc_hub = dev;
161 usbd_devinfo(dev, 1, devinfo);
162 USB_ATTACH_SETUP;
163 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
164
165 err = usbd_set_config_index(dev, 0, 1);
166 if (err) {
167 DPRINTF(("%s: configuration failed, error=%s\n",
168 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
169 USB_ATTACH_ERROR_RETURN;
170 }
171
172 if (dev->depth > USB_HUB_MAX_DEPTH) {
173 printf("%s: hub depth (%d) exceeded, hub ignored\n",
174 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
175 USB_ATTACH_ERROR_RETURN;
176 }
177
178 /* Get hub descriptor. */
179 req.bmRequestType = UT_READ_CLASS_DEVICE;
180 req.bRequest = UR_GET_DESCRIPTOR;
181 USETW2(req.wValue, UDESC_HUB, 0);
182 USETW(req.wIndex, 0);
183 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
184 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
185 err = usbd_do_request(dev, &req, &hubdesc);
186 nports = hubdesc.bNbrPorts;
187 if (!err && nports > 7) {
188 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
189 err = usbd_do_request(dev, &req, &hubdesc);
190 }
191 if (err) {
192 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
193 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
194 USB_ATTACH_ERROR_RETURN;
195 }
196
197 for (nremov = 0, port = 1; port <= nports; port++)
198 if (!UHD_NOT_REMOV(&hubdesc, port))
199 nremov++;
200 printf("%s: %d port%s with %d removable, %s powered\n",
201 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
202 nremov, dev->self_powered ? "self" : "bus");
203
204 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
205 M_USBDEV, M_NOWAIT);
206 if (hub == NULL)
207 USB_ATTACH_ERROR_RETURN;
208 dev->hub = hub;
209 dev->hub->hubsoftc = sc;
210 hub->explore = uhub_explore;
211 hub->hubdesc = hubdesc;
212
213 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
214 "parent->selfpowered=%d\n",
215 dev->self_powered, dev->powersrc->parent,
216 dev->powersrc->parent ?
217 dev->powersrc->parent->self_powered : 0));
218
219 if (!dev->self_powered && dev->powersrc->parent != NULL &&
220 !dev->powersrc->parent->self_powered) {
221 printf("%s: bus powered hub connected to bus powered hub, "
222 "ignored\n", USBDEVNAME(sc->sc_dev));
223 goto bad;
224 }
225
226 /* Set up interrupt pipe. */
227 err = usbd_device2interface_handle(dev, 0, &iface);
228 if (err) {
229 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
230 goto bad;
231 }
232 ed = usbd_interface2endpoint_descriptor(iface, 0);
233 if (ed == NULL) {
234 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
235 goto bad;
236 }
237 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
238 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
239 goto bad;
240 }
241
242 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
243 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
244 sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
245 if (err) {
246 printf("%s: cannot open interrupt pipe\n",
247 USBDEVNAME(sc->sc_dev));
248 goto bad;
249 }
250
251 /* Wait with power off for a while. */
252 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
253
254 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
255
256 /*
257 * To have the best chance of success we do things in the exact same
258 * order as Windoze98. This should not be necessary, but some
259 * devices do not follow the USB specs to the letter.
260 *
261 * These are the events on the bus when a hub is attached:
262 * Get device and config descriptors (see attach code)
263 * Get hub descriptor (see above)
264 * For all ports
265 * turn on power
266 * wait for power to become stable
267 * (all below happens in explore code)
268 * For all ports
269 * clear C_PORT_CONNECTION
270 * For all ports
271 * get port status
272 * if device connected
273 * wait 100 ms
274 * turn on reset
275 * wait
276 * clear C_PORT_RESET
277 * get port status
278 * proceed with device attachment
279 */
280
281 /* Set up data structures */
282 for (p = 0; p < nports; p++) {
283 struct usbd_port *up = &hub->ports[p];
284 up->device = 0;
285 up->parent = dev;
286 up->portno = p+1;
287 if (dev->self_powered)
288 /* Self powered hub, give ports maximum current. */
289 up->power = USB_MAX_POWER;
290 else
291 up->power = USB_MIN_POWER;
292 }
293
294 /* XXX should check for none, individual, or ganged power? */
295
296 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
297 + USB_EXTRA_POWER_UP_TIME;
298 for (port = 1; port <= nports; port++) {
299 /* Turn the power on. */
300 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
301 if (err)
302 printf("%s: port %d power on failed, %s\n",
303 USBDEVNAME(sc->sc_dev), port,
304 usbd_errstr(err));
305 DPRINTF(("usb_init_port: turn on port %d power\n", port));
306 /* Wait for stable power. */
307 usbd_delay_ms(dev, pwrdly);
308 }
309
310 /* The usual exploration will finish the setup. */
311
312 sc->sc_running = 1;
313
314 USB_ATTACH_SUCCESS_RETURN;
315
316 bad:
317 free(hub, M_USBDEV);
318 dev->hub = 0;
319 USB_ATTACH_ERROR_RETURN;
320 }
321
322 usbd_status
323 uhub_explore(usbd_device_handle dev)
324 {
325 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
326 struct uhub_softc *sc = dev->hub->hubsoftc;
327 struct usbd_port *up;
328 usbd_status err;
329 int speed;
330 int port;
331 int change, status;
332
333 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
334
335 if (!sc->sc_running)
336 return (USBD_NOT_STARTED);
337
338 /* Ignore hubs that are too deep. */
339 if (dev->depth > USB_HUB_MAX_DEPTH)
340 return (USBD_TOO_DEEP);
341
342 for(port = 1; port <= hd->bNbrPorts; port++) {
343 up = &dev->hub->ports[port-1];
344 err = usbd_get_port_status(dev, port, &up->status);
345 if (err) {
346 DPRINTF(("uhub_explore: get port status failed, "
347 "error=%s\n", usbd_errstr(err)));
348 continue;
349 }
350 status = UGETW(up->status.wPortStatus);
351 change = UGETW(up->status.wPortChange);
352 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
353 USBDEVNAME(sc->sc_dev), port, status, change));
354 if (change & UPS_C_PORT_ENABLED) {
355 DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
356 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
357 if (status & UPS_PORT_ENABLED) {
358 printf("%s: illegal enable change, port %d\n",
359 USBDEVNAME(sc->sc_dev), port);
360 } else {
361 /* Port error condition. */
362 if (up->restartcnt) /* no message first time */
363 printf("%s: port error, restarting "
364 "port %d\n",
365 USBDEVNAME(sc->sc_dev), port);
366
367 if (up->restartcnt++ < USBD_RESTART_MAX)
368 goto disco;
369 else
370 printf("%s: port error, giving up "
371 "port %d\n",
372 USBDEVNAME(sc->sc_dev), port);
373 }
374 }
375 if (!(change & UPS_C_CONNECT_STATUS)) {
376 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
377 "STATUS\n", port));
378 /* No status change, just do recursive explore. */
379 if (up->device != NULL && up->device->hub != NULL)
380 up->device->hub->explore(up->device);
381 #if 0 && defined(DIAGNOSTIC)
382 if (up->device == NULL &&
383 (status & UPS_CURRENT_CONNECT_STATUS))
384 printf("%s: connected, no device\n",
385 USBDEVNAME(sc->sc_dev));
386 #endif
387 continue;
388 }
389
390 /* We have a connect status change, handle it. */
391
392 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
393 dev->address, port));
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 DPRINTF(("uhub_explore: device addr=%d disappeared "
407 "on port %d\n", up->device->address, port));
408 usb_disconnect_port(up, USBDEV(sc->sc_dev));
409 usbd_clear_port_feature(dev, port,
410 UHF_C_PORT_CONNECTION);
411 }
412 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
413 /* Nothing connected, just ignore it. */
414 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
415 "_STATUS\n", port));
416 continue;
417 }
418
419 /* Connected */
420
421 if (!(status & UPS_PORT_POWER))
422 printf("%s: strange, connected port %d has no power\n",
423 USBDEVNAME(sc->sc_dev), port);
424
425 /* Wait for maximum device power up time. */
426 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
427
428 /* Reset port, which implies enabling it. */
429 if (usbd_reset_port(dev, port, &up->status)) {
430 printf("%s: port %d reset failed\n",
431 USBDEVNAME(sc->sc_dev), port);
432 continue;
433 }
434 /* Get port status again, it might have changed during reset */
435 err = usbd_get_port_status(dev, port, &up->status);
436 if (err) {
437 DPRINTF(("uhub_explore: get port status failed, "
438 "error=%s\n", usbd_errstr(err)));
439 continue;
440 }
441 status = UGETW(up->status.wPortStatus);
442 change = UGETW(up->status.wPortChange);
443 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
444 /* Nothing connected, just ignore it. */
445 #ifdef DIAGNOSTIC
446 printf("%s: port %d, device disappeared after reset\n",
447 USBDEVNAME(sc->sc_dev), port);
448 #endif
449 continue;
450 }
451
452 /* Figure out device speed */
453 if (status & UPS_HIGH_SPEED)
454 speed = USB_SPEED_HIGH;
455 else if (status & UPS_LOW_SPEED)
456 speed = USB_SPEED_LOW;
457 else
458 speed = USB_SPEED_FULL;
459 /* Get device info and set its address. */
460 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
461 dev->depth + 1, speed, port, up);
462 /* XXX retry a few times? */
463 if (err) {
464 DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
465 "error=%s\n", usbd_errstr(err)));
466 /* Avoid addressing problems by disabling. */
467 /* usbd_reset_port(dev, port, &up->status); */
468
469 /*
470 * The unit refused to accept a new address, or had
471 * some other serious problem. Since we cannot leave
472 * at 0 we have to disable the port instead.
473 */
474 printf("%s: device problem, disabling port %d\n",
475 USBDEVNAME(sc->sc_dev), port);
476 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
477 } else {
478 /* The port set up succeeded, reset error count. */
479 up->restartcnt = 0;
480
481 if (up->device->hub)
482 up->device->hub->explore(up->device);
483 }
484 }
485 return (USBD_NORMAL_COMPLETION);
486 }
487
488 #if defined(__NetBSD__) || defined(__OpenBSD__)
489 int
490 uhub_activate(device_ptr_t self, enum devact act)
491 {
492 struct uhub_softc *sc = (struct uhub_softc *)self;
493 struct usbd_hub *hub = sc->sc_hub->hub;
494 usbd_device_handle dev;
495 int nports, port, i;
496
497 switch (act) {
498 case DVACT_ACTIVATE:
499 return (EOPNOTSUPP);
500 break;
501
502 case DVACT_DEACTIVATE:
503 if (hub == NULL) /* malfunctioning hub */
504 break;
505 nports = hub->hubdesc.bNbrPorts;
506 for(port = 0; port < nports; port++) {
507 dev = hub->ports[port].device;
508 if (dev != NULL && dev->subdevs != NULL) {
509 for (i = 0; dev->subdevs[i] != NULL; i++)
510 config_deactivate(dev->subdevs[i]);
511 }
512 }
513 break;
514 }
515 return (0);
516 }
517 #endif
518
519 /*
520 * Called from process context when the hub is gone.
521 * Detach all devices on active ports.
522 */
523 USB_DETACH(uhub)
524 {
525 USB_DETACH_START(uhub, sc);
526 struct usbd_hub *hub = sc->sc_hub->hub;
527 struct usbd_port *rup;
528 int port, nports;
529
530 #if defined(__NetBSD__) || defined(__OpenBSD__)
531 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
532 #elif defined(__FreeBSD__)
533 DPRINTF(("uhub_detach: sc=%port\n", sc));
534 #endif
535
536 if (hub == NULL) /* Must be partially working */
537 return (0);
538
539 usbd_abort_pipe(sc->sc_ipipe);
540 usbd_close_pipe(sc->sc_ipipe);
541
542 nports = hub->hubdesc.bNbrPorts;
543 for(port = 0; port < nports; port++) {
544 rup = &hub->ports[port];
545 if (rup->device)
546 usb_disconnect_port(rup, self);
547 }
548
549 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
550 USBDEV(sc->sc_dev));
551
552 free(hub, M_USBDEV);
553 sc->sc_hub->hub = NULL;
554
555 return (0);
556 }
557
558 #if defined(__FreeBSD__)
559 /* Called when a device has been detached from it */
560 Static void
561 uhub_child_detached(device_t self, device_t child)
562 {
563 struct uhub_softc *sc = device_get_softc(self);
564 usbd_device_handle devhub = sc->sc_hub;
565 usbd_device_handle dev;
566 int nports;
567 int port;
568 int i;
569
570 if (!devhub->hub)
571 /* should never happen; children are only created after init */
572 panic("hub not fully initialised, but child deleted?");
573
574 nports = devhub->hub->hubdesc.bNbrPorts;
575 for (port = 0; port < nports; port++) {
576 dev = devhub->hub->ports[port].device;
577 if (dev && dev->subdevs) {
578 for (i = 0; dev->subdevs[i]; i++) {
579 if (dev->subdevs[i] == child) {
580 dev->subdevs[i] = NULL;
581 return;
582 }
583 }
584 }
585 }
586 }
587 #endif
588
589
590 /*
591 * Hub interrupt.
592 * This an indication that some port has changed status.
593 * Notify the bus event handler thread that we need
594 * to be explored again.
595 */
596 void
597 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
598 {
599 struct uhub_softc *sc = addr;
600
601 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
602 if (status == USBD_STALLED)
603 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
604 else if (status == USBD_NORMAL_COMPLETION)
605 usb_needs_explore(sc->sc_hub);
606 }
607
608 #if defined(__FreeBSD__)
609 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
610 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
611 #endif
612