urb.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Released under the GPLv2 only.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/string.h>
  7. #include <linux/bitops.h>
  8. #include <linux/slab.h>
  9. #include <linux/log2.h>
  10. #include <linux/usb.h>
  11. #include <linux/wait.h>
  12. #include <linux/usb/hcd.h>
  13. #include <linux/scatterlist.h>
  14. #define to_urb(d) container_of(d, struct urb, kref)
  15. static void urb_destroy(struct kref *kref)
  16. {
  17. struct urb *urb = to_urb(kref);
  18. if (urb->transfer_flags & URB_FREE_BUFFER)
  19. kfree(urb->transfer_buffer);
  20. kfree(urb);
  21. }
  22. /**
  23. * usb_init_urb - initializes a urb so that it can be used by a USB driver
  24. * @urb: pointer to the urb to initialize
  25. *
  26. * Initializes a urb so that the USB subsystem can use it properly.
  27. *
  28. * If a urb is created with a call to usb_alloc_urb() it is not
  29. * necessary to call this function. Only use this if you allocate the
  30. * space for a struct urb on your own. If you call this function, be
  31. * careful when freeing the memory for your urb that it is no longer in
  32. * use by the USB core.
  33. *
  34. * Only use this function if you _really_ understand what you are doing.
  35. */
  36. void usb_init_urb(struct urb *urb)
  37. {
  38. if (urb) {
  39. memset(urb, 0, sizeof(*urb));
  40. kref_init(&urb->kref);
  41. INIT_LIST_HEAD(&urb->urb_list);
  42. INIT_LIST_HEAD(&urb->anchor_list);
  43. }
  44. }
  45. EXPORT_SYMBOL_GPL(usb_init_urb);
  46. /**
  47. * usb_alloc_urb - creates a new urb for a USB driver to use
  48. * @iso_packets: number of iso packets for this urb
  49. * @mem_flags: the type of memory to allocate, see kmalloc() for a list of
  50. * valid options for this.
  51. *
  52. * Creates an urb for the USB driver to use, initializes a few internal
  53. * structures, increments the usage counter, and returns a pointer to it.
  54. *
  55. * If the driver want to use this urb for interrupt, control, or bulk
  56. * endpoints, pass '0' as the number of iso packets.
  57. *
  58. * The driver must call usb_free_urb() when it is finished with the urb.
  59. *
  60. * Return: A pointer to the new urb, or %NULL if no memory is available.
  61. */
  62. struct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags)
  63. {
  64. struct urb *urb;
  65. urb = kmalloc(struct_size(urb, iso_frame_desc, iso_packets),
  66. mem_flags);
  67. if (!urb)
  68. return NULL;
  69. usb_init_urb(urb);
  70. return urb;
  71. }
  72. EXPORT_SYMBOL_GPL(usb_alloc_urb);
  73. /**
  74. * usb_free_urb - frees the memory used by a urb when all users of it are finished
  75. * @urb: pointer to the urb to free, may be NULL
  76. *
  77. * Must be called when a user of a urb is finished with it. When the last user
  78. * of the urb calls this function, the memory of the urb is freed.
  79. *
  80. * Note: The transfer buffer associated with the urb is not freed unless the
  81. * URB_FREE_BUFFER transfer flag is set.
  82. */
  83. void usb_free_urb(struct urb *urb)
  84. {
  85. if (urb)
  86. kref_put(&urb->kref, urb_destroy);
  87. }
  88. EXPORT_SYMBOL_GPL(usb_free_urb);
  89. /**
  90. * usb_get_urb - increments the reference count of the urb
  91. * @urb: pointer to the urb to modify, may be NULL
  92. *
  93. * This must be called whenever a urb is transferred from a device driver to a
  94. * host controller driver. This allows proper reference counting to happen
  95. * for urbs.
  96. *
  97. * Return: A pointer to the urb with the incremented reference counter.
  98. */
  99. struct urb *usb_get_urb(struct urb *urb)
  100. {
  101. if (urb)
  102. kref_get(&urb->kref);
  103. return urb;
  104. }
  105. EXPORT_SYMBOL_GPL(usb_get_urb);
  106. /**
  107. * usb_anchor_urb - anchors an URB while it is processed
  108. * @urb: pointer to the urb to anchor
  109. * @anchor: pointer to the anchor
  110. *
  111. * This can be called to have access to URBs which are to be executed
  112. * without bothering to track them
  113. */
  114. void usb_anchor_urb(struct urb *urb, struct usb_anchor *anchor)
  115. {
  116. unsigned long flags;
  117. spin_lock_irqsave(&anchor->lock, flags);
  118. usb_get_urb(urb);
  119. list_add_tail(&urb->anchor_list, &anchor->urb_list);
  120. urb->anchor = anchor;
  121. if (unlikely(anchor->poisoned))
  122. atomic_inc(&urb->reject);
  123. spin_unlock_irqrestore(&anchor->lock, flags);
  124. }
  125. EXPORT_SYMBOL_GPL(usb_anchor_urb);
  126. static int usb_anchor_check_wakeup(struct usb_anchor *anchor)
  127. {
  128. return atomic_read(&anchor->suspend_wakeups) == 0 &&
  129. list_empty(&anchor->urb_list);
  130. }
  131. /* Callers must hold anchor->lock */
  132. static void __usb_unanchor_urb(struct urb *urb, struct usb_anchor *anchor)
  133. {
  134. urb->anchor = NULL;
  135. list_del(&urb->anchor_list);
  136. usb_put_urb(urb);
  137. if (usb_anchor_check_wakeup(anchor))
  138. wake_up(&anchor->wait);
  139. }
  140. /**
  141. * usb_unanchor_urb - unanchors an URB
  142. * @urb: pointer to the urb to anchor
  143. *
  144. * Call this to stop the system keeping track of this URB
  145. */
  146. void usb_unanchor_urb(struct urb *urb)
  147. {
  148. unsigned long flags;
  149. struct usb_anchor *anchor;
  150. if (!urb)
  151. return;
  152. anchor = urb->anchor;
  153. if (!anchor)
  154. return;
  155. spin_lock_irqsave(&anchor->lock, flags);
  156. /*
  157. * At this point, we could be competing with another thread which
  158. * has the same intention. To protect the urb from being unanchored
  159. * twice, only the winner of the race gets the job.
  160. */
  161. if (likely(anchor == urb->anchor))
  162. __usb_unanchor_urb(urb, anchor);
  163. spin_unlock_irqrestore(&anchor->lock, flags);
  164. }
  165. EXPORT_SYMBOL_GPL(usb_unanchor_urb);
  166. /*-------------------------------------------------------------------*/
  167. static const int pipetypes[4] = {
  168. PIPE_CONTROL, PIPE_ISOCHRONOUS, PIPE_BULK, PIPE_INTERRUPT
  169. };
  170. /**
  171. * usb_pipe_type_check - sanity check of a specific pipe for a usb device
  172. * @dev: struct usb_device to be checked
  173. * @pipe: pipe to check
  174. *
  175. * This performs a light-weight sanity check for the endpoint in the
  176. * given usb device. It returns 0 if the pipe is valid for the specific usb
  177. * device, otherwise a negative error code.
  178. */
  179. int usb_pipe_type_check(struct usb_device *dev, unsigned int pipe)
  180. {
  181. const struct usb_host_endpoint *ep;
  182. ep = usb_pipe_endpoint(dev, pipe);
  183. if (!ep)
  184. return -EINVAL;
  185. if (usb_pipetype(pipe) != pipetypes[usb_endpoint_type(&ep->desc)])
  186. return -EINVAL;
  187. return 0;
  188. }
  189. EXPORT_SYMBOL_GPL(usb_pipe_type_check);
  190. /**
  191. * usb_urb_ep_type_check - sanity check of endpoint in the given urb
  192. * @urb: urb to be checked
  193. *
  194. * This performs a light-weight sanity check for the endpoint in the
  195. * given urb. It returns 0 if the urb contains a valid endpoint, otherwise
  196. * a negative error code.
  197. */
  198. int usb_urb_ep_type_check(const struct urb *urb)
  199. {
  200. return usb_pipe_type_check(urb->dev, urb->pipe);
  201. }
  202. EXPORT_SYMBOL_GPL(usb_urb_ep_type_check);
  203. /**
  204. * usb_submit_urb - issue an asynchronous transfer request for an endpoint
  205. * @urb: pointer to the urb describing the request
  206. * @mem_flags: the type of memory to allocate, see kmalloc() for a list
  207. * of valid options for this.
  208. *
  209. * This submits a transfer request, and transfers control of the URB
  210. * describing that request to the USB subsystem. Request completion will
  211. * be indicated later, asynchronously, by calling the completion handler.
  212. * The three types of completion are success, error, and unlink
  213. * (a software-induced fault, also called "request cancellation").
  214. *
  215. * URBs may be submitted in interrupt context.
  216. *
  217. * The caller must have correctly initialized the URB before submitting
  218. * it. Functions such as usb_fill_bulk_urb() and usb_fill_control_urb() are
  219. * available to ensure that most fields are correctly initialized, for
  220. * the particular kind of transfer, although they will not initialize
  221. * any transfer flags.
  222. *
  223. * If the submission is successful, the complete() callback from the URB
  224. * will be called exactly once, when the USB core and Host Controller Driver
  225. * (HCD) are finished with the URB. When the completion function is called,
  226. * control of the URB is returned to the device driver which issued the
  227. * request. The completion handler may then immediately free or reuse that
  228. * URB.
  229. *
  230. * With few exceptions, USB device drivers should never access URB fields
  231. * provided by usbcore or the HCD until its complete() is called.
  232. * The exceptions relate to periodic transfer scheduling. For both
  233. * interrupt and isochronous urbs, as part of successful URB submission
  234. * urb->interval is modified to reflect the actual transfer period used
  235. * (normally some power of two units). And for isochronous urbs,
  236. * urb->start_frame is modified to reflect when the URB's transfers were
  237. * scheduled to start.
  238. *
  239. * Not all isochronous transfer scheduling policies will work, but most
  240. * host controller drivers should easily handle ISO queues going from now
  241. * until 10-200 msec into the future. Drivers should try to keep at
  242. * least one or two msec of data in the queue; many controllers require
  243. * that new transfers start at least 1 msec in the future when they are
  244. * added. If the driver is unable to keep up and the queue empties out,
  245. * the behavior for new submissions is governed by the URB_ISO_ASAP flag.
  246. * If the flag is set, or if the queue is idle, then the URB is always
  247. * assigned to the first available (and not yet expired) slot in the
  248. * endpoint's schedule. If the flag is not set and the queue is active
  249. * then the URB is always assigned to the next slot in the schedule
  250. * following the end of the endpoint's previous URB, even if that slot is
  251. * in the past. When a packet is assigned in this way to a slot that has
  252. * already expired, the packet is not transmitted and the corresponding
  253. * usb_iso_packet_descriptor's status field will return -EXDEV. If this
  254. * would happen to all the packets in the URB, submission fails with a
  255. * -EXDEV error code.
  256. *
  257. * For control endpoints, the synchronous usb_control_msg() call is
  258. * often used (in non-interrupt context) instead of this call.
  259. * That is often used through convenience wrappers, for the requests
  260. * that are standardized in the USB 2.0 specification. For bulk
  261. * endpoints, a synchronous usb_bulk_msg() call is available.
  262. *
  263. * Return:
  264. * 0 on successful submissions. A negative error number otherwise.
  265. *
  266. * Request Queuing:
  267. *
  268. * URBs may be submitted to endpoints before previous ones complete, to
  269. * minimize the impact of interrupt latencies and system overhead on data
  270. * throughput. With that queuing policy, an endpoint's queue would never
  271. * be empty. This is required for continuous isochronous data streams,
  272. * and may also be required for some kinds of interrupt transfers. Such
  273. * queuing also maximizes bandwidth utilization by letting USB controllers
  274. * start work on later requests before driver software has finished the
  275. * completion processing for earlier (successful) requests.
  276. *
  277. * As of Linux 2.6, all USB endpoint transfer queues support depths greater
  278. * than one. This was previously a HCD-specific behavior, except for ISO
  279. * transfers. Non-isochronous endpoint queues are inactive during cleanup
  280. * after faults (transfer errors or cancellation).
  281. *
  282. * Reserved Bandwidth Transfers:
  283. *
  284. * Periodic transfers (interrupt or isochronous) are performed repeatedly,
  285. * using the interval specified in the urb. Submitting the first urb to
  286. * the endpoint reserves the bandwidth necessary to make those transfers.
  287. * If the USB subsystem can't allocate sufficient bandwidth to perform
  288. * the periodic request, submitting such a periodic request should fail.
  289. *
  290. * For devices under xHCI, the bandwidth is reserved at configuration time, or
  291. * when the alt setting is selected. If there is not enough bus bandwidth, the
  292. * configuration/alt setting request will fail. Therefore, submissions to
  293. * periodic endpoints on devices under xHCI should never fail due to bandwidth
  294. * constraints.
  295. *
  296. * Device drivers must explicitly request that repetition, by ensuring that
  297. * some URB is always on the endpoint's queue (except possibly for short
  298. * periods during completion callbacks). When there is no longer an urb
  299. * queued, the endpoint's bandwidth reservation is canceled. This means
  300. * drivers can use their completion handlers to ensure they keep bandwidth
  301. * they need, by reinitializing and resubmitting the just-completed urb
  302. * until the driver longer needs that periodic bandwidth.
  303. *
  304. * Memory Flags:
  305. *
  306. * The general rules for how to decide which mem_flags to use
  307. * are the same as for kmalloc. There are four
  308. * different possible values; GFP_KERNEL, GFP_NOFS, GFP_NOIO and
  309. * GFP_ATOMIC.
  310. *
  311. * GFP_NOFS is not ever used, as it has not been implemented yet.
  312. *
  313. * GFP_ATOMIC is used when
  314. * (a) you are inside a completion handler, an interrupt, bottom half,
  315. * tasklet or timer, or
  316. * (b) you are holding a spinlock or rwlock (does not apply to
  317. * semaphores), or
  318. * (c) current->state != TASK_RUNNING, this is the case only after
  319. * you've changed it.
  320. *
  321. * GFP_NOIO is used in the block io path and error handling of storage
  322. * devices.
  323. *
  324. * All other situations use GFP_KERNEL.
  325. *
  326. * Some more specific rules for mem_flags can be inferred, such as
  327. * (1) start_xmit, timeout, and receive methods of network drivers must
  328. * use GFP_ATOMIC (they are called with a spinlock held);
  329. * (2) queuecommand methods of scsi drivers must use GFP_ATOMIC (also
  330. * called with a spinlock held);
  331. * (3) If you use a kernel thread with a network driver you must use
  332. * GFP_NOIO, unless (b) or (c) apply;
  333. * (4) after you have done a down() you can use GFP_KERNEL, unless (b) or (c)
  334. * apply or your are in a storage driver's block io path;
  335. * (5) USB probe and disconnect can use GFP_KERNEL unless (b) or (c) apply; and
  336. * (6) changing firmware on a running storage or net device uses
  337. * GFP_NOIO, unless b) or c) apply
  338. *
  339. */
  340. int usb_submit_urb(struct urb *urb, gfp_t mem_flags)
  341. {
  342. int xfertype, max;
  343. struct usb_device *dev;
  344. struct usb_host_endpoint *ep;
  345. int is_out;
  346. unsigned int allowed;
  347. if (!urb || !urb->complete)
  348. return -EINVAL;
  349. if (urb->hcpriv) {
  350. WARN_ONCE(1, "URB %pK submitted while active\n", urb);
  351. return -EBUSY;
  352. }
  353. dev = urb->dev;
  354. if ((!dev) || (dev->state < USB_STATE_UNAUTHENTICATED))
  355. return -ENODEV;
  356. /* For now, get the endpoint from the pipe. Eventually drivers
  357. * will be required to set urb->ep directly and we will eliminate
  358. * urb->pipe.
  359. */
  360. ep = usb_pipe_endpoint(dev, urb->pipe);
  361. if (!ep)
  362. return -ENOENT;
  363. urb->ep = ep;
  364. urb->status = -EINPROGRESS;
  365. urb->actual_length = 0;
  366. /* Lots of sanity checks, so HCDs can rely on clean data
  367. * and don't need to duplicate tests
  368. */
  369. xfertype = usb_endpoint_type(&ep->desc);
  370. if (xfertype == USB_ENDPOINT_XFER_CONTROL) {
  371. struct usb_ctrlrequest *setup =
  372. (struct usb_ctrlrequest *) urb->setup_packet;
  373. if (!setup)
  374. return -ENOEXEC;
  375. is_out = !(setup->bRequestType & USB_DIR_IN) ||
  376. !setup->wLength;
  377. } else {
  378. is_out = usb_endpoint_dir_out(&ep->desc);
  379. }
  380. /* Clear the internal flags and cache the direction for later use */
  381. urb->transfer_flags &= ~(URB_DIR_MASK | URB_DMA_MAP_SINGLE |
  382. URB_DMA_MAP_PAGE | URB_DMA_MAP_SG | URB_MAP_LOCAL |
  383. URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL |
  384. URB_DMA_SG_COMBINED);
  385. urb->transfer_flags |= (is_out ? URB_DIR_OUT : URB_DIR_IN);
  386. if (xfertype != USB_ENDPOINT_XFER_CONTROL &&
  387. dev->state < USB_STATE_CONFIGURED)
  388. return -ENODEV;
  389. max = usb_endpoint_maxp(&ep->desc);
  390. if (max <= 0) {
  391. dev_dbg(&dev->dev,
  392. "bogus endpoint ep%d%s in %s (bad maxpacket %d)\n",
  393. usb_endpoint_num(&ep->desc), is_out ? "out" : "in",
  394. __func__, max);
  395. return -EMSGSIZE;
  396. }
  397. /* periodic transfers limit size per frame/uframe,
  398. * but drivers only control those sizes for ISO.
  399. * while we're checking, initialize return status.
  400. */
  401. if (xfertype == USB_ENDPOINT_XFER_ISOC) {
  402. int n, len;
  403. /* SuperSpeed isoc endpoints have up to 16 bursts of up to
  404. * 3 packets each
  405. */
  406. if (dev->speed >= USB_SPEED_SUPER) {
  407. int burst = 1 + ep->ss_ep_comp.bMaxBurst;
  408. int mult = USB_SS_MULT(ep->ss_ep_comp.bmAttributes);
  409. max *= burst;
  410. max *= mult;
  411. }
  412. if (dev->speed == USB_SPEED_SUPER_PLUS &&
  413. USB_SS_SSP_ISOC_COMP(ep->ss_ep_comp.bmAttributes)) {
  414. struct usb_ssp_isoc_ep_comp_descriptor *isoc_ep_comp;
  415. isoc_ep_comp = &ep->ssp_isoc_ep_comp;
  416. max = le32_to_cpu(isoc_ep_comp->dwBytesPerInterval);
  417. }
  418. /* "high bandwidth" mode, 1-3 packets/uframe? */
  419. if (dev->speed == USB_SPEED_HIGH)
  420. max *= usb_endpoint_maxp_mult(&ep->desc);
  421. if (urb->number_of_packets <= 0)
  422. return -EINVAL;
  423. for (n = 0; n < urb->number_of_packets; n++) {
  424. len = urb->iso_frame_desc[n].length;
  425. if (len < 0 || len > max)
  426. return -EMSGSIZE;
  427. urb->iso_frame_desc[n].status = -EXDEV;
  428. urb->iso_frame_desc[n].actual_length = 0;
  429. }
  430. } else if (urb->num_sgs && !urb->dev->bus->no_sg_constraint &&
  431. dev->speed != USB_SPEED_WIRELESS) {
  432. struct scatterlist *sg;
  433. int i;
  434. for_each_sg(urb->sg, sg, urb->num_sgs - 1, i)
  435. if (sg->length % max)
  436. return -EINVAL;
  437. }
  438. /* the I/O buffer must be mapped/unmapped, except when length=0 */
  439. if (urb->transfer_buffer_length > INT_MAX)
  440. return -EMSGSIZE;
  441. /*
  442. * stuff that drivers shouldn't do, but which shouldn't
  443. * cause problems in HCDs if they get it wrong.
  444. */
  445. /* Check that the pipe's type matches the endpoint's type */
  446. if (usb_pipe_type_check(urb->dev, urb->pipe))
  447. dev_WARN(&dev->dev, "BOGUS urb xfer, pipe %x != type %x\n",
  448. usb_pipetype(urb->pipe), pipetypes[xfertype]);
  449. /* Check against a simple/standard policy */
  450. allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT | URB_DIR_MASK |
  451. URB_FREE_BUFFER);
  452. switch (xfertype) {
  453. case USB_ENDPOINT_XFER_BULK:
  454. case USB_ENDPOINT_XFER_INT:
  455. if (is_out)
  456. allowed |= URB_ZERO_PACKET;
  457. fallthrough;
  458. default: /* all non-iso endpoints */
  459. if (!is_out)
  460. allowed |= URB_SHORT_NOT_OK;
  461. break;
  462. case USB_ENDPOINT_XFER_ISOC:
  463. allowed |= URB_ISO_ASAP;
  464. break;
  465. }
  466. allowed &= urb->transfer_flags;
  467. /* warn if submitter gave bogus flags */
  468. if (allowed != urb->transfer_flags)
  469. dev_WARN(&dev->dev, "BOGUS urb flags, %x --> %x\n",
  470. urb->transfer_flags, allowed);
  471. /*
  472. * Force periodic transfer intervals to be legal values that are
  473. * a power of two (so HCDs don't need to).
  474. *
  475. * FIXME want bus->{intr,iso}_sched_horizon values here. Each HC
  476. * supports different values... this uses EHCI/UHCI defaults (and
  477. * EHCI can use smaller non-default values).
  478. */
  479. switch (xfertype) {
  480. case USB_ENDPOINT_XFER_ISOC:
  481. case USB_ENDPOINT_XFER_INT:
  482. /* too small? */
  483. switch (dev->speed) {
  484. case USB_SPEED_WIRELESS:
  485. if ((urb->interval < 6)
  486. && (xfertype == USB_ENDPOINT_XFER_INT))
  487. return -EINVAL;
  488. fallthrough;
  489. default:
  490. if (urb->interval <= 0)
  491. return -EINVAL;
  492. break;
  493. }
  494. /* too big? */
  495. switch (dev->speed) {
  496. case USB_SPEED_SUPER_PLUS:
  497. case USB_SPEED_SUPER: /* units are 125us */
  498. /* Handle up to 2^(16-1) microframes */
  499. if (urb->interval > (1 << 15))
  500. return -EINVAL;
  501. max = 1 << 15;
  502. break;
  503. case USB_SPEED_WIRELESS:
  504. if (urb->interval > 16)
  505. return -EINVAL;
  506. break;
  507. case USB_SPEED_HIGH: /* units are microframes */
  508. /* NOTE usb handles 2^15 */
  509. if (urb->interval > (1024 * 8))
  510. urb->interval = 1024 * 8;
  511. max = 1024 * 8;
  512. break;
  513. case USB_SPEED_FULL: /* units are frames/msec */
  514. case USB_SPEED_LOW:
  515. if (xfertype == USB_ENDPOINT_XFER_INT) {
  516. if (urb->interval > 255)
  517. return -EINVAL;
  518. /* NOTE ohci only handles up to 32 */
  519. max = 128;
  520. } else {
  521. if (urb->interval > 1024)
  522. urb->interval = 1024;
  523. /* NOTE usb and ohci handle up to 2^15 */
  524. max = 1024;
  525. }
  526. break;
  527. default:
  528. return -EINVAL;
  529. }
  530. if (dev->speed != USB_SPEED_WIRELESS) {
  531. /* Round down to a power of 2, no more than max */
  532. urb->interval = min(max, 1 << ilog2(urb->interval));
  533. }
  534. }
  535. return usb_hcd_submit_urb(urb, mem_flags);
  536. }
  537. EXPORT_SYMBOL_GPL(usb_submit_urb);
  538. /*-------------------------------------------------------------------*/
  539. /**
  540. * usb_unlink_urb - abort/cancel a transfer request for an endpoint
  541. * @urb: pointer to urb describing a previously submitted request,
  542. * may be NULL
  543. *
  544. * This routine cancels an in-progress request. URBs complete only once
  545. * per submission, and may be canceled only once per submission.
  546. * Successful cancellation means termination of @urb will be expedited
  547. * and the completion handler will be called with a status code
  548. * indicating that the request has been canceled (rather than any other
  549. * code).
  550. *
  551. * Drivers should not call this routine or related routines, such as
  552. * usb_kill_urb() or usb_unlink_anchored_urbs(), after their disconnect
  553. * method has returned. The disconnect function should synchronize with
  554. * a driver's I/O routines to insure that all URB-related activity has
  555. * completed before it returns.
  556. *
  557. * This request is asynchronous, however the HCD might call the ->complete()
  558. * callback during unlink. Therefore when drivers call usb_unlink_urb(), they
  559. * must not hold any locks that may be taken by the completion function.
  560. * Success is indicated by returning -EINPROGRESS, at which time the URB will
  561. * probably not yet have been given back to the device driver. When it is
  562. * eventually called, the completion function will see @urb->status ==
  563. * -ECONNRESET.
  564. * Failure is indicated by usb_unlink_urb() returning any other value.
  565. * Unlinking will fail when @urb is not currently "linked" (i.e., it was
  566. * never submitted, or it was unlinked before, or the hardware is already
  567. * finished with it), even if the completion handler has not yet run.
  568. *
  569. * The URB must not be deallocated while this routine is running. In
  570. * particular, when a driver calls this routine, it must insure that the
  571. * completion handler cannot deallocate the URB.
  572. *
  573. * Return: -EINPROGRESS on success. See description for other values on
  574. * failure.
  575. *
  576. * Unlinking and Endpoint Queues:
  577. *
  578. * [The behaviors and guarantees described below do not apply to virtual
  579. * root hubs but only to endpoint queues for physical USB devices.]
  580. *
  581. * Host Controller Drivers (HCDs) place all the URBs for a particular
  582. * endpoint in a queue. Normally the queue advances as the controller
  583. * hardware processes each request. But when an URB terminates with an
  584. * error its queue generally stops (see below), at least until that URB's
  585. * completion routine returns. It is guaranteed that a stopped queue
  586. * will not restart until all its unlinked URBs have been fully retired,
  587. * with their completion routines run, even if that's not until some time
  588. * after the original completion handler returns. The same behavior and
  589. * guarantee apply when an URB terminates because it was unlinked.
  590. *
  591. * Bulk and interrupt endpoint queues are guaranteed to stop whenever an
  592. * URB terminates with any sort of error, including -ECONNRESET, -ENOENT,
  593. * and -EREMOTEIO. Control endpoint queues behave the same way except
  594. * that they are not guaranteed to stop for -EREMOTEIO errors. Queues
  595. * for isochronous endpoints are treated differently, because they must
  596. * advance at fixed rates. Such queues do not stop when an URB
  597. * encounters an error or is unlinked. An unlinked isochronous URB may
  598. * leave a gap in the stream of packets; it is undefined whether such
  599. * gaps can be filled in.
  600. *
  601. * Note that early termination of an URB because a short packet was
  602. * received will generate a -EREMOTEIO error if and only if the
  603. * URB_SHORT_NOT_OK flag is set. By setting this flag, USB device
  604. * drivers can build deep queues for large or complex bulk transfers
  605. * and clean them up reliably after any sort of aborted transfer by
  606. * unlinking all pending URBs at the first fault.
  607. *
  608. * When a control URB terminates with an error other than -EREMOTEIO, it
  609. * is quite likely that the status stage of the transfer will not take
  610. * place.
  611. */
  612. int usb_unlink_urb(struct urb *urb)
  613. {
  614. if (!urb)
  615. return -EINVAL;
  616. if (!urb->dev)
  617. return -ENODEV;
  618. if (!urb->ep)
  619. return -EIDRM;
  620. return usb_hcd_unlink_urb(urb, -ECONNRESET);
  621. }
  622. EXPORT_SYMBOL_GPL(usb_unlink_urb);
  623. /**
  624. * usb_kill_urb - cancel a transfer request and wait for it to finish
  625. * @urb: pointer to URB describing a previously submitted request,
  626. * may be NULL
  627. *
  628. * This routine cancels an in-progress request. It is guaranteed that
  629. * upon return all completion handlers will have finished and the URB
  630. * will be totally idle and available for reuse. These features make
  631. * this an ideal way to stop I/O in a disconnect() callback or close()
  632. * function. If the request has not already finished or been unlinked
  633. * the completion handler will see urb->status == -ENOENT.
  634. *
  635. * While the routine is running, attempts to resubmit the URB will fail
  636. * with error -EPERM. Thus even if the URB's completion handler always
  637. * tries to resubmit, it will not succeed and the URB will become idle.
  638. *
  639. * The URB must not be deallocated while this routine is running. In
  640. * particular, when a driver calls this routine, it must insure that the
  641. * completion handler cannot deallocate the URB.
  642. *
  643. * This routine may not be used in an interrupt context (such as a bottom
  644. * half or a completion handler), or when holding a spinlock, or in other
  645. * situations where the caller can't schedule().
  646. *
  647. * This routine should not be called by a driver after its disconnect
  648. * method has returned.
  649. */
  650. void usb_kill_urb(struct urb *urb)
  651. {
  652. might_sleep();
  653. if (!(urb && urb->dev && urb->ep))
  654. return;
  655. atomic_inc(&urb->reject);
  656. /*
  657. * Order the write of urb->reject above before the read
  658. * of urb->use_count below. Pairs with the barriers in
  659. * __usb_hcd_giveback_urb() and usb_hcd_submit_urb().
  660. */
  661. smp_mb__after_atomic();
  662. usb_hcd_unlink_urb(urb, -ENOENT);
  663. wait_event(usb_kill_urb_queue, atomic_read(&urb->use_count) == 0);
  664. atomic_dec(&urb->reject);
  665. }
  666. EXPORT_SYMBOL_GPL(usb_kill_urb);
  667. /**
  668. * usb_poison_urb - reliably kill a transfer and prevent further use of an URB
  669. * @urb: pointer to URB describing a previously submitted request,
  670. * may be NULL
  671. *
  672. * This routine cancels an in-progress request. It is guaranteed that
  673. * upon return all completion handlers will have finished and the URB
  674. * will be totally idle and cannot be reused. These features make
  675. * this an ideal way to stop I/O in a disconnect() callback.
  676. * If the request has not already finished or been unlinked
  677. * the completion handler will see urb->status == -ENOENT.
  678. *
  679. * After and while the routine runs, attempts to resubmit the URB will fail
  680. * with error -EPERM. Thus even if the URB's completion handler always
  681. * tries to resubmit, it will not succeed and the URB will become idle.
  682. *
  683. * The URB must not be deallocated while this routine is running. In
  684. * particular, when a driver calls this routine, it must insure that the
  685. * completion handler cannot deallocate the URB.
  686. *
  687. * This routine may not be used in an interrupt context (such as a bottom
  688. * half or a completion handler), or when holding a spinlock, or in other
  689. * situations where the caller can't schedule().
  690. *
  691. * This routine should not be called by a driver after its disconnect
  692. * method has returned.
  693. */
  694. void usb_poison_urb(struct urb *urb)
  695. {
  696. might_sleep();
  697. if (!urb)
  698. return;
  699. atomic_inc(&urb->reject);
  700. /*
  701. * Order the write of urb->reject above before the read
  702. * of urb->use_count below. Pairs with the barriers in
  703. * __usb_hcd_giveback_urb() and usb_hcd_submit_urb().
  704. */
  705. smp_mb__after_atomic();
  706. if (!urb->dev || !urb->ep)
  707. return;
  708. usb_hcd_unlink_urb(urb, -ENOENT);
  709. wait_event(usb_kill_urb_queue, atomic_read(&urb->use_count) == 0);
  710. }
  711. EXPORT_SYMBOL_GPL(usb_poison_urb);
  712. void usb_unpoison_urb(struct urb *urb)
  713. {
  714. if (!urb)
  715. return;
  716. atomic_dec(&urb->reject);
  717. }
  718. EXPORT_SYMBOL_GPL(usb_unpoison_urb);
  719. /**
  720. * usb_block_urb - reliably prevent further use of an URB
  721. * @urb: pointer to URB to be blocked, may be NULL
  722. *
  723. * After the routine has run, attempts to resubmit the URB will fail
  724. * with error -EPERM. Thus even if the URB's completion handler always
  725. * tries to resubmit, it will not succeed and the URB will become idle.
  726. *
  727. * The URB must not be deallocated while this routine is running. In
  728. * particular, when a driver calls this routine, it must insure that the
  729. * completion handler cannot deallocate the URB.
  730. */
  731. void usb_block_urb(struct urb *urb)
  732. {
  733. if (!urb)
  734. return;
  735. atomic_inc(&urb->reject);
  736. }
  737. EXPORT_SYMBOL_GPL(usb_block_urb);
  738. /**
  739. * usb_kill_anchored_urbs - kill all URBs associated with an anchor
  740. * @anchor: anchor the requests are bound to
  741. *
  742. * This kills all outstanding URBs starting from the back of the queue,
  743. * with guarantee that no completer callbacks will take place from the
  744. * anchor after this function returns.
  745. *
  746. * This routine should not be called by a driver after its disconnect
  747. * method has returned.
  748. */
  749. void usb_kill_anchored_urbs(struct usb_anchor *anchor)
  750. {
  751. struct urb *victim;
  752. int surely_empty;
  753. do {
  754. spin_lock_irq(&anchor->lock);
  755. while (!list_empty(&anchor->urb_list)) {
  756. victim = list_entry(anchor->urb_list.prev,
  757. struct urb, anchor_list);
  758. /* make sure the URB isn't freed before we kill it */
  759. usb_get_urb(victim);
  760. spin_unlock_irq(&anchor->lock);
  761. /* this will unanchor the URB */
  762. usb_kill_urb(victim);
  763. usb_put_urb(victim);
  764. spin_lock_irq(&anchor->lock);
  765. }
  766. surely_empty = usb_anchor_check_wakeup(anchor);
  767. spin_unlock_irq(&anchor->lock);
  768. cpu_relax();
  769. } while (!surely_empty);
  770. }
  771. EXPORT_SYMBOL_GPL(usb_kill_anchored_urbs);
  772. /**
  773. * usb_poison_anchored_urbs - cease all traffic from an anchor
  774. * @anchor: anchor the requests are bound to
  775. *
  776. * this allows all outstanding URBs to be poisoned starting
  777. * from the back of the queue. Newly added URBs will also be
  778. * poisoned
  779. *
  780. * This routine should not be called by a driver after its disconnect
  781. * method has returned.
  782. */
  783. void usb_poison_anchored_urbs(struct usb_anchor *anchor)
  784. {
  785. struct urb *victim;
  786. int surely_empty;
  787. do {
  788. spin_lock_irq(&anchor->lock);
  789. anchor->poisoned = 1;
  790. while (!list_empty(&anchor->urb_list)) {
  791. victim = list_entry(anchor->urb_list.prev,
  792. struct urb, anchor_list);
  793. /* make sure the URB isn't freed before we kill it */
  794. usb_get_urb(victim);
  795. spin_unlock_irq(&anchor->lock);
  796. /* this will unanchor the URB */
  797. usb_poison_urb(victim);
  798. usb_put_urb(victim);
  799. spin_lock_irq(&anchor->lock);
  800. }
  801. surely_empty = usb_anchor_check_wakeup(anchor);
  802. spin_unlock_irq(&anchor->lock);
  803. cpu_relax();
  804. } while (!surely_empty);
  805. }
  806. EXPORT_SYMBOL_GPL(usb_poison_anchored_urbs);
  807. /**
  808. * usb_unpoison_anchored_urbs - let an anchor be used successfully again
  809. * @anchor: anchor the requests are bound to
  810. *
  811. * Reverses the effect of usb_poison_anchored_urbs
  812. * the anchor can be used normally after it returns
  813. */
  814. void usb_unpoison_anchored_urbs(struct usb_anchor *anchor)
  815. {
  816. unsigned long flags;
  817. struct urb *lazarus;
  818. spin_lock_irqsave(&anchor->lock, flags);
  819. list_for_each_entry(lazarus, &anchor->urb_list, anchor_list) {
  820. usb_unpoison_urb(lazarus);
  821. }
  822. anchor->poisoned = 0;
  823. spin_unlock_irqrestore(&anchor->lock, flags);
  824. }
  825. EXPORT_SYMBOL_GPL(usb_unpoison_anchored_urbs);
  826. /**
  827. * usb_unlink_anchored_urbs - asynchronously cancel transfer requests en masse
  828. * @anchor: anchor the requests are bound to
  829. *
  830. * this allows all outstanding URBs to be unlinked starting
  831. * from the back of the queue. This function is asynchronous.
  832. * The unlinking is just triggered. It may happen after this
  833. * function has returned.
  834. *
  835. * This routine should not be called by a driver after its disconnect
  836. * method has returned.
  837. */
  838. void usb_unlink_anchored_urbs(struct usb_anchor *anchor)
  839. {
  840. struct urb *victim;
  841. while ((victim = usb_get_from_anchor(anchor)) != NULL) {
  842. usb_unlink_urb(victim);
  843. usb_put_urb(victim);
  844. }
  845. }
  846. EXPORT_SYMBOL_GPL(usb_unlink_anchored_urbs);
  847. /**
  848. * usb_anchor_suspend_wakeups
  849. * @anchor: the anchor you want to suspend wakeups on
  850. *
  851. * Call this to stop the last urb being unanchored from waking up any
  852. * usb_wait_anchor_empty_timeout waiters. This is used in the hcd urb give-
  853. * back path to delay waking up until after the completion handler has run.
  854. */
  855. void usb_anchor_suspend_wakeups(struct usb_anchor *anchor)
  856. {
  857. if (anchor)
  858. atomic_inc(&anchor->suspend_wakeups);
  859. }
  860. EXPORT_SYMBOL_GPL(usb_anchor_suspend_wakeups);
  861. /**
  862. * usb_anchor_resume_wakeups
  863. * @anchor: the anchor you want to resume wakeups on
  864. *
  865. * Allow usb_wait_anchor_empty_timeout waiters to be woken up again, and
  866. * wake up any current waiters if the anchor is empty.
  867. */
  868. void usb_anchor_resume_wakeups(struct usb_anchor *anchor)
  869. {
  870. if (!anchor)
  871. return;
  872. atomic_dec(&anchor->suspend_wakeups);
  873. if (usb_anchor_check_wakeup(anchor))
  874. wake_up(&anchor->wait);
  875. }
  876. EXPORT_SYMBOL_GPL(usb_anchor_resume_wakeups);
  877. /**
  878. * usb_wait_anchor_empty_timeout - wait for an anchor to be unused
  879. * @anchor: the anchor you want to become unused
  880. * @timeout: how long you are willing to wait in milliseconds
  881. *
  882. * Call this is you want to be sure all an anchor's
  883. * URBs have finished
  884. *
  885. * Return: Non-zero if the anchor became unused. Zero on timeout.
  886. */
  887. int usb_wait_anchor_empty_timeout(struct usb_anchor *anchor,
  888. unsigned int timeout)
  889. {
  890. return wait_event_timeout(anchor->wait,
  891. usb_anchor_check_wakeup(anchor),
  892. msecs_to_jiffies(timeout));
  893. }
  894. EXPORT_SYMBOL_GPL(usb_wait_anchor_empty_timeout);
  895. /**
  896. * usb_get_from_anchor - get an anchor's oldest urb
  897. * @anchor: the anchor whose urb you want
  898. *
  899. * This will take the oldest urb from an anchor,
  900. * unanchor and return it
  901. *
  902. * Return: The oldest urb from @anchor, or %NULL if @anchor has no
  903. * urbs associated with it.
  904. */
  905. struct urb *usb_get_from_anchor(struct usb_anchor *anchor)
  906. {
  907. struct urb *victim;
  908. unsigned long flags;
  909. spin_lock_irqsave(&anchor->lock, flags);
  910. if (!list_empty(&anchor->urb_list)) {
  911. victim = list_entry(anchor->urb_list.next, struct urb,
  912. anchor_list);
  913. usb_get_urb(victim);
  914. __usb_unanchor_urb(victim, anchor);
  915. } else {
  916. victim = NULL;
  917. }
  918. spin_unlock_irqrestore(&anchor->lock, flags);
  919. return victim;
  920. }
  921. EXPORT_SYMBOL_GPL(usb_get_from_anchor);
  922. /**
  923. * usb_scuttle_anchored_urbs - unanchor all an anchor's urbs
  924. * @anchor: the anchor whose urbs you want to unanchor
  925. *
  926. * use this to get rid of all an anchor's urbs
  927. */
  928. void usb_scuttle_anchored_urbs(struct usb_anchor *anchor)
  929. {
  930. struct urb *victim;
  931. unsigned long flags;
  932. int surely_empty;
  933. do {
  934. spin_lock_irqsave(&anchor->lock, flags);
  935. while (!list_empty(&anchor->urb_list)) {
  936. victim = list_entry(anchor->urb_list.prev,
  937. struct urb, anchor_list);
  938. __usb_unanchor_urb(victim, anchor);
  939. }
  940. surely_empty = usb_anchor_check_wakeup(anchor);
  941. spin_unlock_irqrestore(&anchor->lock, flags);
  942. cpu_relax();
  943. } while (!surely_empty);
  944. }
  945. EXPORT_SYMBOL_GPL(usb_scuttle_anchored_urbs);
  946. /**
  947. * usb_anchor_empty - is an anchor empty
  948. * @anchor: the anchor you want to query
  949. *
  950. * Return: 1 if the anchor has no urbs associated with it.
  951. */
  952. int usb_anchor_empty(struct usb_anchor *anchor)
  953. {
  954. return list_empty(&anchor->urb_list);
  955. }
  956. EXPORT_SYMBOL_GPL(usb_anchor_empty);