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 | |
| 72 | .bcdUSB = cpu_to_le16(0x0200), |
| 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 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 91 | static const struct usb_descriptor_header *gfs_otg_desc[] = { |
| 92 | (const struct usb_descriptor_header *) |
| 93 | &(const struct usb_otg_descriptor) { |
| 94 | .bLength = sizeof(struct usb_otg_descriptor), |
| 95 | .bDescriptorType = USB_DT_OTG, |
| 96 | |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 97 | /* |
| 98 | * REVISIT SRP-only hardware is possible, although |
| 99 | * it would not be called "OTG" ... |
| 100 | */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 101 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, |
| 102 | }, |
| 103 | |
| 104 | NULL |
| 105 | }; |
| 106 | |
Michal Nazarewicz | 5ab54cf | 2010-11-12 14:29:28 +0100 | [diff] [blame] | 107 | /* String IDs are assigned dynamically */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 108 | static struct usb_string gfs_strings[] = { |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 109 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
Sebastian Andrzej Siewior | d33f74f | 2012-09-10 15:01:57 +0200 | [diff] [blame] | 110 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 111 | [USB_GADGET_SERIAL_IDX].s = "", |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 112 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 113 | { .s = "FunctionFS + RNDIS" }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 114 | #endif |
| 115 | #ifdef CONFIG_USB_FUNCTIONFS_ETH |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 116 | { .s = "FunctionFS + ECM" }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 117 | #endif |
| 118 | #ifdef CONFIG_USB_FUNCTIONFS_GENERIC |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 119 | { .s = "FunctionFS" }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 120 | #endif |
| 121 | { } /* end of list */ |
| 122 | }; |
| 123 | |
| 124 | static struct usb_gadget_strings *gfs_dev_strings[] = { |
| 125 | &(struct usb_gadget_strings) { |
| 126 | .language = 0x0409, /* en-us */ |
| 127 | .strings = gfs_strings, |
| 128 | }, |
| 129 | NULL, |
| 130 | }; |
| 131 | |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 132 | struct gfs_configuration { |
| 133 | struct usb_configuration c; |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 134 | int (*eth)(struct usb_configuration *c); |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 135 | int num; |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 136 | } gfs_configurations[] = { |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 137 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 138 | { |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 139 | .eth = bind_rndis_config, |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 140 | }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 141 | #endif |
| 142 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 143 | #ifdef CONFIG_USB_FUNCTIONFS_ETH |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 144 | { |
| 145 | .eth = eth_bind_config, |
| 146 | }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 147 | #endif |
| 148 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 149 | #ifdef CONFIG_USB_FUNCTIONFS_GENERIC |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 150 | { |
| 151 | }, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 152 | #endif |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 153 | }; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 154 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 155 | static void *functionfs_acquire_dev(struct ffs_dev *dev); |
| 156 | static void functionfs_release_dev(struct ffs_dev *dev); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 157 | static int functionfs_ready_callback(struct ffs_data *ffs); |
| 158 | static void functionfs_closed_callback(struct ffs_data *ffs); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 159 | static int gfs_bind(struct usb_composite_dev *cdev); |
| 160 | static int gfs_unbind(struct usb_composite_dev *cdev); |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 161 | static int gfs_do_config(struct usb_configuration *c); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 162 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 163 | |
Sebastian Andrzej Siewior | c2ec75c | 2012-09-06 20:11:03 +0200 | [diff] [blame] | 164 | static __refdata struct usb_composite_driver gfs_driver = { |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 165 | .name = DRIVER_NAME, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 166 | .dev = &gfs_dev_desc, |
| 167 | .strings = gfs_dev_strings, |
Tatyana Brokhman | 35a0e0b | 2011-06-29 16:41:49 +0300 | [diff] [blame] | 168 | .max_speed = USB_SPEED_HIGH, |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 169 | .bind = gfs_bind, |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 170 | .unbind = gfs_unbind, |
| 171 | }; |
| 172 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 173 | static unsigned int missing_funcs; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 174 | static bool gfs_registered; |
| 175 | static bool gfs_single_func; |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 176 | static struct usb_function_instance **fi_ffs; |
| 177 | static struct usb_function **f_ffs[] = { |
| 178 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
| 179 | NULL, |
| 180 | #endif |
| 181 | |
| 182 | #ifdef CONFIG_USB_FUNCTIONFS_ETH |
| 183 | NULL, |
| 184 | #endif |
| 185 | |
| 186 | #ifdef CONFIG_USB_FUNCTIONFS_GENERIC |
| 187 | NULL, |
| 188 | #endif |
| 189 | }; |
| 190 | |
| 191 | #define N_CONF ARRAY_SIZE(f_ffs) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 192 | |
Andrzej Pietrasiewicz | 8545e60 | 2012-03-12 12:55:42 +0100 | [diff] [blame] | 193 | static int __init gfs_init(void) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 194 | { |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 195 | struct f_fs_opts *opts; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 196 | int i; |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 197 | int ret = 0; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 198 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 199 | ENTER(); |
| 200 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 201 | if (func_num < 2) { |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 202 | gfs_single_func = true; |
| 203 | func_num = 1; |
| 204 | } |
| 205 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 206 | /* |
| 207 | * Allocate in one chunk for easier maintenance |
| 208 | */ |
| 209 | f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL); |
| 210 | if (!f_ffs[0]) { |
| 211 | ret = -ENOMEM; |
| 212 | goto no_func; |
| 213 | } |
| 214 | for (i = 1; i < N_CONF; ++i) |
| 215 | f_ffs[i] = f_ffs[0] + i * func_num; |
| 216 | |
| 217 | fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL); |
| 218 | if (!fi_ffs) { |
| 219 | ret = -ENOMEM; |
| 220 | goto no_func; |
| 221 | } |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 222 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 223 | for (i = 0; i < func_num; i++) { |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 224 | fi_ffs[i] = usb_get_function_instance("ffs"); |
| 225 | if (IS_ERR(fi_ffs[i])) { |
| 226 | ret = PTR_ERR(fi_ffs[i]); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 227 | --i; |
| 228 | goto no_dev; |
| 229 | } |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 230 | opts = to_f_fs_opts(fi_ffs[i]); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 231 | if (gfs_single_func) |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 232 | ret = ffs_single_dev(opts->dev); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 233 | else |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 234 | ret = ffs_name_dev(opts->dev, func_names[i]); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 235 | if (ret) |
| 236 | goto no_dev; |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 237 | opts->dev->ffs_ready_callback = functionfs_ready_callback; |
| 238 | opts->dev->ffs_closed_callback = functionfs_closed_callback; |
| 239 | opts->dev->ffs_acquire_dev_callback = functionfs_acquire_dev; |
| 240 | opts->dev->ffs_release_dev_callback = functionfs_release_dev; |
| 241 | opts->no_configfs = true; |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 242 | } |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 243 | |
| 244 | missing_funcs = func_num; |
| 245 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 246 | return 0; |
| 247 | no_dev: |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 248 | while (i >= 0) |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 249 | usb_put_function_instance(fi_ffs[i--]); |
| 250 | kfree(fi_ffs); |
| 251 | no_func: |
| 252 | kfree(f_ffs[0]); |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 253 | return ret; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 254 | } |
| 255 | module_init(gfs_init); |
| 256 | |
Andrzej Pietrasiewicz | 8545e60 | 2012-03-12 12:55:42 +0100 | [diff] [blame] | 257 | static void __exit gfs_exit(void) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 258 | { |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 259 | int i; |
| 260 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 261 | ENTER(); |
| 262 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 263 | if (gfs_registered) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 264 | usb_composite_unregister(&gfs_driver); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 265 | gfs_registered = false; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 266 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 267 | kfree(f_ffs[0]); |
| 268 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 269 | for (i = 0; i < func_num; i++) |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 270 | usb_put_function_instance(fi_ffs[i]); |
| 271 | |
| 272 | kfree(fi_ffs); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 273 | } |
| 274 | module_exit(gfs_exit); |
| 275 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 276 | static void *functionfs_acquire_dev(struct ffs_dev *dev) |
| 277 | { |
| 278 | if (!try_module_get(THIS_MODULE)) |
| 279 | return ERR_PTR(-ENODEV); |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | static void functionfs_release_dev(struct ffs_dev *dev) |
| 285 | { |
| 286 | module_put(THIS_MODULE); |
| 287 | } |
| 288 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 289 | /* |
| 290 | * The caller of this function takes ffs_lock |
| 291 | */ |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 292 | static int functionfs_ready_callback(struct ffs_data *ffs) |
| 293 | { |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 294 | int ret = 0; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 295 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 296 | if (--missing_funcs) |
| 297 | return 0; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 298 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 299 | if (gfs_registered) |
| 300 | return -EBUSY; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 301 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 302 | gfs_registered = true; |
| 303 | |
Sebastian Andrzej Siewior | 03e42bd | 2012-09-06 20:11:04 +0200 | [diff] [blame] | 304 | ret = usb_composite_probe(&gfs_driver); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 305 | if (unlikely(ret < 0)) |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 306 | gfs_registered = false; |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 307 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 308 | return ret; |
| 309 | } |
| 310 | |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 311 | /* |
| 312 | * The caller of this function takes ffs_lock |
| 313 | */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 314 | static void functionfs_closed_callback(struct ffs_data *ffs) |
| 315 | { |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 316 | missing_funcs++; |
| 317 | |
| 318 | if (gfs_registered) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 319 | usb_composite_unregister(&gfs_driver); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 320 | gfs_registered = false; |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /* |
Andrzej Pietrasiewicz | 4b187fc | 2013-12-03 15:15:32 +0100 | [diff] [blame] | 324 | * 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] | 325 | */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 326 | static int gfs_bind(struct usb_composite_dev *cdev) |
| 327 | { |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 328 | #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 329 | struct net_device *net; |
| 330 | #endif |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 331 | int ret, i; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 332 | |
| 333 | ENTER(); |
| 334 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 335 | if (missing_funcs) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 336 | return -ENODEV; |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 337 | #if defined CONFIG_USB_FUNCTIONFS_ETH |
| 338 | if (can_support_ecm(cdev->gadget)) { |
| 339 | struct f_ecm_opts *ecm_opts; |
| 340 | |
| 341 | fi_ecm = usb_get_function_instance("ecm"); |
| 342 | if (IS_ERR(fi_ecm)) |
| 343 | return PTR_ERR(fi_ecm); |
| 344 | ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst); |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 345 | net = ecm_opts->net; |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 346 | } else { |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 347 | struct f_gether_opts *geth_opts; |
| 348 | |
| 349 | fi_geth = usb_get_function_instance("geth"); |
| 350 | if (IS_ERR(fi_geth)) |
| 351 | return PTR_ERR(fi_geth); |
| 352 | geth_opts = container_of(fi_geth, struct f_gether_opts, |
| 353 | func_inst); |
| 354 | net = geth_opts->net; |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 355 | } |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 356 | #endif |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 357 | |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 358 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
| 359 | { |
| 360 | struct f_rndis_opts *rndis_opts; |
| 361 | |
| 362 | fi_rndis = usb_get_function_instance("rndis"); |
| 363 | if (IS_ERR(fi_rndis)) { |
| 364 | ret = PTR_ERR(fi_rndis); |
| 365 | goto error; |
| 366 | } |
| 367 | rndis_opts = container_of(fi_rndis, struct f_rndis_opts, |
| 368 | func_inst); |
| 369 | #ifndef CONFIG_USB_FUNCTIONFS_ETH |
| 370 | net = rndis_opts->net; |
| 371 | #endif |
| 372 | } |
| 373 | #endif |
| 374 | |
| 375 | #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS |
| 376 | gether_set_qmult(net, qmult); |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 377 | if (!gether_set_host_addr(net, host_addr)) |
| 378 | pr_info("using host ethernet address: %s", host_addr); |
| 379 | if (!gether_set_dev_addr(net, dev_addr)) |
| 380 | pr_info("using self ethernet address: %s", dev_addr); |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 381 | #endif |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 382 | |
| 383 | #if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 384 | gether_set_gadget(net, cdev->gadget); |
| 385 | ret = gether_register_netdev(net); |
| 386 | if (ret) |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 387 | goto error_rndis; |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 388 | |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 389 | if (can_support_ecm(cdev->gadget)) { |
| 390 | struct f_ecm_opts *ecm_opts; |
| 391 | |
| 392 | ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst); |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 393 | ecm_opts->bound = true; |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 394 | } else { |
| 395 | struct f_gether_opts *geth_opts; |
| 396 | |
| 397 | geth_opts = container_of(fi_geth, struct f_gether_opts, |
| 398 | func_inst); |
| 399 | geth_opts->bound = true; |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 400 | } |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 401 | |
| 402 | rndis_borrow_net(fi_rndis, net); |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 403 | #endif |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 404 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 405 | /* TODO: gstrings_attach? */ |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 406 | ret = usb_string_ids_tab(cdev, gfs_strings); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 407 | if (unlikely(ret < 0)) |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 408 | goto error_rndis; |
Sebastian Andrzej Siewior | d33f74f | 2012-09-10 15:01:57 +0200 | [diff] [blame] | 409 | gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 410 | |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 411 | for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) { |
| 412 | struct gfs_configuration *c = gfs_configurations + i; |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 413 | int sid = USB_GADGET_FIRST_AVAIL_IDX + i; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 414 | |
Sebastian Andrzej Siewior | 276e2e4 | 2012-09-06 20:11:21 +0200 | [diff] [blame] | 415 | c->c.label = gfs_strings[sid].s; |
| 416 | c->c.iConfiguration = gfs_strings[sid].id; |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 417 | c->c.bConfigurationValue = 1 + i; |
| 418 | c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 419 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 420 | c->num = i; |
| 421 | |
Uwe Kleine-König | c9bfff9 | 2010-08-12 17:43:55 +0200 | [diff] [blame] | 422 | ret = usb_add_config(cdev, &c->c, gfs_do_config); |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 423 | if (unlikely(ret < 0)) |
| 424 | goto error_unbind; |
| 425 | } |
Sebastian Andrzej Siewior | 7d16e8d | 2012-09-10 15:01:53 +0200 | [diff] [blame] | 426 | usb_composite_overwrite_options(cdev, &coverwrite); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 427 | return 0; |
| 428 | |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 429 | /* TODO */ |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 430 | error_unbind: |
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 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 475 | /* |
| 476 | * 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] | 477 | * ffs_lock is held |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 478 | */ |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 479 | static int gfs_do_config(struct usb_configuration *c) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 480 | { |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 481 | struct gfs_configuration *gc = |
| 482 | container_of(c, struct gfs_configuration, c); |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 483 | int i; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 484 | int ret; |
| 485 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 486 | if (missing_funcs) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 487 | return -ENODEV; |
| 488 | |
| 489 | if (gadget_is_otg(c->cdev->gadget)) { |
| 490 | c->descriptors = gfs_otg_desc; |
| 491 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; |
| 492 | } |
| 493 | |
Michal Nazarewicz | f8dae53 | 2010-06-25 16:29:27 +0200 | [diff] [blame] | 494 | if (gc->eth) { |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 495 | ret = gc->eth(c); |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 496 | if (unlikely(ret < 0)) |
| 497 | return ret; |
| 498 | } |
| 499 | |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 500 | for (i = 0; i < func_num; i++) { |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 501 | f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]); |
| 502 | if (IS_ERR(f_ffs[gc->num][i])) { |
| 503 | ret = PTR_ERR(f_ffs[gc->num][i]); |
| 504 | goto error; |
| 505 | } |
| 506 | ret = usb_add_function(c, f_ffs[gc->num][i]); |
| 507 | if (ret < 0) { |
| 508 | usb_put_function(f_ffs[gc->num][i]); |
| 509 | goto error; |
| 510 | } |
Andrzej Pietrasiewicz | 581791f | 2012-05-14 15:51:52 +0200 | [diff] [blame] | 511 | } |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 512 | |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 513 | /* |
| 514 | * After previous do_configs there may be some invalid |
Michal Nazarewicz | f588c0d | 2010-06-14 10:43:34 +0200 | [diff] [blame] | 515 | * pointers in c->interface array. This happens every time |
| 516 | * a user space function with fewer interfaces than a user |
| 517 | * space function that was run before the new one is run. The |
| 518 | * compasit's set_config() assumes that if there is no more |
| 519 | * then MAX_CONFIG_INTERFACES interfaces in a configuration |
| 520 | * then there is a NULL pointer after the last interface in |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 521 | * c->interface array. We need to make sure this is true. |
| 522 | */ |
Michal Nazarewicz | f588c0d | 2010-06-14 10:43:34 +0200 | [diff] [blame] | 523 | if (c->next_interface_id < ARRAY_SIZE(c->interface)) |
| 524 | c->interface[c->next_interface_id] = NULL; |
| 525 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 526 | return 0; |
Andrzej Pietrasiewicz | 6f823cd | 2013-12-03 15:15:34 +0100 | [diff] [blame] | 527 | error: |
| 528 | while (--i >= 0) { |
| 529 | if (!IS_ERR(f_ffs[gc->num][i])) |
| 530 | usb_remove_function(c, f_ffs[gc->num][i]); |
| 531 | usb_put_function(f_ffs[gc->num][i]); |
| 532 | } |
| 533 | return ret; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 536 | #ifdef CONFIG_USB_FUNCTIONFS_ETH |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 537 | |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 538 | static int eth_bind_config(struct usb_configuration *c) |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 539 | { |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 540 | int status = 0; |
| 541 | |
| 542 | if (can_support_ecm(c->cdev->gadget)) { |
| 543 | f_ecm = usb_get_function(fi_ecm); |
| 544 | if (IS_ERR(f_ecm)) |
| 545 | return PTR_ERR(f_ecm); |
| 546 | |
| 547 | status = usb_add_function(c, f_ecm); |
| 548 | if (status < 0) |
| 549 | usb_put_function(f_ecm); |
| 550 | |
| 551 | } else { |
Andrzej Pietrasiewicz | 85aec59 | 2013-12-03 15:15:25 +0100 | [diff] [blame] | 552 | f_geth = usb_get_function(fi_geth); |
| 553 | if (IS_ERR(f_geth)) |
| 554 | return PTR_ERR(f_geth); |
| 555 | |
| 556 | status = usb_add_function(c, f_geth); |
| 557 | if (status < 0) |
| 558 | usb_put_function(f_geth); |
Andrzej Pietrasiewicz | f212ad4 | 2013-12-03 15:15:23 +0100 | [diff] [blame] | 559 | } |
| 560 | return status; |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 561 | } |
Michal Nazarewicz | fc19de6 | 2010-08-12 17:43:48 +0200 | [diff] [blame] | 562 | |
Michal Nazarewicz | c6c5600 | 2010-05-05 12:53:15 +0200 | [diff] [blame] | 563 | #endif |
Andrzej Pietrasiewicz | 6e257b1 | 2013-12-03 15:15:27 +0100 | [diff] [blame] | 564 | |
| 565 | #ifdef CONFIG_USB_FUNCTIONFS_RNDIS |
| 566 | |
| 567 | static int bind_rndis_config(struct usb_configuration *c) |
| 568 | { |
| 569 | int status = 0; |
| 570 | |
| 571 | f_rndis = usb_get_function(fi_rndis); |
| 572 | if (IS_ERR(f_rndis)) |
| 573 | return PTR_ERR(f_rndis); |
| 574 | |
| 575 | status = usb_add_function(c, f_rndis); |
| 576 | if (status < 0) |
| 577 | usb_put_function(f_rndis); |
| 578 | |
| 579 | return status; |
| 580 | } |
| 581 | |
| 582 | #endif |