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