uhub.c revision 1.32.4.1 1 /* $NetBSD: uhub.c,v 1.32.4.1 1999/11/15 00:41:36 fvdl 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 #include "bus_if.h"
54 #endif
55 #include <sys/proc.h>
56
57 #include <machine/bus.h>
58
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 #include <dev/usb/usbdi_util.h>
62 #include <dev/usb/usbdivar.h>
63
64 #ifdef UHUB_DEBUG
65 #define DPRINTF(x) if (usbdebug) logprintf x
66 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
67 extern int usbdebug;
68 #else
69 #define DPRINTF(x)
70 #define DPRINTFN(n,x)
71 #endif
72
73 struct uhub_softc {
74 USBBASEDEVICE sc_dev; /* base device */
75 usbd_device_handle sc_hub; /* USB device */
76 usbd_pipe_handle sc_ipipe; /* interrupt pipe */
77 u_int8_t sc_status[1]; /* XXX more ports */
78 u_char sc_running;
79 };
80
81 static usbd_status uhub_init_port __P((struct usbd_port *));
82 static usbd_status uhub_explore __P((usbd_device_handle hub));
83 static void uhub_intr __P((usbd_xfer_handle, usbd_private_handle,
84 usbd_status));
85
86 #if defined(__FreeBSD__)
87 static bus_child_detached_t uhub_child_detached;
88 #endif
89
90 USB_DECLARE_DRIVER_INIT(uhub,
91 DEVMETHOD(bus_child_detached, uhub_child_detached));
92
93 /*
94 * We need two attachment points:
95 * hub to usb and hub to hub
96 * Every other driver only connects to hubs
97 */
98
99 #if defined(__NetBSD__) || defined(__OpenBSD__)
100 /* Create the driver instance for the hub connected to hub case */
101 struct cfattach uhub_uhub_ca = {
102 sizeof(struct uhub_softc), uhub_match, uhub_attach,
103 uhub_detach, uhub_activate
104 };
105 #elif defined(__FreeBSD__)
106 /* Create the driver instance for the hub connected to usb case. */
107 devclass_t uhubroot_devclass;
108
109 static device_method_t uhubroot_methods[] = {
110 DEVMETHOD(device_probe, uhub_match),
111 DEVMETHOD(device_attach, uhub_attach),
112
113 /* detach is not allowed for a root hub */
114 {0,0}
115 };
116
117 static driver_t uhubroot_driver = {
118 "uhub",
119 uhubroot_methods,
120 sizeof(struct uhub_softc)
121 };
122 #endif
123
124 USB_MATCH(uhub)
125 {
126 USB_MATCH_START(uhub, uaa);
127 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
128
129 DPRINTFN(5,("uhub_match, dd=%p\n", dd));
130 /*
131 * The subclass for hubs seems to be 0 for some and 1 for others,
132 * so we just ignore the subclass.
133 */
134 if (uaa->iface == NULL && dd->bDeviceClass == UCLASS_HUB)
135 return (UMATCH_DEVCLASS_DEVSUBCLASS);
136 return (UMATCH_NONE);
137 }
138
139 USB_ATTACH(uhub)
140 {
141 USB_ATTACH_START(uhub, sc, uaa);
142 usbd_device_handle dev = uaa->device;
143 char devinfo[1024];
144 usbd_status err;
145 struct usbd_hub *hub;
146 usb_device_request_t req;
147 usb_hub_descriptor_t hubdesc;
148 int p, port, nports, nremov;
149 usbd_interface_handle iface;
150 usb_endpoint_descriptor_t *ed;
151
152 DPRINTFN(1,("uhub_attach\n"));
153 sc->sc_hub = dev;
154 usbd_devinfo(dev, 1, devinfo);
155 USB_ATTACH_SETUP;
156 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
157
158 err = usbd_set_config_index(dev, 0, 1);
159 if (err) {
160 DPRINTF(("%s: configuration failed, error=%s\n",
161 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
162 USB_ATTACH_ERROR_RETURN;
163 }
164
165 if (dev->depth > USB_HUB_MAX_DEPTH) {
166 printf("%s: hub depth (%d) exceeded, hub ignored\n",
167 USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
168 USB_ATTACH_ERROR_RETURN;
169 }
170
171 /* Get hub descriptor. */
172 req.bmRequestType = UT_READ_CLASS_DEVICE;
173 req.bRequest = UR_GET_DESCRIPTOR;
174 USETW(req.wValue, 0);
175 USETW(req.wIndex, 0);
176 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
177 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
178 err = usbd_do_request(dev, &req, &hubdesc);
179 nports = hubdesc.bNbrPorts;
180 if (!err && nports > 7) {
181 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
182 err = usbd_do_request(dev, &req, &hubdesc);
183 }
184 if (err) {
185 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
186 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
187 USB_ATTACH_ERROR_RETURN;
188 }
189
190 for (nremov = 0, port = 1; port <= nports; port++)
191 if (!UHD_NOT_REMOV(&hubdesc, port))
192 nremov++;
193 printf("%s: %d port%s with %d removable, %s powered\n",
194 USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
195 nremov, dev->self_powered ? "self" : "bus");
196
197 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
198 M_USBDEV, M_NOWAIT);
199 if (hub == NULL)
200 USB_ATTACH_ERROR_RETURN;
201 dev->hub = hub;
202 dev->hub->hubsoftc = sc;
203 hub->explore = uhub_explore;
204 hub->hubdesc = hubdesc;
205
206 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
207 "parent->selfpowered=%d\n",
208 dev->self_powered, dev->powersrc->parent,
209 dev->powersrc->parent ?
210 dev->powersrc->parent->self_powered : 0));
211
212 if (!dev->self_powered && dev->powersrc->parent != NULL &&
213 !dev->powersrc->parent->self_powered) {
214 printf("%s: bus powered hub connected to bus powered hub, "
215 "ignored\n", USBDEVNAME(sc->sc_dev));
216 goto bad;
217 }
218
219 /* Set up interrupt pipe. */
220 err = usbd_device2interface_handle(dev, 0, &iface);
221 if (err) {
222 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
223 goto bad;
224 }
225 ed = usbd_interface2endpoint_descriptor(iface, 0);
226 if (ed == NULL) {
227 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
228 goto bad;
229 }
230 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
231 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
232 goto bad;
233 }
234
235 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
236 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
237 sizeof(sc->sc_status), uhub_intr);
238 if (err) {
239 printf("%s: cannot open interrupt pipe\n",
240 USBDEVNAME(sc->sc_dev));
241 goto bad;
242 }
243
244 /* Wait with power off for a while. */
245 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
246
247 for (p = 0; p < nports; p++) {
248 struct usbd_port *up = &hub->ports[p];
249 up->device = 0;
250 up->parent = dev;
251 up->portno = p+1;
252 err = uhub_init_port(up);
253 if (err)
254 printf("%s: init of port %d failed\n",
255 USBDEVNAME(sc->sc_dev), up->portno);
256 }
257 sc->sc_running = 1;
258
259 USB_ATTACH_SUCCESS_RETURN;
260
261 bad:
262 free(hub, M_USBDEV);
263 dev->hub = 0;
264 USB_ATTACH_ERROR_RETURN;
265 }
266
267 usbd_status
268 uhub_init_port(up)
269 struct usbd_port *up;
270 {
271 int port = up->portno;
272 usbd_device_handle dev = up->parent;
273 usbd_status err;
274 u_int16_t pstatus;
275
276 err = usbd_get_port_status(dev, port, &up->status);
277 if (err)
278 return (err);
279 pstatus = UGETW(up->status.wPortStatus);
280 DPRINTF(("usbd_init_port: adding hub port=%d status=0x%04x "
281 "change=0x%04x\n",
282 port, pstatus, UGETW(up->status.wPortChange)));
283 if ((pstatus & UPS_PORT_POWER) == 0) {
284 /* Port lacks power, turn it on */
285
286 /* First let the device go through a good power cycle, */
287 usbd_delay_ms(dev, USB_PORT_POWER_DOWN_TIME);
288
289 #if 0
290 usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT);
291 usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT);
292 #endif
293
294 /* then turn the power on. */
295 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
296 if (err)
297 return (err);
298 DPRINTF(("usb_init_port: turn on port %d power status=0x%04x "
299 "change=0x%04x\n",
300 port, UGETW(up->status.wPortStatus),
301 UGETW(up->status.wPortChange)));
302 /* Wait for stable power. */
303 usbd_delay_ms(dev, dev->hub->hubdesc.bPwrOn2PwrGood *
304 UHD_PWRON_FACTOR);
305 /* Get the port status again. */
306 err = usbd_get_port_status(dev, port, &up->status);
307 if (err)
308 return (err);
309 DPRINTF(("usb_init_port: after power on status=0x%04x "
310 "change=0x%04x\n",
311 UGETW(up->status.wPortStatus),
312 UGETW(up->status.wPortChange)));
313
314 #if 0
315 usbd_clear_hub_feature(dev, UHF_C_HUB_OVER_CURRENT);
316 usbd_clear_port_feature(dev, port, UHF_C_PORT_OVER_CURRENT);
317 usbd_get_port_status(dev, port, &up->status);
318 #endif
319
320 pstatus = UGETW(up->status.wPortStatus);
321 if ((pstatus & UPS_PORT_POWER) == 0)
322 printf("%s: port %d did not power up\n",
323 USBDEVNAME(((struct uhub_softc *)dev->hub->hubsoftc)->sc_dev), port);
324
325 }
326 if (dev->self_powered)
327 /* Self powered hub, give ports maximum current. */
328 up->power = USB_MAX_POWER;
329 else
330 up->power = USB_MIN_POWER;
331 return (USBD_NORMAL_COMPLETION);
332 }
333
334 usbd_status
335 uhub_explore(dev)
336 usbd_device_handle dev;
337 {
338 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
339 struct uhub_softc *sc = dev->hub->hubsoftc;
340 struct usbd_port *up;
341 usbd_status err;
342 int port;
343 int change, status;
344
345 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
346
347 if (!sc->sc_running)
348 return (USBD_NOT_STARTED);
349
350 /* Ignore hubs that are too deep. */
351 if (dev->depth > USB_HUB_MAX_DEPTH)
352 return (USBD_TOO_DEEP);
353
354 for(port = 1; port <= hd->bNbrPorts; port++) {
355 up = &dev->hub->ports[port-1];
356 err = usbd_get_port_status(dev, port, &up->status);
357 if (err) {
358 DPRINTF(("uhub_explore: get port status failed, "
359 "error=%s\n", usbd_errstr(err)));
360 continue;
361 }
362 status = UGETW(up->status.wPortStatus);
363 change = UGETW(up->status.wPortChange);
364 DPRINTFN(5, ("uhub_explore: port %d status 0x%04x 0x%04x\n",
365 port, status, change));
366 if (change & UPS_C_PORT_ENABLED) {
367 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
368 if (status & UPS_PORT_ENABLED) {
369 printf("%s: illegal enable change, port %d\n",
370 USBDEVNAME(sc->sc_dev), port);
371 } else {
372 /* Port error condition. */
373 if (up->restartcnt++ < USBD_RESTART_MAX) {
374 printf("%s: port error, restarting "
375 "port %d\n",
376 USBDEVNAME(sc->sc_dev), port);
377 goto disco;
378 } else {
379 printf("%s: port error, giving up "
380 "port %d\n",
381 USBDEVNAME(sc->sc_dev), port);
382 }
383 }
384 }
385 if (!(change & UPS_C_CONNECT_STATUS)) {
386 /* No status change, just do recursive explore. */
387 if (up->device && up->device->hub)
388 up->device->hub->explore(up->device);
389 continue;
390 }
391 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
392 dev->address, port));
393 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
394 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
395 /*
396 * If there is already a device on the port the change status
397 * must mean that is has disconnected. Looking at the
398 * current connect status is not enough to figure this out
399 * since a new unit may have been connected before we handle
400 * the disconnect.
401 */
402 disco:
403 if (up->device != NULL) {
404 /* Disconnected */
405 DPRINTF(("uhub_explore: device %d disappeared "
406 "on port %d\n", up->device->address, port));
407 usb_disconnect_port(up, USBDEV(sc->sc_dev));
408 usbd_clear_port_feature(dev, port,
409 UHF_C_PORT_CONNECTION);
410 }
411 if (!(status & UPS_CURRENT_CONNECT_STATUS))
412 continue;
413
414 /* Connected */
415 up->restartcnt = 0;
416
417 /* Wait for maximum device power up time. */
418 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
419
420 /* Reset port, which implies enabling it. */
421 if (usbd_reset_port(dev, port, &up->status) !=
422 USBD_NORMAL_COMPLETION)
423 continue;
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 != NULL)
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(self, act)
457 device_ptr_t self;
458 enum devact act;
459 {
460 struct uhub_softc *sc = (struct uhub_softc *)self;
461 usbd_device_handle devhub = sc->sc_hub;
462 int nports, p, i;
463
464 switch (act) {
465 case DVACT_ACTIVATE:
466 return (EOPNOTSUPP);
467 break;
468
469 case DVACT_DEACTIVATE:
470 nports = devhub->hub->hubdesc.bNbrPorts;
471 for(p = 0; p < nports; p++) {
472 usbd_device_handle dev = devhub->hub->ports[p].device;
473 if (dev != NULL) {
474 for (i = 0; dev->subdevs[i]; i++)
475 config_deactivate(dev->subdevs[i]);
476 }
477 }
478 break;
479 }
480 return (0);
481 }
482 #endif
483
484 /*
485 * Called from process context when the hub is gone.
486 * Detach all devices on active ports.
487 */
488 USB_DETACH(uhub)
489 {
490 USB_DETACH_START(uhub, sc);
491 usbd_device_handle dev = sc->sc_hub;
492 struct usbd_port *rup;
493 int port, nports;
494
495 #if defined(__NetBSD__) || defined(__OpenBSD__)
496 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
497 #elif defined(__FreeBSD__)
498 DPRINTF(("uhub_detach: sc=%port\n", sc));
499 #endif
500
501 if (dev->hub == NULL) /* Must be partially working */
502 return (0);
503
504 usbd_abort_pipe(sc->sc_ipipe);
505 usbd_close_pipe(sc->sc_ipipe);
506
507 nports = dev->hub->hubdesc.bNbrPorts;
508 for(port = 0; port < nports; port++) {
509 rup = &dev->hub->ports[port];
510 if (rup->device)
511 usb_disconnect_port(rup, self);
512 }
513
514 free(dev->hub, M_USBDEV);
515 dev->hub = NULL;
516
517 return (0);
518 }
519
520 /*
521 * Hub interrupt.
522 * This an indication that some port has changed status.
523 * Notify the bus event handler thread that we need
524 * to be explored again.
525 */
526 void
527 uhub_intr(xfer, addr, status)
528 usbd_xfer_handle xfer;
529 usbd_private_handle addr;
530 usbd_status status;
531 {
532 struct uhub_softc *sc = addr;
533
534 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
535 if (status != USBD_NORMAL_COMPLETION)
536 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
537
538 usb_needs_explore(sc->sc_hub->bus);
539 }
540
541 #if defined(__FreeBSD__)
542 DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
543 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
544 #endif
545