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