2 * message.c - synchronous message handling
5 #include <linux/pci.h> /* for scatterlist macros */
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <linux/init.h>
11 #include <linux/timer.h>
12 #include <linux/ctype.h>
13 #include <linux/device.h>
14 #include <linux/usb/quirks.h>
15 #include <asm/byteorder.h>
16 #include <asm/scatterlist.h>
18 #include "hcd.h" /* for usbcore internals */
21 static void usb_api_blocking_completion(struct urb *urb)
23 complete((struct completion *)urb->context);
28 * Starts urb and waits for completion or timeout. Note that this call
29 * is NOT interruptible. Many device driver i/o requests should be
30 * interruptible and therefore these drivers should implement their
31 * own interruptible routines.
33 static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)
35 struct completion done;
38 int status = urb->status;
40 init_completion(&done);
42 urb->actual_length = 0;
43 retval = usb_submit_urb(urb, GFP_NOIO);
47 expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT;
48 if (!wait_for_completion_timeout(&done, expire)) {
50 dev_dbg(&urb->dev->dev,
51 "%s timed out on ep%d%s len=%d/%d\n",
53 usb_pipeendpoint(urb->pipe),
54 usb_pipein(urb->pipe) ? "in" : "out",
56 urb->transfer_buffer_length);
59 retval = status == -ENOENT ? -ETIMEDOUT : status;
64 *actual_length = urb->actual_length;
70 /*-------------------------------------------------------------------*/
71 // returns status (negative) or length (positive)
72 static int usb_internal_control_msg(struct usb_device *usb_dev,
74 struct usb_ctrlrequest *cmd,
75 void *data, int len, int timeout)
81 urb = usb_alloc_urb(0, GFP_NOIO);
85 usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char *)cmd, data,
86 len, usb_api_blocking_completion, NULL);
88 retv = usb_start_wait_urb(urb, timeout, &length);
96 * usb_control_msg - Builds a control urb, sends it off and waits for completion
97 * @dev: pointer to the usb device to send the message to
98 * @pipe: endpoint "pipe" to send the message to
99 * @request: USB message request value
100 * @requesttype: USB message request type value
101 * @value: USB message value
102 * @index: USB message index value
103 * @data: pointer to the data to send
104 * @size: length in bytes of the data to send
105 * @timeout: time in msecs to wait for the message to complete before
106 * timing out (if 0 the wait is forever)
107 * Context: !in_interrupt ()
109 * This function sends a simple control message to a specified endpoint
110 * and waits for the message to complete, or timeout.
112 * If successful, it returns the number of bytes transferred, otherwise a negative error number.
114 * Don't use this function from within an interrupt context, like a
115 * bottom half handler. If you need an asynchronous message, or need to send
116 * a message from within interrupt context, use usb_submit_urb()
117 * If a thread in your driver uses this call, make sure your disconnect()
118 * method can wait for it to complete. Since you don't have a handle on
119 * the URB used, you can't cancel the request.
121 int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype,
122 __u16 value, __u16 index, void *data, __u16 size, int timeout)
124 struct usb_ctrlrequest *dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
130 dr->bRequestType= requesttype;
131 dr->bRequest = request;
132 dr->wValue = cpu_to_le16p(&value);
133 dr->wIndex = cpu_to_le16p(&index);
134 dr->wLength = cpu_to_le16p(&size);
136 //dbg("usb_control_msg");
138 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
147 * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion
148 * @usb_dev: pointer to the usb device to send the message to
149 * @pipe: endpoint "pipe" to send the message to
150 * @data: pointer to the data to send
151 * @len: length in bytes of the data to send
152 * @actual_length: pointer to a location to put the actual length transferred in bytes
153 * @timeout: time in msecs to wait for the message to complete before
154 * timing out (if 0 the wait is forever)
155 * Context: !in_interrupt ()
157 * This function sends a simple interrupt message to a specified endpoint and
158 * waits for the message to complete, or timeout.
160 * If successful, it returns 0, otherwise a negative error number. The number
161 * of actual bytes transferred will be stored in the actual_length paramater.
163 * Don't use this function from within an interrupt context, like a bottom half
164 * handler. If you need an asynchronous message, or need to send a message
165 * from within interrupt context, use usb_submit_urb() If a thread in your
166 * driver uses this call, make sure your disconnect() method can wait for it to
167 * complete. Since you don't have a handle on the URB used, you can't cancel
170 int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
171 void *data, int len, int *actual_length, int timeout)
173 return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout);
175 EXPORT_SYMBOL_GPL(usb_interrupt_msg);
178 * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
179 * @usb_dev: pointer to the usb device to send the message to
180 * @pipe: endpoint "pipe" to send the message to
181 * @data: pointer to the data to send
182 * @len: length in bytes of the data to send
183 * @actual_length: pointer to a location to put the actual length transferred in bytes
184 * @timeout: time in msecs to wait for the message to complete before
185 * timing out (if 0 the wait is forever)
186 * Context: !in_interrupt ()
188 * This function sends a simple bulk message to a specified endpoint
189 * and waits for the message to complete, or timeout.
191 * If successful, it returns 0, otherwise a negative error number.
192 * The number of actual bytes transferred will be stored in the
193 * actual_length paramater.
195 * Don't use this function from within an interrupt context, like a
196 * bottom half handler. If you need an asynchronous message, or need to
197 * send a message from within interrupt context, use usb_submit_urb()
198 * If a thread in your driver uses this call, make sure your disconnect()
199 * method can wait for it to complete. Since you don't have a handle on
200 * the URB used, you can't cancel the request.
202 * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT
203 * ioctl, users are forced to abuse this routine by using it to submit
204 * URBs for interrupt endpoints. We will take the liberty of creating
205 * an interrupt URB (with the default interval) if the target is an
206 * interrupt endpoint.
208 int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
209 void *data, int len, int *actual_length, int timeout)
212 struct usb_host_endpoint *ep;
214 ep = (usb_pipein(pipe) ? usb_dev->ep_in : usb_dev->ep_out)
215 [usb_pipeendpoint(pipe)];
219 urb = usb_alloc_urb(0, GFP_KERNEL);
223 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
224 USB_ENDPOINT_XFER_INT) {
225 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
226 usb_fill_int_urb(urb, usb_dev, pipe, data, len,
227 usb_api_blocking_completion, NULL,
230 usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
231 usb_api_blocking_completion, NULL);
233 return usb_start_wait_urb(urb, timeout, actual_length);
236 /*-------------------------------------------------------------------*/
238 static void sg_clean (struct usb_sg_request *io)
241 while (io->entries--)
242 usb_free_urb (io->urbs [io->entries]);
246 if (io->dev->dev.dma_mask != NULL)
247 usb_buffer_unmap_sg (io->dev, io->pipe, io->sg, io->nents);
251 static void sg_complete (struct urb *urb)
253 struct usb_sg_request *io = urb->context;
254 int status = urb->status;
256 spin_lock (&io->lock);
258 /* In 2.5 we require hcds' endpoint queues not to progress after fault
259 * reports, until the completion callback (this!) returns. That lets
260 * device driver code (like this routine) unlink queued urbs first,
261 * if it needs to, since the HC won't work on them at all. So it's
262 * not possible for page N+1 to overwrite page N, and so on.
264 * That's only for "hard" faults; "soft" faults (unlinks) sometimes
265 * complete before the HCD can get requests away from hardware,
266 * though never during cleanup after a hard fault.
269 && (io->status != -ECONNRESET
270 || status != -ECONNRESET)
271 && urb->actual_length) {
272 dev_err (io->dev->bus->controller,
273 "dev %s ep%d%s scatterlist error %d/%d\n",
275 usb_pipeendpoint (urb->pipe),
276 usb_pipein (urb->pipe) ? "in" : "out",
281 if (io->status == 0 && status && status != -ECONNRESET) {
282 int i, found, retval;
286 /* the previous urbs, and this one, completed already.
287 * unlink pending urbs so they won't rx/tx bad data.
288 * careful: unlink can sometimes be synchronous...
290 spin_unlock (&io->lock);
291 for (i = 0, found = 0; i < io->entries; i++) {
292 if (!io->urbs [i] || !io->urbs [i]->dev)
295 retval = usb_unlink_urb (io->urbs [i]);
296 if (retval != -EINPROGRESS &&
299 dev_err (&io->dev->dev,
300 "%s, unlink --> %d\n",
301 __FUNCTION__, retval);
302 } else if (urb == io->urbs [i])
305 spin_lock (&io->lock);
309 /* on the last completion, signal usb_sg_wait() */
310 io->bytes += urb->actual_length;
313 complete (&io->complete);
315 spin_unlock (&io->lock);
320 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
321 * @io: request block being initialized. until usb_sg_wait() returns,
322 * treat this as a pointer to an opaque block of memory,
323 * @dev: the usb device that will send or receive the data
324 * @pipe: endpoint "pipe" used to transfer the data
325 * @period: polling rate for interrupt endpoints, in frames or
326 * (for high speed endpoints) microframes; ignored for bulk
327 * @sg: scatterlist entries
328 * @nents: how many entries in the scatterlist
329 * @length: how many bytes to send from the scatterlist, or zero to
330 * send every byte identified in the list.
331 * @mem_flags: SLAB_* flags affecting memory allocations in this call
333 * Returns zero for success, else a negative errno value. This initializes a
334 * scatter/gather request, allocating resources such as I/O mappings and urb
335 * memory (except maybe memory used by USB controller drivers).
337 * The request must be issued using usb_sg_wait(), which waits for the I/O to
338 * complete (or to be canceled) and then cleans up all resources allocated by
341 * The request may be canceled with usb_sg_cancel(), either before or after
342 * usb_sg_wait() is called.
345 struct usb_sg_request *io,
346 struct usb_device *dev,
349 struct scatterlist *sg,
359 if (!io || !dev || !sg
360 || usb_pipecontrol (pipe)
361 || usb_pipeisoc (pipe)
365 spin_lock_init (&io->lock);
371 /* not all host controllers use DMA (like the mainstream pci ones);
372 * they can use PIO (sl811) or be software over another transport.
374 dma = (dev->dev.dma_mask != NULL);
376 io->entries = usb_buffer_map_sg (dev, pipe, sg, nents);
380 /* initialize all the urbs we'll use */
381 if (io->entries <= 0)
384 io->count = io->entries;
385 io->urbs = kmalloc (io->entries * sizeof *io->urbs, mem_flags);
389 urb_flags = URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT;
390 if (usb_pipein (pipe))
391 urb_flags |= URB_SHORT_NOT_OK;
393 for (i = 0; i < io->entries; i++) {
396 io->urbs [i] = usb_alloc_urb (0, mem_flags);
402 io->urbs [i]->dev = NULL;
403 io->urbs [i]->pipe = pipe;
404 io->urbs [i]->interval = period;
405 io->urbs [i]->transfer_flags = urb_flags;
407 io->urbs [i]->complete = sg_complete;
408 io->urbs [i]->context = io;
411 * Some systems need to revert to PIO when DMA is temporarily
412 * unavailable. For their sakes, both transfer_buffer and
413 * transfer_dma are set when possible. However this can only
414 * work on systems without HIGHMEM, since DMA buffers located
415 * in high memory are not directly addressable by the CPU for
416 * PIO ... so when HIGHMEM is in use, transfer_buffer is NULL
417 * to prevent stale pointers and to help spot bugs.
420 io->urbs [i]->transfer_dma = sg_dma_address (sg + i);
421 len = sg_dma_len (sg + i);
422 #ifdef CONFIG_HIGHMEM
423 io->urbs[i]->transfer_buffer = NULL;
425 io->urbs[i]->transfer_buffer =
426 page_address(sg[i].page) + sg[i].offset;
429 /* hc may use _only_ transfer_buffer */
430 io->urbs [i]->transfer_buffer =
431 page_address (sg [i].page) + sg [i].offset;
436 len = min_t (unsigned, len, length);
441 io->urbs [i]->transfer_buffer_length = len;
443 io->urbs [--i]->transfer_flags &= ~URB_NO_INTERRUPT;
445 /* transaction state */
448 init_completion (&io->complete);
458 * usb_sg_wait - synchronously execute scatter/gather request
459 * @io: request block handle, as initialized with usb_sg_init().
460 * some fields become accessible when this call returns.
461 * Context: !in_interrupt ()
463 * This function blocks until the specified I/O operation completes. It
464 * leverages the grouping of the related I/O requests to get good transfer
465 * rates, by queueing the requests. At higher speeds, such queuing can
466 * significantly improve USB throughput.
468 * There are three kinds of completion for this function.
469 * (1) success, where io->status is zero. The number of io->bytes
470 * transferred is as requested.
471 * (2) error, where io->status is a negative errno value. The number
472 * of io->bytes transferred before the error is usually less
473 * than requested, and can be nonzero.
474 * (3) cancellation, a type of error with status -ECONNRESET that
475 * is initiated by usb_sg_cancel().
477 * When this function returns, all memory allocated through usb_sg_init() or
478 * this call will have been freed. The request block parameter may still be
479 * passed to usb_sg_cancel(), or it may be freed. It could also be
480 * reinitialized and then reused.
482 * Data Transfer Rates:
484 * Bulk transfers are valid for full or high speed endpoints.
485 * The best full speed data rate is 19 packets of 64 bytes each
486 * per frame, or 1216 bytes per millisecond.
487 * The best high speed data rate is 13 packets of 512 bytes each
488 * per microframe, or 52 KBytes per millisecond.
490 * The reason to use interrupt transfers through this API would most likely
491 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
492 * could be transferred. That capability is less useful for low or full
493 * speed interrupt endpoints, which allow at most one packet per millisecond,
494 * of at most 8 or 64 bytes (respectively).
496 void usb_sg_wait (struct usb_sg_request *io)
498 int i, entries = io->entries;
500 /* queue the urbs. */
501 spin_lock_irq (&io->lock);
503 while (i < entries && !io->status) {
506 io->urbs [i]->dev = io->dev;
507 retval = usb_submit_urb (io->urbs [i], GFP_ATOMIC);
509 /* after we submit, let completions or cancelations fire;
510 * we handshake using io->status.
512 spin_unlock_irq (&io->lock);
514 /* maybe we retrying will recover */
515 case -ENXIO: // hc didn't queue this one
518 io->urbs[i]->dev = NULL;
523 /* no error? continue immediately.
525 * NOTE: to work better with UHCI (4K I/O buffer may
526 * need 3K of TDs) it may be good to limit how many
527 * URBs are queued at once; N milliseconds?
534 /* fail any uncompleted urbs */
536 io->urbs [i]->dev = NULL;
537 io->urbs [i]->status = retval;
538 dev_dbg (&io->dev->dev, "%s, submit --> %d\n",
539 __FUNCTION__, retval);
542 spin_lock_irq (&io->lock);
543 if (retval && (io->status == 0 || io->status == -ECONNRESET))
546 io->count -= entries - i;
548 complete (&io->complete);
549 spin_unlock_irq (&io->lock);
551 /* OK, yes, this could be packaged as non-blocking.
552 * So could the submit loop above ... but it's easier to
553 * solve neither problem than to solve both!
555 wait_for_completion (&io->complete);
561 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
562 * @io: request block, initialized with usb_sg_init()
564 * This stops a request after it has been started by usb_sg_wait().
565 * It can also prevents one initialized by usb_sg_init() from starting,
566 * so that call just frees resources allocated to the request.
568 void usb_sg_cancel (struct usb_sg_request *io)
572 spin_lock_irqsave (&io->lock, flags);
574 /* shut everything down, if it didn't already */
578 io->status = -ECONNRESET;
579 spin_unlock (&io->lock);
580 for (i = 0; i < io->entries; i++) {
583 if (!io->urbs [i]->dev)
585 retval = usb_unlink_urb (io->urbs [i]);
586 if (retval != -EINPROGRESS && retval != -EBUSY)
587 dev_warn (&io->dev->dev, "%s, unlink --> %d\n",
588 __FUNCTION__, retval);
590 spin_lock (&io->lock);
592 spin_unlock_irqrestore (&io->lock, flags);
595 /*-------------------------------------------------------------------*/
598 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
599 * @dev: the device whose descriptor is being retrieved
600 * @type: the descriptor type (USB_DT_*)
601 * @index: the number of the descriptor
602 * @buf: where to put the descriptor
603 * @size: how big is "buf"?
604 * Context: !in_interrupt ()
606 * Gets a USB descriptor. Convenience functions exist to simplify
607 * getting some types of descriptors. Use
608 * usb_get_string() or usb_string() for USB_DT_STRING.
609 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
610 * are part of the device structure.
611 * In addition to a number of USB-standard descriptors, some
612 * devices also use class-specific or vendor-specific descriptors.
614 * This call is synchronous, and may not be used in an interrupt context.
616 * Returns the number of bytes received on success, or else the status code
617 * returned by the underlying usb_control_msg() call.
619 int usb_get_descriptor(struct usb_device *dev, unsigned char type, unsigned char index, void *buf, int size)
624 memset(buf,0,size); // Make sure we parse really received data
626 for (i = 0; i < 3; ++i) {
627 /* retry on length 0 or stall; some devices are flakey */
628 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
629 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
630 (type << 8) + index, 0, buf, size,
631 USB_CTRL_GET_TIMEOUT);
632 if (result == 0 || result == -EPIPE)
634 if (result > 1 && ((u8 *)buf)[1] != type) {
644 * usb_get_string - gets a string descriptor
645 * @dev: the device whose string descriptor is being retrieved
646 * @langid: code for language chosen (from string descriptor zero)
647 * @index: the number of the descriptor
648 * @buf: where to put the string
649 * @size: how big is "buf"?
650 * Context: !in_interrupt ()
652 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
653 * in little-endian byte order).
654 * The usb_string() function will often be a convenient way to turn
655 * these strings into kernel-printable form.
657 * Strings may be referenced in device, configuration, interface, or other
658 * descriptors, and could also be used in vendor-specific ways.
660 * This call is synchronous, and may not be used in an interrupt context.
662 * Returns the number of bytes received on success, or else the status code
663 * returned by the underlying usb_control_msg() call.
665 static int usb_get_string(struct usb_device *dev, unsigned short langid,
666 unsigned char index, void *buf, int size)
671 for (i = 0; i < 3; ++i) {
672 /* retry on length 0 or stall; some devices are flakey */
673 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
674 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
675 (USB_DT_STRING << 8) + index, langid, buf, size,
676 USB_CTRL_GET_TIMEOUT);
677 if (!(result == 0 || result == -EPIPE))
683 static void usb_try_string_workarounds(unsigned char *buf, int *length)
685 int newlength, oldlength = *length;
687 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
688 if (!isprint(buf[newlength]) || buf[newlength + 1])
697 static int usb_string_sub(struct usb_device *dev, unsigned int langid,
698 unsigned int index, unsigned char *buf)
702 /* Try to read the string descriptor by asking for the maximum
703 * possible number of bytes */
704 if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
707 rc = usb_get_string(dev, langid, index, buf, 255);
709 /* If that failed try to read the descriptor length, then
710 * ask for just that many bytes */
712 rc = usb_get_string(dev, langid, index, buf, 2);
714 rc = usb_get_string(dev, langid, index, buf, buf[0]);
718 if (!buf[0] && !buf[1])
719 usb_try_string_workarounds(buf, &rc);
721 /* There might be extra junk at the end of the descriptor */
725 rc = rc - (rc & 1); /* force a multiple of two */
729 rc = (rc < 0 ? rc : -EINVAL);
735 * usb_string - returns ISO 8859-1 version of a string descriptor
736 * @dev: the device whose string descriptor is being retrieved
737 * @index: the number of the descriptor
738 * @buf: where to put the string
739 * @size: how big is "buf"?
740 * Context: !in_interrupt ()
742 * This converts the UTF-16LE encoded strings returned by devices, from
743 * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones
744 * that are more usable in most kernel contexts. Note that all characters
745 * in the chosen descriptor that can't be encoded using ISO-8859-1
746 * are converted to the question mark ("?") character, and this function
747 * chooses strings in the first language supported by the device.
749 * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit
750 * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode,
751 * and is appropriate for use many uses of English and several other
752 * Western European languages. (But it doesn't include the "Euro" symbol.)
754 * This call is synchronous, and may not be used in an interrupt context.
756 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
758 int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
764 if (dev->state == USB_STATE_SUSPENDED)
765 return -EHOSTUNREACH;
766 if (size <= 0 || !buf || !index)
769 tbuf = kmalloc(256, GFP_KERNEL);
773 /* get langid for strings if it's not yet known */
774 if (!dev->have_langid) {
775 err = usb_string_sub(dev, 0, 0, tbuf);
778 "string descriptor 0 read error: %d\n",
781 } else if (err < 4) {
782 dev_err (&dev->dev, "string descriptor 0 too short\n");
786 dev->have_langid = 1;
787 dev->string_langid = tbuf[2] | (tbuf[3]<< 8);
788 /* always use the first langid listed */
789 dev_dbg (&dev->dev, "default language 0x%04x\n",
794 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
798 size--; /* leave room for trailing NULL char in output buffer */
799 for (idx = 0, u = 2; u < err; u += 2) {
802 if (tbuf[u+1]) /* high byte */
803 buf[idx++] = '?'; /* non ISO-8859-1 character */
805 buf[idx++] = tbuf[u];
810 if (tbuf[1] != USB_DT_STRING)
811 dev_dbg(&dev->dev, "wrong descriptor type %02x for string %d (\"%s\")\n", tbuf[1], index, buf);
819 * usb_cache_string - read a string descriptor and cache it for later use
820 * @udev: the device whose string descriptor is being read
821 * @index: the descriptor index
823 * Returns a pointer to a kmalloc'ed buffer containing the descriptor string,
824 * or NULL if the index is 0 or the string could not be read.
826 char *usb_cache_string(struct usb_device *udev, int index)
829 char *smallbuf = NULL;
832 if (index > 0 && (buf = kmalloc(256, GFP_KERNEL)) != NULL) {
833 if ((len = usb_string(udev, index, buf, 256)) > 0) {
834 if ((smallbuf = kmalloc(++len, GFP_KERNEL)) == NULL)
836 memcpy(smallbuf, buf, len);
844 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
845 * @dev: the device whose device descriptor is being updated
846 * @size: how much of the descriptor to read
847 * Context: !in_interrupt ()
849 * Updates the copy of the device descriptor stored in the device structure,
850 * which dedicates space for this purpose.
852 * Not exported, only for use by the core. If drivers really want to read
853 * the device descriptor directly, they can call usb_get_descriptor() with
854 * type = USB_DT_DEVICE and index = 0.
856 * This call is synchronous, and may not be used in an interrupt context.
858 * Returns the number of bytes received on success, or else the status code
859 * returned by the underlying usb_control_msg() call.
861 int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
863 struct usb_device_descriptor *desc;
866 if (size > sizeof(*desc))
868 desc = kmalloc(sizeof(*desc), GFP_NOIO);
872 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
874 memcpy(&dev->descriptor, desc, size);
880 * usb_get_status - issues a GET_STATUS call
881 * @dev: the device whose status is being checked
882 * @type: USB_RECIP_*; for device, interface, or endpoint
883 * @target: zero (for device), else interface or endpoint number
884 * @data: pointer to two bytes of bitmap data
885 * Context: !in_interrupt ()
887 * Returns device, interface, or endpoint status. Normally only of
888 * interest to see if the device is self powered, or has enabled the
889 * remote wakeup facility; or whether a bulk or interrupt endpoint
890 * is halted ("stalled").
892 * Bits in these status bitmaps are set using the SET_FEATURE request,
893 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
894 * function should be used to clear halt ("stall") status.
896 * This call is synchronous, and may not be used in an interrupt context.
898 * Returns the number of bytes received on success, or else the status code
899 * returned by the underlying usb_control_msg() call.
901 int usb_get_status(struct usb_device *dev, int type, int target, void *data)
904 u16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
909 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
910 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
911 sizeof(*status), USB_CTRL_GET_TIMEOUT);
913 *(u16 *)data = *status;
919 * usb_clear_halt - tells device to clear endpoint halt/stall condition
920 * @dev: device whose endpoint is halted
921 * @pipe: endpoint "pipe" being cleared
922 * Context: !in_interrupt ()
924 * This is used to clear halt conditions for bulk and interrupt endpoints,
925 * as reported by URB completion status. Endpoints that are halted are
926 * sometimes referred to as being "stalled". Such endpoints are unable
927 * to transmit or receive data until the halt status is cleared. Any URBs
928 * queued for such an endpoint should normally be unlinked by the driver
929 * before clearing the halt condition, as described in sections 5.7.5
930 * and 5.8.5 of the USB 2.0 spec.
932 * Note that control and isochronous endpoints don't halt, although control
933 * endpoints report "protocol stall" (for unsupported requests) using the
934 * same status code used to report a true stall.
936 * This call is synchronous, and may not be used in an interrupt context.
938 * Returns zero on success, or else the status code returned by the
939 * underlying usb_control_msg() call.
941 int usb_clear_halt(struct usb_device *dev, int pipe)
944 int endp = usb_pipeendpoint(pipe);
946 if (usb_pipein (pipe))
949 /* we don't care if it wasn't halted first. in fact some devices
950 * (like some ibmcam model 1 units) seem to expect hosts to make
951 * this request for iso endpoints, which can't halt!
953 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
954 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
955 USB_ENDPOINT_HALT, endp, NULL, 0,
956 USB_CTRL_SET_TIMEOUT);
958 /* don't un-halt or force to DATA0 except on success */
962 /* NOTE: seems like Microsoft and Apple don't bother verifying
963 * the clear "took", so some devices could lock up if you check...
964 * such as the Hagiwara FlashGate DUAL. So we won't bother.
966 * NOTE: make sure the logic here doesn't diverge much from
967 * the copy in usb-storage, for as long as we need two copies.
970 /* toggle was reset by the clear */
971 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
977 * usb_disable_endpoint -- Disable an endpoint by address
978 * @dev: the device whose endpoint is being disabled
979 * @epaddr: the endpoint's address. Endpoint number for output,
980 * endpoint number + USB_DIR_IN for input
982 * Deallocates hcd/hardware state for this endpoint ... and nukes all
985 * If the HCD hasn't registered a disable() function, this sets the
986 * endpoint's maxpacket size to 0 to prevent further submissions.
988 void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr)
990 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
991 struct usb_host_endpoint *ep;
996 if (usb_endpoint_out(epaddr)) {
997 ep = dev->ep_out[epnum];
998 dev->ep_out[epnum] = NULL;
1000 ep = dev->ep_in[epnum];
1001 dev->ep_in[epnum] = NULL;
1004 usb_hcd_endpoint_disable(dev, ep);
1008 * usb_disable_interface -- Disable all endpoints for an interface
1009 * @dev: the device whose interface is being disabled
1010 * @intf: pointer to the interface descriptor
1012 * Disables all the endpoints for the interface's current altsetting.
1014 void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf)
1016 struct usb_host_interface *alt = intf->cur_altsetting;
1019 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
1020 usb_disable_endpoint(dev,
1021 alt->endpoint[i].desc.bEndpointAddress);
1026 * usb_disable_device - Disable all the endpoints for a USB device
1027 * @dev: the device whose endpoints are being disabled
1028 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
1030 * Disables all the device's endpoints, potentially including endpoint 0.
1031 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1032 * pending urbs) and usbcore state for the interfaces, so that usbcore
1033 * must usb_set_configuration() before any interfaces could be used.
1035 void usb_disable_device(struct usb_device *dev, int skip_ep0)
1039 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __FUNCTION__,
1040 skip_ep0 ? "non-ep0" : "all");
1041 for (i = skip_ep0; i < 16; ++i) {
1042 usb_disable_endpoint(dev, i);
1043 usb_disable_endpoint(dev, i + USB_DIR_IN);
1045 dev->toggle[0] = dev->toggle[1] = 0;
1047 /* getting rid of interfaces will disconnect
1048 * any drivers bound to them (a key side effect)
1050 if (dev->actconfig) {
1051 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1052 struct usb_interface *interface;
1054 /* remove this interface if it has been registered */
1055 interface = dev->actconfig->interface[i];
1056 if (!device_is_registered(&interface->dev))
1058 dev_dbg (&dev->dev, "unregistering interface %s\n",
1059 interface->dev.bus_id);
1060 usb_remove_sysfs_intf_files(interface);
1061 device_del (&interface->dev);
1064 /* Now that the interfaces are unbound, nobody should
1065 * try to access them.
1067 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1068 put_device (&dev->actconfig->interface[i]->dev);
1069 dev->actconfig->interface[i] = NULL;
1071 dev->actconfig = NULL;
1072 if (dev->state == USB_STATE_CONFIGURED)
1073 usb_set_device_state(dev, USB_STATE_ADDRESS);
1079 * usb_enable_endpoint - Enable an endpoint for USB communications
1080 * @dev: the device whose interface is being enabled
1083 * Resets the endpoint toggle, and sets dev->ep_{in,out} pointers.
1084 * For control endpoints, both the input and output sides are handled.
1087 usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep)
1089 unsigned int epaddr = ep->desc.bEndpointAddress;
1090 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1093 is_control = ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1094 == USB_ENDPOINT_XFER_CONTROL);
1095 if (usb_endpoint_out(epaddr) || is_control) {
1096 usb_settoggle(dev, epnum, 1, 0);
1097 dev->ep_out[epnum] = ep;
1099 if (!usb_endpoint_out(epaddr) || is_control) {
1100 usb_settoggle(dev, epnum, 0, 0);
1101 dev->ep_in[epnum] = ep;
1106 * usb_enable_interface - Enable all the endpoints for an interface
1107 * @dev: the device whose interface is being enabled
1108 * @intf: pointer to the interface descriptor
1110 * Enables all the endpoints for the interface's current altsetting.
1112 static void usb_enable_interface(struct usb_device *dev,
1113 struct usb_interface *intf)
1115 struct usb_host_interface *alt = intf->cur_altsetting;
1118 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1119 usb_enable_endpoint(dev, &alt->endpoint[i]);
1123 * usb_set_interface - Makes a particular alternate setting be current
1124 * @dev: the device whose interface is being updated
1125 * @interface: the interface being updated
1126 * @alternate: the setting being chosen.
1127 * Context: !in_interrupt ()
1129 * This is used to enable data transfers on interfaces that may not
1130 * be enabled by default. Not all devices support such configurability.
1131 * Only the driver bound to an interface may change its setting.
1133 * Within any given configuration, each interface may have several
1134 * alternative settings. These are often used to control levels of
1135 * bandwidth consumption. For example, the default setting for a high
1136 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1137 * while interrupt transfers of up to 3KBytes per microframe are legal.
1138 * Also, isochronous endpoints may never be part of an
1139 * interface's default setting. To access such bandwidth, alternate
1140 * interface settings must be made current.
1142 * Note that in the Linux USB subsystem, bandwidth associated with
1143 * an endpoint in a given alternate setting is not reserved until an URB
1144 * is submitted that needs that bandwidth. Some other operating systems
1145 * allocate bandwidth early, when a configuration is chosen.
1147 * This call is synchronous, and may not be used in an interrupt context.
1148 * Also, drivers must not change altsettings while urbs are scheduled for
1149 * endpoints in that interface; all such urbs must first be completed
1150 * (perhaps forced by unlinking).
1152 * Returns zero on success, or else the status code returned by the
1153 * underlying usb_control_msg() call.
1155 int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1157 struct usb_interface *iface;
1158 struct usb_host_interface *alt;
1162 if (dev->state == USB_STATE_SUSPENDED)
1163 return -EHOSTUNREACH;
1165 iface = usb_ifnum_to_if(dev, interface);
1167 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1172 alt = usb_altnum_to_altsetting(iface, alternate);
1174 warn("selecting invalid altsetting %d", alternate);
1178 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1179 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1180 alternate, interface, NULL, 0, 5000);
1182 /* 9.4.10 says devices don't need this and are free to STALL the
1183 * request if the interface only has one alternate setting.
1185 if (ret == -EPIPE && iface->num_altsetting == 1) {
1187 "manual set_interface for iface %d, alt %d\n",
1188 interface, alternate);
1193 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1194 * when they implement async or easily-killable versions of this or
1195 * other "should-be-internal" functions (like clear_halt).
1196 * should hcd+usbcore postprocess control requests?
1199 /* prevent submissions using previous endpoint settings */
1200 if (device_is_registered(&iface->dev))
1201 usb_remove_sysfs_intf_files(iface);
1202 usb_disable_interface(dev, iface);
1204 iface->cur_altsetting = alt;
1206 /* If the interface only has one altsetting and the device didn't
1207 * accept the request, we attempt to carry out the equivalent action
1208 * by manually clearing the HALT feature for each endpoint in the
1214 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
1215 unsigned int epaddr =
1216 alt->endpoint[i].desc.bEndpointAddress;
1218 __create_pipe(dev, USB_ENDPOINT_NUMBER_MASK & epaddr)
1219 | (usb_endpoint_out(epaddr) ? USB_DIR_OUT : USB_DIR_IN);
1221 usb_clear_halt(dev, pipe);
1225 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1228 * Despite EP0 is always present in all interfaces/AS, the list of
1229 * endpoints from the descriptor does not contain EP0. Due to its
1230 * omnipresence one might expect EP0 being considered "affected" by
1231 * any SetInterface request and hence assume toggles need to be reset.
1232 * However, EP0 toggles are re-synced for every individual transfer
1233 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1234 * (Likewise, EP0 never "halts" on well designed devices.)
1236 usb_enable_interface(dev, iface);
1237 if (device_is_registered(&iface->dev))
1238 usb_create_sysfs_intf_files(iface);
1244 * usb_reset_configuration - lightweight device reset
1245 * @dev: the device whose configuration is being reset
1247 * This issues a standard SET_CONFIGURATION request to the device using
1248 * the current configuration. The effect is to reset most USB-related
1249 * state in the device, including interface altsettings (reset to zero),
1250 * endpoint halts (cleared), and data toggle (only for bulk and interrupt
1251 * endpoints). Other usbcore state is unchanged, including bindings of
1252 * usb device drivers to interfaces.
1254 * Because this affects multiple interfaces, avoid using this with composite
1255 * (multi-interface) devices. Instead, the driver for each interface may
1256 * use usb_set_interface() on the interfaces it claims. Be careful though;
1257 * some devices don't support the SET_INTERFACE request, and others won't
1258 * reset all the interface state (notably data toggles). Resetting the whole
1259 * configuration would affect other drivers' interfaces.
1261 * The caller must own the device lock.
1263 * Returns zero on success, else a negative error code.
1265 int usb_reset_configuration(struct usb_device *dev)
1268 struct usb_host_config *config;
1270 if (dev->state == USB_STATE_SUSPENDED)
1271 return -EHOSTUNREACH;
1273 /* caller must have locked the device and must own
1274 * the usb bus readlock (so driver bindings are stable);
1275 * calls during probe() are fine
1278 for (i = 1; i < 16; ++i) {
1279 usb_disable_endpoint(dev, i);
1280 usb_disable_endpoint(dev, i + USB_DIR_IN);
1283 config = dev->actconfig;
1284 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1285 USB_REQ_SET_CONFIGURATION, 0,
1286 config->desc.bConfigurationValue, 0,
1287 NULL, 0, USB_CTRL_SET_TIMEOUT);
1291 dev->toggle[0] = dev->toggle[1] = 0;
1293 /* re-init hc/hcd interface/endpoint state */
1294 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1295 struct usb_interface *intf = config->interface[i];
1296 struct usb_host_interface *alt;
1298 if (device_is_registered(&intf->dev))
1299 usb_remove_sysfs_intf_files(intf);
1300 alt = usb_altnum_to_altsetting(intf, 0);
1302 /* No altsetting 0? We'll assume the first altsetting.
1303 * We could use a GetInterface call, but if a device is
1304 * so non-compliant that it doesn't have altsetting 0
1305 * then I wouldn't trust its reply anyway.
1308 alt = &intf->altsetting[0];
1310 intf->cur_altsetting = alt;
1311 usb_enable_interface(dev, intf);
1312 if (device_is_registered(&intf->dev))
1313 usb_create_sysfs_intf_files(intf);
1318 void usb_release_interface(struct device *dev)
1320 struct usb_interface *intf = to_usb_interface(dev);
1321 struct usb_interface_cache *intfc =
1322 altsetting_to_usb_interface_cache(intf->altsetting);
1324 kref_put(&intfc->ref, usb_release_interface_cache);
1328 #ifdef CONFIG_HOTPLUG
1329 static int usb_if_uevent(struct device *dev, char **envp, int num_envp,
1330 char *buffer, int buffer_size)
1332 struct usb_device *usb_dev;
1333 struct usb_interface *intf;
1334 struct usb_host_interface *alt;
1341 /* driver is often null here; dev_dbg() would oops */
1342 pr_debug ("usb %s: uevent\n", dev->bus_id);
1344 intf = to_usb_interface(dev);
1345 usb_dev = interface_to_usbdev(intf);
1346 alt = intf->cur_altsetting;
1348 if (add_uevent_var(envp, num_envp, &i,
1349 buffer, buffer_size, &length,
1350 "INTERFACE=%d/%d/%d",
1351 alt->desc.bInterfaceClass,
1352 alt->desc.bInterfaceSubClass,
1353 alt->desc.bInterfaceProtocol))
1356 if (add_uevent_var(envp, num_envp, &i,
1357 buffer, buffer_size, &length,
1358 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
1359 le16_to_cpu(usb_dev->descriptor.idVendor),
1360 le16_to_cpu(usb_dev->descriptor.idProduct),
1361 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1362 usb_dev->descriptor.bDeviceClass,
1363 usb_dev->descriptor.bDeviceSubClass,
1364 usb_dev->descriptor.bDeviceProtocol,
1365 alt->desc.bInterfaceClass,
1366 alt->desc.bInterfaceSubClass,
1367 alt->desc.bInterfaceProtocol))
1376 static int usb_if_uevent(struct device *dev, char **envp,
1377 int num_envp, char *buffer, int buffer_size)
1381 #endif /* CONFIG_HOTPLUG */
1383 struct device_type usb_if_device_type = {
1384 .name = "usb_interface",
1385 .release = usb_release_interface,
1386 .uevent = usb_if_uevent,
1389 static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
1390 struct usb_host_config *config,
1393 struct usb_interface_assoc_descriptor *retval = NULL;
1394 struct usb_interface_assoc_descriptor *intf_assoc;
1399 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
1400 intf_assoc = config->intf_assoc[i];
1401 if (intf_assoc->bInterfaceCount == 0)
1404 first_intf = intf_assoc->bFirstInterface;
1405 last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
1406 if (inum >= first_intf && inum <= last_intf) {
1408 retval = intf_assoc;
1410 dev_err(&dev->dev, "Interface #%d referenced"
1411 " by multiple IADs\n", inum);
1420 * usb_set_configuration - Makes a particular device setting be current
1421 * @dev: the device whose configuration is being updated
1422 * @configuration: the configuration being chosen.
1423 * Context: !in_interrupt(), caller owns the device lock
1425 * This is used to enable non-default device modes. Not all devices
1426 * use this kind of configurability; many devices only have one
1429 * @configuration is the value of the configuration to be installed.
1430 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1431 * must be non-zero; a value of zero indicates that the device in
1432 * unconfigured. However some devices erroneously use 0 as one of their
1433 * configuration values. To help manage such devices, this routine will
1434 * accept @configuration = -1 as indicating the device should be put in
1435 * an unconfigured state.
1437 * USB device configurations may affect Linux interoperability,
1438 * power consumption and the functionality available. For example,
1439 * the default configuration is limited to using 100mA of bus power,
1440 * so that when certain device functionality requires more power,
1441 * and the device is bus powered, that functionality should be in some
1442 * non-default device configuration. Other device modes may also be
1443 * reflected as configuration options, such as whether two ISDN
1444 * channels are available independently; and choosing between open
1445 * standard device protocols (like CDC) or proprietary ones.
1447 * Note that USB has an additional level of device configurability,
1448 * associated with interfaces. That configurability is accessed using
1449 * usb_set_interface().
1451 * This call is synchronous. The calling context must be able to sleep,
1452 * must own the device lock, and must not hold the driver model's USB
1453 * bus mutex; usb device driver probe() methods cannot use this routine.
1455 * Returns zero on success, or else the status code returned by the
1456 * underlying call that failed. On successful completion, each interface
1457 * in the original device configuration has been destroyed, and each one
1458 * in the new configuration has been probed by all relevant usb device
1459 * drivers currently known to the kernel.
1461 int usb_set_configuration(struct usb_device *dev, int configuration)
1464 struct usb_host_config *cp = NULL;
1465 struct usb_interface **new_interfaces = NULL;
1468 if (configuration == -1)
1471 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1472 if (dev->config[i].desc.bConfigurationValue ==
1474 cp = &dev->config[i];
1479 if ((!cp && configuration != 0))
1482 /* The USB spec says configuration 0 means unconfigured.
1483 * But if a device includes a configuration numbered 0,
1484 * we will accept it as a correctly configured state.
1485 * Use -1 if you really want to unconfigure the device.
1487 if (cp && configuration == 0)
1488 dev_warn(&dev->dev, "config 0 descriptor??\n");
1490 /* Allocate memory for new interfaces before doing anything else,
1491 * so that if we run out then nothing will have changed. */
1494 nintf = cp->desc.bNumInterfaces;
1495 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
1497 if (!new_interfaces) {
1498 dev_err(&dev->dev, "Out of memory");
1502 for (; n < nintf; ++n) {
1503 new_interfaces[n] = kzalloc(
1504 sizeof(struct usb_interface),
1506 if (!new_interfaces[n]) {
1507 dev_err(&dev->dev, "Out of memory");
1511 kfree(new_interfaces[n]);
1512 kfree(new_interfaces);
1517 i = dev->bus_mA - cp->desc.bMaxPower * 2;
1519 dev_warn(&dev->dev, "new config #%d exceeds power "
1524 /* Wake up the device so we can send it the Set-Config request */
1525 ret = usb_autoresume_device(dev);
1527 goto free_interfaces;
1529 /* if it's already configured, clear out old state first.
1530 * getting rid of old interfaces means unbinding their drivers.
1532 if (dev->state != USB_STATE_ADDRESS)
1533 usb_disable_device (dev, 1); // Skip ep0
1535 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1536 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1537 NULL, 0, USB_CTRL_SET_TIMEOUT)) < 0) {
1539 /* All the old state is gone, so what else can we do?
1540 * The device is probably useless now anyway.
1545 dev->actconfig = cp;
1547 usb_set_device_state(dev, USB_STATE_ADDRESS);
1548 usb_autosuspend_device(dev);
1549 goto free_interfaces;
1551 usb_set_device_state(dev, USB_STATE_CONFIGURED);
1553 /* Initialize the new interface structures and the
1554 * hc/hcd/usbcore interface/endpoint state.
1556 for (i = 0; i < nintf; ++i) {
1557 struct usb_interface_cache *intfc;
1558 struct usb_interface *intf;
1559 struct usb_host_interface *alt;
1561 cp->interface[i] = intf = new_interfaces[i];
1562 intfc = cp->intf_cache[i];
1563 intf->altsetting = intfc->altsetting;
1564 intf->num_altsetting = intfc->num_altsetting;
1565 intf->intf_assoc = find_iad(dev, cp, i);
1566 kref_get(&intfc->ref);
1568 alt = usb_altnum_to_altsetting(intf, 0);
1570 /* No altsetting 0? We'll assume the first altsetting.
1571 * We could use a GetInterface call, but if a device is
1572 * so non-compliant that it doesn't have altsetting 0
1573 * then I wouldn't trust its reply anyway.
1576 alt = &intf->altsetting[0];
1578 intf->cur_altsetting = alt;
1579 usb_enable_interface(dev, intf);
1580 intf->dev.parent = &dev->dev;
1581 intf->dev.driver = NULL;
1582 intf->dev.bus = &usb_bus_type;
1583 intf->dev.type = &usb_if_device_type;
1584 intf->dev.dma_mask = dev->dev.dma_mask;
1585 device_initialize (&intf->dev);
1586 mark_quiesced(intf);
1587 sprintf (&intf->dev.bus_id[0], "%d-%s:%d.%d",
1588 dev->bus->busnum, dev->devpath,
1589 configuration, alt->desc.bInterfaceNumber);
1591 kfree(new_interfaces);
1593 if (cp->string == NULL)
1594 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
1596 /* Now that all the interfaces are set up, register them
1597 * to trigger binding of drivers to interfaces. probe()
1598 * routines may install different altsettings and may
1599 * claim() any interfaces not yet bound. Many class drivers
1600 * need that: CDC, audio, video, etc.
1602 for (i = 0; i < nintf; ++i) {
1603 struct usb_interface *intf = cp->interface[i];
1606 "adding %s (config #%d, interface %d)\n",
1607 intf->dev.bus_id, configuration,
1608 intf->cur_altsetting->desc.bInterfaceNumber);
1609 ret = device_add (&intf->dev);
1611 dev_err(&dev->dev, "device_add(%s) --> %d\n",
1612 intf->dev.bus_id, ret);
1615 usb_create_sysfs_intf_files (intf);
1618 usb_autosuspend_device(dev);
1622 struct set_config_request {
1623 struct usb_device *udev;
1625 struct work_struct work;
1628 /* Worker routine for usb_driver_set_configuration() */
1629 static void driver_set_config_work(struct work_struct *work)
1631 struct set_config_request *req =
1632 container_of(work, struct set_config_request, work);
1634 usb_lock_device(req->udev);
1635 usb_set_configuration(req->udev, req->config);
1636 usb_unlock_device(req->udev);
1637 usb_put_dev(req->udev);
1642 * usb_driver_set_configuration - Provide a way for drivers to change device configurations
1643 * @udev: the device whose configuration is being updated
1644 * @config: the configuration being chosen.
1645 * Context: In process context, must be able to sleep
1647 * Device interface drivers are not allowed to change device configurations.
1648 * This is because changing configurations will destroy the interface the
1649 * driver is bound to and create new ones; it would be like a floppy-disk
1650 * driver telling the computer to replace the floppy-disk drive with a
1653 * Still, in certain specialized circumstances the need may arise. This
1654 * routine gets around the normal restrictions by using a work thread to
1655 * submit the change-config request.
1657 * Returns 0 if the request was succesfully queued, error code otherwise.
1658 * The caller has no way to know whether the queued request will eventually
1661 int usb_driver_set_configuration(struct usb_device *udev, int config)
1663 struct set_config_request *req;
1665 req = kmalloc(sizeof(*req), GFP_KERNEL);
1669 req->config = config;
1670 INIT_WORK(&req->work, driver_set_config_work);
1673 schedule_work(&req->work);
1676 EXPORT_SYMBOL_GPL(usb_driver_set_configuration);
1678 // synchronous request completion model
1679 EXPORT_SYMBOL(usb_control_msg);
1680 EXPORT_SYMBOL(usb_bulk_msg);
1682 EXPORT_SYMBOL(usb_sg_init);
1683 EXPORT_SYMBOL(usb_sg_cancel);
1684 EXPORT_SYMBOL(usb_sg_wait);
1686 // synchronous control message convenience routines
1687 EXPORT_SYMBOL(usb_get_descriptor);
1688 EXPORT_SYMBOL(usb_get_status);
1689 EXPORT_SYMBOL(usb_string);
1691 // synchronous calls that also maintain usbcore state
1692 EXPORT_SYMBOL(usb_clear_halt);
1693 EXPORT_SYMBOL(usb_reset_configuration);
1694 EXPORT_SYMBOL(usb_set_interface);