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