xref: /netbsd-src/sys/dev/usb/ohci.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: ohci.c,v 1.91 2000/06/01 14:28:58 augustss Exp $	*/
2 /*	$FreeBSD: src/sys/dev/usb/ohci.c,v 1.22 1999/11/17 22:33:40 n_hibma Exp $	*/
3 
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 /*
42  * USB Open Host Controller driver.
43  *
44  * OHCI spec: ftp://ftp.compaq.com/pub/supportinformation/papers/hcir1_0a.exe
45  * USB spec: http://www.usb.org/developers/data/usb11.pdf
46  */
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
51 #if defined(__NetBSD__) || defined(__OpenBSD__)
52 #include <sys/kernel.h>
53 #include <sys/device.h>
54 #include <sys/select.h>
55 #elif defined(__FreeBSD__)
56 #include <sys/module.h>
57 #include <sys/bus.h>
58 #include <machine/bus_pio.h>
59 #include <machine/bus_memio.h>
60 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
61 #include <machine/cpu.h>
62 #endif
63 #endif
64 #include <sys/proc.h>
65 #include <sys/queue.h>
66 
67 #include <machine/bus.h>
68 #include <machine/endian.h>
69 
70 #include <dev/usb/usb.h>
71 #include <dev/usb/usbdi.h>
72 #include <dev/usb/usbdivar.h>
73 #include <dev/usb/usb_mem.h>
74 #include <dev/usb/usb_quirks.h>
75 
76 #include <dev/usb/ohcireg.h>
77 #include <dev/usb/ohcivar.h>
78 
79 #if defined(__FreeBSD__)
80 #include <machine/clock.h>
81 
82 #define delay(d)                DELAY(d)
83 #endif
84 
85 #if defined(__OpenBSD__)
86 struct cfdriver ohci_cd = {
87 	NULL, "ohci", DV_DULL
88 };
89 #endif
90 
91 #ifdef OHCI_DEBUG
92 #define DPRINTF(x)	if (ohcidebug) logprintf x
93 #define DPRINTFN(n,x)	if (ohcidebug>(n)) logprintf x
94 int ohcidebug = 0;
95 #else
96 #define DPRINTF(x)
97 #define DPRINTFN(n,x)
98 #endif
99 
100 /*
101  * The OHCI controller is little endian, so on big endian machines
102  * the data strored in memory needs to be swapped.
103  */
104 #if defined(__FreeBSD__) || defined(__OpenBSD__)
105 #if BYTE_ORDER == BIG_ENDIAN
106 #define htole32(x) (bswap32(x))
107 #define le32toh(x) (bswap32(x))
108 #else
109 #define htole32(x) (x)
110 #define le32toh(x) (x)
111 #endif
112 #endif
113 
114 struct ohci_pipe;
115 
116 Static ohci_soft_ed_t  *ohci_alloc_sed(ohci_softc_t *);
117 Static void		ohci_free_sed(ohci_softc_t *, ohci_soft_ed_t *);
118 
119 Static ohci_soft_td_t  *ohci_alloc_std(ohci_softc_t *);
120 Static void		ohci_free_std(ohci_softc_t *, ohci_soft_td_t *);
121 
122 Static ohci_soft_itd_t *ohci_alloc_sitd(ohci_softc_t *);
123 Static void		ohci_free_sitd(ohci_softc_t *,ohci_soft_itd_t *);
124 
125 #if 0
126 Static void		ohci_free_std_chain(ohci_softc_t *, ohci_soft_td_t *,
127 					    ohci_soft_td_t *);
128 #endif
129 Static usbd_status	ohci_alloc_std_chain(struct ohci_pipe *,
130 			    ohci_softc_t *, int, int, usbd_xfer_handle,
131 			    ohci_soft_td_t *, ohci_soft_td_t **);
132 
133 Static void		ohci_shutdown(void *v);
134 Static void		ohci_power(int, void *);
135 Static usbd_status	ohci_open(usbd_pipe_handle);
136 Static void		ohci_poll(struct usbd_bus *);
137 Static void		ohci_softintr(struct usbd_bus *);
138 Static void		ohci_waitintr(ohci_softc_t *, usbd_xfer_handle);
139 Static void		ohci_add_done(ohci_softc_t *, ohci_physaddr_t);
140 Static void		ohci_rhsc(ohci_softc_t *, usbd_xfer_handle);
141 
142 Static usbd_status	ohci_device_request(usbd_xfer_handle xfer);
143 Static void		ohci_add_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
144 Static void		ohci_rem_ed(ohci_soft_ed_t *, ohci_soft_ed_t *);
145 Static void		ohci_hash_add_td(ohci_softc_t *, ohci_soft_td_t *);
146 Static void		ohci_hash_rem_td(ohci_softc_t *, ohci_soft_td_t *);
147 Static ohci_soft_td_t  *ohci_hash_find_td(ohci_softc_t *, ohci_physaddr_t);
148 Static void		ohci_hash_add_itd(ohci_softc_t *, ohci_soft_itd_t *);
149 Static void		ohci_hash_rem_itd(ohci_softc_t *, ohci_soft_itd_t *);
150 Static ohci_soft_itd_t  *ohci_hash_find_itd(ohci_softc_t *, ohci_physaddr_t);
151 
152 Static usbd_status	ohci_setup_isoc(usbd_pipe_handle pipe);
153 Static void		ohci_device_isoc_enter(usbd_xfer_handle);
154 
155 Static usbd_status	ohci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
156 Static void		ohci_freem(struct usbd_bus *, usb_dma_t *);
157 
158 Static usbd_xfer_handle	ohci_allocx(struct usbd_bus *);
159 Static void		ohci_freex(struct usbd_bus *, usbd_xfer_handle);
160 
161 Static usbd_status	ohci_root_ctrl_transfer(usbd_xfer_handle);
162 Static usbd_status	ohci_root_ctrl_start(usbd_xfer_handle);
163 Static void		ohci_root_ctrl_abort(usbd_xfer_handle);
164 Static void		ohci_root_ctrl_close(usbd_pipe_handle);
165 Static void		ohci_root_ctrl_done(usbd_xfer_handle);
166 
167 Static usbd_status	ohci_root_intr_transfer(usbd_xfer_handle);
168 Static usbd_status	ohci_root_intr_start(usbd_xfer_handle);
169 Static void		ohci_root_intr_abort(usbd_xfer_handle);
170 Static void		ohci_root_intr_close(usbd_pipe_handle);
171 Static void		ohci_root_intr_done(usbd_xfer_handle);
172 
173 Static usbd_status	ohci_device_ctrl_transfer(usbd_xfer_handle);
174 Static usbd_status	ohci_device_ctrl_start(usbd_xfer_handle);
175 Static void		ohci_device_ctrl_abort(usbd_xfer_handle);
176 Static void		ohci_device_ctrl_close(usbd_pipe_handle);
177 Static void		ohci_device_ctrl_done(usbd_xfer_handle);
178 
179 Static usbd_status	ohci_device_bulk_transfer(usbd_xfer_handle);
180 Static usbd_status	ohci_device_bulk_start(usbd_xfer_handle);
181 Static void		ohci_device_bulk_abort(usbd_xfer_handle);
182 Static void		ohci_device_bulk_close(usbd_pipe_handle);
183 Static void		ohci_device_bulk_done(usbd_xfer_handle);
184 
185 Static usbd_status	ohci_device_intr_transfer(usbd_xfer_handle);
186 Static usbd_status	ohci_device_intr_start(usbd_xfer_handle);
187 Static void		ohci_device_intr_abort(usbd_xfer_handle);
188 Static void		ohci_device_intr_close(usbd_pipe_handle);
189 Static void		ohci_device_intr_done(usbd_xfer_handle);
190 
191 Static usbd_status	ohci_device_isoc_transfer(usbd_xfer_handle);
192 Static usbd_status	ohci_device_isoc_start(usbd_xfer_handle);
193 Static void		ohci_device_isoc_abort(usbd_xfer_handle);
194 Static void		ohci_device_isoc_close(usbd_pipe_handle);
195 Static void		ohci_device_isoc_done(usbd_xfer_handle);
196 
197 Static usbd_status	ohci_device_setintr(ohci_softc_t *sc,
198 			    struct ohci_pipe *pipe, int ival);
199 
200 Static int		ohci_str(usb_string_descriptor_t *, int, char *);
201 
202 Static void		ohci_timeout(void *);
203 Static void		ohci_rhsc_able(ohci_softc_t *, int);
204 
205 Static void		ohci_close_pipe(usbd_pipe_handle, ohci_soft_ed_t *);
206 Static void		ohci_abort_xfer(usbd_xfer_handle, usbd_status);
207 Static void		ohci_abort_xfer_end(void *);
208 
209 Static void		ohci_device_clear_toggle(usbd_pipe_handle pipe);
210 Static void		ohci_noop(usbd_pipe_handle pipe);
211 
212 #ifdef OHCI_DEBUG
213 Static void		ohci_dumpregs(ohci_softc_t *);
214 Static void		ohci_dump_tds(ohci_soft_td_t *);
215 Static void		ohci_dump_td(ohci_soft_td_t *);
216 Static void		ohci_dump_ed(ohci_soft_ed_t *);
217 Static void		ohci_dump_itd(ohci_soft_itd_t *);
218 Static void		ohci_dump_itds(ohci_soft_itd_t *);
219 #endif
220 
221 #define OBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
222 			BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
223 #define OWRITE1(sc, r, x) \
224  do { OBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
225 #define OWRITE2(sc, r, x) \
226  do { OBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
227 #define OWRITE4(sc, r, x) \
228  do { OBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
229 #define OREAD1(sc, r) (OBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
230 #define OREAD2(sc, r) (OBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
231 #define OREAD4(sc, r) (OBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
232 
233 /* Reverse the bits in a value 0 .. 31 */
234 Static u_int8_t revbits[OHCI_NO_INTRS] =
235   { 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c,
236     0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e,
237     0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d,
238     0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f };
239 
240 struct ohci_pipe {
241 	struct usbd_pipe pipe;
242 	ohci_soft_ed_t *sed;
243 	union {
244 		ohci_soft_td_t *td;
245 		ohci_soft_itd_t *itd;
246 	} tail;
247 	/* Info needed for different pipe kinds. */
248 	union {
249 		/* Control pipe */
250 		struct {
251 			usb_dma_t reqdma;
252 			u_int length;
253 			ohci_soft_td_t *setup, *data, *stat;
254 		} ctl;
255 		/* Interrupt pipe */
256 		struct {
257 			int nslots;
258 			int pos;
259 		} intr;
260 		/* Bulk pipe */
261 		struct {
262 			u_int length;
263 			int isread;
264 		} bulk;
265 		/* Iso pipe */
266 		struct iso {
267 			int next, inuse;
268 		} iso;
269 	} u;
270 };
271 
272 #define OHCI_INTR_ENDPT 1
273 
274 Static struct usbd_bus_methods ohci_bus_methods = {
275 	ohci_open,
276 	ohci_softintr,
277 	ohci_poll,
278 	ohci_allocm,
279 	ohci_freem,
280 	ohci_allocx,
281 	ohci_freex,
282 };
283 
284 Static struct usbd_pipe_methods ohci_root_ctrl_methods = {
285 	ohci_root_ctrl_transfer,
286 	ohci_root_ctrl_start,
287 	ohci_root_ctrl_abort,
288 	ohci_root_ctrl_close,
289 	ohci_noop,
290 	ohci_root_ctrl_done,
291 };
292 
293 Static struct usbd_pipe_methods ohci_root_intr_methods = {
294 	ohci_root_intr_transfer,
295 	ohci_root_intr_start,
296 	ohci_root_intr_abort,
297 	ohci_root_intr_close,
298 	ohci_noop,
299 	ohci_root_intr_done,
300 };
301 
302 Static struct usbd_pipe_methods ohci_device_ctrl_methods = {
303 	ohci_device_ctrl_transfer,
304 	ohci_device_ctrl_start,
305 	ohci_device_ctrl_abort,
306 	ohci_device_ctrl_close,
307 	ohci_noop,
308 	ohci_device_ctrl_done,
309 };
310 
311 Static struct usbd_pipe_methods ohci_device_intr_methods = {
312 	ohci_device_intr_transfer,
313 	ohci_device_intr_start,
314 	ohci_device_intr_abort,
315 	ohci_device_intr_close,
316 	ohci_device_clear_toggle,
317 	ohci_device_intr_done,
318 };
319 
320 Static struct usbd_pipe_methods ohci_device_bulk_methods = {
321 	ohci_device_bulk_transfer,
322 	ohci_device_bulk_start,
323 	ohci_device_bulk_abort,
324 	ohci_device_bulk_close,
325 	ohci_device_clear_toggle,
326 	ohci_device_bulk_done,
327 };
328 
329 Static struct usbd_pipe_methods ohci_device_isoc_methods = {
330 	ohci_device_isoc_transfer,
331 	ohci_device_isoc_start,
332 	ohci_device_isoc_abort,
333 	ohci_device_isoc_close,
334 	ohci_noop,
335 	ohci_device_isoc_done,
336 };
337 
338 #if defined(__NetBSD__) || defined(__OpenBSD__)
339 int
340 ohci_activate(device_ptr_t self, enum devact act)
341 {
342 	struct ohci_softc *sc = (struct ohci_softc *)self;
343 	int rv = 0;
344 
345 	switch (act) {
346 	case DVACT_ACTIVATE:
347 		return (EOPNOTSUPP);
348 		break;
349 
350 	case DVACT_DEACTIVATE:
351 		if (sc->sc_child != NULL)
352 			rv = config_deactivate(sc->sc_child);
353 		sc->sc_dying = 1;
354 		break;
355 	}
356 	return (rv);
357 }
358 
359 int
360 ohci_detach(struct ohci_softc *sc, int flags)
361 {
362 	int rv = 0;
363 
364 	if (sc->sc_child != NULL)
365 		rv = config_detach(sc->sc_child, flags);
366 
367 	if (rv != 0)
368 		return (rv);
369 
370 #if defined(__NetBSD__) || defined(__OpenBSD__)
371 	powerhook_disestablish(sc->sc_powerhook);
372 	shutdownhook_disestablish(sc->sc_shutdownhook);
373 #endif
374 
375 	/* free data structures XXX */
376 
377 	return (rv);
378 }
379 #endif
380 
381 ohci_soft_ed_t *
382 ohci_alloc_sed(ohci_softc_t *sc)
383 {
384 	ohci_soft_ed_t *sed;
385 	usbd_status err;
386 	int i, offs;
387 	usb_dma_t dma;
388 
389 	if (sc->sc_freeeds == NULL) {
390 		DPRINTFN(2, ("ohci_alloc_sed: allocating chunk\n"));
391 		err = usb_allocmem(&sc->sc_bus, OHCI_SED_SIZE * OHCI_SED_CHUNK,
392 			  OHCI_ED_ALIGN, &dma);
393 		if (err)
394 			return (0);
395 		for(i = 0; i < OHCI_SED_CHUNK; i++) {
396 			offs = i * OHCI_SED_SIZE;
397 			sed = (ohci_soft_ed_t *)((char *)KERNADDR(&dma) +offs);
398 			sed->physaddr = DMAADDR(&dma) + offs;
399 			sed->next = sc->sc_freeeds;
400 			sc->sc_freeeds = sed;
401 		}
402 	}
403 	sed = sc->sc_freeeds;
404 	sc->sc_freeeds = sed->next;
405 	memset(&sed->ed, 0, sizeof(ohci_ed_t));
406 	sed->next = 0;
407 	return (sed);
408 }
409 
410 void
411 ohci_free_sed(ohci_softc_t *sc, ohci_soft_ed_t *sed)
412 {
413 	sed->next = sc->sc_freeeds;
414 	sc->sc_freeeds = sed;
415 }
416 
417 ohci_soft_td_t *
418 ohci_alloc_std(ohci_softc_t *sc)
419 {
420 	ohci_soft_td_t *std;
421 	usbd_status err;
422 	int i, offs;
423 	usb_dma_t dma;
424 	int s;
425 
426 	if (sc->sc_freetds == NULL) {
427 		DPRINTFN(2, ("ohci_alloc_std: allocating chunk\n"));
428 		err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
429 			  OHCI_TD_ALIGN, &dma);
430 		if (err)
431 			return (NULL);
432 		s = splusb();
433 		for(i = 0; i < OHCI_STD_CHUNK; i++) {
434 			offs = i * OHCI_STD_SIZE;
435 			std = (ohci_soft_td_t *)((char *)KERNADDR(&dma) +offs);
436 			std->physaddr = DMAADDR(&dma) + offs;
437 			std->nexttd = sc->sc_freetds;
438 			sc->sc_freetds = std;
439 		}
440 		splx(s);
441 	}
442 
443 	s = splusb();
444 	std = sc->sc_freetds;
445 	sc->sc_freetds = std->nexttd;
446 	memset(&std->td, 0, sizeof(ohci_td_t));
447 	std->nexttd = NULL;
448 	std->xfer = NULL;
449 	ohci_hash_add_td(sc, std);
450 	splx(s);
451 
452 	return (std);
453 }
454 
455 void
456 ohci_free_std(ohci_softc_t *sc, ohci_soft_td_t *std)
457 {
458 	int s;
459 
460 	s = splusb();
461 	ohci_hash_rem_td(sc, std);
462 	std->nexttd = sc->sc_freetds;
463 	sc->sc_freetds = std;
464 	splx(s);
465 }
466 
467 usbd_status
468 ohci_alloc_std_chain(struct ohci_pipe *opipe, ohci_softc_t *sc,
469 		     int alen, int rd, usbd_xfer_handle xfer,
470 		     ohci_soft_td_t *sp, ohci_soft_td_t **ep)
471 {
472 	ohci_soft_td_t *next, *cur;
473 	ohci_physaddr_t dataphys, dataphysend;
474 	u_int32_t tdflags;
475 	int len, curlen;
476 	usb_dma_t *dma = &xfer->dmabuf;
477 	u_int16_t flags = xfer->flags;
478 
479 	DPRINTFN(alen < 4096,("ohci_alloc_std_chain: start len=%d\n", alen));
480 
481 	len = alen;
482 	cur = sp;
483 	dataphys = DMAADDR(dma);
484 	dataphysend = OHCI_PAGE(dataphys + len - 1);
485 	tdflags = htole32(
486 	    (rd ? OHCI_TD_IN : OHCI_TD_OUT) |
487 	    (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0) |
488 	    OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY | OHCI_TD_NOINTR);
489 
490 	for (;;) {
491 		next = ohci_alloc_std(sc);
492 		if (next == NULL)
493 			goto nomem;
494 
495 		/* The OHCI hardware can handle at most one page crossing. */
496 		if (OHCI_PAGE(dataphys) == dataphysend ||
497 		    OHCI_PAGE(dataphys) + OHCI_PAGE_SIZE == dataphysend) {
498 			/* we can handle it in this TD */
499 			curlen = len;
500 		} else {
501 			/* must use multiple TDs, fill as much as possible. */
502 			curlen = 2 * OHCI_PAGE_SIZE -
503 				 (dataphys & (OHCI_PAGE_SIZE-1));
504 			/* the length must be a multiple of the max size */
505 			curlen -= curlen % UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize);
506 #ifdef DIAGNOSTIC
507 			if (curlen == 0)
508 				panic("ohci_alloc_std: curlen == 0\n");
509 #endif
510 		}
511 		DPRINTFN(4,("ohci_alloc_std_chain: dataphys=0x%08x "
512 			    "dataphysend=0x%08x len=%d curlen=%d\n",
513 			    dataphys, dataphysend,
514 			    len, curlen));
515 		len -= curlen;
516 
517 		cur->td.td_flags = tdflags;
518 		cur->td.td_cbp = htole32(dataphys);
519 		cur->nexttd = next;
520 		cur->td.td_nexttd = htole32(next->physaddr);
521 		cur->td.td_be = htole32(dataphys + curlen - 1);
522 		cur->len = curlen;
523 		cur->flags = OHCI_ADD_LEN;
524 		cur->xfer = xfer;
525 		DPRINTFN(10,("ohci_alloc_std_chain: cbp=0x%08x be=0x%08x\n",
526 			    dataphys, dataphys + curlen - 1));
527 		if (len == 0)
528 			break;
529 		DPRINTFN(10,("ohci_alloc_std_chain: extend chain\n"));
530 		dataphys += curlen;
531 		cur = next;
532 	}
533 	if ((flags & USBD_FORCE_SHORT_XFER) &&
534 	    alen % UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize) == 0) {
535 		/* Force a 0 length transfer at the end. */
536 
537 		cur = next;
538 		next = ohci_alloc_std(sc);
539 		if (next == NULL)
540 			goto nomem;
541 
542 		cur->td.td_flags = tdflags;
543 		cur->td.td_cbp = 0; /* indicate 0 length packet */
544 		cur->nexttd = next;
545 		cur->td.td_nexttd = htole32(next->physaddr);
546 		cur->td.td_be = ~0;
547 		cur->len = 0;
548 		cur->flags = 0;
549 		cur->xfer = xfer;
550 		DPRINTFN(2,("ohci_alloc_std_chain: add 0 xfer\n"));
551 	}
552 	*ep = cur;
553 
554 	return (USBD_NORMAL_COMPLETION);
555 
556  nomem:
557 	/* XXX free chain */
558 	return (USBD_NOMEM);
559 }
560 
561 #if 0
562 Static void
563 ohci_free_std_chain(ohci_softc_t *sc, ohci_soft_td_t *std,
564 		    ohci_soft_td_t *stdend)
565 {
566 	ohci_soft_td_t *p;
567 
568 	for (; std != stdend; std = p) {
569 		p = std->nexttd;
570 		ohci_free_std(sc, std);
571 	}
572 }
573 #endif
574 
575 ohci_soft_itd_t *
576 ohci_alloc_sitd(ohci_softc_t *sc)
577 {
578 	ohci_soft_itd_t *sitd;
579 	usbd_status err;
580 	int i, s, offs;
581 	usb_dma_t dma;
582 
583 	if (sc->sc_freeitds == NULL) {
584 		DPRINTFN(2, ("ohci_alloc_sitd: allocating chunk\n"));
585 		err = usb_allocmem(&sc->sc_bus, OHCI_SITD_SIZE * OHCI_SITD_CHUNK,
586 			  OHCI_ITD_ALIGN, &dma);
587 		if (err)
588 			return (NULL);
589 		for(i = 0; i < OHCI_SITD_CHUNK; i++) {
590 			offs = i * OHCI_SITD_SIZE;
591 			sitd = (ohci_soft_itd_t *)((char*)KERNADDR(&dma)+offs);
592 			sitd->physaddr = DMAADDR(&dma) + offs;
593 			sitd->nextitd = sc->sc_freeitds;
594 			sc->sc_freeitds = sitd;
595 		}
596 	}
597 
598 	s = splusb();
599 	sitd = sc->sc_freeitds;
600 	sc->sc_freeitds = sitd->nextitd;
601 	memset(&sitd->itd, 0, sizeof(ohci_itd_t));
602 	sitd->nextitd = NULL;
603 	sitd->xfer = NULL;
604 	ohci_hash_add_itd(sc, sitd);
605 	splx(s);
606 
607 #ifdef DIAGNOSTIC
608 	sitd->isdone = 0;
609 #endif
610 
611 	return (sitd);
612 }
613 
614 void
615 ohci_free_sitd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
616 {
617 	int s;
618 
619 	DPRINTFN(10,("ohci_free_sitd: sitd=%p\n", sitd));
620 
621 #ifdef DIAGNOSTIC
622 	if (!sitd->isdone) {
623 		panic("ohci_free_sitd: sitd=%p not done\n", sitd);
624 		return;
625 	}
626 #endif
627 
628 	s = splusb();
629 	ohci_hash_rem_itd(sc, sitd);
630 	sitd->nextitd = sc->sc_freeitds;
631 	sc->sc_freeitds = sitd;
632 	splx(s);
633 }
634 
635 usbd_status
636 ohci_init(ohci_softc_t *sc)
637 {
638 	ohci_soft_ed_t *sed, *psed;
639 	usbd_status err;
640 	int i;
641 	u_int32_t s, ctl, ival, hcr, fm, per, rev, desca;
642 
643 	DPRINTF(("ohci_init: start\n"));
644 #if defined(__OpenBSD__)
645 	printf(",");
646 #else
647 	printf("%s:", USBDEVNAME(sc->sc_bus.bdev));
648 #endif
649 	rev = OREAD4(sc, OHCI_REVISION);
650 	printf(" OHCI version %d.%d%s\n", OHCI_REV_HI(rev), OHCI_REV_LO(rev),
651 	       OHCI_REV_LEGACY(rev) ? ", legacy support" : "");
652 
653 	if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
654 		printf("%s: unsupported OHCI revision\n",
655 		       USBDEVNAME(sc->sc_bus.bdev));
656 		sc->sc_bus.usbrev = USBREV_UNKNOWN;
657 		return (USBD_INVAL);
658 	}
659 	sc->sc_bus.usbrev = USBREV_1_0;
660 
661 	for (i = 0; i < OHCI_HASH_SIZE; i++)
662 		LIST_INIT(&sc->sc_hash_tds[i]);
663 	for (i = 0; i < OHCI_HASH_SIZE; i++)
664 		LIST_INIT(&sc->sc_hash_itds[i]);
665 
666 	SIMPLEQ_INIT(&sc->sc_free_xfers);
667 
668 	/* XXX determine alignment by R/W */
669 	/* Allocate the HCCA area. */
670 	err = usb_allocmem(&sc->sc_bus, OHCI_HCCA_SIZE,
671 			 OHCI_HCCA_ALIGN, &sc->sc_hccadma);
672 	if (err)
673 		return (err);
674 	sc->sc_hcca = (struct ohci_hcca *)KERNADDR(&sc->sc_hccadma);
675 	memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);
676 
677 	sc->sc_eintrs = OHCI_NORMAL_INTRS;
678 
679 	/* Allocate dummy ED that starts the control list. */
680 	sc->sc_ctrl_head = ohci_alloc_sed(sc);
681 	if (sc->sc_ctrl_head == NULL) {
682 		err = USBD_NOMEM;
683 		goto bad1;
684 	}
685 	sc->sc_ctrl_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
686 
687 	/* Allocate dummy ED that starts the bulk list. */
688 	sc->sc_bulk_head = ohci_alloc_sed(sc);
689 	if (sc->sc_bulk_head == NULL) {
690 		err = USBD_NOMEM;
691 		goto bad2;
692 	}
693 	sc->sc_bulk_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
694 
695 	/* Allocate dummy ED that starts the isochronous list. */
696 	sc->sc_isoc_head = ohci_alloc_sed(sc);
697 	if (sc->sc_isoc_head == NULL) {
698 		err = USBD_NOMEM;
699 		goto bad3;
700 	}
701 	sc->sc_isoc_head->ed.ed_flags |= htole32(OHCI_ED_SKIP);
702 
703 	/* Allocate all the dummy EDs that make up the interrupt tree. */
704 	for (i = 0; i < OHCI_NO_EDS; i++) {
705 		sed = ohci_alloc_sed(sc);
706 		if (sed == NULL) {
707 			while (--i >= 0)
708 				ohci_free_sed(sc, sc->sc_eds[i]);
709 			err = USBD_NOMEM;
710 			goto bad4;
711 		}
712 		/* All ED fields are set to 0. */
713 		sc->sc_eds[i] = sed;
714 		sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
715 		if (i != 0)
716 			psed = sc->sc_eds[(i-1) / 2];
717 		else
718 			psed= sc->sc_isoc_head;
719 		sed->next = psed;
720 		sed->ed.ed_nexted = htole32(psed->physaddr);
721 	}
722 	/*
723 	 * Fill HCCA interrupt table.  The bit reversal is to get
724 	 * the tree set up properly to spread the interrupts.
725 	 */
726 	for (i = 0; i < OHCI_NO_INTRS; i++)
727 		sc->sc_hcca->hcca_interrupt_table[revbits[i]] =
728 		    htole32(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr);
729 
730 #ifdef OHCI_DEBUG
731 	if (ohcidebug > 15) {
732 		for (i = 0; i < OHCI_NO_EDS; i++) {
733 			printf("ed#%d ", i);
734 			ohci_dump_ed(sc->sc_eds[i]);
735 		}
736 		printf("iso ");
737 		ohci_dump_ed(sc->sc_isoc_head);
738 	}
739 #endif
740 
741 	/* Determine in what context we are running. */
742 	ctl = OREAD4(sc, OHCI_CONTROL);
743 	if (ctl & OHCI_IR) {
744 		/* SMM active, request change */
745 		DPRINTF(("ohci_init: SMM active, request owner change\n"));
746 		s = OREAD4(sc, OHCI_COMMAND_STATUS);
747 		OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR);
748 		for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
749 			usb_delay_ms(&sc->sc_bus, 1);
750 			ctl = OREAD4(sc, OHCI_CONTROL);
751 		}
752 		if ((ctl & OHCI_IR) == 0) {
753 			printf("%s: SMM does not respond, resetting\n",
754 			       USBDEVNAME(sc->sc_bus.bdev));
755 			OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
756 			goto reset;
757 		}
758 	} else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) {
759 		/* BIOS started controller. */
760 		DPRINTF(("ohci_init: BIOS active\n"));
761 		if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) {
762 			OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL);
763 			usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
764 		}
765 	} else {
766 		DPRINTF(("ohci_init: cold started\n"));
767 	reset:
768 		/* Controller was cold started. */
769 		usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
770 	}
771 
772 	/*
773 	 * This reset should not be necessary according to the OHCI spec, but
774 	 * without it some controllers do not start.
775 	 */
776 	DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
777 	OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
778 	usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
779 
780 	/* We now own the host controller and the bus has been reset. */
781 	ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
782 
783 	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
784 	/* Nominal time for a reset is 10 us. */
785 	for (i = 0; i < 10; i++) {
786 		delay(10);
787 		hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
788 		if (!hcr)
789 			break;
790 	}
791 	if (hcr) {
792 		printf("%s: reset timeout\n", USBDEVNAME(sc->sc_bus.bdev));
793 		err = USBD_IOERROR;
794 		goto bad5;
795 	}
796 #ifdef OHCI_DEBUG
797 	if (ohcidebug > 15)
798 		ohci_dumpregs(sc);
799 #endif
800 
801 	/* The controller is now in SUSPEND state, we have 2ms to finish. */
802 
803 	/* Set up HC registers. */
804 	OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma));
805 	OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
806 	OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
807 	/* disable all interrupts and then switch on all desired interrupts */
808 	OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
809 	OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);
810 	/* switch on desired functional features */
811 	ctl = OREAD4(sc, OHCI_CONTROL);
812 	ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
813 	ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
814 		OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
815 	/* And finally start it! */
816 	OWRITE4(sc, OHCI_CONTROL, ctl);
817 
818 	/*
819 	 * The controller is now OPERATIONAL.  Set a some final
820 	 * registers that should be set earlier, but that the
821 	 * controller ignores when in the SUSPEND state.
822 	 */
823 	fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
824 	fm |= OHCI_FSMPS(ival) | ival;
825 	OWRITE4(sc, OHCI_FM_INTERVAL, fm);
826 	per = OHCI_PERIODIC(ival); /* 90% periodic */
827 	OWRITE4(sc, OHCI_PERIODIC_START, per);
828 
829 	/* Fiddle the No OverCurrent Protection bit to avoid chip bug. */
830 	desca = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
831 	OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca | OHCI_NOCP);
832 	OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */
833 	usb_delay_ms(&sc->sc_bus, OHCI_ENABLE_POWER_DELAY);
834 	OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca);
835 
836 	/*
837 	 * The AMD756 requires a delay before re-reading the register,
838 	 * otherwise it will occasionally report 0 ports.
839 	 */
840 	usb_delay_ms(&sc->sc_bus, OHCI_READ_DESC_DELAY);
841 	sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
842 
843 #ifdef OHCI_DEBUG
844 	if (ohcidebug > 5)
845 		ohci_dumpregs(sc);
846 #endif
847 
848 	/* Set up the bus struct. */
849 	sc->sc_bus.methods = &ohci_bus_methods;
850 	sc->sc_bus.pipe_size = sizeof(struct ohci_pipe);
851 
852 #if defined(__NetBSD__) || defined(__OpenBSD__)
853 	sc->sc_powerhook = powerhook_establish(ohci_power, sc);
854 	sc->sc_shutdownhook = shutdownhook_establish(ohci_shutdown, sc);
855 #endif
856 
857 	return (USBD_NORMAL_COMPLETION);
858 
859  bad5:
860 	for (i = 0; i < OHCI_NO_EDS; i++)
861 		ohci_free_sed(sc, sc->sc_eds[i]);
862  bad4:
863 	ohci_free_sed(sc, sc->sc_isoc_head);
864  bad3:
865 	ohci_free_sed(sc, sc->sc_ctrl_head);
866  bad2:
867 	ohci_free_sed(sc, sc->sc_bulk_head);
868  bad1:
869 	usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
870 	return (err);
871 }
872 
873 usbd_status
874 ohci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
875 {
876 #if defined(__NetBSD__) || defined(__OpenBSD__)
877 	struct ohci_softc *sc = (struct ohci_softc *)bus;
878 #endif
879 
880 	return (usb_allocmem(&sc->sc_bus, size, 0, dma));
881 }
882 
883 void
884 ohci_freem(struct usbd_bus *bus, usb_dma_t *dma)
885 {
886 #if defined(__NetBSD__) || defined(__OpenBSD__)
887 	struct ohci_softc *sc = (struct ohci_softc *)bus;
888 #endif
889 
890 	usb_freemem(&sc->sc_bus, dma);
891 }
892 
893 usbd_xfer_handle
894 ohci_allocx(struct usbd_bus *bus)
895 {
896 	struct ohci_softc *sc = (struct ohci_softc *)bus;
897 	usbd_xfer_handle xfer;
898 
899 	xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
900 	if (xfer != NULL)
901 		SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, xfer, next);
902 	else
903 		xfer = malloc(sizeof(*xfer), M_USB, M_NOWAIT);
904 	if (xfer != NULL)
905 		memset(xfer, 0, sizeof *xfer);
906 	return (xfer);
907 }
908 
909 void
910 ohci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
911 {
912 	struct ohci_softc *sc = (struct ohci_softc *)bus;
913 
914 	SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
915 }
916 
917 /*
918  * Shut down the controller when the system is going down.
919  */
920 void
921 ohci_shutdown(void *v)
922 {
923 	ohci_softc_t *sc = v;
924 
925 	DPRINTF(("ohci_shutdown: stopping the HC\n"));
926 	OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
927 }
928 
929 /*
930  * Handle suspend/resume.
931  *
932  * We need to switch to polling mode here, because this routine is
933  * called from an intterupt context.  This is all right since we
934  * are almost suspended anyway.
935  */
936 void
937 ohci_power(int why, void *v)
938 {
939 #ifdef OHCI_DEBUG
940 	ohci_softc_t *sc = v;
941 
942 	DPRINTF(("ohci_power: sc=%p, why=%d\n", sc, why));
943 	/* XXX should suspend/resume */
944 	ohci_dumpregs(sc);
945 #endif
946 }
947 
948 #ifdef OHCI_DEBUG
949 void
950 ohci_dumpregs(ohci_softc_t *sc)
951 {
952 	DPRINTF(("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
953 		 OREAD4(sc, OHCI_REVISION),
954 		 OREAD4(sc, OHCI_CONTROL),
955 		 OREAD4(sc, OHCI_COMMAND_STATUS)));
956 	DPRINTF(("               intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
957 		 OREAD4(sc, OHCI_INTERRUPT_STATUS),
958 		 OREAD4(sc, OHCI_INTERRUPT_ENABLE),
959 		 OREAD4(sc, OHCI_INTERRUPT_DISABLE)));
960 	DPRINTF(("               hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
961 		 OREAD4(sc, OHCI_HCCA),
962 		 OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
963 		 OREAD4(sc, OHCI_CONTROL_HEAD_ED)));
964 	DPRINTF(("               ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
965 		 OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
966 		 OREAD4(sc, OHCI_BULK_HEAD_ED),
967 		 OREAD4(sc, OHCI_BULK_CURRENT_ED)));
968 	DPRINTF(("               done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
969 		 OREAD4(sc, OHCI_DONE_HEAD),
970 		 OREAD4(sc, OHCI_FM_INTERVAL),
971 		 OREAD4(sc, OHCI_FM_REMAINING)));
972 	DPRINTF(("               fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
973 		 OREAD4(sc, OHCI_FM_NUMBER),
974 		 OREAD4(sc, OHCI_PERIODIC_START),
975 		 OREAD4(sc, OHCI_LS_THRESHOLD)));
976 	DPRINTF(("               desca=0x%08x descb=0x%08x stat=0x%08x\n",
977 		 OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
978 		 OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
979 		 OREAD4(sc, OHCI_RH_STATUS)));
980 	DPRINTF(("               port1=0x%08x port2=0x%08x\n",
981 		 OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
982 		 OREAD4(sc, OHCI_RH_PORT_STATUS(2))));
983 	DPRINTF(("         HCCA: frame_number=0x%04x done_head=0x%08x\n",
984 		 le32toh(sc->sc_hcca->hcca_frame_number),
985 		 le32toh(sc->sc_hcca->hcca_done_head)));
986 }
987 #endif
988 
989 Static int ohci_intr1(ohci_softc_t *);
990 
991 int
992 ohci_intr(void *p)
993 {
994 	ohci_softc_t *sc = p;
995 
996 	/* If we get an interrupt while polling, then just ignore it. */
997 	if (sc->sc_bus.use_polling) {
998 #ifdef DIAGNOSTIC
999 		printf("ohci_intr: ignored interrupt while polling\n");
1000 #endif
1001 		return (0);
1002 	}
1003 
1004 	return (ohci_intr1(sc));
1005 }
1006 
1007 Static int
1008 ohci_intr1(ohci_softc_t *sc)
1009 {
1010 	u_int32_t intrs, eintrs;
1011 	ohci_physaddr_t done;
1012 
1013 	/* In case the interrupt occurs before initialization has completed. */
1014 	if (sc == NULL || sc->sc_hcca == NULL) {
1015 #ifdef DIAGNOSTIC
1016 		printf("ohci_intr: sc->sc_hcca == NULL\n");
1017 #endif
1018 		return (0);
1019 	}
1020 
1021         intrs = 0;
1022 	done = le32toh(sc->sc_hcca->hcca_done_head);
1023 	if (done != 0) {
1024 		if (done & ~OHCI_DONE_INTRS)
1025 			intrs = OHCI_WDH;
1026 		if (done & OHCI_DONE_INTRS)
1027 			intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
1028 	} else
1029 		intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS);
1030 
1031 	if (!intrs)
1032 		return (0);
1033 
1034 	intrs &= ~OHCI_MIE;
1035 	OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs); /* Acknowledge */
1036 	eintrs = intrs & sc->sc_eintrs;
1037 	if (!eintrs)
1038 		return (0);
1039 
1040 	sc->sc_bus.intr_context++;
1041 	sc->sc_bus.no_intrs++;
1042 	DPRINTFN(7, ("ohci_intr: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n",
1043 		     sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS),
1044 		     (u_int)eintrs));
1045 
1046 	if (eintrs & OHCI_SO) {
1047 		printf("%s: scheduling overrun\n",USBDEVNAME(sc->sc_bus.bdev));
1048 		/* XXX do what */
1049 		intrs &= ~OHCI_SO;
1050 	}
1051 	if (eintrs & OHCI_WDH) {
1052 		ohci_add_done(sc, done &~ OHCI_DONE_INTRS);
1053 		sc->sc_hcca->hcca_done_head = 0;
1054 		usb_schedsoftintr(&sc->sc_bus);
1055 		intrs &= ~OHCI_WDH;
1056 	}
1057 	if (eintrs & OHCI_RD) {
1058 		printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1059 		/* XXX process resume detect */
1060 	}
1061 	if (eintrs & OHCI_UE) {
1062 		printf("%s: unrecoverable error, controller halted\n",
1063 		       USBDEVNAME(sc->sc_bus.bdev));
1064 		OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1065 		/* XXX what else */
1066 	}
1067 	if (eintrs & OHCI_RHSC) {
1068 		ohci_rhsc(sc, sc->sc_intrxfer);
1069 		intrs &= ~OHCI_RHSC;
1070 
1071 		/*
1072 		 * Disable RHSC interrupt for now, because it will be
1073 		 * on until the port has been reset.
1074 		 */
1075 		ohci_rhsc_able(sc, 0);
1076 	}
1077 
1078 	sc->sc_bus.intr_context--;
1079 
1080 	/* Block unprocessed interrupts. XXX */
1081 	OWRITE4(sc, OHCI_INTERRUPT_DISABLE, intrs);
1082 	sc->sc_eintrs &= ~intrs;
1083 
1084 	return (1);
1085 }
1086 
1087 void
1088 ohci_rhsc_able(ohci_softc_t *sc, int on)
1089 {
1090 	DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on));
1091 	if (on) {
1092 		sc->sc_eintrs |= OHCI_RHSC;
1093 		OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
1094 	} else {
1095 		sc->sc_eintrs &= ~OHCI_RHSC;
1096 		OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
1097 	}
1098 }
1099 
1100 #ifdef OHCI_DEBUG
1101 char *ohci_cc_strs[] = {
1102 	"NO_ERROR",
1103 	"CRC",
1104 	"BIT_STUFFING",
1105 	"DATA_TOGGLE_MISMATCH",
1106 	"STALL",
1107 	"DEVICE_NOT_RESPONDING",
1108 	"PID_CHECK_FAILURE",
1109 	"UNEXPECTED_PID",
1110 	"DATA_OVERRUN",
1111 	"DATA_UNDERRUN",
1112 	"BUFFER_OVERRUN",
1113 	"BUFFER_UNDERRUN",
1114 	"reserved",
1115 	"reserved",
1116 	"NOT_ACCESSED",
1117 	"NOT_ACCESSED",
1118 };
1119 #endif
1120 
1121 void
1122 ohci_add_done(ohci_softc_t *sc, ohci_physaddr_t done)
1123 {
1124 	ohci_soft_itd_t *sitd, *sidone, **ip;
1125 	ohci_soft_td_t  *std,  *sdone,  **p;
1126 
1127 	/* Reverse the done list. */
1128 	for (sdone = NULL, sidone = NULL; done != 0; ) {
1129 		std = ohci_hash_find_td(sc, done);
1130 		if (std != NULL) {
1131 			std->dnext = sdone;
1132 			done = le32toh(std->td.td_nexttd);
1133 			sdone = std;
1134 			DPRINTFN(10,("add TD %p\n", std));
1135 			continue;
1136 		}
1137 		sitd = ohci_hash_find_itd(sc, done);
1138 		if (sitd != NULL) {
1139 			sitd->dnext = sidone;
1140 			done = le32toh(sitd->itd.itd_nextitd);
1141 			sidone = sitd;
1142 			DPRINTFN(5,("add ITD %p\n", sitd));
1143 			continue;
1144 		}
1145 		panic("ohci_add_done: addr 0x%08lx not found\n", (u_long)done);
1146 	}
1147 
1148 	/* sdone & sidone now hold the done lists. */
1149 	/* Put them on the already processed lists. */
1150 	for (p = &sc->sc_sdone; *p != NULL; p = &(*p)->dnext)
1151 		;
1152 	*p = sdone;
1153 	for (ip = &sc->sc_sidone; *ip != NULL; ip = &(*ip)->dnext)
1154 		;
1155 	*ip = sidone;
1156 }
1157 
1158 void
1159 ohci_softintr(struct usbd_bus *bus)
1160 {
1161 	ohci_softc_t *sc = (ohci_softc_t *)bus;
1162 	ohci_soft_itd_t *sitd, *sidone, *sitdnext;
1163 	ohci_soft_td_t  *std,  *sdone,  *stdnext;
1164 	usbd_xfer_handle xfer;
1165 	int len, cc, s;
1166 
1167 	sc->sc_bus.intr_context++;
1168 
1169 	s = splhardusb();
1170 	sdone = sc->sc_sdone;
1171 	sc->sc_sdone = NULL;
1172 	sidone = sc->sc_sidone;
1173 	sc->sc_sidone = NULL;
1174 	splx(s);
1175 
1176 	DPRINTFN(10,("ohci_process_done: sdone=%p sidone=%p\n", sdone, sidone));
1177 
1178 #ifdef OHCI_DEBUG
1179 	if (ohcidebug > 10) {
1180 		DPRINTF(("ohci_process_done: TD done:\n"));
1181 		ohci_dump_tds(sdone);
1182 	}
1183 #endif
1184 
1185 	for (std = sdone; std; std = stdnext) {
1186 		xfer = std->xfer;
1187 		stdnext = std->dnext;
1188 		DPRINTFN(10, ("ohci_process_done: std=%p xfer=%p hcpriv=%p\n",
1189 				std, xfer, xfer ? xfer->hcpriv : 0));
1190 		if (xfer == NULL) {
1191 			/* xfer == NULL: There seems to be no xfer associated
1192 			 * with this TD. It is tailp that happened to end up on
1193 			 * the done queue.
1194 			 */
1195 			continue;
1196 		}
1197 		if (xfer->status == USBD_CANCELLED ||
1198 		    xfer->status == USBD_TIMEOUT) {
1199 			DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1200 				 xfer));
1201 			/* Handled by abort routine. */
1202 			continue;
1203 		}
1204 		usb_uncallout(xfer->timeout_handle, ohci_timeout, xfer);
1205 		cc = OHCI_TD_GET_CC(le32toh(std->td.td_flags));
1206 		if (cc == OHCI_CC_NO_ERROR) {
1207 			len = std->len;
1208 			if (std->td.td_cbp != 0)
1209 				len -= le32toh(std->td.td_be) -
1210 				       le32toh(std->td.td_cbp) + 1;
1211 			DPRINTFN(10, ("ohci_process_done: len=%d, flags=0x%x\n",
1212 				      len, std->flags));
1213 			if (std->flags & OHCI_ADD_LEN)
1214 				xfer->actlen += len;
1215 			if (std->flags & OHCI_CALL_DONE) {
1216 				xfer->status = USBD_NORMAL_COMPLETION;
1217 				usb_transfer_complete(xfer);
1218 			}
1219 			ohci_free_std(sc, std);
1220 		} else {
1221 			/*
1222 			 * Endpoint is halted.  First unlink all the TDs
1223 			 * belonging to the failed transfer, and then restart
1224 			 * the endpoint.
1225 			 */
1226 			ohci_soft_td_t *p, *n;
1227 			struct ohci_pipe *opipe =
1228 				(struct ohci_pipe *)xfer->pipe;
1229 
1230 			DPRINTFN(15,("ohci_process_done: error cc=%d (%s)\n",
1231 			  OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
1232 			  ohci_cc_strs[OHCI_TD_GET_CC(le32toh(std->td.td_flags))]));
1233 
1234 			/* remove TDs */
1235 			for (p = std; p->xfer == xfer; p = n) {
1236 				n = p->nexttd;
1237 				ohci_free_std(sc, p);
1238 			}
1239 
1240 			/* clear halt */
1241 			opipe->sed->ed.ed_headp = htole32(p->physaddr);
1242 			OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1243 
1244 			if (cc == OHCI_CC_STALL)
1245 				xfer->status = USBD_STALLED;
1246 			else
1247 				xfer->status = USBD_IOERROR;
1248 			usb_transfer_complete(xfer);
1249 		}
1250 	}
1251 
1252 #ifdef OHCI_DEBUG
1253 	if (ohcidebug > 10) {
1254 		DPRINTF(("ohci_process_done: ITD done:\n"));
1255 		ohci_dump_itds(sidone);
1256 	}
1257 #endif
1258 
1259 	for (sitd = sidone; sitd != NULL; sitd = sitdnext) {
1260 		xfer = sitd->xfer;
1261 		sitdnext = sitd->dnext;
1262 		DPRINTFN(1, ("ohci_process_done: sitd=%p xfer=%p hcpriv=%p\n",
1263 			     sitd, xfer, xfer ? xfer->hcpriv : 0));
1264 		if (xfer == NULL)
1265 			continue;
1266 		if (xfer->status == USBD_CANCELLED ||
1267 		    xfer->status == USBD_TIMEOUT) {
1268 			DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1269 				 xfer));
1270 			/* Handled by abort routine. */
1271 			continue;
1272 		}
1273 #ifdef DIAGNOSTIC
1274 		if (sitd->isdone)
1275 			printf("ohci_softintr: sitd=%p is done\n", sitd);
1276 		sitd->isdone = 1;
1277 #endif
1278 		cc = OHCI_ITD_GET_CC(le32toh(sitd->itd.itd_flags));
1279 		if (cc == OHCI_CC_NO_ERROR) {
1280 			/* XXX compute length for input */
1281 			struct ohci_pipe *opipe =
1282 				(struct ohci_pipe *)xfer->pipe;
1283 			if (sitd->flags & OHCI_CALL_DONE) {
1284 				opipe->u.iso.inuse -= xfer->nframes;
1285 				/* XXX xfer->actlen = actlen; */
1286 				xfer->status = USBD_NORMAL_COMPLETION;
1287 				usb_transfer_complete(xfer);
1288 			}
1289 		} else {
1290 			/* XXX Do more */
1291 			xfer->status = USBD_IOERROR;
1292 			usb_transfer_complete(xfer);
1293 		}
1294 	}
1295 
1296 	sc->sc_bus.intr_context--;
1297 }
1298 
1299 void
1300 ohci_device_ctrl_done(usbd_xfer_handle xfer)
1301 {
1302 	DPRINTFN(10,("ohci_ctrl_done: xfer=%p\n", xfer));
1303 
1304 #ifdef DIAGNOSTIC
1305 	if (!(xfer->rqflags & URQ_REQUEST)) {
1306 		panic("ohci_ctrl_done: not a request\n");
1307 	}
1308 #endif
1309 	xfer->hcpriv = NULL;
1310 }
1311 
1312 void
1313 ohci_device_intr_done(usbd_xfer_handle xfer)
1314 {
1315 	struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1316 	ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1317 	ohci_soft_ed_t *sed = opipe->sed;
1318 	ohci_soft_td_t *data, *tail;
1319 
1320 
1321 	DPRINTFN(10,("ohci_intr_done: xfer=%p, actlen=%d\n",
1322 		     xfer, xfer->actlen));
1323 
1324 	xfer->hcpriv = NULL;
1325 
1326 	if (xfer->pipe->repeat) {
1327 		data = opipe->tail.td;
1328 		tail = ohci_alloc_std(sc); /* XXX should reuse TD */
1329 		if (tail == NULL) {
1330 			xfer->status = USBD_NOMEM;
1331 			return;
1332 		}
1333 		tail->xfer = NULL;
1334 
1335 		data->td.td_flags = htole32(
1336 			OHCI_TD_IN | OHCI_TD_NOCC |
1337 			OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
1338 		if (xfer->flags & USBD_SHORT_XFER_OK)
1339 			data->td.td_flags |= htole32(OHCI_TD_R);
1340 		data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf));
1341 		data->nexttd = tail;
1342 		data->td.td_nexttd = htole32(tail->physaddr);
1343 		data->td.td_be = htole32(le32toh(data->td.td_cbp) +
1344 			xfer->length - 1);
1345 		data->len = xfer->length;
1346 		data->xfer = xfer;
1347 		data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1348 		xfer->hcpriv = data;
1349 		xfer->actlen = 0;
1350 
1351 		sed->ed.ed_tailp = htole32(tail->physaddr);
1352 		opipe->tail.td = tail;
1353 	}
1354 }
1355 
1356 void
1357 ohci_device_bulk_done(usbd_xfer_handle xfer)
1358 {
1359 	DPRINTFN(10,("ohci_bulk_done: xfer=%p, actlen=%d\n",
1360 		     xfer, xfer->actlen));
1361 
1362 	xfer->hcpriv = NULL;
1363 }
1364 
1365 void
1366 ohci_rhsc(ohci_softc_t *sc, usbd_xfer_handle xfer)
1367 {
1368 	usbd_pipe_handle pipe;
1369 	struct ohci_pipe *opipe;
1370 	u_char *p;
1371 	int i, m;
1372 	int hstatus;
1373 
1374 	hstatus = OREAD4(sc, OHCI_RH_STATUS);
1375 	DPRINTF(("ohci_rhsc: sc=%p xfer=%p hstatus=0x%08x\n",
1376 		 sc, xfer, hstatus));
1377 
1378 	if (xfer == NULL) {
1379 		/* Just ignore the change. */
1380 		return;
1381 	}
1382 
1383 	pipe = xfer->pipe;
1384 	opipe = (struct ohci_pipe *)pipe;
1385 
1386 	p = KERNADDR(&xfer->dmabuf);
1387 	m = min(sc->sc_noport, xfer->length * 8 - 1);
1388 	memset(p, 0, xfer->length);
1389 	for (i = 1; i <= m; i++) {
1390 		/* Pick out CHANGE bits from the status reg. */
1391 		if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
1392 			p[i/8] |= 1 << (i%8);
1393 	}
1394 	DPRINTF(("ohci_rhsc: change=0x%02x\n", *p));
1395 	xfer->actlen = xfer->length;
1396 	xfer->status = USBD_NORMAL_COMPLETION;
1397 
1398 	usb_transfer_complete(xfer);
1399 }
1400 
1401 void
1402 ohci_root_intr_done(usbd_xfer_handle xfer)
1403 {
1404 	xfer->hcpriv = NULL;
1405 }
1406 
1407 void
1408 ohci_root_ctrl_done(usbd_xfer_handle xfer)
1409 {
1410 	xfer->hcpriv = NULL;
1411 }
1412 
1413 /*
1414  * Wait here until controller claims to have an interrupt.
1415  * Then call ohci_intr and return.  Use timeout to avoid waiting
1416  * too long.
1417  */
1418 void
1419 ohci_waitintr(ohci_softc_t *sc, usbd_xfer_handle xfer)
1420 {
1421 	int timo = xfer->timeout;
1422 	int usecs;
1423 	u_int32_t intrs;
1424 
1425 	xfer->status = USBD_IN_PROGRESS;
1426 	for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
1427 		usb_delay_ms(&sc->sc_bus, 1);
1428 		intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs;
1429 		DPRINTFN(15,("ohci_waitintr: 0x%04x\n", intrs));
1430 #ifdef OHCI_DEBUG
1431 		if (ohcidebug > 15)
1432 			ohci_dumpregs(sc);
1433 #endif
1434 		if (intrs) {
1435 			ohci_intr1(sc);
1436 			if (xfer->status != USBD_IN_PROGRESS)
1437 				return;
1438 		}
1439 	}
1440 
1441 	/* Timeout */
1442 	DPRINTF(("ohci_waitintr: timeout\n"));
1443 	xfer->status = USBD_TIMEOUT;
1444 	usb_transfer_complete(xfer);
1445 	/* XXX should free TD */
1446 }
1447 
1448 void
1449 ohci_poll(struct usbd_bus *bus)
1450 {
1451 	ohci_softc_t *sc = (ohci_softc_t *)bus;
1452 
1453 	if (OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs)
1454 		ohci_intr1(sc);
1455 }
1456 
1457 usbd_status
1458 ohci_device_request(usbd_xfer_handle xfer)
1459 {
1460 	struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1461 	usb_device_request_t *req = &xfer->request;
1462 	usbd_device_handle dev = opipe->pipe.device;
1463 	ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1464 	int addr = dev->address;
1465 	ohci_soft_td_t *setup, *stat, *next, *tail;
1466 	ohci_soft_ed_t *sed;
1467 	int isread;
1468 	int len;
1469 	usbd_status err;
1470 	int s;
1471 
1472 	isread = req->bmRequestType & UT_READ;
1473 	len = UGETW(req->wLength);
1474 
1475 	DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, "
1476 		    "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1477 		    req->bmRequestType, req->bRequest, UGETW(req->wValue),
1478 		    UGETW(req->wIndex), len, addr,
1479 		    opipe->pipe.endpoint->edesc->bEndpointAddress));
1480 
1481 	setup = opipe->tail.td;
1482 	stat = ohci_alloc_std(sc);
1483 	if (stat == NULL) {
1484 		err = USBD_NOMEM;
1485 		goto bad1;
1486 	}
1487 	tail = ohci_alloc_std(sc);
1488 	if (tail == NULL) {
1489 		err = USBD_NOMEM;
1490 		goto bad2;
1491 	}
1492 	tail->xfer = NULL;
1493 
1494 	sed = opipe->sed;
1495 	opipe->u.ctl.length = len;
1496 
1497 	/* Update device address and length since they may have changed. */
1498 	/* XXX This only needs to be done once, but it's too early in open. */
1499 	/* XXXX Should not touch ED here! */
1500 	sed->ed.ed_flags = htole32(
1501 	 (le32toh(sed->ed.ed_flags) & ~(OHCI_ED_ADDRMASK | OHCI_ED_MAXPMASK)) |
1502 	 OHCI_ED_SET_FA(addr) |
1503 	 OHCI_ED_SET_MAXP(UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize)));
1504 
1505 	next = stat;
1506 
1507 	/* Set up data transaction */
1508 	if (len != 0) {
1509 		ohci_soft_td_t *std = stat;
1510 
1511 		err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
1512 			  std, &stat);
1513 		stat = stat->nexttd; /* point at free TD */
1514 		if (err)
1515 			goto bad3;
1516 		/* Start toggle at 1 and then use the carried toggle. */
1517 		std->td.td_flags &= htole32(~OHCI_TD_TOGGLE_MASK);
1518 		std->td.td_flags |= htole32(OHCI_TD_TOGGLE_1);
1519 	}
1520 
1521 	memcpy(KERNADDR(&opipe->u.ctl.reqdma), req, sizeof *req);
1522 
1523 	setup->td.td_flags = htole32(OHCI_TD_SETUP | OHCI_TD_NOCC |
1524 				     OHCI_TD_TOGGLE_0 | OHCI_TD_NOINTR);
1525 	setup->td.td_cbp = htole32(DMAADDR(&opipe->u.ctl.reqdma));
1526 	setup->nexttd = next;
1527 	setup->td.td_nexttd = htole32(next->physaddr);
1528 	setup->td.td_be = htole32(le32toh(setup->td.td_cbp) + sizeof *req - 1);
1529 	setup->len = 0;
1530 	setup->xfer = xfer;
1531 	setup->flags = 0;
1532 	xfer->hcpriv = setup;
1533 
1534 	stat->td.td_flags = htole32(
1535 		(isread ? OHCI_TD_OUT : OHCI_TD_IN) |
1536 		OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1537 	stat->td.td_cbp = 0;
1538 	stat->nexttd = tail;
1539 	stat->td.td_nexttd = htole32(tail->physaddr);
1540 	stat->td.td_be = 0;
1541 	stat->flags = OHCI_CALL_DONE;
1542 	stat->len = 0;
1543 	stat->xfer = xfer;
1544 
1545 #ifdef OHCI_DEBUG
1546 	if (ohcidebug > 5) {
1547 		DPRINTF(("ohci_device_request:\n"));
1548 		ohci_dump_ed(sed);
1549 		ohci_dump_tds(setup);
1550 	}
1551 #endif
1552 
1553 	/* Insert ED in schedule */
1554 	s = splusb();
1555 	sed->ed.ed_tailp = htole32(tail->physaddr);
1556 	opipe->tail.td = tail;
1557 	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1558 	if (xfer->timeout && !sc->sc_bus.use_polling) {
1559                 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1560 			    ohci_timeout, xfer);
1561 	}
1562 	splx(s);
1563 
1564 #if 0
1565 	if (ohcidebug > 10) {
1566 		delay(10000);
1567 		DPRINTF(("ohci_device_request: status=%x\n",
1568 			 OREAD4(sc, OHCI_COMMAND_STATUS)));
1569 		ohci_dump_ed(sed);
1570 		ohci_dump_tds(setup);
1571 	}
1572 #endif
1573 
1574 	return (USBD_NORMAL_COMPLETION);
1575 
1576  bad3:
1577 	ohci_free_std(sc, tail);
1578  bad2:
1579 	ohci_free_std(sc, stat);
1580  bad1:
1581 	return (err);
1582 }
1583 
1584 /*
1585  * Add an ED to the schedule.  Called at splusb().
1586  */
1587 void
1588 ohci_add_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1589 {
1590 	SPLUSBCHECK;
1591 	sed->next = head->next;
1592 	sed->ed.ed_nexted = head->ed.ed_nexted;
1593 	head->next = sed;
1594 	head->ed.ed_nexted = htole32(sed->physaddr);
1595 }
1596 
1597 /*
1598  * Remove an ED from the schedule.  Called at splusb().
1599  */
1600 void
1601 ohci_rem_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head)
1602 {
1603 	ohci_soft_ed_t *p;
1604 
1605 	SPLUSBCHECK;
1606 
1607 	/* XXX */
1608 	for (p = head; p == NULL && p->next != sed; p = p->next)
1609 		;
1610 	if (p == NULL)
1611 		panic("ohci_rem_ed: ED not found\n");
1612 	p->next = sed->next;
1613 	p->ed.ed_nexted = sed->ed.ed_nexted;
1614 }
1615 
1616 /*
1617  * When a transfer is completed the TD is added to the done queue by
1618  * the host controller.  This queue is the processed by software.
1619  * Unfortunately the queue contains the physical address of the TD
1620  * and we have no simple way to translate this back to a kernel address.
1621  * To make the translation possible (and fast) we use a hash table of
1622  * TDs currently in the schedule.  The physical address is used as the
1623  * hash value.
1624  */
1625 
1626 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
1627 /* Called at splusb() */
1628 void
1629 ohci_hash_add_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1630 {
1631 	int h = HASH(std->physaddr);
1632 
1633 	SPLUSBCHECK;
1634 
1635 	LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
1636 }
1637 
1638 /* Called at splusb() */
1639 void
1640 ohci_hash_rem_td(ohci_softc_t *sc, ohci_soft_td_t *std)
1641 {
1642 	SPLUSBCHECK;
1643 
1644 	LIST_REMOVE(std, hnext);
1645 }
1646 
1647 ohci_soft_td_t *
1648 ohci_hash_find_td(ohci_softc_t *sc, ohci_physaddr_t a)
1649 {
1650 	int h = HASH(a);
1651 	ohci_soft_td_t *std;
1652 
1653 	for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
1654 	     std != NULL;
1655 	     std = LIST_NEXT(std, hnext))
1656 		if (std->physaddr == a)
1657 			return (std);
1658 	return (NULL);
1659 }
1660 
1661 /* Called at splusb() */
1662 void
1663 ohci_hash_add_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1664 {
1665 	int h = HASH(sitd->physaddr);
1666 
1667 	SPLUSBCHECK;
1668 
1669 	DPRINTFN(10,("ohci_hash_add_itd: sitd=%p physaddr=0x%08lx\n",
1670 		    sitd, (u_long)sitd->physaddr));
1671 
1672 	LIST_INSERT_HEAD(&sc->sc_hash_itds[h], sitd, hnext);
1673 }
1674 
1675 /* Called at splusb() */
1676 void
1677 ohci_hash_rem_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd)
1678 {
1679 	SPLUSBCHECK;
1680 
1681 	DPRINTFN(10,("ohci_hash_rem_itd: sitd=%p physaddr=0x%08lx\n",
1682 		    sitd, (u_long)sitd->physaddr));
1683 
1684 	LIST_REMOVE(sitd, hnext);
1685 }
1686 
1687 ohci_soft_itd_t *
1688 ohci_hash_find_itd(ohci_softc_t *sc, ohci_physaddr_t a)
1689 {
1690 	int h = HASH(a);
1691 	ohci_soft_itd_t *sitd;
1692 
1693 	for (sitd = LIST_FIRST(&sc->sc_hash_itds[h]);
1694 	     sitd != NULL;
1695 	     sitd = LIST_NEXT(sitd, hnext))
1696 		if (sitd->physaddr == a)
1697 			return (sitd);
1698 	return (NULL);
1699 }
1700 
1701 void
1702 ohci_timeout(void *addr)
1703 {
1704 	usbd_xfer_handle xfer = addr;
1705 	int s;
1706 
1707 	DPRINTF(("ohci_timeout: xfer=%p\n", xfer));
1708 
1709 	s = splusb();
1710 	xfer->device->bus->intr_context++;
1711 	ohci_abort_xfer(xfer, USBD_TIMEOUT);
1712 	xfer->device->bus->intr_context--;
1713 	splx(s);
1714 }
1715 
1716 #ifdef OHCI_DEBUG
1717 void
1718 ohci_dump_tds(ohci_soft_td_t *std)
1719 {
1720 	for (; std; std = std->nexttd)
1721 		ohci_dump_td(std);
1722 }
1723 
1724 void
1725 ohci_dump_td(ohci_soft_td_t *std)
1726 {
1727 	DPRINTF(("TD(%p) at %08lx: %b delay=%d ec=%d cc=%d\ncbp=0x%08lx "
1728 		 "nexttd=0x%08lx be=0x%08lx\n",
1729 		 std, (u_long)std->physaddr,
1730 		 (int)le32toh(std->td.td_flags),
1731 		 "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE",
1732 		 OHCI_TD_GET_DI(le32toh(std->td.td_flags)),
1733 		 OHCI_TD_GET_EC(le32toh(std->td.td_flags)),
1734 		 OHCI_TD_GET_CC(le32toh(std->td.td_flags)),
1735 		 (u_long)le32toh(std->td.td_cbp),
1736 		 (u_long)le32toh(std->td.td_nexttd),
1737 		 (u_long)le32toh(std->td.td_be)));
1738 }
1739 
1740 void
1741 ohci_dump_itd(ohci_soft_itd_t *sitd)
1742 {
1743 	int i;
1744 
1745 	DPRINTF(("ITD(%p) at %08lx: sf=%d di=%d fc=%d cc=%d\n"
1746 		 "bp0=0x%08lx next=0x%08lx be=0x%08lx\n",
1747 		 sitd, (u_long)sitd->physaddr,
1748 		 OHCI_ITD_GET_SF(le32toh(sitd->itd.itd_flags)),
1749 		 OHCI_ITD_GET_DI(le32toh(sitd->itd.itd_flags)),
1750 		 OHCI_ITD_GET_FC(le32toh(sitd->itd.itd_flags)),
1751 		 OHCI_ITD_GET_CC(le32toh(sitd->itd.itd_flags)),
1752 		 (u_long)le32toh(sitd->itd.itd_bp0),
1753 		 (u_long)le32toh(sitd->itd.itd_nextitd),
1754 		 (u_long)le32toh(sitd->itd.itd_be)));
1755 	for (i = 0; i < OHCI_ITD_NOFFSET; i++)
1756 		DPRINTF(("offs[%d]=0x%04x ", i,
1757 			 (u_int)le16toh(sitd->itd.itd_offset[i])));
1758 	DPRINTF(("\n"));
1759 }
1760 
1761 void
1762 ohci_dump_itds(ohci_soft_itd_t *sitd)
1763 {
1764 	for (; sitd; sitd = sitd->nextitd)
1765 		ohci_dump_itd(sitd);
1766 }
1767 
1768 void
1769 ohci_dump_ed(ohci_soft_ed_t *sed)
1770 {
1771 	DPRINTF(("ED(%p) at 0x%08lx: addr=%d endpt=%d maxp=%d %b\ntailp=0x%08lx "
1772 		 "headflags=%b headp=0x%08lx nexted=0x%08lx\n",
1773 		 sed, (u_long)sed->physaddr,
1774 		 OHCI_ED_GET_FA(le32toh(sed->ed.ed_flags)),
1775 		 OHCI_ED_GET_EN(le32toh(sed->ed.ed_flags)),
1776 		 OHCI_ED_GET_MAXP(le32toh(sed->ed.ed_flags)),
1777 		 (int)le32toh(sed->ed.ed_flags),
1778 		 "\20\14OUT\15IN\16LOWSPEED\17SKIP\20ISO",
1779 		 (u_long)le32toh(sed->ed.ed_tailp),
1780 		 (u_long)le32toh(sed->ed.ed_headp),
1781 		 "\20\1HALT\2CARRY",
1782 		 (u_long)le32toh(sed->ed.ed_headp),
1783 		 (u_long)le32toh(sed->ed.ed_nexted)));
1784 }
1785 #endif
1786 
1787 usbd_status
1788 ohci_open(usbd_pipe_handle pipe)
1789 {
1790 	usbd_device_handle dev = pipe->device;
1791 	ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1792 	usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1793 	struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
1794 	u_int8_t addr = dev->address;
1795 	u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
1796 	ohci_soft_ed_t *sed;
1797 	ohci_soft_td_t *std;
1798 	ohci_soft_itd_t *sitd;
1799 	ohci_physaddr_t tdphys;
1800 	u_int32_t fmt;
1801 	usbd_status err;
1802 	int s;
1803 	int ival;
1804 
1805 	DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1806 		     pipe, addr, ed->bEndpointAddress, sc->sc_addr));
1807 
1808 	std = NULL;
1809 	sed = NULL;
1810 
1811 	if (addr == sc->sc_addr) {
1812 		switch (ed->bEndpointAddress) {
1813 		case USB_CONTROL_ENDPOINT:
1814 			pipe->methods = &ohci_root_ctrl_methods;
1815 			break;
1816 		case UE_DIR_IN | OHCI_INTR_ENDPT:
1817 			pipe->methods = &ohci_root_intr_methods;
1818 			break;
1819 		default:
1820 			return (USBD_INVAL);
1821 		}
1822 	} else {
1823 		sed = ohci_alloc_sed(sc);
1824 		if (sed == NULL)
1825 			goto bad0;
1826 		opipe->sed = sed;
1827 		if (xfertype == UE_ISOCHRONOUS) {
1828 			sitd = ohci_alloc_sitd(sc);
1829 			if (sitd == NULL) {
1830 				ohci_free_sitd(sc, sitd);
1831 				goto bad1;
1832 			}
1833 			opipe->tail.itd = sitd;
1834 			tdphys = sitd->physaddr;
1835 			fmt = OHCI_ED_FORMAT_ISO;
1836 			if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
1837 				fmt |= OHCI_ED_DIR_IN;
1838 			else
1839 				fmt |= OHCI_ED_DIR_OUT;
1840 		} else {
1841 			std = ohci_alloc_std(sc);
1842 			if (std == NULL) {
1843 				ohci_free_std(sc, std);
1844 				goto bad1;
1845 			}
1846 			opipe->tail.td = std;
1847 			tdphys = std->physaddr;
1848 			fmt = OHCI_ED_FORMAT_GEN | OHCI_ED_DIR_TD;
1849 		}
1850 		sed->ed.ed_flags = htole32(
1851 			OHCI_ED_SET_FA(addr) |
1852 			OHCI_ED_SET_EN(ed->bEndpointAddress) |
1853 			(dev->lowspeed ? OHCI_ED_SPEED : 0) | fmt |
1854 			OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
1855 		sed->ed.ed_headp = sed->ed.ed_tailp = htole32(tdphys);
1856 
1857 		switch (xfertype) {
1858 		case UE_CONTROL:
1859 			pipe->methods = &ohci_device_ctrl_methods;
1860 			err = usb_allocmem(&sc->sc_bus,
1861 				  sizeof(usb_device_request_t),
1862 				  0, &opipe->u.ctl.reqdma);
1863 			if (err)
1864 				goto bad;
1865 			s = splusb();
1866 			ohci_add_ed(sed, sc->sc_ctrl_head);
1867 			splx(s);
1868 			break;
1869 		case UE_INTERRUPT:
1870 			pipe->methods = &ohci_device_intr_methods;
1871 			ival = pipe->interval;
1872 			if (ival == USBD_DEFAULT_INTERVAL)
1873 				ival = ed->bInterval;
1874 			return (ohci_device_setintr(sc, opipe, ival));
1875 		case UE_ISOCHRONOUS:
1876 			pipe->methods = &ohci_device_isoc_methods;
1877 			return (ohci_setup_isoc(pipe));
1878 		case UE_BULK:
1879 			pipe->methods = &ohci_device_bulk_methods;
1880 			s = splusb();
1881 			ohci_add_ed(sed, sc->sc_bulk_head);
1882 			splx(s);
1883 			break;
1884 		}
1885 	}
1886 	return (USBD_NORMAL_COMPLETION);
1887 
1888  bad:
1889 	if (std != NULL)
1890 		ohci_free_std(sc, std);
1891  bad1:
1892 	if (sed != NULL)
1893 		ohci_free_sed(sc, sed);
1894  bad0:
1895 	return (USBD_NOMEM);
1896 
1897 }
1898 
1899 /*
1900  * Close a reqular pipe.
1901  * Assumes that there are no pending transactions.
1902  */
1903 void
1904 ohci_close_pipe(usbd_pipe_handle pipe, ohci_soft_ed_t *head)
1905 {
1906 	struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
1907 	ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
1908 	ohci_soft_ed_t *sed = opipe->sed;
1909 	int s;
1910 
1911 	s = splusb();
1912 #ifdef DIAGNOSTIC
1913 	sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
1914 	if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
1915 	    (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK)) {
1916 		ohci_physaddr_t td = le32toh(sed->ed.ed_headp);
1917 		ohci_soft_td_t *std;
1918 		for (std = LIST_FIRST(&sc->sc_hash_tds[HASH(td)]);
1919 		     std != NULL;
1920 		     std = LIST_NEXT(std, hnext))
1921 		    if (std->physaddr == td)
1922 			break;
1923 		printf("ohci_close_pipe: pipe not empty sed=%p hd=0x%x "
1924 		       "tl=0x%x pipe=%p, std=%p\n", sed,
1925 		       (int)le32toh(sed->ed.ed_headp),
1926 		       (int)le32toh(sed->ed.ed_tailp),
1927 		       pipe, std);
1928 		usb_delay_ms(&sc->sc_bus, 2);
1929 		if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
1930 		    (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
1931 			printf("ohci_close_pipe: pipe still not empty\n");
1932 	}
1933 #endif
1934 	ohci_rem_ed(sed, head);
1935 	splx(s);
1936 	ohci_free_sed(sc, opipe->sed);
1937 }
1938 
1939 /*
1940  * Abort a device request.
1941  * If this routine is called at splusb() it guarantees that the request
1942  * will be removed from the hardware scheduling and that the callback
1943  * for it will be called with USBD_CANCELLED status.
1944  * It's impossible to guarantee that the requested transfer will not
1945  * have happened since the hardware runs concurrently.
1946  * If the transaction has already happened we rely on the ordinary
1947  * interrupt processing to process it.
1948  */
1949 void
1950 ohci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1951 {
1952 	struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1953 	ohci_soft_ed_t *sed;
1954 
1955 	DPRINTF(("ohci_abort_xfer: xfer=%p pipe=%p\n", xfer, opipe));
1956 
1957 	xfer->status = status;
1958 
1959 	usb_uncallout(xfer->timeout_handle, ohci_timeout, xfer);
1960 
1961 	sed = opipe->sed;
1962 	DPRINTFN(1,("ohci_abort_xfer: stop ed=%p\n", sed));
1963 	sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
1964 
1965 #if 1
1966 	if (xfer->device->bus->intr_context) {
1967 		/* We have no process context, so we can't use tsleep(). */
1968 		usb_callout(xfer->pipe->abort_handle,
1969 		    hz / USB_FRAMES_PER_SECOND, ohci_abort_xfer_end, xfer);
1970 	} else {
1971 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
1972 		KASSERT(intr_nesting_level == 0,
1973 	        	("ohci_abort_req in interrupt context"));
1974 #endif
1975 		usb_delay_ms(opipe->pipe.device->bus, 1);
1976 		ohci_abort_xfer_end(xfer);
1977 	}
1978 #else
1979 	delay(1000);
1980 	ohci_abort_xfer_end(xfer);
1981 #endif
1982 }
1983 
1984 void
1985 ohci_abort_xfer_end(void *v)
1986 {
1987 	usbd_xfer_handle xfer = v;
1988 	struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1989 	ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1990 	ohci_soft_ed_t *sed;
1991 	ohci_soft_td_t *p, *n;
1992 	int s;
1993 
1994 	s = splusb();
1995 
1996 	p = xfer->hcpriv;
1997 #ifdef DIAGNOSTIC
1998 	if (p == NULL) {
1999 		printf("ohci_abort_xfer: hcpriv==0\n");
2000 		return;
2001 	}
2002 #endif
2003 	for (; p->xfer == xfer; p = n) {
2004 		n = p->nexttd;
2005 		ohci_free_std(sc, p);
2006 	}
2007 
2008 	sed = opipe->sed;
2009 	DPRINTFN(2,("ohci_abort_xfer: set hd=%x, tl=%x\n",
2010 		    (int)p->physaddr, (int)le32toh(sed->ed.ed_tailp)));
2011 	sed->ed.ed_headp = htole32(p->physaddr); /* unlink TDs */
2012 	sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
2013 
2014 	usb_transfer_complete(xfer);
2015 
2016 	splx(s);
2017 }
2018 
2019 /*
2020  * Data structures and routines to emulate the root hub.
2021  */
2022 Static usb_device_descriptor_t ohci_devd = {
2023 	USB_DEVICE_DESCRIPTOR_SIZE,
2024 	UDESC_DEVICE,		/* type */
2025 	{0x00, 0x01},		/* USB version */
2026 	UDCLASS_HUB,		/* class */
2027 	UDSUBCLASS_HUB,		/* subclass */
2028 	0,			/* protocol */
2029 	64,			/* max packet */
2030 	{0},{0},{0x00,0x01},	/* device id */
2031 	1,2,0,			/* string indicies */
2032 	1			/* # of configurations */
2033 };
2034 
2035 Static usb_config_descriptor_t ohci_confd = {
2036 	USB_CONFIG_DESCRIPTOR_SIZE,
2037 	UDESC_CONFIG,
2038 	{USB_CONFIG_DESCRIPTOR_SIZE +
2039 	 USB_INTERFACE_DESCRIPTOR_SIZE +
2040 	 USB_ENDPOINT_DESCRIPTOR_SIZE},
2041 	1,
2042 	1,
2043 	0,
2044 	UC_SELF_POWERED,
2045 	0			/* max power */
2046 };
2047 
2048 Static usb_interface_descriptor_t ohci_ifcd = {
2049 	USB_INTERFACE_DESCRIPTOR_SIZE,
2050 	UDESC_INTERFACE,
2051 	0,
2052 	0,
2053 	1,
2054 	UICLASS_HUB,
2055 	UISUBCLASS_HUB,
2056 	0,
2057 	0
2058 };
2059 
2060 Static usb_endpoint_descriptor_t ohci_endpd = {
2061 	USB_ENDPOINT_DESCRIPTOR_SIZE,
2062 	UDESC_ENDPOINT,
2063 	UE_DIR_IN | OHCI_INTR_ENDPT,
2064 	UE_INTERRUPT,
2065 	{8, 0},			/* max packet */
2066 	255
2067 };
2068 
2069 Static usb_hub_descriptor_t ohci_hubd = {
2070 	USB_HUB_DESCRIPTOR_SIZE,
2071 	UDESC_HUB,
2072 	0,
2073 	{0,0},
2074 	0,
2075 	0,
2076 	{0},
2077 };
2078 
2079 Static int
2080 ohci_str(p, l, s)
2081 	usb_string_descriptor_t *p;
2082 	int l;
2083 	char *s;
2084 {
2085 	int i;
2086 
2087 	if (l == 0)
2088 		return (0);
2089 	p->bLength = 2 * strlen(s) + 2;
2090 	if (l == 1)
2091 		return (1);
2092 	p->bDescriptorType = UDESC_STRING;
2093 	l -= 2;
2094 	for (i = 0; s[i] && l > 1; i++, l -= 2)
2095 		USETW2(p->bString[i], 0, s[i]);
2096 	return (2*i+2);
2097 }
2098 
2099 /*
2100  * Simulate a hardware hub by handling all the necessary requests.
2101  */
2102 Static usbd_status
2103 ohci_root_ctrl_transfer(usbd_xfer_handle xfer)
2104 {
2105 	usbd_status err;
2106 
2107 	/* Insert last in queue. */
2108 	err = usb_insert_transfer(xfer);
2109 	if (err)
2110 		return (err);
2111 
2112 	/* Pipe isn't running, start first */
2113 	return (ohci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2114 }
2115 
2116 Static usbd_status
2117 ohci_root_ctrl_start(usbd_xfer_handle xfer)
2118 {
2119 	ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2120 	usb_device_request_t *req;
2121 	void *buf = NULL;
2122 	int port, i;
2123 	int s, len, value, index, l, totlen = 0;
2124 	usb_port_status_t ps;
2125 	usb_hub_descriptor_t hubd;
2126 	usbd_status err;
2127 	u_int32_t v;
2128 
2129 	if (sc->sc_dying)
2130 		return (USBD_IOERROR);
2131 
2132 #ifdef DIAGNOSTIC
2133 	if (!(xfer->rqflags & URQ_REQUEST))
2134 		/* XXX panic */
2135 		return (USBD_INVAL);
2136 #endif
2137 	req = &xfer->request;
2138 
2139 	DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n",
2140 		    req->bmRequestType, req->bRequest));
2141 
2142 	len = UGETW(req->wLength);
2143 	value = UGETW(req->wValue);
2144 	index = UGETW(req->wIndex);
2145 
2146 	if (len != 0)
2147 		buf = KERNADDR(&xfer->dmabuf);
2148 
2149 #define C(x,y) ((x) | ((y) << 8))
2150 	switch(C(req->bRequest, req->bmRequestType)) {
2151 	case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2152 	case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2153 	case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2154 		/*
2155 		 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2156 		 * for the integrated root hub.
2157 		 */
2158 		break;
2159 	case C(UR_GET_CONFIG, UT_READ_DEVICE):
2160 		if (len > 0) {
2161 			*(u_int8_t *)buf = sc->sc_conf;
2162 			totlen = 1;
2163 		}
2164 		break;
2165 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2166 		DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value));
2167 		switch(value >> 8) {
2168 		case UDESC_DEVICE:
2169 			if ((value & 0xff) != 0) {
2170 				err = USBD_IOERROR;
2171 				goto ret;
2172 			}
2173 			totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2174 			USETW(ohci_devd.idVendor, sc->sc_id_vendor);
2175 			memcpy(buf, &ohci_devd, l);
2176 			break;
2177 		case UDESC_CONFIG:
2178 			if ((value & 0xff) != 0) {
2179 				err = USBD_IOERROR;
2180 				goto ret;
2181 			}
2182 			totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2183 			memcpy(buf, &ohci_confd, l);
2184 			buf = (char *)buf + l;
2185 			len -= l;
2186 			l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2187 			totlen += l;
2188 			memcpy(buf, &ohci_ifcd, l);
2189 			buf = (char *)buf + l;
2190 			len -= l;
2191 			l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2192 			totlen += l;
2193 			memcpy(buf, &ohci_endpd, l);
2194 			break;
2195 		case UDESC_STRING:
2196 			if (len == 0)
2197 				break;
2198 			*(u_int8_t *)buf = 0;
2199 			totlen = 1;
2200 			switch (value & 0xff) {
2201 			case 1: /* Vendor */
2202 				totlen = ohci_str(buf, len, sc->sc_vendor);
2203 				break;
2204 			case 2: /* Product */
2205 				totlen = ohci_str(buf, len, "OHCI root hub");
2206 				break;
2207 			}
2208 			break;
2209 		default:
2210 			err = USBD_IOERROR;
2211 			goto ret;
2212 		}
2213 		break;
2214 	case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2215 		if (len > 0) {
2216 			*(u_int8_t *)buf = 0;
2217 			totlen = 1;
2218 		}
2219 		break;
2220 	case C(UR_GET_STATUS, UT_READ_DEVICE):
2221 		if (len > 1) {
2222 			USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2223 			totlen = 2;
2224 		}
2225 		break;
2226 	case C(UR_GET_STATUS, UT_READ_INTERFACE):
2227 	case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2228 		if (len > 1) {
2229 			USETW(((usb_status_t *)buf)->wStatus, 0);
2230 			totlen = 2;
2231 		}
2232 		break;
2233 	case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2234 		if (value >= USB_MAX_DEVICES) {
2235 			err = USBD_IOERROR;
2236 			goto ret;
2237 		}
2238 		sc->sc_addr = value;
2239 		break;
2240 	case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2241 		if (value != 0 && value != 1) {
2242 			err = USBD_IOERROR;
2243 			goto ret;
2244 		}
2245 		sc->sc_conf = value;
2246 		break;
2247 	case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2248 		break;
2249 	case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2250 	case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2251 	case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2252 		err = USBD_IOERROR;
2253 		goto ret;
2254 	case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2255 		break;
2256 	case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2257 		break;
2258 	/* Hub requests */
2259 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2260 		break;
2261 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2262 		DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2263 			     "port=%d feature=%d\n",
2264 			     index, value));
2265 		if (index < 1 || index > sc->sc_noport) {
2266 			err = USBD_IOERROR;
2267 			goto ret;
2268 		}
2269 		port = OHCI_RH_PORT_STATUS(index);
2270 		switch(value) {
2271 		case UHF_PORT_ENABLE:
2272 			OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
2273 			break;
2274 		case UHF_PORT_SUSPEND:
2275 			OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
2276 			break;
2277 		case UHF_PORT_POWER:
2278 			/* Yes, writing to the LOW_SPEED bit clears power. */
2279 			OWRITE4(sc, port, UPS_LOW_SPEED);
2280 			break;
2281 		case UHF_C_PORT_CONNECTION:
2282 			OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
2283 			break;
2284 		case UHF_C_PORT_ENABLE:
2285 			OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
2286 			break;
2287 		case UHF_C_PORT_SUSPEND:
2288 			OWRITE4(sc, port, UPS_C_SUSPEND << 16);
2289 			break;
2290 		case UHF_C_PORT_OVER_CURRENT:
2291 			OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
2292 			break;
2293 		case UHF_C_PORT_RESET:
2294 			OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
2295 			break;
2296 		default:
2297 			err = USBD_IOERROR;
2298 			goto ret;
2299 		}
2300 		switch(value) {
2301 		case UHF_C_PORT_CONNECTION:
2302 		case UHF_C_PORT_ENABLE:
2303 		case UHF_C_PORT_SUSPEND:
2304 		case UHF_C_PORT_OVER_CURRENT:
2305 		case UHF_C_PORT_RESET:
2306 			/* Enable RHSC interrupt if condition is cleared. */
2307 			if ((OREAD4(sc, port) >> 16) == 0)
2308 				ohci_rhsc_able(sc, 1);
2309 			break;
2310 		default:
2311 			break;
2312 		}
2313 		break;
2314 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2315 		if (value != 0) {
2316 			err = USBD_IOERROR;
2317 			goto ret;
2318 		}
2319 		v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
2320 		hubd = ohci_hubd;
2321 		hubd.bNbrPorts = sc->sc_noport;
2322 		USETW(hubd.wHubCharacteristics,
2323 		      (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
2324 		       v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
2325 		      /* XXX overcurrent */
2326 		      );
2327 		hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
2328 		v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
2329 		for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2330 			hubd.DeviceRemovable[i++] = (u_int8_t)v;
2331 		hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2332 		l = min(len, hubd.bDescLength);
2333 		totlen = l;
2334 		memcpy(buf, &hubd, l);
2335 		break;
2336 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2337 		if (len != 4) {
2338 			err = USBD_IOERROR;
2339 			goto ret;
2340 		}
2341 		memset(buf, 0, len); /* ? XXX */
2342 		totlen = len;
2343 		break;
2344 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2345 		DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n",
2346 			    index));
2347 		if (index < 1 || index > sc->sc_noport) {
2348 			err = USBD_IOERROR;
2349 			goto ret;
2350 		}
2351 		if (len != 4) {
2352 			err = USBD_IOERROR;
2353 			goto ret;
2354 		}
2355 		v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
2356 		DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n",
2357 			    v));
2358 		USETW(ps.wPortStatus, v);
2359 		USETW(ps.wPortChange, v >> 16);
2360 		l = min(len, sizeof ps);
2361 		memcpy(buf, &ps, l);
2362 		totlen = l;
2363 		break;
2364 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2365 		err = USBD_IOERROR;
2366 		goto ret;
2367 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2368 		break;
2369 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2370 		if (index < 1 || index > sc->sc_noport) {
2371 			err = USBD_IOERROR;
2372 			goto ret;
2373 		}
2374 		port = OHCI_RH_PORT_STATUS(index);
2375 		switch(value) {
2376 		case UHF_PORT_ENABLE:
2377 			OWRITE4(sc, port, UPS_PORT_ENABLED);
2378 			break;
2379 		case UHF_PORT_SUSPEND:
2380 			OWRITE4(sc, port, UPS_SUSPEND);
2381 			break;
2382 		case UHF_PORT_RESET:
2383 			DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n",
2384 				    index));
2385 			OWRITE4(sc, port, UPS_RESET);
2386 			for (i = 0; i < 10; i++) {
2387 				usb_delay_ms(&sc->sc_bus, 10);
2388 				if ((OREAD4(sc, port) & UPS_RESET) == 0)
2389 					break;
2390 			}
2391 			DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n",
2392 				    index, OREAD4(sc, port)));
2393 			break;
2394 		case UHF_PORT_POWER:
2395 			DPRINTFN(2,("ohci_root_ctrl_transfer: set port power "
2396 				    "%d\n", index));
2397 			OWRITE4(sc, port, UPS_PORT_POWER);
2398 			break;
2399 		default:
2400 			err = USBD_IOERROR;
2401 			goto ret;
2402 		}
2403 		break;
2404 	default:
2405 		err = USBD_IOERROR;
2406 		goto ret;
2407 	}
2408 	xfer->actlen = totlen;
2409 	err = USBD_NORMAL_COMPLETION;
2410  ret:
2411 	xfer->status = err;
2412 	s = splusb();
2413 	usb_transfer_complete(xfer);
2414 	splx(s);
2415 	return (USBD_IN_PROGRESS);
2416 }
2417 
2418 /* Abort a root control request. */
2419 Static void
2420 ohci_root_ctrl_abort(usbd_xfer_handle xfer)
2421 {
2422 	/* Nothing to do, all transfers are synchronous. */
2423 }
2424 
2425 /* Close the root pipe. */
2426 Static void
2427 ohci_root_ctrl_close(usbd_pipe_handle pipe)
2428 {
2429 	DPRINTF(("ohci_root_ctrl_close\n"));
2430 	/* Nothing to do. */
2431 }
2432 
2433 Static usbd_status
2434 ohci_root_intr_transfer(usbd_xfer_handle xfer)
2435 {
2436 	usbd_status err;
2437 
2438 	/* Insert last in queue. */
2439 	err = usb_insert_transfer(xfer);
2440 	if (err)
2441 		return (err);
2442 
2443 	/* Pipe isn't running, start first */
2444 	return (ohci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2445 }
2446 
2447 Static usbd_status
2448 ohci_root_intr_start(usbd_xfer_handle xfer)
2449 {
2450 	usbd_pipe_handle pipe = xfer->pipe;
2451 	ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2452 
2453 	if (sc->sc_dying)
2454 		return (USBD_IOERROR);
2455 
2456 	sc->sc_intrxfer = xfer;
2457 
2458 	return (USBD_IN_PROGRESS);
2459 }
2460 
2461 /* Abort a root interrupt request. */
2462 Static void
2463 ohci_root_intr_abort(usbd_xfer_handle xfer)
2464 {
2465 	int s;
2466 
2467 	if (xfer->pipe->intrxfer == xfer) {
2468 		DPRINTF(("ohci_root_intr_abort: remove\n"));
2469 		xfer->pipe->intrxfer = NULL;
2470 	}
2471 	xfer->status = USBD_CANCELLED;
2472 	s = splusb();
2473 	usb_transfer_complete(xfer);
2474 	splx(s);
2475 }
2476 
2477 /* Close the root pipe. */
2478 Static void
2479 ohci_root_intr_close(usbd_pipe_handle pipe)
2480 {
2481 	ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2482 
2483 	DPRINTF(("ohci_root_intr_close\n"));
2484 
2485 	sc->sc_intrxfer = NULL;
2486 }
2487 
2488 /************************/
2489 
2490 Static usbd_status
2491 ohci_device_ctrl_transfer(usbd_xfer_handle xfer)
2492 {
2493 	usbd_status err;
2494 
2495 	/* Insert last in queue. */
2496 	err = usb_insert_transfer(xfer);
2497 	if (err)
2498 		return (err);
2499 
2500 	/* Pipe isn't running, start first */
2501 	return (ohci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2502 }
2503 
2504 Static usbd_status
2505 ohci_device_ctrl_start(usbd_xfer_handle xfer)
2506 {
2507 	ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2508 	usbd_status err;
2509 
2510 	if (sc->sc_dying)
2511 		return (USBD_IOERROR);
2512 
2513 #ifdef DIAGNOSTIC
2514 	if (!(xfer->rqflags & URQ_REQUEST)) {
2515 		/* XXX panic */
2516 		printf("ohci_device_ctrl_transfer: not a request\n");
2517 		return (USBD_INVAL);
2518 	}
2519 #endif
2520 
2521 	err = ohci_device_request(xfer);
2522 	if (err)
2523 		return (err);
2524 
2525 	if (sc->sc_bus.use_polling)
2526 		ohci_waitintr(sc, xfer);
2527 	return (USBD_IN_PROGRESS);
2528 }
2529 
2530 /* Abort a device control request. */
2531 Static void
2532 ohci_device_ctrl_abort(usbd_xfer_handle xfer)
2533 {
2534 	DPRINTF(("ohci_device_ctrl_abort: xfer=%p\n", xfer));
2535 	ohci_abort_xfer(xfer, USBD_CANCELLED);
2536 }
2537 
2538 /* Close a device control pipe. */
2539 Static void
2540 ohci_device_ctrl_close(usbd_pipe_handle pipe)
2541 {
2542 	struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2543 	ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2544 
2545 	DPRINTF(("ohci_device_ctrl_close: pipe=%p\n", pipe));
2546 	ohci_close_pipe(pipe, sc->sc_ctrl_head);
2547 	ohci_free_std(sc, opipe->tail.td);
2548 }
2549 
2550 /************************/
2551 
2552 Static void
2553 ohci_device_clear_toggle(usbd_pipe_handle pipe)
2554 {
2555 	struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2556 
2557 	opipe->sed->ed.ed_headp &= htole32(~OHCI_TOGGLECARRY);
2558 }
2559 
2560 Static void
2561 ohci_noop(usbd_pipe_handle pipe)
2562 {
2563 }
2564 
2565 Static usbd_status
2566 ohci_device_bulk_transfer(usbd_xfer_handle xfer)
2567 {
2568 	usbd_status err;
2569 
2570 	/* Insert last in queue. */
2571 	err = usb_insert_transfer(xfer);
2572 	if (err)
2573 		return (err);
2574 
2575 	/* Pipe isn't running, start first */
2576 	return (ohci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2577 }
2578 
2579 Static usbd_status
2580 ohci_device_bulk_start(usbd_xfer_handle xfer)
2581 {
2582 	struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2583 	usbd_device_handle dev = opipe->pipe.device;
2584 	ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2585 	int addr = dev->address;
2586 	ohci_soft_td_t *data, *tail, *tdp;
2587 	ohci_soft_ed_t *sed;
2588 	int s, len, isread, endpt;
2589 	usbd_status err;
2590 
2591 	if (sc->sc_dying)
2592 		return (USBD_IOERROR);
2593 
2594 #ifdef DIAGNOSTIC
2595 	if (xfer->rqflags & URQ_REQUEST) {
2596 		/* XXX panic */
2597 		printf("ohci_device_bulk_start: a request\n");
2598 		return (USBD_INVAL);
2599 	}
2600 #endif
2601 
2602 	len = xfer->length;
2603 	endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2604 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2605 	sed = opipe->sed;
2606 
2607 	DPRINTFN(4,("ohci_device_bulk_start: xfer=%p len=%d isread=%d "
2608 		    "flags=%d endpt=%d\n", xfer, len, isread, xfer->flags,
2609 		    endpt));
2610 
2611 	opipe->u.bulk.isread = isread;
2612 	opipe->u.bulk.length = len;
2613 
2614 	/* Update device address */
2615 	sed->ed.ed_flags = htole32(
2616 		(le32toh(sed->ed.ed_flags) & ~OHCI_ED_ADDRMASK) |
2617 		OHCI_ED_SET_FA(addr));
2618 
2619 	/* Allocate a chain of new TDs (including a new tail). */
2620 	data = opipe->tail.td;
2621 	err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer,
2622 		  data, &tail);
2623 	/* We want interrupt at the end of the transfer. */
2624 	tail->td.td_flags &= htole32(~OHCI_TD_INTR_MASK);
2625 	tail->td.td_flags |= htole32(OHCI_TD_SET_DI(1));
2626 	tail->flags |= OHCI_CALL_DONE;
2627 	tail = tail->nexttd;	/* point at sentinel */
2628 	if (err)
2629 		return (err);
2630 
2631 	tail->xfer = NULL;
2632 	xfer->hcpriv = data;
2633 
2634 	DPRINTFN(4,("ohci_device_bulk_start: ed_flags=0x%08x td_flags=0x%08x "
2635 		    "td_cbp=0x%08x td_be=0x%08x\n",
2636 		    (int)le32toh(sed->ed.ed_flags),
2637 		    (int)le32toh(data->td.td_flags),
2638 		    (int)le32toh(data->td.td_cbp),
2639 		    (int)le32toh(data->td.td_be)));
2640 
2641 #ifdef OHCI_DEBUG
2642 	if (ohcidebug > 5) {
2643 		ohci_dump_ed(sed);
2644 		ohci_dump_tds(data);
2645 	}
2646 #endif
2647 
2648 	/* Insert ED in schedule */
2649 	s = splusb();
2650 	for (tdp = data; tdp != tail; tdp = tdp->nexttd) {
2651 		tdp->xfer = xfer;
2652 	}
2653 	sed->ed.ed_tailp = htole32(tail->physaddr);
2654 	opipe->tail.td = tail;
2655 	sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
2656 	OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
2657 	if (xfer->timeout && !sc->sc_bus.use_polling) {
2658                 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2659 			    ohci_timeout, xfer);
2660 	}
2661 
2662 #if 0
2663 /* This goes wrong if we are too slow. */
2664 	if (ohcidebug > 10) {
2665 		delay(10000);
2666 		DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2667 			 OREAD4(sc, OHCI_COMMAND_STATUS)));
2668 		ohci_dump_ed(sed);
2669 		ohci_dump_tds(data);
2670 	}
2671 #endif
2672 
2673 	splx(s);
2674 
2675 	return (USBD_IN_PROGRESS);
2676 }
2677 
2678 Static void
2679 ohci_device_bulk_abort(usbd_xfer_handle xfer)
2680 {
2681 	DPRINTF(("ohci_device_bulk_abort: xfer=%p\n", xfer));
2682 	ohci_abort_xfer(xfer, USBD_CANCELLED);
2683 }
2684 
2685 /*
2686  * Close a device bulk pipe.
2687  */
2688 Static void
2689 ohci_device_bulk_close(usbd_pipe_handle pipe)
2690 {
2691 	struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2692 	ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2693 
2694 	DPRINTF(("ohci_device_bulk_close: pipe=%p\n", pipe));
2695 	ohci_close_pipe(pipe, sc->sc_bulk_head);
2696 	ohci_free_std(sc, opipe->tail.td);
2697 }
2698 
2699 /************************/
2700 
2701 Static usbd_status
2702 ohci_device_intr_transfer(usbd_xfer_handle xfer)
2703 {
2704 	usbd_status err;
2705 
2706 	/* Insert last in queue. */
2707 	err = usb_insert_transfer(xfer);
2708 	if (err)
2709 		return (err);
2710 
2711 	/* Pipe isn't running, start first */
2712 	return (ohci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2713 }
2714 
2715 Static usbd_status
2716 ohci_device_intr_start(usbd_xfer_handle xfer)
2717 {
2718 	struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2719 	usbd_device_handle dev = opipe->pipe.device;
2720 	ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2721 	ohci_soft_ed_t *sed = opipe->sed;
2722 	ohci_soft_td_t *data, *tail;
2723 	int len;
2724 	int s;
2725 
2726 	if (sc->sc_dying)
2727 		return (USBD_IOERROR);
2728 
2729 	DPRINTFN(3, ("ohci_device_intr_transfer: xfer=%p len=%d "
2730 		     "flags=%d priv=%p\n",
2731 		     xfer, xfer->length, xfer->flags, xfer->priv));
2732 
2733 #ifdef DIAGNOSTIC
2734 	if (xfer->rqflags & URQ_REQUEST)
2735 		panic("ohci_device_intr_transfer: a request\n");
2736 #endif
2737 
2738 	len = xfer->length;
2739 
2740 	data = opipe->tail.td;
2741 	tail = ohci_alloc_std(sc);
2742 	if (tail == NULL)
2743 		return (USBD_NOMEM);
2744 	tail->xfer = NULL;
2745 
2746 	data->td.td_flags = htole32(
2747 		OHCI_TD_IN | OHCI_TD_NOCC |
2748 		OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
2749 	if (xfer->flags & USBD_SHORT_XFER_OK)
2750 		data->td.td_flags |= htole32(OHCI_TD_R);
2751 	data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf));
2752 	data->nexttd = tail;
2753 	data->td.td_nexttd = htole32(tail->physaddr);
2754 	data->td.td_be = htole32(le32toh(data->td.td_cbp) + len - 1);
2755 	data->len = len;
2756 	data->xfer = xfer;
2757 	data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
2758 	xfer->hcpriv = data;
2759 
2760 #ifdef OHCI_DEBUG
2761 	if (ohcidebug > 5) {
2762 		DPRINTF(("ohci_device_intr_transfer:\n"));
2763 		ohci_dump_ed(sed);
2764 		ohci_dump_tds(data);
2765 	}
2766 #endif
2767 
2768 	/* Insert ED in schedule */
2769 	s = splusb();
2770 	sed->ed.ed_tailp = htole32(tail->physaddr);
2771 	opipe->tail.td = tail;
2772 	sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP);
2773 
2774 #if 0
2775 /*
2776  * This goes horribly wrong, printing thousands of descriptors,
2777  * because false references are followed due to the fact that the
2778  * TD is gone.
2779  */
2780 	if (ohcidebug > 5) {
2781 		usb_delay_ms(&sc->sc_bus, 5);
2782 		DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2783 			 OREAD4(sc, OHCI_COMMAND_STATUS)));
2784 		ohci_dump_ed(sed);
2785 		ohci_dump_tds(data);
2786 	}
2787 #endif
2788 	splx(s);
2789 
2790 	return (USBD_IN_PROGRESS);
2791 }
2792 
2793 /* Abort a device control request. */
2794 Static void
2795 ohci_device_intr_abort(usbd_xfer_handle xfer)
2796 {
2797 	if (xfer->pipe->intrxfer == xfer) {
2798 		DPRINTF(("ohci_device_intr_abort: remove\n"));
2799 		xfer->pipe->intrxfer = NULL;
2800 	}
2801 	ohci_abort_xfer(xfer, USBD_CANCELLED);
2802 }
2803 
2804 /* Close a device interrupt pipe. */
2805 Static void
2806 ohci_device_intr_close(usbd_pipe_handle pipe)
2807 {
2808 	struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2809 	ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2810 	int nslots = opipe->u.intr.nslots;
2811 	int pos = opipe->u.intr.pos;
2812 	int j;
2813 	ohci_soft_ed_t *p, *sed = opipe->sed;
2814 	int s;
2815 
2816 	DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n",
2817 		    pipe, nslots, pos));
2818 	s = splusb();
2819 	sed->ed.ed_flags |= htole32(OHCI_ED_SKIP);
2820 	if ((le32toh(sed->ed.ed_tailp) & OHCI_HEADMASK) !=
2821 	    (le32toh(sed->ed.ed_headp) & OHCI_HEADMASK))
2822 		usb_delay_ms(&sc->sc_bus, 2);
2823 
2824 	for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
2825 		;
2826 #ifdef DIAGNOSTIC
2827 	if (p == NULL)
2828 		panic("ohci_device_intr_close: ED not found\n");
2829 #endif
2830 	p->next = sed->next;
2831 	p->ed.ed_nexted = sed->ed.ed_nexted;
2832 	splx(s);
2833 
2834 	for (j = 0; j < nslots; j++)
2835 		--sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS];
2836 
2837 	ohci_free_std(sc, opipe->tail.td);
2838 	ohci_free_sed(sc, opipe->sed);
2839 }
2840 
2841 Static usbd_status
2842 ohci_device_setintr(ohci_softc_t *sc, struct ohci_pipe *opipe, int ival)
2843 {
2844 	int i, j, s, best;
2845 	u_int npoll, slow, shigh, nslots;
2846 	u_int bestbw, bw;
2847 	ohci_soft_ed_t *hsed, *sed = opipe->sed;
2848 
2849 	DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe));
2850 	if (ival == 0) {
2851 		printf("ohci_setintr: 0 interval\n");
2852 		return (USBD_INVAL);
2853 	}
2854 
2855 	npoll = OHCI_NO_INTRS;
2856 	while (npoll > ival)
2857 		npoll /= 2;
2858 	DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll));
2859 
2860 	/*
2861 	 * We now know which level in the tree the ED must go into.
2862 	 * Figure out which slot has most bandwidth left over.
2863 	 * Slots to examine:
2864 	 * npoll
2865 	 * 1	0
2866 	 * 2	1 2
2867 	 * 4	3 4 5 6
2868 	 * 8	7 8 9 10 11 12 13 14
2869 	 * N    (N-1) .. (N-1+N-1)
2870 	 */
2871 	slow = npoll-1;
2872 	shigh = slow + npoll;
2873 	nslots = OHCI_NO_INTRS / npoll;
2874 	for (best = i = slow, bestbw = ~0; i < shigh; i++) {
2875 		bw = 0;
2876 		for (j = 0; j < nslots; j++)
2877 			bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
2878 		if (bw < bestbw) {
2879 			best = i;
2880 			bestbw = bw;
2881 		}
2882 	}
2883 	DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n",
2884 		     best, slow, shigh, bestbw));
2885 
2886 	s = splusb();
2887 	hsed = sc->sc_eds[best];
2888 	sed->next = hsed->next;
2889 	sed->ed.ed_nexted = hsed->ed.ed_nexted;
2890 	hsed->next = sed;
2891 	hsed->ed.ed_nexted = htole32(sed->physaddr);
2892 	splx(s);
2893 
2894 	for (j = 0; j < nslots; j++)
2895 		++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
2896 	opipe->u.intr.nslots = nslots;
2897 	opipe->u.intr.pos = best;
2898 
2899 	DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe));
2900 	return (USBD_NORMAL_COMPLETION);
2901 }
2902 
2903 /***********************/
2904 
2905 usbd_status
2906 ohci_device_isoc_transfer(usbd_xfer_handle xfer)
2907 {
2908 	usbd_status err;
2909 
2910 	DPRINTFN(5,("ohci_device_isoc_transfer: xfer=%p\n", xfer));
2911 
2912 	/* Put it on our queue, */
2913 	err = usb_insert_transfer(xfer);
2914 
2915 	/* bail out on error, */
2916 	if (err && err != USBD_IN_PROGRESS)
2917 		return (err);
2918 
2919 	/* XXX should check inuse here */
2920 
2921 	/* insert into schedule, */
2922 	ohci_device_isoc_enter(xfer);
2923 
2924 	/* and start if the pipe wasn't running */
2925 	if (!err)
2926 		ohci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2927 
2928 	return (err);
2929 }
2930 
2931 void
2932 ohci_device_isoc_enter(usbd_xfer_handle xfer)
2933 {
2934 	struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2935 	usbd_device_handle dev = opipe->pipe.device;
2936 	ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2937 	ohci_soft_ed_t *sed = opipe->sed;
2938 	struct iso *iso = &opipe->u.iso;
2939 	ohci_soft_itd_t *sitd, *nsitd;
2940 	ohci_physaddr_t buf, offs, noffs, bp0;
2941 	int i, ncur, nframes;
2942 	int s;
2943 
2944 	DPRINTFN(1,("ohci_device_isoc_enter: used=%d next=%d xfer=%p "
2945 		    "nframes=%d\n",
2946 		    iso->inuse, iso->next, xfer, xfer->nframes));
2947 
2948 	if (sc->sc_dying)
2949 		return;
2950 
2951 	if (iso->next == -1) {
2952 		/* Not in use yet, schedule it a few frames ahead. */
2953 		iso->next = le32toh(sc->sc_hcca->hcca_frame_number) + 5;
2954 		DPRINTFN(2,("ohci_device_isoc_enter: start next=%d\n",
2955 			    iso->next));
2956 	}
2957 
2958 	sitd = opipe->tail.itd;
2959 	buf = DMAADDR(&xfer->dmabuf);
2960 	bp0 = OHCI_PAGE(buf);
2961 	offs = OHCI_PAGE_OFFSET(buf);
2962 	nframes = xfer->nframes;
2963 	xfer->hcpriv = sitd;
2964 	for (i = ncur = 0; i < nframes; i++, ncur++) {
2965 		noffs = offs + xfer->frlengths[i];
2966 		if (ncur == OHCI_ITD_NOFFSET ||	/* all offsets used */
2967 		    OHCI_PAGE(buf + noffs) > bp0 + OHCI_PAGE_SIZE) { /* too many page crossings */
2968 
2969 			/* Allocate next ITD */
2970 			nsitd = ohci_alloc_sitd(sc);
2971 			if (nsitd == NULL) {
2972 				/* XXX what now? */
2973 				printf("%s: isoc TD alloc failed\n",
2974 				       USBDEVNAME(sc->sc_bus.bdev));
2975 				return;
2976 			}
2977 
2978 			/* Fill current ITD */
2979 			sitd->itd.itd_flags = htole32(
2980 				OHCI_ITD_NOCC |
2981 				OHCI_ITD_SET_SF(iso->next) |
2982 				OHCI_ITD_SET_DI(6) | /* delay intr a little */
2983 				OHCI_ITD_SET_FC(ncur));
2984 			sitd->itd.itd_bp0 = htole32(bp0);
2985 			sitd->nextitd = nsitd;
2986 			sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
2987 			sitd->itd.itd_be = htole32(bp0 + offs - 1);
2988 			sitd->xfer = xfer;
2989 			sitd->flags = 0;
2990 
2991 			sitd = nsitd;
2992 			iso->next = iso->next + ncur;
2993 			bp0 = OHCI_PAGE(buf + offs);
2994 			ncur = 0;
2995 		}
2996 		sitd->itd.itd_offset[ncur] = htole16(OHCI_ITD_MK_OFFS(offs));
2997 		offs = noffs;
2998 	}
2999 	nsitd = ohci_alloc_sitd(sc);
3000 	if (nsitd == NULL) {
3001 		/* XXX what now? */
3002 		printf("%s: isoc TD alloc failed\n",
3003 		       USBDEVNAME(sc->sc_bus.bdev));
3004 		return;
3005 	}
3006 	/* Fixup last used ITD */
3007 	sitd->itd.itd_flags = htole32(
3008 		OHCI_ITD_NOCC |
3009 		OHCI_ITD_SET_SF(iso->next) |
3010 		OHCI_ITD_SET_DI(0) |
3011 		OHCI_ITD_SET_FC(ncur));
3012 	sitd->itd.itd_bp0 = htole32(bp0);
3013 	sitd->nextitd = nsitd;
3014 	sitd->itd.itd_nextitd = htole32(nsitd->physaddr);
3015 	sitd->itd.itd_be = htole32(bp0 + offs - 1);
3016 	sitd->xfer = xfer;
3017 	sitd->flags = OHCI_CALL_DONE;
3018 
3019 	iso->next = iso->next + ncur;
3020 	iso->inuse += nframes;
3021 
3022 	xfer->actlen = offs;	/* XXX pretend we did it all */
3023 
3024 	xfer->status = USBD_IN_PROGRESS;
3025 
3026 #ifdef OHCI_DEBUG
3027 	if (ohcidebug > 5) {
3028 		DPRINTF(("ohci_device_isoc_enter: frame=%d\n",
3029 			 le32toh(sc->sc_hcca->hcca_frame_number)));
3030 		ohci_dump_itds(xfer->hcpriv);
3031 		ohci_dump_ed(sed);
3032 	}
3033 #endif
3034 
3035 	s = splusb();
3036 	opipe->tail.itd = nsitd;
3037 	sed->ed.ed_tailp = htole32(nsitd->physaddr);
3038 	splx(s);
3039 
3040 #ifdef OHCI_DEBUG
3041 	if (ohcidebug > 5) {
3042 		delay(150000);
3043 		DPRINTF(("ohci_device_isoc_enter: after frame=%d\n",
3044 			 le32toh(sc->sc_hcca->hcca_frame_number)));
3045 		ohci_dump_itds(xfer->hcpriv);
3046 		ohci_dump_ed(sed);
3047 	}
3048 #endif
3049 }
3050 
3051 usbd_status
3052 ohci_device_isoc_start(usbd_xfer_handle xfer)
3053 {
3054 	struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3055 	ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3056 
3057 	DPRINTFN(5,("ohci_device_isoc_start: xfer=%p\n", xfer));
3058 
3059 	if (sc->sc_dying)
3060 		return (USBD_IOERROR);
3061 
3062 #ifdef DIAGNOSTIC
3063 	if (xfer->status != USBD_IN_PROGRESS)
3064 		printf("uhci_device_isoc_start: not in progress %p\n", xfer);
3065 #endif
3066 
3067 	/* XXX anything to do? */
3068 
3069 	return (USBD_IN_PROGRESS);
3070 }
3071 
3072 void
3073 ohci_device_isoc_abort(usbd_xfer_handle xfer)
3074 {
3075 	struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3076 	ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3077 	ohci_soft_ed_t *sed;
3078 	ohci_soft_itd_t *sitd;
3079 	int s;
3080 
3081 	s = splusb();
3082 
3083 	DPRINTFN(1,("ohci_device_isoc_abort: xfer=%p\n", xfer));
3084 
3085 	/* Transfer is already done. */
3086 	if (xfer->status != USBD_NOT_STARTED &&
3087 	    xfer->status != USBD_IN_PROGRESS) {
3088 		splx(s);
3089 		printf("ohci_device_isoc_abort: early return\n");
3090 		return;
3091 	}
3092 
3093 	/* Give xfer the requested abort code. */
3094 	xfer->status = USBD_CANCELLED;
3095 
3096 	sed = opipe->sed;
3097 	sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */
3098 
3099 	sitd = xfer->hcpriv;
3100 #ifdef DIAGNOSTIC
3101 	if (sitd == NULL) {
3102 		printf("ohci_device_isoc_abort: hcpriv==0\n");
3103 		return;
3104 	}
3105 #endif
3106 	for (; sitd->xfer == xfer; sitd = sitd->nextitd) {
3107 #ifdef DIAGNOSTIC
3108 		DPRINTFN(1,("abort sets done sitd=%p\n", sitd));
3109 		sitd->isdone = 1;
3110 #endif
3111 	}
3112 
3113 	splx(s);
3114 
3115 	usb_delay_ms(&sc->sc_bus, OHCI_ITD_NOFFSET);
3116 
3117 	s = splusb();
3118 
3119 	/* Run callback. */
3120 	usb_transfer_complete(xfer);
3121 
3122 	sed->ed.ed_headp = htole32(sitd->physaddr); /* unlink TDs */
3123 	sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */
3124 
3125 	splx(s);
3126 }
3127 
3128 void
3129 ohci_device_isoc_done(usbd_xfer_handle xfer)
3130 {
3131 	struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
3132 	ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
3133 	ohci_soft_itd_t *sitd, *nsitd;
3134 
3135 	DPRINTFN(1,("ohci_device_isoc_done: xfer=%p\n", xfer));
3136 
3137 	for (sitd = xfer->hcpriv;
3138 	     !(sitd->flags & OHCI_CALL_DONE);
3139 	     sitd = nsitd) {
3140 		nsitd = sitd->nextitd;
3141 		DPRINTFN(1,("ohci_device_isoc_done: free sitd=%p\n", sitd));
3142 		ohci_free_sitd(sc, sitd);
3143 	}
3144 	ohci_free_sitd(sc, sitd);
3145 	xfer->hcpriv = NULL;
3146 }
3147 
3148 usbd_status
3149 ohci_setup_isoc(usbd_pipe_handle pipe)
3150 {
3151 	struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3152 	ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3153 	struct iso *iso = &opipe->u.iso;
3154 	int s;
3155 
3156 	iso->next = -1;
3157 	iso->inuse = 0;
3158 
3159 	s = splusb();
3160 	ohci_add_ed(opipe->sed, sc->sc_isoc_head);
3161 	splx(s);
3162 
3163 	return (USBD_NORMAL_COMPLETION);
3164 }
3165 
3166 void
3167 ohci_device_isoc_close(usbd_pipe_handle pipe)
3168 {
3169 	struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
3170 	ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
3171 	int s;
3172 
3173 	DPRINTF(("ohci_device_isoc_close: pipe=%p\n", pipe));
3174 
3175 	s = splusb();
3176 	ohci_rem_ed(opipe->sed, sc->sc_isoc_head);
3177 	splx(s);
3178 	ohci_close_pipe(pipe, sc->sc_isoc_head);
3179 #ifdef DIAGNOSTIC
3180 	opipe->tail.itd->isdone = 1;
3181 #endif
3182 	ohci_free_sitd(sc, opipe->tail.itd);
3183 }
3184