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