18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Released under the GPLv2 only. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/module.h> 78c2ecf20Sopenharmony_ci#include <linux/string.h> 88c2ecf20Sopenharmony_ci#include <linux/bitops.h> 98c2ecf20Sopenharmony_ci#include <linux/slab.h> 108c2ecf20Sopenharmony_ci#include <linux/log2.h> 118c2ecf20Sopenharmony_ci#include <linux/usb.h> 128c2ecf20Sopenharmony_ci#include <linux/wait.h> 138c2ecf20Sopenharmony_ci#include <linux/usb/hcd.h> 148c2ecf20Sopenharmony_ci#include <linux/scatterlist.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#define to_urb(d) container_of(d, struct urb, kref) 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_cistatic void urb_destroy(struct kref *kref) 208c2ecf20Sopenharmony_ci{ 218c2ecf20Sopenharmony_ci struct urb *urb = to_urb(kref); 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci if (urb->transfer_flags & URB_FREE_BUFFER) 248c2ecf20Sopenharmony_ci kfree(urb->transfer_buffer); 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci kfree(urb); 278c2ecf20Sopenharmony_ci} 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci/** 308c2ecf20Sopenharmony_ci * usb_init_urb - initializes a urb so that it can be used by a USB driver 318c2ecf20Sopenharmony_ci * @urb: pointer to the urb to initialize 328c2ecf20Sopenharmony_ci * 338c2ecf20Sopenharmony_ci * Initializes a urb so that the USB subsystem can use it properly. 348c2ecf20Sopenharmony_ci * 358c2ecf20Sopenharmony_ci * If a urb is created with a call to usb_alloc_urb() it is not 368c2ecf20Sopenharmony_ci * necessary to call this function. Only use this if you allocate the 378c2ecf20Sopenharmony_ci * space for a struct urb on your own. If you call this function, be 388c2ecf20Sopenharmony_ci * careful when freeing the memory for your urb that it is no longer in 398c2ecf20Sopenharmony_ci * use by the USB core. 408c2ecf20Sopenharmony_ci * 418c2ecf20Sopenharmony_ci * Only use this function if you _really_ understand what you are doing. 428c2ecf20Sopenharmony_ci */ 438c2ecf20Sopenharmony_civoid usb_init_urb(struct urb *urb) 448c2ecf20Sopenharmony_ci{ 458c2ecf20Sopenharmony_ci if (urb) { 468c2ecf20Sopenharmony_ci memset(urb, 0, sizeof(*urb)); 478c2ecf20Sopenharmony_ci kref_init(&urb->kref); 488c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&urb->urb_list); 498c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&urb->anchor_list); 508c2ecf20Sopenharmony_ci } 518c2ecf20Sopenharmony_ci} 528c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_init_urb); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci/** 558c2ecf20Sopenharmony_ci * usb_alloc_urb - creates a new urb for a USB driver to use 568c2ecf20Sopenharmony_ci * @iso_packets: number of iso packets for this urb 578c2ecf20Sopenharmony_ci * @mem_flags: the type of memory to allocate, see kmalloc() for a list of 588c2ecf20Sopenharmony_ci * valid options for this. 598c2ecf20Sopenharmony_ci * 608c2ecf20Sopenharmony_ci * Creates an urb for the USB driver to use, initializes a few internal 618c2ecf20Sopenharmony_ci * structures, increments the usage counter, and returns a pointer to it. 628c2ecf20Sopenharmony_ci * 638c2ecf20Sopenharmony_ci * If the driver want to use this urb for interrupt, control, or bulk 648c2ecf20Sopenharmony_ci * endpoints, pass '0' as the number of iso packets. 658c2ecf20Sopenharmony_ci * 668c2ecf20Sopenharmony_ci * The driver must call usb_free_urb() when it is finished with the urb. 678c2ecf20Sopenharmony_ci * 688c2ecf20Sopenharmony_ci * Return: A pointer to the new urb, or %NULL if no memory is available. 698c2ecf20Sopenharmony_ci */ 708c2ecf20Sopenharmony_cistruct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci struct urb *urb; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci urb = kmalloc(struct_size(urb, iso_frame_desc, iso_packets), 758c2ecf20Sopenharmony_ci mem_flags); 768c2ecf20Sopenharmony_ci if (!urb) 778c2ecf20Sopenharmony_ci return NULL; 788c2ecf20Sopenharmony_ci usb_init_urb(urb); 798c2ecf20Sopenharmony_ci return urb; 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_alloc_urb); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci/** 848c2ecf20Sopenharmony_ci * usb_free_urb - frees the memory used by a urb when all users of it are finished 858c2ecf20Sopenharmony_ci * @urb: pointer to the urb to free, may be NULL 868c2ecf20Sopenharmony_ci * 878c2ecf20Sopenharmony_ci * Must be called when a user of a urb is finished with it. When the last user 888c2ecf20Sopenharmony_ci * of the urb calls this function, the memory of the urb is freed. 898c2ecf20Sopenharmony_ci * 908c2ecf20Sopenharmony_ci * Note: The transfer buffer associated with the urb is not freed unless the 918c2ecf20Sopenharmony_ci * URB_FREE_BUFFER transfer flag is set. 928c2ecf20Sopenharmony_ci */ 938c2ecf20Sopenharmony_civoid usb_free_urb(struct urb *urb) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci if (urb) 968c2ecf20Sopenharmony_ci kref_put(&urb->kref, urb_destroy); 978c2ecf20Sopenharmony_ci} 988c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_free_urb); 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci/** 1018c2ecf20Sopenharmony_ci * usb_get_urb - increments the reference count of the urb 1028c2ecf20Sopenharmony_ci * @urb: pointer to the urb to modify, may be NULL 1038c2ecf20Sopenharmony_ci * 1048c2ecf20Sopenharmony_ci * This must be called whenever a urb is transferred from a device driver to a 1058c2ecf20Sopenharmony_ci * host controller driver. This allows proper reference counting to happen 1068c2ecf20Sopenharmony_ci * for urbs. 1078c2ecf20Sopenharmony_ci * 1088c2ecf20Sopenharmony_ci * Return: A pointer to the urb with the incremented reference counter. 1098c2ecf20Sopenharmony_ci */ 1108c2ecf20Sopenharmony_cistruct urb *usb_get_urb(struct urb *urb) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci if (urb) 1138c2ecf20Sopenharmony_ci kref_get(&urb->kref); 1148c2ecf20Sopenharmony_ci return urb; 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_get_urb); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci/** 1198c2ecf20Sopenharmony_ci * usb_anchor_urb - anchors an URB while it is processed 1208c2ecf20Sopenharmony_ci * @urb: pointer to the urb to anchor 1218c2ecf20Sopenharmony_ci * @anchor: pointer to the anchor 1228c2ecf20Sopenharmony_ci * 1238c2ecf20Sopenharmony_ci * This can be called to have access to URBs which are to be executed 1248c2ecf20Sopenharmony_ci * without bothering to track them 1258c2ecf20Sopenharmony_ci */ 1268c2ecf20Sopenharmony_civoid usb_anchor_urb(struct urb *urb, struct usb_anchor *anchor) 1278c2ecf20Sopenharmony_ci{ 1288c2ecf20Sopenharmony_ci unsigned long flags; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci spin_lock_irqsave(&anchor->lock, flags); 1318c2ecf20Sopenharmony_ci usb_get_urb(urb); 1328c2ecf20Sopenharmony_ci list_add_tail(&urb->anchor_list, &anchor->urb_list); 1338c2ecf20Sopenharmony_ci urb->anchor = anchor; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci if (unlikely(anchor->poisoned)) 1368c2ecf20Sopenharmony_ci atomic_inc(&urb->reject); 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&anchor->lock, flags); 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_anchor_urb); 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_cistatic int usb_anchor_check_wakeup(struct usb_anchor *anchor) 1438c2ecf20Sopenharmony_ci{ 1448c2ecf20Sopenharmony_ci return atomic_read(&anchor->suspend_wakeups) == 0 && 1458c2ecf20Sopenharmony_ci list_empty(&anchor->urb_list); 1468c2ecf20Sopenharmony_ci} 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci/* Callers must hold anchor->lock */ 1498c2ecf20Sopenharmony_cistatic void __usb_unanchor_urb(struct urb *urb, struct usb_anchor *anchor) 1508c2ecf20Sopenharmony_ci{ 1518c2ecf20Sopenharmony_ci urb->anchor = NULL; 1528c2ecf20Sopenharmony_ci list_del(&urb->anchor_list); 1538c2ecf20Sopenharmony_ci usb_put_urb(urb); 1548c2ecf20Sopenharmony_ci if (usb_anchor_check_wakeup(anchor)) 1558c2ecf20Sopenharmony_ci wake_up(&anchor->wait); 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci/** 1598c2ecf20Sopenharmony_ci * usb_unanchor_urb - unanchors an URB 1608c2ecf20Sopenharmony_ci * @urb: pointer to the urb to anchor 1618c2ecf20Sopenharmony_ci * 1628c2ecf20Sopenharmony_ci * Call this to stop the system keeping track of this URB 1638c2ecf20Sopenharmony_ci */ 1648c2ecf20Sopenharmony_civoid usb_unanchor_urb(struct urb *urb) 1658c2ecf20Sopenharmony_ci{ 1668c2ecf20Sopenharmony_ci unsigned long flags; 1678c2ecf20Sopenharmony_ci struct usb_anchor *anchor; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci if (!urb) 1708c2ecf20Sopenharmony_ci return; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci anchor = urb->anchor; 1738c2ecf20Sopenharmony_ci if (!anchor) 1748c2ecf20Sopenharmony_ci return; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci spin_lock_irqsave(&anchor->lock, flags); 1778c2ecf20Sopenharmony_ci /* 1788c2ecf20Sopenharmony_ci * At this point, we could be competing with another thread which 1798c2ecf20Sopenharmony_ci * has the same intention. To protect the urb from being unanchored 1808c2ecf20Sopenharmony_ci * twice, only the winner of the race gets the job. 1818c2ecf20Sopenharmony_ci */ 1828c2ecf20Sopenharmony_ci if (likely(anchor == urb->anchor)) 1838c2ecf20Sopenharmony_ci __usb_unanchor_urb(urb, anchor); 1848c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&anchor->lock, flags); 1858c2ecf20Sopenharmony_ci} 1868c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_unanchor_urb); 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------*/ 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cistatic const int pipetypes[4] = { 1918c2ecf20Sopenharmony_ci PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT 1928c2ecf20Sopenharmony_ci}; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci/** 1958c2ecf20Sopenharmony_ci * usb_pipe_type_check - sanity check of a specific pipe for a usb device 1968c2ecf20Sopenharmony_ci * @dev: struct usb_device to be checked 1978c2ecf20Sopenharmony_ci * @pipe: pipe to check 1988c2ecf20Sopenharmony_ci * 1998c2ecf20Sopenharmony_ci * This performs a light-weight sanity check for the endpoint in the 2008c2ecf20Sopenharmony_ci * given usb device. It returns 0 if the pipe is valid for the specific usb 2018c2ecf20Sopenharmony_ci * device, otherwise a negative error code. 2028c2ecf20Sopenharmony_ci */ 2038c2ecf20Sopenharmony_ciint usb_pipe_type_check(struct usb_device *dev, unsigned int pipe) 2048c2ecf20Sopenharmony_ci{ 2058c2ecf20Sopenharmony_ci const struct usb_host_endpoint *ep; 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci ep = usb_pipe_endpoint(dev, pipe); 2088c2ecf20Sopenharmony_ci if (!ep) 2098c2ecf20Sopenharmony_ci return -EINVAL; 2108c2ecf20Sopenharmony_ci if (usb_pipetype(pipe) != pipetypes[usb_endpoint_type(&ep->desc)]) 2118c2ecf20Sopenharmony_ci return -EINVAL; 2128c2ecf20Sopenharmony_ci return 0; 2138c2ecf20Sopenharmony_ci} 2148c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_pipe_type_check); 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci/** 2178c2ecf20Sopenharmony_ci * usb_urb_ep_type_check - sanity check of endpoint in the given urb 2188c2ecf20Sopenharmony_ci * @urb: urb to be checked 2198c2ecf20Sopenharmony_ci * 2208c2ecf20Sopenharmony_ci * This performs a light-weight sanity check for the endpoint in the 2218c2ecf20Sopenharmony_ci * given urb. It returns 0 if the urb contains a valid endpoint, otherwise 2228c2ecf20Sopenharmony_ci * a negative error code. 2238c2ecf20Sopenharmony_ci */ 2248c2ecf20Sopenharmony_ciint usb_urb_ep_type_check(const struct urb *urb) 2258c2ecf20Sopenharmony_ci{ 2268c2ecf20Sopenharmony_ci return usb_pipe_type_check(urb->dev, urb->pipe); 2278c2ecf20Sopenharmony_ci} 2288c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_urb_ep_type_check); 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci/** 2318c2ecf20Sopenharmony_ci * usb_submit_urb - issue an asynchronous transfer request for an endpoint 2328c2ecf20Sopenharmony_ci * @urb: pointer to the urb describing the request 2338c2ecf20Sopenharmony_ci * @mem_flags: the type of memory to allocate, see kmalloc() for a list 2348c2ecf20Sopenharmony_ci * of valid options for this. 2358c2ecf20Sopenharmony_ci * 2368c2ecf20Sopenharmony_ci * This submits a transfer request, and transfers control of the URB 2378c2ecf20Sopenharmony_ci * describing that request to the USB subsystem. Request completion will 2388c2ecf20Sopenharmony_ci * be indicated later, asynchronously, by calling the completion handler. 2398c2ecf20Sopenharmony_ci * The three types of completion are success, error, and unlink 2408c2ecf20Sopenharmony_ci * (a software-induced fault, also called "request cancellation"). 2418c2ecf20Sopenharmony_ci * 2428c2ecf20Sopenharmony_ci * URBs may be submitted in interrupt context. 2438c2ecf20Sopenharmony_ci * 2448c2ecf20Sopenharmony_ci * The caller must have correctly initialized the URB before submitting 2458c2ecf20Sopenharmony_ci * it. Functions such as usb_fill_bulk_urb() and usb_fill_control_urb() are 2468c2ecf20Sopenharmony_ci * available to ensure that most fields are correctly initialized, for 2478c2ecf20Sopenharmony_ci * the particular kind of transfer, although they will not initialize 2488c2ecf20Sopenharmony_ci * any transfer flags. 2498c2ecf20Sopenharmony_ci * 2508c2ecf20Sopenharmony_ci * If the submission is successful, the complete() callback from the URB 2518c2ecf20Sopenharmony_ci * will be called exactly once, when the USB core and Host Controller Driver 2528c2ecf20Sopenharmony_ci * (HCD) are finished with the URB. When the completion function is called, 2538c2ecf20Sopenharmony_ci * control of the URB is returned to the device driver which issued the 2548c2ecf20Sopenharmony_ci * request. The completion handler may then immediately free or reuse that 2558c2ecf20Sopenharmony_ci * URB. 2568c2ecf20Sopenharmony_ci * 2578c2ecf20Sopenharmony_ci * With few exceptions, USB device drivers should never access URB fields 2588c2ecf20Sopenharmony_ci * provided by usbcore or the HCD until its complete() is called. 2598c2ecf20Sopenharmony_ci * The exceptions relate to periodic transfer scheduling. For both 2608c2ecf20Sopenharmony_ci * interrupt and isochronous urbs, as part of successful URB submission 2618c2ecf20Sopenharmony_ci * urb->interval is modified to reflect the actual transfer period used 2628c2ecf20Sopenharmony_ci * (normally some power of two units). And for isochronous urbs, 2638c2ecf20Sopenharmony_ci * urb->start_frame is modified to reflect when the URB's transfers were 2648c2ecf20Sopenharmony_ci * scheduled to start. 2658c2ecf20Sopenharmony_ci * 2668c2ecf20Sopenharmony_ci * Not all isochronous transfer scheduling policies will work, but most 2678c2ecf20Sopenharmony_ci * host controller drivers should easily handle ISO queues going from now 2688c2ecf20Sopenharmony_ci * until 10-200 msec into the future. Drivers should try to keep at 2698c2ecf20Sopenharmony_ci * least one or two msec of data in the queue; many controllers require 2708c2ecf20Sopenharmony_ci * that new transfers start at least 1 msec in the future when they are 2718c2ecf20Sopenharmony_ci * added. If the driver is unable to keep up and the queue empties out, 2728c2ecf20Sopenharmony_ci * the behavior for new submissions is governed by the URB_ISO_ASAP flag. 2738c2ecf20Sopenharmony_ci * If the flag is set, or if the queue is idle, then the URB is always 2748c2ecf20Sopenharmony_ci * assigned to the first available (and not yet expired) slot in the 2758c2ecf20Sopenharmony_ci * endpoint's schedule. If the flag is not set and the queue is active 2768c2ecf20Sopenharmony_ci * then the URB is always assigned to the next slot in the schedule 2778c2ecf20Sopenharmony_ci * following the end of the endpoint's previous URB, even if that slot is 2788c2ecf20Sopenharmony_ci * in the past. When a packet is assigned in this way to a slot that has 2798c2ecf20Sopenharmony_ci * already expired, the packet is not transmitted and the corresponding 2808c2ecf20Sopenharmony_ci * usb_iso_packet_descriptor's status field will return -EXDEV. If this 2818c2ecf20Sopenharmony_ci * would happen to all the packets in the URB, submission fails with a 2828c2ecf20Sopenharmony_ci * -EXDEV error code. 2838c2ecf20Sopenharmony_ci * 2848c2ecf20Sopenharmony_ci * For control endpoints, the synchronous usb_control_msg() call is 2858c2ecf20Sopenharmony_ci * often used (in non-interrupt context) instead of this call. 2868c2ecf20Sopenharmony_ci * That is often used through convenience wrappers, for the requests 2878c2ecf20Sopenharmony_ci * that are standardized in the USB 2.0 specification. For bulk 2888c2ecf20Sopenharmony_ci * endpoints, a synchronous usb_bulk_msg() call is available. 2898c2ecf20Sopenharmony_ci * 2908c2ecf20Sopenharmony_ci * Return: 2918c2ecf20Sopenharmony_ci * 0 on successful submissions. A negative error number otherwise. 2928c2ecf20Sopenharmony_ci * 2938c2ecf20Sopenharmony_ci * Request Queuing: 2948c2ecf20Sopenharmony_ci * 2958c2ecf20Sopenharmony_ci * URBs may be submitted to endpoints before previous ones complete, to 2968c2ecf20Sopenharmony_ci * minimize the impact of interrupt latencies and system overhead on data 2978c2ecf20Sopenharmony_ci * throughput. With that queuing policy, an endpoint's queue would never 2988c2ecf20Sopenharmony_ci * be empty. This is required for continuous isochronous data streams, 2998c2ecf20Sopenharmony_ci * and may also be required for some kinds of interrupt transfers. Such 3008c2ecf20Sopenharmony_ci * queuing also maximizes bandwidth utilization by letting USB controllers 3018c2ecf20Sopenharmony_ci * start work on later requests before driver software has finished the 3028c2ecf20Sopenharmony_ci * completion processing for earlier (successful) requests. 3038c2ecf20Sopenharmony_ci * 3048c2ecf20Sopenharmony_ci * As of Linux 2.6, all USB endpoint transfer queues support depths greater 3058c2ecf20Sopenharmony_ci * than one. This was previously a HCD-specific behavior, except for ISO 3068c2ecf20Sopenharmony_ci * transfers. Non-isochronous endpoint queues are inactive during cleanup 3078c2ecf20Sopenharmony_ci * after faults (transfer errors or cancellation). 3088c2ecf20Sopenharmony_ci * 3098c2ecf20Sopenharmony_ci * Reserved Bandwidth Transfers: 3108c2ecf20Sopenharmony_ci * 3118c2ecf20Sopenharmony_ci * Periodic transfers (interrupt or isochronous) are performed repeatedly, 3128c2ecf20Sopenharmony_ci * using the interval specified in the urb. Submitting the first urb to 3138c2ecf20Sopenharmony_ci * the endpoint reserves the bandwidth necessary to make those transfers. 3148c2ecf20Sopenharmony_ci * If the USB subsystem can't allocate sufficient bandwidth to perform 3158c2ecf20Sopenharmony_ci * the periodic request, submitting such a periodic request should fail. 3168c2ecf20Sopenharmony_ci * 3178c2ecf20Sopenharmony_ci * For devices under xHCI, the bandwidth is reserved at configuration time, or 3188c2ecf20Sopenharmony_ci * when the alt setting is selected. If there is not enough bus bandwidth, the 3198c2ecf20Sopenharmony_ci * configuration/alt setting request will fail. Therefore, submissions to 3208c2ecf20Sopenharmony_ci * periodic endpoints on devices under xHCI should never fail due to bandwidth 3218c2ecf20Sopenharmony_ci * constraints. 3228c2ecf20Sopenharmony_ci * 3238c2ecf20Sopenharmony_ci * Device drivers must explicitly request that repetition, by ensuring that 3248c2ecf20Sopenharmony_ci * some URB is always on the endpoint's queue (except possibly for short 3258c2ecf20Sopenharmony_ci * periods during completion callbacks). When there is no longer an urb 3268c2ecf20Sopenharmony_ci * queued, the endpoint's bandwidth reservation is canceled. This means 3278c2ecf20Sopenharmony_ci * drivers can use their completion handlers to ensure they keep bandwidth 3288c2ecf20Sopenharmony_ci * they need, by reinitializing and resubmitting the just-completed urb 3298c2ecf20Sopenharmony_ci * until the driver longer needs that periodic bandwidth. 3308c2ecf20Sopenharmony_ci * 3318c2ecf20Sopenharmony_ci * Memory Flags: 3328c2ecf20Sopenharmony_ci * 3338c2ecf20Sopenharmony_ci * The general rules for how to decide which mem_flags to use 3348c2ecf20Sopenharmony_ci * are the same as for kmalloc. There are four 3358c2ecf20Sopenharmony_ci * different possible values; GFP_KERNEL, GFP_NOFS, GFP_NOIO and 3368c2ecf20Sopenharmony_ci * GFP_ATOMIC. 3378c2ecf20Sopenharmony_ci * 3388c2ecf20Sopenharmony_ci * GFP_NOFS is not ever used, as it has not been implemented yet. 3398c2ecf20Sopenharmony_ci * 3408c2ecf20Sopenharmony_ci * GFP_ATOMIC is used when 3418c2ecf20Sopenharmony_ci * (a) you are inside a completion handler, an interrupt, bottom half, 3428c2ecf20Sopenharmony_ci * tasklet or timer, or 3438c2ecf20Sopenharmony_ci * (b) you are holding a spinlock or rwlock (does not apply to 3448c2ecf20Sopenharmony_ci * semaphores), or 3458c2ecf20Sopenharmony_ci * (c) current->state != TASK_RUNNING, this is the case only after 3468c2ecf20Sopenharmony_ci * you've changed it. 3478c2ecf20Sopenharmony_ci * 3488c2ecf20Sopenharmony_ci * GFP_NOIO is used in the block io path and error handling of storage 3498c2ecf20Sopenharmony_ci * devices. 3508c2ecf20Sopenharmony_ci * 3518c2ecf20Sopenharmony_ci * All other situations use GFP_KERNEL. 3528c2ecf20Sopenharmony_ci * 3538c2ecf20Sopenharmony_ci * Some more specific rules for mem_flags can be inferred, such as 3548c2ecf20Sopenharmony_ci * (1) start_xmit, timeout, and receive methods of network drivers must 3558c2ecf20Sopenharmony_ci * use GFP_ATOMIC (they are called with a spinlock held); 3568c2ecf20Sopenharmony_ci * (2) queuecommand methods of scsi drivers must use GFP_ATOMIC (also 3578c2ecf20Sopenharmony_ci * called with a spinlock held); 3588c2ecf20Sopenharmony_ci * (3) If you use a kernel thread with a network driver you must use 3598c2ecf20Sopenharmony_ci * GFP_NOIO, unless (b) or (c) apply; 3608c2ecf20Sopenharmony_ci * (4) after you have done a down() you can use GFP_KERNEL, unless (b) or (c) 3618c2ecf20Sopenharmony_ci * apply or your are in a storage driver's block io path; 3628c2ecf20Sopenharmony_ci * (5) USB probe and disconnect can use GFP_KERNEL unless (b) or (c) apply; and 3638c2ecf20Sopenharmony_ci * (6) changing firmware on a running storage or net device uses 3648c2ecf20Sopenharmony_ci * GFP_NOIO, unless b) or c) apply 3658c2ecf20Sopenharmony_ci * 3668c2ecf20Sopenharmony_ci */ 3678c2ecf20Sopenharmony_ciint usb_submit_urb(struct urb *urb, gfp_t mem_flags) 3688c2ecf20Sopenharmony_ci{ 3698c2ecf20Sopenharmony_ci int xfertype, max; 3708c2ecf20Sopenharmony_ci struct usb_device *dev; 3718c2ecf20Sopenharmony_ci struct usb_host_endpoint *ep; 3728c2ecf20Sopenharmony_ci int is_out; 3738c2ecf20Sopenharmony_ci unsigned int allowed; 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci if (!urb || !urb->complete) 3768c2ecf20Sopenharmony_ci return -EINVAL; 3778c2ecf20Sopenharmony_ci if (urb->hcpriv) { 3788c2ecf20Sopenharmony_ci WARN_ONCE(1, "URB %pK submitted while active\n", urb); 3798c2ecf20Sopenharmony_ci return -EBUSY; 3808c2ecf20Sopenharmony_ci } 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci dev = urb->dev; 3838c2ecf20Sopenharmony_ci if ((!dev) || (dev->state < USB_STATE_UNAUTHENTICATED)) 3848c2ecf20Sopenharmony_ci return -ENODEV; 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci /* For now, get the endpoint from the pipe. Eventually drivers 3878c2ecf20Sopenharmony_ci * will be required to set urb->ep directly and we will eliminate 3888c2ecf20Sopenharmony_ci * urb->pipe. 3898c2ecf20Sopenharmony_ci */ 3908c2ecf20Sopenharmony_ci ep = usb_pipe_endpoint(dev, urb->pipe); 3918c2ecf20Sopenharmony_ci if (!ep) 3928c2ecf20Sopenharmony_ci return -ENOENT; 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci urb->ep = ep; 3958c2ecf20Sopenharmony_ci urb->status = -EINPROGRESS; 3968c2ecf20Sopenharmony_ci urb->actual_length = 0; 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci /* Lots of sanity checks, so HCDs can rely on clean data 3998c2ecf20Sopenharmony_ci * and don't need to duplicate tests 4008c2ecf20Sopenharmony_ci */ 4018c2ecf20Sopenharmony_ci xfertype = usb_endpoint_type(&ep->desc); 4028c2ecf20Sopenharmony_ci if (xfertype == USB_ENDPOINT_XFER_CONTROL) { 4038c2ecf20Sopenharmony_ci struct usb_ctrlrequest *setup = 4048c2ecf20Sopenharmony_ci (struct usb_ctrlrequest *) urb->setup_packet; 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci if (!setup) 4078c2ecf20Sopenharmony_ci return -ENOEXEC; 4088c2ecf20Sopenharmony_ci is_out = !(setup->bRequestType & USB_DIR_IN) || 4098c2ecf20Sopenharmony_ci !setup->wLength; 4108c2ecf20Sopenharmony_ci } else { 4118c2ecf20Sopenharmony_ci is_out = usb_endpoint_dir_out(&ep->desc); 4128c2ecf20Sopenharmony_ci } 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci /* Clear the internal flags and cache the direction for later use */ 4158c2ecf20Sopenharmony_ci urb->transfer_flags &= ~(URB_DIR_MASK | URB_DMA_MAP_SINGLE | 4168c2ecf20Sopenharmony_ci URB_DMA_MAP_PAGE | URB_DMA_MAP_SG | URB_MAP_LOCAL | 4178c2ecf20Sopenharmony_ci URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL | 4188c2ecf20Sopenharmony_ci URB_DMA_SG_COMBINED); 4198c2ecf20Sopenharmony_ci urb->transfer_flags |= (is_out ? URB_DIR_OUT : URB_DIR_IN); 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci if (xfertype != USB_ENDPOINT_XFER_CONTROL && 4228c2ecf20Sopenharmony_ci dev->state < USB_STATE_CONFIGURED) 4238c2ecf20Sopenharmony_ci return -ENODEV; 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci max = usb_endpoint_maxp(&ep->desc); 4268c2ecf20Sopenharmony_ci if (max <= 0) { 4278c2ecf20Sopenharmony_ci dev_dbg(&dev->dev, 4288c2ecf20Sopenharmony_ci "bogus endpoint ep%d%s in %s (bad maxpacket %d)\n", 4298c2ecf20Sopenharmony_ci usb_endpoint_num(&ep->desc), is_out ? "out" : "in", 4308c2ecf20Sopenharmony_ci __func__, max); 4318c2ecf20Sopenharmony_ci return -EMSGSIZE; 4328c2ecf20Sopenharmony_ci } 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci /* periodic transfers limit size per frame/uframe, 4358c2ecf20Sopenharmony_ci * but drivers only control those sizes for ISO. 4368c2ecf20Sopenharmony_ci * while we're checking, initialize return status. 4378c2ecf20Sopenharmony_ci */ 4388c2ecf20Sopenharmony_ci if (xfertype == USB_ENDPOINT_XFER_ISOC) { 4398c2ecf20Sopenharmony_ci int n, len; 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_ci /* SuperSpeed isoc endpoints have up to 16 bursts of up to 4428c2ecf20Sopenharmony_ci * 3 packets each 4438c2ecf20Sopenharmony_ci */ 4448c2ecf20Sopenharmony_ci if (dev->speed >= USB_SPEED_SUPER) { 4458c2ecf20Sopenharmony_ci int burst = 1 + ep->ss_ep_comp.bMaxBurst; 4468c2ecf20Sopenharmony_ci int mult = USB_SS_MULT(ep->ss_ep_comp.bmAttributes); 4478c2ecf20Sopenharmony_ci max *= burst; 4488c2ecf20Sopenharmony_ci max *= mult; 4498c2ecf20Sopenharmony_ci } 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci if (dev->speed == USB_SPEED_SUPER_PLUS && 4528c2ecf20Sopenharmony_ci USB_SS_SSP_ISOC_COMP(ep->ss_ep_comp.bmAttributes)) { 4538c2ecf20Sopenharmony_ci struct usb_ssp_isoc_ep_comp_descriptor *isoc_ep_comp; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci isoc_ep_comp = &ep->ssp_isoc_ep_comp; 4568c2ecf20Sopenharmony_ci max = le32_to_cpu(isoc_ep_comp->dwBytesPerInterval); 4578c2ecf20Sopenharmony_ci } 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci /* "high bandwidth" mode, 1-3 packets/uframe? */ 4608c2ecf20Sopenharmony_ci if (dev->speed == USB_SPEED_HIGH) 4618c2ecf20Sopenharmony_ci max *= usb_endpoint_maxp_mult(&ep->desc); 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ci if (urb->number_of_packets <= 0) 4648c2ecf20Sopenharmony_ci return -EINVAL; 4658c2ecf20Sopenharmony_ci for (n = 0; n < urb->number_of_packets; n++) { 4668c2ecf20Sopenharmony_ci len = urb->iso_frame_desc[n].length; 4678c2ecf20Sopenharmony_ci if (len < 0 || len > max) 4688c2ecf20Sopenharmony_ci return -EMSGSIZE; 4698c2ecf20Sopenharmony_ci urb->iso_frame_desc[n].status = -EXDEV; 4708c2ecf20Sopenharmony_ci urb->iso_frame_desc[n].actual_length = 0; 4718c2ecf20Sopenharmony_ci } 4728c2ecf20Sopenharmony_ci } else if (urb->num_sgs && !urb->dev->bus->no_sg_constraint && 4738c2ecf20Sopenharmony_ci dev->speed != USB_SPEED_WIRELESS) { 4748c2ecf20Sopenharmony_ci struct scatterlist *sg; 4758c2ecf20Sopenharmony_ci int i; 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci for_each_sg(urb->sg, sg, urb->num_sgs - 1, i) 4788c2ecf20Sopenharmony_ci if (sg->length % max) 4798c2ecf20Sopenharmony_ci return -EINVAL; 4808c2ecf20Sopenharmony_ci } 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ci /* the I/O buffer must be mapped/unmapped, except when length=0 */ 4838c2ecf20Sopenharmony_ci if (urb->transfer_buffer_length > INT_MAX) 4848c2ecf20Sopenharmony_ci return -EMSGSIZE; 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci /* 4878c2ecf20Sopenharmony_ci * stuff that drivers shouldn't do, but which shouldn't 4888c2ecf20Sopenharmony_ci * cause problems in HCDs if they get it wrong. 4898c2ecf20Sopenharmony_ci */ 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci /* Check that the pipe's type matches the endpoint's type */ 4928c2ecf20Sopenharmony_ci if (usb_pipe_type_check(urb->dev, urb->pipe)) 4938c2ecf20Sopenharmony_ci dev_WARN(&dev->dev, "BOGUS urb xfer, pipe %x != type %x\n", 4948c2ecf20Sopenharmony_ci usb_pipetype(urb->pipe), pipetypes[xfertype]); 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_ci /* Check against a simple/standard policy */ 4978c2ecf20Sopenharmony_ci allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT | URB_DIR_MASK | 4988c2ecf20Sopenharmony_ci URB_FREE_BUFFER); 4998c2ecf20Sopenharmony_ci switch (xfertype) { 5008c2ecf20Sopenharmony_ci case USB_ENDPOINT_XFER_BULK: 5018c2ecf20Sopenharmony_ci case USB_ENDPOINT_XFER_INT: 5028c2ecf20Sopenharmony_ci if (is_out) 5038c2ecf20Sopenharmony_ci allowed |= URB_ZERO_PACKET; 5048c2ecf20Sopenharmony_ci fallthrough; 5058c2ecf20Sopenharmony_ci default: /* all non-iso endpoints */ 5068c2ecf20Sopenharmony_ci if (!is_out) 5078c2ecf20Sopenharmony_ci allowed |= URB_SHORT_NOT_OK; 5088c2ecf20Sopenharmony_ci break; 5098c2ecf20Sopenharmony_ci case USB_ENDPOINT_XFER_ISOC: 5108c2ecf20Sopenharmony_ci allowed |= URB_ISO_ASAP; 5118c2ecf20Sopenharmony_ci break; 5128c2ecf20Sopenharmony_ci } 5138c2ecf20Sopenharmony_ci allowed &= urb->transfer_flags; 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci /* warn if submitter gave bogus flags */ 5168c2ecf20Sopenharmony_ci if (allowed != urb->transfer_flags) 5178c2ecf20Sopenharmony_ci dev_WARN(&dev->dev, "BOGUS urb flags, %x --> %x\n", 5188c2ecf20Sopenharmony_ci urb->transfer_flags, allowed); 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci /* 5218c2ecf20Sopenharmony_ci * Force periodic transfer intervals to be legal values that are 5228c2ecf20Sopenharmony_ci * a power of two (so HCDs don't need to). 5238c2ecf20Sopenharmony_ci * 5248c2ecf20Sopenharmony_ci * FIXME want bus->{intr,iso}_sched_horizon values here. Each HC 5258c2ecf20Sopenharmony_ci * supports different values... this uses EHCI/UHCI defaults (and 5268c2ecf20Sopenharmony_ci * EHCI can use smaller non-default values). 5278c2ecf20Sopenharmony_ci */ 5288c2ecf20Sopenharmony_ci switch (xfertype) { 5298c2ecf20Sopenharmony_ci case USB_ENDPOINT_XFER_ISOC: 5308c2ecf20Sopenharmony_ci case USB_ENDPOINT_XFER_INT: 5318c2ecf20Sopenharmony_ci /* too small? */ 5328c2ecf20Sopenharmony_ci switch (dev->speed) { 5338c2ecf20Sopenharmony_ci case USB_SPEED_WIRELESS: 5348c2ecf20Sopenharmony_ci if ((urb->interval < 6) 5358c2ecf20Sopenharmony_ci && (xfertype == USB_ENDPOINT_XFER_INT)) 5368c2ecf20Sopenharmony_ci return -EINVAL; 5378c2ecf20Sopenharmony_ci fallthrough; 5388c2ecf20Sopenharmony_ci default: 5398c2ecf20Sopenharmony_ci if (urb->interval <= 0) 5408c2ecf20Sopenharmony_ci return -EINVAL; 5418c2ecf20Sopenharmony_ci break; 5428c2ecf20Sopenharmony_ci } 5438c2ecf20Sopenharmony_ci /* too big? */ 5448c2ecf20Sopenharmony_ci switch (dev->speed) { 5458c2ecf20Sopenharmony_ci case USB_SPEED_SUPER_PLUS: 5468c2ecf20Sopenharmony_ci case USB_SPEED_SUPER: /* units are 125us */ 5478c2ecf20Sopenharmony_ci /* Handle up to 2^(16-1) microframes */ 5488c2ecf20Sopenharmony_ci if (urb->interval > (1 << 15)) 5498c2ecf20Sopenharmony_ci return -EINVAL; 5508c2ecf20Sopenharmony_ci max = 1 << 15; 5518c2ecf20Sopenharmony_ci break; 5528c2ecf20Sopenharmony_ci case USB_SPEED_WIRELESS: 5538c2ecf20Sopenharmony_ci if (urb->interval > 16) 5548c2ecf20Sopenharmony_ci return -EINVAL; 5558c2ecf20Sopenharmony_ci break; 5568c2ecf20Sopenharmony_ci case USB_SPEED_HIGH: /* units are microframes */ 5578c2ecf20Sopenharmony_ci /* NOTE usb handles 2^15 */ 5588c2ecf20Sopenharmony_ci if (urb->interval > (1024 * 8)) 5598c2ecf20Sopenharmony_ci urb->interval = 1024 * 8; 5608c2ecf20Sopenharmony_ci max = 1024 * 8; 5618c2ecf20Sopenharmony_ci break; 5628c2ecf20Sopenharmony_ci case USB_SPEED_FULL: /* units are frames/msec */ 5638c2ecf20Sopenharmony_ci case USB_SPEED_LOW: 5648c2ecf20Sopenharmony_ci if (xfertype == USB_ENDPOINT_XFER_INT) { 5658c2ecf20Sopenharmony_ci if (urb->interval > 255) 5668c2ecf20Sopenharmony_ci return -EINVAL; 5678c2ecf20Sopenharmony_ci /* NOTE ohci only handles up to 32 */ 5688c2ecf20Sopenharmony_ci max = 128; 5698c2ecf20Sopenharmony_ci } else { 5708c2ecf20Sopenharmony_ci if (urb->interval > 1024) 5718c2ecf20Sopenharmony_ci urb->interval = 1024; 5728c2ecf20Sopenharmony_ci /* NOTE usb and ohci handle up to 2^15 */ 5738c2ecf20Sopenharmony_ci max = 1024; 5748c2ecf20Sopenharmony_ci } 5758c2ecf20Sopenharmony_ci break; 5768c2ecf20Sopenharmony_ci default: 5778c2ecf20Sopenharmony_ci return -EINVAL; 5788c2ecf20Sopenharmony_ci } 5798c2ecf20Sopenharmony_ci if (dev->speed != USB_SPEED_WIRELESS) { 5808c2ecf20Sopenharmony_ci /* Round down to a power of 2, no more than max */ 5818c2ecf20Sopenharmony_ci urb->interval = min(max, 1 << ilog2(urb->interval)); 5828c2ecf20Sopenharmony_ci } 5838c2ecf20Sopenharmony_ci } 5848c2ecf20Sopenharmony_ci 5858c2ecf20Sopenharmony_ci return usb_hcd_submit_urb(urb, mem_flags); 5868c2ecf20Sopenharmony_ci} 5878c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_submit_urb); 5888c2ecf20Sopenharmony_ci 5898c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------*/ 5908c2ecf20Sopenharmony_ci 5918c2ecf20Sopenharmony_ci/** 5928c2ecf20Sopenharmony_ci * usb_unlink_urb - abort/cancel a transfer request for an endpoint 5938c2ecf20Sopenharmony_ci * @urb: pointer to urb describing a previously submitted request, 5948c2ecf20Sopenharmony_ci * may be NULL 5958c2ecf20Sopenharmony_ci * 5968c2ecf20Sopenharmony_ci * This routine cancels an in-progress request. URBs complete only once 5978c2ecf20Sopenharmony_ci * per submission, and may be canceled only once per submission. 5988c2ecf20Sopenharmony_ci * Successful cancellation means termination of @urb will be expedited 5998c2ecf20Sopenharmony_ci * and the completion handler will be called with a status code 6008c2ecf20Sopenharmony_ci * indicating that the request has been canceled (rather than any other 6018c2ecf20Sopenharmony_ci * code). 6028c2ecf20Sopenharmony_ci * 6038c2ecf20Sopenharmony_ci * Drivers should not call this routine or related routines, such as 6048c2ecf20Sopenharmony_ci * usb_kill_urb() or usb_unlink_anchored_urbs(), after their disconnect 6058c2ecf20Sopenharmony_ci * method has returned. The disconnect function should synchronize with 6068c2ecf20Sopenharmony_ci * a driver's I/O routines to insure that all URB-related activity has 6078c2ecf20Sopenharmony_ci * completed before it returns. 6088c2ecf20Sopenharmony_ci * 6098c2ecf20Sopenharmony_ci * This request is asynchronous, however the HCD might call the ->complete() 6108c2ecf20Sopenharmony_ci * callback during unlink. Therefore when drivers call usb_unlink_urb(), they 6118c2ecf20Sopenharmony_ci * must not hold any locks that may be taken by the completion function. 6128c2ecf20Sopenharmony_ci * Success is indicated by returning -EINPROGRESS, at which time the URB will 6138c2ecf20Sopenharmony_ci * probably not yet have been given back to the device driver. When it is 6148c2ecf20Sopenharmony_ci * eventually called, the completion function will see @urb->status == 6158c2ecf20Sopenharmony_ci * -ECONNRESET. 6168c2ecf20Sopenharmony_ci * Failure is indicated by usb_unlink_urb() returning any other value. 6178c2ecf20Sopenharmony_ci * Unlinking will fail when @urb is not currently "linked" (i.e., it was 6188c2ecf20Sopenharmony_ci * never submitted, or it was unlinked before, or the hardware is already 6198c2ecf20Sopenharmony_ci * finished with it), even if the completion handler has not yet run. 6208c2ecf20Sopenharmony_ci * 6218c2ecf20Sopenharmony_ci * The URB must not be deallocated while this routine is running. In 6228c2ecf20Sopenharmony_ci * particular, when a driver calls this routine, it must insure that the 6238c2ecf20Sopenharmony_ci * completion handler cannot deallocate the URB. 6248c2ecf20Sopenharmony_ci * 6258c2ecf20Sopenharmony_ci * Return: -EINPROGRESS on success. See description for other values on 6268c2ecf20Sopenharmony_ci * failure. 6278c2ecf20Sopenharmony_ci * 6288c2ecf20Sopenharmony_ci * Unlinking and Endpoint Queues: 6298c2ecf20Sopenharmony_ci * 6308c2ecf20Sopenharmony_ci * [The behaviors and guarantees described below do not apply to virtual 6318c2ecf20Sopenharmony_ci * root hubs but only to endpoint queues for physical USB devices.] 6328c2ecf20Sopenharmony_ci * 6338c2ecf20Sopenharmony_ci * Host Controller Drivers (HCDs) place all the URBs for a particular 6348c2ecf20Sopenharmony_ci * endpoint in a queue. Normally the queue advances as the controller 6358c2ecf20Sopenharmony_ci * hardware processes each request. But when an URB terminates with an 6368c2ecf20Sopenharmony_ci * error its queue generally stops (see below), at least until that URB's 6378c2ecf20Sopenharmony_ci * completion routine returns. It is guaranteed that a stopped queue 6388c2ecf20Sopenharmony_ci * will not restart until all its unlinked URBs have been fully retired, 6398c2ecf20Sopenharmony_ci * with their completion routines run, even if that's not until some time 6408c2ecf20Sopenharmony_ci * after the original completion handler returns. The same behavior and 6418c2ecf20Sopenharmony_ci * guarantee apply when an URB terminates because it was unlinked. 6428c2ecf20Sopenharmony_ci * 6438c2ecf20Sopenharmony_ci * Bulk and interrupt endpoint queues are guaranteed to stop whenever an 6448c2ecf20Sopenharmony_ci * URB terminates with any sort of error, including -ECONNRESET, -ENOENT, 6458c2ecf20Sopenharmony_ci * and -EREMOTEIO. Control endpoint queues behave the same way except 6468c2ecf20Sopenharmony_ci * that they are not guaranteed to stop for -EREMOTEIO errors. Queues 6478c2ecf20Sopenharmony_ci * for isochronous endpoints are treated differently, because they must 6488c2ecf20Sopenharmony_ci * advance at fixed rates. Such queues do not stop when an URB 6498c2ecf20Sopenharmony_ci * encounters an error or is unlinked. An unlinked isochronous URB may 6508c2ecf20Sopenharmony_ci * leave a gap in the stream of packets; it is undefined whether such 6518c2ecf20Sopenharmony_ci * gaps can be filled in. 6528c2ecf20Sopenharmony_ci * 6538c2ecf20Sopenharmony_ci * Note that early termination of an URB because a short packet was 6548c2ecf20Sopenharmony_ci * received will generate a -EREMOTEIO error if and only if the 6558c2ecf20Sopenharmony_ci * URB_SHORT_NOT_OK flag is set. By setting this flag, USB device 6568c2ecf20Sopenharmony_ci * drivers can build deep queues for large or complex bulk transfers 6578c2ecf20Sopenharmony_ci * and clean them up reliably after any sort of aborted transfer by 6588c2ecf20Sopenharmony_ci * unlinking all pending URBs at the first fault. 6598c2ecf20Sopenharmony_ci * 6608c2ecf20Sopenharmony_ci * When a control URB terminates with an error other than -EREMOTEIO, it 6618c2ecf20Sopenharmony_ci * is quite likely that the status stage of the transfer will not take 6628c2ecf20Sopenharmony_ci * place. 6638c2ecf20Sopenharmony_ci */ 6648c2ecf20Sopenharmony_ciint usb_unlink_urb(struct urb *urb) 6658c2ecf20Sopenharmony_ci{ 6668c2ecf20Sopenharmony_ci if (!urb) 6678c2ecf20Sopenharmony_ci return -EINVAL; 6688c2ecf20Sopenharmony_ci if (!urb->dev) 6698c2ecf20Sopenharmony_ci return -ENODEV; 6708c2ecf20Sopenharmony_ci if (!urb->ep) 6718c2ecf20Sopenharmony_ci return -EIDRM; 6728c2ecf20Sopenharmony_ci return usb_hcd_unlink_urb(urb, -ECONNRESET); 6738c2ecf20Sopenharmony_ci} 6748c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_unlink_urb); 6758c2ecf20Sopenharmony_ci 6768c2ecf20Sopenharmony_ci/** 6778c2ecf20Sopenharmony_ci * usb_kill_urb - cancel a transfer request and wait for it to finish 6788c2ecf20Sopenharmony_ci * @urb: pointer to URB describing a previously submitted request, 6798c2ecf20Sopenharmony_ci * may be NULL 6808c2ecf20Sopenharmony_ci * 6818c2ecf20Sopenharmony_ci * This routine cancels an in-progress request. It is guaranteed that 6828c2ecf20Sopenharmony_ci * upon return all completion handlers will have finished and the URB 6838c2ecf20Sopenharmony_ci * will be totally idle and available for reuse. These features make 6848c2ecf20Sopenharmony_ci * this an ideal way to stop I/O in a disconnect() callback or close() 6858c2ecf20Sopenharmony_ci * function. If the request has not already finished or been unlinked 6868c2ecf20Sopenharmony_ci * the completion handler will see urb->status == -ENOENT. 6878c2ecf20Sopenharmony_ci * 6888c2ecf20Sopenharmony_ci * While the routine is running, attempts to resubmit the URB will fail 6898c2ecf20Sopenharmony_ci * with error -EPERM. Thus even if the URB's completion handler always 6908c2ecf20Sopenharmony_ci * tries to resubmit, it will not succeed and the URB will become idle. 6918c2ecf20Sopenharmony_ci * 6928c2ecf20Sopenharmony_ci * The URB must not be deallocated while this routine is running. In 6938c2ecf20Sopenharmony_ci * particular, when a driver calls this routine, it must insure that the 6948c2ecf20Sopenharmony_ci * completion handler cannot deallocate the URB. 6958c2ecf20Sopenharmony_ci * 6968c2ecf20Sopenharmony_ci * This routine may not be used in an interrupt context (such as a bottom 6978c2ecf20Sopenharmony_ci * half or a completion handler), or when holding a spinlock, or in other 6988c2ecf20Sopenharmony_ci * situations where the caller can't schedule(). 6998c2ecf20Sopenharmony_ci * 7008c2ecf20Sopenharmony_ci * This routine should not be called by a driver after its disconnect 7018c2ecf20Sopenharmony_ci * method has returned. 7028c2ecf20Sopenharmony_ci */ 7038c2ecf20Sopenharmony_civoid usb_kill_urb(struct urb *urb) 7048c2ecf20Sopenharmony_ci{ 7058c2ecf20Sopenharmony_ci might_sleep(); 7068c2ecf20Sopenharmony_ci if (!(urb && urb->dev && urb->ep)) 7078c2ecf20Sopenharmony_ci return; 7088c2ecf20Sopenharmony_ci atomic_inc(&urb->reject); 7098c2ecf20Sopenharmony_ci /* 7108c2ecf20Sopenharmony_ci * Order the write of urb->reject above before the read 7118c2ecf20Sopenharmony_ci * of urb->use_count below. Pairs with the barriers in 7128c2ecf20Sopenharmony_ci * __usb_hcd_giveback_urb() and usb_hcd_submit_urb(). 7138c2ecf20Sopenharmony_ci */ 7148c2ecf20Sopenharmony_ci smp_mb__after_atomic(); 7158c2ecf20Sopenharmony_ci 7168c2ecf20Sopenharmony_ci usb_hcd_unlink_urb(urb, -ENOENT); 7178c2ecf20Sopenharmony_ci wait_event(usb_kill_urb_queue, atomic_read(&urb->use_count) == 0); 7188c2ecf20Sopenharmony_ci 7198c2ecf20Sopenharmony_ci atomic_dec(&urb->reject); 7208c2ecf20Sopenharmony_ci} 7218c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_kill_urb); 7228c2ecf20Sopenharmony_ci 7238c2ecf20Sopenharmony_ci/** 7248c2ecf20Sopenharmony_ci * usb_poison_urb - reliably kill a transfer and prevent further use of an URB 7258c2ecf20Sopenharmony_ci * @urb: pointer to URB describing a previously submitted request, 7268c2ecf20Sopenharmony_ci * may be NULL 7278c2ecf20Sopenharmony_ci * 7288c2ecf20Sopenharmony_ci * This routine cancels an in-progress request. It is guaranteed that 7298c2ecf20Sopenharmony_ci * upon return all completion handlers will have finished and the URB 7308c2ecf20Sopenharmony_ci * will be totally idle and cannot be reused. These features make 7318c2ecf20Sopenharmony_ci * this an ideal way to stop I/O in a disconnect() callback. 7328c2ecf20Sopenharmony_ci * If the request has not already finished or been unlinked 7338c2ecf20Sopenharmony_ci * the completion handler will see urb->status == -ENOENT. 7348c2ecf20Sopenharmony_ci * 7358c2ecf20Sopenharmony_ci * After and while the routine runs, attempts to resubmit the URB will fail 7368c2ecf20Sopenharmony_ci * with error -EPERM. Thus even if the URB's completion handler always 7378c2ecf20Sopenharmony_ci * tries to resubmit, it will not succeed and the URB will become idle. 7388c2ecf20Sopenharmony_ci * 7398c2ecf20Sopenharmony_ci * The URB must not be deallocated while this routine is running. In 7408c2ecf20Sopenharmony_ci * particular, when a driver calls this routine, it must insure that the 7418c2ecf20Sopenharmony_ci * completion handler cannot deallocate the URB. 7428c2ecf20Sopenharmony_ci * 7438c2ecf20Sopenharmony_ci * This routine may not be used in an interrupt context (such as a bottom 7448c2ecf20Sopenharmony_ci * half or a completion handler), or when holding a spinlock, or in other 7458c2ecf20Sopenharmony_ci * situations where the caller can't schedule(). 7468c2ecf20Sopenharmony_ci * 7478c2ecf20Sopenharmony_ci * This routine should not be called by a driver after its disconnect 7488c2ecf20Sopenharmony_ci * method has returned. 7498c2ecf20Sopenharmony_ci */ 7508c2ecf20Sopenharmony_civoid usb_poison_urb(struct urb *urb) 7518c2ecf20Sopenharmony_ci{ 7528c2ecf20Sopenharmony_ci might_sleep(); 7538c2ecf20Sopenharmony_ci if (!urb) 7548c2ecf20Sopenharmony_ci return; 7558c2ecf20Sopenharmony_ci atomic_inc(&urb->reject); 7568c2ecf20Sopenharmony_ci /* 7578c2ecf20Sopenharmony_ci * Order the write of urb->reject above before the read 7588c2ecf20Sopenharmony_ci * of urb->use_count below. Pairs with the barriers in 7598c2ecf20Sopenharmony_ci * __usb_hcd_giveback_urb() and usb_hcd_submit_urb(). 7608c2ecf20Sopenharmony_ci */ 7618c2ecf20Sopenharmony_ci smp_mb__after_atomic(); 7628c2ecf20Sopenharmony_ci 7638c2ecf20Sopenharmony_ci if (!urb->dev || !urb->ep) 7648c2ecf20Sopenharmony_ci return; 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci usb_hcd_unlink_urb(urb, -ENOENT); 7678c2ecf20Sopenharmony_ci wait_event(usb_kill_urb_queue, atomic_read(&urb->use_count) == 0); 7688c2ecf20Sopenharmony_ci} 7698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_poison_urb); 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_civoid usb_unpoison_urb(struct urb *urb) 7728c2ecf20Sopenharmony_ci{ 7738c2ecf20Sopenharmony_ci if (!urb) 7748c2ecf20Sopenharmony_ci return; 7758c2ecf20Sopenharmony_ci 7768c2ecf20Sopenharmony_ci atomic_dec(&urb->reject); 7778c2ecf20Sopenharmony_ci} 7788c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_unpoison_urb); 7798c2ecf20Sopenharmony_ci 7808c2ecf20Sopenharmony_ci/** 7818c2ecf20Sopenharmony_ci * usb_block_urb - reliably prevent further use of an URB 7828c2ecf20Sopenharmony_ci * @urb: pointer to URB to be blocked, may be NULL 7838c2ecf20Sopenharmony_ci * 7848c2ecf20Sopenharmony_ci * After the routine has run, attempts to resubmit the URB will fail 7858c2ecf20Sopenharmony_ci * with error -EPERM. Thus even if the URB's completion handler always 7868c2ecf20Sopenharmony_ci * tries to resubmit, it will not succeed and the URB will become idle. 7878c2ecf20Sopenharmony_ci * 7888c2ecf20Sopenharmony_ci * The URB must not be deallocated while this routine is running. In 7898c2ecf20Sopenharmony_ci * particular, when a driver calls this routine, it must insure that the 7908c2ecf20Sopenharmony_ci * completion handler cannot deallocate the URB. 7918c2ecf20Sopenharmony_ci */ 7928c2ecf20Sopenharmony_civoid usb_block_urb(struct urb *urb) 7938c2ecf20Sopenharmony_ci{ 7948c2ecf20Sopenharmony_ci if (!urb) 7958c2ecf20Sopenharmony_ci return; 7968c2ecf20Sopenharmony_ci 7978c2ecf20Sopenharmony_ci atomic_inc(&urb->reject); 7988c2ecf20Sopenharmony_ci} 7998c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_block_urb); 8008c2ecf20Sopenharmony_ci 8018c2ecf20Sopenharmony_ci/** 8028c2ecf20Sopenharmony_ci * usb_kill_anchored_urbs - kill all URBs associated with an anchor 8038c2ecf20Sopenharmony_ci * @anchor: anchor the requests are bound to 8048c2ecf20Sopenharmony_ci * 8058c2ecf20Sopenharmony_ci * This kills all outstanding URBs starting from the back of the queue, 8068c2ecf20Sopenharmony_ci * with guarantee that no completer callbacks will take place from the 8078c2ecf20Sopenharmony_ci * anchor after this function returns. 8088c2ecf20Sopenharmony_ci * 8098c2ecf20Sopenharmony_ci * This routine should not be called by a driver after its disconnect 8108c2ecf20Sopenharmony_ci * method has returned. 8118c2ecf20Sopenharmony_ci */ 8128c2ecf20Sopenharmony_civoid usb_kill_anchored_urbs(struct usb_anchor *anchor) 8138c2ecf20Sopenharmony_ci{ 8148c2ecf20Sopenharmony_ci struct urb *victim; 8158c2ecf20Sopenharmony_ci int surely_empty; 8168c2ecf20Sopenharmony_ci 8178c2ecf20Sopenharmony_ci do { 8188c2ecf20Sopenharmony_ci spin_lock_irq(&anchor->lock); 8198c2ecf20Sopenharmony_ci while (!list_empty(&anchor->urb_list)) { 8208c2ecf20Sopenharmony_ci victim = list_entry(anchor->urb_list.prev, 8218c2ecf20Sopenharmony_ci struct urb, anchor_list); 8228c2ecf20Sopenharmony_ci /* make sure the URB isn't freed before we kill it */ 8238c2ecf20Sopenharmony_ci usb_get_urb(victim); 8248c2ecf20Sopenharmony_ci spin_unlock_irq(&anchor->lock); 8258c2ecf20Sopenharmony_ci /* this will unanchor the URB */ 8268c2ecf20Sopenharmony_ci usb_kill_urb(victim); 8278c2ecf20Sopenharmony_ci usb_put_urb(victim); 8288c2ecf20Sopenharmony_ci spin_lock_irq(&anchor->lock); 8298c2ecf20Sopenharmony_ci } 8308c2ecf20Sopenharmony_ci surely_empty = usb_anchor_check_wakeup(anchor); 8318c2ecf20Sopenharmony_ci 8328c2ecf20Sopenharmony_ci spin_unlock_irq(&anchor->lock); 8338c2ecf20Sopenharmony_ci cpu_relax(); 8348c2ecf20Sopenharmony_ci } while (!surely_empty); 8358c2ecf20Sopenharmony_ci} 8368c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_kill_anchored_urbs); 8378c2ecf20Sopenharmony_ci 8388c2ecf20Sopenharmony_ci 8398c2ecf20Sopenharmony_ci/** 8408c2ecf20Sopenharmony_ci * usb_poison_anchored_urbs - cease all traffic from an anchor 8418c2ecf20Sopenharmony_ci * @anchor: anchor the requests are bound to 8428c2ecf20Sopenharmony_ci * 8438c2ecf20Sopenharmony_ci * this allows all outstanding URBs to be poisoned starting 8448c2ecf20Sopenharmony_ci * from the back of the queue. Newly added URBs will also be 8458c2ecf20Sopenharmony_ci * poisoned 8468c2ecf20Sopenharmony_ci * 8478c2ecf20Sopenharmony_ci * This routine should not be called by a driver after its disconnect 8488c2ecf20Sopenharmony_ci * method has returned. 8498c2ecf20Sopenharmony_ci */ 8508c2ecf20Sopenharmony_civoid usb_poison_anchored_urbs(struct usb_anchor *anchor) 8518c2ecf20Sopenharmony_ci{ 8528c2ecf20Sopenharmony_ci struct urb *victim; 8538c2ecf20Sopenharmony_ci int surely_empty; 8548c2ecf20Sopenharmony_ci 8558c2ecf20Sopenharmony_ci do { 8568c2ecf20Sopenharmony_ci spin_lock_irq(&anchor->lock); 8578c2ecf20Sopenharmony_ci anchor->poisoned = 1; 8588c2ecf20Sopenharmony_ci while (!list_empty(&anchor->urb_list)) { 8598c2ecf20Sopenharmony_ci victim = list_entry(anchor->urb_list.prev, 8608c2ecf20Sopenharmony_ci struct urb, anchor_list); 8618c2ecf20Sopenharmony_ci /* make sure the URB isn't freed before we kill it */ 8628c2ecf20Sopenharmony_ci usb_get_urb(victim); 8638c2ecf20Sopenharmony_ci spin_unlock_irq(&anchor->lock); 8648c2ecf20Sopenharmony_ci /* this will unanchor the URB */ 8658c2ecf20Sopenharmony_ci usb_poison_urb(victim); 8668c2ecf20Sopenharmony_ci usb_put_urb(victim); 8678c2ecf20Sopenharmony_ci spin_lock_irq(&anchor->lock); 8688c2ecf20Sopenharmony_ci } 8698c2ecf20Sopenharmony_ci surely_empty = usb_anchor_check_wakeup(anchor); 8708c2ecf20Sopenharmony_ci 8718c2ecf20Sopenharmony_ci spin_unlock_irq(&anchor->lock); 8728c2ecf20Sopenharmony_ci cpu_relax(); 8738c2ecf20Sopenharmony_ci } while (!surely_empty); 8748c2ecf20Sopenharmony_ci} 8758c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_poison_anchored_urbs); 8768c2ecf20Sopenharmony_ci 8778c2ecf20Sopenharmony_ci/** 8788c2ecf20Sopenharmony_ci * usb_unpoison_anchored_urbs - let an anchor be used successfully again 8798c2ecf20Sopenharmony_ci * @anchor: anchor the requests are bound to 8808c2ecf20Sopenharmony_ci * 8818c2ecf20Sopenharmony_ci * Reverses the effect of usb_poison_anchored_urbs 8828c2ecf20Sopenharmony_ci * the anchor can be used normally after it returns 8838c2ecf20Sopenharmony_ci */ 8848c2ecf20Sopenharmony_civoid usb_unpoison_anchored_urbs(struct usb_anchor *anchor) 8858c2ecf20Sopenharmony_ci{ 8868c2ecf20Sopenharmony_ci unsigned long flags; 8878c2ecf20Sopenharmony_ci struct urb *lazarus; 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_ci spin_lock_irqsave(&anchor->lock, flags); 8908c2ecf20Sopenharmony_ci list_for_each_entry(lazarus, &anchor->urb_list, anchor_list) { 8918c2ecf20Sopenharmony_ci usb_unpoison_urb(lazarus); 8928c2ecf20Sopenharmony_ci } 8938c2ecf20Sopenharmony_ci anchor->poisoned = 0; 8948c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&anchor->lock, flags); 8958c2ecf20Sopenharmony_ci} 8968c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_unpoison_anchored_urbs); 8978c2ecf20Sopenharmony_ci/** 8988c2ecf20Sopenharmony_ci * usb_unlink_anchored_urbs - asynchronously cancel transfer requests en masse 8998c2ecf20Sopenharmony_ci * @anchor: anchor the requests are bound to 9008c2ecf20Sopenharmony_ci * 9018c2ecf20Sopenharmony_ci * this allows all outstanding URBs to be unlinked starting 9028c2ecf20Sopenharmony_ci * from the back of the queue. This function is asynchronous. 9038c2ecf20Sopenharmony_ci * The unlinking is just triggered. It may happen after this 9048c2ecf20Sopenharmony_ci * function has returned. 9058c2ecf20Sopenharmony_ci * 9068c2ecf20Sopenharmony_ci * This routine should not be called by a driver after its disconnect 9078c2ecf20Sopenharmony_ci * method has returned. 9088c2ecf20Sopenharmony_ci */ 9098c2ecf20Sopenharmony_civoid usb_unlink_anchored_urbs(struct usb_anchor *anchor) 9108c2ecf20Sopenharmony_ci{ 9118c2ecf20Sopenharmony_ci struct urb *victim; 9128c2ecf20Sopenharmony_ci 9138c2ecf20Sopenharmony_ci while ((victim = usb_get_from_anchor(anchor)) != NULL) { 9148c2ecf20Sopenharmony_ci usb_unlink_urb(victim); 9158c2ecf20Sopenharmony_ci usb_put_urb(victim); 9168c2ecf20Sopenharmony_ci } 9178c2ecf20Sopenharmony_ci} 9188c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_unlink_anchored_urbs); 9198c2ecf20Sopenharmony_ci 9208c2ecf20Sopenharmony_ci/** 9218c2ecf20Sopenharmony_ci * usb_anchor_suspend_wakeups 9228c2ecf20Sopenharmony_ci * @anchor: the anchor you want to suspend wakeups on 9238c2ecf20Sopenharmony_ci * 9248c2ecf20Sopenharmony_ci * Call this to stop the last urb being unanchored from waking up any 9258c2ecf20Sopenharmony_ci * usb_wait_anchor_empty_timeout waiters. This is used in the hcd urb give- 9268c2ecf20Sopenharmony_ci * back path to delay waking up until after the completion handler has run. 9278c2ecf20Sopenharmony_ci */ 9288c2ecf20Sopenharmony_civoid usb_anchor_suspend_wakeups(struct usb_anchor *anchor) 9298c2ecf20Sopenharmony_ci{ 9308c2ecf20Sopenharmony_ci if (anchor) 9318c2ecf20Sopenharmony_ci atomic_inc(&anchor->suspend_wakeups); 9328c2ecf20Sopenharmony_ci} 9338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_anchor_suspend_wakeups); 9348c2ecf20Sopenharmony_ci 9358c2ecf20Sopenharmony_ci/** 9368c2ecf20Sopenharmony_ci * usb_anchor_resume_wakeups 9378c2ecf20Sopenharmony_ci * @anchor: the anchor you want to resume wakeups on 9388c2ecf20Sopenharmony_ci * 9398c2ecf20Sopenharmony_ci * Allow usb_wait_anchor_empty_timeout waiters to be woken up again, and 9408c2ecf20Sopenharmony_ci * wake up any current waiters if the anchor is empty. 9418c2ecf20Sopenharmony_ci */ 9428c2ecf20Sopenharmony_civoid usb_anchor_resume_wakeups(struct usb_anchor *anchor) 9438c2ecf20Sopenharmony_ci{ 9448c2ecf20Sopenharmony_ci if (!anchor) 9458c2ecf20Sopenharmony_ci return; 9468c2ecf20Sopenharmony_ci 9478c2ecf20Sopenharmony_ci atomic_dec(&anchor->suspend_wakeups); 9488c2ecf20Sopenharmony_ci if (usb_anchor_check_wakeup(anchor)) 9498c2ecf20Sopenharmony_ci wake_up(&anchor->wait); 9508c2ecf20Sopenharmony_ci} 9518c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_anchor_resume_wakeups); 9528c2ecf20Sopenharmony_ci 9538c2ecf20Sopenharmony_ci/** 9548c2ecf20Sopenharmony_ci * usb_wait_anchor_empty_timeout - wait for an anchor to be unused 9558c2ecf20Sopenharmony_ci * @anchor: the anchor you want to become unused 9568c2ecf20Sopenharmony_ci * @timeout: how long you are willing to wait in milliseconds 9578c2ecf20Sopenharmony_ci * 9588c2ecf20Sopenharmony_ci * Call this is you want to be sure all an anchor's 9598c2ecf20Sopenharmony_ci * URBs have finished 9608c2ecf20Sopenharmony_ci * 9618c2ecf20Sopenharmony_ci * Return: Non-zero if the anchor became unused. Zero on timeout. 9628c2ecf20Sopenharmony_ci */ 9638c2ecf20Sopenharmony_ciint usb_wait_anchor_empty_timeout(struct usb_anchor *anchor, 9648c2ecf20Sopenharmony_ci unsigned int timeout) 9658c2ecf20Sopenharmony_ci{ 9668c2ecf20Sopenharmony_ci return wait_event_timeout(anchor->wait, 9678c2ecf20Sopenharmony_ci usb_anchor_check_wakeup(anchor), 9688c2ecf20Sopenharmony_ci msecs_to_jiffies(timeout)); 9698c2ecf20Sopenharmony_ci} 9708c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_wait_anchor_empty_timeout); 9718c2ecf20Sopenharmony_ci 9728c2ecf20Sopenharmony_ci/** 9738c2ecf20Sopenharmony_ci * usb_get_from_anchor - get an anchor's oldest urb 9748c2ecf20Sopenharmony_ci * @anchor: the anchor whose urb you want 9758c2ecf20Sopenharmony_ci * 9768c2ecf20Sopenharmony_ci * This will take the oldest urb from an anchor, 9778c2ecf20Sopenharmony_ci * unanchor and return it 9788c2ecf20Sopenharmony_ci * 9798c2ecf20Sopenharmony_ci * Return: The oldest urb from @anchor, or %NULL if @anchor has no 9808c2ecf20Sopenharmony_ci * urbs associated with it. 9818c2ecf20Sopenharmony_ci */ 9828c2ecf20Sopenharmony_cistruct urb *usb_get_from_anchor(struct usb_anchor *anchor) 9838c2ecf20Sopenharmony_ci{ 9848c2ecf20Sopenharmony_ci struct urb *victim; 9858c2ecf20Sopenharmony_ci unsigned long flags; 9868c2ecf20Sopenharmony_ci 9878c2ecf20Sopenharmony_ci spin_lock_irqsave(&anchor->lock, flags); 9888c2ecf20Sopenharmony_ci if (!list_empty(&anchor->urb_list)) { 9898c2ecf20Sopenharmony_ci victim = list_entry(anchor->urb_list.next, struct urb, 9908c2ecf20Sopenharmony_ci anchor_list); 9918c2ecf20Sopenharmony_ci usb_get_urb(victim); 9928c2ecf20Sopenharmony_ci __usb_unanchor_urb(victim, anchor); 9938c2ecf20Sopenharmony_ci } else { 9948c2ecf20Sopenharmony_ci victim = NULL; 9958c2ecf20Sopenharmony_ci } 9968c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&anchor->lock, flags); 9978c2ecf20Sopenharmony_ci 9988c2ecf20Sopenharmony_ci return victim; 9998c2ecf20Sopenharmony_ci} 10008c2ecf20Sopenharmony_ci 10018c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_get_from_anchor); 10028c2ecf20Sopenharmony_ci 10038c2ecf20Sopenharmony_ci/** 10048c2ecf20Sopenharmony_ci * usb_scuttle_anchored_urbs - unanchor all an anchor's urbs 10058c2ecf20Sopenharmony_ci * @anchor: the anchor whose urbs you want to unanchor 10068c2ecf20Sopenharmony_ci * 10078c2ecf20Sopenharmony_ci * use this to get rid of all an anchor's urbs 10088c2ecf20Sopenharmony_ci */ 10098c2ecf20Sopenharmony_civoid usb_scuttle_anchored_urbs(struct usb_anchor *anchor) 10108c2ecf20Sopenharmony_ci{ 10118c2ecf20Sopenharmony_ci struct urb *victim; 10128c2ecf20Sopenharmony_ci unsigned long flags; 10138c2ecf20Sopenharmony_ci int surely_empty; 10148c2ecf20Sopenharmony_ci 10158c2ecf20Sopenharmony_ci do { 10168c2ecf20Sopenharmony_ci spin_lock_irqsave(&anchor->lock, flags); 10178c2ecf20Sopenharmony_ci while (!list_empty(&anchor->urb_list)) { 10188c2ecf20Sopenharmony_ci victim = list_entry(anchor->urb_list.prev, 10198c2ecf20Sopenharmony_ci struct urb, anchor_list); 10208c2ecf20Sopenharmony_ci __usb_unanchor_urb(victim, anchor); 10218c2ecf20Sopenharmony_ci } 10228c2ecf20Sopenharmony_ci surely_empty = usb_anchor_check_wakeup(anchor); 10238c2ecf20Sopenharmony_ci 10248c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&anchor->lock, flags); 10258c2ecf20Sopenharmony_ci cpu_relax(); 10268c2ecf20Sopenharmony_ci } while (!surely_empty); 10278c2ecf20Sopenharmony_ci} 10288c2ecf20Sopenharmony_ci 10298c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_scuttle_anchored_urbs); 10308c2ecf20Sopenharmony_ci 10318c2ecf20Sopenharmony_ci/** 10328c2ecf20Sopenharmony_ci * usb_anchor_empty - is an anchor empty 10338c2ecf20Sopenharmony_ci * @anchor: the anchor you want to query 10348c2ecf20Sopenharmony_ci * 10358c2ecf20Sopenharmony_ci * Return: 1 if the anchor has no urbs associated with it. 10368c2ecf20Sopenharmony_ci */ 10378c2ecf20Sopenharmony_ciint usb_anchor_empty(struct usb_anchor *anchor) 10388c2ecf20Sopenharmony_ci{ 10398c2ecf20Sopenharmony_ci return list_empty(&anchor->urb_list); 10408c2ecf20Sopenharmony_ci} 10418c2ecf20Sopenharmony_ci 10428c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_anchor_empty); 10438c2ecf20Sopenharmony_ci 1044