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