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