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