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