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