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