ohci.c revision 1.30 1 /* $NetBSD: ohci.c,v 1.30 1999/05/21 10:15:23 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) carlstedt.se) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * USB Open Host Controller driver.
42 *
43 * OHCI spec: ftp://ftp.compaq.com/pub/supportinformation/papers/hcir1_0a.exe
44 * USB spec: http://www.usb.org/developers/data/usb11.pdf
45 */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #if defined(__NetBSD__)
52 #include <sys/device.h>
53 #elif defined(__FreeBSD__)
54 #include <sys/module.h>
55 #include <sys/bus.h>
56 #endif
57 #include <sys/proc.h>
58 #include <sys/queue.h>
59 #include <sys/select.h>
60
61 #include <machine/bus.h>
62 #include <machine/endian.h>
63
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdivar.h>
67 #include <dev/usb/usb_quirks.h>
68 #include <dev/usb/usb_mem.h>
69
70 #include <dev/usb/ohcireg.h>
71 #include <dev/usb/ohcivar.h>
72
73 #if defined(__FreeBSD__)
74 #include <machine/clock.h>
75
76 #define delay(d) DELAY(d)
77
78 #endif
79
80 /*
81 * The OHCI controller is little endian, so on big endian machines
82 * the data strored in memory needs to be swapped.
83 */
84 #if BYTE_ORDER == BIG_ENDIAN
85 #define LE(x) (bswap32(x))
86 #else
87 #define LE(x) (x)
88 #endif
89
90 struct ohci_pipe;
91
92 ohci_soft_ed_t *ohci_alloc_sed __P((ohci_softc_t *));
93 void ohci_free_sed __P((ohci_softc_t *, ohci_soft_ed_t *));
94
95 ohci_soft_td_t *ohci_alloc_std __P((ohci_softc_t *));
96 void ohci_free_std __P((ohci_softc_t *, ohci_soft_td_t *));
97
98 usbd_status ohci_open __P((usbd_pipe_handle));
99 void ohci_poll __P((struct usbd_bus *));
100 void ohci_waitintr __P((ohci_softc_t *, usbd_request_handle));
101 void ohci_rhsc __P((ohci_softc_t *, usbd_request_handle));
102 void ohci_process_done __P((ohci_softc_t *, ohci_physaddr_t));
103 void ohci_ii_done __P((ohci_softc_t *, usbd_request_handle));
104 void ohci_ctrl_done __P((ohci_softc_t *, usbd_request_handle));
105 void ohci_intr_done __P((ohci_softc_t *, usbd_request_handle));
106 void ohci_bulk_done __P((ohci_softc_t *, usbd_request_handle));
107
108 usbd_status ohci_device_request __P((usbd_request_handle reqh));
109 void ohci_add_ed __P((ohci_soft_ed_t *, ohci_soft_ed_t *));
110 void ohci_rem_ed __P((ohci_soft_ed_t *, ohci_soft_ed_t *));
111 void ohci_hash_add_td __P((ohci_softc_t *, ohci_soft_td_t *));
112 void ohci_hash_rem_td __P((ohci_softc_t *, ohci_soft_td_t *));
113 ohci_soft_td_t *ohci_hash_find_td __P((ohci_softc_t *, ohci_physaddr_t));
114
115 usbd_status ohci_root_ctrl_transfer __P((usbd_request_handle));
116 usbd_status ohci_root_ctrl_start __P((usbd_request_handle));
117 void ohci_root_ctrl_abort __P((usbd_request_handle));
118 void ohci_root_ctrl_close __P((usbd_pipe_handle));
119
120 usbd_status ohci_root_intr_transfer __P((usbd_request_handle));
121 usbd_status ohci_root_intr_start __P((usbd_request_handle));
122 void ohci_root_intr_abort __P((usbd_request_handle));
123 void ohci_root_intr_close __P((usbd_pipe_handle));
124
125 usbd_status ohci_device_ctrl_transfer __P((usbd_request_handle));
126 usbd_status ohci_device_ctrl_start __P((usbd_request_handle));
127 void ohci_device_ctrl_abort __P((usbd_request_handle));
128 void ohci_device_ctrl_close __P((usbd_pipe_handle));
129
130 usbd_status ohci_device_bulk_transfer __P((usbd_request_handle));
131 usbd_status ohci_device_bulk_start __P((usbd_request_handle));
132 void ohci_device_bulk_abort __P((usbd_request_handle));
133 void ohci_device_bulk_close __P((usbd_pipe_handle));
134
135 usbd_status ohci_device_intr_transfer __P((usbd_request_handle));
136 usbd_status ohci_device_intr_start __P((usbd_request_handle));
137 void ohci_device_intr_abort __P((usbd_request_handle));
138 void ohci_device_intr_close __P((usbd_pipe_handle));
139 usbd_status ohci_device_setintr __P((ohci_softc_t *sc,
140 struct ohci_pipe *pipe, int ival));
141
142 int ohci_str __P((usb_string_descriptor_t *, int, char *));
143
144 void ohci_timeout __P((void *));
145 void ohci_rhsc_able __P((ohci_softc_t *, int));
146
147 #ifdef USB_DEBUG
148 ohci_softc_t *thesc;
149 void ohci_dumpregs __P((ohci_softc_t *));
150 void ohci_dump_tds __P((ohci_soft_td_t *));
151 void ohci_dump_td __P((ohci_soft_td_t *));
152 void ohci_dump_ed __P((ohci_soft_ed_t *));
153 #endif
154
155 #if defined(__NetBSD__)
156 #define OWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
157 #define OREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
158 #define OREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
159 #elif defined(__FreeBSD__)
160 #define OWRITE4(sc, r, x) *(u_int32_t *) ((sc)->sc_iobase + (r)) = x
161 #define OREAD4(sc, r) (*(u_int32_t *) ((sc)->sc_iobase + (r)))
162 #define OREAD2(sc, r) (*(u_int16_t *) ((sc)->sc_iobase + (r)))
163 #endif
164
165 /* Reverse the bits in a value 0 .. 31 */
166 static u_int8_t revbits[OHCI_NO_INTRS] =
167 { 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c,
168 0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e,
169 0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d,
170 0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f };
171
172 struct ohci_pipe {
173 struct usbd_pipe pipe;
174 ohci_soft_ed_t *sed;
175 ohci_soft_td_t *tail;
176 /* Info needed for different pipe kinds. */
177 union {
178 /* Control pipe */
179 struct {
180 usb_dma_t datadma;
181 usb_dma_t reqdma;
182 u_int length;
183 ohci_soft_td_t *setup, *xfer, *stat;
184 } ctl;
185 /* Interrupt pipe */
186 struct {
187 usb_dma_t datadma;
188 int nslots;
189 int pos;
190 } intr;
191 /* Bulk pipe */
192 struct {
193 usb_dma_t datadma;
194 u_int length;
195 } bulk;
196 } u;
197 };
198
199 #define OHCI_INTR_ENDPT 1
200
201 struct usbd_methods ohci_root_ctrl_methods = {
202 ohci_root_ctrl_transfer,
203 ohci_root_ctrl_start,
204 ohci_root_ctrl_abort,
205 ohci_root_ctrl_close,
206 0,
207 };
208
209 struct usbd_methods ohci_root_intr_methods = {
210 ohci_root_intr_transfer,
211 ohci_root_intr_start,
212 ohci_root_intr_abort,
213 ohci_root_intr_close,
214 0,
215 };
216
217 struct usbd_methods ohci_device_ctrl_methods = {
218 ohci_device_ctrl_transfer,
219 ohci_device_ctrl_start,
220 ohci_device_ctrl_abort,
221 ohci_device_ctrl_close,
222 0,
223 };
224
225 struct usbd_methods ohci_device_intr_methods = {
226 ohci_device_intr_transfer,
227 ohci_device_intr_start,
228 ohci_device_intr_abort,
229 ohci_device_intr_close,
230 };
231
232 struct usbd_methods ohci_device_bulk_methods = {
233 ohci_device_bulk_transfer,
234 ohci_device_bulk_start,
235 ohci_device_bulk_abort,
236 ohci_device_bulk_close,
237 0,
238 };
239
240 ohci_soft_ed_t *
241 ohci_alloc_sed(sc)
242 ohci_softc_t *sc;
243 {
244 ohci_soft_ed_t *sed;
245 usbd_status r;
246 int i, offs;
247 usb_dma_t dma;
248
249 if (!sc->sc_freeeds) {
250 DPRINTFN(2, ("ohci_alloc_sed: allocating chunk\n"));
251 sed = malloc(sizeof(ohci_soft_ed_t) * OHCI_ED_CHUNK,
252 M_USBDEV, M_NOWAIT);
253 if (!sed)
254 return 0;
255 r = usb_allocmem(sc->sc_dmatag, OHCI_ED_SIZE * OHCI_ED_CHUNK,
256 OHCI_ED_ALIGN, &dma);
257 if (r != USBD_NORMAL_COMPLETION) {
258 free(sed, M_USBDEV);
259 return 0;
260 }
261 for(i = 0; i < OHCI_ED_CHUNK; i++, sed++) {
262 offs = i * OHCI_ED_SIZE;
263 sed->physaddr = DMAADDR(&dma) + offs;
264 sed->ed = (ohci_ed_t *)
265 ((char *)KERNADDR(&dma) + offs);
266 sed->next = sc->sc_freeeds;
267 sc->sc_freeeds = sed;
268 }
269 }
270 sed = sc->sc_freeeds;
271 sc->sc_freeeds = sed->next;
272 memset(sed->ed, 0, OHCI_ED_SIZE);
273 sed->next = 0;
274 return sed;
275 }
276
277 void
278 ohci_free_sed(sc, sed)
279 ohci_softc_t *sc;
280 ohci_soft_ed_t *sed;
281 {
282 sed->next = sc->sc_freeeds;
283 sc->sc_freeeds = sed;
284 }
285
286 ohci_soft_td_t *
287 ohci_alloc_std(sc)
288 ohci_softc_t *sc;
289 {
290 ohci_soft_td_t *std;
291 usbd_status r;
292 int i, offs;
293 usb_dma_t dma;
294
295 if (!sc->sc_freetds) {
296 DPRINTFN(2, ("ohci_alloc_std: allocating chunk\n"));
297 std = malloc(sizeof(ohci_soft_td_t) * OHCI_TD_CHUNK,
298 M_USBDEV, M_NOWAIT);
299 if (!std)
300 return 0;
301 r = usb_allocmem(sc->sc_dmatag, OHCI_TD_SIZE * OHCI_TD_CHUNK,
302 OHCI_TD_ALIGN, &dma);
303 if (r != USBD_NORMAL_COMPLETION) {
304 free(std, M_USBDEV);
305 return 0;
306 }
307 for(i = 0; i < OHCI_TD_CHUNK; i++, std++) {
308 offs = i * OHCI_TD_SIZE;
309 std->physaddr = DMAADDR(&dma) + offs;
310 std->td = (ohci_td_t *)
311 ((char *)KERNADDR(&dma) + offs);
312 std->nexttd = sc->sc_freetds;
313 sc->sc_freetds = std;
314 }
315 }
316 std = sc->sc_freetds;
317 sc->sc_freetds = std->nexttd;
318 memset(std->td, 0, OHCI_TD_SIZE);
319 std->nexttd = 0;
320 return (std);
321 }
322
323 void
324 ohci_free_std(sc, std)
325 ohci_softc_t *sc;
326 ohci_soft_td_t *std;
327 {
328 std->nexttd = sc->sc_freetds;
329 sc->sc_freetds = std;
330 }
331
332 usbd_status
333 ohci_init(sc)
334 ohci_softc_t *sc;
335 {
336 ohci_soft_ed_t *sed, *psed;
337 usbd_status r;
338 int rev;
339 int i;
340 u_int32_t s, ctl, ival, hcr, fm, per;
341
342 DPRINTF(("ohci_init: start\n"));
343 rev = OREAD4(sc, OHCI_REVISION);
344 printf("%s: OHCI version %d.%d%s\n", USBDEVNAME(sc->sc_bus.bdev),
345 OHCI_REV_HI(rev), OHCI_REV_LO(rev),
346 OHCI_REV_LEGACY(rev) ? ", legacy support" : "");
347 if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
348 printf("%s: unsupported OHCI revision\n",
349 USBDEVNAME(sc->sc_bus.bdev));
350 return (USBD_INVAL);
351 }
352
353 for (i = 0; i < OHCI_HASH_SIZE; i++)
354 LIST_INIT(&sc->sc_hash_tds[i]);
355
356 /* Allocate the HCCA area. */
357 r = usb_allocmem(sc->sc_dmatag, OHCI_HCCA_SIZE,
358 OHCI_HCCA_ALIGN, &sc->sc_hccadma);
359 if (r != USBD_NORMAL_COMPLETION)
360 return (r);
361 sc->sc_hcca = (struct ohci_hcca *)KERNADDR(&sc->sc_hccadma);
362 memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);
363
364 sc->sc_eintrs = OHCI_NORMAL_INTRS;
365
366 sc->sc_ctrl_head = ohci_alloc_sed(sc);
367 if (!sc->sc_ctrl_head) {
368 r = USBD_NOMEM;
369 goto bad1;
370 }
371 sc->sc_ctrl_head->ed->ed_flags |= LE(OHCI_ED_SKIP);
372 sc->sc_bulk_head = ohci_alloc_sed(sc);
373 if (!sc->sc_bulk_head) {
374 r = USBD_NOMEM;
375 goto bad2;
376 }
377 sc->sc_bulk_head->ed->ed_flags |= LE(OHCI_ED_SKIP);
378
379 /* Allocate all the dummy EDs that make up the interrupt tree. */
380 for (i = 0; i < OHCI_NO_EDS; i++) {
381 sed = ohci_alloc_sed(sc);
382 if (!sed) {
383 while (--i >= 0)
384 ohci_free_sed(sc, sc->sc_eds[i]);
385 r = USBD_NOMEM;
386 goto bad3;
387 }
388 /* All ED fields are set to 0. */
389 sc->sc_eds[i] = sed;
390 sed->ed->ed_flags |= LE(OHCI_ED_SKIP);
391 if (i != 0) {
392 psed = sc->sc_eds[(i-1) / 2];
393 sed->next = psed;
394 sed->ed->ed_nexted = LE(psed->physaddr);
395 }
396 }
397 /*
398 * Fill HCCA interrupt table. The bit reversal is to get
399 * the tree set up properly to spread the interrupts.
400 */
401 for (i = 0; i < OHCI_NO_INTRS; i++)
402 sc->sc_hcca->hcca_interrupt_table[revbits[i]] =
403 LE(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr);
404
405 /* Determine in what context we are running. */
406 ctl = OREAD4(sc, OHCI_CONTROL);
407 if (ctl & OHCI_IR) {
408 /* SMM active, request change */
409 DPRINTF(("ohci_init: SMM active, request owner change\n"));
410 s = OREAD4(sc, OHCI_COMMAND_STATUS);
411 OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR);
412 for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
413 delay(1000);
414 ctl = OREAD4(sc, OHCI_CONTROL);
415 }
416 if ((ctl & OHCI_IR) == 0) {
417 printf("%s: SMM does not respond, resetting\n",
418 USBDEVNAME(sc->sc_bus.bdev));
419 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
420 goto reset;
421 }
422 } else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) {
423 /* BIOS started controller. */
424 DPRINTF(("ohci_init: BIOS active\n"));
425 if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) {
426 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL);
427 delay(USB_RESUME_DELAY * 1000);
428 }
429 } else {
430 DPRINTF(("ohci_init: cold started\n"));
431 reset:
432 /* Controller was cold started. */
433 delay(USB_BUS_RESET_DELAY * 1000);
434 }
435
436 /*
437 * This reset should not be necessary according to the OHCI spec, but
438 * without it some controllers do not start.
439 */
440 DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
441 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
442 delay(USB_BUS_RESET_DELAY * 1000);
443
444 /* We now own the host controller and the bus has been reset. */
445 ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
446
447 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
448 /* Nominal time for a reset is 10 us. */
449 for (i = 0; i < 10; i++) {
450 delay(10);
451 hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
452 if (!hcr)
453 break;
454 }
455 if (hcr) {
456 printf("%s: reset timeout\n", USBDEVNAME(sc->sc_bus.bdev));
457 r = USBD_IOERROR;
458 goto bad3;
459 }
460 #ifdef USB_DEBUG
461 thesc = sc;
462 if (ohcidebug > 15)
463 ohci_dumpregs(sc);
464 #endif
465
466 /* The controller is now in suspend state, we have 2ms to finish. */
467
468 /* Set up HC registers. */
469 OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma));
470 OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
471 OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
472 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
473 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);
474 ctl = OREAD4(sc, OHCI_CONTROL);
475 ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
476 ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
477 OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
478 /* And finally start it! */
479 OWRITE4(sc, OHCI_CONTROL, ctl);
480
481 /*
482 * The controller is now OPERATIONAL. Set a some final
483 * registers that should be set earlier, but that the
484 * controller ignores when in the SUSPEND state.
485 */
486 fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
487 fm |= OHCI_FSMPS(ival) | ival;
488 OWRITE4(sc, OHCI_FM_INTERVAL, fm);
489 per = OHCI_PERIODIC(ival); /* 90% periodic */
490 OWRITE4(sc, OHCI_PERIODIC_START, per);
491
492 OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */
493
494 sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
495
496 #ifdef USB_DEBUG
497 if (ohcidebug > 5)
498 ohci_dumpregs(sc);
499 #endif
500
501 /* Set up the bus struct. */
502 sc->sc_bus.open_pipe = ohci_open;
503 sc->sc_bus.pipe_size = sizeof(struct ohci_pipe);
504 sc->sc_bus.do_poll = ohci_poll;
505
506 return (USBD_NORMAL_COMPLETION);
507
508 bad3:
509 ohci_free_sed(sc, sc->sc_ctrl_head);
510 bad2:
511 ohci_free_sed(sc, sc->sc_bulk_head);
512 bad1:
513 usb_freemem(sc->sc_dmatag, &sc->sc_hccadma);
514 return (r);
515 }
516
517 #ifdef USB_DEBUG
518 void ohcidump(void);
519 void ohcidump(void) { ohci_dumpregs(thesc); }
520
521 void
522 ohci_dumpregs(sc)
523 ohci_softc_t *sc;
524 {
525 printf("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
526 OREAD4(sc, OHCI_REVISION),
527 OREAD4(sc, OHCI_CONTROL),
528 OREAD4(sc, OHCI_COMMAND_STATUS));
529 printf(" intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
530 OREAD4(sc, OHCI_INTERRUPT_STATUS),
531 OREAD4(sc, OHCI_INTERRUPT_ENABLE),
532 OREAD4(sc, OHCI_INTERRUPT_DISABLE));
533 printf(" hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
534 OREAD4(sc, OHCI_HCCA),
535 OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
536 OREAD4(sc, OHCI_CONTROL_HEAD_ED));
537 printf(" ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
538 OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
539 OREAD4(sc, OHCI_BULK_HEAD_ED),
540 OREAD4(sc, OHCI_BULK_CURRENT_ED));
541 printf(" done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
542 OREAD4(sc, OHCI_DONE_HEAD),
543 OREAD4(sc, OHCI_FM_INTERVAL),
544 OREAD4(sc, OHCI_FM_REMAINING));
545 printf(" fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
546 OREAD4(sc, OHCI_FM_NUMBER),
547 OREAD4(sc, OHCI_PERIODIC_START),
548 OREAD4(sc, OHCI_LS_THRESHOLD));
549 printf(" desca=0x%08x descb=0x%08x stat=0x%08x\n",
550 OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
551 OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
552 OREAD4(sc, OHCI_RH_STATUS));
553 printf(" port1=0x%08x port2=0x%08x\n",
554 OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
555 OREAD4(sc, OHCI_RH_PORT_STATUS(2)));
556 printf(" HCCA: frame_number=0x%04x done_head=0x%08x\n",
557 LE(sc->sc_hcca->hcca_frame_number),
558 LE(sc->sc_hcca->hcca_done_head));
559 }
560 #endif
561
562 int
563 ohci_intr(p)
564 void *p;
565 {
566 ohci_softc_t *sc = p;
567 u_int32_t intrs, eintrs;
568 ohci_physaddr_t done;
569
570 /* In case the interrupt occurs before initialization has completed. */
571 if (sc == NULL || sc->sc_hcca == NULL) { /* NWH added sc==0 */
572 #ifdef DIAGNOSTIC
573 printf("ohci_intr: sc->sc_hcca == NULL\n");
574 #endif
575 return (0);
576 }
577
578 intrs = 0;
579 done = LE(sc->sc_hcca->hcca_done_head);
580 if (done != 0) {
581 sc->sc_hcca->hcca_done_head = 0;
582 if (done & ~OHCI_DONE_INTRS)
583 intrs = OHCI_WDH;
584 if (done & OHCI_DONE_INTRS)
585 intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
586 } else
587 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS);
588 if (!intrs)
589 return (0);
590 intrs &= ~OHCI_MIE;
591 OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs); /* Acknowledge */
592 eintrs = intrs & sc->sc_eintrs;
593 if (!eintrs)
594 return (0);
595
596 sc->sc_intrs++;
597 DPRINTFN(7, ("ohci_intr: sc=%p intrs=%x(%x) eintr=%x\n",
598 sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS),
599 (u_int)eintrs));
600
601 if (eintrs & OHCI_SO) {
602 printf("%s: scheduling overrun\n",USBDEVNAME(sc->sc_bus.bdev));
603 /* XXX do what */
604 intrs &= ~OHCI_SO;
605 }
606 if (eintrs & OHCI_WDH) {
607 ohci_process_done(sc, done &~ OHCI_DONE_INTRS);
608 intrs &= ~OHCI_WDH;
609 }
610 if (eintrs & OHCI_RD) {
611 /* XXX process resume detect */
612 }
613 if (eintrs & OHCI_UE) {
614 printf("%s: unrecoverable error, controller halted\n",
615 USBDEVNAME(sc->sc_bus.bdev));
616 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
617 /* XXX what else */
618 }
619 if (eintrs & OHCI_RHSC) {
620 ohci_rhsc(sc, sc->sc_intrreqh);
621 intrs &= ~OHCI_RHSC;
622
623 /*
624 * Disable RHSC interrupt for now, because it will be
625 * on until the port has been reset.
626 */
627 ohci_rhsc_able(sc, 0);
628 }
629
630 /* Block unprocessed interrupts. XXX */
631 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, intrs);
632 sc->sc_eintrs &= ~intrs;
633
634 return (1);
635 }
636
637 void
638 ohci_rhsc_able(sc, on)
639 ohci_softc_t *sc;
640 int on;
641 {
642 DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on));
643 if (on) {
644 sc->sc_eintrs |= OHCI_RHSC;
645 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
646 } else {
647 sc->sc_eintrs &= ~OHCI_RHSC;
648 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
649 }
650 }
651
652 #ifdef USB_DEBUG
653 char *ohci_cc_strs[] = {
654 "NO_ERROR",
655 "CRC",
656 "BIT_STUFFING",
657 "DATA_TOGGLE_MISMATCH",
658 "STALL",
659 "DEVICE_NOT_RESPONDING",
660 "PID_CHECK_FAILURE",
661 "UNEXPECTED_PID",
662 "DATA_OVERRUN",
663 "DATA_UNDERRUN",
664 "BUFFER_OVERRUN",
665 "BUFFER_UNDERRUN",
666 "NOT_ACCESSED",
667 };
668 #endif
669
670 void
671 ohci_process_done(sc, done)
672 ohci_softc_t *sc;
673 ohci_physaddr_t done;
674 {
675 ohci_soft_td_t *std, *sdone;
676 usbd_request_handle reqh;
677 int len, cc;
678
679 DPRINTFN(10,("ohci_process_done: done=0x%08lx\n", (u_long)done));
680
681 /* Reverse the done list. */
682 for (sdone = 0; done; done = LE(std->td->td_nexttd)) {
683 std = ohci_hash_find_td(sc, done);
684 std->dnext = sdone;
685 sdone = std;
686 }
687
688 #ifdef USB_DEBUG
689 if (ohcidebug > 10) {
690 printf("ohci_process_done: TD done:\n");
691 ohci_dump_tds(sdone);
692 }
693 #endif
694
695 for (std = sdone; std; std = std->dnext) {
696 reqh = std->reqh;
697 DPRINTFN(10, ("ohci_process_done: std=%p reqh=%p hcpriv=%p\n",
698 std, reqh, reqh->hcpriv));
699 cc = OHCI_TD_GET_CC(LE(std->td->td_flags));
700 if (cc == OHCI_CC_NO_ERROR) {
701 if (std->td->td_cbp == 0)
702 len = std->len;
703 else
704 len = LE(std->td->td_be) -
705 LE(std->td->td_cbp) + 1;
706 /*
707 * Only do a callback on the last stage of a transfer.
708 * Others have hcpriv = 0.
709 */
710 if ((reqh->pipe->endpoint->edesc->bmAttributes &
711 UE_XFERTYPE) == UE_CONTROL) {
712 /* For a control transfer the length is in
713 * the xfer stage */
714 if (reqh->hcpriv == std) {
715 reqh->status = USBD_NORMAL_COMPLETION;
716 ohci_ii_done(sc, reqh);
717 } else
718 reqh->actlen = len;
719 } else {
720 if (reqh->hcpriv == std) {
721 reqh->actlen = len;
722 reqh->status = USBD_NORMAL_COMPLETION;
723 ohci_ii_done(sc, reqh);
724 }
725 }
726 } else {
727 ohci_soft_td_t *p, *n;
728 struct ohci_pipe *opipe =
729 (struct ohci_pipe *)reqh->pipe;
730 DPRINTFN(-1,("ohci_process_done: error cc=%d (%s)\n",
731 OHCI_TD_GET_CC(LE(std->td->td_flags)),
732 ohci_cc_strs[OHCI_TD_GET_CC(LE(std->td->td_flags))]));
733 /*
734 * Endpoint is halted. First unlink all the TDs
735 * belonging to the failed transfer, and then restart
736 * the endpoint.
737 */
738 for (p = std->nexttd; p->reqh == reqh; p = n) {
739 n = p->nexttd;
740 ohci_hash_rem_td(sc, p);
741 ohci_free_std(sc, p);
742 }
743 /* clear halt */
744 opipe->sed->ed->ed_headp = LE(p->physaddr);
745 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
746
747 if (cc == OHCI_CC_STALL)
748 reqh->status = USBD_STALLED;
749 else
750 reqh->status = USBD_IOERROR;
751 ohci_ii_done(sc, reqh);
752 }
753 ohci_hash_rem_td(sc, std);
754 ohci_free_std(sc, std);
755 }
756 }
757
758 void
759 ohci_ii_done(sc, reqh)
760 ohci_softc_t *sc;
761 usbd_request_handle reqh;
762 {
763 switch (reqh->pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
764 case UE_CONTROL:
765 ohci_ctrl_done(sc, reqh);
766 usb_start_next(reqh->pipe);
767 break;
768 case UE_INTERRUPT:
769 ohci_intr_done(sc, reqh);
770 break;
771 case UE_BULK:
772 ohci_bulk_done(sc, reqh);
773 usb_start_next(reqh->pipe);
774 break;
775 case UE_ISOCHRONOUS:
776 printf("ohci_process_done: ISO done?\n");
777 usb_start_next(reqh->pipe);
778 break;
779 }
780
781 /* And finally execute callback. */
782 reqh->xfercb(reqh);
783 }
784
785 void
786 ohci_ctrl_done(sc, reqh)
787 ohci_softc_t *sc;
788 usbd_request_handle reqh;
789 {
790 struct ohci_pipe *opipe = (struct ohci_pipe *)reqh->pipe;
791 u_int len = opipe->u.ctl.length;
792 usb_dma_t *dma;
793
794 DPRINTFN(10,("ohci_ctrl_done: reqh=%p\n", reqh));
795
796 if (!reqh->isreq) {
797 panic("ohci_ctrl_done: not a request\n");
798 return;
799 }
800
801 if (len != 0) {
802 dma = &opipe->u.ctl.datadma;
803 if (reqh->request.bmRequestType & UT_READ)
804 memcpy(reqh->buffer, KERNADDR(dma), len);
805 usb_freemem(sc->sc_dmatag, dma);
806 }
807 usb_untimeout(ohci_timeout, reqh, reqh->timo_handle);
808 }
809
810 void
811 ohci_intr_done(sc, reqh)
812 ohci_softc_t *sc;
813 usbd_request_handle reqh;
814 {
815 struct ohci_pipe *opipe = (struct ohci_pipe *)reqh->pipe;
816 usb_dma_t *dma;
817 ohci_soft_ed_t *sed = opipe->sed;
818 ohci_soft_td_t *xfer, *tail;
819
820
821 DPRINTFN(10,("ohci_intr_done: reqh=%p, actlen=%d\n",
822 reqh, reqh->actlen));
823
824 dma = &opipe->u.intr.datadma;
825 memcpy(reqh->buffer, KERNADDR(dma), reqh->actlen);
826
827 if (reqh->pipe->intrreqh == reqh) {
828 xfer = opipe->tail;
829 tail = ohci_alloc_std(sc); /* XXX should reuse TD */
830 if (!tail) {
831 reqh->status = USBD_NOMEM;
832 return;
833 }
834 tail->reqh = 0;
835
836 xfer->td->td_flags = LE(
837 OHCI_TD_IN | OHCI_TD_NOCC |
838 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
839 if (reqh->flags & USBD_SHORT_XFER_OK)
840 xfer->td->td_flags |= LE(OHCI_TD_R);
841 xfer->td->td_cbp = LE(DMAADDR(dma));
842 xfer->nexttd = tail;
843 xfer->td->td_nexttd = LE(tail->physaddr);
844 xfer->td->td_be = LE(LE(xfer->td->td_cbp) + reqh->length - 1);
845 xfer->len = reqh->length;
846 xfer->reqh = reqh;
847
848 reqh->hcpriv = xfer;
849
850 ohci_hash_add_td(sc, xfer);
851 sed->ed->ed_tailp = LE(tail->physaddr);
852 opipe->tail = tail;
853 } else {
854 usb_freemem(sc->sc_dmatag, dma);
855 usb_start_next(reqh->pipe);
856 }
857 }
858
859 void
860 ohci_bulk_done(sc, reqh)
861 ohci_softc_t *sc;
862 usbd_request_handle reqh;
863 {
864 struct ohci_pipe *opipe = (struct ohci_pipe *)reqh->pipe;
865 usb_dma_t *dma;
866
867
868 DPRINTFN(10,("ohci_bulk_done: reqh=%p, actlen=%d\n",
869 reqh, reqh->actlen));
870
871 dma = &opipe->u.bulk.datadma;
872 if (reqh->request.bmRequestType & UT_READ)
873 memcpy(reqh->buffer, KERNADDR(dma), reqh->actlen);
874 usb_freemem(sc->sc_dmatag, dma);
875 usb_untimeout(ohci_timeout, reqh, reqh->timo_handle);
876 }
877
878 void
879 ohci_rhsc(sc, reqh)
880 ohci_softc_t *sc;
881 usbd_request_handle reqh;
882 {
883 usbd_pipe_handle pipe;
884 struct ohci_pipe *opipe;
885 u_char *p;
886 int i, m;
887 int hstatus;
888
889 hstatus = OREAD4(sc, OHCI_RH_STATUS);
890 DPRINTF(("ohci_rhsc: sc=%p reqh=%p hstatus=0x%08x\n",
891 sc, reqh, hstatus));
892
893 if (reqh == 0) {
894 /* Just ignore the change. */
895 return;
896 }
897
898 pipe = reqh->pipe;
899 opipe = (struct ohci_pipe *)pipe;
900
901 p = KERNADDR(&opipe->u.intr.datadma);
902 m = min(sc->sc_noport, reqh->length * 8 - 1);
903 memset(p, 0, reqh->length);
904 for (i = 1; i <= m; i++) {
905 if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
906 p[i/8] |= 1 << (i%8);
907 }
908 DPRINTF(("ohci_rhsc: change=0x%02x\n", *p));
909 reqh->actlen = reqh->length;
910 reqh->status = USBD_NORMAL_COMPLETION;
911 reqh->xfercb(reqh);
912
913 if (reqh->pipe->intrreqh != reqh) {
914 sc->sc_intrreqh = 0;
915 usb_freemem(sc->sc_dmatag, &opipe->u.intr.datadma);
916 usb_start_next(reqh->pipe);
917 }
918 }
919
920 /*
921 * Wait here until controller claims to have an interrupt.
922 * Then call ohci_intr and return. Use timeout to avoid waiting
923 * too long.
924 */
925 void
926 ohci_waitintr(sc, reqh)
927 ohci_softc_t *sc;
928 usbd_request_handle reqh;
929 {
930 int timo = reqh->timeout;
931 int usecs;
932 u_int32_t intrs;
933
934 reqh->status = USBD_IN_PROGRESS;
935 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
936 usb_delay_ms(&sc->sc_bus, 1);
937 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs;
938 DPRINTFN(15,("ohci_waitintr: 0x%04x\n", intrs));
939 #ifdef USB_DEBUG
940 if (ohcidebug > 15)
941 ohci_dumpregs(sc);
942 #endif
943 if (intrs) {
944 ohci_intr(sc);
945 if (reqh->status != USBD_IN_PROGRESS)
946 return;
947 }
948 }
949
950 /* Timeout */
951 DPRINTF(("ohci_waitintr: timeout\n"));
952 reqh->status = USBD_TIMEOUT;
953 ohci_ii_done(sc, reqh);
954 /* XXX should free TD */
955 }
956
957 void
958 ohci_poll(bus)
959 struct usbd_bus *bus;
960 {
961 ohci_softc_t *sc = (ohci_softc_t *)bus;
962
963 if (OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs)
964 ohci_intr(sc);
965 }
966
967 usbd_status
968 ohci_device_request(reqh)
969 usbd_request_handle reqh;
970 {
971 struct ohci_pipe *opipe = (struct ohci_pipe *)reqh->pipe;
972 usb_device_request_t *req = &reqh->request;
973 usbd_device_handle dev = opipe->pipe.device;
974 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
975 int addr = dev->address;
976 ohci_soft_td_t *setup, *xfer = 0, *stat, *next, *tail;
977 ohci_soft_ed_t *sed;
978 usb_dma_t *dmap;
979 int isread;
980 int len;
981 usbd_status r;
982 int s;
983
984 isread = req->bmRequestType & UT_READ;
985 len = UGETW(req->wLength);
986
987 DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, "
988 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
989 req->bmRequestType, req->bRequest, UGETW(req->wValue),
990 UGETW(req->wIndex), len, addr,
991 opipe->pipe.endpoint->edesc->bEndpointAddress));
992
993 setup = opipe->tail;
994 stat = ohci_alloc_std(sc);
995 if (!stat) {
996 r = USBD_NOMEM;
997 goto bad1;
998 }
999 tail = ohci_alloc_std(sc);
1000 if (!tail) {
1001 r = USBD_NOMEM;
1002 goto bad2;
1003 }
1004 tail->reqh = 0;
1005
1006 sed = opipe->sed;
1007 dmap = &opipe->u.ctl.datadma;
1008 opipe->u.ctl.length = len;
1009
1010 /* Update device address and length since they may have changed. */
1011 /* XXX This only needs to be done once, but it's too early in open. */
1012 sed->ed->ed_flags = LE(
1013 (LE(sed->ed->ed_flags) & ~(OHCI_ED_ADDRMASK | OHCI_ED_MAXPMASK)) |
1014 OHCI_ED_SET_FA(addr) |
1015 OHCI_ED_SET_MAXP(UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize)));
1016
1017 /* Set up data transaction */
1018 if (len != 0) {
1019 xfer = ohci_alloc_std(sc);
1020 if (!xfer) {
1021 r = USBD_NOMEM;
1022 goto bad3;
1023 }
1024 r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
1025 if (r != USBD_NORMAL_COMPLETION)
1026 goto bad4;
1027 xfer->td->td_flags = LE(
1028 (isread ? OHCI_TD_IN : OHCI_TD_OUT) | OHCI_TD_NOCC |
1029 OHCI_TD_TOGGLE_1 | OHCI_TD_NOINTR |
1030 (reqh->flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0));
1031 xfer->td->td_cbp = LE(DMAADDR(dmap));
1032 xfer->nexttd = stat;
1033 xfer->td->td_nexttd = LE(stat->physaddr);
1034 xfer->td->td_be = LE(LE(xfer->td->td_cbp) + len - 1);
1035 xfer->len = len;
1036 xfer->reqh = reqh;
1037
1038 next = xfer;
1039 } else
1040 next = stat;
1041
1042 memcpy(KERNADDR(&opipe->u.ctl.reqdma), req, sizeof *req);
1043 if (!isread && len != 0)
1044 memcpy(KERNADDR(dmap), reqh->buffer, len);
1045
1046 setup->td->td_flags = LE(OHCI_TD_SETUP | OHCI_TD_NOCC |
1047 OHCI_TD_TOGGLE_0 | OHCI_TD_NOINTR);
1048 setup->td->td_cbp = LE(DMAADDR(&opipe->u.ctl.reqdma));
1049 setup->nexttd = next;
1050 setup->td->td_nexttd = LE(next->physaddr);
1051 setup->td->td_be = LE(LE(setup->td->td_cbp) + sizeof *req - 1);
1052 setup->len = 0; /* XXX The number of byte we count */
1053 setup->reqh = reqh;
1054
1055 stat->td->td_flags = LE(
1056 (isread ? OHCI_TD_OUT : OHCI_TD_IN) | OHCI_TD_NOCC |
1057 OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1058 stat->td->td_cbp = 0;
1059 stat->nexttd = tail;
1060 stat->td->td_nexttd = LE(tail->physaddr);
1061 stat->td->td_be = 0;
1062 stat->len = 0;
1063 stat->reqh = reqh;
1064
1065 reqh->hcpriv = stat;
1066
1067 #if USB_DEBUG
1068 if (ohcidebug > 5) {
1069 printf("ohci_device_request:\n");
1070 ohci_dump_ed(sed);
1071 ohci_dump_tds(setup);
1072 }
1073 #endif
1074
1075 /* Insert ED in schedule */
1076 s = splusb();
1077 ohci_hash_add_td(sc, setup);
1078 if (len != 0)
1079 ohci_hash_add_td(sc, xfer);
1080 ohci_hash_add_td(sc, stat);
1081 sed->ed->ed_tailp = LE(tail->physaddr);
1082 opipe->tail = tail;
1083 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1084 if (reqh->timeout && !sc->sc_bus.use_polling) {
1085 usb_timeout(ohci_timeout, reqh,
1086 MS_TO_TICKS(reqh->timeout), reqh->timo_handle);
1087 }
1088 splx(s);
1089
1090 #if USB_DEBUG
1091 if (ohcidebug > 5) {
1092 delay(5000);
1093 printf("ohci_device_request: status=%x\n",
1094 OREAD4(sc, OHCI_COMMAND_STATUS));
1095 ohci_dump_ed(sed);
1096 ohci_dump_tds(setup);
1097 }
1098 #endif
1099
1100 return (USBD_NORMAL_COMPLETION);
1101
1102 bad4:
1103 ohci_free_std(sc, xfer);
1104 bad3:
1105 ohci_free_std(sc, tail);
1106 bad2:
1107 ohci_free_std(sc, stat);
1108 bad1:
1109 return (r);
1110 }
1111
1112 /*
1113 * Add an ED to the schedule. Called at splusb().
1114 */
1115 void
1116 ohci_add_ed(sed, head)
1117 ohci_soft_ed_t *sed;
1118 ohci_soft_ed_t *head;
1119 {
1120 sed->next = head->next;
1121 sed->ed->ed_nexted = head->ed->ed_nexted;
1122 head->next = sed;
1123 head->ed->ed_nexted = LE(sed->physaddr);
1124 }
1125
1126 /*
1127 * Remove an ED from the schedule. Called at splusb().
1128 */
1129 void
1130 ohci_rem_ed(sed, head)
1131 ohci_soft_ed_t *sed;
1132 ohci_soft_ed_t *head;
1133 {
1134 ohci_soft_ed_t *p;
1135
1136 /* XXX */
1137 for (p = head; p && p->next != sed; p = p->next)
1138 ;
1139 if (!p)
1140 panic("ohci_rem_ed: ED not found\n");
1141 p->next = sed->next;
1142 p->ed->ed_nexted = sed->ed->ed_nexted;
1143 }
1144
1145 /*
1146 * When a transfer is completed the TD is added to the done queue by
1147 * the host controller. This queue is the processed by software.
1148 * Unfortunately the queue contains the physical address of the TD
1149 * and we have no simple way to translate this back to a kernel address.
1150 * To make the translation possible (and fast) we use a hash table of
1151 * TDs currently in the schedule. The physical address is used as the
1152 * hash value.
1153 */
1154
1155 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
1156 /* Called at splusb() */
1157 void
1158 ohci_hash_add_td(sc, std)
1159 ohci_softc_t *sc;
1160 ohci_soft_td_t *std;
1161 {
1162 int h = HASH(std->physaddr);
1163
1164 LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
1165 }
1166
1167 /* Called at splusb() */
1168 void
1169 ohci_hash_rem_td(sc, std)
1170 ohci_softc_t *sc;
1171 ohci_soft_td_t *std;
1172 {
1173 LIST_REMOVE(std, hnext);
1174 }
1175
1176 ohci_soft_td_t *
1177 ohci_hash_find_td(sc, a)
1178 ohci_softc_t *sc;
1179 ohci_physaddr_t a;
1180 {
1181 int h = HASH(a);
1182 ohci_soft_td_t *std;
1183
1184 for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
1185 std != 0;
1186 std = LIST_NEXT(std, hnext))
1187 if (std->physaddr == a)
1188 return (std);
1189 panic("ohci_hash_find_td: addr 0x%08lx not found\n", (u_long)a);
1190 }
1191
1192 void
1193 ohci_timeout(addr)
1194 void *addr;
1195 {
1196 #if 0
1197 usbd_request_handle *reqh = addr;
1198 int s;
1199
1200 DPRINTF(("ohci_timeout: reqh=%p\n", reqh));
1201 s = splusb();
1202 /* XXX need to inactivate TD before calling interrupt routine */
1203 ohci_XXX_done(reqh);
1204 splx(s);
1205 #endif
1206 }
1207
1208 #ifdef USB_DEBUG
1209 void
1210 ohci_dump_tds(std)
1211 ohci_soft_td_t *std;
1212 {
1213 for (; std; std = std->nexttd)
1214 ohci_dump_td(std);
1215 }
1216
1217 void
1218 ohci_dump_td(std)
1219 ohci_soft_td_t *std;
1220 {
1221 printf("TD(%p) at %08lx: %b delay=%d ec=%d cc=%d\ncbp=0x%08lx "
1222 "nexttd=0x%08lx be=0x%08lx\n",
1223 std, (u_long)std->physaddr,
1224 (u_long)LE(std->td->td_flags),
1225 "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE",
1226 OHCI_TD_GET_DI(LE(std->td->td_flags)),
1227 OHCI_TD_GET_EC(LE(std->td->td_flags)),
1228 OHCI_TD_GET_CC(LE(std->td->td_flags)),
1229 (u_long)LE(std->td->td_cbp),
1230 (u_long)LE(std->td->td_nexttd), (u_long)LE(std->td->td_be));
1231 }
1232
1233 void
1234 ohci_dump_ed(sed)
1235 ohci_soft_ed_t *sed;
1236 {
1237 printf("ED(%p) at %08lx: addr=%d endpt=%d maxp=%d %b\ntailp=0x%08lx "
1238 "headp=%b nexted=0x%08lx\n",
1239 sed, (u_long)sed->physaddr,
1240 OHCI_ED_GET_FA(LE(sed->ed->ed_flags)),
1241 OHCI_ED_GET_EN(LE(sed->ed->ed_flags)),
1242 OHCI_ED_GET_MAXP(LE(sed->ed->ed_flags)),
1243 (u_long)LE(sed->ed->ed_flags),
1244 "\20\14OUT\15IN\16LOWSPEED\17SKIP\20ISO",
1245 (u_long)LE(sed->ed->ed_tailp),
1246 (u_long)LE(sed->ed->ed_headp), "\20\1HALT\2CARRY",
1247 (u_long)LE(sed->ed->ed_nexted));
1248 }
1249 #endif
1250
1251 usbd_status
1252 ohci_open(pipe)
1253 usbd_pipe_handle pipe;
1254 {
1255 usbd_device_handle dev = pipe->device;
1256 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1257 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1258 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
1259 u_int8_t addr = dev->address;
1260 ohci_soft_ed_t *sed;
1261 ohci_soft_td_t *std;
1262 usbd_status r;
1263 int s;
1264
1265 DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1266 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
1267 if (addr == sc->sc_addr) {
1268 switch (ed->bEndpointAddress) {
1269 case USB_CONTROL_ENDPOINT:
1270 pipe->methods = &ohci_root_ctrl_methods;
1271 break;
1272 case UE_IN | OHCI_INTR_ENDPT:
1273 pipe->methods = &ohci_root_intr_methods;
1274 break;
1275 default:
1276 return (USBD_INVAL);
1277 }
1278 } else {
1279 sed = ohci_alloc_sed(sc);
1280 if (sed == 0)
1281 goto bad0;
1282 std = ohci_alloc_std(sc);
1283 if (std == 0)
1284 goto bad1;
1285 opipe->sed = sed;
1286 opipe->tail = std;
1287 sed->ed->ed_flags = LE(
1288 OHCI_ED_SET_FA(addr) |
1289 OHCI_ED_SET_EN(ed->bEndpointAddress) |
1290 OHCI_ED_DIR_TD |
1291 (dev->lowspeed ? OHCI_ED_SPEED : 0) |
1292 ((ed->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS ?
1293 OHCI_ED_FORMAT_ISO : OHCI_ED_FORMAT_GEN) |
1294 OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
1295 sed->ed->ed_headp = sed->ed->ed_tailp = LE(std->physaddr);
1296
1297 switch (ed->bmAttributes & UE_XFERTYPE) {
1298 case UE_CONTROL:
1299 pipe->methods = &ohci_device_ctrl_methods;
1300 r = usb_allocmem(sc->sc_dmatag,
1301 sizeof(usb_device_request_t),
1302 0, &opipe->u.ctl.reqdma);
1303 if (r != USBD_NORMAL_COMPLETION)
1304 goto bad;
1305 s = splusb();
1306 ohci_add_ed(sed, sc->sc_ctrl_head);
1307 splx(s);
1308 break;
1309 case UE_INTERRUPT:
1310 pipe->methods = &ohci_device_intr_methods;
1311 return (ohci_device_setintr(sc, opipe, ed->bInterval));
1312 case UE_ISOCHRONOUS:
1313 printf("ohci_open: open iso unimplemented\n");
1314 return (USBD_XXX);
1315 case UE_BULK:
1316 pipe->methods = &ohci_device_bulk_methods;
1317 s = splusb();
1318 ohci_add_ed(sed, sc->sc_bulk_head);
1319 splx(s);
1320 break;
1321 }
1322 }
1323 return (USBD_NORMAL_COMPLETION);
1324
1325 bad:
1326 ohci_free_std(sc, std);
1327 bad1:
1328 ohci_free_sed(sc, sed);
1329 bad0:
1330 return (USBD_NOMEM);
1331
1332 }
1333
1334 /*
1335 * Data structures and routines to emulate the root hub.
1336 */
1337 usb_device_descriptor_t ohci_devd = {
1338 USB_DEVICE_DESCRIPTOR_SIZE,
1339 UDESC_DEVICE, /* type */
1340 {0x00, 0x01}, /* USB version */
1341 UCLASS_HUB, /* class */
1342 USUBCLASS_HUB, /* subclass */
1343 0, /* protocol */
1344 64, /* max packet */
1345 {0},{0},{0x00,0x01}, /* device id */
1346 1,2,0, /* string indicies */
1347 1 /* # of configurations */
1348 };
1349
1350 usb_config_descriptor_t ohci_confd = {
1351 USB_CONFIG_DESCRIPTOR_SIZE,
1352 UDESC_CONFIG,
1353 {USB_CONFIG_DESCRIPTOR_SIZE +
1354 USB_INTERFACE_DESCRIPTOR_SIZE +
1355 USB_ENDPOINT_DESCRIPTOR_SIZE},
1356 1,
1357 1,
1358 0,
1359 UC_SELF_POWERED,
1360 0 /* max power */
1361 };
1362
1363 usb_interface_descriptor_t ohci_ifcd = {
1364 USB_INTERFACE_DESCRIPTOR_SIZE,
1365 UDESC_INTERFACE,
1366 0,
1367 0,
1368 1,
1369 UCLASS_HUB,
1370 USUBCLASS_HUB,
1371 0,
1372 0
1373 };
1374
1375 usb_endpoint_descriptor_t ohci_endpd = {
1376 USB_ENDPOINT_DESCRIPTOR_SIZE,
1377 UDESC_ENDPOINT,
1378 UE_IN | OHCI_INTR_ENDPT,
1379 UE_INTERRUPT,
1380 {8, 0}, /* max packet */
1381 255
1382 };
1383
1384 usb_hub_descriptor_t ohci_hubd = {
1385 USB_HUB_DESCRIPTOR_SIZE,
1386 UDESC_HUB,
1387 0,
1388 {0,0},
1389 0,
1390 0,
1391 {0},
1392 };
1393
1394 int
1395 ohci_str(p, l, s)
1396 usb_string_descriptor_t *p;
1397 int l;
1398 char *s;
1399 {
1400 int i;
1401
1402 if (l == 0)
1403 return (0);
1404 p->bLength = 2 * strlen(s) + 2;
1405 if (l == 1)
1406 return (1);
1407 p->bDescriptorType = UDESC_STRING;
1408 l -= 2;
1409 for (i = 0; s[i] && l > 1; i++, l -= 2)
1410 USETW2(p->bString[i], 0, s[i]);
1411 return (2*i+2);
1412 }
1413
1414 /*
1415 * Simulate a hardware hub by handling all the necessary requests.
1416 */
1417 usbd_status
1418 ohci_root_ctrl_transfer(reqh)
1419 usbd_request_handle reqh;
1420 {
1421 int s;
1422 usbd_status r;
1423
1424 s = splusb();
1425 r = usb_insert_transfer(reqh);
1426 splx(s);
1427 if (r != USBD_NORMAL_COMPLETION)
1428 return (r);
1429 else
1430 return (ohci_root_ctrl_start(reqh));
1431 }
1432
1433 usbd_status
1434 ohci_root_ctrl_start(reqh)
1435 usbd_request_handle reqh;
1436 {
1437 ohci_softc_t *sc = (ohci_softc_t *)reqh->pipe->device->bus;
1438 usb_device_request_t *req;
1439 void *buf;
1440 int port, i;
1441 int len, value, index, l, totlen = 0;
1442 usb_port_status_t ps;
1443 usb_hub_descriptor_t hubd;
1444 usbd_status r;
1445 u_int32_t v;
1446
1447 if (!reqh->isreq)
1448 /* XXX panic */
1449 return (USBD_INVAL);
1450 req = &reqh->request;
1451 buf = reqh->buffer;
1452
1453 DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n",
1454 req->bmRequestType, req->bRequest));
1455
1456 len = UGETW(req->wLength);
1457 value = UGETW(req->wValue);
1458 index = UGETW(req->wIndex);
1459 #define C(x,y) ((x) | ((y) << 8))
1460 switch(C(req->bRequest, req->bmRequestType)) {
1461 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
1462 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
1463 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
1464 /*
1465 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
1466 * for the integrated root hub.
1467 */
1468 break;
1469 case C(UR_GET_CONFIG, UT_READ_DEVICE):
1470 if (len > 0) {
1471 *(u_int8_t *)buf = sc->sc_conf;
1472 totlen = 1;
1473 }
1474 break;
1475 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
1476 DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value));
1477 switch(value >> 8) {
1478 case UDESC_DEVICE:
1479 if ((value & 0xff) != 0) {
1480 r = USBD_IOERROR;
1481 goto ret;
1482 }
1483 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
1484 USETW(ohci_devd.idVendor, sc->sc_id_vendor);
1485 memcpy(buf, &ohci_devd, l);
1486 break;
1487 case UDESC_CONFIG:
1488 if ((value & 0xff) != 0) {
1489 r = USBD_IOERROR;
1490 goto ret;
1491 }
1492 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
1493 memcpy(buf, &ohci_confd, l);
1494 buf = (char *)buf + l;
1495 len -= l;
1496 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
1497 totlen += l;
1498 memcpy(buf, &ohci_ifcd, l);
1499 buf = (char *)buf + l;
1500 len -= l;
1501 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
1502 totlen += l;
1503 memcpy(buf, &ohci_endpd, l);
1504 break;
1505 case UDESC_STRING:
1506 if (len == 0)
1507 break;
1508 *(u_int8_t *)buf = 0;
1509 totlen = 1;
1510 switch (value & 0xff) {
1511 case 1: /* Vendor */
1512 totlen = ohci_str(buf, len, sc->sc_vendor);
1513 break;
1514 case 2: /* Product */
1515 totlen = ohci_str(buf, len, "OHCI root hub");
1516 break;
1517 }
1518 break;
1519 default:
1520 r = USBD_IOERROR;
1521 goto ret;
1522 }
1523 break;
1524 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
1525 if (len > 0) {
1526 *(u_int8_t *)buf = 0;
1527 totlen = 1;
1528 }
1529 break;
1530 case C(UR_GET_STATUS, UT_READ_DEVICE):
1531 if (len > 1) {
1532 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
1533 totlen = 2;
1534 }
1535 break;
1536 case C(UR_GET_STATUS, UT_READ_INTERFACE):
1537 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
1538 if (len > 1) {
1539 USETW(((usb_status_t *)buf)->wStatus, 0);
1540 totlen = 2;
1541 }
1542 break;
1543 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
1544 if (value >= USB_MAX_DEVICES) {
1545 r = USBD_IOERROR;
1546 goto ret;
1547 }
1548 sc->sc_addr = value;
1549 break;
1550 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
1551 if (value != 0 && value != 1) {
1552 r = USBD_IOERROR;
1553 goto ret;
1554 }
1555 sc->sc_conf = value;
1556 break;
1557 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
1558 break;
1559 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
1560 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
1561 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
1562 r = USBD_IOERROR;
1563 goto ret;
1564 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
1565 break;
1566 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
1567 break;
1568 /* Hub requests */
1569 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
1570 break;
1571 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
1572 DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
1573 "port=%d feature=%d\n",
1574 index, value));
1575 if (index < 1 || index > sc->sc_noport) {
1576 r = USBD_IOERROR;
1577 goto ret;
1578 }
1579 port = OHCI_RH_PORT_STATUS(index);
1580 switch(value) {
1581 case UHF_PORT_ENABLE:
1582 OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
1583 break;
1584 case UHF_PORT_SUSPEND:
1585 OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
1586 break;
1587 case UHF_PORT_POWER:
1588 OWRITE4(sc, port, UPS_LOW_SPEED);
1589 break;
1590 case UHF_C_PORT_CONNECTION:
1591 OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
1592 break;
1593 case UHF_C_PORT_ENABLE:
1594 OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
1595 break;
1596 case UHF_C_PORT_SUSPEND:
1597 OWRITE4(sc, port, UPS_C_SUSPEND << 16);
1598 break;
1599 case UHF_C_PORT_OVER_CURRENT:
1600 OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
1601 break;
1602 case UHF_C_PORT_RESET:
1603 OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
1604 break;
1605 default:
1606 r = USBD_IOERROR;
1607 goto ret;
1608 }
1609 switch(value) {
1610 case UHF_C_PORT_CONNECTION:
1611 case UHF_C_PORT_ENABLE:
1612 case UHF_C_PORT_SUSPEND:
1613 case UHF_C_PORT_OVER_CURRENT:
1614 case UHF_C_PORT_RESET:
1615 /* Enable RHSC interrupt if condition is cleared. */
1616 if ((OREAD4(sc, port) >> 16) == 0)
1617 ohci_rhsc_able(sc, 1);
1618 break;
1619 default:
1620 break;
1621 }
1622 break;
1623 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
1624 if (value != 0) {
1625 r = USBD_IOERROR;
1626 goto ret;
1627 }
1628 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
1629 hubd = ohci_hubd;
1630 hubd.bNbrPorts = sc->sc_noport;
1631 USETW(hubd.wHubCharacteristics,
1632 (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
1633 v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
1634 /* XXX overcurrent */
1635 );
1636 hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
1637 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
1638 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
1639 hubd.DeviceRemovable[i++] = (u_int8_t)v;
1640 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
1641 l = min(len, hubd.bDescLength);
1642 totlen = l;
1643 memcpy(buf, &hubd, l);
1644 break;
1645 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
1646 if (len != 4) {
1647 r = USBD_IOERROR;
1648 goto ret;
1649 }
1650 memset(buf, 0, len); /* ? XXX */
1651 totlen = len;
1652 break;
1653 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
1654 DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n",
1655 index));
1656 if (index < 1 || index > sc->sc_noport) {
1657 r = USBD_IOERROR;
1658 goto ret;
1659 }
1660 if (len != 4) {
1661 r = USBD_IOERROR;
1662 goto ret;
1663 }
1664 v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
1665 DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n",
1666 v));
1667 USETW(ps.wPortStatus, v);
1668 USETW(ps.wPortChange, v >> 16);
1669 l = min(len, sizeof ps);
1670 memcpy(buf, &ps, l);
1671 totlen = l;
1672 break;
1673 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
1674 r = USBD_IOERROR;
1675 goto ret;
1676 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
1677 break;
1678 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
1679 if (index < 1 || index > sc->sc_noport) {
1680 r = USBD_IOERROR;
1681 goto ret;
1682 }
1683 port = OHCI_RH_PORT_STATUS(index);
1684 switch(value) {
1685 case UHF_PORT_ENABLE:
1686 OWRITE4(sc, port, UPS_PORT_ENABLED);
1687 break;
1688 case UHF_PORT_SUSPEND:
1689 OWRITE4(sc, port, UPS_SUSPEND);
1690 break;
1691 case UHF_PORT_RESET:
1692 DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n",
1693 index));
1694 OWRITE4(sc, port, UPS_RESET);
1695 for (i = 0; i < 10; i++) {
1696 usb_delay_ms(&sc->sc_bus, 10);
1697 if ((OREAD4(sc, port) & UPS_RESET) == 0)
1698 break;
1699 }
1700 DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n",
1701 index, OREAD4(sc, port)));
1702 break;
1703 case UHF_PORT_POWER:
1704 DPRINTFN(2,("ohci_root_ctrl_transfer: set port power "
1705 "%d\n", index));
1706 OWRITE4(sc, port, UPS_PORT_POWER);
1707 break;
1708 default:
1709 r = USBD_IOERROR;
1710 goto ret;
1711 }
1712 break;
1713 default:
1714 r = USBD_IOERROR;
1715 goto ret;
1716 }
1717 reqh->actlen = totlen;
1718 r = USBD_NORMAL_COMPLETION;
1719 ret:
1720 reqh->status = r;
1721 reqh->xfercb(reqh);
1722 usb_start_next(reqh->pipe);
1723 return (USBD_IN_PROGRESS);
1724 }
1725
1726 /* Abort a root control request. */
1727 void
1728 ohci_root_ctrl_abort(reqh)
1729 usbd_request_handle reqh;
1730 {
1731 /* Nothing to do, all transfers are synchronous. */
1732 }
1733
1734 /* Close the root pipe. */
1735 void
1736 ohci_root_ctrl_close(pipe)
1737 usbd_pipe_handle pipe;
1738 {
1739 DPRINTF(("ohci_root_ctrl_close\n"));
1740 }
1741
1742 usbd_status
1743 ohci_root_intr_transfer(reqh)
1744 usbd_request_handle reqh;
1745 {
1746 int s;
1747 usbd_status r;
1748
1749 s = splusb();
1750 r = usb_insert_transfer(reqh);
1751 splx(s);
1752 if (r != USBD_NORMAL_COMPLETION)
1753 return (r);
1754 else
1755 return (ohci_root_intr_start(reqh));
1756 }
1757
1758 usbd_status
1759 ohci_root_intr_start(reqh)
1760 usbd_request_handle reqh;
1761 {
1762 usbd_pipe_handle pipe = reqh->pipe;
1763 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
1764 struct ohci_pipe *upipe = (struct ohci_pipe *)pipe;
1765 usb_dma_t *dmap;
1766 usbd_status r;
1767 int len;
1768
1769 len = reqh->length;
1770 dmap = &upipe->u.intr.datadma;
1771 if (len == 0)
1772 return (USBD_INVAL); /* XXX should it be? */
1773
1774 r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
1775 if (r != USBD_NORMAL_COMPLETION)
1776 return (r);
1777 sc->sc_intrreqh = reqh;
1778
1779 return (USBD_IN_PROGRESS);
1780 }
1781
1782 /* Abort a root interrupt request. */
1783 void
1784 ohci_root_intr_abort(reqh)
1785 usbd_request_handle reqh;
1786 {
1787 /* No need to abort. */
1788 }
1789
1790 /* Close the root pipe. */
1791 void
1792 ohci_root_intr_close(pipe)
1793 usbd_pipe_handle pipe;
1794 {
1795 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
1796 sc->sc_intrreqh = 0;
1797
1798 DPRINTF(("ohci_root_intr_close\n"));
1799 }
1800
1801 /************************/
1802
1803 usbd_status
1804 ohci_device_ctrl_transfer(reqh)
1805 usbd_request_handle reqh;
1806 {
1807 int s;
1808 usbd_status r;
1809
1810 s = splusb();
1811 r = usb_insert_transfer(reqh);
1812 splx(s);
1813 if (r != USBD_NORMAL_COMPLETION)
1814 return (r);
1815 else
1816 return (ohci_device_ctrl_start(reqh));
1817 }
1818
1819 usbd_status
1820 ohci_device_ctrl_start(reqh)
1821 usbd_request_handle reqh;
1822 {
1823 ohci_softc_t *sc = (ohci_softc_t *)reqh->pipe->device->bus;
1824 usbd_status r;
1825
1826 if (!reqh->isreq) {
1827 /* XXX panic */
1828 printf("ohci_device_ctrl_transfer: not a request\n");
1829 return (USBD_INVAL);
1830 }
1831
1832 r = ohci_device_request(reqh);
1833 if (r != USBD_NORMAL_COMPLETION)
1834 return (r);
1835
1836 if (sc->sc_bus.use_polling)
1837 ohci_waitintr(sc, reqh);
1838 return (USBD_IN_PROGRESS);
1839 }
1840
1841 /* Abort a device control request. */
1842 void
1843 ohci_device_ctrl_abort(reqh)
1844 usbd_request_handle reqh;
1845 {
1846 /* XXX inactivate */
1847 usb_delay_ms(reqh->pipe->device->bus, 1); /* make sure it is donw */
1848 /* XXX call done */
1849 }
1850
1851 /* Close a device control pipe. */
1852 void
1853 ohci_device_ctrl_close(pipe)
1854 usbd_pipe_handle pipe;
1855 {
1856 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
1857 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
1858 ohci_soft_ed_t *sed = opipe->sed;
1859 int s;
1860
1861 s = splusb();
1862 sed->ed->ed_flags |= LE(OHCI_ED_SKIP);
1863 if ((LE(sed->ed->ed_tailp) & OHCI_TAILMASK) != LE(sed->ed->ed_headp))
1864 usb_delay_ms(&sc->sc_bus, 2);
1865 ohci_rem_ed(sed, sc->sc_ctrl_head);
1866 splx(s);
1867 ohci_free_std(sc, opipe->tail);
1868 ohci_free_sed(sc, opipe->sed);
1869 /* XXX free other resources */
1870 }
1871
1872 /************************/
1873
1874 usbd_status
1875 ohci_device_bulk_transfer(reqh)
1876 usbd_request_handle reqh;
1877 {
1878 int s;
1879 usbd_status r;
1880
1881 s = splusb();
1882 r = usb_insert_transfer(reqh);
1883 splx(s);
1884 if (r != USBD_NORMAL_COMPLETION)
1885 return (r);
1886 else
1887 return (ohci_device_bulk_start(reqh));
1888 }
1889
1890 usbd_status
1891 ohci_device_bulk_start(reqh)
1892 usbd_request_handle reqh;
1893 {
1894 struct ohci_pipe *opipe = (struct ohci_pipe *)reqh->pipe;
1895 usbd_device_handle dev = opipe->pipe.device;
1896 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1897 int addr = dev->address;
1898 ohci_soft_td_t *xfer, *tail;
1899 ohci_soft_ed_t *sed;
1900 usb_dma_t *dmap;
1901 usbd_status r;
1902 int s, len, isread;
1903
1904 if (reqh->isreq) {
1905 /* XXX panic */
1906 printf("ohci_device_bulk_transfer: a request\n");
1907 return (USBD_INVAL);
1908 }
1909
1910 len = reqh->length;
1911 dmap = &opipe->u.bulk.datadma;
1912 isread = reqh->pipe->endpoint->edesc->bEndpointAddress & UE_IN;
1913 sed = opipe->sed;
1914
1915 opipe->u.bulk.length = len;
1916
1917 r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
1918 if (r != USBD_NORMAL_COMPLETION)
1919 goto ret1;
1920
1921 tail = ohci_alloc_std(sc);
1922 if (!tail) {
1923 r = USBD_NOMEM;
1924 goto ret2;
1925 }
1926 tail->reqh = 0;
1927
1928 /* Update device address */
1929 sed->ed->ed_flags = LE(
1930 (LE(sed->ed->ed_flags) & ~OHCI_ED_ADDRMASK) |
1931 OHCI_ED_SET_FA(addr));
1932
1933 /* Set up data transaction */
1934 xfer = opipe->tail;
1935 xfer->td->td_flags = LE(
1936 (isread ? OHCI_TD_IN : OHCI_TD_OUT) | OHCI_TD_NOCC |
1937 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY |
1938 (reqh->flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0));
1939 xfer->td->td_cbp = LE(DMAADDR(dmap));
1940 xfer->nexttd = tail;
1941 xfer->td->td_nexttd = LE(tail->physaddr);
1942 xfer->td->td_be = LE(LE(xfer->td->td_cbp) + len - 1);
1943 xfer->len = len;
1944 xfer->reqh = reqh;
1945
1946 reqh->hcpriv = xfer;
1947
1948 if (!isread)
1949 memcpy(KERNADDR(dmap), reqh->buffer, len);
1950
1951 /* Insert ED in schedule */
1952 s = splusb();
1953 ohci_hash_add_td(sc, xfer);
1954 sed->ed->ed_tailp = LE(tail->physaddr);
1955 opipe->tail = tail;
1956 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
1957 if (reqh->timeout && !sc->sc_bus.use_polling) {
1958 usb_timeout(ohci_timeout, reqh,
1959 MS_TO_TICKS(reqh->timeout), reqh->timo_handle);
1960 }
1961 splx(s);
1962
1963 return (USBD_IN_PROGRESS);
1964
1965 ret2:
1966 usb_freemem(sc->sc_dmatag, dmap);
1967 ret1:
1968 return (r);
1969 }
1970
1971 /* Abort a device bulk request. */
1972 void
1973 ohci_device_bulk_abort(reqh)
1974 usbd_request_handle reqh;
1975 {
1976 #if 0
1977 sed->ed->ed_flags |= LE(OHCI_ED_SKIP);
1978 if ((LE(sed->ed->ed_tailp) & OHCI_TAILMASK) != LE(sed->ed->ed_headp))
1979 usb_delay_ms(reqh->pipe->device->bus, 2);
1980 #endif
1981 /* XXX inactivate */
1982 usb_delay_ms(reqh->pipe->device->bus, 1); /* make sure it is done */
1983 /* XXX call done */
1984 }
1985
1986 /* Close a device bulk pipe. */
1987 void
1988 ohci_device_bulk_close(pipe)
1989 usbd_pipe_handle pipe;
1990 {
1991 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
1992 usbd_device_handle dev = opipe->pipe.device;
1993 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1994 int s;
1995
1996 s = splusb();
1997 ohci_rem_ed(opipe->sed, sc->sc_bulk_head);
1998 splx(s);
1999 ohci_free_std(sc, opipe->tail);
2000 ohci_free_sed(sc, opipe->sed);
2001 /* XXX free other resources */
2002 }
2003
2004 /************************/
2005
2006 usbd_status
2007 ohci_device_intr_transfer(reqh)
2008 usbd_request_handle reqh;
2009 {
2010 int s;
2011 usbd_status r;
2012
2013 s = splusb();
2014 r = usb_insert_transfer(reqh);
2015 splx(s);
2016 if (r != USBD_NORMAL_COMPLETION)
2017 return (r);
2018 else
2019 return (ohci_device_intr_start(reqh));
2020 }
2021
2022 usbd_status
2023 ohci_device_intr_start(reqh)
2024 usbd_request_handle reqh;
2025 {
2026 struct ohci_pipe *opipe = (struct ohci_pipe *)reqh->pipe;
2027 usbd_device_handle dev = opipe->pipe.device;
2028 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2029 ohci_soft_ed_t *sed = opipe->sed;
2030 ohci_soft_td_t *xfer, *tail;
2031 usb_dma_t *dmap;
2032 usbd_status r;
2033 int len;
2034 int s;
2035
2036 DPRINTFN(3, ("ohci_device_intr_transfer: reqh=%p buf=%p len=%d "
2037 "flags=%d priv=%p\n",
2038 reqh, reqh->buffer, reqh->length, reqh->flags, reqh->priv));
2039
2040 if (reqh->isreq)
2041 panic("ohci_device_intr_transfer: a request\n");
2042
2043 len = reqh->length;
2044 dmap = &opipe->u.intr.datadma;
2045 if (len == 0)
2046 return (USBD_INVAL); /* XXX should it be? */
2047
2048 xfer = opipe->tail;
2049 tail = ohci_alloc_std(sc);
2050 if (!tail) {
2051 r = USBD_NOMEM;
2052 goto ret1;
2053 }
2054 tail->reqh = 0;
2055
2056 r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
2057 if (r != USBD_NORMAL_COMPLETION)
2058 goto ret2;
2059
2060 xfer->td->td_flags = LE(
2061 OHCI_TD_IN | OHCI_TD_NOCC |
2062 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
2063 if (reqh->flags & USBD_SHORT_XFER_OK)
2064 xfer->td->td_flags |= LE(OHCI_TD_R);
2065 xfer->td->td_cbp = LE(DMAADDR(dmap));
2066 xfer->nexttd = tail;
2067 xfer->td->td_nexttd = LE(tail->physaddr);
2068 xfer->td->td_be = LE(LE(xfer->td->td_cbp) + len - 1);
2069 xfer->len = len;
2070 xfer->reqh = reqh;
2071
2072 reqh->hcpriv = xfer;
2073
2074 #if USB_DEBUG
2075 if (ohcidebug > 5) {
2076 printf("ohci_device_intr_transfer:\n");
2077 ohci_dump_ed(sed);
2078 ohci_dump_tds(xfer);
2079 }
2080 #endif
2081
2082 /* Insert ED in schedule */
2083 s = splusb();
2084 ohci_hash_add_td(sc, xfer);
2085 sed->ed->ed_tailp = LE(tail->physaddr);
2086 opipe->tail = tail;
2087 #if 0
2088 if (reqh->timeout && !sc->sc_bus.use_polling) {
2089 usb_timeout(ohci_timeout, reqh,
2090 MS_TO_TICKS(reqh->timeout), reqh->timo_handle);
2091 }
2092 #endif
2093 sed->ed->ed_flags &= LE(~OHCI_ED_SKIP);
2094
2095 #ifdef USB_DEBUG
2096 if (ohcidebug > 5) {
2097 delay(5000);
2098 printf("ohci_device_intr_transfer: status=%x\n",
2099 OREAD4(sc, OHCI_COMMAND_STATUS));
2100 ohci_dump_ed(sed);
2101 ohci_dump_tds(xfer);
2102 }
2103 #endif
2104 splx(s);
2105
2106 return (USBD_IN_PROGRESS);
2107
2108 ret2:
2109 ohci_free_std(sc, xfer);
2110 ret1:
2111 return (r);
2112 }
2113
2114 /* Abort a device control request. */
2115 void
2116 ohci_device_intr_abort(reqh)
2117 usbd_request_handle reqh;
2118 {
2119 /* XXX inactivate */
2120 usb_delay_ms(reqh->pipe->device->bus, 1); /* make sure it is done */
2121 if (reqh->pipe->intrreqh == reqh) {
2122 DPRINTF(("ohci_device_intr_abort: remove\n"));
2123 reqh->pipe->intrreqh = 0;
2124 ohci_intr_done((ohci_softc_t *)reqh->pipe->device->bus, reqh);
2125 }
2126 }
2127
2128 /* Close a device interrupt pipe. */
2129 void
2130 ohci_device_intr_close(pipe)
2131 usbd_pipe_handle pipe;
2132 {
2133 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2134 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2135 int nslots = opipe->u.intr.nslots;
2136 int pos = opipe->u.intr.pos;
2137 int j;
2138 ohci_soft_ed_t *p, *sed = opipe->sed;
2139 int s;
2140
2141 DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n",
2142 pipe, nslots, pos));
2143 s = splusb();
2144 sed->ed->ed_flags |= LE(OHCI_ED_SKIP);
2145 if ((sed->ed->ed_tailp & LE(OHCI_TAILMASK)) != sed->ed->ed_headp)
2146 usb_delay_ms(&sc->sc_bus, 2);
2147
2148 for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
2149 ;
2150 if (!p)
2151 panic("ohci_device_intr_close: ED not found\n");
2152 p->next = sed->next;
2153 p->ed->ed_nexted = sed->ed->ed_nexted;
2154 splx(s);
2155
2156 for (j = 0; j < nslots; j++)
2157 --sc->sc_bws[(pos * nslots + j) & OHCI_NO_INTRS];
2158
2159 ohci_free_std(sc, opipe->tail);
2160 ohci_free_sed(sc, opipe->sed);
2161 /* XXX free other resources */
2162 }
2163
2164 usbd_status
2165 ohci_device_setintr(sc, opipe, ival)
2166 ohci_softc_t *sc;
2167 struct ohci_pipe *opipe;
2168 int ival;
2169 {
2170 int i, j, s, best;
2171 u_int npoll, slow, shigh, nslots;
2172 u_int bestbw, bw;
2173 ohci_soft_ed_t *hsed, *sed = opipe->sed;
2174
2175 DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe));
2176 if (ival == 0) {
2177 printf("ohci_setintr: 0 interval\n");
2178 return (USBD_INVAL);
2179 }
2180
2181 npoll = OHCI_NO_INTRS;
2182 while (npoll > ival)
2183 npoll /= 2;
2184 DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll));
2185
2186 /*
2187 * We now know which level in the tree the ED must go into.
2188 * Figure out which slot has most bandwidth left over.
2189 * Slots to examine:
2190 * npoll
2191 * 1 0
2192 * 2 1 2
2193 * 4 3 4 5 6
2194 * 8 7 8 9 10 11 12 13 14
2195 * N (N-1) .. (N-1+N-1)
2196 */
2197 slow = npoll-1;
2198 shigh = slow + npoll;
2199 nslots = OHCI_NO_INTRS / npoll;
2200 for (best = i = slow, bestbw = ~0; i < shigh; i++) {
2201 bw = 0;
2202 for (j = 0; j < nslots; j++)
2203 bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
2204 if (bw < bestbw) {
2205 best = i;
2206 bestbw = bw;
2207 }
2208 }
2209 DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n",
2210 best, slow, shigh, bestbw));
2211
2212 s = splusb();
2213 hsed = sc->sc_eds[best];
2214 sed->next = hsed->next;
2215 sed->ed->ed_nexted = hsed->ed->ed_nexted;
2216 hsed->next = sed;
2217 hsed->ed->ed_nexted = LE(sed->physaddr);
2218 splx(s);
2219
2220 for (j = 0; j < nslots; j++)
2221 ++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
2222 opipe->u.intr.nslots = nslots;
2223 opipe->u.intr.pos = best;
2224
2225 DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe));
2226 return (USBD_NORMAL_COMPLETION);
2227 }
2228
2229