]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/uhub.c
Use a different task queue for host controller and peripheral driver
[FreeBSD/FreeBSD.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 #if defined(__NetBSD__) || defined(__OpenBSD__)
52 #include <sys/device.h>
53 #include <sys/proc.h>
54 #elif defined(__FreeBSD__)
55 #include <sys/module.h>
56 #include <sys/bus.h>
57 #include <sys/lock.h>
58 #include <sys/mutex.h>
59 #endif
60 #include <sys/sysctl.h>
61
62 #include <machine/bus.h>
63
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/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         device_t                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 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol)
91 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
92 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
93
94 static usbd_status uhub_explore(usbd_device_handle hub);
95 static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
96
97 #if defined(__FreeBSD__)
98 static bus_child_location_str_t uhub_child_location_str;
99 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_str;
100 #endif
101
102
103 /*
104  * We need two attachment points:
105  * hub to usb and hub to hub
106  * Every other driver only connects to hubs
107  */
108
109 #if defined(__NetBSD__) || defined(__OpenBSD__)
110 USB_DECLARE_DRIVER(uhub);
111
112 /* Create the driver instance for the hub connected to hub case */
113 CFATTACH_DECL(uhub_uhub, sizeof(struct uhub_softc),
114     uhub_match, uhub_attach, uhub_detach, uhub_activate);
115 #elif defined(__FreeBSD__)
116 USB_DECLARE_DRIVER_INIT(uhub,
117         DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
118         DEVMETHOD(bus_child_location_str, uhub_child_location_str),
119         DEVMETHOD(bus_driver_added, bus_generic_driver_added),
120         DEVMETHOD(device_suspend, bus_generic_suspend),
121         DEVMETHOD(device_resume, bus_generic_resume),
122         DEVMETHOD(device_shutdown, bus_generic_shutdown)
123         );
124
125 /* Create the driver instance for the hub connected to usb case. */
126 devclass_t uhubroot_devclass;
127
128 static device_method_t uhubroot_methods[] = {
129         DEVMETHOD(bus_child_location_str, uhub_child_location_str),
130         DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
131         DEVMETHOD(bus_driver_added, bus_generic_driver_added),
132
133         DEVMETHOD(device_probe, uhub_match),
134         DEVMETHOD(device_attach, uhub_attach),
135         DEVMETHOD(device_detach, uhub_detach),
136         DEVMETHOD(device_suspend, bus_generic_suspend),
137         DEVMETHOD(device_resume, bus_generic_resume),
138         DEVMETHOD(device_shutdown, bus_generic_shutdown),
139         {0,0}
140 };
141
142 static  driver_t uhubroot_driver = {
143         "uhub",
144         uhubroot_methods,
145         sizeof(struct uhub_softc)
146 };
147 #endif
148
149 USB_MATCH(uhub)
150 {
151         USB_MATCH_START(uhub, uaa);
152         usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
153
154         DPRINTFN(5,("uhub_match, dd=%p\n", dd));
155         /*
156          * The subclass for hubs seems to be 0 for some and 1 for others,
157          * so we just ignore the subclass.
158          */
159         if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
160                 return (UMATCH_DEVCLASS_DEVSUBCLASS);
161         return (UMATCH_NONE);
162 }
163
164 USB_ATTACH(uhub)
165 {
166         USB_ATTACH_START(uhub, sc, uaa);
167         usbd_device_handle dev = uaa->device;
168         char *devinfo;
169         usbd_status err;
170         struct usbd_hub *hub = NULL;
171         usb_device_request_t req;
172         usb_hub_descriptor_t hubdesc;
173         int p, port, nports, nremov, pwrdly;
174         usbd_interface_handle iface;
175         usb_endpoint_descriptor_t *ed;
176         struct usbd_tt *tts = NULL;
177
178         devinfo = malloc(1024, M_TEMP, M_NOWAIT);
179         if (devinfo == NULL) {
180                 USB_ATTACH_ERROR_RETURN;
181         }
182         DPRINTFN(1,("uhub_attach\n"));
183         sc->sc_hub = dev;
184         usbd_devinfo(dev, 1, devinfo);
185         USB_ATTACH_SETUP;
186
187         if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
188                 printf("%s: %s transaction translator%s\n",
189                     device_get_nameunit(sc->sc_dev),
190                     UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
191                     UHUB_IS_SINGLE_TT(sc) ? "" : "s");
192         }
193         err = usbd_set_config_index(dev, 0, 1);
194         if (err) {
195                 DPRINTF(("%s: configuration failed, error=%s\n",
196                          device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
197                 free(devinfo, M_TEMP);
198                 USB_ATTACH_ERROR_RETURN;
199         }
200
201         if (dev->depth > USB_HUB_MAX_DEPTH) {
202                 printf("%s: hub depth (%d) exceeded, hub ignored\n",
203                        device_get_nameunit(sc->sc_dev), USB_HUB_MAX_DEPTH);
204                 free(devinfo, M_TEMP);
205                 USB_ATTACH_ERROR_RETURN;
206         }
207
208         /* Get hub descriptor. */
209         req.bmRequestType = UT_READ_CLASS_DEVICE;
210         req.bRequest = UR_GET_DESCRIPTOR;
211         USETW2(req.wValue, (dev->address > 1 ? UDESC_HUB : 0), 0);
212         USETW(req.wIndex, 0);
213         USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
214         DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
215         err = usbd_do_request(dev, &req, &hubdesc);
216         nports = hubdesc.bNbrPorts;
217         if (!err && nports > 7) {
218                 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
219                 err = usbd_do_request(dev, &req, &hubdesc);
220         }
221         if (err) {
222                 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
223                          device_get_nameunit(sc->sc_dev), usbd_errstr(err)));
224                 free(devinfo, M_TEMP);
225                 USB_ATTACH_ERROR_RETURN;
226         }
227
228         for (nremov = 0, port = 1; port <= nports; port++)
229                 if (!UHD_NOT_REMOV(&hubdesc, port))
230                         nremov++;
231         printf("%s: %d port%s with %d removable, %s powered\n",
232                device_get_nameunit(sc->sc_dev), nports, nports != 1 ? "s" : "",
233                nremov, dev->self_powered ? "self" : "bus");
234
235         if (nports == 0) {
236                 printf("%s: no ports, hub ignored\n", device_get_nameunit(sc->sc_dev));
237                 goto bad;
238         }
239
240         hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
241                      M_USBDEV, M_NOWAIT);
242         if (hub == NULL) {
243                 free(devinfo, M_TEMP);
244                 USB_ATTACH_ERROR_RETURN;
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                 printf("%s: bus powered hub connected to bus powered hub, "
260                        "ignored\n", device_get_nameunit(sc->sc_dev));
261                 goto bad;
262         }
263
264         /* Set up interrupt pipe. */
265         err = usbd_device2interface_handle(dev, 0, &iface);
266         if (err) {
267                 printf("%s: no interface handle\n", device_get_nameunit(sc->sc_dev));
268                 goto bad;
269         }
270         ed = usbd_interface2endpoint_descriptor(iface, 0);
271         if (ed == NULL) {
272                 printf("%s: no endpoint descriptor\n", device_get_nameunit(sc->sc_dev));
273                 goto bad;
274         }
275         if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
276                 printf("%s: bad interrupt endpoint\n", device_get_nameunit(sc->sc_dev));
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                   sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
283         if (err) {
284                 printf("%s: cannot open interrupt pipe\n",
285                        device_get_nameunit(sc->sc_dev));
286                 goto bad;
287         }
288
289         /* Wait with power off for a while. */
290         usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
291
292         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
293
294         /*
295          * To have the best chance of success we do things in the exact same
296          * order as Windoze98.  This should not be necessary, but some
297          * devices do not follow the USB specs to the letter.
298          *
299          * These are the events on the bus when a hub is attached:
300          *  Get device and config descriptors (see attach code)
301          *  Get hub descriptor (see above)
302          *  For all ports
303          *     turn on power
304          *     wait for power to become stable
305          * (all below happens in explore code)
306          *  For all ports
307          *     clear C_PORT_CONNECTION
308          *  For all ports
309          *     get port status
310          *     if device connected
311          *        wait 100 ms
312          *        turn on reset
313          *        wait
314          *        clear C_PORT_RESET
315          *        get port status
316          *        proceed with device attachment
317          */
318
319         if (UHUB_IS_HIGH_SPEED(sc)) {
320                 tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
321                     sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
322                 if (!tts)
323                         goto bad;
324         }
325
326         /* Set up data structures */
327         for (p = 0; p < nports; p++) {
328                 struct usbd_port *up = &hub->ports[p];
329                 up->device = NULL;
330                 up->parent = dev;
331                 up->portno = p+1;
332                 if (dev->self_powered)
333                         /* Self powered hub, give ports maximum current. */
334                         up->power = USB_MAX_POWER;
335                 else
336                         up->power = USB_MIN_POWER;
337                 up->restartcnt = 0;
338                 if (UHUB_IS_HIGH_SPEED(sc)) {
339                         up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
340                         up->tt->hub = hub;
341                 } else {
342                         up->tt = NULL;
343                 }
344         }
345
346         /* XXX should check for none, individual, or ganged power? */
347
348         pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
349             + USB_EXTRA_POWER_UP_TIME;
350         for (port = 1; port <= nports; port++) {
351                 /* Turn the power on. */
352                 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
353                 if (err)
354                         printf("%s: port %d power on failed, %s\n",
355                                device_get_nameunit(sc->sc_dev), port,
356                                usbd_errstr(err));
357                 DPRINTF(("usb_init_port: turn on port %d power\n", port));
358                 /* Wait for stable power. */
359                 usbd_delay_ms(dev, pwrdly);
360         }
361
362         /* The usual exploration will finish the setup. */
363
364         sc->sc_running = 1;
365
366         USB_ATTACH_SUCCESS_RETURN;
367
368  bad:
369         if (hub)
370                 free(hub, M_USBDEV);
371         free(devinfo, M_TEMP);
372         dev->hub = NULL;
373         USB_ATTACH_ERROR_RETURN;
374 }
375
376 usbd_status
377 uhub_explore(usbd_device_handle dev)
378 {
379         usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
380         struct uhub_softc *sc = dev->hub->hubsoftc;
381         struct usbd_port *up;
382         usbd_status err;
383         int speed;
384         int port;
385         int change, status;
386
387         DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
388
389         if (!sc->sc_running)
390                 return (USBD_NOT_STARTED);
391
392         /* Ignore hubs that are too deep. */
393         if (dev->depth > USB_HUB_MAX_DEPTH)
394                 return (USBD_TOO_DEEP);
395
396         for(port = 1; port <= hd->bNbrPorts; port++) {
397                 up = &dev->hub->ports[port-1];
398                 err = usbd_get_port_status(dev, port, &up->status);
399                 if (err) {
400                         DPRINTF(("uhub_explore: get port status failed, "
401                                  "error=%s\n", usbd_errstr(err)));
402                         continue;
403                 }
404                 status = UGETW(up->status.wPortStatus);
405                 change = UGETW(up->status.wPortChange);
406                 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
407                             device_get_nameunit(sc->sc_dev), port, status, change));
408                 if (change & UPS_C_PORT_ENABLED) {
409                         DPRINTF(("uhub_explore: C_PORT_ENABLED 0x%x\n", change));
410                         usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
411                         if (change & UPS_C_CONNECT_STATUS) {
412                                 /* Ignore the port error if the device
413                                    vanished. */
414                         } else if (status & UPS_PORT_ENABLED) {
415                                 printf("%s: illegal enable change, port %d\n",
416                                        device_get_nameunit(sc->sc_dev), port);
417                         } else {
418                                 /* Port error condition. */
419                                 if (up->restartcnt) /* no message first time */
420                                         printf("%s: port error, restarting "
421                                                "port %d\n",
422                                                device_get_nameunit(sc->sc_dev), port);
423
424                                 if (up->restartcnt++ < USBD_RESTART_MAX)
425                                         goto disco;
426                                 else
427                                         printf("%s: port error, giving up "
428                                                "port %d\n",
429                                           device_get_nameunit(sc->sc_dev), port);
430                         }
431                 }
432                 if (!(change & UPS_C_CONNECT_STATUS)) {
433                         DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
434                                     "STATUS\n", port));
435                         /* No status change, just do recursive explore. */
436                         if (up->device != NULL && up->device->hub != NULL)
437                                 up->device->hub->explore(up->device);
438 #if 0 && defined(DIAGNOSTIC)
439                         if (up->device == NULL &&
440                             (status & UPS_CURRENT_CONNECT_STATUS))
441                                 printf("%s: connected, no device\n",
442                                        device_get_nameunit(sc->sc_dev));
443 #endif
444                         continue;
445                 }
446
447                 /* We have a connect status change, handle it. */
448
449                 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
450                          dev->address, port));
451                 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
452                 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
453                 /*
454                  * If there is already a device on the port the change status
455                  * must mean that is has disconnected.  Looking at the
456                  * current connect status is not enough to figure this out
457                  * since a new unit may have been connected before we handle
458                  * the disconnect.
459                  */
460         disco:
461                 if (up->device != NULL) {
462                         /* Disconnected */
463                         DPRINTF(("uhub_explore: device addr=%d disappeared "
464                                  "on port %d\n", up->device->address, port));
465                         usb_disconnect_port(up, USBDEV(sc->sc_dev));
466                         usbd_clear_port_feature(dev, port,
467                                                 UHF_C_PORT_CONNECTION);
468                 }
469                 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
470                         /* Nothing connected, just ignore it. */
471                         DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
472                                     "_STATUS\n", port));
473                         continue;
474                 }
475
476                 /* Connected */
477
478                 if (!(status & UPS_PORT_POWER))
479                         printf("%s: strange, connected port %d has no power\n",
480                                device_get_nameunit(sc->sc_dev), port);
481
482                 /* Wait for maximum device power up time. */
483                 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
484
485                 /* Reset port, which implies enabling it. */
486                 if (usbd_reset_port(dev, port, &up->status)) {
487                         printf("%s: port %d reset failed\n",
488                                device_get_nameunit(sc->sc_dev), port);
489                         continue;
490                 }
491                 /* Get port status again, it might have changed during reset */
492                 err = usbd_get_port_status(dev, port, &up->status);
493                 if (err) {
494                         DPRINTF(("uhub_explore: get port status failed, "
495                                  "error=%s\n", usbd_errstr(err)));
496                         continue;
497                 }
498                 status = UGETW(up->status.wPortStatus);
499                 change = UGETW(up->status.wPortChange);
500                 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
501                         /* Nothing connected, just ignore it. */
502 #ifdef DIAGNOSTIC
503                         printf("%s: port %d, device disappeared after reset\n",
504                                device_get_nameunit(sc->sc_dev), port);
505 #endif
506                         continue;
507                 }
508
509 #if 0
510                 if (UHUB_IS_HIGH_SPEED(sc) && !(status & UPS_HIGH_SPEED)) {
511                         printf("%s: port %d, transaction translation not "
512                             "implemented, low/full speed device ignored\n",
513                             device_get_nameunit(sc->sc_dev), port);
514                         continue;
515                 }
516 #endif
517
518                 /* Figure out device speed */
519                 if (status & UPS_HIGH_SPEED)
520                         speed = USB_SPEED_HIGH;
521                 else if (status & UPS_LOW_SPEED)
522                         speed = USB_SPEED_LOW;
523                 else
524                         speed = USB_SPEED_FULL;
525                 /* Get device info and set its address. */
526                 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
527                     dev->depth + 1, speed, port, up);
528                 /* XXX retry a few times? */
529                 if (err) {
530                         DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
531                                      "error=%s\n", usbd_errstr(err)));
532                         /* Avoid addressing problems by disabling. */
533                         /* usbd_reset_port(dev, port, &up->status); */
534
535                         /*
536                          * The unit refused to accept a new address, or had
537                          * some other serious problem.  Since we cannot leave
538                          * at 0 we have to disable the port instead.
539                          */
540                         printf("%s: device problem (%s), disabling port %d\n",
541                                device_get_nameunit(sc->sc_dev), usbd_errstr(err), port);
542                         usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
543                 } else {
544                         /* The port set up succeeded, reset error count. */
545                         up->restartcnt = 0;
546
547                         if (up->device->hub)
548                                 up->device->hub->explore(up->device);
549                 }
550         }
551         return (USBD_NORMAL_COMPLETION);
552 }
553
554 #if defined(__NetBSD__) || defined(__OpenBSD__)
555 int
556 uhub_activate(device_t self, enum devact act)
557 {
558         struct uhub_softc *sc = (struct uhub_softc *)self;
559         struct usbd_hub *hub = sc->sc_hub->hub;
560         usbd_device_handle dev;
561         int nports, port, i;
562
563         switch (act) {
564         case DVACT_ACTIVATE:
565                 return (EOPNOTSUPP);
566
567         case DVACT_DEACTIVATE:
568                 if (hub == NULL) /* malfunctioning hub */
569                         break;
570                 nports = hub->hubdesc.bNbrPorts;
571                 for(port = 0; port < nports; port++) {
572                         dev = hub->ports[port].device;
573                         if (dev != NULL && dev->subdevs != NULL) {
574                                 for (i = 0; dev->subdevs[i] != NULL; i++)
575                                         config_deactivate(dev->subdevs[i]);
576                         }
577                 }
578                 break;
579         }
580         return (0);
581 }
582 #endif
583
584 /*
585  * Called from process context when the hub is gone.
586  * Detach all devices on active ports.
587  */
588 USB_DETACH(uhub)
589 {
590         USB_DETACH_START(uhub, sc);
591         struct usbd_hub *hub = sc->sc_hub->hub;
592         struct usbd_port *rup;
593         int port, nports;
594
595 #if defined(__NetBSD__) || defined(__OpenBSD__)
596         DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
597 #elif defined(__FreeBSD__)
598         DPRINTF(("uhub_detach: sc=%port\n", sc));
599 #endif
600
601         if (hub == NULL)                /* Must be partially working */
602                 return (0);
603
604         usbd_abort_pipe(sc->sc_ipipe);
605         usbd_close_pipe(sc->sc_ipipe);
606
607         nports = hub->hubdesc.bNbrPorts;
608         for(port = 0; port < nports; port++) {
609                 rup = &hub->ports[port];
610                 if (rup->device)
611                         usb_disconnect_port(rup, self);
612         }
613
614         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
615                            USBDEV(sc->sc_dev));
616
617         if (hub->ports[0].tt)
618                 free(hub->ports[0].tt, M_USBDEV);
619         free(hub, M_USBDEV);
620         sc->sc_hub->hub = NULL;
621
622         return (0);
623 }
624
625 #if defined(__FreeBSD__)
626 int
627 uhub_child_location_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         int nports;
634         int port;
635         int i;
636
637         mtx_lock(&Giant);
638         nports = devhub->hub->hubdesc.bNbrPorts;
639         for (port = 0; port < nports; port++) {
640                 dev = devhub->hub->ports[port].device;
641                 if (dev && dev->subdevs) {
642                         for (i = 0; dev->subdevs[i]; i++) {
643                                 if (dev->subdevs[i] == child) {
644                                         if (dev->ifacenums == NULL) {
645                                                 snprintf(buf, buflen,
646                                                     "port=%i", port);
647                                         } else {
648                                                 snprintf(buf, buflen,
649                                                     "port=%i interface=%i",
650                                                     port, dev->ifacenums[i]);
651                                         }
652                                         goto found_dev;
653                                 }
654                         }
655                 }
656         }
657         DPRINTFN(0,("uhub_child_location_str: device not on hub\n"));
658         buf[0] = '\0';
659 found_dev:
660         mtx_unlock(&Giant);
661         return (0);
662 }
663
664 int
665 uhub_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
666     size_t buflen)
667 {
668         struct uhub_softc *sc = device_get_softc(cbdev);
669         usbd_device_handle devhub = sc->sc_hub;
670         usbd_device_handle dev;
671         struct usbd_interface *iface;
672         char serial[128];
673         int nports;
674         int port;
675         int i;
676
677         mtx_lock(&Giant);
678         nports = devhub->hub->hubdesc.bNbrPorts;
679         for (port = 0; port < nports; port++) {
680                 dev = devhub->hub->ports[port].device;
681                 if (dev && dev->subdevs) {
682                         for (i = 0; dev->subdevs[i]; i++) {
683                                 if (dev->subdevs[i] == child) {
684                                         goto found_dev;
685                                 }
686                         }
687                 }
688         }
689         DPRINTFN(0,("uhub_child_pnpinfo_str: device not on hub\n"));
690         buf[0] = '\0';
691         mtx_unlock(&Giant);
692         return (0);
693
694 found_dev:
695         /* XXX can sleep */
696         (void)usbd_get_string(dev, dev->ddesc.iSerialNumber, &serial[0]);
697         if (dev->ifacenums == NULL) {
698                 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
699                     "devclass=0x%02x devsubclass=0x%02x "
700                     "release=0x%04x sernum=\"%s\"",
701                     UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
702                     dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
703                     UGETW(dev->ddesc.bcdDevice), serial);
704         } else {
705                 iface = &dev->ifaces[dev->ifacenums[i]];
706                 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
707                     "devclass=0x%02x devsubclass=0x%02x "
708                     "release=0x%04x sernum=\"%s\" "
709                     "intclass=0x%02x intsubclass=0x%02x",
710                     UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
711                     dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
712                     UGETW(dev->ddesc.bcdDevice), serial,
713                     iface->idesc->bInterfaceClass,
714                     iface->idesc->bInterfaceSubClass);
715         }
716         mtx_unlock(&Giant);
717         return (0);
718 }
719 #endif
720
721
722 /*
723  * Hub interrupt.
724  * This an indication that some port has changed status.
725  * Notify the bus event handler thread that we need
726  * to be explored again.
727  */
728 void
729 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
730 {
731         struct uhub_softc *sc = addr;
732
733         DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
734         if (status == USBD_STALLED)
735                 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
736         else if (status == USBD_NORMAL_COMPLETION)
737                 usb_needs_explore(sc->sc_hub);
738 }
739
740 #if defined(__FreeBSD__)
741 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
742 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
743 #endif