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