Home | History | Annotate | Line # | Download | only in usb
usbdi.c revision 1.182.4.3
      1  1.182.4.3    martin /*	$NetBSD: usbdi.c,v 1.182.4.3 2020/03/01 12:35:16 martin Exp $	*/
      2        1.1  augustss 
      3        1.1  augustss /*
      4      1.169     skrll  * Copyright (c) 1998, 2012, 2015 The NetBSD Foundation, Inc.
      5        1.1  augustss  * All rights reserved.
      6        1.1  augustss  *
      7       1.11  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8       1.72  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9      1.169     skrll  * Carlstedt Research & Technology, Matthew R. Green (mrg (at) eterna.com.au),
     10      1.169     skrll  * and Nick Hudson.
     11        1.1  augustss  *
     12        1.1  augustss  * Redistribution and use in source and binary forms, with or without
     13        1.1  augustss  * modification, are permitted provided that the following conditions
     14        1.1  augustss  * are met:
     15        1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     16        1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     17        1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     18        1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     19        1.1  augustss  *    documentation and/or other materials provided with the distribution.
     20        1.1  augustss  *
     21        1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22        1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23        1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24        1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25        1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26        1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27        1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28        1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29        1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30        1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31        1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     32        1.1  augustss  */
     33       1.84     lukem 
     34       1.84     lukem #include <sys/cdefs.h>
     35  1.182.4.3    martin __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.182.4.3 2020/03/01 12:35:16 martin Exp $");
     36      1.115     pavel 
     37      1.141  christos #ifdef _KERNEL_OPT
     38      1.162     skrll #include "opt_usb.h"
     39      1.115     pavel #include "opt_compat_netbsd.h"
     40      1.169     skrll #include "usb_dma.h"
     41      1.141  christos #endif
     42        1.1  augustss 
     43        1.1  augustss #include <sys/param.h>
     44        1.1  augustss #include <sys/systm.h>
     45        1.1  augustss #include <sys/kernel.h>
     46       1.15  augustss #include <sys/device.h>
     47      1.169     skrll #include <sys/kmem.h>
     48        1.1  augustss #include <sys/proc.h>
     49      1.121        ad #include <sys/bus.h>
     50      1.136       mrg #include <sys/cpu.h>
     51       1.36  augustss 
     52        1.1  augustss #include <dev/usb/usb.h>
     53        1.1  augustss #include <dev/usb/usbdi.h>
     54        1.1  augustss #include <dev/usb/usbdi_util.h>
     55        1.1  augustss #include <dev/usb/usbdivar.h>
     56       1.36  augustss #include <dev/usb/usb_mem.h>
     57      1.105  augustss #include <dev/usb/usb_quirks.h>
     58      1.162     skrll #include <dev/usb/usbhist.h>
     59        1.1  augustss 
     60      1.119  drochner /* UTF-8 encoding stuff */
     61      1.119  drochner #include <fs/unicode.h>
     62      1.119  drochner 
     63        1.1  augustss extern int usbdebug;
     64        1.1  augustss 
     65      1.169     skrll Static usbd_status usbd_ar_pipe(struct usbd_pipe *);
     66      1.169     skrll Static void usbd_start_next(struct usbd_pipe *);
     67       1.69  augustss Static usbd_status usbd_open_pipe_ival
     68      1.169     skrll 	(struct usbd_interface *, uint8_t, uint8_t, struct usbd_pipe **, int);
     69      1.169     skrll static void *usbd_alloc_buffer(struct usbd_xfer *, uint32_t);
     70      1.169     skrll static void usbd_free_buffer(struct usbd_xfer *);
     71      1.169     skrll static struct usbd_xfer *usbd_alloc_xfer(struct usbd_device *, unsigned int);
     72      1.169     skrll static usbd_status usbd_free_xfer(struct usbd_xfer *);
     73      1.176   khorben static void usbd_request_async_cb(struct usbd_xfer *, void *, usbd_status);
     74  1.182.4.3    martin static void usbd_xfer_timeout(void *);
     75  1.182.4.3    martin static void usbd_xfer_timeout_task(void *);
     76  1.182.4.3    martin static bool usbd_xfer_probe_timeout(struct usbd_xfer *);
     77  1.182.4.3    martin static void usbd_xfer_cancel_timeout_async(struct usbd_xfer *);
     78        1.1  augustss 
     79      1.162     skrll #if defined(USB_DEBUG)
     80       1.82  augustss void
     81       1.82  augustss usbd_dump_iface(struct usbd_interface *iface)
     82       1.82  augustss {
     83  1.182.4.1    martin 	USBHIST_FUNC();
     84  1.182.4.1    martin 	USBHIST_CALLARGS(usbdebug, "iface %#jx", (uintptr_t)iface, 0, 0, 0);
     85      1.162     skrll 
     86       1.82  augustss 	if (iface == NULL)
     87       1.82  augustss 		return;
     88      1.175  pgoyette 	USBHIST_LOG(usbdebug, "     device = %#jx idesc = %#jx index = %d",
     89      1.175  pgoyette 	    (uintptr_t)iface->ui_dev, (uintptr_t)iface->ui_idesc,
     90      1.175  pgoyette 	    iface->ui_index, 0);
     91      1.175  pgoyette 	USBHIST_LOG(usbdebug, "     altindex=%d priv=%#jx",
     92      1.175  pgoyette 	    iface->ui_altindex, (uintptr_t)iface->ui_priv, 0, 0);
     93       1.82  augustss }
     94       1.82  augustss 
     95       1.82  augustss void
     96       1.82  augustss usbd_dump_device(struct usbd_device *dev)
     97       1.82  augustss {
     98  1.182.4.1    martin 	USBHIST_FUNC();
     99  1.182.4.1    martin 	USBHIST_CALLARGS(usbdebug, "dev = %#jx", (uintptr_t)dev, 0, 0, 0);
    100      1.162     skrll 
    101       1.82  augustss 	if (dev == NULL)
    102       1.82  augustss 		return;
    103      1.175  pgoyette 	USBHIST_LOG(usbdebug, "     bus = %#jx default_pipe = %#jx",
    104      1.175  pgoyette 	    (uintptr_t)dev->ud_bus, (uintptr_t)dev->ud_pipe0, 0, 0);
    105      1.175  pgoyette 	USBHIST_LOG(usbdebug, "     address = %jd config = %jd depth = %jd ",
    106      1.169     skrll 	    dev->ud_addr, dev->ud_config, dev->ud_depth, 0);
    107      1.175  pgoyette 	USBHIST_LOG(usbdebug, "     speed = %jd self_powered = %jd "
    108      1.175  pgoyette 	    "power = %jd langid = %jd",
    109      1.169     skrll 	    dev->ud_speed, dev->ud_selfpowered, dev->ud_power, dev->ud_langid);
    110       1.82  augustss }
    111       1.82  augustss 
    112       1.82  augustss void
    113       1.82  augustss usbd_dump_endpoint(struct usbd_endpoint *endp)
    114       1.82  augustss {
    115  1.182.4.1    martin 	USBHIST_FUNC();
    116  1.182.4.1    martin 	USBHIST_CALLARGS(usbdebug, "endp = %#jx", (uintptr_t)endp, 0, 0, 0);
    117      1.162     skrll 
    118       1.82  augustss 	if (endp == NULL)
    119       1.82  augustss 		return;
    120      1.175  pgoyette 	USBHIST_LOG(usbdebug, "    edesc = %#jx refcnt = %jd",
    121      1.175  pgoyette 	    (uintptr_t)endp->ue_edesc, endp->ue_refcnt, 0, 0);
    122      1.169     skrll 	if (endp->ue_edesc)
    123      1.162     skrll 		USBHIST_LOG(usbdebug, "     bEndpointAddress=0x%02x",
    124      1.169     skrll 		    endp->ue_edesc->bEndpointAddress, 0, 0, 0);
    125       1.82  augustss }
    126       1.82  augustss 
    127       1.28  augustss void
    128      1.169     skrll usbd_dump_queue(struct usbd_pipe *pipe)
    129       1.28  augustss {
    130      1.169     skrll 	struct usbd_xfer *xfer;
    131       1.28  augustss 
    132  1.182.4.1    martin 	USBHIST_FUNC();
    133  1.182.4.1    martin 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
    134      1.162     skrll 
    135      1.169     skrll 	SIMPLEQ_FOREACH(xfer, &pipe->up_queue, ux_next) {
    136      1.175  pgoyette 		USBHIST_LOG(usbdebug, "     xfer = %#jx", (uintptr_t)xfer,
    137      1.175  pgoyette 		    0, 0, 0);
    138       1.28  augustss 	}
    139       1.82  augustss }
    140       1.82  augustss 
    141       1.82  augustss void
    142      1.169     skrll usbd_dump_pipe(struct usbd_pipe *pipe)
    143       1.82  augustss {
    144  1.182.4.1    martin 	USBHIST_FUNC();
    145  1.182.4.1    martin 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
    146      1.162     skrll 
    147       1.82  augustss 	if (pipe == NULL)
    148       1.82  augustss 		return;
    149      1.169     skrll 	usbd_dump_iface(pipe->up_iface);
    150      1.169     skrll 	usbd_dump_device(pipe->up_dev);
    151      1.169     skrll 	usbd_dump_endpoint(pipe->up_endpoint);
    152      1.162     skrll 	USBHIST_LOG(usbdebug, "(usbd_dump_pipe)", 0, 0, 0, 0);
    153      1.175  pgoyette 	USBHIST_LOG(usbdebug, "     running = %jd aborting = %jd",
    154      1.169     skrll 	    pipe->up_running, pipe->up_aborting, 0, 0);
    155      1.175  pgoyette 	USBHIST_LOG(usbdebug, "     intrxfer = %#jx, repeat = %jd, "
    156      1.175  pgoyette 	    "interval = %jd", (uintptr_t)pipe->up_intrxfer, pipe->up_repeat,
    157      1.175  pgoyette 	    pipe->up_interval, 0);
    158       1.28  augustss }
    159       1.28  augustss #endif
    160       1.28  augustss 
    161      1.102  augustss usbd_status
    162      1.169     skrll usbd_open_pipe(struct usbd_interface *iface, uint8_t address,
    163      1.169     skrll 	       uint8_t flags, struct usbd_pipe **pipe)
    164      1.102  augustss {
    165      1.102  augustss 	return (usbd_open_pipe_ival(iface, address, flags, pipe,
    166       1.60  augustss 				    USBD_DEFAULT_INTERVAL));
    167       1.60  augustss }
    168       1.60  augustss 
    169      1.102  augustss usbd_status
    170      1.169     skrll usbd_open_pipe_ival(struct usbd_interface *iface, uint8_t address,
    171      1.169     skrll 		    uint8_t flags, struct usbd_pipe **pipe, int ival)
    172      1.102  augustss {
    173      1.169     skrll 	struct usbd_pipe *p;
    174        1.1  augustss 	struct usbd_endpoint *ep;
    175       1.48  augustss 	usbd_status err;
    176       1.12  augustss 	int i;
    177        1.1  augustss 
    178  1.182.4.1    martin 	USBHIST_FUNC();
    179  1.182.4.1    martin 	USBHIST_CALLARGS(usbdebug, "iface = %#jx address = 0x%jx flags = 0x%jx",
    180      1.175  pgoyette 	    (uintptr_t)iface, address, flags, 0);
    181       1.53  augustss 
    182      1.169     skrll 	for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
    183      1.169     skrll 		ep = &iface->ui_endpoints[i];
    184      1.169     skrll 		if (ep->ue_edesc == NULL)
    185      1.169     skrll 			return USBD_IOERROR;
    186      1.169     skrll 		if (ep->ue_edesc->bEndpointAddress == address)
    187        1.1  augustss 			goto found;
    188        1.1  augustss 	}
    189      1.169     skrll 	return USBD_BAD_ADDRESS;
    190        1.1  augustss  found:
    191      1.169     skrll 	if ((flags & USBD_EXCLUSIVE_USE) && ep->ue_refcnt != 0)
    192      1.169     skrll 		return USBD_IN_USE;
    193      1.169     skrll 	err = usbd_setup_pipe_flags(iface->ui_dev, iface, ep, ival, &p, flags);
    194       1.48  augustss 	if (err)
    195      1.169     skrll 		return err;
    196      1.169     skrll 	LIST_INSERT_HEAD(&iface->ui_pipes, p, up_next);
    197        1.1  augustss 	*pipe = p;
    198      1.169     skrll 	return USBD_NORMAL_COMPLETION;
    199        1.1  augustss }
    200        1.1  augustss 
    201      1.102  augustss usbd_status
    202      1.169     skrll usbd_open_pipe_intr(struct usbd_interface *iface, uint8_t address,
    203      1.169     skrll 		    uint8_t flags, struct usbd_pipe **pipe,
    204      1.169     skrll 		    void *priv, void *buffer, uint32_t len,
    205       1.74  augustss 		    usbd_callback cb, int ival)
    206        1.1  augustss {
    207       1.48  augustss 	usbd_status err;
    208      1.169     skrll 	struct usbd_xfer *xfer;
    209      1.169     skrll 	struct usbd_pipe *ipipe;
    210        1.1  augustss 
    211  1.182.4.1    martin 	USBHIST_FUNC();
    212  1.182.4.1    martin 	USBHIST_CALLARGS(usbdebug, "address = 0x%jx flags = 0x%jx len = %jd",
    213      1.162     skrll 	    address, flags, len, 0);
    214       1.53  augustss 
    215      1.150  jmcneill 	err = usbd_open_pipe_ival(iface, address,
    216      1.150  jmcneill 				  USBD_EXCLUSIVE_USE | (flags & USBD_MPSAFE),
    217       1.60  augustss 				  &ipipe, ival);
    218       1.48  augustss 	if (err)
    219      1.169     skrll 		return err;
    220      1.169     skrll 	err = usbd_create_xfer(ipipe, len, flags, 0, &xfer);
    221      1.169     skrll 	if (err)
    222        1.1  augustss 		goto bad1;
    223      1.169     skrll 
    224      1.169     skrll 	usbd_setup_xfer(xfer, priv, buffer, len, flags, USBD_NO_TIMEOUT, cb);
    225      1.169     skrll 	ipipe->up_intrxfer = xfer;
    226      1.169     skrll 	ipipe->up_repeat = 1;
    227       1.48  augustss 	err = usbd_transfer(xfer);
    228        1.1  augustss 	*pipe = ipipe;
    229       1.48  augustss 	if (err != USBD_IN_PROGRESS)
    230      1.169     skrll 		goto bad3;
    231      1.169     skrll 	return USBD_NORMAL_COMPLETION;
    232      1.169     skrll 
    233      1.169     skrll  bad3:
    234      1.169     skrll 	ipipe->up_intrxfer = NULL;
    235      1.169     skrll 	ipipe->up_repeat = 0;
    236        1.1  augustss 
    237      1.169     skrll 	usbd_destroy_xfer(xfer);
    238       1.36  augustss  bad1:
    239        1.1  augustss 	usbd_close_pipe(ipipe);
    240      1.169     skrll 	return err;
    241        1.9  augustss }
    242        1.9  augustss 
    243        1.1  augustss usbd_status
    244      1.169     skrll usbd_close_pipe(struct usbd_pipe *pipe)
    245        1.1  augustss {
    246      1.162     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    247      1.162     skrll 
    248      1.169     skrll 	KASSERT(pipe != NULL);
    249       1.26  augustss 
    250      1.138       mrg 	usbd_lock_pipe(pipe);
    251      1.169     skrll 
    252      1.169     skrll 	if (!SIMPLEQ_EMPTY(&pipe->up_queue)) {
    253      1.169     skrll 		printf("WARNING: pipe closed with active xfers on addr %d\n",
    254      1.169     skrll 		    pipe->up_dev->ud_addr);
    255      1.169     skrll 		usbd_ar_pipe(pipe);
    256      1.138       mrg 	}
    257      1.169     skrll 
    258      1.169     skrll 	KASSERT(SIMPLEQ_EMPTY(&pipe->up_queue));
    259      1.169     skrll 
    260      1.169     skrll 	LIST_REMOVE(pipe, up_next);
    261      1.169     skrll 	pipe->up_endpoint->ue_refcnt--;
    262      1.169     skrll 
    263      1.181     skrll 	pipe->up_methods->upm_close(pipe);
    264      1.181     skrll 
    265      1.169     skrll 	if (pipe->up_intrxfer != NULL) {
    266      1.169     skrll 	    	usbd_unlock_pipe(pipe);
    267      1.169     skrll 		usbd_destroy_xfer(pipe->up_intrxfer);
    268      1.169     skrll 		usbd_lock_pipe(pipe);
    269      1.138       mrg 	}
    270      1.169     skrll 
    271      1.138       mrg 	usbd_unlock_pipe(pipe);
    272      1.169     skrll 	kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
    273      1.169     skrll 
    274      1.169     skrll 	return USBD_NORMAL_COMPLETION;
    275        1.1  augustss }
    276        1.1  augustss 
    277       1.31  augustss usbd_status
    278      1.169     skrll usbd_transfer(struct usbd_xfer *xfer)
    279        1.1  augustss {
    280      1.169     skrll 	struct usbd_pipe *pipe = xfer->ux_pipe;
    281       1.48  augustss 	usbd_status err;
    282      1.120  kiyohara 	unsigned int size, flags;
    283       1.16  augustss 
    284  1.182.4.1    martin 	USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
    285      1.175  pgoyette 	    "xfer = %#jx, flags = %#jx, pipe = %#jx, running = %jd",
    286      1.175  pgoyette 	    (uintptr_t)xfer, xfer->ux_flags, (uintptr_t)pipe, pipe->up_running);
    287      1.177       mrg 	KASSERT(xfer->ux_status == USBD_NOT_STARTED);
    288      1.134  jmcneill 
    289       1.28  augustss #ifdef USB_DEBUG
    290       1.28  augustss 	if (usbdebug > 5)
    291       1.28  augustss 		usbd_dump_queue(pipe);
    292       1.28  augustss #endif
    293      1.169     skrll 	xfer->ux_done = 0;
    294       1.31  augustss 
    295      1.169     skrll 	if (pipe->up_aborting) {
    296      1.175  pgoyette 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, aborting",
    297      1.175  pgoyette 		    (uintptr_t)xfer, 0, 0, 0);
    298      1.169     skrll 		return USBD_CANCELLED;
    299      1.162     skrll 	}
    300       1.71  augustss 
    301      1.169     skrll 	KASSERT(xfer->ux_length == 0 || xfer->ux_buf != NULL);
    302      1.169     skrll 
    303      1.169     skrll 	size = xfer->ux_length;
    304      1.169     skrll 	flags = xfer->ux_flags;
    305       1.37  augustss 
    306      1.169     skrll 	if (size != 0) {
    307      1.169     skrll 		/*
    308      1.169     skrll 		 * Use the xfer buffer if none specified in transfer setup.
    309      1.169     skrll 		 * isoc transfers always use the xfer buffer, i.e.
    310      1.169     skrll 		 * ux_buffer is always NULL for isoc.
    311      1.169     skrll 		 */
    312      1.169     skrll 		if (xfer->ux_buffer == NULL) {
    313      1.169     skrll 			xfer->ux_buffer = xfer->ux_buf;
    314      1.169     skrll 		}
    315      1.169     skrll 
    316      1.169     skrll 		/*
    317      1.169     skrll 		 * If not using the xfer buffer copy data to the
    318      1.169     skrll 		 * xfer buffer for OUT transfers of >0 length
    319      1.169     skrll 		 */
    320      1.169     skrll 		if (xfer->ux_buffer != xfer->ux_buf) {
    321      1.169     skrll 			KASSERT(xfer->ux_buf);
    322      1.169     skrll 			if (!usbd_xfer_isread(xfer)) {
    323      1.169     skrll 				memcpy(xfer->ux_buf, xfer->ux_buffer, size);
    324      1.169     skrll 			}
    325      1.162     skrll 		}
    326       1.38  augustss 	}
    327       1.37  augustss 
    328      1.120  kiyohara 	/* xfer is not valid after the transfer method unless synchronous */
    329      1.169     skrll 	err = pipe->up_methods->upm_transfer(xfer);
    330       1.37  augustss 
    331       1.51  augustss 	if (err != USBD_IN_PROGRESS && err) {
    332      1.168     skrll 		/*
    333      1.168     skrll 		 * The transfer made it onto the pipe queue, but didn't get
    334      1.168     skrll 		 * accepted by the HCD for some reason.  It needs removing
    335      1.168     skrll 		 * from the pipe queue.
    336      1.168     skrll 		 */
    337  1.182.4.1    martin 		USBHIST_LOG(usbdebug, "xfer failed: %s, reinserting",
    338  1.182.4.1    martin 		    err, 0, 0, 0);
    339      1.168     skrll 		usbd_lock_pipe(pipe);
    340      1.169     skrll 		SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
    341      1.169     skrll 		if (pipe->up_serialise)
    342      1.169     skrll 			usbd_start_next(pipe);
    343      1.168     skrll 		usbd_unlock_pipe(pipe);
    344       1.37  augustss 	}
    345       1.37  augustss 
    346      1.162     skrll 	if (!(flags & USBD_SYNCHRONOUS)) {
    347      1.175  pgoyette 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, not sync (err %jd)",
    348      1.175  pgoyette 		    (uintptr_t)xfer, err, 0, 0);
    349      1.169     skrll 		return err;
    350      1.162     skrll 	}
    351       1.31  augustss 
    352      1.162     skrll 	if (err != USBD_IN_PROGRESS) {
    353  1.182.4.1    martin 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, sync (err %jd)",
    354  1.182.4.1    martin 		    (uintptr_t)xfer, err, 0, 0);
    355      1.169     skrll 		return err;
    356      1.162     skrll 	}
    357      1.167     skrll 
    358      1.167     skrll 	/* Sync transfer, wait for completion. */
    359      1.138       mrg 	usbd_lock_pipe(pipe);
    360      1.169     skrll 	while (!xfer->ux_done) {
    361      1.169     skrll 		if (pipe->up_dev->ud_bus->ub_usepolling)
    362      1.103    provos 			panic("usbd_transfer: not done");
    363      1.175  pgoyette 		USBHIST_LOG(usbdebug, "<- sleeping on xfer %#jx",
    364      1.175  pgoyette 		    (uintptr_t)xfer, 0, 0, 0);
    365      1.138       mrg 
    366      1.144  christos 		err = 0;
    367      1.138       mrg 		if ((flags & USBD_SYNCHRONOUS_SIG) != 0) {
    368      1.169     skrll 			err = cv_wait_sig(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
    369      1.138       mrg 		} else {
    370      1.169     skrll 			cv_wait(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
    371      1.138       mrg 		}
    372      1.154   aymeric 		if (err) {
    373      1.169     skrll 			if (!xfer->ux_done)
    374      1.169     skrll 				pipe->up_methods->upm_abort(xfer);
    375      1.144  christos 			break;
    376      1.154   aymeric 		}
    377       1.31  augustss 	}
    378      1.138       mrg 	usbd_unlock_pipe(pipe);
    379      1.169     skrll 	return xfer->ux_status;
    380       1.31  augustss }
    381       1.31  augustss 
    382       1.31  augustss /* Like usbd_transfer(), but waits for completion. */
    383       1.31  augustss usbd_status
    384      1.169     skrll usbd_sync_transfer(struct usbd_xfer *xfer)
    385       1.31  augustss {
    386      1.169     skrll 	xfer->ux_flags |= USBD_SYNCHRONOUS;
    387      1.169     skrll 	return usbd_transfer(xfer);
    388       1.16  augustss }
    389       1.16  augustss 
    390      1.138       mrg /* Like usbd_transfer(), but waits for completion and listens for signals. */
    391      1.138       mrg usbd_status
    392      1.169     skrll usbd_sync_transfer_sig(struct usbd_xfer *xfer)
    393      1.138       mrg {
    394      1.169     skrll 	xfer->ux_flags |= USBD_SYNCHRONOUS | USBD_SYNCHRONOUS_SIG;
    395      1.169     skrll 	return usbd_transfer(xfer);
    396      1.138       mrg }
    397      1.138       mrg 
    398      1.169     skrll static void *
    399      1.169     skrll usbd_alloc_buffer(struct usbd_xfer *xfer, uint32_t size)
    400       1.36  augustss {
    401      1.169     skrll 	KASSERT(xfer->ux_buf == NULL);
    402      1.169     skrll 	KASSERT(size != 0);
    403      1.169     skrll 
    404      1.169     skrll 	xfer->ux_bufsize = 0;
    405      1.169     skrll #if NUSB_DMA > 0
    406      1.169     skrll 	struct usbd_bus *bus = xfer->ux_bus;
    407      1.169     skrll 
    408      1.169     skrll 	if (bus->ub_usedma) {
    409      1.169     skrll 		usb_dma_t *dmap = &xfer->ux_dmabuf;
    410      1.169     skrll 
    411      1.169     skrll 		int err = usb_allocmem_flags(bus, size, 0, dmap, bus->ub_dmaflags);
    412      1.169     skrll 		if (err) {
    413      1.169     skrll 			return NULL;
    414      1.169     skrll 		}
    415      1.169     skrll 		xfer->ux_buf = KERNADDR(&xfer->ux_dmabuf, 0);
    416      1.169     skrll 		xfer->ux_bufsize = size;
    417       1.37  augustss 
    418      1.169     skrll 		return xfer->ux_buf;
    419      1.169     skrll 	}
    420       1.92  augustss #endif
    421      1.169     skrll 	KASSERT(xfer->ux_bus->ub_usedma == false);
    422      1.169     skrll 	xfer->ux_buf = kmem_alloc(size, KM_SLEEP);
    423      1.173       chs 	xfer->ux_bufsize = size;
    424      1.169     skrll 	return xfer->ux_buf;
    425       1.36  augustss }
    426       1.36  augustss 
    427      1.169     skrll static void
    428      1.169     skrll usbd_free_buffer(struct usbd_xfer *xfer)
    429       1.36  augustss {
    430      1.169     skrll 	KASSERT(xfer->ux_buf != NULL);
    431      1.169     skrll 	KASSERT(xfer->ux_bufsize != 0);
    432      1.169     skrll 
    433      1.169     skrll 	void *buf = xfer->ux_buf;
    434      1.169     skrll 	uint32_t size = xfer->ux_bufsize;
    435      1.169     skrll 
    436      1.169     skrll 	xfer->ux_buf = NULL;
    437      1.169     skrll 	xfer->ux_bufsize = 0;
    438      1.169     skrll 
    439      1.169     skrll #if NUSB_DMA > 0
    440      1.169     skrll 	struct usbd_bus *bus = xfer->ux_bus;
    441      1.169     skrll 
    442      1.169     skrll 	if (bus->ub_usedma) {
    443      1.169     skrll 		usb_dma_t *dmap = &xfer->ux_dmabuf;
    444      1.169     skrll 
    445      1.169     skrll 		usb_freemem(bus, dmap);
    446       1.37  augustss 		return;
    447       1.37  augustss 	}
    448       1.37  augustss #endif
    449      1.169     skrll 	KASSERT(xfer->ux_bus->ub_usedma == false);
    450      1.169     skrll 
    451      1.169     skrll 	kmem_free(buf, size);
    452       1.36  augustss }
    453       1.36  augustss 
    454       1.38  augustss void *
    455      1.169     skrll usbd_get_buffer(struct usbd_xfer *xfer)
    456       1.38  augustss {
    457      1.169     skrll 	return xfer->ux_buf;
    458       1.38  augustss }
    459       1.38  augustss 
    460      1.169     skrll struct usbd_pipe *
    461      1.169     skrll usbd_get_pipe0(struct usbd_device *dev)
    462        1.1  augustss {
    463      1.169     skrll 
    464      1.169     skrll 	return dev->ud_pipe0;
    465      1.169     skrll }
    466      1.169     skrll 
    467      1.169     skrll static struct usbd_xfer *
    468      1.169     skrll usbd_alloc_xfer(struct usbd_device *dev, unsigned int nframes)
    469      1.169     skrll {
    470      1.169     skrll 	struct usbd_xfer *xfer;
    471        1.1  augustss 
    472  1.182.4.1    martin 	USBHIST_FUNC();
    473      1.162     skrll 
    474      1.169     skrll 	ASSERT_SLEEPABLE();
    475      1.169     skrll 
    476      1.169     skrll 	xfer = dev->ud_bus->ub_methods->ubm_allocx(dev->ud_bus, nframes);
    477       1.48  augustss 	if (xfer == NULL)
    478  1.182.4.1    martin 		goto out;
    479      1.169     skrll 	xfer->ux_bus = dev->ud_bus;
    480      1.169     skrll 	callout_init(&xfer->ux_callout, CALLOUT_MPSAFE);
    481  1.182.4.3    martin 	callout_setfunc(&xfer->ux_callout, usbd_xfer_timeout, xfer);
    482      1.169     skrll 	cv_init(&xfer->ux_cv, "usbxfer");
    483  1.182.4.3    martin 	usb_init_task(&xfer->ux_aborttask, usbd_xfer_timeout_task, xfer,
    484  1.182.4.3    martin 	    USB_TASKQ_MPSAFE);
    485      1.162     skrll 
    486  1.182.4.1    martin out:
    487  1.182.4.1    martin 	USBHIST_CALLARGS(usbdebug, "returns %#jx", (uintptr_t)xfer, 0, 0, 0);
    488      1.162     skrll 
    489      1.169     skrll 	return xfer;
    490        1.1  augustss }
    491        1.1  augustss 
    492      1.169     skrll static usbd_status
    493      1.169     skrll usbd_free_xfer(struct usbd_xfer *xfer)
    494        1.1  augustss {
    495  1.182.4.1    martin 	USBHIST_FUNC();
    496  1.182.4.1    martin 	USBHIST_CALLARGS(usbdebug, "%#jx", (uintptr_t)xfer, 0, 0, 0);
    497      1.162     skrll 
    498      1.169     skrll 	if (xfer->ux_buf) {
    499       1.48  augustss 		usbd_free_buffer(xfer);
    500      1.169     skrll 	}
    501  1.182.4.3    martin 
    502  1.182.4.3    martin 	/* Wait for any straggling timeout to complete. */
    503  1.182.4.3    martin 	mutex_enter(xfer->ux_bus->ub_lock);
    504  1.182.4.3    martin 	xfer->ux_timeout_reset = false; /* do not resuscitate */
    505  1.182.4.3    martin 	callout_halt(&xfer->ux_callout, xfer->ux_bus->ub_lock);
    506  1.182.4.3    martin 	usb_rem_task_wait(xfer->ux_pipe->up_dev, &xfer->ux_aborttask,
    507  1.182.4.3    martin 	    USB_TASKQ_HC, xfer->ux_bus->ub_lock);
    508  1.182.4.3    martin 	mutex_exit(xfer->ux_bus->ub_lock);
    509  1.182.4.3    martin 
    510      1.169     skrll 	cv_destroy(&xfer->ux_cv);
    511      1.169     skrll 	xfer->ux_bus->ub_methods->ubm_freex(xfer->ux_bus, xfer);
    512      1.169     skrll 	return USBD_NORMAL_COMPLETION;
    513      1.169     skrll }
    514      1.169     skrll 
    515      1.169     skrll int
    516      1.169     skrll usbd_create_xfer(struct usbd_pipe *pipe, size_t len, unsigned int flags,
    517      1.169     skrll     unsigned int nframes, struct usbd_xfer **xp)
    518      1.169     skrll {
    519      1.169     skrll 	KASSERT(xp != NULL);
    520      1.171     pooka 	void *buf = NULL;
    521      1.169     skrll 
    522      1.169     skrll 	struct usbd_xfer *xfer = usbd_alloc_xfer(pipe->up_dev, nframes);
    523      1.169     skrll 	if (xfer == NULL)
    524      1.169     skrll 		return ENOMEM;
    525      1.169     skrll 
    526      1.169     skrll 	if (len) {
    527      1.169     skrll 		buf = usbd_alloc_buffer(xfer, len);
    528      1.169     skrll 		if (!buf) {
    529      1.169     skrll 			usbd_free_xfer(xfer);
    530      1.169     skrll 			return ENOMEM;
    531      1.169     skrll 		}
    532      1.169     skrll 	}
    533      1.169     skrll 	xfer->ux_pipe = pipe;
    534      1.169     skrll 	xfer->ux_flags = flags;
    535      1.169     skrll 	xfer->ux_nframes = nframes;
    536      1.169     skrll 	xfer->ux_methods = pipe->up_methods;
    537      1.169     skrll 
    538      1.169     skrll 	if (xfer->ux_methods->upm_init) {
    539      1.169     skrll 		int err = xfer->ux_methods->upm_init(xfer);
    540      1.169     skrll 		if (err) {
    541      1.169     skrll 			if (buf)
    542      1.169     skrll 				usbd_free_buffer(xfer);
    543      1.169     skrll 			usbd_free_xfer(xfer);
    544      1.169     skrll 			return err;
    545      1.169     skrll 		}
    546      1.169     skrll 	}
    547      1.169     skrll 
    548      1.169     skrll 	*xp = xfer;
    549      1.169     skrll 	return 0;
    550        1.1  augustss }
    551        1.1  augustss 
    552       1.36  augustss void
    553      1.169     skrll usbd_destroy_xfer(struct usbd_xfer *xfer)
    554      1.169     skrll {
    555      1.169     skrll 
    556      1.169     skrll 	if (xfer->ux_methods->upm_fini) {
    557      1.169     skrll 		xfer->ux_methods->upm_fini(xfer);
    558      1.169     skrll 	}
    559      1.169     skrll 
    560      1.169     skrll 	usbd_free_xfer(xfer);
    561        1.1  augustss }
    562        1.1  augustss 
    563       1.36  augustss void
    564      1.169     skrll usbd_setup_xfer(struct usbd_xfer *xfer, void *priv, void *buffer,
    565      1.169     skrll     uint32_t length, uint16_t flags, uint32_t timeout, usbd_callback callback)
    566      1.169     skrll {
    567      1.169     skrll 	KASSERT(xfer->ux_pipe);
    568      1.169     skrll 
    569      1.169     skrll 	xfer->ux_priv = priv;
    570      1.169     skrll 	xfer->ux_buffer = buffer;
    571      1.169     skrll 	xfer->ux_length = length;
    572      1.169     skrll 	xfer->ux_actlen = 0;
    573      1.169     skrll 	xfer->ux_flags = flags;
    574      1.169     skrll 	xfer->ux_timeout = timeout;
    575      1.169     skrll 	xfer->ux_status = USBD_NOT_STARTED;
    576      1.169     skrll 	xfer->ux_callback = callback;
    577      1.169     skrll 	xfer->ux_rqflags &= ~URQ_REQUEST;
    578      1.169     skrll 	xfer->ux_nframes = 0;
    579       1.36  augustss }
    580       1.36  augustss 
    581       1.36  augustss void
    582      1.169     skrll usbd_setup_default_xfer(struct usbd_xfer *xfer, struct usbd_device *dev,
    583      1.169     skrll     void *priv, uint32_t timeout, usb_device_request_t *req, void *buffer,
    584      1.169     skrll     uint32_t length, uint16_t flags, usbd_callback callback)
    585      1.169     skrll {
    586      1.169     skrll 	KASSERT(xfer->ux_pipe == dev->ud_pipe0);
    587      1.169     skrll 
    588      1.169     skrll 	xfer->ux_priv = priv;
    589      1.169     skrll 	xfer->ux_buffer = buffer;
    590      1.169     skrll 	xfer->ux_length = length;
    591      1.169     skrll 	xfer->ux_actlen = 0;
    592      1.169     skrll 	xfer->ux_flags = flags;
    593      1.169     skrll 	xfer->ux_timeout = timeout;
    594      1.169     skrll 	xfer->ux_status = USBD_NOT_STARTED;
    595      1.169     skrll 	xfer->ux_callback = callback;
    596      1.169     skrll 	xfer->ux_request = *req;
    597      1.169     skrll 	xfer->ux_rqflags |= URQ_REQUEST;
    598      1.169     skrll 	xfer->ux_nframes = 0;
    599      1.169     skrll }
    600      1.169     skrll 
    601      1.169     skrll void
    602      1.169     skrll usbd_setup_isoc_xfer(struct usbd_xfer *xfer, void *priv, uint16_t *frlengths,
    603      1.169     skrll     uint32_t nframes, uint16_t flags, usbd_callback callback)
    604      1.169     skrll {
    605      1.169     skrll 	xfer->ux_priv = priv;
    606      1.169     skrll 	xfer->ux_buffer = NULL;
    607      1.169     skrll 	xfer->ux_length = 0;
    608      1.169     skrll 	xfer->ux_actlen = 0;
    609      1.169     skrll 	xfer->ux_flags = flags;
    610      1.169     skrll 	xfer->ux_timeout = USBD_NO_TIMEOUT;
    611      1.169     skrll 	xfer->ux_status = USBD_NOT_STARTED;
    612      1.169     skrll 	xfer->ux_callback = callback;
    613      1.169     skrll 	xfer->ux_rqflags &= ~URQ_REQUEST;
    614      1.169     skrll 	xfer->ux_frlengths = frlengths;
    615      1.169     skrll 	xfer->ux_nframes = nframes;
    616        1.1  augustss }
    617        1.1  augustss 
    618       1.31  augustss void
    619      1.169     skrll usbd_get_xfer_status(struct usbd_xfer *xfer, void **priv,
    620      1.169     skrll 		     void **buffer, uint32_t *count, usbd_status *status)
    621        1.1  augustss {
    622       1.48  augustss 	if (priv != NULL)
    623      1.169     skrll 		*priv = xfer->ux_priv;
    624       1.48  augustss 	if (buffer != NULL)
    625      1.169     skrll 		*buffer = xfer->ux_buffer;
    626       1.48  augustss 	if (count != NULL)
    627      1.169     skrll 		*count = xfer->ux_actlen;
    628       1.48  augustss 	if (status != NULL)
    629      1.169     skrll 		*status = xfer->ux_status;
    630        1.1  augustss }
    631        1.1  augustss 
    632        1.1  augustss usb_config_descriptor_t *
    633      1.169     skrll usbd_get_config_descriptor(struct usbd_device *dev)
    634        1.1  augustss {
    635      1.169     skrll 	KASSERT(dev != NULL);
    636      1.169     skrll 
    637      1.169     skrll 	return dev->ud_cdesc;
    638        1.1  augustss }
    639        1.1  augustss 
    640        1.1  augustss usb_interface_descriptor_t *
    641      1.169     skrll usbd_get_interface_descriptor(struct usbd_interface *iface)
    642        1.1  augustss {
    643      1.169     skrll 	KASSERT(iface != NULL);
    644      1.169     skrll 
    645      1.169     skrll 	return iface->ui_idesc;
    646        1.1  augustss }
    647        1.1  augustss 
    648        1.1  augustss usb_device_descriptor_t *
    649      1.169     skrll usbd_get_device_descriptor(struct usbd_device *dev)
    650        1.1  augustss {
    651      1.169     skrll 	KASSERT(dev != NULL);
    652      1.169     skrll 
    653      1.169     skrll 	return &dev->ud_ddesc;
    654        1.1  augustss }
    655        1.1  augustss 
    656        1.1  augustss usb_endpoint_descriptor_t *
    657      1.169     skrll usbd_interface2endpoint_descriptor(struct usbd_interface *iface, uint8_t index)
    658        1.1  augustss {
    659      1.169     skrll 
    660      1.169     skrll 	if (index >= iface->ui_idesc->bNumEndpoints)
    661      1.169     skrll 		return NULL;
    662      1.169     skrll 	return iface->ui_endpoints[index].ue_edesc;
    663        1.1  augustss }
    664        1.1  augustss 
    665      1.125    jmorse /* Some drivers may wish to abort requests on the default pipe, *
    666      1.125    jmorse  * but there is no mechanism for getting a handle on it.        */
    667      1.125    jmorse usbd_status
    668      1.125    jmorse usbd_abort_default_pipe(struct usbd_device *device)
    669      1.125    jmorse {
    670      1.169     skrll 	return usbd_abort_pipe(device->ud_pipe0);
    671      1.125    jmorse }
    672      1.125    jmorse 
    673      1.102  augustss usbd_status
    674      1.169     skrll usbd_abort_pipe(struct usbd_pipe *pipe)
    675        1.1  augustss {
    676       1.48  augustss 	usbd_status err;
    677        1.1  augustss 
    678      1.169     skrll 	KASSERT(pipe != NULL);
    679      1.169     skrll 
    680      1.138       mrg 	usbd_lock_pipe(pipe);
    681       1.48  augustss 	err = usbd_ar_pipe(pipe);
    682      1.138       mrg 	usbd_unlock_pipe(pipe);
    683      1.169     skrll 	return err;
    684        1.1  augustss }
    685      1.102  augustss 
    686      1.102  augustss usbd_status
    687      1.169     skrll usbd_clear_endpoint_stall(struct usbd_pipe *pipe)
    688        1.1  augustss {
    689      1.169     skrll 	struct usbd_device *dev = pipe->up_dev;
    690        1.1  augustss 	usb_device_request_t req;
    691       1.48  augustss 	usbd_status err;
    692        1.1  augustss 
    693      1.162     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    694       1.33  augustss 
    695      1.102  augustss 	/*
    696       1.87  augustss 	 * Clearing en endpoint stall resets the endpoint toggle, so
    697       1.33  augustss 	 * do the same to the HC toggle.
    698       1.33  augustss 	 */
    699      1.169     skrll 	pipe->up_methods->upm_cleartoggle(pipe);
    700       1.33  augustss 
    701        1.1  augustss 	req.bmRequestType = UT_WRITE_ENDPOINT;
    702        1.1  augustss 	req.bRequest = UR_CLEAR_FEATURE;
    703       1.15  augustss 	USETW(req.wValue, UF_ENDPOINT_HALT);
    704      1.169     skrll 	USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
    705        1.1  augustss 	USETW(req.wLength, 0);
    706       1.48  augustss 	err = usbd_do_request(dev, &req, 0);
    707        1.1  augustss #if 0
    708        1.1  augustss XXX should we do this?
    709       1.51  augustss 	if (!err) {
    710        1.1  augustss 		pipe->state = USBD_PIPE_ACTIVE;
    711        1.1  augustss 		/* XXX activate pipe */
    712        1.1  augustss 	}
    713        1.1  augustss #endif
    714      1.169     skrll 	return err;
    715        1.1  augustss }
    716        1.1  augustss 
    717      1.139       mrg void
    718      1.156     skrll usbd_clear_endpoint_stall_task(void *arg)
    719        1.7  augustss {
    720      1.169     skrll 	struct usbd_pipe *pipe = arg;
    721      1.169     skrll 	struct usbd_device *dev = pipe->up_dev;
    722        1.7  augustss 	usb_device_request_t req;
    723        1.7  augustss 
    724      1.169     skrll 	pipe->up_methods->upm_cleartoggle(pipe);
    725       1.33  augustss 
    726        1.7  augustss 	req.bmRequestType = UT_WRITE_ENDPOINT;
    727        1.7  augustss 	req.bRequest = UR_CLEAR_FEATURE;
    728       1.15  augustss 	USETW(req.wValue, UF_ENDPOINT_HALT);
    729      1.169     skrll 	USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
    730        1.7  augustss 	USETW(req.wLength, 0);
    731      1.156     skrll 	(void)usbd_do_request(dev, &req, 0);
    732      1.139       mrg }
    733      1.139       mrg 
    734      1.139       mrg void
    735      1.169     skrll usbd_clear_endpoint_stall_async(struct usbd_pipe *pipe)
    736      1.139       mrg {
    737      1.169     skrll 	usb_add_task(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER);
    738        1.7  augustss }
    739        1.7  augustss 
    740       1.61  augustss void
    741      1.169     skrll usbd_clear_endpoint_toggle(struct usbd_pipe *pipe)
    742       1.61  augustss {
    743      1.169     skrll 
    744      1.169     skrll 	pipe->up_methods->upm_cleartoggle(pipe);
    745       1.61  augustss }
    746       1.61  augustss 
    747      1.102  augustss usbd_status
    748      1.169     skrll usbd_endpoint_count(struct usbd_interface *iface, uint8_t *count)
    749        1.1  augustss {
    750      1.169     skrll 	KASSERT(iface != NULL);
    751      1.169     skrll 	KASSERT(iface->ui_idesc != NULL);
    752      1.169     skrll 
    753      1.169     skrll 	*count = iface->ui_idesc->bNumEndpoints;
    754      1.169     skrll 	return USBD_NORMAL_COMPLETION;
    755        1.1  augustss }
    756        1.1  augustss 
    757      1.102  augustss usbd_status
    758      1.169     skrll usbd_interface_count(struct usbd_device *dev, uint8_t *count)
    759        1.1  augustss {
    760      1.169     skrll 
    761      1.169     skrll 	if (dev->ud_cdesc == NULL)
    762      1.169     skrll 		return USBD_NOT_CONFIGURED;
    763      1.169     skrll 	*count = dev->ud_cdesc->bNumInterface;
    764      1.169     skrll 	return USBD_NORMAL_COMPLETION;
    765        1.1  augustss }
    766        1.1  augustss 
    767       1.85  augustss void
    768      1.169     skrll usbd_interface2device_handle(struct usbd_interface *iface,
    769      1.169     skrll 			     struct usbd_device **dev)
    770        1.1  augustss {
    771      1.169     skrll 
    772      1.169     skrll 	*dev = iface->ui_dev;
    773        1.1  augustss }
    774        1.1  augustss 
    775      1.102  augustss usbd_status
    776      1.169     skrll usbd_device2interface_handle(struct usbd_device *dev,
    777      1.169     skrll 			     uint8_t ifaceno, struct usbd_interface **iface)
    778        1.1  augustss {
    779      1.169     skrll 
    780      1.169     skrll 	if (dev->ud_cdesc == NULL)
    781      1.169     skrll 		return USBD_NOT_CONFIGURED;
    782      1.169     skrll 	if (ifaceno >= dev->ud_cdesc->bNumInterface)
    783      1.169     skrll 		return USBD_INVAL;
    784      1.169     skrll 	*iface = &dev->ud_ifaces[ifaceno];
    785      1.169     skrll 	return USBD_NORMAL_COMPLETION;
    786        1.1  augustss }
    787        1.1  augustss 
    788      1.169     skrll struct usbd_device *
    789      1.169     skrll usbd_pipe2device_handle(struct usbd_pipe *pipe)
    790       1.36  augustss {
    791      1.169     skrll 	KASSERT(pipe != NULL);
    792      1.169     skrll 
    793      1.169     skrll 	return pipe->up_dev;
    794       1.36  augustss }
    795       1.36  augustss 
    796       1.13  augustss /* XXXX use altno */
    797        1.3  augustss usbd_status
    798      1.169     skrll usbd_set_interface(struct usbd_interface *iface, int altidx)
    799        1.3  augustss {
    800        1.3  augustss 	usb_device_request_t req;
    801       1.48  augustss 	usbd_status err;
    802       1.73  augustss 	void *endpoints;
    803       1.13  augustss 
    804  1.182.4.1    martin 	USBHIST_FUNC();
    805      1.169     skrll 
    806      1.169     skrll 	if (LIST_FIRST(&iface->ui_pipes) != NULL)
    807      1.169     skrll 		return USBD_IN_USE;
    808       1.13  augustss 
    809      1.169     skrll 	endpoints = iface->ui_endpoints;
    810      1.169     skrll 	int nendpt = iface->ui_idesc->bNumEndpoints;
    811  1.182.4.1    martin 	USBHIST_CALLARGS(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
    812      1.175  pgoyette 	    (uintptr_t)iface, (uintptr_t)endpoints,
    813      1.175  pgoyette 	    iface->ui_idesc->bNumEndpoints, 0);
    814      1.169     skrll 	err = usbd_fill_iface_data(iface->ui_dev, iface->ui_index, altidx);
    815       1.48  augustss 	if (err)
    816      1.169     skrll 		return err;
    817       1.73  augustss 
    818       1.76  augustss 	/* new setting works, we can free old endpoints */
    819      1.169     skrll 	if (endpoints != NULL) {
    820      1.175  pgoyette 		USBHIST_LOG(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
    821      1.175  pgoyette 		    (uintptr_t)iface, (uintptr_t)endpoints, nendpt, 0);
    822      1.169     skrll 		kmem_free(endpoints, nendpt * sizeof(struct usbd_endpoint));
    823       1.73  augustss 	}
    824      1.169     skrll 	KASSERT(iface->ui_idesc != NULL);
    825        1.3  augustss 
    826        1.3  augustss 	req.bmRequestType = UT_WRITE_INTERFACE;
    827        1.3  augustss 	req.bRequest = UR_SET_INTERFACE;
    828      1.169     skrll 	USETW(req.wValue, iface->ui_idesc->bAlternateSetting);
    829      1.169     skrll 	USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
    830        1.3  augustss 	USETW(req.wLength, 0);
    831      1.169     skrll 	return usbd_do_request(iface->ui_dev, &req, 0);
    832       1.12  augustss }
    833       1.12  augustss 
    834       1.12  augustss int
    835       1.74  augustss usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
    836       1.12  augustss {
    837       1.13  augustss 	char *p = (char *)cdesc;
    838       1.13  augustss 	char *end = p + UGETW(cdesc->wTotalLength);
    839       1.12  augustss 	usb_interface_descriptor_t *d;
    840       1.12  augustss 	int n;
    841       1.12  augustss 
    842       1.12  augustss 	for (n = 0; p < end; p += d->bLength) {
    843       1.12  augustss 		d = (usb_interface_descriptor_t *)p;
    844      1.102  augustss 		if (p + d->bLength <= end &&
    845       1.12  augustss 		    d->bDescriptorType == UDESC_INTERFACE &&
    846       1.13  augustss 		    d->bInterfaceNumber == ifaceno)
    847       1.12  augustss 			n++;
    848       1.12  augustss 	}
    849      1.169     skrll 	return n;
    850       1.13  augustss }
    851       1.13  augustss 
    852       1.13  augustss int
    853      1.169     skrll usbd_get_interface_altindex(struct usbd_interface *iface)
    854       1.13  augustss {
    855      1.169     skrll 	return iface->ui_altindex;
    856       1.12  augustss }
    857       1.12  augustss 
    858       1.12  augustss usbd_status
    859      1.169     skrll usbd_get_interface(struct usbd_interface *iface, uint8_t *aiface)
    860       1.12  augustss {
    861       1.12  augustss 	usb_device_request_t req;
    862       1.12  augustss 
    863       1.12  augustss 	req.bmRequestType = UT_READ_INTERFACE;
    864       1.12  augustss 	req.bRequest = UR_GET_INTERFACE;
    865       1.12  augustss 	USETW(req.wValue, 0);
    866      1.169     skrll 	USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
    867       1.12  augustss 	USETW(req.wLength, 1);
    868      1.169     skrll 	return usbd_do_request(iface->ui_dev, &req, aiface);
    869        1.3  augustss }
    870        1.1  augustss 
    871        1.1  augustss /*** Internal routines ***/
    872        1.1  augustss 
    873      1.174     skrll /* Dequeue all pipe operations, called with bus lock held. */
    874       1.69  augustss Static usbd_status
    875      1.169     skrll usbd_ar_pipe(struct usbd_pipe *pipe)
    876        1.1  augustss {
    877      1.169     skrll 	struct usbd_xfer *xfer;
    878        1.1  augustss 
    879  1.182.4.1    martin 	USBHIST_FUNC();
    880  1.182.4.1    martin 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
    881      1.162     skrll 
    882      1.169     skrll 	KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
    883      1.138       mrg 
    884       1.28  augustss #ifdef USB_DEBUG
    885       1.28  augustss 	if (usbdebug > 5)
    886       1.28  augustss 		usbd_dump_queue(pipe);
    887       1.28  augustss #endif
    888      1.169     skrll 	pipe->up_repeat = 0;
    889  1.182.4.2    martin 	pipe->up_running = 0;
    890      1.169     skrll 	pipe->up_aborting = 1;
    891      1.169     skrll 	while ((xfer = SIMPLEQ_FIRST(&pipe->up_queue)) != NULL) {
    892      1.175  pgoyette 		USBHIST_LOG(usbdebug, "pipe = %#jx xfer = %#jx "
    893      1.175  pgoyette 		    "(methods = %#jx)", (uintptr_t)pipe, (uintptr_t)xfer,
    894      1.175  pgoyette 		    (uintptr_t)pipe->up_methods, 0);
    895      1.182       rin 		if (xfer->ux_status == USBD_NOT_STARTED) {
    896      1.182       rin 			SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
    897      1.182       rin 		} else {
    898      1.182       rin 			/* Make the HC abort it (and invoke the callback). */
    899      1.182       rin 			pipe->up_methods->upm_abort(xfer);
    900      1.182       rin 			/* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
    901      1.182       rin 		}
    902       1.20  augustss 	}
    903      1.169     skrll 	pipe->up_aborting = 0;
    904      1.169     skrll 	return USBD_NORMAL_COMPLETION;
    905        1.1  augustss }
    906        1.1  augustss 
    907      1.138       mrg /* Called with USB lock held. */
    908       1.31  augustss void
    909      1.169     skrll usb_transfer_complete(struct usbd_xfer *xfer)
    910        1.1  augustss {
    911      1.169     skrll 	struct usbd_pipe *pipe = xfer->ux_pipe;
    912      1.172     skrll 	struct usbd_bus *bus = pipe->up_dev->ud_bus;
    913      1.169     skrll 	int sync = xfer->ux_flags & USBD_SYNCHRONOUS;
    914      1.179      manu 	int erred;
    915      1.169     skrll 	int polling = bus->ub_usepolling;
    916      1.169     skrll 	int repeat = pipe->up_repeat;
    917       1.31  augustss 
    918  1.182.4.1    martin 	USBHIST_FUNC();
    919  1.182.4.1    martin 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx xfer = %#jx status = %jd "
    920      1.175  pgoyette 	    "actlen = %jd", (uintptr_t)pipe, (uintptr_t)xfer, xfer->ux_status,
    921      1.175  pgoyette 	    xfer->ux_actlen);
    922      1.134  jmcneill 
    923      1.169     skrll 	KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
    924      1.177       mrg 	KASSERTMSG(xfer->ux_state == XFER_ONQU, "xfer %p state is %x", xfer,
    925      1.177       mrg 	    xfer->ux_state);
    926      1.169     skrll 	KASSERT(pipe != NULL);
    927        1.5  augustss 
    928      1.179      manu 	/*
    929      1.179      manu 	 * If device is known to miss out ack, then pretend that
    930      1.179      manu 	 * output timeout is a success. Userland should handle
    931      1.179      manu 	 * the logic to verify that the operation succeeded.
    932      1.179      manu 	 */
    933      1.179      manu 	if (pipe->up_dev->ud_quirks &&
    934      1.179      manu 	    pipe->up_dev->ud_quirks->uq_flags & UQ_MISS_OUT_ACK &&
    935      1.179      manu 	    xfer->ux_status == USBD_TIMEOUT &&
    936      1.179      manu 	    !usbd_xfer_isread(xfer)) {
    937      1.179      manu 		USBHIST_LOG(usbdebug, "Possible output ack miss for xfer %#jx: "
    938      1.179      manu 		    "hiding write timeout to %d.%s for %d bytes written",
    939      1.180      manu 		    (uintptr_t)xfer, curlwp->l_proc->p_pid, curlwp->l_lid,
    940      1.179      manu 		    xfer->ux_length);
    941      1.179      manu 
    942      1.179      manu 		xfer->ux_status = USBD_NORMAL_COMPLETION;
    943      1.179      manu 		xfer->ux_actlen = xfer->ux_length;
    944      1.179      manu 	}
    945      1.179      manu 
    946      1.179      manu 	erred = xfer->ux_status == USBD_CANCELLED ||
    947      1.179      manu 	        xfer->ux_status == USBD_TIMEOUT;
    948      1.179      manu 
    949       1.47  augustss 	if (!repeat) {
    950       1.47  augustss 		/* Remove request from queue. */
    951      1.153  jakllsch 
    952      1.169     skrll 		KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->up_queue),
    953      1.145     skrll 		    "pipe %p is empty, but xfer %p wants to complete", pipe,
    954      1.145     skrll 		     xfer);
    955      1.169     skrll 		KASSERTMSG(xfer == SIMPLEQ_FIRST(&pipe->up_queue),
    956      1.169     skrll 		    "xfer %p is not start of queue (%p is at start)", xfer,
    957      1.169     skrll 		   SIMPLEQ_FIRST(&pipe->up_queue));
    958      1.169     skrll 
    959       1.47  augustss #ifdef DIAGNOSTIC
    960      1.169     skrll 		xfer->ux_state = XFER_BUSY;
    961       1.45  augustss #endif
    962      1.169     skrll 		SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
    963       1.47  augustss 	}
    964      1.175  pgoyette 	USBHIST_LOG(usbdebug, "xfer %#jx: repeat %jd new head = %#jx",
    965      1.175  pgoyette 	    (uintptr_t)xfer, repeat, (uintptr_t)SIMPLEQ_FIRST(&pipe->up_queue),
    966      1.175  pgoyette 	    0);
    967       1.31  augustss 
    968       1.31  augustss 	/* Count completed transfers. */
    969      1.169     skrll 	++pipe->up_dev->ud_bus->ub_stats.uds_requests
    970      1.169     skrll 		[pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE];
    971        1.1  augustss 
    972      1.169     skrll 	xfer->ux_done = 1;
    973      1.169     skrll 	if (!xfer->ux_status && xfer->ux_actlen < xfer->ux_length &&
    974      1.169     skrll 	    !(xfer->ux_flags & USBD_SHORT_XFER_OK)) {
    975      1.175  pgoyette 		USBHIST_LOG(usbdebug, "short transfer %jd < %jd",
    976      1.169     skrll 		    xfer->ux_actlen, xfer->ux_length, 0, 0);
    977      1.169     skrll 		xfer->ux_status = USBD_SHORT_XFER;
    978        1.1  augustss 	}
    979       1.31  augustss 
    980      1.175  pgoyette 	USBHIST_LOG(usbdebug, "xfer %#jx doing done %#jx", (uintptr_t)xfer,
    981      1.175  pgoyette 	    (uintptr_t)pipe->up_methods->upm_done, 0, 0);
    982      1.169     skrll 	pipe->up_methods->upm_done(xfer);
    983      1.169     skrll 
    984      1.169     skrll 	if (xfer->ux_length != 0 && xfer->ux_buffer != xfer->ux_buf) {
    985      1.169     skrll 		KDASSERTMSG(xfer->ux_actlen <= xfer->ux_length,
    986      1.169     skrll 		    "actlen %d length %d",xfer->ux_actlen, xfer->ux_length);
    987      1.148  jmcneill 
    988      1.169     skrll 		/* Only if IN transfer */
    989      1.169     skrll 		if (usbd_xfer_isread(xfer)) {
    990      1.169     skrll 			memcpy(xfer->ux_buffer, xfer->ux_buf, xfer->ux_actlen);
    991      1.138       mrg 		}
    992      1.169     skrll 	}
    993      1.169     skrll 
    994      1.175  pgoyette 	USBHIST_LOG(usbdebug, "xfer %#jx doing callback %#jx status %jd",
    995      1.175  pgoyette 	    (uintptr_t)xfer, (uintptr_t)xfer->ux_callback, xfer->ux_status, 0);
    996      1.169     skrll 
    997      1.169     skrll 	if (xfer->ux_callback) {
    998      1.178       mrg 		if (!polling) {
    999      1.169     skrll 			mutex_exit(pipe->up_dev->ud_bus->ub_lock);
   1000      1.178       mrg 			if (!(pipe->up_flags & USBD_MPSAFE))
   1001      1.178       mrg 				KERNEL_LOCK(1, curlwp);
   1002      1.178       mrg 		}
   1003      1.169     skrll 
   1004      1.169     skrll 		xfer->ux_callback(xfer, xfer->ux_priv, xfer->ux_status);
   1005      1.169     skrll 
   1006      1.178       mrg 		if (!polling) {
   1007      1.178       mrg 			if (!(pipe->up_flags & USBD_MPSAFE))
   1008      1.178       mrg 				KERNEL_UNLOCK_ONE(curlwp);
   1009      1.169     skrll 			mutex_enter(pipe->up_dev->ud_bus->ub_lock);
   1010      1.178       mrg 		}
   1011      1.124  jmcneill 	}
   1012       1.31  augustss 
   1013      1.138       mrg 	if (sync && !polling) {
   1014      1.175  pgoyette 		USBHIST_LOG(usbdebug, "<- done xfer %#jx, wakeup",
   1015      1.175  pgoyette 		    (uintptr_t)xfer, 0, 0, 0);
   1016      1.169     skrll 		cv_broadcast(&xfer->ux_cv);
   1017      1.138       mrg 	}
   1018       1.31  augustss 
   1019      1.169     skrll 	if (repeat) {
   1020      1.169     skrll 		xfer->ux_actlen = 0;
   1021      1.169     skrll 		xfer->ux_status = USBD_NOT_STARTED;
   1022      1.169     skrll 	} else {
   1023       1.40  augustss 		/* XXX should we stop the queue on all errors? */
   1024      1.169     skrll 		if (erred && pipe->up_iface != NULL)	/* not control pipe */
   1025      1.169     skrll 			pipe->up_running = 0;
   1026       1.40  augustss 	}
   1027      1.169     skrll 	if (pipe->up_running && pipe->up_serialise)
   1028      1.169     skrll 		usbd_start_next(pipe);
   1029        1.1  augustss }
   1030        1.1  augustss 
   1031      1.138       mrg /* Called with USB lock held. */
   1032       1.31  augustss usbd_status
   1033      1.169     skrll usb_insert_transfer(struct usbd_xfer *xfer)
   1034        1.1  augustss {
   1035      1.169     skrll 	struct usbd_pipe *pipe = xfer->ux_pipe;
   1036       1.48  augustss 	usbd_status err;
   1037       1.31  augustss 
   1038  1.182.4.1    martin 	USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
   1039  1.182.4.1    martin 	    "xfer = %#jx pipe = %#jx running = %jd timeout = %jd",
   1040  1.182.4.1    martin 	    (uintptr_t)xfer, (uintptr_t)pipe,
   1041      1.175  pgoyette 	    pipe->up_running, xfer->ux_timeout);
   1042      1.134  jmcneill 
   1043      1.169     skrll 	KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
   1044      1.174     skrll 	KASSERTMSG(xfer->ux_state == XFER_BUSY, "xfer %p state is %x", xfer,
   1045      1.174     skrll 	    xfer->ux_state);
   1046      1.134  jmcneill 
   1047       1.93  augustss #ifdef DIAGNOSTIC
   1048      1.169     skrll 	xfer->ux_state = XFER_ONQU;
   1049       1.93  augustss #endif
   1050      1.169     skrll 	SIMPLEQ_INSERT_TAIL(&pipe->up_queue, xfer, ux_next);
   1051      1.169     skrll 	if (pipe->up_running && pipe->up_serialise)
   1052       1.48  augustss 		err = USBD_IN_PROGRESS;
   1053       1.37  augustss 	else {
   1054      1.169     skrll 		pipe->up_running = 1;
   1055       1.48  augustss 		err = USBD_NORMAL_COMPLETION;
   1056       1.37  augustss 	}
   1057      1.175  pgoyette 	USBHIST_LOG(usbdebug, "<- done xfer %#jx, err %jd", (uintptr_t)xfer,
   1058      1.175  pgoyette 	    err, 0, 0);
   1059      1.169     skrll 	return err;
   1060        1.1  augustss }
   1061        1.1  augustss 
   1062      1.138       mrg /* Called with USB lock held. */
   1063       1.31  augustss void
   1064      1.169     skrll usbd_start_next(struct usbd_pipe *pipe)
   1065       1.31  augustss {
   1066      1.169     skrll 	struct usbd_xfer *xfer;
   1067       1.48  augustss 	usbd_status err;
   1068       1.40  augustss 
   1069  1.182.4.1    martin 	USBHIST_FUNC();
   1070      1.162     skrll 
   1071      1.169     skrll 	KASSERT(pipe != NULL);
   1072      1.169     skrll 	KASSERT(pipe->up_methods != NULL);
   1073      1.169     skrll 	KASSERT(pipe->up_methods->upm_start != NULL);
   1074      1.169     skrll 	KASSERT(pipe->up_serialise == true);
   1075       1.31  augustss 
   1076      1.169     skrll 	int polling = pipe->up_dev->ud_bus->ub_usepolling;
   1077      1.169     skrll 	KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
   1078      1.138       mrg 
   1079       1.31  augustss 	/* Get next request in queue. */
   1080      1.169     skrll 	xfer = SIMPLEQ_FIRST(&pipe->up_queue);
   1081  1.182.4.1    martin 	USBHIST_CALLARGS(usbdebug, "pipe = %#jx, xfer = %#jx", (uintptr_t)pipe,
   1082      1.175  pgoyette 	    (uintptr_t)xfer, 0, 0);
   1083       1.51  augustss 	if (xfer == NULL) {
   1084      1.169     skrll 		pipe->up_running = 0;
   1085       1.51  augustss 	} else {
   1086      1.165     skrll 		if (!polling)
   1087      1.169     skrll 			mutex_exit(pipe->up_dev->ud_bus->ub_lock);
   1088      1.169     skrll 		err = pipe->up_methods->upm_start(xfer);
   1089      1.165     skrll 		if (!polling)
   1090      1.169     skrll 			mutex_enter(pipe->up_dev->ud_bus->ub_lock);
   1091      1.158     skrll 
   1092       1.48  augustss 		if (err != USBD_IN_PROGRESS) {
   1093      1.175  pgoyette 			USBHIST_LOG(usbdebug, "error = %jd", err, 0, 0, 0);
   1094      1.169     skrll 			pipe->up_running = 0;
   1095       1.31  augustss 			/* XXX do what? */
   1096       1.31  augustss 		}
   1097        1.1  augustss 	}
   1098      1.138       mrg 
   1099      1.169     skrll 	KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
   1100        1.1  augustss }
   1101        1.1  augustss 
   1102        1.1  augustss usbd_status
   1103      1.169     skrll usbd_do_request(struct usbd_device *dev, usb_device_request_t *req, void *data)
   1104        1.1  augustss {
   1105       1.17  augustss 
   1106      1.169     skrll 	return usbd_do_request_flags(dev, req, data, 0, 0,
   1107      1.169     skrll 	    USBD_DEFAULT_TIMEOUT);
   1108       1.77  augustss }
   1109       1.77  augustss 
   1110       1.77  augustss usbd_status
   1111      1.169     skrll usbd_do_request_flags(struct usbd_device *dev, usb_device_request_t *req,
   1112      1.169     skrll     void *data, uint16_t flags, int *actlen, uint32_t timeout)
   1113       1.77  augustss {
   1114  1.182.4.1    martin 	size_t len = UGETW(req->wLength);
   1115  1.182.4.1    martin 
   1116  1.182.4.1    martin 	return usbd_do_request_len(dev, req, len, data, flags, actlen, timeout);
   1117  1.182.4.1    martin }
   1118  1.182.4.1    martin 
   1119  1.182.4.1    martin usbd_status
   1120  1.182.4.1    martin usbd_do_request_len(struct usbd_device *dev, usb_device_request_t *req,
   1121  1.182.4.1    martin     size_t len, void *data, uint16_t flags, int *actlen, uint32_t timeout)
   1122  1.182.4.1    martin {
   1123      1.169     skrll 	struct usbd_xfer *xfer;
   1124       1.48  augustss 	usbd_status err;
   1125        1.1  augustss 
   1126  1.182.4.1    martin 	KASSERT(len >= UGETW(req->wLength));
   1127  1.182.4.1    martin 
   1128  1.182.4.1    martin 	USBHIST_FUNC();
   1129  1.182.4.1    martin 	USBHIST_CALLARGS(usbdebug, "dev=%#jx req=%jx flags=%jx len=%jx",
   1130  1.182.4.1    martin 	    (uintptr_t)dev, (uintptr_t)req, flags, len);
   1131  1.182.4.1    martin 
   1132      1.169     skrll 	ASSERT_SLEEPABLE();
   1133        1.7  augustss 
   1134      1.169     skrll 	int error = usbd_create_xfer(dev->ud_pipe0, len, 0, 0, &xfer);
   1135      1.169     skrll 	if (error)
   1136      1.169     skrll 		return error;
   1137      1.169     skrll 
   1138      1.169     skrll 	usbd_setup_default_xfer(xfer, dev, 0, timeout, req, data,
   1139      1.169     skrll 	    UGETW(req->wLength), flags, NULL);
   1140      1.169     skrll 	KASSERT(xfer->ux_pipe == dev->ud_pipe0);
   1141       1.48  augustss 	err = usbd_sync_transfer(xfer);
   1142        1.1  augustss #if defined(USB_DEBUG) || defined(DIAGNOSTIC)
   1143      1.169     skrll 	if (xfer->ux_actlen > xfer->ux_length) {
   1144      1.175  pgoyette 		USBHIST_LOG(usbdebug, "overrun addr = %jd type = 0x%02jx",
   1145      1.169     skrll 		    dev->ud_addr, xfer->ux_request.bmRequestType, 0, 0);
   1146      1.175  pgoyette 		USBHIST_LOG(usbdebug, "     req = 0x%02jx val = %jd "
   1147      1.175  pgoyette 		    "index = %jd",
   1148      1.169     skrll 		    xfer->ux_request.bRequest, UGETW(xfer->ux_request.wValue),
   1149      1.169     skrll 		    UGETW(xfer->ux_request.wIndex), 0);
   1150      1.175  pgoyette 		USBHIST_LOG(usbdebug, "     rlen = %jd length = %jd "
   1151      1.175  pgoyette 		    "actlen = %jd",
   1152      1.169     skrll 		    UGETW(xfer->ux_request.wLength),
   1153      1.169     skrll 		    xfer->ux_length, xfer->ux_actlen, 0);
   1154      1.112    dogcow 	}
   1155        1.1  augustss #endif
   1156       1.48  augustss 	if (actlen != NULL)
   1157      1.169     skrll 		*actlen = xfer->ux_actlen;
   1158      1.169     skrll 
   1159      1.169     skrll 	usbd_destroy_xfer(xfer);
   1160       1.15  augustss 
   1161      1.132      matt 	if (err) {
   1162      1.175  pgoyette 		USBHIST_LOG(usbdebug, "returning err = %jd", err, 0, 0, 0);
   1163      1.132      matt 	}
   1164      1.169     skrll 	return err;
   1165        1.7  augustss }
   1166        1.7  augustss 
   1167      1.176   khorben static void
   1168      1.176   khorben usbd_request_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
   1169      1.176   khorben {
   1170      1.176   khorben 	usbd_free_xfer(xfer);
   1171      1.176   khorben }
   1172      1.176   khorben 
   1173      1.176   khorben /*
   1174      1.176   khorben  * Execute a request without waiting for completion.
   1175      1.176   khorben  * Can be used from interrupt context.
   1176      1.176   khorben  */
   1177      1.176   khorben usbd_status
   1178      1.176   khorben usbd_request_async(struct usbd_device *dev, struct usbd_xfer *xfer,
   1179      1.176   khorben     usb_device_request_t *req, void *priv, usbd_callback callback)
   1180      1.176   khorben {
   1181      1.176   khorben 	usbd_status err;
   1182      1.176   khorben 
   1183      1.176   khorben 	if (callback == NULL)
   1184      1.176   khorben 		callback = usbd_request_async_cb;
   1185      1.176   khorben 
   1186      1.176   khorben 	usbd_setup_default_xfer(xfer, dev, priv,
   1187      1.176   khorben 	    USBD_DEFAULT_TIMEOUT, req, NULL, UGETW(req->wLength), 0,
   1188      1.176   khorben 	    callback);
   1189      1.176   khorben 	err = usbd_transfer(xfer);
   1190      1.176   khorben 	if (err != USBD_IN_PROGRESS) {
   1191      1.176   khorben 		usbd_free_xfer(xfer);
   1192      1.176   khorben 		return (err);
   1193      1.176   khorben 	}
   1194      1.176   khorben 	return (USBD_NORMAL_COMPLETION);
   1195      1.176   khorben }
   1196      1.176   khorben 
   1197       1.78  augustss const struct usbd_quirks *
   1198      1.169     skrll usbd_get_quirks(struct usbd_device *dev)
   1199        1.1  augustss {
   1200       1.81  augustss #ifdef DIAGNOSTIC
   1201       1.81  augustss 	if (dev == NULL) {
   1202       1.81  augustss 		printf("usbd_get_quirks: dev == NULL\n");
   1203       1.81  augustss 		return 0;
   1204       1.81  augustss 	}
   1205       1.81  augustss #endif
   1206      1.169     skrll 	return dev->ud_quirks;
   1207        1.1  augustss }
   1208        1.1  augustss 
   1209        1.1  augustss /* XXX do periodic free() of free list */
   1210        1.1  augustss 
   1211        1.6  augustss /*
   1212        1.6  augustss  * Called from keyboard driver when in polling mode.
   1213        1.6  augustss  */
   1214        1.6  augustss void
   1215      1.169     skrll usbd_dopoll(struct usbd_interface *iface)
   1216        1.6  augustss {
   1217      1.169     skrll 	iface->ui_dev->ud_bus->ub_methods->ubm_dopoll(iface->ui_dev->ud_bus);
   1218        1.8  augustss }
   1219        1.8  augustss 
   1220      1.138       mrg /*
   1221      1.178       mrg  * This is for keyboard driver as well, which only operates in polling
   1222      1.178       mrg  * mode from the ask root, etc., prompt and from DDB.
   1223      1.138       mrg  */
   1224        1.8  augustss void
   1225      1.169     skrll usbd_set_polling(struct usbd_device *dev, int on)
   1226        1.8  augustss {
   1227       1.42  augustss 	if (on)
   1228      1.169     skrll 		dev->ud_bus->ub_usepolling++;
   1229       1.42  augustss 	else
   1230      1.169     skrll 		dev->ud_bus->ub_usepolling--;
   1231      1.133  jmcneill 
   1232      1.133  jmcneill 	/* Kick the host controller when switching modes */
   1233      1.169     skrll 	mutex_enter(dev->ud_bus->ub_lock);
   1234      1.169     skrll 	dev->ud_bus->ub_methods->ubm_softint(dev->ud_bus);
   1235      1.169     skrll 	mutex_exit(dev->ud_bus->ub_lock);
   1236        1.6  augustss }
   1237       1.12  augustss 
   1238       1.12  augustss 
   1239       1.12  augustss usb_endpoint_descriptor_t *
   1240      1.169     skrll usbd_get_endpoint_descriptor(struct usbd_interface *iface, uint8_t address)
   1241       1.12  augustss {
   1242       1.12  augustss 	struct usbd_endpoint *ep;
   1243       1.12  augustss 	int i;
   1244       1.12  augustss 
   1245      1.169     skrll 	for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
   1246      1.169     skrll 		ep = &iface->ui_endpoints[i];
   1247      1.169     skrll 		if (ep->ue_edesc->bEndpointAddress == address)
   1248      1.169     skrll 			return iface->ui_endpoints[i].ue_edesc;
   1249       1.12  augustss 	}
   1250      1.169     skrll 	return NULL;
   1251       1.63  augustss }
   1252       1.63  augustss 
   1253       1.63  augustss /*
   1254       1.63  augustss  * usbd_ratecheck() can limit the number of error messages that occurs.
   1255       1.63  augustss  * When a device is unplugged it may take up to 0.25s for the hub driver
   1256      1.169     skrll  * to notice it.  If the driver continuously tries to do I/O operations
   1257       1.63  augustss  * this can generate a large number of messages.
   1258       1.63  augustss  */
   1259       1.63  augustss int
   1260       1.74  augustss usbd_ratecheck(struct timeval *last)
   1261       1.63  augustss {
   1262       1.64  augustss 	static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
   1263       1.63  augustss 
   1264      1.169     skrll 	return ratecheck(last, &errinterval);
   1265       1.89  augustss }
   1266       1.89  augustss 
   1267       1.89  augustss /*
   1268       1.89  augustss  * Search for a vendor/product pair in an array.  The item size is
   1269       1.89  augustss  * given as an argument.
   1270       1.89  augustss  */
   1271       1.90  augustss const struct usb_devno *
   1272       1.90  augustss usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
   1273      1.169     skrll 		 uint16_t vendor, uint16_t product)
   1274       1.89  augustss {
   1275       1.89  augustss 	while (nentries-- > 0) {
   1276      1.169     skrll 		uint16_t tproduct = tbl->ud_product;
   1277       1.93  augustss 		if (tbl->ud_vendor == vendor &&
   1278       1.93  augustss 		    (tproduct == product || tproduct == USB_PRODUCT_ANY))
   1279      1.169     skrll 			return tbl;
   1280       1.95  augustss 		tbl = (const struct usb_devno *)((const char *)tbl + sz);
   1281       1.89  augustss 	}
   1282      1.169     skrll 	return NULL;
   1283       1.12  augustss }
   1284       1.12  augustss 
   1285      1.105  augustss 
   1286      1.105  augustss void
   1287      1.169     skrll usb_desc_iter_init(struct usbd_device *dev, usbd_desc_iter_t *iter)
   1288      1.105  augustss {
   1289      1.105  augustss 	const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
   1290      1.105  augustss 
   1291      1.169     skrll 	iter->cur = (const uByte *)cd;
   1292      1.169     skrll 	iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
   1293      1.105  augustss }
   1294      1.105  augustss 
   1295      1.105  augustss const usb_descriptor_t *
   1296      1.105  augustss usb_desc_iter_next(usbd_desc_iter_t *iter)
   1297      1.105  augustss {
   1298      1.105  augustss 	const usb_descriptor_t *desc;
   1299      1.105  augustss 
   1300      1.105  augustss 	if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
   1301      1.105  augustss 		if (iter->cur != iter->end)
   1302      1.105  augustss 			printf("usb_desc_iter_next: bad descriptor\n");
   1303      1.105  augustss 		return NULL;
   1304      1.105  augustss 	}
   1305      1.108  christos 	desc = (const usb_descriptor_t *)iter->cur;
   1306      1.105  augustss 	if (desc->bLength == 0) {
   1307      1.105  augustss 		printf("usb_desc_iter_next: descriptor length = 0\n");
   1308      1.105  augustss 		return NULL;
   1309      1.105  augustss 	}
   1310      1.105  augustss 	iter->cur += desc->bLength;
   1311      1.105  augustss 	if (iter->cur > iter->end) {
   1312      1.105  augustss 		printf("usb_desc_iter_next: descriptor length too large\n");
   1313      1.105  augustss 		return NULL;
   1314      1.105  augustss 	}
   1315      1.105  augustss 	return desc;
   1316      1.105  augustss }
   1317      1.105  augustss 
   1318      1.105  augustss usbd_status
   1319      1.169     skrll usbd_get_string(struct usbd_device *dev, int si, char *buf)
   1320      1.105  augustss {
   1321      1.115     pavel 	return usbd_get_string0(dev, si, buf, 1);
   1322      1.115     pavel }
   1323      1.115     pavel 
   1324      1.115     pavel usbd_status
   1325      1.169     skrll usbd_get_string0(struct usbd_device *dev, int si, char *buf, int unicode)
   1326      1.115     pavel {
   1327      1.169     skrll 	int swap = dev->ud_quirks->uq_flags & UQ_SWAP_UNICODE;
   1328      1.105  augustss 	usb_string_descriptor_t us;
   1329      1.105  augustss 	char *s;
   1330      1.116  macallan 	int i, n;
   1331      1.169     skrll 	uint16_t c;
   1332      1.105  augustss 	usbd_status err;
   1333      1.105  augustss 	int size;
   1334      1.105  augustss 
   1335      1.162     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1336      1.162     skrll 
   1337      1.105  augustss 	buf[0] = '\0';
   1338      1.105  augustss 	if (si == 0)
   1339      1.169     skrll 		return USBD_INVAL;
   1340      1.169     skrll 	if (dev->ud_quirks->uq_flags & UQ_NO_STRINGS)
   1341      1.169     skrll 		return USBD_STALLED;
   1342      1.169     skrll 	if (dev->ud_langid == USBD_NOLANG) {
   1343      1.105  augustss 		/* Set up default language */
   1344      1.105  augustss 		err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
   1345      1.105  augustss 		    &size);
   1346      1.105  augustss 		if (err || size < 4) {
   1347      1.162     skrll 			USBHIST_LOG(usbdebug, "getting lang failed, using 0",
   1348      1.162     skrll 			    0, 0, 0, 0);
   1349      1.169     skrll 			dev->ud_langid = 0; /* Well, just pick something then */
   1350      1.105  augustss 		} else {
   1351      1.105  augustss 			/* Pick the first language as the default. */
   1352      1.169     skrll 			dev->ud_langid = UGETW(us.bString[0]);
   1353      1.105  augustss 		}
   1354      1.105  augustss 	}
   1355      1.169     skrll 	err = usbd_get_string_desc(dev, si, dev->ud_langid, &us, &size);
   1356      1.105  augustss 	if (err)
   1357      1.169     skrll 		return err;
   1358      1.105  augustss 	s = buf;
   1359      1.105  augustss 	n = size / 2 - 1;
   1360      1.115     pavel 	if (unicode) {
   1361      1.115     pavel 		for (i = 0; i < n; i++) {
   1362      1.115     pavel 			c = UGETW(us.bString[i]);
   1363      1.115     pavel 			if (swap)
   1364      1.115     pavel 				c = (c >> 8) | (c << 8);
   1365      1.119  drochner 			s += wput_utf8(s, 3, c);
   1366      1.115     pavel 		}
   1367      1.115     pavel 		*s++ = 0;
   1368      1.115     pavel 	}
   1369      1.115     pavel #ifdef COMPAT_30
   1370      1.115     pavel 	else {
   1371      1.119  drochner 		for (i = 0; i < n; i++) {
   1372      1.115     pavel 			c = UGETW(us.bString[i]);
   1373      1.115     pavel 			if (swap)
   1374      1.115     pavel 				c = (c >> 8) | (c << 8);
   1375      1.119  drochner 			*s++ = (c < 0x80) ? c : '?';
   1376      1.107  augustss 		}
   1377      1.119  drochner 		*s++ = 0;
   1378      1.105  augustss 	}
   1379      1.115     pavel #endif
   1380      1.169     skrll 	return USBD_NORMAL_COMPLETION;
   1381      1.105  augustss }
   1382  1.182.4.3    martin 
   1383  1.182.4.3    martin /*
   1384  1.182.4.3    martin  * usbd_xfer_trycomplete(xfer)
   1385  1.182.4.3    martin  *
   1386  1.182.4.3    martin  *	Try to claim xfer for completion.  Return true if successful,
   1387  1.182.4.3    martin  *	false if the xfer has been synchronously aborted or has timed
   1388  1.182.4.3    martin  *	out.
   1389  1.182.4.3    martin  *
   1390  1.182.4.3    martin  *	If this returns true, caller is responsible for setting
   1391  1.182.4.3    martin  *	xfer->ux_status and calling usb_transfer_complete.  To be used
   1392  1.182.4.3    martin  *	in a host controller interrupt handler.
   1393  1.182.4.3    martin  *
   1394  1.182.4.3    martin  *	Caller must either hold the bus lock or have the bus in polling
   1395  1.182.4.3    martin  *	mode.
   1396  1.182.4.3    martin  */
   1397  1.182.4.3    martin bool
   1398  1.182.4.3    martin usbd_xfer_trycomplete(struct usbd_xfer *xfer)
   1399  1.182.4.3    martin {
   1400  1.182.4.3    martin 	struct usbd_bus *bus __diagused = xfer->ux_bus;
   1401  1.182.4.3    martin 
   1402  1.182.4.3    martin 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
   1403  1.182.4.3    martin 
   1404  1.182.4.3    martin 	/*
   1405  1.182.4.3    martin 	 * If software has completed it, either by synchronous abort or
   1406  1.182.4.3    martin 	 * by timeout, too late.
   1407  1.182.4.3    martin 	 */
   1408  1.182.4.3    martin 	if (xfer->ux_status != USBD_IN_PROGRESS)
   1409  1.182.4.3    martin 		return false;
   1410  1.182.4.3    martin 
   1411  1.182.4.3    martin 	/*
   1412  1.182.4.3    martin 	 * We are completing the xfer.  Cancel the timeout if we can,
   1413  1.182.4.3    martin 	 * but only asynchronously.  See usbd_xfer_cancel_timeout_async
   1414  1.182.4.3    martin 	 * for why we need not wait for the callout or task here.
   1415  1.182.4.3    martin 	 */
   1416  1.182.4.3    martin 	usbd_xfer_cancel_timeout_async(xfer);
   1417  1.182.4.3    martin 
   1418  1.182.4.3    martin 	/* Success!  Note: Caller must set xfer->ux_status afterwar.  */
   1419  1.182.4.3    martin 	return true;
   1420  1.182.4.3    martin }
   1421  1.182.4.3    martin 
   1422  1.182.4.3    martin /*
   1423  1.182.4.3    martin  * usbd_xfer_abort(xfer)
   1424  1.182.4.3    martin  *
   1425  1.182.4.3    martin  *	Try to claim xfer to abort.  If successful, mark it completed
   1426  1.182.4.3    martin  *	with USBD_CANCELLED and call the bus-specific method to abort
   1427  1.182.4.3    martin  *	at the hardware level.
   1428  1.182.4.3    martin  *
   1429  1.182.4.3    martin  *	To be called in thread context from struct
   1430  1.182.4.3    martin  *	usbd_pipe_methods::upm_abort.
   1431  1.182.4.3    martin  *
   1432  1.182.4.3    martin  *	Caller must hold the bus lock.
   1433  1.182.4.3    martin  */
   1434  1.182.4.3    martin void
   1435  1.182.4.3    martin usbd_xfer_abort(struct usbd_xfer *xfer)
   1436  1.182.4.3    martin {
   1437  1.182.4.3    martin 	struct usbd_bus *bus = xfer->ux_bus;
   1438  1.182.4.3    martin 
   1439  1.182.4.3    martin 	KASSERT(mutex_owned(bus->ub_lock));
   1440  1.182.4.3    martin 
   1441  1.182.4.3    martin 	/*
   1442  1.182.4.3    martin 	 * If host controller interrupt or timer interrupt has
   1443  1.182.4.3    martin 	 * completed it, too late.  But the xfer cannot be
   1444  1.182.4.3    martin 	 * cancelled already -- only one caller can synchronously
   1445  1.182.4.3    martin 	 * abort.
   1446  1.182.4.3    martin 	 */
   1447  1.182.4.3    martin 	KASSERT(xfer->ux_status != USBD_CANCELLED);
   1448  1.182.4.3    martin 	if (xfer->ux_status != USBD_IN_PROGRESS)
   1449  1.182.4.3    martin 		return;
   1450  1.182.4.3    martin 
   1451  1.182.4.3    martin 	/*
   1452  1.182.4.3    martin 	 * Cancel the timeout if we can, but only asynchronously; see
   1453  1.182.4.3    martin 	 * usbd_xfer_cancel_timeout_async for why we need not wait for
   1454  1.182.4.3    martin 	 * the callout or task here.
   1455  1.182.4.3    martin 	 */
   1456  1.182.4.3    martin 	usbd_xfer_cancel_timeout_async(xfer);
   1457  1.182.4.3    martin 
   1458  1.182.4.3    martin 	/*
   1459  1.182.4.3    martin 	 * We beat everyone else.  Claim the status as cancelled and do
   1460  1.182.4.3    martin 	 * the bus-specific dance to abort the hardware.
   1461  1.182.4.3    martin 	 */
   1462  1.182.4.3    martin 	xfer->ux_status = USBD_CANCELLED;
   1463  1.182.4.3    martin 	bus->ub_methods->ubm_abortx(xfer);
   1464  1.182.4.3    martin }
   1465  1.182.4.3    martin 
   1466  1.182.4.3    martin /*
   1467  1.182.4.3    martin  * usbd_xfer_timeout(xfer)
   1468  1.182.4.3    martin  *
   1469  1.182.4.3    martin  *	Called at IPL_SOFTCLOCK when too much time has elapsed waiting
   1470  1.182.4.3    martin  *	for xfer to complete.  Since we can't abort the xfer at
   1471  1.182.4.3    martin  *	IPL_SOFTCLOCK, defer to a usb_task to run it in thread context,
   1472  1.182.4.3    martin  *	unless the xfer has completed or aborted concurrently -- and if
   1473  1.182.4.3    martin  *	the xfer has also been resubmitted, take care of rescheduling
   1474  1.182.4.3    martin  *	the callout.
   1475  1.182.4.3    martin  */
   1476  1.182.4.3    martin static void
   1477  1.182.4.3    martin usbd_xfer_timeout(void *cookie)
   1478  1.182.4.3    martin {
   1479  1.182.4.3    martin 	struct usbd_xfer *xfer = cookie;
   1480  1.182.4.3    martin 	struct usbd_bus *bus = xfer->ux_bus;
   1481  1.182.4.3    martin 	struct usbd_device *dev = xfer->ux_pipe->up_dev;
   1482  1.182.4.3    martin 
   1483  1.182.4.3    martin 	/* Acquire the lock so we can transition the timeout state.  */
   1484  1.182.4.3    martin 	mutex_enter(bus->ub_lock);
   1485  1.182.4.3    martin 
   1486  1.182.4.3    martin 	/*
   1487  1.182.4.3    martin 	 * Use usbd_xfer_probe_timeout to check whether the timeout is
   1488  1.182.4.3    martin 	 * still valid, or to reschedule the callout if necessary.  If
   1489  1.182.4.3    martin 	 * it is still valid, schedule the task.
   1490  1.182.4.3    martin 	 */
   1491  1.182.4.3    martin 	if (usbd_xfer_probe_timeout(xfer))
   1492  1.182.4.3    martin 		usb_add_task(dev, &xfer->ux_aborttask, USB_TASKQ_HC);
   1493  1.182.4.3    martin 
   1494  1.182.4.3    martin 	/*
   1495  1.182.4.3    martin 	 * Notify usbd_xfer_cancel_timeout_async that we may have
   1496  1.182.4.3    martin 	 * scheduled the task.  This causes callout_invoking to return
   1497  1.182.4.3    martin 	 * false in usbd_xfer_cancel_timeout_async so that it can tell
   1498  1.182.4.3    martin 	 * which stage in the callout->task->abort process we're at.
   1499  1.182.4.3    martin 	 */
   1500  1.182.4.3    martin 	callout_ack(&xfer->ux_callout);
   1501  1.182.4.3    martin 
   1502  1.182.4.3    martin 	/* All done -- release the lock.  */
   1503  1.182.4.3    martin 	mutex_exit(bus->ub_lock);
   1504  1.182.4.3    martin }
   1505  1.182.4.3    martin 
   1506  1.182.4.3    martin /*
   1507  1.182.4.3    martin  * usbd_xfer_timeout_task(xfer)
   1508  1.182.4.3    martin  *
   1509  1.182.4.3    martin  *	Called in thread context when too much time has elapsed waiting
   1510  1.182.4.3    martin  *	for xfer to complete.  Abort the xfer with USBD_TIMEOUT, unless
   1511  1.182.4.3    martin  *	it has completed or aborted concurrently -- and if the xfer has
   1512  1.182.4.3    martin  *	also been resubmitted, take care of rescheduling the callout.
   1513  1.182.4.3    martin  */
   1514  1.182.4.3    martin static void
   1515  1.182.4.3    martin usbd_xfer_timeout_task(void *cookie)
   1516  1.182.4.3    martin {
   1517  1.182.4.3    martin 	struct usbd_xfer *xfer = cookie;
   1518  1.182.4.3    martin 	struct usbd_bus *bus = xfer->ux_bus;
   1519  1.182.4.3    martin 
   1520  1.182.4.3    martin 	/* Acquire the lock so we can transition the timeout state.  */
   1521  1.182.4.3    martin 	mutex_enter(bus->ub_lock);
   1522  1.182.4.3    martin 
   1523  1.182.4.3    martin 	/*
   1524  1.182.4.3    martin 	 * Use usbd_xfer_probe_timeout to check whether the timeout is
   1525  1.182.4.3    martin 	 * still valid, or to reschedule the callout if necessary.  If
   1526  1.182.4.3    martin 	 * it is not valid -- the timeout has been asynchronously
   1527  1.182.4.3    martin 	 * cancelled, or the xfer has already been resubmitted -- then
   1528  1.182.4.3    martin 	 * we're done here.
   1529  1.182.4.3    martin 	 */
   1530  1.182.4.3    martin 	if (!usbd_xfer_probe_timeout(xfer))
   1531  1.182.4.3    martin 		goto out;
   1532  1.182.4.3    martin 
   1533  1.182.4.3    martin 	/*
   1534  1.182.4.3    martin 	 * May have completed or been aborted, but we're the only one
   1535  1.182.4.3    martin 	 * who can time it out.  If it has completed or been aborted,
   1536  1.182.4.3    martin 	 * no need to timeout.
   1537  1.182.4.3    martin 	 */
   1538  1.182.4.3    martin 	KASSERT(xfer->ux_status != USBD_TIMEOUT);
   1539  1.182.4.3    martin 	if (xfer->ux_status != USBD_IN_PROGRESS)
   1540  1.182.4.3    martin 		goto out;
   1541  1.182.4.3    martin 
   1542  1.182.4.3    martin 	/*
   1543  1.182.4.3    martin 	 * We beat everyone else.  Claim the status as timed out and do
   1544  1.182.4.3    martin 	 * the bus-specific dance to abort the hardware.
   1545  1.182.4.3    martin 	 */
   1546  1.182.4.3    martin 	xfer->ux_status = USBD_TIMEOUT;
   1547  1.182.4.3    martin 	bus->ub_methods->ubm_abortx(xfer);
   1548  1.182.4.3    martin 
   1549  1.182.4.3    martin out:	/* All done -- release the lock.  */
   1550  1.182.4.3    martin 	mutex_exit(bus->ub_lock);
   1551  1.182.4.3    martin }
   1552  1.182.4.3    martin 
   1553  1.182.4.3    martin /*
   1554  1.182.4.3    martin  * usbd_xfer_probe_timeout(xfer)
   1555  1.182.4.3    martin  *
   1556  1.182.4.3    martin  *	Probe the status of xfer's timeout.  Acknowledge and process a
   1557  1.182.4.3    martin  *	request to reschedule.  Return true if the timeout is still
   1558  1.182.4.3    martin  *	valid and the caller should take further action (queueing a
   1559  1.182.4.3    martin  *	task or aborting the xfer), false if it must stop here.
   1560  1.182.4.3    martin  */
   1561  1.182.4.3    martin static bool
   1562  1.182.4.3    martin usbd_xfer_probe_timeout(struct usbd_xfer *xfer)
   1563  1.182.4.3    martin {
   1564  1.182.4.3    martin 	struct usbd_bus *bus = xfer->ux_bus;
   1565  1.182.4.3    martin 	bool valid;
   1566  1.182.4.3    martin 
   1567  1.182.4.3    martin 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
   1568  1.182.4.3    martin 
   1569  1.182.4.3    martin 	/* The timeout must be set.  */
   1570  1.182.4.3    martin 	KASSERT(xfer->ux_timeout_set);
   1571  1.182.4.3    martin 
   1572  1.182.4.3    martin 	/*
   1573  1.182.4.3    martin 	 * Neither callout nor task may be pending; they execute
   1574  1.182.4.3    martin 	 * alternately in lock step.
   1575  1.182.4.3    martin 	 */
   1576  1.182.4.3    martin 	KASSERT(!callout_pending(&xfer->ux_callout));
   1577  1.182.4.3    martin 	KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
   1578  1.182.4.3    martin 
   1579  1.182.4.3    martin 	/* There are a few cases... */
   1580  1.182.4.3    martin 	if (bus->ub_methods->ubm_dying(bus)) {
   1581  1.182.4.3    martin 		/* Host controller dying.  Drop it all on the floor.  */
   1582  1.182.4.3    martin 		xfer->ux_timeout_set = false;
   1583  1.182.4.3    martin 		xfer->ux_timeout_reset = false;
   1584  1.182.4.3    martin 		valid = false;
   1585  1.182.4.3    martin 	} else if (xfer->ux_timeout_reset) {
   1586  1.182.4.3    martin 		/*
   1587  1.182.4.3    martin 		 * The xfer completed _and_ got resubmitted while we
   1588  1.182.4.3    martin 		 * waited for the lock.  Acknowledge the request to
   1589  1.182.4.3    martin 		 * reschedule, and reschedule it if there is a timeout
   1590  1.182.4.3    martin 		 * and the bus is not polling.
   1591  1.182.4.3    martin 		 */
   1592  1.182.4.3    martin 		xfer->ux_timeout_reset = false;
   1593  1.182.4.3    martin 		if (xfer->ux_timeout && !bus->ub_usepolling) {
   1594  1.182.4.3    martin 			KASSERT(xfer->ux_timeout_set);
   1595  1.182.4.3    martin 			callout_schedule(&xfer->ux_callout,
   1596  1.182.4.3    martin 			    mstohz(xfer->ux_timeout));
   1597  1.182.4.3    martin 		} else {
   1598  1.182.4.3    martin 			/* No more callout or task scheduled.  */
   1599  1.182.4.3    martin 			xfer->ux_timeout_set = false;
   1600  1.182.4.3    martin 		}
   1601  1.182.4.3    martin 		valid = false;
   1602  1.182.4.3    martin 	} else if (xfer->ux_status != USBD_IN_PROGRESS) {
   1603  1.182.4.3    martin 		/*
   1604  1.182.4.3    martin 		 * The xfer has completed by hardware completion or by
   1605  1.182.4.3    martin 		 * software abort, and has not been resubmitted, so the
   1606  1.182.4.3    martin 		 * timeout must be unset, and is no longer valid for
   1607  1.182.4.3    martin 		 * the caller.
   1608  1.182.4.3    martin 		 */
   1609  1.182.4.3    martin 		xfer->ux_timeout_set = false;
   1610  1.182.4.3    martin 		valid = false;
   1611  1.182.4.3    martin 	} else {
   1612  1.182.4.3    martin 		/*
   1613  1.182.4.3    martin 		 * The xfer has not yet completed, so the timeout is
   1614  1.182.4.3    martin 		 * valid.
   1615  1.182.4.3    martin 		 */
   1616  1.182.4.3    martin 		valid = true;
   1617  1.182.4.3    martin 	}
   1618  1.182.4.3    martin 
   1619  1.182.4.3    martin 	/* Any reset must have been processed.  */
   1620  1.182.4.3    martin 	KASSERT(!xfer->ux_timeout_reset);
   1621  1.182.4.3    martin 
   1622  1.182.4.3    martin 	/*
   1623  1.182.4.3    martin 	 * Either we claim the timeout is set, or the callout is idle.
   1624  1.182.4.3    martin 	 * If the timeout is still set, we may be handing off to the
   1625  1.182.4.3    martin 	 * task instead, so this is an if but not an iff.
   1626  1.182.4.3    martin 	 */
   1627  1.182.4.3    martin 	KASSERT(xfer->ux_timeout_set || !callout_pending(&xfer->ux_callout));
   1628  1.182.4.3    martin 
   1629  1.182.4.3    martin 	/*
   1630  1.182.4.3    martin 	 * The task must be idle now.
   1631  1.182.4.3    martin 	 *
   1632  1.182.4.3    martin 	 * - If the caller is the callout, _and_ the timeout is still
   1633  1.182.4.3    martin 	 *   valid, the caller will schedule it, but it hasn't been
   1634  1.182.4.3    martin 	 *   scheduled yet.  (If the timeout is not valid, the task
   1635  1.182.4.3    martin 	 *   should not be scheduled.)
   1636  1.182.4.3    martin 	 *
   1637  1.182.4.3    martin 	 * - If the caller is the task, it cannot be scheduled again
   1638  1.182.4.3    martin 	 *   until the callout runs again, which won't happen until we
   1639  1.182.4.3    martin 	 *   next release the lock.
   1640  1.182.4.3    martin 	 */
   1641  1.182.4.3    martin 	KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
   1642  1.182.4.3    martin 
   1643  1.182.4.3    martin 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
   1644  1.182.4.3    martin 
   1645  1.182.4.3    martin 	return valid;
   1646  1.182.4.3    martin }
   1647  1.182.4.3    martin 
   1648  1.182.4.3    martin /*
   1649  1.182.4.3    martin  * usbd_xfer_schedule_timeout(xfer)
   1650  1.182.4.3    martin  *
   1651  1.182.4.3    martin  *	Ensure that xfer has a timeout.  If the callout is already
   1652  1.182.4.3    martin  *	queued or the task is already running, request that they
   1653  1.182.4.3    martin  *	reschedule the callout.  If not, and if we're not polling,
   1654  1.182.4.3    martin  *	schedule the callout anew.
   1655  1.182.4.3    martin  *
   1656  1.182.4.3    martin  *	To be called in thread context from struct
   1657  1.182.4.3    martin  *	usbd_pipe_methods::upm_start.
   1658  1.182.4.3    martin  */
   1659  1.182.4.3    martin void
   1660  1.182.4.3    martin usbd_xfer_schedule_timeout(struct usbd_xfer *xfer)
   1661  1.182.4.3    martin {
   1662  1.182.4.3    martin 	struct usbd_bus *bus = xfer->ux_bus;
   1663  1.182.4.3    martin 
   1664  1.182.4.3    martin 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
   1665  1.182.4.3    martin 
   1666  1.182.4.3    martin 	if (xfer->ux_timeout_set) {
   1667  1.182.4.3    martin 		/*
   1668  1.182.4.3    martin 		 * Callout or task has fired from a prior completed
   1669  1.182.4.3    martin 		 * xfer but has not yet noticed that the xfer is done.
   1670  1.182.4.3    martin 		 * Ask it to reschedule itself to ux_timeout.
   1671  1.182.4.3    martin 		 */
   1672  1.182.4.3    martin 		xfer->ux_timeout_reset = true;
   1673  1.182.4.3    martin 	} else if (xfer->ux_timeout && !bus->ub_usepolling) {
   1674  1.182.4.3    martin 		/* Callout is not scheduled.  Schedule it.  */
   1675  1.182.4.3    martin 		KASSERT(!callout_pending(&xfer->ux_callout));
   1676  1.182.4.3    martin 		callout_schedule(&xfer->ux_callout, mstohz(xfer->ux_timeout));
   1677  1.182.4.3    martin 		xfer->ux_timeout_set = true;
   1678  1.182.4.3    martin 	}
   1679  1.182.4.3    martin 
   1680  1.182.4.3    martin 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
   1681  1.182.4.3    martin }
   1682  1.182.4.3    martin 
   1683  1.182.4.3    martin /*
   1684  1.182.4.3    martin  * usbd_xfer_cancel_timeout_async(xfer)
   1685  1.182.4.3    martin  *
   1686  1.182.4.3    martin  *	Cancel the callout and the task of xfer, which have not yet run
   1687  1.182.4.3    martin  *	to completion, but don't wait for the callout or task to finish
   1688  1.182.4.3    martin  *	running.
   1689  1.182.4.3    martin  *
   1690  1.182.4.3    martin  *	If they have already fired, at worst they are waiting for the
   1691  1.182.4.3    martin  *	bus lock.  They will see that the xfer is no longer in progress
   1692  1.182.4.3    martin  *	and give up, or they will see that the xfer has been
   1693  1.182.4.3    martin  *	resubmitted with a new timeout and reschedule the callout.
   1694  1.182.4.3    martin  *
   1695  1.182.4.3    martin  *	If a resubmitted request completed so fast that the callout
   1696  1.182.4.3    martin  *	didn't have time to process a timer reset, just cancel the
   1697  1.182.4.3    martin  *	timer reset.
   1698  1.182.4.3    martin  */
   1699  1.182.4.3    martin static void
   1700  1.182.4.3    martin usbd_xfer_cancel_timeout_async(struct usbd_xfer *xfer)
   1701  1.182.4.3    martin {
   1702  1.182.4.3    martin 	struct usbd_bus *bus __diagused = xfer->ux_bus;
   1703  1.182.4.3    martin 
   1704  1.182.4.3    martin 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
   1705  1.182.4.3    martin 
   1706  1.182.4.3    martin 	/*
   1707  1.182.4.3    martin 	 * If the timer wasn't running anyway, forget about it.  This
   1708  1.182.4.3    martin 	 * can happen if we are completing an isochronous transfer
   1709  1.182.4.3    martin 	 * which doesn't use the same timeout logic.
   1710  1.182.4.3    martin 	 */
   1711  1.182.4.3    martin 	if (!xfer->ux_timeout_set)
   1712  1.182.4.3    martin 		return;
   1713  1.182.4.3    martin 
   1714  1.182.4.3    martin 	xfer->ux_timeout_reset = false;
   1715  1.182.4.3    martin 	if (!callout_stop(&xfer->ux_callout)) {
   1716  1.182.4.3    martin 		/*
   1717  1.182.4.3    martin 		 * We stopped the callout before it ran.  The timeout
   1718  1.182.4.3    martin 		 * is no longer set.
   1719  1.182.4.3    martin 		 */
   1720  1.182.4.3    martin 		xfer->ux_timeout_set = false;
   1721  1.182.4.3    martin 	} else if (callout_invoking(&xfer->ux_callout)) {
   1722  1.182.4.3    martin 		/*
   1723  1.182.4.3    martin 		 * The callout has begun to run but it has not yet
   1724  1.182.4.3    martin 		 * acquired the lock and called callout_ack.  The task
   1725  1.182.4.3    martin 		 * cannot be queued yet, and the callout cannot have
   1726  1.182.4.3    martin 		 * been rescheduled yet.
   1727  1.182.4.3    martin 		 *
   1728  1.182.4.3    martin 		 * By the time the callout acquires the lock, we will
   1729  1.182.4.3    martin 		 * have transitioned from USBD_IN_PROGRESS to a
   1730  1.182.4.3    martin 		 * completed status, and possibly also resubmitted the
   1731  1.182.4.3    martin 		 * xfer and set xfer->ux_timeout_reset = true.  In both
   1732  1.182.4.3    martin 		 * cases, the callout will DTRT, so no further action
   1733  1.182.4.3    martin 		 * is needed here.
   1734  1.182.4.3    martin 		 */
   1735  1.182.4.3    martin 	} else if (usb_rem_task(xfer->ux_pipe->up_dev, &xfer->ux_aborttask)) {
   1736  1.182.4.3    martin 		/*
   1737  1.182.4.3    martin 		 * The callout had fired and scheduled the task, but we
   1738  1.182.4.3    martin 		 * stopped the task before it could run.  The timeout
   1739  1.182.4.3    martin 		 * is therefore no longer set -- the next resubmission
   1740  1.182.4.3    martin 		 * of the xfer must schedule a new timeout.
   1741  1.182.4.3    martin 		 *
   1742  1.182.4.3    martin 		 * The callout should not be be pending at this point:
   1743  1.182.4.3    martin 		 * it is scheduled only under the lock, and only when
   1744  1.182.4.3    martin 		 * xfer->ux_timeout_set is false, or by the callout or
   1745  1.182.4.3    martin 		 * task itself when xfer->ux_timeout_reset is true.
   1746  1.182.4.3    martin 		 */
   1747  1.182.4.3    martin 		xfer->ux_timeout_set = false;
   1748  1.182.4.3    martin 	}
   1749  1.182.4.3    martin 
   1750  1.182.4.3    martin 	/*
   1751  1.182.4.3    martin 	 * The callout cannot be scheduled and the task cannot be
   1752  1.182.4.3    martin 	 * queued at this point.  Either we cancelled them, or they are
   1753  1.182.4.3    martin 	 * already running and waiting for the bus lock.
   1754  1.182.4.3    martin 	 */
   1755  1.182.4.3    martin 	KASSERT(!callout_pending(&xfer->ux_callout));
   1756  1.182.4.3    martin 	KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
   1757  1.182.4.3    martin 
   1758  1.182.4.3    martin 	KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
   1759  1.182.4.3    martin }
   1760