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