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