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 */
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
112 * timing out (if 0 the wait is forever)
113 * Context: !in_interrupt ()
115 * This function sends a simple control message to a specified endpoint
116 * and waits for the message to complete, or timeout.
118 * If successful, it returns the number of bytes transferred, otherwise a negative error number.
120 * Don't use this function from within an interrupt context, like a
121 * bottom half handler. If you need an asynchronous message, or need to send
122 * a message from within interrupt context, use usb_submit_urb()
123 * If a thread in your driver uses this call, make sure your disconnect()
124 * method can wait for it to complete. Since you don't have a handle on
125 * the URB used, you can't cancel the request.
127 int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype,
128 __u16 value, __u16 index, void *data, __u16 size, int timeout)
130 struct usb_ctrlrequest *dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
136 dr->bRequestType= requesttype;
137 dr->bRequest = request;
138 dr->wValue = cpu_to_le16p(&value);
139 dr->wIndex = cpu_to_le16p(&index);
140 dr->wLength = cpu_to_le16p(&size);
142 //dbg("usb_control_msg");
144 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
153 * usb_interrupt_msg - Builds an interrupt urb, sends it off and waits for completion
154 * @usb_dev: pointer to the usb device to send the message to
155 * @pipe: endpoint "pipe" to send the message to
156 * @data: pointer to the data to send
157 * @len: length in bytes of the data to send
158 * @actual_length: pointer to a location to put the actual length transferred in bytes
159 * @timeout: time in msecs to wait for the message to complete before
160 * timing out (if 0 the wait is forever)
161 * Context: !in_interrupt ()
163 * This function sends a simple interrupt message to a specified endpoint and
164 * waits for the message to complete, or timeout.
166 * If successful, it returns 0, otherwise a negative error number. The number
167 * of actual bytes transferred will be stored in the actual_length paramater.
169 * Don't use this function from within an interrupt context, like a bottom half
170 * handler. If you need an asynchronous message, or need to send a message
171 * from within interrupt context, use usb_submit_urb() If a thread in your
172 * driver uses this call, make sure your disconnect() method can wait for it to
173 * complete. Since you don't have a handle on the URB used, you can't cancel
176 int usb_interrupt_msg(struct usb_device *usb_dev, unsigned int pipe,
177 void *data, int len, int *actual_length, int timeout)
179 return usb_bulk_msg(usb_dev, pipe, data, len, actual_length, timeout);
181 EXPORT_SYMBOL_GPL(usb_interrupt_msg);
184 * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
185 * @usb_dev: pointer to the usb device to send the message to
186 * @pipe: endpoint "pipe" to send the message to
187 * @data: pointer to the data to send
188 * @len: length in bytes of the data to send
189 * @actual_length: pointer to a location to put the actual length transferred in bytes
190 * @timeout: time in msecs to wait for the message to complete before
191 * timing out (if 0 the wait is forever)
192 * Context: !in_interrupt ()
194 * This function sends a simple bulk message to a specified endpoint
195 * and waits for the message to complete, or timeout.
197 * If successful, it returns 0, otherwise a negative error number.
198 * The number of actual bytes transferred will be stored in the
199 * actual_length paramater.
201 * Don't use this function from within an interrupt context, like a
202 * bottom half handler. If you need an asynchronous message, or need to
203 * send a message from within interrupt context, use usb_submit_urb()
204 * If a thread in your driver uses this call, make sure your disconnect()
205 * method can wait for it to complete. Since you don't have a handle on
206 * the URB used, you can't cancel the request.
208 * Because there is no usb_interrupt_msg() and no USBDEVFS_INTERRUPT
209 * ioctl, users are forced to abuse this routine by using it to submit
210 * URBs for interrupt endpoints. We will take the liberty of creating
211 * an interrupt URB (with the default interval) if the target is an
212 * interrupt endpoint.
214 int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
215 void *data, int len, int *actual_length, int timeout)
218 struct usb_host_endpoint *ep;
220 ep = (usb_pipein(pipe) ? usb_dev->ep_in : usb_dev->ep_out)
221 [usb_pipeendpoint(pipe)];
225 urb = usb_alloc_urb(0, GFP_KERNEL);
229 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
230 USB_ENDPOINT_XFER_INT) {
231 pipe = (pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
232 usb_fill_int_urb(urb, usb_dev, pipe, data, len,
233 usb_api_blocking_completion, NULL,
236 usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
237 usb_api_blocking_completion, NULL);
239 return usb_start_wait_urb(urb, timeout, actual_length);
242 /*-------------------------------------------------------------------*/
244 static void sg_clean (struct usb_sg_request *io)
247 while (io->entries--)
248 usb_free_urb (io->urbs [io->entries]);
252 if (io->dev->dev.dma_mask != NULL)
253 usb_buffer_unmap_sg (io->dev, usb_pipein(io->pipe),
258 static void sg_complete (struct urb *urb)
260 struct usb_sg_request *io = urb->context;
261 int status = urb->status;
263 spin_lock (&io->lock);
265 /* In 2.5 we require hcds' endpoint queues not to progress after fault
266 * reports, until the completion callback (this!) returns. That lets
267 * device driver code (like this routine) unlink queued urbs first,
268 * if it needs to, since the HC won't work on them at all. So it's
269 * not possible for page N+1 to overwrite page N, and so on.
271 * That's only for "hard" faults; "soft" faults (unlinks) sometimes
272 * complete before the HCD can get requests away from hardware,
273 * though never during cleanup after a hard fault.
276 && (io->status != -ECONNRESET
277 || status != -ECONNRESET)
278 && urb->actual_length) {
279 dev_err (io->dev->bus->controller,
280 "dev %s ep%d%s scatterlist error %d/%d\n",
282 usb_endpoint_num(&urb->ep->desc),
283 usb_urb_dir_in(urb) ? "in" : "out",
288 if (io->status == 0 && status && status != -ECONNRESET) {
289 int i, found, retval;
293 /* the previous urbs, and this one, completed already.
294 * unlink pending urbs so they won't rx/tx bad data.
295 * careful: unlink can sometimes be synchronous...
297 spin_unlock (&io->lock);
298 for (i = 0, found = 0; i < io->entries; i++) {
299 if (!io->urbs [i] || !io->urbs [i]->dev)
302 retval = usb_unlink_urb (io->urbs [i]);
303 if (retval != -EINPROGRESS &&
306 dev_err (&io->dev->dev,
307 "%s, unlink --> %d\n",
308 __FUNCTION__, retval);
309 } else if (urb == io->urbs [i])
312 spin_lock (&io->lock);
316 /* on the last completion, signal usb_sg_wait() */
317 io->bytes += urb->actual_length;
320 complete (&io->complete);
322 spin_unlock (&io->lock);
327 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
328 * @io: request block being initialized. until usb_sg_wait() returns,
329 * treat this as a pointer to an opaque block of memory,
330 * @dev: the usb device that will send or receive the data
331 * @pipe: endpoint "pipe" used to transfer the data
332 * @period: polling rate for interrupt endpoints, in frames or
333 * (for high speed endpoints) microframes; ignored for bulk
334 * @sg: scatterlist entries
335 * @nents: how many entries in the scatterlist
336 * @length: how many bytes to send from the scatterlist, or zero to
337 * send every byte identified in the list.
338 * @mem_flags: SLAB_* flags affecting memory allocations in this call
340 * Returns zero for success, else a negative errno value. This initializes a
341 * scatter/gather request, allocating resources such as I/O mappings and urb
342 * memory (except maybe memory used by USB controller drivers).
344 * The request must be issued using usb_sg_wait(), which waits for the I/O to
345 * complete (or to be canceled) and then cleans up all resources allocated by
348 * The request may be canceled with usb_sg_cancel(), either before or after
349 * usb_sg_wait() is called.
352 struct usb_sg_request *io,
353 struct usb_device *dev,
356 struct scatterlist *sg,
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_IOMMU)
438 io->urbs[i]->transfer_buffer = NULL;
440 io->urbs[i]->transfer_buffer =
441 page_address(sg[i].page) + sg[i].offset;
444 /* hc may use _only_ transfer_buffer */
445 io->urbs [i]->transfer_buffer =
446 page_address (sg [i].page) + sg [i].offset;
451 len = min_t (unsigned, len, length);
456 io->urbs [i]->transfer_buffer_length = len;
458 io->urbs [--i]->transfer_flags &= ~URB_NO_INTERRUPT;
460 /* transaction state */
463 init_completion (&io->complete);
473 * usb_sg_wait - synchronously execute scatter/gather request
474 * @io: request block handle, as initialized with usb_sg_init().
475 * some fields become accessible when this call returns.
476 * Context: !in_interrupt ()
478 * This function blocks until the specified I/O operation completes. It
479 * leverages the grouping of the related I/O requests to get good transfer
480 * rates, by queueing the requests. At higher speeds, such queuing can
481 * significantly improve USB throughput.
483 * There are three kinds of completion for this function.
484 * (1) success, where io->status is zero. The number of io->bytes
485 * transferred is as requested.
486 * (2) error, where io->status is a negative errno value. The number
487 * of io->bytes transferred before the error is usually less
488 * than requested, and can be nonzero.
489 * (3) cancellation, a type of error with status -ECONNRESET that
490 * is initiated by usb_sg_cancel().
492 * When this function returns, all memory allocated through usb_sg_init() or
493 * this call will have been freed. The request block parameter may still be
494 * passed to usb_sg_cancel(), or it may be freed. It could also be
495 * reinitialized and then reused.
497 * Data Transfer Rates:
499 * Bulk transfers are valid for full or high speed endpoints.
500 * The best full speed data rate is 19 packets of 64 bytes each
501 * per frame, or 1216 bytes per millisecond.
502 * The best high speed data rate is 13 packets of 512 bytes each
503 * per microframe, or 52 KBytes per millisecond.
505 * The reason to use interrupt transfers through this API would most likely
506 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
507 * could be transferred. That capability is less useful for low or full
508 * speed interrupt endpoints, which allow at most one packet per millisecond,
509 * of at most 8 or 64 bytes (respectively).
511 void usb_sg_wait (struct usb_sg_request *io)
513 int i, entries = io->entries;
515 /* queue the urbs. */
516 spin_lock_irq (&io->lock);
518 while (i < entries && !io->status) {
521 io->urbs [i]->dev = io->dev;
522 retval = usb_submit_urb (io->urbs [i], GFP_ATOMIC);
524 /* after we submit, let completions or cancelations fire;
525 * we handshake using io->status.
527 spin_unlock_irq (&io->lock);
529 /* maybe we retrying will recover */
530 case -ENXIO: // hc didn't queue this one
533 io->urbs[i]->dev = NULL;
538 /* no error? continue immediately.
540 * NOTE: to work better with UHCI (4K I/O buffer may
541 * need 3K of TDs) it may be good to limit how many
542 * URBs are queued at once; N milliseconds?
549 /* fail any uncompleted urbs */
551 io->urbs [i]->dev = NULL;
552 io->urbs [i]->status = retval;
553 dev_dbg (&io->dev->dev, "%s, submit --> %d\n",
554 __FUNCTION__, retval);
557 spin_lock_irq (&io->lock);
558 if (retval && (io->status == 0 || io->status == -ECONNRESET))
561 io->count -= entries - i;
563 complete (&io->complete);
564 spin_unlock_irq (&io->lock);
566 /* OK, yes, this could be packaged as non-blocking.
567 * So could the submit loop above ... but it's easier to
568 * solve neither problem than to solve both!
570 wait_for_completion (&io->complete);
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",
603 __FUNCTION__, retval);
605 spin_lock (&io->lock);
607 spin_unlock_irqrestore (&io->lock, flags);
610 /*-------------------------------------------------------------------*/
613 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
614 * @dev: the device whose descriptor is being retrieved
615 * @type: the descriptor type (USB_DT_*)
616 * @index: the number of the descriptor
617 * @buf: where to put the descriptor
618 * @size: how big is "buf"?
619 * Context: !in_interrupt ()
621 * Gets a USB descriptor. Convenience functions exist to simplify
622 * getting some types of descriptors. Use
623 * usb_get_string() or usb_string() for USB_DT_STRING.
624 * Device (USB_DT_DEVICE) and configuration descriptors (USB_DT_CONFIG)
625 * are part of the device structure.
626 * In addition to a number of USB-standard descriptors, some
627 * devices also use class-specific or vendor-specific descriptors.
629 * This call is synchronous, and may not be used in an interrupt context.
631 * Returns the number of bytes received on success, or else the status code
632 * returned by the underlying usb_control_msg() call.
634 int usb_get_descriptor(struct usb_device *dev, unsigned char type, unsigned char index, void *buf, int size)
639 memset(buf,0,size); // Make sure we parse really received data
641 for (i = 0; i < 3; ++i) {
642 /* retry on length 0 or error; some devices are flakey */
643 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
644 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
645 (type << 8) + index, 0, buf, size,
646 USB_CTRL_GET_TIMEOUT);
647 if (result <= 0 && result != -ETIMEDOUT)
649 if (result > 1 && ((u8 *)buf)[1] != type) {
659 * usb_get_string - gets a string descriptor
660 * @dev: the device whose string descriptor is being retrieved
661 * @langid: code for language chosen (from string descriptor zero)
662 * @index: the number of the descriptor
663 * @buf: where to put the string
664 * @size: how big is "buf"?
665 * Context: !in_interrupt ()
667 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
668 * in little-endian byte order).
669 * The usb_string() function will often be a convenient way to turn
670 * these strings into kernel-printable form.
672 * Strings may be referenced in device, configuration, interface, or other
673 * descriptors, and could also be used in vendor-specific ways.
675 * This call is synchronous, and may not be used in an interrupt context.
677 * Returns the number of bytes received on success, or else the status code
678 * returned by the underlying usb_control_msg() call.
680 static int usb_get_string(struct usb_device *dev, unsigned short langid,
681 unsigned char index, void *buf, int size)
686 for (i = 0; i < 3; ++i) {
687 /* retry on length 0 or stall; some devices are flakey */
688 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
689 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
690 (USB_DT_STRING << 8) + index, langid, buf, size,
691 USB_CTRL_GET_TIMEOUT);
692 if (!(result == 0 || result == -EPIPE))
698 static void usb_try_string_workarounds(unsigned char *buf, int *length)
700 int newlength, oldlength = *length;
702 for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
703 if (!isprint(buf[newlength]) || buf[newlength + 1])
712 static int usb_string_sub(struct usb_device *dev, unsigned int langid,
713 unsigned int index, unsigned char *buf)
717 /* Try to read the string descriptor by asking for the maximum
718 * possible number of bytes */
719 if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
722 rc = usb_get_string(dev, langid, index, buf, 255);
724 /* If that failed try to read the descriptor length, then
725 * ask for just that many bytes */
727 rc = usb_get_string(dev, langid, index, buf, 2);
729 rc = usb_get_string(dev, langid, index, buf, buf[0]);
733 if (!buf[0] && !buf[1])
734 usb_try_string_workarounds(buf, &rc);
736 /* There might be extra junk at the end of the descriptor */
740 rc = rc - (rc & 1); /* force a multiple of two */
744 rc = (rc < 0 ? rc : -EINVAL);
750 * usb_string - returns ISO 8859-1 version of a string descriptor
751 * @dev: the device whose string descriptor is being retrieved
752 * @index: the number of the descriptor
753 * @buf: where to put the string
754 * @size: how big is "buf"?
755 * Context: !in_interrupt ()
757 * This converts the UTF-16LE encoded strings returned by devices, from
758 * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones
759 * that are more usable in most kernel contexts. Note that all characters
760 * in the chosen descriptor that can't be encoded using ISO-8859-1
761 * are converted to the question mark ("?") character, and this function
762 * chooses strings in the first language supported by the device.
764 * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit
765 * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode,
766 * and is appropriate for use many uses of English and several other
767 * Western European languages. (But it doesn't include the "Euro" symbol.)
769 * This call is synchronous, and may not be used in an interrupt context.
771 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
773 int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
779 if (dev->state == USB_STATE_SUSPENDED)
780 return -EHOSTUNREACH;
781 if (size <= 0 || !buf || !index)
784 tbuf = kmalloc(256, GFP_KERNEL);
788 /* get langid for strings if it's not yet known */
789 if (!dev->have_langid) {
790 err = usb_string_sub(dev, 0, 0, tbuf);
793 "string descriptor 0 read error: %d\n",
796 } else if (err < 4) {
797 dev_err (&dev->dev, "string descriptor 0 too short\n");
801 dev->have_langid = 1;
802 dev->string_langid = tbuf[2] | (tbuf[3]<< 8);
803 /* always use the first langid listed */
804 dev_dbg (&dev->dev, "default language 0x%04x\n",
809 err = usb_string_sub(dev, dev->string_langid, index, tbuf);
813 size--; /* leave room for trailing NULL char in output buffer */
814 for (idx = 0, u = 2; u < err; u += 2) {
817 if (tbuf[u+1]) /* high byte */
818 buf[idx++] = '?'; /* non ISO-8859-1 character */
820 buf[idx++] = tbuf[u];
825 if (tbuf[1] != USB_DT_STRING)
826 dev_dbg(&dev->dev, "wrong descriptor type %02x for string %d (\"%s\")\n", tbuf[1], index, buf);
834 * usb_cache_string - read a string descriptor and cache it for later use
835 * @udev: the device whose string descriptor is being read
836 * @index: the descriptor index
838 * Returns a pointer to a kmalloc'ed buffer containing the descriptor string,
839 * or NULL if the index is 0 or the string could not be read.
841 char *usb_cache_string(struct usb_device *udev, int index)
844 char *smallbuf = NULL;
847 if (index > 0 && (buf = kmalloc(256, GFP_KERNEL)) != NULL) {
848 if ((len = usb_string(udev, index, buf, 256)) > 0) {
849 if ((smallbuf = kmalloc(++len, GFP_KERNEL)) == NULL)
851 memcpy(smallbuf, buf, len);
859 * usb_get_device_descriptor - (re)reads the device descriptor (usbcore)
860 * @dev: the device whose device descriptor is being updated
861 * @size: how much of the descriptor to read
862 * Context: !in_interrupt ()
864 * Updates the copy of the device descriptor stored in the device structure,
865 * which dedicates space for this purpose.
867 * Not exported, only for use by the core. If drivers really want to read
868 * the device descriptor directly, they can call usb_get_descriptor() with
869 * type = USB_DT_DEVICE and index = 0.
871 * This call is synchronous, and may not be used in an interrupt context.
873 * Returns the number of bytes received on success, or else the status code
874 * returned by the underlying usb_control_msg() call.
876 int usb_get_device_descriptor(struct usb_device *dev, unsigned int size)
878 struct usb_device_descriptor *desc;
881 if (size > sizeof(*desc))
883 desc = kmalloc(sizeof(*desc), GFP_NOIO);
887 ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, desc, size);
889 memcpy(&dev->descriptor, desc, size);
895 * usb_get_status - issues a GET_STATUS call
896 * @dev: the device whose status is being checked
897 * @type: USB_RECIP_*; for device, interface, or endpoint
898 * @target: zero (for device), else interface or endpoint number
899 * @data: pointer to two bytes of bitmap data
900 * Context: !in_interrupt ()
902 * Returns device, interface, or endpoint status. Normally only of
903 * interest to see if the device is self powered, or has enabled the
904 * remote wakeup facility; or whether a bulk or interrupt endpoint
905 * is halted ("stalled").
907 * Bits in these status bitmaps are set using the SET_FEATURE request,
908 * and cleared using the CLEAR_FEATURE request. The usb_clear_halt()
909 * function should be used to clear halt ("stall") status.
911 * This call is synchronous, and may not be used in an interrupt context.
913 * Returns the number of bytes received on success, or else the status code
914 * returned by the underlying usb_control_msg() call.
916 int usb_get_status(struct usb_device *dev, int type, int target, void *data)
919 u16 *status = kmalloc(sizeof(*status), GFP_KERNEL);
924 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
925 USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, status,
926 sizeof(*status), USB_CTRL_GET_TIMEOUT);
928 *(u16 *)data = *status;
934 * usb_clear_halt - tells device to clear endpoint halt/stall condition
935 * @dev: device whose endpoint is halted
936 * @pipe: endpoint "pipe" being cleared
937 * Context: !in_interrupt ()
939 * This is used to clear halt conditions for bulk and interrupt endpoints,
940 * as reported by URB completion status. Endpoints that are halted are
941 * sometimes referred to as being "stalled". Such endpoints are unable
942 * to transmit or receive data until the halt status is cleared. Any URBs
943 * queued for such an endpoint should normally be unlinked by the driver
944 * before clearing the halt condition, as described in sections 5.7.5
945 * and 5.8.5 of the USB 2.0 spec.
947 * Note that control and isochronous endpoints don't halt, although control
948 * endpoints report "protocol stall" (for unsupported requests) using the
949 * same status code used to report a true stall.
951 * This call is synchronous, and may not be used in an interrupt context.
953 * Returns zero on success, or else the status code returned by the
954 * underlying usb_control_msg() call.
956 int usb_clear_halt(struct usb_device *dev, int pipe)
959 int endp = usb_pipeendpoint(pipe);
961 if (usb_pipein (pipe))
964 /* we don't care if it wasn't halted first. in fact some devices
965 * (like some ibmcam model 1 units) seem to expect hosts to make
966 * this request for iso endpoints, which can't halt!
968 result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
969 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
970 USB_ENDPOINT_HALT, endp, NULL, 0,
971 USB_CTRL_SET_TIMEOUT);
973 /* don't un-halt or force to DATA0 except on success */
977 /* NOTE: seems like Microsoft and Apple don't bother verifying
978 * the clear "took", so some devices could lock up if you check...
979 * such as the Hagiwara FlashGate DUAL. So we won't bother.
981 * NOTE: make sure the logic here doesn't diverge much from
982 * the copy in usb-storage, for as long as we need two copies.
985 /* toggle was reset by the clear */
986 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
992 * usb_disable_endpoint -- Disable an endpoint by address
993 * @dev: the device whose endpoint is being disabled
994 * @epaddr: the endpoint's address. Endpoint number for output,
995 * endpoint number + USB_DIR_IN for input
997 * Deallocates hcd/hardware state for this endpoint ... and nukes all
1000 * If the HCD hasn't registered a disable() function, this sets the
1001 * endpoint's maxpacket size to 0 to prevent further submissions.
1003 void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr)
1005 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
1006 struct usb_host_endpoint *ep;
1011 if (usb_endpoint_out(epaddr)) {
1012 ep = dev->ep_out[epnum];
1013 dev->ep_out[epnum] = NULL;
1015 ep = dev->ep_in[epnum];
1016 dev->ep_in[epnum] = NULL;
1020 usb_hcd_flush_endpoint(dev, ep);
1021 usb_hcd_disable_endpoint(dev, ep);
1026 * usb_disable_interface -- Disable all endpoints for an interface
1027 * @dev: the device whose interface is being disabled
1028 * @intf: pointer to the interface descriptor
1030 * Disables all the endpoints for the interface's current altsetting.
1032 void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf)
1034 struct usb_host_interface *alt = intf->cur_altsetting;
1037 for (i = 0; i < alt->desc.bNumEndpoints; ++i) {
1038 usb_disable_endpoint(dev,
1039 alt->endpoint[i].desc.bEndpointAddress);
1044 * usb_disable_device - Disable all the endpoints for a USB device
1045 * @dev: the device whose endpoints are being disabled
1046 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
1048 * Disables all the device's endpoints, potentially including endpoint 0.
1049 * Deallocates hcd/hardware state for the endpoints (nuking all or most
1050 * pending urbs) and usbcore state for the interfaces, so that usbcore
1051 * must usb_set_configuration() before any interfaces could be used.
1053 void usb_disable_device(struct usb_device *dev, int skip_ep0)
1057 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __FUNCTION__,
1058 skip_ep0 ? "non-ep0" : "all");
1059 for (i = skip_ep0; i < 16; ++i) {
1060 usb_disable_endpoint(dev, i);
1061 usb_disable_endpoint(dev, i + USB_DIR_IN);
1063 dev->toggle[0] = dev->toggle[1] = 0;
1065 /* getting rid of interfaces will disconnect
1066 * any drivers bound to them (a key side effect)
1068 if (dev->actconfig) {
1069 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1070 struct usb_interface *interface;
1072 /* remove this interface if it has been registered */
1073 interface = dev->actconfig->interface[i];
1074 if (!device_is_registered(&interface->dev))
1076 dev_dbg (&dev->dev, "unregistering interface %s\n",
1077 interface->dev.bus_id);
1078 usb_remove_sysfs_intf_files(interface);
1079 device_del (&interface->dev);
1082 /* Now that the interfaces are unbound, nobody should
1083 * try to access them.
1085 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1086 put_device (&dev->actconfig->interface[i]->dev);
1087 dev->actconfig->interface[i] = NULL;
1089 dev->actconfig = NULL;
1090 if (dev->state == USB_STATE_CONFIGURED)
1091 usb_set_device_state(dev, USB_STATE_ADDRESS);
1097 * usb_enable_endpoint - Enable an endpoint for USB communications
1098 * @dev: the device whose interface is being enabled
1101 * Resets the endpoint toggle, and sets dev->ep_{in,out} pointers.
1102 * For control endpoints, both the input and output sides are handled.
1104 void usb_enable_endpoint(struct usb_device *dev, struct usb_host_endpoint *ep)
1106 int epnum = usb_endpoint_num(&ep->desc);
1107 int is_out = usb_endpoint_dir_out(&ep->desc);
1108 int is_control = usb_endpoint_xfer_control(&ep->desc);
1110 if (is_out || is_control) {
1111 usb_settoggle(dev, epnum, 1, 0);
1112 dev->ep_out[epnum] = ep;
1114 if (!is_out || is_control) {
1115 usb_settoggle(dev, epnum, 0, 0);
1116 dev->ep_in[epnum] = ep;
1122 * usb_enable_interface - Enable all the endpoints for an interface
1123 * @dev: the device whose interface is being enabled
1124 * @intf: pointer to the interface descriptor
1126 * Enables all the endpoints for the interface's current altsetting.
1128 static void usb_enable_interface(struct usb_device *dev,
1129 struct usb_interface *intf)
1131 struct usb_host_interface *alt = intf->cur_altsetting;
1134 for (i = 0; i < alt->desc.bNumEndpoints; ++i)
1135 usb_enable_endpoint(dev, &alt->endpoint[i]);
1139 * usb_set_interface - Makes a particular alternate setting be current
1140 * @dev: the device whose interface is being updated
1141 * @interface: the interface being updated
1142 * @alternate: the setting being chosen.
1143 * Context: !in_interrupt ()
1145 * This is used to enable data transfers on interfaces that may not
1146 * be enabled by default. Not all devices support such configurability.
1147 * Only the driver bound to an interface may change its setting.
1149 * Within any given configuration, each interface may have several
1150 * alternative settings. These are often used to control levels of
1151 * bandwidth consumption. For example, the default setting for a high
1152 * speed interrupt endpoint may not send more than 64 bytes per microframe,
1153 * while interrupt transfers of up to 3KBytes per microframe are legal.
1154 * Also, isochronous endpoints may never be part of an
1155 * interface's default setting. To access such bandwidth, alternate
1156 * interface settings must be made current.
1158 * Note that in the Linux USB subsystem, bandwidth associated with
1159 * an endpoint in a given alternate setting is not reserved until an URB
1160 * is submitted that needs that bandwidth. Some other operating systems
1161 * allocate bandwidth early, when a configuration is chosen.
1163 * This call is synchronous, and may not be used in an interrupt context.
1164 * Also, drivers must not change altsettings while urbs are scheduled for
1165 * endpoints in that interface; all such urbs must first be completed
1166 * (perhaps forced by unlinking).
1168 * Returns zero on success, or else the status code returned by the
1169 * underlying usb_control_msg() call.
1171 int usb_set_interface(struct usb_device *dev, int interface, int alternate)
1173 struct usb_interface *iface;
1174 struct usb_host_interface *alt;
1179 if (dev->state == USB_STATE_SUSPENDED)
1180 return -EHOSTUNREACH;
1182 iface = usb_ifnum_to_if(dev, interface);
1184 dev_dbg(&dev->dev, "selecting invalid interface %d\n",
1189 alt = usb_altnum_to_altsetting(iface, alternate);
1191 warn("selecting invalid altsetting %d", alternate);
1195 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1196 USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
1197 alternate, interface, NULL, 0, 5000);
1199 /* 9.4.10 says devices don't need this and are free to STALL the
1200 * request if the interface only has one alternate setting.
1202 if (ret == -EPIPE && iface->num_altsetting == 1) {
1204 "manual set_interface for iface %d, alt %d\n",
1205 interface, alternate);
1210 /* FIXME drivers shouldn't need to replicate/bugfix the logic here
1211 * when they implement async or easily-killable versions of this or
1212 * other "should-be-internal" functions (like clear_halt).
1213 * should hcd+usbcore postprocess control requests?
1216 /* prevent submissions using previous endpoint settings */
1217 changed = (iface->cur_altsetting != alt);
1218 if (changed && device_is_registered(&iface->dev))
1219 usb_remove_sysfs_intf_files(iface);
1220 usb_disable_interface(dev, iface);
1222 iface->cur_altsetting = alt;
1224 /* If the interface only has one altsetting and the device didn't
1225 * accept the request, we attempt to carry out the equivalent action
1226 * by manually clearing the HALT feature for each endpoint in the
1232 for (i = 0; i < alt->desc.bNumEndpoints; i++) {
1233 unsigned int epaddr =
1234 alt->endpoint[i].desc.bEndpointAddress;
1236 __create_pipe(dev, USB_ENDPOINT_NUMBER_MASK & epaddr)
1237 | (usb_endpoint_out(epaddr) ? USB_DIR_OUT : USB_DIR_IN);
1239 usb_clear_halt(dev, pipe);
1243 /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
1246 * Despite EP0 is always present in all interfaces/AS, the list of
1247 * endpoints from the descriptor does not contain EP0. Due to its
1248 * omnipresence one might expect EP0 being considered "affected" by
1249 * any SetInterface request and hence assume toggles need to be reset.
1250 * However, EP0 toggles are re-synced for every individual transfer
1251 * during the SETUP stage - hence EP0 toggles are "don't care" here.
1252 * (Likewise, EP0 never "halts" on well designed devices.)
1254 usb_enable_interface(dev, iface);
1255 if (changed && device_is_registered(&iface->dev))
1256 usb_create_sysfs_intf_files(iface);
1262 * usb_reset_configuration - lightweight device reset
1263 * @dev: the device whose configuration is being reset
1265 * This issues a standard SET_CONFIGURATION request to the device using
1266 * the current configuration. The effect is to reset most USB-related
1267 * state in the device, including interface altsettings (reset to zero),
1268 * endpoint halts (cleared), and data toggle (only for bulk and interrupt
1269 * endpoints). Other usbcore state is unchanged, including bindings of
1270 * usb device drivers to interfaces.
1272 * Because this affects multiple interfaces, avoid using this with composite
1273 * (multi-interface) devices. Instead, the driver for each interface may
1274 * use usb_set_interface() on the interfaces it claims. Be careful though;
1275 * some devices don't support the SET_INTERFACE request, and others won't
1276 * reset all the interface state (notably data toggles). Resetting the whole
1277 * configuration would affect other drivers' interfaces.
1279 * The caller must own the device lock.
1281 * Returns zero on success, else a negative error code.
1283 int usb_reset_configuration(struct usb_device *dev)
1286 struct usb_host_config *config;
1288 if (dev->state == USB_STATE_SUSPENDED)
1289 return -EHOSTUNREACH;
1291 /* caller must have locked the device and must own
1292 * the usb bus readlock (so driver bindings are stable);
1293 * calls during probe() are fine
1296 for (i = 1; i < 16; ++i) {
1297 usb_disable_endpoint(dev, i);
1298 usb_disable_endpoint(dev, i + USB_DIR_IN);
1301 config = dev->actconfig;
1302 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1303 USB_REQ_SET_CONFIGURATION, 0,
1304 config->desc.bConfigurationValue, 0,
1305 NULL, 0, USB_CTRL_SET_TIMEOUT);
1309 dev->toggle[0] = dev->toggle[1] = 0;
1311 /* re-init hc/hcd interface/endpoint state */
1312 for (i = 0; i < config->desc.bNumInterfaces; i++) {
1313 struct usb_interface *intf = config->interface[i];
1314 struct usb_host_interface *alt;
1316 if (device_is_registered(&intf->dev))
1317 usb_remove_sysfs_intf_files(intf);
1318 alt = usb_altnum_to_altsetting(intf, 0);
1320 /* No altsetting 0? We'll assume the first altsetting.
1321 * We could use a GetInterface call, but if a device is
1322 * so non-compliant that it doesn't have altsetting 0
1323 * then I wouldn't trust its reply anyway.
1326 alt = &intf->altsetting[0];
1328 intf->cur_altsetting = alt;
1329 usb_enable_interface(dev, intf);
1330 if (device_is_registered(&intf->dev))
1331 usb_create_sysfs_intf_files(intf);
1336 static void usb_release_interface(struct device *dev)
1338 struct usb_interface *intf = to_usb_interface(dev);
1339 struct usb_interface_cache *intfc =
1340 altsetting_to_usb_interface_cache(intf->altsetting);
1342 kref_put(&intfc->ref, usb_release_interface_cache);
1346 #ifdef CONFIG_HOTPLUG
1347 static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
1349 struct usb_device *usb_dev;
1350 struct usb_interface *intf;
1351 struct usb_host_interface *alt;
1356 /* driver is often null here; dev_dbg() would oops */
1357 pr_debug ("usb %s: uevent\n", dev->bus_id);
1359 intf = to_usb_interface(dev);
1360 usb_dev = interface_to_usbdev(intf);
1361 alt = intf->cur_altsetting;
1363 #ifdef CONFIG_USB_DEVICEFS
1364 if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d",
1365 usb_dev->bus->busnum, usb_dev->devnum))
1369 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
1370 le16_to_cpu(usb_dev->descriptor.idVendor),
1371 le16_to_cpu(usb_dev->descriptor.idProduct),
1372 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
1375 if (add_uevent_var(env, "TYPE=%d/%d/%d",
1376 usb_dev->descriptor.bDeviceClass,
1377 usb_dev->descriptor.bDeviceSubClass,
1378 usb_dev->descriptor.bDeviceProtocol))
1381 if (add_uevent_var(env, "INTERFACE=%d/%d/%d",
1382 alt->desc.bInterfaceClass,
1383 alt->desc.bInterfaceSubClass,
1384 alt->desc.bInterfaceProtocol))
1387 if (add_uevent_var(env,
1388 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
1389 le16_to_cpu(usb_dev->descriptor.idVendor),
1390 le16_to_cpu(usb_dev->descriptor.idProduct),
1391 le16_to_cpu(usb_dev->descriptor.bcdDevice),
1392 usb_dev->descriptor.bDeviceClass,
1393 usb_dev->descriptor.bDeviceSubClass,
1394 usb_dev->descriptor.bDeviceProtocol,
1395 alt->desc.bInterfaceClass,
1396 alt->desc.bInterfaceSubClass,
1397 alt->desc.bInterfaceProtocol))
1405 static int usb_if_uevent(struct device *dev, struct kobj_uevent_env *env)
1409 #endif /* CONFIG_HOTPLUG */
1411 struct device_type usb_if_device_type = {
1412 .name = "usb_interface",
1413 .release = usb_release_interface,
1414 .uevent = usb_if_uevent,
1417 static struct usb_interface_assoc_descriptor *find_iad(struct usb_device *dev,
1418 struct usb_host_config *config,
1421 struct usb_interface_assoc_descriptor *retval = NULL;
1422 struct usb_interface_assoc_descriptor *intf_assoc;
1427 for (i = 0; (i < USB_MAXIADS && config->intf_assoc[i]); i++) {
1428 intf_assoc = config->intf_assoc[i];
1429 if (intf_assoc->bInterfaceCount == 0)
1432 first_intf = intf_assoc->bFirstInterface;
1433 last_intf = first_intf + (intf_assoc->bInterfaceCount - 1);
1434 if (inum >= first_intf && inum <= last_intf) {
1436 retval = intf_assoc;
1438 dev_err(&dev->dev, "Interface #%d referenced"
1439 " by multiple IADs\n", inum);
1448 * usb_set_configuration - Makes a particular device setting be current
1449 * @dev: the device whose configuration is being updated
1450 * @configuration: the configuration being chosen.
1451 * Context: !in_interrupt(), caller owns the device lock
1453 * This is used to enable non-default device modes. Not all devices
1454 * use this kind of configurability; many devices only have one
1457 * @configuration is the value of the configuration to be installed.
1458 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1459 * must be non-zero; a value of zero indicates that the device in
1460 * unconfigured. However some devices erroneously use 0 as one of their
1461 * configuration values. To help manage such devices, this routine will
1462 * accept @configuration = -1 as indicating the device should be put in
1463 * an unconfigured state.
1465 * USB device configurations may affect Linux interoperability,
1466 * power consumption and the functionality available. For example,
1467 * the default configuration is limited to using 100mA of bus power,
1468 * so that when certain device functionality requires more power,
1469 * and the device is bus powered, that functionality should be in some
1470 * non-default device configuration. Other device modes may also be
1471 * reflected as configuration options, such as whether two ISDN
1472 * channels are available independently; and choosing between open
1473 * standard device protocols (like CDC) or proprietary ones.
1475 * Note that a non-authorized device (dev->authorized == 0) will only
1476 * be put in unconfigured mode.
1478 * Note that USB has an additional level of device configurability,
1479 * associated with interfaces. That configurability is accessed using
1480 * usb_set_interface().
1482 * This call is synchronous. The calling context must be able to sleep,
1483 * must own the device lock, and must not hold the driver model's USB
1484 * bus mutex; usb device driver probe() methods cannot use this routine.
1486 * Returns zero on success, or else the status code returned by the
1487 * underlying call that failed. On successful completion, each interface
1488 * in the original device configuration has been destroyed, and each one
1489 * in the new configuration has been probed by all relevant usb device
1490 * drivers currently known to the kernel.
1492 int usb_set_configuration(struct usb_device *dev, int configuration)
1495 struct usb_host_config *cp = NULL;
1496 struct usb_interface **new_interfaces = NULL;
1499 if (dev->authorized == 0 || configuration == -1)
1502 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1503 if (dev->config[i].desc.bConfigurationValue ==
1505 cp = &dev->config[i];
1510 if ((!cp && configuration != 0))
1513 /* The USB spec says configuration 0 means unconfigured.
1514 * But if a device includes a configuration numbered 0,
1515 * we will accept it as a correctly configured state.
1516 * Use -1 if you really want to unconfigure the device.
1518 if (cp && configuration == 0)
1519 dev_warn(&dev->dev, "config 0 descriptor??\n");
1521 /* Allocate memory for new interfaces before doing anything else,
1522 * so that if we run out then nothing will have changed. */
1525 nintf = cp->desc.bNumInterfaces;
1526 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
1528 if (!new_interfaces) {
1529 dev_err(&dev->dev, "Out of memory\n");
1533 for (; n < nintf; ++n) {
1534 new_interfaces[n] = kzalloc(
1535 sizeof(struct usb_interface),
1537 if (!new_interfaces[n]) {
1538 dev_err(&dev->dev, "Out of memory\n");
1542 kfree(new_interfaces[n]);
1543 kfree(new_interfaces);
1548 i = dev->bus_mA - cp->desc.bMaxPower * 2;
1550 dev_warn(&dev->dev, "new config #%d exceeds power "
1555 /* Wake up the device so we can send it the Set-Config request */
1556 ret = usb_autoresume_device(dev);
1558 goto free_interfaces;
1560 /* if it's already configured, clear out old state first.
1561 * getting rid of old interfaces means unbinding their drivers.
1563 if (dev->state != USB_STATE_ADDRESS)
1564 usb_disable_device (dev, 1); // Skip ep0
1566 if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1567 USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1568 NULL, 0, USB_CTRL_SET_TIMEOUT)) < 0) {
1570 /* All the old state is gone, so what else can we do?
1571 * The device is probably useless now anyway.
1576 dev->actconfig = cp;
1578 usb_set_device_state(dev, USB_STATE_ADDRESS);
1579 usb_autosuspend_device(dev);
1580 goto free_interfaces;
1582 usb_set_device_state(dev, USB_STATE_CONFIGURED);
1584 /* Initialize the new interface structures and the
1585 * hc/hcd/usbcore interface/endpoint state.
1587 for (i = 0; i < nintf; ++i) {
1588 struct usb_interface_cache *intfc;
1589 struct usb_interface *intf;
1590 struct usb_host_interface *alt;
1592 cp->interface[i] = intf = new_interfaces[i];
1593 intfc = cp->intf_cache[i];
1594 intf->altsetting = intfc->altsetting;
1595 intf->num_altsetting = intfc->num_altsetting;
1596 intf->intf_assoc = find_iad(dev, cp, i);
1597 kref_get(&intfc->ref);
1599 alt = usb_altnum_to_altsetting(intf, 0);
1601 /* No altsetting 0? We'll assume the first altsetting.
1602 * We could use a GetInterface call, but if a device is
1603 * so non-compliant that it doesn't have altsetting 0
1604 * then I wouldn't trust its reply anyway.
1607 alt = &intf->altsetting[0];
1609 intf->cur_altsetting = alt;
1610 usb_enable_interface(dev, intf);
1611 intf->dev.parent = &dev->dev;
1612 intf->dev.driver = NULL;
1613 intf->dev.bus = &usb_bus_type;
1614 intf->dev.type = &usb_if_device_type;
1615 intf->dev.dma_mask = dev->dev.dma_mask;
1616 device_initialize (&intf->dev);
1617 mark_quiesced(intf);
1618 sprintf (&intf->dev.bus_id[0], "%d-%s:%d.%d",
1619 dev->bus->busnum, dev->devpath,
1620 configuration, alt->desc.bInterfaceNumber);
1622 kfree(new_interfaces);
1624 if (cp->string == NULL)
1625 cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
1627 /* Now that all the interfaces are set up, register them
1628 * to trigger binding of drivers to interfaces. probe()
1629 * routines may install different altsettings and may
1630 * claim() any interfaces not yet bound. Many class drivers
1631 * need that: CDC, audio, video, etc.
1633 for (i = 0; i < nintf; ++i) {
1634 struct usb_interface *intf = cp->interface[i];
1637 "adding %s (config #%d, interface %d)\n",
1638 intf->dev.bus_id, configuration,
1639 intf->cur_altsetting->desc.bInterfaceNumber);
1640 ret = device_add (&intf->dev);
1642 dev_err(&dev->dev, "device_add(%s) --> %d\n",
1643 intf->dev.bus_id, ret);
1646 usb_create_sysfs_intf_files (intf);
1649 usb_autosuspend_device(dev);
1653 struct set_config_request {
1654 struct usb_device *udev;
1656 struct work_struct work;
1659 /* Worker routine for usb_driver_set_configuration() */
1660 static void driver_set_config_work(struct work_struct *work)
1662 struct set_config_request *req =
1663 container_of(work, struct set_config_request, work);
1665 usb_lock_device(req->udev);
1666 usb_set_configuration(req->udev, req->config);
1667 usb_unlock_device(req->udev);
1668 usb_put_dev(req->udev);
1673 * usb_driver_set_configuration - Provide a way for drivers to change device configurations
1674 * @udev: the device whose configuration is being updated
1675 * @config: the configuration being chosen.
1676 * Context: In process context, must be able to sleep
1678 * Device interface drivers are not allowed to change device configurations.
1679 * This is because changing configurations will destroy the interface the
1680 * driver is bound to and create new ones; it would be like a floppy-disk
1681 * driver telling the computer to replace the floppy-disk drive with a
1684 * Still, in certain specialized circumstances the need may arise. This
1685 * routine gets around the normal restrictions by using a work thread to
1686 * submit the change-config request.
1688 * Returns 0 if the request was succesfully queued, error code otherwise.
1689 * The caller has no way to know whether the queued request will eventually
1692 int usb_driver_set_configuration(struct usb_device *udev, int config)
1694 struct set_config_request *req;
1696 req = kmalloc(sizeof(*req), GFP_KERNEL);
1700 req->config = config;
1701 INIT_WORK(&req->work, driver_set_config_work);
1704 schedule_work(&req->work);
1707 EXPORT_SYMBOL_GPL(usb_driver_set_configuration);
1709 // synchronous request completion model
1710 EXPORT_SYMBOL(usb_control_msg);
1711 EXPORT_SYMBOL(usb_bulk_msg);
1713 EXPORT_SYMBOL(usb_sg_init);
1714 EXPORT_SYMBOL(usb_sg_cancel);
1715 EXPORT_SYMBOL(usb_sg_wait);
1717 // synchronous control message convenience routines
1718 EXPORT_SYMBOL(usb_get_descriptor);
1719 EXPORT_SYMBOL(usb_get_status);
1720 EXPORT_SYMBOL(usb_string);
1722 // synchronous calls that also maintain usbcore state
1723 EXPORT_SYMBOL(usb_clear_halt);
1724 EXPORT_SYMBOL(usb_reset_configuration);
1725 EXPORT_SYMBOL(usb_set_interface);