162306a36Sopenharmony_ci.. _usb-urb:
262306a36Sopenharmony_ci
362306a36Sopenharmony_ciUSB Request Block (URB)
462306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci:Revised: 2000-Dec-05
762306a36Sopenharmony_ci:Again:   2002-Jul-06
862306a36Sopenharmony_ci:Again:   2005-Sep-19
962306a36Sopenharmony_ci:Again:   2017-Mar-29
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci.. note::
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci    The USB subsystem now has a substantial section at :ref:`usb-hostside-api`
1562306a36Sopenharmony_ci    section, generated from the current source code.
1662306a36Sopenharmony_ci    This particular documentation file isn't complete and may not be
1762306a36Sopenharmony_ci    updated to the last version; don't rely on it except for a quick
1862306a36Sopenharmony_ci    overview.
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ciBasic concept or 'What is an URB?'
2162306a36Sopenharmony_ci==================================
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ciThe basic idea of the new driver is message passing, the message itself is
2462306a36Sopenharmony_cicalled USB Request Block, or URB for short.
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci- An URB consists of all relevant information to execute any USB transaction
2762306a36Sopenharmony_ci  and deliver the data and status back.
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci- Execution of an URB is inherently an asynchronous operation, i.e. the
3062306a36Sopenharmony_ci  :c:func:`usb_submit_urb` call returns immediately after it has successfully
3162306a36Sopenharmony_ci  queued the requested action.
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci- Transfers for one URB can be canceled with :c:func:`usb_unlink_urb`
3462306a36Sopenharmony_ci  at any time.
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci- Each URB has a completion handler, which is called after the action
3762306a36Sopenharmony_ci  has been successfully completed or canceled. The URB also contains a
3862306a36Sopenharmony_ci  context-pointer for passing information to the completion handler.
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci- Each endpoint for a device logically supports a queue of requests.
4162306a36Sopenharmony_ci  You can fill that queue, so that the USB hardware can still transfer
4262306a36Sopenharmony_ci  data to an endpoint while your driver handles completion of another.
4362306a36Sopenharmony_ci  This maximizes use of USB bandwidth, and supports seamless streaming
4462306a36Sopenharmony_ci  of data to (or from) devices when using periodic transfer modes.
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciThe URB structure
4862306a36Sopenharmony_ci=================
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ciSome of the fields in struct urb are::
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci  struct urb
5362306a36Sopenharmony_ci  {
5462306a36Sopenharmony_ci  // (IN) device and pipe specify the endpoint queue
5562306a36Sopenharmony_ci	struct usb_device *dev;         // pointer to associated USB device
5662306a36Sopenharmony_ci	unsigned int pipe;              // endpoint information
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	unsigned int transfer_flags;    // URB_ISO_ASAP, URB_SHORT_NOT_OK, etc.
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci  // (IN) all urbs need completion routines
6162306a36Sopenharmony_ci	void *context;                  // context for completion routine
6262306a36Sopenharmony_ci	usb_complete_t complete;        // pointer to completion routine
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci  // (OUT) status after each completion
6562306a36Sopenharmony_ci	int status;                     // returned status
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci  // (IN) buffer used for data transfers
6862306a36Sopenharmony_ci	void *transfer_buffer;          // associated data buffer
6962306a36Sopenharmony_ci	u32 transfer_buffer_length;     // data buffer length
7062306a36Sopenharmony_ci	int number_of_packets;          // size of iso_frame_desc
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci  // (OUT) sometimes only part of CTRL/BULK/INTR transfer_buffer is used
7362306a36Sopenharmony_ci	u32 actual_length;              // actual data buffer length
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci  // (IN) setup stage for CTRL (pass a struct usb_ctrlrequest)
7662306a36Sopenharmony_ci	unsigned char *setup_packet;    // setup packet (control only)
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci  // Only for PERIODIC transfers (ISO, INTERRUPT)
7962306a36Sopenharmony_ci    // (IN/OUT) start_frame is set unless URB_ISO_ASAP isn't set
8062306a36Sopenharmony_ci	int start_frame;                // start frame
8162306a36Sopenharmony_ci	int interval;                   // polling interval
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci    // ISO only: packets are only "best effort"; each can have errors
8462306a36Sopenharmony_ci	int error_count;                // number of errors
8562306a36Sopenharmony_ci	struct usb_iso_packet_descriptor iso_frame_desc[0];
8662306a36Sopenharmony_ci  };
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciYour driver must create the "pipe" value using values from the appropriate
8962306a36Sopenharmony_ciendpoint descriptor in an interface that it's claimed.
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ciHow to get an URB?
9362306a36Sopenharmony_ci==================
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ciURBs are allocated by calling :c:func:`usb_alloc_urb`::
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	struct urb *usb_alloc_urb(int isoframes, int mem_flags)
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ciReturn value is a pointer to the allocated URB, 0 if allocation failed.
10062306a36Sopenharmony_ciThe parameter isoframes specifies the number of isochronous transfer frames
10162306a36Sopenharmony_ciyou want to schedule. For CTRL/BULK/INT, use 0.  The mem_flags parameter
10262306a36Sopenharmony_ciholds standard memory allocation flags, letting you control (among other
10362306a36Sopenharmony_cithings) whether the underlying code may block or not.
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ciTo free an URB, use :c:func:`usb_free_urb`::
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	void usb_free_urb(struct urb *urb)
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ciYou may free an urb that you've submitted, but which hasn't yet been
11062306a36Sopenharmony_cireturned to you in a completion callback.  It will automatically be
11162306a36Sopenharmony_cideallocated when it is no longer in use.
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ciWhat has to be filled in?
11562306a36Sopenharmony_ci=========================
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ciDepending on the type of transaction, there are some inline functions
11862306a36Sopenharmony_cidefined in ``linux/usb.h`` to simplify the initialization, such as
11962306a36Sopenharmony_ci:c:func:`usb_fill_control_urb`, :c:func:`usb_fill_bulk_urb` and
12062306a36Sopenharmony_ci:c:func:`usb_fill_int_urb`.  In general, they need the usb device pointer,
12162306a36Sopenharmony_cithe pipe (usual format from usb.h), the transfer buffer, the desired transfer
12262306a36Sopenharmony_cilength, the completion handler, and its context. Take a look at the some
12362306a36Sopenharmony_ciexisting drivers to see how they're used.
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ciFlags:
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci- For ISO there are two startup behaviors: Specified start_frame or ASAP.
12862306a36Sopenharmony_ci- For ASAP set ``URB_ISO_ASAP`` in transfer_flags.
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ciIf short packets should NOT be tolerated, set ``URB_SHORT_NOT_OK`` in
13162306a36Sopenharmony_citransfer_flags.
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ciHow to submit an URB?
13562306a36Sopenharmony_ci=====================
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ciJust call :c:func:`usb_submit_urb`::
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	int usb_submit_urb(struct urb *urb, int mem_flags)
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ciThe ``mem_flags`` parameter, such as ``GFP_ATOMIC``, controls memory
14262306a36Sopenharmony_ciallocation, such as whether the lower levels may block when memory is tight.
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ciIt immediately returns, either with status 0 (request queued) or some
14562306a36Sopenharmony_cierror code, usually caused by the following:
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci- Out of memory (``-ENOMEM``)
14862306a36Sopenharmony_ci- Unplugged device (``-ENODEV``)
14962306a36Sopenharmony_ci- Stalled endpoint (``-EPIPE``)
15062306a36Sopenharmony_ci- Too many queued ISO transfers (``-EAGAIN``)
15162306a36Sopenharmony_ci- Too many requested ISO frames (``-EFBIG``)
15262306a36Sopenharmony_ci- Invalid INT interval (``-EINVAL``)
15362306a36Sopenharmony_ci- More than one packet for INT (``-EINVAL``)
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ciAfter submission, ``urb->status`` is ``-EINPROGRESS``; however, you should
15662306a36Sopenharmony_cinever look at that value except in your completion callback.
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ciFor isochronous endpoints, your completion handlers should (re)submit
15962306a36Sopenharmony_ciURBs to the same endpoint with the ``URB_ISO_ASAP`` flag, using
16062306a36Sopenharmony_cimulti-buffering, to get seamless ISO streaming.
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ciHow to cancel an already running URB?
16462306a36Sopenharmony_ci=====================================
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ciThere are two ways to cancel an URB you've submitted but which hasn't
16762306a36Sopenharmony_cibeen returned to your driver yet.  For an asynchronous cancel, call
16862306a36Sopenharmony_ci:c:func:`usb_unlink_urb`::
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	int usb_unlink_urb(struct urb *urb)
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ciIt removes the urb from the internal list and frees all allocated
17362306a36Sopenharmony_ciHW descriptors. The status is changed to reflect unlinking.  Note
17462306a36Sopenharmony_cithat the URB will not normally have finished when :c:func:`usb_unlink_urb`
17562306a36Sopenharmony_cireturns; you must still wait for the completion handler to be called.
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ciTo cancel an URB synchronously, call :c:func:`usb_kill_urb`::
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	void usb_kill_urb(struct urb *urb)
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ciIt does everything :c:func:`usb_unlink_urb` does, and in addition it waits
18262306a36Sopenharmony_ciuntil after the URB has been returned and the completion handler
18362306a36Sopenharmony_cihas finished.  It also marks the URB as temporarily unusable, so
18462306a36Sopenharmony_cithat if the completion handler or anyone else tries to resubmit it
18562306a36Sopenharmony_cithey will get a ``-EPERM`` error.  Thus you can be sure that when
18662306a36Sopenharmony_ci:c:func:`usb_kill_urb` returns, the URB is totally idle.
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ciThere is a lifetime issue to consider.  An URB may complete at any
18962306a36Sopenharmony_citime, and the completion handler may free the URB.  If this happens
19062306a36Sopenharmony_ciwhile :c:func:`usb_unlink_urb` or :c:func:`usb_kill_urb` is running, it will
19162306a36Sopenharmony_cicause a memory-access violation.  The driver is responsible for avoiding this,
19262306a36Sopenharmony_ciwhich often means some sort of lock will be needed to prevent the URB
19362306a36Sopenharmony_cifrom being deallocated while it is still in use.
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ciOn the other hand, since usb_unlink_urb may end up calling the
19662306a36Sopenharmony_cicompletion handler, the handler must not take any lock that is held
19762306a36Sopenharmony_ciwhen usb_unlink_urb is invoked.  The general solution to this problem
19862306a36Sopenharmony_ciis to increment the URB's reference count while holding the lock, then
19962306a36Sopenharmony_cidrop the lock and call usb_unlink_urb or usb_kill_urb, and then
20062306a36Sopenharmony_cidecrement the URB's reference count.  You increment the reference
20162306a36Sopenharmony_cicount by calling :c:func`usb_get_urb`::
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	struct urb *usb_get_urb(struct urb *urb)
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci(ignore the return value; it is the same as the argument) and
20662306a36Sopenharmony_cidecrement the reference count by calling :c:func:`usb_free_urb`.  Of course,
20762306a36Sopenharmony_cinone of this is necessary if there's no danger of the URB being freed
20862306a36Sopenharmony_ciby the completion handler.
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ciWhat about the completion handler?
21262306a36Sopenharmony_ci==================================
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ciThe handler is of the following type::
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	typedef void (*usb_complete_t)(struct urb *)
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ciI.e., it gets the URB that caused the completion call. In the completion
21962306a36Sopenharmony_cihandler, you should have a look at ``urb->status`` to detect any USB errors.
22062306a36Sopenharmony_ciSince the context parameter is included in the URB, you can pass
22162306a36Sopenharmony_ciinformation to the completion handler.
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ciNote that even when an error (or unlink) is reported, data may have been
22462306a36Sopenharmony_citransferred.  That's because USB transfers are packetized; it might take
22562306a36Sopenharmony_cisixteen packets to transfer your 1KByte buffer, and ten of them might
22662306a36Sopenharmony_cihave transferred successfully before the completion was called.
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci.. warning::
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci   NEVER SLEEP IN A COMPLETION HANDLER.
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci   These are often called in atomic context.
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ciIn the current kernel, completion handlers run with local interrupts
23662306a36Sopenharmony_cidisabled, but in the future this will be changed, so don't assume that
23762306a36Sopenharmony_cilocal IRQs are always disabled inside completion handlers.
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ciHow to do isochronous (ISO) transfers?
24062306a36Sopenharmony_ci======================================
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ciBesides the fields present on a bulk transfer, for ISO, you also
24362306a36Sopenharmony_cihave to set ``urb->interval`` to say how often to make transfers; it's
24462306a36Sopenharmony_cioften one per frame (which is once every microframe for highspeed devices).
24562306a36Sopenharmony_ciThe actual interval used will be a power of two that's no bigger than what
24662306a36Sopenharmony_ciyou specify. You can use the :c:func:`usb_fill_int_urb` macro to fill
24762306a36Sopenharmony_cimost ISO transfer fields.
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ciFor ISO transfers you also have to fill a :c:type:`usb_iso_packet_descriptor`
25062306a36Sopenharmony_cistructure, allocated at the end of the URB by :c:func:`usb_alloc_urb`, for
25162306a36Sopenharmony_cieach packet you want to schedule.
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ciThe :c:func:`usb_submit_urb` call modifies ``urb->interval`` to the implemented
25462306a36Sopenharmony_ciinterval value that is less than or equal to the requested interval value.  If
25562306a36Sopenharmony_ci``URB_ISO_ASAP`` scheduling is used, ``urb->start_frame`` is also updated.
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ciFor each entry you have to specify the data offset for this frame (base is
25862306a36Sopenharmony_citransfer_buffer), and the length you want to write/expect to read.
25962306a36Sopenharmony_ciAfter completion, actual_length contains the actual transferred length and
26062306a36Sopenharmony_cistatus contains the resulting status for the ISO transfer for this frame.
26162306a36Sopenharmony_ciIt is allowed to specify a varying length from frame to frame (e.g. for
26262306a36Sopenharmony_ciaudio synchronisation/adaptive transfer rates). You can also use the length
26362306a36Sopenharmony_ci0 to omit one or more frames (striping).
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ciFor scheduling you can choose your own start frame or ``URB_ISO_ASAP``. As
26662306a36Sopenharmony_ciexplained earlier, if you always keep at least one URB queued and your
26762306a36Sopenharmony_cicompletion keeps (re)submitting a later URB, you'll get smooth ISO streaming
26862306a36Sopenharmony_ci(if usb bandwidth utilization allows).
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ciIf you specify your own start frame, make sure it's several frames in advance
27162306a36Sopenharmony_ciof the current frame.  You might want this model if you're synchronizing
27262306a36Sopenharmony_ciISO data with some other event stream.
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ciHow to start interrupt (INT) transfers?
27662306a36Sopenharmony_ci=======================================
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ciInterrupt transfers, like isochronous transfers, are periodic, and happen
27962306a36Sopenharmony_ciin intervals that are powers of two (1, 2, 4 etc) units.  Units are frames
28062306a36Sopenharmony_cifor full and low speed devices, and microframes for high speed ones.
28162306a36Sopenharmony_ciYou can use the :c:func:`usb_fill_int_urb` macro to fill INT transfer fields.
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ciThe :c:func:`usb_submit_urb` call modifies ``urb->interval`` to the implemented
28462306a36Sopenharmony_ciinterval value that is less than or equal to the requested interval value.
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ciIn Linux 2.6, unlike earlier versions, interrupt URBs are not automagically
28762306a36Sopenharmony_cirestarted when they complete.  They end when the completion handler is
28862306a36Sopenharmony_cicalled, just like other URBs.  If you want an interrupt URB to be restarted,
28962306a36Sopenharmony_ciyour completion handler must resubmit it.
29062306a36Sopenharmony_cis
291