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