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