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