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