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