Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / bus / usb / ohci.c
1 /*      $NetBSD: ohci.c,v 1.64 2000/01/19 00:23:58 augustss Exp $       */
2 /*      $FreeBSD: src/sys/dev/usb/ohci.c,v 1.39.2.9 2003/03/05 17:09:44 shiba Exp $     */
3
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 /*
42  * USB Open Host Controller driver.
43  *
44  * OHCI spec: ftp://ftp.compaq.com/pub/supportinformation/papers/hcir1_0a.exe
45  * USB spec: http://www.usb.org/developers/data/usb11.pdf
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
51 #if defined(__NetBSD__) || defined(__OpenBSD__)
52 #include <sys/kernel.h>
53 #include <sys/device.h>
54 #include <sys/select.h>
55 #elif defined(__FreeBSD__)
56 #include <sys/module.h>
57 #include <sys/bus.h>
58 #include <machine/bus_pio.h>
59 #include <machine/bus_memio.h>
60 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
61 #include <machine/cpu.h>
62 #endif
63 #endif
64 #include <sys/proc.h>
65 #include <sys/queue.h>
66 #include <sys/sysctl.h>
67
68 #include <machine/bus.h>
69 #include <machine/endian.h>
70
71 #include <dev/usb/usb.h>
72 #include <dev/usb/usbdi.h>
73 #include <dev/usb/usbdivar.h>
74 #include <dev/usb/usb_mem.h>
75 #include <dev/usb/usb_quirks.h>
76
77 #include <dev/usb/ohcireg.h>
78 #include <dev/usb/ohcivar.h>
79
80 #if defined(__FreeBSD__)
81 #include <machine/clock.h>
82
83 #define delay(d)                DELAY(d)
84 #endif
85
86 #if defined(__OpenBSD__)
87 struct cfdriver ohci_cd = {
88         NULL, "ohci", DV_DULL
89 };
90 #endif
91
92 #ifdef USB_DEBUG
93 #define DPRINTF(x)      if (ohcidebug) logprintf x
94 #define DPRINTFN(n,x)   if (ohcidebug>(n)) logprintf x
95 int ohcidebug = 0;
96 SYSCTL_NODE(_hw_usb, OID_AUTO, ohci, CTLFLAG_RW, 0, "USB ohci");
97 SYSCTL_INT(_hw_usb_ohci, OID_AUTO, debug, CTLFLAG_RW,
98            &ohcidebug, 0, "ohci debug level");
99 #else
100 #define DPRINTF(x)
101 #define DPRINTFN(n,x)
102 #endif
103
104 /*
105  * The OHCI controller is little endian, so on big endian machines
106  * the data strored in memory needs to be swapped.
107  */
108 #if BYTE_ORDER == BIG_ENDIAN
109 #define LE(x) (bswap32(x))
110 #else
111 #define LE(x) (x)
112 #endif
113
114 struct ohci_pipe;
115
116 Static ohci_soft_ed_t  *ohci_alloc_sed(ohci_softc_t *);
117 Static void             ohci_free_sed(ohci_softc_t *, ohci_soft_ed_t *);
118
119 Static ohci_soft_td_t  *ohci_alloc_std(ohci_softc_t *);
120 Static void             ohci_free_std(ohci_softc_t *, ohci_soft_td_t *);
121
122 Static ohci_soft_itd_t *ohci_alloc_sitd(ohci_softc_t *);
123 Static void             ohci_free_sitd(ohci_softc_t *,ohci_soft_itd_t *);
124
125 #if 0
126 Static void             ohci_free_std_chain(ohci_softc_t *, 
127                             ohci_soft_td_t *, ohci_soft_td_t *);
128 #endif
129 Static usbd_status      ohci_alloc_std_chain(struct ohci_pipe *,
130                             ohci_softc_t *, int, int, u_int16_t, usb_dma_t *, 
131                             ohci_soft_td_t *, ohci_soft_td_t **);
132
133 #if defined(__NetBSD__) || defined(__OpenBSD__)
134 Static void             ohci_shutdown(void *v);
135 Static void             ohci_power(int, void *);
136 #endif
137 Static usbd_status      ohci_open(usbd_pipe_handle);
138 Static void             ohci_poll(struct usbd_bus *);
139 Static void             ohci_waitintr(ohci_softc_t *,
140                             usbd_xfer_handle);
141 Static void             ohci_rhsc(ohci_softc_t *, usbd_xfer_handle);
142 Static void             ohci_process_done(ohci_softc_t *,
143                             ohci_physaddr_t);
144
145 Static usbd_status      ohci_device_request(usbd_xfer_handle xfer);
146 Static void             ohci_add_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
147 Static void             ohci_rem_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
148 Static void             ohci_hash_add_td(ohci_softc_t *, 
149                             ohci_soft_td_t *);
150 Static void             ohci_hash_rem_td(ohci_softc_t *,
151                             ohci_soft_td_t *);
152 Static ohci_soft_td_t  *ohci_hash_find_td(ohci_softc_t *,
153                             ohci_physaddr_t);
154
155 Static usbd_status      ohci_setup_isoc(usbd_pipe_handle pipe);
156 Static void             ohci_device_isoc_enter(usbd_xfer_handle);
157
158 Static usbd_status      ohci_allocm(struct usbd_bus *, usb_dma_t *,
159                             u_int32_t);
160 Static void             ohci_freem(struct usbd_bus *, usb_dma_t *);
161
162 Static usbd_xfer_handle ohci_allocx(struct usbd_bus *);
163 Static void             ohci_freex(struct usbd_bus *, usbd_xfer_handle);
164
165 Static usbd_status      ohci_root_ctrl_transfer(usbd_xfer_handle);
166 Static usbd_status      ohci_root_ctrl_start(usbd_xfer_handle);
167 Static void             ohci_root_ctrl_abort(usbd_xfer_handle);
168 Static void             ohci_root_ctrl_close(usbd_pipe_handle);
169
170 Static usbd_status      ohci_root_intr_transfer(usbd_xfer_handle);
171 Static usbd_status      ohci_root_intr_start(usbd_xfer_handle);
172 Static void             ohci_root_intr_abort(usbd_xfer_handle);
173 Static void             ohci_root_intr_close(usbd_pipe_handle);
174 Static void             ohci_root_intr_done (usbd_xfer_handle);
175
176 Static usbd_status      ohci_device_ctrl_transfer(usbd_xfer_handle);
177 Static usbd_status      ohci_device_ctrl_start(usbd_xfer_handle);
178 Static void             ohci_device_ctrl_abort(usbd_xfer_handle);
179 Static void             ohci_device_ctrl_close(usbd_pipe_handle);
180 Static void             ohci_device_ctrl_done (usbd_xfer_handle);
181
182 Static usbd_status      ohci_device_bulk_transfer(usbd_xfer_handle);
183 Static usbd_status      ohci_device_bulk_start(usbd_xfer_handle);
184 Static void             ohci_device_bulk_abort(usbd_xfer_handle);
185 Static void             ohci_device_bulk_close(usbd_pipe_handle);
186 Static void             ohci_device_bulk_done (usbd_xfer_handle);
187
188 Static usbd_status      ohci_device_intr_transfer(usbd_xfer_handle);
189 Static usbd_status      ohci_device_intr_start(usbd_xfer_handle);
190 Static void             ohci_device_intr_abort(usbd_xfer_handle);
191 Static void             ohci_device_intr_close(usbd_pipe_handle);
192 Static void             ohci_device_intr_done (usbd_xfer_handle);
193
194 Static usbd_status      ohci_device_isoc_transfer(usbd_xfer_handle);
195 Static usbd_status      ohci_device_isoc_start(usbd_xfer_handle);
196 Static void             ohci_device_isoc_abort(usbd_xfer_handle);
197 Static void             ohci_device_isoc_close(usbd_pipe_handle);
198 Static void             ohci_device_isoc_done (usbd_xfer_handle);
199
200 Static usbd_status      ohci_device_setintr(ohci_softc_t *sc, 
201                             struct ohci_pipe *pipe, int ival);
202
203 Static int              ohci_str(usb_string_descriptor_t *, int, char *);
204
205 Static void             ohci_timeout(void *);
206 Static void             ohci_rhsc_able(ohci_softc_t *, int);
207
208 Static void             ohci_close_pipe(usbd_pipe_handle pipe, 
209                             ohci_soft_ed_t *head);
210 Static void             ohci_abort_xfer(usbd_xfer_handle xfer,
211                             usbd_status status);
212 Static void             ohci_abort_xfer_end(void *);
213
214 Static void             ohci_device_clear_toggle(usbd_pipe_handle pipe);
215 Static void             ohci_noop(usbd_pipe_handle pipe);
216
217 #ifdef USB_DEBUG
218 Static void             ohci_dumpregs(ohci_softc_t *);
219 Static void             ohci_dump_tds(ohci_soft_td_t *);
220 Static void             ohci_dump_td(ohci_soft_td_t *);
221 Static void             ohci_dump_ed(ohci_soft_ed_t *);
222 #endif
223
224 #define OWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
225 #define OREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
226 #define OREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
227
228 /* Reverse the bits in a value 0 .. 31 */
229 Static u_int8_t revbits[OHCI_NO_INTRS] = 
230   { 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c,
231     0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e,
232     0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d,
233     0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f };
234
235 struct ohci_pipe {
236         struct usbd_pipe pipe;
237         ohci_soft_ed_t *sed;
238         union {
239                 ohci_soft_td_t *td;
240                 ohci_soft_itd_t *itd;
241         } tail;
242         /* Info needed for different pipe kinds. */
243         union {
244                 /* Control pipe */
245                 struct {
246                         usb_dma_t reqdma;
247                         u_int length;
248                         ohci_soft_td_t *setup, *data, *stat;
249                 } ctl;
250                 /* Interrupt pipe */
251                 struct {
252                         int nslots;
253                         int pos;
254                 } intr;
255                 /* Bulk pipe */
256                 struct {
257                         u_int length;
258                         int isread;
259                 } bulk;
260                 /* Iso pipe */
261                 struct iso {
262                         int next, inuse;
263                 } iso;
264         } u;
265 };
266
267 #define OHCI_INTR_ENDPT 1
268
269 Static struct usbd_bus_methods ohci_bus_methods = {
270         ohci_open,
271         ohci_poll,
272         ohci_allocm,
273         ohci_freem,
274         ohci_allocx,
275         ohci_freex,
276 };
277
278 Static struct usbd_pipe_methods ohci_root_ctrl_methods = {      
279         ohci_root_ctrl_transfer,
280         ohci_root_ctrl_start,
281         ohci_root_ctrl_abort,
282         ohci_root_ctrl_close,
283         ohci_noop,
284         0,
285 };
286
287 Static struct usbd_pipe_methods ohci_root_intr_methods = {      
288         ohci_root_intr_transfer,
289         ohci_root_intr_start,
290         ohci_root_intr_abort,
291         ohci_root_intr_close,
292         ohci_noop,
293         ohci_root_intr_done,
294 };
295
296 Static struct usbd_pipe_methods ohci_device_ctrl_methods = {    
297         ohci_device_ctrl_transfer,
298         ohci_device_ctrl_start,
299         ohci_device_ctrl_abort,
300         ohci_device_ctrl_close,
301         ohci_noop,
302         ohci_device_ctrl_done,
303 };
304
305 Static struct usbd_pipe_methods ohci_device_intr_methods = {    
306         ohci_device_intr_transfer,
307         ohci_device_intr_start,
308         ohci_device_intr_abort,
309         ohci_device_intr_close,
310         ohci_device_clear_toggle,
311         ohci_device_intr_done,
312 };
313
314 Static struct usbd_pipe_methods ohci_device_bulk_methods = {    
315         ohci_device_bulk_transfer,
316         ohci_device_bulk_start,
317         ohci_device_bulk_abort,
318         ohci_device_bulk_close,
319         ohci_device_clear_toggle,
320         ohci_device_bulk_done,
321 };
322
323 Static struct usbd_pipe_methods ohci_device_isoc_methods = {
324         ohci_device_isoc_transfer,
325         ohci_device_isoc_start,
326         ohci_device_isoc_abort,
327         ohci_device_isoc_close,
328         ohci_noop,
329         ohci_device_isoc_done,
330 };
331
332 #if defined(__NetBSD__) || defined(__OpenBSD__)
333 int
334 ohci_activate(device_ptr_t self, enum devact act)
335 {
336         struct ohci_softc *sc = (struct ohci_softc *)self;
337         int rv = 0;
338
339         switch (act) {
340         case DVACT_ACTIVATE:
341                 return (EOPNOTSUPP);
342                 break;
343
344         case DVACT_DEACTIVATE:
345                 if (sc->sc_child != NULL)
346                         rv = config_deactivate(sc->sc_child);
347                 break;
348         }
349         return (rv);
350 }
351
352 int
353 ohci_detach(struct ohci_softc *sc, int flags)
354 {
355         int rv = 0;
356
357         if (sc->sc_child != NULL)
358                 rv = config_detach(sc->sc_child, flags);
359         
360         if (rv != 0)
361                 return (rv);
362
363 #if defined(__NetBSD__)
364         powerhook_disestablish(sc->sc_powerhook);
365         shutdownhook_disestablish(sc->sc_shutdownhook);
366 #endif
367         /* free data structures XXX */
368
369         return (rv);
370 }
371 #endif
372
373 ohci_soft_ed_t *
374 ohci_alloc_sed(ohci_softc_t *sc)
375 {
376         ohci_soft_ed_t *sed;
377         usbd_status err;
378         int i, offs;
379         usb_dma_t dma;
380
381         if (sc->sc_freeeds == NULL) {
382                 DPRINTFN(2, ("ohci_alloc_sed: allocating chunk\n"));
383                 err = usb_allocmem(&sc->sc_bus, OHCI_SED_SIZE * OHCI_SED_CHUNK,
384                           OHCI_ED_ALIGN, &dma);
385                 if (err)
386                         return (0);
387                 for(i = 0; i < OHCI_SED_CHUNK; i++) {
388                         offs = i * OHCI_SED_SIZE;
389                         sed = (ohci_soft_ed_t *)((char *)KERNADDR(&dma, offs));
390                         sed->physaddr = DMAADDR(&dma, offs);
391                         sed->next = sc->sc_freeeds;
392                         sc->sc_freeeds = sed;
393                 }
394         }
395         sed = sc->sc_freeeds;
396         sc->sc_freeeds = sed->next;
397         memset(&sed->ed, 0, sizeof(ohci_ed_t));
398         sed->next = 0;
399         return (sed);
400 }
401
402 void
403 ohci_free_sed(ohci_softc_t *sc, ohci_soft_ed_t *sed)
404 {
405         sed->next = sc->sc_freeeds;
406         sc->sc_freeeds = sed;
407 }
408
409 ohci_soft_td_t *
410 ohci_alloc_std(ohci_softc_t *sc)
411 {
412         ohci_soft_td_t *std;
413         usbd_status err;
414         int i, offs;
415         usb_dma_t dma;
416         int s;
417
418         if (sc->sc_freetds == NULL) {
419                 DPRINTFN(2, ("ohci_alloc_std: allocating chunk\n"));
420                 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
421                           OHCI_TD_ALIGN, &dma);
422                 if (err)
423                         return (0);
424                 for(i = 0; i < OHCI_STD_CHUNK; i++) {
425                         offs = i * OHCI_STD_SIZE;
426                         std = (ohci_soft_td_t *)((char *)KERNADDR(&dma, offs));
427                         std->physaddr = DMAADDR(&dma, offs);
428                         std->nexttd = sc->sc_freetds;
429                         sc->sc_freetds = std;
430                 }
431         }
432         std = sc->sc_freetds;
433         sc->sc_freetds = std->nexttd;
434         memset(&std->td, 0, sizeof(ohci_td_t));
435         std->nexttd = NULL;
436
437         s = splusb();
438         ohci_hash_add_td(sc, std);
439         splx(s);
440
441         return (std);
442 }
443
444 void
445 ohci_free_std(ohci_softc_t *sc, ohci_soft_td_t *std)
446 {
447         int s;
448
449         s = splusb();
450         ohci_hash_rem_td(sc, std);
451         splx(s);
452
453         std->nexttd = sc->sc_freetds;
454         sc->sc_freetds = std;
455 }
456
457 usbd_status
458 ohci_alloc_std_chain(struct ohci_pipe *opipe, ohci_softc_t *sc,
459                      int len, int rd, u_int16_t flags, usb_dma_t *dma,
460                      ohci_soft_td_t *std, ohci_soft_td_t **rstd)
461 {
462         ohci_soft_td_t *next, *cur;
463         ohci_physaddr_t dataphys, dataphysend;
464         u_int32_t intr, tdflags;
465         int offset = 0;
466         int curlen;
467
468         DPRINTFN(len < 4096,("ohci_alloc_std_chain: start len=%d\n", len));
469
470         cur = std;
471
472         dataphysend = OHCI_PAGE(DMAADDR(dma, len - 1));
473         tdflags = 
474             (rd ? OHCI_TD_IN : OHCI_TD_OUT) | 
475             OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY | 
476             (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0);
477
478         for (;;) {
479                 next = ohci_alloc_std(sc);
480                 if (next == 0)
481                         goto nomem;
482
483                 dataphys = DMAADDR(dma, offset);
484
485                 /* The OHCI hardware can handle at most one page crossing. */
486 #if defined(__NetBSD__) || defined(__OpenBSD__)
487                 if (OHCI_PAGE(dataphys) == dataphysend ||
488                     OHCI_PAGE(dataphys) + OHCI_PAGE_SIZE == dataphysend)
489 #elif defined(__FreeBSD__)
490                 /* XXX This is pretty broken: Because we do not allocate
491                  * a contiguous buffer (contiguous in physical pages) we
492                  * can only transfer one page in one go.
493                  * So check whether the start and end of the buffer are on
494                  * the same page.
495                  */
496                 if (OHCI_PAGE(dataphys) == dataphysend)
497 #endif
498                 {
499                         /* we can handle it in this TD */
500                         curlen = len;
501                 } else {
502                         /* XXX The calculation below is wrong and could
503                          * result in a packet that is not a multiple of the
504                          * MaxPacketSize in the case where the buffer does not
505                          * start on an appropriate address (like for example in
506                          * the case of an mbuf cluster). You'll get an early
507                          * short packet.
508                          */
509 #if defined(__NetBSD__) || defined(__OpenBSD__)
510                         /* must use multiple TDs, fill as much as possible. */
511                         curlen = 2 * OHCI_PAGE_SIZE - 
512                                  OHCI_PAGE_MASK(dataphys);
513                         if (curlen > len)       /* may have fit in one page */
514                                 curlen = len;
515 #elif defined(__FreeBSD__)
516                         /* See comment above (XXX) */
517                         curlen = OHCI_PAGE_SIZE - 
518                                  OHCI_PAGE_MASK(dataphys);
519 #endif
520                 }
521                 DPRINTFN(4,("ohci_alloc_std_chain: dataphys=0x%08x "
522                             "dataphysend=0x%08x len=%d curlen=%d\n",
523                             dataphys, dataphysend,
524                             len, curlen));
525                 len -= curlen;
526
527                 intr = len == 0 ? OHCI_TD_SET_DI(1) : OHCI_TD_NOINTR;
528                 cur->td.td_flags = LE(tdflags | intr);
529                 cur->td.td_cbp = LE(dataphys);
530                 cur->nexttd = next;
531                 cur->td.td_nexttd = LE(next->physaddr);
532                 cur->td.td_be = LE(dataphys + curlen - 1);
533                 cur->len = curlen;
534                 cur->flags = OHCI_ADD_LEN;
535                 DPRINTFN(10,("ohci_alloc_std_chain: cbp=0x%08x be=0x%08x\n",
536                             dataphys, dataphys + curlen - 1));
537                 if (len == 0)
538                         break;
539                 DPRINTFN(10,("ohci_alloc_std_chain: extend chain\n"));
540                 offset += curlen;
541                 cur = next;
542         }
543         if ((flags & USBD_FORCE_SHORT_XFER) &&
544             len % UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize) == 0) {
545                 /* Force a 0 length transfer at the end. */
546
547                 cur->td.td_flags = LE(tdflags | OHCI_TD_NOINTR);
548                 cur = next;
549
550                 next = ohci_alloc_std(sc);
551                 if (next == 0)
552                         goto nomem;
553
554                 cur->td.td_flags = LE(tdflags | OHCI_TD_SET_DI(1));
555                 cur->td.td_cbp = 0; /* indicate 0 length packet */
556                 cur->nexttd = next;
557                 cur->td.td_nexttd = LE(next->physaddr);
558                 cur->td.td_be = ~0;
559                 cur->len = 0;
560                 cur->flags = 0;
561                 DPRINTFN(2,("ohci_alloc_std_chain: add 0 xfer\n"));
562         }
563         cur->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
564         *rstd = next;
565
566         return (USBD_NORMAL_COMPLETION);
567
568  nomem:
569         /* XXX free chain */
570         return (USBD_NOMEM);
571 }
572
573 #if 0
574 Static void
575 ohci_free_std_chain(ohci_softc_t *sc, ohci_soft_td_t *std,
576                     ohci_soft_td_t *stdend)
577 {
578         ohci_soft_td_t *p;
579
580         for (; std != stdend; std = p) {
581                 p = std->nexttd;
582                 ohci_free_std(sc, std);
583         }
584 }
585 #endif
586
587 ohci_soft_itd_t *
588 ohci_alloc_sitd(ohci_softc_t *sc)
589 {
590         ohci_soft_itd_t *sitd;
591         usbd_status err;
592         int i, offs;
593         usb_dma_t dma;
594
595         if (sc->sc_freeitds == NULL) {
596                 DPRINTFN(2, ("ohci_alloc_sitd: allocating chunk\n"));
597                 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
598                           OHCI_TD_ALIGN, &dma);
599                 if (err)
600                         return (0);
601                 for(i = 0; i < OHCI_STD_CHUNK; i++) {
602                         offs = i * OHCI_STD_SIZE;
603                         sitd = (ohci_soft_itd_t *)((char*)KERNADDR(&dma, offs));
604                         sitd->physaddr = DMAADDR(&dma, offs);
605                         sitd->nextitd = sc->sc_freeitds;
606                         sc->sc_freeitds = sitd;
607                 }
608         }
609         sitd = sc->sc_freeitds;
610         sc->sc_freeitds = sitd->nextitd;
611         memset(&sitd->itd, 0, sizeof(ohci_itd_t));
612         sitd->nextitd = 0;
613         return (sitd);
614 }
615
616 void
617 ohci_free_sitd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
618 {
619         sitd->nextitd = sc->sc_freeitds;
620         sc->sc_freeitds = sitd;
621 }
622
623 usbd_status
624 ohci_init(ohci_softc_t *sc)
625 {
626         ohci_soft_ed_t *sed, *psed;
627         usbd_status err;
628         int i;
629         u_int32_t s, ctl, ival, hcr, fm, per, rev;
630
631         DPRINTF(("ohci_init: start\n"));
632 #if defined(__OpenBSD__)
633         printf(",");
634 #else
635         printf("%s:", USBDEVNAME(sc->sc_bus.bdev));
636 #endif
637         rev = OREAD4(sc, OHCI_REVISION);
638         printf(" OHCI version %d.%d%s\n", OHCI_REV_HI(rev), OHCI_REV_LO(rev),
639                OHCI_REV_LEGACY(rev) ? ", legacy support" : "");
640
641         if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
642                 printf("%s: unsupported OHCI revision\n", 
643                        USBDEVNAME(sc->sc_bus.bdev));
644                 sc->sc_bus.usbrev = USBREV_UNKNOWN;
645                 return (USBD_INVAL);
646         }
647         sc->sc_bus.usbrev = USBREV_1_0;
648
649         for (i = 0; i < OHCI_HASH_SIZE; i++)
650                 LIST_INIT(&sc->sc_hash_tds[i]);
651
652         SIMPLEQ_INIT(&sc->sc_free_xfers);
653
654         /* Allocate the HCCA area. */
655         err = usb_allocmem(&sc->sc_bus, OHCI_HCCA_SIZE, 
656                          OHCI_HCCA_ALIGN, &sc->sc_hccadma);
657         if (err)
658                 return (err);
659         sc->sc_hcca = (struct ohci_hcca *)KERNADDR(&sc->sc_hccadma, 0);
660         memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);
661
662         sc->sc_eintrs = OHCI_NORMAL_INTRS;
663
664         /* Allocate dummy ED that starts the control list. */
665         sc->sc_ctrl_head = ohci_alloc_sed(sc);
666         if (sc->sc_ctrl_head == NULL) {
667                 err = USBD_NOMEM;
668                 goto bad1;
669         }
670         sc->sc_ctrl_head->ed.ed_flags |= LE(OHCI_ED_SKIP);
671
672         /* Allocate dummy ED that starts the bulk list. */
673         sc->sc_bulk_head = ohci_alloc_sed(sc);
674         if (sc->sc_bulk_head == NULL) {
675                 err = USBD_NOMEM;
676                 goto bad2;
677         }
678         sc->sc_bulk_head->ed.ed_flags |= LE(OHCI_ED_SKIP);
679
680         /* Allocate dummy ED that starts the isochronous list. */
681         sc->sc_isoc_head = ohci_alloc_sed(sc);
682         if (sc->sc_isoc_head == NULL) {
683                 err = USBD_NOMEM;
684                 goto bad3;
685         }
686         sc->sc_isoc_head->ed.ed_flags |= LE(OHCI_ED_SKIP);
687
688         /* Allocate all the dummy EDs that make up the interrupt tree. */
689         for (i = 0; i < OHCI_NO_EDS; i++) {
690                 sed = ohci_alloc_sed(sc);
691                 if (sed == NULL) {
692                         while (--i >= 0)
693                                 ohci_free_sed(sc, sc->sc_eds[i]);
694                         err = USBD_NOMEM;
695                         goto bad4;
696                 }
697                 /* All ED fields are set to 0. */
698                 sc->sc_eds[i] = sed;
699                 sed->ed.ed_flags |= LE(OHCI_ED_SKIP);
700                 if (i != 0)
701                         psed = sc->sc_eds[(i-1) / 2];
702                 else
703                         psed= sc->sc_isoc_head;
704                 sed->next = psed;
705                 sed->ed.ed_nexted = LE(psed->physaddr);
706         }
707         /* 
708          * Fill HCCA interrupt table.  The bit reversal is to get
709          * the tree set up properly to spread the interrupts.
710          */
711         for (i = 0; i < OHCI_NO_INTRS; i++)
712                 sc->sc_hcca->hcca_interrupt_table[revbits[i]] = 
713                         LE(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr);
714
715         /* Determine in what context we are running. */
716         ctl = OREAD4(sc, OHCI_CONTROL);
717         if (ctl & OHCI_IR) {
718                 /* SMM active, request change */
719                 DPRINTF(("ohci_init: SMM active, request owner change\n"));
720                 s = OREAD4(sc, OHCI_COMMAND_STATUS);
721                 OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR);
722                 for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
723                         usb_delay_ms(&sc->sc_bus, 1);
724                         ctl = OREAD4(sc, OHCI_CONTROL);
725                 }
726                 if ((ctl & OHCI_IR) == 0) {
727                         printf("%s: SMM does not respond, resetting\n",
728                                USBDEVNAME(sc->sc_bus.bdev));
729                         OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
730                         goto reset;
731                 }
732         } else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) {
733                 /* BIOS started controller. */
734                 DPRINTF(("ohci_init: BIOS active\n"));
735                 if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) {
736                         OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL);
737                         usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
738                 }
739         } else {
740                 DPRINTF(("ohci_init: cold started\n"));
741         reset:
742                 /* Controller was cold started. */
743                 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
744         }
745
746         /*
747          * This reset should not be necessary according to the OHCI spec, but
748          * without it some controllers do not start.
749          */
750         DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
751         OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
752         usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
753
754         /* We now own the host controller and the bus has been reset. */
755         ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
756
757         OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
758         /* Nominal time for a reset is 10 us. */
759         for (i = 0; i < 10; i++) {
760                 delay(10);
761                 hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
762                 if (!hcr)
763                         break;
764         }
765         if (hcr) {
766                 printf("%s: reset timeout\n", USBDEVNAME(sc->sc_bus.bdev));
767                 err = USBD_IOERROR;
768                 goto bad5;
769         }
770 #ifdef USB_DEBUG
771         if (ohcidebug > 15)
772                 ohci_dumpregs(sc);
773 #endif
774
775         /* The controller is now in SUSPEND state, we have 2ms to finish. */
776
777         /* Set up HC registers. */
778         OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0));
779         OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
780         OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
781         /* disable all interrupts and then switch on all desired interrupts */
782         OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
783         OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);
784         /* switch on desired functional features */
785         ctl = OREAD4(sc, OHCI_CONTROL);
786         ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
787         ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
788                 OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
789         /* And finally start it! */
790         OWRITE4(sc, OHCI_CONTROL, ctl);
791
792         /*
793          * The controller is now OPERATIONAL.  Set a some final
794          * registers that should be set earlier, but that the
795          * controller ignores when in the SUSPEND state.
796          */
797         fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
798         fm |= OHCI_FSMPS(ival) | ival;
799         OWRITE4(sc, OHCI_FM_INTERVAL, fm);
800         per = OHCI_PERIODIC(ival); /* 90% periodic */
801         OWRITE4(sc, OHCI_PERIODIC_START, per);
802
803         OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */
804
805         sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
806
807 #ifdef USB_DEBUG
808         if (ohcidebug > 5)
809                 ohci_dumpregs(sc);
810 #endif
811         
812         /* Set up the bus struct. */
813         sc->sc_bus.methods = &ohci_bus_methods;
814         sc->sc_bus.pipe_size = sizeof(struct ohci_pipe);
815
816 #if defined(__NetBSD__)
817         sc->sc_powerhook = powerhook_establish(ohci_power, sc);
818         sc->sc_shutdownhook = shutdownhook_establish(ohci_shutdown, sc);
819 #endif
820
821         return (USBD_NORMAL_COMPLETION);
822
823  bad5:
824         for (i = 0; i < OHCI_NO_EDS; i++)
825                 ohci_free_sed(sc, sc->sc_eds[i]);
826  bad4:
827         ohci_free_sed(sc, sc->sc_isoc_head);
828  bad3:
829         ohci_free_sed(sc, sc->sc_ctrl_head);
830  bad2:
831         ohci_free_sed(sc, sc->sc_bulk_head);
832  bad1:
833         usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
834         return (err);
835 }
836
837 usbd_status
838 ohci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
839 {
840 #if defined(__NetBSD__) || defined(__OpenBSD__)
841         struct ohci_softc *sc = (struct ohci_softc *)bus;
842 #endif
843
844         return (usb_allocmem(&sc->sc_bus, size, 0, dma));
845 }
846
847 void
848 ohci_freem(struct usbd_bus *bus, usb_dma_t *dma)
849 {
850 #if defined(__NetBSD__) || defined(__OpenBSD__)
851         struct ohci_softc *sc = (struct ohci_softc *)bus;
852 #endif
853
854         usb_freemem(&sc->sc_bus, dma);
855 }
856
857 usbd_xfer_handle
858 ohci_allocx(struct usbd_bus *bus)
859 {
860         struct ohci_softc *sc = (struct ohci_softc *)bus;
861         usbd_xfer_handle xfer;
862
863         xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
864         if (xfer != NULL)
865                 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, xfer, next);
866         else
867                 xfer = malloc(sizeof(*xfer), M_USB, M_NOWAIT);
868         if (xfer != NULL)
869                 memset(xfer, 0, sizeof *xfer);
870         return (xfer);
871 }
872
873 void
874 ohci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
875 {
876         struct ohci_softc *sc = (struct ohci_softc *)bus;
877
878         SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
879 }
880
881 /*
882  * Shut down the controller when the system is going down.
883  */
884 #if defined(__NetBSD__) || defined(__OpenBSD__)
885 void
886 ohci_shutdown(void *v)
887 {
888         ohci_softc_t *sc = v;
889
890         DPRINTF(("ohci_shutdown: stopping the HC\n"));
891         OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
892 }
893
894 /*
895  * Handle suspend/resume.
896  *
897  * We need to switch to polling mode here, because this routine is
898  * called from an intterupt context.  This is all right since we
899  * are almost suspended anyway.
900  */
901 void
902 ohci_power(int why, void *v)
903 {
904 #ifdef USB_DEBUG
905         ohci_softc_t *sc = v;
906
907         DPRINTF(("ohci_power: sc=%p, why=%d\n", sc, why));
908         /* XXX should suspend/resume */
909         ohci_dumpregs(sc);
910 #endif
911 }
912 #endif
913
914 #ifdef USB_DEBUG
915 void
916 ohci_dumpregs(ohci_softc_t *sc)
917 {
918         DPRINTF(("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
919                  OREAD4(sc, OHCI_REVISION),
920                  OREAD4(sc, OHCI_CONTROL),
921                  OREAD4(sc, OHCI_COMMAND_STATUS)));
922         DPRINTF(("               intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
923                  OREAD4(sc, OHCI_INTERRUPT_STATUS),
924                  OREAD4(sc, OHCI_INTERRUPT_ENABLE),
925                  OREAD4(sc, OHCI_INTERRUPT_DISABLE)));
926         DPRINTF(("               hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
927                  OREAD4(sc, OHCI_HCCA),
928                  OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
929                  OREAD4(sc, OHCI_CONTROL_HEAD_ED)));
930         DPRINTF(("               ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
931                  OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
932                  OREAD4(sc, OHCI_BULK_HEAD_ED),
933                  OREAD4(sc, OHCI_BULK_CURRENT_ED)));
934         DPRINTF(("               done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
935                  OREAD4(sc, OHCI_DONE_HEAD),
936                  OREAD4(sc, OHCI_FM_INTERVAL),
937                  OREAD4(sc, OHCI_FM_REMAINING)));
938         DPRINTF(("               fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
939                  OREAD4(sc, OHCI_FM_NUMBER),
940                  OREAD4(sc, OHCI_PERIODIC_START),
941                  OREAD4(sc, OHCI_LS_THRESHOLD)));
942         DPRINTF(("               desca=0x%08x descb=0x%08x stat=0x%08x\n",
943                  OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
944                  OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
945                  OREAD4(sc, OHCI_RH_STATUS)));
946         DPRINTF(("               port1=0x%08x port2=0x%08x\n",
947                  OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
948                  OREAD4(sc, OHCI_RH_PORT_STATUS(2))));
949         DPRINTF(("         HCCA: frame_number=0x%04x done_head=0x%08x\n",
950                  LE(sc->sc_hcca->hcca_frame_number),
951                  LE(sc->sc_hcca->hcca_done_head)));
952 }
953 #endif
954
955 Static int ohci_intr1(ohci_softc_t *);
956
957 int
958 ohci_intr(void *p)
959 {
960         ohci_softc_t *sc = p;
961
962         /* If we get an interrupt while polling, then just ignore it. */
963         if (sc->sc_bus.use_polling) {
964 #ifdef DIAGNOSTIC
965                 printf("ohci_intr: ignored interrupt while polling\n");
966 #endif
967                 return (0);
968         }
969
970         return (ohci_intr1(sc)); 
971 }
972
973 Static int
974 ohci_intr1(ohci_softc_t *sc)
975 {
976         u_int32_t intrs, eintrs;
977         ohci_physaddr_t done;
978
979         /* In case the interrupt occurs before initialization has completed. */
980         if (sc == NULL || sc->sc_hcca == NULL) {
981 #ifdef DIAGNOSTIC
982                 printf("ohci_intr: sc->sc_hcca == NULL\n");
983 #endif
984                 return (0);
985         }
986
987         intrs = 0;
988         done = LE(sc->sc_hcca->hcca_done_head);
989
990         /* The LSb of done is used to inform the HC Driver that an interrupt
991          * condition exists for both the Done list and for another event
992          * recorded in HcInterruptStatus. On an interrupt from the HC, the HC
993          * Driver checks the HccaDoneHead Value. If this value is 0, then the
994          * interrupt was caused by other than the HccaDoneHead update and the
995          * HcInterruptStatus register needs to be accessed to determine that
996          * exact interrupt cause. If HccaDoneHead is nonzero, then a Done list
997          * update interrupt is indicated and if the LSb of done is nonzero,
998          * then an additional interrupt event is indicated and
999          * HcInterruptStatus should be checked to determine its cause.
1000          */
1001         if (done != 0) {
1002                 sc->sc_hcca->hcca_done_head = 0;
1003                 if (done & ~OHCI_DONE_INTRS)
1004                         intrs = OHCI_WDH;
1005                 if (done & OHCI_DONE_INTRS) {
1006                         intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
1007                         done &= ~OHCI_DONE_INTRS;
1008                 }
1009         } else {
1010                 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1011         }
1012
1013         if (intrs == 0) {
1014                 /* nothing to be done ?! */
1015                 return (0);
1016         }
1017
1018         intrs &= ~OHCI_MIE;     /* mask out Master Interrupt Enable */
1019
1020         /* Acknowledge any interrupts that have happened */
1021         OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs);
1022
1023         /* Any interrupts we had enabled? */
1024         eintrs = intrs & sc->sc_eintrs;
1025         if (!eintrs)
1026                 return (0);
1027
1028         sc->sc_bus.intr_context++;
1029         sc->sc_bus.no_intrs++;
1030         DPRINTFN(7, ("ohci_intr: sc=%p intrs=%x(%x) eintr=%x\n", 
1031                      sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS),
1032                      (u_int)eintrs));
1033
1034         if (eintrs & OHCI_SO) {
1035                 printf("%s: scheduling overrun\n",USBDEVNAME(sc->sc_bus.bdev));
1036                 /* XXX do what */
1037                 intrs &= ~OHCI_SO;
1038         }
1039         if (eintrs & OHCI_WDH) {
1040                 ohci_process_done(sc, done);
1041                 intrs &= ~OHCI_WDH;
1042         }
1043         if (eintrs & OHCI_RD) {
1044                 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1045                 /* XXX process resume detect */
1046         }
1047         if (eintrs & OHCI_UE) {
1048                 printf("%s: unrecoverable error, controller halted\n",
1049                        USBDEVNAME(sc->sc_bus.bdev));
1050                 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1051                 /* XXX what else */
1052         }
1053         if (eintrs & OHCI_RHSC) {
1054                 ohci_rhsc(sc, sc->sc_intrxfer);
1055                 intrs &= ~OHCI_RHSC;
1056
1057                 /* 
1058                  * Disable RHSC interrupt for now, because it will be
1059                  * on until the port has been reset.
1060                  */
1061                 ohci_rhsc_able(sc, 0);
1062         }
1063
1064         sc->sc_bus.intr_context--;
1065
1066         /* Block unprocessed interrupts. XXX */
1067         OWRITE4(sc, OHCI_INTERRUPT_DISABLE, intrs);
1068         sc->sc_eintrs &= ~intrs;
1069
1070         return (1);
1071 }
1072
1073 void
1074 ohci_rhsc_able(ohci_softc_t *sc, int on)
1075 {
1076         DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on));
1077         if (on) {
1078                 sc->sc_eintrs |= OHCI_RHSC;
1079                 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
1080         } else {
1081                 sc->sc_eintrs &= ~OHCI_RHSC;
1082                 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
1083         }
1084 }
1085
1086 #ifdef USB_DEBUG
1087 char *ohci_cc_strs[] = {
1088         "NO_ERROR",
1089         "CRC",
1090         "BIT_STUFFING",
1091         "DATA_TOGGLE_MISMATCH",
1092         "STALL",
1093         "DEVICE_NOT_RESPONDING",
1094         "PID_CHECK_FAILURE",
1095         "UNEXPECTED_PID",
1096         "DATA_OVERRUN",
1097         "DATA_UNDERRUN",
1098         "BUFFER_OVERRUN",
1099         "BUFFER_UNDERRUN",
1100         "reserved",
1101         "reserved",
1102         "NOT_ACCESSED",
1103         "NOT_ACCESSED"
1104 };
1105 #endif
1106
1107 void
1108 ohci_process_done(ohci_softc_t *sc, ohci_physaddr_t done)
1109 {
1110         ohci_soft_td_t *std, *sdone, *stdnext;
1111         usbd_xfer_handle xfer;
1112         int len, cc;
1113
1114         DPRINTFN(10,("ohci_process_done: done=0x%08lx\n", (u_long)done));
1115
1116         /* Reverse the done list and store the reversed list in sdone */
1117         sdone = NULL;
1118         for (; done; done = LE(std->td.td_nexttd)) {
1119                 std = ohci_hash_find_td(sc, done & LE(OHCI_TAILMASK));
1120                 if (std == NULL) {
1121 #ifdef USB_DEBUG
1122                         DPRINTF(("%s: Invalid done queue 0x%08x",
1123                                 USBDEVNAME(sc->sc_bus.bdev), done));
1124                         ohci_dumpregs(sc);
1125 #endif
1126                         /* XXX Should we compare the list of active TDs with
1127                          * the list of TDs queued at EDs to handle the ones that
1128                          * are not listed on any of the ED queues and therefore
1129                          * must be finished?
1130                          */
1131                         return;
1132                 }
1133
1134                 std->dnext = sdone;
1135                 sdone = std;
1136         }
1137
1138 #ifdef USB_DEBUG
1139         if (ohcidebug > 10) {
1140                 DPRINTF(("ohci_process_done: TD done:\n"));
1141                 for (std = sdone; std; std = std->dnext)
1142                         ohci_dump_td(sdone);
1143         }
1144 #endif
1145
1146         for (std = sdone; std; std = stdnext) {
1147                 xfer = std->xfer;
1148                 stdnext = std->dnext;
1149                 DPRINTFN(5, ("ohci_process_done: std=%p xfer=%p hcpriv=%p\n",
1150                                 std, xfer, (xfer? xfer->hcpriv:NULL)));
1151                 if (xfer == NULL || (std->flags & OHCI_TD_HANDLED)) {
1152                         /* xfer == NULL: There seems to be no xfer associated
1153                          * with this TD. It is tailp that happened to end up on
1154                          * the done queue.
1155                          * flags & OHCI_TD_HANDLED: The TD has already been
1156                          * handled by process_done and should not be done again.
1157                          */
1158                         continue;
1159                 }
1160                 cc = OHCI_TD_GET_CC(LE(std->td.td_flags));
1161                 usb_untimeout(ohci_timeout, xfer, xfer->timo_handle);
1162                 if (xfer->status == USBD_CANCELLED ||
1163                     xfer->status == USBD_TIMEOUT) {
1164                         DPRINTF(("ohci_process_done: cancel/timeout, xfer=%p\n",
1165                                  xfer));
1166                         /* Handled by abort routine. */
1167                 } else if (cc == OHCI_CC_NO_ERROR) {
1168                         DPRINTFN(15, ("ohci_process_done: no error, xfer=%p\n",
1169                                  xfer));
1170                         len = std->len;
1171                         if (std->td.td_cbp != 0)
1172                                 len -= LE(std->td.td_be) -
1173                                        LE(std->td.td_cbp) + 1;
1174                         if (std->flags & OHCI_ADD_LEN)
1175                                 xfer->actlen += len;
1176                         if (std->flags & OHCI_CALL_DONE) {
1177                                 xfer->status = USBD_NORMAL_COMPLETION;
1178                                 usb_transfer_complete(xfer);
1179                         }
1180                         ohci_free_std(sc, std);
1181                 } else {
1182                         /*
1183                          * Endpoint is halted.  First unlink all the TDs
1184                          * belonging to the failed transfer, and then restart
1185                          * the endpoint.
1186                          */
1187                         ohci_soft_td_t *p, *n;
1188                         struct ohci_pipe *opipe = 
1189                                 (struct ohci_pipe *)xfer->pipe;
1190
1191                         DPRINTF(("ohci_process_done: err cc=%d (%s), xfer=%p\n",
1192                           OHCI_TD_GET_CC(LE(std->td.td_flags)),
1193                           ohci_cc_strs[OHCI_TD_GET_CC(LE(std->td.td_flags))],
1194                           xfer));
1195
1196                         /* Mark all the TDs in the done queue for the current
1197                          * xfer as handled
1198                          */
1199                         for (p = stdnext; p; p = p->dnext) {
1200                                 if (p->xfer == xfer)
1201                                         p->flags |= OHCI_TD_HANDLED;
1202                         }
1203
1204                         /* remove TDs for the current xfer from the ED */
1205                         for (p = std; p->xfer == xfer; p = n) {
1206                                 n = p->nexttd;
1207                                 ohci_free_std(sc, p);
1208                         }
1209                         opipe->sed->ed.ed_headp = LE(p->physaddr);
1210
1211                         /* XXX why is this being done? Why not OHCI_BLF too */
1212                         OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1213
1214                         if (cc == OHCI_CC_STALL)
1215                                 xfer->status = USBD_STALLED;
1216                         else
1217                                 xfer->status = USBD_IOERROR;
1218
1219                         usb_transfer_complete(xfer);
1220                 }
1221         }
1222 }
1223
1224 void
1225 ohci_device_ctrl_done(usbd_xfer_handle xfer)
1226 {
1227         DPRINTFN(10,("ohci_ctrl_done: xfer=%p\n", xfer));
1228
1229 #ifdef DIAGNOSTIC
1230         if (!(xfer->rqflags & URQ_REQUEST)) {
1231                 panic("ohci_ctrl_done: not a request\n");
1232         }
1233 #endif
1234         xfer->hcpriv = NULL;
1235 }
1236
1237 void
1238 ohci_device_intr_done(usbd_xfer_handle xfer)
1239 {
1240         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1241         ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1242         ohci_soft_ed_t *sed = opipe->sed;
1243         ohci_soft_td_t *data, *tail;
1244
1245
1246         DPRINTFN(10,("ohci_intr_done: xfer=%p, actlen=%d\n", 
1247                      xfer, xfer->actlen));
1248
1249         xfer->hcpriv = NULL;
1250
1251         if (xfer->pipe->repeat) {
1252                 data = opipe->tail.td;
1253                 tail = ohci_alloc_std(sc); /* XXX should reuse TD */
1254                 if (tail == NULL) {
1255                         xfer->status = USBD_NOMEM;
1256                         return;
1257                 }
1258                 tail->xfer = NULL;
1259                 
1260                 data->td.td_flags = LE(
1261                         OHCI_TD_IN | OHCI_TD_NOCC | 
1262                         OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
1263                 if (xfer->flags & USBD_SHORT_XFER_OK)
1264                         data->td.td_flags |= LE(OHCI_TD_R);
1265                 data->td.td_cbp = LE(DMAADDR(&xfer->dmabuf, 0));
1266                 data->nexttd = tail;
1267                 data->td.td_nexttd = LE(tail->physaddr);
1268                 data->td.td_be = LE(LE(data->td.td_cbp) + xfer->length - 1);
1269                 data->len = xfer->length;
1270                 data->xfer = xfer;
1271                 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1272                 xfer->hcpriv = data;
1273                 xfer->actlen = 0;
1274
1275                 ohci_hash_add_td(sc, data);
1276                 sed->ed.ed_tailp = LE(tail->physaddr);
1277                 opipe->tail.td = tail;
1278         }
1279 }
1280
1281 void
1282 ohci_device_bulk_done(usbd_xfer_handle xfer)
1283 {
1284         DPRINTFN(10,("ohci_bulk_done: xfer=%p, actlen=%d\n", 
1285                      xfer, xfer->actlen));
1286
1287         xfer->hcpriv = NULL;
1288 }
1289
1290 void
1291 ohci_rhsc(ohci_softc_t *sc, usbd_xfer_handle xfer)
1292 {
1293         usbd_pipe_handle pipe;
1294         struct ohci_pipe *opipe;
1295         u_char *p;
1296         int i, m;
1297         int hstatus;
1298
1299         hstatus = OREAD4(sc, OHCI_RH_STATUS);
1300         DPRINTF(("ohci_rhsc: sc=%p xfer=%p hstatus=0x%08x\n", 
1301                  sc, xfer, hstatus));
1302
1303         if (xfer == NULL) {
1304                 /* Just ignore the change. */
1305                 return;
1306         }
1307
1308         pipe = xfer->pipe;
1309         opipe = (struct ohci_pipe *)pipe;
1310
1311         p = KERNADDR(&xfer->dmabuf, 0);
1312         m = min(sc->sc_noport, xfer->length * 8 - 1);
1313         memset(p, 0, xfer->length);
1314         for (i = 1; i <= m; i++) {
1315                 if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
1316                         p[i/8] |= 1 << (i%8);
1317         }
1318         DPRINTF(("ohci_rhsc: change=0x%02x\n", *p));
1319         xfer->actlen = xfer->length;
1320         xfer->status = USBD_NORMAL_COMPLETION;
1321
1322         usb_transfer_complete(xfer);
1323 }
1324
1325 void
1326 ohci_root_intr_done(usbd_xfer_handle xfer)
1327 {
1328         xfer->hcpriv = NULL;
1329 }
1330
1331 /*
1332  * Wait here until controller claims to have an interrupt.
1333  * Then call ohci_intr and return.  Use timeout to avoid waiting
1334  * too long.
1335  */
1336 void
1337 ohci_waitintr(ohci_softc_t *sc, usbd_xfer_handle xfer)
1338 {
1339         int timo = xfer->timeout;
1340         int usecs;
1341         u_int32_t intrs;
1342
1343         xfer->status = USBD_IN_PROGRESS;
1344         for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
1345                 usb_delay_ms(&sc->sc_bus, 1);
1346                 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs;
1347                 DPRINTFN(15,("ohci_waitintr: 0x%04x\n", intrs));
1348 #ifdef USB_DEBUG
1349                 if (ohcidebug > 15)
1350                         ohci_dumpregs(sc);
1351 #endif
1352                 if (intrs) {
1353                         ohci_intr1(sc);
1354                         if (xfer->status != USBD_IN_PROGRESS)
1355                                 return;
1356                 }
1357         }
1358
1359         /* Timeout */
1360         DPRINTF(("ohci_waitintr: timeout\n"));
1361 #ifdef USB_DEBUG
1362         ohci_dumpregs(sc);
1363 #endif
1364         xfer->status = USBD_TIMEOUT;
1365         usb_transfer_complete(xfer);
1366         /* XXX should free TD */
1367 }
1368
1369 void
1370 ohci_poll(struct usbd_bus *bus)
1371 {
1372         ohci_softc_t *sc = (ohci_softc_t *)bus;
1373
1374         if (OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs)
1375                 ohci_intr1(sc);
1376 }
1377
1378 usbd_status
1379 ohci_device_request(usbd_xfer_handle xfer)
1380 {
1381         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1382         usb_device_request_t *req = &xfer->request;
1383         usbd_device_handle dev = opipe->pipe.device;
1384         ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1385         int addr = dev->address;
1386         ohci_soft_td_t *setup, *data = 0, *stat, *next, *tail;
1387         ohci_soft_ed_t *sed;
1388         int isread;
1389         int len;
1390         usbd_status err;
1391         int s;
1392
1393         isread = req->bmRequestType & UT_READ;
1394         len = UGETW(req->wLength);
1395
1396         DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, "
1397                     "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1398                     req->bmRequestType, req->bRequest, UGETW(req->wValue),
1399                     UGETW(req->wIndex), len, addr, 
1400                     opipe->pipe.endpoint->edesc->bEndpointAddress));
1401
1402         setup = opipe->tail.td;
1403         stat = ohci_alloc_std(sc);
1404         if (stat == NULL) {
1405                 err = USBD_NOMEM;
1406                 goto bad1;
1407         }
1408         tail = ohci_alloc_std(sc);
1409         if (tail == NULL) {
1410                 err = USBD_NOMEM;
1411                 goto bad2;
1412         }
1413         tail->xfer = NULL;
1414
1415         sed = opipe->sed;
1416         opipe->u.ctl.length = len;
1417
1418         /* Update device address and length since they may have changed. */
1419         /* XXX This only needs to be done once, but it's too early in open. */
1420         sed->ed.ed_flags = LE(
1421          (LE(sed->ed.ed_flags) & ~(OHCI_ED_ADDRMASK | OHCI_ED_MAXPMASK)) |
1422          OHCI_ED_SET_FA(addr) |
1423          OHCI_ED_SET_MAXP(UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize)));
1424
1425         /* Set up data transaction */
1426         if (len != 0) {
1427                 data = ohci_alloc_std(sc);
1428                 if (data == NULL) {
1429                         err = USBD_NOMEM;
1430                         goto bad3;
1431                 }
1432                 data->td.td_flags = LE(
1433                         (isread ? OHCI_TD_IN : OHCI_TD_OUT) | OHCI_TD_NOCC |
1434                         OHCI_TD_TOGGLE_1 | OHCI_TD_NOINTR |
1435                         (xfer->flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0));
1436                 data->td.td_cbp = LE(DMAADDR(&xfer->dmabuf, 0));
1437                 data->nexttd = stat;
1438                 data->td.td_nexttd = LE(stat->physaddr);
1439                 data->td.td_be = LE(LE(data->td.td_cbp) + len - 1);
1440                 data->len = len;
1441                 data->xfer = xfer;
1442                 data->flags = OHCI_ADD_LEN;
1443
1444                 next = data;
1445                 stat->flags = OHCI_CALL_DONE;
1446         } else {
1447                 next = stat;
1448                 /* XXX ADD_LEN? */
1449                 stat->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1450         }
1451
1452         memcpy(KERNADDR(&opipe->u.ctl.reqdma, 0), req, sizeof *req);
1453
1454         setup->td.td_flags = LE(OHCI_TD_SETUP | OHCI_TD_NOCC |
1455                                 OHCI_TD_TOGGLE_0 | OHCI_TD_NOINTR);
1456         setup->td.td_cbp = LE(DMAADDR(&opipe->u.ctl.reqdma, 0));
1457         setup->nexttd = next;
1458         setup->td.td_nexttd = LE(next->physaddr);
1459         setup->td.td_be = LE(LE(setup->td.td_cbp) + sizeof *req - 1);
1460         setup->len = 0;         /* XXX The number of byte we count */
1461         setup->xfer = xfer;
1462         setup->flags = 0;
1463         xfer->hcpriv = setup;
1464
1465         stat->td.td_flags = LE(
1466                 (isread ? OHCI_TD_OUT : OHCI_TD_IN) | OHCI_TD_NOCC |
1467                 OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1468         stat->td.td_cbp = 0;
1469         stat->nexttd = tail;
1470         stat->td.td_nexttd = LE(tail->physaddr);
1471         stat->td.td_be = 0;
1472         stat->len = 0;
1473         stat->xfer = xfer;
1474
1475 #ifdef USB_DEBUG
1476         if (ohcidebug > 5) {
1477                 DPRINTF(("ohci_device_request:\n"));
1478                 ohci_dump_ed(sed);
1479                 ohci_dump_tds(setup);
1480         }
1481 #endif
1482
1483         /* Insert ED in schedule */
1484         s = splusb();
1485         sed->ed.ed_tailp = LE(tail->physaddr);
1486         opipe->tail.td = tail;
1487         OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1488         if (xfer->timeout && !sc->sc_bus.use_polling) {
1489                 usb_timeout(ohci_timeout, xfer,
1490                             MS_TO_TICKS(xfer->timeout), xfer->timo_handle);
1491         }
1492         splx(s);
1493
1494 #ifdef USB_DEBUG
1495         if (ohcidebug > 25) {
1496                 usb_delay_ms(&sc->sc_bus, 5);
1497                 DPRINTF(("ohci_device_request: status=%x\n",
1498                          OREAD4(sc, OHCI_COMMAND_STATUS)));
1499                 ohci_dump_ed(sed);
1500                 ohci_dump_tds(setup);
1501         }
1502 #endif
1503
1504         return (USBD_NORMAL_COMPLETION);
1505
1506  bad3:
1507         ohci_free_std(sc, tail);
1508  bad2:
1509         ohci_free_std(sc, stat);
1510  bad1:
1511         return (err);
1512 }
1513
1514 /*
1515  * Add an ED to the schedule.  Called at splusb().
1516  */
1517 void
1518 ohci_add_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1519 {
1520         SPLUSBCHECK;
1521         sed->next = head->next;
1522         sed->ed.ed_nexted = head->ed.ed_nexted;
1523         head->next = sed;
1524         head->ed.ed_nexted = LE(sed->physaddr);
1525 }
1526
1527 /*
1528  * Remove an ED from the schedule.  Called at splusb().
1529  */
1530 void
1531 ohci_rem_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1532 {
1533         ohci_soft_ed_t *p; 
1534
1535         SPLUSBCHECK;
1536
1537         /* XXX */
1538         for (p = head; p == NULL && p->next != sed; p = p->next)
1539                 ;
1540         if (p == NULL)
1541                 panic("ohci_rem_ed: ED not found\n");
1542         p->next = sed->next;
1543         p->ed.ed_nexted = sed->ed.ed_nexted;
1544 }
1545
1546 /*
1547  * When a transfer is completed the TD is added to the done queue by
1548  * the host controller.  This queue is the processed by software.
1549  * Unfortunately the queue contains the physical address of the TD
1550  * and we have no simple way to translate this back to a kernel address.
1551  * To make the translation possible (and fast) we use a hash table of
1552  * TDs currently in the schedule.  The physical address is used as the
1553  * hash value.
1554  */
1555
1556 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
1557 /* Called at splusb() */
1558 void
1559 ohci_hash_add_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1560 {
1561         int h = HASH(std->physaddr);
1562
1563         SPLUSBCHECK;
1564
1565         LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
1566 }
1567
1568 /* Called at splusb() */
1569 void
1570 ohci_hash_rem_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1571 {
1572         SPLUSBCHECK;
1573
1574         LIST_REMOVE(std, hnext);
1575 }
1576
1577 ohci_soft_td_t *
1578 ohci_hash_find_td(ohci_softc_t *sc, ohci_physaddr_t a)
1579 {
1580         int h = HASH(a);
1581         ohci_soft_td_t *std;
1582
1583         /* if these are present they should be masked out at an earlier
1584          * stage.
1585          */
1586         KASSERT((a&~OHCI_TAILMASK) == 0, ("%s: 0x%b has lower bits set\n",
1587                                       USBDEVNAME(sc->sc_bus.bdev),
1588                                       (int) a, "\20\1HALT\2TOGGLE"));
1589
1590         for (std = LIST_FIRST(&sc->sc_hash_tds[h]); 
1591              std != NULL;
1592              std = LIST_NEXT(std, hnext))
1593                 if (std->physaddr == a)
1594                         return (std);
1595
1596         DPRINTF(("%s: ohci_hash_find_td: addr 0x%08lx not found\n",
1597                 USBDEVNAME(sc->sc_bus.bdev), (u_long) a));
1598         return NULL;
1599 }
1600
1601 void
1602 ohci_timeout(void *addr)
1603 {
1604         usbd_xfer_handle xfer = addr;
1605         int s;
1606
1607         DPRINTF(("ohci_timeout: xfer=%p\n", xfer));
1608
1609         s = splusb();
1610         xfer->device->bus->intr_context++;
1611         ohci_abort_xfer(xfer, USBD_TIMEOUT);
1612         xfer->device->bus->intr_context--;
1613         splx(s);
1614 }
1615
1616 #ifdef USB_DEBUG
1617 void
1618 ohci_dump_tds(ohci_soft_td_t *std)
1619 {
1620         for (; std; std = std->nexttd)
1621                 ohci_dump_td(std);
1622 }
1623
1624 void
1625 ohci_dump_td(ohci_soft_td_t *std)
1626 {
1627         DPRINTF(("TD(%p) at %08lx: %b delay=%d ec=%d cc=%d\ncbp=0x%08lx "
1628                  "nexttd=0x%08lx be=0x%08lx\n", 
1629                  std, (u_long)std->physaddr,
1630                  (int)LE(std->td.td_flags),
1631                  "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE",
1632                  OHCI_TD_GET_DI(LE(std->td.td_flags)),
1633                  OHCI_TD_GET_EC(LE(std->td.td_flags)),
1634                  OHCI_TD_GET_CC(LE(std->td.td_flags)),
1635                  (u_long)LE(std->td.td_cbp),
1636                  (u_long)LE(std->td.td_nexttd), (u_long)LE(std->td.td_be)));
1637 }
1638
1639 void
1640 ohci_dump_ed(ohci_soft_ed_t *sed)
1641 {
1642         DPRINTF(("ED(%p) at %08lx: addr=%d endpt=%d maxp=%d %b\n"
1643                  "tailp=0x%8b headp=0x%8b nexted=0x%08lx\n",
1644                  sed, (u_long)sed->physaddr, 
1645                  OHCI_ED_GET_FA(LE(sed->ed.ed_flags)),
1646                  OHCI_ED_GET_EN(LE(sed->ed.ed_flags)),
1647                  OHCI_ED_GET_MAXP(LE(sed->ed.ed_flags)),
1648                  (int)LE(sed->ed.ed_flags),
1649                  "\20\14OUT\15IN\16LOWSPEED\17SKIP\20ISO",
1650                  (int)(uintptr_t)LE(sed->ed.ed_tailp),
1651                  "\20\1BIT1\2BIT2",
1652                  (int)(uintptr_t)LE(sed->ed.ed_headp),
1653                  "\20\1HALT\2CARRY",
1654                  (u_long)LE(sed->ed.ed_nexted)));
1655 }
1656 #endif
1657
1658 usbd_status
1659 ohci_open(usbd_pipe_handle pipe)
1660 {
1661         usbd_device_handle dev = pipe->device;
1662         ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1663         usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1664         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
1665         u_int8_t addr = dev->address;
1666         u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
1667         ohci_soft_ed_t *sed;
1668         ohci_soft_td_t *std = NULL;
1669         ohci_soft_itd_t *sitd;
1670         ohci_physaddr_t tdphys;
1671         u_int32_t fmt;
1672         usbd_status err;
1673         int s;
1674         int ival;
1675
1676         DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1677                      pipe, addr, ed->bEndpointAddress, sc->sc_addr));
1678         if (addr == sc->sc_addr) {
1679                 switch (ed->bEndpointAddress) {
1680                 case USB_CONTROL_ENDPOINT:
1681                         pipe->methods = &ohci_root_ctrl_methods;
1682                         break;
1683                 case UE_DIR_IN | OHCI_INTR_ENDPT:
1684                         pipe->methods = &ohci_root_intr_methods;
1685                         break;
1686                 default:
1687                         return (USBD_INVAL);
1688                 }
1689         } else {
1690                 sed = ohci_alloc_sed(sc);
1691                 if (sed == NULL)
1692                         goto bad0;
1693                 opipe->sed = sed;
1694                 if (xfertype == UE_ISOCHRONOUS) {
1695                         sitd = ohci_alloc_sitd(sc);
1696                         if (sitd == NULL) {
1697                                 ohci_free_sitd(sc, sitd);
1698                                 goto bad1;
1699                         }
1700                         opipe->tail.itd = sitd;
1701                         tdphys = LE(sitd->physaddr);
1702                         fmt = OHCI_ED_FORMAT_ISO;
1703                 } else {
1704                         std = ohci_alloc_std(sc);
1705                         if (std == NULL) {
1706                                 ohci_free_std(sc, std);
1707                                 goto bad1;
1708                         }
1709                         opipe->tail.td = std;
1710                         tdphys = LE(std->physaddr);
1711                         fmt = OHCI_ED_FORMAT_GEN;
1712                 }
1713                 sed->ed.ed_flags = LE(
1714                         OHCI_ED_SET_FA(addr) | 
1715                         OHCI_ED_SET_EN(ed->bEndpointAddress) |
1716                         OHCI_ED_DIR_TD | 
1717                         (dev->lowspeed ? OHCI_ED_SPEED : 0) | fmt |
1718                         OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
1719                 sed->ed.ed_headp = sed->ed.ed_tailp = tdphys;
1720
1721                 switch (xfertype) {
1722                 case UE_CONTROL:
1723                         pipe->methods = &ohci_device_ctrl_methods;
1724                         err = usb_allocmem(&sc->sc_bus, 
1725                                   sizeof(usb_device_request_t), 
1726                                   0, &opipe->u.ctl.reqdma);
1727                         if (err)
1728                                 goto bad;
1729                         s = splusb();
1730                         ohci_add_ed(sed, sc->sc_ctrl_head);
1731                         splx(s);
1732                         break;
1733                 case UE_INTERRUPT:
1734                         pipe->methods = &ohci_device_intr_methods;
1735                         ival = pipe->interval;
1736                         if (ival == USBD_DEFAULT_INTERVAL)
1737                                 ival = ed->bInterval;
1738                         return (ohci_device_setintr(sc, opipe, ival));
1739                 case UE_ISOCHRONOUS:
1740                         pipe->methods = &ohci_device_isoc_methods;
1741                         return (ohci_setup_isoc(pipe));
1742                 case UE_BULK:
1743                         pipe->methods = &ohci_device_bulk_methods;
1744                         s = splusb();
1745                         ohci_add_ed(sed, sc->sc_bulk_head);
1746                         splx(s);
1747                         break;
1748                 }
1749         }
1750         return (USBD_NORMAL_COMPLETION);
1751
1752  bad:
1753         ohci_free_std(sc, std);
1754  bad1:
1755         ohci_free_sed(sc, sed);
1756  bad0:
1757         return (USBD_NOMEM);
1758         
1759 }
1760
1761 /*
1762  * Close a reqular pipe.
1763  * Assumes that there are no pending transactions.
1764  */
1765 void
1766 ohci_close_pipe(usbd_pipe_handle pipe, ohci_soft_ed_t *head)
1767 {
1768         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
1769         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
1770         ohci_soft_ed_t *sed = opipe->sed;
1771         int s;
1772
1773         s = splusb();
1774 #ifdef DIAGNOSTIC
1775         sed->ed.ed_flags |= LE(OHCI_ED_SKIP);
1776         if ((sed->ed.ed_tailp & LE(OHCI_TAILMASK))
1777             != (sed->ed.ed_headp & LE(OHCI_HEADMASK))) {
1778                 ohci_physaddr_t td = sed->ed.ed_headp;
1779                 ohci_soft_td_t *std;
1780                 for (std = LIST_FIRST(&sc->sc_hash_tds[HASH(td)]); 
1781                      std != NULL;
1782                      std = LIST_NEXT(std, hnext))
1783                     if (std->physaddr == td)
1784                         break;
1785                 printf("ohci_close_pipe: pipe not empty sed=%p hd=0x%x "
1786                        "tl=0x%x pipe=%p, std=%p\n", sed,
1787                        (int)LE(sed->ed.ed_headp), (int)LE(sed->ed.ed_tailp),
1788                        pipe, std);
1789                 usb_delay_ms(&sc->sc_bus, 2);
1790                 if ((sed->ed.ed_tailp & LE(OHCI_TAILMASK))
1791                     != (sed->ed.ed_headp & LE(OHCI_HEADMASK)))
1792                         printf("ohci_close_pipe: pipe still not empty\n");
1793         }
1794 #endif
1795         ohci_rem_ed(sed, head);
1796         splx(s);
1797         ohci_free_sed(sc, opipe->sed);
1798 }
1799
1800 /* 
1801  * Abort a device request.
1802  * If this routine is called at splusb() it guarantees that the request
1803  * will be removed from the hardware scheduling and that the callback
1804  * for it will be called with USBD_CANCELLED status.
1805  * It's impossible to guarantee that the requested transfer will not
1806  * have happened since the hardware runs concurrently.
1807  * If the transaction has already happened we rely on the ordinary
1808  * interrupt processing to process it.
1809  */
1810 void
1811 ohci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1812 {
1813         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1814         ohci_soft_ed_t *sed;
1815
1816         DPRINTF(("ohci_abort_xfer: xfer=%p pipe=%p\n", xfer, opipe));
1817
1818         xfer->status = status;
1819
1820         usb_untimeout(ohci_timeout, xfer, xfer->timo_handle);
1821
1822         sed = opipe->sed;
1823         sed->ed.ed_flags |= LE(OHCI_ED_SKIP); /* force hardware skip */
1824 #ifdef USB_DEBUG
1825         DPRINTFN(1,("ohci_abort_xfer: stop ed=%p\n", sed));
1826         ohci_dump_ed(sed);
1827 #endif
1828
1829 #if 1
1830         if (xfer->device->bus->intr_context) {
1831                 /* We have no process context, so we can't use tsleep(). */
1832                 timeout(ohci_abort_xfer_end, xfer, hz / USB_FRAMES_PER_SECOND);
1833         } else {
1834 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
1835                 KASSERT(intr_nesting_level == 0,
1836                         ("ohci_abort_req in interrupt context"));
1837 #endif
1838                 usb_delay_ms(opipe->pipe.device->bus, 1);
1839                 ohci_abort_xfer_end(xfer);
1840         }
1841 #else
1842         delay(1000);
1843         ohci_abort_xfer_end(xfer);
1844 #endif
1845 }
1846
1847 void
1848 ohci_abort_xfer_end(void *v)
1849 {
1850         usbd_xfer_handle xfer = v;
1851         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1852         ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1853         ohci_soft_ed_t *sed;
1854         ohci_soft_td_t *p, *n;
1855         int s;
1856
1857         s = splusb();
1858
1859         p = xfer->hcpriv;
1860 #ifdef DIAGNOSTIC
1861         if (p == NULL) {
1862                 printf("ohci_abort_xfer: hcpriv==0\n");
1863                 splx(s);
1864                 return;
1865         }
1866 #endif
1867         for (; p->xfer == xfer; p = n) {
1868                 n = p->nexttd;
1869                 ohci_free_std(sc, p);
1870         }
1871
1872         sed = opipe->sed;
1873         DPRINTFN(2,("ohci_abort_xfer: set hd=%x, tl=%x\n",
1874                     (int)LE(p->physaddr), (int)LE(sed->ed.ed_tailp)));
1875         sed->ed.ed_headp = p->physaddr; /* unlink TDs */
1876         sed->ed.ed_flags &= LE(~OHCI_ED_SKIP); /* remove hardware skip */
1877
1878         usb_transfer_complete(xfer);
1879
1880         splx(s);
1881 }
1882
1883 /*
1884  * Data structures and routines to emulate the root hub.
1885  */
1886 Static usb_device_descriptor_t ohci_devd = {
1887         USB_DEVICE_DESCRIPTOR_SIZE,
1888         UDESC_DEVICE,           /* type */
1889         {0x00, 0x01},           /* USB version */
1890         UDCLASS_HUB,            /* class */
1891         UDSUBCLASS_HUB,         /* subclass */
1892         UDPROTO_FSHUB,          /* protocol */
1893         64,                     /* max packet */
1894         {0},{0},{0x00,0x01},    /* device id */
1895         1,2,0,                  /* string indicies */
1896         1                       /* # of configurations */
1897 };
1898
1899 Static usb_config_descriptor_t ohci_confd = {
1900         USB_CONFIG_DESCRIPTOR_SIZE,
1901         UDESC_CONFIG,
1902         {USB_CONFIG_DESCRIPTOR_SIZE +
1903          USB_INTERFACE_DESCRIPTOR_SIZE +
1904          USB_ENDPOINT_DESCRIPTOR_SIZE},
1905         1,
1906         1,
1907         0,
1908         UC_SELF_POWERED,
1909         0                       /* max power */
1910 };
1911
1912 Static usb_interface_descriptor_t ohci_ifcd = {
1913         USB_INTERFACE_DESCRIPTOR_SIZE,
1914         UDESC_INTERFACE,
1915         0,
1916         0,
1917         1,
1918         UICLASS_HUB,
1919         UISUBCLASS_HUB,
1920         UIPROTO_FSHUB,
1921         0
1922 };
1923
1924 Static usb_endpoint_descriptor_t ohci_endpd = {
1925         USB_ENDPOINT_DESCRIPTOR_SIZE,
1926         UDESC_ENDPOINT,
1927         UE_DIR_IN | OHCI_INTR_ENDPT,
1928         UE_INTERRUPT,
1929         {8, 0},                 /* max packet */
1930         255
1931 };
1932
1933 Static usb_hub_descriptor_t ohci_hubd = {
1934         USB_HUB_DESCRIPTOR_SIZE,
1935         UDESC_HUB,
1936         0,
1937         {0,0},
1938         0,
1939         0,
1940         {0},
1941 };
1942
1943 Static int
1944 ohci_str(usb_string_descriptor_t *p, int l, char *s)
1945 {
1946         int i;
1947
1948         if (l == 0)
1949                 return (0);
1950         p->bLength = 2 * strlen(s) + 2;
1951         if (l == 1)
1952                 return (1);
1953         p->bDescriptorType = UDESC_STRING;
1954         l -= 2;
1955         for (i = 0; s[i] && l > 1; i++, l -= 2)
1956                 USETW2(p->bString[i], 0, s[i]);
1957         return (2*i+2);
1958 }
1959
1960 /*
1961  * Simulate a hardware hub by handling all the necessary requests.
1962  */
1963 Static usbd_status
1964 ohci_root_ctrl_transfer(usbd_xfer_handle xfer)
1965 {
1966         usbd_status err;
1967
1968         /* Insert last in queue. */
1969         err = usb_insert_transfer(xfer);
1970         if (err)
1971                 return (err);
1972
1973         /* Pipe isn't running, start first */
1974         return (ohci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1975 }
1976
1977 Static usbd_status
1978 ohci_root_ctrl_start(usbd_xfer_handle xfer)
1979 {
1980         ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
1981         usb_device_request_t *req;
1982         void *buf = NULL;
1983         int port, i;
1984         int s, len, value, index, l, totlen = 0;
1985         usb_port_status_t ps;
1986         usb_hub_descriptor_t hubd;
1987         usbd_status err;
1988         u_int32_t v;
1989
1990 #ifdef DIAGNOSTIC
1991         if (!(xfer->rqflags & URQ_REQUEST))
1992                 /* XXX panic */
1993                 return (USBD_INVAL);
1994 #endif
1995         req = &xfer->request;
1996
1997         DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n", 
1998                     req->bmRequestType, req->bRequest));
1999
2000         len = UGETW(req->wLength);
2001         value = UGETW(req->wValue);
2002         index = UGETW(req->wIndex);
2003
2004         if (len != 0)
2005                 buf = KERNADDR(&xfer->dmabuf, 0);
2006
2007 #define C(x,y) ((x) | ((y) << 8))
2008         switch(C(req->bRequest, req->bmRequestType)) {
2009         case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2010         case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2011         case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2012                 /* 
2013                  * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2014                  * for the integrated root hub.
2015                  */
2016                 break;
2017         case C(UR_GET_CONFIG, UT_READ_DEVICE):
2018                 if (len > 0) {
2019                         *(u_int8_t *)buf = sc->sc_conf;
2020                         totlen = 1;
2021                 }
2022                 break;
2023         case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2024                 DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value));
2025                 switch(value >> 8) {
2026                 case UDESC_DEVICE:
2027                         if ((value & 0xff) != 0) {
2028                                 err = USBD_IOERROR;
2029                                 goto ret;
2030                         }
2031                         totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2032                         USETW(ohci_devd.idVendor, sc->sc_id_vendor);
2033                         memcpy(buf, &ohci_devd, l);
2034                         break;
2035                 case UDESC_CONFIG:
2036                         if ((value & 0xff) != 0) {
2037                                 err = USBD_IOERROR;
2038                                 goto ret;
2039                         }
2040                         totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2041                         memcpy(buf, &ohci_confd, l);
2042                         buf = (char *)buf + l;
2043                         len -= l;
2044                         l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2045                         totlen += l;
2046                         memcpy(buf, &ohci_ifcd, l);
2047                         buf = (char *)buf + l;
2048                         len -= l;
2049                         l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2050                         totlen += l;
2051                         memcpy(buf, &ohci_endpd, l);
2052                         break;
2053                 case UDESC_STRING:
2054                         if (len == 0)
2055                                 break;
2056                         *(u_int8_t *)buf = 0;
2057                         totlen = 1;
2058                         switch (value & 0xff) {
2059                         case 1: /* Vendor */
2060                                 totlen = ohci_str(buf, len, sc->sc_vendor);
2061                                 break;
2062                         case 2: /* Product */
2063                                 totlen = ohci_str(buf, len, "OHCI root hub");
2064                                 break;
2065                         }
2066                         break;
2067                 default:
2068                         err = USBD_IOERROR;
2069                         goto ret;
2070                 }
2071                 break;
2072         case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2073                 if (len > 0) {
2074                         *(u_int8_t *)buf = 0;
2075                         totlen = 1;
2076                 }
2077                 break;
2078         case C(UR_GET_STATUS, UT_READ_DEVICE):
2079                 if (len > 1) {
2080                         USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2081                         totlen = 2;
2082                 }
2083                 break;
2084         case C(UR_GET_STATUS, UT_READ_INTERFACE):
2085         case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2086                 if (len > 1) {
2087                         USETW(((usb_status_t *)buf)->wStatus, 0);
2088                         totlen = 2;
2089                 }
2090                 break;
2091         case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2092                 if (value >= USB_MAX_DEVICES) {
2093                         err = USBD_IOERROR;
2094                         goto ret;
2095                 }
2096                 sc->sc_addr = value;
2097                 break;
2098         case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2099                 if (value != 0 && value != 1) {
2100                         err = USBD_IOERROR;
2101                         goto ret;
2102                 }
2103                 sc->sc_conf = value;
2104                 break;
2105         case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2106                 break;
2107         case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2108         case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2109         case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2110                 err = USBD_IOERROR;
2111                 goto ret;
2112         case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2113                 break;
2114         case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2115                 break;
2116         /* Hub requests */
2117         case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2118                 break;
2119         case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2120                 DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2121                              "port=%d feature=%d\n",
2122                              index, value));
2123                 if (index < 1 || index > sc->sc_noport) {
2124                         err = USBD_IOERROR;
2125                         goto ret;
2126                 }
2127                 port = OHCI_RH_PORT_STATUS(index);
2128                 switch(value) {
2129                 case UHF_PORT_ENABLE:
2130                         OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
2131                         break;
2132                 case UHF_PORT_SUSPEND:
2133                         OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
2134                         break;
2135                 case UHF_PORT_POWER:
2136                         OWRITE4(sc, port, UPS_LOW_SPEED);
2137                         break;
2138                 case UHF_C_PORT_CONNECTION:
2139                         OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
2140                         break;
2141                 case UHF_C_PORT_ENABLE:
2142                         OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
2143                         break;
2144                 case UHF_C_PORT_SUSPEND:
2145                         OWRITE4(sc, port, UPS_C_SUSPEND << 16);
2146                         break;
2147                 case UHF_C_PORT_OVER_CURRENT:
2148                         OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
2149                         break;
2150                 case UHF_C_PORT_RESET:
2151                         OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
2152                         break;
2153                 default:
2154                         err = USBD_IOERROR;
2155                         goto ret;
2156                 }
2157                 switch(value) {
2158                 case UHF_C_PORT_CONNECTION:
2159                 case UHF_C_PORT_ENABLE:
2160                 case UHF_C_PORT_SUSPEND:
2161                 case UHF_C_PORT_OVER_CURRENT:
2162                 case UHF_C_PORT_RESET:
2163                         /* Enable RHSC interrupt if condition is cleared. */
2164                         if ((OREAD4(sc, port) >> 16) == 0)
2165                                 ohci_rhsc_able(sc, 1);
2166                         break;
2167                 default:
2168                         break;
2169                 }
2170                 break;
2171         case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2172                 if (value != 0) {
2173                         err = USBD_IOERROR;
2174                         goto ret;
2175                 }
2176                 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
2177                 hubd = ohci_hubd;
2178                 hubd.bNbrPorts = sc->sc_noport;
2179                 USETW(hubd.wHubCharacteristics,
2180                       (v & OHCI_NPS ? UHD_PWR_NO_SWITCH : 
2181                        v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
2182                       /* XXX overcurrent */
2183                       );
2184                 hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
2185                 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
2186                 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8) 
2187                         hubd.DeviceRemovable[i++] = (u_int8_t)v;
2188                 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2189                 l = min(len, hubd.bDescLength);
2190                 totlen = l;
2191                 memcpy(buf, &hubd, l);
2192                 break;
2193         case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2194                 if (len != 4) {
2195                         err = USBD_IOERROR;
2196                         goto ret;
2197                 }
2198                 memset(buf, 0, len); /* ? XXX */
2199                 totlen = len;
2200                 break;
2201         case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2202                 DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n",
2203                             index));
2204                 if (index < 1 || index > sc->sc_noport) {
2205                         err = USBD_IOERROR;
2206                         goto ret;
2207                 }
2208                 if (len != 4) {
2209                         err = USBD_IOERROR;
2210                         goto ret;
2211                 }
2212                 v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
2213                 DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n",
2214                             v));
2215                 USETW(ps.wPortStatus, v);
2216                 USETW(ps.wPortChange, v >> 16);
2217                 l = min(len, sizeof ps);
2218                 memcpy(buf, &ps, l);
2219                 totlen = l;
2220                 break;
2221         case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2222                 err = USBD_IOERROR;
2223                 goto ret;
2224         case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2225                 break;
2226         case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2227                 if (index < 1 || index > sc->sc_noport) {
2228                         err = USBD_IOERROR;
2229                         goto ret;
2230                 }
2231                 port = OHCI_RH_PORT_STATUS(index);
2232                 switch(value) {
2233                 case UHF_PORT_ENABLE:
2234                         OWRITE4(sc, port, UPS_PORT_ENABLED);
2235                         break;
2236                 case UHF_PORT_SUSPEND:
2237                         OWRITE4(sc, port, UPS_SUSPEND);
2238                         break;
2239                 case UHF_PORT_RESET:
2240                         DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n",
2241                                     index));
2242                         OWRITE4(sc, port, UPS_RESET);
2243                         for (i = 0; i < 10; i++) {
2244                                 usb_delay_ms(&sc->sc_bus, 10);
2245                                 if ((OREAD4(sc, port) & UPS_RESET) == 0)
2246                                         break;
2247                         }
2248                         DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n",
2249                                     index, OREAD4(sc, port)));
2250                         break;
2251                 case UHF_PORT_POWER:
2252                         DPRINTFN(2,("ohci_root_ctrl_transfer: set port power "
2253                                     "%d\n", index));
2254                         OWRITE4(sc, port, UPS_PORT_POWER);
2255                         break;
2256                 default:
2257                         err = USBD_IOERROR;
2258                         goto ret;
2259                 }
2260                 break;
2261         default:
2262                 err = USBD_IOERROR;
2263                 goto ret;
2264         }
2265         xfer->actlen = totlen;
2266         err = USBD_NORMAL_COMPLETION;
2267  ret:
2268         xfer->status = err;
2269         s = splusb();
2270         usb_transfer_complete(xfer);
2271         splx(s);
2272         return (USBD_IN_PROGRESS);
2273 }
2274
2275 /* Abort a root control request. */
2276 Static void
2277 ohci_root_ctrl_abort(usbd_xfer_handle xfer)
2278 {
2279         /* Nothing to do, all transfers are synchronous. */
2280 }
2281
2282 /* Close the root pipe. */
2283 Static void
2284 ohci_root_ctrl_close(usbd_pipe_handle pipe)
2285 {
2286         DPRINTF(("ohci_root_ctrl_close\n"));
2287         /* Nothing to do. */
2288 }
2289
2290 Static usbd_status
2291 ohci_root_intr_transfer(usbd_xfer_handle xfer)
2292 {
2293         usbd_status err;
2294
2295         /* Insert last in queue. */
2296         err = usb_insert_transfer(xfer);
2297         if (err)
2298                 return (err);
2299
2300         /* Pipe isn't running, start first */
2301         return (ohci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2302 }
2303
2304 Static usbd_status
2305 ohci_root_intr_start(usbd_xfer_handle xfer)
2306 {
2307         usbd_pipe_handle pipe = xfer->pipe;
2308         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2309
2310         sc->sc_intrxfer = xfer;
2311
2312         return (USBD_IN_PROGRESS);
2313 }
2314
2315 /* Abort a root interrupt request. */
2316 Static void
2317 ohci_root_intr_abort(usbd_xfer_handle xfer)
2318 {
2319         int s;
2320
2321         if (xfer->pipe->intrxfer == xfer) {
2322                 DPRINTF(("ohci_root_intr_abort: remove\n"));
2323                 xfer->pipe->intrxfer = NULL;
2324         }
2325         xfer->status = USBD_CANCELLED;
2326         s = splusb();
2327         usb_transfer_complete(xfer);
2328         splx(s);
2329 }
2330
2331 /* Close the root pipe. */
2332 Static void
2333 ohci_root_intr_close(usbd_pipe_handle pipe)
2334 {
2335         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2336         
2337         DPRINTF(("ohci_root_intr_close\n"));
2338
2339         sc->sc_intrxfer = NULL;
2340 }
2341
2342 /************************/
2343
2344 Static usbd_status
2345 ohci_device_ctrl_transfer(usbd_xfer_handle xfer)
2346 {
2347         usbd_status err;
2348
2349         /* Insert last in queue. */
2350         err = usb_insert_transfer(xfer);
2351         if (err)
2352                 return (err);
2353
2354         /* Pipe isn't running, start first */
2355         return (ohci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2356 }
2357
2358 Static usbd_status
2359 ohci_device_ctrl_start(usbd_xfer_handle xfer)
2360 {
2361         ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2362         usbd_status err;
2363
2364 #ifdef DIAGNOSTIC
2365         if (!(xfer->rqflags & URQ_REQUEST)) {
2366                 /* XXX panic */
2367                 printf("ohci_device_ctrl_transfer: not a request\n");
2368                 return (USBD_INVAL);
2369         }
2370 #endif
2371
2372         err = ohci_device_request(xfer);
2373         if (err)
2374                 return (err);
2375
2376         if (sc->sc_bus.use_polling)
2377                 ohci_waitintr(sc, xfer);
2378         return (USBD_IN_PROGRESS);
2379 }
2380
2381 /* Abort a device control request. */
2382 Static void
2383 ohci_device_ctrl_abort(usbd_xfer_handle xfer)
2384 {
2385         DPRINTF(("ohci_device_ctrl_abort: xfer=%p\n", xfer));
2386         ohci_abort_xfer(xfer, USBD_CANCELLED);
2387 }
2388
2389 /* Close a device control pipe. */
2390 Static void
2391 ohci_device_ctrl_close(usbd_pipe_handle pipe)
2392 {
2393         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2394         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2395
2396         DPRINTF(("ohci_device_ctrl_close: pipe=%p\n", pipe));
2397         ohci_close_pipe(pipe, sc->sc_ctrl_head);
2398         ohci_free_std(sc, opipe->tail.td);
2399 }
2400
2401 /************************/
2402
2403 Static void
2404 ohci_device_clear_toggle(usbd_pipe_handle pipe)
2405 {
2406         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2407
2408         opipe->sed->ed.ed_headp &= LE(~OHCI_TOGGLECARRY);
2409 }
2410
2411 Static void
2412 ohci_noop(usbd_pipe_handle pipe)
2413 {
2414 }
2415
2416 Static usbd_status
2417 ohci_device_bulk_transfer(usbd_xfer_handle xfer)
2418 {
2419         usbd_status err;
2420
2421         /* Insert last in queue. */
2422         err = usb_insert_transfer(xfer);
2423         if (err)
2424                 return (err);
2425
2426         /* Pipe isn't running, start first */
2427         return (ohci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2428 }
2429
2430 Static usbd_status
2431 ohci_device_bulk_start(usbd_xfer_handle xfer)
2432 {
2433         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2434         usbd_device_handle dev = opipe->pipe.device;
2435         ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2436         int addr = dev->address;
2437         ohci_soft_td_t *data, *tail, *tdp;
2438         ohci_soft_ed_t *sed;
2439         int s, len, isread, endpt;
2440         usbd_status err;
2441
2442 #ifdef DIAGNOSTIC
2443         if (xfer->rqflags & URQ_REQUEST) {
2444                 /* XXX panic */
2445                 printf("ohci_device_bulk_start: a request\n");
2446                 return (USBD_INVAL);
2447         }
2448 #endif
2449
2450         len = xfer->length;
2451         endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2452         isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2453         sed = opipe->sed;
2454
2455         DPRINTFN(4,("ohci_device_bulk_start: xfer=%p len=%d isread=%d "
2456                     "flags=%d endpt=%d\n", xfer, len, isread, xfer->flags,
2457                     endpt));
2458
2459         opipe->u.bulk.isread = isread;
2460         opipe->u.bulk.length = len;
2461
2462         /* Update device address */
2463         sed->ed.ed_flags = LE(
2464                 (LE(sed->ed.ed_flags) & ~OHCI_ED_ADDRMASK) |
2465                 OHCI_ED_SET_FA(addr));
2466
2467         /* Allocate a chain of new TDs (including a new tail). */
2468         data = opipe->tail.td;
2469         err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer->flags,
2470                   &xfer->dmabuf, data, &tail);
2471         if (err)
2472                 return (err);
2473
2474         tail->xfer = NULL;
2475         xfer->hcpriv = data;
2476
2477         DPRINTFN(4,("ohci_device_bulk_start: ed_flags=0x%08x td_flags=0x%08x "
2478                     "td_cbp=0x%08x td_be=0x%08x\n",
2479                     (int)LE(sed->ed.ed_flags), (int)LE(data->td.td_flags),
2480                     (int)LE(data->td.td_cbp), (int)LE(data->td.td_be)));
2481
2482 #ifdef USB_DEBUG
2483         if (ohcidebug > 4) {
2484                 ohci_dump_ed(sed);
2485                 ohci_dump_tds(data);
2486         }
2487 #endif
2488
2489         /* Insert ED in schedule */
2490         s = splusb();
2491         for (tdp = data; tdp != tail; tdp = tdp->nexttd) {
2492                 tdp->xfer = xfer;
2493                 ohci_hash_add_td(sc, tdp);
2494         }
2495         sed->ed.ed_tailp = LE(tail->physaddr);
2496         opipe->tail.td = tail;
2497         sed->ed.ed_flags &= LE(~OHCI_ED_SKIP);
2498         OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
2499         if (xfer->timeout && !sc->sc_bus.use_polling) {
2500                 usb_timeout(ohci_timeout, xfer,
2501                             MS_TO_TICKS(xfer->timeout), xfer->timo_handle);
2502         }
2503
2504 #if 0
2505 /* This goes wrong if we are too slow. */
2506         if (ohcidebug > 5) {
2507                 usb_delay_ms(&sc->sc_bus, 5);
2508                 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2509                          OREAD4(sc, OHCI_COMMAND_STATUS)));
2510                 ohci_dump_ed(sed);
2511                 ohci_dump_tds(data);
2512         }
2513 #endif
2514
2515         splx(s);
2516
2517         return (USBD_IN_PROGRESS);
2518 }
2519
2520 Static void
2521 ohci_device_bulk_abort(usbd_xfer_handle xfer)
2522 {
2523         DPRINTF(("ohci_device_bulk_abort: xfer=%p\n", xfer));
2524         ohci_abort_xfer(xfer, USBD_CANCELLED);
2525 }
2526
2527 /* 
2528  * Close a device bulk pipe.
2529  */
2530 Static void
2531 ohci_device_bulk_close(usbd_pipe_handle pipe)
2532 {
2533         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2534         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2535
2536         DPRINTF(("ohci_device_bulk_close: pipe=%p\n", pipe));
2537         ohci_close_pipe(pipe, sc->sc_bulk_head);
2538         ohci_free_std(sc, opipe->tail.td);
2539 }
2540
2541 /************************/
2542
2543 Static usbd_status
2544 ohci_device_intr_transfer(usbd_xfer_handle xfer)
2545 {
2546         usbd_status err;
2547
2548         /* Insert last in queue. */
2549         err = usb_insert_transfer(xfer);
2550         if (err)
2551                 return (err);
2552
2553         /* Pipe isn't running, start first */
2554         return (ohci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2555 }
2556
2557 Static usbd_status
2558 ohci_device_intr_start(usbd_xfer_handle xfer)
2559 {
2560         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2561         usbd_device_handle dev = opipe->pipe.device;
2562         ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2563         ohci_soft_ed_t *sed = opipe->sed;
2564         ohci_soft_td_t *data, *tail;
2565         int len;
2566         int s;
2567
2568         DPRINTFN(3, ("ohci_device_intr_transfer: xfer=%p len=%d "
2569                      "flags=%d priv=%p\n",
2570                      xfer, xfer->length, xfer->flags, xfer->priv));
2571
2572 #ifdef DIAGNOSTIC
2573         if (xfer->rqflags & URQ_REQUEST)
2574                 panic("ohci_device_intr_transfer: a request\n");
2575 #endif
2576
2577         len = xfer->length;
2578
2579         data = opipe->tail.td;
2580         tail = ohci_alloc_std(sc);
2581         if (tail == NULL)
2582                 return (USBD_NOMEM);
2583         tail->xfer = NULL;
2584
2585         data->td.td_flags = LE(
2586                 OHCI_TD_IN | OHCI_TD_NOCC | 
2587                 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
2588         if (xfer->flags & USBD_SHORT_XFER_OK)
2589                 data->td.td_flags |= LE(OHCI_TD_R);
2590         data->td.td_cbp = LE(DMAADDR(&xfer->dmabuf, 0));
2591         data->nexttd = tail;
2592         data->td.td_nexttd = LE(tail->physaddr);
2593         data->td.td_be = LE(LE(data->td.td_cbp) + len - 1);
2594         data->len = len;
2595         data->xfer = xfer;
2596         data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
2597         xfer->hcpriv = data;
2598
2599 #ifdef USB_DEBUG
2600         if (ohcidebug > 5) {
2601                 DPRINTF(("ohci_device_intr_transfer:\n"));
2602                 ohci_dump_ed(sed);
2603                 ohci_dump_tds(data);
2604         }
2605 #endif
2606
2607         /* Insert ED in schedule */
2608         s = splusb();
2609         ohci_hash_add_td(sc, data);
2610         sed->ed.ed_tailp = LE(tail->physaddr);
2611         opipe->tail.td = tail;
2612         sed->ed.ed_flags &= LE(~OHCI_ED_SKIP);
2613
2614 #if 0
2615 /*
2616  * This goes horribly wrong, printing thousands of descriptors,
2617  * because false references are followed due to the fact that the
2618  * TD is gone.
2619  */
2620         if (ohcidebug > 5) {
2621                 usb_delay_ms(&sc->sc_bus, 5);
2622                 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2623                          OREAD4(sc, OHCI_COMMAND_STATUS)));
2624                 ohci_dump_ed(sed);
2625                 ohci_dump_tds(data);
2626         }
2627 #endif
2628         splx(s);
2629
2630         return (USBD_IN_PROGRESS);
2631 }
2632
2633 /* Abort a device control request. */
2634 Static void
2635 ohci_device_intr_abort(usbd_xfer_handle xfer)
2636 {
2637         if (xfer->pipe->intrxfer == xfer) {
2638                 DPRINTF(("ohci_device_intr_abort: remove\n"));
2639                 xfer->pipe->intrxfer = NULL;
2640         }
2641         ohci_abort_xfer(xfer, USBD_CANCELLED);
2642 }
2643
2644 /* Close a device interrupt pipe. */
2645 Static void
2646 ohci_device_intr_close(usbd_pipe_handle pipe)
2647 {
2648         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2649         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2650         int nslots = opipe->u.intr.nslots;
2651         int pos = opipe->u.intr.pos;
2652         int j;
2653         ohci_soft_ed_t *p, *sed = opipe->sed;
2654         int s;
2655
2656         DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n",
2657                     pipe, nslots, pos));
2658         s = splusb();
2659         sed->ed.ed_flags |= LE(OHCI_ED_SKIP);
2660         if ((sed->ed.ed_tailp & LE(OHCI_TAILMASK))
2661             != (sed->ed.ed_headp & LE(OHCI_HEADMASK)))
2662                 usb_delay_ms(&sc->sc_bus, 2);
2663 #ifdef DIAGNOSTIC
2664         if ((sed->ed.ed_tailp & LE(OHCI_TAILMASK))
2665             != (sed->ed.ed_headp & LE(OHCI_HEADMASK)))
2666                 panic("%s: Intr pipe %p still has TDs queued\n",
2667                         USBDEVNAME(sc->sc_bus.bdev), pipe);
2668 #endif
2669
2670         for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
2671                 ;
2672 #ifdef DIAGNOSTIC
2673         if (p == NULL)
2674                 panic("ohci_device_intr_close: ED not found\n");
2675 #endif
2676         p->next = sed->next;
2677         p->ed.ed_nexted = sed->ed.ed_nexted;
2678         splx(s);
2679
2680         for (j = 0; j < nslots; j++)
2681                 --sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS];
2682
2683         ohci_free_std(sc, opipe->tail.td);
2684         ohci_free_sed(sc, opipe->sed);
2685 }
2686
2687 Static usbd_status
2688 ohci_device_setintr(ohci_softc_t *sc, struct ohci_pipe *opipe, int ival)
2689 {
2690         int i, j, s, best;
2691         u_int npoll, slow, shigh, nslots;
2692         u_int bestbw, bw;
2693         ohci_soft_ed_t *hsed, *sed = opipe->sed;
2694
2695         DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe));
2696         if (ival == 0) {
2697                 printf("ohci_setintr: 0 interval\n");
2698                 return (USBD_INVAL);
2699         }
2700
2701         npoll = OHCI_NO_INTRS;
2702         while (npoll > ival)
2703                 npoll /= 2;
2704         DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll));
2705
2706         /*
2707          * We now know which level in the tree the ED must go into.
2708          * Figure out which slot has most bandwidth left over.
2709          * Slots to examine:
2710          * npoll
2711          * 1    0
2712          * 2    1 2
2713          * 4    3 4 5 6
2714          * 8    7 8 9 10 11 12 13 14
2715          * N    (N-1) .. (N-1+N-1)
2716          */
2717         slow = npoll-1;
2718         shigh = slow + npoll;
2719         nslots = OHCI_NO_INTRS / npoll;
2720         for (best = i = slow, bestbw = ~0; i < shigh; i++) {
2721                 bw = 0;
2722                 for (j = 0; j < nslots; j++)
2723                         bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
2724                 if (bw < bestbw) {
2725                         best = i;
2726                         bestbw = bw;
2727                 }
2728         }
2729         DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n", 
2730                      best, slow, shigh, bestbw));
2731
2732         s = splusb();
2733         hsed = sc->sc_eds[best];
2734         sed->next = hsed->next;
2735         sed->ed.ed_nexted = hsed->ed.ed_nexted;
2736         hsed->next = sed;
2737         hsed->ed.ed_nexted = LE(sed->physaddr);
2738         splx(s);
2739
2740         for (j = 0; j < nslots; j++)
2741                 ++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
2742         opipe->u.intr.nslots = nslots;
2743         opipe->u.intr.pos = best;
2744
2745         DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe));
2746         return (USBD_NORMAL_COMPLETION);
2747 }
2748
2749 /***********************/
2750
2751 usbd_status
2752 ohci_device_isoc_transfer(usbd_xfer_handle xfer)
2753 {
2754         usbd_status err;
2755
2756         DPRINTFN(5,("ohci_device_isoc_transfer: xfer=%p\n", xfer));
2757
2758         /* Put it on our queue, */
2759         err = usb_insert_transfer(xfer);
2760
2761         /* bail out on error, */
2762         if (err && err != USBD_IN_PROGRESS)
2763                 return (err);
2764
2765         /* XXX should check inuse here */
2766
2767         /* insert into schedule, */
2768         ohci_device_isoc_enter(xfer);
2769
2770         /* and put on interrupt list if the pipe wasn't running */
2771         if (!err)
2772                 ohci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2773
2774         return (err);
2775 }
2776
2777 void
2778 ohci_device_isoc_enter(usbd_xfer_handle xfer)
2779 {
2780         struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2781         usbd_device_handle dev = opipe->pipe.device;
2782         ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2783         ohci_soft_ed_t *sed = opipe->sed;
2784         struct iso *iso = &opipe->u.iso;
2785         ohci_soft_itd_t *sitd, *nsitd;  
2786         ohci_physaddr_t buf, offs;
2787         int i, ncur, nframes;
2788         int ncross = 0;
2789         int s;
2790
2791         s = splusb();
2792         sitd = opipe->tail.itd;
2793         buf = DMAADDR(&xfer->dmabuf, 0);
2794         sitd->itd.itd_bp0 = LE(buf & OHCI_ITD_PAGE_MASK);
2795         nframes = xfer->nframes;
2796         offs = buf & OHCI_ITD_OFFSET_MASK;
2797         for (i = ncur = 0; i < nframes; i++, ncur++) {
2798                 if (ncur == OHCI_ITD_NOFFSET || /* all offsets used */
2799                     ncross > 1) {       /* too many page crossings */
2800                         
2801                         nsitd = ohci_alloc_sitd(sc);
2802                         if (nsitd == NULL) {
2803                                 /* XXX what now? */
2804                                 splx(s);
2805                                 return;
2806                         }
2807                         sitd->nextitd = nsitd;
2808                         sitd->itd.itd_nextitd = LE(nsitd->physaddr);
2809                         sitd->itd.itd_flags = LE(
2810                                 OHCI_ITD_NOCC | 
2811                                 OHCI_ITD_SET_SF(iso->next) |
2812                                 OHCI_ITD_NOINTR |
2813                                 OHCI_ITD_SET_FC(OHCI_ITD_NOFFSET));
2814                         sitd->itd.itd_be = LE(LE(sitd->itd.itd_bp0) + offs - 1);
2815                         nsitd->itd.itd_bp0 = LE((buf + offs) & OHCI_ITD_PAGE_MASK);
2816                         sitd = nsitd;
2817                         iso->next = iso->next + ncur; 
2818                         ncur = 0;
2819                         ncross = 0;
2820                 }
2821                 /* XXX byte order */
2822                 sitd->itd.itd_offset[i] = 
2823                     offs | (ncross == 1 ? OHCI_ITD_PAGE_SELECT : 0);
2824                 offs += xfer->frlengths[i];
2825                 /* XXX update ncross */
2826         }
2827         nsitd = ohci_alloc_sitd(sc);
2828         if (nsitd == NULL) {
2829                 /* XXX what now? */
2830                 splx(s);
2831                 return;
2832         }
2833         sitd->nextitd = nsitd;
2834         sitd->itd.itd_nextitd = LE(nsitd->physaddr);
2835         sitd->itd.itd_flags = LE(
2836                 OHCI_ITD_NOCC | 
2837                 OHCI_ITD_SET_SF(iso->next) |
2838                 OHCI_ITD_SET_DI(0) |
2839                 OHCI_ITD_SET_FC(ncur));
2840         sitd->itd.itd_be = LE(LE(sitd->itd.itd_bp0) + offs - 1);
2841         iso->next = iso->next + ncur;
2842
2843         opipe->tail.itd = nsitd;
2844         sed->ed.ed_tailp = LE(nsitd->physaddr);
2845         /* XXX update ED */
2846         splx(s);
2847 }
2848
2849 usbd_status
2850 ohci_device_isoc_start(usbd_xfer_handle xfer)
2851 {
2852         printf("ohci_device_isoc_start: not implemented\n");
2853         return (USBD_INVAL);
2854 }
2855
2856 void
2857 ohci_device_isoc_abort(usbd_xfer_handle xfer)
2858 {
2859 }
2860
2861 void
2862 ohci_device_isoc_done(usbd_xfer_handle xfer)
2863 {
2864         printf("ohci_device_isoc_done: not implemented\n");
2865 }
2866
2867 usbd_status
2868 ohci_setup_isoc(usbd_pipe_handle pipe)
2869 {
2870         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2871         struct iso *iso = &opipe->u.iso;
2872
2873         iso->next = -1;
2874         iso->inuse = 0;
2875
2876         return (USBD_NORMAL_COMPLETION);
2877 }
2878
2879 void
2880 ohci_device_isoc_close(usbd_pipe_handle pipe)
2881 {
2882         struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2883         ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2884
2885         DPRINTF(("ohci_device_isoc_close: pipe=%p\n", pipe));
2886         ohci_close_pipe(pipe, sc->sc_isoc_head);
2887         ohci_free_sitd(sc, opipe->tail.itd);
2888 }