Home | History | Annotate | Line # | Download | only in usb
ehci.c revision 1.234.2.24
      1  1.234.2.24     skrll /*	$NetBSD: ehci.c,v 1.234.2.24 2015/01/02 07:57:18 skrll Exp $ */
      2         1.1  augustss 
      3         1.1  augustss /*
      4       1.190       mrg  * Copyright (c) 2004-2012 The NetBSD Foundation, Inc.
      5         1.1  augustss  * All rights reserved.
      6         1.1  augustss  *
      7         1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8       1.190       mrg  * by Lennart Augustsson (lennart (at) augustsson.net), Charles M. Hannum,
      9       1.190       mrg  * Jeremy Morse (jeremy.morse (at) gmail.com), Jared D. McNeill
     10       1.190       mrg  * (jmcneill (at) invisible.ca) and Matthew R. Green (mrg (at) eterna.com.au).
     11         1.1  augustss  *
     12         1.1  augustss  * Redistribution and use in source and binary forms, with or without
     13         1.1  augustss  * modification, are permitted provided that the following conditions
     14         1.1  augustss  * are met:
     15         1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     16         1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     17         1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     18         1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     19         1.1  augustss  *    documentation and/or other materials provided with the distribution.
     20         1.1  augustss  *
     21         1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22         1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23         1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24         1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25         1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26         1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27         1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28         1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29         1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30         1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31         1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     32         1.1  augustss  */
     33         1.1  augustss 
     34         1.1  augustss /*
     35         1.3  augustss  * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller.
     36         1.1  augustss  *
     37        1.35     enami  * The EHCI 1.0 spec can be found at
     38       1.160  uebayasi  * http://www.intel.com/technology/usb/spec.htm
     39         1.7  augustss  * and the USB 2.0 spec at
     40       1.160  uebayasi  * http://www.usb.org/developers/docs/
     41         1.1  augustss  *
     42         1.1  augustss  */
     43         1.4     lukem 
     44        1.52  jdolecek /*
     45        1.52  jdolecek  * TODO:
     46        1.52  jdolecek  * 1) hold off explorations by companion controllers until ehci has started.
     47        1.52  jdolecek  *
     48       1.148    cegger  * 2) The hub driver needs to handle and schedule the transaction translator,
     49       1.100  augustss  *    to assign place in frame where different devices get to go. See chapter
     50        1.91     perry  *    on hubs in USB 2.0 for details.
     51        1.52  jdolecek  *
     52       1.164  uebayasi  * 3) Command failures are not recovered correctly.
     53       1.148    cegger  */
     54        1.52  jdolecek 
     55         1.4     lukem #include <sys/cdefs.h>
     56  1.234.2.24     skrll __KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.234.2.24 2015/01/02 07:57:18 skrll Exp $");
     57        1.47  augustss 
     58        1.47  augustss #include "ohci.h"
     59        1.47  augustss #include "uhci.h"
     60       1.229     skrll #include "opt_usb.h"
     61         1.1  augustss 
     62         1.1  augustss #include <sys/param.h>
     63       1.229     skrll 
     64       1.229     skrll #include <sys/bus.h>
     65       1.229     skrll #include <sys/cpu.h>
     66       1.229     skrll #include <sys/device.h>
     67         1.1  augustss #include <sys/kernel.h>
     68       1.190       mrg #include <sys/kmem.h>
     69       1.229     skrll #include <sys/mutex.h>
     70         1.1  augustss #include <sys/proc.h>
     71         1.1  augustss #include <sys/queue.h>
     72       1.229     skrll #include <sys/select.h>
     73       1.229     skrll #include <sys/sysctl.h>
     74       1.229     skrll #include <sys/systm.h>
     75         1.1  augustss 
     76         1.1  augustss #include <machine/endian.h>
     77         1.1  augustss 
     78         1.1  augustss #include <dev/usb/usb.h>
     79         1.1  augustss #include <dev/usb/usbdi.h>
     80         1.1  augustss #include <dev/usb/usbdivar.h>
     81       1.229     skrll #include <dev/usb/usbhist.h>
     82         1.1  augustss #include <dev/usb/usb_mem.h>
     83         1.1  augustss #include <dev/usb/usb_quirks.h>
     84         1.1  augustss 
     85         1.1  augustss #include <dev/usb/ehcireg.h>
     86         1.1  augustss #include <dev/usb/ehcivar.h>
     87  1.234.2.13     skrll #include <dev/usb/usbroothub.h>
     88         1.1  augustss 
     89       1.230     skrll 
     90       1.230     skrll #ifdef USB_DEBUG
     91       1.230     skrll #ifndef EHCI_DEBUG
     92       1.230     skrll #define ehcidebug 0
     93       1.230     skrll #else
     94       1.229     skrll static int ehcidebug = 0;
     95       1.229     skrll 
     96       1.229     skrll SYSCTL_SETUP(sysctl_hw_ehci_setup, "sysctl hw.ehci setup")
     97       1.190       mrg {
     98       1.229     skrll 	int err;
     99       1.229     skrll 	const struct sysctlnode *rnode;
    100       1.229     skrll 	const struct sysctlnode *cnode;
    101       1.229     skrll 
    102       1.229     skrll 	err = sysctl_createv(clog, 0, NULL, &rnode,
    103       1.229     skrll 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ehci",
    104       1.229     skrll 	    SYSCTL_DESCR("ehci global controls"),
    105       1.229     skrll 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
    106       1.229     skrll 
    107       1.229     skrll 	if (err)
    108       1.229     skrll 		goto fail;
    109       1.190       mrg 
    110       1.229     skrll 	/* control debugging printfs */
    111       1.229     skrll 	err = sysctl_createv(clog, 0, &rnode, &cnode,
    112       1.229     skrll 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    113       1.229     skrll 	    "debug", SYSCTL_DESCR("Enable debugging output"),
    114       1.229     skrll 	    NULL, 0, &ehcidebug, sizeof(ehcidebug), CTL_CREATE, CTL_EOL);
    115       1.229     skrll 	if (err)
    116       1.229     skrll 		goto fail;
    117       1.229     skrll 
    118       1.229     skrll 	return;
    119       1.229     skrll fail:
    120       1.229     skrll 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    121       1.190       mrg }
    122       1.190       mrg 
    123       1.229     skrll #endif /* EHCI_DEBUG */
    124       1.230     skrll #endif /* USB_DEBUG */
    125         1.1  augustss 
    126         1.5  augustss struct ehci_pipe {
    127         1.5  augustss 	struct usbd_pipe pipe;
    128        1.55   mycroft 	int nexttoggle;
    129        1.55   mycroft 
    130        1.10  augustss 	ehci_soft_qh_t *sqh;
    131        1.10  augustss 	union {
    132        1.10  augustss 		ehci_soft_qtd_t *qtd;
    133        1.10  augustss 		/* ehci_soft_itd_t *itd; */
    134   1.234.2.3     skrll 		/* ehci_soft_sitd_t *sitd; */
    135        1.10  augustss 	} tail;
    136        1.10  augustss 	union {
    137        1.10  augustss 		/* Control pipe */
    138        1.10  augustss 		struct {
    139        1.10  augustss 			usb_dma_t reqdma;
    140        1.10  augustss 		} ctl;
    141        1.10  augustss 		/* Interrupt pipe */
    142        1.78  augustss 		struct {
    143        1.78  augustss 			u_int length;
    144        1.78  augustss 		} intr;
    145        1.10  augustss 		/* Bulk pipe */
    146        1.10  augustss 		struct {
    147        1.10  augustss 			u_int length;
    148        1.10  augustss 		} bulk;
    149        1.10  augustss 		/* Iso pipe */
    150       1.139  jmcneill 		struct {
    151       1.139  jmcneill 			u_int next_frame;
    152       1.139  jmcneill 			u_int cur_xfers;
    153       1.139  jmcneill 		} isoc;
    154        1.10  augustss 	} u;
    155         1.5  augustss };
    156         1.5  augustss 
    157         1.5  augustss Static usbd_status	ehci_open(usbd_pipe_handle);
    158         1.5  augustss Static void		ehci_poll(struct usbd_bus *);
    159         1.5  augustss Static void		ehci_softintr(void *);
    160        1.11  augustss Static int		ehci_intr1(ehci_softc_t *);
    161        1.15  augustss Static void		ehci_waitintr(ehci_softc_t *, usbd_xfer_handle);
    162        1.18  augustss Static void		ehci_check_intr(ehci_softc_t *, struct ehci_xfer *);
    163       1.139  jmcneill Static void		ehci_check_qh_intr(ehci_softc_t *, struct ehci_xfer *);
    164       1.139  jmcneill Static void		ehci_check_itd_intr(ehci_softc_t *, struct ehci_xfer *);
    165   1.234.2.3     skrll Static void		ehci_check_sitd_intr(ehci_softc_t *, struct ehci_xfer *);
    166        1.18  augustss Static void		ehci_idone(struct ehci_xfer *);
    167        1.15  augustss Static void		ehci_timeout(void *);
    168        1.15  augustss Static void		ehci_timeout_task(void *);
    169       1.108   xtraeme Static void		ehci_intrlist_timeout(void *);
    170       1.190       mrg Static void		ehci_doorbell(void *);
    171       1.190       mrg Static void		ehci_pcd(void *);
    172         1.5  augustss 
    173         1.5  augustss Static usbd_xfer_handle	ehci_allocx(struct usbd_bus *);
    174         1.5  augustss Static void		ehci_freex(struct usbd_bus *, usbd_xfer_handle);
    175       1.190       mrg Static void		ehci_get_lock(struct usbd_bus *, kmutex_t **);
    176  1.234.2.13     skrll Static int		ehci_roothub_ctrl(struct usbd_bus *,
    177  1.234.2.13     skrll     usb_device_request_t *, void *, int);
    178         1.5  augustss 
    179         1.5  augustss Static usbd_status	ehci_root_intr_transfer(usbd_xfer_handle);
    180         1.5  augustss Static usbd_status	ehci_root_intr_start(usbd_xfer_handle);
    181         1.5  augustss Static void		ehci_root_intr_abort(usbd_xfer_handle);
    182         1.5  augustss Static void		ehci_root_intr_close(usbd_pipe_handle);
    183         1.5  augustss Static void		ehci_root_intr_done(usbd_xfer_handle);
    184         1.5  augustss 
    185         1.5  augustss Static usbd_status	ehci_device_ctrl_transfer(usbd_xfer_handle);
    186         1.5  augustss Static usbd_status	ehci_device_ctrl_start(usbd_xfer_handle);
    187         1.5  augustss Static void		ehci_device_ctrl_abort(usbd_xfer_handle);
    188         1.5  augustss Static void		ehci_device_ctrl_close(usbd_pipe_handle);
    189         1.5  augustss Static void		ehci_device_ctrl_done(usbd_xfer_handle);
    190         1.5  augustss 
    191         1.5  augustss Static usbd_status	ehci_device_bulk_transfer(usbd_xfer_handle);
    192         1.5  augustss Static usbd_status	ehci_device_bulk_start(usbd_xfer_handle);
    193         1.5  augustss Static void		ehci_device_bulk_abort(usbd_xfer_handle);
    194         1.5  augustss Static void		ehci_device_bulk_close(usbd_pipe_handle);
    195         1.5  augustss Static void		ehci_device_bulk_done(usbd_xfer_handle);
    196         1.5  augustss 
    197         1.5  augustss Static usbd_status	ehci_device_intr_transfer(usbd_xfer_handle);
    198         1.5  augustss Static usbd_status	ehci_device_intr_start(usbd_xfer_handle);
    199         1.5  augustss Static void		ehci_device_intr_abort(usbd_xfer_handle);
    200         1.5  augustss Static void		ehci_device_intr_close(usbd_pipe_handle);
    201         1.5  augustss Static void		ehci_device_intr_done(usbd_xfer_handle);
    202         1.5  augustss 
    203         1.5  augustss Static usbd_status	ehci_device_isoc_transfer(usbd_xfer_handle);
    204         1.5  augustss Static usbd_status	ehci_device_isoc_start(usbd_xfer_handle);
    205         1.5  augustss Static void		ehci_device_isoc_abort(usbd_xfer_handle);
    206         1.5  augustss Static void		ehci_device_isoc_close(usbd_pipe_handle);
    207         1.5  augustss Static void		ehci_device_isoc_done(usbd_xfer_handle);
    208         1.5  augustss 
    209   1.234.2.3     skrll Static usbd_status	ehci_device_fs_isoc_transfer(usbd_xfer_handle);
    210   1.234.2.3     skrll Static usbd_status	ehci_device_fs_isoc_start(usbd_xfer_handle);
    211   1.234.2.3     skrll Static void		ehci_device_fs_isoc_abort(usbd_xfer_handle);
    212   1.234.2.3     skrll Static void		ehci_device_fs_isoc_close(usbd_pipe_handle);
    213   1.234.2.3     skrll Static void		ehci_device_fs_isoc_done(usbd_xfer_handle);
    214   1.234.2.3     skrll 
    215  1.234.2.16     skrll Static void		ehci_device_clear_toggle(usbd_pipe_handle);
    216  1.234.2.16     skrll Static void		ehci_noop(usbd_pipe_handle);
    217         1.5  augustss 
    218         1.6  augustss Static void		ehci_disown(ehci_softc_t *, int, int);
    219         1.5  augustss 
    220         1.9  augustss Static ehci_soft_qh_t  *ehci_alloc_sqh(ehci_softc_t *);
    221         1.9  augustss Static void		ehci_free_sqh(ehci_softc_t *, ehci_soft_qh_t *);
    222         1.9  augustss 
    223         1.9  augustss Static ehci_soft_qtd_t  *ehci_alloc_sqtd(ehci_softc_t *);
    224         1.9  augustss Static void		ehci_free_sqtd(ehci_softc_t *, ehci_soft_qtd_t *);
    225        1.25  augustss Static usbd_status	ehci_alloc_sqtd_chain(struct ehci_pipe *,
    226        1.15  augustss 			    ehci_softc_t *, int, int, usbd_xfer_handle,
    227        1.15  augustss 			    ehci_soft_qtd_t **, ehci_soft_qtd_t **);
    228        1.25  augustss Static void		ehci_free_sqtd_chain(ehci_softc_t *, ehci_soft_qtd_t *,
    229        1.18  augustss 					    ehci_soft_qtd_t *);
    230        1.15  augustss 
    231  1.234.2.16     skrll Static ehci_soft_itd_t	*ehci_alloc_itd(ehci_softc_t *);
    232  1.234.2.16     skrll Static ehci_soft_sitd_t *ehci_alloc_sitd(ehci_softc_t *);
    233  1.234.2.16     skrll Static void		ehci_free_itd(ehci_softc_t *, ehci_soft_itd_t *);
    234  1.234.2.16     skrll Static void		ehci_free_sitd(ehci_softc_t *, ehci_soft_sitd_t *);
    235  1.234.2.16     skrll Static void 		ehci_rem_free_itd_chain(ehci_softc_t *,
    236  1.234.2.16     skrll 						struct ehci_xfer *);
    237  1.234.2.16     skrll Static void		ehci_rem_free_sitd_chain(ehci_softc_t *,
    238  1.234.2.16     skrll 						 struct ehci_xfer *);
    239  1.234.2.16     skrll Static void 		ehci_abort_isoc_xfer(usbd_xfer_handle,
    240  1.234.2.16     skrll 						usbd_status);
    241       1.139  jmcneill 
    242  1.234.2.16     skrll Static usbd_status	ehci_device_request(usbd_xfer_handle);
    243         1.9  augustss 
    244        1.78  augustss Static usbd_status	ehci_device_setintr(ehci_softc_t *, ehci_soft_qh_t *,
    245  1.234.2.16     skrll 			    int);
    246        1.78  augustss 
    247       1.190       mrg Static void		ehci_add_qh(ehci_softc_t *, ehci_soft_qh_t *,
    248       1.190       mrg 				    ehci_soft_qh_t *);
    249        1.10  augustss Static void		ehci_rem_qh(ehci_softc_t *, ehci_soft_qh_t *,
    250        1.10  augustss 				    ehci_soft_qh_t *);
    251        1.23  augustss Static void		ehci_set_qh_qtd(ehci_soft_qh_t *, ehci_soft_qtd_t *);
    252        1.11  augustss Static void		ehci_sync_hc(ehci_softc_t *);
    253        1.10  augustss 
    254        1.10  augustss Static void		ehci_close_pipe(usbd_pipe_handle, ehci_soft_qh_t *);
    255        1.10  augustss Static void		ehci_abort_xfer(usbd_xfer_handle, usbd_status);
    256         1.9  augustss 
    257         1.5  augustss #ifdef EHCI_DEBUG
    258       1.229     skrll Static ehci_softc_t 	*theehci;
    259       1.229     skrll void			ehci_dump(void);
    260       1.229     skrll #endif
    261       1.229     skrll 
    262       1.229     skrll #ifdef EHCI_DEBUG
    263        1.18  augustss Static void		ehci_dump_regs(ehci_softc_t *);
    264        1.15  augustss Static void		ehci_dump_sqtds(ehci_soft_qtd_t *);
    265         1.9  augustss Static void		ehci_dump_sqtd(ehci_soft_qtd_t *);
    266         1.9  augustss Static void		ehci_dump_qtd(ehci_qtd_t *);
    267         1.9  augustss Static void		ehci_dump_sqh(ehci_soft_qh_t *);
    268  1.234.2.16     skrll Static void		ehci_dump_sitd(struct ehci_soft_itd *);
    269       1.139  jmcneill Static void		ehci_dump_itd(struct ehci_soft_itd *);
    270       1.141    cegger Static void		ehci_dump_exfer(struct ehci_xfer *);
    271         1.5  augustss #endif
    272         1.5  augustss 
    273        1.11  augustss #define EHCI_NULL htole32(EHCI_LINK_TERMINATE)
    274        1.11  augustss 
    275        1.18  augustss #define ehci_add_intr_list(sc, ex) \
    276  1.234.2.20     skrll 	TAILQ_INSERT_TAIL(&(sc)->sc_intrhead, (ex), ex_next);
    277       1.153  jmcneill #define ehci_del_intr_list(sc, ex) \
    278        1.44  augustss 	do { \
    279  1.234.2.20     skrll 		TAILQ_REMOVE(&sc->sc_intrhead, (ex), ex_next); \
    280  1.234.2.20     skrll 		(ex)->ex_next.tqe_prev = NULL; \
    281        1.44  augustss 	} while (0)
    282  1.234.2.20     skrll #define ehci_active_intr_list(ex) ((ex)->ex_next.tqe_prev != NULL)
    283        1.18  augustss 
    284       1.123  drochner Static const struct usbd_bus_methods ehci_bus_methods = {
    285   1.234.2.6     skrll 	.ubm_open =	ehci_open,
    286   1.234.2.6     skrll 	.ubm_softint =	ehci_softintr,
    287   1.234.2.6     skrll 	.ubm_dopoll =	ehci_poll,
    288   1.234.2.6     skrll 	.ubm_allocx =	ehci_allocx,
    289   1.234.2.6     skrll 	.ubm_freex =	ehci_freex,
    290   1.234.2.6     skrll 	.ubm_getlock =	ehci_get_lock,
    291  1.234.2.13     skrll 	.ubm_rhctrl =	ehci_roothub_ctrl,
    292         1.5  augustss };
    293         1.5  augustss 
    294       1.123  drochner Static const struct usbd_pipe_methods ehci_root_intr_methods = {
    295   1.234.2.6     skrll 	.upm_transfer =	ehci_root_intr_transfer,
    296   1.234.2.6     skrll 	.upm_start =	ehci_root_intr_start,
    297   1.234.2.6     skrll 	.upm_abort =	ehci_root_intr_abort,
    298   1.234.2.6     skrll 	.upm_close =	ehci_root_intr_close,
    299   1.234.2.6     skrll 	.upm_cleartoggle =	ehci_noop,
    300   1.234.2.6     skrll 	.upm_done =	ehci_root_intr_done,
    301         1.5  augustss };
    302         1.5  augustss 
    303       1.123  drochner Static const struct usbd_pipe_methods ehci_device_ctrl_methods = {
    304   1.234.2.6     skrll 	.upm_transfer =	ehci_device_ctrl_transfer,
    305   1.234.2.6     skrll 	.upm_start =	ehci_device_ctrl_start,
    306   1.234.2.6     skrll 	.upm_abort =	ehci_device_ctrl_abort,
    307   1.234.2.6     skrll 	.upm_close =	ehci_device_ctrl_close,
    308   1.234.2.6     skrll 	.upm_cleartoggle =	ehci_noop,
    309   1.234.2.6     skrll 	.upm_done =	ehci_device_ctrl_done,
    310         1.5  augustss };
    311         1.5  augustss 
    312       1.123  drochner Static const struct usbd_pipe_methods ehci_device_intr_methods = {
    313   1.234.2.6     skrll 	.upm_transfer =	ehci_device_intr_transfer,
    314   1.234.2.6     skrll 	.upm_start =	ehci_device_intr_start,
    315   1.234.2.6     skrll 	.upm_abort =	ehci_device_intr_abort,
    316   1.234.2.6     skrll 	.upm_close =	ehci_device_intr_close,
    317   1.234.2.6     skrll 	.upm_cleartoggle =	ehci_device_clear_toggle,
    318   1.234.2.6     skrll 	.upm_done =	ehci_device_intr_done,
    319         1.5  augustss };
    320         1.5  augustss 
    321       1.123  drochner Static const struct usbd_pipe_methods ehci_device_bulk_methods = {
    322   1.234.2.6     skrll 	.upm_transfer =	ehci_device_bulk_transfer,
    323   1.234.2.6     skrll 	.upm_start =	ehci_device_bulk_start,
    324   1.234.2.6     skrll 	.upm_abort =	ehci_device_bulk_abort,
    325   1.234.2.6     skrll 	.upm_close =	ehci_device_bulk_close,
    326   1.234.2.6     skrll 	.upm_cleartoggle =	ehci_device_clear_toggle,
    327   1.234.2.6     skrll 	.upm_done =	ehci_device_bulk_done,
    328         1.5  augustss };
    329         1.5  augustss 
    330       1.123  drochner Static const struct usbd_pipe_methods ehci_device_isoc_methods = {
    331   1.234.2.6     skrll 	.upm_transfer =	ehci_device_isoc_transfer,
    332   1.234.2.6     skrll 	.upm_start =	ehci_device_isoc_start,
    333   1.234.2.6     skrll 	.upm_abort =	ehci_device_isoc_abort,
    334   1.234.2.6     skrll 	.upm_close =	ehci_device_isoc_close,
    335   1.234.2.6     skrll 	.upm_cleartoggle =	ehci_noop,
    336   1.234.2.6     skrll 	.upm_done =	ehci_device_isoc_done,
    337         1.5  augustss };
    338         1.5  augustss 
    339   1.234.2.3     skrll Static const struct usbd_pipe_methods ehci_device_fs_isoc_methods = {
    340   1.234.2.6     skrll 	.upm_transfer =	ehci_device_fs_isoc_transfer,
    341   1.234.2.6     skrll 	.upm_start =	ehci_device_fs_isoc_start,
    342   1.234.2.6     skrll 	.upm_abort =	ehci_device_fs_isoc_abort,
    343   1.234.2.6     skrll 	.upm_close =	ehci_device_fs_isoc_close,
    344   1.234.2.6     skrll 	.upm_cleartoggle = ehci_noop,
    345   1.234.2.6     skrll 	.upm_done =	ehci_device_fs_isoc_done,
    346   1.234.2.3     skrll };
    347   1.234.2.3     skrll 
    348       1.123  drochner static const uint8_t revbits[EHCI_MAX_POLLRATE] = {
    349        1.95  augustss 0x00,0x40,0x20,0x60,0x10,0x50,0x30,0x70,0x08,0x48,0x28,0x68,0x18,0x58,0x38,0x78,
    350        1.95  augustss 0x04,0x44,0x24,0x64,0x14,0x54,0x34,0x74,0x0c,0x4c,0x2c,0x6c,0x1c,0x5c,0x3c,0x7c,
    351        1.95  augustss 0x02,0x42,0x22,0x62,0x12,0x52,0x32,0x72,0x0a,0x4a,0x2a,0x6a,0x1a,0x5a,0x3a,0x7a,
    352        1.95  augustss 0x06,0x46,0x26,0x66,0x16,0x56,0x36,0x76,0x0e,0x4e,0x2e,0x6e,0x1e,0x5e,0x3e,0x7e,
    353        1.95  augustss 0x01,0x41,0x21,0x61,0x11,0x51,0x31,0x71,0x09,0x49,0x29,0x69,0x19,0x59,0x39,0x79,
    354        1.95  augustss 0x05,0x45,0x25,0x65,0x15,0x55,0x35,0x75,0x0d,0x4d,0x2d,0x6d,0x1d,0x5d,0x3d,0x7d,
    355        1.95  augustss 0x03,0x43,0x23,0x63,0x13,0x53,0x33,0x73,0x0b,0x4b,0x2b,0x6b,0x1b,0x5b,0x3b,0x7b,
    356        1.95  augustss 0x07,0x47,0x27,0x67,0x17,0x57,0x37,0x77,0x0f,0x4f,0x2f,0x6f,0x1f,0x5f,0x3f,0x7f,
    357        1.94  augustss };
    358        1.94  augustss 
    359  1.234.2.15     skrll int
    360         1.1  augustss ehci_init(ehci_softc_t *sc)
    361         1.1  augustss {
    362   1.234.2.1     skrll 	uint32_t vers, sparams, cparams, hcr;
    363         1.3  augustss 	u_int i;
    364         1.3  augustss 	usbd_status err;
    365        1.11  augustss 	ehci_soft_qh_t *sqh;
    366        1.89  augustss 	u_int ncomp;
    367         1.3  augustss 
    368       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
    369         1.6  augustss #ifdef EHCI_DEBUG
    370         1.6  augustss 	theehci = sc;
    371         1.6  augustss #endif
    372         1.3  augustss 
    373       1.190       mrg 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    374       1.190       mrg 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
    375       1.190       mrg 	cv_init(&sc->sc_softwake_cv, "ehciab");
    376       1.190       mrg 	cv_init(&sc->sc_doorbell, "ehcidi");
    377       1.190       mrg 
    378       1.204  christos 	sc->sc_xferpool = pool_cache_init(sizeof(struct ehci_xfer), 0, 0, 0,
    379       1.204  christos 	    "ehcixfer", NULL, IPL_USB, NULL, NULL, NULL);
    380       1.204  christos 
    381       1.190       mrg 	sc->sc_doorbell_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
    382       1.190       mrg 	    ehci_doorbell, sc);
    383       1.211      matt 	KASSERT(sc->sc_doorbell_si != NULL);
    384       1.190       mrg 	sc->sc_pcd_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
    385       1.190       mrg 	    ehci_pcd, sc);
    386       1.211      matt 	KASSERT(sc->sc_pcd_si != NULL);
    387       1.190       mrg 
    388         1.3  augustss 	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
    389         1.3  augustss 
    390       1.104  christos 	vers = EREAD2(sc, EHCI_HCIVERSION);
    391       1.134  drochner 	aprint_verbose("%s: EHCI version %x.%x\n", device_xname(sc->sc_dev),
    392       1.104  christos 	       vers >> 8, vers & 0xff);
    393         1.3  augustss 
    394         1.3  augustss 	sparams = EREAD4(sc, EHCI_HCSPARAMS);
    395       1.229     skrll 	USBHIST_LOG(ehcidebug, "sparams=%#x", sparams, 0, 0, 0);
    396         1.6  augustss 	sc->sc_npcomp = EHCI_HCS_N_PCC(sparams);
    397        1.89  augustss 	ncomp = EHCI_HCS_N_CC(sparams);
    398        1.89  augustss 	if (ncomp != sc->sc_ncomp) {
    399       1.121        ad 		aprint_verbose("%s: wrong number of companions (%d != %d)\n",
    400       1.134  drochner 			       device_xname(sc->sc_dev), ncomp, sc->sc_ncomp);
    401        1.47  augustss #if NOHCI == 0 || NUHCI == 0
    402        1.47  augustss 		aprint_error("%s: ohci or uhci probably not configured\n",
    403       1.134  drochner 			     device_xname(sc->sc_dev));
    404        1.47  augustss #endif
    405        1.89  augustss 		if (ncomp < sc->sc_ncomp)
    406        1.89  augustss 			sc->sc_ncomp = ncomp;
    407         1.3  augustss 	}
    408         1.3  augustss 	if (sc->sc_ncomp > 0) {
    409       1.172      matt 		KASSERT(!(sc->sc_flags & EHCIF_ETTF));
    410        1.41   thorpej 		aprint_normal("%s: companion controller%s, %d port%s each:",
    411       1.134  drochner 		    device_xname(sc->sc_dev), sc->sc_ncomp!=1 ? "s" : "",
    412         1.3  augustss 		    EHCI_HCS_N_PCC(sparams),
    413         1.3  augustss 		    EHCI_HCS_N_PCC(sparams)!=1 ? "s" : "");
    414         1.3  augustss 		for (i = 0; i < sc->sc_ncomp; i++)
    415       1.134  drochner 			aprint_normal(" %s", device_xname(sc->sc_comps[i]));
    416        1.41   thorpej 		aprint_normal("\n");
    417         1.3  augustss 	}
    418         1.5  augustss 	sc->sc_noport = EHCI_HCS_N_PORTS(sparams);
    419         1.3  augustss 	cparams = EREAD4(sc, EHCI_HCCPARAMS);
    420       1.229     skrll 	USBHIST_LOG(ehcidebug, "cparams=%#x", cparams, 0, 0, 0);
    421       1.106  augustss 	sc->sc_hasppc = EHCI_HCS_PPC(sparams);
    422        1.36  augustss 
    423        1.36  augustss 	if (EHCI_HCC_64BIT(cparams)) {
    424        1.36  augustss 		/* MUST clear segment register if 64 bit capable. */
    425        1.36  augustss 		EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
    426        1.36  augustss 	}
    427        1.33  augustss 
    428   1.234.2.8     skrll 	sc->sc_bus.ub_revision = USBREV_2_0;
    429   1.234.2.8     skrll 	sc->sc_bus.ub_usedma = true;
    430   1.234.2.8     skrll 	sc->sc_bus.ub_dmaflags = USBMALLOC_MULTISEG;
    431        1.90      fvdl 
    432         1.3  augustss 	/* Reset the controller */
    433       1.229     skrll 	USBHIST_LOG(ehcidebug, "resetting", 0, 0, 0, 0);
    434         1.3  augustss 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
    435         1.3  augustss 	usb_delay_ms(&sc->sc_bus, 1);
    436         1.3  augustss 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
    437         1.3  augustss 	for (i = 0; i < 100; i++) {
    438        1.34  augustss 		usb_delay_ms(&sc->sc_bus, 1);
    439         1.3  augustss 		hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET;
    440         1.3  augustss 		if (!hcr)
    441         1.3  augustss 			break;
    442         1.3  augustss 	}
    443         1.3  augustss 	if (hcr) {
    444       1.134  drochner 		aprint_error("%s: reset timeout\n", device_xname(sc->sc_dev));
    445  1.234.2.15     skrll 		return EIO;
    446         1.3  augustss 	}
    447       1.170  kiyohara 	if (sc->sc_vendor_init)
    448       1.170  kiyohara 		sc->sc_vendor_init(sc);
    449         1.3  augustss 
    450       1.172      matt 	/*
    451       1.172      matt 	 * If we are doing embedded transaction translation function, force
    452       1.172      matt 	 * the controller to host mode.
    453       1.172      matt 	 */
    454       1.172      matt 	if (sc->sc_flags & EHCIF_ETTF) {
    455       1.172      matt 		uint32_t usbmode = EREAD4(sc, EHCI_USBMODE);
    456       1.172      matt 		usbmode &= ~EHCI_USBMODE_CM;
    457       1.172      matt 		usbmode |= EHCI_USBMODE_CM_HOST;
    458       1.172      matt 		EWRITE4(sc, EHCI_USBMODE, usbmode);
    459       1.172      matt 	}
    460       1.172      matt 
    461        1.78  augustss 	/* XXX need proper intr scheduling */
    462        1.78  augustss 	sc->sc_rand = 96;
    463        1.78  augustss 
    464         1.3  augustss 	/* frame list size at default, read back what we got and use that */
    465         1.3  augustss 	switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) {
    466        1.78  augustss 	case 0: sc->sc_flsize = 1024; break;
    467        1.78  augustss 	case 1: sc->sc_flsize = 512; break;
    468        1.78  augustss 	case 2: sc->sc_flsize = 256; break;
    469  1.234.2.15     skrll 	case 3: return EIO;
    470         1.3  augustss 	}
    471        1.78  augustss 	err = usb_allocmem(&sc->sc_bus, sc->sc_flsize * sizeof(ehci_link_t),
    472        1.78  augustss 	    EHCI_FLALIGN_ALIGN, &sc->sc_fldma);
    473         1.3  augustss 	if (err)
    474  1.234.2.14     skrll 		return err;
    475       1.229     skrll 	USBHIST_LOG(ehcidebug, "flsize=%d", sc->sc_flsize, 0, 0, 0);
    476        1.78  augustss 	sc->sc_flist = KERNADDR(&sc->sc_fldma, 0);
    477       1.139  jmcneill 
    478       1.139  jmcneill 	for (i = 0; i < sc->sc_flsize; i++) {
    479       1.139  jmcneill 		sc->sc_flist[i] = EHCI_NULL;
    480       1.139  jmcneill 	}
    481       1.139  jmcneill 
    482        1.78  augustss 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
    483         1.3  augustss 
    484       1.190       mrg 	sc->sc_softitds = kmem_zalloc(sc->sc_flsize * sizeof(ehci_soft_itd_t *),
    485       1.190       mrg 				     KM_SLEEP);
    486       1.139  jmcneill 	if (sc->sc_softitds == NULL)
    487       1.139  jmcneill 		return ENOMEM;
    488       1.139  jmcneill 	LIST_INIT(&sc->sc_freeitds);
    489   1.234.2.3     skrll 	LIST_INIT(&sc->sc_freesitds);
    490       1.153  jmcneill 	TAILQ_INIT(&sc->sc_intrhead);
    491       1.139  jmcneill 
    492         1.5  augustss 	/* Set up the bus struct. */
    493   1.234.2.8     skrll 	sc->sc_bus.ub_methods = &ehci_bus_methods;
    494   1.234.2.8     skrll 	sc->sc_bus.ub_pipesize= sizeof(struct ehci_pipe);
    495         1.5  augustss 
    496         1.6  augustss 	sc->sc_eintrs = EHCI_NORMAL_INTRS;
    497         1.6  augustss 
    498        1.78  augustss 	/*
    499        1.78  augustss 	 * Allocate the interrupt dummy QHs. These are arranged to give poll
    500        1.78  augustss 	 * intervals that are powers of 2 times 1ms.
    501        1.78  augustss 	 */
    502        1.78  augustss 	for (i = 0; i < EHCI_INTRQHS; i++) {
    503        1.78  augustss 		sqh = ehci_alloc_sqh(sc);
    504        1.78  augustss 		if (sqh == NULL) {
    505  1.234.2.15     skrll 			err = ENOMEM;
    506        1.78  augustss 			goto bad1;
    507        1.78  augustss 		}
    508        1.78  augustss 		sc->sc_islots[i].sqh = sqh;
    509        1.78  augustss 	}
    510        1.78  augustss 	for (i = 0; i < EHCI_INTRQHS; i++) {
    511        1.78  augustss 		sqh = sc->sc_islots[i].sqh;
    512        1.78  augustss 		if (i == 0) {
    513        1.78  augustss 			/* The last (1ms) QH terminates. */
    514        1.78  augustss 			sqh->qh.qh_link = EHCI_NULL;
    515        1.78  augustss 			sqh->next = NULL;
    516        1.78  augustss 		} else {
    517        1.78  augustss 			/* Otherwise the next QH has half the poll interval */
    518        1.78  augustss 			sqh->next = sc->sc_islots[(i + 1) / 2 - 1].sqh;
    519        1.78  augustss 			sqh->qh.qh_link = htole32(sqh->next->physaddr |
    520        1.78  augustss 			    EHCI_LINK_QH);
    521        1.78  augustss 		}
    522        1.78  augustss 		sqh->qh.qh_endp = htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH));
    523        1.78  augustss 		sqh->qh.qh_curqtd = EHCI_NULL;
    524        1.78  augustss 		sqh->next = NULL;
    525        1.78  augustss 		sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
    526        1.78  augustss 		sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
    527        1.78  augustss 		sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
    528        1.78  augustss 		sqh->sqtd = NULL;
    529       1.138    bouyer 		usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
    530       1.138    bouyer 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    531        1.78  augustss 	}
    532        1.78  augustss 	/* Point the frame list at the last level (128ms). */
    533        1.78  augustss 	for (i = 0; i < sc->sc_flsize; i++) {
    534        1.94  augustss 		int j;
    535        1.94  augustss 
    536        1.94  augustss 		j = (i & ~(EHCI_MAX_POLLRATE-1)) |
    537        1.94  augustss 		    revbits[i & (EHCI_MAX_POLLRATE-1)];
    538        1.94  augustss 		sc->sc_flist[j] = htole32(EHCI_LINK_QH |
    539        1.78  augustss 		    sc->sc_islots[EHCI_IQHIDX(EHCI_IPOLLRATES - 1,
    540        1.78  augustss 		    i)].sqh->physaddr);
    541        1.78  augustss 	}
    542       1.138    bouyer 	usb_syncmem(&sc->sc_fldma, 0, sc->sc_flsize * sizeof(ehci_link_t),
    543       1.138    bouyer 	    BUS_DMASYNC_PREWRITE);
    544        1.78  augustss 
    545        1.11  augustss 	/* Allocate dummy QH that starts the async list. */
    546        1.11  augustss 	sqh = ehci_alloc_sqh(sc);
    547        1.11  augustss 	if (sqh == NULL) {
    548  1.234.2.15     skrll 		err = ENOMEM;
    549         1.9  augustss 		goto bad1;
    550         1.9  augustss 	}
    551        1.11  augustss 	/* Fill the QH */
    552        1.11  augustss 	sqh->qh.qh_endp =
    553        1.11  augustss 	    htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL);
    554        1.11  augustss 	sqh->qh.qh_link =
    555        1.11  augustss 	    htole32(sqh->physaddr | EHCI_LINK_QH);
    556        1.11  augustss 	sqh->qh.qh_curqtd = EHCI_NULL;
    557        1.11  augustss 	sqh->next = NULL;
    558        1.11  augustss 	/* Fill the overlay qTD */
    559        1.11  augustss 	sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
    560        1.11  augustss 	sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
    561        1.26  augustss 	sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED);
    562        1.11  augustss 	sqh->sqtd = NULL;
    563       1.138    bouyer 	usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
    564       1.138    bouyer 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    565         1.9  augustss #ifdef EHCI_DEBUG
    566       1.229     skrll 	ehci_dump_sqh(sqh);
    567         1.9  augustss #endif
    568         1.9  augustss 
    569         1.9  augustss 	/* Point to async list */
    570        1.11  augustss 	sc->sc_async_head = sqh;
    571        1.11  augustss 	EOWRITE4(sc, EHCI_ASYNCLISTADDR, sqh->physaddr | EHCI_LINK_QH);
    572         1.9  augustss 
    573       1.190       mrg 	callout_init(&sc->sc_tmo_intrlist, CALLOUT_MPSAFE);
    574        1.10  augustss 
    575         1.6  augustss 	/* Turn on controller */
    576         1.6  augustss 	EOWRITE4(sc, EHCI_USBCMD,
    577        1.88  augustss 		 EHCI_CMD_ITC_2 | /* 2 microframes interrupt delay */
    578         1.6  augustss 		 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) |
    579        1.10  augustss 		 EHCI_CMD_ASE |
    580        1.78  augustss 		 EHCI_CMD_PSE |
    581         1.6  augustss 		 EHCI_CMD_RS);
    582         1.6  augustss 
    583         1.6  augustss 	/* Take over port ownership */
    584         1.6  augustss 	EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF);
    585         1.6  augustss 
    586         1.8  augustss 	for (i = 0; i < 100; i++) {
    587        1.34  augustss 		usb_delay_ms(&sc->sc_bus, 1);
    588         1.8  augustss 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
    589         1.8  augustss 		if (!hcr)
    590         1.8  augustss 			break;
    591         1.8  augustss 	}
    592         1.8  augustss 	if (hcr) {
    593       1.134  drochner 		aprint_error("%s: run timeout\n", device_xname(sc->sc_dev));
    594  1.234.2.15     skrll 		return EIO;
    595         1.8  augustss 	}
    596         1.8  augustss 
    597       1.105  augustss 	/* Enable interrupts */
    598       1.229     skrll 	USBHIST_LOG(ehcidebug, "enabling interupts", 0, 0, 0, 0);
    599       1.105  augustss 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
    600       1.105  augustss 
    601  1.234.2.15     skrll 	return 0;
    602         1.9  augustss 
    603         1.9  augustss #if 0
    604        1.11  augustss  bad2:
    605        1.15  augustss 	ehci_free_sqh(sc, sc->sc_async_head);
    606         1.9  augustss #endif
    607         1.9  augustss  bad1:
    608         1.9  augustss 	usb_freemem(&sc->sc_bus, &sc->sc_fldma);
    609  1.234.2.14     skrll 	return err;
    610         1.1  augustss }
    611         1.1  augustss 
    612         1.1  augustss int
    613         1.1  augustss ehci_intr(void *v)
    614         1.1  augustss {
    615         1.6  augustss 	ehci_softc_t *sc = v;
    616       1.190       mrg 	int ret = 0;
    617         1.6  augustss 
    618       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
    619       1.229     skrll 
    620       1.190       mrg 	if (sc == NULL)
    621       1.190       mrg 		return 0;
    622       1.190       mrg 
    623       1.190       mrg 	mutex_spin_enter(&sc->sc_intr_lock);
    624       1.190       mrg 
    625       1.190       mrg 	if (sc->sc_dying || !device_has_power(sc->sc_dev))
    626       1.190       mrg 		goto done;
    627        1.15  augustss 
    628         1.6  augustss 	/* If we get an interrupt while polling, then just ignore it. */
    629   1.234.2.8     skrll 	if (sc->sc_bus.ub_usepolling) {
    630   1.234.2.1     skrll 		uint32_t intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
    631        1.78  augustss 
    632        1.78  augustss 		if (intrs)
    633        1.78  augustss 			EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
    634       1.229     skrll 		USBHIST_LOGN(ehcidebug, 16,
    635       1.229     skrll 		    "ignored interrupt while polling", 0, 0, 0, 0);
    636       1.190       mrg 		goto done;
    637         1.6  augustss 	}
    638         1.6  augustss 
    639       1.190       mrg 	ret = ehci_intr1(sc);
    640       1.190       mrg 
    641       1.190       mrg done:
    642       1.190       mrg 	mutex_spin_exit(&sc->sc_intr_lock);
    643       1.190       mrg 	return ret;
    644         1.6  augustss }
    645         1.6  augustss 
    646         1.6  augustss Static int
    647         1.6  augustss ehci_intr1(ehci_softc_t *sc)
    648         1.6  augustss {
    649   1.234.2.1     skrll 	uint32_t intrs, eintrs;
    650         1.6  augustss 
    651       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
    652         1.6  augustss 
    653         1.6  augustss 	/* In case the interrupt occurs before initialization has completed. */
    654         1.6  augustss 	if (sc == NULL) {
    655         1.6  augustss #ifdef DIAGNOSTIC
    656        1.72  augustss 		printf("ehci_intr1: sc == NULL\n");
    657         1.6  augustss #endif
    658  1.234.2.14     skrll 		return 0;
    659         1.6  augustss 	}
    660         1.6  augustss 
    661       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_intr_lock));
    662       1.190       mrg 
    663         1.6  augustss 	intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
    664         1.6  augustss 	if (!intrs)
    665  1.234.2.14     skrll 		return 0;
    666         1.6  augustss 
    667         1.6  augustss 	eintrs = intrs & sc->sc_eintrs;
    668       1.229     skrll 	USBHIST_LOG(ehcidebug, "sc=%p intrs=%#x(%#x) eintrs=%#x",
    669       1.229     skrll 	    sc, intrs, EOREAD4(sc, EHCI_USBSTS), eintrs);
    670         1.6  augustss 	if (!eintrs)
    671  1.234.2.14     skrll 		return 0;
    672         1.6  augustss 
    673        1.68   mycroft 	EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */
    674        1.10  augustss 	if (eintrs & EHCI_STS_IAA) {
    675       1.229     skrll 		USBHIST_LOG(ehcidebug, "door bell", 0, 0, 0, 0);
    676       1.190       mrg 		kpreempt_disable();
    677       1.211      matt 		KASSERT(sc->sc_doorbell_si != NULL);
    678       1.190       mrg 		softint_schedule(sc->sc_doorbell_si);
    679       1.190       mrg 		kpreempt_enable();
    680        1.20  augustss 		eintrs &= ~EHCI_STS_IAA;
    681        1.10  augustss 	}
    682        1.18  augustss 	if (eintrs & (EHCI_STS_INT | EHCI_STS_ERRINT)) {
    683       1.229     skrll 		USBHIST_LOG(ehcidebug, "INT=%d  ERRINT=%d",
    684       1.229     skrll 		    eintrs & EHCI_STS_INT ? 1 : 0,
    685       1.229     skrll 		    eintrs & EHCI_STS_ERRINT ? 1 : 0, 0, 0);
    686        1.18  augustss 		usb_schedsoftintr(&sc->sc_bus);
    687        1.21  augustss 		eintrs &= ~(EHCI_STS_INT | EHCI_STS_ERRINT);
    688         1.6  augustss 	}
    689         1.6  augustss 	if (eintrs & EHCI_STS_HSE) {
    690         1.6  augustss 		printf("%s: unrecoverable error, controller halted\n",
    691       1.134  drochner 		       device_xname(sc->sc_dev));
    692         1.6  augustss 		/* XXX what else */
    693         1.6  augustss 	}
    694         1.6  augustss 	if (eintrs & EHCI_STS_PCD) {
    695       1.190       mrg 		kpreempt_disable();
    696       1.211      matt 		KASSERT(sc->sc_pcd_si != NULL);
    697       1.190       mrg 		softint_schedule(sc->sc_pcd_si);
    698       1.190       mrg 		kpreempt_enable();
    699         1.6  augustss 		eintrs &= ~EHCI_STS_PCD;
    700         1.6  augustss 	}
    701         1.6  augustss 
    702         1.6  augustss 	if (eintrs != 0) {
    703         1.6  augustss 		/* Block unprocessed interrupts. */
    704         1.6  augustss 		sc->sc_eintrs &= ~eintrs;
    705         1.6  augustss 		EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
    706         1.6  augustss 		printf("%s: blocking intrs 0x%x\n",
    707       1.134  drochner 		       device_xname(sc->sc_dev), eintrs);
    708         1.6  augustss 	}
    709         1.6  augustss 
    710  1.234.2.14     skrll 	return 1;
    711         1.6  augustss }
    712         1.6  augustss 
    713       1.190       mrg Static void
    714       1.190       mrg ehci_doorbell(void *addr)
    715       1.190       mrg {
    716       1.190       mrg 	ehci_softc_t *sc = addr;
    717       1.190       mrg 
    718       1.190       mrg 	mutex_enter(&sc->sc_lock);
    719       1.190       mrg 	cv_broadcast(&sc->sc_doorbell);
    720       1.190       mrg 	mutex_exit(&sc->sc_lock);
    721       1.190       mrg }
    722         1.6  augustss 
    723       1.164  uebayasi Static void
    724       1.190       mrg ehci_pcd(void *addr)
    725         1.6  augustss {
    726       1.190       mrg 	ehci_softc_t *sc = addr;
    727       1.190       mrg 	usbd_xfer_handle xfer;
    728         1.6  augustss 	u_char *p;
    729         1.6  augustss 	int i, m;
    730         1.6  augustss 
    731       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
    732       1.229     skrll 
    733       1.190       mrg 	mutex_enter(&sc->sc_lock);
    734       1.190       mrg 	xfer = sc->sc_intrxfer;
    735       1.190       mrg 
    736         1.6  augustss 	if (xfer == NULL) {
    737         1.6  augustss 		/* Just ignore the change. */
    738       1.190       mrg 		goto done;
    739         1.6  augustss 	}
    740         1.6  augustss 
    741   1.234.2.8     skrll 	p = xfer->ux_buf;
    742   1.234.2.8     skrll 	m = min(sc->sc_noport, xfer->ux_length * 8 - 1);
    743   1.234.2.8     skrll 	memset(p, 0, xfer->ux_length);
    744         1.6  augustss 	for (i = 1; i <= m; i++) {
    745         1.6  augustss 		/* Pick out CHANGE bits from the status reg. */
    746         1.6  augustss 		if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR)
    747         1.6  augustss 			p[i/8] |= 1 << (i%8);
    748       1.229     skrll 		if (i % 8 == 7)
    749       1.229     skrll 			USBHIST_LOG(ehcidebug, "change(%d)=0x%02x", i / 8,
    750       1.229     skrll 			    p[i/8], 0, 0);
    751         1.6  augustss 	}
    752   1.234.2.8     skrll 	xfer->ux_actlen = xfer->ux_length;
    753   1.234.2.8     skrll 	xfer->ux_status = USBD_NORMAL_COMPLETION;
    754         1.6  augustss 
    755         1.6  augustss 	usb_transfer_complete(xfer);
    756       1.190       mrg 
    757       1.190       mrg done:
    758       1.190       mrg 	mutex_exit(&sc->sc_lock);
    759         1.1  augustss }
    760         1.1  augustss 
    761       1.164  uebayasi Static void
    762         1.5  augustss ehci_softintr(void *v)
    763         1.5  augustss {
    764       1.134  drochner 	struct usbd_bus *bus = v;
    765   1.234.2.8     skrll 	ehci_softc_t *sc = bus->ub_hcpriv;
    766        1.53       chs 	struct ehci_xfer *ex, *nextex;
    767        1.18  augustss 
    768   1.234.2.8     skrll 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
    769       1.190       mrg 
    770       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
    771        1.18  augustss 
    772        1.18  augustss 	/*
    773        1.18  augustss 	 * The only explanation I can think of for why EHCI is as brain dead
    774        1.18  augustss 	 * as UHCI interrupt-wise is that Intel was involved in both.
    775        1.18  augustss 	 * An interrupt just tells us that something is done, we have no
    776        1.18  augustss 	 * clue what, so we need to scan through all active transfers. :-(
    777        1.18  augustss 	 */
    778       1.153  jmcneill 	for (ex = TAILQ_FIRST(&sc->sc_intrhead); ex; ex = nextex) {
    779  1.234.2.20     skrll 		nextex = TAILQ_NEXT(ex, ex_next);
    780        1.18  augustss 		ehci_check_intr(sc, ex);
    781        1.53       chs 	}
    782        1.18  augustss 
    783       1.108   xtraeme 	/* Schedule a callout to catch any dropped transactions. */
    784       1.108   xtraeme 	if ((sc->sc_flags & EHCIF_DROPPED_INTR_WORKAROUND) &&
    785       1.153  jmcneill 	    !TAILQ_EMPTY(&sc->sc_intrhead))
    786       1.190       mrg 		callout_reset(&sc->sc_tmo_intrlist,
    787       1.190       mrg 		    hz, ehci_intrlist_timeout, sc);
    788       1.108   xtraeme 
    789        1.29  augustss 	if (sc->sc_softwake) {
    790        1.29  augustss 		sc->sc_softwake = 0;
    791       1.190       mrg 		cv_broadcast(&sc->sc_softwake_cv);
    792        1.29  augustss 	}
    793        1.18  augustss }
    794        1.18  augustss 
    795        1.18  augustss /* Check for an interrupt. */
    796       1.164  uebayasi Static void
    797       1.115  christos ehci_check_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
    798        1.18  augustss {
    799  1.234.2.20     skrll 	usbd_device_handle dev = ex->ex_xfer.ux_pipe->up_dev;
    800       1.139  jmcneill 	int attr;
    801        1.18  augustss 
    802       1.229     skrll 	USBHIST_FUNC();	USBHIST_CALLED(ehcidebug);
    803       1.229     skrll 	USBHIST_LOG(ehcidebug, "ex = %p", ex, 0, 0, 0);
    804        1.18  augustss 
    805   1.234.2.8     skrll 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
    806       1.190       mrg 
    807  1.234.2.20     skrll 	attr = ex->ex_xfer.ux_pipe->up_endpoint->ue_edesc->bmAttributes;
    808   1.234.2.3     skrll 	if (UE_GET_XFERTYPE(attr) == UE_ISOCHRONOUS) {
    809   1.234.2.8     skrll 		if (dev->ud_speed == USB_SPEED_HIGH)
    810   1.234.2.3     skrll 			ehci_check_itd_intr(sc, ex);
    811   1.234.2.3     skrll 		else
    812   1.234.2.3     skrll 			ehci_check_sitd_intr(sc, ex);
    813   1.234.2.3     skrll 	} else
    814       1.139  jmcneill 		ehci_check_qh_intr(sc, ex);
    815       1.139  jmcneill 
    816       1.139  jmcneill 	return;
    817       1.139  jmcneill }
    818       1.139  jmcneill 
    819       1.164  uebayasi Static void
    820       1.139  jmcneill ehci_check_qh_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
    821       1.139  jmcneill {
    822       1.139  jmcneill 	ehci_soft_qtd_t *sqtd, *lsqtd;
    823   1.234.2.1     skrll 	uint32_t status;
    824       1.139  jmcneill 
    825       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
    826       1.229     skrll 
    827   1.234.2.8     skrll 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
    828       1.190       mrg 
    829  1.234.2.20     skrll 	if (ex->ex_sqtdstart == NULL) {
    830       1.139  jmcneill 		printf("ehci_check_qh_intr: not valid sqtd\n");
    831        1.18  augustss 		return;
    832        1.18  augustss 	}
    833       1.139  jmcneill 
    834  1.234.2.20     skrll 	lsqtd = ex->ex_sqtdend;
    835        1.18  augustss #ifdef DIAGNOSTIC
    836        1.18  augustss 	if (lsqtd == NULL) {
    837       1.139  jmcneill 		printf("ehci_check_qh_intr: lsqtd==0\n");
    838        1.18  augustss 		return;
    839        1.18  augustss 	}
    840        1.18  augustss #endif
    841        1.33  augustss 	/*
    842        1.18  augustss 	 * If the last TD is still active we need to check whether there
    843       1.210     skrll 	 * is an error somewhere in the middle, or whether there was a
    844        1.18  augustss 	 * short packet (SPD and not ACTIVE).
    845        1.18  augustss 	 */
    846       1.138    bouyer 	usb_syncmem(&lsqtd->dma,
    847       1.138    bouyer 	    lsqtd->offs + offsetof(ehci_qtd_t, qtd_status),
    848       1.138    bouyer 	    sizeof(lsqtd->qtd.qtd_status),
    849       1.138    bouyer 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
    850       1.205   tsutsui 	status = le32toh(lsqtd->qtd.qtd_status);
    851       1.205   tsutsui 	usb_syncmem(&lsqtd->dma,
    852       1.205   tsutsui 	    lsqtd->offs + offsetof(ehci_qtd_t, qtd_status),
    853       1.205   tsutsui 	    sizeof(lsqtd->qtd.qtd_status), BUS_DMASYNC_PREREAD);
    854       1.205   tsutsui 	if (status & EHCI_QTD_ACTIVE) {
    855       1.229     skrll 		USBHIST_LOGN(ehcidebug, 10, "active ex=%p", ex, 0, 0, 0);
    856  1.234.2.20     skrll 		for (sqtd = ex->ex_sqtdstart; sqtd != lsqtd;
    857  1.234.2.20     skrll 		     sqtd = sqtd->nextqtd) {
    858       1.138    bouyer 			usb_syncmem(&sqtd->dma,
    859       1.138    bouyer 			    sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
    860       1.138    bouyer 			    sizeof(sqtd->qtd.qtd_status),
    861       1.138    bouyer 			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
    862        1.18  augustss 			status = le32toh(sqtd->qtd.qtd_status);
    863       1.138    bouyer 			usb_syncmem(&sqtd->dma,
    864       1.138    bouyer 			    sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
    865       1.138    bouyer 			    sizeof(sqtd->qtd.qtd_status), BUS_DMASYNC_PREREAD);
    866        1.18  augustss 			/* If there's an active QTD the xfer isn't done. */
    867        1.18  augustss 			if (status & EHCI_QTD_ACTIVE)
    868        1.18  augustss 				break;
    869        1.18  augustss 			/* Any kind of error makes the xfer done. */
    870        1.18  augustss 			if (status & EHCI_QTD_HALTED)
    871        1.18  augustss 				goto done;
    872       1.221     skrll 			/* Handle short packets */
    873       1.221     skrll 			if (EHCI_QTD_GET_BYTES(status) != 0) {
    874  1.234.2.20     skrll 				usbd_pipe_handle pipe = ex->ex_xfer.ux_pipe;
    875       1.221     skrll 				usb_endpoint_descriptor_t *ed =
    876   1.234.2.8     skrll 				    pipe->up_endpoint->ue_edesc;
    877       1.221     skrll 				uint8_t xt = UE_GET_XFERTYPE(ed->bmAttributes);
    878       1.221     skrll 
    879       1.221     skrll 				/*
    880       1.221     skrll 				 * If we get here for a control transfer then
    881       1.221     skrll 				 * we need to let the hardware complete the
    882       1.221     skrll 				 * status phase.  That is, we're not done
    883       1.221     skrll 				 * quite yet.
    884       1.221     skrll 				 *
    885       1.221     skrll 				 * Otherwise, we're done.
    886       1.221     skrll 				 */
    887       1.221     skrll 				if (xt == UE_CONTROL) {
    888       1.221     skrll 					break;
    889       1.221     skrll 				}
    890        1.18  augustss 				goto done;
    891       1.221     skrll 			}
    892        1.18  augustss 		}
    893       1.229     skrll 		USBHIST_LOGN(ehcidebug, 10, "ex=%p std=%p still active",
    894  1.234.2.20     skrll 		    ex, ex->ex_sqtdstart, 0, 0);
    895        1.18  augustss 		return;
    896        1.18  augustss 	}
    897        1.18  augustss  done:
    898       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10, "ex=%p done", ex, 0, 0, 0);
    899  1.234.2.20     skrll 	callout_stop(&ex->ex_xfer.ux_callout);
    900        1.18  augustss 	ehci_idone(ex);
    901        1.18  augustss }
    902        1.18  augustss 
    903       1.164  uebayasi Static void
    904       1.190       mrg ehci_check_itd_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
    905       1.190       mrg {
    906       1.139  jmcneill 	ehci_soft_itd_t *itd;
    907       1.139  jmcneill 	int i;
    908       1.139  jmcneill 
    909       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
    910       1.229     skrll 
    911       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
    912       1.190       mrg 
    913  1.234.2.20     skrll 	if (&ex->ex_xfer != SIMPLEQ_FIRST(&ex->ex_xfer.ux_pipe->up_queue))
    914       1.153  jmcneill 		return;
    915       1.153  jmcneill 
    916  1.234.2.20     skrll 	if (ex->ex_itdstart == NULL) {
    917       1.139  jmcneill 		printf("ehci_check_itd_intr: not valid itd\n");
    918       1.139  jmcneill 		return;
    919       1.139  jmcneill 	}
    920       1.139  jmcneill 
    921  1.234.2.20     skrll 	itd = ex->ex_itdend;
    922       1.139  jmcneill #ifdef DIAGNOSTIC
    923       1.139  jmcneill 	if (itd == NULL) {
    924       1.139  jmcneill 		printf("ehci_check_itd_intr: itdend == 0\n");
    925       1.139  jmcneill 		return;
    926       1.139  jmcneill 	}
    927       1.139  jmcneill #endif
    928       1.139  jmcneill 
    929       1.139  jmcneill 	/*
    930       1.153  jmcneill 	 * check no active transfers in last itd, meaning we're finished
    931       1.139  jmcneill 	 */
    932       1.139  jmcneill 
    933       1.139  jmcneill 	usb_syncmem(&itd->dma, itd->offs + offsetof(ehci_itd_t, itd_ctl),
    934       1.139  jmcneill 		    sizeof(itd->itd.itd_ctl), BUS_DMASYNC_POSTWRITE |
    935       1.139  jmcneill 		    BUS_DMASYNC_POSTREAD);
    936       1.139  jmcneill 
    937       1.168  jakllsch 	for (i = 0; i < EHCI_ITD_NUFRAMES; i++) {
    938       1.139  jmcneill 		if (le32toh(itd->itd.itd_ctl[i]) & EHCI_ITD_ACTIVE)
    939       1.152  jmcneill 			break;
    940       1.139  jmcneill 	}
    941       1.139  jmcneill 
    942       1.168  jakllsch 	if (i == EHCI_ITD_NUFRAMES) {
    943       1.139  jmcneill 		goto done; /* All 8 descriptors inactive, it's done */
    944       1.139  jmcneill 	}
    945       1.139  jmcneill 
    946       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10, "ex %p itd %p still active", ex,
    947  1.234.2.20     skrll 	    ex->ex_itdstart, 0, 0);
    948       1.139  jmcneill 	return;
    949       1.139  jmcneill done:
    950       1.229     skrll 	USBHIST_LOG(ehcidebug, "ex %p done", ex, 0, 0, 0);
    951  1.234.2.20     skrll 	callout_stop(&ex->ex_xfer.ux_callout);
    952       1.139  jmcneill 	ehci_idone(ex);
    953       1.139  jmcneill }
    954       1.139  jmcneill 
    955   1.234.2.3     skrll void
    956   1.234.2.3     skrll ehci_check_sitd_intr(ehci_softc_t *sc, struct ehci_xfer *ex)
    957   1.234.2.3     skrll {
    958   1.234.2.3     skrll 	ehci_soft_sitd_t *sitd;
    959   1.234.2.3     skrll 
    960   1.234.2.3     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
    961   1.234.2.3     skrll 
    962   1.234.2.3     skrll 	KASSERT(mutex_owned(&sc->sc_lock));
    963   1.234.2.3     skrll 
    964  1.234.2.20     skrll 	if (&ex->ex_xfer != SIMPLEQ_FIRST(&ex->ex_xfer.ux_pipe->up_queue))
    965   1.234.2.3     skrll 		return;
    966   1.234.2.3     skrll 
    967  1.234.2.20     skrll 	if (ex->ex_sitdstart == NULL) {
    968   1.234.2.3     skrll 		printf("ehci_check_sitd_intr: not valid sitd\n");
    969   1.234.2.3     skrll 		return;
    970   1.234.2.3     skrll 	}
    971   1.234.2.3     skrll 
    972  1.234.2.20     skrll 	sitd = ex->ex_sitdend;
    973   1.234.2.3     skrll #ifdef DIAGNOSTIC
    974   1.234.2.3     skrll 	if (sitd == NULL) {
    975   1.234.2.3     skrll 		printf("ehci_check_sitd_intr: sitdend == 0\n");
    976   1.234.2.3     skrll 		return;
    977   1.234.2.3     skrll 	}
    978   1.234.2.3     skrll #endif
    979   1.234.2.3     skrll 
    980   1.234.2.3     skrll 	/*
    981   1.234.2.3     skrll 	 * check no active transfers in last sitd, meaning we're finished
    982   1.234.2.3     skrll 	 */
    983   1.234.2.3     skrll 
    984   1.234.2.3     skrll 	usb_syncmem(&sitd->dma, sitd->offs + offsetof(ehci_sitd_t, sitd_buffer),
    985   1.234.2.3     skrll 		    sizeof(sitd->sitd.sitd_buffer), BUS_DMASYNC_POSTWRITE |
    986   1.234.2.3     skrll 		    BUS_DMASYNC_POSTREAD);
    987   1.234.2.3     skrll 
    988   1.234.2.3     skrll 	if (le32toh(sitd->sitd.sitd_trans) & EHCI_SITD_ACTIVE)
    989   1.234.2.3     skrll 		return;
    990   1.234.2.3     skrll 
    991   1.234.2.3     skrll 	USBHIST_LOGN(ehcidebug, 10, "ex=%p done", ex, 0, 0, 0);
    992  1.234.2.20     skrll 	callout_stop(&(ex->ex_xfer.ux_callout));
    993   1.234.2.3     skrll 	ehci_idone(ex);
    994   1.234.2.3     skrll }
    995   1.234.2.3     skrll 
    996   1.234.2.3     skrll 
    997       1.164  uebayasi Static void
    998        1.18  augustss ehci_idone(struct ehci_xfer *ex)
    999        1.18  augustss {
   1000  1.234.2.20     skrll 	usbd_xfer_handle xfer = &ex->ex_xfer;
   1001   1.234.2.8     skrll 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->ux_pipe;
   1002   1.234.2.8     skrll 	struct ehci_softc *sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   1003        1.82  augustss 	ehci_soft_qtd_t *sqtd, *lsqtd;
   1004   1.234.2.1     skrll 	uint32_t status = 0, nstatus = 0;
   1005        1.18  augustss 	int actlen;
   1006        1.18  augustss 
   1007       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   1008       1.229     skrll 
   1009   1.234.2.8     skrll 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
   1010       1.190       mrg 
   1011       1.229     skrll 	USBHIST_LOG(ehcidebug, "ex=%p", ex, 0, 0, 0);
   1012       1.190       mrg 
   1013        1.18  augustss #ifdef DIAGNOSTIC
   1014  1.234.2.20     skrll 	if (ex->ex_isdone) {
   1015       1.217     skrll 		printf("ehci_idone: ex=%p is done!\n", ex);
   1016        1.18  augustss #ifdef EHCI_DEBUG
   1017       1.216     skrll 		ehci_dump_exfer(ex);
   1018        1.18  augustss #endif
   1019       1.216     skrll 		return;
   1020        1.18  augustss 	}
   1021  1.234.2.20     skrll 	ex->ex_isdone = 1;
   1022        1.18  augustss #endif
   1023       1.217     skrll 
   1024   1.234.2.8     skrll 	if (xfer->ux_status == USBD_CANCELLED ||
   1025   1.234.2.8     skrll 	    xfer->ux_status == USBD_TIMEOUT) {
   1026       1.229     skrll 		USBHIST_LOG(ehcidebug, "aborted xfer=%p", xfer, 0, 0, 0);
   1027        1.18  augustss 		return;
   1028        1.18  augustss 	}
   1029        1.18  augustss 
   1030       1.229     skrll 	USBHIST_LOG(ehcidebug, "xfer=%p, pipe=%p ready", xfer, epipe, 0, 0);
   1031        1.18  augustss #ifdef EHCI_DEBUG
   1032  1.234.2.20     skrll 	ehci_dump_sqtds(ex->ex_sqtdstart);
   1033        1.18  augustss #endif
   1034        1.18  augustss 
   1035        1.18  augustss 	/* The transfer is done, compute actual length and status. */
   1036       1.139  jmcneill 
   1037   1.234.2.3     skrll 	u_int xfertype, speed;
   1038   1.234.2.3     skrll 
   1039   1.234.2.8     skrll 	xfertype = UE_GET_XFERTYPE(xfer->ux_pipe->up_endpoint->ue_edesc->bmAttributes);
   1040   1.234.2.8     skrll 	speed = xfer->ux_pipe->up_dev->ud_speed;
   1041   1.234.2.3     skrll 	if (xfertype == UE_ISOCHRONOUS && speed == USB_SPEED_HIGH) {
   1042   1.234.2.3     skrll 		/* HS isoc transfer */
   1043   1.234.2.3     skrll 
   1044       1.139  jmcneill 		struct ehci_soft_itd *itd;
   1045       1.139  jmcneill 		int i, nframes, len, uframes;
   1046       1.139  jmcneill 
   1047       1.139  jmcneill 		nframes = 0;
   1048       1.139  jmcneill 		actlen = 0;
   1049       1.139  jmcneill 
   1050   1.234.2.8     skrll 		i = xfer->ux_pipe->up_endpoint->ue_edesc->bInterval;
   1051       1.168  jakllsch 		uframes = min(1 << (i - 1), USB_UFRAMES_PER_FRAME);
   1052       1.139  jmcneill 
   1053  1.234.2.20     skrll 		for (itd = ex->ex_itdstart; itd != NULL; itd = itd->xfer_next) {
   1054       1.139  jmcneill 			usb_syncmem(&itd->dma,itd->offs + offsetof(ehci_itd_t,itd_ctl),
   1055       1.139  jmcneill 			    sizeof(itd->itd.itd_ctl), BUS_DMASYNC_POSTWRITE |
   1056       1.139  jmcneill 			    BUS_DMASYNC_POSTREAD);
   1057       1.139  jmcneill 
   1058       1.168  jakllsch 			for (i = 0; i < EHCI_ITD_NUFRAMES; i += uframes) {
   1059       1.139  jmcneill 				/* XXX - driver didn't fill in the frame full
   1060       1.139  jmcneill 				 *   of uframes. This leads to scheduling
   1061       1.139  jmcneill 				 *   inefficiencies, but working around
   1062       1.139  jmcneill 				 *   this doubles complexity of tracking
   1063       1.139  jmcneill 				 *   an xfer.
   1064       1.139  jmcneill 				 */
   1065   1.234.2.8     skrll 				if (nframes >= xfer->ux_nframes)
   1066       1.139  jmcneill 					break;
   1067       1.139  jmcneill 
   1068       1.139  jmcneill 				status = le32toh(itd->itd.itd_ctl[i]);
   1069       1.139  jmcneill 				len = EHCI_ITD_GET_LEN(status);
   1070       1.155    jmorse 				if (EHCI_ITD_GET_STATUS(status) != 0)
   1071       1.155    jmorse 					len = 0; /*No valid data on error*/
   1072       1.155    jmorse 
   1073   1.234.2.8     skrll 				xfer->ux_frlengths[nframes++] = len;
   1074       1.139  jmcneill 				actlen += len;
   1075       1.139  jmcneill 			}
   1076       1.139  jmcneill 
   1077   1.234.2.8     skrll 			if (nframes >= xfer->ux_nframes)
   1078       1.139  jmcneill 				break;
   1079       1.183  jakllsch 	    	}
   1080       1.139  jmcneill 
   1081   1.234.2.8     skrll 		xfer->ux_actlen = actlen;
   1082   1.234.2.8     skrll 		xfer->ux_status = USBD_NORMAL_COMPLETION;
   1083       1.139  jmcneill 		goto end;
   1084       1.139  jmcneill 	}
   1085       1.139  jmcneill 
   1086   1.234.2.3     skrll 	if (xfertype == UE_ISOCHRONOUS && speed == USB_SPEED_FULL) {
   1087   1.234.2.3     skrll 		/* FS isoc transfer */
   1088   1.234.2.3     skrll 		struct ehci_soft_sitd *sitd;
   1089   1.234.2.3     skrll 		int nframes, len;
   1090   1.234.2.3     skrll 
   1091   1.234.2.3     skrll 		nframes = 0;
   1092   1.234.2.3     skrll 		actlen = 0;
   1093   1.234.2.3     skrll 
   1094  1.234.2.20     skrll 		for (sitd = ex->ex_sitdstart; sitd != NULL; sitd = sitd->xfer_next) {
   1095   1.234.2.3     skrll 			usb_syncmem(&sitd->dma,sitd->offs + offsetof(ehci_sitd_t, sitd_buffer),
   1096   1.234.2.3     skrll 			    sizeof(sitd->sitd.sitd_buffer), BUS_DMASYNC_POSTWRITE |
   1097   1.234.2.3     skrll 			    BUS_DMASYNC_POSTREAD);
   1098   1.234.2.3     skrll 
   1099   1.234.2.3     skrll 			/* XXX - driver didn't fill in the frame full
   1100   1.234.2.3     skrll 			 *   of uframes. This leads to scheduling
   1101   1.234.2.3     skrll 			 *   inefficiencies, but working around
   1102   1.234.2.3     skrll 			 *   this doubles complexity of tracking
   1103   1.234.2.3     skrll 			 *   an xfer.
   1104   1.234.2.3     skrll 			 */
   1105   1.234.2.8     skrll 			if (nframes >= xfer->ux_nframes)
   1106   1.234.2.3     skrll 				break;
   1107   1.234.2.3     skrll 
   1108   1.234.2.3     skrll 			status = le32toh(sitd->sitd.sitd_trans);
   1109   1.234.2.3     skrll 			len = EHCI_SITD_GET_LEN(status);
   1110   1.234.2.3     skrll 			if (status & (EHCI_SITD_ERR|EHCI_SITD_BUFERR|
   1111   1.234.2.3     skrll 			    EHCI_SITD_BABBLE|EHCI_SITD_XACTERR|EHCI_SITD_MISS)) {
   1112   1.234.2.3     skrll 				/* No valid data on error */
   1113   1.234.2.8     skrll 				len = xfer->ux_frlengths[nframes];
   1114   1.234.2.3     skrll 			}
   1115   1.234.2.3     skrll 
   1116   1.234.2.3     skrll 			/*
   1117   1.234.2.3     skrll 			 * frlengths[i]: # of bytes to send
   1118   1.234.2.3     skrll 			 * len: # of bytes host didn't send
   1119   1.234.2.3     skrll 			 */
   1120   1.234.2.8     skrll 			xfer->ux_frlengths[nframes] -= len;
   1121   1.234.2.3     skrll 			/* frlengths[i]: # of bytes host sent */
   1122   1.234.2.8     skrll 			actlen += xfer->ux_frlengths[nframes++];
   1123   1.234.2.3     skrll 
   1124   1.234.2.8     skrll 			if (nframes >= xfer->ux_nframes)
   1125   1.234.2.3     skrll 				break;
   1126   1.234.2.3     skrll 	    	}
   1127   1.234.2.3     skrll 
   1128   1.234.2.8     skrll 		xfer->ux_actlen = actlen;
   1129   1.234.2.8     skrll 		xfer->ux_status = USBD_NORMAL_COMPLETION;
   1130   1.234.2.3     skrll 		goto end;
   1131   1.234.2.3     skrll 	}
   1132  1.234.2.19     skrll 	KASSERT(xfertype != UE_ISOCHRONOUS);
   1133   1.234.2.3     skrll 
   1134       1.139  jmcneill 	/* Continue processing xfers using queue heads */
   1135       1.139  jmcneill 
   1136  1.234.2.20     skrll 	lsqtd = ex->ex_sqtdend;
   1137        1.18  augustss 	actlen = 0;
   1138  1.234.2.20     skrll 	for (sqtd = ex->ex_sqtdstart; sqtd != lsqtd->nextqtd;
   1139       1.234     skrll 	     sqtd = sqtd->nextqtd) {
   1140       1.138    bouyer 		usb_syncmem(&sqtd->dma, sqtd->offs, sizeof(sqtd->qtd),
   1141       1.138    bouyer 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   1142        1.18  augustss 		nstatus = le32toh(sqtd->qtd.qtd_status);
   1143        1.18  augustss 		if (nstatus & EHCI_QTD_ACTIVE)
   1144        1.18  augustss 			break;
   1145        1.18  augustss 
   1146        1.18  augustss 		status = nstatus;
   1147       1.139  jmcneill 		if (EHCI_QTD_GET_PID(status) != EHCI_QTD_PID_SETUP)
   1148        1.18  augustss 			actlen += sqtd->len - EHCI_QTD_GET_BYTES(status);
   1149        1.18  augustss 	}
   1150        1.22  augustss 
   1151       1.139  jmcneill 
   1152        1.91     perry 	/*
   1153        1.86  augustss 	 * If there are left over TDs we need to update the toggle.
   1154        1.86  augustss 	 * The default pipe doesn't need it since control transfers
   1155        1.86  augustss 	 * start the toggle at 0 every time.
   1156       1.117  drochner 	 * For a short transfer we need to update the toggle for the missing
   1157       1.117  drochner 	 * packets within the qTD.
   1158        1.86  augustss 	 */
   1159       1.117  drochner 	if ((sqtd != lsqtd->nextqtd || EHCI_QTD_GET_BYTES(status)) &&
   1160   1.234.2.8     skrll 	    xfer->ux_pipe->up_dev->ud_pipe0 != xfer->ux_pipe) {
   1161       1.229     skrll 		USBHIST_LOG(ehcidebug,
   1162       1.229     skrll 		    "toggle update status=0x%08x nstatus=0x%08x",
   1163       1.229     skrll 		    status, nstatus, 0, 0);
   1164        1.58   mycroft #if 0
   1165        1.58   mycroft 		ehci_dump_sqh(epipe->sqh);
   1166  1.234.2.20     skrll 		ehci_dump_sqtds(ex->ex_sqtdstart);
   1167        1.58   mycroft #endif
   1168        1.58   mycroft 		epipe->nexttoggle = EHCI_QTD_GET_TOGGLE(nstatus);
   1169        1.22  augustss 	}
   1170        1.18  augustss 
   1171   1.234.2.8     skrll 	USBHIST_LOG(ehcidebug, "len=%d actlen=%d status=0x%08x", xfer->ux_length,
   1172       1.229     skrll 	    actlen, status, 0);
   1173   1.234.2.8     skrll 	xfer->ux_actlen = actlen;
   1174        1.98  augustss 	if (status & EHCI_QTD_HALTED) {
   1175        1.18  augustss #ifdef EHCI_DEBUG
   1176       1.229     skrll 		USBHIST_LOG(ehcidebug, "halted addr=%d endpt=0x%02x",
   1177   1.234.2.8     skrll 		   xfer->ux_pipe->up_dev->ud_addr,
   1178   1.234.2.8     skrll 		   xfer->ux_pipe->up_endpoint->ue_edesc->bEndpointAddress, 0, 0);
   1179       1.229     skrll 		USBHIST_LOG(ehcidebug, "cerr=%d pid=%d stat=%#x",
   1180       1.229     skrll 		   EHCI_QTD_GET_CERR(status), EHCI_QTD_GET_PID(status),
   1181       1.229     skrll 		   status, 0);
   1182       1.229     skrll 		USBHIST_LOG(ehcidebug,
   1183       1.233     skrll 		    "active =%d halted=%d buferr=%d babble=%d",
   1184       1.229     skrll 		    status & EHCI_QTD_ACTIVE ? 1 : 0,
   1185       1.229     skrll 		    status & EHCI_QTD_HALTED ? 1 : 0,
   1186       1.229     skrll 		    status & EHCI_QTD_BUFERR ? 1 : 0,
   1187       1.229     skrll 		    status & EHCI_QTD_BABBLE ? 1 : 0);
   1188       1.229     skrll 
   1189       1.229     skrll 		USBHIST_LOG(ehcidebug,
   1190       1.233     skrll 		    "xacterr=%d missed=%d split =%d ping  =%d",
   1191       1.229     skrll 		    status & EHCI_QTD_XACTERR ? 1 : 0,
   1192       1.229     skrll 		    status & EHCI_QTD_MISSEDMICRO ? 1 : 0,
   1193       1.229     skrll 		    status & EHCI_QTD_SPLITXSTATE ? 1 : 0,
   1194       1.229     skrll 		    status & EHCI_QTD_PINGSTATE ? 1 : 0);
   1195       1.218     skrll 
   1196       1.229     skrll 		ehci_dump_sqh(epipe->sqh);
   1197  1.234.2.20     skrll 		ehci_dump_sqtds(ex->ex_sqtdstart);
   1198        1.18  augustss #endif
   1199        1.98  augustss 		/* low&full speed has an extra error flag */
   1200        1.98  augustss 		if (EHCI_QH_GET_EPS(epipe->sqh->qh.qh_endp) !=
   1201        1.98  augustss 		    EHCI_QH_SPEED_HIGH)
   1202        1.98  augustss 			status &= EHCI_QTD_STATERRS | EHCI_QTD_PINGSTATE;
   1203        1.98  augustss 		else
   1204        1.98  augustss 			status &= EHCI_QTD_STATERRS;
   1205       1.139  jmcneill 		if (status == 0) /* no other errors means a stall */ {
   1206   1.234.2.8     skrll 			xfer->ux_status = USBD_STALLED;
   1207       1.139  jmcneill 		} else {
   1208   1.234.2.8     skrll 			xfer->ux_status = USBD_IOERROR; /* more info XXX */
   1209       1.139  jmcneill 		}
   1210        1.98  augustss 		/* XXX need to reset TT on missed microframe */
   1211        1.98  augustss 		if (status & EHCI_QTD_MISSEDMICRO) {
   1212        1.98  augustss 			printf("%s: missed microframe, TT reset not "
   1213        1.98  augustss 			    "implemented, hub might be inoperational\n",
   1214       1.134  drochner 			    device_xname(sc->sc_dev));
   1215        1.98  augustss 		}
   1216        1.18  augustss 	} else {
   1217   1.234.2.8     skrll 		xfer->ux_status = USBD_NORMAL_COMPLETION;
   1218        1.18  augustss 	}
   1219        1.18  augustss 
   1220       1.139  jmcneill     end:
   1221       1.139  jmcneill 	/* XXX transfer_complete memcpys out transfer data (for in endpoints)
   1222       1.139  jmcneill 	 * during this call, before methods->done is called: dma sync required
   1223       1.139  jmcneill 	 * beforehand? */
   1224        1.18  augustss 	usb_transfer_complete(xfer);
   1225       1.229     skrll 	USBHIST_LOG(ehcidebug, "ex=%p done", ex, 0, 0, 0);
   1226         1.5  augustss }
   1227         1.5  augustss 
   1228        1.15  augustss /*
   1229        1.15  augustss  * Wait here until controller claims to have an interrupt.
   1230        1.18  augustss  * Then call ehci_intr and return.  Use timeout to avoid waiting
   1231        1.15  augustss  * too long.
   1232        1.15  augustss  */
   1233       1.164  uebayasi Static void
   1234        1.15  augustss ehci_waitintr(ehci_softc_t *sc, usbd_xfer_handle xfer)
   1235        1.15  augustss {
   1236        1.97  augustss 	int timo;
   1237   1.234.2.1     skrll 	uint32_t intrs;
   1238        1.15  augustss 
   1239       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   1240       1.229     skrll 
   1241   1.234.2.8     skrll 	xfer->ux_status = USBD_IN_PROGRESS;
   1242   1.234.2.8     skrll 	for (timo = xfer->ux_timeout; timo >= 0; timo--) {
   1243        1.15  augustss 		usb_delay_ms(&sc->sc_bus, 1);
   1244        1.17  augustss 		if (sc->sc_dying)
   1245        1.17  augustss 			break;
   1246        1.15  augustss 		intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)) &
   1247        1.15  augustss 			sc->sc_eintrs;
   1248       1.229     skrll 		USBHIST_LOG(ehcidebug, "0x%04x", intrs, 0, 0, 0);
   1249        1.70      yamt #ifdef EHCI_DEBUG
   1250        1.15  augustss 		if (ehcidebug > 15)
   1251        1.18  augustss 			ehci_dump_regs(sc);
   1252        1.15  augustss #endif
   1253        1.15  augustss 		if (intrs) {
   1254       1.190       mrg 			mutex_spin_enter(&sc->sc_intr_lock);
   1255        1.15  augustss 			ehci_intr1(sc);
   1256       1.190       mrg 			mutex_spin_exit(&sc->sc_intr_lock);
   1257   1.234.2.8     skrll 			if (xfer->ux_status != USBD_IN_PROGRESS)
   1258        1.15  augustss 				return;
   1259        1.15  augustss 		}
   1260        1.15  augustss 	}
   1261        1.15  augustss 
   1262        1.15  augustss 	/* Timeout */
   1263       1.229     skrll 	USBHIST_LOG(ehcidebug, "timeout", 0, 0, 0, 0);
   1264   1.234.2.8     skrll 	xfer->ux_status = USBD_TIMEOUT;
   1265       1.190       mrg 	mutex_enter(&sc->sc_lock);
   1266        1.15  augustss 	usb_transfer_complete(xfer);
   1267       1.190       mrg 	mutex_exit(&sc->sc_lock);
   1268        1.15  augustss 	/* XXX should free TD */
   1269        1.15  augustss }
   1270        1.15  augustss 
   1271       1.164  uebayasi Static void
   1272         1.5  augustss ehci_poll(struct usbd_bus *bus)
   1273         1.5  augustss {
   1274   1.234.2.8     skrll 	ehci_softc_t *sc = bus->ub_hcpriv;
   1275       1.229     skrll 
   1276       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   1277       1.229     skrll 
   1278         1.5  augustss #ifdef EHCI_DEBUG
   1279         1.5  augustss 	static int last;
   1280         1.5  augustss 	int new;
   1281         1.6  augustss 	new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS));
   1282         1.5  augustss 	if (new != last) {
   1283       1.229     skrll 		USBHIST_LOG(ehcidebug, "intrs=0x%04x", new, 0, 0, 0);
   1284         1.5  augustss 		last = new;
   1285         1.5  augustss 	}
   1286         1.5  augustss #endif
   1287         1.5  augustss 
   1288       1.190       mrg 	if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs) {
   1289       1.190       mrg 		mutex_spin_enter(&sc->sc_intr_lock);
   1290         1.5  augustss 		ehci_intr1(sc);
   1291       1.190       mrg 		mutex_spin_exit(&sc->sc_intr_lock);
   1292       1.190       mrg 	}
   1293         1.5  augustss }
   1294         1.5  augustss 
   1295       1.132    dyoung void
   1296       1.132    dyoung ehci_childdet(device_t self, device_t child)
   1297       1.132    dyoung {
   1298       1.132    dyoung 	struct ehci_softc *sc = device_private(self);
   1299       1.132    dyoung 
   1300       1.132    dyoung 	KASSERT(sc->sc_child == child);
   1301       1.132    dyoung 	sc->sc_child = NULL;
   1302       1.132    dyoung }
   1303       1.132    dyoung 
   1304         1.1  augustss int
   1305         1.1  augustss ehci_detach(struct ehci_softc *sc, int flags)
   1306         1.1  augustss {
   1307         1.1  augustss 	int rv = 0;
   1308         1.1  augustss 
   1309       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   1310       1.229     skrll 
   1311         1.1  augustss 	if (sc->sc_child != NULL)
   1312         1.1  augustss 		rv = config_detach(sc->sc_child, flags);
   1313        1.33  augustss 
   1314         1.1  augustss 	if (rv != 0)
   1315  1.234.2.14     skrll 		return rv;
   1316         1.1  augustss 
   1317       1.190       mrg 	callout_halt(&sc->sc_tmo_intrlist, NULL);
   1318       1.190       mrg 	callout_destroy(&sc->sc_tmo_intrlist);
   1319       1.190       mrg 
   1320       1.190       mrg 	/* XXX free other data structures XXX */
   1321       1.190       mrg 	if (sc->sc_softitds)
   1322       1.190       mrg 		kmem_free(sc->sc_softitds,
   1323       1.190       mrg 		    sc->sc_flsize * sizeof(ehci_soft_itd_t *));
   1324       1.190       mrg 	cv_destroy(&sc->sc_doorbell);
   1325       1.190       mrg 	cv_destroy(&sc->sc_softwake_cv);
   1326       1.190       mrg 
   1327       1.190       mrg #if 0
   1328       1.190       mrg 	/* XXX destroyed in ehci_pci.c as it controls ehci_intr access */
   1329         1.6  augustss 
   1330       1.190       mrg 	softint_disestablish(sc->sc_doorbell_si);
   1331       1.190       mrg 	softint_disestablish(sc->sc_pcd_si);
   1332        1.15  augustss 
   1333       1.190       mrg 	mutex_destroy(&sc->sc_lock);
   1334       1.190       mrg 	mutex_destroy(&sc->sc_intr_lock);
   1335       1.190       mrg #endif
   1336       1.190       mrg 
   1337       1.204  christos 	pool_cache_destroy(sc->sc_xferpool);
   1338         1.1  augustss 
   1339       1.128  jmcneill 	EOWRITE4(sc, EHCI_CONFIGFLAG, 0);
   1340       1.128  jmcneill 
   1341  1.234.2.14     skrll 	return rv;
   1342         1.1  augustss }
   1343         1.1  augustss 
   1344         1.1  augustss 
   1345         1.1  augustss int
   1346       1.132    dyoung ehci_activate(device_t self, enum devact act)
   1347         1.1  augustss {
   1348       1.132    dyoung 	struct ehci_softc *sc = device_private(self);
   1349         1.1  augustss 
   1350         1.1  augustss 	switch (act) {
   1351         1.1  augustss 	case DVACT_DEACTIVATE:
   1352       1.124  kiyohara 		sc->sc_dying = 1;
   1353       1.163    dyoung 		return 0;
   1354       1.163    dyoung 	default:
   1355       1.163    dyoung 		return EOPNOTSUPP;
   1356         1.1  augustss 	}
   1357         1.1  augustss }
   1358         1.1  augustss 
   1359         1.5  augustss /*
   1360         1.5  augustss  * Handle suspend/resume.
   1361         1.5  augustss  *
   1362         1.5  augustss  * We need to switch to polling mode here, because this routine is
   1363        1.73  augustss  * called from an interrupt context.  This is all right since we
   1364         1.5  augustss  * are almost suspended anyway.
   1365       1.127  jmcneill  *
   1366       1.127  jmcneill  * Note that this power handler isn't to be registered directly; the
   1367       1.127  jmcneill  * bus glue needs to call out to it.
   1368         1.5  augustss  */
   1369       1.127  jmcneill bool
   1370       1.166    dyoung ehci_suspend(device_t dv, const pmf_qual_t *qual)
   1371         1.5  augustss {
   1372       1.132    dyoung 	ehci_softc_t *sc = device_private(dv);
   1373       1.190       mrg 	int i;
   1374       1.127  jmcneill 	uint32_t cmd, hcr;
   1375       1.127  jmcneill 
   1376       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   1377       1.229     skrll 
   1378       1.190       mrg 	mutex_spin_enter(&sc->sc_intr_lock);
   1379   1.234.2.8     skrll 	sc->sc_bus.ub_usepolling++;
   1380       1.190       mrg 	mutex_spin_exit(&sc->sc_intr_lock);
   1381       1.127  jmcneill 
   1382       1.127  jmcneill 	for (i = 1; i <= sc->sc_noport; i++) {
   1383       1.129  jmcneill 		cmd = EOREAD4(sc, EHCI_PORTSC(i)) & ~EHCI_PS_CLEAR;
   1384       1.127  jmcneill 		if ((cmd & EHCI_PS_PO) == 0 && (cmd & EHCI_PS_PE) == EHCI_PS_PE)
   1385       1.127  jmcneill 			EOWRITE4(sc, EHCI_PORTSC(i), cmd | EHCI_PS_SUSP);
   1386       1.127  jmcneill 	}
   1387       1.127  jmcneill 
   1388       1.127  jmcneill 	sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD);
   1389       1.127  jmcneill 
   1390       1.127  jmcneill 	cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE);
   1391       1.127  jmcneill 	EOWRITE4(sc, EHCI_USBCMD, cmd);
   1392       1.127  jmcneill 
   1393       1.127  jmcneill 	for (i = 0; i < 100; i++) {
   1394       1.127  jmcneill 		hcr = EOREAD4(sc, EHCI_USBSTS) & (EHCI_STS_ASS | EHCI_STS_PSS);
   1395       1.127  jmcneill 		if (hcr == 0)
   1396       1.127  jmcneill 			break;
   1397         1.5  augustss 
   1398       1.127  jmcneill 		usb_delay_ms(&sc->sc_bus, 1);
   1399       1.127  jmcneill 	}
   1400       1.127  jmcneill 	if (hcr != 0)
   1401       1.134  drochner 		printf("%s: reset timeout\n", device_xname(dv));
   1402         1.5  augustss 
   1403       1.127  jmcneill 	cmd &= ~EHCI_CMD_RS;
   1404       1.127  jmcneill 	EOWRITE4(sc, EHCI_USBCMD, cmd);
   1405        1.74  augustss 
   1406       1.127  jmcneill 	for (i = 0; i < 100; i++) {
   1407       1.127  jmcneill 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
   1408       1.127  jmcneill 		if (hcr == EHCI_STS_HCH)
   1409       1.127  jmcneill 			break;
   1410        1.74  augustss 
   1411       1.127  jmcneill 		usb_delay_ms(&sc->sc_bus, 1);
   1412       1.127  jmcneill 	}
   1413       1.127  jmcneill 	if (hcr != EHCI_STS_HCH)
   1414       1.134  drochner 		printf("%s: config timeout\n", device_xname(dv));
   1415        1.74  augustss 
   1416       1.190       mrg 	mutex_spin_enter(&sc->sc_intr_lock);
   1417   1.234.2.8     skrll 	sc->sc_bus.ub_usepolling--;
   1418       1.190       mrg 	mutex_spin_exit(&sc->sc_intr_lock);
   1419        1.74  augustss 
   1420       1.127  jmcneill 	return true;
   1421       1.127  jmcneill }
   1422        1.74  augustss 
   1423       1.127  jmcneill bool
   1424       1.166    dyoung ehci_resume(device_t dv, const pmf_qual_t *qual)
   1425       1.127  jmcneill {
   1426       1.132    dyoung 	ehci_softc_t *sc = device_private(dv);
   1427       1.132    dyoung 	int i;
   1428       1.127  jmcneill 	uint32_t cmd, hcr;
   1429        1.74  augustss 
   1430       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   1431       1.229     skrll 
   1432       1.127  jmcneill 	/* restore things in case the bios sucks */
   1433       1.127  jmcneill 	EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0);
   1434       1.127  jmcneill 	EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0));
   1435       1.127  jmcneill 	EOWRITE4(sc, EHCI_ASYNCLISTADDR,
   1436       1.127  jmcneill 	    sc->sc_async_head->physaddr | EHCI_LINK_QH);
   1437       1.130  jmcneill 
   1438       1.130  jmcneill 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs & ~EHCI_INTR_PCIE);
   1439        1.74  augustss 
   1440       1.127  jmcneill 	EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
   1441        1.74  augustss 
   1442       1.127  jmcneill 	hcr = 0;
   1443       1.127  jmcneill 	for (i = 1; i <= sc->sc_noport; i++) {
   1444       1.129  jmcneill 		cmd = EOREAD4(sc, EHCI_PORTSC(i)) & ~EHCI_PS_CLEAR;
   1445       1.127  jmcneill 		if ((cmd & EHCI_PS_PO) == 0 &&
   1446       1.127  jmcneill 		    (cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP) {
   1447       1.127  jmcneill 			EOWRITE4(sc, EHCI_PORTSC(i), cmd | EHCI_PS_FPR);
   1448       1.127  jmcneill 			hcr = 1;
   1449        1.74  augustss 		}
   1450       1.127  jmcneill 	}
   1451       1.127  jmcneill 
   1452       1.127  jmcneill 	if (hcr) {
   1453       1.127  jmcneill 		usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
   1454       1.127  jmcneill 
   1455       1.127  jmcneill 		for (i = 1; i <= sc->sc_noport; i++) {
   1456       1.129  jmcneill 			cmd = EOREAD4(sc, EHCI_PORTSC(i)) & ~EHCI_PS_CLEAR;
   1457       1.127  jmcneill 			if ((cmd & EHCI_PS_PO) == 0 &&
   1458       1.127  jmcneill 			    (cmd & EHCI_PS_SUSP) == EHCI_PS_SUSP)
   1459       1.127  jmcneill 				EOWRITE4(sc, EHCI_PORTSC(i),
   1460       1.127  jmcneill 				    cmd & ~EHCI_PS_FPR);
   1461        1.74  augustss 		}
   1462       1.127  jmcneill 	}
   1463       1.127  jmcneill 
   1464       1.127  jmcneill 	EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd);
   1465       1.130  jmcneill 	EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs);
   1466        1.74  augustss 
   1467       1.127  jmcneill 	for (i = 0; i < 100; i++) {
   1468       1.127  jmcneill 		hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH;
   1469       1.127  jmcneill 		if (hcr != EHCI_STS_HCH)
   1470       1.127  jmcneill 			break;
   1471        1.74  augustss 
   1472       1.127  jmcneill 		usb_delay_ms(&sc->sc_bus, 1);
   1473         1.5  augustss 	}
   1474       1.127  jmcneill 	if (hcr == EHCI_STS_HCH)
   1475       1.134  drochner 		printf("%s: config timeout\n", device_xname(dv));
   1476       1.127  jmcneill 
   1477       1.127  jmcneill 	return true;
   1478         1.5  augustss }
   1479         1.5  augustss 
   1480         1.5  augustss /*
   1481         1.5  augustss  * Shut down the controller when the system is going down.
   1482         1.5  augustss  */
   1483       1.133    dyoung bool
   1484       1.133    dyoung ehci_shutdown(device_t self, int flags)
   1485         1.5  augustss {
   1486       1.133    dyoung 	ehci_softc_t *sc = device_private(self);
   1487         1.5  augustss 
   1488       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   1489       1.229     skrll 
   1490         1.8  augustss 	EOWRITE4(sc, EHCI_USBCMD, 0);	/* Halt controller */
   1491         1.8  augustss 	EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET);
   1492       1.133    dyoung 	return true;
   1493         1.5  augustss }
   1494         1.5  augustss 
   1495       1.164  uebayasi Static usbd_xfer_handle
   1496         1.5  augustss ehci_allocx(struct usbd_bus *bus)
   1497         1.5  augustss {
   1498   1.234.2.8     skrll 	struct ehci_softc *sc = bus->ub_hcpriv;
   1499         1.5  augustss 	usbd_xfer_handle xfer;
   1500         1.5  augustss 
   1501       1.204  christos 	xfer = pool_cache_get(sc->sc_xferpool, PR_NOWAIT);
   1502        1.18  augustss 	if (xfer != NULL) {
   1503       1.177   tsutsui 		memset(xfer, 0, sizeof(struct ehci_xfer));
   1504        1.18  augustss #ifdef DIAGNOSTIC
   1505  1.234.2.20     skrll 		EXFER(xfer)->ex_isdone = 1;
   1506   1.234.2.8     skrll 		xfer->ux_state = XFER_BUSY;
   1507        1.18  augustss #endif
   1508        1.18  augustss 	}
   1509  1.234.2.14     skrll 	return xfer;
   1510         1.5  augustss }
   1511         1.5  augustss 
   1512       1.164  uebayasi Static void
   1513         1.5  augustss ehci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
   1514         1.5  augustss {
   1515   1.234.2.8     skrll 	struct ehci_softc *sc = bus->ub_hcpriv;
   1516         1.5  augustss 
   1517        1.18  augustss #ifdef DIAGNOSTIC
   1518   1.234.2.8     skrll 	if (xfer->ux_state != XFER_BUSY) {
   1519        1.18  augustss 		printf("ehci_freex: xfer=%p not busy, 0x%08x\n", xfer,
   1520   1.234.2.8     skrll 		       xfer->ux_state);
   1521        1.18  augustss 	}
   1522   1.234.2.8     skrll 	xfer->ux_state = XFER_FREE;
   1523  1.234.2.20     skrll 	if (!EXFER(xfer)->ex_isdone) {
   1524        1.18  augustss 		printf("ehci_freex: !isdone\n");
   1525        1.18  augustss 	}
   1526        1.18  augustss #endif
   1527       1.204  christos 	pool_cache_put(sc->sc_xferpool, xfer);
   1528         1.5  augustss }
   1529         1.5  augustss 
   1530         1.5  augustss Static void
   1531       1.190       mrg ehci_get_lock(struct usbd_bus *bus, kmutex_t **lock)
   1532       1.190       mrg {
   1533   1.234.2.8     skrll 	struct ehci_softc *sc = bus->ub_hcpriv;
   1534       1.190       mrg 
   1535       1.190       mrg 	*lock = &sc->sc_lock;
   1536       1.190       mrg }
   1537       1.190       mrg 
   1538       1.190       mrg Static void
   1539         1.5  augustss ehci_device_clear_toggle(usbd_pipe_handle pipe)
   1540         1.5  augustss {
   1541        1.15  augustss 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
   1542        1.15  augustss 
   1543       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   1544       1.229     skrll 
   1545       1.229     skrll 	USBHIST_LOG(ehcidebug, "epipe=%p status=0x%08x",
   1546       1.229     skrll 	    epipe, epipe->sqh->qh.qh_qtd.qtd_status, 0, 0);
   1547       1.158    sketch #ifdef EHCI_DEBUG
   1548        1.22  augustss 	if (ehcidebug)
   1549        1.22  augustss 		usbd_dump_pipe(pipe);
   1550         1.5  augustss #endif
   1551        1.55   mycroft 	epipe->nexttoggle = 0;
   1552         1.5  augustss }
   1553         1.5  augustss 
   1554         1.5  augustss Static void
   1555       1.115  christos ehci_noop(usbd_pipe_handle pipe)
   1556         1.5  augustss {
   1557         1.5  augustss }
   1558         1.5  augustss 
   1559         1.5  augustss #ifdef EHCI_DEBUG
   1560        1.40    martin /*
   1561        1.40    martin  * Unused function - this is meant to be called from a kernel
   1562        1.40    martin  * debugger.
   1563        1.40    martin  */
   1564        1.39    martin void
   1565       1.157    cegger ehci_dump(void)
   1566        1.39    martin {
   1567       1.229     skrll 	ehci_softc_t *sc = theehci;
   1568       1.229     skrll 	int i;
   1569       1.229     skrll 	printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n",
   1570       1.229     skrll 	    EOREAD4(sc, EHCI_USBCMD),
   1571       1.229     skrll 	    EOREAD4(sc, EHCI_USBSTS),
   1572       1.229     skrll 	    EOREAD4(sc, EHCI_USBINTR));
   1573       1.229     skrll 	printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n",
   1574       1.229     skrll 	    EOREAD4(sc, EHCI_FRINDEX),
   1575       1.229     skrll 	    EOREAD4(sc, EHCI_CTRLDSSEGMENT),
   1576       1.229     skrll 	    EOREAD4(sc, EHCI_PERIODICLISTBASE),
   1577       1.229     skrll 	    EOREAD4(sc, EHCI_ASYNCLISTADDR));
   1578       1.229     skrll 	for (i = 1; i <= sc->sc_noport; i++)
   1579       1.229     skrll 		printf("port %d status=0x%08x\n", i,
   1580       1.229     skrll 		    EOREAD4(sc, EHCI_PORTSC(i)));
   1581         1.6  augustss }
   1582         1.6  augustss 
   1583       1.164  uebayasi Static void
   1584       1.229     skrll ehci_dump_regs(ehci_softc_t *sc)
   1585         1.9  augustss {
   1586       1.229     skrll 	int i;
   1587       1.229     skrll 
   1588       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   1589       1.229     skrll 
   1590       1.229     skrll 	USBHIST_LOG(ehcidebug,
   1591       1.229     skrll 	    "cmd     = 0x%08x  sts      = 0x%08x  ien      = 0x%08x",
   1592       1.229     skrll 	    EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS),
   1593       1.229     skrll 	    EOREAD4(sc, EHCI_USBINTR), 0);
   1594       1.229     skrll 	USBHIST_LOG(ehcidebug,
   1595       1.229     skrll 	    "frindex = 0x%08x  ctrdsegm = 0x%08x  periodic = 0x%08x  "
   1596       1.229     skrll 	    "async   = 0x%08x",
   1597       1.229     skrll 	    EOREAD4(sc, EHCI_FRINDEX), EOREAD4(sc, EHCI_CTRLDSSEGMENT),
   1598       1.229     skrll 	    EOREAD4(sc, EHCI_PERIODICLISTBASE),
   1599       1.229     skrll 	    EOREAD4(sc, EHCI_ASYNCLISTADDR));
   1600       1.229     skrll 	for (i = 1; i <= sc->sc_noport; i += 2) {
   1601       1.229     skrll 		if (i == sc->sc_noport) {
   1602       1.229     skrll 			USBHIST_LOG(ehcidebug,
   1603       1.229     skrll 			    "port %d status = 0x%08x", i,
   1604       1.229     skrll 			    EOREAD4(sc, EHCI_PORTSC(i)), 0, 0);
   1605       1.229     skrll 		} else {
   1606       1.229     skrll 			USBHIST_LOG(ehcidebug,
   1607       1.229     skrll 			    "port %d status = 0x%08x  port %d status = 0x%08x",
   1608       1.229     skrll 			    i, EOREAD4(sc, EHCI_PORTSC(i)),
   1609       1.229     skrll 			    i+1, EOREAD4(sc, EHCI_PORTSC(i+1)));
   1610        1.15  augustss 		}
   1611        1.15  augustss 	}
   1612        1.15  augustss }
   1613        1.15  augustss 
   1614       1.229     skrll #ifdef EHCI_DEBUG
   1615       1.229     skrll #define ehci_dump_link(link, type) do {					\
   1616       1.229     skrll 	USBHIST_LOG(ehcidebug, "    link 0x%08x (T = %d):",		\
   1617       1.229     skrll 	    link,							\
   1618       1.229     skrll 	    link & EHCI_LINK_TERMINATE ? 1 : 0, 0, 0);			\
   1619       1.229     skrll 	if (type) {							\
   1620       1.229     skrll 		USBHIST_LOG(ehcidebug,					\
   1621       1.229     skrll 		    "        ITD  = %d  QH   = %d  SITD = %d  FSTN = %d",\
   1622       1.229     skrll 		    EHCI_LINK_TYPE(link) == EHCI_LINK_ITD ? 1 : 0,	\
   1623       1.229     skrll 		    EHCI_LINK_TYPE(link) == EHCI_LINK_QH ? 1 : 0,	\
   1624       1.229     skrll 		    EHCI_LINK_TYPE(link) == EHCI_LINK_SITD ? 1 : 0,	\
   1625       1.229     skrll 		    EHCI_LINK_TYPE(link) == EHCI_LINK_FSTN ? 1 : 0);	\
   1626       1.229     skrll 	}								\
   1627       1.229     skrll } while(0)
   1628       1.229     skrll #else
   1629       1.229     skrll #define ehci_dump_link(link, type)
   1630       1.229     skrll #endif
   1631       1.229     skrll 
   1632       1.164  uebayasi Static void
   1633        1.15  augustss ehci_dump_sqtds(ehci_soft_qtd_t *sqtd)
   1634        1.15  augustss {
   1635       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   1636        1.29  augustss 	int i;
   1637       1.229     skrll 	uint32_t stop = 0;
   1638        1.29  augustss 
   1639        1.29  augustss 	for (i = 0; sqtd && i < 20 && !stop; sqtd = sqtd->nextqtd, i++) {
   1640        1.15  augustss 		ehci_dump_sqtd(sqtd);
   1641       1.138    bouyer 		usb_syncmem(&sqtd->dma,
   1642       1.195  christos 		    sqtd->offs + offsetof(ehci_qtd_t, qtd_next),
   1643       1.138    bouyer 		    sizeof(sqtd->qtd),
   1644       1.138    bouyer 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   1645        1.72  augustss 		stop = sqtd->qtd.qtd_next & htole32(EHCI_LINK_TERMINATE);
   1646       1.138    bouyer 		usb_syncmem(&sqtd->dma,
   1647       1.195  christos 		    sqtd->offs + offsetof(ehci_qtd_t, qtd_next),
   1648       1.138    bouyer 		    sizeof(sqtd->qtd), BUS_DMASYNC_PREREAD);
   1649        1.29  augustss 	}
   1650        1.29  augustss 	if (sqtd)
   1651       1.229     skrll 		USBHIST_LOG(ehcidebug,
   1652       1.229     skrll 		    "dump aborted, too many TDs", 0, 0, 0, 0);
   1653         1.9  augustss }
   1654         1.9  augustss 
   1655       1.164  uebayasi Static void
   1656         1.9  augustss ehci_dump_sqtd(ehci_soft_qtd_t *sqtd)
   1657         1.9  augustss {
   1658       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   1659       1.229     skrll 
   1660       1.195  christos 	usb_syncmem(&sqtd->dma, sqtd->offs,
   1661       1.138    bouyer 	    sizeof(sqtd->qtd), BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   1662       1.229     skrll 
   1663       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1664       1.229     skrll 	    "QTD(%p) at 0x%08x:", sqtd, sqtd->physaddr, 0, 0);
   1665         1.9  augustss 	ehci_dump_qtd(&sqtd->qtd);
   1666       1.229     skrll 
   1667       1.195  christos 	usb_syncmem(&sqtd->dma, sqtd->offs,
   1668       1.138    bouyer 	    sizeof(sqtd->qtd), BUS_DMASYNC_PREREAD);
   1669         1.9  augustss }
   1670         1.9  augustss 
   1671       1.164  uebayasi Static void
   1672         1.9  augustss ehci_dump_qtd(ehci_qtd_t *qtd)
   1673         1.9  augustss {
   1674       1.229     skrll 	USBHIST_FUNC();	USBHIST_CALLED(ehcidebug);
   1675         1.9  augustss 
   1676       1.229     skrll #ifdef USBHIST
   1677       1.229     skrll 	uint32_t s = le32toh(qtd->qtd_status);
   1678       1.229     skrll #endif
   1679       1.229     skrll 
   1680       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1681       1.229     skrll 	    "     next = 0x%08x  altnext = 0x%08x  status = 0x%08x",
   1682       1.231     skrll 	    qtd->qtd_next, qtd->qtd_altnext, s, 0);
   1683       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1684       1.229     skrll 	    "   toggle = %d ioc = %d bytes = %#x "
   1685       1.229     skrll 	    "c_page = %#x", EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_IOC(s),
   1686       1.229     skrll 	    EHCI_QTD_GET_BYTES(s), EHCI_QTD_GET_C_PAGE(s));
   1687       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1688       1.229     skrll 	    "     cerr = %d pid = %d stat  = %x",
   1689       1.229     skrll 	    EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s), EHCI_QTD_GET_STATUS(s),
   1690       1.229     skrll 	    0);
   1691       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1692       1.229     skrll 	    "active =%d halted=%d buferr=%d babble=%d",
   1693       1.229     skrll 	    s & EHCI_QTD_ACTIVE ? 1 : 0,
   1694       1.229     skrll 	    s & EHCI_QTD_HALTED ? 1 : 0,
   1695       1.229     skrll 	    s & EHCI_QTD_BUFERR ? 1 : 0,
   1696       1.229     skrll 	    s & EHCI_QTD_BABBLE ? 1 : 0);
   1697       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1698       1.229     skrll 	    "xacterr=%d missed=%d split =%d ping  =%d",
   1699       1.229     skrll 	    s & EHCI_QTD_XACTERR ? 1 : 0,
   1700       1.229     skrll 	    s & EHCI_QTD_MISSEDMICRO ? 1 : 0,
   1701       1.229     skrll 	    s & EHCI_QTD_SPLITXSTATE ? 1 : 0,
   1702       1.229     skrll 	    s & EHCI_QTD_PINGSTATE ? 1 : 0);
   1703       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1704       1.229     skrll 	    "buffer[0] = %#x  buffer[1] = %#x  "
   1705       1.229     skrll 	    "buffer[2] = %#x  buffer[3] = %#x",
   1706       1.229     skrll 	    le32toh(qtd->qtd_buffer[0]), le32toh(qtd->qtd_buffer[1]),
   1707       1.229     skrll 	    le32toh(qtd->qtd_buffer[2]), le32toh(qtd->qtd_buffer[3]));
   1708       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1709       1.229     skrll 	    "buffer[4] = %#x", le32toh(qtd->qtd_buffer[4]), 0, 0, 0);
   1710         1.9  augustss }
   1711         1.9  augustss 
   1712       1.164  uebayasi Static void
   1713         1.9  augustss ehci_dump_sqh(ehci_soft_qh_t *sqh)
   1714         1.9  augustss {
   1715       1.229     skrll #ifdef USBHIST
   1716         1.9  augustss 	ehci_qh_t *qh = &sqh->qh;
   1717       1.229     skrll 	ehci_link_t link;
   1718       1.229     skrll #endif
   1719   1.234.2.1     skrll 	uint32_t endp, endphub;
   1720       1.229     skrll 	USBHIST_FUNC();	USBHIST_CALLED(ehcidebug);
   1721         1.9  augustss 
   1722       1.195  christos 	usb_syncmem(&sqh->dma, sqh->offs,
   1723       1.138    bouyer 	    sizeof(sqh->qh), BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   1724       1.229     skrll 
   1725       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1726       1.229     skrll 	    "QH(%p) at %#x:", sqh, sqh->physaddr, 0, 0);
   1727       1.229     skrll 	link = le32toh(qh->qh_link);
   1728       1.229     skrll 	ehci_dump_link(link, true);
   1729       1.229     skrll 
   1730        1.15  augustss 	endp = le32toh(qh->qh_endp);
   1731       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1732       1.229     skrll 	    "    endp = %#x", endp, 0, 0, 0);
   1733       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1734       1.229     skrll 	    "        addr = 0x%02x  inact = %d  endpt = %d  eps = %d",
   1735       1.229     skrll 	    EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp),
   1736       1.229     skrll 	    EHCI_QH_GET_ENDPT(endp),  EHCI_QH_GET_EPS(endp));
   1737       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1738       1.229     skrll 	    "        dtc  = %d     hrecl = %d",
   1739       1.229     skrll 	    EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp), 0, 0);
   1740       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1741       1.229     skrll 	    "        ctl  = %d     nrl   = %d  mpl   = %#x(%d)",
   1742       1.229     skrll 	    EHCI_QH_GET_CTL(endp),EHCI_QH_GET_NRL(endp),
   1743       1.229     skrll 	    EHCI_QH_GET_MPL(endp), EHCI_QH_GET_MPL(endp));
   1744       1.229     skrll 
   1745        1.15  augustss 	endphub = le32toh(qh->qh_endphub);
   1746       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1747       1.229     skrll 	    " endphub = %#x", endphub, 0, 0, 0);
   1748       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1749       1.229     skrll 	    "      smask = 0x%02x  cmask = 0x%02x",
   1750       1.229     skrll 	    EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub), 1, 0);
   1751       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10,
   1752       1.229     skrll 	    "      huba  = 0x%02x  port  = %d  mult = %d",
   1753       1.229     skrll 	    EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub),
   1754       1.229     skrll 	    EHCI_QH_GET_MULT(endphub), 0);
   1755       1.229     skrll 
   1756       1.229     skrll 	link = le32toh(qh->qh_curqtd);
   1757       1.229     skrll 	ehci_dump_link(link, false);
   1758       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10, "Overlay qTD:", 0, 0, 0, 0);
   1759         1.9  augustss 	ehci_dump_qtd(&qh->qh_qtd);
   1760       1.229     skrll 
   1761       1.195  christos 	usb_syncmem(&sqh->dma, sqh->offs,
   1762       1.138    bouyer 	    sizeof(sqh->qh), BUS_DMASYNC_PREREAD);
   1763         1.9  augustss }
   1764         1.9  augustss 
   1765       1.164  uebayasi Static void
   1766       1.139  jmcneill ehci_dump_itd(struct ehci_soft_itd *itd)
   1767       1.139  jmcneill {
   1768       1.139  jmcneill 	ehci_isoc_trans_t t;
   1769       1.139  jmcneill 	ehci_isoc_bufr_ptr_t b, b2, b3;
   1770       1.139  jmcneill 	int i;
   1771       1.139  jmcneill 
   1772       1.229     skrll 	USBHIST_FUNC();	USBHIST_CALLED(ehcidebug);
   1773       1.229     skrll 
   1774       1.229     skrll 	USBHIST_LOG(ehcidebug, "ITD: next phys = %#x", itd->itd.itd_next, 0,
   1775       1.229     skrll 	    0, 0);
   1776       1.139  jmcneill 
   1777       1.168  jakllsch 	for (i = 0; i < EHCI_ITD_NUFRAMES; i++) {
   1778       1.139  jmcneill 		t = le32toh(itd->itd.itd_ctl[i]);
   1779       1.229     skrll 		USBHIST_LOG(ehcidebug, "ITDctl %d: stat = %x len = %x",
   1780       1.229     skrll 		    i, EHCI_ITD_GET_STATUS(t), EHCI_ITD_GET_LEN(t), 0);
   1781       1.229     skrll 		USBHIST_LOG(ehcidebug, "     ioc = %x pg = %x offs = %x",
   1782       1.139  jmcneill 		    EHCI_ITD_GET_IOC(t), EHCI_ITD_GET_PG(t),
   1783       1.229     skrll 		    EHCI_ITD_GET_OFFS(t), 0);
   1784       1.139  jmcneill 	}
   1785       1.229     skrll 	USBHIST_LOG(ehcidebug, "ITDbufr: ", 0, 0, 0, 0);
   1786       1.168  jakllsch 	for (i = 0; i < EHCI_ITD_NBUFFERS; i++)
   1787       1.229     skrll 		USBHIST_LOG(ehcidebug, "      %x",
   1788       1.229     skrll 		    EHCI_ITD_GET_BPTR(le32toh(itd->itd.itd_bufr[i])), 0, 0, 0);
   1789       1.139  jmcneill 
   1790       1.139  jmcneill 	b = le32toh(itd->itd.itd_bufr[0]);
   1791       1.139  jmcneill 	b2 = le32toh(itd->itd.itd_bufr[1]);
   1792       1.139  jmcneill 	b3 = le32toh(itd->itd.itd_bufr[2]);
   1793       1.229     skrll 	USBHIST_LOG(ehcidebug, "     ep = %x daddr = %x dir = %d",
   1794       1.229     skrll 	    EHCI_ITD_GET_EP(b), EHCI_ITD_GET_DADDR(b), EHCI_ITD_GET_DIR(b2), 0);
   1795       1.229     skrll 	USBHIST_LOG(ehcidebug, "     maxpkt = %x multi = %x",
   1796       1.229     skrll 	    EHCI_ITD_GET_MAXPKT(b2), EHCI_ITD_GET_MULTI(b3), 0, 0);
   1797       1.139  jmcneill }
   1798       1.139  jmcneill 
   1799       1.164  uebayasi Static void
   1800       1.139  jmcneill ehci_dump_sitd(struct ehci_soft_itd *itd)
   1801       1.139  jmcneill {
   1802       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   1803       1.229     skrll 
   1804       1.229     skrll 	USBHIST_LOG(ehcidebug, "SITD %p next = %p prev = %p",
   1805       1.229     skrll 	    itd, itd->u.frame_list.next, itd->u.frame_list.prev, 0);
   1806       1.229     skrll 	USBHIST_LOG(ehcidebug, "        xfernext=%p physaddr=%X slot=%d",
   1807       1.229     skrll 	    itd->xfer_next, itd->physaddr, itd->slot, 0);
   1808       1.139  jmcneill }
   1809       1.139  jmcneill 
   1810       1.164  uebayasi Static void
   1811        1.18  augustss ehci_dump_exfer(struct ehci_xfer *ex)
   1812        1.18  augustss {
   1813       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   1814       1.229     skrll 
   1815       1.229     skrll 	USBHIST_LOG(ehcidebug, "ex = %p sqtdstart = %p end = %p",
   1816  1.234.2.20     skrll 	    ex, ex->ex_sqtdstart, ex->ex_sqtdend, 0);
   1817       1.229     skrll 	USBHIST_LOG(ehcidebug, "     itdstart = %p end = %p isdone = %d",
   1818  1.234.2.20     skrll 	    ex->ex_itdstart, ex->ex_itdend, ex->ex_isdone, 0);
   1819        1.18  augustss }
   1820        1.38    martin #endif
   1821         1.5  augustss 
   1822       1.164  uebayasi Static usbd_status
   1823         1.5  augustss ehci_open(usbd_pipe_handle pipe)
   1824         1.5  augustss {
   1825   1.234.2.8     skrll 	usbd_device_handle dev = pipe->up_dev;
   1826   1.234.2.8     skrll 	ehci_softc_t *sc = dev->ud_bus->ub_hcpriv;
   1827   1.234.2.8     skrll 	usb_endpoint_descriptor_t *ed = pipe->up_endpoint->ue_edesc;
   1828  1.234.2.13     skrll 	uint8_t rhaddr = dev->ud_bus->ub_rhaddr;
   1829   1.234.2.8     skrll 	uint8_t addr = dev->ud_addr;
   1830   1.234.2.1     skrll 	uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes);
   1831         1.5  augustss 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
   1832        1.10  augustss 	ehci_soft_qh_t *sqh;
   1833        1.10  augustss 	usbd_status err;
   1834        1.78  augustss 	int ival, speed, naks;
   1835        1.80  augustss 	int hshubaddr, hshubport;
   1836         1.5  augustss 
   1837       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   1838       1.229     skrll 
   1839       1.229     skrll 	USBHIST_LOG(ehcidebug, "pipe=%p, addr=%d, endpt=%d (%d)",
   1840  1.234.2.13     skrll 	    pipe, addr, ed->bEndpointAddress, rhaddr);
   1841         1.5  augustss 
   1842   1.234.2.8     skrll 	if (dev->ud_myhsport) {
   1843       1.172      matt 		/*
   1844       1.172      matt 		 * When directly attached FS/LS device while doing embedded
   1845       1.172      matt 		 * transaction translations and we are the hub, set the hub
   1846       1.191     skrll 		 * address to 0 (us).
   1847       1.172      matt 		 */
   1848       1.172      matt 		if (!(sc->sc_flags & EHCIF_ETTF)
   1849  1.234.2.13     skrll 		    || (dev->ud_myhsport->up_parent->ud_addr != rhaddr)) {
   1850   1.234.2.8     skrll 			hshubaddr = dev->ud_myhsport->up_parent->ud_addr;
   1851       1.172      matt 		} else {
   1852       1.172      matt 			hshubaddr = 0;
   1853       1.172      matt 		}
   1854   1.234.2.8     skrll 		hshubport = dev->ud_myhsport->up_portno;
   1855        1.80  augustss 	} else {
   1856        1.80  augustss 		hshubaddr = 0;
   1857        1.80  augustss 		hshubport = 0;
   1858        1.80  augustss 	}
   1859        1.80  augustss 
   1860        1.17  augustss 	if (sc->sc_dying)
   1861  1.234.2.14     skrll 		return USBD_IOERROR;
   1862        1.17  augustss 
   1863       1.175  drochner 	/* toggle state needed for bulk endpoints */
   1864   1.234.2.8     skrll 	epipe->nexttoggle = pipe->up_endpoint->ue_toggle;
   1865        1.55   mycroft 
   1866  1.234.2.13     skrll 	if (addr == rhaddr) {
   1867         1.5  augustss 		switch (ed->bEndpointAddress) {
   1868         1.5  augustss 		case USB_CONTROL_ENDPOINT:
   1869  1.234.2.13     skrll 			pipe->up_methods = &roothub_ctrl_methods;
   1870         1.5  augustss 			break;
   1871  1.234.2.13     skrll 		case UE_DIR_IN | USBROOTHUB_INTR_ENDPT:
   1872   1.234.2.8     skrll 			pipe->up_methods = &ehci_root_intr_methods;
   1873         1.5  augustss 			break;
   1874         1.5  augustss 		default:
   1875       1.229     skrll 			USBHIST_LOG(ehcidebug,
   1876       1.229     skrll 			    "bad bEndpointAddress 0x%02x",
   1877       1.229     skrll 			    ed->bEndpointAddress, 0, 0, 0);
   1878  1.234.2.14     skrll 			return USBD_INVAL;
   1879         1.5  augustss 		}
   1880  1.234.2.14     skrll 		return USBD_NORMAL_COMPLETION;
   1881        1.10  augustss 	}
   1882        1.10  augustss 
   1883        1.24  augustss 	/* XXX All this stuff is only valid for async. */
   1884   1.234.2.8     skrll 	switch (dev->ud_speed) {
   1885        1.11  augustss 	case USB_SPEED_LOW:  speed = EHCI_QH_SPEED_LOW;  break;
   1886        1.11  augustss 	case USB_SPEED_FULL: speed = EHCI_QH_SPEED_FULL; break;
   1887        1.11  augustss 	case USB_SPEED_HIGH: speed = EHCI_QH_SPEED_HIGH; break;
   1888   1.234.2.8     skrll 	default: panic("ehci_open: bad device speed %d", dev->ud_speed);
   1889        1.11  augustss 	}
   1890   1.234.2.3     skrll 	if (speed == EHCI_QH_SPEED_LOW && xfertype == UE_ISOCHRONOUS) {
   1891       1.229     skrll 		USBHIST_LOG(ehcidebug, "hshubaddr=%d hshubport=%d",
   1892       1.229     skrll 			    hshubaddr, hshubport, 0, 0);
   1893        1.99  augustss 		return USBD_INVAL;
   1894        1.80  augustss 	}
   1895        1.80  augustss 
   1896       1.169   msaitoh 	/*
   1897       1.169   msaitoh 	 * For interrupt transfer, nak throttling must be disabled, but for
   1898       1.169   msaitoh 	 * the other transfer type, nak throttling should be enabled from the
   1899       1.191     skrll 	 * viewpoint that avoids the memory thrashing.
   1900       1.169   msaitoh 	 */
   1901       1.169   msaitoh 	naks = (xfertype == UE_INTERRUPT) ? 0
   1902       1.169   msaitoh 	    : ((speed == EHCI_QH_SPEED_HIGH) ? 4 : 0);
   1903        1.10  augustss 
   1904       1.139  jmcneill 	/* Allocate sqh for everything, save isoc xfers */
   1905       1.139  jmcneill 	if (xfertype != UE_ISOCHRONOUS) {
   1906       1.139  jmcneill 		sqh = ehci_alloc_sqh(sc);
   1907       1.139  jmcneill 		if (sqh == NULL)
   1908  1.234.2.14     skrll 			return USBD_NOMEM;
   1909       1.139  jmcneill 		/* qh_link filled when the QH is added */
   1910       1.139  jmcneill 		sqh->qh.qh_endp = htole32(
   1911       1.139  jmcneill 		    EHCI_QH_SET_ADDR(addr) |
   1912       1.139  jmcneill 		    EHCI_QH_SET_ENDPT(UE_GET_ADDR(ed->bEndpointAddress)) |
   1913       1.139  jmcneill 		    EHCI_QH_SET_EPS(speed) |
   1914       1.139  jmcneill 		    EHCI_QH_DTC |
   1915       1.139  jmcneill 		    EHCI_QH_SET_MPL(UGETW(ed->wMaxPacketSize)) |
   1916       1.139  jmcneill 		    (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_CONTROL ?
   1917       1.139  jmcneill 		     EHCI_QH_CTL : 0) |
   1918       1.139  jmcneill 		    EHCI_QH_SET_NRL(naks)
   1919       1.139  jmcneill 		    );
   1920       1.139  jmcneill 		sqh->qh.qh_endphub = htole32(
   1921       1.139  jmcneill 		    EHCI_QH_SET_MULT(1) |
   1922       1.139  jmcneill 		    EHCI_QH_SET_SMASK(xfertype == UE_INTERRUPT ? 0x02 : 0)
   1923       1.139  jmcneill 		    );
   1924       1.167  jakllsch 		if (speed != EHCI_QH_SPEED_HIGH)
   1925       1.167  jakllsch 			sqh->qh.qh_endphub |= htole32(
   1926       1.167  jakllsch 			    EHCI_QH_SET_PORT(hshubport) |
   1927       1.167  jakllsch 			    EHCI_QH_SET_HUBA(hshubaddr) |
   1928       1.167  jakllsch 			    EHCI_QH_SET_CMASK(0x08) /* XXX */
   1929       1.167  jakllsch 			);
   1930       1.139  jmcneill 		sqh->qh.qh_curqtd = EHCI_NULL;
   1931       1.139  jmcneill 		/* Fill the overlay qTD */
   1932       1.139  jmcneill 		sqh->qh.qh_qtd.qtd_next = EHCI_NULL;
   1933       1.139  jmcneill 		sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
   1934       1.139  jmcneill 		sqh->qh.qh_qtd.qtd_status = htole32(0);
   1935       1.139  jmcneill 
   1936       1.139  jmcneill 		usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
   1937       1.139  jmcneill 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1938       1.139  jmcneill 		epipe->sqh = sqh;
   1939       1.139  jmcneill 	} else {
   1940       1.139  jmcneill 		sqh = NULL;
   1941       1.139  jmcneill 	} /*xfertype == UE_ISOC*/
   1942         1.5  augustss 
   1943        1.10  augustss 	switch (xfertype) {
   1944        1.10  augustss 	case UE_CONTROL:
   1945        1.33  augustss 		err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t),
   1946        1.10  augustss 				   0, &epipe->u.ctl.reqdma);
   1947        1.25  augustss #ifdef EHCI_DEBUG
   1948        1.25  augustss 		if (err)
   1949        1.25  augustss 			printf("ehci_open: usb_allocmem()=%d\n", err);
   1950        1.25  augustss #endif
   1951        1.10  augustss 		if (err)
   1952       1.116  drochner 			goto bad;
   1953   1.234.2.8     skrll 		pipe->up_methods = &ehci_device_ctrl_methods;
   1954       1.190       mrg 		mutex_enter(&sc->sc_lock);
   1955       1.190       mrg 		ehci_add_qh(sc, sqh, sc->sc_async_head);
   1956       1.190       mrg 		mutex_exit(&sc->sc_lock);
   1957        1.10  augustss 		break;
   1958        1.10  augustss 	case UE_BULK:
   1959   1.234.2.8     skrll 		pipe->up_methods = &ehci_device_bulk_methods;
   1960       1.190       mrg 		mutex_enter(&sc->sc_lock);
   1961       1.190       mrg 		ehci_add_qh(sc, sqh, sc->sc_async_head);
   1962       1.190       mrg 		mutex_exit(&sc->sc_lock);
   1963        1.10  augustss 		break;
   1964        1.24  augustss 	case UE_INTERRUPT:
   1965   1.234.2.8     skrll 		pipe->up_methods = &ehci_device_intr_methods;
   1966   1.234.2.8     skrll 		ival = pipe->up_interval;
   1967       1.116  drochner 		if (ival == USBD_DEFAULT_INTERVAL) {
   1968       1.116  drochner 			if (speed == EHCI_QH_SPEED_HIGH) {
   1969       1.116  drochner 				if (ed->bInterval > 16) {
   1970       1.116  drochner 					/*
   1971       1.116  drochner 					 * illegal with high-speed, but there
   1972       1.116  drochner 					 * were documentation bugs in the spec,
   1973       1.116  drochner 					 * so be generous
   1974       1.116  drochner 					 */
   1975       1.116  drochner 					ival = 256;
   1976       1.116  drochner 				} else
   1977       1.116  drochner 					ival = (1 << (ed->bInterval - 1)) / 8;
   1978       1.116  drochner 			} else
   1979       1.116  drochner 				ival = ed->bInterval;
   1980       1.116  drochner 		}
   1981       1.116  drochner 		err = ehci_device_setintr(sc, sqh, ival);
   1982       1.116  drochner 		if (err)
   1983       1.116  drochner 			goto bad;
   1984       1.116  drochner 		break;
   1985        1.24  augustss 	case UE_ISOCHRONOUS:
   1986   1.234.2.3     skrll 		if (speed == EHCI_QH_SPEED_HIGH)
   1987   1.234.2.8     skrll 			pipe->up_methods = &ehci_device_isoc_methods;
   1988   1.234.2.3     skrll 		else
   1989   1.234.2.8     skrll 			pipe->up_methods = &ehci_device_fs_isoc_methods;
   1990       1.142  drochner 		if (ed->bInterval == 0 || ed->bInterval > 16) {
   1991       1.139  jmcneill 			printf("ehci: opening pipe with invalid bInterval\n");
   1992       1.139  jmcneill 			err = USBD_INVAL;
   1993       1.139  jmcneill 			goto bad;
   1994       1.139  jmcneill 		}
   1995       1.139  jmcneill 		if (UGETW(ed->wMaxPacketSize) == 0) {
   1996       1.139  jmcneill 			printf("ehci: zero length endpoint open request\n");
   1997       1.139  jmcneill 			err = USBD_INVAL;
   1998       1.139  jmcneill 			goto bad;
   1999       1.139  jmcneill 		}
   2000       1.139  jmcneill 		epipe->u.isoc.next_frame = 0;
   2001       1.139  jmcneill 		epipe->u.isoc.cur_xfers = 0;
   2002       1.139  jmcneill 		break;
   2003        1.10  augustss 	default:
   2004       1.229     skrll 		USBHIST_LOG(ehcidebug, "bad xfer type %d", xfertype, 0, 0, 0);
   2005       1.116  drochner 		err = USBD_INVAL;
   2006       1.116  drochner 		goto bad;
   2007         1.5  augustss 	}
   2008  1.234.2.14     skrll 	return USBD_NORMAL_COMPLETION;
   2009         1.5  augustss 
   2010       1.116  drochner  bad:
   2011       1.139  jmcneill 	if (sqh != NULL)
   2012       1.139  jmcneill 		ehci_free_sqh(sc, sqh);
   2013  1.234.2.14     skrll 	return err;
   2014        1.10  augustss }
   2015        1.10  augustss 
   2016        1.10  augustss /*
   2017       1.190       mrg  * Add an ED to the schedule.  Called with USB lock held.
   2018        1.10  augustss  */
   2019       1.164  uebayasi Static void
   2020       1.190       mrg ehci_add_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
   2021        1.10  augustss {
   2022        1.10  augustss 
   2023       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   2024       1.190       mrg 
   2025       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   2026       1.229     skrll 
   2027       1.138    bouyer 	usb_syncmem(&head->dma, head->offs + offsetof(ehci_qh_t, qh_link),
   2028       1.138    bouyer 	    sizeof(head->qh.qh_link), BUS_DMASYNC_POSTWRITE);
   2029       1.229     skrll 
   2030        1.10  augustss 	sqh->next = head->next;
   2031        1.10  augustss 	sqh->qh.qh_link = head->qh.qh_link;
   2032       1.229     skrll 
   2033       1.138    bouyer 	usb_syncmem(&sqh->dma, sqh->offs + offsetof(ehci_qh_t, qh_link),
   2034       1.138    bouyer 	    sizeof(sqh->qh.qh_link), BUS_DMASYNC_PREWRITE);
   2035       1.229     skrll 
   2036        1.10  augustss 	head->next = sqh;
   2037        1.15  augustss 	head->qh.qh_link = htole32(sqh->physaddr | EHCI_LINK_QH);
   2038       1.229     skrll 
   2039       1.138    bouyer 	usb_syncmem(&head->dma, head->offs + offsetof(ehci_qh_t, qh_link),
   2040       1.138    bouyer 	    sizeof(head->qh.qh_link), BUS_DMASYNC_PREWRITE);
   2041        1.10  augustss 
   2042        1.10  augustss #ifdef EHCI_DEBUG
   2043       1.229     skrll 	ehci_dump_sqh(sqh);
   2044         1.5  augustss #endif
   2045         1.5  augustss }
   2046         1.5  augustss 
   2047        1.10  augustss /*
   2048       1.190       mrg  * Remove an ED from the schedule.  Called with USB lock held.
   2049        1.10  augustss  */
   2050       1.164  uebayasi Static void
   2051        1.10  augustss ehci_rem_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head)
   2052        1.10  augustss {
   2053        1.33  augustss 	ehci_soft_qh_t *p;
   2054        1.10  augustss 
   2055       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   2056       1.190       mrg 
   2057        1.10  augustss 	/* XXX */
   2058        1.42  augustss 	for (p = head; p != NULL && p->next != sqh; p = p->next)
   2059        1.10  augustss 		;
   2060        1.10  augustss 	if (p == NULL)
   2061        1.37    provos 		panic("ehci_rem_qh: ED not found");
   2062       1.138    bouyer 	usb_syncmem(&sqh->dma, sqh->offs + offsetof(ehci_qh_t, qh_link),
   2063       1.138    bouyer 	    sizeof(sqh->qh.qh_link), BUS_DMASYNC_POSTWRITE);
   2064        1.10  augustss 	p->next = sqh->next;
   2065        1.10  augustss 	p->qh.qh_link = sqh->qh.qh_link;
   2066       1.138    bouyer 	usb_syncmem(&p->dma, p->offs + offsetof(ehci_qh_t, qh_link),
   2067       1.138    bouyer 	    sizeof(p->qh.qh_link), BUS_DMASYNC_PREWRITE);
   2068        1.10  augustss 
   2069        1.11  augustss 	ehci_sync_hc(sc);
   2070        1.11  augustss }
   2071        1.11  augustss 
   2072       1.164  uebayasi Static void
   2073        1.23  augustss ehci_set_qh_qtd(ehci_soft_qh_t *sqh, ehci_soft_qtd_t *sqtd)
   2074        1.23  augustss {
   2075        1.85  augustss 	int i;
   2076   1.234.2.1     skrll 	uint32_t status;
   2077        1.85  augustss 
   2078        1.87  augustss 	/* Save toggle bit and ping status. */
   2079       1.138    bouyer 	usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
   2080       1.138    bouyer 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   2081        1.87  augustss 	status = sqh->qh.qh_qtd.qtd_status &
   2082        1.87  augustss 	    htole32(EHCI_QTD_TOGGLE_MASK |
   2083        1.87  augustss 		    EHCI_QTD_SET_STATUS(EHCI_QTD_PINGSTATE));
   2084        1.85  augustss 	/* Set HALTED to make hw leave it alone. */
   2085        1.85  augustss 	sqh->qh.qh_qtd.qtd_status =
   2086        1.85  augustss 	    htole32(EHCI_QTD_SET_STATUS(EHCI_QTD_HALTED));
   2087       1.138    bouyer 	usb_syncmem(&sqh->dma,
   2088       1.138    bouyer 	    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
   2089       1.138    bouyer 	    sizeof(sqh->qh.qh_qtd.qtd_status),
   2090       1.138    bouyer 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2091        1.23  augustss 	sqh->qh.qh_curqtd = 0;
   2092        1.23  augustss 	sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr);
   2093       1.179  jmcneill 	sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL;
   2094        1.85  augustss 	for (i = 0; i < EHCI_QTD_NBUFFERS; i++)
   2095        1.85  augustss 		sqh->qh.qh_qtd.qtd_buffer[i] = 0;
   2096        1.23  augustss 	sqh->sqtd = sqtd;
   2097       1.138    bouyer 	usb_syncmem(&sqh->dma, sqh->offs, sizeof(sqh->qh),
   2098       1.138    bouyer 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2099        1.87  augustss 	/* Set !HALTED && !ACTIVE to start execution, preserve some fields */
   2100        1.87  augustss 	sqh->qh.qh_qtd.qtd_status = status;
   2101       1.138    bouyer 	usb_syncmem(&sqh->dma,
   2102       1.138    bouyer 	    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
   2103       1.138    bouyer 	    sizeof(sqh->qh.qh_qtd.qtd_status),
   2104       1.138    bouyer 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2105        1.23  augustss }
   2106        1.23  augustss 
   2107        1.11  augustss /*
   2108        1.11  augustss  * Ensure that the HC has released all references to the QH.  We do this
   2109        1.11  augustss  * by asking for a Async Advance Doorbell interrupt and then we wait for
   2110        1.11  augustss  * the interrupt.
   2111        1.11  augustss  * To make this easier we first obtain exclusive use of the doorbell.
   2112        1.11  augustss  */
   2113       1.164  uebayasi Static void
   2114        1.11  augustss ehci_sync_hc(ehci_softc_t *sc)
   2115        1.11  augustss {
   2116       1.215  christos 	int error __diagused;
   2117       1.190       mrg 
   2118       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   2119        1.11  augustss 
   2120       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   2121       1.229     skrll 
   2122        1.12  augustss 	if (sc->sc_dying) {
   2123       1.229     skrll 		USBHIST_LOG(ehcidebug, "dying", 0, 0, 0, 0);
   2124        1.12  augustss 		return;
   2125        1.12  augustss 	}
   2126        1.10  augustss 	/* ask for doorbell */
   2127        1.10  augustss 	EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | EHCI_CMD_IAAD);
   2128       1.229     skrll 	USBHIST_LOG(ehcidebug, "cmd = 0x%08x sts = 0x%08x",
   2129       1.229     skrll 		    EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS), 0, 0);
   2130       1.229     skrll 
   2131       1.190       mrg 	error = cv_timedwait(&sc->sc_doorbell, &sc->sc_lock, hz); /* bell wait */
   2132       1.229     skrll 
   2133       1.229     skrll 	USBHIST_LOG(ehcidebug, "cmd = 0x%08x sts = 0x%08x",
   2134       1.229     skrll 		    EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS), 0, 0);
   2135        1.15  augustss #ifdef DIAGNOSTIC
   2136        1.15  augustss 	if (error)
   2137       1.190       mrg 		printf("ehci_sync_hc: cv_timedwait() = %d\n", error);
   2138        1.15  augustss #endif
   2139        1.10  augustss }
   2140        1.10  augustss 
   2141       1.164  uebayasi Static void
   2142       1.139  jmcneill ehci_rem_free_itd_chain(ehci_softc_t *sc, struct ehci_xfer *exfer)
   2143       1.139  jmcneill {
   2144       1.139  jmcneill 	struct ehci_soft_itd *itd, *prev;
   2145       1.139  jmcneill 
   2146       1.139  jmcneill 	prev = NULL;
   2147       1.139  jmcneill 
   2148  1.234.2.20     skrll 	if (exfer->ex_itdstart == NULL || exfer->ex_itdend == NULL)
   2149       1.139  jmcneill 		panic("ehci isoc xfer being freed, but with no itd chain\n");
   2150       1.139  jmcneill 
   2151  1.234.2.20     skrll 	for (itd = exfer->ex_itdstart; itd != NULL; itd = itd->xfer_next) {
   2152       1.139  jmcneill 		prev = itd->u.frame_list.prev;
   2153       1.139  jmcneill 		/* Unlink itd from hardware chain, or frame array */
   2154       1.139  jmcneill 		if (prev == NULL) { /* We're at the table head */
   2155       1.139  jmcneill 			sc->sc_softitds[itd->slot] = itd->u.frame_list.next;
   2156       1.139  jmcneill 			sc->sc_flist[itd->slot] = itd->itd.itd_next;
   2157       1.139  jmcneill 			usb_syncmem(&sc->sc_fldma,
   2158       1.139  jmcneill 			    sizeof(ehci_link_t) * itd->slot,
   2159   1.234.2.2     skrll 			    sizeof(ehci_link_t),
   2160       1.139  jmcneill 			    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2161       1.139  jmcneill 
   2162       1.139  jmcneill 			if (itd->u.frame_list.next != NULL)
   2163       1.139  jmcneill 				itd->u.frame_list.next->u.frame_list.prev = NULL;
   2164       1.139  jmcneill 		} else {
   2165       1.139  jmcneill 			/* XXX this part is untested... */
   2166       1.139  jmcneill 			prev->itd.itd_next = itd->itd.itd_next;
   2167       1.139  jmcneill 			usb_syncmem(&itd->dma,
   2168       1.139  jmcneill 			    itd->offs + offsetof(ehci_itd_t, itd_next),
   2169   1.234.2.2     skrll 			    sizeof(itd->itd.itd_next), BUS_DMASYNC_PREWRITE);
   2170       1.139  jmcneill 
   2171       1.139  jmcneill 			prev->u.frame_list.next = itd->u.frame_list.next;
   2172       1.139  jmcneill 			if (itd->u.frame_list.next != NULL)
   2173       1.139  jmcneill 				itd->u.frame_list.next->u.frame_list.prev = prev;
   2174       1.139  jmcneill 		}
   2175       1.139  jmcneill 	}
   2176       1.139  jmcneill 
   2177       1.139  jmcneill 	prev = NULL;
   2178  1.234.2.20     skrll 	for (itd = exfer->ex_itdstart; itd != NULL; itd = itd->xfer_next) {
   2179       1.139  jmcneill 		if (prev != NULL)
   2180       1.139  jmcneill 			ehci_free_itd(sc, prev);
   2181       1.139  jmcneill 		prev = itd;
   2182       1.139  jmcneill 	}
   2183       1.139  jmcneill 	if (prev)
   2184       1.139  jmcneill 		ehci_free_itd(sc, prev);
   2185  1.234.2.20     skrll 	exfer->ex_itdstart = NULL;
   2186  1.234.2.20     skrll 	exfer->ex_itdend = NULL;
   2187       1.139  jmcneill }
   2188       1.139  jmcneill 
   2189   1.234.2.3     skrll Static void
   2190   1.234.2.3     skrll ehci_rem_free_sitd_chain(ehci_softc_t *sc, struct ehci_xfer *exfer)
   2191   1.234.2.3     skrll {
   2192   1.234.2.3     skrll 	struct ehci_soft_sitd *sitd, *prev;
   2193   1.234.2.3     skrll 
   2194   1.234.2.3     skrll 	prev = NULL;
   2195   1.234.2.3     skrll 
   2196  1.234.2.20     skrll 	if (exfer->ex_sitdstart == NULL || exfer->ex_sitdend == NULL)
   2197   1.234.2.3     skrll 		panic("ehci isoc xfer being freed, but with no sitd chain\n");
   2198   1.234.2.3     skrll 
   2199  1.234.2.20     skrll 	for (sitd = exfer->ex_sitdstart; sitd != NULL; sitd = sitd->xfer_next) {
   2200   1.234.2.3     skrll 		prev = sitd->u.frame_list.prev;
   2201   1.234.2.3     skrll 		/* Unlink sitd from hardware chain, or frame array */
   2202   1.234.2.3     skrll 		if (prev == NULL) { /* We're at the table head */
   2203   1.234.2.3     skrll 			sc->sc_softsitds[sitd->slot] = sitd->u.frame_list.next;
   2204   1.234.2.3     skrll 			sc->sc_flist[sitd->slot] = sitd->sitd.sitd_next;
   2205   1.234.2.3     skrll 			usb_syncmem(&sc->sc_fldma,
   2206   1.234.2.3     skrll 			    sizeof(ehci_link_t) * sitd->slot,
   2207   1.234.2.3     skrll 			    sizeof(ehci_link_t),
   2208   1.234.2.3     skrll 			    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2209   1.234.2.3     skrll 
   2210   1.234.2.3     skrll 			if (sitd->u.frame_list.next != NULL)
   2211   1.234.2.3     skrll 				sitd->u.frame_list.next->u.frame_list.prev = NULL;
   2212   1.234.2.3     skrll 		} else {
   2213   1.234.2.3     skrll 			/* XXX this part is untested... */
   2214   1.234.2.3     skrll 			prev->sitd.sitd_next = sitd->sitd.sitd_next;
   2215   1.234.2.3     skrll 			usb_syncmem(&sitd->dma,
   2216   1.234.2.3     skrll 			    sitd->offs + offsetof(ehci_sitd_t, sitd_next),
   2217   1.234.2.3     skrll 			    sizeof(sitd->sitd.sitd_next), BUS_DMASYNC_PREWRITE);
   2218   1.234.2.3     skrll 
   2219   1.234.2.3     skrll 			prev->u.frame_list.next = sitd->u.frame_list.next;
   2220   1.234.2.3     skrll 			if (sitd->u.frame_list.next != NULL)
   2221   1.234.2.3     skrll 				sitd->u.frame_list.next->u.frame_list.prev = prev;
   2222   1.234.2.3     skrll 		}
   2223   1.234.2.3     skrll 	}
   2224   1.234.2.3     skrll 
   2225   1.234.2.3     skrll 	prev = NULL;
   2226  1.234.2.20     skrll 	for (sitd = exfer->ex_sitdstart; sitd != NULL; sitd = sitd->xfer_next) {
   2227   1.234.2.3     skrll 		if (prev != NULL)
   2228   1.234.2.3     skrll 			ehci_free_sitd(sc, prev);
   2229   1.234.2.3     skrll 		prev = sitd;
   2230   1.234.2.3     skrll 	}
   2231   1.234.2.3     skrll 	if (prev)
   2232   1.234.2.3     skrll 		ehci_free_sitd(sc, prev);
   2233  1.234.2.20     skrll 	exfer->ex_sitdstart = NULL;
   2234  1.234.2.20     skrll 	exfer->ex_sitdend = NULL;
   2235   1.234.2.3     skrll }
   2236   1.234.2.3     skrll 
   2237         1.5  augustss /***********/
   2238         1.5  augustss 
   2239  1.234.2.13     skrll Static int
   2240  1.234.2.13     skrll ehci_roothub_ctrl(struct usbd_bus *bus, usb_device_request_t *req,
   2241  1.234.2.13     skrll     void *buf, int buflen)
   2242         1.5  augustss {
   2243  1.234.2.13     skrll 	ehci_softc_t *sc = bus->ub_hcpriv;
   2244         1.5  augustss 	usb_hub_descriptor_t hubd;
   2245  1.234.2.13     skrll 	usb_port_status_t ps;
   2246  1.234.2.13     skrll 	uint16_t len, value, index;
   2247  1.234.2.13     skrll 	int l, totlen = 0;
   2248  1.234.2.13     skrll 	int port, i;
   2249   1.234.2.1     skrll 	uint32_t v;
   2250         1.5  augustss 
   2251       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   2252       1.229     skrll 
   2253         1.5  augustss 	if (sc->sc_dying)
   2254  1.234.2.13     skrll 		return -1;
   2255         1.5  augustss 
   2256       1.229     skrll 	USBHIST_LOG(ehcidebug, "type=0x%02x request=%02x",
   2257       1.229     skrll 		    req->bmRequestType, req->bRequest, 0, 0);
   2258         1.5  augustss 
   2259         1.5  augustss 	len = UGETW(req->wLength);
   2260         1.5  augustss 	value = UGETW(req->wValue);
   2261         1.5  augustss 	index = UGETW(req->wIndex);
   2262         1.5  augustss 
   2263         1.5  augustss #define C(x,y) ((x) | ((y) << 8))
   2264  1.234.2.13     skrll 	switch (C(req->bRequest, req->bmRequestType)) {
   2265         1.5  augustss 	case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
   2266       1.109  christos 		if (len == 0)
   2267       1.109  christos 			break;
   2268  1.234.2.13     skrll 		switch (value) {
   2269  1.234.2.13     skrll 		case C(0, UDESC_DEVICE): {
   2270  1.234.2.13     skrll 			usb_device_descriptor_t devd;
   2271  1.234.2.13     skrll 			totlen = min(buflen, sizeof(devd));
   2272  1.234.2.13     skrll 			memcpy(&devd, buf, totlen);
   2273  1.234.2.13     skrll 			USETW(devd.idVendor, sc->sc_id_vendor);
   2274  1.234.2.13     skrll 			memcpy(buf, &devd, totlen);
   2275         1.5  augustss 			break;
   2276  1.234.2.14     skrll 
   2277  1.234.2.13     skrll 		}
   2278       1.131  drochner #define sd ((usb_string_descriptor_t *)buf)
   2279  1.234.2.13     skrll 		case C(1, UDESC_STRING):
   2280  1.234.2.13     skrll 			/* Vendor */
   2281  1.234.2.13     skrll 			totlen = usb_makestrdesc(sd, len, sc->sc_vendor);
   2282  1.234.2.13     skrll 			break;
   2283  1.234.2.13     skrll 		case C(2, UDESC_STRING):
   2284  1.234.2.13     skrll 			/* Product */
   2285  1.234.2.13     skrll 			totlen = usb_makestrdesc(sd, len, "EHCI root hub");
   2286         1.5  augustss 			break;
   2287  1.234.2.13     skrll #undef sd
   2288         1.5  augustss 		default:
   2289  1.234.2.13     skrll 			/* default from usbroothub */
   2290  1.234.2.13     skrll 			return buflen;
   2291         1.5  augustss 		}
   2292         1.5  augustss 		break;
   2293  1.234.2.13     skrll 
   2294         1.5  augustss 	/* Hub requests */
   2295         1.5  augustss 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
   2296         1.5  augustss 		break;
   2297         1.5  augustss 	case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
   2298       1.229     skrll 		USBHIST_LOG(ehcidebug,
   2299       1.229     skrll 		    "UR_CLEAR_PORT_FEATURE port=%d feature=%d", index, value,
   2300       1.229     skrll 		    0, 0);
   2301         1.5  augustss 		if (index < 1 || index > sc->sc_noport) {
   2302  1.234.2.13     skrll 			return -1;
   2303         1.5  augustss 		}
   2304         1.5  augustss 		port = EHCI_PORTSC(index);
   2305       1.106  augustss 		v = EOREAD4(sc, port);
   2306       1.229     skrll 		USBHIST_LOG(ehcidebug, "portsc=0x%08x", v, 0, 0, 0);
   2307       1.106  augustss 		v &= ~EHCI_PS_CLEAR;
   2308  1.234.2.13     skrll 		switch (value) {
   2309         1.5  augustss 		case UHF_PORT_ENABLE:
   2310         1.5  augustss 			EOWRITE4(sc, port, v &~ EHCI_PS_PE);
   2311         1.5  augustss 			break;
   2312         1.5  augustss 		case UHF_PORT_SUSPEND:
   2313       1.137  drochner 			if (!(v & EHCI_PS_SUSP)) /* not suspended */
   2314       1.137  drochner 				break;
   2315       1.137  drochner 			v &= ~EHCI_PS_SUSP;
   2316       1.137  drochner 			EOWRITE4(sc, port, v | EHCI_PS_FPR);
   2317       1.137  drochner 			/* see USB2 spec ch. 7.1.7.7 */
   2318       1.137  drochner 			usb_delay_ms(&sc->sc_bus, 20);
   2319       1.137  drochner 			EOWRITE4(sc, port, v);
   2320       1.137  drochner 			usb_delay_ms(&sc->sc_bus, 2);
   2321       1.137  drochner #ifdef DEBUG
   2322       1.137  drochner 			v = EOREAD4(sc, port);
   2323       1.137  drochner 			if (v & (EHCI_PS_FPR | EHCI_PS_SUSP))
   2324       1.137  drochner 				printf("ehci: resume failed: %x\n", v);
   2325       1.137  drochner #endif
   2326         1.5  augustss 			break;
   2327         1.5  augustss 		case UHF_PORT_POWER:
   2328       1.106  augustss 			if (sc->sc_hasppc)
   2329       1.106  augustss 				EOWRITE4(sc, port, v &~ EHCI_PS_PP);
   2330         1.5  augustss 			break;
   2331        1.14  augustss 		case UHF_PORT_TEST:
   2332       1.229     skrll 			USBHIST_LOG(ehcidebug, "clear port test "
   2333       1.229     skrll 				    "%d", index, 0, 0, 0);
   2334        1.14  augustss 			break;
   2335        1.14  augustss 		case UHF_PORT_INDICATOR:
   2336       1.229     skrll 			USBHIST_LOG(ehcidebug, "clear port ind "
   2337       1.229     skrll 				    "%d", index, 0, 0, 0);
   2338        1.14  augustss 			EOWRITE4(sc, port, v &~ EHCI_PS_PIC);
   2339        1.14  augustss 			break;
   2340         1.5  augustss 		case UHF_C_PORT_CONNECTION:
   2341         1.5  augustss 			EOWRITE4(sc, port, v | EHCI_PS_CSC);
   2342         1.5  augustss 			break;
   2343         1.5  augustss 		case UHF_C_PORT_ENABLE:
   2344         1.5  augustss 			EOWRITE4(sc, port, v | EHCI_PS_PEC);
   2345         1.5  augustss 			break;
   2346         1.5  augustss 		case UHF_C_PORT_SUSPEND:
   2347         1.5  augustss 			/* how? */
   2348         1.5  augustss 			break;
   2349         1.5  augustss 		case UHF_C_PORT_OVER_CURRENT:
   2350         1.5  augustss 			EOWRITE4(sc, port, v | EHCI_PS_OCC);
   2351         1.5  augustss 			break;
   2352         1.5  augustss 		case UHF_C_PORT_RESET:
   2353       1.106  augustss 			sc->sc_isreset[index] = 0;
   2354         1.5  augustss 			break;
   2355         1.5  augustss 		default:
   2356  1.234.2.13     skrll 			return -1;
   2357         1.5  augustss 		}
   2358         1.5  augustss #if 0
   2359         1.5  augustss 		switch(value) {
   2360         1.5  augustss 		case UHF_C_PORT_CONNECTION:
   2361         1.5  augustss 		case UHF_C_PORT_ENABLE:
   2362         1.5  augustss 		case UHF_C_PORT_SUSPEND:
   2363         1.5  augustss 		case UHF_C_PORT_OVER_CURRENT:
   2364         1.5  augustss 		case UHF_C_PORT_RESET:
   2365         1.5  augustss 		default:
   2366         1.5  augustss 			break;
   2367         1.5  augustss 		}
   2368         1.5  augustss #endif
   2369         1.5  augustss 		break;
   2370         1.5  augustss 	case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
   2371       1.109  christos 		if (len == 0)
   2372       1.109  christos 			break;
   2373        1.51    toshii 		if ((value & 0xff) != 0) {
   2374  1.234.2.13     skrll 			return -1;
   2375         1.5  augustss 		}
   2376  1.234.2.13     skrll 		totlen = min(buflen, sizeof(hubd));
   2377  1.234.2.13     skrll 		memcpy(&hubd, buf, totlen);
   2378         1.5  augustss 		hubd.bNbrPorts = sc->sc_noport;
   2379         1.5  augustss 		v = EOREAD4(sc, EHCI_HCSPARAMS);
   2380         1.5  augustss 		USETW(hubd.wHubCharacteristics,
   2381        1.14  augustss 		    EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH |
   2382        1.78  augustss 		    EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS))
   2383       1.164  uebayasi 			? UHD_PORT_IND : 0);
   2384         1.5  augustss 		hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */
   2385        1.33  augustss 		for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
   2386         1.5  augustss 			hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */
   2387         1.5  augustss 		hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
   2388  1.234.2.13     skrll 		totlen = min(totlen, hubd.bDescLength);
   2389  1.234.2.13     skrll 		memcpy(buf, &hubd, totlen);
   2390         1.5  augustss 		break;
   2391         1.5  augustss 	case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
   2392         1.5  augustss 		if (len != 4) {
   2393  1.234.2.13     skrll 			return -1;
   2394         1.5  augustss 		}
   2395         1.5  augustss 		memset(buf, 0, len); /* ? XXX */
   2396         1.5  augustss 		totlen = len;
   2397         1.5  augustss 		break;
   2398         1.5  augustss 	case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
   2399       1.229     skrll 		USBHIST_LOG(ehcidebug, "get port status i=%d", index, 0, 0, 0);
   2400         1.5  augustss 		if (index < 1 || index > sc->sc_noport) {
   2401  1.234.2.13     skrll 			return -1;
   2402         1.5  augustss 		}
   2403         1.5  augustss 		if (len != 4) {
   2404  1.234.2.13     skrll 			return -1;
   2405         1.5  augustss 		}
   2406         1.5  augustss 		v = EOREAD4(sc, EHCI_PORTSC(index));
   2407       1.229     skrll 		USBHIST_LOG(ehcidebug, "port status=0x%04x", v, 0, 0, 0);
   2408       1.172      matt 
   2409       1.178      matt 		i = UPS_HIGH_SPEED;
   2410       1.172      matt 		if (sc->sc_flags & EHCIF_ETTF) {
   2411       1.172      matt 			/*
   2412       1.172      matt 			 * If we are doing embedded transaction translation,
   2413       1.172      matt 			 * then directly attached LS/FS devices are reset by
   2414       1.172      matt 			 * the EHCI controller itself.  PSPD is encoded
   2415       1.195  christos 			 * the same way as in USBSTATUS.
   2416       1.172      matt 			 */
   2417       1.172      matt 			i = __SHIFTOUT(v, EHCI_PS_PSPD) * UPS_LOW_SPEED;
   2418       1.172      matt 		}
   2419         1.5  augustss 		if (v & EHCI_PS_CS)	i |= UPS_CURRENT_CONNECT_STATUS;
   2420         1.5  augustss 		if (v & EHCI_PS_PE)	i |= UPS_PORT_ENABLED;
   2421         1.5  augustss 		if (v & EHCI_PS_SUSP)	i |= UPS_SUSPEND;
   2422         1.5  augustss 		if (v & EHCI_PS_OCA)	i |= UPS_OVERCURRENT_INDICATOR;
   2423         1.5  augustss 		if (v & EHCI_PS_PR)	i |= UPS_RESET;
   2424         1.5  augustss 		if (v & EHCI_PS_PP)	i |= UPS_PORT_POWER;
   2425       1.170  kiyohara 		if (sc->sc_vendor_port_status)
   2426       1.170  kiyohara 			i = sc->sc_vendor_port_status(sc, v, i);
   2427         1.5  augustss 		USETW(ps.wPortStatus, i);
   2428         1.5  augustss 		i = 0;
   2429         1.5  augustss 		if (v & EHCI_PS_CSC)	i |= UPS_C_CONNECT_STATUS;
   2430         1.5  augustss 		if (v & EHCI_PS_PEC)	i |= UPS_C_PORT_ENABLED;
   2431         1.5  augustss 		if (v & EHCI_PS_OCC)	i |= UPS_C_OVERCURRENT_INDICATOR;
   2432       1.106  augustss 		if (sc->sc_isreset[index]) i |= UPS_C_PORT_RESET;
   2433         1.5  augustss 		USETW(ps.wPortChange, i);
   2434  1.234.2.13     skrll 		totlen = min(len, sizeof(ps));
   2435  1.234.2.13     skrll 		memcpy(buf, &ps, totlen);
   2436         1.5  augustss 		break;
   2437         1.5  augustss 	case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
   2438  1.234.2.13     skrll 		return -1;
   2439         1.5  augustss 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
   2440         1.5  augustss 		break;
   2441         1.5  augustss 	case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
   2442         1.5  augustss 		if (index < 1 || index > sc->sc_noport) {
   2443  1.234.2.13     skrll 			return -1;
   2444         1.5  augustss 		}
   2445         1.5  augustss 		port = EHCI_PORTSC(index);
   2446       1.106  augustss 		v = EOREAD4(sc, port);
   2447       1.229     skrll 		USBHIST_LOG(ehcidebug, "portsc=0x%08x", v, 0, 0, 0);
   2448       1.106  augustss 		v &= ~EHCI_PS_CLEAR;
   2449         1.5  augustss 		switch(value) {
   2450         1.5  augustss 		case UHF_PORT_ENABLE:
   2451         1.5  augustss 			EOWRITE4(sc, port, v | EHCI_PS_PE);
   2452         1.5  augustss 			break;
   2453         1.5  augustss 		case UHF_PORT_SUSPEND:
   2454         1.5  augustss 			EOWRITE4(sc, port, v | EHCI_PS_SUSP);
   2455         1.5  augustss 			break;
   2456         1.5  augustss 		case UHF_PORT_RESET:
   2457       1.229     skrll 			USBHIST_LOG(ehcidebug, "reset port %d", index, 0, 0, 0);
   2458       1.172      matt 			if (EHCI_PS_IS_LOWSPEED(v)
   2459       1.172      matt 			    && sc->sc_ncomp > 0
   2460       1.172      matt 			    && !(sc->sc_flags & EHCIF_ETTF)) {
   2461       1.172      matt 				/*
   2462       1.172      matt 				 * Low speed device on non-ETTF controller or
   2463       1.172      matt 				 * unaccompanied controller, give up ownership.
   2464       1.172      matt 				 */
   2465         1.6  augustss 				ehci_disown(sc, index, 1);
   2466         1.6  augustss 				break;
   2467         1.6  augustss 			}
   2468         1.8  augustss 			/* Start reset sequence. */
   2469         1.8  augustss 			v &= ~ (EHCI_PS_PE | EHCI_PS_PR);
   2470         1.5  augustss 			EOWRITE4(sc, port, v | EHCI_PS_PR);
   2471         1.8  augustss 			/* Wait for reset to complete. */
   2472        1.13  augustss 			usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
   2473        1.17  augustss 			if (sc->sc_dying) {
   2474  1.234.2.13     skrll 				return -1;
   2475        1.17  augustss 			}
   2476       1.172      matt 			/*
   2477       1.207  jakllsch 			 * An embedded transaction translator will automatically
   2478       1.172      matt 			 * terminate the reset sequence so there's no need to
   2479       1.172      matt 			 * it.
   2480       1.172      matt 			 */
   2481       1.178      matt 			v = EOREAD4(sc, port);
   2482       1.178      matt 			if (v & EHCI_PS_PR) {
   2483       1.172      matt 				/* Terminate reset sequence. */
   2484       1.173  jmcneill 				EOWRITE4(sc, port, v & ~EHCI_PS_PR);
   2485       1.172      matt 				/* Wait for HC to complete reset. */
   2486       1.172      matt 				usb_delay_ms(&sc->sc_bus,
   2487       1.172      matt 				    EHCI_PORT_RESET_COMPLETE);
   2488       1.172      matt 				if (sc->sc_dying) {
   2489  1.234.2.13     skrll 					return -1;
   2490       1.172      matt 				}
   2491        1.17  augustss 			}
   2492       1.172      matt 
   2493         1.8  augustss 			v = EOREAD4(sc, port);
   2494       1.229     skrll 			USBHIST_LOG(ehcidebug,
   2495       1.229     skrll 			    "ehci after reset, status=0x%08x", v, 0, 0, 0);
   2496         1.8  augustss 			if (v & EHCI_PS_PR) {
   2497         1.8  augustss 				printf("%s: port reset timeout\n",
   2498       1.134  drochner 				       device_xname(sc->sc_dev));
   2499  1.234.2.14     skrll 				return USBD_TIMEOUT;
   2500         1.5  augustss 			}
   2501         1.8  augustss 			if (!(v & EHCI_PS_PE)) {
   2502         1.6  augustss 				/* Not a high speed device, give up ownership.*/
   2503         1.6  augustss 				ehci_disown(sc, index, 0);
   2504         1.6  augustss 				break;
   2505         1.6  augustss 			}
   2506       1.106  augustss 			sc->sc_isreset[index] = 1;
   2507       1.229     skrll 			USBHIST_LOG(ehcidebug,
   2508       1.229     skrll 			    "ehci port %d reset, status = 0x%08x", index, v, 0,
   2509       1.229     skrll 			    0);
   2510         1.5  augustss 			break;
   2511         1.5  augustss 		case UHF_PORT_POWER:
   2512       1.229     skrll 			USBHIST_LOG(ehcidebug,
   2513       1.229     skrll 			    "set port power %d (has PPC = %d)", index,
   2514       1.229     skrll 			    sc->sc_hasppc, 0, 0);
   2515       1.106  augustss 			if (sc->sc_hasppc)
   2516       1.106  augustss 				EOWRITE4(sc, port, v | EHCI_PS_PP);
   2517         1.5  augustss 			break;
   2518        1.11  augustss 		case UHF_PORT_TEST:
   2519       1.229     skrll 			USBHIST_LOG(ehcidebug, "set port test %d",
   2520       1.229     skrll 				index, 0, 0, 0);
   2521        1.11  augustss 			break;
   2522        1.11  augustss 		case UHF_PORT_INDICATOR:
   2523       1.229     skrll 			USBHIST_LOG(ehcidebug, "set port ind %d",
   2524       1.229     skrll 				index, 0, 0, 0);
   2525        1.14  augustss 			EOWRITE4(sc, port, v | EHCI_PS_PIC);
   2526        1.11  augustss 			break;
   2527         1.5  augustss 		default:
   2528  1.234.2.13     skrll 			return -1;
   2529         1.5  augustss 		}
   2530         1.5  augustss 		break;
   2531        1.11  augustss 	case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
   2532        1.11  augustss 	case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
   2533        1.11  augustss 	case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
   2534        1.11  augustss 	case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
   2535        1.11  augustss 		break;
   2536         1.5  augustss 	default:
   2537  1.234.2.13     skrll 		/* default from usbroothub */
   2538  1.234.2.13     skrll 		return buflen;
   2539         1.5  augustss 	}
   2540  1.234.2.13     skrll 
   2541  1.234.2.13     skrll 	return totlen;
   2542         1.6  augustss }
   2543         1.6  augustss 
   2544       1.164  uebayasi Static void
   2545       1.115  christos ehci_disown(ehci_softc_t *sc, int index, int lowspeed)
   2546         1.6  augustss {
   2547        1.24  augustss 	int port;
   2548   1.234.2.1     skrll 	uint32_t v;
   2549         1.6  augustss 
   2550       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   2551       1.229     skrll 
   2552       1.229     skrll 	USBHIST_LOG(ehcidebug, "index=%d lowspeed=%d", index, lowspeed, 0, 0);
   2553         1.6  augustss #ifdef DIAGNOSTIC
   2554         1.6  augustss 	if (sc->sc_npcomp != 0) {
   2555        1.24  augustss 		int i = (index-1) / sc->sc_npcomp;
   2556         1.6  augustss 		if (i >= sc->sc_ncomp)
   2557         1.6  augustss 			printf("%s: strange port\n",
   2558       1.134  drochner 			       device_xname(sc->sc_dev));
   2559         1.6  augustss 		else
   2560         1.6  augustss 			printf("%s: handing over %s speed device on "
   2561         1.6  augustss 			       "port %d to %s\n",
   2562       1.134  drochner 			       device_xname(sc->sc_dev),
   2563         1.6  augustss 			       lowspeed ? "low" : "full",
   2564       1.134  drochner 			       index, device_xname(sc->sc_comps[i]));
   2565         1.6  augustss 	} else {
   2566       1.134  drochner 		printf("%s: npcomp == 0\n", device_xname(sc->sc_dev));
   2567         1.6  augustss 	}
   2568         1.6  augustss #endif
   2569         1.6  augustss 	port = EHCI_PORTSC(index);
   2570         1.6  augustss 	v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR;
   2571         1.6  augustss 	EOWRITE4(sc, port, v | EHCI_PS_PO);
   2572         1.5  augustss }
   2573         1.5  augustss 
   2574         1.5  augustss Static usbd_status
   2575         1.5  augustss ehci_root_intr_transfer(usbd_xfer_handle xfer)
   2576         1.5  augustss {
   2577   1.234.2.8     skrll 	ehci_softc_t *sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2578         1.5  augustss 	usbd_status err;
   2579         1.5  augustss 
   2580         1.5  augustss 	/* Insert last in queue. */
   2581       1.190       mrg 	mutex_enter(&sc->sc_lock);
   2582         1.5  augustss 	err = usb_insert_transfer(xfer);
   2583       1.190       mrg 	mutex_exit(&sc->sc_lock);
   2584         1.5  augustss 	if (err)
   2585  1.234.2.14     skrll 		return err;
   2586         1.5  augustss 
   2587         1.5  augustss 	/* Pipe isn't running, start first */
   2588  1.234.2.14     skrll 	return ehci_root_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   2589         1.5  augustss }
   2590         1.5  augustss 
   2591         1.5  augustss Static usbd_status
   2592         1.5  augustss ehci_root_intr_start(usbd_xfer_handle xfer)
   2593         1.5  augustss {
   2594   1.234.2.8     skrll 	usbd_pipe_handle pipe = xfer->ux_pipe;
   2595   1.234.2.8     skrll 	ehci_softc_t *sc = pipe->up_dev->ud_bus->ub_hcpriv;
   2596         1.5  augustss 
   2597         1.5  augustss 	if (sc->sc_dying)
   2598  1.234.2.14     skrll 		return USBD_IOERROR;
   2599         1.5  augustss 
   2600       1.190       mrg 	mutex_enter(&sc->sc_lock);
   2601         1.5  augustss 	sc->sc_intrxfer = xfer;
   2602       1.190       mrg 	mutex_exit(&sc->sc_lock);
   2603         1.5  augustss 
   2604  1.234.2.14     skrll 	return USBD_IN_PROGRESS;
   2605         1.5  augustss }
   2606         1.5  augustss 
   2607         1.5  augustss /* Abort a root interrupt request. */
   2608         1.5  augustss Static void
   2609         1.5  augustss ehci_root_intr_abort(usbd_xfer_handle xfer)
   2610         1.5  augustss {
   2611   1.234.2.8     skrll 	ehci_softc_t *sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   2612         1.5  augustss 
   2613       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   2614   1.234.2.8     skrll 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
   2615       1.227     skrll 
   2616       1.227     skrll 	sc->sc_intrxfer = NULL;
   2617       1.227     skrll 
   2618   1.234.2.8     skrll 	xfer->ux_status = USBD_CANCELLED;
   2619         1.5  augustss 	usb_transfer_complete(xfer);
   2620         1.5  augustss }
   2621         1.5  augustss 
   2622         1.5  augustss /* Close the root pipe. */
   2623         1.5  augustss Static void
   2624         1.5  augustss ehci_root_intr_close(usbd_pipe_handle pipe)
   2625         1.5  augustss {
   2626   1.234.2.8     skrll 	ehci_softc_t *sc = pipe->up_dev->ud_bus->ub_hcpriv;
   2627        1.33  augustss 
   2628       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   2629       1.229     skrll 
   2630       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   2631       1.190       mrg 
   2632         1.5  augustss 	sc->sc_intrxfer = NULL;
   2633         1.5  augustss }
   2634         1.5  augustss 
   2635       1.164  uebayasi Static void
   2636       1.208  jakllsch ehci_root_intr_done(usbd_xfer_handle xfer)
   2637         1.5  augustss {
   2638   1.234.2.8     skrll 	xfer->ux_hcpriv = NULL;
   2639         1.9  augustss }
   2640         1.9  augustss 
   2641         1.9  augustss /************************/
   2642         1.9  augustss 
   2643       1.164  uebayasi Static ehci_soft_qh_t *
   2644         1.9  augustss ehci_alloc_sqh(ehci_softc_t *sc)
   2645         1.9  augustss {
   2646         1.9  augustss 	ehci_soft_qh_t *sqh;
   2647         1.9  augustss 	usbd_status err;
   2648         1.9  augustss 	int i, offs;
   2649         1.9  augustss 	usb_dma_t dma;
   2650         1.9  augustss 
   2651       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   2652       1.229     skrll 
   2653         1.9  augustss 	if (sc->sc_freeqhs == NULL) {
   2654       1.229     skrll 		USBHIST_LOG(ehcidebug, "allocating chunk", 0, 0, 0, 0);
   2655         1.9  augustss 		err = usb_allocmem(&sc->sc_bus, EHCI_SQH_SIZE * EHCI_SQH_CHUNK,
   2656         1.9  augustss 			  EHCI_PAGE_SIZE, &dma);
   2657        1.25  augustss #ifdef EHCI_DEBUG
   2658        1.25  augustss 		if (err)
   2659        1.25  augustss 			printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err);
   2660        1.25  augustss #endif
   2661         1.9  augustss 		if (err)
   2662  1.234.2.14     skrll 			return NULL;
   2663         1.9  augustss 		for(i = 0; i < EHCI_SQH_CHUNK; i++) {
   2664         1.9  augustss 			offs = i * EHCI_SQH_SIZE;
   2665        1.30  augustss 			sqh = KERNADDR(&dma, offs);
   2666        1.31  augustss 			sqh->physaddr = DMAADDR(&dma, offs);
   2667       1.138    bouyer 			sqh->dma = dma;
   2668       1.138    bouyer 			sqh->offs = offs;
   2669         1.9  augustss 			sqh->next = sc->sc_freeqhs;
   2670         1.9  augustss 			sc->sc_freeqhs = sqh;
   2671         1.9  augustss 		}
   2672         1.9  augustss 	}
   2673         1.9  augustss 	sqh = sc->sc_freeqhs;
   2674         1.9  augustss 	sc->sc_freeqhs = sqh->next;
   2675         1.9  augustss 	memset(&sqh->qh, 0, sizeof(ehci_qh_t));
   2676        1.11  augustss 	sqh->next = NULL;
   2677  1.234.2.14     skrll 	return sqh;
   2678         1.9  augustss }
   2679         1.9  augustss 
   2680       1.164  uebayasi Static void
   2681         1.9  augustss ehci_free_sqh(ehci_softc_t *sc, ehci_soft_qh_t *sqh)
   2682         1.9  augustss {
   2683         1.9  augustss 	sqh->next = sc->sc_freeqhs;
   2684         1.9  augustss 	sc->sc_freeqhs = sqh;
   2685         1.9  augustss }
   2686         1.9  augustss 
   2687       1.164  uebayasi Static ehci_soft_qtd_t *
   2688         1.9  augustss ehci_alloc_sqtd(ehci_softc_t *sc)
   2689         1.9  augustss {
   2690       1.190       mrg 	ehci_soft_qtd_t *sqtd = NULL;
   2691         1.9  augustss 	usbd_status err;
   2692         1.9  augustss 	int i, offs;
   2693         1.9  augustss 	usb_dma_t dma;
   2694         1.9  augustss 
   2695       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   2696       1.229     skrll 
   2697         1.9  augustss 	if (sc->sc_freeqtds == NULL) {
   2698       1.229     skrll 		USBHIST_LOG(ehcidebug, "allocating chunk", 0, 0, 0, 0);
   2699       1.190       mrg 
   2700         1.9  augustss 		err = usb_allocmem(&sc->sc_bus, EHCI_SQTD_SIZE*EHCI_SQTD_CHUNK,
   2701         1.9  augustss 			  EHCI_PAGE_SIZE, &dma);
   2702        1.25  augustss #ifdef EHCI_DEBUG
   2703        1.25  augustss 		if (err)
   2704        1.25  augustss 			printf("ehci_alloc_sqtd: usb_allocmem()=%d\n", err);
   2705        1.25  augustss #endif
   2706         1.9  augustss 		if (err)
   2707       1.190       mrg 			goto done;
   2708       1.190       mrg 
   2709         1.9  augustss 		for(i = 0; i < EHCI_SQTD_CHUNK; i++) {
   2710         1.9  augustss 			offs = i * EHCI_SQTD_SIZE;
   2711        1.30  augustss 			sqtd = KERNADDR(&dma, offs);
   2712        1.31  augustss 			sqtd->physaddr = DMAADDR(&dma, offs);
   2713       1.138    bouyer 			sqtd->dma = dma;
   2714       1.138    bouyer 			sqtd->offs = offs;
   2715       1.190       mrg 
   2716         1.9  augustss 			sqtd->nextqtd = sc->sc_freeqtds;
   2717         1.9  augustss 			sc->sc_freeqtds = sqtd;
   2718         1.9  augustss 		}
   2719         1.9  augustss 	}
   2720         1.9  augustss 
   2721         1.9  augustss 	sqtd = sc->sc_freeqtds;
   2722         1.9  augustss 	sc->sc_freeqtds = sqtd->nextqtd;
   2723         1.9  augustss 	memset(&sqtd->qtd, 0, sizeof(ehci_qtd_t));
   2724         1.9  augustss 	sqtd->nextqtd = NULL;
   2725         1.9  augustss 	sqtd->xfer = NULL;
   2726         1.9  augustss 
   2727       1.190       mrg done:
   2728  1.234.2.14     skrll 	return sqtd;
   2729         1.9  augustss }
   2730         1.9  augustss 
   2731       1.164  uebayasi Static void
   2732         1.9  augustss ehci_free_sqtd(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd)
   2733         1.9  augustss {
   2734         1.9  augustss 
   2735   1.234.2.8     skrll 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
   2736       1.190       mrg 
   2737         1.9  augustss 	sqtd->nextqtd = sc->sc_freeqtds;
   2738         1.9  augustss 	sc->sc_freeqtds = sqtd;
   2739         1.9  augustss }
   2740         1.9  augustss 
   2741       1.164  uebayasi Static usbd_status
   2742        1.25  augustss ehci_alloc_sqtd_chain(struct ehci_pipe *epipe, ehci_softc_t *sc,
   2743        1.15  augustss 		     int alen, int rd, usbd_xfer_handle xfer,
   2744        1.15  augustss 		     ehci_soft_qtd_t **sp, ehci_soft_qtd_t **ep)
   2745        1.15  augustss {
   2746        1.15  augustss 	ehci_soft_qtd_t *next, *cur;
   2747       1.197     prlw1 	ehci_physaddr_t nextphys;
   2748   1.234.2.1     skrll 	uint32_t qtdstatus;
   2749        1.55   mycroft 	int len, curlen, mps;
   2750        1.55   mycroft 	int i, tog;
   2751       1.197     prlw1 	int pages, pageoffs;
   2752       1.197     prlw1 	bus_size_t curoffs;
   2753       1.197     prlw1 	vaddr_t va, va_offs;
   2754   1.234.2.8     skrll 	usb_dma_t *dma = &xfer->ux_dmabuf;
   2755   1.234.2.8     skrll 	uint16_t flags = xfer->ux_flags;
   2756       1.197     prlw1 	paddr_t a;
   2757        1.15  augustss 
   2758       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   2759       1.229     skrll 
   2760       1.229     skrll 	USBHIST_LOG(ehcidebug, "start len=%d", alen, 0, 0, 0);
   2761        1.15  augustss 
   2762        1.15  augustss 	len = alen;
   2763        1.67   mycroft 	qtdstatus = EHCI_QTD_ACTIVE |
   2764        1.15  augustss 	    EHCI_QTD_SET_PID(rd ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) |
   2765        1.15  augustss 	    EHCI_QTD_SET_CERR(3)
   2766        1.15  augustss 	    /* IOC set below */
   2767        1.15  augustss 	    /* BYTES set below */
   2768        1.67   mycroft 	    ;
   2769   1.234.2.8     skrll 	mps = UGETW(epipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize);
   2770        1.55   mycroft 	tog = epipe->nexttoggle;
   2771        1.64   mycroft 	qtdstatus |= EHCI_QTD_SET_TOGGLE(tog);
   2772        1.15  augustss 
   2773        1.15  augustss 	cur = ehci_alloc_sqtd(sc);
   2774        1.25  augustss 	*sp = cur;
   2775        1.15  augustss 	if (cur == NULL)
   2776        1.15  augustss 		goto nomem;
   2777       1.138    bouyer 
   2778       1.138    bouyer 	usb_syncmem(dma, 0, alen,
   2779       1.138    bouyer 	    rd ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
   2780       1.197     prlw1 	curoffs = 0;
   2781        1.15  augustss 	for (;;) {
   2782        1.26  augustss 		/* The EHCI hardware can handle at most 5 pages. */
   2783       1.197     prlw1 		va_offs = (vaddr_t)KERNADDR(dma, curoffs);
   2784       1.197     prlw1 		va_offs = EHCI_PAGE_OFFSET(va_offs);
   2785       1.197     prlw1 		if (len-curoffs < EHCI_QTD_NBUFFERS*EHCI_PAGE_SIZE - va_offs) {
   2786        1.15  augustss 			/* we can handle it in this QTD */
   2787       1.197     prlw1 			curlen = len - curoffs;
   2788        1.15  augustss 		} else {
   2789        1.15  augustss 			/* must use multiple TDs, fill as much as possible. */
   2790       1.197     prlw1 			curlen = EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE - va_offs;
   2791       1.197     prlw1 
   2792        1.15  augustss 			/* the length must be a multiple of the max size */
   2793        1.55   mycroft 			curlen -= curlen % mps;
   2794       1.229     skrll 			USBHIST_LOG(ehcidebug, "multiple QTDs, "
   2795       1.229     skrll 				    "curlen=%d", curlen, 0, 0, 0);
   2796  1.234.2.17     skrll 			KASSERT(curlen != 0);
   2797        1.15  augustss 		}
   2798       1.229     skrll 		USBHIST_LOG(ehcidebug, "len=%d curlen=%d curoffs=%zu",
   2799       1.229     skrll 			len, curlen, (size_t)curoffs, 0);
   2800        1.15  augustss 
   2801       1.102  augustss 		/*
   2802       1.110     blymn 		 * Allocate another transfer if there's more data left,
   2803       1.110     blymn 		 * or if force last short transfer flag is set and we're
   2804       1.102  augustss 		 * allocating a multiple of the max packet size.
   2805       1.102  augustss 		 */
   2806       1.197     prlw1 
   2807       1.197     prlw1 		if (curoffs + curlen != len ||
   2808       1.102  augustss 		    ((curlen % mps) == 0 && !rd && curlen != 0 &&
   2809       1.102  augustss 		     (flags & USBD_FORCE_SHORT_XFER))) {
   2810        1.15  augustss 			next = ehci_alloc_sqtd(sc);
   2811        1.15  augustss 			if (next == NULL)
   2812        1.15  augustss 				goto nomem;
   2813        1.66   mycroft 			nextphys = htole32(next->physaddr);
   2814        1.15  augustss 		} else {
   2815        1.15  augustss 			next = NULL;
   2816        1.15  augustss 			nextphys = EHCI_NULL;
   2817        1.15  augustss 		}
   2818        1.15  augustss 
   2819       1.197     prlw1 		/* Find number of pages we'll be using, insert dma addresses */
   2820       1.197     prlw1 		pages = EHCI_PAGE(curlen + EHCI_PAGE_SIZE -1) >> 12;
   2821       1.197     prlw1 		KASSERT(pages <= EHCI_QTD_NBUFFERS);
   2822       1.197     prlw1 		pageoffs = EHCI_PAGE(curoffs);
   2823       1.197     prlw1 		for (i = 0; i < pages; i++) {
   2824       1.197     prlw1 			a = DMAADDR(dma, pageoffs + i * EHCI_PAGE_SIZE);
   2825       1.197     prlw1 			cur->qtd.qtd_buffer[i] = htole32(a & 0xFFFFF000);
   2826       1.197     prlw1 			/* Cast up to avoid compiler warnings */
   2827       1.197     prlw1 			cur->qtd.qtd_buffer_hi[i] = htole32((uint64_t)a >> 32);
   2828        1.15  augustss 		}
   2829       1.197     prlw1 
   2830       1.197     prlw1 		/* First buffer pointer requires a page offset to start at */
   2831       1.197     prlw1 		va = (vaddr_t)KERNADDR(dma, curoffs);
   2832       1.197     prlw1 		cur->qtd.qtd_buffer[0] |= htole32(EHCI_PAGE_OFFSET(va));
   2833       1.197     prlw1 
   2834        1.15  augustss 		cur->nextqtd = next;
   2835        1.66   mycroft 		cur->qtd.qtd_next = cur->qtd.qtd_altnext = nextphys;
   2836        1.15  augustss 		cur->qtd.qtd_status =
   2837        1.67   mycroft 		    htole32(qtdstatus | EHCI_QTD_SET_BYTES(curlen));
   2838        1.15  augustss 		cur->xfer = xfer;
   2839        1.18  augustss 		cur->len = curlen;
   2840       1.138    bouyer 
   2841       1.229     skrll 		USBHIST_LOG(ehcidebug, "cbp=0x%08zx end=0x%08zx",
   2842       1.232     skrll 		    (size_t)curoffs, (size_t)(curoffs + curlen), 0, 0);
   2843       1.197     prlw1 
   2844  1.234.2.18     skrll 		/*
   2845  1.234.2.18     skrll 		 * adjust the toggle based on the number of packets in this
   2846  1.234.2.18     skrll 		 * qtd
   2847  1.234.2.18     skrll 		 */
   2848        1.55   mycroft 		if (((curlen + mps - 1) / mps) & 1) {
   2849        1.55   mycroft 			tog ^= 1;
   2850        1.64   mycroft 			qtdstatus ^= EHCI_QTD_TOGGLE_MASK;
   2851        1.55   mycroft 		}
   2852       1.102  augustss 		if (next == NULL)
   2853        1.15  augustss 			break;
   2854       1.138    bouyer 		usb_syncmem(&cur->dma, cur->offs, sizeof(cur->qtd),
   2855       1.138    bouyer 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2856       1.229     skrll 		USBHIST_LOG(ehcidebug, "extend chain", 0, 0, 0, 0);
   2857       1.174  drochner 		if (len)
   2858       1.197     prlw1 			curoffs += curlen;
   2859        1.15  augustss 		cur = next;
   2860        1.15  augustss 	}
   2861        1.15  augustss 	cur->qtd.qtd_status |= htole32(EHCI_QTD_IOC);
   2862       1.138    bouyer 	usb_syncmem(&cur->dma, cur->offs, sizeof(cur->qtd),
   2863       1.138    bouyer 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2864        1.15  augustss 	*ep = cur;
   2865        1.55   mycroft 	epipe->nexttoggle = tog;
   2866        1.15  augustss 
   2867       1.229     skrll 	USBHIST_LOG(ehcidebug, "return sqtd=%p sqtdend=%p",
   2868       1.229     skrll 	    *sp, *ep, 0, 0);
   2869        1.29  augustss 
   2870  1.234.2.14     skrll 	return USBD_NORMAL_COMPLETION;
   2871        1.15  augustss 
   2872        1.15  augustss  nomem:
   2873        1.15  augustss 	/* XXX free chain */
   2874       1.229     skrll 	USBHIST_LOG(ehcidebug, "no memory", 0, 0, 0, 0);
   2875  1.234.2.14     skrll 	return USBD_NOMEM;
   2876        1.15  augustss }
   2877        1.15  augustss 
   2878        1.18  augustss Static void
   2879        1.25  augustss ehci_free_sqtd_chain(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd,
   2880        1.18  augustss 		    ehci_soft_qtd_t *sqtdend)
   2881        1.18  augustss {
   2882        1.18  augustss 	ehci_soft_qtd_t *p;
   2883        1.25  augustss 	int i;
   2884        1.18  augustss 
   2885       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   2886       1.229     skrll 
   2887       1.229     skrll 	USBHIST_LOG(ehcidebug, "sqtd=%p sqtdend=%p",
   2888       1.229     skrll 	    sqtd, sqtdend, 0, 0);
   2889        1.29  augustss 
   2890        1.25  augustss 	for (i = 0; sqtd != sqtdend; sqtd = p, i++) {
   2891        1.18  augustss 		p = sqtd->nextqtd;
   2892        1.18  augustss 		ehci_free_sqtd(sc, sqtd);
   2893        1.18  augustss 	}
   2894        1.18  augustss }
   2895        1.18  augustss 
   2896       1.164  uebayasi Static ehci_soft_itd_t *
   2897       1.139  jmcneill ehci_alloc_itd(ehci_softc_t *sc)
   2898       1.139  jmcneill {
   2899       1.139  jmcneill 	struct ehci_soft_itd *itd, *freeitd;
   2900       1.139  jmcneill 	usbd_status err;
   2901       1.190       mrg 	int i, offs, frindex, previndex;
   2902       1.139  jmcneill 	usb_dma_t dma;
   2903       1.139  jmcneill 
   2904       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   2905       1.229     skrll 
   2906       1.192       mrg 	mutex_enter(&sc->sc_lock);
   2907       1.139  jmcneill 
   2908       1.139  jmcneill 	/* Find an itd that wasn't freed this frame or last frame. This can
   2909       1.139  jmcneill 	 * discard itds that were freed before frindex wrapped around
   2910       1.139  jmcneill 	 * XXX - can this lead to thrashing? Could fix by enabling wrap-around
   2911       1.139  jmcneill 	 *       interrupt and fiddling with list when that happens */
   2912       1.139  jmcneill 	frindex = (EOREAD4(sc, EHCI_FRINDEX) + 1) >> 3;
   2913       1.139  jmcneill 	previndex = (frindex != 0) ? frindex - 1 : sc->sc_flsize;
   2914       1.139  jmcneill 
   2915       1.139  jmcneill 	freeitd = NULL;
   2916       1.139  jmcneill 	LIST_FOREACH(itd, &sc->sc_freeitds, u.free_list) {
   2917       1.139  jmcneill 		if (itd == NULL)
   2918       1.139  jmcneill 			break;
   2919       1.139  jmcneill 		if (itd->slot != frindex && itd->slot != previndex) {
   2920       1.139  jmcneill 			freeitd = itd;
   2921       1.139  jmcneill 			break;
   2922       1.139  jmcneill 		}
   2923       1.139  jmcneill 	}
   2924       1.139  jmcneill 
   2925       1.139  jmcneill 	if (freeitd == NULL) {
   2926       1.229     skrll 		USBHIST_LOG(ehcidebug, "allocating chunk", 0, 0, 0, 0);
   2927       1.139  jmcneill 		err = usb_allocmem(&sc->sc_bus, EHCI_ITD_SIZE * EHCI_ITD_CHUNK,
   2928       1.139  jmcneill 				EHCI_PAGE_SIZE, &dma);
   2929       1.139  jmcneill 
   2930       1.139  jmcneill 		if (err) {
   2931       1.229     skrll 			USBHIST_LOG(ehcidebug,
   2932       1.229     skrll 			    "alloc returned %d", err, 0, 0, 0);
   2933       1.192       mrg 			mutex_exit(&sc->sc_lock);
   2934       1.139  jmcneill 			return NULL;
   2935       1.139  jmcneill 		}
   2936       1.139  jmcneill 
   2937       1.139  jmcneill 		for (i = 0; i < EHCI_ITD_CHUNK; i++) {
   2938       1.139  jmcneill 			offs = i * EHCI_ITD_SIZE;
   2939       1.139  jmcneill 			itd = KERNADDR(&dma, offs);
   2940       1.139  jmcneill 			itd->physaddr = DMAADDR(&dma, offs);
   2941       1.183  jakllsch 	 		itd->dma = dma;
   2942       1.139  jmcneill 			itd->offs = offs;
   2943       1.139  jmcneill 			LIST_INSERT_HEAD(&sc->sc_freeitds, itd, u.free_list);
   2944       1.139  jmcneill 		}
   2945       1.139  jmcneill 		freeitd = LIST_FIRST(&sc->sc_freeitds);
   2946       1.139  jmcneill 	}
   2947       1.139  jmcneill 
   2948       1.139  jmcneill 	itd = freeitd;
   2949       1.139  jmcneill 	LIST_REMOVE(itd, u.free_list);
   2950       1.139  jmcneill 	memset(&itd->itd, 0, sizeof(ehci_itd_t));
   2951       1.139  jmcneill 	usb_syncmem(&itd->dma, itd->offs + offsetof(ehci_itd_t, itd_next),
   2952   1.234.2.2     skrll 	    sizeof(itd->itd.itd_next),
   2953   1.234.2.2     skrll 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2954       1.139  jmcneill 
   2955       1.139  jmcneill 	itd->u.frame_list.next = NULL;
   2956       1.139  jmcneill 	itd->u.frame_list.prev = NULL;
   2957       1.139  jmcneill 	itd->xfer_next = NULL;
   2958       1.139  jmcneill 	itd->slot = 0;
   2959       1.139  jmcneill 
   2960       1.192       mrg 	mutex_exit(&sc->sc_lock);
   2961       1.192       mrg 
   2962       1.139  jmcneill 	return itd;
   2963       1.139  jmcneill }
   2964       1.139  jmcneill 
   2965   1.234.2.3     skrll Static ehci_soft_sitd_t *
   2966   1.234.2.3     skrll ehci_alloc_sitd(ehci_softc_t *sc)
   2967   1.234.2.3     skrll {
   2968   1.234.2.3     skrll 	struct ehci_soft_sitd *sitd, *freesitd;
   2969   1.234.2.3     skrll 	usbd_status err;
   2970   1.234.2.3     skrll 	int i, offs, frindex, previndex;
   2971   1.234.2.3     skrll 	usb_dma_t dma;
   2972   1.234.2.3     skrll 
   2973   1.234.2.3     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   2974   1.234.2.3     skrll 
   2975   1.234.2.3     skrll 	mutex_enter(&sc->sc_lock);
   2976   1.234.2.3     skrll 
   2977   1.234.2.3     skrll 	/* Find an sitd that wasn't freed this frame or last frame. This can
   2978   1.234.2.3     skrll 	 * discard sitds that were freed before frindex wrapped around
   2979   1.234.2.3     skrll 	 * XXX - can this lead to thrashing? Could fix by enabling wrap-around
   2980   1.234.2.3     skrll 	 *       interrupt and fiddling with list when that happens */
   2981   1.234.2.3     skrll 	frindex = (EOREAD4(sc, EHCI_FRINDEX) + 1) >> 3;
   2982   1.234.2.3     skrll 	previndex = (frindex != 0) ? frindex - 1 : sc->sc_flsize;
   2983   1.234.2.3     skrll 
   2984   1.234.2.3     skrll 	freesitd = NULL;
   2985   1.234.2.3     skrll 	LIST_FOREACH(sitd, &sc->sc_freesitds, u.free_list) {
   2986   1.234.2.3     skrll 		if (sitd == NULL)
   2987   1.234.2.3     skrll 			break;
   2988   1.234.2.3     skrll 		if (sitd->slot != frindex && sitd->slot != previndex) {
   2989   1.234.2.3     skrll 			freesitd = sitd;
   2990   1.234.2.3     skrll 			break;
   2991   1.234.2.3     skrll 		}
   2992   1.234.2.3     skrll 	}
   2993   1.234.2.3     skrll 
   2994   1.234.2.3     skrll 	if (freesitd == NULL) {
   2995   1.234.2.3     skrll 		USBHIST_LOG(ehcidebug, "allocating chunk", 0, 0, 0, 0);
   2996   1.234.2.3     skrll 		err = usb_allocmem(&sc->sc_bus, EHCI_SITD_SIZE * EHCI_SITD_CHUNK,
   2997   1.234.2.3     skrll 				EHCI_PAGE_SIZE, &dma);
   2998   1.234.2.3     skrll 
   2999   1.234.2.3     skrll 		if (err) {
   3000   1.234.2.3     skrll 			USBHIST_LOG(ehcidebug,
   3001   1.234.2.3     skrll 			    "alloc returned %d", err, 0, 0, 0);
   3002   1.234.2.3     skrll 			mutex_exit(&sc->sc_lock);
   3003   1.234.2.3     skrll 			return NULL;
   3004   1.234.2.3     skrll 		}
   3005   1.234.2.3     skrll 
   3006   1.234.2.3     skrll 		for (i = 0; i < EHCI_SITD_CHUNK; i++) {
   3007   1.234.2.3     skrll 			offs = i * EHCI_SITD_SIZE;
   3008   1.234.2.3     skrll 			sitd = KERNADDR(&dma, offs);
   3009   1.234.2.3     skrll 			sitd->physaddr = DMAADDR(&dma, offs);
   3010   1.234.2.3     skrll 	 		sitd->dma = dma;
   3011   1.234.2.3     skrll 			sitd->offs = offs;
   3012   1.234.2.3     skrll 			LIST_INSERT_HEAD(&sc->sc_freesitds, sitd, u.free_list);
   3013   1.234.2.3     skrll 		}
   3014   1.234.2.3     skrll 		freesitd = LIST_FIRST(&sc->sc_freesitds);
   3015   1.234.2.3     skrll 	}
   3016   1.234.2.3     skrll 
   3017   1.234.2.3     skrll 	sitd = freesitd;
   3018   1.234.2.3     skrll 	LIST_REMOVE(sitd, u.free_list);
   3019   1.234.2.3     skrll 	memset(&sitd->sitd, 0, sizeof(ehci_sitd_t));
   3020   1.234.2.3     skrll 	usb_syncmem(&sitd->dma, sitd->offs + offsetof(ehci_sitd_t, sitd_next),
   3021   1.234.2.3     skrll 		    sizeof(sitd->sitd.sitd_next), BUS_DMASYNC_PREWRITE |
   3022   1.234.2.3     skrll 		    BUS_DMASYNC_PREREAD);
   3023   1.234.2.3     skrll 
   3024   1.234.2.3     skrll 	sitd->u.frame_list.next = NULL;
   3025   1.234.2.3     skrll 	sitd->u.frame_list.prev = NULL;
   3026   1.234.2.3     skrll 	sitd->xfer_next = NULL;
   3027   1.234.2.3     skrll 	sitd->slot = 0;
   3028   1.234.2.3     skrll 
   3029   1.234.2.3     skrll 	mutex_exit(&sc->sc_lock);
   3030   1.234.2.3     skrll 
   3031   1.234.2.3     skrll 	return sitd;
   3032   1.234.2.3     skrll }
   3033   1.234.2.3     skrll 
   3034       1.164  uebayasi Static void
   3035       1.139  jmcneill ehci_free_itd(ehci_softc_t *sc, ehci_soft_itd_t *itd)
   3036       1.139  jmcneill {
   3037       1.139  jmcneill 
   3038       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   3039       1.190       mrg 
   3040       1.150  jmcneill 	LIST_INSERT_HEAD(&sc->sc_freeitds, itd, u.free_list);
   3041       1.139  jmcneill }
   3042       1.139  jmcneill 
   3043   1.234.2.3     skrll Static void
   3044   1.234.2.3     skrll ehci_free_sitd(ehci_softc_t *sc, ehci_soft_sitd_t *sitd)
   3045   1.234.2.3     skrll {
   3046   1.234.2.3     skrll 
   3047   1.234.2.3     skrll 	KASSERT(mutex_owned(&sc->sc_lock));
   3048   1.234.2.3     skrll 
   3049   1.234.2.3     skrll 	LIST_INSERT_HEAD(&sc->sc_freesitds, sitd, u.free_list);
   3050   1.234.2.3     skrll }
   3051   1.234.2.3     skrll 
   3052        1.15  augustss /****************/
   3053        1.15  augustss 
   3054         1.9  augustss /*
   3055        1.10  augustss  * Close a reqular pipe.
   3056        1.10  augustss  * Assumes that there are no pending transactions.
   3057        1.10  augustss  */
   3058       1.164  uebayasi Static void
   3059        1.10  augustss ehci_close_pipe(usbd_pipe_handle pipe, ehci_soft_qh_t *head)
   3060        1.10  augustss {
   3061        1.10  augustss 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
   3062   1.234.2.8     skrll 	ehci_softc_t *sc = pipe->up_dev->ud_bus->ub_hcpriv;
   3063        1.10  augustss 	ehci_soft_qh_t *sqh = epipe->sqh;
   3064        1.10  augustss 
   3065       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   3066       1.190       mrg 
   3067        1.10  augustss 	ehci_rem_qh(sc, sqh, head);
   3068        1.10  augustss 	ehci_free_sqh(sc, epipe->sqh);
   3069        1.10  augustss }
   3070        1.10  augustss 
   3071        1.33  augustss /*
   3072        1.10  augustss  * Abort a device request.
   3073        1.10  augustss  * If this routine is called at splusb() it guarantees that the request
   3074        1.10  augustss  * will be removed from the hardware scheduling and that the callback
   3075        1.10  augustss  * for it will be called with USBD_CANCELLED status.
   3076        1.10  augustss  * It's impossible to guarantee that the requested transfer will not
   3077        1.10  augustss  * have happened since the hardware runs concurrently.
   3078        1.10  augustss  * If the transaction has already happened we rely on the ordinary
   3079        1.10  augustss  * interrupt processing to process it.
   3080        1.26  augustss  * XXX This is most probably wrong.
   3081       1.190       mrg  * XXXMRG this doesn't make sense anymore.
   3082        1.10  augustss  */
   3083       1.164  uebayasi Static void
   3084        1.10  augustss ehci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
   3085        1.10  augustss {
   3086        1.26  augustss #define exfer EXFER(xfer)
   3087   1.234.2.8     skrll 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->ux_pipe;
   3088   1.234.2.8     skrll 	ehci_softc_t *sc = epipe->pipe.up_dev->ud_bus->ub_hcpriv;
   3089        1.26  augustss 	ehci_soft_qh_t *sqh = epipe->sqh;
   3090        1.26  augustss 	ehci_soft_qtd_t *sqtd;
   3091        1.26  augustss 	ehci_physaddr_t cur;
   3092   1.234.2.1     skrll 	uint32_t qhstatus;
   3093        1.26  augustss 	int hit;
   3094        1.96  augustss 	int wake;
   3095        1.10  augustss 
   3096       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3097       1.229     skrll 
   3098       1.229     skrll 	USBHIST_LOG(ehcidebug, "xfer=%p pipe=%p", xfer, epipe, 0, 0);
   3099        1.10  augustss 
   3100       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   3101   1.234.2.4     skrll 	ASSERT_SLEEPABLE();
   3102       1.190       mrg 
   3103        1.17  augustss 	if (sc->sc_dying) {
   3104        1.17  augustss 		/* If we're dying, just do the software part. */
   3105   1.234.2.8     skrll 		xfer->ux_status = status;	/* make software ignore it */
   3106   1.234.2.8     skrll 		callout_stop(&xfer->ux_callout);
   3107        1.17  augustss 		usb_transfer_complete(xfer);
   3108        1.17  augustss 		return;
   3109        1.17  augustss 	}
   3110        1.17  augustss 
   3111        1.11  augustss 	/*
   3112        1.96  augustss 	 * If an abort is already in progress then just wait for it to
   3113        1.96  augustss 	 * complete and return.
   3114        1.96  augustss 	 */
   3115   1.234.2.8     skrll 	if (xfer->ux_hcflags & UXFER_ABORTING) {
   3116       1.229     skrll 		USBHIST_LOG(ehcidebug, "already aborting", 0, 0, 0, 0);
   3117        1.96  augustss #ifdef DIAGNOSTIC
   3118        1.96  augustss 		if (status == USBD_TIMEOUT)
   3119        1.96  augustss 			printf("ehci_abort_xfer: TIMEOUT while aborting\n");
   3120        1.96  augustss #endif
   3121        1.96  augustss 		/* Override the status which might be USBD_TIMEOUT. */
   3122   1.234.2.8     skrll 		xfer->ux_status = status;
   3123       1.229     skrll 		USBHIST_LOG(ehcidebug, "waiting for abort to finish",
   3124       1.229     skrll 			0, 0, 0, 0);
   3125   1.234.2.8     skrll 		xfer->ux_hcflags |= UXFER_ABORTWAIT;
   3126   1.234.2.8     skrll 		while (xfer->ux_hcflags & UXFER_ABORTING)
   3127   1.234.2.8     skrll 			cv_wait(&xfer->ux_hccv, &sc->sc_lock);
   3128        1.96  augustss 		return;
   3129        1.96  augustss 	}
   3130   1.234.2.8     skrll 	xfer->ux_hcflags |= UXFER_ABORTING;
   3131        1.96  augustss 
   3132        1.96  augustss 	/*
   3133        1.11  augustss 	 * Step 1: Make interrupt routine and hardware ignore xfer.
   3134        1.11  augustss 	 */
   3135   1.234.2.8     skrll 	xfer->ux_status = status;	/* make software ignore it */
   3136   1.234.2.8     skrll 	callout_stop(&xfer->ux_callout);
   3137       1.138    bouyer 
   3138       1.138    bouyer 	usb_syncmem(&sqh->dma,
   3139       1.138    bouyer 	    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
   3140       1.138    bouyer 	    sizeof(sqh->qh.qh_qtd.qtd_status),
   3141       1.138    bouyer 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3142        1.26  augustss 	qhstatus = sqh->qh.qh_qtd.qtd_status;
   3143        1.26  augustss 	sqh->qh.qh_qtd.qtd_status = qhstatus | htole32(EHCI_QTD_HALTED);
   3144       1.138    bouyer 	usb_syncmem(&sqh->dma,
   3145       1.138    bouyer 	    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
   3146       1.138    bouyer 	    sizeof(sqh->qh.qh_qtd.qtd_status),
   3147       1.138    bouyer 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3148  1.234.2.20     skrll 	for (sqtd = exfer->ex_sqtdstart; ; sqtd = sqtd->nextqtd) {
   3149       1.138    bouyer 		usb_syncmem(&sqtd->dma,
   3150       1.138    bouyer 		    sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
   3151       1.138    bouyer 		    sizeof(sqtd->qtd.qtd_status),
   3152       1.138    bouyer 		    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3153        1.26  augustss 		sqtd->qtd.qtd_status |= htole32(EHCI_QTD_HALTED);
   3154       1.138    bouyer 		usb_syncmem(&sqtd->dma,
   3155       1.138    bouyer 		    sqtd->offs + offsetof(ehci_qtd_t, qtd_status),
   3156       1.138    bouyer 		    sizeof(sqtd->qtd.qtd_status),
   3157       1.138    bouyer 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3158  1.234.2.20     skrll 		if (sqtd == exfer->ex_sqtdend)
   3159        1.26  augustss 			break;
   3160        1.26  augustss 	}
   3161        1.11  augustss 
   3162        1.33  augustss 	/*
   3163        1.11  augustss 	 * Step 2: Wait until we know hardware has finished any possible
   3164        1.11  augustss 	 * use of the xfer.  Also make sure the soft interrupt routine
   3165        1.11  augustss 	 * has run.
   3166        1.11  augustss 	 */
   3167        1.26  augustss 	ehci_sync_hc(sc);
   3168        1.29  augustss 	sc->sc_softwake = 1;
   3169        1.29  augustss 	usb_schedsoftintr(&sc->sc_bus);
   3170       1.190       mrg 	cv_wait(&sc->sc_softwake_cv, &sc->sc_lock);
   3171        1.33  augustss 
   3172        1.33  augustss 	/*
   3173        1.11  augustss 	 * Step 3: Remove any vestiges of the xfer from the hardware.
   3174        1.11  augustss 	 * The complication here is that the hardware may have executed
   3175        1.11  augustss 	 * beyond the xfer we're trying to abort.  So as we're scanning
   3176        1.11  augustss 	 * the TDs of this xfer we check if the hardware points to
   3177        1.11  augustss 	 * any of them.
   3178        1.11  augustss 	 */
   3179       1.138    bouyer 
   3180       1.138    bouyer 	usb_syncmem(&sqh->dma,
   3181       1.138    bouyer 	    sqh->offs + offsetof(ehci_qh_t, qh_curqtd),
   3182       1.138    bouyer 	    sizeof(sqh->qh.qh_curqtd),
   3183       1.138    bouyer 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3184        1.26  augustss 	cur = EHCI_LINK_ADDR(le32toh(sqh->qh.qh_curqtd));
   3185        1.26  augustss 	hit = 0;
   3186  1.234.2.20     skrll 	for (sqtd = exfer->ex_sqtdstart; ; sqtd = sqtd->nextqtd) {
   3187        1.26  augustss 		hit |= cur == sqtd->physaddr;
   3188  1.234.2.20     skrll 		if (sqtd == exfer->ex_sqtdend)
   3189        1.26  augustss 			break;
   3190        1.26  augustss 	}
   3191        1.26  augustss 	sqtd = sqtd->nextqtd;
   3192        1.26  augustss 	/* Zap curqtd register if hardware pointed inside the xfer. */
   3193        1.26  augustss 	if (hit && sqtd != NULL) {
   3194       1.229     skrll 		USBHIST_LOG(ehcidebug, "cur=0x%08x", sqtd->physaddr, 0, 0, 0);
   3195        1.26  augustss 		sqh->qh.qh_curqtd = htole32(sqtd->physaddr); /* unlink qTDs */
   3196       1.138    bouyer 		usb_syncmem(&sqh->dma,
   3197       1.138    bouyer 		    sqh->offs + offsetof(ehci_qh_t, qh_curqtd),
   3198       1.138    bouyer 		    sizeof(sqh->qh.qh_curqtd),
   3199       1.138    bouyer 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3200        1.26  augustss 		sqh->qh.qh_qtd.qtd_status = qhstatus;
   3201       1.138    bouyer 		usb_syncmem(&sqh->dma,
   3202       1.138    bouyer 		    sqh->offs + offsetof(ehci_qh_t, qh_qtd.qtd_status),
   3203       1.138    bouyer 		    sizeof(sqh->qh.qh_qtd.qtd_status),
   3204       1.138    bouyer 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3205        1.26  augustss 	} else {
   3206       1.229     skrll 		USBHIST_LOG(ehcidebug, "no hit", 0, 0, 0, 0);
   3207        1.26  augustss 	}
   3208        1.11  augustss 
   3209        1.11  augustss 	/*
   3210        1.26  augustss 	 * Step 4: Execute callback.
   3211        1.11  augustss 	 */
   3212        1.18  augustss #ifdef DIAGNOSTIC
   3213  1.234.2.20     skrll 	exfer->ex_isdone = 1;
   3214        1.18  augustss #endif
   3215   1.234.2.8     skrll 	wake = xfer->ux_hcflags & UXFER_ABORTWAIT;
   3216   1.234.2.8     skrll 	xfer->ux_hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT);
   3217        1.11  augustss 	usb_transfer_complete(xfer);
   3218       1.190       mrg 	if (wake) {
   3219   1.234.2.8     skrll 		cv_broadcast(&xfer->ux_hccv);
   3220       1.190       mrg 	}
   3221        1.11  augustss 
   3222       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   3223        1.26  augustss #undef exfer
   3224        1.10  augustss }
   3225        1.10  augustss 
   3226       1.164  uebayasi Static void
   3227       1.139  jmcneill ehci_abort_isoc_xfer(usbd_xfer_handle xfer, usbd_status status)
   3228       1.139  jmcneill {
   3229       1.139  jmcneill 	ehci_isoc_trans_t trans_status;
   3230       1.139  jmcneill 	struct ehci_pipe *epipe;
   3231       1.139  jmcneill 	struct ehci_xfer *exfer;
   3232       1.139  jmcneill 	ehci_softc_t *sc;
   3233       1.139  jmcneill 	struct ehci_soft_itd *itd;
   3234   1.234.2.3     skrll 	struct ehci_soft_sitd *sitd;
   3235       1.190       mrg 	int i, wake;
   3236       1.139  jmcneill 
   3237       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3238       1.229     skrll 
   3239   1.234.2.8     skrll 	epipe = (struct ehci_pipe *) xfer->ux_pipe;
   3240       1.139  jmcneill 	exfer = EXFER(xfer);
   3241   1.234.2.8     skrll 	sc = epipe->pipe.up_dev->ud_bus->ub_hcpriv;
   3242       1.139  jmcneill 
   3243       1.229     skrll 	USBHIST_LOG(ehcidebug, "xfer %p pipe %p", xfer, epipe, 0, 0);
   3244       1.139  jmcneill 
   3245       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   3246       1.190       mrg 
   3247       1.139  jmcneill 	if (sc->sc_dying) {
   3248   1.234.2.8     skrll 		xfer->ux_status = status;
   3249   1.234.2.8     skrll 		callout_stop(&xfer->ux_callout);
   3250       1.139  jmcneill 		usb_transfer_complete(xfer);
   3251       1.139  jmcneill 		return;
   3252       1.139  jmcneill 	}
   3253       1.139  jmcneill 
   3254   1.234.2.8     skrll 	if (xfer->ux_hcflags & UXFER_ABORTING) {
   3255       1.229     skrll 		USBHIST_LOG(ehcidebug, "already aborting", 0, 0, 0, 0);
   3256       1.139  jmcneill 
   3257       1.139  jmcneill #ifdef DIAGNOSTIC
   3258       1.139  jmcneill 		if (status == USBD_TIMEOUT)
   3259       1.190       mrg 			printf("ehci_abort_isoc_xfer: TIMEOUT while aborting\n");
   3260       1.139  jmcneill #endif
   3261       1.139  jmcneill 
   3262   1.234.2.8     skrll 		xfer->ux_status = status;
   3263       1.229     skrll 		USBHIST_LOG(ehcidebug,
   3264       1.229     skrll 		    "waiting for abort to finish", 0, 0, 0, 0);
   3265   1.234.2.8     skrll 		xfer->ux_hcflags |= UXFER_ABORTWAIT;
   3266   1.234.2.8     skrll 		while (xfer->ux_hcflags & UXFER_ABORTING)
   3267   1.234.2.8     skrll 			cv_wait(&xfer->ux_hccv, &sc->sc_lock);
   3268       1.190       mrg 		goto done;
   3269       1.139  jmcneill 	}
   3270   1.234.2.8     skrll 	xfer->ux_hcflags |= UXFER_ABORTING;
   3271       1.139  jmcneill 
   3272   1.234.2.8     skrll 	xfer->ux_status = status;
   3273   1.234.2.8     skrll 	callout_stop(&xfer->ux_callout);
   3274       1.139  jmcneill 
   3275  1.234.2.19     skrll 	if (xfer->ux_pipe->up_dev->ud_speed == USB_SPEED_HIGH) {
   3276  1.234.2.20     skrll 		for (itd = exfer->ex_itdstart; itd != NULL;
   3277  1.234.2.19     skrll 		     itd = itd->xfer_next) {
   3278  1.234.2.19     skrll 			usb_syncmem(&itd->dma,
   3279  1.234.2.19     skrll 			    itd->offs + offsetof(ehci_itd_t, itd_ctl),
   3280  1.234.2.19     skrll 			    sizeof(itd->itd.itd_ctl),
   3281  1.234.2.19     skrll 			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3282       1.139  jmcneill 
   3283  1.234.2.19     skrll 			for (i = 0; i < 8; i++) {
   3284  1.234.2.19     skrll 				trans_status = le32toh(itd->itd.itd_ctl[i]);
   3285  1.234.2.19     skrll 				trans_status &= ~EHCI_ITD_ACTIVE;
   3286  1.234.2.19     skrll 				itd->itd.itd_ctl[i] = htole32(trans_status);
   3287  1.234.2.19     skrll 			}
   3288       1.139  jmcneill 
   3289  1.234.2.19     skrll 			usb_syncmem(&itd->dma,
   3290  1.234.2.19     skrll 			    itd->offs + offsetof(ehci_itd_t, itd_ctl),
   3291  1.234.2.19     skrll 			    sizeof(itd->itd.itd_ctl),
   3292  1.234.2.19     skrll 			    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3293  1.234.2.19     skrll 		}
   3294  1.234.2.19     skrll 	} else {
   3295  1.234.2.20     skrll 		for (sitd = exfer->ex_sitdstart; sitd != NULL;
   3296  1.234.2.19     skrll 		     sitd = sitd->xfer_next) {
   3297  1.234.2.19     skrll 			usb_syncmem(&sitd->dma,
   3298  1.234.2.19     skrll 			    sitd->offs + offsetof(ehci_sitd_t, sitd_buffer),
   3299  1.234.2.19     skrll 			    sizeof(sitd->sitd.sitd_buffer),
   3300  1.234.2.19     skrll 			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   3301   1.234.2.3     skrll 
   3302  1.234.2.19     skrll 			trans_status = le32toh(sitd->sitd.sitd_trans);
   3303  1.234.2.19     skrll 			trans_status &= ~EHCI_SITD_ACTIVE;
   3304  1.234.2.19     skrll 			sitd->sitd.sitd_trans = htole32(trans_status);
   3305   1.234.2.3     skrll 
   3306  1.234.2.19     skrll 			usb_syncmem(&sitd->dma,
   3307  1.234.2.19     skrll 			    sitd->offs + offsetof(ehci_sitd_t, sitd_buffer),
   3308  1.234.2.19     skrll 			    sizeof(sitd->sitd.sitd_buffer),
   3309  1.234.2.19     skrll 			    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3310  1.234.2.19     skrll 		}
   3311   1.234.2.3     skrll 	}
   3312       1.139  jmcneill 
   3313   1.234.2.2     skrll 	sc->sc_softwake = 1;
   3314   1.234.2.2     skrll 	usb_schedsoftintr(&sc->sc_bus);
   3315       1.190       mrg 	cv_wait(&sc->sc_softwake_cv, &sc->sc_lock);
   3316       1.139  jmcneill 
   3317       1.139  jmcneill #ifdef DIAGNOSTIC
   3318  1.234.2.20     skrll 	exfer->ex_isdone = 1;
   3319       1.139  jmcneill #endif
   3320   1.234.2.8     skrll 	wake = xfer->ux_hcflags & UXFER_ABORTWAIT;
   3321   1.234.2.8     skrll 	xfer->ux_hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT);
   3322       1.139  jmcneill 	usb_transfer_complete(xfer);
   3323       1.190       mrg 	if (wake) {
   3324   1.234.2.8     skrll 		cv_broadcast(&xfer->ux_hccv);
   3325       1.190       mrg 	}
   3326       1.139  jmcneill 
   3327       1.190       mrg done:
   3328       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   3329       1.139  jmcneill 	return;
   3330       1.139  jmcneill }
   3331       1.139  jmcneill 
   3332       1.164  uebayasi Static void
   3333        1.15  augustss ehci_timeout(void *addr)
   3334        1.15  augustss {
   3335        1.15  augustss 	struct ehci_xfer *exfer = addr;
   3336  1.234.2.20     skrll 	struct ehci_pipe *epipe = (struct ehci_pipe *)exfer->ex_xfer.ux_pipe;
   3337   1.234.2.8     skrll 	ehci_softc_t *sc = epipe->pipe.up_dev->ud_bus->ub_hcpriv;
   3338        1.15  augustss 
   3339       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3340       1.229     skrll 
   3341       1.229     skrll 	USBHIST_LOG(ehcidebug, "exfer %p", exfer, 0, 0, 0);
   3342       1.158    sketch #ifdef EHCI_DEBUG
   3343        1.26  augustss 	if (ehcidebug > 1)
   3344  1.234.2.20     skrll 		usbd_dump_pipe(exfer->ex_xfer.ux_pipe);
   3345        1.22  augustss #endif
   3346        1.15  augustss 
   3347        1.17  augustss 	if (sc->sc_dying) {
   3348       1.190       mrg 		mutex_enter(&sc->sc_lock);
   3349  1.234.2.20     skrll 		ehci_abort_xfer(&exfer->ex_xfer, USBD_TIMEOUT);
   3350       1.190       mrg 		mutex_exit(&sc->sc_lock);
   3351        1.17  augustss 		return;
   3352        1.17  augustss 	}
   3353        1.17  augustss 
   3354        1.15  augustss 	/* Execute the abort in a process context. */
   3355  1.234.2.20     skrll 	usb_init_task(&exfer->ex_aborttask, ehci_timeout_task, addr,
   3356       1.203  jmcneill 	    USB_TASKQ_MPSAFE);
   3357  1.234.2.20     skrll 	usb_add_task(exfer->ex_xfer.ux_pipe->up_dev, &exfer->ex_aborttask,
   3358       1.114     joerg 	    USB_TASKQ_HC);
   3359        1.15  augustss }
   3360        1.15  augustss 
   3361       1.164  uebayasi Static void
   3362        1.15  augustss ehci_timeout_task(void *addr)
   3363        1.15  augustss {
   3364        1.15  augustss 	usbd_xfer_handle xfer = addr;
   3365   1.234.2.8     skrll 	ehci_softc_t *sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   3366        1.15  augustss 
   3367       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3368       1.229     skrll 
   3369       1.229     skrll 	USBHIST_LOG(ehcidebug, "xfer=%p", xfer, 0, 0, 0);
   3370        1.15  augustss 
   3371       1.190       mrg 	mutex_enter(&sc->sc_lock);
   3372        1.15  augustss 	ehci_abort_xfer(xfer, USBD_TIMEOUT);
   3373       1.190       mrg 	mutex_exit(&sc->sc_lock);
   3374        1.15  augustss }
   3375        1.15  augustss 
   3376         1.5  augustss /************************/
   3377         1.5  augustss 
   3378        1.10  augustss Static usbd_status
   3379        1.10  augustss ehci_device_ctrl_transfer(usbd_xfer_handle xfer)
   3380        1.10  augustss {
   3381   1.234.2.8     skrll 	ehci_softc_t *sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   3382        1.10  augustss 	usbd_status err;
   3383        1.10  augustss 
   3384        1.10  augustss 	/* Insert last in queue. */
   3385       1.190       mrg 	mutex_enter(&sc->sc_lock);
   3386        1.10  augustss 	err = usb_insert_transfer(xfer);
   3387       1.190       mrg 	mutex_exit(&sc->sc_lock);
   3388        1.10  augustss 	if (err)
   3389  1.234.2.14     skrll 		return err;
   3390        1.10  augustss 
   3391        1.10  augustss 	/* Pipe isn't running, start first */
   3392  1.234.2.14     skrll 	return ehci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   3393        1.10  augustss }
   3394        1.10  augustss 
   3395        1.12  augustss Static usbd_status
   3396        1.12  augustss ehci_device_ctrl_start(usbd_xfer_handle xfer)
   3397        1.12  augustss {
   3398   1.234.2.8     skrll 	ehci_softc_t *sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   3399        1.15  augustss 	usbd_status err;
   3400        1.15  augustss 
   3401        1.15  augustss 	if (sc->sc_dying)
   3402  1.234.2.14     skrll 		return USBD_IOERROR;
   3403        1.15  augustss 
   3404        1.15  augustss #ifdef DIAGNOSTIC
   3405   1.234.2.8     skrll 	if (!(xfer->ux_rqflags & URQ_REQUEST)) {
   3406        1.15  augustss 		/* XXX panic */
   3407        1.15  augustss 		printf("ehci_device_ctrl_transfer: not a request\n");
   3408  1.234.2.14     skrll 		return USBD_INVAL;
   3409        1.15  augustss 	}
   3410        1.15  augustss #endif
   3411        1.15  augustss 
   3412        1.15  augustss 	err = ehci_device_request(xfer);
   3413       1.190       mrg 	if (err) {
   3414  1.234.2.14     skrll 		return err;
   3415       1.190       mrg 	}
   3416        1.15  augustss 
   3417   1.234.2.8     skrll 	if (sc->sc_bus.ub_usepolling)
   3418        1.15  augustss 		ehci_waitintr(sc, xfer);
   3419       1.190       mrg 
   3420  1.234.2.14     skrll 	return USBD_IN_PROGRESS;
   3421        1.12  augustss }
   3422        1.10  augustss 
   3423       1.164  uebayasi Static void
   3424        1.10  augustss ehci_device_ctrl_done(usbd_xfer_handle xfer)
   3425        1.10  augustss {
   3426        1.18  augustss 	struct ehci_xfer *ex = EXFER(xfer);
   3427   1.234.2.8     skrll 	ehci_softc_t *sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   3428   1.234.2.8     skrll 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->ux_pipe;
   3429   1.234.2.8     skrll 	usb_device_request_t *req = &xfer->ux_request;
   3430       1.138    bouyer 	int len = UGETW(req->wLength);
   3431       1.138    bouyer 	int rd = req->bmRequestType & UT_READ;
   3432        1.18  augustss 
   3433       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3434       1.229     skrll 
   3435       1.229     skrll 	USBHIST_LOG(ehcidebug, "xfer=%p", xfer, 0, 0, 0);
   3436        1.10  augustss 
   3437   1.234.2.8     skrll 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
   3438       1.190       mrg 
   3439        1.10  augustss #ifdef DIAGNOSTIC
   3440   1.234.2.8     skrll 	if (!(xfer->ux_rqflags & URQ_REQUEST)) {
   3441        1.37    provos 		panic("ehci_ctrl_done: not a request");
   3442        1.10  augustss 	}
   3443        1.10  augustss #endif
   3444        1.18  augustss 
   3445   1.234.2.8     skrll 	if (xfer->ux_status != USBD_NOMEM && ehci_active_intr_list(ex)) {
   3446       1.153  jmcneill 		ehci_del_intr_list(sc, ex);	/* remove from active list */
   3447  1.234.2.20     skrll 		ehci_free_sqtd_chain(sc, ex->ex_sqtdstart, NULL);
   3448       1.138    bouyer 		usb_syncmem(&epipe->u.ctl.reqdma, 0, sizeof *req,
   3449       1.138    bouyer 		    BUS_DMASYNC_POSTWRITE);
   3450       1.138    bouyer 		if (len)
   3451   1.234.2.8     skrll 			usb_syncmem(&xfer->ux_dmabuf, 0, len,
   3452       1.138    bouyer 			    rd ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   3453        1.25  augustss 	}
   3454        1.18  augustss 
   3455   1.234.2.8     skrll 	USBHIST_LOG(ehcidebug, "length=%d", xfer->ux_actlen, 0, 0, 0);
   3456        1.10  augustss }
   3457        1.10  augustss 
   3458        1.10  augustss /* Abort a device control request. */
   3459        1.10  augustss Static void
   3460        1.10  augustss ehci_device_ctrl_abort(usbd_xfer_handle xfer)
   3461        1.10  augustss {
   3462       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3463       1.229     skrll 
   3464       1.229     skrll 	USBHIST_LOG(ehcidebug, "xfer=%p", xfer, 0, 0, 0);
   3465        1.10  augustss 	ehci_abort_xfer(xfer, USBD_CANCELLED);
   3466        1.10  augustss }
   3467        1.10  augustss 
   3468        1.10  augustss /* Close a device control pipe. */
   3469        1.10  augustss Static void
   3470        1.10  augustss ehci_device_ctrl_close(usbd_pipe_handle pipe)
   3471        1.10  augustss {
   3472   1.234.2.8     skrll 	ehci_softc_t *sc = pipe->up_dev->ud_bus->ub_hcpriv;
   3473        1.10  augustss 	/*struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;*/
   3474        1.10  augustss 
   3475       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3476       1.229     skrll 
   3477       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   3478       1.190       mrg 
   3479       1.229     skrll 	USBHIST_LOG(ehcidebug, "pipe=%p", pipe, 0, 0, 0);
   3480       1.190       mrg 
   3481        1.11  augustss 	ehci_close_pipe(pipe, sc->sc_async_head);
   3482        1.15  augustss }
   3483        1.15  augustss 
   3484       1.164  uebayasi Static usbd_status
   3485        1.15  augustss ehci_device_request(usbd_xfer_handle xfer)
   3486        1.15  augustss {
   3487        1.18  augustss #define exfer EXFER(xfer)
   3488   1.234.2.8     skrll 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->ux_pipe;
   3489   1.234.2.8     skrll 	usb_device_request_t *req = &xfer->ux_request;
   3490   1.234.2.8     skrll 	usbd_device_handle dev = epipe->pipe.up_dev;
   3491   1.234.2.8     skrll 	ehci_softc_t *sc = dev->ud_bus->ub_hcpriv;
   3492        1.15  augustss 	ehci_soft_qtd_t *setup, *stat, *next;
   3493        1.15  augustss 	ehci_soft_qh_t *sqh;
   3494        1.15  augustss 	int isread;
   3495        1.15  augustss 	int len;
   3496        1.15  augustss 	usbd_status err;
   3497        1.15  augustss 
   3498       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3499       1.229     skrll 
   3500        1.15  augustss 	isread = req->bmRequestType & UT_READ;
   3501        1.15  augustss 	len = UGETW(req->wLength);
   3502        1.15  augustss 
   3503       1.229     skrll 	USBHIST_LOG(ehcidebug, "type=0x%02x, request=0x%02x, "
   3504       1.229     skrll 	    "wValue=0x%04x, wIndex=0x%04x",
   3505       1.229     skrll 	    req->bmRequestType, req->bRequest, UGETW(req->wValue),
   3506       1.229     skrll 	    UGETW(req->wIndex));
   3507       1.229     skrll 	USBHIST_LOG(ehcidebug, "len=%d, addr=%d, endpt=%d",
   3508   1.234.2.8     skrll 	    len, dev->ud_addr,
   3509   1.234.2.8     skrll 	    epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress, 0);
   3510        1.15  augustss 
   3511        1.15  augustss 	setup = ehci_alloc_sqtd(sc);
   3512        1.15  augustss 	if (setup == NULL) {
   3513        1.15  augustss 		err = USBD_NOMEM;
   3514        1.15  augustss 		goto bad1;
   3515        1.15  augustss 	}
   3516        1.15  augustss 	stat = ehci_alloc_sqtd(sc);
   3517        1.15  augustss 	if (stat == NULL) {
   3518        1.15  augustss 		err = USBD_NOMEM;
   3519        1.15  augustss 		goto bad2;
   3520        1.15  augustss 	}
   3521        1.15  augustss 
   3522       1.190       mrg 	mutex_enter(&sc->sc_lock);
   3523       1.190       mrg 
   3524        1.15  augustss 	sqh = epipe->sqh;
   3525        1.15  augustss 
   3526   1.234.2.8     skrll 	KASSERTMSG(EHCI_QH_GET_ADDR(le32toh(sqh->qh.qh_endp)) == dev->ud_addr,
   3527       1.225     skrll 	    "address QH %d pipe %d\n",
   3528   1.234.2.8     skrll 	    EHCI_QH_GET_ADDR(le32toh(sqh->qh.qh_endp)), dev->ud_addr);
   3529       1.225     skrll 	KASSERTMSG(EHCI_QH_GET_MPL(le32toh(sqh->qh.qh_endp)) ==
   3530   1.234.2.8     skrll 	    UGETW(epipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize),
   3531       1.225     skrll 	    "MPS QH %d pipe %d\n",
   3532       1.225     skrll 	    EHCI_QH_GET_MPL(le32toh(sqh->qh.qh_endp)),
   3533   1.234.2.8     skrll 	    UGETW(epipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize));
   3534        1.15  augustss 
   3535        1.15  augustss 	/* Set up data transaction */
   3536        1.15  augustss 	if (len != 0) {
   3537        1.15  augustss 		ehci_soft_qtd_t *end;
   3538        1.15  augustss 
   3539        1.55   mycroft 		/* Start toggle at 1. */
   3540        1.55   mycroft 		epipe->nexttoggle = 1;
   3541        1.25  augustss 		err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
   3542        1.15  augustss 			  &next, &end);
   3543        1.15  augustss 		if (err)
   3544        1.15  augustss 			goto bad3;
   3545        1.83  augustss 		end->qtd.qtd_status &= htole32(~EHCI_QTD_IOC);
   3546        1.15  augustss 		end->nextqtd = stat;
   3547       1.214     skrll 		end->qtd.qtd_next = end->qtd.qtd_altnext =
   3548       1.214     skrll 		    htole32(stat->physaddr);
   3549       1.138    bouyer 		usb_syncmem(&end->dma, end->offs, sizeof(end->qtd),
   3550       1.138    bouyer 		   BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3551        1.15  augustss 	} else {
   3552        1.15  augustss 		next = stat;
   3553        1.15  augustss 	}
   3554        1.15  augustss 
   3555        1.30  augustss 	memcpy(KERNADDR(&epipe->u.ctl.reqdma, 0), req, sizeof *req);
   3556       1.138    bouyer 	usb_syncmem(&epipe->u.ctl.reqdma, 0, sizeof *req, BUS_DMASYNC_PREWRITE);
   3557        1.15  augustss 
   3558        1.55   mycroft 	/* Clear toggle */
   3559        1.15  augustss 	setup->qtd.qtd_status = htole32(
   3560        1.26  augustss 	    EHCI_QTD_ACTIVE |
   3561        1.15  augustss 	    EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) |
   3562        1.15  augustss 	    EHCI_QTD_SET_CERR(3) |
   3563        1.64   mycroft 	    EHCI_QTD_SET_TOGGLE(0) |
   3564        1.15  augustss 	    EHCI_QTD_SET_BYTES(sizeof *req)
   3565        1.15  augustss 	    );
   3566        1.31  augustss 	setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->u.ctl.reqdma, 0));
   3567        1.48   mycroft 	setup->qtd.qtd_buffer_hi[0] = 0;
   3568        1.15  augustss 	setup->nextqtd = next;
   3569        1.15  augustss 	setup->qtd.qtd_next = setup->qtd.qtd_altnext = htole32(next->physaddr);
   3570        1.15  augustss 	setup->xfer = xfer;
   3571        1.18  augustss 	setup->len = sizeof *req;
   3572       1.138    bouyer 	usb_syncmem(&setup->dma, setup->offs, sizeof(setup->qtd),
   3573       1.138    bouyer 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3574        1.15  augustss 
   3575        1.15  augustss 	stat->qtd.qtd_status = htole32(
   3576        1.26  augustss 	    EHCI_QTD_ACTIVE |
   3577        1.15  augustss 	    EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) |
   3578        1.15  augustss 	    EHCI_QTD_SET_CERR(3) |
   3579        1.64   mycroft 	    EHCI_QTD_SET_TOGGLE(1) |
   3580        1.15  augustss 	    EHCI_QTD_IOC
   3581        1.15  augustss 	    );
   3582        1.15  augustss 	stat->qtd.qtd_buffer[0] = 0; /* XXX not needed? */
   3583        1.48   mycroft 	stat->qtd.qtd_buffer_hi[0] = 0; /* XXX not needed? */
   3584        1.15  augustss 	stat->nextqtd = NULL;
   3585        1.15  augustss 	stat->qtd.qtd_next = stat->qtd.qtd_altnext = EHCI_NULL;
   3586        1.15  augustss 	stat->xfer = xfer;
   3587        1.18  augustss 	stat->len = 0;
   3588       1.138    bouyer 	usb_syncmem(&stat->dma, stat->offs, sizeof(stat->qtd),
   3589       1.138    bouyer 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   3590        1.15  augustss 
   3591        1.15  augustss #ifdef EHCI_DEBUG
   3592       1.229     skrll 	USBHIST_LOGN(ehcidebug, 5, "dump:", 0, 0, 0, 0);
   3593       1.229     skrll 	ehci_dump_sqh(sqh);
   3594       1.229     skrll 	ehci_dump_sqtds(setup);
   3595        1.15  augustss #endif
   3596        1.15  augustss 
   3597  1.234.2.20     skrll 	exfer->ex_sqtdstart = setup;
   3598  1.234.2.20     skrll 	exfer->ex_sqtdend = stat;
   3599        1.18  augustss #ifdef DIAGNOSTIC
   3600  1.234.2.20     skrll 	if (!exfer->ex_isdone) {
   3601        1.18  augustss 		printf("ehci_device_request: not done, exfer=%p\n", exfer);
   3602        1.18  augustss 	}
   3603  1.234.2.20     skrll 	exfer->ex_isdone = 0;
   3604        1.18  augustss #endif
   3605        1.18  augustss 
   3606        1.15  augustss 	/* Insert qTD in QH list. */
   3607       1.138    bouyer 	ehci_set_qh_qtd(sqh, setup); /* also does usb_syncmem(sqh) */
   3608   1.234.2.8     skrll 	if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
   3609   1.234.2.8     skrll 		callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
   3610       1.190       mrg 		    ehci_timeout, xfer);
   3611        1.15  augustss 	}
   3612        1.18  augustss 	ehci_add_intr_list(sc, exfer);
   3613   1.234.2.8     skrll 	xfer->ux_status = USBD_IN_PROGRESS;
   3614       1.190       mrg 	mutex_exit(&sc->sc_lock);
   3615        1.15  augustss 
   3616        1.17  augustss #ifdef EHCI_DEBUG
   3617       1.229     skrll 	USBHIST_LOGN(ehcidebug, 10, "status=%x, dump:",
   3618       1.229     skrll 	    EOREAD4(sc, EHCI_USBSTS), 0, 0, 0);
   3619       1.229     skrll //	delay(10000);
   3620       1.229     skrll 	ehci_dump_regs(sc);
   3621       1.229     skrll 	ehci_dump_sqh(sc->sc_async_head);
   3622       1.229     skrll 	ehci_dump_sqh(sqh);
   3623       1.229     skrll 	ehci_dump_sqtds(setup);
   3624        1.15  augustss #endif
   3625        1.15  augustss 
   3626  1.234.2.14     skrll 	return USBD_NORMAL_COMPLETION;
   3627        1.15  augustss 
   3628        1.15  augustss  bad3:
   3629       1.190       mrg 	mutex_exit(&sc->sc_lock);
   3630        1.15  augustss 	ehci_free_sqtd(sc, stat);
   3631        1.15  augustss  bad2:
   3632        1.15  augustss 	ehci_free_sqtd(sc, setup);
   3633        1.15  augustss  bad1:
   3634       1.229     skrll 	USBHIST_LOG(ehcidebug, "no memory", 0, 0, 0, 0);
   3635       1.190       mrg 	mutex_enter(&sc->sc_lock);
   3636   1.234.2.8     skrll 	xfer->ux_status = err;
   3637        1.25  augustss 	usb_transfer_complete(xfer);
   3638       1.190       mrg 	mutex_exit(&sc->sc_lock);
   3639  1.234.2.14     skrll 	return err;
   3640        1.18  augustss #undef exfer
   3641        1.10  augustss }
   3642        1.10  augustss 
   3643       1.108   xtraeme /*
   3644       1.108   xtraeme  * Some EHCI chips from VIA seem to trigger interrupts before writing back the
   3645       1.108   xtraeme  * qTD status, or miss signalling occasionally under heavy load.  If the host
   3646       1.108   xtraeme  * machine is too fast, we we can miss transaction completion - when we scan
   3647       1.108   xtraeme  * the active list the transaction still seems to be active.  This generally
   3648       1.108   xtraeme  * exhibits itself as a umass stall that never recovers.
   3649       1.108   xtraeme  *
   3650       1.108   xtraeme  * We work around this behaviour by setting up this callback after any softintr
   3651       1.108   xtraeme  * that completes with transactions still pending, giving us another chance to
   3652       1.108   xtraeme  * check for completion after the writeback has taken place.
   3653       1.108   xtraeme  */
   3654       1.164  uebayasi Static void
   3655       1.108   xtraeme ehci_intrlist_timeout(void *arg)
   3656       1.108   xtraeme {
   3657       1.108   xtraeme 	ehci_softc_t *sc = arg;
   3658       1.108   xtraeme 
   3659       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3660       1.229     skrll 
   3661       1.108   xtraeme 	usb_schedsoftintr(&sc->sc_bus);
   3662       1.108   xtraeme }
   3663       1.108   xtraeme 
   3664        1.10  augustss /************************/
   3665         1.5  augustss 
   3666        1.19  augustss Static usbd_status
   3667        1.19  augustss ehci_device_bulk_transfer(usbd_xfer_handle xfer)
   3668        1.19  augustss {
   3669   1.234.2.8     skrll 	ehci_softc_t *sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   3670        1.19  augustss 	usbd_status err;
   3671        1.19  augustss 
   3672        1.19  augustss 	/* Insert last in queue. */
   3673       1.190       mrg 	mutex_enter(&sc->sc_lock);
   3674        1.19  augustss 	err = usb_insert_transfer(xfer);
   3675       1.190       mrg 	mutex_exit(&sc->sc_lock);
   3676        1.19  augustss 	if (err)
   3677  1.234.2.14     skrll 		return err;
   3678        1.19  augustss 
   3679        1.19  augustss 	/* Pipe isn't running, start first */
   3680  1.234.2.14     skrll 	return ehci_device_bulk_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   3681        1.19  augustss }
   3682        1.19  augustss 
   3683       1.164  uebayasi Static usbd_status
   3684        1.19  augustss ehci_device_bulk_start(usbd_xfer_handle xfer)
   3685        1.19  augustss {
   3686        1.19  augustss #define exfer EXFER(xfer)
   3687   1.234.2.8     skrll 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->ux_pipe;
   3688   1.234.2.8     skrll 	usbd_device_handle dev = epipe->pipe.up_dev;
   3689   1.234.2.8     skrll 	ehci_softc_t *sc = dev->ud_bus->ub_hcpriv;
   3690        1.19  augustss 	ehci_soft_qtd_t *data, *dataend;
   3691        1.19  augustss 	ehci_soft_qh_t *sqh;
   3692        1.19  augustss 	usbd_status err;
   3693        1.19  augustss 	int len, isread, endpt;
   3694        1.19  augustss 
   3695       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3696       1.229     skrll 
   3697       1.229     skrll 	USBHIST_LOG(ehcidebug, "xfer=%p len=%d flags=%d",
   3698   1.234.2.8     skrll 	    xfer, xfer->ux_length, xfer->ux_flags, 0);
   3699        1.19  augustss 
   3700        1.19  augustss 	if (sc->sc_dying)
   3701  1.234.2.14     skrll 		return USBD_IOERROR;
   3702        1.19  augustss 
   3703        1.19  augustss #ifdef DIAGNOSTIC
   3704   1.234.2.8     skrll 	if (xfer->ux_rqflags & URQ_REQUEST)
   3705        1.72  augustss 		panic("ehci_device_bulk_start: a request");
   3706        1.19  augustss #endif
   3707        1.19  augustss 
   3708       1.190       mrg 	mutex_enter(&sc->sc_lock);
   3709       1.190       mrg 
   3710   1.234.2.8     skrll 	len = xfer->ux_length;
   3711   1.234.2.8     skrll 	endpt = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
   3712        1.19  augustss 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   3713        1.19  augustss 	sqh = epipe->sqh;
   3714        1.19  augustss 
   3715        1.19  augustss 	epipe->u.bulk.length = len;
   3716        1.19  augustss 
   3717        1.25  augustss 	err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data,
   3718        1.19  augustss 				   &dataend);
   3719        1.25  augustss 	if (err) {
   3720       1.229     skrll 		USBHIST_LOG(ehcidebug, "no memory", 0, 0, 0, 0);
   3721   1.234.2.8     skrll 		xfer->ux_status = err;
   3722        1.25  augustss 		usb_transfer_complete(xfer);
   3723       1.190       mrg 		mutex_exit(&sc->sc_lock);
   3724  1.234.2.14     skrll 		return err;
   3725        1.25  augustss 	}
   3726        1.19  augustss 
   3727        1.19  augustss #ifdef EHCI_DEBUG
   3728       1.229     skrll 	USBHIST_LOGN(ehcidebug, 5, "data(1):", 0, 0, 0, 0);
   3729       1.229     skrll 	ehci_dump_sqh(sqh);
   3730       1.229     skrll 	ehci_dump_sqtds(data);
   3731        1.19  augustss #endif
   3732        1.19  augustss 
   3733        1.19  augustss 	/* Set up interrupt info. */
   3734  1.234.2.20     skrll 	exfer->ex_sqtdstart = data;
   3735  1.234.2.20     skrll 	exfer->ex_sqtdend = dataend;
   3736        1.19  augustss #ifdef DIAGNOSTIC
   3737  1.234.2.20     skrll 	if (!exfer->ex_isdone) {
   3738        1.72  augustss 		printf("ehci_device_bulk_start: not done, ex=%p\n", exfer);
   3739        1.19  augustss 	}
   3740  1.234.2.20     skrll 	exfer->ex_isdone = 0;
   3741        1.19  augustss #endif
   3742        1.19  augustss 
   3743       1.138    bouyer 	ehci_set_qh_qtd(sqh, data); /* also does usb_syncmem(sqh) */
   3744   1.234.2.8     skrll 	if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
   3745   1.234.2.8     skrll 		callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
   3746       1.190       mrg 		    ehci_timeout, xfer);
   3747        1.19  augustss 	}
   3748        1.19  augustss 	ehci_add_intr_list(sc, exfer);
   3749   1.234.2.8     skrll 	xfer->ux_status = USBD_IN_PROGRESS;
   3750       1.190       mrg 	mutex_exit(&sc->sc_lock);
   3751        1.19  augustss 
   3752        1.19  augustss #ifdef EHCI_DEBUG
   3753       1.229     skrll 	USBHIST_LOGN(ehcidebug, 5, "data(2)", 0, 0, 0, 0);
   3754       1.229     skrll //	delay(10000);
   3755       1.229     skrll 	USBHIST_LOGN(ehcidebug, 5, "data(3)", 0, 0, 0, 0);
   3756       1.229     skrll 	ehci_dump_regs(sc);
   3757        1.29  augustss #if 0
   3758       1.229     skrll 	printf("async_head:\n");
   3759       1.229     skrll 	ehci_dump_sqh(sc->sc_async_head);
   3760        1.29  augustss #endif
   3761       1.229     skrll 	USBHIST_LOG(ehcidebug, "sqh:", 0, 0, 0, 0);
   3762       1.229     skrll 	ehci_dump_sqh(sqh);
   3763       1.229     skrll 	ehci_dump_sqtds(data);
   3764        1.19  augustss #endif
   3765        1.19  augustss 
   3766   1.234.2.8     skrll 	if (sc->sc_bus.ub_usepolling)
   3767        1.19  augustss 		ehci_waitintr(sc, xfer);
   3768        1.19  augustss 
   3769  1.234.2.14     skrll 	return USBD_IN_PROGRESS;
   3770        1.19  augustss #undef exfer
   3771        1.19  augustss }
   3772        1.19  augustss 
   3773        1.19  augustss Static void
   3774        1.19  augustss ehci_device_bulk_abort(usbd_xfer_handle xfer)
   3775        1.19  augustss {
   3776       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3777       1.229     skrll 
   3778       1.229     skrll 	USBHIST_LOG(ehcidebug, "xfer %p", xfer, 0, 0, 0);
   3779        1.19  augustss 	ehci_abort_xfer(xfer, USBD_CANCELLED);
   3780        1.19  augustss }
   3781        1.19  augustss 
   3782        1.33  augustss /*
   3783        1.19  augustss  * Close a device bulk pipe.
   3784        1.19  augustss  */
   3785        1.19  augustss Static void
   3786        1.19  augustss ehci_device_bulk_close(usbd_pipe_handle pipe)
   3787        1.19  augustss {
   3788   1.234.2.8     skrll 	ehci_softc_t *sc = pipe->up_dev->ud_bus->ub_hcpriv;
   3789       1.175  drochner 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
   3790        1.19  augustss 
   3791       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3792       1.229     skrll 
   3793       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   3794       1.190       mrg 
   3795       1.229     skrll 	USBHIST_LOG(ehcidebug, "pipe=%p", pipe, 0, 0, 0);
   3796   1.234.2.8     skrll 	pipe->up_endpoint->ue_toggle = epipe->nexttoggle;
   3797        1.19  augustss 	ehci_close_pipe(pipe, sc->sc_async_head);
   3798        1.19  augustss }
   3799        1.19  augustss 
   3800       1.164  uebayasi Static void
   3801        1.19  augustss ehci_device_bulk_done(usbd_xfer_handle xfer)
   3802        1.19  augustss {
   3803        1.19  augustss 	struct ehci_xfer *ex = EXFER(xfer);
   3804   1.234.2.8     skrll 	ehci_softc_t *sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   3805   1.234.2.8     skrll 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->ux_pipe;
   3806   1.234.2.8     skrll 	int endpt = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
   3807       1.138    bouyer 	int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
   3808        1.19  augustss 
   3809       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3810       1.229     skrll 
   3811       1.229     skrll 	USBHIST_LOG(ehcidebug, "xfer=%p, actlen=%d",
   3812   1.234.2.8     skrll 	    xfer, xfer->ux_actlen, 0, 0);
   3813        1.19  augustss 
   3814       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   3815       1.190       mrg 
   3816   1.234.2.8     skrll 	if (xfer->ux_status != USBD_NOMEM && ehci_active_intr_list(ex)) {
   3817       1.153  jmcneill 		ehci_del_intr_list(sc, ex);	/* remove from active list */
   3818  1.234.2.20     skrll 		ehci_free_sqtd_chain(sc, ex->ex_sqtdstart, NULL);
   3819   1.234.2.8     skrll 		usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
   3820       1.138    bouyer 		    rd ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   3821        1.25  augustss 	}
   3822        1.19  augustss 
   3823   1.234.2.8     skrll 	USBHIST_LOG(ehcidebug, "length=%d", xfer->ux_actlen, 0, 0, 0);
   3824        1.19  augustss }
   3825         1.5  augustss 
   3826        1.10  augustss /************************/
   3827        1.10  augustss 
   3828        1.78  augustss Static usbd_status
   3829        1.78  augustss ehci_device_setintr(ehci_softc_t *sc, ehci_soft_qh_t *sqh, int ival)
   3830        1.78  augustss {
   3831        1.78  augustss 	struct ehci_soft_islot *isp;
   3832        1.78  augustss 	int islot, lev;
   3833        1.78  augustss 
   3834        1.78  augustss 	/* Find a poll rate that is large enough. */
   3835        1.78  augustss 	for (lev = EHCI_IPOLLRATES - 1; lev > 0; lev--)
   3836        1.78  augustss 		if (EHCI_ILEV_IVAL(lev) <= ival)
   3837        1.78  augustss 			break;
   3838        1.78  augustss 
   3839        1.78  augustss 	/* Pick an interrupt slot at the right level. */
   3840        1.78  augustss 	/* XXX could do better than picking at random */
   3841        1.78  augustss 	sc->sc_rand = (sc->sc_rand + 191) % sc->sc_flsize;
   3842        1.78  augustss 	islot = EHCI_IQHIDX(lev, sc->sc_rand);
   3843        1.78  augustss 
   3844        1.78  augustss 	sqh->islot = islot;
   3845        1.78  augustss 	isp = &sc->sc_islots[islot];
   3846       1.190       mrg 	mutex_enter(&sc->sc_lock);
   3847       1.190       mrg 	ehci_add_qh(sc, sqh, isp->sqh);
   3848       1.190       mrg 	mutex_exit(&sc->sc_lock);
   3849        1.78  augustss 
   3850  1.234.2.14     skrll 	return USBD_NORMAL_COMPLETION;
   3851        1.78  augustss }
   3852        1.78  augustss 
   3853        1.78  augustss Static usbd_status
   3854        1.78  augustss ehci_device_intr_transfer(usbd_xfer_handle xfer)
   3855        1.78  augustss {
   3856   1.234.2.8     skrll 	ehci_softc_t *sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   3857        1.78  augustss 	usbd_status err;
   3858        1.78  augustss 
   3859        1.78  augustss 	/* Insert last in queue. */
   3860       1.190       mrg 	mutex_enter(&sc->sc_lock);
   3861        1.78  augustss 	err = usb_insert_transfer(xfer);
   3862       1.190       mrg 	mutex_exit(&sc->sc_lock);
   3863        1.78  augustss 	if (err)
   3864  1.234.2.14     skrll 		return err;
   3865        1.78  augustss 
   3866        1.78  augustss 	/*
   3867        1.78  augustss 	 * Pipe isn't running (otherwise err would be USBD_INPROG),
   3868        1.78  augustss 	 * so start it first.
   3869        1.78  augustss 	 */
   3870  1.234.2.14     skrll 	return ehci_device_intr_start(SIMPLEQ_FIRST(&xfer->ux_pipe->up_queue));
   3871        1.78  augustss }
   3872        1.78  augustss 
   3873        1.78  augustss Static usbd_status
   3874        1.78  augustss ehci_device_intr_start(usbd_xfer_handle xfer)
   3875        1.78  augustss {
   3876        1.78  augustss #define exfer EXFER(xfer)
   3877   1.234.2.8     skrll 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->ux_pipe;
   3878   1.234.2.8     skrll 	usbd_device_handle dev = xfer->ux_pipe->up_dev;
   3879   1.234.2.8     skrll 	ehci_softc_t *sc = dev->ud_bus->ub_hcpriv;
   3880        1.78  augustss 	ehci_soft_qtd_t *data, *dataend;
   3881        1.78  augustss 	ehci_soft_qh_t *sqh;
   3882        1.78  augustss 	usbd_status err;
   3883        1.78  augustss 	int len, isread, endpt;
   3884        1.78  augustss 
   3885       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3886       1.229     skrll 
   3887       1.229     skrll 	USBHIST_LOG(ehcidebug, "xfer=%p len=%d flags=%d",
   3888   1.234.2.8     skrll 	    xfer, xfer->ux_length, xfer->ux_flags, 0);
   3889        1.78  augustss 
   3890        1.78  augustss 	if (sc->sc_dying)
   3891  1.234.2.14     skrll 		return USBD_IOERROR;
   3892        1.78  augustss 
   3893        1.78  augustss #ifdef DIAGNOSTIC
   3894   1.234.2.8     skrll 	if (xfer->ux_rqflags & URQ_REQUEST)
   3895        1.78  augustss 		panic("ehci_device_intr_start: a request");
   3896        1.78  augustss #endif
   3897        1.78  augustss 
   3898       1.190       mrg 	mutex_enter(&sc->sc_lock);
   3899       1.190       mrg 
   3900   1.234.2.8     skrll 	len = xfer->ux_length;
   3901   1.234.2.8     skrll 	endpt = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
   3902        1.78  augustss 	isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   3903        1.78  augustss 	sqh = epipe->sqh;
   3904        1.78  augustss 
   3905        1.78  augustss 	epipe->u.intr.length = len;
   3906        1.78  augustss 
   3907        1.78  augustss 	err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data,
   3908        1.78  augustss 	    &dataend);
   3909        1.78  augustss 	if (err) {
   3910       1.229     skrll 		USBHIST_LOG(ehcidebug, "no memory", 0, 0, 0, 0);
   3911   1.234.2.8     skrll 		xfer->ux_status = err;
   3912        1.78  augustss 		usb_transfer_complete(xfer);
   3913       1.190       mrg 		mutex_exit(&sc->sc_lock);
   3914  1.234.2.14     skrll 		return err;
   3915        1.78  augustss 	}
   3916        1.78  augustss 
   3917        1.78  augustss #ifdef EHCI_DEBUG
   3918       1.229     skrll 	USBHIST_LOGN(ehcidebug, 5, "data(1)", 0, 0, 0, 0);
   3919       1.229     skrll 	ehci_dump_sqh(sqh);
   3920       1.229     skrll 	ehci_dump_sqtds(data);
   3921        1.78  augustss #endif
   3922        1.78  augustss 
   3923        1.78  augustss 	/* Set up interrupt info. */
   3924  1.234.2.20     skrll 	exfer->ex_sqtdstart = data;
   3925  1.234.2.20     skrll 	exfer->ex_sqtdend = dataend;
   3926        1.78  augustss #ifdef DIAGNOSTIC
   3927  1.234.2.20     skrll 	if (!exfer->ex_isdone) {
   3928        1.78  augustss 		printf("ehci_device_intr_start: not done, ex=%p\n", exfer);
   3929        1.78  augustss 	}
   3930  1.234.2.20     skrll 	exfer->ex_isdone = 0;
   3931        1.78  augustss #endif
   3932        1.78  augustss 
   3933       1.138    bouyer 	ehci_set_qh_qtd(sqh, data); /* also does usb_syncmem(sqh) */
   3934   1.234.2.8     skrll 	if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
   3935   1.234.2.8     skrll 		callout_reset(&xfer->ux_callout, mstohz(xfer->ux_timeout),
   3936       1.190       mrg 		    ehci_timeout, xfer);
   3937        1.78  augustss 	}
   3938        1.78  augustss 	ehci_add_intr_list(sc, exfer);
   3939   1.234.2.8     skrll 	xfer->ux_status = USBD_IN_PROGRESS;
   3940       1.190       mrg 	mutex_exit(&sc->sc_lock);
   3941        1.78  augustss 
   3942        1.78  augustss #ifdef EHCI_DEBUG
   3943       1.229     skrll 	USBHIST_LOGN(ehcidebug, 5, "data(2)", 0, 0, 0, 0);
   3944       1.229     skrll //	delay(10000);
   3945       1.229     skrll 	USBHIST_LOGN(ehcidebug, 5, "data(3)", 0, 0, 0, 0);
   3946       1.229     skrll 	ehci_dump_regs(sc);
   3947       1.229     skrll 	USBHIST_LOGN(ehcidebug, 5, "sqh:", 0, 0, 0, 0);
   3948       1.229     skrll 	ehci_dump_sqh(sqh);
   3949       1.229     skrll 	ehci_dump_sqtds(data);
   3950        1.78  augustss #endif
   3951        1.78  augustss 
   3952   1.234.2.8     skrll 	if (sc->sc_bus.ub_usepolling)
   3953        1.78  augustss 		ehci_waitintr(sc, xfer);
   3954        1.78  augustss 
   3955  1.234.2.14     skrll 	return USBD_IN_PROGRESS;
   3956        1.78  augustss #undef exfer
   3957        1.78  augustss }
   3958        1.78  augustss 
   3959        1.78  augustss Static void
   3960        1.78  augustss ehci_device_intr_abort(usbd_xfer_handle xfer)
   3961        1.78  augustss {
   3962       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   3963       1.229     skrll 
   3964       1.229     skrll 	USBHIST_LOG(ehcidebug, "xfer=%p", xfer, 0, 0, 0);
   3965   1.234.2.8     skrll 	KASSERT(xfer->ux_pipe->up_intrxfer == xfer);
   3966       1.227     skrll 
   3967       1.139  jmcneill 	/*
   3968       1.139  jmcneill 	 * XXX - abort_xfer uses ehci_sync_hc, which syncs via the advance
   3969       1.180       wiz 	 *       async doorbell. That's dependent on the async list, wheras
   3970       1.139  jmcneill 	 *       intr xfers are periodic, should not use this?
   3971       1.139  jmcneill 	 */
   3972        1.78  augustss 	ehci_abort_xfer(xfer, USBD_CANCELLED);
   3973        1.78  augustss }
   3974        1.78  augustss 
   3975        1.78  augustss Static void
   3976        1.78  augustss ehci_device_intr_close(usbd_pipe_handle pipe)
   3977        1.78  augustss {
   3978   1.234.2.8     skrll 	ehci_softc_t *sc = pipe->up_dev->ud_bus->ub_hcpriv;
   3979        1.78  augustss 	struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;
   3980        1.78  augustss 	struct ehci_soft_islot *isp;
   3981        1.78  augustss 
   3982       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   3983       1.190       mrg 
   3984        1.78  augustss 	isp = &sc->sc_islots[epipe->sqh->islot];
   3985        1.78  augustss 	ehci_close_pipe(pipe, isp->sqh);
   3986        1.78  augustss }
   3987        1.78  augustss 
   3988        1.78  augustss Static void
   3989        1.78  augustss ehci_device_intr_done(usbd_xfer_handle xfer)
   3990        1.78  augustss {
   3991        1.78  augustss #define exfer EXFER(xfer)
   3992        1.78  augustss 	struct ehci_xfer *ex = EXFER(xfer);
   3993   1.234.2.8     skrll 	ehci_softc_t *sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   3994   1.234.2.8     skrll 	struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->ux_pipe;
   3995        1.78  augustss 	ehci_soft_qtd_t *data, *dataend;
   3996        1.78  augustss 	ehci_soft_qh_t *sqh;
   3997        1.78  augustss 	usbd_status err;
   3998       1.190       mrg 	int len, isread, endpt;
   3999        1.78  augustss 
   4000       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   4001       1.229     skrll 
   4002       1.229     skrll 	USBHIST_LOG(ehcidebug, "xfer=%p, actlen=%d",
   4003   1.234.2.8     skrll 	    xfer, xfer->ux_actlen, 0, 0);
   4004        1.78  augustss 
   4005   1.234.2.8     skrll 	KASSERT(sc->sc_bus.ub_usepolling || mutex_owned(&sc->sc_lock));
   4006       1.190       mrg 
   4007   1.234.2.8     skrll 	if (xfer->ux_pipe->up_repeat) {
   4008  1.234.2.20     skrll 		ehci_free_sqtd_chain(sc, ex->ex_sqtdstart, NULL);
   4009        1.78  augustss 
   4010        1.78  augustss 		len = epipe->u.intr.length;
   4011   1.234.2.8     skrll 		xfer->ux_length = len;
   4012   1.234.2.8     skrll 		endpt = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
   4013        1.78  augustss 		isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   4014   1.234.2.8     skrll 		usb_syncmem(&xfer->ux_dmabuf, 0, len,
   4015       1.138    bouyer 		    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   4016        1.78  augustss 		sqh = epipe->sqh;
   4017        1.78  augustss 
   4018        1.78  augustss 		err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer,
   4019        1.78  augustss 		    &data, &dataend);
   4020        1.78  augustss 		if (err) {
   4021       1.229     skrll 			USBHIST_LOG(ehcidebug, "no memory", 0, 0, 0, 0);
   4022   1.234.2.8     skrll 			xfer->ux_status = err;
   4023        1.78  augustss 			return;
   4024        1.78  augustss 		}
   4025        1.78  augustss 
   4026        1.78  augustss 		/* Set up interrupt info. */
   4027  1.234.2.20     skrll 		exfer->ex_sqtdstart = data;
   4028  1.234.2.20     skrll 		exfer->ex_sqtdend = dataend;
   4029        1.78  augustss #ifdef DIAGNOSTIC
   4030  1.234.2.20     skrll 		if (!exfer->ex_isdone) {
   4031       1.229     skrll 			USBHIST_LOG(ehcidebug, "marked not done, ex = %p",
   4032       1.229     skrll 				exfer, 0, 0, 0);
   4033        1.78  augustss 			printf("ehci_device_intr_done: not done, ex=%p\n",
   4034        1.78  augustss 			    exfer);
   4035        1.78  augustss 		}
   4036  1.234.2.20     skrll 		exfer->ex_isdone = 0;
   4037        1.78  augustss #endif
   4038        1.78  augustss 
   4039       1.138    bouyer 		ehci_set_qh_qtd(sqh, data); /* also does usb_syncmem(sqh) */
   4040   1.234.2.8     skrll 		if (xfer->ux_timeout && !sc->sc_bus.ub_usepolling) {
   4041   1.234.2.8     skrll 			callout_reset(&xfer->ux_callout,
   4042   1.234.2.8     skrll 			    mstohz(xfer->ux_timeout), ehci_timeout, xfer);
   4043        1.78  augustss 		}
   4044        1.78  augustss 
   4045   1.234.2.8     skrll 		xfer->ux_status = USBD_IN_PROGRESS;
   4046   1.234.2.8     skrll 	} else if (xfer->ux_status != USBD_NOMEM && ehci_active_intr_list(ex)) {
   4047       1.153  jmcneill 		ehci_del_intr_list(sc, ex); /* remove from active list */
   4048  1.234.2.20     skrll 		ehci_free_sqtd_chain(sc, ex->ex_sqtdstart, NULL);
   4049   1.234.2.8     skrll 		endpt = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
   4050       1.138    bouyer 		isread = UE_GET_DIR(endpt) == UE_DIR_IN;
   4051   1.234.2.8     skrll 		usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length,
   4052       1.138    bouyer 		    isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
   4053        1.78  augustss 	}
   4054        1.78  augustss #undef exfer
   4055        1.78  augustss }
   4056        1.10  augustss 
   4057        1.10  augustss /************************/
   4058         1.5  augustss 
   4059       1.113  christos Static usbd_status
   4060   1.234.2.3     skrll ehci_device_fs_isoc_transfer(usbd_xfer_handle xfer)
   4061   1.234.2.3     skrll {
   4062   1.234.2.3     skrll 	usbd_status err;
   4063   1.234.2.3     skrll 
   4064   1.234.2.3     skrll 	err = usb_insert_transfer(xfer);
   4065   1.234.2.3     skrll 	if (err && err != USBD_IN_PROGRESS)
   4066   1.234.2.3     skrll 		return err;
   4067   1.234.2.3     skrll 
   4068   1.234.2.3     skrll 	return ehci_device_fs_isoc_start(xfer);
   4069   1.234.2.3     skrll }
   4070   1.234.2.3     skrll 
   4071   1.234.2.3     skrll Static usbd_status
   4072   1.234.2.3     skrll ehci_device_fs_isoc_start(usbd_xfer_handle xfer)
   4073   1.234.2.3     skrll {
   4074   1.234.2.3     skrll 	struct ehci_pipe *epipe;
   4075   1.234.2.3     skrll 	usbd_device_handle dev;
   4076   1.234.2.3     skrll 	ehci_softc_t *sc;
   4077   1.234.2.3     skrll 	struct ehci_xfer *exfer;
   4078   1.234.2.3     skrll 	ehci_soft_sitd_t *sitd, *prev, *start, *stop;
   4079   1.234.2.3     skrll 	usb_dma_t *dma_buf;
   4080   1.234.2.3     skrll 	int i, j, k, frames;
   4081   1.234.2.3     skrll 	int offs, total_length;
   4082   1.234.2.3     skrll 	int frindex;
   4083   1.234.2.3     skrll 	u_int huba, dir;
   4084   1.234.2.3     skrll 
   4085   1.234.2.3     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   4086   1.234.2.3     skrll 
   4087   1.234.2.3     skrll 	start = NULL;
   4088   1.234.2.3     skrll 	prev = NULL;
   4089   1.234.2.3     skrll 	sitd = NULL;
   4090   1.234.2.3     skrll 	total_length = 0;
   4091   1.234.2.3     skrll 	exfer = (struct ehci_xfer *) xfer;
   4092   1.234.2.8     skrll 	sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   4093   1.234.2.8     skrll 	dev = xfer->ux_pipe->up_dev;
   4094   1.234.2.8     skrll 	epipe = (struct ehci_pipe *)xfer->ux_pipe;
   4095   1.234.2.3     skrll 
   4096   1.234.2.3     skrll 	/*
   4097   1.234.2.3     skrll 	 * To allow continuous transfers, above we start all transfers
   4098   1.234.2.3     skrll 	 * immediately. However, we're still going to get usbd_start_next call
   4099   1.234.2.3     skrll 	 * this when another xfer completes. So, check if this is already
   4100   1.234.2.3     skrll 	 * in progress or not
   4101   1.234.2.3     skrll 	 */
   4102   1.234.2.3     skrll 
   4103  1.234.2.20     skrll 	if (exfer->ex_sitdstart != NULL)
   4104   1.234.2.3     skrll 		return USBD_IN_PROGRESS;
   4105   1.234.2.3     skrll 
   4106   1.234.2.3     skrll 	USBHIST_LOG(ehcidebug, "xfer %p len %d flags %d",
   4107   1.234.2.8     skrll 	    xfer, xfer->ux_length, xfer->ux_flags, 0);
   4108   1.234.2.3     skrll 
   4109   1.234.2.3     skrll 	if (sc->sc_dying)
   4110   1.234.2.3     skrll 		return USBD_IOERROR;
   4111   1.234.2.3     skrll 
   4112   1.234.2.3     skrll 	/*
   4113   1.234.2.3     skrll 	 * To avoid complication, don't allow a request right now that'll span
   4114   1.234.2.3     skrll 	 * the entire frame table. To within 4 frames, to allow some leeway
   4115   1.234.2.3     skrll 	 * on either side of where the hc currently is.
   4116   1.234.2.3     skrll 	 */
   4117   1.234.2.8     skrll 	if (epipe->pipe.up_endpoint->ue_edesc->bInterval *
   4118   1.234.2.8     skrll 			xfer->ux_nframes >= sc->sc_flsize - 4) {
   4119   1.234.2.3     skrll 		printf("ehci: isoc descriptor requested that spans the entire"
   4120   1.234.2.3     skrll 		    "frametable, too many frames\n");
   4121   1.234.2.3     skrll 		return USBD_INVAL;
   4122   1.234.2.3     skrll 	}
   4123   1.234.2.3     skrll 
   4124   1.234.2.3     skrll #ifdef DIAGNOSTIC
   4125   1.234.2.8     skrll 	if (xfer->ux_rqflags & URQ_REQUEST)
   4126   1.234.2.3     skrll 		panic("ehci_device_fs_isoc_start: request\n");
   4127   1.234.2.3     skrll 
   4128  1.234.2.20     skrll 	if (!exfer->ex_isdone)
   4129   1.234.2.3     skrll 		printf("ehci_device_fs_isoc_start: not done, ex = %p\n", exfer);
   4130  1.234.2.20     skrll 	exfer->ex_isdone = 0;
   4131   1.234.2.3     skrll #endif
   4132   1.234.2.3     skrll 
   4133   1.234.2.3     skrll 	/*
   4134   1.234.2.3     skrll 	 * Step 1: Allocate and initialize sitds.
   4135   1.234.2.3     skrll 	 */
   4136   1.234.2.3     skrll 
   4137   1.234.2.8     skrll 	i = epipe->pipe.up_endpoint->ue_edesc->bInterval;
   4138   1.234.2.3     skrll 	if (i > 16 || i == 0) {
   4139   1.234.2.3     skrll 		/* Spec page 271 says intervals > 16 are invalid */
   4140  1.234.2.23     skrll 		USBHIST_LOG(ehcidebug, "bInterval %d invalid", i, 0, 0, 0);
   4141   1.234.2.3     skrll 
   4142   1.234.2.3     skrll 		return USBD_INVAL;
   4143   1.234.2.3     skrll 	}
   4144   1.234.2.3     skrll 
   4145   1.234.2.8     skrll 	frames = xfer->ux_nframes;
   4146   1.234.2.3     skrll 
   4147   1.234.2.3     skrll 	if (frames == 0) {
   4148   1.234.2.3     skrll 		USBHIST_LOG(ehcidebug, "frames == 0", 0, 0, 0, 0);
   4149   1.234.2.3     skrll 
   4150   1.234.2.3     skrll 		return USBD_INVAL;
   4151   1.234.2.3     skrll 	}
   4152   1.234.2.3     skrll 
   4153   1.234.2.8     skrll 	dma_buf = &xfer->ux_dmabuf;
   4154   1.234.2.3     skrll 	offs = 0;
   4155   1.234.2.3     skrll 
   4156   1.234.2.3     skrll 	for (i = 0; i < frames; i++) {
   4157   1.234.2.3     skrll 		sitd = ehci_alloc_sitd(sc);
   4158   1.234.2.3     skrll 
   4159   1.234.2.3     skrll 		if (prev)
   4160   1.234.2.3     skrll 			prev->xfer_next = sitd;
   4161   1.234.2.3     skrll 		else
   4162   1.234.2.3     skrll 			start = sitd;
   4163   1.234.2.3     skrll 
   4164   1.234.2.3     skrll #ifdef DIAGNOSTIC
   4165   1.234.2.8     skrll 		if (xfer->ux_frlengths[i] > 0x3ff) {
   4166   1.234.2.3     skrll 			printf("ehci: invalid frame length\n");
   4167   1.234.2.8     skrll 			xfer->ux_frlengths[i] = 0x3ff;
   4168   1.234.2.3     skrll 		}
   4169   1.234.2.3     skrll #endif
   4170   1.234.2.3     skrll 
   4171   1.234.2.3     skrll 		sitd->sitd.sitd_trans = htole32(EHCI_SITD_ACTIVE |
   4172   1.234.2.8     skrll 		    EHCI_SITD_SET_LEN(xfer->ux_frlengths[i]));
   4173   1.234.2.3     skrll 
   4174   1.234.2.3     skrll 		/* Set page0 index and offset. */
   4175   1.234.2.3     skrll 		sitd->sitd.sitd_buffer[0] = htole32(DMAADDR(dma_buf, offs));
   4176   1.234.2.3     skrll 
   4177   1.234.2.8     skrll 		total_length += xfer->ux_frlengths[i];
   4178   1.234.2.8     skrll 		offs += xfer->ux_frlengths[i];
   4179   1.234.2.3     skrll 
   4180   1.234.2.3     skrll 		sitd->sitd.sitd_buffer[1] =
   4181   1.234.2.3     skrll 		    htole32(EHCI_SITD_SET_BPTR(DMAADDR(dma_buf, offs - 1)));
   4182   1.234.2.3     skrll 
   4183   1.234.2.8     skrll 		huba = dev->ud_myhsport->up_parent->ud_addr;
   4184   1.234.2.3     skrll 
   4185   1.234.2.3     skrll /*		if (sc->sc_flags & EHCIF_FREESCALE) {
   4186   1.234.2.3     skrll 			// Set hub address to 0 if embedded TT is used.
   4187   1.234.2.3     skrll 			if (huba == sc->sc_addr)
   4188   1.234.2.3     skrll 				huba = 0;
   4189   1.234.2.3     skrll 		}
   4190   1.234.2.3     skrll */
   4191   1.234.2.3     skrll 
   4192   1.234.2.8     skrll 		k = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
   4193   1.234.2.3     skrll 		dir = UE_GET_DIR(k) ? 1 : 0;
   4194   1.234.2.3     skrll 		sitd->sitd.sitd_endp =
   4195   1.234.2.3     skrll 		    htole32(EHCI_SITD_SET_ENDPT(UE_GET_ADDR(k)) |
   4196   1.234.2.8     skrll 		    EHCI_SITD_SET_DADDR(dev->ud_addr) |
   4197   1.234.2.8     skrll 		    EHCI_SITD_SET_PORT(dev->ud_myhsport->up_portno) |
   4198   1.234.2.3     skrll 		    EHCI_SITD_SET_HUBA(huba) |
   4199   1.234.2.3     skrll 		    EHCI_SITD_SET_DIR(dir));
   4200   1.234.2.3     skrll 
   4201   1.234.2.3     skrll 		sitd->sitd.sitd_back = htole32(EHCI_LINK_TERMINATE);
   4202   1.234.2.3     skrll 
   4203   1.234.2.3     skrll 		/* XXX */
   4204   1.234.2.3     skrll 		u_char sa, sb;
   4205   1.234.2.3     skrll 		u_int temp, tlen;
   4206   1.234.2.3     skrll 		sa = 0;
   4207   1.234.2.3     skrll 
   4208   1.234.2.3     skrll 		if (dir == 0) {	/* OUT */
   4209   1.234.2.3     skrll 			temp = 0;
   4210   1.234.2.8     skrll 			tlen = xfer->ux_frlengths[i];
   4211   1.234.2.3     skrll 			if (tlen <= 188) {
   4212   1.234.2.3     skrll 				temp |= 1;	/* T-count = 1, TP = ALL */
   4213   1.234.2.3     skrll 				tlen = 1;
   4214   1.234.2.3     skrll 			} else {
   4215   1.234.2.3     skrll 				tlen += 187;
   4216   1.234.2.3     skrll 				tlen /= 188;
   4217   1.234.2.3     skrll 				temp |= tlen;	/* T-count = [1..6] */
   4218   1.234.2.3     skrll 				temp |= 8;	/* TP = Begin */
   4219   1.234.2.3     skrll 			}
   4220   1.234.2.3     skrll 			sitd->sitd.sitd_buffer[1] |= htole32(temp);
   4221   1.234.2.3     skrll 
   4222   1.234.2.3     skrll 			tlen += sa;
   4223   1.234.2.3     skrll 
   4224   1.234.2.3     skrll 			if (tlen >= 8) {
   4225   1.234.2.3     skrll 				sb = 0;
   4226   1.234.2.3     skrll 			} else {
   4227   1.234.2.3     skrll 				sb = (1 << tlen);
   4228   1.234.2.3     skrll 			}
   4229   1.234.2.3     skrll 
   4230   1.234.2.3     skrll 			sa = (1 << sa);
   4231   1.234.2.3     skrll 			sa = (sb - sa) & 0x3F;
   4232   1.234.2.3     skrll 			sb = 0;
   4233   1.234.2.3     skrll 		} else {
   4234   1.234.2.3     skrll 			sb = (-(4 << sa)) & 0xFE;
   4235   1.234.2.3     skrll 			sa = (1 << sa) & 0x3F;
   4236   1.234.2.3     skrll 			sa = 0x01;
   4237   1.234.2.3     skrll 			sb = 0xfc;
   4238   1.234.2.3     skrll 		}
   4239   1.234.2.3     skrll 
   4240   1.234.2.3     skrll 		sitd->sitd.sitd_sched = htole32(EHCI_SITD_SET_SMASK(sa) |
   4241   1.234.2.3     skrll 		    EHCI_SITD_SET_CMASK(sb));
   4242   1.234.2.3     skrll 
   4243   1.234.2.3     skrll 		prev = sitd;
   4244   1.234.2.3     skrll 	} /* End of frame */
   4245   1.234.2.3     skrll 
   4246   1.234.2.3     skrll 	sitd->sitd.sitd_trans |= htole32(EHCI_SITD_IOC);
   4247   1.234.2.3     skrll 
   4248   1.234.2.3     skrll 	stop = sitd;
   4249   1.234.2.3     skrll 	stop->xfer_next = NULL;
   4250   1.234.2.3     skrll 
   4251  1.234.2.20     skrll 	usb_syncmem(&exfer->ex_xfer.ux_dmabuf, 0, total_length,
   4252   1.234.2.3     skrll 		BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   4253   1.234.2.3     skrll 
   4254   1.234.2.3     skrll 	/*
   4255   1.234.2.3     skrll 	 * Part 2: Transfer descriptors have now been set up, now they must
   4256   1.234.2.3     skrll 	 * be scheduled into the periodic frame list. Erk. Not wanting to
   4257   1.234.2.3     skrll 	 * complicate matters, transfer is denied if the transfer spans
   4258   1.234.2.3     skrll 	 * more than the period frame list.
   4259   1.234.2.3     skrll 	 */
   4260   1.234.2.3     skrll 
   4261   1.234.2.3     skrll 	mutex_enter(&sc->sc_lock);
   4262   1.234.2.3     skrll 
   4263   1.234.2.3     skrll 	/* Start inserting frames */
   4264   1.234.2.3     skrll 	if (epipe->u.isoc.cur_xfers > 0) {
   4265   1.234.2.3     skrll 		frindex = epipe->u.isoc.next_frame;
   4266   1.234.2.3     skrll 	} else {
   4267   1.234.2.3     skrll 		frindex = EOREAD4(sc, EHCI_FRINDEX);
   4268   1.234.2.3     skrll 		frindex = frindex >> 3; /* Erase microframe index */
   4269   1.234.2.3     skrll 		frindex += 2;
   4270   1.234.2.3     skrll 	}
   4271   1.234.2.3     skrll 
   4272   1.234.2.3     skrll 	if (frindex >= sc->sc_flsize)
   4273   1.234.2.3     skrll 		frindex &= (sc->sc_flsize - 1);
   4274   1.234.2.3     skrll 
   4275   1.234.2.3     skrll 	/* Whats the frame interval? */
   4276   1.234.2.8     skrll 	i = epipe->pipe.up_endpoint->ue_edesc->bInterval;
   4277   1.234.2.3     skrll 
   4278   1.234.2.3     skrll 	sitd = start;
   4279   1.234.2.3     skrll 	for (j = 0; j < frames; j++) {
   4280   1.234.2.3     skrll 		if (sitd == NULL)
   4281   1.234.2.3     skrll 			panic("ehci: unexpectedly ran out of isoc sitds\n");
   4282   1.234.2.3     skrll 
   4283   1.234.2.3     skrll 		sitd->sitd.sitd_next = sc->sc_flist[frindex];
   4284   1.234.2.3     skrll 		if (sitd->sitd.sitd_next == 0)
   4285   1.234.2.3     skrll 			/* FIXME: frindex table gets initialized to NULL
   4286   1.234.2.3     skrll 			 * or EHCI_NULL? */
   4287   1.234.2.3     skrll 			sitd->sitd.sitd_next = EHCI_NULL;
   4288   1.234.2.3     skrll 
   4289   1.234.2.3     skrll 		usb_syncmem(&sitd->dma,
   4290   1.234.2.3     skrll 		    sitd->offs + offsetof(ehci_sitd_t, sitd_next),
   4291   1.234.2.3     skrll 		    sizeof(ehci_sitd_t),
   4292   1.234.2.3     skrll 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   4293   1.234.2.3     skrll 
   4294   1.234.2.3     skrll 		sc->sc_flist[frindex] =
   4295   1.234.2.3     skrll 		    htole32(EHCI_LINK_SITD | sitd->physaddr);
   4296   1.234.2.3     skrll 
   4297   1.234.2.3     skrll 		usb_syncmem(&sc->sc_fldma,
   4298   1.234.2.3     skrll 		    sizeof(ehci_link_t) * frindex,
   4299   1.234.2.3     skrll 		    sizeof(ehci_link_t),
   4300   1.234.2.3     skrll 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   4301   1.234.2.3     skrll 
   4302   1.234.2.3     skrll 		sitd->u.frame_list.next = sc->sc_softsitds[frindex];
   4303   1.234.2.3     skrll 		sc->sc_softsitds[frindex] = sitd;
   4304   1.234.2.3     skrll 		if (sitd->u.frame_list.next != NULL)
   4305   1.234.2.3     skrll 			sitd->u.frame_list.next->u.frame_list.prev = sitd;
   4306   1.234.2.3     skrll 		sitd->slot = frindex;
   4307   1.234.2.3     skrll 		sitd->u.frame_list.prev = NULL;
   4308   1.234.2.3     skrll 
   4309   1.234.2.3     skrll 		frindex += i;
   4310   1.234.2.3     skrll 		if (frindex >= sc->sc_flsize)
   4311   1.234.2.3     skrll 			frindex -= sc->sc_flsize;
   4312   1.234.2.3     skrll 
   4313   1.234.2.3     skrll 		sitd = sitd->xfer_next;
   4314   1.234.2.3     skrll 	}
   4315   1.234.2.3     skrll 
   4316   1.234.2.3     skrll 	epipe->u.isoc.cur_xfers++;
   4317   1.234.2.3     skrll 	epipe->u.isoc.next_frame = frindex;
   4318   1.234.2.3     skrll 
   4319  1.234.2.20     skrll 	exfer->ex_sitdstart = start;
   4320  1.234.2.20     skrll 	exfer->ex_sitdend = stop;
   4321   1.234.2.3     skrll 
   4322   1.234.2.3     skrll 	ehci_add_intr_list(sc, exfer);
   4323   1.234.2.8     skrll 	xfer->ux_status = USBD_IN_PROGRESS;
   4324   1.234.2.8     skrll 	xfer->ux_done = 0;
   4325   1.234.2.3     skrll 
   4326   1.234.2.3     skrll 	mutex_exit(&sc->sc_lock);
   4327   1.234.2.3     skrll 
   4328   1.234.2.8     skrll 	if (sc->sc_bus.ub_usepolling) {
   4329   1.234.2.3     skrll 		printf("Starting ehci isoc xfer with polling. Bad idea?\n");
   4330   1.234.2.3     skrll 		ehci_waitintr(sc, xfer);
   4331   1.234.2.3     skrll 	}
   4332   1.234.2.3     skrll 
   4333   1.234.2.3     skrll 	return USBD_IN_PROGRESS;
   4334   1.234.2.3     skrll }
   4335   1.234.2.3     skrll 
   4336   1.234.2.3     skrll Static void
   4337   1.234.2.3     skrll ehci_device_fs_isoc_abort(usbd_xfer_handle xfer)
   4338   1.234.2.3     skrll {
   4339   1.234.2.3     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   4340   1.234.2.3     skrll 
   4341   1.234.2.3     skrll 	USBHIST_LOG(ehcidebug, "xfer = %p", xfer, 0, 0, 0);
   4342   1.234.2.3     skrll 	ehci_abort_isoc_xfer(xfer, USBD_CANCELLED);
   4343   1.234.2.3     skrll }
   4344   1.234.2.3     skrll 
   4345   1.234.2.3     skrll Static void
   4346   1.234.2.3     skrll ehci_device_fs_isoc_close(usbd_pipe_handle pipe)
   4347   1.234.2.3     skrll {
   4348   1.234.2.3     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   4349   1.234.2.3     skrll 
   4350   1.234.2.3     skrll 	USBHIST_LOG(ehcidebug, "nothing in the pipe to free?", 0, 0, 0, 0);
   4351   1.234.2.3     skrll }
   4352   1.234.2.3     skrll 
   4353   1.234.2.3     skrll Static void
   4354   1.234.2.3     skrll ehci_device_fs_isoc_done(usbd_xfer_handle xfer)
   4355   1.234.2.3     skrll {
   4356   1.234.2.3     skrll 	struct ehci_xfer *exfer;
   4357   1.234.2.3     skrll 	ehci_softc_t *sc;
   4358   1.234.2.3     skrll 	struct ehci_pipe *epipe;
   4359   1.234.2.3     skrll 
   4360   1.234.2.3     skrll 	exfer = EXFER(xfer);
   4361   1.234.2.8     skrll 	sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   4362   1.234.2.8     skrll 	epipe = (struct ehci_pipe *) xfer->ux_pipe;
   4363   1.234.2.3     skrll 
   4364   1.234.2.3     skrll 	KASSERT(mutex_owned(&sc->sc_lock));
   4365   1.234.2.3     skrll 
   4366   1.234.2.3     skrll 	epipe->u.isoc.cur_xfers--;
   4367   1.234.2.8     skrll 	if (xfer->ux_status != USBD_NOMEM && ehci_active_intr_list(exfer)) {
   4368   1.234.2.3     skrll 		ehci_del_intr_list(sc, exfer);
   4369   1.234.2.3     skrll 		ehci_rem_free_sitd_chain(sc, exfer);
   4370   1.234.2.3     skrll 	}
   4371   1.234.2.3     skrll 
   4372   1.234.2.8     skrll 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length, BUS_DMASYNC_POSTWRITE |
   4373   1.234.2.3     skrll 		    BUS_DMASYNC_POSTREAD);
   4374   1.234.2.3     skrll }
   4375   1.234.2.3     skrll Static usbd_status
   4376       1.115  christos ehci_device_isoc_transfer(usbd_xfer_handle xfer)
   4377       1.113  christos {
   4378   1.234.2.8     skrll 	ehci_softc_t *sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   4379       1.139  jmcneill 	usbd_status err;
   4380       1.139  jmcneill 
   4381       1.190       mrg 	mutex_enter(&sc->sc_lock);
   4382       1.139  jmcneill 	err = usb_insert_transfer(xfer);
   4383       1.190       mrg 	mutex_exit(&sc->sc_lock);
   4384       1.139  jmcneill 	if (err && err != USBD_IN_PROGRESS)
   4385       1.139  jmcneill 		return err;
   4386       1.139  jmcneill 
   4387       1.139  jmcneill 	return ehci_device_isoc_start(xfer);
   4388       1.113  christos }
   4389       1.139  jmcneill 
   4390       1.113  christos Static usbd_status
   4391       1.115  christos ehci_device_isoc_start(usbd_xfer_handle xfer)
   4392       1.113  christos {
   4393       1.139  jmcneill 	struct ehci_pipe *epipe;
   4394       1.139  jmcneill 	ehci_softc_t *sc;
   4395       1.139  jmcneill 	struct ehci_xfer *exfer;
   4396       1.139  jmcneill 	ehci_soft_itd_t *itd, *prev, *start, *stop;
   4397       1.139  jmcneill 	usb_dma_t *dma_buf;
   4398       1.142  drochner 	int i, j, k, frames, uframes, ufrperframe;
   4399       1.190       mrg 	int trans_count, offs, total_length;
   4400       1.139  jmcneill 	int frindex;
   4401       1.139  jmcneill 
   4402       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   4403       1.229     skrll 
   4404       1.139  jmcneill 	start = NULL;
   4405       1.139  jmcneill 	prev = NULL;
   4406       1.139  jmcneill 	itd = NULL;
   4407       1.139  jmcneill 	trans_count = 0;
   4408       1.139  jmcneill 	total_length = 0;
   4409       1.139  jmcneill 	exfer = (struct ehci_xfer *) xfer;
   4410   1.234.2.8     skrll 	sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   4411   1.234.2.8     skrll 	epipe = (struct ehci_pipe *)xfer->ux_pipe;
   4412       1.139  jmcneill 
   4413       1.139  jmcneill 	/*
   4414       1.139  jmcneill 	 * To allow continuous transfers, above we start all transfers
   4415       1.139  jmcneill 	 * immediately. However, we're still going to get usbd_start_next call
   4416       1.139  jmcneill 	 * this when another xfer completes. So, check if this is already
   4417       1.139  jmcneill 	 * in progress or not
   4418       1.139  jmcneill 	 */
   4419       1.139  jmcneill 
   4420  1.234.2.20     skrll 	if (exfer->ex_itdstart != NULL)
   4421       1.139  jmcneill 		return USBD_IN_PROGRESS;
   4422       1.139  jmcneill 
   4423       1.229     skrll 	USBHIST_LOG(ehcidebug, "xfer %p len %d flags %d",
   4424   1.234.2.8     skrll 	    xfer, xfer->ux_length, xfer->ux_flags, 0);
   4425       1.139  jmcneill 
   4426       1.139  jmcneill 	if (sc->sc_dying)
   4427       1.139  jmcneill 		return USBD_IOERROR;
   4428       1.139  jmcneill 
   4429       1.139  jmcneill 	/*
   4430       1.139  jmcneill 	 * To avoid complication, don't allow a request right now that'll span
   4431       1.139  jmcneill 	 * the entire frame table. To within 4 frames, to allow some leeway
   4432       1.139  jmcneill 	 * on either side of where the hc currently is.
   4433       1.139  jmcneill 	 */
   4434   1.234.2.8     skrll 	if ((1 << (epipe->pipe.up_endpoint->ue_edesc->bInterval)) *
   4435   1.234.2.8     skrll 			xfer->ux_nframes >= (sc->sc_flsize - 4) * 8) {
   4436       1.229     skrll 		USBHIST_LOG(ehcidebug,
   4437       1.229     skrll 		    "isoc descriptor spans entire frametable", 0, 0, 0, 0);
   4438       1.139  jmcneill 		printf("ehci: isoc descriptor requested that spans the entire frametable, too many frames\n");
   4439       1.139  jmcneill 		return USBD_INVAL;
   4440       1.139  jmcneill 	}
   4441       1.139  jmcneill 
   4442       1.139  jmcneill #ifdef DIAGNOSTIC
   4443   1.234.2.8     skrll 	if (xfer->ux_rqflags & URQ_REQUEST)
   4444       1.139  jmcneill 		panic("ehci_device_isoc_start: request\n");
   4445       1.139  jmcneill 
   4446  1.234.2.20     skrll 	if (!exfer->ex_isdone) {
   4447       1.229     skrll 		USBHIST_LOG(ehcidebug, "marked not done, ex = %p", exfer,
   4448       1.229     skrll 			0, 0, 0);
   4449       1.139  jmcneill 		printf("ehci_device_isoc_start: not done, ex = %p\n", exfer);
   4450       1.229     skrll 	}
   4451  1.234.2.20     skrll 	exfer->ex_isdone = 0;
   4452       1.139  jmcneill #endif
   4453       1.139  jmcneill 
   4454       1.139  jmcneill 	/*
   4455       1.139  jmcneill 	 * Step 1: Allocate and initialize itds, how many do we need?
   4456       1.139  jmcneill 	 * One per transfer if interval >= 8 microframes, fewer if we use
   4457       1.139  jmcneill 	 * multiple microframes per frame.
   4458       1.139  jmcneill 	 */
   4459       1.139  jmcneill 
   4460   1.234.2.8     skrll 	i = epipe->pipe.up_endpoint->ue_edesc->bInterval;
   4461       1.139  jmcneill 	if (i > 16 || i == 0) {
   4462       1.139  jmcneill 		/* Spec page 271 says intervals > 16 are invalid */
   4463  1.234.2.21     skrll 		USBHIST_LOG(ehcidebug, "bInterval %d invalid", i, 0, 0, 0);
   4464       1.139  jmcneill 		return USBD_INVAL;
   4465       1.139  jmcneill 	}
   4466       1.139  jmcneill 
   4467       1.168  jakllsch 	ufrperframe = max(1, USB_UFRAMES_PER_FRAME / (1 << (i - 1)));
   4468   1.234.2.8     skrll 	frames = (xfer->ux_nframes + (ufrperframe - 1)) / ufrperframe;
   4469       1.168  jakllsch 	uframes = USB_UFRAMES_PER_FRAME / ufrperframe;
   4470       1.142  drochner 
   4471       1.139  jmcneill 	if (frames == 0) {
   4472       1.229     skrll 		USBHIST_LOG(ehcidebug, "frames == 0", 0, 0, 0, 0);
   4473       1.139  jmcneill 		return USBD_INVAL;
   4474       1.139  jmcneill 	}
   4475       1.139  jmcneill 
   4476   1.234.2.8     skrll 	dma_buf = &xfer->ux_dmabuf;
   4477       1.139  jmcneill 	offs = 0;
   4478       1.139  jmcneill 
   4479       1.139  jmcneill 	for (i = 0; i < frames; i++) {
   4480       1.139  jmcneill 		int froffs = offs;
   4481       1.139  jmcneill 		itd = ehci_alloc_itd(sc);
   4482       1.139  jmcneill 
   4483       1.139  jmcneill 		if (prev != NULL) {
   4484       1.139  jmcneill 			prev->itd.itd_next =
   4485       1.139  jmcneill 			    htole32(itd->physaddr | EHCI_LINK_ITD);
   4486       1.139  jmcneill 			usb_syncmem(&itd->dma,
   4487       1.139  jmcneill 			    itd->offs + offsetof(ehci_itd_t, itd_next),
   4488   1.234.2.2     skrll 			    sizeof(itd->itd.itd_next), BUS_DMASYNC_POSTWRITE);
   4489       1.139  jmcneill 
   4490       1.139  jmcneill 			prev->xfer_next = itd;
   4491       1.183  jakllsch 	    	} else {
   4492       1.139  jmcneill 			start = itd;
   4493       1.139  jmcneill 		}
   4494       1.139  jmcneill 
   4495       1.139  jmcneill 		/*
   4496       1.139  jmcneill 		 * Step 1.5, initialize uframes
   4497       1.139  jmcneill 		 */
   4498       1.168  jakllsch 		for (j = 0; j < EHCI_ITD_NUFRAMES; j += uframes) {
   4499       1.139  jmcneill 			/* Calculate which page in the list this starts in */
   4500       1.139  jmcneill 			int addr = DMAADDR(dma_buf, froffs);
   4501       1.139  jmcneill 			addr = EHCI_PAGE_OFFSET(addr);
   4502       1.139  jmcneill 			addr += (offs - froffs);
   4503       1.139  jmcneill 			addr = EHCI_PAGE(addr);
   4504       1.139  jmcneill 			addr /= EHCI_PAGE_SIZE;
   4505       1.139  jmcneill 
   4506       1.139  jmcneill 			/* This gets the initial offset into the first page,
   4507       1.139  jmcneill 			 * looks how far further along the current uframe
   4508       1.139  jmcneill 			 * offset is. Works out how many pages that is.
   4509       1.139  jmcneill 			 */
   4510       1.139  jmcneill 
   4511       1.139  jmcneill 			itd->itd.itd_ctl[j] = htole32 ( EHCI_ITD_ACTIVE |
   4512   1.234.2.8     skrll 			    EHCI_ITD_SET_LEN(xfer->ux_frlengths[trans_count]) |
   4513       1.139  jmcneill 			    EHCI_ITD_SET_PG(addr) |
   4514       1.139  jmcneill 			    EHCI_ITD_SET_OFFS(EHCI_PAGE_OFFSET(DMAADDR(dma_buf,offs))));
   4515       1.139  jmcneill 
   4516   1.234.2.8     skrll 			total_length += xfer->ux_frlengths[trans_count];
   4517   1.234.2.8     skrll 			offs += xfer->ux_frlengths[trans_count];
   4518       1.139  jmcneill 			trans_count++;
   4519       1.139  jmcneill 
   4520   1.234.2.8     skrll 			if (trans_count >= xfer->ux_nframes) { /*Set IOC*/
   4521       1.139  jmcneill 				itd->itd.itd_ctl[j] |= htole32(EHCI_ITD_IOC);
   4522       1.145  drochner 				break;
   4523       1.139  jmcneill 			}
   4524       1.195  christos 		}
   4525       1.139  jmcneill 
   4526       1.139  jmcneill 		/* Step 1.75, set buffer pointers. To simplify matters, all
   4527       1.139  jmcneill 		 * pointers are filled out for the next 7 hardware pages in
   4528       1.139  jmcneill 		 * the dma block, so no need to worry what pages to cover
   4529       1.139  jmcneill 		 * and what to not.
   4530       1.139  jmcneill 		 */
   4531       1.139  jmcneill 
   4532       1.168  jakllsch 		for (j = 0; j < EHCI_ITD_NBUFFERS; j++) {
   4533       1.139  jmcneill 			/*
   4534       1.139  jmcneill 			 * Don't try to lookup a page that's past the end
   4535       1.139  jmcneill 			 * of buffer
   4536       1.139  jmcneill 			 */
   4537       1.139  jmcneill 			int page_offs = EHCI_PAGE(froffs + (EHCI_PAGE_SIZE * j));
   4538   1.234.2.8     skrll 			if (page_offs >= dma_buf->udma_block->size)
   4539       1.139  jmcneill 				break;
   4540       1.139  jmcneill 
   4541       1.181       mrg 			unsigned long long page = DMAADDR(dma_buf, page_offs);
   4542       1.139  jmcneill 			page = EHCI_PAGE(page);
   4543       1.139  jmcneill 			itd->itd.itd_bufr[j] =
   4544       1.155    jmorse 			    htole32(EHCI_ITD_SET_BPTR(page));
   4545       1.155    jmorse 			itd->itd.itd_bufr_hi[j] =
   4546       1.155    jmorse 			    htole32(page >> 32);
   4547       1.139  jmcneill 		}
   4548       1.139  jmcneill 
   4549       1.139  jmcneill 		/*
   4550       1.139  jmcneill 		 * Other special values
   4551       1.139  jmcneill 		 */
   4552       1.139  jmcneill 
   4553   1.234.2.8     skrll 		k = epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress;
   4554       1.139  jmcneill 		itd->itd.itd_bufr[0] |= htole32(EHCI_ITD_SET_EP(UE_GET_ADDR(k)) |
   4555   1.234.2.8     skrll 		    EHCI_ITD_SET_DADDR(epipe->pipe.up_dev->ud_addr));
   4556       1.139  jmcneill 
   4557   1.234.2.8     skrll 		k = (UE_GET_DIR(epipe->pipe.up_endpoint->ue_edesc->bEndpointAddress))
   4558       1.139  jmcneill 		    ? 1 : 0;
   4559   1.234.2.8     skrll 		j = UGETW(epipe->pipe.up_endpoint->ue_edesc->wMaxPacketSize);
   4560       1.139  jmcneill 		itd->itd.itd_bufr[1] |= htole32(EHCI_ITD_SET_DIR(k) |
   4561       1.139  jmcneill 		    EHCI_ITD_SET_MAXPKT(UE_GET_SIZE(j)));
   4562       1.139  jmcneill 
   4563       1.139  jmcneill 		/* FIXME: handle invalid trans */
   4564       1.195  christos 		itd->itd.itd_bufr[2] |=
   4565       1.139  jmcneill 		    htole32(EHCI_ITD_SET_MULTI(UE_GET_TRANS(j)+1));
   4566       1.139  jmcneill 
   4567       1.139  jmcneill 		usb_syncmem(&itd->dma,
   4568       1.139  jmcneill 		    itd->offs + offsetof(ehci_itd_t, itd_next),
   4569   1.234.2.2     skrll 		    sizeof(ehci_itd_t),
   4570       1.139  jmcneill 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   4571       1.139  jmcneill 
   4572       1.139  jmcneill 		prev = itd;
   4573       1.139  jmcneill 	} /* End of frame */
   4574       1.139  jmcneill 
   4575       1.139  jmcneill 	stop = itd;
   4576       1.139  jmcneill 	stop->xfer_next = NULL;
   4577       1.139  jmcneill 
   4578  1.234.2.20     skrll 	usb_syncmem(&exfer->ex_xfer.ux_dmabuf, 0, total_length,
   4579       1.155    jmorse 		BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   4580       1.155    jmorse 
   4581       1.139  jmcneill 	/*
   4582       1.139  jmcneill 	 * Part 2: Transfer descriptors have now been set up, now they must
   4583       1.139  jmcneill 	 * be scheduled into the period frame list. Erk. Not wanting to
   4584       1.139  jmcneill 	 * complicate matters, transfer is denied if the transfer spans
   4585       1.139  jmcneill 	 * more than the period frame list.
   4586       1.139  jmcneill 	 */
   4587       1.139  jmcneill 
   4588       1.190       mrg 	mutex_enter(&sc->sc_lock);
   4589       1.139  jmcneill 
   4590       1.139  jmcneill 	/* Start inserting frames */
   4591       1.139  jmcneill 	if (epipe->u.isoc.cur_xfers > 0) {
   4592       1.139  jmcneill 		frindex = epipe->u.isoc.next_frame;
   4593       1.139  jmcneill 	} else {
   4594       1.139  jmcneill 		frindex = EOREAD4(sc, EHCI_FRINDEX);
   4595       1.139  jmcneill 		frindex = frindex >> 3; /* Erase microframe index */
   4596       1.139  jmcneill 		frindex += 2;
   4597       1.139  jmcneill 	}
   4598       1.139  jmcneill 
   4599       1.139  jmcneill 	if (frindex >= sc->sc_flsize)
   4600       1.139  jmcneill 		frindex &= (sc->sc_flsize - 1);
   4601       1.139  jmcneill 
   4602       1.168  jakllsch 	/* What's the frame interval? */
   4603   1.234.2.8     skrll 	i = (1 << (epipe->pipe.up_endpoint->ue_edesc->bInterval - 1));
   4604       1.168  jakllsch 	if (i / USB_UFRAMES_PER_FRAME == 0)
   4605       1.139  jmcneill 		i = 1;
   4606       1.139  jmcneill 	else
   4607       1.168  jakllsch 		i /= USB_UFRAMES_PER_FRAME;
   4608       1.139  jmcneill 
   4609       1.139  jmcneill 	itd = start;
   4610       1.139  jmcneill 	for (j = 0; j < frames; j++) {
   4611       1.139  jmcneill 		if (itd == NULL)
   4612       1.139  jmcneill 			panic("ehci: unexpectedly ran out of isoc itds, isoc_start\n");
   4613       1.139  jmcneill 
   4614       1.139  jmcneill 		itd->itd.itd_next = sc->sc_flist[frindex];
   4615       1.139  jmcneill 		if (itd->itd.itd_next == 0)
   4616       1.139  jmcneill 			/* FIXME: frindex table gets initialized to NULL
   4617       1.139  jmcneill 			 * or EHCI_NULL? */
   4618       1.162  uebayasi 			itd->itd.itd_next = EHCI_NULL;
   4619       1.139  jmcneill 
   4620       1.139  jmcneill 		usb_syncmem(&itd->dma,
   4621       1.139  jmcneill 		    itd->offs + offsetof(ehci_itd_t, itd_next),
   4622   1.234.2.2     skrll 		    sizeof(itd->itd.itd_next),
   4623       1.139  jmcneill 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   4624       1.139  jmcneill 
   4625       1.139  jmcneill 		sc->sc_flist[frindex] = htole32(EHCI_LINK_ITD | itd->physaddr);
   4626       1.139  jmcneill 
   4627       1.139  jmcneill 		usb_syncmem(&sc->sc_fldma,
   4628       1.139  jmcneill 		    sizeof(ehci_link_t) * frindex,
   4629   1.234.2.2     skrll 		    sizeof(ehci_link_t),
   4630       1.139  jmcneill 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   4631       1.139  jmcneill 
   4632       1.139  jmcneill 		itd->u.frame_list.next = sc->sc_softitds[frindex];
   4633       1.139  jmcneill 		sc->sc_softitds[frindex] = itd;
   4634       1.139  jmcneill 		if (itd->u.frame_list.next != NULL)
   4635       1.139  jmcneill 			itd->u.frame_list.next->u.frame_list.prev = itd;
   4636       1.139  jmcneill 		itd->slot = frindex;
   4637       1.139  jmcneill 		itd->u.frame_list.prev = NULL;
   4638       1.139  jmcneill 
   4639       1.139  jmcneill 		frindex += i;
   4640       1.139  jmcneill 		if (frindex >= sc->sc_flsize)
   4641       1.139  jmcneill 			frindex -= sc->sc_flsize;
   4642       1.139  jmcneill 
   4643       1.139  jmcneill 		itd = itd->xfer_next;
   4644       1.139  jmcneill 	}
   4645       1.139  jmcneill 
   4646       1.139  jmcneill 	epipe->u.isoc.cur_xfers++;
   4647       1.139  jmcneill 	epipe->u.isoc.next_frame = frindex;
   4648       1.139  jmcneill 
   4649  1.234.2.20     skrll 	exfer->ex_itdstart = start;
   4650  1.234.2.20     skrll 	exfer->ex_itdend = stop;
   4651       1.139  jmcneill 
   4652       1.139  jmcneill 	ehci_add_intr_list(sc, exfer);
   4653   1.234.2.8     skrll 	xfer->ux_status = USBD_IN_PROGRESS;
   4654   1.234.2.8     skrll 	xfer->ux_done = 0;
   4655       1.190       mrg 	mutex_exit(&sc->sc_lock);
   4656       1.139  jmcneill 
   4657   1.234.2.8     skrll 	if (sc->sc_bus.ub_usepolling) {
   4658       1.139  jmcneill 		printf("Starting ehci isoc xfer with polling. Bad idea?\n");
   4659       1.139  jmcneill 		ehci_waitintr(sc, xfer);
   4660       1.139  jmcneill 	}
   4661       1.139  jmcneill 
   4662       1.139  jmcneill 	return USBD_IN_PROGRESS;
   4663       1.113  christos }
   4664       1.139  jmcneill 
   4665       1.113  christos Static void
   4666       1.115  christos ehci_device_isoc_abort(usbd_xfer_handle xfer)
   4667       1.113  christos {
   4668       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   4669       1.229     skrll 
   4670       1.229     skrll 	USBHIST_LOG(ehcidebug, "xfer = %p", xfer, 0, 0, 0);
   4671       1.139  jmcneill 	ehci_abort_isoc_xfer(xfer, USBD_CANCELLED);
   4672       1.113  christos }
   4673       1.139  jmcneill 
   4674       1.113  christos Static void
   4675       1.115  christos ehci_device_isoc_close(usbd_pipe_handle pipe)
   4676       1.113  christos {
   4677       1.229     skrll 	USBHIST_FUNC(); USBHIST_CALLED(ehcidebug);
   4678       1.229     skrll 
   4679       1.229     skrll 	USBHIST_LOG(ehcidebug, "nothing in the pipe to free?", 0, 0, 0, 0);
   4680       1.113  christos }
   4681       1.139  jmcneill 
   4682       1.113  christos Static void
   4683       1.115  christos ehci_device_isoc_done(usbd_xfer_handle xfer)
   4684       1.113  christos {
   4685       1.139  jmcneill 	struct ehci_xfer *exfer;
   4686       1.139  jmcneill 	ehci_softc_t *sc;
   4687       1.139  jmcneill 	struct ehci_pipe *epipe;
   4688       1.139  jmcneill 
   4689       1.139  jmcneill 	exfer = EXFER(xfer);
   4690   1.234.2.8     skrll 	sc = xfer->ux_pipe->up_dev->ud_bus->ub_hcpriv;
   4691   1.234.2.8     skrll 	epipe = (struct ehci_pipe *) xfer->ux_pipe;
   4692       1.139  jmcneill 
   4693       1.190       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   4694       1.190       mrg 
   4695       1.139  jmcneill 	epipe->u.isoc.cur_xfers--;
   4696   1.234.2.8     skrll 	if (xfer->ux_status != USBD_NOMEM && ehci_active_intr_list(exfer)) {
   4697       1.153  jmcneill 		ehci_del_intr_list(sc, exfer);
   4698       1.139  jmcneill 		ehci_rem_free_itd_chain(sc, exfer);
   4699       1.139  jmcneill 	}
   4700       1.139  jmcneill 
   4701   1.234.2.8     skrll 	usb_syncmem(&xfer->ux_dmabuf, 0, xfer->ux_length, BUS_DMASYNC_POSTWRITE |
   4702   1.234.2.2     skrll 	    BUS_DMASYNC_POSTREAD);
   4703       1.139  jmcneill 
   4704       1.113  christos }
   4705