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