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