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/scatterlist.h>
15 #include <linux/usb/quirks.h>
16 #include <asm/byteorder.h>
18 #include "hcd.h" /* for usbcore internals */
22 struct completion done;
26 static void usb_api_blocking_completion(struct urb *urb)
28 struct api_context *ctx = urb->context;
30 ctx->status = urb->status;
36 * Starts urb and waits for completion or timeout. Note that this call
37 * is NOT interruptible. Many device driver i/o requests should be
38 * interruptible and therefore these drivers should implement their
39 * own interruptible routines.
41 static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)
43 struct api_context ctx;
47 init_completion(&ctx.done);
49 urb->actual_length = 0;
50 retval = usb_submit_urb(urb, GFP_NOIO);
54 expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT;
55 if (!wait_for_completion_timeout(&ctx.done, expire)) {
57 retval = (ctx.status == -ENOENT ? -ETIMEDOUT : ctx.status);
59 dev_dbg(&urb->dev->dev,
60 "%s timed out on ep%d%s len=%d/%d\n",
62 usb_endpoint_num(&urb->ep->desc),
63 usb_urb_dir_in(urb) ? "in" : "out",
65 urb->transfer_buffer_length);
70 *actual_length = urb->actual_length;
76 /*-------------------------------------------------------------------*/
77 /* returns status (negative) or length (positive) */
78 static int usb_internal_control_msg(struct usb_device *usb_dev,
80 struct usb_ctrlrequest *cmd,
81 void *data, int len, int timeout)
87 urb = usb_alloc_urb(0, GFP_NOIO);
91 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data,
92 len, usb_api_blocking_completion, NULL);
94 retv = usb_start_wait_urb(urb, timeout, &length);
102 * usb_control_msg - Builds a control urb, sends it off and waits for completion
103 * @dev: pointer to the usb device to send the message to
104 * @pipe: endpoint "pipe" to send the message to
105 * @request: USB message request value
106 * @requesttype: USB message request type value
107 * @value: USB message value
108 * @index: USB message index value
109 * @data: pointer to the data to send
110 * @size: length in bytes of the data to send
111 * @timeout: time in msecs to wait for the message to complete before timing
112 * out (if 0 the wait is forever)
114 * Context: !in_interrupt ()
116 * This function sends a simple control message to a specified endpoint and
117 * waits for the message to complete, or timeout.
119 * If successful, it returns the number of bytes transferred, otherwise a
120 * negative error number.
122 * Don't use this function from within an interrupt context, like a bottom half
123 * handler. If you need an asynchronous message, or need to send a message
124 * from within interrupt context, use usb_submit_urb().
125 * If a thread in your driver uses this call, make sure your disconnect()
126 * method can wait for it to complete. Since you don't have a handle on the
127 * URB used, you can't cancel the request.
129 int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
130 __u8 requesttype, __u16 value, __u16 index, void *data,
131 __u16 size, int timeout)
133 struct usb_ctrlrequest *dr;
136 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
140 dr->bRequestType = requesttype;
141 dr->bRequest = request;
142 dr->wValue = cpu_to_le16p(&value);
143 dr->wIndex = cpu_to_le16p(&index);
144 dr->wLength = cpu_to_le16p(&size);
146 /* dbg("usb_control_msg"); */
148 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
154 EXPORT_SYMBOL_GPL(usb_control_msg);
157 * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion
158 * @usb_dev: pointer to the usb device to send the message to
159 * @pipe: endpoint "pipe" to send the message to
160 * @data: pointer to the data to send
161 * @len: length in bytes of the data to send
162 * @actual_length: pointer to a location to put the actual length transferred
164 * @timeout: time in msecs to wait for the message to complete before
165 * timing out (if 0 the wait is forever)
167 * Context: !in_interrupt ()
169 * This function sends a simple interrupt message to a specified endpoint and
170 * waits for the message to complete, or timeout.
172 * If successful, it returns 0, otherwise a negative error number. The number
173 * of actual bytes transferred will be stored in the actual_length paramater.
175 * Don't use this function from within an interrupt context, like a bottom half
176 * handler. If you need an asynchronous message, or need to send a message
177 * from within interrupt context, use usb_submit_urb() If a thread in your
178 * driver uses this call, make sure your disconnect() method can wait for it to
179 * complete. Since you don't have a handle on the URB used, you can't cancel
182 int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
183 void *data, int len, int *actual_length, int timeout)
185 return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout);
187 EXPORT_SYMBOL_GPL(usb_interrupt_msg);
190 * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
191 * @usb_dev: pointer to the usb device to send the message to
192 * @pipe: endpoint "pipe" to send the message to
193 * @data: pointer to the data to send
194 * @len: length in bytes of the data to send
195 * @actual_length: pointer to a location to put the actual length transferred
197 * @timeout: time in msecs to wait for the message to complete before
198 * timing out (if 0 the wait is forever)
200 * Context: !in_interrupt ()
202 * This function sends a simple bulk message to a specified endpoint
203 * and waits for the message to complete, or timeout.
205 * If successful, it returns 0, otherwise a negative error number. The number
206 * of actual bytes transferred will be stored in the actual_length paramater.
208 * Don't use this function from within an interrupt context, like a bottom half
209 * handler. If you need an asynchronous message, or need to send a message
210 * from within interrupt context, use usb_submit_urb() If a thread in your
211 * driver uses this call, make sure your disconnect() method can wait for it to
212 * complete. Since you don't have a handle on the URB used, you can't cancel
215 * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT ioctl,
216 * users are forced to abuse this routine by using it to submit URBs for
217 * interrupt endpoints. We will take the liberty of creating an interrupt URB
218 * (with the default interval) if the target is an interrupt endpoint.
220 int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
221 void *data, int len, int *actual_length, int timeout)
224 struct usb_host_endpoint *ep;
226 ep = (usb_pipein(pipe) ? usb_dev->ep_in : usb_dev->ep_out)
227 [usb_pipeendpoint(pipe)];
231 urb = usb_alloc_urb(0, GFP_KERNEL);
235 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
236 USB_ENDPOINT_XFER_INT) {
237 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
238 usb_fill_int_urb(urb, usb_dev, pipe, data, len,
239 usb_api_blocking_completion, NULL,
242 usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
243 usb_api_blocking_completion, NULL);
245 return usb_start_wait_urb(urb, timeout, actual_length);
247 EXPORT_SYMBOL_GPL(usb_bulk_msg);
249 /*-------------------------------------------------------------------*/
251 static void sg_clean(struct usb_sg_request *io)
254 while (io->entries--)
255 usb_free_urb(io->urbs [io->entries]);
259 if (io->dev->dev.dma_mask != NULL)
260 usb_buffer_unmap_sg(io->dev, usb_pipein(io->pipe),
265 static void sg_complete(struct urb *urb)
267 struct usb_sg_request *io = urb->context;
268 int status = urb->status;
270 spin_lock(&io->lock);
272 /* In 2.5 we require hcds' endpoint queues not to progress after fault
273 * reports, until the completion callback (this!) returns. That lets
274 * device driver code (like this routine) unlink queued urbs first,
275 * if it needs to, since the HC won't work on them at all. So it's
276 * not possible for page N+1 to overwrite page N, and so on.
278 * That's only for "hard" faults; "soft" faults (unlinks) sometimes
279 * complete before the HCD can get requests away from hardware,
280 * though never during cleanup after a hard fault.
283 && (io->status != -ECONNRESET
284 || status != -ECONNRESET)
285 && urb->actual_length) {
286 dev_err(io->dev->bus->controller,
287 "dev %s ep%d%s scatterlist error %d/%d\n",
289 usb_endpoint_num(&urb->ep->desc),
290 usb_urb_dir_in(urb) ? "in" : "out",
295 if (io->status == 0 && status && status != -ECONNRESET) {
296 int i, found, retval;
300 /* the previous urbs, and this one, completed already.
301 * unlink pending urbs so they won't rx/tx bad data.
302 * careful: unlink can sometimes be synchronous...
304 spin_unlock(&io->lock);
305 for (i = 0, found = 0; i < io->entries; i++) {
306 if (!io->urbs [i] || !io->urbs [i]->dev)
309 retval = usb_unlink_urb(io->urbs [i]);
310 if (retval != -EINPROGRESS &&
313 dev_err(&io->dev->dev,
314 "%s, unlink --> %d\n",
316 } else if (urb == io->urbs [i])
319 spin_lock(&io->lock);
323 /* on the last completion, signal usb_sg_wait() */
324 io->bytes += urb->actual_length;
327 complete(&io->complete);
329 spin_unlock(&io->lock);
334 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
335 * @io: request block being initialized. until usb_sg_wait() returns,
336 * treat this as a pointer to an opaque block of memory,
337 * @dev: the usb device that will send or receive the data
338 * @pipe: endpoint "pipe" used to transfer the data
339 * @period: polling rate for interrupt endpoints, in frames or
340 * (for high speed endpoints) microframes; ignored for bulk
341 * @sg: scatterlist entries
342 * @nents: how many entries in the scatterlist
343 * @length: how many bytes to send from the scatterlist, or zero to
344 * send every byte identified in the list.
345 * @mem_flags: SLAB_* flags affecting memory allocations in this call
347 * Returns zero for success, else a negative errno value. This initializes a
348 * scatter/gather request, allocating resources such as I/O mappings and urb
349 * memory (except maybe memory used by USB controller drivers).
351 * The request must be issued using usb_sg_wait(), which waits for the I/O to
352 * complete (or to be canceled) and then cleans up all resources allocated by
355 * The request may be canceled with usb_sg_cancel(), either before or after
356 * usb_sg_wait() is called.
358 int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
359 unsigned pipe, unsigned period, struct scatterlist *sg,
360 int nents, size_t length, gfp_t mem_flags)
366 if (!io || !dev || !sg
367 || usb_pipecontrol(pipe)
368 || usb_pipeisoc(pipe)
372 spin_lock_init(&io->lock);
378 /* not all host controllers use DMA (like the mainstream pci ones);
379 * they can use PIO (sl811) or be software over another transport.
381 dma = (dev->dev.dma_mask != NULL);
383 io->entries = usb_buffer_map_sg(dev, usb_pipein(pipe),
388 /* initialize all the urbs we'll use */
389 if (io->entries <= 0)
392 io->count = io->entries;
393 io->urbs = kmalloc(io->entries * sizeof *io->urbs, mem_flags);
397 urb_flags = URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT;
398 if (usb_pipein(pipe))
399 urb_flags |= URB_SHORT_NOT_OK;
401 for (i = 0; i < io->entries; i++) {
404 io->urbs[i] = usb_alloc_urb(0, mem_flags);
410 io->urbs[i]->dev = NULL;
411 io->urbs[i]->pipe = pipe;
412 io->urbs[i]->interval = period;
413 io->urbs[i]->transfer_flags = urb_flags;
415 io->urbs[i]->complete = sg_complete;
416 io->urbs[i]->context = io;
419 * Some systems need to revert to PIO when DMA is temporarily
420 * unavailable. For their sakes, both transfer_buffer and
421 * transfer_dma are set when possible. However this can only
422 * work on systems without:
424 * - HIGHMEM, since DMA buffers located in high memory are
425 * not directly addressable by the CPU for PIO;
427 * - IOMMU, since dma_map_sg() is allowed to use an IOMMU to
428 * make virtually discontiguous buffers be "dma-contiguous"
429 * so that PIO and DMA need diferent numbers of URBs.
431 * So when HIGHMEM or IOMMU are in use, transfer_buffer is NULL
432 * to prevent stale pointers and to help spot bugs.
435 io->urbs[i]->transfer_dma = sg_dma_address(sg + i);
436 len = sg_dma_len(sg + i);
437 #if defined(CONFIG_HIGHMEM) || defined(CONFIG_GART_IOMMU)
438 io->urbs[i]->transfer_buffer = NULL;
440 io->urbs[i]->transfer_buffer = sg_virt(&sg[i]);
443 /* hc may use _only_ transfer_buffer */
444 io->urbs[i]->transfer_buffer = sg_virt(&sg[i]);
449 len = min_t(unsigned, len, length);
454 io->urbs[i]->transfer_buffer_length = len;
456 io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
458 /* transaction state */
461 init_completion(&io->complete);
468 EXPORT_SYMBOL_GPL(usb_sg_init);
471 * usb_sg_wait - synchronously execute scatter/gather request
472 * @io: request block handle, as initialized with usb_sg_init().
473 * some fields become accessible when this call returns.
474 * Context: !in_interrupt ()
476 * This function blocks until the specified I/O operation completes. It
477 * leverages the grouping of the related I/O requests to get good transfer
478 * rates, by queueing the requests. At higher speeds, such queuing can
479 * significantly improve USB throughput.
481 * There are three kinds of completion for this function.
482 * (1) success, where io->status is zero. The number of io->bytes
483 * transferred is as requested.
484 * (2) error, where io->status is a negative errno value. The number
485 * of io->bytes transferred before the error is usually less
486 * than requested, and can be nonzero.
487 * (3) cancellation, a type of error with status -ECONNRESET that
488 * is initiated by usb_sg_cancel().
490 * When this function returns, all memory allocated through usb_sg_init() or
491 * this call will have been freed. The request block parameter may still be
492 * passed to usb_sg_cancel(), or it may be freed. It could also be
493 * reinitialized and then reused.
495 * Data Transfer Rates:
497 * Bulk transfers are valid for full or high speed endpoints.
498 * The best full speed data rate is 19 packets of 64 bytes each
499 * per frame, or 1216 bytes per millisecond.
500 * The best high speed data rate is 13 packets of 512 bytes each
501 * per microframe, or 52 KBytes per millisecond.
503 * The reason to use interrupt transfers through this API would most likely
504 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
505 * could be transferred. That capability is less useful for low or full
506 * speed interrupt endpoints, which allow at most one packet per millisecond,
507 * of at most 8 or 64 bytes (respectively).
509 void usb_sg_wait(struct usb_sg_request *io)
512 int entries = io->entries;
514 /* queue the urbs. */
515 spin_lock_irq(&io->lock);
517 while (i < entries && !io->status) {
520 io->urbs[i]->dev = io->dev;
521 retval = usb_submit_urb(io->urbs [i], GFP_ATOMIC);
523 /* after we submit, let completions or cancelations fire;
524 * we handshake using io->status.
526 spin_unlock_irq(&io->lock);
528 /* maybe we retrying will recover */
529 case -ENXIO: /* hc didn't queue this one */
532 io->urbs[i]->dev = NULL;
537 /* no error? continue immediately.
539 * NOTE: to work better with UHCI (4K I/O buffer may
540 * need 3K of TDs) it may be good to limit how many
541 * URBs are queued at once; N milliseconds?
548 /* fail any uncompleted urbs */
550 io->urbs[i]->dev = NULL;
551 io->urbs[i]->status = retval;
552 dev_dbg(&io->dev->dev, "%s, submit --> %d\n",
556 spin_lock_irq(&io->lock);
557 if (retval && (io->status == 0 || io->status == -ECONNRESET))
560 io->count -= entries - i;
562 complete(&io->complete);
563 spin_unlock_irq(&io->lock);
565 /* OK, yes, this could be packaged as non-blocking.
566 * So could the submit loop above ... but it's easier to
567 * solve neither problem than to solve both!
569 wait_for_completion(&io->complete);
573 EXPORT_SYMBOL_GPL(usb_sg_wait);
576 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
577 * @io: request block, initialized with usb_sg_init()
579 * This stops a request after it has been started by usb_sg_wait().
580 * It can also prevents one initialized by usb_sg_init() from starting,
581 * so that call just frees resources allocated to the request.
583 void usb_sg_cancel(struct usb_sg_request *io)
587 spin_lock_irqsave(&io->lock, flags);
589 /* shut everything down, if it didn't already */
593 io->status = -ECONNRESET;
594 spin_unlock(&io->lock);
595 for (i = 0; i < io->entries; i++) {
598 if (!io->urbs [i]->dev)
600 retval = usb_unlink_urb(io->urbs [i]);
601 if (retval != -EINPROGRESS && retval != -EBUSY)
602 dev_warn(&io->dev->dev, "%s, unlink --> %d\n",
605 spin_lock(&io->lock);
607 spin_unlock_irqrestore(&io->lock, flags);
609 EXPORT_SYMBOL_GPL(usb_sg_cancel);
611 /*-------------------------------------------------------------------*/
614 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
615 * @dev: the device whose descriptor is being retrieved
616 * @type: the descriptor type (USB_DT_*)
617 * @index: the number of the descriptor
618 * @buf: where to put the descriptor
619 * @size: how big is "buf"?
620 * Context: !in_interrupt ()
622 * Gets a USB descriptor. Convenience functions exist to simplify
623 * getting some types of descriptors. Use
624 * usb_get_string() or usb_string() for USB_DT_STRING.
625 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
626 * are part of the device structure.
627 * In addition to a number of USB-standard descriptors, some
628 * devices also use class-specific or vendor-specific descriptors.
630 * This call is synchronous, and may not be used in an interrupt context.
632 * Returns the number of bytes received on success, or else the status code
633 * returned by the underlying usb_control_msg() call.
635 int usb_get_descriptor(struct usb_device *dev, unsigned char type,
636 unsigned char index, void *buf, int size)
641 memset(buf, 0, size); /* Make sure we parse really received data */
643 for (i = 0; i < 3; ++i) {
644 /* retry on length 0 or error; some devices are flakey */
645 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
646 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
647 (type << 8) + index, 0, buf, size,
648 USB_CTRL_GET_TIMEOUT);
649 if (result <= 0 && result != -ETIMEDOUT)
651 if (result > 1 && ((u8 *)buf)[1] != type) {
659 EXPORT_SYMBOL_GPL(usb_get_descriptor);
662 * usb_get_string - gets a string descriptor
663 * @dev: the device whose string descriptor is being retrieved
664 * @langid: code for language chosen (from string descriptor zero)
665 * @index: the number of the descriptor
666 * @buf: where to put the string
667 * @size: how big is "buf"?
668 * Context: !in_interrupt ()
670 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
671 * in little-endian byte order).
672 * The usb_string() function will often be a convenient way to turn
673 * these strings into kernel-printable form.
675 * Strings may be referenced in device, configuration, interface, or other
676 * descriptors, and could also be used in vendor-specific ways.
678 * This call is synchronous, and may not be used in an interrupt context.
680 * Returns the number of bytes received on success, or else the status code
681 * returned by the underlying usb_control_msg() call.
683 static int usb_get_string(struct usb_device *dev, unsigned short langid,
684 unsigned char index, void *buf, int size)
689 for (i = 0; i < 3; ++i) {
690 /* retry on length 0 or stall; some devices are flakey */
691 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
692 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
693 (USB_DT_STRING << 8) + index, langid, buf, size,
694 USB_CTRL_GET_TIMEOUT);
695 if (!(result == 0 || result == -EPIPE))
701 static void usb_try_string_workarounds(unsigned char *buf, int *length)
703 int newlength, oldlength = *length;
705 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
706 if (!isprint(buf[newlength]) || buf[newlength + 1])
715 static int usb_string_sub(struct usb_device *dev, unsigned int langid,
716 unsigned int index, unsigned char *buf)
720 /* Try to read the string descriptor by asking for the maximum
721 * possible number of bytes */
722 if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
725 rc = usb_get_string(dev, langid, index, buf, 255);
727 /* If that failed try to read the descriptor length, then
728 * ask for just that many bytes */
730 rc = usb_get_string(dev, langid, index, buf, 2);
732 rc = usb_get_string(dev, langid, index, buf, buf[0]);
736 if (!buf[0] && !buf[1])
737 usb_try_string_workarounds(buf, &rc);
739 /* There might be extra junk at the end of the descriptor */
743 rc = rc - (rc & 1); /* force a multiple of two */
747 rc = (rc < 0 ? rc : -EINVAL);
753 * usb_string - returns ISO 8859-1 version of a string descriptor
754 * @dev: the device whose string descriptor is being retrieved
755 * @index: the number of the descriptor
756 * @buf: where to put the string
757 * @size: how big is "buf"?
758 * Context: !in_interrupt ()
760 * This converts the UTF-16LE encoded strings returned by devices, from
761 * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones
762 * that are more usable in most kernel contexts. Note that all characters
763 * in the chosen descriptor that can't be encoded using ISO-8859-1
764 * are converted to the question mark ("?") character, and this function
765 * chooses strings in the first language supported by the device.
767 * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit
768 * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode,
769 * and is appropriate for use many uses of English and several other
770 * Western European languages. (But it doesn't include the "Euro" symbol.)
772 * This call is synchronous, and may not be used in an interrupt context.
774 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
776 int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
782 if (dev->state == USB_STATE_SUSPENDED)
783 return -EHOSTUNREACH;
784 if (size <= 0 || !buf || !index)
787 tbuf = kmalloc(256, GFP_NOIO);
791 /* get langid for strings if it's not yet known */
792 if (!dev->have_langid) {
793 err = usb_string_sub(dev, 0, 0, tbuf);
796 "string descriptor 0 read error: %d\n",
799 } else if (err < 4) {
800 dev_err(&dev->dev, "string descriptor 0 too short\n");
804 dev->have_langid = 1;
805 dev->string_langid = tbuf[2] | (tbuf[3] << 8);
806 /* always use the first langid listed */
807 dev_dbg(&dev->dev, "default language 0x%04x\n",
812 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
816 size--; /* leave room for trailing NULL char in output buffer */
817 for (idx = 0, u = 2; u < err; u += 2) {
820 if (tbuf[u+1]) /* high byte */
821 buf[idx++] = '?'; /* non ISO-8859-1 character */
823 buf[idx++] = tbuf[u];
828 if (tbuf[1] != USB_DT_STRING)
830 "wrong descriptor type %02x for string %d (\"%s\")\n",
831 tbuf[1], index, buf);
837 EXPORT_SYMBOL_GPL(usb_string);
840 * usb_cache_string - read a string descriptor and cache it for later use
841 * @udev: the device whose string descriptor is being read
842 * @index: the descriptor index
844 * Returns a pointer to a kmalloc'ed buffer containing the descriptor string,
845 * or NULL if the index is 0 or the string could not be read.
847 char *usb_cache_string(struct usb_device *udev, int index)
850 char *smallbuf = NULL;
856 buf = kmalloc(256, GFP_KERNEL);
858 len = usb_string(udev, index, buf, 256);
860 smallbuf = kmalloc(++len, GFP_KERNEL);
863 memcpy(smallbuf, buf, len);
871 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
872 * @dev: the device whose device descriptor is being updated
873 * @size: how much of the descriptor to read
874 * Context: !in_interrupt ()
876 * Updates the copy of the device descriptor stored in the device structure,
877 * which dedicates space for this purpose.
879 * Not exported, only for use by the core. If drivers really want to read
880 * the device descriptor directly, they can call usb_get_descriptor() with
881 * type = USB_DT_DEVICE and index = 0.
883 * This call is synchronous, and may not be used in an interrupt context.
885 * Returns the number of bytes received on success, or else the status code
886 * returned by the underlying usb_control_msg() call.
888 int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
890 struct usb_device_descriptor *desc;
893 if (size > sizeof(*desc))
895 desc = kmalloc(sizeof(*desc), GFP_NOIO);
899 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
901 memcpy(&dev->descriptor, desc, size);
907 * usb_get_status - issues a GET_STATUS call
908 * @dev: the device whose status is being checked
909 * @type: USB_RECIP_*; for device, interface, or endpoint
910 * @target: zero (for device), else interface or endpoint number
911 * @data: pointer to two bytes of bitmap data
912 * Context: !in_interrupt ()
914 * Returns device, interface, or endpoint status. Normally only of
915 * interest to see if the device is self powered, or has enabled the
916 * remote wakeup facility; or whether a bulk or interrupt endpoint
917 * is halted ("stalled").
919 * Bits in these status bitmaps are set using the SET_FEATURE request,
920 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
921 * function should be used to clear halt ("stall") status.
923 * This call is synchronous, and may not be used in an interrupt context.
925 * Returns the number of bytes received on success, or else the status code
926 * returned by the underlying usb_control_msg() call.
928 int usb_get_status(struct usb_device *dev, int type, int target, void *data)
931 u16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
936 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
937 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
938 sizeof(*status), USB_CTRL_GET_TIMEOUT);
940 *(u16 *)data = *status;
944 EXPORT_SYMBOL_GPL(usb_get_status);
947 * usb_clear_halt - tells device to clear endpoint halt/stall condition
948 * @dev: device whose endpoint is halted
949 * @pipe: endpoint "pipe" being cleared
950 * Context: !in_interrupt ()
952 * This is used to clear halt conditions for bulk and interrupt endpoints,
953 * as reported by URB completion status. Endpoints that are halted are
954 * sometimes referred to as being "stalled". Such endpoints are unable
955 * to transmit or receive data until the halt status is cleared. Any URBs
956 * queued for such an endpoint should normally be unlinked by the driver
957 * before clearing the halt condition, as described in sections 5.7.5
958 * and 5.8.5 of the USB 2.0 spec.
960 * Note that control and isochronous endpoints don't halt, although control
961 * endpoints report "protocol stall" (for unsupported requests) using the
962 * same status code used to report a true stall.
964 * This call is synchronous, and may not be used in an interrupt context.
966 * Returns zero on success, or else the status code returned by the
967 * underlying usb_control_msg() call.
969 int usb_clear_halt(struct usb_device *dev, int pipe)
972 int endp = usb_pipeendpoint(pipe);
974 if (usb_pipein(pipe))
977 /* we don't care if it wasn't halted first. in fact some devices
978 * (like some ibmcam model 1 units) seem to expect hosts to make
979 * this request for iso endpoints, which can't halt!
981 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
982 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
983 USB_ENDPOINT_HALT, endp, NULL, 0,
984 USB_CTRL_SET_TIMEOUT);
986 /* don't un-halt or force to DATA0 except on success */
990 /* NOTE: seems like Microsoft and Apple don't bother verifying
991 * the clear "took", so some devices could lock up if you check...
992 * such as the Hagiwara FlashGate DUAL. So we won't bother.
994 * NOTE: make sure the logic here doesn't diverge much from
995 * the copy in usb-storage, for as long as we need two copies.
998 /* toggle was reset by the clear */
999 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
1003 EXPORT_SYMBOL_GPL(usb_clear_halt);
1006 * usb_disable_endpoint -- Disable an endpoint by address
1007 * @dev: the device whose endpoint is being disabled
1008 * @epaddr: the endpoint's address. Endpoint number for output,
1009 * endpoint number + USB_DIR_IN for input
1011 * Deallocates hcd/hardware state for this endpoint ... and nukes all
1014 * If the HCD hasn't registered a disable() function, this sets the
1015 * endpoint's maxpacket size to 0 to prevent further submissions.
1017 void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr)
1019 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1020 struct usb_host_endpoint *ep;
1025 if (usb_endpoint_out(epaddr)) {
1026 ep = dev->ep_out[epnum];
1027 dev->ep_out[epnum] = NULL;
1029 ep = dev->ep_in[epnum];
1030 dev->ep_in[epnum] = NULL;
1034 usb_hcd_flush_endpoint(dev, ep);
1035 usb_hcd_disable_endpoint(dev, ep);
1040 * usb_disable_interface -- Disable all endpoints for an interface
1041 * @dev: the device whose interface is being disabled
1042 * @intf: pointer to the interface descriptor
1044 * Disables all the endpoints for the interface's current altsetting.
1046 void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf)
1048 struct usb_host_interface *alt = intf->cur_altsetting;
1051 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
1052 usb_disable_endpoint(dev,
1053 alt->endpoint[i].desc.bEndpointAddress);
1058 * usb_disable_device - Disable all the endpoints for a USB device
1059 * @dev: the device whose endpoints are being disabled
1060 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
1062 * Disables all the device's endpoints, potentially including endpoint 0.
1063 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1064 * pending urbs) and usbcore state for the interfaces, so that usbcore
1065 * must usb_set_configuration() before any interfaces could be used.
1067 void usb_disable_device(struct usb_device *dev, int skip_ep0)
1071 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __func__,
1072 skip_ep0 ? "non-ep0" : "all");
1073 for (i = skip_ep0; i < 16; ++i) {
1074 usb_disable_endpoint(dev, i);
1075 usb_disable_endpoint(dev, i + USB_DIR_IN);
1077 dev->toggle[0] = dev->toggle[1] = 0;
1079 /* getting rid of interfaces will disconnect
1080 * any drivers bound to them (a key side effect)
1082 if (dev->actconfig) {
1083 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1084 struct usb_interface *interface;
1086 /* remove this interface if it has been registered */
1087 interface = dev->actconfig->interface[i];
1088 if (!device_is_registered(&interface->dev))
1090 dev_dbg(&dev->dev, "unregistering interface %s\n",
1091 interface->dev.bus_id);
1092 device_del(&interface->dev);
1093 usb_remove_sysfs_intf_files(interface);
1096 /* Now that the interfaces are unbound, nobody should
1097 * try to access them.
1099 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1100 put_device(&dev->actconfig->interface[i]->dev);
1101 dev->actconfig->interface[i] = NULL;
1103 dev->actconfig = NULL;
1104 if (dev->state == USB_STATE_CONFIGURED)
1105 usb_set_device_state(dev, USB_STATE_ADDRESS);
1110 * usb_enable_endpoint - Enable an endpoint for USB communications
1111 * @dev: the device whose interface is being enabled
1114 * Resets the endpoint toggle, and sets dev->ep_{in,out} pointers.
1115 * For control endpoints, both the input and output sides are handled.
1117 void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep)
1119 int epnum = usb_endpoint_num(&ep->desc);
1120 int is_out = usb_endpoint_dir_out(&ep->desc);
1121 int is_control = usb_endpoint_xfer_control(&ep->desc);
1123 if (is_out || is_control) {
1124 usb_settoggle(dev, epnum, 1, 0);
1125 dev->ep_out[epnum] = ep;
1127 if (!is_out || is_control) {
1128 usb_settoggle(dev, epnum, 0, 0);
1129 dev->ep_in[epnum] = ep;
1135 * usb_enable_interface - Enable all the endpoints for an interface
1136 * @dev: the device whose interface is being enabled
1137 * @intf: pointer to the interface descriptor
1139 * Enables all the endpoints for the interface's current altsetting.
1141 static void usb_enable_interface(struct usb_device *dev,
1142 struct usb_interface *intf)
1144 struct usb_host_interface *alt = intf->cur_altsetting;
1147 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1148 usb_enable_endpoint(dev, &alt->endpoint[i]);
1152 * usb_set_interface - Makes a particular alternate setting be current
1153 * @dev: the device whose interface is being updated
1154 * @interface: the interface being updated
1155 * @alternate: the setting being chosen.
1156 * Context: !in_interrupt ()
1158 * This is used to enable data transfers on interfaces that may not
1159 * be enabled by default. Not all devices support such configurability.
1160 * Only the driver bound to an interface may change its setting.
1162 * Within any given configuration, each interface may have several
1163 * alternative settings. These are often used to control levels of
1164 * bandwidth consumption. For example, the default setting for a high
1165 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1166 * while interrupt transfers of up to 3KBytes per microframe are legal.
1167 * Also, isochronous endpoints may never be part of an
1168 * interface's default setting. To access such bandwidth, alternate
1169 * interface settings must be made current.
1171 * Note that in the Linux USB subsystem, bandwidth associated with
1172 * an endpoint in a given alternate setting is not reserved until an URB
1173 * is submitted that needs that bandwidth. Some other operating systems
1174 * allocate bandwidth early, when a configuration is chosen.
1176 * This call is synchronous, and may not be used in an interrupt context.
1177 * Also, drivers must not change altsettings while urbs are scheduled for
1178 * endpoints in that interface; all such urbs must first be completed
1179 * (perhaps forced by unlinking).
1181 * Returns zero on success, or else the status code returned by the
1182 * underlying usb_control_msg() call.
1184 int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1186 struct usb_interface *iface;
1187 struct usb_host_interface *alt;
1190 unsigned int epaddr;
1193 if (dev->state == USB_STATE_SUSPENDED)
1194 return -EHOSTUNREACH;
1196 iface = usb_ifnum_to_if(dev, interface);
1198 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1203 alt = usb_altnum_to_altsetting(iface, alternate);
1205 warn("selecting invalid altsetting %d", alternate);
1209 if (dev->quirks & USB_QUIRK_NO_SET_INTF)
1212 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1213 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1214 alternate, interface, NULL, 0, 5000);
1216 /* 9.4.10 says devices don't need this and are free to STALL the
1217 * request if the interface only has one alternate setting.
1219 if (ret == -EPIPE && iface->num_altsetting == 1) {
1221 "manual set_interface for iface %d, alt %d\n",
1222 interface, alternate);
1227 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1228 * when they implement async or easily-killable versions of this or
1229 * other "should-be-internal" functions (like clear_halt).
1230 * should hcd+usbcore postprocess control requests?
1233 /* prevent submissions using previous endpoint settings */
1234 if (iface->cur_altsetting != alt)
1235 usb_remove_sysfs_intf_files(iface);
1236 usb_disable_interface(dev, iface);
1238 iface->cur_altsetting = alt;
1240 /* If the interface only has one altsetting and the device didn't
1241 * accept the request, we attempt to carry out the equivalent action
1242 * by manually clearing the HALT feature for each endpoint in the
1248 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
1249 epaddr = alt->endpoint[i].desc.bEndpointAddress;
1250 pipe = __create_pipe(dev,
1251 USB_ENDPOINT_NUMBER_MASK & epaddr) |
1252 (usb_endpoint_out(epaddr) ?
1253 USB_DIR_OUT : USB_DIR_IN);
1255 usb_clear_halt(dev, pipe);
1259 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1262 * Despite EP0 is always present in all interfaces/AS, the list of
1263 * endpoints from the descriptor does not contain EP0. Due to its
1264 * omnipresence one might expect EP0 being considered "affected" by
1265 * any SetInterface request and hence assume toggles need to be reset.
1266 * However, EP0 toggles are re-synced for every individual transfer
1267 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1268 * (Likewise, EP0 never "halts" on well designed devices.)
1270 usb_enable_interface(dev, iface);
1271 if (device_is_registered(&iface->dev))
1272 usb_create_sysfs_intf_files(iface);
1276 EXPORT_SYMBOL_GPL(usb_set_interface);
1279 * usb_reset_configuration - lightweight device reset
1280 * @dev: the device whose configuration is being reset
1282 * This issues a standard SET_CONFIGURATION request to the device using
1283 * the current configuration. The effect is to reset most USB-related
1284 * state in the device, including interface altsettings (reset to zero),
1285 * endpoint halts (cleared), and data toggle (only for bulk and interrupt
1286 * endpoints). Other usbcore state is unchanged, including bindings of
1287 * usb device drivers to interfaces.
1289 * Because this affects multiple interfaces, avoid using this with composite
1290 * (multi-interface) devices. Instead, the driver for each interface may
1291 * use usb_set_interface() on the interfaces it claims. Be careful though;
1292 * some devices don't support the SET_INTERFACE request, and others won't
1293 * reset all the interface state (notably data toggles). Resetting the whole
1294 * configuration would affect other drivers' interfaces.
1296 * The caller must own the device lock.
1298 * Returns zero on success, else a negative error code.
1300 int usb_reset_configuration(struct usb_device *dev)
1303 struct usb_host_config *config;
1305 if (dev->state == USB_STATE_SUSPENDED)
1306 return -EHOSTUNREACH;
1308 /* caller must have locked the device and must own
1309 * the usb bus readlock (so driver bindings are stable);
1310 * calls during probe() are fine
1313 for (i = 1; i < 16; ++i) {
1314 usb_disable_endpoint(dev, i);
1315 usb_disable_endpoint(dev, i + USB_DIR_IN);
1318 config = dev->actconfig;
1319 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1320 USB_REQ_SET_CONFIGURATION, 0,
1321 config->desc.bConfigurationValue, 0,
1322 NULL, 0, USB_CTRL_SET_TIMEOUT);
1326 dev->toggle[0] = dev->toggle[1] = 0;
1328 /* re-init hc/hcd interface/endpoint state */
1329 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1330 struct usb_interface *intf = config->interface[i];
1331 struct usb_host_interface *alt;
1333 usb_remove_sysfs_intf_files(intf);
1334 alt = usb_altnum_to_altsetting(intf, 0);
1336 /* No altsetting 0? We'll assume the first altsetting.
1337 * We could use a GetInterface call, but if a device is
1338 * so non-compliant that it doesn't have altsetting 0
1339 * then I wouldn't trust its reply anyway.
1342 alt = &intf->altsetting[0];
1344 intf->cur_altsetting = alt;
1345 usb_enable_interface(dev, intf);
1346 if (device_is_registered(&intf->dev))
1347 usb_create_sysfs_intf_files(intf);
1351 EXPORT_SYMBOL_GPL(usb_reset_configuration);
1353 static void usb_release_interface(struct device *dev)
1355 struct usb_interface *intf = to_usb_interface(dev);
1356 struct usb_interface_cache *intfc =
1357 altsetting_to_usb_interface_cache(intf->altsetting);
1359 kref_put(&intfc->ref, usb_release_interface_cache);
1363 #ifdef CONFIG_HOTPLUG
1364 static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
1366 struct usb_device *usb_dev;
1367 struct usb_interface *intf;
1368 struct usb_host_interface *alt;
1370 intf = to_usb_interface(dev);
1371 usb_dev = interface_to_usbdev(intf);
1372 alt = intf->cur_altsetting;
1374 if (add_uevent_var(env, "INTERFACE=%d/%d/%d",
1375 alt->desc.bInterfaceClass,
1376 alt->desc.bInterfaceSubClass,
1377 alt->desc.bInterfaceProtocol))
1380 if (add_uevent_var(env,
1382 "v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
1383 le16_to_cpu(usb_dev->descriptor.idVendor),
1384 le16_to_cpu(usb_dev->descriptor.idProduct),
1385 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1386 usb_dev->descriptor.bDeviceClass,
1387 usb_dev->descriptor.bDeviceSubClass,
1388 usb_dev->descriptor.bDeviceProtocol,
1389 alt->desc.bInterfaceClass,
1390 alt->desc.bInterfaceSubClass,
1391 alt->desc.bInterfaceProtocol))
1399 static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
1403 #endif /* CONFIG_HOTPLUG */
1405 struct device_type usb_if_device_type = {
1406 .name = "usb_interface",
1407 .release = usb_release_interface,
1408 .uevent = usb_if_uevent,
1411 static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
1412 struct usb_host_config *config,
1415 struct usb_interface_assoc_descriptor *retval = NULL;
1416 struct usb_interface_assoc_descriptor *intf_assoc;
1421 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
1422 intf_assoc = config->intf_assoc[i];
1423 if (intf_assoc->bInterfaceCount == 0)
1426 first_intf = intf_assoc->bFirstInterface;
1427 last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
1428 if (inum >= first_intf && inum <= last_intf) {
1430 retval = intf_assoc;
1432 dev_err(&dev->dev, "Interface #%d referenced"
1433 " by multiple IADs\n", inum);
1441 * usb_set_configuration - Makes a particular device setting be current
1442 * @dev: the device whose configuration is being updated
1443 * @configuration: the configuration being chosen.
1444 * Context: !in_interrupt(), caller owns the device lock
1446 * This is used to enable non-default device modes. Not all devices
1447 * use this kind of configurability; many devices only have one
1450 * @configuration is the value of the configuration to be installed.
1451 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1452 * must be non-zero; a value of zero indicates that the device in
1453 * unconfigured. However some devices erroneously use 0 as one of their
1454 * configuration values. To help manage such devices, this routine will
1455 * accept @configuration = -1 as indicating the device should be put in
1456 * an unconfigured state.
1458 * USB device configurations may affect Linux interoperability,
1459 * power consumption and the functionality available. For example,
1460 * the default configuration is limited to using 100mA of bus power,
1461 * so that when certain device functionality requires more power,
1462 * and the device is bus powered, that functionality should be in some
1463 * non-default device configuration. Other device modes may also be
1464 * reflected as configuration options, such as whether two ISDN
1465 * channels are available independently; and choosing between open
1466 * standard device protocols (like CDC) or proprietary ones.
1468 * Note that a non-authorized device (dev->authorized == 0) will only
1469 * be put in unconfigured mode.
1471 * Note that USB has an additional level of device configurability,
1472 * associated with interfaces. That configurability is accessed using
1473 * usb_set_interface().
1475 * This call is synchronous. The calling context must be able to sleep,
1476 * must own the device lock, and must not hold the driver model's USB
1477 * bus mutex; usb device driver probe() methods cannot use this routine.
1479 * Returns zero on success, or else the status code returned by the
1480 * underlying call that failed. On successful completion, each interface
1481 * in the original device configuration has been destroyed, and each one
1482 * in the new configuration has been probed by all relevant usb device
1483 * drivers currently known to the kernel.
1485 int usb_set_configuration(struct usb_device *dev, int configuration)
1488 struct usb_host_config *cp = NULL;
1489 struct usb_interface **new_interfaces = NULL;
1492 if (dev->authorized == 0 || configuration == -1)
1495 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1496 if (dev->config[i].desc.bConfigurationValue ==
1498 cp = &dev->config[i];
1503 if ((!cp && configuration != 0))
1506 /* The USB spec says configuration 0 means unconfigured.
1507 * But if a device includes a configuration numbered 0,
1508 * we will accept it as a correctly configured state.
1509 * Use -1 if you really want to unconfigure the device.
1511 if (cp && configuration == 0)
1512 dev_warn(&dev->dev, "config 0 descriptor??\n");
1514 /* Allocate memory for new interfaces before doing anything else,
1515 * so that if we run out then nothing will have changed. */
1518 nintf = cp->desc.bNumInterfaces;
1519 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
1521 if (!new_interfaces) {
1522 dev_err(&dev->dev, "Out of memory\n");
1526 for (; n < nintf; ++n) {
1527 new_interfaces[n] = kzalloc(
1528 sizeof(struct usb_interface),
1530 if (!new_interfaces[n]) {
1531 dev_err(&dev->dev, "Out of memory\n");
1535 kfree(new_interfaces[n]);
1536 kfree(new_interfaces);
1541 i = dev->bus_mA - cp->desc.bMaxPower * 2;
1543 dev_warn(&dev->dev, "new config #%d exceeds power "
1548 /* Wake up the device so we can send it the Set-Config request */
1549 ret = usb_autoresume_device(dev);
1551 goto free_interfaces;
1553 /* if it's already configured, clear out old state first.
1554 * getting rid of old interfaces means unbinding their drivers.
1556 if (dev->state != USB_STATE_ADDRESS)
1557 usb_disable_device(dev, 1); /* Skip ep0 */
1559 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1560 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1561 NULL, 0, USB_CTRL_SET_TIMEOUT);
1563 /* All the old state is gone, so what else can we do?
1564 * The device is probably useless now anyway.
1569 dev->actconfig = cp;
1571 usb_set_device_state(dev, USB_STATE_ADDRESS);
1572 usb_autosuspend_device(dev);
1573 goto free_interfaces;
1575 usb_set_device_state(dev, USB_STATE_CONFIGURED);
1577 /* Initialize the new interface structures and the
1578 * hc/hcd/usbcore interface/endpoint state.
1580 for (i = 0; i < nintf; ++i) {
1581 struct usb_interface_cache *intfc;
1582 struct usb_interface *intf;
1583 struct usb_host_interface *alt;
1585 cp->interface[i] = intf = new_interfaces[i];
1586 intfc = cp->intf_cache[i];
1587 intf->altsetting = intfc->altsetting;
1588 intf->num_altsetting = intfc->num_altsetting;
1589 intf->intf_assoc = find_iad(dev, cp, i);
1590 kref_get(&intfc->ref);
1592 alt = usb_altnum_to_altsetting(intf, 0);
1594 /* No altsetting 0? We'll assume the first altsetting.
1595 * We could use a GetInterface call, but if a device is
1596 * so non-compliant that it doesn't have altsetting 0
1597 * then I wouldn't trust its reply anyway.
1600 alt = &intf->altsetting[0];
1602 intf->cur_altsetting = alt;
1603 usb_enable_interface(dev, intf);
1604 intf->dev.parent = &dev->dev;
1605 intf->dev.driver = NULL;
1606 intf->dev.bus = &usb_bus_type;
1607 intf->dev.type = &usb_if_device_type;
1608 intf->dev.dma_mask = dev->dev.dma_mask;
1609 device_initialize(&intf->dev);
1610 mark_quiesced(intf);
1611 sprintf(&intf->dev.bus_id[0], "%d-%s:%d.%d",
1612 dev->bus->busnum, dev->devpath,
1613 configuration, alt->desc.bInterfaceNumber);
1615 kfree(new_interfaces);
1617 if (cp->string == NULL)
1618 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
1620 /* Now that all the interfaces are set up, register them
1621 * to trigger binding of drivers to interfaces. probe()
1622 * routines may install different altsettings and may
1623 * claim() any interfaces not yet bound. Many class drivers
1624 * need that: CDC, audio, video, etc.
1626 for (i = 0; i < nintf; ++i) {
1627 struct usb_interface *intf = cp->interface[i];
1630 "adding %s (config #%d, interface %d)\n",
1631 intf->dev.bus_id, configuration,
1632 intf->cur_altsetting->desc.bInterfaceNumber);
1633 ret = device_add(&intf->dev);
1635 dev_err(&dev->dev, "device_add(%s) --> %d\n",
1636 intf->dev.bus_id, ret);
1639 usb_create_sysfs_intf_files(intf);
1642 usb_autosuspend_device(dev);
1646 struct set_config_request {
1647 struct usb_device *udev;
1649 struct work_struct work;
1652 /* Worker routine for usb_driver_set_configuration() */
1653 static void driver_set_config_work(struct work_struct *work)
1655 struct set_config_request *req =
1656 container_of(work, struct set_config_request, work);
1658 usb_lock_device(req->udev);
1659 usb_set_configuration(req->udev, req->config);
1660 usb_unlock_device(req->udev);
1661 usb_put_dev(req->udev);
1666 * usb_driver_set_configuration - Provide a way for drivers to change device configurations
1667 * @udev: the device whose configuration is being updated
1668 * @config: the configuration being chosen.
1669 * Context: In process context, must be able to sleep
1671 * Device interface drivers are not allowed to change device configurations.
1672 * This is because changing configurations will destroy the interface the
1673 * driver is bound to and create new ones; it would be like a floppy-disk
1674 * driver telling the computer to replace the floppy-disk drive with a
1677 * Still, in certain specialized circumstances the need may arise. This
1678 * routine gets around the normal restrictions by using a work thread to
1679 * submit the change-config request.
1681 * Returns 0 if the request was succesfully queued, error code otherwise.
1682 * The caller has no way to know whether the queued request will eventually
1685 int usb_driver_set_configuration(struct usb_device *udev, int config)
1687 struct set_config_request *req;
1689 req = kmalloc(sizeof(*req), GFP_KERNEL);
1693 req->config = config;
1694 INIT_WORK(&req->work, driver_set_config_work);
1697 schedule_work(&req->work);
1700 EXPORT_SYMBOL_GPL(usb_driver_set_configuration);