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