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