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