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