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