]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/ohci.c
Some whitespace and style fixes.
[FreeBSD/FreeBSD.git] / sys / dev / usb / ohci.c
1 /*      $NetBSD: ohci.c,v 1.138 2003/02/08 03:32:50 ichiro Exp $        */
2
3 /* Also, already ported:
4  *      $NetBSD: ohci.c,v 1.140 2003/05/13 04:42:00 gson Exp $
5  *      $NetBSD: ohci.c,v 1.141 2003/09/10 20:08:29 mycroft Exp $
6  *      $NetBSD: ohci.c,v 1.142 2003/10/11 03:04:26 toshii Exp $
7  *      $NetBSD: ohci.c,v 1.143 2003/10/18 04:50:35 simonb Exp $
8  *      $NetBSD: ohci.c,v 1.144 2003/11/23 19:18:06 augustss Exp $
9  *      $NetBSD: ohci.c,v 1.145 2003/11/23 19:20:25 augustss Exp $
10  *      $NetBSD: ohci.c,v 1.146 2003/12/29 08:17:10 toshii Exp $
11  *      $NetBSD: ohci.c,v 1.147 2004/06/22 07:20:35 mycroft Exp $
12  *      $NetBSD: ohci.c,v 1.148 2004/06/22 18:27:46 mycroft Exp $
13  */
14
15 #include <sys/cdefs.h>
16 __FBSDID("$FreeBSD$");
17
18 /*-
19  * Copyright (c) 1998 The NetBSD Foundation, Inc.
20  * All rights reserved.
21  *
22  * This code is derived from software contributed to The NetBSD Foundation
23  * by Lennart Augustsson (lennart@augustsson.net) at
24  * Carlstedt Research & Technology.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  * 1. Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions and the following disclaimer.
31  * 2. Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in the
33  *    documentation and/or other materials provided with the distribution.
34  * 3. All advertising materials mentioning features or use of this software
35  *    must display the following acknowledgement:
36  *        This product includes software developed by the NetBSD
37  *        Foundation, Inc. and its contributors.
38  * 4. Neither the name of The NetBSD Foundation nor the names of its
39  *    contributors may be used to endorse or promote products derived
40  *    from this software without specific prior written permission.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
43  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
44  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
46  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
47  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
48  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
52  * POSSIBILITY OF SUCH DAMAGE.
53  */
54
55 /*
56  * USB Open Host Controller driver.
57  *
58  * OHCI spec: http://www.compaq.com/productinfo/development/openhci.html
59  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
60  */
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/malloc.h>
65 #include <sys/kernel.h>
66 #include <sys/endian.h>
67 #include <sys/module.h>
68 #include <sys/bus.h>
69 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
70 #include <machine/cpu.h>
71 #endif
72 #include <sys/proc.h>
73 #include <sys/queue.h>
74 #include <sys/sysctl.h>
75
76 #include <machine/bus.h>
77 #include <machine/endian.h>
78
79 #include <dev/usb/usb.h>
80 #include <dev/usb/usbdi.h>
81 #include <dev/usb/usbdivar.h>
82 #include <dev/usb/usb_mem.h>
83 #include <dev/usb/usb_quirks.h>
84
85 #include <dev/usb/ohcireg.h>
86 #include <dev/usb/ohcivar.h>
87
88 #define delay(d)                DELAY(d)
89
90 #ifdef USB_DEBUG
91 #define DPRINTF(x)      if (ohcidebug) printf x
92 #define DPRINTFN(n,x)   if (ohcidebug>(n)) printf x
93 int ohcidebug = 0;
94 SYSCTL_NODE(_hw_usb, OID_AUTO, ohci, CTLFLAG_RW, 0, "USB ohci");
95 SYSCTL_INT(_hw_usb_ohci, OID_AUTO, debug, CTLFLAG_RW,
96            &ohcidebug, 0, "ohci debug level");
97 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
98 #else
99 #define DPRINTF(x)
100 #define DPRINTFN(n,x)
101 #endif
102
103 struct ohci_pipe;
104
105 static ohci_soft_ed_t  *ohci_alloc_sed(ohci_softc_t *);
106 static void             ohci_free_sed(ohci_softc_t *, ohci_soft_ed_t *);
107
108 static ohci_soft_td_t  *ohci_alloc_std(ohci_softc_t *);
109 static void             ohci_free_std(ohci_softc_t *, ohci_soft_td_t *);
110
111 static ohci_soft_itd_t *ohci_alloc_sitd(ohci_softc_t *);
112 static void             ohci_free_sitd(ohci_softc_t *,ohci_soft_itd_t *);
113
114 #if 0
115 static void             ohci_free_std_chain(ohci_softc_t *, ohci_soft_td_t *,
116                                             ohci_soft_td_t *);
117 #endif
118 static usbd_status      ohci_alloc_std_chain(struct ohci_pipe *,
119                             ohci_softc_t *, int, int, usbd_xfer_handle,
120                             ohci_soft_td_t *, ohci_soft_td_t **);
121
122 #if defined(__NetBSD__) || defined(__OpenBSD__)
123 static void             ohci_shutdown(void *v);
124 static void             ohci_power(int, void *);
125 #endif
126 static usbd_status      ohci_open(usbd_pipe_handle);
127 static void             ohci_poll(struct usbd_bus *);
128 static void             ohci_softintr(void *);
129 static void             ohci_waitintr(ohci_softc_t *, usbd_xfer_handle);
130 static void             ohci_add_done(ohci_softc_t *, ohci_physaddr_t);
131 static void             ohci_rhsc(ohci_softc_t *, usbd_xfer_handle);
132
133 static usbd_status      ohci_device_request(usbd_xfer_handle xfer);
134 static void             ohci_add_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
135 static void             ohci_rem_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
136 static void             ohci_hash_add_td(ohci_softc_t *, ohci_soft_td_t *);
137 static void             ohci_hash_rem_td(ohci_softc_t *, ohci_soft_td_t *);
138 static ohci_soft_td_t  *ohci_hash_find_td(ohci_softc_t *, ohci_physaddr_t);
139 static void             ohci_hash_add_itd(ohci_softc_t *, ohci_soft_itd_t *);
140 static void             ohci_hash_rem_itd(ohci_softc_t *, ohci_soft_itd_t *);
141 static ohci_soft_itd_t *ohci_hash_find_itd(ohci_softc_t *, ohci_physaddr_t);
142
143 static usbd_status      ohci_setup_isoc(usbd_pipe_handle pipe);
144 static void             ohci_device_isoc_enter(usbd_xfer_handle);
145
146 static usbd_status      ohci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
147 static void             ohci_freem(struct usbd_bus *, usb_dma_t *);
148
149 static usbd_xfer_handle ohci_allocx(struct usbd_bus *);
150 static void             ohci_freex(struct usbd_bus *, usbd_xfer_handle);
151
152 static usbd_status      ohci_root_ctrl_transfer(usbd_xfer_handle);
153 static usbd_status      ohci_root_ctrl_start(usbd_xfer_handle);
154 static void             ohci_root_ctrl_abort(usbd_xfer_handle);
155 static void             ohci_root_ctrl_close(usbd_pipe_handle);
156 static void             ohci_root_ctrl_done(usbd_xfer_handle);
157
158 static usbd_status      ohci_root_intr_transfer(usbd_xfer_handle);
159 static usbd_status      ohci_root_intr_start(usbd_xfer_handle);
160 static void             ohci_root_intr_abort(usbd_xfer_handle);
161 static void             ohci_root_intr_close(usbd_pipe_handle);
162 static void             ohci_root_intr_done(usbd_xfer_handle);
163
164 static usbd_status      ohci_device_ctrl_transfer(usbd_xfer_handle);
165 static usbd_status      ohci_device_ctrl_start(usbd_xfer_handle);
166 static void             ohci_device_ctrl_abort(usbd_xfer_handle);
167 static void             ohci_device_ctrl_close(usbd_pipe_handle);
168 static void             ohci_device_ctrl_done(usbd_xfer_handle);
169
170 static usbd_status      ohci_device_bulk_transfer(usbd_xfer_handle);
171 static usbd_status      ohci_device_bulk_start(usbd_xfer_handle);
172 static void             ohci_device_bulk_abort(usbd_xfer_handle);
173 static void             ohci_device_bulk_close(usbd_pipe_handle);
174 static void             ohci_device_bulk_done(usbd_xfer_handle);
175
176 static usbd_status      ohci_device_intr_transfer(usbd_xfer_handle);
177 static usbd_status      ohci_device_intr_start(usbd_xfer_handle);
178 static void             ohci_device_intr_abort(usbd_xfer_handle);
179 static void             ohci_device_intr_close(usbd_pipe_handle);
180 static void             ohci_device_intr_done(usbd_xfer_handle);
181
182 static usbd_status      ohci_device_isoc_transfer(usbd_xfer_handle);
183 static usbd_status      ohci_device_isoc_start(usbd_xfer_handle);
184 static void             ohci_device_isoc_abort(usbd_xfer_handle);
185 static void             ohci_device_isoc_close(usbd_pipe_handle);
186 static void             ohci_device_isoc_done(usbd_xfer_handle);
187
188 static usbd_status      ohci_device_setintr(ohci_softc_t *sc,
189                             struct ohci_pipe *pipe, int ival);
190 static usbd_status      ohci_device_intr_insert(ohci_softc_t *sc,
191                             usbd_xfer_handle xfer);
192
193 static int              ohci_str(usb_string_descriptor_t *, int, const char *);
194
195 static void             ohci_timeout(void *);
196 static void             ohci_timeout_task(void *);
197 static void             ohci_rhsc_able(ohci_softc_t *, int);
198 static void             ohci_rhsc_enable(void *);
199
200 static void             ohci_close_pipe(usbd_pipe_handle, ohci_soft_ed_t *);
201 static void             ohci_abort_xfer(usbd_xfer_handle, usbd_status);
202
203 static void             ohci_device_clear_toggle(usbd_pipe_handle pipe);
204 static void             ohci_noop(usbd_pipe_handle pipe);
205
206 static usbd_status ohci_controller_init(ohci_softc_t *sc);
207
208 #ifdef USB_DEBUG
209 static void             ohci_dumpregs(ohci_softc_t *);
210 static void             ohci_dump_tds(ohci_soft_td_t *);
211 static void             ohci_dump_td(ohci_soft_td_t *);
212 static void             ohci_dump_ed(ohci_soft_ed_t *);
213 static void             ohci_dump_itd(ohci_soft_itd_t *);
214 static void             ohci_dump_itds(ohci_soft_itd_t *);
215 #endif
216
217 #define OBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
218                         BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
219 #define OWRITE1(sc, r, x) \
220  do { OBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
221 #define OWRITE2(sc, r, x) \
222  do { OBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
223 #define OWRITE4(sc, r, x) \
224  do { OBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
225 #define OREAD1(sc, r) (OBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
226 #define OREAD2(sc, r) (OBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
227 #define OREAD4(sc, r) (OBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
228
229 /* Reverse the bits in a value 0 .. 31 */
230 static u_int8_t revbits[OHCI_NO_INTRS] =
231   { 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c,
232     0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e,
233     0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d,
234     0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f };
235
236 struct ohci_pipe {
237         struct usbd_pipe pipe;
238         ohci_soft_ed_t *sed;
239         u_int32_t aborting;
240         union {
241                 ohci_soft_td_t *td;
242                 ohci_soft_itd_t *itd;
243         } tail;
244         /* Info needed for different pipe kinds. */
245         union {
246                 /* Control pipe */
247                 struct {
248                         usb_dma_t reqdma;
249                         u_int length;
250                         ohci_soft_td_t *setup, *data, *stat;
251                 } ctl;
252                 /* Interrupt pipe */
253                 struct {
254                         int nslots;
255                         int pos;
256                 } intr;
257                 /* Bulk pipe */
258                 struct {
259                         u_int length;
260                         int isread;
261                 } bulk;
262                 /* Iso pipe */
263                 struct iso {
264                         int next, inuse;
265                 } iso;
266         } u;
267 };
268
269 #define OHCI_INTR_ENDPT 1
270
271 static struct usbd_bus_methods ohci_bus_methods = {
272         ohci_open,
273         ohci_softintr,
274         ohci_poll,
275         ohci_allocm,
276         ohci_freem,
277         ohci_allocx,
278         ohci_freex,
279 };
280
281 static struct usbd_pipe_methods ohci_root_ctrl_methods = {
282         ohci_root_ctrl_transfer,
283         ohci_root_ctrl_start,
284         ohci_root_ctrl_abort,
285         ohci_root_ctrl_close,
286         ohci_noop,
287         ohci_root_ctrl_done,
288 };
289
290 static struct usbd_pipe_methods ohci_root_intr_methods = {
291         ohci_root_intr_transfer,
292         ohci_root_intr_start,
293         ohci_root_intr_abort,
294         ohci_root_intr_close,
295         ohci_noop,
296         ohci_root_intr_done,
297 };
298
299 static struct usbd_pipe_methods ohci_device_ctrl_methods = {
300         ohci_device_ctrl_transfer,
301         ohci_device_ctrl_start,
302         ohci_device_ctrl_abort,
303         ohci_device_ctrl_close,
304         ohci_noop,
305         ohci_device_ctrl_done,
306 };
307
308 static struct usbd_pipe_methods ohci_device_intr_methods = {
309         ohci_device_intr_transfer,
310         ohci_device_intr_start,
311         ohci_device_intr_abort,
312         ohci_device_intr_close,
313         ohci_device_clear_toggle,
314         ohci_device_intr_done,
315 };
316
317 static struct usbd_pipe_methods ohci_device_bulk_methods = {
318         ohci_device_bulk_transfer,
319         ohci_device_bulk_start,
320         ohci_device_bulk_abort,
321         ohci_device_bulk_close,
322         ohci_device_clear_toggle,
323         ohci_device_bulk_done,
324 };
325
326 static struct usbd_pipe_methods ohci_device_isoc_methods = {
327         ohci_device_isoc_transfer,
328         ohci_device_isoc_start,
329         ohci_device_isoc_abort,
330         ohci_device_isoc_close,
331         ohci_noop,
332         ohci_device_isoc_done,
333 };
334
335 int
336 ohci_detach(struct ohci_softc *sc, int flags)
337 {
338         int i, rv = 0;
339
340         sc->sc_dying = 1;
341         callout_stop(&sc->sc_tmo_rhsc);
342
343 #if defined(__NetBSD__) || defined(__OpenBSD__)
344         powerhook_disestablish(sc->sc_powerhook);
345         shutdownhook_disestablish(sc->sc_shutdownhook);
346 #endif
347
348         OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
349         OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
350
351         usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */
352
353         for (i = 0; i < OHCI_NO_EDS; i++)
354                 ohci_free_sed(sc, sc->sc_eds[i]);
355         ohci_free_sed(sc, sc->sc_isoc_head);
356         ohci_free_sed(sc, sc->sc_bulk_head);
357         ohci_free_sed(sc, sc->sc_ctrl_head);
358         usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
359
360         return (rv);
361 }
362
363 ohci_soft_ed_t *
364 ohci_alloc_sed(ohci_softc_t *sc)
365 {
366         ohci_soft_ed_t *sed;
367         usbd_status err;
368         int i, offs;
369         usb_dma_t dma;
370
371         if (sc->sc_freeeds == NULL) {
372                 DPRINTFN(2, ("ohci_alloc_sed: allocating chunk\n"));
373                 err = usb_allocmem(&sc->sc_bus, OHCI_SED_SIZE * OHCI_SED_CHUNK,
374                           OHCI_ED_ALIGN, &dma);
375                 if (err)
376                         return (NULL);
377                 for(i = 0; i < OHCI_SED_CHUNK; i++) {
378                         offs = i * OHCI_SED_SIZE;
379                         sed = KERNADDR(&dma, offs);
380                         sed->physaddr = DMAADDR(&dma, offs);
381                         sed->next = sc->sc_freeeds;
382                         sc->sc_freeeds = sed;
383                 }
384         }
385         sed = sc->sc_freeeds;
386         sc->sc_freeeds = sed->next;
387         memset(&sed->ed, 0, sizeof(ohci_ed_t));
388         sed->next = 0;
389         return (sed);
390 }
391
392 void
393 ohci_free_sed(ohci_softc_t *sc, ohci_soft_ed_t *sed)
394 {
395         sed->next = sc->sc_freeeds;
396         sc->sc_freeeds = sed;
397 }
398
399 ohci_soft_td_t *
400 ohci_alloc_std(ohci_softc_t *sc)
401 {
402         ohci_soft_td_t *std;
403         usbd_status err;
404         int i, offs;
405         usb_dma_t dma;
406         int s;
407
408         if (sc->sc_freetds == NULL) {
409                 DPRINTFN(2, ("ohci_alloc_std: allocating chunk\n"));
410                 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
411                           OHCI_TD_ALIGN, &dma);
412                 if (err)
413                         return (NULL);
414                 s = splusb();
415                 for(i = 0; i < OHCI_STD_CHUNK; i++) {
416                         offs = i * OHCI_STD_SIZE;
417                         std = KERNADDR(&dma, offs);
418                         std->physaddr = DMAADDR(&dma, offs);
419                         std->nexttd = sc->sc_freetds;
420                         sc->sc_freetds = std;
421                 }
422                 splx(s);
423         }
424
425         s = splusb();
426         std = sc->sc_freetds;
427         sc->sc_freetds = std->nexttd;
428         memset(&std->td, 0, sizeof(ohci_td_t));
429         std->nexttd = NULL;
430         std->xfer = NULL;
431         ohci_hash_add_td(sc, std);
432         splx(s);
433
434         return (std);
435 }
436
437 void
438 ohci_free_std(ohci_softc_t *sc, ohci_soft_td_t *std)
439 {
440         int s;
441
442         s = splusb();
443         ohci_hash_rem_td(sc, std);
444         std->nexttd = sc->sc_freetds;
445         sc->sc_freetds = std;
446         splx(s);
447 }
448
449 usbd_status
450 ohci_alloc_std_chain(struct ohci_pipe *opipe, ohci_softc_t *sc,
451                      int alen, int rd, usbd_xfer_handle xfer,
452                      ohci_soft_td_t *sp, ohci_soft_td_t **ep)
453 {
454         ohci_soft_td_t *next, *cur, *end;
455         ohci_physaddr_t dataphys, physend;
456         u_int32_t tdflags;
457         int offset = 0;
458         int len, maxp, curlen, curlen2, seg, segoff;
459         struct usb_dma_mapping *dma = &xfer->dmamap;
460         u_int16_t flags = xfer->flags;
461
462         DPRINTFN(alen < 4096,("ohci_alloc_std_chain: start len=%d\n", alen));
463
464         len = alen;
465         cur = sp;
466         end = NULL;
467
468         maxp = UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize);
469         tdflags = htole32(
470             (rd ? OHCI_TD_IN : OHCI_TD_OUT) |
471             (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0) |
472             OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY | OHCI_TD_SET_DI(6));
473
474         seg = 0;
475         segoff = 0;
476         while (len > 0) {
477                 next = ohci_alloc_std(sc);
478                 if (next == NULL)
479                         goto nomem;
480
481                 /*
482                  * The OHCI hardware can handle at most one 4k crossing.
483                  * The OHCI spec says: If during the data transfer the buffer
484                  * address contained in the HC's working copy of
485                  * CurrentBufferPointer crosses a 4K boundary, the upper 20
486                  * bits of Buffer End are copied to the working value of
487                  * CurrentBufferPointer causing the next buffer address to
488                  * be the 0th byte in the same 4K page that contains the
489                  * last byte of the buffer (the 4K boundary crossing may
490                  * occur within a data packet transfer.)
491                  */
492                 KASSERT(seg < dma->nsegs, ("ohci_alloc_std_chain: overrun"));
493                 dataphys = dma->segs[seg].ds_addr + segoff;
494                 curlen = dma->segs[seg].ds_len - segoff;
495                 if (curlen > len)
496                         curlen = len;
497                 physend = dataphys + curlen - 1;
498                 if (OHCI_PAGE(dataphys) != OHCI_PAGE(physend)) {
499                         /* Truncate to two OHCI pages if there are more. */
500                         if (curlen > 2 * OHCI_PAGE_SIZE -
501                             OHCI_PAGE_OFFSET(dataphys))
502                                 curlen = 2 * OHCI_PAGE_SIZE -
503                                     OHCI_PAGE_OFFSET(dataphys);
504                         if (curlen < len)
505                                 curlen -= curlen % maxp;
506                         physend = dataphys + curlen - 1;
507                 } else if (OHCI_PAGE_OFFSET(physend + 1) == 0 && curlen < len &&
508                     curlen + segoff == dma->segs[seg].ds_len) {
509                         /* We can possibly include another segment. */
510                         KASSERT(seg + 1 < dma->nsegs,
511                             ("ohci_alloc_std_chain: overrun2"));
512                         seg++;
513
514                         /* Determine how much of the second segment to use. */
515                         curlen2 = dma->segs[seg].ds_len;
516                         if (curlen + curlen2 > len)
517                                 curlen2 = len - curlen;
518                         if (OHCI_PAGE(dma->segs[seg].ds_addr) !=
519                             OHCI_PAGE(dma->segs[seg].ds_addr + curlen2 - 1))
520                                 curlen2 = OHCI_PAGE_SIZE -
521                                     OHCI_PAGE_OFFSET(dma->segs[seg].ds_addr);
522                         if (curlen + curlen2 < len)
523                                 curlen2 -= (curlen + curlen2) % maxp;
524
525                         if (curlen2 > 0) {
526                                 /* We can include a second segment */
527                                 segoff = curlen2;
528                                 physend = dma->segs[seg].ds_addr + curlen2 - 1;
529                                 curlen += curlen2;
530                         } else {
531                                 /* Second segment not usable now. */
532                                 seg--;
533                                 segoff += curlen;
534                         }
535                 } else {
536                         /* Simple case where there is just one OHCI page. */
537                         segoff += curlen;
538                 }
539                 if (curlen == 0 && len != 0) {
540                         /*
541                          * A maxp length packet would need to be split.
542                          * This shouldn't be possible if PAGE_SIZE >= 4k
543                          * and the buffer is contiguous in virtual memory.
544                          */
545                         panic("ohci_alloc_std_chain: XXX need to copy");
546                 }
547                 if (segoff >= dma->segs[seg].ds_len) {
548                         KASSERT(segoff == dma->segs[seg].ds_len,
549                             ("ohci_alloc_std_chain: overlap"));
550                         seg++;
551                         segoff = 0;
552                 }
553                 DPRINTFN(4,("ohci_alloc_std_chain: dataphys=0x%08x "
554                             "len=%d curlen=%d\n",
555                             dataphys, len, curlen));
556                 len -= curlen;
557
558                 cur->td.td_flags = tdflags;
559                 cur->td.td_cbp = htole32(dataphys);
560                 cur->nexttd = next;
561                 cur->td.td_nexttd = htole32(next->physaddr);
562                 cur->td.td_be = htole32(physend);
563                 cur->len = curlen;
564                 cur->flags = OHCI_ADD_LEN;
565                 cur->xfer = xfer;
566                 DPRINTFN(10,("ohci_alloc_std_chain: cbp=0x%08x be=0x%08x\n",
567                             dataphys, dataphys + curlen - 1));
568                 if (len < 0)
569                         panic("Length went negative: %d curlen %d dma %p offset %08x", len, curlen, dma, (int)0);
570
571                 DPRINTFN(10,("ohci_alloc_std_chain: extend chain\n"));
572                 offset += curlen;
573                 end = cur;
574                 cur = next;
575         }
576         if (((flags & USBD_FORCE_SHORT_XFER) || alen == 0) &&
577             alen % UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize) == 0) {
578                 /* Force a 0 length transfer at the end. */
579                 next = ohci_alloc_std(sc);
580                 if (next == NULL)
581                         goto nomem;
582
583                 cur->td.td_flags = tdflags;
584                 cur->td.td_cbp = 0; /* indicate 0 length packet */
585                 cur->nexttd = next;
586                 cur->td.td_nexttd = htole32(next->physaddr);
587                 cur->td.td_be = ~0;
588                 cur->len = 0;
589                 cur->flags = 0;
590                 cur->xfer = xfer;
591                 DPRINTFN(2,("ohci_alloc_std_chain: add 0 xfer\n"));
592                 end = cur;
593         }
594         *ep = end;
595
596         return (USBD_NORMAL_COMPLETION);
597
598  nomem:
599         /* XXX free chain */
600         return (USBD_NOMEM);
601 }
602
603 #if 0
604 static void
605 ohci_free_std_chain(ohci_softc_t *sc, ohci_soft_td_t *std,
606                     ohci_soft_td_t *stdend)
607 {
608         ohci_soft_td_t *p;
609
610         for (; std != stdend; std = p) {
611                 p = std->nexttd;
612                 ohci_free_std(sc, std);
613         }
614 }
615 #endif
616
617 ohci_soft_itd_t *
618 ohci_alloc_sitd(ohci_softc_t *sc)
619 {
620         ohci_soft_itd_t *sitd;
621         usbd_status err;
622         int i, s, offs;
623         usb_dma_t dma;
624
625         if (sc->sc_freeitds == NULL) {
626                 DPRINTFN(2, ("ohci_alloc_sitd: allocating chunk\n"));
627                 err = usb_allocmem(&sc->sc_bus, OHCI_SITD_SIZE * OHCI_SITD_CHUNK,
628                           OHCI_ITD_ALIGN, &dma);
629                 if (err)
630                         return (NULL);
631                 s = splusb();
632                 for(i = 0; i < OHCI_SITD_CHUNK; i++) {
633                         offs = i * OHCI_SITD_SIZE;
634                         sitd = KERNADDR(&dma, offs);
635                         sitd->physaddr = DMAADDR(&dma, offs);
636                         sitd->nextitd = sc->sc_freeitds;
637                         sc->sc_freeitds = sitd;
638                 }
639                 splx(s);
640         }
641
642         s = splusb();
643         sitd = sc->sc_freeitds;
644         sc->sc_freeitds = sitd->nextitd;
645         memset(&sitd->itd, 0, sizeof(ohci_itd_t));
646         sitd->nextitd = NULL;
647         sitd->xfer = NULL;
648         ohci_hash_add_itd(sc, sitd);
649         splx(s);
650
651 #ifdef DIAGNOSTIC
652         sitd->isdone = 0;
653 #endif
654
655         return (sitd);
656 }
657
658 void
659 ohci_free_sitd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
660 {
661         int s;
662
663         DPRINTFN(10,("ohci_free_sitd: sitd=%p\n", sitd));
664
665 #ifdef DIAGNOSTIC
666         if (!sitd->isdone) {
667                 panic("ohci_free_sitd: sitd=%p not done", sitd);
668                 return;
669         }
670         /* Warn double free */
671         sitd->isdone = 0;
672 #endif
673
674         s = splusb();
675         ohci_hash_rem_itd(sc, sitd);
676         sitd->nextitd = sc->sc_freeitds;
677         sc->sc_freeitds = sitd;
678         splx(s);
679 }
680
681 usbd_status
682 ohci_init(ohci_softc_t *sc)
683 {
684         ohci_soft_ed_t *sed, *psed;
685         usbd_status err;
686         int i;
687         u_int32_t rev;
688
689         DPRINTF(("ohci_init: start\n"));
690         printf("%s:", device_get_nameunit(sc->sc_bus.bdev));
691         rev = OREAD4(sc, OHCI_REVISION);
692         printf(" OHCI version %d.%d%s\n", OHCI_REV_HI(rev), OHCI_REV_LO(rev),
693                OHCI_REV_LEGACY(rev) ? ", legacy support" : "");
694
695         if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
696                 printf("%s: unsupported OHCI revision\n",
697                        device_get_nameunit(sc->sc_bus.bdev));
698                 sc->sc_bus.usbrev = USBREV_UNKNOWN;
699                 return (USBD_INVAL);
700         }
701         sc->sc_bus.usbrev = USBREV_1_0;
702
703         for (i = 0; i < OHCI_HASH_SIZE; i++)
704                 LIST_INIT(&sc->sc_hash_tds[i]);
705         for (i = 0; i < OHCI_HASH_SIZE; i++)
706                 LIST_INIT(&sc->sc_hash_itds[i]);
707
708         STAILQ_INIT(&sc->sc_free_xfers);
709
710         /* XXX determine alignment by R/W */
711         /* Allocate the HCCA area. */
712         err = usb_allocmem(&sc->sc_bus, OHCI_HCCA_SIZE,
713                          OHCI_HCCA_ALIGN, &sc->sc_hccadma);
714         if (err)
715                 return (err);
716         sc->sc_hcca = KERNADDR(&sc->sc_hccadma, 0);
717         memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);
718
719         sc->sc_eintrs = OHCI_NORMAL_INTRS;
720
721         /* Allocate dummy ED that starts the control list. */
722         sc->sc_ctrl_head = ohci_alloc_sed(sc);
723         if (sc->sc_ctrl_head == NULL) {
724                 err = USBD_NOMEM;
725                 goto bad1;
726         }
727         sc->sc_ctrl_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
728
729         /* Allocate dummy ED that starts the bulk list. */
730         sc->sc_bulk_head = ohci_alloc_sed(sc);
731         if (sc->sc_bulk_head == NULL) {
732                 err = USBD_NOMEM;
733                 goto bad2;
734         }
735         sc->sc_bulk_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
736
737         /* Allocate dummy ED that starts the isochronous list. */
738         sc->sc_isoc_head = ohci_alloc_sed(sc);
739         if (sc->sc_isoc_head == NULL) {
740                 err = USBD_NOMEM;
741                 goto bad3;
742         }
743         sc->sc_isoc_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
744
745         /* Allocate all the dummy EDs that make up the interrupt tree. */
746         for (i = 0; i < OHCI_NO_EDS; i++) {
747                 sed = ohci_alloc_sed(sc);
748                 if (sed == NULL) {
749                         while (--i >= 0)
750                                 ohci_free_sed(sc, sc->sc_eds[i]);
751                         err = USBD_NOMEM;
752                         goto bad4;
753                 }
754                 /* All ED fields are set to 0. */
755                 sc->sc_eds[i] = sed;
756                 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
757                 if (i != 0)
758                         psed = sc->sc_eds[(i-1) / 2];
759                 else
760                         psed= sc->sc_isoc_head;
761                 sed->next = psed;
762                 sed->ed.ed_nexted = htole32(psed->physaddr);
763         }
764         /*
765          * Fill HCCA interrupt table.  The bit reversal is to get
766          * the tree set up properly to spread the interrupts.
767          */
768         for (i = 0; i < OHCI_NO_INTRS; i++)
769                 sc->sc_hcca->hcca_interrupt_table[revbits[i]] =
770                     htole32(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr);
771
772 #ifdef USB_DEBUG
773         if (ohcidebug > 15) {
774                 for (i = 0; i < OHCI_NO_EDS; i++) {
775                         printf("ed#%d ", i);
776                         ohci_dump_ed(sc->sc_eds[i]);
777                 }
778                 printf("iso ");
779                 ohci_dump_ed(sc->sc_isoc_head);
780         }
781 #endif
782
783         err = ohci_controller_init(sc);
784         if (err != USBD_NORMAL_COMPLETION)
785                 goto bad5;
786
787         /* Set up the bus struct. */
788         sc->sc_bus.methods = &ohci_bus_methods;
789         sc->sc_bus.pipe_size = sizeof(struct ohci_pipe);
790
791 #if defined(__NetBSD__) || defined(__OpenBSD__)
792         sc->sc_powerhook = powerhook_establish(ohci_power, sc);
793         sc->sc_shutdownhook = shutdownhook_establish(ohci_shutdown, sc);
794 #endif
795
796         callout_init(&sc->sc_tmo_rhsc, 0);
797
798         return (USBD_NORMAL_COMPLETION);
799
800  bad5:
801         for (i = 0; i < OHCI_NO_EDS; i++)
802                 ohci_free_sed(sc, sc->sc_eds[i]);
803  bad4:
804         ohci_free_sed(sc, sc->sc_isoc_head);
805  bad3:
806         ohci_free_sed(sc, sc->sc_bulk_head);
807  bad2:
808         ohci_free_sed(sc, sc->sc_ctrl_head);
809  bad1:
810         usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
811         return (err);
812 }
813
814 static usbd_status
815 ohci_controller_init(ohci_softc_t *sc)
816 {
817         int i;
818         u_int32_t ctl, ival, hcr, fm, per, desca;
819
820         /* Determine in what context we are running. */
821         ctl = OREAD4(sc, OHCI_CONTROL);
822         if (ctl & OHCI_IR) {
823                 /* SMM active, request change */
824                 DPRINTF(("ohci_init: SMM active, request owner change\n"));
825                 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_OCR);
826                 for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
827                         usb_delay_ms(&sc->sc_bus, 1);
828                         ctl = OREAD4(sc, OHCI_CONTROL);
829                 }
830                 if (ctl & OHCI_IR) {
831                         printf("%s: SMM does not respond, resetting\n",
832                                device_get_nameunit(sc->sc_bus.bdev));
833                         OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
834                         goto reset;
835                 }
836 #if 0
837 /* Don't bother trying to reuse the BIOS init, we'll reset it anyway. */
838         } else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) {
839                 /* BIOS started controller. */
840                 DPRINTF(("ohci_init: BIOS active\n"));
841                 if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) {
842                         OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL);
843                         usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
844                 }
845 #endif
846         } else {
847                 DPRINTF(("ohci_init: cold started\n"));
848         reset:
849                 /* Controller was cold started. */
850                 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
851         }
852
853         /*
854          * This reset should not be necessary according to the OHCI spec, but
855          * without it some controllers do not start.
856          */
857         DPRINTF(("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev)));
858         OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
859         usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
860
861         /* We now own the host controller and the bus has been reset. */
862         ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
863
864         OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
865         /* Nominal time for a reset is 10 us. */
866         for (i = 0; i < 10; i++) {
867                 delay(10);
868                 hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
869                 if (!hcr)
870                         break;
871         }
872         if (hcr) {
873                 printf("%s: reset timeout\n", device_get_nameunit(sc->sc_bus.bdev));
874                 return (USBD_IOERROR);
875         }
876 #ifdef USB_DEBUG
877         if (ohcidebug > 15)
878                 ohci_dumpregs(sc);
879 #endif
880
881         /* The controller is now in SUSPEND state, we have 2ms to finish. */
882
883         /* Set up HC registers. */
884         OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
885         OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
886         OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
887         /* disable all interrupts and then switch on all desired interrupts */
888         OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
889         OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);
890         /* switch on desired functional features */
891         ctl = OREAD4(sc, OHCI_CONTROL);
892         ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
893         ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
894                 OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
895         /* And finally start it! */
896         OWRITE4(sc, OHCI_CONTROL, ctl);
897
898         /*
899          * The controller is now OPERATIONAL.  Set a some final
900          * registers that should be set earlier, but that the
901          * controller ignores when in the SUSPEND state.
902          */
903         fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
904         fm |= OHCI_FSMPS(ival) | ival;
905         OWRITE4(sc, OHCI_FM_INTERVAL, fm);
906         per = OHCI_PERIODIC(ival); /* 90% periodic */
907         OWRITE4(sc, OHCI_PERIODIC_START, per);
908
909         /* Fiddle the No OverCurrent Protection bit to avoid chip bug. */
910         desca = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
911         OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca | OHCI_NOCP);
912         OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */
913         usb_delay_ms(&sc->sc_bus, OHCI_ENABLE_POWER_DELAY);
914         OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca);
915
916         /*
917          * The AMD756 requires a delay before re-reading the register,
918          * otherwise it will occasionally report 0 ports.
919          */
920         sc->sc_noport = 0;
921         for (i = 0; i < 10 && sc->sc_noport == 0; i++) {
922                 usb_delay_ms(&sc->sc_bus, OHCI_READ_DESC_DELAY);
923                 sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
924         }
925
926 #ifdef USB_DEBUG
927         if (ohcidebug > 5)
928                 ohci_dumpregs(sc);
929 #endif
930         return (USBD_NORMAL_COMPLETION);
931 }
932
933 usbd_status
934 ohci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
935 {
936         return (usb_allocmem(bus, size, 0, dma));
937 }
938
939 void
940 ohci_freem(struct usbd_bus *bus, usb_dma_t *dma)
941 {
942         usb_freemem(bus, dma);
943 }
944
945 usbd_xfer_handle
946 ohci_allocx(struct usbd_bus *bus)
947 {
948         struct ohci_softc *sc = (struct ohci_softc *)bus;
949         usbd_xfer_handle xfer;
950
951         xfer = STAILQ_FIRST(&sc->sc_free_xfers);
952         if (xfer != NULL) {
953                 STAILQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
954 #ifdef DIAGNOSTIC
955                 if (xfer->busy_free != XFER_FREE) {
956                         printf("ohci_allocx: xfer=%p not free, 0x%08x\n", xfer,
957                                xfer->busy_free);
958                 }
959 #endif
960         } else {
961                 xfer = malloc(sizeof(struct ohci_xfer), M_USB, M_NOWAIT);
962         }
963         if (xfer != NULL) {
964                 memset(xfer, 0, sizeof (struct ohci_xfer));
965                 usb_init_task(&OXFER(xfer)->abort_task, ohci_timeout_task,
966                     xfer);
967                 OXFER(xfer)->ohci_xfer_flags = 0;
968 #ifdef DIAGNOSTIC
969                 xfer->busy_free = XFER_BUSY;
970 #endif
971         }
972         return (xfer);
973 }
974
975 void
976 ohci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
977 {
978         struct ohci_softc *sc = (struct ohci_softc *)bus;
979
980 #ifdef DIAGNOSTIC
981         if (xfer->busy_free != XFER_BUSY) {
982                 printf("ohci_freex: xfer=%p not busy, 0x%08x\n", xfer,
983                        xfer->busy_free);
984                 return;
985         }
986         xfer->busy_free = XFER_FREE;
987 #endif
988         STAILQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
989 }
990
991 /*
992  * Shut down the controller when the system is going down.
993  */
994 void
995 ohci_shutdown(void *v)
996 {
997         ohci_softc_t *sc = v;
998
999         DPRINTF(("ohci_shutdown: stopping the HC\n"));
1000         OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1001 }
1002
1003 /*
1004  * Handle suspend/resume.
1005  *
1006  * We need to switch to polling mode here, because this routine is
1007  * called from an intterupt context.  This is all right since we
1008  * are almost suspended anyway.
1009  */
1010 void
1011 ohci_power(int why, void *v)
1012 {
1013         ohci_softc_t *sc = v;
1014         u_int32_t ctl;
1015         int s;
1016
1017 #ifdef USB_DEBUG
1018         DPRINTF(("ohci_power: sc=%p, why=%d\n", sc, why));
1019         ohci_dumpregs(sc);
1020 #endif
1021
1022         s = splhardusb();
1023         if (why != PWR_RESUME) {
1024                 sc->sc_bus.use_polling++;
1025                 ctl = OREAD4(sc, OHCI_CONTROL) & ~OHCI_HCFS_MASK;
1026                 if (sc->sc_control == 0) {
1027                         /*
1028                          * Preserve register values, in case that APM BIOS
1029                          * does not recover them.
1030                          */
1031                         sc->sc_control = ctl;
1032                         sc->sc_intre = OREAD4(sc, OHCI_INTERRUPT_ENABLE);
1033                 }
1034                 ctl |= OHCI_HCFS_SUSPEND;
1035                 OWRITE4(sc, OHCI_CONTROL, ctl);
1036                 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
1037                 sc->sc_bus.use_polling--;
1038         } else {
1039                 sc->sc_bus.use_polling++;
1040
1041                 /* Some broken BIOSes never initialize Controller chip */
1042                 ohci_controller_init(sc);
1043
1044                 if (sc->sc_intre)
1045                         OWRITE4(sc, OHCI_INTERRUPT_ENABLE,
1046                                 sc->sc_intre & (OHCI_ALL_INTRS | OHCI_MIE));
1047                 if (sc->sc_control)
1048                         ctl = sc->sc_control;
1049                 else
1050                         ctl = OREAD4(sc, OHCI_CONTROL);
1051                 ctl |= OHCI_HCFS_RESUME;
1052                 OWRITE4(sc, OHCI_CONTROL, ctl);
1053                 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
1054                 ctl = (ctl & ~OHCI_HCFS_MASK) | OHCI_HCFS_OPERATIONAL;
1055                 OWRITE4(sc, OHCI_CONTROL, ctl);
1056                 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
1057                 sc->sc_control = sc->sc_intre = 0;
1058                 sc->sc_bus.use_polling--;
1059         }
1060         splx(s);
1061 }
1062
1063 #ifdef USB_DEBUG
1064 void
1065 ohci_dumpregs(ohci_softc_t *sc)
1066 {
1067         DPRINTF(("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
1068                  OREAD4(sc, OHCI_REVISION),
1069                  OREAD4(sc, OHCI_CONTROL),
1070                  OREAD4(sc, OHCI_COMMAND_STATUS)));
1071         DPRINTF(("               intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
1072                  OREAD4(sc, OHCI_INTERRUPT_STATUS),
1073                  OREAD4(sc, OHCI_INTERRUPT_ENABLE),
1074                  OREAD4(sc, OHCI_INTERRUPT_DISABLE)));
1075         DPRINTF(("               hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
1076                  OREAD4(sc, OHCI_HCCA),
1077                  OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
1078                  OREAD4(sc, OHCI_CONTROL_HEAD_ED)));
1079         DPRINTF(("               ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
1080                  OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
1081                  OREAD4(sc, OHCI_BULK_HEAD_ED),
1082                  OREAD4(sc, OHCI_BULK_CURRENT_ED)));
1083         DPRINTF(("               done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
1084                  OREAD4(sc, OHCI_DONE_HEAD),
1085                  OREAD4(sc, OHCI_FM_INTERVAL),
1086                  OREAD4(sc, OHCI_FM_REMAINING)));
1087         DPRINTF(("               fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
1088                  OREAD4(sc, OHCI_FM_NUMBER),
1089                  OREAD4(sc, OHCI_PERIODIC_START),
1090                  OREAD4(sc, OHCI_LS_THRESHOLD)));
1091         DPRINTF(("               desca=0x%08x descb=0x%08x stat=0x%08x\n",
1092                  OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
1093                  OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
1094                  OREAD4(sc, OHCI_RH_STATUS)));
1095         DPRINTF(("               port1=0x%08x port2=0x%08x\n",
1096                  OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
1097                  OREAD4(sc, OHCI_RH_PORT_STATUS(2))));
1098         DPRINTF(("         HCCA: frame_number=0x%04x done_head=0x%08x\n",
1099                  le32toh(sc->sc_hcca->hcca_frame_number),
1100                  le32toh(sc->sc_hcca->hcca_done_head)));
1101 }
1102 #endif
1103
1104 static int ohci_intr1(ohci_softc_t *);
1105
1106 void
1107 ohci_intr(void *p)
1108 {
1109         ohci_softc_t *sc = p;
1110
1111         if (sc == NULL || sc->sc_dying)
1112                 return;
1113
1114         /* If we get an interrupt while polling, then just ignore it. */
1115         if (sc->sc_bus.use_polling) {
1116 #ifdef DIAGNOSTIC
1117                 printf("ohci_intr: ignored interrupt while polling\n");
1118 #endif
1119                 return;
1120         }
1121
1122         ohci_intr1(sc);
1123 }
1124
1125 static int
1126 ohci_intr1(ohci_softc_t *sc)
1127 {
1128         u_int32_t intrs, eintrs;
1129         ohci_physaddr_t done;
1130
1131         DPRINTFN(14,("ohci_intr1: enter\n"));
1132
1133         /* In case the interrupt occurs before initialization has completed. */
1134         if (sc == NULL || sc->sc_hcca == NULL) {
1135 #ifdef DIAGNOSTIC
1136                 printf("ohci_intr: sc->sc_hcca == NULL\n");
1137 #endif
1138                 return (0);
1139         }
1140
1141         intrs = 0;
1142         done = le32toh(sc->sc_hcca->hcca_done_head);
1143
1144         /* The LSb of done is used to inform the HC Driver that an interrupt
1145          * condition exists for both the Done list and for another event
1146          * recorded in HcInterruptStatus. On an interrupt from the HC, the HC
1147          * Driver checks the HccaDoneHead Value. If this value is 0, then the
1148          * interrupt was caused by other than the HccaDoneHead update and the
1149          * HcInterruptStatus register needs to be accessed to determine that
1150          * exact interrupt cause. If HccaDoneHead is nonzero, then a Done list
1151          * update interrupt is indicated and if the LSb of done is nonzero,
1152          * then an additional interrupt event is indicated and
1153          * HcInterruptStatus should be checked to determine its cause.
1154          */
1155         if (done != 0) {
1156                 if (done & ~OHCI_DONE_INTRS)
1157                         intrs = OHCI_WDH;
1158                 if (done & OHCI_DONE_INTRS) {
1159                         intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
1160                         done &= ~OHCI_DONE_INTRS;
1161                 }
1162                 sc->sc_hcca->hcca_done_head = 0;
1163         } else
1164                 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & ~OHCI_WDH;
1165
1166         if (intrs == 0)         /* nothing to be done (PCI shared interrupt) */
1167                 return (0);
1168
1169         intrs &= ~OHCI_MIE;
1170         OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs); /* Acknowledge */
1171         eintrs = intrs & sc->sc_eintrs;
1172         if (!eintrs)
1173                 return (0);
1174
1175         sc->sc_bus.intr_context++;
1176         sc->sc_bus.no_intrs++;
1177         DPRINTFN(7, ("ohci_intr: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
1178                      sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS),
1179                      (u_int)eintrs));
1180
1181         if (eintrs & OHCI_SO) {
1182                 sc->sc_overrun_cnt++;
1183                 if (usbd_ratecheck(&sc->sc_overrun_ntc)) {
1184                         printf("%s: %u scheduling overruns\n",
1185                             device_get_nameunit(sc->sc_bus.bdev), sc->sc_overrun_cnt);
1186                         sc->sc_overrun_cnt = 0;
1187                 }
1188                 /* XXX do what */
1189                 eintrs &= ~OHCI_SO;
1190         }
1191         if (eintrs & OHCI_WDH) {
1192                 ohci_add_done(sc, done &~ OHCI_DONE_INTRS);
1193                 usb_schedsoftintr(&sc->sc_bus);
1194                 eintrs &= ~OHCI_WDH;
1195         }
1196         if (eintrs & OHCI_RD) {
1197                 printf("%s: resume detect\n", device_get_nameunit(sc->sc_bus.bdev));
1198                 /* XXX process resume detect */
1199         }
1200         if (eintrs & OHCI_UE) {
1201                 printf("%s: unrecoverable error, controller halted\n",
1202                        device_get_nameunit(sc->sc_bus.bdev));
1203                 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1204                 /* XXX what else */
1205         }
1206         if (eintrs & OHCI_RHSC) {
1207                 ohci_rhsc(sc, sc->sc_intrxfer);
1208                 /*
1209                  * Disable RHSC interrupt for now, because it will be
1210                  * on until the port has been reset.
1211                  */
1212                 ohci_rhsc_able(sc, 0);
1213                 /* Do not allow RHSC interrupts > 1 per second */
1214                 callout_reset(&sc->sc_tmo_rhsc, hz, ohci_rhsc_enable, sc);
1215                 eintrs &= ~OHCI_RHSC;
1216         }
1217
1218         sc->sc_bus.intr_context--;
1219
1220         if (eintrs != 0) {
1221                 /* Block unprocessed interrupts. XXX */
1222                 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, eintrs);
1223                 sc->sc_eintrs &= ~eintrs;
1224                 printf("%s: blocking intrs 0x%x\n",
1225                        device_get_nameunit(sc->sc_bus.bdev), eintrs);
1226         }
1227
1228         return (1);
1229 }
1230
1231 void
1232 ohci_rhsc_able(ohci_softc_t *sc, int on)
1233 {
1234         DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on));
1235         if (on) {
1236                 sc->sc_eintrs |= OHCI_RHSC;
1237                 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
1238         } else {
1239                 sc->sc_eintrs &= ~OHCI_RHSC;
1240                 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
1241         }
1242 }
1243
1244 void
1245 ohci_rhsc_enable(void *v_sc)
1246 {
1247         ohci_softc_t *sc = v_sc;
1248         int s;
1249
1250         s = splhardusb();
1251         ohci_rhsc_able(sc, 1);
1252         splx(s);
1253 }
1254
1255 #ifdef USB_DEBUG
1256 char *ohci_cc_strs[] = {
1257         "NO_ERROR",
1258         "CRC",
1259         "BIT_STUFFING",
1260         "DATA_TOGGLE_MISMATCH",
1261         "STALL",
1262         "DEVICE_NOT_RESPONDING",
1263         "PID_CHECK_FAILURE",
1264         "UNEXPECTED_PID",
1265         "DATA_OVERRUN",
1266         "DATA_UNDERRUN",
1267         "BUFFER_OVERRUN",
1268         "BUFFER_UNDERRUN",
1269         "reserved",
1270         "reserved",
1271         "NOT_ACCESSED",
1272         "NOT_ACCESSED"
1273 };
1274 #endif
1275
1276 void
1277 ohci_add_done(ohci_softc_t *sc, ohci_physaddr_t done)
1278 {
1279         ohci_soft_itd_t *sitd, *sidone, **ip;
1280         ohci_soft_td_t  *std,  *sdone,  **p;
1281
1282         /* Reverse the done list. */
1283         for (sdone = NULL, sidone = NULL; done != 0; ) {
1284                 std = ohci_hash_find_td(sc, done);
1285                 if (std != NULL) {
1286                         std->dnext = sdone;
1287                         done = le32toh(std->td.td_nexttd);
1288                         sdone = std;
1289                         DPRINTFN(10,("add TD %p\n", std));
1290                         continue;
1291                 }
1292                 sitd = ohci_hash_find_itd(sc, done);
1293                 if (sitd != NULL) {
1294                         sitd->dnext = sidone;
1295                         done = le32toh(sitd->itd.itd_nextitd);
1296                         sidone = sitd;
1297                         DPRINTFN(5,("add ITD %p\n", sitd));
1298                         continue;
1299                 }
1300                 panic("ohci_add_done: addr 0x%08lx not found", (u_long)done);
1301         }
1302
1303         /* sdone & sidone now hold the done lists. */
1304         /* Put them on the already processed lists. */
1305         for (p = &sc->sc_sdone; *p != NULL; p = &(*p)->dnext)
1306                 ;
1307         *p = sdone;
1308         for (ip = &sc->sc_sidone; *ip != NULL; ip = &(*ip)->dnext)
1309                 ;
1310         *ip = sidone;
1311 }
1312
1313 void
1314 ohci_softintr(void *v)
1315 {
1316         ohci_softc_t *sc = v;
1317         ohci_soft_itd_t *sitd, *sidone, *sitdnext;
1318         ohci_soft_td_t  *std,  *sdone,  *stdnext, *p, *n;
1319         usbd_xfer_handle xfer;
1320         struct ohci_pipe *opipe;
1321         int len, cc, s;
1322         int i, j, iframes;
1323         
1324         DPRINTFN(10,("ohci_softintr: enter\n"));
1325
1326         sc->sc_bus.intr_context++;
1327
1328         s = splhardusb();
1329         sdone = sc->sc_sdone;
1330         sc->sc_sdone = NULL;
1331         sidone = sc->sc_sidone;
1332         sc->sc_sidone = NULL;
1333         splx(s);
1334
1335         DPRINTFN(10,("ohci_softintr: sdone=%p sidone=%p\n", sdone, sidone));
1336
1337 #ifdef USB_DEBUG
1338         if (ohcidebug > 10) {
1339                 DPRINTF(("ohci_process_done: TD done:\n"));
1340                 ohci_dump_tds(sdone);
1341         }
1342 #endif
1343
1344         for (std = sdone; std; std = stdnext) {
1345                 xfer = std->xfer;
1346                 stdnext = std->dnext;
1347                 DPRINTFN(10, ("ohci_process_done: std=%p xfer=%p hcpriv=%p\n",
1348                                 std, xfer, (xfer ? xfer->hcpriv : NULL)));
1349                 if (xfer == NULL) {
1350                         /*
1351                          * xfer == NULL: There seems to be no xfer associated
1352                          * with this TD. It is tailp that happened to end up on
1353                          * the done queue.
1354                          */
1355                         continue;
1356                 }
1357                 if (xfer->status == USBD_CANCELLED ||
1358                     xfer->status == USBD_TIMEOUT) {
1359                         DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1360                                  xfer));
1361                         /* Handled by abort routine. */
1362                         continue;
1363                 }
1364
1365                 len = std->len;
1366                 if (std->td.td_cbp != 0)
1367                         len -= le32toh(std->td.td_be) -
1368                                le32toh(std->td.td_cbp) + 1;
1369                 DPRINTFN(10, ("ohci_process_done: len=%d, flags=0x%x\n", len,
1370                     std->flags));
1371                 if (std->flags & OHCI_ADD_LEN)
1372                         xfer->actlen += len;
1373
1374                 cc = OHCI_TD_GET_CC(le32toh(std->td.td_flags));
1375                 if (cc != OHCI_CC_NO_ERROR) {
1376                         /*
1377                          * Endpoint is halted.  First unlink all the TDs
1378                          * belonging to the failed transfer, and then restart
1379                          * the endpoint.
1380                          */
1381                         opipe = (struct ohci_pipe *)xfer->pipe;
1382
1383                         DPRINTFN(15,("ohci_process_done: error cc=%d (%s)\n",
1384                           OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
1385                           ohci_cc_strs[OHCI_TD_GET_CC(le32toh(std->td.td_flags))]));
1386                         callout_stop(&xfer->timeout_handle);
1387                         usb_rem_task(OXFER(xfer)->xfer.pipe->device,
1388                             &OXFER(xfer)->abort_task);
1389
1390                         /* Remove all this xfer's TDs from the done queue. */
1391                         for (p = std; p->dnext != NULL; p = p->dnext) {
1392                                 if (p->dnext->xfer != xfer)
1393                                         continue;
1394                                 p->dnext = p->dnext->dnext;
1395                         }
1396                         /* The next TD may have been removed. */
1397                         stdnext = std->dnext;
1398
1399                         /* Remove all TDs belonging to this xfer. */
1400                         for (p = xfer->hcpriv; p->xfer == xfer; p = n) {
1401                                 n = p->nexttd;
1402                                 ohci_free_std(sc, p);
1403                         }
1404
1405                         /* clear halt */
1406                         opipe->sed->ed.ed_headp = htole32(p->physaddr);
1407                         OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1408
1409                         if (cc == OHCI_CC_STALL)
1410                                 xfer->status = USBD_STALLED;
1411                         else
1412                                 xfer->status = USBD_IOERROR;
1413                         s = splusb();
1414                         usb_transfer_complete(xfer);
1415                         splx(s);
1416                         continue;
1417                 }
1418                 /*
1419                  * Skip intermediate TDs. They remain linked from
1420                  * xfer->hcpriv and we free them when the transfer completes.
1421                  */
1422                 if ((std->flags & OHCI_CALL_DONE) == 0)
1423                         continue;
1424
1425                 /* Normal transfer completion */
1426                 callout_stop(&xfer->timeout_handle);
1427                 usb_rem_task(OXFER(xfer)->xfer.pipe->device,
1428                     &OXFER(xfer)->abort_task);
1429                 for (p = xfer->hcpriv; p->xfer == xfer; p = n) {
1430                         n = p->nexttd;
1431                         ohci_free_std(sc, p);
1432                 }
1433                 xfer->status = USBD_NORMAL_COMPLETION;
1434                 s = splusb();
1435                 usb_transfer_complete(xfer);
1436                 splx(s);
1437         }
1438
1439 #ifdef USB_DEBUG
1440         if (ohcidebug > 10) {
1441                 DPRINTF(("ohci_softintr: ITD done:\n"));
1442                 ohci_dump_itds(sidone);
1443         }
1444 #endif
1445
1446         for (sitd = sidone; sitd != NULL; sitd = sitdnext) {
1447                 xfer = sitd->xfer;
1448                 sitdnext = sitd->dnext;
1449                 sitd->flags |= OHCI_ITD_INTFIN;
1450                 DPRINTFN(1, ("ohci_process_done: sitd=%p xfer=%p hcpriv=%p\n",
1451                              sitd, xfer, xfer ? xfer->hcpriv : 0));
1452                 if (xfer == NULL)
1453                         continue;
1454                 if (xfer->status == USBD_CANCELLED ||
1455                     xfer->status == USBD_TIMEOUT) {
1456                         DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1457                                  xfer));
1458                         /* Handled by abort routine. */
1459                         continue;
1460                 }
1461                 if (xfer->pipe)
1462                         if (xfer->pipe->aborting)
1463                                 continue; /*Ignore.*/
1464 #ifdef DIAGNOSTIC
1465                 if (sitd->isdone)
1466                         printf("ohci_softintr: sitd=%p is done\n", sitd);
1467                 sitd->isdone = 1;
1468 #endif
1469                 opipe = (struct ohci_pipe *)xfer->pipe;
1470                 if (opipe->aborting)
1471                         continue;
1472  
1473                 if (sitd->flags & OHCI_CALL_DONE) {
1474                         ohci_soft_itd_t *next;
1475
1476                         opipe->u.iso.inuse -= xfer->nframes;
1477                         xfer->status = USBD_NORMAL_COMPLETION;
1478                         for (i = 0, sitd = xfer->hcpriv;;sitd = next) {
1479                                 next = sitd->nextitd;
1480                                 if (OHCI_ITD_GET_CC(sitd->itd.itd_flags) != OHCI_CC_NO_ERROR)
1481                                         xfer->status = USBD_IOERROR;
1482
1483                                 if (xfer->status == USBD_NORMAL_COMPLETION) {
1484                                         iframes = OHCI_ITD_GET_FC(sitd->itd.itd_flags);
1485                                         for (j = 0; j < iframes; i++, j++) {
1486                                                 len = le16toh(sitd->itd.itd_offset[j]);
1487                                                 len =
1488                                                    (OHCI_ITD_PSW_GET_CC(len) ==
1489                                                     OHCI_CC_NOT_ACCESSED) ? 0 :
1490                                                     OHCI_ITD_PSW_LENGTH(len);
1491                                                 xfer->frlengths[i] = len;
1492                                         }
1493                                 }
1494                                 if (sitd->flags & OHCI_CALL_DONE)
1495                                         break;
1496                         }
1497                         for (sitd = xfer->hcpriv; sitd->xfer == xfer;
1498                             sitd = next) {
1499                                 next = sitd->nextitd;
1500                                 ohci_free_sitd(sc, sitd);
1501                         }
1502
1503                         s = splusb();
1504                         usb_transfer_complete(xfer);
1505                         splx(s);
1506                 }
1507         }
1508
1509 #ifdef USB_USE_SOFTINTR
1510         if (sc->sc_softwake) {
1511                 sc->sc_softwake = 0;
1512                 wakeup(&sc->sc_softwake);
1513         }
1514 #endif /* USB_USE_SOFTINTR */
1515
1516         sc->sc_bus.intr_context--;
1517         DPRINTFN(10,("ohci_softintr: done:\n"));
1518 }
1519
1520 void
1521 ohci_device_ctrl_done(usbd_xfer_handle xfer)
1522 {
1523         DPRINTFN(10,("ohci_device_ctrl_done: xfer=%p\n", xfer));
1524
1525 #ifdef DIAGNOSTIC
1526         if (!(xfer->rqflags & URQ_REQUEST)) {
1527                 panic("ohci_device_ctrl_done: not a request");
1528         }
1529 #endif
1530         xfer->hcpriv = NULL;
1531 }
1532
1533 void
1534 ohci_device_intr_done(usbd_xfer_handle xfer)
1535 {
1536         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1537         ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1538         usbd_status err;
1539
1540         DPRINTFN(10,("ohci_device_intr_done: xfer=%p, actlen=%d\n",
1541                      xfer, xfer->actlen));
1542
1543         xfer->hcpriv = NULL;
1544         if (xfer->pipe->repeat) {
1545                 err = ohci_device_intr_insert(sc, xfer);
1546                 if (err) {
1547                         xfer->status = err;
1548                         return;
1549                 }
1550         }
1551 }
1552
1553 void
1554 ohci_device_bulk_done(usbd_xfer_handle xfer)
1555 {
1556         DPRINTFN(10,("ohci_device_bulk_done: xfer=%p, actlen=%d\n",
1557                      xfer, xfer->actlen));
1558
1559         xfer->hcpriv = NULL;
1560 }
1561
1562 /*
1563  * XXX write back xfer data for architectures with a write-back
1564  *     data cache; this is a hack because usb is mis-architected
1565  *     in blindly mixing bus_dma w/ PIO.
1566  */
1567 static __inline void
1568 hacksync(usbd_xfer_handle xfer)
1569 {
1570         bus_dma_tag_t tag;
1571         struct usb_dma_mapping *dmap;
1572
1573         if (xfer->length == 0)
1574                 return;
1575         tag = xfer->pipe->device->bus->buffer_dmatag;
1576         dmap = &xfer->dmamap;
1577         bus_dmamap_sync(tag, dmap->map, BUS_DMASYNC_PREWRITE);
1578 }
1579
1580 void
1581 ohci_rhsc(ohci_softc_t *sc, usbd_xfer_handle xfer)
1582 {
1583         usbd_pipe_handle pipe;
1584         u_char *p;
1585         int i, m;
1586         int hstatus;
1587
1588         hstatus = OREAD4(sc, OHCI_RH_STATUS);
1589         DPRINTF(("ohci_rhsc: sc=%p xfer=%p hstatus=0x%08x\n",
1590                  sc, xfer, hstatus));
1591
1592         if (xfer == NULL) {
1593                 /* Just ignore the change. */
1594                 return;
1595         }
1596
1597         pipe = xfer->pipe;
1598
1599         p = xfer->buffer;
1600         m = min(sc->sc_noport, xfer->length * 8 - 1);
1601         memset(p, 0, xfer->length);
1602         for (i = 1; i <= m; i++) {
1603                 /* Pick out CHANGE bits from the status reg. */
1604                 if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
1605                         p[i/8] |= 1 << (i%8);
1606         }
1607         DPRINTF(("ohci_rhsc: change=0x%02x\n", *p));
1608         xfer->actlen = xfer->length;
1609         xfer->status = USBD_NORMAL_COMPLETION;
1610
1611         hacksync(xfer); /* XXX to compensate for usb_transfer_complete */
1612         usb_transfer_complete(xfer);
1613 }
1614
1615 void
1616 ohci_root_intr_done(usbd_xfer_handle xfer)
1617 {
1618         xfer->hcpriv = NULL;
1619 }
1620
1621 void
1622 ohci_root_ctrl_done(usbd_xfer_handle xfer)
1623 {
1624         xfer->hcpriv = NULL;
1625 }
1626
1627 /*
1628  * Wait here until controller claims to have an interrupt.
1629  * Then call ohci_intr and return.  Use timeout to avoid waiting
1630  * too long.
1631  */
1632 void
1633 ohci_waitintr(ohci_softc_t *sc, usbd_xfer_handle xfer)
1634 {
1635         int timo = xfer->timeout;
1636         int usecs;
1637         u_int32_t intrs;
1638
1639         xfer->status = USBD_IN_PROGRESS;
1640         for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
1641                 usb_delay_ms(&sc->sc_bus, 1);
1642                 if (sc->sc_dying)
1643                         break;
1644                 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs;
1645                 DPRINTFN(15,("ohci_waitintr: 0x%04x\n", intrs));
1646 #ifdef USB_DEBUG
1647                 if (ohcidebug > 15)
1648                         ohci_dumpregs(sc);
1649 #endif
1650                 if (intrs) {
1651                         ohci_intr1(sc);
1652                         if (xfer->status != USBD_IN_PROGRESS)
1653                                 return;
1654                 }
1655         }
1656
1657         /* Timeout */
1658         DPRINTF(("ohci_waitintr: timeout\n"));
1659         xfer->status = USBD_TIMEOUT;
1660         usb_transfer_complete(xfer);
1661         /* XXX should free TD */
1662 }
1663
1664 void
1665 ohci_poll(struct usbd_bus *bus)
1666 {
1667         ohci_softc_t *sc = (ohci_softc_t *)bus;
1668 #ifdef USB_DEBUG
1669         static int last;
1670         int new;
1671         new = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1672         if (new != last) {
1673                 DPRINTFN(10,("ohci_poll: intrs=0x%04x\n", new));
1674                 last = new;
1675         }
1676 #endif
1677
1678         if (OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs)
1679                 ohci_intr1(sc);
1680 }
1681
1682 usbd_status
1683 ohci_device_request(usbd_xfer_handle xfer)
1684 {
1685         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1686         usb_device_request_t *req = &xfer->request;
1687         usbd_device_handle dev = opipe->pipe.device;
1688         ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1689         ohci_soft_td_t *setup, *stat, *next, *tail;
1690         ohci_soft_ed_t *sed;
1691         int isread;
1692         int len;
1693         usbd_status err;
1694         int s;
1695
1696         isread = req->bmRequestType & UT_READ;
1697         len = UGETW(req->wLength);
1698
1699         DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, "
1700                     "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1701                     req->bmRequestType, req->bRequest, UGETW(req->wValue),
1702                     UGETW(req->wIndex), len, dev->address,
1703                     opipe->pipe.endpoint->edesc->bEndpointAddress));
1704
1705         setup = opipe->tail.td;
1706         stat = ohci_alloc_std(sc);
1707         if (stat == NULL) {
1708                 err = USBD_NOMEM;
1709                 goto bad1;
1710         }
1711         tail = ohci_alloc_std(sc);
1712         if (tail == NULL) {
1713                 err = USBD_NOMEM;
1714                 goto bad2;
1715         }
1716         tail->xfer = NULL;
1717
1718         sed = opipe->sed;
1719         opipe->u.ctl.length = len;
1720         next = stat;
1721
1722         /* Set up data transaction */
1723         if (len != 0) {
1724                 ohci_soft_td_t *std = stat;
1725
1726                 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
1727                           std, &stat);
1728                 stat = stat->nexttd; /* point at free TD */
1729                 if (err)
1730                         goto bad3;
1731                 /* Start toggle at 1 and then use the carried toggle. */
1732                 std->td.td_flags &= htole32(~OHCI_TD_TOGGLE_MASK);
1733                 std->td.td_flags |= htole32(OHCI_TD_TOGGLE_1);
1734         }
1735
1736         memcpy(KERNADDR(&opipe->u.ctl.reqdma, 0), req, sizeof *req);
1737
1738         setup->td.td_flags = htole32(OHCI_TD_SETUP | OHCI_TD_NOCC |
1739                                      OHCI_TD_TOGGLE_0 | OHCI_TD_SET_DI(6));
1740         setup->td.td_cbp = htole32(DMAADDR(&opipe->u.ctl.reqdma, 0));
1741         setup->nexttd = next;
1742         setup->td.td_nexttd = htole32(next->physaddr);
1743         setup->td.td_be = htole32(le32toh(setup->td.td_cbp) + sizeof *req - 1);
1744         setup->len = 0;
1745         setup->xfer = xfer;
1746         setup->flags = 0;
1747         xfer->hcpriv = setup;
1748
1749         stat->td.td_flags = htole32(
1750                 (isread ? OHCI_TD_OUT : OHCI_TD_IN) |
1751                 OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1752         stat->td.td_cbp = 0;
1753         stat->nexttd = tail;
1754         stat->td.td_nexttd = htole32(tail->physaddr);
1755         stat->td.td_be = 0;
1756         stat->flags = OHCI_CALL_DONE;
1757         stat->len = 0;
1758         stat->xfer = xfer;
1759
1760 #ifdef USB_DEBUG
1761         if (ohcidebug > 5) {
1762                 DPRINTF(("ohci_device_request:\n"));
1763                 ohci_dump_ed(sed);
1764                 ohci_dump_tds(setup);
1765         }
1766 #endif
1767
1768         /* Insert ED in schedule */
1769         s = splusb();
1770         sed->ed.ed_tailp = htole32(tail->physaddr);
1771         opipe->tail.td = tail;
1772         OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1773         if (xfer->timeout && !sc->sc_bus.use_polling) {
1774                 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1775                     ohci_timeout, xfer);
1776         }
1777         splx(s);
1778
1779 #ifdef USB_DEBUG
1780         if (ohcidebug > 20) {
1781                 delay(10000);
1782                 DPRINTF(("ohci_device_request: status=%x\n",
1783                          OREAD4(sc, OHCI_COMMAND_STATUS)));
1784                 ohci_dumpregs(sc);
1785                 printf("ctrl head:\n");
1786                 ohci_dump_ed(sc->sc_ctrl_head);
1787                 printf("sed:\n");
1788                 ohci_dump_ed(sed);
1789                 ohci_dump_tds(setup);
1790         }
1791 #endif
1792
1793         return (USBD_NORMAL_COMPLETION);
1794
1795  bad3:
1796         ohci_free_std(sc, tail);
1797  bad2:
1798         ohci_free_std(sc, stat);
1799  bad1:
1800         return (err);
1801 }
1802
1803 /*
1804  * Add an ED to the schedule.  Called at splusb().
1805  */
1806 void
1807 ohci_add_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1808 {
1809         DPRINTFN(8,("ohci_add_ed: sed=%p head=%p\n", sed, head));
1810
1811         SPLUSBCHECK;
1812         sed->next = head->next;
1813         sed->ed.ed_nexted = head->ed.ed_nexted;
1814         head->next = sed;
1815         head->ed.ed_nexted = htole32(sed->physaddr);
1816 }
1817
1818 /*
1819  * Remove an ED from the schedule.  Called at splusb().
1820  */
1821 void
1822 ohci_rem_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1823 {
1824         ohci_soft_ed_t *p;
1825
1826         SPLUSBCHECK;
1827
1828         /* XXX */
1829         for (p = head; p != NULL && p->next != sed; p = p->next)
1830                 ;
1831         if (p == NULL)
1832                 panic("ohci_rem_ed: ED not found");
1833         p->next = sed->next;
1834         p->ed.ed_nexted = sed->ed.ed_nexted;
1835 }
1836
1837 /*
1838  * When a transfer is completed the TD is added to the done queue by
1839  * the host controller.  This queue is the processed by software.
1840  * Unfortunately the queue contains the physical address of the TD
1841  * and we have no simple way to translate this back to a kernel address.
1842  * To make the translation possible (and fast) we use a hash table of
1843  * TDs currently in the schedule.  The physical address is used as the
1844  * hash value.
1845  */
1846
1847 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
1848 /* Called at splusb() */
1849 void
1850 ohci_hash_add_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1851 {
1852         int h = HASH(std->physaddr);
1853
1854         SPLUSBCHECK;
1855
1856         LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
1857 }
1858
1859 /* Called at splusb() */
1860 void
1861 ohci_hash_rem_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1862 {
1863         SPLUSBCHECK;
1864
1865         LIST_REMOVE(std, hnext);
1866 }
1867
1868 ohci_soft_td_t *
1869 ohci_hash_find_td(ohci_softc_t *sc, ohci_physaddr_t a)
1870 {
1871         int h = HASH(a);
1872         ohci_soft_td_t *std;
1873
1874         /* if these are present they should be masked out at an earlier
1875          * stage.
1876          */
1877         KASSERT((a&~OHCI_HEADMASK) == 0, ("%s: 0x%b has lower bits set\n",
1878                                       device_get_nameunit(sc->sc_bus.bdev),
1879                                       (int) a, "\20\1HALT\2TOGGLE"));
1880
1881         for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
1882              std != NULL;
1883              std = LIST_NEXT(std, hnext))
1884                 if (std->physaddr == a)
1885                         return (std);
1886
1887         DPRINTF(("%s: ohci_hash_find_td: addr 0x%08lx not found\n",
1888                 device_get_nameunit(sc->sc_bus.bdev), (u_long) a));
1889         return (NULL);
1890 }
1891
1892 /* Called at splusb() */
1893 void
1894 ohci_hash_add_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1895 {
1896         int h = HASH(sitd->physaddr);
1897
1898         SPLUSBCHECK;
1899
1900         DPRINTFN(10,("ohci_hash_add_itd: sitd=%p physaddr=0x%08lx\n",
1901                     sitd, (u_long)sitd->physaddr));
1902
1903         LIST_INSERT_HEAD(&sc->sc_hash_itds[h], sitd, hnext);
1904 }
1905
1906 /* Called at splusb() */
1907 void
1908 ohci_hash_rem_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1909 {
1910         SPLUSBCHECK;
1911
1912         DPRINTFN(10,("ohci_hash_rem_itd: sitd=%p physaddr=0x%08lx\n",
1913                     sitd, (u_long)sitd->physaddr));
1914
1915         LIST_REMOVE(sitd, hnext);
1916 }
1917
1918 ohci_soft_itd_t *
1919 ohci_hash_find_itd(ohci_softc_t *sc, ohci_physaddr_t a)
1920 {
1921         int h = HASH(a);
1922         ohci_soft_itd_t *sitd;
1923
1924         for (sitd = LIST_FIRST(&sc->sc_hash_itds[h]);
1925              sitd != NULL;
1926              sitd = LIST_NEXT(sitd, hnext))
1927                 if (sitd->physaddr == a)
1928                         return (sitd);
1929         return (NULL);
1930 }
1931
1932 void
1933 ohci_timeout(void *addr)
1934 {
1935         struct ohci_xfer *oxfer = addr;
1936         struct ohci_pipe *opipe = (struct ohci_pipe *)oxfer->xfer.pipe;
1937         ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1938
1939         DPRINTF(("ohci_timeout: oxfer=%p\n", oxfer));
1940
1941         if (sc->sc_dying) {
1942                 ohci_abort_xfer(&oxfer->xfer, USBD_TIMEOUT);
1943                 return;
1944         }
1945
1946         /* Execute the abort in a process context. */
1947         usb_add_task(oxfer->xfer.pipe->device, &oxfer->abort_task,
1948             USB_TASKQ_HC);
1949 }
1950
1951 void
1952 ohci_timeout_task(void *addr)
1953 {
1954         usbd_xfer_handle xfer = addr;
1955         int s;
1956
1957         DPRINTF(("ohci_timeout_task: xfer=%p\n", xfer));
1958
1959         s = splusb();
1960         ohci_abort_xfer(xfer, USBD_TIMEOUT);
1961         splx(s);
1962 }
1963
1964 #ifdef USB_DEBUG
1965 void
1966 ohci_dump_tds(ohci_soft_td_t *std)
1967 {
1968         for (; std; std = std->nexttd)
1969                 ohci_dump_td(std);
1970 }
1971
1972 void
1973 ohci_dump_td(ohci_soft_td_t *std)
1974 {
1975         char sbuf[128];
1976
1977         bitmask_snprintf((u_int32_t)le32toh(std->td.td_flags),
1978                          "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE",
1979                          sbuf, sizeof(sbuf));
1980
1981         printf("TD(%p) at %08lx: %s delay=%d ec=%d cc=%d\ncbp=0x%08lx "
1982                "nexttd=0x%08lx be=0x%08lx\n",
1983                std, (u_long)std->physaddr, sbuf,
1984                OHCI_TD_GET_DI(le32toh(std->td.td_flags)),
1985                OHCI_TD_GET_EC(le32toh(std->td.td_flags)),
1986                OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
1987                (u_long)le32toh(std->td.td_cbp),
1988                (u_long)le32toh(std->td.td_nexttd),
1989                (u_long)le32toh(std->td.td_be));
1990 }
1991
1992 void
1993 ohci_dump_itd(ohci_soft_itd_t *sitd)
1994 {
1995         int i;
1996
1997         printf("ITD(%p) at %08lx: sf=%d di=%d fc=%d cc=%d\n"
1998                "bp0=0x%08lx next=0x%08lx be=0x%08lx\n",
1999                sitd, (u_long)sitd->physaddr,
2000                OHCI_ITD_GET_SF(le32toh(sitd->itd.itd_flags)),
2001                OHCI_ITD_GET_DI(le32toh(sitd->itd.itd_flags)),
2002                OHCI_ITD_GET_FC(le32toh(sitd->itd.itd_flags)),
2003                OHCI_ITD_GET_CC(le32toh(sitd->itd.itd_flags)),
2004                (u_long)le32toh(sitd->itd.itd_bp0),
2005                (u_long)le32toh(sitd->itd.itd_nextitd),
2006                (u_long)le32toh(sitd->itd.itd_be));
2007         for (i = 0; i < OHCI_ITD_NOFFSET; i++)
2008                 printf("offs[%d]=0x%04x ", i,
2009                        (u_int)le16toh(sitd->itd.itd_offset[i]));
2010         printf("\n");
2011 }
2012
2013 void
2014 ohci_dump_itds(ohci_soft_itd_t *sitd)
2015 {
2016         for (; sitd; sitd = sitd->nextitd)
2017                 ohci_dump_itd(sitd);
2018 }
2019
2020 void
2021 ohci_dump_ed(ohci_soft_ed_t *sed)
2022 {
2023         char sbuf[128], sbuf2[128];
2024
2025         bitmask_snprintf((u_int32_t)le32toh(sed->ed.ed_flags),
2026                          "\20\14OUT\15IN\16LOWSPEED\17SKIP\20ISO",
2027                          sbuf, sizeof(sbuf));
2028         bitmask_snprintf((u_int32_t)le32toh(sed->ed.ed_headp),
2029                          "\20\1HALT\2CARRY", sbuf2, sizeof(sbuf2));
2030
2031         printf("ED(%p) at 0x%08lx: addr=%d endpt=%d maxp=%d flags=%s\ntailp=0x%08lx "
2032                  "headflags=%s headp=0x%08lx nexted=0x%08lx\n",
2033                  sed, (u_long)sed->physaddr,
2034                  OHCI_ED_GET_FA(le32toh(sed->ed.ed_flags)),
2035                  OHCI_ED_GET_EN(le32toh(sed->ed.ed_flags)),
2036                  OHCI_ED_GET_MAXP(le32toh(sed->ed.ed_flags)), sbuf,
2037                  (u_long)le32toh(sed->ed.ed_tailp), sbuf2,
2038                  (u_long)le32toh(sed->ed.ed_headp),
2039                  (u_long)le32toh(sed->ed.ed_nexted));
2040 }
2041 #endif
2042
2043 usbd_status
2044 ohci_open(usbd_pipe_handle pipe)
2045 {
2046         usbd_device_handle dev = pipe->device;
2047         ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2048         usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2049         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2050         u_int8_t addr = dev->address;
2051         u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
2052         ohci_soft_ed_t *sed;
2053         ohci_soft_td_t *std;
2054         ohci_soft_itd_t *sitd;
2055         ohci_physaddr_t tdphys;
2056         u_int32_t fmt;
2057         usbd_status err;
2058         int s;
2059         int ival;
2060
2061         DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2062                      pipe, addr, ed->bEndpointAddress, sc->sc_addr));
2063
2064         if (sc->sc_dying)
2065                 return (USBD_IOERROR);
2066
2067         std = NULL;
2068         sed = NULL;
2069
2070         if (addr == sc->sc_addr) {
2071                 switch (ed->bEndpointAddress) {
2072                 case USB_CONTROL_ENDPOINT:
2073                         pipe->methods = &ohci_root_ctrl_methods;
2074                         break;
2075                 case UE_DIR_IN | OHCI_INTR_ENDPT:
2076                         pipe->methods = &ohci_root_intr_methods;
2077                         break;
2078                 default:
2079                         return (USBD_INVAL);
2080                 }
2081         } else {
2082                 sed = ohci_alloc_sed(sc);
2083                 if (sed == NULL)
2084                         goto bad0;
2085                 opipe->sed = sed;
2086                 if (xfertype == UE_ISOCHRONOUS) {
2087                         sitd = ohci_alloc_sitd(sc);
2088                         if (sitd == NULL)
2089                                 goto bad1;
2090                         opipe->tail.itd = sitd;
2091                         opipe->aborting = 0;
2092                         tdphys = sitd->physaddr;
2093                         fmt = OHCI_ED_FORMAT_ISO;
2094                         if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
2095                                 fmt |= OHCI_ED_DIR_IN;
2096                         else
2097                                 fmt |= OHCI_ED_DIR_OUT;
2098                 } else {
2099                         std = ohci_alloc_std(sc);
2100                         if (std == NULL)
2101                                 goto bad1;
2102                         opipe->tail.td = std;
2103                         tdphys = std->physaddr;
2104                         fmt = OHCI_ED_FORMAT_GEN | OHCI_ED_DIR_TD;
2105                 }
2106                 sed->ed.ed_flags = htole32(
2107                         OHCI_ED_SET_FA(addr) |
2108                         OHCI_ED_SET_EN(UE_GET_ADDR(ed->bEndpointAddress)) |
2109                         (dev->speed == USB_SPEED_LOW ? OHCI_ED_SPEED : 0) |
2110                         fmt |
2111                         OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
2112                 sed->ed.ed_headp = htole32(tdphys |
2113                     (pipe->endpoint->savedtoggle ? OHCI_TOGGLECARRY : 0));
2114                 sed->ed.ed_tailp = htole32(tdphys);
2115
2116                 switch (xfertype) {
2117                 case UE_CONTROL:
2118                         pipe->methods = &ohci_device_ctrl_methods;
2119                         err = usb_allocmem(&sc->sc_bus,
2120                                   sizeof(usb_device_request_t),
2121                                   0, &opipe->u.ctl.reqdma);
2122                         if (err)
2123                                 goto bad;
2124                         s = splusb();
2125                         ohci_add_ed(sed, sc->sc_ctrl_head);
2126                         splx(s);
2127                         break;
2128                 case UE_INTERRUPT:
2129                         pipe->methods = &ohci_device_intr_methods;
2130                         ival = pipe->interval;
2131                         if (ival == USBD_DEFAULT_INTERVAL)
2132                                 ival = ed->bInterval;
2133                         return (ohci_device_setintr(sc, opipe, ival));
2134                 case UE_ISOCHRONOUS:
2135                         pipe->methods = &ohci_device_isoc_methods;
2136                         return (ohci_setup_isoc(pipe));
2137                 case UE_BULK:
2138                         pipe->methods = &ohci_device_bulk_methods;
2139                         s = splusb();
2140                         ohci_add_ed(sed, sc->sc_bulk_head);
2141                         splx(s);
2142                         break;
2143                 }
2144         }
2145         return (USBD_NORMAL_COMPLETION);
2146
2147  bad:
2148         if (std != NULL)
2149                 ohci_free_std(sc, std);
2150  bad1:
2151         if (sed != NULL)
2152                 ohci_free_sed(sc, sed);
2153  bad0:
2154         return (USBD_NOMEM);
2155
2156 }
2157
2158 /*
2159  * Close a reqular pipe.
2160  * Assumes that there are no pending transactions.
2161  */
2162 void
2163 ohci_close_pipe(usbd_pipe_handle pipe, ohci_soft_ed_t *head)
2164 {
2165         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2166         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2167         ohci_soft_ed_t *sed = opipe->sed;
2168         int s;
2169
2170         s = splusb();
2171 #ifdef DIAGNOSTIC
2172         sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
2173         if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2174             (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK)) {
2175                 ohci_soft_td_t *std;
2176                 std = ohci_hash_find_td(sc, le32toh(sed->ed.ed_headp));
2177                 printf("ohci_close_pipe: pipe not empty sed=%p hd=0x%x "
2178                        "tl=0x%x pipe=%p, std=%p\n", sed,
2179                        (int)le32toh(sed->ed.ed_headp),
2180                        (int)le32toh(sed->ed.ed_tailp),
2181                        pipe, std);
2182 #ifdef USB_DEBUG
2183                 usbd_dump_pipe(&opipe->pipe);
2184 #endif
2185 #ifdef USB_DEBUG
2186                 ohci_dump_ed(sed);
2187                 if (std)
2188                         ohci_dump_td(std);
2189 #endif
2190                 usb_delay_ms(&sc->sc_bus, 2);
2191                 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2192                     (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
2193                         printf("ohci_close_pipe: pipe still not empty\n");
2194         }
2195 #endif
2196         ohci_rem_ed(sed, head);
2197         /* Make sure the host controller is not touching this ED */
2198         usb_delay_ms(&sc->sc_bus, 1);
2199         splx(s);
2200         pipe->endpoint->savedtoggle =
2201             (le32toh(sed->ed.ed_headp) & OHCI_TOGGLECARRY) ? 1 : 0;
2202         ohci_free_sed(sc, opipe->sed);
2203 }
2204
2205 /*
2206  * Abort a device request.
2207  * If this routine is called at splusb() it guarantees that the request
2208  * will be removed from the hardware scheduling and that the callback
2209  * for it will be called with USBD_CANCELLED status.
2210  * It's impossible to guarantee that the requested transfer will not
2211  * have happened since the hardware runs concurrently.
2212  * If the transaction has already happened we rely on the ordinary
2213  * interrupt processing to process it.
2214  */
2215 void
2216 ohci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2217 {
2218         struct ohci_xfer *oxfer = OXFER(xfer);
2219         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2220         ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
2221         ohci_soft_ed_t *sed = opipe->sed;
2222         ohci_soft_td_t *p, *n;
2223         ohci_physaddr_t headp;
2224         int s, hit;
2225
2226         DPRINTF(("ohci_abort_xfer: xfer=%p pipe=%p sed=%p\n", xfer, opipe,sed));
2227
2228         if (sc->sc_dying) {
2229                 /* If we're dying, just do the software part. */
2230                 s = splusb();
2231                 xfer->status = status;  /* make software ignore it */
2232                 callout_stop(&xfer->timeout_handle);
2233                 usb_rem_task(xfer->pipe->device, &OXFER(xfer)->abort_task);
2234                 usb_transfer_complete(xfer);
2235                 splx(s);
2236                 return;
2237         }
2238
2239         if (xfer->device->bus->intr_context || !curproc)
2240                 panic("ohci_abort_xfer: not in process context");
2241
2242         /*
2243          * If an abort is already in progress then just wait for it to
2244          * complete and return.
2245          */
2246         if (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTING) {
2247                 DPRINTFN(2, ("ohci_abort_xfer: already aborting\n"));
2248                 /* No need to wait if we're aborting from a timeout. */
2249                 if (status == USBD_TIMEOUT)
2250                         return;
2251                 /* Override the status which might be USBD_TIMEOUT. */
2252                 xfer->status = status;
2253                 DPRINTFN(2, ("ohci_abort_xfer: waiting for abort to finish\n"));
2254                 oxfer->ohci_xfer_flags |= OHCI_XFER_ABORTWAIT;
2255                 while (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTING)
2256                         tsleep(&oxfer->ohci_xfer_flags, PZERO, "ohciaw", 0);
2257                 return;
2258         }
2259
2260         /*
2261          * Step 1: Make interrupt routine and hardware ignore xfer.
2262          */
2263         s = splusb();
2264         oxfer->ohci_xfer_flags |= OHCI_XFER_ABORTING;
2265         xfer->status = status;  /* make software ignore it */
2266         callout_stop(&xfer->timeout_handle);
2267         usb_rem_task(xfer->pipe->device, &OXFER(xfer)->abort_task);
2268         splx(s);
2269         DPRINTFN(1,("ohci_abort_xfer: stop ed=%p\n", sed));
2270         sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
2271
2272         /*
2273          * Step 2: Wait until we know hardware has finished any possible
2274          * use of the xfer.  Also make sure the soft interrupt routine
2275          * has run.
2276          */
2277         usb_delay_ms(opipe->pipe.device->bus, 20); /* Hardware finishes in 1ms */
2278         s = splusb();
2279 #ifdef USB_USE_SOFTINTR
2280         sc->sc_softwake = 1;
2281 #endif /* USB_USE_SOFTINTR */
2282         usb_schedsoftintr(&sc->sc_bus);
2283 #ifdef USB_USE_SOFTINTR
2284         tsleep(&sc->sc_softwake, PZERO, "ohciab", 0);
2285 #endif /* USB_USE_SOFTINTR */
2286         splx(s);
2287
2288         /*
2289          * Step 3: Remove any vestiges of the xfer from the hardware.
2290          * The complication here is that the hardware may have executed
2291          * beyond the xfer we're trying to abort.  So as we're scanning
2292          * the TDs of this xfer we check if the hardware points to
2293          * any of them.
2294          */
2295         s = splusb();           /* XXX why? */
2296         p = xfer->hcpriv;
2297 #ifdef DIAGNOSTIC
2298         if (p == NULL) {
2299                 oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTING; /* XXX */
2300                 splx(s);
2301                 printf("ohci_abort_xfer: hcpriv is NULL\n");
2302                 return;
2303         }
2304 #endif
2305 #ifdef USB_DEBUG
2306         if (ohcidebug > 1) {
2307                 DPRINTF(("ohci_abort_xfer: sed=\n"));
2308                 ohci_dump_ed(sed);
2309                 ohci_dump_tds(p);
2310         }
2311 #endif
2312         headp = le32toh(sed->ed.ed_headp) & OHCI_HEADMASK;
2313         hit = 0;
2314         for (; p->xfer == xfer; p = n) {
2315                 hit |= headp == p->physaddr;
2316                 n = p->nexttd;
2317                 ohci_free_std(sc, p);
2318         }
2319         /* Zap headp register if hardware pointed inside the xfer. */
2320         if (hit) {
2321                 DPRINTFN(1,("ohci_abort_xfer: set hd=0x08%x, tl=0x%08x\n",
2322                             (int)p->physaddr, (int)le32toh(sed->ed.ed_tailp)));
2323                 sed->ed.ed_headp = htole32(p->physaddr); /* unlink TDs */
2324         } else {
2325                 DPRINTFN(1,("ohci_abort_xfer: no hit\n"));
2326         }
2327
2328         /*
2329          * Step 4: Turn on hardware again.
2330          */
2331         sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
2332
2333         /*
2334          * Step 5: Execute callback.
2335          */
2336         /* Do the wakeup first to avoid touching the xfer after the callback. */
2337         oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTING;
2338         if (oxfer->ohci_xfer_flags & OHCI_XFER_ABORTWAIT) {
2339                 oxfer->ohci_xfer_flags &= ~OHCI_XFER_ABORTWAIT;
2340                 wakeup(&oxfer->ohci_xfer_flags);
2341         }
2342         usb_transfer_complete(xfer);
2343
2344         splx(s);
2345 }
2346
2347 /*
2348  * Data structures and routines to emulate the root hub.
2349  */
2350 static usb_device_descriptor_t ohci_devd = {
2351         USB_DEVICE_DESCRIPTOR_SIZE,
2352         UDESC_DEVICE,           /* type */
2353         {0x00, 0x01},           /* USB version */
2354         UDCLASS_HUB,            /* class */
2355         UDSUBCLASS_HUB,         /* subclass */
2356         UDPROTO_FSHUB,          /* protocol */
2357         64,                     /* max packet */
2358         {0},{0},{0x00,0x01},    /* device id */
2359         1,2,0,                  /* string indicies */
2360         1                       /* # of configurations */
2361 };
2362
2363 static usb_config_descriptor_t ohci_confd = {
2364         USB_CONFIG_DESCRIPTOR_SIZE,
2365         UDESC_CONFIG,
2366         {USB_CONFIG_DESCRIPTOR_SIZE +
2367          USB_INTERFACE_DESCRIPTOR_SIZE +
2368          USB_ENDPOINT_DESCRIPTOR_SIZE},
2369         1,
2370         1,
2371         0,
2372         UC_SELF_POWERED,
2373         0                       /* max power */
2374 };
2375
2376 static usb_interface_descriptor_t ohci_ifcd = {
2377         USB_INTERFACE_DESCRIPTOR_SIZE,
2378         UDESC_INTERFACE,
2379         0,
2380         0,
2381         1,
2382         UICLASS_HUB,
2383         UISUBCLASS_HUB,
2384         UIPROTO_FSHUB,
2385         0
2386 };
2387
2388 static usb_endpoint_descriptor_t ohci_endpd = {
2389         USB_ENDPOINT_DESCRIPTOR_SIZE,
2390         UDESC_ENDPOINT,
2391         UE_DIR_IN | OHCI_INTR_ENDPT,
2392         UE_INTERRUPT,
2393         {8, 0},                 /* max packet */
2394         255
2395 };
2396
2397 static usb_hub_descriptor_t ohci_hubd = {
2398         USB_HUB_DESCRIPTOR_SIZE,
2399         UDESC_HUB,
2400         0,
2401         {0,0},
2402         0,
2403         0,
2404         {0},
2405 };
2406
2407 static int
2408 ohci_str(usb_string_descriptor_t *p, int l, const char *s)
2409 {
2410         int i;
2411
2412         if (l == 0)
2413                 return (0);
2414         p->bLength = 2 * strlen(s) + 2;
2415         if (l == 1)
2416                 return (1);
2417         p->bDescriptorType = UDESC_STRING;
2418         l -= 2;
2419         for (i = 0; s[i] && l > 1; i++, l -= 2)
2420                 USETW2(p->bString[i], 0, s[i]);
2421         return (2*i+2);
2422 }
2423
2424 /*
2425  * Simulate a hardware hub by handling all the necessary requests.
2426  */
2427 static usbd_status
2428 ohci_root_ctrl_transfer(usbd_xfer_handle xfer)
2429 {
2430         usbd_status err;
2431
2432         /* Insert last in queue. */
2433         err = usb_insert_transfer(xfer);
2434         if (err)
2435                 return (err);
2436
2437         /* Pipe isn't running, start first */
2438         return (ohci_root_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
2439 }
2440
2441 static usbd_status
2442 ohci_root_ctrl_start(usbd_xfer_handle xfer)
2443 {
2444         ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2445         usb_device_request_t *req;
2446         void *buf = NULL;
2447         int port, i;
2448         int s, len, value, index, l, totlen = 0;
2449         usb_port_status_t ps;
2450         usb_hub_descriptor_t hubd;
2451         usbd_status err;
2452         u_int32_t v;
2453
2454         if (sc->sc_dying)
2455                 return (USBD_IOERROR);
2456
2457 #ifdef DIAGNOSTIC
2458         if (!(xfer->rqflags & URQ_REQUEST))
2459                 /* XXX panic */
2460                 return (USBD_INVAL);
2461 #endif
2462         req = &xfer->request;
2463
2464         DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n",
2465                     req->bmRequestType, req->bRequest));
2466
2467         len = UGETW(req->wLength);
2468         value = UGETW(req->wValue);
2469         index = UGETW(req->wIndex);
2470
2471         if (len != 0)
2472                 buf = xfer->buffer;
2473
2474 #define C(x,y) ((x) | ((y) << 8))
2475         switch(C(req->bRequest, req->bmRequestType)) {
2476         case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2477         case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2478         case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2479                 /*
2480                  * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2481                  * for the integrated root hub.
2482                  */
2483                 break;
2484         case C(UR_GET_CONFIG, UT_READ_DEVICE):
2485                 if (len > 0) {
2486                         *(u_int8_t *)buf = sc->sc_conf;
2487                         totlen = 1;
2488                 }
2489                 break;
2490         case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2491                 DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value));
2492                 switch(value >> 8) {
2493                 case UDESC_DEVICE:
2494                         if ((value & 0xff) != 0) {
2495                                 err = USBD_IOERROR;
2496                                 goto ret;
2497                         }
2498                         totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2499                         USETW(ohci_devd.idVendor, sc->sc_id_vendor);
2500                         memcpy(buf, &ohci_devd, l);
2501                         break;
2502                 case UDESC_CONFIG:
2503                         if ((value & 0xff) != 0) {
2504                                 err = USBD_IOERROR;
2505                                 goto ret;
2506                         }
2507                         totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2508                         memcpy(buf, &ohci_confd, l);
2509                         buf = (char *)buf + l;
2510                         len -= l;
2511                         l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2512                         totlen += l;
2513                         memcpy(buf, &ohci_ifcd, l);
2514                         buf = (char *)buf + l;
2515                         len -= l;
2516                         l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2517                         totlen += l;
2518                         memcpy(buf, &ohci_endpd, l);
2519                         break;
2520                 case UDESC_STRING:
2521                         if (len == 0)
2522                                 break;
2523                         *(u_int8_t *)buf = 0;
2524                         totlen = 1;
2525                         switch (value & 0xff) {
2526                         case 1: /* Vendor */
2527                                 totlen = ohci_str(buf, len, sc->sc_vendor);
2528                                 break;
2529                         case 2: /* Product */
2530                                 totlen = ohci_str(buf, len, "OHCI root hub");
2531                                 break;
2532                         }
2533                         break;
2534                 default:
2535                         err = USBD_IOERROR;
2536                         goto ret;
2537                 }
2538                 break;
2539         case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2540                 if (len > 0) {
2541                         *(u_int8_t *)buf = 0;
2542                         totlen = 1;
2543                 }
2544                 break;
2545         case C(UR_GET_STATUS, UT_READ_DEVICE):
2546                 if (len > 1) {
2547                         USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2548                         totlen = 2;
2549                 }
2550                 break;
2551         case C(UR_GET_STATUS, UT_READ_INTERFACE):
2552         case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2553                 if (len > 1) {
2554                         USETW(((usb_status_t *)buf)->wStatus, 0);
2555                         totlen = 2;
2556                 }
2557                 break;
2558         case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2559                 if (value >= USB_MAX_DEVICES) {
2560                         err = USBD_IOERROR;
2561                         goto ret;
2562                 }
2563                 sc->sc_addr = value;
2564                 break;
2565         case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2566                 if (value != 0 && value != 1) {
2567                         err = USBD_IOERROR;
2568                         goto ret;
2569                 }
2570                 sc->sc_conf = value;
2571                 break;
2572         case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2573                 break;
2574         case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2575         case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2576         case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2577                 err = USBD_IOERROR;
2578                 goto ret;
2579         case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2580                 break;
2581         case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2582                 break;
2583         /* Hub requests */
2584         case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2585                 break;
2586         case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2587                 DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2588                              "port=%d feature=%d\n",
2589                              index, value));
2590                 if (index < 1 || index > sc->sc_noport) {
2591                         err = USBD_IOERROR;
2592                         goto ret;
2593                 }
2594                 port = OHCI_RH_PORT_STATUS(index);
2595                 switch(value) {
2596                 case UHF_PORT_ENABLE:
2597                         OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
2598                         break;
2599                 case UHF_PORT_SUSPEND:
2600                         OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
2601                         break;
2602                 case UHF_PORT_POWER:
2603                         /* Yes, writing to the LOW_SPEED bit clears power. */
2604                         OWRITE4(sc, port, UPS_LOW_SPEED);
2605                         break;
2606                 case UHF_C_PORT_CONNECTION:
2607                         OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
2608                         break;
2609                 case UHF_C_PORT_ENABLE:
2610                         OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
2611                         break;
2612                 case UHF_C_PORT_SUSPEND:
2613                         OWRITE4(sc, port, UPS_C_SUSPEND << 16);
2614                         break;
2615                 case UHF_C_PORT_OVER_CURRENT:
2616                         OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
2617                         break;
2618                 case UHF_C_PORT_RESET:
2619                         OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
2620                         break;
2621                 default:
2622                         err = USBD_IOERROR;
2623                         goto ret;
2624                 }
2625                 switch(value) {
2626                 case UHF_C_PORT_CONNECTION:
2627                 case UHF_C_PORT_ENABLE:
2628                 case UHF_C_PORT_SUSPEND:
2629                 case UHF_C_PORT_OVER_CURRENT:
2630                 case UHF_C_PORT_RESET:
2631                         /* Enable RHSC interrupt if condition is cleared. */
2632                         if ((OREAD4(sc, port) >> 16) == 0)
2633                                 ohci_rhsc_able(sc, 1);
2634                         break;
2635                 default:
2636                         break;
2637                 }
2638                 break;
2639         case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2640                 if ((value & 0xff) != 0) {
2641                         err = USBD_IOERROR;
2642                         goto ret;
2643                 }
2644                 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
2645                 hubd = ohci_hubd;
2646                 hubd.bNbrPorts = sc->sc_noport;
2647                 USETW(hubd.wHubCharacteristics,
2648                       (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
2649                        v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
2650                       /* XXX overcurrent */
2651                       );
2652                 hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
2653                 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
2654                 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2655                         hubd.DeviceRemovable[i++] = (u_int8_t)v;
2656                 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2657                 l = min(len, hubd.bDescLength);
2658                 totlen = l;
2659                 memcpy(buf, &hubd, l);
2660                 break;
2661         case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2662                 if (len != 4) {
2663                         err = USBD_IOERROR;
2664                         goto ret;
2665                 }
2666                 memset(buf, 0, len); /* ? XXX */
2667                 totlen = len;
2668                 break;
2669         case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2670                 DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n",
2671                             index));
2672                 if (index < 1 || index > sc->sc_noport) {
2673                         err = USBD_IOERROR;
2674                         goto ret;
2675                 }
2676                 if (len != 4) {
2677                         err = USBD_IOERROR;
2678                         goto ret;
2679                 }
2680                 v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
2681                 DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n",
2682                             v));
2683                 USETW(ps.wPortStatus, v);
2684                 USETW(ps.wPortChange, v >> 16);
2685                 l = min(len, sizeof ps);
2686                 memcpy(buf, &ps, l);
2687                 totlen = l;
2688                 break;
2689         case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2690                 err = USBD_IOERROR;
2691                 goto ret;
2692         case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2693                 break;
2694         case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2695                 if (index < 1 || index > sc->sc_noport) {
2696                         err = USBD_IOERROR;
2697                         goto ret;
2698                 }
2699                 port = OHCI_RH_PORT_STATUS(index);
2700                 switch(value) {
2701                 case UHF_PORT_ENABLE:
2702                         OWRITE4(sc, port, UPS_PORT_ENABLED);
2703                         break;
2704                 case UHF_PORT_SUSPEND:
2705                         OWRITE4(sc, port, UPS_SUSPEND);
2706                         break;
2707                 case UHF_PORT_RESET:
2708                         DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n",
2709                                     index));
2710                         OWRITE4(sc, port, UPS_RESET);
2711                         for (i = 0; i < 5; i++) {
2712                                 usb_delay_ms(&sc->sc_bus,
2713                                              USB_PORT_ROOT_RESET_DELAY);
2714                                 if (sc->sc_dying) {
2715                                         err = USBD_IOERROR;
2716                                         goto ret;
2717                                 }
2718                                 if ((OREAD4(sc, port) & UPS_RESET) == 0)
2719                                         break;
2720                         }
2721                         DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n",
2722                                     index, OREAD4(sc, port)));
2723                         break;
2724                 case UHF_PORT_POWER:
2725                         DPRINTFN(2,("ohci_root_ctrl_transfer: set port power "
2726                                     "%d\n", index));
2727                         OWRITE4(sc, port, UPS_PORT_POWER);
2728                         break;
2729                 default:
2730                         err = USBD_IOERROR;
2731                         goto ret;
2732                 }
2733                 break;
2734         default:
2735                 err = USBD_IOERROR;
2736                 goto ret;
2737         }
2738         xfer->actlen = totlen;
2739         err = USBD_NORMAL_COMPLETION;
2740  ret:
2741         xfer->status = err;
2742         s = splusb();
2743         hacksync(xfer); /* XXX to compensate for usb_transfer_complete */
2744         usb_transfer_complete(xfer);
2745         splx(s);
2746         return (USBD_IN_PROGRESS);
2747 }
2748
2749 /* Abort a root control request. */
2750 static void
2751 ohci_root_ctrl_abort(usbd_xfer_handle xfer)
2752 {
2753         /* Nothing to do, all transfers are synchronous. */
2754 }
2755
2756 /* Close the root pipe. */
2757 static void
2758 ohci_root_ctrl_close(usbd_pipe_handle pipe)
2759 {
2760         DPRINTF(("ohci_root_ctrl_close\n"));
2761         /* Nothing to do. */
2762 }
2763
2764 static usbd_status
2765 ohci_root_intr_transfer(usbd_xfer_handle xfer)
2766 {
2767         usbd_status err;
2768
2769         /* Insert last in queue. */
2770         err = usb_insert_transfer(xfer);
2771         if (err)
2772                 return (err);
2773
2774         /* Pipe isn't running, start first */
2775         return (ohci_root_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
2776 }
2777
2778 static usbd_status
2779 ohci_root_intr_start(usbd_xfer_handle xfer)
2780 {
2781         usbd_pipe_handle pipe = xfer->pipe;
2782         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2783
2784         if (sc->sc_dying)
2785                 return (USBD_IOERROR);
2786
2787         sc->sc_intrxfer = xfer;
2788
2789         return (USBD_IN_PROGRESS);
2790 }
2791
2792 /* Abort a root interrupt request. */
2793 static void
2794 ohci_root_intr_abort(usbd_xfer_handle xfer)
2795 {
2796         int s;
2797
2798         if (xfer->pipe->intrxfer == xfer) {
2799                 DPRINTF(("ohci_root_intr_abort: remove\n"));
2800                 xfer->pipe->intrxfer = NULL;
2801         }
2802         xfer->status = USBD_CANCELLED;
2803         s = splusb();
2804         usb_transfer_complete(xfer);
2805         splx(s);
2806 }
2807
2808 /* Close the root pipe. */
2809 static void
2810 ohci_root_intr_close(usbd_pipe_handle pipe)
2811 {
2812         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2813
2814         DPRINTF(("ohci_root_intr_close\n"));
2815
2816         sc->sc_intrxfer = NULL;
2817 }
2818
2819 /************************/
2820
2821 static usbd_status
2822 ohci_device_ctrl_transfer(usbd_xfer_handle xfer)
2823 {
2824         usbd_status err;
2825
2826         /* Insert last in queue. */
2827         err = usb_insert_transfer(xfer);
2828         if (err)
2829                 return (err);
2830
2831         /* Pipe isn't running, start first */
2832         return (ohci_device_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
2833 }
2834
2835 static usbd_status
2836 ohci_device_ctrl_start(usbd_xfer_handle xfer)
2837 {
2838         ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2839         usbd_status err;
2840
2841         if (sc->sc_dying)
2842                 return (USBD_IOERROR);
2843
2844 #ifdef DIAGNOSTIC
2845         if (!(xfer->rqflags & URQ_REQUEST)) {
2846                 /* XXX panic */
2847                 printf("ohci_device_ctrl_transfer: not a request\n");
2848                 return (USBD_INVAL);
2849         }
2850 #endif
2851
2852         err = ohci_device_request(xfer);
2853         if (err)
2854                 return (err);
2855
2856         if (sc->sc_bus.use_polling)
2857                 ohci_waitintr(sc, xfer);
2858         return (USBD_IN_PROGRESS);
2859 }
2860
2861 /* Abort a device control request. */
2862 static void
2863 ohci_device_ctrl_abort(usbd_xfer_handle xfer)
2864 {
2865         DPRINTF(("ohci_device_ctrl_abort: xfer=%p\n", xfer));
2866         ohci_abort_xfer(xfer, USBD_CANCELLED);
2867 }
2868
2869 /* Close a device control pipe. */
2870 static void
2871 ohci_device_ctrl_close(usbd_pipe_handle pipe)
2872 {
2873         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2874         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2875
2876         DPRINTF(("ohci_device_ctrl_close: pipe=%p\n", pipe));
2877         ohci_close_pipe(pipe, sc->sc_ctrl_head);
2878         ohci_free_std(sc, opipe->tail.td);
2879 }
2880
2881 /************************/
2882
2883 static void
2884 ohci_device_clear_toggle(usbd_pipe_handle pipe)
2885 {
2886         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2887
2888         opipe->sed->ed.ed_headp &= htole32(~OHCI_TOGGLECARRY);
2889 }
2890
2891 static void
2892 ohci_noop(usbd_pipe_handle pipe)
2893 {
2894 }
2895
2896 static usbd_status
2897 ohci_device_bulk_transfer(usbd_xfer_handle xfer)
2898 {
2899         usbd_status err;
2900
2901         /* Insert last in queue. */
2902         err = usb_insert_transfer(xfer);
2903         if (err)
2904                 return (err);
2905
2906         /* Pipe isn't running, start first */
2907         return (ohci_device_bulk_start(STAILQ_FIRST(&xfer->pipe->queue)));
2908 }
2909
2910 static usbd_status
2911 ohci_device_bulk_start(usbd_xfer_handle xfer)
2912 {
2913         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2914         usbd_device_handle dev = opipe->pipe.device;
2915         ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2916         int addr = dev->address;
2917         ohci_soft_td_t *data, *tail, *tdp;
2918         ohci_soft_ed_t *sed;
2919         int s, len, isread, endpt;
2920         usbd_status err;
2921
2922         if (sc->sc_dying)
2923                 return (USBD_IOERROR);
2924
2925 #ifdef DIAGNOSTIC
2926         if (xfer->rqflags & URQ_REQUEST) {
2927                 /* XXX panic */
2928                 printf("ohci_device_bulk_start: a request\n");
2929                 return (USBD_INVAL);
2930         }
2931 #endif
2932
2933         len = xfer->length;
2934         endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2935         isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2936         sed = opipe->sed;
2937
2938         DPRINTFN(4,("ohci_device_bulk_start: xfer=%p len=%d isread=%d "
2939                     "flags=%d endpt=%d\n", xfer, len, isread, xfer->flags,
2940                     endpt));
2941
2942         opipe->u.bulk.isread = isread;
2943         opipe->u.bulk.length = len;
2944
2945         /* Update device address */
2946         sed->ed.ed_flags = htole32(
2947                 (le32toh(sed->ed.ed_flags) & ~OHCI_ED_ADDRMASK) |
2948                 OHCI_ED_SET_FA(addr));
2949
2950         /* Allocate a chain of new TDs (including a new tail). */
2951         data = opipe->tail.td;
2952         err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
2953                   data, &tail);
2954         /* We want interrupt at the end of the transfer. */
2955         tail->td.td_flags &= htole32(~OHCI_TD_INTR_MASK);
2956         tail->td.td_flags |= htole32(OHCI_TD_SET_DI(1));
2957         tail->flags |= OHCI_CALL_DONE;
2958         tail = tail->nexttd;    /* point at sentinel */
2959         if (err)
2960                 return (err);
2961
2962         tail->xfer = NULL;
2963         xfer->hcpriv = data;
2964
2965         DPRINTFN(4,("ohci_device_bulk_start: ed_flags=0x%08x td_flags=0x%08x "
2966                     "td_cbp=0x%08x td_be=0x%08x\n",
2967                     (int)le32toh(sed->ed.ed_flags),
2968                     (int)le32toh(data->td.td_flags),
2969                     (int)le32toh(data->td.td_cbp),
2970                     (int)le32toh(data->td.td_be)));
2971
2972 #ifdef USB_DEBUG
2973         if (ohcidebug > 5) {
2974                 ohci_dump_ed(sed);
2975                 ohci_dump_tds(data);
2976         }
2977 #endif
2978
2979         /* Insert ED in schedule */
2980         s = splusb();
2981         for (tdp = data; tdp != tail; tdp = tdp->nexttd) {
2982                 tdp->xfer = xfer;
2983         }
2984         sed->ed.ed_tailp = htole32(tail->physaddr);
2985         opipe->tail.td = tail;
2986         sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
2987         OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
2988         if (xfer->timeout && !sc->sc_bus.use_polling) {
2989                 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2990                     ohci_timeout, xfer);
2991         }
2992
2993 #if 0
2994 /* This goes wrong if we are too slow. */
2995         if (ohcidebug > 10) {
2996                 delay(10000);
2997                 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2998                          OREAD4(sc, OHCI_COMMAND_STATUS)));
2999                 ohci_dump_ed(sed);
3000                 ohci_dump_tds(data);
3001         }
3002 #endif
3003
3004         splx(s);
3005
3006         if (sc->sc_bus.use_polling)
3007                 ohci_waitintr(sc, xfer);
3008
3009         return (USBD_IN_PROGRESS);
3010 }
3011
3012 static void
3013 ohci_device_bulk_abort(usbd_xfer_handle xfer)
3014 {
3015         DPRINTF(("ohci_device_bulk_abort: xfer=%p\n", xfer));
3016         ohci_abort_xfer(xfer, USBD_CANCELLED);
3017 }
3018
3019 /*
3020  * Close a device bulk pipe.
3021  */
3022 static void
3023 ohci_device_bulk_close(usbd_pipe_handle pipe)
3024 {
3025         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3026         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3027
3028         DPRINTF(("ohci_device_bulk_close: pipe=%p\n", pipe));
3029         ohci_close_pipe(pipe, sc->sc_bulk_head);
3030         ohci_free_std(sc, opipe->tail.td);
3031 }
3032
3033 /************************/
3034
3035 static usbd_status
3036 ohci_device_intr_transfer(usbd_xfer_handle xfer)
3037 {
3038         usbd_status err;
3039
3040         /* Insert last in queue. */
3041         err = usb_insert_transfer(xfer);
3042         if (err)
3043                 return (err);
3044
3045         /* Pipe isn't running, start first */
3046         return (ohci_device_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
3047 }
3048
3049 static usbd_status
3050 ohci_device_intr_start(usbd_xfer_handle xfer)
3051 {
3052         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3053         ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3054         ohci_soft_ed_t *sed = opipe->sed;
3055         usbd_status err;
3056
3057         if (sc->sc_dying)
3058                 return (USBD_IOERROR);
3059
3060         DPRINTFN(3, ("ohci_device_intr_start: xfer=%p len=%d "
3061                      "flags=%d priv=%p\n",
3062                      xfer, xfer->length, xfer->flags, xfer->priv));
3063
3064 #ifdef DIAGNOSTIC
3065         if (xfer->rqflags & URQ_REQUEST)
3066                 panic("ohci_device_intr_start: a request");
3067 #endif
3068
3069         err = ohci_device_intr_insert(sc, xfer);
3070         if (err)
3071                 return (err);
3072
3073         sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
3074
3075         return (USBD_IN_PROGRESS);
3076 }
3077
3078 /*
3079  * Insert an interrupt transfer into an endpoint descriptor list
3080  */
3081 static usbd_status
3082 ohci_device_intr_insert(ohci_softc_t *sc, usbd_xfer_handle xfer)
3083 {
3084         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3085         ohci_soft_ed_t *sed = opipe->sed;
3086         ohci_soft_td_t *data, *tail;
3087         ohci_physaddr_t dataphys, physend;
3088         int s;
3089
3090         DPRINTFN(4, ("ohci_device_intr_insert: xfer=%p", xfer));
3091
3092         data = opipe->tail.td;
3093         tail = ohci_alloc_std(sc);
3094         if (tail == NULL)
3095                 return (USBD_NOMEM);
3096         tail->xfer = NULL;
3097
3098         data->td.td_flags = htole32(
3099                 OHCI_TD_IN | OHCI_TD_NOCC |
3100                 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
3101         if (xfer->flags & USBD_SHORT_XFER_OK)
3102                 data->td.td_flags |= htole32(OHCI_TD_R);
3103         /*
3104          * Assume a short mapping with no complications, which
3105          * should always be true for <= 4k buffers in contiguous
3106          * virtual memory. The data can take the following forms:
3107          *      1 segment in 1 OHCI page
3108          *      1 segment in 2 OHCI pages
3109          *      2 segments in 2 OHCI pages
3110          * (see comment in ohci_alloc_std_chain() for details)
3111          */
3112         KASSERT(xfer->length > 0 && xfer->length <= OHCI_PAGE_SIZE,
3113             ("ohci_device_intr_insert: bad length %d", xfer->length));
3114         dataphys = xfer->dmamap.segs[0].ds_addr;
3115         physend = dataphys + xfer->length - 1;
3116         if (xfer->dmamap.nsegs == 2) {
3117                 KASSERT(OHCI_PAGE_OFFSET(dataphys +
3118                     xfer->dmamap.segs[0].ds_len) == 0,
3119                     ("ohci_device_intr_insert: bad seg 0 termination"));
3120                 physend = xfer->dmamap.segs[1].ds_addr + xfer->length -
3121                     xfer->dmamap.segs[0].ds_len - 1;
3122         } else {
3123                 KASSERT(xfer->dmamap.nsegs == 1,
3124                     ("ohci_device_intr_insert: bad seg count %d",
3125                     (u_int)xfer->dmamap.nsegs));
3126         }
3127         data->td.td_cbp = htole32(dataphys);
3128         data->nexttd = tail;
3129         data->td.td_nexttd = htole32(tail->physaddr);
3130         data->td.td_be = htole32(physend);
3131         data->len = xfer->length;
3132         data->xfer = xfer;
3133         data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
3134         xfer->hcpriv = data;
3135         xfer->actlen = 0;
3136
3137 #ifdef USB_DEBUG
3138         if (ohcidebug > 5) {
3139                 DPRINTF(("ohci_device_intr_insert:\n"));
3140                 ohci_dump_ed(sed);
3141                 ohci_dump_tds(data);
3142         }
3143 #endif
3144
3145         /* Insert ED in schedule */
3146         s = splusb();
3147         sed->ed.ed_tailp = htole32(tail->physaddr);
3148         opipe->tail.td = tail;
3149         splx(s);
3150
3151         return (USBD_NORMAL_COMPLETION);
3152 }
3153
3154 /* Abort a device control request. */
3155 static void
3156 ohci_device_intr_abort(usbd_xfer_handle xfer)
3157 {
3158         if (xfer->pipe->intrxfer == xfer) {
3159                 DPRINTF(("ohci_device_intr_abort: remove\n"));
3160                 xfer->pipe->intrxfer = NULL;
3161         }
3162         ohci_abort_xfer(xfer, USBD_CANCELLED);
3163 }
3164
3165 /* Close a device interrupt pipe. */
3166 static void
3167 ohci_device_intr_close(usbd_pipe_handle pipe)
3168 {
3169         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3170         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3171         int nslots = opipe->u.intr.nslots;
3172         int pos = opipe->u.intr.pos;
3173         int j;
3174         ohci_soft_ed_t *p, *sed = opipe->sed;
3175         int s;
3176
3177         DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n",
3178                     pipe, nslots, pos));
3179         s = splusb();
3180         sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
3181         if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
3182             (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
3183                 usb_delay_ms(&sc->sc_bus, 2);
3184 #ifdef DIAGNOSTIC
3185         if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
3186             (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
3187                 panic("%s: Intr pipe %p still has TDs queued",
3188                         device_get_nameunit(sc->sc_bus.bdev), pipe);
3189 #endif
3190
3191         for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
3192                 ;
3193 #ifdef DIAGNOSTIC
3194         if (p == NULL)
3195                 panic("ohci_device_intr_close: ED not found");
3196 #endif
3197         p->next = sed->next;
3198         p->ed.ed_nexted = sed->ed.ed_nexted;
3199         splx(s);
3200
3201         for (j = 0; j < nslots; j++)
3202                 --sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS];
3203
3204         ohci_free_std(sc, opipe->tail.td);
3205         ohci_free_sed(sc, opipe->sed);
3206 }
3207
3208 static usbd_status
3209 ohci_device_setintr(ohci_softc_t *sc, struct ohci_pipe *opipe, int ival)
3210 {
3211         int i, j, s, best;
3212         u_int npoll, slow, shigh, nslots;
3213         u_int bestbw, bw;
3214         ohci_soft_ed_t *hsed, *sed = opipe->sed;
3215
3216         DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe));
3217         if (ival == 0) {
3218                 printf("ohci_setintr: 0 interval\n");
3219                 return (USBD_INVAL);
3220         }
3221
3222         npoll = OHCI_NO_INTRS;
3223         while (npoll > ival)
3224                 npoll /= 2;
3225         DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll));
3226
3227         /*
3228          * We now know which level in the tree the ED must go into.
3229          * Figure out which slot has most bandwidth left over.
3230          * Slots to examine:
3231          * npoll
3232          * 1    0
3233          * 2    1 2
3234          * 4    3 4 5 6
3235          * 8    7 8 9 10 11 12 13 14
3236          * N    (N-1) .. (N-1+N-1)
3237          */
3238         slow = npoll-1;
3239         shigh = slow + npoll;
3240         nslots = OHCI_NO_INTRS / npoll;
3241         for (best = i = slow, bestbw = ~0; i < shigh; i++) {
3242                 bw = 0;
3243                 for (j = 0; j < nslots; j++)
3244                         bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
3245                 if (bw < bestbw) {
3246                         best = i;
3247                         bestbw = bw;
3248                 }
3249         }
3250         DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n",
3251                      best, slow, shigh, bestbw));
3252
3253         s = splusb();
3254         hsed = sc->sc_eds[best];
3255         sed->next = hsed->next;
3256         sed->ed.ed_nexted = hsed->ed.ed_nexted;
3257         hsed->next = sed;
3258         hsed->ed.ed_nexted = htole32(sed->physaddr);
3259         splx(s);
3260
3261         for (j = 0; j < nslots; j++)
3262                 ++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
3263         opipe->u.intr.nslots = nslots;
3264         opipe->u.intr.pos = best;
3265
3266         DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe));
3267         return (USBD_NORMAL_COMPLETION);
3268 }
3269
3270 /***********************/
3271
3272 usbd_status
3273 ohci_device_isoc_transfer(usbd_xfer_handle xfer)
3274 {
3275         usbd_status err;
3276
3277         DPRINTFN(5,("ohci_device_isoc_transfer: xfer=%p\n", xfer));
3278
3279         /* Put it on our queue, */
3280         err = usb_insert_transfer(xfer);
3281
3282         /* bail out on error, */
3283         if (err && err != USBD_IN_PROGRESS)
3284                 return (err);
3285
3286         /* XXX should check inuse here */
3287
3288         /* insert into schedule, */
3289         ohci_device_isoc_enter(xfer);
3290
3291         /* and start if the pipe wasn't running */
3292         if (!err)
3293                 ohci_device_isoc_start(STAILQ_FIRST(&xfer->pipe->queue));
3294
3295         return (err);
3296 }
3297
3298 void
3299 ohci_device_isoc_enter(usbd_xfer_handle xfer)
3300 {
3301         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3302         usbd_device_handle dev = opipe->pipe.device;
3303         ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
3304         ohci_soft_ed_t *sed = opipe->sed;
3305         struct iso *iso = &opipe->u.iso;
3306         struct usb_dma_mapping *dma = &xfer->dmamap;
3307         ohci_soft_itd_t *sitd, *nsitd;
3308         ohci_physaddr_t dataphys, bp0, physend, prevpage;
3309         int curlen, i, len, ncur, nframes, npages, seg, segoff;
3310         int s;
3311
3312         DPRINTFN(1,("ohci_device_isoc_enter: used=%d next=%d xfer=%p "
3313                     "nframes=%d\n",
3314                     iso->inuse, iso->next, xfer, xfer->nframes));
3315
3316         if (sc->sc_dying)
3317                 return;
3318
3319         if (iso->next == -1) {
3320                 /* Not in use yet, schedule it a few frames ahead. */
3321                 iso->next = le32toh(sc->sc_hcca->hcca_frame_number) + 5;
3322                 DPRINTFN(2,("ohci_device_isoc_enter: start next=%d\n",
3323                             iso->next));
3324         }
3325
3326         sitd = opipe->tail.itd;
3327         nframes = xfer->nframes;
3328         xfer->hcpriv = sitd;
3329         seg = 0;
3330         segoff = 0;
3331         i = 0;
3332         while (i < nframes) {
3333                 /*
3334                  * Fill in as many ITD frames as possible.
3335                  */
3336                 KASSERT(seg < dma->nsegs, ("ohci_device_isoc_enter: overrun"));
3337                 bp0 = dma->segs[seg].ds_addr + segoff;
3338                 sitd->itd.itd_bp0 = htole32(bp0);
3339                 prevpage = OHCI_PAGE(bp0);
3340                 npages = 1;
3341
3342                 ncur = 0;
3343                 while (ncur < OHCI_ITD_NOFFSET && i < nframes) {
3344                         /* Find the frame start and end physical addresses. */
3345                         len = xfer->frlengths[i];
3346                         dataphys = dma->segs[seg].ds_addr + segoff;
3347                         curlen = dma->segs[seg].ds_len - segoff;
3348                         if (len > curlen) {
3349                                 KASSERT(seg + 1 < dma->nsegs,
3350                                     ("ohci_device_isoc_enter: overrun2"));
3351                                 seg++;
3352                                 segoff = len - curlen;
3353                         } else {
3354                                 segoff += len;
3355                         }
3356                         KASSERT(segoff <= dma->segs[seg].ds_len,
3357                             ("ohci_device_isoc_enter: overrun3"));
3358                         physend = dma->segs[seg].ds_addr + segoff - 1;
3359
3360                         /* Check if there would be more than 2 pages . */
3361                         if (OHCI_PAGE(dataphys) != prevpage) {
3362                                 prevpage = OHCI_PAGE(dataphys);
3363                                 npages++;
3364                         }
3365                         if (OHCI_PAGE(physend) != prevpage) {
3366                                 prevpage = OHCI_PAGE(physend);
3367                                 npages++;
3368                         }
3369                         if (npages > 2) {
3370                                 /* We cannot fit this frame now. */
3371                                 segoff -= len;
3372                                 if (segoff < 0) {
3373                                         seg--;
3374                                         segoff += dma->segs[seg].ds_len;
3375                                 }
3376                                 break;
3377                         }
3378
3379                         sitd->itd.itd_be = htole32(physend);
3380                         sitd->itd.itd_offset[ncur] =
3381                             htole16(OHCI_ITD_MK_OFFS(OHCI_PAGE(dataphys) ==
3382                             OHCI_PAGE(bp0) ? 0 : 1, dataphys));
3383                         i++;
3384                         ncur++;
3385                 }
3386                 if (segoff >= dma->segs[seg].ds_len) {
3387                         KASSERT(segoff == dma->segs[seg].ds_len,
3388                             ("ohci_device_isoc_enter: overlap"));
3389                         seg++;
3390                         segoff = 0;
3391                 }
3392
3393                 /* Allocate next ITD */
3394                 nsitd = ohci_alloc_sitd(sc);
3395                 if (nsitd == NULL) {
3396                         /* XXX what now? */
3397                         printf("%s: isoc TD alloc failed\n",
3398                                device_get_nameunit(sc->sc_bus.bdev));
3399                         return;
3400                 }
3401
3402                 /* Fill out remaining fields of current ITD */
3403                 sitd->nextitd = nsitd;
3404                 sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
3405                 sitd->xfer = xfer;
3406                 if (i < nframes) {
3407                         sitd->itd.itd_flags = htole32(
3408                                 OHCI_ITD_NOCC |
3409                                 OHCI_ITD_SET_SF(iso->next) |
3410                                 OHCI_ITD_SET_DI(6) | /* delay intr a little */
3411                                 OHCI_ITD_SET_FC(ncur));
3412                         sitd->flags = OHCI_ITD_ACTIVE;
3413                 } else {
3414                         sitd->itd.itd_flags = htole32(
3415                                 OHCI_ITD_NOCC |
3416                                 OHCI_ITD_SET_SF(iso->next) |
3417                                 OHCI_ITD_SET_DI(0) |
3418                                 OHCI_ITD_SET_FC(ncur));
3419                         sitd->flags = OHCI_CALL_DONE | OHCI_ITD_ACTIVE;
3420                 }
3421                 iso->next += ncur;
3422
3423                 sitd = nsitd;
3424         }
3425
3426         iso->inuse += nframes;
3427
3428         /* XXX pretend we did it all */
3429         xfer->actlen = 0;
3430         for (i = 0; i < nframes; i++)
3431                 xfer->actlen += xfer->frlengths[i];
3432
3433         xfer->status = USBD_IN_PROGRESS;
3434
3435 #ifdef USB_DEBUG
3436         if (ohcidebug > 5) {
3437                 DPRINTF(("ohci_device_isoc_enter: frame=%d\n",
3438                          le32toh(sc->sc_hcca->hcca_frame_number)));
3439                 ohci_dump_itds(xfer->hcpriv);
3440                 ohci_dump_ed(sed);
3441         }
3442 #endif
3443
3444         s = splusb();
3445         opipe->tail.itd = sitd;
3446         sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
3447         sed->ed.ed_tailp = htole32(sitd->physaddr);
3448         splx(s);
3449
3450 #ifdef USB_DEBUG
3451         if (ohcidebug > 5) {
3452                 delay(150000);
3453                 DPRINTF(("ohci_device_isoc_enter: after frame=%d\n",
3454                          le32toh(sc->sc_hcca->hcca_frame_number)));
3455                 ohci_dump_itds(xfer->hcpriv);
3456                 ohci_dump_ed(sed);
3457         }
3458 #endif
3459 }
3460
3461 usbd_status
3462 ohci_device_isoc_start(usbd_xfer_handle xfer)
3463 {
3464         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3465         ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3466         ohci_soft_ed_t *sed;
3467         int s;
3468
3469         DPRINTFN(5,("ohci_device_isoc_start: xfer=%p\n", xfer));
3470
3471         if (sc->sc_dying)
3472                 return (USBD_IOERROR);
3473
3474 #ifdef DIAGNOSTIC
3475         if (xfer->status != USBD_IN_PROGRESS)
3476                 printf("ohci_device_isoc_start: not in progress %p\n", xfer);
3477 #endif
3478
3479         /* XXX anything to do? */
3480
3481         s = splusb();
3482         sed = opipe->sed;  /*  Turn off ED skip-bit to start processing */
3483         sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);    /* ED's ITD list.*/
3484         splx(s);
3485
3486         return (USBD_IN_PROGRESS);
3487 }
3488
3489 void
3490 ohci_device_isoc_abort(usbd_xfer_handle xfer)
3491 {
3492         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3493         ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3494         ohci_soft_ed_t *sed;
3495         ohci_soft_itd_t *sitd, *sitdnext, *tmp_sitd;
3496         int s,undone,num_sitds;
3497
3498         s = splusb();
3499         opipe->aborting = 1;
3500
3501         DPRINTFN(1,("ohci_device_isoc_abort: xfer=%p\n", xfer));
3502
3503         /* Transfer is already done. */
3504         if (xfer->status != USBD_NOT_STARTED &&
3505             xfer->status != USBD_IN_PROGRESS) {
3506                 splx(s);
3507                 printf("ohci_device_isoc_abort: early return\n");
3508                 return;
3509         }
3510
3511         /* Give xfer the requested abort code. */
3512         xfer->status = USBD_CANCELLED;
3513
3514         sed = opipe->sed;
3515         sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
3516
3517         num_sitds = 0;
3518         sitd = xfer->hcpriv;
3519 #ifdef DIAGNOSTIC
3520         if (sitd == NULL) {
3521                 splx(s);
3522                 printf("ohci_device_isoc_abort: hcpriv==0\n");
3523                 return;
3524         }
3525 #endif
3526         for (; sitd != NULL && sitd->xfer == xfer; sitd = sitd->nextitd) {
3527                 num_sitds++;
3528 #ifdef DIAGNOSTIC
3529                 DPRINTFN(1,("abort sets done sitd=%p\n", sitd));
3530                 sitd->isdone = 1;
3531 #endif
3532         }
3533
3534         splx(s);
3535
3536         /*
3537          * Each sitd has up to OHCI_ITD_NOFFSET transfers, each can
3538          * take a usb 1ms cycle. Conservatively wait for it to drain.
3539          * Even with DMA done, it can take awhile for the "batch"
3540          * delivery of completion interrupts to occur thru the controller.
3541          */
3542  
3543         do {
3544                 usb_delay_ms(&sc->sc_bus, 2*(num_sitds*OHCI_ITD_NOFFSET));
3545
3546                 undone   = 0;
3547                 tmp_sitd = xfer->hcpriv;
3548                 for (; tmp_sitd != NULL && tmp_sitd->xfer == xfer;
3549                     tmp_sitd = tmp_sitd->nextitd) {
3550                         if (OHCI_CC_NO_ERROR ==
3551                             OHCI_ITD_GET_CC(le32toh(tmp_sitd->itd.itd_flags)) &&
3552                             tmp_sitd->flags & OHCI_ITD_ACTIVE &&
3553                             (tmp_sitd->flags & OHCI_ITD_INTFIN) == 0)
3554                                 undone++;
3555                 }
3556         } while( undone != 0 );
3557
3558         /* Free the sitds */
3559         for (sitd = xfer->hcpriv; sitd->xfer == xfer;
3560             sitd = sitdnext) {
3561                 sitdnext = sitd->nextitd;
3562                 ohci_free_sitd(sc, sitd);
3563         }
3564
3565         s = splusb();
3566
3567         /* Run callback. */
3568         usb_transfer_complete(xfer);
3569
3570         /* There is always a `next' sitd so link it up. */
3571         sed->ed.ed_headp = htole32(sitd->physaddr);
3572
3573         sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
3574
3575         splx(s);
3576 }
3577
3578 void
3579 ohci_device_isoc_done(usbd_xfer_handle xfer)
3580 {
3581         /* This null routine corresponds to non-isoc "done()" routines
3582          * that free the stds associated with an xfer after a completed
3583          * xfer interrupt. However, in the case of isoc transfers, the
3584          * sitds associated with the transfer have already been processed
3585          * and reallocated for the next iteration by
3586          * "ohci_device_isoc_transfer()".
3587          *
3588          * Routine "usb_transfer_complete()" is called at the end of every
3589          * relevant usb interrupt. "usb_transfer_complete()" indirectly
3590          * calls 1) "ohci_device_isoc_transfer()" (which keeps pumping the
3591          * pipeline by setting up the next transfer iteration) and 2) then 
3592          * calls "ohci_device_isoc_done()". Isoc transfers have not been 
3593          * working for the ohci usb because this routine was trashing the
3594          * xfer set up for the next iteration (thus, only the first 
3595          * UGEN_NISOREQS xfers outstanding on an open would work). Perhaps
3596          * this could all be re-factored, but that's another pass...
3597          */
3598 }
3599
3600 usbd_status
3601 ohci_setup_isoc(usbd_pipe_handle pipe)
3602 {
3603         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3604         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3605         struct iso *iso = &opipe->u.iso;
3606         int s;
3607
3608         iso->next = -1;
3609         iso->inuse = 0;
3610
3611         s = splusb();
3612         ohci_add_ed(opipe->sed, sc->sc_isoc_head);
3613         splx(s);
3614
3615         return (USBD_NORMAL_COMPLETION);
3616 }
3617
3618 void
3619 ohci_device_isoc_close(usbd_pipe_handle pipe)
3620 {
3621         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3622         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3623         ohci_soft_ed_t *sed;
3624
3625         DPRINTF(("ohci_device_isoc_close: pipe=%p\n", pipe));
3626
3627         sed = opipe->sed;
3628         sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* Stop device. */
3629
3630         ohci_close_pipe(pipe, sc->sc_isoc_head); /* Stop isoc list, free ED.*/
3631
3632         /* up to NISOREQs xfers still outstanding. */
3633
3634 #ifdef DIAGNOSTIC
3635         opipe->tail.itd->isdone = 1;
3636 #endif
3637         ohci_free_sitd(sc, opipe->tail.itd);    /* Next `avail free' sitd.*/
3638 }