Resync patch with contrib.
[dragonfly.git] / sys / bus / usb / uhci.c
blobf2728708210d3a5c3c55ba15aef6fa0961ce4c1f
1 /* $NetBSD: uhci.c,v 1.170 2003/02/19 01:35:04 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/uhci.c,v 1.162.2.1 2006/03/01 01:59:04 iedowse Exp $ */
3 /* $DragonFly: src/sys/bus/usb/uhci.c,v 1.25 2007/06/29 22:56:31 hasso Exp $ */
5 /* Also already incorporated from NetBSD:
6 * $NetBSD: uhci.c,v 1.172 2003/02/23 04:19:26 simonb Exp $
7 * $NetBSD: uhci.c,v 1.173 2003/05/13 04:41:59 gson Exp $
8 * $NetBSD: uhci.c,v 1.175 2003/09/12 16:18:08 mycroft Exp $
9 * $NetBSD: uhci.c,v 1.176 2003/11/04 19:11:21 mycroft Exp $
10 * $NetBSD: uhci.c,v 1.177 2003/12/29 08:17:10 toshii Exp $
11 * $NetBSD: uhci.c,v 1.178 2004/03/02 16:32:05 martin Exp $
12 * $NetBSD: uhci.c,v 1.180 2004/07/17 20:12:03 mycroft Exp $
16 * Copyright (c) 1998 The NetBSD Foundation, Inc.
17 * All rights reserved.
19 * This code is derived from software contributed to The NetBSD Foundation
20 * by Lennart Augustsson (lennart@augustsson.net) at
21 * Carlstedt Research & Technology.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * This product includes software developed by the NetBSD
34 * Foundation, Inc. and its contributors.
35 * 4. Neither the name of The NetBSD Foundation nor the names of its
36 * contributors may be used to endorse or promote products derived
37 * from this software without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
53 * USB Universal Host Controller driver.
54 * Handles e.g. PIIX3 and PIIX4.
56 * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
57 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
58 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
59 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/malloc.h>
66 #include <sys/endian.h>
67 #include <sys/module.h>
68 #include <sys/bus.h>
69 #if defined(DIAGNOSTIC) && defined(__i386__)
70 #include <machine/cpu.h>
71 #endif
72 #include <sys/proc.h>
73 #include <sys/queue.h>
74 #include <sys/sysctl.h>
75 #include <sys/thread2.h>
77 #include <machine/endian.h>
79 #include <bus/usb/usb.h>
80 #include <bus/usb/usbdi.h>
81 #include <bus/usb/usbdivar.h>
82 #include <bus/usb/usb_mem.h>
83 #include <bus/usb/usb_quirks.h>
85 #include <bus/usb/uhcireg.h>
86 #include <bus/usb/uhcivar.h>
88 /* Use bandwidth reclamation for control transfers. Some devices choke on it. */
89 /*#define UHCI_CTL_LOOP */
91 #define delay(d) DELAY(d)
93 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
95 #ifdef USB_DEBUG
96 uhci_softc_t *thesc;
97 #define DPRINTF(x) if (uhcidebug) kprintf x
98 #define DPRINTFN(n,x) if (uhcidebug>(n)) kprintf x
99 int uhcidebug = 0;
100 int uhcinoloop = 0;
101 SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
102 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW,
103 &uhcidebug, 0, "uhci debug level");
104 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW,
105 &uhcinoloop, 0, "uhci noloop");
106 #define bitmask_snprintf(q,f,b,l) ksnprintf((b), (l), "%b", (q), (f))
107 #else
108 #define DPRINTF(x)
109 #define DPRINTFN(n,x)
110 #endif
112 struct uhci_pipe {
113 struct usbd_pipe pipe;
114 int nexttoggle;
116 u_char aborting;
117 usbd_xfer_handle abortstart, abortend;
119 /* Info needed for different pipe kinds. */
120 union {
121 /* Control pipe */
122 struct {
123 uhci_soft_qh_t *sqh;
124 usb_dma_t reqdma;
125 uhci_soft_td_t *setup, *stat;
126 u_int length;
127 } ctl;
128 /* Interrupt pipe */
129 struct {
130 int npoll;
131 int isread;
132 uhci_soft_qh_t **qhs;
133 } intr;
134 /* Bulk pipe */
135 struct {
136 uhci_soft_qh_t *sqh;
137 u_int length;
138 int isread;
139 } bulk;
140 /* Iso pipe */
141 struct iso {
142 uhci_soft_td_t **stds;
143 int next, inuse;
144 } iso;
145 } u;
148 static void uhci_globalreset(uhci_softc_t *);
149 static usbd_status uhci_portreset(uhci_softc_t*, int);
150 static void uhci_reset(uhci_softc_t *);
151 static usbd_status uhci_run(uhci_softc_t *, int run);
152 static uhci_soft_td_t *uhci_alloc_std(uhci_softc_t *);
153 static void uhci_free_std(uhci_softc_t *, uhci_soft_td_t *);
154 static uhci_soft_qh_t *uhci_alloc_sqh(uhci_softc_t *);
155 static void uhci_free_sqh(uhci_softc_t *, uhci_soft_qh_t *);
156 #if 0
157 static void uhci_enter_ctl_q(uhci_softc_t *, uhci_soft_qh_t *,
158 uhci_intr_info_t *);
159 static void uhci_exit_ctl_q(uhci_softc_t *, uhci_soft_qh_t *);
160 #endif
162 static void uhci_free_std_chain(uhci_softc_t *,
163 uhci_soft_td_t *, uhci_soft_td_t *);
164 static usbd_status uhci_alloc_std_chain(struct uhci_pipe *,
165 uhci_softc_t *, int, int, u_int16_t, usb_dma_t *,
166 uhci_soft_td_t **, uhci_soft_td_t **);
167 static void uhci_poll_hub(void *);
168 static void uhci_waitintr(uhci_softc_t *, usbd_xfer_handle);
169 static void uhci_check_intr(uhci_softc_t *, uhci_intr_info_t *);
170 static void uhci_idone(uhci_intr_info_t *);
172 static void uhci_abort_xfer(usbd_xfer_handle, usbd_status status);
174 static void uhci_timeout(void *);
175 static void uhci_timeout_task(void *);
176 static void uhci_add_ls_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
177 static void uhci_add_hs_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
178 static void uhci_add_bulk(uhci_softc_t *, uhci_soft_qh_t *);
179 static void uhci_remove_ls_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
180 static void uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
181 static void uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *);
182 static int uhci_str(usb_string_descriptor_t *, int, char *);
183 static void uhci_add_loop(uhci_softc_t *sc);
184 static void uhci_rem_loop(uhci_softc_t *sc);
186 static usbd_status uhci_setup_isoc(usbd_pipe_handle pipe);
187 static void uhci_device_isoc_enter(usbd_xfer_handle);
189 static usbd_status uhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
190 static void uhci_freem(struct usbd_bus *, usb_dma_t *);
192 static usbd_xfer_handle uhci_allocx(struct usbd_bus *);
193 static void uhci_freex(struct usbd_bus *, usbd_xfer_handle);
195 static usbd_status uhci_device_ctrl_transfer(usbd_xfer_handle);
196 static usbd_status uhci_device_ctrl_start(usbd_xfer_handle);
197 static void uhci_device_ctrl_abort(usbd_xfer_handle);
198 static void uhci_device_ctrl_close(usbd_pipe_handle);
199 static void uhci_device_ctrl_done(usbd_xfer_handle);
201 static usbd_status uhci_device_intr_transfer(usbd_xfer_handle);
202 static usbd_status uhci_device_intr_start(usbd_xfer_handle);
203 static void uhci_device_intr_abort(usbd_xfer_handle);
204 static void uhci_device_intr_close(usbd_pipe_handle);
205 static void uhci_device_intr_done(usbd_xfer_handle);
207 static usbd_status uhci_device_bulk_transfer(usbd_xfer_handle);
208 static usbd_status uhci_device_bulk_start(usbd_xfer_handle);
209 static void uhci_device_bulk_abort(usbd_xfer_handle);
210 static void uhci_device_bulk_close(usbd_pipe_handle);
211 static void uhci_device_bulk_done(usbd_xfer_handle);
213 static usbd_status uhci_device_isoc_transfer(usbd_xfer_handle);
214 static usbd_status uhci_device_isoc_start(usbd_xfer_handle);
215 static void uhci_device_isoc_abort(usbd_xfer_handle);
216 static void uhci_device_isoc_close(usbd_pipe_handle);
217 static void uhci_device_isoc_done(usbd_xfer_handle);
219 static usbd_status uhci_root_ctrl_transfer(usbd_xfer_handle);
220 static usbd_status uhci_root_ctrl_start(usbd_xfer_handle);
221 static void uhci_root_ctrl_abort(usbd_xfer_handle);
222 static void uhci_root_ctrl_close(usbd_pipe_handle);
223 static void uhci_root_ctrl_done(usbd_xfer_handle);
225 static usbd_status uhci_root_intr_transfer(usbd_xfer_handle);
226 static usbd_status uhci_root_intr_start(usbd_xfer_handle);
227 static void uhci_root_intr_abort(usbd_xfer_handle);
228 static void uhci_root_intr_close(usbd_pipe_handle);
229 static void uhci_root_intr_done(usbd_xfer_handle);
231 static usbd_status uhci_open(usbd_pipe_handle);
232 static void uhci_poll(struct usbd_bus *);
233 static void uhci_softintr(void *);
235 static usbd_status uhci_device_request(usbd_xfer_handle xfer);
237 static void uhci_add_intr(uhci_softc_t *, uhci_soft_qh_t *);
238 static void uhci_remove_intr(uhci_softc_t *, uhci_soft_qh_t *);
239 static usbd_status uhci_device_setintr(uhci_softc_t *sc,
240 struct uhci_pipe *pipe, int ival);
242 static void uhci_device_clear_toggle(usbd_pipe_handle pipe);
243 static void uhci_noop(usbd_pipe_handle pipe);
245 static __inline uhci_soft_qh_t *uhci_find_prev_qh(uhci_soft_qh_t *,
246 uhci_soft_qh_t *);
248 #ifdef USB_DEBUG
249 static void uhci_dump_all(uhci_softc_t *);
250 static void uhci_dumpregs(uhci_softc_t *);
251 static void uhci_dump_qhs(uhci_soft_qh_t *);
252 static void uhci_dump_qh(uhci_soft_qh_t *);
253 static void uhci_dump_tds(uhci_soft_td_t *);
254 static void uhci_dump_td(uhci_soft_td_t *);
255 static void uhci_dump_ii(uhci_intr_info_t *ii);
256 void uhci_dump(void);
257 #endif
259 #define UBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
260 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
261 #define UWRITE1(sc, r, x) \
262 do { UBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); \
263 } while (/*CONSTCOND*/0)
264 #define UWRITE2(sc, r, x) \
265 do { UBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); \
266 } while (/*CONSTCOND*/0)
267 #define UWRITE4(sc, r, x) \
268 do { UBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); \
269 } while (/*CONSTCOND*/0)
270 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
271 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
272 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
274 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
275 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
277 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
279 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
281 #define UHCI_INTR_ENDPT 1
283 struct usbd_bus_methods uhci_bus_methods = {
284 uhci_open,
285 uhci_softintr,
286 uhci_poll,
287 uhci_allocm,
288 uhci_freem,
289 uhci_allocx,
290 uhci_freex,
293 struct usbd_pipe_methods uhci_root_ctrl_methods = {
294 uhci_root_ctrl_transfer,
295 uhci_root_ctrl_start,
296 uhci_root_ctrl_abort,
297 uhci_root_ctrl_close,
298 uhci_noop,
299 uhci_root_ctrl_done,
302 struct usbd_pipe_methods uhci_root_intr_methods = {
303 uhci_root_intr_transfer,
304 uhci_root_intr_start,
305 uhci_root_intr_abort,
306 uhci_root_intr_close,
307 uhci_noop,
308 uhci_root_intr_done,
311 struct usbd_pipe_methods uhci_device_ctrl_methods = {
312 uhci_device_ctrl_transfer,
313 uhci_device_ctrl_start,
314 uhci_device_ctrl_abort,
315 uhci_device_ctrl_close,
316 uhci_noop,
317 uhci_device_ctrl_done,
320 struct usbd_pipe_methods uhci_device_intr_methods = {
321 uhci_device_intr_transfer,
322 uhci_device_intr_start,
323 uhci_device_intr_abort,
324 uhci_device_intr_close,
325 uhci_device_clear_toggle,
326 uhci_device_intr_done,
329 struct usbd_pipe_methods uhci_device_bulk_methods = {
330 uhci_device_bulk_transfer,
331 uhci_device_bulk_start,
332 uhci_device_bulk_abort,
333 uhci_device_bulk_close,
334 uhci_device_clear_toggle,
335 uhci_device_bulk_done,
338 struct usbd_pipe_methods uhci_device_isoc_methods = {
339 uhci_device_isoc_transfer,
340 uhci_device_isoc_start,
341 uhci_device_isoc_abort,
342 uhci_device_isoc_close,
343 uhci_noop,
344 uhci_device_isoc_done,
347 #define uhci_add_intr_info(sc, ii) \
348 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list)
349 #define uhci_del_intr_info(ii) \
350 do { \
351 LIST_REMOVE((ii), list); \
352 (ii)->list.le_prev = NULL; \
353 } while (0)
354 #define uhci_active_intr_info(ii) ((ii)->list.le_prev != NULL)
356 static __inline uhci_soft_qh_t *
357 uhci_find_prev_qh(uhci_soft_qh_t *pqh, uhci_soft_qh_t *sqh)
359 DPRINTFN(15,("uhci_find_prev_qh: pqh=%p sqh=%p\n", pqh, sqh));
361 for (; pqh->hlink != sqh; pqh = pqh->hlink) {
362 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
363 if (le32toh(pqh->qh.qh_hlink) & UHCI_PTR_T) {
364 kprintf("uhci_find_prev_qh: QH not found\n");
365 return (NULL);
367 #endif
369 return (pqh);
372 void
373 uhci_globalreset(uhci_softc_t *sc)
375 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */
376 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
377 UHCICMD(sc, 0); /* do nothing */
380 usbd_status
381 uhci_init(uhci_softc_t *sc)
383 usbd_status err;
384 int i, j;
385 uhci_soft_qh_t *clsqh, *chsqh, *bsqh, *sqh, *lsqh;
386 uhci_soft_td_t *std;
388 DPRINTFN(1,("uhci_init: start\n"));
390 #ifdef USB_DEBUG
391 thesc = sc;
393 if (uhcidebug > 2)
394 uhci_dumpregs(sc);
395 #endif
397 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
398 uhci_globalreset(sc); /* reset the controller */
399 uhci_reset(sc);
401 /* Allocate and initialize real frame array. */
402 err = usb_allocmem(&sc->sc_bus,
403 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
404 UHCI_FRAMELIST_ALIGN, &sc->sc_dma);
405 if (err)
406 return (err);
407 sc->sc_pframes = KERNADDR(&sc->sc_dma, 0);
408 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */
409 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0)); /* set frame list*/
412 * Allocate a TD, inactive, that hangs from the last QH.
413 * This is to avoid a bug in the PIIX that makes it run berserk
414 * otherwise.
416 std = uhci_alloc_std(sc);
417 if (std == NULL)
418 return (USBD_NOMEM);
419 std->link.std = NULL;
420 std->td.td_link = htole32(UHCI_PTR_T);
421 std->td.td_status = htole32(0); /* inactive */
422 std->td.td_token = htole32(0);
423 std->td.td_buffer = htole32(0);
425 /* Allocate the dummy QH marking the end and used for looping the QHs.*/
426 lsqh = uhci_alloc_sqh(sc);
427 if (lsqh == NULL)
428 return (USBD_NOMEM);
429 lsqh->hlink = NULL;
430 lsqh->qh.qh_hlink = htole32(UHCI_PTR_T); /* end of QH chain */
431 lsqh->elink = std;
432 lsqh->qh.qh_elink = htole32(std->physaddr | UHCI_PTR_TD);
433 sc->sc_last_qh = lsqh;
435 /* Allocate the dummy QH where bulk traffic will be queued. */
436 bsqh = uhci_alloc_sqh(sc);
437 if (bsqh == NULL)
438 return (USBD_NOMEM);
439 bsqh->hlink = lsqh;
440 bsqh->qh.qh_hlink = htole32(lsqh->physaddr | UHCI_PTR_QH);
441 bsqh->elink = NULL;
442 bsqh->qh.qh_elink = htole32(UHCI_PTR_T);
443 sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
445 /* Allocate dummy QH where high speed control traffic will be queued. */
446 chsqh = uhci_alloc_sqh(sc);
447 if (chsqh == NULL)
448 return (USBD_NOMEM);
449 chsqh->hlink = bsqh;
450 chsqh->qh.qh_hlink = htole32(bsqh->physaddr | UHCI_PTR_QH);
451 chsqh->elink = NULL;
452 chsqh->qh.qh_elink = htole32(UHCI_PTR_T);
453 sc->sc_hctl_start = sc->sc_hctl_end = chsqh;
455 /* Allocate dummy QH where control traffic will be queued. */
456 clsqh = uhci_alloc_sqh(sc);
457 if (clsqh == NULL)
458 return (USBD_NOMEM);
459 clsqh->hlink = bsqh;
460 clsqh->qh.qh_hlink = htole32(chsqh->physaddr | UHCI_PTR_QH);
461 clsqh->elink = NULL;
462 clsqh->qh.qh_elink = htole32(UHCI_PTR_T);
463 sc->sc_lctl_start = sc->sc_lctl_end = clsqh;
466 * Make all (virtual) frame list pointers point to the interrupt
467 * queue heads and the interrupt queue heads at the control
468 * queue head and point the physical frame list to the virtual.
470 for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
471 std = uhci_alloc_std(sc);
472 sqh = uhci_alloc_sqh(sc);
473 if (std == NULL || sqh == NULL)
474 return (USBD_NOMEM);
475 std->link.sqh = sqh;
477 std->td.td_link = htole32(sqh->physaddr | UHCI_PTR_QH);
478 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
479 std->td.td_token = htole32(0);
480 std->td.td_buffer = htole32(0);
481 sqh->hlink = clsqh;
482 sqh->qh.qh_hlink = htole32(clsqh->physaddr | UHCI_PTR_QH);
483 sqh->elink = NULL;
484 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
485 sc->sc_vframes[i].htd = std;
486 sc->sc_vframes[i].etd = std;
487 sc->sc_vframes[i].hqh = sqh;
488 sc->sc_vframes[i].eqh = sqh;
489 for (j = i;
490 j < UHCI_FRAMELIST_COUNT;
491 j += UHCI_VFRAMELIST_COUNT) {
492 sc->sc_pframes[j] = htole32(std->physaddr);
496 LIST_INIT(&sc->sc_intrhead);
498 STAILQ_INIT(&sc->sc_free_xfers);
500 callout_init(&sc->sc_poll_handle);
502 /* Set up the bus struct. */
503 sc->sc_bus.methods = &uhci_bus_methods;
504 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
506 DPRINTFN(1,("uhci_init: enabling\n"));
507 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
508 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */
510 UHCICMD(sc, UHCI_CMD_MAXP); /* Assume 64 byte packets at frame end */
512 return (uhci_run(sc, 1)); /* and here we go... */
516 uhci_detach(struct uhci_softc *sc, int flags)
518 usbd_xfer_handle xfer;
519 int rv = 0;
521 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
522 uhci_run(sc, 0);
524 /* Free all xfers associated with this HC. */
525 for (;;) {
526 xfer = STAILQ_FIRST(&sc->sc_free_xfers);
527 if (xfer == NULL)
528 break;
529 STAILQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
530 kfree(xfer, M_USB);
533 /* XXX free other data structures XXX */
534 usb_freemem(&sc->sc_bus, &sc->sc_dma);
536 return (rv);
539 usbd_status
540 uhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
542 return (usb_allocmem(bus, size, 0, dma));
545 void
546 uhci_freem(struct usbd_bus *bus, usb_dma_t *dma)
548 usb_freemem(bus, dma);
551 usbd_xfer_handle
552 uhci_allocx(struct usbd_bus *bus)
554 struct uhci_softc *sc = (struct uhci_softc *)bus;
555 usbd_xfer_handle xfer;
557 xfer = STAILQ_FIRST(&sc->sc_free_xfers);
558 if (xfer != NULL) {
559 STAILQ_REMOVE_HEAD(&sc->sc_free_xfers, next);
560 #ifdef DIAGNOSTIC
561 if (xfer->busy_free != XFER_FREE) {
562 kprintf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer,
563 xfer->busy_free);
565 #endif
566 } else {
567 xfer = kmalloc(sizeof(struct uhci_xfer), M_USB, M_INTWAIT);
569 if (xfer != NULL) {
570 memset(xfer, 0, sizeof (struct uhci_xfer));
571 UXFER(xfer)->iinfo.sc = sc;
572 usb_init_task(&UXFER(xfer)->abort_task, uhci_timeout_task,
573 xfer);
574 UXFER(xfer)->uhci_xfer_flags = 0;
575 #ifdef DIAGNOSTIC
576 UXFER(xfer)->iinfo.isdone = 1;
577 xfer->busy_free = XFER_BUSY;
578 #endif
580 return (xfer);
583 void
584 uhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
586 struct uhci_softc *sc = (struct uhci_softc *)bus;
588 #ifdef DIAGNOSTIC
589 if (xfer->busy_free != XFER_BUSY) {
590 kprintf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer,
591 xfer->busy_free);
592 return;
594 xfer->busy_free = XFER_FREE;
595 if (!UXFER(xfer)->iinfo.isdone) {
596 kprintf("uhci_freex: !isdone\n");
597 return;
599 #endif
600 STAILQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
604 * Shut down the controller when the system is going down.
606 void
607 uhci_shutdown(void *v)
609 uhci_softc_t *sc = v;
611 DPRINTF(("uhci_shutdown: stopping the HC\n"));
612 uhci_run(sc, 0); /* stop the controller */
616 * Handle suspend/resume.
618 * We need to switch to polling mode here, because this routine is
619 * called from an interrupt context. This is all right since we
620 * are almost suspended anyway.
622 void
623 uhci_power(int why, void *v)
625 uhci_softc_t *sc = v;
626 int cmd;
628 crit_enter();
629 cmd = UREAD2(sc, UHCI_CMD);
631 DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
632 sc, why, sc->sc_suspend, cmd));
634 if (why != PWR_RESUME) {
635 #ifdef USB_DEBUG
636 if (uhcidebug > 2)
637 uhci_dumpregs(sc);
638 #endif
639 if (sc->sc_intr_xfer != NULL)
640 callout_stop(&sc->sc_poll_handle);
641 sc->sc_bus.use_polling++;
642 uhci_run(sc, 0); /* stop the controller */
644 /* save some state if BIOS doesn't */
645 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
646 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
648 UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */
650 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter global suspend */
651 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
652 sc->sc_suspend = why;
653 sc->sc_bus.use_polling--;
654 DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc, UHCI_CMD)));
655 } else {
656 #ifdef DIAGNOSTIC
657 if (sc->sc_suspend == PWR_RESUME)
658 kprintf("uhci_power: weird, resume without suspend.\n");
659 #endif
660 sc->sc_bus.use_polling++;
661 sc->sc_suspend = why;
662 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
663 uhci_globalreset(sc); /* reset the controller */
664 uhci_reset(sc);
665 if (cmd & UHCI_CMD_RS)
666 uhci_run(sc, 0); /* in case BIOS has started it */
668 uhci_globalreset(sc);
669 uhci_reset(sc);
671 /* restore saved state */
672 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0));
673 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
674 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
676 UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force global resume */
677 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
678 UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
679 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
680 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* re-enable intrs */
681 UHCICMD(sc, UHCI_CMD_MAXP);
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 callout_reset(&sc->sc_poll_handle, sc->sc_ival,
687 uhci_poll_hub, sc->sc_intr_xfer);
688 #ifdef USB_DEBUG
689 if (uhcidebug > 2)
690 uhci_dumpregs(sc);
691 #endif
693 crit_exit();
696 #ifdef USB_DEBUG
697 static void
698 uhci_dumpregs(uhci_softc_t *sc)
700 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
701 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
702 device_get_nameunit(sc->sc_bus.bdev),
703 UREAD2(sc, UHCI_CMD),
704 UREAD2(sc, UHCI_STS),
705 UREAD2(sc, UHCI_INTR),
706 UREAD2(sc, UHCI_FRNUM),
707 UREAD4(sc, UHCI_FLBASEADDR),
708 UREAD1(sc, UHCI_SOF),
709 UREAD2(sc, UHCI_PORTSC1),
710 UREAD2(sc, UHCI_PORTSC2)));
713 void
714 uhci_dump_td(uhci_soft_td_t *p)
716 char sbuf[128], sbuf2[128];
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)));
726 bitmask_snprintf((u_int32_t)le32toh(p->td.td_link), "\20\1T\2Q\3VF",
727 sbuf, sizeof(sbuf));
728 bitmask_snprintf((u_int32_t)le32toh(p->td.td_status),
729 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
730 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
731 sbuf2, sizeof(sbuf2));
733 DPRINTFN(-1,(" %s %s,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
734 "D=%d,maxlen=%d\n", sbuf, sbuf2,
735 UHCI_TD_GET_ERRCNT(le32toh(p->td.td_status)),
736 UHCI_TD_GET_ACTLEN(le32toh(p->td.td_status)),
737 UHCI_TD_GET_PID(le32toh(p->td.td_token)),
738 UHCI_TD_GET_DEVADDR(le32toh(p->td.td_token)),
739 UHCI_TD_GET_ENDPT(le32toh(p->td.td_token)),
740 UHCI_TD_GET_DT(le32toh(p->td.td_token)),
741 UHCI_TD_GET_MAXLEN(le32toh(p->td.td_token))));
744 void
745 uhci_dump_qh(uhci_soft_qh_t *sqh)
747 DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh,
748 (int)sqh->physaddr, le32toh(sqh->qh.qh_hlink),
749 le32toh(sqh->qh.qh_elink)));
752 #if 1
753 void
754 uhci_dump(void)
756 uhci_dump_all(thesc);
758 #endif
760 void
761 uhci_dump_all(uhci_softc_t *sc)
763 uhci_dumpregs(sc);
764 kprintf("intrs=%d\n", sc->sc_bus.no_intrs);
765 /*kprintf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);*/
766 uhci_dump_qh(sc->sc_lctl_start);
769 void
770 uhci_dump_qhs(uhci_soft_qh_t *sqh)
772 uhci_dump_qh(sqh);
774 /* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
775 * Traverses sideways first, then down.
777 * QH1
778 * QH2
779 * No QH
780 * TD2.1
781 * TD2.2
782 * TD1.1
783 * etc.
785 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
789 if (sqh->hlink != NULL && !(le32toh(sqh->qh.qh_hlink) & UHCI_PTR_T))
790 uhci_dump_qhs(sqh->hlink);
791 else
792 DPRINTF(("No QH\n"));
794 if (sqh->elink != NULL && !(le32toh(sqh->qh.qh_elink) & UHCI_PTR_T))
795 uhci_dump_tds(sqh->elink);
796 else
797 DPRINTF(("No TD\n"));
800 void
801 uhci_dump_tds(uhci_soft_td_t *std)
803 uhci_soft_td_t *td;
805 for(td = std; td != NULL; td = td->link.std) {
806 uhci_dump_td(td);
808 /* Check whether the link pointer in this TD marks
809 * the link pointer as end of queue. This avoids
810 * printing the free list in case the queue/TD has
811 * already been moved there (seatbelt).
813 if (le32toh(td->td.td_link) & UHCI_PTR_T ||
814 le32toh(td->td.td_link) == 0)
815 break;
819 static void
820 uhci_dump_ii(uhci_intr_info_t *ii)
822 usbd_pipe_handle pipe;
823 usb_endpoint_descriptor_t *ed;
824 usbd_device_handle dev;
826 #ifdef DIAGNOSTIC
827 #define DONE ii->isdone
828 #else
829 #define DONE 0
830 #endif
831 if (ii == NULL) {
832 kprintf("ii NULL\n");
833 return;
835 if (ii->xfer == NULL) {
836 kprintf("ii %p: done=%d xfer=NULL\n",
837 ii, DONE);
838 return;
840 pipe = ii->xfer->pipe;
841 if (pipe == NULL) {
842 kprintf("ii %p: done=%d xfer=%p pipe=NULL\n",
843 ii, DONE, ii->xfer);
844 return;
846 if (pipe->endpoint == NULL) {
847 kprintf("ii %p: done=%d xfer=%p pipe=%p pipe->endpoint=NULL\n",
848 ii, DONE, ii->xfer, pipe);
849 return;
851 if (pipe->device == NULL) {
852 kprintf("ii %p: done=%d xfer=%p pipe=%p pipe->device=NULL\n",
853 ii, DONE, ii->xfer, pipe);
854 return;
856 ed = pipe->endpoint->edesc;
857 dev = pipe->device;
858 kprintf("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",
859 ii, DONE, ii->xfer, dev,
860 UGETW(dev->ddesc.idVendor),
861 UGETW(dev->ddesc.idProduct),
862 dev->address, pipe,
863 ed->bEndpointAddress, ed->bmAttributes);
864 #undef DONE
867 void uhci_dump_iis(struct uhci_softc *sc);
868 void
869 uhci_dump_iis(struct uhci_softc *sc)
871 uhci_intr_info_t *ii;
873 kprintf("intr_info list:\n");
874 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
875 uhci_dump_ii(ii);
878 void iidump(void);
879 void iidump(void) { uhci_dump_iis(thesc); }
881 #endif
884 * This routine is executed periodically and simulates interrupts
885 * from the root controller interrupt pipe for port status change.
887 void
888 uhci_poll_hub(void *addr)
890 usbd_xfer_handle xfer = addr;
891 usbd_pipe_handle pipe = xfer->pipe;
892 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
893 u_char *p;
895 DPRINTFN(20, ("uhci_poll_hub\n"));
897 callout_reset(&sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
899 p = KERNADDR(&xfer->dmabuf, 0);
900 p[0] = 0;
901 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
902 p[0] |= 1<<1;
903 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
904 p[0] |= 1<<2;
905 if (p[0] == 0)
906 /* No change, try again in a while */
907 return;
909 xfer->actlen = 1;
910 xfer->status = USBD_NORMAL_COMPLETION;
911 crit_enter();
912 xfer->device->bus->intr_context++;
913 usb_transfer_complete(xfer);
914 xfer->device->bus->intr_context--;
915 crit_exit();
918 void
919 uhci_root_intr_done(usbd_xfer_handle xfer)
923 void
924 uhci_root_ctrl_done(usbd_xfer_handle xfer)
929 * Let the last QH loop back to the high speed control transfer QH.
930 * This is what intel calls "bandwidth reclamation" and improves
931 * USB performance a lot for some devices.
932 * If we are already looping, just count it.
934 void
935 uhci_add_loop(uhci_softc_t *sc) {
936 #ifdef USB_DEBUG
937 if (uhcinoloop)
938 return;
939 #endif
940 if (++sc->sc_loops == 1) {
941 DPRINTFN(5,("uhci_start_loop: add\n"));
942 /* Note, we don't loop back the soft pointer. */
943 sc->sc_last_qh->qh.qh_hlink =
944 htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH);
948 void
949 uhci_rem_loop(uhci_softc_t *sc) {
950 #ifdef USB_DEBUG
951 if (uhcinoloop)
952 return;
953 #endif
954 if (--sc->sc_loops == 0) {
955 DPRINTFN(5,("uhci_end_loop: remove\n"));
956 sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T);
960 /* Add high speed control QH, called from a critical section. */
961 void
962 uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
964 uhci_soft_qh_t *eqh;
966 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
967 eqh = sc->sc_hctl_end;
968 sqh->hlink = eqh->hlink;
969 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
970 eqh->hlink = sqh;
971 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
972 sc->sc_hctl_end = sqh;
973 #ifdef UHCI_CTL_LOOP
974 uhci_add_loop(sc);
975 #endif
978 /* Remove high speed control QH, called from a critical section. */
979 void
980 uhci_remove_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
982 uhci_soft_qh_t *pqh;
984 DPRINTFN(10, ("uhci_remove_hs_ctrl: sqh=%p\n", sqh));
985 #ifdef UHCI_CTL_LOOP
986 uhci_rem_loop(sc);
987 #endif
989 * The T bit should be set in the elink of the QH so that the HC
990 * doesn't follow the pointer. This condition may fail if the
991 * the transferred packet was short so that the QH still points
992 * at the last used TD.
993 * In this case we set the T bit and wait a little for the HC
994 * to stop looking at the TD.
996 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
997 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
998 delay(UHCI_QH_REMOVE_DELAY);
1001 pqh = uhci_find_prev_qh(sc->sc_hctl_start, sqh);
1002 pqh->hlink = sqh->hlink;
1003 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1004 delay(UHCI_QH_REMOVE_DELAY);
1005 if (sc->sc_hctl_end == sqh)
1006 sc->sc_hctl_end = pqh;
1009 /* Add low speed control QH, called from a critical section. */
1010 void
1011 uhci_add_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1013 uhci_soft_qh_t *eqh;
1015 DPRINTFN(10, ("uhci_add_ls_ctrl: sqh=%p\n", sqh));
1016 eqh = sc->sc_lctl_end;
1017 sqh->hlink = eqh->hlink;
1018 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1019 eqh->hlink = sqh;
1020 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1021 sc->sc_lctl_end = sqh;
1024 /* Remove low speed control QH, called from a critical section. */
1025 void
1026 uhci_remove_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1028 uhci_soft_qh_t *pqh;
1030 DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh));
1031 /* See comment in uhci_remove_hs_ctrl() */
1032 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1033 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1034 delay(UHCI_QH_REMOVE_DELAY);
1036 pqh = uhci_find_prev_qh(sc->sc_lctl_start, sqh);
1037 pqh->hlink = sqh->hlink;
1038 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1039 delay(UHCI_QH_REMOVE_DELAY);
1040 if (sc->sc_lctl_end == sqh)
1041 sc->sc_lctl_end = pqh;
1044 /* Add bulk QH, called from a critical section. */
1045 void
1046 uhci_add_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1048 uhci_soft_qh_t *eqh;
1050 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
1051 eqh = sc->sc_bulk_end;
1052 sqh->hlink = eqh->hlink;
1053 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1054 eqh->hlink = sqh;
1055 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1056 sc->sc_bulk_end = sqh;
1057 uhci_add_loop(sc);
1060 /* Remove bulk QH, called from a critical section. */
1061 void
1062 uhci_remove_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1064 uhci_soft_qh_t *pqh;
1066 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
1067 uhci_rem_loop(sc);
1068 /* See comment in uhci_remove_hs_ctrl() */
1069 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1070 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1071 delay(UHCI_QH_REMOVE_DELAY);
1073 pqh = uhci_find_prev_qh(sc->sc_bulk_start, sqh);
1074 pqh->hlink = sqh->hlink;
1075 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1076 delay(UHCI_QH_REMOVE_DELAY);
1077 if (sc->sc_bulk_end == sqh)
1078 sc->sc_bulk_end = pqh;
1081 static int uhci_intr1(uhci_softc_t *);
1084 uhci_intr(void *arg)
1086 uhci_softc_t *sc = arg;
1088 if (sc->sc_dying || (sc->sc_flags & UHCI_SCFLG_DONEINIT) == 0)
1089 return (0);
1091 DPRINTFN(15,("uhci_intr: real interrupt\n"));
1092 if (sc->sc_bus.use_polling) {
1093 #ifdef DIAGNOSTIC
1094 DPRINTFN(16, ("uhci_intr: ignored interrupt while polling\n"));
1095 #endif
1096 return (0);
1099 return (uhci_intr1(sc));
1103 uhci_intr1(uhci_softc_t *sc)
1106 int status;
1107 int ack;
1110 * It can happen that an interrupt will be delivered to
1111 * us before the device has been fully attached and the
1112 * softc struct has been configured. Usually this happens
1113 * when kldloading the USB support as a module after the
1114 * system has been booted. If we detect this condition,
1115 * we need to squelch the unwanted interrupts until we're
1116 * ready for them.
1118 if (sc->sc_bus.bdev == NULL) {
1119 UWRITE2(sc, UHCI_STS, 0xFFFF); /* ack pending interrupts */
1120 uhci_run(sc, 0); /* stop the controller */
1121 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
1122 return(0);
1125 #ifdef USB_DEBUG
1126 if (uhcidebug > 15) {
1127 DPRINTF(("%s: uhci_intr1\n", device_get_nameunit(sc->sc_bus.bdev)));
1128 uhci_dumpregs(sc);
1130 #endif
1131 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1132 if (status == 0) /* The interrupt was not for us. */
1133 return (0);
1135 if (sc->sc_suspend != PWR_RESUME) {
1136 kprintf("%s: interrupt while not operating ignored\n",
1137 device_get_nameunit(sc->sc_bus.bdev));
1138 UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */
1139 return (0);
1142 ack = 0;
1143 if (status & UHCI_STS_USBINT)
1144 ack |= UHCI_STS_USBINT;
1145 if (status & UHCI_STS_USBEI)
1146 ack |= UHCI_STS_USBEI;
1147 if (status & UHCI_STS_RD) {
1148 ack |= UHCI_STS_RD;
1149 #ifdef USB_DEBUG
1150 kprintf("%s: resume detect\n", device_get_nameunit(sc->sc_bus.bdev));
1151 #endif
1153 if (status & UHCI_STS_HSE) {
1154 ack |= UHCI_STS_HSE;
1155 kprintf("%s: host system error\n", device_get_nameunit(sc->sc_bus.bdev));
1157 if (status & UHCI_STS_HCPE) {
1158 ack |= UHCI_STS_HCPE;
1159 kprintf("%s: host controller process error\n",
1160 device_get_nameunit(sc->sc_bus.bdev));
1162 if (status & UHCI_STS_HCH) {
1163 /* no acknowledge needed */
1164 if (!sc->sc_dying) {
1165 kprintf("%s: host controller halted\n",
1166 device_get_nameunit(sc->sc_bus.bdev));
1167 #ifdef USB_DEBUG
1168 uhci_dump_all(sc);
1169 #endif
1171 sc->sc_dying = 1;
1174 if (!ack)
1175 return (0); /* nothing to acknowledge */
1176 UWRITE2(sc, UHCI_STS, ack); /* acknowledge the ints */
1178 sc->sc_bus.no_intrs++;
1179 usb_schedsoftintr(&sc->sc_bus);
1181 DPRINTFN(15, ("%s: uhci_intr: exit\n", device_get_nameunit(sc->sc_bus.bdev)));
1183 return (1);
1186 void
1187 uhci_softintr(void *v)
1189 uhci_softc_t *sc = v;
1190 uhci_intr_info_t *ii, *nextii;
1192 DPRINTFN(10,("%s: uhci_softintr (%d)\n", device_get_nameunit(sc->sc_bus.bdev),
1193 sc->sc_bus.intr_context));
1195 sc->sc_bus.intr_context++;
1198 * Interrupts on UHCI really suck. When the host controller
1199 * interrupts because a transfer is completed there is no
1200 * way of knowing which transfer it was. You can scan down
1201 * the TDs and QHs of the previous frame to limit the search,
1202 * but that assumes that the interrupt was not delayed by more
1203 * than 1 ms, which may not always be true (e.g. after debug
1204 * output on a slow console).
1205 * We scan all interrupt descriptors to see if any have
1206 * completed.
1208 LIST_FOREACH_MUTABLE(ii, &sc->sc_intrhead, list, nextii)
1209 uhci_check_intr(sc, ii);
1211 #ifdef USB_USE_SOFTINTR
1212 if (sc->sc_softwake) {
1213 sc->sc_softwake = 0;
1214 wakeup(&sc->sc_softwake);
1216 #endif /* USB_USE_SOFTINTR */
1218 sc->sc_bus.intr_context--;
1221 /* Check for an interrupt. */
1222 void
1223 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
1225 uhci_soft_td_t *std, *lstd;
1226 u_int32_t status;
1228 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
1229 #ifdef DIAGNOSTIC
1230 if (ii == NULL) {
1231 kprintf("uhci_check_intr: no ii? %p\n", ii);
1232 return;
1234 #endif
1235 if (ii->xfer->status == USBD_CANCELLED ||
1236 ii->xfer->status == USBD_TIMEOUT) {
1237 DPRINTF(("uhci_check_intr: aborted xfer=%p\n", ii->xfer));
1238 return;
1241 if (ii->stdstart == NULL)
1242 return;
1243 lstd = ii->stdend;
1244 #ifdef DIAGNOSTIC
1245 if (lstd == NULL) {
1246 kprintf("uhci_check_intr: std==0\n");
1247 return;
1249 #endif
1251 * If the last TD is still active we need to check whether there
1252 * is an error somewhere in the middle, or whether there was a
1253 * short packet (SPD and not ACTIVE).
1255 if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) {
1256 DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii));
1257 for (std = ii->stdstart; std != lstd; std = std->link.std) {
1258 status = le32toh(std->td.td_status);
1259 /* If there's an active TD the xfer isn't done. */
1260 if (status & UHCI_TD_ACTIVE)
1261 break;
1262 /* Any kind of error makes the xfer done. */
1263 if (status & UHCI_TD_STALLED)
1264 goto done;
1265 /* We want short packets, and it is short: it's done */
1266 if ((status & UHCI_TD_SPD) &&
1267 UHCI_TD_GET_ACTLEN(status) <
1268 UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token))) {
1269 goto done;
1272 DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
1273 ii, ii->stdstart));
1274 return;
1276 done:
1277 DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii));
1278 callout_stop(&ii->xfer->timeout_handle);
1279 usb_rem_task(ii->xfer->pipe->device, &UXFER(ii->xfer)->abort_task);
1280 uhci_idone(ii);
1283 /* Called from a critical section. */
1284 void
1285 uhci_idone(uhci_intr_info_t *ii)
1287 usbd_xfer_handle xfer = ii->xfer;
1288 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1289 uhci_soft_td_t *std;
1290 u_int32_t status = 0, nstatus;
1291 int actlen;
1293 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii));
1294 #ifdef DIAGNOSTIC
1296 crit_enter();
1297 if (ii->isdone) {
1298 crit_exit();
1299 #ifdef USB_DEBUG
1300 kprintf("uhci_idone: ii is done!\n ");
1301 uhci_dump_ii(ii);
1302 #else
1303 kprintf("uhci_idone: ii=%p is done!\n", ii);
1304 #endif
1305 return;
1307 ii->isdone = 1;
1308 crit_exit();
1310 #endif
1312 if (xfer->nframes != 0) {
1313 /* Isoc transfer, do things differently. */
1314 uhci_soft_td_t **stds = upipe->u.iso.stds;
1315 int i, n, nframes, len;
1317 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1319 nframes = xfer->nframes;
1320 actlen = 0;
1321 n = UXFER(xfer)->curframe;
1322 for (i = 0; i < nframes; i++) {
1323 std = stds[n];
1324 #ifdef USB_DEBUG
1325 if (uhcidebug > 5) {
1326 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1327 uhci_dump_td(std);
1329 #endif
1330 if (++n >= UHCI_VFRAMELIST_COUNT)
1331 n = 0;
1332 status = le32toh(std->td.td_status);
1333 len = UHCI_TD_GET_ACTLEN(status);
1334 xfer->frlengths[i] = len;
1335 actlen += len;
1337 upipe->u.iso.inuse -= nframes;
1338 xfer->actlen = actlen;
1339 xfer->status = USBD_NORMAL_COMPLETION;
1340 goto end;
1343 #ifdef USB_DEBUG
1344 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1345 ii, xfer, upipe));
1346 if (uhcidebug > 10)
1347 uhci_dump_tds(ii->stdstart);
1348 #endif
1350 /* The transfer is done, compute actual length and status. */
1351 actlen = 0;
1352 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1353 nstatus = le32toh(std->td.td_status);
1354 if (nstatus & UHCI_TD_ACTIVE)
1355 break;
1357 status = nstatus;
1358 if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
1359 UHCI_TD_PID_SETUP) {
1360 actlen += UHCI_TD_GET_ACTLEN(status);
1361 } else {
1363 * UHCI will report CRCTO in addition to a STALL or NAK
1364 * for a SETUP transaction. See section 3.2.2, "TD
1365 * CONTROL AND STATUS".
1367 if (status & (UHCI_TD_STALLED | UHCI_TD_NAK))
1368 status &= ~UHCI_TD_CRCTO;
1372 /* If there are left over TDs we need to update the toggle. */
1373 if (std != NULL)
1374 upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
1376 status &= UHCI_TD_ERROR;
1377 DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n",
1378 actlen, status));
1379 xfer->actlen = actlen;
1380 if (status != 0) {
1381 #ifdef USB_DEBUG
1382 char sbuf[128];
1384 bitmask_snprintf((u_int32_t)status,
1385 "\20\22BITSTUFF\23CRCTO\24NAK\25"
1386 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
1387 sbuf, sizeof(sbuf));
1389 DPRINTFN((status == UHCI_TD_STALLED)*10,
1390 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1391 "status 0x%s\n",
1392 xfer->pipe->device->address,
1393 xfer->pipe->endpoint->edesc->bEndpointAddress,
1394 sbuf));
1395 #endif
1396 if (status == UHCI_TD_STALLED)
1397 xfer->status = USBD_STALLED;
1398 else
1399 xfer->status = USBD_IOERROR; /* more info XXX */
1400 } else {
1401 xfer->status = USBD_NORMAL_COMPLETION;
1404 end:
1405 usb_transfer_complete(xfer);
1406 DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii));
1410 * Called when a request does not complete.
1412 void
1413 uhci_timeout(void *addr)
1415 uhci_intr_info_t *ii = addr;
1416 struct uhci_xfer *uxfer = UXFER(ii->xfer);
1417 struct uhci_pipe *upipe = (struct uhci_pipe *)uxfer->xfer.pipe;
1418 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1420 DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer));
1422 if (sc->sc_dying) {
1423 uhci_abort_xfer(&uxfer->xfer, USBD_TIMEOUT);
1424 return;
1427 /* Execute the abort in a process context. */
1428 usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task,
1429 USB_TASKQ_HC);
1432 void
1433 uhci_timeout_task(void *addr)
1435 usbd_xfer_handle xfer = addr;
1437 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer));
1439 crit_enter();
1440 uhci_abort_xfer(xfer, USBD_TIMEOUT);
1441 crit_exit();
1445 * Wait here until controller claims to have an interrupt.
1446 * Then call uhci_intr and return. Use timeout to avoid waiting
1447 * too long.
1448 * Only used during boot when interrupts are not enabled yet.
1450 void
1451 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
1453 int timo = xfer->timeout;
1454 uhci_intr_info_t *ii;
1456 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1458 xfer->status = USBD_IN_PROGRESS;
1459 for (; timo >= 0; timo--) {
1460 usb_delay_ms(&sc->sc_bus, 1);
1461 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1462 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1463 uhci_intr1(sc);
1464 if (xfer->status != USBD_IN_PROGRESS)
1465 return;
1468 /* Timeout */
1469 DPRINTF(("uhci_waitintr: timeout\n"));
1470 for (ii = LIST_FIRST(&sc->sc_intrhead);
1471 ii != NULL && ii->xfer != xfer;
1472 ii = LIST_NEXT(ii, list))
1474 #ifdef DIAGNOSTIC
1475 if (ii == NULL)
1476 panic("uhci_waitintr: lost intr_info");
1477 #endif
1478 uhci_idone(ii);
1481 void
1482 uhci_poll(struct usbd_bus *bus)
1484 uhci_softc_t *sc = (uhci_softc_t *)bus;
1486 if (UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS)
1487 uhci_intr1(sc);
1490 void
1491 uhci_reset(uhci_softc_t *sc)
1493 int n;
1495 UHCICMD(sc, UHCI_CMD_HCRESET);
1496 /* The reset bit goes low when the controller is done. */
1497 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1498 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1499 usb_delay_ms(&sc->sc_bus, 1);
1500 if (n >= UHCI_RESET_TIMEOUT)
1501 kprintf("%s: controller did not reset\n",
1502 device_get_nameunit(sc->sc_bus.bdev));
1505 usbd_status
1506 uhci_run(uhci_softc_t *sc, int run)
1508 int n, running;
1509 u_int16_t cmd;
1511 run = run != 0;
1512 crit_enter();
1513 DPRINTF(("uhci_run: setting run=%d\n", run));
1514 cmd = UREAD2(sc, UHCI_CMD);
1515 if (run)
1516 cmd |= UHCI_CMD_RS;
1517 else
1518 cmd &= ~UHCI_CMD_RS;
1519 UHCICMD(sc, cmd);
1520 for(n = 0; n < 10; n++) {
1521 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1522 /* return when we've entered the state we want */
1523 if (run == running) {
1524 crit_exit();
1525 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1526 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1527 return (USBD_NORMAL_COMPLETION);
1529 usb_delay_ms(&sc->sc_bus, 1);
1531 crit_exit();
1532 kprintf("%s: cannot %s\n", device_get_nameunit(sc->sc_bus.bdev),
1533 run ? "start" : "stop");
1534 return (USBD_IOERROR);
1538 * Memory management routines.
1539 * uhci_alloc_std allocates TDs
1540 * uhci_alloc_sqh allocates QHs
1541 * These two routines do their own free list management,
1542 * partly for speed, partly because allocating DMAable memory
1543 * has page size granularaity so much memory would be wasted if
1544 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1547 uhci_soft_td_t *
1548 uhci_alloc_std(uhci_softc_t *sc)
1550 uhci_soft_td_t *std;
1551 usbd_status err;
1552 int i, offs;
1553 usb_dma_t dma;
1555 if (sc->sc_freetds == NULL) {
1556 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1557 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1558 UHCI_TD_ALIGN, &dma);
1559 if (err)
1560 return (0);
1561 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1562 offs = i * UHCI_STD_SIZE;
1563 std = KERNADDR(&dma, offs);
1564 std->physaddr = DMAADDR(&dma, offs);
1565 std->link.std = sc->sc_freetds;
1566 sc->sc_freetds = std;
1569 std = sc->sc_freetds;
1570 sc->sc_freetds = std->link.std;
1571 memset(&std->td, 0, sizeof(uhci_td_t));
1572 return std;
1575 void
1576 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
1578 #ifdef DIAGNOSTIC
1579 #define TD_IS_FREE 0x12345678
1580 if (le32toh(std->td.td_token) == TD_IS_FREE) {
1581 kprintf("uhci_free_std: freeing free TD %p\n", std);
1582 return;
1584 std->td.td_token = htole32(TD_IS_FREE);
1585 #endif
1586 std->link.std = sc->sc_freetds;
1587 sc->sc_freetds = std;
1590 uhci_soft_qh_t *
1591 uhci_alloc_sqh(uhci_softc_t *sc)
1593 uhci_soft_qh_t *sqh;
1594 usbd_status err;
1595 int i, offs;
1596 usb_dma_t dma;
1598 if (sc->sc_freeqhs == NULL) {
1599 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1600 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1601 UHCI_QH_ALIGN, &dma);
1602 if (err)
1603 return (0);
1604 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1605 offs = i * UHCI_SQH_SIZE;
1606 sqh = KERNADDR(&dma, offs);
1607 sqh->physaddr = DMAADDR(&dma, offs);
1608 sqh->hlink = sc->sc_freeqhs;
1609 sc->sc_freeqhs = sqh;
1612 sqh = sc->sc_freeqhs;
1613 sc->sc_freeqhs = sqh->hlink;
1614 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1615 return (sqh);
1618 void
1619 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1621 sqh->hlink = sc->sc_freeqhs;
1622 sc->sc_freeqhs = sqh;
1625 void
1626 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
1627 uhci_soft_td_t *stdend)
1629 uhci_soft_td_t *p;
1631 for (; std != stdend; std = p) {
1632 p = std->link.std;
1633 uhci_free_std(sc, std);
1637 usbd_status
1638 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
1639 int rd, u_int16_t flags, usb_dma_t *dma,
1640 uhci_soft_td_t **sp, uhci_soft_td_t **ep)
1642 uhci_soft_td_t *p, *lastp;
1643 uhci_physaddr_t lastlink;
1644 int i, ntd, l, tog, maxp;
1645 u_int32_t status;
1646 int addr = upipe->pipe.device->address;
1647 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1649 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
1650 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
1651 upipe->pipe.device->speed, flags));
1652 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1653 if (maxp == 0) {
1654 kprintf("uhci_alloc_std_chain: maxp=0\n");
1655 return (USBD_INVAL);
1657 ntd = (len + maxp - 1) / maxp;
1658 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
1659 ntd++;
1660 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1661 if (ntd == 0) {
1662 *sp = *ep = 0;
1663 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
1664 return (USBD_NORMAL_COMPLETION);
1666 tog = upipe->nexttoggle;
1667 if (ntd % 2 == 0)
1668 tog ^= 1;
1669 upipe->nexttoggle = tog ^ 1;
1670 lastp = NULL;
1671 lastlink = UHCI_PTR_T;
1672 ntd--;
1673 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1674 if (upipe->pipe.device->speed == USB_SPEED_LOW)
1675 status |= UHCI_TD_LS;
1676 if (flags & USBD_SHORT_XFER_OK)
1677 status |= UHCI_TD_SPD;
1678 for (i = ntd; i >= 0; i--) {
1679 p = uhci_alloc_std(sc);
1680 if (p == NULL) {
1681 uhci_free_std_chain(sc, lastp, NULL);
1682 return (USBD_NOMEM);
1684 p->link.std = lastp;
1685 p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD);
1686 lastp = p;
1687 lastlink = p->physaddr;
1688 p->td.td_status = htole32(status);
1689 if (i == ntd) {
1690 /* last TD */
1691 l = len % maxp;
1692 if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
1693 l = maxp;
1694 *ep = p;
1695 } else
1696 l = maxp;
1697 p->td.td_token =
1698 htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1699 UHCI_TD_OUT(l, endpt, addr, tog));
1700 p->td.td_buffer = htole32(DMAADDR(dma, i * maxp));
1701 tog ^= 1;
1703 *sp = lastp;
1704 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1705 upipe->nexttoggle));
1706 return (USBD_NORMAL_COMPLETION);
1709 void
1710 uhci_device_clear_toggle(usbd_pipe_handle pipe)
1712 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1713 upipe->nexttoggle = 0;
1716 void
1717 uhci_noop(usbd_pipe_handle pipe)
1721 usbd_status
1722 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
1724 usbd_status err;
1726 /* Insert last in queue. */
1727 err = usb_insert_transfer(xfer);
1728 if (err)
1729 return (err);
1732 * Pipe isn't running (otherwise err would be USBD_INPROG),
1733 * so start it first.
1735 return (uhci_device_bulk_start(STAILQ_FIRST(&xfer->pipe->queue)));
1738 usbd_status
1739 uhci_device_bulk_start(usbd_xfer_handle xfer)
1741 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1742 usbd_device_handle dev = upipe->pipe.device;
1743 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1744 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1745 uhci_soft_td_t *data, *dataend;
1746 uhci_soft_qh_t *sqh;
1747 usbd_status err;
1748 int len, isread, endpt;
1750 DPRINTFN(3, ("uhci_device_bulk_start: xfer=%p len=%d flags=%d ii=%p\n",
1751 xfer, xfer->length, xfer->flags, ii));
1753 if (sc->sc_dying)
1754 return (USBD_IOERROR);
1756 #ifdef DIAGNOSTIC
1757 if (xfer->rqflags & URQ_REQUEST)
1758 panic("uhci_device_bulk_transfer: a request");
1759 #endif
1761 len = xfer->length;
1762 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1763 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1764 sqh = upipe->u.bulk.sqh;
1766 upipe->u.bulk.isread = isread;
1767 upipe->u.bulk.length = len;
1769 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
1770 &xfer->dmabuf, &data, &dataend);
1771 if (err)
1772 return (err);
1773 dataend->td.td_status |= htole32(UHCI_TD_IOC);
1775 #ifdef USB_DEBUG
1776 if (uhcidebug > 8) {
1777 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1778 uhci_dump_tds(data);
1780 #endif
1782 /* Set up interrupt info. */
1783 ii->xfer = xfer;
1784 ii->stdstart = data;
1785 ii->stdend = dataend;
1786 #ifdef DIAGNOSTIC
1787 if (!ii->isdone) {
1788 kprintf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
1790 ii->isdone = 0;
1791 #endif
1793 sqh->elink = data;
1794 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
1796 crit_enter();
1797 uhci_add_bulk(sc, sqh);
1798 uhci_add_intr_info(sc, ii);
1800 if (xfer->timeout && !sc->sc_bus.use_polling) {
1801 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1802 uhci_timeout, ii);
1804 xfer->status = USBD_IN_PROGRESS;
1805 crit_exit();
1807 #ifdef USB_DEBUG
1808 if (uhcidebug > 10) {
1809 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1810 uhci_dump_tds(data);
1812 #endif
1814 if (sc->sc_bus.use_polling)
1815 uhci_waitintr(sc, xfer);
1817 return (USBD_IN_PROGRESS);
1820 /* Abort a device bulk request. */
1821 void
1822 uhci_device_bulk_abort(usbd_xfer_handle xfer)
1824 DPRINTF(("uhci_device_bulk_abort:\n"));
1825 uhci_abort_xfer(xfer, USBD_CANCELLED);
1829 * Abort a device request.
1830 * If this routine is called from a critical section. It guarantees that
1831 * the request will be removed from the hardware scheduling and that
1832 * the callback for it will be called with USBD_CANCELLED status.
1833 * It's impossible to guarantee that the requested transfer will not
1834 * have happened since the hardware runs concurrently.
1835 * If the transaction has already happened we rely on the ordinary
1836 * interrupt processing to process it.
1838 void
1839 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1841 struct uhci_xfer *uxfer = UXFER(xfer);
1842 uhci_intr_info_t *ii = &uxfer->iinfo;
1843 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1844 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1845 uhci_soft_td_t *std;
1847 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1849 if (sc->sc_dying) {
1850 /* If we're dying, just do the software part. */
1851 crit_enter();
1852 xfer->status = status; /* make software ignore it */
1853 callout_stop(&xfer->timeout_handle);
1854 usb_rem_task(xfer->pipe->device, &UXFER(xfer)->abort_task);
1855 usb_transfer_complete(xfer);
1856 crit_exit();
1857 return;
1860 if (xfer->device->bus->intr_context /* || !curproc REMOVED DFly */)
1861 panic("uhci_abort_xfer: not in process context");
1864 * If an abort is already in progress then just wait for it to
1865 * complete and return.
1867 if (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTING) {
1868 DPRINTFN(2, ("uhci_abort_xfer: already aborting\n"));
1869 /* No need to wait if we're aborting from a timeout. */
1870 if (status == USBD_TIMEOUT)
1871 return;
1872 /* Override the status which might be USBD_TIMEOUT. */
1873 xfer->status = status;
1874 DPRINTFN(2, ("uhci_abort_xfer: waiting for abort to finish\n"));
1875 uxfer->uhci_xfer_flags |= UHCI_XFER_ABORTWAIT;
1876 while (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTING)
1877 tsleep(&uxfer->uhci_xfer_flags, 0, "uhciaw", 0);
1878 return;
1882 * Step 1: Make interrupt routine and hardware ignore xfer.
1884 crit_enter();
1885 uxfer->uhci_xfer_flags |= UHCI_XFER_ABORTING;
1886 xfer->status = status; /* make software ignore it */
1887 callout_stop(&xfer->timeout_handle);
1888 usb_rem_task(xfer->pipe->device, &UXFER(xfer)->abort_task);
1889 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii));
1890 for (std = ii->stdstart; std != NULL; std = std->link.std)
1891 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1892 crit_exit();
1895 * Step 2: Wait until we know hardware has finished any possible
1896 * use of the xfer. Also make sure the soft interrupt routine
1897 * has run.
1899 usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */
1900 crit_enter();
1901 #ifdef USB_USE_SOFTINTR
1902 sc->sc_softwake = 1;
1903 #endif /* USB_USE_SOFTINTR */
1904 usb_schedsoftintr(&sc->sc_bus);
1905 #ifdef USB_USE_SOFTINTR
1906 DPRINTFN(1,("uhci_abort_xfer: tsleep\n"));
1907 tsleep(&sc->sc_softwake, 0, "uhciab", 0);
1908 #endif /* USB_USE_SOFTINTR */
1911 * Step 3: Execute callback.
1913 DPRINTFN(1,("uhci_abort_xfer: callback\n"));
1914 #ifdef DIAGNOSTIC
1915 ii->isdone = 1;
1916 #endif
1917 /* Do the wakeup first to avoid touching the xfer after the callback. */
1918 uxfer->uhci_xfer_flags &= ~UHCI_XFER_ABORTING;
1919 if (uxfer->uhci_xfer_flags & UHCI_XFER_ABORTWAIT) {
1920 uxfer->uhci_xfer_flags &= ~UHCI_XFER_ABORTWAIT;
1921 wakeup(&uxfer->uhci_xfer_flags);
1923 usb_transfer_complete(xfer);
1924 crit_exit();
1927 /* Close a device bulk pipe. */
1928 void
1929 uhci_device_bulk_close(usbd_pipe_handle pipe)
1931 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1932 usbd_device_handle dev = upipe->pipe.device;
1933 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1935 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1936 pipe->endpoint->savedtoggle = upipe->nexttoggle;
1939 usbd_status
1940 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
1942 usbd_status err;
1944 /* Insert last in queue. */
1945 err = usb_insert_transfer(xfer);
1946 if (err)
1947 return (err);
1950 * Pipe isn't running (otherwise err would be USBD_INPROG),
1951 * so start it first.
1953 return (uhci_device_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
1956 usbd_status
1957 uhci_device_ctrl_start(usbd_xfer_handle xfer)
1959 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
1960 usbd_status err;
1962 if (sc->sc_dying)
1963 return (USBD_IOERROR);
1965 #ifdef DIAGNOSTIC
1966 if (!(xfer->rqflags & URQ_REQUEST))
1967 panic("uhci_device_ctrl_transfer: not a request");
1968 #endif
1970 err = uhci_device_request(xfer);
1971 if (err)
1972 return (err);
1974 if (sc->sc_bus.use_polling)
1975 uhci_waitintr(sc, xfer);
1976 return (USBD_IN_PROGRESS);
1979 usbd_status
1980 uhci_device_intr_transfer(usbd_xfer_handle xfer)
1982 usbd_status err;
1984 /* Insert last in queue. */
1985 err = usb_insert_transfer(xfer);
1986 if (err)
1987 return (err);
1990 * Pipe isn't running (otherwise err would be USBD_INPROG),
1991 * so start it first.
1993 return (uhci_device_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
1996 usbd_status
1997 uhci_device_intr_start(usbd_xfer_handle xfer)
1999 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2000 usbd_device_handle dev = upipe->pipe.device;
2001 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2002 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2003 uhci_soft_td_t *data, *dataend;
2004 uhci_soft_qh_t *sqh;
2005 usbd_status err;
2006 int isread, endpt;
2007 int i;
2009 if (sc->sc_dying)
2010 return (USBD_IOERROR);
2012 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
2013 xfer, xfer->length, xfer->flags));
2015 #ifdef DIAGNOSTIC
2016 if (xfer->rqflags & URQ_REQUEST)
2017 panic("uhci_device_intr_transfer: a request");
2018 #endif
2020 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2021 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2022 sqh = upipe->u.bulk.sqh;
2024 upipe->u.intr.isread = isread;
2026 err = uhci_alloc_std_chain(upipe, sc, xfer->length, isread,
2027 xfer->flags, &xfer->dmabuf, &data,
2028 &dataend);
2029 if (err)
2030 return (err);
2031 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2033 #ifdef USB_DEBUG
2034 if (uhcidebug > 10) {
2035 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
2036 uhci_dump_tds(data);
2037 uhci_dump_qh(upipe->u.intr.qhs[0]);
2039 #endif
2041 crit_enter();
2042 /* Set up interrupt info. */
2043 ii->xfer = xfer;
2044 ii->stdstart = data;
2045 ii->stdend = dataend;
2046 #ifdef DIAGNOSTIC
2047 if (!ii->isdone) {
2048 kprintf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
2050 ii->isdone = 0;
2051 #endif
2053 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
2054 upipe->u.intr.qhs[0]));
2055 for (i = 0; i < upipe->u.intr.npoll; i++) {
2056 sqh = upipe->u.intr.qhs[i];
2057 sqh->elink = data;
2058 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2060 uhci_add_intr_info(sc, ii);
2061 xfer->status = USBD_IN_PROGRESS;
2062 crit_exit();
2064 #ifdef USB_DEBUG
2065 if (uhcidebug > 10) {
2066 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2067 uhci_dump_tds(data);
2068 uhci_dump_qh(upipe->u.intr.qhs[0]);
2070 #endif
2072 return (USBD_IN_PROGRESS);
2075 /* Abort a device control request. */
2076 void
2077 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
2079 DPRINTF(("uhci_device_ctrl_abort:\n"));
2080 uhci_abort_xfer(xfer, USBD_CANCELLED);
2083 /* Close a device control pipe. */
2084 void
2085 uhci_device_ctrl_close(usbd_pipe_handle pipe)
2089 /* Abort a device interrupt request. */
2090 void
2091 uhci_device_intr_abort(usbd_xfer_handle xfer)
2093 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
2094 if (xfer->pipe->intrxfer == xfer) {
2095 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2096 xfer->pipe->intrxfer = NULL;
2098 uhci_abort_xfer(xfer, USBD_CANCELLED);
2101 /* Close a device interrupt pipe. */
2102 void
2103 uhci_device_intr_close(usbd_pipe_handle pipe)
2105 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2106 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2107 int i, npoll;
2109 /* Unlink descriptors from controller data structures. */
2110 npoll = upipe->u.intr.npoll;
2111 crit_enter();
2112 for (i = 0; i < npoll; i++)
2113 uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
2114 crit_exit();
2117 * We now have to wait for any activity on the physical
2118 * descriptors to stop.
2120 usb_delay_ms(&sc->sc_bus, 2);
2122 for(i = 0; i < npoll; i++)
2123 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
2124 kfree(upipe->u.intr.qhs, M_USBHC);
2126 /* XXX free other resources */
2129 usbd_status
2130 uhci_device_request(usbd_xfer_handle xfer)
2132 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2133 usb_device_request_t *req = &xfer->request;
2134 usbd_device_handle dev = upipe->pipe.device;
2135 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2136 int addr = dev->address;
2137 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2138 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2139 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
2140 uhci_soft_qh_t *sqh;
2141 int len;
2142 u_int32_t ls;
2143 usbd_status err;
2144 int isread;
2146 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2147 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2148 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2149 UGETW(req->wIndex), UGETW(req->wLength),
2150 addr, endpt));
2152 ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0;
2153 isread = req->bmRequestType & UT_READ;
2154 len = UGETW(req->wLength);
2156 setup = upipe->u.ctl.setup;
2157 stat = upipe->u.ctl.stat;
2158 sqh = upipe->u.ctl.sqh;
2160 /* Set up data transaction */
2161 if (len != 0) {
2162 upipe->nexttoggle = 1;
2163 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
2164 &xfer->dmabuf, &data, &dataend);
2165 if (err)
2166 return (err);
2167 next = data;
2168 dataend->link.std = stat;
2169 dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2170 } else {
2171 next = stat;
2173 upipe->u.ctl.length = len;
2175 memcpy(KERNADDR(&upipe->u.ctl.reqdma, 0), req, sizeof *req);
2177 setup->link.std = next;
2178 setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2179 setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2180 UHCI_TD_ACTIVE);
2181 setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
2182 setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma, 0));
2183 stat->link.std = NULL;
2184 stat->td.td_link = htole32(UHCI_PTR_T);
2185 stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2186 UHCI_TD_ACTIVE | UHCI_TD_IOC);
2188 stat->td.td_token =
2189 htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
2190 UHCI_TD_IN (0, endpt, addr, 1));
2191 stat->td.td_buffer = htole32(0);
2193 #ifdef USB_DEBUG
2194 if (uhcidebug > 10) {
2195 DPRINTF(("uhci_device_request: before transfer\n"));
2196 uhci_dump_tds(setup);
2198 #endif
2200 /* Set up interrupt info. */
2201 ii->xfer = xfer;
2202 ii->stdstart = setup;
2203 ii->stdend = stat;
2204 #ifdef DIAGNOSTIC
2205 if (!ii->isdone) {
2206 kprintf("uhci_device_request: not done, ii=%p\n", ii);
2208 ii->isdone = 0;
2209 #endif
2211 sqh->elink = setup;
2212 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2214 crit_enter();
2215 if (dev->speed == USB_SPEED_LOW)
2216 uhci_add_ls_ctrl(sc, sqh);
2217 else
2218 uhci_add_hs_ctrl(sc, sqh);
2219 uhci_add_intr_info(sc, ii);
2220 #ifdef USB_DEBUG
2221 if (uhcidebug > 12) {
2222 uhci_soft_td_t *std;
2223 uhci_soft_qh_t *xqh;
2224 uhci_soft_qh_t *sxqh;
2225 int maxqh = 0;
2226 uhci_physaddr_t link;
2227 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
2228 for (std = sc->sc_vframes[0].htd, link = 0;
2229 (link & UHCI_PTR_QH) == 0;
2230 std = std->link.std) {
2231 link = le32toh(std->td.td_link);
2232 uhci_dump_td(std);
2234 sxqh = (uhci_soft_qh_t *)std;
2235 uhci_dump_qh(sxqh);
2236 for (xqh = sxqh;
2237 xqh != NULL;
2238 xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
2239 xqh->hlink == xqh ? NULL : xqh->hlink)) {
2240 uhci_dump_qh(xqh);
2242 DPRINTF(("Enqueued QH:\n"));
2243 uhci_dump_qh(sqh);
2244 uhci_dump_tds(sqh->elink);
2246 #endif
2247 if (xfer->timeout && !sc->sc_bus.use_polling) {
2248 callout_reset(&xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2249 uhci_timeout, ii);
2251 xfer->status = USBD_IN_PROGRESS;
2252 crit_exit();
2254 return (USBD_NORMAL_COMPLETION);
2257 usbd_status
2258 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
2260 usbd_status err;
2262 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
2264 /* Put it on our queue, */
2265 err = usb_insert_transfer(xfer);
2267 /* bail out on error, */
2268 if (err && err != USBD_IN_PROGRESS)
2269 return (err);
2271 /* XXX should check inuse here */
2273 /* insert into schedule, */
2274 uhci_device_isoc_enter(xfer);
2276 /* and start if the pipe wasn't running */
2277 if (!err)
2278 uhci_device_isoc_start(STAILQ_FIRST(&xfer->pipe->queue));
2280 return (err);
2283 void
2284 uhci_device_isoc_enter(usbd_xfer_handle xfer)
2286 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2287 usbd_device_handle dev = upipe->pipe.device;
2288 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2289 struct iso *iso = &upipe->u.iso;
2290 uhci_soft_td_t *std;
2291 u_int32_t buf, len, status;
2292 int i, next, nframes;
2294 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2295 "nframes=%d\n",
2296 iso->inuse, iso->next, xfer, xfer->nframes));
2298 if (sc->sc_dying)
2299 return;
2301 if (xfer->status == USBD_IN_PROGRESS) {
2302 /* This request has already been entered into the frame list */
2303 kprintf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
2304 /* XXX */
2307 #ifdef DIAGNOSTIC
2308 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
2309 kprintf("uhci_device_isoc_enter: overflow!\n");
2310 #endif
2312 next = iso->next;
2313 if (next == -1) {
2314 /* Not in use yet, schedule it a few frames ahead. */
2315 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2316 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2319 xfer->status = USBD_IN_PROGRESS;
2320 UXFER(xfer)->curframe = next;
2322 buf = DMAADDR(&xfer->dmabuf, 0);
2323 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2324 UHCI_TD_ACTIVE |
2325 UHCI_TD_IOS);
2326 nframes = xfer->nframes;
2327 crit_enter();
2328 for (i = 0; i < nframes; i++) {
2329 std = iso->stds[next];
2330 if (++next >= UHCI_VFRAMELIST_COUNT)
2331 next = 0;
2332 len = xfer->frlengths[i];
2333 std->td.td_buffer = htole32(buf);
2334 if (i == nframes - 1)
2335 status |= UHCI_TD_IOC;
2336 std->td.td_status = htole32(status);
2337 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2338 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
2339 #ifdef USB_DEBUG
2340 if (uhcidebug > 5) {
2341 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2342 uhci_dump_td(std);
2344 #endif
2345 buf += len;
2347 iso->next = next;
2348 iso->inuse += xfer->nframes;
2350 crit_exit();
2353 usbd_status
2354 uhci_device_isoc_start(usbd_xfer_handle xfer)
2356 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2357 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2358 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2359 uhci_soft_td_t *end;
2360 int i;
2362 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
2364 if (sc->sc_dying)
2365 return (USBD_IOERROR);
2367 #ifdef DIAGNOSTIC
2368 if (xfer->status != USBD_IN_PROGRESS)
2369 kprintf("uhci_device_isoc_start: not in progress %p\n", xfer);
2370 #endif
2372 /* Find the last TD */
2373 i = UXFER(xfer)->curframe + xfer->nframes;
2374 if (i >= UHCI_VFRAMELIST_COUNT)
2375 i -= UHCI_VFRAMELIST_COUNT;
2376 end = upipe->u.iso.stds[i];
2378 #ifdef DIAGNOSTIC
2379 if (end == NULL) {
2380 kprintf("uhci_device_isoc_start: end == NULL\n");
2381 return (USBD_INVAL);
2383 #endif
2385 crit_enter();
2387 /* Set up interrupt info. */
2388 ii->xfer = xfer;
2389 ii->stdstart = end;
2390 ii->stdend = end;
2391 #ifdef DIAGNOSTIC
2392 if (!ii->isdone)
2393 kprintf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2394 ii->isdone = 0;
2395 #endif
2396 uhci_add_intr_info(sc, ii);
2398 crit_exit();
2400 return (USBD_IN_PROGRESS);
2403 void
2404 uhci_device_isoc_abort(usbd_xfer_handle xfer)
2406 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2407 uhci_soft_td_t **stds = upipe->u.iso.stds;
2408 uhci_soft_td_t *std;
2409 int i, n, nframes, maxlen, len;
2411 crit_enter();
2413 /* Transfer is already done. */
2414 if (xfer->status != USBD_NOT_STARTED &&
2415 xfer->status != USBD_IN_PROGRESS) {
2416 crit_exit();
2417 return;
2420 /* Give xfer the requested abort code. */
2421 xfer->status = USBD_CANCELLED;
2423 /* make hardware ignore it, */
2424 nframes = xfer->nframes;
2425 n = UXFER(xfer)->curframe;
2426 maxlen = 0;
2427 for (i = 0; i < nframes; i++) {
2428 std = stds[n];
2429 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2430 len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
2431 if (len > maxlen)
2432 maxlen = len;
2433 if (++n >= UHCI_VFRAMELIST_COUNT)
2434 n = 0;
2437 /* and wait until we are sure the hardware has finished. */
2438 delay(maxlen);
2440 #ifdef DIAGNOSTIC
2441 UXFER(xfer)->iinfo.isdone = 1;
2442 #endif
2443 /* Run callback and remove from interrupt list. */
2444 usb_transfer_complete(xfer);
2446 crit_exit();
2449 void
2450 uhci_device_isoc_close(usbd_pipe_handle pipe)
2452 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2453 usbd_device_handle dev = upipe->pipe.device;
2454 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2455 uhci_soft_td_t *std, *vstd;
2456 struct iso *iso;
2457 int i;
2460 * Make sure all TDs are marked as inactive.
2461 * Wait for completion.
2462 * Unschedule.
2463 * Deallocate.
2465 iso = &upipe->u.iso;
2467 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2468 iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
2469 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2471 crit_enter();
2472 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2473 std = iso->stds[i];
2474 for (vstd = sc->sc_vframes[i].htd;
2475 vstd != NULL && vstd->link.std != std;
2476 vstd = vstd->link.std)
2478 if (vstd == NULL) {
2479 /*panic*/
2480 kprintf("uhci_device_isoc_close: %p not found\n", std);
2481 crit_exit();
2482 return;
2484 vstd->link = std->link;
2485 vstd->td.td_link = std->td.td_link;
2486 uhci_free_std(sc, std);
2488 crit_exit();
2490 kfree(iso->stds, M_USBHC);
2493 usbd_status
2494 uhci_setup_isoc(usbd_pipe_handle pipe)
2496 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2497 usbd_device_handle dev = upipe->pipe.device;
2498 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2499 int addr = upipe->pipe.device->address;
2500 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2501 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2502 uhci_soft_td_t *std, *vstd;
2503 u_int32_t token;
2504 struct iso *iso;
2505 int i;
2507 iso = &upipe->u.iso;
2508 iso->stds = kmalloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2509 M_USBHC, M_INTWAIT);
2511 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2512 UHCI_TD_OUT(0, endpt, addr, 0);
2514 /* Allocate the TDs and mark as inactive; */
2515 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2516 std = uhci_alloc_std(sc);
2517 if (std == 0)
2518 goto bad;
2519 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
2520 std->td.td_token = htole32(token);
2521 iso->stds[i] = std;
2524 /* Insert TDs into schedule. */
2525 crit_enter();
2526 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2527 std = iso->stds[i];
2528 vstd = sc->sc_vframes[i].htd;
2529 std->link = vstd->link;
2530 std->td.td_link = vstd->td.td_link;
2531 vstd->link.std = std;
2532 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
2534 crit_exit();
2536 iso->next = -1;
2537 iso->inuse = 0;
2539 return (USBD_NORMAL_COMPLETION);
2541 bad:
2542 while (--i >= 0)
2543 uhci_free_std(sc, iso->stds[i]);
2544 kfree(iso->stds, M_USBHC);
2545 return (USBD_NOMEM);
2548 void
2549 uhci_device_isoc_done(usbd_xfer_handle xfer)
2551 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2553 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2555 if (ii->xfer != xfer)
2556 /* Not on interrupt list, ignore it. */
2557 return;
2559 if (!uhci_active_intr_info(ii))
2560 return;
2562 #ifdef DIAGNOSTIC
2563 if (xfer->busy_free != XFER_BUSY) {
2564 kprintf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
2565 xfer, xfer->busy_free);
2566 return;
2569 if (ii->stdend == NULL) {
2570 kprintf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2571 #ifdef USB_DEBUG
2572 uhci_dump_ii(ii);
2573 #endif
2574 return;
2576 #endif
2578 /* Turn off the interrupt since it is active even if the TD is not. */
2579 ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
2581 uhci_del_intr_info(ii); /* remove from active list */
2583 #ifdef DIAGNOSTIC
2584 if (ii->stdend == NULL) {
2585 kprintf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2586 #ifdef USB_DEBUG
2587 uhci_dump_ii(ii);
2588 #endif
2589 return;
2591 #endif
2594 void
2595 uhci_device_intr_done(usbd_xfer_handle xfer)
2597 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2598 uhci_softc_t *sc = ii->sc;
2599 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2600 uhci_soft_qh_t *sqh;
2601 int i, npoll;
2603 DPRINTFN(5, ("uhci_device_intr_done: length=%d\n", xfer->actlen));
2605 npoll = upipe->u.intr.npoll;
2606 for(i = 0; i < npoll; i++) {
2607 sqh = upipe->u.intr.qhs[i];
2608 sqh->elink = NULL;
2609 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2611 uhci_free_std_chain(sc, ii->stdstart, NULL);
2613 /* XXX Wasteful. */
2614 if (xfer->pipe->repeat) {
2615 uhci_soft_td_t *data, *dataend;
2617 DPRINTFN(5,("uhci_device_intr_done: requeueing\n"));
2619 /* This alloc cannot fail since we freed the chain above. */
2620 uhci_alloc_std_chain(upipe, sc, xfer->length,
2621 upipe->u.intr.isread, xfer->flags,
2622 &xfer->dmabuf, &data, &dataend);
2623 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2625 #ifdef USB_DEBUG
2626 if (uhcidebug > 10) {
2627 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2628 uhci_dump_tds(data);
2629 uhci_dump_qh(upipe->u.intr.qhs[0]);
2631 #endif
2633 ii->stdstart = data;
2634 ii->stdend = dataend;
2635 #ifdef DIAGNOSTIC
2636 if (!ii->isdone) {
2637 kprintf("uhci_device_intr_done: not done, ii=%p\n", ii);
2639 ii->isdone = 0;
2640 #endif
2641 for (i = 0; i < npoll; i++) {
2642 sqh = upipe->u.intr.qhs[i];
2643 sqh->elink = data;
2644 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2646 xfer->status = USBD_IN_PROGRESS;
2647 /* The ii is already on the examined list, just leave it. */
2648 } else {
2649 DPRINTFN(5,("uhci_device_intr_done: removing\n"));
2650 if (uhci_active_intr_info(ii))
2651 uhci_del_intr_info(ii);
2655 /* Deallocate request data structures */
2656 void
2657 uhci_device_ctrl_done(usbd_xfer_handle xfer)
2659 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2660 uhci_softc_t *sc = ii->sc;
2661 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2663 #ifdef DIAGNOSTIC
2664 if (!(xfer->rqflags & URQ_REQUEST))
2665 panic("uhci_device_ctrl_done: not a request");
2666 #endif
2668 if (!uhci_active_intr_info(ii))
2669 return;
2671 uhci_del_intr_info(ii); /* remove from active list */
2673 if (upipe->pipe.device->speed == USB_SPEED_LOW)
2674 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
2675 else
2676 uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
2678 if (upipe->u.ctl.length != 0)
2679 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2681 DPRINTFN(5, ("uhci_device_ctrl_done: length=%d\n", xfer->actlen));
2684 /* Deallocate request data structures */
2685 void
2686 uhci_device_bulk_done(usbd_xfer_handle xfer)
2688 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2689 uhci_softc_t *sc = ii->sc;
2690 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2692 DPRINTFN(5,("uhci_device_bulk_done: xfer=%p ii=%p sc=%p upipe=%p\n",
2693 xfer, ii, sc, upipe));
2695 if (!uhci_active_intr_info(ii))
2696 return;
2698 uhci_del_intr_info(ii); /* remove from active list */
2700 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2702 uhci_free_std_chain(sc, ii->stdstart, NULL);
2704 DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer->actlen));
2707 /* Add interrupt QH, called with vflock. */
2708 void
2709 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2711 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2712 uhci_soft_qh_t *eqh;
2714 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2716 eqh = vf->eqh;
2717 sqh->hlink = eqh->hlink;
2718 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2719 eqh->hlink = sqh;
2720 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
2721 vf->eqh = sqh;
2722 vf->bandwidth++;
2725 /* Remove interrupt QH. */
2726 void
2727 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2729 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2730 uhci_soft_qh_t *pqh;
2732 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2734 /* See comment in uhci_remove_ctrl() */
2735 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
2736 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2737 delay(UHCI_QH_REMOVE_DELAY);
2740 pqh = uhci_find_prev_qh(vf->hqh, sqh);
2741 pqh->hlink = sqh->hlink;
2742 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2743 delay(UHCI_QH_REMOVE_DELAY);
2744 if (vf->eqh == sqh)
2745 vf->eqh = pqh;
2746 vf->bandwidth--;
2749 usbd_status
2750 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
2752 uhci_soft_qh_t *sqh;
2753 int i, npoll;
2754 u_int bestbw, bw, bestoffs, offs;
2756 DPRINTFN(2, ("uhci_device_setintr: pipe=%p\n", upipe));
2757 if (ival == 0) {
2758 kprintf("uhci_setintr: 0 interval\n");
2759 return (USBD_INVAL);
2762 if (ival > UHCI_VFRAMELIST_COUNT)
2763 ival = UHCI_VFRAMELIST_COUNT;
2764 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2765 DPRINTFN(2, ("uhci_device_setintr: ival=%d npoll=%d\n", ival, npoll));
2767 upipe->u.intr.npoll = npoll;
2768 upipe->u.intr.qhs =
2769 kmalloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_INTWAIT);
2772 * Figure out which offset in the schedule that has most
2773 * bandwidth left over.
2775 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2776 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2777 for (bw = i = 0; i < npoll; i++)
2778 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2779 if (bw < bestbw) {
2780 bestbw = bw;
2781 bestoffs = offs;
2784 DPRINTFN(1, ("uhci_device_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2786 for(i = 0; i < npoll; i++) {
2787 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2788 sqh->elink = NULL;
2789 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2790 sqh->pos = MOD(i * ival + bestoffs);
2792 #undef MOD
2794 crit_enter();
2795 /* Enter QHs into the controller data structures. */
2796 for(i = 0; i < npoll; i++)
2797 uhci_add_intr(sc, upipe->u.intr.qhs[i]);
2798 crit_exit();
2800 DPRINTFN(5, ("uhci_device_setintr: returns %p\n", upipe));
2801 return (USBD_NORMAL_COMPLETION);
2804 /* Open a new pipe. */
2805 usbd_status
2806 uhci_open(usbd_pipe_handle pipe)
2808 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2809 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2810 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2811 usbd_status err;
2812 int ival;
2814 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2815 pipe, pipe->device->address,
2816 ed->bEndpointAddress, sc->sc_addr));
2818 upipe->aborting = 0;
2819 upipe->nexttoggle = pipe->endpoint->savedtoggle;
2821 if (pipe->device->address == sc->sc_addr) {
2822 switch (ed->bEndpointAddress) {
2823 case USB_CONTROL_ENDPOINT:
2824 pipe->methods = &uhci_root_ctrl_methods;
2825 break;
2826 case UE_DIR_IN | UHCI_INTR_ENDPT:
2827 pipe->methods = &uhci_root_intr_methods;
2828 break;
2829 default:
2830 return (USBD_INVAL);
2832 } else {
2833 switch (ed->bmAttributes & UE_XFERTYPE) {
2834 case UE_CONTROL:
2835 pipe->methods = &uhci_device_ctrl_methods;
2836 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2837 if (upipe->u.ctl.sqh == NULL)
2838 goto bad;
2839 upipe->u.ctl.setup = uhci_alloc_std(sc);
2840 if (upipe->u.ctl.setup == NULL) {
2841 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2842 goto bad;
2844 upipe->u.ctl.stat = uhci_alloc_std(sc);
2845 if (upipe->u.ctl.stat == NULL) {
2846 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2847 uhci_free_std(sc, upipe->u.ctl.setup);
2848 goto bad;
2850 err = usb_allocmem(&sc->sc_bus,
2851 sizeof(usb_device_request_t),
2852 0, &upipe->u.ctl.reqdma);
2853 if (err) {
2854 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2855 uhci_free_std(sc, upipe->u.ctl.setup);
2856 uhci_free_std(sc, upipe->u.ctl.stat);
2857 goto bad;
2859 break;
2860 case UE_INTERRUPT:
2861 pipe->methods = &uhci_device_intr_methods;
2862 ival = pipe->interval;
2863 if (ival == USBD_DEFAULT_INTERVAL)
2864 ival = ed->bInterval;
2865 return (uhci_device_setintr(sc, upipe, ival));
2866 case UE_ISOCHRONOUS:
2867 pipe->methods = &uhci_device_isoc_methods;
2868 return (uhci_setup_isoc(pipe));
2869 case UE_BULK:
2870 pipe->methods = &uhci_device_bulk_methods;
2871 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2872 if (upipe->u.bulk.sqh == NULL)
2873 goto bad;
2874 break;
2877 return (USBD_NORMAL_COMPLETION);
2879 bad:
2880 return (USBD_NOMEM);
2884 * Data structures and routines to emulate the root hub.
2886 usb_device_descriptor_t uhci_devd = {
2887 USB_DEVICE_DESCRIPTOR_SIZE,
2888 UDESC_DEVICE, /* type */
2889 {0x00, 0x01}, /* USB version */
2890 UDCLASS_HUB, /* class */
2891 UDSUBCLASS_HUB, /* subclass */
2892 UDPROTO_FSHUB, /* protocol */
2893 64, /* max packet */
2894 {0},{0},{0x00,0x01}, /* device id */
2895 1,2,0, /* string indicies */
2896 1 /* # of configurations */
2899 usb_config_descriptor_t uhci_confd = {
2900 USB_CONFIG_DESCRIPTOR_SIZE,
2901 UDESC_CONFIG,
2902 {USB_CONFIG_DESCRIPTOR_SIZE +
2903 USB_INTERFACE_DESCRIPTOR_SIZE +
2904 USB_ENDPOINT_DESCRIPTOR_SIZE},
2908 UC_SELF_POWERED,
2909 0 /* max power */
2912 usb_interface_descriptor_t uhci_ifcd = {
2913 USB_INTERFACE_DESCRIPTOR_SIZE,
2914 UDESC_INTERFACE,
2918 UICLASS_HUB,
2919 UISUBCLASS_HUB,
2920 UIPROTO_FSHUB,
2924 usb_endpoint_descriptor_t uhci_endpd = {
2925 USB_ENDPOINT_DESCRIPTOR_SIZE,
2926 UDESC_ENDPOINT,
2927 UE_DIR_IN | UHCI_INTR_ENDPT,
2928 UE_INTERRUPT,
2929 {8},
2933 usb_hub_descriptor_t uhci_hubd_piix = {
2934 USB_HUB_DESCRIPTOR_SIZE,
2935 UDESC_HUB,
2937 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2938 50, /* power on to power good */
2940 { 0x00 }, /* both ports are removable */
2944 uhci_str(usb_string_descriptor_t *p, int l, char *s)
2946 int i;
2948 if (l == 0)
2949 return (0);
2950 p->bLength = 2 * strlen(s) + 2;
2951 if (l == 1)
2952 return (1);
2953 p->bDescriptorType = UDESC_STRING;
2954 l -= 2;
2955 for (i = 0; s[i] && l > 1; i++, l -= 2)
2956 USETW2(p->bString[i], 0, s[i]);
2957 return (2*i+2);
2961 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
2962 * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
2963 * should not be used by the USB subsystem. As we cannot issue a
2964 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
2965 * will be enabled as part of the reset.
2967 * On the VT83C572, the port cannot be successfully enabled until the
2968 * outstanding "port enable change" and "connection status change"
2969 * events have been reset.
2971 static usbd_status
2972 uhci_portreset(uhci_softc_t *sc, int index)
2974 int lim, port, x;
2976 if (index == 1)
2977 port = UHCI_PORTSC1;
2978 else if (index == 2)
2979 port = UHCI_PORTSC2;
2980 else
2981 return (USBD_IOERROR);
2983 x = URWMASK(UREAD2(sc, port));
2984 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2986 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
2988 DPRINTFN(3,("uhci port %d reset, status0 = 0x%04x\n",
2989 index, UREAD2(sc, port)));
2991 x = URWMASK(UREAD2(sc, port));
2992 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2994 delay(100);
2996 DPRINTFN(3,("uhci port %d reset, status1 = 0x%04x\n",
2997 index, UREAD2(sc, port)));
2999 x = URWMASK(UREAD2(sc, port));
3000 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3002 for (lim = 10; --lim > 0;) {
3003 usb_delay_ms(&sc->sc_bus, USB_PORT_RESET_DELAY);
3005 x = UREAD2(sc, port);
3007 DPRINTFN(3,("uhci port %d iteration %u, status = 0x%04x\n",
3008 index, lim, x));
3010 if (!(x & UHCI_PORTSC_CCS)) {
3012 * No device is connected (or was disconnected
3013 * during reset). Consider the port reset.
3014 * The delay must be long enough to ensure on
3015 * the initial iteration that the device
3016 * connection will have been registered. 50ms
3017 * appears to be sufficient, but 20ms is not.
3019 DPRINTFN(3,("uhci port %d loop %u, device detached\n",
3020 index, lim));
3021 break;
3024 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
3026 * Port enabled changed and/or connection
3027 * status changed were set. Reset either or
3028 * both raised flags (by writing a 1 to that
3029 * bit), and wait again for state to settle.
3031 UWRITE2(sc, port, URWMASK(x) |
3032 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
3033 continue;
3036 if (x & UHCI_PORTSC_PE)
3037 /* Port is enabled */
3038 break;
3040 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
3043 DPRINTFN(3,("uhci port %d reset, status2 = 0x%04x\n",
3044 index, UREAD2(sc, port)));
3046 if (lim <= 0) {
3047 DPRINTFN(1,("uhci port %d reset timed out\n", index));
3048 return (USBD_TIMEOUT);
3051 sc->sc_isreset = 1;
3052 return (USBD_NORMAL_COMPLETION);
3056 * Simulate a hardware hub by handling all the necessary requests.
3058 usbd_status
3059 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
3061 usbd_status err;
3063 /* Insert last in queue. */
3064 err = usb_insert_transfer(xfer);
3065 if (err)
3066 return (err);
3069 * Pipe isn't running (otherwise err would be USBD_INPROG),
3070 * so start it first.
3072 return (uhci_root_ctrl_start(STAILQ_FIRST(&xfer->pipe->queue)));
3075 usbd_status
3076 uhci_root_ctrl_start(usbd_xfer_handle xfer)
3078 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3079 usb_device_request_t *req;
3080 void *buf = NULL;
3081 int port, x;
3082 int len, value, index, status, change, l, totlen = 0;
3083 usb_port_status_t ps;
3084 usbd_status err;
3086 if (sc->sc_dying)
3087 return (USBD_IOERROR);
3089 #ifdef DIAGNOSTIC
3090 if (!(xfer->rqflags & URQ_REQUEST))
3091 panic("uhci_root_ctrl_transfer: not a request");
3092 #endif
3093 req = &xfer->request;
3095 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
3096 req->bmRequestType, req->bRequest));
3098 len = UGETW(req->wLength);
3099 value = UGETW(req->wValue);
3100 index = UGETW(req->wIndex);
3102 if (len != 0)
3103 buf = KERNADDR(&xfer->dmabuf, 0);
3105 #define C(x,y) ((x) | ((y) << 8))
3106 switch(C(req->bRequest, req->bmRequestType)) {
3107 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3108 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3109 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3111 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3112 * for the integrated root hub.
3114 break;
3115 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3116 if (len > 0) {
3117 *(u_int8_t *)buf = sc->sc_conf;
3118 totlen = 1;
3120 break;
3121 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3122 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
3123 switch(value >> 8) {
3124 case UDESC_DEVICE:
3125 if ((value & 0xff) != 0) {
3126 err = USBD_IOERROR;
3127 goto ret;
3129 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
3130 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
3131 memcpy(buf, &uhci_devd, l);
3132 break;
3133 case UDESC_CONFIG:
3134 if ((value & 0xff) != 0) {
3135 err = USBD_IOERROR;
3136 goto ret;
3138 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
3139 memcpy(buf, &uhci_confd, l);
3140 buf = (char *)buf + l;
3141 len -= l;
3142 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
3143 totlen += l;
3144 memcpy(buf, &uhci_ifcd, l);
3145 buf = (char *)buf + l;
3146 len -= l;
3147 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
3148 totlen += l;
3149 memcpy(buf, &uhci_endpd, l);
3150 break;
3151 case UDESC_STRING:
3152 if (len == 0)
3153 break;
3154 *(u_int8_t *)buf = 0;
3155 totlen = 1;
3156 switch (value & 0xff) {
3157 case 1: /* Vendor */
3158 totlen = uhci_str(buf, len, sc->sc_vendor);
3159 break;
3160 case 2: /* Product */
3161 totlen = uhci_str(buf, len, "UHCI root hub");
3162 break;
3164 break;
3165 default:
3166 err = USBD_IOERROR;
3167 goto ret;
3169 break;
3170 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3171 if (len > 0) {
3172 *(u_int8_t *)buf = 0;
3173 totlen = 1;
3175 break;
3176 case C(UR_GET_STATUS, UT_READ_DEVICE):
3177 if (len > 1) {
3178 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
3179 totlen = 2;
3181 break;
3182 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3183 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3184 if (len > 1) {
3185 USETW(((usb_status_t *)buf)->wStatus, 0);
3186 totlen = 2;
3188 break;
3189 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3190 if (value >= USB_MAX_DEVICES) {
3191 err = USBD_IOERROR;
3192 goto ret;
3194 sc->sc_addr = value;
3195 break;
3196 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3197 if (value != 0 && value != 1) {
3198 err = USBD_IOERROR;
3199 goto ret;
3201 sc->sc_conf = value;
3202 break;
3203 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3204 break;
3205 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3206 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3207 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3208 err = USBD_IOERROR;
3209 goto ret;
3210 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3211 break;
3212 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3213 break;
3214 /* Hub requests */
3215 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3216 break;
3217 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3218 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
3219 "port=%d feature=%d\n",
3220 index, value));
3221 if (index == 1)
3222 port = UHCI_PORTSC1;
3223 else if (index == 2)
3224 port = UHCI_PORTSC2;
3225 else {
3226 err = USBD_IOERROR;
3227 goto ret;
3229 switch(value) {
3230 case UHF_PORT_ENABLE:
3231 x = URWMASK(UREAD2(sc, port));
3232 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
3233 break;
3234 case UHF_PORT_SUSPEND:
3235 x = URWMASK(UREAD2(sc, port));
3236 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
3237 break;
3238 case UHF_PORT_RESET:
3239 x = URWMASK(UREAD2(sc, port));
3240 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3241 break;
3242 case UHF_C_PORT_CONNECTION:
3243 x = URWMASK(UREAD2(sc, port));
3244 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
3245 break;
3246 case UHF_C_PORT_ENABLE:
3247 x = URWMASK(UREAD2(sc, port));
3248 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
3249 break;
3250 case UHF_C_PORT_OVER_CURRENT:
3251 x = URWMASK(UREAD2(sc, port));
3252 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
3253 break;
3254 case UHF_C_PORT_RESET:
3255 sc->sc_isreset = 0;
3256 err = USBD_NORMAL_COMPLETION;
3257 goto ret;
3258 case UHF_PORT_CONNECTION:
3259 case UHF_PORT_OVER_CURRENT:
3260 case UHF_PORT_POWER:
3261 case UHF_PORT_LOW_SPEED:
3262 case UHF_C_PORT_SUSPEND:
3263 default:
3264 err = USBD_IOERROR;
3265 goto ret;
3267 break;
3268 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
3269 if (index == 1)
3270 port = UHCI_PORTSC1;
3271 else if (index == 2)
3272 port = UHCI_PORTSC2;
3273 else {
3274 err = USBD_IOERROR;
3275 goto ret;
3277 if (len > 0) {
3278 *(u_int8_t *)buf =
3279 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
3280 UHCI_PORTSC_LS_SHIFT;
3281 totlen = 1;
3283 break;
3284 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3285 if ((value & 0xff) != 0) {
3286 err = USBD_IOERROR;
3287 goto ret;
3289 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
3290 totlen = l;
3291 memcpy(buf, &uhci_hubd_piix, l);
3292 break;
3293 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3294 if (len != 4) {
3295 err = USBD_IOERROR;
3296 goto ret;
3298 memset(buf, 0, len);
3299 totlen = len;
3300 break;
3301 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3302 if (index == 1)
3303 port = UHCI_PORTSC1;
3304 else if (index == 2)
3305 port = UHCI_PORTSC2;
3306 else {
3307 err = USBD_IOERROR;
3308 goto ret;
3310 if (len != 4) {
3311 err = USBD_IOERROR;
3312 goto ret;
3314 x = UREAD2(sc, port);
3315 status = change = 0;
3316 if (x & UHCI_PORTSC_CCS)
3317 status |= UPS_CURRENT_CONNECT_STATUS;
3318 if (x & UHCI_PORTSC_CSC)
3319 change |= UPS_C_CONNECT_STATUS;
3320 if (x & UHCI_PORTSC_PE)
3321 status |= UPS_PORT_ENABLED;
3322 if (x & UHCI_PORTSC_POEDC)
3323 change |= UPS_C_PORT_ENABLED;
3324 if (x & UHCI_PORTSC_OCI)
3325 status |= UPS_OVERCURRENT_INDICATOR;
3326 if (x & UHCI_PORTSC_OCIC)
3327 change |= UPS_C_OVERCURRENT_INDICATOR;
3328 if (x & UHCI_PORTSC_SUSP)
3329 status |= UPS_SUSPEND;
3330 if (x & UHCI_PORTSC_LSDA)
3331 status |= UPS_LOW_SPEED;
3332 status |= UPS_PORT_POWER;
3333 if (sc->sc_isreset)
3334 change |= UPS_C_PORT_RESET;
3335 USETW(ps.wPortStatus, status);
3336 USETW(ps.wPortChange, change);
3337 l = min(len, sizeof ps);
3338 memcpy(buf, &ps, l);
3339 totlen = l;
3340 break;
3341 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3342 err = USBD_IOERROR;
3343 goto ret;
3344 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3345 break;
3346 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3347 if (index == 1)
3348 port = UHCI_PORTSC1;
3349 else if (index == 2)
3350 port = UHCI_PORTSC2;
3351 else {
3352 err = USBD_IOERROR;
3353 goto ret;
3355 switch(value) {
3356 case UHF_PORT_ENABLE:
3357 x = URWMASK(UREAD2(sc, port));
3358 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3359 break;
3360 case UHF_PORT_SUSPEND:
3361 x = URWMASK(UREAD2(sc, port));
3362 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
3363 break;
3364 case UHF_PORT_RESET:
3365 err = uhci_portreset(sc, index);
3366 goto ret;
3367 case UHF_PORT_POWER:
3368 /* Pretend we turned on power */
3369 err = USBD_NORMAL_COMPLETION;
3370 goto ret;
3371 case UHF_C_PORT_CONNECTION:
3372 case UHF_C_PORT_ENABLE:
3373 case UHF_C_PORT_OVER_CURRENT:
3374 case UHF_PORT_CONNECTION:
3375 case UHF_PORT_OVER_CURRENT:
3376 case UHF_PORT_LOW_SPEED:
3377 case UHF_C_PORT_SUSPEND:
3378 case UHF_C_PORT_RESET:
3379 default:
3380 err = USBD_IOERROR;
3381 goto ret;
3383 break;
3384 default:
3385 err = USBD_IOERROR;
3386 goto ret;
3388 xfer->actlen = totlen;
3389 err = USBD_NORMAL_COMPLETION;
3390 ret:
3391 xfer->status = err;
3392 crit_enter();
3393 usb_transfer_complete(xfer);
3394 crit_exit();
3395 return (USBD_IN_PROGRESS);
3398 /* Abort a root control request. */
3399 void
3400 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
3402 /* Nothing to do, all transfers are synchronous. */
3405 /* Close the root pipe. */
3406 void
3407 uhci_root_ctrl_close(usbd_pipe_handle pipe)
3409 DPRINTF(("uhci_root_ctrl_close\n"));
3412 /* Abort a root interrupt request. */
3413 void
3414 uhci_root_intr_abort(usbd_xfer_handle xfer)
3416 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3418 callout_stop(&sc->sc_poll_handle);
3419 sc->sc_intr_xfer = NULL;
3421 if (xfer->pipe->intrxfer == xfer) {
3422 DPRINTF(("uhci_root_intr_abort: remove\n"));
3423 xfer->pipe->intrxfer = 0;
3425 xfer->status = USBD_CANCELLED;
3426 #ifdef DIAGNOSTIC
3427 UXFER(xfer)->iinfo.isdone = 1;
3428 #endif
3429 usb_transfer_complete(xfer);
3432 usbd_status
3433 uhci_root_intr_transfer(usbd_xfer_handle xfer)
3435 usbd_status err;
3437 /* Insert last in queue. */
3438 err = usb_insert_transfer(xfer);
3439 if (err)
3440 return (err);
3443 * Pipe isn't running (otherwise err would be USBD_INPROG),
3444 * so start it first.
3446 return (uhci_root_intr_start(STAILQ_FIRST(&xfer->pipe->queue)));
3449 /* Start a transfer on the root interrupt pipe */
3450 usbd_status
3451 uhci_root_intr_start(usbd_xfer_handle xfer)
3453 usbd_pipe_handle pipe = xfer->pipe;
3454 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3456 DPRINTFN(3, ("uhci_root_intr_start: xfer=%p len=%d flags=%d\n",
3457 xfer, xfer->length, xfer->flags));
3459 if (sc->sc_dying)
3460 return (USBD_IOERROR);
3462 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3463 callout_reset(&sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
3464 sc->sc_intr_xfer = xfer;
3465 return (USBD_IN_PROGRESS);
3468 /* Close the root interrupt pipe. */
3469 void
3470 uhci_root_intr_close(usbd_pipe_handle pipe)
3472 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3474 callout_stop(&sc->sc_poll_handle);
3475 sc->sc_intr_xfer = NULL;
3476 DPRINTF(("uhci_root_intr_close\n"));