ohci.c revision 190183
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: head/sys/dev/usb/controller/ohci.c 190183 2009-03-20 21:57:54Z thompsa $");
30
31/*
32 * USB Open Host Controller driver.
33 *
34 * OHCI spec: http://www.compaq.com/productinfo/development/openhci.html
35 * USB spec: http://www.usb.org/developers/docs/usbspec.zip
36 */
37
38#include <dev/usb/usb.h>
39#include <dev/usb/usb_mfunc.h>
40#include <dev/usb/usb_error.h>
41
42#define	USB_DEBUG_VAR ohcidebug
43
44#include <dev/usb/usb_core.h>
45#include <dev/usb/usb_debug.h>
46#include <dev/usb/usb_busdma.h>
47#include <dev/usb/usb_process.h>
48#include <dev/usb/usb_sw_transfer.h>
49#include <dev/usb/usb_transfer.h>
50#include <dev/usb/usb_device.h>
51#include <dev/usb/usb_hub.h>
52#include <dev/usb/usb_util.h>
53
54#include <dev/usb/usb_controller.h>
55#include <dev/usb/usb_bus.h>
56#include <dev/usb/controller/ohci.h>
57
58#define	OHCI_BUS2SC(bus) \
59   ((ohci_softc_t *)(((uint8_t *)(bus)) - \
60    ((uint8_t *)&(((ohci_softc_t *)0)->sc_bus))))
61
62#if USB_DEBUG
63static int ohcidebug = 0;
64
65SYSCTL_NODE(_hw_usb2, OID_AUTO, ohci, CTLFLAG_RW, 0, "USB ohci");
66SYSCTL_INT(_hw_usb2_ohci, OID_AUTO, debug, CTLFLAG_RW,
67    &ohcidebug, 0, "ohci debug level");
68static void ohci_dumpregs(ohci_softc_t *);
69static void ohci_dump_tds(ohci_td_t *);
70static uint8_t ohci_dump_td(ohci_td_t *);
71static void ohci_dump_ed(ohci_ed_t *);
72static uint8_t ohci_dump_itd(ohci_itd_t *);
73static void ohci_dump_itds(ohci_itd_t *);
74
75#endif
76
77#define	OBARR(sc) bus_space_barrier((sc)->sc_io_tag, (sc)->sc_io_hdl, 0, (sc)->sc_io_size, \
78			BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
79#define	OWRITE1(sc, r, x) \
80 do { OBARR(sc); bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); } while (0)
81#define	OWRITE2(sc, r, x) \
82 do { OBARR(sc); bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); } while (0)
83#define	OWRITE4(sc, r, x) \
84 do { OBARR(sc); bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); } while (0)
85#define	OREAD1(sc, r) (OBARR(sc), bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
86#define	OREAD2(sc, r) (OBARR(sc), bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
87#define	OREAD4(sc, r) (OBARR(sc), bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r)))
88
89#define	OHCI_INTR_ENDPT 1
90
91extern struct usb2_bus_methods ohci_bus_methods;
92extern struct usb2_pipe_methods ohci_device_bulk_methods;
93extern struct usb2_pipe_methods ohci_device_ctrl_methods;
94extern struct usb2_pipe_methods ohci_device_intr_methods;
95extern struct usb2_pipe_methods ohci_device_isoc_methods;
96extern struct usb2_pipe_methods ohci_root_ctrl_methods;
97extern struct usb2_pipe_methods ohci_root_intr_methods;
98
99static void ohci_root_ctrl_poll(struct ohci_softc *sc);
100static void ohci_do_poll(struct usb2_bus *bus);
101static void ohci_device_done(struct usb2_xfer *xfer, usb2_error_t error);
102
103static usb2_sw_transfer_func_t ohci_root_intr_done;
104static usb2_sw_transfer_func_t ohci_root_ctrl_done;
105static void ohci_timeout(void *arg);
106static uint8_t ohci_check_transfer(struct usb2_xfer *xfer);
107
108struct ohci_std_temp {
109	struct usb2_page_cache *pc;
110	ohci_td_t *td;
111	ohci_td_t *td_next;
112	uint32_t average;
113	uint32_t td_flags;
114	uint32_t len;
115	uint16_t max_frame_size;
116	uint8_t	shortpkt;
117	uint8_t	setup_alt_next;
118	uint8_t last_frame;
119};
120
121static struct ohci_hcca *
122ohci_get_hcca(ohci_softc_t *sc)
123{
124	usb2_pc_cpu_invalidate(&sc->sc_hw.hcca_pc);
125	return (sc->sc_hcca_p);
126}
127
128void
129ohci_iterate_hw_softc(struct usb2_bus *bus, usb2_bus_mem_sub_cb_t *cb)
130{
131	struct ohci_softc *sc = OHCI_BUS2SC(bus);
132	uint32_t i;
133
134	cb(bus, &sc->sc_hw.hcca_pc, &sc->sc_hw.hcca_pg,
135	    sizeof(ohci_hcca_t), OHCI_HCCA_ALIGN);
136
137	cb(bus, &sc->sc_hw.ctrl_start_pc, &sc->sc_hw.ctrl_start_pg,
138	    sizeof(ohci_ed_t), OHCI_ED_ALIGN);
139
140	cb(bus, &sc->sc_hw.bulk_start_pc, &sc->sc_hw.bulk_start_pg,
141	    sizeof(ohci_ed_t), OHCI_ED_ALIGN);
142
143	cb(bus, &sc->sc_hw.isoc_start_pc, &sc->sc_hw.isoc_start_pg,
144	    sizeof(ohci_ed_t), OHCI_ED_ALIGN);
145
146	for (i = 0; i != OHCI_NO_EDS; i++) {
147		cb(bus, sc->sc_hw.intr_start_pc + i, sc->sc_hw.intr_start_pg + i,
148		    sizeof(ohci_ed_t), OHCI_ED_ALIGN);
149	}
150}
151
152static usb2_error_t
153ohci_controller_init(ohci_softc_t *sc)
154{
155	struct usb2_page_search buf_res;
156	uint32_t i;
157	uint32_t ctl;
158	uint32_t ival;
159	uint32_t hcr;
160	uint32_t fm;
161	uint32_t per;
162	uint32_t desca;
163
164	/* Determine in what context we are running. */
165	ctl = OREAD4(sc, OHCI_CONTROL);
166	if (ctl & OHCI_IR) {
167		/* SMM active, request change */
168		DPRINTF("SMM active, request owner change\n");
169		OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_OCR);
170		for (i = 0; (i < 100) && (ctl & OHCI_IR); i++) {
171			usb2_pause_mtx(NULL, hz / 1000);
172			ctl = OREAD4(sc, OHCI_CONTROL);
173		}
174		if (ctl & OHCI_IR) {
175			device_printf(sc->sc_bus.bdev,
176			    "SMM does not respond, resetting\n");
177			OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
178			goto reset;
179		}
180	} else {
181		DPRINTF("cold started\n");
182reset:
183		/* controller was cold started */
184		usb2_pause_mtx(NULL,
185		    USB_MS_TO_TICKS(USB_BUS_RESET_DELAY));
186	}
187
188	/*
189	 * This reset should not be necessary according to the OHCI spec, but
190	 * without it some controllers do not start.
191	 */
192	DPRINTF("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev));
193	OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
194
195	usb2_pause_mtx(NULL,
196	    USB_MS_TO_TICKS(USB_BUS_RESET_DELAY));
197
198	/* we now own the host controller and the bus has been reset */
199	ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
200
201	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR);	/* Reset HC */
202	/* nominal time for a reset is 10 us */
203	for (i = 0; i < 10; i++) {
204		DELAY(10);
205		hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
206		if (!hcr) {
207			break;
208		}
209	}
210	if (hcr) {
211		device_printf(sc->sc_bus.bdev, "reset timeout\n");
212		return (USB_ERR_IOERROR);
213	}
214#if USB_DEBUG
215	if (ohcidebug > 15) {
216		ohci_dumpregs(sc);
217	}
218#endif
219
220	/* The controller is now in SUSPEND state, we have 2ms to finish. */
221
222	/* set up HC registers */
223	usb2_get_page(&sc->sc_hw.hcca_pc, 0, &buf_res);
224	OWRITE4(sc, OHCI_HCCA, buf_res.physaddr);
225
226	usb2_get_page(&sc->sc_hw.ctrl_start_pc, 0, &buf_res);
227	OWRITE4(sc, OHCI_CONTROL_HEAD_ED, buf_res.physaddr);
228
229	usb2_get_page(&sc->sc_hw.bulk_start_pc, 0, &buf_res);
230	OWRITE4(sc, OHCI_BULK_HEAD_ED, buf_res.physaddr);
231
232	/* disable all interrupts and then switch on all desired interrupts */
233	OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
234	OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);
235	/* switch on desired functional features */
236	ctl = OREAD4(sc, OHCI_CONTROL);
237	ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
238	ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
239	    OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
240	/* And finally start it! */
241	OWRITE4(sc, OHCI_CONTROL, ctl);
242
243	/*
244	 * The controller is now OPERATIONAL.  Set a some final
245	 * registers that should be set earlier, but that the
246	 * controller ignores when in the SUSPEND state.
247	 */
248	fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
249	fm |= OHCI_FSMPS(ival) | ival;
250	OWRITE4(sc, OHCI_FM_INTERVAL, fm);
251	per = OHCI_PERIODIC(ival);	/* 90% periodic */
252	OWRITE4(sc, OHCI_PERIODIC_START, per);
253
254	/* Fiddle the No OverCurrent Protection bit to avoid chip bug. */
255	desca = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
256	OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca | OHCI_NOCP);
257	OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC);	/* Enable port power */
258	usb2_pause_mtx(NULL,
259	    USB_MS_TO_TICKS(OHCI_ENABLE_POWER_DELAY));
260	OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca);
261
262	/*
263	 * The AMD756 requires a delay before re-reading the register,
264	 * otherwise it will occasionally report 0 ports.
265	 */
266	sc->sc_noport = 0;
267	for (i = 0; (i < 10) && (sc->sc_noport == 0); i++) {
268		usb2_pause_mtx(NULL,
269		    USB_MS_TO_TICKS(OHCI_READ_DESC_DELAY));
270		sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
271	}
272
273#if USB_DEBUG
274	if (ohcidebug > 5) {
275		ohci_dumpregs(sc);
276	}
277#endif
278	return (USB_ERR_NORMAL_COMPLETION);
279}
280
281static struct ohci_ed *
282ohci_init_ed(struct usb2_page_cache *pc)
283{
284	struct usb2_page_search buf_res;
285	struct ohci_ed *ed;
286
287	usb2_get_page(pc, 0, &buf_res);
288
289	ed = buf_res.buffer;
290
291	ed->ed_self = htole32(buf_res.physaddr);
292	ed->ed_flags = htole32(OHCI_ED_SKIP);
293	ed->page_cache = pc;
294
295	return (ed);
296}
297
298usb2_error_t
299ohci_init(ohci_softc_t *sc)
300{
301	struct usb2_page_search buf_res;
302	uint16_t i;
303	uint16_t bit;
304	uint16_t x;
305	uint16_t y;
306
307	DPRINTF("start\n");
308
309	sc->sc_eintrs = OHCI_NORMAL_INTRS;
310
311	/*
312	 * Setup all ED's
313	 */
314
315	sc->sc_ctrl_p_last =
316	    ohci_init_ed(&sc->sc_hw.ctrl_start_pc);
317
318	sc->sc_bulk_p_last =
319	    ohci_init_ed(&sc->sc_hw.bulk_start_pc);
320
321	sc->sc_isoc_p_last =
322	    ohci_init_ed(&sc->sc_hw.isoc_start_pc);
323
324	for (i = 0; i != OHCI_NO_EDS; i++) {
325		sc->sc_intr_p_last[i] =
326		    ohci_init_ed(sc->sc_hw.intr_start_pc + i);
327	}
328
329	/*
330	 * the QHs are arranged to give poll intervals that are
331	 * powers of 2 times 1ms
332	 */
333	bit = OHCI_NO_EDS / 2;
334	while (bit) {
335		x = bit;
336		while (x & bit) {
337			ohci_ed_t *ed_x;
338			ohci_ed_t *ed_y;
339
340			y = (x ^ bit) | (bit / 2);
341
342			/*
343			 * the next QH has half the poll interval
344			 */
345			ed_x = sc->sc_intr_p_last[x];
346			ed_y = sc->sc_intr_p_last[y];
347
348			ed_x->next = NULL;
349			ed_x->ed_next = ed_y->ed_self;
350
351			x++;
352		}
353		bit >>= 1;
354	}
355
356	if (1) {
357
358		ohci_ed_t *ed_int;
359		ohci_ed_t *ed_isc;
360
361		ed_int = sc->sc_intr_p_last[0];
362		ed_isc = sc->sc_isoc_p_last;
363
364		/* the last (1ms) QH */
365		ed_int->next = ed_isc;
366		ed_int->ed_next = ed_isc->ed_self;
367	}
368	usb2_get_page(&sc->sc_hw.hcca_pc, 0, &buf_res);
369
370	sc->sc_hcca_p = buf_res.buffer;
371
372	/*
373	 * Fill HCCA interrupt table.  The bit reversal is to get
374	 * the tree set up properly to spread the interrupts.
375	 */
376	for (i = 0; i != OHCI_NO_INTRS; i++) {
377		sc->sc_hcca_p->hcca_interrupt_table[i] =
378		    sc->sc_intr_p_last[i | (OHCI_NO_EDS / 2)]->ed_self;
379	}
380	/* flush all cache into memory */
381
382	usb2_bus_mem_flush_all(&sc->sc_bus, &ohci_iterate_hw_softc);
383
384	/* set up the bus struct */
385	sc->sc_bus.methods = &ohci_bus_methods;
386
387	usb2_callout_init_mtx(&sc->sc_tmo_rhsc, &sc->sc_bus.bus_mtx, 0);
388
389#if USB_DEBUG
390	if (ohcidebug > 15) {
391		for (i = 0; i != OHCI_NO_EDS; i++) {
392			printf("ed#%d ", i);
393			ohci_dump_ed(sc->sc_intr_p_last[i]);
394		}
395		printf("iso ");
396		ohci_dump_ed(sc->sc_isoc_p_last);
397	}
398#endif
399
400	sc->sc_bus.usbrev = USB_REV_1_0;
401
402	if (ohci_controller_init(sc)) {
403		return (USB_ERR_INVAL);
404	} else {
405		/* catch any lost interrupts */
406		ohci_do_poll(&sc->sc_bus);
407		return (USB_ERR_NORMAL_COMPLETION);
408	}
409}
410
411/*
412 * shut down the controller when the system is going down
413 */
414void
415ohci_detach(struct ohci_softc *sc)
416{
417	USB_BUS_LOCK(&sc->sc_bus);
418
419	usb2_callout_stop(&sc->sc_tmo_rhsc);
420
421	OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
422	OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
423
424	USB_BUS_UNLOCK(&sc->sc_bus);
425
426	/* XXX let stray task complete */
427	usb2_pause_mtx(NULL, hz / 20);
428
429	usb2_callout_drain(&sc->sc_tmo_rhsc);
430}
431
432/* NOTE: suspend/resume is called from
433 * interrupt context and cannot sleep!
434 */
435void
436ohci_suspend(ohci_softc_t *sc)
437{
438	uint32_t ctl;
439
440	USB_BUS_LOCK(&sc->sc_bus);
441
442#if USB_DEBUG
443	DPRINTF("\n");
444	if (ohcidebug > 2) {
445		ohci_dumpregs(sc);
446	}
447#endif
448
449	ctl = OREAD4(sc, OHCI_CONTROL) & ~OHCI_HCFS_MASK;
450	if (sc->sc_control == 0) {
451		/*
452		 * Preserve register values, in case that APM BIOS
453		 * does not recover them.
454		 */
455		sc->sc_control = ctl;
456		sc->sc_intre = OREAD4(sc, OHCI_INTERRUPT_ENABLE);
457	}
458	ctl |= OHCI_HCFS_SUSPEND;
459	OWRITE4(sc, OHCI_CONTROL, ctl);
460
461	usb2_pause_mtx(&sc->sc_bus.bus_mtx,
462	    USB_MS_TO_TICKS(USB_RESUME_WAIT));
463
464	USB_BUS_UNLOCK(&sc->sc_bus);
465}
466
467void
468ohci_resume(ohci_softc_t *sc)
469{
470	uint32_t ctl;
471
472#if USB_DEBUG
473	DPRINTF("\n");
474	if (ohcidebug > 2) {
475		ohci_dumpregs(sc);
476	}
477#endif
478	/* some broken BIOSes never initialize the Controller chip */
479	ohci_controller_init(sc);
480
481	USB_BUS_LOCK(&sc->sc_bus);
482	if (sc->sc_intre) {
483		OWRITE4(sc, OHCI_INTERRUPT_ENABLE,
484		    sc->sc_intre & (OHCI_ALL_INTRS | OHCI_MIE));
485	}
486	if (sc->sc_control)
487		ctl = sc->sc_control;
488	else
489		ctl = OREAD4(sc, OHCI_CONTROL);
490	ctl |= OHCI_HCFS_RESUME;
491	OWRITE4(sc, OHCI_CONTROL, ctl);
492	usb2_pause_mtx(&sc->sc_bus.bus_mtx,
493	    USB_MS_TO_TICKS(USB_RESUME_DELAY));
494	ctl = (ctl & ~OHCI_HCFS_MASK) | OHCI_HCFS_OPERATIONAL;
495	OWRITE4(sc, OHCI_CONTROL, ctl);
496	usb2_pause_mtx(&sc->sc_bus.bus_mtx,
497	    USB_MS_TO_TICKS(USB_RESUME_RECOVERY));
498	sc->sc_control = sc->sc_intre = 0;
499
500	USB_BUS_UNLOCK(&sc->sc_bus);
501
502	/* catch any lost interrupts */
503	ohci_do_poll(&sc->sc_bus);
504}
505
506#if USB_DEBUG
507static void
508ohci_dumpregs(ohci_softc_t *sc)
509{
510	struct ohci_hcca *hcca;
511
512	DPRINTF("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
513	    OREAD4(sc, OHCI_REVISION),
514	    OREAD4(sc, OHCI_CONTROL),
515	    OREAD4(sc, OHCI_COMMAND_STATUS));
516	DPRINTF("               intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
517	    OREAD4(sc, OHCI_INTERRUPT_STATUS),
518	    OREAD4(sc, OHCI_INTERRUPT_ENABLE),
519	    OREAD4(sc, OHCI_INTERRUPT_DISABLE));
520	DPRINTF("               hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
521	    OREAD4(sc, OHCI_HCCA),
522	    OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
523	    OREAD4(sc, OHCI_CONTROL_HEAD_ED));
524	DPRINTF("               ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
525	    OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
526	    OREAD4(sc, OHCI_BULK_HEAD_ED),
527	    OREAD4(sc, OHCI_BULK_CURRENT_ED));
528	DPRINTF("               done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
529	    OREAD4(sc, OHCI_DONE_HEAD),
530	    OREAD4(sc, OHCI_FM_INTERVAL),
531	    OREAD4(sc, OHCI_FM_REMAINING));
532	DPRINTF("               fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
533	    OREAD4(sc, OHCI_FM_NUMBER),
534	    OREAD4(sc, OHCI_PERIODIC_START),
535	    OREAD4(sc, OHCI_LS_THRESHOLD));
536	DPRINTF("               desca=0x%08x descb=0x%08x stat=0x%08x\n",
537	    OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
538	    OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
539	    OREAD4(sc, OHCI_RH_STATUS));
540	DPRINTF("               port1=0x%08x port2=0x%08x\n",
541	    OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
542	    OREAD4(sc, OHCI_RH_PORT_STATUS(2)));
543
544	hcca = ohci_get_hcca(sc);
545
546	DPRINTF("         HCCA: frame_number=0x%04x done_head=0x%08x\n",
547	    le32toh(hcca->hcca_frame_number),
548	    le32toh(hcca->hcca_done_head));
549}
550static void
551ohci_dump_tds(ohci_td_t *std)
552{
553	for (; std; std = std->obj_next) {
554		if (ohci_dump_td(std)) {
555			break;
556		}
557	}
558}
559
560static uint8_t
561ohci_dump_td(ohci_td_t *std)
562{
563	uint32_t td_flags;
564	uint8_t temp;
565
566	usb2_pc_cpu_invalidate(std->page_cache);
567
568	td_flags = le32toh(std->td_flags);
569	temp = (std->td_next == 0);
570
571	printf("TD(%p) at 0x%08x: %s%s%s%s%s delay=%d ec=%d "
572	    "cc=%d\ncbp=0x%08x next=0x%08x be=0x%08x\n",
573	    std, le32toh(std->td_self),
574	    (td_flags & OHCI_TD_R) ? "-R" : "",
575	    (td_flags & OHCI_TD_OUT) ? "-OUT" : "",
576	    (td_flags & OHCI_TD_IN) ? "-IN" : "",
577	    ((td_flags & OHCI_TD_TOGGLE_MASK) == OHCI_TD_TOGGLE_1) ? "-TOG1" : "",
578	    ((td_flags & OHCI_TD_TOGGLE_MASK) == OHCI_TD_TOGGLE_0) ? "-TOG0" : "",
579	    OHCI_TD_GET_DI(td_flags),
580	    OHCI_TD_GET_EC(td_flags),
581	    OHCI_TD_GET_CC(td_flags),
582	    le32toh(std->td_cbp),
583	    le32toh(std->td_next),
584	    le32toh(std->td_be));
585
586	return (temp);
587}
588
589static uint8_t
590ohci_dump_itd(ohci_itd_t *sitd)
591{
592	uint32_t itd_flags;
593	uint16_t i;
594	uint8_t temp;
595
596	usb2_pc_cpu_invalidate(sitd->page_cache);
597
598	itd_flags = le32toh(sitd->itd_flags);
599	temp = (sitd->itd_next == 0);
600
601	printf("ITD(%p) at 0x%08x: sf=%d di=%d fc=%d cc=%d\n"
602	    "bp0=0x%08x next=0x%08x be=0x%08x\n",
603	    sitd, le32toh(sitd->itd_self),
604	    OHCI_ITD_GET_SF(itd_flags),
605	    OHCI_ITD_GET_DI(itd_flags),
606	    OHCI_ITD_GET_FC(itd_flags),
607	    OHCI_ITD_GET_CC(itd_flags),
608	    le32toh(sitd->itd_bp0),
609	    le32toh(sitd->itd_next),
610	    le32toh(sitd->itd_be));
611	for (i = 0; i < OHCI_ITD_NOFFSET; i++) {
612		printf("offs[%d]=0x%04x ", i,
613		    (uint32_t)le16toh(sitd->itd_offset[i]));
614	}
615	printf("\n");
616
617	return (temp);
618}
619
620static void
621ohci_dump_itds(ohci_itd_t *sitd)
622{
623	for (; sitd; sitd = sitd->obj_next) {
624		if (ohci_dump_itd(sitd)) {
625			break;
626		}
627	}
628}
629
630static void
631ohci_dump_ed(ohci_ed_t *sed)
632{
633	uint32_t ed_flags;
634	uint32_t ed_headp;
635
636	usb2_pc_cpu_invalidate(sed->page_cache);
637
638	ed_flags = le32toh(sed->ed_flags);
639	ed_headp = le32toh(sed->ed_headp);
640
641	printf("ED(%p) at 0x%08x: addr=%d endpt=%d maxp=%d flags=%s%s%s%s%s\n"
642	    "tailp=0x%08x headflags=%s%s headp=0x%08x nexted=0x%08x\n",
643	    sed, le32toh(sed->ed_self),
644	    OHCI_ED_GET_FA(ed_flags),
645	    OHCI_ED_GET_EN(ed_flags),
646	    OHCI_ED_GET_MAXP(ed_flags),
647	    (ed_flags & OHCI_ED_DIR_OUT) ? "-OUT" : "",
648	    (ed_flags & OHCI_ED_DIR_IN) ? "-IN" : "",
649	    (ed_flags & OHCI_ED_SPEED) ? "-LOWSPEED" : "",
650	    (ed_flags & OHCI_ED_SKIP) ? "-SKIP" : "",
651	    (ed_flags & OHCI_ED_FORMAT_ISO) ? "-ISO" : "",
652	    le32toh(sed->ed_tailp),
653	    (ed_headp & OHCI_HALTED) ? "-HALTED" : "",
654	    (ed_headp & OHCI_TOGGLECARRY) ? "-CARRY" : "",
655	    le32toh(sed->ed_headp),
656	    le32toh(sed->ed_next));
657}
658
659#endif
660
661static void
662ohci_transfer_intr_enqueue(struct usb2_xfer *xfer)
663{
664	/* check for early completion */
665	if (ohci_check_transfer(xfer)) {
666		return;
667	}
668	/* put transfer on interrupt queue */
669	usb2_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
670
671	/* start timeout, if any */
672	if (xfer->timeout != 0) {
673		usb2_transfer_timeout_ms(xfer, &ohci_timeout, xfer->timeout);
674	}
675}
676
677#define	OHCI_APPEND_QH(sed,last) (last) = _ohci_append_qh(sed,last)
678static ohci_ed_t *
679_ohci_append_qh(ohci_ed_t *sed, ohci_ed_t *last)
680{
681	DPRINTFN(11, "%p to %p\n", sed, last);
682
683	if (sed->prev != NULL) {
684		/* should not happen */
685		DPRINTFN(0, "ED already linked!\n");
686		return (last);
687	}
688	/* (sc->sc_bus.bus_mtx) must be locked */
689
690	sed->next = last->next;
691	sed->ed_next = last->ed_next;
692	sed->ed_tailp = 0;
693
694	sed->prev = last;
695
696	usb2_pc_cpu_flush(sed->page_cache);
697
698	/*
699	 * the last->next->prev is never followed: sed->next->prev = sed;
700	 */
701
702	last->next = sed;
703	last->ed_next = sed->ed_self;
704
705	usb2_pc_cpu_flush(last->page_cache);
706
707	return (sed);
708}
709
710#define	OHCI_REMOVE_QH(sed,last) (last) = _ohci_remove_qh(sed,last)
711static ohci_ed_t *
712_ohci_remove_qh(ohci_ed_t *sed, ohci_ed_t *last)
713{
714	DPRINTFN(11, "%p from %p\n", sed, last);
715
716	/* (sc->sc_bus.bus_mtx) must be locked */
717
718	/* only remove if not removed from a queue */
719	if (sed->prev) {
720
721		sed->prev->next = sed->next;
722		sed->prev->ed_next = sed->ed_next;
723
724		usb2_pc_cpu_flush(sed->prev->page_cache);
725
726		if (sed->next) {
727			sed->next->prev = sed->prev;
728			usb2_pc_cpu_flush(sed->next->page_cache);
729		}
730		last = ((last == sed) ? sed->prev : last);
731
732		sed->prev = 0;
733
734		usb2_pc_cpu_flush(sed->page_cache);
735	}
736	return (last);
737}
738
739static void
740ohci_isoc_done(struct usb2_xfer *xfer)
741{
742	uint8_t nframes;
743	uint32_t *plen = xfer->frlengths;
744	volatile uint16_t *olen;
745	uint16_t len = 0;
746	ohci_itd_t *td = xfer->td_transfer_first;
747
748	while (1) {
749		if (td == NULL) {
750			panic("%s:%d: out of TD's\n",
751			    __FUNCTION__, __LINE__);
752		}
753#if USB_DEBUG
754		if (ohcidebug > 5) {
755			DPRINTF("isoc TD\n");
756			ohci_dump_itd(td);
757		}
758#endif
759		usb2_pc_cpu_invalidate(td->page_cache);
760
761		nframes = td->frames;
762		olen = &td->itd_offset[0];
763
764		if (nframes > 8) {
765			nframes = 8;
766		}
767		while (nframes--) {
768			len = le16toh(*olen);
769
770			if ((len >> 12) == OHCI_CC_NOT_ACCESSED) {
771				len = 0;
772			} else {
773				len &= ((1 << 12) - 1);
774			}
775
776			if (len > *plen) {
777				len = 0;/* invalid length */
778			}
779			*plen = len;
780			plen++;
781			olen++;
782		}
783
784		if (((void *)td) == xfer->td_transfer_last) {
785			break;
786		}
787		td = td->obj_next;
788	}
789
790	xfer->aframes = xfer->nframes;
791	ohci_device_done(xfer, USB_ERR_NORMAL_COMPLETION);
792}
793
794#if USB_DEBUG
795static const char *const
796	ohci_cc_strs[] =
797{
798	"NO_ERROR",
799	"CRC",
800	"BIT_STUFFING",
801	"DATA_TOGGLE_MISMATCH",
802
803	"STALL",
804	"DEVICE_NOT_RESPONDING",
805	"PID_CHECK_FAILURE",
806	"UNEXPECTED_PID",
807
808	"DATA_OVERRUN",
809	"DATA_UNDERRUN",
810	"BUFFER_OVERRUN",
811	"BUFFER_UNDERRUN",
812
813	"reserved",
814	"reserved",
815	"NOT_ACCESSED",
816	"NOT_ACCESSED"
817};
818
819#endif
820
821static usb2_error_t
822ohci_non_isoc_done_sub(struct usb2_xfer *xfer)
823{
824	ohci_td_t *td;
825	ohci_td_t *td_alt_next;
826	uint32_t temp;
827	uint32_t phy_start;
828	uint32_t phy_end;
829	uint32_t td_flags;
830	uint16_t cc;
831
832	td = xfer->td_transfer_cache;
833	td_alt_next = td->alt_next;
834	td_flags = 0;
835
836	if (xfer->aframes != xfer->nframes) {
837		xfer->frlengths[xfer->aframes] = 0;
838	}
839	while (1) {
840
841		usb2_pc_cpu_invalidate(td->page_cache);
842		phy_start = le32toh(td->td_cbp);
843		td_flags = le32toh(td->td_flags);
844		cc = OHCI_TD_GET_CC(td_flags);
845
846		if (phy_start) {
847			/*
848			 * short transfer - compute the number of remaining
849			 * bytes in the hardware buffer:
850			 */
851			phy_end = le32toh(td->td_be);
852			temp = (OHCI_PAGE(phy_start ^ phy_end) ?
853			    (OHCI_PAGE_SIZE + 1) : 0x0001);
854			temp += OHCI_PAGE_OFFSET(phy_end);
855			temp -= OHCI_PAGE_OFFSET(phy_start);
856
857			if (temp > td->len) {
858				/* guard against corruption */
859				cc = OHCI_CC_STALL;
860			} else if (xfer->aframes != xfer->nframes) {
861				/*
862				 * Sum up total transfer length
863				 * in "frlengths[]":
864				 */
865				xfer->frlengths[xfer->aframes] += td->len - temp;
866			}
867		} else {
868			if (xfer->aframes != xfer->nframes) {
869				/* transfer was complete */
870				xfer->frlengths[xfer->aframes] += td->len;
871			}
872		}
873		/* Check for last transfer */
874		if (((void *)td) == xfer->td_transfer_last) {
875			td = NULL;
876			break;
877		}
878		/* Check transfer status */
879		if (cc) {
880			/* the transfer is finished */
881			td = NULL;
882			break;
883		}
884		/* Check for short transfer */
885		if (phy_start) {
886			if (xfer->flags_int.short_frames_ok) {
887				/* follow alt next */
888				td = td->alt_next;
889			} else {
890				/* the transfer is finished */
891				td = NULL;
892			}
893			break;
894		}
895		td = td->obj_next;
896
897		if (td->alt_next != td_alt_next) {
898			/* this USB frame is complete */
899			break;
900		}
901	}
902
903	/* update transfer cache */
904
905	xfer->td_transfer_cache = td;
906
907	DPRINTFN(16, "error cc=%d (%s)\n",
908	    cc, ohci_cc_strs[cc]);
909
910	return ((cc == 0) ? USB_ERR_NORMAL_COMPLETION :
911	    (cc == OHCI_CC_STALL) ? USB_ERR_STALLED : USB_ERR_IOERROR);
912}
913
914static void
915ohci_non_isoc_done(struct usb2_xfer *xfer)
916{
917	usb2_error_t err = 0;
918
919	DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
920	    xfer, xfer->pipe);
921
922#if USB_DEBUG
923	if (ohcidebug > 10) {
924		ohci_dump_tds(xfer->td_transfer_first);
925	}
926#endif
927
928	/* reset scanner */
929
930	xfer->td_transfer_cache = xfer->td_transfer_first;
931
932	if (xfer->flags_int.control_xfr) {
933
934		if (xfer->flags_int.control_hdr) {
935
936			err = ohci_non_isoc_done_sub(xfer);
937		}
938		xfer->aframes = 1;
939
940		if (xfer->td_transfer_cache == NULL) {
941			goto done;
942		}
943	}
944	while (xfer->aframes != xfer->nframes) {
945
946		err = ohci_non_isoc_done_sub(xfer);
947		xfer->aframes++;
948
949		if (xfer->td_transfer_cache == NULL) {
950			goto done;
951		}
952	}
953
954	if (xfer->flags_int.control_xfr &&
955	    !xfer->flags_int.control_act) {
956
957		err = ohci_non_isoc_done_sub(xfer);
958	}
959done:
960	ohci_device_done(xfer, err);
961}
962
963/*------------------------------------------------------------------------*
964 *	ohci_check_transfer_sub
965 *------------------------------------------------------------------------*/
966static void
967ohci_check_transfer_sub(struct usb2_xfer *xfer)
968{
969	ohci_td_t *td;
970	ohci_ed_t *ed;
971	uint32_t phy_start;
972	uint32_t td_flags;
973	uint32_t td_next;
974	uint16_t cc;
975
976	td = xfer->td_transfer_cache;
977
978	while (1) {
979
980		usb2_pc_cpu_invalidate(td->page_cache);
981		phy_start = le32toh(td->td_cbp);
982		td_flags = le32toh(td->td_flags);
983		td_next = le32toh(td->td_next);
984
985		/* Check for last transfer */
986		if (((void *)td) == xfer->td_transfer_last) {
987			/* the transfer is finished */
988			td = NULL;
989			break;
990		}
991		/* Check transfer status */
992		cc = OHCI_TD_GET_CC(td_flags);
993		if (cc) {
994			/* the transfer is finished */
995			td = NULL;
996			break;
997		}
998		/*
999	         * Check if we reached the last packet
1000	         * or if there is a short packet:
1001	         */
1002
1003		if (((td_next & (~0xF)) == OHCI_TD_NEXT_END) || phy_start) {
1004			/* follow alt next */
1005			td = td->alt_next;
1006			break;
1007		}
1008		td = td->obj_next;
1009	}
1010
1011	/* update transfer cache */
1012
1013	xfer->td_transfer_cache = td;
1014
1015	if (td) {
1016
1017		ed = xfer->qh_start[xfer->flags_int.curr_dma_set];
1018
1019		ed->ed_headp = td->td_self;
1020		usb2_pc_cpu_flush(ed->page_cache);
1021
1022		DPRINTFN(13, "xfer=%p following alt next\n", xfer);
1023
1024		/*
1025		 * Make sure that the OHCI re-scans the schedule by
1026		 * writing the BLF and CLF bits:
1027		 */
1028
1029		if (xfer->xroot->udev->pwr_save.suspended) {
1030			/* nothing to do */
1031		} else if (xfer->pipe->methods == &ohci_device_bulk_methods) {
1032			ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
1033
1034			OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
1035		} else if (xfer->pipe->methods == &ohci_device_ctrl_methods) {
1036			ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
1037
1038			OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1039		}
1040	}
1041}
1042
1043/*------------------------------------------------------------------------*
1044 *	ohci_check_transfer
1045 *
1046 * Return values:
1047 *    0: USB transfer is not finished
1048 * Else: USB transfer is finished
1049 *------------------------------------------------------------------------*/
1050static uint8_t
1051ohci_check_transfer(struct usb2_xfer *xfer)
1052{
1053	ohci_ed_t *ed;
1054	uint32_t ed_headp;
1055	uint32_t ed_tailp;
1056
1057	DPRINTFN(13, "xfer=%p checking transfer\n", xfer);
1058
1059	ed = xfer->qh_start[xfer->flags_int.curr_dma_set];
1060
1061	usb2_pc_cpu_invalidate(ed->page_cache);
1062	ed_headp = le32toh(ed->ed_headp);
1063	ed_tailp = le32toh(ed->ed_tailp);
1064
1065	if ((ed_headp & OHCI_HALTED) ||
1066	    (((ed_headp ^ ed_tailp) & (~0xF)) == 0)) {
1067		if (xfer->pipe->methods == &ohci_device_isoc_methods) {
1068			/* isochronous transfer */
1069			ohci_isoc_done(xfer);
1070		} else {
1071			if (xfer->flags_int.short_frames_ok) {
1072				ohci_check_transfer_sub(xfer);
1073				if (xfer->td_transfer_cache) {
1074					/* not finished yet */
1075					return (0);
1076				}
1077			}
1078			/* store data-toggle */
1079			if (ed_headp & OHCI_TOGGLECARRY) {
1080				xfer->pipe->toggle_next = 1;
1081			} else {
1082				xfer->pipe->toggle_next = 0;
1083			}
1084
1085			/* non-isochronous transfer */
1086			ohci_non_isoc_done(xfer);
1087		}
1088		return (1);
1089	}
1090	DPRINTFN(13, "xfer=%p is still active\n", xfer);
1091	return (0);
1092}
1093
1094static void
1095ohci_rhsc_enable(ohci_softc_t *sc)
1096{
1097	DPRINTFN(5, "\n");
1098
1099	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1100
1101	sc->sc_eintrs |= OHCI_RHSC;
1102	OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
1103
1104	/* acknowledge any RHSC interrupt */
1105	OWRITE4(sc, OHCI_INTERRUPT_STATUS, OHCI_RHSC);
1106
1107	usb2_sw_transfer(&sc->sc_root_intr,
1108	    &ohci_root_intr_done);
1109}
1110
1111static void
1112ohci_interrupt_poll(ohci_softc_t *sc)
1113{
1114	struct usb2_xfer *xfer;
1115
1116repeat:
1117	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
1118		/*
1119		 * check if transfer is transferred
1120		 */
1121		if (ohci_check_transfer(xfer)) {
1122			/* queue has been modified */
1123			goto repeat;
1124		}
1125	}
1126}
1127
1128/*------------------------------------------------------------------------*
1129 *	ohci_interrupt - OHCI interrupt handler
1130 *
1131 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler,
1132 * hence the interrupt handler will be setup before "sc->sc_bus.bdev"
1133 * is present !
1134 *------------------------------------------------------------------------*/
1135void
1136ohci_interrupt(ohci_softc_t *sc)
1137{
1138	struct ohci_hcca *hcca;
1139	uint32_t status;
1140	uint32_t done;
1141
1142	USB_BUS_LOCK(&sc->sc_bus);
1143
1144	hcca = ohci_get_hcca(sc);
1145
1146	DPRINTFN(16, "real interrupt\n");
1147
1148#if USB_DEBUG
1149	if (ohcidebug > 15) {
1150		ohci_dumpregs(sc);
1151	}
1152#endif
1153
1154	done = le32toh(hcca->hcca_done_head);
1155
1156	/*
1157	 * The LSb of done is used to inform the HC Driver that an interrupt
1158	 * condition exists for both the Done list and for another event
1159	 * recorded in HcInterruptStatus. On an interrupt from the HC, the
1160	 * HC Driver checks the HccaDoneHead Value. If this value is 0, then
1161	 * the interrupt was caused by other than the HccaDoneHead update
1162	 * and the HcInterruptStatus register needs to be accessed to
1163	 * determine that exact interrupt cause. If HccaDoneHead is nonzero,
1164	 * then a Done list update interrupt is indicated and if the LSb of
1165	 * done is nonzero, then an additional interrupt event is indicated
1166	 * and HcInterruptStatus should be checked to determine its cause.
1167	 */
1168	if (done != 0) {
1169		status = 0;
1170
1171		if (done & ~OHCI_DONE_INTRS) {
1172			status |= OHCI_WDH;
1173		}
1174		if (done & OHCI_DONE_INTRS) {
1175			status |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
1176		}
1177		hcca->hcca_done_head = 0;
1178
1179		usb2_pc_cpu_flush(&sc->sc_hw.hcca_pc);
1180	} else {
1181		status = OREAD4(sc, OHCI_INTERRUPT_STATUS) & ~OHCI_WDH;
1182	}
1183
1184	status &= ~OHCI_MIE;
1185	if (status == 0) {
1186		/*
1187		 * nothing to be done (PCI shared
1188		 * interrupt)
1189		 */
1190		goto done;
1191	}
1192	OWRITE4(sc, OHCI_INTERRUPT_STATUS, status);	/* Acknowledge */
1193
1194	status &= sc->sc_eintrs;
1195	if (status == 0) {
1196		goto done;
1197	}
1198	if (status & (OHCI_SO | OHCI_RD | OHCI_UE | OHCI_RHSC)) {
1199#if 0
1200		if (status & OHCI_SO) {
1201			/* XXX do what */
1202		}
1203#endif
1204		if (status & OHCI_RD) {
1205			printf("%s: resume detect\n", __FUNCTION__);
1206			/* XXX process resume detect */
1207		}
1208		if (status & OHCI_UE) {
1209			printf("%s: unrecoverable error, "
1210			    "controller halted\n", __FUNCTION__);
1211			OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1212			/* XXX what else */
1213		}
1214		if (status & OHCI_RHSC) {
1215			/*
1216			 * Disable RHSC interrupt for now, because it will be
1217			 * on until the port has been reset.
1218			 */
1219			sc->sc_eintrs &= ~OHCI_RHSC;
1220			OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
1221
1222			usb2_sw_transfer(&sc->sc_root_intr,
1223			    &ohci_root_intr_done);
1224
1225			/* do not allow RHSC interrupts > 1 per second */
1226			usb2_callout_reset(&sc->sc_tmo_rhsc, hz,
1227			    (void *)&ohci_rhsc_enable, sc);
1228		}
1229	}
1230	status &= ~(OHCI_RHSC | OHCI_WDH | OHCI_SO);
1231	if (status != 0) {
1232		/* Block unprocessed interrupts. XXX */
1233		OWRITE4(sc, OHCI_INTERRUPT_DISABLE, status);
1234		sc->sc_eintrs &= ~status;
1235		printf("%s: blocking intrs 0x%x\n",
1236		    __FUNCTION__, status);
1237	}
1238	/* poll all the USB transfers */
1239	ohci_interrupt_poll(sc);
1240
1241done:
1242	USB_BUS_UNLOCK(&sc->sc_bus);
1243}
1244
1245/*
1246 * called when a request does not complete
1247 */
1248static void
1249ohci_timeout(void *arg)
1250{
1251	struct usb2_xfer *xfer = arg;
1252
1253	DPRINTF("xfer=%p\n", xfer);
1254
1255	USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1256
1257	/* transfer is transferred */
1258	ohci_device_done(xfer, USB_ERR_TIMEOUT);
1259}
1260
1261static void
1262ohci_do_poll(struct usb2_bus *bus)
1263{
1264	struct ohci_softc *sc = OHCI_BUS2SC(bus);
1265
1266	USB_BUS_LOCK(&sc->sc_bus);
1267	ohci_interrupt_poll(sc);
1268	ohci_root_ctrl_poll(sc);
1269	USB_BUS_UNLOCK(&sc->sc_bus);
1270}
1271
1272static void
1273ohci_setup_standard_chain_sub(struct ohci_std_temp *temp)
1274{
1275	struct usb2_page_search buf_res;
1276	ohci_td_t *td;
1277	ohci_td_t *td_next;
1278	ohci_td_t *td_alt_next;
1279	uint32_t buf_offset;
1280	uint32_t average;
1281	uint32_t len_old;
1282	uint8_t shortpkt_old;
1283	uint8_t precompute;
1284
1285	td_alt_next = NULL;
1286	buf_offset = 0;
1287	shortpkt_old = temp->shortpkt;
1288	len_old = temp->len;
1289	precompute = 1;
1290
1291	/* software is used to detect short incoming transfers */
1292
1293	if ((temp->td_flags & htole32(OHCI_TD_DP_MASK)) == htole32(OHCI_TD_IN)) {
1294		temp->td_flags |= htole32(OHCI_TD_R);
1295	} else {
1296		temp->td_flags &= ~htole32(OHCI_TD_R);
1297	}
1298
1299restart:
1300
1301	td = temp->td;
1302	td_next = temp->td_next;
1303
1304	while (1) {
1305
1306		if (temp->len == 0) {
1307
1308			if (temp->shortpkt) {
1309				break;
1310			}
1311			/* send a Zero Length Packet, ZLP, last */
1312
1313			temp->shortpkt = 1;
1314			average = 0;
1315
1316		} else {
1317
1318			average = temp->average;
1319
1320			if (temp->len < average) {
1321				if (temp->len % temp->max_frame_size) {
1322					temp->shortpkt = 1;
1323				}
1324				average = temp->len;
1325			}
1326		}
1327
1328		if (td_next == NULL) {
1329			panic("%s: out of OHCI transfer descriptors!", __FUNCTION__);
1330		}
1331		/* get next TD */
1332
1333		td = td_next;
1334		td_next = td->obj_next;
1335
1336		/* check if we are pre-computing */
1337
1338		if (precompute) {
1339
1340			/* update remaining length */
1341
1342			temp->len -= average;
1343
1344			continue;
1345		}
1346		/* fill out current TD */
1347		td->td_flags = temp->td_flags;
1348
1349		/* the next TD uses TOGGLE_CARRY */
1350		temp->td_flags &= ~htole32(OHCI_TD_TOGGLE_MASK);
1351
1352		if (average == 0) {
1353
1354			td->td_cbp = 0;
1355			td->td_be = ~0;
1356			td->len = 0;
1357
1358		} else {
1359
1360			usb2_get_page(temp->pc, buf_offset, &buf_res);
1361			td->td_cbp = htole32(buf_res.physaddr);
1362			buf_offset += (average - 1);
1363
1364			usb2_get_page(temp->pc, buf_offset, &buf_res);
1365			td->td_be = htole32(buf_res.physaddr);
1366			buf_offset++;
1367
1368			td->len = average;
1369
1370			/* update remaining length */
1371
1372			temp->len -= average;
1373		}
1374
1375		if ((td_next == td_alt_next) && temp->setup_alt_next) {
1376			/* we need to receive these frames one by one ! */
1377			td->td_flags &= htole32(~OHCI_TD_INTR_MASK);
1378			td->td_flags |= htole32(OHCI_TD_SET_DI(1));
1379			td->td_next = htole32(OHCI_TD_NEXT_END);
1380		} else {
1381			if (td_next) {
1382				/* link the current TD with the next one */
1383				td->td_next = td_next->td_self;
1384			}
1385		}
1386
1387		td->alt_next = td_alt_next;
1388
1389		usb2_pc_cpu_flush(td->page_cache);
1390	}
1391
1392	if (precompute) {
1393		precompute = 0;
1394
1395		/* setup alt next pointer, if any */
1396		if (temp->last_frame) {
1397			/* no alternate next */
1398			td_alt_next = NULL;
1399		} else {
1400			/* we use this field internally */
1401			td_alt_next = td_next;
1402		}
1403
1404		/* restore */
1405		temp->shortpkt = shortpkt_old;
1406		temp->len = len_old;
1407		goto restart;
1408	}
1409	temp->td = td;
1410	temp->td_next = td_next;
1411}
1412
1413static void
1414ohci_setup_standard_chain(struct usb2_xfer *xfer, ohci_ed_t **ed_last)
1415{
1416	struct ohci_std_temp temp;
1417	struct usb2_pipe_methods *methods;
1418	ohci_ed_t *ed;
1419	ohci_td_t *td;
1420	uint32_t ed_flags;
1421	uint32_t x;
1422
1423	DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1424	    xfer->address, UE_GET_ADDR(xfer->endpoint),
1425	    xfer->sumlen, usb2_get_speed(xfer->xroot->udev));
1426
1427	temp.average = xfer->max_hc_frame_size;
1428	temp.max_frame_size = xfer->max_frame_size;
1429
1430	/* toggle the DMA set we are using */
1431	xfer->flags_int.curr_dma_set ^= 1;
1432
1433	/* get next DMA set */
1434	td = xfer->td_start[xfer->flags_int.curr_dma_set];
1435
1436	xfer->td_transfer_first = td;
1437	xfer->td_transfer_cache = td;
1438
1439	temp.td = NULL;
1440	temp.td_next = td;
1441	temp.last_frame = 0;
1442	temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1443
1444	methods = xfer->pipe->methods;
1445
1446	/* check if we should prepend a setup message */
1447
1448	if (xfer->flags_int.control_xfr) {
1449		if (xfer->flags_int.control_hdr) {
1450
1451			temp.td_flags = htole32(OHCI_TD_SETUP | OHCI_TD_NOCC |
1452			    OHCI_TD_TOGGLE_0 | OHCI_TD_NOINTR);
1453
1454			temp.len = xfer->frlengths[0];
1455			temp.pc = xfer->frbuffers + 0;
1456			temp.shortpkt = temp.len ? 1 : 0;
1457			/* check for last frame */
1458			if (xfer->nframes == 1) {
1459				/* no STATUS stage yet, SETUP is last */
1460				if (xfer->flags_int.control_act) {
1461					temp.last_frame = 1;
1462					temp.setup_alt_next = 0;
1463				}
1464			}
1465			ohci_setup_standard_chain_sub(&temp);
1466
1467			/*
1468			 * XXX assume that the setup message is
1469			 * contained within one USB packet:
1470			 */
1471			xfer->pipe->toggle_next = 1;
1472		}
1473		x = 1;
1474	} else {
1475		x = 0;
1476	}
1477	temp.td_flags = htole32(OHCI_TD_NOCC | OHCI_TD_NOINTR);
1478
1479	/* set data toggle */
1480
1481	if (xfer->pipe->toggle_next) {
1482		temp.td_flags |= htole32(OHCI_TD_TOGGLE_1);
1483	} else {
1484		temp.td_flags |= htole32(OHCI_TD_TOGGLE_0);
1485	}
1486
1487	/* set endpoint direction */
1488
1489	if (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) {
1490		temp.td_flags |= htole32(OHCI_TD_IN);
1491	} else {
1492		temp.td_flags |= htole32(OHCI_TD_OUT);
1493	}
1494
1495	while (x != xfer->nframes) {
1496
1497		/* DATA0 / DATA1 message */
1498
1499		temp.len = xfer->frlengths[x];
1500		temp.pc = xfer->frbuffers + x;
1501
1502		x++;
1503
1504		if (x == xfer->nframes) {
1505			if (xfer->flags_int.control_xfr) {
1506				/* no STATUS stage yet, DATA is last */
1507				if (xfer->flags_int.control_act) {
1508					temp.last_frame = 1;
1509					temp.setup_alt_next = 0;
1510				}
1511			} else {
1512				temp.last_frame = 1;
1513				temp.setup_alt_next = 0;
1514			}
1515		}
1516		if (temp.len == 0) {
1517
1518			/* make sure that we send an USB packet */
1519
1520			temp.shortpkt = 0;
1521
1522		} else {
1523
1524			/* regular data transfer */
1525
1526			temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1;
1527		}
1528
1529		ohci_setup_standard_chain_sub(&temp);
1530	}
1531
1532	/* check if we should append a status stage */
1533
1534	if (xfer->flags_int.control_xfr &&
1535	    !xfer->flags_int.control_act) {
1536
1537		/*
1538		 * Send a DATA1 message and invert the current endpoint
1539		 * direction.
1540		 */
1541
1542		/* set endpoint direction and data toggle */
1543
1544		if (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN) {
1545			temp.td_flags = htole32(OHCI_TD_OUT |
1546			    OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1547		} else {
1548			temp.td_flags = htole32(OHCI_TD_IN |
1549			    OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1550		}
1551
1552		temp.len = 0;
1553		temp.pc = NULL;
1554		temp.shortpkt = 0;
1555		temp.last_frame = 1;
1556		temp.setup_alt_next = 0;
1557
1558		ohci_setup_standard_chain_sub(&temp);
1559	}
1560	td = temp.td;
1561
1562	/* Ensure that last TD is terminating: */
1563	td->td_next = htole32(OHCI_TD_NEXT_END);
1564	td->td_flags &= ~htole32(OHCI_TD_INTR_MASK);
1565	td->td_flags |= htole32(OHCI_TD_SET_DI(1));
1566
1567	usb2_pc_cpu_flush(td->page_cache);
1568
1569	/* must have at least one frame! */
1570
1571	xfer->td_transfer_last = td;
1572
1573#if USB_DEBUG
1574	if (ohcidebug > 8) {
1575		DPRINTF("nexttog=%d; data before transfer:\n",
1576		    xfer->pipe->toggle_next);
1577		ohci_dump_tds(xfer->td_transfer_first);
1578	}
1579#endif
1580
1581	ed = xfer->qh_start[xfer->flags_int.curr_dma_set];
1582
1583	ed_flags = (OHCI_ED_SET_FA(xfer->address) |
1584	    OHCI_ED_SET_EN(UE_GET_ADDR(xfer->endpoint)) |
1585	    OHCI_ED_SET_MAXP(xfer->max_frame_size));
1586
1587	ed_flags |= (OHCI_ED_FORMAT_GEN | OHCI_ED_DIR_TD);
1588
1589	if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
1590		ed_flags |= OHCI_ED_SPEED;
1591	}
1592	ed->ed_flags = htole32(ed_flags);
1593
1594	td = xfer->td_transfer_first;
1595
1596	ed->ed_headp = td->td_self;
1597
1598	if (xfer->xroot->udev->pwr_save.suspended == 0) {
1599		/* the append function will flush the endpoint descriptor */
1600		OHCI_APPEND_QH(ed, *ed_last);
1601
1602		if (methods == &ohci_device_bulk_methods) {
1603			ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
1604
1605			OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
1606		}
1607		if (methods == &ohci_device_ctrl_methods) {
1608			ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
1609
1610			OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1611		}
1612	} else {
1613		usb2_pc_cpu_flush(ed->page_cache);
1614	}
1615}
1616
1617static void
1618ohci_root_intr_done(struct usb2_xfer *xfer,
1619    struct usb2_sw_transfer *std)
1620{
1621	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
1622	uint32_t hstatus;
1623	uint16_t i;
1624	uint16_t m;
1625
1626	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1627
1628	if (std->state != USB_SW_TR_PRE_DATA) {
1629		if (std->state == USB_SW_TR_PRE_CALLBACK) {
1630			/* transfer transferred */
1631			ohci_device_done(xfer, std->err);
1632		}
1633		goto done;
1634	}
1635	/* setup buffer */
1636	std->ptr = sc->sc_hub_idata;
1637	std->len = sizeof(sc->sc_hub_idata);
1638
1639	/* clear any old interrupt data */
1640	bzero(sc->sc_hub_idata, sizeof(sc->sc_hub_idata));
1641
1642	hstatus = OREAD4(sc, OHCI_RH_STATUS);
1643	DPRINTF("sc=%p xfer=%p hstatus=0x%08x\n",
1644	    sc, xfer, hstatus);
1645
1646	/* set bits */
1647	m = (sc->sc_noport + 1);
1648	if (m > (8 * sizeof(sc->sc_hub_idata))) {
1649		m = (8 * sizeof(sc->sc_hub_idata));
1650	}
1651	for (i = 1; i < m; i++) {
1652		/* pick out CHANGE bits from the status register */
1653		if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16) {
1654			sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
1655			DPRINTF("port %d changed\n", i);
1656		}
1657	}
1658done:
1659	return;
1660}
1661
1662/* NOTE: "done" can be run two times in a row,
1663 * from close and from interrupt
1664 */
1665static void
1666ohci_device_done(struct usb2_xfer *xfer, usb2_error_t error)
1667{
1668	struct usb2_pipe_methods *methods = xfer->pipe->methods;
1669	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
1670	ohci_ed_t *ed;
1671
1672	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1673
1674
1675	DPRINTFN(2, "xfer=%p, pipe=%p, error=%d\n",
1676	    xfer, xfer->pipe, error);
1677
1678	ed = xfer->qh_start[xfer->flags_int.curr_dma_set];
1679	if (ed) {
1680		usb2_pc_cpu_invalidate(ed->page_cache);
1681	}
1682	if (methods == &ohci_device_bulk_methods) {
1683		OHCI_REMOVE_QH(ed, sc->sc_bulk_p_last);
1684	}
1685	if (methods == &ohci_device_ctrl_methods) {
1686		OHCI_REMOVE_QH(ed, sc->sc_ctrl_p_last);
1687	}
1688	if (methods == &ohci_device_intr_methods) {
1689		OHCI_REMOVE_QH(ed, sc->sc_intr_p_last[xfer->qh_pos]);
1690	}
1691	if (methods == &ohci_device_isoc_methods) {
1692		OHCI_REMOVE_QH(ed, sc->sc_isoc_p_last);
1693	}
1694	xfer->td_transfer_first = NULL;
1695	xfer->td_transfer_last = NULL;
1696
1697	/* dequeue transfer and start next transfer */
1698	usb2_transfer_done(xfer, error);
1699}
1700
1701/*------------------------------------------------------------------------*
1702 * ohci bulk support
1703 *------------------------------------------------------------------------*/
1704static void
1705ohci_device_bulk_open(struct usb2_xfer *xfer)
1706{
1707	return;
1708}
1709
1710static void
1711ohci_device_bulk_close(struct usb2_xfer *xfer)
1712{
1713	ohci_device_done(xfer, USB_ERR_CANCELLED);
1714}
1715
1716static void
1717ohci_device_bulk_enter(struct usb2_xfer *xfer)
1718{
1719	return;
1720}
1721
1722static void
1723ohci_device_bulk_start(struct usb2_xfer *xfer)
1724{
1725	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
1726
1727	/* setup TD's and QH */
1728	ohci_setup_standard_chain(xfer, &sc->sc_bulk_p_last);
1729
1730	/* put transfer on interrupt queue */
1731	ohci_transfer_intr_enqueue(xfer);
1732}
1733
1734struct usb2_pipe_methods ohci_device_bulk_methods =
1735{
1736	.open = ohci_device_bulk_open,
1737	.close = ohci_device_bulk_close,
1738	.enter = ohci_device_bulk_enter,
1739	.start = ohci_device_bulk_start,
1740	.enter_is_cancelable = 1,
1741	.start_is_cancelable = 1,
1742};
1743
1744/*------------------------------------------------------------------------*
1745 * ohci control support
1746 *------------------------------------------------------------------------*/
1747static void
1748ohci_device_ctrl_open(struct usb2_xfer *xfer)
1749{
1750	return;
1751}
1752
1753static void
1754ohci_device_ctrl_close(struct usb2_xfer *xfer)
1755{
1756	ohci_device_done(xfer, USB_ERR_CANCELLED);
1757}
1758
1759static void
1760ohci_device_ctrl_enter(struct usb2_xfer *xfer)
1761{
1762	return;
1763}
1764
1765static void
1766ohci_device_ctrl_start(struct usb2_xfer *xfer)
1767{
1768	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
1769
1770	/* setup TD's and QH */
1771	ohci_setup_standard_chain(xfer, &sc->sc_ctrl_p_last);
1772
1773	/* put transfer on interrupt queue */
1774	ohci_transfer_intr_enqueue(xfer);
1775}
1776
1777struct usb2_pipe_methods ohci_device_ctrl_methods =
1778{
1779	.open = ohci_device_ctrl_open,
1780	.close = ohci_device_ctrl_close,
1781	.enter = ohci_device_ctrl_enter,
1782	.start = ohci_device_ctrl_start,
1783	.enter_is_cancelable = 1,
1784	.start_is_cancelable = 1,
1785};
1786
1787/*------------------------------------------------------------------------*
1788 * ohci interrupt support
1789 *------------------------------------------------------------------------*/
1790static void
1791ohci_device_intr_open(struct usb2_xfer *xfer)
1792{
1793	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
1794	uint16_t best;
1795	uint16_t bit;
1796	uint16_t x;
1797
1798	best = 0;
1799	bit = OHCI_NO_EDS / 2;
1800	while (bit) {
1801		if (xfer->interval >= bit) {
1802			x = bit;
1803			best = bit;
1804			while (x & bit) {
1805				if (sc->sc_intr_stat[x] <
1806				    sc->sc_intr_stat[best]) {
1807					best = x;
1808				}
1809				x++;
1810			}
1811			break;
1812		}
1813		bit >>= 1;
1814	}
1815
1816	sc->sc_intr_stat[best]++;
1817	xfer->qh_pos = best;
1818
1819	DPRINTFN(3, "best=%d interval=%d\n",
1820	    best, xfer->interval);
1821}
1822
1823static void
1824ohci_device_intr_close(struct usb2_xfer *xfer)
1825{
1826	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
1827
1828	sc->sc_intr_stat[xfer->qh_pos]--;
1829
1830	ohci_device_done(xfer, USB_ERR_CANCELLED);
1831}
1832
1833static void
1834ohci_device_intr_enter(struct usb2_xfer *xfer)
1835{
1836	return;
1837}
1838
1839static void
1840ohci_device_intr_start(struct usb2_xfer *xfer)
1841{
1842	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
1843
1844	/* setup TD's and QH */
1845	ohci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]);
1846
1847	/* put transfer on interrupt queue */
1848	ohci_transfer_intr_enqueue(xfer);
1849}
1850
1851struct usb2_pipe_methods ohci_device_intr_methods =
1852{
1853	.open = ohci_device_intr_open,
1854	.close = ohci_device_intr_close,
1855	.enter = ohci_device_intr_enter,
1856	.start = ohci_device_intr_start,
1857	.enter_is_cancelable = 1,
1858	.start_is_cancelable = 1,
1859};
1860
1861/*------------------------------------------------------------------------*
1862 * ohci isochronous support
1863 *------------------------------------------------------------------------*/
1864static void
1865ohci_device_isoc_open(struct usb2_xfer *xfer)
1866{
1867	return;
1868}
1869
1870static void
1871ohci_device_isoc_close(struct usb2_xfer *xfer)
1872{
1873	/**/
1874	ohci_device_done(xfer, USB_ERR_CANCELLED);
1875}
1876
1877static void
1878ohci_device_isoc_enter(struct usb2_xfer *xfer)
1879{
1880	struct usb2_page_search buf_res;
1881	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
1882	struct ohci_hcca *hcca;
1883	uint32_t buf_offset;
1884	uint32_t nframes;
1885	uint32_t ed_flags;
1886	uint32_t *plen;
1887	uint16_t itd_offset[OHCI_ITD_NOFFSET];
1888	uint16_t length;
1889	uint8_t ncur;
1890	ohci_itd_t *td;
1891	ohci_itd_t *td_last = NULL;
1892	ohci_ed_t *ed;
1893
1894	hcca = ohci_get_hcca(sc);
1895
1896	nframes = le32toh(hcca->hcca_frame_number);
1897
1898	DPRINTFN(6, "xfer=%p isoc_next=%u nframes=%u hcca_fn=%u\n",
1899	    xfer, xfer->pipe->isoc_next, xfer->nframes, nframes);
1900
1901	if ((xfer->pipe->is_synced == 0) ||
1902	    (((nframes - xfer->pipe->isoc_next) & 0xFFFF) < xfer->nframes) ||
1903	    (((xfer->pipe->isoc_next - nframes) & 0xFFFF) >= 128)) {
1904		/*
1905		 * If there is data underflow or the pipe queue is empty we
1906		 * schedule the transfer a few frames ahead of the current
1907		 * frame position. Else two isochronous transfers might
1908		 * overlap.
1909		 */
1910		xfer->pipe->isoc_next = (nframes + 3) & 0xFFFF;
1911		xfer->pipe->is_synced = 1;
1912		DPRINTFN(3, "start next=%d\n", xfer->pipe->isoc_next);
1913	}
1914	/*
1915	 * compute how many milliseconds the insertion is ahead of the
1916	 * current frame position:
1917	 */
1918	buf_offset = ((xfer->pipe->isoc_next - nframes) & 0xFFFF);
1919
1920	/*
1921	 * pre-compute when the isochronous transfer will be finished:
1922	 */
1923	xfer->isoc_time_complete =
1924	    (usb2_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset +
1925	    xfer->nframes);
1926
1927	/* get the real number of frames */
1928
1929	nframes = xfer->nframes;
1930
1931	buf_offset = 0;
1932
1933	plen = xfer->frlengths;
1934
1935	/* toggle the DMA set we are using */
1936	xfer->flags_int.curr_dma_set ^= 1;
1937
1938	/* get next DMA set */
1939	td = xfer->td_start[xfer->flags_int.curr_dma_set];
1940
1941	xfer->td_transfer_first = td;
1942
1943	ncur = 0;
1944	length = 0;
1945
1946	while (nframes--) {
1947		if (td == NULL) {
1948			panic("%s:%d: out of TD's\n",
1949			    __FUNCTION__, __LINE__);
1950		}
1951		itd_offset[ncur] = length;
1952		buf_offset += *plen;
1953		length += *plen;
1954		plen++;
1955		ncur++;
1956
1957		if (			/* check if the ITD is full */
1958		    (ncur == OHCI_ITD_NOFFSET) ||
1959		/* check if we have put more than 4K into the ITD */
1960		    (length & 0xF000) ||
1961		/* check if it is the last frame */
1962		    (nframes == 0)) {
1963
1964			/* fill current ITD */
1965			td->itd_flags = htole32(
1966			    OHCI_ITD_NOCC |
1967			    OHCI_ITD_SET_SF(xfer->pipe->isoc_next) |
1968			    OHCI_ITD_NOINTR |
1969			    OHCI_ITD_SET_FC(ncur));
1970
1971			td->frames = ncur;
1972			xfer->pipe->isoc_next += ncur;
1973
1974			if (length == 0) {
1975				/* all zero */
1976				td->itd_bp0 = 0;
1977				td->itd_be = ~0;
1978
1979				while (ncur--) {
1980					td->itd_offset[ncur] =
1981					    htole16(OHCI_ITD_MK_OFFS(0));
1982				}
1983			} else {
1984				usb2_get_page(xfer->frbuffers, buf_offset - length, &buf_res);
1985				length = OHCI_PAGE_MASK(buf_res.physaddr);
1986				buf_res.physaddr =
1987				    OHCI_PAGE(buf_res.physaddr);
1988				td->itd_bp0 = htole32(buf_res.physaddr);
1989				usb2_get_page(xfer->frbuffers, buf_offset - 1, &buf_res);
1990				td->itd_be = htole32(buf_res.physaddr);
1991
1992				while (ncur--) {
1993					itd_offset[ncur] += length;
1994					itd_offset[ncur] =
1995					    OHCI_ITD_MK_OFFS(itd_offset[ncur]);
1996					td->itd_offset[ncur] =
1997					    htole16(itd_offset[ncur]);
1998				}
1999			}
2000			ncur = 0;
2001			length = 0;
2002			td_last = td;
2003			td = td->obj_next;
2004
2005			if (td) {
2006				/* link the last TD with the next one */
2007				td_last->itd_next = td->itd_self;
2008			}
2009			usb2_pc_cpu_flush(td_last->page_cache);
2010		}
2011	}
2012
2013	/* update the last TD */
2014	td_last->itd_flags &= ~htole32(OHCI_ITD_NOINTR);
2015	td_last->itd_flags |= htole32(OHCI_ITD_SET_DI(0));
2016	td_last->itd_next = 0;
2017
2018	usb2_pc_cpu_flush(td_last->page_cache);
2019
2020	xfer->td_transfer_last = td_last;
2021
2022#if USB_DEBUG
2023	if (ohcidebug > 8) {
2024		DPRINTF("data before transfer:\n");
2025		ohci_dump_itds(xfer->td_transfer_first);
2026	}
2027#endif
2028	ed = xfer->qh_start[xfer->flags_int.curr_dma_set];
2029
2030	if (UE_GET_DIR(xfer->endpoint) == UE_DIR_IN)
2031		ed_flags = (OHCI_ED_DIR_IN | OHCI_ED_FORMAT_ISO);
2032	else
2033		ed_flags = (OHCI_ED_DIR_OUT | OHCI_ED_FORMAT_ISO);
2034
2035	ed_flags |= (OHCI_ED_SET_FA(xfer->address) |
2036	    OHCI_ED_SET_EN(UE_GET_ADDR(xfer->endpoint)) |
2037	    OHCI_ED_SET_MAXP(xfer->max_frame_size));
2038
2039	if (xfer->xroot->udev->speed == USB_SPEED_LOW) {
2040		ed_flags |= OHCI_ED_SPEED;
2041	}
2042	ed->ed_flags = htole32(ed_flags);
2043
2044	td = xfer->td_transfer_first;
2045
2046	ed->ed_headp = td->itd_self;
2047
2048	/* isochronous transfers are not affected by suspend / resume */
2049	/* the append function will flush the endpoint descriptor */
2050
2051	OHCI_APPEND_QH(ed, sc->sc_isoc_p_last);
2052}
2053
2054static void
2055ohci_device_isoc_start(struct usb2_xfer *xfer)
2056{
2057	/* put transfer on interrupt queue */
2058	ohci_transfer_intr_enqueue(xfer);
2059}
2060
2061struct usb2_pipe_methods ohci_device_isoc_methods =
2062{
2063	.open = ohci_device_isoc_open,
2064	.close = ohci_device_isoc_close,
2065	.enter = ohci_device_isoc_enter,
2066	.start = ohci_device_isoc_start,
2067	.enter_is_cancelable = 1,
2068	.start_is_cancelable = 1,
2069};
2070
2071/*------------------------------------------------------------------------*
2072 * ohci root control support
2073 *------------------------------------------------------------------------*
2074 * simulate a hardware hub by handling
2075 * all the necessary requests
2076 *------------------------------------------------------------------------*/
2077
2078static void
2079ohci_root_ctrl_open(struct usb2_xfer *xfer)
2080{
2081	return;
2082}
2083
2084static void
2085ohci_root_ctrl_close(struct usb2_xfer *xfer)
2086{
2087	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
2088
2089	if (sc->sc_root_ctrl.xfer == xfer) {
2090		sc->sc_root_ctrl.xfer = NULL;
2091	}
2092	ohci_device_done(xfer, USB_ERR_CANCELLED);
2093}
2094
2095/* data structures and routines
2096 * to emulate the root hub:
2097 */
2098static const
2099struct usb2_device_descriptor ohci_devd =
2100{
2101	sizeof(struct usb2_device_descriptor),
2102	UDESC_DEVICE,			/* type */
2103	{0x00, 0x01},			/* USB version */
2104	UDCLASS_HUB,			/* class */
2105	UDSUBCLASS_HUB,			/* subclass */
2106	UDPROTO_FSHUB,			/* protocol */
2107	64,				/* max packet */
2108	{0}, {0}, {0x00, 0x01},		/* device id */
2109	1, 2, 0,			/* string indicies */
2110	1				/* # of configurations */
2111};
2112
2113static const
2114struct ohci_config_desc ohci_confd =
2115{
2116	.confd = {
2117		.bLength = sizeof(struct usb2_config_descriptor),
2118		.bDescriptorType = UDESC_CONFIG,
2119		.wTotalLength[0] = sizeof(ohci_confd),
2120		.bNumInterface = 1,
2121		.bConfigurationValue = 1,
2122		.iConfiguration = 0,
2123		.bmAttributes = UC_SELF_POWERED,
2124		.bMaxPower = 0,		/* max power */
2125	},
2126
2127	.ifcd = {
2128		.bLength = sizeof(struct usb2_interface_descriptor),
2129		.bDescriptorType = UDESC_INTERFACE,
2130		.bNumEndpoints = 1,
2131		.bInterfaceClass = UICLASS_HUB,
2132		.bInterfaceSubClass = UISUBCLASS_HUB,
2133		.bInterfaceProtocol = UIPROTO_FSHUB,
2134	},
2135
2136	.endpd = {
2137		.bLength = sizeof(struct usb2_endpoint_descriptor),
2138		.bDescriptorType = UDESC_ENDPOINT,
2139		.bEndpointAddress = UE_DIR_IN | OHCI_INTR_ENDPT,
2140		.bmAttributes = UE_INTERRUPT,
2141		.wMaxPacketSize[0] = 32,/* max packet (255 ports) */
2142		.bInterval = 255,
2143	},
2144};
2145
2146static const
2147struct usb2_hub_descriptor ohci_hubd =
2148{
2149	0,				/* dynamic length */
2150	UDESC_HUB,
2151	0,
2152	{0, 0},
2153	0,
2154	0,
2155	{0},
2156};
2157
2158static void
2159ohci_root_ctrl_enter(struct usb2_xfer *xfer)
2160{
2161	return;
2162}
2163
2164static void
2165ohci_root_ctrl_start(struct usb2_xfer *xfer)
2166{
2167	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
2168
2169	sc->sc_root_ctrl.xfer = xfer;
2170
2171	usb2_bus_roothub_exec(xfer->xroot->bus);
2172}
2173
2174static void
2175ohci_root_ctrl_task(struct usb2_bus *bus)
2176{
2177	ohci_root_ctrl_poll(OHCI_BUS2SC(bus));
2178}
2179
2180static void
2181ohci_root_ctrl_done(struct usb2_xfer *xfer,
2182    struct usb2_sw_transfer *std)
2183{
2184	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
2185	char *ptr;
2186	uint32_t port;
2187	uint32_t v;
2188	uint16_t value;
2189	uint16_t index;
2190	uint8_t l;
2191
2192	USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2193
2194	if (std->state != USB_SW_TR_SETUP) {
2195		if (std->state == USB_SW_TR_PRE_CALLBACK) {
2196			/* transfer transferred */
2197			ohci_device_done(xfer, std->err);
2198		}
2199		goto done;
2200	}
2201	/* buffer reset */
2202	std->ptr = sc->sc_hub_desc.temp;
2203	std->len = 0;
2204
2205	value = UGETW(std->req.wValue);
2206	index = UGETW(std->req.wIndex);
2207
2208	DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
2209	    "wValue=0x%04x wIndex=0x%04x\n",
2210	    std->req.bmRequestType, std->req.bRequest,
2211	    UGETW(std->req.wLength), value, index);
2212
2213#define	C(x,y) ((x) | ((y) << 8))
2214	switch (C(std->req.bRequest, std->req.bmRequestType)) {
2215	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2216	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2217	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2218		/*
2219		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2220		 * for the integrated root hub.
2221		 */
2222		break;
2223	case C(UR_GET_CONFIG, UT_READ_DEVICE):
2224		std->len = 1;
2225		sc->sc_hub_desc.temp[0] = sc->sc_conf;
2226		break;
2227	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2228		switch (value >> 8) {
2229		case UDESC_DEVICE:
2230			if ((value & 0xff) != 0) {
2231				std->err = USB_ERR_IOERROR;
2232				goto done;
2233			}
2234			std->len = sizeof(ohci_devd);
2235			sc->sc_hub_desc.devd = ohci_devd;
2236			break;
2237
2238		case UDESC_CONFIG:
2239			if ((value & 0xff) != 0) {
2240				std->err = USB_ERR_IOERROR;
2241				goto done;
2242			}
2243			std->len = sizeof(ohci_confd);
2244			std->ptr = USB_ADD_BYTES(&ohci_confd, 0);
2245			break;
2246
2247		case UDESC_STRING:
2248			switch (value & 0xff) {
2249			case 0:	/* Language table */
2250				ptr = "\001";
2251				break;
2252
2253			case 1:	/* Vendor */
2254				ptr = sc->sc_vendor;
2255				break;
2256
2257			case 2:	/* Product */
2258				ptr = "OHCI root HUB";
2259				break;
2260
2261			default:
2262				ptr = "";
2263				break;
2264			}
2265
2266			std->len = usb2_make_str_desc
2267			    (sc->sc_hub_desc.temp,
2268			    sizeof(sc->sc_hub_desc.temp),
2269			    ptr);
2270			break;
2271
2272		default:
2273			std->err = USB_ERR_IOERROR;
2274			goto done;
2275		}
2276		break;
2277	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2278		std->len = 1;
2279		sc->sc_hub_desc.temp[0] = 0;
2280		break;
2281	case C(UR_GET_STATUS, UT_READ_DEVICE):
2282		std->len = 2;
2283		USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
2284		break;
2285	case C(UR_GET_STATUS, UT_READ_INTERFACE):
2286	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2287		std->len = 2;
2288		USETW(sc->sc_hub_desc.stat.wStatus, 0);
2289		break;
2290	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2291		if (value >= OHCI_MAX_DEVICES) {
2292			std->err = USB_ERR_IOERROR;
2293			goto done;
2294		}
2295		sc->sc_addr = value;
2296		break;
2297	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2298		if ((value != 0) && (value != 1)) {
2299			std->err = USB_ERR_IOERROR;
2300			goto done;
2301		}
2302		sc->sc_conf = value;
2303		break;
2304	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2305		break;
2306	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2307	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2308	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2309		std->err = USB_ERR_IOERROR;
2310		goto done;
2311	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2312		break;
2313	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2314		break;
2315		/* Hub requests */
2316	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2317		break;
2318	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2319		DPRINTFN(9, "UR_CLEAR_PORT_FEATURE "
2320		    "port=%d feature=%d\n",
2321		    index, value);
2322		if ((index < 1) ||
2323		    (index > sc->sc_noport)) {
2324			std->err = USB_ERR_IOERROR;
2325			goto done;
2326		}
2327		port = OHCI_RH_PORT_STATUS(index);
2328		switch (value) {
2329		case UHF_PORT_ENABLE:
2330			OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
2331			break;
2332		case UHF_PORT_SUSPEND:
2333			OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
2334			break;
2335		case UHF_PORT_POWER:
2336			/* Yes, writing to the LOW_SPEED bit clears power. */
2337			OWRITE4(sc, port, UPS_LOW_SPEED);
2338			break;
2339		case UHF_C_PORT_CONNECTION:
2340			OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
2341			break;
2342		case UHF_C_PORT_ENABLE:
2343			OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
2344			break;
2345		case UHF_C_PORT_SUSPEND:
2346			OWRITE4(sc, port, UPS_C_SUSPEND << 16);
2347			break;
2348		case UHF_C_PORT_OVER_CURRENT:
2349			OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
2350			break;
2351		case UHF_C_PORT_RESET:
2352			OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
2353			break;
2354		default:
2355			std->err = USB_ERR_IOERROR;
2356			goto done;
2357		}
2358		switch (value) {
2359		case UHF_C_PORT_CONNECTION:
2360		case UHF_C_PORT_ENABLE:
2361		case UHF_C_PORT_SUSPEND:
2362		case UHF_C_PORT_OVER_CURRENT:
2363		case UHF_C_PORT_RESET:
2364			/* enable RHSC interrupt if condition is cleared. */
2365			if ((OREAD4(sc, port) >> 16) == 0)
2366				ohci_rhsc_enable(sc);
2367			break;
2368		default:
2369			break;
2370		}
2371		break;
2372	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2373		if ((value & 0xff) != 0) {
2374			std->err = USB_ERR_IOERROR;
2375			goto done;
2376		}
2377		v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
2378
2379		sc->sc_hub_desc.hubd = ohci_hubd;
2380		sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
2381		USETW(sc->sc_hub_desc.hubd.wHubCharacteristics,
2382		    (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
2383		    v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
2384		/* XXX overcurrent */
2385		    );
2386		sc->sc_hub_desc.hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
2387		v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
2388
2389		for (l = 0; l < sc->sc_noport; l++) {
2390			if (v & 1) {
2391				sc->sc_hub_desc.hubd.DeviceRemovable[l / 8] |= (1 << (l % 8));
2392			}
2393			v >>= 1;
2394		}
2395		sc->sc_hub_desc.hubd.bDescLength =
2396		    8 + ((sc->sc_noport + 7) / 8);
2397		std->len = sc->sc_hub_desc.hubd.bDescLength;
2398		break;
2399
2400	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2401		std->len = 16;
2402		bzero(sc->sc_hub_desc.temp, 16);
2403		break;
2404	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2405		DPRINTFN(9, "get port status i=%d\n",
2406		    index);
2407		if ((index < 1) ||
2408		    (index > sc->sc_noport)) {
2409			std->err = USB_ERR_IOERROR;
2410			goto done;
2411		}
2412		v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
2413		DPRINTFN(9, "port status=0x%04x\n", v);
2414		USETW(sc->sc_hub_desc.ps.wPortStatus, v);
2415		USETW(sc->sc_hub_desc.ps.wPortChange, v >> 16);
2416		std->len = sizeof(sc->sc_hub_desc.ps);
2417		break;
2418	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2419		std->err = USB_ERR_IOERROR;
2420		goto done;
2421	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2422		break;
2423	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2424		if ((index < 1) ||
2425		    (index > sc->sc_noport)) {
2426			std->err = USB_ERR_IOERROR;
2427			goto done;
2428		}
2429		port = OHCI_RH_PORT_STATUS(index);
2430		switch (value) {
2431		case UHF_PORT_ENABLE:
2432			OWRITE4(sc, port, UPS_PORT_ENABLED);
2433			break;
2434		case UHF_PORT_SUSPEND:
2435			OWRITE4(sc, port, UPS_SUSPEND);
2436			break;
2437		case UHF_PORT_RESET:
2438			DPRINTFN(6, "reset port %d\n", index);
2439			OWRITE4(sc, port, UPS_RESET);
2440			for (v = 0;; v++) {
2441				if (v < 12) {
2442					usb2_pause_mtx(&sc->sc_bus.bus_mtx,
2443					    USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY));
2444
2445					if ((OREAD4(sc, port) & UPS_RESET) == 0) {
2446						break;
2447					}
2448				} else {
2449					std->err = USB_ERR_TIMEOUT;
2450					goto done;
2451				}
2452			}
2453			DPRINTFN(9, "ohci port %d reset, status = 0x%04x\n",
2454			    index, OREAD4(sc, port));
2455			break;
2456		case UHF_PORT_POWER:
2457			DPRINTFN(3, "set port power %d\n", index);
2458			OWRITE4(sc, port, UPS_PORT_POWER);
2459			break;
2460		default:
2461			std->err = USB_ERR_IOERROR;
2462			goto done;
2463		}
2464		break;
2465	default:
2466		std->err = USB_ERR_IOERROR;
2467		goto done;
2468	}
2469done:
2470	return;
2471}
2472
2473static void
2474ohci_root_ctrl_poll(struct ohci_softc *sc)
2475{
2476	usb2_sw_transfer(&sc->sc_root_ctrl,
2477	    &ohci_root_ctrl_done);
2478}
2479
2480struct usb2_pipe_methods ohci_root_ctrl_methods =
2481{
2482	.open = ohci_root_ctrl_open,
2483	.close = ohci_root_ctrl_close,
2484	.enter = ohci_root_ctrl_enter,
2485	.start = ohci_root_ctrl_start,
2486	.enter_is_cancelable = 1,
2487	.start_is_cancelable = 0,
2488};
2489
2490/*------------------------------------------------------------------------*
2491 * ohci root interrupt support
2492 *------------------------------------------------------------------------*/
2493static void
2494ohci_root_intr_open(struct usb2_xfer *xfer)
2495{
2496	return;
2497}
2498
2499static void
2500ohci_root_intr_close(struct usb2_xfer *xfer)
2501{
2502	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
2503
2504	if (sc->sc_root_intr.xfer == xfer) {
2505		sc->sc_root_intr.xfer = NULL;
2506	}
2507	ohci_device_done(xfer, USB_ERR_CANCELLED);
2508}
2509
2510static void
2511ohci_root_intr_enter(struct usb2_xfer *xfer)
2512{
2513	return;
2514}
2515
2516static void
2517ohci_root_intr_start(struct usb2_xfer *xfer)
2518{
2519	ohci_softc_t *sc = OHCI_BUS2SC(xfer->xroot->bus);
2520
2521	sc->sc_root_intr.xfer = xfer;
2522}
2523
2524struct usb2_pipe_methods ohci_root_intr_methods =
2525{
2526	.open = ohci_root_intr_open,
2527	.close = ohci_root_intr_close,
2528	.enter = ohci_root_intr_enter,
2529	.start = ohci_root_intr_start,
2530	.enter_is_cancelable = 1,
2531	.start_is_cancelable = 1,
2532};
2533
2534static void
2535ohci_xfer_setup(struct usb2_setup_params *parm)
2536{
2537	struct usb2_page_search page_info;
2538	struct usb2_page_cache *pc;
2539	ohci_softc_t *sc;
2540	struct usb2_xfer *xfer;
2541	void *last_obj;
2542	uint32_t ntd;
2543	uint32_t nitd;
2544	uint32_t nqh;
2545	uint32_t n;
2546
2547	sc = OHCI_BUS2SC(parm->udev->bus);
2548	xfer = parm->curr_xfer;
2549
2550	parm->hc_max_packet_size = 0x500;
2551	parm->hc_max_packet_count = 1;
2552	parm->hc_max_frame_size = OHCI_PAGE_SIZE;
2553
2554	/*
2555	 * calculate ntd and nqh
2556	 */
2557	if (parm->methods == &ohci_device_ctrl_methods) {
2558		xfer->flags_int.bdma_enable = 1;
2559
2560		usb2_transfer_setup_sub(parm);
2561
2562		nitd = 0;
2563		ntd = ((2 * xfer->nframes) + 1	/* STATUS */
2564		    + (xfer->max_data_length / xfer->max_hc_frame_size));
2565		nqh = 1;
2566
2567	} else if (parm->methods == &ohci_device_bulk_methods) {
2568		xfer->flags_int.bdma_enable = 1;
2569
2570		usb2_transfer_setup_sub(parm);
2571
2572		nitd = 0;
2573		ntd = ((2 * xfer->nframes)
2574		    + (xfer->max_data_length / xfer->max_hc_frame_size));
2575		nqh = 1;
2576
2577	} else if (parm->methods == &ohci_device_intr_methods) {
2578		xfer->flags_int.bdma_enable = 1;
2579
2580		usb2_transfer_setup_sub(parm);
2581
2582		nitd = 0;
2583		ntd = ((2 * xfer->nframes)
2584		    + (xfer->max_data_length / xfer->max_hc_frame_size));
2585		nqh = 1;
2586
2587	} else if (parm->methods == &ohci_device_isoc_methods) {
2588		xfer->flags_int.bdma_enable = 1;
2589
2590		usb2_transfer_setup_sub(parm);
2591
2592		nitd = ((xfer->max_data_length / OHCI_PAGE_SIZE) +
2593		    ((xfer->nframes + OHCI_ITD_NOFFSET - 1) / OHCI_ITD_NOFFSET) +
2594		    1 /* EXTRA */ );
2595		ntd = 0;
2596		nqh = 1;
2597
2598	} else {
2599
2600		usb2_transfer_setup_sub(parm);
2601
2602		nitd = 0;
2603		ntd = 0;
2604		nqh = 0;
2605	}
2606
2607alloc_dma_set:
2608
2609	if (parm->err) {
2610		return;
2611	}
2612	last_obj = NULL;
2613
2614	if (usb2_transfer_setup_sub_malloc(
2615	    parm, &pc, sizeof(ohci_td_t),
2616	    OHCI_TD_ALIGN, ntd)) {
2617		parm->err = USB_ERR_NOMEM;
2618		return;
2619	}
2620	if (parm->buf) {
2621		for (n = 0; n != ntd; n++) {
2622			ohci_td_t *td;
2623
2624			usb2_get_page(pc + n, 0, &page_info);
2625
2626			td = page_info.buffer;
2627
2628			/* init TD */
2629			td->td_self = htole32(page_info.physaddr);
2630			td->obj_next = last_obj;
2631			td->page_cache = pc + n;
2632
2633			last_obj = td;
2634
2635			usb2_pc_cpu_flush(pc + n);
2636		}
2637	}
2638	if (usb2_transfer_setup_sub_malloc(
2639	    parm, &pc, sizeof(ohci_itd_t),
2640	    OHCI_ITD_ALIGN, nitd)) {
2641		parm->err = USB_ERR_NOMEM;
2642		return;
2643	}
2644	if (parm->buf) {
2645		for (n = 0; n != nitd; n++) {
2646			ohci_itd_t *itd;
2647
2648			usb2_get_page(pc + n, 0, &page_info);
2649
2650			itd = page_info.buffer;
2651
2652			/* init TD */
2653			itd->itd_self = htole32(page_info.physaddr);
2654			itd->obj_next = last_obj;
2655			itd->page_cache = pc + n;
2656
2657			last_obj = itd;
2658
2659			usb2_pc_cpu_flush(pc + n);
2660		}
2661	}
2662	xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
2663
2664	last_obj = NULL;
2665
2666	if (usb2_transfer_setup_sub_malloc(
2667	    parm, &pc, sizeof(ohci_ed_t),
2668	    OHCI_ED_ALIGN, nqh)) {
2669		parm->err = USB_ERR_NOMEM;
2670		return;
2671	}
2672	if (parm->buf) {
2673		for (n = 0; n != nqh; n++) {
2674			ohci_ed_t *ed;
2675
2676			usb2_get_page(pc + n, 0, &page_info);
2677
2678			ed = page_info.buffer;
2679
2680			/* init QH */
2681			ed->ed_self = htole32(page_info.physaddr);
2682			ed->obj_next = last_obj;
2683			ed->page_cache = pc + n;
2684
2685			last_obj = ed;
2686
2687			usb2_pc_cpu_flush(pc + n);
2688		}
2689	}
2690	xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj;
2691
2692	if (!xfer->flags_int.curr_dma_set) {
2693		xfer->flags_int.curr_dma_set = 1;
2694		goto alloc_dma_set;
2695	}
2696}
2697
2698static void
2699ohci_pipe_init(struct usb2_device *udev, struct usb2_endpoint_descriptor *edesc,
2700    struct usb2_pipe *pipe)
2701{
2702	ohci_softc_t *sc = OHCI_BUS2SC(udev->bus);
2703
2704	DPRINTFN(2, "pipe=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
2705	    pipe, udev->address,
2706	    edesc->bEndpointAddress, udev->flags.usb2_mode,
2707	    sc->sc_addr);
2708
2709	if (udev->flags.usb2_mode != USB_MODE_HOST) {
2710		/* not supported */
2711		return;
2712	}
2713	if (udev->device_index == sc->sc_addr) {
2714		switch (edesc->bEndpointAddress) {
2715		case USB_CONTROL_ENDPOINT:
2716			pipe->methods = &ohci_root_ctrl_methods;
2717			break;
2718		case UE_DIR_IN | OHCI_INTR_ENDPT:
2719			pipe->methods = &ohci_root_intr_methods;
2720			break;
2721		default:
2722			/* do nothing */
2723			break;
2724		}
2725	} else {
2726		switch (edesc->bmAttributes & UE_XFERTYPE) {
2727		case UE_CONTROL:
2728			pipe->methods = &ohci_device_ctrl_methods;
2729			break;
2730		case UE_INTERRUPT:
2731			pipe->methods = &ohci_device_intr_methods;
2732			break;
2733		case UE_ISOCHRONOUS:
2734			if (udev->speed == USB_SPEED_FULL) {
2735				pipe->methods = &ohci_device_isoc_methods;
2736			}
2737			break;
2738		case UE_BULK:
2739			if (udev->speed != USB_SPEED_LOW) {
2740				pipe->methods = &ohci_device_bulk_methods;
2741			}
2742			break;
2743		default:
2744			/* do nothing */
2745			break;
2746		}
2747	}
2748}
2749
2750static void
2751ohci_xfer_unsetup(struct usb2_xfer *xfer)
2752{
2753	return;
2754}
2755
2756static void
2757ohci_get_dma_delay(struct usb2_bus *bus, uint32_t *pus)
2758{
2759	/*
2760	 * Wait until hardware has finished any possible use of the
2761	 * transfer descriptor(s) and QH
2762	 */
2763	*pus = (1125);			/* microseconds */
2764}
2765
2766static void
2767ohci_device_resume(struct usb2_device *udev)
2768{
2769	struct ohci_softc *sc = OHCI_BUS2SC(udev->bus);
2770	struct usb2_xfer *xfer;
2771	struct usb2_pipe_methods *methods;
2772	ohci_ed_t *ed;
2773
2774	DPRINTF("\n");
2775
2776	USB_BUS_LOCK(udev->bus);
2777
2778	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2779
2780		if (xfer->xroot->udev == udev) {
2781
2782			methods = xfer->pipe->methods;
2783			ed = xfer->qh_start[xfer->flags_int.curr_dma_set];
2784
2785			if (methods == &ohci_device_bulk_methods) {
2786				OHCI_APPEND_QH(ed, sc->sc_bulk_p_last);
2787				OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
2788			}
2789			if (methods == &ohci_device_ctrl_methods) {
2790				OHCI_APPEND_QH(ed, sc->sc_ctrl_p_last);
2791				OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
2792			}
2793			if (methods == &ohci_device_intr_methods) {
2794				OHCI_APPEND_QH(ed, sc->sc_intr_p_last[xfer->qh_pos]);
2795			}
2796		}
2797	}
2798
2799	USB_BUS_UNLOCK(udev->bus);
2800
2801	return;
2802}
2803
2804static void
2805ohci_device_suspend(struct usb2_device *udev)
2806{
2807	struct ohci_softc *sc = OHCI_BUS2SC(udev->bus);
2808	struct usb2_xfer *xfer;
2809	struct usb2_pipe_methods *methods;
2810	ohci_ed_t *ed;
2811
2812	DPRINTF("\n");
2813
2814	USB_BUS_LOCK(udev->bus);
2815
2816	TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2817
2818		if (xfer->xroot->udev == udev) {
2819
2820			methods = xfer->pipe->methods;
2821			ed = xfer->qh_start[xfer->flags_int.curr_dma_set];
2822
2823			if (methods == &ohci_device_bulk_methods) {
2824				OHCI_REMOVE_QH(ed, sc->sc_bulk_p_last);
2825			}
2826			if (methods == &ohci_device_ctrl_methods) {
2827				OHCI_REMOVE_QH(ed, sc->sc_ctrl_p_last);
2828			}
2829			if (methods == &ohci_device_intr_methods) {
2830				OHCI_REMOVE_QH(ed, sc->sc_intr_p_last[xfer->qh_pos]);
2831			}
2832		}
2833	}
2834
2835	USB_BUS_UNLOCK(udev->bus);
2836
2837	return;
2838}
2839
2840static void
2841ohci_set_hw_power(struct usb2_bus *bus)
2842{
2843	struct ohci_softc *sc = OHCI_BUS2SC(bus);
2844	uint32_t temp;
2845	uint32_t flags;
2846
2847	DPRINTF("\n");
2848
2849	USB_BUS_LOCK(bus);
2850
2851	flags = bus->hw_power_state;
2852
2853	temp = OREAD4(sc, OHCI_CONTROL);
2854	temp &= ~(OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE);
2855
2856	if (flags & USB_HW_POWER_CONTROL)
2857		temp |= OHCI_CLE;
2858
2859	if (flags & USB_HW_POWER_BULK)
2860		temp |= OHCI_BLE;
2861
2862	if (flags & USB_HW_POWER_INTERRUPT)
2863		temp |= OHCI_PLE;
2864
2865	if (flags & USB_HW_POWER_ISOC)
2866		temp |= OHCI_IE | OHCI_PLE;
2867
2868	OWRITE4(sc, OHCI_CONTROL, temp);
2869
2870	USB_BUS_UNLOCK(bus);
2871
2872	return;
2873}
2874
2875struct usb2_bus_methods ohci_bus_methods =
2876{
2877	.pipe_init = ohci_pipe_init,
2878	.xfer_setup = ohci_xfer_setup,
2879	.xfer_unsetup = ohci_xfer_unsetup,
2880	.get_dma_delay = ohci_get_dma_delay,
2881	.device_resume = ohci_device_resume,
2882	.device_suspend = ohci_device_suspend,
2883	.set_hw_power = ohci_set_hw_power,
2884	.roothub_exec = ohci_root_ctrl_task,
2885};
2886