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