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