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