blob: 06acfa55864a639a6015689770b4ccb06f01ad40 [file] [log] [blame]
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01001/*
2 * g_ffs.c -- user mode file system API for USB composite function controllers
3 *
4 * Copyright (C) 2010 Samsung Electronics
Michal Nazarewicz54b83602012-01-13 15:05:16 +01005 * Author: Michal Nazarewicz <mina86@mina86.com>
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +01006 *
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 Nazarewicz5ab54cf2010-11-12 14:29:28 +010011 */
12
Michal Nazarewiczaa02f172010-11-17 17:09:47 +010013#define pr_fmt(fmt) "g_ffs: " fmt
14
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020015#include <linux/module.h>
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +010016
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020017#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +010018#include <linux/netdevice.h>
19
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020020# 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 Pietrasiewiczf212ad42013-12-03 15:15:23 +010027# include "u_ecm.h"
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +010028# include "u_gether.h"
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020029# ifdef USB_ETH_RNDIS
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +010030# include "u_rndis.h"
Andrzej Pietrasiewiczcbbd14a2013-05-24 10:23:02 +020031# include "rndis.h"
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020032# endif
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020033# include "u_ether.h"
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020034
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +010035USB_ETHERNET_MODULE_PARAMETERS();
36
Michal Nazarewiczf8dae532010-06-25 16:29:27 +020037# ifdef CONFIG_USB_FUNCTIONFS_ETH
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +010038static int eth_bind_config(struct usb_configuration *c);
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +010039static struct usb_function_instance *fi_ecm;
40static struct usb_function *f_ecm;
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +010041static struct usb_function_instance *fi_geth;
42static struct usb_function *f_geth;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020043# endif
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +010044# ifdef CONFIG_USB_FUNCTIONFS_RNDIS
45static int bind_rndis_config(struct usb_configuration *c);
46static struct usb_function_instance *fi_rndis;
47static struct usb_function *f_rndis;
48# endif
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020049#endif
50
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +010051#include "u_fs.h"
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020052
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020053#define DRIVER_NAME "g_ffs"
54#define DRIVER_DESC "USB Function Filesystem"
55#define DRIVER_VERSION "24 Aug 2004"
56
57MODULE_DESCRIPTION(DRIVER_DESC);
58MODULE_AUTHOR("Michal Nazarewicz");
59MODULE_LICENSE("GPL");
60
Michal Nazarewiczfc19de62010-08-12 17:43:48 +020061#define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
62#define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020063
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +020064#define GFS_MAX_DEVS 10
65
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020066USB_GADGET_COMPOSITE_OPTIONS();
67
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020068static 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 Nazarewiczfc19de62010-08-12 17:43:48 +020075 .idVendor = cpu_to_le16(GFS_VENDOR_ID),
76 .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020077};
78
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +020079static char *func_names[GFS_MAX_DEVS];
80static unsigned int func_num;
81
Michal Nazarewiczfc19de62010-08-12 17:43:48 +020082module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
83MODULE_PARM_DESC(bDeviceClass, "USB Device class");
84module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
85MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
86module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
87MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +020088module_param_array_named(functions, func_names, charp, &func_num, 0);
89MODULE_PARM_DESC(functions, "USB Functions list");
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020090
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020091static 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 Nazarewiczfc19de62010-08-12 17:43:48 +020097 /*
98 * REVISIT SRP-only hardware is possible, although
99 * it would not be called "OTG" ...
100 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200101 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
102 },
103
104 NULL
105};
106
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100107/* String IDs are assigned dynamically */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200108static struct usb_string gfs_strings[] = {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200109 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewiord33f74f2012-09-10 15:01:57 +0200110 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200111 [USB_GADGET_SERIAL_IDX].s = "",
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200112#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200113 { .s = "FunctionFS + RNDIS" },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200114#endif
115#ifdef CONFIG_USB_FUNCTIONFS_ETH
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200116 { .s = "FunctionFS + ECM" },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200117#endif
118#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200119 { .s = "FunctionFS" },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200120#endif
121 { } /* end of list */
122};
123
124static 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 Nazarewiczf8dae532010-06-25 16:29:27 +0200132struct gfs_configuration {
133 struct usb_configuration c;
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100134 int (*eth)(struct usb_configuration *c);
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100135 int num;
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200136} gfs_configurations[] = {
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200137#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200138 {
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100139 .eth = bind_rndis_config,
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200140 },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200141#endif
142
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200143#ifdef CONFIG_USB_FUNCTIONFS_ETH
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200144 {
145 .eth = eth_bind_config,
146 },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200147#endif
148
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200149#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200150 {
151 },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200152#endif
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200153};
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200154
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100155static void *functionfs_acquire_dev(struct ffs_dev *dev);
156static void functionfs_release_dev(struct ffs_dev *dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100157static int functionfs_ready_callback(struct ffs_data *ffs);
158static void functionfs_closed_callback(struct ffs_data *ffs);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200159static int gfs_bind(struct usb_composite_dev *cdev);
160static int gfs_unbind(struct usb_composite_dev *cdev);
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200161static int gfs_do_config(struct usb_configuration *c);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200162
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100163
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200164static __refdata struct usb_composite_driver gfs_driver = {
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200165 .name = DRIVER_NAME,
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200166 .dev = &gfs_dev_desc,
167 .strings = gfs_dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300168 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200169 .bind = gfs_bind,
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200170 .unbind = gfs_unbind,
171};
172
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200173static unsigned int missing_funcs;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200174static bool gfs_registered;
175static bool gfs_single_func;
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100176static struct usb_function_instance **fi_ffs;
177static 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 Nazarewiczc6c56002010-05-05 12:53:15 +0200192
Andrzej Pietrasiewicz8545e602012-03-12 12:55:42 +0100193static int __init gfs_init(void)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200194{
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100195 struct f_fs_opts *opts;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200196 int i;
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100197 int ret = 0;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200198
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200199 ENTER();
200
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100201 if (func_num < 2) {
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200202 gfs_single_func = true;
203 func_num = 1;
204 }
205
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100206 /*
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 Pietrasiewicz581791f2012-05-14 15:51:52 +0200222
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100223 for (i = 0; i < func_num; i++) {
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100224 fi_ffs[i] = usb_get_function_instance("ffs");
225 if (IS_ERR(fi_ffs[i])) {
226 ret = PTR_ERR(fi_ffs[i]);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100227 --i;
228 goto no_dev;
229 }
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100230 opts = to_f_fs_opts(fi_ffs[i]);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100231 if (gfs_single_func)
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100232 ret = ffs_single_dev(opts->dev);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100233 else
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100234 ret = ffs_name_dev(opts->dev, func_names[i]);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100235 if (ret)
236 goto no_dev;
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100237 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 Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100242 }
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200243
244 missing_funcs = func_num;
245
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100246 return 0;
247no_dev:
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100248 while (i >= 0)
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100249 usb_put_function_instance(fi_ffs[i--]);
250 kfree(fi_ffs);
251no_func:
252 kfree(f_ffs[0]);
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100253 return ret;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200254}
255module_init(gfs_init);
256
Andrzej Pietrasiewicz8545e602012-03-12 12:55:42 +0100257static void __exit gfs_exit(void)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200258{
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100259 int i;
260
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200261 ENTER();
262
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200263 if (gfs_registered)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200264 usb_composite_unregister(&gfs_driver);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200265 gfs_registered = false;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200266
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100267 kfree(f_ffs[0]);
268
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100269 for (i = 0; i < func_num; i++)
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100270 usb_put_function_instance(fi_ffs[i]);
271
272 kfree(fi_ffs);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200273}
274module_exit(gfs_exit);
275
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100276static void *functionfs_acquire_dev(struct ffs_dev *dev)
277{
278 if (!try_module_get(THIS_MODULE))
Krzysztof Opasiakd668b4f2014-05-21 14:05:35 +0200279 return ERR_PTR(-ENOENT);
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100280
281 return 0;
282}
283
284static void functionfs_release_dev(struct ffs_dev *dev)
285{
286 module_put(THIS_MODULE);
287}
288
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100289/*
290 * The caller of this function takes ffs_lock
291 */
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200292static int functionfs_ready_callback(struct ffs_data *ffs)
293{
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100294 int ret = 0;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200295
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100296 if (--missing_funcs)
297 return 0;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200298
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100299 if (gfs_registered)
300 return -EBUSY;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200301
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200302 gfs_registered = true;
303
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200304 ret = usb_composite_probe(&gfs_driver);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200305 if (unlikely(ret < 0))
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200306 gfs_registered = false;
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100307
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200308 return ret;
309}
310
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100311/*
312 * The caller of this function takes ffs_lock
313 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200314static void functionfs_closed_callback(struct ffs_data *ffs)
315{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200316 missing_funcs++;
317
318 if (gfs_registered)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200319 usb_composite_unregister(&gfs_driver);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200320 gfs_registered = false;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200321}
322
323/*
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100324 * It is assumed that gfs_bind is called from a context where ffs_lock is held
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200325 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200326static int gfs_bind(struct usb_composite_dev *cdev)
327{
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100328#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100329 struct net_device *net;
330#endif
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200331 int ret, i;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200332
333 ENTER();
334
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200335 if (missing_funcs)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200336 return -ENODEV;
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100337#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 Pietrasiewicz85aec592013-12-03 15:15:25 +0100345 net = ecm_opts->net;
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100346 } else {
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100347 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 Pietrasiewiczf212ad42013-12-03 15:15:23 +0100355 }
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100356#endif
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100357
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100358#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 Pietrasiewicz85aec592013-12-03 15:15:25 +0100377 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 Siewiord6a01432012-12-23 21:10:12 +0100381#endif
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100382
383#if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100384 gether_set_gadget(net, cdev->gadget);
385 ret = gether_register_netdev(net);
386 if (ret)
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100387 goto error_rndis;
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100388
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100389 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 Pietrasiewiczf212ad42013-12-03 15:15:23 +0100393 ecm_opts->bound = true;
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100394 } 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 Siewiord6a01432012-12-23 21:10:12 +0100400 }
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100401
402 rndis_borrow_net(fi_rndis, net);
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100403#endif
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200404
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100405 /* TODO: gstrings_attach? */
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200406 ret = usb_string_ids_tab(cdev, gfs_strings);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200407 if (unlikely(ret < 0))
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100408 goto error_rndis;
Sebastian Andrzej Siewiord33f74f2012-09-10 15:01:57 +0200409 gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200410
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200411 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
412 struct gfs_configuration *c = gfs_configurations + i;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200413 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200414
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200415 c->c.label = gfs_strings[sid].s;
416 c->c.iConfiguration = gfs_strings[sid].id;
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200417 c->c.bConfigurationValue = 1 + i;
418 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200419
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100420 c->num = i;
421
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200422 ret = usb_add_config(cdev, &c->c, gfs_do_config);
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200423 if (unlikely(ret < 0))
424 goto error_unbind;
425 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200426 usb_composite_overwrite_options(cdev, &coverwrite);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200427 return 0;
428
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100429/* TODO */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200430error_unbind:
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100431error_rndis:
432#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
433 usb_put_function_instance(fi_rndis);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200434error:
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100435#endif
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100436#if defined CONFIG_USB_FUNCTIONFS_ETH
437 if (can_support_ecm(cdev->gadget))
438 usb_put_function_instance(fi_ecm);
439 else
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100440 usb_put_function_instance(fi_geth);
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100441#endif
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200442 return ret;
443}
444
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200445/*
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100446 * It is assumed that gfs_unbind is called from a context where ffs_lock is held
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200447 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200448static int gfs_unbind(struct usb_composite_dev *cdev)
449{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200450 int i;
451
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200452 ENTER();
453
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100454
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100455#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
456 usb_put_function(f_rndis);
457 usb_put_function_instance(fi_rndis);
458#endif
459
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100460#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 Pietrasiewicz85aec592013-12-03 15:15:25 +0100465 usb_put_function(f_geth);
466 usb_put_function_instance(fi_geth);
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100467 }
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100468#endif
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100469 for (i = 0; i < N_CONF * func_num; ++i)
470 usb_put_function(*(f_ffs[0] + i));
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200471
472 return 0;
473}
474
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200475/*
476 * It is assumed that gfs_do_config is called from a context where
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100477 * ffs_lock is held
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200478 */
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200479static int gfs_do_config(struct usb_configuration *c)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200480{
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200481 struct gfs_configuration *gc =
482 container_of(c, struct gfs_configuration, c);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200483 int i;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200484 int ret;
485
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200486 if (missing_funcs)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200487 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 Nazarewiczf8dae532010-06-25 16:29:27 +0200494 if (gc->eth) {
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100495 ret = gc->eth(c);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200496 if (unlikely(ret < 0))
497 return ret;
498 }
499
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200500 for (i = 0; i < func_num; i++) {
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100501 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 Pietrasiewicz581791f2012-05-14 15:51:52 +0200511 }
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200512
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200513 /*
514 * After previous do_configs there may be some invalid
Michal Nazarewiczf588c0d2010-06-14 10:43:34 +0200515 * 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 Nazarewiczfc19de62010-08-12 17:43:48 +0200521 * c->interface array. We need to make sure this is true.
522 */
Michal Nazarewiczf588c0d2010-06-14 10:43:34 +0200523 if (c->next_interface_id < ARRAY_SIZE(c->interface))
524 c->interface[c->next_interface_id] = NULL;
525
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200526 return 0;
Andrzej Pietrasiewicz6f823cd2013-12-03 15:15:34 +0100527error:
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 Nazarewiczc6c56002010-05-05 12:53:15 +0200534}
535
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200536#ifdef CONFIG_USB_FUNCTIONFS_ETH
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200537
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100538static int eth_bind_config(struct usb_configuration *c)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200539{
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100540 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 Pietrasiewicz85aec592013-12-03 15:15:25 +0100552 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 Pietrasiewiczf212ad42013-12-03 15:15:23 +0100559 }
560 return status;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200561}
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200562
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200563#endif
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100564
565#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
566
567static 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