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