Merge from vendor branch BZIP:
[dragonfly.git] / sys / bus / usb / uhub.c
1 /*      $NetBSD: uhub.c,v 1.68 2004/06/29 06:30:05 mycroft Exp $        */
2 /*      $FreeBSD: src/sys/dev/usb/uhub.c,v 1.69.2.1 2005/12/18 15:51:31 iedowse Exp $   */
3 /*      $DragonFly: src/sys/bus/usb/uhub.c,v 1.20 2008/01/25 08:49:47 hasso 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  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41
42 /*
43  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
44  */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/module.h>
51 #include <sys/bus.h>
52 #include <sys/lock.h>
53 #include <sys/thread2.h>
54 #include <sys/sysctl.h>
55
56 #include <bus/usb/usb.h>
57 #include <bus/usb/usbdi.h>
58 #include <bus/usb/usbdi_util.h>
59 #include <bus/usb/usbdivar.h>
60
61 #define UHUB_INTR_INTERVAL 255  /* ms */
62
63 #ifdef USB_DEBUG
64 #define DPRINTF(x)      if (uhubdebug) kprintf x
65 #define DPRINTFN(n,x)   if (uhubdebug>(n)) kprintf x
66 int     uhubdebug = 0;
67 SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB uhub");
68 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW,
69            &uhubdebug, 0, "uhub debug level");
70 #else
71 #define DPRINTF(x)
72 #define DPRINTFN(n,x)
73 #endif
74
75 struct uhub_softc {
76         device_t                sc_dev;         /* base device */
77         usbd_device_handle      sc_hub;         /* USB device */
78         usbd_pipe_handle        sc_ipipe;       /* interrupt pipe */
79         u_int8_t                sc_status[1];   /* XXX more ports */
80         u_char                  sc_running;
81 };
82 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol)
83 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
84 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
85
86 static usbd_status uhub_explore(usbd_device_handle hub);
87 static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
88
89 static bus_child_detached_t uhub_child_detached;
90 static bus_child_location_str_t uhub_child_location_str;
91 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_str;
92
93 /*
94  * We need two attachment points:
95  * hub to usb and hub to hub
96  * Every other driver only connects to hubs
97  */
98
99 static device_probe_t uhub_match;
100 static device_attach_t uhub_attach;
101 static device_detach_t uhub_detach;
102
103 static devclass_t uhub_devclass;
104
105 static kobj_method_t uhub_methods[] = {
106         DEVMETHOD(device_probe, uhub_match),
107         DEVMETHOD(device_attach, uhub_attach),
108         DEVMETHOD(device_detach, uhub_detach),
109         DEVMETHOD(bus_child_detached, uhub_child_detached),
110         DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
111         DEVMETHOD(bus_child_location_str, uhub_child_location_str),
112         DEVMETHOD(bus_driver_added, bus_generic_driver_added),
113         DEVMETHOD(device_suspend, bus_generic_suspend),
114         DEVMETHOD(device_resume, bus_generic_resume),
115         DEVMETHOD(device_shutdown, bus_generic_shutdown),
116         {0,0}
117 };
118
119 static driver_t uhub_driver = {
120         "uhub",
121         uhub_methods,
122         sizeof(struct uhub_softc)
123 };
124
125 MODULE_DEPEND(uhub, usb, 1, 1, 1);
126
127 /* Create the driver instance for the hub connected to usb case. */
128 devclass_t uhubroot_devclass;
129
130 static device_method_t uhubroot_methods[] = {
131         DEVMETHOD(bus_child_detached, uhub_child_detached),
132         DEVMETHOD(bus_child_location_str, uhub_child_location_str),
133         DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
134         DEVMETHOD(bus_driver_added, bus_generic_driver_added),
135
136         DEVMETHOD(device_probe, uhub_match),
137         DEVMETHOD(device_attach, uhub_attach),
138         DEVMETHOD(device_detach, uhub_detach),
139         DEVMETHOD(device_suspend, bus_generic_suspend),
140         DEVMETHOD(device_resume, bus_generic_resume),
141         DEVMETHOD(device_shutdown, bus_generic_shutdown),
142         {0,0}
143 };
144
145 static  driver_t uhubroot_driver = {
146         "uhub",
147         uhubroot_methods,
148         sizeof(struct uhub_softc)
149 };
150
151 static int
152 uhub_match(device_t self)
153 {
154         struct usb_attach_arg *uaa = device_get_ivars(self);
155         usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
156
157         DPRINTFN(5,("uhub_match, dd=%p\n", dd));
158         /*
159          * The subclass for hubs seems to be 0 for some and 1 for others,
160          * so we just ignore the subclass.
161          */
162         if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
163                 return (UMATCH_DEVCLASS_DEVSUBCLASS);
164         return (UMATCH_NONE);
165 }
166
167 static int
168 uhub_attach(device_t self)
169 {
170         struct uhub_softc *sc = device_get_softc(self);
171         struct usb_attach_arg *uaa = device_get_ivars(self);
172         usbd_device_handle dev = uaa->device;
173         usbd_status err;
174         struct usbd_hub *hub = NULL;
175         usb_device_request_t req;
176         usb_hub_descriptor_t hubdesc;
177         int p, port, nports, nremov, pwrdly;
178         usbd_interface_handle iface;
179         usb_endpoint_descriptor_t *ed;
180         struct usbd_tt *tts = NULL;
181
182         DPRINTFN(1,("uhub_attach\n"));
183         sc->sc_hub = dev;
184         sc->sc_dev = self;
185
186         if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
187                 kprintf("%s: %s transaction translator%s\n",
188                     device_get_nameunit(sc->sc_dev),
189                     UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
190                     UHUB_IS_SINGLE_TT(sc) ? "" : "s");
191         }
192         err = usbd_set_config_index(dev, 0, 1);
193         if (err) {
194                 DPRINTF(("%s: configuration failed, error=%s\n",
195                          device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
196                 return ENXIO;
197         }
198
199         if (dev->depth > USB_HUB_MAX_DEPTH) {
200                 kprintf("%s: hub depth (%d) exceeded, hub ignored\n",
201                        device_get_nameunit(sc->sc_dev), USB_HUB_MAX_DEPTH);
202                 return ENXIO;
203         }
204
205         /* Get hub descriptor. */
206         req.bmRequestType = UT_READ_CLASS_DEVICE;
207         req.bRequest = UR_GET_DESCRIPTOR;
208         USETW2(req.wValue, (dev->address > 1 ? UDESC_HUB : 0), 0);
209         USETW(req.wIndex, 0);
210         USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
211         DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
212         err = usbd_do_request(dev, &req, &hubdesc);
213         nports = hubdesc.bNbrPorts;
214         if (!err && nports > 7) {
215                 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
216                 err = usbd_do_request(dev, &req, &hubdesc);
217         }
218         if (err) {
219                 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
220                          device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
221                 return ENXIO;
222         }
223
224         for (nremov = 0, port = 1; port <= nports; port++)
225                 if (!UHD_NOT_REMOV(&hubdesc, port))
226                         nremov++;
227         kprintf("%s: %d port%s with %d removable, %s powered\n",
228                device_get_nameunit(sc->sc_dev), nports, nports != 1 ? "s" : "",
229                nremov, dev->self_powered ? "self" : "bus");
230
231         if (nports == 0) {
232                 kprintf("%s: no ports, hub ignored\n", device_get_nameunit(sc->sc_dev));
233                 goto bad;
234         }
235
236         hub = kmalloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
237                      M_USBDEV, M_WAITOK);
238         dev->hub = hub;
239         dev->hub->hubsoftc = sc;
240         hub->explore = uhub_explore;
241         hub->hubdesc = hubdesc;
242
243         DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
244                     "parent->selfpowered=%d\n",
245                  dev->self_powered, dev->powersrc->parent,
246                  dev->powersrc->parent ?
247                  dev->powersrc->parent->self_powered : 0));
248
249         if (!dev->self_powered && dev->powersrc->parent != NULL &&
250             !dev->powersrc->parent->self_powered) {
251                 kprintf("%s: bus powered hub connected to bus powered hub, "
252                        "ignored\n", device_get_nameunit(sc->sc_dev));
253                 goto bad;
254         }
255
256         /* Set up interrupt pipe. */
257         err = usbd_device2interface_handle(dev, 0, &iface);
258         if (err) {
259                 kprintf("%s: no interface handle\n", device_get_nameunit(sc->sc_dev));
260                 goto bad;
261         }
262         ed = usbd_interface2endpoint_descriptor(iface, 0);
263         if (ed == NULL) {
264                 kprintf("%s: no endpoint descriptor\n", device_get_nameunit(sc->sc_dev));
265                 goto bad;
266         }
267         if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
268                 kprintf("%s: bad interrupt endpoint\n", device_get_nameunit(sc->sc_dev));
269                 goto bad;
270         }
271
272         err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
273                   USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
274                   sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
275         if (err) {
276                 kprintf("%s: cannot open interrupt pipe\n",
277                        device_get_nameunit(sc->sc_dev));
278                 goto bad;
279         }
280
281         /* Wait with power off for a while. */
282         usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
283
284         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev);
285
286         /*
287          * To have the best chance of success we do things in the exact same
288          * order as Windoze98.  This should not be necessary, but some
289          * devices do not follow the USB specs to the letter.
290          *
291          * These are the events on the bus when a hub is attached:
292          *  Get device and config descriptors (see attach code)
293          *  Get hub descriptor (see above)
294          *  For all ports
295          *     turn on power
296          *     wait for power to become stable
297          * (all below happens in explore code)
298          *  For all ports
299          *     clear C_PORT_CONNECTION
300          *  For all ports
301          *     get port status
302          *     if device connected
303          *        wait 100 ms
304          *        turn on reset
305          *        wait
306          *        clear C_PORT_RESET
307          *        get port status
308          *        proceed with device attachment
309          */
310
311         if (UHUB_IS_HIGH_SPEED(sc)) {
312                 tts = kmalloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
313                               sizeof(struct usbd_tt), M_USBDEV, M_WAITOK);
314         }
315
316         /* Set up data structures */
317         for (p = 0; p < nports; p++) {
318                 struct usbd_port *up = &hub->ports[p];
319                 up->device = NULL;
320                 up->parent = dev;
321                 up->portno = p+1;
322                 if (dev->self_powered)
323                         /* Self powered hub, give ports maximum current. */
324                         up->power = USB_MAX_POWER;
325                 else
326                         up->power = USB_MIN_POWER;
327                 up->restartcnt = 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         }
335
336         /* XXX should check for none, individual, or ganged power? */
337
338         pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
339             + USB_EXTRA_POWER_UP_TIME;
340         for (port = 1; port <= nports; port++) {
341                 /* Turn the power on. */
342                 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
343                 if (err)
344                         kprintf("%s: port %d power on failed, %s\n",
345                                device_get_nameunit(sc->sc_dev), port,
346                                usbd_errstr(err));
347                 DPRINTF(("usb_init_port: turn on port %d power\n", port));
348         }
349
350         /* Wait for stable power if we are not a root hub */
351         if (dev->powersrc->parent != NULL)
352                 usbd_delay_ms(dev, pwrdly);
353
354         /* The usual exploration will finish the setup. */
355
356         sc->sc_running = 1;
357
358         return 0;
359
360  bad:
361         if (hub)
362                 kfree(hub, M_USBDEV);
363         dev->hub = NULL;
364         return ENXIO;
365 }
366
367 usbd_status
368 uhub_explore(usbd_device_handle dev)
369 {
370         usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
371         struct uhub_softc *sc = dev->hub->hubsoftc;
372         struct usbd_port *up;
373         usbd_status err;
374         int speed;
375         int port;
376         int change, status;
377
378         DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
379
380         if (!sc->sc_running)
381                 return (USBD_NOT_STARTED);
382
383         /* Ignore hubs that are too deep. */
384         if (dev->depth > USB_HUB_MAX_DEPTH)
385                 return (USBD_TOO_DEEP);
386
387         for(port = 1; port <= hd->bNbrPorts; port++) {
388                 up = &dev->hub->ports[port-1];
389                 err = usbd_get_port_status(dev, port, &up->status);
390                 if (err) {
391                         DPRINTF(("uhub_explore: get port status failed, "
392                                  "error=%s\n", usbd_errstr(err)));
393                         continue;
394                 }
395                 status = UGETW(up->status.wPortStatus);
396                 change = UGETW(up->status.wPortChange);
397                 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
398                             device_get_nameunit(sc->sc_dev), port, status, change));
399                 if (change & UPS_C_PORT_ENABLED) {
400                         DPRINTF(("uhub_explore: C_PORT_ENABLED 0x%x\n", change));
401                         usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
402                         if (change & UPS_C_CONNECT_STATUS) {
403                                 /* Ignore the port error if the device
404                                    vanished. */
405                         } else if (status & UPS_PORT_ENABLED) {
406                                 kprintf("%s: illegal enable change, port %d\n",
407                                        device_get_nameunit(sc->sc_dev), port);
408                         } else {
409                                 /* Port error condition. */
410                                 if (up->restartcnt) /* no message first time */
411                                         kprintf("%s: port error, restarting "
412                                                "port %d\n",
413                                                device_get_nameunit(sc->sc_dev), port);
414
415                                 if (up->restartcnt++ < USBD_RESTART_MAX)
416                                         goto disco;
417                                 else
418                                         kprintf("%s: port error, giving up "
419                                                "port %d\n",
420                                           device_get_nameunit(sc->sc_dev), port);
421                         }
422                 }
423                 if (!(change & UPS_C_CONNECT_STATUS)) {
424                         DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
425                                     "STATUS\n", port));
426                         /* No status change, just do recursive explore. */
427                         if (up->device != NULL && up->device->hub != NULL)
428                                 up->device->hub->explore(up->device);
429 #if 0 && defined(DIAGNOSTIC)
430                         if (up->device == NULL &&
431                             (status & UPS_CURRENT_CONNECT_STATUS))
432                                 kprintf("%s: connected, no device\n",
433                                        device_get_nameunit(sc->sc_dev));
434 #endif
435                         continue;
436                 }
437
438                 /* We have a connect status change, handle it. */
439
440                 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
441                          dev->address, port));
442                 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
443                 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
444                 /*
445                  * If there is already a device on the port the change status
446                  * must mean that is has disconnected.  Looking at the
447                  * current connect status is not enough to figure this out
448                  * since a new unit may have been connected before we handle
449                  * the disconnect.
450                  */
451         disco:
452                 if (up->device != NULL) {
453                         /* Disconnected */
454                         DPRINTF(("uhub_explore: device addr=%d disappeared "
455                                  "on port %d\n", up->device->address, port));
456                         usb_disconnect_port(up, sc->sc_dev);
457                         usbd_clear_port_feature(dev, port,
458                                                 UHF_C_PORT_CONNECTION);
459                 }
460                 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
461                         /* Nothing connected, just ignore it. */
462                         DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
463                                     "_STATUS\n", port));
464                         continue;
465                 }
466
467                 /* Connected */
468
469                 if (!(status & UPS_PORT_POWER))
470                         kprintf("%s: strange, connected port %d has no power\n",
471                                device_get_nameunit(sc->sc_dev), port);
472
473                 /* Wait for maximum device power up time. */
474                 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
475
476                 /* Reset port, which implies enabling it. */
477                 if (usbd_reset_port(dev, port, &up->status)) {
478                         kprintf("%s: port %d reset failed\n",
479                                device_get_nameunit(sc->sc_dev), port);
480                         continue;
481                 }
482                 /* Get port status again, it might have changed during reset */
483                 err = usbd_get_port_status(dev, port, &up->status);
484                 if (err) {
485                         DPRINTF(("uhub_explore: get port status failed, "
486                                  "error=%s\n", usbd_errstr(err)));
487                         continue;
488                 }
489                 status = UGETW(up->status.wPortStatus);
490                 change = UGETW(up->status.wPortChange);
491                 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
492                         /* Nothing connected, just ignore it. */
493 #ifdef DIAGNOSTIC
494                         kprintf("%s: port %d, device disappeared after reset\n",
495                                device_get_nameunit(sc->sc_dev), port);
496 #endif
497                         continue;
498                 }
499
500 #if 0
501                 if (UHUB_IS_HIGH_SPEED(sc) && !(status & UPS_HIGH_SPEED)) {
502                         kprintf("%s: port %d, transaction translation not "
503                             "implemented, low/full speed device ignored\n",
504                             device_get_nameunit(sc->sc_dev), port);
505                         continue;
506                 }
507 #endif
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(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                         kprintf("%s: device problem (%s), disabling port %d\n",
532                                device_get_nameunit(sc->sc_dev), usbd_errstr(err), 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         return (USBD_NORMAL_COMPLETION);
543 }
544
545 /*
546  * Called from process context when the hub is gone.
547  * Detach all devices on active ports.
548  */
549 static int
550 uhub_detach(device_t self)
551 {
552         struct uhub_softc *sc = device_get_softc(self);
553         struct usbd_hub *hub = sc->sc_hub->hub;
554         struct usbd_port *rup;
555         int port, nports;
556
557         DPRINTF(("uhub_detach: sc=%port\n", sc));
558
559         crit_enter();
560
561         if (hub == NULL) {              /* Must be partially working */
562                 crit_exit();
563                 return (0);
564         }
565
566         usbd_abort_pipe(sc->sc_ipipe);
567         usbd_close_pipe(sc->sc_ipipe);
568
569         nports = hub->hubdesc.bNbrPorts;
570         for(port = 0; port < nports; port++) {
571                 rup = &hub->ports[port];
572                 if (rup->device)
573                         usb_disconnect_port(rup, self);
574         }
575
576         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
577                            sc->sc_dev);
578
579         if (hub->ports[0].tt)
580                 kfree(hub->ports[0].tt, M_USBDEV);
581         kfree(hub, M_USBDEV);
582         sc->sc_hub->hub = NULL;
583
584         crit_exit();
585
586         return (0);
587 }
588
589 int
590 uhub_child_location_str(device_t cbdev, device_t child, char *buf,
591     size_t buflen)
592 {
593         struct uhub_softc *sc = device_get_softc(cbdev);
594         usbd_device_handle devhub = sc->sc_hub;
595         usbd_device_handle dev;
596         int nports;
597         int port;
598         int i;
599
600         crit_enter();
601         nports = devhub->hub->hubdesc.bNbrPorts;
602         for (port = 0; port < nports; port++) {
603                 dev = devhub->hub->ports[port].device;
604                 if (dev && dev->subdevs) {
605                         for (i = 0; dev->subdevs[i]; i++) {
606                                 if (dev->subdevs[i] == child) {
607                                         if (dev->ifacenums == NULL) {
608                                                 ksnprintf(buf, buflen,
609                                                     "port=%i", port);
610                                         } else {
611                                                 ksnprintf(buf, buflen,
612                                                     "port=%i interface=%i",
613                                                     port, dev->ifacenums[i]);
614                                         }
615                                         goto found_dev;
616                                 }
617                         }
618                 }
619         }
620         DPRINTFN(0,("uhub_child_location_str: device not on hub\n"));
621         buf[0] = '\0';
622 found_dev:
623         crit_exit();
624         return (0);
625 }
626
627 int
628 uhub_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
629     size_t buflen)
630 {
631         struct uhub_softc *sc = device_get_softc(cbdev);
632         usbd_device_handle devhub = sc->sc_hub;
633         usbd_device_handle dev;
634         struct usbd_interface *iface;
635         char serial[128];
636         int nports;
637         int port;
638         int i;
639
640         crit_enter();
641         nports = devhub->hub->hubdesc.bNbrPorts;
642         for (port = 0; port < nports; port++) {
643                 dev = devhub->hub->ports[port].device;
644                 if (dev && dev->subdevs) {
645                         for (i = 0; dev->subdevs[i]; i++) {
646                                 if (dev->subdevs[i] == child) {
647                                         goto found_dev;
648                                 }
649                         }
650                 }
651         }
652         DPRINTFN(0,("uhub_child_pnpinfo_str: device not on hub\n"));
653         buf[0] = '\0';
654         crit_exit();
655         return (0);
656
657 found_dev:
658         /* XXX can sleep */
659         (void)usbd_get_string(dev, dev->ddesc.iSerialNumber, &serial[0]);
660         if (dev->ifacenums == NULL) {
661                 ksnprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
662                     "devclass=0x%02x devsubclass=0x%02x "
663                     "release=0x%04x sernum=\"%s\"",
664                     UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
665                     dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
666                     UGETW(dev->ddesc.bcdDevice), serial);
667         } else {
668                 iface = &dev->ifaces[dev->ifacenums[i]];
669                 ksnprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
670                     "devclass=0x%02x devsubclass=0x%02x "
671                     "release=0x%04x sernum=\"%s\" "
672                     "intclass=0x%02x intsubclass=0x%02x",
673                     UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
674                     dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
675                     UGETW(dev->ddesc.bcdDevice), serial,
676                     iface->idesc->bInterfaceClass,
677                     iface->idesc->bInterfaceSubClass);
678         }
679         crit_exit();
680         return (0);
681 }
682
683 static void
684 uhub_child_detached(device_t self, device_t child)
685 {
686         struct uhub_softc *sc = device_get_softc(self);
687         usbd_device_handle devhub = sc->sc_hub;
688         usbd_device_handle dev = NULL;
689         int nports, port, i = 0;
690
691         crit_enter();
692         nports = devhub->hub->hubdesc.bNbrPorts;
693         for (port = 0; port < nports; port++) {
694                 dev = devhub->hub->ports[port].device;
695                 if (dev && dev->subdevs) {
696                         for (i = 0; dev->subdevs[i]; ++i) {
697                                 if (dev->subdevs[i] == child)
698                                         goto found_dev;
699                         }
700                 }
701         }
702         crit_exit();
703         return;
704
705 found_dev:
706 #if 0
707         kprintf("%s: at %s", device_get_nameunit(dev->subdevs[i]),
708                device_get_nameunit(self));
709         if (port != 0)
710                 kprintf(" port %d", port);
711         kprintf(" (addr %d) disconnected\n", dev->address);
712 #endif
713         kfree(device_get_ivars(dev->subdevs[i]), M_USB);
714         dev->subdevs[i] = NULL;
715         crit_exit();
716 }
717
718 /*
719  * Hub interrupt.
720  * This an indication that some port has changed status.
721  * Notify the bus event handler thread that we need
722  * to be explored again.
723  */
724 void
725 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
726 {
727         struct uhub_softc *sc = addr;
728
729         DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
730         if (status == USBD_STALLED)
731                 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
732         else if (status == USBD_NORMAL_COMPLETION)
733                 usb_needs_explore(sc->sc_hub);
734 }
735
736 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
737 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);