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