uhub.c revision 1.126.2.12 1 /* $NetBSD: uhub.c,v 1.126.2.12 2015/05/28 06:15:47 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.12 2015/05/28 06:15:47 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 int is_wrc = 0;
590 if (change & UPS_C_PORT_RESET)
591 usbd_clear_port_feature(dev, port, UHF_C_PORT_RESET);
592 if (change & UPS_C_BH_PORT_RESET) {
593 is_wrc = 1;
594 usbd_clear_port_feature(dev, port,
595 UHF_C_BH_PORT_RESET);
596 }
597 if (change & UPS_C_PORT_LINK_STATE)
598 usbd_clear_port_feature(dev, port,
599 UHF_C_PORT_LINK_STATE);
600 if (change & UPS_C_PORT_CONFIG_ERROR)
601 usbd_clear_port_feature(dev, port,
602 UHF_C_PORT_CONFIG_ERROR);
603
604 /* XXX handle overcurrent and resume events! */
605
606 /* xHCI sets WRC instead of CSC when port is reset */
607 if (!reconnect && !(change & UPS_C_CONNECT_STATUS) &&
608 !(is_wrc && (status & UPS_CURRENT_CONNECT_STATUS))) {
609 /* No status change, just do recursive explore. */
610 if (up->up_dev != NULL && up->up_dev->ud_hub != NULL)
611 up->up_dev->ud_hub->uh_explore(up->up_dev);
612 continue;
613 }
614
615 /* We have a connect status change, handle it. */
616
617 DPRINTF("status change hub=%d port=%d", dev->ud_addr, port, 0,
618 0);
619 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
620 /*
621 * If there is already a device on the port the change status
622 * must mean that is has disconnected. Looking at the
623 * current connect status is not enough to figure this out
624 * since a new unit may have been connected before we handle
625 * the disconnect.
626 */
627 disco:
628 if (up->up_dev != NULL) {
629 /* Disconnected */
630 DPRINTF("uhub %d device addr=%d disappeared on port %d",
631 device_unit(sc->sc_dev), up->up_dev->ud_addr, port,
632 0);
633
634 usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
635 usbd_clear_port_feature(dev, port,
636 UHF_C_PORT_CONNECTION);
637 continue;
638 }
639 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
640 /* Nothing connected, just ignore it. */
641 DPRINTFN(3, "uhub %d port=%d !CURRENT_CONNECT_STATUS",
642 device_unit(sc->sc_dev), port, 0, 0);
643 usb_disconnect_port(up, sc->sc_dev, DETACH_FORCE);
644 usbd_clear_port_feature(dev, port,
645 UHF_C_PORT_CONNECTION);
646 continue;
647 }
648
649 /* Connected */
650 DPRINTF("unit %d dev->speed=%u dev->depth=%u",
651 device_unit(sc->sc_dev), dev->ud_speed, dev->ud_depth, 0);
652
653 /*
654 * To check whether port has power,
655 * check UPS_PORT_POWER bit if port speed is HS/FS/LS and
656 * check UPS_PORT_POWER_SS bit if port speed is SS.
657 */
658 if (status & UPS_OTHER_SPEED) {
659 if (!(status & UPS_PORT_POWER_SS))
660 aprint_normal_dev(sc->sc_dev,
661 "strange, connected port %d has no power\n",
662 port);
663 } else {
664 if (!(status & UPS_PORT_POWER))
665 aprint_normal_dev(sc->sc_dev,
666 "strange, connected port %d has no power\n",
667 port);
668 }
669
670 /* Wait for maximum device power up time. */
671 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
672
673 /* Reset port, which implies enabling it. */
674 if (usbd_reset_port(dev, port, &up->up_status)) {
675 aprint_error_dev(sc->sc_dev,
676 "port %d reset failed\n", port);
677 continue;
678 }
679 /* Get port status again, it might have changed during reset */
680 err = usbd_get_port_status(dev, port, &up->up_status);
681 if (err) {
682 DPRINTF("uhub %d port=%d get port status failed, "
683 "err %d", device_unit(sc->sc_dev), port, err, 0);
684 continue;
685 }
686 status = UGETW(up->up_status.wPortStatus);
687 change = UGETW(up->up_status.wPortChange);
688 if (USB_IS_SS(dev->ud_speed)) {
689 status |= UPS_OTHER_SPEED;
690 USETW(up->up_status.wPortStatus, status);
691 }
692 DPRINTF("hub %d port %d after reset: s/c=%x/%x",
693 device_unit(sc->sc_dev), port, status, change);
694
695 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
696 /* Nothing connected, just ignore it. */
697 #ifdef DIAGNOSTIC
698 aprint_debug_dev(sc->sc_dev,
699 "port %d, device disappeared after reset\n", port);
700 #endif
701 continue;
702 }
703 if (!(status & UPS_PORT_ENABLED)) {
704 /* Not allowed send/receive packet. */
705 #ifdef DIAGNOSTIC
706 printf("%s: port %d, device not enabled\n",
707 device_xname(sc->sc_dev), port);
708 #endif
709 continue;
710 }
711 /* port reset may cause Warm Reset Change, drop it. */
712 if (change & UPS_C_BH_PORT_RESET)
713 usbd_clear_port_feature(dev, port,
714 UHF_C_BH_PORT_RESET);
715
716 /* Figure out device speed */
717 if (status & UPS_OTHER_SPEED) {
718 speed = USB_SPEED_SUPER;
719 } else if (status & UPS_HIGH_SPEED)
720 speed = USB_SPEED_HIGH;
721 else if (status & UPS_LOW_SPEED)
722 speed = USB_SPEED_LOW;
723 else
724 speed = USB_SPEED_FULL;
725
726 DPRINTF("uhub %d speed %u", device_unit(sc->sc_dev), speed, 0,
727 0);
728
729 /* Get device info and set its address. */
730 err = usbd_new_device(sc->sc_dev, dev->ud_bus,
731 dev->ud_depth + 1, speed, port, up);
732 /* XXX retry a few times? */
733 if (err) {
734 DPRINTF("usbd_new_device failed, error %d", err, 0, 0,
735 0);
736 /* Avoid addressing problems by disabling. */
737 /* usbd_reset_port(dev, port, &up->status); */
738
739 /*
740 * The unit refused to accept a new address, or had
741 * some other serious problem. Since we cannot leave
742 * at 0 we have to disable the port instead.
743 */
744 aprint_error_dev(sc->sc_dev,
745 "device problem, disabling port %d\n", port);
746 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
747 } else {
748 /* The port set up succeeded, reset error count. */
749 up->up_restartcnt = 0;
750
751 if (up->up_dev->ud_hub)
752 up->up_dev->ud_hub->uh_explore(up->up_dev);
753 }
754 }
755 /* enable status change notifications again */
756 if (!sc->sc_isehciroothub)
757 memset(sc->sc_status, 0, sc->sc_statuslen);
758 sc->sc_explorepending = 0;
759 for (int i = 0; i < sc->sc_statuslen; i++) {
760 if (sc->sc_statuspend[i] != 0) {
761 memcpy(sc->sc_status, sc->sc_statuspend,
762 sc->sc_statuslen);
763 memset(sc->sc_statuspend, 0, sc->sc_statuslen);
764 usb_needs_explore(sc->sc_hub);
765 break;
766 }
767 }
768 return USBD_NORMAL_COMPLETION;
769 }
770
771 /*
772 * Called from process context when the hub is gone.
773 * Detach all devices on active ports.
774 */
775 int
776 uhub_detach(device_t self, int flags)
777 {
778 struct uhub_softc *sc = device_private(self);
779 struct usbd_hub *hub = sc->sc_hub->ud_hub;
780 struct usbd_port *rup;
781 int nports, port, rc;
782
783 UHUBHIST_FUNC(); UHUBHIST_CALLED();
784
785 DPRINTF("uhub %d flags=%d", device_unit(self), flags, 0, 0);
786
787 if (hub == NULL) /* Must be partially working */
788 return 0;
789
790 /* XXXSMP usb */
791 KERNEL_LOCK(1, curlwp);
792
793 nports = hub->uh_hubdesc.bNbrPorts;
794 for(port = 0; port < nports; port++) {
795 rup = &hub->uh_ports[port];
796 if (rup->up_dev == NULL)
797 continue;
798 if ((rc = usb_disconnect_port(rup, self, flags)) != 0) {
799 /* XXXSMP usb */
800 KERNEL_UNLOCK_ONE(curlwp);
801
802 return rc;
803 }
804 }
805
806 pmf_device_deregister(self);
807 usbd_abort_pipe(sc->sc_ipipe);
808 usbd_close_pipe(sc->sc_ipipe);
809
810 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub, sc->sc_dev);
811
812 #if 0
813 if (hub->ports[0].tt)
814 kmem_free(hub->ports[0].tt,
815 (UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
816 sizeof (struct usbd_tt));
817 #endif
818 kmem_free(hub,
819 sizeof(*hub) + (nports-1) * sizeof(struct usbd_port));
820 sc->sc_hub->ud_hub = NULL;
821 if (sc->sc_status)
822 kmem_free(sc->sc_status, sc->sc_statuslen);
823 if (sc->sc_statuspend)
824 kmem_free(sc->sc_statuspend, sc->sc_statuslen);
825 if (sc->sc_statusbuf)
826 kmem_free(sc->sc_statusbuf, sc->sc_statuslen);
827
828 /* XXXSMP usb */
829 KERNEL_UNLOCK_ONE(curlwp);
830
831 return 0;
832 }
833
834 int
835 uhub_rescan(device_t self, const char *ifattr, const int *locators)
836 {
837 struct uhub_softc *sc = device_private(self);
838 struct usbd_hub *hub = sc->sc_hub->ud_hub;
839 struct usbd_device *dev;
840 int port;
841
842 for (port = 0; port < hub->uh_hubdesc.bNbrPorts; port++) {
843 dev = hub->uh_ports[port].up_dev;
844 if (dev == NULL)
845 continue;
846 usbd_reattach_device(sc->sc_dev, dev, port, locators);
847 }
848 return 0;
849 }
850
851 /* Called when a device has been detached from it */
852 void
853 uhub_childdet(device_t self, device_t child)
854 {
855 struct uhub_softc *sc = device_private(self);
856 struct usbd_device *devhub = sc->sc_hub;
857 struct usbd_device *dev;
858 int nports;
859 int port;
860 int i;
861
862 if (!devhub->ud_hub)
863 /* should never happen; children are only created after init */
864 panic("hub not fully initialised, but child deleted?");
865
866 nports = devhub->ud_hub->uh_hubdesc.bNbrPorts;
867 for (port = 0; port < nports; port++) {
868 dev = devhub->ud_hub->uh_ports[port].up_dev;
869 if (!dev || dev->ud_subdevlen == 0)
870 continue;
871 for (i = 0; i < dev->ud_subdevlen; i++) {
872 if (dev->ud_subdevs[i] == child) {
873 dev->ud_subdevs[i] = NULL;
874 dev->ud_nifaces_claimed--;
875 }
876 }
877 if (dev->ud_nifaces_claimed == 0) {
878 kmem_free(dev->ud_subdevs,
879 dev->ud_subdevlen * sizeof(device_t));
880 dev->ud_subdevs = NULL;
881 dev->ud_subdevlen = 0;
882 }
883 }
884 }
885
886
887 /*
888 * Hub interrupt.
889 * This an indication that some port has changed status.
890 * Notify the bus event handler thread that we need
891 * to be explored again.
892 */
893 void
894 uhub_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
895 {
896 struct uhub_softc *sc = addr;
897
898 UHUBHIST_FUNC(); UHUBHIST_CALLED();
899
900 DPRINTFN(5, "uhub %d", device_unit(sc->sc_dev), 0, 0, 0);
901
902 if (status == USBD_STALLED)
903 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
904 else if (status == USBD_NORMAL_COMPLETION) {
905 int i;
906
907 /* merge port bitmap into pending interrupts list */
908 for (i = 0; i < sc->sc_statuslen; i++)
909 sc->sc_statuspend[i] |= sc->sc_statusbuf[i];
910
911 if (!sc->sc_explorepending) {
912 /*
913 * Make sure the status is not overwritten in between.
914 * XXX we should suspend the pipe instead
915 */
916 sc->sc_explorepending = 1;
917 memcpy(sc->sc_status, sc->sc_statuspend,
918 sc->sc_statuslen);
919 memset(sc->sc_statuspend, 0, sc->sc_statuslen);
920 DPRINTFN(5, "uhub%d: exploring ports %02x",
921 device_unit(sc->sc_dev), *sc->sc_status, 0, 0);
922 usb_needs_explore(sc->sc_hub);
923 }
924 }
925 /*
926 * XXX workaround for broken implementation of the interrupt
927 * pipe in EHCI root hub emulation which doesn't resend
928 * status change notifications until handled: force a rescan
929 * of the ports we touched in the last run
930 */
931 if (status == USBD_NORMAL_COMPLETION && sc->sc_explorepending &&
932 sc->sc_isehciroothub)
933 usb_needs_explore(sc->sc_hub);
934 }
935