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