aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/gadget
diff options
context:
space:
mode:
authorGrant Likely <grant.likely@secretlab.ca>2010-05-22 02:36:56 -0400
committerGrant Likely <grant.likely@secretlab.ca>2010-05-22 02:36:56 -0400
commitcf9b59e9d3e008591d1f54830f570982bb307a0d (patch)
tree113478ce8fd8c832ba726ffdf59b82cb46356476 /drivers/usb/gadget
parent44504b2bebf8b5823c59484e73096a7d6574471d (diff)
parentf4b87dee923342505e1ddba8d34ce9de33e75050 (diff)
Merge remote branch 'origin' into secretlab/next-devicetree
Merging in current state of Linus' tree to deal with merge conflicts and build failures in vio.c after merge. Conflicts: drivers/i2c/busses/i2c-cpm.c drivers/i2c/busses/i2c-mpc.c drivers/net/gianfar.c Also fixed up one line in arch/powerpc/kernel/vio.c to use the correct node pointer. Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
Diffstat (limited to 'drivers/usb/gadget')
-rw-r--r--drivers/usb/gadget/Kconfig58
-rw-r--r--drivers/usb/gadget/Makefile8
-rw-r--r--drivers/usb/gadget/at91_udc.c7
-rw-r--r--drivers/usb/gadget/atmel_usba_udc.c3
-rw-r--r--drivers/usb/gadget/composite.c60
-rw-r--r--drivers/usb/gadget/config.c4
-rw-r--r--drivers/usb/gadget/dummy_hcd.c4
-rw-r--r--drivers/usb/gadget/epautoconf.c12
-rw-r--r--drivers/usb/gadget/f_acm.c32
-rw-r--r--drivers/usb/gadget/f_ecm.c33
-rw-r--r--drivers/usb/gadget/f_fs.c2442
-rw-r--r--drivers/usb/gadget/f_hid.c673
-rw-r--r--drivers/usb/gadget/f_mass_storage.c138
-rw-r--r--drivers/usb/gadget/f_rndis.c33
-rw-r--r--drivers/usb/gadget/f_uvc.c661
-rw-r--r--drivers/usb/gadget/f_uvc.h376
-rw-r--r--drivers/usb/gadget/fsl_mxc_udc.c (renamed from drivers/usb/gadget/fsl_mx3_udc.c)14
-rw-r--r--drivers/usb/gadget/fsl_udc_core.c2
-rw-r--r--drivers/usb/gadget/g_ffs.c426
-rw-r--r--drivers/usb/gadget/hid.c298
-rw-r--r--drivers/usb/gadget/pxa27x_udc.h2
-rw-r--r--drivers/usb/gadget/s3c-hsotg.c7
-rw-r--r--drivers/usb/gadget/storage_common.c2
-rw-r--r--drivers/usb/gadget/u_ether.c4
-rw-r--r--drivers/usb/gadget/uvc.h241
-rw-r--r--drivers/usb/gadget/uvc_queue.c583
-rw-r--r--drivers/usb/gadget/uvc_queue.h89
-rw-r--r--drivers/usb/gadget/uvc_v4l2.c374
-rw-r--r--drivers/usb/gadget/uvc_video.c386
-rw-r--r--drivers/usb/gadget/webcam.c399
30 files changed, 7241 insertions, 130 deletions
diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
index 11a3e0fa4331..649c0c5f7158 100644
--- a/drivers/usb/gadget/Kconfig
+++ b/drivers/usb/gadget/Kconfig
@@ -710,6 +710,43 @@ config USB_GADGETFS
710 Say "y" to link the driver statically, or "m" to build a 710 Say "y" to link the driver statically, or "m" to build a
711 dynamically linked module called "gadgetfs". 711 dynamically linked module called "gadgetfs".
712 712
713config USB_FUNCTIONFS
714 tristate "Function Filesystem (EXPERIMENTAL)"
715 depends on EXPERIMENTAL
716 help
717 The Function Filesystem (FunctioFS) lets one create USB
718 composite functions in user space in the same way as GadgetFS
719 lets one create USB gadgets in user space. This allows creation
720 of composite gadgets such that some of the functions are
721 implemented in kernel space (for instance Ethernet, serial or
722 mass storage) and other are implemented in user space.
723
724 Say "y" to link the driver statically, or "m" to build
725 a dynamically linked module called "g_ffs".
726
727config USB_FUNCTIONFS_ETH
728 bool "Include CDC ECM (Ethernet) function"
729 depends on USB_FUNCTIONFS && NET
730 help
731 Include an CDC ECM (Ethernet) funcion in the CDC ECM (Funcion)
732 Filesystem. If you also say "y" to the RNDIS query below the
733 gadget will have two configurations.
734
735config USB_FUNCTIONFS_RNDIS
736 bool "Include RNDIS (Ethernet) function"
737 depends on USB_FUNCTIONFS && NET
738 help
739 Include an RNDIS (Ethernet) funcion in the Funcion Filesystem.
740 If you also say "y" to the CDC ECM query above the gadget will
741 have two configurations.
742
743config USB_FUNCTIONFS_GENERIC
744 bool "Include 'pure' configuration"
745 depends on USB_FUNCTIONFS && (USB_FUNCTIONFS_ETH || USB_FUNCTIONFS_RNDIS)
746 help
747 Include a configuration with FunctionFS and no Ethernet
748 configuration.
749
713config USB_FILE_STORAGE 750config USB_FILE_STORAGE
714 tristate "File-backed Storage Gadget" 751 tristate "File-backed Storage Gadget"
715 depends on BLOCK 752 depends on BLOCK
@@ -863,11 +900,30 @@ config USB_G_MULTI_CDC
863 900
864 If unsure, say "y". 901 If unsure, say "y".
865 902
903config USB_G_HID
904 tristate "HID Gadget"
905 help
906 The HID gadget driver provides generic emulation of USB
907 Human Interface Devices (HID).
908
909 For more information, see Documentation/usb/gadget_hid.txt which
910 includes sample code for accessing the device files.
911
912 Say "y" to link the driver statically, or "m" to build a
913 dynamically linked module called "g_hid".
866 914
867# put drivers that need isochronous transfer support (for audio 915# put drivers that need isochronous transfer support (for audio
868# or video class gadget drivers), or specific hardware, here. 916# or video class gadget drivers), or specific hardware, here.
917config USB_G_WEBCAM
918 tristate "USB Webcam Gadget"
919 depends on VIDEO_DEV
920 help
921 The Webcam Gadget acts as a composite USB Audio and Video Class
922 device. It provides a userspace API to process UVC control requests
923 and stream video data to the host.
869 924
870# - none yet 925 Say "y" to link the driver statically, or "m" to build a
926 dynamically linked module called "g_webcam".
871 927
872endchoice 928endchoice
873 929
diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile
index 43b51da8d727..9bcde110feb1 100644
--- a/drivers/usb/gadget/Makefile
+++ b/drivers/usb/gadget/Makefile
@@ -20,7 +20,7 @@ obj-$(CONFIG_USB_ATMEL_USBA) += atmel_usba_udc.o
20obj-$(CONFIG_USB_FSL_USB2) += fsl_usb2_udc.o 20obj-$(CONFIG_USB_FSL_USB2) += fsl_usb2_udc.o
21fsl_usb2_udc-objs := fsl_udc_core.o 21fsl_usb2_udc-objs := fsl_udc_core.o
22ifeq ($(CONFIG_ARCH_MXC),y) 22ifeq ($(CONFIG_ARCH_MXC),y)
23fsl_usb2_udc-objs += fsl_mx3_udc.o 23fsl_usb2_udc-objs += fsl_mxc_udc.o
24endif 24endif
25obj-$(CONFIG_USB_M66592) += m66592-udc.o 25obj-$(CONFIG_USB_M66592) += m66592-udc.o
26obj-$(CONFIG_USB_R8A66597) += r8a66597-udc.o 26obj-$(CONFIG_USB_R8A66597) += r8a66597-udc.o
@@ -43,18 +43,24 @@ g_mass_storage-objs := mass_storage.o
43g_printer-objs := printer.o 43g_printer-objs := printer.o
44g_cdc-objs := cdc2.o 44g_cdc-objs := cdc2.o
45g_multi-objs := multi.o 45g_multi-objs := multi.o
46g_hid-objs := hid.o
46g_nokia-objs := nokia.o 47g_nokia-objs := nokia.o
48g_webcam-objs := webcam.o
47 49
48obj-$(CONFIG_USB_ZERO) += g_zero.o 50obj-$(CONFIG_USB_ZERO) += g_zero.o
49obj-$(CONFIG_USB_AUDIO) += g_audio.o 51obj-$(CONFIG_USB_AUDIO) += g_audio.o
50obj-$(CONFIG_USB_ETH) += g_ether.o 52obj-$(CONFIG_USB_ETH) += g_ether.o
51obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o 53obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o
54obj-$(CONFIG_USB_FUNCTIONFS) += g_ffs.o
55obj-$(CONFIG_USB_ETH_FUNCTIONFS) += g_eth_ffs.o
52obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o 56obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o
53obj-$(CONFIG_USB_MASS_STORAGE) += g_mass_storage.o 57obj-$(CONFIG_USB_MASS_STORAGE) += g_mass_storage.o
54obj-$(CONFIG_USB_G_SERIAL) += g_serial.o 58obj-$(CONFIG_USB_G_SERIAL) += g_serial.o
55obj-$(CONFIG_USB_G_PRINTER) += g_printer.o 59obj-$(CONFIG_USB_G_PRINTER) += g_printer.o
56obj-$(CONFIG_USB_MIDI_GADGET) += g_midi.o 60obj-$(CONFIG_USB_MIDI_GADGET) += g_midi.o
57obj-$(CONFIG_USB_CDC_COMPOSITE) += g_cdc.o 61obj-$(CONFIG_USB_CDC_COMPOSITE) += g_cdc.o
62obj-$(CONFIG_USB_G_HID) += g_hid.o
58obj-$(CONFIG_USB_G_MULTI) += g_multi.o 63obj-$(CONFIG_USB_G_MULTI) += g_multi.o
59obj-$(CONFIG_USB_G_NOKIA) += g_nokia.o 64obj-$(CONFIG_USB_G_NOKIA) += g_nokia.o
65obj-$(CONFIG_USB_G_WEBCAM) += g_webcam.o
60 66
diff --git a/drivers/usb/gadget/at91_udc.c b/drivers/usb/gadget/at91_udc.c
index df1bae9b048e..eaa79c8a9b8c 100644
--- a/drivers/usb/gadget/at91_udc.c
+++ b/drivers/usb/gadget/at91_udc.c
@@ -366,6 +366,13 @@ rescan:
366 if (is_done) 366 if (is_done)
367 done(ep, req, 0); 367 done(ep, req, 0);
368 else if (ep->is_pingpong) { 368 else if (ep->is_pingpong) {
369 /*
370 * One dummy read to delay the code because of a HW glitch:
371 * CSR returns bad RXCOUNT when read too soon after updating
372 * RX_DATA_BK flags.
373 */
374 csr = __raw_readl(creg);
375
369 bufferspace -= count; 376 bufferspace -= count;
370 buf += count; 377 buf += count;
371 goto rescan; 378 goto rescan;
diff --git a/drivers/usb/gadget/atmel_usba_udc.c b/drivers/usb/gadget/atmel_usba_udc.c
index 75a256f3d45b..d623c7bda1f6 100644
--- a/drivers/usb/gadget/atmel_usba_udc.c
+++ b/drivers/usb/gadget/atmel_usba_udc.c
@@ -48,10 +48,9 @@ static int queue_dbg_open(struct inode *inode, struct file *file)
48 48
49 spin_lock_irq(&ep->udc->lock); 49 spin_lock_irq(&ep->udc->lock);
50 list_for_each_entry(req, &ep->queue, queue) { 50 list_for_each_entry(req, &ep->queue, queue) {
51 req_copy = kmalloc(sizeof(*req_copy), GFP_ATOMIC); 51 req_copy = kmemdup(req, sizeof(*req_copy), GFP_ATOMIC);
52 if (!req_copy) 52 if (!req_copy)
53 goto fail; 53 goto fail;
54 memcpy(req_copy, req, sizeof(*req_copy));
55 list_add_tail(&req_copy->queue, queue_data); 54 list_add_tail(&req_copy->queue, queue_data);
56 } 55 }
57 spin_unlock_irq(&ep->udc->lock); 56 spin_unlock_irq(&ep->udc->lock);
diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c
index 09289bb1e20f..391d169f8d07 100644
--- a/drivers/usb/gadget/composite.c
+++ b/drivers/usb/gadget/composite.c
@@ -36,7 +36,7 @@
36 */ 36 */
37 37
38/* big enough to hold our biggest descriptor */ 38/* big enough to hold our biggest descriptor */
39#define USB_BUFSIZ 512 39#define USB_BUFSIZ 1024
40 40
41static struct usb_composite_driver *composite; 41static struct usb_composite_driver *composite;
42 42
@@ -85,7 +85,7 @@ MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
85 * This function returns the value of the function's bind(), which is 85 * This function returns the value of the function's bind(), which is
86 * zero for success else a negative errno value. 86 * zero for success else a negative errno value.
87 */ 87 */
88int __init usb_add_function(struct usb_configuration *config, 88int usb_add_function(struct usb_configuration *config,
89 struct usb_function *function) 89 struct usb_function *function)
90{ 90{
91 int value = -EINVAL; 91 int value = -EINVAL;
@@ -215,7 +215,7 @@ int usb_function_activate(struct usb_function *function)
215 * Returns the interface ID which was allocated; or -ENODEV if no 215 * Returns the interface ID which was allocated; or -ENODEV if no
216 * more interface IDs can be allocated. 216 * more interface IDs can be allocated.
217 */ 217 */
218int __init usb_interface_id(struct usb_configuration *config, 218int usb_interface_id(struct usb_configuration *config,
219 struct usb_function *function) 219 struct usb_function *function)
220{ 220{
221 unsigned id = config->next_interface_id; 221 unsigned id = config->next_interface_id;
@@ -480,7 +480,7 @@ done:
480 * assigns global resources including string IDs, and per-configuration 480 * assigns global resources including string IDs, and per-configuration
481 * resources such as interface IDs and endpoints. 481 * resources such as interface IDs and endpoints.
482 */ 482 */
483int __init usb_add_config(struct usb_composite_dev *cdev, 483int usb_add_config(struct usb_composite_dev *cdev,
484 struct usb_configuration *config) 484 struct usb_configuration *config)
485{ 485{
486 int status = -EINVAL; 486 int status = -EINVAL;
@@ -677,7 +677,7 @@ static int get_string(struct usb_composite_dev *cdev,
677 * ensure that for example different functions don't wrongly assign 677 * ensure that for example different functions don't wrongly assign
678 * different meanings to the same identifier. 678 * different meanings to the same identifier.
679 */ 679 */
680int __init usb_string_id(struct usb_composite_dev *cdev) 680int usb_string_id(struct usb_composite_dev *cdev)
681{ 681{
682 if (cdev->next_string_id < 254) { 682 if (cdev->next_string_id < 254) {
683 /* string id 0 is reserved */ 683 /* string id 0 is reserved */
@@ -898,7 +898,19 @@ static void composite_disconnect(struct usb_gadget *gadget)
898 898
899/*-------------------------------------------------------------------------*/ 899/*-------------------------------------------------------------------------*/
900 900
901static void /* __init_or_exit */ 901static ssize_t composite_show_suspended(struct device *dev,
902 struct device_attribute *attr,
903 char *buf)
904{
905 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
906 struct usb_composite_dev *cdev = get_gadget_data(gadget);
907
908 return sprintf(buf, "%d\n", cdev->suspended);
909}
910
911static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
912
913static void
902composite_unbind(struct usb_gadget *gadget) 914composite_unbind(struct usb_gadget *gadget)
903{ 915{
904 struct usb_composite_dev *cdev = get_gadget_data(gadget); 916 struct usb_composite_dev *cdev = get_gadget_data(gadget);
@@ -944,10 +956,11 @@ composite_unbind(struct usb_gadget *gadget)
944 } 956 }
945 kfree(cdev); 957 kfree(cdev);
946 set_gadget_data(gadget, NULL); 958 set_gadget_data(gadget, NULL);
959 device_remove_file(&gadget->dev, &dev_attr_suspended);
947 composite = NULL; 960 composite = NULL;
948} 961}
949 962
950static void __init 963static void
951string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s) 964string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
952{ 965{
953 struct usb_string *str = tab->strings; 966 struct usb_string *str = tab->strings;
@@ -960,7 +973,7 @@ string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
960 } 973 }
961} 974}
962 975
963static void __init 976static void
964string_override(struct usb_gadget_strings **tab, u8 id, const char *s) 977string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
965{ 978{
966 while (*tab) { 979 while (*tab) {
@@ -969,7 +982,7 @@ string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
969 } 982 }
970} 983}
971 984
972static int __init composite_bind(struct usb_gadget *gadget) 985static int composite_bind(struct usb_gadget *gadget)
973{ 986{
974 struct usb_composite_dev *cdev; 987 struct usb_composite_dev *cdev;
975 int status = -ENOMEM; 988 int status = -ENOMEM;
@@ -1004,6 +1017,14 @@ static int __init composite_bind(struct usb_gadget *gadget)
1004 */ 1017 */
1005 usb_ep_autoconfig_reset(cdev->gadget); 1018 usb_ep_autoconfig_reset(cdev->gadget);
1006 1019
1020 /* standardized runtime overrides for device ID data */
1021 if (idVendor)
1022 cdev->desc.idVendor = cpu_to_le16(idVendor);
1023 if (idProduct)
1024 cdev->desc.idProduct = cpu_to_le16(idProduct);
1025 if (bcdDevice)
1026 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1027
1007 /* composite gadget needs to assign strings for whole device (like 1028 /* composite gadget needs to assign strings for whole device (like
1008 * serial number), register function drivers, potentially update 1029 * serial number), register function drivers, potentially update
1009 * power state and consumption, etc 1030 * power state and consumption, etc
@@ -1015,14 +1036,6 @@ static int __init composite_bind(struct usb_gadget *gadget)
1015 cdev->desc = *composite->dev; 1036 cdev->desc = *composite->dev;
1016 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; 1037 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1017 1038
1018 /* standardized runtime overrides for device ID data */
1019 if (idVendor)
1020 cdev->desc.idVendor = cpu_to_le16(idVendor);
1021 if (idProduct)
1022 cdev->desc.idProduct = cpu_to_le16(idProduct);
1023 if (bcdDevice)
1024 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1025
1026 /* strings can't be assigned before bind() allocates the 1039 /* strings can't be assigned before bind() allocates the
1027 * releavnt identifiers 1040 * releavnt identifiers
1028 */ 1041 */
@@ -1036,6 +1049,10 @@ static int __init composite_bind(struct usb_gadget *gadget)
1036 string_override(composite->strings, 1049 string_override(composite->strings,
1037 cdev->desc.iSerialNumber, iSerialNumber); 1050 cdev->desc.iSerialNumber, iSerialNumber);
1038 1051
1052 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1053 if (status)
1054 goto fail;
1055
1039 INFO(cdev, "%s ready\n", composite->name); 1056 INFO(cdev, "%s ready\n", composite->name);
1040 return 0; 1057 return 0;
1041 1058
@@ -1064,6 +1081,8 @@ composite_suspend(struct usb_gadget *gadget)
1064 } 1081 }
1065 if (composite->suspend) 1082 if (composite->suspend)
1066 composite->suspend(cdev); 1083 composite->suspend(cdev);
1084
1085 cdev->suspended = 1;
1067} 1086}
1068 1087
1069static void 1088static void
@@ -1084,6 +1103,8 @@ composite_resume(struct usb_gadget *gadget)
1084 f->resume(f); 1103 f->resume(f);
1085 } 1104 }
1086 } 1105 }
1106
1107 cdev->suspended = 0;
1087} 1108}
1088 1109
1089/*-------------------------------------------------------------------------*/ 1110/*-------------------------------------------------------------------------*/
@@ -1092,7 +1113,6 @@ static struct usb_gadget_driver composite_driver = {
1092 .speed = USB_SPEED_HIGH, 1113 .speed = USB_SPEED_HIGH,
1093 1114
1094 .bind = composite_bind, 1115 .bind = composite_bind,
1095 /* .unbind = __exit_p(composite_unbind), */
1096 .unbind = composite_unbind, 1116 .unbind = composite_unbind,
1097 1117
1098 .setup = composite_setup, 1118 .setup = composite_setup,
@@ -1121,7 +1141,7 @@ static struct usb_gadget_driver composite_driver = {
1121 * while it was binding. That would usually be done in order to wait for 1141 * while it was binding. That would usually be done in order to wait for
1122 * some userspace participation. 1142 * some userspace participation.
1123 */ 1143 */
1124int __init usb_composite_register(struct usb_composite_driver *driver) 1144int usb_composite_register(struct usb_composite_driver *driver)
1125{ 1145{
1126 if (!driver || !driver->dev || !driver->bind || composite) 1146 if (!driver || !driver->dev || !driver->bind || composite)
1127 return -EINVAL; 1147 return -EINVAL;
@@ -1142,7 +1162,7 @@ int __init usb_composite_register(struct usb_composite_driver *driver)
1142 * This function is used to unregister drivers using the composite 1162 * This function is used to unregister drivers using the composite
1143 * driver framework. 1163 * driver framework.
1144 */ 1164 */
1145void /* __exit */ usb_composite_unregister(struct usb_composite_driver *driver) 1165void usb_composite_unregister(struct usb_composite_driver *driver)
1146{ 1166{
1147 if (composite != driver) 1167 if (composite != driver)
1148 return; 1168 return;
diff --git a/drivers/usb/gadget/config.c b/drivers/usb/gadget/config.c
index 47e8e722682c..09084fd646ab 100644
--- a/drivers/usb/gadget/config.c
+++ b/drivers/usb/gadget/config.c
@@ -128,7 +128,7 @@ int usb_gadget_config_buf(
128 * with identifiers (for interfaces, strings, endpoints, and more) 128 * with identifiers (for interfaces, strings, endpoints, and more)
129 * as needed by a given function instance. 129 * as needed by a given function instance.
130 */ 130 */
131struct usb_descriptor_header **__init 131struct usb_descriptor_header **
132usb_copy_descriptors(struct usb_descriptor_header **src) 132usb_copy_descriptors(struct usb_descriptor_header **src)
133{ 133{
134 struct usb_descriptor_header **tmp; 134 struct usb_descriptor_header **tmp;
@@ -175,7 +175,7 @@ usb_copy_descriptors(struct usb_descriptor_header **src)
175 * intended use is to help remembering the endpoint descriptor to use 175 * intended use is to help remembering the endpoint descriptor to use
176 * when enabling a given endpoint. 176 * when enabling a given endpoint.
177 */ 177 */
178struct usb_endpoint_descriptor *__init 178struct usb_endpoint_descriptor *
179usb_find_endpoint( 179usb_find_endpoint(
180 struct usb_descriptor_header **src, 180 struct usb_descriptor_header **src,
181 struct usb_descriptor_header **copy, 181 struct usb_descriptor_header **copy,
diff --git a/drivers/usb/gadget/dummy_hcd.c b/drivers/usb/gadget/dummy_hcd.c
index 5e0966485188..4f9e578cde9d 100644
--- a/drivers/usb/gadget/dummy_hcd.c
+++ b/drivers/usb/gadget/dummy_hcd.c
@@ -47,6 +47,7 @@
47#include <linux/platform_device.h> 47#include <linux/platform_device.h>
48#include <linux/usb.h> 48#include <linux/usb.h>
49#include <linux/usb/gadget.h> 49#include <linux/usb/gadget.h>
50#include <linux/usb/hcd.h>
50 51
51#include <asm/byteorder.h> 52#include <asm/byteorder.h>
52#include <asm/io.h> 53#include <asm/io.h>
@@ -55,9 +56,6 @@
55#include <asm/unaligned.h> 56#include <asm/unaligned.h>
56 57
57 58
58#include "../core/hcd.h"
59
60
61#define DRIVER_DESC "USB Host+Gadget Emulator" 59#define DRIVER_DESC "USB Host+Gadget Emulator"
62#define DRIVER_VERSION "02 May 2005" 60#define DRIVER_VERSION "02 May 2005"
63 61
diff --git a/drivers/usb/gadget/epautoconf.c b/drivers/usb/gadget/epautoconf.c
index 3568de210f79..8a832488ccdd 100644
--- a/drivers/usb/gadget/epautoconf.c
+++ b/drivers/usb/gadget/epautoconf.c
@@ -34,12 +34,12 @@
34 34
35 35
36/* we must assign addresses for configurable endpoints (like net2280) */ 36/* we must assign addresses for configurable endpoints (like net2280) */
37static __initdata unsigned epnum; 37static unsigned epnum;
38 38
39// #define MANY_ENDPOINTS 39// #define MANY_ENDPOINTS
40#ifdef MANY_ENDPOINTS 40#ifdef MANY_ENDPOINTS
41/* more than 15 configurable endpoints */ 41/* more than 15 configurable endpoints */
42static __initdata unsigned in_epnum; 42static unsigned in_epnum;
43#endif 43#endif
44 44
45 45
@@ -59,7 +59,7 @@ static __initdata unsigned in_epnum;
59 * NOTE: each endpoint is unidirectional, as specified by its USB 59 * NOTE: each endpoint is unidirectional, as specified by its USB
60 * descriptor; and isn't specific to a configuration or altsetting. 60 * descriptor; and isn't specific to a configuration or altsetting.
61 */ 61 */
62static int __init 62static int
63ep_matches ( 63ep_matches (
64 struct usb_gadget *gadget, 64 struct usb_gadget *gadget,
65 struct usb_ep *ep, 65 struct usb_ep *ep,
@@ -187,7 +187,7 @@ ep_matches (
187 return 1; 187 return 1;
188} 188}
189 189
190static struct usb_ep * __init 190static struct usb_ep *
191find_ep (struct usb_gadget *gadget, const char *name) 191find_ep (struct usb_gadget *gadget, const char *name)
192{ 192{
193 struct usb_ep *ep; 193 struct usb_ep *ep;
@@ -229,7 +229,7 @@ find_ep (struct usb_gadget *gadget, const char *name)
229 * 229 *
230 * On failure, this returns a null endpoint descriptor. 230 * On failure, this returns a null endpoint descriptor.
231 */ 231 */
232struct usb_ep * __init usb_ep_autoconfig ( 232struct usb_ep *usb_ep_autoconfig (
233 struct usb_gadget *gadget, 233 struct usb_gadget *gadget,
234 struct usb_endpoint_descriptor *desc 234 struct usb_endpoint_descriptor *desc
235) 235)
@@ -304,7 +304,7 @@ struct usb_ep * __init usb_ep_autoconfig (
304 * state such as ep->driver_data and the record of assigned endpoints 304 * state such as ep->driver_data and the record of assigned endpoints
305 * used by usb_ep_autoconfig(). 305 * used by usb_ep_autoconfig().
306 */ 306 */
307void __init usb_ep_autoconfig_reset (struct usb_gadget *gadget) 307void usb_ep_autoconfig_reset (struct usb_gadget *gadget)
308{ 308{
309 struct usb_ep *ep; 309 struct usb_ep *ep;
310 310
diff --git a/drivers/usb/gadget/f_acm.c b/drivers/usb/gadget/f_acm.c
index 400e1ebe6976..d47a123f15ab 100644
--- a/drivers/usb/gadget/f_acm.c
+++ b/drivers/usb/gadget/f_acm.c
@@ -116,7 +116,7 @@ acm_iad_descriptor = {
116}; 116};
117 117
118 118
119static struct usb_interface_descriptor acm_control_interface_desc __initdata = { 119static struct usb_interface_descriptor acm_control_interface_desc = {
120 .bLength = USB_DT_INTERFACE_SIZE, 120 .bLength = USB_DT_INTERFACE_SIZE,
121 .bDescriptorType = USB_DT_INTERFACE, 121 .bDescriptorType = USB_DT_INTERFACE,
122 /* .bInterfaceNumber = DYNAMIC */ 122 /* .bInterfaceNumber = DYNAMIC */
@@ -127,7 +127,7 @@ static struct usb_interface_descriptor acm_control_interface_desc __initdata = {
127 /* .iInterface = DYNAMIC */ 127 /* .iInterface = DYNAMIC */
128}; 128};
129 129
130static struct usb_interface_descriptor acm_data_interface_desc __initdata = { 130static struct usb_interface_descriptor acm_data_interface_desc = {
131 .bLength = USB_DT_INTERFACE_SIZE, 131 .bLength = USB_DT_INTERFACE_SIZE,
132 .bDescriptorType = USB_DT_INTERFACE, 132 .bDescriptorType = USB_DT_INTERFACE,
133 /* .bInterfaceNumber = DYNAMIC */ 133 /* .bInterfaceNumber = DYNAMIC */
@@ -138,7 +138,7 @@ static struct usb_interface_descriptor acm_data_interface_desc __initdata = {
138 /* .iInterface = DYNAMIC */ 138 /* .iInterface = DYNAMIC */
139}; 139};
140 140
141static struct usb_cdc_header_desc acm_header_desc __initdata = { 141static struct usb_cdc_header_desc acm_header_desc = {
142 .bLength = sizeof(acm_header_desc), 142 .bLength = sizeof(acm_header_desc),
143 .bDescriptorType = USB_DT_CS_INTERFACE, 143 .bDescriptorType = USB_DT_CS_INTERFACE,
144 .bDescriptorSubType = USB_CDC_HEADER_TYPE, 144 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
@@ -146,7 +146,7 @@ static struct usb_cdc_header_desc acm_header_desc __initdata = {
146}; 146};
147 147
148static struct usb_cdc_call_mgmt_descriptor 148static struct usb_cdc_call_mgmt_descriptor
149acm_call_mgmt_descriptor __initdata = { 149acm_call_mgmt_descriptor = {
150 .bLength = sizeof(acm_call_mgmt_descriptor), 150 .bLength = sizeof(acm_call_mgmt_descriptor),
151 .bDescriptorType = USB_DT_CS_INTERFACE, 151 .bDescriptorType = USB_DT_CS_INTERFACE,
152 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, 152 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
@@ -154,14 +154,14 @@ acm_call_mgmt_descriptor __initdata = {
154 /* .bDataInterface = DYNAMIC */ 154 /* .bDataInterface = DYNAMIC */
155}; 155};
156 156
157static struct usb_cdc_acm_descriptor acm_descriptor __initdata = { 157static struct usb_cdc_acm_descriptor acm_descriptor = {
158 .bLength = sizeof(acm_descriptor), 158 .bLength = sizeof(acm_descriptor),
159 .bDescriptorType = USB_DT_CS_INTERFACE, 159 .bDescriptorType = USB_DT_CS_INTERFACE,
160 .bDescriptorSubType = USB_CDC_ACM_TYPE, 160 .bDescriptorSubType = USB_CDC_ACM_TYPE,
161 .bmCapabilities = USB_CDC_CAP_LINE, 161 .bmCapabilities = USB_CDC_CAP_LINE,
162}; 162};
163 163
164static struct usb_cdc_union_desc acm_union_desc __initdata = { 164static struct usb_cdc_union_desc acm_union_desc = {
165 .bLength = sizeof(acm_union_desc), 165 .bLength = sizeof(acm_union_desc),
166 .bDescriptorType = USB_DT_CS_INTERFACE, 166 .bDescriptorType = USB_DT_CS_INTERFACE,
167 .bDescriptorSubType = USB_CDC_UNION_TYPE, 167 .bDescriptorSubType = USB_CDC_UNION_TYPE,
@@ -171,7 +171,7 @@ static struct usb_cdc_union_desc acm_union_desc __initdata = {
171 171
172/* full speed support: */ 172/* full speed support: */
173 173
174static struct usb_endpoint_descriptor acm_fs_notify_desc __initdata = { 174static struct usb_endpoint_descriptor acm_fs_notify_desc = {
175 .bLength = USB_DT_ENDPOINT_SIZE, 175 .bLength = USB_DT_ENDPOINT_SIZE,
176 .bDescriptorType = USB_DT_ENDPOINT, 176 .bDescriptorType = USB_DT_ENDPOINT,
177 .bEndpointAddress = USB_DIR_IN, 177 .bEndpointAddress = USB_DIR_IN,
@@ -180,21 +180,21 @@ static struct usb_endpoint_descriptor acm_fs_notify_desc __initdata = {
180 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL, 180 .bInterval = 1 << GS_LOG2_NOTIFY_INTERVAL,
181}; 181};
182 182
183static struct usb_endpoint_descriptor acm_fs_in_desc __initdata = { 183static struct usb_endpoint_descriptor acm_fs_in_desc = {
184 .bLength = USB_DT_ENDPOINT_SIZE, 184 .bLength = USB_DT_ENDPOINT_SIZE,
185 .bDescriptorType = USB_DT_ENDPOINT, 185 .bDescriptorType = USB_DT_ENDPOINT,
186 .bEndpointAddress = USB_DIR_IN, 186 .bEndpointAddress = USB_DIR_IN,
187 .bmAttributes = USB_ENDPOINT_XFER_BULK, 187 .bmAttributes = USB_ENDPOINT_XFER_BULK,
188}; 188};
189 189
190static struct usb_endpoint_descriptor acm_fs_out_desc __initdata = { 190static struct usb_endpoint_descriptor acm_fs_out_desc = {
191 .bLength = USB_DT_ENDPOINT_SIZE, 191 .bLength = USB_DT_ENDPOINT_SIZE,
192 .bDescriptorType = USB_DT_ENDPOINT, 192 .bDescriptorType = USB_DT_ENDPOINT,
193 .bEndpointAddress = USB_DIR_OUT, 193 .bEndpointAddress = USB_DIR_OUT,
194 .bmAttributes = USB_ENDPOINT_XFER_BULK, 194 .bmAttributes = USB_ENDPOINT_XFER_BULK,
195}; 195};
196 196
197static struct usb_descriptor_header *acm_fs_function[] __initdata = { 197static struct usb_descriptor_header *acm_fs_function[] = {
198 (struct usb_descriptor_header *) &acm_iad_descriptor, 198 (struct usb_descriptor_header *) &acm_iad_descriptor,
199 (struct usb_descriptor_header *) &acm_control_interface_desc, 199 (struct usb_descriptor_header *) &acm_control_interface_desc,
200 (struct usb_descriptor_header *) &acm_header_desc, 200 (struct usb_descriptor_header *) &acm_header_desc,
@@ -210,7 +210,7 @@ static struct usb_descriptor_header *acm_fs_function[] __initdata = {
210 210
211/* high speed support: */ 211/* high speed support: */
212 212
213static struct usb_endpoint_descriptor acm_hs_notify_desc __initdata = { 213static struct usb_endpoint_descriptor acm_hs_notify_desc = {
214 .bLength = USB_DT_ENDPOINT_SIZE, 214 .bLength = USB_DT_ENDPOINT_SIZE,
215 .bDescriptorType = USB_DT_ENDPOINT, 215 .bDescriptorType = USB_DT_ENDPOINT,
216 .bEndpointAddress = USB_DIR_IN, 216 .bEndpointAddress = USB_DIR_IN,
@@ -219,21 +219,21 @@ static struct usb_endpoint_descriptor acm_hs_notify_desc __initdata = {
219 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4, 219 .bInterval = GS_LOG2_NOTIFY_INTERVAL+4,
220}; 220};
221 221
222static struct usb_endpoint_descriptor acm_hs_in_desc __initdata = { 222static struct usb_endpoint_descriptor acm_hs_in_desc = {
223 .bLength = USB_DT_ENDPOINT_SIZE, 223 .bLength = USB_DT_ENDPOINT_SIZE,
224 .bDescriptorType = USB_DT_ENDPOINT, 224 .bDescriptorType = USB_DT_ENDPOINT,
225 .bmAttributes = USB_ENDPOINT_XFER_BULK, 225 .bmAttributes = USB_ENDPOINT_XFER_BULK,
226 .wMaxPacketSize = cpu_to_le16(512), 226 .wMaxPacketSize = cpu_to_le16(512),
227}; 227};
228 228
229static struct usb_endpoint_descriptor acm_hs_out_desc __initdata = { 229static struct usb_endpoint_descriptor acm_hs_out_desc = {
230 .bLength = USB_DT_ENDPOINT_SIZE, 230 .bLength = USB_DT_ENDPOINT_SIZE,
231 .bDescriptorType = USB_DT_ENDPOINT, 231 .bDescriptorType = USB_DT_ENDPOINT,
232 .bmAttributes = USB_ENDPOINT_XFER_BULK, 232 .bmAttributes = USB_ENDPOINT_XFER_BULK,
233 .wMaxPacketSize = cpu_to_le16(512), 233 .wMaxPacketSize = cpu_to_le16(512),
234}; 234};
235 235
236static struct usb_descriptor_header *acm_hs_function[] __initdata = { 236static struct usb_descriptor_header *acm_hs_function[] = {
237 (struct usb_descriptor_header *) &acm_iad_descriptor, 237 (struct usb_descriptor_header *) &acm_iad_descriptor,
238 (struct usb_descriptor_header *) &acm_control_interface_desc, 238 (struct usb_descriptor_header *) &acm_control_interface_desc,
239 (struct usb_descriptor_header *) &acm_header_desc, 239 (struct usb_descriptor_header *) &acm_header_desc,
@@ -571,7 +571,7 @@ static int acm_send_break(struct gserial *port, int duration)
571/*-------------------------------------------------------------------------*/ 571/*-------------------------------------------------------------------------*/
572 572
573/* ACM function driver setup/binding */ 573/* ACM function driver setup/binding */
574static int __init 574static int
575acm_bind(struct usb_configuration *c, struct usb_function *f) 575acm_bind(struct usb_configuration *c, struct usb_function *f)
576{ 576{
577 struct usb_composite_dev *cdev = c->cdev; 577 struct usb_composite_dev *cdev = c->cdev;
@@ -719,7 +719,7 @@ static inline bool can_support_cdc(struct usb_configuration *c)
719 * handle all the ones it binds. Caller is also responsible 719 * handle all the ones it binds. Caller is also responsible
720 * for calling @gserial_cleanup() before module unload. 720 * for calling @gserial_cleanup() before module unload.
721 */ 721 */
722int __init acm_bind_config(struct usb_configuration *c, u8 port_num) 722int acm_bind_config(struct usb_configuration *c, u8 port_num)
723{ 723{
724 struct f_acm *acm; 724 struct f_acm *acm;
725 int status; 725 int status;
diff --git a/drivers/usb/gadget/f_ecm.c b/drivers/usb/gadget/f_ecm.c
index 4e595324c614..544257a89ed2 100644
--- a/drivers/usb/gadget/f_ecm.c
+++ b/drivers/usb/gadget/f_ecm.c
@@ -113,7 +113,7 @@ static inline unsigned ecm_bitrate(struct usb_gadget *g)
113 113
114/* interface descriptor: */ 114/* interface descriptor: */
115 115
116static struct usb_interface_descriptor ecm_control_intf __initdata = { 116static struct usb_interface_descriptor ecm_control_intf = {
117 .bLength = sizeof ecm_control_intf, 117 .bLength = sizeof ecm_control_intf,
118 .bDescriptorType = USB_DT_INTERFACE, 118 .bDescriptorType = USB_DT_INTERFACE,
119 119
@@ -126,7 +126,7 @@ static struct usb_interface_descriptor ecm_control_intf __initdata = {
126 /* .iInterface = DYNAMIC */ 126 /* .iInterface = DYNAMIC */
127}; 127};
128 128
129static struct usb_cdc_header_desc ecm_header_desc __initdata = { 129static struct usb_cdc_header_desc ecm_header_desc = {
130 .bLength = sizeof ecm_header_desc, 130 .bLength = sizeof ecm_header_desc,
131 .bDescriptorType = USB_DT_CS_INTERFACE, 131 .bDescriptorType = USB_DT_CS_INTERFACE,
132 .bDescriptorSubType = USB_CDC_HEADER_TYPE, 132 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
@@ -134,7 +134,7 @@ static struct usb_cdc_header_desc ecm_header_desc __initdata = {
134 .bcdCDC = cpu_to_le16(0x0110), 134 .bcdCDC = cpu_to_le16(0x0110),
135}; 135};
136 136
137static struct usb_cdc_union_desc ecm_union_desc __initdata = { 137static struct usb_cdc_union_desc ecm_union_desc = {
138 .bLength = sizeof(ecm_union_desc), 138 .bLength = sizeof(ecm_union_desc),
139 .bDescriptorType = USB_DT_CS_INTERFACE, 139 .bDescriptorType = USB_DT_CS_INTERFACE,
140 .bDescriptorSubType = USB_CDC_UNION_TYPE, 140 .bDescriptorSubType = USB_CDC_UNION_TYPE,
@@ -142,7 +142,7 @@ static struct usb_cdc_union_desc ecm_union_desc __initdata = {
142 /* .bSlaveInterface0 = DYNAMIC */ 142 /* .bSlaveInterface0 = DYNAMIC */
143}; 143};
144 144
145static struct usb_cdc_ether_desc ecm_desc __initdata = { 145static struct usb_cdc_ether_desc ecm_desc = {
146 .bLength = sizeof ecm_desc, 146 .bLength = sizeof ecm_desc,
147 .bDescriptorType = USB_DT_CS_INTERFACE, 147 .bDescriptorType = USB_DT_CS_INTERFACE,
148 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE, 148 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
@@ -157,7 +157,7 @@ static struct usb_cdc_ether_desc ecm_desc __initdata = {
157 157
158/* the default data interface has no endpoints ... */ 158/* the default data interface has no endpoints ... */
159 159
160static struct usb_interface_descriptor ecm_data_nop_intf __initdata = { 160static struct usb_interface_descriptor ecm_data_nop_intf = {
161 .bLength = sizeof ecm_data_nop_intf, 161 .bLength = sizeof ecm_data_nop_intf,
162 .bDescriptorType = USB_DT_INTERFACE, 162 .bDescriptorType = USB_DT_INTERFACE,
163 163
@@ -172,7 +172,7 @@ static struct usb_interface_descriptor ecm_data_nop_intf __initdata = {
172 172
173/* ... but the "real" data interface has two bulk endpoints */ 173/* ... but the "real" data interface has two bulk endpoints */
174 174
175static struct usb_interface_descriptor ecm_data_intf __initdata = { 175static struct usb_interface_descriptor ecm_data_intf = {
176 .bLength = sizeof ecm_data_intf, 176 .bLength = sizeof ecm_data_intf,
177 .bDescriptorType = USB_DT_INTERFACE, 177 .bDescriptorType = USB_DT_INTERFACE,
178 178
@@ -187,7 +187,7 @@ static struct usb_interface_descriptor ecm_data_intf __initdata = {
187 187
188/* full speed support: */ 188/* full speed support: */
189 189
190static struct usb_endpoint_descriptor fs_ecm_notify_desc __initdata = { 190static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
191 .bLength = USB_DT_ENDPOINT_SIZE, 191 .bLength = USB_DT_ENDPOINT_SIZE,
192 .bDescriptorType = USB_DT_ENDPOINT, 192 .bDescriptorType = USB_DT_ENDPOINT,
193 193
@@ -197,7 +197,7 @@ static struct usb_endpoint_descriptor fs_ecm_notify_desc __initdata = {
197 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, 197 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
198}; 198};
199 199
200static struct usb_endpoint_descriptor fs_ecm_in_desc __initdata = { 200static struct usb_endpoint_descriptor fs_ecm_in_desc = {
201 .bLength = USB_DT_ENDPOINT_SIZE, 201 .bLength = USB_DT_ENDPOINT_SIZE,
202 .bDescriptorType = USB_DT_ENDPOINT, 202 .bDescriptorType = USB_DT_ENDPOINT,
203 203
@@ -205,7 +205,7 @@ static struct usb_endpoint_descriptor fs_ecm_in_desc __initdata = {
205 .bmAttributes = USB_ENDPOINT_XFER_BULK, 205 .bmAttributes = USB_ENDPOINT_XFER_BULK,
206}; 206};
207 207
208static struct usb_endpoint_descriptor fs_ecm_out_desc __initdata = { 208static struct usb_endpoint_descriptor fs_ecm_out_desc = {
209 .bLength = USB_DT_ENDPOINT_SIZE, 209 .bLength = USB_DT_ENDPOINT_SIZE,
210 .bDescriptorType = USB_DT_ENDPOINT, 210 .bDescriptorType = USB_DT_ENDPOINT,
211 211
@@ -213,7 +213,7 @@ static struct usb_endpoint_descriptor fs_ecm_out_desc __initdata = {
213 .bmAttributes = USB_ENDPOINT_XFER_BULK, 213 .bmAttributes = USB_ENDPOINT_XFER_BULK,
214}; 214};
215 215
216static struct usb_descriptor_header *ecm_fs_function[] __initdata = { 216static struct usb_descriptor_header *ecm_fs_function[] = {
217 /* CDC ECM control descriptors */ 217 /* CDC ECM control descriptors */
218 (struct usb_descriptor_header *) &ecm_control_intf, 218 (struct usb_descriptor_header *) &ecm_control_intf,
219 (struct usb_descriptor_header *) &ecm_header_desc, 219 (struct usb_descriptor_header *) &ecm_header_desc,
@@ -231,7 +231,7 @@ static struct usb_descriptor_header *ecm_fs_function[] __initdata = {
231 231
232/* high speed support: */ 232/* high speed support: */
233 233
234static struct usb_endpoint_descriptor hs_ecm_notify_desc __initdata = { 234static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
235 .bLength = USB_DT_ENDPOINT_SIZE, 235 .bLength = USB_DT_ENDPOINT_SIZE,
236 .bDescriptorType = USB_DT_ENDPOINT, 236 .bDescriptorType = USB_DT_ENDPOINT,
237 237
@@ -240,7 +240,7 @@ static struct usb_endpoint_descriptor hs_ecm_notify_desc __initdata = {
240 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT), 240 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
241 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, 241 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
242}; 242};
243static struct usb_endpoint_descriptor hs_ecm_in_desc __initdata = { 243static struct usb_endpoint_descriptor hs_ecm_in_desc = {
244 .bLength = USB_DT_ENDPOINT_SIZE, 244 .bLength = USB_DT_ENDPOINT_SIZE,
245 .bDescriptorType = USB_DT_ENDPOINT, 245 .bDescriptorType = USB_DT_ENDPOINT,
246 246
@@ -249,7 +249,7 @@ static struct usb_endpoint_descriptor hs_ecm_in_desc __initdata = {
249 .wMaxPacketSize = cpu_to_le16(512), 249 .wMaxPacketSize = cpu_to_le16(512),
250}; 250};
251 251
252static struct usb_endpoint_descriptor hs_ecm_out_desc __initdata = { 252static struct usb_endpoint_descriptor hs_ecm_out_desc = {
253 .bLength = USB_DT_ENDPOINT_SIZE, 253 .bLength = USB_DT_ENDPOINT_SIZE,
254 .bDescriptorType = USB_DT_ENDPOINT, 254 .bDescriptorType = USB_DT_ENDPOINT,
255 255
@@ -258,7 +258,7 @@ static struct usb_endpoint_descriptor hs_ecm_out_desc __initdata = {
258 .wMaxPacketSize = cpu_to_le16(512), 258 .wMaxPacketSize = cpu_to_le16(512),
259}; 259};
260 260
261static struct usb_descriptor_header *ecm_hs_function[] __initdata = { 261static struct usb_descriptor_header *ecm_hs_function[] = {
262 /* CDC ECM control descriptors */ 262 /* CDC ECM control descriptors */
263 (struct usb_descriptor_header *) &ecm_control_intf, 263 (struct usb_descriptor_header *) &ecm_control_intf,
264 (struct usb_descriptor_header *) &ecm_header_desc, 264 (struct usb_descriptor_header *) &ecm_header_desc,
@@ -597,7 +597,7 @@ static void ecm_close(struct gether *geth)
597 597
598/* ethernet function driver setup/binding */ 598/* ethernet function driver setup/binding */
599 599
600static int __init 600static int
601ecm_bind(struct usb_configuration *c, struct usb_function *f) 601ecm_bind(struct usb_configuration *c, struct usb_function *f)
602{ 602{
603 struct usb_composite_dev *cdev = c->cdev; 603 struct usb_composite_dev *cdev = c->cdev;
@@ -763,7 +763,8 @@ ecm_unbind(struct usb_configuration *c, struct usb_function *f)
763 * Caller must have called @gether_setup(). Caller is also responsible 763 * Caller must have called @gether_setup(). Caller is also responsible
764 * for calling @gether_cleanup() before module unload. 764 * for calling @gether_cleanup() before module unload.
765 */ 765 */
766int __init ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) 766int
767ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
767{ 768{
768 struct f_ecm *ecm; 769 struct f_ecm *ecm;
769 int status; 770 int status;
diff --git a/drivers/usb/gadget/f_fs.c b/drivers/usb/gadget/f_fs.c
new file mode 100644
index 000000000000..d69eccf5f197
--- /dev/null
+++ b/drivers/usb/gadget/f_fs.c
@@ -0,0 +1,2442 @@
1/*
2 * f_fs.c -- user mode filesystem api for usb composite funtcion controllers
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 * Author: Michal Nazarewicz <m.nazarewicz@samsung.com>
6 *
7 * Based on inode.c (GadgetFS):
8 * Copyright (C) 2003-2004 David Brownell
9 * Copyright (C) 2003 Agilent Technologies
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26
27/* #define DEBUG */
28/* #define VERBOSE_DEBUG */
29
30#include <linux/blkdev.h>
31#include <linux/pagemap.h>
32#include <asm/unaligned.h>
33#include <linux/smp_lock.h>
34
35#include <linux/usb/composite.h>
36#include <linux/usb/functionfs.h>
37
38
39#define FUNCTIONFS_MAGIC 0xa647361 /* Chosen by a honest dice roll ;) */
40
41
42/* Debuging *****************************************************************/
43
44#define ffs_printk(level, fmt, args...) printk(level "f_fs: " fmt "\n", ## args)
45
46#define FERR(...) ffs_printk(KERN_ERR, __VA_ARGS__)
47#define FINFO(...) ffs_printk(KERN_INFO, __VA_ARGS__)
48
49#ifdef DEBUG
50# define FDBG(...) ffs_printk(KERN_DEBUG, __VA_ARGS__)
51#else
52# define FDBG(...) do { } while (0)
53#endif /* DEBUG */
54
55#ifdef VERBOSE_DEBUG
56# define FVDBG FDBG
57#else
58# define FVDBG(...) do { } while (0)
59#endif /* VERBOSE_DEBUG */
60
61#define ENTER() FVDBG("%s()", __func__)
62
63#ifdef VERBOSE_DEBUG
64# define ffs_dump_mem(prefix, ptr, len) \
65 print_hex_dump_bytes("f_fs" prefix ": ", DUMP_PREFIX_NONE, ptr, len)
66#else
67# define ffs_dump_mem(prefix, ptr, len) do { } while (0)
68#endif
69
70
71/* The data structure and setup file ****************************************/
72
73enum ffs_state {
74 /* Waiting for descriptors and strings. */
75 /* In this state no open(2), read(2) or write(2) on epfiles
76 * may succeed (which should not be the problem as there
77 * should be no such files opened in the firts place). */
78 FFS_READ_DESCRIPTORS,
79 FFS_READ_STRINGS,
80
81 /* We've got descriptors and strings. We are or have called
82 * functionfs_ready_callback(). functionfs_bind() may have
83 * been called but we don't know. */
84 /* This is the only state in which operations on epfiles may
85 * succeed. */
86 FFS_ACTIVE,
87
88 /* All endpoints have been closed. This state is also set if
89 * we encounter an unrecoverable error. The only
90 * unrecoverable error is situation when after reading strings
91 * from user space we fail to initialise EP files or
92 * functionfs_ready_callback() returns with error (<0). */
93 /* In this state no open(2), read(2) or write(2) (both on ep0
94 * as well as epfile) may succeed (at this point epfiles are
95 * unlinked and all closed so this is not a problem; ep0 is
96 * also closed but ep0 file exists and so open(2) on ep0 must
97 * fail). */
98 FFS_CLOSING
99};
100
101
102enum ffs_setup_state {
103 /* There is no setup request pending. */
104 FFS_NO_SETUP,
105 /* User has read events and there was a setup request event
106 * there. The next read/write on ep0 will handle the
107 * request. */
108 FFS_SETUP_PENDING,
109 /* There was event pending but before user space handled it
110 * some other event was introduced which canceled existing
111 * setup. If this state is set read/write on ep0 return
112 * -EIDRM. This state is only set when adding event. */
113 FFS_SETUP_CANCELED
114};
115
116
117
118struct ffs_epfile;
119struct ffs_function;
120
121struct ffs_data {
122 struct usb_gadget *gadget;
123
124 /* Protect access read/write operations, only one read/write
125 * at a time. As a consequence protects ep0req and company.
126 * While setup request is being processed (queued) this is
127 * held. */
128 struct mutex mutex;
129
130 /* Protect access to enpoint related structures (basically
131 * usb_ep_queue(), usb_ep_dequeue(), etc. calls) except for
132 * endpint zero. */
133 spinlock_t eps_lock;
134
135 /* XXX REVISIT do we need our own request? Since we are not
136 * handling setup requests immidiatelly user space may be so
137 * slow that another setup will be sent to the gadget but this
138 * time not to us but another function and then there could be
139 * a race. Is taht the case? Or maybe we can use cdev->req
140 * after all, maybe we just need some spinlock for that? */
141 struct usb_request *ep0req; /* P: mutex */
142 struct completion ep0req_completion; /* P: mutex */
143 int ep0req_status; /* P: mutex */
144
145 /* reference counter */
146 atomic_t ref;
147 /* how many files are opened (EP0 and others) */
148 atomic_t opened;
149
150 /* EP0 state */
151 enum ffs_state state;
152
153 /*
154 * Possible transations:
155 * + FFS_NO_SETUP -> FFS_SETUP_PENDING -- P: ev.waitq.lock
156 * happens only in ep0 read which is P: mutex
157 * + FFS_SETUP_PENDING -> FFS_NO_SETUP -- P: ev.waitq.lock
158 * happens only in ep0 i/o which is P: mutex
159 * + FFS_SETUP_PENDING -> FFS_SETUP_CANCELED -- P: ev.waitq.lock
160 * + FFS_SETUP_CANCELED -> FFS_NO_SETUP -- cmpxchg
161 */
162 enum ffs_setup_state setup_state;
163
164#define FFS_SETUP_STATE(ffs) \
165 ((enum ffs_setup_state)cmpxchg(&(ffs)->setup_state, \
166 FFS_SETUP_CANCELED, FFS_NO_SETUP))
167
168 /* Events & such. */
169 struct {
170 u8 types[4];
171 unsigned short count;
172 /* XXX REVISIT need to update it in some places, or do we? */
173 unsigned short can_stall;
174 struct usb_ctrlrequest setup;
175
176 wait_queue_head_t waitq;
177 } ev; /* the whole structure, P: ev.waitq.lock */
178
179 /* Flags */
180 unsigned long flags;
181#define FFS_FL_CALL_CLOSED_CALLBACK 0
182#define FFS_FL_BOUND 1
183
184 /* Active function */
185 struct ffs_function *func;
186
187 /* Device name, write once when file system is mounted.
188 * Intendet for user to read if she wants. */
189 const char *dev_name;
190 /* Private data for our user (ie. gadget). Managed by
191 * user. */
192 void *private_data;
193
194 /* filled by __ffs_data_got_descs() */
195 /* real descriptors are 16 bytes after raw_descs (so you need
196 * to skip 16 bytes (ie. ffs->raw_descs + 16) to get to the
197 * first full speed descriptor). raw_descs_length and
198 * raw_fs_descs_length do not have those 16 bytes added. */
199 const void *raw_descs;
200 unsigned raw_descs_length;
201 unsigned raw_fs_descs_length;
202 unsigned fs_descs_count;
203 unsigned hs_descs_count;
204
205 unsigned short strings_count;
206 unsigned short interfaces_count;
207 unsigned short eps_count;
208 unsigned short _pad1;
209
210 /* filled by __ffs_data_got_strings() */
211 /* ids in stringtabs are set in functionfs_bind() */
212 const void *raw_strings;
213 struct usb_gadget_strings **stringtabs;
214
215 /* File system's super block, write once when file system is mounted. */
216 struct super_block *sb;
217
218 /* File permissions, written once when fs is mounted*/
219 struct ffs_file_perms {
220 umode_t mode;
221 uid_t uid;
222 gid_t gid;
223 } file_perms;
224
225 /* The endpoint files, filled by ffs_epfiles_create(),
226 * destroyed by ffs_epfiles_destroy(). */
227 struct ffs_epfile *epfiles;
228};
229
230/* Reference counter handling */
231static void ffs_data_get(struct ffs_data *ffs);
232static void ffs_data_put(struct ffs_data *ffs);
233/* Creates new ffs_data object. */
234static struct ffs_data *__must_check ffs_data_new(void) __attribute__((malloc));
235
236/* Opened counter handling. */
237static void ffs_data_opened(struct ffs_data *ffs);
238static void ffs_data_closed(struct ffs_data *ffs);
239
240/* Called with ffs->mutex held; take over ownerrship of data. */
241static int __must_check
242__ffs_data_got_descs(struct ffs_data *ffs, char *data, size_t len);
243static int __must_check
244__ffs_data_got_strings(struct ffs_data *ffs, char *data, size_t len);
245
246
247/* The function structure ***************************************************/
248
249struct ffs_ep;
250
251struct ffs_function {
252 struct usb_configuration *conf;
253 struct usb_gadget *gadget;
254 struct ffs_data *ffs;
255
256 struct ffs_ep *eps;
257 u8 eps_revmap[16];
258 short *interfaces_nums;
259
260 struct usb_function function;
261};
262
263
264static struct ffs_function *ffs_func_from_usb(struct usb_function *f)
265{
266 return container_of(f, struct ffs_function, function);
267}
268
269static void ffs_func_free(struct ffs_function *func);
270
271
272static void ffs_func_eps_disable(struct ffs_function *func);
273static int __must_check ffs_func_eps_enable(struct ffs_function *func);
274
275
276static int ffs_func_bind(struct usb_configuration *,
277 struct usb_function *);
278static void ffs_func_unbind(struct usb_configuration *,
279 struct usb_function *);
280static int ffs_func_set_alt(struct usb_function *, unsigned, unsigned);
281static void ffs_func_disable(struct usb_function *);
282static int ffs_func_setup(struct usb_function *,
283 const struct usb_ctrlrequest *);
284static void ffs_func_suspend(struct usb_function *);
285static void ffs_func_resume(struct usb_function *);
286
287
288static int ffs_func_revmap_ep(struct ffs_function *func, u8 num);
289static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf);
290
291
292
293/* The endpoints structures *************************************************/
294
295struct ffs_ep {
296 struct usb_ep *ep; /* P: ffs->eps_lock */
297 struct usb_request *req; /* P: epfile->mutex */
298
299 /* [0]: full speed, [1]: high speed */
300 struct usb_endpoint_descriptor *descs[2];
301
302 u8 num;
303
304 int status; /* P: epfile->mutex */
305};
306
307struct ffs_epfile {
308 /* Protects ep->ep and ep->req. */
309 struct mutex mutex;
310 wait_queue_head_t wait;
311
312 struct ffs_data *ffs;
313 struct ffs_ep *ep; /* P: ffs->eps_lock */
314
315 struct dentry *dentry;
316
317 char name[5];
318
319 unsigned char in; /* P: ffs->eps_lock */
320 unsigned char isoc; /* P: ffs->eps_lock */
321
322 unsigned char _pad;
323};
324
325
326static int __must_check ffs_epfiles_create(struct ffs_data *ffs);
327static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count);
328
329static struct inode *__must_check
330ffs_sb_create_file(struct super_block *sb, const char *name, void *data,
331 const struct file_operations *fops,
332 struct dentry **dentry_p);
333
334
335/* Misc helper functions ****************************************************/
336
337static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
338 __attribute__((warn_unused_result, nonnull));
339static char *ffs_prepare_buffer(const char * __user buf, size_t len)
340 __attribute__((warn_unused_result, nonnull));
341
342
343/* Control file aka ep0 *****************************************************/
344
345static void ffs_ep0_complete(struct usb_ep *ep, struct usb_request *req)
346{
347 struct ffs_data *ffs = req->context;
348
349 complete_all(&ffs->ep0req_completion);
350}
351
352
353static int __ffs_ep0_queue_wait(struct ffs_data *ffs, char *data, size_t len)
354{
355 struct usb_request *req = ffs->ep0req;
356 int ret;
357
358 req->zero = len < le16_to_cpu(ffs->ev.setup.wLength);
359
360 spin_unlock_irq(&ffs->ev.waitq.lock);
361
362 req->buf = data;
363 req->length = len;
364
365 INIT_COMPLETION(ffs->ep0req_completion);
366
367 ret = usb_ep_queue(ffs->gadget->ep0, req, GFP_ATOMIC);
368 if (unlikely(ret < 0))
369 return ret;
370
371 ret = wait_for_completion_interruptible(&ffs->ep0req_completion);
372 if (unlikely(ret)) {
373 usb_ep_dequeue(ffs->gadget->ep0, req);
374 return -EINTR;
375 }
376
377 ffs->setup_state = FFS_NO_SETUP;
378 return ffs->ep0req_status;
379}
380
381static int __ffs_ep0_stall(struct ffs_data *ffs)
382{
383 if (ffs->ev.can_stall) {
384 FVDBG("ep0 stall\n");
385 usb_ep_set_halt(ffs->gadget->ep0);
386 ffs->setup_state = FFS_NO_SETUP;
387 return -EL2HLT;
388 } else {
389 FDBG("bogus ep0 stall!\n");
390 return -ESRCH;
391 }
392}
393
394
395static ssize_t ffs_ep0_write(struct file *file, const char __user *buf,
396 size_t len, loff_t *ptr)
397{
398 struct ffs_data *ffs = file->private_data;
399 ssize_t ret;
400 char *data;
401
402 ENTER();
403
404 /* Fast check if setup was canceled */
405 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
406 return -EIDRM;
407
408 /* Acquire mutex */
409 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
410 if (unlikely(ret < 0))
411 return ret;
412
413
414 /* Check state */
415 switch (ffs->state) {
416 case FFS_READ_DESCRIPTORS:
417 case FFS_READ_STRINGS:
418 /* Copy data */
419 if (unlikely(len < 16)) {
420 ret = -EINVAL;
421 break;
422 }
423
424 data = ffs_prepare_buffer(buf, len);
425 if (unlikely(IS_ERR(data))) {
426 ret = PTR_ERR(data);
427 break;
428 }
429
430 /* Handle data */
431 if (ffs->state == FFS_READ_DESCRIPTORS) {
432 FINFO("read descriptors");
433 ret = __ffs_data_got_descs(ffs, data, len);
434 if (unlikely(ret < 0))
435 break;
436
437 ffs->state = FFS_READ_STRINGS;
438 ret = len;
439 } else {
440 FINFO("read strings");
441 ret = __ffs_data_got_strings(ffs, data, len);
442 if (unlikely(ret < 0))
443 break;
444
445 ret = ffs_epfiles_create(ffs);
446 if (unlikely(ret)) {
447 ffs->state = FFS_CLOSING;
448 break;
449 }
450
451 ffs->state = FFS_ACTIVE;
452 mutex_unlock(&ffs->mutex);
453
454 ret = functionfs_ready_callback(ffs);
455 if (unlikely(ret < 0)) {
456 ffs->state = FFS_CLOSING;
457 return ret;
458 }
459
460 set_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags);
461 return len;
462 }
463 break;
464
465
466 case FFS_ACTIVE:
467 data = NULL;
468 /* We're called from user space, we can use _irq
469 * rather then _irqsave */
470 spin_lock_irq(&ffs->ev.waitq.lock);
471 switch (FFS_SETUP_STATE(ffs)) {
472 case FFS_SETUP_CANCELED:
473 ret = -EIDRM;
474 goto done_spin;
475
476 case FFS_NO_SETUP:
477 ret = -ESRCH;
478 goto done_spin;
479
480 case FFS_SETUP_PENDING:
481 break;
482 }
483
484 /* FFS_SETUP_PENDING */
485 if (!(ffs->ev.setup.bRequestType & USB_DIR_IN)) {
486 spin_unlock_irq(&ffs->ev.waitq.lock);
487 ret = __ffs_ep0_stall(ffs);
488 break;
489 }
490
491 /* FFS_SETUP_PENDING and not stall */
492 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
493
494 spin_unlock_irq(&ffs->ev.waitq.lock);
495
496 data = ffs_prepare_buffer(buf, len);
497 if (unlikely(IS_ERR(data))) {
498 ret = PTR_ERR(data);
499 break;
500 }
501
502 spin_lock_irq(&ffs->ev.waitq.lock);
503
504 /* We are guaranteed to be still in FFS_ACTIVE state
505 * but the state of setup could have changed from
506 * FFS_SETUP_PENDING to FFS_SETUP_CANCELED so we need
507 * to check for that. If that happened we copied data
508 * from user space in vain but it's unlikely. */
509 /* For sure we are not in FFS_NO_SETUP since this is
510 * the only place FFS_SETUP_PENDING -> FFS_NO_SETUP
511 * transition can be performed and it's protected by
512 * mutex. */
513
514 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
515 ret = -EIDRM;
516done_spin:
517 spin_unlock_irq(&ffs->ev.waitq.lock);
518 } else {
519 /* unlocks spinlock */
520 ret = __ffs_ep0_queue_wait(ffs, data, len);
521 }
522 kfree(data);
523 break;
524
525
526 default:
527 ret = -EBADFD;
528 break;
529 }
530
531
532 mutex_unlock(&ffs->mutex);
533 return ret;
534}
535
536
537
538static ssize_t __ffs_ep0_read_events(struct ffs_data *ffs, char __user *buf,
539 size_t n)
540{
541 /* We are holding ffs->ev.waitq.lock and ffs->mutex and we need
542 * to release them. */
543
544 struct usb_functionfs_event events[n];
545 unsigned i = 0;
546
547 memset(events, 0, sizeof events);
548
549 do {
550 events[i].type = ffs->ev.types[i];
551 if (events[i].type == FUNCTIONFS_SETUP) {
552 events[i].u.setup = ffs->ev.setup;
553 ffs->setup_state = FFS_SETUP_PENDING;
554 }
555 } while (++i < n);
556
557 if (n < ffs->ev.count) {
558 ffs->ev.count -= n;
559 memmove(ffs->ev.types, ffs->ev.types + n,
560 ffs->ev.count * sizeof *ffs->ev.types);
561 } else {
562 ffs->ev.count = 0;
563 }
564
565 spin_unlock_irq(&ffs->ev.waitq.lock);
566 mutex_unlock(&ffs->mutex);
567
568 return unlikely(__copy_to_user(buf, events, sizeof events))
569 ? -EFAULT : sizeof events;
570}
571
572
573static ssize_t ffs_ep0_read(struct file *file, char __user *buf,
574 size_t len, loff_t *ptr)
575{
576 struct ffs_data *ffs = file->private_data;
577 char *data = NULL;
578 size_t n;
579 int ret;
580
581 ENTER();
582
583 /* Fast check if setup was canceled */
584 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED)
585 return -EIDRM;
586
587 /* Acquire mutex */
588 ret = ffs_mutex_lock(&ffs->mutex, file->f_flags & O_NONBLOCK);
589 if (unlikely(ret < 0))
590 return ret;
591
592
593 /* Check state */
594 if (ffs->state != FFS_ACTIVE) {
595 ret = -EBADFD;
596 goto done_mutex;
597 }
598
599
600 /* We're called from user space, we can use _irq rather then
601 * _irqsave */
602 spin_lock_irq(&ffs->ev.waitq.lock);
603
604 switch (FFS_SETUP_STATE(ffs)) {
605 case FFS_SETUP_CANCELED:
606 ret = -EIDRM;
607 break;
608
609 case FFS_NO_SETUP:
610 n = len / sizeof(struct usb_functionfs_event);
611 if (unlikely(!n)) {
612 ret = -EINVAL;
613 break;
614 }
615
616 if ((file->f_flags & O_NONBLOCK) && !ffs->ev.count) {
617 ret = -EAGAIN;
618 break;
619 }
620
621 if (unlikely(wait_event_interruptible_exclusive_locked_irq(ffs->ev.waitq, ffs->ev.count))) {
622 ret = -EINTR;
623 break;
624 }
625
626 return __ffs_ep0_read_events(ffs, buf,
627 min(n, (size_t)ffs->ev.count));
628
629
630 case FFS_SETUP_PENDING:
631 if (ffs->ev.setup.bRequestType & USB_DIR_IN) {
632 spin_unlock_irq(&ffs->ev.waitq.lock);
633 ret = __ffs_ep0_stall(ffs);
634 goto done_mutex;
635 }
636
637 len = min(len, (size_t)le16_to_cpu(ffs->ev.setup.wLength));
638
639 spin_unlock_irq(&ffs->ev.waitq.lock);
640
641 if (likely(len)) {
642 data = kmalloc(len, GFP_KERNEL);
643 if (unlikely(!data)) {
644 ret = -ENOMEM;
645 goto done_mutex;
646 }
647 }
648
649 spin_lock_irq(&ffs->ev.waitq.lock);
650
651 /* See ffs_ep0_write() */
652 if (FFS_SETUP_STATE(ffs) == FFS_SETUP_CANCELED) {
653 ret = -EIDRM;
654 break;
655 }
656
657 /* unlocks spinlock */
658 ret = __ffs_ep0_queue_wait(ffs, data, len);
659 if (likely(ret > 0) && unlikely(__copy_to_user(buf, data, len)))
660 ret = -EFAULT;
661 goto done_mutex;
662
663 default:
664 ret = -EBADFD;
665 break;
666 }
667
668 spin_unlock_irq(&ffs->ev.waitq.lock);
669done_mutex:
670 mutex_unlock(&ffs->mutex);
671 kfree(data);
672 return ret;
673}
674
675
676
677static int ffs_ep0_open(struct inode *inode, struct file *file)
678{
679 struct ffs_data *ffs = inode->i_private;
680
681 ENTER();
682
683 if (unlikely(ffs->state == FFS_CLOSING))
684 return -EBUSY;
685
686 file->private_data = ffs;
687 ffs_data_opened(ffs);
688
689 return 0;
690}
691
692
693static int ffs_ep0_release(struct inode *inode, struct file *file)
694{
695 struct ffs_data *ffs = file->private_data;
696
697 ENTER();
698
699 ffs_data_closed(ffs);
700
701 return 0;
702}
703
704
705static long ffs_ep0_ioctl(struct file *file, unsigned code, unsigned long value)
706{
707 struct ffs_data *ffs = file->private_data;
708 struct usb_gadget *gadget = ffs->gadget;
709 long ret;
710
711 ENTER();
712
713 if (code == FUNCTIONFS_INTERFACE_REVMAP) {
714 struct ffs_function *func = ffs->func;
715 ret = func ? ffs_func_revmap_intf(func, value) : -ENODEV;
716 } else if (gadget->ops->ioctl) {
717 lock_kernel();
718 ret = gadget->ops->ioctl(gadget, code, value);
719 unlock_kernel();
720 } else {
721 ret = -ENOTTY;
722 }
723
724 return ret;
725}
726
727
728static const struct file_operations ffs_ep0_operations = {
729 .owner = THIS_MODULE,
730 .llseek = no_llseek,
731
732 .open = ffs_ep0_open,
733 .write = ffs_ep0_write,
734 .read = ffs_ep0_read,
735 .release = ffs_ep0_release,
736 .unlocked_ioctl = ffs_ep0_ioctl,
737};
738
739
740/* "Normal" endpoints operations ********************************************/
741
742
743static void ffs_epfile_io_complete(struct usb_ep *_ep, struct usb_request *req)
744{
745 ENTER();
746 if (likely(req->context)) {
747 struct ffs_ep *ep = _ep->driver_data;
748 ep->status = req->status ? req->status : req->actual;
749 complete(req->context);
750 }
751}
752
753
754static ssize_t ffs_epfile_io(struct file *file,
755 char __user *buf, size_t len, int read)
756{
757 struct ffs_epfile *epfile = file->private_data;
758 struct ffs_ep *ep;
759 char *data = NULL;
760 ssize_t ret;
761 int halt;
762
763 goto first_try;
764 do {
765 spin_unlock_irq(&epfile->ffs->eps_lock);
766 mutex_unlock(&epfile->mutex);
767
768first_try:
769 /* Are we still active? */
770 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE)) {
771 ret = -ENODEV;
772 goto error;
773 }
774
775 /* Wait for endpoint to be enabled */
776 ep = epfile->ep;
777 if (!ep) {
778 if (file->f_flags & O_NONBLOCK) {
779 ret = -EAGAIN;
780 goto error;
781 }
782
783 if (unlikely(wait_event_interruptible
784 (epfile->wait, (ep = epfile->ep)))) {
785 ret = -EINTR;
786 goto error;
787 }
788 }
789
790 /* Do we halt? */
791 halt = !read == !epfile->in;
792 if (halt && epfile->isoc) {
793 ret = -EINVAL;
794 goto error;
795 }
796
797 /* Allocate & copy */
798 if (!halt && !data) {
799 data = kzalloc(len, GFP_KERNEL);
800 if (unlikely(!data))
801 return -ENOMEM;
802
803 if (!read &&
804 unlikely(__copy_from_user(data, buf, len))) {
805 ret = -EFAULT;
806 goto error;
807 }
808 }
809
810 /* We will be using request */
811 ret = ffs_mutex_lock(&epfile->mutex,
812 file->f_flags & O_NONBLOCK);
813 if (unlikely(ret))
814 goto error;
815
816 /* We're called from user space, we can use _irq rather then
817 * _irqsave */
818 spin_lock_irq(&epfile->ffs->eps_lock);
819
820 /* While we were acquiring mutex endpoint got disabled
821 * or changed? */
822 } while (unlikely(epfile->ep != ep));
823
824 /* Halt */
825 if (unlikely(halt)) {
826 if (likely(epfile->ep == ep) && !WARN_ON(!ep->ep))
827 usb_ep_set_halt(ep->ep);
828 spin_unlock_irq(&epfile->ffs->eps_lock);
829 ret = -EBADMSG;
830 } else {
831 /* Fire the request */
832 DECLARE_COMPLETION_ONSTACK(done);
833
834 struct usb_request *req = ep->req;
835 req->context = &done;
836 req->complete = ffs_epfile_io_complete;
837 req->buf = data;
838 req->length = len;
839
840 ret = usb_ep_queue(ep->ep, req, GFP_ATOMIC);
841
842 spin_unlock_irq(&epfile->ffs->eps_lock);
843
844 if (unlikely(ret < 0)) {
845 /* nop */
846 } else if (unlikely(wait_for_completion_interruptible(&done))) {
847 ret = -EINTR;
848 usb_ep_dequeue(ep->ep, req);
849 } else {
850 ret = ep->status;
851 if (read && ret > 0 &&
852 unlikely(copy_to_user(buf, data, ret)))
853 ret = -EFAULT;
854 }
855 }
856
857 mutex_unlock(&epfile->mutex);
858error:
859 kfree(data);
860 return ret;
861}
862
863
864static ssize_t
865ffs_epfile_write(struct file *file, const char __user *buf, size_t len,
866 loff_t *ptr)
867{
868 ENTER();
869
870 return ffs_epfile_io(file, (char __user *)buf, len, 0);
871}
872
873static ssize_t
874ffs_epfile_read(struct file *file, char __user *buf, size_t len, loff_t *ptr)
875{
876 ENTER();
877
878 return ffs_epfile_io(file, buf, len, 1);
879}
880
881static int
882ffs_epfile_open(struct inode *inode, struct file *file)
883{
884 struct ffs_epfile *epfile = inode->i_private;
885
886 ENTER();
887
888 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
889 return -ENODEV;
890
891 file->private_data = epfile;
892 ffs_data_opened(epfile->ffs);
893
894 return 0;
895}
896
897static int
898ffs_epfile_release(struct inode *inode, struct file *file)
899{
900 struct ffs_epfile *epfile = inode->i_private;
901
902 ENTER();
903
904 ffs_data_closed(epfile->ffs);
905
906 return 0;
907}
908
909
910static long ffs_epfile_ioctl(struct file *file, unsigned code,
911 unsigned long value)
912{
913 struct ffs_epfile *epfile = file->private_data;
914 int ret;
915
916 ENTER();
917
918 if (WARN_ON(epfile->ffs->state != FFS_ACTIVE))
919 return -ENODEV;
920
921 spin_lock_irq(&epfile->ffs->eps_lock);
922 if (likely(epfile->ep)) {
923 switch (code) {
924 case FUNCTIONFS_FIFO_STATUS:
925 ret = usb_ep_fifo_status(epfile->ep->ep);
926 break;
927 case FUNCTIONFS_FIFO_FLUSH:
928 usb_ep_fifo_flush(epfile->ep->ep);
929 ret = 0;
930 break;
931 case FUNCTIONFS_CLEAR_HALT:
932 ret = usb_ep_clear_halt(epfile->ep->ep);
933 break;
934 case FUNCTIONFS_ENDPOINT_REVMAP:
935 ret = epfile->ep->num;
936 break;
937 default:
938 ret = -ENOTTY;
939 }
940 } else {
941 ret = -ENODEV;
942 }
943 spin_unlock_irq(&epfile->ffs->eps_lock);
944
945 return ret;
946}
947
948
949static const struct file_operations ffs_epfile_operations = {
950 .owner = THIS_MODULE,
951 .llseek = no_llseek,
952
953 .open = ffs_epfile_open,
954 .write = ffs_epfile_write,
955 .read = ffs_epfile_read,
956 .release = ffs_epfile_release,
957 .unlocked_ioctl = ffs_epfile_ioctl,
958};
959
960
961
962/* File system and super block operations ***********************************/
963
964/*
965 * Mounting the filesystem creates a controller file, used first for
966 * function configuration then later for event monitoring.
967 */
968
969
970static struct inode *__must_check
971ffs_sb_make_inode(struct super_block *sb, void *data,
972 const struct file_operations *fops,
973 const struct inode_operations *iops,
974 struct ffs_file_perms *perms)
975{
976 struct inode *inode;
977
978 ENTER();
979
980 inode = new_inode(sb);
981
982 if (likely(inode)) {
983 struct timespec current_time = CURRENT_TIME;
984
985 inode->i_mode = perms->mode;
986 inode->i_uid = perms->uid;
987 inode->i_gid = perms->gid;
988 inode->i_atime = current_time;
989 inode->i_mtime = current_time;
990 inode->i_ctime = current_time;
991 inode->i_private = data;
992 if (fops)
993 inode->i_fop = fops;
994 if (iops)
995 inode->i_op = iops;
996 }
997
998 return inode;
999}
1000
1001
1002/* Create "regular" file */
1003
1004static struct inode *ffs_sb_create_file(struct super_block *sb,
1005 const char *name, void *data,
1006 const struct file_operations *fops,
1007 struct dentry **dentry_p)
1008{
1009 struct ffs_data *ffs = sb->s_fs_info;
1010 struct dentry *dentry;
1011 struct inode *inode;
1012
1013 ENTER();
1014
1015 dentry = d_alloc_name(sb->s_root, name);
1016 if (unlikely(!dentry))
1017 return NULL;
1018
1019 inode = ffs_sb_make_inode(sb, data, fops, NULL, &ffs->file_perms);
1020 if (unlikely(!inode)) {
1021 dput(dentry);
1022 return NULL;
1023 }
1024
1025 d_add(dentry, inode);
1026 if (dentry_p)
1027 *dentry_p = dentry;
1028
1029 return inode;
1030}
1031
1032
1033/* Super block */
1034
1035static const struct super_operations ffs_sb_operations = {
1036 .statfs = simple_statfs,
1037 .drop_inode = generic_delete_inode,
1038};
1039
1040struct ffs_sb_fill_data {
1041 struct ffs_file_perms perms;
1042 umode_t root_mode;
1043 const char *dev_name;
1044};
1045
1046static int ffs_sb_fill(struct super_block *sb, void *_data, int silent)
1047{
1048 struct ffs_sb_fill_data *data = _data;
1049 struct inode *inode;
1050 struct dentry *d;
1051 struct ffs_data *ffs;
1052
1053 ENTER();
1054
1055 /* Initialize data */
1056 ffs = ffs_data_new();
1057 if (unlikely(!ffs))
1058 goto enomem0;
1059
1060 ffs->sb = sb;
1061 ffs->dev_name = data->dev_name;
1062 ffs->file_perms = data->perms;
1063
1064 sb->s_fs_info = ffs;
1065 sb->s_blocksize = PAGE_CACHE_SIZE;
1066 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1067 sb->s_magic = FUNCTIONFS_MAGIC;
1068 sb->s_op = &ffs_sb_operations;
1069 sb->s_time_gran = 1;
1070
1071 /* Root inode */
1072 data->perms.mode = data->root_mode;
1073 inode = ffs_sb_make_inode(sb, NULL,
1074 &simple_dir_operations,
1075 &simple_dir_inode_operations,
1076 &data->perms);
1077 if (unlikely(!inode))
1078 goto enomem1;
1079 d = d_alloc_root(inode);
1080 if (unlikely(!d))
1081 goto enomem2;
1082 sb->s_root = d;
1083
1084 /* EP0 file */
1085 if (unlikely(!ffs_sb_create_file(sb, "ep0", ffs,
1086 &ffs_ep0_operations, NULL)))
1087 goto enomem3;
1088
1089 return 0;
1090
1091enomem3:
1092 dput(d);
1093enomem2:
1094 iput(inode);
1095enomem1:
1096 ffs_data_put(ffs);
1097enomem0:
1098 return -ENOMEM;
1099}
1100
1101
1102static int ffs_fs_parse_opts(struct ffs_sb_fill_data *data, char *opts)
1103{
1104 ENTER();
1105
1106 if (!opts || !*opts)
1107 return 0;
1108
1109 for (;;) {
1110 char *end, *eq, *comma;
1111 unsigned long value;
1112
1113 /* Option limit */
1114 comma = strchr(opts, ',');
1115 if (comma)
1116 *comma = 0;
1117
1118 /* Value limit */
1119 eq = strchr(opts, '=');
1120 if (unlikely(!eq)) {
1121 FERR("'=' missing in %s", opts);
1122 return -EINVAL;
1123 }
1124 *eq = 0;
1125
1126 /* Parse value */
1127 value = simple_strtoul(eq + 1, &end, 0);
1128 if (unlikely(*end != ',' && *end != 0)) {
1129 FERR("%s: invalid value: %s", opts, eq + 1);
1130 return -EINVAL;
1131 }
1132
1133 /* Interpret option */
1134 switch (eq - opts) {
1135 case 5:
1136 if (!memcmp(opts, "rmode", 5))
1137 data->root_mode = (value & 0555) | S_IFDIR;
1138 else if (!memcmp(opts, "fmode", 5))
1139 data->perms.mode = (value & 0666) | S_IFREG;
1140 else
1141 goto invalid;
1142 break;
1143
1144 case 4:
1145 if (!memcmp(opts, "mode", 4)) {
1146 data->root_mode = (value & 0555) | S_IFDIR;
1147 data->perms.mode = (value & 0666) | S_IFREG;
1148 } else {
1149 goto invalid;
1150 }
1151 break;
1152
1153 case 3:
1154 if (!memcmp(opts, "uid", 3))
1155 data->perms.uid = value;
1156 else if (!memcmp(opts, "gid", 3))
1157 data->perms.gid = value;
1158 else
1159 goto invalid;
1160 break;
1161
1162 default:
1163invalid:
1164 FERR("%s: invalid option", opts);
1165 return -EINVAL;
1166 }
1167
1168 /* Next iteration */
1169 if (!comma)
1170 break;
1171 opts = comma + 1;
1172 }
1173
1174 return 0;
1175}
1176
1177
1178/* "mount -t functionfs dev_name /dev/function" ends up here */
1179
1180static int
1181ffs_fs_get_sb(struct file_system_type *t, int flags,
1182 const char *dev_name, void *opts, struct vfsmount *mnt)
1183{
1184 struct ffs_sb_fill_data data = {
1185 .perms = {
1186 .mode = S_IFREG | 0600,
1187 .uid = 0,
1188 .gid = 0
1189 },
1190 .root_mode = S_IFDIR | 0500,
1191 };
1192 int ret;
1193
1194 ENTER();
1195
1196 ret = functionfs_check_dev_callback(dev_name);
1197 if (unlikely(ret < 0))
1198 return ret;
1199
1200 ret = ffs_fs_parse_opts(&data, opts);
1201 if (unlikely(ret < 0))
1202 return ret;
1203
1204 data.dev_name = dev_name;
1205 return get_sb_single(t, flags, &data, ffs_sb_fill, mnt);
1206}
1207
1208static void
1209ffs_fs_kill_sb(struct super_block *sb)
1210{
1211 void *ptr;
1212
1213 ENTER();
1214
1215 kill_litter_super(sb);
1216 ptr = xchg(&sb->s_fs_info, NULL);
1217 if (ptr)
1218 ffs_data_put(ptr);
1219}
1220
1221static struct file_system_type ffs_fs_type = {
1222 .owner = THIS_MODULE,
1223 .name = "functionfs",
1224 .get_sb = ffs_fs_get_sb,
1225 .kill_sb = ffs_fs_kill_sb,
1226};
1227
1228
1229
1230/* Driver's main init/cleanup functions *************************************/
1231
1232
1233static int functionfs_init(void)
1234{
1235 int ret;
1236
1237 ENTER();
1238
1239 ret = register_filesystem(&ffs_fs_type);
1240 if (likely(!ret))
1241 FINFO("file system registered");
1242 else
1243 FERR("failed registering file system (%d)", ret);
1244
1245 return ret;
1246}
1247
1248static void functionfs_cleanup(void)
1249{
1250 ENTER();
1251
1252 FINFO("unloading");
1253 unregister_filesystem(&ffs_fs_type);
1254}
1255
1256
1257
1258/* ffs_data and ffs_function construction and destruction code **************/
1259
1260static void ffs_data_clear(struct ffs_data *ffs);
1261static void ffs_data_reset(struct ffs_data *ffs);
1262
1263
1264static void ffs_data_get(struct ffs_data *ffs)
1265{
1266 ENTER();
1267
1268 atomic_inc(&ffs->ref);
1269}
1270
1271static void ffs_data_opened(struct ffs_data *ffs)
1272{
1273 ENTER();
1274
1275 atomic_inc(&ffs->ref);
1276 atomic_inc(&ffs->opened);
1277}
1278
1279static void ffs_data_put(struct ffs_data *ffs)
1280{
1281 ENTER();
1282
1283 if (unlikely(atomic_dec_and_test(&ffs->ref))) {
1284 FINFO("%s(): freeing", __func__);
1285 ffs_data_clear(ffs);
1286 BUG_ON(mutex_is_locked(&ffs->mutex) ||
1287 spin_is_locked(&ffs->ev.waitq.lock) ||
1288 waitqueue_active(&ffs->ev.waitq) ||
1289 waitqueue_active(&ffs->ep0req_completion.wait));
1290 kfree(ffs);
1291 }
1292}
1293
1294
1295
1296static void ffs_data_closed(struct ffs_data *ffs)
1297{
1298 ENTER();
1299
1300 if (atomic_dec_and_test(&ffs->opened)) {
1301 ffs->state = FFS_CLOSING;
1302 ffs_data_reset(ffs);
1303 }
1304
1305 ffs_data_put(ffs);
1306}
1307
1308
1309static struct ffs_data *ffs_data_new(void)
1310{
1311 struct ffs_data *ffs = kzalloc(sizeof *ffs, GFP_KERNEL);
1312 if (unlikely(!ffs))
1313 return 0;
1314
1315 ENTER();
1316
1317 atomic_set(&ffs->ref, 1);
1318 atomic_set(&ffs->opened, 0);
1319 ffs->state = FFS_READ_DESCRIPTORS;
1320 mutex_init(&ffs->mutex);
1321 spin_lock_init(&ffs->eps_lock);
1322 init_waitqueue_head(&ffs->ev.waitq);
1323 init_completion(&ffs->ep0req_completion);
1324
1325 /* XXX REVISIT need to update it in some places, or do we? */
1326 ffs->ev.can_stall = 1;
1327
1328 return ffs;
1329}
1330
1331
1332static void ffs_data_clear(struct ffs_data *ffs)
1333{
1334 ENTER();
1335
1336 if (test_and_clear_bit(FFS_FL_CALL_CLOSED_CALLBACK, &ffs->flags))
1337 functionfs_closed_callback(ffs);
1338
1339 BUG_ON(ffs->gadget);
1340
1341 if (ffs->epfiles)
1342 ffs_epfiles_destroy(ffs->epfiles, ffs->eps_count);
1343
1344 kfree(ffs->raw_descs);
1345 kfree(ffs->raw_strings);
1346 kfree(ffs->stringtabs);
1347}
1348
1349
1350static void ffs_data_reset(struct ffs_data *ffs)
1351{
1352 ENTER();
1353
1354 ffs_data_clear(ffs);
1355
1356 ffs->epfiles = NULL;
1357 ffs->raw_descs = NULL;
1358 ffs->raw_strings = NULL;
1359 ffs->stringtabs = NULL;
1360
1361 ffs->raw_descs_length = 0;
1362 ffs->raw_fs_descs_length = 0;
1363 ffs->fs_descs_count = 0;
1364 ffs->hs_descs_count = 0;
1365
1366 ffs->strings_count = 0;
1367 ffs->interfaces_count = 0;
1368 ffs->eps_count = 0;
1369
1370 ffs->ev.count = 0;
1371
1372 ffs->state = FFS_READ_DESCRIPTORS;
1373 ffs->setup_state = FFS_NO_SETUP;
1374 ffs->flags = 0;
1375}
1376
1377
1378static int functionfs_bind(struct ffs_data *ffs, struct usb_composite_dev *cdev)
1379{
1380 unsigned i, count;
1381
1382 ENTER();
1383
1384 if (WARN_ON(ffs->state != FFS_ACTIVE
1385 || test_and_set_bit(FFS_FL_BOUND, &ffs->flags)))
1386 return -EBADFD;
1387
1388 ffs_data_get(ffs);
1389
1390 ffs->ep0req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
1391 if (unlikely(!ffs->ep0req))
1392 return -ENOMEM;
1393 ffs->ep0req->complete = ffs_ep0_complete;
1394 ffs->ep0req->context = ffs;
1395
1396 /* Get strings identifiers */
1397 for (count = ffs->strings_count, i = 0; i < count; ++i) {
1398 struct usb_gadget_strings **lang;
1399
1400 int id = usb_string_id(cdev);
1401 if (unlikely(id < 0)) {
1402 usb_ep_free_request(cdev->gadget->ep0, ffs->ep0req);
1403 ffs->ep0req = NULL;
1404 return id;
1405 }
1406
1407 lang = ffs->stringtabs;
1408 do {
1409 (*lang)->strings[i].id = id;
1410 ++lang;
1411 } while (*lang);
1412 }
1413
1414 ffs->gadget = cdev->gadget;
1415 return 0;
1416}
1417
1418
1419static void functionfs_unbind(struct ffs_data *ffs)
1420{
1421 ENTER();
1422
1423 if (!WARN_ON(!ffs->gadget)) {
1424 usb_ep_free_request(ffs->gadget->ep0, ffs->ep0req);
1425 ffs->ep0req = NULL;
1426 ffs->gadget = NULL;
1427 ffs_data_put(ffs);
1428 }
1429}
1430
1431
1432static int ffs_epfiles_create(struct ffs_data *ffs)
1433{
1434 struct ffs_epfile *epfile, *epfiles;
1435 unsigned i, count;
1436
1437 ENTER();
1438
1439 count = ffs->eps_count;
1440 epfiles = kzalloc(count * sizeof *epfiles, GFP_KERNEL);
1441 if (!epfiles)
1442 return -ENOMEM;
1443
1444 epfile = epfiles;
1445 for (i = 1; i <= count; ++i, ++epfile) {
1446 epfile->ffs = ffs;
1447 mutex_init(&epfile->mutex);
1448 init_waitqueue_head(&epfile->wait);
1449 sprintf(epfiles->name, "ep%u", i);
1450 if (!unlikely(ffs_sb_create_file(ffs->sb, epfiles->name, epfile,
1451 &ffs_epfile_operations,
1452 &epfile->dentry))) {
1453 ffs_epfiles_destroy(epfiles, i - 1);
1454 return -ENOMEM;
1455 }
1456 }
1457
1458 ffs->epfiles = epfiles;
1459 return 0;
1460}
1461
1462
1463static void ffs_epfiles_destroy(struct ffs_epfile *epfiles, unsigned count)
1464{
1465 struct ffs_epfile *epfile = epfiles;
1466
1467 ENTER();
1468
1469 for (; count; --count, ++epfile) {
1470 BUG_ON(mutex_is_locked(&epfile->mutex) ||
1471 waitqueue_active(&epfile->wait));
1472 if (epfile->dentry) {
1473 d_delete(epfile->dentry);
1474 dput(epfile->dentry);
1475 epfile->dentry = NULL;
1476 }
1477 }
1478
1479 kfree(epfiles);
1480}
1481
1482
1483static int functionfs_add(struct usb_composite_dev *cdev,
1484 struct usb_configuration *c,
1485 struct ffs_data *ffs)
1486{
1487 struct ffs_function *func;
1488 int ret;
1489
1490 ENTER();
1491
1492 func = kzalloc(sizeof *func, GFP_KERNEL);
1493 if (unlikely(!func))
1494 return -ENOMEM;
1495
1496 func->function.name = "Function FS Gadget";
1497 func->function.strings = ffs->stringtabs;
1498
1499 func->function.bind = ffs_func_bind;
1500 func->function.unbind = ffs_func_unbind;
1501 func->function.set_alt = ffs_func_set_alt;
1502 /*func->function.get_alt = ffs_func_get_alt;*/
1503 func->function.disable = ffs_func_disable;
1504 func->function.setup = ffs_func_setup;
1505 func->function.suspend = ffs_func_suspend;
1506 func->function.resume = ffs_func_resume;
1507
1508 func->conf = c;
1509 func->gadget = cdev->gadget;
1510 func->ffs = ffs;
1511 ffs_data_get(ffs);
1512
1513 ret = usb_add_function(c, &func->function);
1514 if (unlikely(ret))
1515 ffs_func_free(func);
1516
1517 return ret;
1518}
1519
1520static void ffs_func_free(struct ffs_function *func)
1521{
1522 ENTER();
1523
1524 ffs_data_put(func->ffs);
1525
1526 kfree(func->eps);
1527 /* eps and interfaces_nums are allocated in the same chunk so
1528 * only one free is required. Descriptors are also allocated
1529 * in the same chunk. */
1530
1531 kfree(func);
1532}
1533
1534
1535static void ffs_func_eps_disable(struct ffs_function *func)
1536{
1537 struct ffs_ep *ep = func->eps;
1538 struct ffs_epfile *epfile = func->ffs->epfiles;
1539 unsigned count = func->ffs->eps_count;
1540 unsigned long flags;
1541
1542 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1543 do {
1544 /* pending requests get nuked */
1545 if (likely(ep->ep))
1546 usb_ep_disable(ep->ep);
1547 epfile->ep = NULL;
1548
1549 ++ep;
1550 ++epfile;
1551 } while (--count);
1552 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1553}
1554
1555static int ffs_func_eps_enable(struct ffs_function *func)
1556{
1557 struct ffs_data *ffs = func->ffs;
1558 struct ffs_ep *ep = func->eps;
1559 struct ffs_epfile *epfile = ffs->epfiles;
1560 unsigned count = ffs->eps_count;
1561 unsigned long flags;
1562 int ret = 0;
1563
1564 spin_lock_irqsave(&func->ffs->eps_lock, flags);
1565 do {
1566 struct usb_endpoint_descriptor *ds;
1567 ds = ep->descs[ep->descs[1] ? 1 : 0];
1568
1569 ep->ep->driver_data = ep;
1570 ret = usb_ep_enable(ep->ep, ds);
1571 if (likely(!ret)) {
1572 epfile->ep = ep;
1573 epfile->in = usb_endpoint_dir_in(ds);
1574 epfile->isoc = usb_endpoint_xfer_isoc(ds);
1575 } else {
1576 break;
1577 }
1578
1579 wake_up(&epfile->wait);
1580
1581 ++ep;
1582 ++epfile;
1583 } while (--count);
1584 spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
1585
1586 return ret;
1587}
1588
1589
1590/* Parsing and building descriptors and strings *****************************/
1591
1592
1593/* This validates if data pointed by data is a valid USB descriptor as
1594 * well as record how many interfaces, endpoints and strings are
1595 * required by given configuration. Returns address afther the
1596 * descriptor or NULL if data is invalid. */
1597
1598enum ffs_entity_type {
1599 FFS_DESCRIPTOR, FFS_INTERFACE, FFS_STRING, FFS_ENDPOINT
1600};
1601
1602typedef int (*ffs_entity_callback)(enum ffs_entity_type entity,
1603 u8 *valuep,
1604 struct usb_descriptor_header *desc,
1605 void *priv);
1606
1607static int __must_check ffs_do_desc(char *data, unsigned len,
1608 ffs_entity_callback entity, void *priv)
1609{
1610 struct usb_descriptor_header *_ds = (void *)data;
1611 u8 length;
1612 int ret;
1613
1614 ENTER();
1615
1616 /* At least two bytes are required: length and type */
1617 if (len < 2) {
1618 FVDBG("descriptor too short");
1619 return -EINVAL;
1620 }
1621
1622 /* If we have at least as many bytes as the descriptor takes? */
1623 length = _ds->bLength;
1624 if (len < length) {
1625 FVDBG("descriptor longer then available data");
1626 return -EINVAL;
1627 }
1628
1629#define __entity_check_INTERFACE(val) 1
1630#define __entity_check_STRING(val) (val)
1631#define __entity_check_ENDPOINT(val) ((val) & USB_ENDPOINT_NUMBER_MASK)
1632#define __entity(type, val) do { \
1633 FVDBG("entity " #type "(%02x)", (val)); \
1634 if (unlikely(!__entity_check_ ##type(val))) { \
1635 FVDBG("invalid entity's value"); \
1636 return -EINVAL; \
1637 } \
1638 ret = entity(FFS_ ##type, &val, _ds, priv); \
1639 if (unlikely(ret < 0)) { \
1640 FDBG("entity " #type "(%02x); ret = %d", \
1641 (val), ret); \
1642 return ret; \
1643 } \
1644 } while (0)
1645
1646 /* Parse descriptor depending on type. */
1647 switch (_ds->bDescriptorType) {
1648 case USB_DT_DEVICE:
1649 case USB_DT_CONFIG:
1650 case USB_DT_STRING:
1651 case USB_DT_DEVICE_QUALIFIER:
1652 /* function can't have any of those */
1653 FVDBG("descriptor reserved for gadget: %d", _ds->bDescriptorType);
1654 return -EINVAL;
1655
1656 case USB_DT_INTERFACE: {
1657 struct usb_interface_descriptor *ds = (void *)_ds;
1658 FVDBG("interface descriptor");
1659 if (length != sizeof *ds)
1660 goto inv_length;
1661
1662 __entity(INTERFACE, ds->bInterfaceNumber);
1663 if (ds->iInterface)
1664 __entity(STRING, ds->iInterface);
1665 }
1666 break;
1667
1668 case USB_DT_ENDPOINT: {
1669 struct usb_endpoint_descriptor *ds = (void *)_ds;
1670 FVDBG("endpoint descriptor");
1671 if (length != USB_DT_ENDPOINT_SIZE &&
1672 length != USB_DT_ENDPOINT_AUDIO_SIZE)
1673 goto inv_length;
1674 __entity(ENDPOINT, ds->bEndpointAddress);
1675 }
1676 break;
1677
1678 case USB_DT_OTG:
1679 if (length != sizeof(struct usb_otg_descriptor))
1680 goto inv_length;
1681 break;
1682
1683 case USB_DT_INTERFACE_ASSOCIATION: {
1684 struct usb_interface_assoc_descriptor *ds = (void *)_ds;
1685 FVDBG("interface association descriptor");
1686 if (length != sizeof *ds)
1687 goto inv_length;
1688 if (ds->iFunction)
1689 __entity(STRING, ds->iFunction);
1690 }
1691 break;
1692
1693 case USB_DT_OTHER_SPEED_CONFIG:
1694 case USB_DT_INTERFACE_POWER:
1695 case USB_DT_DEBUG:
1696 case USB_DT_SECURITY:
1697 case USB_DT_CS_RADIO_CONTROL:
1698 /* TODO */
1699 FVDBG("unimplemented descriptor: %d", _ds->bDescriptorType);
1700 return -EINVAL;
1701
1702 default:
1703 /* We should never be here */
1704 FVDBG("unknown descriptor: %d", _ds->bDescriptorType);
1705 return -EINVAL;
1706
1707 inv_length:
1708 FVDBG("invalid length: %d (descriptor %d)",
1709 _ds->bLength, _ds->bDescriptorType);
1710 return -EINVAL;
1711 }
1712
1713#undef __entity
1714#undef __entity_check_DESCRIPTOR
1715#undef __entity_check_INTERFACE
1716#undef __entity_check_STRING
1717#undef __entity_check_ENDPOINT
1718
1719 return length;
1720}
1721
1722
1723static int __must_check ffs_do_descs(unsigned count, char *data, unsigned len,
1724 ffs_entity_callback entity, void *priv)
1725{
1726 const unsigned _len = len;
1727 unsigned long num = 0;
1728
1729 ENTER();
1730
1731 for (;;) {
1732 int ret;
1733
1734 if (num == count)
1735 data = NULL;
1736
1737 /* Record "descriptor" entitny */
1738 ret = entity(FFS_DESCRIPTOR, (u8 *)num, (void *)data, priv);
1739 if (unlikely(ret < 0)) {
1740 FDBG("entity DESCRIPTOR(%02lx); ret = %d", num, ret);
1741 return ret;
1742 }
1743
1744 if (!data)
1745 return _len - len;
1746
1747 ret = ffs_do_desc(data, len, entity, priv);
1748 if (unlikely(ret < 0)) {
1749 FDBG("%s returns %d", __func__, ret);
1750 return ret;
1751 }
1752
1753 len -= ret;
1754 data += ret;
1755 ++num;
1756 }
1757}
1758
1759
1760static int __ffs_data_do_entity(enum ffs_entity_type type,
1761 u8 *valuep, struct usb_descriptor_header *desc,
1762 void *priv)
1763{
1764 struct ffs_data *ffs = priv;
1765
1766 ENTER();
1767
1768 switch (type) {
1769 case FFS_DESCRIPTOR:
1770 break;
1771
1772 case FFS_INTERFACE:
1773 /* Interfaces are indexed from zero so if we
1774 * encountered interface "n" then there are at least
1775 * "n+1" interfaces. */
1776 if (*valuep >= ffs->interfaces_count)
1777 ffs->interfaces_count = *valuep + 1;
1778 break;
1779
1780 case FFS_STRING:
1781 /* Strings are indexed from 1 (0 is magic ;) reserved
1782 * for languages list or some such) */
1783 if (*valuep > ffs->strings_count)
1784 ffs->strings_count = *valuep;
1785 break;
1786
1787 case FFS_ENDPOINT:
1788 /* Endpoints are indexed from 1 as well. */
1789 if ((*valuep & USB_ENDPOINT_NUMBER_MASK) > ffs->eps_count)
1790 ffs->eps_count = (*valuep & USB_ENDPOINT_NUMBER_MASK);
1791 break;
1792 }
1793
1794 return 0;
1795}
1796
1797
1798static int __ffs_data_got_descs(struct ffs_data *ffs,
1799 char *const _data, size_t len)
1800{
1801 unsigned fs_count, hs_count;
1802 int fs_len, ret = -EINVAL;
1803 char *data = _data;
1804
1805 ENTER();
1806
1807 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_DESCRIPTORS_MAGIC ||
1808 get_unaligned_le32(data + 4) != len))
1809 goto error;
1810 fs_count = get_unaligned_le32(data + 8);
1811 hs_count = get_unaligned_le32(data + 12);
1812
1813 if (!fs_count && !hs_count)
1814 goto einval;
1815
1816 data += 16;
1817 len -= 16;
1818
1819 if (likely(fs_count)) {
1820 fs_len = ffs_do_descs(fs_count, data, len,
1821 __ffs_data_do_entity, ffs);
1822 if (unlikely(fs_len < 0)) {
1823 ret = fs_len;
1824 goto error;
1825 }
1826
1827 data += fs_len;
1828 len -= fs_len;
1829 } else {
1830 fs_len = 0;
1831 }
1832
1833 if (likely(hs_count)) {
1834 ret = ffs_do_descs(hs_count, data, len,
1835 __ffs_data_do_entity, ffs);
1836 if (unlikely(ret < 0))
1837 goto error;
1838 } else {
1839 ret = 0;
1840 }
1841
1842 if (unlikely(len != ret))
1843 goto einval;
1844
1845 ffs->raw_fs_descs_length = fs_len;
1846 ffs->raw_descs_length = fs_len + ret;
1847 ffs->raw_descs = _data;
1848 ffs->fs_descs_count = fs_count;
1849 ffs->hs_descs_count = hs_count;
1850
1851 return 0;
1852
1853einval:
1854 ret = -EINVAL;
1855error:
1856 kfree(_data);
1857 return ret;
1858}
1859
1860
1861
1862static int __ffs_data_got_strings(struct ffs_data *ffs,
1863 char *const _data, size_t len)
1864{
1865 u32 str_count, needed_count, lang_count;
1866 struct usb_gadget_strings **stringtabs, *t;
1867 struct usb_string *strings, *s;
1868 const char *data = _data;
1869
1870 ENTER();
1871
1872 if (unlikely(get_unaligned_le32(data) != FUNCTIONFS_STRINGS_MAGIC ||
1873 get_unaligned_le32(data + 4) != len))
1874 goto error;
1875 str_count = get_unaligned_le32(data + 8);
1876 lang_count = get_unaligned_le32(data + 12);
1877
1878 /* if one is zero the other must be zero */
1879 if (unlikely(!str_count != !lang_count))
1880 goto error;
1881
1882 /* Do we have at least as many strings as descriptors need? */
1883 needed_count = ffs->strings_count;
1884 if (unlikely(str_count < needed_count))
1885 goto error;
1886
1887 /* If we don't need any strings just return and free all
1888 * memory */
1889 if (!needed_count) {
1890 kfree(_data);
1891 return 0;
1892 }
1893
1894 /* Allocate */
1895 {
1896 /* Allocate everything in one chunk so there's less
1897 * maintanance. */
1898 struct {
1899 struct usb_gadget_strings *stringtabs[lang_count + 1];
1900 struct usb_gadget_strings stringtab[lang_count];
1901 struct usb_string strings[lang_count*(needed_count+1)];
1902 } *d;
1903 unsigned i = 0;
1904
1905 d = kmalloc(sizeof *d, GFP_KERNEL);
1906 if (unlikely(!d)) {
1907 kfree(_data);
1908 return -ENOMEM;
1909 }
1910
1911 stringtabs = d->stringtabs;
1912 t = d->stringtab;
1913 i = lang_count;
1914 do {
1915 *stringtabs++ = t++;
1916 } while (--i);
1917 *stringtabs = NULL;
1918
1919 stringtabs = d->stringtabs;
1920 t = d->stringtab;
1921 s = d->strings;
1922 strings = s;
1923 }
1924
1925 /* For each language */
1926 data += 16;
1927 len -= 16;
1928
1929 do { /* lang_count > 0 so we can use do-while */
1930 unsigned needed = needed_count;
1931
1932 if (unlikely(len < 3))
1933 goto error_free;
1934 t->language = get_unaligned_le16(data);
1935 t->strings = s;
1936 ++t;
1937
1938 data += 2;
1939 len -= 2;
1940
1941 /* For each string */
1942 do { /* str_count > 0 so we can use do-while */
1943 size_t length = strnlen(data, len);
1944
1945 if (unlikely(length == len))
1946 goto error_free;
1947
1948 /* user may provide more strings then we need,
1949 * if that's the case we simply ingore the
1950 * rest */
1951 if (likely(needed)) {
1952 /* s->id will be set while adding
1953 * function to configuration so for
1954 * now just leave garbage here. */
1955 s->s = data;
1956 --needed;
1957 ++s;
1958 }
1959
1960 data += length + 1;
1961 len -= length + 1;
1962 } while (--str_count);
1963
1964 s->id = 0; /* terminator */
1965 s->s = NULL;
1966 ++s;
1967
1968 } while (--lang_count);
1969
1970 /* Some garbage left? */
1971 if (unlikely(len))
1972 goto error_free;
1973
1974 /* Done! */
1975 ffs->stringtabs = stringtabs;
1976 ffs->raw_strings = _data;
1977
1978 return 0;
1979
1980error_free:
1981 kfree(stringtabs);
1982error:
1983 kfree(_data);
1984 return -EINVAL;
1985}
1986
1987
1988
1989
1990/* Events handling and management *******************************************/
1991
1992static void __ffs_event_add(struct ffs_data *ffs,
1993 enum usb_functionfs_event_type type)
1994{
1995 enum usb_functionfs_event_type rem_type1, rem_type2 = type;
1996 int neg = 0;
1997
1998 /* Abort any unhandled setup */
1999 /* We do not need to worry about some cmpxchg() changing value
2000 * of ffs->setup_state without holding the lock because when
2001 * state is FFS_SETUP_PENDING cmpxchg() in several places in
2002 * the source does nothing. */
2003 if (ffs->setup_state == FFS_SETUP_PENDING)
2004 ffs->setup_state = FFS_SETUP_CANCELED;
2005
2006 switch (type) {
2007 case FUNCTIONFS_RESUME:
2008 rem_type2 = FUNCTIONFS_SUSPEND;
2009 /* FALL THGOUTH */
2010 case FUNCTIONFS_SUSPEND:
2011 case FUNCTIONFS_SETUP:
2012 rem_type1 = type;
2013 /* discard all similar events */
2014 break;
2015
2016 case FUNCTIONFS_BIND:
2017 case FUNCTIONFS_UNBIND:
2018 case FUNCTIONFS_DISABLE:
2019 case FUNCTIONFS_ENABLE:
2020 /* discard everything other then power management. */
2021 rem_type1 = FUNCTIONFS_SUSPEND;
2022 rem_type2 = FUNCTIONFS_RESUME;
2023 neg = 1;
2024 break;
2025
2026 default:
2027 BUG();
2028 }
2029
2030 {
2031 u8 *ev = ffs->ev.types, *out = ev;
2032 unsigned n = ffs->ev.count;
2033 for (; n; --n, ++ev)
2034 if ((*ev == rem_type1 || *ev == rem_type2) == neg)
2035 *out++ = *ev;
2036 else
2037 FVDBG("purging event %d", *ev);
2038 ffs->ev.count = out - ffs->ev.types;
2039 }
2040
2041 FVDBG("adding event %d", type);
2042 ffs->ev.types[ffs->ev.count++] = type;
2043 wake_up_locked(&ffs->ev.waitq);
2044}
2045
2046static void ffs_event_add(struct ffs_data *ffs,
2047 enum usb_functionfs_event_type type)
2048{
2049 unsigned long flags;
2050 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2051 __ffs_event_add(ffs, type);
2052 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2053}
2054
2055
2056/* Bind/unbind USB function hooks *******************************************/
2057
2058static int __ffs_func_bind_do_descs(enum ffs_entity_type type, u8 *valuep,
2059 struct usb_descriptor_header *desc,
2060 void *priv)
2061{
2062 struct usb_endpoint_descriptor *ds = (void *)desc;
2063 struct ffs_function *func = priv;
2064 struct ffs_ep *ffs_ep;
2065
2066 /* If hs_descriptors is not NULL then we are reading hs
2067 * descriptors now */
2068 const int isHS = func->function.hs_descriptors != NULL;
2069 unsigned idx;
2070
2071 if (type != FFS_DESCRIPTOR)
2072 return 0;
2073
2074 if (isHS)
2075 func->function.hs_descriptors[(long)valuep] = desc;
2076 else
2077 func->function.descriptors[(long)valuep] = desc;
2078
2079 if (!desc || desc->bDescriptorType != USB_DT_ENDPOINT)
2080 return 0;
2081
2082 idx = (ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK) - 1;
2083 ffs_ep = func->eps + idx;
2084
2085 if (unlikely(ffs_ep->descs[isHS])) {
2086 FVDBG("two %sspeed descriptors for EP %d",
2087 isHS ? "high" : "full",
2088 ds->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2089 return -EINVAL;
2090 }
2091 ffs_ep->descs[isHS] = ds;
2092
2093 ffs_dump_mem(": Original ep desc", ds, ds->bLength);
2094 if (ffs_ep->ep) {
2095 ds->bEndpointAddress = ffs_ep->descs[0]->bEndpointAddress;
2096 if (!ds->wMaxPacketSize)
2097 ds->wMaxPacketSize = ffs_ep->descs[0]->wMaxPacketSize;
2098 } else {
2099 struct usb_request *req;
2100 struct usb_ep *ep;
2101
2102 FVDBG("autoconfig");
2103 ep = usb_ep_autoconfig(func->gadget, ds);
2104 if (unlikely(!ep))
2105 return -ENOTSUPP;
2106 ep->driver_data = func->eps + idx;;
2107
2108 req = usb_ep_alloc_request(ep, GFP_KERNEL);
2109 if (unlikely(!req))
2110 return -ENOMEM;
2111
2112 ffs_ep->ep = ep;
2113 ffs_ep->req = req;
2114 func->eps_revmap[ds->bEndpointAddress &
2115 USB_ENDPOINT_NUMBER_MASK] = idx + 1;
2116 }
2117 ffs_dump_mem(": Rewritten ep desc", ds, ds->bLength);
2118
2119 return 0;
2120}
2121
2122
2123static int __ffs_func_bind_do_nums(enum ffs_entity_type type, u8 *valuep,
2124 struct usb_descriptor_header *desc,
2125 void *priv)
2126{
2127 struct ffs_function *func = priv;
2128 unsigned idx;
2129 u8 newValue;
2130
2131 switch (type) {
2132 default:
2133 case FFS_DESCRIPTOR:
2134 /* Handled in previous pass by __ffs_func_bind_do_descs() */
2135 return 0;
2136
2137 case FFS_INTERFACE:
2138 idx = *valuep;
2139 if (func->interfaces_nums[idx] < 0) {
2140 int id = usb_interface_id(func->conf, &func->function);
2141 if (unlikely(id < 0))
2142 return id;
2143 func->interfaces_nums[idx] = id;
2144 }
2145 newValue = func->interfaces_nums[idx];
2146 break;
2147
2148 case FFS_STRING:
2149 /* String' IDs are allocated when fsf_data is bound to cdev */
2150 newValue = func->ffs->stringtabs[0]->strings[*valuep - 1].id;
2151 break;
2152
2153 case FFS_ENDPOINT:
2154 /* USB_DT_ENDPOINT are handled in
2155 * __ffs_func_bind_do_descs(). */
2156 if (desc->bDescriptorType == USB_DT_ENDPOINT)
2157 return 0;
2158
2159 idx = (*valuep & USB_ENDPOINT_NUMBER_MASK) - 1;
2160 if (unlikely(!func->eps[idx].ep))
2161 return -EINVAL;
2162
2163 {
2164 struct usb_endpoint_descriptor **descs;
2165 descs = func->eps[idx].descs;
2166 newValue = descs[descs[0] ? 0 : 1]->bEndpointAddress;
2167 }
2168 break;
2169 }
2170
2171 FVDBG("%02x -> %02x", *valuep, newValue);
2172 *valuep = newValue;
2173 return 0;
2174}
2175
2176static int ffs_func_bind(struct usb_configuration *c,
2177 struct usb_function *f)
2178{
2179 struct ffs_function *func = ffs_func_from_usb(f);
2180 struct ffs_data *ffs = func->ffs;
2181
2182 const int full = !!func->ffs->fs_descs_count;
2183 const int high = gadget_is_dualspeed(func->gadget) &&
2184 func->ffs->hs_descs_count;
2185
2186 int ret;
2187
2188 /* Make it a single chunk, less management later on */
2189 struct {
2190 struct ffs_ep eps[ffs->eps_count];
2191 struct usb_descriptor_header
2192 *fs_descs[full ? ffs->fs_descs_count + 1 : 0];
2193 struct usb_descriptor_header
2194 *hs_descs[high ? ffs->hs_descs_count + 1 : 0];
2195 short inums[ffs->interfaces_count];
2196 char raw_descs[high ? ffs->raw_descs_length
2197 : ffs->raw_fs_descs_length];
2198 } *data;
2199
2200 ENTER();
2201
2202 /* Only high speed but not supported by gadget? */
2203 if (unlikely(!(full | high)))
2204 return -ENOTSUPP;
2205
2206 /* Allocate */
2207 data = kmalloc(sizeof *data, GFP_KERNEL);
2208 if (unlikely(!data))
2209 return -ENOMEM;
2210
2211 /* Zero */
2212 memset(data->eps, 0, sizeof data->eps);
2213 memcpy(data->raw_descs, ffs->raw_descs + 16, sizeof data->raw_descs);
2214 memset(data->inums, 0xff, sizeof data->inums);
2215 for (ret = ffs->eps_count; ret; --ret)
2216 data->eps[ret].num = -1;
2217
2218 /* Save pointers */
2219 func->eps = data->eps;
2220 func->interfaces_nums = data->inums;
2221
2222 /* Go throught all the endpoint descriptors and allocate
2223 * endpoints first, so that later we can rewrite the endpoint
2224 * numbers without worying that it may be described later on. */
2225 if (likely(full)) {
2226 func->function.descriptors = data->fs_descs;
2227 ret = ffs_do_descs(ffs->fs_descs_count,
2228 data->raw_descs,
2229 sizeof data->raw_descs,
2230 __ffs_func_bind_do_descs, func);
2231 if (unlikely(ret < 0))
2232 goto error;
2233 } else {
2234 ret = 0;
2235 }
2236
2237 if (likely(high)) {
2238 func->function.hs_descriptors = data->hs_descs;
2239 ret = ffs_do_descs(ffs->hs_descs_count,
2240 data->raw_descs + ret,
2241 (sizeof data->raw_descs) - ret,
2242 __ffs_func_bind_do_descs, func);
2243 }
2244
2245 /* Now handle interface numbers allocation and interface and
2246 * enpoint numbers rewritting. We can do that in one go
2247 * now. */
2248 ret = ffs_do_descs(ffs->fs_descs_count +
2249 (high ? ffs->hs_descs_count : 0),
2250 data->raw_descs, sizeof data->raw_descs,
2251 __ffs_func_bind_do_nums, func);
2252 if (unlikely(ret < 0))
2253 goto error;
2254
2255 /* And we're done */
2256 ffs_event_add(ffs, FUNCTIONFS_BIND);
2257 return 0;
2258
2259error:
2260 /* XXX Do we need to release all claimed endpoints here? */
2261 return ret;
2262}
2263
2264
2265/* Other USB function hooks *************************************************/
2266
2267static void ffs_func_unbind(struct usb_configuration *c,
2268 struct usb_function *f)
2269{
2270 struct ffs_function *func = ffs_func_from_usb(f);
2271 struct ffs_data *ffs = func->ffs;
2272
2273 ENTER();
2274
2275 if (ffs->func == func) {
2276 ffs_func_eps_disable(func);
2277 ffs->func = NULL;
2278 }
2279
2280 ffs_event_add(ffs, FUNCTIONFS_UNBIND);
2281
2282 ffs_func_free(func);
2283}
2284
2285
2286static int ffs_func_set_alt(struct usb_function *f,
2287 unsigned interface, unsigned alt)
2288{
2289 struct ffs_function *func = ffs_func_from_usb(f);
2290 struct ffs_data *ffs = func->ffs;
2291 int ret = 0, intf;
2292
2293 if (alt != (unsigned)-1) {
2294 intf = ffs_func_revmap_intf(func, interface);
2295 if (unlikely(intf < 0))
2296 return intf;
2297 }
2298
2299 if (ffs->func)
2300 ffs_func_eps_disable(ffs->func);
2301
2302 if (ffs->state != FFS_ACTIVE)
2303 return -ENODEV;
2304
2305 if (alt == (unsigned)-1) {
2306 ffs->func = NULL;
2307 ffs_event_add(ffs, FUNCTIONFS_DISABLE);
2308 return 0;
2309 }
2310
2311 ffs->func = func;
2312 ret = ffs_func_eps_enable(func);
2313 if (likely(ret >= 0))
2314 ffs_event_add(ffs, FUNCTIONFS_ENABLE);
2315 return ret;
2316}
2317
2318static void ffs_func_disable(struct usb_function *f)
2319{
2320 ffs_func_set_alt(f, 0, (unsigned)-1);
2321}
2322
2323static int ffs_func_setup(struct usb_function *f,
2324 const struct usb_ctrlrequest *creq)
2325{
2326 struct ffs_function *func = ffs_func_from_usb(f);
2327 struct ffs_data *ffs = func->ffs;
2328 unsigned long flags;
2329 int ret;
2330
2331 ENTER();
2332
2333 FVDBG("creq->bRequestType = %02x", creq->bRequestType);
2334 FVDBG("creq->bRequest = %02x", creq->bRequest);
2335 FVDBG("creq->wValue = %04x", le16_to_cpu(creq->wValue));
2336 FVDBG("creq->wIndex = %04x", le16_to_cpu(creq->wIndex));
2337 FVDBG("creq->wLength = %04x", le16_to_cpu(creq->wLength));
2338
2339 /* Most requests directed to interface go throught here
2340 * (notable exceptions are set/get interface) so we need to
2341 * handle them. All other either handled by composite or
2342 * passed to usb_configuration->setup() (if one is set). No
2343 * matter, we will handle requests directed to endpoint here
2344 * as well (as it's straightforward) but what to do with any
2345 * other request? */
2346
2347 if (ffs->state != FFS_ACTIVE)
2348 return -ENODEV;
2349
2350 switch (creq->bRequestType & USB_RECIP_MASK) {
2351 case USB_RECIP_INTERFACE:
2352 ret = ffs_func_revmap_intf(func, le16_to_cpu(creq->wIndex));
2353 if (unlikely(ret < 0))
2354 return ret;
2355 break;
2356
2357 case USB_RECIP_ENDPOINT:
2358 ret = ffs_func_revmap_ep(func, le16_to_cpu(creq->wIndex));
2359 if (unlikely(ret < 0))
2360 return ret;
2361 break;
2362
2363 default:
2364 return -EOPNOTSUPP;
2365 }
2366
2367 spin_lock_irqsave(&ffs->ev.waitq.lock, flags);
2368 ffs->ev.setup = *creq;
2369 ffs->ev.setup.wIndex = cpu_to_le16(ret);
2370 __ffs_event_add(ffs, FUNCTIONFS_SETUP);
2371 spin_unlock_irqrestore(&ffs->ev.waitq.lock, flags);
2372
2373 return 0;
2374}
2375
2376static void ffs_func_suspend(struct usb_function *f)
2377{
2378 ENTER();
2379 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_SUSPEND);
2380}
2381
2382static void ffs_func_resume(struct usb_function *f)
2383{
2384 ENTER();
2385 ffs_event_add(ffs_func_from_usb(f)->ffs, FUNCTIONFS_RESUME);
2386}
2387
2388
2389
2390/* Enpoint and interface numbers reverse mapping ****************************/
2391
2392static int ffs_func_revmap_ep(struct ffs_function *func, u8 num)
2393{
2394 num = func->eps_revmap[num & USB_ENDPOINT_NUMBER_MASK];
2395 return num ? num : -EDOM;
2396}
2397
2398static int ffs_func_revmap_intf(struct ffs_function *func, u8 intf)
2399{
2400 short *nums = func->interfaces_nums;
2401 unsigned count = func->ffs->interfaces_count;
2402
2403 for (; count; --count, ++nums) {
2404 if (*nums >= 0 && *nums == intf)
2405 return nums - func->interfaces_nums;
2406 }
2407
2408 return -EDOM;
2409}
2410
2411
2412/* Misc helper functions ****************************************************/
2413
2414static int ffs_mutex_lock(struct mutex *mutex, unsigned nonblock)
2415{
2416 return nonblock
2417 ? likely(mutex_trylock(mutex)) ? 0 : -EAGAIN
2418 : mutex_lock_interruptible(mutex);
2419}
2420
2421
2422static char *ffs_prepare_buffer(const char * __user buf, size_t len)
2423{
2424 char *data;
2425
2426 if (unlikely(!len))
2427 return NULL;
2428
2429 data = kmalloc(len, GFP_KERNEL);
2430 if (unlikely(!data))
2431 return ERR_PTR(-ENOMEM);
2432
2433 if (unlikely(__copy_from_user(data, buf, len))) {
2434 kfree(data);
2435 return ERR_PTR(-EFAULT);
2436 }
2437
2438 FVDBG("Buffer from user space:");
2439 ffs_dump_mem("", data, len);
2440
2441 return data;
2442}
diff --git a/drivers/usb/gadget/f_hid.c b/drivers/usb/gadget/f_hid.c
new file mode 100644
index 000000000000..1e00ff9866af
--- /dev/null
+++ b/drivers/usb/gadget/f_hid.c
@@ -0,0 +1,673 @@
1/*
2 * f_hid.c -- USB HID function driver
3 *
4 * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/kernel.h>
22#include <linux/utsname.h>
23#include <linux/module.h>
24#include <linux/hid.h>
25#include <linux/cdev.h>
26#include <linux/mutex.h>
27#include <linux/poll.h>
28#include <linux/smp_lock.h>
29#include <linux/uaccess.h>
30#include <linux/wait.h>
31#include <linux/usb/g_hid.h>
32
33static int major, minors;
34static struct class *hidg_class;
35
36/*-------------------------------------------------------------------------*/
37/* HID gadget struct */
38
39struct f_hidg {
40 /* configuration */
41 unsigned char bInterfaceSubClass;
42 unsigned char bInterfaceProtocol;
43 unsigned short report_desc_length;
44 char *report_desc;
45 unsigned short report_length;
46
47 /* recv report */
48 char *set_report_buff;
49 unsigned short set_report_length;
50 spinlock_t spinlock;
51 wait_queue_head_t read_queue;
52
53 /* send report */
54 struct mutex lock;
55 bool write_pending;
56 wait_queue_head_t write_queue;
57 struct usb_request *req;
58
59 int minor;
60 struct cdev cdev;
61 struct usb_function func;
62 struct usb_ep *in_ep;
63 struct usb_endpoint_descriptor *fs_in_ep_desc;
64 struct usb_endpoint_descriptor *hs_in_ep_desc;
65};
66
67static inline struct f_hidg *func_to_hidg(struct usb_function *f)
68{
69 return container_of(f, struct f_hidg, func);
70}
71
72/*-------------------------------------------------------------------------*/
73/* Static descriptors */
74
75static struct usb_interface_descriptor hidg_interface_desc = {
76 .bLength = sizeof hidg_interface_desc,
77 .bDescriptorType = USB_DT_INTERFACE,
78 /* .bInterfaceNumber = DYNAMIC */
79 .bAlternateSetting = 0,
80 .bNumEndpoints = 1,
81 .bInterfaceClass = USB_CLASS_HID,
82 /* .bInterfaceSubClass = DYNAMIC */
83 /* .bInterfaceProtocol = DYNAMIC */
84 /* .iInterface = DYNAMIC */
85};
86
87static struct hid_descriptor hidg_desc = {
88 .bLength = sizeof hidg_desc,
89 .bDescriptorType = HID_DT_HID,
90 .bcdHID = 0x0101,
91 .bCountryCode = 0x00,
92 .bNumDescriptors = 0x1,
93 /*.desc[0].bDescriptorType = DYNAMIC */
94 /*.desc[0].wDescriptorLenght = DYNAMIC */
95};
96
97/* High-Speed Support */
98
99static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
100 .bLength = USB_DT_ENDPOINT_SIZE,
101 .bDescriptorType = USB_DT_ENDPOINT,
102 .bEndpointAddress = USB_DIR_IN,
103 .bmAttributes = USB_ENDPOINT_XFER_INT,
104 /*.wMaxPacketSize = DYNAMIC */
105 .bInterval = 4, /* FIXME: Add this field in the
106 * HID gadget configuration?
107 * (struct hidg_func_descriptor)
108 */
109};
110
111static struct usb_descriptor_header *hidg_hs_descriptors[] = {
112 (struct usb_descriptor_header *)&hidg_interface_desc,
113 (struct usb_descriptor_header *)&hidg_desc,
114 (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
115 NULL,
116};
117
118/* Full-Speed Support */
119
120static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
121 .bLength = USB_DT_ENDPOINT_SIZE,
122 .bDescriptorType = USB_DT_ENDPOINT,
123 .bEndpointAddress = USB_DIR_IN,
124 .bmAttributes = USB_ENDPOINT_XFER_INT,
125 /*.wMaxPacketSize = DYNAMIC */
126 .bInterval = 10, /* FIXME: Add this field in the
127 * HID gadget configuration?
128 * (struct hidg_func_descriptor)
129 */
130};
131
132static struct usb_descriptor_header *hidg_fs_descriptors[] = {
133 (struct usb_descriptor_header *)&hidg_interface_desc,
134 (struct usb_descriptor_header *)&hidg_desc,
135 (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
136 NULL,
137};
138
139/*-------------------------------------------------------------------------*/
140/* Char Device */
141
142static ssize_t f_hidg_read(struct file *file, char __user *buffer,
143 size_t count, loff_t *ptr)
144{
145 struct f_hidg *hidg = (struct f_hidg *)file->private_data;
146 char *tmp_buff = NULL;
147 unsigned long flags;
148
149 if (!count)
150 return 0;
151
152 if (!access_ok(VERIFY_WRITE, buffer, count))
153 return -EFAULT;
154
155 spin_lock_irqsave(&hidg->spinlock, flags);
156
157#define READ_COND (hidg->set_report_buff != NULL)
158
159 while (!READ_COND) {
160 spin_unlock_irqrestore(&hidg->spinlock, flags);
161 if (file->f_flags & O_NONBLOCK)
162 return -EAGAIN;
163
164 if (wait_event_interruptible(hidg->read_queue, READ_COND))
165 return -ERESTARTSYS;
166
167 spin_lock_irqsave(&hidg->spinlock, flags);
168 }
169
170
171 count = min_t(unsigned, count, hidg->set_report_length);
172 tmp_buff = hidg->set_report_buff;
173 hidg->set_report_buff = NULL;
174
175 spin_unlock_irqrestore(&hidg->spinlock, flags);
176
177 if (tmp_buff != NULL) {
178 /* copy to user outside spinlock */
179 count -= copy_to_user(buffer, tmp_buff, count);
180 kfree(tmp_buff);
181 } else
182 count = -ENOMEM;
183
184 return count;
185}
186
187static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
188{
189 struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
190
191 if (req->status != 0) {
192 ERROR(hidg->func.config->cdev,
193 "End Point Request ERROR: %d\n", req->status);
194 }
195
196 hidg->write_pending = 0;
197 wake_up(&hidg->write_queue);
198}
199
200static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
201 size_t count, loff_t *offp)
202{
203 struct f_hidg *hidg = (struct f_hidg *)file->private_data;
204 ssize_t status = -ENOMEM;
205
206 if (!access_ok(VERIFY_READ, buffer, count))
207 return -EFAULT;
208
209 mutex_lock(&hidg->lock);
210
211#define WRITE_COND (!hidg->write_pending)
212
213 /* write queue */
214 while (!WRITE_COND) {
215 mutex_unlock(&hidg->lock);
216 if (file->f_flags & O_NONBLOCK)
217 return -EAGAIN;
218
219 if (wait_event_interruptible_exclusive(
220 hidg->write_queue, WRITE_COND))
221 return -ERESTARTSYS;
222
223 mutex_lock(&hidg->lock);
224 }
225
226 count = min_t(unsigned, count, hidg->report_length);
227 status = copy_from_user(hidg->req->buf, buffer, count);
228
229 if (status != 0) {
230 ERROR(hidg->func.config->cdev,
231 "copy_from_user error\n");
232 mutex_unlock(&hidg->lock);
233 return -EINVAL;
234 }
235
236 hidg->req->status = 0;
237 hidg->req->zero = 0;
238 hidg->req->length = count;
239 hidg->req->complete = f_hidg_req_complete;
240 hidg->req->context = hidg;
241 hidg->write_pending = 1;
242
243 status = usb_ep_queue(hidg->in_ep, hidg->req, GFP_ATOMIC);
244 if (status < 0) {
245 ERROR(hidg->func.config->cdev,
246 "usb_ep_queue error on int endpoint %zd\n", status);
247 hidg->write_pending = 0;
248 wake_up(&hidg->write_queue);
249 } else {
250 status = count;
251 }
252
253 mutex_unlock(&hidg->lock);
254
255 return status;
256}
257
258static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
259{
260 struct f_hidg *hidg = (struct f_hidg *)file->private_data;
261 unsigned int ret = 0;
262
263 poll_wait(file, &hidg->read_queue, wait);
264 poll_wait(file, &hidg->write_queue, wait);
265
266 if (WRITE_COND)
267 ret |= POLLOUT | POLLWRNORM;
268
269 if (READ_COND)
270 ret |= POLLIN | POLLRDNORM;
271
272 return ret;
273}
274
275#undef WRITE_COND
276#undef READ_COND
277
278static int f_hidg_release(struct inode *inode, struct file *fd)
279{
280 fd->private_data = NULL;
281 return 0;
282}
283
284static int f_hidg_open(struct inode *inode, struct file *fd)
285{
286 struct f_hidg *hidg =
287 container_of(inode->i_cdev, struct f_hidg, cdev);
288
289 fd->private_data = hidg;
290
291 return 0;
292}
293
294/*-------------------------------------------------------------------------*/
295/* usb_function */
296
297static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
298{
299 struct f_hidg *hidg = (struct f_hidg *)req->context;
300
301 if (req->status != 0 || req->buf == NULL || req->actual == 0) {
302 ERROR(hidg->func.config->cdev, "%s FAILED\n", __func__);
303 return;
304 }
305
306 spin_lock(&hidg->spinlock);
307
308 hidg->set_report_buff = krealloc(hidg->set_report_buff,
309 req->actual, GFP_ATOMIC);
310
311 if (hidg->set_report_buff == NULL) {
312 spin_unlock(&hidg->spinlock);
313 return;
314 }
315 hidg->set_report_length = req->actual;
316 memcpy(hidg->set_report_buff, req->buf, req->actual);
317
318 spin_unlock(&hidg->spinlock);
319
320 wake_up(&hidg->read_queue);
321
322 return;
323}
324
325static int hidg_setup(struct usb_function *f,
326 const struct usb_ctrlrequest *ctrl)
327{
328 struct f_hidg *hidg = func_to_hidg(f);
329 struct usb_composite_dev *cdev = f->config->cdev;
330 struct usb_request *req = cdev->req;
331 int status = 0;
332 __u16 value, length;
333
334 value = __le16_to_cpu(ctrl->wValue);
335 length = __le16_to_cpu(ctrl->wLength);
336
337 VDBG(cdev, "hid_setup crtl_request : bRequestType:0x%x bRequest:0x%x "
338 "Value:0x%x\n", ctrl->bRequestType, ctrl->bRequest, value);
339
340 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
341 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
342 | HID_REQ_GET_REPORT):
343 VDBG(cdev, "get_report\n");
344
345 /* send an empty report */
346 length = min_t(unsigned, length, hidg->report_length);
347 memset(req->buf, 0x0, length);
348
349 goto respond;
350 break;
351
352 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
353 | HID_REQ_GET_PROTOCOL):
354 VDBG(cdev, "get_protocol\n");
355 goto stall;
356 break;
357
358 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
359 | HID_REQ_SET_REPORT):
360 VDBG(cdev, "set_report | wLenght=%d\n", ctrl->wLength);
361 req->context = hidg;
362 req->complete = hidg_set_report_complete;
363 goto respond;
364 break;
365
366 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
367 | HID_REQ_SET_PROTOCOL):
368 VDBG(cdev, "set_protocol\n");
369 goto stall;
370 break;
371
372 case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
373 | USB_REQ_GET_DESCRIPTOR):
374 switch (value >> 8) {
375 case HID_DT_REPORT:
376 VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
377 length = min_t(unsigned short, length,
378 hidg->report_desc_length);
379 memcpy(req->buf, hidg->report_desc, length);
380 goto respond;
381 break;
382
383 default:
384 VDBG(cdev, "Unknown decriptor request 0x%x\n",
385 value >> 8);
386 goto stall;
387 break;
388 }
389 break;
390
391 default:
392 VDBG(cdev, "Unknown request 0x%x\n",
393 ctrl->bRequest);
394 goto stall;
395 break;
396 }
397
398stall:
399 return -EOPNOTSUPP;
400
401respond:
402 req->zero = 0;
403 req->length = length;
404 status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
405 if (status < 0)
406 ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
407 return status;
408}
409
410static void hidg_disable(struct usb_function *f)
411{
412 struct f_hidg *hidg = func_to_hidg(f);
413
414 usb_ep_disable(hidg->in_ep);
415 hidg->in_ep->driver_data = NULL;
416
417 return;
418}
419
420static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
421{
422 struct usb_composite_dev *cdev = f->config->cdev;
423 struct f_hidg *hidg = func_to_hidg(f);
424 const struct usb_endpoint_descriptor *ep_desc;
425 int status = 0;
426
427 VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);
428
429 if (hidg->in_ep != NULL) {
430 /* restart endpoint */
431 if (hidg->in_ep->driver_data != NULL)
432 usb_ep_disable(hidg->in_ep);
433
434 ep_desc = ep_choose(f->config->cdev->gadget,
435 hidg->hs_in_ep_desc, hidg->fs_in_ep_desc);
436 status = usb_ep_enable(hidg->in_ep, ep_desc);
437 if (status < 0) {
438 ERROR(cdev, "Enable endpoint FAILED!\n");
439 goto fail;
440 }
441 hidg->in_ep->driver_data = hidg;
442 }
443fail:
444 return status;
445}
446
447const struct file_operations f_hidg_fops = {
448 .owner = THIS_MODULE,
449 .open = f_hidg_open,
450 .release = f_hidg_release,
451 .write = f_hidg_write,
452 .read = f_hidg_read,
453 .poll = f_hidg_poll,
454};
455
456static int __init hidg_bind(struct usb_configuration *c, struct usb_function *f)
457{
458 struct usb_ep *ep;
459 struct f_hidg *hidg = func_to_hidg(f);
460 int status;
461 dev_t dev;
462
463 /* allocate instance-specific interface IDs, and patch descriptors */
464 status = usb_interface_id(c, f);
465 if (status < 0)
466 goto fail;
467 hidg_interface_desc.bInterfaceNumber = status;
468
469
470 /* allocate instance-specific endpoints */
471 status = -ENODEV;
472 ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
473 if (!ep)
474 goto fail;
475 ep->driver_data = c->cdev; /* claim */
476 hidg->in_ep = ep;
477
478 /* preallocate request and buffer */
479 status = -ENOMEM;
480 hidg->req = usb_ep_alloc_request(hidg->in_ep, GFP_KERNEL);
481 if (!hidg->req)
482 goto fail;
483
484
485 hidg->req->buf = kmalloc(hidg->report_length, GFP_KERNEL);
486 if (!hidg->req->buf)
487 goto fail;
488
489 /* set descriptor dynamic values */
490 hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
491 hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
492 hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
493 hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
494 hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
495 hidg_desc.desc[0].wDescriptorLength =
496 cpu_to_le16(hidg->report_desc_length);
497
498 hidg->set_report_buff = NULL;
499
500 /* copy descriptors */
501 f->descriptors = usb_copy_descriptors(hidg_fs_descriptors);
502 if (!f->descriptors)
503 goto fail;
504
505 hidg->fs_in_ep_desc = usb_find_endpoint(hidg_fs_descriptors,
506 f->descriptors,
507 &hidg_fs_in_ep_desc);
508
509 if (gadget_is_dualspeed(c->cdev->gadget)) {
510 hidg_hs_in_ep_desc.bEndpointAddress =
511 hidg_fs_in_ep_desc.bEndpointAddress;
512 f->hs_descriptors = usb_copy_descriptors(hidg_hs_descriptors);
513 if (!f->hs_descriptors)
514 goto fail;
515 hidg->hs_in_ep_desc = usb_find_endpoint(hidg_hs_descriptors,
516 f->hs_descriptors,
517 &hidg_hs_in_ep_desc);
518 } else {
519 hidg->hs_in_ep_desc = NULL;
520 }
521
522 mutex_init(&hidg->lock);
523 spin_lock_init(&hidg->spinlock);
524 init_waitqueue_head(&hidg->write_queue);
525 init_waitqueue_head(&hidg->read_queue);
526
527 /* create char device */
528 cdev_init(&hidg->cdev, &f_hidg_fops);
529 dev = MKDEV(major, hidg->minor);
530 status = cdev_add(&hidg->cdev, dev, 1);
531 if (status)
532 goto fail;
533
534 device_create(hidg_class, NULL, dev, NULL, "%s%d", "hidg", hidg->minor);
535
536 return 0;
537
538fail:
539 ERROR(f->config->cdev, "hidg_bind FAILED\n");
540 if (hidg->req != NULL) {
541 kfree(hidg->req->buf);
542 if (hidg->in_ep != NULL)
543 usb_ep_free_request(hidg->in_ep, hidg->req);
544 }
545
546 usb_free_descriptors(f->hs_descriptors);
547 usb_free_descriptors(f->descriptors);
548
549 return status;
550}
551
552static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
553{
554 struct f_hidg *hidg = func_to_hidg(f);
555
556 device_destroy(hidg_class, MKDEV(major, hidg->minor));
557 cdev_del(&hidg->cdev);
558
559 /* disable/free request and end point */
560 usb_ep_disable(hidg->in_ep);
561 usb_ep_dequeue(hidg->in_ep, hidg->req);
562 kfree(hidg->req->buf);
563 usb_ep_free_request(hidg->in_ep, hidg->req);
564
565 /* free descriptors copies */
566 usb_free_descriptors(f->hs_descriptors);
567 usb_free_descriptors(f->descriptors);
568
569 kfree(hidg->report_desc);
570 kfree(hidg->set_report_buff);
571 kfree(hidg);
572}
573
574/*-------------------------------------------------------------------------*/
575/* Strings */
576
577#define CT_FUNC_HID_IDX 0
578
579static struct usb_string ct_func_string_defs[] = {
580 [CT_FUNC_HID_IDX].s = "HID Interface",
581 {}, /* end of list */
582};
583
584static struct usb_gadget_strings ct_func_string_table = {
585 .language = 0x0409, /* en-US */
586 .strings = ct_func_string_defs,
587};
588
589static struct usb_gadget_strings *ct_func_strings[] = {
590 &ct_func_string_table,
591 NULL,
592};
593
594/*-------------------------------------------------------------------------*/
595/* usb_configuration */
596
597int __init hidg_bind_config(struct usb_configuration *c,
598 struct hidg_func_descriptor *fdesc, int index)
599{
600 struct f_hidg *hidg;
601 int status;
602
603 if (index >= minors)
604 return -ENOENT;
605
606 /* maybe allocate device-global string IDs, and patch descriptors */
607 if (ct_func_string_defs[CT_FUNC_HID_IDX].id == 0) {
608 status = usb_string_id(c->cdev);
609 if (status < 0)
610 return status;
611 ct_func_string_defs[CT_FUNC_HID_IDX].id = status;
612 hidg_interface_desc.iInterface = status;
613 }
614
615 /* allocate and initialize one new instance */
616 hidg = kzalloc(sizeof *hidg, GFP_KERNEL);
617 if (!hidg)
618 return -ENOMEM;
619
620 hidg->minor = index;
621 hidg->bInterfaceSubClass = fdesc->subclass;
622 hidg->bInterfaceProtocol = fdesc->protocol;
623 hidg->report_length = fdesc->report_length;
624 hidg->report_desc_length = fdesc->report_desc_length;
625 hidg->report_desc = kmemdup(fdesc->report_desc,
626 fdesc->report_desc_length,
627 GFP_KERNEL);
628 if (!hidg->report_desc) {
629 kfree(hidg);
630 return -ENOMEM;
631 }
632
633 hidg->func.name = "hid";
634 hidg->func.strings = ct_func_strings;
635 hidg->func.bind = hidg_bind;
636 hidg->func.unbind = hidg_unbind;
637 hidg->func.set_alt = hidg_set_alt;
638 hidg->func.disable = hidg_disable;
639 hidg->func.setup = hidg_setup;
640
641 status = usb_add_function(c, &hidg->func);
642 if (status)
643 kfree(hidg);
644
645 return status;
646}
647
648int __init ghid_setup(struct usb_gadget *g, int count)
649{
650 int status;
651 dev_t dev;
652
653 hidg_class = class_create(THIS_MODULE, "hidg");
654
655 status = alloc_chrdev_region(&dev, 0, count, "hidg");
656 if (!status) {
657 major = MAJOR(dev);
658 minors = count;
659 }
660
661 return status;
662}
663
664void ghid_cleanup(void)
665{
666 if (major) {
667 unregister_chrdev_region(MKDEV(major, 0), minors);
668 major = minors = 0;
669 }
670
671 class_destroy(hidg_class);
672 hidg_class = NULL;
673}
diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c
index f4911c09022e..7d05a0be5c60 100644
--- a/drivers/usb/gadget/f_mass_storage.c
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -163,6 +163,10 @@
163 * ro setting are not allowed when the medium is loaded or if CD-ROM 163 * ro setting are not allowed when the medium is loaded or if CD-ROM
164 * emulation is being used. 164 * emulation is being used.
165 * 165 *
166 * When a LUN receive an "eject" SCSI request (Start/Stop Unit),
167 * if the LUN is removable, the backing file is released to simulate
168 * ejection.
169 *
166 * 170 *
167 * This function is heavily based on "File-backed Storage Gadget" by 171 * This function is heavily based on "File-backed Storage Gadget" by
168 * Alan Stern which in turn is heavily based on "Gadget Zero" by David 172 * Alan Stern which in turn is heavily based on "Gadget Zero" by David
@@ -302,7 +306,6 @@ static const char fsg_string_interface[] = "Mass Storage";
302 306
303 307
304#define FSG_NO_INTR_EP 1 308#define FSG_NO_INTR_EP 1
305#define FSG_BUFFHD_STATIC_BUFFER 1
306#define FSG_NO_DEVICE_STRINGS 1 309#define FSG_NO_DEVICE_STRINGS 1
307#define FSG_NO_OTG 1 310#define FSG_NO_OTG 1
308#define FSG_NO_INTR_EP 1 311#define FSG_NO_INTR_EP 1
@@ -1385,12 +1388,50 @@ static int do_mode_sense(struct fsg_common *common, struct fsg_buffhd *bh)
1385 1388
1386static int do_start_stop(struct fsg_common *common) 1389static int do_start_stop(struct fsg_common *common)
1387{ 1390{
1388 if (!common->curlun) { 1391 struct fsg_lun *curlun = common->curlun;
1392 int loej, start;
1393
1394 if (!curlun) {
1389 return -EINVAL; 1395 return -EINVAL;
1390 } else if (!common->curlun->removable) { 1396 } else if (!curlun->removable) {
1391 common->curlun->sense_data = SS_INVALID_COMMAND; 1397 curlun->sense_data = SS_INVALID_COMMAND;
1392 return -EINVAL; 1398 return -EINVAL;
1393 } 1399 }
1400
1401 loej = common->cmnd[4] & 0x02;
1402 start = common->cmnd[4] & 0x01;
1403
1404 /* eject code from file_storage.c:do_start_stop() */
1405
1406 if ((common->cmnd[1] & ~0x01) != 0 || /* Mask away Immed */
1407 (common->cmnd[4] & ~0x03) != 0) { /* Mask LoEj, Start */
1408 curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
1409 return -EINVAL;
1410 }
1411
1412 if (!start) {
1413 /* Are we allowed to unload the media? */
1414 if (curlun->prevent_medium_removal) {
1415 LDBG(curlun, "unload attempt prevented\n");
1416 curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
1417 return -EINVAL;
1418 }
1419 if (loej) { /* Simulate an unload/eject */
1420 up_read(&common->filesem);
1421 down_write(&common->filesem);
1422 fsg_lun_close(curlun);
1423 up_write(&common->filesem);
1424 down_read(&common->filesem);
1425 }
1426 } else {
1427
1428 /* Our emulation doesn't support mounting; the medium is
1429 * available for use as soon as it is loaded. */
1430 if (!fsg_lun_is_open(curlun)) {
1431 curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
1432 return -EINVAL;
1433 }
1434 }
1394 return 0; 1435 return 0;
1395} 1436}
1396 1437
@@ -2701,10 +2742,8 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2701 /* Maybe allocate device-global string IDs, and patch descriptors */ 2742 /* Maybe allocate device-global string IDs, and patch descriptors */
2702 if (fsg_strings[FSG_STRING_INTERFACE].id == 0) { 2743 if (fsg_strings[FSG_STRING_INTERFACE].id == 0) {
2703 rc = usb_string_id(cdev); 2744 rc = usb_string_id(cdev);
2704 if (rc < 0) { 2745 if (unlikely(rc < 0))
2705 kfree(common); 2746 goto error_release;
2706 return ERR_PTR(rc);
2707 }
2708 fsg_strings[FSG_STRING_INTERFACE].id = rc; 2747 fsg_strings[FSG_STRING_INTERFACE].id = rc;
2709 fsg_intf_desc.iInterface = rc; 2748 fsg_intf_desc.iInterface = rc;
2710 } 2749 }
@@ -2712,9 +2751,9 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2712 /* Create the LUNs, open their backing files, and register the 2751 /* Create the LUNs, open their backing files, and register the
2713 * LUN devices in sysfs. */ 2752 * LUN devices in sysfs. */
2714 curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL); 2753 curlun = kzalloc(nluns * sizeof *curlun, GFP_KERNEL);
2715 if (!curlun) { 2754 if (unlikely(!curlun)) {
2716 kfree(common); 2755 rc = -ENOMEM;
2717 return ERR_PTR(-ENOMEM); 2756 goto error_release;
2718 } 2757 }
2719 common->luns = curlun; 2758 common->luns = curlun;
2720 2759
@@ -2762,13 +2801,19 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common,
2762 2801
2763 2802
2764 /* Data buffers cyclic list */ 2803 /* Data buffers cyclic list */
2765 /* Buffers in buffhds are static -- no need for additional
2766 * allocation. */
2767 bh = common->buffhds; 2804 bh = common->buffhds;
2768 i = FSG_NUM_BUFFERS - 1; 2805 i = FSG_NUM_BUFFERS;
2806 goto buffhds_first_it;
2769 do { 2807 do {
2770 bh->next = bh + 1; 2808 bh->next = bh + 1;
2771 } while (++bh, --i); 2809 ++bh;
2810buffhds_first_it:
2811 bh->buf = kmalloc(FSG_BUFLEN, GFP_KERNEL);
2812 if (unlikely(!bh->buf)) {
2813 rc = -ENOMEM;
2814 goto error_release;
2815 }
2816 } while (--i);
2772 bh->next = common->buffhds; 2817 bh->next = common->buffhds;
2773 2818
2774 2819
@@ -2867,10 +2912,7 @@ error_release:
2867 2912
2868static void fsg_common_release(struct kref *ref) 2913static void fsg_common_release(struct kref *ref)
2869{ 2914{
2870 struct fsg_common *common = 2915 struct fsg_common *common = container_of(ref, struct fsg_common, ref);
2871 container_of(ref, struct fsg_common, ref);
2872 unsigned i = common->nluns;
2873 struct fsg_lun *lun = common->luns;
2874 2916
2875 /* If the thread isn't already dead, tell it to exit now */ 2917 /* If the thread isn't already dead, tell it to exit now */
2876 if (common->state != FSG_STATE_TERMINATED) { 2918 if (common->state != FSG_STATE_TERMINATED) {
@@ -2881,17 +2923,29 @@ static void fsg_common_release(struct kref *ref)
2881 complete(&common->thread_notifier); 2923 complete(&common->thread_notifier);
2882 } 2924 }
2883 2925
2884 /* Beware tempting for -> do-while optimization: when in error 2926 if (likely(common->luns)) {
2885 * recovery nluns may be zero. */ 2927 struct fsg_lun *lun = common->luns;
2928 unsigned i = common->nluns;
2929
2930 /* In error recovery common->nluns may be zero. */
2931 for (; i; --i, ++lun) {
2932 device_remove_file(&lun->dev, &dev_attr_ro);
2933 device_remove_file(&lun->dev, &dev_attr_file);
2934 fsg_lun_close(lun);
2935 device_unregister(&lun->dev);
2936 }
2937
2938 kfree(common->luns);
2939 }
2886 2940
2887 for (; i; --i, ++lun) { 2941 {
2888 device_remove_file(&lun->dev, &dev_attr_ro); 2942 struct fsg_buffhd *bh = common->buffhds;
2889 device_remove_file(&lun->dev, &dev_attr_file); 2943 unsigned i = FSG_NUM_BUFFERS;
2890 fsg_lun_close(lun); 2944 do {
2891 device_unregister(&lun->dev); 2945 kfree(bh->buf);
2946 } while (++bh, --i);
2892 } 2947 }
2893 2948
2894 kfree(common->luns);
2895 if (common->free_storage_on_release) 2949 if (common->free_storage_on_release)
2896 kfree(common); 2950 kfree(common);
2897} 2951}
@@ -2906,11 +2960,13 @@ static void fsg_unbind(struct usb_configuration *c, struct usb_function *f)
2906 2960
2907 DBG(fsg, "unbind\n"); 2961 DBG(fsg, "unbind\n");
2908 fsg_common_put(fsg->common); 2962 fsg_common_put(fsg->common);
2963 usb_free_descriptors(fsg->function.descriptors);
2964 usb_free_descriptors(fsg->function.hs_descriptors);
2909 kfree(fsg); 2965 kfree(fsg);
2910} 2966}
2911 2967
2912 2968
2913static int __init fsg_bind(struct usb_configuration *c, struct usb_function *f) 2969static int fsg_bind(struct usb_configuration *c, struct usb_function *f)
2914{ 2970{
2915 struct fsg_dev *fsg = fsg_from_func(f); 2971 struct fsg_dev *fsg = fsg_from_func(f);
2916 struct usb_gadget *gadget = c->cdev->gadget; 2972 struct usb_gadget *gadget = c->cdev->gadget;
@@ -2946,7 +3002,9 @@ static int __init fsg_bind(struct usb_configuration *c, struct usb_function *f)
2946 fsg_fs_bulk_in_desc.bEndpointAddress; 3002 fsg_fs_bulk_in_desc.bEndpointAddress;
2947 fsg_hs_bulk_out_desc.bEndpointAddress = 3003 fsg_hs_bulk_out_desc.bEndpointAddress =
2948 fsg_fs_bulk_out_desc.bEndpointAddress; 3004 fsg_fs_bulk_out_desc.bEndpointAddress;
2949 f->hs_descriptors = fsg_hs_function; 3005 f->hs_descriptors = usb_copy_descriptors(fsg_hs_function);
3006 if (unlikely(!f->hs_descriptors))
3007 return -ENOMEM;
2950 } 3008 }
2951 3009
2952 return 0; 3010 return 0;
@@ -2978,7 +3036,11 @@ static int fsg_add(struct usb_composite_dev *cdev,
2978 3036
2979 fsg->function.name = FSG_DRIVER_DESC; 3037 fsg->function.name = FSG_DRIVER_DESC;
2980 fsg->function.strings = fsg_strings_array; 3038 fsg->function.strings = fsg_strings_array;
2981 fsg->function.descriptors = fsg_fs_function; 3039 fsg->function.descriptors = usb_copy_descriptors(fsg_fs_function);
3040 if (unlikely(!fsg->function.descriptors)) {
3041 rc = -ENOMEM;
3042 goto error_free_fsg;
3043 }
2982 fsg->function.bind = fsg_bind; 3044 fsg->function.bind = fsg_bind;
2983 fsg->function.unbind = fsg_unbind; 3045 fsg->function.unbind = fsg_unbind;
2984 fsg->function.setup = fsg_setup; 3046 fsg->function.setup = fsg_setup;
@@ -2993,11 +3055,19 @@ static int fsg_add(struct usb_composite_dev *cdev,
2993 * call to usb_add_function() was successful. */ 3055 * call to usb_add_function() was successful. */
2994 3056
2995 rc = usb_add_function(c, &fsg->function); 3057 rc = usb_add_function(c, &fsg->function);
3058 if (unlikely(rc))
3059 goto error_free_all;
2996 3060
2997 if (likely(rc == 0)) 3061 fsg_common_get(fsg->common);
2998 fsg_common_get(fsg->common); 3062 return 0;
2999 else 3063
3000 kfree(fsg); 3064error_free_all:
3065 usb_free_descriptors(fsg->function.descriptors);
3066 /* fsg_bind() might have copied those; or maybe not? who cares
3067 * -- free it just in case. */
3068 usb_free_descriptors(fsg->function.hs_descriptors);
3069error_free_fsg:
3070 kfree(fsg);
3001 3071
3002 return rc; 3072 return rc;
3003} 3073}
diff --git a/drivers/usb/gadget/f_rndis.c b/drivers/usb/gadget/f_rndis.c
index 56b022150f22..882484a40398 100644
--- a/drivers/usb/gadget/f_rndis.c
+++ b/drivers/usb/gadget/f_rndis.c
@@ -122,7 +122,7 @@ static unsigned int bitrate(struct usb_gadget *g)
122 122
123/* interface descriptor: */ 123/* interface descriptor: */
124 124
125static struct usb_interface_descriptor rndis_control_intf __initdata = { 125static struct usb_interface_descriptor rndis_control_intf = {
126 .bLength = sizeof rndis_control_intf, 126 .bLength = sizeof rndis_control_intf,
127 .bDescriptorType = USB_DT_INTERFACE, 127 .bDescriptorType = USB_DT_INTERFACE,
128 128
@@ -135,7 +135,7 @@ static struct usb_interface_descriptor rndis_control_intf __initdata = {
135 /* .iInterface = DYNAMIC */ 135 /* .iInterface = DYNAMIC */
136}; 136};
137 137
138static struct usb_cdc_header_desc header_desc __initdata = { 138static struct usb_cdc_header_desc header_desc = {
139 .bLength = sizeof header_desc, 139 .bLength = sizeof header_desc,
140 .bDescriptorType = USB_DT_CS_INTERFACE, 140 .bDescriptorType = USB_DT_CS_INTERFACE,
141 .bDescriptorSubType = USB_CDC_HEADER_TYPE, 141 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
@@ -143,7 +143,7 @@ static struct usb_cdc_header_desc header_desc __initdata = {
143 .bcdCDC = cpu_to_le16(0x0110), 143 .bcdCDC = cpu_to_le16(0x0110),
144}; 144};
145 145
146static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor __initdata = { 146static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
147 .bLength = sizeof call_mgmt_descriptor, 147 .bLength = sizeof call_mgmt_descriptor,
148 .bDescriptorType = USB_DT_CS_INTERFACE, 148 .bDescriptorType = USB_DT_CS_INTERFACE,
149 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, 149 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
@@ -152,7 +152,7 @@ static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor __initdata = {
152 .bDataInterface = 0x01, 152 .bDataInterface = 0x01,
153}; 153};
154 154
155static struct usb_cdc_acm_descriptor rndis_acm_descriptor __initdata = { 155static struct usb_cdc_acm_descriptor rndis_acm_descriptor = {
156 .bLength = sizeof rndis_acm_descriptor, 156 .bLength = sizeof rndis_acm_descriptor,
157 .bDescriptorType = USB_DT_CS_INTERFACE, 157 .bDescriptorType = USB_DT_CS_INTERFACE,
158 .bDescriptorSubType = USB_CDC_ACM_TYPE, 158 .bDescriptorSubType = USB_CDC_ACM_TYPE,
@@ -160,7 +160,7 @@ static struct usb_cdc_acm_descriptor rndis_acm_descriptor __initdata = {
160 .bmCapabilities = 0x00, 160 .bmCapabilities = 0x00,
161}; 161};
162 162
163static struct usb_cdc_union_desc rndis_union_desc __initdata = { 163static struct usb_cdc_union_desc rndis_union_desc = {
164 .bLength = sizeof(rndis_union_desc), 164 .bLength = sizeof(rndis_union_desc),
165 .bDescriptorType = USB_DT_CS_INTERFACE, 165 .bDescriptorType = USB_DT_CS_INTERFACE,
166 .bDescriptorSubType = USB_CDC_UNION_TYPE, 166 .bDescriptorSubType = USB_CDC_UNION_TYPE,
@@ -170,7 +170,7 @@ static struct usb_cdc_union_desc rndis_union_desc __initdata = {
170 170
171/* the data interface has two bulk endpoints */ 171/* the data interface has two bulk endpoints */
172 172
173static struct usb_interface_descriptor rndis_data_intf __initdata = { 173static struct usb_interface_descriptor rndis_data_intf = {
174 .bLength = sizeof rndis_data_intf, 174 .bLength = sizeof rndis_data_intf,
175 .bDescriptorType = USB_DT_INTERFACE, 175 .bDescriptorType = USB_DT_INTERFACE,
176 176
@@ -198,7 +198,7 @@ rndis_iad_descriptor = {
198 198
199/* full speed support: */ 199/* full speed support: */
200 200
201static struct usb_endpoint_descriptor fs_notify_desc __initdata = { 201static struct usb_endpoint_descriptor fs_notify_desc = {
202 .bLength = USB_DT_ENDPOINT_SIZE, 202 .bLength = USB_DT_ENDPOINT_SIZE,
203 .bDescriptorType = USB_DT_ENDPOINT, 203 .bDescriptorType = USB_DT_ENDPOINT,
204 204
@@ -208,7 +208,7 @@ static struct usb_endpoint_descriptor fs_notify_desc __initdata = {
208 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC, 208 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
209}; 209};
210 210
211static struct usb_endpoint_descriptor fs_in_desc __initdata = { 211static struct usb_endpoint_descriptor fs_in_desc = {
212 .bLength = USB_DT_ENDPOINT_SIZE, 212 .bLength = USB_DT_ENDPOINT_SIZE,
213 .bDescriptorType = USB_DT_ENDPOINT, 213 .bDescriptorType = USB_DT_ENDPOINT,
214 214
@@ -216,7 +216,7 @@ static struct usb_endpoint_descriptor fs_in_desc __initdata = {
216 .bmAttributes = USB_ENDPOINT_XFER_BULK, 216 .bmAttributes = USB_ENDPOINT_XFER_BULK,
217}; 217};
218 218
219static struct usb_endpoint_descriptor fs_out_desc __initdata = { 219static struct usb_endpoint_descriptor fs_out_desc = {
220 .bLength = USB_DT_ENDPOINT_SIZE, 220 .bLength = USB_DT_ENDPOINT_SIZE,
221 .bDescriptorType = USB_DT_ENDPOINT, 221 .bDescriptorType = USB_DT_ENDPOINT,
222 222
@@ -224,7 +224,7 @@ static struct usb_endpoint_descriptor fs_out_desc __initdata = {
224 .bmAttributes = USB_ENDPOINT_XFER_BULK, 224 .bmAttributes = USB_ENDPOINT_XFER_BULK,
225}; 225};
226 226
227static struct usb_descriptor_header *eth_fs_function[] __initdata = { 227static struct usb_descriptor_header *eth_fs_function[] = {
228 (struct usb_descriptor_header *) &rndis_iad_descriptor, 228 (struct usb_descriptor_header *) &rndis_iad_descriptor,
229 /* control interface matches ACM, not Ethernet */ 229 /* control interface matches ACM, not Ethernet */
230 (struct usb_descriptor_header *) &rndis_control_intf, 230 (struct usb_descriptor_header *) &rndis_control_intf,
@@ -242,7 +242,7 @@ static struct usb_descriptor_header *eth_fs_function[] __initdata = {
242 242
243/* high speed support: */ 243/* high speed support: */
244 244
245static struct usb_endpoint_descriptor hs_notify_desc __initdata = { 245static struct usb_endpoint_descriptor hs_notify_desc = {
246 .bLength = USB_DT_ENDPOINT_SIZE, 246 .bLength = USB_DT_ENDPOINT_SIZE,
247 .bDescriptorType = USB_DT_ENDPOINT, 247 .bDescriptorType = USB_DT_ENDPOINT,
248 248
@@ -251,7 +251,7 @@ static struct usb_endpoint_descriptor hs_notify_desc __initdata = {
251 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT), 251 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
252 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4, 252 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
253}; 253};
254static struct usb_endpoint_descriptor hs_in_desc __initdata = { 254static struct usb_endpoint_descriptor hs_in_desc = {
255 .bLength = USB_DT_ENDPOINT_SIZE, 255 .bLength = USB_DT_ENDPOINT_SIZE,
256 .bDescriptorType = USB_DT_ENDPOINT, 256 .bDescriptorType = USB_DT_ENDPOINT,
257 257
@@ -260,7 +260,7 @@ static struct usb_endpoint_descriptor hs_in_desc __initdata = {
260 .wMaxPacketSize = cpu_to_le16(512), 260 .wMaxPacketSize = cpu_to_le16(512),
261}; 261};
262 262
263static struct usb_endpoint_descriptor hs_out_desc __initdata = { 263static struct usb_endpoint_descriptor hs_out_desc = {
264 .bLength = USB_DT_ENDPOINT_SIZE, 264 .bLength = USB_DT_ENDPOINT_SIZE,
265 .bDescriptorType = USB_DT_ENDPOINT, 265 .bDescriptorType = USB_DT_ENDPOINT,
266 266
@@ -269,7 +269,7 @@ static struct usb_endpoint_descriptor hs_out_desc __initdata = {
269 .wMaxPacketSize = cpu_to_le16(512), 269 .wMaxPacketSize = cpu_to_le16(512),
270}; 270};
271 271
272static struct usb_descriptor_header *eth_hs_function[] __initdata = { 272static struct usb_descriptor_header *eth_hs_function[] = {
273 (struct usb_descriptor_header *) &rndis_iad_descriptor, 273 (struct usb_descriptor_header *) &rndis_iad_descriptor,
274 /* control interface matches ACM, not Ethernet */ 274 /* control interface matches ACM, not Ethernet */
275 (struct usb_descriptor_header *) &rndis_control_intf, 275 (struct usb_descriptor_header *) &rndis_control_intf,
@@ -594,7 +594,7 @@ static void rndis_close(struct gether *geth)
594 594
595/* ethernet function driver setup/binding */ 595/* ethernet function driver setup/binding */
596 596
597static int __init 597static int
598rndis_bind(struct usb_configuration *c, struct usb_function *f) 598rndis_bind(struct usb_configuration *c, struct usb_function *f)
599{ 599{
600 struct usb_composite_dev *cdev = c->cdev; 600 struct usb_composite_dev *cdev = c->cdev;
@@ -786,7 +786,8 @@ static inline bool can_support_rndis(struct usb_configuration *c)
786 * Caller must have called @gether_setup(). Caller is also responsible 786 * Caller must have called @gether_setup(). Caller is also responsible
787 * for calling @gether_cleanup() before module unload. 787 * for calling @gether_cleanup() before module unload.
788 */ 788 */
789int __init rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) 789int
790rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
790{ 791{
791 struct f_rndis *rndis; 792 struct f_rndis *rndis;
792 int status; 793 int status;
diff --git a/drivers/usb/gadget/f_uvc.c b/drivers/usb/gadget/f_uvc.c
new file mode 100644
index 000000000000..fc2611f8b326
--- /dev/null
+++ b/drivers/usb/gadget/f_uvc.c
@@ -0,0 +1,661 @@
1/*
2 * uvc_gadget.c -- USB Video Class Gadget driver
3 *
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/device.h>
16#include <linux/errno.h>
17#include <linux/fs.h>
18#include <linux/list.h>
19#include <linux/mutex.h>
20#include <linux/usb/ch9.h>
21#include <linux/usb/gadget.h>
22#include <linux/usb/video.h>
23#include <linux/vmalloc.h>
24#include <linux/wait.h>
25
26#include <media/v4l2-dev.h>
27#include <media/v4l2-event.h>
28
29#include "uvc.h"
30
31unsigned int uvc_trace_param;
32
33/* --------------------------------------------------------------------------
34 * Function descriptors
35 */
36
37/* string IDs are assigned dynamically */
38
39#define UVC_STRING_ASSOCIATION_IDX 0
40#define UVC_STRING_CONTROL_IDX 1
41#define UVC_STRING_STREAMING_IDX 2
42
43static struct usb_string uvc_en_us_strings[] = {
44 [UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera",
45 [UVC_STRING_CONTROL_IDX].s = "Video Control",
46 [UVC_STRING_STREAMING_IDX].s = "Video Streaming",
47 { }
48};
49
50static struct usb_gadget_strings uvc_stringtab = {
51 .language = 0x0409, /* en-us */
52 .strings = uvc_en_us_strings,
53};
54
55static struct usb_gadget_strings *uvc_function_strings[] = {
56 &uvc_stringtab,
57 NULL,
58};
59
60#define UVC_INTF_VIDEO_CONTROL 0
61#define UVC_INTF_VIDEO_STREAMING 1
62
63static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
64 .bLength = USB_DT_INTERFACE_ASSOCIATION_SIZE,
65 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
66 .bFirstInterface = 0,
67 .bInterfaceCount = 2,
68 .bFunctionClass = USB_CLASS_VIDEO,
69 .bFunctionSubClass = 0x03,
70 .bFunctionProtocol = 0x00,
71 .iFunction = 0,
72};
73
74static struct usb_interface_descriptor uvc_control_intf __initdata = {
75 .bLength = USB_DT_INTERFACE_SIZE,
76 .bDescriptorType = USB_DT_INTERFACE,
77 .bInterfaceNumber = UVC_INTF_VIDEO_CONTROL,
78 .bAlternateSetting = 0,
79 .bNumEndpoints = 1,
80 .bInterfaceClass = USB_CLASS_VIDEO,
81 .bInterfaceSubClass = 0x01,
82 .bInterfaceProtocol = 0x00,
83 .iInterface = 0,
84};
85
86static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
87 .bLength = USB_DT_ENDPOINT_SIZE,
88 .bDescriptorType = USB_DT_ENDPOINT,
89 .bEndpointAddress = USB_DIR_IN,
90 .bmAttributes = USB_ENDPOINT_XFER_INT,
91 .wMaxPacketSize = cpu_to_le16(16),
92 .bInterval = 8,
93};
94
95static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
96 .bLength = UVC_DT_CONTROL_ENDPOINT_SIZE,
97 .bDescriptorType = USB_DT_CS_ENDPOINT,
98 .bDescriptorSubType = UVC_EP_INTERRUPT,
99 .wMaxTransferSize = cpu_to_le16(16),
100};
101
102static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
103 .bLength = USB_DT_INTERFACE_SIZE,
104 .bDescriptorType = USB_DT_INTERFACE,
105 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
106 .bAlternateSetting = 0,
107 .bNumEndpoints = 0,
108 .bInterfaceClass = USB_CLASS_VIDEO,
109 .bInterfaceSubClass = 0x02,
110 .bInterfaceProtocol = 0x00,
111 .iInterface = 0,
112};
113
114static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
115 .bLength = USB_DT_INTERFACE_SIZE,
116 .bDescriptorType = USB_DT_INTERFACE,
117 .bInterfaceNumber = UVC_INTF_VIDEO_STREAMING,
118 .bAlternateSetting = 1,
119 .bNumEndpoints = 1,
120 .bInterfaceClass = USB_CLASS_VIDEO,
121 .bInterfaceSubClass = 0x02,
122 .bInterfaceProtocol = 0x00,
123 .iInterface = 0,
124};
125
126static struct usb_endpoint_descriptor uvc_streaming_ep = {
127 .bLength = USB_DT_ENDPOINT_SIZE,
128 .bDescriptorType = USB_DT_ENDPOINT,
129 .bEndpointAddress = USB_DIR_IN,
130 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
131 .wMaxPacketSize = cpu_to_le16(512),
132 .bInterval = 1,
133};
134
135static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
136 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
137 (struct usb_descriptor_header *) &uvc_streaming_ep,
138 NULL,
139};
140
141static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
142 (struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
143 (struct usb_descriptor_header *) &uvc_streaming_ep,
144 NULL,
145};
146
147/* --------------------------------------------------------------------------
148 * Control requests
149 */
150
151static void
152uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
153{
154 struct uvc_device *uvc = req->context;
155 struct v4l2_event v4l2_event;
156 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
157
158 if (uvc->event_setup_out) {
159 uvc->event_setup_out = 0;
160
161 memset(&v4l2_event, 0, sizeof(v4l2_event));
162 v4l2_event.type = UVC_EVENT_DATA;
163 uvc_event->data.length = req->actual;
164 memcpy(&uvc_event->data.data, req->buf, req->actual);
165 v4l2_event_queue(uvc->vdev, &v4l2_event);
166 }
167}
168
169static int
170uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
171{
172 struct uvc_device *uvc = to_uvc(f);
173 struct v4l2_event v4l2_event;
174 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
175
176 /* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
177 * ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
178 * le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
179 */
180
181 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
182 INFO(f->config->cdev, "invalid request type\n");
183 return -EINVAL;
184 }
185
186 /* Stall too big requests. */
187 if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
188 return -EINVAL;
189
190 memset(&v4l2_event, 0, sizeof(v4l2_event));
191 v4l2_event.type = UVC_EVENT_SETUP;
192 memcpy(&uvc_event->req, ctrl, sizeof(uvc_event->req));
193 v4l2_event_queue(uvc->vdev, &v4l2_event);
194
195 return 0;
196}
197
198static int
199uvc_function_get_alt(struct usb_function *f, unsigned interface)
200{
201 struct uvc_device *uvc = to_uvc(f);
202
203 INFO(f->config->cdev, "uvc_function_get_alt(%u)\n", interface);
204
205 if (interface == uvc->control_intf)
206 return 0;
207 else if (interface != uvc->streaming_intf)
208 return -EINVAL;
209 else
210 return uvc->state == UVC_STATE_STREAMING ? 1 : 0;
211}
212
213static int
214uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
215{
216 struct uvc_device *uvc = to_uvc(f);
217 struct v4l2_event v4l2_event;
218 struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
219
220 INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
221
222 if (interface == uvc->control_intf) {
223 if (alt)
224 return -EINVAL;
225
226 if (uvc->state == UVC_STATE_DISCONNECTED) {
227 memset(&v4l2_event, 0, sizeof(v4l2_event));
228 v4l2_event.type = UVC_EVENT_CONNECT;
229 uvc_event->speed = f->config->cdev->gadget->speed;
230 v4l2_event_queue(uvc->vdev, &v4l2_event);
231
232 uvc->state = UVC_STATE_CONNECTED;
233 }
234
235 return 0;
236 }
237
238 if (interface != uvc->streaming_intf)
239 return -EINVAL;
240
241 /* TODO
242 if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
243 return alt ? -EINVAL : 0;
244 */
245
246 switch (alt) {
247 case 0:
248 if (uvc->state != UVC_STATE_STREAMING)
249 return 0;
250
251 if (uvc->video.ep)
252 usb_ep_disable(uvc->video.ep);
253
254 memset(&v4l2_event, 0, sizeof(v4l2_event));
255 v4l2_event.type = UVC_EVENT_STREAMOFF;
256 v4l2_event_queue(uvc->vdev, &v4l2_event);
257
258 uvc->state = UVC_STATE_CONNECTED;
259 break;
260
261 case 1:
262 if (uvc->state != UVC_STATE_CONNECTED)
263 return 0;
264
265 if (uvc->video.ep)
266 usb_ep_enable(uvc->video.ep, &uvc_streaming_ep);
267
268 memset(&v4l2_event, 0, sizeof(v4l2_event));
269 v4l2_event.type = UVC_EVENT_STREAMON;
270 v4l2_event_queue(uvc->vdev, &v4l2_event);
271
272 uvc->state = UVC_STATE_STREAMING;
273 break;
274
275 default:
276 return -EINVAL;
277 }
278
279 return 0;
280}
281
282static void
283uvc_function_disable(struct usb_function *f)
284{
285 struct uvc_device *uvc = to_uvc(f);
286 struct v4l2_event v4l2_event;
287
288 INFO(f->config->cdev, "uvc_function_disable\n");
289
290 memset(&v4l2_event, 0, sizeof(v4l2_event));
291 v4l2_event.type = UVC_EVENT_DISCONNECT;
292 v4l2_event_queue(uvc->vdev, &v4l2_event);
293
294 uvc->state = UVC_STATE_DISCONNECTED;
295}
296
297/* --------------------------------------------------------------------------
298 * Connection / disconnection
299 */
300
301void
302uvc_function_connect(struct uvc_device *uvc)
303{
304 struct usb_composite_dev *cdev = uvc->func.config->cdev;
305 int ret;
306
307 if ((ret = usb_function_activate(&uvc->func)) < 0)
308 INFO(cdev, "UVC connect failed with %d\n", ret);
309}
310
311void
312uvc_function_disconnect(struct uvc_device *uvc)
313{
314 struct usb_composite_dev *cdev = uvc->func.config->cdev;
315 int ret;
316
317 if ((ret = usb_function_deactivate(&uvc->func)) < 0)
318 INFO(cdev, "UVC disconnect failed with %d\n", ret);
319}
320
321/* --------------------------------------------------------------------------
322 * USB probe and disconnect
323 */
324
325static int
326uvc_register_video(struct uvc_device *uvc)
327{
328 struct usb_composite_dev *cdev = uvc->func.config->cdev;
329 struct video_device *video;
330
331 /* TODO reference counting. */
332 video = video_device_alloc();
333 if (video == NULL)
334 return -ENOMEM;
335
336 video->parent = &cdev->gadget->dev;
337 video->minor = -1;
338 video->fops = &uvc_v4l2_fops;
339 video->release = video_device_release;
340 strncpy(video->name, cdev->gadget->name, sizeof(video->name));
341
342 uvc->vdev = video;
343 video_set_drvdata(video, uvc);
344
345 return video_register_device(video, VFL_TYPE_GRABBER, -1);
346}
347
348#define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
349 do { \
350 memcpy(mem, desc, (desc)->bLength); \
351 *(dst)++ = mem; \
352 mem += (desc)->bLength; \
353 } while (0);
354
355#define UVC_COPY_DESCRIPTORS(mem, dst, src) \
356 do { \
357 const struct usb_descriptor_header * const *__src; \
358 for (__src = src; *__src; ++__src) { \
359 memcpy(mem, *__src, (*__src)->bLength); \
360 *dst++ = mem; \
361 mem += (*__src)->bLength; \
362 } \
363 } while (0)
364
365static struct usb_descriptor_header ** __init
366uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
367{
368 struct uvc_input_header_descriptor *uvc_streaming_header;
369 struct uvc_header_descriptor *uvc_control_header;
370 const struct uvc_descriptor_header * const *uvc_streaming_cls;
371 const struct usb_descriptor_header * const *uvc_streaming_std;
372 const struct usb_descriptor_header * const *src;
373 struct usb_descriptor_header **dst;
374 struct usb_descriptor_header **hdr;
375 unsigned int control_size;
376 unsigned int streaming_size;
377 unsigned int n_desc;
378 unsigned int bytes;
379 void *mem;
380
381 uvc_streaming_cls = (speed == USB_SPEED_FULL)
382 ? uvc->desc.fs_streaming : uvc->desc.hs_streaming;
383 uvc_streaming_std = (speed == USB_SPEED_FULL)
384 ? uvc_fs_streaming : uvc_hs_streaming;
385
386 /* Descriptors layout
387 *
388 * uvc_iad
389 * uvc_control_intf
390 * Class-specific UVC control descriptors
391 * uvc_control_ep
392 * uvc_control_cs_ep
393 * uvc_streaming_intf_alt0
394 * Class-specific UVC streaming descriptors
395 * uvc_{fs|hs}_streaming
396 */
397
398 /* Count descriptors and compute their size. */
399 control_size = 0;
400 streaming_size = 0;
401 bytes = uvc_iad.bLength + uvc_control_intf.bLength
402 + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
403 + uvc_streaming_intf_alt0.bLength;
404 n_desc = 5;
405
406 for (src = (const struct usb_descriptor_header**)uvc->desc.control; *src; ++src) {
407 control_size += (*src)->bLength;
408 bytes += (*src)->bLength;
409 n_desc++;
410 }
411 for (src = (const struct usb_descriptor_header**)uvc_streaming_cls; *src; ++src) {
412 streaming_size += (*src)->bLength;
413 bytes += (*src)->bLength;
414 n_desc++;
415 }
416 for (src = uvc_streaming_std; *src; ++src) {
417 bytes += (*src)->bLength;
418 n_desc++;
419 }
420
421 mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
422 if (mem == NULL)
423 return NULL;
424
425 hdr = mem;
426 dst = mem;
427 mem += (n_desc + 1) * sizeof(*src);
428
429 /* Copy the descriptors. */
430 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
431 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
432
433 uvc_control_header = mem;
434 UVC_COPY_DESCRIPTORS(mem, dst,
435 (const struct usb_descriptor_header**)uvc->desc.control);
436 uvc_control_header->wTotalLength = cpu_to_le16(control_size);
437 uvc_control_header->bInCollection = 1;
438 uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
439
440 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
441 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
442 UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
443
444 uvc_streaming_header = mem;
445 UVC_COPY_DESCRIPTORS(mem, dst,
446 (const struct usb_descriptor_header**)uvc_streaming_cls);
447 uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
448 uvc_streaming_header->bEndpointAddress = uvc_streaming_ep.bEndpointAddress;
449
450 UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
451
452 *dst = NULL;
453 return hdr;
454}
455
456static void
457uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
458{
459 struct usb_composite_dev *cdev = c->cdev;
460 struct uvc_device *uvc = to_uvc(f);
461
462 INFO(cdev, "uvc_function_unbind\n");
463
464 if (uvc->vdev) {
465 if (uvc->vdev->minor == -1)
466 video_device_release(uvc->vdev);
467 else
468 video_unregister_device(uvc->vdev);
469 uvc->vdev = NULL;
470 }
471
472 if (uvc->control_ep)
473 uvc->control_ep->driver_data = NULL;
474 if (uvc->video.ep)
475 uvc->video.ep->driver_data = NULL;
476
477 if (uvc->control_req) {
478 usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
479 kfree(uvc->control_buf);
480 }
481
482 kfree(f->descriptors);
483 kfree(f->hs_descriptors);
484
485 kfree(uvc);
486}
487
488static int __init
489uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
490{
491 struct usb_composite_dev *cdev = c->cdev;
492 struct uvc_device *uvc = to_uvc(f);
493 struct usb_ep *ep;
494 int ret = -EINVAL;
495
496 INFO(cdev, "uvc_function_bind\n");
497
498 /* Allocate endpoints. */
499 ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
500 if (!ep) {
501 INFO(cdev, "Unable to allocate control EP\n");
502 goto error;
503 }
504 uvc->control_ep = ep;
505 ep->driver_data = uvc;
506
507 ep = usb_ep_autoconfig(cdev->gadget, &uvc_streaming_ep);
508 if (!ep) {
509 INFO(cdev, "Unable to allocate streaming EP\n");
510 goto error;
511 }
512 uvc->video.ep = ep;
513 ep->driver_data = uvc;
514
515 /* Allocate interface IDs. */
516 if ((ret = usb_interface_id(c, f)) < 0)
517 goto error;
518 uvc_iad.bFirstInterface = ret;
519 uvc_control_intf.bInterfaceNumber = ret;
520 uvc->control_intf = ret;
521
522 if ((ret = usb_interface_id(c, f)) < 0)
523 goto error;
524 uvc_streaming_intf_alt0.bInterfaceNumber = ret;
525 uvc_streaming_intf_alt1.bInterfaceNumber = ret;
526 uvc->streaming_intf = ret;
527
528 /* Copy descriptors. */
529 f->descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
530 f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
531
532 /* Preallocate control endpoint request. */
533 uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
534 uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
535 if (uvc->control_req == NULL || uvc->control_buf == NULL) {
536 ret = -ENOMEM;
537 goto error;
538 }
539
540 uvc->control_req->buf = uvc->control_buf;
541 uvc->control_req->complete = uvc_function_ep0_complete;
542 uvc->control_req->context = uvc;
543
544 /* Avoid letting this gadget enumerate until the userspace server is
545 * active.
546 */
547 if ((ret = usb_function_deactivate(f)) < 0)
548 goto error;
549
550 /* Initialise video. */
551 ret = uvc_video_init(&uvc->video);
552 if (ret < 0)
553 goto error;
554
555 /* Register a V4L2 device. */
556 ret = uvc_register_video(uvc);
557 if (ret < 0) {
558 printk(KERN_INFO "Unable to register video device\n");
559 goto error;
560 }
561
562 return 0;
563
564error:
565 uvc_function_unbind(c, f);
566 return ret;
567}
568
569/* --------------------------------------------------------------------------
570 * USB gadget function
571 */
572
573/**
574 * uvc_bind_config - add a UVC function to a configuration
575 * @c: the configuration to support the UVC instance
576 * Context: single threaded during gadget setup
577 *
578 * Returns zero on success, else negative errno.
579 *
580 * Caller must have called @uvc_setup(). Caller is also responsible for
581 * calling @uvc_cleanup() before module unload.
582 */
583int __init
584uvc_bind_config(struct usb_configuration *c,
585 const struct uvc_descriptor_header * const *control,
586 const struct uvc_descriptor_header * const *fs_streaming,
587 const struct uvc_descriptor_header * const *hs_streaming)
588{
589 struct uvc_device *uvc;
590 int ret = 0;
591
592 /* TODO Check if the USB device controller supports the required
593 * features.
594 */
595 if (!gadget_is_dualspeed(c->cdev->gadget))
596 return -EINVAL;
597
598 uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
599 if (uvc == NULL)
600 return -ENOMEM;
601
602 uvc->state = UVC_STATE_DISCONNECTED;
603
604 /* Validate the descriptors. */
605 if (control == NULL || control[0] == NULL ||
606 control[0]->bDescriptorSubType != UVC_DT_HEADER)
607 goto error;
608
609 if (fs_streaming == NULL || fs_streaming[0] == NULL ||
610 fs_streaming[0]->bDescriptorSubType != UVC_DT_INPUT_HEADER)
611 goto error;
612
613 if (hs_streaming == NULL || hs_streaming[0] == NULL ||
614 hs_streaming[0]->bDescriptorSubType != UVC_DT_INPUT_HEADER)
615 goto error;
616
617 uvc->desc.control = control;
618 uvc->desc.fs_streaming = fs_streaming;
619 uvc->desc.hs_streaming = hs_streaming;
620
621 /* Allocate string descriptor numbers. */
622 if ((ret = usb_string_id(c->cdev)) < 0)
623 goto error;
624 uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = ret;
625 uvc_iad.iFunction = ret;
626
627 if ((ret = usb_string_id(c->cdev)) < 0)
628 goto error;
629 uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = ret;
630 uvc_control_intf.iInterface = ret;
631
632 if ((ret = usb_string_id(c->cdev)) < 0)
633 goto error;
634 uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id = ret;
635 uvc_streaming_intf_alt0.iInterface = ret;
636 uvc_streaming_intf_alt1.iInterface = ret;
637
638 /* Register the function. */
639 uvc->func.name = "uvc";
640 uvc->func.strings = uvc_function_strings;
641 uvc->func.bind = uvc_function_bind;
642 uvc->func.unbind = uvc_function_unbind;
643 uvc->func.get_alt = uvc_function_get_alt;
644 uvc->func.set_alt = uvc_function_set_alt;
645 uvc->func.disable = uvc_function_disable;
646 uvc->func.setup = uvc_function_setup;
647
648 ret = usb_add_function(c, &uvc->func);
649 if (ret)
650 kfree(uvc);
651
652 return 0;
653
654error:
655 kfree(uvc);
656 return ret;
657}
658
659module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
660MODULE_PARM_DESC(trace, "Trace level bitmask");
661
diff --git a/drivers/usb/gadget/f_uvc.h b/drivers/usb/gadget/f_uvc.h
new file mode 100644
index 000000000000..8a5db7c4fe7c
--- /dev/null
+++ b/drivers/usb/gadget/f_uvc.h
@@ -0,0 +1,376 @@
1/*
2 * f_uvc.h -- USB Video Class Gadget driver
3 *
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#ifndef _F_UVC_H_
15#define _F_UVC_H_
16
17#include <linux/usb/composite.h>
18
19#define USB_CLASS_VIDEO_CONTROL 1
20#define USB_CLASS_VIDEO_STREAMING 2
21
22struct uvc_descriptor_header {
23 __u8 bLength;
24 __u8 bDescriptorType;
25 __u8 bDescriptorSubType;
26} __attribute__ ((packed));
27
28struct uvc_header_descriptor {
29 __u8 bLength;
30 __u8 bDescriptorType;
31 __u8 bDescriptorSubType;
32 __u16 bcdUVC;
33 __u16 wTotalLength;
34 __u32 dwClockFrequency;
35 __u8 bInCollection;
36 __u8 baInterfaceNr[];
37} __attribute__((__packed__));
38
39#define UVC_HEADER_DESCRIPTOR(n) uvc_header_descriptor_##n
40
41#define DECLARE_UVC_HEADER_DESCRIPTOR(n) \
42struct UVC_HEADER_DESCRIPTOR(n) { \
43 __u8 bLength; \
44 __u8 bDescriptorType; \
45 __u8 bDescriptorSubType; \
46 __u16 bcdUVC; \
47 __u16 wTotalLength; \
48 __u32 dwClockFrequency; \
49 __u8 bInCollection; \
50 __u8 baInterfaceNr[n]; \
51} __attribute__ ((packed))
52
53struct uvc_input_terminal_descriptor {
54 __u8 bLength;
55 __u8 bDescriptorType;
56 __u8 bDescriptorSubType;
57 __u8 bTerminalID;
58 __u16 wTerminalType;
59 __u8 bAssocTerminal;
60 __u8 iTerminal;
61} __attribute__((__packed__));
62
63struct uvc_output_terminal_descriptor {
64 __u8 bLength;
65 __u8 bDescriptorType;
66 __u8 bDescriptorSubType;
67 __u8 bTerminalID;
68 __u16 wTerminalType;
69 __u8 bAssocTerminal;
70 __u8 bSourceID;
71 __u8 iTerminal;
72} __attribute__((__packed__));
73
74struct uvc_camera_terminal_descriptor {
75 __u8 bLength;
76 __u8 bDescriptorType;
77 __u8 bDescriptorSubType;
78 __u8 bTerminalID;
79 __u16 wTerminalType;
80 __u8 bAssocTerminal;
81 __u8 iTerminal;
82 __u16 wObjectiveFocalLengthMin;
83 __u16 wObjectiveFocalLengthMax;
84 __u16 wOcularFocalLength;
85 __u8 bControlSize;
86 __u8 bmControls[3];
87} __attribute__((__packed__));
88
89struct uvc_selector_unit_descriptor {
90 __u8 bLength;
91 __u8 bDescriptorType;
92 __u8 bDescriptorSubType;
93 __u8 bUnitID;
94 __u8 bNrInPins;
95 __u8 baSourceID[0];
96 __u8 iSelector;
97} __attribute__((__packed__));
98
99#define UVC_SELECTOR_UNIT_DESCRIPTOR(n) \
100 uvc_selector_unit_descriptor_##n
101
102#define DECLARE_UVC_SELECTOR_UNIT_DESCRIPTOR(n) \
103struct UVC_SELECTOR_UNIT_DESCRIPTOR(n) { \
104 __u8 bLength; \
105 __u8 bDescriptorType; \
106 __u8 bDescriptorSubType; \
107 __u8 bUnitID; \
108 __u8 bNrInPins; \
109 __u8 baSourceID[n]; \
110 __u8 iSelector; \
111} __attribute__ ((packed))
112
113struct uvc_processing_unit_descriptor {
114 __u8 bLength;
115 __u8 bDescriptorType;
116 __u8 bDescriptorSubType;
117 __u8 bUnitID;
118 __u8 bSourceID;
119 __u16 wMaxMultiplier;
120 __u8 bControlSize;
121 __u8 bmControls[2];
122 __u8 iProcessing;
123} __attribute__((__packed__));
124
125struct uvc_extension_unit_descriptor {
126 __u8 bLength;
127 __u8 bDescriptorType;
128 __u8 bDescriptorSubType;
129 __u8 bUnitID;
130 __u8 guidExtensionCode[16];
131 __u8 bNumControls;
132 __u8 bNrInPins;
133 __u8 baSourceID[0];
134 __u8 bControlSize;
135 __u8 bmControls[0];
136 __u8 iExtension;
137} __attribute__((__packed__));
138
139#define UVC_EXTENSION_UNIT_DESCRIPTOR(p, n) \
140 uvc_extension_unit_descriptor_##p_##n
141
142#define DECLARE_UVC_EXTENSION_UNIT_DESCRIPTOR(p, n) \
143struct UVC_EXTENSION_UNIT_DESCRIPTOR(p, n) { \
144 __u8 bLength; \
145 __u8 bDescriptorType; \
146 __u8 bDescriptorSubType; \
147 __u8 bUnitID; \
148 __u8 guidExtensionCode[16]; \
149 __u8 bNumControls; \
150 __u8 bNrInPins; \
151 __u8 baSourceID[p]; \
152 __u8 bControlSize; \
153 __u8 bmControls[n]; \
154 __u8 iExtension; \
155} __attribute__ ((packed))
156
157struct uvc_control_endpoint_descriptor {
158 __u8 bLength;
159 __u8 bDescriptorType;
160 __u8 bDescriptorSubType;
161 __u16 wMaxTransferSize;
162} __attribute__((__packed__));
163
164#define UVC_DT_HEADER 1
165#define UVC_DT_INPUT_TERMINAL 2
166#define UVC_DT_OUTPUT_TERMINAL 3
167#define UVC_DT_SELECTOR_UNIT 4
168#define UVC_DT_PROCESSING_UNIT 5
169#define UVC_DT_EXTENSION_UNIT 6
170
171#define UVC_DT_HEADER_SIZE(n) (12+(n))
172#define UVC_DT_INPUT_TERMINAL_SIZE 8
173#define UVC_DT_OUTPUT_TERMINAL_SIZE 9
174#define UVC_DT_CAMERA_TERMINAL_SIZE(n) (15+(n))
175#define UVC_DT_SELECTOR_UNIT_SIZE(n) (6+(n))
176#define UVC_DT_PROCESSING_UNIT_SIZE(n) (9+(n))
177#define UVC_DT_EXTENSION_UNIT_SIZE(p,n) (24+(p)+(n))
178#define UVC_DT_CONTROL_ENDPOINT_SIZE 5
179
180struct uvc_input_header_descriptor {
181 __u8 bLength;
182 __u8 bDescriptorType;
183 __u8 bDescriptorSubType;
184 __u8 bNumFormats;
185 __u16 wTotalLength;
186 __u8 bEndpointAddress;
187 __u8 bmInfo;
188 __u8 bTerminalLink;
189 __u8 bStillCaptureMethod;
190 __u8 bTriggerSupport;
191 __u8 bTriggerUsage;
192 __u8 bControlSize;
193 __u8 bmaControls[];
194} __attribute__((__packed__));
195
196#define UVC_INPUT_HEADER_DESCRIPTOR(n, p) \
197 uvc_input_header_descriptor_##n_##p
198
199#define DECLARE_UVC_INPUT_HEADER_DESCRIPTOR(n, p) \
200struct UVC_INPUT_HEADER_DESCRIPTOR(n, p) { \
201 __u8 bLength; \
202 __u8 bDescriptorType; \
203 __u8 bDescriptorSubType; \
204 __u8 bNumFormats; \
205 __u16 wTotalLength; \
206 __u8 bEndpointAddress; \
207 __u8 bmInfo; \
208 __u8 bTerminalLink; \
209 __u8 bStillCaptureMethod; \
210 __u8 bTriggerSupport; \
211 __u8 bTriggerUsage; \
212 __u8 bControlSize; \
213 __u8 bmaControls[p][n]; \
214} __attribute__ ((packed))
215
216struct uvc_output_header_descriptor {
217 __u8 bLength;
218 __u8 bDescriptorType;
219 __u8 bDescriptorSubType;
220 __u8 bNumFormats;
221 __u16 wTotalLength;
222 __u8 bEndpointAddress;
223 __u8 bTerminalLink;
224 __u8 bControlSize;
225 __u8 bmaControls[];
226} __attribute__((__packed__));
227
228#define UVC_OUTPUT_HEADER_DESCRIPTOR(n, p) \
229 uvc_output_header_descriptor_##n_##p
230
231#define DECLARE_UVC_OUTPUT_HEADER_DESCRIPTOR(n, p) \
232struct UVC_OUTPUT_HEADER_DESCRIPTOR(n, p) { \
233 __u8 bLength; \
234 __u8 bDescriptorType; \
235 __u8 bDescriptorSubType; \
236 __u8 bNumFormats; \
237 __u16 wTotalLength; \
238 __u8 bEndpointAddress; \
239 __u8 bTerminalLink; \
240 __u8 bControlSize; \
241 __u8 bmaControls[p][n]; \
242} __attribute__ ((packed))
243
244struct uvc_format_uncompressed {
245 __u8 bLength;
246 __u8 bDescriptorType;
247 __u8 bDescriptorSubType;
248 __u8 bFormatIndex;
249 __u8 bNumFrameDescriptors;
250 __u8 guidFormat[16];
251 __u8 bBitsPerPixel;
252 __u8 bDefaultFrameIndex;
253 __u8 bAspectRatioX;
254 __u8 bAspectRatioY;
255 __u8 bmInterfaceFlags;
256 __u8 bCopyProtect;
257} __attribute__((__packed__));
258
259struct uvc_frame_uncompressed {
260 __u8 bLength;
261 __u8 bDescriptorType;
262 __u8 bDescriptorSubType;
263 __u8 bFrameIndex;
264 __u8 bmCapabilities;
265 __u16 wWidth;
266 __u16 wHeight;
267 __u32 dwMinBitRate;
268 __u32 dwMaxBitRate;
269 __u32 dwMaxVideoFrameBufferSize;
270 __u32 dwDefaultFrameInterval;
271 __u8 bFrameIntervalType;
272 __u32 dwFrameInterval[];
273} __attribute__((__packed__));
274
275#define UVC_FRAME_UNCOMPRESSED(n) \
276 uvc_frame_uncompressed_##n
277
278#define DECLARE_UVC_FRAME_UNCOMPRESSED(n) \
279struct UVC_FRAME_UNCOMPRESSED(n) { \
280 __u8 bLength; \
281 __u8 bDescriptorType; \
282 __u8 bDescriptorSubType; \
283 __u8 bFrameIndex; \
284 __u8 bmCapabilities; \
285 __u16 wWidth; \
286 __u16 wHeight; \
287 __u32 dwMinBitRate; \
288 __u32 dwMaxBitRate; \
289 __u32 dwMaxVideoFrameBufferSize; \
290 __u32 dwDefaultFrameInterval; \
291 __u8 bFrameIntervalType; \
292 __u32 dwFrameInterval[n]; \
293} __attribute__ ((packed))
294
295struct uvc_format_mjpeg {
296 __u8 bLength;
297 __u8 bDescriptorType;
298 __u8 bDescriptorSubType;
299 __u8 bFormatIndex;
300 __u8 bNumFrameDescriptors;
301 __u8 bmFlags;
302 __u8 bDefaultFrameIndex;
303 __u8 bAspectRatioX;
304 __u8 bAspectRatioY;
305 __u8 bmInterfaceFlags;
306 __u8 bCopyProtect;
307} __attribute__((__packed__));
308
309struct uvc_frame_mjpeg {
310 __u8 bLength;
311 __u8 bDescriptorType;
312 __u8 bDescriptorSubType;
313 __u8 bFrameIndex;
314 __u8 bmCapabilities;
315 __u16 wWidth;
316 __u16 wHeight;
317 __u32 dwMinBitRate;
318 __u32 dwMaxBitRate;
319 __u32 dwMaxVideoFrameBufferSize;
320 __u32 dwDefaultFrameInterval;
321 __u8 bFrameIntervalType;
322 __u32 dwFrameInterval[];
323} __attribute__((__packed__));
324
325#define UVC_FRAME_MJPEG(n) \
326 uvc_frame_mjpeg_##n
327
328#define DECLARE_UVC_FRAME_MJPEG(n) \
329struct UVC_FRAME_MJPEG(n) { \
330 __u8 bLength; \
331 __u8 bDescriptorType; \
332 __u8 bDescriptorSubType; \
333 __u8 bFrameIndex; \
334 __u8 bmCapabilities; \
335 __u16 wWidth; \
336 __u16 wHeight; \
337 __u32 dwMinBitRate; \
338 __u32 dwMaxBitRate; \
339 __u32 dwMaxVideoFrameBufferSize; \
340 __u32 dwDefaultFrameInterval; \
341 __u8 bFrameIntervalType; \
342 __u32 dwFrameInterval[n]; \
343} __attribute__ ((packed))
344
345struct uvc_color_matching_descriptor {
346 __u8 bLength;
347 __u8 bDescriptorType;
348 __u8 bDescriptorSubType;
349 __u8 bColorPrimaries;
350 __u8 bTransferCharacteristics;
351 __u8 bMatrixCoefficients;
352} __attribute__((__packed__));
353
354#define UVC_DT_INPUT_HEADER 1
355#define UVC_DT_OUTPUT_HEADER 2
356#define UVC_DT_FORMAT_UNCOMPRESSED 4
357#define UVC_DT_FRAME_UNCOMPRESSED 5
358#define UVC_DT_FORMAT_MJPEG 6
359#define UVC_DT_FRAME_MJPEG 7
360#define UVC_DT_COLOR_MATCHING 13
361
362#define UVC_DT_INPUT_HEADER_SIZE(n, p) (13+(n*p))
363#define UVC_DT_OUTPUT_HEADER_SIZE(n, p) (9+(n*p))
364#define UVC_DT_FORMAT_UNCOMPRESSED_SIZE 27
365#define UVC_DT_FRAME_UNCOMPRESSED_SIZE(n) (26+4*(n))
366#define UVC_DT_FORMAT_MJPEG_SIZE 11
367#define UVC_DT_FRAME_MJPEG_SIZE(n) (26+4*(n))
368#define UVC_DT_COLOR_MATCHING_SIZE 6
369
370extern int uvc_bind_config(struct usb_configuration *c,
371 const struct uvc_descriptor_header * const *control,
372 const struct uvc_descriptor_header * const *fs_streaming,
373 const struct uvc_descriptor_header * const *hs_streaming);
374
375#endif /* _F_UVC_H_ */
376
diff --git a/drivers/usb/gadget/fsl_mx3_udc.c b/drivers/usb/gadget/fsl_mxc_udc.c
index 20a802ecaa15..d0b8bde59e59 100644
--- a/drivers/usb/gadget/fsl_mx3_udc.c
+++ b/drivers/usb/gadget/fsl_mxc_udc.c
@@ -50,12 +50,14 @@ int fsl_udc_clk_init(struct platform_device *pdev)
50 goto egusb; 50 goto egusb;
51 } 51 }
52 52
53 freq = clk_get_rate(mxc_usb_clk); 53 if (!cpu_is_mx51()) {
54 if (pdata->phy_mode != FSL_USB2_PHY_ULPI && 54 freq = clk_get_rate(mxc_usb_clk);
55 (freq < 59999000 || freq > 60001000)) { 55 if (pdata->phy_mode != FSL_USB2_PHY_ULPI &&
56 dev_err(&pdev->dev, "USB_CLK=%lu, should be 60MHz\n", freq); 56 (freq < 59999000 || freq > 60001000)) {
57 ret = -EINVAL; 57 dev_err(&pdev->dev, "USB_CLK=%lu, should be 60MHz\n", freq);
58 goto eclkrate; 58 ret = -EINVAL;
59 goto eclkrate;
60 }
59 } 61 }
60 62
61 ret = clk_enable(mxc_usb_clk); 63 ret = clk_enable(mxc_usb_clk);
diff --git a/drivers/usb/gadget/fsl_udc_core.c b/drivers/usb/gadget/fsl_udc_core.c
index fa3d142ba64d..08a9a62a39e3 100644
--- a/drivers/usb/gadget/fsl_udc_core.c
+++ b/drivers/usb/gadget/fsl_udc_core.c
@@ -489,7 +489,7 @@ static int fsl_ep_enable(struct usb_ep *_ep,
489 case USB_ENDPOINT_XFER_ISOC: 489 case USB_ENDPOINT_XFER_ISOC:
490 /* Calculate transactions needed for high bandwidth iso */ 490 /* Calculate transactions needed for high bandwidth iso */
491 mult = (unsigned char)(1 + ((max >> 11) & 0x03)); 491 mult = (unsigned char)(1 + ((max >> 11) & 0x03));
492 max = max & 0x8ff; /* bit 0~10 */ 492 max = max & 0x7ff; /* bit 0~10 */
493 /* 3 transactions at most */ 493 /* 3 transactions at most */
494 if (mult > 3) 494 if (mult > 3)
495 goto en_done; 495 goto en_done;
diff --git a/drivers/usb/gadget/g_ffs.c b/drivers/usb/gadget/g_ffs.c
new file mode 100644
index 000000000000..4b0e4a040d6f
--- /dev/null
+++ b/drivers/usb/gadget/g_ffs.c
@@ -0,0 +1,426 @@
1#include <linux/module.h>
2#include <linux/utsname.h>
3
4
5/*
6 * kbuild is not very cooperative with respect to linking separately
7 * compiled library objects into one module. So for now we won't use
8 * separate compilation ... ensuring init/exit sections work to shrink
9 * the runtime footprint, and giving us at least some parts of what
10 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
11 */
12
13#include "composite.c"
14#include "usbstring.c"
15#include "config.c"
16#include "epautoconf.c"
17
18#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
19# if defined USB_ETH_RNDIS
20# undef USB_ETH_RNDIS
21# endif
22# ifdef CONFIG_USB_FUNCTIONFS_RNDIS
23# define USB_ETH_RNDIS y
24# endif
25
26# include "f_ecm.c"
27# include "f_subset.c"
28# ifdef USB_ETH_RNDIS
29# include "f_rndis.c"
30# include "rndis.c"
31# endif
32# include "u_ether.c"
33
34static u8 gfs_hostaddr[ETH_ALEN];
35#else
36# if !defined CONFIG_USB_FUNCTIONFS_GENERIC
37# define CONFIG_USB_FUNCTIONFS_GENERIC
38# endif
39# define gether_cleanup() do { } while (0)
40# define gether_setup(gadget, hostaddr) ((int)0)
41#endif
42
43#include "f_fs.c"
44
45
46#define DRIVER_NAME "g_ffs"
47#define DRIVER_DESC "USB Function Filesystem"
48#define DRIVER_VERSION "24 Aug 2004"
49
50MODULE_DESCRIPTION(DRIVER_DESC);
51MODULE_AUTHOR("Michal Nazarewicz");
52MODULE_LICENSE("GPL");
53
54
55static unsigned short gfs_vendor_id = 0x0525; /* XXX NetChip */
56static unsigned short gfs_product_id = 0xa4ac; /* XXX */
57
58static struct usb_device_descriptor gfs_dev_desc = {
59 .bLength = sizeof gfs_dev_desc,
60 .bDescriptorType = USB_DT_DEVICE,
61
62 .bcdUSB = cpu_to_le16(0x0200),
63 .bDeviceClass = USB_CLASS_PER_INTERFACE,
64
65 /* Vendor and product id can be overridden by module parameters. */
66 /* .idVendor = cpu_to_le16(gfs_vendor_id), */
67 /* .idProduct = cpu_to_le16(gfs_product_id), */
68 /* .bcdDevice = f(hardware) */
69 /* .iManufacturer = DYNAMIC */
70 /* .iProduct = DYNAMIC */
71 /* NO SERIAL NUMBER */
72 .bNumConfigurations = 1,
73};
74
75#define GFS_MODULE_PARAM_DESC(name, field) \
76 MODULE_PARM_DESC(name, "Value of the " #field " field of the device descriptor sent to the host. Takes effect only prior to the user-space driver registering to the FunctionFS.")
77
78module_param_named(usb_class, gfs_dev_desc.bDeviceClass, byte, 0644);
79GFS_MODULE_PARAM_DESC(usb_class, bDeviceClass);
80module_param_named(usb_subclass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
81GFS_MODULE_PARAM_DESC(usb_subclass, bDeviceSubClass);
82module_param_named(usb_protocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
83GFS_MODULE_PARAM_DESC(usb_protocol, bDeviceProtocol);
84module_param_named(usb_vendor, gfs_vendor_id, ushort, 0644);
85GFS_MODULE_PARAM_DESC(usb_vendor, idVendor);
86module_param_named(usb_product, gfs_product_id, ushort, 0644);
87GFS_MODULE_PARAM_DESC(usb_product, idProduct);
88
89
90
91static const struct usb_descriptor_header *gfs_otg_desc[] = {
92 (const struct usb_descriptor_header *)
93 &(const struct usb_otg_descriptor) {
94 .bLength = sizeof(struct usb_otg_descriptor),
95 .bDescriptorType = USB_DT_OTG,
96
97 /* REVISIT SRP-only hardware is possible, although
98 * it would not be called "OTG" ... */
99 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
100 },
101
102 NULL
103};
104
105/* string IDs are assigned dynamically */
106
107enum {
108 GFS_STRING_MANUFACTURER_IDX,
109 GFS_STRING_PRODUCT_IDX,
110#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
111 GFS_STRING_RNDIS_CONFIG_IDX,
112#endif
113#ifdef CONFIG_USB_FUNCTIONFS_ETH
114 GFS_STRING_ECM_CONFIG_IDX,
115#endif
116#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
117 GFS_STRING_GENERIC_CONFIG_IDX,
118#endif
119};
120
121static char gfs_manufacturer[50];
122static const char gfs_driver_desc[] = DRIVER_DESC;
123static const char gfs_short_name[] = DRIVER_NAME;
124
125static struct usb_string gfs_strings[] = {
126 [GFS_STRING_MANUFACTURER_IDX].s = gfs_manufacturer,
127 [GFS_STRING_PRODUCT_IDX].s = gfs_driver_desc,
128#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
129 [GFS_STRING_RNDIS_CONFIG_IDX].s = "FunctionFS + RNDIS",
130#endif
131#ifdef CONFIG_USB_FUNCTIONFS_ETH
132 [GFS_STRING_ECM_CONFIG_IDX].s = "FunctionFS + ECM",
133#endif
134#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
135 [GFS_STRING_GENERIC_CONFIG_IDX].s = "FunctionFS",
136#endif
137 { } /* end of list */
138};
139
140static struct usb_gadget_strings *gfs_dev_strings[] = {
141 &(struct usb_gadget_strings) {
142 .language = 0x0409, /* en-us */
143 .strings = gfs_strings,
144 },
145 NULL,
146};
147
148
149#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
150static int gfs_do_rndis_config(struct usb_configuration *c);
151
152static struct usb_configuration gfs_rndis_config_driver = {
153 .label = "FunctionFS + RNDIS",
154 .bind = gfs_do_rndis_config,
155 .bConfigurationValue = 1,
156 /* .iConfiguration = DYNAMIC */
157 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
158};
159# define gfs_add_rndis_config(cdev) \
160 usb_add_config(cdev, &gfs_rndis_config_driver)
161#else
162# define gfs_add_rndis_config(cdev) 0
163#endif
164
165
166#ifdef CONFIG_USB_FUNCTIONFS_ETH
167static int gfs_do_ecm_config(struct usb_configuration *c);
168
169static struct usb_configuration gfs_ecm_config_driver = {
170 .label = "FunctionFS + ECM",
171 .bind = gfs_do_ecm_config,
172 .bConfigurationValue = 1,
173 /* .iConfiguration = DYNAMIC */
174 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
175};
176# define gfs_add_ecm_config(cdev) \
177 usb_add_config(cdev, &gfs_ecm_config_driver)
178#else
179# define gfs_add_ecm_config(cdev) 0
180#endif
181
182
183#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
184static int gfs_do_generic_config(struct usb_configuration *c);
185
186static struct usb_configuration gfs_generic_config_driver = {
187 .label = "FunctionFS",
188 .bind = gfs_do_generic_config,
189 .bConfigurationValue = 2,
190 /* .iConfiguration = DYNAMIC */
191 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
192};
193# define gfs_add_generic_config(cdev) \
194 usb_add_config(cdev, &gfs_generic_config_driver)
195#else
196# define gfs_add_generic_config(cdev) 0
197#endif
198
199
200static int gfs_bind(struct usb_composite_dev *cdev);
201static int gfs_unbind(struct usb_composite_dev *cdev);
202
203static struct usb_composite_driver gfs_driver = {
204 .name = gfs_short_name,
205 .dev = &gfs_dev_desc,
206 .strings = gfs_dev_strings,
207 .bind = gfs_bind,
208 .unbind = gfs_unbind,
209};
210
211
212static struct ffs_data *gfs_ffs_data;
213static unsigned long gfs_registered;
214
215
216static int gfs_init(void)
217{
218 ENTER();
219
220 return functionfs_init();
221}
222module_init(gfs_init);
223
224static void gfs_exit(void)
225{
226 ENTER();
227
228 if (test_and_clear_bit(0, &gfs_registered))
229 usb_composite_unregister(&gfs_driver);
230
231 functionfs_cleanup();
232}
233module_exit(gfs_exit);
234
235
236static int functionfs_ready_callback(struct ffs_data *ffs)
237{
238 int ret;
239
240 ENTER();
241
242 if (WARN_ON(test_and_set_bit(0, &gfs_registered)))
243 return -EBUSY;
244
245 gfs_ffs_data = ffs;
246 ret = usb_composite_register(&gfs_driver);
247 if (unlikely(ret < 0))
248 clear_bit(0, &gfs_registered);
249 return ret;
250}
251
252static void functionfs_closed_callback(struct ffs_data *ffs)
253{
254 ENTER();
255
256 if (test_and_clear_bit(0, &gfs_registered))
257 usb_composite_unregister(&gfs_driver);
258}
259
260
261static int functionfs_check_dev_callback(const char *dev_name)
262{
263 return 0;
264}
265
266
267
268static int gfs_bind(struct usb_composite_dev *cdev)
269{
270 int ret;
271
272 ENTER();
273
274 if (WARN_ON(!gfs_ffs_data))
275 return -ENODEV;
276
277 ret = gether_setup(cdev->gadget, gfs_hostaddr);
278 if (unlikely(ret < 0))
279 goto error_quick;
280
281 gfs_dev_desc.idVendor = cpu_to_le16(gfs_vendor_id);
282 gfs_dev_desc.idProduct = cpu_to_le16(gfs_product_id);
283
284 snprintf(gfs_manufacturer, sizeof gfs_manufacturer, "%s %s with %s",
285 init_utsname()->sysname, init_utsname()->release,
286 cdev->gadget->name);
287 ret = usb_string_id(cdev);
288 if (unlikely(ret < 0))
289 goto error;
290 gfs_strings[GFS_STRING_MANUFACTURER_IDX].id = ret;
291 gfs_dev_desc.iManufacturer = ret;
292
293 ret = usb_string_id(cdev);
294 if (unlikely(ret < 0))
295 goto error;
296 gfs_strings[GFS_STRING_PRODUCT_IDX].id = ret;
297 gfs_dev_desc.iProduct = ret;
298
299#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
300 ret = usb_string_id(cdev);
301 if (unlikely(ret < 0))
302 goto error;
303 gfs_strings[GFS_STRING_RNDIS_CONFIG_IDX].id = ret;
304 gfs_rndis_config_driver.iConfiguration = ret;
305#endif
306
307#ifdef CONFIG_USB_FUNCTIONFS_ETH
308 ret = usb_string_id(cdev);
309 if (unlikely(ret < 0))
310 goto error;
311 gfs_strings[GFS_STRING_ECM_CONFIG_IDX].id = ret;
312 gfs_ecm_config_driver.iConfiguration = ret;
313#endif
314
315#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
316 ret = usb_string_id(cdev);
317 if (unlikely(ret < 0))
318 goto error;
319 gfs_strings[GFS_STRING_GENERIC_CONFIG_IDX].id = ret;
320 gfs_generic_config_driver.iConfiguration = ret;
321#endif
322
323 ret = functionfs_bind(gfs_ffs_data, cdev);
324 if (unlikely(ret < 0))
325 goto error;
326
327 ret = gfs_add_rndis_config(cdev);
328 if (unlikely(ret < 0))
329 goto error_unbind;
330
331 ret = gfs_add_ecm_config(cdev);
332 if (unlikely(ret < 0))
333 goto error_unbind;
334
335 ret = gfs_add_generic_config(cdev);
336 if (unlikely(ret < 0))
337 goto error_unbind;
338
339 return 0;
340
341error_unbind:
342 functionfs_unbind(gfs_ffs_data);
343error:
344 gether_cleanup();
345error_quick:
346 gfs_ffs_data = NULL;
347 return ret;
348}
349
350static int gfs_unbind(struct usb_composite_dev *cdev)
351{
352 ENTER();
353
354 /* We may have been called in an error recovery frem
355 * composite_bind() after gfs_unbind() failure so we need to
356 * check if gfs_ffs_data is not NULL since gfs_bind() handles
357 * all error recovery itself. I'd rather we werent called
358 * from composite on orror recovery, but what you're gonna
359 * do...? */
360
361 if (gfs_ffs_data) {
362 gether_cleanup();
363 functionfs_unbind(gfs_ffs_data);
364 gfs_ffs_data = NULL;
365 }
366
367 return 0;
368}
369
370
371static int __gfs_do_config(struct usb_configuration *c,
372 int (*eth)(struct usb_configuration *c, u8 *ethaddr),
373 u8 *ethaddr)
374{
375 int ret;
376
377 if (WARN_ON(!gfs_ffs_data))
378 return -ENODEV;
379
380 if (gadget_is_otg(c->cdev->gadget)) {
381 c->descriptors = gfs_otg_desc;
382 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
383 }
384
385 if (eth) {
386 ret = eth(c, ethaddr);
387 if (unlikely(ret < 0))
388 return ret;
389 }
390
391 ret = functionfs_add(c->cdev, c, gfs_ffs_data);
392 if (unlikely(ret < 0))
393 return ret;
394
395 return 0;
396}
397
398#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
399static int gfs_do_rndis_config(struct usb_configuration *c)
400{
401 ENTER();
402
403 return __gfs_do_config(c, rndis_bind_config, gfs_hostaddr);
404}
405#endif
406
407#ifdef CONFIG_USB_FUNCTIONFS_ETH
408static int gfs_do_ecm_config(struct usb_configuration *c)
409{
410 ENTER();
411
412 return __gfs_do_config(c,
413 can_support_ecm(c->cdev->gadget)
414 ? ecm_bind_config : geth_bind_config,
415 gfs_hostaddr);
416}
417#endif
418
419#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
420static int gfs_do_generic_config(struct usb_configuration *c)
421{
422 ENTER();
423
424 return __gfs_do_config(c, NULL, NULL);
425}
426#endif
diff --git a/drivers/usb/gadget/hid.c b/drivers/usb/gadget/hid.c
new file mode 100644
index 000000000000..775722686ed8
--- /dev/null
+++ b/drivers/usb/gadget/hid.c
@@ -0,0 +1,298 @@
1/*
2 * hid.c -- HID Composite driver
3 *
4 * Based on multi.c
5 *
6 * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23
24#include <linux/kernel.h>
25#include <linux/platform_device.h>
26#include <linux/list.h>
27
28#define DRIVER_DESC "HID Gadget"
29#define DRIVER_VERSION "2010/03/16"
30
31/*-------------------------------------------------------------------------*/
32
33#define HIDG_VENDOR_NUM 0x0525 /* XXX NetChip */
34#define HIDG_PRODUCT_NUM 0xa4ac /* Linux-USB HID gadget */
35
36/*-------------------------------------------------------------------------*/
37
38/*
39 * kbuild is not very cooperative with respect to linking separately
40 * compiled library objects into one module. So for now we won't use
41 * separate compilation ... ensuring init/exit sections work to shrink
42 * the runtime footprint, and giving us at least some parts of what
43 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
44 */
45
46#include "composite.c"
47#include "usbstring.c"
48#include "config.c"
49#include "epautoconf.c"
50
51#include "f_hid.c"
52
53
54struct hidg_func_node {
55 struct list_head node;
56 struct hidg_func_descriptor *func;
57};
58
59static LIST_HEAD(hidg_func_list);
60
61/*-------------------------------------------------------------------------*/
62
63static struct usb_device_descriptor device_desc = {
64 .bLength = sizeof device_desc,
65 .bDescriptorType = USB_DT_DEVICE,
66
67 .bcdUSB = cpu_to_le16(0x0200),
68
69 /* .bDeviceClass = USB_CLASS_COMM, */
70 /* .bDeviceSubClass = 0, */
71 /* .bDeviceProtocol = 0, */
72 .bDeviceClass = 0xEF,
73 .bDeviceSubClass = 2,
74 .bDeviceProtocol = 1,
75 /* .bMaxPacketSize0 = f(hardware) */
76
77 /* Vendor and product id can be overridden by module parameters. */
78 .idVendor = cpu_to_le16(HIDG_VENDOR_NUM),
79 .idProduct = cpu_to_le16(HIDG_PRODUCT_NUM),
80 /* .bcdDevice = f(hardware) */
81 /* .iManufacturer = DYNAMIC */
82 /* .iProduct = DYNAMIC */
83 /* NO SERIAL NUMBER */
84 .bNumConfigurations = 1,
85};
86
87static struct usb_otg_descriptor otg_descriptor = {
88 .bLength = sizeof otg_descriptor,
89 .bDescriptorType = USB_DT_OTG,
90
91 /* REVISIT SRP-only hardware is possible, although
92 * it would not be called "OTG" ...
93 */
94 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
95};
96
97static const struct usb_descriptor_header *otg_desc[] = {
98 (struct usb_descriptor_header *) &otg_descriptor,
99 NULL,
100};
101
102
103/* string IDs are assigned dynamically */
104
105#define STRING_MANUFACTURER_IDX 0
106#define STRING_PRODUCT_IDX 1
107
108static char manufacturer[50];
109
110static struct usb_string strings_dev[] = {
111 [STRING_MANUFACTURER_IDX].s = manufacturer,
112 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
113 { } /* end of list */
114};
115
116static struct usb_gadget_strings stringtab_dev = {
117 .language = 0x0409, /* en-us */
118 .strings = strings_dev,
119};
120
121static struct usb_gadget_strings *dev_strings[] = {
122 &stringtab_dev,
123 NULL,
124};
125
126
127
128/****************************** Configurations ******************************/
129
130static int __init do_config(struct usb_configuration *c)
131{
132 struct hidg_func_node *e;
133 int func = 0, status = 0;
134
135 if (gadget_is_otg(c->cdev->gadget)) {
136 c->descriptors = otg_desc;
137 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
138 }
139
140 list_for_each_entry(e, &hidg_func_list, node) {
141 status = hidg_bind_config(c, e->func, func++);
142 if (status)
143 break;
144 }
145
146 return status;
147}
148
149static struct usb_configuration config_driver = {
150 .label = "HID Gadget",
151 .bind = do_config,
152 .bConfigurationValue = 1,
153 /* .iConfiguration = DYNAMIC */
154 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
155};
156
157/****************************** Gadget Bind ******************************/
158
159static int __init hid_bind(struct usb_composite_dev *cdev)
160{
161 struct usb_gadget *gadget = cdev->gadget;
162 struct list_head *tmp;
163 int status, gcnum, funcs = 0;
164
165 list_for_each(tmp, &hidg_func_list)
166 funcs++;
167
168 if (!funcs)
169 return -ENODEV;
170
171 /* set up HID */
172 status = ghid_setup(cdev->gadget, funcs);
173 if (status < 0)
174 return status;
175
176 gcnum = usb_gadget_controller_number(gadget);
177 if (gcnum >= 0)
178 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
179 else
180 device_desc.bcdDevice = cpu_to_le16(0x0300 | 0x0099);
181
182
183 /* Allocate string descriptor numbers ... note that string
184 * contents can be overridden by the composite_dev glue.
185 */
186
187 /* device descriptor strings: manufacturer, product */
188 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
189 init_utsname()->sysname, init_utsname()->release,
190 gadget->name);
191 status = usb_string_id(cdev);
192 if (status < 0)
193 return status;
194 strings_dev[STRING_MANUFACTURER_IDX].id = status;
195 device_desc.iManufacturer = status;
196
197 status = usb_string_id(cdev);
198 if (status < 0)
199 return status;
200 strings_dev[STRING_PRODUCT_IDX].id = status;
201 device_desc.iProduct = status;
202
203 /* register our configuration */
204 status = usb_add_config(cdev, &config_driver);
205 if (status < 0)
206 return status;
207
208 dev_info(&gadget->dev, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
209
210 return 0;
211}
212
213static int __exit hid_unbind(struct usb_composite_dev *cdev)
214{
215 ghid_cleanup();
216 return 0;
217}
218
219static int __init hidg_plat_driver_probe(struct platform_device *pdev)
220{
221 struct hidg_func_descriptor *func = pdev->dev.platform_data;
222 struct hidg_func_node *entry;
223
224 if (!func) {
225 dev_err(&pdev->dev, "Platform data missing\n");
226 return -ENODEV;
227 }
228
229 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
230 if (!entry)
231 return -ENOMEM;
232
233 entry->func = func;
234 list_add_tail(&entry->node, &hidg_func_list);
235
236 return 0;
237}
238
239static int __devexit hidg_plat_driver_remove(struct platform_device *pdev)
240{
241 struct hidg_func_node *e, *n;
242
243 list_for_each_entry_safe(e, n, &hidg_func_list, node) {
244 list_del(&e->node);
245 kfree(e);
246 }
247
248 return 0;
249}
250
251
252/****************************** Some noise ******************************/
253
254
255static struct usb_composite_driver hidg_driver = {
256 .name = "g_hid",
257 .dev = &device_desc,
258 .strings = dev_strings,
259 .bind = hid_bind,
260 .unbind = __exit_p(hid_unbind),
261};
262
263static struct platform_driver hidg_plat_driver = {
264 .remove = __devexit_p(hidg_plat_driver_remove),
265 .driver = {
266 .owner = THIS_MODULE,
267 .name = "hidg",
268 },
269};
270
271
272MODULE_DESCRIPTION(DRIVER_DESC);
273MODULE_AUTHOR("Fabien Chouteau, Peter Korsgaard");
274MODULE_LICENSE("GPL");
275
276static int __init hidg_init(void)
277{
278 int status;
279
280 status = platform_driver_probe(&hidg_plat_driver,
281 hidg_plat_driver_probe);
282 if (status < 0)
283 return status;
284
285 status = usb_composite_register(&hidg_driver);
286 if (status < 0)
287 platform_driver_unregister(&hidg_plat_driver);
288
289 return status;
290}
291module_init(hidg_init);
292
293static void __exit hidg_cleanup(void)
294{
295 platform_driver_unregister(&hidg_plat_driver);
296 usb_composite_unregister(&hidg_driver);
297}
298module_exit(hidg_cleanup);
diff --git a/drivers/usb/gadget/pxa27x_udc.h b/drivers/usb/gadget/pxa27x_udc.h
index ff61e4866e8a..cd16231d8c73 100644
--- a/drivers/usb/gadget/pxa27x_udc.h
+++ b/drivers/usb/gadget/pxa27x_udc.h
@@ -360,7 +360,7 @@ struct pxa_ep {
360 * Specific pxa endpoint data, needed for hardware initialization 360 * Specific pxa endpoint data, needed for hardware initialization
361 */ 361 */
362 unsigned dir_in:1; 362 unsigned dir_in:1;
363 unsigned addr:3; 363 unsigned addr:4;
364 unsigned config:2; 364 unsigned config:2;
365 unsigned interface:3; 365 unsigned interface:3;
366 unsigned alternate:3; 366 unsigned alternate:3;
diff --git a/drivers/usb/gadget/s3c-hsotg.c b/drivers/usb/gadget/s3c-hsotg.c
index 124a8ccfdcda..1f73b485732d 100644
--- a/drivers/usb/gadget/s3c-hsotg.c
+++ b/drivers/usb/gadget/s3c-hsotg.c
@@ -2145,6 +2145,7 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2145 u32 epctrl; 2145 u32 epctrl;
2146 u32 mps; 2146 u32 mps;
2147 int dir_in; 2147 int dir_in;
2148 int ret = 0;
2148 2149
2149 dev_dbg(hsotg->dev, 2150 dev_dbg(hsotg->dev,
2150 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n", 2151 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
@@ -2196,7 +2197,8 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2196 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { 2197 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2197 case USB_ENDPOINT_XFER_ISOC: 2198 case USB_ENDPOINT_XFER_ISOC:
2198 dev_err(hsotg->dev, "no current ISOC support\n"); 2199 dev_err(hsotg->dev, "no current ISOC support\n");
2199 return -EINVAL; 2200 ret = -EINVAL;
2201 goto out;
2200 2202
2201 case USB_ENDPOINT_XFER_BULK: 2203 case USB_ENDPOINT_XFER_BULK:
2202 epctrl |= S3C_DxEPCTL_EPType_Bulk; 2204 epctrl |= S3C_DxEPCTL_EPType_Bulk;
@@ -2235,8 +2237,9 @@ static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2235 /* enable the endpoint interrupt */ 2237 /* enable the endpoint interrupt */
2236 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1); 2238 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2237 2239
2240out:
2238 spin_unlock_irqrestore(&hs_ep->lock, flags); 2241 spin_unlock_irqrestore(&hs_ep->lock, flags);
2239 return 0; 2242 return ret;
2240} 2243}
2241 2244
2242static int s3c_hsotg_ep_disable(struct usb_ep *ep) 2245static int s3c_hsotg_ep_disable(struct usb_ep *ep)
diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c
index 868d8ee86756..04c462ff0ea6 100644
--- a/drivers/usb/gadget/storage_common.c
+++ b/drivers/usb/gadget/storage_common.c
@@ -654,7 +654,7 @@ static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
654 654
655 if (curlun->ro || !filp) 655 if (curlun->ro || !filp)
656 return 0; 656 return 0;
657 return vfs_fsync(filp, filp->f_path.dentry, 1); 657 return vfs_fsync(filp, 1);
658} 658}
659 659
660static void store_cdrom_address(u8 *dest, int msf, u32 addr) 660static void store_cdrom_address(u8 *dest, int msf, u32 addr)
diff --git a/drivers/usb/gadget/u_ether.c b/drivers/usb/gadget/u_ether.c
index 07f4178ad178..1da755a1c855 100644
--- a/drivers/usb/gadget/u_ether.c
+++ b/drivers/usb/gadget/u_ether.c
@@ -715,7 +715,7 @@ static u8 __init nibble(unsigned char c)
715 return 0; 715 return 0;
716} 716}
717 717
718static int __init get_ether_addr(const char *str, u8 *dev_addr) 718static int get_ether_addr(const char *str, u8 *dev_addr)
719{ 719{
720 if (str) { 720 if (str) {
721 unsigned i; 721 unsigned i;
@@ -764,7 +764,7 @@ static struct device_type gadget_type = {
764 * 764 *
765 * Returns negative errno, or zero on success 765 * Returns negative errno, or zero on success
766 */ 766 */
767int __init gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN]) 767int gether_setup(struct usb_gadget *g, u8 ethaddr[ETH_ALEN])
768{ 768{
769 struct eth_dev *dev; 769 struct eth_dev *dev;
770 struct net_device *net; 770 struct net_device *net;
diff --git a/drivers/usb/gadget/uvc.h b/drivers/usb/gadget/uvc.h
new file mode 100644
index 000000000000..0a705e63c936
--- /dev/null
+++ b/drivers/usb/gadget/uvc.h
@@ -0,0 +1,241 @@
1/*
2 * uvc_gadget.h -- USB Video Class Gadget driver
3 *
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#ifndef _UVC_GADGET_H_
15#define _UVC_GADGET_H_
16
17#include <linux/ioctl.h>
18#include <linux/types.h>
19#include <linux/usb/ch9.h>
20
21#define UVC_EVENT_FIRST (V4L2_EVENT_PRIVATE_START + 0)
22#define UVC_EVENT_CONNECT (V4L2_EVENT_PRIVATE_START + 0)
23#define UVC_EVENT_DISCONNECT (V4L2_EVENT_PRIVATE_START + 1)
24#define UVC_EVENT_STREAMON (V4L2_EVENT_PRIVATE_START + 2)
25#define UVC_EVENT_STREAMOFF (V4L2_EVENT_PRIVATE_START + 3)
26#define UVC_EVENT_SETUP (V4L2_EVENT_PRIVATE_START + 4)
27#define UVC_EVENT_DATA (V4L2_EVENT_PRIVATE_START + 5)
28#define UVC_EVENT_LAST (V4L2_EVENT_PRIVATE_START + 5)
29
30struct uvc_request_data
31{
32 unsigned int length;
33 __u8 data[60];
34};
35
36struct uvc_event
37{
38 union {
39 enum usb_device_speed speed;
40 struct usb_ctrlrequest req;
41 struct uvc_request_data data;
42 };
43};
44
45#define UVCIOC_SEND_RESPONSE _IOW('U', 1, struct uvc_request_data)
46
47#define UVC_INTF_CONTROL 0
48#define UVC_INTF_STREAMING 1
49
50/* ------------------------------------------------------------------------
51 * UVC constants & structures
52 */
53
54/* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
55#define UVC_STREAM_EOH (1 << 7)
56#define UVC_STREAM_ERR (1 << 6)
57#define UVC_STREAM_STI (1 << 5)
58#define UVC_STREAM_RES (1 << 4)
59#define UVC_STREAM_SCR (1 << 3)
60#define UVC_STREAM_PTS (1 << 2)
61#define UVC_STREAM_EOF (1 << 1)
62#define UVC_STREAM_FID (1 << 0)
63
64struct uvc_streaming_control {
65 __u16 bmHint;
66 __u8 bFormatIndex;
67 __u8 bFrameIndex;
68 __u32 dwFrameInterval;
69 __u16 wKeyFrameRate;
70 __u16 wPFrameRate;
71 __u16 wCompQuality;
72 __u16 wCompWindowSize;
73 __u16 wDelay;
74 __u32 dwMaxVideoFrameSize;
75 __u32 dwMaxPayloadTransferSize;
76 __u32 dwClockFrequency;
77 __u8 bmFramingInfo;
78 __u8 bPreferedVersion;
79 __u8 bMinVersion;
80 __u8 bMaxVersion;
81} __attribute__((__packed__));
82
83/* ------------------------------------------------------------------------
84 * Debugging, printing and logging
85 */
86
87#ifdef __KERNEL__
88
89#include <linux/usb.h> /* For usb_endpoint_* */
90#include <linux/usb/gadget.h>
91#include <linux/videodev2.h>
92#include <media/v4l2-fh.h>
93
94#include "uvc_queue.h"
95
96#define UVC_TRACE_PROBE (1 << 0)
97#define UVC_TRACE_DESCR (1 << 1)
98#define UVC_TRACE_CONTROL (1 << 2)
99#define UVC_TRACE_FORMAT (1 << 3)
100#define UVC_TRACE_CAPTURE (1 << 4)
101#define UVC_TRACE_CALLS (1 << 5)
102#define UVC_TRACE_IOCTL (1 << 6)
103#define UVC_TRACE_FRAME (1 << 7)
104#define UVC_TRACE_SUSPEND (1 << 8)
105#define UVC_TRACE_STATUS (1 << 9)
106
107#define UVC_WARN_MINMAX 0
108#define UVC_WARN_PROBE_DEF 1
109
110extern unsigned int uvc_trace_param;
111
112#define uvc_trace(flag, msg...) \
113 do { \
114 if (uvc_trace_param & flag) \
115 printk(KERN_DEBUG "uvcvideo: " msg); \
116 } while (0)
117
118#define uvc_warn_once(dev, warn, msg...) \
119 do { \
120 if (!test_and_set_bit(warn, &dev->warnings)) \
121 printk(KERN_INFO "uvcvideo: " msg); \
122 } while (0)
123
124#define uvc_printk(level, msg...) \
125 printk(level "uvcvideo: " msg)
126
127/* ------------------------------------------------------------------------
128 * Driver specific constants
129 */
130
131#define DRIVER_VERSION "0.1.0"
132#define DRIVER_VERSION_NUMBER KERNEL_VERSION(0, 1, 0)
133
134#define DMA_ADDR_INVALID (~(dma_addr_t)0)
135
136#define UVC_NUM_REQUESTS 4
137#define UVC_MAX_REQUEST_SIZE 64
138#define UVC_MAX_EVENTS 4
139
140#define USB_DT_INTERFACE_ASSOCIATION_SIZE 8
141#define USB_CLASS_MISC 0xef
142
143/* ------------------------------------------------------------------------
144 * Structures
145 */
146
147struct uvc_video
148{
149 struct usb_ep *ep;
150
151 /* Frame parameters */
152 u8 bpp;
153 u32 fcc;
154 unsigned int width;
155 unsigned int height;
156 unsigned int imagesize;
157
158 /* Requests */
159 unsigned int req_size;
160 struct usb_request *req[UVC_NUM_REQUESTS];
161 __u8 *req_buffer[UVC_NUM_REQUESTS];
162 struct list_head req_free;
163 spinlock_t req_lock;
164
165 void (*encode) (struct usb_request *req, struct uvc_video *video,
166 struct uvc_buffer *buf);
167
168 /* Context data used by the completion handler */
169 __u32 payload_size;
170 __u32 max_payload_size;
171
172 struct uvc_video_queue queue;
173 unsigned int fid;
174};
175
176enum uvc_state
177{
178 UVC_STATE_DISCONNECTED,
179 UVC_STATE_CONNECTED,
180 UVC_STATE_STREAMING,
181};
182
183struct uvc_device
184{
185 struct video_device *vdev;
186 enum uvc_state state;
187 struct usb_function func;
188 struct uvc_video video;
189
190 /* Descriptors */
191 struct {
192 const struct uvc_descriptor_header * const *control;
193 const struct uvc_descriptor_header * const *fs_streaming;
194 const struct uvc_descriptor_header * const *hs_streaming;
195 } desc;
196
197 unsigned int control_intf;
198 struct usb_ep *control_ep;
199 struct usb_request *control_req;
200 void *control_buf;
201
202 unsigned int streaming_intf;
203
204 /* Events */
205 unsigned int event_length;
206 unsigned int event_setup_out : 1;
207};
208
209static inline struct uvc_device *to_uvc(struct usb_function *f)
210{
211 return container_of(f, struct uvc_device, func);
212}
213
214struct uvc_file_handle
215{
216 struct v4l2_fh vfh;
217 struct uvc_video *device;
218};
219
220#define to_uvc_file_handle(handle) \
221 container_of(handle, struct uvc_file_handle, vfh)
222
223extern struct v4l2_file_operations uvc_v4l2_fops;
224
225/* ------------------------------------------------------------------------
226 * Functions
227 */
228
229extern int uvc_video_enable(struct uvc_video *video, int enable);
230extern int uvc_video_init(struct uvc_video *video);
231extern int uvc_video_pump(struct uvc_video *video);
232
233extern void uvc_endpoint_stream(struct uvc_device *dev);
234
235extern void uvc_function_connect(struct uvc_device *uvc);
236extern void uvc_function_disconnect(struct uvc_device *uvc);
237
238#endif /* __KERNEL__ */
239
240#endif /* _UVC_GADGET_H_ */
241
diff --git a/drivers/usb/gadget/uvc_queue.c b/drivers/usb/gadget/uvc_queue.c
new file mode 100644
index 000000000000..43891991bf21
--- /dev/null
+++ b/drivers/usb/gadget/uvc_queue.c
@@ -0,0 +1,583 @@
1/*
2 * uvc_queue.c -- USB Video Class driver - Buffers management
3 *
4 * Copyright (C) 2005-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/mm.h>
16#include <linux/list.h>
17#include <linux/module.h>
18#include <linux/usb.h>
19#include <linux/videodev2.h>
20#include <linux/vmalloc.h>
21#include <linux/wait.h>
22#include <asm/atomic.h>
23
24#include "uvc.h"
25
26/* ------------------------------------------------------------------------
27 * Video buffers queue management.
28 *
29 * Video queues is initialized by uvc_queue_init(). The function performs
30 * basic initialization of the uvc_video_queue struct and never fails.
31 *
32 * Video buffer allocation and freeing are performed by uvc_alloc_buffers and
33 * uvc_free_buffers respectively. The former acquires the video queue lock,
34 * while the later must be called with the lock held (so that allocation can
35 * free previously allocated buffers). Trying to free buffers that are mapped
36 * to user space will return -EBUSY.
37 *
38 * Video buffers are managed using two queues. However, unlike most USB video
39 * drivers that use an in queue and an out queue, we use a main queue to hold
40 * all queued buffers (both 'empty' and 'done' buffers), and an irq queue to
41 * hold empty buffers. This design (copied from video-buf) minimizes locking
42 * in interrupt, as only one queue is shared between interrupt and user
43 * contexts.
44 *
45 * Use cases
46 * ---------
47 *
48 * Unless stated otherwise, all operations that modify the irq buffers queue
49 * are protected by the irq spinlock.
50 *
51 * 1. The user queues the buffers, starts streaming and dequeues a buffer.
52 *
53 * The buffers are added to the main and irq queues. Both operations are
54 * protected by the queue lock, and the later is protected by the irq
55 * spinlock as well.
56 *
57 * The completion handler fetches a buffer from the irq queue and fills it
58 * with video data. If no buffer is available (irq queue empty), the handler
59 * returns immediately.
60 *
61 * When the buffer is full, the completion handler removes it from the irq
62 * queue, marks it as ready (UVC_BUF_STATE_DONE) and wakes its wait queue.
63 * At that point, any process waiting on the buffer will be woken up. If a
64 * process tries to dequeue a buffer after it has been marked ready, the
65 * dequeing will succeed immediately.
66 *
67 * 2. Buffers are queued, user is waiting on a buffer and the device gets
68 * disconnected.
69 *
70 * When the device is disconnected, the kernel calls the completion handler
71 * with an appropriate status code. The handler marks all buffers in the
72 * irq queue as being erroneous (UVC_BUF_STATE_ERROR) and wakes them up so
73 * that any process waiting on a buffer gets woken up.
74 *
75 * Waking up up the first buffer on the irq list is not enough, as the
76 * process waiting on the buffer might restart the dequeue operation
77 * immediately.
78 *
79 */
80
81void uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type)
82{
83 mutex_init(&queue->mutex);
84 spin_lock_init(&queue->irqlock);
85 INIT_LIST_HEAD(&queue->mainqueue);
86 INIT_LIST_HEAD(&queue->irqqueue);
87 queue->type = type;
88}
89
90/*
91 * Allocate the video buffers.
92 *
93 * Pages are reserved to make sure they will not be swapped, as they will be
94 * filled in the URB completion handler.
95 *
96 * Buffers will be individually mapped, so they must all be page aligned.
97 */
98int uvc_alloc_buffers(struct uvc_video_queue *queue, unsigned int nbuffers,
99 unsigned int buflength)
100{
101 unsigned int bufsize = PAGE_ALIGN(buflength);
102 unsigned int i;
103 void *mem = NULL;
104 int ret;
105
106 if (nbuffers > UVC_MAX_VIDEO_BUFFERS)
107 nbuffers = UVC_MAX_VIDEO_BUFFERS;
108
109 mutex_lock(&queue->mutex);
110
111 if ((ret = uvc_free_buffers(queue)) < 0)
112 goto done;
113
114 /* Bail out if no buffers should be allocated. */
115 if (nbuffers == 0)
116 goto done;
117
118 /* Decrement the number of buffers until allocation succeeds. */
119 for (; nbuffers > 0; --nbuffers) {
120 mem = vmalloc_32(nbuffers * bufsize);
121 if (mem != NULL)
122 break;
123 }
124
125 if (mem == NULL) {
126 ret = -ENOMEM;
127 goto done;
128 }
129
130 for (i = 0; i < nbuffers; ++i) {
131 memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);
132 queue->buffer[i].buf.index = i;
133 queue->buffer[i].buf.m.offset = i * bufsize;
134 queue->buffer[i].buf.length = buflength;
135 queue->buffer[i].buf.type = queue->type;
136 queue->buffer[i].buf.sequence = 0;
137 queue->buffer[i].buf.field = V4L2_FIELD_NONE;
138 queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
139 queue->buffer[i].buf.flags = 0;
140 init_waitqueue_head(&queue->buffer[i].wait);
141 }
142
143 queue->mem = mem;
144 queue->count = nbuffers;
145 queue->buf_size = bufsize;
146 ret = nbuffers;
147
148done:
149 mutex_unlock(&queue->mutex);
150 return ret;
151}
152
153/*
154 * Free the video buffers.
155 *
156 * This function must be called with the queue lock held.
157 */
158int uvc_free_buffers(struct uvc_video_queue *queue)
159{
160 unsigned int i;
161
162 for (i = 0; i < queue->count; ++i) {
163 if (queue->buffer[i].vma_use_count != 0)
164 return -EBUSY;
165 }
166
167 if (queue->count) {
168 vfree(queue->mem);
169 queue->count = 0;
170 }
171
172 return 0;
173}
174
175static void __uvc_query_buffer(struct uvc_buffer *buf,
176 struct v4l2_buffer *v4l2_buf)
177{
178 memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);
179
180 if (buf->vma_use_count)
181 v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
182
183 switch (buf->state) {
184 case UVC_BUF_STATE_ERROR:
185 case UVC_BUF_STATE_DONE:
186 v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
187 break;
188 case UVC_BUF_STATE_QUEUED:
189 case UVC_BUF_STATE_ACTIVE:
190 v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
191 break;
192 case UVC_BUF_STATE_IDLE:
193 default:
194 break;
195 }
196}
197
198int uvc_query_buffer(struct uvc_video_queue *queue,
199 struct v4l2_buffer *v4l2_buf)
200{
201 int ret = 0;
202
203 mutex_lock(&queue->mutex);
204 if (v4l2_buf->index >= queue->count) {
205 ret = -EINVAL;
206 goto done;
207 }
208
209 __uvc_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);
210
211done:
212 mutex_unlock(&queue->mutex);
213 return ret;
214}
215
216/*
217 * Queue a video buffer. Attempting to queue a buffer that has already been
218 * queued will return -EINVAL.
219 */
220int uvc_queue_buffer(struct uvc_video_queue *queue,
221 struct v4l2_buffer *v4l2_buf)
222{
223 struct uvc_buffer *buf;
224 unsigned long flags;
225 int ret = 0;
226
227 uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index);
228
229 if (v4l2_buf->type != queue->type ||
230 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
231 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
232 "and/or memory (%u).\n", v4l2_buf->type,
233 v4l2_buf->memory);
234 return -EINVAL;
235 }
236
237 mutex_lock(&queue->mutex);
238 if (v4l2_buf->index >= queue->count) {
239 uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n");
240 ret = -EINVAL;
241 goto done;
242 }
243
244 buf = &queue->buffer[v4l2_buf->index];
245 if (buf->state != UVC_BUF_STATE_IDLE) {
246 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state "
247 "(%u).\n", buf->state);
248 ret = -EINVAL;
249 goto done;
250 }
251
252 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
253 v4l2_buf->bytesused > buf->buf.length) {
254 uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
255 ret = -EINVAL;
256 goto done;
257 }
258
259 if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
260 buf->buf.bytesused = 0;
261 else
262 buf->buf.bytesused = v4l2_buf->bytesused;
263
264 spin_lock_irqsave(&queue->irqlock, flags);
265 if (queue->flags & UVC_QUEUE_DISCONNECTED) {
266 spin_unlock_irqrestore(&queue->irqlock, flags);
267 ret = -ENODEV;
268 goto done;
269 }
270 buf->state = UVC_BUF_STATE_QUEUED;
271
272 ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
273 queue->flags &= ~UVC_QUEUE_PAUSED;
274
275 list_add_tail(&buf->stream, &queue->mainqueue);
276 list_add_tail(&buf->queue, &queue->irqqueue);
277 spin_unlock_irqrestore(&queue->irqlock, flags);
278
279done:
280 mutex_unlock(&queue->mutex);
281 return ret;
282}
283
284static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking)
285{
286 if (nonblocking) {
287 return (buf->state != UVC_BUF_STATE_QUEUED &&
288 buf->state != UVC_BUF_STATE_ACTIVE)
289 ? 0 : -EAGAIN;
290 }
291
292 return wait_event_interruptible(buf->wait,
293 buf->state != UVC_BUF_STATE_QUEUED &&
294 buf->state != UVC_BUF_STATE_ACTIVE);
295}
296
297/*
298 * Dequeue a video buffer. If nonblocking is false, block until a buffer is
299 * available.
300 */
301int uvc_dequeue_buffer(struct uvc_video_queue *queue,
302 struct v4l2_buffer *v4l2_buf, int nonblocking)
303{
304 struct uvc_buffer *buf;
305 int ret = 0;
306
307 if (v4l2_buf->type != queue->type ||
308 v4l2_buf->memory != V4L2_MEMORY_MMAP) {
309 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
310 "and/or memory (%u).\n", v4l2_buf->type,
311 v4l2_buf->memory);
312 return -EINVAL;
313 }
314
315 mutex_lock(&queue->mutex);
316 if (list_empty(&queue->mainqueue)) {
317 uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n");
318 ret = -EINVAL;
319 goto done;
320 }
321
322 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
323 if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0)
324 goto done;
325
326 uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u (%u, %u bytes).\n",
327 buf->buf.index, buf->state, buf->buf.bytesused);
328
329 switch (buf->state) {
330 case UVC_BUF_STATE_ERROR:
331 uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data "
332 "(transmission error).\n");
333 ret = -EIO;
334 case UVC_BUF_STATE_DONE:
335 buf->state = UVC_BUF_STATE_IDLE;
336 break;
337
338 case UVC_BUF_STATE_IDLE:
339 case UVC_BUF_STATE_QUEUED:
340 case UVC_BUF_STATE_ACTIVE:
341 default:
342 uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u "
343 "(driver bug?).\n", buf->state);
344 ret = -EINVAL;
345 goto done;
346 }
347
348 list_del(&buf->stream);
349 __uvc_query_buffer(buf, v4l2_buf);
350
351done:
352 mutex_unlock(&queue->mutex);
353 return ret;
354}
355
356/*
357 * Poll the video queue.
358 *
359 * This function implements video queue polling and is intended to be used by
360 * the device poll handler.
361 */
362unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
363 poll_table *wait)
364{
365 struct uvc_buffer *buf;
366 unsigned int mask = 0;
367
368 mutex_lock(&queue->mutex);
369 if (list_empty(&queue->mainqueue))
370 goto done;
371
372 buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
373
374 poll_wait(file, &buf->wait, wait);
375 if (buf->state == UVC_BUF_STATE_DONE ||
376 buf->state == UVC_BUF_STATE_ERROR)
377 mask |= POLLOUT | POLLWRNORM;
378
379done:
380 mutex_unlock(&queue->mutex);
381 return mask;
382}
383
384/*
385 * VMA operations.
386 */
387static void uvc_vm_open(struct vm_area_struct *vma)
388{
389 struct uvc_buffer *buffer = vma->vm_private_data;
390 buffer->vma_use_count++;
391}
392
393static void uvc_vm_close(struct vm_area_struct *vma)
394{
395 struct uvc_buffer *buffer = vma->vm_private_data;
396 buffer->vma_use_count--;
397}
398
399static struct vm_operations_struct uvc_vm_ops = {
400 .open = uvc_vm_open,
401 .close = uvc_vm_close,
402};
403
404/*
405 * Memory-map a buffer.
406 *
407 * This function implements video buffer memory mapping and is intended to be
408 * used by the device mmap handler.
409 */
410int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
411{
412 struct uvc_buffer *uninitialized_var(buffer);
413 struct page *page;
414 unsigned long addr, start, size;
415 unsigned int i;
416 int ret = 0;
417
418 start = vma->vm_start;
419 size = vma->vm_end - vma->vm_start;
420
421 mutex_lock(&queue->mutex);
422
423 for (i = 0; i < queue->count; ++i) {
424 buffer = &queue->buffer[i];
425 if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
426 break;
427 }
428
429 if (i == queue->count || size != queue->buf_size) {
430 ret = -EINVAL;
431 goto done;
432 }
433
434 /*
435 * VM_IO marks the area as being an mmaped region for I/O to a
436 * device. It also prevents the region from being core dumped.
437 */
438 vma->vm_flags |= VM_IO;
439
440 addr = (unsigned long)queue->mem + buffer->buf.m.offset;
441 while (size > 0) {
442 page = vmalloc_to_page((void *)addr);
443 if ((ret = vm_insert_page(vma, start, page)) < 0)
444 goto done;
445
446 start += PAGE_SIZE;
447 addr += PAGE_SIZE;
448 size -= PAGE_SIZE;
449 }
450
451 vma->vm_ops = &uvc_vm_ops;
452 vma->vm_private_data = buffer;
453 uvc_vm_open(vma);
454
455done:
456 mutex_unlock(&queue->mutex);
457 return ret;
458}
459
460/*
461 * Enable or disable the video buffers queue.
462 *
463 * The queue must be enabled before starting video acquisition and must be
464 * disabled after stopping it. This ensures that the video buffers queue
465 * state can be properly initialized before buffers are accessed from the
466 * interrupt handler.
467 *
468 * Enabling the video queue initializes parameters (such as sequence number,
469 * sync pattern, ...). If the queue is already enabled, return -EBUSY.
470 *
471 * Disabling the video queue cancels the queue and removes all buffers from
472 * the main queue.
473 *
474 * This function can't be called from interrupt context. Use
475 * uvc_queue_cancel() instead.
476 */
477int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
478{
479 unsigned int i;
480 int ret = 0;
481
482 mutex_lock(&queue->mutex);
483 if (enable) {
484 if (uvc_queue_streaming(queue)) {
485 ret = -EBUSY;
486 goto done;
487 }
488 queue->sequence = 0;
489 queue->flags |= UVC_QUEUE_STREAMING;
490 queue->buf_used = 0;
491 } else {
492 uvc_queue_cancel(queue, 0);
493 INIT_LIST_HEAD(&queue->mainqueue);
494
495 for (i = 0; i < queue->count; ++i)
496 queue->buffer[i].state = UVC_BUF_STATE_IDLE;
497
498 queue->flags &= ~UVC_QUEUE_STREAMING;
499 }
500
501done:
502 mutex_unlock(&queue->mutex);
503 return ret;
504}
505
506/*
507 * Cancel the video buffers queue.
508 *
509 * Cancelling the queue marks all buffers on the irq queue as erroneous,
510 * wakes them up and removes them from the queue.
511 *
512 * If the disconnect parameter is set, further calls to uvc_queue_buffer will
513 * fail with -ENODEV.
514 *
515 * This function acquires the irq spinlock and can be called from interrupt
516 * context.
517 */
518void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
519{
520 struct uvc_buffer *buf;
521 unsigned long flags;
522
523 spin_lock_irqsave(&queue->irqlock, flags);
524 while (!list_empty(&queue->irqqueue)) {
525 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
526 queue);
527 list_del(&buf->queue);
528 buf->state = UVC_BUF_STATE_ERROR;
529 wake_up(&buf->wait);
530 }
531 /* This must be protected by the irqlock spinlock to avoid race
532 * conditions between uvc_queue_buffer and the disconnection event that
533 * could result in an interruptible wait in uvc_dequeue_buffer. Do not
534 * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
535 * state outside the queue code.
536 */
537 if (disconnect)
538 queue->flags |= UVC_QUEUE_DISCONNECTED;
539 spin_unlock_irqrestore(&queue->irqlock, flags);
540}
541
542struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
543 struct uvc_buffer *buf)
544{
545 struct uvc_buffer *nextbuf;
546 unsigned long flags;
547
548 if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
549 buf->buf.length != buf->buf.bytesused) {
550 buf->state = UVC_BUF_STATE_QUEUED;
551 buf->buf.bytesused = 0;
552 return buf;
553 }
554
555 spin_lock_irqsave(&queue->irqlock, flags);
556 list_del(&buf->queue);
557 if (!list_empty(&queue->irqqueue))
558 nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
559 queue);
560 else
561 nextbuf = NULL;
562 spin_unlock_irqrestore(&queue->irqlock, flags);
563
564 buf->buf.sequence = queue->sequence++;
565 do_gettimeofday(&buf->buf.timestamp);
566
567 wake_up(&buf->wait);
568 return nextbuf;
569}
570
571struct uvc_buffer *uvc_queue_head(struct uvc_video_queue *queue)
572{
573 struct uvc_buffer *buf = NULL;
574
575 if (!list_empty(&queue->irqqueue))
576 buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
577 queue);
578 else
579 queue->flags |= UVC_QUEUE_PAUSED;
580
581 return buf;
582}
583
diff --git a/drivers/usb/gadget/uvc_queue.h b/drivers/usb/gadget/uvc_queue.h
new file mode 100644
index 000000000000..7f5a33fe7ae2
--- /dev/null
+++ b/drivers/usb/gadget/uvc_queue.h
@@ -0,0 +1,89 @@
1#ifndef _UVC_QUEUE_H_
2#define _UVC_QUEUE_H_
3
4#ifdef __KERNEL__
5
6#include <linux/kernel.h>
7#include <linux/poll.h>
8#include <linux/videodev2.h>
9
10/* Maximum frame size in bytes, for sanity checking. */
11#define UVC_MAX_FRAME_SIZE (16*1024*1024)
12/* Maximum number of video buffers. */
13#define UVC_MAX_VIDEO_BUFFERS 32
14
15/* ------------------------------------------------------------------------
16 * Structures.
17 */
18
19enum uvc_buffer_state {
20 UVC_BUF_STATE_IDLE = 0,
21 UVC_BUF_STATE_QUEUED = 1,
22 UVC_BUF_STATE_ACTIVE = 2,
23 UVC_BUF_STATE_DONE = 3,
24 UVC_BUF_STATE_ERROR = 4,
25};
26
27struct uvc_buffer {
28 unsigned long vma_use_count;
29 struct list_head stream;
30
31 /* Touched by interrupt handler. */
32 struct v4l2_buffer buf;
33 struct list_head queue;
34 wait_queue_head_t wait;
35 enum uvc_buffer_state state;
36};
37
38#define UVC_QUEUE_STREAMING (1 << 0)
39#define UVC_QUEUE_DISCONNECTED (1 << 1)
40#define UVC_QUEUE_DROP_INCOMPLETE (1 << 2)
41#define UVC_QUEUE_PAUSED (1 << 3)
42
43struct uvc_video_queue {
44 enum v4l2_buf_type type;
45
46 void *mem;
47 unsigned int flags;
48 __u32 sequence;
49
50 unsigned int count;
51 unsigned int buf_size;
52 unsigned int buf_used;
53 struct uvc_buffer buffer[UVC_MAX_VIDEO_BUFFERS];
54 struct mutex mutex; /* protects buffers and mainqueue */
55 spinlock_t irqlock; /* protects irqqueue */
56
57 struct list_head mainqueue;
58 struct list_head irqqueue;
59};
60
61extern void uvc_queue_init(struct uvc_video_queue *queue,
62 enum v4l2_buf_type type);
63extern int uvc_alloc_buffers(struct uvc_video_queue *queue,
64 unsigned int nbuffers, unsigned int buflength);
65extern int uvc_free_buffers(struct uvc_video_queue *queue);
66extern int uvc_query_buffer(struct uvc_video_queue *queue,
67 struct v4l2_buffer *v4l2_buf);
68extern int uvc_queue_buffer(struct uvc_video_queue *queue,
69 struct v4l2_buffer *v4l2_buf);
70extern int uvc_dequeue_buffer(struct uvc_video_queue *queue,
71 struct v4l2_buffer *v4l2_buf, int nonblocking);
72extern int uvc_queue_enable(struct uvc_video_queue *queue, int enable);
73extern void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect);
74extern struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
75 struct uvc_buffer *buf);
76extern unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
77 struct file *file, poll_table *wait);
78extern int uvc_queue_mmap(struct uvc_video_queue *queue,
79 struct vm_area_struct *vma);
80static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
81{
82 return queue->flags & UVC_QUEUE_STREAMING;
83}
84extern struct uvc_buffer *uvc_queue_head(struct uvc_video_queue *queue);
85
86#endif /* __KERNEL__ */
87
88#endif /* _UVC_QUEUE_H_ */
89
diff --git a/drivers/usb/gadget/uvc_v4l2.c b/drivers/usb/gadget/uvc_v4l2.c
new file mode 100644
index 000000000000..a7989f29837e
--- /dev/null
+++ b/drivers/usb/gadget/uvc_v4l2.c
@@ -0,0 +1,374 @@
1/*
2 * uvc_v4l2.c -- USB Video Class Gadget driver
3 *
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/device.h>
16#include <linux/errno.h>
17#include <linux/list.h>
18#include <linux/mutex.h>
19#include <linux/version.h>
20#include <linux/videodev2.h>
21#include <linux/vmalloc.h>
22#include <linux/wait.h>
23
24#include <media/v4l2-dev.h>
25#include <media/v4l2-event.h>
26#include <media/v4l2-ioctl.h>
27
28#include "uvc.h"
29#include "uvc_queue.h"
30
31/* --------------------------------------------------------------------------
32 * Requests handling
33 */
34
35static int
36uvc_send_response(struct uvc_device *uvc, struct uvc_request_data *data)
37{
38 struct usb_composite_dev *cdev = uvc->func.config->cdev;
39 struct usb_request *req = uvc->control_req;
40
41 if (data->length < 0)
42 return usb_ep_set_halt(cdev->gadget->ep0);
43
44 req->length = min(uvc->event_length, data->length);
45 req->zero = data->length < uvc->event_length;
46 req->dma = DMA_ADDR_INVALID;
47
48 memcpy(req->buf, data->data, data->length);
49
50 return usb_ep_queue(cdev->gadget->ep0, req, GFP_KERNEL);
51}
52
53/* --------------------------------------------------------------------------
54 * V4L2
55 */
56
57struct uvc_format
58{
59 u8 bpp;
60 u32 fcc;
61};
62
63static struct uvc_format uvc_formats[] = {
64 { 16, V4L2_PIX_FMT_YUYV },
65 { 0, V4L2_PIX_FMT_MJPEG },
66};
67
68static int
69uvc_v4l2_get_format(struct uvc_video *video, struct v4l2_format *fmt)
70{
71 fmt->fmt.pix.pixelformat = video->fcc;
72 fmt->fmt.pix.width = video->width;
73 fmt->fmt.pix.height = video->height;
74 fmt->fmt.pix.field = V4L2_FIELD_NONE;
75 fmt->fmt.pix.bytesperline = video->bpp * video->width / 8;
76 fmt->fmt.pix.sizeimage = video->imagesize;
77 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
78 fmt->fmt.pix.priv = 0;
79
80 return 0;
81}
82
83static int
84uvc_v4l2_set_format(struct uvc_video *video, struct v4l2_format *fmt)
85{
86 struct uvc_format *format;
87 unsigned int imagesize;
88 unsigned int bpl;
89 unsigned int i;
90
91 for (i = 0; i < ARRAY_SIZE(uvc_formats); ++i) {
92 format = &uvc_formats[i];
93 if (format->fcc == fmt->fmt.pix.pixelformat)
94 break;
95 }
96
97 if (format == NULL || format->fcc != fmt->fmt.pix.pixelformat) {
98 printk(KERN_INFO "Unsupported format 0x%08x.\n",
99 fmt->fmt.pix.pixelformat);
100 return -EINVAL;
101 }
102
103 bpl = format->bpp * fmt->fmt.pix.width / 8;
104 imagesize = bpl ? bpl * fmt->fmt.pix.height : fmt->fmt.pix.sizeimage;
105
106 video->fcc = format->fcc;
107 video->bpp = format->bpp;
108 video->width = fmt->fmt.pix.width;
109 video->height = fmt->fmt.pix.height;
110 video->imagesize = imagesize;
111
112 fmt->fmt.pix.field = V4L2_FIELD_NONE;
113 fmt->fmt.pix.bytesperline = bpl;
114 fmt->fmt.pix.sizeimage = imagesize;
115 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
116 fmt->fmt.pix.priv = 0;
117
118 return 0;
119}
120
121static int
122uvc_v4l2_open(struct file *file)
123{
124 struct video_device *vdev = video_devdata(file);
125 struct uvc_device *uvc = video_get_drvdata(vdev);
126 struct uvc_file_handle *handle;
127 int ret;
128
129 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
130 if (handle == NULL)
131 return -ENOMEM;
132
133 ret = v4l2_fh_init(&handle->vfh, vdev);
134 if (ret < 0)
135 goto error;
136
137 ret = v4l2_event_init(&handle->vfh);
138 if (ret < 0)
139 goto error;
140
141 ret = v4l2_event_alloc(&handle->vfh, 8);
142 if (ret < 0)
143 goto error;
144
145 v4l2_fh_add(&handle->vfh);
146
147 handle->device = &uvc->video;
148 file->private_data = &handle->vfh;
149
150 uvc_function_connect(uvc);
151 return 0;
152
153error:
154 v4l2_fh_exit(&handle->vfh);
155 return ret;
156}
157
158static int
159uvc_v4l2_release(struct file *file)
160{
161 struct video_device *vdev = video_devdata(file);
162 struct uvc_device *uvc = video_get_drvdata(vdev);
163 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
164 struct uvc_video *video = handle->device;
165
166 uvc_function_disconnect(uvc);
167
168 uvc_video_enable(video, 0);
169 mutex_lock(&video->queue.mutex);
170 if (uvc_free_buffers(&video->queue) < 0)
171 printk(KERN_ERR "uvc_v4l2_release: Unable to free "
172 "buffers.\n");
173 mutex_unlock(&video->queue.mutex);
174
175 file->private_data = NULL;
176 v4l2_fh_del(&handle->vfh);
177 v4l2_fh_exit(&handle->vfh);
178 kfree(handle);
179 return 0;
180}
181
182static long
183uvc_v4l2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
184{
185 struct video_device *vdev = video_devdata(file);
186 struct uvc_device *uvc = video_get_drvdata(vdev);
187 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
188 struct usb_composite_dev *cdev = uvc->func.config->cdev;
189 struct uvc_video *video = &uvc->video;
190 int ret = 0;
191
192 switch (cmd) {
193 /* Query capabilities */
194 case VIDIOC_QUERYCAP:
195 {
196 struct v4l2_capability *cap = arg;
197
198 memset(cap, 0, sizeof *cap);
199 strncpy(cap->driver, "g_uvc", sizeof(cap->driver));
200 strncpy(cap->card, cdev->gadget->name, sizeof(cap->card));
201 strncpy(cap->bus_info, dev_name(&cdev->gadget->dev),
202 sizeof cap->bus_info);
203 cap->version = DRIVER_VERSION_NUMBER;
204 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
205 break;
206 }
207
208 /* Get & Set format */
209 case VIDIOC_G_FMT:
210 {
211 struct v4l2_format *fmt = arg;
212
213 if (fmt->type != video->queue.type)
214 return -EINVAL;
215
216 return uvc_v4l2_get_format(video, fmt);
217 }
218
219 case VIDIOC_S_FMT:
220 {
221 struct v4l2_format *fmt = arg;
222
223 if (fmt->type != video->queue.type)
224 return -EINVAL;
225
226 return uvc_v4l2_set_format(video, fmt);
227 }
228
229 /* Buffers & streaming */
230 case VIDIOC_REQBUFS:
231 {
232 struct v4l2_requestbuffers *rb = arg;
233
234 if (rb->type != video->queue.type ||
235 rb->memory != V4L2_MEMORY_MMAP)
236 return -EINVAL;
237
238 ret = uvc_alloc_buffers(&video->queue, rb->count,
239 video->imagesize);
240 if (ret < 0)
241 return ret;
242
243 rb->count = ret;
244 ret = 0;
245 break;
246 }
247
248 case VIDIOC_QUERYBUF:
249 {
250 struct v4l2_buffer *buf = arg;
251
252 if (buf->type != video->queue.type)
253 return -EINVAL;
254
255 return uvc_query_buffer(&video->queue, buf);
256 }
257
258 case VIDIOC_QBUF:
259 if ((ret = uvc_queue_buffer(&video->queue, arg)) < 0)
260 return ret;
261
262 return uvc_video_pump(video);
263
264 case VIDIOC_DQBUF:
265 return uvc_dequeue_buffer(&video->queue, arg,
266 file->f_flags & O_NONBLOCK);
267
268 case VIDIOC_STREAMON:
269 {
270 int *type = arg;
271
272 if (*type != video->queue.type)
273 return -EINVAL;
274
275 return uvc_video_enable(video, 1);
276 }
277
278 case VIDIOC_STREAMOFF:
279 {
280 int *type = arg;
281
282 if (*type != video->queue.type)
283 return -EINVAL;
284
285 return uvc_video_enable(video, 0);
286 }
287
288 /* Events */
289 case VIDIOC_DQEVENT:
290 {
291 struct v4l2_event *event = arg;
292
293 ret = v4l2_event_dequeue(&handle->vfh, event,
294 file->f_flags & O_NONBLOCK);
295 if (ret == 0 && event->type == UVC_EVENT_SETUP) {
296 struct uvc_event *uvc_event = (void *)&event->u.data;
297
298 /* Tell the complete callback to generate an event for
299 * the next request that will be enqueued by
300 * uvc_event_write.
301 */
302 uvc->event_setup_out =
303 !(uvc_event->req.bRequestType & USB_DIR_IN);
304 uvc->event_length = uvc_event->req.wLength;
305 }
306
307 return ret;
308 }
309
310 case VIDIOC_SUBSCRIBE_EVENT:
311 {
312 struct v4l2_event_subscription *sub = arg;
313
314 if (sub->type < UVC_EVENT_FIRST || sub->type > UVC_EVENT_LAST)
315 return -EINVAL;
316
317 return v4l2_event_subscribe(&handle->vfh, arg);
318 }
319
320 case VIDIOC_UNSUBSCRIBE_EVENT:
321 return v4l2_event_unsubscribe(&handle->vfh, arg);
322
323 case UVCIOC_SEND_RESPONSE:
324 ret = uvc_send_response(uvc, arg);
325 break;
326
327 default:
328 return -ENOIOCTLCMD;
329 }
330
331 return ret;
332}
333
334static long
335uvc_v4l2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
336{
337 return video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);
338}
339
340static int
341uvc_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
342{
343 struct video_device *vdev = video_devdata(file);
344 struct uvc_device *uvc = video_get_drvdata(vdev);
345
346 return uvc_queue_mmap(&uvc->video.queue, vma);
347}
348
349static unsigned int
350uvc_v4l2_poll(struct file *file, poll_table *wait)
351{
352 struct video_device *vdev = video_devdata(file);
353 struct uvc_device *uvc = video_get_drvdata(vdev);
354 struct uvc_file_handle *handle = to_uvc_file_handle(file->private_data);
355 unsigned int mask = 0;
356
357 poll_wait(file, &handle->vfh.events->wait, wait);
358 if (v4l2_event_pending(&handle->vfh))
359 mask |= POLLPRI;
360
361 mask |= uvc_queue_poll(&uvc->video.queue, file, wait);
362
363 return mask;
364}
365
366struct v4l2_file_operations uvc_v4l2_fops = {
367 .owner = THIS_MODULE,
368 .open = uvc_v4l2_open,
369 .release = uvc_v4l2_release,
370 .ioctl = uvc_v4l2_ioctl,
371 .mmap = uvc_v4l2_mmap,
372 .poll = uvc_v4l2_poll,
373};
374
diff --git a/drivers/usb/gadget/uvc_video.c b/drivers/usb/gadget/uvc_video.c
new file mode 100644
index 000000000000..de8cbc46518d
--- /dev/null
+++ b/drivers/usb/gadget/uvc_video.c
@@ -0,0 +1,386 @@
1/*
2 * uvc_video.c -- USB Video Class Gadget driver
3 *
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/device.h>
16#include <linux/errno.h>
17#include <linux/usb/ch9.h>
18#include <linux/usb/gadget.h>
19
20#include <media/v4l2-dev.h>
21
22#include "uvc.h"
23#include "uvc_queue.h"
24
25/* --------------------------------------------------------------------------
26 * Video codecs
27 */
28
29static int
30uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
31 u8 *data, int len)
32{
33 data[0] = 2;
34 data[1] = UVC_STREAM_EOH | video->fid;
35
36 if (buf->buf.bytesused - video->queue.buf_used <= len - 2)
37 data[1] |= UVC_STREAM_EOF;
38
39 return 2;
40}
41
42static int
43uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
44 u8 *data, int len)
45{
46 struct uvc_video_queue *queue = &video->queue;
47 unsigned int nbytes;
48 void *mem;
49
50 /* Copy video data to the USB buffer. */
51 mem = queue->mem + buf->buf.m.offset + queue->buf_used;
52 nbytes = min((unsigned int)len, buf->buf.bytesused - queue->buf_used);
53
54 memcpy(data, mem, nbytes);
55 queue->buf_used += nbytes;
56
57 return nbytes;
58}
59
60static void
61uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
62 struct uvc_buffer *buf)
63{
64 void *mem = req->buf;
65 int len = video->req_size;
66 int ret;
67
68 /* Add a header at the beginning of the payload. */
69 if (video->payload_size == 0) {
70 ret = uvc_video_encode_header(video, buf, mem, len);
71 video->payload_size += ret;
72 mem += ret;
73 len -= ret;
74 }
75
76 /* Process video data. */
77 len = min((int)(video->max_payload_size - video->payload_size), len);
78 ret = uvc_video_encode_data(video, buf, mem, len);
79
80 video->payload_size += ret;
81 len -= ret;
82
83 req->length = video->req_size - len;
84 req->zero = video->payload_size == video->max_payload_size;
85
86 if (buf->buf.bytesused == video->queue.buf_used) {
87 video->queue.buf_used = 0;
88 buf->state = UVC_BUF_STATE_DONE;
89 uvc_queue_next_buffer(&video->queue, buf);
90 video->fid ^= UVC_STREAM_FID;
91
92 video->payload_size = 0;
93 }
94
95 if (video->payload_size == video->max_payload_size ||
96 buf->buf.bytesused == video->queue.buf_used)
97 video->payload_size = 0;
98}
99
100static void
101uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
102 struct uvc_buffer *buf)
103{
104 void *mem = req->buf;
105 int len = video->req_size;
106 int ret;
107
108 /* Add the header. */
109 ret = uvc_video_encode_header(video, buf, mem, len);
110 mem += ret;
111 len -= ret;
112
113 /* Process video data. */
114 ret = uvc_video_encode_data(video, buf, mem, len);
115 len -= ret;
116
117 req->length = video->req_size - len;
118
119 if (buf->buf.bytesused == video->queue.buf_used) {
120 video->queue.buf_used = 0;
121 buf->state = UVC_BUF_STATE_DONE;
122 uvc_queue_next_buffer(&video->queue, buf);
123 video->fid ^= UVC_STREAM_FID;
124 }
125}
126
127/* --------------------------------------------------------------------------
128 * Request handling
129 */
130
131/*
132 * I somehow feel that synchronisation won't be easy to achieve here. We have
133 * three events that control USB requests submission:
134 *
135 * - USB request completion: the completion handler will resubmit the request
136 * if a video buffer is available.
137 *
138 * - USB interface setting selection: in response to a SET_INTERFACE request,
139 * the handler will start streaming if a video buffer is available and if
140 * video is not currently streaming.
141 *
142 * - V4L2 buffer queueing: the driver will start streaming if video is not
143 * currently streaming.
144 *
145 * Race conditions between those 3 events might lead to deadlocks or other
146 * nasty side effects.
147 *
148 * The "video currently streaming" condition can't be detected by the irqqueue
149 * being empty, as a request can still be in flight. A separate "queue paused"
150 * flag is thus needed.
151 *
152 * The paused flag will be set when we try to retrieve the irqqueue head if the
153 * queue is empty, and cleared when we queue a buffer.
154 *
155 * The USB request completion handler will get the buffer at the irqqueue head
156 * under protection of the queue spinlock. If the queue is empty, the streaming
157 * paused flag will be set. Right after releasing the spinlock a userspace
158 * application can queue a buffer. The flag will then cleared, and the ioctl
159 * handler will restart the video stream.
160 */
161static void
162uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
163{
164 struct uvc_video *video = req->context;
165 struct uvc_buffer *buf;
166 unsigned long flags;
167 int ret;
168
169 switch (req->status) {
170 case 0:
171 break;
172
173 case -ESHUTDOWN:
174 printk(KERN_INFO "VS request cancelled.\n");
175 goto requeue;
176
177 default:
178 printk(KERN_INFO "VS request completed with status %d.\n",
179 req->status);
180 goto requeue;
181 }
182
183 spin_lock_irqsave(&video->queue.irqlock, flags);
184 buf = uvc_queue_head(&video->queue);
185 if (buf == NULL) {
186 spin_unlock_irqrestore(&video->queue.irqlock, flags);
187 goto requeue;
188 }
189
190 video->encode(req, video, buf);
191
192 if ((ret = usb_ep_queue(ep, req, GFP_ATOMIC)) < 0) {
193 printk(KERN_INFO "Failed to queue request (%d).\n", ret);
194 usb_ep_set_halt(ep);
195 spin_unlock_irqrestore(&video->queue.irqlock, flags);
196 goto requeue;
197 }
198 spin_unlock_irqrestore(&video->queue.irqlock, flags);
199
200 return;
201
202requeue:
203 spin_lock_irqsave(&video->req_lock, flags);
204 list_add_tail(&req->list, &video->req_free);
205 spin_unlock_irqrestore(&video->req_lock, flags);
206}
207
208static int
209uvc_video_free_requests(struct uvc_video *video)
210{
211 unsigned int i;
212
213 for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
214 if (video->req[i]) {
215 usb_ep_free_request(video->ep, video->req[i]);
216 video->req[i] = NULL;
217 }
218
219 if (video->req_buffer[i]) {
220 kfree(video->req_buffer[i]);
221 video->req_buffer[i] = NULL;
222 }
223 }
224
225 INIT_LIST_HEAD(&video->req_free);
226 video->req_size = 0;
227 return 0;
228}
229
230static int
231uvc_video_alloc_requests(struct uvc_video *video)
232{
233 unsigned int i;
234 int ret = -ENOMEM;
235
236 BUG_ON(video->req_size);
237
238 for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
239 video->req_buffer[i] = kmalloc(video->ep->maxpacket, GFP_KERNEL);
240 if (video->req_buffer[i] == NULL)
241 goto error;
242
243 video->req[i] = usb_ep_alloc_request(video->ep, GFP_KERNEL);
244 if (video->req[i] == NULL)
245 goto error;
246
247 video->req[i]->buf = video->req_buffer[i];
248 video->req[i]->length = 0;
249 video->req[i]->dma = DMA_ADDR_INVALID;
250 video->req[i]->complete = uvc_video_complete;
251 video->req[i]->context = video;
252
253 list_add_tail(&video->req[i]->list, &video->req_free);
254 }
255
256 video->req_size = video->ep->maxpacket;
257 return 0;
258
259error:
260 uvc_video_free_requests(video);
261 return ret;
262}
263
264/* --------------------------------------------------------------------------
265 * Video streaming
266 */
267
268/*
269 * uvc_video_pump - Pump video data into the USB requests
270 *
271 * This function fills the available USB requests (listed in req_free) with
272 * video data from the queued buffers.
273 */
274int
275uvc_video_pump(struct uvc_video *video)
276{
277 struct usb_request *req;
278 struct uvc_buffer *buf;
279 unsigned long flags;
280 int ret;
281
282 /* FIXME TODO Race between uvc_video_pump and requests completion
283 * handler ???
284 */
285
286 while (1) {
287 /* Retrieve the first available USB request, protected by the
288 * request lock.
289 */
290 spin_lock_irqsave(&video->req_lock, flags);
291 if (list_empty(&video->req_free)) {
292 spin_unlock_irqrestore(&video->req_lock, flags);
293 return 0;
294 }
295 req = list_first_entry(&video->req_free, struct usb_request,
296 list);
297 list_del(&req->list);
298 spin_unlock_irqrestore(&video->req_lock, flags);
299
300 /* Retrieve the first available video buffer and fill the
301 * request, protected by the video queue irqlock.
302 */
303 spin_lock_irqsave(&video->queue.irqlock, flags);
304 buf = uvc_queue_head(&video->queue);
305 if (buf == NULL) {
306 spin_unlock_irqrestore(&video->queue.irqlock, flags);
307 break;
308 }
309
310 video->encode(req, video, buf);
311
312 /* Queue the USB request */
313 if ((ret = usb_ep_queue(video->ep, req, GFP_KERNEL)) < 0) {
314 printk(KERN_INFO "Failed to queue request (%d)\n", ret);
315 usb_ep_set_halt(video->ep);
316 spin_unlock_irqrestore(&video->queue.irqlock, flags);
317 break;
318 }
319 spin_unlock_irqrestore(&video->queue.irqlock, flags);
320 }
321
322 spin_lock_irqsave(&video->req_lock, flags);
323 list_add_tail(&req->list, &video->req_free);
324 spin_unlock_irqrestore(&video->req_lock, flags);
325 return 0;
326}
327
328/*
329 * Enable or disable the video stream.
330 */
331int
332uvc_video_enable(struct uvc_video *video, int enable)
333{
334 unsigned int i;
335 int ret;
336
337 if (video->ep == NULL) {
338 printk(KERN_INFO "Video enable failed, device is "
339 "uninitialized.\n");
340 return -ENODEV;
341 }
342
343 if (!enable) {
344 for (i = 0; i < UVC_NUM_REQUESTS; ++i)
345 usb_ep_dequeue(video->ep, video->req[i]);
346
347 uvc_video_free_requests(video);
348 uvc_queue_enable(&video->queue, 0);
349 return 0;
350 }
351
352 if ((ret = uvc_queue_enable(&video->queue, 1)) < 0)
353 return ret;
354
355 if ((ret = uvc_video_alloc_requests(video)) < 0)
356 return ret;
357
358 if (video->max_payload_size) {
359 video->encode = uvc_video_encode_bulk;
360 video->payload_size = 0;
361 } else
362 video->encode = uvc_video_encode_isoc;
363
364 return uvc_video_pump(video);
365}
366
367/*
368 * Initialize the UVC video stream.
369 */
370int
371uvc_video_init(struct uvc_video *video)
372{
373 INIT_LIST_HEAD(&video->req_free);
374 spin_lock_init(&video->req_lock);
375
376 video->fcc = V4L2_PIX_FMT_YUYV;
377 video->bpp = 16;
378 video->width = 320;
379 video->height = 240;
380 video->imagesize = 320 * 240 * 2;
381
382 /* Initialize the video buffers queue. */
383 uvc_queue_init(&video->queue, V4L2_BUF_TYPE_VIDEO_OUTPUT);
384 return 0;
385}
386
diff --git a/drivers/usb/gadget/webcam.c b/drivers/usb/gadget/webcam.c
new file mode 100644
index 000000000000..417fd6887698
--- /dev/null
+++ b/drivers/usb/gadget/webcam.c
@@ -0,0 +1,399 @@
1/*
2 * webcam.c -- USB webcam gadget driver
3 *
4 * Copyright (C) 2009-2010
5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13#include <linux/kernel.h>
14#include <linux/device.h>
15#include <linux/usb/video.h>
16
17#include "f_uvc.h"
18
19/*
20 * Kbuild is not very cooperative with respect to linking separately
21 * compiled library objects into one module. So for now we won't use
22 * separate compilation ... ensuring init/exit sections work to shrink
23 * the runtime footprint, and giving us at least some parts of what
24 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
25 */
26#include "composite.c"
27#include "usbstring.c"
28#include "config.c"
29#include "epautoconf.c"
30
31#include "f_uvc.c"
32#include "uvc_queue.c"
33#include "uvc_v4l2.c"
34#include "uvc_video.c"
35
36/* --------------------------------------------------------------------------
37 * Device descriptor
38 */
39
40#define WEBCAM_VENDOR_ID 0x1d6b /* Linux Foundation */
41#define WEBCAM_PRODUCT_ID 0x0102 /* Webcam A/V gadget */
42#define WEBCAM_DEVICE_BCD 0x0010 /* 0.10 */
43
44static char webcam_vendor_label[] = "Linux Foundation";
45static char webcam_product_label[] = "Webcam gadget";
46static char webcam_config_label[] = "Video";
47
48/* string IDs are assigned dynamically */
49
50#define STRING_MANUFACTURER_IDX 0
51#define STRING_PRODUCT_IDX 1
52#define STRING_DESCRIPTION_IDX 2
53
54static struct usb_string webcam_strings[] = {
55 [STRING_MANUFACTURER_IDX].s = webcam_vendor_label,
56 [STRING_PRODUCT_IDX].s = webcam_product_label,
57 [STRING_DESCRIPTION_IDX].s = webcam_config_label,
58 { }
59};
60
61static struct usb_gadget_strings webcam_stringtab = {
62 .language = 0x0409, /* en-us */
63 .strings = webcam_strings,
64};
65
66static struct usb_gadget_strings *webcam_device_strings[] = {
67 &webcam_stringtab,
68 NULL,
69};
70
71static struct usb_device_descriptor webcam_device_descriptor = {
72 .bLength = USB_DT_DEVICE_SIZE,
73 .bDescriptorType = USB_DT_DEVICE,
74 .bcdUSB = cpu_to_le16(0x0200),
75 .bDeviceClass = USB_CLASS_MISC,
76 .bDeviceSubClass = 0x02,
77 .bDeviceProtocol = 0x01,
78 .bMaxPacketSize0 = 0, /* dynamic */
79 .idVendor = cpu_to_le16(WEBCAM_VENDOR_ID),
80 .idProduct = cpu_to_le16(WEBCAM_PRODUCT_ID),
81 .bcdDevice = cpu_to_le16(WEBCAM_DEVICE_BCD),
82 .iManufacturer = 0, /* dynamic */
83 .iProduct = 0, /* dynamic */
84 .iSerialNumber = 0, /* dynamic */
85 .bNumConfigurations = 0, /* dynamic */
86};
87
88DECLARE_UVC_HEADER_DESCRIPTOR(1);
89
90static const struct UVC_HEADER_DESCRIPTOR(1) uvc_control_header = {
91 .bLength = UVC_DT_HEADER_SIZE(1),
92 .bDescriptorType = USB_DT_CS_INTERFACE,
93 .bDescriptorSubType = UVC_DT_HEADER,
94 .bcdUVC = cpu_to_le16(0x0100),
95 .wTotalLength = 0, /* dynamic */
96 .dwClockFrequency = cpu_to_le32(48000000),
97 .bInCollection = 0, /* dynamic */
98 .baInterfaceNr[0] = 0, /* dynamic */
99};
100
101static const struct uvc_camera_terminal_descriptor uvc_camera_terminal = {
102 .bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3),
103 .bDescriptorType = USB_DT_CS_INTERFACE,
104 .bDescriptorSubType = UVC_DT_INPUT_TERMINAL,
105 .bTerminalID = 1,
106 .wTerminalType = cpu_to_le16(0x0201),
107 .bAssocTerminal = 0,
108 .iTerminal = 0,
109 .wObjectiveFocalLengthMin = cpu_to_le16(0),
110 .wObjectiveFocalLengthMax = cpu_to_le16(0),
111 .wOcularFocalLength = cpu_to_le16(0),
112 .bControlSize = 3,
113 .bmControls[0] = 2,
114 .bmControls[1] = 0,
115 .bmControls[2] = 0,
116};
117
118static const struct uvc_processing_unit_descriptor uvc_processing = {
119 .bLength = UVC_DT_PROCESSING_UNIT_SIZE(2),
120 .bDescriptorType = USB_DT_CS_INTERFACE,
121 .bDescriptorSubType = UVC_DT_PROCESSING_UNIT,
122 .bUnitID = 2,
123 .bSourceID = 1,
124 .wMaxMultiplier = cpu_to_le16(16*1024),
125 .bControlSize = 2,
126 .bmControls[0] = 1,
127 .bmControls[1] = 0,
128 .iProcessing = 0,
129};
130
131static const struct uvc_output_terminal_descriptor uvc_output_terminal = {
132 .bLength = UVC_DT_OUTPUT_TERMINAL_SIZE,
133 .bDescriptorType = USB_DT_CS_INTERFACE,
134 .bDescriptorSubType = UVC_DT_OUTPUT_TERMINAL,
135 .bTerminalID = 3,
136 .wTerminalType = cpu_to_le16(0x0101),
137 .bAssocTerminal = 0,
138 .bSourceID = 2,
139 .iTerminal = 0,
140};
141
142DECLARE_UVC_INPUT_HEADER_DESCRIPTOR(1, 2);
143
144static const struct UVC_INPUT_HEADER_DESCRIPTOR(1, 2) uvc_input_header = {
145 .bLength = UVC_DT_INPUT_HEADER_SIZE(1, 2),
146 .bDescriptorType = USB_DT_CS_INTERFACE,
147 .bDescriptorSubType = UVC_DT_INPUT_HEADER,
148 .bNumFormats = 2,
149 .wTotalLength = 0, /* dynamic */
150 .bEndpointAddress = 0, /* dynamic */
151 .bmInfo = 0,
152 .bTerminalLink = 3,
153 .bStillCaptureMethod = 0,
154 .bTriggerSupport = 0,
155 .bTriggerUsage = 0,
156 .bControlSize = 1,
157 .bmaControls[0][0] = 0,
158 .bmaControls[1][0] = 4,
159};
160
161static const struct uvc_format_uncompressed uvc_format_yuv = {
162 .bLength = UVC_DT_FORMAT_UNCOMPRESSED_SIZE,
163 .bDescriptorType = USB_DT_CS_INTERFACE,
164 .bDescriptorSubType = UVC_DT_FORMAT_UNCOMPRESSED,
165 .bFormatIndex = 1,
166 .bNumFrameDescriptors = 2,
167 .guidFormat =
168 { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00,
169 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71},
170 .bBitsPerPixel = 16,
171 .bDefaultFrameIndex = 1,
172 .bAspectRatioX = 0,
173 .bAspectRatioY = 0,
174 .bmInterfaceFlags = 0,
175 .bCopyProtect = 0,
176};
177
178DECLARE_UVC_FRAME_UNCOMPRESSED(1);
179DECLARE_UVC_FRAME_UNCOMPRESSED(3);
180
181static const struct UVC_FRAME_UNCOMPRESSED(3) uvc_frame_yuv_360p = {
182 .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(3),
183 .bDescriptorType = USB_DT_CS_INTERFACE,
184 .bDescriptorSubType = UVC_DT_FRAME_UNCOMPRESSED,
185 .bFrameIndex = 1,
186 .bmCapabilities = 0,
187 .wWidth = cpu_to_le16(640),
188 .wHeight = cpu_to_le16(360),
189 .dwMinBitRate = cpu_to_le32(18432000),
190 .dwMaxBitRate = cpu_to_le32(55296000),
191 .dwMaxVideoFrameBufferSize = cpu_to_le32(460800),
192 .dwDefaultFrameInterval = cpu_to_le32(666666),
193 .bFrameIntervalType = 3,
194 .dwFrameInterval[0] = cpu_to_le32(666666),
195 .dwFrameInterval[1] = cpu_to_le32(1000000),
196 .dwFrameInterval[2] = cpu_to_le32(5000000),
197};
198
199static const struct UVC_FRAME_UNCOMPRESSED(1) uvc_frame_yuv_720p = {
200 .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(1),
201 .bDescriptorType = USB_DT_CS_INTERFACE,
202 .bDescriptorSubType = UVC_DT_FRAME_UNCOMPRESSED,
203 .bFrameIndex = 2,
204 .bmCapabilities = 0,
205 .wWidth = cpu_to_le16(1280),
206 .wHeight = cpu_to_le16(720),
207 .dwMinBitRate = cpu_to_le32(29491200),
208 .dwMaxBitRate = cpu_to_le32(29491200),
209 .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200),
210 .dwDefaultFrameInterval = cpu_to_le32(5000000),
211 .bFrameIntervalType = 1,
212 .dwFrameInterval[0] = cpu_to_le32(5000000),
213};
214
215static const struct uvc_format_mjpeg uvc_format_mjpg = {
216 .bLength = UVC_DT_FORMAT_MJPEG_SIZE,
217 .bDescriptorType = USB_DT_CS_INTERFACE,
218 .bDescriptorSubType = UVC_DT_FORMAT_MJPEG,
219 .bFormatIndex = 2,
220 .bNumFrameDescriptors = 2,
221 .bmFlags = 0,
222 .bDefaultFrameIndex = 1,
223 .bAspectRatioX = 0,
224 .bAspectRatioY = 0,
225 .bmInterfaceFlags = 0,
226 .bCopyProtect = 0,
227};
228
229DECLARE_UVC_FRAME_MJPEG(1);
230DECLARE_UVC_FRAME_MJPEG(3);
231
232static const struct UVC_FRAME_MJPEG(3) uvc_frame_mjpg_360p = {
233 .bLength = UVC_DT_FRAME_MJPEG_SIZE(3),
234 .bDescriptorType = USB_DT_CS_INTERFACE,
235 .bDescriptorSubType = UVC_DT_FRAME_MJPEG,
236 .bFrameIndex = 1,
237 .bmCapabilities = 0,
238 .wWidth = cpu_to_le16(640),
239 .wHeight = cpu_to_le16(360),
240 .dwMinBitRate = cpu_to_le32(18432000),
241 .dwMaxBitRate = cpu_to_le32(55296000),
242 .dwMaxVideoFrameBufferSize = cpu_to_le32(460800),
243 .dwDefaultFrameInterval = cpu_to_le32(666666),
244 .bFrameIntervalType = 3,
245 .dwFrameInterval[0] = cpu_to_le32(666666),
246 .dwFrameInterval[1] = cpu_to_le32(1000000),
247 .dwFrameInterval[2] = cpu_to_le32(5000000),
248};
249
250static const struct UVC_FRAME_MJPEG(1) uvc_frame_mjpg_720p = {
251 .bLength = UVC_DT_FRAME_MJPEG_SIZE(1),
252 .bDescriptorType = USB_DT_CS_INTERFACE,
253 .bDescriptorSubType = UVC_DT_FRAME_MJPEG,
254 .bFrameIndex = 2,
255 .bmCapabilities = 0,
256 .wWidth = cpu_to_le16(1280),
257 .wHeight = cpu_to_le16(720),
258 .dwMinBitRate = cpu_to_le32(29491200),
259 .dwMaxBitRate = cpu_to_le32(29491200),
260 .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200),
261 .dwDefaultFrameInterval = cpu_to_le32(5000000),
262 .bFrameIntervalType = 1,
263 .dwFrameInterval[0] = cpu_to_le32(5000000),
264};
265
266static const struct uvc_color_matching_descriptor uvc_color_matching = {
267 .bLength = UVC_DT_COLOR_MATCHING_SIZE,
268 .bDescriptorType = USB_DT_CS_INTERFACE,
269 .bDescriptorSubType = UVC_DT_COLOR_MATCHING,
270 .bColorPrimaries = 1,
271 .bTransferCharacteristics = 1,
272 .bMatrixCoefficients = 4,
273};
274
275static const struct uvc_descriptor_header * const uvc_control_cls[] = {
276 (const struct uvc_descriptor_header *) &uvc_control_header,
277 (const struct uvc_descriptor_header *) &uvc_camera_terminal,
278 (const struct uvc_descriptor_header *) &uvc_processing,
279 (const struct uvc_descriptor_header *) &uvc_output_terminal,
280 NULL,
281};
282
283static const struct uvc_descriptor_header * const uvc_fs_streaming_cls[] = {
284 (const struct uvc_descriptor_header *) &uvc_input_header,
285 (const struct uvc_descriptor_header *) &uvc_format_yuv,
286 (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
287 (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
288 (const struct uvc_descriptor_header *) &uvc_format_mjpg,
289 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
290 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
291 (const struct uvc_descriptor_header *) &uvc_color_matching,
292 NULL,
293};
294
295static const struct uvc_descriptor_header * const uvc_hs_streaming_cls[] = {
296 (const struct uvc_descriptor_header *) &uvc_input_header,
297 (const struct uvc_descriptor_header *) &uvc_format_yuv,
298 (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p,
299 (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p,
300 (const struct uvc_descriptor_header *) &uvc_format_mjpg,
301 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p,
302 (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p,
303 (const struct uvc_descriptor_header *) &uvc_color_matching,
304 NULL,
305};
306
307/* --------------------------------------------------------------------------
308 * USB configuration
309 */
310
311static int __init
312webcam_config_bind(struct usb_configuration *c)
313{
314 return uvc_bind_config(c, uvc_control_cls, uvc_fs_streaming_cls,
315 uvc_hs_streaming_cls);
316}
317
318static struct usb_configuration webcam_config_driver = {
319 .label = webcam_config_label,
320 .bind = webcam_config_bind,
321 .bConfigurationValue = 1,
322 .iConfiguration = 0, /* dynamic */
323 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
324 .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
325};
326
327static int /* __init_or_exit */
328webcam_unbind(struct usb_composite_dev *cdev)
329{
330 return 0;
331}
332
333static int __init
334webcam_bind(struct usb_composite_dev *cdev)
335{
336 int ret;
337
338 /* Allocate string descriptor numbers ... note that string contents
339 * can be overridden by the composite_dev glue.
340 */
341 if ((ret = usb_string_id(cdev)) < 0)
342 goto error;
343 webcam_strings[STRING_MANUFACTURER_IDX].id = ret;
344 webcam_device_descriptor.iManufacturer = ret;
345
346 if ((ret = usb_string_id(cdev)) < 0)
347 goto error;
348 webcam_strings[STRING_PRODUCT_IDX].id = ret;
349 webcam_device_descriptor.iProduct = ret;
350
351 if ((ret = usb_string_id(cdev)) < 0)
352 goto error;
353 webcam_strings[STRING_DESCRIPTION_IDX].id = ret;
354 webcam_config_driver.iConfiguration = ret;
355
356 /* Register our configuration. */
357 if ((ret = usb_add_config(cdev, &webcam_config_driver)) < 0)
358 goto error;
359
360 INFO(cdev, "Webcam Video Gadget\n");
361 return 0;
362
363error:
364 webcam_unbind(cdev);
365 return ret;
366}
367
368/* --------------------------------------------------------------------------
369 * Driver
370 */
371
372static struct usb_composite_driver webcam_driver = {
373 .name = "g_webcam",
374 .dev = &webcam_device_descriptor,
375 .strings = webcam_device_strings,
376 .bind = webcam_bind,
377 .unbind = webcam_unbind,
378};
379
380static int __init
381webcam_init(void)
382{
383 return usb_composite_register(&webcam_driver);
384}
385
386static void __exit
387webcam_cleanup(void)
388{
389 usb_composite_unregister(&webcam_driver);
390}
391
392module_init(webcam_init);
393module_exit(webcam_cleanup);
394
395MODULE_AUTHOR("Laurent Pinchart");
396MODULE_DESCRIPTION("Webcam Video Gadget");
397MODULE_LICENSE("GPL");
398MODULE_VERSION("0.1.0");
399