1 /* $NetBSD: ehci.c,v 1.164 2009/11/14 17:06:12 uebayasi Exp $ */
4 * Copyright (c) 2004-2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net), Charles M. Hannum and
9 * Jeremy Morse (jeremy.morse@gmail.com).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
36 * The EHCI 1.0 spec can be found at
37 * http://www.intel.com/technology/usb/spec.htm
38 * and the USB 2.0 spec at
39 * http://www.usb.org/developers/docs/
45 * 1) hold off explorations by companion controllers until ehci has started.
47 * 2) The hub driver needs to handle and schedule the transaction translator,
48 * to assign place in frame where different devices get to go. See chapter
49 * on hubs in USB 2.0 for details.
51 * 3) Command failures are not recovered correctly.
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.164 2009/11/14 17:06:12 uebayasi Exp $");
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/malloc.h>
64 #include <sys/device.h>
65 #include <sys/select.h>
67 #include <sys/queue.h>
68 #include <sys/mutex.h>
71 #include <machine/endian.h>
73 #include <dev/usb/usb.h>
74 #include <dev/usb/usbdi.h>
75 #include <dev/usb/usbdivar.h>
76 #include <dev/usb/usb_mem.h>
77 #include <dev/usb/usb_quirks.h>
79 #include <dev/usb/ehcireg.h>
80 #include <dev/usb/ehcivar.h>
81 #include <dev/usb/usbroothub_subr.h>
84 #define DPRINTF(x) do { if (ehcidebug) printf x; } while(0)
85 #define DPRINTFN(n,x) do { if (ehcidebug>(n)) printf x; } while (0)
93 struct usbd_pipe pipe
;
99 /* ehci_soft_itd_t *itd; */
123 Static usbd_status
ehci_open(usbd_pipe_handle
);
124 Static
void ehci_poll(struct usbd_bus
*);
125 Static
void ehci_softintr(void *);
126 Static
int ehci_intr1(ehci_softc_t
*);
127 Static
void ehci_waitintr(ehci_softc_t
*, usbd_xfer_handle
);
128 Static
void ehci_check_intr(ehci_softc_t
*, struct ehci_xfer
*);
129 Static
void ehci_check_qh_intr(ehci_softc_t
*, struct ehci_xfer
*);
130 Static
void ehci_check_itd_intr(ehci_softc_t
*, struct ehci_xfer
*);
131 Static
void ehci_idone(struct ehci_xfer
*);
132 Static
void ehci_timeout(void *);
133 Static
void ehci_timeout_task(void *);
134 Static
void ehci_intrlist_timeout(void *);
136 Static usbd_status
ehci_allocm(struct usbd_bus
*, usb_dma_t
*, u_int32_t
);
137 Static
void ehci_freem(struct usbd_bus
*, usb_dma_t
*);
139 Static usbd_xfer_handle
ehci_allocx(struct usbd_bus
*);
140 Static
void ehci_freex(struct usbd_bus
*, usbd_xfer_handle
);
142 Static usbd_status
ehci_root_ctrl_transfer(usbd_xfer_handle
);
143 Static usbd_status
ehci_root_ctrl_start(usbd_xfer_handle
);
144 Static
void ehci_root_ctrl_abort(usbd_xfer_handle
);
145 Static
void ehci_root_ctrl_close(usbd_pipe_handle
);
146 Static
void ehci_root_ctrl_done(usbd_xfer_handle
);
148 Static usbd_status
ehci_root_intr_transfer(usbd_xfer_handle
);
149 Static usbd_status
ehci_root_intr_start(usbd_xfer_handle
);
150 Static
void ehci_root_intr_abort(usbd_xfer_handle
);
151 Static
void ehci_root_intr_close(usbd_pipe_handle
);
152 Static
void ehci_root_intr_done(usbd_xfer_handle
);
154 Static usbd_status
ehci_device_ctrl_transfer(usbd_xfer_handle
);
155 Static usbd_status
ehci_device_ctrl_start(usbd_xfer_handle
);
156 Static
void ehci_device_ctrl_abort(usbd_xfer_handle
);
157 Static
void ehci_device_ctrl_close(usbd_pipe_handle
);
158 Static
void ehci_device_ctrl_done(usbd_xfer_handle
);
160 Static usbd_status
ehci_device_bulk_transfer(usbd_xfer_handle
);
161 Static usbd_status
ehci_device_bulk_start(usbd_xfer_handle
);
162 Static
void ehci_device_bulk_abort(usbd_xfer_handle
);
163 Static
void ehci_device_bulk_close(usbd_pipe_handle
);
164 Static
void ehci_device_bulk_done(usbd_xfer_handle
);
166 Static usbd_status
ehci_device_intr_transfer(usbd_xfer_handle
);
167 Static usbd_status
ehci_device_intr_start(usbd_xfer_handle
);
168 Static
void ehci_device_intr_abort(usbd_xfer_handle
);
169 Static
void ehci_device_intr_close(usbd_pipe_handle
);
170 Static
void ehci_device_intr_done(usbd_xfer_handle
);
172 Static usbd_status
ehci_device_isoc_transfer(usbd_xfer_handle
);
173 Static usbd_status
ehci_device_isoc_start(usbd_xfer_handle
);
174 Static
void ehci_device_isoc_abort(usbd_xfer_handle
);
175 Static
void ehci_device_isoc_close(usbd_pipe_handle
);
176 Static
void ehci_device_isoc_done(usbd_xfer_handle
);
178 Static
void ehci_device_clear_toggle(usbd_pipe_handle pipe
);
179 Static
void ehci_noop(usbd_pipe_handle pipe
);
181 Static
void ehci_pcd(ehci_softc_t
*, usbd_xfer_handle
);
182 Static
void ehci_disown(ehci_softc_t
*, int, int);
184 Static ehci_soft_qh_t
*ehci_alloc_sqh(ehci_softc_t
*);
185 Static
void ehci_free_sqh(ehci_softc_t
*, ehci_soft_qh_t
*);
187 Static ehci_soft_qtd_t
*ehci_alloc_sqtd(ehci_softc_t
*);
188 Static
void ehci_free_sqtd(ehci_softc_t
*, ehci_soft_qtd_t
*);
189 Static usbd_status
ehci_alloc_sqtd_chain(struct ehci_pipe
*,
190 ehci_softc_t
*, int, int, usbd_xfer_handle
,
191 ehci_soft_qtd_t
**, ehci_soft_qtd_t
**);
192 Static
void ehci_free_sqtd_chain(ehci_softc_t
*, ehci_soft_qtd_t
*,
195 Static ehci_soft_itd_t
*ehci_alloc_itd(ehci_softc_t
*sc
);
196 Static
void ehci_free_itd(ehci_softc_t
*sc
, ehci_soft_itd_t
*itd
);
197 Static
void ehci_rem_free_itd_chain(ehci_softc_t
*sc
,
198 struct ehci_xfer
*exfer
);
199 Static
void ehci_abort_isoc_xfer(usbd_xfer_handle xfer
,
202 Static usbd_status
ehci_device_request(usbd_xfer_handle xfer
);
204 Static usbd_status
ehci_device_setintr(ehci_softc_t
*, ehci_soft_qh_t
*,
207 Static
void ehci_add_qh(ehci_soft_qh_t
*, ehci_soft_qh_t
*);
208 Static
void ehci_rem_qh(ehci_softc_t
*, ehci_soft_qh_t
*,
210 Static
void ehci_set_qh_qtd(ehci_soft_qh_t
*, ehci_soft_qtd_t
*);
211 Static
void ehci_sync_hc(ehci_softc_t
*);
213 Static
void ehci_close_pipe(usbd_pipe_handle
, ehci_soft_qh_t
*);
214 Static
void ehci_abort_xfer(usbd_xfer_handle
, usbd_status
);
217 Static
void ehci_dump_regs(ehci_softc_t
*);
218 void ehci_dump(void);
219 Static ehci_softc_t
*theehci
;
220 Static
void ehci_dump_link(ehci_link_t
, int);
221 Static
void ehci_dump_sqtds(ehci_soft_qtd_t
*);
222 Static
void ehci_dump_sqtd(ehci_soft_qtd_t
*);
223 Static
void ehci_dump_qtd(ehci_qtd_t
*);
224 Static
void ehci_dump_sqh(ehci_soft_qh_t
*);
226 Static
void ehci_dump_sitd(struct ehci_soft_itd
*itd
);
227 Static
void ehci_dump_itd(struct ehci_soft_itd
*);
230 Static
void ehci_dump_exfer(struct ehci_xfer
*);
234 #define EHCI_NULL htole32(EHCI_LINK_TERMINATE)
236 #define EHCI_INTR_ENDPT 1
238 #define ehci_add_intr_list(sc, ex) \
239 TAILQ_INSERT_TAIL(&(sc)->sc_intrhead, (ex), inext);
240 #define ehci_del_intr_list(sc, ex) \
242 TAILQ_REMOVE(&sc->sc_intrhead, (ex), inext); \
243 (ex)->inext.tqe_prev = NULL; \
245 #define ehci_active_intr_list(ex) ((ex)->inext.tqe_prev != NULL)
247 Static
const struct usbd_bus_methods ehci_bus_methods
= {
257 Static
const struct usbd_pipe_methods ehci_root_ctrl_methods
= {
258 ehci_root_ctrl_transfer
,
259 ehci_root_ctrl_start
,
260 ehci_root_ctrl_abort
,
261 ehci_root_ctrl_close
,
266 Static
const struct usbd_pipe_methods ehci_root_intr_methods
= {
267 ehci_root_intr_transfer
,
268 ehci_root_intr_start
,
269 ehci_root_intr_abort
,
270 ehci_root_intr_close
,
275 Static
const struct usbd_pipe_methods ehci_device_ctrl_methods
= {
276 ehci_device_ctrl_transfer
,
277 ehci_device_ctrl_start
,
278 ehci_device_ctrl_abort
,
279 ehci_device_ctrl_close
,
281 ehci_device_ctrl_done
,
284 Static
const struct usbd_pipe_methods ehci_device_intr_methods
= {
285 ehci_device_intr_transfer
,
286 ehci_device_intr_start
,
287 ehci_device_intr_abort
,
288 ehci_device_intr_close
,
289 ehci_device_clear_toggle
,
290 ehci_device_intr_done
,
293 Static
const struct usbd_pipe_methods ehci_device_bulk_methods
= {
294 ehci_device_bulk_transfer
,
295 ehci_device_bulk_start
,
296 ehci_device_bulk_abort
,
297 ehci_device_bulk_close
,
298 ehci_device_clear_toggle
,
299 ehci_device_bulk_done
,
302 Static
const struct usbd_pipe_methods ehci_device_isoc_methods
= {
303 ehci_device_isoc_transfer
,
304 ehci_device_isoc_start
,
305 ehci_device_isoc_abort
,
306 ehci_device_isoc_close
,
308 ehci_device_isoc_done
,
311 static const uint8_t revbits
[EHCI_MAX_POLLRATE
] = {
312 0x00,0x40,0x20,0x60,0x10,0x50,0x30,0x70,0x08,0x48,0x28,0x68,0x18,0x58,0x38,0x78,
313 0x04,0x44,0x24,0x64,0x14,0x54,0x34,0x74,0x0c,0x4c,0x2c,0x6c,0x1c,0x5c,0x3c,0x7c,
314 0x02,0x42,0x22,0x62,0x12,0x52,0x32,0x72,0x0a,0x4a,0x2a,0x6a,0x1a,0x5a,0x3a,0x7a,
315 0x06,0x46,0x26,0x66,0x16,0x56,0x36,0x76,0x0e,0x4e,0x2e,0x6e,0x1e,0x5e,0x3e,0x7e,
316 0x01,0x41,0x21,0x61,0x11,0x51,0x31,0x71,0x09,0x49,0x29,0x69,0x19,0x59,0x39,0x79,
317 0x05,0x45,0x25,0x65,0x15,0x55,0x35,0x75,0x0d,0x4d,0x2d,0x6d,0x1d,0x5d,0x3d,0x7d,
318 0x03,0x43,0x23,0x63,0x13,0x53,0x33,0x73,0x0b,0x4b,0x2b,0x6b,0x1b,0x5b,0x3b,0x7b,
319 0x07,0x47,0x27,0x67,0x17,0x57,0x37,0x77,0x0f,0x4f,0x2f,0x6f,0x1f,0x5f,0x3f,0x7f,
323 ehci_init(ehci_softc_t
*sc
)
325 u_int32_t vers
, sparams
, cparams
, hcr
;
331 DPRINTF(("ehci_init: start\n"));
336 sc
->sc_offs
= EREAD1(sc
, EHCI_CAPLENGTH
);
338 vers
= EREAD2(sc
, EHCI_HCIVERSION
);
339 aprint_verbose("%s: EHCI version %x.%x\n", device_xname(sc
->sc_dev
),
340 vers
>> 8, vers
& 0xff);
342 sparams
= EREAD4(sc
, EHCI_HCSPARAMS
);
343 DPRINTF(("ehci_init: sparams=0x%x\n", sparams
));
344 sc
->sc_npcomp
= EHCI_HCS_N_PCC(sparams
);
345 ncomp
= EHCI_HCS_N_CC(sparams
);
346 if (ncomp
!= sc
->sc_ncomp
) {
347 aprint_verbose("%s: wrong number of companions (%d != %d)\n",
348 device_xname(sc
->sc_dev
), ncomp
, sc
->sc_ncomp
);
349 #if NOHCI == 0 || NUHCI == 0
350 aprint_error("%s: ohci or uhci probably not configured\n",
351 device_xname(sc
->sc_dev
));
353 if (ncomp
< sc
->sc_ncomp
)
354 sc
->sc_ncomp
= ncomp
;
356 if (sc
->sc_ncomp
> 0) {
357 aprint_normal("%s: companion controller%s, %d port%s each:",
358 device_xname(sc
->sc_dev
), sc
->sc_ncomp
!=1 ? "s" : "",
359 EHCI_HCS_N_PCC(sparams
),
360 EHCI_HCS_N_PCC(sparams
)!=1 ? "s" : "");
361 for (i
= 0; i
< sc
->sc_ncomp
; i
++)
362 aprint_normal(" %s", device_xname(sc
->sc_comps
[i
]));
365 sc
->sc_noport
= EHCI_HCS_N_PORTS(sparams
);
366 cparams
= EREAD4(sc
, EHCI_HCCPARAMS
);
367 DPRINTF(("ehci_init: cparams=0x%x\n", cparams
));
368 sc
->sc_hasppc
= EHCI_HCS_PPC(sparams
);
370 if (EHCI_HCC_64BIT(cparams
)) {
371 /* MUST clear segment register if 64 bit capable. */
372 EWRITE4(sc
, EHCI_CTRLDSSEGMENT
, 0);
375 sc
->sc_bus
.usbrev
= USBREV_2_0
;
377 usb_setup_reserve(sc
->sc_dev
, &sc
->sc_dma_reserve
, sc
->sc_bus
.dmatag
,
380 /* Reset the controller */
381 DPRINTF(("%s: resetting\n", device_xname(sc
->sc_dev
)));
382 EOWRITE4(sc
, EHCI_USBCMD
, 0); /* Halt controller */
383 usb_delay_ms(&sc
->sc_bus
, 1);
384 EOWRITE4(sc
, EHCI_USBCMD
, EHCI_CMD_HCRESET
);
385 for (i
= 0; i
< 100; i
++) {
386 usb_delay_ms(&sc
->sc_bus
, 1);
387 hcr
= EOREAD4(sc
, EHCI_USBCMD
) & EHCI_CMD_HCRESET
;
392 aprint_error("%s: reset timeout\n", device_xname(sc
->sc_dev
));
393 return (USBD_IOERROR
);
396 /* XXX need proper intr scheduling */
399 /* frame list size at default, read back what we got and use that */
400 switch (EHCI_CMD_FLS(EOREAD4(sc
, EHCI_USBCMD
))) {
401 case 0: sc
->sc_flsize
= 1024; break;
402 case 1: sc
->sc_flsize
= 512; break;
403 case 2: sc
->sc_flsize
= 256; break;
404 case 3: return (USBD_IOERROR
);
406 err
= usb_allocmem(&sc
->sc_bus
, sc
->sc_flsize
* sizeof(ehci_link_t
),
407 EHCI_FLALIGN_ALIGN
, &sc
->sc_fldma
);
410 DPRINTF(("%s: flsize=%d\n", device_xname(sc
->sc_dev
),sc
->sc_flsize
));
411 sc
->sc_flist
= KERNADDR(&sc
->sc_fldma
, 0);
413 for (i
= 0; i
< sc
->sc_flsize
; i
++) {
414 sc
->sc_flist
[i
] = EHCI_NULL
;
417 EOWRITE4(sc
, EHCI_PERIODICLISTBASE
, DMAADDR(&sc
->sc_fldma
, 0));
419 sc
->sc_softitds
= malloc(sc
->sc_flsize
* sizeof(ehci_soft_itd_t
*),
420 M_USB
, M_NOWAIT
| M_ZERO
);
421 if (sc
->sc_softitds
== NULL
)
423 LIST_INIT(&sc
->sc_freeitds
);
424 TAILQ_INIT(&sc
->sc_intrhead
);
425 mutex_init(&sc
->sc_intrhead_lock
, MUTEX_DEFAULT
, IPL_USB
);
427 /* Set up the bus struct. */
428 sc
->sc_bus
.methods
= &ehci_bus_methods
;
429 sc
->sc_bus
.pipe_size
= sizeof(struct ehci_pipe
);
431 sc
->sc_eintrs
= EHCI_NORMAL_INTRS
;
434 * Allocate the interrupt dummy QHs. These are arranged to give poll
435 * intervals that are powers of 2 times 1ms.
437 for (i
= 0; i
< EHCI_INTRQHS
; i
++) {
438 sqh
= ehci_alloc_sqh(sc
);
443 sc
->sc_islots
[i
].sqh
= sqh
;
445 for (i
= 0; i
< EHCI_INTRQHS
; i
++) {
446 sqh
= sc
->sc_islots
[i
].sqh
;
448 /* The last (1ms) QH terminates. */
449 sqh
->qh
.qh_link
= EHCI_NULL
;
452 /* Otherwise the next QH has half the poll interval */
453 sqh
->next
= sc
->sc_islots
[(i
+ 1) / 2 - 1].sqh
;
454 sqh
->qh
.qh_link
= htole32(sqh
->next
->physaddr
|
457 sqh
->qh
.qh_endp
= htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH
));
458 sqh
->qh
.qh_curqtd
= EHCI_NULL
;
460 sqh
->qh
.qh_qtd
.qtd_next
= EHCI_NULL
;
461 sqh
->qh
.qh_qtd
.qtd_altnext
= EHCI_NULL
;
462 sqh
->qh
.qh_qtd
.qtd_status
= htole32(EHCI_QTD_HALTED
);
464 usb_syncmem(&sqh
->dma
, sqh
->offs
, sizeof(sqh
->qh
),
465 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
467 /* Point the frame list at the last level (128ms). */
468 for (i
= 0; i
< sc
->sc_flsize
; i
++) {
471 j
= (i
& ~(EHCI_MAX_POLLRATE
-1)) |
472 revbits
[i
& (EHCI_MAX_POLLRATE
-1)];
473 sc
->sc_flist
[j
] = htole32(EHCI_LINK_QH
|
474 sc
->sc_islots
[EHCI_IQHIDX(EHCI_IPOLLRATES
- 1,
477 usb_syncmem(&sc
->sc_fldma
, 0, sc
->sc_flsize
* sizeof(ehci_link_t
),
478 BUS_DMASYNC_PREWRITE
);
480 /* Allocate dummy QH that starts the async list. */
481 sqh
= ehci_alloc_sqh(sc
);
488 htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH
) | EHCI_QH_HRECL
);
490 htole32(sqh
->physaddr
| EHCI_LINK_QH
);
491 sqh
->qh
.qh_curqtd
= EHCI_NULL
;
493 /* Fill the overlay qTD */
494 sqh
->qh
.qh_qtd
.qtd_next
= EHCI_NULL
;
495 sqh
->qh
.qh_qtd
.qtd_altnext
= EHCI_NULL
;
496 sqh
->qh
.qh_qtd
.qtd_status
= htole32(EHCI_QTD_HALTED
);
498 usb_syncmem(&sqh
->dma
, sqh
->offs
, sizeof(sqh
->qh
),
499 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
506 /* Point to async list */
507 sc
->sc_async_head
= sqh
;
508 EOWRITE4(sc
, EHCI_ASYNCLISTADDR
, sqh
->physaddr
| EHCI_LINK_QH
);
510 callout_init(&(sc
->sc_tmo_intrlist
), 0);
512 mutex_init(&sc
->sc_doorbell_lock
, MUTEX_DEFAULT
, IPL_NONE
);
514 /* Turn on controller */
515 EOWRITE4(sc
, EHCI_USBCMD
,
516 EHCI_CMD_ITC_2
| /* 2 microframes interrupt delay */
517 (EOREAD4(sc
, EHCI_USBCMD
) & EHCI_CMD_FLS_M
) |
522 /* Take over port ownership */
523 EOWRITE4(sc
, EHCI_CONFIGFLAG
, EHCI_CONF_CF
);
525 for (i
= 0; i
< 100; i
++) {
526 usb_delay_ms(&sc
->sc_bus
, 1);
527 hcr
= EOREAD4(sc
, EHCI_USBSTS
) & EHCI_STS_HCH
;
532 aprint_error("%s: run timeout\n", device_xname(sc
->sc_dev
));
533 return (USBD_IOERROR
);
536 /* Enable interrupts */
537 DPRINTFN(1,("ehci_init: enabling\n"));
538 EOWRITE4(sc
, EHCI_USBINTR
, sc
->sc_eintrs
);
540 return (USBD_NORMAL_COMPLETION
);
544 ehci_free_sqh(sc
, sc
->sc_async_head
);
547 usb_freemem(&sc
->sc_bus
, &sc
->sc_fldma
);
554 ehci_softc_t
*sc
= v
;
556 if (sc
== NULL
|| sc
->sc_dying
|| !device_has_power(sc
->sc_dev
))
559 /* If we get an interrupt while polling, then just ignore it. */
560 if (sc
->sc_bus
.use_polling
) {
561 u_int32_t intrs
= EHCI_STS_INTRS(EOREAD4(sc
, EHCI_USBSTS
));
564 EOWRITE4(sc
, EHCI_USBSTS
, intrs
); /* Acknowledge */
566 DPRINTFN(16, ("ehci_intr: ignored interrupt while polling\n"));
571 return (ehci_intr1(sc
));
575 ehci_intr1(ehci_softc_t
*sc
)
577 u_int32_t intrs
, eintrs
;
579 DPRINTFN(20,("ehci_intr1: enter\n"));
581 /* In case the interrupt occurs before initialization has completed. */
584 printf("ehci_intr1: sc == NULL\n");
589 intrs
= EHCI_STS_INTRS(EOREAD4(sc
, EHCI_USBSTS
));
593 eintrs
= intrs
& sc
->sc_eintrs
;
594 DPRINTFN(7, ("ehci_intr1: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
595 sc
, (u_int
)intrs
, EOREAD4(sc
, EHCI_USBSTS
),
600 EOWRITE4(sc
, EHCI_USBSTS
, intrs
); /* Acknowledge */
601 sc
->sc_bus
.intr_context
++;
602 sc
->sc_bus
.no_intrs
++;
603 if (eintrs
& EHCI_STS_IAA
) {
604 DPRINTF(("ehci_intr1: door bell\n"));
605 wakeup(&sc
->sc_async_head
);
606 eintrs
&= ~EHCI_STS_IAA
;
608 if (eintrs
& (EHCI_STS_INT
| EHCI_STS_ERRINT
)) {
609 DPRINTFN(5,("ehci_intr1: %s %s\n",
610 eintrs
& EHCI_STS_INT
? "INT" : "",
611 eintrs
& EHCI_STS_ERRINT
? "ERRINT" : ""));
612 usb_schedsoftintr(&sc
->sc_bus
);
613 eintrs
&= ~(EHCI_STS_INT
| EHCI_STS_ERRINT
);
615 if (eintrs
& EHCI_STS_HSE
) {
616 printf("%s: unrecoverable error, controller halted\n",
617 device_xname(sc
->sc_dev
));
620 if (eintrs
& EHCI_STS_PCD
) {
621 ehci_pcd(sc
, sc
->sc_intrxfer
);
622 eintrs
&= ~EHCI_STS_PCD
;
625 sc
->sc_bus
.intr_context
--;
628 /* Block unprocessed interrupts. */
629 sc
->sc_eintrs
&= ~eintrs
;
630 EOWRITE4(sc
, EHCI_USBINTR
, sc
->sc_eintrs
);
631 printf("%s: blocking intrs 0x%x\n",
632 device_xname(sc
->sc_dev
), eintrs
);
640 ehci_pcd(ehci_softc_t
*sc
, usbd_xfer_handle xfer
)
642 usbd_pipe_handle pipe
;
647 /* Just ignore the change. */
653 p
= KERNADDR(&xfer
->dmabuf
, 0);
654 m
= min(sc
->sc_noport
, xfer
->length
* 8 - 1);
655 memset(p
, 0, xfer
->length
);
656 for (i
= 1; i
<= m
; i
++) {
657 /* Pick out CHANGE bits from the status reg. */
658 if (EOREAD4(sc
, EHCI_PORTSC(i
)) & EHCI_PS_CLEAR
)
659 p
[i
/8] |= 1 << (i
%8);
661 DPRINTF(("ehci_pcd: change=0x%02x\n", *p
));
662 xfer
->actlen
= xfer
->length
;
663 xfer
->status
= USBD_NORMAL_COMPLETION
;
665 usb_transfer_complete(xfer
);
669 ehci_softintr(void *v
)
671 struct usbd_bus
*bus
= v
;
672 ehci_softc_t
*sc
= bus
->hci_private
;
673 struct ehci_xfer
*ex
, *nextex
;
675 DPRINTFN(10,("%s: ehci_softintr (%d)\n", device_xname(sc
->sc_dev
),
676 sc
->sc_bus
.intr_context
));
678 sc
->sc_bus
.intr_context
++;
681 * The only explanation I can think of for why EHCI is as brain dead
682 * as UHCI interrupt-wise is that Intel was involved in both.
683 * An interrupt just tells us that something is done, we have no
684 * clue what, so we need to scan through all active transfers. :-(
686 for (ex
= TAILQ_FIRST(&sc
->sc_intrhead
); ex
; ex
= nextex
) {
687 nextex
= TAILQ_NEXT(ex
, inext
);
688 ehci_check_intr(sc
, ex
);
691 /* Schedule a callout to catch any dropped transactions. */
692 if ((sc
->sc_flags
& EHCIF_DROPPED_INTR_WORKAROUND
) &&
693 !TAILQ_EMPTY(&sc
->sc_intrhead
))
694 callout_reset(&(sc
->sc_tmo_intrlist
),
695 (hz
), (ehci_intrlist_timeout
), (sc
));
697 #ifdef USB_USE_SOFTINTR
698 if (sc
->sc_softwake
) {
700 wakeup(&sc
->sc_softwake
);
702 #endif /* USB_USE_SOFTINTR */
704 sc
->sc_bus
.intr_context
--;
707 /* Check for an interrupt. */
709 ehci_check_intr(ehci_softc_t
*sc
, struct ehci_xfer
*ex
)
713 DPRINTFN(/*15*/2, ("ehci_check_intr: ex=%p\n", ex
));
715 attr
= ex
->xfer
.pipe
->endpoint
->edesc
->bmAttributes
;
716 if (UE_GET_XFERTYPE(attr
) == UE_ISOCHRONOUS
)
717 ehci_check_itd_intr(sc
, ex
);
719 ehci_check_qh_intr(sc
, ex
);
725 ehci_check_qh_intr(ehci_softc_t
*sc
, struct ehci_xfer
*ex
)
727 ehci_soft_qtd_t
*sqtd
, *lsqtd
;
730 if (ex
->sqtdstart
== NULL
) {
731 printf("ehci_check_qh_intr: not valid sqtd\n");
738 printf("ehci_check_qh_intr: lsqtd==0\n");
743 * If the last TD is still active we need to check whether there
744 * is a an error somewhere in the middle, or whether there was a
745 * short packet (SPD and not ACTIVE).
747 usb_syncmem(&lsqtd
->dma
,
748 lsqtd
->offs
+ offsetof(ehci_qtd_t
, qtd_status
),
749 sizeof(lsqtd
->qtd
.qtd_status
),
750 BUS_DMASYNC_POSTWRITE
| BUS_DMASYNC_POSTREAD
);
751 if (le32toh(lsqtd
->qtd
.qtd_status
) & EHCI_QTD_ACTIVE
) {
752 DPRINTFN(12, ("ehci_check_intr: active ex=%p\n", ex
));
753 for (sqtd
= ex
->sqtdstart
; sqtd
!= lsqtd
; sqtd
=sqtd
->nextqtd
) {
754 usb_syncmem(&sqtd
->dma
,
755 sqtd
->offs
+ offsetof(ehci_qtd_t
, qtd_status
),
756 sizeof(sqtd
->qtd
.qtd_status
),
757 BUS_DMASYNC_POSTWRITE
| BUS_DMASYNC_POSTREAD
);
758 status
= le32toh(sqtd
->qtd
.qtd_status
);
759 usb_syncmem(&sqtd
->dma
,
760 sqtd
->offs
+ offsetof(ehci_qtd_t
, qtd_status
),
761 sizeof(sqtd
->qtd
.qtd_status
), BUS_DMASYNC_PREREAD
);
762 /* If there's an active QTD the xfer isn't done. */
763 if (status
& EHCI_QTD_ACTIVE
)
765 /* Any kind of error makes the xfer done. */
766 if (status
& EHCI_QTD_HALTED
)
768 /* We want short packets, and it is short: it's done */
769 if (EHCI_QTD_GET_BYTES(status
) != 0)
772 DPRINTFN(12, ("ehci_check_intr: ex=%p std=%p still active\n",
774 usb_syncmem(&lsqtd
->dma
,
775 lsqtd
->offs
+ offsetof(ehci_qtd_t
, qtd_status
),
776 sizeof(lsqtd
->qtd
.qtd_status
), BUS_DMASYNC_PREREAD
);
780 DPRINTFN(12, ("ehci_check_intr: ex=%p done\n", ex
));
781 callout_stop(&(ex
->xfer
.timeout_handle
));
786 ehci_check_itd_intr(ehci_softc_t
*sc
, struct ehci_xfer
*ex
) {
787 ehci_soft_itd_t
*itd
;
790 if (&ex
->xfer
!= SIMPLEQ_FIRST(&ex
->xfer
.pipe
->queue
))
793 if (ex
->itdstart
== NULL
) {
794 printf("ehci_check_itd_intr: not valid itd\n");
801 printf("ehci_check_itd_intr: itdend == 0\n");
807 * check no active transfers in last itd, meaning we're finished
810 usb_syncmem(&itd
->dma
, itd
->offs
+ offsetof(ehci_itd_t
, itd_ctl
),
811 sizeof(itd
->itd
.itd_ctl
), BUS_DMASYNC_POSTWRITE
|
812 BUS_DMASYNC_POSTREAD
);
814 for (i
= 0; i
< 8; i
++) {
815 if (le32toh(itd
->itd
.itd_ctl
[i
]) & EHCI_ITD_ACTIVE
)
820 goto done
; /* All 8 descriptors inactive, it's done */
823 DPRINTFN(12, ("ehci_check_itd_intr: ex %p itd %p still active\n", ex
,
827 DPRINTFN(12, ("ehci_check_itd_intr: ex=%p done\n", ex
));
828 callout_stop(&(ex
->xfer
.timeout_handle
));
833 ehci_idone(struct ehci_xfer
*ex
)
835 usbd_xfer_handle xfer
= &ex
->xfer
;
836 struct ehci_pipe
*epipe
= (struct ehci_pipe
*)xfer
->pipe
;
837 ehci_soft_qtd_t
*sqtd
, *lsqtd
;
838 u_int32_t status
= 0, nstatus
= 0;
841 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p\n", ex
));
848 printf("ehci_idone: ex is done!\n ");
851 printf("ehci_idone: ex=%p is done!\n", ex
);
859 if (xfer
->status
== USBD_CANCELLED
||
860 xfer
->status
== USBD_TIMEOUT
) {
861 DPRINTF(("ehci_idone: aborted xfer=%p\n", xfer
));
866 DPRINTFN(/*10*/2, ("ehci_idone: xfer=%p, pipe=%p ready\n", xfer
, epipe
));
868 ehci_dump_sqtds(ex
->sqtdstart
);
871 /* The transfer is done, compute actual length and status. */
873 if (UE_GET_XFERTYPE(xfer
->pipe
->endpoint
->edesc
->bmAttributes
)
876 struct ehci_soft_itd
*itd
;
877 int i
, nframes
, len
, uframes
;
882 switch (xfer
->pipe
->endpoint
->edesc
->bInterval
) {
884 panic("ehci: isoc xfer suddenly has 0 bInterval, invalid\n");
885 case 1: uframes
= 1; break;
886 case 2: uframes
= 2; break;
887 case 3: uframes
= 4; break;
888 default: uframes
= 8; break;
891 for (itd
= ex
->itdstart
; itd
!= NULL
; itd
= itd
->xfer_next
) {
892 usb_syncmem(&itd
->dma
,itd
->offs
+ offsetof(ehci_itd_t
,itd_ctl
),
893 sizeof(itd
->itd
.itd_ctl
), BUS_DMASYNC_POSTWRITE
|
894 BUS_DMASYNC_POSTREAD
);
896 for (i
= 0; i
< 8; i
+= uframes
) {
897 /* XXX - driver didn't fill in the frame full
898 * of uframes. This leads to scheduling
899 * inefficiencies, but working around
900 * this doubles complexity of tracking
903 if (nframes
>= xfer
->nframes
)
906 status
= le32toh(itd
->itd
.itd_ctl
[i
]);
907 len
= EHCI_ITD_GET_LEN(status
);
908 if (EHCI_ITD_GET_STATUS(status
) != 0)
909 len
= 0; /*No valid data on error*/
911 xfer
->frlengths
[nframes
++] = len
;
915 if (nframes
>= xfer
->nframes
)
919 xfer
->actlen
= actlen
;
920 xfer
->status
= USBD_NORMAL_COMPLETION
;
924 /* Continue processing xfers using queue heads */
928 for (sqtd
= ex
->sqtdstart
; sqtd
!= lsqtd
->nextqtd
; sqtd
= sqtd
->nextqtd
) {
929 usb_syncmem(&sqtd
->dma
, sqtd
->offs
, sizeof(sqtd
->qtd
),
930 BUS_DMASYNC_POSTWRITE
| BUS_DMASYNC_POSTREAD
);
931 nstatus
= le32toh(sqtd
->qtd
.qtd_status
);
932 if (nstatus
& EHCI_QTD_ACTIVE
)
936 if (EHCI_QTD_GET_PID(status
) != EHCI_QTD_PID_SETUP
)
937 actlen
+= sqtd
->len
- EHCI_QTD_GET_BYTES(status
);
942 * If there are left over TDs we need to update the toggle.
943 * The default pipe doesn't need it since control transfers
944 * start the toggle at 0 every time.
945 * For a short transfer we need to update the toggle for the missing
946 * packets within the qTD.
948 if ((sqtd
!= lsqtd
->nextqtd
|| EHCI_QTD_GET_BYTES(status
)) &&
949 xfer
->pipe
->device
->default_pipe
!= xfer
->pipe
) {
950 DPRINTFN(2, ("ehci_idone: need toggle update "
951 "status=%08x nstatus=%08x\n", status
, nstatus
));
953 ehci_dump_sqh(epipe
->sqh
);
954 ehci_dump_sqtds(ex
->sqtdstart
);
956 epipe
->nexttoggle
= EHCI_QTD_GET_TOGGLE(nstatus
);
959 DPRINTFN(/*10*/2, ("ehci_idone: len=%d, actlen=%d, status=0x%x\n",
960 xfer
->length
, actlen
, status
));
961 xfer
->actlen
= actlen
;
962 if (status
& EHCI_QTD_HALTED
) {
966 snprintb(sbuf
, sizeof(sbuf
),
967 "\20\7HALTED\6BUFERR\5BABBLE\4XACTERR\3MISSED\1PINGSTATE",
970 DPRINTFN(2, ("ehci_idone: error, addr=%d, endpt=0x%02x, "
972 xfer
->pipe
->device
->address
,
973 xfer
->pipe
->endpoint
->edesc
->bEndpointAddress
,
976 ehci_dump_sqh(epipe
->sqh
);
977 ehci_dump_sqtds(ex
->sqtdstart
);
980 /* low&full speed has an extra error flag */
981 if (EHCI_QH_GET_EPS(epipe
->sqh
->qh
.qh_endp
) !=
983 status
&= EHCI_QTD_STATERRS
| EHCI_QTD_PINGSTATE
;
985 status
&= EHCI_QTD_STATERRS
;
986 if (status
== 0) /* no other errors means a stall */ {
987 xfer
->status
= USBD_STALLED
;
989 xfer
->status
= USBD_IOERROR
; /* more info XXX */
991 /* XXX need to reset TT on missed microframe */
992 if (status
& EHCI_QTD_MISSEDMICRO
) {
994 xfer
->pipe
->device
->bus
->hci_private
;
996 printf("%s: missed microframe, TT reset not "
997 "implemented, hub might be inoperational\n",
998 device_xname(sc
->sc_dev
));
1001 xfer
->status
= USBD_NORMAL_COMPLETION
;
1005 /* XXX transfer_complete memcpys out transfer data (for in endpoints)
1006 * during this call, before methods->done is called: dma sync required
1008 usb_transfer_complete(xfer
);
1009 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p done\n", ex
));
1013 * Wait here until controller claims to have an interrupt.
1014 * Then call ehci_intr and return. Use timeout to avoid waiting
1018 ehci_waitintr(ehci_softc_t
*sc
, usbd_xfer_handle xfer
)
1023 xfer
->status
= USBD_IN_PROGRESS
;
1024 for (timo
= xfer
->timeout
; timo
>= 0; timo
--) {
1025 usb_delay_ms(&sc
->sc_bus
, 1);
1028 intrs
= EHCI_STS_INTRS(EOREAD4(sc
, EHCI_USBSTS
)) &
1030 DPRINTFN(15,("ehci_waitintr: 0x%04x\n", intrs
));
1037 if (xfer
->status
!= USBD_IN_PROGRESS
)
1043 DPRINTF(("ehci_waitintr: timeout\n"));
1044 xfer
->status
= USBD_TIMEOUT
;
1045 usb_transfer_complete(xfer
);
1046 /* XXX should free TD */
1050 ehci_poll(struct usbd_bus
*bus
)
1052 ehci_softc_t
*sc
= bus
->hci_private
;
1056 new = EHCI_STS_INTRS(EOREAD4(sc
, EHCI_USBSTS
));
1058 DPRINTFN(10,("ehci_poll: intrs=0x%04x\n", new));
1063 if (EOREAD4(sc
, EHCI_USBSTS
) & sc
->sc_eintrs
)
1068 ehci_childdet(device_t self
, device_t child
)
1070 struct ehci_softc
*sc
= device_private(self
);
1072 KASSERT(sc
->sc_child
== child
);
1073 sc
->sc_child
= NULL
;
1077 ehci_detach(struct ehci_softc
*sc
, int flags
)
1081 if (sc
->sc_child
!= NULL
)
1082 rv
= config_detach(sc
->sc_child
, flags
);
1087 callout_stop(&(sc
->sc_tmo_intrlist
));
1089 usb_delay_ms(&sc
->sc_bus
, 300); /* XXX let stray task complete */
1091 /* XXX free other data structures XXX */
1092 mutex_destroy(&sc
->sc_doorbell_lock
);
1093 mutex_destroy(&sc
->sc_intrhead_lock
);
1095 EOWRITE4(sc
, EHCI_CONFIGFLAG
, 0);
1102 ehci_activate(device_t self
, enum devact act
)
1104 struct ehci_softc
*sc
= device_private(self
);
1107 case DVACT_DEACTIVATE
:
1116 * Handle suspend/resume.
1118 * We need to switch to polling mode here, because this routine is
1119 * called from an interrupt context. This is all right since we
1120 * are almost suspended anyway.
1122 * Note that this power handler isn't to be registered directly; the
1123 * bus glue needs to call out to it.
1126 ehci_suspend(device_t dv
, pmf_qual_t qual
)
1128 ehci_softc_t
*sc
= device_private(dv
);
1134 sc
->sc_bus
.use_polling
++;
1136 for (i
= 1; i
<= sc
->sc_noport
; i
++) {
1137 cmd
= EOREAD4(sc
, EHCI_PORTSC(i
)) & ~EHCI_PS_CLEAR
;
1138 if ((cmd
& EHCI_PS_PO
) == 0 && (cmd
& EHCI_PS_PE
) == EHCI_PS_PE
)
1139 EOWRITE4(sc
, EHCI_PORTSC(i
), cmd
| EHCI_PS_SUSP
);
1142 sc
->sc_cmd
= EOREAD4(sc
, EHCI_USBCMD
);
1144 cmd
= sc
->sc_cmd
& ~(EHCI_CMD_ASE
| EHCI_CMD_PSE
);
1145 EOWRITE4(sc
, EHCI_USBCMD
, cmd
);
1147 for (i
= 0; i
< 100; i
++) {
1148 hcr
= EOREAD4(sc
, EHCI_USBSTS
) & (EHCI_STS_ASS
| EHCI_STS_PSS
);
1152 usb_delay_ms(&sc
->sc_bus
, 1);
1155 printf("%s: reset timeout\n", device_xname(dv
));
1157 cmd
&= ~EHCI_CMD_RS
;
1158 EOWRITE4(sc
, EHCI_USBCMD
, cmd
);
1160 for (i
= 0; i
< 100; i
++) {
1161 hcr
= EOREAD4(sc
, EHCI_USBSTS
) & EHCI_STS_HCH
;
1162 if (hcr
== EHCI_STS_HCH
)
1165 usb_delay_ms(&sc
->sc_bus
, 1);
1167 if (hcr
!= EHCI_STS_HCH
)
1168 printf("%s: config timeout\n", device_xname(dv
));
1170 sc
->sc_bus
.use_polling
--;
1177 ehci_resume(device_t dv
, pmf_qual_t qual
)
1179 ehci_softc_t
*sc
= device_private(dv
);
1183 /* restore things in case the bios sucks */
1184 EOWRITE4(sc
, EHCI_CTRLDSSEGMENT
, 0);
1185 EOWRITE4(sc
, EHCI_PERIODICLISTBASE
, DMAADDR(&sc
->sc_fldma
, 0));
1186 EOWRITE4(sc
, EHCI_ASYNCLISTADDR
,
1187 sc
->sc_async_head
->physaddr
| EHCI_LINK_QH
);
1189 EOWRITE4(sc
, EHCI_USBINTR
, sc
->sc_eintrs
& ~EHCI_INTR_PCIE
);
1191 EOWRITE4(sc
, EHCI_USBCMD
, sc
->sc_cmd
);
1194 for (i
= 1; i
<= sc
->sc_noport
; i
++) {
1195 cmd
= EOREAD4(sc
, EHCI_PORTSC(i
)) & ~EHCI_PS_CLEAR
;
1196 if ((cmd
& EHCI_PS_PO
) == 0 &&
1197 (cmd
& EHCI_PS_SUSP
) == EHCI_PS_SUSP
) {
1198 EOWRITE4(sc
, EHCI_PORTSC(i
), cmd
| EHCI_PS_FPR
);
1204 usb_delay_ms(&sc
->sc_bus
, USB_RESUME_WAIT
);
1206 for (i
= 1; i
<= sc
->sc_noport
; i
++) {
1207 cmd
= EOREAD4(sc
, EHCI_PORTSC(i
)) & ~EHCI_PS_CLEAR
;
1208 if ((cmd
& EHCI_PS_PO
) == 0 &&
1209 (cmd
& EHCI_PS_SUSP
) == EHCI_PS_SUSP
)
1210 EOWRITE4(sc
, EHCI_PORTSC(i
),
1211 cmd
& ~EHCI_PS_FPR
);
1215 EOWRITE4(sc
, EHCI_USBCMD
, sc
->sc_cmd
);
1216 EOWRITE4(sc
, EHCI_USBINTR
, sc
->sc_eintrs
);
1218 for (i
= 0; i
< 100; i
++) {
1219 hcr
= EOREAD4(sc
, EHCI_USBSTS
) & EHCI_STS_HCH
;
1220 if (hcr
!= EHCI_STS_HCH
)
1223 usb_delay_ms(&sc
->sc_bus
, 1);
1225 if (hcr
== EHCI_STS_HCH
)
1226 printf("%s: config timeout\n", device_xname(dv
));
1232 * Shut down the controller when the system is going down.
1235 ehci_shutdown(device_t self
, int flags
)
1237 ehci_softc_t
*sc
= device_private(self
);
1239 DPRINTF(("ehci_shutdown: stopping the HC\n"));
1240 EOWRITE4(sc
, EHCI_USBCMD
, 0); /* Halt controller */
1241 EOWRITE4(sc
, EHCI_USBCMD
, EHCI_CMD_HCRESET
);
1246 ehci_allocm(struct usbd_bus
*bus
, usb_dma_t
*dma
, u_int32_t size
)
1248 struct ehci_softc
*sc
= bus
->hci_private
;
1251 err
= usb_allocmem(&sc
->sc_bus
, size
, 0, dma
);
1252 if (err
== USBD_NOMEM
)
1253 err
= usb_reserve_allocm(&sc
->sc_dma_reserve
, dma
, size
);
1256 printf("ehci_allocm: usb_allocmem()=%d\n", err
);
1262 ehci_freem(struct usbd_bus
*bus
, usb_dma_t
*dma
)
1264 struct ehci_softc
*sc
= bus
->hci_private
;
1266 if (dma
->block
->flags
& USB_DMA_RESERVE
) {
1267 usb_reserve_freem(&sc
->sc_dma_reserve
,
1271 usb_freemem(&sc
->sc_bus
, dma
);
1274 Static usbd_xfer_handle
1275 ehci_allocx(struct usbd_bus
*bus
)
1277 struct ehci_softc
*sc
= bus
->hci_private
;
1278 usbd_xfer_handle xfer
;
1280 xfer
= SIMPLEQ_FIRST(&sc
->sc_free_xfers
);
1282 SIMPLEQ_REMOVE_HEAD(&sc
->sc_free_xfers
, next
);
1284 if (xfer
->busy_free
!= XFER_FREE
) {
1285 printf("ehci_allocx: xfer=%p not free, 0x%08x\n", xfer
,
1290 xfer
= malloc(sizeof(struct ehci_xfer
), M_USB
, M_NOWAIT
);
1293 memset(xfer
, 0, sizeof(struct ehci_xfer
));
1295 EXFER(xfer
)->isdone
= 1;
1296 xfer
->busy_free
= XFER_BUSY
;
1303 ehci_freex(struct usbd_bus
*bus
, usbd_xfer_handle xfer
)
1305 struct ehci_softc
*sc
= bus
->hci_private
;
1308 if (xfer
->busy_free
!= XFER_BUSY
) {
1309 printf("ehci_freex: xfer=%p not busy, 0x%08x\n", xfer
,
1312 xfer
->busy_free
= XFER_FREE
;
1313 if (!EXFER(xfer
)->isdone
) {
1314 printf("ehci_freex: !isdone\n");
1317 SIMPLEQ_INSERT_HEAD(&sc
->sc_free_xfers
, xfer
, next
);
1321 ehci_device_clear_toggle(usbd_pipe_handle pipe
)
1323 struct ehci_pipe
*epipe
= (struct ehci_pipe
*)pipe
;
1325 DPRINTF(("ehci_device_clear_toggle: epipe=%p status=0x%x\n",
1326 epipe
, epipe
->sqh
->qh
.qh_qtd
.qtd_status
));
1329 usbd_dump_pipe(pipe
);
1331 epipe
->nexttoggle
= 0;
1335 ehci_noop(usbd_pipe_handle pipe
)
1341 ehci_dump_regs(ehci_softc_t
*sc
)
1344 printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n",
1345 EOREAD4(sc
, EHCI_USBCMD
),
1346 EOREAD4(sc
, EHCI_USBSTS
),
1347 EOREAD4(sc
, EHCI_USBINTR
));
1348 printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
1349 EOREAD4(sc
, EHCI_FRINDEX
),
1350 EOREAD4(sc
, EHCI_CTRLDSSEGMENT
),
1351 EOREAD4(sc
, EHCI_PERIODICLISTBASE
),
1352 EOREAD4(sc
, EHCI_ASYNCLISTADDR
));
1353 for (i
= 1; i
<= sc
->sc_noport
; i
++)
1354 printf("port %d status=0x%08x\n", i
,
1355 EOREAD4(sc
, EHCI_PORTSC(i
)));
1359 * Unused function - this is meant to be called from a kernel
1365 ehci_dump_regs(theehci
);
1369 ehci_dump_link(ehci_link_t link
, int type
)
1371 link
= le32toh(link
);
1372 printf("0x%08x", link
);
1373 if (link
& EHCI_LINK_TERMINATE
)
1378 switch (EHCI_LINK_TYPE(link
)) {
1379 case EHCI_LINK_ITD
: printf("ITD"); break;
1380 case EHCI_LINK_QH
: printf("QH"); break;
1381 case EHCI_LINK_SITD
: printf("SITD"); break;
1382 case EHCI_LINK_FSTN
: printf("FSTN"); break;
1390 ehci_dump_sqtds(ehci_soft_qtd_t
*sqtd
)
1396 for (i
= 0; sqtd
&& i
< 20 && !stop
; sqtd
= sqtd
->nextqtd
, i
++) {
1397 ehci_dump_sqtd(sqtd
);
1398 usb_syncmem(&sqtd
->dma
,
1399 sqtd
->offs
+ offsetof(ehci_qtd_t
, qtd_next
),
1401 BUS_DMASYNC_POSTWRITE
| BUS_DMASYNC_POSTREAD
);
1402 stop
= sqtd
->qtd
.qtd_next
& htole32(EHCI_LINK_TERMINATE
);
1403 usb_syncmem(&sqtd
->dma
,
1404 sqtd
->offs
+ offsetof(ehci_qtd_t
, qtd_next
),
1405 sizeof(sqtd
->qtd
), BUS_DMASYNC_PREREAD
);
1408 printf("dump aborted, too many TDs\n");
1412 ehci_dump_sqtd(ehci_soft_qtd_t
*sqtd
)
1414 usb_syncmem(&sqtd
->dma
, sqtd
->offs
,
1415 sizeof(sqtd
->qtd
), BUS_DMASYNC_POSTWRITE
| BUS_DMASYNC_POSTREAD
);
1416 printf("QTD(%p) at 0x%08x:\n", sqtd
, sqtd
->physaddr
);
1417 ehci_dump_qtd(&sqtd
->qtd
);
1418 usb_syncmem(&sqtd
->dma
, sqtd
->offs
,
1419 sizeof(sqtd
->qtd
), BUS_DMASYNC_PREREAD
);
1423 ehci_dump_qtd(ehci_qtd_t
*qtd
)
1428 printf(" next="); ehci_dump_link(qtd
->qtd_next
, 0);
1429 printf(" altnext="); ehci_dump_link(qtd
->qtd_altnext
, 0);
1431 s
= le32toh(qtd
->qtd_status
);
1432 snprintb(sbuf
, sizeof(sbuf
),
1433 "\20\10ACTIVE\7HALTED\6BUFERR\5BABBLE\4XACTERR"
1434 "\3MISSED\2SPLIT\1PING", EHCI_QTD_GET_STATUS(s
));
1435 printf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
1436 s
, EHCI_QTD_GET_TOGGLE(s
), EHCI_QTD_GET_BYTES(s
),
1437 EHCI_QTD_GET_IOC(s
), EHCI_QTD_GET_C_PAGE(s
));
1438 printf(" cerr=%d pid=%d stat=0x%s\n", EHCI_QTD_GET_CERR(s
),
1439 EHCI_QTD_GET_PID(s
), sbuf
);
1440 for (s
= 0; s
< 5; s
++)
1441 printf(" buffer[%d]=0x%08x\n", s
, le32toh(qtd
->qtd_buffer
[s
]));
1445 ehci_dump_sqh(ehci_soft_qh_t
*sqh
)
1447 ehci_qh_t
*qh
= &sqh
->qh
;
1448 u_int32_t endp
, endphub
;
1450 usb_syncmem(&sqh
->dma
, sqh
->offs
,
1451 sizeof(sqh
->qh
), BUS_DMASYNC_POSTWRITE
| BUS_DMASYNC_POSTREAD
);
1452 printf("QH(%p) at 0x%08x:\n", sqh
, sqh
->physaddr
);
1453 printf(" link="); ehci_dump_link(qh
->qh_link
, 1); printf("\n");
1454 endp
= le32toh(qh
->qh_endp
);
1455 printf(" endp=0x%08x\n", endp
);
1456 printf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
1457 EHCI_QH_GET_ADDR(endp
), EHCI_QH_GET_INACT(endp
),
1458 EHCI_QH_GET_ENDPT(endp
), EHCI_QH_GET_EPS(endp
),
1459 EHCI_QH_GET_DTC(endp
), EHCI_QH_GET_HRECL(endp
));
1460 printf(" mpl=0x%x ctl=%d nrl=%d\n",
1461 EHCI_QH_GET_MPL(endp
), EHCI_QH_GET_CTL(endp
),
1462 EHCI_QH_GET_NRL(endp
));
1463 endphub
= le32toh(qh
->qh_endphub
);
1464 printf(" endphub=0x%08x\n", endphub
);
1465 printf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
1466 EHCI_QH_GET_SMASK(endphub
), EHCI_QH_GET_CMASK(endphub
),
1467 EHCI_QH_GET_HUBA(endphub
), EHCI_QH_GET_PORT(endphub
),
1468 EHCI_QH_GET_MULT(endphub
));
1469 printf(" curqtd="); ehci_dump_link(qh
->qh_curqtd
, 0); printf("\n");
1470 printf("Overlay qTD:\n");
1471 ehci_dump_qtd(&qh
->qh_qtd
);
1472 usb_syncmem(&sqh
->dma
, sqh
->offs
,
1473 sizeof(sqh
->qh
), BUS_DMASYNC_PREREAD
);
1478 ehci_dump_itd(struct ehci_soft_itd
*itd
)
1480 ehci_isoc_trans_t t
;
1481 ehci_isoc_bufr_ptr_t b
, b2
, b3
;
1484 printf("ITD: next phys=%X\n", itd
->itd
.itd_next
);
1486 for (i
= 0; i
< 8;i
++) {
1487 t
= le32toh(itd
->itd
.itd_ctl
[i
]);
1488 printf("ITDctl %d: stat=%X len=%X ioc=%X pg=%X offs=%X\n", i
,
1489 EHCI_ITD_GET_STATUS(t
), EHCI_ITD_GET_LEN(t
),
1490 EHCI_ITD_GET_IOC(t
), EHCI_ITD_GET_PG(t
),
1491 EHCI_ITD_GET_OFFS(t
));
1493 printf("ITDbufr: ");
1494 for (i
= 0; i
< 7; i
++)
1495 printf("%X,", EHCI_ITD_GET_BPTR(le32toh(itd
->itd
.itd_bufr
[i
])));
1497 b
= le32toh(itd
->itd
.itd_bufr
[0]);
1498 b2
= le32toh(itd
->itd
.itd_bufr
[1]);
1499 b3
= le32toh(itd
->itd
.itd_bufr
[2]);
1500 printf("\nep=%X daddr=%X dir=%d maxpkt=%X multi=%X\n",
1501 EHCI_ITD_GET_EP(b
), EHCI_ITD_GET_DADDR(b
), EHCI_ITD_GET_DIR(b2
),
1502 EHCI_ITD_GET_MAXPKT(b2
), EHCI_ITD_GET_MULTI(b3
));
1506 ehci_dump_sitd(struct ehci_soft_itd
*itd
)
1508 printf("SITD %p next=%p prev=%p xfernext=%p physaddr=%X slot=%d\n",
1509 itd
, itd
->u
.frame_list
.next
, itd
->u
.frame_list
.prev
,
1510 itd
->xfer_next
, itd
->physaddr
, itd
->slot
);
1516 ehci_dump_exfer(struct ehci_xfer
*ex
)
1518 printf("ehci_dump_exfer: ex=%p sqtdstart=%p end=%p itdstart=%p end=%p isdone=%d\n", ex
, ex
->sqtdstart
, ex
->sqtdend
, ex
->itdstart
, ex
->itdend
, ex
->isdone
);
1524 ehci_open(usbd_pipe_handle pipe
)
1526 usbd_device_handle dev
= pipe
->device
;
1527 ehci_softc_t
*sc
= dev
->bus
->hci_private
;
1528 usb_endpoint_descriptor_t
*ed
= pipe
->endpoint
->edesc
;
1529 u_int8_t addr
= dev
->address
;
1530 u_int8_t xfertype
= ed
->bmAttributes
& UE_XFERTYPE
;
1531 struct ehci_pipe
*epipe
= (struct ehci_pipe
*)pipe
;
1532 ehci_soft_qh_t
*sqh
;
1535 int ival
, speed
, naks
;
1536 int hshubaddr
, hshubport
;
1538 DPRINTFN(1, ("ehci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1539 pipe
, addr
, ed
->bEndpointAddress
, sc
->sc_addr
));
1541 if (dev
->myhsport
) {
1542 hshubaddr
= dev
->myhsport
->parent
->address
;
1543 hshubport
= dev
->myhsport
->portno
;
1550 return (USBD_IOERROR
);
1552 epipe
->nexttoggle
= 0;
1554 if (addr
== sc
->sc_addr
) {
1555 switch (ed
->bEndpointAddress
) {
1556 case USB_CONTROL_ENDPOINT
:
1557 pipe
->methods
= &ehci_root_ctrl_methods
;
1559 case UE_DIR_IN
| EHCI_INTR_ENDPT
:
1560 pipe
->methods
= &ehci_root_intr_methods
;
1563 DPRINTF(("ehci_open: bad bEndpointAddress 0x%02x\n",
1564 ed
->bEndpointAddress
));
1565 return (USBD_INVAL
);
1567 return (USBD_NORMAL_COMPLETION
);
1570 /* XXX All this stuff is only valid for async. */
1571 switch (dev
->speed
) {
1572 case USB_SPEED_LOW
: speed
= EHCI_QH_SPEED_LOW
; break;
1573 case USB_SPEED_FULL
: speed
= EHCI_QH_SPEED_FULL
; break;
1574 case USB_SPEED_HIGH
: speed
= EHCI_QH_SPEED_HIGH
; break;
1575 default: panic("ehci_open: bad device speed %d", dev
->speed
);
1577 if (speed
!= EHCI_QH_SPEED_HIGH
&& xfertype
== UE_ISOCHRONOUS
) {
1578 aprint_error_dev(sc
->sc_dev
, "error opening low/full speed "
1579 "isoc endpoint.\n");
1580 aprint_normal_dev(sc
->sc_dev
, "a low/full speed device is "
1581 "attached to a USB2 hub, and transaction translations are "
1582 "not yet supported.\n");
1583 aprint_normal_dev(sc
->sc_dev
, "reattach the device to the "
1584 "root hub instead.\n");
1585 DPRINTFN(1,("ehci_open: hshubaddr=%d hshubport=%d\n",
1586 hshubaddr
, hshubport
));
1592 /* Allocate sqh for everything, save isoc xfers */
1593 if (xfertype
!= UE_ISOCHRONOUS
) {
1594 sqh
= ehci_alloc_sqh(sc
);
1596 return (USBD_NOMEM
);
1597 /* qh_link filled when the QH is added */
1598 sqh
->qh
.qh_endp
= htole32(
1599 EHCI_QH_SET_ADDR(addr
) |
1600 EHCI_QH_SET_ENDPT(UE_GET_ADDR(ed
->bEndpointAddress
)) |
1601 EHCI_QH_SET_EPS(speed
) |
1603 EHCI_QH_SET_MPL(UGETW(ed
->wMaxPacketSize
)) |
1604 (speed
!= EHCI_QH_SPEED_HIGH
&& xfertype
== UE_CONTROL
?
1606 EHCI_QH_SET_NRL(naks
)
1608 sqh
->qh
.qh_endphub
= htole32(
1609 EHCI_QH_SET_MULT(1) |
1610 EHCI_QH_SET_HUBA(hshubaddr
) |
1611 EHCI_QH_SET_PORT(hshubport
) |
1612 EHCI_QH_SET_CMASK(0x08) | /* XXX */
1613 EHCI_QH_SET_SMASK(xfertype
== UE_INTERRUPT
? 0x02 : 0)
1615 sqh
->qh
.qh_curqtd
= EHCI_NULL
;
1616 /* Fill the overlay qTD */
1617 sqh
->qh
.qh_qtd
.qtd_next
= EHCI_NULL
;
1618 sqh
->qh
.qh_qtd
.qtd_altnext
= EHCI_NULL
;
1619 sqh
->qh
.qh_qtd
.qtd_status
= htole32(0);
1621 usb_syncmem(&sqh
->dma
, sqh
->offs
, sizeof(sqh
->qh
),
1622 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
1626 } /*xfertype == UE_ISOC*/
1630 err
= usb_allocmem(&sc
->sc_bus
, sizeof(usb_device_request_t
),
1631 0, &epipe
->u
.ctl
.reqdma
);
1634 printf("ehci_open: usb_allocmem()=%d\n", err
);
1638 pipe
->methods
= &ehci_device_ctrl_methods
;
1640 ehci_add_qh(sqh
, sc
->sc_async_head
);
1644 pipe
->methods
= &ehci_device_bulk_methods
;
1646 ehci_add_qh(sqh
, sc
->sc_async_head
);
1650 pipe
->methods
= &ehci_device_intr_methods
;
1651 ival
= pipe
->interval
;
1652 if (ival
== USBD_DEFAULT_INTERVAL
) {
1653 if (speed
== EHCI_QH_SPEED_HIGH
) {
1654 if (ed
->bInterval
> 16) {
1656 * illegal with high-speed, but there
1657 * were documentation bugs in the spec,
1662 ival
= (1 << (ed
->bInterval
- 1)) / 8;
1664 ival
= ed
->bInterval
;
1666 err
= ehci_device_setintr(sc
, sqh
, ival
);
1670 case UE_ISOCHRONOUS
:
1671 pipe
->methods
= &ehci_device_isoc_methods
;
1672 if (ed
->bInterval
== 0 || ed
->bInterval
> 16) {
1673 printf("ehci: opening pipe with invalid bInterval\n");
1677 if (UGETW(ed
->wMaxPacketSize
) == 0) {
1678 printf("ehci: zero length endpoint open request\n");
1682 epipe
->u
.isoc
.next_frame
= 0;
1683 epipe
->u
.isoc
.cur_xfers
= 0;
1686 DPRINTF(("ehci: bad xfer type %d\n", xfertype
));
1690 return (USBD_NORMAL_COMPLETION
);
1694 ehci_free_sqh(sc
, sqh
);
1699 * Add an ED to the schedule. Called at splusb().
1702 ehci_add_qh(ehci_soft_qh_t
*sqh
, ehci_soft_qh_t
*head
)
1706 usb_syncmem(&head
->dma
, head
->offs
+ offsetof(ehci_qh_t
, qh_link
),
1707 sizeof(head
->qh
.qh_link
), BUS_DMASYNC_POSTWRITE
);
1708 sqh
->next
= head
->next
;
1709 sqh
->qh
.qh_link
= head
->qh
.qh_link
;
1710 usb_syncmem(&sqh
->dma
, sqh
->offs
+ offsetof(ehci_qh_t
, qh_link
),
1711 sizeof(sqh
->qh
.qh_link
), BUS_DMASYNC_PREWRITE
);
1713 head
->qh
.qh_link
= htole32(sqh
->physaddr
| EHCI_LINK_QH
);
1714 usb_syncmem(&head
->dma
, head
->offs
+ offsetof(ehci_qh_t
, qh_link
),
1715 sizeof(head
->qh
.qh_link
), BUS_DMASYNC_PREWRITE
);
1718 if (ehcidebug
> 5) {
1719 printf("ehci_add_qh:\n");
1726 * Remove an ED from the schedule. Called at splusb().
1729 ehci_rem_qh(ehci_softc_t
*sc
, ehci_soft_qh_t
*sqh
, ehci_soft_qh_t
*head
)
1735 for (p
= head
; p
!= NULL
&& p
->next
!= sqh
; p
= p
->next
)
1738 panic("ehci_rem_qh: ED not found");
1739 usb_syncmem(&sqh
->dma
, sqh
->offs
+ offsetof(ehci_qh_t
, qh_link
),
1740 sizeof(sqh
->qh
.qh_link
), BUS_DMASYNC_POSTWRITE
);
1741 p
->next
= sqh
->next
;
1742 p
->qh
.qh_link
= sqh
->qh
.qh_link
;
1743 usb_syncmem(&p
->dma
, p
->offs
+ offsetof(ehci_qh_t
, qh_link
),
1744 sizeof(p
->qh
.qh_link
), BUS_DMASYNC_PREWRITE
);
1750 ehci_set_qh_qtd(ehci_soft_qh_t
*sqh
, ehci_soft_qtd_t
*sqtd
)
1755 /* Save toggle bit and ping status. */
1756 usb_syncmem(&sqh
->dma
, sqh
->offs
, sizeof(sqh
->qh
),
1757 BUS_DMASYNC_POSTWRITE
| BUS_DMASYNC_POSTREAD
);
1758 status
= sqh
->qh
.qh_qtd
.qtd_status
&
1759 htole32(EHCI_QTD_TOGGLE_MASK
|
1760 EHCI_QTD_SET_STATUS(EHCI_QTD_PINGSTATE
));
1761 /* Set HALTED to make hw leave it alone. */
1762 sqh
->qh
.qh_qtd
.qtd_status
=
1763 htole32(EHCI_QTD_SET_STATUS(EHCI_QTD_HALTED
));
1764 usb_syncmem(&sqh
->dma
,
1765 sqh
->offs
+ offsetof(ehci_qh_t
, qh_qtd
.qtd_status
),
1766 sizeof(sqh
->qh
.qh_qtd
.qtd_status
),
1767 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
1768 sqh
->qh
.qh_curqtd
= 0;
1769 sqh
->qh
.qh_qtd
.qtd_next
= htole32(sqtd
->physaddr
);
1770 sqh
->qh
.qh_qtd
.qtd_altnext
= 0;
1771 for (i
= 0; i
< EHCI_QTD_NBUFFERS
; i
++)
1772 sqh
->qh
.qh_qtd
.qtd_buffer
[i
] = 0;
1774 usb_syncmem(&sqh
->dma
, sqh
->offs
, sizeof(sqh
->qh
),
1775 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
1776 /* Set !HALTED && !ACTIVE to start execution, preserve some fields */
1777 sqh
->qh
.qh_qtd
.qtd_status
= status
;
1778 usb_syncmem(&sqh
->dma
,
1779 sqh
->offs
+ offsetof(ehci_qh_t
, qh_qtd
.qtd_status
),
1780 sizeof(sqh
->qh
.qh_qtd
.qtd_status
),
1781 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
1785 * Ensure that the HC has released all references to the QH. We do this
1786 * by asking for a Async Advance Doorbell interrupt and then we wait for
1788 * To make this easier we first obtain exclusive use of the doorbell.
1791 ehci_sync_hc(ehci_softc_t
*sc
)
1796 DPRINTFN(2,("ehci_sync_hc: dying\n"));
1799 DPRINTFN(2,("ehci_sync_hc: enter\n"));
1800 mutex_enter(&sc
->sc_doorbell_lock
); /* get doorbell */
1802 /* ask for doorbell */
1803 EOWRITE4(sc
, EHCI_USBCMD
, EOREAD4(sc
, EHCI_USBCMD
) | EHCI_CMD_IAAD
);
1804 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1805 EOREAD4(sc
, EHCI_USBCMD
), EOREAD4(sc
, EHCI_USBSTS
)));
1806 error
= tsleep(&sc
->sc_async_head
, PZERO
, "ehcidi", hz
); /* bell wait */
1807 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1808 EOREAD4(sc
, EHCI_USBCMD
), EOREAD4(sc
, EHCI_USBSTS
)));
1810 mutex_exit(&sc
->sc_doorbell_lock
); /* release doorbell */
1813 printf("ehci_sync_hc: tsleep() = %d\n", error
);
1815 DPRINTFN(2,("ehci_sync_hc: exit\n"));
1820 ehci_rem_free_itd_chain(ehci_softc_t
*sc
, struct ehci_xfer
*exfer
)
1822 struct ehci_soft_itd
*itd
, *prev
;
1826 if (exfer
->itdstart
== NULL
|| exfer
->itdend
== NULL
)
1827 panic("ehci isoc xfer being freed, but with no itd chain\n");
1829 for (itd
= exfer
->itdstart
; itd
!= NULL
; itd
= itd
->xfer_next
) {
1830 prev
= itd
->u
.frame_list
.prev
;
1831 /* Unlink itd from hardware chain, or frame array */
1832 if (prev
== NULL
) { /* We're at the table head */
1833 sc
->sc_softitds
[itd
->slot
] = itd
->u
.frame_list
.next
;
1834 sc
->sc_flist
[itd
->slot
] = itd
->itd
.itd_next
;
1835 usb_syncmem(&sc
->sc_fldma
,
1836 sizeof(ehci_link_t
) * itd
->slot
,
1837 sizeof(ehci_link_t
),
1838 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
1840 if (itd
->u
.frame_list
.next
!= NULL
)
1841 itd
->u
.frame_list
.next
->u
.frame_list
.prev
= NULL
;
1843 /* XXX this part is untested... */
1844 prev
->itd
.itd_next
= itd
->itd
.itd_next
;
1845 usb_syncmem(&itd
->dma
,
1846 itd
->offs
+ offsetof(ehci_itd_t
, itd_next
),
1847 sizeof(itd
->itd
.itd_next
), BUS_DMASYNC_PREWRITE
);
1849 prev
->u
.frame_list
.next
= itd
->u
.frame_list
.next
;
1850 if (itd
->u
.frame_list
.next
!= NULL
)
1851 itd
->u
.frame_list
.next
->u
.frame_list
.prev
= prev
;
1856 for (itd
= exfer
->itdstart
; itd
!= NULL
; itd
= itd
->xfer_next
) {
1858 ehci_free_itd(sc
, prev
);
1862 ehci_free_itd(sc
, prev
);
1863 exfer
->itdstart
= NULL
;
1864 exfer
->itdend
= NULL
;
1870 * Data structures and routines to emulate the root hub.
1872 Static usb_device_descriptor_t ehci_devd
= {
1873 USB_DEVICE_DESCRIPTOR_SIZE
,
1874 UDESC_DEVICE
, /* type */
1875 {0x00, 0x02}, /* USB version */
1876 UDCLASS_HUB
, /* class */
1877 UDSUBCLASS_HUB
, /* subclass */
1878 UDPROTO_HSHUBSTT
, /* protocol */
1879 64, /* max packet */
1880 {0},{0},{0x00,0x01}, /* device id */
1881 1,2,0, /* string indicies */
1882 1 /* # of configurations */
1885 Static
const usb_device_qualifier_t ehci_odevd
= {
1886 USB_DEVICE_DESCRIPTOR_SIZE
,
1887 UDESC_DEVICE_QUALIFIER
, /* type */
1888 {0x00, 0x02}, /* USB version */
1889 UDCLASS_HUB
, /* class */
1890 UDSUBCLASS_HUB
, /* subclass */
1891 UDPROTO_FSHUB
, /* protocol */
1892 64, /* max packet */
1893 1, /* # of configurations */
1897 Static
const usb_config_descriptor_t ehci_confd
= {
1898 USB_CONFIG_DESCRIPTOR_SIZE
,
1900 {USB_CONFIG_DESCRIPTOR_SIZE
+
1901 USB_INTERFACE_DESCRIPTOR_SIZE
+
1902 USB_ENDPOINT_DESCRIPTOR_SIZE
},
1906 UC_ATTR_MBO
| UC_SELF_POWERED
,
1910 Static
const usb_interface_descriptor_t ehci_ifcd
= {
1911 USB_INTERFACE_DESCRIPTOR_SIZE
,
1922 Static
const usb_endpoint_descriptor_t ehci_endpd
= {
1923 USB_ENDPOINT_DESCRIPTOR_SIZE
,
1925 UE_DIR_IN
| EHCI_INTR_ENDPT
,
1927 {8, 0}, /* max packet */
1931 Static
const usb_hub_descriptor_t ehci_hubd
= {
1932 USB_HUB_DESCRIPTOR_SIZE
,
1943 * Simulate a hardware hub by handling all the necessary requests.
1946 ehci_root_ctrl_transfer(usbd_xfer_handle xfer
)
1950 /* Insert last in queue. */
1951 err
= usb_insert_transfer(xfer
);
1955 /* Pipe isn't running, start first */
1956 return (ehci_root_ctrl_start(SIMPLEQ_FIRST(&xfer
->pipe
->queue
)));
1960 ehci_root_ctrl_start(usbd_xfer_handle xfer
)
1962 ehci_softc_t
*sc
= xfer
->pipe
->device
->bus
->hci_private
;
1963 usb_device_request_t
*req
;
1966 int s
, len
, value
, index
, l
, totlen
= 0;
1967 usb_port_status_t ps
;
1968 usb_hub_descriptor_t hubd
;
1973 return (USBD_IOERROR
);
1976 if (!(xfer
->rqflags
& URQ_REQUEST
))
1978 return (USBD_INVAL
);
1980 req
= &xfer
->request
;
1982 DPRINTFN(4,("ehci_root_ctrl_start: type=0x%02x request=%02x\n",
1983 req
->bmRequestType
, req
->bRequest
));
1985 len
= UGETW(req
->wLength
);
1986 value
= UGETW(req
->wValue
);
1987 index
= UGETW(req
->wIndex
);
1990 buf
= KERNADDR(&xfer
->dmabuf
, 0);
1992 #define C(x,y) ((x) | ((y) << 8))
1993 switch(C(req
->bRequest
, req
->bmRequestType
)) {
1994 case C(UR_CLEAR_FEATURE
, UT_WRITE_DEVICE
):
1995 case C(UR_CLEAR_FEATURE
, UT_WRITE_INTERFACE
):
1996 case C(UR_CLEAR_FEATURE
, UT_WRITE_ENDPOINT
):
1998 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
1999 * for the integrated root hub.
2002 case C(UR_GET_CONFIG
, UT_READ_DEVICE
):
2004 *(u_int8_t
*)buf
= sc
->sc_conf
;
2008 case C(UR_GET_DESCRIPTOR
, UT_READ_DEVICE
):
2009 DPRINTFN(8,("ehci_root_ctrl_start: wValue=0x%04x\n", value
));
2012 switch(value
>> 8) {
2014 if ((value
& 0xff) != 0) {
2018 totlen
= l
= min(len
, USB_DEVICE_DESCRIPTOR_SIZE
);
2019 USETW(ehci_devd
.idVendor
, sc
->sc_id_vendor
);
2020 memcpy(buf
, &ehci_devd
, l
);
2023 * We can't really operate at another speed, but the spec says
2024 * we need this descriptor.
2026 case UDESC_DEVICE_QUALIFIER
:
2027 if ((value
& 0xff) != 0) {
2031 totlen
= l
= min(len
, USB_DEVICE_DESCRIPTOR_SIZE
);
2032 memcpy(buf
, &ehci_odevd
, l
);
2035 * We can't really operate at another speed, but the spec says
2036 * we need this descriptor.
2038 case UDESC_OTHER_SPEED_CONFIGURATION
:
2040 if ((value
& 0xff) != 0) {
2044 totlen
= l
= min(len
, USB_CONFIG_DESCRIPTOR_SIZE
);
2045 memcpy(buf
, &ehci_confd
, l
);
2046 ((usb_config_descriptor_t
*)buf
)->bDescriptorType
=
2048 buf
= (char *)buf
+ l
;
2050 l
= min(len
, USB_INTERFACE_DESCRIPTOR_SIZE
);
2052 memcpy(buf
, &ehci_ifcd
, l
);
2053 buf
= (char *)buf
+ l
;
2055 l
= min(len
, USB_ENDPOINT_DESCRIPTOR_SIZE
);
2057 memcpy(buf
, &ehci_endpd
, l
);
2060 #define sd ((usb_string_descriptor_t *)buf)
2061 switch (value
& 0xff) {
2062 case 0: /* Language table */
2063 totlen
= usb_makelangtbl(sd
, len
);
2065 case 1: /* Vendor */
2066 totlen
= usb_makestrdesc(sd
, len
,
2069 case 2: /* Product */
2070 totlen
= usb_makestrdesc(sd
, len
,
2081 case C(UR_GET_INTERFACE
, UT_READ_INTERFACE
):
2083 *(u_int8_t
*)buf
= 0;
2087 case C(UR_GET_STATUS
, UT_READ_DEVICE
):
2089 USETW(((usb_status_t
*)buf
)->wStatus
,UDS_SELF_POWERED
);
2093 case C(UR_GET_STATUS
, UT_READ_INTERFACE
):
2094 case C(UR_GET_STATUS
, UT_READ_ENDPOINT
):
2096 USETW(((usb_status_t
*)buf
)->wStatus
, 0);
2100 case C(UR_SET_ADDRESS
, UT_WRITE_DEVICE
):
2101 if (value
>= USB_MAX_DEVICES
) {
2105 sc
->sc_addr
= value
;
2107 case C(UR_SET_CONFIG
, UT_WRITE_DEVICE
):
2108 if (value
!= 0 && value
!= 1) {
2112 sc
->sc_conf
= value
;
2114 case C(UR_SET_DESCRIPTOR
, UT_WRITE_DEVICE
):
2116 case C(UR_SET_FEATURE
, UT_WRITE_DEVICE
):
2117 case C(UR_SET_FEATURE
, UT_WRITE_INTERFACE
):
2118 case C(UR_SET_FEATURE
, UT_WRITE_ENDPOINT
):
2121 case C(UR_SET_INTERFACE
, UT_WRITE_INTERFACE
):
2123 case C(UR_SYNCH_FRAME
, UT_WRITE_ENDPOINT
):
2126 case C(UR_CLEAR_FEATURE
, UT_WRITE_CLASS_DEVICE
):
2128 case C(UR_CLEAR_FEATURE
, UT_WRITE_CLASS_OTHER
):
2129 DPRINTFN(4, ("ehci_root_ctrl_start: UR_CLEAR_PORT_FEATURE "
2130 "port=%d feature=%d\n",
2132 if (index
< 1 || index
> sc
->sc_noport
) {
2136 port
= EHCI_PORTSC(index
);
2137 v
= EOREAD4(sc
, port
);
2138 DPRINTFN(4, ("ehci_root_ctrl_start: portsc=0x%08x\n", v
));
2139 v
&= ~EHCI_PS_CLEAR
;
2141 case UHF_PORT_ENABLE
:
2142 EOWRITE4(sc
, port
, v
&~ EHCI_PS_PE
);
2144 case UHF_PORT_SUSPEND
:
2145 if (!(v
& EHCI_PS_SUSP
)) /* not suspended */
2148 EOWRITE4(sc
, port
, v
| EHCI_PS_FPR
);
2149 /* see USB2 spec ch. 7.1.7.7 */
2150 usb_delay_ms(&sc
->sc_bus
, 20);
2151 EOWRITE4(sc
, port
, v
);
2152 usb_delay_ms(&sc
->sc_bus
, 2);
2154 v
= EOREAD4(sc
, port
);
2155 if (v
& (EHCI_PS_FPR
| EHCI_PS_SUSP
))
2156 printf("ehci: resume failed: %x\n", v
);
2159 case UHF_PORT_POWER
:
2161 EOWRITE4(sc
, port
, v
&~ EHCI_PS_PP
);
2164 DPRINTFN(2,("ehci_root_ctrl_start: clear port test "
2167 case UHF_PORT_INDICATOR
:
2168 DPRINTFN(2,("ehci_root_ctrl_start: clear port ind "
2170 EOWRITE4(sc
, port
, v
&~ EHCI_PS_PIC
);
2172 case UHF_C_PORT_CONNECTION
:
2173 EOWRITE4(sc
, port
, v
| EHCI_PS_CSC
);
2175 case UHF_C_PORT_ENABLE
:
2176 EOWRITE4(sc
, port
, v
| EHCI_PS_PEC
);
2178 case UHF_C_PORT_SUSPEND
:
2181 case UHF_C_PORT_OVER_CURRENT
:
2182 EOWRITE4(sc
, port
, v
| EHCI_PS_OCC
);
2184 case UHF_C_PORT_RESET
:
2185 sc
->sc_isreset
[index
] = 0;
2193 case UHF_C_PORT_CONNECTION
:
2194 case UHF_C_PORT_ENABLE
:
2195 case UHF_C_PORT_SUSPEND
:
2196 case UHF_C_PORT_OVER_CURRENT
:
2197 case UHF_C_PORT_RESET
:
2203 case C(UR_GET_DESCRIPTOR
, UT_READ_CLASS_DEVICE
):
2206 if ((value
& 0xff) != 0) {
2211 hubd
.bNbrPorts
= sc
->sc_noport
;
2212 v
= EOREAD4(sc
, EHCI_HCSPARAMS
);
2213 USETW(hubd
.wHubCharacteristics
,
2214 EHCI_HCS_PPC(v
) ? UHD_PWR_INDIVIDUAL
: UHD_PWR_NO_SWITCH
|
2215 EHCI_HCS_P_INDICATOR(EREAD4(sc
, EHCI_HCSPARAMS
))
2216 ? UHD_PORT_IND
: 0);
2217 hubd
.bPwrOn2PwrGood
= 200; /* XXX can't find out? */
2218 for (i
= 0, l
= sc
->sc_noport
; l
> 0; i
++, l
-= 8, v
>>= 8)
2219 hubd
.DeviceRemovable
[i
++] = 0; /* XXX can't find out? */
2220 hubd
.bDescLength
= USB_HUB_DESCRIPTOR_SIZE
+ i
;
2221 l
= min(len
, hubd
.bDescLength
);
2223 memcpy(buf
, &hubd
, l
);
2225 case C(UR_GET_STATUS
, UT_READ_CLASS_DEVICE
):
2230 memset(buf
, 0, len
); /* ? XXX */
2233 case C(UR_GET_STATUS
, UT_READ_CLASS_OTHER
):
2234 DPRINTFN(8,("ehci_root_ctrl_start: get port status i=%d\n",
2236 if (index
< 1 || index
> sc
->sc_noport
) {
2244 v
= EOREAD4(sc
, EHCI_PORTSC(index
));
2245 DPRINTFN(8,("ehci_root_ctrl_start: port status=0x%04x\n",
2248 if (v
& EHCI_PS_CS
) i
|= UPS_CURRENT_CONNECT_STATUS
;
2249 if (v
& EHCI_PS_PE
) i
|= UPS_PORT_ENABLED
;
2250 if (v
& EHCI_PS_SUSP
) i
|= UPS_SUSPEND
;
2251 if (v
& EHCI_PS_OCA
) i
|= UPS_OVERCURRENT_INDICATOR
;
2252 if (v
& EHCI_PS_PR
) i
|= UPS_RESET
;
2253 if (v
& EHCI_PS_PP
) i
|= UPS_PORT_POWER
;
2254 USETW(ps
.wPortStatus
, i
);
2256 if (v
& EHCI_PS_CSC
) i
|= UPS_C_CONNECT_STATUS
;
2257 if (v
& EHCI_PS_PEC
) i
|= UPS_C_PORT_ENABLED
;
2258 if (v
& EHCI_PS_OCC
) i
|= UPS_C_OVERCURRENT_INDICATOR
;
2259 if (sc
->sc_isreset
[index
]) i
|= UPS_C_PORT_RESET
;
2260 USETW(ps
.wPortChange
, i
);
2261 l
= min(len
, sizeof ps
);
2262 memcpy(buf
, &ps
, l
);
2265 case C(UR_SET_DESCRIPTOR
, UT_WRITE_CLASS_DEVICE
):
2268 case C(UR_SET_FEATURE
, UT_WRITE_CLASS_DEVICE
):
2270 case C(UR_SET_FEATURE
, UT_WRITE_CLASS_OTHER
):
2271 if (index
< 1 || index
> sc
->sc_noport
) {
2275 port
= EHCI_PORTSC(index
);
2276 v
= EOREAD4(sc
, port
);
2277 DPRINTFN(4, ("ehci_root_ctrl_start: portsc=0x%08x\n", v
));
2278 v
&= ~EHCI_PS_CLEAR
;
2280 case UHF_PORT_ENABLE
:
2281 EOWRITE4(sc
, port
, v
| EHCI_PS_PE
);
2283 case UHF_PORT_SUSPEND
:
2284 EOWRITE4(sc
, port
, v
| EHCI_PS_SUSP
);
2286 case UHF_PORT_RESET
:
2287 DPRINTFN(5,("ehci_root_ctrl_start: reset port %d\n",
2289 if (EHCI_PS_IS_LOWSPEED(v
)) {
2290 /* Low speed device, give up ownership. */
2291 ehci_disown(sc
, index
, 1);
2294 /* Start reset sequence. */
2295 v
&= ~ (EHCI_PS_PE
| EHCI_PS_PR
);
2296 EOWRITE4(sc
, port
, v
| EHCI_PS_PR
);
2297 /* Wait for reset to complete. */
2298 usb_delay_ms(&sc
->sc_bus
, USB_PORT_ROOT_RESET_DELAY
);
2303 /* Terminate reset sequence. */
2304 EOWRITE4(sc
, port
, v
);
2305 /* Wait for HC to complete reset. */
2306 usb_delay_ms(&sc
->sc_bus
, EHCI_PORT_RESET_COMPLETE
);
2311 v
= EOREAD4(sc
, port
);
2312 DPRINTF(("ehci after reset, status=0x%08x\n", v
));
2313 if (v
& EHCI_PS_PR
) {
2314 printf("%s: port reset timeout\n",
2315 device_xname(sc
->sc_dev
));
2316 return (USBD_TIMEOUT
);
2318 if (!(v
& EHCI_PS_PE
)) {
2319 /* Not a high speed device, give up ownership.*/
2320 ehci_disown(sc
, index
, 0);
2323 sc
->sc_isreset
[index
] = 1;
2324 DPRINTF(("ehci port %d reset, status = 0x%08x\n",
2327 case UHF_PORT_POWER
:
2328 DPRINTFN(2,("ehci_root_ctrl_start: set port power "
2329 "%d (has PPC = %d)\n", index
,
2332 EOWRITE4(sc
, port
, v
| EHCI_PS_PP
);
2335 DPRINTFN(2,("ehci_root_ctrl_start: set port test "
2338 case UHF_PORT_INDICATOR
:
2339 DPRINTFN(2,("ehci_root_ctrl_start: set port ind "
2341 EOWRITE4(sc
, port
, v
| EHCI_PS_PIC
);
2348 case C(UR_CLEAR_TT_BUFFER
, UT_WRITE_CLASS_OTHER
):
2349 case C(UR_RESET_TT
, UT_WRITE_CLASS_OTHER
):
2350 case C(UR_GET_TT_STATE
, UT_READ_CLASS_OTHER
):
2351 case C(UR_STOP_TT
, UT_WRITE_CLASS_OTHER
):
2357 xfer
->actlen
= totlen
;
2358 err
= USBD_NORMAL_COMPLETION
;
2362 usb_transfer_complete(xfer
);
2364 return (USBD_IN_PROGRESS
);
2368 ehci_disown(ehci_softc_t
*sc
, int index
, int lowspeed
)
2373 DPRINTF(("ehci_disown: index=%d lowspeed=%d\n", index
, lowspeed
));
2375 if (sc
->sc_npcomp
!= 0) {
2376 int i
= (index
-1) / sc
->sc_npcomp
;
2377 if (i
>= sc
->sc_ncomp
)
2378 printf("%s: strange port\n",
2379 device_xname(sc
->sc_dev
));
2381 printf("%s: handing over %s speed device on "
2383 device_xname(sc
->sc_dev
),
2384 lowspeed
? "low" : "full",
2385 index
, device_xname(sc
->sc_comps
[i
]));
2387 printf("%s: npcomp == 0\n", device_xname(sc
->sc_dev
));
2390 port
= EHCI_PORTSC(index
);
2391 v
= EOREAD4(sc
, port
) &~ EHCI_PS_CLEAR
;
2392 EOWRITE4(sc
, port
, v
| EHCI_PS_PO
);
2395 /* Abort a root control request. */
2397 ehci_root_ctrl_abort(usbd_xfer_handle xfer
)
2399 /* Nothing to do, all transfers are synchronous. */
2402 /* Close the root pipe. */
2404 ehci_root_ctrl_close(usbd_pipe_handle pipe
)
2406 DPRINTF(("ehci_root_ctrl_close\n"));
2407 /* Nothing to do. */
2411 ehci_root_intr_done(usbd_xfer_handle xfer
)
2413 xfer
->hcpriv
= NULL
;
2417 ehci_root_intr_transfer(usbd_xfer_handle xfer
)
2421 /* Insert last in queue. */
2422 err
= usb_insert_transfer(xfer
);
2426 /* Pipe isn't running, start first */
2427 return (ehci_root_intr_start(SIMPLEQ_FIRST(&xfer
->pipe
->queue
)));
2431 ehci_root_intr_start(usbd_xfer_handle xfer
)
2433 usbd_pipe_handle pipe
= xfer
->pipe
;
2434 ehci_softc_t
*sc
= pipe
->device
->bus
->hci_private
;
2437 return (USBD_IOERROR
);
2439 sc
->sc_intrxfer
= xfer
;
2441 return (USBD_IN_PROGRESS
);
2444 /* Abort a root interrupt request. */
2446 ehci_root_intr_abort(usbd_xfer_handle xfer
)
2450 if (xfer
->pipe
->intrxfer
== xfer
) {
2451 DPRINTF(("ehci_root_intr_abort: remove\n"));
2452 xfer
->pipe
->intrxfer
= NULL
;
2454 xfer
->status
= USBD_CANCELLED
;
2456 usb_transfer_complete(xfer
);
2460 /* Close the root pipe. */
2462 ehci_root_intr_close(usbd_pipe_handle pipe
)
2464 ehci_softc_t
*sc
= pipe
->device
->bus
->hci_private
;
2466 DPRINTF(("ehci_root_intr_close\n"));
2468 sc
->sc_intrxfer
= NULL
;
2472 ehci_root_ctrl_done(usbd_xfer_handle xfer
)
2474 xfer
->hcpriv
= NULL
;
2477 /************************/
2479 Static ehci_soft_qh_t
*
2480 ehci_alloc_sqh(ehci_softc_t
*sc
)
2482 ehci_soft_qh_t
*sqh
;
2487 if (sc
->sc_freeqhs
== NULL
) {
2488 DPRINTFN(2, ("ehci_alloc_sqh: allocating chunk\n"));
2489 err
= usb_allocmem(&sc
->sc_bus
, EHCI_SQH_SIZE
* EHCI_SQH_CHUNK
,
2490 EHCI_PAGE_SIZE
, &dma
);
2493 printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err
);
2497 for(i
= 0; i
< EHCI_SQH_CHUNK
; i
++) {
2498 offs
= i
* EHCI_SQH_SIZE
;
2499 sqh
= KERNADDR(&dma
, offs
);
2500 sqh
->physaddr
= DMAADDR(&dma
, offs
);
2503 sqh
->next
= sc
->sc_freeqhs
;
2504 sc
->sc_freeqhs
= sqh
;
2507 sqh
= sc
->sc_freeqhs
;
2508 sc
->sc_freeqhs
= sqh
->next
;
2509 memset(&sqh
->qh
, 0, sizeof(ehci_qh_t
));
2515 ehci_free_sqh(ehci_softc_t
*sc
, ehci_soft_qh_t
*sqh
)
2517 sqh
->next
= sc
->sc_freeqhs
;
2518 sc
->sc_freeqhs
= sqh
;
2521 Static ehci_soft_qtd_t
*
2522 ehci_alloc_sqtd(ehci_softc_t
*sc
)
2524 ehci_soft_qtd_t
*sqtd
;
2530 if (sc
->sc_freeqtds
== NULL
) {
2531 DPRINTFN(2, ("ehci_alloc_sqtd: allocating chunk\n"));
2532 err
= usb_allocmem(&sc
->sc_bus
, EHCI_SQTD_SIZE
*EHCI_SQTD_CHUNK
,
2533 EHCI_PAGE_SIZE
, &dma
);
2536 printf("ehci_alloc_sqtd: usb_allocmem()=%d\n", err
);
2541 for(i
= 0; i
< EHCI_SQTD_CHUNK
; i
++) {
2542 offs
= i
* EHCI_SQTD_SIZE
;
2543 sqtd
= KERNADDR(&dma
, offs
);
2544 sqtd
->physaddr
= DMAADDR(&dma
, offs
);
2547 sqtd
->nextqtd
= sc
->sc_freeqtds
;
2548 sc
->sc_freeqtds
= sqtd
;
2554 sqtd
= sc
->sc_freeqtds
;
2555 sc
->sc_freeqtds
= sqtd
->nextqtd
;
2556 memset(&sqtd
->qtd
, 0, sizeof(ehci_qtd_t
));
2557 sqtd
->nextqtd
= NULL
;
2565 ehci_free_sqtd(ehci_softc_t
*sc
, ehci_soft_qtd_t
*sqtd
)
2570 sqtd
->nextqtd
= sc
->sc_freeqtds
;
2571 sc
->sc_freeqtds
= sqtd
;
2576 ehci_alloc_sqtd_chain(struct ehci_pipe
*epipe
, ehci_softc_t
*sc
,
2577 int alen
, int rd
, usbd_xfer_handle xfer
,
2578 ehci_soft_qtd_t
**sp
, ehci_soft_qtd_t
**ep
)
2580 ehci_soft_qtd_t
*next
, *cur
;
2581 ehci_physaddr_t dataphys
, dataphyspage
, dataphyslastpage
, nextphys
;
2582 u_int32_t qtdstatus
;
2583 int len
, curlen
, mps
;
2585 usb_dma_t
*dma
= &xfer
->dmabuf
;
2586 u_int16_t flags
= xfer
->flags
;
2588 DPRINTFN(alen
<4*4096,("ehci_alloc_sqtd_chain: start len=%d\n", alen
));
2591 dataphys
= DMAADDR(dma
, 0);
2592 dataphyslastpage
= EHCI_PAGE(dataphys
+ len
- 1);
2593 qtdstatus
= EHCI_QTD_ACTIVE
|
2594 EHCI_QTD_SET_PID(rd
? EHCI_QTD_PID_IN
: EHCI_QTD_PID_OUT
) |
2595 EHCI_QTD_SET_CERR(3)
2597 /* BYTES set below */
2599 mps
= UGETW(epipe
->pipe
.endpoint
->edesc
->wMaxPacketSize
);
2600 tog
= epipe
->nexttoggle
;
2601 qtdstatus
|= EHCI_QTD_SET_TOGGLE(tog
);
2603 cur
= ehci_alloc_sqtd(sc
);
2608 usb_syncmem(dma
, 0, alen
,
2609 rd
? BUS_DMASYNC_PREREAD
: BUS_DMASYNC_PREWRITE
);
2611 dataphyspage
= EHCI_PAGE(dataphys
);
2612 /* The EHCI hardware can handle at most 5 pages. */
2613 if (dataphyslastpage
- dataphyspage
<
2614 EHCI_QTD_NBUFFERS
* EHCI_PAGE_SIZE
) {
2615 /* we can handle it in this QTD */
2618 /* must use multiple TDs, fill as much as possible. */
2619 curlen
= EHCI_QTD_NBUFFERS
* EHCI_PAGE_SIZE
-
2620 EHCI_PAGE_OFFSET(dataphys
);
2623 printf("ehci_alloc_sqtd_chain: curlen=0x%x "
2624 "len=0x%x offs=0x%x\n", curlen
, len
,
2625 EHCI_PAGE_OFFSET(dataphys
));
2626 printf("lastpage=0x%x page=0x%x phys=0x%x\n",
2627 dataphyslastpage
, dataphyspage
,
2632 /* the length must be a multiple of the max size */
2633 curlen
-= curlen
% mps
;
2634 DPRINTFN(1,("ehci_alloc_sqtd_chain: multiple QTDs, "
2635 "curlen=%d\n", curlen
));
2638 panic("ehci_alloc_sqtd_chain: curlen == 0");
2641 DPRINTFN(4,("ehci_alloc_sqtd_chain: dataphys=0x%08x "
2642 "dataphyslastpage=0x%08x len=%d curlen=%d\n",
2643 dataphys
, dataphyslastpage
,
2648 * Allocate another transfer if there's more data left,
2649 * or if force last short transfer flag is set and we're
2650 * allocating a multiple of the max packet size.
2653 ((curlen
% mps
) == 0 && !rd
&& curlen
!= 0 &&
2654 (flags
& USBD_FORCE_SHORT_XFER
))) {
2655 next
= ehci_alloc_sqtd(sc
);
2658 nextphys
= htole32(next
->physaddr
);
2661 nextphys
= EHCI_NULL
;
2664 for (i
= 0; i
* EHCI_PAGE_SIZE
<
2665 curlen
+ EHCI_PAGE_OFFSET(dataphys
); i
++) {
2666 ehci_physaddr_t a
= dataphys
+ i
* EHCI_PAGE_SIZE
;
2667 if (i
!= 0) /* use offset only in first buffer */
2669 cur
->qtd
.qtd_buffer
[i
] = htole32(a
);
2670 cur
->qtd
.qtd_buffer_hi
[i
] = 0;
2672 if (i
>= EHCI_QTD_NBUFFERS
) {
2673 printf("ehci_alloc_sqtd_chain: i=%d\n", i
);
2678 cur
->nextqtd
= next
;
2679 cur
->qtd
.qtd_next
= cur
->qtd
.qtd_altnext
= nextphys
;
2680 cur
->qtd
.qtd_status
=
2681 htole32(qtdstatus
| EHCI_QTD_SET_BYTES(curlen
));
2685 DPRINTFN(10,("ehci_alloc_sqtd_chain: cbp=0x%08x end=0x%08x\n",
2686 dataphys
, dataphys
+ curlen
));
2687 /* adjust the toggle based on the number of packets in this
2689 if (((curlen
+ mps
- 1) / mps
) & 1) {
2691 qtdstatus
^= EHCI_QTD_TOGGLE_MASK
;
2695 usb_syncmem(&cur
->dma
, cur
->offs
, sizeof(cur
->qtd
),
2696 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
2697 DPRINTFN(10,("ehci_alloc_sqtd_chain: extend chain\n"));
2701 cur
->qtd
.qtd_status
|= htole32(EHCI_QTD_IOC
);
2702 usb_syncmem(&cur
->dma
, cur
->offs
, sizeof(cur
->qtd
),
2703 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
2705 epipe
->nexttoggle
= tog
;
2707 DPRINTFN(10,("ehci_alloc_sqtd_chain: return sqtd=%p sqtdend=%p\n",
2710 return (USBD_NORMAL_COMPLETION
);
2713 /* XXX free chain */
2714 DPRINTFN(-1,("ehci_alloc_sqtd_chain: no memory\n"));
2715 return (USBD_NOMEM
);
2719 ehci_free_sqtd_chain(ehci_softc_t
*sc
, ehci_soft_qtd_t
*sqtd
,
2720 ehci_soft_qtd_t
*sqtdend
)
2725 DPRINTFN(10,("ehci_free_sqtd_chain: sqtd=%p sqtdend=%p\n",
2728 for (i
= 0; sqtd
!= sqtdend
; sqtd
= p
, i
++) {
2730 ehci_free_sqtd(sc
, sqtd
);
2734 Static ehci_soft_itd_t
*
2735 ehci_alloc_itd(ehci_softc_t
*sc
)
2737 struct ehci_soft_itd
*itd
, *freeitd
;
2739 int i
, s
, offs
, frindex
, previndex
;
2744 /* Find an itd that wasn't freed this frame or last frame. This can
2745 * discard itds that were freed before frindex wrapped around
2746 * XXX - can this lead to thrashing? Could fix by enabling wrap-around
2747 * interrupt and fiddling with list when that happens */
2748 frindex
= (EOREAD4(sc
, EHCI_FRINDEX
) + 1) >> 3;
2749 previndex
= (frindex
!= 0) ? frindex
- 1 : sc
->sc_flsize
;
2752 LIST_FOREACH(itd
, &sc
->sc_freeitds
, u
.free_list
) {
2755 if (itd
->slot
!= frindex
&& itd
->slot
!= previndex
) {
2761 if (freeitd
== NULL
) {
2762 DPRINTFN(2, ("ehci_alloc_itd allocating chunk\n"));
2763 err
= usb_allocmem(&sc
->sc_bus
, EHCI_ITD_SIZE
* EHCI_ITD_CHUNK
,
2764 EHCI_PAGE_SIZE
, &dma
);
2767 DPRINTF(("ehci_alloc_itd, alloc returned %d\n", err
));
2771 for (i
= 0; i
< EHCI_ITD_CHUNK
; i
++) {
2772 offs
= i
* EHCI_ITD_SIZE
;
2773 itd
= KERNADDR(&dma
, offs
);
2774 itd
->physaddr
= DMAADDR(&dma
, offs
);
2777 LIST_INSERT_HEAD(&sc
->sc_freeitds
, itd
, u
.free_list
);
2779 freeitd
= LIST_FIRST(&sc
->sc_freeitds
);
2783 LIST_REMOVE(itd
, u
.free_list
);
2784 memset(&itd
->itd
, 0, sizeof(ehci_itd_t
));
2785 usb_syncmem(&itd
->dma
, itd
->offs
+ offsetof(ehci_itd_t
, itd_next
),
2786 sizeof(itd
->itd
.itd_next
), BUS_DMASYNC_PREWRITE
|
2787 BUS_DMASYNC_PREREAD
);
2789 itd
->u
.frame_list
.next
= NULL
;
2790 itd
->u
.frame_list
.prev
= NULL
;
2791 itd
->xfer_next
= NULL
;
2799 ehci_free_itd(ehci_softc_t
*sc
, ehci_soft_itd_t
*itd
)
2804 LIST_INSERT_HEAD(&sc
->sc_freeitds
, itd
, u
.free_list
);
2811 * Close a reqular pipe.
2812 * Assumes that there are no pending transactions.
2815 ehci_close_pipe(usbd_pipe_handle pipe
, ehci_soft_qh_t
*head
)
2817 struct ehci_pipe
*epipe
= (struct ehci_pipe
*)pipe
;
2818 ehci_softc_t
*sc
= pipe
->device
->bus
->hci_private
;
2819 ehci_soft_qh_t
*sqh
= epipe
->sqh
;
2823 ehci_rem_qh(sc
, sqh
, head
);
2825 ehci_free_sqh(sc
, epipe
->sqh
);
2829 * Abort a device request.
2830 * If this routine is called at splusb() it guarantees that the request
2831 * will be removed from the hardware scheduling and that the callback
2832 * for it will be called with USBD_CANCELLED status.
2833 * It's impossible to guarantee that the requested transfer will not
2834 * have happened since the hardware runs concurrently.
2835 * If the transaction has already happened we rely on the ordinary
2836 * interrupt processing to process it.
2837 * XXX This is most probably wrong.
2840 ehci_abort_xfer(usbd_xfer_handle xfer
, usbd_status status
)
2842 #define exfer EXFER(xfer)
2843 struct ehci_pipe
*epipe
= (struct ehci_pipe
*)xfer
->pipe
;
2844 ehci_softc_t
*sc
= epipe
->pipe
.device
->bus
->hci_private
;
2845 ehci_soft_qh_t
*sqh
= epipe
->sqh
;
2846 ehci_soft_qtd_t
*sqtd
;
2847 ehci_physaddr_t cur
;
2853 DPRINTF(("ehci_abort_xfer: xfer=%p pipe=%p\n", xfer
, epipe
));
2856 /* If we're dying, just do the software part. */
2858 xfer
->status
= status
; /* make software ignore it */
2859 callout_stop(&(xfer
->timeout_handle
));
2860 usb_transfer_complete(xfer
);
2865 if (xfer
->device
->bus
->intr_context
)
2866 panic("ehci_abort_xfer: not in process context");
2869 * If an abort is already in progress then just wait for it to
2870 * complete and return.
2872 if (xfer
->hcflags
& UXFER_ABORTING
) {
2873 DPRINTFN(2, ("ehci_abort_xfer: already aborting\n"));
2875 if (status
== USBD_TIMEOUT
)
2876 printf("ehci_abort_xfer: TIMEOUT while aborting\n");
2878 /* Override the status which might be USBD_TIMEOUT. */
2879 xfer
->status
= status
;
2880 DPRINTFN(2, ("ehci_abort_xfer: waiting for abort to finish\n"));
2881 xfer
->hcflags
|= UXFER_ABORTWAIT
;
2882 while (xfer
->hcflags
& UXFER_ABORTING
)
2883 tsleep(&xfer
->hcflags
, PZERO
, "ehciaw", 0);
2886 xfer
->hcflags
|= UXFER_ABORTING
;
2889 * Step 1: Make interrupt routine and hardware ignore xfer.
2892 xfer
->status
= status
; /* make software ignore it */
2893 callout_stop(&(xfer
->timeout_handle
));
2895 usb_syncmem(&sqh
->dma
,
2896 sqh
->offs
+ offsetof(ehci_qh_t
, qh_qtd
.qtd_status
),
2897 sizeof(sqh
->qh
.qh_qtd
.qtd_status
),
2898 BUS_DMASYNC_POSTWRITE
| BUS_DMASYNC_POSTREAD
);
2899 qhstatus
= sqh
->qh
.qh_qtd
.qtd_status
;
2900 sqh
->qh
.qh_qtd
.qtd_status
= qhstatus
| htole32(EHCI_QTD_HALTED
);
2901 usb_syncmem(&sqh
->dma
,
2902 sqh
->offs
+ offsetof(ehci_qh_t
, qh_qtd
.qtd_status
),
2903 sizeof(sqh
->qh
.qh_qtd
.qtd_status
),
2904 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
2905 for (sqtd
= exfer
->sqtdstart
; ; sqtd
= sqtd
->nextqtd
) {
2906 usb_syncmem(&sqtd
->dma
,
2907 sqtd
->offs
+ offsetof(ehci_qtd_t
, qtd_status
),
2908 sizeof(sqtd
->qtd
.qtd_status
),
2909 BUS_DMASYNC_POSTWRITE
| BUS_DMASYNC_POSTREAD
);
2910 sqtd
->qtd
.qtd_status
|= htole32(EHCI_QTD_HALTED
);
2911 usb_syncmem(&sqtd
->dma
,
2912 sqtd
->offs
+ offsetof(ehci_qtd_t
, qtd_status
),
2913 sizeof(sqtd
->qtd
.qtd_status
),
2914 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
2915 if (sqtd
== exfer
->sqtdend
)
2921 * Step 2: Wait until we know hardware has finished any possible
2922 * use of the xfer. Also make sure the soft interrupt routine
2927 #ifdef USB_USE_SOFTINTR
2928 sc
->sc_softwake
= 1;
2929 #endif /* USB_USE_SOFTINTR */
2930 usb_schedsoftintr(&sc
->sc_bus
);
2931 #ifdef USB_USE_SOFTINTR
2932 tsleep(&sc
->sc_softwake
, PZERO
, "ehciab", 0);
2933 #endif /* USB_USE_SOFTINTR */
2937 * Step 3: Remove any vestiges of the xfer from the hardware.
2938 * The complication here is that the hardware may have executed
2939 * beyond the xfer we're trying to abort. So as we're scanning
2940 * the TDs of this xfer we check if the hardware points to
2943 s
= splusb(); /* XXX why? */
2945 usb_syncmem(&sqh
->dma
,
2946 sqh
->offs
+ offsetof(ehci_qh_t
, qh_curqtd
),
2947 sizeof(sqh
->qh
.qh_curqtd
),
2948 BUS_DMASYNC_POSTWRITE
| BUS_DMASYNC_POSTREAD
);
2949 cur
= EHCI_LINK_ADDR(le32toh(sqh
->qh
.qh_curqtd
));
2951 for (sqtd
= exfer
->sqtdstart
; ; sqtd
= sqtd
->nextqtd
) {
2952 hit
|= cur
== sqtd
->physaddr
;
2953 if (sqtd
== exfer
->sqtdend
)
2956 sqtd
= sqtd
->nextqtd
;
2957 /* Zap curqtd register if hardware pointed inside the xfer. */
2958 if (hit
&& sqtd
!= NULL
) {
2959 DPRINTFN(1,("ehci_abort_xfer: cur=0x%08x\n", sqtd
->physaddr
));
2960 sqh
->qh
.qh_curqtd
= htole32(sqtd
->physaddr
); /* unlink qTDs */
2961 usb_syncmem(&sqh
->dma
,
2962 sqh
->offs
+ offsetof(ehci_qh_t
, qh_curqtd
),
2963 sizeof(sqh
->qh
.qh_curqtd
),
2964 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
2965 sqh
->qh
.qh_qtd
.qtd_status
= qhstatus
;
2966 usb_syncmem(&sqh
->dma
,
2967 sqh
->offs
+ offsetof(ehci_qh_t
, qh_qtd
.qtd_status
),
2968 sizeof(sqh
->qh
.qh_qtd
.qtd_status
),
2969 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
2971 DPRINTFN(1,("ehci_abort_xfer: no hit\n"));
2975 * Step 4: Execute callback.
2980 wake
= xfer
->hcflags
& UXFER_ABORTWAIT
;
2981 xfer
->hcflags
&= ~(UXFER_ABORTING
| UXFER_ABORTWAIT
);
2982 usb_transfer_complete(xfer
);
2984 wakeup(&xfer
->hcflags
);
2991 ehci_abort_isoc_xfer(usbd_xfer_handle xfer
, usbd_status status
)
2993 ehci_isoc_trans_t trans_status
;
2994 struct ehci_pipe
*epipe
;
2995 struct ehci_xfer
*exfer
;
2997 struct ehci_soft_itd
*itd
;
3000 epipe
= (struct ehci_pipe
*) xfer
->pipe
;
3001 exfer
= EXFER(xfer
);
3002 sc
= epipe
->pipe
.device
->bus
->hci_private
;
3004 DPRINTF(("ehci_abort_isoc_xfer: xfer %p pipe %p\n", xfer
, epipe
));
3008 xfer
->status
= status
;
3009 callout_stop(&(xfer
->timeout_handle
));
3010 usb_transfer_complete(xfer
);
3015 if (xfer
->hcflags
& UXFER_ABORTING
) {
3016 DPRINTFN(2, ("ehci_abort_isoc_xfer: already aborting\n"));
3019 if (status
== USBD_TIMEOUT
)
3020 printf("ehci_abort_xfer: TIMEOUT while aborting\n");
3023 xfer
->status
= status
;
3024 DPRINTFN(2, ("ehci_abort_xfer: waiting for abort to finish\n"));
3025 xfer
->hcflags
|= UXFER_ABORTWAIT
;
3026 while (xfer
->hcflags
& UXFER_ABORTING
)
3027 tsleep(&xfer
->hcflags
, PZERO
, "ehciiaw", 0);
3030 xfer
->hcflags
|= UXFER_ABORTING
;
3032 xfer
->status
= status
;
3033 callout_stop(&(xfer
->timeout_handle
));
3036 for (itd
= exfer
->itdstart
; itd
!= NULL
; itd
= itd
->xfer_next
) {
3037 usb_syncmem(&itd
->dma
,
3038 itd
->offs
+ offsetof(ehci_itd_t
, itd_ctl
),
3039 sizeof(itd
->itd
.itd_ctl
),
3040 BUS_DMASYNC_POSTWRITE
| BUS_DMASYNC_POSTREAD
);
3042 for (i
= 0; i
< 8; i
++) {
3043 trans_status
= le32toh(itd
->itd
.itd_ctl
[i
]);
3044 trans_status
&= ~EHCI_ITD_ACTIVE
;
3045 itd
->itd
.itd_ctl
[i
] = htole32(trans_status
);
3048 usb_syncmem(&itd
->dma
,
3049 itd
->offs
+ offsetof(ehci_itd_t
, itd_ctl
),
3050 sizeof(itd
->itd
.itd_ctl
),
3051 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
3056 #ifdef USB_USE_SOFTINTR
3057 sc
->sc_softwake
= 1;
3058 #endif /* USB_USE_SOFTINTR */
3059 usb_schedsoftintr(&sc
->sc_bus
);
3060 #ifdef USB_USE_SOFTINTR
3061 tsleep(&sc
->sc_softwake
, PZERO
, "ehciab", 0);
3062 #endif /* USB_USE_SOFTINTR */
3068 wake
= xfer
->hcflags
& UXFER_ABORTWAIT
;
3069 xfer
->hcflags
&= ~(UXFER_ABORTING
| UXFER_ABORTWAIT
);
3070 usb_transfer_complete(xfer
);
3072 wakeup(&xfer
->hcflags
);
3078 ehci_timeout(void *addr
)
3080 struct ehci_xfer
*exfer
= addr
;
3081 struct ehci_pipe
*epipe
= (struct ehci_pipe
*)exfer
->xfer
.pipe
;
3082 ehci_softc_t
*sc
= epipe
->pipe
.device
->bus
->hci_private
;
3084 DPRINTF(("ehci_timeout: exfer=%p\n", exfer
));
3087 usbd_dump_pipe(exfer
->xfer
.pipe
);
3091 ehci_abort_xfer(&exfer
->xfer
, USBD_TIMEOUT
);
3095 /* Execute the abort in a process context. */
3096 usb_init_task(&exfer
->abort_task
, ehci_timeout_task
, addr
);
3097 usb_add_task(exfer
->xfer
.pipe
->device
, &exfer
->abort_task
,
3102 ehci_timeout_task(void *addr
)
3104 usbd_xfer_handle xfer
= addr
;
3107 DPRINTF(("ehci_timeout_task: xfer=%p\n", xfer
));
3110 ehci_abort_xfer(xfer
, USBD_TIMEOUT
);
3114 /************************/
3117 ehci_device_ctrl_transfer(usbd_xfer_handle xfer
)
3121 /* Insert last in queue. */
3122 err
= usb_insert_transfer(xfer
);
3126 /* Pipe isn't running, start first */
3127 return (ehci_device_ctrl_start(SIMPLEQ_FIRST(&xfer
->pipe
->queue
)));
3131 ehci_device_ctrl_start(usbd_xfer_handle xfer
)
3133 ehci_softc_t
*sc
= xfer
->pipe
->device
->bus
->hci_private
;
3137 return (USBD_IOERROR
);
3140 if (!(xfer
->rqflags
& URQ_REQUEST
)) {
3142 printf("ehci_device_ctrl_transfer: not a request\n");
3143 return (USBD_INVAL
);
3147 err
= ehci_device_request(xfer
);
3151 if (sc
->sc_bus
.use_polling
)
3152 ehci_waitintr(sc
, xfer
);
3153 return (USBD_IN_PROGRESS
);
3157 ehci_device_ctrl_done(usbd_xfer_handle xfer
)
3159 struct ehci_xfer
*ex
= EXFER(xfer
);
3160 ehci_softc_t
*sc
= xfer
->pipe
->device
->bus
->hci_private
;
3161 struct ehci_pipe
*epipe
= (struct ehci_pipe
*)xfer
->pipe
;
3162 usb_device_request_t
*req
= &xfer
->request
;
3163 int len
= UGETW(req
->wLength
);
3164 int rd
= req
->bmRequestType
& UT_READ
;
3166 DPRINTFN(10,("ehci_ctrl_done: xfer=%p\n", xfer
));
3169 if (!(xfer
->rqflags
& URQ_REQUEST
)) {
3170 panic("ehci_ctrl_done: not a request");
3174 mutex_enter(&sc
->sc_intrhead_lock
);
3175 if (xfer
->status
!= USBD_NOMEM
&& ehci_active_intr_list(ex
)) {
3176 ehci_del_intr_list(sc
, ex
); /* remove from active list */
3177 ehci_free_sqtd_chain(sc
, ex
->sqtdstart
, NULL
);
3178 usb_syncmem(&epipe
->u
.ctl
.reqdma
, 0, sizeof *req
,
3179 BUS_DMASYNC_POSTWRITE
);
3181 usb_syncmem(&xfer
->dmabuf
, 0, len
,
3182 rd
? BUS_DMASYNC_POSTREAD
: BUS_DMASYNC_POSTWRITE
);
3184 mutex_exit(&sc
->sc_intrhead_lock
);
3186 DPRINTFN(5, ("ehci_ctrl_done: length=%d\n", xfer
->actlen
));
3189 /* Abort a device control request. */
3191 ehci_device_ctrl_abort(usbd_xfer_handle xfer
)
3193 DPRINTF(("ehci_device_ctrl_abort: xfer=%p\n", xfer
));
3194 ehci_abort_xfer(xfer
, USBD_CANCELLED
);
3197 /* Close a device control pipe. */
3199 ehci_device_ctrl_close(usbd_pipe_handle pipe
)
3201 ehci_softc_t
*sc
= pipe
->device
->bus
->hci_private
;
3202 /*struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;*/
3204 DPRINTF(("ehci_device_ctrl_close: pipe=%p\n", pipe
));
3205 ehci_close_pipe(pipe
, sc
->sc_async_head
);
3209 ehci_device_request(usbd_xfer_handle xfer
)
3211 #define exfer EXFER(xfer)
3212 struct ehci_pipe
*epipe
= (struct ehci_pipe
*)xfer
->pipe
;
3213 usb_device_request_t
*req
= &xfer
->request
;
3214 usbd_device_handle dev
= epipe
->pipe
.device
;
3215 ehci_softc_t
*sc
= dev
->bus
->hci_private
;
3216 int addr
= dev
->address
;
3217 ehci_soft_qtd_t
*setup
, *stat
, *next
;
3218 ehci_soft_qh_t
*sqh
;
3224 isread
= req
->bmRequestType
& UT_READ
;
3225 len
= UGETW(req
->wLength
);
3227 DPRINTFN(3,("ehci_device_request: type=0x%02x, request=0x%02x, "
3228 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
3229 req
->bmRequestType
, req
->bRequest
, UGETW(req
->wValue
),
3230 UGETW(req
->wIndex
), len
, addr
,
3231 epipe
->pipe
.endpoint
->edesc
->bEndpointAddress
));
3233 setup
= ehci_alloc_sqtd(sc
);
3234 if (setup
== NULL
) {
3238 stat
= ehci_alloc_sqtd(sc
);
3245 epipe
->u
.ctl
.length
= len
;
3247 /* Update device address and length since they may have changed
3248 during the setup of the control pipe in usbd_new_device(). */
3249 /* XXX This only needs to be done once, but it's too early in open. */
3250 /* XXXX Should not touch ED here! */
3252 (sqh
->qh
.qh_endp
& htole32(~(EHCI_QH_ADDRMASK
| EHCI_QH_MPLMASK
))) |
3254 EHCI_QH_SET_ADDR(addr
) |
3255 EHCI_QH_SET_MPL(UGETW(epipe
->pipe
.endpoint
->edesc
->wMaxPacketSize
))
3258 /* Set up data transaction */
3260 ehci_soft_qtd_t
*end
;
3262 /* Start toggle at 1. */
3263 epipe
->nexttoggle
= 1;
3264 err
= ehci_alloc_sqtd_chain(epipe
, sc
, len
, isread
, xfer
,
3268 end
->qtd
.qtd_status
&= htole32(~EHCI_QTD_IOC
);
3269 end
->nextqtd
= stat
;
3271 end
->qtd
.qtd_altnext
= htole32(stat
->physaddr
);
3272 usb_syncmem(&end
->dma
, end
->offs
, sizeof(end
->qtd
),
3273 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
3278 memcpy(KERNADDR(&epipe
->u
.ctl
.reqdma
, 0), req
, sizeof *req
);
3279 usb_syncmem(&epipe
->u
.ctl
.reqdma
, 0, sizeof *req
, BUS_DMASYNC_PREWRITE
);
3282 setup
->qtd
.qtd_status
= htole32(
3284 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP
) |
3285 EHCI_QTD_SET_CERR(3) |
3286 EHCI_QTD_SET_TOGGLE(0) |
3287 EHCI_QTD_SET_BYTES(sizeof *req
)
3289 setup
->qtd
.qtd_buffer
[0] = htole32(DMAADDR(&epipe
->u
.ctl
.reqdma
, 0));
3290 setup
->qtd
.qtd_buffer_hi
[0] = 0;
3291 setup
->nextqtd
= next
;
3292 setup
->qtd
.qtd_next
= setup
->qtd
.qtd_altnext
= htole32(next
->physaddr
);
3294 setup
->len
= sizeof *req
;
3295 usb_syncmem(&setup
->dma
, setup
->offs
, sizeof(setup
->qtd
),
3296 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
3298 stat
->qtd
.qtd_status
= htole32(
3300 EHCI_QTD_SET_PID(isread
? EHCI_QTD_PID_OUT
: EHCI_QTD_PID_IN
) |
3301 EHCI_QTD_SET_CERR(3) |
3302 EHCI_QTD_SET_TOGGLE(1) |
3305 stat
->qtd
.qtd_buffer
[0] = 0; /* XXX not needed? */
3306 stat
->qtd
.qtd_buffer_hi
[0] = 0; /* XXX not needed? */
3307 stat
->nextqtd
= NULL
;
3308 stat
->qtd
.qtd_next
= stat
->qtd
.qtd_altnext
= EHCI_NULL
;
3311 usb_syncmem(&stat
->dma
, stat
->offs
, sizeof(stat
->qtd
),
3312 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
3315 if (ehcidebug
> 5) {
3316 DPRINTF(("ehci_device_request:\n"));
3318 ehci_dump_sqtds(setup
);
3322 exfer
->sqtdstart
= setup
;
3323 exfer
->sqtdend
= stat
;
3325 if (!exfer
->isdone
) {
3326 printf("ehci_device_request: not done, exfer=%p\n", exfer
);
3331 /* Insert qTD in QH list. */
3333 ehci_set_qh_qtd(sqh
, setup
); /* also does usb_syncmem(sqh) */
3334 if (xfer
->timeout
&& !sc
->sc_bus
.use_polling
) {
3335 callout_reset(&(xfer
->timeout_handle
), (mstohz(xfer
->timeout
)),
3336 (ehci_timeout
), (xfer
));
3338 mutex_enter(&sc
->sc_intrhead_lock
);
3339 ehci_add_intr_list(sc
, exfer
);
3340 mutex_exit(&sc
->sc_intrhead_lock
);
3341 xfer
->status
= USBD_IN_PROGRESS
;
3345 if (ehcidebug
> 10) {
3346 DPRINTF(("ehci_device_request: status=%x\n",
3347 EOREAD4(sc
, EHCI_USBSTS
)));
3350 ehci_dump_sqh(sc
->sc_async_head
);
3352 ehci_dump_sqtds(setup
);
3356 return (USBD_NORMAL_COMPLETION
);
3359 ehci_free_sqtd(sc
, stat
);
3361 ehci_free_sqtd(sc
, setup
);
3363 DPRINTFN(-1,("ehci_device_request: no memory\n"));
3365 usb_transfer_complete(xfer
);
3371 * Some EHCI chips from VIA seem to trigger interrupts before writing back the
3372 * qTD status, or miss signalling occasionally under heavy load. If the host
3373 * machine is too fast, we we can miss transaction completion - when we scan
3374 * the active list the transaction still seems to be active. This generally
3375 * exhibits itself as a umass stall that never recovers.
3377 * We work around this behaviour by setting up this callback after any softintr
3378 * that completes with transactions still pending, giving us another chance to
3379 * check for completion after the writeback has taken place.
3382 ehci_intrlist_timeout(void *arg
)
3384 ehci_softc_t
*sc
= arg
;
3387 DPRINTF(("ehci_intrlist_timeout\n"));
3388 usb_schedsoftintr(&sc
->sc_bus
);
3393 /************************/
3396 ehci_device_bulk_transfer(usbd_xfer_handle xfer
)
3400 /* Insert last in queue. */
3401 err
= usb_insert_transfer(xfer
);
3405 /* Pipe isn't running, start first */
3406 return (ehci_device_bulk_start(SIMPLEQ_FIRST(&xfer
->pipe
->queue
)));
3410 ehci_device_bulk_start(usbd_xfer_handle xfer
)
3412 #define exfer EXFER(xfer)
3413 struct ehci_pipe
*epipe
= (struct ehci_pipe
*)xfer
->pipe
;
3414 usbd_device_handle dev
= epipe
->pipe
.device
;
3415 ehci_softc_t
*sc
= dev
->bus
->hci_private
;
3416 ehci_soft_qtd_t
*data
, *dataend
;
3417 ehci_soft_qh_t
*sqh
;
3419 int len
, isread
, endpt
;
3422 DPRINTFN(2, ("ehci_device_bulk_start: xfer=%p len=%d flags=%d\n",
3423 xfer
, xfer
->length
, xfer
->flags
));
3426 return (USBD_IOERROR
);
3429 if (xfer
->rqflags
& URQ_REQUEST
)
3430 panic("ehci_device_bulk_start: a request");
3434 endpt
= epipe
->pipe
.endpoint
->edesc
->bEndpointAddress
;
3435 isread
= UE_GET_DIR(endpt
) == UE_DIR_IN
;
3438 epipe
->u
.bulk
.length
= len
;
3440 err
= ehci_alloc_sqtd_chain(epipe
, sc
, len
, isread
, xfer
, &data
,
3443 DPRINTFN(-1,("ehci_device_bulk_transfer: no memory\n"));
3445 usb_transfer_complete(xfer
);
3450 if (ehcidebug
> 5) {
3451 DPRINTF(("ehci_device_bulk_start: data(1)\n"));
3453 ehci_dump_sqtds(data
);
3457 /* Set up interrupt info. */
3458 exfer
->sqtdstart
= data
;
3459 exfer
->sqtdend
= dataend
;
3461 if (!exfer
->isdone
) {
3462 printf("ehci_device_bulk_start: not done, ex=%p\n", exfer
);
3468 ehci_set_qh_qtd(sqh
, data
); /* also does usb_syncmem(sqh) */
3469 if (xfer
->timeout
&& !sc
->sc_bus
.use_polling
) {
3470 callout_reset(&(xfer
->timeout_handle
), (mstohz(xfer
->timeout
)),
3471 (ehci_timeout
), (xfer
));
3473 mutex_enter(&sc
->sc_intrhead_lock
);
3474 ehci_add_intr_list(sc
, exfer
);
3475 mutex_exit(&sc
->sc_intrhead_lock
);
3476 xfer
->status
= USBD_IN_PROGRESS
;
3480 if (ehcidebug
> 10) {
3481 DPRINTF(("ehci_device_bulk_start: data(2)\n"));
3483 DPRINTF(("ehci_device_bulk_start: data(3)\n"));
3486 printf("async_head:\n");
3487 ehci_dump_sqh(sc
->sc_async_head
);
3491 ehci_dump_sqtds(data
);
3495 if (sc
->sc_bus
.use_polling
)
3496 ehci_waitintr(sc
, xfer
);
3498 return (USBD_IN_PROGRESS
);
3503 ehci_device_bulk_abort(usbd_xfer_handle xfer
)
3505 DPRINTF(("ehci_device_bulk_abort: xfer=%p\n", xfer
));
3506 ehci_abort_xfer(xfer
, USBD_CANCELLED
);
3510 * Close a device bulk pipe.
3513 ehci_device_bulk_close(usbd_pipe_handle pipe
)
3515 ehci_softc_t
*sc
= pipe
->device
->bus
->hci_private
;
3517 DPRINTF(("ehci_device_bulk_close: pipe=%p\n", pipe
));
3518 ehci_close_pipe(pipe
, sc
->sc_async_head
);
3522 ehci_device_bulk_done(usbd_xfer_handle xfer
)
3524 struct ehci_xfer
*ex
= EXFER(xfer
);
3525 ehci_softc_t
*sc
= xfer
->pipe
->device
->bus
->hci_private
;
3526 struct ehci_pipe
*epipe
= (struct ehci_pipe
*)xfer
->pipe
;
3527 int endpt
= epipe
->pipe
.endpoint
->edesc
->bEndpointAddress
;
3528 int rd
= UE_GET_DIR(endpt
) == UE_DIR_IN
;
3530 DPRINTFN(10,("ehci_bulk_done: xfer=%p, actlen=%d\n",
3531 xfer
, xfer
->actlen
));
3533 mutex_enter(&sc
->sc_intrhead_lock
);
3534 if (xfer
->status
!= USBD_NOMEM
&& ehci_active_intr_list(ex
)) {
3535 ehci_del_intr_list(sc
, ex
); /* remove from active list */
3536 ehci_free_sqtd_chain(sc
, ex
->sqtdstart
, NULL
);
3537 usb_syncmem(&xfer
->dmabuf
, 0, xfer
->length
,
3538 rd
? BUS_DMASYNC_POSTREAD
: BUS_DMASYNC_POSTWRITE
);
3540 mutex_exit(&sc
->sc_intrhead_lock
);
3542 DPRINTFN(5, ("ehci_bulk_done: length=%d\n", xfer
->actlen
));
3545 /************************/
3548 ehci_device_setintr(ehci_softc_t
*sc
, ehci_soft_qh_t
*sqh
, int ival
)
3550 struct ehci_soft_islot
*isp
;
3553 /* Find a poll rate that is large enough. */
3554 for (lev
= EHCI_IPOLLRATES
- 1; lev
> 0; lev
--)
3555 if (EHCI_ILEV_IVAL(lev
) <= ival
)
3558 /* Pick an interrupt slot at the right level. */
3559 /* XXX could do better than picking at random */
3560 sc
->sc_rand
= (sc
->sc_rand
+ 191) % sc
->sc_flsize
;
3561 islot
= EHCI_IQHIDX(lev
, sc
->sc_rand
);
3564 isp
= &sc
->sc_islots
[islot
];
3565 ehci_add_qh(sqh
, isp
->sqh
);
3567 return (USBD_NORMAL_COMPLETION
);
3571 ehci_device_intr_transfer(usbd_xfer_handle xfer
)
3575 /* Insert last in queue. */
3576 err
= usb_insert_transfer(xfer
);
3581 * Pipe isn't running (otherwise err would be USBD_INPROG),
3582 * so start it first.
3584 return (ehci_device_intr_start(SIMPLEQ_FIRST(&xfer
->pipe
->queue
)));
3588 ehci_device_intr_start(usbd_xfer_handle xfer
)
3590 #define exfer EXFER(xfer)
3591 struct ehci_pipe
*epipe
= (struct ehci_pipe
*)xfer
->pipe
;
3592 usbd_device_handle dev
= xfer
->pipe
->device
;
3593 ehci_softc_t
*sc
= dev
->bus
->hci_private
;
3594 ehci_soft_qtd_t
*data
, *dataend
;
3595 ehci_soft_qh_t
*sqh
;
3597 int len
, isread
, endpt
;
3600 DPRINTFN(2, ("ehci_device_intr_start: xfer=%p len=%d flags=%d\n",
3601 xfer
, xfer
->length
, xfer
->flags
));
3604 return (USBD_IOERROR
);
3607 if (xfer
->rqflags
& URQ_REQUEST
)
3608 panic("ehci_device_intr_start: a request");
3612 endpt
= epipe
->pipe
.endpoint
->edesc
->bEndpointAddress
;
3613 isread
= UE_GET_DIR(endpt
) == UE_DIR_IN
;
3616 epipe
->u
.intr
.length
= len
;
3618 err
= ehci_alloc_sqtd_chain(epipe
, sc
, len
, isread
, xfer
, &data
,
3621 DPRINTFN(-1, ("ehci_device_intr_start: no memory\n"));
3623 usb_transfer_complete(xfer
);
3628 if (ehcidebug
> 5) {
3629 DPRINTF(("ehci_device_intr_start: data(1)\n"));
3631 ehci_dump_sqtds(data
);
3635 /* Set up interrupt info. */
3636 exfer
->sqtdstart
= data
;
3637 exfer
->sqtdend
= dataend
;
3639 if (!exfer
->isdone
) {
3640 printf("ehci_device_intr_start: not done, ex=%p\n", exfer
);
3646 ehci_set_qh_qtd(sqh
, data
); /* also does usb_syncmem(sqh) */
3647 if (xfer
->timeout
&& !sc
->sc_bus
.use_polling
) {
3648 callout_reset(&(xfer
->timeout_handle
), (mstohz(xfer
->timeout
)),
3649 (ehci_timeout
), (xfer
));
3651 mutex_enter(&sc
->sc_intrhead_lock
);
3652 ehci_add_intr_list(sc
, exfer
);
3653 mutex_exit(&sc
->sc_intrhead_lock
);
3654 xfer
->status
= USBD_IN_PROGRESS
;
3658 if (ehcidebug
> 10) {
3659 DPRINTF(("ehci_device_intr_start: data(2)\n"));
3661 DPRINTF(("ehci_device_intr_start: data(3)\n"));
3665 ehci_dump_sqtds(data
);
3669 if (sc
->sc_bus
.use_polling
)
3670 ehci_waitintr(sc
, xfer
);
3672 return (USBD_IN_PROGRESS
);
3677 ehci_device_intr_abort(usbd_xfer_handle xfer
)
3679 DPRINTFN(1, ("ehci_device_intr_abort: xfer=%p\n", xfer
));
3680 if (xfer
->pipe
->intrxfer
== xfer
) {
3681 DPRINTFN(1, ("echi_device_intr_abort: remove\n"));
3682 xfer
->pipe
->intrxfer
= NULL
;
3685 * XXX - abort_xfer uses ehci_sync_hc, which syncs via the advance
3686 * async doorbell. That's dependant on the async list, wheras
3687 * intr xfers are periodic, should not use this?
3689 ehci_abort_xfer(xfer
, USBD_CANCELLED
);
3693 ehci_device_intr_close(usbd_pipe_handle pipe
)
3695 ehci_softc_t
*sc
= pipe
->device
->bus
->hci_private
;
3696 struct ehci_pipe
*epipe
= (struct ehci_pipe
*)pipe
;
3697 struct ehci_soft_islot
*isp
;
3699 isp
= &sc
->sc_islots
[epipe
->sqh
->islot
];
3700 ehci_close_pipe(pipe
, isp
->sqh
);
3704 ehci_device_intr_done(usbd_xfer_handle xfer
)
3706 #define exfer EXFER(xfer)
3707 struct ehci_xfer
*ex
= EXFER(xfer
);
3708 ehci_softc_t
*sc
= xfer
->pipe
->device
->bus
->hci_private
;
3709 struct ehci_pipe
*epipe
= (struct ehci_pipe
*)xfer
->pipe
;
3710 ehci_soft_qtd_t
*data
, *dataend
;
3711 ehci_soft_qh_t
*sqh
;
3713 int len
, isread
, endpt
, s
;
3715 DPRINTFN(10, ("ehci_device_intr_done: xfer=%p, actlen=%d\n",
3716 xfer
, xfer
->actlen
));
3718 mutex_enter(&sc
->sc_intrhead_lock
);
3719 if (xfer
->pipe
->repeat
) {
3720 ehci_free_sqtd_chain(sc
, ex
->sqtdstart
, NULL
);
3722 len
= epipe
->u
.intr
.length
;
3724 endpt
= epipe
->pipe
.endpoint
->edesc
->bEndpointAddress
;
3725 isread
= UE_GET_DIR(endpt
) == UE_DIR_IN
;
3726 usb_syncmem(&xfer
->dmabuf
, 0, len
,
3727 isread
? BUS_DMASYNC_POSTREAD
: BUS_DMASYNC_POSTWRITE
);
3730 err
= ehci_alloc_sqtd_chain(epipe
, sc
, len
, isread
, xfer
,
3733 DPRINTFN(-1, ("ehci_device_intr_done: no memory\n"));
3735 mutex_exit(&sc
->sc_intrhead_lock
);
3739 /* Set up interrupt info. */
3740 exfer
->sqtdstart
= data
;
3741 exfer
->sqtdend
= dataend
;
3743 if (!exfer
->isdone
) {
3744 printf("ehci_device_intr_done: not done, ex=%p\n",
3751 ehci_set_qh_qtd(sqh
, data
); /* also does usb_syncmem(sqh) */
3752 if (xfer
->timeout
&& !sc
->sc_bus
.use_polling
) {
3753 callout_reset(&(xfer
->timeout_handle
),
3754 (mstohz(xfer
->timeout
)), (ehci_timeout
), (xfer
));
3758 xfer
->status
= USBD_IN_PROGRESS
;
3759 } else if (xfer
->status
!= USBD_NOMEM
&& ehci_active_intr_list(ex
)) {
3760 ehci_del_intr_list(sc
, ex
); /* remove from active list */
3761 ehci_free_sqtd_chain(sc
, ex
->sqtdstart
, NULL
);
3762 endpt
= epipe
->pipe
.endpoint
->edesc
->bEndpointAddress
;
3763 isread
= UE_GET_DIR(endpt
) == UE_DIR_IN
;
3764 usb_syncmem(&xfer
->dmabuf
, 0, xfer
->length
,
3765 isread
? BUS_DMASYNC_POSTREAD
: BUS_DMASYNC_POSTWRITE
);
3767 mutex_exit(&sc
->sc_intrhead_lock
);
3771 /************************/
3774 ehci_device_isoc_transfer(usbd_xfer_handle xfer
)
3778 err
= usb_insert_transfer(xfer
);
3779 if (err
&& err
!= USBD_IN_PROGRESS
)
3782 return ehci_device_isoc_start(xfer
);
3786 ehci_device_isoc_start(usbd_xfer_handle xfer
)
3788 struct ehci_pipe
*epipe
;
3789 usbd_device_handle dev
;
3791 struct ehci_xfer
*exfer
;
3792 ehci_soft_itd_t
*itd
, *prev
, *start
, *stop
;
3794 int i
, j
, k
, frames
, uframes
, ufrperframe
;
3795 int s
, trans_count
, offs
, total_length
;
3803 exfer
= (struct ehci_xfer
*) xfer
;
3804 sc
= xfer
->pipe
->device
->bus
->hci_private
;
3805 dev
= xfer
->pipe
->device
;
3806 epipe
= (struct ehci_pipe
*)xfer
->pipe
;
3809 * To allow continuous transfers, above we start all transfers
3810 * immediately. However, we're still going to get usbd_start_next call
3811 * this when another xfer completes. So, check if this is already
3812 * in progress or not
3815 if (exfer
->itdstart
!= NULL
)
3816 return USBD_IN_PROGRESS
;
3818 DPRINTFN(2, ("ehci_device_isoc_start: xfer %p len %d flags %d\n",
3819 xfer
, xfer
->length
, xfer
->flags
));
3822 return USBD_IOERROR
;
3825 * To avoid complication, don't allow a request right now that'll span
3826 * the entire frame table. To within 4 frames, to allow some leeway
3827 * on either side of where the hc currently is.
3829 if ((1 << (epipe
->pipe
.endpoint
->edesc
->bInterval
)) *
3830 xfer
->nframes
>= (sc
->sc_flsize
- 4) * 8) {
3831 printf("ehci: isoc descriptor requested that spans the entire frametable, too many frames\n");
3836 if (xfer
->rqflags
& URQ_REQUEST
)
3837 panic("ehci_device_isoc_start: request\n");
3840 printf("ehci_device_isoc_start: not done, ex = %p\n", exfer
);
3845 * Step 1: Allocate and initialize itds, how many do we need?
3846 * One per transfer if interval >= 8 microframes, fewer if we use
3847 * multiple microframes per frame.
3850 i
= epipe
->pipe
.endpoint
->edesc
->bInterval
;
3851 if (i
> 16 || i
== 0) {
3852 /* Spec page 271 says intervals > 16 are invalid */
3853 DPRINTF(("ehci_device_isoc_start: bInvertal %d invalid\n", i
));
3871 frames
= (xfer
->nframes
+ (ufrperframe
- 1)) / ufrperframe
;
3872 uframes
= 8 / ufrperframe
;
3875 DPRINTF(("ehci_device_isoc_start: frames == 0\n"));
3879 dma_buf
= &xfer
->dmabuf
;
3882 for (i
= 0; i
< frames
; i
++) {
3884 itd
= ehci_alloc_itd(sc
);
3887 prev
->itd
.itd_next
=
3888 htole32(itd
->physaddr
| EHCI_LINK_ITD
);
3889 usb_syncmem(&itd
->dma
,
3890 itd
->offs
+ offsetof(ehci_itd_t
, itd_next
),
3891 sizeof(itd
->itd
.itd_next
), BUS_DMASYNC_POSTWRITE
);
3893 prev
->xfer_next
= itd
;
3899 * Step 1.5, initialize uframes
3901 for (j
= 0; j
< 8; j
+= uframes
) {
3902 /* Calculate which page in the list this starts in */
3903 int addr
= DMAADDR(dma_buf
, froffs
);
3904 addr
= EHCI_PAGE_OFFSET(addr
);
3905 addr
+= (offs
- froffs
);
3906 addr
= EHCI_PAGE(addr
);
3907 addr
/= EHCI_PAGE_SIZE
;
3909 /* This gets the initial offset into the first page,
3910 * looks how far further along the current uframe
3911 * offset is. Works out how many pages that is.
3914 itd
->itd
.itd_ctl
[j
] = htole32 ( EHCI_ITD_ACTIVE
|
3915 EHCI_ITD_SET_LEN(xfer
->frlengths
[trans_count
]) |
3916 EHCI_ITD_SET_PG(addr
) |
3917 EHCI_ITD_SET_OFFS(EHCI_PAGE_OFFSET(DMAADDR(dma_buf
,offs
))));
3919 total_length
+= xfer
->frlengths
[trans_count
];
3920 offs
+= xfer
->frlengths
[trans_count
];
3923 if (trans_count
>= xfer
->nframes
) { /*Set IOC*/
3924 itd
->itd
.itd_ctl
[j
] |= htole32(EHCI_ITD_IOC
);
3929 /* Step 1.75, set buffer pointers. To simplify matters, all
3930 * pointers are filled out for the next 7 hardware pages in
3931 * the dma block, so no need to worry what pages to cover
3935 for (j
=0; j
< 7; j
++) {
3937 * Don't try to lookup a page that's past the end
3940 int page_offs
= EHCI_PAGE(froffs
+ (EHCI_PAGE_SIZE
* j
));
3941 if (page_offs
>= dma_buf
->block
->size
)
3944 long long page
= DMAADDR(dma_buf
, page_offs
);
3945 page
= EHCI_PAGE(page
);
3946 itd
->itd
.itd_bufr
[j
] =
3947 htole32(EHCI_ITD_SET_BPTR(page
));
3948 itd
->itd
.itd_bufr_hi
[j
] =
3949 htole32(page
>> 32);
3953 * Other special values
3956 k
= epipe
->pipe
.endpoint
->edesc
->bEndpointAddress
;
3957 itd
->itd
.itd_bufr
[0] |= htole32(EHCI_ITD_SET_EP(UE_GET_ADDR(k
)) |
3958 EHCI_ITD_SET_DADDR(epipe
->pipe
.device
->address
));
3960 k
= (UE_GET_DIR(epipe
->pipe
.endpoint
->edesc
->bEndpointAddress
))
3962 j
= UGETW(epipe
->pipe
.endpoint
->edesc
->wMaxPacketSize
);
3963 itd
->itd
.itd_bufr
[1] |= htole32(EHCI_ITD_SET_DIR(k
) |
3964 EHCI_ITD_SET_MAXPKT(UE_GET_SIZE(j
)));
3966 /* FIXME: handle invalid trans */
3967 itd
->itd
.itd_bufr
[2] |=
3968 htole32(EHCI_ITD_SET_MULTI(UE_GET_TRANS(j
)+1));
3970 usb_syncmem(&itd
->dma
,
3971 itd
->offs
+ offsetof(ehci_itd_t
, itd_next
),
3973 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
3976 } /* End of frame */
3979 stop
->xfer_next
= NULL
;
3980 exfer
->isoc_len
= total_length
;
3982 usb_syncmem(&exfer
->xfer
.dmabuf
, 0, total_length
,
3983 BUS_DMASYNC_PREREAD
| BUS_DMASYNC_PREWRITE
);
3986 * Part 2: Transfer descriptors have now been set up, now they must
3987 * be scheduled into the period frame list. Erk. Not wanting to
3988 * complicate matters, transfer is denied if the transfer spans
3989 * more than the period frame list.
3994 /* Start inserting frames */
3995 if (epipe
->u
.isoc
.cur_xfers
> 0) {
3996 frindex
= epipe
->u
.isoc
.next_frame
;
3998 frindex
= EOREAD4(sc
, EHCI_FRINDEX
);
3999 frindex
= frindex
>> 3; /* Erase microframe index */
4003 if (frindex
>= sc
->sc_flsize
)
4004 frindex
&= (sc
->sc_flsize
- 1);
4006 /* Whats the frame interval? */
4007 i
= (1 << epipe
->pipe
.endpoint
->edesc
->bInterval
);
4014 for (j
= 0; j
< frames
; j
++) {
4016 panic("ehci: unexpectedly ran out of isoc itds, isoc_start\n");
4018 itd
->itd
.itd_next
= sc
->sc_flist
[frindex
];
4019 if (itd
->itd
.itd_next
== 0)
4020 /* FIXME: frindex table gets initialized to NULL
4022 itd
->itd
.itd_next
= EHCI_NULL
;
4024 usb_syncmem(&itd
->dma
,
4025 itd
->offs
+ offsetof(ehci_itd_t
, itd_next
),
4026 sizeof(itd
->itd
.itd_next
),
4027 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
4029 sc
->sc_flist
[frindex
] = htole32(EHCI_LINK_ITD
| itd
->physaddr
);
4031 usb_syncmem(&sc
->sc_fldma
,
4032 sizeof(ehci_link_t
) * frindex
,
4033 sizeof(ehci_link_t
),
4034 BUS_DMASYNC_PREWRITE
| BUS_DMASYNC_PREREAD
);
4036 itd
->u
.frame_list
.next
= sc
->sc_softitds
[frindex
];
4037 sc
->sc_softitds
[frindex
] = itd
;
4038 if (itd
->u
.frame_list
.next
!= NULL
)
4039 itd
->u
.frame_list
.next
->u
.frame_list
.prev
= itd
;
4040 itd
->slot
= frindex
;
4041 itd
->u
.frame_list
.prev
= NULL
;
4044 if (frindex
>= sc
->sc_flsize
)
4045 frindex
-= sc
->sc_flsize
;
4047 itd
= itd
->xfer_next
;
4050 epipe
->u
.isoc
.cur_xfers
++;
4051 epipe
->u
.isoc
.next_frame
= frindex
;
4053 exfer
->itdstart
= start
;
4054 exfer
->itdend
= stop
;
4055 exfer
->sqtdstart
= NULL
;
4056 exfer
->sqtdstart
= NULL
;
4058 mutex_enter(&sc
->sc_intrhead_lock
);
4059 ehci_add_intr_list(sc
, exfer
);
4060 mutex_exit(&sc
->sc_intrhead_lock
);
4061 xfer
->status
= USBD_IN_PROGRESS
;
4065 if (sc
->sc_bus
.use_polling
) {
4066 printf("Starting ehci isoc xfer with polling. Bad idea?\n");
4067 ehci_waitintr(sc
, xfer
);
4070 return USBD_IN_PROGRESS
;
4074 ehci_device_isoc_abort(usbd_xfer_handle xfer
)
4076 DPRINTFN(1, ("ehci_device_isoc_abort: xfer = %p\n", xfer
));
4077 ehci_abort_isoc_xfer(xfer
, USBD_CANCELLED
);
4081 ehci_device_isoc_close(usbd_pipe_handle pipe
)
4083 DPRINTFN(1, ("ehci_device_isoc_close: nothing in the pipe to free?\n"));
4087 ehci_device_isoc_done(usbd_xfer_handle xfer
)
4089 struct ehci_xfer
*exfer
;
4091 struct ehci_pipe
*epipe
;
4094 exfer
= EXFER(xfer
);
4095 sc
= xfer
->pipe
->device
->bus
->hci_private
;
4096 epipe
= (struct ehci_pipe
*) xfer
->pipe
;
4099 epipe
->u
.isoc
.cur_xfers
--;
4100 mutex_enter(&sc
->sc_intrhead_lock
);
4101 if (xfer
->status
!= USBD_NOMEM
&& ehci_active_intr_list(exfer
)) {
4102 ehci_del_intr_list(sc
, exfer
);
4103 ehci_rem_free_itd_chain(sc
, exfer
);
4105 mutex_exit(&sc
->sc_intrhead_lock
);
4108 usb_syncmem(&xfer
->dmabuf
, 0, xfer
->length
, BUS_DMASYNC_POSTWRITE
|
4109 BUS_DMASYNC_POSTREAD
);