ehci.c revision 1.5 1 /* $NetBSD: ehci.c,v 1.5 2001/11/15 23:25:09 augustss Exp $ */
2
3 /*
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
41 *
42 * The EHCI 0.96 spec can be found at
43 * http://developer.intel.com/technology/usb/download/ehci-r096.pdf
44 *
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.5 2001/11/15 23:25:09 augustss Exp $");
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/device.h>
55 #include <sys/select.h>
56 #include <sys/proc.h>
57 #include <sys/queue.h>
58
59 #include <machine/bus.h>
60 #include <machine/endian.h>
61
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbdi.h>
64 #include <dev/usb/usbdivar.h>
65 #include <dev/usb/usb_mem.h>
66 #include <dev/usb/usb_quirks.h>
67
68 #include <dev/usb/ehcireg.h>
69 #include <dev/usb/ehcivar.h>
70
71 #ifdef EHCI_DEBUG
72 #define DPRINTF(x) if (ehcidebug) printf x
73 #define DPRINTFN(n,x) if (ehcidebug>(n)) printf x
74 int ehcidebug = 5;
75 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
76 #else
77 #define DPRINTF(x)
78 #define DPRINTFN(n,x)
79 #endif
80
81 struct ehci_pipe {
82 struct usbd_pipe pipe;
83 };
84
85 Static void ehci_shutdown(void *);
86 Static void ehci_power(int, void *);
87
88 Static usbd_status ehci_open(usbd_pipe_handle);
89 Static void ehci_poll(struct usbd_bus *);
90 Static void ehci_softintr(void *);
91
92 Static usbd_status ehci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
93 Static void ehci_freem(struct usbd_bus *, usb_dma_t *);
94
95 Static usbd_xfer_handle ehci_allocx(struct usbd_bus *);
96 Static void ehci_freex(struct usbd_bus *, usbd_xfer_handle);
97
98 Static usbd_status ehci_root_ctrl_transfer(usbd_xfer_handle);
99 Static usbd_status ehci_root_ctrl_start(usbd_xfer_handle);
100 Static void ehci_root_ctrl_abort(usbd_xfer_handle);
101 Static void ehci_root_ctrl_close(usbd_pipe_handle);
102 Static void ehci_root_ctrl_done(usbd_xfer_handle);
103
104 Static usbd_status ehci_root_intr_transfer(usbd_xfer_handle);
105 Static usbd_status ehci_root_intr_start(usbd_xfer_handle);
106 Static void ehci_root_intr_abort(usbd_xfer_handle);
107 Static void ehci_root_intr_close(usbd_pipe_handle);
108 Static void ehci_root_intr_done(usbd_xfer_handle);
109
110 Static usbd_status ehci_device_ctrl_transfer(usbd_xfer_handle);
111 Static usbd_status ehci_device_ctrl_start(usbd_xfer_handle);
112 Static void ehci_device_ctrl_abort(usbd_xfer_handle);
113 Static void ehci_device_ctrl_close(usbd_pipe_handle);
114 Static void ehci_device_ctrl_done(usbd_xfer_handle);
115
116 Static usbd_status ehci_device_bulk_transfer(usbd_xfer_handle);
117 Static usbd_status ehci_device_bulk_start(usbd_xfer_handle);
118 Static void ehci_device_bulk_abort(usbd_xfer_handle);
119 Static void ehci_device_bulk_close(usbd_pipe_handle);
120 Static void ehci_device_bulk_done(usbd_xfer_handle);
121
122 Static usbd_status ehci_device_intr_transfer(usbd_xfer_handle);
123 Static usbd_status ehci_device_intr_start(usbd_xfer_handle);
124 Static void ehci_device_intr_abort(usbd_xfer_handle);
125 Static void ehci_device_intr_close(usbd_pipe_handle);
126 Static void ehci_device_intr_done(usbd_xfer_handle);
127
128 Static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle);
129 Static usbd_status ehci_device_isoc_start(usbd_xfer_handle);
130 Static void ehci_device_isoc_abort(usbd_xfer_handle);
131 Static void ehci_device_isoc_close(usbd_pipe_handle);
132 Static void ehci_device_isoc_done(usbd_xfer_handle);
133
134 Static void ehci_device_clear_toggle(usbd_pipe_handle pipe);
135 Static void ehci_noop(usbd_pipe_handle pipe);
136
137 Static int ehci_str(usb_string_descriptor_t *, int, char *);
138
139 #ifdef EHCI_DEBUG
140 Static void ehci_dumpregs(ehci_softc_t *);
141 #endif
142
143 #define EHCI_INTR_ENDPT 1
144
145 Static struct usbd_bus_methods ehci_bus_methods = {
146 ehci_open,
147 ehci_softintr,
148 ehci_poll,
149 ehci_allocm,
150 ehci_freem,
151 ehci_allocx,
152 ehci_freex,
153 };
154
155 Static struct usbd_pipe_methods ehci_root_ctrl_methods = {
156 ehci_root_ctrl_transfer,
157 ehci_root_ctrl_start,
158 ehci_root_ctrl_abort,
159 ehci_root_ctrl_close,
160 ehci_noop,
161 ehci_root_ctrl_done,
162 };
163
164 Static struct usbd_pipe_methods ehci_root_intr_methods = {
165 ehci_root_intr_transfer,
166 ehci_root_intr_start,
167 ehci_root_intr_abort,
168 ehci_root_intr_close,
169 ehci_noop,
170 ehci_root_intr_done,
171 };
172
173 Static struct usbd_pipe_methods ehci_device_ctrl_methods = {
174 ehci_device_ctrl_transfer,
175 ehci_device_ctrl_start,
176 ehci_device_ctrl_abort,
177 ehci_device_ctrl_close,
178 ehci_noop,
179 ehci_device_ctrl_done,
180 };
181
182 Static struct usbd_pipe_methods ehci_device_intr_methods = {
183 ehci_device_intr_transfer,
184 ehci_device_intr_start,
185 ehci_device_intr_abort,
186 ehci_device_intr_close,
187 ehci_device_clear_toggle,
188 ehci_device_intr_done,
189 };
190
191 Static struct usbd_pipe_methods ehci_device_bulk_methods = {
192 ehci_device_bulk_transfer,
193 ehci_device_bulk_start,
194 ehci_device_bulk_abort,
195 ehci_device_bulk_close,
196 ehci_device_clear_toggle,
197 ehci_device_bulk_done,
198 };
199
200 Static struct usbd_pipe_methods ehci_device_isoc_methods = {
201 ehci_device_isoc_transfer,
202 ehci_device_isoc_start,
203 ehci_device_isoc_abort,
204 ehci_device_isoc_close,
205 ehci_noop,
206 ehci_device_isoc_done,
207 };
208
209 usbd_status
210 ehci_init(ehci_softc_t *sc)
211 {
212 u_int32_t version, sparams, cparams, hcr;
213 u_int i;
214 usbd_status err;
215
216 DPRINTF(("ehci_init: start\n"));
217
218 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
219
220 version = EREAD2(sc, EHCI_HCIVERSION);
221 printf("%s: EHCI version %x.%x\n", USBDEVNAME(sc->sc_bus.bdev),
222 version >> 8, version & 0xff);
223
224 sparams = EREAD4(sc, EHCI_HCSPARAMS);
225 DPRINTF(("ehci_init: sparams=0x%x\n", sparams));
226 if (EHCI_HCS_N_CC(sparams) != sc->sc_ncomp) {
227 printf("%s: wrong number of companions (%d != %d)\n",
228 USBDEVNAME(sc->sc_bus.bdev),
229 EHCI_HCS_N_CC(sparams), sc->sc_ncomp);
230 return (USBD_IOERROR);
231 }
232 if (sc->sc_ncomp > 0) {
233 printf("%s: companion controller%s, %d port%s each:",
234 USBDEVNAME(sc->sc_bus.bdev), sc->sc_ncomp!=1 ? "s" : "",
235 EHCI_HCS_N_PCC(sparams),
236 EHCI_HCS_N_PCC(sparams)!=1 ? "s" : "");
237 for (i = 0; i < sc->sc_ncomp; i++)
238 printf(" %s", USBDEVNAME(sc->sc_comps[i]->bdev));
239 printf("\n");
240 }
241 sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
242 cparams = EREAD4(sc, EHCI_HCCPARAMS);
243 DPRINTF(("ehci_init: cparams=0x%x\n", cparams));
244
245 sc->sc_bus.usbrev = USBREV_2_0;
246
247 /* Reset the controller */
248 DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
249 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
250 usb_delay_ms(&sc->sc_bus, 1);
251 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
252 for (i = 0; i < 100; i++) {
253 delay(10);
254 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
255 if (!hcr)
256 break;
257 }
258 if (hcr) {
259 printf("%s: reset timeout\n", USBDEVNAME(sc->sc_bus.bdev));
260 return (USBD_IOERROR);
261 }
262
263 /* frame list size at default, read back what we got and use that */
264 switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) {
265 case 0: sc->sc_flsize = 1024*4; break;
266 case 1: sc->sc_flsize = 512*4; break;
267 case 2: sc->sc_flsize = 256*4; break;
268 case 3: return (USBD_IOERROR);
269 }
270 err = usb_allocmem(&sc->sc_bus, sc->sc_flsize,
271 EHCI_FLALIGN_ALIGN, &sc->sc_fldma);
272 if (err)
273 return (err);
274 DPRINTF(("%s: flsize=%d\n", USBDEVNAME(sc->sc_bus.bdev),sc->sc_flsize));
275
276 /* Set up the bus struct. */
277 sc->sc_bus.methods = &ehci_bus_methods;
278 sc->sc_bus.pipe_size = sizeof(struct ehci_pipe);
279
280 sc->sc_powerhook = powerhook_establish(ehci_power, sc);
281 sc->sc_shutdownhook = shutdownhook_establish(ehci_shutdown, sc);
282
283 return (USBD_NORMAL_COMPLETION);
284 }
285
286 int
287 ehci_intr(void *v)
288 {
289 return (0);
290 }
291
292 void
293 ehci_softintr(void *v)
294 {
295 //ehci_softc_t *sc = v;
296 }
297
298 void
299 ehci_poll(struct usbd_bus *bus)
300 {
301 #if 0
302 ehci_softc_t *sc = (ehci_softc_t *)bus;
303 #ifdef EHCI_DEBUG
304 static int last;
305 int new;
306 new = OREAD4(sc, EHCI_INTERRUPT_STATUS);
307 if (new != last) {
308 DPRINTFN(10,("ehci_poll: intrs=0x%04x\n", new));
309 last = new;
310 }
311 #endif
312
313 if (OREAD4(sc, EHCI_INTERRUPT_STATUS) & sc->sc_eintrs)
314 ehci_intr1(sc);
315 #endif
316 }
317
318 int
319 ehci_detach(struct ehci_softc *sc, int flags)
320 {
321 int rv = 0;
322
323 if (sc->sc_child != NULL)
324 rv = config_detach(sc->sc_child, flags);
325
326 if (rv != 0)
327 return (rv);
328
329 if (sc->sc_powerhook != NULL)
330 powerhook_disestablish(sc->sc_powerhook);
331 if (sc->sc_shutdownhook != NULL)
332 shutdownhook_disestablish(sc->sc_shutdownhook);
333
334 /* XXX free other data structures XXX */
335
336 return (rv);
337 }
338
339
340 int
341 ehci_activate(device_ptr_t self, enum devact act)
342 {
343 struct ehci_softc *sc = (struct ehci_softc *)self;
344 int rv = 0;
345
346 switch (act) {
347 case DVACT_ACTIVATE:
348 return (EOPNOTSUPP);
349 break;
350
351 case DVACT_DEACTIVATE:
352 if (sc->sc_child != NULL)
353 rv = config_deactivate(sc->sc_child);
354 sc->sc_dying = 1;
355 break;
356 }
357 return (rv);
358 }
359
360 /*
361 * Handle suspend/resume.
362 *
363 * We need to switch to polling mode here, because this routine is
364 * called from an intterupt context. This is all right since we
365 * are almost suspended anyway.
366 */
367 void
368 ehci_power(int why, void *v)
369 {
370 ehci_softc_t *sc = v;
371 //u_int32_t ctl;
372 int s;
373
374 #ifdef EHCI_DEBUG
375 DPRINTF(("ehci_power: sc=%p, why=%d\n", sc, why));
376 ehci_dumpregs(sc);
377 #endif
378
379 s = splhardusb();
380 switch (why) {
381 case PWR_SUSPEND:
382 case PWR_STANDBY:
383 sc->sc_bus.use_polling++;
384 #if 0
385 OOO
386 ctl = OREAD4(sc, EHCI_CONTROL) & ~EHCI_HCFS_MASK;
387 if (sc->sc_control == 0) {
388 /*
389 * Preserve register values, in case that APM BIOS
390 * does not recover them.
391 */
392 sc->sc_control = ctl;
393 sc->sc_intre = OREAD4(sc, EHCI_INTERRUPT_ENABLE);
394 }
395 ctl |= EHCI_HCFS_SUSPEND;
396 OWRITE4(sc, EHCI_CONTROL, ctl);
397 #endif
398 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
399 sc->sc_bus.use_polling--;
400 break;
401 case PWR_RESUME:
402 sc->sc_bus.use_polling++;
403 #if 0
404 OOO
405 /* Some broken BIOSes do not recover these values */
406 OWRITE4(sc, EHCI_HCCA, DMAADDR(&sc->sc_hccadma));
407 OWRITE4(sc, EHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
408 OWRITE4(sc, EHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
409 if (sc->sc_intre)
410 OWRITE4(sc, EHCI_INTERRUPT_ENABLE,
411 sc->sc_intre & (EHCI_ALL_INTRS | EHCI_MIE));
412 if (sc->sc_control)
413 ctl = sc->sc_control;
414 else
415 ctl = OREAD4(sc, EHCI_CONTROL);
416 ctl |= EHCI_HCFS_RESUME;
417 OWRITE4(sc, EHCI_CONTROL, ctl);
418 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
419 ctl = (ctl & ~EHCI_HCFS_MASK) | EHCI_HCFS_OPERATIONAL;
420 OWRITE4(sc, EHCI_CONTROL, ctl);
421 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
422 sc->sc_control = sc->sc_intre = 0;
423 #endif
424 sc->sc_bus.use_polling--;
425 break;
426 case PWR_SOFTSUSPEND:
427 case PWR_SOFTSTANDBY:
428 case PWR_SOFTRESUME:
429 break;
430 }
431 splx(s);
432 }
433
434 /*
435 * Shut down the controller when the system is going down.
436 */
437 void
438 ehci_shutdown(void *v)
439 {
440 //ehci_softc_t *sc = v;
441
442 DPRINTF(("ehci_shutdown: stopping the HC\n"));
443 #if 0
444 OOO
445 OWRITE4(sc, EHCI_CONTROL, EHCI_HCFS_RESET);
446 #endif
447 }
448
449 usbd_status
450 ehci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
451 {
452 struct ehci_softc *sc = (struct ehci_softc *)bus;
453
454 return (usb_allocmem(&sc->sc_bus, size, 0, dma));
455 }
456
457 void
458 ehci_freem(struct usbd_bus *bus, usb_dma_t *dma)
459 {
460 struct ehci_softc *sc = (struct ehci_softc *)bus;
461
462 usb_freemem(&sc->sc_bus, dma);
463 }
464
465 usbd_xfer_handle
466 ehci_allocx(struct usbd_bus *bus)
467 {
468 struct ehci_softc *sc = (struct ehci_softc *)bus;
469 usbd_xfer_handle xfer;
470
471 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
472 if (xfer != NULL)
473 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, xfer, next);
474 else
475 xfer = malloc(sizeof(*xfer), M_USB, M_NOWAIT);
476 if (xfer != NULL)
477 memset(xfer, 0, sizeof *xfer);
478 return (xfer);
479 }
480
481 void
482 ehci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
483 {
484 struct ehci_softc *sc = (struct ehci_softc *)bus;
485
486 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
487 }
488
489 Static void
490 ehci_device_clear_toggle(usbd_pipe_handle pipe)
491 {
492 #if 0
493 OOO
494 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
495
496 epipe->sed->ed.ed_headp &= htole32(~EHCI_TOGGLECARRY);
497 #endif
498 }
499
500 Static void
501 ehci_noop(usbd_pipe_handle pipe)
502 {
503 }
504
505 #ifdef EHCI_DEBUG
506 void
507 ehci_dumpregs(ehci_softc_t *sc)
508 {
509 // OOO
510 }
511 #endif
512
513 usbd_status
514 ehci_open(usbd_pipe_handle pipe)
515 {
516 usbd_device_handle dev = pipe->device;
517 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
518 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
519 u_int8_t addr = dev->address;
520 #if 0
521 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
522 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
523 ehci_soft_ed_t *sed;
524 ehci_soft_td_t *std;
525 ehci_soft_itd_t *sitd;
526 ehci_physaddr_t tdphys;
527 u_int32_t fmt;
528 usbd_status err;
529 int s;
530 int ival;
531 #endif
532
533 DPRINTFN(1, ("ehci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
534 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
535
536 if (addr == sc->sc_addr) {
537 switch (ed->bEndpointAddress) {
538 case USB_CONTROL_ENDPOINT:
539 pipe->methods = &ehci_root_ctrl_methods;
540 break;
541 case UE_DIR_IN | EHCI_INTR_ENDPT:
542 pipe->methods = &ehci_root_intr_methods;
543 break;
544 default:
545 return (USBD_INVAL);
546 }
547 } else {
548 #if 0
549 std = NULL;
550 sed = NULL;
551
552 sed = ehci_alloc_sed(sc);
553 if (sed == NULL)
554 goto bad0;
555 epipe->sed = sed;
556 if (xfertype == UE_ISOCHRONOUS) {
557 sitd = ehci_alloc_sitd(sc);
558 if (sitd == NULL) {
559 ehci_free_sitd(sc, sitd);
560 goto bad1;
561 }
562 epipe->tail.itd = sitd;
563 tdphys = sitd->physaddr;
564 fmt = EHCI_ED_FORMAT_ISO;
565 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
566 fmt |= EHCI_ED_DIR_IN;
567 else
568 fmt |= EHCI_ED_DIR_OUT;
569 } else {
570 std = ehci_alloc_std(sc);
571 if (std == NULL) {
572 ehci_free_std(sc, std);
573 goto bad1;
574 }
575 epipe->tail.td = std;
576 tdphys = std->physaddr;
577 fmt = EHCI_ED_FORMAT_GEN | EHCI_ED_DIR_TD;
578 }
579 sed->ed.ed_flags = htole32(
580 EHCI_ED_SET_FA(addr) |
581 EHCI_ED_SET_EN(ed->bEndpointAddress) |
582 (dev->lowspeed ? EHCI_ED_SPEED : 0) | fmt |
583 EHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
584 sed->ed.ed_headp = sed->ed.ed_tailp = htole32(tdphys);
585
586 switch (xfertype) {
587 case UE_CONTROL:
588 pipe->methods = &ehci_device_ctrl_methods;
589 err = usb_allocmem(&sc->sc_bus,
590 sizeof(usb_device_request_t),
591 0, &epipe->u.ctl.reqdma);
592 if (err)
593 goto bad;
594 s = splusb();
595 ehci_add_ed(sed, sc->sc_ctrl_head);
596 splx(s);
597 break;
598 case UE_INTERRUPT:
599 pipe->methods = &ehci_device_intr_methods;
600 ival = pipe->interval;
601 if (ival == USBD_DEFAULT_INTERVAL)
602 ival = ed->bInterval;
603 return (ehci_device_setintr(sc, epipe, ival));
604 case UE_ISOCHRONOUS:
605 pipe->methods = &ehci_device_isoc_methods;
606 return (ehci_setup_isoc(pipe));
607 case UE_BULK:
608 pipe->methods = &ehci_device_bulk_methods;
609 s = splusb();
610 ehci_add_ed(sed, sc->sc_bulk_head);
611 splx(s);
612 break;
613 }
614 #else
615 return (USBD_IOERROR);
616 #endif
617 }
618 return (USBD_NORMAL_COMPLETION);
619
620 #if 0
621 bad:
622 if (std != NULL)
623 ehci_free_std(sc, std);
624 bad1:
625 if (sed != NULL)
626 ehci_free_sed(sc, sed);
627 bad0:
628 return (USBD_NOMEM);
629 #endif
630 }
631
632 /***********/
633
634 /*
635 * Data structures and routines to emulate the root hub.
636 */
637 Static usb_device_descriptor_t ehci_devd = {
638 USB_DEVICE_DESCRIPTOR_SIZE,
639 UDESC_DEVICE, /* type */
640 {0x00, 0x02}, /* USB version */
641 UDCLASS_HUB, /* class */
642 UDSUBCLASS_HUB, /* subclass */
643 0, /* protocol */
644 64, /* max packet */
645 {0},{0},{0x00,0x01}, /* device id */
646 1,2,0, /* string indicies */
647 1 /* # of configurations */
648 };
649
650 Static usb_config_descriptor_t ehci_confd = {
651 USB_CONFIG_DESCRIPTOR_SIZE,
652 UDESC_CONFIG,
653 {USB_CONFIG_DESCRIPTOR_SIZE +
654 USB_INTERFACE_DESCRIPTOR_SIZE +
655 USB_ENDPOINT_DESCRIPTOR_SIZE},
656 1,
657 1,
658 0,
659 UC_SELF_POWERED,
660 0 /* max power */
661 };
662
663 Static usb_interface_descriptor_t ehci_ifcd = {
664 USB_INTERFACE_DESCRIPTOR_SIZE,
665 UDESC_INTERFACE,
666 0,
667 0,
668 1,
669 UICLASS_HUB,
670 UISUBCLASS_HUB,
671 0,
672 0
673 };
674
675 Static usb_endpoint_descriptor_t ehci_endpd = {
676 USB_ENDPOINT_DESCRIPTOR_SIZE,
677 UDESC_ENDPOINT,
678 UE_DIR_IN | EHCI_INTR_ENDPT,
679 UE_INTERRUPT,
680 {8, 0}, /* max packet */
681 255
682 };
683
684 Static usb_hub_descriptor_t ehci_hubd = {
685 USB_HUB_DESCRIPTOR_SIZE,
686 UDESC_HUB,
687 0,
688 {0,0},
689 0,
690 0,
691 {0},
692 };
693
694 Static int
695 ehci_str(p, l, s)
696 usb_string_descriptor_t *p;
697 int l;
698 char *s;
699 {
700 int i;
701
702 if (l == 0)
703 return (0);
704 p->bLength = 2 * strlen(s) + 2;
705 if (l == 1)
706 return (1);
707 p->bDescriptorType = UDESC_STRING;
708 l -= 2;
709 for (i = 0; s[i] && l > 1; i++, l -= 2)
710 USETW2(p->bString[i], 0, s[i]);
711 return (2*i+2);
712 }
713
714 /*
715 * Simulate a hardware hub by handling all the necessary requests.
716 */
717 Static usbd_status
718 ehci_root_ctrl_transfer(usbd_xfer_handle xfer)
719 {
720 usbd_status err;
721
722 /* Insert last in queue. */
723 err = usb_insert_transfer(xfer);
724 if (err)
725 return (err);
726
727 /* Pipe isn't running, start first */
728 return (ehci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
729 }
730
731 Static usbd_status
732 ehci_root_ctrl_start(usbd_xfer_handle xfer)
733 {
734 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
735 usb_device_request_t *req;
736 void *buf = NULL;
737 int port, i;
738 int s, len, value, index, l, totlen = 0;
739 usb_port_status_t ps;
740 usb_hub_descriptor_t hubd;
741 usbd_status err;
742 u_int32_t v;
743
744 if (sc->sc_dying)
745 return (USBD_IOERROR);
746
747 #ifdef DIAGNOSTIC
748 if (!(xfer->rqflags & URQ_REQUEST))
749 /* XXX panic */
750 return (USBD_INVAL);
751 #endif
752 req = &xfer->request;
753
754 DPRINTFN(4,("ehci_root_ctrl_control type=0x%02x request=%02x\n",
755 req->bmRequestType, req->bRequest));
756
757 len = UGETW(req->wLength);
758 value = UGETW(req->wValue);
759 index = UGETW(req->wIndex);
760
761 if (len != 0)
762 buf = KERNADDR(&xfer->dmabuf);
763
764 #define C(x,y) ((x) | ((y) << 8))
765 switch(C(req->bRequest, req->bmRequestType)) {
766 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
767 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
768 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
769 /*
770 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
771 * for the integrated root hub.
772 */
773 break;
774 case C(UR_GET_CONFIG, UT_READ_DEVICE):
775 if (len > 0) {
776 *(u_int8_t *)buf = sc->sc_conf;
777 totlen = 1;
778 }
779 break;
780 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
781 DPRINTFN(8,("ehci_root_ctrl_control wValue=0x%04x\n", value));
782 switch(value >> 8) {
783 case UDESC_DEVICE:
784 if ((value & 0xff) != 0) {
785 err = USBD_IOERROR;
786 goto ret;
787 }
788 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
789 USETW(ehci_devd.idVendor, sc->sc_id_vendor);
790 memcpy(buf, &ehci_devd, l);
791 break;
792 case UDESC_CONFIG:
793 if ((value & 0xff) != 0) {
794 err = USBD_IOERROR;
795 goto ret;
796 }
797 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
798 memcpy(buf, &ehci_confd, l);
799 buf = (char *)buf + l;
800 len -= l;
801 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
802 totlen += l;
803 memcpy(buf, &ehci_ifcd, l);
804 buf = (char *)buf + l;
805 len -= l;
806 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
807 totlen += l;
808 memcpy(buf, &ehci_endpd, l);
809 break;
810 case UDESC_STRING:
811 if (len == 0)
812 break;
813 *(u_int8_t *)buf = 0;
814 totlen = 1;
815 switch (value & 0xff) {
816 case 1: /* Vendor */
817 totlen = ehci_str(buf, len, sc->sc_vendor);
818 break;
819 case 2: /* Product */
820 totlen = ehci_str(buf, len, "EHCI root hub");
821 break;
822 }
823 break;
824 default:
825 err = USBD_IOERROR;
826 goto ret;
827 }
828 break;
829 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
830 if (len > 0) {
831 *(u_int8_t *)buf = 0;
832 totlen = 1;
833 }
834 break;
835 case C(UR_GET_STATUS, UT_READ_DEVICE):
836 if (len > 1) {
837 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
838 totlen = 2;
839 }
840 break;
841 case C(UR_GET_STATUS, UT_READ_INTERFACE):
842 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
843 if (len > 1) {
844 USETW(((usb_status_t *)buf)->wStatus, 0);
845 totlen = 2;
846 }
847 break;
848 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
849 if (value >= USB_MAX_DEVICES) {
850 err = USBD_IOERROR;
851 goto ret;
852 }
853 sc->sc_addr = value;
854 break;
855 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
856 if (value != 0 && value != 1) {
857 err = USBD_IOERROR;
858 goto ret;
859 }
860 sc->sc_conf = value;
861 break;
862 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
863 break;
864 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
865 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
866 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
867 err = USBD_IOERROR;
868 goto ret;
869 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
870 break;
871 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
872 break;
873 /* Hub requests */
874 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
875 break;
876 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
877 DPRINTFN(8, ("ehci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
878 "port=%d feature=%d\n",
879 index, value));
880 if (index < 1 || index > sc->sc_noport) {
881 err = USBD_IOERROR;
882 goto ret;
883 }
884 port = EHCI_PORTSC(index);
885 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
886 switch(value) {
887 case UHF_PORT_ENABLE:
888 EOWRITE4(sc, port, v &~ EHCI_PS_PE);
889 break;
890 case UHF_PORT_SUSPEND:
891 EOWRITE4(sc, port, v &~ EHCI_PS_SUSP);
892 break;
893 case UHF_PORT_POWER:
894 EOWRITE4(sc, port, v &~ EHCI_PS_PP);
895 break;
896 case UHF_C_PORT_CONNECTION:
897 EOWRITE4(sc, port, v | EHCI_PS_CSC);
898 break;
899 case UHF_C_PORT_ENABLE:
900 EOWRITE4(sc, port, v | EHCI_PS_PEC);
901 break;
902 case UHF_C_PORT_SUSPEND:
903 /* how? */
904 break;
905 case UHF_C_PORT_OVER_CURRENT:
906 EOWRITE4(sc, port, v | EHCI_PS_OCC);
907 break;
908 case UHF_C_PORT_RESET:
909 /* how? */
910 break;
911 default:
912 err = USBD_IOERROR;
913 goto ret;
914 }
915 #if 0
916 switch(value) {
917 case UHF_C_PORT_CONNECTION:
918 case UHF_C_PORT_ENABLE:
919 case UHF_C_PORT_SUSPEND:
920 case UHF_C_PORT_OVER_CURRENT:
921 case UHF_C_PORT_RESET:
922 /* Enable RHSC interrupt if condition is cleared. */
923 if ((OREAD4(sc, port) >> 16) == 0)
924 ehci_rhsc_able(sc, 1);
925 break;
926 default:
927 break;
928 }
929 #endif
930 break;
931 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
932 if (value != 0) {
933 err = USBD_IOERROR;
934 goto ret;
935 }
936 hubd = ehci_hubd;
937 hubd.bNbrPorts = sc->sc_noport;
938 v = EOREAD4(sc, EHCI_HCSPARAMS);
939 USETW(hubd.wHubCharacteristics,
940 EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH);
941 hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */
942 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
943 hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
944 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
945 l = min(len, hubd.bDescLength);
946 totlen = l;
947 memcpy(buf, &hubd, l);
948 break;
949 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
950 if (len != 4) {
951 err = USBD_IOERROR;
952 goto ret;
953 }
954 memset(buf, 0, len); /* ? XXX */
955 totlen = len;
956 break;
957 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
958 DPRINTFN(8,("ehci_root_ctrl_transfer: get port status i=%d\n",
959 index));
960 if (index < 1 || index > sc->sc_noport) {
961 err = USBD_IOERROR;
962 goto ret;
963 }
964 if (len != 4) {
965 err = USBD_IOERROR;
966 goto ret;
967 }
968 v = EOREAD4(sc, EHCI_PORTSC(index));
969 DPRINTFN(8,("ehci_root_ctrl_transfer: port status=0x%04x\n",
970 v));
971 i = 0;
972 if (v & EHCI_PS_CS) i |= UPS_CURRENT_CONNECT_STATUS;
973 if (v & EHCI_PS_PE) i |= UPS_PORT_ENABLED;
974 if (v & EHCI_PS_SUSP) i |= UPS_SUSPEND;
975 if (v & EHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR;
976 if (v & EHCI_PS_PR) i |= UPS_RESET;
977 if (v & EHCI_PS_PP) i |= UPS_PORT_POWER;
978 USETW(ps.wPortStatus, i);
979 i = 0;
980 if (v & EHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS;
981 if (v & EHCI_PS_PEC) i |= UPS_C_PORT_ENABLED;
982 if (v & EHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR;
983 USETW(ps.wPortChange, i);
984 l = min(len, sizeof ps);
985 memcpy(buf, &ps, l);
986 totlen = l;
987 break;
988 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
989 err = USBD_IOERROR;
990 goto ret;
991 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
992 break;
993 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
994 if (index < 1 || index > sc->sc_noport) {
995 err = USBD_IOERROR;
996 goto ret;
997 }
998 port = EHCI_PORTSC(index);
999 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
1000 switch(value) {
1001 case UHF_PORT_ENABLE:
1002 EOWRITE4(sc, port, v | EHCI_PS_PE);
1003 break;
1004 case UHF_PORT_SUSPEND:
1005 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
1006 break;
1007 case UHF_PORT_RESET:
1008 DPRINTFN(5,("ehci_root_ctrl_transfer: reset port %d\n",
1009 index));
1010 EOWRITE4(sc, port, v | EHCI_PS_PR);
1011 for (i = 0; i < 10; i++) {
1012 usb_delay_ms(&sc->sc_bus, 10); /* XXX */
1013 if ((EOREAD4(sc, port) & EHCI_PS_PR) == 0)
1014 break;
1015 }
1016 DPRINTFN(8,("ehci port %d reset, status = 0x%04x\n",
1017 index, EOREAD4(sc, port)));
1018 break;
1019 case UHF_PORT_POWER:
1020 DPRINTFN(2,("ehci_root_ctrl_transfer: set port power "
1021 "%d\n", index));
1022 EOWRITE4(sc, port, v | EHCI_PS_PP);
1023 break;
1024 default:
1025 err = USBD_IOERROR;
1026 goto ret;
1027 }
1028 break;
1029 default:
1030 err = USBD_IOERROR;
1031 goto ret;
1032 }
1033 xfer->actlen = totlen;
1034 err = USBD_NORMAL_COMPLETION;
1035 ret:
1036 xfer->status = err;
1037 s = splusb();
1038 usb_transfer_complete(xfer);
1039 splx(s);
1040 return (USBD_IN_PROGRESS);
1041 }
1042
1043 /* Abort a root control request. */
1044 Static void
1045 ehci_root_ctrl_abort(usbd_xfer_handle xfer)
1046 {
1047 /* Nothing to do, all transfers are synchronous. */
1048 }
1049
1050 /* Close the root pipe. */
1051 Static void
1052 ehci_root_ctrl_close(usbd_pipe_handle pipe)
1053 {
1054 DPRINTF(("ehci_root_ctrl_close\n"));
1055 /* Nothing to do. */
1056 }
1057
1058 void
1059 ehci_root_intr_done(usbd_xfer_handle xfer)
1060 {
1061 xfer->hcpriv = NULL;
1062 }
1063
1064 Static usbd_status
1065 ehci_root_intr_transfer(usbd_xfer_handle xfer)
1066 {
1067 usbd_status err;
1068
1069 /* Insert last in queue. */
1070 err = usb_insert_transfer(xfer);
1071 if (err)
1072 return (err);
1073
1074 /* Pipe isn't running, start first */
1075 return (ehci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1076 }
1077
1078 Static usbd_status
1079 ehci_root_intr_start(usbd_xfer_handle xfer)
1080 {
1081 usbd_pipe_handle pipe = xfer->pipe;
1082 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
1083
1084 if (sc->sc_dying)
1085 return (USBD_IOERROR);
1086
1087 sc->sc_intrxfer = xfer;
1088
1089 return (USBD_IN_PROGRESS);
1090 }
1091
1092 /* Abort a root interrupt request. */
1093 Static void
1094 ehci_root_intr_abort(usbd_xfer_handle xfer)
1095 {
1096 int s;
1097
1098 if (xfer->pipe->intrxfer == xfer) {
1099 DPRINTF(("ehci_root_intr_abort: remove\n"));
1100 xfer->pipe->intrxfer = NULL;
1101 }
1102 xfer->status = USBD_CANCELLED;
1103 s = splusb();
1104 usb_transfer_complete(xfer);
1105 splx(s);
1106 }
1107
1108 /* Close the root pipe. */
1109 Static void
1110 ehci_root_intr_close(usbd_pipe_handle pipe)
1111 {
1112 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
1113
1114 DPRINTF(("ehci_root_intr_close\n"));
1115
1116 sc->sc_intrxfer = NULL;
1117 }
1118
1119 void
1120 ehci_root_ctrl_done(usbd_xfer_handle xfer)
1121 {
1122 xfer->hcpriv = NULL;
1123 }
1124
1125 /************************/
1126
1127 Static usbd_status ehci_device_ctrl_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
1128 Static usbd_status ehci_device_ctrl_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
1129 Static void ehci_device_ctrl_abort(usbd_xfer_handle xfer) { }
1130 Static void ehci_device_ctrl_close(usbd_pipe_handle pipe) { }
1131 Static void ehci_device_ctrl_done(usbd_xfer_handle xfer) { }
1132
1133 Static usbd_status ehci_device_bulk_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
1134 Static usbd_status ehci_device_bulk_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
1135 Static void ehci_device_bulk_abort(usbd_xfer_handle xfer) { }
1136 Static void ehci_device_bulk_close(usbd_pipe_handle pipe) { }
1137 Static void ehci_device_bulk_done(usbd_xfer_handle xfer) { }
1138
1139 Static usbd_status ehci_device_intr_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
1140 Static usbd_status ehci_device_intr_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
1141 Static void ehci_device_intr_abort(usbd_xfer_handle xfer) { }
1142 Static void ehci_device_intr_close(usbd_pipe_handle pipe) { }
1143 Static void ehci_device_intr_done(usbd_xfer_handle xfer) { }
1144
1145 Static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; }
1146 Static usbd_status ehci_device_isoc_start(usbd_xfer_handle xfer) { return USBD_IOERROR; }
1147 Static void ehci_device_isoc_abort(usbd_xfer_handle xfer) { }
1148 Static void ehci_device_isoc_close(usbd_pipe_handle pipe) { }
1149 Static void ehci_device_isoc_done(usbd_xfer_handle xfer) { }
1150