Rev 1049 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
846 | giacomo | 1 | /* |
2 | * message.c - synchronous message handling |
||
3 | */ |
||
4 | |||
5 | #include <linuxcomp.h> |
||
6 | |||
7 | #include <linux/config.h> |
||
8 | |||
9 | #ifdef CONFIG_USB_DEBUG |
||
10 | #define DEBUG |
||
11 | #else |
||
12 | #undef DEBUG |
||
13 | #endif |
||
14 | |||
15 | #include <linux/pci.h> /* for scatterlist macros */ |
||
16 | #include <linux/usb.h> |
||
17 | #include <linux/module.h> |
||
18 | #include <linux/slab.h> |
||
19 | #include <linux/init.h> |
||
20 | #include <linux/mm.h> |
||
21 | #include <linux/timer.h> |
||
22 | #include <asm/byteorder.h> |
||
23 | |||
24 | #include "hcd.h" /* for usbcore internals */ |
||
25 | #include "usb.h" |
||
26 | |||
27 | static void usb_api_blocking_completion(struct urb *urb, struct pt_regs *regs) |
||
28 | { |
||
29 | complete((struct completion *)urb->context); |
||
30 | } |
||
31 | |||
32 | |||
33 | static void timeout_kill(unsigned long data) |
||
34 | { |
||
35 | struct urb *urb = (struct urb *) data; |
||
36 | |||
37 | dev_warn(&urb->dev->dev, "%s timeout on ep%d%s\n", |
||
38 | usb_pipecontrol(urb->pipe) ? "control" : "bulk", |
||
39 | usb_pipeendpoint(urb->pipe), |
||
40 | usb_pipein(urb->pipe) ? "in" : "out"); |
||
41 | usb_unlink_urb(urb); |
||
42 | } |
||
43 | |||
44 | // Starts urb and waits for completion or timeout |
||
45 | // note that this call is NOT interruptible, while |
||
46 | // many device driver i/o requests should be interruptible |
||
47 | static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length) |
||
48 | { |
||
49 | struct completion done; |
||
50 | struct timer_list timer; |
||
51 | int status; |
||
52 | |||
53 | init_completion(&done); |
||
54 | urb->context = &done; |
||
55 | urb->transfer_flags |= URB_ASYNC_UNLINK; |
||
56 | urb->actual_length = 0; |
||
57 | |||
58 | status = usb_submit_urb(urb, GFP_NOIO); |
||
59 | |||
60 | if (status == 0) { |
||
61 | if (timeout > 0) { |
||
62 | init_timer(&timer); |
||
63 | timer.expires = jiffies26 + timeout; |
||
64 | timer.data = (unsigned long)urb; |
||
65 | timer.function = timeout_kill; |
||
66 | /* grr. timeout _should_ include submit delays. */ |
||
67 | add_timer(&timer); |
||
68 | } |
||
69 | wait_for_completion(&done); |
||
70 | status = urb->status; |
||
71 | /* note: HCDs return ETIMEDOUT for other reasons too */ |
||
72 | if (status == -ECONNRESET) |
||
73 | status = -ETIMEDOUT; |
||
74 | if (timeout > 0) |
||
75 | del_timer_sync(&timer); |
||
76 | } |
||
77 | |||
78 | if (actual_length) |
||
79 | *actual_length = urb->actual_length; |
||
80 | usb_free_urb(urb); |
||
81 | return status; |
||
82 | } |
||
83 | |||
84 | /*-------------------------------------------------------------------*/ |
||
85 | // returns status (negative) or length (positive) |
||
86 | int usb_internal_control_msg(struct usb_device *usb_dev, unsigned int pipe, |
||
87 | struct usb_ctrlrequest *cmd, void *data, int len, int timeout) |
||
88 | { |
||
89 | struct urb *urb; |
||
90 | int retv; |
||
91 | int length; |
||
92 | |||
93 | urb = usb_alloc_urb(0, GFP_NOIO); |
||
94 | if (!urb) |
||
95 | return -ENOMEM; |
||
96 | |||
97 | usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data, len, |
||
98 | usb_api_blocking_completion, 0); |
||
99 | retv = usb_start_wait_urb(urb, timeout, &length); |
||
100 | if (retv < 0) |
||
101 | return retv; |
||
102 | else |
||
103 | return length; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * usb_control_msg - Builds a control urb, sends it off and waits for completion |
||
108 | * @dev: pointer to the usb device to send the message to |
||
109 | * @pipe: endpoint "pipe" to send the message to |
||
110 | * @request: USB message request value |
||
111 | * @requesttype: USB message request type value |
||
112 | * @value: USB message value |
||
113 | * @index: USB message index value |
||
114 | * @data: pointer to the data to send |
||
115 | * @size: length in bytes of the data to send |
||
116 | * @timeout: time in jiffies26 to wait for the message to complete before |
||
117 | * timing out (if 0 the wait is forever) |
||
118 | * Context: !in_interrupt () |
||
119 | * |
||
120 | * This function sends a simple control message to a specified endpoint |
||
121 | * and waits for the message to complete, or timeout. |
||
122 | * |
||
123 | * If successful, it returns the number of bytes transferred, otherwise a negative error number. |
||
124 | * |
||
125 | * Don't use this function from within an interrupt context, like a |
||
126 | * bottom half handler. If you need an asynchronous message, or need to send |
||
127 | * a message from within interrupt context, use usb_submit_urb() |
||
128 | * If a thread in your driver uses this call, make sure your disconnect() |
||
129 | * method can wait for it to complete. Since you don't have a handle on |
||
130 | * the URB used, you can't cancel the request. |
||
131 | */ |
||
132 | int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype, |
||
133 | __u16 value, __u16 index, void *data, __u16 size, int timeout) |
||
134 | { |
||
135 | struct usb_ctrlrequest *dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO); |
||
136 | int ret; |
||
137 | |||
138 | if (!dr) |
||
139 | return -ENOMEM; |
||
140 | |||
141 | dr->bRequestType= requesttype; |
||
142 | dr->bRequest = request; |
||
143 | dr->wValue = cpu_to_le16p(&value); |
||
144 | dr->wIndex = cpu_to_le16p(&index); |
||
145 | dr->wLength = cpu_to_le16p(&size); |
||
146 | |||
147 | //dbg("usb_control_msg"); |
||
148 | |||
149 | ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout); |
||
150 | |||
151 | kfree(dr); |
||
152 | |||
153 | return ret; |
||
154 | } |
||
155 | |||
156 | |||
157 | /** |
||
158 | * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion |
||
159 | * @usb_dev: pointer to the usb device to send the message to |
||
160 | * @pipe: endpoint "pipe" to send the message to |
||
161 | * @data: pointer to the data to send |
||
162 | * @len: length in bytes of the data to send |
||
163 | * @actual_length: pointer to a location to put the actual length transferred in bytes |
||
164 | * @timeout: time in jiffies26 to wait for the message to complete before |
||
165 | * timing out (if 0 the wait is forever) |
||
166 | * Context: !in_interrupt () |
||
167 | * |
||
168 | * This function sends a simple bulk message to a specified endpoint |
||
169 | * and waits for the message to complete, or timeout. |
||
170 | * |
||
171 | * If successful, it returns 0, otherwise a negative error number. |
||
172 | * The number of actual bytes transferred will be stored in the |
||
173 | * actual_length paramater. |
||
174 | * |
||
175 | * Don't use this function from within an interrupt context, like a |
||
176 | * bottom half handler. If you need an asynchronous message, or need to |
||
177 | * send a message from within interrupt context, use usb_submit_urb() |
||
178 | * If a thread in your driver uses this call, make sure your disconnect() |
||
179 | * method can wait for it to complete. Since you don't have a handle on |
||
180 | * the URB used, you can't cancel the request. |
||
181 | */ |
||
182 | int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, |
||
183 | void *data, int len, int *actual_length, int timeout) |
||
184 | { |
||
185 | struct urb *urb; |
||
186 | |||
187 | if (len < 0) |
||
188 | return -EINVAL; |
||
189 | |||
190 | urb=usb_alloc_urb(0, GFP_KERNEL); |
||
191 | if (!urb) |
||
192 | return -ENOMEM; |
||
193 | |||
194 | usb_fill_bulk_urb(urb, usb_dev, pipe, data, len, |
||
195 | usb_api_blocking_completion, 0); |
||
196 | |||
197 | return usb_start_wait_urb(urb,timeout,actual_length); |
||
198 | } |
||
199 | |||
200 | /*-------------------------------------------------------------------*/ |
||
201 | |||
202 | static void sg_clean (struct usb_sg_request *io) |
||
203 | { |
||
204 | if (io->urbs) { |
||
205 | while (io->entries--) |
||
206 | usb_free_urb (io->urbs [io->entries]); |
||
207 | kfree (io->urbs); |
||
208 | io->urbs = 0; |
||
209 | } |
||
210 | if (io->dev->dev.dma_mask != 0) |
||
211 | usb_buffer_unmap_sg (io->dev, io->pipe, io->sg, io->nents); |
||
212 | io->dev = 0; |
||
213 | } |
||
214 | |||
215 | static void sg_complete (struct urb *urb, struct pt_regs *regs) |
||
216 | { |
||
217 | struct usb_sg_request *io = (struct usb_sg_request *) urb->context; |
||
218 | unsigned long flags; |
||
219 | |||
220 | spin_lock_irqsave (&io->lock, flags); |
||
221 | |||
222 | /* In 2.5 we require hcds' endpoint queues not to progress after fault |
||
223 | * reports, until the completion callback (this!) returns. That lets |
||
224 | * device driver code (like this routine) unlink queued urbs first, |
||
225 | * if it needs to, since the HC won't work on them at all. So it's |
||
226 | * not possible for page N+1 to overwrite page N, and so on. |
||
227 | * |
||
228 | * That's only for "hard" faults; "soft" faults (unlinks) sometimes |
||
229 | * complete before the HCD can get requests away from hardware, |
||
230 | * though never during cleanup after a hard fault. |
||
231 | */ |
||
232 | if (io->status |
||
233 | && (io->status != -ECONNRESET |
||
234 | || urb->status != -ECONNRESET) |
||
235 | && urb->actual_length) { |
||
236 | dev_err (io->dev->bus->controller, |
||
237 | "dev %s ep%d%s scatterlist error %d/%d\n", |
||
238 | io->dev->devpath, |
||
239 | usb_pipeendpoint (urb->pipe), |
||
240 | usb_pipein (urb->pipe) ? "in" : "out", |
||
241 | urb->status, io->status); |
||
242 | // BUG (); |
||
243 | } |
||
244 | |||
245 | if (urb->status && urb->status != -ECONNRESET) { |
||
246 | int i, found, status; |
||
247 | |||
248 | io->status = urb->status; |
||
249 | |||
250 | /* the previous urbs, and this one, completed already. |
||
251 | * unlink pending urbs so they won't rx/tx bad data. |
||
252 | */ |
||
253 | for (i = 0, found = 0; i < io->entries; i++) { |
||
254 | if (!io->urbs [i]) |
||
255 | continue; |
||
256 | if (found) { |
||
257 | status = usb_unlink_urb (io->urbs [i]); |
||
258 | if (status != -EINPROGRESS && status != -EBUSY) |
||
259 | dev_err (&io->dev->dev, |
||
260 | "%s, unlink --> %d\n", |
||
261 | __FUNCTION__, status); |
||
262 | } else if (urb == io->urbs [i]) |
||
263 | found = 1; |
||
264 | } |
||
265 | } |
||
266 | urb->dev = 0; |
||
267 | |||
268 | /* on the last completion, signal usb_sg_wait() */ |
||
269 | io->bytes += urb->actual_length; |
||
270 | io->count--; |
||
271 | if (!io->count) |
||
272 | complete (&io->complete); |
||
273 | |||
274 | spin_unlock_irqrestore (&io->lock, flags); |
||
275 | } |
||
276 | |||
277 | |||
278 | /** |
||
279 | * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request |
||
280 | * @io: request block being initialized. until usb_sg_wait() returns, |
||
281 | * treat this as a pointer to an opaque block of memory, |
||
282 | * @dev: the usb device that will send or receive the data |
||
283 | * @pipe: endpoint "pipe" used to transfer the data |
||
284 | * @period: polling rate for interrupt endpoints, in frames or |
||
285 | * (for high speed endpoints) microframes; ignored for bulk |
||
286 | * @sg: scatterlist entries |
||
287 | * @nents: how many entries in the scatterlist |
||
288 | * @length: how many bytes to send from the scatterlist, or zero to |
||
289 | * send every byte identified in the list. |
||
290 | * @mem_flags: SLAB_* flags affecting memory allocations in this call |
||
291 | * |
||
292 | * Returns zero for success, else a negative errno value. This initializes a |
||
293 | * scatter/gather request, allocating resources such as I/O mappings and urb |
||
294 | * memory (except maybe memory used by USB controller drivers). |
||
295 | * |
||
296 | * The request must be issued using usb_sg_wait(), which waits for the I/O to |
||
297 | * complete (or to be canceled) and then cleans up all resources allocated by |
||
298 | * usb_sg_init(). |
||
299 | * |
||
300 | * The request may be canceled with usb_sg_cancel(), either before or after |
||
301 | * usb_sg_wait() is called. |
||
302 | */ |
||
303 | int usb_sg_init ( |
||
304 | struct usb_sg_request *io, |
||
305 | struct usb_device *dev, |
||
306 | unsigned pipe, |
||
307 | unsigned period, |
||
308 | struct scatterlist *sg, |
||
309 | int nents, |
||
310 | size_t length, |
||
311 | int mem_flags |
||
312 | ) |
||
313 | { |
||
314 | int i; |
||
315 | int urb_flags; |
||
316 | int dma; |
||
317 | |||
318 | if (!io || !dev || !sg |
||
319 | || usb_pipecontrol (pipe) |
||
320 | || usb_pipeisoc (pipe) |
||
321 | || nents <= 0) |
||
322 | return -EINVAL; |
||
323 | |||
324 | spin_lock_init (&io->lock); |
||
325 | io->dev = dev; |
||
326 | io->pipe = pipe; |
||
327 | io->sg = sg; |
||
328 | io->nents = nents; |
||
329 | |||
330 | /* not all host controllers use DMA (like the mainstream pci ones); |
||
331 | * they can use PIO (sl811) or be software over another transport. |
||
332 | */ |
||
333 | dma = (dev->dev.dma_mask != 0); |
||
334 | if (dma) |
||
335 | io->entries = usb_buffer_map_sg (dev, pipe, sg, nents); |
||
336 | else |
||
337 | io->entries = nents; |
||
338 | |||
339 | /* initialize all the urbs we'll use */ |
||
340 | if (io->entries <= 0) |
||
341 | return io->entries; |
||
342 | |||
343 | io->count = 0; |
||
344 | io->urbs = kmalloc (io->entries * sizeof *io->urbs, mem_flags); |
||
345 | if (!io->urbs) |
||
346 | goto nomem; |
||
347 | |||
348 | urb_flags = URB_ASYNC_UNLINK | URB_NO_TRANSFER_DMA_MAP |
||
349 | | URB_NO_INTERRUPT; |
||
350 | if (usb_pipein (pipe)) |
||
351 | urb_flags |= URB_SHORT_NOT_OK; |
||
352 | |||
353 | for (i = 0; i < io->entries; i++, io->count = i) { |
||
354 | unsigned len; |
||
355 | |||
356 | io->urbs [i] = usb_alloc_urb (0, mem_flags); |
||
357 | if (!io->urbs [i]) { |
||
358 | io->entries = i; |
||
359 | goto nomem; |
||
360 | } |
||
361 | |||
362 | io->urbs [i]->dev = 0; |
||
363 | io->urbs [i]->pipe = pipe; |
||
364 | io->urbs [i]->interval = period; |
||
365 | io->urbs [i]->transfer_flags = urb_flags; |
||
366 | |||
367 | io->urbs [i]->complete = sg_complete; |
||
368 | io->urbs [i]->context = io; |
||
369 | io->urbs [i]->status = -EINPROGRESS; |
||
370 | io->urbs [i]->actual_length = 0; |
||
371 | |||
372 | if (dma) { |
||
373 | /* hc may use _only_ transfer_dma */ |
||
374 | io->urbs [i]->transfer_dma = sg_dma_address (sg + i); |
||
375 | len = sg_dma_len (sg + i); |
||
376 | } else { |
||
377 | /* hc may use _only_ transfer_buffer */ |
||
378 | io->urbs [i]->transfer_buffer = |
||
379 | page_address (sg [i].page) + sg [i].offset; |
||
380 | len = sg [i].length; |
||
381 | } |
||
382 | |||
383 | if (length) { |
||
384 | len = min_t (unsigned, len, length); |
||
385 | length -= len; |
||
386 | if (length == 0) |
||
387 | io->entries = i + 1; |
||
388 | } |
||
389 | io->urbs [i]->transfer_buffer_length = len; |
||
390 | } |
||
391 | io->urbs [--i]->transfer_flags &= ~URB_NO_INTERRUPT; |
||
392 | |||
393 | /* transaction state */ |
||
394 | io->status = 0; |
||
395 | io->bytes = 0; |
||
396 | init_completion (&io->complete); |
||
397 | return 0; |
||
398 | |||
399 | nomem: |
||
400 | sg_clean (io); |
||
401 | return -ENOMEM; |
||
402 | } |
||
403 | |||
404 | |||
405 | /** |
||
406 | * usb_sg_wait - synchronously execute scatter/gather request |
||
407 | * @io: request block handle, as initialized with usb_sg_init(). |
||
408 | * some fields become accessible when this call returns. |
||
409 | * Context: !in_interrupt () |
||
410 | * |
||
411 | * This function blocks until the specified I/O operation completes. It |
||
412 | * leverages the grouping of the related I/O requests to get good transfer |
||
413 | * rates, by queueing the requests. At higher speeds, such queuing can |
||
414 | * significantly improve USB throughput. |
||
415 | * |
||
416 | * There are three kinds of completion for this function. |
||
417 | * (1) success, where io->status is zero. The number of io->bytes |
||
418 | * transferred is as requested. |
||
419 | * (2) error, where io->status is a negative errno value. The number |
||
420 | * of io->bytes transferred before the error is usually less |
||
421 | * than requested, and can be nonzero. |
||
422 | * (3) cancelation, a type of error with status -ECONNRESET that |
||
423 | * is initiated by usb_sg_cancel(). |
||
424 | * |
||
425 | * When this function returns, all memory allocated through usb_sg_init() or |
||
426 | * this call will have been freed. The request block parameter may still be |
||
427 | * passed to usb_sg_cancel(), or it may be freed. It could also be |
||
428 | * reinitialized and then reused. |
||
429 | * |
||
430 | * Data Transfer Rates: |
||
431 | * |
||
432 | * Bulk transfers are valid for full or high speed endpoints. |
||
433 | * The best full speed data rate is 19 packets of 64 bytes each |
||
434 | * per frame, or 1216 bytes per millisecond. |
||
435 | * The best high speed data rate is 13 packets of 512 bytes each |
||
436 | * per microframe, or 52 KBytes per millisecond. |
||
437 | * |
||
438 | * The reason to use interrupt transfers through this API would most likely |
||
439 | * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond |
||
440 | * could be transferred. That capability is less useful for low or full |
||
441 | * speed interrupt endpoints, which allow at most one packet per millisecond, |
||
442 | * of at most 8 or 64 bytes (respectively). |
||
443 | */ |
||
444 | void usb_sg_wait (struct usb_sg_request *io) |
||
445 | { |
||
446 | int i; |
||
447 | unsigned long flags; |
||
448 | |||
449 | /* queue the urbs. */ |
||
450 | spin_lock_irqsave (&io->lock, flags); |
||
451 | for (i = 0; i < io->entries && !io->status; i++) { |
||
452 | int retval; |
||
453 | |||
454 | io->urbs [i]->dev = io->dev; |
||
455 | retval = usb_submit_urb (io->urbs [i], SLAB_ATOMIC); |
||
456 | |||
457 | /* after we submit, let completions or cancelations fire; |
||
458 | * we handshake using io->status. |
||
459 | */ |
||
460 | spin_unlock_irqrestore (&io->lock, flags); |
||
461 | switch (retval) { |
||
462 | /* maybe we retrying will recover */ |
||
463 | case -ENXIO: // hc didn't queue this one |
||
464 | case -EAGAIN: |
||
465 | case -ENOMEM: |
||
466 | io->urbs [i]->dev = 0; |
||
467 | retval = 0; |
||
468 | i--; |
||
469 | yield (); |
||
470 | break; |
||
471 | |||
472 | /* no error? continue immediately. |
||
473 | * |
||
474 | * NOTE: to work better with UHCI (4K I/O buffer may |
||
475 | * need 3K of TDs) it may be good to limit how many |
||
476 | * URBs are queued at once; N milliseconds? |
||
477 | */ |
||
478 | case 0: |
||
479 | cpu_relax (); |
||
480 | break; |
||
481 | |||
482 | /* fail any uncompleted urbs */ |
||
483 | default: |
||
484 | io->urbs [i]->dev = 0; |
||
485 | io->urbs [i]->status = retval; |
||
486 | dev_dbg (&io->dev->dev, "%s, submit --> %d\n", |
||
487 | __FUNCTION__, retval); |
||
488 | usb_sg_cancel (io); |
||
489 | } |
||
490 | spin_lock_irqsave (&io->lock, flags); |
||
491 | if (retval && io->status == -ECONNRESET) |
||
492 | io->status = retval; |
||
493 | } |
||
494 | spin_unlock_irqrestore (&io->lock, flags); |
||
495 | |||
496 | /* OK, yes, this could be packaged as non-blocking. |
||
497 | * So could the submit loop above ... but it's easier to |
||
498 | * solve neither problem than to solve both! |
||
499 | */ |
||
500 | wait_for_completion (&io->complete); |
||
501 | |||
502 | sg_clean (io); |
||
503 | } |
||
504 | |||
505 | /** |
||
506 | * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait() |
||
507 | * @io: request block, initialized with usb_sg_init() |
||
508 | * |
||
509 | * This stops a request after it has been started by usb_sg_wait(). |
||
510 | * It can also prevents one initialized by usb_sg_init() from starting, |
||
511 | * so that call just frees resources allocated to the request. |
||
512 | */ |
||
513 | void usb_sg_cancel (struct usb_sg_request *io) |
||
514 | { |
||
515 | unsigned long flags; |
||
516 | |||
517 | spin_lock_irqsave (&io->lock, flags); |
||
518 | |||
519 | /* shut everything down, if it didn't already */ |
||
520 | if (!io->status) { |
||
521 | int i; |
||
522 | |||
523 | io->status = -ECONNRESET; |
||
524 | for (i = 0; i < io->entries; i++) { |
||
525 | int retval; |
||
526 | |||
527 | if (!io->urbs [i]->dev) |
||
528 | continue; |
||
529 | retval = usb_unlink_urb (io->urbs [i]); |
||
530 | if (retval != -EINPROGRESS && retval != -EBUSY) |
||
531 | dev_warn (&io->dev->dev, "%s, unlink --> %d\n", |
||
532 | __FUNCTION__, retval); |
||
533 | } |
||
534 | } |
||
535 | spin_unlock_irqrestore (&io->lock, flags); |
||
536 | } |
||
537 | |||
538 | /*-------------------------------------------------------------------*/ |
||
539 | |||
540 | /** |
||
541 | * usb_get_descriptor - issues a generic GET_DESCRIPTOR request |
||
542 | * @dev: the device whose descriptor is being retrieved |
||
543 | * @type: the descriptor type (USB_DT_*) |
||
544 | * @index: the number of the descriptor |
||
545 | * @buf: where to put the descriptor |
||
546 | * @size: how big is "buf"? |
||
547 | * Context: !in_interrupt () |
||
548 | * |
||
549 | * Gets a USB descriptor. Convenience functions exist to simplify |
||
550 | * getting some types of descriptors. Use |
||
551 | * usb_get_device_descriptor() for USB_DT_DEVICE, |
||
552 | * and usb_get_string() or usb_string() for USB_DT_STRING. |
||
553 | * Configuration descriptors (USB_DT_CONFIG) are part of the device |
||
554 | * structure, at least for the current configuration. |
||
555 | * In addition to a number of USB-standard descriptors, some |
||
556 | * devices also use class-specific or vendor-specific descriptors. |
||
557 | * |
||
558 | * This call is synchronous, and may not be used in an interrupt context. |
||
559 | * |
||
560 | * Returns the number of bytes received on success, or else the status code |
||
561 | * returned by the underlying usb_control_msg() call. |
||
562 | */ |
||
563 | int usb_get_descriptor(struct usb_device *dev, unsigned char type, unsigned char index, void *buf, int size) |
||
564 | { |
||
565 | int i = 5; |
||
566 | int result = 0; |
||
567 | |||
568 | memset(buf,0,size); // Make sure we parse really received data |
||
569 | |||
570 | while (i--) { |
||
571 | /* retries if the returned length was 0; flakey device */ |
||
572 | if ((result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
||
573 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, |
||
574 | (type << 8) + index, 0, buf, size, |
||
575 | HZ * USB_CTRL_GET_TIMEOUT)) > 0 |
||
576 | || result == -EPIPE) |
||
577 | break; |
||
578 | } |
||
579 | return result; |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * usb_get_string - gets a string descriptor |
||
584 | * @dev: the device whose string descriptor is being retrieved |
||
585 | * @langid: code for language chosen (from string descriptor zero) |
||
586 | * @index: the number of the descriptor |
||
587 | * @buf: where to put the string |
||
588 | * @size: how big is "buf"? |
||
589 | * Context: !in_interrupt () |
||
590 | * |
||
591 | * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character, |
||
592 | * in little-endian byte order). |
||
593 | * The usb_string() function will often be a convenient way to turn |
||
594 | * these strings into kernel-printable form. |
||
595 | * |
||
596 | * Strings may be referenced in device, configuration, interface, or other |
||
597 | * descriptors, and could also be used in vendor-specific ways. |
||
598 | * |
||
599 | * This call is synchronous, and may not be used in an interrupt context. |
||
600 | * |
||
601 | * Returns the number of bytes received on success, or else the status code |
||
602 | * returned by the underlying usb_control_msg() call. |
||
603 | */ |
||
604 | int usb_get_string(struct usb_device *dev, unsigned short langid, unsigned char index, void *buf, int size) |
||
605 | { |
||
606 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
||
607 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, |
||
608 | (USB_DT_STRING << 8) + index, langid, buf, size, |
||
609 | HZ * USB_CTRL_GET_TIMEOUT); |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * usb_get_device_descriptor - (re)reads the device descriptor |
||
614 | * @dev: the device whose device descriptor is being updated |
||
615 | * Context: !in_interrupt () |
||
616 | * |
||
617 | * Updates the copy of the device descriptor stored in the device structure, |
||
618 | * which dedicates space for this purpose. Note that several fields are |
||
619 | * converted to the host CPU's byte order: the USB version (bcdUSB), and |
||
620 | * vendors product and version fields (idVendor, idProduct, and bcdDevice). |
||
621 | * That lets device drivers compare against non-byteswapped constants. |
||
622 | * |
||
623 | * There's normally no need to use this call, although some devices |
||
624 | * will change their descriptors after events like updating firmware. |
||
625 | * |
||
626 | * This call is synchronous, and may not be used in an interrupt context. |
||
627 | * |
||
628 | * Returns the number of bytes received on success, or else the status code |
||
629 | * returned by the underlying usb_control_msg() call. |
||
630 | */ |
||
631 | int usb_get_device_descriptor(struct usb_device *dev) |
||
632 | { |
||
633 | int ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, &dev->descriptor, |
||
634 | sizeof(dev->descriptor)); |
||
635 | if (ret >= 0) { |
||
636 | le16_to_cpus(&dev->descriptor.bcdUSB); |
||
637 | le16_to_cpus(&dev->descriptor.idVendor); |
||
638 | le16_to_cpus(&dev->descriptor.idProduct); |
||
639 | le16_to_cpus(&dev->descriptor.bcdDevice); |
||
640 | } |
||
641 | return ret; |
||
642 | } |
||
643 | |||
644 | /** |
||
645 | * usb_get_status - issues a GET_STATUS call |
||
646 | * @dev: the device whose status is being checked |
||
647 | * @type: USB_RECIP_*; for device, interface, or endpoint |
||
648 | * @target: zero (for device), else interface or endpoint number |
||
649 | * @data: pointer to two bytes of bitmap data |
||
650 | * Context: !in_interrupt () |
||
651 | * |
||
652 | * Returns device, interface, or endpoint status. Normally only of |
||
653 | * interest to see if the device is self powered, or has enabled the |
||
654 | * remote wakeup facility; or whether a bulk or interrupt endpoint |
||
655 | * is halted ("stalled"). |
||
656 | * |
||
657 | * Bits in these status bitmaps are set using the SET_FEATURE request, |
||
658 | * and cleared using the CLEAR_FEATURE request. The usb_clear_halt() |
||
659 | * function should be used to clear halt ("stall") status. |
||
660 | * |
||
661 | * This call is synchronous, and may not be used in an interrupt context. |
||
662 | * |
||
663 | * Returns the number of bytes received on success, or else the status code |
||
664 | * returned by the underlying usb_control_msg() call. |
||
665 | */ |
||
666 | int usb_get_status(struct usb_device *dev, int type, int target, void *data) |
||
667 | { |
||
668 | return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
||
669 | USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, data, 2, |
||
670 | HZ * USB_CTRL_GET_TIMEOUT); |
||
671 | } |
||
672 | |||
673 | /** |
||
674 | * usb_clear_halt - tells device to clear endpoint halt/stall condition |
||
675 | * @dev: device whose endpoint is halted |
||
676 | * @pipe: endpoint "pipe" being cleared |
||
677 | * Context: !in_interrupt () |
||
678 | * |
||
679 | * This is used to clear halt conditions for bulk and interrupt endpoints, |
||
680 | * as reported by URB completion status. Endpoints that are halted are |
||
681 | * sometimes referred to as being "stalled". Such endpoints are unable |
||
682 | * to transmit or receive data until the halt status is cleared. Any URBs |
||
683 | * queued for such an endpoint should normally be unlinked by the driver |
||
684 | * before clearing the halt condition, as described in sections 5.7.5 |
||
685 | * and 5.8.5 of the USB 2.0 spec. |
||
686 | * |
||
687 | * Note that control and isochronous endpoints don't halt, although control |
||
688 | * endpoints report "protocol stall" (for unsupported requests) using the |
||
689 | * same status code used to report a true stall. |
||
690 | * |
||
691 | * This call is synchronous, and may not be used in an interrupt context. |
||
692 | * |
||
693 | * Returns zero on success, or else the status code returned by the |
||
694 | * underlying usb_control_msg() call. |
||
695 | */ |
||
696 | int usb_clear_halt(struct usb_device *dev, int pipe) |
||
697 | { |
||
698 | int result; |
||
699 | int endp = usb_pipeendpoint(pipe); |
||
700 | |||
701 | if (usb_pipein (pipe)) |
||
702 | endp |= USB_DIR_IN; |
||
703 | |||
704 | /* we don't care if it wasn't halted first. in fact some devices |
||
705 | * (like some ibmcam model 1 units) seem to expect hosts to make |
||
706 | * this request for iso endpoints, which can't halt! |
||
707 | */ |
||
708 | result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
||
709 | USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0, endp, NULL, 0, |
||
710 | HZ * USB_CTRL_SET_TIMEOUT); |
||
711 | |||
712 | /* don't un-halt or force to DATA0 except on success */ |
||
713 | if (result < 0) |
||
714 | return result; |
||
715 | |||
716 | /* NOTE: seems like Microsoft and Apple don't bother verifying |
||
717 | * the clear "took", so some devices could lock up if you check... |
||
718 | * such as the Hagiwara FlashGate DUAL. So we won't bother. |
||
719 | * |
||
720 | * NOTE: make sure the logic here doesn't diverge much from |
||
721 | * the copy in usb-storage, for as long as we need two copies. |
||
722 | */ |
||
723 | |||
724 | /* toggle was reset by the clear, then ep was reactivated */ |
||
725 | usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0); |
||
726 | usb_endpoint_running(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)); |
||
727 | |||
728 | return 0; |
||
729 | } |
||
730 | |||
731 | /** |
||
732 | * usb_disable_endpoint -- Disable an endpoint by address |
||
733 | * @dev: the device whose endpoint is being disabled |
||
734 | * @epaddr: the endpoint's address. Endpoint number for output, |
||
735 | * endpoint number + USB_DIR_IN for input |
||
736 | * |
||
737 | * Deallocates hcd/hardware state for this endpoint ... and nukes all |
||
738 | * pending urbs. |
||
739 | * |
||
740 | * If the HCD hasn't registered a disable() function, this marks the |
||
741 | * endpoint as halted and sets its maxpacket size to 0 to prevent |
||
742 | * further submissions. |
||
743 | */ |
||
744 | void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr) |
||
745 | { |
||
746 | if (dev && dev->bus && dev->bus->op && dev->bus->op->disable) |
||
747 | dev->bus->op->disable(dev, epaddr); |
||
748 | else { |
||
749 | unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK; |
||
750 | |||
751 | if (usb_endpoint_out(epaddr)) { |
||
752 | usb_endpoint_halt(dev, epnum, 1); |
||
753 | dev->epmaxpacketout[epnum] = 0; |
||
754 | } else { |
||
755 | usb_endpoint_halt(dev, epnum, 0); |
||
756 | dev->epmaxpacketin[epnum] = 0; |
||
757 | } |
||
758 | } |
||
759 | } |
||
760 | |||
761 | /** |
||
762 | * usb_disable_interface -- Disable all endpoints for an interface |
||
763 | * @dev: the device whose interface is being disabled |
||
764 | * @intf: pointer to the interface descriptor |
||
765 | * |
||
766 | * Disables all the endpoints for the interface's current altsetting. |
||
767 | */ |
||
768 | void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf) |
||
769 | { |
||
770 | struct usb_host_interface *hintf = |
||
771 | &intf->altsetting[intf->act_altsetting]; |
||
772 | int i; |
||
773 | |||
774 | for (i = 0; i < hintf->desc.bNumEndpoints; ++i) { |
||
775 | usb_disable_endpoint(dev, |
||
776 | hintf->endpoint[i].desc.bEndpointAddress); |
||
777 | } |
||
778 | } |
||
779 | |||
780 | /* |
||
781 | * usb_disable_device - Disable all the endpoints for a USB device |
||
782 | * @dev: the device whose endpoints are being disabled |
||
783 | * @skip_ep0: 0 to disable endpoint 0, 1 to skip it. |
||
784 | * |
||
785 | * Disables all the device's endpoints, potentially including endpoint 0. |
||
786 | * Deallocates hcd/hardware state for the endpoints (nuking all or most |
||
787 | * pending urbs) and usbcore state for the interfaces, so that usbcore |
||
788 | * must usb_set_configuration() before any interfaces could be used. |
||
789 | */ |
||
790 | void usb_disable_device(struct usb_device *dev, int skip_ep0) |
||
791 | { |
||
792 | int i; |
||
793 | |||
794 | dev_dbg(&dev->dev, "%s nuking %s URBs\n", __FUNCTION__, |
||
795 | skip_ep0 ? "non-ep0" : "all"); |
||
796 | for (i = skip_ep0; i < 16; ++i) { |
||
797 | usb_disable_endpoint(dev, i); |
||
798 | usb_disable_endpoint(dev, i + USB_DIR_IN); |
||
799 | } |
||
800 | dev->toggle[0] = dev->toggle[1] = 0; |
||
801 | dev->halted[0] = dev->halted[1] = 0; |
||
802 | |||
803 | /* getting rid of interfaces will disconnect |
||
804 | * any drivers bound to them (a key side effect) |
||
805 | */ |
||
806 | if (dev->actconfig) { |
||
807 | for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { |
||
808 | struct usb_interface *interface; |
||
809 | |||
810 | /* remove this interface */ |
||
811 | interface = dev->actconfig->interface[i]; |
||
812 | dev_dbg (&dev->dev, "unregistering interface %s\n", |
||
813 | interface->dev.bus_id); |
||
814 | device_del(&interface->dev); |
||
815 | } |
||
816 | dev->actconfig = 0; |
||
817 | if (dev->state == USB_STATE_CONFIGURED) |
||
818 | dev->state = USB_STATE_ADDRESS; |
||
819 | } |
||
820 | } |
||
821 | |||
822 | |||
823 | /* |
||
824 | * usb_enable_endpoint - Enable an endpoint for USB communications |
||
825 | * @dev: the device whose interface is being enabled |
||
826 | * @epd: pointer to the endpoint descriptor |
||
827 | * |
||
828 | * Marks the endpoint as running, resets its toggle, and stores |
||
829 | * its maxpacket value. For control endpoints, both the input |
||
830 | * and output sides are handled. |
||
831 | */ |
||
832 | void usb_enable_endpoint(struct usb_device *dev, |
||
833 | struct usb_endpoint_descriptor *epd) |
||
834 | { |
||
835 | int maxsize = epd->wMaxPacketSize; |
||
836 | unsigned int epaddr = epd->bEndpointAddress; |
||
837 | unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK; |
||
838 | int is_control = ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == |
||
839 | USB_ENDPOINT_XFER_CONTROL); |
||
840 | |||
841 | if (usb_endpoint_out(epaddr) || is_control) { |
||
842 | usb_endpoint_running(dev, epnum, 1); |
||
843 | usb_settoggle(dev, epnum, 1, 0); |
||
844 | dev->epmaxpacketout[epnum] = maxsize; |
||
845 | } |
||
846 | if (!usb_endpoint_out(epaddr) || is_control) { |
||
847 | usb_endpoint_running(dev, epnum, 0); |
||
848 | usb_settoggle(dev, epnum, 0, 0); |
||
849 | dev->epmaxpacketin[epnum] = maxsize; |
||
850 | } |
||
851 | } |
||
852 | |||
853 | /* |
||
854 | * usb_enable_interface - Enable all the endpoints for an interface |
||
855 | * @dev: the device whose interface is being enabled |
||
856 | * @intf: pointer to the interface descriptor |
||
857 | * |
||
858 | * Enables all the endpoints for the interface's current altsetting. |
||
859 | */ |
||
860 | void usb_enable_interface(struct usb_device *dev, |
||
861 | struct usb_interface *intf) |
||
862 | { |
||
863 | struct usb_host_interface *hintf = |
||
864 | &intf->altsetting[intf->act_altsetting]; |
||
865 | int i; |
||
866 | |||
867 | for (i = 0; i < hintf->desc.bNumEndpoints; ++i) |
||
868 | usb_enable_endpoint(dev, &hintf->endpoint[i].desc); |
||
869 | } |
||
870 | |||
871 | /** |
||
872 | * usb_set_interface - Makes a particular alternate setting be current |
||
873 | * @dev: the device whose interface is being updated |
||
874 | * @interface: the interface being updated |
||
875 | * @alternate: the setting being chosen. |
||
876 | * Context: !in_interrupt () |
||
877 | * |
||
878 | * This is used to enable data transfers on interfaces that may not |
||
879 | * be enabled by default. Not all devices support such configurability. |
||
880 | * Only the driver bound to an interface may change its setting. |
||
881 | * |
||
882 | * Within any given configuration, each interface may have several |
||
883 | * alternative settings. These are often used to control levels of |
||
884 | * bandwidth consumption. For example, the default setting for a high |
||
885 | * speed interrupt endpoint may not send more than 64 bytes per microframe, |
||
886 | * while interrupt transfers of up to 3KBytes per microframe are legal. |
||
887 | * Also, isochronous endpoints may never be part of an |
||
888 | * interface's default setting. To access such bandwidth, alternate |
||
889 | * interface settings must be made current. |
||
890 | * |
||
891 | * Note that in the Linux USB subsystem, bandwidth associated with |
||
892 | * an endpoint in a given alternate setting is not reserved until an URB |
||
893 | * is submitted that needs that bandwidth. Some other operating systems |
||
894 | * allocate bandwidth early, when a configuration is chosen. |
||
895 | * |
||
896 | * This call is synchronous, and may not be used in an interrupt context. |
||
897 | * Also, drivers must not change altsettings while urbs are scheduled for |
||
898 | * endpoints in that interface; all such urbs must first be completed |
||
899 | * (perhaps forced by unlinking). |
||
900 | * |
||
901 | * Returns zero on success, or else the status code returned by the |
||
902 | * underlying usb_control_msg() call. |
||
903 | */ |
||
904 | int usb_set_interface(struct usb_device *dev, int interface, int alternate) |
||
905 | { |
||
906 | struct usb_interface *iface; |
||
907 | int ret; |
||
908 | int manual = 0; |
||
909 | |||
910 | iface = usb_ifnum_to_if(dev, interface); |
||
911 | if (!iface) { |
||
912 | warn("selecting invalid interface %d", interface); |
||
913 | return -EINVAL; |
||
914 | } |
||
915 | |||
916 | if (alternate < 0 || alternate >= iface->num_altsetting) |
||
917 | return -EINVAL; |
||
918 | |||
919 | ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
||
920 | USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE, |
||
921 | iface->altsetting[alternate] |
||
922 | .desc.bAlternateSetting, |
||
923 | interface, NULL, 0, HZ * 5); |
||
924 | |||
925 | /* 9.4.10 says devices don't need this and are free to STALL the |
||
926 | * request if the interface only has one alternate setting. |
||
927 | */ |
||
928 | if (ret == -EPIPE && iface->num_altsetting == 1) { |
||
929 | dbg("manual set_interface for dev %d, iface %d, alt %d", |
||
930 | dev->devnum, interface, alternate); |
||
931 | manual = 1; |
||
932 | } else if (ret < 0) |
||
933 | return ret; |
||
934 | |||
935 | /* FIXME drivers shouldn't need to replicate/bugfix the logic here |
||
936 | * when they implement async or easily-killable versions of this or |
||
937 | * other "should-be-internal" functions (like clear_halt). |
||
938 | * should hcd+usbcore postprocess control requests? |
||
939 | */ |
||
940 | |||
941 | /* prevent submissions using previous endpoint settings */ |
||
942 | usb_disable_interface(dev, iface); |
||
943 | |||
944 | iface->act_altsetting = alternate; |
||
945 | |||
946 | /* If the interface only has one altsetting and the device didn't |
||
947 | * accept the request, we attempt to carry out the equivalent action |
||
948 | * by manually clearing the HALT feature for each endpoint in the |
||
949 | * new altsetting. |
||
950 | */ |
||
951 | if (manual) { |
||
952 | struct usb_host_interface *iface_as = |
||
953 | &iface->altsetting[alternate]; |
||
954 | int i; |
||
955 | |||
956 | for (i = 0; i < iface_as->desc.bNumEndpoints; i++) { |
||
957 | unsigned int epaddr = |
||
958 | iface_as->endpoint[i].desc.bEndpointAddress; |
||
959 | unsigned int pipe = |
||
960 | __create_pipe(dev, USB_ENDPOINT_NUMBER_MASK & epaddr) |
||
961 | | (usb_endpoint_out(epaddr) ? USB_DIR_OUT : USB_DIR_IN); |
||
962 | |||
963 | usb_clear_halt(dev, pipe); |
||
964 | } |
||
965 | } |
||
966 | |||
967 | /* 9.1.1.5: reset toggles for all endpoints in the new altsetting |
||
968 | * |
||
969 | * Note: |
||
970 | * Despite EP0 is always present in all interfaces/AS, the list of |
||
971 | * endpoints from the descriptor does not contain EP0. Due to its |
||
972 | * omnipresence one might expect EP0 being considered "affected" by |
||
973 | * any SetInterface request and hence assume toggles need to be reset. |
||
974 | * However, EP0 toggles are re-synced for every individual transfer |
||
975 | * during the SETUP stage - hence EP0 toggles are "don't care" here. |
||
976 | * (Likewise, EP0 never "halts" on well designed devices.) |
||
977 | */ |
||
978 | usb_enable_interface(dev, iface); |
||
979 | |||
980 | return 0; |
||
981 | } |
||
982 | |||
983 | /** |
||
984 | * usb_reset_configuration - lightweight device reset |
||
985 | * @dev: the device whose configuration is being reset |
||
986 | * |
||
987 | * This issues a standard SET_CONFIGURATION request to the device using |
||
988 | * the current configuration. The effect is to reset most USB-related |
||
989 | * state in the device, including interface altsettings (reset to zero), |
||
990 | * endpoint halts (cleared), and data toggle (only for bulk and interrupt |
||
991 | * endpoints). Other usbcore state is unchanged, including bindings of |
||
992 | * usb device drivers to interfaces. |
||
993 | * |
||
994 | * Because this affects multiple interfaces, avoid using this with composite |
||
995 | * (multi-interface) devices. Instead, the driver for each interface may |
||
996 | * use usb_set_interface() on the interfaces it claims. Resetting the whole |
||
997 | * configuration would affect other drivers' interfaces. |
||
998 | * |
||
999 | * Returns zero on success, else a negative error code. |
||
1000 | */ |
||
1001 | int usb_reset_configuration(struct usb_device *dev) |
||
1002 | { |
||
1003 | int i, retval; |
||
1004 | struct usb_host_config *config; |
||
1005 | |||
1006 | /* caller must own dev->serialize (config won't change) |
||
1007 | * and the usb bus readlock (so driver bindings are stable); |
||
1008 | * so calls during probe() are fine |
||
1009 | */ |
||
1010 | |||
1011 | for (i = 1; i < 16; ++i) { |
||
1012 | usb_disable_endpoint(dev, i); |
||
1013 | usb_disable_endpoint(dev, i + USB_DIR_IN); |
||
1014 | } |
||
1015 | |||
1016 | config = dev->actconfig; |
||
1017 | retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
||
1018 | USB_REQ_SET_CONFIGURATION, 0, |
||
1019 | config->desc.bConfigurationValue, 0, |
||
1020 | NULL, 0, HZ * USB_CTRL_SET_TIMEOUT); |
||
1021 | if (retval < 0) { |
||
1022 | dev->state = USB_STATE_ADDRESS; |
||
1023 | return retval; |
||
1024 | } |
||
1025 | |||
1026 | dev->toggle[0] = dev->toggle[1] = 0; |
||
1027 | dev->halted[0] = dev->halted[1] = 0; |
||
1028 | |||
1029 | /* re-init hc/hcd interface/endpoint state */ |
||
1030 | for (i = 0; i < config->desc.bNumInterfaces; i++) { |
||
1031 | struct usb_interface *intf = config->interface[i]; |
||
1032 | |||
1033 | intf->act_altsetting = 0; |
||
1034 | usb_enable_interface(dev, intf); |
||
1035 | } |
||
1036 | return 0; |
||
1037 | } |
||
1038 | |||
1039 | /** |
||
1040 | * usb_set_configuration - Makes a particular device setting be current |
||
1041 | * @dev: the device whose configuration is being updated |
||
1042 | * @configuration: the configuration being chosen. |
||
1043 | * Context: !in_interrupt () |
||
1044 | * |
||
1045 | * This is used to enable non-default device modes. Not all devices |
||
1046 | * use this kind of configurability; many devices only have one |
||
1047 | * configuration. |
||
1048 | * |
||
1049 | * USB device configurations may affect Linux interoperability, |
||
1050 | * power consumption and the functionality available. For example, |
||
1051 | * the default configuration is limited to using 100mA of bus power, |
||
1052 | * so that when certain device functionality requires more power, |
||
1053 | * and the device is bus powered, that functionality should be in some |
||
1054 | * non-default device configuration. Other device modes may also be |
||
1055 | * reflected as configuration options, such as whether two ISDN |
||
1056 | * channels are available independently; and choosing between open |
||
1057 | * standard device protocols (like CDC) or proprietary ones. |
||
1058 | * |
||
1059 | * Note that USB has an additional level of device configurability, |
||
1060 | * associated with interfaces. That configurability is accessed using |
||
1061 | * usb_set_interface(). |
||
1062 | * |
||
1063 | * This call is synchronous. The calling context must be able to sleep, |
||
1064 | * and must not hold the driver model lock for USB; usb device driver |
||
1065 | * probe() methods may not use this routine. |
||
1066 | * |
||
1067 | * Returns zero on success, or else the status code returned by the |
||
1068 | * underlying call that failed. On succesful completion, each interface |
||
1069 | * in the original device configuration has been destroyed, and each one |
||
1070 | * in the new configuration has been probed by all relevant usb device |
||
1071 | * drivers currently known to the kernel. |
||
1072 | */ |
||
1073 | int usb_set_configuration(struct usb_device *dev, int configuration) |
||
1074 | { |
||
1075 | int i, ret; |
||
1076 | struct usb_host_config *cp = NULL; |
||
1077 | |||
1078 | /* dev->serialize guards all config changes */ |
||
1079 | down(&dev->serialize); |
||
1080 | |||
1081 | for (i=0; i<dev->descriptor.bNumConfigurations; i++) { |
||
1082 | if (dev->config[i].desc.bConfigurationValue == configuration) { |
||
1083 | cp = &dev->config[i]; |
||
1084 | break; |
||
1085 | } |
||
1086 | } |
||
1087 | if ((!cp && configuration != 0)) { |
||
1088 | ret = -EINVAL; |
||
1089 | goto out; |
||
1090 | } |
||
1091 | if (cp && configuration == 0) |
||
1092 | dev_warn(&dev->dev, "config 0 descriptor??\n"); |
||
1093 | |||
1094 | /* if it's already configured, clear out old state first. |
||
1095 | * getting rid of old interfaces means unbinding their drivers. |
||
1096 | */ |
||
1097 | if (dev->state != USB_STATE_ADDRESS) |
||
1098 | usb_disable_device (dev, 1); // Skip ep0 |
||
1099 | |||
1100 | if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
||
1101 | USB_REQ_SET_CONFIGURATION, 0, configuration, 0, |
||
1102 | NULL, 0, HZ * USB_CTRL_SET_TIMEOUT)) < 0) |
||
1103 | goto out; |
||
1104 | |||
1105 | dev->actconfig = cp; |
||
1049 | mauro | 1106 | //** if (!configuration) 2.6.1 |
1107 | if (!cp) |
||
846 | giacomo | 1108 | dev->state = USB_STATE_ADDRESS; |
1109 | else { |
||
1110 | dev->state = USB_STATE_CONFIGURED; |
||
1111 | |||
1112 | /* re-initialize hc/hcd/usbcore interface/endpoint state. |
||
1113 | * this triggers binding of drivers to interfaces; and |
||
1114 | * maybe probe() calls will choose different altsettings. |
||
1115 | */ |
||
1116 | for (i = 0; i < cp->desc.bNumInterfaces; ++i) { |
||
1117 | struct usb_interface *intf = cp->interface[i]; |
||
1118 | struct usb_interface_descriptor *desc; |
||
1119 | |||
1120 | intf->act_altsetting = 0; |
||
1121 | desc = &intf->altsetting [0].desc; |
||
1122 | usb_enable_interface(dev, intf); |
||
1123 | |||
1124 | intf->dev.parent = &dev->dev; |
||
1125 | intf->dev.driver = NULL; |
||
1126 | intf->dev.bus = &usb_bus_type; |
||
1127 | intf->dev.dma_mask = dev->dev.dma_mask; |
||
1128 | sprintf26 (&intf->dev.bus_id[0], "%d-%s:%d.%d", |
||
1129 | dev->bus->busnum, dev->devpath, |
||
1130 | configuration, |
||
1131 | desc->bInterfaceNumber); |
||
1132 | dev_dbg (&dev->dev, |
||
1133 | "registering %s (config #%d, interface %d)\n", |
||
1134 | intf->dev.bus_id, configuration, |
||
1135 | desc->bInterfaceNumber); |
||
1136 | device_add (&intf->dev); |
||
1137 | usb_create_driverfs_intf_files (intf); |
||
1138 | } |
||
1139 | } |
||
1140 | |||
1141 | out: |
||
1142 | up(&dev->serialize); |
||
1143 | return ret; |
||
1144 | } |
||
1145 | |||
1146 | /** |
||
1147 | * usb_string - returns ISO 8859-1 version of a string descriptor |
||
1148 | * @dev: the device whose string descriptor is being retrieved |
||
1149 | * @index: the number of the descriptor |
||
1150 | * @buf: where to put the string |
||
1151 | * @size: how big is "buf"? |
||
1152 | * Context: !in_interrupt () |
||
1153 | * |
||
1154 | * This converts the UTF-16LE encoded strings returned by devices, from |
||
1155 | * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones |
||
1156 | * that are more usable in most kernel contexts. Note that all characters |
||
1157 | * in the chosen descriptor that can't be encoded using ISO-8859-1 |
||
1158 | * are converted to the question mark ("?") character, and this function |
||
1159 | * chooses strings in the first language supported by the device. |
||
1160 | * |
||
1161 | * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit |
||
1162 | * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode, |
||
1163 | * and is appropriate for use many uses of English and several other |
||
1164 | * Western European languages. (But it doesn't include the "Euro" symbol.) |
||
1165 | * |
||
1166 | * This call is synchronous, and may not be used in an interrupt context. |
||
1167 | * |
||
1168 | * Returns length of the string (>= 0) or usb_control_msg status (< 0). |
||
1169 | */ |
||
1170 | int usb_string(struct usb_device *dev, int index, char *buf, size_t size) |
||
1171 | { |
||
1172 | unsigned char *tbuf; |
||
1173 | int err, len; |
||
1174 | unsigned int u, idx; |
||
1175 | |||
1176 | if (size <= 0 || !buf || !index) |
||
1177 | return -EINVAL; |
||
1178 | buf[0] = 0; |
||
1179 | tbuf = kmalloc(256, GFP_KERNEL); |
||
1180 | if (!tbuf) |
||
1181 | return -ENOMEM; |
||
1182 | |||
1183 | /* get langid for strings if it's not yet known */ |
||
1184 | if (!dev->have_langid) { |
||
1185 | err = usb_get_string(dev, 0, 0, tbuf, 4); |
||
1186 | if (err < 0) { |
||
1187 | err("error getting string descriptor 0 (error=%d)", err); |
||
1188 | goto errout; |
||
1189 | } else if (err < 4 || tbuf[0] < 4) { |
||
1190 | err("string descriptor 0 too short"); |
||
1191 | err = -EINVAL; |
||
1192 | goto errout; |
||
1193 | } else { |
||
1194 | dev->have_langid = -1; |
||
1195 | dev->string_langid = tbuf[2] | (tbuf[3]<< 8); |
||
1196 | /* always use the first langid listed */ |
||
1197 | dbg("USB device number %d default language ID 0x%x", |
||
1198 | dev->devnum, dev->string_langid); |
||
1199 | } |
||
1200 | } |
||
1201 | |||
1202 | /* |
||
1203 | * ask for the length of the string |
||
1204 | */ |
||
1205 | |||
1206 | err = usb_get_string(dev, dev->string_langid, index, tbuf, 2); |
||
1207 | if(err<2) |
||
1208 | goto errout; |
||
1209 | len=tbuf[0]; |
||
1210 | |||
1211 | err = usb_get_string(dev, dev->string_langid, index, tbuf, len); |
||
1212 | if (err < 0) |
||
1213 | goto errout; |
||
1214 | |||
1215 | size--; /* leave room for trailing NULL char in output buffer */ |
||
1216 | for (idx = 0, u = 2; u < err; u += 2) { |
||
1217 | if (idx >= size) |
||
1218 | break; |
||
1219 | if (tbuf[u+1]) /* high byte */ |
||
1220 | buf[idx++] = '?'; /* non ISO-8859-1 character */ |
||
1221 | else |
||
1222 | buf[idx++] = tbuf[u]; |
||
1223 | } |
||
1224 | buf[idx] = 0; |
||
1225 | err = idx; |
||
1226 | |||
1227 | errout: |
||
1228 | kfree(tbuf); |
||
1229 | return err; |
||
1230 | } |
||
1231 | |||
1232 | // synchronous request completion model |
||
1233 | EXPORT_SYMBOL(usb_control_msg); |
||
1234 | EXPORT_SYMBOL(usb_bulk_msg); |
||
1235 | |||
1236 | EXPORT_SYMBOL(usb_sg_init); |
||
1237 | EXPORT_SYMBOL(usb_sg_cancel); |
||
1238 | EXPORT_SYMBOL(usb_sg_wait); |
||
1239 | |||
1240 | // synchronous control message convenience routines |
||
1241 | EXPORT_SYMBOL(usb_get_descriptor); |
||
1242 | EXPORT_SYMBOL(usb_get_device_descriptor); |
||
1243 | EXPORT_SYMBOL(usb_get_status); |
||
1244 | EXPORT_SYMBOL(usb_get_string); |
||
1245 | EXPORT_SYMBOL(usb_string); |
||
1246 | |||
1247 | // synchronous calls that also maintain usbcore state |
||
1248 | EXPORT_SYMBOL(usb_clear_halt); |
||
1249 | EXPORT_SYMBOL(usb_reset_configuration); |
||
1250 | EXPORT_SYMBOL(usb_set_configuration); |
||
1251 | EXPORT_SYMBOL(usb_set_interface); |
||
1252 |