2 * message.c - synchronous message handling
5 #include <linux/pci.h> /* for scatterlist macros */
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <linux/init.h>
11 #include <linux/timer.h>
12 #include <linux/ctype.h>
13 #include <linux/device.h>
14 #include <linux/usb/quirks.h>
15 #include <asm/byteorder.h>
16 #include <asm/scatterlist.h>
18 #include "hcd.h" /* for usbcore internals */
21 static void usb_api_blocking_completion(struct urb
*urb
)
23 complete((struct completion
*)urb
->context
);
28 * Starts urb and waits for completion or timeout. Note that this call
29 * is NOT interruptible. Many device driver i/o requests should be
30 * interruptible and therefore these drivers should implement their
31 * own interruptible routines.
33 static int usb_start_wait_urb(struct urb
*urb
, int timeout
, int *actual_length
)
35 struct completion done
;
39 init_completion(&done
);
41 urb
->actual_length
= 0;
42 status
= usb_submit_urb(urb
, GFP_NOIO
);
46 expire
= timeout
? msecs_to_jiffies(timeout
) : MAX_SCHEDULE_TIMEOUT
;
47 if (!wait_for_completion_timeout(&done
, expire
)) {
49 dev_dbg(&urb
->dev
->dev
,
50 "%s timed out on ep%d%s len=%d/%d\n",
52 usb_pipeendpoint(urb
->pipe
),
53 usb_pipein(urb
->pipe
) ? "in" : "out",
55 urb
->transfer_buffer_length
);
58 status
= urb
->status
== -ENOENT
? -ETIMEDOUT
: urb
->status
;
63 *actual_length
= urb
->actual_length
;
69 /*-------------------------------------------------------------------*/
70 // returns status (negative) or length (positive)
71 static int usb_internal_control_msg(struct usb_device
*usb_dev
,
73 struct usb_ctrlrequest
*cmd
,
74 void *data
, int len
, int timeout
)
80 urb
= usb_alloc_urb(0, GFP_NOIO
);
84 usb_fill_control_urb(urb
, usb_dev
, pipe
, (unsigned char *)cmd
, data
,
85 len
, usb_api_blocking_completion
, NULL
);
87 retv
= usb_start_wait_urb(urb
, timeout
, &length
);
95 * usb_control_msg - Builds a control urb, sends it off and waits for completion
96 * @dev: pointer to the usb device to send the message to
97 * @pipe: endpoint "pipe" to send the message to
98 * @request: USB message request value
99 * @requesttype: USB message request type value
100 * @value: USB message value
101 * @index: USB message index value
102 * @data: pointer to the data to send
103 * @size: length in bytes of the data to send
104 * @timeout: time in msecs to wait for the message to complete before
105 * timing out (if 0 the wait is forever)
106 * Context: !in_interrupt ()
108 * This function sends a simple control message to a specified endpoint
109 * and waits for the message to complete, or timeout.
111 * If successful, it returns the number of bytes transferred, otherwise a negative error number.
113 * Don't use this function from within an interrupt context, like a
114 * bottom half handler. If you need an asynchronous message, or need to send
115 * a message from within interrupt context, use usb_submit_urb()
116 * If a thread in your driver uses this call, make sure your disconnect()
117 * method can wait for it to complete. Since you don't have a handle on
118 * the URB used, you can't cancel the request.
120 int usb_control_msg(struct usb_device
*dev
, unsigned int pipe
, __u8 request
, __u8 requesttype
,
121 __u16 value
, __u16 index
, void *data
, __u16 size
, int timeout
)
123 struct usb_ctrlrequest
*dr
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_NOIO
);
129 dr
->bRequestType
= requesttype
;
130 dr
->bRequest
= request
;
131 dr
->wValue
= cpu_to_le16p(&value
);
132 dr
->wIndex
= cpu_to_le16p(&index
);
133 dr
->wLength
= cpu_to_le16p(&size
);
135 //dbg("usb_control_msg");
137 ret
= usb_internal_control_msg(dev
, pipe
, dr
, data
, size
, timeout
);
146 * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion
147 * @usb_dev: pointer to the usb device to send the message to
148 * @pipe: endpoint "pipe" to send the message to
149 * @data: pointer to the data to send
150 * @len: length in bytes of the data to send
151 * @actual_length: pointer to a location to put the actual length transferred in bytes
152 * @timeout: time in msecs to wait for the message to complete before
153 * timing out (if 0 the wait is forever)
154 * Context: !in_interrupt ()
156 * This function sends a simple interrupt message to a specified endpoint and
157 * waits for the message to complete, or timeout.
159 * If successful, it returns 0, otherwise a negative error number. The number
160 * of actual bytes transferred will be stored in the actual_length paramater.
162 * Don't use this function from within an interrupt context, like a bottom half
163 * handler. If you need an asynchronous message, or need to send a message
164 * from within interrupt context, use usb_submit_urb() If a thread in your
165 * driver uses this call, make sure your disconnect() method can wait for it to
166 * complete. Since you don't have a handle on the URB used, you can't cancel
169 int usb_interrupt_msg(struct usb_device
*usb_dev
, unsigned int pipe
,
170 void *data
, int len
, int *actual_length
, int timeout
)
172 return usb_bulk_msg(usb_dev
, pipe
, data
, len
, actual_length
, timeout
);
174 EXPORT_SYMBOL_GPL(usb_interrupt_msg
);
177 * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
178 * @usb_dev: pointer to the usb device to send the message to
179 * @pipe: endpoint "pipe" to send the message to
180 * @data: pointer to the data to send
181 * @len: length in bytes of the data to send
182 * @actual_length: pointer to a location to put the actual length transferred in bytes
183 * @timeout: time in msecs to wait for the message to complete before
184 * timing out (if 0 the wait is forever)
185 * Context: !in_interrupt ()
187 * This function sends a simple bulk message to a specified endpoint
188 * and waits for the message to complete, or timeout.
190 * If successful, it returns 0, otherwise a negative error number.
191 * The number of actual bytes transferred will be stored in the
192 * actual_length paramater.
194 * Don't use this function from within an interrupt context, like a
195 * bottom half handler. If you need an asynchronous message, or need to
196 * send a message from within interrupt context, use usb_submit_urb()
197 * If a thread in your driver uses this call, make sure your disconnect()
198 * method can wait for it to complete. Since you don't have a handle on
199 * the URB used, you can't cancel the request.
201 * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT
202 * ioctl, users are forced to abuse this routine by using it to submit
203 * URBs for interrupt endpoints. We will take the liberty of creating
204 * an interrupt URB (with the default interval) if the target is an
205 * interrupt endpoint.
207 int usb_bulk_msg(struct usb_device
*usb_dev
, unsigned int pipe
,
208 void *data
, int len
, int *actual_length
, int timeout
)
211 struct usb_host_endpoint
*ep
;
213 ep
= (usb_pipein(pipe
) ? usb_dev
->ep_in
: usb_dev
->ep_out
)
214 [usb_pipeendpoint(pipe
)];
218 urb
= usb_alloc_urb(0, GFP_KERNEL
);
222 if ((ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) ==
223 USB_ENDPOINT_XFER_INT
) {
226 if (usb_dev
->speed
== USB_SPEED_HIGH
)
227 interval
= 1 << min(15, ep
->desc
.bInterval
- 1);
229 interval
= ep
->desc
.bInterval
;
230 pipe
= (pipe
& ~(3 << 30)) | (PIPE_INTERRUPT
<< 30);
231 usb_fill_int_urb(urb
, usb_dev
, pipe
, data
, len
,
232 usb_api_blocking_completion
, NULL
, interval
);
234 usb_fill_bulk_urb(urb
, usb_dev
, pipe
, data
, len
,
235 usb_api_blocking_completion
, NULL
);
237 return usb_start_wait_urb(urb
, timeout
, actual_length
);
240 /*-------------------------------------------------------------------*/
242 static void sg_clean (struct usb_sg_request
*io
)
245 while (io
->entries
--)
246 usb_free_urb (io
->urbs
[io
->entries
]);
250 if (io
->dev
->dev
.dma_mask
!= NULL
)
251 usb_buffer_unmap_sg (io
->dev
, io
->pipe
, io
->sg
, io
->nents
);
255 static void sg_complete (struct urb
*urb
)
257 struct usb_sg_request
*io
= urb
->context
;
259 spin_lock (&io
->lock
);
261 /* In 2.5 we require hcds' endpoint queues not to progress after fault
262 * reports, until the completion callback (this!) returns. That lets
263 * device driver code (like this routine) unlink queued urbs first,
264 * if it needs to, since the HC won't work on them at all. So it's
265 * not possible for page N+1 to overwrite page N, and so on.
267 * That's only for "hard" faults; "soft" faults (unlinks) sometimes
268 * complete before the HCD can get requests away from hardware,
269 * though never during cleanup after a hard fault.
272 && (io
->status
!= -ECONNRESET
273 || urb
->status
!= -ECONNRESET
)
274 && urb
->actual_length
) {
275 dev_err (io
->dev
->bus
->controller
,
276 "dev %s ep%d%s scatterlist error %d/%d\n",
278 usb_pipeendpoint (urb
->pipe
),
279 usb_pipein (urb
->pipe
) ? "in" : "out",
280 urb
->status
, io
->status
);
284 if (io
->status
== 0 && urb
->status
&& urb
->status
!= -ECONNRESET
) {
285 int i
, found
, status
;
287 io
->status
= urb
->status
;
289 /* the previous urbs, and this one, completed already.
290 * unlink pending urbs so they won't rx/tx bad data.
291 * careful: unlink can sometimes be synchronous...
293 spin_unlock (&io
->lock
);
294 for (i
= 0, found
= 0; i
< io
->entries
; i
++) {
295 if (!io
->urbs
[i
] || !io
->urbs
[i
]->dev
)
298 status
= usb_unlink_urb (io
->urbs
[i
]);
299 if (status
!= -EINPROGRESS
302 dev_err (&io
->dev
->dev
,
303 "%s, unlink --> %d\n",
304 __FUNCTION__
, status
);
305 } else if (urb
== io
->urbs
[i
])
308 spin_lock (&io
->lock
);
312 /* on the last completion, signal usb_sg_wait() */
313 io
->bytes
+= urb
->actual_length
;
316 complete (&io
->complete
);
318 spin_unlock (&io
->lock
);
323 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
324 * @io: request block being initialized. until usb_sg_wait() returns,
325 * treat this as a pointer to an opaque block of memory,
326 * @dev: the usb device that will send or receive the data
327 * @pipe: endpoint "pipe" used to transfer the data
328 * @period: polling rate for interrupt endpoints, in frames or
329 * (for high speed endpoints) microframes; ignored for bulk
330 * @sg: scatterlist entries
331 * @nents: how many entries in the scatterlist
332 * @length: how many bytes to send from the scatterlist, or zero to
333 * send every byte identified in the list.
334 * @mem_flags: SLAB_* flags affecting memory allocations in this call
336 * Returns zero for success, else a negative errno value. This initializes a
337 * scatter/gather request, allocating resources such as I/O mappings and urb
338 * memory (except maybe memory used by USB controller drivers).
340 * The request must be issued using usb_sg_wait(), which waits for the I/O to
341 * complete (or to be canceled) and then cleans up all resources allocated by
344 * The request may be canceled with usb_sg_cancel(), either before or after
345 * usb_sg_wait() is called.
348 struct usb_sg_request
*io
,
349 struct usb_device
*dev
,
352 struct scatterlist
*sg
,
362 if (!io
|| !dev
|| !sg
363 || usb_pipecontrol (pipe
)
364 || usb_pipeisoc (pipe
)
368 spin_lock_init (&io
->lock
);
374 /* not all host controllers use DMA (like the mainstream pci ones);
375 * they can use PIO (sl811) or be software over another transport.
377 dma
= (dev
->dev
.dma_mask
!= NULL
);
379 io
->entries
= usb_buffer_map_sg (dev
, pipe
, sg
, nents
);
383 /* initialize all the urbs we'll use */
384 if (io
->entries
<= 0)
387 io
->count
= io
->entries
;
388 io
->urbs
= kmalloc (io
->entries
* sizeof *io
->urbs
, mem_flags
);
392 urb_flags
= URB_NO_TRANSFER_DMA_MAP
| URB_NO_INTERRUPT
;
393 if (usb_pipein (pipe
))
394 urb_flags
|= URB_SHORT_NOT_OK
;
396 for (i
= 0; i
< io
->entries
; i
++) {
399 io
->urbs
[i
] = usb_alloc_urb (0, mem_flags
);
405 io
->urbs
[i
]->dev
= NULL
;
406 io
->urbs
[i
]->pipe
= pipe
;
407 io
->urbs
[i
]->interval
= period
;
408 io
->urbs
[i
]->transfer_flags
= urb_flags
;
410 io
->urbs
[i
]->complete
= sg_complete
;
411 io
->urbs
[i
]->context
= io
;
412 io
->urbs
[i
]->status
= -EINPROGRESS
;
413 io
->urbs
[i
]->actual_length
= 0;
416 /* hc may use _only_ transfer_dma */
417 io
->urbs
[i
]->transfer_dma
= sg_dma_address (sg
+ i
);
418 len
= sg_dma_len (sg
+ i
);
420 /* hc may use _only_ transfer_buffer */
421 io
->urbs
[i
]->transfer_buffer
=
422 page_address (sg
[i
].page
) + sg
[i
].offset
;
427 len
= min_t (unsigned, len
, length
);
432 io
->urbs
[i
]->transfer_buffer_length
= len
;
434 io
->urbs
[--i
]->transfer_flags
&= ~URB_NO_INTERRUPT
;
436 /* transaction state */
439 init_completion (&io
->complete
);
449 * usb_sg_wait - synchronously execute scatter/gather request
450 * @io: request block handle, as initialized with usb_sg_init().
451 * some fields become accessible when this call returns.
452 * Context: !in_interrupt ()
454 * This function blocks until the specified I/O operation completes. It
455 * leverages the grouping of the related I/O requests to get good transfer
456 * rates, by queueing the requests. At higher speeds, such queuing can
457 * significantly improve USB throughput.
459 * There are three kinds of completion for this function.
460 * (1) success, where io->status is zero. The number of io->bytes
461 * transferred is as requested.
462 * (2) error, where io->status is a negative errno value. The number
463 * of io->bytes transferred before the error is usually less
464 * than requested, and can be nonzero.
465 * (3) cancellation, a type of error with status -ECONNRESET that
466 * is initiated by usb_sg_cancel().
468 * When this function returns, all memory allocated through usb_sg_init() or
469 * this call will have been freed. The request block parameter may still be
470 * passed to usb_sg_cancel(), or it may be freed. It could also be
471 * reinitialized and then reused.
473 * Data Transfer Rates:
475 * Bulk transfers are valid for full or high speed endpoints.
476 * The best full speed data rate is 19 packets of 64 bytes each
477 * per frame, or 1216 bytes per millisecond.
478 * The best high speed data rate is 13 packets of 512 bytes each
479 * per microframe, or 52 KBytes per millisecond.
481 * The reason to use interrupt transfers through this API would most likely
482 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
483 * could be transferred. That capability is less useful for low or full
484 * speed interrupt endpoints, which allow at most one packet per millisecond,
485 * of at most 8 or 64 bytes (respectively).
487 void usb_sg_wait (struct usb_sg_request
*io
)
489 int i
, entries
= io
->entries
;
491 /* queue the urbs. */
492 spin_lock_irq (&io
->lock
);
493 for (i
= 0; i
< entries
&& !io
->status
; i
++) {
496 io
->urbs
[i
]->dev
= io
->dev
;
497 retval
= usb_submit_urb (io
->urbs
[i
], GFP_ATOMIC
);
499 /* after we submit, let completions or cancelations fire;
500 * we handshake using io->status.
502 spin_unlock_irq (&io
->lock
);
504 /* maybe we retrying will recover */
505 case -ENXIO
: // hc didn't queue this one
508 io
->urbs
[i
]->dev
= NULL
;
514 /* no error? continue immediately.
516 * NOTE: to work better with UHCI (4K I/O buffer may
517 * need 3K of TDs) it may be good to limit how many
518 * URBs are queued at once; N milliseconds?
524 /* fail any uncompleted urbs */
526 io
->urbs
[i
]->dev
= NULL
;
527 io
->urbs
[i
]->status
= retval
;
528 dev_dbg (&io
->dev
->dev
, "%s, submit --> %d\n",
529 __FUNCTION__
, retval
);
532 spin_lock_irq (&io
->lock
);
533 if (retval
&& (io
->status
== 0 || io
->status
== -ECONNRESET
))
536 io
->count
-= entries
- i
;
538 complete (&io
->complete
);
539 spin_unlock_irq (&io
->lock
);
541 /* OK, yes, this could be packaged as non-blocking.
542 * So could the submit loop above ... but it's easier to
543 * solve neither problem than to solve both!
545 wait_for_completion (&io
->complete
);
551 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
552 * @io: request block, initialized with usb_sg_init()
554 * This stops a request after it has been started by usb_sg_wait().
555 * It can also prevents one initialized by usb_sg_init() from starting,
556 * so that call just frees resources allocated to the request.
558 void usb_sg_cancel (struct usb_sg_request
*io
)
562 spin_lock_irqsave (&io
->lock
, flags
);
564 /* shut everything down, if it didn't already */
568 io
->status
= -ECONNRESET
;
569 spin_unlock (&io
->lock
);
570 for (i
= 0; i
< io
->entries
; i
++) {
573 if (!io
->urbs
[i
]->dev
)
575 retval
= usb_unlink_urb (io
->urbs
[i
]);
576 if (retval
!= -EINPROGRESS
&& retval
!= -EBUSY
)
577 dev_warn (&io
->dev
->dev
, "%s, unlink --> %d\n",
578 __FUNCTION__
, retval
);
580 spin_lock (&io
->lock
);
582 spin_unlock_irqrestore (&io
->lock
, flags
);
585 /*-------------------------------------------------------------------*/
588 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
589 * @dev: the device whose descriptor is being retrieved
590 * @type: the descriptor type (USB_DT_*)
591 * @index: the number of the descriptor
592 * @buf: where to put the descriptor
593 * @size: how big is "buf"?
594 * Context: !in_interrupt ()
596 * Gets a USB descriptor. Convenience functions exist to simplify
597 * getting some types of descriptors. Use
598 * usb_get_string() or usb_string() for USB_DT_STRING.
599 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
600 * are part of the device structure.
601 * In addition to a number of USB-standard descriptors, some
602 * devices also use class-specific or vendor-specific descriptors.
604 * This call is synchronous, and may not be used in an interrupt context.
606 * Returns the number of bytes received on success, or else the status code
607 * returned by the underlying usb_control_msg() call.
609 int usb_get_descriptor(struct usb_device
*dev
, unsigned char type
, unsigned char index
, void *buf
, int size
)
614 memset(buf
,0,size
); // Make sure we parse really received data
616 for (i
= 0; i
< 3; ++i
) {
617 /* retry on length 0 or stall; some devices are flakey */
618 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
619 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
,
620 (type
<< 8) + index
, 0, buf
, size
,
621 USB_CTRL_GET_TIMEOUT
);
622 if (result
== 0 || result
== -EPIPE
)
624 if (result
> 1 && ((u8
*)buf
)[1] != type
) {
634 * usb_get_string - gets a string descriptor
635 * @dev: the device whose string descriptor is being retrieved
636 * @langid: code for language chosen (from string descriptor zero)
637 * @index: the number of the descriptor
638 * @buf: where to put the string
639 * @size: how big is "buf"?
640 * Context: !in_interrupt ()
642 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
643 * in little-endian byte order).
644 * The usb_string() function will often be a convenient way to turn
645 * these strings into kernel-printable form.
647 * Strings may be referenced in device, configuration, interface, or other
648 * descriptors, and could also be used in vendor-specific ways.
650 * This call is synchronous, and may not be used in an interrupt context.
652 * Returns the number of bytes received on success, or else the status code
653 * returned by the underlying usb_control_msg() call.
655 static int usb_get_string(struct usb_device
*dev
, unsigned short langid
,
656 unsigned char index
, void *buf
, int size
)
661 for (i
= 0; i
< 3; ++i
) {
662 /* retry on length 0 or stall; some devices are flakey */
663 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
664 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
,
665 (USB_DT_STRING
<< 8) + index
, langid
, buf
, size
,
666 USB_CTRL_GET_TIMEOUT
);
667 if (!(result
== 0 || result
== -EPIPE
))
673 static void usb_try_string_workarounds(unsigned char *buf
, int *length
)
675 int newlength
, oldlength
= *length
;
677 for (newlength
= 2; newlength
+ 1 < oldlength
; newlength
+= 2)
678 if (!isprint(buf
[newlength
]) || buf
[newlength
+ 1])
687 static int usb_string_sub(struct usb_device
*dev
, unsigned int langid
,
688 unsigned int index
, unsigned char *buf
)
692 /* Try to read the string descriptor by asking for the maximum
693 * possible number of bytes */
694 if (dev
->quirks
& USB_QUIRK_STRING_FETCH_255
)
697 rc
= usb_get_string(dev
, langid
, index
, buf
, 255);
699 /* If that failed try to read the descriptor length, then
700 * ask for just that many bytes */
702 rc
= usb_get_string(dev
, langid
, index
, buf
, 2);
704 rc
= usb_get_string(dev
, langid
, index
, buf
, buf
[0]);
708 if (!buf
[0] && !buf
[1])
709 usb_try_string_workarounds(buf
, &rc
);
711 /* There might be extra junk at the end of the descriptor */
715 rc
= rc
- (rc
& 1); /* force a multiple of two */
719 rc
= (rc
< 0 ? rc
: -EINVAL
);
725 * usb_string - returns ISO 8859-1 version of a string descriptor
726 * @dev: the device whose string descriptor is being retrieved
727 * @index: the number of the descriptor
728 * @buf: where to put the string
729 * @size: how big is "buf"?
730 * Context: !in_interrupt ()
732 * This converts the UTF-16LE encoded strings returned by devices, from
733 * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones
734 * that are more usable in most kernel contexts. Note that all characters
735 * in the chosen descriptor that can't be encoded using ISO-8859-1
736 * are converted to the question mark ("?") character, and this function
737 * chooses strings in the first language supported by the device.
739 * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit
740 * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode,
741 * and is appropriate for use many uses of English and several other
742 * Western European languages. (But it doesn't include the "Euro" symbol.)
744 * This call is synchronous, and may not be used in an interrupt context.
746 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
748 int usb_string(struct usb_device
*dev
, int index
, char *buf
, size_t size
)
754 if (dev
->state
== USB_STATE_SUSPENDED
)
755 return -EHOSTUNREACH
;
756 if (size
<= 0 || !buf
|| !index
)
759 tbuf
= kmalloc(256, GFP_KERNEL
);
763 /* get langid for strings if it's not yet known */
764 if (!dev
->have_langid
) {
765 err
= usb_string_sub(dev
, 0, 0, tbuf
);
768 "string descriptor 0 read error: %d\n",
771 } else if (err
< 4) {
772 dev_err (&dev
->dev
, "string descriptor 0 too short\n");
776 dev
->have_langid
= 1;
777 dev
->string_langid
= tbuf
[2] | (tbuf
[3]<< 8);
778 /* always use the first langid listed */
779 dev_dbg (&dev
->dev
, "default language 0x%04x\n",
784 err
= usb_string_sub(dev
, dev
->string_langid
, index
, tbuf
);
788 size
--; /* leave room for trailing NULL char in output buffer */
789 for (idx
= 0, u
= 2; u
< err
; u
+= 2) {
792 if (tbuf
[u
+1]) /* high byte */
793 buf
[idx
++] = '?'; /* non ISO-8859-1 character */
795 buf
[idx
++] = tbuf
[u
];
800 if (tbuf
[1] != USB_DT_STRING
)
801 dev_dbg(&dev
->dev
, "wrong descriptor type %02x for string %d (\"%s\")\n", tbuf
[1], index
, buf
);
809 * usb_cache_string - read a string descriptor and cache it for later use
810 * @udev: the device whose string descriptor is being read
811 * @index: the descriptor index
813 * Returns a pointer to a kmalloc'ed buffer containing the descriptor string,
814 * or NULL if the index is 0 or the string could not be read.
816 char *usb_cache_string(struct usb_device
*udev
, int index
)
819 char *smallbuf
= NULL
;
822 if (index
> 0 && (buf
= kmalloc(256, GFP_KERNEL
)) != NULL
) {
823 if ((len
= usb_string(udev
, index
, buf
, 256)) > 0) {
824 if ((smallbuf
= kmalloc(++len
, GFP_KERNEL
)) == NULL
)
826 memcpy(smallbuf
, buf
, len
);
834 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
835 * @dev: the device whose device descriptor is being updated
836 * @size: how much of the descriptor to read
837 * Context: !in_interrupt ()
839 * Updates the copy of the device descriptor stored in the device structure,
840 * which dedicates space for this purpose.
842 * Not exported, only for use by the core. If drivers really want to read
843 * the device descriptor directly, they can call usb_get_descriptor() with
844 * type = USB_DT_DEVICE and index = 0.
846 * This call is synchronous, and may not be used in an interrupt context.
848 * Returns the number of bytes received on success, or else the status code
849 * returned by the underlying usb_control_msg() call.
851 int usb_get_device_descriptor(struct usb_device
*dev
, unsigned int size
)
853 struct usb_device_descriptor
*desc
;
856 if (size
> sizeof(*desc
))
858 desc
= kmalloc(sizeof(*desc
), GFP_NOIO
);
862 ret
= usb_get_descriptor(dev
, USB_DT_DEVICE
, 0, desc
, size
);
864 memcpy(&dev
->descriptor
, desc
, size
);
870 * usb_get_status - issues a GET_STATUS call
871 * @dev: the device whose status is being checked
872 * @type: USB_RECIP_*; for device, interface, or endpoint
873 * @target: zero (for device), else interface or endpoint number
874 * @data: pointer to two bytes of bitmap data
875 * Context: !in_interrupt ()
877 * Returns device, interface, or endpoint status. Normally only of
878 * interest to see if the device is self powered, or has enabled the
879 * remote wakeup facility; or whether a bulk or interrupt endpoint
880 * is halted ("stalled").
882 * Bits in these status bitmaps are set using the SET_FEATURE request,
883 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
884 * function should be used to clear halt ("stall") status.
886 * This call is synchronous, and may not be used in an interrupt context.
888 * Returns the number of bytes received on success, or else the status code
889 * returned by the underlying usb_control_msg() call.
891 int usb_get_status(struct usb_device
*dev
, int type
, int target
, void *data
)
894 u16
*status
= kmalloc(sizeof(*status
), GFP_KERNEL
);
899 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
900 USB_REQ_GET_STATUS
, USB_DIR_IN
| type
, 0, target
, status
,
901 sizeof(*status
), USB_CTRL_GET_TIMEOUT
);
903 *(u16
*)data
= *status
;
909 * usb_clear_halt - tells device to clear endpoint halt/stall condition
910 * @dev: device whose endpoint is halted
911 * @pipe: endpoint "pipe" being cleared
912 * Context: !in_interrupt ()
914 * This is used to clear halt conditions for bulk and interrupt endpoints,
915 * as reported by URB completion status. Endpoints that are halted are
916 * sometimes referred to as being "stalled". Such endpoints are unable
917 * to transmit or receive data until the halt status is cleared. Any URBs
918 * queued for such an endpoint should normally be unlinked by the driver
919 * before clearing the halt condition, as described in sections 5.7.5
920 * and 5.8.5 of the USB 2.0 spec.
922 * Note that control and isochronous endpoints don't halt, although control
923 * endpoints report "protocol stall" (for unsupported requests) using the
924 * same status code used to report a true stall.
926 * This call is synchronous, and may not be used in an interrupt context.
928 * Returns zero on success, or else the status code returned by the
929 * underlying usb_control_msg() call.
931 int usb_clear_halt(struct usb_device
*dev
, int pipe
)
934 int endp
= usb_pipeendpoint(pipe
);
936 if (usb_pipein (pipe
))
939 /* we don't care if it wasn't halted first. in fact some devices
940 * (like some ibmcam model 1 units) seem to expect hosts to make
941 * this request for iso endpoints, which can't halt!
943 result
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
944 USB_REQ_CLEAR_FEATURE
, USB_RECIP_ENDPOINT
,
945 USB_ENDPOINT_HALT
, endp
, NULL
, 0,
946 USB_CTRL_SET_TIMEOUT
);
948 /* don't un-halt or force to DATA0 except on success */
952 /* NOTE: seems like Microsoft and Apple don't bother verifying
953 * the clear "took", so some devices could lock up if you check...
954 * such as the Hagiwara FlashGate DUAL. So we won't bother.
956 * NOTE: make sure the logic here doesn't diverge much from
957 * the copy in usb-storage, for as long as we need two copies.
960 /* toggle was reset by the clear */
961 usb_settoggle(dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
), 0);
967 * usb_disable_endpoint -- Disable an endpoint by address
968 * @dev: the device whose endpoint is being disabled
969 * @epaddr: the endpoint's address. Endpoint number for output,
970 * endpoint number + USB_DIR_IN for input
972 * Deallocates hcd/hardware state for this endpoint ... and nukes all
975 * If the HCD hasn't registered a disable() function, this sets the
976 * endpoint's maxpacket size to 0 to prevent further submissions.
978 void usb_disable_endpoint(struct usb_device
*dev
, unsigned int epaddr
)
980 unsigned int epnum
= epaddr
& USB_ENDPOINT_NUMBER_MASK
;
981 struct usb_host_endpoint
*ep
;
986 if (usb_endpoint_out(epaddr
)) {
987 ep
= dev
->ep_out
[epnum
];
988 dev
->ep_out
[epnum
] = NULL
;
990 ep
= dev
->ep_in
[epnum
];
991 dev
->ep_in
[epnum
] = NULL
;
994 usb_hcd_endpoint_disable(dev
, ep
);
998 * usb_disable_interface -- Disable all endpoints for an interface
999 * @dev: the device whose interface is being disabled
1000 * @intf: pointer to the interface descriptor
1002 * Disables all the endpoints for the interface's current altsetting.
1004 void usb_disable_interface(struct usb_device
*dev
, struct usb_interface
*intf
)
1006 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
1009 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; ++i
) {
1010 usb_disable_endpoint(dev
,
1011 alt
->endpoint
[i
].desc
.bEndpointAddress
);
1016 * usb_disable_device - Disable all the endpoints for a USB device
1017 * @dev: the device whose endpoints are being disabled
1018 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
1020 * Disables all the device's endpoints, potentially including endpoint 0.
1021 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1022 * pending urbs) and usbcore state for the interfaces, so that usbcore
1023 * must usb_set_configuration() before any interfaces could be used.
1025 void usb_disable_device(struct usb_device
*dev
, int skip_ep0
)
1029 dev_dbg(&dev
->dev
, "%s nuking %s URBs\n", __FUNCTION__
,
1030 skip_ep0
? "non-ep0" : "all");
1031 for (i
= skip_ep0
; i
< 16; ++i
) {
1032 usb_disable_endpoint(dev
, i
);
1033 usb_disable_endpoint(dev
, i
+ USB_DIR_IN
);
1035 dev
->toggle
[0] = dev
->toggle
[1] = 0;
1037 /* getting rid of interfaces will disconnect
1038 * any drivers bound to them (a key side effect)
1040 if (dev
->actconfig
) {
1041 for (i
= 0; i
< dev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1042 struct usb_interface
*interface
;
1044 /* remove this interface if it has been registered */
1045 interface
= dev
->actconfig
->interface
[i
];
1046 if (!device_is_registered(&interface
->dev
))
1048 dev_dbg (&dev
->dev
, "unregistering interface %s\n",
1049 interface
->dev
.bus_id
);
1050 usb_remove_sysfs_intf_files(interface
);
1051 device_del (&interface
->dev
);
1054 /* Now that the interfaces are unbound, nobody should
1055 * try to access them.
1057 for (i
= 0; i
< dev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1058 put_device (&dev
->actconfig
->interface
[i
]->dev
);
1059 dev
->actconfig
->interface
[i
] = NULL
;
1061 dev
->actconfig
= NULL
;
1062 if (dev
->state
== USB_STATE_CONFIGURED
)
1063 usb_set_device_state(dev
, USB_STATE_ADDRESS
);
1069 * usb_enable_endpoint - Enable an endpoint for USB communications
1070 * @dev: the device whose interface is being enabled
1073 * Resets the endpoint toggle, and sets dev->ep_{in,out} pointers.
1074 * For control endpoints, both the input and output sides are handled.
1077 usb_enable_endpoint(struct usb_device
*dev
, struct usb_host_endpoint
*ep
)
1079 unsigned int epaddr
= ep
->desc
.bEndpointAddress
;
1080 unsigned int epnum
= epaddr
& USB_ENDPOINT_NUMBER_MASK
;
1083 is_control
= ((ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
1084 == USB_ENDPOINT_XFER_CONTROL
);
1085 if (usb_endpoint_out(epaddr
) || is_control
) {
1086 usb_settoggle(dev
, epnum
, 1, 0);
1087 dev
->ep_out
[epnum
] = ep
;
1089 if (!usb_endpoint_out(epaddr
) || is_control
) {
1090 usb_settoggle(dev
, epnum
, 0, 0);
1091 dev
->ep_in
[epnum
] = ep
;
1096 * usb_enable_interface - Enable all the endpoints for an interface
1097 * @dev: the device whose interface is being enabled
1098 * @intf: pointer to the interface descriptor
1100 * Enables all the endpoints for the interface's current altsetting.
1102 static void usb_enable_interface(struct usb_device
*dev
,
1103 struct usb_interface
*intf
)
1105 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
1108 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; ++i
)
1109 usb_enable_endpoint(dev
, &alt
->endpoint
[i
]);
1113 * usb_set_interface - Makes a particular alternate setting be current
1114 * @dev: the device whose interface is being updated
1115 * @interface: the interface being updated
1116 * @alternate: the setting being chosen.
1117 * Context: !in_interrupt ()
1119 * This is used to enable data transfers on interfaces that may not
1120 * be enabled by default. Not all devices support such configurability.
1121 * Only the driver bound to an interface may change its setting.
1123 * Within any given configuration, each interface may have several
1124 * alternative settings. These are often used to control levels of
1125 * bandwidth consumption. For example, the default setting for a high
1126 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1127 * while interrupt transfers of up to 3KBytes per microframe are legal.
1128 * Also, isochronous endpoints may never be part of an
1129 * interface's default setting. To access such bandwidth, alternate
1130 * interface settings must be made current.
1132 * Note that in the Linux USB subsystem, bandwidth associated with
1133 * an endpoint in a given alternate setting is not reserved until an URB
1134 * is submitted that needs that bandwidth. Some other operating systems
1135 * allocate bandwidth early, when a configuration is chosen.
1137 * This call is synchronous, and may not be used in an interrupt context.
1138 * Also, drivers must not change altsettings while urbs are scheduled for
1139 * endpoints in that interface; all such urbs must first be completed
1140 * (perhaps forced by unlinking).
1142 * Returns zero on success, or else the status code returned by the
1143 * underlying usb_control_msg() call.
1145 int usb_set_interface(struct usb_device
*dev
, int interface
, int alternate
)
1147 struct usb_interface
*iface
;
1148 struct usb_host_interface
*alt
;
1152 if (dev
->state
== USB_STATE_SUSPENDED
)
1153 return -EHOSTUNREACH
;
1155 iface
= usb_ifnum_to_if(dev
, interface
);
1157 dev_dbg(&dev
->dev
, "selecting invalid interface %d\n",
1162 alt
= usb_altnum_to_altsetting(iface
, alternate
);
1164 warn("selecting invalid altsetting %d", alternate
);
1168 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
1169 USB_REQ_SET_INTERFACE
, USB_RECIP_INTERFACE
,
1170 alternate
, interface
, NULL
, 0, 5000);
1172 /* 9.4.10 says devices don't need this and are free to STALL the
1173 * request if the interface only has one alternate setting.
1175 if (ret
== -EPIPE
&& iface
->num_altsetting
== 1) {
1177 "manual set_interface for iface %d, alt %d\n",
1178 interface
, alternate
);
1183 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1184 * when they implement async or easily-killable versions of this or
1185 * other "should-be-internal" functions (like clear_halt).
1186 * should hcd+usbcore postprocess control requests?
1189 /* prevent submissions using previous endpoint settings */
1190 if (device_is_registered(&iface
->dev
))
1191 usb_remove_sysfs_intf_files(iface
);
1192 usb_disable_interface(dev
, iface
);
1194 iface
->cur_altsetting
= alt
;
1196 /* If the interface only has one altsetting and the device didn't
1197 * accept the request, we attempt to carry out the equivalent action
1198 * by manually clearing the HALT feature for each endpoint in the
1204 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; i
++) {
1205 unsigned int epaddr
=
1206 alt
->endpoint
[i
].desc
.bEndpointAddress
;
1208 __create_pipe(dev
, USB_ENDPOINT_NUMBER_MASK
& epaddr
)
1209 | (usb_endpoint_out(epaddr
) ? USB_DIR_OUT
: USB_DIR_IN
);
1211 usb_clear_halt(dev
, pipe
);
1215 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1218 * Despite EP0 is always present in all interfaces/AS, the list of
1219 * endpoints from the descriptor does not contain EP0. Due to its
1220 * omnipresence one might expect EP0 being considered "affected" by
1221 * any SetInterface request and hence assume toggles need to be reset.
1222 * However, EP0 toggles are re-synced for every individual transfer
1223 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1224 * (Likewise, EP0 never "halts" on well designed devices.)
1226 usb_enable_interface(dev
, iface
);
1227 if (device_is_registered(&iface
->dev
))
1228 usb_create_sysfs_intf_files(iface
);
1234 * usb_reset_configuration - lightweight device reset
1235 * @dev: the device whose configuration is being reset
1237 * This issues a standard SET_CONFIGURATION request to the device using
1238 * the current configuration. The effect is to reset most USB-related
1239 * state in the device, including interface altsettings (reset to zero),
1240 * endpoint halts (cleared), and data toggle (only for bulk and interrupt
1241 * endpoints). Other usbcore state is unchanged, including bindings of
1242 * usb device drivers to interfaces.
1244 * Because this affects multiple interfaces, avoid using this with composite
1245 * (multi-interface) devices. Instead, the driver for each interface may
1246 * use usb_set_interface() on the interfaces it claims. Be careful though;
1247 * some devices don't support the SET_INTERFACE request, and others won't
1248 * reset all the interface state (notably data toggles). Resetting the whole
1249 * configuration would affect other drivers' interfaces.
1251 * The caller must own the device lock.
1253 * Returns zero on success, else a negative error code.
1255 int usb_reset_configuration(struct usb_device
*dev
)
1258 struct usb_host_config
*config
;
1260 if (dev
->state
== USB_STATE_SUSPENDED
)
1261 return -EHOSTUNREACH
;
1263 /* caller must have locked the device and must own
1264 * the usb bus readlock (so driver bindings are stable);
1265 * calls during probe() are fine
1268 for (i
= 1; i
< 16; ++i
) {
1269 usb_disable_endpoint(dev
, i
);
1270 usb_disable_endpoint(dev
, i
+ USB_DIR_IN
);
1273 config
= dev
->actconfig
;
1274 retval
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
1275 USB_REQ_SET_CONFIGURATION
, 0,
1276 config
->desc
.bConfigurationValue
, 0,
1277 NULL
, 0, USB_CTRL_SET_TIMEOUT
);
1281 dev
->toggle
[0] = dev
->toggle
[1] = 0;
1283 /* re-init hc/hcd interface/endpoint state */
1284 for (i
= 0; i
< config
->desc
.bNumInterfaces
; i
++) {
1285 struct usb_interface
*intf
= config
->interface
[i
];
1286 struct usb_host_interface
*alt
;
1288 if (device_is_registered(&intf
->dev
))
1289 usb_remove_sysfs_intf_files(intf
);
1290 alt
= usb_altnum_to_altsetting(intf
, 0);
1292 /* No altsetting 0? We'll assume the first altsetting.
1293 * We could use a GetInterface call, but if a device is
1294 * so non-compliant that it doesn't have altsetting 0
1295 * then I wouldn't trust its reply anyway.
1298 alt
= &intf
->altsetting
[0];
1300 intf
->cur_altsetting
= alt
;
1301 usb_enable_interface(dev
, intf
);
1302 if (device_is_registered(&intf
->dev
))
1303 usb_create_sysfs_intf_files(intf
);
1308 void usb_release_interface(struct device
*dev
)
1310 struct usb_interface
*intf
= to_usb_interface(dev
);
1311 struct usb_interface_cache
*intfc
=
1312 altsetting_to_usb_interface_cache(intf
->altsetting
);
1314 kref_put(&intfc
->ref
, usb_release_interface_cache
);
1318 #ifdef CONFIG_HOTPLUG
1319 static int usb_if_uevent(struct device
*dev
, char **envp
, int num_envp
,
1320 char *buffer
, int buffer_size
)
1322 struct usb_device
*usb_dev
;
1323 struct usb_interface
*intf
;
1324 struct usb_host_interface
*alt
;
1331 /* driver is often null here; dev_dbg() would oops */
1332 pr_debug ("usb %s: uevent\n", dev
->bus_id
);
1334 intf
= to_usb_interface(dev
);
1335 usb_dev
= interface_to_usbdev(intf
);
1336 alt
= intf
->cur_altsetting
;
1338 if (add_uevent_var(envp
, num_envp
, &i
,
1339 buffer
, buffer_size
, &length
,
1340 "INTERFACE=%d/%d/%d",
1341 alt
->desc
.bInterfaceClass
,
1342 alt
->desc
.bInterfaceSubClass
,
1343 alt
->desc
.bInterfaceProtocol
))
1346 if (add_uevent_var(envp
, num_envp
, &i
,
1347 buffer
, buffer_size
, &length
,
1348 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
1349 le16_to_cpu(usb_dev
->descriptor
.idVendor
),
1350 le16_to_cpu(usb_dev
->descriptor
.idProduct
),
1351 le16_to_cpu(usb_dev
->descriptor
.bcdDevice
),
1352 usb_dev
->descriptor
.bDeviceClass
,
1353 usb_dev
->descriptor
.bDeviceSubClass
,
1354 usb_dev
->descriptor
.bDeviceProtocol
,
1355 alt
->desc
.bInterfaceClass
,
1356 alt
->desc
.bInterfaceSubClass
,
1357 alt
->desc
.bInterfaceProtocol
))
1366 static int usb_if_uevent(struct device
*dev
, char **envp
,
1367 int num_envp
, char *buffer
, int buffer_size
)
1371 #endif /* CONFIG_HOTPLUG */
1373 struct device_type usb_if_device_type
= {
1374 .name
= "usb_interface",
1375 .release
= usb_release_interface
,
1376 .uevent
= usb_if_uevent
,
1380 * usb_set_configuration - Makes a particular device setting be current
1381 * @dev: the device whose configuration is being updated
1382 * @configuration: the configuration being chosen.
1383 * Context: !in_interrupt(), caller owns the device lock
1385 * This is used to enable non-default device modes. Not all devices
1386 * use this kind of configurability; many devices only have one
1389 * @configuration is the value of the configuration to be installed.
1390 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1391 * must be non-zero; a value of zero indicates that the device in
1392 * unconfigured. However some devices erroneously use 0 as one of their
1393 * configuration values. To help manage such devices, this routine will
1394 * accept @configuration = -1 as indicating the device should be put in
1395 * an unconfigured state.
1397 * USB device configurations may affect Linux interoperability,
1398 * power consumption and the functionality available. For example,
1399 * the default configuration is limited to using 100mA of bus power,
1400 * so that when certain device functionality requires more power,
1401 * and the device is bus powered, that functionality should be in some
1402 * non-default device configuration. Other device modes may also be
1403 * reflected as configuration options, such as whether two ISDN
1404 * channels are available independently; and choosing between open
1405 * standard device protocols (like CDC) or proprietary ones.
1407 * Note that USB has an additional level of device configurability,
1408 * associated with interfaces. That configurability is accessed using
1409 * usb_set_interface().
1411 * This call is synchronous. The calling context must be able to sleep,
1412 * must own the device lock, and must not hold the driver model's USB
1413 * bus mutex; usb device driver probe() methods cannot use this routine.
1415 * Returns zero on success, or else the status code returned by the
1416 * underlying call that failed. On successful completion, each interface
1417 * in the original device configuration has been destroyed, and each one
1418 * in the new configuration has been probed by all relevant usb device
1419 * drivers currently known to the kernel.
1421 int usb_set_configuration(struct usb_device
*dev
, int configuration
)
1424 struct usb_host_config
*cp
= NULL
;
1425 struct usb_interface
**new_interfaces
= NULL
;
1428 if (configuration
== -1)
1431 for (i
= 0; i
< dev
->descriptor
.bNumConfigurations
; i
++) {
1432 if (dev
->config
[i
].desc
.bConfigurationValue
==
1434 cp
= &dev
->config
[i
];
1439 if ((!cp
&& configuration
!= 0))
1442 /* The USB spec says configuration 0 means unconfigured.
1443 * But if a device includes a configuration numbered 0,
1444 * we will accept it as a correctly configured state.
1445 * Use -1 if you really want to unconfigure the device.
1447 if (cp
&& configuration
== 0)
1448 dev_warn(&dev
->dev
, "config 0 descriptor??\n");
1450 /* Allocate memory for new interfaces before doing anything else,
1451 * so that if we run out then nothing will have changed. */
1454 nintf
= cp
->desc
.bNumInterfaces
;
1455 new_interfaces
= kmalloc(nintf
* sizeof(*new_interfaces
),
1457 if (!new_interfaces
) {
1458 dev_err(&dev
->dev
, "Out of memory");
1462 for (; n
< nintf
; ++n
) {
1463 new_interfaces
[n
] = kzalloc(
1464 sizeof(struct usb_interface
),
1466 if (!new_interfaces
[n
]) {
1467 dev_err(&dev
->dev
, "Out of memory");
1471 kfree(new_interfaces
[n
]);
1472 kfree(new_interfaces
);
1477 i
= dev
->bus_mA
- cp
->desc
.bMaxPower
* 2;
1479 dev_warn(&dev
->dev
, "new config #%d exceeds power "
1484 /* Wake up the device so we can send it the Set-Config request */
1485 ret
= usb_autoresume_device(dev
);
1487 goto free_interfaces
;
1489 /* if it's already configured, clear out old state first.
1490 * getting rid of old interfaces means unbinding their drivers.
1492 if (dev
->state
!= USB_STATE_ADDRESS
)
1493 usb_disable_device (dev
, 1); // Skip ep0
1495 if ((ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
1496 USB_REQ_SET_CONFIGURATION
, 0, configuration
, 0,
1497 NULL
, 0, USB_CTRL_SET_TIMEOUT
)) < 0) {
1499 /* All the old state is gone, so what else can we do?
1500 * The device is probably useless now anyway.
1505 dev
->actconfig
= cp
;
1507 usb_set_device_state(dev
, USB_STATE_ADDRESS
);
1508 usb_autosuspend_device(dev
);
1509 goto free_interfaces
;
1511 usb_set_device_state(dev
, USB_STATE_CONFIGURED
);
1513 /* Initialize the new interface structures and the
1514 * hc/hcd/usbcore interface/endpoint state.
1516 for (i
= 0; i
< nintf
; ++i
) {
1517 struct usb_interface_cache
*intfc
;
1518 struct usb_interface
*intf
;
1519 struct usb_host_interface
*alt
;
1521 cp
->interface
[i
] = intf
= new_interfaces
[i
];
1522 intfc
= cp
->intf_cache
[i
];
1523 intf
->altsetting
= intfc
->altsetting
;
1524 intf
->num_altsetting
= intfc
->num_altsetting
;
1525 kref_get(&intfc
->ref
);
1527 alt
= usb_altnum_to_altsetting(intf
, 0);
1529 /* No altsetting 0? We'll assume the first altsetting.
1530 * We could use a GetInterface call, but if a device is
1531 * so non-compliant that it doesn't have altsetting 0
1532 * then I wouldn't trust its reply anyway.
1535 alt
= &intf
->altsetting
[0];
1537 intf
->cur_altsetting
= alt
;
1538 usb_enable_interface(dev
, intf
);
1539 intf
->dev
.parent
= &dev
->dev
;
1540 intf
->dev
.driver
= NULL
;
1541 intf
->dev
.bus
= &usb_bus_type
;
1542 intf
->dev
.type
= &usb_if_device_type
;
1543 intf
->dev
.dma_mask
= dev
->dev
.dma_mask
;
1544 device_initialize (&intf
->dev
);
1545 mark_quiesced(intf
);
1546 sprintf (&intf
->dev
.bus_id
[0], "%d-%s:%d.%d",
1547 dev
->bus
->busnum
, dev
->devpath
,
1548 configuration
, alt
->desc
.bInterfaceNumber
);
1550 kfree(new_interfaces
);
1552 if (cp
->string
== NULL
)
1553 cp
->string
= usb_cache_string(dev
, cp
->desc
.iConfiguration
);
1555 /* Now that all the interfaces are set up, register them
1556 * to trigger binding of drivers to interfaces. probe()
1557 * routines may install different altsettings and may
1558 * claim() any interfaces not yet bound. Many class drivers
1559 * need that: CDC, audio, video, etc.
1561 for (i
= 0; i
< nintf
; ++i
) {
1562 struct usb_interface
*intf
= cp
->interface
[i
];
1565 "adding %s (config #%d, interface %d)\n",
1566 intf
->dev
.bus_id
, configuration
,
1567 intf
->cur_altsetting
->desc
.bInterfaceNumber
);
1568 ret
= device_add (&intf
->dev
);
1570 dev_err(&dev
->dev
, "device_add(%s) --> %d\n",
1571 intf
->dev
.bus_id
, ret
);
1574 usb_create_sysfs_intf_files (intf
);
1577 usb_autosuspend_device(dev
);
1581 struct set_config_request
{
1582 struct usb_device
*udev
;
1584 struct work_struct work
;
1587 /* Worker routine for usb_driver_set_configuration() */
1588 static void driver_set_config_work(struct work_struct
*work
)
1590 struct set_config_request
*req
=
1591 container_of(work
, struct set_config_request
, work
);
1593 usb_lock_device(req
->udev
);
1594 usb_set_configuration(req
->udev
, req
->config
);
1595 usb_unlock_device(req
->udev
);
1596 usb_put_dev(req
->udev
);
1601 * usb_driver_set_configuration - Provide a way for drivers to change device configurations
1602 * @udev: the device whose configuration is being updated
1603 * @config: the configuration being chosen.
1604 * Context: In process context, must be able to sleep
1606 * Device interface drivers are not allowed to change device configurations.
1607 * This is because changing configurations will destroy the interface the
1608 * driver is bound to and create new ones; it would be like a floppy-disk
1609 * driver telling the computer to replace the floppy-disk drive with a
1612 * Still, in certain specialized circumstances the need may arise. This
1613 * routine gets around the normal restrictions by using a work thread to
1614 * submit the change-config request.
1616 * Returns 0 if the request was succesfully queued, error code otherwise.
1617 * The caller has no way to know whether the queued request will eventually
1620 int usb_driver_set_configuration(struct usb_device
*udev
, int config
)
1622 struct set_config_request
*req
;
1624 req
= kmalloc(sizeof(*req
), GFP_KERNEL
);
1628 req
->config
= config
;
1629 INIT_WORK(&req
->work
, driver_set_config_work
);
1632 schedule_work(&req
->work
);
1635 EXPORT_SYMBOL_GPL(usb_driver_set_configuration
);
1637 // synchronous request completion model
1638 EXPORT_SYMBOL(usb_control_msg
);
1639 EXPORT_SYMBOL(usb_bulk_msg
);
1641 EXPORT_SYMBOL(usb_sg_init
);
1642 EXPORT_SYMBOL(usb_sg_cancel
);
1643 EXPORT_SYMBOL(usb_sg_wait
);
1645 // synchronous control message convenience routines
1646 EXPORT_SYMBOL(usb_get_descriptor
);
1647 EXPORT_SYMBOL(usb_get_status
);
1648 EXPORT_SYMBOL(usb_string
);
1650 // synchronous calls that also maintain usbcore state
1651 EXPORT_SYMBOL(usb_clear_halt
);
1652 EXPORT_SYMBOL(usb_reset_configuration
);
1653 EXPORT_SYMBOL(usb_set_interface
);