uhub.c revision 1.12 1 /* $NetBSD: uhub.c,v 1.12 1998/12/28 02:23:25 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 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #if defined(__NetBSD__)
45 #include <sys/device.h>
46 #elif defined(__FreeBSD__)
47 #include <sys/module.h>
48 #include <sys/bus.h>
49 #endif
50 #include <sys/proc.h>
51
52 #include <dev/usb/usb.h>
53
54 #include <dev/usb/usbdi.h>
55 #include <dev/usb/usbdi_util.h>
56 #include <dev/usb/usbdivar.h>
57
58 #ifdef USB_DEBUG
59 #define DPRINTF(x) if (usbdebug) printf x
60 #define DPRINTFN(n,x) if (usbdebug>(n)) printf x
61 extern int usbdebug;
62 extern char *usbd_error_strs[];
63 #else
64 #define DPRINTF(x)
65 #define DPRINTFN(n,x)
66 #endif
67
68 struct uhub_softc {
69 bdevice sc_dev; /* base device */
70 usbd_device_handle sc_hub; /* USB device */
71 usbd_pipe_handle sc_ipipe; /* interrupt pipe */
72 u_int8_t sc_status[1]; /* XXX more ports */
73 u_char sc_running;
74 };
75
76 usbd_status uhub_init_port __P((struct usbd_port *));
77 void uhub_disconnect __P((struct usbd_port *up));
78 usbd_status uhub_explore __P((usbd_device_handle hub));
79 void uhub_intr __P((usbd_request_handle, usbd_private_handle, usbd_status));
80
81 /*void uhub_disco __P((void *));*/
82
83 USB_DECLARE_DRIVER_NAME("usb", uhub);
84
85 /* FIXME what does FreeBSD need? */
86 #if defined(__NetBSD__)
87 struct cfattach uhub_uhub_ca = {
88 sizeof(struct uhub_softc), uhub_match, uhub_attach
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=%d(%s)\n",
129 USBDEVNAME(sc->sc_dev), r, usbd_error_strs[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=%d(%s)\n",
154 USBDEVNAME(sc->sc_dev), r, usbd_error_strs[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_USB, 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",
184 USBDEVNAME(sc->sc_dev));
185 USB_ATTACH_ERROR_RETURN;
186 }
187
188 /* Set up interrupt pipe. */
189 r = usbd_device2interface_handle(dev, 0, &iface);
190 if (r != USBD_NORMAL_COMPLETION) {
191 printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
192 USB_ATTACH_ERROR_RETURN;
193 }
194 ed = usbd_interface2endpoint_descriptor(iface, 0);
195 if (ed == 0) {
196 printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
197 USB_ATTACH_ERROR_RETURN;
198 }
199 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
200 printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
201 USB_ATTACH_ERROR_RETURN;
202 }
203
204 r = usbd_open_pipe_intr(iface, ed->bEndpointAddress,USBD_SHORT_XFER_OK,
205 &sc->sc_ipipe, sc, sc->sc_status,
206 sizeof(sc->sc_status),
207 uhub_intr);
208 if (r != USBD_NORMAL_COMPLETION) {
209 printf("%s: cannot open interrupt pipe\n",
210 USBDEVNAME(sc->sc_dev));
211 USB_ATTACH_ERROR_RETURN;
212 }
213
214 /* Wait with power off for a while. */
215 usbd_delay_ms(dev->bus, USB_POWER_DOWN_TIME);
216
217 for (p = 0; p < nports; p++) {
218 struct usbd_port *up = &hub->ports[p];
219 up->device = 0;
220 up->parent = dev;
221 up->portno = p+1;
222 r = uhub_init_port(up);
223 if (r != USBD_NORMAL_COMPLETION)
224 printf("%s: init of port %d failed\n",
225 USBDEVNAME(sc->sc_dev), up->portno);
226 }
227 sc->sc_running = 1;
228
229 USB_ATTACH_SUCCESS_RETURN;
230 }
231
232 #if defined(__FreeBSD__)
233 static int
234 uhub_detach(device_t self)
235 {
236 struct uhub_softc *sc = device_get_softc(self);
237 int nports = sc->sc_hub->hub->hubdesc.bNbrPorts;
238 int p;
239
240 for (p = 0; p < nports; p++) {
241 struct usbd_port *up = &sc->sc_hub->hub->ports[p];
242 if (up->device)
243 uhub_disconnect(up);
244 }
245
246 free(sc->sc_hub->hub, M_USB);
247
248 return 0;
249 }
250 #endif
251
252 usbd_status
253 uhub_init_port(up)
254 struct usbd_port *up;
255 {
256 int port = up->portno;
257 usbd_device_handle dev = up->parent;
258 usbd_status r;
259 u_int16_t pstatus;
260
261 r = usbd_get_port_status(dev, port, &up->status);
262 if (r != USBD_NORMAL_COMPLETION)
263 return (r);
264 pstatus = UGETW(up->status.wPortStatus);
265 DPRINTF(("usbd_init_port: adding hub port=%d status=0x%04x "
266 "change=0x%04x\n",
267 port, pstatus, UGETW(up->status.wPortChange)));
268 if ((pstatus & UPS_PORT_POWER) == 0) {
269 /* Port lacks power, turn it on */
270
271 /* First let the device go through a good power cycle, */
272 usbd_delay_ms(dev->bus, USB_PORT_POWER_DOWN_TIME);
273
274 /* then turn the power on. */
275 r = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
276 if (r != USBD_NORMAL_COMPLETION)
277 return (r);
278 r = usbd_get_port_status(dev, port, &up->status);
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->bus, dev->hub->hubdesc.bPwrOn2PwrGood *
287 UHD_PWRON_FACTOR);
288 }
289 if (dev->self_powered)
290 /* Self powered hub, give ports maximum current. */
291 up->power = USB_MAX_POWER;
292 else
293 up->power = USB_MIN_POWER;
294 return (USBD_NORMAL_COMPLETION);
295 }
296
297 usbd_status
298 uhub_explore(dev)
299 usbd_device_handle dev;
300 {
301 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
302 struct uhub_softc *sc = dev->hub->hubsoftc;
303 struct usbd_port *up;
304 usbd_status r;
305 int port;
306 int change, status;
307
308 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
309
310 if (!sc->sc_running)
311 return (USBD_NOT_STARTED);
312
313 /* Ignore hubs that are too deep. */
314 if (dev->depth > USB_HUB_MAX_DEPTH)
315 return (USBD_TOO_DEEP);
316
317 for(port = 1; port <= hd->bNbrPorts; port++) {
318 up = &dev->hub->ports[port-1];
319 r = usbd_get_port_status(dev, port, &up->status);
320 if (r != USBD_NORMAL_COMPLETION) {
321 DPRINTF(("uhub_explore: get port status failed, "
322 "error=%d(%s)\n",
323 r, usbd_error_strs[r]));
324 continue;
325 }
326 status = UGETW(up->status.wPortStatus);
327 change = UGETW(up->status.wPortChange);
328 DPRINTFN(5, ("uhub_explore: port %d status 0x%04x 0x%04x\n",
329 port, status, change));
330 if (change & UPS_C_PORT_ENABLED) {
331 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
332 if (status & UPS_PORT_ENABLED) {
333 printf("%s: illegal enable change, port %d\n",
334 USBDEVNAME(sc->sc_dev), port);
335 } else {
336 /* Port error condition. */
337 if (up->restartcnt++ < USBD_RESTART_MAX) {
338 printf("%s: port error, restarting "
339 "port %d\n",
340 USBDEVNAME(sc->sc_dev), port);
341 goto disco;
342 } else {
343 printf("%s: port error, giving up "
344 "port %d\n",
345 USBDEVNAME(sc->sc_dev), port);
346 }
347 }
348 }
349 if (!(change & UPS_C_CONNECT_STATUS)) {
350 /* No status change, just do recursive explore. */
351 if (up->device && up->device->hub)
352 up->device->hub->explore(up->device);
353 continue;
354 }
355 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
356 dev->address, port));
357 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
358 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
359 /*
360 * If there is already a device on the port the change status
361 * must mean that is has disconnected. Looking at the
362 * current connect status is not enough to figure this out
363 * since a new unit may have been connected before we handle
364 * the disconnect.
365 */
366 disco:
367 if (up->device) {
368 /* Disconnected */
369 DPRINTF(("uhub_explore: device %d disappeared "
370 "on port %d\n",
371 up->device->address, port));
372 uhub_disconnect(up);
373 usbd_clear_port_feature(dev, port,
374 UHF_C_PORT_CONNECTION);
375 }
376 if (!(status & UPS_CURRENT_CONNECT_STATUS))
377 continue;
378
379 /* Connected */
380 up->restartcnt = 0;
381
382 /* Wait for maximum device power up time. */
383 usbd_delay_ms(dev->bus, USB_PORT_POWERUP_DELAY);
384
385 /* Reset port, which implies enabling it. */
386 if (usbd_reset_port(dev, port, &up->status) !=
387 USBD_NORMAL_COMPLETION)
388 continue;
389
390 /* Get device info and set its address. */
391 r = usbd_new_device(&sc->sc_dev, dev->bus,
392 dev->depth + 1, status & UPS_LOW_SPEED,
393 port, up);
394 /* XXX retry a few times? */
395 if (r != USBD_NORMAL_COMPLETION) {
396 DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
397 "error=%d(%s)\n", r, usbd_error_strs[r]));
398 /* Avoid addressing problems by disabling. */
399 /* usbd_reset_port(dev, port, &up->status); */
400 /* XXX
401 * What should we do. The device may or may not be at its
402 * assigned address. In any case we'd like to ignore it.
403 * Maybe the port should be disabled until the device is
404 * disconnected.
405 */
406 if (r == USBD_SET_ADDR_FAILED || 1) {/* XXX */
407 /* The unit refused to accept a new
408 * address, and since we cannot leave
409 * at 0 we have to disable the port
410 * instead. */
411 printf("%s: device problem, disabling "
412 "port %d\n",
413 USBDEVNAME(sc->sc_dev), port);
414 usbd_clear_port_feature(dev, port,
415 UHF_PORT_ENABLE);
416 /* Make sure we don't try to restart it. */
417 up->restartcnt = USBD_RESTART_MAX;
418 }
419 } else {
420 if (up->device->hub)
421 up->device->hub->explore(up->device);
422 }
423 }
424 return (USBD_NORMAL_COMPLETION);
425 }
426
427 void
428 uhub_disconnect(up)
429 struct usbd_port *up;
430 {
431 usbd_device_handle dev = up->device;
432 usbd_pipe_handle p, n;
433 int i;
434 struct softc { bdevice sc_dev; }; /* all softc begin like this */
435
436 DPRINTFN(3,("uhub_disconnect: up=%p dev=%p port=%d\n",
437 up, dev, up->portno));
438
439 printf("%s: at %s port %d (addr %d) disconnected\n",
440 USBDEVNAME(((struct softc *)dev->softc)->sc_dev),
441 USBDEVNAME(((struct uhub_softc *)up->parent->softc)->sc_dev),
442 up->portno, dev->address);
443
444 if (!dev->cdesc) {
445 /* Partially attached device, just drop it. */
446 dev->bus->devices[dev->address] = 0;
447 up->device = 0;
448 return;
449 }
450
451 for (i = 0; i < dev->cdesc->bNumInterface; i++) {
452 for (p = LIST_FIRST(&dev->ifaces[i].pipes); p; p = n) {
453 n = LIST_NEXT(p, next);
454 if (p->disco)
455 p->disco(p->discoarg);
456 usbd_abort_pipe(p);
457 usbd_close_pipe(p);
458 }
459 }
460
461 #if defined(__NetBSD__)
462 /* XXX Free all data structures and disable further I/O. */
463 if (dev->hub) {
464 struct usbd_port *rup;
465 int p, nports;
466
467 DPRINTFN(3,("usb_disconnect: hub, recursing\n"));
468 nports = dev->hub->hubdesc.bNbrPorts;
469 for(p = 0; p < nports; p++) {
470 rup = &dev->hub->ports[p];
471 if (rup->device)
472 uhub_disconnect(rup);
473 }
474 }
475
476 dev->bus->devices[dev->address] = 0;
477 up->device = 0;
478 /* XXX free */
479 #elif defined(__FreeBSD__)
480 /* XXX this doesn't work. */
481 /* clean up the kindergarten, get rid of the kids */
482 usbd_remove_device(dev, up);
483 #endif
484 }
485
486 void
487 uhub_intr(reqh, addr, status)
488 usbd_request_handle reqh;
489 usbd_private_handle addr;
490 usbd_status status;
491 {
492 struct uhub_softc *sc = addr;
493
494 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
495 if (status != USBD_NORMAL_COMPLETION)
496 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
497 else
498 usb_needs_explore(sc->sc_hub->bus);
499 }
500
501 #if defined(__FreeBSD__)
502 DRIVER_MODULE(uhub, usb, uhub_driver, uhub_devclass, usb_driver_load, 0);
503 #endif
504