Rename sprintf -> ksprintf
[dfdiff.git] / sys / bus / usb / uhub.c
blobbcaaa2f23cdd412258ba71da06e5fe55b4c3c5c4
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.11 2006/12/20 18:14:37 dillon Exp $ */
5 /*
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * All rights reserved.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
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.
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.
43 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #if defined(__NetBSD__) || defined(__OpenBSD__)
51 #include <sys/device.h>
52 #include <sys/proc.h>
53 #elif defined(__FreeBSD__) || defined(__DragonFly__)
54 #include <sys/module.h>
55 #include <sys/bus.h>
56 #include <sys/lock.h>
57 #ifdef __FreeBSD__
58 #include <sys/mutex.h>
59 #else
60 #include <sys/thread2.h>
61 #endif
62 #endif
63 #include <sys/sysctl.h>
65 #include <bus/usb/usb.h>
66 #include <bus/usb/usbdi.h>
67 #include <bus/usb/usbdi_util.h>
68 #include <bus/usb/usbdivar.h>
70 #define UHUB_INTR_INTERVAL 255 /* ms */
72 #ifdef USB_DEBUG
73 #define DPRINTF(x) if (uhubdebug) logprintf x
74 #define DPRINTFN(n,x) if (uhubdebug>(n)) logprintf x
75 int uhubdebug = 0;
76 SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB uhub");
77 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW,
78 &uhubdebug, 0, "uhub debug level");
79 #else
80 #define DPRINTF(x)
81 #define DPRINTFN(n,x)
82 #endif
84 struct uhub_softc {
85 USBBASEDEVICE sc_dev; /* base device */
86 usbd_device_handle sc_hub; /* USB device */
87 usbd_pipe_handle sc_ipipe; /* interrupt pipe */
88 u_int8_t sc_status[1]; /* XXX more ports */
89 u_char sc_running;
91 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol)
92 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
93 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
95 Static usbd_status uhub_explore(usbd_device_handle hub);
96 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
98 #if defined(__FreeBSD__) || defined(__DragonFly__)
99 Static bus_child_detached_t uhub_child_detached;
100 Static bus_child_location_str_t uhub_child_location_str;
101 Static bus_child_pnpinfo_str_t uhub_child_pnpinfo_str;
102 #endif
106 * We need two attachment points:
107 * hub to usb and hub to hub
108 * Every other driver only connects to hubs
111 #if defined(__NetBSD__) || defined(__OpenBSD__)
112 USB_DECLARE_DRIVER(uhub);
114 /* Create the driver instance for the hub connected to hub case */
115 CFATTACH_DECL(uhub_uhub, sizeof(struct uhub_softc),
116 uhub_match, uhub_attach, uhub_detach, uhub_activate);
117 #elif defined(__FreeBSD__) || defined(__DragonFly__)
118 USB_DECLARE_DRIVER_INIT(uhub,
119 DEVMETHOD(bus_child_detached, uhub_child_detached),
120 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
121 DEVMETHOD(bus_child_location_str, uhub_child_location_str),
122 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
123 DEVMETHOD(device_suspend, bus_generic_suspend),
124 DEVMETHOD(device_resume, bus_generic_resume),
125 DEVMETHOD(device_shutdown, bus_generic_shutdown)
128 /* Create the driver instance for the hub connected to usb case. */
129 devclass_t uhubroot_devclass;
131 Static device_method_t uhubroot_methods[] = {
132 DEVMETHOD(bus_child_detached, uhub_child_detached),
133 DEVMETHOD(bus_child_location_str, uhub_child_location_str),
134 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
135 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
137 DEVMETHOD(device_probe, uhub_match),
138 DEVMETHOD(device_attach, uhub_attach),
139 DEVMETHOD(device_detach, uhub_detach),
140 DEVMETHOD(device_suspend, bus_generic_suspend),
141 DEVMETHOD(device_resume, bus_generic_resume),
142 DEVMETHOD(device_shutdown, bus_generic_shutdown),
143 {0,0}
146 Static driver_t uhubroot_driver = {
147 "uhub",
148 uhubroot_methods,
149 sizeof(struct uhub_softc)
151 #endif
153 USB_MATCH(uhub)
155 USB_MATCH_START(uhub, uaa);
156 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
158 DPRINTFN(5,("uhub_match, dd=%p\n", dd));
160 * The subclass for hubs seems to be 0 for some and 1 for others,
161 * so we just ignore the subclass.
163 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
164 return (UMATCH_DEVCLASS_DEVSUBCLASS);
165 return (UMATCH_NONE);
168 USB_ATTACH(uhub)
170 USB_ATTACH_START(uhub, sc, uaa);
171 usbd_device_handle dev = uaa->device;
172 char *devinfo;
173 usbd_status err;
174 struct usbd_hub *hub = NULL;
175 usb_device_request_t req;
176 usb_hub_descriptor_t hubdesc;
177 int p, port, nports, nremov, pwrdly;
178 usbd_interface_handle iface;
179 usb_endpoint_descriptor_t *ed;
180 struct usbd_tt *tts = NULL;
182 devinfo = kmalloc(1024, M_TEMP, M_WAITOK);
183 DPRINTFN(1,("uhub_attach\n"));
184 sc->sc_hub = dev;
185 usbd_devinfo(dev, 1, devinfo);
186 USB_ATTACH_SETUP;
188 if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
189 printf("%s: %s transaction translator%s\n",
190 USBDEVNAME(sc->sc_dev),
191 UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
192 UHUB_IS_SINGLE_TT(sc) ? "" : "s");
194 err = usbd_set_config_index(dev, 0, 1);
195 if (err) {
196 DPRINTF(("%s: configuration failed, error=%s\n",
197 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
198 kfree(devinfo, M_TEMP);
199 USB_ATTACH_ERROR_RETURN;
202 if (dev->depth > USB_HUB_MAX_DEPTH) {
203 printf("%s: hub depth (%d) exceeded, hub ignored\n",
204 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
205 kfree(devinfo, M_TEMP);
206 USB_ATTACH_ERROR_RETURN;
209 /* Get hub descriptor. */
210 req.bmRequestType = UT_READ_CLASS_DEVICE;
211 req.bRequest = UR_GET_DESCRIPTOR;
212 USETW2(req.wValue, (dev->address > 1 ? UDESC_HUB : 0), 0);
213 USETW(req.wIndex, 0);
214 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
215 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
216 err = usbd_do_request(dev, &req, &hubdesc);
217 nports = hubdesc.bNbrPorts;
218 if (!err && nports > 7) {
219 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
220 err = usbd_do_request(dev, &req, &hubdesc);
222 if (err) {
223 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
224 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
225 kfree(devinfo, M_TEMP);
226 USB_ATTACH_ERROR_RETURN;
229 for (nremov = 0, port = 1; port <= nports; port++)
230 if (!UHD_NOT_REMOV(&hubdesc, port))
231 nremov++;
232 printf("%s: %d port%s with %d removable, %s powered\n",
233 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
234 nremov, dev->self_powered ? "self" : "bus");
236 if (nports == 0) {
237 printf("%s: no ports, hub ignored\n", USBDEVNAME(sc->sc_dev));
238 goto bad;
241 hub = kmalloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
242 M_USBDEV, M_WAITOK);
243 dev->hub = hub;
244 dev->hub->hubsoftc = sc;
245 hub->explore = uhub_explore;
246 hub->hubdesc = hubdesc;
248 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
249 "parent->selfpowered=%d\n",
250 dev->self_powered, dev->powersrc->parent,
251 dev->powersrc->parent ?
252 dev->powersrc->parent->self_powered : 0));
254 if (!dev->self_powered && dev->powersrc->parent != NULL &&
255 !dev->powersrc->parent->self_powered) {
256 printf("%s: bus powered hub connected to bus powered hub, "
257 "ignored\n", USBDEVNAME(sc->sc_dev));
258 goto bad;
261 /* Set up interrupt pipe. */
262 err = usbd_device2interface_handle(dev, 0, &iface);
263 if (err) {
264 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
265 goto bad;
267 ed = usbd_interface2endpoint_descriptor(iface, 0);
268 if (ed == NULL) {
269 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
270 goto bad;
272 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
273 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
274 goto bad;
277 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
278 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
279 sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
280 if (err) {
281 printf("%s: cannot open interrupt pipe\n",
282 USBDEVNAME(sc->sc_dev));
283 goto bad;
286 /* Wait with power off for a while. */
287 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
289 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
292 * To have the best chance of success we do things in the exact same
293 * order as Windoze98. This should not be necessary, but some
294 * devices do not follow the USB specs to the letter.
296 * These are the events on the bus when a hub is attached:
297 * Get device and config descriptors (see attach code)
298 * Get hub descriptor (see above)
299 * For all ports
300 * turn on power
301 * wait for power to become stable
302 * (all below happens in explore code)
303 * For all ports
304 * clear C_PORT_CONNECTION
305 * For all ports
306 * get port status
307 * if device connected
308 * wait 100 ms
309 * turn on reset
310 * wait
311 * clear C_PORT_RESET
312 * get port status
313 * proceed with device attachment
316 if (UHUB_IS_HIGH_SPEED(sc)) {
317 tts = kmalloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
318 sizeof(struct usbd_tt), M_USBDEV, M_WAITOK);
321 /* Set up data structures */
322 for (p = 0; p < nports; p++) {
323 struct usbd_port *up = &hub->ports[p];
324 up->device = NULL;
325 up->parent = dev;
326 up->portno = p+1;
327 if (dev->self_powered)
328 /* Self powered hub, give ports maximum current. */
329 up->power = USB_MAX_POWER;
330 else
331 up->power = USB_MIN_POWER;
332 up->restartcnt = 0;
333 if (UHUB_IS_HIGH_SPEED(sc)) {
334 up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
335 up->tt->hub = hub;
336 } else {
337 up->tt = NULL;
341 /* XXX should check for none, individual, or ganged power? */
343 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
344 + USB_EXTRA_POWER_UP_TIME;
345 for (port = 1; port <= nports; port++) {
346 /* Turn the power on. */
347 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
348 if (err)
349 printf("%s: port %d power on failed, %s\n",
350 USBDEVNAME(sc->sc_dev), port,
351 usbd_errstr(err));
352 DPRINTF(("usb_init_port: turn on port %d power\n", port));
353 /* Wait for stable power. */
354 usbd_delay_ms(dev, pwrdly);
357 /* The usual exploration will finish the setup. */
359 sc->sc_running = 1;
361 USB_ATTACH_SUCCESS_RETURN;
363 bad:
364 if (hub)
365 kfree(hub, M_USBDEV);
366 kfree(devinfo, M_TEMP);
367 dev->hub = NULL;
368 USB_ATTACH_ERROR_RETURN;
371 usbd_status
372 uhub_explore(usbd_device_handle dev)
374 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
375 struct uhub_softc *sc = dev->hub->hubsoftc;
376 struct usbd_port *up;
377 usbd_status err;
378 int speed;
379 int port;
380 int change, status;
382 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
384 if (!sc->sc_running)
385 return (USBD_NOT_STARTED);
387 /* Ignore hubs that are too deep. */
388 if (dev->depth > USB_HUB_MAX_DEPTH)
389 return (USBD_TOO_DEEP);
391 for(port = 1; port <= hd->bNbrPorts; port++) {
392 up = &dev->hub->ports[port-1];
393 err = usbd_get_port_status(dev, port, &up->status);
394 if (err) {
395 DPRINTF(("uhub_explore: get port status failed, "
396 "error=%s\n", usbd_errstr(err)));
397 continue;
399 status = UGETW(up->status.wPortStatus);
400 change = UGETW(up->status.wPortChange);
401 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
402 USBDEVNAME(sc->sc_dev), port, status, change));
403 if (change & UPS_C_PORT_ENABLED) {
404 DPRINTF(("uhub_explore: C_PORT_ENABLED 0x%x\n", change));
405 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
406 if (change & UPS_C_CONNECT_STATUS) {
407 /* Ignore the port error if the device
408 vanished. */
409 } else if (status & UPS_PORT_ENABLED) {
410 printf("%s: illegal enable change, port %d\n",
411 USBDEVNAME(sc->sc_dev), port);
412 } else {
413 /* Port error condition. */
414 if (up->restartcnt) /* no message first time */
415 printf("%s: port error, restarting "
416 "port %d\n",
417 USBDEVNAME(sc->sc_dev), port);
419 if (up->restartcnt++ < USBD_RESTART_MAX)
420 goto disco;
421 else
422 printf("%s: port error, giving up "
423 "port %d\n",
424 USBDEVNAME(sc->sc_dev), port);
427 if (!(change & UPS_C_CONNECT_STATUS)) {
428 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
429 "STATUS\n", port));
430 /* No status change, just do recursive explore. */
431 if (up->device != NULL && up->device->hub != NULL)
432 up->device->hub->explore(up->device);
433 #if 0 && defined(DIAGNOSTIC)
434 if (up->device == NULL &&
435 (status & UPS_CURRENT_CONNECT_STATUS))
436 printf("%s: connected, no device\n",
437 USBDEVNAME(sc->sc_dev));
438 #endif
439 continue;
442 /* We have a connect status change, handle it. */
444 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
445 dev->address, port));
446 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
447 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
449 * If there is already a device on the port the change status
450 * must mean that is has disconnected. Looking at the
451 * current connect status is not enough to figure this out
452 * since a new unit may have been connected before we handle
453 * the disconnect.
455 disco:
456 if (up->device != NULL) {
457 /* Disconnected */
458 DPRINTF(("uhub_explore: device addr=%d disappeared "
459 "on port %d\n", up->device->address, port));
460 usb_disconnect_port(up, USBDEV(sc->sc_dev));
461 usbd_clear_port_feature(dev, port,
462 UHF_C_PORT_CONNECTION);
464 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
465 /* Nothing connected, just ignore it. */
466 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
467 "_STATUS\n", port));
468 continue;
471 /* Connected */
473 if (!(status & UPS_PORT_POWER))
474 printf("%s: strange, connected port %d has no power\n",
475 USBDEVNAME(sc->sc_dev), port);
477 /* Wait for maximum device power up time. */
478 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
480 /* Reset port, which implies enabling it. */
481 if (usbd_reset_port(dev, port, &up->status)) {
482 printf("%s: port %d reset failed\n",
483 USBDEVNAME(sc->sc_dev), port);
484 continue;
486 /* Get port status again, it might have changed during reset */
487 err = usbd_get_port_status(dev, port, &up->status);
488 if (err) {
489 DPRINTF(("uhub_explore: get port status failed, "
490 "error=%s\n", usbd_errstr(err)));
491 continue;
493 status = UGETW(up->status.wPortStatus);
494 change = UGETW(up->status.wPortChange);
495 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
496 /* Nothing connected, just ignore it. */
497 #ifdef DIAGNOSTIC
498 printf("%s: port %d, device disappeared after reset\n",
499 USBDEVNAME(sc->sc_dev), port);
500 #endif
501 continue;
504 #if 0
505 if (UHUB_IS_HIGH_SPEED(sc) && !(status & UPS_HIGH_SPEED)) {
506 printf("%s: port %d, transaction translation not "
507 "implemented, low/full speed device ignored\n",
508 USBDEVNAME(sc->sc_dev), port);
509 continue;
511 #endif
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(USBDEV(sc->sc_dev), 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); */
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.
535 printf("%s: device problem (%s), disabling port %d\n",
536 USBDEVNAME(sc->sc_dev), usbd_errstr(err), port);
537 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
538 } else {
539 /* The port set up succeeded, reset error count. */
540 up->restartcnt = 0;
542 if (up->device->hub)
543 up->device->hub->explore(up->device);
546 return (USBD_NORMAL_COMPLETION);
549 #if defined(__NetBSD__) || defined(__OpenBSD__)
551 uhub_activate(device_ptr_t self, enum devact act)
553 struct uhub_softc *sc = (struct uhub_softc *)self;
554 struct usbd_hub *hub = sc->sc_hub->hub;
555 usbd_device_handle dev;
556 int nports, port, i;
558 switch (act) {
559 case DVACT_ACTIVATE:
560 return (EOPNOTSUPP);
562 case DVACT_DEACTIVATE:
563 if (hub == NULL) /* malfunctioning hub */
564 break;
565 nports = hub->hubdesc.bNbrPorts;
566 for(port = 0; port < nports; port++) {
567 dev = hub->ports[port].device;
568 if (dev != NULL && dev->subdevs != NULL) {
569 for (i = 0; dev->subdevs[i] != NULL; i++)
570 config_deactivate(dev->subdevs[i]);
573 break;
575 return (0);
577 #endif
580 * Called from process context when the hub is gone.
581 * Detach all devices on active ports.
583 USB_DETACH(uhub)
585 USB_DETACH_START(uhub, sc);
586 struct usbd_hub *hub = sc->sc_hub->hub;
587 struct usbd_port *rup;
588 int port, nports;
590 #if defined(__NetBSD__) || defined(__OpenBSD__)
591 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
592 #elif defined(__FreeBSD__) || defined(__DragonFly__)
593 DPRINTF(("uhub_detach: sc=%port\n", sc));
594 #endif
596 crit_enter();
598 if (hub == NULL) { /* Must be partially working */
599 crit_exit();
600 return (0);
603 usbd_abort_pipe(sc->sc_ipipe);
604 usbd_close_pipe(sc->sc_ipipe);
606 nports = hub->hubdesc.bNbrPorts;
607 for(port = 0; port < nports; port++) {
608 rup = &hub->ports[port];
609 if (rup->device)
610 usb_disconnect_port(rup, self);
613 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
614 USBDEV(sc->sc_dev));
616 if (hub->ports[0].tt)
617 kfree(hub->ports[0].tt, M_USBDEV);
618 kfree(hub, M_USBDEV);
619 sc->sc_hub->hub = NULL;
621 crit_exit();
623 return (0);
626 #if defined(__FreeBSD__) || defined(__DragonFly__)
628 uhub_child_location_str(device_t cbdev, device_t child, char *buf,
629 size_t buflen)
631 struct uhub_softc *sc = device_get_softc(cbdev);
632 usbd_device_handle devhub = sc->sc_hub;
633 usbd_device_handle dev;
634 int nports;
635 int port;
636 int i;
638 crit_enter();
639 nports = devhub->hub->hubdesc.bNbrPorts;
640 for (port = 0; port < nports; port++) {
641 dev = devhub->hub->ports[port].device;
642 if (dev && dev->subdevs) {
643 for (i = 0; dev->subdevs[i]; i++) {
644 if (dev->subdevs[i] == child) {
645 if (dev->ifacenums == NULL) {
646 ksnprintf(buf, buflen,
647 "port=%i", port);
648 } else {
649 ksnprintf(buf, buflen,
650 "port=%i interface=%i",
651 port, dev->ifacenums[i]);
653 goto found_dev;
658 DPRINTFN(0,("uhub_child_location_str: device not on hub\n"));
659 buf[0] = '\0';
660 found_dev:
661 crit_exit();
662 return (0);
666 uhub_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
667 size_t buflen)
669 struct uhub_softc *sc = device_get_softc(cbdev);
670 usbd_device_handle devhub = sc->sc_hub;
671 usbd_device_handle dev;
672 struct usbd_interface *iface;
673 char serial[128];
674 int nports;
675 int port;
676 int i;
678 crit_enter();
679 nports = devhub->hub->hubdesc.bNbrPorts;
680 for (port = 0; port < nports; port++) {
681 dev = devhub->hub->ports[port].device;
682 if (dev && dev->subdevs) {
683 for (i = 0; dev->subdevs[i]; i++) {
684 if (dev->subdevs[i] == child) {
685 goto found_dev;
690 DPRINTFN(0,("uhub_child_pnpinfo_str: device not on hub\n"));
691 buf[0] = '\0';
692 crit_exit();
693 return (0);
695 found_dev:
696 /* XXX can sleep */
697 (void)usbd_get_string(dev, dev->ddesc.iSerialNumber, &serial[0]);
698 if (dev->ifacenums == NULL) {
699 ksnprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
700 "devclass=0x%02x devsubclass=0x%02x "
701 "release=0x%04x sernum=\"%s\"",
702 UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
703 dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
704 UGETW(dev->ddesc.bcdDevice), serial);
705 } else {
706 iface = &dev->ifaces[dev->ifacenums[i]];
707 ksnprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
708 "devclass=0x%02x devsubclass=0x%02x "
709 "release=0x%04x sernum=\"%s\" "
710 "intclass=0x%02x intsubclass=0x%02x",
711 UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
712 dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
713 UGETW(dev->ddesc.bcdDevice), serial,
714 iface->idesc->bInterfaceClass,
715 iface->idesc->bInterfaceSubClass);
717 crit_exit();
718 return (0);
721 Static void
722 uhub_child_detached(device_t self, device_t child)
724 struct uhub_softc *sc = device_get_softc(self);
725 usbd_device_handle devhub = sc->sc_hub;
726 usbd_device_handle dev = NULL;
727 int nports, port, i = 0;
729 crit_enter();
730 nports = devhub->hub->hubdesc.bNbrPorts;
731 for (port = 0; port < nports; port++) {
732 dev = devhub->hub->ports[port].device;
733 if (dev && dev->subdevs) {
734 for (i = 0; dev->subdevs[i]; ++i) {
735 if (dev->subdevs[i] == child)
736 goto found_dev;
740 crit_exit();
741 return;
743 found_dev:
744 #if 0
745 printf("%s: at %s", USBDEVPTRNAME(dev->subdevs[i]),
746 USBDEVPTRNAME(self));
747 if (port != 0)
748 printf(" port %d", port);
749 printf(" (addr %d) disconnected\n", dev->address);
750 #endif
751 kfree(device_get_ivars(dev->subdevs[i]), M_USB);
752 dev->subdevs[i] = NULL;
753 crit_exit();
755 #endif
759 * Hub interrupt.
760 * This an indication that some port has changed status.
761 * Notify the bus event handler thread that we need
762 * to be explored again.
764 void
765 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
767 struct uhub_softc *sc = addr;
769 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
770 if (status == USBD_STALLED)
771 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
772 else if (status == USBD_NORMAL_COMPLETION)
773 usb_needs_explore(sc->sc_hub);
776 #if defined(__FreeBSD__) || defined(__DragonFly__)
777 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
778 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
779 #endif