ohci.c revision 1.108 1 /* $NetBSD: ohci.c,v 1.108 2001/11/13 06:24:54 lukem 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.108 2001/11/13 06:24:54 lukem 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 we get an interrupt while polling, then just ignore it. */
1060 if (sc->sc_bus.use_polling) {
1061 #ifdef DIAGNOSTIC
1062 printf("ohci_intr: ignored interrupt while polling\n");
1063 #endif
1064 return (0);
1065 }
1066
1067 return (ohci_intr1(sc));
1068 }
1069
1070 Static int
1071 ohci_intr1(ohci_softc_t *sc)
1072 {
1073 u_int32_t intrs, eintrs;
1074 ohci_physaddr_t done;
1075
1076 DPRINTFN(14,("ohci_intr1: enter\n"));
1077
1078 /* In case the interrupt occurs before initialization has completed. */
1079 if (sc == NULL || sc->sc_hcca == NULL) {
1080 #ifdef DIAGNOSTIC
1081 printf("ohci_intr: sc->sc_hcca == NULL\n");
1082 #endif
1083 return (0);
1084 }
1085
1086 intrs = 0;
1087 done = le32toh(sc->sc_hcca->hcca_done_head);
1088 if (done != 0) {
1089 if (done & ~OHCI_DONE_INTRS)
1090 intrs = OHCI_WDH;
1091 if (done & OHCI_DONE_INTRS)
1092 intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
1093 } else
1094 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1095
1096 if (!intrs)
1097 return (0);
1098
1099 intrs &= ~OHCI_MIE;
1100 OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs); /* Acknowledge */
1101 eintrs = intrs & sc->sc_eintrs;
1102 if (!eintrs)
1103 return (0);
1104
1105 sc->sc_bus.intr_context++;
1106 sc->sc_bus.no_intrs++;
1107 DPRINTFN(7, ("ohci_intr: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
1108 sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS),
1109 (u_int)eintrs));
1110
1111 if (eintrs & OHCI_SO) {
1112 sc->sc_overrun_cnt++;
1113 if (usbd_ratecheck(&sc->sc_overrun_ntc)) {
1114 printf("%s: %u scheduling overruns\n",
1115 USBDEVNAME(sc->sc_bus.bdev), sc->sc_overrun_cnt);
1116 sc->sc_overrun_cnt = 0;
1117 }
1118 /* XXX do what */
1119 eintrs &= ~OHCI_SO;
1120 }
1121 if (eintrs & OHCI_WDH) {
1122 ohci_add_done(sc, done &~ OHCI_DONE_INTRS);
1123 sc->sc_hcca->hcca_done_head = 0;
1124 usb_schedsoftintr(&sc->sc_bus);
1125 eintrs &= ~OHCI_WDH;
1126 }
1127 if (eintrs & OHCI_RD) {
1128 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1129 /* XXX process resume detect */
1130 }
1131 if (eintrs & OHCI_UE) {
1132 printf("%s: unrecoverable error, controller halted\n",
1133 USBDEVNAME(sc->sc_bus.bdev));
1134 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1135 /* XXX what else */
1136 }
1137 if (eintrs & OHCI_RHSC) {
1138 ohci_rhsc(sc, sc->sc_intrxfer);
1139 /*
1140 * Disable RHSC interrupt for now, because it will be
1141 * on until the port has been reset.
1142 */
1143 ohci_rhsc_able(sc, 0);
1144 /* Do not allow RHSC interrupts > 1 per second */
1145 usb_callout(sc->sc_tmo_rhsc, hz, ohci_rhsc_enable, sc);
1146 eintrs &= ~OHCI_RHSC;
1147 }
1148
1149 sc->sc_bus.intr_context--;
1150
1151 if (eintrs != 0) {
1152 /* Block unprocessed interrupts. XXX */
1153 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, eintrs);
1154 sc->sc_eintrs &= ~eintrs;
1155 printf("%s: blocking intrs 0x%x\n",
1156 USBDEVNAME(sc->sc_bus.bdev), eintrs);
1157 }
1158
1159 return (1);
1160 }
1161
1162 void
1163 ohci_rhsc_able(ohci_softc_t *sc, int on)
1164 {
1165 DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on));
1166 if (on) {
1167 sc->sc_eintrs |= OHCI_RHSC;
1168 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
1169 } else {
1170 sc->sc_eintrs &= ~OHCI_RHSC;
1171 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
1172 }
1173 }
1174
1175 void
1176 ohci_rhsc_enable(void *v_sc)
1177 {
1178 ohci_softc_t *sc = v_sc;
1179
1180 ohci_rhsc_able(sc, 1);
1181 }
1182
1183 #ifdef OHCI_DEBUG
1184 char *ohci_cc_strs[] = {
1185 "NO_ERROR",
1186 "CRC",
1187 "BIT_STUFFING",
1188 "DATA_TOGGLE_MISMATCH",
1189 "STALL",
1190 "DEVICE_NOT_RESPONDING",
1191 "PID_CHECK_FAILURE",
1192 "UNEXPECTED_PID",
1193 "DATA_OVERRUN",
1194 "DATA_UNDERRUN",
1195 "BUFFER_OVERRUN",
1196 "BUFFER_UNDERRUN",
1197 "reserved",
1198 "reserved",
1199 "NOT_ACCESSED",
1200 "NOT_ACCESSED",
1201 };
1202 #endif
1203
1204 void
1205 ohci_add_done(ohci_softc_t *sc, ohci_physaddr_t done)
1206 {
1207 ohci_soft_itd_t *sitd, *sidone, **ip;
1208 ohci_soft_td_t *std, *sdone, **p;
1209
1210 /* Reverse the done list. */
1211 for (sdone = NULL, sidone = NULL; done != 0; ) {
1212 std = ohci_hash_find_td(sc, done);
1213 if (std != NULL) {
1214 std->dnext = sdone;
1215 done = le32toh(std->td.td_nexttd);
1216 sdone = std;
1217 DPRINTFN(10,("add TD %p\n", std));
1218 continue;
1219 }
1220 sitd = ohci_hash_find_itd(sc, done);
1221 if (sitd != NULL) {
1222 sitd->dnext = sidone;
1223 done = le32toh(sitd->itd.itd_nextitd);
1224 sidone = sitd;
1225 DPRINTFN(5,("add ITD %p\n", sitd));
1226 continue;
1227 }
1228 panic("ohci_add_done: addr 0x%08lx not found\n", (u_long)done);
1229 }
1230
1231 /* sdone & sidone now hold the done lists. */
1232 /* Put them on the already processed lists. */
1233 for (p = &sc->sc_sdone; *p != NULL; p = &(*p)->dnext)
1234 ;
1235 *p = sdone;
1236 for (ip = &sc->sc_sidone; *ip != NULL; ip = &(*ip)->dnext)
1237 ;
1238 *ip = sidone;
1239 }
1240
1241 void
1242 ohci_softintr(void *v)
1243 {
1244 ohci_softc_t *sc = v;
1245 ohci_soft_itd_t *sitd, *sidone, *sitdnext;
1246 ohci_soft_td_t *std, *sdone, *stdnext;
1247 usbd_xfer_handle xfer;
1248 int len, cc, s;
1249
1250 DPRINTFN(10,("ohci_softintr: enter\n:"));
1251
1252 sc->sc_bus.intr_context++;
1253
1254 s = splhardusb();
1255 sdone = sc->sc_sdone;
1256 sc->sc_sdone = NULL;
1257 sidone = sc->sc_sidone;
1258 sc->sc_sidone = NULL;
1259 splx(s);
1260
1261 DPRINTFN(10,("ohci_softintr: sdone=%p sidone=%p\n", sdone, sidone));
1262
1263 #ifdef OHCI_DEBUG
1264 if (ohcidebug > 10) {
1265 DPRINTF(("ohci_process_done: TD done:\n"));
1266 ohci_dump_tds(sdone);
1267 }
1268 #endif
1269
1270 for (std = sdone; std; std = stdnext) {
1271 xfer = std->xfer;
1272 stdnext = std->dnext;
1273 DPRINTFN(10, ("ohci_process_done: std=%p xfer=%p hcpriv=%p\n",
1274 std, xfer, xfer ? xfer->hcpriv : 0));
1275 if (xfer == NULL) {
1276 /* xfer == NULL: There seems to be no xfer associated
1277 * with this TD. It is tailp that happened to end up on
1278 * the done queue.
1279 */
1280 continue;
1281 }
1282 if (xfer->status == USBD_CANCELLED ||
1283 xfer->status == USBD_TIMEOUT) {
1284 DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1285 xfer));
1286 /* Handled by abort routine. */
1287 continue;
1288 }
1289 usb_uncallout(xfer->timeout_handle, ohci_timeout, xfer);
1290 cc = OHCI_TD_GET_CC(le32toh(std->td.td_flags));
1291 if (cc == OHCI_CC_NO_ERROR) {
1292 len = std->len;
1293 if (std->td.td_cbp != 0)
1294 len -= le32toh(std->td.td_be) -
1295 le32toh(std->td.td_cbp) + 1;
1296 DPRINTFN(10, ("ohci_process_done: len=%d, flags=0x%x\n",
1297 len, std->flags));
1298 if (std->flags & OHCI_ADD_LEN)
1299 xfer->actlen += len;
1300 if (std->flags & OHCI_CALL_DONE) {
1301 xfer->status = USBD_NORMAL_COMPLETION;
1302 usb_transfer_complete(xfer);
1303 }
1304 ohci_free_std(sc, std);
1305 } else {
1306 /*
1307 * Endpoint is halted. First unlink all the TDs
1308 * belonging to the failed transfer, and then restart
1309 * the endpoint.
1310 */
1311 ohci_soft_td_t *p, *n;
1312 struct ohci_pipe *opipe =
1313 (struct ohci_pipe *)xfer->pipe;
1314
1315 DPRINTFN(15,("ohci_process_done: error cc=%d (%s)\n",
1316 OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
1317 ohci_cc_strs[OHCI_TD_GET_CC(le32toh(std->td.td_flags))]));
1318
1319 /* remove TDs */
1320 for (p = std; p->xfer == xfer; p = n) {
1321 n = p->nexttd;
1322 ohci_free_std(sc, p);
1323 }
1324
1325 /* clear halt */
1326 opipe->sed->ed.ed_headp = htole32(p->physaddr);
1327 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1328
1329 if (cc == OHCI_CC_STALL)
1330 xfer->status = USBD_STALLED;
1331 else
1332 xfer->status = USBD_IOERROR;
1333 usb_transfer_complete(xfer);
1334 }
1335 }
1336
1337 #ifdef OHCI_DEBUG
1338 if (ohcidebug > 10) {
1339 DPRINTF(("ohci_softintr: ITD done:\n"));
1340 ohci_dump_itds(sidone);
1341 }
1342 #endif
1343
1344 for (sitd = sidone; sitd != NULL; sitd = sitdnext) {
1345 xfer = sitd->xfer;
1346 sitdnext = sitd->dnext;
1347 DPRINTFN(1, ("ohci_process_done: sitd=%p xfer=%p hcpriv=%p\n",
1348 sitd, xfer, xfer ? xfer->hcpriv : 0));
1349 if (xfer == NULL)
1350 continue;
1351 if (xfer->status == USBD_CANCELLED ||
1352 xfer->status == USBD_TIMEOUT) {
1353 DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1354 xfer));
1355 /* Handled by abort routine. */
1356 continue;
1357 }
1358 #ifdef DIAGNOSTIC
1359 if (sitd->isdone)
1360 printf("ohci_softintr: sitd=%p is done\n", sitd);
1361 sitd->isdone = 1;
1362 #endif
1363 cc = OHCI_ITD_GET_CC(le32toh(sitd->itd.itd_flags));
1364 if (cc == OHCI_CC_NO_ERROR) {
1365 /* XXX compute length for input */
1366 struct ohci_pipe *opipe =
1367 (struct ohci_pipe *)xfer->pipe;
1368 if (sitd->flags & OHCI_CALL_DONE) {
1369 opipe->u.iso.inuse -= xfer->nframes;
1370 /* XXX update frlengths with actual length */
1371 /* XXX xfer->actlen = actlen; */
1372 xfer->status = USBD_NORMAL_COMPLETION;
1373 usb_transfer_complete(xfer);
1374 }
1375 } else {
1376 /* XXX Do more */
1377 xfer->status = USBD_IOERROR;
1378 usb_transfer_complete(xfer);
1379 }
1380 }
1381
1382 sc->sc_bus.intr_context--;
1383 DPRINTFN(10,("ohci_softintr: done:\n"));
1384 }
1385
1386 void
1387 ohci_device_ctrl_done(usbd_xfer_handle xfer)
1388 {
1389 DPRINTFN(10,("ohci_ctrl_done: xfer=%p\n", xfer));
1390
1391 #ifdef DIAGNOSTIC
1392 if (!(xfer->rqflags & URQ_REQUEST)) {
1393 panic("ohci_ctrl_done: not a request\n");
1394 }
1395 #endif
1396 xfer->hcpriv = NULL;
1397 }
1398
1399 void
1400 ohci_device_intr_done(usbd_xfer_handle xfer)
1401 {
1402 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1403 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1404 ohci_soft_ed_t *sed = opipe->sed;
1405 ohci_soft_td_t *data, *tail;
1406
1407
1408 DPRINTFN(10,("ohci_intr_done: xfer=%p, actlen=%d\n",
1409 xfer, xfer->actlen));
1410
1411 xfer->hcpriv = NULL;
1412
1413 if (xfer->pipe->repeat) {
1414 data = opipe->tail.td;
1415 tail = ohci_alloc_std(sc); /* XXX should reuse TD */
1416 if (tail == NULL) {
1417 xfer->status = USBD_NOMEM;
1418 return;
1419 }
1420 tail->xfer = NULL;
1421
1422 data->td.td_flags = htole32(
1423 OHCI_TD_IN | OHCI_TD_NOCC |
1424 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
1425 if (xfer->flags & USBD_SHORT_XFER_OK)
1426 data->td.td_flags |= htole32(OHCI_TD_R);
1427 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf));
1428 data->nexttd = tail;
1429 data->td.td_nexttd = htole32(tail->physaddr);
1430 data->td.td_be = htole32(le32toh(data->td.td_cbp) +
1431 xfer->length - 1);
1432 data->len = xfer->length;
1433 data->xfer = xfer;
1434 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1435 xfer->hcpriv = data;
1436 xfer->actlen = 0;
1437
1438 sed->ed.ed_tailp = htole32(tail->physaddr);
1439 opipe->tail.td = tail;
1440 }
1441 }
1442
1443 void
1444 ohci_device_bulk_done(usbd_xfer_handle xfer)
1445 {
1446 DPRINTFN(10,("ohci_bulk_done: xfer=%p, actlen=%d\n",
1447 xfer, xfer->actlen));
1448
1449 xfer->hcpriv = NULL;
1450 }
1451
1452 void
1453 ohci_rhsc(ohci_softc_t *sc, usbd_xfer_handle xfer)
1454 {
1455 usbd_pipe_handle pipe;
1456 struct ohci_pipe *opipe;
1457 u_char *p;
1458 int i, m;
1459 int hstatus;
1460
1461 hstatus = OREAD4(sc, OHCI_RH_STATUS);
1462 DPRINTF(("ohci_rhsc: sc=%p xfer=%p hstatus=0x%08x\n",
1463 sc, xfer, hstatus));
1464
1465 if (xfer == NULL) {
1466 /* Just ignore the change. */
1467 return;
1468 }
1469
1470 pipe = xfer->pipe;
1471 opipe = (struct ohci_pipe *)pipe;
1472
1473 p = KERNADDR(&xfer->dmabuf);
1474 m = min(sc->sc_noport, xfer->length * 8 - 1);
1475 memset(p, 0, xfer->length);
1476 for (i = 1; i <= m; i++) {
1477 /* Pick out CHANGE bits from the status reg. */
1478 if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
1479 p[i/8] |= 1 << (i%8);
1480 }
1481 DPRINTF(("ohci_rhsc: change=0x%02x\n", *p));
1482 xfer->actlen = xfer->length;
1483 xfer->status = USBD_NORMAL_COMPLETION;
1484
1485 usb_transfer_complete(xfer);
1486 }
1487
1488 void
1489 ohci_root_intr_done(usbd_xfer_handle xfer)
1490 {
1491 xfer->hcpriv = NULL;
1492 }
1493
1494 void
1495 ohci_root_ctrl_done(usbd_xfer_handle xfer)
1496 {
1497 xfer->hcpriv = NULL;
1498 }
1499
1500 /*
1501 * Wait here until controller claims to have an interrupt.
1502 * Then call ohci_intr and return. Use timeout to avoid waiting
1503 * too long.
1504 */
1505 void
1506 ohci_waitintr(ohci_softc_t *sc, usbd_xfer_handle xfer)
1507 {
1508 int timo = xfer->timeout;
1509 int usecs;
1510 u_int32_t intrs;
1511
1512 xfer->status = USBD_IN_PROGRESS;
1513 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
1514 usb_delay_ms(&sc->sc_bus, 1);
1515 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs;
1516 DPRINTFN(15,("ohci_waitintr: 0x%04x\n", intrs));
1517 #ifdef OHCI_DEBUG
1518 if (ohcidebug > 15)
1519 ohci_dumpregs(sc);
1520 #endif
1521 if (intrs) {
1522 ohci_intr1(sc);
1523 if (xfer->status != USBD_IN_PROGRESS)
1524 return;
1525 }
1526 }
1527
1528 /* Timeout */
1529 DPRINTF(("ohci_waitintr: timeout\n"));
1530 xfer->status = USBD_TIMEOUT;
1531 usb_transfer_complete(xfer);
1532 /* XXX should free TD */
1533 }
1534
1535 void
1536 ohci_poll(struct usbd_bus *bus)
1537 {
1538 ohci_softc_t *sc = (ohci_softc_t *)bus;
1539 #ifdef OHCI_DEBUG
1540 static int last;
1541 int new;
1542 new = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1543 if (new != last) {
1544 DPRINTFN(10,("ohci_poll: intrs=0x%04x\n", new));
1545 last = new;
1546 }
1547 #endif
1548
1549 if (OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs)
1550 ohci_intr1(sc);
1551 }
1552
1553 usbd_status
1554 ohci_device_request(usbd_xfer_handle xfer)
1555 {
1556 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1557 usb_device_request_t *req = &xfer->request;
1558 usbd_device_handle dev = opipe->pipe.device;
1559 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1560 int addr = dev->address;
1561 ohci_soft_td_t *setup, *stat, *next, *tail;
1562 ohci_soft_ed_t *sed;
1563 int isread;
1564 int len;
1565 usbd_status err;
1566 int s;
1567
1568 isread = req->bmRequestType & UT_READ;
1569 len = UGETW(req->wLength);
1570
1571 DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, "
1572 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1573 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1574 UGETW(req->wIndex), len, addr,
1575 opipe->pipe.endpoint->edesc->bEndpointAddress));
1576
1577 setup = opipe->tail.td;
1578 stat = ohci_alloc_std(sc);
1579 if (stat == NULL) {
1580 err = USBD_NOMEM;
1581 goto bad1;
1582 }
1583 tail = ohci_alloc_std(sc);
1584 if (tail == NULL) {
1585 err = USBD_NOMEM;
1586 goto bad2;
1587 }
1588 tail->xfer = NULL;
1589
1590 sed = opipe->sed;
1591 opipe->u.ctl.length = len;
1592
1593 /* Update device address and length since they may have changed. */
1594 /* XXX This only needs to be done once, but it's too early in open. */
1595 /* XXXX Should not touch ED here! */
1596 sed->ed.ed_flags = htole32(
1597 (le32toh(sed->ed.ed_flags) & ~(OHCI_ED_ADDRMASK | OHCI_ED_MAXPMASK)) |
1598 OHCI_ED_SET_FA(addr) |
1599 OHCI_ED_SET_MAXP(UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize)));
1600
1601 next = stat;
1602
1603 /* Set up data transaction */
1604 if (len != 0) {
1605 ohci_soft_td_t *std = stat;
1606
1607 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
1608 std, &stat);
1609 stat = stat->nexttd; /* point at free TD */
1610 if (err)
1611 goto bad3;
1612 /* Start toggle at 1 and then use the carried toggle. */
1613 std->td.td_flags &= htole32(~OHCI_TD_TOGGLE_MASK);
1614 std->td.td_flags |= htole32(OHCI_TD_TOGGLE_1);
1615 }
1616
1617 memcpy(KERNADDR(&opipe->u.ctl.reqdma), req, sizeof *req);
1618
1619 setup->td.td_flags = htole32(OHCI_TD_SETUP | OHCI_TD_NOCC |
1620 OHCI_TD_TOGGLE_0 | OHCI_TD_NOINTR);
1621 setup->td.td_cbp = htole32(DMAADDR(&opipe->u.ctl.reqdma));
1622 setup->nexttd = next;
1623 setup->td.td_nexttd = htole32(next->physaddr);
1624 setup->td.td_be = htole32(le32toh(setup->td.td_cbp) + sizeof *req - 1);
1625 setup->len = 0;
1626 setup->xfer = xfer;
1627 setup->flags = 0;
1628 xfer->hcpriv = setup;
1629
1630 stat->td.td_flags = htole32(
1631 (isread ? OHCI_TD_OUT : OHCI_TD_IN) |
1632 OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1633 stat->td.td_cbp = 0;
1634 stat->nexttd = tail;
1635 stat->td.td_nexttd = htole32(tail->physaddr);
1636 stat->td.td_be = 0;
1637 stat->flags = OHCI_CALL_DONE;
1638 stat->len = 0;
1639 stat->xfer = xfer;
1640
1641 #ifdef OHCI_DEBUG
1642 if (ohcidebug > 5) {
1643 DPRINTF(("ohci_device_request:\n"));
1644 ohci_dump_ed(sed);
1645 ohci_dump_tds(setup);
1646 }
1647 #endif
1648
1649 /* Insert ED in schedule */
1650 s = splusb();
1651 sed->ed.ed_tailp = htole32(tail->physaddr);
1652 opipe->tail.td = tail;
1653 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1654 if (xfer->timeout && !sc->sc_bus.use_polling) {
1655 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1656 ohci_timeout, xfer);
1657 }
1658 splx(s);
1659
1660 #if 0
1661 if (ohcidebug > 10) {
1662 delay(10000);
1663 DPRINTF(("ohci_device_request: status=%x\n",
1664 OREAD4(sc, OHCI_COMMAND_STATUS)));
1665 ohci_dump_ed(sed);
1666 ohci_dump_tds(setup);
1667 }
1668 #endif
1669
1670 return (USBD_NORMAL_COMPLETION);
1671
1672 bad3:
1673 ohci_free_std(sc, tail);
1674 bad2:
1675 ohci_free_std(sc, stat);
1676 bad1:
1677 return (err);
1678 }
1679
1680 /*
1681 * Add an ED to the schedule. Called at splusb().
1682 */
1683 void
1684 ohci_add_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1685 {
1686 SPLUSBCHECK;
1687 sed->next = head->next;
1688 sed->ed.ed_nexted = head->ed.ed_nexted;
1689 head->next = sed;
1690 head->ed.ed_nexted = htole32(sed->physaddr);
1691 }
1692
1693 /*
1694 * Remove an ED from the schedule. Called at splusb().
1695 */
1696 void
1697 ohci_rem_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1698 {
1699 ohci_soft_ed_t *p;
1700
1701 SPLUSBCHECK;
1702
1703 /* XXX */
1704 for (p = head; p == NULL && p->next != sed; p = p->next)
1705 ;
1706 if (p == NULL)
1707 panic("ohci_rem_ed: ED not found\n");
1708 p->next = sed->next;
1709 p->ed.ed_nexted = sed->ed.ed_nexted;
1710 }
1711
1712 /*
1713 * When a transfer is completed the TD is added to the done queue by
1714 * the host controller. This queue is the processed by software.
1715 * Unfortunately the queue contains the physical address of the TD
1716 * and we have no simple way to translate this back to a kernel address.
1717 * To make the translation possible (and fast) we use a hash table of
1718 * TDs currently in the schedule. The physical address is used as the
1719 * hash value.
1720 */
1721
1722 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
1723 /* Called at splusb() */
1724 void
1725 ohci_hash_add_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1726 {
1727 int h = HASH(std->physaddr);
1728
1729 SPLUSBCHECK;
1730
1731 LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
1732 }
1733
1734 /* Called at splusb() */
1735 void
1736 ohci_hash_rem_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1737 {
1738 SPLUSBCHECK;
1739
1740 LIST_REMOVE(std, hnext);
1741 }
1742
1743 ohci_soft_td_t *
1744 ohci_hash_find_td(ohci_softc_t *sc, ohci_physaddr_t a)
1745 {
1746 int h = HASH(a);
1747 ohci_soft_td_t *std;
1748
1749 for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
1750 std != NULL;
1751 std = LIST_NEXT(std, hnext))
1752 if (std->physaddr == a)
1753 return (std);
1754 return (NULL);
1755 }
1756
1757 /* Called at splusb() */
1758 void
1759 ohci_hash_add_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1760 {
1761 int h = HASH(sitd->physaddr);
1762
1763 SPLUSBCHECK;
1764
1765 DPRINTFN(10,("ohci_hash_add_itd: sitd=%p physaddr=0x%08lx\n",
1766 sitd, (u_long)sitd->physaddr));
1767
1768 LIST_INSERT_HEAD(&sc->sc_hash_itds[h], sitd, hnext);
1769 }
1770
1771 /* Called at splusb() */
1772 void
1773 ohci_hash_rem_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1774 {
1775 SPLUSBCHECK;
1776
1777 DPRINTFN(10,("ohci_hash_rem_itd: sitd=%p physaddr=0x%08lx\n",
1778 sitd, (u_long)sitd->physaddr));
1779
1780 LIST_REMOVE(sitd, hnext);
1781 }
1782
1783 ohci_soft_itd_t *
1784 ohci_hash_find_itd(ohci_softc_t *sc, ohci_physaddr_t a)
1785 {
1786 int h = HASH(a);
1787 ohci_soft_itd_t *sitd;
1788
1789 for (sitd = LIST_FIRST(&sc->sc_hash_itds[h]);
1790 sitd != NULL;
1791 sitd = LIST_NEXT(sitd, hnext))
1792 if (sitd->physaddr == a)
1793 return (sitd);
1794 return (NULL);
1795 }
1796
1797 void
1798 ohci_timeout(void *addr)
1799 {
1800 usbd_xfer_handle xfer = addr;
1801 int s;
1802
1803 DPRINTF(("ohci_timeout: xfer=%p\n", xfer));
1804
1805 s = splusb();
1806 xfer->device->bus->intr_context++;
1807 ohci_abort_xfer(xfer, USBD_TIMEOUT);
1808 xfer->device->bus->intr_context--;
1809 splx(s);
1810 }
1811
1812 #ifdef OHCI_DEBUG
1813 void
1814 ohci_dump_tds(ohci_soft_td_t *std)
1815 {
1816 for (; std; std = std->nexttd)
1817 ohci_dump_td(std);
1818 }
1819
1820 void
1821 ohci_dump_td(ohci_soft_td_t *std)
1822 {
1823 char sbuf[128];
1824
1825 bitmask_snprintf((int)le32toh(std->td.td_flags),
1826 "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE",
1827 sbuf, sizeof(sbuf));
1828
1829 printf("TD(%p) at %08lx: %s delay=%d ec=%d cc=%d\ncbp=0x%08lx "
1830 "nexttd=0x%08lx be=0x%08lx\n",
1831 std, (u_long)std->physaddr, sbuf,
1832 OHCI_TD_GET_DI(le32toh(std->td.td_flags)),
1833 OHCI_TD_GET_EC(le32toh(std->td.td_flags)),
1834 OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
1835 (u_long)le32toh(std->td.td_cbp),
1836 (u_long)le32toh(std->td.td_nexttd),
1837 (u_long)le32toh(std->td.td_be));
1838 }
1839
1840 void
1841 ohci_dump_itd(ohci_soft_itd_t *sitd)
1842 {
1843 int i;
1844
1845 printf("ITD(%p) at %08lx: sf=%d di=%d fc=%d cc=%d\n"
1846 "bp0=0x%08lx next=0x%08lx be=0x%08lx\n",
1847 sitd, (u_long)sitd->physaddr,
1848 OHCI_ITD_GET_SF(le32toh(sitd->itd.itd_flags)),
1849 OHCI_ITD_GET_DI(le32toh(sitd->itd.itd_flags)),
1850 OHCI_ITD_GET_FC(le32toh(sitd->itd.itd_flags)),
1851 OHCI_ITD_GET_CC(le32toh(sitd->itd.itd_flags)),
1852 (u_long)le32toh(sitd->itd.itd_bp0),
1853 (u_long)le32toh(sitd->itd.itd_nextitd),
1854 (u_long)le32toh(sitd->itd.itd_be));
1855 for (i = 0; i < OHCI_ITD_NOFFSET; i++)
1856 printf("offs[%d]=0x%04x ", i,
1857 (u_int)le16toh(sitd->itd.itd_offset[i]));
1858 printf("\n");
1859 }
1860
1861 void
1862 ohci_dump_itds(ohci_soft_itd_t *sitd)
1863 {
1864 for (; sitd; sitd = sitd->nextitd)
1865 ohci_dump_itd(sitd);
1866 }
1867
1868 void
1869 ohci_dump_ed(ohci_soft_ed_t *sed)
1870 {
1871 char sbuf[128], sbuf2[128];
1872
1873 bitmask_snprintf((int)le32toh(sed->ed.ed_flags),
1874 "\20\14OUT\15IN\16LOWSPEED\17SKIP\20ISO",
1875 sbuf, sizeof(sbuf));
1876 bitmask_snprintf((u_long)le32toh(sed->ed.ed_headp),
1877 "\20\1HALT\2CARRY", sbuf2, sizeof(sbuf2));
1878
1879 printf("ED(%p) at 0x%08lx: addr=%d endpt=%d maxp=%d flags=%s\ntailp=0x%08lx "
1880 "headflags=%s headp=0x%08lx nexted=0x%08lx\n",
1881 sed, (u_long)sed->physaddr,
1882 OHCI_ED_GET_FA(le32toh(sed->ed.ed_flags)),
1883 OHCI_ED_GET_EN(le32toh(sed->ed.ed_flags)),
1884 OHCI_ED_GET_MAXP(le32toh(sed->ed.ed_flags)), sbuf,
1885 (u_long)le32toh(sed->ed.ed_tailp), sbuf2,
1886 (u_long)le32toh(sed->ed.ed_headp),
1887 (u_long)le32toh(sed->ed.ed_nexted));
1888 }
1889 #endif
1890
1891 usbd_status
1892 ohci_open(usbd_pipe_handle pipe)
1893 {
1894 usbd_device_handle dev = pipe->device;
1895 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1896 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1897 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
1898 u_int8_t addr = dev->address;
1899 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
1900 ohci_soft_ed_t *sed;
1901 ohci_soft_td_t *std;
1902 ohci_soft_itd_t *sitd;
1903 ohci_physaddr_t tdphys;
1904 u_int32_t fmt;
1905 usbd_status err;
1906 int s;
1907 int ival;
1908
1909 DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1910 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
1911
1912 std = NULL;
1913 sed = NULL;
1914
1915 if (addr == sc->sc_addr) {
1916 switch (ed->bEndpointAddress) {
1917 case USB_CONTROL_ENDPOINT:
1918 pipe->methods = &ohci_root_ctrl_methods;
1919 break;
1920 case UE_DIR_IN | OHCI_INTR_ENDPT:
1921 pipe->methods = &ohci_root_intr_methods;
1922 break;
1923 default:
1924 return (USBD_INVAL);
1925 }
1926 } else {
1927 sed = ohci_alloc_sed(sc);
1928 if (sed == NULL)
1929 goto bad0;
1930 opipe->sed = sed;
1931 if (xfertype == UE_ISOCHRONOUS) {
1932 sitd = ohci_alloc_sitd(sc);
1933 if (sitd == NULL) {
1934 ohci_free_sitd(sc, sitd);
1935 goto bad1;
1936 }
1937 opipe->tail.itd = sitd;
1938 tdphys = sitd->physaddr;
1939 fmt = OHCI_ED_FORMAT_ISO;
1940 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
1941 fmt |= OHCI_ED_DIR_IN;
1942 else
1943 fmt |= OHCI_ED_DIR_OUT;
1944 } else {
1945 std = ohci_alloc_std(sc);
1946 if (std == NULL) {
1947 ohci_free_std(sc, std);
1948 goto bad1;
1949 }
1950 opipe->tail.td = std;
1951 tdphys = std->physaddr;
1952 fmt = OHCI_ED_FORMAT_GEN | OHCI_ED_DIR_TD;
1953 }
1954 sed->ed.ed_flags = htole32(
1955 OHCI_ED_SET_FA(addr) |
1956 OHCI_ED_SET_EN(ed->bEndpointAddress) |
1957 (dev->lowspeed ? OHCI_ED_SPEED : 0) | fmt |
1958 OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
1959 sed->ed.ed_headp = sed->ed.ed_tailp = htole32(tdphys);
1960
1961 switch (xfertype) {
1962 case UE_CONTROL:
1963 pipe->methods = &ohci_device_ctrl_methods;
1964 err = usb_allocmem(&sc->sc_bus,
1965 sizeof(usb_device_request_t),
1966 0, &opipe->u.ctl.reqdma);
1967 if (err)
1968 goto bad;
1969 s = splusb();
1970 ohci_add_ed(sed, sc->sc_ctrl_head);
1971 splx(s);
1972 break;
1973 case UE_INTERRUPT:
1974 pipe->methods = &ohci_device_intr_methods;
1975 ival = pipe->interval;
1976 if (ival == USBD_DEFAULT_INTERVAL)
1977 ival = ed->bInterval;
1978 return (ohci_device_setintr(sc, opipe, ival));
1979 case UE_ISOCHRONOUS:
1980 pipe->methods = &ohci_device_isoc_methods;
1981 return (ohci_setup_isoc(pipe));
1982 case UE_BULK:
1983 pipe->methods = &ohci_device_bulk_methods;
1984 s = splusb();
1985 ohci_add_ed(sed, sc->sc_bulk_head);
1986 splx(s);
1987 break;
1988 }
1989 }
1990 return (USBD_NORMAL_COMPLETION);
1991
1992 bad:
1993 if (std != NULL)
1994 ohci_free_std(sc, std);
1995 bad1:
1996 if (sed != NULL)
1997 ohci_free_sed(sc, sed);
1998 bad0:
1999 return (USBD_NOMEM);
2000
2001 }
2002
2003 /*
2004 * Close a reqular pipe.
2005 * Assumes that there are no pending transactions.
2006 */
2007 void
2008 ohci_close_pipe(usbd_pipe_handle pipe, ohci_soft_ed_t *head)
2009 {
2010 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2011 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2012 ohci_soft_ed_t *sed = opipe->sed;
2013 int s;
2014
2015 s = splusb();
2016 #ifdef DIAGNOSTIC
2017 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
2018 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2019 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK)) {
2020 ohci_soft_td_t *std;
2021 std = ohci_hash_find_td(sc, le32toh(sed->ed.ed_headp));
2022 printf("ohci_close_pipe: pipe not empty sed=%p hd=0x%x "
2023 "tl=0x%x pipe=%p, std=%p\n", sed,
2024 (int)le32toh(sed->ed.ed_headp),
2025 (int)le32toh(sed->ed.ed_tailp),
2026 pipe, std);
2027 #ifdef USB_DEBUG
2028 usbd_dump_pipe(&opipe->pipe);
2029 #endif
2030 #ifdef OHCI_DEBUG
2031 ohci_dump_ed(sed);
2032 if (std)
2033 ohci_dump_td(std);
2034 #endif
2035 usb_delay_ms(&sc->sc_bus, 2);
2036 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2037 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
2038 printf("ohci_close_pipe: pipe still not empty\n");
2039 }
2040 #endif
2041 ohci_rem_ed(sed, head);
2042 splx(s);
2043 ohci_free_sed(sc, opipe->sed);
2044 }
2045
2046 /*
2047 * Abort a device request.
2048 * If this routine is called at splusb() it guarantees that the request
2049 * will be removed from the hardware scheduling and that the callback
2050 * for it will be called with USBD_CANCELLED status.
2051 * It's impossible to guarantee that the requested transfer will not
2052 * have happened since the hardware runs concurrently.
2053 * If the transaction has already happened we rely on the ordinary
2054 * interrupt processing to process it.
2055 */
2056 void
2057 ohci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
2058 {
2059 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2060 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
2061 ohci_soft_ed_t *sed = opipe->sed;
2062 ohci_soft_td_t *p, *n;
2063 ohci_physaddr_t headp;
2064 int s, hit;
2065
2066 DPRINTF(("ohci_abort_xfer: xfer=%p pipe=%p sed=%p\n", xfer, opipe,sed));
2067
2068 if (xfer->device->bus->intr_context || !curproc)
2069 panic("ohci_abort_xfer: not in process context\n");
2070
2071 /*
2072 * Step 1: Make interrupt routine and hardware ignore xfer.
2073 */
2074 s = splusb();
2075 xfer->status = status; /* make software ignore it */
2076 usb_uncallout(xfer->timeout_handle, ohci_timeout, xfer);
2077 splx(s);
2078 DPRINTFN(1,("ohci_abort_xfer: stop ed=%p\n", sed));
2079 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
2080
2081 /*
2082 * Step 2: Wait until we know hardware has finished any possible
2083 * use of the xfer. Also make sure the soft interrupt routine
2084 * has run.
2085 */
2086 usb_delay_ms(opipe->pipe.device->bus, 1); /* Hardware finishes in 1ms */
2087 /* XXX should have some communication with softintr() to know
2088 when it's done */
2089 usb_delay_ms(opipe->pipe.device->bus, 250);
2090
2091 /*
2092 * Step 3: Remove any vestiges of the xfer from the hardware.
2093 * The complication here is that the hardware may have executed
2094 * beyond the xfer we're trying to abort. So as we're scanning
2095 * the TDs of this xfer we check if the hardware points to
2096 * any of them.
2097 */
2098 s = splusb(); /* XXX why? */
2099 p = xfer->hcpriv;
2100 #ifdef DIAGNOSTIC
2101 if (p == NULL) {
2102 splx(s);
2103 printf("ohci_abort_xfer: hcpriv is NULL\n");
2104 return;
2105 }
2106 #endif
2107 #ifdef OHCI_DEBUG
2108 if (ohcidebug > 1) {
2109 DPRINTF(("ohci_abort_xfer: sed=\n"));
2110 ohci_dump_ed(sed);
2111 ohci_dump_tds(p);
2112 }
2113 #endif
2114 headp = le32toh(sed->ed.ed_headp) & OHCI_HEADMASK;
2115 hit = 0;
2116 for (; p->xfer == xfer; p = n) {
2117 hit |= headp == p->physaddr;
2118 n = p->nexttd;
2119 ohci_free_std(sc, p);
2120 }
2121 /* Zap headp register if hardware pointed inside the xfer. */
2122 if (hit) {
2123 DPRINTFN(1,("ohci_abort_xfer: set hd=0x08%x, tl=0x%08x\n",
2124 (int)p->physaddr, (int)le32toh(sed->ed.ed_tailp)));
2125 sed->ed.ed_headp = htole32(p->physaddr); /* unlink TDs */
2126 } else {
2127 DPRINTFN(1,("ohci_abort_xfer: no hit\n"));
2128 }
2129
2130 /*
2131 * Step 4: Turn on hardware again.
2132 */
2133 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
2134
2135 /*
2136 * Step 5: Execute callback.
2137 */
2138 usb_transfer_complete(xfer);
2139
2140 splx(s);
2141 }
2142
2143 /*
2144 * Data structures and routines to emulate the root hub.
2145 */
2146 Static usb_device_descriptor_t ohci_devd = {
2147 USB_DEVICE_DESCRIPTOR_SIZE,
2148 UDESC_DEVICE, /* type */
2149 {0x00, 0x01}, /* USB version */
2150 UDCLASS_HUB, /* class */
2151 UDSUBCLASS_HUB, /* subclass */
2152 0, /* protocol */
2153 64, /* max packet */
2154 {0},{0},{0x00,0x01}, /* device id */
2155 1,2,0, /* string indicies */
2156 1 /* # of configurations */
2157 };
2158
2159 Static usb_config_descriptor_t ohci_confd = {
2160 USB_CONFIG_DESCRIPTOR_SIZE,
2161 UDESC_CONFIG,
2162 {USB_CONFIG_DESCRIPTOR_SIZE +
2163 USB_INTERFACE_DESCRIPTOR_SIZE +
2164 USB_ENDPOINT_DESCRIPTOR_SIZE},
2165 1,
2166 1,
2167 0,
2168 UC_SELF_POWERED,
2169 0 /* max power */
2170 };
2171
2172 Static usb_interface_descriptor_t ohci_ifcd = {
2173 USB_INTERFACE_DESCRIPTOR_SIZE,
2174 UDESC_INTERFACE,
2175 0,
2176 0,
2177 1,
2178 UICLASS_HUB,
2179 UISUBCLASS_HUB,
2180 0,
2181 0
2182 };
2183
2184 Static usb_endpoint_descriptor_t ohci_endpd = {
2185 USB_ENDPOINT_DESCRIPTOR_SIZE,
2186 UDESC_ENDPOINT,
2187 UE_DIR_IN | OHCI_INTR_ENDPT,
2188 UE_INTERRUPT,
2189 {8, 0}, /* max packet */
2190 255
2191 };
2192
2193 Static usb_hub_descriptor_t ohci_hubd = {
2194 USB_HUB_DESCRIPTOR_SIZE,
2195 UDESC_HUB,
2196 0,
2197 {0,0},
2198 0,
2199 0,
2200 {0},
2201 };
2202
2203 Static int
2204 ohci_str(p, l, s)
2205 usb_string_descriptor_t *p;
2206 int l;
2207 char *s;
2208 {
2209 int i;
2210
2211 if (l == 0)
2212 return (0);
2213 p->bLength = 2 * strlen(s) + 2;
2214 if (l == 1)
2215 return (1);
2216 p->bDescriptorType = UDESC_STRING;
2217 l -= 2;
2218 for (i = 0; s[i] && l > 1; i++, l -= 2)
2219 USETW2(p->bString[i], 0, s[i]);
2220 return (2*i+2);
2221 }
2222
2223 /*
2224 * Simulate a hardware hub by handling all the necessary requests.
2225 */
2226 Static usbd_status
2227 ohci_root_ctrl_transfer(usbd_xfer_handle xfer)
2228 {
2229 usbd_status err;
2230
2231 /* Insert last in queue. */
2232 err = usb_insert_transfer(xfer);
2233 if (err)
2234 return (err);
2235
2236 /* Pipe isn't running, start first */
2237 return (ohci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2238 }
2239
2240 Static usbd_status
2241 ohci_root_ctrl_start(usbd_xfer_handle xfer)
2242 {
2243 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2244 usb_device_request_t *req;
2245 void *buf = NULL;
2246 int port, i;
2247 int s, len, value, index, l, totlen = 0;
2248 usb_port_status_t ps;
2249 usb_hub_descriptor_t hubd;
2250 usbd_status err;
2251 u_int32_t v;
2252
2253 if (sc->sc_dying)
2254 return (USBD_IOERROR);
2255
2256 #ifdef DIAGNOSTIC
2257 if (!(xfer->rqflags & URQ_REQUEST))
2258 /* XXX panic */
2259 return (USBD_INVAL);
2260 #endif
2261 req = &xfer->request;
2262
2263 DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n",
2264 req->bmRequestType, req->bRequest));
2265
2266 len = UGETW(req->wLength);
2267 value = UGETW(req->wValue);
2268 index = UGETW(req->wIndex);
2269
2270 if (len != 0)
2271 buf = KERNADDR(&xfer->dmabuf);
2272
2273 #define C(x,y) ((x) | ((y) << 8))
2274 switch(C(req->bRequest, req->bmRequestType)) {
2275 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2276 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2277 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2278 /*
2279 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2280 * for the integrated root hub.
2281 */
2282 break;
2283 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2284 if (len > 0) {
2285 *(u_int8_t *)buf = sc->sc_conf;
2286 totlen = 1;
2287 }
2288 break;
2289 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2290 DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value));
2291 switch(value >> 8) {
2292 case UDESC_DEVICE:
2293 if ((value & 0xff) != 0) {
2294 err = USBD_IOERROR;
2295 goto ret;
2296 }
2297 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2298 USETW(ohci_devd.idVendor, sc->sc_id_vendor);
2299 memcpy(buf, &ohci_devd, l);
2300 break;
2301 case UDESC_CONFIG:
2302 if ((value & 0xff) != 0) {
2303 err = USBD_IOERROR;
2304 goto ret;
2305 }
2306 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2307 memcpy(buf, &ohci_confd, l);
2308 buf = (char *)buf + l;
2309 len -= l;
2310 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2311 totlen += l;
2312 memcpy(buf, &ohci_ifcd, l);
2313 buf = (char *)buf + l;
2314 len -= l;
2315 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2316 totlen += l;
2317 memcpy(buf, &ohci_endpd, l);
2318 break;
2319 case UDESC_STRING:
2320 if (len == 0)
2321 break;
2322 *(u_int8_t *)buf = 0;
2323 totlen = 1;
2324 switch (value & 0xff) {
2325 case 1: /* Vendor */
2326 totlen = ohci_str(buf, len, sc->sc_vendor);
2327 break;
2328 case 2: /* Product */
2329 totlen = ohci_str(buf, len, "OHCI root hub");
2330 break;
2331 }
2332 break;
2333 default:
2334 err = USBD_IOERROR;
2335 goto ret;
2336 }
2337 break;
2338 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2339 if (len > 0) {
2340 *(u_int8_t *)buf = 0;
2341 totlen = 1;
2342 }
2343 break;
2344 case C(UR_GET_STATUS, UT_READ_DEVICE):
2345 if (len > 1) {
2346 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2347 totlen = 2;
2348 }
2349 break;
2350 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2351 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2352 if (len > 1) {
2353 USETW(((usb_status_t *)buf)->wStatus, 0);
2354 totlen = 2;
2355 }
2356 break;
2357 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2358 if (value >= USB_MAX_DEVICES) {
2359 err = USBD_IOERROR;
2360 goto ret;
2361 }
2362 sc->sc_addr = value;
2363 break;
2364 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2365 if (value != 0 && value != 1) {
2366 err = USBD_IOERROR;
2367 goto ret;
2368 }
2369 sc->sc_conf = value;
2370 break;
2371 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2372 break;
2373 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2374 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2375 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2376 err = USBD_IOERROR;
2377 goto ret;
2378 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2379 break;
2380 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2381 break;
2382 /* Hub requests */
2383 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2384 break;
2385 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2386 DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2387 "port=%d feature=%d\n",
2388 index, value));
2389 if (index < 1 || index > sc->sc_noport) {
2390 err = USBD_IOERROR;
2391 goto ret;
2392 }
2393 port = OHCI_RH_PORT_STATUS(index);
2394 switch(value) {
2395 case UHF_PORT_ENABLE:
2396 OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
2397 break;
2398 case UHF_PORT_SUSPEND:
2399 OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
2400 break;
2401 case UHF_PORT_POWER:
2402 /* Yes, writing to the LOW_SPEED bit clears power. */
2403 OWRITE4(sc, port, UPS_LOW_SPEED);
2404 break;
2405 case UHF_C_PORT_CONNECTION:
2406 OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
2407 break;
2408 case UHF_C_PORT_ENABLE:
2409 OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
2410 break;
2411 case UHF_C_PORT_SUSPEND:
2412 OWRITE4(sc, port, UPS_C_SUSPEND << 16);
2413 break;
2414 case UHF_C_PORT_OVER_CURRENT:
2415 OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
2416 break;
2417 case UHF_C_PORT_RESET:
2418 OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
2419 break;
2420 default:
2421 err = USBD_IOERROR;
2422 goto ret;
2423 }
2424 switch(value) {
2425 case UHF_C_PORT_CONNECTION:
2426 case UHF_C_PORT_ENABLE:
2427 case UHF_C_PORT_SUSPEND:
2428 case UHF_C_PORT_OVER_CURRENT:
2429 case UHF_C_PORT_RESET:
2430 /* Enable RHSC interrupt if condition is cleared. */
2431 if ((OREAD4(sc, port) >> 16) == 0)
2432 ohci_rhsc_able(sc, 1);
2433 break;
2434 default:
2435 break;
2436 }
2437 break;
2438 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2439 if (value != 0) {
2440 err = USBD_IOERROR;
2441 goto ret;
2442 }
2443 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
2444 hubd = ohci_hubd;
2445 hubd.bNbrPorts = sc->sc_noport;
2446 USETW(hubd.wHubCharacteristics,
2447 (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
2448 v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
2449 /* XXX overcurrent */
2450 );
2451 hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
2452 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
2453 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2454 hubd.DeviceRemovable[i++] = (u_int8_t)v;
2455 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2456 l = min(len, hubd.bDescLength);
2457 totlen = l;
2458 memcpy(buf, &hubd, l);
2459 break;
2460 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2461 if (len != 4) {
2462 err = USBD_IOERROR;
2463 goto ret;
2464 }
2465 memset(buf, 0, len); /* ? XXX */
2466 totlen = len;
2467 break;
2468 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2469 DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n",
2470 index));
2471 if (index < 1 || index > sc->sc_noport) {
2472 err = USBD_IOERROR;
2473 goto ret;
2474 }
2475 if (len != 4) {
2476 err = USBD_IOERROR;
2477 goto ret;
2478 }
2479 v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
2480 DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n",
2481 v));
2482 USETW(ps.wPortStatus, v);
2483 USETW(ps.wPortChange, v >> 16);
2484 l = min(len, sizeof ps);
2485 memcpy(buf, &ps, l);
2486 totlen = l;
2487 break;
2488 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2489 err = USBD_IOERROR;
2490 goto ret;
2491 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2492 break;
2493 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2494 if (index < 1 || index > sc->sc_noport) {
2495 err = USBD_IOERROR;
2496 goto ret;
2497 }
2498 port = OHCI_RH_PORT_STATUS(index);
2499 switch(value) {
2500 case UHF_PORT_ENABLE:
2501 OWRITE4(sc, port, UPS_PORT_ENABLED);
2502 break;
2503 case UHF_PORT_SUSPEND:
2504 OWRITE4(sc, port, UPS_SUSPEND);
2505 break;
2506 case UHF_PORT_RESET:
2507 DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n",
2508 index));
2509 OWRITE4(sc, port, UPS_RESET);
2510 for (i = 0; i < 10; i++) {
2511 usb_delay_ms(&sc->sc_bus, 10); /* XXX */
2512 if ((OREAD4(sc, port) & UPS_RESET) == 0)
2513 break;
2514 }
2515 DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n",
2516 index, OREAD4(sc, port)));
2517 break;
2518 case UHF_PORT_POWER:
2519 DPRINTFN(2,("ohci_root_ctrl_transfer: set port power "
2520 "%d\n", index));
2521 OWRITE4(sc, port, UPS_PORT_POWER);
2522 break;
2523 default:
2524 err = USBD_IOERROR;
2525 goto ret;
2526 }
2527 break;
2528 default:
2529 err = USBD_IOERROR;
2530 goto ret;
2531 }
2532 xfer->actlen = totlen;
2533 err = USBD_NORMAL_COMPLETION;
2534 ret:
2535 xfer->status = err;
2536 s = splusb();
2537 usb_transfer_complete(xfer);
2538 splx(s);
2539 return (USBD_IN_PROGRESS);
2540 }
2541
2542 /* Abort a root control request. */
2543 Static void
2544 ohci_root_ctrl_abort(usbd_xfer_handle xfer)
2545 {
2546 /* Nothing to do, all transfers are synchronous. */
2547 }
2548
2549 /* Close the root pipe. */
2550 Static void
2551 ohci_root_ctrl_close(usbd_pipe_handle pipe)
2552 {
2553 DPRINTF(("ohci_root_ctrl_close\n"));
2554 /* Nothing to do. */
2555 }
2556
2557 Static usbd_status
2558 ohci_root_intr_transfer(usbd_xfer_handle xfer)
2559 {
2560 usbd_status err;
2561
2562 /* Insert last in queue. */
2563 err = usb_insert_transfer(xfer);
2564 if (err)
2565 return (err);
2566
2567 /* Pipe isn't running, start first */
2568 return (ohci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2569 }
2570
2571 Static usbd_status
2572 ohci_root_intr_start(usbd_xfer_handle xfer)
2573 {
2574 usbd_pipe_handle pipe = xfer->pipe;
2575 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2576
2577 if (sc->sc_dying)
2578 return (USBD_IOERROR);
2579
2580 sc->sc_intrxfer = xfer;
2581
2582 return (USBD_IN_PROGRESS);
2583 }
2584
2585 /* Abort a root interrupt request. */
2586 Static void
2587 ohci_root_intr_abort(usbd_xfer_handle xfer)
2588 {
2589 int s;
2590
2591 if (xfer->pipe->intrxfer == xfer) {
2592 DPRINTF(("ohci_root_intr_abort: remove\n"));
2593 xfer->pipe->intrxfer = NULL;
2594 }
2595 xfer->status = USBD_CANCELLED;
2596 s = splusb();
2597 usb_transfer_complete(xfer);
2598 splx(s);
2599 }
2600
2601 /* Close the root pipe. */
2602 Static void
2603 ohci_root_intr_close(usbd_pipe_handle pipe)
2604 {
2605 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2606
2607 DPRINTF(("ohci_root_intr_close\n"));
2608
2609 sc->sc_intrxfer = NULL;
2610 }
2611
2612 /************************/
2613
2614 Static usbd_status
2615 ohci_device_ctrl_transfer(usbd_xfer_handle xfer)
2616 {
2617 usbd_status err;
2618
2619 /* Insert last in queue. */
2620 err = usb_insert_transfer(xfer);
2621 if (err)
2622 return (err);
2623
2624 /* Pipe isn't running, start first */
2625 return (ohci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2626 }
2627
2628 Static usbd_status
2629 ohci_device_ctrl_start(usbd_xfer_handle xfer)
2630 {
2631 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2632 usbd_status err;
2633
2634 if (sc->sc_dying)
2635 return (USBD_IOERROR);
2636
2637 #ifdef DIAGNOSTIC
2638 if (!(xfer->rqflags & URQ_REQUEST)) {
2639 /* XXX panic */
2640 printf("ohci_device_ctrl_transfer: not a request\n");
2641 return (USBD_INVAL);
2642 }
2643 #endif
2644
2645 err = ohci_device_request(xfer);
2646 if (err)
2647 return (err);
2648
2649 if (sc->sc_bus.use_polling)
2650 ohci_waitintr(sc, xfer);
2651 return (USBD_IN_PROGRESS);
2652 }
2653
2654 /* Abort a device control request. */
2655 Static void
2656 ohci_device_ctrl_abort(usbd_xfer_handle xfer)
2657 {
2658 DPRINTF(("ohci_device_ctrl_abort: xfer=%p\n", xfer));
2659 ohci_abort_xfer(xfer, USBD_CANCELLED);
2660 }
2661
2662 /* Close a device control pipe. */
2663 Static void
2664 ohci_device_ctrl_close(usbd_pipe_handle pipe)
2665 {
2666 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2667 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2668
2669 DPRINTF(("ohci_device_ctrl_close: pipe=%p\n", pipe));
2670 ohci_close_pipe(pipe, sc->sc_ctrl_head);
2671 ohci_free_std(sc, opipe->tail.td);
2672 }
2673
2674 /************************/
2675
2676 Static void
2677 ohci_device_clear_toggle(usbd_pipe_handle pipe)
2678 {
2679 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2680
2681 opipe->sed->ed.ed_headp &= htole32(~OHCI_TOGGLECARRY);
2682 }
2683
2684 Static void
2685 ohci_noop(usbd_pipe_handle pipe)
2686 {
2687 }
2688
2689 Static usbd_status
2690 ohci_device_bulk_transfer(usbd_xfer_handle xfer)
2691 {
2692 usbd_status err;
2693
2694 /* Insert last in queue. */
2695 err = usb_insert_transfer(xfer);
2696 if (err)
2697 return (err);
2698
2699 /* Pipe isn't running, start first */
2700 return (ohci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2701 }
2702
2703 Static usbd_status
2704 ohci_device_bulk_start(usbd_xfer_handle xfer)
2705 {
2706 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2707 usbd_device_handle dev = opipe->pipe.device;
2708 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2709 int addr = dev->address;
2710 ohci_soft_td_t *data, *tail, *tdp;
2711 ohci_soft_ed_t *sed;
2712 int s, len, isread, endpt;
2713 usbd_status err;
2714
2715 if (sc->sc_dying)
2716 return (USBD_IOERROR);
2717
2718 #ifdef DIAGNOSTIC
2719 if (xfer->rqflags & URQ_REQUEST) {
2720 /* XXX panic */
2721 printf("ohci_device_bulk_start: a request\n");
2722 return (USBD_INVAL);
2723 }
2724 #endif
2725
2726 len = xfer->length;
2727 endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2728 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2729 sed = opipe->sed;
2730
2731 DPRINTFN(4,("ohci_device_bulk_start: xfer=%p len=%d isread=%d "
2732 "flags=%d endpt=%d\n", xfer, len, isread, xfer->flags,
2733 endpt));
2734
2735 opipe->u.bulk.isread = isread;
2736 opipe->u.bulk.length = len;
2737
2738 /* Update device address */
2739 sed->ed.ed_flags = htole32(
2740 (le32toh(sed->ed.ed_flags) & ~OHCI_ED_ADDRMASK) |
2741 OHCI_ED_SET_FA(addr));
2742
2743 /* Allocate a chain of new TDs (including a new tail). */
2744 data = opipe->tail.td;
2745 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
2746 data, &tail);
2747 /* We want interrupt at the end of the transfer. */
2748 tail->td.td_flags &= htole32(~OHCI_TD_INTR_MASK);
2749 tail->td.td_flags |= htole32(OHCI_TD_SET_DI(1));
2750 tail->flags |= OHCI_CALL_DONE;
2751 tail = tail->nexttd; /* point at sentinel */
2752 if (err)
2753 return (err);
2754
2755 tail->xfer = NULL;
2756 xfer->hcpriv = data;
2757
2758 DPRINTFN(4,("ohci_device_bulk_start: ed_flags=0x%08x td_flags=0x%08x "
2759 "td_cbp=0x%08x td_be=0x%08x\n",
2760 (int)le32toh(sed->ed.ed_flags),
2761 (int)le32toh(data->td.td_flags),
2762 (int)le32toh(data->td.td_cbp),
2763 (int)le32toh(data->td.td_be)));
2764
2765 #ifdef OHCI_DEBUG
2766 if (ohcidebug > 5) {
2767 ohci_dump_ed(sed);
2768 ohci_dump_tds(data);
2769 }
2770 #endif
2771
2772 /* Insert ED in schedule */
2773 s = splusb();
2774 for (tdp = data; tdp != tail; tdp = tdp->nexttd) {
2775 tdp->xfer = xfer;
2776 }
2777 sed->ed.ed_tailp = htole32(tail->physaddr);
2778 opipe->tail.td = tail;
2779 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
2780 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
2781 if (xfer->timeout && !sc->sc_bus.use_polling) {
2782 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2783 ohci_timeout, xfer);
2784 }
2785
2786 #if 0
2787 /* This goes wrong if we are too slow. */
2788 if (ohcidebug > 10) {
2789 delay(10000);
2790 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2791 OREAD4(sc, OHCI_COMMAND_STATUS)));
2792 ohci_dump_ed(sed);
2793 ohci_dump_tds(data);
2794 }
2795 #endif
2796
2797 splx(s);
2798
2799 return (USBD_IN_PROGRESS);
2800 }
2801
2802 Static void
2803 ohci_device_bulk_abort(usbd_xfer_handle xfer)
2804 {
2805 DPRINTF(("ohci_device_bulk_abort: xfer=%p\n", xfer));
2806 ohci_abort_xfer(xfer, USBD_CANCELLED);
2807 }
2808
2809 /*
2810 * Close a device bulk pipe.
2811 */
2812 Static void
2813 ohci_device_bulk_close(usbd_pipe_handle pipe)
2814 {
2815 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2816 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2817
2818 DPRINTF(("ohci_device_bulk_close: pipe=%p\n", pipe));
2819 ohci_close_pipe(pipe, sc->sc_bulk_head);
2820 ohci_free_std(sc, opipe->tail.td);
2821 }
2822
2823 /************************/
2824
2825 Static usbd_status
2826 ohci_device_intr_transfer(usbd_xfer_handle xfer)
2827 {
2828 usbd_status err;
2829
2830 /* Insert last in queue. */
2831 err = usb_insert_transfer(xfer);
2832 if (err)
2833 return (err);
2834
2835 /* Pipe isn't running, start first */
2836 return (ohci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2837 }
2838
2839 Static usbd_status
2840 ohci_device_intr_start(usbd_xfer_handle xfer)
2841 {
2842 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2843 usbd_device_handle dev = opipe->pipe.device;
2844 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2845 ohci_soft_ed_t *sed = opipe->sed;
2846 ohci_soft_td_t *data, *tail;
2847 int len;
2848 int s;
2849
2850 if (sc->sc_dying)
2851 return (USBD_IOERROR);
2852
2853 DPRINTFN(3, ("ohci_device_intr_transfer: xfer=%p len=%d "
2854 "flags=%d priv=%p\n",
2855 xfer, xfer->length, xfer->flags, xfer->priv));
2856
2857 #ifdef DIAGNOSTIC
2858 if (xfer->rqflags & URQ_REQUEST)
2859 panic("ohci_device_intr_transfer: a request\n");
2860 #endif
2861
2862 len = xfer->length;
2863
2864 data = opipe->tail.td;
2865 tail = ohci_alloc_std(sc);
2866 if (tail == NULL)
2867 return (USBD_NOMEM);
2868 tail->xfer = NULL;
2869
2870 data->td.td_flags = htole32(
2871 OHCI_TD_IN | OHCI_TD_NOCC |
2872 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
2873 if (xfer->flags & USBD_SHORT_XFER_OK)
2874 data->td.td_flags |= htole32(OHCI_TD_R);
2875 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf));
2876 data->nexttd = tail;
2877 data->td.td_nexttd = htole32(tail->physaddr);
2878 data->td.td_be = htole32(le32toh(data->td.td_cbp) + len - 1);
2879 data->len = len;
2880 data->xfer = xfer;
2881 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
2882 xfer->hcpriv = data;
2883
2884 #ifdef OHCI_DEBUG
2885 if (ohcidebug > 5) {
2886 DPRINTF(("ohci_device_intr_transfer:\n"));
2887 ohci_dump_ed(sed);
2888 ohci_dump_tds(data);
2889 }
2890 #endif
2891
2892 /* Insert ED in schedule */
2893 s = splusb();
2894 sed->ed.ed_tailp = htole32(tail->physaddr);
2895 opipe->tail.td = tail;
2896 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
2897
2898 #if 0
2899 /*
2900 * This goes horribly wrong, printing thousands of descriptors,
2901 * because false references are followed due to the fact that the
2902 * TD is gone.
2903 */
2904 if (ohcidebug > 5) {
2905 usb_delay_ms(&sc->sc_bus, 5);
2906 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2907 OREAD4(sc, OHCI_COMMAND_STATUS)));
2908 ohci_dump_ed(sed);
2909 ohci_dump_tds(data);
2910 }
2911 #endif
2912 splx(s);
2913
2914 return (USBD_IN_PROGRESS);
2915 }
2916
2917 /* Abort a device control request. */
2918 Static void
2919 ohci_device_intr_abort(usbd_xfer_handle xfer)
2920 {
2921 if (xfer->pipe->intrxfer == xfer) {
2922 DPRINTF(("ohci_device_intr_abort: remove\n"));
2923 xfer->pipe->intrxfer = NULL;
2924 }
2925 ohci_abort_xfer(xfer, USBD_CANCELLED);
2926 }
2927
2928 /* Close a device interrupt pipe. */
2929 Static void
2930 ohci_device_intr_close(usbd_pipe_handle pipe)
2931 {
2932 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2933 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2934 int nslots = opipe->u.intr.nslots;
2935 int pos = opipe->u.intr.pos;
2936 int j;
2937 ohci_soft_ed_t *p, *sed = opipe->sed;
2938 int s;
2939
2940 DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n",
2941 pipe, nslots, pos));
2942 s = splusb();
2943 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
2944 if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2945 (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
2946 usb_delay_ms(&sc->sc_bus, 2);
2947
2948 for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
2949 ;
2950 #ifdef DIAGNOSTIC
2951 if (p == NULL)
2952 panic("ohci_device_intr_close: ED not found\n");
2953 #endif
2954 p->next = sed->next;
2955 p->ed.ed_nexted = sed->ed.ed_nexted;
2956 splx(s);
2957
2958 for (j = 0; j < nslots; j++)
2959 --sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS];
2960
2961 ohci_free_std(sc, opipe->tail.td);
2962 ohci_free_sed(sc, opipe->sed);
2963 }
2964
2965 Static usbd_status
2966 ohci_device_setintr(ohci_softc_t *sc, struct ohci_pipe *opipe, int ival)
2967 {
2968 int i, j, s, best;
2969 u_int npoll, slow, shigh, nslots;
2970 u_int bestbw, bw;
2971 ohci_soft_ed_t *hsed, *sed = opipe->sed;
2972
2973 DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe));
2974 if (ival == 0) {
2975 printf("ohci_setintr: 0 interval\n");
2976 return (USBD_INVAL);
2977 }
2978
2979 npoll = OHCI_NO_INTRS;
2980 while (npoll > ival)
2981 npoll /= 2;
2982 DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll));
2983
2984 /*
2985 * We now know which level in the tree the ED must go into.
2986 * Figure out which slot has most bandwidth left over.
2987 * Slots to examine:
2988 * npoll
2989 * 1 0
2990 * 2 1 2
2991 * 4 3 4 5 6
2992 * 8 7 8 9 10 11 12 13 14
2993 * N (N-1) .. (N-1+N-1)
2994 */
2995 slow = npoll-1;
2996 shigh = slow + npoll;
2997 nslots = OHCI_NO_INTRS / npoll;
2998 for (best = i = slow, bestbw = ~0; i < shigh; i++) {
2999 bw = 0;
3000 for (j = 0; j < nslots; j++)
3001 bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
3002 if (bw < bestbw) {
3003 best = i;
3004 bestbw = bw;
3005 }
3006 }
3007 DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n",
3008 best, slow, shigh, bestbw));
3009
3010 s = splusb();
3011 hsed = sc->sc_eds[best];
3012 sed->next = hsed->next;
3013 sed->ed.ed_nexted = hsed->ed.ed_nexted;
3014 hsed->next = sed;
3015 hsed->ed.ed_nexted = htole32(sed->physaddr);
3016 splx(s);
3017
3018 for (j = 0; j < nslots; j++)
3019 ++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
3020 opipe->u.intr.nslots = nslots;
3021 opipe->u.intr.pos = best;
3022
3023 DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe));
3024 return (USBD_NORMAL_COMPLETION);
3025 }
3026
3027 /***********************/
3028
3029 usbd_status
3030 ohci_device_isoc_transfer(usbd_xfer_handle xfer)
3031 {
3032 usbd_status err;
3033
3034 DPRINTFN(5,("ohci_device_isoc_transfer: xfer=%p\n", xfer));
3035
3036 /* Put it on our queue, */
3037 err = usb_insert_transfer(xfer);
3038
3039 /* bail out on error, */
3040 if (err && err != USBD_IN_PROGRESS)
3041 return (err);
3042
3043 /* XXX should check inuse here */
3044
3045 /* insert into schedule, */
3046 ohci_device_isoc_enter(xfer);
3047
3048 /* and start if the pipe wasn't running */
3049 if (!err)
3050 ohci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
3051
3052 return (err);
3053 }
3054
3055 void
3056 ohci_device_isoc_enter(usbd_xfer_handle xfer)
3057 {
3058 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3059 usbd_device_handle dev = opipe->pipe.device;
3060 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
3061 ohci_soft_ed_t *sed = opipe->sed;
3062 struct iso *iso = &opipe->u.iso;
3063 ohci_soft_itd_t *sitd, *nsitd;
3064 ohci_physaddr_t buf, offs, noffs, bp0;
3065 int i, ncur, nframes;
3066 int s;
3067
3068 DPRINTFN(1,("ohci_device_isoc_enter: used=%d next=%d xfer=%p "
3069 "nframes=%d\n",
3070 iso->inuse, iso->next, xfer, xfer->nframes));
3071
3072 if (sc->sc_dying)
3073 return;
3074
3075 if (iso->next == -1) {
3076 /* Not in use yet, schedule it a few frames ahead. */
3077 iso->next = le32toh(sc->sc_hcca->hcca_frame_number) + 5;
3078 DPRINTFN(2,("ohci_device_isoc_enter: start next=%d\n",
3079 iso->next));
3080 }
3081
3082 sitd = opipe->tail.itd;
3083 buf = DMAADDR(&xfer->dmabuf);
3084 bp0 = OHCI_PAGE(buf);
3085 offs = OHCI_PAGE_OFFSET(buf);
3086 nframes = xfer->nframes;
3087 xfer->hcpriv = sitd;
3088 for (i = ncur = 0; i < nframes; i++, ncur++) {
3089 noffs = offs + xfer->frlengths[i];
3090 if (ncur == OHCI_ITD_NOFFSET || /* all offsets used */
3091 OHCI_PAGE(buf + noffs) > bp0 + OHCI_PAGE_SIZE) { /* too many page crossings */
3092
3093 /* Allocate next ITD */
3094 nsitd = ohci_alloc_sitd(sc);
3095 if (nsitd == NULL) {
3096 /* XXX what now? */
3097 printf("%s: isoc TD alloc failed\n",
3098 USBDEVNAME(sc->sc_bus.bdev));
3099 return;
3100 }
3101
3102 /* Fill current ITD */
3103 sitd->itd.itd_flags = htole32(
3104 OHCI_ITD_NOCC |
3105 OHCI_ITD_SET_SF(iso->next) |
3106 OHCI_ITD_SET_DI(6) | /* delay intr a little */
3107 OHCI_ITD_SET_FC(ncur));
3108 sitd->itd.itd_bp0 = htole32(bp0);
3109 sitd->nextitd = nsitd;
3110 sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
3111 sitd->itd.itd_be = htole32(bp0 + offs - 1);
3112 sitd->xfer = xfer;
3113 sitd->flags = 0;
3114
3115 sitd = nsitd;
3116 iso->next = iso->next + ncur;
3117 bp0 = OHCI_PAGE(buf + offs);
3118 ncur = 0;
3119 }
3120 sitd->itd.itd_offset[ncur] = htole16(OHCI_ITD_MK_OFFS(offs));
3121 offs = noffs;
3122 }
3123 nsitd = ohci_alloc_sitd(sc);
3124 if (nsitd == NULL) {
3125 /* XXX what now? */
3126 printf("%s: isoc TD alloc failed\n",
3127 USBDEVNAME(sc->sc_bus.bdev));
3128 return;
3129 }
3130 /* Fixup last used ITD */
3131 sitd->itd.itd_flags = htole32(
3132 OHCI_ITD_NOCC |
3133 OHCI_ITD_SET_SF(iso->next) |
3134 OHCI_ITD_SET_DI(0) |
3135 OHCI_ITD_SET_FC(ncur));
3136 sitd->itd.itd_bp0 = htole32(bp0);
3137 sitd->nextitd = nsitd;
3138 sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
3139 sitd->itd.itd_be = htole32(bp0 + offs - 1);
3140 sitd->xfer = xfer;
3141 sitd->flags = OHCI_CALL_DONE;
3142
3143 iso->next = iso->next + ncur;
3144 iso->inuse += nframes;
3145
3146 xfer->actlen = offs; /* XXX pretend we did it all */
3147
3148 xfer->status = USBD_IN_PROGRESS;
3149
3150 #ifdef OHCI_DEBUG
3151 if (ohcidebug > 5) {
3152 DPRINTF(("ohci_device_isoc_enter: frame=%d\n",
3153 le32toh(sc->sc_hcca->hcca_frame_number)));
3154 ohci_dump_itds(xfer->hcpriv);
3155 ohci_dump_ed(sed);
3156 }
3157 #endif
3158
3159 s = splusb();
3160 opipe->tail.itd = nsitd;
3161 sed->ed.ed_tailp = htole32(nsitd->physaddr);
3162 splx(s);
3163
3164 #ifdef OHCI_DEBUG
3165 if (ohcidebug > 5) {
3166 delay(150000);
3167 DPRINTF(("ohci_device_isoc_enter: after frame=%d\n",
3168 le32toh(sc->sc_hcca->hcca_frame_number)));
3169 ohci_dump_itds(xfer->hcpriv);
3170 ohci_dump_ed(sed);
3171 }
3172 #endif
3173 }
3174
3175 usbd_status
3176 ohci_device_isoc_start(usbd_xfer_handle xfer)
3177 {
3178 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3179 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3180
3181 DPRINTFN(5,("ohci_device_isoc_start: xfer=%p\n", xfer));
3182
3183 if (sc->sc_dying)
3184 return (USBD_IOERROR);
3185
3186 #ifdef DIAGNOSTIC
3187 if (xfer->status != USBD_IN_PROGRESS)
3188 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
3189 #endif
3190
3191 /* XXX anything to do? */
3192
3193 return (USBD_IN_PROGRESS);
3194 }
3195
3196 void
3197 ohci_device_isoc_abort(usbd_xfer_handle xfer)
3198 {
3199 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3200 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3201 ohci_soft_ed_t *sed;
3202 ohci_soft_itd_t *sitd;
3203 int s;
3204
3205 s = splusb();
3206
3207 DPRINTFN(1,("ohci_device_isoc_abort: xfer=%p\n", xfer));
3208
3209 /* Transfer is already done. */
3210 if (xfer->status != USBD_NOT_STARTED &&
3211 xfer->status != USBD_IN_PROGRESS) {
3212 splx(s);
3213 printf("ohci_device_isoc_abort: early return\n");
3214 return;
3215 }
3216
3217 /* Give xfer the requested abort code. */
3218 xfer->status = USBD_CANCELLED;
3219
3220 sed = opipe->sed;
3221 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
3222
3223 sitd = xfer->hcpriv;
3224 #ifdef DIAGNOSTIC
3225 if (sitd == NULL) {
3226 splx(s);
3227 printf("ohci_device_isoc_abort: hcpriv==0\n");
3228 return;
3229 }
3230 #endif
3231 for (; sitd->xfer == xfer; sitd = sitd->nextitd) {
3232 #ifdef DIAGNOSTIC
3233 DPRINTFN(1,("abort sets done sitd=%p\n", sitd));
3234 sitd->isdone = 1;
3235 #endif
3236 }
3237
3238 splx(s);
3239
3240 usb_delay_ms(&sc->sc_bus, OHCI_ITD_NOFFSET);
3241
3242 s = splusb();
3243
3244 /* Run callback. */
3245 usb_transfer_complete(xfer);
3246
3247 sed->ed.ed_headp = htole32(sitd->physaddr); /* unlink TDs */
3248 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
3249
3250 splx(s);
3251 }
3252
3253 void
3254 ohci_device_isoc_done(usbd_xfer_handle xfer)
3255 {
3256 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3257 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3258 ohci_soft_itd_t *sitd, *nsitd;
3259
3260 DPRINTFN(1,("ohci_device_isoc_done: xfer=%p\n", xfer));
3261
3262 for (sitd = xfer->hcpriv;
3263 !(sitd->flags & OHCI_CALL_DONE);
3264 sitd = nsitd) {
3265 nsitd = sitd->nextitd;
3266 DPRINTFN(1,("ohci_device_isoc_done: free sitd=%p\n", sitd));
3267 ohci_free_sitd(sc, sitd);
3268 }
3269 ohci_free_sitd(sc, sitd);
3270 xfer->hcpriv = NULL;
3271 }
3272
3273 usbd_status
3274 ohci_setup_isoc(usbd_pipe_handle pipe)
3275 {
3276 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3277 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3278 struct iso *iso = &opipe->u.iso;
3279 int s;
3280
3281 iso->next = -1;
3282 iso->inuse = 0;
3283
3284 s = splusb();
3285 ohci_add_ed(opipe->sed, sc->sc_isoc_head);
3286 splx(s);
3287
3288 return (USBD_NORMAL_COMPLETION);
3289 }
3290
3291 void
3292 ohci_device_isoc_close(usbd_pipe_handle pipe)
3293 {
3294 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3295 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3296 int s;
3297
3298 DPRINTF(("ohci_device_isoc_close: pipe=%p\n", pipe));
3299
3300 s = splusb();
3301 ohci_rem_ed(opipe->sed, sc->sc_isoc_head);
3302 splx(s);
3303 ohci_close_pipe(pipe, sc->sc_isoc_head);
3304 #ifdef DIAGNOSTIC
3305 opipe->tail.itd->isdone = 1;
3306 #endif
3307 ohci_free_sitd(sc, opipe->tail.itd);
3308 }
3309