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