uhub.c revision 1.85.8.2 1 1.85.8.2 itohy /* $NetBSD: uhub.c,v 1.85.8.2 2007/06/18 13:53:06 itohy Exp $ */
2 1.1 augustss
3 1.85.8.1 itohy /*-
4 1.74 mycroft * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.6 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.44 augustss * by Lennart Augustsson (lennart (at) augustsson.net) at
9 1.6 augustss * Carlstedt Research & Technology.
10 1.1 augustss *
11 1.1 augustss * Redistribution and use in source and binary forms, with or without
12 1.1 augustss * modification, are permitted provided that the following conditions
13 1.1 augustss * are met:
14 1.1 augustss * 1. Redistributions of source code must retain the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer.
16 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 augustss * notice, this list of conditions and the following disclaimer in the
18 1.1 augustss * documentation and/or other materials provided with the distribution.
19 1.1 augustss * 3. All advertising materials mentioning features or use of this software
20 1.1 augustss * must display the following acknowledgement:
21 1.1 augustss * This product includes software developed by the NetBSD
22 1.1 augustss * Foundation, Inc. and its contributors.
23 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 augustss * contributors may be used to endorse or promote products derived
25 1.1 augustss * from this software without specific prior written permission.
26 1.1 augustss *
27 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
38 1.15 augustss */
39 1.15 augustss
40 1.15 augustss /*
41 1.64 ichiro * USB spec: http://www.usb.org/developers/docs/usbspec.zip
42 1.1 augustss */
43 1.53 lukem
44 1.53 lukem #include <sys/cdefs.h>
45 1.85.8.2 itohy __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.85.8.2 2007/06/18 13:53:06 itohy Exp $");
46 1.85.8.1 itohy /* __FBSDID("$FreeBSD: src/sys/dev/usb/uhub.c,v 1.74 2007/02/03 21:11:11 flz Exp $"); */
47 1.1 augustss
48 1.1 augustss #include <sys/param.h>
49 1.1 augustss #include <sys/systm.h>
50 1.1 augustss #include <sys/kernel.h>
51 1.1 augustss #include <sys/malloc.h>
52 1.20 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
53 1.1 augustss #include <sys/device.h>
54 1.34 augustss #include <sys/proc.h>
55 1.11 augustss #elif defined(__FreeBSD__)
56 1.11 augustss #include <sys/module.h>
57 1.11 augustss #include <sys/bus.h>
58 1.85.8.1 itohy #include <sys/lock.h>
59 1.85.8.1 itohy #include <sys/mutex.h>
60 1.85.8.1 itohy #include <sys/sysctl.h>
61 1.11 augustss #endif
62 1.28 augustss
63 1.28 augustss #include <machine/bus.h>
64 1.1 augustss
65 1.1 augustss #include <dev/usb/usb.h>
66 1.1 augustss #include <dev/usb/usbdi.h>
67 1.1 augustss #include <dev/usb/usbdi_util.h>
68 1.1 augustss #include <dev/usb/usbdivar.h>
69 1.1 augustss
70 1.32 augustss #ifdef UHUB_DEBUG
71 1.85.8.1 itohy #define DPRINTF(x) if (uhubdebug) printf x
72 1.85.8.1 itohy #define DPRINTFN(n,x) if (uhubdebug > (n)) printf x
73 1.63 dsainty int uhubdebug = 0;
74 1.85.8.1 itohy #if defined(__FreeBSD__)
75 1.85.8.1 itohy SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB uhub");
76 1.85.8.1 itohy SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW,
77 1.85.8.1 itohy &uhubdebug, 0, "uhub debug level");
78 1.85.8.1 itohy #endif
79 1.1 augustss #else
80 1.1 augustss #define DPRINTF(x)
81 1.1 augustss #define DPRINTFN(n,x)
82 1.1 augustss #endif
83 1.1 augustss
84 1.1 augustss struct uhub_softc {
85 1.26 augustss USBBASEDEVICE sc_dev; /* base device */
86 1.11 augustss usbd_device_handle sc_hub; /* USB device */
87 1.85.8.2 itohy int sc_proto; /* device protocol */
88 1.11 augustss usbd_pipe_handle sc_ipipe; /* interrupt pipe */
89 1.84 drochner u_int8_t *sc_status;
90 1.11 augustss u_char sc_running;
91 1.1 augustss };
92 1.85.8.2 itohy #define UHUB_IS_HIGH_SPEED(sc) ((sc)->sc_proto != UDPROTO_FSHUB)
93 1.85.8.2 itohy #define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT)
94 1.1 augustss
95 1.45 augustss Static usbd_status uhub_explore(usbd_device_handle hub);
96 1.45 augustss Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
97 1.1 augustss
98 1.32 augustss #if defined(__FreeBSD__)
99 1.85.8.1 itohy Static bus_child_location_str_t uhub_child_location_str;
100 1.85.8.1 itohy Static bus_child_pnpinfo_str_t uhub_child_pnpinfo_str;
101 1.32 augustss #endif
102 1.32 augustss
103 1.58 augustss /*
104 1.32 augustss * We need two attachment points:
105 1.32 augustss * hub to usb and hub to hub
106 1.32 augustss * Every other driver only connects to hubs
107 1.32 augustss */
108 1.1 augustss
109 1.20 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
110 1.34 augustss USB_DECLARE_DRIVER(uhub);
111 1.32 augustss #elif defined(__FreeBSD__)
112 1.34 augustss USB_DECLARE_DRIVER_INIT(uhub,
113 1.85.8.1 itohy DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
114 1.85.8.1 itohy DEVMETHOD(bus_child_location_str, uhub_child_location_str),
115 1.85.8.1 itohy DEVMETHOD(bus_driver_added, bus_generic_driver_added),
116 1.85.8.1 itohy DEVMETHOD(device_suspend, bus_generic_suspend),
117 1.85.8.1 itohy DEVMETHOD(device_resume, bus_generic_resume),
118 1.85.8.1 itohy DEVMETHOD(device_shutdown, bus_generic_shutdown)
119 1.85.8.1 itohy );
120 1.58 augustss
121 1.32 augustss /* Create the driver instance for the hub connected to usb case. */
122 1.32 augustss devclass_t uhubroot_devclass;
123 1.32 augustss
124 1.41 augustss Static device_method_t uhubroot_methods[] = {
125 1.85.8.1 itohy DEVMETHOD(bus_child_location_str, uhub_child_location_str),
126 1.85.8.1 itohy DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_str),
127 1.85.8.1 itohy DEVMETHOD(bus_driver_added, bus_generic_driver_added),
128 1.85.8.1 itohy
129 1.32 augustss DEVMETHOD(device_probe, uhub_match),
130 1.32 augustss DEVMETHOD(device_attach, uhub_attach),
131 1.85.8.1 itohy DEVMETHOD(device_detach, uhub_detach),
132 1.85.8.1 itohy DEVMETHOD(device_suspend, bus_generic_suspend),
133 1.85.8.1 itohy DEVMETHOD(device_resume, bus_generic_resume),
134 1.85.8.1 itohy DEVMETHOD(device_shutdown, bus_generic_shutdown),
135 1.32 augustss {0,0}
136 1.32 augustss };
137 1.32 augustss
138 1.41 augustss Static driver_t uhubroot_driver = {
139 1.32 augustss "uhub",
140 1.32 augustss uhubroot_methods,
141 1.32 augustss sizeof(struct uhub_softc)
142 1.32 augustss };
143 1.11 augustss #endif
144 1.1 augustss
145 1.11 augustss USB_MATCH(uhub)
146 1.1 augustss {
147 1.11 augustss USB_MATCH_START(uhub, uaa);
148 1.85.8.2 itohy #ifndef USB_USE_IFATTACH
149 1.1 augustss usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
150 1.85.8.2 itohy #endif /* USB_USE_IFATTACH */
151 1.58 augustss
152 1.11 augustss DPRINTFN(5,("uhub_match, dd=%p\n", dd));
153 1.58 augustss /*
154 1.1 augustss * The subclass for hubs seems to be 0 for some and 1 for others,
155 1.1 augustss * so we just ignore the subclass.
156 1.1 augustss */
157 1.85.8.2 itohy #ifndef USB_USE_IFATTACH
158 1.40 augustss if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
159 1.85.8.2 itohy #else
160 1.85.8.2 itohy if (uaa->class == UDCLASS_HUB)
161 1.85.8.2 itohy #endif /* USB_USE_IFATTACH */
162 1.1 augustss return (UMATCH_DEVCLASS_DEVSUBCLASS);
163 1.1 augustss return (UMATCH_NONE);
164 1.1 augustss }
165 1.1 augustss
166 1.11 augustss USB_ATTACH(uhub)
167 1.1 augustss {
168 1.11 augustss USB_ATTACH_START(uhub, sc, uaa);
169 1.1 augustss usbd_device_handle dev = uaa->device;
170 1.85.8.1 itohy char *devinfo;
171 1.33 augustss usbd_status err;
172 1.70 augustss struct usbd_hub *hub = NULL;
173 1.1 augustss usb_device_request_t req;
174 1.1 augustss usb_hub_descriptor_t hubdesc;
175 1.42 augustss int p, port, nports, nremov, pwrdly;
176 1.1 augustss usbd_interface_handle iface;
177 1.1 augustss usb_endpoint_descriptor_t *ed;
178 1.70 augustss struct usbd_tt *tts = NULL;
179 1.84 drochner size_t statuslen;
180 1.58 augustss
181 1.1 augustss DPRINTFN(1,("uhub_attach\n"));
182 1.1 augustss sc->sc_hub = dev;
183 1.85.8.2 itohy #ifndef USB_USE_IFATTACH
184 1.85.8.2 itohy sc->sc_proto = sc->sc_hub->ddesc.bDeviceProtocol;
185 1.85.8.2 itohy #else
186 1.85.8.2 itohy sc->sc_proto = uaa->proto;
187 1.85.8.2 itohy #endif /* USB_USE_IFATTACH */
188 1.76 augustss
189 1.85.8.1 itohy devinfo = usbd_devinfo_alloc(dev, 1);
190 1.11 augustss USB_ATTACH_SETUP;
191 1.85.8.1 itohy printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
192 1.85.8.1 itohy usbd_devinfo_free(devinfo);
193 1.1 augustss
194 1.75 augustss if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
195 1.70 augustss printf("%s: %s transaction translator%s\n",
196 1.73 perry USBDEVNAME(sc->sc_dev),
197 1.70 augustss UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
198 1.70 augustss UHUB_IS_SINGLE_TT(sc) ? "" : "s");
199 1.69 augustss }
200 1.69 augustss
201 1.33 augustss err = usbd_set_config_index(dev, 0, 1);
202 1.33 augustss if (err) {
203 1.21 augustss DPRINTF(("%s: configuration failed, error=%s\n",
204 1.33 augustss USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
205 1.11 augustss USB_ATTACH_ERROR_RETURN;
206 1.1 augustss }
207 1.1 augustss
208 1.1 augustss if (dev->depth > USB_HUB_MAX_DEPTH) {
209 1.1 augustss printf("%s: hub depth (%d) exceeded, hub ignored\n",
210 1.11 augustss USBDEVNAME(sc->sc_dev), USB_HUB_MAX_DEPTH);
211 1.11 augustss USB_ATTACH_ERROR_RETURN;
212 1.1 augustss }
213 1.1 augustss
214 1.1 augustss /* Get hub descriptor. */
215 1.1 augustss req.bmRequestType = UT_READ_CLASS_DEVICE;
216 1.1 augustss req.bRequest = UR_GET_DESCRIPTOR;
217 1.85.8.1 itohy USETW2(req.wValue, (dev->address > 1 ? UDESC_HUB : 0), 0);
218 1.1 augustss USETW(req.wIndex, 0);
219 1.1 augustss USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
220 1.1 augustss DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
221 1.33 augustss err = usbd_do_request(dev, &req, &hubdesc);
222 1.11 augustss nports = hubdesc.bNbrPorts;
223 1.33 augustss if (!err && nports > 7) {
224 1.11 augustss USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
225 1.33 augustss err = usbd_do_request(dev, &req, &hubdesc);
226 1.11 augustss }
227 1.33 augustss if (err) {
228 1.21 augustss DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
229 1.33 augustss USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
230 1.11 augustss USB_ATTACH_ERROR_RETURN;
231 1.1 augustss }
232 1.1 augustss
233 1.11 augustss for (nremov = 0, port = 1; port <= nports; port++)
234 1.11 augustss if (!UHD_NOT_REMOV(&hubdesc, port))
235 1.11 augustss nremov++;
236 1.11 augustss printf("%s: %d port%s with %d removable, %s powered\n",
237 1.11 augustss USBDEVNAME(sc->sc_dev), nports, nports != 1 ? "s" : "",
238 1.11 augustss nremov, dev->self_powered ? "self" : "bus");
239 1.11 augustss
240 1.70 augustss if (nports == 0) {
241 1.70 augustss printf("%s: no ports, hub ignored\n", USBDEVNAME(sc->sc_dev));
242 1.70 augustss goto bad;
243 1.70 augustss }
244 1.70 augustss
245 1.1 augustss hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
246 1.18 augustss M_USBDEV, M_NOWAIT);
247 1.33 augustss if (hub == NULL)
248 1.11 augustss USB_ATTACH_ERROR_RETURN;
249 1.1 augustss dev->hub = hub;
250 1.11 augustss dev->hub->hubsoftc = sc;
251 1.1 augustss hub->explore = uhub_explore;
252 1.1 augustss hub->hubdesc = hubdesc;
253 1.58 augustss
254 1.85.8.1 itohy #if 0 /* This chack is disabled since the spec is unclear. */
255 1.85.8.1 itohy DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
256 1.85.8.1 itohy "parent->selfpowered=%d\n",
257 1.85.8.1 itohy dev->self_powered, dev->powersrc->parent,
258 1.85.8.1 itohy dev->powersrc->parent ?
259 1.85.8.1 itohy dev->powersrc->parent->self_powered : 0));
260 1.85.8.1 itohy
261 1.85.8.1 itohy if (!dev->self_powered && dev->powersrc->parent != NULL &&
262 1.85.8.1 itohy !dev->powersrc->parent->self_powered) {
263 1.85.8.1 itohy printf("%s: bus powered hub connected to bus powered hub, "
264 1.85.8.1 itohy "ignored\n", USBDEVNAME(sc->sc_dev));
265 1.85.8.1 itohy goto bad;
266 1.85.8.1 itohy }
267 1.85.8.1 itohy #endif
268 1.85.8.1 itohy
269 1.1 augustss /* Set up interrupt pipe. */
270 1.33 augustss err = usbd_device2interface_handle(dev, 0, &iface);
271 1.33 augustss if (err) {
272 1.11 augustss printf("%s: no interface handle\n", USBDEVNAME(sc->sc_dev));
273 1.18 augustss goto bad;
274 1.1 augustss }
275 1.83 drochner
276 1.83 drochner if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
277 1.83 drochner err = usbd_set_interface(iface, 1);
278 1.83 drochner if (err)
279 1.83 drochner printf("%s: can't enable multiple TTs\n",
280 1.83 drochner USBDEVNAME(sc->sc_dev));
281 1.83 drochner }
282 1.83 drochner
283 1.1 augustss ed = usbd_interface2endpoint_descriptor(iface, 0);
284 1.33 augustss if (ed == NULL) {
285 1.11 augustss printf("%s: no endpoint descriptor\n", USBDEVNAME(sc->sc_dev));
286 1.18 augustss goto bad;
287 1.1 augustss }
288 1.1 augustss if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
289 1.11 augustss printf("%s: bad interrupt endpoint\n", USBDEVNAME(sc->sc_dev));
290 1.18 augustss goto bad;
291 1.1 augustss }
292 1.1 augustss
293 1.84 drochner statuslen = (nports + 1 + 7) / 8;
294 1.84 drochner sc->sc_status = malloc(statuslen, M_USBDEV, M_NOWAIT);
295 1.84 drochner if (!sc->sc_status)
296 1.84 drochner goto bad;
297 1.84 drochner
298 1.33 augustss err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
299 1.58 augustss USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
300 1.84 drochner statuslen, uhub_intr, USBD_DEFAULT_INTERVAL);
301 1.33 augustss if (err) {
302 1.58 augustss printf("%s: cannot open interrupt pipe\n",
303 1.11 augustss USBDEVNAME(sc->sc_dev));
304 1.18 augustss goto bad;
305 1.1 augustss }
306 1.1 augustss
307 1.11 augustss /* Wait with power off for a while. */
308 1.13 augustss usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
309 1.11 augustss
310 1.37 augustss usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
311 1.37 augustss
312 1.42 augustss /*
313 1.42 augustss * To have the best chance of success we do things in the exact same
314 1.42 augustss * order as Windoze98. This should not be necessary, but some
315 1.42 augustss * devices do not follow the USB specs to the letter.
316 1.42 augustss *
317 1.42 augustss * These are the events on the bus when a hub is attached:
318 1.42 augustss * Get device and config descriptors (see attach code)
319 1.42 augustss * Get hub descriptor (see above)
320 1.42 augustss * For all ports
321 1.42 augustss * turn on power
322 1.42 augustss * wait for power to become stable
323 1.42 augustss * (all below happens in explore code)
324 1.42 augustss * For all ports
325 1.42 augustss * clear C_PORT_CONNECTION
326 1.42 augustss * For all ports
327 1.42 augustss * get port status
328 1.42 augustss * if device connected
329 1.57 augustss * wait 100 ms
330 1.42 augustss * turn on reset
331 1.42 augustss * wait
332 1.42 augustss * clear C_PORT_RESET
333 1.42 augustss * get port status
334 1.42 augustss * proceed with device attachment
335 1.42 augustss */
336 1.42 augustss
337 1.85.8.1 itohy if (UHUB_IS_HIGH_SPEED(sc)) {
338 1.70 augustss tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
339 1.85.8.1 itohy sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
340 1.70 augustss if (!tts)
341 1.70 augustss goto bad;
342 1.70 augustss }
343 1.85.8.1 itohy
344 1.42 augustss /* Set up data structures */
345 1.11 augustss for (p = 0; p < nports; p++) {
346 1.11 augustss struct usbd_port *up = &hub->ports[p];
347 1.70 augustss up->device = NULL;
348 1.11 augustss up->parent = dev;
349 1.11 augustss up->portno = p+1;
350 1.42 augustss if (dev->self_powered)
351 1.42 augustss /* Self powered hub, give ports maximum current. */
352 1.42 augustss up->power = USB_MAX_POWER;
353 1.42 augustss else
354 1.42 augustss up->power = USB_MIN_POWER;
355 1.67 petrov up->restartcnt = 0;
356 1.72 joff up->reattach = 0;
357 1.70 augustss if (UHUB_IS_HIGH_SPEED(sc)) {
358 1.70 augustss up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
359 1.70 augustss up->tt->hub = hub;
360 1.70 augustss } else {
361 1.70 augustss up->tt = NULL;
362 1.70 augustss }
363 1.42 augustss }
364 1.42 augustss
365 1.42 augustss /* XXX should check for none, individual, or ganged power? */
366 1.42 augustss
367 1.42 augustss pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
368 1.42 augustss + USB_EXTRA_POWER_UP_TIME;
369 1.42 augustss for (port = 1; port <= nports; port++) {
370 1.42 augustss /* Turn the power on. */
371 1.42 augustss err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
372 1.33 augustss if (err)
373 1.58 augustss printf("%s: port %d power on failed, %s\n",
374 1.42 augustss USBDEVNAME(sc->sc_dev), port,
375 1.42 augustss usbd_errstr(err));
376 1.42 augustss DPRINTF(("usb_init_port: turn on port %d power\n", port));
377 1.42 augustss /* Wait for stable power. */
378 1.42 augustss usbd_delay_ms(dev, pwrdly);
379 1.1 augustss }
380 1.42 augustss
381 1.42 augustss /* The usual exploration will finish the setup. */
382 1.42 augustss
383 1.1 augustss sc->sc_running = 1;
384 1.11 augustss
385 1.11 augustss USB_ATTACH_SUCCESS_RETURN;
386 1.11 augustss
387 1.18 augustss bad:
388 1.84 drochner if (sc->sc_status)
389 1.84 drochner free(sc->sc_status, M_USBDEV);
390 1.70 augustss if (hub)
391 1.70 augustss free(hub, M_USBDEV);
392 1.70 augustss dev->hub = NULL;
393 1.18 augustss USB_ATTACH_ERROR_RETURN;
394 1.11 augustss }
395 1.11 augustss
396 1.1 augustss usbd_status
397 1.45 augustss uhub_explore(usbd_device_handle dev)
398 1.1 augustss {
399 1.1 augustss usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
400 1.11 augustss struct uhub_softc *sc = dev->hub->hubsoftc;
401 1.1 augustss struct usbd_port *up;
402 1.33 augustss usbd_status err;
403 1.56 augustss int speed;
404 1.1 augustss int port;
405 1.72 joff int change, status, reconnect;
406 1.1 augustss
407 1.11 augustss DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
408 1.1 augustss
409 1.1 augustss if (!sc->sc_running)
410 1.1 augustss return (USBD_NOT_STARTED);
411 1.1 augustss
412 1.1 augustss /* Ignore hubs that are too deep. */
413 1.1 augustss if (dev->depth > USB_HUB_MAX_DEPTH)
414 1.1 augustss return (USBD_TOO_DEEP);
415 1.1 augustss
416 1.84 drochner for (port = 1; port <= hd->bNbrPorts; port++) {
417 1.1 augustss up = &dev->hub->ports[port-1];
418 1.33 augustss err = usbd_get_port_status(dev, port, &up->status);
419 1.33 augustss if (err) {
420 1.10 drochner DPRINTF(("uhub_explore: get port status failed, "
421 1.33 augustss "error=%s\n", usbd_errstr(err)));
422 1.1 augustss continue;
423 1.1 augustss }
424 1.1 augustss status = UGETW(up->status.wPortStatus);
425 1.1 augustss change = UGETW(up->status.wPortChange);
426 1.72 joff reconnect = up->reattach;
427 1.72 joff up->reattach = 0;
428 1.85.8.1 itohy DPRINTFN(3,("%s: uhub_explore: port %d status 0x%04x 0x%04x\n",
429 1.51 augustss USBDEVNAME(sc->sc_dev), port, status, change));
430 1.11 augustss if (change & UPS_C_PORT_ENABLED) {
431 1.85.8.1 itohy DPRINTF(("uhub_explore: C_PORT_ENABLED 0x%x\n", change));
432 1.11 augustss usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
433 1.68 mycroft if (change & UPS_C_CONNECT_STATUS) {
434 1.68 mycroft /* Ignore the port error if the device
435 1.68 mycroft vanished. */
436 1.68 mycroft } else if (status & UPS_PORT_ENABLED) {
437 1.11 augustss printf("%s: illegal enable change, port %d\n",
438 1.11 augustss USBDEVNAME(sc->sc_dev), port);
439 1.11 augustss } else {
440 1.11 augustss /* Port error condition. */
441 1.47 augustss if (up->restartcnt) /* no message first time */
442 1.11 augustss printf("%s: port error, restarting "
443 1.11 augustss "port %d\n",
444 1.11 augustss USBDEVNAME(sc->sc_dev), port);
445 1.47 augustss
446 1.47 augustss if (up->restartcnt++ < USBD_RESTART_MAX)
447 1.11 augustss goto disco;
448 1.47 augustss else
449 1.11 augustss printf("%s: port error, giving up "
450 1.11 augustss "port %d\n",
451 1.11 augustss USBDEVNAME(sc->sc_dev), port);
452 1.11 augustss }
453 1.11 augustss }
454 1.72 joff if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
455 1.35 augustss DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
456 1.35 augustss "STATUS\n", port));
457 1.1 augustss /* No status change, just do recursive explore. */
458 1.52 augustss if (up->device != NULL && up->device->hub != NULL)
459 1.11 augustss up->device->hub->explore(up->device);
460 1.52 augustss #if 0 && defined(DIAGNOSTIC)
461 1.58 augustss if (up->device == NULL &&
462 1.52 augustss (status & UPS_CURRENT_CONNECT_STATUS))
463 1.52 augustss printf("%s: connected, no device\n",
464 1.52 augustss USBDEVNAME(sc->sc_dev));
465 1.52 augustss #endif
466 1.1 augustss continue;
467 1.1 augustss }
468 1.42 augustss
469 1.42 augustss /* We have a connect status change, handle it. */
470 1.42 augustss
471 1.1 augustss DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
472 1.1 augustss dev->address, port));
473 1.1 augustss usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
474 1.42 augustss /*usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);*/
475 1.1 augustss /*
476 1.1 augustss * If there is already a device on the port the change status
477 1.1 augustss * must mean that is has disconnected. Looking at the
478 1.1 augustss * current connect status is not enough to figure this out
479 1.1 augustss * since a new unit may have been connected before we handle
480 1.1 augustss * the disconnect.
481 1.1 augustss */
482 1.11 augustss disco:
483 1.33 augustss if (up->device != NULL) {
484 1.1 augustss /* Disconnected */
485 1.35 augustss DPRINTF(("uhub_explore: device addr=%d disappeared "
486 1.31 augustss "on port %d\n", up->device->address, port));
487 1.31 augustss usb_disconnect_port(up, USBDEV(sc->sc_dev));
488 1.58 augustss usbd_clear_port_feature(dev, port,
489 1.1 augustss UHF_C_PORT_CONNECTION);
490 1.1 augustss }
491 1.35 augustss if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
492 1.42 augustss /* Nothing connected, just ignore it. */
493 1.35 augustss DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
494 1.35 augustss "_STATUS\n", port));
495 1.1 augustss continue;
496 1.35 augustss }
497 1.1 augustss
498 1.1 augustss /* Connected */
499 1.42 augustss
500 1.42 augustss if (!(status & UPS_PORT_POWER))
501 1.42 augustss printf("%s: strange, connected port %d has no power\n",
502 1.42 augustss USBDEVNAME(sc->sc_dev), port);
503 1.42 augustss
504 1.1 augustss /* Wait for maximum device power up time. */
505 1.13 augustss usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
506 1.11 augustss
507 1.1 augustss /* Reset port, which implies enabling it. */
508 1.35 augustss if (usbd_reset_port(dev, port, &up->status)) {
509 1.55 augustss printf("%s: port %d reset failed\n",
510 1.55 augustss USBDEVNAME(sc->sc_dev), port);
511 1.54 augustss continue;
512 1.54 augustss }
513 1.54 augustss /* Get port status again, it might have changed during reset */
514 1.54 augustss err = usbd_get_port_status(dev, port, &up->status);
515 1.54 augustss if (err) {
516 1.54 augustss DPRINTF(("uhub_explore: get port status failed, "
517 1.54 augustss "error=%s\n", usbd_errstr(err)));
518 1.54 augustss continue;
519 1.54 augustss }
520 1.54 augustss status = UGETW(up->status.wPortStatus);
521 1.54 augustss change = UGETW(up->status.wPortChange);
522 1.54 augustss if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
523 1.54 augustss /* Nothing connected, just ignore it. */
524 1.54 augustss #ifdef DIAGNOSTIC
525 1.56 augustss printf("%s: port %d, device disappeared after reset\n",
526 1.54 augustss USBDEVNAME(sc->sc_dev), port);
527 1.54 augustss #endif
528 1.1 augustss continue;
529 1.35 augustss }
530 1.1 augustss
531 1.70 augustss #if 0
532 1.69 augustss if (UHUB_IS_HIGH_SPEED(sc) && !(status & UPS_HIGH_SPEED)) {
533 1.69 augustss printf("%s: port %d, transaction translation not "
534 1.69 augustss "implemented, low/full speed device ignored\n",
535 1.69 augustss USBDEVNAME(sc->sc_dev), port);
536 1.69 augustss continue;
537 1.69 augustss }
538 1.70 augustss #endif
539 1.69 augustss
540 1.56 augustss /* Figure out device speed */
541 1.56 augustss if (status & UPS_HIGH_SPEED)
542 1.56 augustss speed = USB_SPEED_HIGH;
543 1.56 augustss else if (status & UPS_LOW_SPEED)
544 1.56 augustss speed = USB_SPEED_LOW;
545 1.56 augustss else
546 1.56 augustss speed = USB_SPEED_FULL;
547 1.1 augustss /* Get device info and set its address. */
548 1.58 augustss err = usbd_new_device(USBDEV(sc->sc_dev), dev->bus,
549 1.85.8.1 itohy dev->depth + 1, speed, port, up);
550 1.1 augustss /* XXX retry a few times? */
551 1.33 augustss if (err) {
552 1.10 drochner DPRINTFN(-1,("uhub_explore: usb_new_device failed, "
553 1.33 augustss "error=%s\n", usbd_errstr(err)));
554 1.1 augustss /* Avoid addressing problems by disabling. */
555 1.1 augustss /* usbd_reset_port(dev, port, &up->status); */
556 1.30 augustss
557 1.58 augustss /*
558 1.30 augustss * The unit refused to accept a new address, or had
559 1.30 augustss * some other serious problem. Since we cannot leave
560 1.30 augustss * at 0 we have to disable the port instead.
561 1.30 augustss */
562 1.85.8.1 itohy printf("%s: device problem (%s), disabling port %d\n",
563 1.85.8.1 itohy USBDEVNAME(sc->sc_dev), usbd_errstr(err), port);
564 1.30 augustss usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
565 1.1 augustss } else {
566 1.47 augustss /* The port set up succeeded, reset error count. */
567 1.47 augustss up->restartcnt = 0;
568 1.47 augustss
569 1.34 augustss if (up->device->hub)
570 1.11 augustss up->device->hub->explore(up->device);
571 1.1 augustss }
572 1.1 augustss }
573 1.1 augustss return (USBD_NORMAL_COMPLETION);
574 1.1 augustss }
575 1.1 augustss
576 1.32 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
577 1.18 augustss int
578 1.45 augustss uhub_activate(device_ptr_t self, enum devact act)
579 1.18 augustss {
580 1.27 augustss struct uhub_softc *sc = (struct uhub_softc *)self;
581 1.39 augustss struct usbd_hub *hub = sc->sc_hub->hub;
582 1.34 augustss usbd_device_handle dev;
583 1.34 augustss int nports, port, i;
584 1.27 augustss
585 1.23 augustss switch (act) {
586 1.23 augustss case DVACT_ACTIVATE:
587 1.23 augustss return (EOPNOTSUPP);
588 1.23 augustss
589 1.23 augustss case DVACT_DEACTIVATE:
590 1.39 augustss if (hub == NULL) /* malfunctioning hub */
591 1.39 augustss break;
592 1.39 augustss nports = hub->hubdesc.bNbrPorts;
593 1.34 augustss for(port = 0; port < nports; port++) {
594 1.39 augustss dev = hub->ports[port].device;
595 1.46 augustss if (dev != NULL && dev->subdevs != NULL) {
596 1.46 augustss for (i = 0; dev->subdevs[i] != NULL; i++)
597 1.27 augustss config_deactivate(dev->subdevs[i]);
598 1.27 augustss }
599 1.27 augustss }
600 1.23 augustss break;
601 1.23 augustss }
602 1.18 augustss return (0);
603 1.18 augustss }
604 1.32 augustss #endif
605 1.18 augustss
606 1.18 augustss /*
607 1.18 augustss * Called from process context when the hub is gone.
608 1.18 augustss * Detach all devices on active ports.
609 1.18 augustss */
610 1.32 augustss USB_DETACH(uhub)
611 1.18 augustss {
612 1.32 augustss USB_DETACH_START(uhub, sc);
613 1.39 augustss struct usbd_hub *hub = sc->sc_hub->hub;
614 1.18 augustss struct usbd_port *rup;
615 1.32 augustss int port, nports;
616 1.18 augustss
617 1.32 augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
618 1.18 augustss DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
619 1.32 augustss #elif defined(__FreeBSD__)
620 1.32 augustss DPRINTF(("uhub_detach: sc=%port\n", sc));
621 1.32 augustss #endif
622 1.18 augustss
623 1.39 augustss if (hub == NULL) /* Must be partially working */
624 1.18 augustss return (0);
625 1.18 augustss
626 1.18 augustss usbd_abort_pipe(sc->sc_ipipe);
627 1.18 augustss usbd_close_pipe(sc->sc_ipipe);
628 1.18 augustss
629 1.39 augustss nports = hub->hubdesc.bNbrPorts;
630 1.32 augustss for(port = 0; port < nports; port++) {
631 1.39 augustss rup = &hub->ports[port];
632 1.18 augustss if (rup->device)
633 1.31 augustss usb_disconnect_port(rup, self);
634 1.18 augustss }
635 1.58 augustss
636 1.39 augustss usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
637 1.39 augustss USBDEV(sc->sc_dev));
638 1.37 augustss
639 1.70 augustss if (hub->ports[0].tt)
640 1.70 augustss free(hub->ports[0].tt, M_USBDEV);
641 1.71 augustss free(hub, M_USBDEV);
642 1.39 augustss sc->sc_hub->hub = NULL;
643 1.84 drochner if (sc->sc_status)
644 1.84 drochner free(sc->sc_status, M_USBDEV);
645 1.18 augustss
646 1.18 augustss return (0);
647 1.18 augustss }
648 1.34 augustss
649 1.34 augustss #if defined(__FreeBSD__)
650 1.85.8.1 itohy int
651 1.85.8.1 itohy uhub_child_location_str(device_t cbdev, device_t child, char *buf,
652 1.85.8.1 itohy size_t buflen)
653 1.34 augustss {
654 1.85.8.1 itohy struct uhub_softc *sc = device_get_softc(cbdev);
655 1.85.8.1 itohy usbd_device_handle devhub = sc->sc_hub;
656 1.85.8.1 itohy usbd_device_handle dev;
657 1.85.8.1 itohy int nports;
658 1.85.8.1 itohy int port;
659 1.85.8.1 itohy int i;
660 1.85.8.1 itohy
661 1.85.8.1 itohy mtx_lock(&Giant);
662 1.85.8.1 itohy nports = devhub->hub->hubdesc.bNbrPorts;
663 1.85.8.1 itohy for (port = 0; port < nports; port++) {
664 1.85.8.1 itohy dev = devhub->hub->ports[port].device;
665 1.85.8.1 itohy if (dev && dev->subdevs) {
666 1.85.8.1 itohy for (i = 0; dev->subdevs[i]; i++) {
667 1.85.8.1 itohy if (dev->subdevs[i] == child) {
668 1.85.8.1 itohy if (dev->ifacenums == NULL) {
669 1.85.8.1 itohy snprintf(buf, buflen,
670 1.85.8.1 itohy "port=%i", port);
671 1.85.8.1 itohy } else {
672 1.85.8.1 itohy snprintf(buf, buflen,
673 1.85.8.1 itohy "port=%i interface=%i",
674 1.85.8.1 itohy port, dev->ifacenums[i]);
675 1.85.8.1 itohy }
676 1.85.8.1 itohy goto found_dev;
677 1.85.8.1 itohy }
678 1.85.8.1 itohy }
679 1.85.8.1 itohy }
680 1.85.8.1 itohy }
681 1.85.8.1 itohy DPRINTFN(0,("uhub_child_location_str: device not on hub\n"));
682 1.85.8.1 itohy buf[0] = '\0';
683 1.85.8.1 itohy found_dev:
684 1.85.8.1 itohy mtx_unlock(&Giant);
685 1.85.8.1 itohy return (0);
686 1.34 augustss }
687 1.34 augustss
688 1.85.8.1 itohy int
689 1.85.8.1 itohy uhub_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
690 1.85.8.1 itohy size_t buflen)
691 1.85.8.1 itohy {
692 1.85.8.1 itohy struct uhub_softc *sc = device_get_softc(cbdev);
693 1.85.8.1 itohy usbd_device_handle devhub = sc->sc_hub;
694 1.85.8.1 itohy usbd_device_handle dev;
695 1.85.8.1 itohy struct usbd_interface *iface;
696 1.85.8.1 itohy char serial[128];
697 1.85.8.1 itohy int nports;
698 1.85.8.1 itohy int port;
699 1.85.8.1 itohy int i;
700 1.85.8.1 itohy
701 1.85.8.1 itohy mtx_lock(&Giant);
702 1.85.8.1 itohy nports = devhub->hub->hubdesc.bNbrPorts;
703 1.85.8.1 itohy for (port = 0; port < nports; port++) {
704 1.85.8.1 itohy dev = devhub->hub->ports[port].device;
705 1.85.8.1 itohy if (dev && dev->subdevs) {
706 1.85.8.1 itohy for (i = 0; dev->subdevs[i]; i++) {
707 1.85.8.1 itohy if (dev->subdevs[i] == child) {
708 1.85.8.1 itohy goto found_dev;
709 1.85.8.1 itohy }
710 1.85.8.1 itohy }
711 1.85.8.1 itohy }
712 1.85.8.1 itohy }
713 1.85.8.1 itohy DPRINTFN(0,("uhub_child_pnpinfo_str: device not on hub\n"));
714 1.85.8.1 itohy buf[0] = '\0';
715 1.85.8.1 itohy mtx_unlock(&Giant);
716 1.85.8.1 itohy return (0);
717 1.85.8.1 itohy
718 1.85.8.1 itohy found_dev:
719 1.85.8.1 itohy /* XXX can sleep */
720 1.85.8.1 itohy (void)usbd_get_string(dev, dev->ddesc.iSerialNumber, &serial[0]);
721 1.85.8.1 itohy if (dev->ifacenums == NULL) {
722 1.85.8.1 itohy snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
723 1.85.8.1 itohy "devclass=0x%02x devsubclass=0x%02x "
724 1.85.8.1 itohy "release=0x%04x sernum=\"%s\"",
725 1.85.8.1 itohy UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
726 1.85.8.1 itohy dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
727 1.85.8.1 itohy UGETW(dev->ddesc.bcdDevice), serial);
728 1.85.8.1 itohy } else {
729 1.85.8.1 itohy iface = &dev->ifaces[dev->ifacenums[i]];
730 1.85.8.1 itohy snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
731 1.85.8.1 itohy "devclass=0x%02x devsubclass=0x%02x "
732 1.85.8.1 itohy "release=0x%04x sernum=\"%s\" "
733 1.85.8.1 itohy "intclass=0x%02x intsubclass=0x%02x",
734 1.85.8.1 itohy UGETW(dev->ddesc.idVendor), UGETW(dev->ddesc.idProduct),
735 1.85.8.1 itohy dev->ddesc.bDeviceClass, dev->ddesc.bDeviceSubClass,
736 1.85.8.1 itohy UGETW(dev->ddesc.bcdDevice), serial,
737 1.85.8.1 itohy iface->idesc->bInterfaceClass,
738 1.85.8.1 itohy iface->idesc->bInterfaceSubClass);
739 1.85.8.1 itohy }
740 1.85.8.1 itohy mtx_unlock(&Giant);
741 1.85.8.1 itohy return (0);
742 1.85.8.1 itohy }
743 1.85.8.1 itohy #endif /* defined(__FreeBSD__) */
744 1.18 augustss
745 1.18 augustss /*
746 1.18 augustss * Hub interrupt.
747 1.18 augustss * This an indication that some port has changed status.
748 1.18 augustss * Notify the bus event handler thread that we need
749 1.18 augustss * to be explored again.
750 1.18 augustss */
751 1.1 augustss void
752 1.81 christos uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr,
753 1.79 christos usbd_status status)
754 1.1 augustss {
755 1.1 augustss struct uhub_softc *sc = addr;
756 1.1 augustss
757 1.84 drochner #if 0
758 1.84 drochner void *buf; int cnt;
759 1.84 drochner usbd_get_xfer_status(xfer, NULL, &buf, &cnt, NULL);
760 1.84 drochner #endif
761 1.11 augustss DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
762 1.50 augustss if (status == USBD_STALLED)
763 1.9 augustss usbd_clear_endpoint_stall_async(sc->sc_ipipe);
764 1.50 augustss else if (status == USBD_NORMAL_COMPLETION)
765 1.50 augustss usb_needs_explore(sc->sc_hub);
766 1.1 augustss }
767 1.11 augustss
768 1.11 augustss #if defined(__FreeBSD__)
769 1.19 augustss DRIVER_MODULE(uhub, usb, uhubroot_driver, uhubroot_devclass, 0, 0);
770 1.19 augustss DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, usbd_driver_load, 0);
771 1.11 augustss #endif
772