0001 .. _usb-urb:
0002
0003 USB Request Block (URB)
0004 ~~~~~~~~~~~~~~~~~~~~~~~
0005
0006 :Revised: 2000-Dec-05
0007 :Again: 2002-Jul-06
0008 :Again: 2005-Sep-19
0009 :Again: 2017-Mar-29
0010
0011
0012 .. note::
0013
0014 The USB subsystem now has a substantial section at :ref:`usb-hostside-api`
0015 section, generated from the current source code.
0016 This particular documentation file isn't complete and may not be
0017 updated to the last version; don't rely on it except for a quick
0018 overview.
0019
0020 Basic concept or 'What is an URB?'
0021 ==================================
0022
0023 The basic idea of the new driver is message passing, the message itself is
0024 called USB Request Block, or URB for short.
0025
0026 - An URB consists of all relevant information to execute any USB transaction
0027 and deliver the data and status back.
0028
0029 - Execution of an URB is inherently an asynchronous operation, i.e. the
0030 :c:func:`usb_submit_urb` call returns immediately after it has successfully
0031 queued the requested action.
0032
0033 - Transfers for one URB can be canceled with :c:func:`usb_unlink_urb`
0034 at any time.
0035
0036 - Each URB has a completion handler, which is called after the action
0037 has been successfully completed or canceled. The URB also contains a
0038 context-pointer for passing information to the completion handler.
0039
0040 - Each endpoint for a device logically supports a queue of requests.
0041 You can fill that queue, so that the USB hardware can still transfer
0042 data to an endpoint while your driver handles completion of another.
0043 This maximizes use of USB bandwidth, and supports seamless streaming
0044 of data to (or from) devices when using periodic transfer modes.
0045
0046
0047 The URB structure
0048 =================
0049
0050 Some of the fields in struct urb are::
0051
0052 struct urb
0053 {
0054 // (IN) device and pipe specify the endpoint queue
0055 struct usb_device *dev; // pointer to associated USB device
0056 unsigned int pipe; // endpoint information
0057
0058 unsigned int transfer_flags; // URB_ISO_ASAP, URB_SHORT_NOT_OK, etc.
0059
0060 // (IN) all urbs need completion routines
0061 void *context; // context for completion routine
0062 usb_complete_t complete; // pointer to completion routine
0063
0064 // (OUT) status after each completion
0065 int status; // returned status
0066
0067 // (IN) buffer used for data transfers
0068 void *transfer_buffer; // associated data buffer
0069 u32 transfer_buffer_length; // data buffer length
0070 int number_of_packets; // size of iso_frame_desc
0071
0072 // (OUT) sometimes only part of CTRL/BULK/INTR transfer_buffer is used
0073 u32 actual_length; // actual data buffer length
0074
0075 // (IN) setup stage for CTRL (pass a struct usb_ctrlrequest)
0076 unsigned char *setup_packet; // setup packet (control only)
0077
0078 // Only for PERIODIC transfers (ISO, INTERRUPT)
0079 // (IN/OUT) start_frame is set unless URB_ISO_ASAP isn't set
0080 int start_frame; // start frame
0081 int interval; // polling interval
0082
0083 // ISO only: packets are only "best effort"; each can have errors
0084 int error_count; // number of errors
0085 struct usb_iso_packet_descriptor iso_frame_desc[0];
0086 };
0087
0088 Your driver must create the "pipe" value using values from the appropriate
0089 endpoint descriptor in an interface that it's claimed.
0090
0091
0092 How to get an URB?
0093 ==================
0094
0095 URBs are allocated by calling :c:func:`usb_alloc_urb`::
0096
0097 struct urb *usb_alloc_urb(int isoframes, int mem_flags)
0098
0099 Return value is a pointer to the allocated URB, 0 if allocation failed.
0100 The parameter isoframes specifies the number of isochronous transfer frames
0101 you want to schedule. For CTRL/BULK/INT, use 0. The mem_flags parameter
0102 holds standard memory allocation flags, letting you control (among other
0103 things) whether the underlying code may block or not.
0104
0105 To free an URB, use :c:func:`usb_free_urb`::
0106
0107 void usb_free_urb(struct urb *urb)
0108
0109 You may free an urb that you've submitted, but which hasn't yet been
0110 returned to you in a completion callback. It will automatically be
0111 deallocated when it is no longer in use.
0112
0113
0114 What has to be filled in?
0115 =========================
0116
0117 Depending on the type of transaction, there are some inline functions
0118 defined in ``linux/usb.h`` to simplify the initialization, such as
0119 :c:func:`usb_fill_control_urb`, :c:func:`usb_fill_bulk_urb` and
0120 :c:func:`usb_fill_int_urb`. In general, they need the usb device pointer,
0121 the pipe (usual format from usb.h), the transfer buffer, the desired transfer
0122 length, the completion handler, and its context. Take a look at the some
0123 existing drivers to see how they're used.
0124
0125 Flags:
0126
0127 - For ISO there are two startup behaviors: Specified start_frame or ASAP.
0128 - For ASAP set ``URB_ISO_ASAP`` in transfer_flags.
0129
0130 If short packets should NOT be tolerated, set ``URB_SHORT_NOT_OK`` in
0131 transfer_flags.
0132
0133
0134 How to submit an URB?
0135 =====================
0136
0137 Just call :c:func:`usb_submit_urb`::
0138
0139 int usb_submit_urb(struct urb *urb, int mem_flags)
0140
0141 The ``mem_flags`` parameter, such as ``GFP_ATOMIC``, controls memory
0142 allocation, such as whether the lower levels may block when memory is tight.
0143
0144 It immediately returns, either with status 0 (request queued) or some
0145 error code, usually caused by the following:
0146
0147 - Out of memory (``-ENOMEM``)
0148 - Unplugged device (``-ENODEV``)
0149 - Stalled endpoint (``-EPIPE``)
0150 - Too many queued ISO transfers (``-EAGAIN``)
0151 - Too many requested ISO frames (``-EFBIG``)
0152 - Invalid INT interval (``-EINVAL``)
0153 - More than one packet for INT (``-EINVAL``)
0154
0155 After submission, ``urb->status`` is ``-EINPROGRESS``; however, you should
0156 never look at that value except in your completion callback.
0157
0158 For isochronous endpoints, your completion handlers should (re)submit
0159 URBs to the same endpoint with the ``URB_ISO_ASAP`` flag, using
0160 multi-buffering, to get seamless ISO streaming.
0161
0162
0163 How to cancel an already running URB?
0164 =====================================
0165
0166 There are two ways to cancel an URB you've submitted but which hasn't
0167 been returned to your driver yet. For an asynchronous cancel, call
0168 :c:func:`usb_unlink_urb`::
0169
0170 int usb_unlink_urb(struct urb *urb)
0171
0172 It removes the urb from the internal list and frees all allocated
0173 HW descriptors. The status is changed to reflect unlinking. Note
0174 that the URB will not normally have finished when :c:func:`usb_unlink_urb`
0175 returns; you must still wait for the completion handler to be called.
0176
0177 To cancel an URB synchronously, call :c:func:`usb_kill_urb`::
0178
0179 void usb_kill_urb(struct urb *urb)
0180
0181 It does everything :c:func:`usb_unlink_urb` does, and in addition it waits
0182 until after the URB has been returned and the completion handler
0183 has finished. It also marks the URB as temporarily unusable, so
0184 that if the completion handler or anyone else tries to resubmit it
0185 they will get a ``-EPERM`` error. Thus you can be sure that when
0186 :c:func:`usb_kill_urb` returns, the URB is totally idle.
0187
0188 There is a lifetime issue to consider. An URB may complete at any
0189 time, and the completion handler may free the URB. If this happens
0190 while :c:func:`usb_unlink_urb` or :c:func:`usb_kill_urb` is running, it will
0191 cause a memory-access violation. The driver is responsible for avoiding this,
0192 which often means some sort of lock will be needed to prevent the URB
0193 from being deallocated while it is still in use.
0194
0195 On the other hand, since usb_unlink_urb may end up calling the
0196 completion handler, the handler must not take any lock that is held
0197 when usb_unlink_urb is invoked. The general solution to this problem
0198 is to increment the URB's reference count while holding the lock, then
0199 drop the lock and call usb_unlink_urb or usb_kill_urb, and then
0200 decrement the URB's reference count. You increment the reference
0201 count by calling :c:func`usb_get_urb`::
0202
0203 struct urb *usb_get_urb(struct urb *urb)
0204
0205 (ignore the return value; it is the same as the argument) and
0206 decrement the reference count by calling :c:func:`usb_free_urb`. Of course,
0207 none of this is necessary if there's no danger of the URB being freed
0208 by the completion handler.
0209
0210
0211 What about the completion handler?
0212 ==================================
0213
0214 The handler is of the following type::
0215
0216 typedef void (*usb_complete_t)(struct urb *)
0217
0218 I.e., it gets the URB that caused the completion call. In the completion
0219 handler, you should have a look at ``urb->status`` to detect any USB errors.
0220 Since the context parameter is included in the URB, you can pass
0221 information to the completion handler.
0222
0223 Note that even when an error (or unlink) is reported, data may have been
0224 transferred. That's because USB transfers are packetized; it might take
0225 sixteen packets to transfer your 1KByte buffer, and ten of them might
0226 have transferred successfully before the completion was called.
0227
0228
0229 .. warning::
0230
0231 NEVER SLEEP IN A COMPLETION HANDLER.
0232
0233 These are often called in atomic context.
0234
0235 In the current kernel, completion handlers run with local interrupts
0236 disabled, but in the future this will be changed, so don't assume that
0237 local IRQs are always disabled inside completion handlers.
0238
0239 How to do isochronous (ISO) transfers?
0240 ======================================
0241
0242 Besides the fields present on a bulk transfer, for ISO, you also
0243 have to set ``urb->interval`` to say how often to make transfers; it's
0244 often one per frame (which is once every microframe for highspeed devices).
0245 The actual interval used will be a power of two that's no bigger than what
0246 you specify. You can use the :c:func:`usb_fill_int_urb` macro to fill
0247 most ISO transfer fields.
0248
0249 For ISO transfers you also have to fill a :c:type:`usb_iso_packet_descriptor`
0250 structure, allocated at the end of the URB by :c:func:`usb_alloc_urb`, for
0251 each packet you want to schedule.
0252
0253 The :c:func:`usb_submit_urb` call modifies ``urb->interval`` to the implemented
0254 interval value that is less than or equal to the requested interval value. If
0255 ``URB_ISO_ASAP`` scheduling is used, ``urb->start_frame`` is also updated.
0256
0257 For each entry you have to specify the data offset for this frame (base is
0258 transfer_buffer), and the length you want to write/expect to read.
0259 After completion, actual_length contains the actual transferred length and
0260 status contains the resulting status for the ISO transfer for this frame.
0261 It is allowed to specify a varying length from frame to frame (e.g. for
0262 audio synchronisation/adaptive transfer rates). You can also use the length
0263 0 to omit one or more frames (striping).
0264
0265 For scheduling you can choose your own start frame or ``URB_ISO_ASAP``. As
0266 explained earlier, if you always keep at least one URB queued and your
0267 completion keeps (re)submitting a later URB, you'll get smooth ISO streaming
0268 (if usb bandwidth utilization allows).
0269
0270 If you specify your own start frame, make sure it's several frames in advance
0271 of the current frame. You might want this model if you're synchronizing
0272 ISO data with some other event stream.
0273
0274
0275 How to start interrupt (INT) transfers?
0276 =======================================
0277
0278 Interrupt transfers, like isochronous transfers, are periodic, and happen
0279 in intervals that are powers of two (1, 2, 4 etc) units. Units are frames
0280 for full and low speed devices, and microframes for high speed ones.
0281 You can use the :c:func:`usb_fill_int_urb` macro to fill INT transfer fields.
0282
0283 The :c:func:`usb_submit_urb` call modifies ``urb->interval`` to the implemented
0284 interval value that is less than or equal to the requested interval value.
0285
0286 In Linux 2.6, unlike earlier versions, interrupt URBs are not automagically
0287 restarted when they complete. They end when the completion handler is
0288 called, just like other URBs. If you want an interrupt URB to be restarted,
0289 your completion handler must resubmit it.
0290 s