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