15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
28443f2d2SAndrzej Pietrasiewicz /*
38443f2d2SAndrzej Pietrasiewicz * g_ffs.c -- user mode file system API for USB composite function controllers
48443f2d2SAndrzej Pietrasiewicz *
58443f2d2SAndrzej Pietrasiewicz * Copyright (C) 2010 Samsung Electronics
68443f2d2SAndrzej Pietrasiewicz * Author: Michal Nazarewicz <mina86@mina86.com>
78443f2d2SAndrzej Pietrasiewicz */
88443f2d2SAndrzej Pietrasiewicz
98443f2d2SAndrzej Pietrasiewicz #define pr_fmt(fmt) "g_ffs: " fmt
108443f2d2SAndrzej Pietrasiewicz
118443f2d2SAndrzej Pietrasiewicz #include <linux/module.h>
128443f2d2SAndrzej Pietrasiewicz
138443f2d2SAndrzej Pietrasiewicz #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
148443f2d2SAndrzej Pietrasiewicz #include <linux/netdevice.h>
158443f2d2SAndrzej Pietrasiewicz
168443f2d2SAndrzej Pietrasiewicz # if defined USB_ETH_RNDIS
178443f2d2SAndrzej Pietrasiewicz # undef USB_ETH_RNDIS
188443f2d2SAndrzej Pietrasiewicz # endif
198443f2d2SAndrzej Pietrasiewicz # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
208443f2d2SAndrzej Pietrasiewicz # define USB_ETH_RNDIS y
218443f2d2SAndrzej Pietrasiewicz # endif
228443f2d2SAndrzej Pietrasiewicz
238443f2d2SAndrzej Pietrasiewicz # include "u_ecm.h"
248443f2d2SAndrzej Pietrasiewicz # include "u_gether.h"
258443f2d2SAndrzej Pietrasiewicz # ifdef USB_ETH_RNDIS
268443f2d2SAndrzej Pietrasiewicz # include "u_rndis.h"
278443f2d2SAndrzej Pietrasiewicz # include "rndis.h"
288443f2d2SAndrzej Pietrasiewicz # endif
298443f2d2SAndrzej Pietrasiewicz # include "u_ether.h"
308443f2d2SAndrzej Pietrasiewicz
318443f2d2SAndrzej Pietrasiewicz USB_ETHERNET_MODULE_PARAMETERS();
328443f2d2SAndrzej Pietrasiewicz
338443f2d2SAndrzej Pietrasiewicz # ifdef CONFIG_USB_FUNCTIONFS_ETH
348443f2d2SAndrzej Pietrasiewicz static int eth_bind_config(struct usb_configuration *c);
358443f2d2SAndrzej Pietrasiewicz static struct usb_function_instance *fi_ecm;
368443f2d2SAndrzej Pietrasiewicz static struct usb_function *f_ecm;
378443f2d2SAndrzej Pietrasiewicz static struct usb_function_instance *fi_geth;
388443f2d2SAndrzej Pietrasiewicz static struct usb_function *f_geth;
398443f2d2SAndrzej Pietrasiewicz # endif
408443f2d2SAndrzej Pietrasiewicz # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
418443f2d2SAndrzej Pietrasiewicz static int bind_rndis_config(struct usb_configuration *c);
428443f2d2SAndrzej Pietrasiewicz static struct usb_function_instance *fi_rndis;
438443f2d2SAndrzej Pietrasiewicz static struct usb_function *f_rndis;
448443f2d2SAndrzej Pietrasiewicz # endif
458443f2d2SAndrzej Pietrasiewicz #endif
468443f2d2SAndrzej Pietrasiewicz
478443f2d2SAndrzej Pietrasiewicz #include "u_fs.h"
488443f2d2SAndrzej Pietrasiewicz
498443f2d2SAndrzej Pietrasiewicz #define DRIVER_NAME "g_ffs"
508443f2d2SAndrzej Pietrasiewicz #define DRIVER_DESC "USB Function Filesystem"
518443f2d2SAndrzej Pietrasiewicz #define DRIVER_VERSION "24 Aug 2004"
528443f2d2SAndrzej Pietrasiewicz
538443f2d2SAndrzej Pietrasiewicz MODULE_DESCRIPTION(DRIVER_DESC);
548443f2d2SAndrzej Pietrasiewicz MODULE_AUTHOR("Michal Nazarewicz");
558443f2d2SAndrzej Pietrasiewicz MODULE_LICENSE("GPL");
568443f2d2SAndrzej Pietrasiewicz
578443f2d2SAndrzej Pietrasiewicz #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
588443f2d2SAndrzej Pietrasiewicz #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
598443f2d2SAndrzej Pietrasiewicz
608443f2d2SAndrzej Pietrasiewicz #define GFS_MAX_DEVS 10
618443f2d2SAndrzej Pietrasiewicz
628443f2d2SAndrzej Pietrasiewicz USB_GADGET_COMPOSITE_OPTIONS();
638443f2d2SAndrzej Pietrasiewicz
648443f2d2SAndrzej Pietrasiewicz static struct usb_device_descriptor gfs_dev_desc = {
658443f2d2SAndrzej Pietrasiewicz .bLength = sizeof gfs_dev_desc,
668443f2d2SAndrzej Pietrasiewicz .bDescriptorType = USB_DT_DEVICE,
678443f2d2SAndrzej Pietrasiewicz
680aecfc1bSIgor Kotrasinski /* .bcdUSB = DYNAMIC */
698443f2d2SAndrzej Pietrasiewicz .bDeviceClass = USB_CLASS_PER_INTERFACE,
708443f2d2SAndrzej Pietrasiewicz
718443f2d2SAndrzej Pietrasiewicz .idVendor = cpu_to_le16(GFS_VENDOR_ID),
728443f2d2SAndrzej Pietrasiewicz .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
738443f2d2SAndrzej Pietrasiewicz };
748443f2d2SAndrzej Pietrasiewicz
758443f2d2SAndrzej Pietrasiewicz static char *func_names[GFS_MAX_DEVS];
768443f2d2SAndrzej Pietrasiewicz static unsigned int func_num;
778443f2d2SAndrzej Pietrasiewicz
788443f2d2SAndrzej Pietrasiewicz module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
798443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(bDeviceClass, "USB Device class");
808443f2d2SAndrzej Pietrasiewicz module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
818443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
828443f2d2SAndrzej Pietrasiewicz module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
838443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
848443f2d2SAndrzej Pietrasiewicz module_param_array_named(functions, func_names, charp, &func_num, 0);
858443f2d2SAndrzej Pietrasiewicz MODULE_PARM_DESC(functions, "USB Functions list");
868443f2d2SAndrzej Pietrasiewicz
8775c9310aSLi Jun static const struct usb_descriptor_header *gfs_otg_desc[2];
888443f2d2SAndrzej Pietrasiewicz
898443f2d2SAndrzej Pietrasiewicz /* String IDs are assigned dynamically */
908443f2d2SAndrzej Pietrasiewicz static struct usb_string gfs_strings[] = {
918443f2d2SAndrzej Pietrasiewicz [USB_GADGET_MANUFACTURER_IDX].s = "",
928443f2d2SAndrzej Pietrasiewicz [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
938443f2d2SAndrzej Pietrasiewicz [USB_GADGET_SERIAL_IDX].s = "",
948443f2d2SAndrzej Pietrasiewicz #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
958443f2d2SAndrzej Pietrasiewicz { .s = "FunctionFS + RNDIS" },
968443f2d2SAndrzej Pietrasiewicz #endif
978443f2d2SAndrzej Pietrasiewicz #ifdef CONFIG_USB_FUNCTIONFS_ETH
988443f2d2SAndrzej Pietrasiewicz { .s = "FunctionFS + ECM" },
998443f2d2SAndrzej Pietrasiewicz #endif
1008443f2d2SAndrzej Pietrasiewicz #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
1018443f2d2SAndrzej Pietrasiewicz { .s = "FunctionFS" },
1028443f2d2SAndrzej Pietrasiewicz #endif
1038443f2d2SAndrzej Pietrasiewicz { } /* end of list */
1048443f2d2SAndrzej Pietrasiewicz };
1058443f2d2SAndrzej Pietrasiewicz
1068443f2d2SAndrzej Pietrasiewicz static struct usb_gadget_strings *gfs_dev_strings[] = {
1078443f2d2SAndrzej Pietrasiewicz &(struct usb_gadget_strings) {
1088443f2d2SAndrzej Pietrasiewicz .language = 0x0409, /* en-us */
1098443f2d2SAndrzej Pietrasiewicz .strings = gfs_strings,
1108443f2d2SAndrzej Pietrasiewicz },
1118443f2d2SAndrzej Pietrasiewicz NULL,
1128443f2d2SAndrzej Pietrasiewicz };
1138443f2d2SAndrzej Pietrasiewicz
1148443f2d2SAndrzej Pietrasiewicz struct gfs_configuration {
1158443f2d2SAndrzej Pietrasiewicz struct usb_configuration c;
1168443f2d2SAndrzej Pietrasiewicz int (*eth)(struct usb_configuration *c);
1178443f2d2SAndrzej Pietrasiewicz int num;
1182b87cd24SLad, Prabhakar };
1192b87cd24SLad, Prabhakar
1202b87cd24SLad, Prabhakar static struct gfs_configuration gfs_configurations[] = {
1218443f2d2SAndrzej Pietrasiewicz #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
1228443f2d2SAndrzej Pietrasiewicz {
1238443f2d2SAndrzej Pietrasiewicz .eth = bind_rndis_config,
1248443f2d2SAndrzej Pietrasiewicz },
1258443f2d2SAndrzej Pietrasiewicz #endif
1268443f2d2SAndrzej Pietrasiewicz
1278443f2d2SAndrzej Pietrasiewicz #ifdef CONFIG_USB_FUNCTIONFS_ETH
1288443f2d2SAndrzej Pietrasiewicz {
1298443f2d2SAndrzej Pietrasiewicz .eth = eth_bind_config,
1308443f2d2SAndrzej Pietrasiewicz },
1318443f2d2SAndrzej Pietrasiewicz #endif
1328443f2d2SAndrzej Pietrasiewicz
1338443f2d2SAndrzej Pietrasiewicz #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
1348443f2d2SAndrzej Pietrasiewicz {
1358443f2d2SAndrzej Pietrasiewicz },
1368443f2d2SAndrzej Pietrasiewicz #endif
1378443f2d2SAndrzej Pietrasiewicz };
1388443f2d2SAndrzej Pietrasiewicz
1398443f2d2SAndrzej Pietrasiewicz static void *functionfs_acquire_dev(struct ffs_dev *dev);
1408443f2d2SAndrzej Pietrasiewicz static void functionfs_release_dev(struct ffs_dev *dev);
1418443f2d2SAndrzej Pietrasiewicz static int functionfs_ready_callback(struct ffs_data *ffs);
1428443f2d2SAndrzej Pietrasiewicz static void functionfs_closed_callback(struct ffs_data *ffs);
1438443f2d2SAndrzej Pietrasiewicz static int gfs_bind(struct usb_composite_dev *cdev);
1448443f2d2SAndrzej Pietrasiewicz static int gfs_unbind(struct usb_composite_dev *cdev);
1458443f2d2SAndrzej Pietrasiewicz static int gfs_do_config(struct usb_configuration *c);
1468443f2d2SAndrzej Pietrasiewicz
1478443f2d2SAndrzej Pietrasiewicz
148c94e289fSArnd Bergmann static struct usb_composite_driver gfs_driver = {
1498443f2d2SAndrzej Pietrasiewicz .name = DRIVER_NAME,
1508443f2d2SAndrzej Pietrasiewicz .dev = &gfs_dev_desc,
1518443f2d2SAndrzej Pietrasiewicz .strings = gfs_dev_strings,
152*463f67aeSRoger Quadros .max_speed = USB_SPEED_SUPER,
1538443f2d2SAndrzej Pietrasiewicz .bind = gfs_bind,
1548443f2d2SAndrzej Pietrasiewicz .unbind = gfs_unbind,
1558443f2d2SAndrzej Pietrasiewicz };
1568443f2d2SAndrzej Pietrasiewicz
1578443f2d2SAndrzej Pietrasiewicz static unsigned int missing_funcs;
1588443f2d2SAndrzej Pietrasiewicz static bool gfs_registered;
1598443f2d2SAndrzej Pietrasiewicz static bool gfs_single_func;
1608443f2d2SAndrzej Pietrasiewicz static struct usb_function_instance **fi_ffs;
1618443f2d2SAndrzej Pietrasiewicz static struct usb_function **f_ffs[] = {
1628443f2d2SAndrzej Pietrasiewicz #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
1638443f2d2SAndrzej Pietrasiewicz NULL,
1648443f2d2SAndrzej Pietrasiewicz #endif
1658443f2d2SAndrzej Pietrasiewicz
1668443f2d2SAndrzej Pietrasiewicz #ifdef CONFIG_USB_FUNCTIONFS_ETH
1678443f2d2SAndrzej Pietrasiewicz NULL,
1688443f2d2SAndrzej Pietrasiewicz #endif
1698443f2d2SAndrzej Pietrasiewicz
1708443f2d2SAndrzej Pietrasiewicz #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
1718443f2d2SAndrzej Pietrasiewicz NULL,
1728443f2d2SAndrzej Pietrasiewicz #endif
1738443f2d2SAndrzej Pietrasiewicz };
1748443f2d2SAndrzej Pietrasiewicz
1758443f2d2SAndrzej Pietrasiewicz #define N_CONF ARRAY_SIZE(f_ffs)
1768443f2d2SAndrzej Pietrasiewicz
gfs_init(void)1778443f2d2SAndrzej Pietrasiewicz static int __init gfs_init(void)
1788443f2d2SAndrzej Pietrasiewicz {
1798443f2d2SAndrzej Pietrasiewicz struct f_fs_opts *opts;
1808443f2d2SAndrzej Pietrasiewicz int i;
1818443f2d2SAndrzej Pietrasiewicz int ret = 0;
1828443f2d2SAndrzej Pietrasiewicz
1838443f2d2SAndrzej Pietrasiewicz if (func_num < 2) {
1848443f2d2SAndrzej Pietrasiewicz gfs_single_func = true;
1858443f2d2SAndrzej Pietrasiewicz func_num = 1;
1868443f2d2SAndrzej Pietrasiewicz }
1878443f2d2SAndrzej Pietrasiewicz
1888443f2d2SAndrzej Pietrasiewicz /*
1898443f2d2SAndrzej Pietrasiewicz * Allocate in one chunk for easier maintenance
1908443f2d2SAndrzej Pietrasiewicz */
1918443f2d2SAndrzej Pietrasiewicz f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL);
1928443f2d2SAndrzej Pietrasiewicz if (!f_ffs[0]) {
1938443f2d2SAndrzej Pietrasiewicz ret = -ENOMEM;
1948443f2d2SAndrzej Pietrasiewicz goto no_func;
1958443f2d2SAndrzej Pietrasiewicz }
1968443f2d2SAndrzej Pietrasiewicz for (i = 1; i < N_CONF; ++i)
1978443f2d2SAndrzej Pietrasiewicz f_ffs[i] = f_ffs[0] + i * func_num;
1988443f2d2SAndrzej Pietrasiewicz
1998443f2d2SAndrzej Pietrasiewicz fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL);
2008443f2d2SAndrzej Pietrasiewicz if (!fi_ffs) {
2018443f2d2SAndrzej Pietrasiewicz ret = -ENOMEM;
2028443f2d2SAndrzej Pietrasiewicz goto no_func;
2038443f2d2SAndrzej Pietrasiewicz }
2048443f2d2SAndrzej Pietrasiewicz
2058443f2d2SAndrzej Pietrasiewicz for (i = 0; i < func_num; i++) {
2068443f2d2SAndrzej Pietrasiewicz fi_ffs[i] = usb_get_function_instance("ffs");
2078443f2d2SAndrzej Pietrasiewicz if (IS_ERR(fi_ffs[i])) {
2088443f2d2SAndrzej Pietrasiewicz ret = PTR_ERR(fi_ffs[i]);
2098443f2d2SAndrzej Pietrasiewicz --i;
2108443f2d2SAndrzej Pietrasiewicz goto no_dev;
2118443f2d2SAndrzej Pietrasiewicz }
2128443f2d2SAndrzej Pietrasiewicz opts = to_f_fs_opts(fi_ffs[i]);
2138443f2d2SAndrzej Pietrasiewicz if (gfs_single_func)
2148443f2d2SAndrzej Pietrasiewicz ret = ffs_single_dev(opts->dev);
2158443f2d2SAndrzej Pietrasiewicz else
2168443f2d2SAndrzej Pietrasiewicz ret = ffs_name_dev(opts->dev, func_names[i]);
2178443f2d2SAndrzej Pietrasiewicz if (ret)
2188443f2d2SAndrzej Pietrasiewicz goto no_dev;
2198443f2d2SAndrzej Pietrasiewicz opts->dev->ffs_ready_callback = functionfs_ready_callback;
2208443f2d2SAndrzej Pietrasiewicz opts->dev->ffs_closed_callback = functionfs_closed_callback;
2218443f2d2SAndrzej Pietrasiewicz opts->dev->ffs_acquire_dev_callback = functionfs_acquire_dev;
2228443f2d2SAndrzej Pietrasiewicz opts->dev->ffs_release_dev_callback = functionfs_release_dev;
2238443f2d2SAndrzej Pietrasiewicz opts->no_configfs = true;
2248443f2d2SAndrzej Pietrasiewicz }
2258443f2d2SAndrzej Pietrasiewicz
2268443f2d2SAndrzej Pietrasiewicz missing_funcs = func_num;
2278443f2d2SAndrzej Pietrasiewicz
2288443f2d2SAndrzej Pietrasiewicz return 0;
2298443f2d2SAndrzej Pietrasiewicz no_dev:
2308443f2d2SAndrzej Pietrasiewicz while (i >= 0)
2318443f2d2SAndrzej Pietrasiewicz usb_put_function_instance(fi_ffs[i--]);
2328443f2d2SAndrzej Pietrasiewicz kfree(fi_ffs);
2338443f2d2SAndrzej Pietrasiewicz no_func:
2348443f2d2SAndrzej Pietrasiewicz kfree(f_ffs[0]);
2358443f2d2SAndrzej Pietrasiewicz return ret;
2368443f2d2SAndrzej Pietrasiewicz }
2378443f2d2SAndrzej Pietrasiewicz module_init(gfs_init);
2388443f2d2SAndrzej Pietrasiewicz
gfs_exit(void)2398443f2d2SAndrzej Pietrasiewicz static void __exit gfs_exit(void)
2408443f2d2SAndrzej Pietrasiewicz {
2418443f2d2SAndrzej Pietrasiewicz int i;
2428443f2d2SAndrzej Pietrasiewicz
2438443f2d2SAndrzej Pietrasiewicz if (gfs_registered)
2448443f2d2SAndrzej Pietrasiewicz usb_composite_unregister(&gfs_driver);
2458443f2d2SAndrzej Pietrasiewicz gfs_registered = false;
2468443f2d2SAndrzej Pietrasiewicz
2478443f2d2SAndrzej Pietrasiewicz kfree(f_ffs[0]);
2488443f2d2SAndrzej Pietrasiewicz
2498443f2d2SAndrzej Pietrasiewicz for (i = 0; i < func_num; i++)
2508443f2d2SAndrzej Pietrasiewicz usb_put_function_instance(fi_ffs[i]);
2518443f2d2SAndrzej Pietrasiewicz
2528443f2d2SAndrzej Pietrasiewicz kfree(fi_ffs);
2538443f2d2SAndrzej Pietrasiewicz }
2548443f2d2SAndrzej Pietrasiewicz module_exit(gfs_exit);
2558443f2d2SAndrzej Pietrasiewicz
functionfs_acquire_dev(struct ffs_dev * dev)2568443f2d2SAndrzej Pietrasiewicz static void *functionfs_acquire_dev(struct ffs_dev *dev)
2578443f2d2SAndrzej Pietrasiewicz {
2588443f2d2SAndrzej Pietrasiewicz if (!try_module_get(THIS_MODULE))
2598443f2d2SAndrzej Pietrasiewicz return ERR_PTR(-ENOENT);
2608443f2d2SAndrzej Pietrasiewicz
2612b87cd24SLad, Prabhakar return NULL;
2628443f2d2SAndrzej Pietrasiewicz }
2638443f2d2SAndrzej Pietrasiewicz
functionfs_release_dev(struct ffs_dev * dev)2648443f2d2SAndrzej Pietrasiewicz static void functionfs_release_dev(struct ffs_dev *dev)
2658443f2d2SAndrzej Pietrasiewicz {
2668443f2d2SAndrzej Pietrasiewicz module_put(THIS_MODULE);
2678443f2d2SAndrzej Pietrasiewicz }
2688443f2d2SAndrzej Pietrasiewicz
2698443f2d2SAndrzej Pietrasiewicz /*
2708443f2d2SAndrzej Pietrasiewicz * The caller of this function takes ffs_lock
2718443f2d2SAndrzej Pietrasiewicz */
functionfs_ready_callback(struct ffs_data * ffs)2728443f2d2SAndrzej Pietrasiewicz static int functionfs_ready_callback(struct ffs_data *ffs)
2738443f2d2SAndrzej Pietrasiewicz {
2748443f2d2SAndrzej Pietrasiewicz int ret = 0;
2758443f2d2SAndrzej Pietrasiewicz
2768443f2d2SAndrzej Pietrasiewicz if (--missing_funcs)
2778443f2d2SAndrzej Pietrasiewicz return 0;
2788443f2d2SAndrzej Pietrasiewicz
2798443f2d2SAndrzej Pietrasiewicz if (gfs_registered)
2808443f2d2SAndrzej Pietrasiewicz return -EBUSY;
2818443f2d2SAndrzej Pietrasiewicz
2828443f2d2SAndrzej Pietrasiewicz gfs_registered = true;
2838443f2d2SAndrzej Pietrasiewicz
2848443f2d2SAndrzej Pietrasiewicz ret = usb_composite_probe(&gfs_driver);
285c41b33c5SKrzysztof Opasiak if (unlikely(ret < 0)) {
286c41b33c5SKrzysztof Opasiak ++missing_funcs;
2878443f2d2SAndrzej Pietrasiewicz gfs_registered = false;
288c41b33c5SKrzysztof Opasiak }
2898443f2d2SAndrzej Pietrasiewicz
2908443f2d2SAndrzej Pietrasiewicz return ret;
2918443f2d2SAndrzej Pietrasiewicz }
2928443f2d2SAndrzej Pietrasiewicz
2938443f2d2SAndrzej Pietrasiewicz /*
2948443f2d2SAndrzej Pietrasiewicz * The caller of this function takes ffs_lock
2958443f2d2SAndrzej Pietrasiewicz */
functionfs_closed_callback(struct ffs_data * ffs)2968443f2d2SAndrzej Pietrasiewicz static void functionfs_closed_callback(struct ffs_data *ffs)
2978443f2d2SAndrzej Pietrasiewicz {
2988443f2d2SAndrzej Pietrasiewicz missing_funcs++;
2998443f2d2SAndrzej Pietrasiewicz
3008443f2d2SAndrzej Pietrasiewicz if (gfs_registered)
3018443f2d2SAndrzej Pietrasiewicz usb_composite_unregister(&gfs_driver);
3028443f2d2SAndrzej Pietrasiewicz gfs_registered = false;
3038443f2d2SAndrzej Pietrasiewicz }
3048443f2d2SAndrzej Pietrasiewicz
3058443f2d2SAndrzej Pietrasiewicz /*
3068443f2d2SAndrzej Pietrasiewicz * It is assumed that gfs_bind is called from a context where ffs_lock is held
3078443f2d2SAndrzej Pietrasiewicz */
gfs_bind(struct usb_composite_dev * cdev)3088443f2d2SAndrzej Pietrasiewicz static int gfs_bind(struct usb_composite_dev *cdev)
3098443f2d2SAndrzej Pietrasiewicz {
3108443f2d2SAndrzej Pietrasiewicz #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
3118443f2d2SAndrzej Pietrasiewicz struct net_device *net;
3128443f2d2SAndrzej Pietrasiewicz #endif
3138443f2d2SAndrzej Pietrasiewicz int ret, i;
3148443f2d2SAndrzej Pietrasiewicz
3158443f2d2SAndrzej Pietrasiewicz if (missing_funcs)
3168443f2d2SAndrzej Pietrasiewicz return -ENODEV;
3178443f2d2SAndrzej Pietrasiewicz #if defined CONFIG_USB_FUNCTIONFS_ETH
3188443f2d2SAndrzej Pietrasiewicz if (can_support_ecm(cdev->gadget)) {
3198443f2d2SAndrzej Pietrasiewicz struct f_ecm_opts *ecm_opts;
3208443f2d2SAndrzej Pietrasiewicz
3218443f2d2SAndrzej Pietrasiewicz fi_ecm = usb_get_function_instance("ecm");
3228443f2d2SAndrzej Pietrasiewicz if (IS_ERR(fi_ecm))
3238443f2d2SAndrzej Pietrasiewicz return PTR_ERR(fi_ecm);
3248443f2d2SAndrzej Pietrasiewicz ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
3258443f2d2SAndrzej Pietrasiewicz net = ecm_opts->net;
3268443f2d2SAndrzej Pietrasiewicz } else {
3278443f2d2SAndrzej Pietrasiewicz struct f_gether_opts *geth_opts;
3288443f2d2SAndrzej Pietrasiewicz
3298443f2d2SAndrzej Pietrasiewicz fi_geth = usb_get_function_instance("geth");
3308443f2d2SAndrzej Pietrasiewicz if (IS_ERR(fi_geth))
3318443f2d2SAndrzej Pietrasiewicz return PTR_ERR(fi_geth);
3328443f2d2SAndrzej Pietrasiewicz geth_opts = container_of(fi_geth, struct f_gether_opts,
3338443f2d2SAndrzej Pietrasiewicz func_inst);
3348443f2d2SAndrzej Pietrasiewicz net = geth_opts->net;
3358443f2d2SAndrzej Pietrasiewicz }
3368443f2d2SAndrzej Pietrasiewicz #endif
3378443f2d2SAndrzej Pietrasiewicz
3388443f2d2SAndrzej Pietrasiewicz #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
3398443f2d2SAndrzej Pietrasiewicz {
3408443f2d2SAndrzej Pietrasiewicz fi_rndis = usb_get_function_instance("rndis");
3418443f2d2SAndrzej Pietrasiewicz if (IS_ERR(fi_rndis)) {
3428443f2d2SAndrzej Pietrasiewicz ret = PTR_ERR(fi_rndis);
3438443f2d2SAndrzej Pietrasiewicz goto error;
3448443f2d2SAndrzej Pietrasiewicz }
3458443f2d2SAndrzej Pietrasiewicz #ifndef CONFIG_USB_FUNCTIONFS_ETH
346872ce511SMichal Nazarewicz net = container_of(fi_rndis, struct f_rndis_opts,
347872ce511SMichal Nazarewicz func_inst)->net;
3488443f2d2SAndrzej Pietrasiewicz #endif
3498443f2d2SAndrzej Pietrasiewicz }
3508443f2d2SAndrzej Pietrasiewicz #endif
3518443f2d2SAndrzej Pietrasiewicz
3528443f2d2SAndrzej Pietrasiewicz #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
3538443f2d2SAndrzej Pietrasiewicz gether_set_qmult(net, qmult);
3548443f2d2SAndrzej Pietrasiewicz if (!gether_set_host_addr(net, host_addr))
3558443f2d2SAndrzej Pietrasiewicz pr_info("using host ethernet address: %s", host_addr);
3568443f2d2SAndrzej Pietrasiewicz if (!gether_set_dev_addr(net, dev_addr))
3578443f2d2SAndrzej Pietrasiewicz pr_info("using self ethernet address: %s", dev_addr);
3588443f2d2SAndrzej Pietrasiewicz #endif
3598443f2d2SAndrzej Pietrasiewicz
3608443f2d2SAndrzej Pietrasiewicz #if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
3618443f2d2SAndrzej Pietrasiewicz gether_set_gadget(net, cdev->gadget);
3628443f2d2SAndrzej Pietrasiewicz ret = gether_register_netdev(net);
3638443f2d2SAndrzej Pietrasiewicz if (ret)
3648443f2d2SAndrzej Pietrasiewicz goto error_rndis;
3658443f2d2SAndrzej Pietrasiewicz
3668443f2d2SAndrzej Pietrasiewicz if (can_support_ecm(cdev->gadget)) {
3678443f2d2SAndrzej Pietrasiewicz struct f_ecm_opts *ecm_opts;
3688443f2d2SAndrzej Pietrasiewicz
3698443f2d2SAndrzej Pietrasiewicz ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
3708443f2d2SAndrzej Pietrasiewicz ecm_opts->bound = true;
3718443f2d2SAndrzej Pietrasiewicz } else {
3728443f2d2SAndrzej Pietrasiewicz struct f_gether_opts *geth_opts;
3738443f2d2SAndrzej Pietrasiewicz
3748443f2d2SAndrzej Pietrasiewicz geth_opts = container_of(fi_geth, struct f_gether_opts,
3758443f2d2SAndrzej Pietrasiewicz func_inst);
3768443f2d2SAndrzej Pietrasiewicz geth_opts->bound = true;
3778443f2d2SAndrzej Pietrasiewicz }
3788443f2d2SAndrzej Pietrasiewicz
3798443f2d2SAndrzej Pietrasiewicz rndis_borrow_net(fi_rndis, net);
3808443f2d2SAndrzej Pietrasiewicz #endif
3818443f2d2SAndrzej Pietrasiewicz
3828443f2d2SAndrzej Pietrasiewicz /* TODO: gstrings_attach? */
3838443f2d2SAndrzej Pietrasiewicz ret = usb_string_ids_tab(cdev, gfs_strings);
3848443f2d2SAndrzej Pietrasiewicz if (unlikely(ret < 0))
3858443f2d2SAndrzej Pietrasiewicz goto error_rndis;
3868443f2d2SAndrzej Pietrasiewicz gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
3878443f2d2SAndrzej Pietrasiewicz
38875c9310aSLi Jun if (gadget_is_otg(cdev->gadget) && !gfs_otg_desc[0]) {
38975c9310aSLi Jun struct usb_descriptor_header *usb_desc;
39075c9310aSLi Jun
39175c9310aSLi Jun usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
39275c9310aSLi Jun if (!usb_desc) {
39375c9310aSLi Jun ret = -ENOMEM;
39475c9310aSLi Jun goto error_rndis;
39575c9310aSLi Jun }
39675c9310aSLi Jun usb_otg_descriptor_init(cdev->gadget, usb_desc);
39775c9310aSLi Jun gfs_otg_desc[0] = usb_desc;
39875c9310aSLi Jun gfs_otg_desc[1] = NULL;
3998443f2d2SAndrzej Pietrasiewicz }
4008443f2d2SAndrzej Pietrasiewicz
4018443f2d2SAndrzej Pietrasiewicz for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
4028443f2d2SAndrzej Pietrasiewicz struct gfs_configuration *c = gfs_configurations + i;
4038443f2d2SAndrzej Pietrasiewicz int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
4048443f2d2SAndrzej Pietrasiewicz
4058443f2d2SAndrzej Pietrasiewicz c->c.label = gfs_strings[sid].s;
4068443f2d2SAndrzej Pietrasiewicz c->c.iConfiguration = gfs_strings[sid].id;
4078443f2d2SAndrzej Pietrasiewicz c->c.bConfigurationValue = 1 + i;
4088443f2d2SAndrzej Pietrasiewicz c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
4098443f2d2SAndrzej Pietrasiewicz
4108443f2d2SAndrzej Pietrasiewicz c->num = i;
4118443f2d2SAndrzej Pietrasiewicz
4128443f2d2SAndrzej Pietrasiewicz ret = usb_add_config(cdev, &c->c, gfs_do_config);
4138443f2d2SAndrzej Pietrasiewicz if (unlikely(ret < 0))
4148443f2d2SAndrzej Pietrasiewicz goto error_unbind;
4158443f2d2SAndrzej Pietrasiewicz }
4168443f2d2SAndrzej Pietrasiewicz usb_composite_overwrite_options(cdev, &coverwrite);
4178443f2d2SAndrzej Pietrasiewicz return 0;
4188443f2d2SAndrzej Pietrasiewicz
41975c9310aSLi Jun /* TODO */
42075c9310aSLi Jun error_unbind:
4218443f2d2SAndrzej Pietrasiewicz kfree(gfs_otg_desc[0]);
4228443f2d2SAndrzej Pietrasiewicz gfs_otg_desc[0] = NULL;
4238443f2d2SAndrzej Pietrasiewicz error_rndis:
4248443f2d2SAndrzej Pietrasiewicz #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
4258443f2d2SAndrzej Pietrasiewicz usb_put_function_instance(fi_rndis);
4268443f2d2SAndrzej Pietrasiewicz error:
4278443f2d2SAndrzej Pietrasiewicz #endif
4288443f2d2SAndrzej Pietrasiewicz #if defined CONFIG_USB_FUNCTIONFS_ETH
4298443f2d2SAndrzej Pietrasiewicz if (can_support_ecm(cdev->gadget))
4308443f2d2SAndrzej Pietrasiewicz usb_put_function_instance(fi_ecm);
4318443f2d2SAndrzej Pietrasiewicz else
4328443f2d2SAndrzej Pietrasiewicz usb_put_function_instance(fi_geth);
4338443f2d2SAndrzej Pietrasiewicz #endif
4348443f2d2SAndrzej Pietrasiewicz return ret;
4358443f2d2SAndrzej Pietrasiewicz }
4368443f2d2SAndrzej Pietrasiewicz
4378443f2d2SAndrzej Pietrasiewicz /*
4388443f2d2SAndrzej Pietrasiewicz * It is assumed that gfs_unbind is called from a context where ffs_lock is held
4398443f2d2SAndrzej Pietrasiewicz */
gfs_unbind(struct usb_composite_dev * cdev)4408443f2d2SAndrzej Pietrasiewicz static int gfs_unbind(struct usb_composite_dev *cdev)
4418443f2d2SAndrzej Pietrasiewicz {
4428443f2d2SAndrzej Pietrasiewicz int i;
4438443f2d2SAndrzej Pietrasiewicz
4448443f2d2SAndrzej Pietrasiewicz #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
4458443f2d2SAndrzej Pietrasiewicz usb_put_function(f_rndis);
4468443f2d2SAndrzej Pietrasiewicz usb_put_function_instance(fi_rndis);
4478443f2d2SAndrzej Pietrasiewicz #endif
4488443f2d2SAndrzej Pietrasiewicz
4498443f2d2SAndrzej Pietrasiewicz #if defined CONFIG_USB_FUNCTIONFS_ETH
4508443f2d2SAndrzej Pietrasiewicz if (can_support_ecm(cdev->gadget)) {
4518443f2d2SAndrzej Pietrasiewicz usb_put_function(f_ecm);
4528443f2d2SAndrzej Pietrasiewicz usb_put_function_instance(fi_ecm);
4538443f2d2SAndrzej Pietrasiewicz } else {
4548443f2d2SAndrzej Pietrasiewicz usb_put_function(f_geth);
4558443f2d2SAndrzej Pietrasiewicz usb_put_function_instance(fi_geth);
4568443f2d2SAndrzej Pietrasiewicz }
4578443f2d2SAndrzej Pietrasiewicz #endif
4588443f2d2SAndrzej Pietrasiewicz for (i = 0; i < N_CONF * func_num; ++i)
45975c9310aSLi Jun usb_put_function(*(f_ffs[0] + i));
46075c9310aSLi Jun
46175c9310aSLi Jun kfree(gfs_otg_desc[0]);
4628443f2d2SAndrzej Pietrasiewicz gfs_otg_desc[0] = NULL;
4638443f2d2SAndrzej Pietrasiewicz
4648443f2d2SAndrzej Pietrasiewicz return 0;
4658443f2d2SAndrzej Pietrasiewicz }
4668443f2d2SAndrzej Pietrasiewicz
4678443f2d2SAndrzej Pietrasiewicz /*
4688443f2d2SAndrzej Pietrasiewicz * It is assumed that gfs_do_config is called from a context where
4698443f2d2SAndrzej Pietrasiewicz * ffs_lock is held
4708443f2d2SAndrzej Pietrasiewicz */
gfs_do_config(struct usb_configuration * c)4718443f2d2SAndrzej Pietrasiewicz static int gfs_do_config(struct usb_configuration *c)
4728443f2d2SAndrzej Pietrasiewicz {
4738443f2d2SAndrzej Pietrasiewicz struct gfs_configuration *gc =
4748443f2d2SAndrzej Pietrasiewicz container_of(c, struct gfs_configuration, c);
4758443f2d2SAndrzej Pietrasiewicz int i;
4768443f2d2SAndrzej Pietrasiewicz int ret;
4778443f2d2SAndrzej Pietrasiewicz
4788443f2d2SAndrzej Pietrasiewicz if (missing_funcs)
4798443f2d2SAndrzej Pietrasiewicz return -ENODEV;
4808443f2d2SAndrzej Pietrasiewicz
4818443f2d2SAndrzej Pietrasiewicz if (gadget_is_otg(c->cdev->gadget)) {
4828443f2d2SAndrzej Pietrasiewicz c->descriptors = gfs_otg_desc;
4838443f2d2SAndrzej Pietrasiewicz c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
4848443f2d2SAndrzej Pietrasiewicz }
4858443f2d2SAndrzej Pietrasiewicz
4868443f2d2SAndrzej Pietrasiewicz if (gc->eth) {
4878443f2d2SAndrzej Pietrasiewicz ret = gc->eth(c);
4888443f2d2SAndrzej Pietrasiewicz if (unlikely(ret < 0))
4898443f2d2SAndrzej Pietrasiewicz return ret;
4908443f2d2SAndrzej Pietrasiewicz }
4918443f2d2SAndrzej Pietrasiewicz
4928443f2d2SAndrzej Pietrasiewicz for (i = 0; i < func_num; i++) {
4938443f2d2SAndrzej Pietrasiewicz f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]);
4948443f2d2SAndrzej Pietrasiewicz if (IS_ERR(f_ffs[gc->num][i])) {
4958443f2d2SAndrzej Pietrasiewicz ret = PTR_ERR(f_ffs[gc->num][i]);
4968443f2d2SAndrzej Pietrasiewicz goto error;
4978443f2d2SAndrzej Pietrasiewicz }
4988443f2d2SAndrzej Pietrasiewicz ret = usb_add_function(c, f_ffs[gc->num][i]);
4998443f2d2SAndrzej Pietrasiewicz if (ret < 0) {
5008443f2d2SAndrzej Pietrasiewicz usb_put_function(f_ffs[gc->num][i]);
5018443f2d2SAndrzej Pietrasiewicz goto error;
5028443f2d2SAndrzej Pietrasiewicz }
5038443f2d2SAndrzej Pietrasiewicz }
5048443f2d2SAndrzej Pietrasiewicz
5058443f2d2SAndrzej Pietrasiewicz /*
5068443f2d2SAndrzej Pietrasiewicz * After previous do_configs there may be some invalid
5078443f2d2SAndrzej Pietrasiewicz * pointers in c->interface array. This happens every time
5088443f2d2SAndrzej Pietrasiewicz * a user space function with fewer interfaces than a user
5098443f2d2SAndrzej Pietrasiewicz * space function that was run before the new one is run. The
5108443f2d2SAndrzej Pietrasiewicz * compasit's set_config() assumes that if there is no more
5118443f2d2SAndrzej Pietrasiewicz * then MAX_CONFIG_INTERFACES interfaces in a configuration
5128443f2d2SAndrzej Pietrasiewicz * then there is a NULL pointer after the last interface in
5138443f2d2SAndrzej Pietrasiewicz * c->interface array. We need to make sure this is true.
5148443f2d2SAndrzej Pietrasiewicz */
5158443f2d2SAndrzej Pietrasiewicz if (c->next_interface_id < ARRAY_SIZE(c->interface))
5168443f2d2SAndrzej Pietrasiewicz c->interface[c->next_interface_id] = NULL;
5178443f2d2SAndrzej Pietrasiewicz
5188443f2d2SAndrzej Pietrasiewicz return 0;
5198443f2d2SAndrzej Pietrasiewicz error:
5208443f2d2SAndrzej Pietrasiewicz while (--i >= 0) {
5218443f2d2SAndrzej Pietrasiewicz if (!IS_ERR(f_ffs[gc->num][i]))
5228443f2d2SAndrzej Pietrasiewicz usb_remove_function(c, f_ffs[gc->num][i]);
5238443f2d2SAndrzej Pietrasiewicz usb_put_function(f_ffs[gc->num][i]);
5248443f2d2SAndrzej Pietrasiewicz }
5258443f2d2SAndrzej Pietrasiewicz return ret;
5268443f2d2SAndrzej Pietrasiewicz }
5278443f2d2SAndrzej Pietrasiewicz
5288443f2d2SAndrzej Pietrasiewicz #ifdef CONFIG_USB_FUNCTIONFS_ETH
5298443f2d2SAndrzej Pietrasiewicz
eth_bind_config(struct usb_configuration * c)5308443f2d2SAndrzej Pietrasiewicz static int eth_bind_config(struct usb_configuration *c)
5318443f2d2SAndrzej Pietrasiewicz {
5328443f2d2SAndrzej Pietrasiewicz int status = 0;
5338443f2d2SAndrzej Pietrasiewicz
5348443f2d2SAndrzej Pietrasiewicz if (can_support_ecm(c->cdev->gadget)) {
5358443f2d2SAndrzej Pietrasiewicz f_ecm = usb_get_function(fi_ecm);
5368443f2d2SAndrzej Pietrasiewicz if (IS_ERR(f_ecm))
5378443f2d2SAndrzej Pietrasiewicz return PTR_ERR(f_ecm);
5388443f2d2SAndrzej Pietrasiewicz
5398443f2d2SAndrzej Pietrasiewicz status = usb_add_function(c, f_ecm);
5408443f2d2SAndrzej Pietrasiewicz if (status < 0)
5418443f2d2SAndrzej Pietrasiewicz usb_put_function(f_ecm);
5428443f2d2SAndrzej Pietrasiewicz
5438443f2d2SAndrzej Pietrasiewicz } else {
5448443f2d2SAndrzej Pietrasiewicz f_geth = usb_get_function(fi_geth);
5458443f2d2SAndrzej Pietrasiewicz if (IS_ERR(f_geth))
5468443f2d2SAndrzej Pietrasiewicz return PTR_ERR(f_geth);
5478443f2d2SAndrzej Pietrasiewicz
5488443f2d2SAndrzej Pietrasiewicz status = usb_add_function(c, f_geth);
5498443f2d2SAndrzej Pietrasiewicz if (status < 0)
5508443f2d2SAndrzej Pietrasiewicz usb_put_function(f_geth);
5518443f2d2SAndrzej Pietrasiewicz }
5528443f2d2SAndrzej Pietrasiewicz return status;
5538443f2d2SAndrzej Pietrasiewicz }
5548443f2d2SAndrzej Pietrasiewicz
5558443f2d2SAndrzej Pietrasiewicz #endif
5568443f2d2SAndrzej Pietrasiewicz
5578443f2d2SAndrzej Pietrasiewicz #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
5588443f2d2SAndrzej Pietrasiewicz
bind_rndis_config(struct usb_configuration * c)5598443f2d2SAndrzej Pietrasiewicz static int bind_rndis_config(struct usb_configuration *c)
5608443f2d2SAndrzej Pietrasiewicz {
5618443f2d2SAndrzej Pietrasiewicz int status = 0;
5628443f2d2SAndrzej Pietrasiewicz
5638443f2d2SAndrzej Pietrasiewicz f_rndis = usb_get_function(fi_rndis);
5648443f2d2SAndrzej Pietrasiewicz if (IS_ERR(f_rndis))
5658443f2d2SAndrzej Pietrasiewicz return PTR_ERR(f_rndis);
5668443f2d2SAndrzej Pietrasiewicz
5678443f2d2SAndrzej Pietrasiewicz status = usb_add_function(c, f_rndis);
5688443f2d2SAndrzej Pietrasiewicz if (status < 0)
5698443f2d2SAndrzej Pietrasiewicz usb_put_function(f_rndis);
5708443f2d2SAndrzej Pietrasiewicz
5718443f2d2SAndrzej Pietrasiewicz return status;
5728443f2d2SAndrzej Pietrasiewicz }
573
574 #endif
575