uhub.c revision 1.27 1 /* $NetBSD: uhub.c,v 1.27 1999/09/12 08:23:42 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) carlstedt.se) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * USB spec: http://www.usb.org/cgi-usb/mailmerge.cgi/home/usb/docs/developers/cgiform.tpl
42 */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #if defined(__NetBSD__) || defined(__OpenBSD__)
49 #include <sys/device.h>
50 #elif defined(__FreeBSD__)
51 #include <sys/module.h>
52 #include <sys/bus.h>
53 #endif
54 #include <sys/proc.h>
55
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
59 #include <dev/usb/usbdivar.h>
60
61 #ifdef USB_DEBUG
62 #define DPRINTF(x) if (usbdebug) logprintf x
63 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
64 extern int usbdebug;
65 #else
66 #define DPRINTF(x)
67 #define DPRINTFN(n,x)
68 #endif
69
70 struct uhub_softc {
71 USBBASEDEVICE sc_dev; /* base device */
72 usbd_device_handle sc_hub; /* USB device */
73 usbd_pipe_handle sc_ipipe; /* interrupt pipe */
74 u_int8_t sc_status[1]; /* XXX more ports */
75 u_char sc_running;
76 };
77
78 usbd_status uhub_init_port __P((struct usbd_port *));
79 void uhub_disconnect_port __P((struct usbd_port *up));
80 usbd_status uhub_explore __P((usbd_device_handle hub));
81 void uhub_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
82
83 USB_DECLARE_DRIVER_NAME(usb, uhub);
84
85 #if defined(__NetBSD__) || defined(__OpenBSD__)
86 struct cfattach uhub_uhub_ca = {
87 sizeof(struct uhub_softc), uhub_match, uhub_attach,
88 uhub_detach, uhub_activate
89 };
90 #endif
91
92 USB_MATCH(uhub)
93 {
94 USB_MATCH_START(uhub, uaa);
95 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
96
97 DPRINTFN(5,("uhub_match, dd=%p\n", dd));
98 /*
99 * The subclass for hubs seems to be 0 for some and 1 for others,
100 * so we just ignore the subclass.
101 */
102 if (uaa->iface == 0 && dd->bDeviceClass == UCLASS_HUB)
103 return (UMATCH_DEVCLASS_DEVSUBCLASS);
104 return (UMATCH_NONE);
105 }
106
107 USB_ATTACH(uhub)
108 {
109 USB_ATTACH_START(uhub, sc, uaa);
110 usbd_device_handle dev = uaa->device;
111 char devinfo[1024];
112 usbd_status r;
113 struct usbd_hub *hub;
114 usb_device_request_t req;
115 usb_hub_descriptor_t hubdesc;
116 int p, port, nports, nremov;
117 usbd_interface_handle iface;
118 usb_endpoint_descriptor_t *ed;
119
120 DPRINTFN(1,("uhub_attach\n"));
121 sc->sc_hub = dev;
122 usbd_devinfo(dev, 1, devinfo);
123 USB_ATTACH_SETUP;
124 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
125
126 r = usbd_set_config_index(dev, 0, 1);
127 if (r != USBD_NORMAL_COMPLETION) {
128 DPRINTF(("%s: configuration failed, error=%s\n",
129 USBDEVNAME(sc->sc_dev), usbd_errstr(r)));
130 USB_ATTACH_ERROR_RETURN;
131 }
132
133 if (dev->depth > USB_HUB_MAX_DEPTH) {
134 printf("%s: hub depth (%d) exceeded, hub ignored\n",
135 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
136 USB_ATTACH_ERROR_RETURN;
137 }
138
139 /* Get hub descriptor. */
140 req.bmRequestType = UT_READ_CLASS_DEVICE;
141 req.bRequest = UR_GET_DESCRIPTOR;
142 USETW(req.wValue, 0);
143 USETW(req.wIndex, 0);
144 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
145 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
146 r = usbd_do_request(dev, &req, &hubdesc);
147 nports = hubdesc.bNbrPorts;
148 if (r == USBD_NORMAL_COMPLETION && nports > 7) {
149 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
150 r = usbd_do_request(dev, &req, &hubdesc);
151 }
152 if (r != USBD_NORMAL_COMPLETION) {
153 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
154 USBDEVNAME(sc->sc_dev), usbd_errstr(r)));
155 USB_ATTACH_ERROR_RETURN;
156 }
157
158 for (nremov = 0, port = 1; port <= nports; port++)
159 if (!UHD_NOT_REMOV(&hubdesc, port))
160 nremov++;
161 printf("%s: %d port%s with %d removable, %s powered\n",
162 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
163 nremov, dev->self_powered ? "self" : "bus");
164
165
166 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
167 M_USBDEV, M_NOWAIT);
168 if (hub == 0)
169 USB_ATTACH_ERROR_RETURN;
170 dev->hub = hub;
171 dev->hub->hubsoftc = sc;
172 hub->explore = uhub_explore;
173 hub->hubdesc = hubdesc;
174
175 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
176 "parent->selfpowered=%d\n",
177 dev->self_powered, dev->powersrc->parent,
178 dev->powersrc->parent ?
179 dev->powersrc->parent->self_powered : 0));
180 if (!dev->self_powered && dev->powersrc->parent &&
181 !dev->powersrc->parent->self_powered) {
182 printf("%s: bus powered hub connected to bus powered hub, "
183 "ignored\n", USBDEVNAME(sc->sc_dev));
184 goto bad;
185 }
186
187 /* Set up interrupt pipe. */
188 r = usbd_device2interface_handle(dev, 0, &iface);
189 if (r != USBD_NORMAL_COMPLETION) {
190 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
191 goto bad;
192 }
193 ed = usbd_interface2endpoint_descriptor(iface, 0);
194 if (ed == 0) {
195 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
196 goto bad;
197 }
198 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
199 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
200 goto bad;
201 }
202
203 r = usbd_open_pipe_intr(iface, ed->bEndpointAddress,USBD_SHORT_XFER_OK,
204 &sc->sc_ipipe, sc, sc->sc_status,
205 sizeof(sc->sc_status),
206 uhub_intr);
207 if (r != USBD_NORMAL_COMPLETION) {
208 printf("%s: cannot open interrupt pipe\n",
209 USBDEVNAME(sc->sc_dev));
210 goto bad;
211 }
212
213 /* Wait with power off for a while. */
214 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
215
216 for (p = 0; p < nports; p++) {
217 struct usbd_port *up = &hub->ports[p];
218 up->device = 0;
219 up->parent = dev;
220 up->portno = p+1;
221 r = uhub_init_port(up);
222 if (r != USBD_NORMAL_COMPLETION)
223 printf("%s: init of port %d failed\n",
224 USBDEVNAME(sc->sc_dev), up->portno);
225 }
226 sc->sc_running = 1;
227
228 USB_ATTACH_SUCCESS_RETURN;
229
230 bad:
231 free(hub, M_USBDEV);
232 dev->hub = 0;
233 USB_ATTACH_ERROR_RETURN;
234 }
235
236 usbd_status
237 uhub_init_port(up)
238 struct usbd_port *up;
239 {
240 int port = up->portno;
241 usbd_device_handle dev = up->parent;
242 usbd_status r;
243 u_int16_t pstatus;
244
245 r = usbd_get_port_status(dev, port, &up->status);
246 if (r != USBD_NORMAL_COMPLETION)
247 return (r);
248 pstatus = UGETW(up->status.wPortStatus);
249 DPRINTF(("usbd_init_port: adding hub port=%d status=0x%04x "
250 "change=0x%04x\n",
251 port, pstatus, UGETW(up->status.wPortChange)));
252 if ((pstatus & UPS_PORT_POWER) == 0) {
253 /* Port lacks power, turn it on */
254
255 /* First let the device go through a good power cycle, */
256 usbd_delay_ms(dev, USB_PORT_POWER_DOWN_TIME);
257
258 #if 0
259 usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT);
260 usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT);
261 #endif
262
263 /* then turn the power on. */
264 r = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
265 if (r != USBD_NORMAL_COMPLETION)
266 return (r);
267 DPRINTF(("usb_init_port: turn on port %d power status=0x%04x "
268 "change=0x%04x\n",
269 port, UGETW(up->status.wPortStatus),
270 UGETW(up->status.wPortChange)));
271 /* Wait for stable power. */
272 usbd_delay_ms(dev, dev->hub->hubdesc.bPwrOn2PwrGood *
273 UHD_PWRON_FACTOR);
274 /* Get the port status again. */
275 r = usbd_get_port_status(dev, port, &up->status);
276 if (r != USBD_NORMAL_COMPLETION)
277 return (r);
278 DPRINTF(("usb_init_port: after power on status=0x%04x "
279 "change=0x%04x\n",
280 UGETW(up->status.wPortStatus),
281 UGETW(up->status.wPortChange)));
282
283 #if 0
284 usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT);
285 usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT);
286 usbd_get_port_status(dev, port, &up->status);
287 #endif
288
289 pstatus = UGETW(up->status.wPortStatus);
290 if ((pstatus & UPS_PORT_POWER) == 0)
291 printf("%s: port %d did not power up\n",
292 USBDEVNAME(((struct uhub_softc *)dev->hub->hubsoftc)->sc_dev), port);
293
294 }
295 if (dev->self_powered)
296 /* Self powered hub, give ports maximum current. */
297 up->power = USB_MAX_POWER;
298 else
299 up->power = USB_MIN_POWER;
300 return (USBD_NORMAL_COMPLETION);
301 }
302
303 usbd_status
304 uhub_explore(dev)
305 usbd_device_handle dev;
306 {
307 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
308 struct uhub_softc *sc = dev->hub->hubsoftc;
309 struct usbd_port *up;
310 usbd_status r;
311 int port;
312 int change, status;
313
314 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
315
316 if (!sc->sc_running)
317 return (USBD_NOT_STARTED);
318
319 /* Ignore hubs that are too deep. */
320 if (dev->depth > USB_HUB_MAX_DEPTH)
321 return (USBD_TOO_DEEP);
322
323 for(port = 1; port <= hd->bNbrPorts; port++) {
324 up = &dev->hub->ports[port-1];
325 r = usbd_get_port_status(dev, port, &up->status);
326 if (r != USBD_NORMAL_COMPLETION) {
327 DPRINTF(("uhub_explore: get port status failed, "
328 "error=%s\n",
329 usbd_errstr(r)));
330 continue;
331 }
332 status = UGETW(up->status.wPortStatus);
333 change = UGETW(up->status.wPortChange);
334 DPRINTFN(5, ("uhub_explore: port %d status 0x%04x 0x%04x\n",
335 port, status, change));
336 if (change & UPS_C_PORT_ENABLED) {
337 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
338 if (status & UPS_PORT_ENABLED) {
339 printf("%s: illegal enable change, port %d\n",
340 USBDEVNAME(sc->sc_dev), port);
341 } else {
342 /* Port error condition. */
343 if (up->restartcnt++ < USBD_RESTART_MAX) {
344 printf("%s: port error, restarting "
345 "port %d\n",
346 USBDEVNAME(sc->sc_dev), port);
347 goto disco;
348 } else {
349 printf("%s: port error, giving up "
350 "port %d\n",
351 USBDEVNAME(sc->sc_dev), port);
352 }
353 }
354 }
355 if (!(change & UPS_C_CONNECT_STATUS)) {
356 /* No status change, just do recursive explore. */
357 if (up->device && up->device->hub)
358 up->device->hub->explore(up->device);
359 continue;
360 }
361 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
362 dev->address, port));
363 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
364 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
365 /*
366 * If there is already a device on the port the change status
367 * must mean that is has disconnected. Looking at the
368 * current connect status is not enough to figure this out
369 * since a new unit may have been connected before we handle
370 * the disconnect.
371 */
372 disco:
373 if (up->device) {
374 /* Disconnected */
375 DPRINTF(("uhub_explore: device %d disappeared "
376 "on port %d\n",
377 up->device->address, port));
378 uhub_disconnect_port(up);
379 usbd_clear_port_feature(dev, port,
380 UHF_C_PORT_CONNECTION);
381 }
382 if (!(status & UPS_CURRENT_CONNECT_STATUS))
383 continue;
384
385 /* Connected */
386 up->restartcnt = 0;
387
388 /* Wait for maximum device power up time. */
389 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
390
391 /* Reset port, which implies enabling it. */
392 if (usbd_reset_port(dev, port, &up->status) !=
393 USBD_NORMAL_COMPLETION)
394 continue;
395
396 /* Get device info and set its address. */
397 r = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
398 dev->depth + 1, status & UPS_LOW_SPEED,
399 port, up);
400 /* XXX retry a few times? */
401 if (r != USBD_NORMAL_COMPLETION) {
402 DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
403 "error=%s\n", usbd_errstr(r)));
404 /* Avoid addressing problems by disabling. */
405 /* usbd_reset_port(dev, port, &up->status); */
406 /* XXX
407 * What should we do. The device may or may not be at its
408 * assigned address. In any case we'd like to ignore it.
409 * Maybe the port should be disabled until the device is
410 * disconnected.
411 */
412 if (r == USBD_SET_ADDR_FAILED || 1) {/* XXX */
413 /* The unit refused to accept a new
414 * address, and since we cannot leave
415 * at 0 we have to disable the port
416 * instead. */
417 printf("%s: device problem, disabling "
418 "port %d\n",
419 USBDEVNAME(sc->sc_dev), port);
420 usbd_clear_port_feature(dev, port,
421 UHF_PORT_ENABLE);
422 /* Make sure we don't try to restart it. */
423 up->restartcnt = USBD_RESTART_MAX;
424 }
425 } else {
426 if (up->device->hub)
427 up->device->hub->explore(up->device);
428 }
429 }
430 return (USBD_NORMAL_COMPLETION);
431 }
432
433 /*
434 * The general mechanism for detaching drivers works as follows: Each
435 * driver is responsible for maintaining a reference count on the
436 * number of outstanding references to its softc (e.g. from
437 * processing hanging in a read or write). The detach method of the
438 * driver decrements this counter and flags in the softc that the
439 * driver is dying and then wakes any sleepers. It then sleeps on the
440 * softc. Each place that can sleep must maintain the reference
441 * count. When the reference count drops to -1 (0 is the normal value
442 * of the reference count) the a wakeup on the softc is performed
443 * signaling to the detach waiter that all references are gone.
444 */
445
446 /*
447 * Called from process context when we discover that a port has
448 * been disconnected.
449 */
450 void
451 uhub_disconnect_port(up)
452 struct usbd_port *up;
453 {
454 usbd_device_handle dev = up->device;
455 char *hubname;
456 int i;
457
458 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
459 up, dev, up->portno));
460
461 if (!dev->cdesc) {
462 /* Partially attached device, just drop it. */
463 dev->bus->devices[dev->address] = 0;
464 up->device = 0;
465 return;
466 }
467
468 if (dev->subdevs) {
469 hubname = USBDEVPTRNAME(up->parent->subdevs[0]);
470 for (i = 0; dev->subdevs[i]; i++) {
471 printf("%s: at %s port %d (addr %d) disconnected\n",
472 USBDEVPTRNAME(dev->subdevs[i]), hubname,
473 up->portno, dev->address);
474 config_detach(dev->subdevs[i], DETACH_FORCE);
475 }
476 }
477
478 dev->bus->devices[dev->address] = 0;
479 up->device = 0;
480 usb_free_device(dev);
481
482 #if defined(__FreeBSD__)
483 device_delete_child(
484 device_get_parent(((struct softc *)dev->softc)->sc_dev),
485 ((struct softc *)dev->softc)->sc_dev);
486 #endif
487 }
488
489 int
490 uhub_activate(self, act)
491 device_ptr_t self;
492 enum devact act;
493 {
494 struct uhub_softc *sc = (struct uhub_softc *)self;
495 usbd_device_handle devhub = sc->sc_hub;
496 int nports, p, i;
497
498 switch (act) {
499 case DVACT_ACTIVATE:
500 return (EOPNOTSUPP);
501 break;
502
503 case DVACT_DEACTIVATE:
504 nports = devhub->hub->hubdesc.bNbrPorts;
505 for(p = 0; p < nports; p++) {
506 usbd_device_handle dev = devhub->hub->ports[p].device;
507 if (dev) {
508 for (i = 0; dev->subdevs[i]; i++)
509 config_deactivate(dev->subdevs[i]);
510 }
511 }
512 break;
513 }
514 return (0);
515 }
516
517 /*
518 * Called from process context when the hub is gone.
519 * Detach all devices on active ports.
520 */
521 int
522 uhub_detach(self, flags)
523 device_ptr_t self;
524 int flags;
525 {
526 struct uhub_softc *sc = (struct uhub_softc *)self;
527 usbd_device_handle dev = sc->sc_hub;
528 struct usbd_port *rup;
529 int p, nports;
530
531 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
532
533 if (!dev->hub) {
534 /* Must be partially working */
535 return (0);
536 }
537
538 usbd_abort_pipe(sc->sc_ipipe);
539 usbd_close_pipe(sc->sc_ipipe);
540
541 nports = dev->hub->hubdesc.bNbrPorts;
542 for(p = 0; p < nports; p++) {
543 rup = &dev->hub->ports[p];
544 if (rup->device)
545 uhub_disconnect_port(rup);
546 }
547
548 free(dev->hub, M_USBDEV);
549 dev->hub = 0;
550
551 return (0);
552 }
553
554 /*
555 * Hub interrupt.
556 * This an indication that some port has changed status.
557 * Notify the bus event handler thread that we need
558 * to be explored again.
559 */
560 void
561 uhub_intr(reqh, addr, status)
562 usbd_request_handle reqh;
563 usbd_private_handle addr;
564 usbd_status status;
565 {
566 struct uhub_softc *sc = addr;
567
568 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
569 if (status != USBD_NORMAL_COMPLETION)
570 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
571
572 usb_needs_explore(sc->sc_hub->bus);
573 }
574
575 #if defined(__FreeBSD__)
576 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
577 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
578 #endif
579