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