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