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