uhci.c revision 1.70 1 /* $NetBSD: uhci.c,v 1.70 1999/12/06 21:07:00 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 != NULL)
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 != NULL)
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 if (!ii->isdone) {
1514 printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
1515 }
1516 ii->isdone = 0;
1517 #endif
1518
1519 sqh->elink = data;
1520 sqh->qh.qh_elink = LE(data->physaddr);
1521 sqh->intr_info = ii;
1522
1523 s = splusb();
1524 uhci_add_bulk(sc, sqh);
1525 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
1526
1527 if (xfer->timeout && !sc->sc_bus.use_polling) {
1528 usb_timeout(uhci_timeout, ii, MS_TO_TICKS(xfer->timeout),
1529 ii->timeout_handle);
1530 }
1531 splx(s);
1532
1533 #ifdef UHCI_DEBUG
1534 if (uhcidebug > 10) {
1535 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1536 uhci_dump_tds(data);
1537 }
1538 #endif
1539
1540 if (sc->sc_bus.use_polling)
1541 uhci_waitintr(sc, xfer);
1542
1543 return (USBD_IN_PROGRESS);
1544 }
1545
1546 /* Abort a device bulk request. */
1547 void
1548 uhci_device_bulk_abort(xfer)
1549 usbd_xfer_handle xfer;
1550 {
1551 DPRINTF(("uhci_device_bulk_abort:\n"));
1552 uhci_abort_xfer(xfer, USBD_CANCELLED);
1553 }
1554
1555 void
1556 uhci_abort_xfer(xfer, status)
1557 usbd_xfer_handle xfer;
1558 usbd_status status;
1559 {
1560 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1561 uhci_intr_info_t *ii = upipe->iinfo;
1562 uhci_soft_td_t *std;
1563
1564 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1565
1566 /* Make interrupt routine ignore it, */
1567 xfer->status = status;
1568
1569 /* don't timeout, */
1570 usb_untimeout(uhci_timeout, ii, ii->timeout_handle);
1571
1572 /* make hardware ignore it, */
1573 for (std = ii->stdstart; std != 0; std = std->link.std)
1574 std->td.td_status &= LE(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1575
1576 xfer->hcpriv = ii;
1577
1578 /* make sure hardware has completed, */
1579 if (xfer->device->bus->intr_context) {
1580 /* We have no process context, so we can't use tsleep(). */
1581 timeout(uhci_abort_xfer_end, xfer, hz / USB_FRAMES_PER_SECOND);
1582 } else {
1583 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
1584 KASSERT(intr_nesting_level == 0,
1585 ("ohci_abort_req in interrupt context"));
1586 #endif
1587 usb_delay_ms(xfer->pipe->device->bus, 1);
1588 /* and call final part of interrupt handler. */
1589 uhci_abort_xfer_end(xfer);
1590 }
1591 }
1592
1593 void
1594 uhci_abort_xfer_end(v)
1595 void *v;
1596 {
1597 usbd_xfer_handle xfer = v;
1598 int s;
1599
1600 s = splusb();
1601 usb_transfer_complete(xfer);
1602 splx(s);
1603 }
1604
1605 /* Close a device bulk pipe. */
1606 void
1607 uhci_device_bulk_close(pipe)
1608 usbd_pipe_handle pipe;
1609 {
1610 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1611 usbd_device_handle dev = upipe->pipe.device;
1612 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1613
1614 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1615 uhci_free_intr_info(upipe->iinfo);
1616 /* XXX free other resources */
1617 }
1618
1619 usbd_status
1620 uhci_device_ctrl_transfer(xfer)
1621 usbd_xfer_handle xfer;
1622 {
1623 usbd_status err;
1624
1625 /* Insert last in queue. */
1626 err = usb_insert_transfer(xfer);
1627 if (err)
1628 return (err);
1629
1630 /* Pipe isn't running (otherwise err would be USBD_INPROG),
1631 * start first
1632 */
1633 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1634 }
1635
1636 usbd_status
1637 uhci_device_ctrl_start(xfer)
1638 usbd_xfer_handle xfer;
1639 {
1640 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
1641 usbd_status err;
1642
1643 #ifdef DIAGNOSTIC
1644 if (!(xfer->rqflags & URQ_REQUEST))
1645 panic("uhci_device_ctrl_transfer: not a request\n");
1646 #endif
1647
1648 err = uhci_device_request(xfer);
1649 if (err)
1650 return (err);
1651
1652 if (sc->sc_bus.use_polling)
1653 uhci_waitintr(sc, xfer);
1654 return (USBD_IN_PROGRESS);
1655 }
1656
1657 usbd_status
1658 uhci_device_intr_transfer(xfer)
1659 usbd_xfer_handle xfer;
1660 {
1661 usbd_status err;
1662
1663 /* Insert last in queue. */
1664 err = usb_insert_transfer(xfer);
1665 if (err)
1666 return (err);
1667
1668 /* Pipe isn't running (otherwise err would be USBD_INPROG),
1669 * start first
1670 */
1671 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1672 }
1673
1674 usbd_status
1675 uhci_device_intr_start(xfer)
1676 usbd_xfer_handle xfer;
1677 {
1678 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1679 usbd_device_handle dev = upipe->pipe.device;
1680 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1681 uhci_intr_info_t *ii = upipe->iinfo;
1682 uhci_soft_td_t *data, *dataend;
1683 uhci_soft_qh_t *sqh;
1684 usbd_status err;
1685 int i, s;
1686
1687 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
1688 xfer, xfer->length, xfer->flags));
1689
1690 #ifdef DIAGNOSTIC
1691 if (xfer->rqflags & URQ_REQUEST)
1692 panic("uhci_device_intr_transfer: a request\n");
1693 #endif
1694
1695 err = uhci_alloc_std_chain(upipe, sc, xfer->length, 1,
1696 xfer->flags & USBD_SHORT_XFER_OK,
1697 &xfer->dmabuf, &data, &dataend);
1698 if (err)
1699 return (err);
1700 dataend->td.td_status |= LE(UHCI_TD_IOC);
1701
1702 #ifdef UHCI_DEBUG
1703 if (uhcidebug > 10) {
1704 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
1705 uhci_dump_tds(data);
1706 uhci_dump_qh(upipe->u.intr.qhs[0]);
1707 }
1708 #endif
1709
1710 s = splusb();
1711 /* Set up interrupt info. */
1712 ii->xfer = xfer;
1713 ii->stdstart = data;
1714 ii->stdend = dataend;
1715 #if defined(__FreeBSD__)
1716 callout_handle_init(&ii->timeout_handle);
1717 #endif
1718 #ifdef DIAGNOSTIC
1719 if (!ii->isdone) {
1720 printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
1721 }
1722 ii->isdone = 0;
1723 #endif
1724
1725 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
1726 upipe->u.intr.qhs[0]));
1727 for (i = 0; i < upipe->u.intr.npoll; i++) {
1728 sqh = upipe->u.intr.qhs[i];
1729 sqh->elink = data;
1730 sqh->qh.qh_elink = LE(data->physaddr);
1731 }
1732 splx(s);
1733
1734 #ifdef UHCI_DEBUG
1735 if (uhcidebug > 10) {
1736 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
1737 uhci_dump_tds(data);
1738 uhci_dump_qh(upipe->u.intr.qhs[0]);
1739 }
1740 #endif
1741
1742 return (USBD_IN_PROGRESS);
1743 }
1744
1745 /* Abort a device control request. */
1746 void
1747 uhci_device_ctrl_abort(xfer)
1748 usbd_xfer_handle xfer;
1749 {
1750 DPRINTF(("uhci_device_ctrl_abort:\n"));
1751 uhci_abort_xfer(xfer, USBD_CANCELLED);
1752 }
1753
1754 /* Close a device control pipe. */
1755 void
1756 uhci_device_ctrl_close(pipe)
1757 usbd_pipe_handle pipe;
1758 {
1759 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1760
1761 uhci_free_intr_info(upipe->iinfo);
1762 /* XXX free other resources? */
1763 }
1764
1765 /* Abort a device interrupt request. */
1766 void
1767 uhci_device_intr_abort(xfer)
1768 usbd_xfer_handle xfer;
1769 {
1770 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
1771 if (xfer->pipe->intrxfer == xfer) {
1772 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
1773 xfer->pipe->intrxfer = 0;
1774 }
1775 uhci_abort_xfer(xfer, USBD_CANCELLED);
1776 }
1777
1778 /* Close a device interrupt pipe. */
1779 void
1780 uhci_device_intr_close(pipe)
1781 usbd_pipe_handle pipe;
1782 {
1783 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1784 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
1785 int i, s, npoll;
1786
1787 upipe->iinfo->stdstart = 0; /* inactive */
1788
1789 /* Unlink descriptors from controller data structures. */
1790 npoll = upipe->u.intr.npoll;
1791 uhci_lock_frames(sc);
1792 for (i = 0; i < npoll; i++)
1793 uhci_remove_intr(sc, upipe->u.intr.qhs[i]->pos,
1794 upipe->u.intr.qhs[i]);
1795 uhci_unlock_frames(sc);
1796
1797 /*
1798 * We now have to wait for any activity on the physical
1799 * descriptors to stop.
1800 */
1801 usb_delay_ms(&sc->sc_bus, 2);
1802
1803 for(i = 0; i < npoll; i++)
1804 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
1805 free(upipe->u.intr.qhs, M_USBHC);
1806
1807 s = splusb();
1808 LIST_REMOVE(upipe->iinfo, list); /* remove from active list */
1809 splx(s);
1810 uhci_free_intr_info(upipe->iinfo);
1811
1812 /* XXX free other resources */
1813 }
1814
1815 usbd_status
1816 uhci_device_request(xfer)
1817 usbd_xfer_handle xfer;
1818 {
1819 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1820 usb_device_request_t *req = &xfer->request;
1821 usbd_device_handle dev = upipe->pipe.device;
1822 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1823 int addr = dev->address;
1824 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1825 uhci_intr_info_t *ii = upipe->iinfo;
1826 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
1827 uhci_soft_qh_t *sqh;
1828 int len;
1829 u_int32_t ls;
1830 usbd_status err;
1831 int isread;
1832 int s;
1833
1834 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
1835 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1836 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1837 UGETW(req->wIndex), UGETW(req->wLength),
1838 addr, endpt));
1839
1840 ls = dev->lowspeed ? UHCI_TD_LS : 0;
1841 isread = req->bmRequestType & UT_READ;
1842 len = UGETW(req->wLength);
1843
1844 setup = upipe->u.ctl.setup;
1845 stat = upipe->u.ctl.stat;
1846 sqh = upipe->u.ctl.sqh;
1847
1848 /* Set up data transaction */
1849 if (len != 0) {
1850 upipe->nexttoggle = 1;
1851 err = uhci_alloc_std_chain(upipe, sc, len, isread,
1852 xfer->flags & USBD_SHORT_XFER_OK,
1853 &xfer->dmabuf, &data, &dataend);
1854 if (err)
1855 return (err);
1856 next = data;
1857 dataend->link.std = stat;
1858 dataend->td.td_link = LE(stat->physaddr | UHCI_PTR_VF);
1859 } else {
1860 next = stat;
1861 }
1862 upipe->u.ctl.length = len;
1863
1864 memcpy(KERNADDR(&upipe->u.ctl.reqdma), req, sizeof *req);
1865
1866 setup->link.std = next;
1867 setup->td.td_link = LE(next->physaddr | UHCI_PTR_VF);
1868 setup->td.td_status = LE(UHCI_TD_SET_ERRCNT(3) | ls | UHCI_TD_ACTIVE);
1869 setup->td.td_token = LE(UHCI_TD_SETUP(sizeof *req, endpt, addr));
1870 setup->td.td_buffer = LE(DMAADDR(&upipe->u.ctl.reqdma));
1871
1872 stat->link.std = 0;
1873 stat->td.td_link = LE(UHCI_PTR_T);
1874 stat->td.td_status = LE(UHCI_TD_SET_ERRCNT(3) | ls |
1875 UHCI_TD_ACTIVE | UHCI_TD_IOC);
1876 stat->td.td_token =
1877 LE(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
1878 UHCI_TD_IN (0, endpt, addr, 1));
1879 stat->td.td_buffer = LE(0);
1880
1881 #ifdef UHCI_DEBUG
1882 if (uhcidebug > 10) {
1883 DPRINTF(("uhci_device_request: before transfer\n"));
1884 uhci_dump_tds(setup);
1885 }
1886 #endif
1887
1888 /* Set up interrupt info. */
1889 ii->xfer = xfer;
1890 ii->stdstart = setup;
1891 ii->stdend = stat;
1892 #if defined(__FreeBSD__)
1893 callout_handle_init(&ii->timeout_handle);
1894 #endif
1895 #ifdef DIAGNOSTIC
1896 if (!ii->isdone) {
1897 printf("uhci_device_request: not done, ii=%p\n", ii);
1898 }
1899 ii->isdone = 0;
1900 #endif
1901
1902 sqh->elink = setup;
1903 sqh->qh.qh_elink = LE(setup->physaddr);
1904 sqh->intr_info = ii;
1905
1906 s = splusb();
1907 uhci_add_ctrl(sc, sqh);
1908 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
1909 #ifdef UHCI_DEBUG
1910 if (uhcidebug > 12) {
1911 uhci_soft_td_t *std;
1912 uhci_soft_qh_t *xqh;
1913 uhci_soft_qh_t *sxqh;
1914 int maxqh = 0;
1915 uhci_physaddr_t link;
1916 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
1917 for (std = sc->sc_vframes[0].htd, link = 0;
1918 (link & UHCI_PTR_Q) == 0;
1919 std = std->link.std) {
1920 link = LE(std->td.td_link);
1921 uhci_dump_td(std);
1922 }
1923 sxqh = (uhci_soft_qh_t *)std;
1924 uhci_dump_qh(sxqh);
1925 for (xqh = sxqh;
1926 xqh != NULL;
1927 xqh = (maxqh++ == 5 || xqh->hlink==sxqh ||
1928 xqh->hlink==xqh ? NULL : xqh->hlink)) {
1929 uhci_dump_qh(xqh);
1930 }
1931 DPRINTF(("Enqueued QH:\n"));
1932 uhci_dump_qh(sqh);
1933 uhci_dump_tds(sqh->elink);
1934 }
1935 #endif
1936 if (xfer->timeout && !sc->sc_bus.use_polling) {
1937 usb_timeout(uhci_timeout, ii,
1938 MS_TO_TICKS(xfer->timeout), ii->timeout_handle);
1939 }
1940 splx(s);
1941
1942 return (USBD_NORMAL_COMPLETION);
1943 }
1944
1945 usbd_status
1946 uhci_device_isoc_transfer(xfer)
1947 usbd_xfer_handle xfer;
1948 {
1949 usbd_status err;
1950
1951 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
1952
1953 /* Put it on our queue, */
1954 err = usb_insert_transfer(xfer);
1955
1956 /* bail out on error, */
1957 if (err && err != USBD_IN_PROGRESS)
1958 return (err);
1959
1960 /* XXX should check inuse here */
1961
1962 /* insert into schedule, */
1963 uhci_device_isoc_enter(xfer);
1964
1965 /* and put on interrupt list if the pipe wasn't running */
1966 if (!err)
1967 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
1968
1969 return (err);
1970 }
1971
1972 void
1973 uhci_device_isoc_enter(xfer)
1974 usbd_xfer_handle xfer;
1975 {
1976 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1977 usbd_device_handle dev = upipe->pipe.device;
1978 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1979 struct iso *iso = &upipe->u.iso;
1980 uhci_soft_td_t *std;
1981 u_int32_t buf, len, status;
1982 int s, i, next, nframes;
1983
1984 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
1985 "nframes=%d\n",
1986 iso->inuse, iso->next, xfer, xfer->nframes));
1987
1988 if (xfer->status == USBD_IN_PROGRESS) {
1989 /* This request has already been entered into the frame list */
1990 /* XXX */
1991 }
1992
1993 #ifdef DIAGNOSTIC
1994 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
1995 printf("uhci_device_isoc_enter: overflow!\n");
1996 #endif
1997
1998 next = iso->next;
1999 if (next == -1) {
2000 /* Not in use yet, schedule it a few frames ahead. */
2001 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2002 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2003 }
2004
2005 xfer->status = USBD_IN_PROGRESS;
2006 xfer->hcprivint = next;
2007
2008 buf = DMAADDR(&xfer->dmabuf);
2009 status = LE(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2010 UHCI_TD_ACTIVE |
2011 UHCI_TD_IOS));
2012 nframes = xfer->nframes;
2013 s = splusb();
2014 for (i = 0; i < nframes; i++) {
2015 std = iso->stds[next];
2016 if (++next >= UHCI_VFRAMELIST_COUNT)
2017 next = 0;
2018 len = xfer->frlengths[i];
2019 std->td.td_buffer = LE(buf);
2020 if (i == nframes - 1)
2021 status |= LE(UHCI_TD_IOC);
2022 std->td.td_status = status;
2023 std->td.td_token &= LE(~UHCI_TD_MAXLEN_MASK);
2024 std->td.td_token |= LE(UHCI_TD_SET_MAXLEN(len));
2025 #ifdef UHCI_DEBUG
2026 if (uhcidebug > 5) {
2027 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2028 uhci_dump_td(std);
2029 }
2030 #endif
2031 buf += len;
2032 }
2033 iso->next = next;
2034 iso->inuse += xfer->nframes;
2035
2036 splx(s);
2037 }
2038
2039 usbd_status
2040 uhci_device_isoc_start(xfer)
2041 usbd_xfer_handle xfer;
2042 {
2043 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2044 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2045 uhci_intr_info_t *ii = upipe->iinfo;
2046 uhci_soft_td_t *end;
2047 int s, i;
2048
2049 #ifdef DIAGNOSTIC
2050 if (xfer->status != USBD_IN_PROGRESS)
2051 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2052 #endif
2053
2054 /* Find the last TD */
2055 i = xfer->hcprivint + xfer->nframes;
2056 if (i >= UHCI_VFRAMELIST_COUNT)
2057 i -= UHCI_VFRAMELIST_COUNT;
2058 end = upipe->u.iso.stds[i];
2059
2060 s = splusb();
2061
2062 /* Set up interrupt info. */
2063 ii->xfer = xfer;
2064 ii->stdstart = end;
2065 ii->stdend = end;
2066 #if defined(__FreeBSD__)
2067 callout_handle_init(&ii->timeout_handle);
2068 #endif
2069 #ifdef DIAGNOSTIC
2070 if (!ii->isdone) {
2071 printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2072 }
2073 ii->isdone = 0;
2074 #endif
2075 LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
2076
2077 splx(s);
2078
2079 return (USBD_IN_PROGRESS);
2080 }
2081
2082 void
2083 uhci_device_isoc_abort(xfer)
2084 usbd_xfer_handle xfer;
2085 {
2086 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2087 uhci_intr_info_t *ii = upipe->iinfo;
2088 uhci_soft_td_t **stds = upipe->u.iso.stds;
2089 uhci_soft_td_t *std;
2090 int i, n, nframes;
2091
2092 /* Make interrupt routine ignore it, */
2093 xfer->status = USBD_CANCELLED;
2094
2095 /* make hardware ignore it, */
2096 nframes = xfer->nframes;
2097 n = xfer->hcprivint;
2098 for (i = 0; i < nframes; i++) {
2099 std = stds[n];
2100 std->td.td_status &= LE(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2101 if (++n >= UHCI_VFRAMELIST_COUNT)
2102 n = 0;
2103 }
2104
2105 xfer->hcpriv = ii;
2106
2107 /* make sure hardware has completed, */
2108 if (xfer->device->bus->intr_context) {
2109 /* We have no process context, so we can't use tsleep(). */
2110 timeout(uhci_abort_xfer_end, xfer, hz / USB_FRAMES_PER_SECOND);
2111 } else {
2112 usb_delay_ms(xfer->pipe->device->bus, 1);
2113 /* and call final part of interrupt handler. */
2114 uhci_abort_xfer_end(xfer);
2115 }
2116 }
2117
2118 void
2119 uhci_device_isoc_close(pipe)
2120 usbd_pipe_handle pipe;
2121 {
2122 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2123 usbd_device_handle dev = upipe->pipe.device;
2124 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2125 uhci_soft_td_t *std, *vstd;
2126 struct iso *iso;
2127 int i;
2128
2129 /*
2130 * Make sure all TDs are marked as inactive.
2131 * Wait for completion.
2132 * Unschedule.
2133 * Deallocate.
2134 */
2135 iso = &upipe->u.iso;
2136
2137 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2138 iso->stds[i]->td.td_status &= LE(~UHCI_TD_ACTIVE);
2139 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2140
2141 uhci_lock_frames(sc);
2142 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2143 std = iso->stds[i];
2144 for (vstd = sc->sc_vframes[i].htd;
2145 vstd != NULL && vstd->link.std != std;
2146 vstd = vstd->link.std)
2147 ;
2148 if (vstd == NULL) {
2149 /*panic*/
2150 printf("uhci_device_isoc_close: %p not found\n", std);
2151 uhci_unlock_frames(sc);
2152 return;
2153 }
2154 vstd->link = std->link;
2155 vstd->td.td_link = std->td.td_link;
2156 uhci_free_std(sc, std);
2157 }
2158 uhci_unlock_frames(sc);
2159
2160 free(iso->stds, M_USBHC);
2161 }
2162
2163 usbd_status
2164 uhci_setup_isoc(pipe)
2165 usbd_pipe_handle pipe;
2166 {
2167 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2168 usbd_device_handle dev = upipe->pipe.device;
2169 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2170 int addr = upipe->pipe.device->address;
2171 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2172 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2173 uhci_soft_td_t *std, *vstd;
2174 u_int32_t token;
2175 struct iso *iso;
2176 int i;
2177
2178 iso = &upipe->u.iso;
2179 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2180 M_USBHC, M_WAITOK);
2181
2182 token = LE(rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2183 UHCI_TD_OUT(0, endpt, addr, 0));
2184
2185 /* Allocate the TDs and mark as inactive; */
2186 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2187 std = uhci_alloc_std(sc);
2188 if (std == 0)
2189 goto bad;
2190 std->td.td_status = LE(UHCI_TD_IOS); /* iso, inactive */
2191 std->td.td_token = token;
2192 iso->stds[i] = std;
2193 }
2194
2195 /* Insert TDs into schedule. */
2196 uhci_lock_frames(sc);
2197 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2198 std = iso->stds[i];
2199 vstd = sc->sc_vframes[i].htd;
2200 std->link = vstd->link;
2201 std->td.td_link = vstd->td.td_link;
2202 vstd->link.std = std;
2203 vstd->td.td_link = LE(std->physaddr);
2204 }
2205 uhci_unlock_frames(sc);
2206
2207 iso->next = -1;
2208 iso->inuse = 0;
2209
2210 return (USBD_NORMAL_COMPLETION);
2211
2212 bad:
2213 while (--i >= 0)
2214 uhci_free_std(sc, iso->stds[i]);
2215 free(iso->stds, M_USBHC);
2216 return (USBD_NOMEM);
2217 }
2218
2219 void
2220 uhci_device_isoc_done(xfer)
2221 usbd_xfer_handle xfer;
2222 {
2223 uhci_intr_info_t *ii = xfer->hcpriv;
2224
2225 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2226
2227 /* Turn off the interrupt since it is active even if the TD is not. */
2228 ii->stdend->td.td_status &= LE(~UHCI_TD_IOC);
2229
2230 LIST_REMOVE(ii, list); /* remove from active list */
2231 }
2232
2233 void
2234 uhci_device_intr_done(xfer)
2235 usbd_xfer_handle xfer;
2236 {
2237 uhci_intr_info_t *ii = xfer->hcpriv;
2238 uhci_softc_t *sc = ii->sc;
2239 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2240 uhci_soft_qh_t *sqh;
2241 int i, npoll;
2242
2243 DPRINTFN(5, ("uhci_intr_done: length=%d\n", xfer->actlen));
2244
2245 npoll = upipe->u.intr.npoll;
2246 for(i = 0; i < npoll; i++) {
2247 sqh = upipe->u.intr.qhs[i];
2248 sqh->elink = 0;
2249 sqh->qh.qh_elink = LE(UHCI_PTR_T);
2250 }
2251 uhci_free_std_chain(sc, ii->stdstart, 0);
2252
2253 /* XXX Wasteful. */
2254 if (xfer->pipe->repeat) {
2255 uhci_soft_td_t *data, *dataend;
2256
2257 /* This alloc cannot fail since we freed the chain above. */
2258 uhci_alloc_std_chain(upipe, sc, xfer->length, 1,
2259 xfer->flags & USBD_SHORT_XFER_OK,
2260 &xfer->dmabuf, &data, &dataend);
2261 dataend->td.td_status |= LE(UHCI_TD_IOC);
2262
2263 #ifdef UHCI_DEBUG
2264 if (uhcidebug > 10) {
2265 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2266 uhci_dump_tds(data);
2267 uhci_dump_qh(upipe->u.intr.qhs[0]);
2268 }
2269 #endif
2270
2271 ii->stdstart = data;
2272 ii->stdend = dataend;
2273 #if defined(__FreeBSD__)
2274 callout_handle_init(&ii->timeout_handle);
2275 #endif
2276 #ifdef DIAGNOSTIC
2277 if (!ii->isdone) {
2278 printf("uhci_device_intr_done: not done, ii=%p\n", ii);
2279 }
2280 ii->isdone = 0;
2281 #endif
2282 for (i = 0; i < npoll; i++) {
2283 sqh = upipe->u.intr.qhs[i];
2284 sqh->elink = data;
2285 sqh->qh.qh_elink = LE(data->physaddr);
2286 }
2287 } else {
2288 ii->stdstart = 0; /* mark as inactive */
2289 }
2290 }
2291
2292 /* Deallocate request data structures */
2293 void
2294 uhci_device_ctrl_done(xfer)
2295 usbd_xfer_handle xfer;
2296 {
2297 uhci_intr_info_t *ii = xfer->hcpriv;
2298 uhci_softc_t *sc = ii->sc;
2299 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2300
2301 #ifdef DIAGNOSTIC
2302 if (!(xfer->rqflags & URQ_REQUEST))
2303 panic("uhci_ctrl_done: not a request\n");
2304 #endif
2305
2306 LIST_REMOVE(ii, list); /* remove from active list */
2307
2308 uhci_remove_ctrl(sc, upipe->u.ctl.sqh);
2309
2310 if (upipe->u.ctl.length != 0)
2311 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2312
2313 DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", xfer->actlen));
2314 }
2315
2316 /* Deallocate request data structures */
2317 void
2318 uhci_device_bulk_done(xfer)
2319 usbd_xfer_handle xfer;
2320 {
2321 uhci_intr_info_t *ii = xfer->hcpriv;
2322 uhci_softc_t *sc = ii->sc;
2323 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2324
2325 LIST_REMOVE(ii, list); /* remove from active list */
2326
2327 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2328
2329 uhci_free_std_chain(sc, ii->stdstart, 0);
2330
2331 DPRINTFN(5, ("uhci_bulk_done: length=%d\n", xfer->actlen));
2332 }
2333
2334 /* Add interrupt QH, called with vflock. */
2335 void
2336 uhci_add_intr(sc, n, sqh)
2337 uhci_softc_t *sc;
2338 int n;
2339 uhci_soft_qh_t *sqh;
2340 {
2341 struct uhci_vframe *vf = &sc->sc_vframes[n];
2342 uhci_soft_qh_t *eqh;
2343
2344 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", n, sqh));
2345 eqh = vf->eqh;
2346 sqh->hlink = eqh->hlink;
2347 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2348 eqh->hlink = sqh;
2349 eqh->qh.qh_hlink = LE(sqh->physaddr | UHCI_PTR_Q);
2350 vf->eqh = sqh;
2351 vf->bandwidth++;
2352 }
2353
2354 /* Remove interrupt QH, called with vflock. */
2355 void
2356 uhci_remove_intr(sc, n, sqh)
2357 uhci_softc_t *sc;
2358 int n;
2359 uhci_soft_qh_t *sqh;
2360 {
2361 struct uhci_vframe *vf = &sc->sc_vframes[n];
2362 uhci_soft_qh_t *pqh;
2363
2364 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", n, sqh));
2365
2366 for (pqh = vf->hqh; pqh->hlink != sqh; pqh = pqh->hlink)
2367 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG)
2368 if (LE(pqh->qh.qh_hlink) & UHCI_PTR_T) {
2369 DPRINTF(("uhci_remove_intr: QH not found\n"));
2370 return;
2371 }
2372 #else
2373 ;
2374 #endif
2375 pqh->hlink = sqh->hlink;
2376 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2377 if (vf->eqh == sqh)
2378 vf->eqh = pqh;
2379 vf->bandwidth--;
2380 }
2381
2382 usbd_status
2383 uhci_device_setintr(sc, upipe, ival)
2384 uhci_softc_t *sc;
2385 struct uhci_pipe *upipe;
2386 int ival;
2387 {
2388 uhci_soft_qh_t *sqh;
2389 int i, npoll, s;
2390 u_int bestbw, bw, bestoffs, offs;
2391
2392 DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
2393 if (ival == 0) {
2394 printf("uhci_setintr: 0 interval\n");
2395 return (USBD_INVAL);
2396 }
2397
2398 if (ival > UHCI_VFRAMELIST_COUNT)
2399 ival = UHCI_VFRAMELIST_COUNT;
2400 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2401 DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
2402
2403 upipe->u.intr.npoll = npoll;
2404 upipe->u.intr.qhs =
2405 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
2406
2407 /*
2408 * Figure out which offset in the schedule that has most
2409 * bandwidth left over.
2410 */
2411 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2412 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2413 for (bw = i = 0; i < npoll; i++)
2414 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2415 if (bw < bestbw) {
2416 bestbw = bw;
2417 bestoffs = offs;
2418 }
2419 }
2420 DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2421
2422 upipe->iinfo->stdstart = 0;
2423 for(i = 0; i < npoll; i++) {
2424 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2425 sqh->elink = 0;
2426 sqh->qh.qh_elink = LE(UHCI_PTR_T);
2427 sqh->pos = MOD(i * ival + bestoffs);
2428 sqh->intr_info = upipe->iinfo;
2429 }
2430 #undef MOD
2431
2432 s = splusb();
2433 LIST_INSERT_HEAD(&sc->sc_intrhead, upipe->iinfo, list);
2434 splx(s);
2435
2436 uhci_lock_frames(sc);
2437 /* Enter QHs into the controller data structures. */
2438 for(i = 0; i < npoll; i++)
2439 uhci_add_intr(sc, upipe->u.intr.qhs[i]->pos,
2440 upipe->u.intr.qhs[i]);
2441 uhci_unlock_frames(sc);
2442
2443 DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
2444 return (USBD_NORMAL_COMPLETION);
2445 }
2446
2447 /* Open a new pipe. */
2448 usbd_status
2449 uhci_open(pipe)
2450 usbd_pipe_handle pipe;
2451 {
2452 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2453 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2454 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2455 usbd_status err;
2456
2457 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2458 pipe, pipe->device->address,
2459 ed->bEndpointAddress, sc->sc_addr));
2460 if (pipe->device->address == sc->sc_addr) {
2461 switch (ed->bEndpointAddress) {
2462 case USB_CONTROL_ENDPOINT:
2463 pipe->methods = &uhci_root_ctrl_methods;
2464 break;
2465 case UE_DIR_IN | UHCI_INTR_ENDPT:
2466 pipe->methods = &uhci_root_intr_methods;
2467 break;
2468 default:
2469 return (USBD_INVAL);
2470 }
2471 } else {
2472 upipe->iinfo = uhci_alloc_intr_info(sc);
2473 if (upipe->iinfo == 0)
2474 return (USBD_NOMEM);
2475 switch (ed->bmAttributes & UE_XFERTYPE) {
2476 case UE_CONTROL:
2477 pipe->methods = &uhci_device_ctrl_methods;
2478 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2479 if (upipe->u.ctl.sqh == NULL)
2480 goto bad;
2481 upipe->u.ctl.setup = uhci_alloc_std(sc);
2482 if (upipe->u.ctl.setup == NULL) {
2483 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2484 goto bad;
2485 }
2486 upipe->u.ctl.stat = uhci_alloc_std(sc);
2487 if (upipe->u.ctl.stat == NULL) {
2488 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2489 uhci_free_std(sc, upipe->u.ctl.setup);
2490 goto bad;
2491 }
2492 err = usb_allocmem(&sc->sc_bus,
2493 sizeof(usb_device_request_t),
2494 0, &upipe->u.ctl.reqdma);
2495 if (err) {
2496 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2497 uhci_free_std(sc, upipe->u.ctl.setup);
2498 uhci_free_std(sc, upipe->u.ctl.stat);
2499 goto bad;
2500 }
2501 break;
2502 case UE_INTERRUPT:
2503 pipe->methods = &uhci_device_intr_methods;
2504 return (uhci_device_setintr(sc, upipe, ed->bInterval));
2505 case UE_ISOCHRONOUS:
2506 pipe->methods = &uhci_device_isoc_methods;
2507 return (uhci_setup_isoc(pipe));
2508 case UE_BULK:
2509 pipe->methods = &uhci_device_bulk_methods;
2510 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2511 if (upipe->u.bulk.sqh == NULL)
2512 goto bad;
2513 break;
2514 }
2515 }
2516 return (USBD_NORMAL_COMPLETION);
2517
2518 bad:
2519 uhci_free_intr_info(upipe->iinfo);
2520 return (USBD_NOMEM);
2521 }
2522
2523 /*
2524 * Data structures and routines to emulate the root hub.
2525 */
2526 usb_device_descriptor_t uhci_devd = {
2527 USB_DEVICE_DESCRIPTOR_SIZE,
2528 UDESC_DEVICE, /* type */
2529 {0x00, 0x01}, /* USB version */
2530 UCLASS_HUB, /* class */
2531 USUBCLASS_HUB, /* subclass */
2532 0, /* protocol */
2533 64, /* max packet */
2534 {0},{0},{0x00,0x01}, /* device id */
2535 1,2,0, /* string indicies */
2536 1 /* # of configurations */
2537 };
2538
2539 usb_config_descriptor_t uhci_confd = {
2540 USB_CONFIG_DESCRIPTOR_SIZE,
2541 UDESC_CONFIG,
2542 {USB_CONFIG_DESCRIPTOR_SIZE +
2543 USB_INTERFACE_DESCRIPTOR_SIZE +
2544 USB_ENDPOINT_DESCRIPTOR_SIZE},
2545 1,
2546 1,
2547 0,
2548 UC_SELF_POWERED,
2549 0 /* max power */
2550 };
2551
2552 usb_interface_descriptor_t uhci_ifcd = {
2553 USB_INTERFACE_DESCRIPTOR_SIZE,
2554 UDESC_INTERFACE,
2555 0,
2556 0,
2557 1,
2558 UCLASS_HUB,
2559 USUBCLASS_HUB,
2560 0,
2561 0
2562 };
2563
2564 usb_endpoint_descriptor_t uhci_endpd = {
2565 USB_ENDPOINT_DESCRIPTOR_SIZE,
2566 UDESC_ENDPOINT,
2567 UE_DIR_IN | UHCI_INTR_ENDPT,
2568 UE_INTERRUPT,
2569 {8},
2570 255
2571 };
2572
2573 usb_hub_descriptor_t uhci_hubd_piix = {
2574 USB_HUB_DESCRIPTOR_SIZE,
2575 UDESC_HUB,
2576 2,
2577 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2578 50, /* power on to power good */
2579 0,
2580 { 0x00 }, /* both ports are removable */
2581 };
2582
2583 int
2584 uhci_str(p, l, s)
2585 usb_string_descriptor_t *p;
2586 int l;
2587 char *s;
2588 {
2589 int i;
2590
2591 if (l == 0)
2592 return (0);
2593 p->bLength = 2 * strlen(s) + 2;
2594 if (l == 1)
2595 return (1);
2596 p->bDescriptorType = UDESC_STRING;
2597 l -= 2;
2598 for (i = 0; s[i] && l > 1; i++, l -= 2)
2599 USETW2(p->bString[i], 0, s[i]);
2600 return (2*i+2);
2601 }
2602
2603 /*
2604 * Simulate a hardware hub by handling all the necessary requests.
2605 */
2606 usbd_status
2607 uhci_root_ctrl_transfer(xfer)
2608 usbd_xfer_handle xfer;
2609 {
2610 usbd_status err;
2611
2612 /* Insert last in queue. */
2613 err = usb_insert_transfer(xfer);
2614 if (err)
2615 return (err);
2616
2617 /* Pipe isn't running (otherwise err would be USBD_INPROG),
2618 * start first
2619 */
2620 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2621 }
2622
2623 usbd_status
2624 uhci_root_ctrl_start(xfer)
2625 usbd_xfer_handle xfer;
2626 {
2627 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2628 usb_device_request_t *req;
2629 void *buf = NULL;
2630 int port, x;
2631 int s, len, value, index, status, change, l, totlen = 0;
2632 usb_port_status_t ps;
2633 usbd_status err;
2634
2635 #ifdef DIAGNOSTIC
2636 if (!(xfer->rqflags & URQ_REQUEST))
2637 panic("uhci_root_ctrl_transfer: not a request\n");
2638 #endif
2639 req = &xfer->request;
2640
2641 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
2642 req->bmRequestType, req->bRequest));
2643
2644 len = UGETW(req->wLength);
2645 value = UGETW(req->wValue);
2646 index = UGETW(req->wIndex);
2647
2648 if (len != 0)
2649 buf = KERNADDR(&xfer->dmabuf);
2650
2651 #define C(x,y) ((x) | ((y) << 8))
2652 switch(C(req->bRequest, req->bmRequestType)) {
2653 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2654 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2655 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2656 /*
2657 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2658 * for the integrated root hub.
2659 */
2660 break;
2661 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2662 if (len > 0) {
2663 *(u_int8_t *)buf = sc->sc_conf;
2664 totlen = 1;
2665 }
2666 break;
2667 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2668 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
2669 switch(value >> 8) {
2670 case UDESC_DEVICE:
2671 if ((value & 0xff) != 0) {
2672 err = USBD_IOERROR;
2673 goto ret;
2674 }
2675 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2676 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
2677 memcpy(buf, &uhci_devd, l);
2678 break;
2679 case UDESC_CONFIG:
2680 if ((value & 0xff) != 0) {
2681 err = USBD_IOERROR;
2682 goto ret;
2683 }
2684 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2685 memcpy(buf, &uhci_confd, l);
2686 buf = (char *)buf + l;
2687 len -= l;
2688 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2689 totlen += l;
2690 memcpy(buf, &uhci_ifcd, l);
2691 buf = (char *)buf + l;
2692 len -= l;
2693 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2694 totlen += l;
2695 memcpy(buf, &uhci_endpd, l);
2696 break;
2697 case UDESC_STRING:
2698 if (len == 0)
2699 break;
2700 *(u_int8_t *)buf = 0;
2701 totlen = 1;
2702 switch (value & 0xff) {
2703 case 1: /* Vendor */
2704 totlen = uhci_str(buf, len, sc->sc_vendor);
2705 break;
2706 case 2: /* Product */
2707 totlen = uhci_str(buf, len, "UHCI root hub");
2708 break;
2709 }
2710 break;
2711 default:
2712 err = USBD_IOERROR;
2713 goto ret;
2714 }
2715 break;
2716 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2717 if (len > 0) {
2718 *(u_int8_t *)buf = 0;
2719 totlen = 1;
2720 }
2721 break;
2722 case C(UR_GET_STATUS, UT_READ_DEVICE):
2723 if (len > 1) {
2724 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2725 totlen = 2;
2726 }
2727 break;
2728 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2729 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2730 if (len > 1) {
2731 USETW(((usb_status_t *)buf)->wStatus, 0);
2732 totlen = 2;
2733 }
2734 break;
2735 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2736 if (value >= USB_MAX_DEVICES) {
2737 err = USBD_IOERROR;
2738 goto ret;
2739 }
2740 sc->sc_addr = value;
2741 break;
2742 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2743 if (value != 0 && value != 1) {
2744 err = USBD_IOERROR;
2745 goto ret;
2746 }
2747 sc->sc_conf = value;
2748 break;
2749 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2750 break;
2751 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2752 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2753 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2754 err = USBD_IOERROR;
2755 goto ret;
2756 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2757 break;
2758 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2759 break;
2760 /* Hub requests */
2761 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2762 break;
2763 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2764 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2765 "port=%d feature=%d\n",
2766 index, value));
2767 if (index == 1)
2768 port = UHCI_PORTSC1;
2769 else if (index == 2)
2770 port = UHCI_PORTSC2;
2771 else {
2772 err = USBD_IOERROR;
2773 goto ret;
2774 }
2775 switch(value) {
2776 case UHF_PORT_ENABLE:
2777 x = UREAD2(sc, port);
2778 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
2779 break;
2780 case UHF_PORT_SUSPEND:
2781 x = UREAD2(sc, port);
2782 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
2783 break;
2784 case UHF_PORT_RESET:
2785 x = UREAD2(sc, port);
2786 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2787 break;
2788 case UHF_C_PORT_CONNECTION:
2789 x = UREAD2(sc, port);
2790 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
2791 break;
2792 case UHF_C_PORT_ENABLE:
2793 x = UREAD2(sc, port);
2794 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
2795 break;
2796 case UHF_C_PORT_OVER_CURRENT:
2797 x = UREAD2(sc, port);
2798 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
2799 break;
2800 case UHF_C_PORT_RESET:
2801 sc->sc_isreset = 0;
2802 err = USBD_NORMAL_COMPLETION;
2803 goto ret;
2804 case UHF_PORT_CONNECTION:
2805 case UHF_PORT_OVER_CURRENT:
2806 case UHF_PORT_POWER:
2807 case UHF_PORT_LOW_SPEED:
2808 case UHF_C_PORT_SUSPEND:
2809 default:
2810 err = USBD_IOERROR;
2811 goto ret;
2812 }
2813 break;
2814 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
2815 if (index == 1)
2816 port = UHCI_PORTSC1;
2817 else if (index == 2)
2818 port = UHCI_PORTSC2;
2819 else {
2820 err = USBD_IOERROR;
2821 goto ret;
2822 }
2823 if (len > 0) {
2824 *(u_int8_t *)buf =
2825 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
2826 UHCI_PORTSC_LS_SHIFT;
2827 totlen = 1;
2828 }
2829 break;
2830 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2831 if (value != 0) {
2832 err = USBD_IOERROR;
2833 goto ret;
2834 }
2835 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
2836 totlen = l;
2837 memcpy(buf, &uhci_hubd_piix, l);
2838 break;
2839 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2840 if (len != 4) {
2841 err = USBD_IOERROR;
2842 goto ret;
2843 }
2844 memset(buf, 0, len);
2845 totlen = len;
2846 break;
2847 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2848 if (index == 1)
2849 port = UHCI_PORTSC1;
2850 else if (index == 2)
2851 port = UHCI_PORTSC2;
2852 else {
2853 err = USBD_IOERROR;
2854 goto ret;
2855 }
2856 if (len != 4) {
2857 err = USBD_IOERROR;
2858 goto ret;
2859 }
2860 x = UREAD2(sc, port);
2861 status = change = 0;
2862 if (x & UHCI_PORTSC_CCS )
2863 status |= UPS_CURRENT_CONNECT_STATUS;
2864 if (x & UHCI_PORTSC_CSC )
2865 change |= UPS_C_CONNECT_STATUS;
2866 if (x & UHCI_PORTSC_PE )
2867 status |= UPS_PORT_ENABLED;
2868 if (x & UHCI_PORTSC_POEDC)
2869 change |= UPS_C_PORT_ENABLED;
2870 if (x & UHCI_PORTSC_OCI )
2871 status |= UPS_OVERCURRENT_INDICATOR;
2872 if (x & UHCI_PORTSC_OCIC )
2873 change |= UPS_C_OVERCURRENT_INDICATOR;
2874 if (x & UHCI_PORTSC_SUSP )
2875 status |= UPS_SUSPEND;
2876 if (x & UHCI_PORTSC_LSDA )
2877 status |= UPS_LOW_SPEED;
2878 status |= UPS_PORT_POWER;
2879 if (sc->sc_isreset)
2880 change |= UPS_C_PORT_RESET;
2881 USETW(ps.wPortStatus, status);
2882 USETW(ps.wPortChange, change);
2883 l = min(len, sizeof ps);
2884 memcpy(buf, &ps, l);
2885 totlen = l;
2886 break;
2887 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2888 err = USBD_IOERROR;
2889 goto ret;
2890 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2891 break;
2892 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2893 if (index == 1)
2894 port = UHCI_PORTSC1;
2895 else if (index == 2)
2896 port = UHCI_PORTSC2;
2897 else {
2898 err = USBD_IOERROR;
2899 goto ret;
2900 }
2901 switch(value) {
2902 case UHF_PORT_ENABLE:
2903 x = UREAD2(sc, port);
2904 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2905 break;
2906 case UHF_PORT_SUSPEND:
2907 x = UREAD2(sc, port);
2908 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
2909 break;
2910 case UHF_PORT_RESET:
2911 x = UREAD2(sc, port);
2912 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2913 usb_delay_ms(&sc->sc_bus, 10);
2914 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2915 delay(100);
2916 x = UREAD2(sc, port);
2917 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2918 delay(100);
2919 DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
2920 index, UREAD2(sc, port)));
2921 sc->sc_isreset = 1;
2922 break;
2923 case UHF_C_PORT_CONNECTION:
2924 case UHF_C_PORT_ENABLE:
2925 case UHF_C_PORT_OVER_CURRENT:
2926 case UHF_PORT_CONNECTION:
2927 case UHF_PORT_OVER_CURRENT:
2928 case UHF_PORT_POWER:
2929 case UHF_PORT_LOW_SPEED:
2930 case UHF_C_PORT_SUSPEND:
2931 case UHF_C_PORT_RESET:
2932 default:
2933 err = USBD_IOERROR;
2934 goto ret;
2935 }
2936 break;
2937 default:
2938 err = USBD_IOERROR;
2939 goto ret;
2940 }
2941 xfer->actlen = totlen;
2942 err = USBD_NORMAL_COMPLETION;
2943 ret:
2944 xfer->status = err;
2945 xfer->hcpriv = 0;
2946 s = splusb();
2947 usb_transfer_complete(xfer);
2948 splx(s);
2949 return (USBD_IN_PROGRESS);
2950 }
2951
2952 /* Abort a root control request. */
2953 void
2954 uhci_root_ctrl_abort(xfer)
2955 usbd_xfer_handle xfer;
2956 {
2957 /* Nothing to do, all transfers are synchronous. */
2958 }
2959
2960 /* Close the root pipe. */
2961 void
2962 uhci_root_ctrl_close(pipe)
2963 usbd_pipe_handle pipe;
2964 {
2965 DPRINTF(("uhci_root_ctrl_close\n"));
2966 }
2967
2968 /* Abort a root interrupt request. */
2969 void
2970 uhci_root_intr_abort(xfer)
2971 usbd_xfer_handle xfer;
2972 {
2973 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2974
2975 usb_untimeout(uhci_timo, xfer, xfer->timo_handle);
2976 sc->sc_has_timo = NULL;
2977
2978 if (xfer->pipe->intrxfer == xfer) {
2979 DPRINTF(("uhci_root_intr_abort: remove\n"));
2980 xfer->pipe->intrxfer = 0;
2981 }
2982 xfer->status = USBD_CANCELLED;
2983 usb_transfer_complete(xfer);
2984 }
2985
2986 usbd_status
2987 uhci_root_intr_transfer(xfer)
2988 usbd_xfer_handle xfer;
2989 {
2990 usbd_status err;
2991
2992 /* Insert last in queue. */
2993 err = usb_insert_transfer(xfer);
2994 if (err)
2995 return (err);
2996
2997 /* Pipe isn't running (otherwise err would be USBD_INPROG),
2998 * start first
2999 */
3000 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3001 }
3002
3003 /* Start a transfer on the root interrupt pipe */
3004 usbd_status
3005 uhci_root_intr_start(xfer)
3006 usbd_xfer_handle xfer;
3007 {
3008 usbd_pipe_handle pipe = xfer->pipe;
3009 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3010
3011 DPRINTFN(3, ("uhci_root_intr_transfer: xfer=%p len=%d flags=%d\n",
3012 xfer, xfer->length, xfer->flags));
3013
3014 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3015 usb_timeout(uhci_timo, xfer, sc->sc_ival, xfer->timo_handle);
3016 sc->sc_has_timo = xfer;
3017 return (USBD_IN_PROGRESS);
3018 }
3019
3020 /* Close the root interrupt pipe. */
3021 void
3022 uhci_root_intr_close(pipe)
3023 usbd_pipe_handle pipe;
3024 {
3025 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3026
3027 usb_untimeout(uhci_timo, pipe->intrxfer, pipe->intrxfer->timo_handle);
3028 sc->sc_has_timo = NULL;
3029 DPRINTF(("uhci_root_intr_close\n"));
3030 }
3031