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