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