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