This source file includes following definitions.
- uhub_match
- uhub_attach
- uhub_explore
- uhub_activate
- uhub_detach
- uhub_intr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/device.h>
51 #include <sys/proc.h>
52
53 #include <machine/bus.h>
54
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
58 #include <dev/usb/usbdivar.h>
59
60 #define UHUB_INTR_INTERVAL 255
61
62 #ifdef UHUB_DEBUG
63 #define DPRINTF(x) do { if (uhubdebug) printf x; } while (0)
64 #define DPRINTFN(n,x) do { if (uhubdebug>(n)) printf x; } while (0)
65 int uhubdebug = 0;
66 #else
67 #define DPRINTF(x)
68 #define DPRINTFN(n,x)
69 #endif
70
71 struct uhub_softc {
72 struct device sc_dev;
73 usbd_device_handle sc_hub;
74 usbd_pipe_handle sc_ipipe;
75 u_int8_t sc_status[1];
76 u_char sc_running;
77 };
78 #define UHUB_PROTO(sc) ((sc)->sc_hub->ddesc.bDeviceProtocol)
79 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
80 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
81
82 usbd_status uhub_explore(usbd_device_handle hub);
83 void uhub_intr(usbd_xfer_handle, usbd_private_handle,usbd_status);
84
85
86
87
88
89
90
91 int uhub_match(struct device *, void *, void *);
92 void uhub_attach(struct device *, struct device *, void *);
93 int uhub_detach(struct device *, int);
94 int uhub_activate(struct device *, enum devact);
95
96 struct cfdriver uhub_cd = {
97 NULL, "uhub", DV_DULL
98 };
99
100 const struct cfattach uhub_ca = {
101 sizeof(struct uhub_softc),
102 uhub_match,
103 uhub_attach,
104 uhub_detach,
105 uhub_activate,
106 };
107
108 struct cfattach uhub_uhub_ca = {
109 sizeof(struct uhub_softc), uhub_match, uhub_attach,
110 uhub_detach, uhub_activate
111 };
112
113 int
114 uhub_match(struct device *parent, void *match, void *aux)
115 {
116 struct usb_attach_arg *uaa = aux;
117 usb_device_descriptor_t *dd = usbd_get_device_descriptor(uaa->device);
118
119 DPRINTFN(5,("uhub_match, dd=%p\n", dd));
120
121
122
123
124 if (uaa->iface == NULL && dd->bDeviceClass == UDCLASS_HUB)
125 return (UMATCH_DEVCLASS_DEVSUBCLASS);
126 return (UMATCH_NONE);
127 }
128
129 void
130 uhub_attach(struct device *parent, struct device *self, void *aux)
131 {
132 struct uhub_softc *sc = (struct uhub_softc *)self;
133 struct usb_attach_arg *uaa = aux;
134 usbd_device_handle dev = uaa->device;
135 char *devinfop;
136 usbd_status err;
137 struct usbd_hub *hub = NULL;
138 usb_device_request_t req;
139 usb_hub_descriptor_t hubdesc;
140 int p, port, nports, nremov, pwrdly;
141 usbd_interface_handle iface;
142 usb_endpoint_descriptor_t *ed;
143 struct usbd_tt *tts = NULL;
144
145 DPRINTFN(1,("uhub_attach\n"));
146 sc->sc_hub = dev;
147
148 devinfop = usbd_devinfo_alloc(dev, 0);
149 printf(": %s\n", devinfop);
150 usbd_devinfo_free(devinfop);
151
152 err = usbd_set_config_index(dev, 0, 1);
153 if (err) {
154 DPRINTF(("%s: configuration failed, error=%s\n",
155 sc->sc_dev.dv_xname, usbd_errstr(err)));
156 return;
157 }
158
159 if (dev->depth > USB_HUB_MAX_DEPTH) {
160 printf("%s: hub depth (%d) exceeded, hub ignored\n",
161 sc->sc_dev.dv_xname, USB_HUB_MAX_DEPTH);
162 return;
163 }
164
165
166 req.bmRequestType = UT_READ_CLASS_DEVICE;
167 req.bRequest = UR_GET_DESCRIPTOR;
168 USETW2(req.wValue, UDESC_HUB, 0);
169 USETW(req.wIndex, 0);
170 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE);
171 DPRINTFN(1,("usb_init_hub: getting hub descriptor\n"));
172 err = usbd_do_request(dev, &req, &hubdesc);
173 nports = hubdesc.bNbrPorts;
174 if (!err && nports > 7) {
175 USETW(req.wLength, USB_HUB_DESCRIPTOR_SIZE + (nports+1) / 8);
176 err = usbd_do_request(dev, &req, &hubdesc);
177 }
178 if (err) {
179 DPRINTF(("%s: getting hub descriptor failed, error=%s\n",
180 sc->sc_dev.dv_xname, usbd_errstr(err)));
181 return;
182 }
183
184 for (nremov = 0, port = 1; port <= nports; port++)
185 if (!UHD_NOT_REMOV(&hubdesc, port))
186 nremov++;
187
188 #ifdef UHUB_DEBUG
189 printf("%s: %d port%s with %d removable, %s powered",
190 sc->sc_dev.dv_xname, nports, nports != 1 ? "s" : "",
191 nremov, dev->self_powered ? "self" : "bus");
192
193 if (dev->depth > 0 && UHUB_IS_HIGH_SPEED(sc)) {
194 printf(", %s transaction translator%s",
195 UHUB_IS_SINGLE_TT(sc) ? "single" : "multiple",
196 UHUB_IS_SINGLE_TT(sc) ? "" : "s");
197 }
198 printf("\n");
199 #endif
200
201 if (nports == 0) {
202 printf("%s: no ports, hub ignored\n", sc->sc_dev.dv_xname);
203 goto bad;
204 }
205
206 hub = malloc(sizeof(*hub) + (nports-1) * sizeof(struct usbd_port),
207 M_USBDEV, M_NOWAIT);
208 if (hub == NULL)
209 return;
210 dev->hub = hub;
211 dev->hub->hubsoftc = sc;
212 hub->explore = uhub_explore;
213 hub->hubdesc = hubdesc;
214
215 DPRINTFN(1,("usbhub_init_hub: selfpowered=%d, parent=%p, "
216 "parent->selfpowered=%d\n",
217 dev->self_powered, dev->powersrc->parent,
218 dev->powersrc->parent ?
219 dev->powersrc->parent->self_powered : 0));
220
221 if (!dev->self_powered && dev->powersrc->parent != NULL &&
222 !dev->powersrc->parent->self_powered) {
223 printf("%s: bus powered hub connected to bus powered hub, "
224 "ignored\n", sc->sc_dev.dv_xname);
225 goto bad;
226 }
227
228
229 err = usbd_device2interface_handle(dev, 0, &iface);
230 if (err) {
231 printf("%s: no interface handle\n", sc->sc_dev.dv_xname);
232 goto bad;
233 }
234 ed = usbd_interface2endpoint_descriptor(iface, 0);
235 if (ed == NULL) {
236 printf("%s: no endpoint descriptor\n", sc->sc_dev.dv_xname);
237 goto bad;
238 }
239 if ((ed->bmAttributes & UE_XFERTYPE) != UE_INTERRUPT) {
240 printf("%s: bad interrupt endpoint\n", sc->sc_dev.dv_xname);
241 goto bad;
242 }
243
244 err = usbd_open_pipe_intr(iface, ed->bEndpointAddress,
245 USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_status,
246 sizeof(sc->sc_status), uhub_intr, UHUB_INTR_INTERVAL);
247 if (err) {
248 printf("%s: cannot open interrupt pipe\n",
249 sc->sc_dev.dv_xname);
250 goto bad;
251 }
252
253
254 usbd_delay_ms(dev, USB_POWER_DOWN_TIME);
255
256 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, &sc->sc_dev);
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 if (UHUB_IS_HIGH_SPEED(sc)) {
284 tts = malloc((UHUB_IS_SINGLE_TT(sc) ? 1 : nports) *
285 sizeof (struct usbd_tt), M_USBDEV, M_NOWAIT);
286 if (!tts)
287 goto bad;
288 }
289
290 for (p = 0; p < nports; p++) {
291 struct usbd_port *up = &hub->ports[p];
292 up->device = NULL;
293 up->parent = dev;
294 up->portno = p+1;
295 if (dev->self_powered)
296
297 up->power = USB_MAX_POWER;
298 else
299 up->power = USB_MIN_POWER;
300 up->restartcnt = 0;
301 up->reattach = 0;
302 if (UHUB_IS_HIGH_SPEED(sc)) {
303 up->tt = &tts[UHUB_IS_SINGLE_TT(sc) ? 0 : p];
304 up->tt->hub = hub;
305 } else {
306 up->tt = NULL;
307 }
308 }
309
310
311
312 pwrdly = dev->hub->hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR
313 + USB_EXTRA_POWER_UP_TIME;
314 for (port = 1; port <= nports; port++) {
315
316 err = usbd_set_port_feature(dev, port, UHF_PORT_POWER);
317 if (err)
318 printf("%s: port %d power on failed, %s\n",
319 sc->sc_dev.dv_xname, port,
320 usbd_errstr(err));
321 DPRINTF(("usb_init_port: turn on port %d power\n", port));
322 }
323
324
325 if (dev->powersrc->parent != NULL)
326 usbd_delay_ms(dev, pwrdly);
327
328
329
330 sc->sc_running = 1;
331
332 return;
333
334 bad:
335 if (hub)
336 free(hub, M_USBDEV);
337 dev->hub = NULL;
338 }
339
340 usbd_status
341 uhub_explore(usbd_device_handle dev)
342 {
343 usb_hub_descriptor_t *hd = &dev->hub->hubdesc;
344 struct uhub_softc *sc = dev->hub->hubsoftc;
345 struct usbd_port *up;
346 usbd_status err;
347 int speed;
348 int port;
349 int change, status, reconnect;
350
351 DPRINTFN(10, ("uhub_explore dev=%p addr=%d\n", dev, dev->address));
352
353 if (!sc->sc_running)
354 return (USBD_NOT_STARTED);
355
356
357 if (dev->depth > USB_HUB_MAX_DEPTH)
358 return (USBD_TOO_DEEP);
359
360 for(port = 1; port <= hd->bNbrPorts; port++) {
361 up = &dev->hub->ports[port-1];
362 err = usbd_get_port_status(dev, port, &up->status);
363 if (err) {
364 DPRINTF(("uhub_explore: get port status failed, "
365 "error=%s\n", usbd_errstr(err)));
366 continue;
367 }
368 status = UGETW(up->status.wPortStatus);
369 change = UGETW(up->status.wPortChange);
370 reconnect = up->reattach;
371 up->reattach = 0;
372 DPRINTFN(3,("uhub_explore: %s port %d status 0x%04x 0x%04x\n",
373 sc->sc_dev.dv_xname, port, status, change));
374 if (change & UPS_C_PORT_ENABLED) {
375 DPRINTF(("uhub_explore: C_PORT_ENABLED\n"));
376 usbd_clear_port_feature(dev, port, UHF_C_PORT_ENABLE);
377 if (change & UPS_C_CONNECT_STATUS) {
378
379
380 } else if (status & UPS_PORT_ENABLED) {
381 printf("%s: illegal enable change, port %d\n",
382 sc->sc_dev.dv_xname, port);
383 } else {
384
385 if (up->restartcnt)
386 printf("%s: port error, restarting "
387 "port %d\n",
388 sc->sc_dev.dv_xname, port);
389
390 if (up->restartcnt++ < USBD_RESTART_MAX)
391 goto disco;
392 else
393 printf("%s: port error, giving up "
394 "port %d\n",
395 sc->sc_dev.dv_xname, port);
396 }
397 }
398 if (!reconnect && !(change & UPS_C_CONNECT_STATUS)) {
399 DPRINTFN(3,("uhub_explore: port=%d !C_CONNECT_"
400 "STATUS\n", port));
401
402 if (up->device != NULL && up->device->hub != NULL)
403 up->device->hub->explore(up->device);
404 #if 0 && defined(DIAGNOSTIC)
405 if (up->device == NULL &&
406 (status & UPS_CURRENT_CONNECT_STATUS))
407 printf("%s: connected, no device\n",
408 sc->sc_dev.dv_xname);
409 #endif
410 continue;
411 }
412
413
414
415 DPRINTF(("uhub_explore: status change hub=%d port=%d\n",
416 dev->address, port));
417 usbd_clear_port_feature(dev, port, UHF_C_PORT_CONNECTION);
418
419
420
421
422
423
424
425
426 disco:
427 if (up->device != NULL) {
428
429 DPRINTF(("uhub_explore: device addr=%d disappeared "
430 "on port %d\n", up->device->address, port));
431 usb_disconnect_port(up, &sc->sc_dev);
432 usbd_clear_port_feature(dev, port,
433 UHF_C_PORT_CONNECTION);
434 }
435 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
436
437 DPRINTFN(3,("uhub_explore: port=%d !CURRENT_CONNECT"
438 "_STATUS\n", port));
439 continue;
440 }
441
442
443
444 if (!(status & UPS_PORT_POWER))
445 printf("%s: strange, connected port %d has no power\n",
446 sc->sc_dev.dv_xname, port);
447
448
449 usbd_delay_ms(dev, USB_PORT_POWERUP_DELAY);
450
451
452 if (usbd_reset_port(dev, port, &up->status)) {
453 printf("%s: port %d reset failed\n",
454 sc->sc_dev.dv_xname, port);
455 continue;
456 }
457
458 err = usbd_get_port_status(dev, port, &up->status);
459 if (err) {
460 DPRINTF(("uhub_explore: get port status failed, "
461 "error=%s\n", usbd_errstr(err)));
462 continue;
463 }
464 status = UGETW(up->status.wPortStatus);
465 change = UGETW(up->status.wPortChange);
466 if (!(status & UPS_CURRENT_CONNECT_STATUS)) {
467
468 #ifdef UHUB_DEBUG
469 printf("%s: port %d, device disappeared after reset\n",
470 sc->sc_dev.dv_xname, port);
471 #endif
472 continue;
473 }
474
475
476 if (status & UPS_HIGH_SPEED)
477 speed = USB_SPEED_HIGH;
478 else if (status & UPS_LOW_SPEED)
479 speed = USB_SPEED_LOW;
480 else
481 speed = USB_SPEED_FULL;
482
483 err = usbd_new_device(&sc->sc_dev, dev->bus,
484 dev->depth + 1, speed, port, up);
485
486 if (err) {
487 DPRINTFN(-1,("uhub_explore: usbd_new_device failed, "
488 "error=%s\n", usbd_errstr(err)));
489
490
491
492
493
494
495
496
497 printf("%s: device problem, disabling port %d\n",
498 sc->sc_dev.dv_xname, port);
499 usbd_clear_port_feature(dev, port, UHF_PORT_ENABLE);
500 } else {
501
502 up->restartcnt = 0;
503
504 if (up->device->hub)
505 up->device->hub->explore(up->device);
506 }
507 }
508 return (USBD_NORMAL_COMPLETION);
509 }
510
511 int
512 uhub_activate(struct device *self, enum devact act)
513 {
514 struct uhub_softc *sc = (struct uhub_softc *)self;
515 struct usbd_hub *hub = sc->sc_hub->hub;
516 usbd_device_handle dev;
517 int nports, port, i;
518
519 switch (act) {
520 case DVACT_ACTIVATE:
521 break;
522
523 case DVACT_DEACTIVATE:
524 if (hub == NULL)
525 break;
526 nports = hub->hubdesc.bNbrPorts;
527 for(port = 0; port < nports; port++) {
528 dev = hub->ports[port].device;
529 if (dev != NULL && dev->subdevs != NULL) {
530 for (i = 0; dev->subdevs[i] != NULL; i++)
531 config_deactivate(dev->subdevs[i]);
532 }
533 }
534 break;
535 }
536 return (0);
537 }
538
539
540
541
542
543 int
544 uhub_detach(struct device *self, int flags)
545 {
546 struct uhub_softc *sc = (struct uhub_softc *)self;
547 struct usbd_hub *hub = sc->sc_hub->hub;
548 struct usbd_port *rup;
549 int port, nports;
550
551 DPRINTF(("uhub_detach: sc=%p flags=%d\n", sc, flags));
552
553 if (hub == NULL)
554 return (0);
555
556 usbd_abort_pipe(sc->sc_ipipe);
557 usbd_close_pipe(sc->sc_ipipe);
558
559 nports = hub->hubdesc.bNbrPorts;
560 for(port = 0; port < nports; port++) {
561 rup = &hub->ports[port];
562 if (rup->device)
563 usb_disconnect_port(rup, self);
564 }
565
566 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_hub,
567 &sc->sc_dev);
568
569 if (hub->ports[0].tt)
570 free(hub->ports[0].tt, M_USBDEV);
571 free(hub, M_USBDEV);
572 sc->sc_hub->hub = NULL;
573
574 return (0);
575 }
576
577
578
579
580
581
582
583 void
584 uhub_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
585 {
586 struct uhub_softc *sc = addr;
587
588 DPRINTFN(5,("uhub_intr: sc=%p\n", sc));
589 if (status == USBD_STALLED)
590 usbd_clear_endpoint_stall_async(sc->sc_ipipe);
591 else if (status == USBD_NORMAL_COMPLETION)
592 usb_needs_explore(sc->sc_hub);
593 }