uhci.c revision 1.69 1 /* $NetBSD: uhci.c,v 1.69 1999/12/01 00:42:05 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/uhci.c,v 1.33 1999/11/17 22:33:41 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 Universal Host Controller driver.
43 * Handles e.g. PIIX3 and PIIX4.
44 *
45 * Data sheets: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
46 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
47 * UHCI spec: http://www.intel.com/design/usb/uhci11d.pdf
48 * USB spec: http://www.usb.org/developers/data/usb11.pdf
49 */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #if defined(__NetBSD__) || defined(__OpenBSD__)
56 #include <sys/device.h>
57 #include <sys/select.h>
58 #elif defined(__FreeBSD__)
59 #include <sys/module.h>
60 #include <sys/bus.h>
61 #include <machine/bus_pio.h>
62 #if defined(DIAGNOSTIC) && defined(__i386__)
63 #include <machine/cpu.h>
64 #endif
65 #endif
66 #include <sys/proc.h>
67 #include <sys/queue.h>
68
69 #include <machine/bus.h>
70 #include <machine/endian.h>
71
72 #include <dev/usb/usb.h>
73 #include <dev/usb/usbdi.h>
74 #include <dev/usb/usbdivar.h>
75 #include <dev/usb/usb_mem.h>
76 #include <dev/usb/usb_quirks.h>
77
78 #include <dev/usb/uhcireg.h>
79 #include <dev/usb/uhcivar.h>
80
81 #if defined(__FreeBSD__)
82 #include <machine/clock.h>
83
84 #define delay(d) DELAY(d)
85 #endif
86
87 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
88
89 #if defined(__OpenBSD__)
90 struct cfdriver uhci_cd = {
91 NULL, "uhci", DV_DULL
92 };
93 #endif
94
95 #ifdef UHCI_DEBUG
96 #define DPRINTF(x) if (uhcidebug) printf x
97 #define DPRINTFN(n,x) if (uhcidebug>(n)) printf x
98 int uhcidebug = 0;
99 #else
100 #define DPRINTF(x)
101 #define DPRINTFN(n,x)
102 #endif
103
104 /*
105 * The UHCI controller is little endian, so on big endian machines
106 * the data strored in memory needs to be swapped.
107 */
108 #if BYTE_ORDER == BIG_ENDIAN
109 #define LE(x) (bswap32(x))
110 #else
111 #define LE(x) (x)
112 #endif
113
114 struct uhci_pipe {
115 struct usbd_pipe pipe;
116 uhci_intr_info_t *iinfo;
117 int nexttoggle;
118 /* Info needed for different pipe kinds. */
119 union {
120 /* Control pipe */
121 struct {
122 uhci_soft_qh_t *sqh;
123 usb_dma_t reqdma;
124 uhci_soft_td_t *setup, *stat;
125 u_int length;
126 } ctl;
127 /* Interrupt pipe */
128 struct {
129 int npoll;
130 uhci_soft_qh_t **qhs;
131 } intr;
132 /* Bulk pipe */
133 struct {
134 uhci_soft_qh_t *sqh;
135 u_int length;
136 int isread;
137 } bulk;
138 /* Iso pipe */
139 struct iso {
140 uhci_soft_td_t **stds;
141 int next, inuse;
142 } iso;
143 } u;
144 };
145
146 /*
147 * The uhci_intr_info free list can be global since they contain
148 * no dma specific data. The other free lists do.
149 */
150 LIST_HEAD(, uhci_intr_info) uhci_ii_free;
151
152 static void uhci_busreset __P((uhci_softc_t *));
153 #if defined(__NetBSD__) || defined(__OpenBSD__)
154 static void uhci_power __P((int, void *));
155 #endif
156 static usbd_status uhci_run __P((uhci_softc_t *, int run));
157 static uhci_soft_td_t *uhci_alloc_std __P((uhci_softc_t *));
158 static void uhci_free_std __P((uhci_softc_t *, uhci_soft_td_t *));
159 static uhci_soft_qh_t *uhci_alloc_sqh __P((uhci_softc_t *));
160 static void uhci_free_sqh __P((uhci_softc_t *, uhci_soft_qh_t *));
161 static uhci_intr_info_t *uhci_alloc_intr_info __P((uhci_softc_t *));
162 static void uhci_free_intr_info __P((uhci_intr_info_t *ii));
163 #if 0
164 static void uhci_enter_ctl_q __P((uhci_softc_t *, uhci_soft_qh_t *,
165 uhci_intr_info_t *));
166 static void uhci_exit_ctl_q __P((uhci_softc_t *, uhci_soft_qh_t *));
167 #endif
168
169 static void uhci_free_std_chain __P((uhci_softc_t *,
170 uhci_soft_td_t *, uhci_soft_td_t *));
171 static usbd_status uhci_alloc_std_chain __P((struct uhci_pipe *,
172 uhci_softc_t *, int, int, int, usb_dma_t *,
173 uhci_soft_td_t **, uhci_soft_td_t **));
174 static void uhci_timo __P((void *));
175 static void uhci_waitintr __P((uhci_softc_t *,
176 usbd_xfer_handle));
177 static void uhci_check_intr __P((uhci_softc_t *,
178 uhci_intr_info_t *));
179 static void uhci_idone __P((uhci_intr_info_t *));
180 static void uhci_abort_xfer __P((usbd_xfer_handle,
181 usbd_status status));
182 static void uhci_abort_xfer_end __P((void *v));
183 static void uhci_timeout __P((void *));
184 static void uhci_lock_frames __P((uhci_softc_t *));
185 static void uhci_unlock_frames __P((uhci_softc_t *));
186 static void uhci_add_ctrl __P((uhci_softc_t *, uhci_soft_qh_t *));
187 static void uhci_add_bulk __P((uhci_softc_t *, uhci_soft_qh_t *));
188 static void uhci_remove_ctrl __P((uhci_softc_t *,uhci_soft_qh_t *));
189 static void uhci_remove_bulk __P((uhci_softc_t *,uhci_soft_qh_t *));
190 static int uhci_str __P((usb_string_descriptor_t *, int, char *));
191
192 static usbd_status uhci_setup_isoc __P((usbd_pipe_handle pipe));
193 static void uhci_device_isoc_enter __P((usbd_xfer_handle));
194
195 static usbd_status uhci_allocm __P((struct usbd_bus *, usb_dma_t *,
196 u_int32_t));
197 static void uhci_freem __P((struct usbd_bus *, usb_dma_t *));
198
199 static usbd_status uhci_device_ctrl_transfer __P((usbd_xfer_handle));
200 static usbd_status uhci_device_ctrl_start __P((usbd_xfer_handle));
201 static void uhci_device_ctrl_abort __P((usbd_xfer_handle));
202 static void uhci_device_ctrl_close __P((usbd_pipe_handle));
203 static void uhci_device_ctrl_done __P((usbd_xfer_handle));
204
205 static usbd_status uhci_device_intr_transfer __P((usbd_xfer_handle));
206 static usbd_status uhci_device_intr_start __P((usbd_xfer_handle));
207 static void uhci_device_intr_abort __P((usbd_xfer_handle));
208 static void uhci_device_intr_close __P((usbd_pipe_handle));
209 static void uhci_device_intr_done __P((usbd_xfer_handle));
210
211 static usbd_status uhci_device_bulk_transfer __P((usbd_xfer_handle));
212 static usbd_status uhci_device_bulk_start __P((usbd_xfer_handle));
213 static void uhci_device_bulk_abort __P((usbd_xfer_handle));
214 static void uhci_device_bulk_close __P((usbd_pipe_handle));
215 static void uhci_device_bulk_done __P((usbd_xfer_handle));
216
217 static usbd_status uhci_device_isoc_transfer __P((usbd_xfer_handle));
218 static usbd_status uhci_device_isoc_start __P((usbd_xfer_handle));
219 static void uhci_device_isoc_abort __P((usbd_xfer_handle));
220 static void uhci_device_isoc_close __P((usbd_pipe_handle));
221 static void uhci_device_isoc_done __P((usbd_xfer_handle));
222
223 static usbd_status uhci_root_ctrl_transfer __P((usbd_xfer_handle));
224 static usbd_status uhci_root_ctrl_start __P((usbd_xfer_handle));
225 static void uhci_root_ctrl_abort __P((usbd_xfer_handle));
226 static void uhci_root_ctrl_close __P((usbd_pipe_handle));
227
228 static usbd_status uhci_root_intr_transfer __P((usbd_xfer_handle));
229 static usbd_status uhci_root_intr_start __P((usbd_xfer_handle));
230 static void uhci_root_intr_abort __P((usbd_xfer_handle));
231 static void uhci_root_intr_close __P((usbd_pipe_handle));
232 static void uhci_root_intr_done __P((usbd_xfer_handle));
233
234 static usbd_status uhci_open __P((usbd_pipe_handle));
235 static void uhci_poll __P((struct usbd_bus *));
236
237 static usbd_status uhci_device_request __P((usbd_xfer_handle xfer));
238
239 static void uhci_add_intr __P((uhci_softc_t *, int,
240 uhci_soft_qh_t *));
241 static void uhci_remove_intr __P((uhci_softc_t *, int,
242 uhci_soft_qh_t *));
243 static usbd_status uhci_device_setintr __P((uhci_softc_t *sc,
244 struct uhci_pipe *pipe, int ival));
245
246 static void uhci_device_clear_toggle __P((usbd_pipe_handle pipe));
247 static void uhci_noop __P((usbd_pipe_handle pipe));
248
249 #ifdef UHCI_DEBUG
250 static void uhci_dumpregs __P((uhci_softc_t *));
251 static void uhci_dump_qhs __P((uhci_soft_qh_t *));
252 static void uhci_dump_qh __P((uhci_soft_qh_t *));
253 static void uhci_dump_tds __P((uhci_soft_td_t *));
254 static void uhci_dump_td __P((uhci_soft_td_t *));
255 #endif
256
257 #define UWRITE2(sc, r, x) bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x))
258 #define UWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
259 #define UREAD1(sc, r) bus_space_read_1((sc)->iot, (sc)->ioh, (r))
260 #define UREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
261 #define UREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
262
263 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
264 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
265
266 #define UHCI_RESET_TIMEOUT 100 /* reset timeout */
267
268 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
269
270 #define UHCI_INTR_ENDPT 1
271
272 struct usbd_bus_methods uhci_bus_methods = {
273 uhci_open,
274 uhci_poll,
275 uhci_allocm,
276 uhci_freem,
277 };
278
279 struct usbd_pipe_methods uhci_root_ctrl_methods = {
280 uhci_root_ctrl_transfer,
281 uhci_root_ctrl_start,
282 uhci_root_ctrl_abort,
283 uhci_root_ctrl_close,
284 uhci_noop,
285 0,
286 };
287
288 struct usbd_pipe_methods uhci_root_intr_methods = {
289 uhci_root_intr_transfer,
290 uhci_root_intr_start,
291 uhci_root_intr_abort,
292 uhci_root_intr_close,
293 uhci_noop,
294 uhci_root_intr_done,
295 };
296
297 struct usbd_pipe_methods uhci_device_ctrl_methods = {
298 uhci_device_ctrl_transfer,
299 uhci_device_ctrl_start,
300 uhci_device_ctrl_abort,
301 uhci_device_ctrl_close,
302 uhci_noop,
303 uhci_device_ctrl_done,
304 };
305
306 struct usbd_pipe_methods uhci_device_intr_methods = {
307 uhci_device_intr_transfer,
308 uhci_device_intr_start,
309 uhci_device_intr_abort,
310 uhci_device_intr_close,
311 uhci_device_clear_toggle,
312 uhci_device_intr_done,
313 };
314
315 struct usbd_pipe_methods uhci_device_bulk_methods = {
316 uhci_device_bulk_transfer,
317 uhci_device_bulk_start,
318 uhci_device_bulk_abort,
319 uhci_device_bulk_close,
320 uhci_device_clear_toggle,
321 uhci_device_bulk_done,
322 };
323
324 struct usbd_pipe_methods uhci_device_isoc_methods = {
325 uhci_device_isoc_transfer,
326 uhci_device_isoc_start,
327 uhci_device_isoc_abort,
328 uhci_device_isoc_close,
329 uhci_noop,
330 uhci_device_isoc_done,
331 };
332
333 void
334 uhci_busreset(sc)
335 uhci_softc_t *sc;
336 {
337 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */
338 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
339 UHCICMD(sc, 0); /* do nothing */
340 }
341
342 usbd_status
343 uhci_init(sc)
344 uhci_softc_t *sc;
345 {
346 usbd_status err;
347 int i, j;
348 uhci_soft_qh_t *csqh, *bsqh, *sqh;
349 uhci_soft_td_t *std;
350
351 DPRINTFN(1,("uhci_init: start\n"));
352
353 #ifdef UHCI_DEBUG
354 if (uhcidebug > 2)
355 uhci_dumpregs(sc);
356 #endif
357
358 uhci_run(sc, 0); /* stop the controller */
359 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
360
361 uhci_busreset(sc);
362
363 /* Allocate and initialize real frame array. */
364 err = usb_allocmem(&sc->sc_bus,
365 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
366 UHCI_FRAMELIST_ALIGN, &sc->sc_dma);
367 if (err)
368 return (err);
369 sc->sc_pframes = KERNADDR(&sc->sc_dma);
370 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */
371 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma)); /* set frame list*/
372
373 /* Allocate the dummy QH where bulk traffic will be queued. */
374 bsqh = uhci_alloc_sqh(sc);
375 if (bsqh == NULL)
376 return (USBD_NOMEM);
377 bsqh->qh.qh_hlink = LE(UHCI_PTR_T); /* end of QH chain */
378 bsqh->qh.qh_elink = LE(UHCI_PTR_T);
379 sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
380
381 /* Allocate the dummy QH where control traffic will be queued. */
382 csqh = uhci_alloc_sqh(sc);
383 if (csqh == NULL)
384 return (USBD_NOMEM);
385 csqh->hlink = bsqh;
386 csqh->qh.qh_hlink = LE(bsqh->physaddr | UHCI_PTR_Q);
387 csqh->qh.qh_elink = LE(UHCI_PTR_T);
388 sc->sc_ctl_start = sc->sc_ctl_end = csqh;
389
390 /*
391 * Make all (virtual) frame list pointers point to the interrupt
392 * queue heads and the interrupt queue heads at the control
393 * queue head and point the physical frame list to the virtual.
394 */
395 for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
396 std = uhci_alloc_std(sc);
397 sqh = uhci_alloc_sqh(sc);
398 if (std == NULL || sqh == NULL)
399 return (USBD_NOMEM);
400 std->link.sqh = sqh;
401 std->td.td_link = LE(sqh->physaddr | UHCI_PTR_Q);
402 std->td.td_status = LE(UHCI_TD_IOS); /* iso, inactive */
403 std->td.td_token = LE(0);
404 std->td.td_buffer = LE(0);
405 sqh->hlink = csqh;
406 sqh->qh.qh_hlink = LE(csqh->physaddr | UHCI_PTR_Q);
407 sqh->elink = 0;
408 sqh->qh.qh_elink = LE(UHCI_PTR_T);
409 sc->sc_vframes[i].htd = std;
410 sc->sc_vframes[i].etd = std;
411 sc->sc_vframes[i].hqh = sqh;
412 sc->sc_vframes[i].eqh = sqh;
413 for (j = i;
414 j < UHCI_FRAMELIST_COUNT;
415 j += UHCI_VFRAMELIST_COUNT)
416 sc->sc_pframes[j] = LE(std->physaddr);
417 }
418
419 LIST_INIT(&sc->sc_intrhead);
420
421 /* Set up the bus struct. */
422 sc->sc_bus.methods = &uhci_bus_methods;
423 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
424
425 sc->sc_suspend = PWR_RESUME;
426 sc->sc_powerhook = powerhook_establish(uhci_power, sc);
427
428 DPRINTFN(1,("uhci_init: enabling\n"));
429 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
430 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */
431
432 return (uhci_run(sc, 1)); /* and here we go... */
433 }
434
435 #if defined(__NetBSD__) || defined(__OpenBSD__)
436 int
437 uhci_activate(self, act)
438 device_ptr_t self;
439 enum devact act;
440 {
441 struct uhci_softc *sc = (struct uhci_softc *)self;
442 int rv = 0;
443
444 switch (act) {
445 case DVACT_ACTIVATE:
446 return (EOPNOTSUPP);
447 break;
448
449 case DVACT_DEACTIVATE:
450 if (sc->sc_child != NULL)
451 rv = config_deactivate(sc->sc_child);
452 break;
453 }
454 return (rv);
455 }
456
457 int
458 uhci_detach(sc, flags)
459 struct uhci_softc *sc;
460 int flags;
461 {
462 int rv = 0;
463
464 if (sc->sc_child != NULL)
465 rv = config_detach(sc->sc_child, flags);
466
467 if (rv != 0)
468 return (rv);
469
470 powerhook_disestablish(sc->sc_powerhook);
471 /* free data structures XXX */
472
473 return (rv);
474 }
475 #endif
476
477 usbd_status
478 uhci_allocm(bus, dma, size)
479 struct usbd_bus *bus;
480 usb_dma_t *dma;
481 u_int32_t size;
482 {
483 return (usb_allocmem(&((struct uhci_softc *)bus)->sc_bus, size, 0,
484 dma));
485 }
486
487 void
488 uhci_freem(bus, dma)
489 struct usbd_bus *bus;
490 usb_dma_t *dma;
491 {
492 usb_freemem(&((struct uhci_softc *)bus)->sc_bus, dma);
493 }
494
495 #if defined(__NetBSD__)
496 /*
497 * Handle suspend/resume.
498 *
499 * We need to switch to polling mode here, because this routine is
500 * called from an intterupt context. This is all right since we
501 * are almost suspended anyway.
502 */
503 void
504 uhci_power(why, v)
505 int why;
506 void *v;
507 {
508 uhci_softc_t *sc = v;
509 int cmd;
510 int s;
511
512 s = splusb();
513 cmd = UREAD2(sc, UHCI_CMD);
514
515 DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
516 sc, why, sc->sc_suspend, cmd));
517
518 if (why != PWR_RESUME) {
519 #ifdef UHCI_DEBUG
520 if (uhcidebug > 2)
521 uhci_dumpregs(sc);
522 #endif
523 if (sc->sc_has_timo)
524 usb_untimeout(uhci_timo, sc->sc_has_timo,
525 sc->sc_has_timo->timo_handle);
526 sc->sc_bus.use_polling++;
527 uhci_run(sc, 0); /* stop the controller */
528 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter global suspend */
529 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
530 sc->sc_suspend = why;
531 sc->sc_bus.use_polling--;
532 DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc, UHCI_CMD)));
533 } else {
534 /*
535 * XXX We should really do much more here in case the
536 * controller registers have been lost and BIOS has
537 * not restored them.
538 */
539 #ifdef DIAGNOSTIC
540 if (sc->sc_suspend == PWR_RESUME)
541 printf("uhci_power: weird, resume without suspend.\n");
542 #endif
543 sc->sc_bus.use_polling++;
544 sc->sc_suspend = why;
545 if (cmd & UHCI_CMD_RS)
546 uhci_run(sc, 0); /* in case BIOS has started it */
547 UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force global resume */
548 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
549 UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
550 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
551 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* re-enable intrs */
552 uhci_run(sc, 1); /* and start traffic again */
553 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
554 sc->sc_bus.use_polling--;
555 if (sc->sc_has_timo)
556 usb_timeout(uhci_timo, sc->sc_has_timo,
557 sc->sc_ival, sc->sc_has_timo->timo_handle);
558 #ifdef UHCI_DEBUG
559 if (uhcidebug > 2)
560 uhci_dumpregs(sc);
561 #endif
562 }
563 splx(s);
564 }
565 #endif /* defined(__NetBSD__) */
566
567 #ifdef UHCI_DEBUG
568 static void
569 uhci_dumpregs(sc)
570 uhci_softc_t *sc;
571 {
572 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
573 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
574 USBDEVNAME(sc->sc_bus.bdev),
575 UREAD2(sc, UHCI_CMD),
576 UREAD2(sc, UHCI_STS),
577 UREAD2(sc, UHCI_INTR),
578 UREAD2(sc, UHCI_FRNUM),
579 UREAD4(sc, UHCI_FLBASEADDR),
580 UREAD1(sc, UHCI_SOF),
581 UREAD2(sc, UHCI_PORTSC1),
582 UREAD2(sc, UHCI_PORTSC2)));
583 }
584
585 void
586 uhci_dump_td(p)
587 uhci_soft_td_t *p;
588 {
589 DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx "
590 "token=0x%08lx buffer=0x%08lx\n",
591 p, (long)p->physaddr,
592 (long)LE(p->td.td_link),
593 (long)LE(p->td.td_status),
594 (long)LE(p->td.td_token),
595 (long)LE(p->td.td_buffer)));
596 DPRINTFN(-1,(" %b %b,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
597 "D=%d,maxlen=%d\n",
598 (int)LE(p->td.td_link),
599 "\20\1T\2Q\3VF",
600 (int)LE(p->td.td_status),
601 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
602 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
603 UHCI_TD_GET_ERRCNT(LE(p->td.td_status)),
604 UHCI_TD_GET_ACTLEN(LE(p->td.td_status)),
605 UHCI_TD_GET_PID(LE(p->td.td_token)),
606 UHCI_TD_GET_DEVADDR(LE(p->td.td_token)),
607 UHCI_TD_GET_ENDPT(LE(p->td.td_token)),
608 UHCI_TD_GET_DT(LE(p->td.td_token)),
609 UHCI_TD_GET_MAXLEN(LE(p->td.td_token))));
610 }
611
612 void
613 uhci_dump_qh(sqh)
614 uhci_soft_qh_t *sqh;
615 {
616 DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh,
617 (int)sqh->physaddr, LE(sqh->qh.qh_hlink), LE(sqh->qh.qh_elink)));
618 }
619
620
621 #if 0
622 void
623 uhci_dump()
624 {
625 uhci_softc_t *sc = uhci;
626
627 uhci_dumpregs(sc);
628 printf("intrs=%d\n", sc->sc_bus.no_intrs);
629 printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);
630 uhci_dump_qh(sc->sc_ctl_start->qh.hlink);
631 }
632 #endif
633
634
635 void
636 uhci_dump_qhs(sqh)
637 uhci_soft_qh_t *sqh;
638 {
639 uhci_dump_qh(sqh);
640
641 /* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
642 * Traverses sideways first, then down.
643 *
644 * QH1
645 * QH2
646 * No QH
647 * TD2.1
648 * TD2.2
649 * TD1.1
650 * etc.
651 *
652 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
653 */
654
655
656 if (sqh->hlink != NULL && !(sqh->qh.qh_hlink & UHCI_PTR_T))
657 uhci_dump_qhs(sqh->hlink);
658 else
659 DPRINTF(("No QH\n"));
660
661 if (sqh->elink != NULL && !(sqh->qh.qh_elink & UHCI_PTR_T))
662 uhci_dump_tds(sqh->elink);
663 else
664 DPRINTF(("No TD\n"));
665 }
666
667 void
668 uhci_dump_tds(std)
669 uhci_soft_td_t *std;
670 {
671 uhci_soft_td_t *td;
672
673 for(td = std; td != NULL; td = td->link.std) {
674 uhci_dump_td(td);
675
676 /* Check whether the link pointer in this TD marks
677 * the link pointer as end of queue. This avoids
678 * printing the free list in case the queue/TD has
679 * already been moved there (seatbelt).
680 */
681 if (td->td.td_link & UHCI_PTR_T ||
682 td->td.td_link == 0)
683 break;
684 }
685 }
686 #endif
687
688 /*
689 * This routine is executed periodically and simulates interrupts
690 * from the root controller interrupt pipe for port status change.
691 */
692 void
693 uhci_timo(addr)
694 void *addr;
695 {
696 usbd_xfer_handle xfer = addr;
697 usbd_pipe_handle pipe = xfer->pipe;
698 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
699 int s;
700 u_char *p;
701
702 DPRINTFN(20, ("uhci_timo\n"));
703
704 usb_timeout(uhci_timo, xfer, sc->sc_ival, xfer->timo_handle);
705
706 p = KERNADDR(&xfer->dmabuf);
707 p[0] = 0;
708 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
709 p[0] |= 1<<1;
710 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
711 p[0] |= 1<<2;
712 if (p[0] == 0)
713 /* No change, try again in a while */
714 return;
715
716 xfer->actlen = 1;
717 xfer->status = USBD_NORMAL_COMPLETION;
718 s = splusb();
719 xfer->hcpriv = 0;
720 xfer->device->bus->intr_context++;
721 usb_transfer_complete(xfer);
722 xfer->device->bus->intr_context--;
723 splx(s);
724 }
725
726 void
727 uhci_root_intr_done(xfer)
728 usbd_xfer_handle xfer;
729 {
730 }
731
732
733 void
734 uhci_lock_frames(sc)
735 uhci_softc_t *sc;
736 {
737 int s = splusb();
738
739 while (sc->sc_vflock) {
740 sc->sc_vflock |= UHCI_WANT_LOCK;
741 tsleep(&sc->sc_vflock, PRIBIO, "uhcqhl", 0);
742 }
743 sc->sc_vflock = UHCI_HAS_LOCK;
744 splx(s);
745 }
746
747 void
748 uhci_unlock_frames(sc)
749 uhci_softc_t *sc;
750 {
751 int s = splusb();
752
753 sc->sc_vflock &= ~UHCI_HAS_LOCK;
754 if (sc->sc_vflock & UHCI_WANT_LOCK)
755 wakeup(&sc->sc_vflock);
756 splx(s);
757 }
758
759 /*
760 * Allocate an interrupt information struct. A free list is kept
761 * for fast allocation.
762 */
763 uhci_intr_info_t *
764 uhci_alloc_intr_info(sc)
765 uhci_softc_t *sc;
766 {
767 uhci_intr_info_t *ii;
768
769 ii = LIST_FIRST(&uhci_ii_free);
770 if (ii)
771 LIST_REMOVE(ii, list);
772 else {
773 ii = malloc(sizeof(uhci_intr_info_t), M_USBHC, M_NOWAIT);
774 }
775 ii->sc = sc;
776 #if defined(__FreeBSD__)
777 callout_handle_init(&ii->timeout_handle);
778 #endif
779
780 return ii;
781 }
782
783 void
784 uhci_free_intr_info(ii)
785 uhci_intr_info_t *ii;
786 {
787 LIST_INSERT_HEAD(&uhci_ii_free, ii, list); /* and put on free list */
788 }
789
790 /* Add control QH, called at splusb(). */
791 void
792 uhci_add_ctrl(sc, sqh)
793 uhci_softc_t *sc;
794 uhci_soft_qh_t *sqh;
795 {
796 uhci_soft_qh_t *eqh;
797
798 SPLUSBCHECK;
799
800 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
801 eqh = sc->sc_ctl_end;
802 sqh->hlink = eqh->hlink;
803 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
804 eqh->hlink = sqh;
805 eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
806 sc->sc_ctl_end = sqh;
807 }
808
809 /* Remove control QH, called at splusb(). */
810 void
811 uhci_remove_ctrl(sc, sqh)
812 uhci_softc_t *sc;
813 uhci_soft_qh_t *sqh;
814 {
815 uhci_soft_qh_t *pqh;
816
817 SPLUSBCHECK;
818
819 DPRINTFN(10, ("uhci_remove_ctrl: sqh=%p\n", sqh));
820 for (pqh = sc->sc_ctl_start; pqh->hlink != sqh; pqh=pqh->hlink)
821 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
822 if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
823 printf("uhci_remove_ctrl: QH not found\n");
824 return;
825 }
826 #else
827 ;
828 #endif
829 pqh->hlink = sqh->hlink;
830 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
831 if (sc->sc_ctl_end == sqh)
832 sc->sc_ctl_end = pqh;
833 }
834
835 /* Add bulk QH, called at splusb(). */
836 void
837 uhci_add_bulk(sc, sqh)
838 uhci_softc_t *sc;
839 uhci_soft_qh_t *sqh;
840 {
841 uhci_soft_qh_t *eqh;
842
843 SPLUSBCHECK;
844
845 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
846 eqh = sc->sc_bulk_end;
847 sqh->hlink = eqh->hlink;
848 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
849 eqh->hlink = sqh;
850 eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
851 sc->sc_bulk_end = sqh;
852 }
853
854 /* Remove bulk QH, called at splusb(). */
855 void
856 uhci_remove_bulk(sc, sqh)
857 uhci_softc_t *sc;
858 uhci_soft_qh_t *sqh;
859 {
860 uhci_soft_qh_t *pqh;
861
862 SPLUSBCHECK;
863
864 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
865 for (pqh = sc->sc_bulk_start; pqh->hlink != sqh; pqh = pqh->hlink)
866 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
867 if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
868 printf("uhci_remove_bulk: QH not found\n");
869 return;
870 }
871 #else
872 ;
873 #endif
874 pqh->hlink = sqh->hlink;
875 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
876 if (sc->sc_bulk_end == sqh)
877 sc->sc_bulk_end = pqh;
878 }
879
880 int
881 uhci_intr(arg)
882 void *arg;
883 {
884 uhci_softc_t *sc = arg;
885 int status;
886 int ack;
887 uhci_intr_info_t *ii;
888
889 #ifdef UHCI_DEBUG
890 if (uhcidebug > 15) {
891 DPRINTF(("%s: uhci_intr\n", USBDEVNAME(sc->sc_bus.bdev)));
892 uhci_dumpregs(sc);
893 }
894 #endif
895
896 status = UREAD2(sc, UHCI_STS);
897 if (status == 0) /* The interrupt was not for us. */
898 return (0);
899
900 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
901 if (sc->sc_suspend != PWR_RESUME)
902 printf("uhci_intr: suspended sts=0x%x\n", status);
903 #endif
904
905 ack = 0;
906 if (status & UHCI_STS_USBINT)
907 ack |= UHCI_STS_USBINT;
908 if (status & UHCI_STS_USBEI)
909 ack |= UHCI_STS_USBEI;
910 if (status & UHCI_STS_RD) {
911 ack |= UHCI_STS_RD;
912 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
913 }
914 if (status & UHCI_STS_HSE) {
915 ack |= UHCI_STS_HSE;
916 printf("%s: host controller process error\n",
917 USBDEVNAME(sc->sc_bus.bdev));
918 }
919 if (status & UHCI_STS_HCPE) {
920 ack |= UHCI_STS_HCPE;
921 printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev));
922 }
923 if (status & UHCI_STS_HCH) {
924 /* no acknowledge needed */
925 printf("%s: host controller halted\n",
926 USBDEVNAME(sc->sc_bus.bdev));
927 }
928
929 if (ack) /* acknowledge the ints */
930 UWRITE2(sc, UHCI_STS, ack);
931 else /* nothing to acknowledge */
932 return (0);
933
934 sc->sc_bus.intr_context++;
935 sc->sc_bus.no_intrs++;
936
937 /*
938 * Interrupts on UHCI really suck. When the host controller
939 * interrupts because a transfer is completed there is no
940 * way of knowing which transfer it was. You can scan down
941 * the TDs and QHs of the previous frame to limit the search,
942 * but that assumes that the interrupt was not delayed by more
943 * than 1 ms, which may not always be true (e.g. after debug
944 * output on a slow console).
945 * We scan all interrupt descriptors to see if any have
946 * completed.
947 */
948 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
949 uhci_check_intr(sc, ii);
950
951 DPRINTFN(10, ("%s: uhci_intr: exit\n", USBDEVNAME(sc->sc_bus.bdev)));
952
953 sc->sc_bus.intr_context--;
954
955 return (1);
956 }
957
958 /* Check for an interrupt. */
959 void
960 uhci_check_intr(sc, ii)
961 uhci_softc_t *sc;
962 uhci_intr_info_t *ii;
963 {
964 uhci_soft_td_t *std, *lstd;
965 u_int32_t status;
966
967 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
968 #ifdef DIAGNOSTIC
969 if (ii == NULL) {
970 printf("uhci_check_intr: no ii? %p\n", ii);
971 return;
972 }
973 #endif
974 if (ii->stdstart == NULL)
975 return;
976 lstd = ii->stdend;
977 #ifdef DIAGNOSTIC
978 if (lstd == NULL) {
979 printf("uhci_check_intr: std==0\n");
980 return;
981 }
982 #endif
983 /*
984 * If the last TD is still active we need to check whether there
985 * is a an error somewhere in the middle, or whether there was a
986 * short packet (SPD and not ACTIVE).
987 */
988 if (LE(lstd->td.td_status) & UHCI_TD_ACTIVE) {
989 DPRINTFN(15, ("uhci_check_intr: active ii=%p\n", ii));
990 for (std = ii->stdstart; std != lstd; std = std->link.std) {
991 status = LE(std->td.td_status);
992 if ((status & UHCI_TD_STALLED) ||
993 (status & (UHCI_TD_SPD | UHCI_TD_ACTIVE)) ==
994 UHCI_TD_SPD)
995 goto done;
996 }
997 DPRINTFN(15, ("uhci_check_intr: ii=%p std=%p still active\n",
998 ii, ii->stdstart));
999 return;
1000 }
1001 done:
1002 usb_untimeout(uhci_timeout, ii, ii->timeout_handle);
1003 uhci_idone(ii);
1004 }
1005
1006 /* Called at splusb() */
1007 void
1008 uhci_idone(ii)
1009 uhci_intr_info_t *ii;
1010 {
1011 usbd_xfer_handle xfer = ii->xfer;
1012 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1013 uhci_soft_td_t *std;
1014 u_int32_t status = 0, nstatus;
1015 int actlen;
1016
1017 #ifdef DIAGNOSTIC
1018 {
1019 int s = splhigh();
1020 if (ii->isdone) {
1021 splx(s);
1022 printf("uhci_idone: ii=%p is done!\n", ii);
1023 return;
1024 }
1025 ii->isdone = 1;
1026 splx(s);
1027 }
1028 #endif
1029
1030 if (xfer->status == USBD_CANCELLED ||
1031 xfer->status == USBD_TIMEOUT) {
1032 DPRINTF(("uhci_idone: aborted xfer=%p\n", xfer));
1033 return;
1034 }
1035
1036 if (xfer->nframes != 0) {
1037 /* Isoc transfer, do things differently. */
1038 uhci_soft_td_t **stds = upipe->u.iso.stds;
1039 int i, n, nframes;
1040
1041 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1042
1043 nframes = xfer->nframes;
1044 actlen = 0;
1045 n = xfer->hcprivint;
1046 for (i = 0; i < nframes; i++) {
1047 std = stds[n];
1048 #ifdef UHCI_DEBUG
1049 if (uhcidebug > 5) {
1050 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1051 uhci_dump_td(std);
1052 }
1053 #endif
1054 if (++n >= UHCI_VFRAMELIST_COUNT)
1055 n = 0;
1056 status = LE(std->td.td_status);
1057 actlen += UHCI_TD_GET_ACTLEN(status);
1058 }
1059 upipe->u.iso.inuse -= nframes;
1060 xfer->actlen = actlen;
1061 xfer->status = USBD_NORMAL_COMPLETION;
1062 xfer->hcpriv = ii;
1063 usb_transfer_complete(xfer);
1064 return;
1065 }
1066
1067 #ifdef UHCI_DEBUG
1068 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1069 ii, xfer, upipe));
1070 if (uhcidebug > 10)
1071 uhci_dump_tds(ii->stdstart);
1072 #endif
1073
1074 /* The transfer is done, compute actual length and status. */
1075 actlen = 0;
1076 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1077 nstatus = LE(std->td.td_status);
1078 if (nstatus & UHCI_TD_ACTIVE)
1079 break;
1080
1081 status = nstatus;
1082 if (UHCI_TD_GET_PID(LE(std->td.td_token)) != UHCI_TD_PID_SETUP)
1083 actlen += UHCI_TD_GET_ACTLEN(status);
1084 }
1085 /* If there are left over TDs we need to update the toggle. */
1086 if (std != NULL)
1087 upipe->nexttoggle = UHCI_TD_GET_DT(LE(std->td.td_token));
1088
1089 status &= UHCI_TD_ERROR;
1090 DPRINTFN(10, ("uhci_check_intr: actlen=%d, status=0x%x\n",
1091 actlen, status));
1092 xfer->actlen = actlen;
1093 if (status != 0) {
1094 DPRINTFN((status&UHCI_TD_STALLED)*10,
1095 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1096 "status 0x%b\n",
1097 xfer->pipe->device->address,
1098 xfer->pipe->endpoint->edesc->bEndpointAddress,
1099 (int)status,
1100 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
1101 "STALLED\30ACTIVE"));
1102 if (status == UHCI_TD_STALLED)
1103 xfer->status = USBD_STALLED;
1104 else
1105 xfer->status = USBD_IOERROR; /* more info XXX */
1106 } else {
1107 xfer->status = USBD_NORMAL_COMPLETION;
1108 }
1109 xfer->hcpriv = ii;
1110 usb_transfer_complete(xfer);
1111 }
1112
1113 /*
1114 * Called when a request does not complete.
1115 */
1116 void
1117 uhci_timeout(addr)
1118 void *addr;
1119 {
1120 uhci_intr_info_t *ii = addr;
1121
1122 DPRINTF(("uhci_timeout: ii=%p\n", ii));
1123
1124 #ifdef UHCI_DEBUG
1125 if (uhcidebug > 10)
1126 uhci_dump_tds(ii->stdstart);
1127 #endif
1128
1129 ii->xfer->device->bus->intr_context++;
1130 uhci_abort_xfer(ii->xfer, USBD_TIMEOUT);
1131 ii->xfer->device->bus->intr_context--;
1132 }
1133
1134 /*
1135 * Wait here until controller claims to have an interrupt.
1136 * Then call uhci_intr and return. Use timeout to avoid waiting
1137 * too long.
1138 * Only used during boot when interrupts are not enabled yet.
1139 */
1140 void
1141 uhci_waitintr(sc, xfer)
1142 uhci_softc_t *sc;
1143 usbd_xfer_handle xfer;
1144 {
1145 int timo = xfer->timeout;
1146 uhci_intr_info_t *ii;
1147
1148 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1149
1150 xfer->status = USBD_IN_PROGRESS;
1151 for (; timo >= 0; timo--) {
1152 usb_delay_ms(&sc->sc_bus, 1);
1153 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1154 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
1155 uhci_intr(sc);
1156 if (xfer->status != USBD_IN_PROGRESS)
1157 return;
1158 }
1159 }
1160
1161 /* Timeout */
1162 DPRINTF(("uhci_waitintr: timeout\n"));
1163 for (ii = LIST_FIRST(&sc->sc_intrhead);
1164 ii != NULL && ii->xfer != xfer;
1165 ii = LIST_NEXT(ii, list))
1166 ;
1167 #ifdef DIAGNOSTIC
1168 if (ii == NULL)
1169 panic("uhci_waitintr: lost intr_info\n");
1170 #endif
1171 uhci_idone(ii);
1172 }
1173
1174 void
1175 uhci_poll(bus)
1176 struct usbd_bus *bus;
1177 {
1178 uhci_softc_t *sc = (uhci_softc_t *)bus;
1179
1180 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
1181 uhci_intr(sc);
1182 }
1183
1184 #if 0
1185 void
1186 uhci_reset(p)
1187 void *p;
1188 {
1189 uhci_softc_t *sc = p;
1190 int n;
1191
1192 UHCICMD(sc, UHCI_CMD_HCRESET);
1193 /* The reset bit goes low when the controller is done. */
1194 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1195 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1196 delay(100);
1197 if (n >= UHCI_RESET_TIMEOUT)
1198 printf("%s: controller did not reset\n",
1199 USBDEVNAME(sc->sc_bus.bdev));
1200 }
1201 #endif
1202
1203 usbd_status
1204 uhci_run(sc, run)
1205 uhci_softc_t *sc;
1206 int run;
1207 {
1208 int s, n, running;
1209
1210 run = run != 0;
1211 s = splusb();
1212 DPRINTF(("uhci_run: setting run=%d\n", run));
1213 UHCICMD(sc, run ? UHCI_CMD_RS : 0);
1214 for(n = 0; n < 10; n++) {
1215 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1216 /* return when we've entered the state we want */
1217 if (run == running) {
1218 splx(s);
1219 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1220 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1221 return (USBD_NORMAL_COMPLETION);
1222 }
1223 usb_delay_ms(&sc->sc_bus, 1);
1224 }
1225 splx(s);
1226 printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
1227 run ? "start" : "stop");
1228 return (USBD_IOERROR);
1229 }
1230
1231 /*
1232 * Memory management routines.
1233 * uhci_alloc_std allocates TDs
1234 * uhci_alloc_sqh allocates QHs
1235 * These two routines do their own free list management,
1236 * partly for speed, partly because allocating DMAable memory
1237 * has page size granularaity so much memory would be wasted if
1238 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1239 */
1240
1241 uhci_soft_td_t *
1242 uhci_alloc_std(sc)
1243 uhci_softc_t *sc;
1244 {
1245 uhci_soft_td_t *std;
1246 usbd_status err;
1247 int i, offs;
1248 usb_dma_t dma;
1249
1250 if (sc->sc_freetds == NULL) {
1251 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1252 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1253 UHCI_TD_ALIGN, &dma);
1254 if (err)
1255 return (0);
1256 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1257 offs = i * UHCI_STD_SIZE;
1258 std = (uhci_soft_td_t *)((char *)KERNADDR(&dma) +offs);
1259 std->physaddr = DMAADDR(&dma) + offs;
1260 std->link.std = sc->sc_freetds;
1261 sc->sc_freetds = std;
1262 }
1263 }
1264 std = sc->sc_freetds;
1265 sc->sc_freetds = std->link.std;
1266 memset(&std->td, 0, sizeof(uhci_td_t));
1267 return std;
1268 }
1269
1270 void
1271 uhci_free_std(sc, std)
1272 uhci_softc_t *sc;
1273 uhci_soft_td_t *std;
1274 {
1275 #ifdef DIAGNOSTIC
1276 #define TD_IS_FREE 0x12345678
1277 if (std->td.td_token == LE(TD_IS_FREE)) {
1278 printf("uhci_free_std: freeing free TD %p\n", std);
1279 return;
1280 }
1281 std->td.td_token = LE(TD_IS_FREE);
1282 #endif
1283 std->link.std = sc->sc_freetds;
1284 sc->sc_freetds = std;
1285 }
1286
1287 uhci_soft_qh_t *
1288 uhci_alloc_sqh(sc)
1289 uhci_softc_t *sc;
1290 {
1291 uhci_soft_qh_t *sqh;
1292 usbd_status err;
1293 int i, offs;
1294 usb_dma_t dma;
1295
1296 if (sc->sc_freeqhs == NULL) {
1297 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1298 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1299 UHCI_QH_ALIGN, &dma);
1300 if (err)
1301 return (0);
1302 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1303 offs = i * UHCI_SQH_SIZE;
1304 sqh = (uhci_soft_qh_t *)((char *)KERNADDR(&dma) +offs);
1305 sqh->physaddr = DMAADDR(&dma) + offs;
1306 sqh->hlink = sc->sc_freeqhs;
1307 sc->sc_freeqhs = sqh;
1308 }
1309 }
1310 sqh = sc->sc_freeqhs;
1311 sc->sc_freeqhs = sqh->hlink;
1312 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1313 return (sqh);
1314 }
1315
1316 void
1317 uhci_free_sqh(sc, sqh)
1318 uhci_softc_t *sc;
1319 uhci_soft_qh_t *sqh;
1320 {
1321 sqh->hlink = sc->sc_freeqhs;
1322 sc->sc_freeqhs = sqh;
1323 }
1324
1325 #if 0
1326 /*
1327 * Enter a list of transfers onto a control queue.
1328 * Called at splusb()
1329 */
1330 void
1331 uhci_enter_ctl_q(sc, sqh, ii)
1332 uhci_softc_t *sc;
1333 uhci_soft_qh_t *sqh;
1334 uhci_intr_info_t *ii;
1335 {
1336 DPRINTFN(5, ("uhci_enter_ctl_q: sqh=%p\n", sqh));
1337
1338 }
1339 #endif
1340
1341 void
1342 uhci_free_std_chain(sc, std, stdend)
1343 uhci_softc_t *sc;
1344 uhci_soft_td_t *std;
1345 uhci_soft_td_t *stdend;
1346 {
1347 uhci_soft_td_t *p;
1348
1349 for (; std != stdend; std = p) {
1350 p = std->link.std;
1351 uhci_free_std(sc, std);
1352 }
1353 }
1354
1355 usbd_status
1356 uhci_alloc_std_chain(upipe, sc, len, rd, shortok, dma, sp, ep)
1357 struct uhci_pipe *upipe;
1358 uhci_softc_t *sc;
1359 int len, rd, shortok;
1360 usb_dma_t *dma;
1361 uhci_soft_td_t **sp, **ep;
1362 {
1363 uhci_soft_td_t *p, *lastp;
1364 uhci_physaddr_t lastlink;
1365 int i, ntd, l, tog, maxp;
1366 u_int32_t status;
1367 int addr = upipe->pipe.device->address;
1368 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1369
1370 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d ls=%d "
1371 "shortok=%d\n", addr, UE_GET_ADDR(endpt), len,
1372 upipe->pipe.device->lowspeed, shortok));
1373 if (len == 0) {
1374 *sp = *ep = 0;
1375 DPRINTFN(-1,("uhci_alloc_std_chain: len=0\n"));
1376 return (USBD_NORMAL_COMPLETION);
1377 }
1378 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1379 if (maxp == 0) {
1380 printf("uhci_alloc_std_chain: maxp=0\n");
1381 return (USBD_INVAL);
1382 }
1383 ntd = (len + maxp - 1) / maxp;
1384 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1385 tog = upipe->nexttoggle;
1386 if (ntd % 2 == 0)
1387 tog ^= 1;
1388 upipe->nexttoggle = tog ^ 1;
1389 lastp = 0;
1390 lastlink = UHCI_PTR_T;
1391 ntd--;
1392 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1393 if (upipe->pipe.device->lowspeed)
1394 status |= UHCI_TD_LS;
1395 if (shortok)
1396 status |= UHCI_TD_SPD;
1397 for (i = ntd; i >= 0; i--) {
1398 p = uhci_alloc_std(sc);
1399 if (p == NULL) {
1400 uhci_free_std_chain(sc, lastp, 0);
1401 return (USBD_NOMEM);
1402 }
1403 p->link.std = lastp;
1404 if (lastlink == UHCI_PTR_T)
1405 p->td.td_link = LE(lastlink);
1406 else
1407 p->td.td_link = LE(lastlink|UHCI_PTR_VF);
1408 lastp = p;
1409 lastlink = p->physaddr;
1410 p->td.td_status = LE(status);
1411 if (i == ntd) {
1412 /* last TD */
1413 l = len % maxp;
1414 if (l == 0) l = maxp;
1415 *ep = p;
1416 } else
1417 l = maxp;
1418 p->td.td_token =
1419 LE(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1420 UHCI_TD_OUT(l, endpt, addr, tog));
1421 p->td.td_buffer = LE(DMAADDR(dma) + i * maxp);
1422 tog ^= 1;
1423 }
1424 *sp = lastp;
1425 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1426 upipe->nexttoggle));
1427 return (USBD_NORMAL_COMPLETION);
1428 }
1429
1430 void
1431 uhci_device_clear_toggle(pipe)
1432 usbd_pipe_handle pipe;
1433 {
1434 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1435 upipe->nexttoggle = 0;
1436 }
1437
1438 void
1439 uhci_noop(pipe)
1440 usbd_pipe_handle pipe;
1441 {
1442 }
1443
1444 usbd_status
1445 uhci_device_bulk_transfer(xfer)
1446 usbd_xfer_handle xfer;
1447 {
1448 usbd_status err;
1449
1450 /* Insert last in queue. */
1451 err = usb_insert_transfer(xfer);
1452 if (err)
1453 return (err);
1454
1455 /* Pipe isn't running (otherwise err would be USBD_INPROG),
1456 * start first
1457 */
1458 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1459 }
1460
1461 usbd_status
1462 uhci_device_bulk_start(xfer)
1463 usbd_xfer_handle xfer;
1464 {
1465 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1466 usbd_device_handle dev = upipe->pipe.device;
1467 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1468 uhci_intr_info_t *ii = upipe->iinfo;
1469 uhci_soft_td_t *data, *dataend;
1470 uhci_soft_qh_t *sqh;
1471 usbd_status err;
1472 int len, isread, endpt;
1473 int s;
1474
1475 DPRINTFN(3, ("uhci_device_bulk_transfer: xfer=%p len=%d flags=%d\n",
1476 xfer, xfer->length, xfer->flags));
1477
1478 #ifdef DIAGNOSTIC
1479 if (xfer->rqflags & URQ_REQUEST)
1480 panic("uhci_device_bulk_transfer: a request\n");
1481 #endif
1482
1483 len = xfer->length;
1484 endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
1485 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1486 sqh = upipe->u.bulk.sqh;
1487
1488 upipe->u.bulk.isread = isread;
1489 upipe->u.bulk.length = len;
1490
1491 err = uhci_alloc_std_chain(upipe, sc, len, isread,
1492 xfer->flags & USBD_SHORT_XFER_OK,
1493 &xfer->dmabuf, &data, &dataend);
1494 if (err)
1495 return (err);
1496 dataend->td.td_status |= LE(UHCI_TD_IOC);
1497
1498 #ifdef UHCI_DEBUG
1499 if (uhcidebug > 8) {
1500 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1501 uhci_dump_tds(data);
1502 }
1503 #endif
1504
1505 /* Set up interrupt info. */
1506 ii->xfer = xfer;
1507 ii->stdstart = data;
1508 ii->stdend = dataend;
1509 #if defined(__FreeBSD__)
1510 callout_handle_init(&ii->timeout_handle);
1511 #endif
1512 #ifdef DIAGNOSTIC
1513 ii->isdone = 0;
1514 #endif
1515
1516 sqh->elink = data;
1517 sqh->qh.qh_elink = LE(data->physaddr);
1518 sqh->intr_info = ii;
1519
1520 s = splusb();
1521 uhci_add_bulk(sc, sqh);
1522 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
1523
1524 if (xfer->timeout && !sc->sc_bus.use_polling) {
1525 usb_timeout(uhci_timeout, ii, MS_TO_TICKS(xfer->timeout),
1526 ii->timeout_handle);
1527 }
1528 splx(s);
1529
1530 #ifdef UHCI_DEBUG
1531 if (uhcidebug > 10) {
1532 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1533 uhci_dump_tds(data);
1534 }
1535 #endif
1536
1537 if (sc->sc_bus.use_polling)
1538 uhci_waitintr(sc, xfer);
1539
1540 return (USBD_IN_PROGRESS);
1541 }
1542
1543 /* Abort a device bulk request. */
1544 void
1545 uhci_device_bulk_abort(xfer)
1546 usbd_xfer_handle xfer;
1547 {
1548 DPRINTF(("uhci_device_bulk_abort:\n"));
1549 uhci_abort_xfer(xfer, USBD_CANCELLED);
1550 }
1551
1552 void
1553 uhci_abort_xfer(xfer, status)
1554 usbd_xfer_handle xfer;
1555 usbd_status status;
1556 {
1557 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1558 uhci_intr_info_t *ii = upipe->iinfo;
1559 uhci_soft_td_t *std;
1560
1561 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1562
1563 /* Make interrupt routine ignore it, */
1564 xfer->status = status;
1565
1566 /* don't timeout, */
1567 usb_untimeout(uhci_timeout, ii, ii->timeout_handle);
1568
1569 /* make hardware ignore it, */
1570 for (std = ii->stdstart; std != 0; std = std->link.std)
1571 std->td.td_status &= LE(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1572
1573 xfer->hcpriv = ii;
1574
1575 /* make sure hardware has completed, */
1576 if (xfer->device->bus->intr_context) {
1577 /* We have no process context, so we can't use tsleep(). */
1578 timeout(uhci_abort_xfer_end, xfer, hz / USB_FRAMES_PER_SECOND);
1579 } else {
1580 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
1581 KASSERT(intr_nesting_level == 0,
1582 ("ohci_abort_req in interrupt context"));
1583 #endif
1584 usb_delay_ms(xfer->pipe->device->bus, 1);
1585 /* and call final part of interrupt handler. */
1586 uhci_abort_xfer_end(xfer);
1587 }
1588 }
1589
1590 void
1591 uhci_abort_xfer_end(v)
1592 void *v;
1593 {
1594 usbd_xfer_handle xfer = v;
1595 int s;
1596
1597 s = splusb();
1598 usb_transfer_complete(xfer);
1599 splx(s);
1600 }
1601
1602 /* Close a device bulk pipe. */
1603 void
1604 uhci_device_bulk_close(pipe)
1605 usbd_pipe_handle pipe;
1606 {
1607 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1608 usbd_device_handle dev = upipe->pipe.device;
1609 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1610
1611 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1612 uhci_free_intr_info(upipe->iinfo);
1613 /* XXX free other resources */
1614 }
1615
1616 usbd_status
1617 uhci_device_ctrl_transfer(xfer)
1618 usbd_xfer_handle xfer;
1619 {
1620 usbd_status err;
1621
1622 /* Insert last in queue. */
1623 err = usb_insert_transfer(xfer);
1624 if (err)
1625 return (err);
1626
1627 /* Pipe isn't running (otherwise err would be USBD_INPROG),
1628 * start first
1629 */
1630 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1631 }
1632
1633 usbd_status
1634 uhci_device_ctrl_start(xfer)
1635 usbd_xfer_handle xfer;
1636 {
1637 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
1638 usbd_status err;
1639
1640 #ifdef DIAGNOSTIC
1641 if (!(xfer->rqflags & URQ_REQUEST))
1642 panic("uhci_device_ctrl_transfer: not a request\n");
1643 #endif
1644
1645 err = uhci_device_request(xfer);
1646 if (err)
1647 return (err);
1648
1649 if (sc->sc_bus.use_polling)
1650 uhci_waitintr(sc, xfer);
1651 return (USBD_IN_PROGRESS);
1652 }
1653
1654 usbd_status
1655 uhci_device_intr_transfer(xfer)
1656 usbd_xfer_handle xfer;
1657 {
1658 usbd_status err;
1659
1660 /* Insert last in queue. */
1661 err = usb_insert_transfer(xfer);
1662 if (err)
1663 return (err);
1664
1665 /* Pipe isn't running (otherwise err would be USBD_INPROG),
1666 * start first
1667 */
1668 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1669 }
1670
1671 usbd_status
1672 uhci_device_intr_start(xfer)
1673 usbd_xfer_handle xfer;
1674 {
1675 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1676 usbd_device_handle dev = upipe->pipe.device;
1677 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1678 uhci_intr_info_t *ii = upipe->iinfo;
1679 uhci_soft_td_t *data, *dataend;
1680 uhci_soft_qh_t *sqh;
1681 usbd_status err;
1682 int i, s;
1683
1684 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
1685 xfer, xfer->length, xfer->flags));
1686
1687 #ifdef DIAGNOSTIC
1688 if (xfer->rqflags & URQ_REQUEST)
1689 panic("uhci_device_intr_transfer: a request\n");
1690 #endif
1691
1692 err = uhci_alloc_std_chain(upipe, sc, xfer->length, 1,
1693 xfer->flags & USBD_SHORT_XFER_OK,
1694 &xfer->dmabuf, &data, &dataend);
1695 if (err)
1696 return (err);
1697 dataend->td.td_status |= LE(UHCI_TD_IOC);
1698
1699 #ifdef UHCI_DEBUG
1700 if (uhcidebug > 10) {
1701 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
1702 uhci_dump_tds(data);
1703 uhci_dump_qh(upipe->u.intr.qhs[0]);
1704 }
1705 #endif
1706
1707 s = splusb();
1708 /* Set up interrupt info. */
1709 ii->xfer = xfer;
1710 ii->stdstart = data;
1711 ii->stdend = dataend;
1712 #if defined(__FreeBSD__)
1713 callout_handle_init(&ii->timeout_handle);
1714 #endif
1715 #ifdef DIAGNOSTIC
1716 ii->isdone = 0;
1717 #endif
1718
1719 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
1720 upipe->u.intr.qhs[0]));
1721 for (i = 0; i < upipe->u.intr.npoll; i++) {
1722 sqh = upipe->u.intr.qhs[i];
1723 sqh->elink = data;
1724 sqh->qh.qh_elink = LE(data->physaddr);
1725 }
1726 splx(s);
1727
1728 #ifdef UHCI_DEBUG
1729 if (uhcidebug > 10) {
1730 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
1731 uhci_dump_tds(data);
1732 uhci_dump_qh(upipe->u.intr.qhs[0]);
1733 }
1734 #endif
1735
1736 return (USBD_IN_PROGRESS);
1737 }
1738
1739 /* Abort a device control request. */
1740 void
1741 uhci_device_ctrl_abort(xfer)
1742 usbd_xfer_handle xfer;
1743 {
1744 DPRINTF(("uhci_device_ctrl_abort:\n"));
1745 uhci_abort_xfer(xfer, USBD_CANCELLED);
1746 }
1747
1748 /* Close a device control pipe. */
1749 void
1750 uhci_device_ctrl_close(pipe)
1751 usbd_pipe_handle pipe;
1752 {
1753 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1754
1755 uhci_free_intr_info(upipe->iinfo);
1756 /* XXX free other resources? */
1757 }
1758
1759 /* Abort a device interrupt request. */
1760 void
1761 uhci_device_intr_abort(xfer)
1762 usbd_xfer_handle xfer;
1763 {
1764 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
1765 if (xfer->pipe->intrxfer == xfer) {
1766 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
1767 xfer->pipe->intrxfer = 0;
1768 }
1769 uhci_abort_xfer(xfer, USBD_CANCELLED);
1770 }
1771
1772 /* Close a device interrupt pipe. */
1773 void
1774 uhci_device_intr_close(pipe)
1775 usbd_pipe_handle pipe;
1776 {
1777 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1778 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
1779 int i, s, npoll;
1780
1781 upipe->iinfo->stdstart = 0; /* inactive */
1782
1783 /* Unlink descriptors from controller data structures. */
1784 npoll = upipe->u.intr.npoll;
1785 uhci_lock_frames(sc);
1786 for (i = 0; i < npoll; i++)
1787 uhci_remove_intr(sc, upipe->u.intr.qhs[i]->pos,
1788 upipe->u.intr.qhs[i]);
1789 uhci_unlock_frames(sc);
1790
1791 /*
1792 * We now have to wait for any activity on the physical
1793 * descriptors to stop.
1794 */
1795 usb_delay_ms(&sc->sc_bus, 2);
1796
1797 for(i = 0; i < npoll; i++)
1798 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
1799 free(upipe->u.intr.qhs, M_USBHC);
1800
1801 s = splusb();
1802 LIST_REMOVE(upipe->iinfo, list); /* remove from active list */
1803 splx(s);
1804 uhci_free_intr_info(upipe->iinfo);
1805
1806 /* XXX free other resources */
1807 }
1808
1809 usbd_status
1810 uhci_device_request(xfer)
1811 usbd_xfer_handle xfer;
1812 {
1813 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1814 usb_device_request_t *req = &xfer->request;
1815 usbd_device_handle dev = upipe->pipe.device;
1816 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1817 int addr = dev->address;
1818 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1819 uhci_intr_info_t *ii = upipe->iinfo;
1820 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
1821 uhci_soft_qh_t *sqh;
1822 int len;
1823 u_int32_t ls;
1824 usbd_status err;
1825 int isread;
1826 int s;
1827
1828 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
1829 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1830 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1831 UGETW(req->wIndex), UGETW(req->wLength),
1832 addr, endpt));
1833
1834 ls = dev->lowspeed ? UHCI_TD_LS : 0;
1835 isread = req->bmRequestType & UT_READ;
1836 len = UGETW(req->wLength);
1837
1838 setup = upipe->u.ctl.setup;
1839 stat = upipe->u.ctl.stat;
1840 sqh = upipe->u.ctl.sqh;
1841
1842 /* Set up data transaction */
1843 if (len != 0) {
1844 upipe->nexttoggle = 1;
1845 err = uhci_alloc_std_chain(upipe, sc, len, isread,
1846 xfer->flags & USBD_SHORT_XFER_OK,
1847 &xfer->dmabuf, &data, &dataend);
1848 if (err)
1849 return (err);
1850 next = data;
1851 dataend->link.std = stat;
1852 dataend->td.td_link = LE(stat->physaddr | UHCI_PTR_VF);
1853 } else {
1854 next = stat;
1855 }
1856 upipe->u.ctl.length = len;
1857
1858 memcpy(KERNADDR(&upipe->u.ctl.reqdma), req, sizeof *req);
1859
1860 setup->link.std = next;
1861 setup->td.td_link = LE(next->physaddr | UHCI_PTR_VF);
1862 setup->td.td_status = LE(UHCI_TD_SET_ERRCNT(3) | ls | UHCI_TD_ACTIVE);
1863 setup->td.td_token = LE(UHCI_TD_SETUP(sizeof *req, endpt, addr));
1864 setup->td.td_buffer = LE(DMAADDR(&upipe->u.ctl.reqdma));
1865
1866 stat->link.std = 0;
1867 stat->td.td_link = LE(UHCI_PTR_T);
1868 stat->td.td_status = LE(UHCI_TD_SET_ERRCNT(3) | ls |
1869 UHCI_TD_ACTIVE | UHCI_TD_IOC);
1870 stat->td.td_token =
1871 LE(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
1872 UHCI_TD_IN (0, endpt, addr, 1));
1873 stat->td.td_buffer = LE(0);
1874
1875 #ifdef UHCI_DEBUG
1876 if (uhcidebug > 10) {
1877 DPRINTF(("uhci_device_request: before transfer\n"));
1878 uhci_dump_tds(setup);
1879 }
1880 #endif
1881
1882 /* Set up interrupt info. */
1883 ii->xfer = xfer;
1884 ii->stdstart = setup;
1885 ii->stdend = stat;
1886 #if defined(__FreeBSD__)
1887 callout_handle_init(&ii->timeout_handle);
1888 #endif
1889 #ifdef DIAGNOSTIC
1890 ii->isdone = 0;
1891 #endif
1892
1893 sqh->elink = setup;
1894 sqh->qh.qh_elink = LE(setup->physaddr);
1895 sqh->intr_info = ii;
1896
1897 s = splusb();
1898 uhci_add_ctrl(sc, sqh);
1899 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
1900 #ifdef UHCI_DEBUG
1901 if (uhcidebug > 12) {
1902 uhci_soft_td_t *std;
1903 uhci_soft_qh_t *xqh;
1904 uhci_soft_qh_t *sxqh;
1905 int maxqh = 0;
1906 uhci_physaddr_t link;
1907 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
1908 for (std = sc->sc_vframes[0].htd, link = 0;
1909 (link & UHCI_PTR_Q) == 0;
1910 std = std->link.std) {
1911 link = LE(std->td.td_link);
1912 uhci_dump_td(std);
1913 }
1914 sxqh = (uhci_soft_qh_t *)std;
1915 uhci_dump_qh(sxqh);
1916 for (xqh = sxqh;
1917 xqh != NULL;
1918 xqh = (maxqh++ == 5 || xqh->hlink==sxqh ||
1919 xqh->hlink==xqh ? NULL : xqh->hlink)) {
1920 uhci_dump_qh(xqh);
1921 }
1922 DPRINTF(("Enqueued QH:\n"));
1923 uhci_dump_qh(sqh);
1924 uhci_dump_tds(sqh->elink);
1925 }
1926 #endif
1927 if (xfer->timeout && !sc->sc_bus.use_polling) {
1928 usb_timeout(uhci_timeout, ii,
1929 MS_TO_TICKS(xfer->timeout), ii->timeout_handle);
1930 }
1931 splx(s);
1932
1933 return (USBD_NORMAL_COMPLETION);
1934 }
1935
1936 usbd_status
1937 uhci_device_isoc_transfer(xfer)
1938 usbd_xfer_handle xfer;
1939 {
1940 usbd_status err;
1941
1942 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
1943
1944 /* Put it on our queue, */
1945 err = usb_insert_transfer(xfer);
1946
1947 /* bail out on error, */
1948 if (err && err != USBD_IN_PROGRESS)
1949 return (err);
1950
1951 /* XXX should check inuse here */
1952
1953 /* insert into schedule, */
1954 uhci_device_isoc_enter(xfer);
1955
1956 /* and put on interrupt list if the pipe wasn't running */
1957 if (!err)
1958 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
1959
1960 return (err);
1961 }
1962
1963 void
1964 uhci_device_isoc_enter(xfer)
1965 usbd_xfer_handle xfer;
1966 {
1967 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1968 usbd_device_handle dev = upipe->pipe.device;
1969 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1970 struct iso *iso = &upipe->u.iso;
1971 uhci_soft_td_t *std;
1972 u_int32_t buf, len, status;
1973 int s, i, next, nframes;
1974
1975 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
1976 "nframes=%d\n",
1977 iso->inuse, iso->next, xfer, xfer->nframes));
1978
1979 if (xfer->status == USBD_IN_PROGRESS) {
1980 /* This request has already been entered into the frame list */
1981 /* XXX */
1982 }
1983
1984 #ifdef DIAGNOSTIC
1985 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
1986 printf("uhci_device_isoc_enter: overflow!\n");
1987 #endif
1988
1989 next = iso->next;
1990 if (next == -1) {
1991 /* Not in use yet, schedule it a few frames ahead. */
1992 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
1993 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
1994 }
1995
1996 xfer->status = USBD_IN_PROGRESS;
1997 xfer->hcprivint = next;
1998
1999 buf = DMAADDR(&xfer->dmabuf);
2000 status = LE(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2001 UHCI_TD_ACTIVE |
2002 UHCI_TD_IOS));
2003 nframes = xfer->nframes;
2004 s = splusb();
2005 for (i = 0; i < nframes; i++) {
2006 std = iso->stds[next];
2007 if (++next >= UHCI_VFRAMELIST_COUNT)
2008 next = 0;
2009 len = xfer->frlengths[i];
2010 std->td.td_buffer = LE(buf);
2011 if (i == nframes - 1)
2012 status |= LE(UHCI_TD_IOC);
2013 std->td.td_status = status;
2014 std->td.td_token &= LE(~UHCI_TD_MAXLEN_MASK);
2015 std->td.td_token |= LE(UHCI_TD_SET_MAXLEN(len));
2016 #ifdef UHCI_DEBUG
2017 if (uhcidebug > 5) {
2018 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2019 uhci_dump_td(std);
2020 }
2021 #endif
2022 buf += len;
2023 }
2024 iso->next = next;
2025 iso->inuse += xfer->nframes;
2026
2027 splx(s);
2028 }
2029
2030 usbd_status
2031 uhci_device_isoc_start(xfer)
2032 usbd_xfer_handle xfer;
2033 {
2034 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2035 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2036 uhci_intr_info_t *ii = upipe->iinfo;
2037 uhci_soft_td_t *end;
2038 int s, i;
2039
2040 #ifdef DIAGNOSTIC
2041 if (xfer->status != USBD_IN_PROGRESS)
2042 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2043 #endif
2044
2045 /* Find the last TD */
2046 i = xfer->hcprivint + xfer->nframes;
2047 if (i >= UHCI_VFRAMELIST_COUNT)
2048 i -= UHCI_VFRAMELIST_COUNT;
2049 end = upipe->u.iso.stds[i];
2050
2051 s = splusb();
2052
2053 /* Set up interrupt info. */
2054 ii->xfer = xfer;
2055 ii->stdstart = end;
2056 ii->stdend = end;
2057 #if defined(__FreeBSD__)
2058 callout_handle_init(&ii->timeout_handle);
2059 #endif
2060 #ifdef DIAGNOSTIC
2061 ii->isdone = 0;
2062 #endif
2063 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
2064
2065 splx(s);
2066
2067 return (USBD_IN_PROGRESS);
2068 }
2069
2070 void
2071 uhci_device_isoc_abort(xfer)
2072 usbd_xfer_handle xfer;
2073 {
2074 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2075 uhci_intr_info_t *ii = upipe->iinfo;
2076 uhci_soft_td_t **stds = upipe->u.iso.stds;
2077 uhci_soft_td_t *std;
2078 int i, n, nframes;
2079
2080 /* Make interrupt routine ignore it, */
2081 xfer->status = USBD_CANCELLED;
2082
2083 /* make hardware ignore it, */
2084 nframes = xfer->nframes;
2085 n = xfer->hcprivint;
2086 for (i = 0; i < nframes; i++) {
2087 std = stds[n];
2088 std->td.td_status &= LE(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2089 if (++n >= UHCI_VFRAMELIST_COUNT)
2090 n = 0;
2091 }
2092
2093 xfer->hcpriv = ii;
2094
2095 /* make sure hardware has completed, */
2096 if (xfer->device->bus->intr_context) {
2097 /* We have no process context, so we can't use tsleep(). */
2098 timeout(uhci_abort_xfer_end, xfer, hz / USB_FRAMES_PER_SECOND);
2099 } else {
2100 usb_delay_ms(xfer->pipe->device->bus, 1);
2101 /* and call final part of interrupt handler. */
2102 uhci_abort_xfer_end(xfer);
2103 }
2104 }
2105
2106 void
2107 uhci_device_isoc_close(pipe)
2108 usbd_pipe_handle pipe;
2109 {
2110 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2111 usbd_device_handle dev = upipe->pipe.device;
2112 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2113 uhci_soft_td_t *std, *vstd;
2114 struct iso *iso;
2115 int i;
2116
2117 /*
2118 * Make sure all TDs are marked as inactive.
2119 * Wait for completion.
2120 * Unschedule.
2121 * Deallocate.
2122 */
2123 iso = &upipe->u.iso;
2124
2125 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2126 iso->stds[i]->td.td_status &= LE(~UHCI_TD_ACTIVE);
2127 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2128
2129 uhci_lock_frames(sc);
2130 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2131 std = iso->stds[i];
2132 for (vstd = sc->sc_vframes[i].htd;
2133 vstd != NULL && vstd->link.std != std;
2134 vstd = vstd->link.std)
2135 ;
2136 if (vstd == NULL) {
2137 /*panic*/
2138 printf("uhci_device_isoc_close: %p not found\n", std);
2139 uhci_unlock_frames(sc);
2140 return;
2141 }
2142 vstd->link = std->link;
2143 vstd->td.td_link = std->td.td_link;
2144 uhci_free_std(sc, std);
2145 }
2146 uhci_unlock_frames(sc);
2147
2148 free(iso->stds, M_USBHC);
2149 }
2150
2151 usbd_status
2152 uhci_setup_isoc(pipe)
2153 usbd_pipe_handle pipe;
2154 {
2155 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2156 usbd_device_handle dev = upipe->pipe.device;
2157 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2158 int addr = upipe->pipe.device->address;
2159 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2160 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2161 uhci_soft_td_t *std, *vstd;
2162 u_int32_t token;
2163 struct iso *iso;
2164 int i;
2165
2166 iso = &upipe->u.iso;
2167 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2168 M_USBHC, M_WAITOK);
2169
2170 token = LE(rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2171 UHCI_TD_OUT(0, endpt, addr, 0));
2172
2173 /* Allocate the TDs and mark as inactive; */
2174 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2175 std = uhci_alloc_std(sc);
2176 if (std == 0)
2177 goto bad;
2178 std->td.td_status = LE(UHCI_TD_IOS); /* iso, inactive */
2179 std->td.td_token = token;
2180 iso->stds[i] = std;
2181 }
2182
2183 /* Insert TDs into schedule. */
2184 uhci_lock_frames(sc);
2185 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2186 std = iso->stds[i];
2187 vstd = sc->sc_vframes[i].htd;
2188 std->link = vstd->link;
2189 std->td.td_link = vstd->td.td_link;
2190 vstd->link.std = std;
2191 vstd->td.td_link = LE(std->physaddr);
2192 }
2193 uhci_unlock_frames(sc);
2194
2195 iso->next = -1;
2196 iso->inuse = 0;
2197
2198 return (USBD_NORMAL_COMPLETION);
2199
2200 bad:
2201 while (--i >= 0)
2202 uhci_free_std(sc, iso->stds[i]);
2203 free(iso->stds, M_USBHC);
2204 return (USBD_NOMEM);
2205 }
2206
2207 void
2208 uhci_device_isoc_done(xfer)
2209 usbd_xfer_handle xfer;
2210 {
2211 uhci_intr_info_t *ii = xfer->hcpriv;
2212
2213 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2214
2215 /* Turn off the interrupt since it is active even if the TD is not. */
2216 ii->stdend->td.td_status &= LE(~UHCI_TD_IOC);
2217
2218 LIST_REMOVE(ii, list); /* remove from active list */
2219 }
2220
2221 void
2222 uhci_device_intr_done(xfer)
2223 usbd_xfer_handle xfer;
2224 {
2225 uhci_intr_info_t *ii = xfer->hcpriv;
2226 uhci_softc_t *sc = ii->sc;
2227 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2228 uhci_soft_qh_t *sqh;
2229 int i, npoll;
2230
2231 DPRINTFN(5, ("uhci_intr_done: length=%d\n", xfer->actlen));
2232
2233 npoll = upipe->u.intr.npoll;
2234 for(i = 0; i < npoll; i++) {
2235 sqh = upipe->u.intr.qhs[i];
2236 sqh->elink = 0;
2237 sqh->qh.qh_elink = LE(UHCI_PTR_T);
2238 }
2239 uhci_free_std_chain(sc, ii->stdstart, 0);
2240
2241 /* XXX Wasteful. */
2242 if (xfer->pipe->repeat) {
2243 uhci_soft_td_t *data, *dataend;
2244
2245 /* This alloc cannot fail since we freed the chain above. */
2246 uhci_alloc_std_chain(upipe, sc, xfer->length, 1,
2247 xfer->flags & USBD_SHORT_XFER_OK,
2248 &xfer->dmabuf, &data, &dataend);
2249 dataend->td.td_status |= LE(UHCI_TD_IOC);
2250
2251 #ifdef UHCI_DEBUG
2252 if (uhcidebug > 10) {
2253 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2254 uhci_dump_tds(data);
2255 uhci_dump_qh(upipe->u.intr.qhs[0]);
2256 }
2257 #endif
2258
2259 ii->stdstart = data;
2260 ii->stdend = dataend;
2261 #if defined(__FreeBSD__)
2262 callout_handle_init(&ii->timeout_handle);
2263 #endif
2264 #ifdef DIAGNOSTIC
2265 ii->isdone = 0;
2266 #endif
2267 for (i = 0; i < npoll; i++) {
2268 sqh = upipe->u.intr.qhs[i];
2269 sqh->elink = data;
2270 sqh->qh.qh_elink = LE(data->physaddr);
2271 }
2272 } else {
2273 ii->stdstart = 0; /* mark as inactive */
2274 }
2275 }
2276
2277 /* Deallocate request data structures */
2278 void
2279 uhci_device_ctrl_done(xfer)
2280 usbd_xfer_handle xfer;
2281 {
2282 uhci_intr_info_t *ii = xfer->hcpriv;
2283 uhci_softc_t *sc = ii->sc;
2284 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2285
2286 #ifdef DIAGNOSTIC
2287 if (!(xfer->rqflags & URQ_REQUEST))
2288 panic("uhci_ctrl_done: not a request\n");
2289 #endif
2290
2291 LIST_REMOVE(ii, list); /* remove from active list */
2292
2293 uhci_remove_ctrl(sc, upipe->u.ctl.sqh);
2294
2295 if (upipe->u.ctl.length != 0)
2296 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2297
2298 DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", xfer->actlen));
2299 }
2300
2301 /* Deallocate request data structures */
2302 void
2303 uhci_device_bulk_done(xfer)
2304 usbd_xfer_handle xfer;
2305 {
2306 uhci_intr_info_t *ii = xfer->hcpriv;
2307 uhci_softc_t *sc = ii->sc;
2308 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2309
2310 LIST_REMOVE(ii, list); /* remove from active list */
2311
2312 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2313
2314 uhci_free_std_chain(sc, ii->stdstart, 0);
2315
2316 DPRINTFN(5, ("uhci_bulk_done: length=%d\n", xfer->actlen));
2317 }
2318
2319 /* Add interrupt QH, called with vflock. */
2320 void
2321 uhci_add_intr(sc, n, sqh)
2322 uhci_softc_t *sc;
2323 int n;
2324 uhci_soft_qh_t *sqh;
2325 {
2326 struct uhci_vframe *vf = &sc->sc_vframes[n];
2327 uhci_soft_qh_t *eqh;
2328
2329 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", n, sqh));
2330 eqh = vf->eqh;
2331 sqh->hlink = eqh->hlink;
2332 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2333 eqh->hlink = sqh;
2334 eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
2335 vf->eqh = sqh;
2336 vf->bandwidth++;
2337 }
2338
2339 /* Remove interrupt QH, called with vflock. */
2340 void
2341 uhci_remove_intr(sc, n, sqh)
2342 uhci_softc_t *sc;
2343 int n;
2344 uhci_soft_qh_t *sqh;
2345 {
2346 struct uhci_vframe *vf = &sc->sc_vframes[n];
2347 uhci_soft_qh_t *pqh;
2348
2349 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", n, sqh));
2350
2351 for (pqh = vf->hqh; pqh->hlink != sqh; pqh = pqh->hlink)
2352 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
2353 if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
2354 DPRINTF(("uhci_remove_intr: QH not found\n"));
2355 return;
2356 }
2357 #else
2358 ;
2359 #endif
2360 pqh->hlink = sqh->hlink;
2361 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2362 if (vf->eqh == sqh)
2363 vf->eqh = pqh;
2364 vf->bandwidth--;
2365 }
2366
2367 usbd_status
2368 uhci_device_setintr(sc, upipe, ival)
2369 uhci_softc_t *sc;
2370 struct uhci_pipe *upipe;
2371 int ival;
2372 {
2373 uhci_soft_qh_t *sqh;
2374 int i, npoll, s;
2375 u_int bestbw, bw, bestoffs, offs;
2376
2377 DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
2378 if (ival == 0) {
2379 printf("uhci_setintr: 0 interval\n");
2380 return (USBD_INVAL);
2381 }
2382
2383 if (ival > UHCI_VFRAMELIST_COUNT)
2384 ival = UHCI_VFRAMELIST_COUNT;
2385 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2386 DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
2387
2388 upipe->u.intr.npoll = npoll;
2389 upipe->u.intr.qhs =
2390 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
2391
2392 /*
2393 * Figure out which offset in the schedule that has most
2394 * bandwidth left over.
2395 */
2396 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2397 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2398 for (bw = i = 0; i < npoll; i++)
2399 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2400 if (bw < bestbw) {
2401 bestbw = bw;
2402 bestoffs = offs;
2403 }
2404 }
2405 DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2406
2407 upipe->iinfo->stdstart = 0;
2408 for(i = 0; i < npoll; i++) {
2409 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2410 sqh->elink = 0;
2411 sqh->qh.qh_elink = LE(UHCI_PTR_T);
2412 sqh->pos = MOD(i * ival + bestoffs);
2413 sqh->intr_info = upipe->iinfo;
2414 }
2415 #undef MOD
2416
2417 s = splusb();
2418 LIST_INSERT_HEAD(&sc->sc_intrhead, upipe->iinfo, list);
2419 splx(s);
2420
2421 uhci_lock_frames(sc);
2422 /* Enter QHs into the controller data structures. */
2423 for(i = 0; i < npoll; i++)
2424 uhci_add_intr(sc, upipe->u.intr.qhs[i]->pos,
2425 upipe->u.intr.qhs[i]);
2426 uhci_unlock_frames(sc);
2427
2428 DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
2429 return (USBD_NORMAL_COMPLETION);
2430 }
2431
2432 /* Open a new pipe. */
2433 usbd_status
2434 uhci_open(pipe)
2435 usbd_pipe_handle pipe;
2436 {
2437 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2438 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2439 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2440 usbd_status err;
2441
2442 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2443 pipe, pipe->device->address,
2444 ed->bEndpointAddress, sc->sc_addr));
2445 if (pipe->device->address == sc->sc_addr) {
2446 switch (ed->bEndpointAddress) {
2447 case USB_CONTROL_ENDPOINT:
2448 pipe->methods = &uhci_root_ctrl_methods;
2449 break;
2450 case UE_DIR_IN | UHCI_INTR_ENDPT:
2451 pipe->methods = &uhci_root_intr_methods;
2452 break;
2453 default:
2454 return (USBD_INVAL);
2455 }
2456 } else {
2457 upipe->iinfo = uhci_alloc_intr_info(sc);
2458 if (upipe->iinfo == 0)
2459 return (USBD_NOMEM);
2460 switch (ed->bmAttributes & UE_XFERTYPE) {
2461 case UE_CONTROL:
2462 pipe->methods = &uhci_device_ctrl_methods;
2463 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2464 if (upipe->u.ctl.sqh == NULL)
2465 goto bad;
2466 upipe->u.ctl.setup = uhci_alloc_std(sc);
2467 if (upipe->u.ctl.setup == NULL) {
2468 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2469 goto bad;
2470 }
2471 upipe->u.ctl.stat = uhci_alloc_std(sc);
2472 if (upipe->u.ctl.stat == NULL) {
2473 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2474 uhci_free_std(sc, upipe->u.ctl.setup);
2475 goto bad;
2476 }
2477 err = usb_allocmem(&sc->sc_bus,
2478 sizeof(usb_device_request_t),
2479 0, &upipe->u.ctl.reqdma);
2480 if (err) {
2481 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2482 uhci_free_std(sc, upipe->u.ctl.setup);
2483 uhci_free_std(sc, upipe->u.ctl.stat);
2484 goto bad;
2485 }
2486 break;
2487 case UE_INTERRUPT:
2488 pipe->methods = &uhci_device_intr_methods;
2489 return (uhci_device_setintr(sc, upipe, ed->bInterval));
2490 case UE_ISOCHRONOUS:
2491 pipe->methods = &uhci_device_isoc_methods;
2492 return (uhci_setup_isoc(pipe));
2493 case UE_BULK:
2494 pipe->methods = &uhci_device_bulk_methods;
2495 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2496 if (upipe->u.bulk.sqh == NULL)
2497 goto bad;
2498 break;
2499 }
2500 }
2501 return (USBD_NORMAL_COMPLETION);
2502
2503 bad:
2504 uhci_free_intr_info(upipe->iinfo);
2505 return (USBD_NOMEM);
2506 }
2507
2508 /*
2509 * Data structures and routines to emulate the root hub.
2510 */
2511 usb_device_descriptor_t uhci_devd = {
2512 USB_DEVICE_DESCRIPTOR_SIZE,
2513 UDESC_DEVICE, /* type */
2514 {0x00, 0x01}, /* USB version */
2515 UCLASS_HUB, /* class */
2516 USUBCLASS_HUB, /* subclass */
2517 0, /* protocol */
2518 64, /* max packet */
2519 {0},{0},{0x00,0x01}, /* device id */
2520 1,2,0, /* string indicies */
2521 1 /* # of configurations */
2522 };
2523
2524 usb_config_descriptor_t uhci_confd = {
2525 USB_CONFIG_DESCRIPTOR_SIZE,
2526 UDESC_CONFIG,
2527 {USB_CONFIG_DESCRIPTOR_SIZE +
2528 USB_INTERFACE_DESCRIPTOR_SIZE +
2529 USB_ENDPOINT_DESCRIPTOR_SIZE},
2530 1,
2531 1,
2532 0,
2533 UC_SELF_POWERED,
2534 0 /* max power */
2535 };
2536
2537 usb_interface_descriptor_t uhci_ifcd = {
2538 USB_INTERFACE_DESCRIPTOR_SIZE,
2539 UDESC_INTERFACE,
2540 0,
2541 0,
2542 1,
2543 UCLASS_HUB,
2544 USUBCLASS_HUB,
2545 0,
2546 0
2547 };
2548
2549 usb_endpoint_descriptor_t uhci_endpd = {
2550 USB_ENDPOINT_DESCRIPTOR_SIZE,
2551 UDESC_ENDPOINT,
2552 UE_DIR_IN | UHCI_INTR_ENDPT,
2553 UE_INTERRUPT,
2554 {8},
2555 255
2556 };
2557
2558 usb_hub_descriptor_t uhci_hubd_piix = {
2559 USB_HUB_DESCRIPTOR_SIZE,
2560 UDESC_HUB,
2561 2,
2562 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2563 50, /* power on to power good */
2564 0,
2565 { 0x00 }, /* both ports are removable */
2566 };
2567
2568 int
2569 uhci_str(p, l, s)
2570 usb_string_descriptor_t *p;
2571 int l;
2572 char *s;
2573 {
2574 int i;
2575
2576 if (l == 0)
2577 return (0);
2578 p->bLength = 2 * strlen(s) + 2;
2579 if (l == 1)
2580 return (1);
2581 p->bDescriptorType = UDESC_STRING;
2582 l -= 2;
2583 for (i = 0; s[i] && l > 1; i++, l -= 2)
2584 USETW2(p->bString[i], 0, s[i]);
2585 return (2*i+2);
2586 }
2587
2588 /*
2589 * Simulate a hardware hub by handling all the necessary requests.
2590 */
2591 usbd_status
2592 uhci_root_ctrl_transfer(xfer)
2593 usbd_xfer_handle xfer;
2594 {
2595 usbd_status err;
2596
2597 /* Insert last in queue. */
2598 err = usb_insert_transfer(xfer);
2599 if (err)
2600 return (err);
2601
2602 /* Pipe isn't running (otherwise err would be USBD_INPROG),
2603 * start first
2604 */
2605 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2606 }
2607
2608 usbd_status
2609 uhci_root_ctrl_start(xfer)
2610 usbd_xfer_handle xfer;
2611 {
2612 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2613 usb_device_request_t *req;
2614 void *buf = NULL;
2615 int port, x;
2616 int s, len, value, index, status, change, l, totlen = 0;
2617 usb_port_status_t ps;
2618 usbd_status err;
2619
2620 #ifdef DIAGNOSTIC
2621 if (!(xfer->rqflags & URQ_REQUEST))
2622 panic("uhci_root_ctrl_transfer: not a request\n");
2623 #endif
2624 req = &xfer->request;
2625
2626 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
2627 req->bmRequestType, req->bRequest));
2628
2629 len = UGETW(req->wLength);
2630 value = UGETW(req->wValue);
2631 index = UGETW(req->wIndex);
2632
2633 if (len != 0)
2634 buf = KERNADDR(&xfer->dmabuf);
2635
2636 #define C(x,y) ((x) | ((y) << 8))
2637 switch(C(req->bRequest, req->bmRequestType)) {
2638 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2639 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2640 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2641 /*
2642 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2643 * for the integrated root hub.
2644 */
2645 break;
2646 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2647 if (len > 0) {
2648 *(u_int8_t *)buf = sc->sc_conf;
2649 totlen = 1;
2650 }
2651 break;
2652 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2653 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
2654 switch(value >> 8) {
2655 case UDESC_DEVICE:
2656 if ((value & 0xff) != 0) {
2657 err = USBD_IOERROR;
2658 goto ret;
2659 }
2660 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2661 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
2662 memcpy(buf, &uhci_devd, l);
2663 break;
2664 case UDESC_CONFIG:
2665 if ((value & 0xff) != 0) {
2666 err = USBD_IOERROR;
2667 goto ret;
2668 }
2669 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2670 memcpy(buf, &uhci_confd, l);
2671 buf = (char *)buf + l;
2672 len -= l;
2673 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2674 totlen += l;
2675 memcpy(buf, &uhci_ifcd, l);
2676 buf = (char *)buf + l;
2677 len -= l;
2678 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2679 totlen += l;
2680 memcpy(buf, &uhci_endpd, l);
2681 break;
2682 case UDESC_STRING:
2683 if (len == 0)
2684 break;
2685 *(u_int8_t *)buf = 0;
2686 totlen = 1;
2687 switch (value & 0xff) {
2688 case 1: /* Vendor */
2689 totlen = uhci_str(buf, len, sc->sc_vendor);
2690 break;
2691 case 2: /* Product */
2692 totlen = uhci_str(buf, len, "UHCI root hub");
2693 break;
2694 }
2695 break;
2696 default:
2697 err = USBD_IOERROR;
2698 goto ret;
2699 }
2700 break;
2701 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2702 if (len > 0) {
2703 *(u_int8_t *)buf = 0;
2704 totlen = 1;
2705 }
2706 break;
2707 case C(UR_GET_STATUS, UT_READ_DEVICE):
2708 if (len > 1) {
2709 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2710 totlen = 2;
2711 }
2712 break;
2713 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2714 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2715 if (len > 1) {
2716 USETW(((usb_status_t *)buf)->wStatus, 0);
2717 totlen = 2;
2718 }
2719 break;
2720 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2721 if (value >= USB_MAX_DEVICES) {
2722 err = USBD_IOERROR;
2723 goto ret;
2724 }
2725 sc->sc_addr = value;
2726 break;
2727 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2728 if (value != 0 && value != 1) {
2729 err = USBD_IOERROR;
2730 goto ret;
2731 }
2732 sc->sc_conf = value;
2733 break;
2734 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2735 break;
2736 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2737 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2738 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2739 err = USBD_IOERROR;
2740 goto ret;
2741 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2742 break;
2743 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2744 break;
2745 /* Hub requests */
2746 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2747 break;
2748 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2749 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2750 "port=%d feature=%d\n",
2751 index, value));
2752 if (index == 1)
2753 port = UHCI_PORTSC1;
2754 else if (index == 2)
2755 port = UHCI_PORTSC2;
2756 else {
2757 err = USBD_IOERROR;
2758 goto ret;
2759 }
2760 switch(value) {
2761 case UHF_PORT_ENABLE:
2762 x = UREAD2(sc, port);
2763 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
2764 break;
2765 case UHF_PORT_SUSPEND:
2766 x = UREAD2(sc, port);
2767 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
2768 break;
2769 case UHF_PORT_RESET:
2770 x = UREAD2(sc, port);
2771 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2772 break;
2773 case UHF_C_PORT_CONNECTION:
2774 x = UREAD2(sc, port);
2775 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
2776 break;
2777 case UHF_C_PORT_ENABLE:
2778 x = UREAD2(sc, port);
2779 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
2780 break;
2781 case UHF_C_PORT_OVER_CURRENT:
2782 x = UREAD2(sc, port);
2783 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
2784 break;
2785 case UHF_C_PORT_RESET:
2786 sc->sc_isreset = 0;
2787 err = USBD_NORMAL_COMPLETION;
2788 goto ret;
2789 case UHF_PORT_CONNECTION:
2790 case UHF_PORT_OVER_CURRENT:
2791 case UHF_PORT_POWER:
2792 case UHF_PORT_LOW_SPEED:
2793 case UHF_C_PORT_SUSPEND:
2794 default:
2795 err = USBD_IOERROR;
2796 goto ret;
2797 }
2798 break;
2799 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
2800 if (index == 1)
2801 port = UHCI_PORTSC1;
2802 else if (index == 2)
2803 port = UHCI_PORTSC2;
2804 else {
2805 err = USBD_IOERROR;
2806 goto ret;
2807 }
2808 if (len > 0) {
2809 *(u_int8_t *)buf =
2810 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
2811 UHCI_PORTSC_LS_SHIFT;
2812 totlen = 1;
2813 }
2814 break;
2815 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2816 if (value != 0) {
2817 err = USBD_IOERROR;
2818 goto ret;
2819 }
2820 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
2821 totlen = l;
2822 memcpy(buf, &uhci_hubd_piix, l);
2823 break;
2824 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2825 if (len != 4) {
2826 err = USBD_IOERROR;
2827 goto ret;
2828 }
2829 memset(buf, 0, len);
2830 totlen = len;
2831 break;
2832 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2833 if (index == 1)
2834 port = UHCI_PORTSC1;
2835 else if (index == 2)
2836 port = UHCI_PORTSC2;
2837 else {
2838 err = USBD_IOERROR;
2839 goto ret;
2840 }
2841 if (len != 4) {
2842 err = USBD_IOERROR;
2843 goto ret;
2844 }
2845 x = UREAD2(sc, port);
2846 status = change = 0;
2847 if (x & UHCI_PORTSC_CCS )
2848 status |= UPS_CURRENT_CONNECT_STATUS;
2849 if (x & UHCI_PORTSC_CSC )
2850 change |= UPS_C_CONNECT_STATUS;
2851 if (x & UHCI_PORTSC_PE )
2852 status |= UPS_PORT_ENABLED;
2853 if (x & UHCI_PORTSC_POEDC)
2854 change |= UPS_C_PORT_ENABLED;
2855 if (x & UHCI_PORTSC_OCI )
2856 status |= UPS_OVERCURRENT_INDICATOR;
2857 if (x & UHCI_PORTSC_OCIC )
2858 change |= UPS_C_OVERCURRENT_INDICATOR;
2859 if (x & UHCI_PORTSC_SUSP )
2860 status |= UPS_SUSPEND;
2861 if (x & UHCI_PORTSC_LSDA )
2862 status |= UPS_LOW_SPEED;
2863 status |= UPS_PORT_POWER;
2864 if (sc->sc_isreset)
2865 change |= UPS_C_PORT_RESET;
2866 USETW(ps.wPortStatus, status);
2867 USETW(ps.wPortChange, change);
2868 l = min(len, sizeof ps);
2869 memcpy(buf, &ps, l);
2870 totlen = l;
2871 break;
2872 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2873 err = USBD_IOERROR;
2874 goto ret;
2875 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2876 break;
2877 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2878 if (index == 1)
2879 port = UHCI_PORTSC1;
2880 else if (index == 2)
2881 port = UHCI_PORTSC2;
2882 else {
2883 err = USBD_IOERROR;
2884 goto ret;
2885 }
2886 switch(value) {
2887 case UHF_PORT_ENABLE:
2888 x = UREAD2(sc, port);
2889 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2890 break;
2891 case UHF_PORT_SUSPEND:
2892 x = UREAD2(sc, port);
2893 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
2894 break;
2895 case UHF_PORT_RESET:
2896 x = UREAD2(sc, port);
2897 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2898 usb_delay_ms(&sc->sc_bus, 10);
2899 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2900 delay(100);
2901 x = UREAD2(sc, port);
2902 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2903 delay(100);
2904 DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
2905 index, UREAD2(sc, port)));
2906 sc->sc_isreset = 1;
2907 break;
2908 case UHF_C_PORT_CONNECTION:
2909 case UHF_C_PORT_ENABLE:
2910 case UHF_C_PORT_OVER_CURRENT:
2911 case UHF_PORT_CONNECTION:
2912 case UHF_PORT_OVER_CURRENT:
2913 case UHF_PORT_POWER:
2914 case UHF_PORT_LOW_SPEED:
2915 case UHF_C_PORT_SUSPEND:
2916 case UHF_C_PORT_RESET:
2917 default:
2918 err = USBD_IOERROR;
2919 goto ret;
2920 }
2921 break;
2922 default:
2923 err = USBD_IOERROR;
2924 goto ret;
2925 }
2926 xfer->actlen = totlen;
2927 err = USBD_NORMAL_COMPLETION;
2928 ret:
2929 xfer->status = err;
2930 xfer->hcpriv = 0;
2931 s = splusb();
2932 usb_transfer_complete(xfer);
2933 splx(s);
2934 return (USBD_IN_PROGRESS);
2935 }
2936
2937 /* Abort a root control request. */
2938 void
2939 uhci_root_ctrl_abort(xfer)
2940 usbd_xfer_handle xfer;
2941 {
2942 /* Nothing to do, all transfers are syncronous. */
2943 }
2944
2945 /* Close the root pipe. */
2946 void
2947 uhci_root_ctrl_close(pipe)
2948 usbd_pipe_handle pipe;
2949 {
2950 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2951
2952 sc->sc_has_timo = 0;
2953 DPRINTF(("uhci_root_ctrl_close\n"));
2954 }
2955
2956 /* Abort a root interrupt request. */
2957 void
2958 uhci_root_intr_abort(xfer)
2959 usbd_xfer_handle xfer;
2960 {
2961 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2962
2963 usb_untimeout(uhci_timo, xfer, xfer->timo_handle);
2964 sc->sc_has_timo = 0;
2965
2966 if (xfer->pipe->intrxfer == xfer) {
2967 DPRINTF(("uhci_root_intr_abort: remove\n"));
2968 xfer->pipe->intrxfer = 0;
2969 }
2970 xfer->status = USBD_CANCELLED;
2971 usb_transfer_complete(xfer);
2972 }
2973
2974 usbd_status
2975 uhci_root_intr_transfer(xfer)
2976 usbd_xfer_handle xfer;
2977 {
2978 usbd_status err;
2979
2980 /* Insert last in queue. */
2981 err = usb_insert_transfer(xfer);
2982 if (err)
2983 return (err);
2984
2985 /* Pipe isn't running (otherwise err would be USBD_INPROG),
2986 * start first
2987 */
2988 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2989 }
2990
2991 /* Start a transfer on the root interrupt pipe */
2992 usbd_status
2993 uhci_root_intr_start(xfer)
2994 usbd_xfer_handle xfer;
2995 {
2996 usbd_pipe_handle pipe = xfer->pipe;
2997 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2998
2999 DPRINTFN(3, ("uhci_root_intr_transfer: xfer=%p len=%d flags=%d\n",
3000 xfer, xfer->length, xfer->flags));
3001
3002 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3003 usb_timeout(uhci_timo, xfer, sc->sc_ival, xfer->timo_handle);
3004 sc->sc_has_timo = xfer;
3005 return (USBD_IN_PROGRESS);
3006 }
3007
3008 /* Close the root interrupt pipe. */
3009 void
3010 uhci_root_intr_close(pipe)
3011 usbd_pipe_handle pipe;
3012 {
3013 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3014
3015 usb_untimeout(uhci_timo, pipe->intrxfer, pipe->intrxfer->timo_handle);
3016 sc->sc_has_timo = 0;
3017 DPRINTF(("uhci_root_intr_close\n"));
3018 }
3019