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