uhub.c revision 1.44.2.1 1 /* $NetBSD: uhub.c,v 1.44.2.1 2000/06/22 17:08:38 minoura 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/docs.htm
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: port %d status 0x%04x 0x%04x\n",
348 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++ < USBD_RESTART_MAX) {
358 printf("%s: port error, restarting "
359 "port %d\n",
360 USBDEVNAME(sc->sc_dev), port);
361 goto disco;
362 } else {
363 printf("%s: port error, giving up "
364 "port %d\n",
365 USBDEVNAME(sc->sc_dev), port);
366 }
367 }
368 }
369 if (!(change & UPS_C_CONNECT_STATUS)) {
370 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
371 "STATUS\n", port));
372 /* No status change, just do recursive explore. */
373 if (up->device && up->device->hub)
374 up->device->hub->explore(up->device);
375 continue;
376 }
377
378 /* We have a connect status change, handle it. */
379
380 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
381 dev->address, port));
382 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
383 /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
384 /*
385 * If there is already a device on the port the change status
386 * must mean that is has disconnected. Looking at the
387 * current connect status is not enough to figure this out
388 * since a new unit may have been connected before we handle
389 * the disconnect.
390 */
391 disco:
392 if (up->device != NULL) {
393 /* Disconnected */
394 DPRINTF(("uhub_explore: device addr=%d disappeared "
395 "on port %d\n", up->device->address, port));
396 usb_disconnect_port(up, USBDEV(sc->sc_dev));
397 usbd_clear_port_feature(dev, port,
398 UHF_C_PORT_CONNECTION);
399 }
400 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
401 /* Nothing connected, just ignore it. */
402 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
403 "_STATUS\n", port));
404 continue;
405 }
406
407 /* Connected */
408
409 if (!(status & UPS_PORT_POWER))
410 printf("%s: strange, connected port %d has no power\n",
411 USBDEVNAME(sc->sc_dev), port);
412
413 up->restartcnt = 0;
414
415 /* Wait for maximum device power up time. */
416 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
417
418 /* Reset port, which implies enabling it. */
419 if (usbd_reset_port(dev, port, &up->status)) {
420 printf("uhub_explore: port=%d reset failed\n",
421 port);
422 continue;
423 }
424
425 /* Get device info and set its address. */
426 err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
427 dev->depth + 1, status & UPS_LOW_SPEED,
428 port, up);
429 /* XXX retry a few times? */
430 if (err) {
431 DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
432 "error=%s\n", usbd_errstr(err)));
433 /* Avoid addressing problems by disabling. */
434 /* usbd_reset_port(dev, port, &up->status); */
435
436 /*
437 * The unit refused to accept a new address, or had
438 * some other serious problem. Since we cannot leave
439 * at 0 we have to disable the port instead.
440 */
441 printf("%s: device problem, disabling port %d\n",
442 USBDEVNAME(sc->sc_dev), port);
443 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
444 /* Make sure we don't try to restart it infinitely. */
445 up->restartcnt = USBD_RESTART_MAX;
446 } else {
447 if (up->device->hub)
448 up->device->hub->explore(up->device);
449 }
450 }
451 return (USBD_NORMAL_COMPLETION);
452 }
453
454 #if defined(__NetBSD__) || defined(__OpenBSD__)
455 int
456 uhub_activate(device_ptr_t self, enum devact act)
457 {
458 struct uhub_softc *sc = (struct uhub_softc *)self;
459 struct usbd_hub *hub = sc->sc_hub->hub;
460 usbd_device_handle dev;
461 int nports, port, i;
462
463 switch (act) {
464 case DVACT_ACTIVATE:
465 return (EOPNOTSUPP);
466 break;
467
468 case DVACT_DEACTIVATE:
469 if (hub == NULL) /* malfunctioning hub */
470 break;
471 nports = hub->hubdesc.bNbrPorts;
472 for(port = 0; port < nports; port++) {
473 dev = hub->ports[port].device;
474 if (dev != NULL) {
475 for (i = 0; dev->subdevs[i]; i++)
476 config_deactivate(dev->subdevs[i]);
477 }
478 }
479 break;
480 }
481 return (0);
482 }
483 #endif
484
485 /*
486 * Called from process context when the hub is gone.
487 * Detach all devices on active ports.
488 */
489 USB_DETACH(uhub)
490 {
491 USB_DETACH_START(uhub, sc);
492 struct usbd_hub *hub = sc->sc_hub->hub;
493 struct usbd_port *rup;
494 int port, nports;
495
496 #if defined(__NetBSD__) || defined(__OpenBSD__)
497 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
498 #elif defined(__FreeBSD__)
499 DPRINTF(("uhub_detach: sc=%port\n", sc));
500 #endif
501
502 if (hub == NULL) /* Must be partially working */
503 return (0);
504
505 usbd_abort_pipe(sc->sc_ipipe);
506 usbd_close_pipe(sc->sc_ipipe);
507
508 nports = hub->hubdesc.bNbrPorts;
509 for(port = 0; port < nports; port++) {
510 rup = &hub->ports[port];
511 if (rup->device)
512 usb_disconnect_port(rup, self);
513 }
514
515 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
516 USBDEV(sc->sc_dev));
517
518 free(hub, M_USBDEV);
519 sc->sc_hub->hub = NULL;
520
521 return (0);
522 }
523
524 #if defined(__FreeBSD__)
525 /* Called when a device has been detached from it */
526 Static void
527 uhub_child_detached(device_t self, device_t child)
528 {
529 struct uhub_softc *sc = device_get_softc(self);
530 usbd_device_handle devhub = sc->sc_hub;
531 usbd_device_handle dev;
532 int nports;
533 int port;
534 int i;
535
536 if (!devhub->hub)
537 /* should never happen; children are only created after init */
538 panic("hub not fully initialised, but child deleted?");
539
540 nports = devhub->hub->hubdesc.bNbrPorts;
541 for (port = 0; port < nports; port++) {
542 dev = devhub->hub->ports[port].device;
543 if (dev && dev->subdevs) {
544 for (i = 0; dev->subdevs[i]; i++) {
545 if (dev->subdevs[i] == child) {
546 dev->subdevs[i] = NULL;
547 return;
548 }
549 }
550 }
551 }
552 }
553 #endif
554
555
556 /*
557 * Hub interrupt.
558 * This an indication that some port has changed status.
559 * Notify the bus event handler thread that we need
560 * to be explored again.
561 */
562 void
563 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
564 {
565 struct uhub_softc *sc = addr;
566
567 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
568 if (status != USBD_NORMAL_COMPLETION)
569 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
570
571 usb_needs_explore(sc->sc_hub->bus);
572 }
573
574 #if defined(__FreeBSD__)
575 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
576 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
577 #endif
578