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