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