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