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