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