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