Rev 846 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
846 | giacomo | 1 | #include <linuxcomp.h> |
2 | |||
3 | #include <linux/config.h> |
||
4 | #include <linux/module.h> |
||
5 | #include <linux/string.h> |
||
6 | #include <linux/bitops.h> |
||
7 | #include <linux/slab.h> |
||
8 | #include <linux/init.h> |
||
9 | |||
10 | #ifdef CONFIG_USB_DEBUG |
||
11 | #define DEBUG |
||
12 | #else |
||
13 | #undef DEBUG |
||
14 | #endif |
||
15 | #include <linux/usb.h> |
||
16 | #include "hcd.h" |
||
17 | |||
18 | /** |
||
19 | * usb_init_urb - initializes a urb so that it can be used by a USB driver |
||
20 | * @urb: pointer to the urb to initialize |
||
21 | * |
||
22 | * Initializes a urb so that the USB subsystem can use it properly. |
||
23 | * |
||
24 | * If a urb is created with a call to usb_alloc_urb() it is not |
||
25 | * necessary to call this function. Only use this if you allocate the |
||
26 | * space for a struct urb on your own. If you call this function, be |
||
27 | * careful when freeing the memory for your urb that it is no longer in |
||
28 | * use by the USB core. |
||
29 | * |
||
30 | * Only use this function if you _really_ understand what you are doing. |
||
31 | */ |
||
32 | void usb_init_urb(struct urb *urb) |
||
33 | { |
||
34 | if (urb) { |
||
35 | memset(urb, 0, sizeof(*urb)); |
||
36 | urb->count = (atomic_t)ATOMIC_INIT(1); |
||
37 | spin_lock_init(&urb->lock); |
||
38 | } |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * usb_alloc_urb - creates a new urb for a USB driver to use |
||
43 | * @iso_packets: number of iso packets for this urb |
||
44 | * @mem_flags: the type of memory to allocate, see kmalloc() for a list of |
||
45 | * valid options for this. |
||
46 | * |
||
47 | * Creates an urb for the USB driver to use, initializes a few internal |
||
48 | * structures, incrementes the usage counter, and returns a pointer to it. |
||
49 | * |
||
50 | * If no memory is available, NULL is returned. |
||
51 | * |
||
52 | * If the driver want to use this urb for interrupt, control, or bulk |
||
53 | * endpoints, pass '0' as the number of iso packets. |
||
54 | * |
||
55 | * The driver must call usb_free_urb() when it is finished with the urb. |
||
56 | */ |
||
57 | struct urb *usb_alloc_urb(int iso_packets, int mem_flags) |
||
58 | { |
||
59 | struct urb *urb; |
||
60 | |||
61 | urb = (struct urb *)kmalloc(sizeof(struct urb) + |
||
62 | iso_packets * sizeof(struct usb_iso_packet_descriptor), |
||
63 | mem_flags); |
||
64 | if (!urb) { |
||
65 | err("alloc_urb: kmalloc failed"); |
||
66 | return NULL; |
||
67 | } |
||
68 | usb_init_urb(urb); |
||
69 | return urb; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * usb_free_urb - frees the memory used by a urb when all users of it are finished |
||
74 | * @urb: pointer to the urb to free |
||
75 | * |
||
76 | * Must be called when a user of a urb is finished with it. When the last user |
||
77 | * of the urb calls this function, the memory of the urb is freed. |
||
78 | * |
||
79 | * Note: The transfer buffer associated with the urb is not freed, that must be |
||
80 | * done elsewhere. |
||
81 | */ |
||
82 | void usb_free_urb(struct urb *urb) |
||
83 | { |
||
84 | if (urb) |
||
85 | if (atomic_dec_and_test(&urb->count)) |
||
86 | kfree(urb); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * usb_get_urb - increments the reference count of the urb |
||
91 | * @urb: pointer to the urb to modify |
||
92 | * |
||
93 | * This must be called whenever a urb is transferred from a device driver to a |
||
94 | * host controller driver. This allows proper reference counting to happen |
||
95 | * for urbs. |
||
96 | * |
||
97 | * A pointer to the urb with the incremented reference counter is returned. |
||
98 | */ |
||
99 | struct urb * usb_get_urb(struct urb *urb) |
||
100 | { |
||
101 | if (urb) { |
||
102 | atomic_inc(&urb->count); |
||
103 | return urb; |
||
104 | } else |
||
105 | return NULL; |
||
106 | } |
||
107 | |||
108 | |||
109 | /*-------------------------------------------------------------------*/ |
||
110 | |||
111 | /** |
||
112 | * usb_submit_urb - issue an asynchronous transfer request for an endpoint |
||
113 | * @urb: pointer to the urb describing the request |
||
114 | * @mem_flags: the type of memory to allocate, see kmalloc() for a list |
||
115 | * of valid options for this. |
||
116 | * |
||
117 | * This submits a transfer request, and transfers control of the URB |
||
118 | * describing that request to the USB subsystem. Request completion will |
||
119 | * be indicated later, asynchronously, by calling the completion handler. |
||
120 | * The three types of completion are success, error, and unlink |
||
121 | * (also called "request cancellation"). |
||
122 | * URBs may be submitted in interrupt context. |
||
123 | * |
||
124 | * The caller must have correctly initialized the URB before submitting |
||
125 | * it. Functions such as usb_fill_bulk_urb() and usb_fill_control_urb() are |
||
126 | * available to ensure that most fields are correctly initialized, for |
||
127 | * the particular kind of transfer, although they will not initialize |
||
128 | * any transfer flags. |
||
129 | * |
||
130 | * Successful submissions return 0; otherwise this routine returns a |
||
131 | * negative error number. If the submission is successful, the complete() |
||
132 | * callback from the urb will be called exactly once, when the USB core and |
||
133 | * host controller driver are finished with the urb. When the completion |
||
134 | * function is called, control of the URB is returned to the device |
||
135 | * driver which issued the request. The completion handler may then |
||
136 | * immediately free or reuse that URB. |
||
137 | * |
||
138 | * For control endpoints, the synchronous usb_control_msg() call is |
||
139 | * often used (in non-interrupt context) instead of this call. |
||
140 | * That is often used through convenience wrappers, for the requests |
||
141 | * that are standardized in the USB 2.0 specification. For bulk |
||
142 | * endpoints, a synchronous usb_bulk_msg() call is available. |
||
143 | * |
||
144 | * Request Queuing: |
||
145 | * |
||
146 | * URBs may be submitted to endpoints before previous ones complete, to |
||
147 | * minimize the impact of interrupt latencies and system overhead on data |
||
148 | * throughput. This is required for continuous isochronous data streams, |
||
149 | * and may also be required for some kinds of interrupt transfers. Such |
||
150 | * queueing also maximizes bandwidth utilization by letting USB controllers |
||
151 | * start work on later requests before driver software has finished the |
||
152 | * completion processing for earlier requests. |
||
153 | * |
||
154 | * Bulk and Isochronous URBs may always be queued. At this writing, all |
||
155 | * mainstream host controller drivers support queueing for control and |
||
156 | * interrupt transfer requests. |
||
157 | * |
||
158 | * Reserved Bandwidth Transfers: |
||
159 | * |
||
160 | * Periodic transfers (interrupt or isochronous) are performed repeatedly, |
||
161 | * using the interval specified in the urb. Submitting the first urb to |
||
162 | * the endpoint reserves the bandwidth necessary to make those transfers. |
||
163 | * If the USB subsystem can't allocate sufficient bandwidth to perform |
||
164 | * the periodic request, submitting such a periodic request should fail. |
||
165 | * |
||
166 | * Device drivers must explicitly request that repetition, by ensuring that |
||
167 | * some URB is always on the endpoint's queue (except possibly for short |
||
168 | * periods during completion callacks). When there is no longer an urb |
||
169 | * queued, the endpoint's bandwidth reservation is canceled. This means |
||
170 | * drivers can use their completion handlers to ensure they keep bandwidth |
||
171 | * they need, by reinitializing and resubmitting the just-completed urb |
||
172 | * until the driver longer needs that periodic bandwidth. |
||
173 | * |
||
174 | * Memory Flags: |
||
175 | * |
||
176 | * The general rules for how to decide which mem_flags to use |
||
177 | * are the same as for kmalloc. There are four |
||
178 | * different possible values; GFP_KERNEL, GFP_NOFS, GFP_NOIO and |
||
179 | * GFP_ATOMIC. |
||
180 | * |
||
181 | * GFP_NOFS is not ever used, as it has not been implemented yet. |
||
182 | * |
||
183 | * GFP_ATOMIC is used when |
||
184 | * (a) you are inside a completion handler, an interrupt, bottom half, |
||
185 | * tasklet or timer, or |
||
186 | * (b) you are holding a spinlock or rwlock (does not apply to |
||
187 | * semaphores), or |
||
188 | * (c) current->state != TASK_RUNNING, this is the case only after |
||
189 | * you've changed it. |
||
190 | * |
||
191 | * GFP_NOIO is used in the block io path and error handling of storage |
||
192 | * devices. |
||
193 | * |
||
194 | * All other situations use GFP_KERNEL. |
||
195 | * |
||
196 | * Some more specific rules for mem_flags can be inferred, such as |
||
197 | * (1) start_xmit, timeout, and receive methods of network drivers must |
||
198 | * use GFP_ATOMIC (they are called with a spinlock held); |
||
199 | * (2) queuecommand methods of scsi drivers must use GFP_ATOMIC (also |
||
200 | * called with a spinlock held); |
||
201 | * (3) If you use a kernel thread with a network driver you must use |
||
202 | * GFP_NOIO, unless (b) or (c) apply; |
||
203 | * (4) after you have done a down() you can use GFP_KERNEL, unless (b) or (c) |
||
204 | * apply or your are in a storage driver's block io path; |
||
205 | * (5) USB probe and disconnect can use GFP_KERNEL unless (b) or (c) apply; and |
||
206 | * (6) changing firmware on a running storage or net device uses |
||
207 | * GFP_NOIO, unless b) or c) apply |
||
208 | * |
||
209 | */ |
||
210 | int usb_submit_urb(struct urb *urb, int mem_flags) |
||
211 | { |
||
212 | int pipe, temp, max; |
||
213 | struct usb_device *dev; |
||
214 | struct usb_operations *op; |
||
215 | int is_out; |
||
216 | |||
217 | if (!urb || urb->hcpriv || !urb->complete) |
||
218 | return -EINVAL; |
||
219 | if (!(dev = urb->dev) || |
||
220 | (dev->state < USB_STATE_DEFAULT) || |
||
221 | (!dev->bus) || (dev->devnum <= 0)) |
||
222 | return -ENODEV; |
||
223 | if (!(op = dev->bus->op) || !op->submit_urb) |
||
224 | return -ENODEV; |
||
225 | |||
226 | urb->status = -EINPROGRESS; |
||
227 | urb->actual_length = 0; |
||
228 | urb->bandwidth = 0; |
||
229 | |||
230 | /* Lots of sanity checks, so HCDs can rely on clean data |
||
231 | * and don't need to duplicate tests |
||
232 | */ |
||
233 | pipe = urb->pipe; |
||
234 | temp = usb_pipetype (pipe); |
||
235 | is_out = usb_pipeout (pipe); |
||
236 | |||
237 | if (!usb_pipecontrol (pipe) && dev->state < USB_STATE_CONFIGURED) |
||
238 | return -ENODEV; |
||
239 | |||
240 | /* (actually HCDs may need to duplicate this, endpoint might yet |
||
241 | * stall due to queued bulk/intr transactions that complete after |
||
242 | * we check) |
||
243 | */ |
||
244 | if (usb_endpoint_halted (dev, usb_pipeendpoint (pipe), is_out)) |
||
245 | return -EPIPE; |
||
246 | |||
247 | /* FIXME there should be a sharable lock protecting us against |
||
248 | * config/altsetting changes and disconnects, kicking in here. |
||
249 | * (here == before maxpacket, and eventually endpoint type, |
||
250 | * checks get made.) |
||
251 | */ |
||
252 | |||
253 | max = usb_maxpacket (dev, pipe, is_out); |
||
254 | if (max <= 0) { |
||
255 | dbg ("%s: bogus endpoint %d-%s on usb-%s-%s (bad maxpacket %d)", |
||
256 | __FUNCTION__, |
||
257 | usb_pipeendpoint (pipe), is_out ? "OUT" : "IN", |
||
258 | dev->bus->bus_name, dev->devpath, |
||
259 | max); |
||
260 | return -EMSGSIZE; |
||
261 | } |
||
262 | |||
263 | /* periodic transfers limit size per frame/uframe, |
||
264 | * but drivers only control those sizes for ISO. |
||
265 | * while we're checking, initialize return status. |
||
266 | */ |
||
267 | if (temp == PIPE_ISOCHRONOUS) { |
||
268 | int n, len; |
||
269 | |||
270 | /* "high bandwidth" mode, 1-3 packets/uframe? */ |
||
271 | if (dev->speed == USB_SPEED_HIGH) { |
||
272 | int mult = 1 + ((max >> 11) & 0x03); |
||
273 | max &= 0x03ff; |
||
274 | max *= mult; |
||
275 | } |
||
276 | |||
277 | if (urb->number_of_packets <= 0) |
||
278 | return -EINVAL; |
||
279 | for (n = 0; n < urb->number_of_packets; n++) { |
||
280 | len = urb->iso_frame_desc [n].length; |
||
281 | if (len < 0 || len > max) |
||
282 | return -EMSGSIZE; |
||
283 | urb->iso_frame_desc [n].status = -EXDEV; |
||
284 | urb->iso_frame_desc [n].actual_length = 0; |
||
285 | } |
||
286 | } |
||
287 | |||
288 | /* the I/O buffer must be mapped/unmapped, except when length=0 */ |
||
289 | if (urb->transfer_buffer_length < 0) |
||
290 | return -EMSGSIZE; |
||
291 | |||
292 | #ifdef DEBUG |
||
293 | /* stuff that drivers shouldn't do, but which shouldn't |
||
294 | * cause problems in HCDs if they get it wrong. |
||
295 | */ |
||
296 | { |
||
297 | unsigned int orig_flags = urb->transfer_flags; |
||
298 | unsigned int allowed; |
||
299 | |||
300 | /* enforce simple/standard policy */ |
||
301 | allowed = URB_ASYNC_UNLINK; // affects later unlinks |
||
302 | allowed |= (URB_NO_TRANSFER_DMA_MAP | URB_NO_SETUP_DMA_MAP); |
||
303 | allowed |= URB_NO_INTERRUPT; |
||
304 | switch (temp) { |
||
305 | case PIPE_BULK: |
||
306 | if (is_out) |
||
307 | allowed |= URB_ZERO_PACKET; |
||
308 | /* FALLTHROUGH */ |
||
309 | case PIPE_CONTROL: |
||
310 | allowed |= URB_NO_FSBR; /* only affects UHCI */ |
||
311 | /* FALLTHROUGH */ |
||
312 | default: /* all non-iso endpoints */ |
||
313 | if (!is_out) |
||
314 | allowed |= URB_SHORT_NOT_OK; |
||
315 | break; |
||
316 | case PIPE_ISOCHRONOUS: |
||
317 | allowed |= URB_ISO_ASAP; |
||
318 | break; |
||
319 | } |
||
320 | urb->transfer_flags &= allowed; |
||
321 | |||
322 | /* fail if submitter gave bogus flags */ |
||
323 | if (urb->transfer_flags != orig_flags) { |
||
324 | err ("BOGUS urb flags, %x --> %x", |
||
325 | orig_flags, urb->transfer_flags); |
||
326 | return -EINVAL; |
||
327 | } |
||
328 | } |
||
329 | #endif |
||
330 | /* |
||
331 | * Force periodic transfer intervals to be legal values that are |
||
332 | * a power of two (so HCDs don't need to). |
||
333 | * |
||
334 | * FIXME want bus->{intr,iso}_sched_horizon values here. Each HC |
||
335 | * supports different values... this uses EHCI/UHCI defaults (and |
||
336 | * EHCI can use smaller non-default values). |
||
337 | */ |
||
338 | switch (temp) { |
||
339 | case PIPE_ISOCHRONOUS: |
||
340 | case PIPE_INTERRUPT: |
||
341 | /* too small? */ |
||
342 | if (urb->interval <= 0) |
||
343 | return -EINVAL; |
||
344 | /* too big? */ |
||
345 | switch (dev->speed) { |
||
346 | case USB_SPEED_HIGH: /* units are microframes */ |
||
347 | // NOTE usb handles 2^15 |
||
348 | if (urb->interval > (1024 * 8)) |
||
349 | urb->interval = 1024 * 8; |
||
350 | temp = 1024 * 8; |
||
351 | break; |
||
352 | case USB_SPEED_FULL: /* units are frames/msec */ |
||
353 | case USB_SPEED_LOW: |
||
354 | if (temp == PIPE_INTERRUPT) { |
||
355 | if (urb->interval > 255) |
||
356 | return -EINVAL; |
||
357 | // NOTE ohci only handles up to 32 |
||
358 | temp = 128; |
||
359 | } else { |
||
360 | if (urb->interval > 1024) |
||
361 | urb->interval = 1024; |
||
362 | // NOTE usb and ohci handle up to 2^15 |
||
363 | temp = 1024; |
||
364 | } |
||
365 | break; |
||
366 | default: |
||
367 | return -EINVAL; |
||
368 | } |
||
369 | /* power of two? */ |
||
370 | while (temp > urb->interval) |
||
371 | temp >>= 1; |
||
372 | urb->interval = temp; |
||
373 | } |
||
374 | |||
375 | return op->submit_urb (urb, mem_flags); |
||
376 | } |
||
377 | |||
378 | /*-------------------------------------------------------------------*/ |
||
379 | |||
380 | /** |
||
381 | * usb_unlink_urb - abort/cancel a transfer request for an endpoint |
||
382 | * @urb: pointer to urb describing a previously submitted request |
||
383 | * |
||
384 | * This routine cancels an in-progress request. URBs complete only |
||
385 | * once per submission, and may be canceled only once per submission. |
||
386 | * Successful cancelation means the requests's completion handler will |
||
387 | * be called with a status code indicating that the request has been |
||
388 | * canceled (rather than any other code) and will quickly be removed |
||
389 | * from host controller data structures. |
||
390 | * |
||
391 | * When the URB_ASYNC_UNLINK transfer flag for the URB is clear, this |
||
392 | * request is synchronous. Success is indicated by returning zero, |
||
393 | * at which time the urb will have been unlinked and its completion |
||
394 | * handler will have been called with urb->status -ENOENT. Failure is |
||
395 | * indicated by any other return value. |
||
396 | * |
||
397 | * The synchronous cancelation mode may not be used |
||
398 | * when unlinking an urb from an interrupt context, such as a bottom |
||
399 | * half or a completion handler; or when holding a spinlock; or in |
||
400 | * other cases when the caller can't schedule(). |
||
401 | * |
||
402 | * When the URB_ASYNC_UNLINK transfer flag for the URB is set, this |
||
403 | * request is asynchronous. Success is indicated by returning -EINPROGRESS, |
||
404 | * at which time the urb will normally not have been unlinked. |
||
405 | * The completion function will see urb->status -ECONNRESET. Failure |
||
406 | * is indicated by any other return value. |
||
407 | */ |
||
408 | int usb_unlink_urb(struct urb *urb) |
||
409 | { |
||
410 | if (urb && urb->dev && urb->dev->bus && urb->dev->bus->op) |
||
411 | return urb->dev->bus->op->unlink_urb(urb); |
||
412 | else |
||
413 | return -ENODEV; |
||
414 | } |
||
415 | |||
416 | EXPORT_SYMBOL(usb_init_urb); |
||
417 | EXPORT_SYMBOL(usb_alloc_urb); |
||
418 | EXPORT_SYMBOL(usb_free_urb); |
||
419 | EXPORT_SYMBOL(usb_get_urb); |
||
420 | EXPORT_SYMBOL(usb_submit_urb); |
||
421 | EXPORT_SYMBOL(usb_unlink_urb); |
||
422 |