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