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