uhub.c revision 1.126.2.15 1 1.126.2.15 skrll /* $NetBSD: uhub.c,v 1.126.2.15 2015/09/29 11:38:29 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.126.2.15 skrll __KERNEL_RCSID(0, "$NetBSD: uhub.c,v 1.126.2.15 2015/09/29 11:38:29 skrll Exp $");
40 1.1 augustss
41 1.1 augustss #include <sys/param.h>
42 1.126.2.9 skrll
43 1.126.2.10 skrll #include <sys/bus.h>
44 1.126.2.9 skrll #include <sys/device.h>
45 1.1 augustss #include <sys/kernel.h>
46 1.126.2.3 skrll #include <sys/kmem.h>
47 1.34 augustss #include <sys/proc.h>
48 1.126.2.9 skrll #include <sys/sysctl.h>
49 1.126.2.10 skrll #include <sys/systm.h>
50 1.28 augustss
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.126.2.9 skrll #include <dev/usb/usbhist.h>
57 1.1 augustss
58 1.126.2.9 skrll #ifdef USB_DEBUG
59 1.126.2.9 skrll #ifndef UHUB_DEBUG
60 1.126.2.9 skrll #define uhubdebug 0
61 1.1 augustss #else
62 1.126.2.9 skrll static int uhubdebug = 0;
63 1.126.2.9 skrll
64 1.126.2.9 skrll SYSCTL_SETUP(sysctl_hw_uhub_setup, "sysctl hw.uhub setup")
65 1.126.2.9 skrll {
66 1.126.2.9 skrll int err;
67 1.126.2.9 skrll const struct sysctlnode *rnode;
68 1.126.2.9 skrll const struct sysctlnode *cnode;
69 1.126.2.9 skrll
70 1.126.2.9 skrll err = sysctl_createv(clog, 0, NULL, &rnode,
71 1.126.2.9 skrll CTLFLAG_PERMANENT, CTLTYPE_NODE, "uhub",
72 1.126.2.9 skrll SYSCTL_DESCR("uhub global controls"),
73 1.126.2.9 skrll NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
74 1.126.2.9 skrll
75 1.126.2.9 skrll if (err)
76 1.126.2.9 skrll goto fail;
77 1.126.2.9 skrll
78 1.126.2.9 skrll /* control debugging printfs */
79 1.126.2.9 skrll err = sysctl_createv(clog, 0, &rnode, &cnode,
80 1.126.2.9 skrll CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
81 1.126.2.9 skrll "debug", SYSCTL_DESCR("Enable debugging output"),
82 1.126.2.9 skrll NULL, 0, &uhubdebug, sizeof(uhubdebug), CTL_CREATE, CTL_EOL);
83 1.126.2.9 skrll if (err)
84 1.126.2.9 skrll goto fail;
85 1.126.2.9 skrll
86 1.126.2.9 skrll return;
87 1.126.2.9 skrll fail:
88 1.126.2.9 skrll aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
89 1.126.2.9 skrll }
90 1.126.2.9 skrll
91 1.126.2.9 skrll #endif /* UHUB_DEBUG */
92 1.126.2.9 skrll #endif /* USB_DEBUG */
93 1.126.2.9 skrll
94 1.126.2.9 skrll #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(uhubdebug,1,FMT,A,B,C,D)
95 1.126.2.9 skrll #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(uhubdebug,N,FMT,A,B,C,D)
96 1.126.2.9 skrll #define UHUBHIST_FUNC() USBHIST_FUNC()
97 1.126.2.9 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.126.2.7 skrll struct usbd_device * sc_hub; /* USB device */
102 1.86 drochner int sc_proto; /* device protocol */
103 1.126.2.7 skrll struct usbd_pipe * 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.126.2.1 skrll uint8_t *sc_statusbuf;
107 1.126.2.12 skrll uint8_t *sc_statuspend;
108 1.126.2.1 skrll uint8_t *sc_status;
109 1.87 drochner size_t sc_statuslen;
110 1.87 drochner int sc_explorepending;
111 1.101 drochner int sc_isehciroothub; /* see comment in uhub_intr() */
112 1.87 drochner
113 1.11 augustss u_char sc_running;
114 1.1 augustss };
115 1.87 drochner
116 1.126.2.12 skrll #define UHUB_IS_HIGH_SPEED(sc) \
117 1.126.2.12 skrll ((sc)->sc_proto == UDPROTO_HSHUBSTT || (sc)->sc_proto == UDPROTO_HSHUBMTT)
118 1.86 drochner #define UHUB_IS_SINGLE_TT(sc) ((sc)->sc_proto == UDPROTO_HSHUBSTT)
119 1.1 augustss
120 1.87 drochner #define PORTSTAT_ISSET(sc, port) \
121 1.87 drochner ((sc)->sc_status[(port) / 8] & (1 << ((port) % 8)))
122 1.87 drochner
123 1.126.2.7 skrll Static usbd_status uhub_explore(struct usbd_device *);
124 1.126.2.7 skrll Static void uhub_intr(struct usbd_xfer *, void *, usbd_status);
125 1.1 augustss
126 1.32 augustss
127 1.58 augustss /*
128 1.32 augustss * We need two attachment points:
129 1.32 augustss * hub to usb and hub to hub
130 1.32 augustss * Every other driver only connects to hubs
131 1.32 augustss */
132 1.1 augustss
133 1.98 cube int uhub_match(device_t, cfdata_t, void *);
134 1.95 dyoung void uhub_attach(device_t, device_t, void *);
135 1.103 kent int uhub_rescan(device_t, const char *, const int *);
136 1.95 dyoung void uhub_childdet(device_t, device_t);
137 1.95 dyoung int uhub_detach(device_t, int);
138 1.95 dyoung extern struct cfdriver uhub_cd;
139 1.104 dyoung CFATTACH_DECL3_NEW(uhub, sizeof(struct uhub_softc), uhub_match,
140 1.108 dyoung uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet,
141 1.104 dyoung DVF_DETACH_SHUTDOWN);
142 1.99 drochner CFATTACH_DECL2_NEW(uroothub, sizeof(struct uhub_softc), uhub_match,
143 1.108 dyoung uhub_attach, uhub_detach, NULL, uhub_rescan, uhub_childdet);
144 1.1 augustss
145 1.109 pooka /*
146 1.109 pooka * Setting this to 1 makes sure than an uhub attaches even at higher
147 1.109 pooka * priority than ugen when ugen_override is set to 1. This allows to
148 1.109 pooka * probe the whole USB bus and attach functions with ugen.
149 1.109 pooka */
150 1.109 pooka int uhub_ubermatch = 0;
151 1.109 pooka
152 1.126.2.12 skrll static usbd_status
153 1.126.2.12 skrll usbd_get_hub_desc(struct usbd_device *dev, usb_hub_descriptor_t *hd, int speed)
154 1.126.2.12 skrll {
155 1.126.2.12 skrll usb_device_request_t req;
156 1.126.2.12 skrll usbd_status err;
157 1.126.2.12 skrll int nports;
158 1.126.2.12 skrll
159 1.126.2.12 skrll UHUBHIST_FUNC(); UHUBHIST_CALLED();
160 1.126.2.12 skrll
161 1.126.2.12 skrll /* don't issue UDESC_HUB to SS hub, or it would stall */
162 1.126.2.12 skrll if (dev->ud_depth != 0 && USB_IS_SS(dev->ud_speed)) {
163 1.126.2.12 skrll usb_hub_ss_descriptor_t hssd;
164 1.126.2.12 skrll int rmvlen;
165 1.126.2.12 skrll
166 1.126.2.12 skrll memset(&hssd, 0, sizeof(hssd));
167 1.126.2.12 skrll req.bmRequestType = UT_READ_CLASS_DEVICE;
168 1.126.2.12 skrll req.bRequest = UR_GET_DESCRIPTOR;
169 1.126.2.12 skrll USETW2(req.wValue, UDESC_SS_HUB, 0);
170 1.126.2.12 skrll USETW(req.wIndex, 0);
171 1.126.2.12 skrll USETW(req.wLength, USB_HUB_SS_DESCRIPTOR_SIZE);
172 1.126.2.12 skrll DPRINTFN(1, "getting sshub descriptor", 0, 0, 0, 0);
173 1.126.2.12 skrll err = usbd_do_request(dev, &req, &hssd);
174 1.126.2.12 skrll nports = hssd.bNbrPorts;
175 1.126.2.12 skrll if (dev->ud_depth != 0 && nports > UHD_SS_NPORTS_MAX) {
176 1.126.2.12 skrll DPRINTF("num of ports %d exceeds maxports %d",
177 1.126.2.12 skrll nports, UHD_SS_NPORTS_MAX, 0, 0);
178 1.126.2.12 skrll nports = hd->bNbrPorts = UHD_SS_NPORTS_MAX;
179 1.126.2.12 skrll }
180 1.126.2.12 skrll rmvlen = (nports + 7) / 8;
181 1.126.2.12 skrll hd->bDescLength = USB_HUB_DESCRIPTOR_SIZE +
182 1.126.2.12 skrll (rmvlen > 1 ? rmvlen : 1) - 1;
183 1.126.2.12 skrll memcpy(hd->DeviceRemovable, hssd.DeviceRemovable, rmvlen);
184 1.126.2.12 skrll hd->bDescriptorType = hssd.bDescriptorType;
185 1.126.2.12 skrll hd->bNbrPorts = hssd.bNbrPorts;
186 1.126.2.12 skrll hd->wHubCharacteristics[0] = hssd.wHubCharacteristics[0];
187 1.126.2.12 skrll hd->wHubCharacteristics[1] = hssd.wHubCharacteristics[1];
188 1.126.2.12 skrll hd->bPwrOn2PwrGood = hssd.bPwrOn2PwrGood;
189 1.126.2.12 skrll hd->bHubContrCurrent = hssd.bHubContrCurrent;
190 1.126.2.12 skrll } else {
191 1.126.2.12 skrll req.bmRequestType = UT_READ_CLASS_DEVICE;
192 1.126.2.12 skrll req.bRequest = UR_GET_DESCRIPTOR;
193 1.126.2.12 skrll USETW2(req.wValue, UDESC_HUB, 0);
194 1.126.2.12 skrll USETW(req.wIndex, 0);
195 1.126.2.12 skrll USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
196 1.126.2.12 skrll DPRINTFN(1, "getting hub descriptor", 0, 0, 0, 0);
197 1.126.2.12 skrll err = usbd_do_request(dev, &req, hd);
198 1.126.2.12 skrll nports = hd->bNbrPorts;
199 1.126.2.12 skrll if (!err && nports > 7) {
200 1.126.2.12 skrll USETW(req.wLength,
201 1.126.2.12 skrll USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
202 1.126.2.12 skrll err = usbd_do_request(dev, &req, hd);
203 1.126.2.12 skrll }
204 1.126.2.12 skrll }
205 1.126.2.12 skrll
206 1.126.2.12 skrll return err;
207 1.126.2.12 skrll }
208 1.126.2.12 skrll
209 1.126.2.12 skrll static usbd_status
210 1.126.2.12 skrll usbd_set_hub_depth(struct usbd_device *dev, int depth)
211 1.126.2.12 skrll {
212 1.126.2.12 skrll usb_device_request_t req;
213 1.126.2.12 skrll
214 1.126.2.12 skrll req.bmRequestType = UT_WRITE_CLASS_DEVICE;
215 1.126.2.12 skrll req.bRequest = UR_SET_HUB_DEPTH;
216 1.126.2.12 skrll USETW(req.wValue, depth);
217 1.126.2.12 skrll USETW(req.wIndex, 0);
218 1.126.2.12 skrll USETW(req.wLength, 0);
219 1.126.2.12 skrll return usbd_do_request(dev, &req, 0);
220 1.126.2.12 skrll }
221 1.126.2.12 skrll
222 1.105 dyoung int
223 1.105 dyoung uhub_match(device_t parent, cfdata_t match, void *aux)
224 1.1 augustss {
225 1.107 dyoung struct usb_attach_arg *uaa = aux;
226 1.109 pooka int matchvalue;
227 1.109 pooka
228 1.126.2.9 skrll UHUBHIST_FUNC(); UHUBHIST_CALLED();
229 1.126.2.9 skrll
230 1.109 pooka if (uhub_ubermatch)
231 1.109 pooka matchvalue = UMATCH_HIGHEST+1;
232 1.109 pooka else
233 1.109 pooka matchvalue = UMATCH_DEVCLASS_DEVSUBCLASS;
234 1.58 augustss
235 1.126.2.9 skrll DPRINTFN(5, "uaa=%p", uaa, 0, 0, 0);
236 1.58 augustss /*
237 1.1 augustss * The subclass for hubs seems to be 0 for some and 1 for others,
238 1.1 augustss * so we just ignore the subclass.
239 1.1 augustss */
240 1.126.2.8 skrll if (uaa->uaa_class == UDCLASS_HUB)
241 1.126.2.4 skrll return matchvalue;
242 1.126.2.4 skrll return UMATCH_NONE;
243 1.1 augustss }
244 1.1 augustss
245 1.105 dyoung void
246 1.105 dyoung uhub_attach(device_t parent, device_t self, void *aux)
247 1.1 augustss {
248 1.107 dyoung struct uhub_softc *sc = device_private(self);
249 1.107 dyoung struct usb_attach_arg *uaa = aux;
250 1.126.2.8 skrll struct usbd_device *dev = uaa->uaa_device;
251 1.76 augustss char *devinfop;
252 1.33 augustss usbd_status err;
253 1.70 augustss struct usbd_hub *hub = NULL;
254 1.1 augustss usb_hub_descriptor_t hubdesc;
255 1.42 augustss int p, port, nports, nremov, pwrdly;
256 1.126.2.7 skrll struct usbd_interface *iface;
257 1.1 augustss usb_endpoint_descriptor_t *ed;
258 1.83 drochner #if 0 /* notyet */
259 1.70 augustss struct usbd_tt *tts = NULL;
260 1.83 drochner #endif
261 1.58 augustss
262 1.126.2.9 skrll UHUBHIST_FUNC(); UHUBHIST_CALLED();
263 1.126.2.9 skrll
264 1.98 cube sc->sc_dev = self;
265 1.1 augustss sc->sc_hub = dev;
266 1.126.2.8 skrll sc->sc_proto = uaa->uaa_proto;
267 1.76 augustss
268 1.76 augustss devinfop = usbd_devinfo_alloc(dev, 1);
269 1.96 jmcneill aprint_naive("\n");
270 1.96 jmcneill aprint_normal(": %s\n", devinfop);
271 1.76 augustss usbd_devinfo_free(devinfop);
272 1.1 augustss
273 1.126.2.2 skrll if (dev->ud_depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
274 1.98 cube aprint_normal_dev(self, "%s transaction translator%s\n",
275 1.70 augustss UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
276 1.70 augustss UHUB_IS_SINGLE_TT(sc) ? "" : "s");
277 1.69 augustss }
278 1.69 augustss
279 1.33 augustss err = usbd_set_config_index(dev, 0, 1);
280 1.33 augustss if (err) {
281 1.126.2.9 skrll DPRINTF("configuration failed, sc %p error %d", sc, err, 0, 0);
282 1.107 dyoung return;
283 1.1 augustss }
284 1.1 augustss
285 1.126.2.2 skrll if (dev->ud_depth > USB_HUB_MAX_DEPTH) {
286 1.98 cube aprint_error_dev(self,
287 1.98 cube "hub depth (%d) exceeded, hub ignored\n",
288 1.98 cube USB_HUB_MAX_DEPTH);
289 1.107 dyoung return;
290 1.1 augustss }
291 1.1 augustss
292 1.1 augustss /* Get hub descriptor. */
293 1.126.2.12 skrll memset(&hubdesc, 0, sizeof(hubdesc));
294 1.126.2.12 skrll err = usbd_get_hub_desc(dev, &hubdesc, dev->ud_speed);
295 1.11 augustss nports = hubdesc.bNbrPorts;
296 1.33 augustss if (err) {
297 1.126.2.9 skrll DPRINTF("getting hub descriptor failed, uhub %d error %d",
298 1.126.2.9 skrll device_unit(self), err, 0, 0);
299 1.107 dyoung return;
300 1.1 augustss }
301 1.1 augustss
302 1.11 augustss for (nremov = 0, port = 1; port <= nports; port++)
303 1.11 augustss if (!UHD_NOT_REMOV(&hubdesc, port))
304 1.11 augustss nremov++;
305 1.98 cube aprint_verbose_dev(self, "%d port%s with %d removable, %s powered\n",
306 1.98 cube nports, nports != 1 ? "s" : "", nremov,
307 1.126.2.2 skrll dev->ud_selfpowered ? "self" : "bus");
308 1.11 augustss
309 1.70 augustss if (nports == 0) {
310 1.98 cube aprint_debug_dev(self, "no ports, hub ignored\n");
311 1.70 augustss goto bad;
312 1.70 augustss }
313 1.70 augustss
314 1.126.2.3 skrll hub = kmem_alloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
315 1.126.2.3 skrll KM_SLEEP);
316 1.33 augustss if (hub == NULL)
317 1.107 dyoung return;
318 1.126.2.2 skrll dev->ud_hub = hub;
319 1.126.2.2 skrll dev->ud_hub->uh_hubsoftc = sc;
320 1.126.2.2 skrll hub->uh_explore = uhub_explore;
321 1.126.2.2 skrll hub->uh_hubdesc = hubdesc;
322 1.58 augustss
323 1.126.2.12 skrll if (USB_IS_SS(dev->ud_speed) && dev->ud_depth != 0) {
324 1.126.2.12 skrll aprint_debug_dev(self, "setting hub depth %u\n",
325 1.126.2.12 skrll dev->ud_depth-1);
326 1.126.2.12 skrll err = usbd_set_hub_depth(dev, dev->ud_depth - 1);
327 1.126.2.12 skrll if (err) {
328 1.126.2.12 skrll aprint_error_dev(self, "can't set depth\n");
329 1.126.2.12 skrll goto bad;
330 1.126.2.12 skrll }
331 1.126.2.12 skrll }
332 1.126.2.12 skrll
333 1.1 augustss /* Set up interrupt pipe. */
334 1.33 augustss err = usbd_device2interface_handle(dev, 0, &iface);
335 1.33 augustss if (err) {
336 1.98 cube aprint_error_dev(self, "no interface handle\n");
337 1.18 augustss goto bad;
338 1.1 augustss }
339 1.83 drochner
340 1.83 drochner if (UHUB_IS_HIGH_SPEED(sc) && !UHUB_IS_SINGLE_TT(sc)) {
341 1.83 drochner err = usbd_set_interface(iface, 1);
342 1.83 drochner if (err)
343 1.98 cube aprint_error_dev(self, "can't enable multiple TTs\n");
344 1.83 drochner }
345 1.83 drochner
346 1.1 augustss ed = usbd_interface2endpoint_descriptor(iface, 0);
347 1.33 augustss if (ed == NULL) {
348 1.98 cube aprint_error_dev(self, "no endpoint descriptor\n");
349 1.18 augustss goto bad;
350 1.1 augustss }
351 1.1 augustss if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
352 1.98 cube aprint_error_dev(self, "bad interrupt endpoint\n");
353 1.18 augustss goto bad;
354 1.1 augustss }
355 1.1 augustss
356 1.87 drochner sc->sc_statuslen = (nports + 1 + 7) / 8;
357 1.126.2.3 skrll sc->sc_statusbuf = kmem_alloc(sc->sc_statuslen, KM_SLEEP);
358 1.87 drochner if (!sc->sc_statusbuf)
359 1.87 drochner goto bad;
360 1.126.2.12 skrll sc->sc_statuspend = kmem_zalloc(sc->sc_statuslen, KM_SLEEP);
361 1.126.2.12 skrll if (!sc->sc_statuspend)
362 1.126.2.12 skrll goto bad;
363 1.126.2.3 skrll sc->sc_status = kmem_alloc(sc->sc_statuslen, KM_SLEEP);
364 1.84 drochner if (!sc->sc_status)
365 1.84 drochner goto bad;
366 1.101 drochner if (device_is_a(device_parent(device_parent(sc->sc_dev)), "ehci"))
367 1.101 drochner sc->sc_isehciroothub = 1;
368 1.84 drochner
369 1.87 drochner /* force initial scan */
370 1.87 drochner memset(sc->sc_status, 0xff, sc->sc_statuslen);
371 1.87 drochner sc->sc_explorepending = 1;
372 1.87 drochner
373 1.33 augustss err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
374 1.122 jmcneill USBD_SHORT_XFER_OK|USBD_MPSAFE, &sc->sc_ipipe, sc,
375 1.122 jmcneill sc->sc_statusbuf, sc->sc_statuslen,
376 1.122 jmcneill uhub_intr, USBD_DEFAULT_INTERVAL);
377 1.33 augustss if (err) {
378 1.98 cube aprint_error_dev(self, "cannot open interrupt pipe\n");
379 1.18 augustss goto bad;
380 1.1 augustss }
381 1.1 augustss
382 1.11 augustss /* Wait with power off for a while. */
383 1.13 augustss usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
384 1.11 augustss
385 1.105 dyoung usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev);
386 1.37 augustss
387 1.42 augustss /*
388 1.42 augustss * To have the best chance of success we do things in the exact same
389 1.123 jakllsch * order as Windows 98. This should not be necessary, but some
390 1.42 augustss * devices do not follow the USB specs to the letter.
391 1.42 augustss *
392 1.42 augustss * These are the events on the bus when a hub is attached:
393 1.42 augustss * Get device and config descriptors (see attach code)
394 1.42 augustss * Get hub descriptor (see above)
395 1.42 augustss * For all ports
396 1.42 augustss * turn on power
397 1.42 augustss * wait for power to become stable
398 1.42 augustss * (all below happens in explore code)
399 1.42 augustss * For all ports
400 1.42 augustss * clear C_PORT_CONNECTION
401 1.42 augustss * For all ports
402 1.42 augustss * get port status
403 1.42 augustss * if device connected
404 1.57 augustss * wait 100 ms
405 1.42 augustss * turn on reset
406 1.42 augustss * wait
407 1.42 augustss * clear C_PORT_RESET
408 1.42 augustss * get port status
409 1.42 augustss * proceed with device attachment
410 1.42 augustss */
411 1.42 augustss
412 1.83 drochner #if 0
413 1.78 christos if (UHUB_IS_HIGH_SPEED(sc) && nports > 0) {
414 1.126.2.3 skrll tts = kmem_alloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
415 1.126.2.15 skrll sizeof(struct usbd_tt), KM_SLEEP);
416 1.70 augustss if (!tts)
417 1.70 augustss goto bad;
418 1.70 augustss }
419 1.83 drochner #endif
420 1.42 augustss /* Set up data structures */
421 1.11 augustss for (p = 0; p < nports; p++) {
422 1.126.2.2 skrll struct usbd_port *up = &hub->uh_ports[p];
423 1.126.2.2 skrll up->up_dev = NULL;
424 1.126.2.2 skrll up->up_parent = dev;
425 1.126.2.2 skrll up->up_portno = p+1;
426 1.126.2.2 skrll if (dev->ud_selfpowered)
427 1.42 augustss /* Self powered hub, give ports maximum current. */
428 1.126.2.2 skrll up->up_power = USB_MAX_POWER;
429 1.42 augustss else
430 1.126.2.2 skrll up->up_power = USB_MIN_POWER;
431 1.126.2.2 skrll up->up_restartcnt = 0;
432 1.126.2.2 skrll up->up_reattach = 0;
433 1.83 drochner #if 0
434 1.70 augustss if (UHUB_IS_HIGH_SPEED(sc)) {
435 1.70 augustss up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
436 1.70 augustss up->tt->hub = hub;
437 1.70 augustss } else {
438 1.70 augustss up->tt = NULL;
439 1.70 augustss }
440 1.83 drochner #endif
441 1.42 augustss }
442 1.42 augustss
443 1.42 augustss /* XXX should check for none, individual, or ganged power? */
444 1.42 augustss
445 1.126.2.2 skrll pwrdly = dev->ud_hub->uh_hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
446 1.42 augustss + USB_EXTRA_POWER_UP_TIME;
447 1.42 augustss for (port = 1; port <= nports; port++) {
448 1.42 augustss /* Turn the power on. */
449 1.42 augustss err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
450 1.33 augustss if (err)
451 1.98 cube aprint_error_dev(self, "port %d power on failed, %s\n",
452 1.98 cube port, usbd_errstr(err));
453 1.126.2.9 skrll DPRINTF("uhub %d turn on port %d power", device_unit(self),
454 1.126.2.9 skrll port, 0, 0);
455 1.94 jmcneill }
456 1.94 jmcneill
457 1.94 jmcneill /* Wait for stable power if we are not a root hub */
458 1.126.2.2 skrll if (dev->ud_powersrc->up_parent != NULL)
459 1.42 augustss usbd_delay_ms(dev, pwrdly);
460 1.42 augustss
461 1.42 augustss /* The usual exploration will finish the setup. */
462 1.42 augustss
463 1.1 augustss sc->sc_running = 1;
464 1.11 augustss
465 1.92 jmcneill if (!pmf_device_register(self, NULL, NULL))
466 1.92 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
467 1.92 jmcneill
468 1.107 dyoung return;
469 1.11 augustss
470 1.18 augustss bad:
471 1.84 drochner if (sc->sc_status)
472 1.126.2.6 skrll kmem_free(sc->sc_status, sc->sc_statuslen);
473 1.126.2.12 skrll if (sc->sc_statuspend)
474 1.126.2.12 skrll kmem_free(sc->sc_statuspend, sc->sc_statuslen);
475 1.101 drochner if (sc->sc_statusbuf)
476 1.126.2.3 skrll kmem_free(sc->sc_statusbuf, sc->sc_statuslen);
477 1.70 augustss if (hub)
478 1.126.2.3 skrll kmem_free(hub,
479 1.126.2.3 skrll sizeof(*hub) + (nports-1) * sizeof(struct usbd_port));
480 1.126.2.2 skrll dev->ud_hub = NULL;
481 1.107 dyoung return;
482 1.11 augustss }
483 1.11 augustss
484 1.1 augustss usbd_status
485 1.126.2.7 skrll uhub_explore(struct usbd_device *dev)
486 1.1 augustss {
487 1.126.2.2 skrll usb_hub_descriptor_t *hd = &dev->ud_hub->uh_hubdesc;
488 1.126.2.2 skrll struct uhub_softc *sc = dev->ud_hub->uh_hubsoftc;
489 1.1 augustss struct usbd_port *up;
490 1.33 augustss usbd_status err;
491 1.56 augustss int speed;
492 1.1 augustss int port;
493 1.72 joff int change, status, reconnect;
494 1.1 augustss
495 1.126.2.9 skrll UHUBHIST_FUNC(); UHUBHIST_CALLED();
496 1.126.2.9 skrll
497 1.126.2.9 skrll DPRINTFN(10, "uhub %d dev=%p addr=%d speed=%u",
498 1.126.2.9 skrll device_unit(sc->sc_dev), dev, dev->ud_addr, dev->ud_speed);
499 1.1 augustss
500 1.1 augustss if (!sc->sc_running)
501 1.126.2.4 skrll return USBD_NOT_STARTED;
502 1.1 augustss
503 1.1 augustss /* Ignore hubs that are too deep. */
504 1.126.2.2 skrll if (dev->ud_depth > USB_HUB_MAX_DEPTH)
505 1.126.2.4 skrll return USBD_TOO_DEEP;
506 1.1 augustss
507 1.101 drochner if (PORTSTAT_ISSET(sc, 0)) { /* hub status change */
508 1.101 drochner usb_hub_status_t hs;
509 1.101 drochner
510 1.101 drochner err = usbd_get_hub_status(dev, &hs);
511 1.101 drochner if (err) {
512 1.126.2.9 skrll DPRINTF("uhub %d get hub status failed, err %d",
513 1.126.2.9 skrll device_unit(sc->sc_dev), err, 0, 0);
514 1.101 drochner } else {
515 1.101 drochner /* just acknowledge */
516 1.101 drochner status = UGETW(hs.wHubStatus);
517 1.101 drochner change = UGETW(hs.wHubChange);
518 1.126.2.9 skrll DPRINTF("uhub %d s/c=%x/%x", device_unit(sc->sc_dev),
519 1.126.2.9 skrll status, change, 0);
520 1.126.2.9 skrll
521 1.101 drochner if (change & UHS_LOCAL_POWER)
522 1.101 drochner usbd_clear_hub_feature(dev,
523 1.101 drochner UHF_C_HUB_LOCAL_POWER);
524 1.101 drochner if (change & UHS_OVER_CURRENT)
525 1.101 drochner usbd_clear_hub_feature(dev,
526 1.101 drochner UHF_C_HUB_OVER_CURRENT);
527 1.101 drochner }
528 1.101 drochner }
529 1.101 drochner
530 1.84 drochner for (port = 1; port <= hd->bNbrPorts; port++) {
531 1.126.2.2 skrll up = &dev->ud_hub->uh_ports[port-1];
532 1.87 drochner
533 1.87 drochner /* reattach is needed after firmware upload */
534 1.126.2.2 skrll reconnect = up->up_reattach;
535 1.126.2.2 skrll up->up_reattach = 0;
536 1.87 drochner
537 1.87 drochner status = change = 0;
538 1.87 drochner
539 1.87 drochner /* don't check if no change summary notification */
540 1.87 drochner if (PORTSTAT_ISSET(sc, port) || reconnect) {
541 1.126.2.2 skrll err = usbd_get_port_status(dev, port, &up->up_status);
542 1.87 drochner if (err) {
543 1.126.2.9 skrll DPRINTF("uhub %d get port stat failed, err %d",
544 1.126.2.9 skrll device_unit(sc->sc_dev), err, 0, 0);
545 1.87 drochner continue;
546 1.87 drochner }
547 1.126.2.2 skrll status = UGETW(up->up_status.wPortStatus);
548 1.126.2.2 skrll change = UGETW(up->up_status.wPortChange);
549 1.126.2.12 skrll if (USB_IS_SS(dev->ud_speed)) {
550 1.126.2.12 skrll status |= UPS_OTHER_SPEED;
551 1.126.2.12 skrll USETW(up->up_status.wPortStatus, status);
552 1.126.2.12 skrll }
553 1.126.2.9 skrll
554 1.126.2.9 skrll DPRINTF("uhub %d port %d: s/c=%x/%x",
555 1.126.2.9 skrll device_unit(sc->sc_dev), port, status, change);
556 1.87 drochner }
557 1.87 drochner if (!change && !reconnect) {
558 1.87 drochner /* No status change, just do recursive explore. */
559 1.126.2.2 skrll if (up->up_dev != NULL && up->up_dev->ud_hub != NULL)
560 1.126.2.2 skrll up->up_dev->ud_hub->uh_explore(up->up_dev);
561 1.1 augustss continue;
562 1.1 augustss }
563 1.87 drochner
564 1.11 augustss if (change & UPS_C_PORT_ENABLED) {
565 1.126.2.9 skrll DPRINTF("uhub %d port %d C_PORT_ENABLED",
566 1.126.2.9 skrll device_unit(sc->sc_dev), port, 0, 0);
567 1.11 augustss usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
568 1.68 mycroft if (change & UPS_C_CONNECT_STATUS) {
569 1.68 mycroft /* Ignore the port error if the device
570 1.68 mycroft vanished. */
571 1.68 mycroft } else if (status & UPS_PORT_ENABLED) {
572 1.98 cube aprint_error_dev(sc->sc_dev,
573 1.98 cube "illegal enable change, port %d\n", port);
574 1.11 augustss } else {
575 1.11 augustss /* Port error condition. */
576 1.126.2.2 skrll if (up->up_restartcnt) /* no message first time */
577 1.98 cube aprint_error_dev(sc->sc_dev,
578 1.98 cube "port error, restarting port %d\n",
579 1.98 cube port);
580 1.47 augustss
581 1.126.2.2 skrll if (up->up_restartcnt++ < USBD_RESTART_MAX)
582 1.11 augustss goto disco;
583 1.47 augustss else
584 1.98 cube aprint_error_dev(sc->sc_dev,
585 1.98 cube "port error, giving up port %d\n",
586 1.98 cube port);
587 1.11 augustss }
588 1.11 augustss }
589 1.126.2.13 skrll if (change & UPS_C_PORT_RESET) {
590 1.126.2.14 skrll /*
591 1.126.2.14 skrll * some xHCs set PortResetChange instead of CSC
592 1.126.2.14 skrll * when port is reset.
593 1.126.2.14 skrll */
594 1.126.2.14 skrll if ((status & UPS_CURRENT_CONNECT_STATUS) != 0) {
595 1.126.2.14 skrll change |= UPS_C_CONNECT_STATUS;
596 1.126.2.14 skrll }
597 1.126.2.12 skrll usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
598 1.126.2.13 skrll }
599 1.126.2.12 skrll if (change & UPS_C_BH_PORT_RESET) {
600 1.126.2.13 skrll /*
601 1.126.2.13 skrll * some xHCs set WarmResetChange instead of CSC
602 1.126.2.13 skrll * when port is reset.
603 1.126.2.13 skrll */
604 1.126.2.13 skrll if ((status & UPS_CURRENT_CONNECT_STATUS) != 0) {
605 1.126.2.13 skrll change |= UPS_C_CONNECT_STATUS;
606 1.126.2.13 skrll }
607 1.126.2.12 skrll usbd_clear_port_feature(dev, port,
608 1.126.2.12 skrll UHF_C_BH_PORT_RESET);
609 1.126.2.12 skrll }
610 1.126.2.12 skrll if (change & UPS_C_PORT_LINK_STATE)
611 1.126.2.12 skrll usbd_clear_port_feature(dev, port,
612 1.126.2.12 skrll UHF_C_PORT_LINK_STATE);
613 1.126.2.12 skrll if (change & UPS_C_PORT_CONFIG_ERROR)
614 1.126.2.12 skrll usbd_clear_port_feature(dev, port,
615 1.126.2.12 skrll UHF_C_PORT_CONFIG_ERROR);
616 1.87 drochner
617 1.87 drochner /* XXX handle overcurrent and resume events! */
618 1.87 drochner
619 1.126.2.13 skrll if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
620 1.116 jakllsch /* No status change, just do recursive explore. */
621 1.126.2.2 skrll if (up->up_dev != NULL && up->up_dev->ud_hub != NULL)
622 1.126.2.2 skrll up->up_dev->ud_hub->uh_explore(up->up_dev);
623 1.1 augustss continue;
624 1.116 jakllsch }
625 1.42 augustss
626 1.42 augustss /* We have a connect status change, handle it. */
627 1.42 augustss
628 1.126.2.9 skrll DPRINTF("status change hub=%d port=%d", dev->ud_addr, port, 0,
629 1.126.2.9 skrll 0);
630 1.1 augustss usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
631 1.1 augustss /*
632 1.1 augustss * If there is already a device on the port the change status
633 1.1 augustss * must mean that is has disconnected. Looking at the
634 1.1 augustss * current connect status is not enough to figure this out
635 1.1 augustss * since a new unit may have been connected before we handle
636 1.1 augustss * the disconnect.
637 1.1 augustss */
638 1.11 augustss disco:
639 1.126.2.2 skrll if (up->up_dev != NULL) {
640 1.1 augustss /* Disconnected */
641 1.126.2.9 skrll DPRINTF("uhub %d device addr=%d disappeared on port %d",
642 1.126.2.9 skrll device_unit(sc->sc_dev), up->up_dev->ud_addr, port,
643 1.126.2.9 skrll 0);
644 1.126.2.9 skrll
645 1.108 dyoung usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
646 1.58 augustss usbd_clear_port_feature(dev, port,
647 1.1 augustss UHF_C_PORT_CONNECTION);
648 1.126.2.12 skrll continue;
649 1.1 augustss }
650 1.35 augustss if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
651 1.42 augustss /* Nothing connected, just ignore it. */
652 1.126.2.9 skrll DPRINTFN(3, "uhub %d port=%d !CURRENT_CONNECT_STATUS",
653 1.126.2.9 skrll device_unit(sc->sc_dev), port, 0, 0);
654 1.126.2.12 skrll usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
655 1.126.2.12 skrll usbd_clear_port_feature(dev, port,
656 1.126.2.12 skrll UHF_C_PORT_CONNECTION);
657 1.1 augustss continue;
658 1.35 augustss }
659 1.1 augustss
660 1.1 augustss /* Connected */
661 1.126.2.9 skrll DPRINTF("unit %d dev->speed=%u dev->depth=%u",
662 1.126.2.9 skrll device_unit(sc->sc_dev), dev->ud_speed, dev->ud_depth, 0);
663 1.42 augustss
664 1.126.2.12 skrll /*
665 1.126.2.12 skrll * To check whether port has power,
666 1.126.2.12 skrll * check UPS_PORT_POWER bit if port speed is HS/FS/LS and
667 1.126.2.12 skrll * check UPS_PORT_POWER_SS bit if port speed is SS.
668 1.126.2.12 skrll */
669 1.126.2.12 skrll if (status & UPS_OTHER_SPEED) {
670 1.126.2.12 skrll if (!(status & UPS_PORT_POWER_SS))
671 1.126.2.12 skrll aprint_normal_dev(sc->sc_dev,
672 1.126.2.12 skrll "strange, connected port %d has no power\n",
673 1.126.2.12 skrll port);
674 1.126.2.12 skrll } else {
675 1.126.2.12 skrll if (!(status & UPS_PORT_POWER))
676 1.126.2.12 skrll aprint_normal_dev(sc->sc_dev,
677 1.126.2.12 skrll "strange, connected port %d has no power\n",
678 1.126.2.12 skrll port);
679 1.126.2.12 skrll }
680 1.42 augustss
681 1.1 augustss /* Wait for maximum device power up time. */
682 1.13 augustss usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
683 1.11 augustss
684 1.1 augustss /* Reset port, which implies enabling it. */
685 1.126.2.2 skrll if (usbd_reset_port(dev, port, &up->up_status)) {
686 1.98 cube aprint_error_dev(sc->sc_dev,
687 1.98 cube "port %d reset failed\n", port);
688 1.54 augustss continue;
689 1.54 augustss }
690 1.54 augustss /* Get port status again, it might have changed during reset */
691 1.126.2.2 skrll err = usbd_get_port_status(dev, port, &up->up_status);
692 1.54 augustss if (err) {
693 1.126.2.9 skrll DPRINTF("uhub %d port=%d get port status failed, "
694 1.126.2.9 skrll "err %d", device_unit(sc->sc_dev), port, err, 0);
695 1.54 augustss continue;
696 1.54 augustss }
697 1.126.2.2 skrll status = UGETW(up->up_status.wPortStatus);
698 1.126.2.2 skrll change = UGETW(up->up_status.wPortChange);
699 1.126.2.12 skrll if (USB_IS_SS(dev->ud_speed)) {
700 1.126.2.12 skrll status |= UPS_OTHER_SPEED;
701 1.126.2.12 skrll USETW(up->up_status.wPortStatus, status);
702 1.126.2.12 skrll }
703 1.126.2.11 skrll DPRINTF("hub %d port %d after reset: s/c=%x/%x",
704 1.126.2.11 skrll device_unit(sc->sc_dev), port, status, change);
705 1.126.2.11 skrll
706 1.54 augustss if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
707 1.54 augustss /* Nothing connected, just ignore it. */
708 1.54 augustss #ifdef DIAGNOSTIC
709 1.98 cube aprint_debug_dev(sc->sc_dev,
710 1.98 cube "port %d, device disappeared after reset\n", port);
711 1.54 augustss #endif
712 1.1 augustss continue;
713 1.35 augustss }
714 1.110 kiyohara if (!(status & UPS_PORT_ENABLED)) {
715 1.110 kiyohara /* Not allowed send/receive packet. */
716 1.110 kiyohara #ifdef DIAGNOSTIC
717 1.115 sborrill printf("%s: port %d, device not enabled\n",
718 1.112 dyoung device_xname(sc->sc_dev), port);
719 1.110 kiyohara #endif
720 1.110 kiyohara continue;
721 1.110 kiyohara }
722 1.126.2.12 skrll /* port reset may cause Warm Reset Change, drop it. */
723 1.126.2.12 skrll if (change & UPS_C_BH_PORT_RESET)
724 1.126.2.12 skrll usbd_clear_port_feature(dev, port,
725 1.126.2.12 skrll UHF_C_BH_PORT_RESET);
726 1.1 augustss
727 1.56 augustss /* Figure out device speed */
728 1.126.2.12 skrll if (status & UPS_OTHER_SPEED) {
729 1.125 skrll speed = USB_SPEED_SUPER;
730 1.126.2.12 skrll } else if (status & UPS_HIGH_SPEED)
731 1.56 augustss speed = USB_SPEED_HIGH;
732 1.56 augustss else if (status & UPS_LOW_SPEED)
733 1.56 augustss speed = USB_SPEED_LOW;
734 1.56 augustss else
735 1.56 augustss speed = USB_SPEED_FULL;
736 1.126.2.9 skrll
737 1.126.2.9 skrll DPRINTF("uhub %d speed %u", device_unit(sc->sc_dev), speed, 0,
738 1.126.2.9 skrll 0);
739 1.126.2.9 skrll
740 1.1 augustss /* Get device info and set its address. */
741 1.126.2.2 skrll err = usbd_new_device(sc->sc_dev, dev->ud_bus,
742 1.126.2.2 skrll dev->ud_depth + 1, speed, port, up);
743 1.1 augustss /* XXX retry a few times? */
744 1.33 augustss if (err) {
745 1.126.2.9 skrll DPRINTF("usbd_new_device failed, error %d", err, 0, 0,
746 1.126.2.9 skrll 0);
747 1.1 augustss /* Avoid addressing problems by disabling. */
748 1.1 augustss /* usbd_reset_port(dev, port, &up->status); */
749 1.30 augustss
750 1.58 augustss /*
751 1.30 augustss * The unit refused to accept a new address, or had
752 1.30 augustss * some other serious problem. Since we cannot leave
753 1.30 augustss * at 0 we have to disable the port instead.
754 1.30 augustss */
755 1.98 cube aprint_error_dev(sc->sc_dev,
756 1.98 cube "device problem, disabling port %d\n", port);
757 1.30 augustss usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
758 1.1 augustss } else {
759 1.47 augustss /* The port set up succeeded, reset error count. */
760 1.126.2.2 skrll up->up_restartcnt = 0;
761 1.47 augustss
762 1.126.2.2 skrll if (up->up_dev->ud_hub)
763 1.126.2.2 skrll up->up_dev->ud_hub->uh_explore(up->up_dev);
764 1.1 augustss }
765 1.1 augustss }
766 1.87 drochner /* enable status change notifications again */
767 1.101 drochner if (!sc->sc_isehciroothub)
768 1.101 drochner memset(sc->sc_status, 0, sc->sc_statuslen);
769 1.87 drochner sc->sc_explorepending = 0;
770 1.126.2.12 skrll for (int i = 0; i < sc->sc_statuslen; i++) {
771 1.126.2.12 skrll if (sc->sc_statuspend[i] != 0) {
772 1.126.2.12 skrll memcpy(sc->sc_status, sc->sc_statuspend,
773 1.126.2.12 skrll sc->sc_statuslen);
774 1.126.2.12 skrll memset(sc->sc_statuspend, 0, sc->sc_statuslen);
775 1.126.2.12 skrll usb_needs_explore(sc->sc_hub);
776 1.126.2.12 skrll break;
777 1.126.2.12 skrll }
778 1.126.2.12 skrll }
779 1.126.2.4 skrll return USBD_NORMAL_COMPLETION;
780 1.1 augustss }
781 1.1 augustss
782 1.18 augustss /*
783 1.18 augustss * Called from process context when the hub is gone.
784 1.18 augustss * Detach all devices on active ports.
785 1.18 augustss */
786 1.105 dyoung int
787 1.105 dyoung uhub_detach(device_t self, int flags)
788 1.18 augustss {
789 1.107 dyoung struct uhub_softc *sc = device_private(self);
790 1.126.2.2 skrll struct usbd_hub *hub = sc->sc_hub->ud_hub;
791 1.18 augustss struct usbd_port *rup;
792 1.108 dyoung int nports, port, rc;
793 1.18 augustss
794 1.126.2.9 skrll UHUBHIST_FUNC(); UHUBHIST_CALLED();
795 1.126.2.9 skrll
796 1.126.2.9 skrll DPRINTF("uhub %d flags=%d", device_unit(self), flags, 0, 0);
797 1.18 augustss
798 1.39 augustss if (hub == NULL) /* Must be partially working */
799 1.126.2.4 skrll return 0;
800 1.18 augustss
801 1.117 mrg /* XXXSMP usb */
802 1.117 mrg KERNEL_LOCK(1, curlwp);
803 1.117 mrg
804 1.126.2.2 skrll nports = hub->uh_hubdesc.bNbrPorts;
805 1.32 augustss for(port = 0; port < nports; port++) {
806 1.126.2.2 skrll rup = &hub->uh_ports[port];
807 1.126.2.2 skrll if (rup->up_dev == NULL)
808 1.108 dyoung continue;
809 1.117 mrg if ((rc = usb_disconnect_port(rup, self, flags)) != 0) {
810 1.117 mrg /* XXXSMP usb */
811 1.117 mrg KERNEL_UNLOCK_ONE(curlwp);
812 1.117 mrg
813 1.108 dyoung return rc;
814 1.117 mrg }
815 1.18 augustss }
816 1.58 augustss
817 1.108 dyoung pmf_device_deregister(self);
818 1.108 dyoung usbd_abort_pipe(sc->sc_ipipe);
819 1.108 dyoung usbd_close_pipe(sc->sc_ipipe);
820 1.108 dyoung
821 1.105 dyoung usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, sc->sc_dev);
822 1.37 augustss
823 1.83 drochner #if 0
824 1.70 augustss if (hub->ports[0].tt)
825 1.126.2.3 skrll kmem_free(hub->ports[0].tt,
826 1.126.2.3 skrll (UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
827 1.126.2.15 skrll sizeof(struct usbd_tt));
828 1.83 drochner #endif
829 1.126.2.3 skrll kmem_free(hub,
830 1.126.2.3 skrll sizeof(*hub) + (nports-1) * sizeof(struct usbd_port));
831 1.126.2.2 skrll sc->sc_hub->ud_hub = NULL;
832 1.84 drochner if (sc->sc_status)
833 1.126.2.6 skrll kmem_free(sc->sc_status, sc->sc_statuslen);
834 1.126.2.12 skrll if (sc->sc_statuspend)
835 1.126.2.12 skrll kmem_free(sc->sc_statuspend, sc->sc_statuslen);
836 1.101 drochner if (sc->sc_statusbuf)
837 1.126.2.3 skrll kmem_free(sc->sc_statusbuf, sc->sc_statuslen);
838 1.18 augustss
839 1.117 mrg /* XXXSMP usb */
840 1.117 mrg KERNEL_UNLOCK_ONE(curlwp);
841 1.117 mrg
842 1.126.2.4 skrll return 0;
843 1.18 augustss }
844 1.34 augustss
845 1.103 kent int
846 1.103 kent uhub_rescan(device_t self, const char *ifattr, const int *locators)
847 1.103 kent {
848 1.103 kent struct uhub_softc *sc = device_private(self);
849 1.126.2.2 skrll struct usbd_hub *hub = sc->sc_hub->ud_hub;
850 1.126.2.7 skrll struct usbd_device *dev;
851 1.124 martin int port;
852 1.103 kent
853 1.126.2.2 skrll for (port = 0; port < hub->uh_hubdesc.bNbrPorts; port++) {
854 1.126.2.2 skrll dev = hub->uh_ports[port].up_dev;
855 1.103 kent if (dev == NULL)
856 1.103 kent continue;
857 1.124 martin usbd_reattach_device(sc->sc_dev, dev, port, locators);
858 1.103 kent }
859 1.103 kent return 0;
860 1.103 kent }
861 1.103 kent
862 1.34 augustss /* Called when a device has been detached from it */
863 1.95 dyoung void
864 1.95 dyoung uhub_childdet(device_t self, device_t child)
865 1.34 augustss {
866 1.95 dyoung struct uhub_softc *sc = device_private(self);
867 1.126.2.7 skrll struct usbd_device *devhub = sc->sc_hub;
868 1.126.2.7 skrll struct usbd_device *dev;
869 1.95 dyoung int nports;
870 1.95 dyoung int port;
871 1.95 dyoung int i;
872 1.95 dyoung
873 1.126.2.2 skrll if (!devhub->ud_hub)
874 1.95 dyoung /* should never happen; children are only created after init */
875 1.95 dyoung panic("hub not fully initialised, but child deleted?");
876 1.95 dyoung
877 1.126.2.2 skrll nports = devhub->ud_hub->uh_hubdesc.bNbrPorts;
878 1.95 dyoung for (port = 0; port < nports; port++) {
879 1.126.2.2 skrll dev = devhub->ud_hub->uh_ports[port].up_dev;
880 1.126.2.2 skrll if (!dev || dev->ud_subdevlen == 0)
881 1.95 dyoung continue;
882 1.126.2.2 skrll for (i = 0; i < dev->ud_subdevlen; i++) {
883 1.126.2.2 skrll if (dev->ud_subdevs[i] == child) {
884 1.126.2.2 skrll dev->ud_subdevs[i] = NULL;
885 1.126.2.2 skrll dev->ud_nifaces_claimed--;
886 1.95 dyoung }
887 1.95 dyoung }
888 1.126.2.2 skrll if (dev->ud_nifaces_claimed == 0) {
889 1.126.2.3 skrll kmem_free(dev->ud_subdevs,
890 1.126.2.3 skrll dev->ud_subdevlen * sizeof(device_t));
891 1.126.2.2 skrll dev->ud_subdevs = NULL;
892 1.126.2.2 skrll dev->ud_subdevlen = 0;
893 1.113 jmcneill }
894 1.95 dyoung }
895 1.34 augustss }
896 1.34 augustss
897 1.18 augustss
898 1.18 augustss /*
899 1.18 augustss * Hub interrupt.
900 1.18 augustss * This an indication that some port has changed status.
901 1.18 augustss * Notify the bus event handler thread that we need
902 1.18 augustss * to be explored again.
903 1.18 augustss */
904 1.1 augustss void
905 1.126.2.7 skrll uhub_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
906 1.1 augustss {
907 1.1 augustss struct uhub_softc *sc = addr;
908 1.1 augustss
909 1.126.2.9 skrll UHUBHIST_FUNC(); UHUBHIST_CALLED();
910 1.126.2.9 skrll
911 1.126.2.9 skrll DPRINTFN(5, "uhub %d", device_unit(sc->sc_dev), 0, 0, 0);
912 1.87 drochner
913 1.50 augustss if (status == USBD_STALLED)
914 1.9 augustss usbd_clear_endpoint_stall_async(sc->sc_ipipe);
915 1.126.2.12 skrll else if (status == USBD_NORMAL_COMPLETION) {
916 1.126.2.12 skrll int i;
917 1.126.2.12 skrll
918 1.126.2.12 skrll /* merge port bitmap into pending interrupts list */
919 1.126.2.12 skrll for (i = 0; i < sc->sc_statuslen; i++)
920 1.126.2.12 skrll sc->sc_statuspend[i] |= sc->sc_statusbuf[i];
921 1.126.2.12 skrll
922 1.126.2.12 skrll if (!sc->sc_explorepending) {
923 1.126.2.12 skrll /*
924 1.126.2.12 skrll * Make sure the status is not overwritten in between.
925 1.126.2.12 skrll * XXX we should suspend the pipe instead
926 1.126.2.12 skrll */
927 1.126.2.12 skrll sc->sc_explorepending = 1;
928 1.126.2.12 skrll memcpy(sc->sc_status, sc->sc_statuspend,
929 1.126.2.12 skrll sc->sc_statuslen);
930 1.126.2.12 skrll memset(sc->sc_statuspend, 0, sc->sc_statuslen);
931 1.126.2.12 skrll DPRINTFN(5, "uhub%d: exploring ports %02x",
932 1.126.2.12 skrll device_unit(sc->sc_dev), *sc->sc_status, 0, 0);
933 1.126.2.12 skrll usb_needs_explore(sc->sc_hub);
934 1.126.2.12 skrll }
935 1.87 drochner }
936 1.89 drochner /*
937 1.89 drochner * XXX workaround for broken implementation of the interrupt
938 1.89 drochner * pipe in EHCI root hub emulation which doesn't resend
939 1.89 drochner * status change notifications until handled: force a rescan
940 1.89 drochner * of the ports we touched in the last run
941 1.89 drochner */
942 1.89 drochner if (status == USBD_NORMAL_COMPLETION && sc->sc_explorepending &&
943 1.101 drochner sc->sc_isehciroothub)
944 1.89 drochner usb_needs_explore(sc->sc_hub);
945 1.1 augustss }
946