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