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