aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab@s-opensource.com>2017-04-05 09:23:10 -0400
committerJonathan Corbet <corbet@lwn.net>2017-04-11 16:40:25 -0400
commite463c06335d04043c079f1d1d66472ec049de5dd (patch)
tree3190169c346b97ce5c88171db0b8af164dd112a9
parent32a3bebce9d09598d4f4c5afca929a2ce148b8c4 (diff)
usb/URB.txt: convert to ReST and update it
The URB doc describes the Kernel mechanism that do USB transfers. While the functions are already described at urb.h, there are a number of concepts and theory that are important for USB driver developers. Convert it to ReST and use C ref links to point to the places at usb.h where each function and struct is located. A few of those descriptions were incomplete. While here, update to reflect the current API status. Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Jonathan Corbet <corbet@lwn.net>
-rw-r--r--Documentation/driver-api/usb/URB.rst (renamed from Documentation/usb/URB.txt)221
-rw-r--r--Documentation/driver-api/usb/index.rst1
-rw-r--r--Documentation/driver-api/usb/usb.rst2
3 files changed, 127 insertions, 97 deletions
diff --git a/Documentation/usb/URB.txt b/Documentation/driver-api/usb/URB.rst
index 50da0d455444..c4a141f29477 100644
--- a/Documentation/usb/URB.txt
+++ b/Documentation/driver-api/usb/URB.rst
@@ -1,28 +1,35 @@
1Revised: 2000-Dec-05. 1USB Request Block (URB)
2Again: 2002-Jul-06 2~~~~~~~~~~~~~~~~~~~~~~~
3Again: 2005-Sep-19
4 3
5 NOTE: 4:Revised: 2000-Dec-05
5:Again: 2002-Jul-06
6:Again: 2005-Sep-19
7:Again: 2017-Mar-29
6 8
7 The USB subsystem now has a substantial section in "The Linux Kernel API"
8 guide (in Documentation/DocBook), generated from the current source
9 code. This particular documentation file isn't particularly current or
10 complete; don't rely on it except for a quick overview.
11 9
10.. note::
12 11
131.1. Basic concept or 'What is an URB?' 12 The USB subsystem now has a substantial section at :ref:`usb-hostside-api`
13 section, generated from the current source code.
14 This particular documentation file isn't complete and may not be
15 updated to the last version; don't rely on it except for a quick
16 overview.
14 17
15The basic idea of the new driver is message passing, the message itself is 18Basic concept or 'What is an URB?'
16called USB Request Block, or URB for short. 19==================================
17 20
18- An URB consists of all relevant information to execute any USB transaction 21The basic idea of the new driver is message passing, the message itself is
19 and deliver the data and status back. 22called USB Request Block, or URB for short.
20 23
21- Execution of an URB is inherently an asynchronous operation, i.e. the 24- An URB consists of all relevant information to execute any USB transaction
22 usb_submit_urb(urb) call returns immediately after it has successfully 25 and deliver the data and status back.
26
27- Execution of an URB is inherently an asynchronous operation, i.e. the
28 :c:func:`usb_submit_urb` call returns immediately after it has successfully
23 queued the requested action. 29 queued the requested action.
24 30
25- Transfers for one URB can be canceled with usb_unlink_urb(urb) at any time. 31- Transfers for one URB can be canceled with :c:func:`usb_unlink_urb`
32 at any time.
26 33
27- Each URB has a completion handler, which is called after the action 34- Each URB has a completion handler, which is called after the action
28 has been successfully completed or canceled. The URB also contains a 35 has been successfully completed or canceled. The URB also contains a
@@ -35,53 +42,55 @@ called USB Request Block, or URB for short.
35 of data to (or from) devices when using periodic transfer modes. 42 of data to (or from) devices when using periodic transfer modes.
36 43
37 44
381.2. The URB structure 45The URB structure
46=================
39 47
40Some of the fields in an URB are: 48Some of the fields in struct :c:type:`urb` are::
41 49
42struct urb 50 struct urb
43{ 51 {
44// (IN) device and pipe specify the endpoint queue 52 // (IN) device and pipe specify the endpoint queue
45 struct usb_device *dev; // pointer to associated USB device 53 struct usb_device *dev; // pointer to associated USB device
46 unsigned int pipe; // endpoint information 54 unsigned int pipe; // endpoint information
47 55
48 unsigned int transfer_flags; // ISO_ASAP, SHORT_NOT_OK, etc. 56 unsigned int transfer_flags; // URB_ISO_ASAP, URB_SHORT_NOT_OK, etc.
49 57
50// (IN) all urbs need completion routines 58 // (IN) all urbs need completion routines
51 void *context; // context for completion routine 59 void *context; // context for completion routine
52 void (*complete)(struct urb *); // pointer to completion routine 60 usb_complete_t complete; // pointer to completion routine
53 61
54// (OUT) status after each completion 62 // (OUT) status after each completion
55 int status; // returned status 63 int status; // returned status
56 64
57// (IN) buffer used for data transfers 65 // (IN) buffer used for data transfers
58 void *transfer_buffer; // associated data buffer 66 void *transfer_buffer; // associated data buffer
59 int transfer_buffer_length; // data buffer length 67 u32 transfer_buffer_length; // data buffer length
60 int number_of_packets; // size of iso_frame_desc 68 int number_of_packets; // size of iso_frame_desc
61 69
62// (OUT) sometimes only part of CTRL/BULK/INTR transfer_buffer is used 70 // (OUT) sometimes only part of CTRL/BULK/INTR transfer_buffer is used
63 int actual_length; // actual data buffer length 71 u32 actual_length; // actual data buffer length
64 72
65// (IN) setup stage for CTRL (pass a struct usb_ctrlrequest) 73 // (IN) setup stage for CTRL (pass a struct usb_ctrlrequest)
66 unsigned char* setup_packet; // setup packet (control only) 74 unsigned char *setup_packet; // setup packet (control only)
67 75
68// Only for PERIODIC transfers (ISO, INTERRUPT) 76 // Only for PERIODIC transfers (ISO, INTERRUPT)
69 // (IN/OUT) start_frame is set unless ISO_ASAP isn't set 77 // (IN/OUT) start_frame is set unless URB_ISO_ASAP isn't set
70 int start_frame; // start frame 78 int start_frame; // start frame
71 int interval; // polling interval 79 int interval; // polling interval
72 80
73 // ISO only: packets are only "best effort"; each can have errors 81 // ISO only: packets are only "best effort"; each can have errors
74 int error_count; // number of errors 82 int error_count; // number of errors
75 struct usb_iso_packet_descriptor iso_frame_desc[0]; 83 struct usb_iso_packet_descriptor iso_frame_desc[0];
76}; 84 };
77 85
78Your driver must create the "pipe" value using values from the appropriate 86Your driver must create the "pipe" value using values from the appropriate
79endpoint descriptor in an interface that it's claimed. 87endpoint descriptor in an interface that it's claimed.
80 88
81 89
821.3. How to get an URB? 90How to get an URB?
91==================
83 92
84URBs are allocated with the following call 93URBs are allocated by calling :c:func:`usb_alloc_urb`::
85 94
86 struct urb *usb_alloc_urb(int isoframes, int mem_flags) 95 struct urb *usb_alloc_urb(int isoframes, int mem_flags)
87 96
@@ -91,7 +100,7 @@ you want to schedule. For CTRL/BULK/INT, use 0. The mem_flags parameter
91holds standard memory allocation flags, letting you control (among other 100holds standard memory allocation flags, letting you control (among other
92things) whether the underlying code may block or not. 101things) whether the underlying code may block or not.
93 102
94To free an URB, use 103To free an URB, use :c:func:`usb_free_urb`::
95 104
96 void usb_free_urb(struct urb *urb) 105 void usb_free_urb(struct urb *urb)
97 106
@@ -100,78 +109,84 @@ returned to you in a completion callback. It will automatically be
100deallocated when it is no longer in use. 109deallocated when it is no longer in use.
101 110
102 111
1031.4. What has to be filled in? 112What has to be filled in?
113=========================
104 114
105Depending on the type of transaction, there are some inline functions 115Depending on the type of transaction, there are some inline functions
106defined in <linux/usb.h> to simplify the initialization, such as 116defined in ``linux/usb.h`` to simplify the initialization, such as
107fill_control_urb() and fill_bulk_urb(). In general, they need the usb 117:c:func:`usb_fill_control_urb`, :c:func:`usb_fill_bulk_urb` and
108device pointer, the pipe (usual format from usb.h), the transfer buffer, 118:c:func:`usb_fill_int_urb`. In general, they need the usb device pointer,
109the desired transfer length, the completion handler, and its context. 119the pipe (usual format from usb.h), the transfer buffer, the desired transfer
110Take a look at the some existing drivers to see how they're used. 120length, the completion handler, and its context. Take a look at the some
121existing drivers to see how they're used.
111 122
112Flags: 123Flags:
113For ISO there are two startup behaviors: Specified start_frame or ASAP.
114For ASAP set URB_ISO_ASAP in transfer_flags.
115 124
116If short packets should NOT be tolerated, set URB_SHORT_NOT_OK in 125- For ISO there are two startup behaviors: Specified start_frame or ASAP.
126- For ASAP set ``URB_ISO_ASAP`` in transfer_flags.
127
128If short packets should NOT be tolerated, set ``URB_SHORT_NOT_OK`` in
117transfer_flags. 129transfer_flags.
118 130
119 131
1201.5. How to submit an URB? 132How to submit an URB?
133=====================
121 134
122Just call 135Just call :c:func:`usb_submit_urb`::
123 136
124 int usb_submit_urb(struct urb *urb, int mem_flags) 137 int usb_submit_urb(struct urb *urb, int mem_flags)
125 138
126The mem_flags parameter, such as SLAB_ATOMIC, controls memory allocation, 139The ``mem_flags`` parameter, such as ``GFP_ATOMIC``, controls memory
127such as whether the lower levels may block when memory is tight. 140allocation, such as whether the lower levels may block when memory is tight.
128 141
129It immediately returns, either with status 0 (request queued) or some 142It immediately returns, either with status 0 (request queued) or some
130error code, usually caused by the following: 143error code, usually caused by the following:
131 144
132- Out of memory (-ENOMEM) 145- Out of memory (``-ENOMEM``)
133- Unplugged device (-ENODEV) 146- Unplugged device (``-ENODEV``)
134- Stalled endpoint (-EPIPE) 147- Stalled endpoint (``-EPIPE``)
135- Too many queued ISO transfers (-EAGAIN) 148- Too many queued ISO transfers (``-EAGAIN``)
136- Too many requested ISO frames (-EFBIG) 149- Too many requested ISO frames (``-EFBIG``)
137- Invalid INT interval (-EINVAL) 150- Invalid INT interval (``-EINVAL``)
138- More than one packet for INT (-EINVAL) 151- More than one packet for INT (``-EINVAL``)
139 152
140After submission, urb->status is -EINPROGRESS; however, you should never 153After submission, ``urb->status`` is ``-EINPROGRESS``; however, you should
141look at that value except in your completion callback. 154never look at that value except in your completion callback.
142 155
143For isochronous endpoints, your completion handlers should (re)submit 156For isochronous endpoints, your completion handlers should (re)submit
144URBs to the same endpoint with the ISO_ASAP flag, using multi-buffering, 157URBs to the same endpoint with the ``URB_ISO_ASAP`` flag, using
145to get seamless ISO streaming. 158multi-buffering, to get seamless ISO streaming.
146 159
147 160
1481.6. How to cancel an already running URB? 161How to cancel an already running URB?
162=====================================
149 163
150There are two ways to cancel an URB you've submitted but which hasn't 164There are two ways to cancel an URB you've submitted but which hasn't
151been returned to your driver yet. For an asynchronous cancel, call 165been returned to your driver yet. For an asynchronous cancel, call
166:c:func:`usb_unlink_urb`::
152 167
153 int usb_unlink_urb(struct urb *urb) 168 int usb_unlink_urb(struct urb *urb)
154 169
155It removes the urb from the internal list and frees all allocated 170It removes the urb from the internal list and frees all allocated
156HW descriptors. The status is changed to reflect unlinking. Note 171HW descriptors. The status is changed to reflect unlinking. Note
157that the URB will not normally have finished when usb_unlink_urb() 172that the URB will not normally have finished when :c:func:`usb_unlink_urb`
158returns; you must still wait for the completion handler to be called. 173returns; you must still wait for the completion handler to be called.
159 174
160To cancel an URB synchronously, call 175To cancel an URB synchronously, call :c:func:`usb_kill_urb`::
161 176
162 void usb_kill_urb(struct urb *urb) 177 void usb_kill_urb(struct urb *urb)
163 178
164It does everything usb_unlink_urb does, and in addition it waits 179It does everything :c:func:`usb_unlink_urb` does, and in addition it waits
165until after the URB has been returned and the completion handler 180until after the URB has been returned and the completion handler
166has finished. It also marks the URB as temporarily unusable, so 181has finished. It also marks the URB as temporarily unusable, so
167that if the completion handler or anyone else tries to resubmit it 182that if the completion handler or anyone else tries to resubmit it
168they will get a -EPERM error. Thus you can be sure that when 183they will get a ``-EPERM`` error. Thus you can be sure that when
169usb_kill_urb() returns, the URB is totally idle. 184:c:func:`usb_kill_urb` returns, the URB is totally idle.
170 185
171There is a lifetime issue to consider. An URB may complete at any 186There is a lifetime issue to consider. An URB may complete at any
172time, and the completion handler may free the URB. If this happens 187time, and the completion handler may free the URB. If this happens
173while usb_unlink_urb or usb_kill_urb is running, it will cause a 188while :c:func:`usb_unlink_urb` or :c:func:`usb_kill_urb` is running, it will
174memory-access violation. The driver is responsible for avoiding this, 189cause a memory-access violation. The driver is responsible for avoiding this,
175which often means some sort of lock will be needed to prevent the URB 190which often means some sort of lock will be needed to prevent the URB
176from being deallocated while it is still in use. 191from being deallocated while it is still in use.
177 192
@@ -181,24 +196,25 @@ when usb_unlink_urb is invoked. The general solution to this problem
181is to increment the URB's reference count while holding the lock, then 196is to increment the URB's reference count while holding the lock, then
182drop the lock and call usb_unlink_urb or usb_kill_urb, and then 197drop the lock and call usb_unlink_urb or usb_kill_urb, and then
183decrement the URB's reference count. You increment the reference 198decrement the URB's reference count. You increment the reference
184count by calling 199count by calling :c:func`usb_get_urb`::
185 200
186 struct urb *usb_get_urb(struct urb *urb) 201 struct urb *usb_get_urb(struct urb *urb)
187 202
188(ignore the return value; it is the same as the argument) and 203(ignore the return value; it is the same as the argument) and
189decrement the reference count by calling usb_free_urb. Of course, 204decrement the reference count by calling :c:func:`usb_free_urb`. Of course,
190none of this is necessary if there's no danger of the URB being freed 205none of this is necessary if there's no danger of the URB being freed
191by the completion handler. 206by the completion handler.
192 207
193 208
1941.7. What about the completion handler? 209What about the completion handler?
210==================================
195 211
196The handler is of the following type: 212The handler is of the following type::
197 213
198 typedef void (*usb_complete_t)(struct urb *) 214 typedef void (*usb_complete_t)(struct urb *)
199 215
200I.e., it gets the URB that caused the completion call. In the completion 216I.e., it gets the URB that caused the completion call. In the completion
201handler, you should have a look at urb->status to detect any USB errors. 217handler, you should have a look at ``urb->status`` to detect any USB errors.
202Since the context parameter is included in the URB, you can pass 218Since the context parameter is included in the URB, you can pass
203information to the completion handler. 219information to the completion handler.
204 220
@@ -208,54 +224,65 @@ sixteen packets to transfer your 1KByte buffer, and ten of them might
208have transferred successfully before the completion was called. 224have transferred successfully before the completion was called.
209 225
210 226
211NOTE: ***** WARNING ***** 227.. warning::
212NEVER SLEEP IN A COMPLETION HANDLER. These are often called in atomic 228
213context. 229 NEVER SLEEP IN A COMPLETION HANDLER.
230
231 These are often called in atomic context.
214 232
215In the current kernel, completion handlers run with local interrupts 233In the current kernel, completion handlers run with local interrupts
216disabled, but in the future this will be changed, so don't assume that 234disabled, but in the future this will be changed, so don't assume that
217local IRQs are always disabled inside completion handlers. 235local IRQs are always disabled inside completion handlers.
218 236
2191.8. How to do isochronous (ISO) transfers? 237How to do isochronous (ISO) transfers?
238======================================
239
240Besides the fields present on a bulk transfer, for ISO, you also
241also have to set ``urb->interval`` to say how often to make transfers; it's
242often one per frame (which is once every microframe for highspeed devices).
243The actual interval used will be a power of two that's no bigger than what
244you specify. You can use the :c:func:`usb_fill_int_urb` macro to fill
245most ISO transfer fields.
220 246
221For ISO transfers you have to fill a usb_iso_packet_descriptor structure, 247For ISO transfers you also have to fill a :c:type:`usb_iso_packet_descriptor`
222allocated at the end of the URB by usb_alloc_urb(n,mem_flags), for each 248structure, allocated at the end of the URB by :c:func:`usb_alloc_urb`, for
223packet you want to schedule. You also have to set urb->interval to say 249each packet you want to schedule.
224how often to make transfers; it's often one per frame (which is once
225every microframe for highspeed devices). The actual interval used will
226be a power of two that's no bigger than what you specify.
227 250
228The usb_submit_urb() call modifies urb->interval to the implemented interval 251The :c:func:`usb_submit_urb` call modifies ``urb->interval`` to the implemented
229value that is less than or equal to the requested interval value. If 252interval value that is less than or equal to the requested interval value. If
230ISO_ASAP scheduling is used, urb->start_frame is also updated. 253``URB_ISO_ASAP`` scheduling is used, ``urb->start_frame`` is also updated.
231 254
232For each entry you have to specify the data offset for this frame (base is 255For each entry you have to specify the data offset for this frame (base is
233transfer_buffer), and the length you want to write/expect to read. 256transfer_buffer), and the length you want to write/expect to read.
234After completion, actual_length contains the actual transferred length and 257After completion, actual_length contains the actual transferred length and
235status contains the resulting status for the ISO transfer for this frame. 258status contains the resulting status for the ISO transfer for this frame.
236It is allowed to specify a varying length from frame to frame (e.g. for 259It is allowed to specify a varying length from frame to frame (e.g. for
237audio synchronisation/adaptive transfer rates). You can also use the length 260audio synchronisation/adaptive transfer rates). You can also use the length
2380 to omit one or more frames (striping). 2610 to omit one or more frames (striping).
239 262
240For scheduling you can choose your own start frame or ISO_ASAP. As explained 263For scheduling you can choose your own start frame or ``URB_ISO_ASAP``. As
241earlier, if you always keep at least one URB queued and your completion 264explained earlier, if you always keep at least one URB queued and your
242keeps (re)submitting a later URB, you'll get smooth ISO streaming (if usb 265completion keeps (re)submitting a later URB, you'll get smooth ISO streaming
243bandwidth utilization allows). 266(if usb bandwidth utilization allows).
244 267
245If you specify your own start frame, make sure it's several frames in advance 268If you specify your own start frame, make sure it's several frames in advance
246of the current frame. You might want this model if you're synchronizing 269of the current frame. You might want this model if you're synchronizing
247ISO data with some other event stream. 270ISO data with some other event stream.
248 271
249 272
2501.9. How to start interrupt (INT) transfers? 273How to start interrupt (INT) transfers?
274=======================================
251 275
252Interrupt transfers, like isochronous transfers, are periodic, and happen 276Interrupt transfers, like isochronous transfers, are periodic, and happen
253in intervals that are powers of two (1, 2, 4 etc) units. Units are frames 277in intervals that are powers of two (1, 2, 4 etc) units. Units are frames
254for full and low speed devices, and microframes for high speed ones. 278for full and low speed devices, and microframes for high speed ones.
255The usb_submit_urb() call modifies urb->interval to the implemented interval 279You can use the :c:func:`usb_fill_int_urb` macro to fill INT transfer fields.
256value that is less than or equal to the requested interval value. 280
281The :c:func:`usb_submit_urb` call modifies ``urb->interval`` to the implemented
282interval value that is less than or equal to the requested interval value.
257 283
258In Linux 2.6, unlike earlier versions, interrupt URBs are not automagically 284In Linux 2.6, unlike earlier versions, interrupt URBs are not automagically
259restarted when they complete. They end when the completion handler is 285restarted when they complete. They end when the completion handler is
260called, just like other URBs. If you want an interrupt URB to be restarted, 286called, just like other URBs. If you want an interrupt URB to be restarted,
261your completion handler must resubmit it. 287your completion handler must resubmit it.
288s
diff --git a/Documentation/driver-api/usb/index.rst b/Documentation/driver-api/usb/index.rst
index 3f08cb5d5feb..1bf64edc8c8a 100644
--- a/Documentation/driver-api/usb/index.rst
+++ b/Documentation/driver-api/usb/index.rst
@@ -10,6 +10,7 @@ Linux USB API
10 bulk-streams 10 bulk-streams
11 callbacks 11 callbacks
12 dma 12 dma
13 URB
13 power-management 14 power-management
14 hotplug 15 hotplug
15 persist 16 persist
diff --git a/Documentation/driver-api/usb/usb.rst b/Documentation/driver-api/usb/usb.rst
index 7e820768ee4f..d15ab8ae5239 100644
--- a/Documentation/driver-api/usb/usb.rst
+++ b/Documentation/driver-api/usb/usb.rst
@@ -1,3 +1,5 @@
1.. _usb-hostside-api:
2
1=========================== 3===========================
2The Linux-USB Host Side API 4The Linux-USB Host Side API
3=========================== 5===========================