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