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