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