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