URB.txt 10 KB

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