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