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