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