uhub.c revision 1.50.2.1 1 /* $NetBSD: uhub.c,v 1.50.2.1 2001/11/12 21:18:32 thorpej 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 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/data/usbspec.zip
43 */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #if defined(__NetBSD__) || defined(__OpenBSD__)
50 #include <sys/device.h>
51 #include <sys/proc.h>
52 #elif defined(__FreeBSD__)
53 #include <sys/module.h>
54 #include <sys/bus.h>
55 #include "bus_if.h"
56 #endif
57
58 #include <machine/bus.h>
59
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62 #include <dev/usb/usbdi_util.h>
63 #include <dev/usb/usbdivar.h>
64
65 #define UHUB_INTR_INTERVAL 255 /* ms */
66
67 #ifdef UHUB_DEBUG
68 #define DPRINTF(x) if (uhubdebug) logprintf x
69 #define DPRINTFN(n,x) if (uhubdebug>(n)) logprintf x
70 int uhubdebug;
71 #else
72 #define DPRINTF(x)
73 #define DPRINTFN(n,x)
74 #endif
75
76 struct uhub_softc {
77 USBBASEDEVICE sc_dev; /* base device */
78 usbd_device_handle sc_hub; /* USB device */
79 usbd_pipe_handle sc_ipipe; /* interrupt pipe */
80 u_int8_t sc_status[1]; /* XXX more ports */
81 u_char sc_running;
82 };
83
84 Static usbd_status uhub_explore(usbd_device_handle hub);
85 Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
86
87 #if defined(__FreeBSD__)
88 Static bus_child_detached_t uhub_child_detached;
89 #endif
90
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 #if defined(__NetBSD__) || defined(__OpenBSD__)
99 USB_DECLARE_DRIVER(uhub);
100
101 /* Create the driver instance for the hub connected to hub case */
102 struct cfattach uhub_uhub_ca = {
103 sizeof(struct uhub_softc), uhub_match, uhub_attach,
104 uhub_detach, uhub_activate
105 };
106 #elif defined(__FreeBSD__)
107 USB_DECLARE_DRIVER_INIT(uhub,
108 DEVMETHOD(bus_child_detached, uhub_child_detached));
109
110 /* Create the driver instance for the hub connected to usb case. */
111 devclass_t uhubroot_devclass;
112
113 Static device_method_t uhubroot_methods[] = {
114 DEVMETHOD(device_probe, uhub_match),
115 DEVMETHOD(device_attach, uhub_attach),
116
117 /* detach is not allowed for a root hub */
118 {0,0}
119 };
120
121 Static driver_t uhubroot_driver = {
122 "uhub",
123 uhubroot_methods,
124 sizeof(struct uhub_softc)
125 };
126 #endif
127
128 USB_MATCH(uhub)
129 {
130 USB_MATCH_START(uhub, uaa);
131 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
132
133 DPRINTFN(5,("uhub_match, dd=%p\n", dd));
134 /*
135 * The subclass for hubs seems to be 0 for some and 1 for others,
136 * so we just ignore the subclass.
137 */
138 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
139 return (UMATCH_DEVCLASS_DEVSUBCLASS);
140 return (UMATCH_NONE);
141 }
142
143 USB_ATTACH(uhub)
144 {
145 USB_ATTACH_START(uhub, sc, uaa);
146 usbd_device_handle dev = uaa->device;
147 char devinfo[1024];
148 usbd_status err;
149 struct usbd_hub *hub;
150 usb_device_request_t req;
151 usb_hub_descriptor_t hubdesc;
152 int p, port, nports, nremov, pwrdly;
153 usbd_interface_handle iface;
154 usb_endpoint_descriptor_t *ed;
155
156 DPRINTFN(1,("uhub_attach\n"));
157 sc->sc_hub = dev;
158 usbd_devinfo(dev, 1, devinfo);
159 USB_ATTACH_SETUP;
160 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
161
162 err = usbd_set_config_index(dev, 0, 1);
163 if (err) {
164 DPRINTF(("%s: configuration failed, error=%s\n",
165 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
166 USB_ATTACH_ERROR_RETURN;
167 }
168
169 if (dev->depth > USB_HUB_MAX_DEPTH) {
170 printf("%s: hub depth (%d) exceeded, hub ignored\n",
171 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
172 USB_ATTACH_ERROR_RETURN;
173 }
174
175 /* Get hub descriptor. */
176 req.bmRequestType = UT_READ_CLASS_DEVICE;
177 req.bRequest = UR_GET_DESCRIPTOR;
178 USETW(req.wValue, 0);
179 USETW(req.wIndex, 0);
180 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
181 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
182 err = usbd_do_request(dev, &req, &hubdesc);
183 nports = hubdesc.bNbrPorts;
184 if (!err && nports > 7) {
185 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
186 err = usbd_do_request(dev, &req, &hubdesc);
187 }
188 if (err) {
189 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
190 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
191 USB_ATTACH_ERROR_RETURN;
192 }
193
194 for (nremov = 0, port = 1; port <= nports; port++)
195 if (!UHD_NOT_REMOV(&hubdesc, port))
196 nremov++;
197 printf("%s: %d port%s with %d removable, %s powered\n",
198 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
199 nremov, dev->self_powered ? "self" : "bus");
200
201 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
202 M_USBDEV, M_NOWAIT);
203 if (hub == NULL)
204 USB_ATTACH_ERROR_RETURN;
205 dev->hub = hub;
206 dev->hub->hubsoftc = sc;
207 hub->explore = uhub_explore;
208 hub->hubdesc = hubdesc;
209
210 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
211 "parent->selfpowered=%d\n",
212 dev->self_powered, dev->powersrc->parent,
213 dev->powersrc->parent ?
214 dev->powersrc->parent->self_powered : 0));
215
216 if (!dev->self_powered && dev->powersrc->parent != NULL &&
217 !dev->powersrc->parent->self_powered) {
218 printf("%s: bus powered hub connected to bus powered hub, "
219 "ignored\n", USBDEVNAME(sc->sc_dev));
220 goto bad;
221 }
222
223 /* Set up interrupt pipe. */
224 err = usbd_device2interface_handle(dev, 0, &iface);
225 if (err) {
226 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
227 goto bad;
228 }
229 ed = usbd_interface2endpoint_descriptor(iface, 0);
230 if (ed == NULL) {
231 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
232 goto bad;
233 }
234 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
235 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
236 goto bad;
237 }
238
239 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
240 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
241 sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
242 if (err) {
243 printf("%s: cannot open interrupt pipe\n",
244 USBDEVNAME(sc->sc_dev));
245 goto bad;
246 }
247
248 /* Wait with power off for a while. */
249 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
250
251 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
252
253 /*
254 * To have the best chance of success we do things in the exact same
255 * order as Windoze98. This should not be necessary, but some
256 * devices do not follow the USB specs to the letter.
257 *
258 * These are the events on the bus when a hub is attached:
259 * Get device and config descriptors (see attach code)
260 * Get hub descriptor (see above)
261 * For all ports
262 * turn on power
263 * wait for power to become stable
264 * (all below happens in explore code)
265 * For all ports
266 * clear C_PORT_CONNECTION
267 * For all ports
268 * get port status
269 * if device connected
270 * turn on reset
271 * wait
272 * clear C_PORT_RESET
273 * get port status
274 * proceed with device attachment
275 */
276
277 /* Set up data structures */
278 for (p = 0; p < nports; p++) {
279 struct usbd_port *up = &hub->ports[p];
280 up->device = 0;
281 up->parent = dev;
282 up->portno = p+1;
283 if (dev->self_powered)
284 /* Self powered hub, give ports maximum current. */
285 up->power = USB_MAX_POWER;
286 else
287 up->power = USB_MIN_POWER;
288 }
289
290 /* XXX should check for none, individual, or ganged power? */
291
292 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
293 + USB_EXTRA_POWER_UP_TIME;
294 for (port = 1; port <= nports; port++) {
295 /* Turn the power on. */
296 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
297 if (err)
298 printf("%s: port %d power on failed, %s\n",
299 USBDEVNAME(sc->sc_dev), port,
300 usbd_errstr(err));
301 DPRINTF(("usb_init_port: turn on port %d power\n", port));
302 /* Wait for stable power. */
303 usbd_delay_ms(dev, pwrdly);
304 }
305
306 /* The usual exploration will finish the setup. */
307
308 sc->sc_running = 1;
309
310 USB_ATTACH_SUCCESS_RETURN;
311
312 bad:
313 free(hub, M_USBDEV);
314 dev->hub = 0;
315 USB_ATTACH_ERROR_RETURN;
316 }
317
318 usbd_status
319 uhub_explore(usbd_device_handle dev)
320 {
321 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
322 struct uhub_softc *sc = dev->hub->hubsoftc;
323 struct usbd_port *up;
324 usbd_status err;
325 int port;
326 int change, status;
327
328 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
329
330 if (!sc->sc_running)
331 return (USBD_NOT_STARTED);
332
333 /* Ignore hubs that are too deep. */
334 if (dev->depth > USB_HUB_MAX_DEPTH)
335 return (USBD_TOO_DEEP);
336
337 for(port = 1; port <= hd->bNbrPorts; port++) {
338 up = &dev->hub->ports[port-1];
339 err = usbd_get_port_status(dev, port, &up->status);
340 if (err) {
341 DPRINTF(("uhub_explore: get port status failed, "
342 "error=%s\n", usbd_errstr(err)));
343 continue;
344 }
345 status = UGETW(up->status.wPortStatus);
346 change = UGETW(up->status.wPortChange);
347 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
348 USBDEVNAME(sc->sc_dev), port, status, change));
349 if (change & UPS_C_PORT_ENABLED) {
350 DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
351 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
352 if (status & UPS_PORT_ENABLED) {
353 printf("%s: illegal enable change, port %d\n",
354 USBDEVNAME(sc->sc_dev), port);
355 } else {
356 /* Port error condition. */
357 if (up->restartcnt) /* no message first time */
358 printf("%s: port error, restarting "
359 "port %d\n",
360 USBDEVNAME(sc->sc_dev), port);
361
362 if (up->restartcnt++ < USBD_RESTART_MAX)
363 goto disco;
364 else
365 printf("%s: port error, giving up "
366 "port %d\n",
367 USBDEVNAME(sc->sc_dev), port);
368 }
369 }
370 if (!(change & UPS_C_CONNECT_STATUS)) {
371 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
372 "STATUS\n", port));
373 /* No status change, just do recursive explore. */
374 if (up->device != NULL && up->device->hub != NULL)
375 up->device->hub->explore(up->device);
376 #if 0 && defined(DIAGNOSTIC)
377 if (up->device == NULL &&
378 (status & UPS_CURRENT_CONNECT_STATUS))
379 printf("%s: connected, no device\n",
380 USBDEVNAME(sc->sc_dev));
381 #endif
382 continue;
383 }
384
385 /* We have a connect status change, handle it. */
386
387 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
388 dev->address, port));
389 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
390 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
391 /*
392 * If there is already a device on the port the change status
393 * must mean that is has disconnected. Looking at the
394 * current connect status is not enough to figure this out
395 * since a new unit may have been connected before we handle
396 * the disconnect.
397 */
398 disco:
399 if (up->device != NULL) {
400 /* Disconnected */
401 DPRINTF(("uhub_explore: device addr=%d disappeared "
402 "on port %d\n", up->device->address, port));
403 usb_disconnect_port(up, USBDEV(sc->sc_dev));
404 usbd_clear_port_feature(dev, port,
405 UHF_C_PORT_CONNECTION);
406 }
407 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
408 /* Nothing connected, just ignore it. */
409 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
410 "_STATUS\n", port));
411 continue;
412 }
413
414 /* Connected */
415
416 if (!(status & UPS_PORT_POWER))
417 printf("%s: strange, connected port %d has no power\n",
418 USBDEVNAME(sc->sc_dev), port);
419
420 /* Wait for maximum device power up time. */
421 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
422
423 /* Reset port, which implies enabling it. */
424 if (usbd_reset_port(dev, port, &up->status)) {
425 printf("uhub_explore: port=%d reset failed\n",
426 port);
427 continue;
428 }
429
430 /* Get device info and set its address. */
431 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
432 dev->depth + 1, status & UPS_LOW_SPEED,
433 port, up);
434 /* XXX retry a few times? */
435 if (err) {
436 DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
437 "error=%s\n", usbd_errstr(err)));
438 /* Avoid addressing problems by disabling. */
439 /* usbd_reset_port(dev, port, &up->status); */
440
441 /*
442 * The unit refused to accept a new address, or had
443 * some other serious problem. Since we cannot leave
444 * at 0 we have to disable the port instead.
445 */
446 printf("%s: device problem, disabling port %d\n",
447 USBDEVNAME(sc->sc_dev), port);
448 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
449 } else {
450 /* The port set up succeeded, reset error count. */
451 up->restartcnt = 0;
452
453 if (up->device->hub)
454 up->device->hub->explore(up->device);
455 }
456 }
457 return (USBD_NORMAL_COMPLETION);
458 }
459
460 #if defined(__NetBSD__) || defined(__OpenBSD__)
461 int
462 uhub_activate(device_ptr_t self, enum devact act)
463 {
464 struct uhub_softc *sc = (struct uhub_softc *)self;
465 struct usbd_hub *hub = sc->sc_hub->hub;
466 usbd_device_handle dev;
467 int nports, port, i;
468
469 switch (act) {
470 case DVACT_ACTIVATE:
471 return (EOPNOTSUPP);
472 break;
473
474 case DVACT_DEACTIVATE:
475 if (hub == NULL) /* malfunctioning hub */
476 break;
477 nports = hub->hubdesc.bNbrPorts;
478 for(port = 0; port < nports; port++) {
479 dev = hub->ports[port].device;
480 if (dev != NULL && dev->subdevs != NULL) {
481 for (i = 0; dev->subdevs[i] != NULL; i++)
482 config_deactivate(dev->subdevs[i]);
483 }
484 }
485 break;
486 }
487 return (0);
488 }
489 #endif
490
491 /*
492 * Called from process context when the hub is gone.
493 * Detach all devices on active ports.
494 */
495 USB_DETACH(uhub)
496 {
497 USB_DETACH_START(uhub, sc);
498 struct usbd_hub *hub = sc->sc_hub->hub;
499 struct usbd_port *rup;
500 int port, nports;
501
502 #if defined(__NetBSD__) || defined(__OpenBSD__)
503 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
504 #elif defined(__FreeBSD__)
505 DPRINTF(("uhub_detach: sc=%port\n", sc));
506 #endif
507
508 if (hub == NULL) /* Must be partially working */
509 return (0);
510
511 usbd_abort_pipe(sc->sc_ipipe);
512 usbd_close_pipe(sc->sc_ipipe);
513
514 nports = hub->hubdesc.bNbrPorts;
515 for(port = 0; port < nports; port++) {
516 rup = &hub->ports[port];
517 if (rup->device)
518 usb_disconnect_port(rup, self);
519 }
520
521 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
522 USBDEV(sc->sc_dev));
523
524 free(hub, M_USBDEV);
525 sc->sc_hub->hub = NULL;
526
527 return (0);
528 }
529
530 #if defined(__FreeBSD__)
531 /* Called when a device has been detached from it */
532 Static void
533 uhub_child_detached(device_t self, device_t child)
534 {
535 struct uhub_softc *sc = device_get_softc(self);
536 usbd_device_handle devhub = sc->sc_hub;
537 usbd_device_handle dev;
538 int nports;
539 int port;
540 int i;
541
542 if (!devhub->hub)
543 /* should never happen; children are only created after init */
544 panic("hub not fully initialised, but child deleted?");
545
546 nports = devhub->hub->hubdesc.bNbrPorts;
547 for (port = 0; port < nports; port++) {
548 dev = devhub->hub->ports[port].device;
549 if (dev && dev->subdevs) {
550 for (i = 0; dev->subdevs[i]; i++) {
551 if (dev->subdevs[i] == child) {
552 dev->subdevs[i] = NULL;
553 return;
554 }
555 }
556 }
557 }
558 }
559 #endif
560
561
562 /*
563 * Hub interrupt.
564 * This an indication that some port has changed status.
565 * Notify the bus event handler thread that we need
566 * to be explored again.
567 */
568 void
569 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
570 {
571 struct uhub_softc *sc = addr;
572
573 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
574 if (status == USBD_STALLED)
575 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
576 else if (status == USBD_NORMAL_COMPLETION)
577 usb_needs_explore(sc->sc_hub);
578 }
579
580 #if defined(__FreeBSD__)
581 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
582 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
583 #endif
584