xref: /freebsd/sys/dev/usb/controller/uhci.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
1 /*-
2  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
3  * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4  * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * USB Universal Host Controller driver.
33  * Handles e.g. PIIX3 and PIIX4.
34  *
35  * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
36  * USB spec: http://www.usb.org/developers/docs/usbspec.zip
37  * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
38  *             ftp://download.intel.com/design/intarch/datashts/29056201.pdf
39  */
40 
41 #include <dev/usb/usb.h>
42 #include <dev/usb/usb_mfunc.h>
43 #include <dev/usb/usb_error.h>
44 #include <dev/usb/usb_defs.h>
45 
46 #define	USB_DEBUG_VAR uhcidebug
47 
48 #include <dev/usb/usb_core.h>
49 #include <dev/usb/usb_debug.h>
50 #include <dev/usb/usb_busdma.h>
51 #include <dev/usb/usb_process.h>
52 #include <dev/usb/usb_sw_transfer.h>
53 #include <dev/usb/usb_transfer.h>
54 #include <dev/usb/usb_device.h>
55 #include <dev/usb/usb_hub.h>
56 #include <dev/usb/usb_util.h>
57 
58 #include <dev/usb/usb_controller.h>
59 #include <dev/usb/usb_bus.h>
60 #include <dev/usb/controller/uhci.h>
61 
62 #define	alt_next next
63 #define	UHCI_BUS2SC(bus) ((uhci_softc_t *)(((uint8_t *)(bus)) - \
64    USB_P2U(&(((uhci_softc_t *)0)->sc_bus))))
65 
66 #if USB_DEBUG
67 static int uhcidebug = 0;
68 static int uhcinoloop = 0;
69 
70 SYSCTL_NODE(_hw_usb2, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
71 SYSCTL_INT(_hw_usb2_uhci, OID_AUTO, debug, CTLFLAG_RW,
72     &uhcidebug, 0, "uhci debug level");
73 SYSCTL_INT(_hw_usb2_uhci, OID_AUTO, loop, CTLFLAG_RW,
74     &uhcinoloop, 0, "uhci noloop");
75 static void uhci_dumpregs(uhci_softc_t *sc);
76 static void uhci_dump_tds(uhci_td_t *td);
77 
78 #endif
79 
80 #define	UBARR(sc) bus_space_barrier((sc)->sc_io_tag, (sc)->sc_io_hdl, 0, (sc)->sc_io_size, \
81 			BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
82 #define	UWRITE1(sc, r, x) \
83  do { UBARR(sc); bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
84  } while (/*CONSTCOND*/0)
85 #define	UWRITE2(sc, r, x) \
86  do { UBARR(sc); bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
87  } while (/*CONSTCOND*/0)
88 #define	UWRITE4(sc, r, x) \
89  do { UBARR(sc); bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \
90  } while (/*CONSTCOND*/0)
91 #define	UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
92 #define	UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
93 #define	UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
94 
95 #define	UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
96 #define	UHCISTS(sc) UREAD2(sc, UHCI_STS)
97 
98 #define	UHCI_RESET_TIMEOUT 100		/* ms, reset timeout */
99 
100 #define	UHCI_INTR_ENDPT 1
101 
102 struct uhci_mem_layout {
103 
104 	struct usb2_page_search buf_res;
105 	struct usb2_page_search fix_res;
106 
107 	struct usb2_page_cache *buf_pc;
108 	struct usb2_page_cache *fix_pc;
109 
110 	uint32_t buf_offset;
111 
112 	uint16_t max_frame_size;
113 };
114 
115 struct uhci_std_temp {
116 
117 	struct uhci_mem_layout ml;
118 	uhci_td_t *td;
119 	uhci_td_t *td_next;
120 	uint32_t average;
121 	uint32_t td_status;
122 	uint32_t td_token;
123 	uint32_t len;
124 	uint16_t max_frame_size;
125 	uint8_t	shortpkt;
126 	uint8_t	setup_alt_next;
127 	uint8_t	short_frames_ok;
128 };
129 
130 extern struct usb2_bus_methods uhci_bus_methods;
131 extern struct usb2_pipe_methods uhci_device_bulk_methods;
132 extern struct usb2_pipe_methods uhci_device_ctrl_methods;
133 extern struct usb2_pipe_methods uhci_device_intr_methods;
134 extern struct usb2_pipe_methods uhci_device_isoc_methods;
135 extern struct usb2_pipe_methods uhci_root_ctrl_methods;
136 extern struct usb2_pipe_methods uhci_root_intr_methods;
137 
138 static void	uhci_root_ctrl_poll(struct uhci_softc *);
139 static void	uhci_do_poll(struct usb2_bus *);
140 static void	uhci_device_done(struct usb2_xfer *, usb2_error_t);
141 static void	uhci_transfer_intr_enqueue(struct usb2_xfer *);
142 static void	uhci_root_intr_check(void *);
143 static void	uhci_timeout(void *);
144 static uint8_t	uhci_check_transfer(struct usb2_xfer *);
145 
146 void
147 uhci_iterate_hw_softc(struct usb2_bus *bus, usb2_bus_mem_sub_cb_t *cb)
148 {
149 	struct uhci_softc *sc = UHCI_BUS2SC(bus);
150 	uint32_t i;
151 
152 	cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg,
153 	    sizeof(uint32_t) * UHCI_FRAMELIST_COUNT, UHCI_FRAMELIST_ALIGN);
154 
155 	cb(bus, &sc->sc_hw.ls_ctl_start_pc, &sc->sc_hw.ls_ctl_start_pg,
156 	    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
157 
158 	cb(bus, &sc->sc_hw.fs_ctl_start_pc, &sc->sc_hw.fs_ctl_start_pg,
159 	    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
160 
161 	cb(bus, &sc->sc_hw.bulk_start_pc, &sc->sc_hw.bulk_start_pg,
162 	    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
163 
164 	cb(bus, &sc->sc_hw.last_qh_pc, &sc->sc_hw.last_qh_pg,
165 	    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
166 
167 	cb(bus, &sc->sc_hw.last_td_pc, &sc->sc_hw.last_td_pg,
168 	    sizeof(uhci_td_t), UHCI_TD_ALIGN);
169 
170 	for (i = 0; i != UHCI_VFRAMELIST_COUNT; i++) {
171 		cb(bus, sc->sc_hw.isoc_start_pc + i,
172 		    sc->sc_hw.isoc_start_pg + i,
173 		    sizeof(uhci_td_t), UHCI_TD_ALIGN);
174 	}
175 
176 	for (i = 0; i != UHCI_IFRAMELIST_COUNT; i++) {
177 		cb(bus, sc->sc_hw.intr_start_pc + i,
178 		    sc->sc_hw.intr_start_pg + i,
179 		    sizeof(uhci_qh_t), UHCI_QH_ALIGN);
180 	}
181 }
182 
183 static void
184 uhci_mem_layout_init(struct uhci_mem_layout *ml, struct usb2_xfer *xfer)
185 {
186 	ml->buf_pc = xfer->frbuffers + 0;
187 	ml->fix_pc = xfer->buf_fixup;
188 
189 	ml->buf_offset = 0;
190 
191 	ml->max_frame_size = xfer->max_frame_size;
192 }
193 
194 static void
195 uhci_mem_layout_fixup(struct uhci_mem_layout *ml, struct uhci_td *td)
196 {
197 	usb2_get_page(ml->buf_pc, ml->buf_offset, &ml->buf_res);
198 
199 	if (ml->buf_res.length < td->len) {
200 
201 		/* need to do a fixup */
202 
203 		usb2_get_page(ml->fix_pc, 0, &ml->fix_res);
204 
205 		td->td_buffer = htole32(ml->fix_res.physaddr);
206 
207 		/*
208 	         * The UHCI driver cannot handle
209 	         * page crossings, so a fixup is
210 	         * needed:
211 	         *
212 	         *  +----+----+ - - -
213 	         *  | YYY|Y   |
214 	         *  +----+----+ - - -
215 	         *     \    \
216 	         *      \    \
217 	         *       +----+
218 	         *       |YYYY|  (fixup)
219 	         *       +----+
220 	         */
221 
222 		if ((td->td_token & htole32(UHCI_TD_PID)) ==
223 		    htole32(UHCI_TD_PID_IN)) {
224 			td->fix_pc = ml->fix_pc;
225 			usb2_pc_cpu_invalidate(ml->fix_pc);
226 
227 		} else {
228 			td->fix_pc = NULL;
229 
230 			/* copy data to fixup location */
231 
232 			usb2_copy_out(ml->buf_pc, ml->buf_offset,
233 			    ml->fix_res.buffer, td->len);
234 
235 			usb2_pc_cpu_flush(ml->fix_pc);
236 		}
237 
238 		/* prepare next fixup */
239 
240 		ml->fix_pc++;
241 
242 	} else {
243 
244 		td->td_buffer = htole32(ml->buf_res.physaddr);
245 		td->fix_pc = NULL;
246 	}
247 
248 	/* prepare next data location */
249 
250 	ml->buf_offset += td->len;
251 }
252 
253 void
254 uhci_reset(uhci_softc_t *sc)
255 {
256 	struct usb2_page_search buf_res;
257 	uint16_t n;
258 
259 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
260 
261 	DPRINTF("resetting the HC\n");
262 
263 	/* disable interrupts */
264 
265 	UWRITE2(sc, UHCI_INTR, 0);
266 
267 	/* global reset */
268 
269 	UHCICMD(sc, UHCI_CMD_GRESET);
270 
271 	/* wait */
272 
273 	usb2_pause_mtx(&sc->sc_bus.bus_mtx,
274 	    USB_MS_TO_TICKS(USB_BUS_RESET_DELAY));
275 
276 	/* terminate all transfers */
277 
278 	UHCICMD(sc, UHCI_CMD_HCRESET);
279 
280 	/* the reset bit goes low when the controller is done */
281 
282 	n = UHCI_RESET_TIMEOUT;
283 	while (n--) {
284 		/* wait one millisecond */
285 
286 		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
287 
288 		if (!(UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET)) {
289 			goto done_1;
290 		}
291 	}
292 
293 	device_printf(sc->sc_bus.bdev,
294 	    "controller did not reset\n");
295 
296 done_1:
297 
298 	n = 10;
299 	while (n--) {
300 		/* wait one millisecond */
301 
302 		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
303 
304 		/* check if HC is stopped */
305 		if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) {
306 			goto done_2;
307 		}
308 	}
309 
310 	device_printf(sc->sc_bus.bdev,
311 	    "controller did not stop\n");
312 
313 done_2:
314 
315 	/* reload the configuration */
316 	usb2_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
317 	UWRITE4(sc, UHCI_FLBASEADDR, buf_res.physaddr);
318 	UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
319 	UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
320 }
321 
322 static void
323 uhci_start(uhci_softc_t *sc)
324 {
325 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
326 
327 	DPRINTFN(2, "enabling\n");
328 
329 	/* enable interrupts */
330 
331 	UWRITE2(sc, UHCI_INTR,
332 	    (UHCI_INTR_TOCRCIE |
333 	    UHCI_INTR_RIE |
334 	    UHCI_INTR_IOCE |
335 	    UHCI_INTR_SPIE));
336 
337 	/*
338 	 * assume 64 byte packets at frame end and start HC controller
339 	 */
340 
341 	UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS));
342 
343 	uint8_t n = 10;
344 
345 	while (n--) {
346 		/* wait one millisecond */
347 
348 		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
349 
350 		/* check that controller has started */
351 
352 		if (!(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH)) {
353 			goto done;
354 		}
355 	}
356 
357 	device_printf(sc->sc_bus.bdev,
358 	    "cannot start HC controller\n");
359 
360 done:
361 	return;
362 }
363 
364 static struct uhci_qh *
365 uhci_init_qh(struct usb2_page_cache *pc)
366 {
367 	struct usb2_page_search buf_res;
368 	struct uhci_qh *qh;
369 
370 	usb2_get_page(pc, 0, &buf_res);
371 
372 	qh = buf_res.buffer;
373 
374 	qh->qh_self =
375 	    htole32(buf_res.physaddr) |
376 	    htole32(UHCI_PTR_QH);
377 
378 	qh->page_cache = pc;
379 
380 	return (qh);
381 }
382 
383 static struct uhci_td *
384 uhci_init_td(struct usb2_page_cache *pc)
385 {
386 	struct usb2_page_search buf_res;
387 	struct uhci_td *td;
388 
389 	usb2_get_page(pc, 0, &buf_res);
390 
391 	td = buf_res.buffer;
392 
393 	td->td_self =
394 	    htole32(buf_res.physaddr) |
395 	    htole32(UHCI_PTR_TD);
396 
397 	td->page_cache = pc;
398 
399 	return (td);
400 }
401 
402 usb2_error_t
403 uhci_init(uhci_softc_t *sc)
404 {
405 	uint16_t bit;
406 	uint16_t x;
407 	uint16_t y;
408 
409 	DPRINTF("start\n");
410 
411 #if USB_DEBUG
412 	if (uhcidebug > 2) {
413 		uhci_dumpregs(sc);
414 	}
415 #endif
416 
417 	sc->sc_saved_sof = 0x40;	/* default value */
418 	sc->sc_saved_frnum = 0;		/* default frame number */
419 
420 	/*
421 	 * Setup QH's
422 	 */
423 	sc->sc_ls_ctl_p_last =
424 	    uhci_init_qh(&sc->sc_hw.ls_ctl_start_pc);
425 
426 	sc->sc_fs_ctl_p_last =
427 	    uhci_init_qh(&sc->sc_hw.fs_ctl_start_pc);
428 
429 	sc->sc_bulk_p_last =
430 	    uhci_init_qh(&sc->sc_hw.bulk_start_pc);
431 #if 0
432 	sc->sc_reclaim_qh_p =
433 	    sc->sc_fs_ctl_p_last;
434 #else
435 	/* setup reclaim looping point */
436 	sc->sc_reclaim_qh_p =
437 	    sc->sc_bulk_p_last;
438 #endif
439 
440 	sc->sc_last_qh_p =
441 	    uhci_init_qh(&sc->sc_hw.last_qh_pc);
442 
443 	sc->sc_last_td_p =
444 	    uhci_init_td(&sc->sc_hw.last_td_pc);
445 
446 	for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
447 		sc->sc_isoc_p_last[x] =
448 		    uhci_init_td(sc->sc_hw.isoc_start_pc + x);
449 	}
450 
451 	for (x = 0; x != UHCI_IFRAMELIST_COUNT; x++) {
452 		sc->sc_intr_p_last[x] =
453 		    uhci_init_qh(sc->sc_hw.intr_start_pc + x);
454 	}
455 
456 	/*
457 	 * the QHs are arranged to give poll intervals that are
458 	 * powers of 2 times 1ms
459 	 */
460 	bit = UHCI_IFRAMELIST_COUNT / 2;
461 	while (bit) {
462 		x = bit;
463 		while (x & bit) {
464 			uhci_qh_t *qh_x;
465 			uhci_qh_t *qh_y;
466 
467 			y = (x ^ bit) | (bit / 2);
468 
469 			/*
470 			 * the next QH has half the poll interval
471 			 */
472 			qh_x = sc->sc_intr_p_last[x];
473 			qh_y = sc->sc_intr_p_last[y];
474 
475 			qh_x->h_next = NULL;
476 			qh_x->qh_h_next = qh_y->qh_self;
477 			qh_x->e_next = NULL;
478 			qh_x->qh_e_next = htole32(UHCI_PTR_T);
479 			x++;
480 		}
481 		bit >>= 1;
482 	}
483 
484 	if (1) {
485 		uhci_qh_t *qh_ls;
486 		uhci_qh_t *qh_intr;
487 
488 		qh_ls = sc->sc_ls_ctl_p_last;
489 		qh_intr = sc->sc_intr_p_last[0];
490 
491 		/* start QH for interrupt traffic */
492 		qh_intr->h_next = qh_ls;
493 		qh_intr->qh_h_next = qh_ls->qh_self;
494 		qh_intr->e_next = 0;
495 		qh_intr->qh_e_next = htole32(UHCI_PTR_T);
496 	}
497 	for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) {
498 
499 		uhci_td_t *td_x;
500 		uhci_qh_t *qh_intr;
501 
502 		td_x = sc->sc_isoc_p_last[x];
503 		qh_intr = sc->sc_intr_p_last[x | (UHCI_IFRAMELIST_COUNT / 2)];
504 
505 		/* start TD for isochronous traffic */
506 		td_x->next = NULL;
507 		td_x->td_next = qh_intr->qh_self;
508 		td_x->td_status = htole32(UHCI_TD_IOS);
509 		td_x->td_token = htole32(0);
510 		td_x->td_buffer = htole32(0);
511 	}
512 
513 	if (1) {
514 		uhci_qh_t *qh_ls;
515 		uhci_qh_t *qh_fs;
516 
517 		qh_ls = sc->sc_ls_ctl_p_last;
518 		qh_fs = sc->sc_fs_ctl_p_last;
519 
520 		/* start QH where low speed control traffic will be queued */
521 		qh_ls->h_next = qh_fs;
522 		qh_ls->qh_h_next = qh_fs->qh_self;
523 		qh_ls->e_next = 0;
524 		qh_ls->qh_e_next = htole32(UHCI_PTR_T);
525 	}
526 	if (1) {
527 		uhci_qh_t *qh_ctl;
528 		uhci_qh_t *qh_blk;
529 		uhci_qh_t *qh_lst;
530 		uhci_td_t *td_lst;
531 
532 		qh_ctl = sc->sc_fs_ctl_p_last;
533 		qh_blk = sc->sc_bulk_p_last;
534 
535 		/* start QH where full speed control traffic will be queued */
536 		qh_ctl->h_next = qh_blk;
537 		qh_ctl->qh_h_next = qh_blk->qh_self;
538 		qh_ctl->e_next = 0;
539 		qh_ctl->qh_e_next = htole32(UHCI_PTR_T);
540 
541 		qh_lst = sc->sc_last_qh_p;
542 
543 		/* start QH where bulk traffic will be queued */
544 		qh_blk->h_next = qh_lst;
545 		qh_blk->qh_h_next = qh_lst->qh_self;
546 		qh_blk->e_next = 0;
547 		qh_blk->qh_e_next = htole32(UHCI_PTR_T);
548 
549 		td_lst = sc->sc_last_td_p;
550 
551 		/* end QH which is used for looping the QHs */
552 		qh_lst->h_next = 0;
553 		qh_lst->qh_h_next = htole32(UHCI_PTR_T);	/* end of QH chain */
554 		qh_lst->e_next = td_lst;
555 		qh_lst->qh_e_next = td_lst->td_self;
556 
557 		/*
558 		 * end TD which hangs from the last QH, to avoid a bug in the PIIX
559 		 * that makes it run berserk otherwise
560 		 */
561 		td_lst->next = 0;
562 		td_lst->td_next = htole32(UHCI_PTR_T);
563 		td_lst->td_status = htole32(0);	/* inactive */
564 		td_lst->td_token = htole32(0);
565 		td_lst->td_buffer = htole32(0);
566 	}
567 	if (1) {
568 		struct usb2_page_search buf_res;
569 		uint32_t *pframes;
570 
571 		usb2_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res);
572 
573 		pframes = buf_res.buffer;
574 
575 
576 		/*
577 		 * Setup UHCI framelist
578 		 *
579 		 * Execution order:
580 		 *
581 		 * pframes -> full speed isochronous -> interrupt QH's -> low
582 		 * speed control -> full speed control -> bulk transfers
583 		 *
584 		 */
585 
586 		for (x = 0; x != UHCI_FRAMELIST_COUNT; x++) {
587 			pframes[x] =
588 			    sc->sc_isoc_p_last[x % UHCI_VFRAMELIST_COUNT]->td_self;
589 		}
590 	}
591 	/* flush all cache into memory */
592 
593 	usb2_bus_mem_flush_all(&sc->sc_bus, &uhci_iterate_hw_softc);
594 
595 	/* set up the bus struct */
596 	sc->sc_bus.methods = &uhci_bus_methods;
597 
598 	USB_BUS_LOCK(&sc->sc_bus);
599 	/* reset the controller */
600 	uhci_reset(sc);
601 
602 	/* start the controller */
603 	uhci_start(sc);
604 	USB_BUS_UNLOCK(&sc->sc_bus);
605 
606 	/* catch lost interrupts */
607 	uhci_do_poll(&sc->sc_bus);
608 
609 	return (0);
610 }
611 
612 /* NOTE: suspend/resume is called from
613  * interrupt context and cannot sleep!
614  */
615 
616 void
617 uhci_suspend(uhci_softc_t *sc)
618 {
619 	USB_BUS_LOCK(&sc->sc_bus);
620 
621 #if USB_DEBUG
622 	if (uhcidebug > 2) {
623 		uhci_dumpregs(sc);
624 	}
625 #endif
626 	/* save some state if BIOS doesn't */
627 
628 	sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
629 	sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
630 
631 	/* stop the controller */
632 
633 	uhci_reset(sc);
634 
635 	/* enter global suspend */
636 
637 	UHCICMD(sc, UHCI_CMD_EGSM);
638 
639 	usb2_pause_mtx(&sc->sc_bus.bus_mtx,
640 	    USB_MS_TO_TICKS(USB_RESUME_WAIT));
641 
642 	USB_BUS_UNLOCK(&sc->sc_bus);
643 }
644 
645 void
646 uhci_resume(uhci_softc_t *sc)
647 {
648 	USB_BUS_LOCK(&sc->sc_bus);
649 
650 	/* reset the controller */
651 
652 	uhci_reset(sc);
653 
654 	/* force global resume */
655 
656 	UHCICMD(sc, UHCI_CMD_FGR);
657 
658 	usb2_pause_mtx(&sc->sc_bus.bus_mtx,
659 	    USB_MS_TO_TICKS(USB_RESUME_DELAY));
660 
661 	/* and start traffic again */
662 
663 	uhci_start(sc);
664 
665 #if USB_DEBUG
666 	if (uhcidebug > 2) {
667 		uhci_dumpregs(sc);
668 	}
669 #endif
670 
671 	USB_BUS_UNLOCK(&sc->sc_bus);
672 
673 	/* catch lost interrupts */
674 	uhci_do_poll(&sc->sc_bus);
675 }
676 
677 #if USB_DEBUG
678 static void
679 uhci_dumpregs(uhci_softc_t *sc)
680 {
681 	DPRINTFN(0, "%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
682 	    "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
683 	    device_get_nameunit(sc->sc_bus.bdev),
684 	    UREAD2(sc, UHCI_CMD),
685 	    UREAD2(sc, UHCI_STS),
686 	    UREAD2(sc, UHCI_INTR),
687 	    UREAD2(sc, UHCI_FRNUM),
688 	    UREAD4(sc, UHCI_FLBASEADDR),
689 	    UREAD1(sc, UHCI_SOF),
690 	    UREAD2(sc, UHCI_PORTSC1),
691 	    UREAD2(sc, UHCI_PORTSC2));
692 }
693 
694 static uint8_t
695 uhci_dump_td(uhci_td_t *p)
696 {
697 	uint32_t td_next;
698 	uint32_t td_status;
699 	uint32_t td_token;
700 	uint8_t temp;
701 
702 	usb2_pc_cpu_invalidate(p->page_cache);
703 
704 	td_next = le32toh(p->td_next);
705 	td_status = le32toh(p->td_status);
706 	td_token = le32toh(p->td_token);
707 
708 	/*
709 	 * Check whether the link pointer in this TD marks the link pointer
710 	 * as end of queue:
711 	 */
712 	temp = ((td_next & UHCI_PTR_T) || (td_next == 0));
713 
714 	printf("TD(%p) at 0x%08x = link=0x%08x status=0x%08x "
715 	    "token=0x%08x buffer=0x%08x\n",
716 	    p,
717 	    le32toh(p->td_self),
718 	    td_next,
719 	    td_status,
720 	    td_token,
721 	    le32toh(p->td_buffer));
722 
723 	printf("TD(%p) td_next=%s%s%s td_status=%s%s%s%s%s%s%s%s%s%s%s, errcnt=%d, actlen=%d pid=%02x,"
724 	    "addr=%d,endpt=%d,D=%d,maxlen=%d\n",
725 	    p,
726 	    (td_next & 1) ? "-T" : "",
727 	    (td_next & 2) ? "-Q" : "",
728 	    (td_next & 4) ? "-VF" : "",
729 	    (td_status & UHCI_TD_BITSTUFF) ? "-BITSTUFF" : "",
730 	    (td_status & UHCI_TD_CRCTO) ? "-CRCTO" : "",
731 	    (td_status & UHCI_TD_NAK) ? "-NAK" : "",
732 	    (td_status & UHCI_TD_BABBLE) ? "-BABBLE" : "",
733 	    (td_status & UHCI_TD_DBUFFER) ? "-DBUFFER" : "",
734 	    (td_status & UHCI_TD_STALLED) ? "-STALLED" : "",
735 	    (td_status & UHCI_TD_ACTIVE) ? "-ACTIVE" : "",
736 	    (td_status & UHCI_TD_IOC) ? "-IOC" : "",
737 	    (td_status & UHCI_TD_IOS) ? "-IOS" : "",
738 	    (td_status & UHCI_TD_LS) ? "-LS" : "",
739 	    (td_status & UHCI_TD_SPD) ? "-SPD" : "",
740 	    UHCI_TD_GET_ERRCNT(td_status),
741 	    UHCI_TD_GET_ACTLEN(td_status),
742 	    UHCI_TD_GET_PID(td_token),
743 	    UHCI_TD_GET_DEVADDR(td_token),
744 	    UHCI_TD_GET_ENDPT(td_token),
745 	    UHCI_TD_GET_DT(td_token),
746 	    UHCI_TD_GET_MAXLEN(td_token));
747 
748 	return (temp);
749 }
750 
751 static uint8_t
752 uhci_dump_qh(uhci_qh_t *sqh)
753 {
754 	uint8_t temp;
755 	uint32_t qh_h_next;
756 	uint32_t qh_e_next;
757 
758 	usb2_pc_cpu_invalidate(sqh->page_cache);
759 
760 	qh_h_next = le32toh(sqh->qh_h_next);
761 	qh_e_next = le32toh(sqh->qh_e_next);
762 
763 	DPRINTFN(0, "QH(%p) at 0x%08x: h_next=0x%08x e_next=0x%08x\n", sqh,
764 	    le32toh(sqh->qh_self), qh_h_next, qh_e_next);
765 
766 	temp = ((((sqh->h_next != NULL) && !(qh_h_next & UHCI_PTR_T)) ? 1 : 0) |
767 	    (((sqh->e_next != NULL) && !(qh_e_next & UHCI_PTR_T)) ? 2 : 0));
768 
769 	return (temp);
770 }
771 
772 static void
773 uhci_dump_all(uhci_softc_t *sc)
774 {
775 	uhci_dumpregs(sc);
776 	uhci_dump_qh(sc->sc_ls_ctl_p_last);
777 	uhci_dump_qh(sc->sc_fs_ctl_p_last);
778 	uhci_dump_qh(sc->sc_bulk_p_last);
779 	uhci_dump_qh(sc->sc_last_qh_p);
780 }
781 
782 static void
783 uhci_dump_qhs(uhci_qh_t *sqh)
784 {
785 	uint8_t temp;
786 
787 	temp = uhci_dump_qh(sqh);
788 
789 	/*
790 	 * uhci_dump_qhs displays all the QHs and TDs from the given QH
791 	 * onwards Traverses sideways first, then down.
792 	 *
793 	 * QH1 QH2 No QH TD2.1 TD2.2 TD1.1 etc.
794 	 *
795 	 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
796 	 */
797 
798 	if (temp & 1)
799 		uhci_dump_qhs(sqh->h_next);
800 	else
801 		DPRINTF("No QH\n");
802 
803 	if (temp & 2)
804 		uhci_dump_tds(sqh->e_next);
805 	else
806 		DPRINTF("No TD\n");
807 }
808 
809 static void
810 uhci_dump_tds(uhci_td_t *td)
811 {
812 	for (;
813 	    td != NULL;
814 	    td = td->obj_next) {
815 		if (uhci_dump_td(td)) {
816 			break;
817 		}
818 	}
819 }
820 
821 #endif
822 
823 /*
824  * Let the last QH loop back to the full speed control transfer QH.
825  * This is what intel calls "bandwidth reclamation" and improves
826  * USB performance a lot for some devices.
827  * If we are already looping, just count it.
828  */
829 static void
830 uhci_add_loop(uhci_softc_t *sc)
831 {
832 	struct uhci_qh *qh_lst;
833 	struct uhci_qh *qh_rec;
834 
835 #if USB_DEBUG
836 	if (uhcinoloop) {
837 		return;
838 	}
839 #endif
840 	if (++(sc->sc_loops) == 1) {
841 		DPRINTFN(6, "add\n");
842 
843 		qh_lst = sc->sc_last_qh_p;
844 		qh_rec = sc->sc_reclaim_qh_p;
845 
846 		/* NOTE: we don't loop back the soft pointer */
847 
848 		qh_lst->qh_h_next = qh_rec->qh_self;
849 		usb2_pc_cpu_flush(qh_lst->page_cache);
850 	}
851 }
852 
853 static void
854 uhci_rem_loop(uhci_softc_t *sc)
855 {
856 	struct uhci_qh *qh_lst;
857 
858 #if USB_DEBUG
859 	if (uhcinoloop) {
860 		return;
861 	}
862 #endif
863 	if (--(sc->sc_loops) == 0) {
864 		DPRINTFN(6, "remove\n");
865 
866 		qh_lst = sc->sc_last_qh_p;
867 		qh_lst->qh_h_next = htole32(UHCI_PTR_T);
868 		usb2_pc_cpu_flush(qh_lst->page_cache);
869 	}
870 }
871 
872 static void
873 uhci_transfer_intr_enqueue(struct usb2_xfer *xfer)
874 {
875 	/* check for early completion */
876 	if (uhci_check_transfer(xfer)) {
877 		return;
878 	}
879 	/* put transfer on interrupt queue */
880 	usb2_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
881 
882 	/* start timeout, if any */
883 	if (xfer->timeout != 0) {
884 		usb2_transfer_timeout_ms(xfer, &uhci_timeout, xfer->timeout);
885 	}
886 }
887 
888 #define	UHCI_APPEND_TD(std,last) (last) = _uhci_append_td(std,last)
889 static uhci_td_t *
890 _uhci_append_td(uhci_td_t *std, uhci_td_t *last)
891 {
892 	DPRINTFN(11, "%p to %p\n", std, last);
893 
894 	/* (sc->sc_bus.mtx) must be locked */
895 
896 	std->next = last->next;
897 	std->td_next = last->td_next;
898 
899 	std->prev = last;
900 
901 	usb2_pc_cpu_flush(std->page_cache);
902 
903 	/*
904 	 * the last->next->prev is never followed: std->next->prev = std;
905 	 */
906 	last->next = std;
907 	last->td_next = std->td_self;
908 
909 	usb2_pc_cpu_flush(last->page_cache);
910 
911 	return (std);
912 }
913 
914 #define	UHCI_APPEND_QH(sqh,last) (last) = _uhci_append_qh(sqh,last)
915 static uhci_qh_t *
916 _uhci_append_qh(uhci_qh_t *sqh, uhci_qh_t *last)
917 {
918 	DPRINTFN(11, "%p to %p\n", sqh, last);
919 
920 	if (sqh->h_prev != NULL) {
921 		/* should not happen */
922 		DPRINTFN(0, "QH already linked!\n");
923 		return (last);
924 	}
925 	/* (sc->sc_bus.mtx) must be locked */
926 
927 	sqh->h_next = last->h_next;
928 	sqh->qh_h_next = last->qh_h_next;
929 
930 	sqh->h_prev = last;
931 
932 	usb2_pc_cpu_flush(sqh->page_cache);
933 
934 	/*
935 	 * The "last->h_next->h_prev" is never followed:
936 	 *
937 	 * "sqh->h_next->h_prev" = sqh;
938 	 */
939 
940 	last->h_next = sqh;
941 	last->qh_h_next = sqh->qh_self;
942 
943 	usb2_pc_cpu_flush(last->page_cache);
944 
945 	return (sqh);
946 }
947 
948 /**/
949 
950 #define	UHCI_REMOVE_TD(std,last) (last) = _uhci_remove_td(std,last)
951 static uhci_td_t *
952 _uhci_remove_td(uhci_td_t *std, uhci_td_t *last)
953 {
954 	DPRINTFN(11, "%p from %p\n", std, last);
955 
956 	/* (sc->sc_bus.mtx) must be locked */
957 
958 	std->prev->next = std->next;
959 	std->prev->td_next = std->td_next;
960 
961 	usb2_pc_cpu_flush(std->prev->page_cache);
962 
963 	if (std->next) {
964 		std->next->prev = std->prev;
965 		usb2_pc_cpu_flush(std->next->page_cache);
966 	}
967 	return ((last == std) ? std->prev : last);
968 }
969 
970 #define	UHCI_REMOVE_QH(sqh,last) (last) = _uhci_remove_qh(sqh,last)
971 static uhci_qh_t *
972 _uhci_remove_qh(uhci_qh_t *sqh, uhci_qh_t *last)
973 {
974 	DPRINTFN(11, "%p from %p\n", sqh, last);
975 
976 	/* (sc->sc_bus.mtx) must be locked */
977 
978 	/* only remove if not removed from a queue */
979 	if (sqh->h_prev) {
980 
981 		sqh->h_prev->h_next = sqh->h_next;
982 		sqh->h_prev->qh_h_next = sqh->qh_h_next;
983 
984 		usb2_pc_cpu_flush(sqh->h_prev->page_cache);
985 
986 		if (sqh->h_next) {
987 			sqh->h_next->h_prev = sqh->h_prev;
988 			usb2_pc_cpu_flush(sqh->h_next->page_cache);
989 		}
990 		last = ((last == sqh) ? sqh->h_prev : last);
991 
992 		sqh->h_prev = 0;
993 
994 		usb2_pc_cpu_flush(sqh->page_cache);
995 	}
996 	return (last);
997 }
998 
999 static void
1000 uhci_isoc_done(uhci_softc_t *sc, struct usb2_xfer *xfer)
1001 {
1002 	struct usb2_page_search res;
1003 	uint32_t nframes = xfer->nframes;
1004 	uint32_t status;
1005 	uint32_t offset = 0;
1006 	uint32_t *plen = xfer->frlengths;
1007 	uint16_t len = 0;
1008 	uhci_td_t *td = xfer->td_transfer_first;
1009 	uhci_td_t **pp_last = &sc->sc_isoc_p_last[xfer->qh_pos];
1010 
1011 	DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
1012 	    xfer, xfer->pipe);
1013 
1014 	/* sync any DMA memory before doing fixups */
1015 
1016 	usb2_bdma_post_sync(xfer);
1017 
1018 	while (nframes--) {
1019 		if (td == NULL) {
1020 			panic("%s:%d: out of TD's\n",
1021 			    __FUNCTION__, __LINE__);
1022 		}
1023 		if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
1024 			pp_last = &sc->sc_isoc_p_last[0];
1025 		}
1026 #if USB_DEBUG
1027 		if (uhcidebug > 5) {
1028 			DPRINTF("isoc TD\n");
1029 			uhci_dump_td(td);
1030 		}
1031 #endif
1032 		usb2_pc_cpu_invalidate(td->page_cache);
1033 		status = le32toh(td->td_status);
1034 
1035 		len = UHCI_TD_GET_ACTLEN(status);
1036 
1037 		if (len > *plen) {
1038 			len = *plen;
1039 		}
1040 		if (td->fix_pc) {
1041 
1042 			usb2_get_page(td->fix_pc, 0, &res);
1043 
1044 			/* copy data from fixup location to real location */
1045 
1046 			usb2_pc_cpu_invalidate(td->fix_pc);
1047 
1048 			usb2_copy_in(xfer->frbuffers, offset,
1049 			    res.buffer, len);
1050 		}
1051 		offset += *plen;
1052 
1053 		*plen = len;
1054 
1055 		/* remove TD from schedule */
1056 		UHCI_REMOVE_TD(td, *pp_last);
1057 
1058 		pp_last++;
1059 		plen++;
1060 		td = td->obj_next;
1061 	}
1062 
1063 	xfer->aframes = xfer->nframes;
1064 }
1065 
1066 static usb2_error_t
1067 uhci_non_isoc_done_sub(struct usb2_xfer *xfer)
1068 {
1069 	struct usb2_page_search res;
1070 	uhci_td_t *td;
1071 	uhci_td_t *td_alt_next;
1072 	uint32_t status;
1073 	uint32_t token;
1074 	uint16_t len;
1075 
1076 	td = xfer->td_transfer_cache;
1077 	td_alt_next = td->alt_next;
1078 
1079 	if (xfer->aframes != xfer->nframes) {
1080 		xfer->frlengths[xfer->aframes] = 0;
1081 	}
1082 	while (1) {
1083 
1084 		usb2_pc_cpu_invalidate(td->page_cache);
1085 		status = le32toh(td->td_status);
1086 		token = le32toh(td->td_token);
1087 
1088 		/*
1089 	         * Verify the status and add
1090 	         * up the actual length:
1091 	         */
1092 
1093 		len = UHCI_TD_GET_ACTLEN(status);
1094 		if (len > td->len) {
1095 			/* should not happen */
1096 			DPRINTF("Invalid status length, "
1097 			    "0x%04x/0x%04x bytes\n", len, td->len);
1098 			status |= UHCI_TD_STALLED;
1099 
1100 		} else if ((xfer->aframes != xfer->nframes) && (len > 0)) {
1101 
1102 			if (td->fix_pc) {
1103 
1104 				usb2_get_page(td->fix_pc, 0, &res);
1105 
1106 				/*
1107 				 * copy data from fixup location to real
1108 				 * location
1109 				 */
1110 
1111 				usb2_pc_cpu_invalidate(td->fix_pc);
1112 
1113 				usb2_copy_in(xfer->frbuffers + xfer->aframes,
1114 				    xfer->frlengths[xfer->aframes], res.buffer, len);
1115 			}
1116 			/* update actual length */
1117 
1118 			xfer->frlengths[xfer->aframes] += len;
1119 		}
1120 		/* Check for last transfer */
1121 		if (((void *)td) == xfer->td_transfer_last) {
1122 			td = NULL;
1123 			break;
1124 		}
1125 		if (status & UHCI_TD_STALLED) {
1126 			/* the transfer is finished */
1127 			td = NULL;
1128 			break;
1129 		}
1130 		/* Check for short transfer */
1131 		if (len != td->len) {
1132 			if (xfer->flags_int.short_frames_ok) {
1133 				/* follow alt next */
1134 				td = td->alt_next;
1135 			} else {
1136 				/* the transfer is finished */
1137 				td = NULL;
1138 			}
1139 			break;
1140 		}
1141 		td = td->obj_next;
1142 
1143 		if (td->alt_next != td_alt_next) {
1144 			/* this USB frame is complete */
1145 			break;
1146 		}
1147 	}
1148 
1149 	/* update transfer cache */
1150 
1151 	xfer->td_transfer_cache = td;
1152 
1153 	/* update data toggle */
1154 
1155 	xfer->pipe->toggle_next = (token & UHCI_TD_SET_DT(1)) ? 0 : 1;
1156 
1157 #if USB_DEBUG
1158 	if (status & UHCI_TD_ERROR) {
1159 		DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x "
1160 		    "status=%s%s%s%s%s%s%s%s%s%s%s\n",
1161 		    xfer->address, xfer->endpoint, xfer->aframes,
1162 		    (status & UHCI_TD_BITSTUFF) ? "[BITSTUFF]" : "",
1163 		    (status & UHCI_TD_CRCTO) ? "[CRCTO]" : "",
1164 		    (status & UHCI_TD_NAK) ? "[NAK]" : "",
1165 		    (status & UHCI_TD_BABBLE) ? "[BABBLE]" : "",
1166 		    (status & UHCI_TD_DBUFFER) ? "[DBUFFER]" : "",
1167 		    (status & UHCI_TD_STALLED) ? "[STALLED]" : "",
1168 		    (status & UHCI_TD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]",
1169 		    (status & UHCI_TD_IOC) ? "[IOC]" : "",
1170 		    (status & UHCI_TD_IOS) ? "[IOS]" : "",
1171 		    (status & UHCI_TD_LS) ? "[LS]" : "",
1172 		    (status & UHCI_TD_SPD) ? "[SPD]" : "");
1173 	}
1174 #endif
1175 	return (status & UHCI_TD_STALLED) ?
1176 	    USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION;
1177 }
1178 
1179 static void
1180 uhci_non_isoc_done(struct usb2_xfer *xfer)
1181 {
1182 	usb2_error_t err = 0;
1183 
1184 	DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
1185 	    xfer, xfer->pipe);
1186 
1187 #if USB_DEBUG
1188 	if (uhcidebug > 10) {
1189 		uhci_dump_tds(xfer->td_transfer_first);
1190 	}
1191 #endif
1192 
1193 	/* sync any DMA memory before doing fixups */
1194 
1195 	usb2_bdma_post_sync(xfer);
1196 
1197 	/* reset scanner */
1198 
1199 	xfer->td_transfer_cache = xfer->td_transfer_first;
1200 
1201 	if (xfer->flags_int.control_xfr) {
1202 		if (xfer->flags_int.control_hdr) {
1203 
1204 			err = uhci_non_isoc_done_sub(xfer);
1205 		}
1206 		xfer->aframes = 1;
1207 
1208 		if (xfer->td_transfer_cache == NULL) {
1209 			goto done;
1210 		}
1211 	}
1212 	while (xfer->aframes != xfer->nframes) {
1213 
1214 		err = uhci_non_isoc_done_sub(xfer);
1215 		xfer->aframes++;
1216 
1217 		if (xfer->td_transfer_cache == NULL) {
1218 			goto done;
1219 		}
1220 	}
1221 
1222 	if (xfer->flags_int.control_xfr &&
1223 	    !xfer->flags_int.control_act) {
1224 
1225 		err = uhci_non_isoc_done_sub(xfer);
1226 	}
1227 done:
1228 	uhci_device_done(xfer, err);
1229 }
1230 
1231 /*------------------------------------------------------------------------*
1232  *	uhci_check_transfer_sub
1233  *
1234  * The main purpose of this function is to update the data-toggle
1235  * in case it is wrong.
1236  *------------------------------------------------------------------------*/
1237 static void
1238 uhci_check_transfer_sub(struct usb2_xfer *xfer)
1239 {
1240 	uhci_qh_t *qh;
1241 	uhci_td_t *td;
1242 	uhci_td_t *td_alt_next;
1243 
1244 	uint32_t td_token;
1245 	uint32_t td_self;
1246 
1247 	td = xfer->td_transfer_cache;
1248 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1249 
1250 	td_token = td->obj_next->td_token;
1251 	td = td->alt_next;
1252 	xfer->td_transfer_cache = td;
1253 	td_self = td->td_self;
1254 	td_alt_next = td->alt_next;
1255 
1256 	if ((td->td_token ^ td_token) & htole32(UHCI_TD_SET_DT(1))) {
1257 
1258 		/*
1259 	         * The data toggle is wrong and
1260 	         * we need to switch it !
1261 	         */
1262 
1263 		while (1) {
1264 
1265 			td->td_token ^= htole32(UHCI_TD_SET_DT(1));
1266 			usb2_pc_cpu_flush(td->page_cache);
1267 
1268 			if (td == xfer->td_transfer_last) {
1269 				/* last transfer */
1270 				break;
1271 			}
1272 			td = td->obj_next;
1273 
1274 			if (td->alt_next != td_alt_next) {
1275 				/* next frame */
1276 				break;
1277 			}
1278 		}
1279 	}
1280 	/* update the QH */
1281 	qh->qh_e_next = td_self;
1282 	usb2_pc_cpu_flush(qh->page_cache);
1283 
1284 	DPRINTFN(13, "xfer=%p following alt next\n", xfer);
1285 }
1286 
1287 /*------------------------------------------------------------------------*
1288  *	uhci_check_transfer
1289  *
1290  * Return values:
1291  *    0: USB transfer is not finished
1292  * Else: USB transfer is finished
1293  *------------------------------------------------------------------------*/
1294 static uint8_t
1295 uhci_check_transfer(struct usb2_xfer *xfer)
1296 {
1297 	uint32_t status;
1298 	uint32_t token;
1299 	uhci_td_t *td;
1300 
1301 	DPRINTFN(16, "xfer=%p checking transfer\n", xfer);
1302 
1303 	if (xfer->pipe->methods == &uhci_device_isoc_methods) {
1304 		/* isochronous transfer */
1305 
1306 		td = xfer->td_transfer_last;
1307 
1308 		usb2_pc_cpu_invalidate(td->page_cache);
1309 		status = le32toh(td->td_status);
1310 
1311 		/* check also if the first is complete */
1312 
1313 		td = xfer->td_transfer_first;
1314 
1315 		usb2_pc_cpu_invalidate(td->page_cache);
1316 		status |= le32toh(td->td_status);
1317 
1318 		if (!(status & UHCI_TD_ACTIVE)) {
1319 			uhci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
1320 			goto transferred;
1321 		}
1322 	} else {
1323 		/* non-isochronous transfer */
1324 
1325 		/*
1326 		 * check whether there is an error somewhere
1327 		 * in the middle, or whether there was a short
1328 		 * packet (SPD and not ACTIVE)
1329 		 */
1330 		td = xfer->td_transfer_cache;
1331 
1332 		while (1) {
1333 			usb2_pc_cpu_invalidate(td->page_cache);
1334 			status = le32toh(td->td_status);
1335 			token = le32toh(td->td_token);
1336 
1337 			/*
1338 			 * if there is an active TD the transfer isn't done
1339 			 */
1340 			if (status & UHCI_TD_ACTIVE) {
1341 				/* update cache */
1342 				xfer->td_transfer_cache = td;
1343 				goto done;
1344 			}
1345 			/*
1346 			 * last transfer descriptor makes the transfer done
1347 			 */
1348 			if (((void *)td) == xfer->td_transfer_last) {
1349 				break;
1350 			}
1351 			/*
1352 			 * any kind of error makes the transfer done
1353 			 */
1354 			if (status & UHCI_TD_STALLED) {
1355 				break;
1356 			}
1357 			/*
1358 			 * check if we reached the last packet
1359 			 * or if there is a short packet:
1360 			 */
1361 			if ((td->td_next == htole32(UHCI_PTR_T)) ||
1362 			    (UHCI_TD_GET_ACTLEN(status) < td->len)) {
1363 
1364 				if (xfer->flags_int.short_frames_ok) {
1365 					/* follow alt next */
1366 					if (td->alt_next) {
1367 						/* update cache */
1368 						xfer->td_transfer_cache = td;
1369 						uhci_check_transfer_sub(xfer);
1370 						goto done;
1371 					}
1372 				}
1373 				/* transfer is done */
1374 				break;
1375 			}
1376 			td = td->obj_next;
1377 		}
1378 		uhci_non_isoc_done(xfer);
1379 		goto transferred;
1380 	}
1381 
1382 done:
1383 	DPRINTFN(13, "xfer=%p is still active\n", xfer);
1384 	return (0);
1385 
1386 transferred:
1387 	return (1);
1388 }
1389 
1390 static void
1391 uhci_interrupt_poll(uhci_softc_t *sc)
1392 {
1393 	struct usb2_xfer *xfer;
1394 
1395 repeat:
1396 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1397 		/*
1398 		 * check if transfer is transferred
1399 		 */
1400 		if (uhci_check_transfer(xfer)) {
1401 			/* queue has been modified */
1402 			goto repeat;
1403 		}
1404 	}
1405 }
1406 
1407 /*------------------------------------------------------------------------*
1408  *	uhci_interrupt - UHCI interrupt handler
1409  *
1410  * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
1411  * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
1412  * is present !
1413  *------------------------------------------------------------------------*/
1414 void
1415 uhci_interrupt(uhci_softc_t *sc)
1416 {
1417 	uint32_t status;
1418 
1419 	USB_BUS_LOCK(&sc->sc_bus);
1420 
1421 	DPRINTFN(16, "real interrupt\n");
1422 
1423 #if USB_DEBUG
1424 	if (uhcidebug > 15) {
1425 		uhci_dumpregs(sc);
1426 	}
1427 #endif
1428 	status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1429 	if (status == 0) {
1430 		/* the interrupt was not for us */
1431 		goto done;
1432 	}
1433 	if (status & (UHCI_STS_RD | UHCI_STS_HSE |
1434 	    UHCI_STS_HCPE | UHCI_STS_HCH)) {
1435 
1436 		if (status & UHCI_STS_RD) {
1437 #if USB_DEBUG
1438 			printf("%s: resume detect\n",
1439 			    __FUNCTION__);
1440 #endif
1441 		}
1442 		if (status & UHCI_STS_HSE) {
1443 			printf("%s: host system error\n",
1444 			    __FUNCTION__);
1445 		}
1446 		if (status & UHCI_STS_HCPE) {
1447 			printf("%s: host controller process error\n",
1448 			    __FUNCTION__);
1449 		}
1450 		if (status & UHCI_STS_HCH) {
1451 			/* no acknowledge needed */
1452 			DPRINTF("%s: host controller halted\n",
1453 			    __FUNCTION__);
1454 #if USB_DEBUG
1455 			if (uhcidebug > 0) {
1456 				uhci_dump_all(sc);
1457 			}
1458 #endif
1459 		}
1460 	}
1461 	/* get acknowledge bits */
1462 	status &= (UHCI_STS_USBINT |
1463 	    UHCI_STS_USBEI |
1464 	    UHCI_STS_RD |
1465 	    UHCI_STS_HSE |
1466 	    UHCI_STS_HCPE);
1467 
1468 	if (status == 0) {
1469 		/* nothing to acknowledge */
1470 		goto done;
1471 	}
1472 	/* acknowledge interrupts */
1473 	UWRITE2(sc, UHCI_STS, status);
1474 
1475 	/* poll all the USB transfers */
1476 	uhci_interrupt_poll(sc);
1477 
1478 done:
1479 	USB_BUS_UNLOCK(&sc->sc_bus);
1480 }
1481 
1482 /*
1483  * called when a request does not complete
1484  */
1485 static void
1486 uhci_timeout(void *arg)
1487 {
1488 	struct usb2_xfer *xfer = arg;
1489 
1490 	DPRINTF("xfer=%p\n", xfer);
1491 
1492 	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1493 
1494 	/* transfer is transferred */
1495 	uhci_device_done(xfer, USB_ERR_TIMEOUT);
1496 }
1497 
1498 static void
1499 uhci_do_poll(struct usb2_bus *bus)
1500 {
1501 	struct uhci_softc *sc = UHCI_BUS2SC(bus);
1502 
1503 	USB_BUS_LOCK(&sc->sc_bus);
1504 	uhci_interrupt_poll(sc);
1505 	uhci_root_ctrl_poll(sc);
1506 	USB_BUS_UNLOCK(&sc->sc_bus);
1507 }
1508 
1509 static void
1510 uhci_setup_standard_chain_sub(struct uhci_std_temp *temp)
1511 {
1512 	uhci_td_t *td;
1513 	uhci_td_t *td_next;
1514 	uhci_td_t *td_alt_next;
1515 	uint32_t average;
1516 	uint32_t len_old;
1517 	uint8_t shortpkt_old;
1518 	uint8_t precompute;
1519 
1520 	td_alt_next = NULL;
1521 	shortpkt_old = temp->shortpkt;
1522 	len_old = temp->len;
1523 	precompute = 1;
1524 
1525 	/* software is used to detect short incoming transfers */
1526 
1527 	if ((temp->td_token & htole32(UHCI_TD_PID)) == htole32(UHCI_TD_PID_IN)) {
1528 		temp->td_status |= htole32(UHCI_TD_SPD);
1529 	} else {
1530 		temp->td_status &= ~htole32(UHCI_TD_SPD);
1531 	}
1532 
1533 	temp->ml.buf_offset = 0;
1534 
1535 restart:
1536 
1537 	temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
1538 	temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->average));
1539 
1540 	td = temp->td;
1541 	td_next = temp->td_next;
1542 
1543 	while (1) {
1544 
1545 		if (temp->len == 0) {
1546 
1547 			if (temp->shortpkt) {
1548 				break;
1549 			}
1550 			/* send a Zero Length Packet, ZLP, last */
1551 
1552 			temp->shortpkt = 1;
1553 			temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(0));
1554 			average = 0;
1555 
1556 		} else {
1557 
1558 			average = temp->average;
1559 
1560 			if (temp->len < average) {
1561 				temp->shortpkt = 1;
1562 				temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0));
1563 				temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->len));
1564 				average = temp->len;
1565 			}
1566 		}
1567 
1568 		if (td_next == NULL) {
1569 			panic("%s: out of UHCI transfer descriptors!", __FUNCTION__);
1570 		}
1571 		/* get next TD */
1572 
1573 		td = td_next;
1574 		td_next = td->obj_next;
1575 
1576 		/* check if we are pre-computing */
1577 
1578 		if (precompute) {
1579 
1580 			/* update remaining length */
1581 
1582 			temp->len -= average;
1583 
1584 			continue;
1585 		}
1586 		/* fill out current TD */
1587 
1588 		td->td_status = temp->td_status;
1589 		td->td_token = temp->td_token;
1590 
1591 		/* update data toggle */
1592 
1593 		temp->td_token ^= htole32(UHCI_TD_SET_DT(1));
1594 
1595 		if (average == 0) {
1596 
1597 			td->len = 0;
1598 			td->td_buffer = 0;
1599 			td->fix_pc = NULL;
1600 
1601 		} else {
1602 
1603 			/* update remaining length */
1604 
1605 			temp->len -= average;
1606 
1607 			td->len = average;
1608 
1609 			/* fill out buffer pointer and do fixup, if any */
1610 
1611 			uhci_mem_layout_fixup(&temp->ml, td);
1612 		}
1613 
1614 		td->alt_next = td_alt_next;
1615 
1616 		if ((td_next == td_alt_next) && temp->setup_alt_next) {
1617 			/* we need to receive these frames one by one ! */
1618 			td->td_status |= htole32(UHCI_TD_IOC);
1619 			td->td_next = htole32(UHCI_PTR_T);
1620 		} else {
1621 			if (td_next) {
1622 				/* link the current TD with the next one */
1623 				td->td_next = td_next->td_self;
1624 			}
1625 		}
1626 
1627 		usb2_pc_cpu_flush(td->page_cache);
1628 	}
1629 
1630 	if (precompute) {
1631 		precompute = 0;
1632 
1633 		/* setup alt next pointer, if any */
1634 		if (temp->short_frames_ok) {
1635 			if (temp->setup_alt_next) {
1636 				td_alt_next = td_next;
1637 			}
1638 		} else {
1639 			/* we use this field internally */
1640 			td_alt_next = td_next;
1641 		}
1642 
1643 		/* restore */
1644 		temp->shortpkt = shortpkt_old;
1645 		temp->len = len_old;
1646 		goto restart;
1647 	}
1648 	temp->td = td;
1649 	temp->td_next = td_next;
1650 }
1651 
1652 static uhci_td_t *
1653 uhci_setup_standard_chain(struct usb2_xfer *xfer)
1654 {
1655 	struct uhci_std_temp temp;
1656 	uhci_td_t *td;
1657 	uint32_t x;
1658 
1659 	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1660 	    xfer->address, UE_GET_ADDR(xfer->endpoint),
1661 	    xfer->sumlen, usb2_get_speed(xfer->xroot->udev));
1662 
1663 	temp.average = xfer->max_frame_size;
1664 	temp.max_frame_size = xfer->max_frame_size;
1665 
1666 	/* toggle the DMA set we are using */
1667 	xfer->flags_int.curr_dma_set ^= 1;
1668 
1669 	/* get next DMA set */
1670 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
1671 	xfer->td_transfer_first = td;
1672 	xfer->td_transfer_cache = td;
1673 
1674 	temp.td = NULL;
1675 	temp.td_next = td;
1676 	temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1677 	temp.short_frames_ok = xfer->flags_int.short_frames_ok;
1678 
1679 	uhci_mem_layout_init(&temp.ml, xfer);
1680 
1681 	temp.td_status =
1682 	    htole32(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) |
1683 	    UHCI_TD_ACTIVE));
1684 
1685 	if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1686 		temp.td_status |= htole32(UHCI_TD_LS);
1687 	}
1688 	temp.td_token =
1689 	    htole32(UHCI_TD_SET_ENDPT(xfer->endpoint) |
1690 	    UHCI_TD_SET_DEVADDR(xfer->address));
1691 
1692 	if (xfer->pipe->toggle_next) {
1693 		/* DATA1 is next */
1694 		temp.td_token |= htole32(UHCI_TD_SET_DT(1));
1695 	}
1696 	/* check if we should prepend a setup message */
1697 
1698 	if (xfer->flags_int.control_xfr) {
1699 
1700 		if (xfer->flags_int.control_hdr) {
1701 
1702 			temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1703 			    UHCI_TD_SET_ENDPT(0xF));
1704 			temp.td_token |= htole32(UHCI_TD_PID_SETUP |
1705 			    UHCI_TD_SET_DT(0));
1706 
1707 			temp.len = xfer->frlengths[0];
1708 			temp.ml.buf_pc = xfer->frbuffers + 0;
1709 			temp.shortpkt = temp.len ? 1 : 0;
1710 
1711 			uhci_setup_standard_chain_sub(&temp);
1712 		}
1713 		x = 1;
1714 	} else {
1715 		x = 0;
1716 	}
1717 
1718 	while (x != xfer->nframes) {
1719 
1720 		/* DATA0 / DATA1 message */
1721 
1722 		temp.len = xfer->frlengths[x];
1723 		temp.ml.buf_pc = xfer->frbuffers + x;
1724 
1725 		x++;
1726 
1727 		if (x == xfer->nframes) {
1728 			temp.setup_alt_next = 0;
1729 		}
1730 		/*
1731 		 * Keep previous data toggle,
1732 		 * device address and endpoint number:
1733 		 */
1734 
1735 		temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1736 		    UHCI_TD_SET_ENDPT(0xF) |
1737 		    UHCI_TD_SET_DT(1));
1738 
1739 		if (temp.len == 0) {
1740 
1741 			/* make sure that we send an USB packet */
1742 
1743 			temp.shortpkt = 0;
1744 
1745 		} else {
1746 
1747 			/* regular data transfer */
1748 
1749 			temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1750 		}
1751 
1752 		/* set endpoint direction */
1753 
1754 		temp.td_token |=
1755 		    (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) ?
1756 		    htole32(UHCI_TD_PID_IN) :
1757 		    htole32(UHCI_TD_PID_OUT);
1758 
1759 		uhci_setup_standard_chain_sub(&temp);
1760 	}
1761 
1762 	/* check if we should append a status stage */
1763 
1764 	if (xfer->flags_int.control_xfr &&
1765 	    !xfer->flags_int.control_act) {
1766 
1767 		/*
1768 		 * send a DATA1 message and reverse the current endpoint
1769 		 * direction
1770 		 */
1771 
1772 		temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) |
1773 		    UHCI_TD_SET_ENDPT(0xF) |
1774 		    UHCI_TD_SET_DT(1));
1775 		temp.td_token |=
1776 		    (UE_GET_DIR(xfer->endpoint) == UE_DIR_OUT) ?
1777 		    htole32(UHCI_TD_PID_IN | UHCI_TD_SET_DT(1)) :
1778 		    htole32(UHCI_TD_PID_OUT | UHCI_TD_SET_DT(1));
1779 
1780 		temp.len = 0;
1781 		temp.ml.buf_pc = NULL;
1782 		temp.shortpkt = 0;
1783 
1784 		uhci_setup_standard_chain_sub(&temp);
1785 	}
1786 	td = temp.td;
1787 
1788 	td->td_next = htole32(UHCI_PTR_T);
1789 
1790 	/* set interrupt bit */
1791 
1792 	td->td_status |= htole32(UHCI_TD_IOC);
1793 
1794 	usb2_pc_cpu_flush(td->page_cache);
1795 
1796 	/* must have at least one frame! */
1797 
1798 	xfer->td_transfer_last = td;
1799 
1800 #if USB_DEBUG
1801 	if (uhcidebug > 8) {
1802 		DPRINTF("nexttog=%d; data before transfer:\n",
1803 		    xfer->pipe->toggle_next);
1804 		uhci_dump_tds(xfer->td_transfer_first);
1805 	}
1806 #endif
1807 	return (xfer->td_transfer_first);
1808 }
1809 
1810 /* NOTE: "done" can be run two times in a row,
1811  * from close and from interrupt
1812  */
1813 
1814 static void
1815 uhci_device_done(struct usb2_xfer *xfer, usb2_error_t error)
1816 {
1817 	struct usb2_pipe_methods *methods = xfer->pipe->methods;
1818 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1819 	uhci_qh_t *qh;
1820 
1821 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1822 
1823 	DPRINTFN(2, "xfer=%p, pipe=%p, error=%d\n",
1824 	    xfer, xfer->pipe, error);
1825 
1826 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1827 	if (qh) {
1828 		usb2_pc_cpu_invalidate(qh->page_cache);
1829 	}
1830 	if (xfer->flags_int.bandwidth_reclaimed) {
1831 		xfer->flags_int.bandwidth_reclaimed = 0;
1832 		uhci_rem_loop(sc);
1833 	}
1834 	if (methods == &uhci_device_bulk_methods) {
1835 		UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
1836 	}
1837 	if (methods == &uhci_device_ctrl_methods) {
1838 		if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1839 			UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
1840 		} else {
1841 			UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
1842 		}
1843 	}
1844 	if (methods == &uhci_device_intr_methods) {
1845 		UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
1846 	}
1847 	/*
1848 	 * Only finish isochronous transfers once
1849 	 * which will update "xfer->frlengths".
1850 	 */
1851 	if (xfer->td_transfer_first &&
1852 	    xfer->td_transfer_last) {
1853 		if (methods == &uhci_device_isoc_methods) {
1854 			uhci_isoc_done(sc, xfer);
1855 		}
1856 		xfer->td_transfer_first = NULL;
1857 		xfer->td_transfer_last = NULL;
1858 	}
1859 	/* dequeue transfer and start next transfer */
1860 	usb2_transfer_done(xfer, error);
1861 }
1862 
1863 /*------------------------------------------------------------------------*
1864  * uhci bulk support
1865  *------------------------------------------------------------------------*/
1866 static void
1867 uhci_device_bulk_open(struct usb2_xfer *xfer)
1868 {
1869 	return;
1870 }
1871 
1872 static void
1873 uhci_device_bulk_close(struct usb2_xfer *xfer)
1874 {
1875 	uhci_device_done(xfer, USB_ERR_CANCELLED);
1876 }
1877 
1878 static void
1879 uhci_device_bulk_enter(struct usb2_xfer *xfer)
1880 {
1881 	return;
1882 }
1883 
1884 static void
1885 uhci_device_bulk_start(struct usb2_xfer *xfer)
1886 {
1887 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1888 	uhci_td_t *td;
1889 	uhci_qh_t *qh;
1890 
1891 	/* setup TD's */
1892 	td = uhci_setup_standard_chain(xfer);
1893 
1894 	/* setup QH */
1895 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1896 
1897 	qh->e_next = td;
1898 	qh->qh_e_next = td->td_self;
1899 
1900 	if (xfer->xroot->udev->pwr_save.suspended == 0) {
1901 		UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
1902 		uhci_add_loop(sc);
1903 		xfer->flags_int.bandwidth_reclaimed = 1;
1904 	} else {
1905 		usb2_pc_cpu_flush(qh->page_cache);
1906 	}
1907 
1908 	/* put transfer on interrupt queue */
1909 	uhci_transfer_intr_enqueue(xfer);
1910 }
1911 
1912 struct usb2_pipe_methods uhci_device_bulk_methods =
1913 {
1914 	.open = uhci_device_bulk_open,
1915 	.close = uhci_device_bulk_close,
1916 	.enter = uhci_device_bulk_enter,
1917 	.start = uhci_device_bulk_start,
1918 	.enter_is_cancelable = 1,
1919 	.start_is_cancelable = 1,
1920 };
1921 
1922 /*------------------------------------------------------------------------*
1923  * uhci control support
1924  *------------------------------------------------------------------------*/
1925 static void
1926 uhci_device_ctrl_open(struct usb2_xfer *xfer)
1927 {
1928 	return;
1929 }
1930 
1931 static void
1932 uhci_device_ctrl_close(struct usb2_xfer *xfer)
1933 {
1934 	uhci_device_done(xfer, USB_ERR_CANCELLED);
1935 }
1936 
1937 static void
1938 uhci_device_ctrl_enter(struct usb2_xfer *xfer)
1939 {
1940 	return;
1941 }
1942 
1943 static void
1944 uhci_device_ctrl_start(struct usb2_xfer *xfer)
1945 {
1946 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1947 	uhci_qh_t *qh;
1948 	uhci_td_t *td;
1949 
1950 	/* setup TD's */
1951 	td = uhci_setup_standard_chain(xfer);
1952 
1953 	/* setup QH */
1954 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
1955 
1956 	qh->e_next = td;
1957 	qh->qh_e_next = td->td_self;
1958 
1959 	/*
1960 	 * NOTE: some devices choke on bandwidth- reclamation for control
1961 	 * transfers
1962 	 */
1963 	if (xfer->xroot->udev->pwr_save.suspended == 0) {
1964 		if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1965 			UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
1966 		} else {
1967 			UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
1968 		}
1969 	} else {
1970 		usb2_pc_cpu_flush(qh->page_cache);
1971 	}
1972 	/* put transfer on interrupt queue */
1973 	uhci_transfer_intr_enqueue(xfer);
1974 }
1975 
1976 struct usb2_pipe_methods uhci_device_ctrl_methods =
1977 {
1978 	.open = uhci_device_ctrl_open,
1979 	.close = uhci_device_ctrl_close,
1980 	.enter = uhci_device_ctrl_enter,
1981 	.start = uhci_device_ctrl_start,
1982 	.enter_is_cancelable = 1,
1983 	.start_is_cancelable = 1,
1984 };
1985 
1986 /*------------------------------------------------------------------------*
1987  * uhci interrupt support
1988  *------------------------------------------------------------------------*/
1989 static void
1990 uhci_device_intr_open(struct usb2_xfer *xfer)
1991 {
1992 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
1993 	uint16_t best;
1994 	uint16_t bit;
1995 	uint16_t x;
1996 
1997 	best = 0;
1998 	bit = UHCI_IFRAMELIST_COUNT / 2;
1999 	while (bit) {
2000 		if (xfer->interval >= bit) {
2001 			x = bit;
2002 			best = bit;
2003 			while (x & bit) {
2004 				if (sc->sc_intr_stat[x] <
2005 				    sc->sc_intr_stat[best]) {
2006 					best = x;
2007 				}
2008 				x++;
2009 			}
2010 			break;
2011 		}
2012 		bit >>= 1;
2013 	}
2014 
2015 	sc->sc_intr_stat[best]++;
2016 	xfer->qh_pos = best;
2017 
2018 	DPRINTFN(3, "best=%d interval=%d\n",
2019 	    best, xfer->interval);
2020 }
2021 
2022 static void
2023 uhci_device_intr_close(struct usb2_xfer *xfer)
2024 {
2025 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2026 
2027 	sc->sc_intr_stat[xfer->qh_pos]--;
2028 
2029 	uhci_device_done(xfer, USB_ERR_CANCELLED);
2030 }
2031 
2032 static void
2033 uhci_device_intr_enter(struct usb2_xfer *xfer)
2034 {
2035 	return;
2036 }
2037 
2038 static void
2039 uhci_device_intr_start(struct usb2_xfer *xfer)
2040 {
2041 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2042 	uhci_qh_t *qh;
2043 	uhci_td_t *td;
2044 
2045 	/* setup TD's */
2046 	td = uhci_setup_standard_chain(xfer);
2047 
2048 	/* setup QH */
2049 	qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
2050 
2051 	qh->e_next = td;
2052 	qh->qh_e_next = td->td_self;
2053 
2054 	if (xfer->xroot->udev->pwr_save.suspended == 0) {
2055 
2056 		/* enter QHs into the controller data structures */
2057 		UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
2058 
2059 	} else {
2060 		usb2_pc_cpu_flush(qh->page_cache);
2061 	}
2062 
2063 	/* put transfer on interrupt queue */
2064 	uhci_transfer_intr_enqueue(xfer);
2065 }
2066 
2067 struct usb2_pipe_methods uhci_device_intr_methods =
2068 {
2069 	.open = uhci_device_intr_open,
2070 	.close = uhci_device_intr_close,
2071 	.enter = uhci_device_intr_enter,
2072 	.start = uhci_device_intr_start,
2073 	.enter_is_cancelable = 1,
2074 	.start_is_cancelable = 1,
2075 };
2076 
2077 /*------------------------------------------------------------------------*
2078  * uhci isochronous support
2079  *------------------------------------------------------------------------*/
2080 static void
2081 uhci_device_isoc_open(struct usb2_xfer *xfer)
2082 {
2083 	uhci_td_t *td;
2084 	uint32_t td_token;
2085 	uint8_t ds;
2086 
2087 	td_token =
2088 	    (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) ?
2089 	    UHCI_TD_IN(0, xfer->endpoint, xfer->address, 0) :
2090 	    UHCI_TD_OUT(0, xfer->endpoint, xfer->address, 0);
2091 
2092 	td_token = htole32(td_token);
2093 
2094 	/* initialize all TD's */
2095 
2096 	for (ds = 0; ds != 2; ds++) {
2097 
2098 		for (td = xfer->td_start[ds]; td; td = td->obj_next) {
2099 
2100 			/* mark TD as inactive */
2101 			td->td_status = htole32(UHCI_TD_IOS);
2102 			td->td_token = td_token;
2103 
2104 			usb2_pc_cpu_flush(td->page_cache);
2105 		}
2106 	}
2107 }
2108 
2109 static void
2110 uhci_device_isoc_close(struct usb2_xfer *xfer)
2111 {
2112 	uhci_device_done(xfer, USB_ERR_CANCELLED);
2113 }
2114 
2115 static void
2116 uhci_device_isoc_enter(struct usb2_xfer *xfer)
2117 {
2118 	struct uhci_mem_layout ml;
2119 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2120 	uint32_t nframes;
2121 	uint32_t temp;
2122 	uint32_t *plen;
2123 
2124 #if USB_DEBUG
2125 	uint8_t once = 1;
2126 
2127 #endif
2128 	uhci_td_t *td;
2129 	uhci_td_t *td_last = NULL;
2130 	uhci_td_t **pp_last;
2131 
2132 	DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
2133 	    xfer, xfer->pipe->isoc_next, xfer->nframes);
2134 
2135 	nframes = UREAD2(sc, UHCI_FRNUM);
2136 
2137 	temp = (nframes - xfer->pipe->isoc_next) &
2138 	    (UHCI_VFRAMELIST_COUNT - 1);
2139 
2140 	if ((xfer->pipe->is_synced == 0) ||
2141 	    (temp < xfer->nframes)) {
2142 		/*
2143 		 * If there is data underflow or the pipe queue is empty we
2144 		 * schedule the transfer a few frames ahead of the current
2145 		 * frame position. Else two isochronous transfers might
2146 		 * overlap.
2147 		 */
2148 		xfer->pipe->isoc_next = (nframes + 3) & (UHCI_VFRAMELIST_COUNT - 1);
2149 		xfer->pipe->is_synced = 1;
2150 		DPRINTFN(3, "start next=%d\n", xfer->pipe->isoc_next);
2151 	}
2152 	/*
2153 	 * compute how many milliseconds the insertion is ahead of the
2154 	 * current frame position:
2155 	 */
2156 	temp = (xfer->pipe->isoc_next - nframes) &
2157 	    (UHCI_VFRAMELIST_COUNT - 1);
2158 
2159 	/*
2160 	 * pre-compute when the isochronous transfer will be finished:
2161 	 */
2162 	xfer->isoc_time_complete =
2163 	    usb2_isoc_time_expand(&sc->sc_bus, nframes) + temp +
2164 	    xfer->nframes;
2165 
2166 	/* get the real number of frames */
2167 
2168 	nframes = xfer->nframes;
2169 
2170 	uhci_mem_layout_init(&ml, xfer);
2171 
2172 	plen = xfer->frlengths;
2173 
2174 	/* toggle the DMA set we are using */
2175 	xfer->flags_int.curr_dma_set ^= 1;
2176 
2177 	/* get next DMA set */
2178 	td = xfer->td_start[xfer->flags_int.curr_dma_set];
2179 	xfer->td_transfer_first = td;
2180 
2181 	pp_last = &sc->sc_isoc_p_last[xfer->pipe->isoc_next];
2182 
2183 	/* store starting position */
2184 
2185 	xfer->qh_pos = xfer->pipe->isoc_next;
2186 
2187 	while (nframes--) {
2188 		if (td == NULL) {
2189 			panic("%s:%d: out of TD's\n",
2190 			    __FUNCTION__, __LINE__);
2191 		}
2192 		if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) {
2193 			pp_last = &sc->sc_isoc_p_last[0];
2194 		}
2195 		if (*plen > xfer->max_frame_size) {
2196 #if USB_DEBUG
2197 			if (once) {
2198 				once = 0;
2199 				printf("%s: frame length(%d) exceeds %d "
2200 				    "bytes (frame truncated)\n",
2201 				    __FUNCTION__, *plen,
2202 				    xfer->max_frame_size);
2203 			}
2204 #endif
2205 			*plen = xfer->max_frame_size;
2206 		}
2207 		/* reuse td_token from last transfer */
2208 
2209 		td->td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2210 		td->td_token |= htole32(UHCI_TD_SET_MAXLEN(*plen));
2211 
2212 		td->len = *plen;
2213 
2214 		if (td->len == 0) {
2215 			/*
2216 			 * Do not call "uhci_mem_layout_fixup()" when the
2217 			 * length is zero!
2218 			 */
2219 			td->td_buffer = 0;
2220 			td->fix_pc = NULL;
2221 
2222 		} else {
2223 
2224 			/* fill out buffer pointer and do fixup, if any */
2225 
2226 			uhci_mem_layout_fixup(&ml, td);
2227 
2228 		}
2229 
2230 		/* update status */
2231 		if (nframes == 0) {
2232 			td->td_status = htole32
2233 			    (UHCI_TD_ZERO_ACTLEN
2234 			    (UHCI_TD_SET_ERRCNT(0) |
2235 			    UHCI_TD_ACTIVE |
2236 			    UHCI_TD_IOS |
2237 			    UHCI_TD_IOC));
2238 		} else {
2239 			td->td_status = htole32
2240 			    (UHCI_TD_ZERO_ACTLEN
2241 			    (UHCI_TD_SET_ERRCNT(0) |
2242 			    UHCI_TD_ACTIVE |
2243 			    UHCI_TD_IOS));
2244 		}
2245 
2246 		usb2_pc_cpu_flush(td->page_cache);
2247 
2248 #if USB_DEBUG
2249 		if (uhcidebug > 5) {
2250 			DPRINTF("TD %d\n", nframes);
2251 			uhci_dump_td(td);
2252 		}
2253 #endif
2254 		/* insert TD into schedule */
2255 		UHCI_APPEND_TD(td, *pp_last);
2256 		pp_last++;
2257 
2258 		plen++;
2259 		td_last = td;
2260 		td = td->obj_next;
2261 	}
2262 
2263 	xfer->td_transfer_last = td_last;
2264 
2265 	/* update isoc_next */
2266 	xfer->pipe->isoc_next = (pp_last - &sc->sc_isoc_p_last[0]) &
2267 	    (UHCI_VFRAMELIST_COUNT - 1);
2268 }
2269 
2270 static void
2271 uhci_device_isoc_start(struct usb2_xfer *xfer)
2272 {
2273 	/* put transfer on interrupt queue */
2274 	uhci_transfer_intr_enqueue(xfer);
2275 }
2276 
2277 struct usb2_pipe_methods uhci_device_isoc_methods =
2278 {
2279 	.open = uhci_device_isoc_open,
2280 	.close = uhci_device_isoc_close,
2281 	.enter = uhci_device_isoc_enter,
2282 	.start = uhci_device_isoc_start,
2283 	.enter_is_cancelable = 1,
2284 	.start_is_cancelable = 1,
2285 };
2286 
2287 /*------------------------------------------------------------------------*
2288  * uhci root control support
2289  *------------------------------------------------------------------------*
2290  * simulate a hardware hub by handling
2291  * all the necessary requests
2292  *------------------------------------------------------------------------*/
2293 
2294 static void
2295 uhci_root_ctrl_open(struct usb2_xfer *xfer)
2296 {
2297 	return;
2298 }
2299 
2300 static void
2301 uhci_root_ctrl_close(struct usb2_xfer *xfer)
2302 {
2303 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2304 
2305 	if (sc->sc_root_ctrl.xfer == xfer) {
2306 		sc->sc_root_ctrl.xfer = NULL;
2307 	}
2308 	uhci_device_done(xfer, USB_ERR_CANCELLED);
2309 }
2310 
2311 /* data structures and routines
2312  * to emulate the root hub:
2313  */
2314 
2315 static const
2316 struct usb2_device_descriptor uhci_devd =
2317 {
2318 	sizeof(struct usb2_device_descriptor),
2319 	UDESC_DEVICE,			/* type */
2320 	{0x00, 0x01},			/* USB version */
2321 	UDCLASS_HUB,			/* class */
2322 	UDSUBCLASS_HUB,			/* subclass */
2323 	UDPROTO_FSHUB,			/* protocol */
2324 	64,				/* max packet */
2325 	{0}, {0}, {0x00, 0x01},		/* device id */
2326 	1, 2, 0,			/* string indicies */
2327 	1				/* # of configurations */
2328 };
2329 
2330 static const struct uhci_config_desc uhci_confd = {
2331 	.confd = {
2332 		.bLength = sizeof(struct usb2_config_descriptor),
2333 		.bDescriptorType = UDESC_CONFIG,
2334 		.wTotalLength[0] = sizeof(uhci_confd),
2335 		.bNumInterface = 1,
2336 		.bConfigurationValue = 1,
2337 		.iConfiguration = 0,
2338 		.bmAttributes = UC_SELF_POWERED,
2339 		.bMaxPower = 0		/* max power */
2340 	},
2341 
2342 	.ifcd = {
2343 		.bLength = sizeof(struct usb2_interface_descriptor),
2344 		.bDescriptorType = UDESC_INTERFACE,
2345 		.bNumEndpoints = 1,
2346 		.bInterfaceClass = UICLASS_HUB,
2347 		.bInterfaceSubClass = UISUBCLASS_HUB,
2348 		.bInterfaceProtocol = UIPROTO_FSHUB,
2349 	},
2350 
2351 	.endpd = {
2352 		.bLength = sizeof(struct usb2_endpoint_descriptor),
2353 		.bDescriptorType = UDESC_ENDPOINT,
2354 		.bEndpointAddress = UE_DIR_IN | UHCI_INTR_ENDPT,
2355 		.bmAttributes = UE_INTERRUPT,
2356 		.wMaxPacketSize[0] = 8,	/* max packet (63 ports) */
2357 		.bInterval = 255,
2358 	},
2359 };
2360 
2361 static const
2362 struct usb2_hub_descriptor_min uhci_hubd_piix =
2363 {
2364 	sizeof(uhci_hubd_piix),
2365 	UDESC_HUB,
2366 	2,
2367 	{UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0},
2368 	50,				/* power on to power good */
2369 	0,
2370 	{0x00},				/* both ports are removable */
2371 };
2372 
2373 /*
2374  * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also
2375  * enables the port, and also states that SET_FEATURE(PORT_ENABLE)
2376  * should not be used by the USB subsystem.  As we cannot issue a
2377  * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port
2378  * will be enabled as part of the reset.
2379  *
2380  * On the VT83C572, the port cannot be successfully enabled until the
2381  * outstanding "port enable change" and "connection status change"
2382  * events have been reset.
2383  */
2384 static usb2_error_t
2385 uhci_portreset(uhci_softc_t *sc, uint16_t index)
2386 {
2387 	uint16_t port;
2388 	uint16_t x;
2389 	uint8_t lim;
2390 
2391 	if (index == 1)
2392 		port = UHCI_PORTSC1;
2393 	else if (index == 2)
2394 		port = UHCI_PORTSC2;
2395 	else
2396 		return (USB_ERR_IOERROR);
2397 
2398 	/*
2399 	 * Before we do anything, turn on SOF messages on the USB
2400 	 * BUS. Some USB devices do not cope without them!
2401 	 */
2402   	if (!(UREAD2(sc, UHCI_CMD) & UHCI_CMD_RS)) {
2403 
2404 		DPRINTF("Activating SOFs!\n");
2405 
2406 		UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS));
2407 
2408 		/* wait a little bit */
2409 		usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
2410 	}
2411 
2412 	x = URWMASK(UREAD2(sc, port));
2413 	UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2414 
2415 	usb2_pause_mtx(&sc->sc_bus.bus_mtx,
2416 	    USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY));
2417 
2418 	DPRINTFN(4, "uhci port %d reset, status0 = 0x%04x\n",
2419 	    index, UREAD2(sc, port));
2420 
2421 	x = URWMASK(UREAD2(sc, port));
2422 	UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2423 
2424 
2425 	mtx_unlock(&sc->sc_bus.bus_mtx);
2426 
2427 	/*
2428 	 * This delay needs to be exactly 100us, else some USB devices
2429 	 * fail to attach!
2430 	 */
2431 	DELAY(100);
2432 
2433 	mtx_lock(&sc->sc_bus.bus_mtx);
2434 
2435 	DPRINTFN(4, "uhci port %d reset, status1 = 0x%04x\n",
2436 	    index, UREAD2(sc, port));
2437 
2438 	x = URWMASK(UREAD2(sc, port));
2439 	UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2440 
2441 	for (lim = 0; lim < 12; lim++) {
2442 
2443 		usb2_pause_mtx(&sc->sc_bus.bus_mtx,
2444 		    USB_MS_TO_TICKS(USB_PORT_RESET_DELAY));
2445 
2446 		x = UREAD2(sc, port);
2447 
2448 		DPRINTFN(4, "uhci port %d iteration %u, status = 0x%04x\n",
2449 		    index, lim, x);
2450 
2451 		if (!(x & UHCI_PORTSC_CCS)) {
2452 			/*
2453 			 * No device is connected (or was disconnected
2454 			 * during reset).  Consider the port reset.
2455 			 * The delay must be long enough to ensure on
2456 			 * the initial iteration that the device
2457 			 * connection will have been registered.  50ms
2458 			 * appears to be sufficient, but 20ms is not.
2459 			 */
2460 			DPRINTFN(4, "uhci port %d loop %u, device detached\n",
2461 			    index, lim);
2462 			goto done;
2463 		}
2464 		if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) {
2465 			/*
2466 			 * Port enabled changed and/or connection
2467 			 * status changed were set.  Reset either or
2468 			 * both raised flags (by writing a 1 to that
2469 			 * bit), and wait again for state to settle.
2470 			 */
2471 			UWRITE2(sc, port, URWMASK(x) |
2472 			    (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)));
2473 			continue;
2474 		}
2475 		if (x & UHCI_PORTSC_PE) {
2476 			/* port is enabled */
2477 			goto done;
2478 		}
2479 		UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE);
2480 	}
2481 
2482 	DPRINTFN(2, "uhci port %d reset timed out\n", index);
2483 	return (USB_ERR_TIMEOUT);
2484 
2485 done:
2486 	DPRINTFN(4, "uhci port %d reset, status2 = 0x%04x\n",
2487 	    index, UREAD2(sc, port));
2488 
2489 	sc->sc_isreset = 1;
2490 	return (USB_ERR_NORMAL_COMPLETION);
2491 }
2492 
2493 static void
2494 uhci_root_ctrl_enter(struct usb2_xfer *xfer)
2495 {
2496 	return;
2497 }
2498 
2499 static void
2500 uhci_root_ctrl_start(struct usb2_xfer *xfer)
2501 {
2502 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2503 
2504 	DPRINTF("\n");
2505 
2506 	sc->sc_root_ctrl.xfer = xfer;
2507 
2508 	usb2_bus_roothub_exec(xfer->xroot->bus);
2509 }
2510 
2511 static void
2512 uhci_root_ctrl_task(struct usb2_bus *bus)
2513 {
2514 	uhci_root_ctrl_poll(UHCI_BUS2SC(bus));
2515 }
2516 
2517 static void
2518 uhci_root_ctrl_done(struct usb2_xfer *xfer,
2519     struct usb2_sw_transfer *std)
2520 {
2521 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2522 	char *ptr;
2523 	uint16_t x;
2524 	uint16_t port;
2525 	uint16_t value;
2526 	uint16_t index;
2527 	uint16_t status;
2528 	uint16_t change;
2529 
2530 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2531 
2532 	if (std->state != USB_SW_TR_SETUP) {
2533 		if (std->state == USB_SW_TR_PRE_CALLBACK) {
2534 			/* transfer transferred */
2535 			uhci_device_done(xfer, std->err);
2536 		}
2537 		goto done;
2538 	}
2539 	/* buffer reset */
2540 	std->ptr = sc->sc_hub_desc.temp;
2541 	std->len = 0;
2542 
2543 	value = UGETW(std->req.wValue);
2544 	index = UGETW(std->req.wIndex);
2545 
2546 	DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
2547 	    "wValue=0x%04x wIndex=0x%04x\n",
2548 	    std->req.bmRequestType, std->req.bRequest,
2549 	    UGETW(std->req.wLength), value, index);
2550 
2551 #define	C(x,y) ((x) | ((y) << 8))
2552 	switch (C(std->req.bRequest, std->req.bmRequestType)) {
2553 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2554 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2555 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2556 		/*
2557 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2558 		 * for the integrated root hub.
2559 		 */
2560 		break;
2561 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
2562 		std->len = 1;
2563 		sc->sc_hub_desc.temp[0] = sc->sc_conf;
2564 		break;
2565 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2566 		switch (value >> 8) {
2567 		case UDESC_DEVICE:
2568 			if ((value & 0xff) != 0) {
2569 				std->err = USB_ERR_IOERROR;
2570 				goto done;
2571 			}
2572 			std->len = sizeof(uhci_devd);
2573 			sc->sc_hub_desc.devd = uhci_devd;
2574 			break;
2575 
2576 		case UDESC_CONFIG:
2577 			if ((value & 0xff) != 0) {
2578 				std->err = USB_ERR_IOERROR;
2579 				goto done;
2580 			}
2581 			std->len = sizeof(uhci_confd);
2582 			std->ptr = USB_ADD_BYTES(&uhci_confd, 0);
2583 			break;
2584 
2585 		case UDESC_STRING:
2586 			switch (value & 0xff) {
2587 			case 0:	/* Language table */
2588 				ptr = "\001";
2589 				break;
2590 
2591 			case 1:	/* Vendor */
2592 				ptr = sc->sc_vendor;
2593 				break;
2594 
2595 			case 2:	/* Product */
2596 				ptr = "UHCI root HUB";
2597 				break;
2598 
2599 			default:
2600 				ptr = "";
2601 				break;
2602 			}
2603 
2604 			std->len = usb2_make_str_desc
2605 			    (sc->sc_hub_desc.temp,
2606 			    sizeof(sc->sc_hub_desc.temp),
2607 			    ptr);
2608 			break;
2609 
2610 		default:
2611 			std->err = USB_ERR_IOERROR;
2612 			goto done;
2613 		}
2614 		break;
2615 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2616 		std->len = 1;
2617 		sc->sc_hub_desc.temp[0] = 0;
2618 		break;
2619 	case C(UR_GET_STATUS, UT_READ_DEVICE):
2620 		std->len = 2;
2621 		USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
2622 		break;
2623 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
2624 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2625 		std->len = 2;
2626 		USETW(sc->sc_hub_desc.stat.wStatus, 0);
2627 		break;
2628 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2629 		if (value >= USB_MAX_DEVICES) {
2630 			std->err = USB_ERR_IOERROR;
2631 			goto done;
2632 		}
2633 		sc->sc_addr = value;
2634 		break;
2635 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2636 		if ((value != 0) && (value != 1)) {
2637 			std->err = USB_ERR_IOERROR;
2638 			goto done;
2639 		}
2640 		sc->sc_conf = value;
2641 		break;
2642 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2643 		break;
2644 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2645 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2646 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2647 		std->err = USB_ERR_IOERROR;
2648 		goto done;
2649 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2650 		break;
2651 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2652 		break;
2653 		/* Hub requests */
2654 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2655 		break;
2656 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2657 		DPRINTFN(4, "UR_CLEAR_PORT_FEATURE "
2658 		    "port=%d feature=%d\n",
2659 		    index, value);
2660 		if (index == 1)
2661 			port = UHCI_PORTSC1;
2662 		else if (index == 2)
2663 			port = UHCI_PORTSC2;
2664 		else {
2665 			std->err = USB_ERR_IOERROR;
2666 			goto done;
2667 		}
2668 		switch (value) {
2669 		case UHF_PORT_ENABLE:
2670 			x = URWMASK(UREAD2(sc, port));
2671 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
2672 			break;
2673 		case UHF_PORT_SUSPEND:
2674 			x = URWMASK(UREAD2(sc, port));
2675 			UWRITE2(sc, port, x & ~(UHCI_PORTSC_SUSP));
2676 			break;
2677 		case UHF_PORT_RESET:
2678 			x = URWMASK(UREAD2(sc, port));
2679 			UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2680 			break;
2681 		case UHF_C_PORT_CONNECTION:
2682 			x = URWMASK(UREAD2(sc, port));
2683 			UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
2684 			break;
2685 		case UHF_C_PORT_ENABLE:
2686 			x = URWMASK(UREAD2(sc, port));
2687 			UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
2688 			break;
2689 		case UHF_C_PORT_OVER_CURRENT:
2690 			x = URWMASK(UREAD2(sc, port));
2691 			UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
2692 			break;
2693 		case UHF_C_PORT_RESET:
2694 			sc->sc_isreset = 0;
2695 			std->err = USB_ERR_NORMAL_COMPLETION;
2696 			goto done;
2697 		case UHF_C_PORT_SUSPEND:
2698 			sc->sc_isresumed &= ~(1 << index);
2699 			break;
2700 		case UHF_PORT_CONNECTION:
2701 		case UHF_PORT_OVER_CURRENT:
2702 		case UHF_PORT_POWER:
2703 		case UHF_PORT_LOW_SPEED:
2704 		default:
2705 			std->err = USB_ERR_IOERROR;
2706 			goto done;
2707 		}
2708 		break;
2709 	case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
2710 		if (index == 1)
2711 			port = UHCI_PORTSC1;
2712 		else if (index == 2)
2713 			port = UHCI_PORTSC2;
2714 		else {
2715 			std->err = USB_ERR_IOERROR;
2716 			goto done;
2717 		}
2718 		std->len = 1;
2719 		sc->sc_hub_desc.temp[0] =
2720 		    ((UREAD2(sc, port) & UHCI_PORTSC_LS) >>
2721 		    UHCI_PORTSC_LS_SHIFT);
2722 		break;
2723 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2724 		if ((value & 0xff) != 0) {
2725 			std->err = USB_ERR_IOERROR;
2726 			goto done;
2727 		}
2728 		std->len = sizeof(uhci_hubd_piix);
2729 		std->ptr = USB_ADD_BYTES(&uhci_hubd_piix, 0);
2730 		break;
2731 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2732 		std->len = 16;
2733 		bzero(sc->sc_hub_desc.temp, 16);
2734 		break;
2735 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2736 		if (index == 1)
2737 			port = UHCI_PORTSC1;
2738 		else if (index == 2)
2739 			port = UHCI_PORTSC2;
2740 		else {
2741 			std->err = USB_ERR_IOERROR;
2742 			goto done;
2743 		}
2744 		x = UREAD2(sc, port);
2745 		status = change = 0;
2746 		if (x & UHCI_PORTSC_CCS)
2747 			status |= UPS_CURRENT_CONNECT_STATUS;
2748 		if (x & UHCI_PORTSC_CSC)
2749 			change |= UPS_C_CONNECT_STATUS;
2750 		if (x & UHCI_PORTSC_PE)
2751 			status |= UPS_PORT_ENABLED;
2752 		if (x & UHCI_PORTSC_POEDC)
2753 			change |= UPS_C_PORT_ENABLED;
2754 		if (x & UHCI_PORTSC_OCI)
2755 			status |= UPS_OVERCURRENT_INDICATOR;
2756 		if (x & UHCI_PORTSC_OCIC)
2757 			change |= UPS_C_OVERCURRENT_INDICATOR;
2758 		if (x & UHCI_PORTSC_LSDA)
2759 			status |= UPS_LOW_SPEED;
2760 		if ((x & UHCI_PORTSC_PE) && (x & UHCI_PORTSC_RD)) {
2761 			/* need to do a write back */
2762 			UWRITE2(sc, port, URWMASK(x));
2763 
2764 			/* wait 20ms for resume sequence to complete */
2765 			usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
2766 
2767 			/* clear suspend and resume detect */
2768 			UWRITE2(sc, port, URWMASK(x) & ~(UHCI_PORTSC_RD |
2769 			    UHCI_PORTSC_SUSP));
2770 
2771 			/* wait a little bit */
2772 			usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 500);
2773 
2774 			sc->sc_isresumed |= (1 << index);
2775 
2776 		} else if (x & UHCI_PORTSC_SUSP) {
2777 			status |= UPS_SUSPEND;
2778 		}
2779 		status |= UPS_PORT_POWER;
2780 		if (sc->sc_isresumed & (1 << index))
2781 			change |= UPS_C_SUSPEND;
2782 		if (sc->sc_isreset)
2783 			change |= UPS_C_PORT_RESET;
2784 		USETW(sc->sc_hub_desc.ps.wPortStatus, status);
2785 		USETW(sc->sc_hub_desc.ps.wPortChange, change);
2786 		std->len = sizeof(sc->sc_hub_desc.ps);
2787 		break;
2788 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2789 		std->err = USB_ERR_IOERROR;
2790 		goto done;
2791 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2792 		break;
2793 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2794 		if (index == 1)
2795 			port = UHCI_PORTSC1;
2796 		else if (index == 2)
2797 			port = UHCI_PORTSC2;
2798 		else {
2799 			std->err = USB_ERR_IOERROR;
2800 			goto done;
2801 		}
2802 		switch (value) {
2803 		case UHF_PORT_ENABLE:
2804 			x = URWMASK(UREAD2(sc, port));
2805 			UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2806 			break;
2807 		case UHF_PORT_SUSPEND:
2808 			x = URWMASK(UREAD2(sc, port));
2809 			UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
2810 			break;
2811 		case UHF_PORT_RESET:
2812 			std->err = uhci_portreset(sc, index);
2813 			goto done;
2814 		case UHF_PORT_POWER:
2815 			/* pretend we turned on power */
2816 			std->err = USB_ERR_NORMAL_COMPLETION;
2817 			goto done;
2818 		case UHF_C_PORT_CONNECTION:
2819 		case UHF_C_PORT_ENABLE:
2820 		case UHF_C_PORT_OVER_CURRENT:
2821 		case UHF_PORT_CONNECTION:
2822 		case UHF_PORT_OVER_CURRENT:
2823 		case UHF_PORT_LOW_SPEED:
2824 		case UHF_C_PORT_SUSPEND:
2825 		case UHF_C_PORT_RESET:
2826 		default:
2827 			std->err = USB_ERR_IOERROR;
2828 			goto done;
2829 		}
2830 		break;
2831 	default:
2832 		std->err = USB_ERR_IOERROR;
2833 		goto done;
2834 	}
2835 done:
2836 	return;
2837 }
2838 
2839 static void
2840 uhci_root_ctrl_poll(struct uhci_softc *sc)
2841 {
2842 	usb2_sw_transfer(&sc->sc_root_ctrl,
2843 	    &uhci_root_ctrl_done);
2844 }
2845 
2846 struct usb2_pipe_methods uhci_root_ctrl_methods =
2847 {
2848 	.open = uhci_root_ctrl_open,
2849 	.close = uhci_root_ctrl_close,
2850 	.enter = uhci_root_ctrl_enter,
2851 	.start = uhci_root_ctrl_start,
2852 	.enter_is_cancelable = 1,
2853 	.start_is_cancelable = 0,
2854 };
2855 
2856 /*------------------------------------------------------------------------*
2857  * uhci root interrupt support
2858  *------------------------------------------------------------------------*/
2859 static void
2860 uhci_root_intr_open(struct usb2_xfer *xfer)
2861 {
2862 	return;
2863 }
2864 
2865 static void
2866 uhci_root_intr_close(struct usb2_xfer *xfer)
2867 {
2868 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2869 
2870 	if (sc->sc_root_intr.xfer == xfer) {
2871 		sc->sc_root_intr.xfer = NULL;
2872 	}
2873 	uhci_device_done(xfer, USB_ERR_CANCELLED);
2874 }
2875 
2876 static void
2877 uhci_root_intr_enter(struct usb2_xfer *xfer)
2878 {
2879 	return;
2880 }
2881 
2882 static void
2883 uhci_root_intr_start(struct usb2_xfer *xfer)
2884 {
2885 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2886 
2887 	sc->sc_root_intr.xfer = xfer;
2888 
2889 	usb2_transfer_timeout_ms(xfer,
2890 	    &uhci_root_intr_check, xfer->interval);
2891 }
2892 
2893 static void
2894 uhci_root_intr_done(struct usb2_xfer *xfer,
2895     struct usb2_sw_transfer *std)
2896 {
2897 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2898 
2899 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2900 
2901 	if (std->state != USB_SW_TR_PRE_DATA) {
2902 		if (std->state == USB_SW_TR_PRE_CALLBACK) {
2903 			/* transfer is transferred */
2904 			uhci_device_done(xfer, std->err);
2905 		}
2906 		goto done;
2907 	}
2908 	/* setup buffer */
2909 	std->ptr = sc->sc_hub_idata;
2910 	std->len = sizeof(sc->sc_hub_idata);
2911 done:
2912 	return;
2913 }
2914 
2915 /*
2916  * this routine is executed periodically and simulates interrupts
2917  * from the root controller interrupt pipe for port status change
2918  */
2919 static void
2920 uhci_root_intr_check(void *arg)
2921 {
2922 	struct usb2_xfer *xfer = arg;
2923 	uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus);
2924 
2925 	DPRINTFN(21, "\n");
2926 
2927 	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2928 
2929 	sc->sc_hub_idata[0] = 0;
2930 
2931 	if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC |
2932 	    UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
2933 		sc->sc_hub_idata[0] |= 1 << 1;
2934 	}
2935 	if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC |
2936 	    UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) {
2937 		sc->sc_hub_idata[0] |= 1 << 2;
2938 	}
2939 	if (sc->sc_hub_idata[0] == 0) {
2940 		/*
2941 		 * no change or controller not running, try again in a while
2942 		 */
2943 		uhci_root_intr_start(xfer);
2944 	} else {
2945 		usb2_sw_transfer(&sc->sc_root_intr,
2946 		    &uhci_root_intr_done);
2947 	}
2948 }
2949 
2950 struct usb2_pipe_methods uhci_root_intr_methods =
2951 {
2952 	.open = uhci_root_intr_open,
2953 	.close = uhci_root_intr_close,
2954 	.enter = uhci_root_intr_enter,
2955 	.start = uhci_root_intr_start,
2956 	.enter_is_cancelable = 1,
2957 	.start_is_cancelable = 1,
2958 };
2959 
2960 static void
2961 uhci_xfer_setup(struct usb2_setup_params *parm)
2962 {
2963 	struct usb2_page_search page_info;
2964 	struct usb2_page_cache *pc;
2965 	uhci_softc_t *sc;
2966 	struct usb2_xfer *xfer;
2967 	void *last_obj;
2968 	uint32_t ntd;
2969 	uint32_t nqh;
2970 	uint32_t nfixup;
2971 	uint32_t n;
2972 	uint16_t align;
2973 
2974 	sc = UHCI_BUS2SC(parm->udev->bus);
2975 	xfer = parm->curr_xfer;
2976 
2977 	parm->hc_max_packet_size = 0x500;
2978 	parm->hc_max_packet_count = 1;
2979 	parm->hc_max_frame_size = 0x500;
2980 
2981 	/*
2982 	 * compute ntd and nqh
2983 	 */
2984 	if (parm->methods == &uhci_device_ctrl_methods) {
2985 		xfer->flags_int.bdma_enable = 1;
2986 		xfer->flags_int.bdma_no_post_sync = 1;
2987 
2988 		usb2_transfer_setup_sub(parm);
2989 
2990 		/* see EHCI HC driver for proof of "ntd" formula */
2991 
2992 		nqh = 1;
2993 		ntd = ((2 * xfer->nframes) + 1	/* STATUS */
2994 		    + (xfer->max_data_length / xfer->max_frame_size));
2995 
2996 	} else if (parm->methods == &uhci_device_bulk_methods) {
2997 		xfer->flags_int.bdma_enable = 1;
2998 		xfer->flags_int.bdma_no_post_sync = 1;
2999 
3000 		usb2_transfer_setup_sub(parm);
3001 
3002 		nqh = 1;
3003 		ntd = ((2 * xfer->nframes)
3004 		    + (xfer->max_data_length / xfer->max_frame_size));
3005 
3006 	} else if (parm->methods == &uhci_device_intr_methods) {
3007 		xfer->flags_int.bdma_enable = 1;
3008 		xfer->flags_int.bdma_no_post_sync = 1;
3009 
3010 		usb2_transfer_setup_sub(parm);
3011 
3012 		nqh = 1;
3013 		ntd = ((2 * xfer->nframes)
3014 		    + (xfer->max_data_length / xfer->max_frame_size));
3015 
3016 	} else if (parm->methods == &uhci_device_isoc_methods) {
3017 		xfer->flags_int.bdma_enable = 1;
3018 		xfer->flags_int.bdma_no_post_sync = 1;
3019 
3020 		usb2_transfer_setup_sub(parm);
3021 
3022 		nqh = 0;
3023 		ntd = xfer->nframes;
3024 
3025 	} else {
3026 
3027 		usb2_transfer_setup_sub(parm);
3028 
3029 		nqh = 0;
3030 		ntd = 0;
3031 	}
3032 
3033 	if (parm->err) {
3034 		return;
3035 	}
3036 	/*
3037 	 * NOTE: the UHCI controller requires that
3038 	 * every packet must be contiguous on
3039 	 * the same USB memory page !
3040 	 */
3041 	nfixup = (parm->bufsize / USB_PAGE_SIZE) + 1;
3042 
3043 	/*
3044 	 * Compute a suitable power of two alignment
3045 	 * for our "max_frame_size" fixup buffer(s):
3046 	 */
3047 	align = xfer->max_frame_size;
3048 	n = 0;
3049 	while (align) {
3050 		align >>= 1;
3051 		n++;
3052 	}
3053 
3054 	/* check for power of two */
3055 	if (!(xfer->max_frame_size &
3056 	    (xfer->max_frame_size - 1))) {
3057 		n--;
3058 	}
3059 	/*
3060 	 * We don't allow alignments of
3061 	 * less than 8 bytes:
3062 	 *
3063 	 * NOTE: Allocating using an aligment
3064 	 * of 1 byte has special meaning!
3065 	 */
3066 	if (n < 3) {
3067 		n = 3;
3068 	}
3069 	align = (1 << n);
3070 
3071 	if (usb2_transfer_setup_sub_malloc(
3072 	    parm, &pc, xfer->max_frame_size,
3073 	    align, nfixup)) {
3074 		parm->err = USB_ERR_NOMEM;
3075 		return;
3076 	}
3077 	xfer->buf_fixup = pc;
3078 
3079 alloc_dma_set:
3080 
3081 	if (parm->err) {
3082 		return;
3083 	}
3084 	last_obj = NULL;
3085 
3086 	if (usb2_transfer_setup_sub_malloc(
3087 	    parm, &pc, sizeof(uhci_td_t),
3088 	    UHCI_TD_ALIGN, ntd)) {
3089 		parm->err = USB_ERR_NOMEM;
3090 		return;
3091 	}
3092 	if (parm->buf) {
3093 		for (n = 0; n != ntd; n++) {
3094 			uhci_td_t *td;
3095 
3096 			usb2_get_page(pc + n, 0, &page_info);
3097 
3098 			td = page_info.buffer;
3099 
3100 			/* init TD */
3101 			if ((parm->methods == &uhci_device_bulk_methods) ||
3102 			    (parm->methods == &uhci_device_ctrl_methods) ||
3103 			    (parm->methods == &uhci_device_intr_methods)) {
3104 				/* set depth first bit */
3105 				td->td_self = htole32(page_info.physaddr |
3106 				    UHCI_PTR_TD | UHCI_PTR_VF);
3107 			} else {
3108 				td->td_self = htole32(page_info.physaddr |
3109 				    UHCI_PTR_TD);
3110 			}
3111 
3112 			td->obj_next = last_obj;
3113 			td->page_cache = pc + n;
3114 
3115 			last_obj = td;
3116 
3117 			usb2_pc_cpu_flush(pc + n);
3118 		}
3119 	}
3120 	xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3121 
3122 	last_obj = NULL;
3123 
3124 	if (usb2_transfer_setup_sub_malloc(
3125 	    parm, &pc, sizeof(uhci_qh_t),
3126 	    UHCI_QH_ALIGN, nqh)) {
3127 		parm->err = USB_ERR_NOMEM;
3128 		return;
3129 	}
3130 	if (parm->buf) {
3131 		for (n = 0; n != nqh; n++) {
3132 			uhci_qh_t *qh;
3133 
3134 			usb2_get_page(pc + n, 0, &page_info);
3135 
3136 			qh = page_info.buffer;
3137 
3138 			/* init QH */
3139 			qh->qh_self = htole32(page_info.physaddr | UHCI_PTR_QH);
3140 			qh->obj_next = last_obj;
3141 			qh->page_cache = pc + n;
3142 
3143 			last_obj = qh;
3144 
3145 			usb2_pc_cpu_flush(pc + n);
3146 		}
3147 	}
3148 	xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
3149 
3150 	if (!xfer->flags_int.curr_dma_set) {
3151 		xfer->flags_int.curr_dma_set = 1;
3152 		goto alloc_dma_set;
3153 	}
3154 }
3155 
3156 static void
3157 uhci_pipe_init(struct usb2_device *udev, struct usb2_endpoint_descriptor *edesc,
3158     struct usb2_pipe *pipe)
3159 {
3160 	uhci_softc_t *sc = UHCI_BUS2SC(udev->bus);
3161 
3162 	DPRINTFN(2, "pipe=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
3163 	    pipe, udev->address,
3164 	    edesc->bEndpointAddress, udev->flags.usb2_mode,
3165 	    sc->sc_addr);
3166 
3167 	if (udev->flags.usb2_mode != USB_MODE_HOST) {
3168 		/* not supported */
3169 		return;
3170 	}
3171 	if (udev->device_index == sc->sc_addr) {
3172 		switch (edesc->bEndpointAddress) {
3173 		case USB_CONTROL_ENDPOINT:
3174 			pipe->methods = &uhci_root_ctrl_methods;
3175 			break;
3176 		case UE_DIR_IN | UHCI_INTR_ENDPT:
3177 			pipe->methods = &uhci_root_intr_methods;
3178 			break;
3179 		default:
3180 			/* do nothing */
3181 			break;
3182 		}
3183 	} else {
3184 		switch (edesc->bmAttributes & UE_XFERTYPE) {
3185 		case UE_CONTROL:
3186 			pipe->methods = &uhci_device_ctrl_methods;
3187 			break;
3188 		case UE_INTERRUPT:
3189 			pipe->methods = &uhci_device_intr_methods;
3190 			break;
3191 		case UE_ISOCHRONOUS:
3192 			if (udev->speed == USB_SPEED_FULL) {
3193 				pipe->methods = &uhci_device_isoc_methods;
3194 			}
3195 			break;
3196 		case UE_BULK:
3197 			if (udev->speed != USB_SPEED_LOW) {
3198 				pipe->methods = &uhci_device_bulk_methods;
3199 			}
3200 			break;
3201 		default:
3202 			/* do nothing */
3203 			break;
3204 		}
3205 	}
3206 }
3207 
3208 static void
3209 uhci_xfer_unsetup(struct usb2_xfer *xfer)
3210 {
3211 	return;
3212 }
3213 
3214 static void
3215 uhci_get_dma_delay(struct usb2_bus *bus, uint32_t *pus)
3216 {
3217 	/*
3218 	 * Wait until hardware has finished any possible use of the
3219 	 * transfer descriptor(s) and QH
3220 	 */
3221 	*pus = (1125);			/* microseconds */
3222 }
3223 
3224 static void
3225 uhci_device_resume(struct usb2_device *udev)
3226 {
3227 	struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3228 	struct usb2_xfer *xfer;
3229 	struct usb2_pipe_methods *methods;
3230 	uhci_qh_t *qh;
3231 
3232 	DPRINTF("\n");
3233 
3234 	USB_BUS_LOCK(udev->bus);
3235 
3236 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3237 
3238 		if (xfer->xroot->udev == udev) {
3239 
3240 			methods = xfer->pipe->methods;
3241 			qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
3242 
3243 			if (methods == &uhci_device_bulk_methods) {
3244 				UHCI_APPEND_QH(qh, sc->sc_bulk_p_last);
3245 				uhci_add_loop(sc);
3246 				xfer->flags_int.bandwidth_reclaimed = 1;
3247 			}
3248 			if (methods == &uhci_device_ctrl_methods) {
3249 				if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
3250 					UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last);
3251 				} else {
3252 					UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last);
3253 				}
3254 			}
3255 			if (methods == &uhci_device_intr_methods) {
3256 				UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
3257 			}
3258 		}
3259 	}
3260 
3261 	USB_BUS_UNLOCK(udev->bus);
3262 
3263 	return;
3264 }
3265 
3266 static void
3267 uhci_device_suspend(struct usb2_device *udev)
3268 {
3269 	struct uhci_softc *sc = UHCI_BUS2SC(udev->bus);
3270 	struct usb2_xfer *xfer;
3271 	struct usb2_pipe_methods *methods;
3272 	uhci_qh_t *qh;
3273 
3274 	DPRINTF("\n");
3275 
3276 	USB_BUS_LOCK(udev->bus);
3277 
3278 	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3279 
3280 		if (xfer->xroot->udev == udev) {
3281 
3282 			methods = xfer->pipe->methods;
3283 			qh = xfer->qh_start[xfer->flags_int.curr_dma_set];
3284 
3285 			if (xfer->flags_int.bandwidth_reclaimed) {
3286 				xfer->flags_int.bandwidth_reclaimed = 0;
3287 				uhci_rem_loop(sc);
3288 			}
3289 			if (methods == &uhci_device_bulk_methods) {
3290 				UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last);
3291 			}
3292 			if (methods == &uhci_device_ctrl_methods) {
3293 				if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
3294 					UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last);
3295 				} else {
3296 					UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last);
3297 				}
3298 			}
3299 			if (methods == &uhci_device_intr_methods) {
3300 				UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]);
3301 			}
3302 		}
3303 	}
3304 
3305 	USB_BUS_UNLOCK(udev->bus);
3306 
3307 	return;
3308 }
3309 
3310 static void
3311 uhci_set_hw_power(struct usb2_bus *bus)
3312 {
3313 	struct uhci_softc *sc = UHCI_BUS2SC(bus);
3314 	uint32_t flags;
3315 
3316 	DPRINTF("\n");
3317 
3318 	USB_BUS_LOCK(bus);
3319 
3320 	flags = bus->hw_power_state;
3321 
3322 	/*
3323 	 * WARNING: Some FULL speed USB devices require periodic SOF
3324 	 * messages! If any USB devices are connected through the
3325 	 * UHCI, power save will be disabled!
3326 	 */
3327 	if (flags & (USB_HW_POWER_CONTROL |
3328 	    USB_HW_POWER_NON_ROOT_HUB |
3329 	    USB_HW_POWER_BULK |
3330 	    USB_HW_POWER_INTERRUPT |
3331 	    USB_HW_POWER_ISOC)) {
3332 		DPRINTF("Some USB transfer is "
3333 		    "active on %u.\n",
3334 		    device_get_unit(sc->sc_bus.bdev));
3335 		UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS));
3336 	} else {
3337 		DPRINTF("Power save on %u.\n",
3338 		    device_get_unit(sc->sc_bus.bdev));
3339 		UHCICMD(sc, UHCI_CMD_MAXP);
3340 	}
3341 
3342 	USB_BUS_UNLOCK(bus);
3343 
3344 	return;
3345 }
3346 
3347 
3348 struct usb2_bus_methods uhci_bus_methods =
3349 {
3350 	.pipe_init = uhci_pipe_init,
3351 	.xfer_setup = uhci_xfer_setup,
3352 	.xfer_unsetup = uhci_xfer_unsetup,
3353 	.get_dma_delay = uhci_get_dma_delay,
3354 	.device_resume = uhci_device_resume,
3355 	.device_suspend = uhci_device_suspend,
3356 	.set_hw_power = uhci_set_hw_power,
3357 	.roothub_exec = uhci_root_ctrl_task,
3358 };
3359