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