uhub.c revision 1.121 1 /* $NetBSD: uhub.c,v 1.121 2013/01/05 23:34:19 christos Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma Exp $ */
3
4 /*
5 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart (at) augustsson.net) at
10 * Carlstedt Research & Technology.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.121 2013/01/05 23:34:19 christos Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/device.h>
46 #include <sys/proc.h>
47
48 #include <sys/bus.h>
49
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usbdi_util.h>
53 #include <dev/usb/usbdivar.h>
54
55 #ifdef UHUB_DEBUG
56 #define DPRINTF(x) if (uhubdebug) printf x
57 #define DPRINTFN(n,x) if (uhubdebug>(n)) printf x
58 int uhubdebug = 0;
59 #else
60 #define DPRINTF(x)
61 #define DPRINTFN(n,x)
62 #endif
63
64 struct uhub_softc {
65 device_t sc_dev; /* base device */
66 usbd_device_handle sc_hub; /* USB device */
67 int sc_proto; /* device protocol */
68 usbd_pipe_handle sc_ipipe; /* interrupt pipe */
69
70 /* XXX second buffer needed because we can't suspend pipes yet */
71 u_int8_t *sc_statusbuf;
72 u_int8_t *sc_status;
73 size_t sc_statuslen;
74 int sc_explorepending;
75 int sc_isehciroothub; /* see comment in uhub_intr() */
76
77 u_char sc_running;
78 };
79
80 #define UHUB_IS_HIGH_SPEED(sc) ((sc)->sc_proto != UDPROTO_FSHUB)
81 #define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT)
82
83 #define PORTSTAT_ISSET(sc, port) \
84 ((sc)->sc_status[(port) / 8] & (1 << ((port) % 8)))
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
90 /*
91 * We need two attachment points:
92 * hub to usb and hub to hub
93 * Every other driver only connects to hubs
94 */
95
96 int uhub_match(device_t, cfdata_t, void *);
97 void uhub_attach(device_t, device_t, void *);
98 int uhub_rescan(device_t, const char *, const int *);
99 void uhub_childdet(device_t, device_t);
100 int uhub_detach(device_t, int);
101 extern struct cfdriver uhub_cd;
102 CFATTACH_DECL3_NEW(uhub, sizeof(struct uhub_softc), uhub_match,
103 uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet,
104 DVF_DETACH_SHUTDOWN);
105 CFATTACH_DECL2_NEW(uroothub, sizeof(struct uhub_softc), uhub_match,
106 uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet);
107
108 /*
109 * Setting this to 1 makes sure than an uhub attaches even at higher
110 * priority than ugen when ugen_override is set to 1. This allows to
111 * probe the whole USB bus and attach functions with ugen.
112 */
113 int uhub_ubermatch = 0;
114
115 int
116 uhub_match(device_t parent, cfdata_t match, void *aux)
117 {
118 struct usb_attach_arg *uaa = aux;
119 int matchvalue;
120
121 if (uhub_ubermatch)
122 matchvalue = UMATCH_HIGHEST+1;
123 else
124 matchvalue = UMATCH_DEVCLASS_DEVSUBCLASS;
125
126 DPRINTFN(5,("uhub_match, uaa=%p\n", uaa));
127 /*
128 * The subclass for hubs seems to be 0 for some and 1 for others,
129 * so we just ignore the subclass.
130 */
131 if (uaa->class == UDCLASS_HUB)
132 return (matchvalue);
133 return (UMATCH_NONE);
134 }
135
136 void
137 uhub_attach(device_t parent, device_t self, void *aux)
138 {
139 struct uhub_softc *sc = device_private(self);
140 struct usb_attach_arg *uaa = aux;
141 usbd_device_handle dev = uaa->device;
142 char *devinfop;
143 usbd_status err;
144 struct usbd_hub *hub = NULL;
145 usb_device_request_t req;
146 usb_hub_descriptor_t hubdesc;
147 int p, port, nports, nremov, pwrdly;
148 usbd_interface_handle iface;
149 usb_endpoint_descriptor_t *ed;
150 #if 0 /* notyet */
151 struct usbd_tt *tts = NULL;
152 #endif
153
154 DPRINTFN(1,("uhub_attach\n"));
155 sc->sc_dev = self;
156 sc->sc_hub = dev;
157 sc->sc_proto = uaa->proto;
158
159 devinfop = usbd_devinfo_alloc(dev, 1);
160 aprint_naive("\n");
161 aprint_normal(": %s\n", devinfop);
162 usbd_devinfo_free(devinfop);
163
164 if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
165 aprint_normal_dev(self, "%s transaction translator%s\n",
166 UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
167 UHUB_IS_SINGLE_TT(sc) ? "" : "s");
168 }
169
170 err = usbd_set_config_index(dev, 0, 1);
171 if (err) {
172 DPRINTF(("%s: configuration failed, error=%s\n",
173 device_xname(sc->sc_dev), usbd_errstr(err)));
174 return;
175 }
176
177 if (dev->depth > USB_HUB_MAX_DEPTH) {
178 aprint_error_dev(self,
179 "hub depth (%d) exceeded, hub ignored\n",
180 USB_HUB_MAX_DEPTH);
181 return;
182 }
183
184 /* Get hub descriptor. */
185 req.bmRequestType = UT_READ_CLASS_DEVICE;
186 req.bRequest = UR_GET_DESCRIPTOR;
187 USETW2(req.wValue, UDESC_HUB, 0);
188 USETW(req.wIndex, 0);
189 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
190 DPRINTFN(1,("%s: getting hub descriptor\n", __func__));
191 err = usbd_do_request(dev, &req, &hubdesc);
192 nports = hubdesc.bNbrPorts;
193 if (!err && nports > 7) {
194 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
195 err = usbd_do_request(dev, &req, &hubdesc);
196 }
197 if (err) {
198 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
199 device_xname(sc->sc_dev), usbd_errstr(err)));
200 return;
201 }
202
203 for (nremov = 0, port = 1; port <= nports; port++)
204 if (!UHD_NOT_REMOV(&hubdesc, port))
205 nremov++;
206 aprint_verbose_dev(self, "%d port%s with %d removable, %s powered\n",
207 nports, nports != 1 ? "s" : "", nremov,
208 dev->self_powered ? "self" : "bus");
209
210 if (nports == 0) {
211 aprint_debug_dev(self, "no ports, hub ignored\n");
212 goto bad;
213 }
214
215 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
216 M_USBDEV, M_NOWAIT);
217 if (hub == NULL)
218 return;
219 dev->hub = hub;
220 dev->hub->hubsoftc = sc;
221 hub->explore = uhub_explore;
222 hub->hubdesc = hubdesc;
223
224 /* Set up interrupt pipe. */
225 err = usbd_device2interface_handle(dev, 0, &iface);
226 if (err) {
227 aprint_error_dev(self, "no interface handle\n");
228 goto bad;
229 }
230
231 if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
232 err = usbd_set_interface(iface, 1);
233 if (err)
234 aprint_error_dev(self, "can't enable multiple TTs\n");
235 }
236
237 ed = usbd_interface2endpoint_descriptor(iface, 0);
238 if (ed == NULL) {
239 aprint_error_dev(self, "no endpoint descriptor\n");
240 goto bad;
241 }
242 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
243 aprint_error_dev(self, "bad interrupt endpoint\n");
244 goto bad;
245 }
246
247 sc->sc_statuslen = (nports + 1 + 7) / 8;
248 sc->sc_statusbuf = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT);
249 if (!sc->sc_statusbuf)
250 goto bad;
251 sc->sc_status = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT);
252 if (!sc->sc_status)
253 goto bad;
254 if (device_is_a(device_parent(device_parent(sc->sc_dev)), "ehci"))
255 sc->sc_isehciroothub = 1;
256
257 /* force initial scan */
258 memset(sc->sc_status, 0xff, sc->sc_statuslen);
259 sc->sc_explorepending = 1;
260
261 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
262 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_statusbuf,
263 sc->sc_statuslen, uhub_intr, USBD_DEFAULT_INTERVAL);
264 if (err) {
265 aprint_error_dev(self, "cannot open interrupt pipe\n");
266 goto bad;
267 }
268
269 /* Wait with power off for a while. */
270 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
271
272 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev);
273
274 /*
275 * To have the best chance of success we do things in the exact same
276 * order as Windoze98. This should not be necessary, but some
277 * devices do not follow the USB specs to the letter.
278 *
279 * These are the events on the bus when a hub is attached:
280 * Get device and config descriptors (see attach code)
281 * Get hub descriptor (see above)
282 * For all ports
283 * turn on power
284 * wait for power to become stable
285 * (all below happens in explore code)
286 * For all ports
287 * clear C_PORT_CONNECTION
288 * For all ports
289 * get port status
290 * if device connected
291 * wait 100 ms
292 * turn on reset
293 * wait
294 * clear C_PORT_RESET
295 * get port status
296 * proceed with device attachment
297 */
298
299 #if 0
300 if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) {
301 tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
302 sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
303 if (!tts)
304 goto bad;
305 }
306 #endif
307 /* Set up data structures */
308 for (p = 0; p < nports; p++) {
309 struct usbd_port *up = &hub->ports[p];
310 up->device = NULL;
311 up->parent = dev;
312 up->portno = p+1;
313 if (dev->self_powered)
314 /* Self powered hub, give ports maximum current. */
315 up->power = USB_MAX_POWER;
316 else
317 up->power = USB_MIN_POWER;
318 up->restartcnt = 0;
319 up->reattach = 0;
320 #if 0
321 if (UHUB_IS_HIGH_SPEED(sc)) {
322 up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
323 up->tt->hub = hub;
324 } else {
325 up->tt = NULL;
326 }
327 #endif
328 }
329
330 /* XXX should check for none, individual, or ganged power? */
331
332 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
333 + USB_EXTRA_POWER_UP_TIME;
334 for (port = 1; port <= nports; port++) {
335 /* Turn the power on. */
336 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
337 if (err)
338 aprint_error_dev(self, "port %d power on failed, %s\n",
339 port, usbd_errstr(err));
340 DPRINTF(("usb_init_port: turn on port %d power\n", port));
341 }
342
343 /* Wait for stable power if we are not a root hub */
344 if (dev->powersrc->parent != NULL)
345 usbd_delay_ms(dev, pwrdly);
346
347 /* The usual exploration will finish the setup. */
348
349 sc->sc_running = 1;
350
351 if (!pmf_device_register(self, NULL, NULL))
352 aprint_error_dev(self, "couldn't establish power handler\n");
353
354 return;
355
356 bad:
357 if (sc->sc_status)
358 free(sc->sc_status, M_USBDEV);
359 if (sc->sc_statusbuf)
360 free(sc->sc_statusbuf, M_USBDEV);
361 if (hub)
362 free(hub, M_USBDEV);
363 dev->hub = NULL;
364 return;
365 }
366
367 usbd_status
368 uhub_explore(usbd_device_handle dev)
369 {
370 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
371 struct uhub_softc *sc = dev->hub->hubsoftc;
372 struct usbd_port *up;
373 usbd_status err;
374 int speed;
375 int port;
376 int change, status, reconnect;
377
378 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
379
380 if (!sc->sc_running)
381 return (USBD_NOT_STARTED);
382
383 /* Ignore hubs that are too deep. */
384 if (dev->depth > USB_HUB_MAX_DEPTH)
385 return (USBD_TOO_DEEP);
386
387 if (PORTSTAT_ISSET(sc, 0)) { /* hub status change */
388 usb_hub_status_t hs;
389
390 err = usbd_get_hub_status(dev, &hs);
391 if (err) {
392 DPRINTF(("%s: get hub status failed, err=%d\n",
393 device_xname(sc->sc_dev), err));
394 } else {
395 /* just acknowledge */
396 status = UGETW(hs.wHubStatus);
397 change = UGETW(hs.wHubChange);
398 if (change & UHS_LOCAL_POWER)
399 usbd_clear_hub_feature(dev,
400 UHF_C_HUB_LOCAL_POWER);
401 if (change & UHS_OVER_CURRENT)
402 usbd_clear_hub_feature(dev,
403 UHF_C_HUB_OVER_CURRENT);
404 }
405 }
406
407 for (port = 1; port <= hd->bNbrPorts; port++) {
408 up = &dev->hub->ports[port-1];
409
410 /* reattach is needed after firmware upload */
411 reconnect = up->reattach;
412 up->reattach = 0;
413
414 status = change = 0;
415
416 /* don't check if no change summary notification */
417 if (PORTSTAT_ISSET(sc, port) || reconnect) {
418 err = usbd_get_port_status(dev, port, &up->status);
419 if (err) {
420 DPRINTF(("uhub_explore: get port stat failed, "
421 "error=%s\n", usbd_errstr(err)));
422 continue;
423 }
424 status = UGETW(up->status.wPortStatus);
425 change = UGETW(up->status.wPortChange);
426 #if 0
427 printf("%s port %d: s/c=%x/%x\n",
428 device_xname(sc->sc_dev), port, status, change);
429 #endif
430 }
431 if (!change && !reconnect) {
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 continue;
436 }
437
438 if (change & UPS_C_PORT_ENABLED) {
439 DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
440 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
441 if (change & UPS_C_CONNECT_STATUS) {
442 /* Ignore the port error if the device
443 vanished. */
444 } else if (status & UPS_PORT_ENABLED) {
445 aprint_error_dev(sc->sc_dev,
446 "illegal enable change, port %d\n", port);
447 } else {
448 /* Port error condition. */
449 if (up->restartcnt) /* no message first time */
450 aprint_error_dev(sc->sc_dev,
451 "port error, restarting port %d\n",
452 port);
453
454 if (up->restartcnt++ < USBD_RESTART_MAX)
455 goto disco;
456 else
457 aprint_error_dev(sc->sc_dev,
458 "port error, giving up port %d\n",
459 port);
460 }
461 }
462
463 /* XXX handle overcurrent and resume events! */
464
465 if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
466 /* No status change, just do recursive explore. */
467 if (up->device != NULL && up->device->hub != NULL)
468 up->device->hub->explore(up->device);
469 continue;
470 }
471
472 /* We have a connect status change, handle it. */
473
474 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
475 dev->address, port));
476 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
477 /*
478 * If there is already a device on the port the change status
479 * must mean that is has disconnected. Looking at the
480 * current connect status is not enough to figure this out
481 * since a new unit may have been connected before we handle
482 * the disconnect.
483 */
484 disco:
485 if (up->device != NULL) {
486 /* Disconnected */
487 DPRINTF(("uhub_explore: device addr=%d disappeared "
488 "on port %d\n", up->device->address, port));
489 usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
490 usbd_clear_port_feature(dev, port,
491 UHF_C_PORT_CONNECTION);
492 }
493 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
494 /* Nothing connected, just ignore it. */
495 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
496 "_STATUS\n", port));
497 continue;
498 }
499
500 /* Connected */
501
502 if (!(status & UPS_PORT_POWER))
503 aprint_normal_dev(sc->sc_dev,
504 "strange, connected port %d has no power\n", port);
505
506 /* Wait for maximum device power up time. */
507 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
508
509 /* Reset port, which implies enabling it. */
510 if (usbd_reset_port(dev, port, &up->status)) {
511 aprint_error_dev(sc->sc_dev,
512 "port %d reset failed\n", port);
513 continue;
514 }
515 /* Get port status again, it might have changed during reset */
516 err = usbd_get_port_status(dev, port, &up->status);
517 if (err) {
518 DPRINTF(("uhub_explore: get port status failed, "
519 "error=%s\n", usbd_errstr(err)));
520 continue;
521 }
522 status = UGETW(up->status.wPortStatus);
523 change = UGETW(up->status.wPortChange);
524 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
525 /* Nothing connected, just ignore it. */
526 #ifdef DIAGNOSTIC
527 aprint_debug_dev(sc->sc_dev,
528 "port %d, device disappeared after reset\n", port);
529 #endif
530 continue;
531 }
532 if (!(status & UPS_PORT_ENABLED)) {
533 /* Not allowed send/receive packet. */
534 #ifdef DIAGNOSTIC
535 printf("%s: port %d, device not enabled\n",
536 device_xname(sc->sc_dev), port);
537 #endif
538 continue;
539 }
540
541 /* Figure out device speed */
542 if (status & UPS_HIGH_SPEED)
543 speed = USB_SPEED_HIGH;
544 else if (status & UPS_LOW_SPEED)
545 speed = USB_SPEED_LOW;
546 else
547 speed = USB_SPEED_FULL;
548 /* Get device info and set its address. */
549 err = usbd_new_device(sc->sc_dev, dev->bus,
550 dev->depth + 1, speed, port, up);
551 /* XXX retry a few times? */
552 if (err) {
553 DPRINTFN(-1,("uhub_explore: usbd_new_device failed, "
554 "error=%s\n", usbd_errstr(err)));
555 /* Avoid addressing problems by disabling. */
556 /* usbd_reset_port(dev, port, &up->status); */
557
558 /*
559 * The unit refused to accept a new address, or had
560 * some other serious problem. Since we cannot leave
561 * at 0 we have to disable the port instead.
562 */
563 aprint_error_dev(sc->sc_dev,
564 "device problem, disabling port %d\n", port);
565 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
566 } else {
567 /* The port set up succeeded, reset error count. */
568 up->restartcnt = 0;
569
570 if (up->device->hub)
571 up->device->hub->explore(up->device);
572 }
573 }
574 /* enable status change notifications again */
575 if (!sc->sc_isehciroothub)
576 memset(sc->sc_status, 0, sc->sc_statuslen);
577 sc->sc_explorepending = 0;
578 return (USBD_NORMAL_COMPLETION);
579 }
580
581 /*
582 * Called from process context when the hub is gone.
583 * Detach all devices on active ports.
584 */
585 int
586 uhub_detach(device_t self, int flags)
587 {
588 struct uhub_softc *sc = device_private(self);
589 struct usbd_hub *hub = sc->sc_hub->hub;
590 struct usbd_port *rup;
591 int nports, port, rc;
592
593 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
594
595 if (hub == NULL) /* Must be partially working */
596 return (0);
597
598 /* XXXSMP usb */
599 KERNEL_LOCK(1, curlwp);
600
601 nports = hub->hubdesc.bNbrPorts;
602 for(port = 0; port < nports; port++) {
603 rup = &hub->ports[port];
604 if (rup->device == NULL)
605 continue;
606 if ((rc = usb_disconnect_port(rup, self, flags)) != 0) {
607 /* XXXSMP usb */
608 KERNEL_UNLOCK_ONE(curlwp);
609
610 return rc;
611 }
612 }
613
614 pmf_device_deregister(self);
615 usbd_abort_pipe(sc->sc_ipipe);
616 usbd_close_pipe(sc->sc_ipipe);
617
618 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, sc->sc_dev);
619
620 #if 0
621 if (hub->ports[0].tt)
622 free(hub->ports[0].tt, M_USBDEV);
623 #endif
624 free(hub, M_USBDEV);
625 sc->sc_hub->hub = NULL;
626 if (sc->sc_status)
627 free(sc->sc_status, M_USBDEV);
628 if (sc->sc_statusbuf)
629 free(sc->sc_statusbuf, M_USBDEV);
630
631 /* XXXSMP usb */
632 KERNEL_UNLOCK_ONE(curlwp);
633
634 return (0);
635 }
636
637 int
638 uhub_rescan(device_t self, const char *ifattr, const int *locators)
639 {
640 struct uhub_softc *sc = device_private(self);
641 struct usbd_hub *hub = sc->sc_hub->hub;
642 usbd_device_handle dev;
643 int port, err;
644
645 for (port = 0; port < hub->hubdesc.bNbrPorts; port++) {
646 dev = hub->ports[port].device;
647 if (dev == NULL)
648 continue;
649 err = usbd_reattach_device(sc->sc_dev, dev, port, locators);
650 }
651 return 0;
652 }
653
654 /* Called when a device has been detached from it */
655 void
656 uhub_childdet(device_t self, device_t child)
657 {
658 struct uhub_softc *sc = device_private(self);
659 usbd_device_handle devhub = sc->sc_hub;
660 usbd_device_handle dev;
661 int nports;
662 int port;
663 int i;
664
665 if (!devhub->hub)
666 /* should never happen; children are only created after init */
667 panic("hub not fully initialised, but child deleted?");
668
669 nports = devhub->hub->hubdesc.bNbrPorts;
670 for (port = 0; port < nports; port++) {
671 dev = devhub->hub->ports[port].device;
672 if (!dev || dev->subdevlen == 0)
673 continue;
674 for (i = 0; i < dev->subdevlen; i++) {
675 if (dev->subdevs[i] == child) {
676 dev->subdevs[i] = NULL;
677 dev->nifaces_claimed--;
678 }
679 }
680 if (dev->nifaces_claimed == 0) {
681 free(dev->subdevs, M_USB);
682 dev->subdevs = NULL;
683 dev->subdevlen = 0;
684 }
685 }
686 }
687
688
689 /*
690 * Hub interrupt.
691 * This an indication that some port has changed status.
692 * Notify the bus event handler thread that we need
693 * to be explored again.
694 */
695 void
696 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
697 {
698 struct uhub_softc *sc = addr;
699
700 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
701
702 if (status == USBD_STALLED)
703 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
704 else if (status == USBD_NORMAL_COMPLETION &&
705 !sc->sc_explorepending) {
706 /*
707 * Make sure the status is not overwritten in between.
708 * XXX we should suspend the pipe instead
709 */
710 memcpy(sc->sc_status, sc->sc_statusbuf, sc->sc_statuslen);
711 sc->sc_explorepending = 1;
712 usb_needs_explore(sc->sc_hub);
713 }
714 /*
715 * XXX workaround for broken implementation of the interrupt
716 * pipe in EHCI root hub emulation which doesn't resend
717 * status change notifications until handled: force a rescan
718 * of the ports we touched in the last run
719 */
720 if (status == USBD_NORMAL_COMPLETION && sc->sc_explorepending &&
721 sc->sc_isehciroothub)
722 usb_needs_explore(sc->sc_hub);
723 }
724