3 1. Specification of the API
5 1.1. Basic concept or 'What is an URB?'
7 The basic idea of the new driver is message passing, the message itself is
8 called USB Request Block, or URB for short.
10 - An URB consists of all relevant information to execute any USB transaction
11 and deliver the data and status back.
13 - Execution of an URB is inherently an asynchronous operation, i.e. the
14 usb_submit_urb(urb) call returns immediately after it has successfully queued
17 - Ongoing transfers for one URB (e.g. ISO) can simply be canceled with
18 usb_unlink_urb(urb) at any time.
20 - Each URB has a completion handler, which is called after the action
21 has been successfully completed or canceled (INT transfers behave a bit
22 differently, see below). The URB also contains a context-pointer for free
23 usage and information passing to the completion handler.
25 - URBs can be linked. After completing one URB, the next one can be
26 automatically submitted. This is especially useful for ISO transfers:
27 You only have read/write the data from/to the buffers in the completion
28 handler, the continuous streaming itself is transparently done by the
32 1.2. The URB structure
36 spinlock_t lock; // lock for the URB
38 // ignore, for host controller/URB machine internal use
39 void *hcpriv; // private data for host controller
40 struct list_head urb_list; // list pointer to all active urbs
42 // This is used for urb linking
43 struct urb* next; // pointer to next URB
44 struct usb_device *dev; // pointer to associated USB device
46 // pipe is assembled by the various well-known pipe macros in usb.h
47 unsigned int pipe; // pipe information
49 // status after each completion
50 int status; // returned status
51 unsigned int transfer_flags; // ASAP, DISABLE_SPD, etc.
53 // for data stage (CTRL), BULK, INT and ISO
54 void *transfer_buffer; // associated data buffer
57 int transfer_buffer_length; // data buffer length
58 int actual_length; // actual data buffer length
60 // setup stage for CTRL (always 8 bytes!)
61 unsigned char* setup_packet; // setup packet (control only)
63 // with ASAP, start_frame is set to the determined frame
64 int start_frame; // start frame (iso/irq)
65 int number_of_packets; // # of packets (iso/int)
66 int interval; // polling interval (irq only)
67 int error_count; // number of errors (iso only)
69 void *context; // context for completion routine
70 usb_complete_t complete; // pointer to completion routine
72 // specification of the requested data offsets and length for ISO
73 iso_packet_descriptor_t iso_frame_desc[0];
77 1.3. How to get an URB?
79 URBs are allocated with the following call
81 purb_t usb_alloc_urb(int isoframes)
83 Return value is a pointer to the allocated URB, 0 if allocation failed.
84 The parameter isoframes specifies the number of isochronous transfer frames
85 you want to schedule. For CTRL/BULK/INT, use 0.
89 void usb_free_urb(purb_t purb)
91 This call also may free internal (host controller specific) memory in the
95 1.4. What has to be filled in?
97 Depending on the type of transaction, there are some macros
98 (FILL_CONTROL_URB, FILL_CONTROL_URB_TO, FILL_BULK_URB,
99 FILL_BULK_URB_TO, and FILL_INT_URB, defined in usb.h)
100 that simplify the URB creation. In general, all macros need the usb
101 device pointer, the pipe (usual format from usb.h), the transfer buffer,
102 the desired transfer length, the completion handler, and its context.
103 Take a look at the usb_control_msg function that converts the old API
107 For ISO there are two startup behaviors: Specified start_frame or ASAP.
108 For ASAP set USB_ISO_ASAP in transfer_flags.
110 If short packets should NOT be tolerated, set USB_DISABLE_SPD in
113 Usually, to reduce restart time, the completion handler is called
114 AFTER the URB re-submission. However, it is called BEFORE URB
115 re-submission for INT transfers that are being continued.
118 1.5. How to submit an URB?
122 int usb_submit_urb(purb_t purb)
124 It immediately returns, either with status 0 (request queued) or some
125 error code, usually caused by the following:
127 - Out of memory (-ENOMEM)
128 - Wrong pipe handle (-ENXIO)
129 - Unplugged device (-ENODEV)
130 - Stalled endpoint (-EPIPE)
131 - Too many queued ISO transfers (-EAGAIN)
132 - Too many requested ISO frames (-EFBIG)
133 - Invalid INT interval (-EINVAL)
134 - More than one packet for INT (-EINVAL)
136 After submission, urb->status is USB_ST_URB_PENDING (-EINPROGRESS).
138 For isochronous endpoints, subsequent submitting of URBs to the same endpoint
139 with the ASAP flag result in a seamless ISO streaming. Exception: The
140 execution cannot be scheduled later than 900 frames from the 'now'-time.
141 The same applies to INT transfers, but here the seamless continuation is
142 independent of the transfer flags (implicitly ASAP).
145 1.6. How to cancel an already running URB?
147 For an URB which you've submitted, but which hasn't been returned to
148 your driver by the host controller, call
150 int usb_unlink_urb(purb_t purb)
152 It removes the urb from the internal list and frees all allocated
153 HW descriptors. The status is changed to USB_ST_URB_KILLED. After
154 usb_unlink_urb() returns, you can safely free the URB with usb_free_urb(urb)
155 and all other possibly associated data (urb->context etc.)
157 There is also an asynchronous unlink mode. To use this, set the
158 the USB_ASYNC_UNLINK flag in urb->transfer flags before calling
159 usb_unlink_urb(). When using async unlinking, the URB will not
160 normally be unlinked when usb_unlink_urb() returns. Instead, wait
161 for the completion handler to be called.
164 1.7. What about the completion handler?
166 The completion handler is optional, but useful for fast data processing
167 or wakeup of a sleeping process (as shown in the compatibility wrapper's
170 The handler is of the following type:
172 typedef void (*usb_complete_t)(struct urb *);
174 i.e. it gets just the URB that caused the completion call.
175 In the completion handler, you should have a look at urb->status to
176 detect any USB errors. Since the context parameter is included in the URB,
177 you can pass information to the completion handler.
179 NOTE: ***** WARNING *****
180 AVOID using the urb->dev field in your completion handler; it's cleared
181 as part of URB unlinking. Instead, use urb->context to hold all the
182 data your driver needs.
184 NOTE: ***** WARNING *****
185 Also, NEVER SLEEP IN A COMPLETION HANDLER. These are normally called
186 during hardware interrupt processing. If you can, defer substantial
187 work to a tasklet (bottom half) to keep system latencies low. You'll
188 probably need to use spinlocks to protect data structures you manipulate
189 in completion handlers.
192 1.8. How to do isochronous (ISO) transfers?
194 For ISO transfers you have to append the iso_packet_descriptor_t structure
195 to the URB for each frame you want to schedule. When using usb_alloc_urb(n)
196 (recommended), the iso_packets parameter can be used to allocate the
197 structures for iso_packets frames.
199 For each entry you have to specify the data offset for this frame (base is
200 transfer_buffer), and the length you want to write/expect to read.
201 After completion, actual_length contains the actual transferred length and
202 status contains the resulting USB-status for the ISO transfer for this frame.
203 It is allowed to specify a varying length from frame to frame (e.g. for
204 audio synchronisation/adaptive transfer rates). You can also use the length
205 0 to omit one or more frames (striping).
207 As can be concluded from above, the UHCI-driver does not care for continuous
208 data in case of short packet ISO reads! There's no fixup_isoc() like in the
209 old driver. There may be a common routine to do this in the future, but this
210 has nothing to do with the UHCI-driver!
212 For scheduling you can choose your own start frame or ASAP. As written above,
213 queuing more than one ISO frame with ASAP to the same device&endpoint result
214 in seamless ISO streaming. For continuous streaming you have to use URB
218 1.9. How to start interrupt (INT) transfers?
220 INT transfers are currently implemented with different queues for intervals
221 for 1, 2, 4,... 128ms. Only one URB is allocated for each interrupt. After
222 calling the completion handler, that URB is recycled by the host controller
224 With the submission of one URB, the interrupt is scheduled until it is
225 canceled by usb_unlink_urb.
227 The usb_submit_urb() call modifies urb->interval to the implemented interval
228 value that is less than or equal to the requested interval value.