kernel: Use NULL for DRIVER_MODULE()'s evh & arg (which are pointers).
[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
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@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/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/bus.h>
51 #include <sys/lock.h>
52 #include <sys/thread2.h>
53 #include <sys/sysctl.h>
54
55 #include <bus/usb/usb.h>
56 #include <bus/usb/usbdi.h>
57 #include <bus/usb/usbdi_util.h>
58 #include <bus/usb/usbdivar.h>
59
60 #define UHUB_INTR_INTERVAL 255  /* ms */
61
62 #ifdef USB_DEBUG
63 #define DPRINTF(x)      if (uhubdebug) kprintf x
64 #define DPRINTFN(n,x)   if (uhubdebug>(n)) kprintf x
65 int     uhubdebug = 0;
66 SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB uhub");
67 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW,
68            &uhubdebug, 0, "uhub debug level");
69 #else
70 #define DPRINTF(x)
71 #define DPRINTFN(n,x)
72 #endif
73
74 struct uhub_softc {
75         usbd_device_handle      sc_hub;         /* USB device */
76         usbd_pipe_handle        sc_ipipe;       /* interrupt pipe */
77         u_int8_t                sc_status[1];   /* XXX more ports */
78         u_char                  sc_running;
79 };
80 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol)
81 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
82 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
83
84 static usbd_status uhub_explore(usbd_device_handle hub);
85 static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
86
87 static bus_child_detached_t uhub_child_detached;
88 static bus_child_location_str_t uhub_child_location_str;
89 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_str;
90
91 /*
92  * We need two attachment points:
93  * hub to usb and hub to hub
94  * Every other driver only connects to hubs
95  */
96
97 static device_probe_t uhub_match;
98 static device_attach_t uhub_attach;
99 static device_detach_t uhub_detach;
100
101 static devclass_t uhub_devclass;
102
103 static kobj_method_t uhub_methods[] = {
104         DEVMETHOD(device_probe, uhub_match),
105         DEVMETHOD(device_attach, uhub_attach),
106         DEVMETHOD(device_detach, uhub_detach),
107         DEVMETHOD(bus_child_detached, uhub_child_detached),
108         DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
109         DEVMETHOD(bus_child_location_str, uhub_child_location_str),
110         DEVMETHOD(bus_driver_added, bus_generic_driver_added),
111         DEVMETHOD(device_suspend, bus_generic_suspend),
112         DEVMETHOD(device_resume, bus_generic_resume),
113         DEVMETHOD(device_shutdown, bus_generic_shutdown),
114         {0,0}
115 };
116
117 static driver_t uhub_driver = {
118         "uhub",
119         uhub_methods,
120         sizeof(struct uhub_softc)
121 };
122
123 MODULE_DEPEND(uhub, usb, 1, 1, 1);
124
125 /* Create the driver instance for the hub connected to usb case. */
126 devclass_t uhubroot_devclass;
127
128 static device_method_t uhubroot_methods[] = {
129         DEVMETHOD(bus_child_detached, uhub_child_detached),
130         DEVMETHOD(bus_child_location_str, uhub_child_location_str),
131         DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
132         DEVMETHOD(bus_driver_added, bus_generic_driver_added),
133
134         DEVMETHOD(device_probe, uhub_match),
135         DEVMETHOD(device_attach, uhub_attach),
136         DEVMETHOD(device_detach, uhub_detach),
137         DEVMETHOD(device_suspend, bus_generic_suspend),
138         DEVMETHOD(device_resume, bus_generic_resume),
139         DEVMETHOD(device_shutdown, bus_generic_shutdown),
140         {0,0}
141 };
142
143 static  driver_t uhubroot_driver = {
144         "uhub",
145         uhubroot_methods,
146         sizeof(struct uhub_softc)
147 };
148
149 static int
150 uhub_match(device_t self)
151 {
152         struct usb_attach_arg *uaa = device_get_ivars(self);
153         usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
154
155         DPRINTFN(5,("uhub_match, dd=%p\n", dd));
156         /*
157          * The subclass for hubs seems to be 0 for some and 1 for others,
158          * so we just ignore the subclass.
159          */
160         if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
161                 return (UMATCH_DEVCLASS_DEVSUBCLASS);
162         return (UMATCH_NONE);
163 }
164
165 static int
166 uhub_attach(device_t self)
167 {
168         struct uhub_softc *sc = device_get_softc(self);
169         struct usb_attach_arg *uaa = device_get_ivars(self);
170         usbd_device_handle dev = uaa->device;
171         usbd_status err;
172         struct usbd_hub *hub = NULL;
173         usb_device_request_t req;
174         usb_hub_descriptor_t hubdesc;
175         int p, port, nports, nremov, pwrdly;
176         usbd_interface_handle iface;
177         usb_endpoint_descriptor_t *ed;
178         struct usbd_tt *tts = NULL;
179
180         DPRINTFN(1,("uhub_attach\n"));
181         sc->sc_hub = dev;
182
183         if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
184                 device_printf(self,
185                     "%s transaction translator%s\n",
186                     UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
187                     UHUB_IS_SINGLE_TT(sc) ? "" : "s");
188         }
189         err = usbd_set_config_index(dev, 0, 1);
190         if (err) {
191                 DPRINTF(("%s: configuration failed, error=%s\n",
192                          device_get_nameunit(self), usbd_errstr(err)));
193                 return ENXIO;
194         }
195
196         if (dev->depth > USB_HUB_MAX_DEPTH) {
197                 device_printf(self,
198                     "hub depth (%d) exceeded, hub ignored\n",
199                     USB_HUB_MAX_DEPTH);
200                 return ENXIO;
201         }
202
203         /* Get hub descriptor. */
204         req.bmRequestType = UT_READ_CLASS_DEVICE;
205         req.bRequest = UR_GET_DESCRIPTOR;
206         USETW2(req.wValue, (dev->address > 1 ? UDESC_HUB : 0), 0);
207         USETW(req.wIndex, 0);
208         USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
209         DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
210         err = usbd_do_request(dev, &req, &hubdesc);
211         nports = hubdesc.bNbrPorts;
212         if (!err && nports > 7) {
213                 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
214                 err = usbd_do_request(dev, &req, &hubdesc);
215         }
216         if (err) {
217                 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
218                          device_get_nameunit(self), usbd_errstr(err)));
219                 return ENXIO;
220         }
221
222         for (nremov = 0, port = 1; port <= nports; port++)
223                 if (!UHD_NOT_REMOV(&hubdesc, port))
224                         nremov++;
225         device_printf(self,
226             "%d port%s with %d removable, %s powered\n",
227             nports, nports != 1 ? "s" : "",
228             nremov, dev->self_powered ? "self" : "bus");
229
230         if (nports == 0) {
231                 device_printf(self, "no ports, hub ignored\n");
232                 goto bad;
233         }
234
235         hub = kmalloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
236                      M_USBDEV, M_WAITOK);
237         dev->hub = hub;
238         dev->hub->hubdev = self; 
239         hub->explore = uhub_explore;
240         hub->hubdesc = hubdesc;
241
242         DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
243                     "parent->selfpowered=%d\n",
244                  dev->self_powered, dev->powersrc->parent,
245                  dev->powersrc->parent ?
246                  dev->powersrc->parent->self_powered : 0));
247
248         if (!dev->self_powered && dev->powersrc->parent != NULL &&
249             !dev->powersrc->parent->self_powered) {
250                 device_printf(self,
251                     "bus powered hub connected to bus powered hub, "
252                     "ignored\n");
253                 goto bad;
254         }
255
256         /* Set up interrupt pipe. */
257         err = usbd_device2interface_handle(dev, 0, &iface);
258         if (err) {
259                 device_printf(self, "no interface handle\n");
260                 goto bad;
261         }
262         ed = usbd_interface2endpoint_descriptor(iface, 0);
263         if (ed == NULL) {
264                 device_printf(self, "no endpoint descriptor\n");
265                 goto bad;
266         }
267         if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
268                 device_printf(self, "bad interrupt endpoint\n");
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                 device_printf(self, "cannot open interrupt pipe\n");
277                 goto bad;
278         }
279
280         /* Wait with power off for a while. */
281         usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
282
283         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, self);
284
285         /*
286          * To have the best chance of success we do things in the exact same
287          * order as Windoze98.  This should not be necessary, but some
288          * devices do not follow the USB specs to the letter.
289          *
290          * These are the events on the bus when a hub is attached:
291          *  Get device and config descriptors (see attach code)
292          *  Get hub descriptor (see above)
293          *  For all ports
294          *     turn on power
295          *     wait for power to become stable
296          * (all below happens in explore code)
297          *  For all ports
298          *     clear C_PORT_CONNECTION
299          *  For all ports
300          *     get port status
301          *     if device connected
302          *        wait 100 ms
303          *        turn on reset
304          *        wait
305          *        clear C_PORT_RESET
306          *        get port status
307          *        proceed with device attachment
308          */
309
310         if (UHUB_IS_HIGH_SPEED(sc)) {
311                 tts = kmalloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
312                               sizeof(struct usbd_tt), M_USBDEV, M_WAITOK);
313         }
314
315         /* Set up data structures */
316         for (p = 0; p < nports; p++) {
317                 struct usbd_port *up = &hub->ports[p];
318                 up->device = NULL;
319                 up->parent = dev;
320                 up->portno = p+1;
321                 if (dev->self_powered)
322                         /* Self powered hub, give ports maximum current. */
323                         up->power = USB_MAX_POWER;
324                 else
325                         up->power = USB_MIN_POWER;
326                 up->restartcnt = 0;
327                 if (UHUB_IS_HIGH_SPEED(sc)) {
328                         up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
329                         up->tt->hub = hub;
330                 } else {
331                         up->tt = NULL;
332                 }
333         }
334
335         /* XXX should check for none, individual, or ganged power? */
336
337         pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
338             + USB_EXTRA_POWER_UP_TIME;
339         for (port = 1; port <= nports; port++) {
340                 /* Turn the power on. */
341                 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
342                 if (err)
343                         device_printf(self,
344                             "port %d power on failed, %s\n",
345                             port, usbd_errstr(err));
346                 DPRINTF(("usb_init_port: turn on port %d power\n", port));
347         }
348
349         /* Wait for stable power if we are not a root hub */
350         if (dev->powersrc->parent != NULL)
351                 usbd_delay_ms(dev, pwrdly);
352
353         /* The usual exploration will finish the setup. */
354
355         sc->sc_running = 1;
356
357         return 0;
358
359  bad:
360         if (hub)
361                 kfree(hub, M_USBDEV);
362         dev->hub = NULL;
363         return ENXIO;
364 }
365
366 usbd_status
367 uhub_explore(usbd_device_handle dev)
368 {
369         usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
370         device_t self = dev->hub->hubdev;
371         struct uhub_softc *sc = device_get_softc(self);
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(self), 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                                 device_printf(self,
407                                     "illegal enable change, port %d\n", port);
408                         } else {
409                                 /* Port error condition. */
410                                 if (up->restartcnt) /* no message first time */
411                                         device_printf(self,
412                                             "port error, restarting "
413                                             "port %d\n", port);
414
415                                 if (up->restartcnt++ < USBD_RESTART_MAX)
416                                         goto disco;
417                                 else
418                                         device_printf(self,
419                                             "port error, giving up "
420                                             "port %d\n", 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                                 device_printf(self,
433                                     "connected, no device\n");
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, self);
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                         device_printf(self,
471                             "strange, connected port %d has no power\n",
472                             port);
473
474                 /* Wait for maximum device power up time. */
475                 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
476
477                 /* Reset port, which implies enabling it. */
478                 if (usbd_reset_port(dev, port, &up->status)) {
479                         device_printf(self,
480                             "port %d reset failed\n", port);
481                         continue;
482                 }
483                 /* Get port status again, it might have changed during reset */
484                 err = usbd_get_port_status(dev, port, &up->status);
485                 if (err) {
486                         DPRINTF(("uhub_explore: get port status failed, "
487                                  "error=%s\n", usbd_errstr(err)));
488                         continue;
489                 }
490                 status = UGETW(up->status.wPortStatus);
491                 change = UGETW(up->status.wPortChange);
492                 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
493                         /* Nothing connected, just ignore it. */
494 #ifdef DIAGNOSTIC
495                         device_printf(self,
496                             "port %d, device disappeared after reset\n",
497                             port);
498 #endif
499                         continue;
500                 }
501
502 #if 0
503                 if (UHUB_IS_HIGH_SPEED(sc) && !(status & UPS_HIGH_SPEED)) {
504                         device_printf(self,
505                             "port %d, transaction translation not "
506                             "implemented, low/full speed device ignored\n",
507                             port);
508                         continue;
509                 }
510 #endif
511
512                 /* Figure out device speed */
513                 if (status & UPS_HIGH_SPEED)
514                         speed = USB_SPEED_HIGH;
515                 else if (status & UPS_LOW_SPEED)
516                         speed = USB_SPEED_LOW;
517                 else
518                         speed = USB_SPEED_FULL;
519                 /* Get device info and set its address. */
520                 err = usbd_new_device(self, dev->bus,
521                                       dev->depth + 1, speed, port, up);
522                 /* XXX retry a few times? */
523                 if (err) {
524                         DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
525                                      "error=%s\n", usbd_errstr(err)));
526                         /* Avoid addressing problems by disabling. */
527                         /* usbd_reset_port(dev, port, &up->status); */
528
529                         /*
530                          * The unit refused to accept a new address, or had
531                          * some other serious problem.  Since we cannot leave
532                          * at 0 we have to disable the port instead.
533                          */
534                         device_printf(self,
535                             "device problem (%s), disabling port %d\n",
536                             usbd_errstr(err), port);
537                         usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
538                 } else {
539                         /* The port set up succeeded, reset error count. */
540                         up->restartcnt = 0;
541
542                         if (up->device->hub)
543                                 up->device->hub->explore(up->device);
544                 }
545         }
546         return (USBD_NORMAL_COMPLETION);
547 }
548
549 /*
550  * Called from process context when the hub is gone.
551  * Detach all devices on active ports.
552  */
553 static int
554 uhub_detach(device_t self)
555 {
556         struct uhub_softc *sc = device_get_softc(self);
557         struct usbd_hub *hub = sc->sc_hub->hub;
558         struct usbd_port *rup;
559         int port, nports;
560
561         DPRINTF(("uhub_detach: sc=%port\n", sc));
562
563         crit_enter();
564
565         if (hub == NULL) {              /* Must be partially working */
566                 crit_exit();
567                 return (0);
568         }
569
570         usbd_abort_pipe(sc->sc_ipipe);
571         usbd_close_pipe(sc->sc_ipipe);
572
573         nports = hub->hubdesc.bNbrPorts;
574         for(port = 0; port < nports; port++) {
575                 rup = &hub->ports[port];
576                 if (rup->device)
577                         usb_disconnect_port(rup, self);
578         }
579
580         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, self);
581
582         if (hub->ports[0].tt)
583                 kfree(hub->ports[0].tt, M_USBDEV);
584         kfree(hub, M_USBDEV);
585         sc->sc_hub->hub = NULL;
586
587         crit_exit();
588
589         return (0);
590 }
591
592 int
593 uhub_child_location_str(device_t cbdev, device_t child, char *buf,
594     size_t buflen)
595 {
596         struct uhub_softc *sc = device_get_softc(cbdev);
597         usbd_device_handle devhub = sc->sc_hub;
598         usbd_device_handle dev;
599         int nports;
600         int port;
601         int i;
602
603         crit_enter();
604         nports = devhub->hub->hubdesc.bNbrPorts;
605         for (port = 0; port < nports; port++) {
606                 dev = devhub->hub->ports[port].device;
607                 if (dev && dev->subdevs) {
608                         for (i = 0; dev->subdevs[i]; i++) {
609                                 if (dev->subdevs[i] == child) {
610                                         if (dev->ifacenums == NULL) {
611                                                 ksnprintf(buf, buflen,
612                                                     "port=%i", port);
613                                         } else {
614                                                 ksnprintf(buf, buflen,
615                                                     "port=%i interface=%i",
616                                                     port, dev->ifacenums[i]);
617                                         }
618                                         goto found_dev;
619                                 }
620                         }
621                 }
622         }
623         DPRINTFN(0,("uhub_child_location_str: device not on hub\n"));
624         buf[0] = '\0';
625 found_dev:
626         crit_exit();
627         return (0);
628 }
629
630 int
631 uhub_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
632     size_t buflen)
633 {
634         struct uhub_softc *sc = device_get_softc(cbdev);
635         usbd_device_handle devhub = sc->sc_hub;
636         usbd_device_handle dev;
637         struct usbd_interface *iface;
638         char serial[128];
639         int nports;
640         int port;
641         int i;
642
643         crit_enter();
644         nports = devhub->hub->hubdesc.bNbrPorts;
645         for (port = 0; port < nports; port++) {
646                 dev = devhub->hub->ports[port].device;
647                 if (dev && dev->subdevs) {
648                         for (i = 0; dev->subdevs[i]; i++) {
649                                 if (dev->subdevs[i] == child) {
650                                         goto found_dev;
651                                 }
652                         }
653                 }
654         }
655         DPRINTFN(0,("uhub_child_pnpinfo_str: device not on hub\n"));
656         buf[0] = '\0';
657         crit_exit();
658         return (0);
659
660 found_dev:
661         /* XXX can sleep */
662         (void)usbd_get_string(dev, dev->ddesc.iSerialNumber, &serial[0]);
663         if (dev->ifacenums == NULL) {
664                 ksnprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
665                     "devclass=0x%02x devsubclass=0x%02x "
666                     "release=0x%04x sernum=\"%s\"",
667                     UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
668                     dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
669                     UGETW(dev->ddesc.bcdDevice), serial);
670         } else {
671                 iface = &dev->ifaces[dev->ifacenums[i]];
672                 ksnprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
673                     "devclass=0x%02x devsubclass=0x%02x "
674                     "release=0x%04x sernum=\"%s\" "
675                     "intclass=0x%02x intsubclass=0x%02x",
676                     UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
677                     dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
678                     UGETW(dev->ddesc.bcdDevice), serial,
679                     iface->idesc->bInterfaceClass,
680                     iface->idesc->bInterfaceSubClass);
681         }
682         crit_exit();
683         return (0);
684 }
685
686 static void
687 uhub_child_detached(device_t self, device_t child)
688 {
689         struct uhub_softc *sc = device_get_softc(self);
690         usbd_device_handle devhub = sc->sc_hub;
691         usbd_device_handle dev = NULL;
692         int nports, port, i = 0;
693
694         crit_enter();
695         nports = devhub->hub->hubdesc.bNbrPorts;
696         for (port = 0; port < nports; port++) {
697                 dev = devhub->hub->ports[port].device;
698                 if (dev && dev->subdevs) {
699                         for (i = 0; dev->subdevs[i]; ++i) {
700                                 if (dev->subdevs[i] == child)
701                                         goto found_dev;
702                         }
703                 }
704         }
705         crit_exit();
706         return;
707
708 found_dev:
709 #if 0
710         device_printf(dev->subdevs[i], "at %s", device_get_nameunit(self));
711         if (port != 0)
712                 kprintf(" port %d", port);
713         kprintf(" (addr %d) disconnected\n", dev->address);
714 #endif
715         kfree(device_get_ivars(dev->subdevs[i]), M_USB);
716         dev->subdevs[i] = NULL;
717         crit_exit();
718 }
719
720 /*
721  * Hub interrupt.
722  * This an indication that some port has changed status.
723  * Notify the bus event handler thread that we need
724  * to be explored again.
725  */
726 void
727 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
728 {
729         struct uhub_softc *sc = addr;
730
731         DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
732         if (status == USBD_STALLED)
733                 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
734         else if (status == USBD_NORMAL_COMPLETION)
735                 usb_needs_explore(sc->sc_hub);
736 }
737
738 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, NULL, NULL);
739 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, NULL);