aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/core/message.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/core/message.c')
-rw-r--r--drivers/usb/core/message.c194
1 files changed, 123 insertions, 71 deletions
diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index b6262837765..2bed83caacb 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -10,6 +10,7 @@
10#include <linux/mm.h> 10#include <linux/mm.h>
11#include <linux/timer.h> 11#include <linux/timer.h>
12#include <linux/ctype.h> 12#include <linux/ctype.h>
13#include <linux/nls.h>
13#include <linux/device.h> 14#include <linux/device.h>
14#include <linux/scatterlist.h> 15#include <linux/scatterlist.h>
15#include <linux/usb/quirks.h> 16#include <linux/usb/quirks.h>
@@ -364,6 +365,7 @@ int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
364 int i; 365 int i;
365 int urb_flags; 366 int urb_flags;
366 int dma; 367 int dma;
368 int use_sg;
367 369
368 if (!io || !dev || !sg 370 if (!io || !dev || !sg
369 || usb_pipecontrol(pipe) 371 || usb_pipecontrol(pipe)
@@ -391,7 +393,19 @@ int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
391 if (io->entries <= 0) 393 if (io->entries <= 0)
392 return io->entries; 394 return io->entries;
393 395
394 io->urbs = kmalloc(io->entries * sizeof *io->urbs, mem_flags); 396 /* If we're running on an xHCI host controller, queue the whole scatter
397 * gather list with one call to urb_enqueue(). This is only for bulk,
398 * as that endpoint type does not care how the data gets broken up
399 * across frames.
400 */
401 if (usb_pipebulk(pipe) &&
402 bus_to_hcd(dev->bus)->driver->flags & HCD_USB3) {
403 io->urbs = kmalloc(sizeof *io->urbs, mem_flags);
404 use_sg = true;
405 } else {
406 io->urbs = kmalloc(io->entries * sizeof *io->urbs, mem_flags);
407 use_sg = false;
408 }
395 if (!io->urbs) 409 if (!io->urbs)
396 goto nomem; 410 goto nomem;
397 411
@@ -401,62 +415,92 @@ int usb_sg_init(struct usb_sg_request *io, struct usb_device *dev,
401 if (usb_pipein(pipe)) 415 if (usb_pipein(pipe))
402 urb_flags |= URB_SHORT_NOT_OK; 416 urb_flags |= URB_SHORT_NOT_OK;
403 417
404 for_each_sg(sg, sg, io->entries, i) { 418 if (use_sg) {
405 unsigned len; 419 io->urbs[0] = usb_alloc_urb(0, mem_flags);
406 420 if (!io->urbs[0]) {
407 io->urbs[i] = usb_alloc_urb(0, mem_flags); 421 io->entries = 0;
408 if (!io->urbs[i]) {
409 io->entries = i;
410 goto nomem; 422 goto nomem;
411 } 423 }
412 424
413 io->urbs[i]->dev = NULL; 425 io->urbs[0]->dev = NULL;
414 io->urbs[i]->pipe = pipe; 426 io->urbs[0]->pipe = pipe;
415 io->urbs[i]->interval = period; 427 io->urbs[0]->interval = period;
416 io->urbs[i]->transfer_flags = urb_flags; 428 io->urbs[0]->transfer_flags = urb_flags;
417 429
418 io->urbs[i]->complete = sg_complete; 430 io->urbs[0]->complete = sg_complete;
419 io->urbs[i]->context = io; 431 io->urbs[0]->context = io;
420 432 /* A length of zero means transfer the whole sg list */
421 /* 433 io->urbs[0]->transfer_buffer_length = length;
422 * Some systems need to revert to PIO when DMA is temporarily 434 if (length == 0) {
423 * unavailable. For their sakes, both transfer_buffer and 435 for_each_sg(sg, sg, io->entries, i) {
424 * transfer_dma are set when possible. However this can only 436 io->urbs[0]->transfer_buffer_length +=
425 * work on systems without: 437 sg_dma_len(sg);
426 * 438 }
427 * - HIGHMEM, since DMA buffers located in high memory are 439 }
428 * not directly addressable by the CPU for PIO; 440 io->urbs[0]->sg = io;
429 * 441 io->urbs[0]->num_sgs = io->entries;
430 * - IOMMU, since dma_map_sg() is allowed to use an IOMMU to 442 io->entries = 1;
431 * make virtually discontiguous buffers be "dma-contiguous" 443 } else {
432 * so that PIO and DMA need diferent numbers of URBs. 444 for_each_sg(sg, sg, io->entries, i) {
433 * 445 unsigned len;
434 * So when HIGHMEM or IOMMU are in use, transfer_buffer is NULL 446
435 * to prevent stale pointers and to help spot bugs. 447 io->urbs[i] = usb_alloc_urb(0, mem_flags);
436 */ 448 if (!io->urbs[i]) {
437 if (dma) { 449 io->entries = i;
438 io->urbs[i]->transfer_dma = sg_dma_address(sg); 450 goto nomem;
439 len = sg_dma_len(sg); 451 }
452
453 io->urbs[i]->dev = NULL;
454 io->urbs[i]->pipe = pipe;
455 io->urbs[i]->interval = period;
456 io->urbs[i]->transfer_flags = urb_flags;
457
458 io->urbs[i]->complete = sg_complete;
459 io->urbs[i]->context = io;
460
461 /*
462 * Some systems need to revert to PIO when DMA is
463 * temporarily unavailable. For their sakes, both
464 * transfer_buffer and transfer_dma are set when
465 * possible. However this can only work on systems
466 * without:
467 *
468 * - HIGHMEM, since DMA buffers located in high memory
469 * are not directly addressable by the CPU for PIO;
470 *
471 * - IOMMU, since dma_map_sg() is allowed to use an
472 * IOMMU to make virtually discontiguous buffers be
473 * "dma-contiguous" so that PIO and DMA need diferent
474 * numbers of URBs.
475 *
476 * So when HIGHMEM or IOMMU are in use, transfer_buffer
477 * is NULL to prevent stale pointers and to help spot
478 * bugs.
479 */
480 if (dma) {
481 io->urbs[i]->transfer_dma = sg_dma_address(sg);
482 len = sg_dma_len(sg);
440#if defined(CONFIG_HIGHMEM) || defined(CONFIG_GART_IOMMU) 483#if defined(CONFIG_HIGHMEM) || defined(CONFIG_GART_IOMMU)
441 io->urbs[i]->transfer_buffer = NULL; 484 io->urbs[i]->transfer_buffer = NULL;
442#else 485#else
443 io->urbs[i]->transfer_buffer = sg_virt(sg); 486 io->urbs[i]->transfer_buffer = sg_virt(sg);
444#endif 487#endif
445 } else { 488 } else {
446 /* hc may use _only_ transfer_buffer */ 489 /* hc may use _only_ transfer_buffer */
447 io->urbs[i]->transfer_buffer = sg_virt(sg); 490 io->urbs[i]->transfer_buffer = sg_virt(sg);
448 len = sg->length; 491 len = sg->length;
449 } 492 }
450 493
451 if (length) { 494 if (length) {
452 len = min_t(unsigned, len, length); 495 len = min_t(unsigned, len, length);
453 length -= len; 496 length -= len;
454 if (length == 0) 497 if (length == 0)
455 io->entries = i + 1; 498 io->entries = i + 1;
499 }
500 io->urbs[i]->transfer_buffer_length = len;
456 } 501 }
457 io->urbs[i]->transfer_buffer_length = len; 502 io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
458 } 503 }
459 io->urbs[--i]->transfer_flags &= ~URB_NO_INTERRUPT;
460 504
461 /* transaction state */ 505 /* transaction state */
462 io->count = io->entries; 506 io->count = io->entries;
@@ -509,6 +553,10 @@ EXPORT_SYMBOL_GPL(usb_sg_init);
509 * could be transferred. That capability is less useful for low or full 553 * could be transferred. That capability is less useful for low or full
510 * speed interrupt endpoints, which allow at most one packet per millisecond, 554 * speed interrupt endpoints, which allow at most one packet per millisecond,
511 * of at most 8 or 64 bytes (respectively). 555 * of at most 8 or 64 bytes (respectively).
556 *
557 * It is not necessary to call this function to reserve bandwidth for devices
558 * under an xHCI host controller, as the bandwidth is reserved when the
559 * configuration or interface alt setting is selected.
512 */ 560 */
513void usb_sg_wait(struct usb_sg_request *io) 561void usb_sg_wait(struct usb_sg_request *io)
514{ 562{
@@ -759,7 +807,7 @@ static int usb_string_sub(struct usb_device *dev, unsigned int langid,
759} 807}
760 808
761/** 809/**
762 * usb_string - returns ISO 8859-1 version of a string descriptor 810 * usb_string - returns UTF-8 version of a string descriptor
763 * @dev: the device whose string descriptor is being retrieved 811 * @dev: the device whose string descriptor is being retrieved
764 * @index: the number of the descriptor 812 * @index: the number of the descriptor
765 * @buf: where to put the string 813 * @buf: where to put the string
@@ -767,17 +815,10 @@ static int usb_string_sub(struct usb_device *dev, unsigned int langid,
767 * Context: !in_interrupt () 815 * Context: !in_interrupt ()
768 * 816 *
769 * This converts the UTF-16LE encoded strings returned by devices, from 817 * This converts the UTF-16LE encoded strings returned by devices, from
770 * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones 818 * usb_get_string_descriptor(), to null-terminated UTF-8 encoded ones
771 * that are more usable in most kernel contexts. Note that all characters 819 * that are more usable in most kernel contexts. Note that this function
772 * in the chosen descriptor that can't be encoded using ISO-8859-1
773 * are converted to the question mark ("?") character, and this function
774 * chooses strings in the first language supported by the device. 820 * chooses strings in the first language supported by the device.
775 * 821 *
776 * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit
777 * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode,
778 * and is appropriate for use many uses of English and several other
779 * Western European languages. (But it doesn't include the "Euro" symbol.)
780 *
781 * This call is synchronous, and may not be used in an interrupt context. 822 * This call is synchronous, and may not be used in an interrupt context.
782 * 823 *
783 * Returns length of the string (>= 0) or usb_control_msg status (< 0). 824 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
@@ -786,7 +827,6 @@ int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
786{ 827{
787 unsigned char *tbuf; 828 unsigned char *tbuf;
788 int err; 829 int err;
789 unsigned int u, idx;
790 830
791 if (dev->state == USB_STATE_SUSPENDED) 831 if (dev->state == USB_STATE_SUSPENDED)
792 return -EHOSTUNREACH; 832 return -EHOSTUNREACH;
@@ -821,16 +861,9 @@ int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
821 goto errout; 861 goto errout;
822 862
823 size--; /* leave room for trailing NULL char in output buffer */ 863 size--; /* leave room for trailing NULL char in output buffer */
824 for (idx = 0, u = 2; u < err; u += 2) { 864 err = utf16s_to_utf8s((wchar_t *) &tbuf[2], (err - 2) / 2,
825 if (idx >= size) 865 UTF16_LITTLE_ENDIAN, buf, size);
826 break; 866 buf[err] = 0;
827 if (tbuf[u+1]) /* high byte */
828 buf[idx++] = '?'; /* non ISO-8859-1 character */
829 else
830 buf[idx++] = tbuf[u];
831 }
832 buf[idx] = 0;
833 err = idx;
834 867
835 if (tbuf[1] != USB_DT_STRING) 868 if (tbuf[1] != USB_DT_STRING)
836 dev_dbg(&dev->dev, 869 dev_dbg(&dev->dev,
@@ -843,6 +876,9 @@ int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
843} 876}
844EXPORT_SYMBOL_GPL(usb_string); 877EXPORT_SYMBOL_GPL(usb_string);
845 878
879/* one UTF-8-encoded 16-bit character has at most three bytes */
880#define MAX_USB_STRING_SIZE (127 * 3 + 1)
881
846/** 882/**
847 * usb_cache_string - read a string descriptor and cache it for later use 883 * usb_cache_string - read a string descriptor and cache it for later use
848 * @udev: the device whose string descriptor is being read 884 * @udev: the device whose string descriptor is being read
@@ -860,9 +896,9 @@ char *usb_cache_string(struct usb_device *udev, int index)
860 if (index <= 0) 896 if (index <= 0)
861 return NULL; 897 return NULL;
862 898
863 buf = kmalloc(256, GFP_KERNEL); 899 buf = kmalloc(MAX_USB_STRING_SIZE, GFP_KERNEL);
864 if (buf) { 900 if (buf) {
865 len = usb_string(udev, index, buf, 256); 901 len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
866 if (len > 0) { 902 if (len > 0) {
867 smallbuf = kmalloc(++len, GFP_KERNEL); 903 smallbuf = kmalloc(++len, GFP_KERNEL);
868 if (!smallbuf) 904 if (!smallbuf)
@@ -1664,6 +1700,21 @@ free_interfaces:
1664 if (ret) 1700 if (ret)
1665 goto free_interfaces; 1701 goto free_interfaces;
1666 1702
1703 /* Make sure we have bandwidth (and available HCD resources) for this
1704 * configuration. Remove endpoints from the schedule if we're dropping
1705 * this configuration to set configuration 0. After this point, the
1706 * host controller will not allow submissions to dropped endpoints. If
1707 * this call fails, the device state is unchanged.
1708 */
1709 if (cp)
1710 ret = usb_hcd_check_bandwidth(dev, cp, NULL);
1711 else
1712 ret = usb_hcd_check_bandwidth(dev, NULL, NULL);
1713 if (ret < 0) {
1714 usb_autosuspend_device(dev);
1715 goto free_interfaces;
1716 }
1717
1667 /* if it's already configured, clear out old state first. 1718 /* if it's already configured, clear out old state first.
1668 * getting rid of old interfaces means unbinding their drivers. 1719 * getting rid of old interfaces means unbinding their drivers.
1669 */ 1720 */
@@ -1686,6 +1737,7 @@ free_interfaces:
1686 dev->actconfig = cp; 1737 dev->actconfig = cp;
1687 if (!cp) { 1738 if (!cp) {
1688 usb_set_device_state(dev, USB_STATE_ADDRESS); 1739 usb_set_device_state(dev, USB_STATE_ADDRESS);
1740 usb_hcd_check_bandwidth(dev, NULL, NULL);
1689 usb_autosuspend_device(dev); 1741 usb_autosuspend_device(dev);
1690 goto free_interfaces; 1742 goto free_interfaces;
1691 } 1743 }