Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 1 | /* |
| 2 | * g_ffs.c -- user mode file system API for USB composite function controllers |
| 3 | * |
| 4 | * Copyright (C) 2010 Samsung Electronics |
Michal Nazarewicz | 54b8360 | 2012-01-13 15:05:16 +0100 | [diff] [blame] | 5 | * Author: Michal Nazarewicz <mina86@mina86.com> |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 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. |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 11 | */ |
| 12 | |
Michal Nazarewicz | aa02f17 | 2010-11-17 17:09:47 +0100 | [diff] [blame] | 13 | #define pr_fmt(fmt) "g_ffs: " fmt |
| 14 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 15 | #include <linux/module.h> |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 16 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 17 | #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 18 | #include <linux/netdevice.h> |
| 19 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 20 | # if defined USB_ETH_RNDIS |
| 21 | # undef USB_ETH_RNDIS |
| 22 | # endif |
| 23 | # ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
| 24 | # define USB_ETH_RNDIS y |
| 25 | # endif |
| 26 | |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 27 | # include "u_ecm.h" |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 28 | # include "u_gether.h" |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 29 | # ifdef USB_ETH_RNDIS |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 30 | # include "u_rndis.h" |
Andrzej Pietrasiewicz | cbbd14a | 2013-05-24 10:23:02 +0200 | [diff] [blame] | 31 | # include "rndis.h" |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 32 | # endif |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 33 | # include "u_ether.h" |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 34 | |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 35 | USB_ETHERNET_MODULE_PARAMETERS(); |
| 36 | |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 37 | # ifdef CONFIG_USB_FUNCTIONFS_ETH |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 38 | static int eth_bind_config(struct usb_configuration *c); |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 39 | static struct usb_function_instance *fi_ecm; |
| 40 | static struct usb_function *f_ecm; |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 41 | static struct usb_function_instance *fi_geth; |
| 42 | static struct usb_function *f_geth; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 43 | # endif |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 44 | # ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
| 45 | static int bind_rndis_config(struct usb_configuration *c); |
| 46 | static struct usb_function_instance *fi_rndis; |
| 47 | static struct usb_function *f_rndis; |
| 48 | # endif |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 49 | #endif |
| 50 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 51 | #include "u_fs.h" |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 52 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 53 | #define DRIVER_NAME "g_ffs" |
| 54 | #define DRIVER_DESC "USB Function Filesystem" |
| 55 | #define DRIVER_VERSION "24 Aug 2004" |
| 56 | |
| 57 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 58 | MODULE_AUTHOR("Michal Nazarewicz"); |
| 59 | MODULE_LICENSE("GPL"); |
| 60 | |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 61 | #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */ |
| 62 | #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 63 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 64 | #define GFS_MAX_DEVS 10 |
| 65 | |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 66 | USB_GADGET_COMPOSITE_OPTIONS(); |
| 67 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 68 | static struct usb_device_descriptor gfs_dev_desc = { |
| 69 | .bLength = sizeof gfs_dev_desc, |
| 70 | .bDescriptorType = USB_DT_DEVICE, |
| 71 | |
Igor Kotrasinski | 0aecfc1 | 2015-10-20 18:33:13 +0200 | [diff] [blame] | 72 | /* .bcdUSB = DYNAMIC */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 73 | .bDeviceClass = USB_CLASS_PER_INTERFACE, |
| 74 | |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 75 | .idVendor = cpu_to_le16(GFS_VENDOR_ID), |
| 76 | .idProduct = cpu_to_le16(GFS_PRODUCT_ID), |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 79 | static char *func_names[GFS_MAX_DEVS]; |
| 80 | static unsigned int func_num; |
| 81 | |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 82 | module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644); |
| 83 | MODULE_PARM_DESC(bDeviceClass, "USB Device class"); |
| 84 | module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644); |
| 85 | MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass"); |
| 86 | module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644); |
| 87 | MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol"); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 88 | module_param_array_named(functions, func_names, charp, &func_num, 0); |
| 89 | MODULE_PARM_DESC(functions, "USB Functions list"); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 90 | |
Li Jun | 75c9310 | 2015-07-09 15:18:53 +0800 | [diff] [blame] | 91 | static const struct usb_descriptor_header *gfs_otg_desc[2]; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 92 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 93 | /* String IDs are assigned dynamically */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 94 | static struct usb_string gfs_strings[] = { |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 95 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | d33f74f | 2012-09-10 15:01:57 +0200 | [diff] [blame] | 96 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 97 | [USB_GADGET_SERIAL_IDX].s = "", |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 98 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 99 | { .s = "FunctionFS + RNDIS" }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 100 | #endif |
| 101 | #ifdef CONFIG_USB_FUNCTIONFS_ETH |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 102 | { .s = "FunctionFS + ECM" }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 103 | #endif |
| 104 | #ifdef CONFIG_USB_FUNCTIONFS_GENERIC |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 105 | { .s = "FunctionFS" }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 106 | #endif |
| 107 | { } /* end of list */ |
| 108 | }; |
| 109 | |
| 110 | static struct usb_gadget_strings *gfs_dev_strings[] = { |
| 111 | &(struct usb_gadget_strings) { |
| 112 | .language = 0x0409, /* en-us */ |
| 113 | .strings = gfs_strings, |
| 114 | }, |
| 115 | NULL, |
| 116 | }; |
| 117 | |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 118 | struct gfs_configuration { |
| 119 | struct usb_configuration c; |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 120 | int (*eth)(struct usb_configuration *c); |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 121 | int num; |
Lad, Prabhakar | 2b87cd2 | 2015-02-05 13:11:47 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | static struct gfs_configuration gfs_configurations[] = { |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 125 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 126 | { |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 127 | .eth = bind_rndis_config, |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 128 | }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 129 | #endif |
| 130 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 131 | #ifdef CONFIG_USB_FUNCTIONFS_ETH |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 132 | { |
| 133 | .eth = eth_bind_config, |
| 134 | }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 135 | #endif |
| 136 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 137 | #ifdef CONFIG_USB_FUNCTIONFS_GENERIC |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 138 | { |
| 139 | }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 140 | #endif |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 141 | }; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 142 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 143 | static void *functionfs_acquire_dev(struct ffs_dev *dev); |
| 144 | static void functionfs_release_dev(struct ffs_dev *dev); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 145 | static int functionfs_ready_callback(struct ffs_data *ffs); |
| 146 | static void functionfs_closed_callback(struct ffs_data *ffs); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 147 | static int gfs_bind(struct usb_composite_dev *cdev); |
| 148 | static int gfs_unbind(struct usb_composite_dev *cdev); |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 149 | static int gfs_do_config(struct usb_configuration *c); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 150 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 151 | |
Arnd Bergmann | c94e289 | 2015-04-11 00:14:21 +0200 | [diff] [blame] | 152 | static struct usb_composite_driver gfs_driver = { |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 153 | .name = DRIVER_NAME, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 154 | .dev = &gfs_dev_desc, |
| 155 | .strings = gfs_dev_strings, |
Tatyana Brokhman | 35a0e0b | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 156 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 157 | .bind = gfs_bind, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 158 | .unbind = gfs_unbind, |
| 159 | }; |
| 160 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 161 | static unsigned int missing_funcs; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 162 | static bool gfs_registered; |
| 163 | static bool gfs_single_func; |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 164 | static struct usb_function_instance **fi_ffs; |
| 165 | static struct usb_function **f_ffs[] = { |
| 166 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
| 167 | NULL, |
| 168 | #endif |
| 169 | |
| 170 | #ifdef CONFIG_USB_FUNCTIONFS_ETH |
| 171 | NULL, |
| 172 | #endif |
| 173 | |
| 174 | #ifdef CONFIG_USB_FUNCTIONFS_GENERIC |
| 175 | NULL, |
| 176 | #endif |
| 177 | }; |
| 178 | |
| 179 | #define N_CONF ARRAY_SIZE(f_ffs) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 180 | |
Andrzej Pietrasiewicz | 8545e60 | 2012-03-12 12:55:42 +0100 | [diff] [blame] | 181 | static int __init gfs_init(void) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 182 | { |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 183 | struct f_fs_opts *opts; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 184 | int i; |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 185 | int ret = 0; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 186 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 187 | ENTER(); |
| 188 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 189 | if (func_num < 2) { |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 190 | gfs_single_func = true; |
| 191 | func_num = 1; |
| 192 | } |
| 193 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 194 | /* |
| 195 | * Allocate in one chunk for easier maintenance |
| 196 | */ |
| 197 | f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL); |
| 198 | if (!f_ffs[0]) { |
| 199 | ret = -ENOMEM; |
| 200 | goto no_func; |
| 201 | } |
| 202 | for (i = 1; i < N_CONF; ++i) |
| 203 | f_ffs[i] = f_ffs[0] + i * func_num; |
| 204 | |
| 205 | fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL); |
| 206 | if (!fi_ffs) { |
| 207 | ret = -ENOMEM; |
| 208 | goto no_func; |
| 209 | } |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 210 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 211 | for (i = 0; i < func_num; i++) { |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 212 | fi_ffs[i] = usb_get_function_instance("ffs"); |
| 213 | if (IS_ERR(fi_ffs[i])) { |
| 214 | ret = PTR_ERR(fi_ffs[i]); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 215 | --i; |
| 216 | goto no_dev; |
| 217 | } |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 218 | opts = to_f_fs_opts(fi_ffs[i]); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 219 | if (gfs_single_func) |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 220 | ret = ffs_single_dev(opts->dev); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 221 | else |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 222 | ret = ffs_name_dev(opts->dev, func_names[i]); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 223 | if (ret) |
| 224 | goto no_dev; |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 225 | opts->dev->ffs_ready_callback = functionfs_ready_callback; |
| 226 | opts->dev->ffs_closed_callback = functionfs_closed_callback; |
| 227 | opts->dev->ffs_acquire_dev_callback = functionfs_acquire_dev; |
| 228 | opts->dev->ffs_release_dev_callback = functionfs_release_dev; |
| 229 | opts->no_configfs = true; |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 230 | } |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 231 | |
| 232 | missing_funcs = func_num; |
| 233 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 234 | return 0; |
| 235 | no_dev: |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 236 | while (i >= 0) |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 237 | usb_put_function_instance(fi_ffs[i--]); |
| 238 | kfree(fi_ffs); |
| 239 | no_func: |
| 240 | kfree(f_ffs[0]); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 241 | return ret; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 242 | } |
| 243 | module_init(gfs_init); |
| 244 | |
Andrzej Pietrasiewicz | 8545e60 | 2012-03-12 12:55:42 +0100 | [diff] [blame] | 245 | static void __exit gfs_exit(void) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 246 | { |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 247 | int i; |
| 248 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 249 | ENTER(); |
| 250 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 251 | if (gfs_registered) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 252 | usb_composite_unregister(&gfs_driver); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 253 | gfs_registered = false; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 254 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 255 | kfree(f_ffs[0]); |
| 256 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 257 | for (i = 0; i < func_num; i++) |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 258 | usb_put_function_instance(fi_ffs[i]); |
| 259 | |
| 260 | kfree(fi_ffs); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 261 | } |
| 262 | module_exit(gfs_exit); |
| 263 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 264 | static void *functionfs_acquire_dev(struct ffs_dev *dev) |
| 265 | { |
| 266 | if (!try_module_get(THIS_MODULE)) |
Krzysztof Opasiak | d668b4f | 2014-05-21 14:05:35 +0200 | [diff] [blame] | 267 | return ERR_PTR(-ENOENT); |
Michal Nazarewicz | 872ce51 | 2016-05-31 14:17:21 +0200 | [diff] [blame] | 268 | |
Lad, Prabhakar | 2b87cd2 | 2015-02-05 13:11:47 +0000 | [diff] [blame] | 269 | return NULL; |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static void functionfs_release_dev(struct ffs_dev *dev) |
| 273 | { |
| 274 | module_put(THIS_MODULE); |
| 275 | } |
| 276 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 277 | /* |
Michal Nazarewicz | 872ce51 | 2016-05-31 14:17:21 +0200 | [diff] [blame] | 278 | * The caller of this function takes ffs_lock |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 279 | */ |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 280 | static int functionfs_ready_callback(struct ffs_data *ffs) |
| 281 | { |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 282 | int ret = 0; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 283 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 284 | if (--missing_funcs) |
| 285 | return 0; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 286 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 287 | if (gfs_registered) |
| 288 | return -EBUSY; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 289 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 290 | gfs_registered = true; |
| 291 | |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 292 | ret = usb_composite_probe(&gfs_driver); |
Krzysztof Opasiak | c41b33c | 2015-05-22 17:25:17 +0200 | [diff] [blame] | 293 | if (unlikely(ret < 0)) { |
| 294 | ++missing_funcs; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 295 | gfs_registered = false; |
Krzysztof Opasiak | c41b33c | 2015-05-22 17:25:17 +0200 | [diff] [blame] | 296 | } |
Michal Nazarewicz | 872ce51 | 2016-05-31 14:17:21 +0200 | [diff] [blame] | 297 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 298 | return ret; |
| 299 | } |
| 300 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 301 | /* |
Michal Nazarewicz | 872ce51 | 2016-05-31 14:17:21 +0200 | [diff] [blame] | 302 | * The caller of this function takes ffs_lock |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 303 | */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 304 | static void functionfs_closed_callback(struct ffs_data *ffs) |
| 305 | { |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 306 | missing_funcs++; |
| 307 | |
| 308 | if (gfs_registered) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 309 | usb_composite_unregister(&gfs_driver); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 310 | gfs_registered = false; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /* |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 314 | * It is assumed that gfs_bind is called from a context where ffs_lock is held |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 315 | */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 316 | static int gfs_bind(struct usb_composite_dev *cdev) |
| 317 | { |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 318 | #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 319 | struct net_device *net; |
| 320 | #endif |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 321 | int ret, i; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 322 | |
| 323 | ENTER(); |
| 324 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 325 | if (missing_funcs) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 326 | return -ENODEV; |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 327 | #if defined CONFIG_USB_FUNCTIONFS_ETH |
| 328 | if (can_support_ecm(cdev->gadget)) { |
| 329 | struct f_ecm_opts *ecm_opts; |
| 330 | |
| 331 | fi_ecm = usb_get_function_instance("ecm"); |
| 332 | if (IS_ERR(fi_ecm)) |
| 333 | return PTR_ERR(fi_ecm); |
| 334 | ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst); |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 335 | net = ecm_opts->net; |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 336 | } else { |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 337 | struct f_gether_opts *geth_opts; |
| 338 | |
| 339 | fi_geth = usb_get_function_instance("geth"); |
| 340 | if (IS_ERR(fi_geth)) |
| 341 | return PTR_ERR(fi_geth); |
| 342 | geth_opts = container_of(fi_geth, struct f_gether_opts, |
| 343 | func_inst); |
| 344 | net = geth_opts->net; |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 345 | } |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 346 | #endif |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 347 | |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 348 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
| 349 | { |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 350 | fi_rndis = usb_get_function_instance("rndis"); |
| 351 | if (IS_ERR(fi_rndis)) { |
| 352 | ret = PTR_ERR(fi_rndis); |
| 353 | goto error; |
| 354 | } |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 355 | #ifndef CONFIG_USB_FUNCTIONFS_ETH |
Michal Nazarewicz | 872ce51 | 2016-05-31 14:17:21 +0200 | [diff] [blame] | 356 | net = container_of(fi_rndis, struct f_rndis_opts, |
| 357 | func_inst)->net; |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 358 | #endif |
| 359 | } |
| 360 | #endif |
| 361 | |
| 362 | #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS |
| 363 | gether_set_qmult(net, qmult); |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 364 | if (!gether_set_host_addr(net, host_addr)) |
| 365 | pr_info("using host ethernet address: %s", host_addr); |
| 366 | if (!gether_set_dev_addr(net, dev_addr)) |
| 367 | pr_info("using self ethernet address: %s", dev_addr); |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 368 | #endif |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 369 | |
| 370 | #if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 371 | gether_set_gadget(net, cdev->gadget); |
| 372 | ret = gether_register_netdev(net); |
| 373 | if (ret) |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 374 | goto error_rndis; |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 375 | |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 376 | if (can_support_ecm(cdev->gadget)) { |
| 377 | struct f_ecm_opts *ecm_opts; |
| 378 | |
| 379 | ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst); |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 380 | ecm_opts->bound = true; |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 381 | } else { |
| 382 | struct f_gether_opts *geth_opts; |
| 383 | |
| 384 | geth_opts = container_of(fi_geth, struct f_gether_opts, |
| 385 | func_inst); |
| 386 | geth_opts->bound = true; |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 387 | } |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 388 | |
| 389 | rndis_borrow_net(fi_rndis, net); |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 390 | #endif |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 391 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 392 | /* TODO: gstrings_attach? */ |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 393 | ret = usb_string_ids_tab(cdev, gfs_strings); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 394 | if (unlikely(ret < 0)) |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 395 | goto error_rndis; |
Sebastian Andrzej Siewior | d33f74f | 2012-09-10 15:01:57 +0200 | [diff] [blame] | 396 | gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 397 | |
Li Jun | 75c9310 | 2015-07-09 15:18:53 +0800 | [diff] [blame] | 398 | if (gadget_is_otg(cdev->gadget) && !gfs_otg_desc[0]) { |
| 399 | struct usb_descriptor_header *usb_desc; |
| 400 | |
| 401 | usb_desc = usb_otg_descriptor_alloc(cdev->gadget); |
| 402 | if (!usb_desc) |
| 403 | goto error_rndis; |
| 404 | usb_otg_descriptor_init(cdev->gadget, usb_desc); |
| 405 | gfs_otg_desc[0] = usb_desc; |
| 406 | gfs_otg_desc[1] = NULL; |
| 407 | } |
| 408 | |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 409 | for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) { |
| 410 | struct gfs_configuration *c = gfs_configurations + i; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 411 | int sid = USB_GADGET_FIRST_AVAIL_IDX + i; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 412 | |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 413 | c->c.label = gfs_strings[sid].s; |
| 414 | c->c.iConfiguration = gfs_strings[sid].id; |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 415 | c->c.bConfigurationValue = 1 + i; |
| 416 | c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 417 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 418 | c->num = i; |
| 419 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 420 | ret = usb_add_config(cdev, &c->c, gfs_do_config); |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 421 | if (unlikely(ret < 0)) |
| 422 | goto error_unbind; |
| 423 | } |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 424 | usb_composite_overwrite_options(cdev, &coverwrite); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 425 | return 0; |
| 426 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 427 | /* TODO */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 428 | error_unbind: |
Li Jun | 75c9310 | 2015-07-09 15:18:53 +0800 | [diff] [blame] | 429 | kfree(gfs_otg_desc[0]); |
| 430 | gfs_otg_desc[0] = NULL; |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 431 | error_rndis: |
| 432 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
| 433 | usb_put_function_instance(fi_rndis); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 434 | error: |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 435 | #endif |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 436 | #if defined CONFIG_USB_FUNCTIONFS_ETH |
| 437 | if (can_support_ecm(cdev->gadget)) |
| 438 | usb_put_function_instance(fi_ecm); |
| 439 | else |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 440 | usb_put_function_instance(fi_geth); |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 441 | #endif |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 442 | return ret; |
| 443 | } |
| 444 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 445 | /* |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 446 | * It is assumed that gfs_unbind is called from a context where ffs_lock is held |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 447 | */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 448 | static int gfs_unbind(struct usb_composite_dev *cdev) |
| 449 | { |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 450 | int i; |
| 451 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 452 | ENTER(); |
| 453 | |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 454 | |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 455 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
| 456 | usb_put_function(f_rndis); |
| 457 | usb_put_function_instance(fi_rndis); |
| 458 | #endif |
| 459 | |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 460 | #if defined CONFIG_USB_FUNCTIONFS_ETH |
| 461 | if (can_support_ecm(cdev->gadget)) { |
| 462 | usb_put_function(f_ecm); |
| 463 | usb_put_function_instance(fi_ecm); |
| 464 | } else { |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 465 | usb_put_function(f_geth); |
| 466 | usb_put_function_instance(fi_geth); |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 467 | } |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 468 | #endif |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 469 | for (i = 0; i < N_CONF * func_num; ++i) |
| 470 | usb_put_function(*(f_ffs[0] + i)); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 471 | |
Li Jun | 75c9310 | 2015-07-09 15:18:53 +0800 | [diff] [blame] | 472 | kfree(gfs_otg_desc[0]); |
| 473 | gfs_otg_desc[0] = NULL; |
| 474 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 475 | return 0; |
| 476 | } |
| 477 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 478 | /* |
| 479 | * It is assumed that gfs_do_config is called from a context where |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 480 | * ffs_lock is held |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 481 | */ |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 482 | static int gfs_do_config(struct usb_configuration *c) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 483 | { |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 484 | struct gfs_configuration *gc = |
| 485 | container_of(c, struct gfs_configuration, c); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 486 | int i; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 487 | int ret; |
| 488 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 489 | if (missing_funcs) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 490 | return -ENODEV; |
| 491 | |
| 492 | if (gadget_is_otg(c->cdev->gadget)) { |
| 493 | c->descriptors = gfs_otg_desc; |
| 494 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 495 | } |
| 496 | |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 497 | if (gc->eth) { |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 498 | ret = gc->eth(c); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 499 | if (unlikely(ret < 0)) |
| 500 | return ret; |
| 501 | } |
| 502 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 503 | for (i = 0; i < func_num; i++) { |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 504 | f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]); |
| 505 | if (IS_ERR(f_ffs[gc->num][i])) { |
| 506 | ret = PTR_ERR(f_ffs[gc->num][i]); |
| 507 | goto error; |
| 508 | } |
| 509 | ret = usb_add_function(c, f_ffs[gc->num][i]); |
| 510 | if (ret < 0) { |
| 511 | usb_put_function(f_ffs[gc->num][i]); |
| 512 | goto error; |
| 513 | } |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 514 | } |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 515 | |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 516 | /* |
| 517 | * After previous do_configs there may be some invalid |
Michal Nazarewicz | f588c0d | 2010-06-14 10:43:34 +0200 | [diff] [blame] | 518 | * pointers in c->interface array. This happens every time |
| 519 | * a user space function with fewer interfaces than a user |
| 520 | * space function that was run before the new one is run. The |
| 521 | * compasit's set_config() assumes that if there is no more |
| 522 | * then MAX_CONFIG_INTERFACES interfaces in a configuration |
| 523 | * then there is a NULL pointer after the last interface in |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 524 | * c->interface array. We need to make sure this is true. |
| 525 | */ |
Michal Nazarewicz | f588c0d | 2010-06-14 10:43:34 +0200 | [diff] [blame] | 526 | if (c->next_interface_id < ARRAY_SIZE(c->interface)) |
| 527 | c->interface[c->next_interface_id] = NULL; |
| 528 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 529 | return 0; |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 530 | error: |
| 531 | while (--i >= 0) { |
| 532 | if (!IS_ERR(f_ffs[gc->num][i])) |
| 533 | usb_remove_function(c, f_ffs[gc->num][i]); |
| 534 | usb_put_function(f_ffs[gc->num][i]); |
| 535 | } |
| 536 | return ret; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 537 | } |
| 538 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 539 | #ifdef CONFIG_USB_FUNCTIONFS_ETH |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 540 | |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 541 | static int eth_bind_config(struct usb_configuration *c) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 542 | { |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 543 | int status = 0; |
| 544 | |
| 545 | if (can_support_ecm(c->cdev->gadget)) { |
| 546 | f_ecm = usb_get_function(fi_ecm); |
| 547 | if (IS_ERR(f_ecm)) |
| 548 | return PTR_ERR(f_ecm); |
| 549 | |
| 550 | status = usb_add_function(c, f_ecm); |
| 551 | if (status < 0) |
| 552 | usb_put_function(f_ecm); |
| 553 | |
| 554 | } else { |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 555 | f_geth = usb_get_function(fi_geth); |
| 556 | if (IS_ERR(f_geth)) |
| 557 | return PTR_ERR(f_geth); |
| 558 | |
| 559 | status = usb_add_function(c, f_geth); |
| 560 | if (status < 0) |
| 561 | usb_put_function(f_geth); |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 562 | } |
| 563 | return status; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 564 | } |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 565 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 566 | #endif |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 567 | |
| 568 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
| 569 | |
| 570 | static int bind_rndis_config(struct usb_configuration *c) |
| 571 | { |
| 572 | int status = 0; |
| 573 | |
| 574 | f_rndis = usb_get_function(fi_rndis); |
| 575 | if (IS_ERR(f_rndis)) |
| 576 | return PTR_ERR(f_rndis); |
| 577 | |
| 578 | status = usb_add_function(c, f_rndis); |
| 579 | if (status < 0) |
| 580 | usb_put_function(f_rndis); |
| 581 | |
| 582 | return status; |
| 583 | } |
| 584 | |
| 585 | #endif |