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