URB.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. .. _usb-urb:
  2. USB Request Block (URB)
  3. ~~~~~~~~~~~~~~~~~~~~~~~
  4. :Revised: 2000-Dec-05
  5. :Again: 2002-Jul-06
  6. :Again: 2005-Sep-19
  7. :Again: 2017-Mar-29
  8. .. note::
  9. The USB subsystem now has a substantial section at :ref:`usb-hostside-api`
  10. section, generated from the current source code.
  11. This particular documentation file isn't complete and may not be
  12. updated to the last version; don't rely on it except for a quick
  13. overview.
  14. Basic concept or 'What is an URB?'
  15. ==================================
  16. The basic idea of the new driver is message passing, the message itself is
  17. called USB Request Block, or URB for short.
  18. - An URB consists of all relevant information to execute any USB transaction
  19. and deliver the data and status back.
  20. - Execution of an URB is inherently an asynchronous operation, i.e. the
  21. :c:func:`usb_submit_urb` call returns immediately after it has successfully
  22. queued the requested action.
  23. - Transfers for one URB can be canceled with :c:func:`usb_unlink_urb`
  24. at any time.
  25. - Each URB has a completion handler, which is called after the action
  26. has been successfully completed or canceled. The URB also contains a
  27. context-pointer for passing information to the completion handler.
  28. - Each endpoint for a device logically supports a queue of requests.
  29. You can fill that queue, so that the USB hardware can still transfer
  30. data to an endpoint while your driver handles completion of another.
  31. This maximizes use of USB bandwidth, and supports seamless streaming
  32. of data to (or from) devices when using periodic transfer modes.
  33. The URB structure
  34. =================
  35. Some of the fields in struct urb are::
  36. struct urb
  37. {
  38. // (IN) device and pipe specify the endpoint queue
  39. struct usb_device *dev; // pointer to associated USB device
  40. unsigned int pipe; // endpoint information
  41. unsigned int transfer_flags; // URB_ISO_ASAP, URB_SHORT_NOT_OK, etc.
  42. // (IN) all urbs need completion routines
  43. void *context; // context for completion routine
  44. usb_complete_t complete; // pointer to completion routine
  45. // (OUT) status after each completion
  46. int status; // returned status
  47. // (IN) buffer used for data transfers
  48. void *transfer_buffer; // associated data buffer
  49. u32 transfer_buffer_length; // data buffer length
  50. int number_of_packets; // size of iso_frame_desc
  51. // (OUT) sometimes only part of CTRL/BULK/INTR transfer_buffer is used
  52. u32 actual_length; // actual data buffer length
  53. // (IN) setup stage for CTRL (pass a struct usb_ctrlrequest)
  54. unsigned char *setup_packet; // setup packet (control only)
  55. // Only for PERIODIC transfers (ISO, INTERRUPT)
  56. // (IN/OUT) start_frame is set unless URB_ISO_ASAP isn't set
  57. int start_frame; // start frame
  58. int interval; // polling interval
  59. // ISO only: packets are only "best effort"; each can have errors
  60. int error_count; // number of errors
  61. struct usb_iso_packet_descriptor iso_frame_desc[0];
  62. };
  63. Your driver must create the "pipe" value using values from the appropriate
  64. endpoint descriptor in an interface that it's claimed.
  65. How to get an URB?
  66. ==================
  67. URBs are allocated by calling :c:func:`usb_alloc_urb`::
  68. struct urb *usb_alloc_urb(int isoframes, int mem_flags)
  69. Return value is a pointer to the allocated URB, 0 if allocation failed.
  70. The parameter isoframes specifies the number of isochronous transfer frames
  71. you want to schedule. For CTRL/BULK/INT, use 0. The mem_flags parameter
  72. holds standard memory allocation flags, letting you control (among other
  73. things) whether the underlying code may block or not.
  74. To free an URB, use :c:func:`usb_free_urb`::
  75. void usb_free_urb(struct urb *urb)
  76. You may free an urb that you've submitted, but which hasn't yet been
  77. returned to you in a completion callback. It will automatically be
  78. deallocated when it is no longer in use.
  79. What has to be filled in?
  80. =========================
  81. Depending on the type of transaction, there are some inline functions
  82. defined in ``linux/usb.h`` to simplify the initialization, such as
  83. :c:func:`usb_fill_control_urb`, :c:func:`usb_fill_bulk_urb` and
  84. :c:func:`usb_fill_int_urb`. In general, they need the usb device pointer,
  85. the pipe (usual format from usb.h), the transfer buffer, the desired transfer
  86. length, the completion handler, and its context. Take a look at the some
  87. existing drivers to see how they're used.
  88. Flags:
  89. - For ISO there are two startup behaviors: Specified start_frame or ASAP.
  90. - For ASAP set ``URB_ISO_ASAP`` in transfer_flags.
  91. If short packets should NOT be tolerated, set ``URB_SHORT_NOT_OK`` in
  92. transfer_flags.
  93. How to submit an URB?
  94. =====================
  95. Just call :c:func:`usb_submit_urb`::
  96. int usb_submit_urb(struct urb *urb, int mem_flags)
  97. The ``mem_flags`` parameter, such as ``GFP_ATOMIC``, controls memory
  98. allocation, such as whether the lower levels may block when memory is tight.
  99. It immediately returns, either with status 0 (request queued) or some
  100. error code, usually caused by the following:
  101. - Out of memory (``-ENOMEM``)
  102. - Unplugged device (``-ENODEV``)
  103. - Stalled endpoint (``-EPIPE``)
  104. - Too many queued ISO transfers (``-EAGAIN``)
  105. - Too many requested ISO frames (``-EFBIG``)
  106. - Invalid INT interval (``-EINVAL``)
  107. - More than one packet for INT (``-EINVAL``)
  108. After submission, ``urb->status`` is ``-EINPROGRESS``; however, you should
  109. never look at that value except in your completion callback.
  110. For isochronous endpoints, your completion handlers should (re)submit
  111. URBs to the same endpoint with the ``URB_ISO_ASAP`` flag, using
  112. multi-buffering, to get seamless ISO streaming.
  113. How to cancel an already running URB?
  114. =====================================
  115. There are two ways to cancel an URB you've submitted but which hasn't
  116. been returned to your driver yet. For an asynchronous cancel, call
  117. :c:func:`usb_unlink_urb`::
  118. int usb_unlink_urb(struct urb *urb)
  119. It removes the urb from the internal list and frees all allocated
  120. HW descriptors. The status is changed to reflect unlinking. Note
  121. that the URB will not normally have finished when :c:func:`usb_unlink_urb`
  122. returns; you must still wait for the completion handler to be called.
  123. To cancel an URB synchronously, call :c:func:`usb_kill_urb`::
  124. void usb_kill_urb(struct urb *urb)
  125. It does everything :c:func:`usb_unlink_urb` does, and in addition it waits
  126. until after the URB has been returned and the completion handler
  127. has finished. It also marks the URB as temporarily unusable, so
  128. that if the completion handler or anyone else tries to resubmit it
  129. they will get a ``-EPERM`` error. Thus you can be sure that when
  130. :c:func:`usb_kill_urb` returns, the URB is totally idle.
  131. There is a lifetime issue to consider. An URB may complete at any
  132. time, and the completion handler may free the URB. If this happens
  133. while :c:func:`usb_unlink_urb` or :c:func:`usb_kill_urb` is running, it will
  134. cause a memory-access violation. The driver is responsible for avoiding this,
  135. which often means some sort of lock will be needed to prevent the URB
  136. from being deallocated while it is still in use.
  137. On the other hand, since usb_unlink_urb may end up calling the
  138. completion handler, the handler must not take any lock that is held
  139. when usb_unlink_urb is invoked. The general solution to this problem
  140. is to increment the URB's reference count while holding the lock, then
  141. drop the lock and call usb_unlink_urb or usb_kill_urb, and then
  142. decrement the URB's reference count. You increment the reference
  143. count by calling :c:func`usb_get_urb`::
  144. struct urb *usb_get_urb(struct urb *urb)
  145. (ignore the return value; it is the same as the argument) and
  146. decrement the reference count by calling :c:func:`usb_free_urb`. Of course,
  147. none of this is necessary if there's no danger of the URB being freed
  148. by the completion handler.
  149. What about the completion handler?
  150. ==================================
  151. The handler is of the following type::
  152. typedef void (*usb_complete_t)(struct urb *)
  153. I.e., it gets the URB that caused the completion call. In the completion
  154. handler, you should have a look at ``urb->status`` to detect any USB errors.
  155. Since the context parameter is included in the URB, you can pass
  156. information to the completion handler.
  157. Note that even when an error (or unlink) is reported, data may have been
  158. transferred. That's because USB transfers are packetized; it might take
  159. sixteen packets to transfer your 1KByte buffer, and ten of them might
  160. have transferred successfully before the completion was called.
  161. .. warning::
  162. NEVER SLEEP IN A COMPLETION HANDLER.
  163. These are often called in atomic context.
  164. In the current kernel, completion handlers run with local interrupts
  165. disabled, but in the future this will be changed, so don't assume that
  166. local IRQs are always disabled inside completion handlers.
  167. How to do isochronous (ISO) transfers?
  168. ======================================
  169. Besides the fields present on a bulk transfer, for ISO, you also
  170. have to set ``urb->interval`` to say how often to make transfers; it's
  171. often one per frame (which is once every microframe for highspeed devices).
  172. The actual interval used will be a power of two that's no bigger than what
  173. you specify. You can use the :c:func:`usb_fill_int_urb` macro to fill
  174. most ISO transfer fields.
  175. For ISO transfers you also have to fill a :c:type:`usb_iso_packet_descriptor`
  176. structure, allocated at the end of the URB by :c:func:`usb_alloc_urb`, for
  177. each packet you want to schedule.
  178. The :c:func:`usb_submit_urb` call modifies ``urb->interval`` to the implemented
  179. interval value that is less than or equal to the requested interval value. If
  180. ``URB_ISO_ASAP`` scheduling is used, ``urb->start_frame`` is also updated.
  181. For each entry you have to specify the data offset for this frame (base is
  182. transfer_buffer), and the length you want to write/expect to read.
  183. After completion, actual_length contains the actual transferred length and
  184. status contains the resulting status for the ISO transfer for this frame.
  185. It is allowed to specify a varying length from frame to frame (e.g. for
  186. audio synchronisation/adaptive transfer rates). You can also use the length
  187. 0 to omit one or more frames (striping).
  188. For scheduling you can choose your own start frame or ``URB_ISO_ASAP``. As
  189. explained earlier, if you always keep at least one URB queued and your
  190. completion keeps (re)submitting a later URB, you'll get smooth ISO streaming
  191. (if usb bandwidth utilization allows).
  192. If you specify your own start frame, make sure it's several frames in advance
  193. of the current frame. You might want this model if you're synchronizing
  194. ISO data with some other event stream.
  195. How to start interrupt (INT) transfers?
  196. =======================================
  197. Interrupt transfers, like isochronous transfers, are periodic, and happen
  198. in intervals that are powers of two (1, 2, 4 etc) units. Units are frames
  199. for full and low speed devices, and microframes for high speed ones.
  200. You can use the :c:func:`usb_fill_int_urb` macro to fill INT transfer fields.
  201. The :c:func:`usb_submit_urb` call modifies ``urb->interval`` to the implemented
  202. interval value that is less than or equal to the requested interval value.
  203. In Linux 2.6, unlike earlier versions, interrupt URBs are not automagically
  204. restarted when they complete. They end when the completion handler is
  205. called, just like other URBs. If you want an interrupt URB to be restarted,
  206. your completion handler must resubmit it.
  207. s