162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Released under the GPLv2 only. 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci#include <linux/module.h> 762306a36Sopenharmony_ci#include <linux/string.h> 862306a36Sopenharmony_ci#include <linux/bitops.h> 962306a36Sopenharmony_ci#include <linux/slab.h> 1062306a36Sopenharmony_ci#include <linux/log2.h> 1162306a36Sopenharmony_ci#include <linux/kmsan.h> 1262306a36Sopenharmony_ci#include <linux/usb.h> 1362306a36Sopenharmony_ci#include <linux/wait.h> 1462306a36Sopenharmony_ci#include <linux/usb/hcd.h> 1562306a36Sopenharmony_ci#include <linux/scatterlist.h> 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci#define to_urb(d) container_of(d, struct urb, kref) 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_cistatic void urb_destroy(struct kref *kref) 2162306a36Sopenharmony_ci{ 2262306a36Sopenharmony_ci struct urb *urb = to_urb(kref); 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci if (urb->transfer_flags & URB_FREE_BUFFER) 2562306a36Sopenharmony_ci kfree(urb->transfer_buffer); 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci kfree(urb); 2862306a36Sopenharmony_ci} 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci/** 3162306a36Sopenharmony_ci * usb_init_urb - initializes a urb so that it can be used by a USB driver 3262306a36Sopenharmony_ci * @urb: pointer to the urb to initialize 3362306a36Sopenharmony_ci * 3462306a36Sopenharmony_ci * Initializes a urb so that the USB subsystem can use it properly. 3562306a36Sopenharmony_ci * 3662306a36Sopenharmony_ci * If a urb is created with a call to usb_alloc_urb() it is not 3762306a36Sopenharmony_ci * necessary to call this function. Only use this if you allocate the 3862306a36Sopenharmony_ci * space for a struct urb on your own. If you call this function, be 3962306a36Sopenharmony_ci * careful when freeing the memory for your urb that it is no longer in 4062306a36Sopenharmony_ci * use by the USB core. 4162306a36Sopenharmony_ci * 4262306a36Sopenharmony_ci * Only use this function if you _really_ understand what you are doing. 4362306a36Sopenharmony_ci */ 4462306a36Sopenharmony_civoid usb_init_urb(struct urb *urb) 4562306a36Sopenharmony_ci{ 4662306a36Sopenharmony_ci if (urb) { 4762306a36Sopenharmony_ci memset(urb, 0, sizeof(*urb)); 4862306a36Sopenharmony_ci kref_init(&urb->kref); 4962306a36Sopenharmony_ci INIT_LIST_HEAD(&urb->urb_list); 5062306a36Sopenharmony_ci INIT_LIST_HEAD(&urb->anchor_list); 5162306a36Sopenharmony_ci } 5262306a36Sopenharmony_ci} 5362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_init_urb); 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci/** 5662306a36Sopenharmony_ci * usb_alloc_urb - creates a new urb for a USB driver to use 5762306a36Sopenharmony_ci * @iso_packets: number of iso packets for this urb 5862306a36Sopenharmony_ci * @mem_flags: the type of memory to allocate, see kmalloc() for a list of 5962306a36Sopenharmony_ci * valid options for this. 6062306a36Sopenharmony_ci * 6162306a36Sopenharmony_ci * Creates an urb for the USB driver to use, initializes a few internal 6262306a36Sopenharmony_ci * structures, increments the usage counter, and returns a pointer to it. 6362306a36Sopenharmony_ci * 6462306a36Sopenharmony_ci * If the driver want to use this urb for interrupt, control, or bulk 6562306a36Sopenharmony_ci * endpoints, pass '0' as the number of iso packets. 6662306a36Sopenharmony_ci * 6762306a36Sopenharmony_ci * The driver must call usb_free_urb() when it is finished with the urb. 6862306a36Sopenharmony_ci * 6962306a36Sopenharmony_ci * Return: A pointer to the new urb, or %NULL if no memory is available. 7062306a36Sopenharmony_ci */ 7162306a36Sopenharmony_cistruct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags) 7262306a36Sopenharmony_ci{ 7362306a36Sopenharmony_ci struct urb *urb; 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci urb = kmalloc(struct_size(urb, iso_frame_desc, iso_packets), 7662306a36Sopenharmony_ci mem_flags); 7762306a36Sopenharmony_ci if (!urb) 7862306a36Sopenharmony_ci return NULL; 7962306a36Sopenharmony_ci usb_init_urb(urb); 8062306a36Sopenharmony_ci return urb; 8162306a36Sopenharmony_ci} 8262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_alloc_urb); 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci/** 8562306a36Sopenharmony_ci * usb_free_urb - frees the memory used by a urb when all users of it are finished 8662306a36Sopenharmony_ci * @urb: pointer to the urb to free, may be NULL 8762306a36Sopenharmony_ci * 8862306a36Sopenharmony_ci * Must be called when a user of a urb is finished with it. When the last user 8962306a36Sopenharmony_ci * of the urb calls this function, the memory of the urb is freed. 9062306a36Sopenharmony_ci * 9162306a36Sopenharmony_ci * Note: The transfer buffer associated with the urb is not freed unless the 9262306a36Sopenharmony_ci * URB_FREE_BUFFER transfer flag is set. 9362306a36Sopenharmony_ci */ 9462306a36Sopenharmony_civoid usb_free_urb(struct urb *urb) 9562306a36Sopenharmony_ci{ 9662306a36Sopenharmony_ci if (urb) 9762306a36Sopenharmony_ci kref_put(&urb->kref, urb_destroy); 9862306a36Sopenharmony_ci} 9962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_free_urb); 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ci/** 10262306a36Sopenharmony_ci * usb_get_urb - increments the reference count of the urb 10362306a36Sopenharmony_ci * @urb: pointer to the urb to modify, may be NULL 10462306a36Sopenharmony_ci * 10562306a36Sopenharmony_ci * This must be called whenever a urb is transferred from a device driver to a 10662306a36Sopenharmony_ci * host controller driver. This allows proper reference counting to happen 10762306a36Sopenharmony_ci * for urbs. 10862306a36Sopenharmony_ci * 10962306a36Sopenharmony_ci * Return: A pointer to the urb with the incremented reference counter. 11062306a36Sopenharmony_ci */ 11162306a36Sopenharmony_cistruct urb *usb_get_urb(struct urb *urb) 11262306a36Sopenharmony_ci{ 11362306a36Sopenharmony_ci if (urb) 11462306a36Sopenharmony_ci kref_get(&urb->kref); 11562306a36Sopenharmony_ci return urb; 11662306a36Sopenharmony_ci} 11762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_get_urb); 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci/** 12062306a36Sopenharmony_ci * usb_anchor_urb - anchors an URB while it is processed 12162306a36Sopenharmony_ci * @urb: pointer to the urb to anchor 12262306a36Sopenharmony_ci * @anchor: pointer to the anchor 12362306a36Sopenharmony_ci * 12462306a36Sopenharmony_ci * This can be called to have access to URBs which are to be executed 12562306a36Sopenharmony_ci * without bothering to track them 12662306a36Sopenharmony_ci */ 12762306a36Sopenharmony_civoid usb_anchor_urb(struct urb *urb, struct usb_anchor *anchor) 12862306a36Sopenharmony_ci{ 12962306a36Sopenharmony_ci unsigned long flags; 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci spin_lock_irqsave(&anchor->lock, flags); 13262306a36Sopenharmony_ci usb_get_urb(urb); 13362306a36Sopenharmony_ci list_add_tail(&urb->anchor_list, &anchor->urb_list); 13462306a36Sopenharmony_ci urb->anchor = anchor; 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci if (unlikely(anchor->poisoned)) 13762306a36Sopenharmony_ci atomic_inc(&urb->reject); 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci spin_unlock_irqrestore(&anchor->lock, flags); 14062306a36Sopenharmony_ci} 14162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_anchor_urb); 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_cistatic int usb_anchor_check_wakeup(struct usb_anchor *anchor) 14462306a36Sopenharmony_ci{ 14562306a36Sopenharmony_ci return atomic_read(&anchor->suspend_wakeups) == 0 && 14662306a36Sopenharmony_ci list_empty(&anchor->urb_list); 14762306a36Sopenharmony_ci} 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ci/* Callers must hold anchor->lock */ 15062306a36Sopenharmony_cistatic void __usb_unanchor_urb(struct urb *urb, struct usb_anchor *anchor) 15162306a36Sopenharmony_ci{ 15262306a36Sopenharmony_ci urb->anchor = NULL; 15362306a36Sopenharmony_ci list_del(&urb->anchor_list); 15462306a36Sopenharmony_ci usb_put_urb(urb); 15562306a36Sopenharmony_ci if (usb_anchor_check_wakeup(anchor)) 15662306a36Sopenharmony_ci wake_up(&anchor->wait); 15762306a36Sopenharmony_ci} 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci/** 16062306a36Sopenharmony_ci * usb_unanchor_urb - unanchors an URB 16162306a36Sopenharmony_ci * @urb: pointer to the urb to anchor 16262306a36Sopenharmony_ci * 16362306a36Sopenharmony_ci * Call this to stop the system keeping track of this URB 16462306a36Sopenharmony_ci */ 16562306a36Sopenharmony_civoid usb_unanchor_urb(struct urb *urb) 16662306a36Sopenharmony_ci{ 16762306a36Sopenharmony_ci unsigned long flags; 16862306a36Sopenharmony_ci struct usb_anchor *anchor; 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci if (!urb) 17162306a36Sopenharmony_ci return; 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ci anchor = urb->anchor; 17462306a36Sopenharmony_ci if (!anchor) 17562306a36Sopenharmony_ci return; 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci spin_lock_irqsave(&anchor->lock, flags); 17862306a36Sopenharmony_ci /* 17962306a36Sopenharmony_ci * At this point, we could be competing with another thread which 18062306a36Sopenharmony_ci * has the same intention. To protect the urb from being unanchored 18162306a36Sopenharmony_ci * twice, only the winner of the race gets the job. 18262306a36Sopenharmony_ci */ 18362306a36Sopenharmony_ci if (likely(anchor == urb->anchor)) 18462306a36Sopenharmony_ci __usb_unanchor_urb(urb, anchor); 18562306a36Sopenharmony_ci spin_unlock_irqrestore(&anchor->lock, flags); 18662306a36Sopenharmony_ci} 18762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_unanchor_urb); 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci/*-------------------------------------------------------------------*/ 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_cistatic const int pipetypes[4] = { 19262306a36Sopenharmony_ci PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT 19362306a36Sopenharmony_ci}; 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci/** 19662306a36Sopenharmony_ci * usb_pipe_type_check - sanity check of a specific pipe for a usb device 19762306a36Sopenharmony_ci * @dev: struct usb_device to be checked 19862306a36Sopenharmony_ci * @pipe: pipe to check 19962306a36Sopenharmony_ci * 20062306a36Sopenharmony_ci * This performs a light-weight sanity check for the endpoint in the 20162306a36Sopenharmony_ci * given usb device. It returns 0 if the pipe is valid for the specific usb 20262306a36Sopenharmony_ci * device, otherwise a negative error code. 20362306a36Sopenharmony_ci */ 20462306a36Sopenharmony_ciint usb_pipe_type_check(struct usb_device *dev, unsigned int pipe) 20562306a36Sopenharmony_ci{ 20662306a36Sopenharmony_ci const struct usb_host_endpoint *ep; 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ci ep = usb_pipe_endpoint(dev, pipe); 20962306a36Sopenharmony_ci if (!ep) 21062306a36Sopenharmony_ci return -EINVAL; 21162306a36Sopenharmony_ci if (usb_pipetype(pipe) != pipetypes[usb_endpoint_type(&ep->desc)]) 21262306a36Sopenharmony_ci return -EINVAL; 21362306a36Sopenharmony_ci return 0; 21462306a36Sopenharmony_ci} 21562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_pipe_type_check); 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ci/** 21862306a36Sopenharmony_ci * usb_urb_ep_type_check - sanity check of endpoint in the given urb 21962306a36Sopenharmony_ci * @urb: urb to be checked 22062306a36Sopenharmony_ci * 22162306a36Sopenharmony_ci * This performs a light-weight sanity check for the endpoint in the 22262306a36Sopenharmony_ci * given urb. It returns 0 if the urb contains a valid endpoint, otherwise 22362306a36Sopenharmony_ci * a negative error code. 22462306a36Sopenharmony_ci */ 22562306a36Sopenharmony_ciint usb_urb_ep_type_check(const struct urb *urb) 22662306a36Sopenharmony_ci{ 22762306a36Sopenharmony_ci return usb_pipe_type_check(urb->dev, urb->pipe); 22862306a36Sopenharmony_ci} 22962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_urb_ep_type_check); 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci/** 23262306a36Sopenharmony_ci * usb_submit_urb - issue an asynchronous transfer request for an endpoint 23362306a36Sopenharmony_ci * @urb: pointer to the urb describing the request 23462306a36Sopenharmony_ci * @mem_flags: the type of memory to allocate, see kmalloc() for a list 23562306a36Sopenharmony_ci * of valid options for this. 23662306a36Sopenharmony_ci * 23762306a36Sopenharmony_ci * This submits a transfer request, and transfers control of the URB 23862306a36Sopenharmony_ci * describing that request to the USB subsystem. Request completion will 23962306a36Sopenharmony_ci * be indicated later, asynchronously, by calling the completion handler. 24062306a36Sopenharmony_ci * The three types of completion are success, error, and unlink 24162306a36Sopenharmony_ci * (a software-induced fault, also called "request cancellation"). 24262306a36Sopenharmony_ci * 24362306a36Sopenharmony_ci * URBs may be submitted in interrupt context. 24462306a36Sopenharmony_ci * 24562306a36Sopenharmony_ci * The caller must have correctly initialized the URB before submitting 24662306a36Sopenharmony_ci * it. Functions such as usb_fill_bulk_urb() and usb_fill_control_urb() are 24762306a36Sopenharmony_ci * available to ensure that most fields are correctly initialized, for 24862306a36Sopenharmony_ci * the particular kind of transfer, although they will not initialize 24962306a36Sopenharmony_ci * any transfer flags. 25062306a36Sopenharmony_ci * 25162306a36Sopenharmony_ci * If the submission is successful, the complete() callback from the URB 25262306a36Sopenharmony_ci * will be called exactly once, when the USB core and Host Controller Driver 25362306a36Sopenharmony_ci * (HCD) are finished with the URB. When the completion function is called, 25462306a36Sopenharmony_ci * control of the URB is returned to the device driver which issued the 25562306a36Sopenharmony_ci * request. The completion handler may then immediately free or reuse that 25662306a36Sopenharmony_ci * URB. 25762306a36Sopenharmony_ci * 25862306a36Sopenharmony_ci * With few exceptions, USB device drivers should never access URB fields 25962306a36Sopenharmony_ci * provided by usbcore or the HCD until its complete() is called. 26062306a36Sopenharmony_ci * The exceptions relate to periodic transfer scheduling. For both 26162306a36Sopenharmony_ci * interrupt and isochronous urbs, as part of successful URB submission 26262306a36Sopenharmony_ci * urb->interval is modified to reflect the actual transfer period used 26362306a36Sopenharmony_ci * (normally some power of two units). And for isochronous urbs, 26462306a36Sopenharmony_ci * urb->start_frame is modified to reflect when the URB's transfers were 26562306a36Sopenharmony_ci * scheduled to start. 26662306a36Sopenharmony_ci * 26762306a36Sopenharmony_ci * Not all isochronous transfer scheduling policies will work, but most 26862306a36Sopenharmony_ci * host controller drivers should easily handle ISO queues going from now 26962306a36Sopenharmony_ci * until 10-200 msec into the future. Drivers should try to keep at 27062306a36Sopenharmony_ci * least one or two msec of data in the queue; many controllers require 27162306a36Sopenharmony_ci * that new transfers start at least 1 msec in the future when they are 27262306a36Sopenharmony_ci * added. If the driver is unable to keep up and the queue empties out, 27362306a36Sopenharmony_ci * the behavior for new submissions is governed by the URB_ISO_ASAP flag. 27462306a36Sopenharmony_ci * If the flag is set, or if the queue is idle, then the URB is always 27562306a36Sopenharmony_ci * assigned to the first available (and not yet expired) slot in the 27662306a36Sopenharmony_ci * endpoint's schedule. If the flag is not set and the queue is active 27762306a36Sopenharmony_ci * then the URB is always assigned to the next slot in the schedule 27862306a36Sopenharmony_ci * following the end of the endpoint's previous URB, even if that slot is 27962306a36Sopenharmony_ci * in the past. When a packet is assigned in this way to a slot that has 28062306a36Sopenharmony_ci * already expired, the packet is not transmitted and the corresponding 28162306a36Sopenharmony_ci * usb_iso_packet_descriptor's status field will return -EXDEV. If this 28262306a36Sopenharmony_ci * would happen to all the packets in the URB, submission fails with a 28362306a36Sopenharmony_ci * -EXDEV error code. 28462306a36Sopenharmony_ci * 28562306a36Sopenharmony_ci * For control endpoints, the synchronous usb_control_msg() call is 28662306a36Sopenharmony_ci * often used (in non-interrupt context) instead of this call. 28762306a36Sopenharmony_ci * That is often used through convenience wrappers, for the requests 28862306a36Sopenharmony_ci * that are standardized in the USB 2.0 specification. For bulk 28962306a36Sopenharmony_ci * endpoints, a synchronous usb_bulk_msg() call is available. 29062306a36Sopenharmony_ci * 29162306a36Sopenharmony_ci * Return: 29262306a36Sopenharmony_ci * 0 on successful submissions. A negative error number otherwise. 29362306a36Sopenharmony_ci * 29462306a36Sopenharmony_ci * Request Queuing: 29562306a36Sopenharmony_ci * 29662306a36Sopenharmony_ci * URBs may be submitted to endpoints before previous ones complete, to 29762306a36Sopenharmony_ci * minimize the impact of interrupt latencies and system overhead on data 29862306a36Sopenharmony_ci * throughput. With that queuing policy, an endpoint's queue would never 29962306a36Sopenharmony_ci * be empty. This is required for continuous isochronous data streams, 30062306a36Sopenharmony_ci * and may also be required for some kinds of interrupt transfers. Such 30162306a36Sopenharmony_ci * queuing also maximizes bandwidth utilization by letting USB controllers 30262306a36Sopenharmony_ci * start work on later requests before driver software has finished the 30362306a36Sopenharmony_ci * completion processing for earlier (successful) requests. 30462306a36Sopenharmony_ci * 30562306a36Sopenharmony_ci * As of Linux 2.6, all USB endpoint transfer queues support depths greater 30662306a36Sopenharmony_ci * than one. This was previously a HCD-specific behavior, except for ISO 30762306a36Sopenharmony_ci * transfers. Non-isochronous endpoint queues are inactive during cleanup 30862306a36Sopenharmony_ci * after faults (transfer errors or cancellation). 30962306a36Sopenharmony_ci * 31062306a36Sopenharmony_ci * Reserved Bandwidth Transfers: 31162306a36Sopenharmony_ci * 31262306a36Sopenharmony_ci * Periodic transfers (interrupt or isochronous) are performed repeatedly, 31362306a36Sopenharmony_ci * using the interval specified in the urb. Submitting the first urb to 31462306a36Sopenharmony_ci * the endpoint reserves the bandwidth necessary to make those transfers. 31562306a36Sopenharmony_ci * If the USB subsystem can't allocate sufficient bandwidth to perform 31662306a36Sopenharmony_ci * the periodic request, submitting such a periodic request should fail. 31762306a36Sopenharmony_ci * 31862306a36Sopenharmony_ci * For devices under xHCI, the bandwidth is reserved at configuration time, or 31962306a36Sopenharmony_ci * when the alt setting is selected. If there is not enough bus bandwidth, the 32062306a36Sopenharmony_ci * configuration/alt setting request will fail. Therefore, submissions to 32162306a36Sopenharmony_ci * periodic endpoints on devices under xHCI should never fail due to bandwidth 32262306a36Sopenharmony_ci * constraints. 32362306a36Sopenharmony_ci * 32462306a36Sopenharmony_ci * Device drivers must explicitly request that repetition, by ensuring that 32562306a36Sopenharmony_ci * some URB is always on the endpoint's queue (except possibly for short 32662306a36Sopenharmony_ci * periods during completion callbacks). When there is no longer an urb 32762306a36Sopenharmony_ci * queued, the endpoint's bandwidth reservation is canceled. This means 32862306a36Sopenharmony_ci * drivers can use their completion handlers to ensure they keep bandwidth 32962306a36Sopenharmony_ci * they need, by reinitializing and resubmitting the just-completed urb 33062306a36Sopenharmony_ci * until the driver longer needs that periodic bandwidth. 33162306a36Sopenharmony_ci * 33262306a36Sopenharmony_ci * Memory Flags: 33362306a36Sopenharmony_ci * 33462306a36Sopenharmony_ci * The general rules for how to decide which mem_flags to use 33562306a36Sopenharmony_ci * are the same as for kmalloc. There are four 33662306a36Sopenharmony_ci * different possible values; GFP_KERNEL, GFP_NOFS, GFP_NOIO and 33762306a36Sopenharmony_ci * GFP_ATOMIC. 33862306a36Sopenharmony_ci * 33962306a36Sopenharmony_ci * GFP_NOFS is not ever used, as it has not been implemented yet. 34062306a36Sopenharmony_ci * 34162306a36Sopenharmony_ci * GFP_ATOMIC is used when 34262306a36Sopenharmony_ci * (a) you are inside a completion handler, an interrupt, bottom half, 34362306a36Sopenharmony_ci * tasklet or timer, or 34462306a36Sopenharmony_ci * (b) you are holding a spinlock or rwlock (does not apply to 34562306a36Sopenharmony_ci * semaphores), or 34662306a36Sopenharmony_ci * (c) current->state != TASK_RUNNING, this is the case only after 34762306a36Sopenharmony_ci * you've changed it. 34862306a36Sopenharmony_ci * 34962306a36Sopenharmony_ci * GFP_NOIO is used in the block io path and error handling of storage 35062306a36Sopenharmony_ci * devices. 35162306a36Sopenharmony_ci * 35262306a36Sopenharmony_ci * All other situations use GFP_KERNEL. 35362306a36Sopenharmony_ci * 35462306a36Sopenharmony_ci * Some more specific rules for mem_flags can be inferred, such as 35562306a36Sopenharmony_ci * (1) start_xmit, timeout, and receive methods of network drivers must 35662306a36Sopenharmony_ci * use GFP_ATOMIC (they are called with a spinlock held); 35762306a36Sopenharmony_ci * (2) queuecommand methods of scsi drivers must use GFP_ATOMIC (also 35862306a36Sopenharmony_ci * called with a spinlock held); 35962306a36Sopenharmony_ci * (3) If you use a kernel thread with a network driver you must use 36062306a36Sopenharmony_ci * GFP_NOIO, unless (b) or (c) apply; 36162306a36Sopenharmony_ci * (4) after you have done a down() you can use GFP_KERNEL, unless (b) or (c) 36262306a36Sopenharmony_ci * apply or your are in a storage driver's block io path; 36362306a36Sopenharmony_ci * (5) USB probe and disconnect can use GFP_KERNEL unless (b) or (c) apply; and 36462306a36Sopenharmony_ci * (6) changing firmware on a running storage or net device uses 36562306a36Sopenharmony_ci * GFP_NOIO, unless b) or c) apply 36662306a36Sopenharmony_ci * 36762306a36Sopenharmony_ci */ 36862306a36Sopenharmony_ciint usb_submit_urb(struct urb *urb, gfp_t mem_flags) 36962306a36Sopenharmony_ci{ 37062306a36Sopenharmony_ci int xfertype, max; 37162306a36Sopenharmony_ci struct usb_device *dev; 37262306a36Sopenharmony_ci struct usb_host_endpoint *ep; 37362306a36Sopenharmony_ci int is_out; 37462306a36Sopenharmony_ci unsigned int allowed; 37562306a36Sopenharmony_ci 37662306a36Sopenharmony_ci if (!urb || !urb->complete) 37762306a36Sopenharmony_ci return -EINVAL; 37862306a36Sopenharmony_ci if (urb->hcpriv) { 37962306a36Sopenharmony_ci WARN_ONCE(1, "URB %pK submitted while active\n", urb); 38062306a36Sopenharmony_ci return -EBUSY; 38162306a36Sopenharmony_ci } 38262306a36Sopenharmony_ci 38362306a36Sopenharmony_ci dev = urb->dev; 38462306a36Sopenharmony_ci if ((!dev) || (dev->state < USB_STATE_UNAUTHENTICATED)) 38562306a36Sopenharmony_ci return -ENODEV; 38662306a36Sopenharmony_ci 38762306a36Sopenharmony_ci /* For now, get the endpoint from the pipe. Eventually drivers 38862306a36Sopenharmony_ci * will be required to set urb->ep directly and we will eliminate 38962306a36Sopenharmony_ci * urb->pipe. 39062306a36Sopenharmony_ci */ 39162306a36Sopenharmony_ci ep = usb_pipe_endpoint(dev, urb->pipe); 39262306a36Sopenharmony_ci if (!ep) 39362306a36Sopenharmony_ci return -ENOENT; 39462306a36Sopenharmony_ci 39562306a36Sopenharmony_ci urb->ep = ep; 39662306a36Sopenharmony_ci urb->status = -EINPROGRESS; 39762306a36Sopenharmony_ci urb->actual_length = 0; 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_ci /* Lots of sanity checks, so HCDs can rely on clean data 40062306a36Sopenharmony_ci * and don't need to duplicate tests 40162306a36Sopenharmony_ci */ 40262306a36Sopenharmony_ci xfertype = usb_endpoint_type(&ep->desc); 40362306a36Sopenharmony_ci if (xfertype == USB_ENDPOINT_XFER_CONTROL) { 40462306a36Sopenharmony_ci struct usb_ctrlrequest *setup = 40562306a36Sopenharmony_ci (struct usb_ctrlrequest *) urb->setup_packet; 40662306a36Sopenharmony_ci 40762306a36Sopenharmony_ci if (!setup) 40862306a36Sopenharmony_ci return -ENOEXEC; 40962306a36Sopenharmony_ci is_out = !(setup->bRequestType & USB_DIR_IN) || 41062306a36Sopenharmony_ci !setup->wLength; 41162306a36Sopenharmony_ci dev_WARN_ONCE(&dev->dev, (usb_pipeout(urb->pipe) != is_out), 41262306a36Sopenharmony_ci "BOGUS control dir, pipe %x doesn't match bRequestType %x\n", 41362306a36Sopenharmony_ci urb->pipe, setup->bRequestType); 41462306a36Sopenharmony_ci if (le16_to_cpu(setup->wLength) != urb->transfer_buffer_length) { 41562306a36Sopenharmony_ci dev_dbg(&dev->dev, "BOGUS control len %d doesn't match transfer length %d\n", 41662306a36Sopenharmony_ci le16_to_cpu(setup->wLength), 41762306a36Sopenharmony_ci urb->transfer_buffer_length); 41862306a36Sopenharmony_ci return -EBADR; 41962306a36Sopenharmony_ci } 42062306a36Sopenharmony_ci } else { 42162306a36Sopenharmony_ci is_out = usb_endpoint_dir_out(&ep->desc); 42262306a36Sopenharmony_ci } 42362306a36Sopenharmony_ci 42462306a36Sopenharmony_ci /* Clear the internal flags and cache the direction for later use */ 42562306a36Sopenharmony_ci urb->transfer_flags &= ~(URB_DIR_MASK | URB_DMA_MAP_SINGLE | 42662306a36Sopenharmony_ci URB_DMA_MAP_PAGE | URB_DMA_MAP_SG | URB_MAP_LOCAL | 42762306a36Sopenharmony_ci URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL | 42862306a36Sopenharmony_ci URB_DMA_SG_COMBINED); 42962306a36Sopenharmony_ci urb->transfer_flags |= (is_out ? URB_DIR_OUT : URB_DIR_IN); 43062306a36Sopenharmony_ci kmsan_handle_urb(urb, is_out); 43162306a36Sopenharmony_ci 43262306a36Sopenharmony_ci if (xfertype != USB_ENDPOINT_XFER_CONTROL && 43362306a36Sopenharmony_ci dev->state < USB_STATE_CONFIGURED) 43462306a36Sopenharmony_ci return -ENODEV; 43562306a36Sopenharmony_ci 43662306a36Sopenharmony_ci max = usb_endpoint_maxp(&ep->desc); 43762306a36Sopenharmony_ci if (max <= 0) { 43862306a36Sopenharmony_ci dev_dbg(&dev->dev, 43962306a36Sopenharmony_ci "bogus endpoint ep%d%s in %s (bad maxpacket %d)\n", 44062306a36Sopenharmony_ci usb_endpoint_num(&ep->desc), is_out ? "out" : "in", 44162306a36Sopenharmony_ci __func__, max); 44262306a36Sopenharmony_ci return -EMSGSIZE; 44362306a36Sopenharmony_ci } 44462306a36Sopenharmony_ci 44562306a36Sopenharmony_ci /* periodic transfers limit size per frame/uframe, 44662306a36Sopenharmony_ci * but drivers only control those sizes for ISO. 44762306a36Sopenharmony_ci * while we're checking, initialize return status. 44862306a36Sopenharmony_ci */ 44962306a36Sopenharmony_ci if (xfertype == USB_ENDPOINT_XFER_ISOC) { 45062306a36Sopenharmony_ci int n, len; 45162306a36Sopenharmony_ci 45262306a36Sopenharmony_ci /* SuperSpeed isoc endpoints have up to 16 bursts of up to 45362306a36Sopenharmony_ci * 3 packets each 45462306a36Sopenharmony_ci */ 45562306a36Sopenharmony_ci if (dev->speed >= USB_SPEED_SUPER) { 45662306a36Sopenharmony_ci int burst = 1 + ep->ss_ep_comp.bMaxBurst; 45762306a36Sopenharmony_ci int mult = USB_SS_MULT(ep->ss_ep_comp.bmAttributes); 45862306a36Sopenharmony_ci max *= burst; 45962306a36Sopenharmony_ci max *= mult; 46062306a36Sopenharmony_ci } 46162306a36Sopenharmony_ci 46262306a36Sopenharmony_ci if (dev->speed == USB_SPEED_SUPER_PLUS && 46362306a36Sopenharmony_ci USB_SS_SSP_ISOC_COMP(ep->ss_ep_comp.bmAttributes)) { 46462306a36Sopenharmony_ci struct usb_ssp_isoc_ep_comp_descriptor *isoc_ep_comp; 46562306a36Sopenharmony_ci 46662306a36Sopenharmony_ci isoc_ep_comp = &ep->ssp_isoc_ep_comp; 46762306a36Sopenharmony_ci max = le32_to_cpu(isoc_ep_comp->dwBytesPerInterval); 46862306a36Sopenharmony_ci } 46962306a36Sopenharmony_ci 47062306a36Sopenharmony_ci /* "high bandwidth" mode, 1-3 packets/uframe? */ 47162306a36Sopenharmony_ci if (dev->speed == USB_SPEED_HIGH) 47262306a36Sopenharmony_ci max *= usb_endpoint_maxp_mult(&ep->desc); 47362306a36Sopenharmony_ci 47462306a36Sopenharmony_ci if (urb->number_of_packets <= 0) 47562306a36Sopenharmony_ci return -EINVAL; 47662306a36Sopenharmony_ci for (n = 0; n < urb->number_of_packets; n++) { 47762306a36Sopenharmony_ci len = urb->iso_frame_desc[n].length; 47862306a36Sopenharmony_ci if (len < 0 || len > max) 47962306a36Sopenharmony_ci return -EMSGSIZE; 48062306a36Sopenharmony_ci urb->iso_frame_desc[n].status = -EXDEV; 48162306a36Sopenharmony_ci urb->iso_frame_desc[n].actual_length = 0; 48262306a36Sopenharmony_ci } 48362306a36Sopenharmony_ci } else if (urb->num_sgs && !urb->dev->bus->no_sg_constraint) { 48462306a36Sopenharmony_ci struct scatterlist *sg; 48562306a36Sopenharmony_ci int i; 48662306a36Sopenharmony_ci 48762306a36Sopenharmony_ci for_each_sg(urb->sg, sg, urb->num_sgs - 1, i) 48862306a36Sopenharmony_ci if (sg->length % max) 48962306a36Sopenharmony_ci return -EINVAL; 49062306a36Sopenharmony_ci } 49162306a36Sopenharmony_ci 49262306a36Sopenharmony_ci /* the I/O buffer must be mapped/unmapped, except when length=0 */ 49362306a36Sopenharmony_ci if (urb->transfer_buffer_length > INT_MAX) 49462306a36Sopenharmony_ci return -EMSGSIZE; 49562306a36Sopenharmony_ci 49662306a36Sopenharmony_ci /* 49762306a36Sopenharmony_ci * stuff that drivers shouldn't do, but which shouldn't 49862306a36Sopenharmony_ci * cause problems in HCDs if they get it wrong. 49962306a36Sopenharmony_ci */ 50062306a36Sopenharmony_ci 50162306a36Sopenharmony_ci /* Check that the pipe's type matches the endpoint's type */ 50262306a36Sopenharmony_ci if (usb_pipe_type_check(urb->dev, urb->pipe)) 50362306a36Sopenharmony_ci dev_WARN(&dev->dev, "BOGUS urb xfer, pipe %x != type %x\n", 50462306a36Sopenharmony_ci usb_pipetype(urb->pipe), pipetypes[xfertype]); 50562306a36Sopenharmony_ci 50662306a36Sopenharmony_ci /* Check against a simple/standard policy */ 50762306a36Sopenharmony_ci allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT | URB_DIR_MASK | 50862306a36Sopenharmony_ci URB_FREE_BUFFER); 50962306a36Sopenharmony_ci switch (xfertype) { 51062306a36Sopenharmony_ci case USB_ENDPOINT_XFER_BULK: 51162306a36Sopenharmony_ci case USB_ENDPOINT_XFER_INT: 51262306a36Sopenharmony_ci if (is_out) 51362306a36Sopenharmony_ci allowed |= URB_ZERO_PACKET; 51462306a36Sopenharmony_ci fallthrough; 51562306a36Sopenharmony_ci default: /* all non-iso endpoints */ 51662306a36Sopenharmony_ci if (!is_out) 51762306a36Sopenharmony_ci allowed |= URB_SHORT_NOT_OK; 51862306a36Sopenharmony_ci break; 51962306a36Sopenharmony_ci case USB_ENDPOINT_XFER_ISOC: 52062306a36Sopenharmony_ci allowed |= URB_ISO_ASAP; 52162306a36Sopenharmony_ci break; 52262306a36Sopenharmony_ci } 52362306a36Sopenharmony_ci allowed &= urb->transfer_flags; 52462306a36Sopenharmony_ci 52562306a36Sopenharmony_ci /* warn if submitter gave bogus flags */ 52662306a36Sopenharmony_ci if (allowed != urb->transfer_flags) 52762306a36Sopenharmony_ci dev_WARN(&dev->dev, "BOGUS urb flags, %x --> %x\n", 52862306a36Sopenharmony_ci urb->transfer_flags, allowed); 52962306a36Sopenharmony_ci 53062306a36Sopenharmony_ci /* 53162306a36Sopenharmony_ci * Force periodic transfer intervals to be legal values that are 53262306a36Sopenharmony_ci * a power of two (so HCDs don't need to). 53362306a36Sopenharmony_ci * 53462306a36Sopenharmony_ci * FIXME want bus->{intr,iso}_sched_horizon values here. Each HC 53562306a36Sopenharmony_ci * supports different values... this uses EHCI/UHCI defaults (and 53662306a36Sopenharmony_ci * EHCI can use smaller non-default values). 53762306a36Sopenharmony_ci */ 53862306a36Sopenharmony_ci switch (xfertype) { 53962306a36Sopenharmony_ci case USB_ENDPOINT_XFER_ISOC: 54062306a36Sopenharmony_ci case USB_ENDPOINT_XFER_INT: 54162306a36Sopenharmony_ci /* too small? */ 54262306a36Sopenharmony_ci if (urb->interval <= 0) 54362306a36Sopenharmony_ci return -EINVAL; 54462306a36Sopenharmony_ci 54562306a36Sopenharmony_ci /* too big? */ 54662306a36Sopenharmony_ci switch (dev->speed) { 54762306a36Sopenharmony_ci case USB_SPEED_SUPER_PLUS: 54862306a36Sopenharmony_ci case USB_SPEED_SUPER: /* units are 125us */ 54962306a36Sopenharmony_ci /* Handle up to 2^(16-1) microframes */ 55062306a36Sopenharmony_ci if (urb->interval > (1 << 15)) 55162306a36Sopenharmony_ci return -EINVAL; 55262306a36Sopenharmony_ci max = 1 << 15; 55362306a36Sopenharmony_ci break; 55462306a36Sopenharmony_ci case USB_SPEED_HIGH: /* units are microframes */ 55562306a36Sopenharmony_ci /* NOTE usb handles 2^15 */ 55662306a36Sopenharmony_ci if (urb->interval > (1024 * 8)) 55762306a36Sopenharmony_ci urb->interval = 1024 * 8; 55862306a36Sopenharmony_ci max = 1024 * 8; 55962306a36Sopenharmony_ci break; 56062306a36Sopenharmony_ci case USB_SPEED_FULL: /* units are frames/msec */ 56162306a36Sopenharmony_ci case USB_SPEED_LOW: 56262306a36Sopenharmony_ci if (xfertype == USB_ENDPOINT_XFER_INT) { 56362306a36Sopenharmony_ci if (urb->interval > 255) 56462306a36Sopenharmony_ci return -EINVAL; 56562306a36Sopenharmony_ci /* NOTE ohci only handles up to 32 */ 56662306a36Sopenharmony_ci max = 128; 56762306a36Sopenharmony_ci } else { 56862306a36Sopenharmony_ci if (urb->interval > 1024) 56962306a36Sopenharmony_ci urb->interval = 1024; 57062306a36Sopenharmony_ci /* NOTE usb and ohci handle up to 2^15 */ 57162306a36Sopenharmony_ci max = 1024; 57262306a36Sopenharmony_ci } 57362306a36Sopenharmony_ci break; 57462306a36Sopenharmony_ci default: 57562306a36Sopenharmony_ci return -EINVAL; 57662306a36Sopenharmony_ci } 57762306a36Sopenharmony_ci /* Round down to a power of 2, no more than max */ 57862306a36Sopenharmony_ci urb->interval = min(max, 1 << ilog2(urb->interval)); 57962306a36Sopenharmony_ci } 58062306a36Sopenharmony_ci 58162306a36Sopenharmony_ci return usb_hcd_submit_urb(urb, mem_flags); 58262306a36Sopenharmony_ci} 58362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_submit_urb); 58462306a36Sopenharmony_ci 58562306a36Sopenharmony_ci/*-------------------------------------------------------------------*/ 58662306a36Sopenharmony_ci 58762306a36Sopenharmony_ci/** 58862306a36Sopenharmony_ci * usb_unlink_urb - abort/cancel a transfer request for an endpoint 58962306a36Sopenharmony_ci * @urb: pointer to urb describing a previously submitted request, 59062306a36Sopenharmony_ci * may be NULL 59162306a36Sopenharmony_ci * 59262306a36Sopenharmony_ci * This routine cancels an in-progress request. URBs complete only once 59362306a36Sopenharmony_ci * per submission, and may be canceled only once per submission. 59462306a36Sopenharmony_ci * Successful cancellation means termination of @urb will be expedited 59562306a36Sopenharmony_ci * and the completion handler will be called with a status code 59662306a36Sopenharmony_ci * indicating that the request has been canceled (rather than any other 59762306a36Sopenharmony_ci * code). 59862306a36Sopenharmony_ci * 59962306a36Sopenharmony_ci * Drivers should not call this routine or related routines, such as 60062306a36Sopenharmony_ci * usb_kill_urb() or usb_unlink_anchored_urbs(), after their disconnect 60162306a36Sopenharmony_ci * method has returned. The disconnect function should synchronize with 60262306a36Sopenharmony_ci * a driver's I/O routines to insure that all URB-related activity has 60362306a36Sopenharmony_ci * completed before it returns. 60462306a36Sopenharmony_ci * 60562306a36Sopenharmony_ci * This request is asynchronous, however the HCD might call the ->complete() 60662306a36Sopenharmony_ci * callback during unlink. Therefore when drivers call usb_unlink_urb(), they 60762306a36Sopenharmony_ci * must not hold any locks that may be taken by the completion function. 60862306a36Sopenharmony_ci * Success is indicated by returning -EINPROGRESS, at which time the URB will 60962306a36Sopenharmony_ci * probably not yet have been given back to the device driver. When it is 61062306a36Sopenharmony_ci * eventually called, the completion function will see @urb->status == 61162306a36Sopenharmony_ci * -ECONNRESET. 61262306a36Sopenharmony_ci * Failure is indicated by usb_unlink_urb() returning any other value. 61362306a36Sopenharmony_ci * Unlinking will fail when @urb is not currently "linked" (i.e., it was 61462306a36Sopenharmony_ci * never submitted, or it was unlinked before, or the hardware is already 61562306a36Sopenharmony_ci * finished with it), even if the completion handler has not yet run. 61662306a36Sopenharmony_ci * 61762306a36Sopenharmony_ci * The URB must not be deallocated while this routine is running. In 61862306a36Sopenharmony_ci * particular, when a driver calls this routine, it must insure that the 61962306a36Sopenharmony_ci * completion handler cannot deallocate the URB. 62062306a36Sopenharmony_ci * 62162306a36Sopenharmony_ci * Return: -EINPROGRESS on success. See description for other values on 62262306a36Sopenharmony_ci * failure. 62362306a36Sopenharmony_ci * 62462306a36Sopenharmony_ci * Unlinking and Endpoint Queues: 62562306a36Sopenharmony_ci * 62662306a36Sopenharmony_ci * [The behaviors and guarantees described below do not apply to virtual 62762306a36Sopenharmony_ci * root hubs but only to endpoint queues for physical USB devices.] 62862306a36Sopenharmony_ci * 62962306a36Sopenharmony_ci * Host Controller Drivers (HCDs) place all the URBs for a particular 63062306a36Sopenharmony_ci * endpoint in a queue. Normally the queue advances as the controller 63162306a36Sopenharmony_ci * hardware processes each request. But when an URB terminates with an 63262306a36Sopenharmony_ci * error its queue generally stops (see below), at least until that URB's 63362306a36Sopenharmony_ci * completion routine returns. It is guaranteed that a stopped queue 63462306a36Sopenharmony_ci * will not restart until all its unlinked URBs have been fully retired, 63562306a36Sopenharmony_ci * with their completion routines run, even if that's not until some time 63662306a36Sopenharmony_ci * after the original completion handler returns. The same behavior and 63762306a36Sopenharmony_ci * guarantee apply when an URB terminates because it was unlinked. 63862306a36Sopenharmony_ci * 63962306a36Sopenharmony_ci * Bulk and interrupt endpoint queues are guaranteed to stop whenever an 64062306a36Sopenharmony_ci * URB terminates with any sort of error, including -ECONNRESET, -ENOENT, 64162306a36Sopenharmony_ci * and -EREMOTEIO. Control endpoint queues behave the same way except 64262306a36Sopenharmony_ci * that they are not guaranteed to stop for -EREMOTEIO errors. Queues 64362306a36Sopenharmony_ci * for isochronous endpoints are treated differently, because they must 64462306a36Sopenharmony_ci * advance at fixed rates. Such queues do not stop when an URB 64562306a36Sopenharmony_ci * encounters an error or is unlinked. An unlinked isochronous URB may 64662306a36Sopenharmony_ci * leave a gap in the stream of packets; it is undefined whether such 64762306a36Sopenharmony_ci * gaps can be filled in. 64862306a36Sopenharmony_ci * 64962306a36Sopenharmony_ci * Note that early termination of an URB because a short packet was 65062306a36Sopenharmony_ci * received will generate a -EREMOTEIO error if and only if the 65162306a36Sopenharmony_ci * URB_SHORT_NOT_OK flag is set. By setting this flag, USB device 65262306a36Sopenharmony_ci * drivers can build deep queues for large or complex bulk transfers 65362306a36Sopenharmony_ci * and clean them up reliably after any sort of aborted transfer by 65462306a36Sopenharmony_ci * unlinking all pending URBs at the first fault. 65562306a36Sopenharmony_ci * 65662306a36Sopenharmony_ci * When a control URB terminates with an error other than -EREMOTEIO, it 65762306a36Sopenharmony_ci * is quite likely that the status stage of the transfer will not take 65862306a36Sopenharmony_ci * place. 65962306a36Sopenharmony_ci */ 66062306a36Sopenharmony_ciint usb_unlink_urb(struct urb *urb) 66162306a36Sopenharmony_ci{ 66262306a36Sopenharmony_ci if (!urb) 66362306a36Sopenharmony_ci return -EINVAL; 66462306a36Sopenharmony_ci if (!urb->dev) 66562306a36Sopenharmony_ci return -ENODEV; 66662306a36Sopenharmony_ci if (!urb->ep) 66762306a36Sopenharmony_ci return -EIDRM; 66862306a36Sopenharmony_ci return usb_hcd_unlink_urb(urb, -ECONNRESET); 66962306a36Sopenharmony_ci} 67062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_unlink_urb); 67162306a36Sopenharmony_ci 67262306a36Sopenharmony_ci/** 67362306a36Sopenharmony_ci * usb_kill_urb - cancel a transfer request and wait for it to finish 67462306a36Sopenharmony_ci * @urb: pointer to URB describing a previously submitted request, 67562306a36Sopenharmony_ci * may be NULL 67662306a36Sopenharmony_ci * 67762306a36Sopenharmony_ci * This routine cancels an in-progress request. It is guaranteed that 67862306a36Sopenharmony_ci * upon return all completion handlers will have finished and the URB 67962306a36Sopenharmony_ci * will be totally idle and available for reuse. These features make 68062306a36Sopenharmony_ci * this an ideal way to stop I/O in a disconnect() callback or close() 68162306a36Sopenharmony_ci * function. If the request has not already finished or been unlinked 68262306a36Sopenharmony_ci * the completion handler will see urb->status == -ENOENT. 68362306a36Sopenharmony_ci * 68462306a36Sopenharmony_ci * While the routine is running, attempts to resubmit the URB will fail 68562306a36Sopenharmony_ci * with error -EPERM. Thus even if the URB's completion handler always 68662306a36Sopenharmony_ci * tries to resubmit, it will not succeed and the URB will become idle. 68762306a36Sopenharmony_ci * 68862306a36Sopenharmony_ci * The URB must not be deallocated while this routine is running. In 68962306a36Sopenharmony_ci * particular, when a driver calls this routine, it must insure that the 69062306a36Sopenharmony_ci * completion handler cannot deallocate the URB. 69162306a36Sopenharmony_ci * 69262306a36Sopenharmony_ci * This routine may not be used in an interrupt context (such as a bottom 69362306a36Sopenharmony_ci * half or a completion handler), or when holding a spinlock, or in other 69462306a36Sopenharmony_ci * situations where the caller can't schedule(). 69562306a36Sopenharmony_ci * 69662306a36Sopenharmony_ci * This routine should not be called by a driver after its disconnect 69762306a36Sopenharmony_ci * method has returned. 69862306a36Sopenharmony_ci */ 69962306a36Sopenharmony_civoid usb_kill_urb(struct urb *urb) 70062306a36Sopenharmony_ci{ 70162306a36Sopenharmony_ci might_sleep(); 70262306a36Sopenharmony_ci if (!(urb && urb->dev && urb->ep)) 70362306a36Sopenharmony_ci return; 70462306a36Sopenharmony_ci atomic_inc(&urb->reject); 70562306a36Sopenharmony_ci /* 70662306a36Sopenharmony_ci * Order the write of urb->reject above before the read 70762306a36Sopenharmony_ci * of urb->use_count below. Pairs with the barriers in 70862306a36Sopenharmony_ci * __usb_hcd_giveback_urb() and usb_hcd_submit_urb(). 70962306a36Sopenharmony_ci */ 71062306a36Sopenharmony_ci smp_mb__after_atomic(); 71162306a36Sopenharmony_ci 71262306a36Sopenharmony_ci usb_hcd_unlink_urb(urb, -ENOENT); 71362306a36Sopenharmony_ci wait_event(usb_kill_urb_queue, atomic_read(&urb->use_count) == 0); 71462306a36Sopenharmony_ci 71562306a36Sopenharmony_ci atomic_dec(&urb->reject); 71662306a36Sopenharmony_ci} 71762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_kill_urb); 71862306a36Sopenharmony_ci 71962306a36Sopenharmony_ci/** 72062306a36Sopenharmony_ci * usb_poison_urb - reliably kill a transfer and prevent further use of an URB 72162306a36Sopenharmony_ci * @urb: pointer to URB describing a previously submitted request, 72262306a36Sopenharmony_ci * may be NULL 72362306a36Sopenharmony_ci * 72462306a36Sopenharmony_ci * This routine cancels an in-progress request. It is guaranteed that 72562306a36Sopenharmony_ci * upon return all completion handlers will have finished and the URB 72662306a36Sopenharmony_ci * will be totally idle and cannot be reused. These features make 72762306a36Sopenharmony_ci * this an ideal way to stop I/O in a disconnect() callback. 72862306a36Sopenharmony_ci * If the request has not already finished or been unlinked 72962306a36Sopenharmony_ci * the completion handler will see urb->status == -ENOENT. 73062306a36Sopenharmony_ci * 73162306a36Sopenharmony_ci * After and while the routine runs, attempts to resubmit the URB will fail 73262306a36Sopenharmony_ci * with error -EPERM. Thus even if the URB's completion handler always 73362306a36Sopenharmony_ci * tries to resubmit, it will not succeed and the URB will become idle. 73462306a36Sopenharmony_ci * 73562306a36Sopenharmony_ci * The URB must not be deallocated while this routine is running. In 73662306a36Sopenharmony_ci * particular, when a driver calls this routine, it must insure that the 73762306a36Sopenharmony_ci * completion handler cannot deallocate the URB. 73862306a36Sopenharmony_ci * 73962306a36Sopenharmony_ci * This routine may not be used in an interrupt context (such as a bottom 74062306a36Sopenharmony_ci * half or a completion handler), or when holding a spinlock, or in other 74162306a36Sopenharmony_ci * situations where the caller can't schedule(). 74262306a36Sopenharmony_ci * 74362306a36Sopenharmony_ci * This routine should not be called by a driver after its disconnect 74462306a36Sopenharmony_ci * method has returned. 74562306a36Sopenharmony_ci */ 74662306a36Sopenharmony_civoid usb_poison_urb(struct urb *urb) 74762306a36Sopenharmony_ci{ 74862306a36Sopenharmony_ci might_sleep(); 74962306a36Sopenharmony_ci if (!urb) 75062306a36Sopenharmony_ci return; 75162306a36Sopenharmony_ci atomic_inc(&urb->reject); 75262306a36Sopenharmony_ci /* 75362306a36Sopenharmony_ci * Order the write of urb->reject above before the read 75462306a36Sopenharmony_ci * of urb->use_count below. Pairs with the barriers in 75562306a36Sopenharmony_ci * __usb_hcd_giveback_urb() and usb_hcd_submit_urb(). 75662306a36Sopenharmony_ci */ 75762306a36Sopenharmony_ci smp_mb__after_atomic(); 75862306a36Sopenharmony_ci 75962306a36Sopenharmony_ci if (!urb->dev || !urb->ep) 76062306a36Sopenharmony_ci return; 76162306a36Sopenharmony_ci 76262306a36Sopenharmony_ci usb_hcd_unlink_urb(urb, -ENOENT); 76362306a36Sopenharmony_ci wait_event(usb_kill_urb_queue, atomic_read(&urb->use_count) == 0); 76462306a36Sopenharmony_ci} 76562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_poison_urb); 76662306a36Sopenharmony_ci 76762306a36Sopenharmony_civoid usb_unpoison_urb(struct urb *urb) 76862306a36Sopenharmony_ci{ 76962306a36Sopenharmony_ci if (!urb) 77062306a36Sopenharmony_ci return; 77162306a36Sopenharmony_ci 77262306a36Sopenharmony_ci atomic_dec(&urb->reject); 77362306a36Sopenharmony_ci} 77462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_unpoison_urb); 77562306a36Sopenharmony_ci 77662306a36Sopenharmony_ci/** 77762306a36Sopenharmony_ci * usb_block_urb - reliably prevent further use of an URB 77862306a36Sopenharmony_ci * @urb: pointer to URB to be blocked, may be NULL 77962306a36Sopenharmony_ci * 78062306a36Sopenharmony_ci * After the routine has run, attempts to resubmit the URB will fail 78162306a36Sopenharmony_ci * with error -EPERM. Thus even if the URB's completion handler always 78262306a36Sopenharmony_ci * tries to resubmit, it will not succeed and the URB will become idle. 78362306a36Sopenharmony_ci * 78462306a36Sopenharmony_ci * The URB must not be deallocated while this routine is running. In 78562306a36Sopenharmony_ci * particular, when a driver calls this routine, it must insure that the 78662306a36Sopenharmony_ci * completion handler cannot deallocate the URB. 78762306a36Sopenharmony_ci */ 78862306a36Sopenharmony_civoid usb_block_urb(struct urb *urb) 78962306a36Sopenharmony_ci{ 79062306a36Sopenharmony_ci if (!urb) 79162306a36Sopenharmony_ci return; 79262306a36Sopenharmony_ci 79362306a36Sopenharmony_ci atomic_inc(&urb->reject); 79462306a36Sopenharmony_ci} 79562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_block_urb); 79662306a36Sopenharmony_ci 79762306a36Sopenharmony_ci/** 79862306a36Sopenharmony_ci * usb_kill_anchored_urbs - kill all URBs associated with an anchor 79962306a36Sopenharmony_ci * @anchor: anchor the requests are bound to 80062306a36Sopenharmony_ci * 80162306a36Sopenharmony_ci * This kills all outstanding URBs starting from the back of the queue, 80262306a36Sopenharmony_ci * with guarantee that no completer callbacks will take place from the 80362306a36Sopenharmony_ci * anchor after this function returns. 80462306a36Sopenharmony_ci * 80562306a36Sopenharmony_ci * This routine should not be called by a driver after its disconnect 80662306a36Sopenharmony_ci * method has returned. 80762306a36Sopenharmony_ci */ 80862306a36Sopenharmony_civoid usb_kill_anchored_urbs(struct usb_anchor *anchor) 80962306a36Sopenharmony_ci{ 81062306a36Sopenharmony_ci struct urb *victim; 81162306a36Sopenharmony_ci int surely_empty; 81262306a36Sopenharmony_ci 81362306a36Sopenharmony_ci do { 81462306a36Sopenharmony_ci spin_lock_irq(&anchor->lock); 81562306a36Sopenharmony_ci while (!list_empty(&anchor->urb_list)) { 81662306a36Sopenharmony_ci victim = list_entry(anchor->urb_list.prev, 81762306a36Sopenharmony_ci struct urb, anchor_list); 81862306a36Sopenharmony_ci /* make sure the URB isn't freed before we kill it */ 81962306a36Sopenharmony_ci usb_get_urb(victim); 82062306a36Sopenharmony_ci spin_unlock_irq(&anchor->lock); 82162306a36Sopenharmony_ci /* this will unanchor the URB */ 82262306a36Sopenharmony_ci usb_kill_urb(victim); 82362306a36Sopenharmony_ci usb_put_urb(victim); 82462306a36Sopenharmony_ci spin_lock_irq(&anchor->lock); 82562306a36Sopenharmony_ci } 82662306a36Sopenharmony_ci surely_empty = usb_anchor_check_wakeup(anchor); 82762306a36Sopenharmony_ci 82862306a36Sopenharmony_ci spin_unlock_irq(&anchor->lock); 82962306a36Sopenharmony_ci cpu_relax(); 83062306a36Sopenharmony_ci } while (!surely_empty); 83162306a36Sopenharmony_ci} 83262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_kill_anchored_urbs); 83362306a36Sopenharmony_ci 83462306a36Sopenharmony_ci 83562306a36Sopenharmony_ci/** 83662306a36Sopenharmony_ci * usb_poison_anchored_urbs - cease all traffic from an anchor 83762306a36Sopenharmony_ci * @anchor: anchor the requests are bound to 83862306a36Sopenharmony_ci * 83962306a36Sopenharmony_ci * this allows all outstanding URBs to be poisoned starting 84062306a36Sopenharmony_ci * from the back of the queue. Newly added URBs will also be 84162306a36Sopenharmony_ci * poisoned 84262306a36Sopenharmony_ci * 84362306a36Sopenharmony_ci * This routine should not be called by a driver after its disconnect 84462306a36Sopenharmony_ci * method has returned. 84562306a36Sopenharmony_ci */ 84662306a36Sopenharmony_civoid usb_poison_anchored_urbs(struct usb_anchor *anchor) 84762306a36Sopenharmony_ci{ 84862306a36Sopenharmony_ci struct urb *victim; 84962306a36Sopenharmony_ci int surely_empty; 85062306a36Sopenharmony_ci 85162306a36Sopenharmony_ci do { 85262306a36Sopenharmony_ci spin_lock_irq(&anchor->lock); 85362306a36Sopenharmony_ci anchor->poisoned = 1; 85462306a36Sopenharmony_ci while (!list_empty(&anchor->urb_list)) { 85562306a36Sopenharmony_ci victim = list_entry(anchor->urb_list.prev, 85662306a36Sopenharmony_ci struct urb, anchor_list); 85762306a36Sopenharmony_ci /* make sure the URB isn't freed before we kill it */ 85862306a36Sopenharmony_ci usb_get_urb(victim); 85962306a36Sopenharmony_ci spin_unlock_irq(&anchor->lock); 86062306a36Sopenharmony_ci /* this will unanchor the URB */ 86162306a36Sopenharmony_ci usb_poison_urb(victim); 86262306a36Sopenharmony_ci usb_put_urb(victim); 86362306a36Sopenharmony_ci spin_lock_irq(&anchor->lock); 86462306a36Sopenharmony_ci } 86562306a36Sopenharmony_ci surely_empty = usb_anchor_check_wakeup(anchor); 86662306a36Sopenharmony_ci 86762306a36Sopenharmony_ci spin_unlock_irq(&anchor->lock); 86862306a36Sopenharmony_ci cpu_relax(); 86962306a36Sopenharmony_ci } while (!surely_empty); 87062306a36Sopenharmony_ci} 87162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_poison_anchored_urbs); 87262306a36Sopenharmony_ci 87362306a36Sopenharmony_ci/** 87462306a36Sopenharmony_ci * usb_unpoison_anchored_urbs - let an anchor be used successfully again 87562306a36Sopenharmony_ci * @anchor: anchor the requests are bound to 87662306a36Sopenharmony_ci * 87762306a36Sopenharmony_ci * Reverses the effect of usb_poison_anchored_urbs 87862306a36Sopenharmony_ci * the anchor can be used normally after it returns 87962306a36Sopenharmony_ci */ 88062306a36Sopenharmony_civoid usb_unpoison_anchored_urbs(struct usb_anchor *anchor) 88162306a36Sopenharmony_ci{ 88262306a36Sopenharmony_ci unsigned long flags; 88362306a36Sopenharmony_ci struct urb *lazarus; 88462306a36Sopenharmony_ci 88562306a36Sopenharmony_ci spin_lock_irqsave(&anchor->lock, flags); 88662306a36Sopenharmony_ci list_for_each_entry(lazarus, &anchor->urb_list, anchor_list) { 88762306a36Sopenharmony_ci usb_unpoison_urb(lazarus); 88862306a36Sopenharmony_ci } 88962306a36Sopenharmony_ci anchor->poisoned = 0; 89062306a36Sopenharmony_ci spin_unlock_irqrestore(&anchor->lock, flags); 89162306a36Sopenharmony_ci} 89262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_unpoison_anchored_urbs); 89362306a36Sopenharmony_ci/** 89462306a36Sopenharmony_ci * usb_unlink_anchored_urbs - asynchronously cancel transfer requests en masse 89562306a36Sopenharmony_ci * @anchor: anchor the requests are bound to 89662306a36Sopenharmony_ci * 89762306a36Sopenharmony_ci * this allows all outstanding URBs to be unlinked starting 89862306a36Sopenharmony_ci * from the back of the queue. This function is asynchronous. 89962306a36Sopenharmony_ci * The unlinking is just triggered. It may happen after this 90062306a36Sopenharmony_ci * function has returned. 90162306a36Sopenharmony_ci * 90262306a36Sopenharmony_ci * This routine should not be called by a driver after its disconnect 90362306a36Sopenharmony_ci * method has returned. 90462306a36Sopenharmony_ci */ 90562306a36Sopenharmony_civoid usb_unlink_anchored_urbs(struct usb_anchor *anchor) 90662306a36Sopenharmony_ci{ 90762306a36Sopenharmony_ci struct urb *victim; 90862306a36Sopenharmony_ci 90962306a36Sopenharmony_ci while ((victim = usb_get_from_anchor(anchor)) != NULL) { 91062306a36Sopenharmony_ci usb_unlink_urb(victim); 91162306a36Sopenharmony_ci usb_put_urb(victim); 91262306a36Sopenharmony_ci } 91362306a36Sopenharmony_ci} 91462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_unlink_anchored_urbs); 91562306a36Sopenharmony_ci 91662306a36Sopenharmony_ci/** 91762306a36Sopenharmony_ci * usb_anchor_suspend_wakeups 91862306a36Sopenharmony_ci * @anchor: the anchor you want to suspend wakeups on 91962306a36Sopenharmony_ci * 92062306a36Sopenharmony_ci * Call this to stop the last urb being unanchored from waking up any 92162306a36Sopenharmony_ci * usb_wait_anchor_empty_timeout waiters. This is used in the hcd urb give- 92262306a36Sopenharmony_ci * back path to delay waking up until after the completion handler has run. 92362306a36Sopenharmony_ci */ 92462306a36Sopenharmony_civoid usb_anchor_suspend_wakeups(struct usb_anchor *anchor) 92562306a36Sopenharmony_ci{ 92662306a36Sopenharmony_ci if (anchor) 92762306a36Sopenharmony_ci atomic_inc(&anchor->suspend_wakeups); 92862306a36Sopenharmony_ci} 92962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_anchor_suspend_wakeups); 93062306a36Sopenharmony_ci 93162306a36Sopenharmony_ci/** 93262306a36Sopenharmony_ci * usb_anchor_resume_wakeups 93362306a36Sopenharmony_ci * @anchor: the anchor you want to resume wakeups on 93462306a36Sopenharmony_ci * 93562306a36Sopenharmony_ci * Allow usb_wait_anchor_empty_timeout waiters to be woken up again, and 93662306a36Sopenharmony_ci * wake up any current waiters if the anchor is empty. 93762306a36Sopenharmony_ci */ 93862306a36Sopenharmony_civoid usb_anchor_resume_wakeups(struct usb_anchor *anchor) 93962306a36Sopenharmony_ci{ 94062306a36Sopenharmony_ci if (!anchor) 94162306a36Sopenharmony_ci return; 94262306a36Sopenharmony_ci 94362306a36Sopenharmony_ci atomic_dec(&anchor->suspend_wakeups); 94462306a36Sopenharmony_ci if (usb_anchor_check_wakeup(anchor)) 94562306a36Sopenharmony_ci wake_up(&anchor->wait); 94662306a36Sopenharmony_ci} 94762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_anchor_resume_wakeups); 94862306a36Sopenharmony_ci 94962306a36Sopenharmony_ci/** 95062306a36Sopenharmony_ci * usb_wait_anchor_empty_timeout - wait for an anchor to be unused 95162306a36Sopenharmony_ci * @anchor: the anchor you want to become unused 95262306a36Sopenharmony_ci * @timeout: how long you are willing to wait in milliseconds 95362306a36Sopenharmony_ci * 95462306a36Sopenharmony_ci * Call this is you want to be sure all an anchor's 95562306a36Sopenharmony_ci * URBs have finished 95662306a36Sopenharmony_ci * 95762306a36Sopenharmony_ci * Return: Non-zero if the anchor became unused. Zero on timeout. 95862306a36Sopenharmony_ci */ 95962306a36Sopenharmony_ciint usb_wait_anchor_empty_timeout(struct usb_anchor *anchor, 96062306a36Sopenharmony_ci unsigned int timeout) 96162306a36Sopenharmony_ci{ 96262306a36Sopenharmony_ci return wait_event_timeout(anchor->wait, 96362306a36Sopenharmony_ci usb_anchor_check_wakeup(anchor), 96462306a36Sopenharmony_ci msecs_to_jiffies(timeout)); 96562306a36Sopenharmony_ci} 96662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_wait_anchor_empty_timeout); 96762306a36Sopenharmony_ci 96862306a36Sopenharmony_ci/** 96962306a36Sopenharmony_ci * usb_get_from_anchor - get an anchor's oldest urb 97062306a36Sopenharmony_ci * @anchor: the anchor whose urb you want 97162306a36Sopenharmony_ci * 97262306a36Sopenharmony_ci * This will take the oldest urb from an anchor, 97362306a36Sopenharmony_ci * unanchor and return it 97462306a36Sopenharmony_ci * 97562306a36Sopenharmony_ci * Return: The oldest urb from @anchor, or %NULL if @anchor has no 97662306a36Sopenharmony_ci * urbs associated with it. 97762306a36Sopenharmony_ci */ 97862306a36Sopenharmony_cistruct urb *usb_get_from_anchor(struct usb_anchor *anchor) 97962306a36Sopenharmony_ci{ 98062306a36Sopenharmony_ci struct urb *victim; 98162306a36Sopenharmony_ci unsigned long flags; 98262306a36Sopenharmony_ci 98362306a36Sopenharmony_ci spin_lock_irqsave(&anchor->lock, flags); 98462306a36Sopenharmony_ci if (!list_empty(&anchor->urb_list)) { 98562306a36Sopenharmony_ci victim = list_entry(anchor->urb_list.next, struct urb, 98662306a36Sopenharmony_ci anchor_list); 98762306a36Sopenharmony_ci usb_get_urb(victim); 98862306a36Sopenharmony_ci __usb_unanchor_urb(victim, anchor); 98962306a36Sopenharmony_ci } else { 99062306a36Sopenharmony_ci victim = NULL; 99162306a36Sopenharmony_ci } 99262306a36Sopenharmony_ci spin_unlock_irqrestore(&anchor->lock, flags); 99362306a36Sopenharmony_ci 99462306a36Sopenharmony_ci return victim; 99562306a36Sopenharmony_ci} 99662306a36Sopenharmony_ci 99762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_get_from_anchor); 99862306a36Sopenharmony_ci 99962306a36Sopenharmony_ci/** 100062306a36Sopenharmony_ci * usb_scuttle_anchored_urbs - unanchor all an anchor's urbs 100162306a36Sopenharmony_ci * @anchor: the anchor whose urbs you want to unanchor 100262306a36Sopenharmony_ci * 100362306a36Sopenharmony_ci * use this to get rid of all an anchor's urbs 100462306a36Sopenharmony_ci */ 100562306a36Sopenharmony_civoid usb_scuttle_anchored_urbs(struct usb_anchor *anchor) 100662306a36Sopenharmony_ci{ 100762306a36Sopenharmony_ci struct urb *victim; 100862306a36Sopenharmony_ci unsigned long flags; 100962306a36Sopenharmony_ci int surely_empty; 101062306a36Sopenharmony_ci 101162306a36Sopenharmony_ci do { 101262306a36Sopenharmony_ci spin_lock_irqsave(&anchor->lock, flags); 101362306a36Sopenharmony_ci while (!list_empty(&anchor->urb_list)) { 101462306a36Sopenharmony_ci victim = list_entry(anchor->urb_list.prev, 101562306a36Sopenharmony_ci struct urb, anchor_list); 101662306a36Sopenharmony_ci __usb_unanchor_urb(victim, anchor); 101762306a36Sopenharmony_ci } 101862306a36Sopenharmony_ci surely_empty = usb_anchor_check_wakeup(anchor); 101962306a36Sopenharmony_ci 102062306a36Sopenharmony_ci spin_unlock_irqrestore(&anchor->lock, flags); 102162306a36Sopenharmony_ci cpu_relax(); 102262306a36Sopenharmony_ci } while (!surely_empty); 102362306a36Sopenharmony_ci} 102462306a36Sopenharmony_ci 102562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_scuttle_anchored_urbs); 102662306a36Sopenharmony_ci 102762306a36Sopenharmony_ci/** 102862306a36Sopenharmony_ci * usb_anchor_empty - is an anchor empty 102962306a36Sopenharmony_ci * @anchor: the anchor you want to query 103062306a36Sopenharmony_ci * 103162306a36Sopenharmony_ci * Return: 1 if the anchor has no urbs associated with it. 103262306a36Sopenharmony_ci */ 103362306a36Sopenharmony_ciint usb_anchor_empty(struct usb_anchor *anchor) 103462306a36Sopenharmony_ci{ 103562306a36Sopenharmony_ci return list_empty(&anchor->urb_list); 103662306a36Sopenharmony_ci} 103762306a36Sopenharmony_ci 103862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_anchor_empty); 103962306a36Sopenharmony_ci 1040