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