27 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/utsname.h>
34 #include <linux/usb/ch9.h>
55 #define USB_ETH_RNDIS y
66 static const char longname[] =
"Configurable Composite Gadget";
69 #define VENDOR_ID 0x1d6b
70 #define PRODUCT_ID 0x0107
71 #define GFS_MAX_DEVS 10
126 static struct class *ccg_class;
127 static struct ccg_dev *_ccg_dev;
131 static char func_names_buf[256];
134 .bLength =
sizeof(device_desc),
141 .bNumConfigurations = 1,
146 .unbind = ccg_unbind_config,
147 .bConfigurationValue = 1,
157 static char *connected[2] = {
"USB_STATE=CONNECTED",
NULL };
159 char **uevent_envp =
NULL;
168 spin_unlock_irqrestore(&cdev->
lock, flags);
172 pr_info(
"%s: sent uevent %s\n", __func__, uevent_envp[0]);
174 pr_info(
"%s: did not send uevent (%d %d %p)\n", __func__,
184 const char *dev_name)
195 static bool functionfs_all_ready(
struct ccg_dev *dev)
206 static int functionfs_ready_callback(
struct ffs_data *
ffs)
224 if (functionfs_all_ready(_ccg_dev)) {
235 static void reset_usb(
struct ccg_dev *dev)
238 usb_ep_dequeue(dev->
cdev->gadget->ep0, dev->
cdev->req);
241 usb_gadget_disconnect(dev->
cdev->gadget);
244 static void functionfs_closed_callback(
struct ffs_data *ffs)
246 struct ffs_obj *ffs_obj;
263 static void *functionfs_acquire_dev_callback(
const char *dev_name)
265 struct ffs_obj *ffs_dev;
269 ffs_dev = functionfs_find_dev(_ccg_dev, dev_name);
271 ffs_dev = ERR_PTR(-
ENODEV);
276 ffs_dev = ERR_PTR(-
EBUSY);
288 struct ffs_obj *ffs_dev;
302 return functionfs_init();
307 functionfs_cleanup();
313 struct ccg_dev *dev = _ccg_dev;
319 ret = functionfs_bind(dev->
ffs_tab[i].ffs_data, c->
cdev);
322 functionfs_unbind(dev->
ffs_tab[i].ffs_data);
330 ret = functionfs_bind_config(c->
cdev, c,
342 struct ccg_dev *dev = _ccg_dev;
347 functionfs_unbind(dev->
ffs_tab[i].ffs_data);
350 static ssize_t functionfs_user_functions_show(
struct device *_dev,
354 struct ccg_dev *dev = _ccg_dev;
371 static ssize_t functionfs_user_functions_store(
struct device *_dev,
373 const char *buff,
size_t size)
375 struct ccg_dev *dev = _ccg_dev;
397 strlcpy(func_names_buf, buff,
sizeof(func_names_buf));
398 b =
strim(func_names_buf);
408 if (functionfs_find_dev(dev, name)) {
421 functionfs_user_functions_show,
422 functionfs_user_functions_store);
424 static ssize_t functionfs_max_user_functions_show(
struct device *_dev,
432 functionfs_max_user_functions_show,
NULL);
435 &dev_attr_user_functions,
436 &dev_attr_max_user_functions,
442 .init = functionfs_function_init,
443 .cleanup = functionfs_function_cleanup,
444 .bind_config = functionfs_function_bind_config,
445 .unbind_config = functionfs_function_unbind_config,
446 .attributes = functionfs_function_attributes,
449 #define MAX_ACM_INSTANCES 4
479 for (i = 0; i < config->
instances; i++) {
482 pr_err(
"Could not bind acm%u config\n", i);
519 acm_instances_store);
527 .init = acm_function_init,
528 .cleanup = acm_function_cleanup,
529 .bind_config = acm_function_bind_config,
530 .attributes = acm_function_attributes,
563 pr_err(
"%s: rndis_pdata\n", __func__);
571 pr_err(
"%s: gether_setup failed\n", __func__);
577 rndis_iad_descriptor.bFunctionClass =
579 rndis_iad_descriptor.bFunctionSubClass = 0x01;
580 rndis_iad_descriptor.bFunctionProtocol = 0x03;
581 rndis_control_intf.bInterfaceClass =
583 rndis_control_intf.bInterfaceSubClass = 0x01;
584 rndis_control_intf.bInterfaceProtocol = 0x03;
620 rndis_manufacturer_store);
663 unsigned char tmp[6];
665 if (
sscanf(buf,
"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
666 tmp + 0, tmp + 1, tmp + 2, tmp + 3, tmp + 4, tmp + 5) !=
677 rndis_ethaddr_store);
695 ret = kstrtou32(buf, 16, &value);
705 rndis_vendorID_store);
708 &dev_attr_manufacturer,
717 .init = rndis_function_init,
718 .cleanup = rndis_function_cleanup,
719 .bind_config = rndis_function_bind_config,
720 .unbind_config = rndis_function_unbind_config,
721 .attributes = rndis_function_attributes,
733 fsg.luns[0].removable = 1;
737 common = fsg_common_init(
NULL, cdev, &
fsg);
739 return PTR_ERR(common);
742 &common->
luns[0].dev.kobj,
745 fsg_common_put(common);
755 fsg_common_put(f->
config);
763 return fsg_bind_config(c->
cdev, c, common);
767 .name =
"mass_storage",
768 .init = mass_storage_function_init,
769 .cleanup = mass_storage_function_cleanup,
770 .bind_config = mass_storage_function_bind_config,
774 &functionfs_function,
777 &mass_storage_function,
785 struct ccg_dev *dev = _ccg_dev;
792 for (; (f = *functions++); index++) {
795 pr_err(
"%s: Failed to alloc name %s", __func__,
802 if (IS_ERR(f->
dev)) {
803 pr_err(
"%s: Failed to create dev %s", __func__,
805 err = PTR_ERR(f->
dev);
811 err = f->
init(f, cdev);
813 pr_err(
"%s: Failed to init %s", __func__,
821 while ((attr = *attrs++) && !err)
825 pr_err(
"%s: Failed to create function %s attributes",
860 static int ccg_bind_enabled_functions(
struct ccg_dev *dev,
876 static void ccg_unbind_enabled_functions(
struct ccg_dev *dev,
886 static
int ccg_enable_function(
struct ccg_dev *dev,
char *name)
890 while ((f = *functions++)) {
893 &dev->enabled_functions);
914 buff +=
sprintf(buff, "%
s,", f->name);
915 for (i = 0; i < dev->max_func_num; i++)
917 buff +=
sprintf(buff, "%s", dev->ffs_tab[i].name);
928 const char *buff,
size_t size)
934 bool functionfs_enabled;
948 functionfs_enabled =
false;
952 strlcpy(buf, buff,
sizeof(buf));
956 struct ffs_obj *user_func;
960 if (!
strcmp(name, functionfs_function.
name)) {
961 pr_err(
"ccg_usb: Cannot explicitly enable '%s'", name);
964 user_func = functionfs_find_dev(dev, name);
966 name = functionfs_function.
name;
968 if (!user_func || !functionfs_enabled)
969 err = ccg_enable_function(dev, name);
971 pr_err(
"ccg_usb: Cannot enable '%s'", name);
972 else if (user_func) {
973 user_func->
used =
true;
975 functionfs_enabled =
true;
992 const char *buff,
size_t size)
999 sscanf(buff,
"%d", &enabled);
1000 if (enabled && dev->
func_num && !functionfs_all_ready(dev)) {
1005 if (enabled && !dev->
enabled) {
1022 ret = usb_gadget_connect(cdev->
gadget);
1027 }
else if (!enabled && dev->
enabled) {
1030 pr_err(
"ccg_usb: already %s\n",
1031 dev->
enabled ?
"enabled" :
"disabled");
1043 char *
state =
"DISCONNECTED";
1044 unsigned long flags;
1051 state =
"CONFIGURED";
1053 state =
"CONNECTED";
1054 spin_unlock_irqrestore(&cdev->
lock, flags);
1056 return sprintf(buf,
"%s\n", state);
1059 #define DESCRIPTOR_ATTR(field, format_string) \
1061 field ## _show(struct device *dev, struct device_attribute *attr, \
1064 return sprintf(buf, format_string, device_desc.field); \
1067 field ## _store(struct device *dev, struct device_attribute *attr, \
1068 const char *buf, size_t size) \
1071 if (sscanf(buf, format_string, &value) == 1) { \
1072 device_desc.field = value; \
1077 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, field ## _show, field ## _store);
1089 &dev_attr_bDeviceClass,
1090 &dev_attr_bDeviceSubClass,
1091 &dev_attr_bDeviceProtocol,
1092 &dev_attr_functions,
1103 struct ccg_dev *dev = _ccg_dev;
1104 return ccg_bind_enabled_functions(dev, c);
1109 struct ccg_dev *dev = _ccg_dev;
1111 ccg_unbind_enabled_functions(dev, c);
1118 struct ccg_dev *dev = _ccg_dev;
1126 usb_gadget_disconnect(gadget);
1128 ret = ccg_init_functions(dev->
functions, cdev);
1132 gcnum = usb_gadget_controller_number(gadget);
1136 pr_warn(
"%s: controller '%s' not recognized\n",
1137 longname, gadget->
name);
1141 usb_gadget_set_selfpowered(gadget);
1149 struct ccg_dev *dev = _ccg_dev;
1157 .name =
"configurable_usb",
1158 .dev = &device_desc,
1160 .unbind = ccg_usb_unbind,
1161 .needs_serial =
true,
1162 .iManufacturer =
"Linux Foundation",
1163 .iProduct = longname,
1164 .iSerialNumber =
"1234567890123456",
1169 struct ccg_dev *dev = _ccg_dev;
1174 unsigned long flags;
1177 req->
complete = composite_setup_complete;
1179 gadget->
ep0->driver_data =
cdev;
1190 value = composite_setup(gadget, c);
1200 spin_unlock_irqrestore(&cdev->
lock, flags);
1205 static void ccg_disconnect(
struct usb_gadget *gadget)
1207 struct ccg_dev *dev = _ccg_dev;
1209 unsigned long flags;
1211 composite_disconnect(gadget);
1216 spin_unlock_irqrestore(&cdev->
lock, flags);
1219 static int ccg_create_device(
struct ccg_dev *dev)
1226 if (IS_ERR(dev->
dev))
1227 return PTR_ERR(dev->
dev);
1231 while ((attr = *attrs++)) {
1248 if (IS_ERR(ccg_class))
1249 return PTR_ERR(ccg_class);
1262 err = ccg_create_device(dev);
1272 composite_driver.setup = ccg_setup;
1273 composite_driver.disconnect = ccg_disconnect;
1285 static void __exit cleanup(
void)