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