uhub.c revision 1.128 1 1.128 skrll /* $NetBSD: uhub.c,v 1.128 2015/03/26 16:19:26 skrll Exp $ */
2 1.34 augustss /* $FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma Exp $ */
3 1.1 augustss
4 1.1 augustss /*
5 1.74 mycroft * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
6 1.1 augustss * All rights reserved.
7 1.1 augustss *
8 1.6 augustss * This code is derived from software contributed to The NetBSD Foundation
9 1.44 augustss * by Lennart Augustsson (lennart (at) augustsson.net) at
10 1.6 augustss * Carlstedt Research & Technology.
11 1.1 augustss *
12 1.1 augustss * Redistribution and use in source and binary forms, with or without
13 1.1 augustss * modification, are permitted provided that the following conditions
14 1.1 augustss * are met:
15 1.1 augustss * 1. Redistributions of source code must retain the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer.
17 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 augustss * notice, this list of conditions and the following disclaimer in the
19 1.1 augustss * documentation and/or other materials provided with the distribution.
20 1.1 augustss *
21 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
32 1.15 augustss */
33 1.15 augustss
34 1.15 augustss /*
35 1.64 ichiro * USB spec: http://www.usb.org/developers/docs/usbspec.zip
36 1.1 augustss */
37 1.53 lukem
38 1.53 lukem #include <sys/cdefs.h>
39 1.128 skrll __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.128 2015/03/26 16:19:26 skrll Exp $");
40 1.1 augustss
41 1.1 augustss #include <sys/param.h>
42 1.127 skrll
43 1.1 augustss #include <sys/systm.h>
44 1.127 skrll #include <sys/device.h>
45 1.1 augustss #include <sys/kernel.h>
46 1.1 augustss #include <sys/malloc.h>
47 1.34 augustss #include <sys/proc.h>
48 1.127 skrll #include <sys/sysctl.h>
49 1.28 augustss
50 1.90 ad #include <sys/bus.h>
51 1.1 augustss
52 1.1 augustss #include <dev/usb/usb.h>
53 1.1 augustss #include <dev/usb/usbdi.h>
54 1.1 augustss #include <dev/usb/usbdi_util.h>
55 1.1 augustss #include <dev/usb/usbdivar.h>
56 1.127 skrll #include <dev/usb/usbhist.h>
57 1.1 augustss
58 1.127 skrll #ifdef USB_DEBUG
59 1.127 skrll #ifndef UHUB_DEBUG
60 1.127 skrll #define uhubdebug 0
61 1.1 augustss #else
62 1.127 skrll static int uhubdebug = 0;
63 1.127 skrll
64 1.127 skrll SYSCTL_SETUP(sysctl_hw_uhub_setup, "sysctl hw.uhub setup")
65 1.127 skrll {
66 1.127 skrll int err;
67 1.127 skrll const struct sysctlnode *rnode;
68 1.127 skrll const struct sysctlnode *cnode;
69 1.127 skrll
70 1.127 skrll err = sysctl_createv(clog, 0, NULL, &rnode,
71 1.127 skrll CTLFLAG_PERMANENT, CTLTYPE_NODE, "uhub",
72 1.127 skrll SYSCTL_DESCR("uhub global controls"),
73 1.127 skrll NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
74 1.127 skrll
75 1.127 skrll if (err)
76 1.127 skrll goto fail;
77 1.127 skrll
78 1.127 skrll /* control debugging printfs */
79 1.127 skrll err = sysctl_createv(clog, 0, &rnode, &cnode,
80 1.127 skrll CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
81 1.127 skrll "debug", SYSCTL_DESCR("Enable debugging output"),
82 1.127 skrll NULL, 0, &uhubdebug, sizeof(uhubdebug), CTL_CREATE, CTL_EOL);
83 1.127 skrll if (err)
84 1.127 skrll goto fail;
85 1.127 skrll
86 1.127 skrll return;
87 1.127 skrll fail:
88 1.127 skrll aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
89 1.127 skrll }
90 1.127 skrll
91 1.127 skrll #endif /* UHUB_DEBUG */
92 1.127 skrll #endif /* USB_DEBUG */
93 1.127 skrll
94 1.127 skrll #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(uhubdebug,1,FMT,A,B,C,D)
95 1.127 skrll #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(uhubdebug,N,FMT,A,B,C,D)
96 1.127 skrll #define UHUBHIST_FUNC() USBHIST_FUNC()
97 1.127 skrll #define UHUBHIST_CALLED(name) USBHIST_CALLED(uhubdebug)
98 1.1 augustss
99 1.1 augustss struct uhub_softc {
100 1.111 dyoung device_t sc_dev; /* base device */
101 1.11 augustss usbd_device_handle sc_hub; /* USB device */
102 1.86 drochner int sc_proto; /* device protocol */
103 1.11 augustss usbd_pipe_handle sc_ipipe; /* interrupt pipe */
104 1.87 drochner
105 1.87 drochner /* XXX second buffer needed because we can't suspend pipes yet */
106 1.87 drochner u_int8_t *sc_statusbuf;
107 1.84 drochner u_int8_t *sc_status;
108 1.87 drochner size_t sc_statuslen;
109 1.87 drochner int sc_explorepending;
110 1.101 drochner int sc_isehciroothub; /* see comment in uhub_intr() */
111 1.87 drochner
112 1.11 augustss u_char sc_running;
113 1.1 augustss };
114 1.87 drochner
115 1.86 drochner #define UHUB_IS_HIGH_SPEED(sc) ((sc)->sc_proto != UDPROTO_FSHUB)
116 1.86 drochner #define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT)
117 1.1 augustss
118 1.87 drochner #define PORTSTAT_ISSET(sc, port) \
119 1.87 drochner ((sc)->sc_status[(port) / 8] & (1 << ((port) % 8)))
120 1.87 drochner
121 1.45 augustss Static usbd_status uhub_explore(usbd_device_handle hub);
122 1.45 augustss Static void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
123 1.1 augustss
124 1.32 augustss
125 1.58 augustss /*
126 1.32 augustss * We need two attachment points:
127 1.32 augustss * hub to usb and hub to hub
128 1.32 augustss * Every other driver only connects to hubs
129 1.32 augustss */
130 1.1 augustss
131 1.98 cube int uhub_match(device_t, cfdata_t, void *);
132 1.95 dyoung void uhub_attach(device_t, device_t, void *);
133 1.103 kent int uhub_rescan(device_t, const char *, const int *);
134 1.95 dyoung void uhub_childdet(device_t, device_t);
135 1.95 dyoung int uhub_detach(device_t, int);
136 1.95 dyoung extern struct cfdriver uhub_cd;
137 1.104 dyoung CFATTACH_DECL3_NEW(uhub, sizeof(struct uhub_softc), uhub_match,
138 1.108 dyoung uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet,
139 1.104 dyoung DVF_DETACH_SHUTDOWN);
140 1.99 drochner CFATTACH_DECL2_NEW(uroothub, sizeof(struct uhub_softc), uhub_match,
141 1.108 dyoung uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet);
142 1.1 augustss
143 1.109 pooka /*
144 1.109 pooka * Setting this to 1 makes sure than an uhub attaches even at higher
145 1.109 pooka * priority than ugen when ugen_override is set to 1. This allows to
146 1.109 pooka * probe the whole USB bus and attach functions with ugen.
147 1.109 pooka */
148 1.109 pooka int uhub_ubermatch = 0;
149 1.109 pooka
150 1.105 dyoung int
151 1.105 dyoung uhub_match(device_t parent, cfdata_t match, void *aux)
152 1.1 augustss {
153 1.107 dyoung struct usb_attach_arg *uaa = aux;
154 1.109 pooka int matchvalue;
155 1.109 pooka
156 1.127 skrll UHUBHIST_FUNC(); UHUBHIST_CALLED();
157 1.127 skrll
158 1.109 pooka if (uhub_ubermatch)
159 1.109 pooka matchvalue = UMATCH_HIGHEST+1;
160 1.109 pooka else
161 1.109 pooka matchvalue = UMATCH_DEVCLASS_DEVSUBCLASS;
162 1.58 augustss
163 1.127 skrll DPRINTFN(5, "uaa=%p", uaa, 0, 0, 0);
164 1.58 augustss /*
165 1.1 augustss * The subclass for hubs seems to be 0 for some and 1 for others,
166 1.1 augustss * so we just ignore the subclass.
167 1.1 augustss */
168 1.86 drochner if (uaa->class == UDCLASS_HUB)
169 1.109 pooka return (matchvalue);
170 1.1 augustss return (UMATCH_NONE);
171 1.1 augustss }
172 1.1 augustss
173 1.105 dyoung void
174 1.105 dyoung uhub_attach(device_t parent, device_t self, void *aux)
175 1.1 augustss {
176 1.107 dyoung struct uhub_softc *sc = device_private(self);
177 1.107 dyoung struct usb_attach_arg *uaa = aux;
178 1.1 augustss usbd_device_handle dev = uaa->device;
179 1.76 augustss char *devinfop;
180 1.33 augustss usbd_status err;
181 1.70 augustss struct usbd_hub *hub = NULL;
182 1.1 augustss usb_device_request_t req;
183 1.1 augustss usb_hub_descriptor_t hubdesc;
184 1.42 augustss int p, port, nports, nremov, pwrdly;
185 1.1 augustss usbd_interface_handle iface;
186 1.1 augustss usb_endpoint_descriptor_t *ed;
187 1.83 drochner #if 0 /* notyet */
188 1.70 augustss struct usbd_tt *tts = NULL;
189 1.83 drochner #endif
190 1.58 augustss
191 1.127 skrll UHUBHIST_FUNC(); UHUBHIST_CALLED();
192 1.127 skrll
193 1.98 cube sc->sc_dev = self;
194 1.1 augustss sc->sc_hub = dev;
195 1.86 drochner sc->sc_proto = uaa->proto;
196 1.76 augustss
197 1.76 augustss devinfop = usbd_devinfo_alloc(dev, 1);
198 1.96 jmcneill aprint_naive("\n");
199 1.96 jmcneill aprint_normal(": %s\n", devinfop);
200 1.76 augustss usbd_devinfo_free(devinfop);
201 1.1 augustss
202 1.75 augustss if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
203 1.98 cube aprint_normal_dev(self, "%s transaction translator%s\n",
204 1.70 augustss UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
205 1.70 augustss UHUB_IS_SINGLE_TT(sc) ? "" : "s");
206 1.69 augustss }
207 1.69 augustss
208 1.33 augustss err = usbd_set_config_index(dev, 0, 1);
209 1.33 augustss if (err) {
210 1.127 skrll DPRINTF("configuration failed, sc %p error %d", sc, err, 0, 0);
211 1.107 dyoung return;
212 1.1 augustss }
213 1.1 augustss
214 1.1 augustss if (dev->depth > USB_HUB_MAX_DEPTH) {
215 1.98 cube aprint_error_dev(self,
216 1.98 cube "hub depth (%d) exceeded, hub ignored\n",
217 1.98 cube USB_HUB_MAX_DEPTH);
218 1.107 dyoung return;
219 1.1 augustss }
220 1.1 augustss
221 1.1 augustss /* Get hub descriptor. */
222 1.1 augustss req.bmRequestType = UT_READ_CLASS_DEVICE;
223 1.1 augustss req.bRequest = UR_GET_DESCRIPTOR;
224 1.65 toshii USETW2(req.wValue, UDESC_HUB, 0);
225 1.1 augustss USETW(req.wIndex, 0);
226 1.1 augustss USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
227 1.128 skrll DPRINTF("uhub %d getting hub descriptor", device_unit(self), 0, 0, 0);
228 1.33 augustss err = usbd_do_request(dev, &req, &hubdesc);
229 1.11 augustss nports = hubdesc.bNbrPorts;
230 1.33 augustss if (!err && nports > 7) {
231 1.11 augustss USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
232 1.33 augustss err = usbd_do_request(dev, &req, &hubdesc);
233 1.11 augustss }
234 1.33 augustss if (err) {
235 1.128 skrll DPRINTF("getting hub descriptor failed, uhub %d error %d",
236 1.128 skrll device_unit(self), err, 0, 0);
237 1.107 dyoung return;
238 1.1 augustss }
239 1.1 augustss
240 1.11 augustss for (nremov = 0, port = 1; port <= nports; port++)
241 1.11 augustss if (!UHD_NOT_REMOV(&hubdesc, port))
242 1.11 augustss nremov++;
243 1.98 cube aprint_verbose_dev(self, "%d port%s with %d removable, %s powered\n",
244 1.98 cube nports, nports != 1 ? "s" : "", nremov,
245 1.98 cube dev->self_powered ? "self" : "bus");
246 1.11 augustss
247 1.70 augustss if (nports == 0) {
248 1.98 cube aprint_debug_dev(self, "no ports, hub ignored\n");
249 1.70 augustss goto bad;
250 1.70 augustss }
251 1.70 augustss
252 1.1 augustss hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
253 1.18 augustss M_USBDEV, M_NOWAIT);
254 1.33 augustss if (hub == NULL)
255 1.107 dyoung return;
256 1.1 augustss dev->hub = hub;
257 1.11 augustss dev->hub->hubsoftc = sc;
258 1.1 augustss hub->explore = uhub_explore;
259 1.1 augustss hub->hubdesc = hubdesc;
260 1.58 augustss
261 1.1 augustss /* Set up interrupt pipe. */
262 1.33 augustss err = usbd_device2interface_handle(dev, 0, &iface);
263 1.33 augustss if (err) {
264 1.98 cube aprint_error_dev(self, "no interface handle\n");
265 1.18 augustss goto bad;
266 1.1 augustss }
267 1.83 drochner
268 1.83 drochner if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
269 1.83 drochner err = usbd_set_interface(iface, 1);
270 1.83 drochner if (err)
271 1.98 cube aprint_error_dev(self, "can't enable multiple TTs\n");
272 1.83 drochner }
273 1.83 drochner
274 1.1 augustss ed = usbd_interface2endpoint_descriptor(iface, 0);
275 1.33 augustss if (ed == NULL) {
276 1.98 cube aprint_error_dev(self, "no endpoint descriptor\n");
277 1.18 augustss goto bad;
278 1.1 augustss }
279 1.1 augustss if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
280 1.98 cube aprint_error_dev(self, "bad interrupt endpoint\n");
281 1.18 augustss goto bad;
282 1.1 augustss }
283 1.1 augustss
284 1.87 drochner sc->sc_statuslen = (nports + 1 + 7) / 8;
285 1.87 drochner sc->sc_statusbuf = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT);
286 1.87 drochner if (!sc->sc_statusbuf)
287 1.87 drochner goto bad;
288 1.87 drochner sc->sc_status = malloc(sc->sc_statuslen, M_USBDEV, M_NOWAIT);
289 1.84 drochner if (!sc->sc_status)
290 1.84 drochner goto bad;
291 1.101 drochner if (device_is_a(device_parent(device_parent(sc->sc_dev)), "ehci"))
292 1.101 drochner sc->sc_isehciroothub = 1;
293 1.84 drochner
294 1.87 drochner /* force initial scan */
295 1.87 drochner memset(sc->sc_status, 0xff, sc->sc_statuslen);
296 1.87 drochner sc->sc_explorepending = 1;
297 1.87 drochner
298 1.33 augustss err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
299 1.122 jmcneill USBD_SHORT_XFER_OK|USBD_MPSAFE, &sc->sc_ipipe, sc,
300 1.122 jmcneill sc->sc_statusbuf, sc->sc_statuslen,
301 1.122 jmcneill uhub_intr, USBD_DEFAULT_INTERVAL);
302 1.33 augustss if (err) {
303 1.98 cube aprint_error_dev(self, "cannot open interrupt pipe\n");
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.105 dyoung usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, 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.123 jakllsch * order as Windows 98. 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.83 drochner #if 0
338 1.78 christos if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) {
339 1.70 augustss tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
340 1.70 augustss sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
341 1.70 augustss if (!tts)
342 1.70 augustss goto bad;
343 1.70 augustss }
344 1.83 drochner #endif
345 1.42 augustss /* Set up data structures */
346 1.11 augustss for (p = 0; p < nports; p++) {
347 1.11 augustss struct usbd_port *up = &hub->ports[p];
348 1.70 augustss up->device = NULL;
349 1.11 augustss up->parent = dev;
350 1.11 augustss up->portno = p+1;
351 1.42 augustss if (dev->self_powered)
352 1.42 augustss /* Self powered hub, give ports maximum current. */
353 1.42 augustss up->power = USB_MAX_POWER;
354 1.42 augustss else
355 1.42 augustss up->power = USB_MIN_POWER;
356 1.67 petrov up->restartcnt = 0;
357 1.72 joff up->reattach = 0;
358 1.83 drochner #if 0
359 1.70 augustss if (UHUB_IS_HIGH_SPEED(sc)) {
360 1.70 augustss up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
361 1.70 augustss up->tt->hub = hub;
362 1.70 augustss } else {
363 1.70 augustss up->tt = NULL;
364 1.70 augustss }
365 1.83 drochner #endif
366 1.42 augustss }
367 1.42 augustss
368 1.42 augustss /* XXX should check for none, individual, or ganged power? */
369 1.42 augustss
370 1.42 augustss pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
371 1.42 augustss + USB_EXTRA_POWER_UP_TIME;
372 1.42 augustss for (port = 1; port <= nports; port++) {
373 1.42 augustss /* Turn the power on. */
374 1.42 augustss err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
375 1.33 augustss if (err)
376 1.98 cube aprint_error_dev(self, "port %d power on failed, %s\n",
377 1.98 cube port, usbd_errstr(err));
378 1.128 skrll DPRINTF("uhub %d turn on port %d power", device_unit(self),
379 1.128 skrll port, 0, 0);
380 1.94 jmcneill }
381 1.94 jmcneill
382 1.94 jmcneill /* Wait for stable power if we are not a root hub */
383 1.94 jmcneill if (dev->powersrc->parent != NULL)
384 1.42 augustss usbd_delay_ms(dev, pwrdly);
385 1.42 augustss
386 1.42 augustss /* The usual exploration will finish the setup. */
387 1.42 augustss
388 1.1 augustss sc->sc_running = 1;
389 1.11 augustss
390 1.92 jmcneill if (!pmf_device_register(self, NULL, NULL))
391 1.92 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
392 1.92 jmcneill
393 1.107 dyoung return;
394 1.11 augustss
395 1.18 augustss bad:
396 1.84 drochner if (sc->sc_status)
397 1.84 drochner free(sc->sc_status, M_USBDEV);
398 1.101 drochner if (sc->sc_statusbuf)
399 1.101 drochner free(sc->sc_statusbuf, M_USBDEV);
400 1.70 augustss if (hub)
401 1.70 augustss free(hub, M_USBDEV);
402 1.70 augustss dev->hub = NULL;
403 1.107 dyoung return;
404 1.11 augustss }
405 1.11 augustss
406 1.1 augustss usbd_status
407 1.45 augustss uhub_explore(usbd_device_handle dev)
408 1.1 augustss {
409 1.1 augustss usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
410 1.11 augustss struct uhub_softc *sc = dev->hub->hubsoftc;
411 1.1 augustss struct usbd_port *up;
412 1.33 augustss usbd_status err;
413 1.56 augustss int speed;
414 1.1 augustss int port;
415 1.72 joff int change, status, reconnect;
416 1.1 augustss
417 1.127 skrll UHUBHIST_FUNC(); UHUBHIST_CALLED();
418 1.127 skrll
419 1.128 skrll DPRINTFN(10, "uhub %d dev=%p addr=%d", device_unit(sc->sc_dev), dev,
420 1.128 skrll dev->address, 0);
421 1.1 augustss
422 1.1 augustss if (!sc->sc_running)
423 1.1 augustss return (USBD_NOT_STARTED);
424 1.1 augustss
425 1.1 augustss /* Ignore hubs that are too deep. */
426 1.1 augustss if (dev->depth > USB_HUB_MAX_DEPTH)
427 1.1 augustss return (USBD_TOO_DEEP);
428 1.1 augustss
429 1.101 drochner if (PORTSTAT_ISSET(sc, 0)) { /* hub status change */
430 1.101 drochner usb_hub_status_t hs;
431 1.101 drochner
432 1.101 drochner err = usbd_get_hub_status(dev, &hs);
433 1.101 drochner if (err) {
434 1.128 skrll DPRINTF("get hub status failed, uhub %d err %d",
435 1.128 skrll device_unit(sc->sc_dev), err, 0, 0);
436 1.101 drochner } else {
437 1.101 drochner /* just acknowledge */
438 1.101 drochner status = UGETW(hs.wHubStatus);
439 1.101 drochner change = UGETW(hs.wHubChange);
440 1.101 drochner if (change & UHS_LOCAL_POWER)
441 1.101 drochner usbd_clear_hub_feature(dev,
442 1.101 drochner UHF_C_HUB_LOCAL_POWER);
443 1.101 drochner if (change & UHS_OVER_CURRENT)
444 1.101 drochner usbd_clear_hub_feature(dev,
445 1.101 drochner UHF_C_HUB_OVER_CURRENT);
446 1.101 drochner }
447 1.101 drochner }
448 1.101 drochner
449 1.84 drochner for (port = 1; port <= hd->bNbrPorts; port++) {
450 1.1 augustss up = &dev->hub->ports[port-1];
451 1.87 drochner
452 1.87 drochner /* reattach is needed after firmware upload */
453 1.87 drochner reconnect = up->reattach;
454 1.87 drochner up->reattach = 0;
455 1.87 drochner
456 1.87 drochner status = change = 0;
457 1.87 drochner
458 1.87 drochner /* don't check if no change summary notification */
459 1.87 drochner if (PORTSTAT_ISSET(sc, port) || reconnect) {
460 1.87 drochner err = usbd_get_port_status(dev, port, &up->status);
461 1.87 drochner if (err) {
462 1.128 skrll DPRINTF("get port stat failed, uhub %d err %d",
463 1.128 skrll device_unit(sc->sc_dev), err, 0, 0);
464 1.87 drochner continue;
465 1.87 drochner }
466 1.87 drochner status = UGETW(up->status.wPortStatus);
467 1.87 drochner change = UGETW(up->status.wPortChange);
468 1.101 drochner #if 0
469 1.101 drochner printf("%s port %d: s/c=%x/%x\n",
470 1.101 drochner device_xname(sc->sc_dev), port, status, change);
471 1.101 drochner #endif
472 1.87 drochner }
473 1.87 drochner if (!change && !reconnect) {
474 1.87 drochner /* No status change, just do recursive explore. */
475 1.87 drochner if (up->device != NULL && up->device->hub != NULL)
476 1.87 drochner up->device->hub->explore(up->device);
477 1.1 augustss continue;
478 1.1 augustss }
479 1.87 drochner
480 1.11 augustss if (change & UPS_C_PORT_ENABLED) {
481 1.127 skrll DPRINTF("C_PORT_ENABLED", 0, 0, 0, 0);
482 1.11 augustss usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
483 1.68 mycroft if (change & UPS_C_CONNECT_STATUS) {
484 1.68 mycroft /* Ignore the port error if the device
485 1.68 mycroft vanished. */
486 1.68 mycroft } else if (status & UPS_PORT_ENABLED) {
487 1.98 cube aprint_error_dev(sc->sc_dev,
488 1.98 cube "illegal enable change, port %d\n", port);
489 1.11 augustss } else {
490 1.11 augustss /* Port error condition. */
491 1.47 augustss if (up->restartcnt) /* no message first time */
492 1.98 cube aprint_error_dev(sc->sc_dev,
493 1.98 cube "port error, restarting port %d\n",
494 1.98 cube port);
495 1.47 augustss
496 1.47 augustss if (up->restartcnt++ < USBD_RESTART_MAX)
497 1.11 augustss goto disco;
498 1.47 augustss else
499 1.98 cube aprint_error_dev(sc->sc_dev,
500 1.98 cube "port error, giving up port %d\n",
501 1.98 cube port);
502 1.11 augustss }
503 1.11 augustss }
504 1.87 drochner
505 1.87 drochner /* XXX handle overcurrent and resume events! */
506 1.87 drochner
507 1.116 jakllsch if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
508 1.116 jakllsch /* No status change, just do recursive explore. */
509 1.116 jakllsch if (up->device != NULL && up->device->hub != NULL)
510 1.116 jakllsch up->device->hub->explore(up->device);
511 1.1 augustss continue;
512 1.116 jakllsch }
513 1.42 augustss
514 1.42 augustss /* We have a connect status change, handle it. */
515 1.42 augustss
516 1.127 skrll DPRINTF("status change hub=%d port=%d", dev->address, port, 0,
517 1.127 skrll 0);
518 1.1 augustss usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
519 1.1 augustss /*
520 1.1 augustss * If there is already a device on the port the change status
521 1.1 augustss * must mean that is has disconnected. Looking at the
522 1.1 augustss * current connect status is not enough to figure this out
523 1.1 augustss * since a new unit may have been connected before we handle
524 1.1 augustss * the disconnect.
525 1.1 augustss */
526 1.11 augustss disco:
527 1.33 augustss if (up->device != NULL) {
528 1.1 augustss /* Disconnected */
529 1.127 skrll DPRINTF("device addr=%d disappeared on port %d",
530 1.127 skrll up->device->address, port, 0, 0);
531 1.108 dyoung usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
532 1.58 augustss usbd_clear_port_feature(dev, port,
533 1.1 augustss UHF_C_PORT_CONNECTION);
534 1.1 augustss }
535 1.35 augustss if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
536 1.42 augustss /* Nothing connected, just ignore it. */
537 1.127 skrll DPRINTFN(3, "port=%d !CURRENT_CONNECT_STATUS", port, 0,
538 1.127 skrll 0, 0);
539 1.1 augustss continue;
540 1.35 augustss }
541 1.1 augustss
542 1.1 augustss /* Connected */
543 1.42 augustss
544 1.42 augustss if (!(status & UPS_PORT_POWER))
545 1.98 cube aprint_normal_dev(sc->sc_dev,
546 1.98 cube "strange, connected port %d has no power\n", port);
547 1.42 augustss
548 1.1 augustss /* Wait for maximum device power up time. */
549 1.13 augustss usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
550 1.11 augustss
551 1.1 augustss /* Reset port, which implies enabling it. */
552 1.35 augustss if (usbd_reset_port(dev, port, &up->status)) {
553 1.98 cube aprint_error_dev(sc->sc_dev,
554 1.98 cube "port %d reset failed\n", port);
555 1.54 augustss continue;
556 1.54 augustss }
557 1.54 augustss /* Get port status again, it might have changed during reset */
558 1.54 augustss err = usbd_get_port_status(dev, port, &up->status);
559 1.54 augustss if (err) {
560 1.127 skrll DPRINTF("get port status failed, error %d", err, 0, 0,
561 1.127 skrll 0);
562 1.54 augustss continue;
563 1.54 augustss }
564 1.54 augustss status = UGETW(up->status.wPortStatus);
565 1.54 augustss change = UGETW(up->status.wPortChange);
566 1.54 augustss if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
567 1.54 augustss /* Nothing connected, just ignore it. */
568 1.54 augustss #ifdef DIAGNOSTIC
569 1.98 cube aprint_debug_dev(sc->sc_dev,
570 1.98 cube "port %d, device disappeared after reset\n", port);
571 1.54 augustss #endif
572 1.1 augustss continue;
573 1.35 augustss }
574 1.110 kiyohara if (!(status & UPS_PORT_ENABLED)) {
575 1.110 kiyohara /* Not allowed send/receive packet. */
576 1.110 kiyohara #ifdef DIAGNOSTIC
577 1.115 sborrill printf("%s: port %d, device not enabled\n",
578 1.112 dyoung device_xname(sc->sc_dev), port);
579 1.110 kiyohara #endif
580 1.110 kiyohara continue;
581 1.110 kiyohara }
582 1.1 augustss
583 1.56 augustss /* Figure out device speed */
584 1.126 skrll #if 0
585 1.125 skrll if (status & UPS_SUPER_SPEED)
586 1.125 skrll speed = USB_SPEED_SUPER;
587 1.126 skrll else
588 1.126 skrll #endif
589 1.126 skrll if (status & UPS_HIGH_SPEED)
590 1.56 augustss speed = USB_SPEED_HIGH;
591 1.56 augustss else if (status & UPS_LOW_SPEED)
592 1.56 augustss speed = USB_SPEED_LOW;
593 1.56 augustss else
594 1.56 augustss speed = USB_SPEED_FULL;
595 1.1 augustss /* Get device info and set its address. */
596 1.105 dyoung err = usbd_new_device(sc->sc_dev, dev->bus,
597 1.56 augustss dev->depth + 1, speed, port, up);
598 1.1 augustss /* XXX retry a few times? */
599 1.33 augustss if (err) {
600 1.127 skrll DPRINTF("usbd_new_device failed, error %d", err, 0, 0,
601 1.127 skrll 0);
602 1.1 augustss /* Avoid addressing problems by disabling. */
603 1.1 augustss /* usbd_reset_port(dev, port, &up->status); */
604 1.30 augustss
605 1.58 augustss /*
606 1.30 augustss * The unit refused to accept a new address, or had
607 1.30 augustss * some other serious problem. Since we cannot leave
608 1.30 augustss * at 0 we have to disable the port instead.
609 1.30 augustss */
610 1.98 cube aprint_error_dev(sc->sc_dev,
611 1.98 cube "device problem, disabling port %d\n", port);
612 1.30 augustss usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
613 1.1 augustss } else {
614 1.47 augustss /* The port set up succeeded, reset error count. */
615 1.47 augustss up->restartcnt = 0;
616 1.47 augustss
617 1.34 augustss if (up->device->hub)
618 1.11 augustss up->device->hub->explore(up->device);
619 1.1 augustss }
620 1.1 augustss }
621 1.87 drochner /* enable status change notifications again */
622 1.101 drochner if (!sc->sc_isehciroothub)
623 1.101 drochner memset(sc->sc_status, 0, sc->sc_statuslen);
624 1.87 drochner sc->sc_explorepending = 0;
625 1.1 augustss return (USBD_NORMAL_COMPLETION);
626 1.1 augustss }
627 1.1 augustss
628 1.18 augustss /*
629 1.18 augustss * Called from process context when the hub is gone.
630 1.18 augustss * Detach all devices on active ports.
631 1.18 augustss */
632 1.105 dyoung int
633 1.105 dyoung uhub_detach(device_t self, int flags)
634 1.18 augustss {
635 1.107 dyoung struct uhub_softc *sc = device_private(self);
636 1.39 augustss struct usbd_hub *hub = sc->sc_hub->hub;
637 1.18 augustss struct usbd_port *rup;
638 1.108 dyoung int nports, port, rc;
639 1.18 augustss
640 1.127 skrll UHUBHIST_FUNC(); UHUBHIST_CALLED();
641 1.127 skrll
642 1.128 skrll DPRINTF("uhub %d flags=%d", device_unit(self), flags, 0, 0);
643 1.18 augustss
644 1.39 augustss if (hub == NULL) /* Must be partially working */
645 1.18 augustss return (0);
646 1.18 augustss
647 1.117 mrg /* XXXSMP usb */
648 1.117 mrg KERNEL_LOCK(1, curlwp);
649 1.117 mrg
650 1.39 augustss nports = hub->hubdesc.bNbrPorts;
651 1.32 augustss for(port = 0; port < nports; port++) {
652 1.39 augustss rup = &hub->ports[port];
653 1.108 dyoung if (rup->device == NULL)
654 1.108 dyoung continue;
655 1.117 mrg if ((rc = usb_disconnect_port(rup, self, flags)) != 0) {
656 1.117 mrg /* XXXSMP usb */
657 1.117 mrg KERNEL_UNLOCK_ONE(curlwp);
658 1.117 mrg
659 1.108 dyoung return rc;
660 1.117 mrg }
661 1.18 augustss }
662 1.58 augustss
663 1.108 dyoung pmf_device_deregister(self);
664 1.108 dyoung usbd_abort_pipe(sc->sc_ipipe);
665 1.108 dyoung usbd_close_pipe(sc->sc_ipipe);
666 1.108 dyoung
667 1.105 dyoung usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, sc->sc_dev);
668 1.37 augustss
669 1.83 drochner #if 0
670 1.70 augustss if (hub->ports[0].tt)
671 1.70 augustss free(hub->ports[0].tt, M_USBDEV);
672 1.83 drochner #endif
673 1.71 augustss free(hub, M_USBDEV);
674 1.39 augustss sc->sc_hub->hub = NULL;
675 1.84 drochner if (sc->sc_status)
676 1.84 drochner free(sc->sc_status, M_USBDEV);
677 1.101 drochner if (sc->sc_statusbuf)
678 1.101 drochner free(sc->sc_statusbuf, M_USBDEV);
679 1.18 augustss
680 1.117 mrg /* XXXSMP usb */
681 1.117 mrg KERNEL_UNLOCK_ONE(curlwp);
682 1.117 mrg
683 1.18 augustss return (0);
684 1.18 augustss }
685 1.34 augustss
686 1.103 kent int
687 1.103 kent uhub_rescan(device_t self, const char *ifattr, const int *locators)
688 1.103 kent {
689 1.103 kent struct uhub_softc *sc = device_private(self);
690 1.103 kent struct usbd_hub *hub = sc->sc_hub->hub;
691 1.103 kent usbd_device_handle dev;
692 1.124 martin int port;
693 1.103 kent
694 1.103 kent for (port = 0; port < hub->hubdesc.bNbrPorts; port++) {
695 1.103 kent dev = hub->ports[port].device;
696 1.103 kent if (dev == NULL)
697 1.103 kent continue;
698 1.124 martin usbd_reattach_device(sc->sc_dev, dev, port, locators);
699 1.103 kent }
700 1.103 kent return 0;
701 1.103 kent }
702 1.103 kent
703 1.34 augustss /* Called when a device has been detached from it */
704 1.95 dyoung void
705 1.95 dyoung uhub_childdet(device_t self, device_t child)
706 1.34 augustss {
707 1.95 dyoung struct uhub_softc *sc = device_private(self);
708 1.95 dyoung usbd_device_handle devhub = sc->sc_hub;
709 1.95 dyoung usbd_device_handle dev;
710 1.95 dyoung int nports;
711 1.95 dyoung int port;
712 1.95 dyoung int i;
713 1.95 dyoung
714 1.95 dyoung if (!devhub->hub)
715 1.95 dyoung /* should never happen; children are only created after init */
716 1.95 dyoung panic("hub not fully initialised, but child deleted?");
717 1.95 dyoung
718 1.95 dyoung nports = devhub->hub->hubdesc.bNbrPorts;
719 1.95 dyoung for (port = 0; port < nports; port++) {
720 1.95 dyoung dev = devhub->hub->ports[port].device;
721 1.118 gsutre if (!dev || dev->subdevlen == 0)
722 1.95 dyoung continue;
723 1.99 drochner for (i = 0; i < dev->subdevlen; i++) {
724 1.95 dyoung if (dev->subdevs[i] == child) {
725 1.95 dyoung dev->subdevs[i] = NULL;
726 1.102 drochner dev->nifaces_claimed--;
727 1.95 dyoung }
728 1.95 dyoung }
729 1.113 jmcneill if (dev->nifaces_claimed == 0) {
730 1.113 jmcneill free(dev->subdevs, M_USB);
731 1.113 jmcneill dev->subdevs = NULL;
732 1.113 jmcneill dev->subdevlen = 0;
733 1.113 jmcneill }
734 1.95 dyoung }
735 1.34 augustss }
736 1.34 augustss
737 1.18 augustss
738 1.18 augustss /*
739 1.18 augustss * Hub interrupt.
740 1.18 augustss * This an indication that some port has changed status.
741 1.18 augustss * Notify the bus event handler thread that we need
742 1.18 augustss * to be explored again.
743 1.18 augustss */
744 1.1 augustss void
745 1.105 dyoung uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
746 1.1 augustss {
747 1.1 augustss struct uhub_softc *sc = addr;
748 1.1 augustss
749 1.127 skrll UHUBHIST_FUNC(); UHUBHIST_CALLED();
750 1.127 skrll
751 1.128 skrll DPRINTFN(5, "uhub %d", device_unit(sc->sc_dev), 0, 0, 0);
752 1.87 drochner
753 1.50 augustss if (status == USBD_STALLED)
754 1.9 augustss usbd_clear_endpoint_stall_async(sc->sc_ipipe);
755 1.87 drochner else if (status == USBD_NORMAL_COMPLETION &&
756 1.87 drochner !sc->sc_explorepending) {
757 1.87 drochner /*
758 1.87 drochner * Make sure the status is not overwritten in between.
759 1.87 drochner * XXX we should suspend the pipe instead
760 1.87 drochner */
761 1.87 drochner memcpy(sc->sc_status, sc->sc_statusbuf, sc->sc_statuslen);
762 1.87 drochner sc->sc_explorepending = 1;
763 1.50 augustss usb_needs_explore(sc->sc_hub);
764 1.87 drochner }
765 1.89 drochner /*
766 1.89 drochner * XXX workaround for broken implementation of the interrupt
767 1.89 drochner * pipe in EHCI root hub emulation which doesn't resend
768 1.89 drochner * status change notifications until handled: force a rescan
769 1.89 drochner * of the ports we touched in the last run
770 1.89 drochner */
771 1.89 drochner if (status == USBD_NORMAL_COMPLETION && sc->sc_explorepending &&
772 1.101 drochner sc->sc_isehciroothub)
773 1.89 drochner usb_needs_explore(sc->sc_hub);
774 1.1 augustss }
775