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