uhub.c revision 1.130 1 1.130 skrll /* $NetBSD: uhub.c,v 1.130 2016/02/05 15:41:49 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.130 skrll __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.130 2016/02/05 15:41:49 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.129 skrll DPRINTFN(10, "uhub %d dev=%p addr=%d speed=%u",
420 1.129 skrll device_unit(sc->sc_dev), dev, dev->address, dev->speed);
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.129 skrll DPRINTF("uhub %d get hub status failed, 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.129 skrll DPRINTF("uhub %d s/c=%x/%x", device_unit(sc->sc_dev),
441 1.129 skrll status, change, 0);
442 1.129 skrll
443 1.101 drochner if (change & UHS_LOCAL_POWER)
444 1.101 drochner usbd_clear_hub_feature(dev,
445 1.101 drochner UHF_C_HUB_LOCAL_POWER);
446 1.101 drochner if (change & UHS_OVER_CURRENT)
447 1.101 drochner usbd_clear_hub_feature(dev,
448 1.101 drochner UHF_C_HUB_OVER_CURRENT);
449 1.101 drochner }
450 1.101 drochner }
451 1.101 drochner
452 1.84 drochner for (port = 1; port <= hd->bNbrPorts; port++) {
453 1.1 augustss up = &dev->hub->ports[port-1];
454 1.87 drochner
455 1.87 drochner /* reattach is needed after firmware upload */
456 1.87 drochner reconnect = up->reattach;
457 1.87 drochner up->reattach = 0;
458 1.87 drochner
459 1.87 drochner status = change = 0;
460 1.87 drochner
461 1.87 drochner /* don't check if no change summary notification */
462 1.87 drochner if (PORTSTAT_ISSET(sc, port) || reconnect) {
463 1.87 drochner err = usbd_get_port_status(dev, port, &up->status);
464 1.87 drochner if (err) {
465 1.129 skrll DPRINTF("uhub %d get port stat failed, err %d",
466 1.128 skrll device_unit(sc->sc_dev), err, 0, 0);
467 1.87 drochner continue;
468 1.87 drochner }
469 1.87 drochner status = UGETW(up->status.wPortStatus);
470 1.87 drochner change = UGETW(up->status.wPortChange);
471 1.129 skrll
472 1.129 skrll DPRINTF("uhub %d port %d: s/c=%x/%x",
473 1.129 skrll device_unit(sc->sc_dev), port, status, change);
474 1.87 drochner }
475 1.87 drochner if (!change && !reconnect) {
476 1.87 drochner /* No status change, just do recursive explore. */
477 1.87 drochner if (up->device != NULL && up->device->hub != NULL)
478 1.87 drochner up->device->hub->explore(up->device);
479 1.1 augustss continue;
480 1.1 augustss }
481 1.87 drochner
482 1.11 augustss if (change & UPS_C_PORT_ENABLED) {
483 1.129 skrll DPRINTF("uhub %d port %d C_PORT_ENABLED",
484 1.129 skrll device_unit(sc->sc_dev), port, 0, 0);
485 1.11 augustss usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
486 1.68 mycroft if (change & UPS_C_CONNECT_STATUS) {
487 1.68 mycroft /* Ignore the port error if the device
488 1.68 mycroft vanished. */
489 1.68 mycroft } else if (status & UPS_PORT_ENABLED) {
490 1.98 cube aprint_error_dev(sc->sc_dev,
491 1.98 cube "illegal enable change, port %d\n", port);
492 1.11 augustss } else {
493 1.11 augustss /* Port error condition. */
494 1.47 augustss if (up->restartcnt) /* no message first time */
495 1.98 cube aprint_error_dev(sc->sc_dev,
496 1.98 cube "port error, restarting port %d\n",
497 1.98 cube port);
498 1.47 augustss
499 1.47 augustss if (up->restartcnt++ < USBD_RESTART_MAX)
500 1.11 augustss goto disco;
501 1.47 augustss else
502 1.98 cube aprint_error_dev(sc->sc_dev,
503 1.98 cube "port error, giving up port %d\n",
504 1.98 cube port);
505 1.11 augustss }
506 1.11 augustss }
507 1.87 drochner
508 1.87 drochner /* XXX handle overcurrent and resume events! */
509 1.87 drochner
510 1.116 jakllsch if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
511 1.116 jakllsch /* No status change, just do recursive explore. */
512 1.116 jakllsch if (up->device != NULL && up->device->hub != NULL)
513 1.116 jakllsch up->device->hub->explore(up->device);
514 1.1 augustss continue;
515 1.116 jakllsch }
516 1.42 augustss
517 1.42 augustss /* We have a connect status change, handle it. */
518 1.42 augustss
519 1.127 skrll DPRINTF("status change hub=%d port=%d", dev->address, port, 0,
520 1.127 skrll 0);
521 1.1 augustss usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
522 1.1 augustss /*
523 1.1 augustss * If there is already a device on the port the change status
524 1.1 augustss * must mean that is has disconnected. Looking at the
525 1.1 augustss * current connect status is not enough to figure this out
526 1.1 augustss * since a new unit may have been connected before we handle
527 1.1 augustss * the disconnect.
528 1.1 augustss */
529 1.11 augustss disco:
530 1.33 augustss if (up->device != NULL) {
531 1.1 augustss /* Disconnected */
532 1.129 skrll DPRINTF("uhub %d device addr=%d disappeared on port %d",
533 1.129 skrll device_unit(sc->sc_dev), up->device->address, port,
534 1.129 skrll 0);
535 1.108 dyoung usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
536 1.58 augustss usbd_clear_port_feature(dev, port,
537 1.1 augustss UHF_C_PORT_CONNECTION);
538 1.1 augustss }
539 1.35 augustss if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
540 1.42 augustss /* Nothing connected, just ignore it. */
541 1.129 skrll DPRINTFN(3, "uhub %d port=%d !CURRENT_CONNECT_STATUS",
542 1.129 skrll device_unit(sc->sc_dev), port, 0, 0);
543 1.1 augustss continue;
544 1.35 augustss }
545 1.1 augustss
546 1.1 augustss /* Connected */
547 1.129 skrll DPRINTF("unit %d dev->speed=%u dev->depth=%u",
548 1.129 skrll device_unit(sc->sc_dev), dev->speed, dev->depth, 0);
549 1.42 augustss
550 1.42 augustss if (!(status & UPS_PORT_POWER))
551 1.98 cube aprint_normal_dev(sc->sc_dev,
552 1.98 cube "strange, connected port %d has no power\n", port);
553 1.42 augustss
554 1.1 augustss /* Wait for maximum device power up time. */
555 1.13 augustss usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
556 1.11 augustss
557 1.1 augustss /* Reset port, which implies enabling it. */
558 1.35 augustss if (usbd_reset_port(dev, port, &up->status)) {
559 1.98 cube aprint_error_dev(sc->sc_dev,
560 1.98 cube "port %d reset failed\n", port);
561 1.54 augustss continue;
562 1.54 augustss }
563 1.130 skrll #if 0
564 1.54 augustss /* Get port status again, it might have changed during reset */
565 1.54 augustss err = usbd_get_port_status(dev, port, &up->status);
566 1.54 augustss if (err) {
567 1.129 skrll DPRINTF("uhub %d port=%d get port status failed, "
568 1.129 skrll "err %d", device_unit(sc->sc_dev), port, err, 0);
569 1.54 augustss continue;
570 1.54 augustss }
571 1.130 skrll #endif
572 1.54 augustss status = UGETW(up->status.wPortStatus);
573 1.54 augustss change = UGETW(up->status.wPortChange);
574 1.54 augustss if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
575 1.54 augustss /* Nothing connected, just ignore it. */
576 1.54 augustss #ifdef DIAGNOSTIC
577 1.98 cube aprint_debug_dev(sc->sc_dev,
578 1.98 cube "port %d, device disappeared after reset\n", port);
579 1.54 augustss #endif
580 1.1 augustss continue;
581 1.35 augustss }
582 1.110 kiyohara if (!(status & UPS_PORT_ENABLED)) {
583 1.110 kiyohara /* Not allowed send/receive packet. */
584 1.110 kiyohara #ifdef DIAGNOSTIC
585 1.115 sborrill printf("%s: port %d, device not enabled\n",
586 1.112 dyoung device_xname(sc->sc_dev), port);
587 1.110 kiyohara #endif
588 1.110 kiyohara continue;
589 1.110 kiyohara }
590 1.1 augustss
591 1.56 augustss /* Figure out device speed */
592 1.126 skrll #if 0
593 1.125 skrll if (status & UPS_SUPER_SPEED)
594 1.125 skrll speed = USB_SPEED_SUPER;
595 1.126 skrll else
596 1.126 skrll #endif
597 1.126 skrll if (status & UPS_HIGH_SPEED)
598 1.56 augustss speed = USB_SPEED_HIGH;
599 1.56 augustss else if (status & UPS_LOW_SPEED)
600 1.56 augustss speed = USB_SPEED_LOW;
601 1.56 augustss else
602 1.56 augustss speed = USB_SPEED_FULL;
603 1.129 skrll
604 1.129 skrll DPRINTF("uhub %d speed %u", device_unit(sc->sc_dev), speed, 0,
605 1.129 skrll 0);
606 1.129 skrll
607 1.1 augustss /* Get device info and set its address. */
608 1.105 dyoung err = usbd_new_device(sc->sc_dev, dev->bus,
609 1.56 augustss dev->depth + 1, speed, port, up);
610 1.1 augustss /* XXX retry a few times? */
611 1.33 augustss if (err) {
612 1.127 skrll DPRINTF("usbd_new_device failed, error %d", err, 0, 0,
613 1.127 skrll 0);
614 1.1 augustss /* Avoid addressing problems by disabling. */
615 1.1 augustss /* usbd_reset_port(dev, port, &up->status); */
616 1.30 augustss
617 1.58 augustss /*
618 1.30 augustss * The unit refused to accept a new address, or had
619 1.30 augustss * some other serious problem. Since we cannot leave
620 1.30 augustss * at 0 we have to disable the port instead.
621 1.30 augustss */
622 1.98 cube aprint_error_dev(sc->sc_dev,
623 1.98 cube "device problem, disabling port %d\n", port);
624 1.30 augustss usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
625 1.1 augustss } else {
626 1.47 augustss /* The port set up succeeded, reset error count. */
627 1.47 augustss up->restartcnt = 0;
628 1.47 augustss
629 1.34 augustss if (up->device->hub)
630 1.11 augustss up->device->hub->explore(up->device);
631 1.1 augustss }
632 1.1 augustss }
633 1.87 drochner /* enable status change notifications again */
634 1.101 drochner if (!sc->sc_isehciroothub)
635 1.101 drochner memset(sc->sc_status, 0, sc->sc_statuslen);
636 1.87 drochner sc->sc_explorepending = 0;
637 1.1 augustss return (USBD_NORMAL_COMPLETION);
638 1.1 augustss }
639 1.1 augustss
640 1.18 augustss /*
641 1.18 augustss * Called from process context when the hub is gone.
642 1.18 augustss * Detach all devices on active ports.
643 1.18 augustss */
644 1.105 dyoung int
645 1.105 dyoung uhub_detach(device_t self, int flags)
646 1.18 augustss {
647 1.107 dyoung struct uhub_softc *sc = device_private(self);
648 1.39 augustss struct usbd_hub *hub = sc->sc_hub->hub;
649 1.18 augustss struct usbd_port *rup;
650 1.108 dyoung int nports, port, rc;
651 1.18 augustss
652 1.127 skrll UHUBHIST_FUNC(); UHUBHIST_CALLED();
653 1.127 skrll
654 1.128 skrll DPRINTF("uhub %d flags=%d", device_unit(self), flags, 0, 0);
655 1.18 augustss
656 1.39 augustss if (hub == NULL) /* Must be partially working */
657 1.18 augustss return (0);
658 1.18 augustss
659 1.117 mrg /* XXXSMP usb */
660 1.117 mrg KERNEL_LOCK(1, curlwp);
661 1.117 mrg
662 1.39 augustss nports = hub->hubdesc.bNbrPorts;
663 1.32 augustss for(port = 0; port < nports; port++) {
664 1.39 augustss rup = &hub->ports[port];
665 1.108 dyoung if (rup->device == NULL)
666 1.108 dyoung continue;
667 1.117 mrg if ((rc = usb_disconnect_port(rup, self, flags)) != 0) {
668 1.117 mrg /* XXXSMP usb */
669 1.117 mrg KERNEL_UNLOCK_ONE(curlwp);
670 1.117 mrg
671 1.108 dyoung return rc;
672 1.117 mrg }
673 1.18 augustss }
674 1.58 augustss
675 1.108 dyoung pmf_device_deregister(self);
676 1.108 dyoung usbd_abort_pipe(sc->sc_ipipe);
677 1.108 dyoung usbd_close_pipe(sc->sc_ipipe);
678 1.108 dyoung
679 1.105 dyoung usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, sc->sc_dev);
680 1.37 augustss
681 1.83 drochner #if 0
682 1.70 augustss if (hub->ports[0].tt)
683 1.70 augustss free(hub->ports[0].tt, M_USBDEV);
684 1.83 drochner #endif
685 1.71 augustss free(hub, M_USBDEV);
686 1.39 augustss sc->sc_hub->hub = NULL;
687 1.84 drochner if (sc->sc_status)
688 1.84 drochner free(sc->sc_status, M_USBDEV);
689 1.101 drochner if (sc->sc_statusbuf)
690 1.101 drochner free(sc->sc_statusbuf, M_USBDEV);
691 1.18 augustss
692 1.117 mrg /* XXXSMP usb */
693 1.117 mrg KERNEL_UNLOCK_ONE(curlwp);
694 1.117 mrg
695 1.18 augustss return (0);
696 1.18 augustss }
697 1.34 augustss
698 1.103 kent int
699 1.103 kent uhub_rescan(device_t self, const char *ifattr, const int *locators)
700 1.103 kent {
701 1.103 kent struct uhub_softc *sc = device_private(self);
702 1.103 kent struct usbd_hub *hub = sc->sc_hub->hub;
703 1.103 kent usbd_device_handle dev;
704 1.124 martin int port;
705 1.103 kent
706 1.103 kent for (port = 0; port < hub->hubdesc.bNbrPorts; port++) {
707 1.103 kent dev = hub->ports[port].device;
708 1.103 kent if (dev == NULL)
709 1.103 kent continue;
710 1.124 martin usbd_reattach_device(sc->sc_dev, dev, port, locators);
711 1.103 kent }
712 1.103 kent return 0;
713 1.103 kent }
714 1.103 kent
715 1.34 augustss /* Called when a device has been detached from it */
716 1.95 dyoung void
717 1.95 dyoung uhub_childdet(device_t self, device_t child)
718 1.34 augustss {
719 1.95 dyoung struct uhub_softc *sc = device_private(self);
720 1.95 dyoung usbd_device_handle devhub = sc->sc_hub;
721 1.95 dyoung usbd_device_handle dev;
722 1.95 dyoung int nports;
723 1.95 dyoung int port;
724 1.95 dyoung int i;
725 1.95 dyoung
726 1.95 dyoung if (!devhub->hub)
727 1.95 dyoung /* should never happen; children are only created after init */
728 1.95 dyoung panic("hub not fully initialised, but child deleted?");
729 1.95 dyoung
730 1.95 dyoung nports = devhub->hub->hubdesc.bNbrPorts;
731 1.95 dyoung for (port = 0; port < nports; port++) {
732 1.95 dyoung dev = devhub->hub->ports[port].device;
733 1.118 gsutre if (!dev || dev->subdevlen == 0)
734 1.95 dyoung continue;
735 1.99 drochner for (i = 0; i < dev->subdevlen; i++) {
736 1.95 dyoung if (dev->subdevs[i] == child) {
737 1.95 dyoung dev->subdevs[i] = NULL;
738 1.102 drochner dev->nifaces_claimed--;
739 1.95 dyoung }
740 1.95 dyoung }
741 1.113 jmcneill if (dev->nifaces_claimed == 0) {
742 1.113 jmcneill free(dev->subdevs, M_USB);
743 1.113 jmcneill dev->subdevs = NULL;
744 1.113 jmcneill dev->subdevlen = 0;
745 1.113 jmcneill }
746 1.95 dyoung }
747 1.34 augustss }
748 1.34 augustss
749 1.18 augustss
750 1.18 augustss /*
751 1.18 augustss * Hub interrupt.
752 1.18 augustss * This an indication that some port has changed status.
753 1.18 augustss * Notify the bus event handler thread that we need
754 1.18 augustss * to be explored again.
755 1.18 augustss */
756 1.1 augustss void
757 1.105 dyoung uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
758 1.1 augustss {
759 1.1 augustss struct uhub_softc *sc = addr;
760 1.1 augustss
761 1.127 skrll UHUBHIST_FUNC(); UHUBHIST_CALLED();
762 1.127 skrll
763 1.128 skrll DPRINTFN(5, "uhub %d", device_unit(sc->sc_dev), 0, 0, 0);
764 1.87 drochner
765 1.50 augustss if (status == USBD_STALLED)
766 1.9 augustss usbd_clear_endpoint_stall_async(sc->sc_ipipe);
767 1.87 drochner else if (status == USBD_NORMAL_COMPLETION &&
768 1.87 drochner !sc->sc_explorepending) {
769 1.87 drochner /*
770 1.87 drochner * Make sure the status is not overwritten in between.
771 1.87 drochner * XXX we should suspend the pipe instead
772 1.87 drochner */
773 1.87 drochner memcpy(sc->sc_status, sc->sc_statusbuf, sc->sc_statuslen);
774 1.87 drochner sc->sc_explorepending = 1;
775 1.50 augustss usb_needs_explore(sc->sc_hub);
776 1.87 drochner }
777 1.89 drochner /*
778 1.89 drochner * XXX workaround for broken implementation of the interrupt
779 1.89 drochner * pipe in EHCI root hub emulation which doesn't resend
780 1.89 drochner * status change notifications until handled: force a rescan
781 1.89 drochner * of the ports we touched in the last run
782 1.89 drochner */
783 1.89 drochner if (status == USBD_NORMAL_COMPLETION && sc->sc_explorepending &&
784 1.101 drochner sc->sc_isehciroothub)
785 1.89 drochner usb_needs_explore(sc->sc_hub);
786 1.1 augustss }
787