]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/uhci.c
Updated USB kernel sources to NetBSD sources of 1998-12-09.
[FreeBSD/FreeBSD.git] / sys / dev / usb / uhci.c
1 /*      $NetBSD: uhci.c,v 1.10 1998/08/02 22:30:52 augustss Exp $       */
2
3 /*
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Author: Lennart Augustsson <augustss@carlstedt.se>
8  *         Carlstedt Research & Technology
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38
39 /*
40  * USB Universal Host Controller driver.
41  * Handles PIIX3 and PIIX4.
42  *
43  * Data sheets: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
44  *              ftp://download.intel.com/design/intarch/datashts/29056201.pdf
45  * UHCI spec: http://www.intel.com/design/usb/uhci11d.pdf
46  * USB spec: http://www.teleport.com/cgi-bin/mailmerge.cgi/~usb/cgiform.tpl
47  */
48
49 #include <dev/usb/usb_port.h>
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__)
56 #include <sys/device.h>
57 #elif defined(__FreeBSD__)
58 #include <sys/module.h>
59 #include <sys/bus.h>
60 #endif
61 #include <sys/proc.h>
62 #include <sys/queue.h>
63 #include <sys/select.h>
64
65 #include <machine/bus.h>
66
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 #include <dev/usb/usbdivar.h>
70 #include <dev/usb/usb_mem.h>
71 #include <dev/usb/usb_quirks.h>
72
73 #include <dev/usb/uhcireg.h>
74 #include <dev/usb/uhcivar.h>
75
76 #if defined(__FreeBSD__)
77 #include <machine/clock.h>
78 #include "dev/usb/queue.addendum.h"
79
80 #define delay(d)                DELAY(d)
81 #endif
82
83 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
84
85 struct uhci_pipe {
86         struct usbd_pipe pipe;
87         uhci_intr_info_t *iinfo;
88         int newtoggle;
89         /* Info needed for different pipe kinds. */
90         union {
91                 /* Control pipe */
92                 struct {
93                         uhci_soft_qh_t *sqh;
94                         usb_dma_t reqdma;
95                         usb_dma_t datadma;
96                         uhci_soft_td_t *setup, *stat, *xferend;
97                         u_int length;
98                 } ctl;
99                 /* Interrupt pipe */
100                 struct {
101                         usb_dma_t datadma;
102                         int npoll;
103                         uhci_soft_qh_t **qhs;
104                 } intr;
105                 /* Bulk pipe */
106                 struct {
107                         uhci_soft_qh_t *sqh;
108                         usb_dma_t datadma;
109                         u_int length;
110                         int isread;
111                 } bulk;
112         } u;
113 };
114
115 /* 
116  * The uhci_intr_info free list can be global since they contain
117  * no dma specific data.  The other free lists do.
118  */
119 LIST_HEAD(, uhci_intr_info) uhci_ii_free;
120
121 void            uhci_busreset __P((uhci_softc_t *));
122 void            uhci_run __P((uhci_softc_t *, int run));
123 uhci_soft_td_t *uhci_alloc_std __P((uhci_softc_t *));
124 void            uhci_free_std __P((uhci_softc_t *, uhci_soft_td_t *));
125 uhci_soft_qh_t *uhci_alloc_sqh __P((uhci_softc_t *));
126 void            uhci_free_sqh __P((uhci_softc_t *, uhci_soft_qh_t *));
127 uhci_intr_info_t *uhci_alloc_intr_info __P((uhci_softc_t *));
128 void            uhci_free_intr_info __P((uhci_intr_info_t *ii));
129 void            uhci_enter_ctl_q __P((uhci_softc_t *, uhci_soft_qh_t *,
130                                       uhci_intr_info_t *));
131 void            uhci_exit_ctl_q __P((uhci_softc_t *, uhci_soft_qh_t *));
132
133 void            uhci_free_std_chain __P((uhci_softc_t *, 
134                                          uhci_soft_td_t *, uhci_soft_td_t *));
135 usbd_status     uhci_alloc_std_chain __P((struct uhci_pipe *, uhci_softc_t *,
136                                           int, int, usb_dma_t *, 
137                                           uhci_soft_td_t **,
138                                           uhci_soft_td_t **));
139 void            uhci_timo __P((void *));
140 void            uhci_waitintr __P((uhci_softc_t *, usbd_request_handle));
141 void            uhci_check_intr __P((uhci_softc_t *, uhci_intr_info_t *));
142 void            uhci_ii_done __P((uhci_intr_info_t *, int));
143 void            uhci_timeout __P((void *));
144 void            uhci_wakeup_ctrl __P((void *, int, int, void *, int));
145 void            uhci_lock_frames __P((uhci_softc_t *));
146 void            uhci_unlock_frames __P((uhci_softc_t *));
147 void            uhci_add_ctrl __P((uhci_softc_t *, uhci_soft_qh_t *));
148 void            uhci_add_bulk __P((uhci_softc_t *, uhci_soft_qh_t *));
149 void            uhci_remove_ctrl __P((uhci_softc_t *, uhci_soft_qh_t *));
150 void            uhci_remove_bulk __P((uhci_softc_t *, uhci_soft_qh_t *));
151 int             uhci_str __P((usb_string_descriptor_t *, int, char *));
152
153 void            uhci_device_close __P((struct uhci_pipe *));
154
155 void            uhci_wakeup_cb __P((usbd_request_handle reqh));
156
157 usbd_status     uhci_device_ctrl_transfer __P((usbd_request_handle));
158 void            uhci_device_ctrl_abort __P((usbd_request_handle));
159 void            uhci_device_ctrl_close __P((usbd_pipe_handle));
160 usbd_status     uhci_device_intr_transfer __P((usbd_request_handle));
161 void            uhci_device_intr_abort __P((usbd_request_handle));
162 void            uhci_device_intr_close __P((usbd_pipe_handle));
163 usbd_status     uhci_device_bulk_transfer __P((usbd_request_handle));
164 void            uhci_device_bulk_abort __P((usbd_request_handle));
165 void            uhci_device_bulk_close __P((usbd_pipe_handle));
166
167 usbd_status     uhci_root_ctrl_transfer __P((usbd_request_handle));
168 void            uhci_root_ctrl_abort __P((usbd_request_handle));
169 void            uhci_root_ctrl_close __P((usbd_pipe_handle));
170 usbd_status     uhci_root_intr_transfer __P((usbd_request_handle));
171 void            uhci_root_intr_abort __P((usbd_request_handle));
172 void            uhci_root_intr_close __P((usbd_pipe_handle));
173
174 usbd_status     uhci_open __P((usbd_pipe_handle));
175 void            uhci_poll __P((struct usbd_bus *));
176
177 usbd_status     uhci_device_request __P((usbd_request_handle reqh));
178 void            uhci_ctrl_done __P((uhci_intr_info_t *ii));
179 void            uhci_bulk_done __P((uhci_intr_info_t *ii));
180
181 void            uhci_add_intr __P((uhci_softc_t *, int, uhci_soft_qh_t *));
182 void            uhci_remove_intr __P((uhci_softc_t *, int, uhci_soft_qh_t *));
183 usbd_status     uhci_device_setintr __P((uhci_softc_t *sc, 
184                                          struct uhci_pipe *pipe, int ival));
185 void            uhci_intr_done __P((uhci_intr_info_t *ii));
186
187 #ifdef USB_DEBUG
188 static void     uhci_dumpregs __P((uhci_softc_t *));
189 void            uhci_dump_tds __P((uhci_soft_td_t *));
190 void            uhci_dump_qh __P((uhci_soft_qh_t *));
191 void            uhci_dump __P((void));
192 void            uhci_dump_td __P((uhci_soft_td_t *));
193 #endif
194
195 #if defined(__NetBSD__)
196 #define UWRITE2(sc, r, x) bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x))
197 #define UWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
198 #define UREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
199 #define UREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
200 #elif defined(__FreeBSD__)
201 #define UWRITE2(sc,r,x) outw((sc)->sc_iobase + (r), (x))
202 #define UWRITE4(sc,r,x) outl((sc)->sc_iobase + (r), (x))
203 #define UREAD2(sc,r)    inw((sc)->sc_iobase + (r))
204 #define UREAD4(sc,r)    inl((sc)->sc_iobase + (r))
205 #endif
206
207 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
208 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
209
210 #define UHCI_RESET_TIMEOUT 100  /* reset timeout */
211 #define UHCI_CTRL_TIMEOUT 500   /* control transaction timeout */
212 #define UHCI_ISO_DELAY 50       /* delay of start of iso */
213
214 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
215
216 #define UHCI_INTR_ENDPT 1
217
218 struct usbd_methods uhci_root_ctrl_methods = {  
219         uhci_root_ctrl_transfer,
220         uhci_root_ctrl_abort,
221         uhci_root_ctrl_close,
222         0,
223 };
224
225 struct usbd_methods uhci_root_intr_methods = {  
226         uhci_root_intr_transfer,
227         uhci_root_intr_abort,
228         uhci_root_intr_close,
229         0,
230 };
231
232 struct usbd_methods uhci_device_ctrl_methods = {
233         uhci_device_ctrl_transfer,
234         uhci_device_ctrl_abort,
235         uhci_device_ctrl_close,
236         0,
237 };
238
239 struct usbd_methods uhci_device_intr_methods = {
240         uhci_device_intr_transfer,
241         uhci_device_intr_abort,
242         uhci_device_intr_close,
243         0,
244 };
245
246 struct usbd_methods uhci_device_bulk_methods = {
247         uhci_device_bulk_transfer,
248         uhci_device_bulk_abort,
249         uhci_device_bulk_close,
250         0,
251 };
252
253 void
254 uhci_busreset(sc)
255         uhci_softc_t *sc;
256 {
257         UHCICMD(sc, UHCI_CMD_GRESET);   /* global reset */
258         usbd_delay_ms(&sc->sc_bus, USB_RESET_DELAY); /* wait at least 10ms */
259         UHCICMD(sc, 0);                 /* do nothing */
260 }
261
262 usbd_status
263 uhci_init(sc)
264         uhci_softc_t *sc;
265 {
266         usbd_status r;
267         int i, j;
268         uhci_soft_qh_t *csqh, *bsqh, *sqh;
269         uhci_soft_td_t *std;
270         usb_dma_t dma;
271         static int uhci_global_init_done = 0;
272
273         DPRINTFN(1,("uhci_init: start\n"));
274
275         if (!uhci_global_init_done) {
276                 uhci_global_init_done = 1;
277                 LIST_INIT(&uhci_ii_free);
278         }
279
280 #if defined(USB_DEBUG)
281         if (uhcidebug > 2)
282                 uhci_dumpregs(sc);
283 #endif
284
285         uhci_run(sc, 0);                        /* stop the controller */
286         UWRITE2(sc, UHCI_INTR, 0);              /* disable interrupts */
287
288         /* Allocate and initialize real frame array. */
289         r = usb_allocmem(sc->sc_dmatag, 
290                          UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
291                          UHCI_FRAMELIST_ALIGN, &dma);
292         if (r != USBD_NORMAL_COMPLETION)
293                 return (r);
294         sc->sc_pframes = KERNADDR(&dma);
295         UWRITE2(sc, UHCI_FRNUM, 0);             /* set frame number to 0 */
296         UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&dma)); /* set frame list */
297
298         uhci_busreset(sc);
299
300         /* Allocate the dummy QH where bulk traffic will be queued. */
301         bsqh = uhci_alloc_sqh(sc);
302         if (!bsqh)
303                 return (USBD_NOMEM);
304         bsqh->qh->qh_hlink = UHCI_PTR_T;        /* end of QH chain */
305         bsqh->qh->qh_elink = UHCI_PTR_T;
306         sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
307
308         /* Allocate the dummy QH where control traffic will be queued. */
309         csqh = uhci_alloc_sqh(sc);
310         if (!csqh)
311                 return (USBD_NOMEM);
312         csqh->qh->hlink = bsqh;
313         csqh->qh->qh_hlink = bsqh->physaddr | UHCI_PTR_Q;
314         csqh->qh->qh_elink = UHCI_PTR_T;
315         sc->sc_ctl_start = sc->sc_ctl_end = csqh;
316
317         /* 
318          * Make all (virtual) frame list pointers point to the interrupt
319          * queue heads and the interrupt queue heads at the control
320          * queue head and point the physical frame list to the virtual.
321          */
322         for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
323                 std = uhci_alloc_std(sc);
324                 sqh = uhci_alloc_sqh(sc);
325                 if (!std || !sqh)
326                         return (USBD_NOMEM);
327                 std->td->link.sqh = sqh;
328                 std->td->td_link = sqh->physaddr | UHCI_PTR_Q;
329                 std->td->td_status = UHCI_TD_IOS;       /* iso, inactive */
330                 std->td->td_token = 0;
331                 std->td->td_buffer = 0;
332                 sqh->qh->hlink = csqh;
333                 sqh->qh->qh_hlink = csqh->physaddr | UHCI_PTR_Q;
334                 sqh->qh->elink = 0;
335                 sqh->qh->qh_elink = UHCI_PTR_T;
336                 sc->sc_vframes[i].htd = std;
337                 sc->sc_vframes[i].etd = std;
338                 sc->sc_vframes[i].hqh = sqh;
339                 sc->sc_vframes[i].eqh = sqh;
340                 for (j = i; 
341                      j < UHCI_FRAMELIST_COUNT; 
342                      j += UHCI_VFRAMELIST_COUNT)
343                         sc->sc_pframes[j] = std->physaddr;
344         }
345
346         LIST_INIT(&sc->sc_intrhead);
347
348         /* Set up the bus struct. */
349         sc->sc_bus.open_pipe = uhci_open;
350         sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
351         sc->sc_bus.do_poll = uhci_poll;
352
353         DPRINTFN(1,("uhci_init: enabling\n"));
354         UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE | 
355                 UHCI_INTR_IOCE | UHCI_INTR_SPIE);       /* enable interrupts */
356
357         uhci_run(sc, 1);                        /* and here we go... */
358         return (USBD_NORMAL_COMPLETION);
359 }
360
361 #ifdef USB_DEBUG
362 static void
363 uhci_dumpregs(sc)
364         uhci_softc_t *sc;
365 {
366         DEVICE_MSG(sc->sc_bus.bdev,("regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
367                UREAD2(sc, UHCI_CMD),
368                UREAD2(sc, UHCI_STS),
369                UREAD2(sc, UHCI_INTR),
370                UREAD2(sc, UHCI_FRNUM),
371                UREAD2(sc, UHCI_FLBASEADDR),
372                UREAD2(sc, UHCI_SOF),
373                UREAD2(sc, UHCI_PORTSC1),
374                UREAD2(sc, UHCI_PORTSC2)));
375 }
376
377 int uhci_longtd = 1;
378
379 void
380 uhci_dump_td(p)
381         uhci_soft_td_t *p;
382 {
383         printf("TD(%p) at %08lx = 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n",
384                p, (long)p->physaddr,
385                (long)p->td->td_link,
386                (long)p->td->td_status,
387                (long)p->td->td_token,
388                (long)p->td->td_buffer);
389         if (uhci_longtd)
390                 printf("  %b %b,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,D=%d,maxlen=%d\n",
391                        (long)p->td->td_link,
392                        "\20\1T\2Q\3VF",
393                        (long)p->td->td_status,
394                        "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
395                        UHCI_TD_GET_ERRCNT(p->td->td_status),
396                        UHCI_TD_GET_ACTLEN(p->td->td_status),
397                        UHCI_TD_GET_PID(p->td->td_token),
398                        UHCI_TD_GET_DEVADDR(p->td->td_token),
399                        UHCI_TD_GET_ENDPT(p->td->td_token),
400                        UHCI_TD_GET_DT(p->td->td_token),
401                        UHCI_TD_GET_MAXLEN(p->td->td_token));
402 }
403
404 void
405 uhci_dump_qh(p)
406         uhci_soft_qh_t *p;
407 {
408         printf("QH(%p) at %08x: hlink=%08x elink=%08x\n", p, (int)p->physaddr,
409                p->qh->qh_hlink, p->qh->qh_elink);
410 }
411
412
413 #if 0
414 void
415 uhci_dump()
416 {
417         uhci_softc_t *sc = uhci;
418
419         uhci_dumpregs(sc);
420         printf("intrs=%d\n", sc->sc_intrs);
421         printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);
422         uhci_dump_qh(sc->sc_ctl_start->qh->hlink);
423 }
424 #endif
425
426 void
427 uhci_dump_tds(std)
428         uhci_soft_td_t *std;
429 {
430         uhci_soft_td_t *p;
431
432         for(p = std; p; p = p->td->link.std)
433                 uhci_dump_td(p);
434 }
435 #endif
436
437 /*
438  * This routine is executed periodically and simulates interrupts
439  * from the root controller interrupt pipe for port status change.
440  */
441 void
442 uhci_timo(addr)
443         void *addr;
444 {
445         usbd_request_handle reqh = addr;
446         usbd_pipe_handle pipe = reqh->pipe;
447         uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
448         struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
449         int s;
450         u_char *p;
451
452         DPRINTFN(15, ("uhci_timo\n"));
453
454         p = KERNADDR(&upipe->u.intr.datadma);
455         p[0] = 0;
456         if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
457                 p[0] |= 1<<1;
458         if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
459                 p[0] |= 1<<2;
460         if (p[0] != 0) {
461                 reqh->actlen = 1;
462                 reqh->status = USBD_NORMAL_COMPLETION;
463                 s = splusb();
464                 reqh->xfercb(reqh);
465                 splx(s);
466         }
467         if (reqh->pipe->intrreqh == reqh) {
468 #if defined(__NetBSD__)
469                 timeout(uhci_timo, reqh, sc->sc_ival);
470 #elif defined(__FreeBSD__)
471                 /* To avoid race conditions we first initialise the struct
472                  * before we use it. The timeout might happen between the
473                  * setting of the timeout and the setting of timo_handle
474                  */
475                 callout_handle_init(&reqh->timo_handle);
476                 reqh->timo_handle = timeout(uhci_timo, reqh, sc->sc_ival);
477 #endif
478         } else {
479                 usb_freemem(sc->sc_dmatag, &upipe->u.intr.datadma);
480         }
481 }
482
483
484 void
485 uhci_lock_frames(sc)
486         uhci_softc_t *sc;
487 {
488         int s = splusb();
489         while (sc->sc_vflock) {
490                 sc->sc_vflock |= UHCI_WANT_LOCK;
491                 tsleep(&sc->sc_vflock, PRIBIO, "uhcqhl", 0);
492         }
493         sc->sc_vflock = UHCI_HAS_LOCK;
494         splx(s);
495 }
496
497 void
498 uhci_unlock_frames(sc)
499         uhci_softc_t *sc;
500 {
501         int s = splusb();
502         sc->sc_vflock &= ~UHCI_HAS_LOCK;
503         if (sc->sc_vflock & UHCI_WANT_LOCK)
504                 wakeup(&sc->sc_vflock);
505         splx(s);
506 }
507
508 /*
509  * Allocate an interrupt information struct.  A free list is kept
510  * for fast allocation.
511  */
512 uhci_intr_info_t *
513 uhci_alloc_intr_info(sc)
514         uhci_softc_t *sc;
515 {
516         uhci_intr_info_t *ii;
517
518         ii = LIST_FIRST(&uhci_ii_free);
519         if (ii)
520                 LIST_REMOVE(ii, list);
521         else {
522                 ii = malloc(sizeof(uhci_intr_info_t), M_USBDEV, M_NOWAIT);
523         }
524         ii->sc = sc;
525         return ii;
526 }
527
528 void
529 uhci_free_intr_info(ii)
530         uhci_intr_info_t *ii;
531 {
532         LIST_INSERT_HEAD(&uhci_ii_free, ii, list); /* and put on free list */
533 }
534
535 /* Add control QH, called at splusb(). */
536 void
537 uhci_add_ctrl(sc, sqh)
538         uhci_softc_t *sc;
539         uhci_soft_qh_t *sqh;
540 {
541         uhci_qh_t *eqh;
542
543         DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
544         eqh = sc->sc_ctl_end->qh;
545         sqh->qh->hlink     = eqh->hlink;
546         sqh->qh->qh_hlink  = eqh->qh_hlink;
547         eqh->hlink         = sqh;
548         eqh->qh_hlink      = sqh->physaddr | UHCI_PTR_Q;
549         sc->sc_ctl_end = sqh;
550 }
551
552 /* Remove control QH, called at splusb(). */
553 void
554 uhci_remove_ctrl(sc, sqh)
555         uhci_softc_t *sc;
556         uhci_soft_qh_t *sqh;
557 {
558         uhci_soft_qh_t *pqh;
559
560         DPRINTFN(10, ("uhci_remove_ctrl: sqh=%p\n", sqh));
561         for (pqh = sc->sc_ctl_start; pqh->qh->hlink != sqh; pqh=pqh->qh->hlink)
562 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)           
563                 if (pqh->qh->qh_hlink & UHCI_PTR_T) {
564                         printf("uhci_remove_ctrl: QH not found\n");
565                         return;
566                 }
567 #else
568                 ;
569 #endif
570         pqh->qh->hlink    = sqh->qh->hlink;
571         pqh->qh->qh_hlink = sqh->qh->qh_hlink;
572         if (sc->sc_ctl_end == sqh)
573                 sc->sc_ctl_end = pqh;
574 }
575
576 /* Add bulk QH, called at splusb(). */
577 void
578 uhci_add_bulk(sc, sqh)
579         uhci_softc_t *sc;
580         uhci_soft_qh_t *sqh;
581 {
582         uhci_qh_t *eqh;
583
584         DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
585         eqh = sc->sc_bulk_end->qh;
586         sqh->qh->hlink     = eqh->hlink;
587         sqh->qh->qh_hlink  = eqh->qh_hlink;
588         eqh->hlink         = sqh;
589         eqh->qh_hlink      = sqh->physaddr | UHCI_PTR_Q;
590         sc->sc_bulk_end = sqh;
591 }
592
593 /* Remove bulk QH, called at splusb(). */
594 void
595 uhci_remove_bulk(sc, sqh)
596         uhci_softc_t *sc;
597         uhci_soft_qh_t *sqh;
598 {
599         uhci_soft_qh_t *pqh;
600
601         DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
602         for (pqh = sc->sc_bulk_start; pqh->qh->hlink != sqh; pqh=pqh->qh->hlink)
603 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)           
604                 if (pqh->qh->qh_hlink & UHCI_PTR_T) {
605                         printf("uhci_remove_bulk: QH not found\n");
606                         return;
607                 }
608 #else
609                 ;
610 #endif
611         pqh->qh->hlink    = sqh->qh->hlink;
612         pqh->qh->qh_hlink = sqh->qh->qh_hlink;
613         if (sc->sc_bulk_end == sqh)
614                 sc->sc_bulk_end = pqh;
615 }
616
617 int
618 uhci_intr(p)
619         void *p;
620 {
621         uhci_softc_t *sc = p;
622         int status, ret;
623         uhci_intr_info_t *ii;
624
625         sc->sc_intrs++;
626 #if defined(USB_DEBUG)
627         if (uhcidebug > 9) {
628                 DEVICE_MSG(sc->sc_bus.bdev, ("uhci_intr %p\n", sc));
629                 uhci_dumpregs(sc);
630         }
631 #endif
632         status = UREAD2(sc, UHCI_STS);
633         ret = 0;
634         if (status & UHCI_STS_USBINT) {
635                 UWRITE2(sc, UHCI_STS, UHCI_STS_USBINT); /* acknowledge */
636                 ret = 1;
637         }
638         if (status & UHCI_STS_USBEI) {
639                 UWRITE2(sc, UHCI_STS, UHCI_STS_USBEI); /* acknowledge */
640                 ret = 1;
641         }
642         if (status & UHCI_STS_RD) {
643                 UWRITE2(sc, UHCI_STS, UHCI_STS_RD); /* acknowledge */
644                 DEVICE_MSG(sc->sc_bus.bdev, ("resume detect\n"));
645                 ret = 1;
646         }
647         if (status & UHCI_STS_HSE) {
648                 UWRITE2(sc, UHCI_STS, UHCI_STS_HSE); /* acknowledge */
649                 DEVICE_MSG(sc->sc_bus.bdev, ("Host System Error\n"));
650                 ret = 1;
651         }
652         if (status & UHCI_STS_HCPE) {
653                 UWRITE2(sc, UHCI_STS, UHCI_STS_HCPE); /* acknowledge */
654                 DEVICE_MSG(sc->sc_bus.bdev, ("Host System Error\n"));
655                 ret = 1;
656         }
657         if (status & UHCI_STS_HCH)
658                 DEVICE_ERROR(sc->sc_bus.bdev, ("controller halted\n"));
659         if (!ret)
660                 return 0;
661
662         /*
663          * Interrupts on UHCI really suck.  When the host controller
664          * interrupts because a transfer is completed there is no
665          * way of knowing which transfer it was.  You can scan down
666          * the TDs and QHs of the previous frame to limit the search,
667          * but that assumes that the interrupt was not delayed by more
668          * than 1 ms, which may not always be true (e.g. after debug
669          * output on a slow console).
670          * We scan all interrupt descriptors to see if any have
671          * completed.
672          */
673         for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
674                 uhci_check_intr(sc, ii);
675
676         DPRINTFN(10, ("uhci_intr: exit\n"));
677         return 1;
678 }
679
680 /* Check for an interrupt. */
681 void
682 uhci_check_intr(sc, ii)
683         uhci_softc_t *sc;
684         uhci_intr_info_t *ii;
685 {
686         struct uhci_pipe *upipe;
687         uhci_soft_td_t *std, *lstd;
688
689         DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
690 #ifdef DIAGNOSTIC
691         if (!ii) {
692                 printf("uhci_check_intr: no ii? %p\n", ii);
693                 return;
694         }
695 #endif
696         if (!ii->stdstart)
697                 return;
698         lstd = ii->stdend;
699 #ifdef DIAGNOSTIC
700         if (!lstd) {
701                 printf("uhci_check_intr: std==0\n");
702                 return;
703         }
704 #endif
705         /* If the last TD is still active the whole transfer probably is. */
706         if (lstd->td->td_status & UHCI_TD_ACTIVE) {
707                 DPRINTFN(15, ("uhci_check_intr: active ii=%p\n", ii));
708                 for (std = ii->stdstart; std != lstd; std = std->td->link.std)
709                         if (std->td->td_status & UHCI_TD_STALLED)
710                                 goto done;
711                 DPRINTFN(15, ("uhci_check_intr: ii=%p still active\n", ii));
712                 return;
713         }
714  done:
715         upipe = (struct uhci_pipe *)ii->reqh->pipe;
716         upipe->pipe.endpoint->toggle = upipe->newtoggle;
717         uhci_ii_done(ii, 0);
718 #if defined(__NetBSD__)
719         untimeout(uhci_timeout, ii);
720 #elif defined(__FreeBSD__)
721         untimeout(uhci_timeout, ii, ii->timeout_handle);
722 #endif
723 }
724
725 void
726 uhci_ii_done(ii, timo)
727         uhci_intr_info_t *ii;
728         int timo;
729 {
730         usbd_request_handle reqh = ii->reqh;
731         uhci_soft_td_t *std;
732         u_int32_t tst;
733         int len, status;
734
735         DPRINTFN(10, ("uhci_ii_done: ii=%p ready %d\n", ii, timo));
736
737 #ifdef DIAGNOSTIC
738         {
739                 int s = splhigh();
740                 if (ii->isdone) {
741                         printf("uhci_ii_done: is done!\n");
742                         splx(s);
743                         return;
744                 }
745                 ii->isdone = 1;
746                 splx(s);
747         }
748 #endif
749
750         /* The transfer is done, compute length and status. */
751         for (len = status = 0, std = ii->stdstart; 
752              std != 0; 
753              std = std->td->link.std) {
754                 tst = std->td->td_status;
755                 status |= tst;
756 #ifdef USB_DEBUG
757                 if ((tst & UHCI_TD_ERROR) && uhcidebug>10) {
758                         printf("uhci_ii_done: intr error TD:\n");
759                         uhci_dump_td(std);
760                 }
761 #endif
762                 if (UHCI_TD_GET_PID(std->td->td_token) != UHCI_TD_PID_SETUP)
763                         len += UHCI_TD_GET_ACTLEN(tst);
764         }
765         status &= UHCI_TD_ERROR;
766         DPRINTFN(10, ("uhci_ii_done: len=%d\n", len));
767         if (status != 0) {
768                 DPRINTFN(10,
769                          ("uhci_ii_done: error, status 0x%b\n", (long)status, 
770                           "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27STALLED\30ACTIVE"));
771                 if (status == UHCI_TD_STALLED)
772                         reqh->status = USBD_STALLED;
773                 else
774                         reqh->status = USBD_IOERROR; /* more info XXX */
775                 reqh->actlen = 0;
776         } else {
777                 reqh->status = USBD_NORMAL_COMPLETION;
778                 reqh->actlen = len;
779         }
780         if (timo) {
781                 /* We got a timeout.  Make sure transaction is not active. */
782                 reqh->status = USBD_TIMEOUT;
783                 for (std = ii->stdstart; std != 0; std = std->td->link.std)
784                         std->td->td_status &= ~UHCI_TD_ACTIVE;
785                 /* XXX should we wait 1 ms */
786         }
787         DPRINTFN(5, ("uhci_ii_done: calling handler ii=%p\n", ii));
788
789         switch (reqh->pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
790         case UE_CONTROL:
791                 uhci_ctrl_done(ii);
792                 break;
793         case UE_ISOCHRONOUS:
794                 printf("uhci_ii_done: ISO??\n");
795                 break;
796         case UE_BULK:
797                 uhci_bulk_done(ii);
798                 break;
799         case UE_INTERRUPT:
800                 uhci_intr_done(ii);
801                 break;
802         }
803
804         /* And finally execute callback. */
805         reqh->xfercb(reqh);
806 }
807
808 void
809 uhci_timeout(addr)
810         void *addr;
811 {
812         uhci_intr_info_t *ii = addr;
813         int s;
814
815         DPRINTF(("uhci_timeout: ii=%p\n", ii));
816         s = splusb();
817         uhci_ii_done(ii, 1);
818         splx(s);
819 }
820
821 /*
822  * Wait here until controller claims to have an interrupt.
823  * Then call uhci_intr and return.  Use timeout to avoid waiting
824  * too long.
825  */
826 void
827 uhci_waitintr(sc, reqh)
828         uhci_softc_t *sc;
829         usbd_request_handle reqh;
830 {
831         int timo = reqh->timeout;
832         int usecs;
833         int hzs;
834
835         DPRINTFN(10,("uhci_waitintr: timout = %ds\n", timo));
836
837         reqh->status = USBD_IN_PROGRESS;
838         for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 100000) {
839                 /* NWH replaced by usbd_delay_ms
840                 delay(1000);
841                    NWH and descreased frequency from 1ms to 100ms, see also usecs -=...
842                  */
843                 usbd_delay_ms(&(sc->sc_bus), 100);
844                 /* NWH disabled 
845                 DPRINTFN(10,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
846                  */
847                 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
848                         uhci_intr(sc);
849                         if (reqh->status != USBD_IN_PROGRESS)
850                                 return;
851                 }
852         }
853         reqh->status = USBD_TIMEOUT;
854         reqh->xfercb(reqh);
855 }
856
857 void
858 uhci_poll(bus)
859         struct usbd_bus *bus;
860 {
861         uhci_softc_t *sc = (uhci_softc_t *)bus;
862
863         if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
864                 uhci_intr(sc);
865 }
866
867 #if 0
868 void
869 uhci_reset(p)
870         void *p;
871 {
872         uhci_softc_t *sc = p;
873         int n;
874
875         UHCICMD(sc, UHCI_CMD_HCRESET);
876         /* The reset bit goes low when the controller is done. */
877         for (n = 0; n < UHCI_RESET_TIMEOUT && 
878                     (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
879                 delay(100);
880         if (n >= UHCI_RESET_TIMEOUT)
881                 DEVICE_ERROR(sc->sc_bus.bdev, ("controller did not reset\n"));
882 }
883 #endif
884
885 void
886 uhci_run(sc, run)
887         uhci_softc_t *sc;
888         int run;
889 {
890         int s, n, running;
891
892         run = (run? UHCI_STS_HCH:0);
893         s = splusb();           /* XXX really? */
894         running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
895         if (run == running) {
896                 splx(s);
897                 return;
898         }
899         UWRITE2(sc, UHCI_CMD, run ? UHCI_CMD_RS : 0);
900         for(n = 0; n < 100; n++) {
901                 /* XXX NWH should this not be a delay of 2ms (>1024usecs)
902                  * followed by one check? This fails in case a large transfer
903                  * is going on on a fast processor.
904                  */
905                 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
906                 /* return when we've entered the state we want */
907                 if (run == running) {
908                         splx(s);
909                         return;
910                 }
911         }
912         splx(s);
913         DEVICE_ERROR(sc->sc_bus.bdev, ("cannot %s\n", (run ? "start" : "stop")));
914 }
915
916 /*
917  * Memory management routines.
918  *  uhci_alloc_std allocates TDs
919  *  uhci_alloc_sqh allocates QHs
920  * These two routines do their own free list management,
921  * partly for speed, partly because allocating DMAable memory
922  * has page size granularaity so much memory would be wasted if
923  * only one TD/QH (32 bytes) was placed in each alloacted chunk.
924  */
925
926 uhci_soft_td_t *
927 uhci_alloc_std(sc)
928         uhci_softc_t *sc;
929 {
930         uhci_soft_td_t *std;
931         usbd_status r;
932         int i;
933         usb_dma_t dma;
934
935         if (!sc->sc_freetds) {
936                 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
937                 std = malloc(sizeof(uhci_soft_td_t) * UHCI_TD_CHUNK, 
938                              M_USBDEV, M_NOWAIT);
939                 if (!std)
940                         return 0;
941                 r = usb_allocmem(sc->sc_dmatag, UHCI_TD_SIZE * UHCI_TD_CHUNK,
942                                  UHCI_TD_ALIGN, &dma);
943                 if (r != USBD_NORMAL_COMPLETION) {
944                         free(std, M_USBDEV);
945                         return 0;
946                 }
947                 for(i = 0; i < UHCI_TD_CHUNK; i++, std++) {
948                         std->physaddr = DMAADDR(&dma) + 
949                                 i * UHCI_TD_SIZE;
950                         std->td = (uhci_td_t *)
951                                 ((char *)KERNADDR(&dma) + i * UHCI_TD_SIZE);
952                         std->td->link.std = sc->sc_freetds;
953                         sc->sc_freetds = std;
954                 }
955         }
956         std = sc->sc_freetds;
957         sc->sc_freetds = std->td->link.std;
958         memset(std->td, 0, UHCI_TD_SIZE);
959         return std;
960 }
961
962 void
963 uhci_free_std(sc, std)
964         uhci_softc_t *sc;
965         uhci_soft_td_t *std;
966 {
967 #ifdef DIAGNOSTIC
968 #define TD_IS_FREE 0x12345678
969         if (std->td->td_token == TD_IS_FREE) {
970                 printf("uhci_free_std: freeing free TD %p\n", std);
971                 return;
972         }
973         std->td->td_token = TD_IS_FREE;
974 #endif
975         std->td->link.std = sc->sc_freetds;
976         sc->sc_freetds = std;
977 }
978
979 uhci_soft_qh_t *
980 uhci_alloc_sqh(sc)
981         uhci_softc_t *sc;
982 {
983         uhci_soft_qh_t *sqh;
984         usbd_status r;
985         int i, offs;
986         usb_dma_t dma;
987
988         if (!sc->sc_freeqhs) {
989                 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
990                 sqh = malloc(sizeof(uhci_soft_qh_t) * UHCI_QH_CHUNK, 
991                              M_USBDEV, M_NOWAIT);
992                 if (!sqh)
993                         return 0;
994                 r = usb_allocmem(sc->sc_dmatag, UHCI_QH_SIZE * UHCI_QH_CHUNK,
995                                  UHCI_QH_ALIGN, &dma);
996                 if (r != USBD_NORMAL_COMPLETION) {
997                         free(sqh, M_USBDEV);
998                         return 0;
999                 }
1000                 for(i = 0; i < UHCI_QH_CHUNK; i++, sqh++) {
1001                         offs = i * UHCI_QH_SIZE;
1002                         sqh->physaddr = DMAADDR(&dma) + offs;
1003                         sqh->qh = (uhci_qh_t *)
1004                                         ((char *)KERNADDR(&dma) + offs);
1005                         sqh->qh->hlink = sc->sc_freeqhs;
1006                         sc->sc_freeqhs = sqh;
1007                 }
1008         }
1009         sqh = sc->sc_freeqhs;
1010         sc->sc_freeqhs = sqh->qh->hlink;
1011         memset(sqh->qh, 0, UHCI_QH_SIZE);
1012         return sqh;
1013 }
1014
1015 void
1016 uhci_free_sqh(sc, sqh)
1017         uhci_softc_t *sc;
1018         uhci_soft_qh_t *sqh;
1019 {
1020         sqh->qh->hlink = sc->sc_freeqhs;
1021         sc->sc_freeqhs = sqh;
1022 }
1023
1024 /* 
1025  * Enter a list of transfers onto a control queue.
1026  * Called at splusb() 
1027  */
1028 void
1029 uhci_enter_ctl_q(sc, sqh, ii)
1030         uhci_softc_t *sc;
1031         uhci_soft_qh_t *sqh;
1032         uhci_intr_info_t *ii;
1033 {
1034         DPRINTFN(5, ("uhci_enter_ctl_q: sqh=%p\n", sqh));
1035
1036 }
1037
1038 void
1039 uhci_free_std_chain(sc, std, stdend)
1040         uhci_softc_t *sc;
1041         uhci_soft_td_t *std;
1042         uhci_soft_td_t *stdend;
1043 {
1044         uhci_soft_td_t *p;
1045
1046         for (; std != stdend; std = p) {
1047                 p = std->td->link.std;
1048                 uhci_free_std(sc, std);
1049         }
1050 }
1051
1052 usbd_status
1053 uhci_alloc_std_chain(upipe, sc, len, rd, dma, sp, ep)
1054         struct uhci_pipe *upipe;
1055         uhci_softc_t *sc;
1056         int len, rd;
1057         usb_dma_t *dma;
1058         uhci_soft_td_t **sp, **ep;
1059 {
1060         uhci_soft_td_t *p, *lastp;
1061         uhci_physaddr_t lastlink;
1062         u_int32_t ls;
1063         int i, ntd, l, tog, maxp;
1064         int addr = upipe->pipe.device->address;
1065         int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1066
1067         DPRINTFN(15, ("uhci_alloc_std_chain: len=%d\n", len));
1068         if (len == 0) {
1069                 *sp = *ep = 0;
1070                 printf("uhci_alloc_std_chain: len=0\n");
1071                 return (USBD_NORMAL_COMPLETION);
1072         }
1073         ls = upipe->pipe.device->lowspeed ? UHCI_TD_LS : 0;
1074         maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1075         if (maxp == 0) {
1076                 printf("uhci_alloc_std_chain: maxp=0\n");
1077                 return (USBD_INVAL);
1078         }
1079         ntd = (len + maxp - 1) / maxp;
1080         tog = upipe->pipe.endpoint->toggle;
1081         if (ntd % 2 == 0)
1082                 tog ^= 1;
1083         upipe->newtoggle = tog ^ 1;
1084         lastp = 0;
1085         lastlink = UHCI_PTR_T;
1086         ntd--;
1087         for (i = ntd; i >= 0; i--) {
1088                 p = uhci_alloc_std(sc);
1089                 if (!p) {
1090                         uhci_free_std_chain(sc, lastp, 0);
1091                         return (USBD_NOMEM);
1092                 }
1093                 p->td->link.std = lastp;
1094                 p->td->td_link = lastlink;
1095                 lastp = p;
1096                 lastlink = p->physaddr;
1097                 p->td->td_status = UHCI_TD_SET_ERRCNT(2) | ls | UHCI_TD_ACTIVE;
1098                 if (i == ntd) {
1099                         /* last TD */
1100                         l = len % maxp;
1101                         if (l == 0) l = maxp;
1102                         *ep = p;
1103                 } else
1104                         l = maxp;
1105                 p->td->td_token = 
1106                     rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1107                          UHCI_TD_OUT(l, endpt, addr, tog);
1108                 p->td->td_buffer = DMAADDR(dma) + i * maxp;
1109                 tog ^= 1;
1110         }
1111         *sp = lastp;
1112         /*upipe->pipe.endpoint->toggle = tog;*/
1113         DPRINTFN(10, ("uhci_alloc_std_chain: oldtog=%d newtog=%d\n", 
1114                       upipe->pipe.endpoint->toggle, upipe->newtoggle));
1115         return (USBD_NORMAL_COMPLETION);
1116 }
1117
1118 usbd_status
1119 uhci_device_bulk_transfer(reqh)
1120         usbd_request_handle reqh;
1121 {
1122         struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1123         usbd_device_handle dev = upipe->pipe.device;
1124         uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1125         uhci_intr_info_t *ii = upipe->iinfo;
1126         uhci_soft_td_t *xfer, *xferend;
1127         uhci_soft_qh_t *sqh;
1128         usb_dma_t *dmap;
1129         usbd_status r;
1130         int len, isread;
1131         int s;
1132
1133         DPRINTFN(3, ("uhci_device_bulk_transfer: reqh=%p buf=%p len=%d flags=%d\n",
1134                      reqh, reqh->buffer, reqh->length, reqh->flags));
1135
1136         if (reqh->isreq)
1137                 panic("uhci_device_bulk_transfer: a request\n");
1138
1139         len = reqh->length;
1140         dmap = &upipe->u.bulk.datadma;
1141         isread = reqh->pipe->endpoint->edesc->bEndpointAddress & UE_IN;
1142         sqh = upipe->u.bulk.sqh;
1143
1144         upipe->u.bulk.isread = isread;
1145         upipe->u.bulk.length = len;
1146
1147         r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
1148         if (r != USBD_NORMAL_COMPLETION)
1149                 goto ret1;
1150         r = uhci_alloc_std_chain(upipe, sc, len, isread, 
1151                                  dmap, &xfer, &xferend);
1152         if (r != USBD_NORMAL_COMPLETION)
1153                 goto ret2;
1154         xferend->td->td_status |= UHCI_TD_IOC;
1155
1156         if (!isread && len != 0)
1157                 memcpy(KERNADDR(dmap), reqh->buffer, len);
1158
1159 #ifdef USB_DEBUG
1160         if (uhcidebug > 10) {
1161                 printf("uhci_device_bulk_transfer: xfer(1)\n");
1162                 uhci_dump_tds(xfer);
1163         }
1164 #endif
1165
1166         /* Set up interrupt info. */
1167         ii->reqh = reqh;
1168         ii->stdstart = xfer;
1169         ii->stdend = xferend;
1170 #ifdef DIAGNOSTIC
1171         ii->isdone = 0;
1172 #endif
1173
1174         sqh->qh->elink = xfer;
1175         sqh->qh->qh_elink = xfer->physaddr;
1176         sqh->intr_info = ii;
1177
1178         s = splusb();
1179         uhci_add_bulk(sc, sqh);
1180         LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
1181
1182         if (reqh->timeout && !sc->sc_bus.use_polling) {
1183 #if defined(__NetBSD__)
1184                 timeout(uhci_timeout, ii, MS_TO_TICKS(reqh->timeout));
1185 #elif defined(__FreeBSD__)
1186                 /* To avoid race conditions we first initialise the struct
1187                  * before we use it. The timeout might happen between the
1188                  * setting of the timeout and the setting of timeout_handle
1189                  */
1190                 callout_handle_init(&ii->timeout_handle);
1191                 ii->timeout_handle = timeout(uhci_timeout, ii,
1192                                                 MS_TO_TICKS(reqh->timeout));
1193 #endif
1194         }
1195         splx(s);
1196
1197 #ifdef USB_DEBUG
1198         if (uhcidebug > 10) {
1199                 printf("uhci_device_bulk_transfer: xfer(2)\n");
1200                 uhci_dump_tds(xfer);
1201         }
1202 #endif
1203
1204         return (USBD_IN_PROGRESS);
1205
1206  ret2:
1207         if (len != 0)
1208                 usb_freemem(sc->sc_dmatag, dmap);
1209  ret1:
1210         return (r);
1211 }
1212
1213 /* Abort a device bulk request. */
1214 void
1215 uhci_device_bulk_abort(reqh)
1216         usbd_request_handle reqh;
1217 {
1218         /* XXX inactivate */
1219         usbd_delay_ms(reqh->pipe->device->bus, 1);      /* make sure it is finished */
1220         /* XXX call done */
1221 }
1222
1223 /* Close a device bulk pipe. */
1224 void
1225 uhci_device_bulk_close(pipe)
1226         usbd_pipe_handle pipe;
1227 {
1228         struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1229         usbd_device_handle dev = upipe->pipe.device;
1230         uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1231
1232         uhci_free_sqh(sc, upipe->u.bulk.sqh);
1233         uhci_free_intr_info(upipe->iinfo);
1234         /* XXX free other resources */
1235 }
1236
1237 usbd_status
1238 uhci_device_ctrl_transfer(reqh)
1239         usbd_request_handle reqh;
1240 {
1241         uhci_softc_t *sc = (uhci_softc_t *)reqh->pipe->device->bus;
1242         usbd_status r;
1243
1244         if (!reqh->isreq)
1245                 panic("uhci_device_ctrl_transfer: not a request\n");
1246
1247         r = uhci_device_request(reqh);
1248         if (r != USBD_NORMAL_COMPLETION)
1249                 return (r);
1250
1251         if (sc->sc_bus.use_polling)
1252                 uhci_waitintr(sc, reqh);
1253         return (USBD_IN_PROGRESS);
1254 }
1255
1256 usbd_status
1257 uhci_device_intr_transfer(reqh)
1258         usbd_request_handle reqh;
1259 {
1260         struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1261         usbd_device_handle dev = upipe->pipe.device;
1262         uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1263         uhci_intr_info_t *ii = upipe->iinfo;
1264         uhci_soft_td_t *xfer, *xferend;
1265         uhci_soft_qh_t *sqh;
1266         usb_dma_t *dmap;
1267         usbd_status r;
1268         int len, i;
1269         int s;
1270
1271         DPRINTFN(3, ("uhci_device_intr_transfer: reqh=%p buf=%p len=%d flags=%d\n",
1272                      reqh, reqh->buffer, reqh->length, reqh->flags));
1273
1274         if (reqh->isreq)
1275                 panic("uhci_device_intr_transfer: a request\n");
1276
1277         len = reqh->length;
1278         dmap = &upipe->u.intr.datadma;
1279         if (len == 0)
1280                 return (USBD_INVAL); /* XXX should it be? */
1281
1282         r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
1283         if (r != USBD_NORMAL_COMPLETION)
1284                 goto ret1;
1285         r = uhci_alloc_std_chain(upipe, sc, len, 1, dmap, &xfer, &xferend);
1286         if (r != USBD_NORMAL_COMPLETION)
1287                 goto ret2;
1288         xferend->td->td_status |= UHCI_TD_IOC;
1289
1290 #ifdef USB_DEBUG
1291         if (uhcidebug > 10) {
1292                 printf("uhci_device_intr_transfer: xfer(1)\n");
1293                 uhci_dump_tds(xfer);
1294                 uhci_dump_qh(upipe->u.intr.qhs[0]);
1295         }
1296 #endif
1297
1298         s = splusb();
1299         /* Set up interrupt info. */
1300         ii->reqh = reqh;
1301         ii->stdstart = xfer;
1302         ii->stdend = xferend;
1303 #ifdef DIAGNOSTIC
1304         ii->isdone = 0;
1305 #endif
1306
1307 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n", upipe->u.intr.qhs[0]));
1308         for (i = 0; i < upipe->u.intr.npoll; i++) {
1309                 sqh = upipe->u.intr.qhs[i];
1310                 sqh->qh->elink = xfer;
1311                 sqh->qh->qh_elink = xfer->physaddr;
1312         }
1313         splx(s);
1314
1315 #ifdef USB_DEBUG
1316         if (uhcidebug > 10) {
1317                 printf("uhci_device_intr_transfer: xfer(2)\n");
1318                 uhci_dump_tds(xfer);
1319                 uhci_dump_qh(upipe->u.intr.qhs[0]);
1320         }
1321 #endif
1322
1323         return (USBD_IN_PROGRESS);
1324
1325  ret2:
1326         if (len != 0)
1327                 usb_freemem(sc->sc_dmatag, dmap);
1328  ret1:
1329         return (r);
1330 }
1331
1332 /* Abort a device control request. */
1333 void
1334 uhci_device_ctrl_abort(reqh)
1335         usbd_request_handle reqh;
1336 {
1337         /* XXX inactivate */
1338         usbd_delay_ms(reqh->pipe->device->bus, 1);      /* make sure it is finished */
1339         /* XXX call done */
1340 }
1341
1342 /* Close a device control pipe. */
1343 void
1344 uhci_device_ctrl_close(pipe)
1345         usbd_pipe_handle pipe;
1346 {
1347         struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1348
1349         uhci_free_intr_info(upipe->iinfo);
1350         /* XXX free other resources */
1351 }
1352
1353 /* Abort a device interrupt request. */
1354 void
1355 uhci_device_intr_abort(reqh)
1356         usbd_request_handle reqh;
1357 {
1358         struct uhci_pipe *upipe;
1359
1360         DPRINTFN(1, ("uhci_device_intr_abort: reqh=%p\n", reqh));
1361         /* XXX inactivate */
1362         usbd_delay_ms(reqh->pipe->device->bus, 2);      /* make sure it is finished */
1363         if (reqh->pipe->intrreqh == reqh) {
1364                 DPRINTF(("uhci_device_intr_abort: remove\n"));
1365                 reqh->pipe->intrreqh = 0;
1366                 upipe = (struct uhci_pipe *)reqh->pipe;
1367                 uhci_intr_done(upipe->u.intr.qhs[0]->intr_info);
1368         }
1369 }
1370
1371 /* Close a device interrupt pipe. */
1372 void
1373 uhci_device_intr_close(pipe)
1374         usbd_pipe_handle pipe;
1375 {
1376         struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1377         uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
1378         int i, s, npoll;
1379
1380         upipe->iinfo->stdstart = 0;             /* inactive */
1381
1382         /* Unlink descriptors from controller data structures. */
1383         npoll = upipe->u.intr.npoll;
1384         uhci_lock_frames(sc);
1385         for (i = 0; i < npoll; i++)
1386                 uhci_remove_intr(sc, upipe->u.intr.qhs[i]->pos, 
1387                                  upipe->u.intr.qhs[i]);
1388         uhci_unlock_frames(sc);
1389
1390         /* 
1391          * We now have to wait for any activity on the physical
1392          * descriptors to stop.
1393          */
1394         usbd_delay_ms(&sc->sc_bus, 2);
1395
1396         for(i = 0; i < npoll; i++)
1397                 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
1398         free(upipe->u.intr.qhs, M_USB);
1399
1400         s = splusb();
1401         LIST_REMOVE(upipe->iinfo, list);        /* remove from active list */
1402         splx(s);
1403         uhci_free_intr_info(upipe->iinfo);
1404
1405         /* XXX free other resources */
1406 }
1407
1408 usbd_status
1409 uhci_device_request(reqh)
1410         usbd_request_handle reqh;
1411 {
1412         struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1413         usb_device_request_t *req = &reqh->request;
1414         usbd_device_handle dev = upipe->pipe.device;
1415         uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1416         int addr = dev->address;
1417         int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1418         uhci_intr_info_t *ii = upipe->iinfo;
1419         uhci_soft_td_t *setup, *xfer, *stat, *next, *xferend;
1420         uhci_soft_qh_t *sqh;
1421         usb_dma_t *dmap;
1422         int len;
1423         u_int32_t ls;
1424         usbd_status r;
1425         int isread;
1426         int s;
1427
1428         DPRINTFN(5,("uhci_device_control type=0x%02x, request=0x%02x, wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1429                     req->bmRequestType, req->bRequest, UGETW(req->wValue),
1430                     UGETW(req->wIndex), UGETW(req->wLength),
1431                     addr, endpt));
1432
1433         ls = dev->lowspeed ? UHCI_TD_LS : 0;
1434         isread = req->bmRequestType & UT_READ;
1435         len = UGETW(req->wLength);
1436
1437         setup = upipe->u.ctl.setup;
1438         stat = upipe->u.ctl.stat;
1439         sqh = upipe->u.ctl.sqh;
1440         dmap = &upipe->u.ctl.datadma;
1441
1442         /* Set up data transaction */
1443         if (len != 0) {
1444                 r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
1445                 if (r != USBD_NORMAL_COMPLETION)
1446                         goto ret1;
1447                 upipe->pipe.endpoint->toggle = 1;
1448                 r = uhci_alloc_std_chain(upipe, sc, len, isread, 
1449                                          dmap, &xfer, &xferend);
1450                 if (r != USBD_NORMAL_COMPLETION)
1451                         goto ret2;
1452                 next = xfer;
1453                 xferend->td->link.std = stat;
1454                 xferend->td->td_link = stat->physaddr;
1455         } else {
1456                 xfer = 0;
1457                 next = stat;
1458         }
1459         upipe->u.ctl.length = len;
1460         upipe->u.ctl.xferend = xferend;
1461
1462         memcpy(KERNADDR(&upipe->u.ctl.reqdma), req, sizeof *req);
1463         if (!isread && len != 0)
1464                 memcpy(KERNADDR(dmap), reqh->buffer, len);
1465
1466         setup->td->link.std = next;
1467         setup->td->td_link = next->physaddr;
1468         setup->td->td_status = UHCI_TD_SET_ERRCNT(2) | ls | UHCI_TD_ACTIVE;
1469         setup->td->td_token = UHCI_TD_SETUP(sizeof *req, endpt, addr);
1470         setup->td->td_buffer = DMAADDR(&upipe->u.ctl.reqdma);
1471
1472         stat->td->link.std = 0;
1473         stat->td->td_link = UHCI_PTR_T;
1474         stat->td->td_status = UHCI_TD_SET_ERRCNT(2) | ls | 
1475                 UHCI_TD_ACTIVE | UHCI_TD_IOC;
1476         stat->td->td_token = 
1477                 isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
1478                          UHCI_TD_IN (0, endpt, addr, 1);
1479         stat->td->td_buffer = 0;
1480
1481 #ifdef USB_DEBUG
1482         if (uhcidebug > 20) {
1483                 printf("uhci_device_request: setup\n");
1484                 uhci_dump_td(setup);
1485                 printf("uhci_device_request: stat\n");
1486                 uhci_dump_td(stat);
1487         }
1488 #endif
1489
1490         /* Set up interrupt info. */
1491         ii->reqh = reqh;
1492         ii->stdstart = setup;
1493         ii->stdend = stat;
1494 #ifdef DIAGNOSTIC
1495         ii->isdone = 0;
1496 #endif
1497
1498         sqh->qh->elink = setup;
1499         sqh->qh->qh_elink = setup->physaddr;
1500         sqh->intr_info = ii;
1501
1502         s = splusb();
1503         uhci_add_ctrl(sc, sqh);
1504         LIST_INSERT_HEAD(&sc->sc_intrhead, ii, list);
1505 #ifdef USB_DEBUG
1506         if (uhcidebug > 12) {
1507                 uhci_soft_td_t *std;
1508                 uhci_soft_qh_t *xqh;
1509                 uhci_soft_qh_t *sxqh;
1510                 int maxqh = 0;
1511                 uhci_physaddr_t link;
1512                 printf("uhci_enter_ctl_q: follow from [0]\n");
1513                 for (std = sc->sc_vframes[0].htd, link = 0;
1514                      (link & UHCI_PTR_Q) == 0;
1515                      std = std->td->link.std) {
1516                         link = std->td->td_link;
1517                         uhci_dump_td(std);
1518                 }
1519                 for (sxqh = xqh = (uhci_soft_qh_t *)std;
1520                      xqh;
1521                      /* FIXME NWH seems to be a circular list ??
1522                       * checking for beginning of list end of list
1523                       * and printing a maximum of 5 QH's ...
1524                      xqh = xqh->qh->hlink)
1525                         */
1526                      xqh = (maxqh++ == 5 || xqh->qh->hlink==sxqh || xqh->qh->hlink==xqh? NULL : xqh->qh->hlink)) {
1527                         uhci_dump_qh(xqh);
1528                         uhci_dump_qh(sxqh);
1529                 }
1530                 printf("Enqueued QH:\n");
1531                 uhci_dump_qh(sqh);
1532                 uhci_dump_tds(sqh->qh->elink);
1533         }
1534 #endif
1535         if (reqh->timeout && !sc->sc_bus.use_polling) {
1536 #if defined(__NetBSD__)
1537                 timeout(uhci_timeout, ii, MS_TO_TICKS(reqh->timeout));
1538 #elif defined(__FreeBSD__)
1539                 /* To avoid race conditions we first initialise the struct
1540                  * before we use it. The timeout may happen between the setting
1541                  * of the timeout and the setting of callout_handle
1542                  */
1543                 callout_handle_init(&ii->timeout_handle);
1544                 ii->timeout_handle = timeout(uhci_timeout, ii,
1545                                                 MS_TO_TICKS(reqh->timeout));
1546 #endif
1547         }
1548         splx(s);
1549
1550         return (USBD_NORMAL_COMPLETION);
1551
1552  ret2:
1553         if (len != 0)
1554                 usb_freemem(sc->sc_dmatag, dmap);
1555  ret1:
1556         return (r);
1557 }
1558
1559 void
1560 uhci_intr_done(ii)
1561         uhci_intr_info_t *ii;
1562 {
1563         uhci_softc_t *sc = ii->sc;
1564         usbd_request_handle reqh = ii->reqh;
1565         struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1566         usb_dma_t *dma;
1567         uhci_soft_qh_t *sqh;
1568         int i, npoll;
1569
1570         DPRINTFN(5, ("uhci_intr_done: length=%d\n", reqh->actlen));
1571
1572         dma = &upipe->u.intr.datadma;
1573         memcpy(reqh->buffer, KERNADDR(dma), reqh->actlen);
1574         npoll = upipe->u.intr.npoll;
1575         for(i = 0; i < npoll; i++) {
1576                 sqh = upipe->u.intr.qhs[i];
1577                 sqh->qh->elink = 0;
1578                 sqh->qh->qh_elink = UHCI_PTR_T;
1579         }
1580         uhci_free_std_chain(sc, ii->stdstart, 0);
1581
1582         /* XXX Wasteful. */
1583         if (reqh->pipe->intrreqh == reqh) {
1584                 uhci_soft_td_t *xfer, *xferend;
1585
1586                 /* This alloc cannot fail since we freed the chain above. */
1587                 uhci_alloc_std_chain(upipe, sc, reqh->length, 1, dma,
1588                                      &xfer, &xferend);
1589                 xferend->td->td_status |= UHCI_TD_IOC;
1590
1591 #ifdef USB_DEBUG
1592                 if (uhcidebug > 10) {
1593                         printf("uhci_device_intr_done: xfer(1)\n");
1594                         uhci_dump_tds(xfer);
1595                         uhci_dump_qh(upipe->u.intr.qhs[0]);
1596                 }
1597 #endif
1598
1599                 ii->stdstart = xfer;
1600                 ii->stdend = xferend;
1601 #ifdef DIAGNOSTIC
1602                 ii->isdone = 0;
1603 #endif
1604                 for (i = 0; i < npoll; i++) {
1605                         sqh = upipe->u.intr.qhs[i];
1606                         sqh->qh->elink = xfer;
1607                         sqh->qh->qh_elink = xfer->physaddr;
1608                 }
1609         } else {
1610                 usb_freemem(sc->sc_dmatag, dma);
1611                 ii->stdstart = 0;       /* mark as inactive */
1612         }
1613 }
1614
1615 /* Deallocate request data structures */
1616 void
1617 uhci_ctrl_done(ii)
1618         uhci_intr_info_t *ii;
1619 {
1620         uhci_softc_t *sc = ii->sc;
1621         usbd_request_handle reqh = ii->reqh;
1622         struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1623         u_int len = upipe->u.ctl.length;
1624         usb_dma_t *dma;
1625         uhci_td_t *htd = ii->stdstart->td;
1626
1627 #ifdef DIAGNOSTIC
1628         if (!reqh->isreq)
1629                 panic("uhci_ctrl_done: not a request\n");
1630 #endif
1631
1632         LIST_REMOVE(ii, list);  /* remove from active list */
1633
1634         uhci_remove_ctrl(sc, upipe->u.ctl.sqh);
1635
1636         if (len != 0) {
1637                 dma = &upipe->u.ctl.datadma;
1638                 if (reqh->request.bmRequestType & UT_READ)
1639                         memcpy(reqh->buffer, KERNADDR(dma), len);
1640                 uhci_free_std_chain(sc, htd->link.std, ii->stdend);
1641                 usb_freemem(sc->sc_dmatag, dma);
1642         }
1643         DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", reqh->actlen));
1644 }
1645
1646 /* Deallocate request data structures */
1647 void
1648 uhci_bulk_done(ii)
1649         uhci_intr_info_t *ii;
1650 {
1651         uhci_softc_t *sc = ii->sc;
1652         usbd_request_handle reqh = ii->reqh;
1653         struct uhci_pipe *upipe = (struct uhci_pipe *)reqh->pipe;
1654         u_int len = upipe->u.bulk.length;
1655         usb_dma_t *dma;
1656         uhci_td_t *htd = ii->stdstart->td;
1657
1658         LIST_REMOVE(ii, list);  /* remove from active list */
1659
1660         uhci_remove_bulk(sc, upipe->u.bulk.sqh);
1661
1662         if (len != 0) {
1663                 dma = &upipe->u.bulk.datadma;
1664                 if (upipe->u.bulk.isread && len != 0)
1665                         memcpy(reqh->buffer, KERNADDR(dma), len);
1666                 uhci_free_std_chain(sc, htd->link.std, 0);
1667                 usb_freemem(sc->sc_dmatag, dma);
1668         }
1669         DPRINTFN(4, ("uhci_bulk_done: length=%d\n", reqh->actlen));
1670         /* XXX compute new toggle */
1671 }
1672
1673 /* Add interrupt QH, called with vflock. */
1674 void
1675 uhci_add_intr(sc, n, sqh)
1676         uhci_softc_t *sc;
1677         int n;
1678         uhci_soft_qh_t *sqh;
1679 {
1680         struct uhci_vframe *vf = &sc->sc_vframes[n];
1681         uhci_qh_t *eqh;
1682
1683         DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", n, sqh));
1684         eqh = vf->eqh->qh;
1685         sqh->qh->hlink     = eqh->hlink;
1686         sqh->qh->qh_hlink  = eqh->qh_hlink;
1687         eqh->hlink         = sqh;
1688         eqh->qh_hlink      = sqh->physaddr | UHCI_PTR_Q;
1689         vf->eqh = sqh;
1690         vf->bandwidth++;
1691 }
1692
1693 /* Remove interrupt QH, called with vflock. */
1694 void
1695 uhci_remove_intr(sc, n, sqh)
1696         uhci_softc_t *sc;
1697         int n;
1698         uhci_soft_qh_t *sqh;
1699 {
1700         struct uhci_vframe *vf = &sc->sc_vframes[n];
1701         uhci_soft_qh_t *pqh;
1702
1703         DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", n, sqh));
1704
1705         for (pqh = vf->hqh; pqh->qh->hlink != sqh; pqh = pqh->qh->hlink)
1706 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)           
1707                 if (pqh->qh->qh_hlink & UHCI_PTR_T) {
1708                         printf("uhci_remove_intr: QH not found\n");
1709                         return;
1710                 }
1711 #else
1712                 ;
1713 #endif
1714         pqh->qh->hlink    = sqh->qh->hlink;
1715         pqh->qh->qh_hlink = sqh->qh->qh_hlink;
1716         if (vf->eqh == sqh)
1717                 vf->eqh = pqh;
1718         vf->bandwidth--;
1719 }
1720
1721 usbd_status
1722 uhci_device_setintr(sc, upipe, ival)
1723         uhci_softc_t *sc;
1724         struct uhci_pipe *upipe;
1725         int ival;
1726 {
1727         uhci_soft_qh_t *sqh;
1728         int i, npoll, s;
1729         u_int bestbw, bw, bestoffs, offs;
1730
1731         DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
1732         if (ival == 0) {
1733                 printf("uhci_setintr: 0 interval\n");
1734                 return (USBD_INVAL);
1735         }
1736
1737         if (ival > UHCI_VFRAMELIST_COUNT)
1738                 ival = UHCI_VFRAMELIST_COUNT;
1739         npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
1740         DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
1741
1742         upipe->u.intr.npoll = npoll;
1743         upipe->u.intr.qhs = 
1744                 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USB, M_WAITOK);
1745
1746         /* 
1747          * Figure out which offset in the schedule that has most
1748          * bandwidth left over.
1749          */
1750 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
1751         for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
1752                 for (bw = i = 0; i < npoll; i++)
1753                         bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
1754                 if (bw < bestbw) {
1755                         bestbw = bw;
1756                         bestoffs = offs;
1757                 }
1758         }
1759         DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
1760
1761         upipe->iinfo->stdstart = 0;
1762         for(i = 0; i < npoll; i++) {
1763                 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
1764                 sqh->qh->elink = 0;
1765                 sqh->qh->qh_elink = UHCI_PTR_T;
1766                 sqh->pos = MOD(i * ival + bestoffs);
1767                 sqh->intr_info = upipe->iinfo;
1768         }
1769 #undef MOD
1770
1771         s = splusb();
1772         LIST_INSERT_HEAD(&sc->sc_intrhead, upipe->iinfo, list);
1773         splx(s);
1774
1775         uhci_lock_frames(sc);
1776         /* Enter QHs into the controller data structures. */
1777         for(i = 0; i < npoll; i++)
1778                 uhci_add_intr(sc, upipe->u.intr.qhs[i]->pos, 
1779                               upipe->u.intr.qhs[i]);
1780         uhci_unlock_frames(sc);
1781
1782         DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
1783         return (USBD_NORMAL_COMPLETION);
1784 }
1785
1786 /* Open a new pipe. */
1787 usbd_status
1788 uhci_open(pipe)
1789         usbd_pipe_handle pipe;
1790 {
1791         uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
1792         struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1793         usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1794         usbd_status r;
1795
1796         DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1797                      pipe, pipe->device->address, 
1798                      ed->bEndpointAddress, sc->sc_addr));
1799         if (pipe->device->address == sc->sc_addr) {
1800                 switch (ed->bEndpointAddress) {
1801                 case USB_CONTROL_ENDPOINT:
1802                         pipe->methods = &uhci_root_ctrl_methods;
1803                         break;
1804                 case UE_IN | UHCI_INTR_ENDPT:
1805                         pipe->methods = &uhci_root_intr_methods;
1806                         break;
1807                 default:
1808                         return (USBD_INVAL);
1809                 }
1810         } else {
1811                 upipe->iinfo = uhci_alloc_intr_info(sc);
1812                 if (upipe->iinfo == 0)
1813                         return (USBD_NOMEM);
1814                 switch (ed->bmAttributes & UE_XFERTYPE) {
1815                 case UE_CONTROL:
1816                         pipe->methods = &uhci_device_ctrl_methods;
1817                         upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
1818                         if (upipe->u.ctl.sqh == 0)
1819                                 goto bad;
1820                         upipe->u.ctl.setup = uhci_alloc_std(sc);
1821                         if (upipe->u.ctl.setup == 0) {
1822                                 uhci_free_sqh(sc, upipe->u.ctl.sqh);
1823                                 goto bad;
1824                         }
1825                         upipe->u.ctl.stat = uhci_alloc_std(sc);
1826                         if (upipe->u.ctl.stat == 0) {
1827                                 uhci_free_sqh(sc, upipe->u.ctl.sqh);
1828                                 uhci_free_std(sc, upipe->u.ctl.setup);
1829                                 goto bad;
1830                         }
1831                         r = usb_allocmem(sc->sc_dmatag, 
1832                                          sizeof(usb_device_request_t), 
1833                                          0, &upipe->u.ctl.reqdma);
1834                         if (r != USBD_NORMAL_COMPLETION) {
1835                                 uhci_free_sqh(sc, upipe->u.ctl.sqh);
1836                                 uhci_free_std(sc, upipe->u.ctl.setup);
1837                                 uhci_free_std(sc, upipe->u.ctl.stat);
1838                                 goto bad;
1839                         }
1840                         break;
1841                 case UE_INTERRUPT:
1842                         pipe->methods = &uhci_device_intr_methods;
1843                         return (uhci_device_setintr(sc, upipe, ed->bInterval));
1844                 case UE_ISOCHRONOUS:
1845                         printf("uhci_open: iso not implemented\n");
1846                         return (USBD_XXX);
1847                 case UE_BULK:
1848                         pipe->methods = &uhci_device_bulk_methods;
1849                         upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
1850                         if (upipe->u.bulk.sqh == 0)
1851                                 goto bad;
1852                         break;
1853                 }
1854         }
1855         return (USBD_NORMAL_COMPLETION);
1856
1857  bad:
1858         uhci_free_intr_info(upipe->iinfo);
1859         return (USBD_NOMEM);
1860 }
1861
1862 /*
1863  * Data structures and routines to emulate the root hub.
1864  */
1865 usb_device_descriptor_t uhci_devd = {
1866         USB_DEVICE_DESCRIPTOR_SIZE,
1867         UDESC_DEVICE,           /* type */
1868         {0x00, 0x01},           /* USB version */
1869         UCLASS_HUB,             /* class */
1870         USUBCLASS_HUB,          /* subclass */
1871         0,                      /* protocol */
1872         64,                     /* max packet */
1873         {0},{0},{0x00,0x01},    /* device id */
1874         1,2,0,                  /* string indicies */
1875         1                       /* # of configurations */
1876 };
1877
1878 usb_config_descriptor_t uhci_confd = {
1879         USB_CONFIG_DESCRIPTOR_SIZE,
1880         UDESC_CONFIG,
1881         {USB_CONFIG_DESCRIPTOR_SIZE +
1882          USB_INTERFACE_DESCRIPTOR_SIZE +
1883          USB_ENDPOINT_DESCRIPTOR_SIZE},
1884         1,
1885         1,
1886         0,
1887         UC_SELF_POWERED,
1888         0                       /* max power */
1889 };
1890
1891 usb_interface_descriptor_t uhci_ifcd = {
1892         USB_INTERFACE_DESCRIPTOR_SIZE,
1893         UDESC_INTERFACE,
1894         0,
1895         0,
1896         1,
1897         UCLASS_HUB,
1898         USUBCLASS_HUB,
1899         0,
1900         0
1901 };
1902
1903 usb_endpoint_descriptor_t uhci_endpd = {
1904         USB_ENDPOINT_DESCRIPTOR_SIZE,
1905         UDESC_ENDPOINT,
1906         UE_IN | UHCI_INTR_ENDPT,
1907         UE_INTERRUPT,
1908         {8},
1909         255
1910 };
1911
1912 usb_hub_descriptor_t uhci_hubd_piix = {
1913         USB_HUB_DESCRIPTOR_SIZE,
1914         UDESC_HUB,
1915         2,
1916         { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
1917         50,                     /* power on to power good */
1918         0,
1919         { 0x00 },               /* both ports are removable */
1920         { 0x00 },               /* no ports can power down individually */
1921 };
1922
1923 int
1924 uhci_str(p, l, s)
1925         usb_string_descriptor_t *p;
1926         int l;
1927         char *s;
1928 {
1929         int i;
1930
1931         if (l == 0)
1932                 return (0);
1933         p->bLength = 2 * strlen(s) + 2;
1934         if (l == 1)
1935                 return (1);
1936         p->bDescriptorType = UDESC_STRING;
1937         l -= 2;
1938         for (i = 0; s[i] && l > 1; i++, l -= 2)
1939                 USETW2(p->bString[i], 0, s[i]);
1940         return (2*i+2);
1941 }
1942
1943 /*
1944  * Simulate a hardware hub by handling all the necessary requests.
1945  */
1946 usbd_status
1947 uhci_root_ctrl_transfer(reqh)
1948         usbd_request_handle reqh;
1949 {
1950         uhci_softc_t *sc = (uhci_softc_t *)reqh->pipe->device->bus;
1951         usb_device_request_t *req;
1952         void *buf;
1953         int port, x;
1954         int len, value, index, status, change, l, totlen = 0;
1955         usb_port_status_t ps;
1956         usbd_status r;
1957
1958         if (!reqh->isreq)
1959                 panic("uhci_root_ctrl_transfer: not a request\n");
1960         req = &reqh->request;
1961         buf = reqh->buffer;
1962
1963         DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n", 
1964                     req->bmRequestType, req->bRequest));
1965
1966         len = UGETW(req->wLength);
1967         value = UGETW(req->wValue);
1968         index = UGETW(req->wIndex);
1969 #define C(x,y) ((x) | ((y) << 8))
1970         switch(C(req->bRequest, req->bmRequestType)) {
1971         case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
1972         case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
1973         case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
1974                 /* 
1975                  * DEVICE_REMOTE_WAKEUP and ENDPOINT_STALL are no-ops
1976                  * for the integrated root hub.
1977                  */
1978                 break;
1979         case C(UR_GET_CONFIG, UT_READ_DEVICE):
1980                 if (len > 0) {
1981                         *(u_int8_t *)buf = sc->sc_conf;
1982                         totlen = 1;
1983                 }
1984                 break;
1985         case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
1986                 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
1987                 switch(value >> 8) {
1988                 case UDESC_DEVICE:
1989                         if ((value & 0xff) != 0) {
1990                                 r = USBD_IOERROR;
1991                                 goto ret;
1992                         }
1993                         totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
1994                         memcpy(buf, &uhci_devd, l);
1995                         break;
1996                 case UDESC_CONFIG:
1997                         if ((value & 0xff) != 0) {
1998                                 r = USBD_IOERROR;
1999                                 goto ret;
2000                         }
2001                         totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2002                         memcpy(buf, &uhci_confd, l);
2003                         buf = (char *)buf + l;
2004                         len -= l;
2005                         l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2006                         totlen += l;
2007                         memcpy(buf, &uhci_ifcd, l);
2008                         buf = (char *)buf + l;
2009                         len -= l;
2010                         l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2011                         totlen += l;
2012                         memcpy(buf, &uhci_endpd, l);
2013                         break;
2014                 case UDESC_STRING:
2015                         if (len == 0)
2016                                 break;
2017                         *(u_int8_t *)buf = 0;
2018                         totlen = 1;
2019                         switch (value & 0xff) {
2020                         case 1: /* Vendor */
2021                                 totlen = uhci_str(buf, len, sc->sc_vendor);
2022                                 break;
2023                         case 2: /* Product */
2024                                 totlen = uhci_str(buf, len, "UHCI root hub");
2025                                 break;
2026                         }
2027                         break;
2028                 default:
2029                         r = USBD_IOERROR;
2030                         goto ret;
2031                 }
2032                 break;
2033         case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2034                 if (len > 0) {
2035                         *(u_int8_t *)buf = 0;
2036                         totlen = 1;
2037                 }
2038                 break;
2039         case C(UR_GET_STATUS, UT_READ_DEVICE):
2040                 if (len > 1) {
2041                         USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2042                         totlen = 2;
2043                 }
2044                 break;
2045         case C(UR_GET_STATUS, UT_READ_INTERFACE):
2046         case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2047                 if (len > 1) {
2048                         USETW(((usb_status_t *)buf)->wStatus, 0);
2049                         totlen = 2;
2050                 }
2051                 break;
2052         case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2053                 if (value >= USB_MAX_DEVICES) {
2054                         r = USBD_IOERROR;
2055                         goto ret;
2056                 }
2057                 sc->sc_addr = value;
2058                 break;
2059         case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2060                 if (value != 0 && value != 1) {
2061                         r = USBD_IOERROR;
2062                         goto ret;
2063                 }
2064                 sc->sc_conf = value;
2065                 break;
2066         case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2067                 break;
2068         case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2069         case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2070         case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2071                 r = USBD_IOERROR;
2072                 goto ret;
2073         case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2074                 break;
2075         case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2076                 break;
2077         /* Hub requests */
2078         case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2079                 break;
2080         case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2081                 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE port=%d feature=%d\n",
2082                              index, value));
2083                 if (index == 1)
2084                         port = UHCI_PORTSC1;
2085                 else if (index == 2)
2086                         port = UHCI_PORTSC2;
2087                 else {
2088                         r = USBD_IOERROR;
2089                         goto ret;
2090                 }
2091                 switch(value) {
2092                 case UHF_PORT_ENABLE:
2093                         x = UREAD2(sc, port);
2094                         UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
2095                         break;
2096                 case UHF_PORT_SUSPEND:
2097                         x = UREAD2(sc, port);
2098                         UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
2099                         break;
2100                 case UHF_PORT_RESET:
2101                         x = UREAD2(sc, port);
2102                         UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2103                         break;
2104                 case UHF_C_PORT_CONNECTION:
2105                         x = UREAD2(sc, port);
2106                         UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
2107                         break;
2108                 case UHF_C_PORT_ENABLE:
2109                         x = UREAD2(sc, port);
2110                         UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
2111                         break;
2112                 case UHF_C_PORT_OVER_CURRENT:
2113                         x = UREAD2(sc, port);
2114                         UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
2115                         break;
2116                 case UHF_C_PORT_RESET:
2117                         sc->sc_isreset = 0;
2118                         r = USBD_NORMAL_COMPLETION;
2119                         goto ret;
2120                 case UHF_PORT_CONNECTION:
2121                 case UHF_PORT_OVER_CURRENT:
2122                 case UHF_PORT_POWER:
2123                 case UHF_PORT_LOW_SPEED:
2124                 case UHF_C_PORT_SUSPEND:
2125                 default:
2126                         r = USBD_IOERROR;
2127                         goto ret;
2128                 }
2129                 break;
2130         case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
2131                 if (index == 1)
2132                         port = UHCI_PORTSC1;
2133                 else if (index == 2)
2134                         port = UHCI_PORTSC2;
2135                 else {
2136                         r = USBD_IOERROR;
2137                         goto ret;
2138                 }
2139                 if (len > 0) {
2140                         *(u_int8_t *)buf = 
2141                                 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
2142                                 UHCI_PORTSC_LS_SHIFT;
2143                         totlen = 1;
2144                 }
2145                 break;
2146         case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2147                 if (value != 0) {
2148                         r = USBD_IOERROR;
2149                         goto ret;
2150                 }
2151                 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
2152                 totlen = l;
2153                 memcpy(buf, &uhci_hubd_piix, l);
2154                 break;
2155         case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2156                 if (len != 4) {
2157                         r = USBD_IOERROR;
2158                         goto ret;
2159                 }
2160                 memset(buf, 0, len);
2161                 totlen = len;
2162                 break;
2163         case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2164                 if (index == 1)
2165                         port = UHCI_PORTSC1;
2166                 else if (index == 2)
2167                         port = UHCI_PORTSC2;
2168                 else {
2169                         r = USBD_IOERROR;
2170                         goto ret;
2171                 }
2172                 if (len != 4) {
2173                         r = USBD_IOERROR;
2174                         goto ret;
2175                 }
2176                 x = UREAD2(sc, port);
2177                 status = change = 0;
2178                 if (x & UHCI_PORTSC_CCS  )
2179                         status |= UPS_CURRENT_CONNECT_STATUS;
2180                 if (x & UHCI_PORTSC_CSC  ) 
2181                         change |= UPS_C_CONNECT_STATUS;
2182                 if (x & UHCI_PORTSC_PE   ) 
2183                         status |= UPS_PORT_ENABLED;
2184                 if (x & UHCI_PORTSC_POEDC) 
2185                         change |= UPS_C_PORT_ENABLED;
2186                 if (x & UHCI_PORTSC_OCI  ) 
2187                         status |= UPS_OVERCURRENT_INDICATOR;
2188                 if (x & UHCI_PORTSC_OCIC ) 
2189                         change |= UPS_C_OVERCURRENT_INDICATOR;
2190                 if (x & UHCI_PORTSC_SUSP ) 
2191                         status |= UPS_SUSPEND;
2192                 if (x & UHCI_PORTSC_LSDA ) 
2193                         status |= UPS_LOW_SPEED;
2194                 status |= UPS_PORT_POWER;
2195                 if (sc->sc_isreset)
2196                         change |= UPS_C_PORT_RESET;
2197                 USETW(ps.wPortStatus, status);
2198                 USETW(ps.wPortChange, change);
2199                 l = min(len, sizeof ps);
2200                 memcpy(buf, &ps, l);
2201                 totlen = l;
2202                 break;
2203         case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2204                 r = USBD_IOERROR;
2205                 goto ret;
2206         case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2207                 break;
2208         case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2209                 if (index == 1)
2210                         port = UHCI_PORTSC1;
2211                 else if (index == 2)
2212                         port = UHCI_PORTSC2;
2213                 else {
2214                         r = USBD_IOERROR;
2215                         goto ret;
2216                 }
2217                 switch(value) {
2218                 case UHF_PORT_ENABLE:
2219                         x = UREAD2(sc, port);
2220                         UWRITE2(sc, port, x | UHCI_PORTSC_PE);
2221                         break;
2222                 case UHF_PORT_SUSPEND:
2223                         x = UREAD2(sc, port);
2224                         UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
2225                         break;
2226                 case UHF_PORT_RESET:
2227                         x = UREAD2(sc, port);
2228                         UWRITE2(sc, port, x | UHCI_PORTSC_PR);
2229                         usbd_delay_ms(&sc->sc_bus, 10);
2230                         UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
2231                         delay(100);
2232                         x = UREAD2(sc, port);
2233                         UWRITE2(sc, port, x  | UHCI_PORTSC_PE);
2234                         delay(100);
2235                         DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
2236                                     index, UREAD2(sc, port)));
2237                         sc->sc_isreset = 1;
2238                         break;
2239                 case UHF_C_PORT_CONNECTION:
2240                 case UHF_C_PORT_ENABLE:
2241                 case UHF_C_PORT_OVER_CURRENT:
2242                 case UHF_PORT_CONNECTION:
2243                 case UHF_PORT_OVER_CURRENT:
2244                 case UHF_PORT_POWER:
2245                 case UHF_PORT_LOW_SPEED:
2246                 case UHF_C_PORT_SUSPEND:
2247                 case UHF_C_PORT_RESET:
2248                 default:
2249                         r = USBD_IOERROR;
2250                         goto ret;
2251                 }
2252                 break;
2253         default:
2254                 r = USBD_IOERROR;
2255                 goto ret;
2256         }
2257         reqh->actlen = totlen;
2258         r = USBD_NORMAL_COMPLETION;
2259  ret:
2260         reqh->status = r;
2261         reqh->xfercb(reqh);
2262         return (USBD_IN_PROGRESS);
2263 }
2264
2265 /* Abort a root control request. */
2266 void
2267 uhci_root_ctrl_abort(reqh)
2268         usbd_request_handle reqh;
2269 {
2270         /* Nothing to do, all transfers are syncronous. */
2271 }
2272
2273 /* Close the root pipe. */
2274 void
2275 uhci_root_ctrl_close(pipe)
2276         usbd_pipe_handle pipe;
2277 {
2278 #if defined(__NetBSD__)
2279         untimeout(uhci_timo, pipe->intrreqh);
2280 #elif defined(__FreeBSD__)
2281         untimeout(uhci_timo, pipe->intrreqh, pipe->intrreqh->timo_handle);
2282 #endif
2283         DPRINTF(("uhci_root_ctrl_close\n"));
2284 }
2285
2286 /* Abort a root interrupt request. */
2287 void
2288 uhci_root_intr_abort(reqh)
2289         usbd_request_handle reqh;
2290 {
2291 #if defined(__NetBSD__)
2292         untimeout(uhci_timo, reqh);
2293 #elif defined(__FreeBSD__)
2294         untimeout(uhci_timo, reqh, reqh->timo_handle);
2295 #endif
2296 }
2297
2298 /* Start a transfer on the root interrupt pipe */
2299 usbd_status
2300 uhci_root_intr_transfer(reqh)
2301         usbd_request_handle reqh;
2302 {
2303         usbd_pipe_handle pipe = reqh->pipe;
2304         uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2305         struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2306         usb_dma_t *dmap;
2307         usbd_status r;
2308         int len;
2309
2310         DPRINTFN(3, ("uhci_root_intr_transfer: reqh=%p buf=%p len=%d flags=%d\n",
2311                      reqh, reqh->buffer, reqh->length, reqh->flags));
2312
2313         len = reqh->length;
2314         dmap = &upipe->u.intr.datadma;
2315         if (len == 0)
2316                 return (USBD_INVAL); /* XXX should it be? */
2317
2318         r = usb_allocmem(sc->sc_dmatag, len, 0, dmap);
2319         if (r != USBD_NORMAL_COMPLETION)
2320                 return (r);
2321
2322         sc->sc_ival = MS_TO_TICKS(reqh->pipe->endpoint->edesc->bInterval);
2323 #if defined(__NetBSD__)
2324         timeout(uhci_timo, reqh, sc->sc_ival);
2325 #elif defined(__FreeBSD__)
2326         /* To avoid race conditions we first initialise the struct
2327          * before we use it. The timeout happen between the setting
2328          * of the timeout and the setting of callout_handle
2329          */
2330         callout_handle_init(&reqh->timo_handle);
2331         reqh->timo_handle = timeout(uhci_timo, reqh, sc->sc_ival);
2332 #endif
2333         return (USBD_IN_PROGRESS);
2334 }
2335
2336 /* Close the root interrupt pipe. */
2337 void
2338 uhci_root_intr_close(pipe)
2339         usbd_pipe_handle pipe;
2340 {
2341 #if defined(__NetBSD__)
2342         untimeout(uhci_timo, pipe->intrreqh);
2343 #elif defined(__FreeBSD__)
2344         untimeout(uhci_timo, pipe->intrreqh, pipe->intrreqh->timo_handle);
2345 #endif
2346         DPRINTF(("uhci_root_intr_close\n"));
2347 }