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