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