ehci.c revision 1.123.12.1 1 /* $NetBSD: ehci.c,v 1.123.12.1 2007/05/22 14:57:34 itohy Exp $ */
2
3 /*-
4 * Copyright (c) 2004, 2005, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net) and by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
41 *
42 * The EHCI 1.0 spec can be found at
43 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf
44 * and the USB 2.0 spec at
45 * http://www.usb.org/developers/docs/usb_20.zip
46 *
47 */
48
49 /*
50 * TODO:
51 * 1) The EHCI driver lacks support for isochronous transfers, so
52 * devices using them don't work.
53 *
54 * 2) Interrupt transfer scheduling does not manage the time available
55 * in each frame, so it is possible for the transfers to overrun
56 * the end of the frame.
57 *
58 * 3) Command failures are not recovered correctly.
59 */
60
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.123.12.1 2007/05/22 14:57:34 itohy Exp $");
63 /* __FBSDID("$FreeBSD: /repoman/r/ncvs/src/sys/dev/usb/ehci.c,v 1.52 2006/10/19 01:15:58 iedowse Exp $"); */
64
65 #if defined(__NetBSD__) || defined(__OpenBSD__)
66 #include "ohci.h"
67 #include "uhci.h"
68 #endif
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/kernel.h>
73 #include <sys/malloc.h>
74 #if defined(__NetBSD__) || defined(__OpenBSD__)
75 #include <sys/device.h>
76 #include <sys/select.h>
77 #elif defined(__FreeBSD__)
78 #include <sys/endian.h>
79 #include <sys/module.h>
80 #include <sys/bus.h>
81 #include <sys/lockmgr.h>
82 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
83 #include <machine/cpu.h>
84 #endif
85 #endif
86 #include <sys/proc.h>
87 #include <sys/queue.h>
88 #include <sys/sysctl.h>
89
90 #include <machine/bus.h>
91 #include <machine/endian.h>
92
93 #include <dev/usb/usb.h>
94 #include <dev/usb/usbdi.h>
95 #include <dev/usb/usbdivar.h>
96 #include <dev/usb/usb_mem.h>
97 #include <dev/usb/usb_quirks.h>
98
99 #include <dev/usb/ehcireg.h>
100 #include <dev/usb/ehcivar.h>
101
102 #if defined(__FreeBSD__)
103
104 #define delay(d) DELAY(d)
105 #endif
106
107 #ifdef USB_DEBUG
108 /* #define EHCI_DEBUG USB_DEBUG */
109 #define DPRINTF(x) do { if (ehcidebug) logprintf x; } while (0)
110 #define DPRINTFN(n,x) do { if (ehcidebug>(n)) logprintf x; } while (0)
111 int ehcidebug = 0;
112 #ifdef __FreeBSD__
113 SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci");
114 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RW,
115 &ehcidebug, 0, "ehci debug level");
116 #endif
117 #ifndef __NetBSD__
118 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
119 #endif
120 #else
121 #define DPRINTF(x)
122 #define DPRINTFN(n,x)
123 #endif
124
125 struct ehci_pipe {
126 struct usbd_pipe pipe;
127
128 ehci_soft_qh_t *sqh;
129 union {
130 ehci_soft_qtd_t *qtd;
131 /* ehci_soft_itd_t *itd; */
132 } tail;
133 union {
134 /* Control pipe */
135 struct {
136 usb_dma_t reqdma;
137 u_int length;
138 } ctl;
139 /* Interrupt pipe */
140 struct {
141 u_int length;
142 } intr;
143 /* Bulk pipe */
144 struct {
145 u_int length;
146 } bulk;
147 /* Iso pipe */
148 /* XXX */
149 } u;
150 };
151
152 Static usbd_status ehci_open(usbd_pipe_handle);
153 Static void ehci_poll(struct usbd_bus *);
154 Static void ehci_softintr(void *);
155 Static int ehci_intr1(ehci_softc_t *);
156 Static void ehci_waitintr(ehci_softc_t *, usbd_xfer_handle);
157 Static void ehci_check_intr(ehci_softc_t *, struct ehci_xfer *);
158 Static void ehci_idone(struct ehci_xfer *);
159 Static void ehci_timeout(void *);
160 Static void ehci_timeout_task(void *);
161 Static void ehci_intrlist_timeout(void *);
162
163 Static usbd_status ehci_prealloc(struct ehci_softc *, struct ehci_xfer *,
164 size_t, int);
165 Static usbd_status ehci_allocm(struct usbd_bus *, usbd_xfer_handle,
166 void *, size_t);
167 Static void ehci_freem(struct usbd_bus *, usbd_xfer_handle,
168 enum usbd_waitflg);
169
170 Static usbd_status ehci_map_alloc(usbd_xfer_handle);
171 Static void ehci_map_free(usbd_xfer_handle);
172 Static void ehci_mapm(usbd_xfer_handle, void *, size_t);
173 Static void ehci_mapm_mbuf(usbd_xfer_handle, struct mbuf *);
174 Static void ehci_unmapm(usbd_xfer_handle);
175
176 Static usbd_xfer_handle ehci_allocx(struct usbd_bus *, usbd_pipe_handle,
177 enum usbd_waitflg);
178 Static void ehci_freex(struct usbd_bus *, usbd_xfer_handle);
179
180 Static usbd_status ehci_root_ctrl_transfer(usbd_xfer_handle);
181 Static usbd_status ehci_root_ctrl_start(usbd_xfer_handle);
182 Static void ehci_root_ctrl_abort(usbd_xfer_handle);
183 Static void ehci_root_ctrl_close(usbd_pipe_handle);
184 Static void ehci_root_ctrl_done(usbd_xfer_handle);
185
186 Static usbd_status ehci_root_intr_transfer(usbd_xfer_handle);
187 Static usbd_status ehci_root_intr_start(usbd_xfer_handle);
188 Static void ehci_root_intr_abort(usbd_xfer_handle);
189 Static void ehci_root_intr_close(usbd_pipe_handle);
190 Static void ehci_root_intr_done(usbd_xfer_handle);
191
192 Static usbd_status ehci_device_ctrl_transfer(usbd_xfer_handle);
193 Static usbd_status ehci_device_ctrl_start(usbd_xfer_handle);
194 Static void ehci_device_ctrl_abort(usbd_xfer_handle);
195 Static void ehci_device_ctrl_close(usbd_pipe_handle);
196 Static void ehci_device_ctrl_done(usbd_xfer_handle);
197
198 Static usbd_status ehci_device_bulk_transfer(usbd_xfer_handle);
199 Static usbd_status ehci_device_bulk_start(usbd_xfer_handle);
200 Static void ehci_device_bulk_abort(usbd_xfer_handle);
201 Static void ehci_device_bulk_close(usbd_pipe_handle);
202 Static void ehci_device_bulk_done(usbd_xfer_handle);
203
204 Static usbd_status ehci_device_intr_transfer(usbd_xfer_handle);
205 Static usbd_status ehci_device_intr_start(usbd_xfer_handle);
206 Static void ehci_device_intr_abort(usbd_xfer_handle);
207 Static void ehci_device_intr_close(usbd_pipe_handle);
208 Static void ehci_device_intr_done(usbd_xfer_handle);
209
210 Static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle);
211 Static usbd_status ehci_device_isoc_start(usbd_xfer_handle);
212 Static void ehci_device_isoc_abort(usbd_xfer_handle);
213 Static void ehci_device_isoc_close(usbd_pipe_handle);
214 Static void ehci_device_isoc_done(usbd_xfer_handle);
215
216 Static void ehci_device_clear_toggle(usbd_pipe_handle pipe);
217 Static void ehci_noop(usbd_pipe_handle pipe);
218
219 Static int ehci_str(usb_string_descriptor_t *, int, const char *);
220 Static void ehci_pcd(ehci_softc_t *, usbd_xfer_handle);
221 Static void ehci_disown(ehci_softc_t *, int, int);
222
223 Static usbd_status ehci_grow_sqtd(ehci_softc_t *);
224 Static ehci_soft_qh_t *ehci_alloc_sqh(ehci_softc_t *);
225 Static void ehci_free_sqh(ehci_softc_t *, ehci_soft_qh_t *);
226
227 Static ehci_soft_qtd_t *ehci_alloc_sqtd(ehci_softc_t *);
228 Static ehci_soft_qtd_t *ehci_alloc_sqtd_norsv(ehci_softc_t *);
229 Static void ehci_free_sqtd(ehci_softc_t *, ehci_soft_qtd_t *);
230 Static void ehci_free_sqtd_norsv(ehci_softc_t *, ehci_soft_qtd_t *);
231 Static usbd_status ehci_alloc_sqtd_chain(struct ehci_pipe *,
232 ehci_softc_t *, int, int, usbd_xfer_handle,
233 ehci_soft_qtd_t *, ehci_soft_qtd_t *,
234 ehci_soft_qtd_t **, ehci_soft_qtd_t **);
235 Static void ehci_free_sqtd_chain(ehci_softc_t *, ehci_soft_qh_t *,
236 ehci_soft_qtd_t *, ehci_soft_qtd_t *);
237 Static void ehci_free_desc_chunks(ehci_softc_t *,
238 struct ehci_mdescs *);
239
240 Static usbd_status ehci_device_request(usbd_xfer_handle xfer);
241
242 Static usbd_status ehci_device_setintr(ehci_softc_t *, ehci_soft_qh_t *,
243 int ival);
244
245 Static void ehci_add_qh(ehci_softc_t *sc, ehci_soft_qh_t *,
246 ehci_soft_qh_t *);
247 Static void ehci_rem_qh(ehci_softc_t *, ehci_soft_qh_t *,
248 ehci_soft_qh_t *);
249 Static void ehci_activate_qh(ehci_softc_t *, ehci_soft_qh_t *,
250 ehci_soft_qtd_t *);
251 Static void ehci_sync_hc(ehci_softc_t *);
252
253 Static void ehci_close_pipe(usbd_pipe_handle, ehci_soft_qh_t *);
254 Static void ehci_abort_xfer(usbd_xfer_handle, usbd_status);
255
256 #ifdef EHCI_DEBUG
257 Static void ehci_dump_regs(ehci_softc_t *);
258 void ehci_dump(void);
259 Static ehci_softc_t *theehci;
260 Static void ehci_dump_link(ehci_link_t, int);
261 Static void ehci_dump_sqtds(ehci_soft_qtd_t *);
262 Static void ehci_dump_sqtd(ehci_soft_qtd_t *);
263 Static void ehci_dump_qtd(ehci_qtd_t *);
264 Static void ehci_dump_sqh(ehci_soft_qh_t *);
265 #ifdef DIAGNOSTIC
266 Static void ehci_dump_exfer(struct ehci_xfer *);
267 #endif
268 #endif
269
270 #define EHCI_NULL htole32(EHCI_LINK_TERMINATE)
271
272 #define EHCI_INTR_ENDPT 1
273
274 #define ehci_add_intr_list(sc, ex) \
275 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ex), inext);
276 #define ehci_del_intr_list(ex) \
277 do { \
278 LIST_REMOVE((ex), inext); \
279 (ex)->inext.le_prev = NULL; \
280 } while (0)
281 #define ehci_active_intr_list(ex) ((ex)->inext.le_prev != NULL)
282
283 Static const struct usbd_bus_methods ehci_bus_methods = {
284 ehci_open,
285 ehci_softintr,
286 ehci_poll,
287 ehci_allocm,
288 ehci_freem,
289 ehci_map_alloc,
290 ehci_map_free,
291 ehci_mapm,
292 ehci_mapm_mbuf,
293 ehci_unmapm,
294 ehci_allocx,
295 ehci_freex,
296 };
297
298 Static const struct usbd_pipe_methods ehci_root_ctrl_methods = {
299 ehci_root_ctrl_transfer,
300 ehci_root_ctrl_start,
301 ehci_root_ctrl_abort,
302 ehci_root_ctrl_close,
303 ehci_noop,
304 ehci_root_ctrl_done,
305 };
306
307 Static const struct usbd_pipe_methods ehci_root_intr_methods = {
308 ehci_root_intr_transfer,
309 ehci_root_intr_start,
310 ehci_root_intr_abort,
311 ehci_root_intr_close,
312 ehci_noop,
313 ehci_root_intr_done,
314 };
315
316 Static const struct usbd_pipe_methods ehci_device_ctrl_methods = {
317 ehci_device_ctrl_transfer,
318 ehci_device_ctrl_start,
319 ehci_device_ctrl_abort,
320 ehci_device_ctrl_close,
321 ehci_noop,
322 ehci_device_ctrl_done,
323 };
324
325 Static const struct usbd_pipe_methods ehci_device_intr_methods = {
326 ehci_device_intr_transfer,
327 ehci_device_intr_start,
328 ehci_device_intr_abort,
329 ehci_device_intr_close,
330 ehci_device_clear_toggle,
331 ehci_device_intr_done,
332 };
333
334 Static const struct usbd_pipe_methods ehci_device_bulk_methods = {
335 ehci_device_bulk_transfer,
336 ehci_device_bulk_start,
337 ehci_device_bulk_abort,
338 ehci_device_bulk_close,
339 ehci_device_clear_toggle,
340 ehci_device_bulk_done,
341 };
342
343 Static const struct usbd_pipe_methods ehci_device_isoc_methods = {
344 ehci_device_isoc_transfer,
345 ehci_device_isoc_start,
346 ehci_device_isoc_abort,
347 ehci_device_isoc_close,
348 ehci_noop,
349 ehci_device_isoc_done,
350 };
351
352 static const uint8_t revbits[EHCI_MAX_POLLRATE] = {
353 0x00,0x40,0x20,0x60,0x10,0x50,0x30,0x70,0x08,0x48,0x28,0x68,0x18,0x58,0x38,0x78,
354 0x04,0x44,0x24,0x64,0x14,0x54,0x34,0x74,0x0c,0x4c,0x2c,0x6c,0x1c,0x5c,0x3c,0x7c,
355 0x02,0x42,0x22,0x62,0x12,0x52,0x32,0x72,0x0a,0x4a,0x2a,0x6a,0x1a,0x5a,0x3a,0x7a,
356 0x06,0x46,0x26,0x66,0x16,0x56,0x36,0x76,0x0e,0x4e,0x2e,0x6e,0x1e,0x5e,0x3e,0x7e,
357 0x01,0x41,0x21,0x61,0x11,0x51,0x31,0x71,0x09,0x49,0x29,0x69,0x19,0x59,0x39,0x79,
358 0x05,0x45,0x25,0x65,0x15,0x55,0x35,0x75,0x0d,0x4d,0x2d,0x6d,0x1d,0x5d,0x3d,0x7d,
359 0x03,0x43,0x23,0x63,0x13,0x53,0x33,0x73,0x0b,0x4b,0x2b,0x6b,0x1b,0x5b,0x3b,0x7b,
360 0x07,0x47,0x27,0x67,0x17,0x57,0x37,0x77,0x0f,0x4f,0x2f,0x6f,0x1f,0x5f,0x3f,0x7f,
361 };
362
363 usbd_status
364 ehci_init(ehci_softc_t *sc)
365 {
366 u_int32_t vers, sparams, cparams, hcr;
367 u_int i;
368 usbd_status err;
369 ehci_soft_qh_t *sqh;
370 u_int ncomp;
371 int lev;
372
373 DPRINTF(("ehci_init: start\n"));
374 #ifdef EHCI_DEBUG
375 theehci = sc;
376 #endif
377
378 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
379
380 vers = EREAD2(sc, EHCI_HCIVERSION);
381 aprint_verbose("%s: EHCI version %x.%x\n", USBDEVNAME(sc->sc_bus.bdev),
382 vers >> 8, vers & 0xff);
383
384 sparams = EREAD4(sc, EHCI_HCSPARAMS);
385 DPRINTF(("ehci_init: sparams=0x%x\n", sparams));
386 sc->sc_npcomp = EHCI_HCS_N_PCC(sparams);
387 ncomp = EHCI_HCS_N_CC(sparams);
388 if (ncomp != sc->sc_ncomp) {
389 aprint_verbose("%s: wrong number of companions (%d != %d)\n",
390 USBDEVNAME(sc->sc_bus.bdev),
391 ncomp, sc->sc_ncomp);
392 if (ncomp < sc->sc_ncomp)
393 sc->sc_ncomp = ncomp;
394 }
395 if (sc->sc_ncomp > 0) {
396 printf("%s: companion controller%s, %d port%s each:",
397 USBDEVNAME(sc->sc_bus.bdev), sc->sc_ncomp!=1 ? "s" : "",
398 EHCI_HCS_N_PCC(sparams),
399 EHCI_HCS_N_PCC(sparams)!=1 ? "s" : "");
400 for (i = 0; i < sc->sc_ncomp; i++)
401 printf(" %s", USBDEVNAME(sc->sc_comps[i]->bdev));
402 printf("\n");
403 }
404 sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
405 cparams = EREAD4(sc, EHCI_HCCPARAMS);
406 DPRINTF(("ehci_init: cparams=0x%x\n", cparams));
407 sc->sc_hasppc = EHCI_HCS_PPC(sparams);
408
409 if (EHCI_HCC_64BIT(cparams)) {
410 /* MUST clear segment register if 64 bit capable. */
411 EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
412 }
413
414 sc->sc_bus.usbrev = USBREV_2_0;
415 usb_dma_tag_init(&sc->sc_dmatag);
416
417 /* Reset the controller */
418 DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
419 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
420 usb_delay_ms(&sc->sc_bus, 1);
421 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
422 for (i = 0; i < 100; i++) {
423 usb_delay_ms(&sc->sc_bus, 1);
424 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
425 if (!hcr)
426 break;
427 }
428 if (hcr) {
429 printf("%s: reset timeout\n",
430 USBDEVNAME(sc->sc_bus.bdev));
431 return (USBD_IOERROR);
432 }
433
434 /* frame list size at default, read back what we got and use that */
435 switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) {
436 case 0: sc->sc_flsize = 1024; break;
437 case 1: sc->sc_flsize = 512; break;
438 case 2: sc->sc_flsize = 256; break;
439 case 3: return (USBD_IOERROR);
440 }
441 err = usb_allocmem(&sc->sc_dmatag, sc->sc_flsize * sizeof(ehci_link_t),
442 EHCI_FLALIGN_ALIGN, &sc->sc_fldma);
443 if (err)
444 return (err);
445 DPRINTF(("%s: flsize=%d\n", USBDEVNAME(sc->sc_bus.bdev),sc->sc_flsize));
446 sc->sc_flist = KERNADDR(&sc->sc_fldma, 0);
447 EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
448
449 /* Set up the bus struct. */
450 sc->sc_bus.methods = &ehci_bus_methods;
451 sc->sc_bus.pipe_size = sizeof(struct ehci_pipe);
452
453 #if defined(__NetBSD__) || defined(__OpenBSD__)
454 sc->sc_powerhook = powerhook_establish(USBDEVNAME(sc->sc_bus.bdev),
455 ehci_power, sc);
456 sc->sc_shutdownhook = shutdownhook_establish(ehci_shutdown, sc);
457 #endif
458
459 sc->sc_eintrs = EHCI_NORMAL_INTRS;
460
461 /*
462 * Allocate the interrupt dummy QHs. These are arranged to give
463 * poll intervals that are powers of 2 times 1ms.
464 */
465 for (i = 0; i < EHCI_INTRQHS; i++) {
466 sqh = ehci_alloc_sqh(sc);
467 if (sqh == NULL) {
468 err = USBD_NOMEM;
469 goto bad1;
470 }
471 sc->sc_islots[i].sqh = sqh;
472 }
473 lev = 0;
474 for (i = 0; i < EHCI_INTRQHS; i++) {
475 if (i == EHCI_IQHIDX(lev + 1, 0))
476 lev++;
477 sqh = sc->sc_islots[i].sqh;
478 if (i == 0) {
479 /* The last (1ms) QH terminates. */
480 sqh->qh.qh_link = EHCI_NULL;
481 sqh->next = NULL;
482 } else {
483 /* Otherwise the next QH has half the poll interval */
484 sqh->next =
485 sc->sc_islots[EHCI_IQHIDX(lev - 1, i + 1)].sqh;
486 sqh->qh.qh_link = htole32(EHCI_SQH_DMAADDR(sqh->next) |
487 EHCI_LINK_QH);
488 }
489 sqh->qh.qh_endp = htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
490 sqh->qh.qh_endphub = htole32(EHCI_QH_SET_MULT(1));
491 sqh->qh.qh_curqtd = EHCI_NULL;
492 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
493 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
494 sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
495 }
496 /* Point the frame list at the last level (128ms). */
497 for (i = 0; i < sc->sc_flsize; i++) {
498 int j;
499
500 j = (i & ~(EHCI_MAX_POLLRATE-1)) |
501 revbits[i & (EHCI_MAX_POLLRATE-1)];
502 sc->sc_flist[j] = htole32(EHCI_LINK_QH |
503 EHCI_SQH_DMAADDR(sc->sc_islots[EHCI_IQHIDX(EHCI_IPOLLRATES
504 - 1, i)].sqh));
505 }
506 EHCI_SQH_SYNC(sc, sqh, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
507
508 /* Allocate dummy QH that starts the async list. */
509 sqh = ehci_alloc_sqh(sc);
510 if (sqh == NULL) {
511 err = USBD_NOMEM;
512 goto bad1;
513 }
514 /* Fill the QH */
515 sqh->qh.qh_endp =
516 htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
517 sqh->qh.qh_link =
518 htole32(EHCI_SQH_DMAADDR(sqh) | EHCI_LINK_QH);
519 sqh->qh.qh_curqtd = EHCI_NULL;
520 sqh->prev = sqh; /*It's a circular list.. */
521 sqh->next = sqh;
522 /* Fill the overlay qTD */
523 sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
524 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
525 sqh->qh.qh_qtd.qtd_status = htole32(0);
526 #ifdef EHCI_DEBUG
527 if (ehcidebug) {
528 ehci_dump_sqh(sqh);
529 }
530 #endif
531 EHCI_SQH_SYNC(sc, sqh, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
532
533 /* Point to async list */
534 sc->sc_async_head = sqh;
535 EOWRITE4(sc, EHCI_ASYNCLISTADDR, EHCI_SQH_DMAADDR(sqh) | EHCI_LINK_QH);
536
537 usb_callout_init(sc->sc_tmo_intrlist);
538
539 lockinit(&sc->sc_doorbell_lock, PZERO, "ehcidb", 0, 0);
540
541 /* Enable interrupts */
542 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
543
544 /* Turn on controller */
545 EOWRITE4(sc, EHCI_USBCMD,
546 EHCI_CMD_ITC_2 | /* 2 microframes interrupt delay */
547 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
548 EHCI_CMD_ASE |
549 EHCI_CMD_PSE |
550 EHCI_CMD_RS);
551
552 /* Take over port ownership */
553 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
554
555 for (i = 0; i < 100; i++) {
556 usb_delay_ms(&sc->sc_bus, 1);
557 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
558 if (!hcr)
559 break;
560 }
561 if (hcr) {
562 printf("%s: run timeout\n", USBDEVNAME(sc->sc_bus.bdev));
563 return (USBD_IOERROR);
564 }
565
566 /* Enable interrupts */
567 DPRINTFN(1,("ehci_init: enabling\n"));
568 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
569
570 return (USBD_NORMAL_COMPLETION);
571
572 #if 0
573 bad2:
574 ehci_free_sqh(sc, sc->sc_async_head);
575 #endif
576 bad1:
577 usb_freemem(&sc->sc_dmatag, &sc->sc_fldma);
578 usb_dma_tag_finish(&sc->sc_dmatag);
579 return (err);
580 }
581
582 int
583 ehci_intr(void *v)
584 {
585 ehci_softc_t *sc = v;
586
587 if (sc == NULL || sc->sc_dying)
588 return (0);
589
590 /* If we get an interrupt while polling, then just ignore it. */
591 if (sc->sc_bus.use_polling) {
592 u_int32_t intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
593
594 if (intrs)
595 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
596 #ifdef DIAGNOSTIC
597 DPRINTFN(16, ("ehci_intr: ignored interrupt while polling\n"));
598 #endif
599 return (0);
600 }
601
602 return (ehci_intr1(sc));
603 }
604
605 Static int
606 ehci_intr1(ehci_softc_t *sc)
607 {
608 u_int32_t intrs, eintrs;
609
610 DPRINTFN(20,("ehci_intr1: enter\n"));
611
612 /* In case the interrupt occurs before initialization has completed. */
613 if (sc == NULL) {
614 #ifdef DIAGNOSTIC
615 printf("ehci_intr1: sc == NULL\n");
616 #endif
617 return (0);
618 }
619
620 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
621 if (!intrs)
622 return (0);
623
624 eintrs = intrs & sc->sc_eintrs;
625 DPRINTFN(7, ("ehci_intr1: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
626 sc, (u_int)intrs, EOREAD4(sc, EHCI_USBSTS),
627 (u_int)eintrs));
628 if (!eintrs)
629 return (0);
630
631 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
632 sc->sc_bus.intr_context++;
633 sc->sc_bus.no_intrs++;
634 if (eintrs & EHCI_STS_IAA) {
635 DPRINTF(("ehci_intr1: door bell\n"));
636 wakeup(&sc->sc_async_head);
637 eintrs &= ~EHCI_STS_IAA;
638 }
639 if (eintrs & (EHCI_STS_INT | EHCI_STS_ERRINT)) {
640 DPRINTFN(5,("ehci_intr1: %s %s\n",
641 eintrs & EHCI_STS_INT ? "INT" : "",
642 eintrs & EHCI_STS_ERRINT ? "ERRINT" : ""));
643 usb_schedsoftintr(&sc->sc_bus);
644 eintrs &= ~(EHCI_STS_INT | EHCI_STS_ERRINT);
645 }
646 if (eintrs & EHCI_STS_HSE) {
647 printf("%s: unrecoverable error, controller halted\n",
648 USBDEVNAME(sc->sc_bus.bdev));
649 /* XXX what else */
650 }
651 if (eintrs & EHCI_STS_PCD) {
652 ehci_pcd(sc, sc->sc_intrxfer);
653 eintrs &= ~EHCI_STS_PCD;
654 }
655
656 sc->sc_bus.intr_context--;
657
658 if (eintrs != 0) {
659 /* Block unprocessed interrupts. */
660 sc->sc_eintrs &= ~eintrs;
661 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
662 printf("%s: blocking intrs 0x%x\n",
663 USBDEVNAME(sc->sc_bus.bdev), eintrs);
664 }
665
666 return (1);
667 }
668
669
670 void
671 ehci_pcd(ehci_softc_t *sc, usbd_xfer_handle xfer)
672 {
673 usbd_pipe_handle pipe;
674 u_char *p;
675 int i, m;
676
677 if (xfer == NULL) {
678 /* Just ignore the change. */
679 return;
680 }
681
682 pipe = xfer->pipe;
683
684 p = xfer->hcbuffer;
685 m = min(sc->sc_noport, xfer->length * 8 - 1);
686 memset(p, 0, xfer->length);
687 for (i = 1; i <= m; i++) {
688 /* Pick out CHANGE bits from the status reg. */
689 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR)
690 p[i/8] |= 1 << (i%8);
691 }
692 DPRINTF(("ehci_pcd: change=0x%02x\n", *p));
693 xfer->actlen = xfer->length;
694 xfer->status = USBD_NORMAL_COMPLETION;
695
696 usb_transfer_complete_dma(xfer, &sc->sc_dmatag, &EXFER(xfer)->dmabuf);
697 }
698
699 void
700 ehci_softintr(void *v)
701 {
702 ehci_softc_t *sc = v;
703 struct ehci_xfer *ex, *nextex;
704
705 DPRINTFN(10,("%s: ehci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev),
706 sc->sc_bus.intr_context));
707
708 sc->sc_bus.intr_context++;
709
710 /*
711 * The only explanation I can think of for why EHCI is as brain dead
712 * as UHCI interrupt-wise is that Intel was involved in both.
713 * An interrupt just tells us that something is done, we have no
714 * clue what, so we need to scan through all active transfers. :-(
715 */
716 for (ex = LIST_FIRST(&sc->sc_intrhead); ex; ex = nextex) {
717 nextex = LIST_NEXT(ex, inext);
718 ehci_check_intr(sc, ex);
719 }
720
721 /* Schedule a callout to catch any dropped transactions. */
722 if ((sc->sc_flags & EHCI_SCFLG_LOSTINTRBUG) &&
723 !LIST_EMPTY(&sc->sc_intrhead))
724 usb_callout(sc->sc_tmo_intrlist, hz / 5, ehci_intrlist_timeout,
725 sc);
726
727 /* Schedule a callout to catch any dropped transactions. */
728 if ((sc->sc_flags & EHCIF_DROPPED_INTR_WORKAROUND) &&
729 !LIST_EMPTY(&sc->sc_intrhead))
730 usb_callout(sc->sc_tmo_intrlist, hz,
731 ehci_intrlist_timeout, sc);
732
733 #ifdef USB_USE_SOFTINTR
734 if (sc->sc_softwake) {
735 sc->sc_softwake = 0;
736 wakeup(&sc->sc_softwake);
737 }
738 #endif /* USB_USE_SOFTINTR */
739
740 sc->sc_bus.intr_context--;
741 }
742
743 /* Check for an interrupt. */
744 void
745 ehci_check_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
746 {
747 ehci_soft_qtd_t *sqtd, *lsqtd;
748 u_int32_t status;
749
750 DPRINTFN(/*15*/2, ("ehci_check_intr: ex=%p\n", ex));
751
752 if (ex->sqtdstart == NULL) {
753 printf("ehci_check_intr: sqtdstart=NULL\n");
754 return;
755 }
756 lsqtd = ex->sqtdend;
757 #ifdef DIAGNOSTIC
758 if (lsqtd == NULL) {
759 printf("ehci_check_intr: lsqtd==0\n");
760 return;
761 }
762 #endif
763 /*
764 * If the last TD is still active we need to check whether there
765 * is a an error somewhere in the middle, or whether there was a
766 * short packet (SPD and not ACTIVE).
767 */
768 EHCI_SQTD_SYNC(sc, lsqtd, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
769 if (le32toh(lsqtd->qtd.qtd_status) & EHCI_QTD_ACTIVE) {
770 DPRINTFN(12, ("ehci_check_intr: active ex=%p\n", ex));
771 for (sqtd = ex->sqtdstart; sqtd != lsqtd; sqtd=sqtd->nextqtd) {
772 EHCI_SQTD_SYNC(sc, sqtd,
773 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
774 status = le32toh(sqtd->qtd.qtd_status);
775 /* If there's an active QTD the xfer isn't done. */
776 if (status & EHCI_QTD_ACTIVE)
777 break;
778 /* Any kind of error makes the xfer done. */
779 if (status & EHCI_QTD_HALTED)
780 goto done;
781 /* We want short packets, and it is short: it's done */
782 if (EHCI_QTD_GET_BYTES(status) != 0)
783 goto done;
784 }
785 DPRINTFN(12, ("ehci_check_intr: ex=%p std=%p still active\n",
786 ex, ex->sqtdstart));
787 return;
788 }
789 done:
790 DPRINTFN(12, ("ehci_check_intr: ex=%p done\n", ex));
791 usb_uncallout(ex->xfer.timeout_handle, ehci_timeout, ex);
792 usb_rem_task(ex->xfer.pipe->device, &ex->abort_task);
793 ehci_idone(ex);
794 }
795
796 void
797 ehci_idone(struct ehci_xfer *ex)
798 {
799 usbd_xfer_handle xfer = &ex->xfer;
800 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
801 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
802 ehci_soft_qtd_t *sqtd, *lsqtd;
803 u_int32_t status = 0, nstatus = 0;
804 ehci_physaddr_t nextphys, altnextphys;
805 ehci_physaddr_t stdma;
806 int actlen, cerr;
807
808 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p\n", ex));
809 #ifdef DIAGNOSTIC
810 {
811 int s = splhigh();
812 if (ex->isdone) {
813 splx(s);
814 #ifdef EHCI_DEBUG
815 printf("ehci_idone: ex is done!\n ");
816 ehci_dump_exfer(ex);
817 #else
818 printf("ehci_idone: ex=%p is done!\n", ex);
819 #endif
820 return;
821 }
822 ex->isdone = 1;
823 splx(s);
824 }
825 #endif
826
827 if (xfer->status == USBD_CANCELLED ||
828 xfer->status == USBD_TIMEOUT) {
829 DPRINTF(("ehci_idone: aborted xfer=%p\n", xfer));
830 return;
831 }
832
833 #ifdef EHCI_DEBUG
834 DPRINTFN(/*10*/2, ("ehci_idone: xfer=%p, pipe=%p ready\n", xfer, epipe));
835 if (ehcidebug > 10)
836 ehci_dump_sqtds(ex->sqtdstart);
837 #endif
838
839 /*
840 * Make sure that the QH overlay qTD does not reference any
841 * of the qTDs we are about to free. This is probably only
842 * necessary if the transfer is marked as HALTED.
843 */
844 EHCI_SQH_SYNC(sc, epipe->sqh,
845 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
846 nextphys = EHCI_LINK_ADDR(le32toh(epipe->sqh->qh.qh_qtd.qtd_next));
847 altnextphys =
848 EHCI_LINK_ADDR(le32toh(epipe->sqh->qh.qh_qtd.qtd_altnext));
849 for (sqtd = ex->sqtdstart; sqtd != ex->sqtdend->nextqtd;
850 sqtd = sqtd->nextqtd) {
851 stdma = EHCI_SQTD_DMAADDR(sqtd);
852 if (stdma == nextphys) {
853 epipe->sqh->qh.qh_qtd.qtd_next =
854 htole32(EHCI_SQTD_DMAADDR(ex->sqtdend->nextqtd));
855 DPRINTFN(4, ("ehci_idone: updated overlay next ptr\n"));
856 EHCI_SQH_SYNC(sc, epipe->sqh,
857 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
858
859 }
860 if (stdma == altnextphys) {
861 DPRINTFN(4,
862 ("ehci_idone: updated overlay altnext ptr\n"));
863 epipe->sqh->qh.qh_qtd.qtd_altnext =
864 htole32(EHCI_SQTD_DMAADDR(ex->sqtdend->nextqtd));
865 EHCI_SQH_SYNC(sc, epipe->sqh,
866 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
867 }
868 }
869
870 /* The transfer is done, compute actual length and status. */
871 lsqtd = ex->sqtdend;
872 actlen = 0;
873 for (sqtd = ex->sqtdstart; sqtd != lsqtd->nextqtd; sqtd=sqtd->nextqtd) {
874 EHCI_SQTD_SYNC(sc, sqtd,
875 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
876 nstatus = le32toh(sqtd->qtd.qtd_status);
877 if (nstatus & EHCI_QTD_ACTIVE)
878 break;
879
880 status = nstatus;
881 if (EHCI_QTD_GET_PID(status) != EHCI_QTD_PID_SETUP)
882 actlen += sqtd->len - EHCI_QTD_GET_BYTES(status);
883 }
884
885 cerr = EHCI_QTD_GET_CERR(status);
886 DPRINTFN(/*10*/2, ("ehci_idone: len=%d, actlen=%d, cerr=%d, "
887 "status=0x%x\n", xfer->length, actlen, cerr, status));
888 xfer->actlen = actlen;
889 if ((status & EHCI_QTD_HALTED) != 0) {
890 #ifdef EHCI_DEBUG
891 char sbuf[128];
892
893 bitmask_snprintf((u_int32_t)status,
894 "\20\7HALTED\6BUFERR\5BABBLE\4XACTERR"
895 "\3MISSED\2SPLIT\1PING", sbuf, sizeof(sbuf));
896
897 DPRINTFN(2,
898 ("ehci_idone: error, addr=%d, endpt=0x%02x, "
899 "status 0x%s\n",
900 xfer->pipe->device->address,
901 xfer->pipe->endpoint->edesc->bEndpointAddress,
902 sbuf));
903 if (ehcidebug > 2) {
904 ehci_dump_sqh(epipe->sqh);
905 ehci_dump_sqtds(ex->sqtdstart);
906 }
907 #endif
908 /* low&full speed has an extra error flag */
909 if (EHCI_QH_GET_EPS(epipe->sqh->qh.qh_endp) !=
910 EHCI_QH_SPEED_HIGH)
911 status &= EHCI_QTD_STATERRS | EHCI_QTD_PINGSTATE;
912 else
913 status &= EHCI_QTD_STATERRS;
914 if (status == 0) /* no other errors means a stall */
915 xfer->status = USBD_STALLED;
916 else
917 xfer->status = USBD_IOERROR; /* more info XXX */
918 /* XXX need to reset TT on missed microframe */
919 if (status & EHCI_QTD_MISSEDMICRO) {
920 printf("%s: missed microframe, TT reset not "
921 "implemented, hub might be inoperational\n",
922 USBDEVNAME(sc->sc_bus.bdev));
923 }
924 } else {
925 xfer->status = USBD_NORMAL_COMPLETION;
926 }
927
928 usb_transfer_complete_dma(xfer, &sc->sc_dmatag, &EXFER(xfer)->dmabuf);
929 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p done\n", ex));
930 }
931
932 /*
933 * Wait here until controller claims to have an interrupt.
934 * Then call ehci_intr and return. Use timeout to avoid waiting
935 * too long.
936 */
937 void
938 ehci_waitintr(ehci_softc_t *sc, usbd_xfer_handle xfer)
939 {
940 int timo;
941 u_int32_t intrs;
942
943 xfer->status = USBD_IN_PROGRESS;
944 for (timo = xfer->timeout; timo >= 0; timo--) {
945 usb_delay_ms(&sc->sc_bus, 1);
946 if (sc->sc_dying)
947 break;
948 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)) &
949 sc->sc_eintrs;
950 DPRINTFN(15,("ehci_waitintr: 0x%04x\n", intrs));
951 #ifdef EHCI_DEBUG
952 if (ehcidebug > 15)
953 ehci_dump_regs(sc);
954 #endif
955 if (intrs) {
956 ehci_intr1(sc);
957 if (xfer->status != USBD_IN_PROGRESS)
958 return;
959 }
960 }
961
962 /* Timeout */
963 DPRINTF(("ehci_waitintr: timeout\n"));
964 xfer->status = USBD_TIMEOUT;
965 usb_transfer_complete_dma(xfer, &sc->sc_dmatag, &EXFER(xfer)->dmabuf);
966 /* XXX should free TD */
967 }
968
969 void
970 ehci_poll(struct usbd_bus *bus)
971 {
972 ehci_softc_t *sc = (ehci_softc_t *)bus;
973 #ifdef EHCI_DEBUG
974 static int last;
975 int new;
976 new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
977 if (new != last) {
978 DPRINTFN(10,("ehci_poll: intrs=0x%04x\n", new));
979 last = new;
980 }
981 #endif
982
983 if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs)
984 ehci_intr1(sc);
985 }
986
987 int
988 ehci_detach(struct ehci_softc *sc, int flags)
989 {
990 int rv = 0;
991 usbd_xfer_handle xfer;
992
993 #if defined(__NetBSD__) || defined(__OpenBSD__)
994 if (sc->sc_child != NULL)
995 rv = config_detach(sc->sc_child, flags);
996
997 if (rv != 0)
998 return (rv);
999 #else
1000 sc->sc_dying = 1;
1001 #endif
1002
1003 if (sc->sc_bus.methods == NULL)
1004 return (rv); /* attach has been aborted */
1005
1006 #if defined(__NetBSD__) || defined(__OpenBSD__)
1007 /* Don't touch hardware if it has already been gone. */
1008 if ((flags & DETACH_FORCE) == 0)
1009 #endif
1010 {
1011 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1012 EOWRITE4(sc, EHCI_USBCMD, 0);
1013 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
1014 }
1015 usb_uncallout(sc->sc_tmo_intrlist, ehci_intrlist_timeout, sc);
1016
1017 #if defined(__NetBSD__) || defined(__OpenBSD__)
1018 if (sc->sc_powerhook != NULL)
1019 powerhook_disestablish(sc->sc_powerhook);
1020 if (sc->sc_shutdownhook != NULL)
1021 shutdownhook_disestablish(sc->sc_shutdownhook);
1022 #endif
1023
1024 usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */
1025
1026 usb_freemem(&sc->sc_dmatag, &sc->sc_fldma);
1027 /* XXX free other data structures XXX */
1028
1029 while ((xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers)) != NULL) {
1030 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
1031 usb_clean_buffer_dma(&sc->sc_dmatag, &EXFER(xfer)->dmabuf);
1032 free(xfer, M_USB);
1033 }
1034 ehci_free_desc_chunks(sc, &sc->sc_sqh_chunks);
1035 ehci_free_desc_chunks(sc, &sc->sc_sqtd_chunks);
1036 usb_dma_tag_finish(&sc->sc_dmatag);
1037
1038 return (rv);
1039 }
1040
1041 Static void
1042 ehci_free_desc_chunks(ehci_softc_t *sc, struct ehci_mdescs *c)
1043 {
1044 struct ehci_mem_desc *em;
1045
1046 while ((em = SIMPLEQ_FIRST(c)) != NULL) {
1047 SIMPLEQ_REMOVE_HEAD(c, em_next);
1048 usb_freemem(&sc->sc_dmatag, &em->em_dma);
1049 }
1050 }
1051
1052 #if defined(__NetBSD__) || defined(__OpenBSD__)
1053 int
1054 ehci_activate(device_t self, enum devact act)
1055 {
1056 struct ehci_softc *sc = (struct ehci_softc *)self;
1057 int rv = 0;
1058
1059 switch (act) {
1060 case DVACT_ACTIVATE:
1061 return (EOPNOTSUPP);
1062
1063 case DVACT_DEACTIVATE:
1064 if (sc->sc_child != NULL)
1065 rv = config_deactivate(sc->sc_child);
1066 sc->sc_dying = 1;
1067 break;
1068 }
1069 return (rv);
1070 }
1071 #endif
1072
1073 /*
1074 * Handle suspend/resume.
1075 *
1076 * We need to switch to polling mode here, because this routine is
1077 * called from an interrupt context. This is all right since we
1078 * are almost suspended anyway.
1079 */
1080 void
1081 ehci_power(int why, void *v)
1082 {
1083 ehci_softc_t *sc = v;
1084 u_int32_t cmd, hcr;
1085 int s, i;
1086
1087 #ifdef EHCI_DEBUG
1088 DPRINTF(("ehci_power: sc=%p, why=%d\n", sc, why));
1089 if (ehcidebug > 0)
1090 ehci_dump_regs(sc);
1091 #endif
1092
1093 s = splhardusb();
1094 switch (why) {
1095 USB_PWR_CASE_SUSPEND:
1096 sc->sc_bus.use_polling++;
1097
1098 for (i = 1; i <= sc->sc_noport; i++) {
1099 cmd = EOREAD4(sc, EHCI_PORTSC(i));
1100 if ((cmd & EHCI_PS_PO) == 0 &&
1101 (cmd & EHCI_PS_PE) == EHCI_PS_PE)
1102 EOWRITE4(sc, EHCI_PORTSC(i),
1103 cmd | EHCI_PS_SUSP);
1104 }
1105
1106 sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
1107
1108 cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
1109 EOWRITE4(sc, EHCI_USBCMD, cmd);
1110
1111 for (i = 0; i < 100; i++) {
1112 hcr = EOREAD4(sc, EHCI_USBSTS) &
1113 (EHCI_STS_ASS | EHCI_STS_PSS);
1114 if (hcr == 0)
1115 break;
1116
1117 usb_delay_ms(&sc->sc_bus, 1);
1118 }
1119 if (hcr != 0) {
1120 printf("%s: reset timeout\n",
1121 USBDEVNAME(sc->sc_bus.bdev));
1122 }
1123
1124 cmd &= ~EHCI_CMD_RS;
1125 EOWRITE4(sc, EHCI_USBCMD, cmd);
1126
1127 for (i = 0; i < 100; i++) {
1128 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
1129 if (hcr == EHCI_STS_HCH)
1130 break;
1131
1132 usb_delay_ms(&sc->sc_bus, 1);
1133 }
1134 if (hcr != EHCI_STS_HCH) {
1135 printf("%s: config timeout\n",
1136 USBDEVNAME(sc->sc_bus.bdev));
1137 }
1138
1139 sc->sc_bus.use_polling--;
1140 break;
1141
1142 USB_PWR_CASE_RESUME:
1143 sc->sc_bus.use_polling++;
1144
1145 /* restore things in case the bios sucks */
1146 EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
1147 EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
1148 EOWRITE4(sc, EHCI_ASYNCLISTADDR,
1149 EHCI_SQH_DMAADDR(sc->sc_async_head) | EHCI_LINK_QH);
1150 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
1151
1152 hcr = 0;
1153 for (i = 1; i <= sc->sc_noport; i++) {
1154 cmd = EOREAD4(sc, EHCI_PORTSC(i));
1155 if ((cmd & EHCI_PS_PO) == 0 &&
1156 (cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP) {
1157 EOWRITE4(sc, EHCI_PORTSC(i),
1158 cmd | EHCI_PS_FPR);
1159 hcr = 1;
1160 }
1161 }
1162
1163 if (hcr) {
1164 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1165
1166 for (i = 1; i <= sc->sc_noport; i++) {
1167 cmd = EOREAD4(sc, EHCI_PORTSC(i));
1168 if ((cmd & EHCI_PS_PO) == 0 &&
1169 (cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)
1170 EOWRITE4(sc, EHCI_PORTSC(i),
1171 cmd & ~EHCI_PS_FPR);
1172 }
1173 }
1174
1175 EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
1176
1177 for (i = 0; i < 100; i++) {
1178 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
1179 if (hcr != EHCI_STS_HCH)
1180 break;
1181
1182 usb_delay_ms(&sc->sc_bus, 1);
1183 }
1184 if (hcr == EHCI_STS_HCH) {
1185 printf("%s: config timeout\n",
1186 USBDEVNAME(sc->sc_bus.bdev));
1187 }
1188
1189 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1190
1191 sc->sc_bus.use_polling--;
1192 break;
1193
1194 default:
1195 break;
1196 }
1197 splx(s);
1198
1199 #ifdef EHCI_DEBUG
1200 DPRINTF(("ehci_power: sc=%p\n", sc));
1201 if (ehcidebug > 0)
1202 ehci_dump_regs(sc);
1203 #endif
1204 }
1205
1206 /*
1207 * Shut down the controller when the system is going down.
1208 */
1209 void
1210 ehci_shutdown(void *v)
1211 {
1212 ehci_softc_t *sc = v;
1213
1214 DPRINTF(("ehci_shutdown: stopping the HC\n"));
1215 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */
1216 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
1217 }
1218
1219 Static usbd_status
1220 ehci_prealloc(struct ehci_softc *sc, struct ehci_xfer *exfer,
1221 size_t bufsize, int nseg)
1222 {
1223 int seglen, ntd;
1224 int s;
1225 int err;
1226
1227 /* (over) estimate needed number of TDs */
1228 seglen = 19520; /* 4096*5 - (4096*5 % maxp), when maxp = 976 */
1229 ntd = bufsize / seglen + nseg;
1230
1231 s = splusb();
1232 /* pre-allocate QDs */
1233 /* XXX ITDs */
1234 while (sc->sc_nfreeqtds < ntd) {
1235 DPRINTF(("%s: qhci_prealloc: need %d QTD (%d cur)\n",
1236 USBDEVNAME(sc->sc_bus.bdev), ntd, sc->sc_nfreeqtds));
1237 if ((err = ehci_grow_sqtd(sc)) != USBD_NORMAL_COMPLETION) {
1238 splx(s);
1239 return err;
1240 }
1241 }
1242 sc->sc_nfreeqtds -= ntd;
1243 splx(s);
1244
1245 exfer->rsvd_tds = ntd;
1246
1247 return USBD_NORMAL_COMPLETION;
1248 }
1249
1250 usbd_status
1251 ehci_allocm(struct usbd_bus *bus, usbd_xfer_handle xfer, void *buf, size_t size)
1252 {
1253 struct ehci_softc *sc = (struct ehci_softc *)bus;
1254 struct ehci_xfer *exfer = EXFER(xfer);
1255 usbd_status err;
1256
1257 if ((err = usb_alloc_buffer_dma(&sc->sc_dmatag, &exfer->dmabuf,
1258 buf, size, &xfer->hcbuffer)) == USBD_NORMAL_COMPLETION) {
1259 if ((xfer->rqflags & URQ_DEV_MAP_PREPARED) == 0 &&
1260 (err = ehci_prealloc(sc, exfer, size,
1261 USB_BUFFER_NSEGS(&exfer->dmabuf)))
1262 != USBD_NORMAL_COMPLETION) {
1263 usb_free_buffer_dma(&sc->sc_dmatag, &exfer->dmabuf,
1264 U_WAITOK);
1265 }
1266 }
1267 #ifdef EHCI_DEBUG
1268 if (err)
1269 printf("ehci_allocm: usb_alloc_buffer_dma()=%d\n", err);
1270 #endif
1271 return (err);
1272 }
1273
1274 void
1275 ehci_freem(struct usbd_bus *bus, usbd_xfer_handle xfer,
1276 enum usbd_waitflg waitflg)
1277 {
1278 struct ehci_softc *sc = (struct ehci_softc *)bus;
1279 struct ehci_xfer *exfer = EXFER(xfer);
1280 int s;
1281
1282 usb_free_buffer_dma(&sc->sc_dmatag, &exfer->dmabuf, waitflg);
1283
1284 /* XXX ITDs */
1285
1286 s = splusb();
1287 sc->sc_nfreeqtds += exfer->rsvd_tds;
1288 splx(s);
1289 exfer->rsvd_tds = 0;
1290 }
1291
1292 Static usbd_status
1293 ehci_map_alloc(usbd_xfer_handle xfer)
1294 {
1295 struct ehci_softc *sc = (struct ehci_softc *)xfer->device->bus;
1296 struct ehci_xfer *exfer = EXFER(xfer);
1297 usbd_status st;
1298
1299 st = usb_alloc_dma_resources(&sc->sc_dmatag, &exfer->dmabuf);
1300 if (st)
1301 return st;
1302
1303 if ((st = ehci_prealloc(sc, exfer, MAXPHYS, USB_DMA_NSEG))) {
1304 usb_free_dma_resources(&sc->sc_dmatag, &exfer->dmabuf);
1305 }
1306
1307 return st;
1308 }
1309
1310 Static void
1311 ehci_map_free(usbd_xfer_handle xfer)
1312 {
1313 struct ehci_softc *sc = (struct ehci_softc *)xfer->device->bus;
1314 struct ehci_xfer *exfer = EXFER(xfer);
1315 int s;
1316
1317 USB_KASSERT(xfer->rqflags & URQ_DEV_MAP_PREPARED);
1318
1319 usb_free_dma_resources(&sc->sc_dmatag, &exfer->dmabuf);
1320
1321 /* XXX ITDs */
1322
1323 s = splusb();
1324 sc->sc_nfreeqtds += exfer->rsvd_tds;
1325 splx(s);
1326 exfer->rsvd_tds = 0;
1327 }
1328
1329 Static void
1330 ehci_mapm(usbd_xfer_handle xfer, void *buf, size_t size)
1331 {
1332 struct ehci_softc *sc = (struct ehci_softc *)xfer->device->bus;
1333 struct ehci_xfer *exfer = EXFER(xfer);
1334
1335 usb_map_dma(&sc->sc_dmatag, &exfer->dmabuf, buf, size);
1336 }
1337
1338 Static void
1339 ehci_mapm_mbuf(usbd_xfer_handle xfer, struct mbuf *chain)
1340 {
1341 struct ehci_softc *sc = (struct ehci_softc *)xfer->device->bus;
1342 struct ehci_xfer *exfer = EXFER(xfer);
1343
1344 usb_map_mbuf_dma(&sc->sc_dmatag, &exfer->dmabuf, chain);
1345 }
1346
1347 Static void
1348 ehci_unmapm(usbd_xfer_handle xfer)
1349 {
1350 struct ehci_softc *sc = (struct ehci_softc *)xfer->device->bus;
1351 struct ehci_xfer *exfer = EXFER(xfer);
1352
1353 usb_unmap_dma(&sc->sc_dmatag, &exfer->dmabuf);
1354 }
1355
1356 usbd_xfer_handle
1357 ehci_allocx(struct usbd_bus *bus, usbd_pipe_handle pipe,
1358 enum usbd_waitflg waitflg)
1359 {
1360 struct ehci_softc *sc = (struct ehci_softc *)bus;
1361 usbd_xfer_handle xfer;
1362
1363 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
1364 if (xfer != NULL) {
1365 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
1366 #ifdef DIAGNOSTIC
1367 if (xfer->busy_free != XFER_FREE) {
1368 printf("ehci_allocx: xfer=%p not free, 0x%08x\n", xfer,
1369 xfer->busy_free);
1370 }
1371 #endif
1372 } else {
1373 xfer = malloc(sizeof(struct ehci_xfer), M_USB,
1374 waitflg == U_WAITOK ? M_WAITOK : M_NOWAIT);
1375 }
1376 if (xfer != NULL) {
1377 memset(xfer, 0, sizeof(struct ehci_xfer));
1378 usb_init_task(&EXFER(xfer)->abort_task, ehci_timeout_task,
1379 xfer);
1380 EXFER(xfer)->ehci_xfer_flags = 0;
1381 EXFER(xfer)->rsvd_tds = 0;
1382 #ifdef DIAGNOSTIC
1383 EXFER(xfer)->isdone = 1;
1384 xfer->busy_free = XFER_BUSY;
1385 #endif
1386 }
1387 return (xfer);
1388 }
1389
1390 void
1391 ehci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
1392 {
1393 struct ehci_softc *sc = (struct ehci_softc *)bus;
1394
1395 #ifdef DIAGNOSTIC
1396 if (xfer->busy_free != XFER_BUSY) {
1397 printf("ehci_freex: xfer=%p not busy, 0x%08x\n", xfer,
1398 xfer->busy_free);
1399 }
1400 xfer->busy_free = XFER_FREE;
1401 if (!EXFER(xfer)->isdone) {
1402 printf("ehci_freex: !isdone\n");
1403 }
1404 #endif
1405 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
1406 }
1407
1408 Static void
1409 ehci_device_clear_toggle(usbd_pipe_handle pipe)
1410 {
1411 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
1412 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
1413
1414 EHCI_SQH_SYNC(sc, epipe->sqh,
1415 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1416
1417 DPRINTF(("ehci_device_clear_toggle: epipe=%p status=0x%x\n",
1418 epipe, epipe->sqh->qh.qh_qtd.qtd_status));
1419 #ifdef USB_DEBUG
1420 if (ehcidebug)
1421 usbd_dump_pipe(pipe);
1422 #endif
1423 USB_KASSERT2((epipe->sqh->qh.qh_qtd.qtd_status &
1424 htole32(EHCI_QTD_ACTIVE)) == 0,
1425 ("ehci_device_clear_toggle: queue active"));
1426 epipe->sqh->qh.qh_qtd.qtd_status &= htole32(~EHCI_QTD_TOGGLE_MASK);
1427 EHCI_SQH_SYNC(sc, epipe->sqh,
1428 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1429 }
1430
1431 Static void
1432 ehci_noop(usbd_pipe_handle pipe)
1433 {
1434 }
1435
1436 #ifdef EHCI_DEBUG
1437 void
1438 ehci_dump_regs(ehci_softc_t *sc)
1439 {
1440 int i;
1441 printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n",
1442 EOREAD4(sc, EHCI_USBCMD),
1443 EOREAD4(sc, EHCI_USBSTS),
1444 EOREAD4(sc, EHCI_USBINTR));
1445 printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
1446 EOREAD4(sc, EHCI_FRINDEX),
1447 EOREAD4(sc, EHCI_CTRLDSSEGMENT),
1448 EOREAD4(sc, EHCI_PERIODICLISTBASE),
1449 EOREAD4(sc, EHCI_ASYNCLISTADDR));
1450 for (i = 1; i <= sc->sc_noport; i++)
1451 printf("port %d status=0x%08x\n", i,
1452 EOREAD4(sc, EHCI_PORTSC(i)));
1453 }
1454
1455 /*
1456 * Unused function - this is meant to be called from a kernel
1457 * debugger.
1458 */
1459 void
1460 ehci_dump()
1461 {
1462 ehci_dump_regs(theehci);
1463 }
1464
1465 void
1466 ehci_dump_link(ehci_link_t link, int type)
1467 {
1468 link = le32toh(link);
1469 printf("0x%08x", link);
1470 if (link & EHCI_LINK_TERMINATE)
1471 printf("<T>");
1472 else {
1473 printf("<");
1474 if (type) {
1475 switch (EHCI_LINK_TYPE(link)) {
1476 case EHCI_LINK_ITD: printf("ITD"); break;
1477 case EHCI_LINK_QH: printf("QH"); break;
1478 case EHCI_LINK_SITD: printf("SITD"); break;
1479 case EHCI_LINK_FSTN: printf("FSTN"); break;
1480 }
1481 }
1482 printf(">");
1483 }
1484 }
1485
1486 void
1487 ehci_dump_sqtds(ehci_soft_qtd_t *sqtd)
1488 {
1489 int i;
1490 u_int32_t stop;
1491
1492 stop = 0;
1493 for (i = 0; sqtd && i < 20 && !stop; sqtd = sqtd->nextqtd, i++) {
1494 ehci_dump_sqtd(sqtd);
1495 stop = sqtd->qtd.qtd_next & htole32(EHCI_LINK_TERMINATE);
1496 }
1497 if (sqtd)
1498 printf("dump aborted, too many TDs\n");
1499 }
1500
1501 void
1502 ehci_dump_sqtd(ehci_soft_qtd_t *sqtd)
1503 {
1504 printf("QTD(%p) at 0x%08x:\n", sqtd, EHCI_SQTD_DMAADDR(sqtd));
1505 ehci_dump_qtd(&sqtd->qtd);
1506 }
1507
1508 void
1509 ehci_dump_qtd(ehci_qtd_t *qtd)
1510 {
1511 u_int32_t s;
1512 char sbuf[128];
1513
1514 printf(" next="); ehci_dump_link(qtd->qtd_next, 0);
1515 printf(" altnext="); ehci_dump_link(qtd->qtd_altnext, 0);
1516 printf("\n");
1517 s = le32toh(qtd->qtd_status);
1518 bitmask_snprintf(EHCI_QTD_GET_STATUS(s),
1519 "\20\10ACTIVE\7HALTED\6BUFERR\5BABBLE\4XACTERR"
1520 "\3MISSED\2SPLIT\1PING", sbuf, sizeof(sbuf));
1521 printf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n",
1522 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s),
1523 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s));
1524 printf(" cerr=%d pid=%d stat=0x%s\n", EHCI_QTD_GET_CERR(s),
1525 EHCI_QTD_GET_PID(s), sbuf);
1526 for (s = 0; s < 5; s++)
1527 printf(" buffer[%d]=0x%08x\n", s, le32toh(qtd->qtd_buffer[s]));
1528 }
1529
1530 void
1531 ehci_dump_sqh(ehci_soft_qh_t *sqh)
1532 {
1533 ehci_qh_t *qh = &sqh->qh;
1534 u_int32_t endp, endphub;
1535
1536 printf("QH(%p) at 0x%08x:\n", sqh, EHCI_SQH_DMAADDR(sqh));
1537 printf(" sqtd=%p inactivesqtd=%p\n", sqh->sqtd, sqh->inactivesqtd);
1538 printf(" link="); ehci_dump_link(qh->qh_link, 1); printf("\n");
1539 endp = le32toh(qh->qh_endp);
1540 printf(" endp=0x%08x\n", endp);
1541 printf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n",
1542 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
1543 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp),
1544 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp));
1545 printf(" mpl=0x%x ctl=%d nrl=%d\n",
1546 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp),
1547 EHCI_QH_GET_NRL(endp));
1548 endphub = le32toh(qh->qh_endphub);
1549 printf(" endphub=0x%08x\n", endphub);
1550 printf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n",
1551 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub),
1552 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
1553 EHCI_QH_GET_MULT(endphub));
1554 printf(" curqtd="); ehci_dump_link(qh->qh_curqtd, 0); printf("\n");
1555 printf("Overlay qTD:\n");
1556 ehci_dump_qtd(&qh->qh_qtd);
1557 }
1558
1559 #ifdef DIAGNOSTIC
1560 Static void
1561 ehci_dump_exfer(struct ehci_xfer *ex)
1562 {
1563 printf("ehci_dump_exfer: ex=%p\n", ex);
1564 }
1565 #endif
1566 #endif
1567
1568 usbd_status
1569 ehci_open(usbd_pipe_handle pipe)
1570 {
1571 usbd_device_handle dev = pipe->device;
1572 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
1573 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1574 u_int8_t addr = dev->address;
1575 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
1576 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
1577 ehci_soft_qh_t *sqh;
1578 usbd_status err;
1579 int s;
1580 int ival, speed, naks;
1581 int hshubaddr, hshubport;
1582
1583 DPRINTFN(1, ("ehci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1584 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
1585
1586 if (dev->myhsport) {
1587 hshubaddr = dev->myhsport->parent->address;
1588 hshubport = dev->myhsport->portno;
1589 } else {
1590 hshubaddr = 0;
1591 hshubport = 0;
1592 }
1593
1594 if (sc->sc_dying)
1595 return (USBD_IOERROR);
1596
1597 if (addr == sc->sc_addr) {
1598 switch (ed->bEndpointAddress) {
1599 case USB_CONTROL_ENDPOINT:
1600 pipe->methods = &ehci_root_ctrl_methods;
1601 break;
1602 case UE_DIR_IN | EHCI_INTR_ENDPT:
1603 pipe->methods = &ehci_root_intr_methods;
1604 break;
1605 default:
1606 return (USBD_INVAL);
1607 }
1608 return (USBD_NORMAL_COMPLETION);
1609 }
1610
1611 /* XXX All this stuff is only valid for async. */
1612 switch (dev->speed) {
1613 case USB_SPEED_LOW: speed = EHCI_QH_SPEED_LOW; break;
1614 case USB_SPEED_FULL: speed = EHCI_QH_SPEED_FULL; break;
1615 case USB_SPEED_HIGH: speed = EHCI_QH_SPEED_HIGH; break;
1616 default: panic("ehci_open: bad device speed %d", dev->speed);
1617 }
1618 if (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_ISOCHRONOUS) {
1619 printf("%s: *** WARNING: opening low/full speed isoc device, "
1620 "this does not work yet.\n",
1621 USBDEVNAME(sc->sc_bus.bdev));
1622 DPRINTFN(1,("ehci_open: hshubaddr=%d hshubport=%d\n",
1623 hshubaddr, hshubport));
1624 return USBD_INVAL;
1625 }
1626
1627 naks = 8; /* XXX */
1628 sqh = ehci_alloc_sqh(sc);
1629 if (sqh == NULL)
1630 return (USBD_NOMEM);
1631 /* qh_link filled when the QH is added */
1632 sqh->qh.qh_endp = htole32(
1633 EHCI_QH_SET_ADDR(addr) |
1634 EHCI_QH_SET_ENDPT(UE_GET_ADDR(ed->bEndpointAddress)) |
1635 EHCI_QH_SET_EPS(speed) |
1636 (xfertype == UE_CONTROL ? EHCI_QH_DTC : 0) |
1637 EHCI_QH_SET_MPL(UE_MAXPKTSZ(ed)) |
1638 (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_CONTROL ?
1639 EHCI_QH_CTL : 0) |
1640 EHCI_QH_SET_NRL(naks)
1641 );
1642 sqh->qh.qh_endphub = htole32(
1643 EHCI_QH_SET_MULT(1) |
1644 EHCI_QH_SET_HUBA(hshubaddr) |
1645 EHCI_QH_SET_PORT(hshubport) |
1646 EHCI_QH_SET_CMASK(0x1c) |
1647 EHCI_QH_SET_SMASK(xfertype == UE_INTERRUPT ? 0x01 : 0)
1648 );
1649 sqh->qh.qh_curqtd = EHCI_NULL;
1650 /* The overlay qTD was already set up by ehci_alloc_sqh(). */
1651 sqh->qh.qh_qtd.qtd_status =
1652 htole32(EHCI_QTD_SET_TOGGLE(pipe->endpoint->savedtoggle));
1653
1654 epipe->sqh = sqh;
1655
1656 switch (xfertype) {
1657 case UE_CONTROL:
1658 err = usb_allocmem(&sc->sc_dmatag, sizeof(usb_device_request_t),
1659 0, &epipe->u.ctl.reqdma);
1660 #ifdef EHCI_DEBUG
1661 if (err)
1662 printf("ehci_open: usb_allocmem()=%d\n", err);
1663 #endif
1664 if (err)
1665 goto bad;
1666 pipe->methods = &ehci_device_ctrl_methods;
1667 s = splusb();
1668 ehci_add_qh(sc, sqh, sc->sc_async_head);
1669 splx(s);
1670 break;
1671 case UE_BULK:
1672 pipe->methods = &ehci_device_bulk_methods;
1673 s = splusb();
1674 ehci_add_qh(sc, sqh, sc->sc_async_head);
1675 splx(s);
1676 break;
1677 case UE_INTERRUPT:
1678 pipe->methods = &ehci_device_intr_methods;
1679 ival = pipe->interval;
1680 if (ival == USBD_DEFAULT_INTERVAL) {
1681 if (speed == EHCI_QH_SPEED_HIGH) {
1682 if (ed->bInterval > 16) {
1683 /*
1684 * illegal with high-speed, but there
1685 * were documentation bugs in the spec,
1686 * so be generous
1687 */
1688 ival = 256;
1689 } else
1690 ival = (1 << (ed->bInterval - 1)) / 8;
1691 } else
1692 ival = ed->bInterval;
1693 }
1694 err = ehci_device_setintr(sc, sqh, ival);
1695 if (err)
1696 goto bad;
1697 break;
1698 case UE_ISOCHRONOUS:
1699 pipe->methods = &ehci_device_isoc_methods;
1700 /* FALLTHROUGH */
1701 default:
1702 err = USBD_INVAL;
1703 goto bad;
1704 }
1705 return (USBD_NORMAL_COMPLETION);
1706
1707 bad:
1708 ehci_free_sqh(sc, sqh);
1709 return (err);
1710 }
1711
1712 /*
1713 * Add an ED to the schedule. Called at splusb().
1714 * If in the async schedule, it will always have a next.
1715 * If in the intr schedule it may not.
1716 */
1717 void
1718 ehci_add_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
1719 {
1720 SPLUSBCHECK;
1721
1722 sqh->next = head->next;
1723 sqh->prev = head;
1724 sqh->qh.qh_link = head->qh.qh_link;
1725 EHCI_SQH_SYNC(sc, sqh,
1726 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1727 head->next = sqh;
1728 if (sqh->next)
1729 sqh->next->prev = sqh;
1730 head->qh.qh_link = htole32(EHCI_SQH_DMAADDR(sqh) | EHCI_LINK_QH);
1731 EHCI_SQH_SYNC(sc, head,
1732 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1733
1734 #ifdef EHCI_DEBUG
1735 if (ehcidebug > 5) {
1736 printf("ehci_add_qh:\n");
1737 ehci_dump_sqh(sqh);
1738 }
1739 #endif
1740 }
1741
1742 /*
1743 * Remove an ED from the schedule. Called at splusb().
1744 * Will always have a 'next' if it's in the async list as it's circular.
1745 */
1746 void
1747 ehci_rem_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
1748 {
1749 SPLUSBCHECK;
1750 /* XXX */
1751 sqh->prev->qh.qh_link = sqh->qh.qh_link;
1752 EHCI_SQH_SYNC(sc, sqh->prev,
1753 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1754 sqh->prev->next = sqh->next;
1755 if (sqh->next)
1756 sqh->next->prev = sqh->prev;
1757 ehci_sync_hc(sc);
1758 }
1759
1760 /* Restart a QH following the addition of a qTD. */
1761 void
1762 ehci_activate_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qtd_t *sqtd)
1763 {
1764 ehci_physaddr_t qtddma;
1765
1766 EHCI_SQTD_SYNC(sc, sqtd, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1767
1768 USB_KASSERT2((sqtd->qtd.qtd_status & htole32(EHCI_QTD_ACTIVE)) == 0,
1769 ("ehci_activate_qh: already active"));
1770
1771 /*
1772 * When a QH is idle, the overlay qTD should be marked as not
1773 * halted and not active. This causes the host controller to
1774 * retrieve the real qTD on each pass (rather than just examinig
1775 * the overlay), so it will notice when we activate the qTD.
1776 */
1777 if (sqtd == sqh->sqtd) {
1778 /* Check that the hardware is in the state we expect. */
1779 qtddma = EHCI_SQTD_DMAADDR(sqtd);
1780 if (EHCI_LINK_ADDR(le32toh(sqh->qh.qh_qtd.qtd_next)) !=
1781 qtddma) {
1782 #ifdef EHCI_DEBUG
1783 printf("ehci_activate_qh: unexpected next ptr\n");
1784 ehci_dump_sqh(sqh);
1785 ehci_dump_sqtds(sqh->sqtd);
1786 #endif
1787 sqh->qh.qh_qtd.qtd_next = htole32(qtddma);
1788 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
1789 }
1790 /* Ensure the flags are correct. */
1791 sqh->qh.qh_qtd.qtd_status &= htole32(EHCI_QTD_PINGSTATE |
1792 EHCI_QTD_TOGGLE_MASK);
1793 EHCI_SQH_SYNC(sc, sqh,
1794 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1795 }
1796
1797 /* Now activate the qTD. */
1798 sqtd->qtd.qtd_status |= htole32(EHCI_QTD_ACTIVE);
1799 EHCI_SQTD_SYNC(sc, sqtd, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1800 }
1801
1802 /*
1803 * Ensure that the HC has released all references to the QH. We do this
1804 * by asking for a Async Advance Doorbell interrupt and then we wait for
1805 * the interrupt.
1806 * To make this easier we first obtain exclusive use of the doorbell.
1807 */
1808 void
1809 ehci_sync_hc(ehci_softc_t *sc)
1810 {
1811 int s, error;
1812
1813 if (sc->sc_dying) {
1814 DPRINTFN(2,("ehci_sync_hc: dying\n"));
1815 return;
1816 }
1817 DPRINTFN(2,("ehci_sync_hc: enter\n"));
1818 /* get doorbell */
1819 usb_lockmgr(&sc->sc_doorbell_lock, LK_EXCLUSIVE, NULL);
1820 s = splhardusb();
1821 /* ask for doorbell */
1822 EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | EHCI_CMD_IAAD);
1823 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1824 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
1825 error = tsleep(&sc->sc_async_head, PZERO, "ehcidi", hz); /* bell wait */
1826 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n",
1827 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS)));
1828 splx(s);
1829 /* release doorbell */
1830 usb_lockmgr(&sc->sc_doorbell_lock, LK_RELEASE, NULL);
1831 #ifdef DIAGNOSTIC
1832 if (error)
1833 printf("ehci_sync_hc: tsleep() = %d\n", error);
1834 #endif
1835 DPRINTFN(2,("ehci_sync_hc: exit\n"));
1836 }
1837
1838 /***********/
1839
1840 /*
1841 * Data structures and routines to emulate the root hub.
1842 */
1843 Static usb_device_descriptor_t ehci_devd = {
1844 USB_DEVICE_DESCRIPTOR_SIZE,
1845 UDESC_DEVICE, /* type */
1846 {0x00, 0x02}, /* USB version */
1847 UDCLASS_HUB, /* class */
1848 UDSUBCLASS_HUB, /* subclass */
1849 UDPROTO_HSHUBSTT, /* protocol */
1850 64, /* max packet */
1851 {0},{0},{0x00,0x01}, /* device id */
1852 1,2,0, /* string indicies */
1853 1 /* # of configurations */
1854 };
1855
1856 Static const usb_device_qualifier_t ehci_odevd = {
1857 USB_DEVICE_DESCRIPTOR_SIZE,
1858 UDESC_DEVICE_QUALIFIER, /* type */
1859 {0x00, 0x02}, /* USB version */
1860 UDCLASS_HUB, /* class */
1861 UDSUBCLASS_HUB, /* subclass */
1862 UDPROTO_FSHUB, /* protocol */
1863 64, /* max packet */
1864 1, /* # of configurations */
1865 0
1866 };
1867
1868 Static const usb_config_descriptor_t ehci_confd = {
1869 USB_CONFIG_DESCRIPTOR_SIZE,
1870 UDESC_CONFIG,
1871 {USB_CONFIG_DESCRIPTOR_SIZE +
1872 USB_INTERFACE_DESCRIPTOR_SIZE +
1873 USB_ENDPOINT_DESCRIPTOR_SIZE},
1874 1,
1875 1,
1876 0,
1877 UC_ATTR_MBO | UC_SELF_POWERED,
1878 0 /* max power */
1879 };
1880
1881 Static const usb_interface_descriptor_t ehci_ifcd = {
1882 USB_INTERFACE_DESCRIPTOR_SIZE,
1883 UDESC_INTERFACE,
1884 0,
1885 0,
1886 1,
1887 UICLASS_HUB,
1888 UISUBCLASS_HUB,
1889 UIPROTO_HSHUBSTT,
1890 0
1891 };
1892
1893 Static const usb_endpoint_descriptor_t ehci_endpd = {
1894 USB_ENDPOINT_DESCRIPTOR_SIZE,
1895 UDESC_ENDPOINT,
1896 UE_DIR_IN | EHCI_INTR_ENDPT,
1897 UE_INTERRUPT,
1898 {8, 0}, /* max packet */
1899 12
1900 };
1901
1902 Static const usb_hub_descriptor_t ehci_hubd = {
1903 USB_HUB_DESCRIPTOR_SIZE,
1904 UDESC_HUB,
1905 0,
1906 {0,0},
1907 0,
1908 0,
1909 {""},
1910 {""},
1911 };
1912
1913 Static int
1914 ehci_str(usb_string_descriptor_t *p, int l, const char *s)
1915 {
1916 int i;
1917
1918 if (l == 0)
1919 return (0);
1920 p->bLength = 2 * strlen(s) + 2;
1921 if (l == 1)
1922 return (1);
1923 p->bDescriptorType = UDESC_STRING;
1924 l -= 2;
1925 for (i = 0; s[i] && l > 1; i++, l -= 2)
1926 USETW2(p->bString[i], 0, s[i]);
1927 return (2*i+2);
1928 }
1929
1930 /*
1931 * Simulate a hardware hub by handling all the necessary requests.
1932 */
1933 Static usbd_status
1934 ehci_root_ctrl_transfer(usbd_xfer_handle xfer)
1935 {
1936 usbd_status err;
1937
1938 /* Insert last in queue. */
1939 err = usb_insert_transfer(xfer);
1940 if (err)
1941 return (err);
1942
1943 /* Pipe isn't running, start first */
1944 return (ehci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1945 }
1946
1947 Static usbd_status
1948 ehci_root_ctrl_start(usbd_xfer_handle xfer)
1949 {
1950 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
1951 usb_device_request_t *req;
1952 void *buf = NULL;
1953 int port, i;
1954 int s, len, value, index, l, totlen = 0;
1955 usb_port_status_t ps;
1956 usb_hub_descriptor_t hubd;
1957 usbd_status err;
1958 u_int32_t v;
1959
1960 if (sc->sc_dying)
1961 return (USBD_IOERROR);
1962
1963 #ifdef DIAGNOSTIC
1964 if (!(xfer->rqflags & URQ_REQUEST))
1965 /* XXX panic */
1966 return (USBD_INVAL);
1967 #endif
1968 req = &xfer->request;
1969
1970 DPRINTFN(4,("ehci_root_ctrl_start: type=0x%02x request=%02x\n",
1971 req->bmRequestType, req->bRequest));
1972
1973 len = UGETW(req->wLength);
1974 value = UGETW(req->wValue);
1975 index = UGETW(req->wIndex);
1976
1977 if (len != 0)
1978 buf = xfer->hcbuffer;
1979
1980 #define C(x,y) ((x) | ((y) << 8))
1981 switch(C(req->bRequest, req->bmRequestType)) {
1982 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
1983 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
1984 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
1985 /*
1986 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
1987 * for the integrated root hub.
1988 */
1989 break;
1990 case C(UR_GET_CONFIG, UT_READ_DEVICE):
1991 if (len > 0) {
1992 *(u_int8_t *)buf = sc->sc_conf;
1993 totlen = 1;
1994 }
1995 break;
1996 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
1997 DPRINTFN(8,("ehci_root_ctrl_start: wValue=0x%04x\n", value));
1998 if (len == 0)
1999 break;
2000 switch(value >> 8) {
2001 case UDESC_DEVICE:
2002 if ((value & 0xff) != 0) {
2003 err = USBD_IOERROR;
2004 goto ret;
2005 }
2006 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2007 USETW(ehci_devd.idVendor, sc->sc_id_vendor);
2008 memcpy(buf, &ehci_devd, l);
2009 break;
2010 /*
2011 * We can't really operate at another speed, but the spec says
2012 * we need this descriptor.
2013 */
2014 case UDESC_DEVICE_QUALIFIER:
2015 if ((value & 0xff) != 0) {
2016 err = USBD_IOERROR;
2017 goto ret;
2018 }
2019 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2020 memcpy(buf, &ehci_odevd, l);
2021 break;
2022 /*
2023 * We can't really operate at another speed, but the spec says
2024 * we need this descriptor.
2025 */
2026 case UDESC_OTHER_SPEED_CONFIGURATION:
2027 case UDESC_CONFIG:
2028 if ((value & 0xff) != 0) {
2029 err = USBD_IOERROR;
2030 goto ret;
2031 }
2032 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2033 memcpy(buf, &ehci_confd, l);
2034 ((usb_config_descriptor_t *)buf)->bDescriptorType =
2035 value >> 8;
2036 buf = (char *)buf + l;
2037 len -= l;
2038 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2039 totlen += l;
2040 memcpy(buf, &ehci_ifcd, l);
2041 buf = (char *)buf + l;
2042 len -= l;
2043 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2044 totlen += l;
2045 memcpy(buf, &ehci_endpd, l);
2046 break;
2047 case UDESC_STRING:
2048 *(u_int8_t *)buf = 0;
2049 totlen = 1;
2050 switch (value & 0xff) {
2051 case 0: /* Language table */
2052 if (len > 0)
2053 *(u_int8_t *)buf = 4;
2054 if (len >= 4) {
2055 USETW(((usb_string_descriptor_t *)buf)->bString[0], 0x0409);
2056 totlen = 4;
2057 }
2058 break;
2059 case 1: /* Vendor */
2060 totlen = ehci_str(buf, len, sc->sc_vendor);
2061 break;
2062 case 2: /* Product */
2063 totlen = ehci_str(buf, len, "EHCI root hub");
2064 break;
2065 }
2066 break;
2067 default:
2068 err = USBD_IOERROR;
2069 goto ret;
2070 }
2071 break;
2072 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2073 if (len > 0) {
2074 *(u_int8_t *)buf = 0;
2075 totlen = 1;
2076 }
2077 break;
2078 case C(UR_GET_STATUS, UT_READ_DEVICE):
2079 if (len > 1) {
2080 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2081 totlen = 2;
2082 }
2083 break;
2084 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2085 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2086 if (len > 1) {
2087 USETW(((usb_status_t *)buf)->wStatus, 0);
2088 totlen = 2;
2089 }
2090 break;
2091 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2092 if (value >= USB_MAX_DEVICES) {
2093 err = USBD_IOERROR;
2094 goto ret;
2095 }
2096 sc->sc_addr = value;
2097 break;
2098 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2099 if (value != 0 && value != 1) {
2100 err = USBD_IOERROR;
2101 goto ret;
2102 }
2103 sc->sc_conf = value;
2104 break;
2105 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2106 break;
2107 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2108 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2109 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2110 err = USBD_IOERROR;
2111 goto ret;
2112 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2113 break;
2114 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2115 break;
2116 /* Hub requests */
2117 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2118 break;
2119 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2120 DPRINTFN(4, ("ehci_root_ctrl_start: UR_CLEAR_PORT_FEATURE "
2121 "port=%d feature=%d\n",
2122 index, value));
2123 if (index < 1 || index > sc->sc_noport) {
2124 err = USBD_IOERROR;
2125 goto ret;
2126 }
2127 port = EHCI_PORTSC(index);
2128 v = EOREAD4(sc, port);
2129 DPRINTFN(4, ("ehci_root_ctrl_start: portsc=0x%08x\n", v));
2130 v &= ~EHCI_PS_CLEAR;
2131 switch(value) {
2132 case UHF_PORT_ENABLE:
2133 EOWRITE4(sc, port, v &~ EHCI_PS_PE);
2134 break;
2135 case UHF_PORT_SUSPEND:
2136 EOWRITE4(sc, port, v &~ EHCI_PS_SUSP);
2137 break;
2138 case UHF_PORT_POWER:
2139 if (sc->sc_hasppc)
2140 EOWRITE4(sc, port, v &~ EHCI_PS_PP);
2141 break;
2142 case UHF_PORT_TEST:
2143 DPRINTFN(2,("ehci_root_ctrl_start: clear port test "
2144 "%d\n", index));
2145 break;
2146 case UHF_PORT_INDICATOR:
2147 DPRINTFN(2,("ehci_root_ctrl_start: clear port ind "
2148 "%d\n", index));
2149 EOWRITE4(sc, port, v &~ EHCI_PS_PIC);
2150 break;
2151 case UHF_C_PORT_CONNECTION:
2152 EOWRITE4(sc, port, v | EHCI_PS_CSC);
2153 break;
2154 case UHF_C_PORT_ENABLE:
2155 EOWRITE4(sc, port, v | EHCI_PS_PEC);
2156 break;
2157 case UHF_C_PORT_SUSPEND:
2158 /* how? */
2159 break;
2160 case UHF_C_PORT_OVER_CURRENT:
2161 EOWRITE4(sc, port, v | EHCI_PS_OCC);
2162 break;
2163 case UHF_C_PORT_RESET:
2164 sc->sc_isreset[index] = 0;
2165 break;
2166 default:
2167 err = USBD_IOERROR;
2168 goto ret;
2169 }
2170 #if 0
2171 switch(value) {
2172 case UHF_C_PORT_CONNECTION:
2173 case UHF_C_PORT_ENABLE:
2174 case UHF_C_PORT_SUSPEND:
2175 case UHF_C_PORT_OVER_CURRENT:
2176 case UHF_C_PORT_RESET:
2177 default:
2178 break;
2179 }
2180 #endif
2181 break;
2182 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2183 if (len == 0)
2184 break;
2185 if ((value & 0xff) != 0) {
2186 err = USBD_IOERROR;
2187 goto ret;
2188 }
2189 hubd = ehci_hubd;
2190 hubd.bNbrPorts = sc->sc_noport;
2191 v = EOREAD4(sc, EHCI_HCSPARAMS);
2192 USETW(hubd.wHubCharacteristics,
2193 EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH |
2194 EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS))
2195 ? UHD_PORT_IND : 0);
2196 hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */
2197 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2198 hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
2199 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2200 l = min(len, hubd.bDescLength);
2201 totlen = l;
2202 memcpy(buf, &hubd, l);
2203 break;
2204 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2205 if (len != 4) {
2206 err = USBD_IOERROR;
2207 goto ret;
2208 }
2209 memset(buf, 0, len); /* ? XXX */
2210 totlen = len;
2211 break;
2212 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2213 DPRINTFN(8,("ehci_root_ctrl_start: get port status i=%d\n",
2214 index));
2215 if (index < 1 || index > sc->sc_noport) {
2216 err = USBD_IOERROR;
2217 goto ret;
2218 }
2219 if (len != 4) {
2220 err = USBD_IOERROR;
2221 goto ret;
2222 }
2223 v = EOREAD4(sc, EHCI_PORTSC(index));
2224 DPRINTFN(8,("ehci_root_ctrl_start: port status=0x%04x\n",
2225 v));
2226 i = UPS_HIGH_SPEED;
2227 if (v & EHCI_PS_CS) i |= UPS_CURRENT_CONNECT_STATUS;
2228 if (v & EHCI_PS_PE) i |= UPS_PORT_ENABLED;
2229 if (v & EHCI_PS_SUSP) i |= UPS_SUSPEND;
2230 if (v & EHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR;
2231 if (v & EHCI_PS_PR) i |= UPS_RESET;
2232 if (v & EHCI_PS_PP) i |= UPS_PORT_POWER;
2233 USETW(ps.wPortStatus, i);
2234 i = 0;
2235 if (v & EHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS;
2236 if (v & EHCI_PS_PEC) i |= UPS_C_PORT_ENABLED;
2237 if (v & EHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR;
2238 if (sc->sc_isreset[index]) i |= UPS_C_PORT_RESET;
2239 USETW(ps.wPortChange, i);
2240 l = min(len, sizeof ps);
2241 memcpy(buf, &ps, l);
2242 totlen = l;
2243 break;
2244 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2245 err = USBD_IOERROR;
2246 goto ret;
2247 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2248 break;
2249 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2250 if (index < 1 || index > sc->sc_noport) {
2251 err = USBD_IOERROR;
2252 goto ret;
2253 }
2254 port = EHCI_PORTSC(index);
2255 v = EOREAD4(sc, port);
2256 DPRINTFN(4, ("ehci_root_ctrl_start: portsc=0x%08x\n", v));
2257 v &= ~EHCI_PS_CLEAR;
2258 switch(value) {
2259 case UHF_PORT_ENABLE:
2260 EOWRITE4(sc, port, v | EHCI_PS_PE);
2261 break;
2262 case UHF_PORT_SUSPEND:
2263 EOWRITE4(sc, port, v | EHCI_PS_SUSP);
2264 break;
2265 case UHF_PORT_RESET:
2266 DPRINTFN(5,("ehci_root_ctrl_start: reset port %d\n",
2267 index));
2268 if (EHCI_PS_IS_LOWSPEED(v)) {
2269 /* Low speed device, give up ownership. */
2270 ehci_disown(sc, index, 1);
2271 break;
2272 }
2273 /* Start reset sequence. */
2274 v &= ~ (EHCI_PS_PE | EHCI_PS_PR);
2275 EOWRITE4(sc, port, v | EHCI_PS_PR);
2276 /* Wait for reset to complete. */
2277 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
2278 if (sc->sc_dying) {
2279 err = USBD_IOERROR;
2280 goto ret;
2281 }
2282 /* Terminate reset sequence. */
2283 EOWRITE4(sc, port, v);
2284 /* Wait for HC to complete reset. */
2285 usb_delay_ms(&sc->sc_bus, EHCI_PORT_RESET_COMPLETE);
2286 if (sc->sc_dying) {
2287 err = USBD_IOERROR;
2288 goto ret;
2289 }
2290 v = EOREAD4(sc, port);
2291 DPRINTF(("ehci after reset, status=0x%08x\n", v));
2292 if (v & EHCI_PS_PR) {
2293 printf("%s: port reset timeout\n",
2294 USBDEVNAME(sc->sc_bus.bdev));
2295 return (USBD_TIMEOUT);
2296 }
2297 if (!(v & EHCI_PS_PE)) {
2298 /* Not a high speed device, give up ownership.*/
2299 ehci_disown(sc, index, 0);
2300 break;
2301 }
2302 sc->sc_isreset[index] = 1;
2303 DPRINTF(("ehci port %d reset, status = 0x%08x\n",
2304 index, v));
2305 break;
2306 case UHF_PORT_POWER:
2307 DPRINTFN(2,("ehci_root_ctrl_start: set port power "
2308 "%d (has PPC = %d)\n", index,
2309 sc->sc_hasppc));
2310 if (sc->sc_hasppc)
2311 EOWRITE4(sc, port, v | EHCI_PS_PP);
2312 break;
2313 case UHF_PORT_TEST:
2314 DPRINTFN(2,("ehci_root_ctrl_start: set port test "
2315 "%d\n", index));
2316 break;
2317 case UHF_PORT_INDICATOR:
2318 DPRINTFN(2,("ehci_root_ctrl_start: set port ind "
2319 "%d\n", index));
2320 EOWRITE4(sc, port, v | EHCI_PS_PIC);
2321 break;
2322 default:
2323 err = USBD_IOERROR;
2324 goto ret;
2325 }
2326 break;
2327 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
2328 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
2329 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
2330 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
2331 break;
2332 default:
2333 err = USBD_IOERROR;
2334 goto ret;
2335 }
2336 xfer->actlen = totlen;
2337 err = USBD_NORMAL_COMPLETION;
2338 ret:
2339 xfer->status = err;
2340 s = splusb();
2341 usb_transfer_complete(xfer);
2342 splx(s);
2343 return (USBD_IN_PROGRESS);
2344 }
2345
2346 void
2347 ehci_disown(ehci_softc_t *sc, int index, int lowspeed)
2348 {
2349 int port;
2350 u_int32_t v;
2351
2352 DPRINTF(("ehci_disown: index=%d lowspeed=%d\n", index, lowspeed));
2353 #ifdef DIAGNOSTIC
2354 if (sc->sc_npcomp != 0) {
2355 int i = (index-1) / sc->sc_npcomp;
2356 if (i >= sc->sc_ncomp)
2357 printf("%s: strange port\n",
2358 USBDEVNAME(sc->sc_bus.bdev));
2359 else
2360 printf("%s: handing over %s speed device on "
2361 "port %d to %s\n",
2362 USBDEVNAME(sc->sc_bus.bdev),
2363 lowspeed ? "low" : "full",
2364 index, USBDEVNAME(sc->sc_comps[i]->bdev));
2365 } else {
2366 printf("%s: npcomp == 0\n", USBDEVNAME(sc->sc_bus.bdev));
2367 }
2368 #endif
2369 port = EHCI_PORTSC(index);
2370 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
2371 EOWRITE4(sc, port, v | EHCI_PS_PO);
2372 }
2373
2374 /* Abort a root control request. */
2375 Static void
2376 ehci_root_ctrl_abort(usbd_xfer_handle xfer)
2377 {
2378 /* Nothing to do, all transfers are synchronous. */
2379 }
2380
2381 /* Close the root pipe. */
2382 Static void
2383 ehci_root_ctrl_close(usbd_pipe_handle pipe)
2384 {
2385 DPRINTF(("ehci_root_ctrl_close\n"));
2386 /* Nothing to do. */
2387 }
2388
2389 void
2390 ehci_root_intr_done(usbd_xfer_handle xfer)
2391 {
2392 }
2393
2394 Static usbd_status
2395 ehci_root_intr_transfer(usbd_xfer_handle xfer)
2396 {
2397 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2398 usbd_status err;
2399
2400 /* Insert last in queue. */
2401 err = usb_insert_transfer_dma(xfer, &sc->sc_dmatag,
2402 &EXFER(xfer)->dmabuf);
2403 if (err)
2404 return (err);
2405
2406 /* Pipe isn't running, start first */
2407 return (ehci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2408 }
2409
2410 Static usbd_status
2411 ehci_root_intr_start(usbd_xfer_handle xfer)
2412 {
2413 usbd_pipe_handle pipe = xfer->pipe;
2414 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2415
2416 if (sc->sc_dying)
2417 return (USBD_IOERROR);
2418
2419 sc->sc_intrxfer = xfer;
2420
2421 return (USBD_IN_PROGRESS);
2422 }
2423
2424 /* Abort a root interrupt request. */
2425 Static void
2426 ehci_root_intr_abort(usbd_xfer_handle xfer)
2427 {
2428 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
2429 int s;
2430
2431 if (xfer->pipe->intrxfer == xfer) {
2432 DPRINTF(("ehci_root_intr_abort: remove\n"));
2433 xfer->pipe->intrxfer = NULL;
2434 }
2435 xfer->status = USBD_CANCELLED;
2436 s = splusb();
2437 usb_transfer_complete_dma(xfer, &sc->sc_dmatag, &EXFER(xfer)->dmabuf);
2438 splx(s);
2439 }
2440
2441 /* Close the root pipe. */
2442 Static void
2443 ehci_root_intr_close(usbd_pipe_handle pipe)
2444 {
2445 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2446
2447 DPRINTF(("ehci_root_intr_close\n"));
2448
2449 sc->sc_intrxfer = NULL;
2450 }
2451
2452 void
2453 ehci_root_ctrl_done(usbd_xfer_handle xfer)
2454 {
2455 }
2456
2457 /************************/
2458
2459 ehci_soft_qh_t *
2460 ehci_alloc_sqh(ehci_softc_t *sc)
2461 {
2462 ehci_soft_qh_t *sqh;
2463 ehci_soft_qtd_t *sqtd;
2464 usbd_status err;
2465 int i, offs;
2466 usb_dma_t dma;
2467 struct ehci_mem_desc *em;
2468
2469 if (sc->sc_freeqhs == NULL) {
2470 DPRINTFN(2, ("ehci_alloc_sqh: allocating chunk\n"));
2471 err = usb_allocmem(&sc->sc_dmatag,
2472 EHCI_SQH_SIZE*EHCI_SQH_CHUNK + sizeof(struct ehci_mem_desc),
2473 EHCI_PAGE_SIZE, &dma);
2474 #ifdef EHCI_DEBUG
2475 if (err)
2476 printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err);
2477 #endif
2478 if (err)
2479 return (NULL);
2480 em = KERNADDR(&dma, EHCI_SQH_SIZE * EHCI_SQH_CHUNK);
2481 em->em_top = KERNADDR(&dma, 0);
2482 em->em_topdma = DMAADDR(&dma, 0);
2483 em->em_dma = dma;
2484 SIMPLEQ_INSERT_HEAD(&sc->sc_sqh_chunks, em, em_next);
2485 for(i = 0; i < EHCI_SQH_CHUNK; i++) {
2486 offs = i * EHCI_SQH_SIZE;
2487 sqh = KERNADDR(&dma, offs);
2488 sqh->eh_mdesc = em;
2489 sqh->next = sc->sc_freeqhs;
2490 sc->sc_freeqhs = sqh;
2491 }
2492 }
2493 /* Allocate the initial inactive sqtd. */
2494 sqtd = ehci_alloc_sqtd_norsv(sc);
2495 if (sqtd == NULL)
2496 return (NULL);
2497 sqtd->qtd.qtd_status = htole32(0);
2498 sqtd->qtd.qtd_next = EHCI_NULL;
2499 sqtd->qtd.qtd_altnext = EHCI_NULL;
2500
2501 sqh = sc->sc_freeqhs;
2502 sc->sc_freeqhs = sqh->next;
2503
2504 /* The overlay QTD should begin zeroed. */
2505 sqh->qh.qh_qtd.qtd_next = htole32(EHCI_SQTD_DMAADDR(sqtd));
2506 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
2507 sqh->qh.qh_qtd.qtd_status = 0;
2508 for (i = 0; i < EHCI_QTD_NBUFFERS; i++) {
2509 sqh->qh.qh_qtd.qtd_buffer[i] = 0;
2510 sqh->qh.qh_qtd.qtd_buffer_hi[i] = 0;
2511 }
2512 sqh->next = NULL;
2513 sqh->prev = NULL;
2514 sqh->sqtd = sqtd;
2515 sqh->inactivesqtd = sqtd;
2516 return (sqh);
2517 }
2518
2519 void
2520 ehci_free_sqh(ehci_softc_t *sc, ehci_soft_qh_t *sqh)
2521 {
2522 ehci_free_sqtd_norsv(sc, sqh->inactivesqtd);
2523 sqh->next = sc->sc_freeqhs;
2524 sc->sc_freeqhs = sqh;
2525 }
2526
2527 Static usbd_status
2528 ehci_grow_sqtd(ehci_softc_t *sc)
2529 {
2530 usb_dma_t dma;
2531 struct ehci_mem_desc *em;
2532 ehci_soft_qtd_t *sqtd;
2533 usbd_status err;
2534 int i, s, offs;
2535
2536 DPRINTFN(2, ("ehci_grow_sqtd: allocating chunk\n"));
2537 err = usb_allocmem(&sc->sc_dmatag,
2538 EHCI_SQTD_SIZE*EHCI_SQTD_CHUNK + sizeof(struct ehci_mem_desc),
2539 EHCI_PAGE_SIZE, &dma);
2540 #ifdef EHCI_DEBUG
2541 if (err)
2542 printf("ehci_grow_sqtd: usb_allocmem()=%d\n", err);
2543 #endif
2544 if (err)
2545 return (err);
2546 em = KERNADDR(&dma, EHCI_SQTD_SIZE * EHCI_SQTD_CHUNK);
2547 em->em_top = KERNADDR(&dma, 0);
2548 em->em_topdma = DMAADDR(&dma, 0);
2549 em->em_dma = dma;
2550 s = splusb();
2551 SIMPLEQ_INSERT_HEAD(&sc->sc_sqtd_chunks, em, em_next);
2552 for(i = 0; i < EHCI_SQTD_CHUNK; i++) {
2553 offs = i * EHCI_SQTD_SIZE;
2554 sqtd = KERNADDR(&dma, offs);
2555 sqtd->et_mdesc = em;
2556 sqtd->nextqtd = sc->sc_freeqtds;
2557 sc->sc_freeqtds = sqtd;
2558 sc->sc_nfreeqtds++;
2559 }
2560 splx(s);
2561
2562 return (USBD_NORMAL_COMPLETION);
2563 }
2564
2565 ehci_soft_qtd_t *
2566 ehci_alloc_sqtd(ehci_softc_t *sc)
2567 {
2568 ehci_soft_qtd_t *sqtd;
2569 int i;
2570 int s;
2571
2572 s = splusb();
2573
2574 #ifdef DIAGNOSTIC
2575 if (sc->sc_freeqtds == NULL)
2576 panic("ehci_alloc_sqtd: %p %d",
2577 sc->sc_freeqtds, sc->sc_nfreeqtds);
2578 #endif
2579 sqtd = sc->sc_freeqtds;
2580 sc->sc_freeqtds = sqtd->nextqtd;
2581 splx(s);
2582 sqtd->qtd.qtd_next = EHCI_NULL;
2583 sqtd->qtd.qtd_altnext = EHCI_NULL;
2584 sqtd->qtd.qtd_status = 0;
2585 for (i = 0; i < EHCI_QTD_NBUFFERS; i++) {
2586 sqtd->qtd.qtd_buffer[i] = 0;
2587 sqtd->qtd.qtd_buffer_hi[i] = 0;
2588 }
2589 sqtd->nextqtd = NULL;
2590 sqtd->xfer = NULL;
2591
2592 return (sqtd);
2593 }
2594
2595 Static ehci_soft_qtd_t *
2596 ehci_alloc_sqtd_norsv(ehci_softc_t *sc)
2597 {
2598 int s;
2599
2600 s = splusb();
2601 if (sc->sc_nfreeqtds < 1)
2602 if (ehci_grow_sqtd(sc))
2603 return (NULL);
2604 sc->sc_nfreeqtds--;
2605 splx(s);
2606 return (ehci_alloc_sqtd(sc));
2607 }
2608
2609 void
2610 ehci_free_sqtd(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd)
2611 {
2612 int s;
2613
2614 s = splusb();
2615 sqtd->nextqtd = sc->sc_freeqtds;
2616 sc->sc_freeqtds = sqtd;
2617 splx(s);
2618 }
2619
2620 Static void
2621 ehci_free_sqtd_norsv(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd)
2622 {
2623 int s;
2624
2625 ehci_free_sqtd(sc, sqtd);
2626 s = splusb();
2627 sc->sc_nfreeqtds++;
2628 splx(s);
2629 }
2630
2631 usbd_status
2632 ehci_alloc_sqtd_chain(struct ehci_pipe *epipe, ehci_softc_t *sc,
2633 int alen, int rd, usbd_xfer_handle xfer, ehci_soft_qtd_t *start,
2634 ehci_soft_qtd_t *newinactive, ehci_soft_qtd_t **sp, ehci_soft_qtd_t **ep)
2635 {
2636 ehci_soft_qtd_t *next, *cur;
2637 ehci_physaddr_t dataphys, nextphys;
2638 u_int32_t qtdstatus;
2639 int adj, len, curlen, mps, offset, pagelen, seg, segoff;
2640 int i, iscontrol, forceshort;
2641 struct usb_buffer_dma *ub = &EXFER(xfer)->dmabuf;
2642 bus_dma_segment_t *segs = USB_BUFFER_SEGS(ub);
2643 int nsegs = USB_BUFFER_NSEGS(ub);
2644
2645 DPRINTFN(alen<4*4096,("ehci_alloc_sqtd_chain: start len=%d\n", alen));
2646
2647 offset = 0;
2648 len = alen;
2649 iscontrol = (epipe->pipe.endpoint->edesc->bmAttributes & UE_XFERTYPE) ==
2650 UE_CONTROL;
2651 qtdstatus = EHCI_QTD_ACTIVE |
2652 EHCI_QTD_SET_PID(rd ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) |
2653 EHCI_QTD_SET_CERR(3)
2654 /* IOC set below */
2655 /* BYTES set below */
2656 ;
2657 mps = UE_MAXPKTSZ(epipe->pipe.endpoint->edesc);
2658 forceshort = ((xfer->flags & USBD_FORCE_SHORT_XFER) || len == 0) &&
2659 len % mps == 0;
2660 /*
2661 * The control transfer data stage always starts with a toggle of 1.
2662 * For other transfers we let the hardware track the toggle state.
2663 */
2664 if (iscontrol)
2665 qtdstatus |= EHCI_QTD_SET_TOGGLE(1);
2666
2667 if (start != NULL) {
2668 /*
2669 * If we are given a starting qTD, assume it is linked into
2670 * an active QH so be careful not to mark it active.
2671 */
2672 cur = start;
2673 *sp = cur;
2674 qtdstatus &= ~EHCI_QTD_ACTIVE;
2675 } else {
2676 cur = ehci_alloc_sqtd(sc);
2677 *sp = cur;
2678 if (cur == NULL)
2679 goto nomem;
2680 }
2681 seg = 0;
2682 segoff = 0;
2683 for (;;) {
2684 curlen = 0;
2685
2686 /* The EHCI hardware can handle at most 5 pages. */
2687 for (i = 0; i < EHCI_QTD_NBUFFERS && curlen < len; i++) {
2688 USB_KASSERT2(seg < nsegs,
2689 ("ehci_alloc_sqtd_chain: overrun"));
2690 dataphys = segs[seg].ds_addr + segoff;
2691 pagelen = segs[seg].ds_len - segoff;
2692 if (pagelen > len - curlen)
2693 pagelen = len - curlen;
2694 if (pagelen > EHCI_PAGE_SIZE -
2695 EHCI_PAGE_OFFSET(dataphys))
2696 pagelen = EHCI_PAGE_SIZE -
2697 EHCI_PAGE_OFFSET(dataphys);
2698 segoff += pagelen;
2699 if (segoff >= segs[seg].ds_len) {
2700 USB_KASSERT2(segoff == segs[seg].ds_len,
2701 ("ehci_alloc_sqtd_chain: overlap"));
2702 seg++;
2703 segoff = 0;
2704 }
2705
2706 cur->qtd.qtd_buffer[i] = htole32(dataphys);
2707 cur->qtd.qtd_buffer_hi[i] = 0;
2708 curlen += pagelen;
2709
2710 /*
2711 * Must stop if there is any gap before or after
2712 * the page boundary.
2713 */
2714 if (EHCI_PAGE_OFFSET(dataphys + pagelen) != 0)
2715 break;
2716 if (seg < nsegs && EHCI_PAGE_OFFSET(segoff +
2717 segs[seg].ds_addr) != 0)
2718 break;
2719 }
2720 /* Adjust down to a multiple of mps if not at the end. */
2721 if (curlen < len && curlen % mps != 0) {
2722 adj = curlen % mps;
2723 curlen -= adj;
2724 USB_KASSERT2(curlen > 0,
2725 ("ehci_alloc_sqtd_chain: need to copy"));
2726 segoff -= adj;
2727 if (segoff < 0) {
2728 seg--;
2729 segoff += segs[seg].ds_len;
2730 }
2731 USB_KASSERT2(seg >= 0 && segoff >= 0,
2732 ("ehci_alloc_sqtd_chain: adjust to mps"));
2733 }
2734
2735 len -= curlen;
2736
2737 if (len != 0 || forceshort) {
2738 next = ehci_alloc_sqtd(sc);
2739 if (next == NULL)
2740 goto nomem;
2741 nextphys = htole32(EHCI_SQTD_DMAADDR(next));
2742 } else {
2743 next = NULL;
2744 nextphys = EHCI_NULL;
2745 }
2746
2747 cur->nextqtd = next;
2748 cur->qtd.qtd_next = nextphys;
2749 /* Make sure to stop after a short transfer. */
2750 cur->qtd.qtd_altnext = htole32(EHCI_SQTD_DMAADDR(newinactive));
2751 cur->qtd.qtd_status =
2752 htole32(qtdstatus | EHCI_QTD_SET_BYTES(curlen));
2753 cur->xfer = xfer;
2754 cur->len = curlen;
2755 DPRINTFN(10,("ehci_alloc_sqtd_chain: curlen=%d\n", curlen));
2756 if (iscontrol) {
2757 /*
2758 * adjust the toggle based on the number of packets
2759 * in this qtd
2760 */
2761 if ((((curlen + mps - 1) / mps) & 1) || curlen == 0)
2762 qtdstatus ^= EHCI_QTD_TOGGLE_MASK;
2763 }
2764 qtdstatus |= EHCI_QTD_ACTIVE;
2765 if (len == 0) {
2766 if (!forceshort)
2767 break;
2768 forceshort = 0;
2769 }
2770 EHCI_SQTD_SYNC(sc, cur,
2771 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2772 DPRINTFN(10,("ehci_alloc_sqtd_chain: extend chain\n"));
2773 offset += curlen;
2774 cur = next;
2775 }
2776 cur->qtd.qtd_status |= htole32(EHCI_QTD_IOC);
2777 EHCI_SQTD_SYNC(sc, cur,
2778 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2779 *ep = cur;
2780
2781 DPRINTFN(10,("ehci_alloc_sqtd_chain: return sqtd=%p sqtdend=%p\n",
2782 *sp, *ep));
2783
2784 return (USBD_NORMAL_COMPLETION);
2785
2786 nomem:
2787 /* XXX free chain */
2788 DPRINTFN(-1,("ehci_alloc_sqtd_chain: no memory\n"));
2789 return (USBD_NOMEM);
2790 }
2791
2792 /* Free the chain starting at sqtd and end at the qTD before sqtdend */
2793 Static void
2794 ehci_free_sqtd_chain(ehci_softc_t *sc, ehci_soft_qh_t *sqh,
2795 ehci_soft_qtd_t *sqtd, ehci_soft_qtd_t *sqtdend)
2796 {
2797 ehci_soft_qtd_t *p, **prevp;
2798 int i;
2799
2800 DPRINTFN(10,("ehci_free_sqtd_chain: sqtd=%p sqtdend=%p\n",
2801 sqtd, sqtdend));
2802
2803 /* First unlink the chain from the QH's software qTD list. */
2804 prevp = &sqh->sqtd;
2805 for (p = sqh->sqtd; p != NULL; p = p->nextqtd) {
2806 if (p == sqtd) {
2807 *prevp = sqtdend;
2808 break;
2809 }
2810 prevp = &p->nextqtd;
2811 }
2812 USB_KASSERT2(p != NULL, ("ehci_free_sqtd_chain: chain not found"));
2813 for (i = 0; sqtd != sqtdend; sqtd = p, i++) {
2814 p = sqtd->nextqtd;
2815 ehci_free_sqtd(sc, sqtd);
2816 }
2817 }
2818
2819 /****************/
2820
2821 /*
2822 * Close a reqular pipe.
2823 * Assumes that there are no pending transactions.
2824 */
2825 void
2826 ehci_close_pipe(usbd_pipe_handle pipe, ehci_soft_qh_t *head)
2827 {
2828 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
2829 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
2830 ehci_soft_qh_t *sqh = epipe->sqh;
2831 int s;
2832
2833 s = splusb();
2834 ehci_rem_qh(sc, sqh, head);
2835 splx(s);
2836 pipe->endpoint->savedtoggle =
2837 EHCI_QTD_GET_TOGGLE(le32toh(sqh->qh.qh_qtd.qtd_status));
2838 ehci_free_sqh(sc, epipe->sqh);
2839 }
2840
2841 /*
2842 * Abort a device request.
2843 * If this routine is called at splusb() it guarantees that the request
2844 * will be removed from the hardware scheduling and that the callback
2845 * for it will be called with USBD_CANCELLED status.
2846 * It's impossible to guarantee that the requested transfer will not
2847 * have happened since the hardware runs concurrently.
2848 * If the transaction has already happened we rely on the ordinary
2849 * interrupt processing to process it.
2850 */
2851 void
2852 ehci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2853 {
2854 #define exfer EXFER(xfer)
2855 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
2856 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
2857 ehci_soft_qh_t *sqh = epipe->sqh;
2858 ehci_soft_qtd_t *sqtd, *snext;
2859 ehci_physaddr_t cur, us, next;
2860 int s;
2861 int hit, i;
2862 /* int count = 0; */
2863 ehci_soft_qh_t *psqh;
2864
2865 DPRINTF(("ehci_abort_xfer: xfer=%p pipe=%p\n", xfer, epipe));
2866
2867 if (sc->sc_dying) {
2868 /* If we're dying, just do the software part. */
2869 s = splusb();
2870 xfer->status = status; /* make software ignore it */
2871 usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer);
2872 usb_rem_task(epipe->pipe.device, &exfer->abort_task);
2873 usb_transfer_complete_dma(xfer, &sc->sc_dmatag,
2874 &EXFER(xfer)->dmabuf);
2875 splx(s);
2876 return;
2877 }
2878
2879 if (xfer->device->bus->intr_context || !curproc)
2880 panic("ehci_abort_xfer: not in process context");
2881
2882 /*
2883 * If an abort is already in progress then just wait for it to
2884 * complete and return.
2885 */
2886 if (exfer->ehci_xfer_flags & EHCI_XFER_ABORTING) {
2887 DPRINTFN(2, ("ehci_abort_xfer: already aborting\n"));
2888 /* No need to wait if we're aborting from a timeout. */
2889 if (status == USBD_TIMEOUT)
2890 return;
2891 /* Override the status which might be USBD_TIMEOUT. */
2892 xfer->status = status;
2893 DPRINTFN(2, ("ehci_abort_xfer: waiting for abort to finish\n"));
2894 exfer->ehci_xfer_flags |= EHCI_XFER_ABORTWAIT;
2895 while (exfer->ehci_xfer_flags & EHCI_XFER_ABORTING)
2896 tsleep(&exfer->ehci_xfer_flags, PZERO, "ehciaw", 0);
2897 return;
2898 }
2899
2900 /*
2901 * Step 1: Make interrupt routine and timeouts ignore xfer.
2902 */
2903 s = splusb();
2904 exfer->ehci_xfer_flags |= EHCI_XFER_ABORTING;
2905 xfer->status = status; /* make software ignore it */
2906 usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer);
2907 usb_rem_task(epipe->pipe.device, &exfer->abort_task);
2908 splx(s);
2909
2910 /*
2911 * Step 2: Wait until we know hardware has finished any possible
2912 * use of the xfer. We do this by removing the entire
2913 * queue from the async schedule and waiting for the doorbell.
2914 * Nothing else should be touching the queue now.
2915 */
2916 psqh = sqh->prev;
2917 ehci_rem_qh(sc, sqh, psqh);
2918
2919 /*
2920 * Step 3: make sure the soft interrupt routine
2921 * has run. This should remove any completed items off the queue.
2922 * The hardware has no reference to completed items (TDs).
2923 * It's safe to remove them at any time.
2924 */
2925 s = splusb();
2926 #ifdef USB_USE_SOFTINTR
2927 sc->sc_softwake = 1;
2928 #endif /* USB_USE_SOFTINTR */
2929 usb_schedsoftintr(&sc->sc_bus);
2930 #ifdef USB_USE_SOFTINTR
2931 tsleep(&sc->sc_softwake, PZERO, "ehciab", 0);
2932 #endif /* USB_USE_SOFTINTR */
2933
2934 /*
2935 * Step 4: Remove any vestiges of the xfer from the hardware.
2936 * The complication here is that the hardware may have executed
2937 * into or even beyond the xfer we're trying to abort.
2938 * So as we're scanning the TDs of this xfer we check if
2939 * the hardware points to any of them.
2940 *
2941 * first we need to see if there are any transfers
2942 * on this queue before the xfer we are aborting.. we need
2943 * to update any pointers that point to us to point past
2944 * the aborting xfer. (If there is something past us).
2945 * Hardware and software.
2946 */
2947 cur = EHCI_LINK_ADDR(le32toh(sqh->qh.qh_curqtd));
2948 hit = 0;
2949
2950 /* If they initially point here. */
2951 us = EHCI_SQTD_DMAADDR(exfer->sqtdstart);
2952
2953 /* We will change them to point here */
2954 snext = exfer->sqtdend->nextqtd;
2955 next = htole32(EHCI_SQTD_DMAADDR(snext));
2956
2957 /*
2958 * Now loop through any qTDs before us and keep track of the pointer
2959 * that points to us for the end.
2960 */
2961 sqtd = sqh->sqtd;
2962 while (sqtd && sqtd != exfer->sqtdstart) {
2963 hit |= (cur == EHCI_SQTD_DMAADDR(sqtd));
2964 if (EHCI_LINK_ADDR(le32toh(sqtd->qtd.qtd_next)) == us) {
2965 sqtd->qtd.qtd_next = next;
2966 EHCI_SQTD_SYNC(sc, sqtd,
2967 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2968 }
2969 if (EHCI_LINK_ADDR(le32toh(sqtd->qtd.qtd_altnext)) == us) {
2970 sqtd->qtd.qtd_altnext = next;
2971 EHCI_SQTD_SYNC(sc, sqtd,
2972 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2973 }
2974 sqtd = sqtd->nextqtd;
2975 }
2976
2977 /*
2978 * If we already saw the active one then we are pretty much done.
2979 * We've done all the relinking we need to do.
2980 */
2981 if (!hit) {
2982
2983 /*
2984 * Now reinitialise the QH to point to the next qTD
2985 * (if there is one). We only need to do this if
2986 * it was previously pointing to us.
2987 */
2988 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) {
2989 if (cur == EHCI_SQTD_DMAADDR(sqtd)) {
2990 hit++;
2991 }
2992 if (sqtd == exfer->sqtdend)
2993 break;
2994 }
2995 sqtd = sqtd->nextqtd;
2996 /*
2997 * Only need to alter the QH if it was pointing at a qTD
2998 * that we are removing.
2999 */
3000 if (hit) {
3001 sqh->qh.qh_qtd.qtd_next =
3002 htole32(EHCI_SQTD_DMAADDR(snext));
3003 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
3004 sqh->qh.qh_qtd.qtd_status &=
3005 htole32(EHCI_QTD_TOGGLE_MASK);
3006 for (i = 0; i < EHCI_QTD_NBUFFERS; i++) {
3007 sqh->qh.qh_qtd.qtd_buffer[i] = 0;
3008 sqh->qh.qh_qtd.qtd_buffer_hi[i] = 0;
3009 }
3010 }
3011 }
3012 ehci_add_qh(sc, sqh, psqh);
3013 /*
3014 * Step 5: Execute callback.
3015 */
3016 #ifdef DIAGNOSTIC
3017 exfer->isdone = 1;
3018 #endif
3019 /* Do the wakeup first to avoid touching the xfer after the callback. */
3020 exfer->ehci_xfer_flags &= ~EHCI_XFER_ABORTING;
3021 if (exfer->ehci_xfer_flags & EHCI_XFER_ABORTWAIT) {
3022 exfer->ehci_xfer_flags &= ~EHCI_XFER_ABORTWAIT;
3023 wakeup(&exfer->ehci_xfer_flags);
3024 }
3025 usb_transfer_complete_dma(xfer, &sc->sc_dmatag, &EXFER(xfer)->dmabuf);
3026
3027 /* printf("%s: %d TDs aborted\n", __func__, count); */
3028 splx(s);
3029 #undef exfer
3030 }
3031
3032 void
3033 ehci_timeout(void *addr)
3034 {
3035 struct ehci_xfer *exfer = addr;
3036 struct ehci_pipe *epipe = (struct ehci_pipe *)exfer->xfer.pipe;
3037 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus;
3038
3039 DPRINTF(("ehci_timeout: exfer=%p\n", exfer));
3040 #ifdef USB_DEBUG
3041 if (ehcidebug > 1)
3042 usbd_dump_pipe(exfer->xfer.pipe);
3043 #endif
3044
3045 if (sc->sc_dying) {
3046 ehci_abort_xfer(&exfer->xfer, USBD_TIMEOUT);
3047 return;
3048 }
3049
3050 /* Execute the abort in a process context. */
3051 usb_add_task(exfer->xfer.pipe->device, &exfer->abort_task,
3052 USB_TASKQ_HC);
3053 }
3054
3055 void
3056 ehci_timeout_task(void *addr)
3057 {
3058 usbd_xfer_handle xfer = addr;
3059 int s;
3060
3061 DPRINTF(("ehci_timeout_task: xfer=%p\n", xfer));
3062
3063 s = splusb();
3064 ehci_abort_xfer(xfer, USBD_TIMEOUT);
3065 splx(s);
3066 }
3067
3068 /*
3069 * Some EHCI chips from VIA / ATI seem to trigger interrupts before writing
3070 * back the qTD status, or miss signalling occasionally under heavy load.
3071 * If the host machine is too fast, we can miss transaction completion - when
3072 * we scan the active list the transaction still seems to be active. This
3073 * generally exhibits itself as a umass stall that never recovers.
3074 *
3075 * We work around this behaviour by setting up this callback after any softintr
3076 * that completes with transactions still pending, giving us another chance to
3077 * check for completion after the writeback has taken place.
3078 */
3079 void
3080 ehci_intrlist_timeout(void *arg)
3081 {
3082 ehci_softc_t *sc = arg;
3083 int s = splusb();
3084
3085 DPRINTFN(3, ("ehci_intrlist_timeout\n"));
3086 usb_schedsoftintr(&sc->sc_bus);
3087
3088 splx(s);
3089 }
3090
3091 /************************/
3092
3093 Static usbd_status
3094 ehci_device_ctrl_transfer(usbd_xfer_handle xfer)
3095 {
3096 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
3097 usbd_status err;
3098
3099 /* Insert last in queue. */
3100 err = usb_insert_transfer_dma(xfer, &sc->sc_dmatag,
3101 &EXFER(xfer)->dmabuf);
3102 if (err)
3103 return (err);
3104
3105 /* Pipe isn't running, start first */
3106 return (ehci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3107 }
3108
3109 Static usbd_status
3110 ehci_device_ctrl_start(usbd_xfer_handle xfer)
3111 {
3112 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
3113 usbd_status err;
3114
3115 if (sc->sc_dying)
3116 return (USBD_IOERROR);
3117
3118 #ifdef DIAGNOSTIC
3119 if (!(xfer->rqflags & URQ_REQUEST)) {
3120 /* XXX panic */
3121 printf("ehci_device_ctrl_transfer: not a request\n");
3122 return (USBD_INVAL);
3123 }
3124 #endif
3125
3126 err = ehci_device_request(xfer);
3127 if (err)
3128 return (err);
3129
3130 if (sc->sc_bus.use_polling)
3131 ehci_waitintr(sc, xfer);
3132 return (USBD_IN_PROGRESS);
3133 }
3134
3135 void
3136 ehci_device_ctrl_done(usbd_xfer_handle xfer)
3137 {
3138 struct ehci_xfer *ex = EXFER(xfer);
3139 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
3140 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3141
3142 DPRINTFN(10,("ehci_ctrl_done: xfer=%p\n", xfer));
3143
3144 #ifdef DIAGNOSTIC
3145 if (!(xfer->rqflags & URQ_REQUEST)) {
3146 panic("ehci_ctrl_done: not a request");
3147 }
3148 #endif
3149
3150 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
3151 ehci_del_intr_list(ex); /* remove from active list */
3152 ehci_free_sqtd_chain(sc, epipe->sqh, ex->sqtdstart,
3153 ex->sqtdend->nextqtd);
3154 }
3155
3156 DPRINTFN(5, ("ehci_ctrl_done: length=%d\n", xfer->actlen));
3157 }
3158
3159 /* Abort a device control request. */
3160 Static void
3161 ehci_device_ctrl_abort(usbd_xfer_handle xfer)
3162 {
3163 DPRINTF(("ehci_device_ctrl_abort: xfer=%p\n", xfer));
3164 ehci_abort_xfer(xfer, USBD_CANCELLED);
3165 }
3166
3167 /* Close a device control pipe. */
3168 Static void
3169 ehci_device_ctrl_close(usbd_pipe_handle pipe)
3170 {
3171 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
3172 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
3173
3174 DPRINTF(("ehci_device_ctrl_close: pipe=%p\n", pipe));
3175 ehci_close_pipe(pipe, sc->sc_async_head);
3176 usb_freemem(&sc->sc_dmatag, &epipe->u.ctl.reqdma);
3177 }
3178
3179 usbd_status
3180 ehci_device_request(usbd_xfer_handle xfer)
3181 {
3182 #define exfer EXFER(xfer)
3183 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3184 usb_device_request_t *req = &xfer->request;
3185 usbd_device_handle dev = epipe->pipe.device;
3186 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
3187 ehci_soft_qtd_t *newinactive, *setup, *stat, *next;
3188 ehci_soft_qh_t *sqh;
3189 int isread;
3190 int len;
3191 usbd_status err;
3192 int s;
3193
3194 isread = req->bmRequestType & UT_READ;
3195 len = UGETW(req->wLength);
3196
3197 DPRINTFN(3,("ehci_device_request: type=0x%02x, request=0x%02x, "
3198 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
3199 req->bmRequestType, req->bRequest, UGETW(req->wValue),
3200 UGETW(req->wIndex), len, dev->address,
3201 epipe->pipe.endpoint->edesc->bEndpointAddress));
3202
3203 newinactive = ehci_alloc_sqtd(sc);
3204 if (newinactive == NULL) {
3205 err = USBD_NOMEM;
3206 goto bad1;
3207 }
3208 newinactive->qtd.qtd_status = htole32(0);
3209 newinactive->qtd.qtd_next = EHCI_NULL;
3210 newinactive->qtd.qtd_altnext = EHCI_NULL;
3211 EHCI_SQTD_SYNC(sc, newinactive,
3212 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3213 stat = ehci_alloc_sqtd(sc);
3214 if (stat == NULL) {
3215 err = USBD_NOMEM;
3216 goto bad2;
3217 }
3218
3219 sqh = epipe->sqh;
3220 setup = sqh->inactivesqtd;
3221 sqh->inactivesqtd = newinactive;
3222 epipe->u.ctl.length = len;
3223
3224 /* Set up data transaction */
3225 if (len != 0) {
3226 ehci_soft_qtd_t *end;
3227
3228 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
3229 NULL, newinactive, &next, &end);
3230 if (err)
3231 goto bad3;
3232 end->qtd.qtd_status &= htole32(~EHCI_QTD_IOC);
3233 end->nextqtd = stat;
3234 end->qtd.qtd_next = htole32(EHCI_SQTD_DMAADDR(stat));
3235 end->qtd.qtd_altnext = htole32(EHCI_SQTD_DMAADDR(newinactive));
3236 } else {
3237 next = stat;
3238 }
3239
3240 memcpy(KERNADDR(&epipe->u.ctl.reqdma, 0), req, sizeof *req);
3241
3242 /* Clear toggle, and do not activate until complete */
3243 setup->qtd.qtd_status = htole32(
3244 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
3245 EHCI_QTD_SET_CERR(3) |
3246 EHCI_QTD_SET_TOGGLE(0) |
3247 EHCI_QTD_SET_BYTES(sizeof *req)
3248 );
3249 setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->u.ctl.reqdma, 0));
3250 setup->qtd.qtd_buffer_hi[0] = 0;
3251 setup->nextqtd = next;
3252 setup->qtd.qtd_next = htole32(EHCI_SQTD_DMAADDR(next));
3253 setup->qtd.qtd_altnext = htole32(EHCI_SQTD_DMAADDR(newinactive));
3254 setup->xfer = xfer;
3255 setup->len = sizeof *req;
3256 EHCI_SQTD_SYNC(sc, setup, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3257
3258 stat->qtd.qtd_status = htole32(
3259 EHCI_QTD_ACTIVE |
3260 EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) |
3261 EHCI_QTD_SET_CERR(3) |
3262 EHCI_QTD_SET_TOGGLE(1) |
3263 EHCI_QTD_IOC
3264 );
3265 stat->qtd.qtd_buffer[0] = 0; /* XXX not needed? */
3266 stat->qtd.qtd_buffer_hi[0] = 0; /* XXX not needed? */
3267 stat->nextqtd = newinactive;
3268 stat->qtd.qtd_next = htole32(EHCI_SQTD_DMAADDR(newinactive));
3269 stat->qtd.qtd_altnext = htole32(EHCI_SQTD_DMAADDR(newinactive));
3270 stat->xfer = xfer;
3271 stat->len = 0;
3272 EHCI_SQTD_SYNC(sc, stat, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3273
3274 #ifdef EHCI_DEBUG
3275 if (ehcidebug > 5) {
3276 DPRINTF(("ehci_device_request:\n"));
3277 ehci_dump_sqh(sqh);
3278 ehci_dump_sqtds(setup);
3279 }
3280 #endif
3281
3282 exfer->sqtdstart = setup;
3283 exfer->sqtdend = stat;
3284 #ifdef DIAGNOSTIC
3285 if (!exfer->isdone) {
3286 printf("ehci_device_request: not done, exfer=%p\n", exfer);
3287 }
3288 exfer->isdone = 0;
3289 #endif
3290
3291 /* Activate the new qTD in the QH list. */
3292 s = splusb();
3293 ehci_activate_qh(sc, sqh, setup);
3294 if (xfer->timeout && !sc->sc_bus.use_polling) {
3295 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
3296 ehci_timeout, xfer);
3297 }
3298 ehci_add_intr_list(sc, exfer);
3299 xfer->status = USBD_IN_PROGRESS;
3300 splx(s);
3301
3302 #ifdef EHCI_DEBUG
3303 if (ehcidebug > 10) {
3304 DPRINTF(("ehci_device_request: status=%x\n",
3305 EOREAD4(sc, EHCI_USBSTS)));
3306 delay(10000);
3307 ehci_dump_regs(sc);
3308 ehci_dump_sqh(sc->sc_async_head);
3309 ehci_dump_sqh(sqh);
3310 ehci_dump_sqtds(setup);
3311 }
3312 #endif
3313
3314 return (USBD_NORMAL_COMPLETION);
3315
3316 bad3:
3317 sqh->inactivesqtd = setup;
3318 ehci_free_sqtd(sc, stat);
3319 bad2:
3320 ehci_free_sqtd(sc, newinactive);
3321 bad1:
3322 DPRINTFN(-1,("ehci_device_request: no memory\n"));
3323 xfer->status = err;
3324 usb_transfer_complete_dma(xfer, &sc->sc_dmatag,
3325 &EXFER(xfer)->dmabuf);
3326 return (err);
3327 #undef exfer
3328 }
3329
3330 /************************/
3331
3332 Static usbd_status
3333 ehci_device_bulk_transfer(usbd_xfer_handle xfer)
3334 {
3335 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
3336 usbd_status err;
3337
3338 /* Insert last in queue. */
3339 err = usb_insert_transfer_dma(xfer, &sc->sc_dmatag,
3340 &EXFER(xfer)->dmabuf);
3341 if (err)
3342 return (err);
3343
3344 /* Pipe isn't running, start first */
3345 return (ehci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3346 }
3347
3348 usbd_status
3349 ehci_device_bulk_start(usbd_xfer_handle xfer)
3350 {
3351 #define exfer EXFER(xfer)
3352 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3353 usbd_device_handle dev = epipe->pipe.device;
3354 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
3355 ehci_soft_qtd_t *data, *dataend, *newinactive;
3356 ehci_soft_qh_t *sqh;
3357 usbd_status err;
3358 int len, isread, endpt;
3359 int s;
3360
3361 DPRINTFN(2, ("ehci_device_bulk_start: xfer=%p len=%d flags=%d\n",
3362 xfer, xfer->length, xfer->flags));
3363
3364 if (sc->sc_dying)
3365 return (USBD_IOERROR);
3366
3367 #ifdef DIAGNOSTIC
3368 if (xfer->rqflags & URQ_REQUEST)
3369 panic("ehci_device_bulk_start: a request");
3370 #endif
3371
3372 len = xfer->length;
3373 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
3374 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3375 sqh = epipe->sqh;
3376
3377 epipe->u.bulk.length = len;
3378
3379 newinactive = ehci_alloc_sqtd(sc);
3380 if (newinactive == NULL) {
3381 DPRINTFN(-1,("ehci_device_bulk_start: no sqtd memory\n"));
3382 err = USBD_NOMEM;
3383 xfer->status = err;
3384 usb_transfer_complete(xfer);
3385 return (err);
3386 }
3387 newinactive->qtd.qtd_status = htole32(0);
3388 newinactive->qtd.qtd_next = EHCI_NULL;
3389 newinactive->qtd.qtd_altnext = EHCI_NULL;
3390 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
3391 sqh->inactivesqtd, newinactive, &data, &dataend);
3392 if (err) {
3393 DPRINTFN(-1,("ehci_device_bulk_start: no memory\n"));
3394 ehci_free_sqtd(sc, newinactive);
3395 xfer->status = err;
3396 usb_transfer_complete(xfer);
3397 return (err);
3398 }
3399 dataend->nextqtd = newinactive;
3400 dataend->qtd.qtd_next = htole32(EHCI_SQTD_DMAADDR(newinactive));
3401 dataend->qtd.qtd_altnext = htole32(EHCI_SQTD_DMAADDR(newinactive));
3402 sqh->inactivesqtd = newinactive;
3403 EHCI_SQTD_SYNC(sc, newinactive,
3404 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3405 EHCI_SQTD_SYNC(sc, dataend,
3406 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3407
3408 #ifdef EHCI_DEBUG
3409 if (ehcidebug > 5) {
3410 DPRINTF(("ehci_device_bulk_start: data(1)\n"));
3411 ehci_dump_sqh(sqh);
3412 ehci_dump_sqtds(data);
3413 }
3414 #endif
3415
3416 /* Set up interrupt info. */
3417 exfer->sqtdstart = data;
3418 exfer->sqtdend = dataend;
3419 #ifdef DIAGNOSTIC
3420 if (!exfer->isdone) {
3421 printf("ehci_device_bulk_start: not done, ex=%p\n", exfer);
3422 }
3423 exfer->isdone = 0;
3424 #endif
3425
3426 s = splusb();
3427 ehci_activate_qh(sc, sqh, data);
3428 if (xfer->timeout && !sc->sc_bus.use_polling) {
3429 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
3430 ehci_timeout, xfer);
3431 }
3432 ehci_add_intr_list(sc, exfer);
3433 xfer->status = USBD_IN_PROGRESS;
3434 splx(s);
3435
3436 #ifdef EHCI_DEBUG
3437 if (ehcidebug > 10) {
3438 DPRINTF(("ehci_device_bulk_start: data(2)\n"));
3439 delay(10000);
3440 DPRINTF(("ehci_device_bulk_start: data(3)\n"));
3441 ehci_dump_regs(sc);
3442 #if 0
3443 printf("async_head:\n");
3444 ehci_dump_sqh(sc->sc_async_head);
3445 #endif
3446 printf("sqh:\n");
3447 ehci_dump_sqh(sqh);
3448 ehci_dump_sqtds(data);
3449 }
3450 #endif
3451
3452 if (sc->sc_bus.use_polling)
3453 ehci_waitintr(sc, xfer);
3454
3455 return (USBD_IN_PROGRESS);
3456 #undef exfer
3457 }
3458
3459 Static void
3460 ehci_device_bulk_abort(usbd_xfer_handle xfer)
3461 {
3462 DPRINTF(("ehci_device_bulk_abort: xfer=%p\n", xfer));
3463 ehci_abort_xfer(xfer, USBD_CANCELLED);
3464 }
3465
3466 /*
3467 * Close a device bulk pipe.
3468 */
3469 Static void
3470 ehci_device_bulk_close(usbd_pipe_handle pipe)
3471 {
3472 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
3473
3474 DPRINTF(("ehci_device_bulk_close: pipe=%p\n", pipe));
3475 ehci_close_pipe(pipe, sc->sc_async_head);
3476 }
3477
3478 void
3479 ehci_device_bulk_done(usbd_xfer_handle xfer)
3480 {
3481 struct ehci_xfer *ex = EXFER(xfer);
3482 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
3483 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3484
3485 DPRINTFN(10,("ehci_bulk_done: xfer=%p, actlen=%d\n",
3486 xfer, xfer->actlen));
3487
3488 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
3489 ehci_del_intr_list(ex); /* remove from active list */
3490 ehci_free_sqtd_chain(sc, epipe->sqh, ex->sqtdstart,
3491 ex->sqtdend->nextqtd);
3492 }
3493
3494 DPRINTFN(5, ("ehci_bulk_done: length=%d\n", xfer->actlen));
3495 }
3496
3497 /************************/
3498
3499 Static usbd_status
3500 ehci_device_setintr(ehci_softc_t *sc, ehci_soft_qh_t *sqh, int ival)
3501 {
3502 struct ehci_soft_islot *isp;
3503 int islot, lev;
3504
3505 /* Find a poll rate that is large enough. */
3506 for (lev = EHCI_IPOLLRATES - 1; lev > 0; lev--)
3507 if (EHCI_ILEV_IVAL(lev) <= ival)
3508 break;
3509
3510 /* Pick an interrupt slot at the right level. */
3511 /* XXX could do better than picking at random. */
3512 islot = EHCI_IQHIDX(lev, arc4random());
3513
3514 sqh->islot = islot;
3515 isp = &sc->sc_islots[islot];
3516 ehci_add_qh(sc, sqh, isp->sqh);
3517
3518 return (USBD_NORMAL_COMPLETION);
3519 }
3520
3521 Static usbd_status
3522 ehci_device_intr_transfer(usbd_xfer_handle xfer)
3523 {
3524 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
3525 usbd_status err;
3526
3527 /* Insert last in queue. */
3528 err = usb_insert_transfer_dma(xfer, &sc->sc_dmatag,
3529 &EXFER(xfer)->dmabuf);
3530 if (err)
3531 return (err);
3532
3533 /*
3534 * Pipe isn't running (otherwise err would be USBD_INPROG),
3535 * so start it first.
3536 */
3537 return (ehci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3538 }
3539
3540 Static usbd_status
3541 ehci_device_intr_start(usbd_xfer_handle xfer)
3542 {
3543 #define exfer EXFER(xfer)
3544 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3545 usbd_device_handle dev = xfer->pipe->device;
3546 ehci_softc_t *sc = (ehci_softc_t *)dev->bus;
3547 ehci_soft_qtd_t *data, *dataend, *newinactive;
3548 ehci_soft_qh_t *sqh;
3549 usbd_status err;
3550 int len, isread, endpt;
3551 int s;
3552
3553 DPRINTFN(2, ("ehci_device_intr_start: xfer=%p len=%d flags=%d\n",
3554 xfer, xfer->length, xfer->flags));
3555
3556 if (sc->sc_dying)
3557 return (USBD_IOERROR);
3558
3559 #ifdef DIAGNOSTIC
3560 if (xfer->rqflags & URQ_REQUEST)
3561 panic("ehci_device_intr_start: a request");
3562 #endif
3563
3564 len = xfer->length;
3565 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
3566 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3567 sqh = epipe->sqh;
3568
3569 epipe->u.intr.length = len;
3570
3571 newinactive = ehci_alloc_sqtd(sc);
3572 if (newinactive == NULL) {
3573 DPRINTFN(-1,("ehci_device_intr_start: no sqtd memory\n"));
3574 err = USBD_NOMEM;
3575 xfer->status = err;
3576 usb_transfer_complete(xfer);
3577 return (err);
3578 }
3579 newinactive->qtd.qtd_status = htole32(0);
3580 newinactive->qtd.qtd_next = EHCI_NULL;
3581 newinactive->qtd.qtd_altnext = EHCI_NULL;
3582 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
3583 sqh->inactivesqtd, newinactive, &data, &dataend);
3584 if (err) {
3585 DPRINTFN(-1, ("ehci_device_intr_start: no memory\n"));
3586 xfer->status = err;
3587 usb_transfer_complete(xfer);
3588 return (err);
3589 }
3590 dataend->nextqtd = newinactive;
3591 dataend->qtd.qtd_next = htole32(EHCI_SQTD_DMAADDR(newinactive));
3592 dataend->qtd.qtd_altnext = htole32(EHCI_SQTD_DMAADDR(newinactive));
3593 sqh->inactivesqtd = newinactive;
3594 EHCI_SQTD_SYNC(sc, newinactive,
3595 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3596 EHCI_SQTD_SYNC(sc, dataend,
3597 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3598
3599 #ifdef EHCI_DEBUG
3600 if (ehcidebug > 5) {
3601 DPRINTF(("ehci_device_intr_start: data(1)\n"));
3602 ehci_dump_sqh(sqh);
3603 ehci_dump_sqtds(data);
3604 }
3605 #endif
3606
3607 /* Set up interrupt info. */
3608 exfer->sqtdstart = data;
3609 exfer->sqtdend = dataend;
3610 #ifdef DIAGNOSTIC
3611 if (!exfer->isdone) {
3612 printf("ehci_device_intr_start: not done, ex=%p\n", exfer);
3613 }
3614 exfer->isdone = 0;
3615 #endif
3616
3617 s = splusb();
3618 ehci_activate_qh(sc, sqh, data);
3619 if (xfer->timeout && !sc->sc_bus.use_polling) {
3620 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
3621 ehci_timeout, xfer);
3622 }
3623 ehci_add_intr_list(sc, exfer);
3624 xfer->status = USBD_IN_PROGRESS;
3625 splx(s);
3626
3627 #ifdef EHCI_DEBUG
3628 if (ehcidebug > 10) {
3629 DPRINTF(("ehci_device_intr_start: data(2)\n"));
3630 delay(10000);
3631 DPRINTF(("ehci_device_intr_start: data(3)\n"));
3632 ehci_dump_regs(sc);
3633 printf("sqh:\n");
3634 ehci_dump_sqh(sqh);
3635 ehci_dump_sqtds(data);
3636 }
3637 #endif
3638
3639 if (sc->sc_bus.use_polling)
3640 ehci_waitintr(sc, xfer);
3641
3642 return (USBD_IN_PROGRESS);
3643 #undef exfer
3644 }
3645
3646 Static void
3647 ehci_device_intr_abort(usbd_xfer_handle xfer)
3648 {
3649 DPRINTFN(1, ("ehci_device_intr_abort: xfer=%p\n", xfer));
3650 if (xfer->pipe->intrxfer == xfer) {
3651 DPRINTFN(1, ("ehci_device_intr_abort: remove\n"));
3652 xfer->pipe->intrxfer = NULL;
3653 }
3654 ehci_abort_xfer(xfer, USBD_CANCELLED);
3655 }
3656
3657 Static void
3658 ehci_device_intr_close(usbd_pipe_handle pipe)
3659 {
3660 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus;
3661 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
3662 struct ehci_soft_islot *isp;
3663
3664 isp = &sc->sc_islots[epipe->sqh->islot];
3665 ehci_close_pipe(pipe, isp->sqh);
3666 }
3667
3668 Static void
3669 ehci_device_intr_done(usbd_xfer_handle xfer)
3670 {
3671 #define exfer EXFER(xfer)
3672 struct ehci_xfer *ex = EXFER(xfer);
3673 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus;
3674 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;
3675 ehci_soft_qtd_t *data, *dataend, *newinactive;
3676 ehci_soft_qh_t *sqh;
3677 usbd_status err;
3678 int len, isread, endpt, s;
3679
3680 DPRINTFN(10, ("ehci_device_intr_done: xfer=%p, actlen=%d\n",
3681 xfer, xfer->actlen));
3682
3683 sqh = epipe->sqh;
3684 if (xfer->pipe->repeat) {
3685 ehci_free_sqtd_chain(sc, sqh, ex->sqtdstart,
3686 ex->sqtdend->nextqtd);
3687
3688 len = epipe->u.intr.length;
3689 xfer->length = len;
3690 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress;
3691 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
3692
3693 newinactive = ehci_alloc_sqtd(sc);
3694 if (newinactive == NULL) {
3695 DPRINTFN(-1,
3696 ("ehci_device_intr_done: no sqtd memory\n"));
3697 err = USBD_NOMEM;
3698 xfer->status = err;
3699 return;
3700 }
3701 newinactive->qtd.qtd_status = htole32(0);
3702 newinactive->qtd.qtd_next = EHCI_NULL;
3703 newinactive->qtd.qtd_altnext = EHCI_NULL;
3704 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
3705 sqh->inactivesqtd, newinactive, &data, &dataend);
3706 if (err) {
3707 DPRINTFN(-1, ("ehci_device_intr_done: no memory\n"));
3708 xfer->status = err;
3709 return;
3710 }
3711 dataend->nextqtd = newinactive;
3712 dataend->qtd.qtd_next = htole32(EHCI_SQTD_DMAADDR(newinactive));
3713 dataend->qtd.qtd_altnext = htole32(EHCI_SQTD_DMAADDR(newinactive));
3714 sqh->inactivesqtd = newinactive;
3715 EHCI_SQTD_SYNC(sc, newinactive,
3716 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3717 EHCI_SQTD_SYNC(sc, dataend,
3718 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
3719
3720 /* Set up interrupt info. */
3721 exfer->sqtdstart = data;
3722 exfer->sqtdend = dataend;
3723 #ifdef DIAGNOSTIC
3724 if (!exfer->isdone) {
3725 printf("ehci_device_intr_done: not done, ex=%p\n",
3726 exfer);
3727 }
3728 exfer->isdone = 0;
3729 #endif
3730
3731 s = splusb();
3732 ehci_activate_qh(sc, sqh, data);
3733 if (xfer->timeout && !sc->sc_bus.use_polling) {
3734 usb_callout(xfer->timeout_handle,
3735 MS_TO_TICKS(xfer->timeout), ehci_timeout, xfer);
3736 }
3737 splx(s);
3738
3739 xfer->status = USBD_IN_PROGRESS;
3740 } else if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) {
3741 ehci_del_intr_list(ex); /* remove from active list */
3742 ehci_free_sqtd_chain(sc, sqh, ex->sqtdstart,
3743 ex->sqtdend->nextqtd);
3744 }
3745 #undef exfer
3746 }
3747
3748 /************************/
3749
3750 Static usbd_status
3751 ehci_device_isoc_transfer(usbd_xfer_handle xfer)
3752 {
3753 return USBD_IOERROR;
3754 }
3755 Static usbd_status
3756 ehci_device_isoc_start(usbd_xfer_handle xfer)
3757 {
3758 return USBD_IOERROR;
3759 }
3760 Static void
3761 ehci_device_isoc_abort(usbd_xfer_handle xfer)
3762 {
3763 }
3764 Static void
3765 ehci_device_isoc_close(usbd_pipe_handle pipe)
3766 {
3767 }
3768 Static void
3769 ehci_device_isoc_done(usbd_xfer_handle xfer)
3770 {
3771 }
3772