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