uhub.c revision 1.76.2.2 1 /* $NetBSD: uhub.c,v 1.76.2.2 2006/12/30 20:49:39 yamt 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, 2004 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/docs/usbspec.zip
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.76.2.2 2006/12/30 20:49:39 yamt 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 #ifdef UHUB_DEBUG
69 #define DPRINTF(x) if (uhubdebug) logprintf x
70 #define DPRINTFN(n,x) if (uhubdebug>(n)) logprintf x
71 int uhubdebug = 0;
72 #else
73 #define DPRINTF(x)
74 #define DPRINTFN(n,x)
75 #endif
76
77 struct uhub_softc {
78 USBBASEDEVICE sc_dev; /* base device */
79 usbd_device_handle sc_hub; /* USB device */
80 usbd_pipe_handle sc_ipipe; /* interrupt pipe */
81 u_int8_t sc_status[1]; /* XXX more ports */
82 u_char sc_running;
83 };
84 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol)
85 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
86 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
87
88 Static usbd_status uhub_explore(usbd_device_handle hub);
89 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
90
91 #if defined(__FreeBSD__)
92 Static bus_child_detached_t uhub_child_detached;
93 #endif
94
95
96 /*
97 * We need two attachment points:
98 * hub to usb and hub to hub
99 * Every other driver only connects to hubs
100 */
101
102 #if defined(__NetBSD__) || defined(__OpenBSD__)
103 USB_DECLARE_DRIVER(uhub);
104 #elif defined(__FreeBSD__)
105 USB_DECLARE_DRIVER_INIT(uhub,
106 DEVMETHOD(bus_child_detached, uhub_child_detached));
107
108 /* Create the driver instance for the hub connected to usb case. */
109 devclass_t uhubroot_devclass;
110
111 Static device_method_t uhubroot_methods[] = {
112 DEVMETHOD(device_probe, uhub_match),
113 DEVMETHOD(device_attach, uhub_attach),
114
115 /* detach is not allowed for a root hub */
116 {0,0}
117 };
118
119 Static driver_t uhubroot_driver = {
120 "uhub",
121 uhubroot_methods,
122 sizeof(struct uhub_softc)
123 };
124 #endif
125
126 USB_MATCH(uhub)
127 {
128 USB_MATCH_START(uhub, uaa);
129 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
130
131 DPRINTFN(5,("uhub_match, dd=%p\n", dd));
132 /*
133 * The subclass for hubs seems to be 0 for some and 1 for others,
134 * so we just ignore the subclass.
135 */
136 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
137 return (UMATCH_DEVCLASS_DEVSUBCLASS);
138 return (UMATCH_NONE);
139 }
140
141 USB_ATTACH(uhub)
142 {
143 USB_ATTACH_START(uhub, sc, uaa);
144 usbd_device_handle dev = uaa->device;
145 char *devinfop;
146 usbd_status err;
147 struct usbd_hub *hub = NULL;
148 usb_device_request_t req;
149 usb_hub_descriptor_t hubdesc;
150 int p, port, nports, nremov, pwrdly;
151 usbd_interface_handle iface;
152 usb_endpoint_descriptor_t *ed;
153 #if 0 /* notyet */
154 struct usbd_tt *tts = NULL;
155 #endif
156
157 DPRINTFN(1,("uhub_attach\n"));
158 sc->sc_hub = dev;
159
160 devinfop = usbd_devinfo_alloc(dev, 1);
161 USB_ATTACH_SETUP;
162 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
163 usbd_devinfo_free(devinfop);
164
165 if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
166 printf("%s: %s transaction translator%s\n",
167 USBDEVNAME(sc->sc_dev),
168 UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
169 UHUB_IS_SINGLE_TT(sc) ? "" : "s");
170 }
171
172 err = usbd_set_config_index(dev, 0, 1);
173 if (err) {
174 DPRINTF(("%s: configuration failed, error=%s\n",
175 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
176 USB_ATTACH_ERROR_RETURN;
177 }
178
179 if (dev->depth > USB_HUB_MAX_DEPTH) {
180 printf("%s: hub depth (%d) exceeded, hub ignored\n",
181 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
182 USB_ATTACH_ERROR_RETURN;
183 }
184
185 /* Get hub descriptor. */
186 req.bmRequestType = UT_READ_CLASS_DEVICE;
187 req.bRequest = UR_GET_DESCRIPTOR;
188 USETW2(req.wValue, UDESC_HUB, 0);
189 USETW(req.wIndex, 0);
190 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
191 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
192 err = usbd_do_request(dev, &req, &hubdesc);
193 nports = hubdesc.bNbrPorts;
194 if (!err && nports > 7) {
195 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
196 err = usbd_do_request(dev, &req, &hubdesc);
197 }
198 if (err) {
199 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
200 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
201 USB_ATTACH_ERROR_RETURN;
202 }
203
204 for (nremov = 0, port = 1; port <= nports; port++)
205 if (!UHD_NOT_REMOV(&hubdesc, port))
206 nremov++;
207 printf("%s: %d port%s with %d removable, %s powered\n",
208 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
209 nremov, dev->self_powered ? "self" : "bus");
210
211 if (nports == 0) {
212 printf("%s: no ports, hub ignored\n", USBDEVNAME(sc->sc_dev));
213 goto bad;
214 }
215
216 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
217 M_USBDEV, M_NOWAIT);
218 if (hub == NULL)
219 USB_ATTACH_ERROR_RETURN;
220 dev->hub = hub;
221 dev->hub->hubsoftc = sc;
222 hub->explore = uhub_explore;
223 hub->hubdesc = hubdesc;
224
225 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
226 "parent->selfpowered=%d\n",
227 dev->self_powered, dev->powersrc->parent,
228 dev->powersrc->parent ?
229 dev->powersrc->parent->self_powered : 0));
230
231 if (!dev->self_powered && dev->powersrc->parent != NULL &&
232 !dev->powersrc->parent->self_powered) {
233 printf("%s: bus powered hub connected to bus powered hub, "
234 "ignored\n", USBDEVNAME(sc->sc_dev));
235 goto bad;
236 }
237
238 /* Set up interrupt pipe. */
239 err = usbd_device2interface_handle(dev, 0, &iface);
240 if (err) {
241 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
242 goto bad;
243 }
244
245 if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
246 err = usbd_set_interface(iface, 1);
247 if (err)
248 printf("%s: can't enable multiple TTs\n",
249 USBDEVNAME(sc->sc_dev));
250 }
251
252 ed = usbd_interface2endpoint_descriptor(iface, 0);
253 if (ed == NULL) {
254 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
255 goto bad;
256 }
257 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
258 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
259 goto bad;
260 }
261
262 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
263 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
264 sizeof(sc->sc_status), uhub_intr, USBD_DEFAULT_INTERVAL);
265 if (err) {
266 printf("%s: cannot open interrupt pipe\n",
267 USBDEVNAME(sc->sc_dev));
268 goto bad;
269 }
270
271 /* Wait with power off for a while. */
272 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
273
274 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
275
276 /*
277 * To have the best chance of success we do things in the exact same
278 * order as Windoze98. This should not be necessary, but some
279 * devices do not follow the USB specs to the letter.
280 *
281 * These are the events on the bus when a hub is attached:
282 * Get device and config descriptors (see attach code)
283 * Get hub descriptor (see above)
284 * For all ports
285 * turn on power
286 * wait for power to become stable
287 * (all below happens in explore code)
288 * For all ports
289 * clear C_PORT_CONNECTION
290 * For all ports
291 * get port status
292 * if device connected
293 * wait 100 ms
294 * turn on reset
295 * wait
296 * clear C_PORT_RESET
297 * get port status
298 * proceed with device attachment
299 */
300
301 #if 0
302 if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) {
303 tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
304 sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
305 if (!tts)
306 goto bad;
307 }
308 #endif
309 /* Set up data structures */
310 for (p = 0; p < nports; p++) {
311 struct usbd_port *up = &hub->ports[p];
312 up->device = NULL;
313 up->parent = dev;
314 up->portno = p+1;
315 if (dev->self_powered)
316 /* Self powered hub, give ports maximum current. */
317 up->power = USB_MAX_POWER;
318 else
319 up->power = USB_MIN_POWER;
320 up->restartcnt = 0;
321 up->reattach = 0;
322 #if 0
323 if (UHUB_IS_HIGH_SPEED(sc)) {
324 up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
325 up->tt->hub = hub;
326 } else {
327 up->tt = NULL;
328 }
329 #endif
330 }
331
332 /* XXX should check for none, individual, or ganged power? */
333
334 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
335 + USB_EXTRA_POWER_UP_TIME;
336 for (port = 1; port <= nports; port++) {
337 /* Turn the power on. */
338 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
339 if (err)
340 printf("%s: port %d power on failed, %s\n",
341 USBDEVNAME(sc->sc_dev), port,
342 usbd_errstr(err));
343 DPRINTF(("usb_init_port: turn on port %d power\n", port));
344 /* Wait for stable power. */
345 usbd_delay_ms(dev, pwrdly);
346 }
347
348 /* The usual exploration will finish the setup. */
349
350 sc->sc_running = 1;
351
352 USB_ATTACH_SUCCESS_RETURN;
353
354 bad:
355 if (hub)
356 free(hub, M_USBDEV);
357 dev->hub = NULL;
358 USB_ATTACH_ERROR_RETURN;
359 }
360
361 usbd_status
362 uhub_explore(usbd_device_handle dev)
363 {
364 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
365 struct uhub_softc *sc = dev->hub->hubsoftc;
366 struct usbd_port *up;
367 usbd_status err;
368 int speed;
369 int port;
370 int change, status, reconnect;
371
372 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
373
374 if (!sc->sc_running)
375 return (USBD_NOT_STARTED);
376
377 /* Ignore hubs that are too deep. */
378 if (dev->depth > USB_HUB_MAX_DEPTH)
379 return (USBD_TOO_DEEP);
380
381 for(port = 1; port <= hd->bNbrPorts; port++) {
382 up = &dev->hub->ports[port-1];
383 err = usbd_get_port_status(dev, port, &up->status);
384 if (err) {
385 DPRINTF(("uhub_explore: get port status failed, "
386 "error=%s\n", usbd_errstr(err)));
387 continue;
388 }
389 status = UGETW(up->status.wPortStatus);
390 change = UGETW(up->status.wPortChange);
391 reconnect = up->reattach;
392 up->reattach = 0;
393 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
394 USBDEVNAME(sc->sc_dev), port, status, change));
395 if (change & UPS_C_PORT_ENABLED) {
396 DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
397 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
398 if (change & UPS_C_CONNECT_STATUS) {
399 /* Ignore the port error if the device
400 vanished. */
401 } else if (status & UPS_PORT_ENABLED) {
402 printf("%s: illegal enable change, port %d\n",
403 USBDEVNAME(sc->sc_dev), port);
404 } else {
405 /* Port error condition. */
406 if (up->restartcnt) /* no message first time */
407 printf("%s: port error, restarting "
408 "port %d\n",
409 USBDEVNAME(sc->sc_dev), port);
410
411 if (up->restartcnt++ < USBD_RESTART_MAX)
412 goto disco;
413 else
414 printf("%s: port error, giving up "
415 "port %d\n",
416 USBDEVNAME(sc->sc_dev), port);
417 }
418 }
419 if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
420 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
421 "STATUS\n", port));
422 /* No status change, just do recursive explore. */
423 if (up->device != NULL && up->device->hub != NULL)
424 up->device->hub->explore(up->device);
425 #if 0 && defined(DIAGNOSTIC)
426 if (up->device == NULL &&
427 (status & UPS_CURRENT_CONNECT_STATUS))
428 printf("%s: connected, no device\n",
429 USBDEVNAME(sc->sc_dev));
430 #endif
431 continue;
432 }
433
434 /* We have a connect status change, handle it. */
435
436 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
437 dev->address, port));
438 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
439 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
440 /*
441 * If there is already a device on the port the change status
442 * must mean that is has disconnected. Looking at the
443 * current connect status is not enough to figure this out
444 * since a new unit may have been connected before we handle
445 * the disconnect.
446 */
447 disco:
448 if (up->device != NULL) {
449 /* Disconnected */
450 DPRINTF(("uhub_explore: device addr=%d disappeared "
451 "on port %d\n", up->device->address, port));
452 usb_disconnect_port(up, USBDEV(sc->sc_dev));
453 usbd_clear_port_feature(dev, port,
454 UHF_C_PORT_CONNECTION);
455 }
456 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
457 /* Nothing connected, just ignore it. */
458 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
459 "_STATUS\n", port));
460 continue;
461 }
462
463 /* Connected */
464
465 if (!(status & UPS_PORT_POWER))
466 printf("%s: strange, connected port %d has no power\n",
467 USBDEVNAME(sc->sc_dev), port);
468
469 /* Wait for maximum device power up time. */
470 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
471
472 /* Reset port, which implies enabling it. */
473 if (usbd_reset_port(dev, port, &up->status)) {
474 printf("%s: port %d reset failed\n",
475 USBDEVNAME(sc->sc_dev), port);
476 continue;
477 }
478 /* Get port status again, it might have changed during reset */
479 err = usbd_get_port_status(dev, port, &up->status);
480 if (err) {
481 DPRINTF(("uhub_explore: get port status failed, "
482 "error=%s\n", usbd_errstr(err)));
483 continue;
484 }
485 status = UGETW(up->status.wPortStatus);
486 change = UGETW(up->status.wPortChange);
487 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
488 /* Nothing connected, just ignore it. */
489 #ifdef DIAGNOSTIC
490 printf("%s: port %d, device disappeared after reset\n",
491 USBDEVNAME(sc->sc_dev), port);
492 #endif
493 continue;
494 }
495
496 #if 0
497 if (UHUB_IS_HIGH_SPEED(sc) && !(status & UPS_HIGH_SPEED)) {
498 printf("%s: port %d, transaction translation not "
499 "implemented, low/full speed device ignored\n",
500 USBDEVNAME(sc->sc_dev), port);
501 continue;
502 }
503 #endif
504
505 /* Figure out device speed */
506 if (status & UPS_HIGH_SPEED)
507 speed = USB_SPEED_HIGH;
508 else if (status & UPS_LOW_SPEED)
509 speed = USB_SPEED_LOW;
510 else
511 speed = USB_SPEED_FULL;
512 /* Get device info and set its address. */
513 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
514 dev->depth + 1, speed, port, up);
515 /* XXX retry a few times? */
516 if (err) {
517 DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
518 "error=%s\n", usbd_errstr(err)));
519 /* Avoid addressing problems by disabling. */
520 /* usbd_reset_port(dev, port, &up->status); */
521
522 /*
523 * The unit refused to accept a new address, or had
524 * some other serious problem. Since we cannot leave
525 * at 0 we have to disable the port instead.
526 */
527 printf("%s: device problem, disabling port %d\n",
528 USBDEVNAME(sc->sc_dev), port);
529 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
530 } else {
531 /* The port set up succeeded, reset error count. */
532 up->restartcnt = 0;
533
534 if (up->device->hub)
535 up->device->hub->explore(up->device);
536 }
537 }
538 return (USBD_NORMAL_COMPLETION);
539 }
540
541 #if defined(__NetBSD__) || defined(__OpenBSD__)
542 int
543 uhub_activate(device_ptr_t self, enum devact act)
544 {
545 struct uhub_softc *sc = (struct uhub_softc *)self;
546 struct usbd_hub *hub = sc->sc_hub->hub;
547 usbd_device_handle dev;
548 int nports, port, i;
549
550 switch (act) {
551 case DVACT_ACTIVATE:
552 return (EOPNOTSUPP);
553
554 case DVACT_DEACTIVATE:
555 if (hub == NULL) /* malfunctioning hub */
556 break;
557 nports = hub->hubdesc.bNbrPorts;
558 for(port = 0; port < nports; port++) {
559 dev = hub->ports[port].device;
560 if (dev != NULL && dev->subdevs != NULL) {
561 for (i = 0; dev->subdevs[i] != NULL; i++)
562 config_deactivate(dev->subdevs[i]);
563 }
564 }
565 break;
566 }
567 return (0);
568 }
569 #endif
570
571 /*
572 * Called from process context when the hub is gone.
573 * Detach all devices on active ports.
574 */
575 USB_DETACH(uhub)
576 {
577 USB_DETACH_START(uhub, sc);
578 struct usbd_hub *hub = sc->sc_hub->hub;
579 struct usbd_port *rup;
580 int port, nports;
581
582 #if defined(__NetBSD__) || defined(__OpenBSD__)
583 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
584 #elif defined(__FreeBSD__)
585 DPRINTF(("uhub_detach: sc=%port\n", sc));
586 #endif
587
588 if (hub == NULL) /* Must be partially working */
589 return (0);
590
591 usbd_abort_pipe(sc->sc_ipipe);
592 usbd_close_pipe(sc->sc_ipipe);
593
594 nports = hub->hubdesc.bNbrPorts;
595 for(port = 0; port < nports; port++) {
596 rup = &hub->ports[port];
597 if (rup->device)
598 usb_disconnect_port(rup, self);
599 }
600
601 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
602 USBDEV(sc->sc_dev));
603
604 #if 0
605 if (hub->ports[0].tt)
606 free(hub->ports[0].tt, M_USBDEV);
607 #endif
608 free(hub, M_USBDEV);
609 sc->sc_hub->hub = NULL;
610
611 return (0);
612 }
613
614 #if defined(__FreeBSD__)
615 /* Called when a device has been detached from it */
616 Static void
617 uhub_child_detached(device_t self, device_t child)
618 {
619 struct uhub_softc *sc = device_get_softc(self);
620 usbd_device_handle devhub = sc->sc_hub;
621 usbd_device_handle dev;
622 int nports;
623 int port;
624 int i;
625
626 if (!devhub->hub)
627 /* should never happen; children are only created after init */
628 panic("hub not fully initialised, but child deleted?");
629
630 nports = devhub->hub->hubdesc.bNbrPorts;
631 for (port = 0; port < nports; port++) {
632 dev = devhub->hub->ports[port].device;
633 if (dev && dev->subdevs) {
634 for (i = 0; dev->subdevs[i]; i++) {
635 if (dev->subdevs[i] == child) {
636 dev->subdevs[i] = NULL;
637 return;
638 }
639 }
640 }
641 }
642 }
643 #endif
644
645
646 /*
647 * Hub interrupt.
648 * This an indication that some port has changed status.
649 * Notify the bus event handler thread that we need
650 * to be explored again.
651 */
652 void
653 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr,
654 usbd_status status)
655 {
656 struct uhub_softc *sc = addr;
657
658 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
659 if (status == USBD_STALLED)
660 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
661 else if (status == USBD_NORMAL_COMPLETION)
662 usb_needs_explore(sc->sc_hub);
663 }
664
665 #if defined(__FreeBSD__)
666 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
667 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
668 #endif
669