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