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