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