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