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