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