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