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