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