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