![]() |
|
|||
0001 // SPDX-License-Identifier: GPL-2.0 0002 /* 0003 * Released under the GPLv2 only. 0004 */ 0005 0006 #include <linux/module.h> 0007 #include <linux/string.h> 0008 #include <linux/bitops.h> 0009 #include <linux/slab.h> 0010 #include <linux/log2.h> 0011 #include <linux/usb.h> 0012 #include <linux/wait.h> 0013 #include <linux/usb/hcd.h> 0014 #include <linux/scatterlist.h> 0015 0016 #define to_urb(d) container_of(d, struct urb, kref) 0017 0018 0019 static void urb_destroy(struct kref *kref) 0020 { 0021 struct urb *urb = to_urb(kref); 0022 0023 if (urb->transfer_flags & URB_FREE_BUFFER) 0024 kfree(urb->transfer_buffer); 0025 0026 kfree(urb); 0027 } 0028 0029 /** 0030 * usb_init_urb - initializes a urb so that it can be used by a USB driver 0031 * @urb: pointer to the urb to initialize 0032 * 0033 * Initializes a urb so that the USB subsystem can use it properly. 0034 * 0035 * If a urb is created with a call to usb_alloc_urb() it is not 0036 * necessary to call this function. Only use this if you allocate the 0037 * space for a struct urb on your own. If you call this function, be 0038 * careful when freeing the memory for your urb that it is no longer in 0039 * use by the USB core. 0040 * 0041 * Only use this function if you _really_ understand what you are doing. 0042 */ 0043 void usb_init_urb(struct urb *urb) 0044 { 0045 if (urb) { 0046 memset(urb, 0, sizeof(*urb)); 0047 kref_init(&urb->kref); 0048 INIT_LIST_HEAD(&urb->urb_list); 0049 INIT_LIST_HEAD(&urb->anchor_list); 0050 } 0051 } 0052 EXPORT_SYMBOL_GPL(usb_init_urb); 0053 0054 /** 0055 * usb_alloc_urb - creates a new urb for a USB driver to use 0056 * @iso_packets: number of iso packets for this urb 0057 * @mem_flags: the type of memory to allocate, see kmalloc() for a list of 0058 * valid options for this. 0059 * 0060 * Creates an urb for the USB driver to use, initializes a few internal 0061 * structures, increments the usage counter, and returns a pointer to it. 0062 * 0063 * If the driver want to use this urb for interrupt, control, or bulk 0064 * endpoints, pass '0' as the number of iso packets. 0065 * 0066 * The driver must call usb_free_urb() when it is finished with the urb. 0067 * 0068 * Return: A pointer to the new urb, or %NULL if no memory is available. 0069 */ 0070 struct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags) 0071 { 0072 struct urb *urb; 0073 0074 urb = kmalloc(struct_size(urb, iso_frame_desc, iso_packets), 0075 mem_flags); 0076 if (!urb) 0077 return NULL; 0078 usb_init_urb(urb); 0079 return urb; 0080 } 0081 EXPORT_SYMBOL_GPL(usb_alloc_urb); 0082 0083 /** 0084 * usb_free_urb - frees the memory used by a urb when all users of it are finished 0085 * @urb: pointer to the urb to free, may be NULL 0086 * 0087 * Must be called when a user of a urb is finished with it. When the last user 0088 * of the urb calls this function, the memory of the urb is freed. 0089 * 0090 * Note: The transfer buffer associated with the urb is not freed unless the 0091 * URB_FREE_BUFFER transfer flag is set. 0092 */ 0093 void usb_free_urb(struct urb *urb) 0094 { 0095 if (urb) 0096 kref_put(&urb->kref, urb_destroy); 0097 } 0098 EXPORT_SYMBOL_GPL(usb_free_urb); 0099 0100 /** 0101 * usb_get_urb - increments the reference count of the urb 0102 * @urb: pointer to the urb to modify, may be NULL 0103 * 0104 * This must be called whenever a urb is transferred from a device driver to a 0105 * host controller driver. This allows proper reference counting to happen 0106 * for urbs. 0107 * 0108 * Return: A pointer to the urb with the incremented reference counter. 0109 */ 0110 struct urb *usb_get_urb(struct urb *urb) 0111 { 0112 if (urb) 0113 kref_get(&urb->kref); 0114 return urb; 0115 } 0116 EXPORT_SYMBOL_GPL(usb_get_urb); 0117 0118 /** 0119 * usb_anchor_urb - anchors an URB while it is processed 0120 * @urb: pointer to the urb to anchor 0121 * @anchor: pointer to the anchor 0122 * 0123 * This can be called to have access to URBs which are to be executed 0124 * without bothering to track them 0125 */ 0126 void usb_anchor_urb(struct urb *urb, struct usb_anchor *anchor) 0127 { 0128 unsigned long flags; 0129 0130 spin_lock_irqsave(&anchor->lock, flags); 0131 usb_get_urb(urb); 0132 list_add_tail(&urb->anchor_list, &anchor->urb_list); 0133 urb->anchor = anchor; 0134 0135 if (unlikely(anchor->poisoned)) 0136 atomic_inc(&urb->reject); 0137 0138 spin_unlock_irqrestore(&anchor->lock, flags); 0139 } 0140 EXPORT_SYMBOL_GPL(usb_anchor_urb); 0141 0142 static int usb_anchor_check_wakeup(struct usb_anchor *anchor) 0143 { 0144 return atomic_read(&anchor->suspend_wakeups) == 0 && 0145 list_empty(&anchor->urb_list); 0146 } 0147 0148 /* Callers must hold anchor->lock */ 0149 static void __usb_unanchor_urb(struct urb *urb, struct usb_anchor *anchor) 0150 { 0151 urb->anchor = NULL; 0152 list_del(&urb->anchor_list); 0153 usb_put_urb(urb); 0154 if (usb_anchor_check_wakeup(anchor)) 0155 wake_up(&anchor->wait); 0156 } 0157 0158 /** 0159 * usb_unanchor_urb - unanchors an URB 0160 * @urb: pointer to the urb to anchor 0161 * 0162 * Call this to stop the system keeping track of this URB 0163 */ 0164 void usb_unanchor_urb(struct urb *urb) 0165 { 0166 unsigned long flags; 0167 struct usb_anchor *anchor; 0168 0169 if (!urb) 0170 return; 0171 0172 anchor = urb->anchor; 0173 if (!anchor) 0174 return; 0175 0176 spin_lock_irqsave(&anchor->lock, flags); 0177 /* 0178 * At this point, we could be competing with another thread which 0179 * has the same intention. To protect the urb from being unanchored 0180 * twice, only the winner of the race gets the job. 0181 */ 0182 if (likely(anchor == urb->anchor)) 0183 __usb_unanchor_urb(urb, anchor); 0184 spin_unlock_irqrestore(&anchor->lock, flags); 0185 } 0186 EXPORT_SYMBOL_GPL(usb_unanchor_urb); 0187 0188 /*-------------------------------------------------------------------*/ 0189 0190 static const int pipetypes[4] = { 0191 PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT 0192 }; 0193 0194 /** 0195 * usb_pipe_type_check - sanity check of a specific pipe for a usb device 0196 * @dev: struct usb_device to be checked 0197 * @pipe: pipe to check 0198 * 0199 * This performs a light-weight sanity check for the endpoint in the 0200 * given usb device. It returns 0 if the pipe is valid for the specific usb 0201 * device, otherwise a negative error code. 0202 */ 0203 int usb_pipe_type_check(struct usb_device *dev, unsigned int pipe) 0204 { 0205 const struct usb_host_endpoint *ep; 0206 0207 ep = usb_pipe_endpoint(dev, pipe); 0208 if (!ep) 0209 return -EINVAL; 0210 if (usb_pipetype(pipe) != pipetypes[usb_endpoint_type(&ep->desc)]) 0211 return -EINVAL; 0212 return 0; 0213 } 0214 EXPORT_SYMBOL_GPL(usb_pipe_type_check); 0215 0216 /** 0217 * usb_urb_ep_type_check - sanity check of endpoint in the given urb 0218 * @urb: urb to be checked 0219 * 0220 * This performs a light-weight sanity check for the endpoint in the 0221 * given urb. It returns 0 if the urb contains a valid endpoint, otherwise 0222 * a negative error code. 0223 */ 0224 int usb_urb_ep_type_check(const struct urb *urb) 0225 { 0226 return usb_pipe_type_check(urb->dev, urb->pipe); 0227 } 0228 EXPORT_SYMBOL_GPL(usb_urb_ep_type_check); 0229 0230 /** 0231 * usb_submit_urb - issue an asynchronous transfer request for an endpoint 0232 * @urb: pointer to the urb describing the request 0233 * @mem_flags: the type of memory to allocate, see kmalloc() for a list 0234 * of valid options for this. 0235 * 0236 * This submits a transfer request, and transfers control of the URB 0237 * describing that request to the USB subsystem. Request completion will 0238 * be indicated later, asynchronously, by calling the completion handler. 0239 * The three types of completion are success, error, and unlink 0240 * (a software-induced fault, also called "request cancellation"). 0241 * 0242 * URBs may be submitted in interrupt context. 0243 * 0244 * The caller must have correctly initialized the URB before submitting 0245 * it. Functions such as usb_fill_bulk_urb() and usb_fill_control_urb() are 0246 * available to ensure that most fields are correctly initialized, for 0247 * the particular kind of transfer, although they will not initialize 0248 * any transfer flags. 0249 * 0250 * If the submission is successful, the complete() callback from the URB 0251 * will be called exactly once, when the USB core and Host Controller Driver 0252 * (HCD) are finished with the URB. When the completion function is called, 0253 * control of the URB is returned to the device driver which issued the 0254 * request. The completion handler may then immediately free or reuse that 0255 * URB. 0256 * 0257 * With few exceptions, USB device drivers should never access URB fields 0258 * provided by usbcore or the HCD until its complete() is called. 0259 * The exceptions relate to periodic transfer scheduling. For both 0260 * interrupt and isochronous urbs, as part of successful URB submission 0261 * urb->interval is modified to reflect the actual transfer period used 0262 * (normally some power of two units). And for isochronous urbs, 0263 * urb->start_frame is modified to reflect when the URB's transfers were 0264 * scheduled to start. 0265 * 0266 * Not all isochronous transfer scheduling policies will work, but most 0267 * host controller drivers should easily handle ISO queues going from now 0268 * until 10-200 msec into the future. Drivers should try to keep at 0269 * least one or two msec of data in the queue; many controllers require 0270 * that new transfers start at least 1 msec in the future when they are 0271 * added. If the driver is unable to keep up and the queue empties out, 0272 * the behavior for new submissions is governed by the URB_ISO_ASAP flag. 0273 * If the flag is set, or if the queue is idle, then the URB is always 0274 * assigned to the first available (and not yet expired) slot in the 0275 * endpoint's schedule. If the flag is not set and the queue is active 0276 * then the URB is always assigned to the next slot in the schedule 0277 * following the end of the endpoint's previous URB, even if that slot is 0278 * in the past. When a packet is assigned in this way to a slot that has 0279 * already expired, the packet is not transmitted and the corresponding 0280 * usb_iso_packet_descriptor's status field will return -EXDEV. If this 0281 * would happen to all the packets in the URB, submission fails with a 0282 * -EXDEV error code. 0283 * 0284 * For control endpoints, the synchronous usb_control_msg() call is 0285 * often used (in non-interrupt context) instead of this call. 0286 * That is often used through convenience wrappers, for the requests 0287 * that are standardized in the USB 2.0 specification. For bulk 0288 * endpoints, a synchronous usb_bulk_msg() call is available. 0289 * 0290 * Return: 0291 * 0 on successful submissions. A negative error number otherwise. 0292 * 0293 * Request Queuing: 0294 * 0295 * URBs may be submitted to endpoints before previous ones complete, to 0296 * minimize the impact of interrupt latencies and system overhead on data 0297 * throughput. With that queuing policy, an endpoint's queue would never 0298 * be empty. This is required for continuous isochronous data streams, 0299 * and may also be required for some kinds of interrupt transfers. Such 0300 * queuing also maximizes bandwidth utilization by letting USB controllers 0301 * start work on later requests before driver software has finished the 0302 * completion processing for earlier (successful) requests. 0303 * 0304 * As of Linux 2.6, all USB endpoint transfer queues support depths greater 0305 * than one. This was previously a HCD-specific behavior, except for ISO 0306 * transfers. Non-isochronous endpoint queues are inactive during cleanup 0307 * after faults (transfer errors or cancellation). 0308 * 0309 * Reserved Bandwidth Transfers: 0310 * 0311 * Periodic transfers (interrupt or isochronous) are performed repeatedly, 0312 * using the interval specified in the urb. Submitting the first urb to 0313 * the endpoint reserves the bandwidth necessary to make those transfers. 0314 * If the USB subsystem can't allocate sufficient bandwidth to perform 0315 * the periodic request, submitting such a periodic request should fail. 0316 * 0317 * For devices under xHCI, the bandwidth is reserved at configuration time, or 0318 * when the alt setting is selected. If there is not enough bus bandwidth, the 0319 * configuration/alt setting request will fail. Therefore, submissions to 0320 * periodic endpoints on devices under xHCI should never fail due to bandwidth 0321 * constraints. 0322 * 0323 * Device drivers must explicitly request that repetition, by ensuring that 0324 * some URB is always on the endpoint's queue (except possibly for short 0325 * periods during completion callbacks). When there is no longer an urb 0326 * queued, the endpoint's bandwidth reservation is canceled. This means 0327 * drivers can use their completion handlers to ensure they keep bandwidth 0328 * they need, by reinitializing and resubmitting the just-completed urb 0329 * until the driver longer needs that periodic bandwidth. 0330 * 0331 * Memory Flags: 0332 * 0333 * The general rules for how to decide which mem_flags to use 0334 * are the same as for kmalloc. There are four 0335 * different possible values; GFP_KERNEL, GFP_NOFS, GFP_NOIO and 0336 * GFP_ATOMIC. 0337 * 0338 * GFP_NOFS is not ever used, as it has not been implemented yet. 0339 * 0340 * GFP_ATOMIC is used when 0341 * (a) you are inside a completion handler, an interrupt, bottom half, 0342 * tasklet or timer, or 0343 * (b) you are holding a spinlock or rwlock (does not apply to 0344 * semaphores), or 0345 * (c) current->state != TASK_RUNNING, this is the case only after 0346 * you've changed it. 0347 * 0348 * GFP_NOIO is used in the block io path and error handling of storage 0349 * devices. 0350 * 0351 * All other situations use GFP_KERNEL. 0352 * 0353 * Some more specific rules for mem_flags can be inferred, such as 0354 * (1) start_xmit, timeout, and receive methods of network drivers must 0355 * use GFP_ATOMIC (they are called with a spinlock held); 0356 * (2) queuecommand methods of scsi drivers must use GFP_ATOMIC (also 0357 * called with a spinlock held); 0358 * (3) If you use a kernel thread with a network driver you must use 0359 * GFP_NOIO, unless (b) or (c) apply; 0360 * (4) after you have done a down() you can use GFP_KERNEL, unless (b) or (c) 0361 * apply or your are in a storage driver's block io path; 0362 * (5) USB probe and disconnect can use GFP_KERNEL unless (b) or (c) apply; and 0363 * (6) changing firmware on a running storage or net device uses 0364 * GFP_NOIO, unless b) or c) apply 0365 * 0366 */ 0367 int usb_submit_urb(struct urb *urb, gfp_t mem_flags) 0368 { 0369 int xfertype, max; 0370 struct usb_device *dev; 0371 struct usb_host_endpoint *ep; 0372 int is_out; 0373 unsigned int allowed; 0374 0375 if (!urb || !urb->complete) 0376 return -EINVAL; 0377 if (urb->hcpriv) { 0378 WARN_ONCE(1, "URB %pK submitted while active\n", urb); 0379 return -EBUSY; 0380 } 0381 0382 dev = urb->dev; 0383 if ((!dev) || (dev->state < USB_STATE_UNAUTHENTICATED)) 0384 return -ENODEV; 0385 0386 /* For now, get the endpoint from the pipe. Eventually drivers 0387 * will be required to set urb->ep directly and we will eliminate 0388 * urb->pipe. 0389 */ 0390 ep = usb_pipe_endpoint(dev, urb->pipe); 0391 if (!ep) 0392 return -ENOENT; 0393 0394 urb->ep = ep; 0395 urb->status = -EINPROGRESS; 0396 urb->actual_length = 0; 0397 0398 /* Lots of sanity checks, so HCDs can rely on clean data 0399 * and don't need to duplicate tests 0400 */ 0401 xfertype = usb_endpoint_type(&ep->desc); 0402 if (xfertype == USB_ENDPOINT_XFER_CONTROL) { 0403 struct usb_ctrlrequest *setup = 0404 (struct usb_ctrlrequest *) urb->setup_packet; 0405 0406 if (!setup) 0407 return -ENOEXEC; 0408 is_out = !(setup->bRequestType & USB_DIR_IN) || 0409 !setup->wLength; 0410 dev_WARN_ONCE(&dev->dev, (usb_pipeout(urb->pipe) != is_out), 0411 "BOGUS control dir, pipe %x doesn't match bRequestType %x\n", 0412 urb->pipe, setup->bRequestType); 0413 if (le16_to_cpu(setup->wLength) != urb->transfer_buffer_length) { 0414 dev_dbg(&dev->dev, "BOGUS control len %d doesn't match transfer length %d\n", 0415 le16_to_cpu(setup->wLength), 0416 urb->transfer_buffer_length); 0417 return -EBADR; 0418 } 0419 } else { 0420 is_out = usb_endpoint_dir_out(&ep->desc); 0421 } 0422 0423 /* Clear the internal flags and cache the direction for later use */ 0424 urb->transfer_flags &= ~(URB_DIR_MASK | URB_DMA_MAP_SINGLE | 0425 URB_DMA_MAP_PAGE | URB_DMA_MAP_SG | URB_MAP_LOCAL | 0426 URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL | 0427 URB_DMA_SG_COMBINED); 0428 urb->transfer_flags |= (is_out ? URB_DIR_OUT : URB_DIR_IN); 0429 0430 if (xfertype != USB_ENDPOINT_XFER_CONTROL && 0431 dev->state < USB_STATE_CONFIGURED) 0432 return -ENODEV; 0433 0434 max = usb_endpoint_maxp(&ep->desc); 0435 if (max <= 0) { 0436 dev_dbg(&dev->dev, 0437 "bogus endpoint ep%d%s in %s (bad maxpacket %d)\n", 0438 usb_endpoint_num(&ep->desc), is_out ? "out" : "in", 0439 __func__, max); 0440 return -EMSGSIZE; 0441 } 0442 0443 /* periodic transfers limit size per frame/uframe, 0444 * but drivers only control those sizes for ISO. 0445 * while we're checking, initialize return status. 0446 */ 0447 if (xfertype == USB_ENDPOINT_XFER_ISOC) { 0448 int n, len; 0449 0450 /* SuperSpeed isoc endpoints have up to 16 bursts of up to 0451 * 3 packets each 0452 */ 0453 if (dev->speed >= USB_SPEED_SUPER) { 0454 int burst = 1 + ep->ss_ep_comp.bMaxBurst; 0455 int mult = USB_SS_MULT(ep->ss_ep_comp.bmAttributes); 0456 max *= burst; 0457 max *= mult; 0458 } 0459 0460 if (dev->speed == USB_SPEED_SUPER_PLUS && 0461 USB_SS_SSP_ISOC_COMP(ep->ss_ep_comp.bmAttributes)) { 0462 struct usb_ssp_isoc_ep_comp_descriptor *isoc_ep_comp; 0463 0464 isoc_ep_comp = &ep->ssp_isoc_ep_comp; 0465 max = le32_to_cpu(isoc_ep_comp->dwBytesPerInterval); 0466 } 0467 0468 /* "high bandwidth" mode, 1-3 packets/uframe? */ 0469 if (dev->speed == USB_SPEED_HIGH) 0470 max *= usb_endpoint_maxp_mult(&ep->desc); 0471 0472 if (urb->number_of_packets <= 0) 0473 return -EINVAL; 0474 for (n = 0; n < urb->number_of_packets; n++) { 0475 len = urb->iso_frame_desc[n].length; 0476 if (len < 0 || len > max) 0477 return -EMSGSIZE; 0478 urb->iso_frame_desc[n].status = -EXDEV; 0479 urb->iso_frame_desc[n].actual_length = 0; 0480 } 0481 } else if (urb->num_sgs && !urb->dev->bus->no_sg_constraint && 0482 dev->speed != USB_SPEED_WIRELESS) { 0483 struct scatterlist *sg; 0484 int i; 0485 0486 for_each_sg(urb->sg, sg, urb->num_sgs - 1, i) 0487 if (sg->length % max) 0488 return -EINVAL; 0489 } 0490 0491 /* the I/O buffer must be mapped/unmapped, except when length=0 */ 0492 if (urb->transfer_buffer_length > INT_MAX) 0493 return -EMSGSIZE; 0494 0495 /* 0496 * stuff that drivers shouldn't do, but which shouldn't 0497 * cause problems in HCDs if they get it wrong. 0498 */ 0499 0500 /* Check that the pipe's type matches the endpoint's type */ 0501 if (usb_pipe_type_check(urb->dev, urb->pipe)) 0502 dev_WARN(&dev->dev, "BOGUS urb xfer, pipe %x != type %x\n", 0503 usb_pipetype(urb->pipe), pipetypes[xfertype]); 0504 0505 /* Check against a simple/standard policy */ 0506 allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT | URB_DIR_MASK | 0507 URB_FREE_BUFFER); 0508 switch (xfertype) { 0509 case USB_ENDPOINT_XFER_BULK: 0510 case USB_ENDPOINT_XFER_INT: 0511 if (is_out) 0512 allowed |= URB_ZERO_PACKET; 0513 fallthrough; 0514 default: /* all non-iso endpoints */ 0515 if (!is_out) 0516 allowed |= URB_SHORT_NOT_OK; 0517 break; 0518 case USB_ENDPOINT_XFER_ISOC: 0519 allowed |= URB_ISO_ASAP; 0520 break; 0521 } 0522 allowed &= urb->transfer_flags; 0523 0524 /* warn if submitter gave bogus flags */ 0525 if (allowed != urb->transfer_flags) 0526 dev_WARN(&dev->dev, "BOGUS urb flags, %x --> %x\n", 0527 urb->transfer_flags, allowed); 0528 0529 /* 0530 * Force periodic transfer intervals to be legal values that are 0531 * a power of two (so HCDs don't need to). 0532 * 0533 * FIXME want bus->{intr,iso}_sched_horizon values here. Each HC 0534 * supports different values... this uses EHCI/UHCI defaults (and 0535 * EHCI can use smaller non-default values). 0536 */ 0537 switch (xfertype) { 0538 case USB_ENDPOINT_XFER_ISOC: 0539 case USB_ENDPOINT_XFER_INT: 0540 /* too small? */ 0541 switch (dev->speed) { 0542 case USB_SPEED_WIRELESS: 0543 if ((urb->interval < 6) 0544 && (xfertype == USB_ENDPOINT_XFER_INT)) 0545 return -EINVAL; 0546 fallthrough; 0547 default: 0548 if (urb->interval <= 0) 0549 return -EINVAL; 0550 break; 0551 } 0552 /* too big? */ 0553 switch (dev->speed) { 0554 case USB_SPEED_SUPER_PLUS: 0555 case USB_SPEED_SUPER: /* units are 125us */ 0556 /* Handle up to 2^(16-1) microframes */ 0557 if (urb->interval > (1 << 15)) 0558 return -EINVAL; 0559 max = 1 << 15; 0560 break; 0561 case USB_SPEED_WIRELESS: 0562 if (urb->interval > 16) 0563 return -EINVAL; 0564 break; 0565 case USB_SPEED_HIGH: /* units are microframes */ 0566 /* NOTE usb handles 2^15 */ 0567 if (urb->interval > (1024 * 8)) 0568 urb->interval = 1024 * 8; 0569 max = 1024 * 8; 0570 break; 0571 case USB_SPEED_FULL: /* units are frames/msec */ 0572 case USB_SPEED_LOW: 0573 if (xfertype == USB_ENDPOINT_XFER_INT) { 0574 if (urb->interval > 255) 0575 return -EINVAL; 0576 /* NOTE ohci only handles up to 32 */ 0577 max = 128; 0578 } else { 0579 if (urb->interval > 1024) 0580 urb->interval = 1024; 0581 /* NOTE usb and ohci handle up to 2^15 */ 0582 max = 1024; 0583 } 0584 break; 0585 default: 0586 return -EINVAL; 0587 } 0588 if (dev->speed != USB_SPEED_WIRELESS) { 0589 /* Round down to a power of 2, no more than max */ 0590 urb->interval = min(max, 1 << ilog2(urb->interval)); 0591 } 0592 } 0593 0594 return usb_hcd_submit_urb(urb, mem_flags); 0595 } 0596 EXPORT_SYMBOL_GPL(usb_submit_urb); 0597 0598 /*-------------------------------------------------------------------*/ 0599 0600 /** 0601 * usb_unlink_urb - abort/cancel a transfer request for an endpoint 0602 * @urb: pointer to urb describing a previously submitted request, 0603 * may be NULL 0604 * 0605 * This routine cancels an in-progress request. URBs complete only once 0606 * per submission, and may be canceled only once per submission. 0607 * Successful cancellation means termination of @urb will be expedited 0608 * and the completion handler will be called with a status code 0609 * indicating that the request has been canceled (rather than any other 0610 * code). 0611 * 0612 * Drivers should not call this routine or related routines, such as 0613 * usb_kill_urb() or usb_unlink_anchored_urbs(), after their disconnect 0614 * method has returned. The disconnect function should synchronize with 0615 * a driver's I/O routines to insure that all URB-related activity has 0616 * completed before it returns. 0617 * 0618 * This request is asynchronous, however the HCD might call the ->complete() 0619 * callback during unlink. Therefore when drivers call usb_unlink_urb(), they 0620 * must not hold any locks that may be taken by the completion function. 0621 * Success is indicated by returning -EINPROGRESS, at which time the URB will 0622 * probably not yet have been given back to the device driver. When it is 0623 * eventually called, the completion function will see @urb->status == 0624 * -ECONNRESET. 0625 * Failure is indicated by usb_unlink_urb() returning any other value. 0626 * Unlinking will fail when @urb is not currently "linked" (i.e., it was 0627 * never submitted, or it was unlinked before, or the hardware is already 0628 * finished with it), even if the completion handler has not yet run. 0629 * 0630 * The URB must not be deallocated while this routine is running. In 0631 * particular, when a driver calls this routine, it must insure that the 0632 * completion handler cannot deallocate the URB. 0633 * 0634 * Return: -EINPROGRESS on success. See description for other values on 0635 * failure. 0636 * 0637 * Unlinking and Endpoint Queues: 0638 * 0639 * [The behaviors and guarantees described below do not apply to virtual 0640 * root hubs but only to endpoint queues for physical USB devices.] 0641 * 0642 * Host Controller Drivers (HCDs) place all the URBs for a particular 0643 * endpoint in a queue. Normally the queue advances as the controller 0644 * hardware processes each request. But when an URB terminates with an 0645 * error its queue generally stops (see below), at least until that URB's 0646 * completion routine returns. It is guaranteed that a stopped queue 0647 * will not restart until all its unlinked URBs have been fully retired, 0648 * with their completion routines run, even if that's not until some time 0649 * after the original completion handler returns. The same behavior and 0650 * guarantee apply when an URB terminates because it was unlinked. 0651 * 0652 * Bulk and interrupt endpoint queues are guaranteed to stop whenever an 0653 * URB terminates with any sort of error, including -ECONNRESET, -ENOENT, 0654 * and -EREMOTEIO. Control endpoint queues behave the same way except 0655 * that they are not guaranteed to stop for -EREMOTEIO errors. Queues 0656 * for isochronous endpoints are treated differently, because they must 0657 * advance at fixed rates. Such queues do not stop when an URB 0658 * encounters an error or is unlinked. An unlinked isochronous URB may 0659 * leave a gap in the stream of packets; it is undefined whether such 0660 * gaps can be filled in. 0661 * 0662 * Note that early termination of an URB because a short packet was 0663 * received will generate a -EREMOTEIO error if and only if the 0664 * URB_SHORT_NOT_OK flag is set. By setting this flag, USB device 0665 * drivers can build deep queues for large or complex bulk transfers 0666 * and clean them up reliably after any sort of aborted transfer by 0667 * unlinking all pending URBs at the first fault. 0668 * 0669 * When a control URB terminates with an error other than -EREMOTEIO, it 0670 * is quite likely that the status stage of the transfer will not take 0671 * place. 0672 */ 0673 int usb_unlink_urb(struct urb *urb) 0674 { 0675 if (!urb) 0676 return -EINVAL; 0677 if (!urb->dev) 0678 return -ENODEV; 0679 if (!urb->ep) 0680 return -EIDRM; 0681 return usb_hcd_unlink_urb(urb, -ECONNRESET); 0682 } 0683 EXPORT_SYMBOL_GPL(usb_unlink_urb); 0684 0685 /** 0686 * usb_kill_urb - cancel a transfer request and wait for it to finish 0687 * @urb: pointer to URB describing a previously submitted request, 0688 * may be NULL 0689 * 0690 * This routine cancels an in-progress request. It is guaranteed that 0691 * upon return all completion handlers will have finished and the URB 0692 * will be totally idle and available for reuse. These features make 0693 * this an ideal way to stop I/O in a disconnect() callback or close() 0694 * function. If the request has not already finished or been unlinked 0695 * the completion handler will see urb->status == -ENOENT. 0696 * 0697 * While the routine is running, attempts to resubmit the URB will fail 0698 * with error -EPERM. Thus even if the URB's completion handler always 0699 * tries to resubmit, it will not succeed and the URB will become idle. 0700 * 0701 * The URB must not be deallocated while this routine is running. In 0702 * particular, when a driver calls this routine, it must insure that the 0703 * completion handler cannot deallocate the URB. 0704 * 0705 * This routine may not be used in an interrupt context (such as a bottom 0706 * half or a completion handler), or when holding a spinlock, or in other 0707 * situations where the caller can't schedule(). 0708 * 0709 * This routine should not be called by a driver after its disconnect 0710 * method has returned. 0711 */ 0712 void usb_kill_urb(struct urb *urb) 0713 { 0714 might_sleep(); 0715 if (!(urb && urb->dev && urb->ep)) 0716 return; 0717 atomic_inc(&urb->reject); 0718 /* 0719 * Order the write of urb->reject above before the read 0720 * of urb->use_count below. Pairs with the barriers in 0721 * __usb_hcd_giveback_urb() and usb_hcd_submit_urb(). 0722 */ 0723 smp_mb__after_atomic(); 0724 0725 usb_hcd_unlink_urb(urb, -ENOENT); 0726 wait_event(usb_kill_urb_queue, atomic_read(&urb->use_count) == 0); 0727 0728 atomic_dec(&urb->reject); 0729 } 0730 EXPORT_SYMBOL_GPL(usb_kill_urb); 0731 0732 /** 0733 * usb_poison_urb - reliably kill a transfer and prevent further use of an URB 0734 * @urb: pointer to URB describing a previously submitted request, 0735 * may be NULL 0736 * 0737 * This routine cancels an in-progress request. It is guaranteed that 0738 * upon return all completion handlers will have finished and the URB 0739 * will be totally idle and cannot be reused. These features make 0740 * this an ideal way to stop I/O in a disconnect() callback. 0741 * If the request has not already finished or been unlinked 0742 * the completion handler will see urb->status == -ENOENT. 0743 * 0744 * After and while the routine runs, attempts to resubmit the URB will fail 0745 * with error -EPERM. Thus even if the URB's completion handler always 0746 * tries to resubmit, it will not succeed and the URB will become idle. 0747 * 0748 * The URB must not be deallocated while this routine is running. In 0749 * particular, when a driver calls this routine, it must insure that the 0750 * completion handler cannot deallocate the URB. 0751 * 0752 * This routine may not be used in an interrupt context (such as a bottom 0753 * half or a completion handler), or when holding a spinlock, or in other 0754 * situations where the caller can't schedule(). 0755 * 0756 * This routine should not be called by a driver after its disconnect 0757 * method has returned. 0758 */ 0759 void usb_poison_urb(struct urb *urb) 0760 { 0761 might_sleep(); 0762 if (!urb) 0763 return; 0764 atomic_inc(&urb->reject); 0765 /* 0766 * Order the write of urb->reject above before the read 0767 * of urb->use_count below. Pairs with the barriers in 0768 * __usb_hcd_giveback_urb() and usb_hcd_submit_urb(). 0769 */ 0770 smp_mb__after_atomic(); 0771 0772 if (!urb->dev || !urb->ep) 0773 return; 0774 0775 usb_hcd_unlink_urb(urb, -ENOENT); 0776 wait_event(usb_kill_urb_queue, atomic_read(&urb->use_count) == 0); 0777 } 0778 EXPORT_SYMBOL_GPL(usb_poison_urb); 0779 0780 void usb_unpoison_urb(struct urb *urb) 0781 { 0782 if (!urb) 0783 return; 0784 0785 atomic_dec(&urb->reject); 0786 } 0787 EXPORT_SYMBOL_GPL(usb_unpoison_urb); 0788 0789 /** 0790 * usb_block_urb - reliably prevent further use of an URB 0791 * @urb: pointer to URB to be blocked, may be NULL 0792 * 0793 * After the routine has run, attempts to resubmit the URB will fail 0794 * with error -EPERM. Thus even if the URB's completion handler always 0795 * tries to resubmit, it will not succeed and the URB will become idle. 0796 * 0797 * The URB must not be deallocated while this routine is running. In 0798 * particular, when a driver calls this routine, it must insure that the 0799 * completion handler cannot deallocate the URB. 0800 */ 0801 void usb_block_urb(struct urb *urb) 0802 { 0803 if (!urb) 0804 return; 0805 0806 atomic_inc(&urb->reject); 0807 } 0808 EXPORT_SYMBOL_GPL(usb_block_urb); 0809 0810 /** 0811 * usb_kill_anchored_urbs - kill all URBs associated with an anchor 0812 * @anchor: anchor the requests are bound to 0813 * 0814 * This kills all outstanding URBs starting from the back of the queue, 0815 * with guarantee that no completer callbacks will take place from the 0816 * anchor after this function returns. 0817 * 0818 * This routine should not be called by a driver after its disconnect 0819 * method has returned. 0820 */ 0821 void usb_kill_anchored_urbs(struct usb_anchor *anchor) 0822 { 0823 struct urb *victim; 0824 int surely_empty; 0825 0826 do { 0827 spin_lock_irq(&anchor->lock); 0828 while (!list_empty(&anchor->urb_list)) { 0829 victim = list_entry(anchor->urb_list.prev, 0830 struct urb, anchor_list); 0831 /* make sure the URB isn't freed before we kill it */ 0832 usb_get_urb(victim); 0833 spin_unlock_irq(&anchor->lock); 0834 /* this will unanchor the URB */ 0835 usb_kill_urb(victim); 0836 usb_put_urb(victim); 0837 spin_lock_irq(&anchor->lock); 0838 } 0839 surely_empty = usb_anchor_check_wakeup(anchor); 0840 0841 spin_unlock_irq(&anchor->lock); 0842 cpu_relax(); 0843 } while (!surely_empty); 0844 } 0845 EXPORT_SYMBOL_GPL(usb_kill_anchored_urbs); 0846 0847 0848 /** 0849 * usb_poison_anchored_urbs - cease all traffic from an anchor 0850 * @anchor: anchor the requests are bound to 0851 * 0852 * this allows all outstanding URBs to be poisoned starting 0853 * from the back of the queue. Newly added URBs will also be 0854 * poisoned 0855 * 0856 * This routine should not be called by a driver after its disconnect 0857 * method has returned. 0858 */ 0859 void usb_poison_anchored_urbs(struct usb_anchor *anchor) 0860 { 0861 struct urb *victim; 0862 int surely_empty; 0863 0864 do { 0865 spin_lock_irq(&anchor->lock); 0866 anchor->poisoned = 1; 0867 while (!list_empty(&anchor->urb_list)) { 0868 victim = list_entry(anchor->urb_list.prev, 0869 struct urb, anchor_list); 0870 /* make sure the URB isn't freed before we kill it */ 0871 usb_get_urb(victim); 0872 spin_unlock_irq(&anchor->lock); 0873 /* this will unanchor the URB */ 0874 usb_poison_urb(victim); 0875 usb_put_urb(victim); 0876 spin_lock_irq(&anchor->lock); 0877 } 0878 surely_empty = usb_anchor_check_wakeup(anchor); 0879 0880 spin_unlock_irq(&anchor->lock); 0881 cpu_relax(); 0882 } while (!surely_empty); 0883 } 0884 EXPORT_SYMBOL_GPL(usb_poison_anchored_urbs); 0885 0886 /** 0887 * usb_unpoison_anchored_urbs - let an anchor be used successfully again 0888 * @anchor: anchor the requests are bound to 0889 * 0890 * Reverses the effect of usb_poison_anchored_urbs 0891 * the anchor can be used normally after it returns 0892 */ 0893 void usb_unpoison_anchored_urbs(struct usb_anchor *anchor) 0894 { 0895 unsigned long flags; 0896 struct urb *lazarus; 0897 0898 spin_lock_irqsave(&anchor->lock, flags); 0899 list_for_each_entry(lazarus, &anchor->urb_list, anchor_list) { 0900 usb_unpoison_urb(lazarus); 0901 } 0902 anchor->poisoned = 0; 0903 spin_unlock_irqrestore(&anchor->lock, flags); 0904 } 0905 EXPORT_SYMBOL_GPL(usb_unpoison_anchored_urbs); 0906 /** 0907 * usb_unlink_anchored_urbs - asynchronously cancel transfer requests en masse 0908 * @anchor: anchor the requests are bound to 0909 * 0910 * this allows all outstanding URBs to be unlinked starting 0911 * from the back of the queue. This function is asynchronous. 0912 * The unlinking is just triggered. It may happen after this 0913 * function has returned. 0914 * 0915 * This routine should not be called by a driver after its disconnect 0916 * method has returned. 0917 */ 0918 void usb_unlink_anchored_urbs(struct usb_anchor *anchor) 0919 { 0920 struct urb *victim; 0921 0922 while ((victim = usb_get_from_anchor(anchor)) != NULL) { 0923 usb_unlink_urb(victim); 0924 usb_put_urb(victim); 0925 } 0926 } 0927 EXPORT_SYMBOL_GPL(usb_unlink_anchored_urbs); 0928 0929 /** 0930 * usb_anchor_suspend_wakeups 0931 * @anchor: the anchor you want to suspend wakeups on 0932 * 0933 * Call this to stop the last urb being unanchored from waking up any 0934 * usb_wait_anchor_empty_timeout waiters. This is used in the hcd urb give- 0935 * back path to delay waking up until after the completion handler has run. 0936 */ 0937 void usb_anchor_suspend_wakeups(struct usb_anchor *anchor) 0938 { 0939 if (anchor) 0940 atomic_inc(&anchor->suspend_wakeups); 0941 } 0942 EXPORT_SYMBOL_GPL(usb_anchor_suspend_wakeups); 0943 0944 /** 0945 * usb_anchor_resume_wakeups 0946 * @anchor: the anchor you want to resume wakeups on 0947 * 0948 * Allow usb_wait_anchor_empty_timeout waiters to be woken up again, and 0949 * wake up any current waiters if the anchor is empty. 0950 */ 0951 void usb_anchor_resume_wakeups(struct usb_anchor *anchor) 0952 { 0953 if (!anchor) 0954 return; 0955 0956 atomic_dec(&anchor->suspend_wakeups); 0957 if (usb_anchor_check_wakeup(anchor)) 0958 wake_up(&anchor->wait); 0959 } 0960 EXPORT_SYMBOL_GPL(usb_anchor_resume_wakeups); 0961 0962 /** 0963 * usb_wait_anchor_empty_timeout - wait for an anchor to be unused 0964 * @anchor: the anchor you want to become unused 0965 * @timeout: how long you are willing to wait in milliseconds 0966 * 0967 * Call this is you want to be sure all an anchor's 0968 * URBs have finished 0969 * 0970 * Return: Non-zero if the anchor became unused. Zero on timeout. 0971 */ 0972 int usb_wait_anchor_empty_timeout(struct usb_anchor *anchor, 0973 unsigned int timeout) 0974 { 0975 return wait_event_timeout(anchor->wait, 0976 usb_anchor_check_wakeup(anchor), 0977 msecs_to_jiffies(timeout)); 0978 } 0979 EXPORT_SYMBOL_GPL(usb_wait_anchor_empty_timeout); 0980 0981 /** 0982 * usb_get_from_anchor - get an anchor's oldest urb 0983 * @anchor: the anchor whose urb you want 0984 * 0985 * This will take the oldest urb from an anchor, 0986 * unanchor and return it 0987 * 0988 * Return: The oldest urb from @anchor, or %NULL if @anchor has no 0989 * urbs associated with it. 0990 */ 0991 struct urb *usb_get_from_anchor(struct usb_anchor *anchor) 0992 { 0993 struct urb *victim; 0994 unsigned long flags; 0995 0996 spin_lock_irqsave(&anchor->lock, flags); 0997 if (!list_empty(&anchor->urb_list)) { 0998 victim = list_entry(anchor->urb_list.next, struct urb, 0999 anchor_list); 1000 usb_get_urb(victim); 1001 __usb_unanchor_urb(victim, anchor); 1002 } else { 1003 victim = NULL; 1004 } 1005 spin_unlock_irqrestore(&anchor->lock, flags); 1006 1007 return victim; 1008 } 1009 1010 EXPORT_SYMBOL_GPL(usb_get_from_anchor); 1011 1012 /** 1013 * usb_scuttle_anchored_urbs - unanchor all an anchor's urbs 1014 * @anchor: the anchor whose urbs you want to unanchor 1015 * 1016 * use this to get rid of all an anchor's urbs 1017 */ 1018 void usb_scuttle_anchored_urbs(struct usb_anchor *anchor) 1019 { 1020 struct urb *victim; 1021 unsigned long flags; 1022 int surely_empty; 1023 1024 do { 1025 spin_lock_irqsave(&anchor->lock, flags); 1026 while (!list_empty(&anchor->urb_list)) { 1027 victim = list_entry(anchor->urb_list.prev, 1028 struct urb, anchor_list); 1029 __usb_unanchor_urb(victim, anchor); 1030 } 1031 surely_empty = usb_anchor_check_wakeup(anchor); 1032 1033 spin_unlock_irqrestore(&anchor->lock, flags); 1034 cpu_relax(); 1035 } while (!surely_empty); 1036 } 1037 1038 EXPORT_SYMBOL_GPL(usb_scuttle_anchored_urbs); 1039 1040 /** 1041 * usb_anchor_empty - is an anchor empty 1042 * @anchor: the anchor you want to query 1043 * 1044 * Return: 1 if the anchor has no urbs associated with it. 1045 */ 1046 int usb_anchor_empty(struct usb_anchor *anchor) 1047 { 1048 return list_empty(&anchor->urb_list); 1049 } 1050 1051 EXPORT_SYMBOL_GPL(usb_anchor_empty); 1052
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |