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/nls.h>
14 #include <linux/device.h>
15 #include <linux/scatterlist.h>
16 #include <linux/usb/quirks.h>
17 #include <asm/byteorder.h>
18 #include <asm/scatterlist.h>
20 #include "hcd.h" /* for usbcore internals */
24 struct completion done
;
28 static void usb_api_blocking_completion(struct urb
*urb
)
30 struct api_context
*ctx
= urb
->context
;
32 ctx
->status
= urb
->status
;
38 * Starts urb and waits for completion or timeout. Note that this call
39 * is NOT interruptible. Many device driver i/o requests should be
40 * interruptible and therefore these drivers should implement their
41 * own interruptible routines.
43 static int usb_start_wait_urb(struct urb
*urb
, int timeout
, int *actual_length
)
45 struct api_context ctx
;
49 init_completion(&ctx
.done
);
51 urb
->actual_length
= 0;
52 retval
= usb_submit_urb(urb
, GFP_NOIO
);
56 expire
= timeout
? msecs_to_jiffies(timeout
) : MAX_SCHEDULE_TIMEOUT
;
57 if (!wait_for_completion_timeout(&ctx
.done
, expire
)) {
59 retval
= (ctx
.status
== -ENOENT
? -ETIMEDOUT
: ctx
.status
);
61 dev_dbg(&urb
->dev
->dev
,
62 "%s timed out on ep%d%s len=%u/%u\n",
64 usb_endpoint_num(&urb
->ep
->desc
),
65 usb_urb_dir_in(urb
) ? "in" : "out",
67 urb
->transfer_buffer_length
);
72 *actual_length
= urb
->actual_length
;
78 /*-------------------------------------------------------------------*/
79 /* returns status (negative) or length (positive) */
80 static int usb_internal_control_msg(struct usb_device
*usb_dev
,
82 struct usb_ctrlrequest
*cmd
,
83 void *data
, int len
, int timeout
)
89 urb
= usb_alloc_urb(0, GFP_NOIO
);
93 usb_fill_control_urb(urb
, usb_dev
, pipe
, (unsigned char *)cmd
, data
,
94 len
, usb_api_blocking_completion
, NULL
);
96 retv
= usb_start_wait_urb(urb
, timeout
, &length
);
104 * usb_control_msg - Builds a control urb, sends it off and waits for completion
105 * @dev: pointer to the usb device to send the message to
106 * @pipe: endpoint "pipe" to send the message to
107 * @request: USB message request value
108 * @requesttype: USB message request type value
109 * @value: USB message value
110 * @index: USB message index value
111 * @data: pointer to the data to send
112 * @size: length in bytes of the data to send
113 * @timeout: time in msecs to wait for the message to complete before timing
114 * out (if 0 the wait is forever)
116 * Context: !in_interrupt ()
118 * This function sends a simple control message to a specified endpoint and
119 * waits for the message to complete, or timeout.
121 * If successful, it returns the number of bytes transferred, otherwise a
122 * negative error number.
124 * Don't use this function from within an interrupt context, like a bottom half
125 * handler. If you need an asynchronous message, or need to send a message
126 * from within interrupt context, use usb_submit_urb().
127 * If a thread in your driver uses this call, make sure your disconnect()
128 * method can wait for it to complete. Since you don't have a handle on the
129 * URB used, you can't cancel the request.
131 int usb_control_msg(struct usb_device
*dev
, unsigned int pipe
, __u8 request
,
132 __u8 requesttype
, __u16 value
, __u16 index
, void *data
,
133 __u16 size
, int timeout
)
135 struct usb_ctrlrequest
*dr
;
138 dr
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_NOIO
);
142 dr
->bRequestType
= requesttype
;
143 dr
->bRequest
= request
;
144 dr
->wValue
= cpu_to_le16(value
);
145 dr
->wIndex
= cpu_to_le16(index
);
146 dr
->wLength
= cpu_to_le16(size
);
148 /* dbg("usb_control_msg"); */
150 ret
= usb_internal_control_msg(dev
, pipe
, dr
, data
, size
, timeout
);
156 EXPORT_SYMBOL_GPL(usb_control_msg
);
159 * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion
160 * @usb_dev: pointer to the usb device to send the message to
161 * @pipe: endpoint "pipe" to send the message to
162 * @data: pointer to the data to send
163 * @len: length in bytes of the data to send
164 * @actual_length: pointer to a location to put the actual length transferred
166 * @timeout: time in msecs to wait for the message to complete before
167 * timing out (if 0 the wait is forever)
169 * Context: !in_interrupt ()
171 * This function sends a simple interrupt message to a specified endpoint and
172 * waits for the message to complete, or timeout.
174 * If successful, it returns 0, otherwise a negative error number. The number
175 * of actual bytes transferred will be stored in the actual_length paramater.
177 * Don't use this function from within an interrupt context, like a bottom half
178 * handler. If you need an asynchronous message, or need to send a message
179 * from within interrupt context, use usb_submit_urb() If a thread in your
180 * driver uses this call, make sure your disconnect() method can wait for it to
181 * complete. Since you don't have a handle on the URB used, you can't cancel
184 int usb_interrupt_msg(struct usb_device
*usb_dev
, unsigned int pipe
,
185 void *data
, int len
, int *actual_length
, int timeout
)
187 return usb_bulk_msg(usb_dev
, pipe
, data
, len
, actual_length
, timeout
);
189 EXPORT_SYMBOL_GPL(usb_interrupt_msg
);
192 * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
193 * @usb_dev: pointer to the usb device to send the message to
194 * @pipe: endpoint "pipe" to send the message to
195 * @data: pointer to the data to send
196 * @len: length in bytes of the data to send
197 * @actual_length: pointer to a location to put the actual length transferred
199 * @timeout: time in msecs to wait for the message to complete before
200 * timing out (if 0 the wait is forever)
202 * Context: !in_interrupt ()
204 * This function sends a simple bulk message to a specified endpoint
205 * and waits for the message to complete, or timeout.
207 * If successful, it returns 0, otherwise a negative error number. The number
208 * of actual bytes transferred will be stored in the actual_length paramater.
210 * Don't use this function from within an interrupt context, like a bottom half
211 * handler. If you need an asynchronous message, or need to send a message
212 * from within interrupt context, use usb_submit_urb() If a thread in your
213 * driver uses this call, make sure your disconnect() method can wait for it to
214 * complete. Since you don't have a handle on the URB used, you can't cancel
217 * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT ioctl,
218 * users are forced to abuse this routine by using it to submit URBs for
219 * interrupt endpoints. We will take the liberty of creating an interrupt URB
220 * (with the default interval) if the target is an interrupt endpoint.
222 int usb_bulk_msg(struct usb_device
*usb_dev
, unsigned int pipe
,
223 void *data
, int len
, int *actual_length
, int timeout
)
226 struct usb_host_endpoint
*ep
;
228 ep
= (usb_pipein(pipe
) ? usb_dev
->ep_in
: usb_dev
->ep_out
)
229 [usb_pipeendpoint(pipe
)];
233 urb
= usb_alloc_urb(0, GFP_KERNEL
);
237 if ((ep
->desc
.bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) ==
238 USB_ENDPOINT_XFER_INT
) {
239 pipe
= (pipe
& ~(3 << 30)) | (PIPE_INTERRUPT
<< 30);
240 usb_fill_int_urb(urb
, usb_dev
, pipe
, data
, len
,
241 usb_api_blocking_completion
, NULL
,
244 usb_fill_bulk_urb(urb
, usb_dev
, pipe
, data
, len
,
245 usb_api_blocking_completion
, NULL
);
247 return usb_start_wait_urb(urb
, timeout
, actual_length
);
249 EXPORT_SYMBOL_GPL(usb_bulk_msg
);
251 /*-------------------------------------------------------------------*/
253 static void sg_clean(struct usb_sg_request
*io
)
256 while (io
->entries
--)
257 usb_free_urb(io
->urbs
[io
->entries
]);
261 if (io
->dev
->dev
.dma_mask
!= NULL
)
262 usb_buffer_unmap_sg(io
->dev
, usb_pipein(io
->pipe
),
267 static void sg_complete(struct urb
*urb
)
269 struct usb_sg_request
*io
= urb
->context
;
270 int status
= urb
->status
;
272 spin_lock(&io
->lock
);
274 /* In 2.5 we require hcds' endpoint queues not to progress after fault
275 * reports, until the completion callback (this!) returns. That lets
276 * device driver code (like this routine) unlink queued urbs first,
277 * if it needs to, since the HC won't work on them at all. So it's
278 * not possible for page N+1 to overwrite page N, and so on.
280 * That's only for "hard" faults; "soft" faults (unlinks) sometimes
281 * complete before the HCD can get requests away from hardware,
282 * though never during cleanup after a hard fault.
285 && (io
->status
!= -ECONNRESET
286 || status
!= -ECONNRESET
)
287 && urb
->actual_length
) {
288 dev_err(io
->dev
->bus
->controller
,
289 "dev %s ep%d%s scatterlist error %d/%d\n",
291 usb_endpoint_num(&urb
->ep
->desc
),
292 usb_urb_dir_in(urb
) ? "in" : "out",
297 if (io
->status
== 0 && status
&& status
!= -ECONNRESET
) {
298 int i
, found
, retval
;
302 /* the previous urbs, and this one, completed already.
303 * unlink pending urbs so they won't rx/tx bad data.
304 * careful: unlink can sometimes be synchronous...
306 spin_unlock(&io
->lock
);
307 for (i
= 0, found
= 0; i
< io
->entries
; i
++) {
308 if (!io
->urbs
[i
] || !io
->urbs
[i
]->dev
)
311 retval
= usb_unlink_urb(io
->urbs
[i
]);
312 if (retval
!= -EINPROGRESS
&&
315 dev_err(&io
->dev
->dev
,
316 "%s, unlink --> %d\n",
317 __FUNCTION__
, retval
);
318 } else if (urb
== io
->urbs
[i
])
321 spin_lock(&io
->lock
);
325 /* on the last completion, signal usb_sg_wait() */
326 io
->bytes
+= urb
->actual_length
;
329 complete(&io
->complete
);
331 spin_unlock(&io
->lock
);
336 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
337 * @io: request block being initialized. until usb_sg_wait() returns,
338 * treat this as a pointer to an opaque block of memory,
339 * @dev: the usb device that will send or receive the data
340 * @pipe: endpoint "pipe" used to transfer the data
341 * @period: polling rate for interrupt endpoints, in frames or
342 * (for high speed endpoints) microframes; ignored for bulk
343 * @sg: scatterlist entries
344 * @nents: how many entries in the scatterlist
345 * @length: how many bytes to send from the scatterlist, or zero to
346 * send every byte identified in the list.
347 * @mem_flags: SLAB_* flags affecting memory allocations in this call
349 * Returns zero for success, else a negative errno value. This initializes a
350 * scatter/gather request, allocating resources such as I/O mappings and urb
351 * memory (except maybe memory used by USB controller drivers).
353 * The request must be issued using usb_sg_wait(), which waits for the I/O to
354 * complete (or to be canceled) and then cleans up all resources allocated by
357 * The request may be canceled with usb_sg_cancel(), either before or after
358 * usb_sg_wait() is called.
360 int usb_sg_init(struct usb_sg_request
*io
, struct usb_device
*dev
,
361 unsigned pipe
, unsigned period
, struct scatterlist
*sg
,
362 int nents
, size_t length
, gfp_t mem_flags
)
368 if (!io
|| !dev
|| !sg
369 || usb_pipecontrol(pipe
)
370 || usb_pipeisoc(pipe
)
374 spin_lock_init(&io
->lock
);
380 /* not all host controllers use DMA (like the mainstream pci ones);
381 * they can use PIO (sl811) or be software over another transport.
383 dma
= (dev
->dev
.dma_mask
!= NULL
);
385 io
->entries
= usb_buffer_map_sg(dev
, usb_pipein(pipe
),
390 /* initialize all the urbs we'll use */
391 if (io
->entries
<= 0)
394 io
->urbs
= kmalloc(io
->entries
* sizeof *io
->urbs
, mem_flags
);
398 urb_flags
= URB_NO_TRANSFER_DMA_MAP
| URB_NO_INTERRUPT
;
399 if (usb_pipein(pipe
))
400 urb_flags
|= URB_SHORT_NOT_OK
;
402 for (i
= 0; i
< io
->entries
; i
++) {
405 io
->urbs
[i
] = usb_alloc_urb(0, mem_flags
);
411 io
->urbs
[i
]->dev
= NULL
;
412 io
->urbs
[i
]->pipe
= pipe
;
413 io
->urbs
[i
]->interval
= period
;
414 io
->urbs
[i
]->transfer_flags
= urb_flags
;
416 io
->urbs
[i
]->complete
= sg_complete
;
417 io
->urbs
[i
]->context
= io
;
420 * Some systems need to revert to PIO when DMA is temporarily
421 * unavailable. For their sakes, both transfer_buffer and
422 * transfer_dma are set when possible. However this can only
423 * work on systems without:
425 * - HIGHMEM, since DMA buffers located in high memory are
426 * not directly addressable by the CPU for PIO;
428 * - IOMMU, since dma_map_sg() is allowed to use an IOMMU to
429 * make virtually discontiguous buffers be "dma-contiguous"
430 * so that PIO and DMA need diferent numbers of URBs.
432 * So when HIGHMEM or IOMMU are in use, transfer_buffer is NULL
433 * to prevent stale pointers and to help spot bugs.
436 io
->urbs
[i
]->transfer_dma
= sg_dma_address(sg
+ i
);
437 len
= sg_dma_len(sg
+ i
);
438 #if defined(CONFIG_HIGHMEM) || defined(CONFIG_IOMMU)
439 io
->urbs
[i
]->transfer_buffer
= NULL
;
441 io
->urbs
[i
]->transfer_buffer
= sg_virt(&sg
[i
]);
444 /* hc may use _only_ transfer_buffer */
445 io
->urbs
[i
]->transfer_buffer
= sg_virt(&sg
[i
]);
450 len
= min_t(unsigned, len
, length
);
455 io
->urbs
[i
]->transfer_buffer_length
= len
;
457 io
->urbs
[--i
]->transfer_flags
&= ~URB_NO_INTERRUPT
;
459 /* transaction state */
460 io
->count
= io
->entries
;
463 init_completion(&io
->complete
);
470 EXPORT_SYMBOL_GPL(usb_sg_init
);
473 * usb_sg_wait - synchronously execute scatter/gather request
474 * @io: request block handle, as initialized with usb_sg_init().
475 * some fields become accessible when this call returns.
476 * Context: !in_interrupt ()
478 * This function blocks until the specified I/O operation completes. It
479 * leverages the grouping of the related I/O requests to get good transfer
480 * rates, by queueing the requests. At higher speeds, such queuing can
481 * significantly improve USB throughput.
483 * There are three kinds of completion for this function.
484 * (1) success, where io->status is zero. The number of io->bytes
485 * transferred is as requested.
486 * (2) error, where io->status is a negative errno value. The number
487 * of io->bytes transferred before the error is usually less
488 * than requested, and can be nonzero.
489 * (3) cancellation, a type of error with status -ECONNRESET that
490 * is initiated by usb_sg_cancel().
492 * When this function returns, all memory allocated through usb_sg_init() or
493 * this call will have been freed. The request block parameter may still be
494 * passed to usb_sg_cancel(), or it may be freed. It could also be
495 * reinitialized and then reused.
497 * Data Transfer Rates:
499 * Bulk transfers are valid for full or high speed endpoints.
500 * The best full speed data rate is 19 packets of 64 bytes each
501 * per frame, or 1216 bytes per millisecond.
502 * The best high speed data rate is 13 packets of 512 bytes each
503 * per microframe, or 52 KBytes per millisecond.
505 * The reason to use interrupt transfers through this API would most likely
506 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
507 * could be transferred. That capability is less useful for low or full
508 * speed interrupt endpoints, which allow at most one packet per millisecond,
509 * of at most 8 or 64 bytes (respectively).
511 void usb_sg_wait(struct usb_sg_request
*io
)
514 int entries
= io
->entries
;
516 /* queue the urbs. */
517 spin_lock_irq(&io
->lock
);
519 while (i
< entries
&& !io
->status
) {
522 io
->urbs
[i
]->dev
= io
->dev
;
523 retval
= usb_submit_urb(io
->urbs
[i
], GFP_ATOMIC
);
525 /* after we submit, let completions or cancelations fire;
526 * we handshake using io->status.
528 spin_unlock_irq(&io
->lock
);
530 /* maybe we retrying will recover */
531 case -ENXIO
: /* hc didn't queue this one */
534 io
->urbs
[i
]->dev
= NULL
;
539 /* no error? continue immediately.
541 * NOTE: to work better with UHCI (4K I/O buffer may
542 * need 3K of TDs) it may be good to limit how many
543 * URBs are queued at once; N milliseconds?
550 /* fail any uncompleted urbs */
552 io
->urbs
[i
]->dev
= NULL
;
553 io
->urbs
[i
]->status
= retval
;
554 dev_dbg(&io
->dev
->dev
, "%s, submit --> %d\n",
555 __FUNCTION__
, retval
);
558 spin_lock_irq(&io
->lock
);
559 if (retval
&& (io
->status
== 0 || io
->status
== -ECONNRESET
))
562 io
->count
-= entries
- i
;
564 complete(&io
->complete
);
565 spin_unlock_irq(&io
->lock
);
567 /* OK, yes, this could be packaged as non-blocking.
568 * So could the submit loop above ... but it's easier to
569 * solve neither problem than to solve both!
571 wait_for_completion(&io
->complete
);
575 EXPORT_SYMBOL_GPL(usb_sg_wait
);
578 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
579 * @io: request block, initialized with usb_sg_init()
581 * This stops a request after it has been started by usb_sg_wait().
582 * It can also prevents one initialized by usb_sg_init() from starting,
583 * so that call just frees resources allocated to the request.
585 void usb_sg_cancel(struct usb_sg_request
*io
)
589 spin_lock_irqsave(&io
->lock
, flags
);
591 /* shut everything down, if it didn't already */
595 io
->status
= -ECONNRESET
;
596 spin_unlock(&io
->lock
);
597 for (i
= 0; i
< io
->entries
; i
++) {
600 if (!io
->urbs
[i
]->dev
)
602 retval
= usb_unlink_urb(io
->urbs
[i
]);
603 if (retval
!= -EINPROGRESS
&& retval
!= -EBUSY
)
604 dev_warn(&io
->dev
->dev
, "%s, unlink --> %d\n",
605 __FUNCTION__
, retval
);
607 spin_lock(&io
->lock
);
609 spin_unlock_irqrestore(&io
->lock
, flags
);
611 EXPORT_SYMBOL_GPL(usb_sg_cancel
);
613 /*-------------------------------------------------------------------*/
616 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
617 * @dev: the device whose descriptor is being retrieved
618 * @type: the descriptor type (USB_DT_*)
619 * @index: the number of the descriptor
620 * @buf: where to put the descriptor
621 * @size: how big is "buf"?
622 * Context: !in_interrupt ()
624 * Gets a USB descriptor. Convenience functions exist to simplify
625 * getting some types of descriptors. Use
626 * usb_get_string() or usb_string() for USB_DT_STRING.
627 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
628 * are part of the device structure.
629 * In addition to a number of USB-standard descriptors, some
630 * devices also use class-specific or vendor-specific descriptors.
632 * This call is synchronous, and may not be used in an interrupt context.
634 * Returns the number of bytes received on success, or else the status code
635 * returned by the underlying usb_control_msg() call.
637 int usb_get_descriptor(struct usb_device
*dev
, unsigned char type
,
638 unsigned char index
, void *buf
, int size
)
643 memset(buf
, 0, size
); /* Make sure we parse really received data */
645 for (i
= 0; i
< 3; ++i
) {
646 /* retry on length 0 or error; some devices are flakey */
647 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
648 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
,
649 (type
<< 8) + index
, 0, buf
, size
,
650 USB_CTRL_GET_TIMEOUT
);
651 if (result
<= 0 && result
!= -ETIMEDOUT
)
653 if (result
> 1 && ((u8
*)buf
)[1] != type
) {
661 EXPORT_SYMBOL_GPL(usb_get_descriptor
);
664 * usb_get_string - gets a string descriptor
665 * @dev: the device whose string descriptor is being retrieved
666 * @langid: code for language chosen (from string descriptor zero)
667 * @index: the number of the descriptor
668 * @buf: where to put the string
669 * @size: how big is "buf"?
670 * Context: !in_interrupt ()
672 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
673 * in little-endian byte order).
674 * The usb_string() function will often be a convenient way to turn
675 * these strings into kernel-printable form.
677 * Strings may be referenced in device, configuration, interface, or other
678 * descriptors, and could also be used in vendor-specific ways.
680 * This call is synchronous, and may not be used in an interrupt context.
682 * Returns the number of bytes received on success, or else the status code
683 * returned by the underlying usb_control_msg() call.
685 static int usb_get_string(struct usb_device
*dev
, unsigned short langid
,
686 unsigned char index
, void *buf
, int size
)
691 for (i
= 0; i
< 3; ++i
) {
692 /* retry on length 0 or stall; some devices are flakey */
693 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
694 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
,
695 (USB_DT_STRING
<< 8) + index
, langid
, buf
, size
,
696 USB_CTRL_GET_TIMEOUT
);
697 if (result
== 0 || result
== -EPIPE
)
699 if (result
> 1 && ((u8
*) buf
)[1] != USB_DT_STRING
) {
708 static void usb_try_string_workarounds(unsigned char *buf
, int *length
)
710 int newlength
, oldlength
= *length
;
712 for (newlength
= 2; newlength
+ 1 < oldlength
; newlength
+= 2)
713 if (!isprint(buf
[newlength
]) || buf
[newlength
+ 1])
722 static int usb_string_sub(struct usb_device
*dev
, unsigned int langid
,
723 unsigned int index
, unsigned char *buf
)
727 /* Try to read the string descriptor by asking for the maximum
728 * possible number of bytes */
729 if (dev
->quirks
& USB_QUIRK_STRING_FETCH_255
)
732 rc
= usb_get_string(dev
, langid
, index
, buf
, 255);
734 /* If that failed try to read the descriptor length, then
735 * ask for just that many bytes */
737 rc
= usb_get_string(dev
, langid
, index
, buf
, 2);
739 rc
= usb_get_string(dev
, langid
, index
, buf
, buf
[0]);
743 if (!buf
[0] && !buf
[1])
744 usb_try_string_workarounds(buf
, &rc
);
746 /* There might be extra junk at the end of the descriptor */
750 rc
= rc
- (rc
& 1); /* force a multiple of two */
754 rc
= (rc
< 0 ? rc
: -EINVAL
);
759 static int usb_get_langid(struct usb_device
*dev
, unsigned char *tbuf
)
763 if (dev
->have_langid
)
766 if (dev
->string_langid
< 0)
769 err
= usb_string_sub(dev
, 0, 0, tbuf
);
771 /* If the string was reported but is malformed, default to english
773 if (err
== -ENODATA
|| (err
> 0 && err
< 4)) {
774 dev
->string_langid
= 0x0409;
775 dev
->have_langid
= 1;
777 "string descriptor 0 malformed (err = %d), "
778 "defaulting to 0x%04x\n",
779 err
, dev
->string_langid
);
783 /* In case of all other errors, we assume the device is not able to
784 * deal with strings at all. Set string_langid to -1 in order to
785 * prevent any string to be retrieved from the device */
787 dev_err(&dev
->dev
, "string descriptor 0 read error: %d\n",
789 dev
->string_langid
= -1;
793 /* always use the first langid listed */
794 dev
->string_langid
= tbuf
[2] | (tbuf
[3] << 8);
795 dev
->have_langid
= 1;
796 dev_dbg(&dev
->dev
, "default language 0x%04x\n",
802 * usb_string - returns UTF-8 version of a string descriptor
803 * @dev: the device whose string descriptor is being retrieved
804 * @index: the number of the descriptor
805 * @buf: where to put the string
806 * @size: how big is "buf"?
807 * Context: !in_interrupt ()
809 * This converts the UTF-16LE encoded strings returned by devices, from
810 * usb_get_string_descriptor(), to null-terminated UTF-8 encoded ones
811 * that are more usable in most kernel contexts. Note that this function
812 * chooses strings in the first language supported by the device.
814 * This call is synchronous, and may not be used in an interrupt context.
816 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
818 int usb_string(struct usb_device
*dev
, int index
, char *buf
, size_t size
)
824 if (dev
->state
== USB_STATE_SUSPENDED
)
825 return -EHOSTUNREACH
;
826 if (size
<= 0 || !buf
|| !index
)
829 tbuf
= kmalloc(256 + 2, GFP_NOIO
);
833 err
= usb_get_langid(dev
, tbuf
);
837 err
= usb_string_sub(dev
, dev
->string_langid
, index
, tbuf
);
841 for (u
= 2; u
< err
; u
+= 2)
842 le16_to_cpus((u16
*)&tbuf
[u
]);
845 size
--; /* leave room for trailing NULL char in output buffer */
846 err
= utf8_wcstombs(buf
, (u16
*)&tbuf
[2], size
);
849 if (tbuf
[1] != USB_DT_STRING
)
851 "wrong descriptor type %02x for string %d (\"%s\")\n",
852 tbuf
[1], index
, buf
);
858 EXPORT_SYMBOL_GPL(usb_string
);
860 /* one UTF-8-encoded 16-bit character has at most three bytes */
861 #define MAX_USB_STRING_SIZE (127 * 3 + 1)
864 * usb_cache_string - read a string descriptor and cache it for later use
865 * @udev: the device whose string descriptor is being read
866 * @index: the descriptor index
868 * Returns a pointer to a kmalloc'ed buffer containing the descriptor string,
869 * or NULL if the index is 0 or the string could not be read.
871 char *usb_cache_string(struct usb_device
*udev
, int index
)
874 char *smallbuf
= NULL
;
880 buf
= kmalloc(MAX_USB_STRING_SIZE
, GFP_NOIO
);
882 len
= usb_string(udev
, index
, buf
, MAX_USB_STRING_SIZE
);
884 smallbuf
= kmalloc(++len
, GFP_NOIO
);
887 memcpy(smallbuf
, buf
, len
);
895 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
896 * @dev: the device whose device descriptor is being updated
897 * @size: how much of the descriptor to read
898 * Context: !in_interrupt ()
900 * Updates the copy of the device descriptor stored in the device structure,
901 * which dedicates space for this purpose.
903 * Not exported, only for use by the core. If drivers really want to read
904 * the device descriptor directly, they can call usb_get_descriptor() with
905 * type = USB_DT_DEVICE and index = 0.
907 * This call is synchronous, and may not be used in an interrupt context.
909 * Returns the number of bytes received on success, or else the status code
910 * returned by the underlying usb_control_msg() call.
912 int usb_get_device_descriptor(struct usb_device
*dev
, unsigned int size
)
914 struct usb_device_descriptor
*desc
;
917 if (size
> sizeof(*desc
))
919 desc
= kmalloc(sizeof(*desc
), GFP_NOIO
);
923 ret
= usb_get_descriptor(dev
, USB_DT_DEVICE
, 0, desc
, size
);
925 memcpy(&dev
->descriptor
, desc
, size
);
931 * usb_get_status - issues a GET_STATUS call
932 * @dev: the device whose status is being checked
933 * @type: USB_RECIP_*; for device, interface, or endpoint
934 * @target: zero (for device), else interface or endpoint number
935 * @data: pointer to two bytes of bitmap data
936 * Context: !in_interrupt ()
938 * Returns device, interface, or endpoint status. Normally only of
939 * interest to see if the device is self powered, or has enabled the
940 * remote wakeup facility; or whether a bulk or interrupt endpoint
941 * is halted ("stalled").
943 * Bits in these status bitmaps are set using the SET_FEATURE request,
944 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
945 * function should be used to clear halt ("stall") status.
947 * This call is synchronous, and may not be used in an interrupt context.
949 * Returns the number of bytes received on success, or else the status code
950 * returned by the underlying usb_control_msg() call.
952 int usb_get_status(struct usb_device
*dev
, int type
, int target
, void *data
)
955 u16
*status
= kmalloc(sizeof(*status
), GFP_KERNEL
);
960 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
961 USB_REQ_GET_STATUS
, USB_DIR_IN
| type
, 0, target
, status
,
962 sizeof(*status
), USB_CTRL_GET_TIMEOUT
);
964 *(u16
*)data
= *status
;
968 EXPORT_SYMBOL_GPL(usb_get_status
);
971 * usb_clear_halt - tells device to clear endpoint halt/stall condition
972 * @dev: device whose endpoint is halted
973 * @pipe: endpoint "pipe" being cleared
974 * Context: !in_interrupt ()
976 * This is used to clear halt conditions for bulk and interrupt endpoints,
977 * as reported by URB completion status. Endpoints that are halted are
978 * sometimes referred to as being "stalled". Such endpoints are unable
979 * to transmit or receive data until the halt status is cleared. Any URBs
980 * queued for such an endpoint should normally be unlinked by the driver
981 * before clearing the halt condition, as described in sections 5.7.5
982 * and 5.8.5 of the USB 2.0 spec.
984 * Note that control and isochronous endpoints don't halt, although control
985 * endpoints report "protocol stall" (for unsupported requests) using the
986 * same status code used to report a true stall.
988 * This call is synchronous, and may not be used in an interrupt context.
990 * Returns zero on success, or else the status code returned by the
991 * underlying usb_control_msg() call.
993 int usb_clear_halt(struct usb_device
*dev
, int pipe
)
996 int endp
= usb_pipeendpoint(pipe
);
998 if (usb_pipein(pipe
))
1001 /* we don't care if it wasn't halted first. in fact some devices
1002 * (like some ibmcam model 1 units) seem to expect hosts to make
1003 * this request for iso endpoints, which can't halt!
1005 result
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
1006 USB_REQ_CLEAR_FEATURE
, USB_RECIP_ENDPOINT
,
1007 USB_ENDPOINT_HALT
, endp
, NULL
, 0,
1008 USB_CTRL_SET_TIMEOUT
);
1010 /* don't un-halt or force to DATA0 except on success */
1014 /* NOTE: seems like Microsoft and Apple don't bother verifying
1015 * the clear "took", so some devices could lock up if you check...
1016 * such as the Hagiwara FlashGate DUAL. So we won't bother.
1018 * NOTE: make sure the logic here doesn't diverge much from
1019 * the copy in usb-storage, for as long as we need two copies.
1022 /* toggle was reset by the clear */
1023 usb_settoggle(dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
), 0);
1027 EXPORT_SYMBOL_GPL(usb_clear_halt
);
1030 * usb_disable_endpoint -- Disable an endpoint by address
1031 * @dev: the device whose endpoint is being disabled
1032 * @epaddr: the endpoint's address. Endpoint number for output,
1033 * endpoint number + USB_DIR_IN for input
1035 * Deallocates hcd/hardware state for this endpoint ... and nukes all
1038 * If the HCD hasn't registered a disable() function, this sets the
1039 * endpoint's maxpacket size to 0 to prevent further submissions.
1041 void usb_disable_endpoint(struct usb_device
*dev
, unsigned int epaddr
)
1043 unsigned int epnum
= epaddr
& USB_ENDPOINT_NUMBER_MASK
;
1044 struct usb_host_endpoint
*ep
;
1049 if (usb_endpoint_out(epaddr
)) {
1050 ep
= dev
->ep_out
[epnum
];
1051 dev
->ep_out
[epnum
] = NULL
;
1053 ep
= dev
->ep_in
[epnum
];
1054 dev
->ep_in
[epnum
] = NULL
;
1058 usb_hcd_flush_endpoint(dev
, ep
);
1059 usb_hcd_disable_endpoint(dev
, ep
);
1064 * usb_disable_interface -- Disable all endpoints for an interface
1065 * @dev: the device whose interface is being disabled
1066 * @intf: pointer to the interface descriptor
1068 * Disables all the endpoints for the interface's current altsetting.
1070 void usb_disable_interface(struct usb_device
*dev
, struct usb_interface
*intf
)
1072 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
1075 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; ++i
) {
1076 usb_disable_endpoint(dev
,
1077 alt
->endpoint
[i
].desc
.bEndpointAddress
);
1082 * usb_disable_device - Disable all the endpoints for a USB device
1083 * @dev: the device whose endpoints are being disabled
1084 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
1086 * Disables all the device's endpoints, potentially including endpoint 0.
1087 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1088 * pending urbs) and usbcore state for the interfaces, so that usbcore
1089 * must usb_set_configuration() before any interfaces could be used.
1091 void usb_disable_device(struct usb_device
*dev
, int skip_ep0
)
1095 dev
->toggle
[0] = dev
->toggle
[1] = 0;
1097 /* getting rid of interfaces will disconnect
1098 * any drivers bound to them (a key side effect)
1100 if (dev
->actconfig
) {
1101 for (i
= 0; i
< dev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1102 struct usb_interface
*interface
;
1104 /* remove this interface if it has been registered */
1105 interface
= dev
->actconfig
->interface
[i
];
1106 if (!device_is_registered(&interface
->dev
))
1108 dev_dbg(&dev
->dev
, "unregistering interface %s\n",
1109 interface
->dev
.bus_id
);
1110 interface
->unregistering
= 1;
1111 usb_remove_sysfs_intf_files(interface
);
1112 device_del(&interface
->dev
);
1115 /* Now that the interfaces are unbound, nobody should
1116 * try to access them.
1118 for (i
= 0; i
< dev
->actconfig
->desc
.bNumInterfaces
; i
++) {
1119 put_device(&dev
->actconfig
->interface
[i
]->dev
);
1120 dev
->actconfig
->interface
[i
] = NULL
;
1122 dev
->actconfig
= NULL
;
1123 if (dev
->state
== USB_STATE_CONFIGURED
)
1124 usb_set_device_state(dev
, USB_STATE_ADDRESS
);
1127 dev_dbg(&dev
->dev
, "%s nuking %s URBs\n", __FUNCTION__
,
1128 skip_ep0
? "non-ep0" : "all");
1129 for (i
= skip_ep0
; i
< 16; ++i
) {
1130 usb_disable_endpoint(dev
, i
);
1131 usb_disable_endpoint(dev
, i
+ USB_DIR_IN
);
1136 * usb_enable_endpoint - Enable an endpoint for USB communications
1137 * @dev: the device whose interface is being enabled
1140 * Resets the endpoint toggle, and sets dev->ep_{in,out} pointers.
1141 * For control endpoints, both the input and output sides are handled.
1143 void usb_enable_endpoint(struct usb_device
*dev
, struct usb_host_endpoint
*ep
)
1145 int epnum
= usb_endpoint_num(&ep
->desc
);
1146 int is_out
= usb_endpoint_dir_out(&ep
->desc
);
1147 int is_control
= usb_endpoint_xfer_control(&ep
->desc
);
1149 if (is_out
|| is_control
) {
1150 usb_settoggle(dev
, epnum
, 1, 0);
1151 dev
->ep_out
[epnum
] = ep
;
1153 if (!is_out
|| is_control
) {
1154 usb_settoggle(dev
, epnum
, 0, 0);
1155 dev
->ep_in
[epnum
] = ep
;
1161 * usb_enable_interface - Enable all the endpoints for an interface
1162 * @dev: the device whose interface is being enabled
1163 * @intf: pointer to the interface descriptor
1165 * Enables all the endpoints for the interface's current altsetting.
1167 static void usb_enable_interface(struct usb_device
*dev
,
1168 struct usb_interface
*intf
)
1170 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
1173 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; ++i
)
1174 usb_enable_endpoint(dev
, &alt
->endpoint
[i
]);
1178 * usb_set_interface - Makes a particular alternate setting be current
1179 * @dev: the device whose interface is being updated
1180 * @interface: the interface being updated
1181 * @alternate: the setting being chosen.
1182 * Context: !in_interrupt ()
1184 * This is used to enable data transfers on interfaces that may not
1185 * be enabled by default. Not all devices support such configurability.
1186 * Only the driver bound to an interface may change its setting.
1188 * Within any given configuration, each interface may have several
1189 * alternative settings. These are often used to control levels of
1190 * bandwidth consumption. For example, the default setting for a high
1191 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1192 * while interrupt transfers of up to 3KBytes per microframe are legal.
1193 * Also, isochronous endpoints may never be part of an
1194 * interface's default setting. To access such bandwidth, alternate
1195 * interface settings must be made current.
1197 * Note that in the Linux USB subsystem, bandwidth associated with
1198 * an endpoint in a given alternate setting is not reserved until an URB
1199 * is submitted that needs that bandwidth. Some other operating systems
1200 * allocate bandwidth early, when a configuration is chosen.
1202 * This call is synchronous, and may not be used in an interrupt context.
1203 * Also, drivers must not change altsettings while urbs are scheduled for
1204 * endpoints in that interface; all such urbs must first be completed
1205 * (perhaps forced by unlinking).
1207 * Returns zero on success, or else the status code returned by the
1208 * underlying usb_control_msg() call.
1210 int usb_set_interface(struct usb_device
*dev
, int interface
, int alternate
)
1212 struct usb_interface
*iface
;
1213 struct usb_host_interface
*alt
;
1216 unsigned int epaddr
;
1219 if (dev
->state
== USB_STATE_SUSPENDED
)
1220 return -EHOSTUNREACH
;
1222 iface
= usb_ifnum_to_if(dev
, interface
);
1224 dev_dbg(&dev
->dev
, "selecting invalid interface %d\n",
1229 alt
= usb_altnum_to_altsetting(iface
, alternate
);
1231 warn("selecting invalid altsetting %d\n", alternate
);
1235 if (dev
->quirks
& USB_QUIRK_NO_SET_INTF
)
1238 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
1239 USB_REQ_SET_INTERFACE
, USB_RECIP_INTERFACE
,
1240 alternate
, interface
, NULL
, 0, 5000);
1242 /* 9.4.10 says devices don't need this and are free to STALL the
1243 * request if the interface only has one alternate setting.
1245 if (ret
== -EPIPE
&& iface
->num_altsetting
== 1) {
1247 "manual set_interface for iface %d, alt %d\n",
1248 interface
, alternate
);
1253 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1254 * when they implement async or easily-killable versions of this or
1255 * other "should-be-internal" functions (like clear_halt).
1256 * should hcd+usbcore postprocess control requests?
1259 /* prevent submissions using previous endpoint settings */
1260 if (iface
->cur_altsetting
!= alt
)
1261 usb_remove_sysfs_intf_files(iface
);
1262 usb_disable_interface(dev
, iface
);
1264 iface
->cur_altsetting
= alt
;
1266 /* If the interface only has one altsetting and the device didn't
1267 * accept the request, we attempt to carry out the equivalent action
1268 * by manually clearing the HALT feature for each endpoint in the
1274 for (i
= 0; i
< alt
->desc
.bNumEndpoints
; i
++) {
1275 epaddr
= alt
->endpoint
[i
].desc
.bEndpointAddress
;
1276 pipe
= __create_pipe(dev
,
1277 USB_ENDPOINT_NUMBER_MASK
& epaddr
) |
1278 (usb_endpoint_out(epaddr
) ?
1279 USB_DIR_OUT
: USB_DIR_IN
);
1281 usb_clear_halt(dev
, pipe
);
1285 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1288 * Despite EP0 is always present in all interfaces/AS, the list of
1289 * endpoints from the descriptor does not contain EP0. Due to its
1290 * omnipresence one might expect EP0 being considered "affected" by
1291 * any SetInterface request and hence assume toggles need to be reset.
1292 * However, EP0 toggles are re-synced for every individual transfer
1293 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1294 * (Likewise, EP0 never "halts" on well designed devices.)
1296 usb_enable_interface(dev
, iface
);
1297 if (device_is_registered(&iface
->dev
))
1298 usb_create_sysfs_intf_files(iface
);
1302 EXPORT_SYMBOL_GPL(usb_set_interface
);
1305 * usb_reset_configuration - lightweight device reset
1306 * @dev: the device whose configuration is being reset
1308 * This issues a standard SET_CONFIGURATION request to the device using
1309 * the current configuration. The effect is to reset most USB-related
1310 * state in the device, including interface altsettings (reset to zero),
1311 * endpoint halts (cleared), and data toggle (only for bulk and interrupt
1312 * endpoints). Other usbcore state is unchanged, including bindings of
1313 * usb device drivers to interfaces.
1315 * Because this affects multiple interfaces, avoid using this with composite
1316 * (multi-interface) devices. Instead, the driver for each interface may
1317 * use usb_set_interface() on the interfaces it claims. Be careful though;
1318 * some devices don't support the SET_INTERFACE request, and others won't
1319 * reset all the interface state (notably data toggles). Resetting the whole
1320 * configuration would affect other drivers' interfaces.
1322 * The caller must own the device lock.
1324 * Returns zero on success, else a negative error code.
1326 int usb_reset_configuration(struct usb_device
*dev
)
1329 struct usb_host_config
*config
;
1331 if (dev
->state
== USB_STATE_SUSPENDED
)
1332 return -EHOSTUNREACH
;
1334 /* caller must have locked the device and must own
1335 * the usb bus readlock (so driver bindings are stable);
1336 * calls during probe() are fine
1339 for (i
= 1; i
< 16; ++i
) {
1340 usb_disable_endpoint(dev
, i
);
1341 usb_disable_endpoint(dev
, i
+ USB_DIR_IN
);
1344 config
= dev
->actconfig
;
1345 retval
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
1346 USB_REQ_SET_CONFIGURATION
, 0,
1347 config
->desc
.bConfigurationValue
, 0,
1348 NULL
, 0, USB_CTRL_SET_TIMEOUT
);
1352 dev
->toggle
[0] = dev
->toggle
[1] = 0;
1354 /* re-init hc/hcd interface/endpoint state */
1355 for (i
= 0; i
< config
->desc
.bNumInterfaces
; i
++) {
1356 struct usb_interface
*intf
= config
->interface
[i
];
1357 struct usb_host_interface
*alt
;
1359 usb_remove_sysfs_intf_files(intf
);
1360 alt
= usb_altnum_to_altsetting(intf
, 0);
1362 /* No altsetting 0? We'll assume the first altsetting.
1363 * We could use a GetInterface call, but if a device is
1364 * so non-compliant that it doesn't have altsetting 0
1365 * then I wouldn't trust its reply anyway.
1368 alt
= &intf
->altsetting
[0];
1370 intf
->cur_altsetting
= alt
;
1371 usb_enable_interface(dev
, intf
);
1372 if (device_is_registered(&intf
->dev
))
1373 usb_create_sysfs_intf_files(intf
);
1377 EXPORT_SYMBOL_GPL(usb_reset_configuration
);
1379 static void usb_release_interface(struct device
*dev
)
1381 struct usb_interface
*intf
= to_usb_interface(dev
);
1382 struct usb_interface_cache
*intfc
=
1383 altsetting_to_usb_interface_cache(intf
->altsetting
);
1385 kref_put(&intfc
->ref
, usb_release_interface_cache
);
1389 #ifdef CONFIG_HOTPLUG
1390 static int usb_if_uevent(struct device
*dev
, char **envp
, int num_envp
,
1391 char *buffer
, int buffer_size
)
1393 struct usb_device
*usb_dev
;
1394 struct usb_interface
*intf
;
1395 struct usb_host_interface
*alt
;
1402 /* driver is often null here; dev_dbg() would oops */
1403 pr_debug ("usb %s: uevent\n", dev
->bus_id
);
1405 intf
= to_usb_interface(dev
);
1406 usb_dev
= interface_to_usbdev(intf
);
1407 alt
= intf
->cur_altsetting
;
1409 #ifdef CONFIG_USB_DEVICEFS
1410 if (add_uevent_var(envp
, num_envp
, &i
,
1411 buffer
, buffer_size
, &length
,
1412 "DEVICE=/proc/bus/usb/%03d/%03d",
1413 usb_dev
->bus
->busnum
, usb_dev
->devnum
))
1417 if (add_uevent_var(envp
, num_envp
, &i
,
1418 buffer
, buffer_size
, &length
,
1420 le16_to_cpu(usb_dev
->descriptor
.idVendor
),
1421 le16_to_cpu(usb_dev
->descriptor
.idProduct
),
1422 le16_to_cpu(usb_dev
->descriptor
.bcdDevice
)))
1425 if (add_uevent_var(envp
, num_envp
, &i
,
1426 buffer
, buffer_size
, &length
,
1428 usb_dev
->descriptor
.bDeviceClass
,
1429 usb_dev
->descriptor
.bDeviceSubClass
,
1430 usb_dev
->descriptor
.bDeviceProtocol
))
1433 if (add_uevent_var(envp
, num_envp
, &i
,
1434 buffer
, buffer_size
, &length
,
1435 "INTERFACE=%d/%d/%d",
1436 alt
->desc
.bInterfaceClass
,
1437 alt
->desc
.bInterfaceSubClass
,
1438 alt
->desc
.bInterfaceProtocol
))
1441 if (add_uevent_var(envp
, num_envp
, &i
,
1442 buffer
, buffer_size
, &length
,
1443 "USBDEVICE_PATH=%s",
1447 if (add_uevent_var(envp
, num_envp
, &i
,
1448 buffer
, buffer_size
, &length
,
1450 "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02Xin%02X",
1451 le16_to_cpu(usb_dev
->descriptor
.idVendor
),
1452 le16_to_cpu(usb_dev
->descriptor
.idProduct
),
1453 le16_to_cpu(usb_dev
->descriptor
.bcdDevice
),
1454 usb_dev
->descriptor
.bDeviceClass
,
1455 usb_dev
->descriptor
.bDeviceSubClass
,
1456 usb_dev
->descriptor
.bDeviceProtocol
,
1457 alt
->desc
.bInterfaceClass
,
1458 alt
->desc
.bInterfaceSubClass
,
1459 alt
->desc
.bInterfaceProtocol
,
1460 alt
->desc
.bInterfaceNumber
))
1469 static int usb_if_uevent(struct device
*dev
, char **envp
,
1470 int num_envp
, char *buffer
, int buffer_size
)
1474 #endif /* CONFIG_HOTPLUG */
1476 struct device_type usb_if_device_type
= {
1477 .name
= "usb_interface",
1478 .release
= usb_release_interface
,
1479 .uevent
= usb_if_uevent
,
1482 static struct usb_interface_assoc_descriptor
*find_iad(struct usb_device
*dev
,
1483 struct usb_host_config
*config
,
1486 struct usb_interface_assoc_descriptor
*retval
= NULL
;
1487 struct usb_interface_assoc_descriptor
*intf_assoc
;
1492 for (i
= 0; (i
< USB_MAXIADS
&& config
->intf_assoc
[i
]); i
++) {
1493 intf_assoc
= config
->intf_assoc
[i
];
1494 if (intf_assoc
->bInterfaceCount
== 0)
1497 first_intf
= intf_assoc
->bFirstInterface
;
1498 last_intf
= first_intf
+ (intf_assoc
->bInterfaceCount
- 1);
1499 if (inum
>= first_intf
&& inum
<= last_intf
) {
1501 retval
= intf_assoc
;
1503 dev_err(&dev
->dev
, "Interface #%d referenced"
1504 " by multiple IADs\n", inum
);
1513 * Internal function to queue a device reset
1515 * This is initialized into the workstruct in 'struct
1516 * usb_device->reset_ws' that is launched by
1517 * message.c:usb_set_configuration() when initializing each 'struct
1520 * It is safe to get the USB device without reference counts because
1521 * the life cycle of @iface is bound to the life cycle of @udev. Then,
1522 * this function will be ran only if @iface is alive (and before
1523 * freeing it any scheduled instances of it will have been cancelled).
1525 * We need to set a flag (usb_dev->reset_running) because when we call
1526 * the reset, the interfaces might be unbound. The current interface
1527 * cannot try to remove the queued work as it would cause a deadlock
1528 * (you cannot remove your work from within your executing
1529 * workqueue). This flag lets it know, so that
1530 * usb_cancel_queued_reset() doesn't try to do it.
1532 * See usb_queue_reset_device() for more details
1534 static void __usb_queue_reset_device(struct work_struct
*ws
)
1537 struct usb_interface
*iface
=
1538 container_of(ws
, struct usb_interface
, reset_ws
);
1539 struct usb_device
*udev
= interface_to_usbdev(iface
);
1541 rc
= usb_lock_device_for_reset(udev
, iface
);
1543 iface
->reset_running
= 1;
1544 usb_reset_device(udev
);
1545 iface
->reset_running
= 0;
1546 usb_unlock_device(udev
);
1552 * usb_set_configuration - Makes a particular device setting be current
1553 * @dev: the device whose configuration is being updated
1554 * @configuration: the configuration being chosen.
1555 * Context: !in_interrupt(), caller owns the device lock
1557 * This is used to enable non-default device modes. Not all devices
1558 * use this kind of configurability; many devices only have one
1561 * @configuration is the value of the configuration to be installed.
1562 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1563 * must be non-zero; a value of zero indicates that the device in
1564 * unconfigured. However some devices erroneously use 0 as one of their
1565 * configuration values. To help manage such devices, this routine will
1566 * accept @configuration = -1 as indicating the device should be put in
1567 * an unconfigured state.
1569 * USB device configurations may affect Linux interoperability,
1570 * power consumption and the functionality available. For example,
1571 * the default configuration is limited to using 100mA of bus power,
1572 * so that when certain device functionality requires more power,
1573 * and the device is bus powered, that functionality should be in some
1574 * non-default device configuration. Other device modes may also be
1575 * reflected as configuration options, such as whether two ISDN
1576 * channels are available independently; and choosing between open
1577 * standard device protocols (like CDC) or proprietary ones.
1579 * Note that USB has an additional level of device configurability,
1580 * associated with interfaces. That configurability is accessed using
1581 * usb_set_interface().
1583 * This call is synchronous. The calling context must be able to sleep,
1584 * must own the device lock, and must not hold the driver model's USB
1585 * bus mutex; usb interface driver probe() methods cannot use this routine.
1587 * Returns zero on success, or else the status code returned by the
1588 * underlying call that failed. On successful completion, each interface
1589 * in the original device configuration has been destroyed, and each one
1590 * in the new configuration has been probed by all relevant usb device
1591 * drivers currently known to the kernel.
1593 int usb_set_configuration(struct usb_device
*dev
, int configuration
)
1596 struct usb_host_config
*cp
= NULL
;
1597 struct usb_interface
**new_interfaces
= NULL
;
1600 if (configuration
== -1)
1603 for (i
= 0; i
< dev
->descriptor
.bNumConfigurations
; i
++) {
1604 if (dev
->config
[i
].desc
.bConfigurationValue
==
1606 cp
= &dev
->config
[i
];
1611 if ((!cp
&& configuration
!= 0))
1614 /* The USB spec says configuration 0 means unconfigured.
1615 * But if a device includes a configuration numbered 0,
1616 * we will accept it as a correctly configured state.
1617 * Use -1 if you really want to unconfigure the device.
1619 if (cp
&& configuration
== 0)
1620 dev_warn(&dev
->dev
, "config 0 descriptor??\n");
1622 /* Allocate memory for new interfaces before doing anything else,
1623 * so that if we run out then nothing will have changed. */
1626 nintf
= cp
->desc
.bNumInterfaces
;
1627 new_interfaces
= kmalloc(nintf
* sizeof(*new_interfaces
),
1629 if (!new_interfaces
) {
1630 dev_err(&dev
->dev
, "Out of memory\n");
1634 for (; n
< nintf
; ++n
) {
1635 new_interfaces
[n
] = kzalloc(
1636 sizeof(struct usb_interface
),
1638 if (!new_interfaces
[n
]) {
1639 dev_err(&dev
->dev
, "Out of memory\n");
1643 kfree(new_interfaces
[n
]);
1644 kfree(new_interfaces
);
1649 i
= dev
->bus_mA
- cp
->desc
.bMaxPower
* 2;
1651 dev_warn(&dev
->dev
, "new config #%d exceeds power "
1656 /* Wake up the device so we can send it the Set-Config request */
1657 ret
= usb_autoresume_device(dev
);
1659 goto free_interfaces
;
1661 /* if it's already configured, clear out old state first.
1662 * getting rid of old interfaces means unbinding their drivers.
1664 if (dev
->state
!= USB_STATE_ADDRESS
)
1665 usb_disable_device(dev
, 1); /* Skip ep0 */
1667 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
1668 USB_REQ_SET_CONFIGURATION
, 0, configuration
, 0,
1669 NULL
, 0, USB_CTRL_SET_TIMEOUT
);
1671 /* All the old state is gone, so what else can we do?
1672 * The device is probably useless now anyway.
1677 dev
->actconfig
= cp
;
1679 usb_set_device_state(dev
, USB_STATE_ADDRESS
);
1680 usb_autosuspend_device(dev
);
1681 goto free_interfaces
;
1683 usb_set_device_state(dev
, USB_STATE_CONFIGURED
);
1685 /* Initialize the new interface structures and the
1686 * hc/hcd/usbcore interface/endpoint state.
1688 for (i
= 0; i
< nintf
; ++i
) {
1689 struct usb_interface_cache
*intfc
;
1690 struct usb_interface
*intf
;
1691 struct usb_host_interface
*alt
;
1693 cp
->interface
[i
] = intf
= new_interfaces
[i
];
1694 intfc
= cp
->intf_cache
[i
];
1695 intf
->altsetting
= intfc
->altsetting
;
1696 intf
->num_altsetting
= intfc
->num_altsetting
;
1697 intf
->intf_assoc
= find_iad(dev
, cp
, i
);
1698 kref_get(&intfc
->ref
);
1700 alt
= usb_altnum_to_altsetting(intf
, 0);
1702 /* No altsetting 0? We'll assume the first altsetting.
1703 * We could use a GetInterface call, but if a device is
1704 * so non-compliant that it doesn't have altsetting 0
1705 * then I wouldn't trust its reply anyway.
1708 alt
= &intf
->altsetting
[0];
1710 intf
->cur_altsetting
= alt
;
1711 usb_enable_interface(dev
, intf
);
1712 intf
->dev
.parent
= &dev
->dev
;
1713 intf
->dev
.driver
= NULL
;
1714 intf
->dev
.bus
= &usb_bus_type
;
1715 intf
->dev
.type
= &usb_if_device_type
;
1716 intf
->dev
.groups
= usb_interface_groups
;
1717 intf
->dev
.dma_mask
= dev
->dev
.dma_mask
;
1718 INIT_WORK(&intf
->reset_ws
, __usb_queue_reset_device
);
1720 device_initialize(&intf
->dev
);
1721 mark_quiesced(intf
);
1722 sprintf(&intf
->dev
.bus_id
[0], "%d-%s:%d.%d",
1723 dev
->bus
->busnum
, dev
->devpath
,
1724 configuration
, alt
->desc
.bInterfaceNumber
);
1726 kfree(new_interfaces
);
1728 if (cp
->string
== NULL
&&
1729 !(dev
->quirks
& USB_QUIRK_CONFIG_INTF_STRINGS
))
1730 cp
->string
= usb_cache_string(dev
, cp
->desc
.iConfiguration
);
1732 /* Now that all the interfaces are set up, register them
1733 * to trigger binding of drivers to interfaces. probe()
1734 * routines may install different altsettings and may
1735 * claim() any interfaces not yet bound. Many class drivers
1736 * need that: CDC, audio, video, etc.
1738 for (i
= 0; i
< nintf
; ++i
) {
1739 struct usb_interface
*intf
= cp
->interface
[i
];
1742 "adding %s (config #%d, interface %d)\n",
1743 intf
->dev
.bus_id
, configuration
,
1744 intf
->cur_altsetting
->desc
.bInterfaceNumber
);
1745 ret
= device_add(&intf
->dev
);
1747 dev_err(&dev
->dev
, "device_add(%s) --> %d\n",
1748 intf
->dev
.bus_id
, ret
);
1751 usb_create_sysfs_intf_files(intf
);
1754 usb_autosuspend_device(dev
);
1758 struct set_config_request
{
1759 struct usb_device
*udev
;
1761 struct work_struct work
;
1764 /* Worker routine for usb_driver_set_configuration() */
1765 static void driver_set_config_work(struct work_struct
*work
)
1767 struct set_config_request
*req
=
1768 container_of(work
, struct set_config_request
, work
);
1770 usb_lock_device(req
->udev
);
1771 usb_set_configuration(req
->udev
, req
->config
);
1772 usb_unlock_device(req
->udev
);
1773 usb_put_dev(req
->udev
);
1778 * usb_driver_set_configuration - Provide a way for drivers to change device configurations
1779 * @udev: the device whose configuration is being updated
1780 * @config: the configuration being chosen.
1781 * Context: In process context, must be able to sleep
1783 * Device interface drivers are not allowed to change device configurations.
1784 * This is because changing configurations will destroy the interface the
1785 * driver is bound to and create new ones; it would be like a floppy-disk
1786 * driver telling the computer to replace the floppy-disk drive with a
1789 * Still, in certain specialized circumstances the need may arise. This
1790 * routine gets around the normal restrictions by using a work thread to
1791 * submit the change-config request.
1793 * Returns 0 if the request was succesfully queued, error code otherwise.
1794 * The caller has no way to know whether the queued request will eventually
1797 int usb_driver_set_configuration(struct usb_device
*udev
, int config
)
1799 struct set_config_request
*req
;
1801 req
= kmalloc(sizeof(*req
), GFP_KERNEL
);
1805 req
->config
= config
;
1806 INIT_WORK(&req
->work
, driver_set_config_work
);
1809 schedule_work(&req
->work
);
1812 EXPORT_SYMBOL_GPL(usb_driver_set_configuration
);