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