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