File: | dev/usb/uhub.c |
Warning: | line 553, column 2 Value stored to 'change' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: uhub.c,v 1.95 2020/07/31 10:49:33 mglocker Exp $ */ |
2 | /* $NetBSD: uhub.c,v 1.64 2003/02/08 03:32:51 ichiro Exp $ */ |
3 | /* $FreeBSD: src/sys/dev/usb/uhub.c,v 1.18 1999/11/17 22:33:43 n_hibma Exp $ */ |
4 | |
5 | /* |
6 | * Copyright (c) 1998 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 | #include <sys/param.h> |
36 | #include <sys/systm.h> |
37 | #include <sys/kernel.h> |
38 | #include <sys/malloc.h> |
39 | #include <sys/device.h> |
40 | |
41 | #include <machine/bus.h> |
42 | |
43 | #include <dev/usb/usb.h> |
44 | #include <dev/usb/usbdi.h> |
45 | #include <dev/usb/usbdi_util.h> |
46 | #include <dev/usb/usbdivar.h> |
47 | |
48 | #define UHUB_INTR_INTERVAL255 255 /* ms */ |
49 | |
50 | #ifdef UHUB_DEBUG |
51 | #define DPRINTF(x...) do { printf(x); } while (0) |
52 | #else |
53 | #define DPRINTF(x...) |
54 | #endif |
55 | |
56 | #define DEVNAME(sc)((sc)->sc_dev.dv_xname) ((sc)->sc_dev.dv_xname) |
57 | |
58 | struct uhub_softc { |
59 | struct device sc_dev; /* base device */ |
60 | struct usbd_device *sc_hub; /* USB device */ |
61 | struct usbd_pipe *sc_ipipe; /* interrupt pipe */ |
62 | |
63 | uint32_t sc_status; /* status from last interrupt */ |
64 | uint8_t *sc_statusbuf; /* per port status buffer */ |
65 | size_t sc_statuslen; /* status bufferlen */ |
66 | |
67 | u_char sc_running; |
68 | }; |
69 | #define UHUB_PROTO(sc)((sc)->sc_hub->ddesc.bDeviceProtocol) ((sc)->sc_hub->ddesc.bDeviceProtocol) |
70 | #define UHUB_IS_HIGH_SPEED(sc)(((sc)->sc_hub->ddesc.bDeviceProtocol) != 0x00) (UHUB_PROTO(sc)((sc)->sc_hub->ddesc.bDeviceProtocol) != UDPROTO_FSHUB0x00) |
71 | #define UHUB_IS_SINGLE_TT(sc)(((sc)->sc_hub->ddesc.bDeviceProtocol) == 0x01) (UHUB_PROTO(sc)((sc)->sc_hub->ddesc.bDeviceProtocol) == UDPROTO_HSHUBSTT0x01) |
72 | |
73 | int uhub_explore(struct usbd_device *hub); |
74 | void uhub_intr(struct usbd_xfer *, void *, usbd_status); |
75 | int uhub_port_connect(struct uhub_softc *, int, int); |
76 | |
77 | /* |
78 | * We need two attachment points: |
79 | * hub to usb and hub to hub |
80 | * Every other driver only connects to hubs |
81 | */ |
82 | |
83 | int uhub_match(struct device *, void *, void *); |
84 | void uhub_attach(struct device *, struct device *, void *); |
85 | int uhub_detach(struct device *, int); |
86 | |
87 | struct cfdriver uhub_cd = { |
88 | NULL((void *)0), "uhub", DV_DULL |
89 | }; |
90 | |
91 | const struct cfattach uhub_ca = { |
92 | sizeof(struct uhub_softc), uhub_match, uhub_attach, uhub_detach |
93 | }; |
94 | |
95 | const struct cfattach uhub_uhub_ca = { |
96 | sizeof(struct uhub_softc), uhub_match, uhub_attach, uhub_detach |
97 | }; |
98 | |
99 | int |
100 | uhub_match(struct device *parent, void *match, void *aux) |
101 | { |
102 | struct usb_attach_arg *uaa = aux; |
103 | usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device); |
104 | |
105 | if (uaa->iface == NULL((void *)0)) |
106 | return (UMATCH_NONE0); |
107 | |
108 | /* |
109 | * The subclass for hubs seems to be 0 for some and 1 for others, |
110 | * so we just ignore the subclass. |
111 | */ |
112 | if (dd->bDeviceClass == UDCLASS_HUB0x09) |
113 | return (UMATCH_DEVCLASS_DEVSUBCLASS10); |
114 | return (UMATCH_NONE0); |
115 | } |
116 | |
117 | void |
118 | uhub_attach(struct device *parent, struct device *self, void *aux) |
119 | { |
120 | struct uhub_softc *sc = (struct uhub_softc *)self; |
121 | struct usb_attach_arg *uaa = aux; |
122 | struct usbd_device *dev = uaa->device; |
123 | struct usbd_hub *hub = NULL((void *)0); |
124 | union { |
125 | usb_hub_descriptor_t hs; |
126 | usb_hub_ss_descriptor_t ss; |
127 | } hd; |
128 | int p, port, nports, powerdelay; |
129 | struct usbd_interface *iface = uaa->iface; |
130 | usb_endpoint_descriptor_t *ed; |
131 | struct usbd_tt *tts = NULL((void *)0); |
132 | uint8_t ttthink = 0; |
133 | usbd_status err; |
134 | #ifdef UHUB_DEBUG |
135 | int nremov; |
136 | #endif |
137 | |
138 | sc->sc_hub = dev; |
139 | |
140 | if (dev->depth > USB_HUB_MAX_DEPTH5) { |
141 | printf("%s: hub depth (%d) exceeded, hub ignored\n", |
142 | sc->sc_dev.dv_xname, USB_HUB_MAX_DEPTH5); |
143 | return; |
144 | } |
145 | |
146 | /* |
147 | * Super-Speed hubs need to know their depth to be able to |
148 | * parse the bits of the route-string that correspond to |
149 | * their downstream port number. |
150 | * |
151 | * This does no apply to root hubs. |
152 | */ |
153 | if (dev->depth != 0 && dev->speed == USB_SPEED_SUPER4) { |
154 | if (usbd_set_hub_depth(dev, dev->depth - 1)) { |
155 | printf("%s: unable to set HUB depth\n", |
156 | sc->sc_dev.dv_xname); |
157 | return; |
158 | } |
159 | } |
160 | |
161 | /* Get hub descriptor. */ |
162 | if (dev->speed == USB_SPEED_SUPER4) { |
163 | err = usbd_get_hub_ss_descriptor(dev, &hd.ss, 1); |
164 | nports = hd.ss.bNbrPorts; |
165 | powerdelay = (hd.ss.bPwrOn2PwrGood * UHD_PWRON_FACTOR2); |
166 | if (!err && nports > 7) |
167 | usbd_get_hub_ss_descriptor(dev, &hd.ss, nports); |
168 | } else { |
169 | err = usbd_get_hub_descriptor(dev, &hd.hs, 1); |
170 | nports = hd.hs.bNbrPorts; |
171 | powerdelay = (hd.hs.bPwrOn2PwrGood * UHD_PWRON_FACTOR2); |
172 | ttthink = UGETW(hd.hs.wHubCharacteristics)(*(u_int16_t *)(hd.hs.wHubCharacteristics)) & UHD_TT_THINK0x0060; |
173 | if (!err && nports > 7) |
174 | usbd_get_hub_descriptor(dev, &hd.hs, nports); |
175 | } |
176 | |
177 | if (err) { |
178 | DPRINTF("%s: getting hub descriptor failed, error=%s\n", |
179 | sc->sc_dev.dv_xname, usbd_errstr(err)); |
180 | return; |
181 | } |
182 | |
183 | #ifdef UHUB_DEBUG |
184 | for (nremov = 0, port = 1; port <= nports; port++) { |
185 | if (dev->speed == USB_SPEED_SUPER4) { |
186 | if (!UHD_NOT_REMOV(&hd.ss, port)(((&hd.ss)->DeviceRemovable[(port)/8] >> ((port) % 8)) & 1)) |
187 | nremov++; |
188 | } else { |
189 | if (!UHD_NOT_REMOV(&hd.hs, port)(((&hd.hs)->DeviceRemovable[(port)/8] >> ((port) % 8)) & 1)) |
190 | nremov++; |
191 | } |
192 | } |
193 | |
194 | printf("%s: %d port%s with %d removable, %s powered", |
195 | sc->sc_dev.dv_xname, nports, nports != 1 ? "s" : "", |
196 | nremov, dev->self_powered ? "self" : "bus"); |
197 | |
198 | if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)(((sc)->sc_hub->ddesc.bDeviceProtocol) != 0x00)) { |
199 | printf(", %s transaction translator%s", |
200 | UHUB_IS_SINGLE_TT(sc)(((sc)->sc_hub->ddesc.bDeviceProtocol) == 0x01) ? "single" : "multiple", |
201 | UHUB_IS_SINGLE_TT(sc)(((sc)->sc_hub->ddesc.bDeviceProtocol) == 0x01) ? "" : "s"); |
202 | } |
203 | |
204 | printf("\n"); |
205 | #endif |
206 | |
207 | if (nports == 0) { |
208 | printf("%s: no ports, hub ignored\n", sc->sc_dev.dv_xname); |
209 | goto bad; |
210 | } |
211 | |
212 | hub = malloc(sizeof(*hub), M_USBDEV102, M_NOWAIT0x0002); |
213 | if (hub == NULL((void *)0)) |
214 | return; |
215 | hub->ports = mallocarray(nports, sizeof(struct usbd_port), |
216 | M_USBDEV102, M_NOWAIT0x0002); |
217 | if (hub->ports == NULL((void *)0)) { |
218 | free(hub, M_USBDEV102, sizeof *hub); |
219 | return; |
220 | } |
221 | dev->hub = hub; |
222 | dev->hub->hubsoftc = sc; |
223 | hub->explore = uhub_explore; |
224 | hub->nports = nports; |
225 | hub->powerdelay = powerdelay; |
226 | hub->ttthink = ttthink >> 5; |
227 | |
228 | if (!dev->self_powered && dev->powersrc->parent != NULL((void *)0) && |
229 | !dev->powersrc->parent->self_powered) { |
230 | printf("%s: bus powered hub connected to bus powered hub, " |
231 | "ignored\n", sc->sc_dev.dv_xname); |
232 | goto bad; |
233 | } |
234 | |
235 | /* Set up interrupt pipe. */ |
236 | ed = usbd_interface2endpoint_descriptor(iface, 0); |
237 | if (ed == NULL((void *)0)) { |
238 | printf("%s: no endpoint descriptor\n", sc->sc_dev.dv_xname); |
239 | goto bad; |
240 | } |
241 | if (UE_GET_XFERTYPE(ed->bmAttributes)((ed->bmAttributes) & 0x03) != UE_INTERRUPT0x03) { |
242 | printf("%s: bad interrupt endpoint\n", sc->sc_dev.dv_xname); |
243 | goto bad; |
244 | } |
245 | |
246 | sc->sc_statuslen = (nports + 1 + 7) / 8; |
247 | sc->sc_statusbuf = malloc(sc->sc_statuslen, M_USBDEV102, M_NOWAIT0x0002); |
248 | if (!sc->sc_statusbuf) |
249 | goto bad; |
250 | |
251 | err = usbd_open_pipe_intr(iface, ed->bEndpointAddress, |
252 | USBD_SHORT_XFER_OK0x04, &sc->sc_ipipe, sc, sc->sc_statusbuf, |
253 | sc->sc_statuslen, uhub_intr, UHUB_INTR_INTERVAL255); |
254 | if (err) { |
255 | printf("%s: cannot open interrupt pipe\n", |
256 | sc->sc_dev.dv_xname); |
257 | goto bad; |
258 | } |
259 | |
260 | /* Wait with power off for a while. */ |
261 | usbd_delay_ms(dev, USB_POWER_DOWN_TIME200); |
262 | |
263 | /* |
264 | * To have the best chance of success we do things in the exact same |
265 | * order as Windoze98. This should not be necessary, but some |
266 | * devices do not follow the USB specs to the letter. |
267 | * |
268 | * These are the events on the bus when a hub is attached: |
269 | * Get device and config descriptors (see attach code) |
270 | * Get hub descriptor (see above) |
271 | * For all ports |
272 | * turn on power |
273 | * wait for power to become stable |
274 | * (all below happens in explore code) |
275 | * For all ports |
276 | * clear C_PORT_CONNECTION |
277 | * For all ports |
278 | * get port status |
279 | * if device connected |
280 | * wait 100 ms |
281 | * turn on reset |
282 | * wait |
283 | * clear C_PORT_RESET |
284 | * get port status |
285 | * proceed with device attachment |
286 | */ |
287 | |
288 | if (UHUB_IS_HIGH_SPEED(sc)(((sc)->sc_hub->ddesc.bDeviceProtocol) != 0x00)) { |
289 | tts = mallocarray((UHUB_IS_SINGLE_TT(sc)(((sc)->sc_hub->ddesc.bDeviceProtocol) == 0x01) ? 1 : nports), |
290 | sizeof(struct usbd_tt), M_USBDEV102, M_NOWAIT0x0002); |
291 | if (!tts) |
292 | goto bad; |
293 | } |
294 | /* Set up data structures */ |
295 | for (p = 0; p < nports; p++) { |
296 | struct usbd_port *up = &hub->ports[p]; |
297 | up->device = NULL((void *)0); |
298 | up->parent = dev; |
299 | up->portno = p + 1; |
300 | if (dev->self_powered) |
301 | /* Self powered hub, give ports maximum current. */ |
302 | up->power = USB_MAX_POWER500; |
303 | else |
304 | up->power = USB_MIN_POWER100; |
305 | up->restartcnt = 0; |
306 | up->reattach = 0; |
307 | if (UHUB_IS_HIGH_SPEED(sc)(((sc)->sc_hub->ddesc.bDeviceProtocol) != 0x00)) { |
308 | up->tt = &tts[UHUB_IS_SINGLE_TT(sc)(((sc)->sc_hub->ddesc.bDeviceProtocol) == 0x01) ? 0 : p]; |
309 | up->tt->hub = hub; |
310 | } else { |
311 | up->tt = NULL((void *)0); |
312 | } |
313 | } |
314 | |
315 | for (port = 1; port <= nports; port++) { |
316 | /* Turn the power on. */ |
317 | err = usbd_set_port_feature(dev, port, UHF_PORT_POWER8); |
318 | if (err) |
319 | printf("%s: port %d power on failed, %s\n", |
320 | sc->sc_dev.dv_xname, port, |
321 | usbd_errstr(err)); |
322 | /* Make sure we check the port status at least once. */ |
323 | sc->sc_status |= (1 << port); |
324 | } |
325 | |
326 | /* Wait for stable power. */ |
327 | if (dev->powersrc->parent != NULL((void *)0)) |
328 | usbd_delay_ms(dev, powerdelay + USB_EXTRA_POWER_UP_TIME20); |
329 | |
330 | /* The usual exploration will finish the setup. */ |
331 | |
332 | sc->sc_running = 1; |
333 | |
334 | return; |
335 | |
336 | bad: |
337 | free(sc->sc_statusbuf, M_USBDEV102, sc->sc_statuslen); |
338 | if (hub) { |
339 | free(hub->ports, M_USBDEV102, hub->nports * sizeof(*hub->ports)); |
340 | free(hub, M_USBDEV102, sizeof(*hub)); |
341 | } |
342 | dev->hub = NULL((void *)0); |
343 | } |
344 | |
345 | int |
346 | uhub_explore(struct usbd_device *dev) |
347 | { |
348 | struct uhub_softc *sc = dev->hub->hubsoftc; |
349 | struct usbd_port *up; |
350 | int status, change; |
351 | int port; |
352 | |
353 | if (usbd_is_dying(sc->sc_hub)) |
354 | return (EIO5); |
355 | |
356 | if (!sc->sc_running) |
357 | return (ENXIO6); |
358 | |
359 | /* Ignore hubs that are too deep. */ |
360 | if (sc->sc_hub->depth > USB_HUB_MAX_DEPTH5) |
361 | return (EOPNOTSUPP45); |
362 | |
363 | for (port = 1; port <= sc->sc_hub->hub->nports; port++) { |
364 | up = &sc->sc_hub->hub->ports[port-1]; |
365 | |
366 | change = 0; |
367 | status = 0; |
368 | |
369 | if ((sc->sc_status & (1 << port)) || up->reattach) { |
370 | sc->sc_status &= ~(1 << port); |
371 | |
372 | if (usbd_get_port_status(dev, port, &up->status)) |
373 | continue; |
374 | |
375 | status = UGETW(up->status.wPortStatus)(*(u_int16_t *)(up->status.wPortStatus)); |
376 | change = UGETW(up->status.wPortChange)(*(u_int16_t *)(up->status.wPortChange)); |
377 | DPRINTF("%s: port %d status=0x%04x change=0x%04x\n", |
378 | sc->sc_dev.dv_xname, port, status, change); |
379 | } |
380 | |
381 | if (up->reattach) { |
382 | change |= UPS_C_CONNECT_STATUS0x0001; |
383 | up->reattach = 0; |
384 | } |
385 | |
386 | if (change & UPS_C_PORT_ENABLED0x0002) { |
387 | usbd_clear_port_feature(sc->sc_hub, port, |
388 | UHF_C_PORT_ENABLE17); |
389 | if (change & UPS_C_CONNECT_STATUS0x0001) { |
390 | /* Ignore the port error if the device |
391 | vanished. */ |
392 | } else if (status & UPS_PORT_ENABLED0x0002) { |
393 | printf("%s: illegal enable change, port %d\n", |
394 | sc->sc_dev.dv_xname, port); |
395 | } else { |
396 | /* Port error condition. */ |
397 | if (up->restartcnt) /* no message first time */ |
398 | printf("%s: port error, restarting " |
399 | "port %d\n", |
400 | sc->sc_dev.dv_xname, port); |
401 | |
402 | if (up->restartcnt++ < USBD_RESTART_MAX5) |
403 | change |= UPS_C_CONNECT_STATUS0x0001; |
404 | else |
405 | printf("%s: port error, giving up " |
406 | "port %d\n", |
407 | sc->sc_dev.dv_xname, port); |
408 | } |
409 | } |
410 | |
411 | if (change & UPS_C_PORT_RESET0x0010) { |
412 | usbd_clear_port_feature(sc->sc_hub, port, |
413 | UHF_C_PORT_RESET20); |
414 | change |= UPS_C_CONNECT_STATUS0x0001; |
415 | } |
416 | |
417 | if (change & UPS_C_BH_PORT_RESET0x0020 && |
418 | sc->sc_hub->speed == USB_SPEED_SUPER4) { |
419 | usbd_clear_port_feature(sc->sc_hub, port, |
420 | UHF_C_BH_PORT_RESET29); |
421 | } |
422 | |
423 | if (change & UPS_C_CONNECT_STATUS0x0001) { |
424 | if (uhub_port_connect(sc, port, status)) |
425 | continue; |
426 | |
427 | /* The port set up succeeded, reset error count. */ |
428 | up->restartcnt = 0; |
429 | } |
430 | |
431 | if (change & UPS_C_PORT_LINK_STATE0x0040) { |
432 | usbd_clear_port_feature(sc->sc_hub, port, |
433 | UHF_C_PORT_LINK_STATE25); |
434 | } |
435 | |
436 | /* Recursive explore. */ |
437 | if (up->device != NULL((void *)0) && up->device->hub != NULL((void *)0)) |
438 | up->device->hub->explore(up->device); |
439 | } |
440 | |
441 | return (0); |
442 | } |
443 | |
444 | /* |
445 | * Called from process context when the hub is gone. |
446 | * Detach all devices on active ports. |
447 | */ |
448 | int |
449 | uhub_detach(struct device *self, int flags) |
450 | { |
451 | struct uhub_softc *sc = (struct uhub_softc *)self; |
452 | struct usbd_hub *hub = sc->sc_hub->hub; |
453 | struct usbd_port *rup; |
454 | int port; |
455 | |
456 | if (hub == NULL((void *)0)) /* Must be partially working */ |
457 | return (0); |
458 | |
459 | usbd_close_pipe(sc->sc_ipipe); |
460 | |
461 | for (port = 0; port < hub->nports; port++) { |
462 | rup = &hub->ports[port]; |
463 | if (rup->device != NULL((void *)0)) { |
464 | usbd_detach(rup->device, self); |
465 | rup->device = NULL((void *)0); |
466 | } |
467 | } |
468 | |
469 | free(hub->ports[0].tt, M_USBDEV102, |
470 | (UHUB_IS_SINGLE_TT(sc)(((sc)->sc_hub->ddesc.bDeviceProtocol) == 0x01) ? 1 : hub->nports) * sizeof(struct usbd_tt)); |
471 | free(sc->sc_statusbuf, M_USBDEV102, sc->sc_statuslen); |
472 | free(hub->ports, M_USBDEV102, hub->nports * sizeof(*hub->ports)); |
473 | free(hub, M_USBDEV102, sizeof(*hub)); |
474 | sc->sc_hub->hub = NULL((void *)0); |
475 | |
476 | return (0); |
477 | } |
478 | |
479 | /* |
480 | * This is an indication that some port has changed status. Remember |
481 | * the ports that need attention and notify the USB task thread that |
482 | * we need to be explored again. |
483 | */ |
484 | void |
485 | uhub_intr(struct usbd_xfer *xfer, void *addr, usbd_status status) |
486 | { |
487 | struct uhub_softc *sc = addr; |
488 | uint32_t stats = 0; |
489 | int i; |
490 | |
491 | if (usbd_is_dying(sc->sc_hub)) |
492 | return; |
493 | |
494 | DPRINTF("%s: intr status=%d\n", sc->sc_dev.dv_xname, status); |
495 | |
496 | if (status == USBD_STALLED) |
497 | usbd_clear_endpoint_stall_async(sc->sc_ipipe); |
498 | else if (status == USBD_NORMAL_COMPLETION) { |
499 | for (i = 0; i < xfer->actlen; i++) |
500 | stats |= (uint32_t)(xfer->buffer[i]) << (i * 8); |
501 | sc->sc_status |= stats; |
502 | |
503 | usb_needs_explore(sc->sc_hub, 0); |
504 | } |
505 | } |
506 | |
507 | int |
508 | uhub_port_connect(struct uhub_softc *sc, int port, int status) |
509 | { |
510 | struct usbd_port *up = &sc->sc_hub->hub->ports[port-1]; |
511 | int speed, change; |
512 | |
513 | /* We have a connect status change, handle it. */ |
514 | usbd_clear_port_feature(sc->sc_hub, port, UHF_C_PORT_CONNECTION16); |
515 | |
516 | /* |
517 | * If there is already a device on the port the change status |
518 | * must mean that is has disconnected. Looking at the |
519 | * current connect status is not enough to figure this out |
520 | * since a new unit may have been connected before we handle |
521 | * the disconnect. |
522 | */ |
523 | if (up->device != NULL((void *)0)) { |
524 | /* Disconnected */ |
525 | usbd_detach(up->device, &sc->sc_dev); |
526 | up->device = NULL((void *)0); |
527 | } |
528 | |
529 | /* Nothing connected, just ignore it. */ |
530 | if ((status & UPS_CURRENT_CONNECT_STATUS0x0001) == 0) |
531 | return (0); |
532 | |
533 | /* Connected */ |
534 | if ((status & (UPS_PORT_POWER0x0100|UPS_PORT_POWER_SS0x0200)) == 0) { |
535 | printf("%s: connected port %d has no power\n", DEVNAME(sc)((sc)->sc_dev.dv_xname), |
536 | port); |
537 | return (-1); |
538 | } |
539 | |
540 | /* Wait for maximum device power up time. */ |
541 | usbd_delay_ms(sc->sc_hub, USB_PORT_POWERUP_DELAY300); |
542 | |
543 | /* Reset port, which implies enabling it. */ |
544 | if (usbd_reset_port(sc->sc_hub, port)) { |
545 | printf("%s: port %d reset failed\n", DEVNAME(sc)((sc)->sc_dev.dv_xname), port); |
546 | return (-1); |
547 | } |
548 | /* Get port status again, it might have changed during reset */ |
549 | if (usbd_get_port_status(sc->sc_hub, port, &up->status)) |
550 | return (-1); |
551 | |
552 | status = UGETW(up->status.wPortStatus)(*(u_int16_t *)(up->status.wPortStatus)); |
553 | change = UGETW(up->status.wPortChange)(*(u_int16_t *)(up->status.wPortChange)); |
Value stored to 'change' is never read | |
554 | DPRINTF("%s: port %d status=0x%04x change=0x%04x\n", DEVNAME(sc), |
555 | port, status, change); |
556 | |
557 | /* Nothing connected, just ignore it. */ |
558 | if ((status & UPS_CURRENT_CONNECT_STATUS0x0001) == 0) { |
559 | DPRINTF("%s: port %d, device disappeared after reset\n", |
560 | DEVNAME(sc), port); |
561 | return (-1); |
562 | } |
563 | |
564 | /* |
565 | * Figure out device speed. This is a bit tricky because |
566 | * UPS_PORT_POWER_SS and UPS_LOW_SPEED share the same bit. |
567 | */ |
568 | if ((status & UPS_PORT_POWER0x0100) == 0) |
569 | status &= ~UPS_PORT_POWER_SS0x0200; |
570 | |
571 | if (status & UPS_HIGH_SPEED0x0400) |
572 | speed = USB_SPEED_HIGH3; |
573 | else if (status & UPS_LOW_SPEED0x0200) |
574 | speed = USB_SPEED_LOW1; |
575 | else { |
576 | /* |
577 | * If there is no power bit set, it is certainly |
578 | * a Super Speed device, so use the speed of its |
579 | * parent hub. |
580 | */ |
581 | if (status & UPS_PORT_POWER0x0100) |
582 | speed = USB_SPEED_FULL2; |
583 | else |
584 | speed = sc->sc_hub->speed; |
585 | } |
586 | |
587 | /* |
588 | * Reduce the speed, otherwise we won't setup the proper |
589 | * transfer methods. |
590 | */ |
591 | if (speed > sc->sc_hub->speed) |
592 | speed = sc->sc_hub->speed; |
593 | |
594 | /* Get device info and set its address. */ |
595 | if (usbd_new_device(&sc->sc_dev, sc->sc_hub->bus, sc->sc_hub->depth + 1, |
596 | speed, port, up)) { |
597 | /* |
598 | * The unit refused to accept a new address, or had |
599 | * some other serious problem. Since we cannot leave |
600 | * at 0 we have to disable the port instead. |
601 | */ |
602 | printf("%s: device problem, disabling port %d\n", DEVNAME(sc)((sc)->sc_dev.dv_xname), |
603 | port); |
604 | usbd_clear_port_feature(sc->sc_hub, port, UHF_PORT_ENABLE1); |
605 | |
606 | return (-1); |
607 | } |
608 | |
609 | return (0); |
610 | } |