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