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