blob: 074df0d56255a41492f2da1253ccce8150f153db [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>
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020016/*
17 * kbuild is not very cooperative with respect to linking separately
18 * compiled library objects into one module. So for now we won't use
19 * separate compilation ... ensuring init/exit sections work to shrink
20 * the runtime footprint, and giving us at least some parts of what
21 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
22 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020023#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +010024#include <linux/netdevice.h>
25
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020026# if defined USB_ETH_RNDIS
27# undef USB_ETH_RNDIS
28# endif
29# ifdef CONFIG_USB_FUNCTIONFS_RNDIS
30# define USB_ETH_RNDIS y
31# endif
32
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +010033# include "u_ecm.h"
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +010034# include "u_gether.h"
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020035# ifdef USB_ETH_RNDIS
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +010036# include "u_rndis.h"
Andrzej Pietrasiewiczcbbd14a2013-05-24 10:23:02 +020037# include "rndis.h"
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020038# endif
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020039# include "u_ether.h"
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020040
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +010041USB_ETHERNET_MODULE_PARAMETERS();
42
Michal Nazarewiczf8dae532010-06-25 16:29:27 +020043# ifdef CONFIG_USB_FUNCTIONFS_ETH
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +010044static int eth_bind_config(struct usb_configuration *c);
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +010045static struct usb_function_instance *fi_ecm;
46static struct usb_function *f_ecm;
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +010047static struct usb_function_instance *fi_geth;
48static struct usb_function *f_geth;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020049# endif
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +010050# ifdef CONFIG_USB_FUNCTIONFS_RNDIS
51static int bind_rndis_config(struct usb_configuration *c);
52static struct usb_function_instance *fi_rndis;
53static struct usb_function *f_rndis;
54# endif
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020055#endif
56
57#include "f_fs.c"
58
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020059#define DRIVER_NAME "g_ffs"
60#define DRIVER_DESC "USB Function Filesystem"
61#define DRIVER_VERSION "24 Aug 2004"
62
63MODULE_DESCRIPTION(DRIVER_DESC);
64MODULE_AUTHOR("Michal Nazarewicz");
65MODULE_LICENSE("GPL");
66
Michal Nazarewiczfc19de62010-08-12 17:43:48 +020067#define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
68#define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020069
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +020070#define GFS_MAX_DEVS 10
71
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020072USB_GADGET_COMPOSITE_OPTIONS();
73
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020074static struct usb_device_descriptor gfs_dev_desc = {
75 .bLength = sizeof gfs_dev_desc,
76 .bDescriptorType = USB_DT_DEVICE,
77
78 .bcdUSB = cpu_to_le16(0x0200),
79 .bDeviceClass = USB_CLASS_PER_INTERFACE,
80
Michal Nazarewiczfc19de62010-08-12 17:43:48 +020081 .idVendor = cpu_to_le16(GFS_VENDOR_ID),
82 .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020083};
84
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +020085static char *func_names[GFS_MAX_DEVS];
86static unsigned int func_num;
87
Michal Nazarewiczfc19de62010-08-12 17:43:48 +020088module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
89MODULE_PARM_DESC(bDeviceClass, "USB Device class");
90module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
91MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
92module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
93MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +020094module_param_array_named(functions, func_names, charp, &func_num, 0);
95MODULE_PARM_DESC(functions, "USB Functions list");
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020096
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020097static const struct usb_descriptor_header *gfs_otg_desc[] = {
98 (const struct usb_descriptor_header *)
99 &(const struct usb_otg_descriptor) {
100 .bLength = sizeof(struct usb_otg_descriptor),
101 .bDescriptorType = USB_DT_OTG,
102
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200103 /*
104 * REVISIT SRP-only hardware is possible, although
105 * it would not be called "OTG" ...
106 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200107 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
108 },
109
110 NULL
111};
112
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100113/* String IDs are assigned dynamically */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200114static struct usb_string gfs_strings[] = {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200115 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewiord33f74f2012-09-10 15:01:57 +0200116 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200117 [USB_GADGET_SERIAL_IDX].s = "",
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200118#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200119 { .s = "FunctionFS + RNDIS" },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200120#endif
121#ifdef CONFIG_USB_FUNCTIONFS_ETH
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200122 { .s = "FunctionFS + ECM" },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200123#endif
124#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200125 { .s = "FunctionFS" },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200126#endif
127 { } /* end of list */
128};
129
130static struct usb_gadget_strings *gfs_dev_strings[] = {
131 &(struct usb_gadget_strings) {
132 .language = 0x0409, /* en-us */
133 .strings = gfs_strings,
134 },
135 NULL,
136};
137
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200138struct gfs_configuration {
139 struct usb_configuration c;
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100140 int (*eth)(struct usb_configuration *c);
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200141} gfs_configurations[] = {
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200142#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200143 {
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100144 .eth = bind_rndis_config,
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200145 },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200146#endif
147
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200148#ifdef CONFIG_USB_FUNCTIONFS_ETH
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200149 {
150 .eth = eth_bind_config,
151 },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200152#endif
153
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200154#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200155 {
156 },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200157#endif
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200158};
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200159
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100160static int functionfs_ready_callback(struct ffs_data *ffs);
161static void functionfs_closed_callback(struct ffs_data *ffs);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200162static int gfs_bind(struct usb_composite_dev *cdev);
163static int gfs_unbind(struct usb_composite_dev *cdev);
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200164static int gfs_do_config(struct usb_configuration *c);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200165
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200166static __refdata struct usb_composite_driver gfs_driver = {
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200167 .name = DRIVER_NAME,
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200168 .dev = &gfs_dev_desc,
169 .strings = gfs_dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300170 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200171 .bind = gfs_bind,
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200172 .unbind = gfs_unbind,
173};
174
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200175static unsigned int missing_funcs;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200176static bool gfs_registered;
177static bool gfs_single_func;
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100178static struct ffs_dev **ffs_tab;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200179
Andrzej Pietrasiewicz8545e602012-03-12 12:55:42 +0100180static int __init gfs_init(void)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200181{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200182 int i;
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100183 int ret = 0;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200184
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200185 ENTER();
186
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100187 if (func_num < 2) {
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200188 gfs_single_func = true;
189 func_num = 1;
190 }
191
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100192 ffs_tab = kcalloc(func_num, sizeof(*ffs_tab), GFP_KERNEL);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200193 if (!ffs_tab)
194 return -ENOMEM;
195
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100196 for (i = 0; i < func_num; i++) {
197 ffs_dev_lock();
198 ffs_tab[i] = ffs_alloc_dev();
199 ffs_dev_unlock();
200 if (IS_ERR(ffs_tab[i])) {
201 ret = PTR_ERR(ffs_tab[i]);
202 --i;
203 goto no_dev;
204 }
205 if (gfs_single_func)
206 ret = ffs_single_dev(ffs_tab[i]);
207 else
208 ret = ffs_name_dev(ffs_tab[i], func_names[i]);
209 if (ret)
210 goto no_dev;
211 ffs_tab[i]->ffs_ready_callback = functionfs_ready_callback;
212 ffs_tab[i]->ffs_closed_callback = functionfs_closed_callback;
213 }
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200214
215 missing_funcs = func_num;
216
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100217 return 0;
218no_dev:
219 ffs_dev_lock();
220 while (i >= 0)
221 ffs_free_dev(ffs_tab[i--]);
222 ffs_dev_unlock();
223 kfree(ffs_tab);
224 return ret;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200225}
226module_init(gfs_init);
227
Andrzej Pietrasiewicz8545e602012-03-12 12:55:42 +0100228static void __exit gfs_exit(void)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200229{
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100230 int i;
231
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200232 ENTER();
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100233 ffs_dev_lock();
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200234
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200235 if (gfs_registered)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200236 usb_composite_unregister(&gfs_driver);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200237 gfs_registered = false;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200238
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100239 for (i = 0; i < func_num; i++)
240 ffs_free_dev(ffs_tab[i]);
241 ffs_dev_unlock();
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200242 kfree(ffs_tab);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200243}
244module_exit(gfs_exit);
245
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100246/*
247 * The caller of this function takes ffs_lock
248 */
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200249static int functionfs_ready_callback(struct ffs_data *ffs)
250{
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100251 int ret = 0;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200252
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100253 if (--missing_funcs)
254 return 0;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200255
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100256 if (gfs_registered)
257 return -EBUSY;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200258
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200259 gfs_registered = true;
260
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200261 ret = usb_composite_probe(&gfs_driver);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200262 if (unlikely(ret < 0))
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200263 gfs_registered = false;
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100264
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200265 return ret;
266}
267
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100268/*
269 * The caller of this function takes ffs_lock
270 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200271static void functionfs_closed_callback(struct ffs_data *ffs)
272{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200273 missing_funcs++;
274
275 if (gfs_registered)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200276 usb_composite_unregister(&gfs_driver);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200277 gfs_registered = false;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200278}
279
280/*
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100281 * It is assumed that gfs_bind is called from a context where ffs_lock is held
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200282 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200283static int gfs_bind(struct usb_composite_dev *cdev)
284{
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100285#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100286 struct net_device *net;
287#endif
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200288 int ret, i;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200289
290 ENTER();
291
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200292 if (missing_funcs)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200293 return -ENODEV;
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100294#if defined CONFIG_USB_FUNCTIONFS_ETH
295 if (can_support_ecm(cdev->gadget)) {
296 struct f_ecm_opts *ecm_opts;
297
298 fi_ecm = usb_get_function_instance("ecm");
299 if (IS_ERR(fi_ecm))
300 return PTR_ERR(fi_ecm);
301 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100302 net = ecm_opts->net;
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100303 } else {
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100304 struct f_gether_opts *geth_opts;
305
306 fi_geth = usb_get_function_instance("geth");
307 if (IS_ERR(fi_geth))
308 return PTR_ERR(fi_geth);
309 geth_opts = container_of(fi_geth, struct f_gether_opts,
310 func_inst);
311 net = geth_opts->net;
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100312 }
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100313#endif
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100314
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100315#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
316 {
317 struct f_rndis_opts *rndis_opts;
318
319 fi_rndis = usb_get_function_instance("rndis");
320 if (IS_ERR(fi_rndis)) {
321 ret = PTR_ERR(fi_rndis);
322 goto error;
323 }
324 rndis_opts = container_of(fi_rndis, struct f_rndis_opts,
325 func_inst);
326#ifndef CONFIG_USB_FUNCTIONFS_ETH
327 net = rndis_opts->net;
328#endif
329 }
330#endif
331
332#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
333 gether_set_qmult(net, qmult);
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100334 if (!gether_set_host_addr(net, host_addr))
335 pr_info("using host ethernet address: %s", host_addr);
336 if (!gether_set_dev_addr(net, dev_addr))
337 pr_info("using self ethernet address: %s", dev_addr);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100338#endif
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100339
340#if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100341 gether_set_gadget(net, cdev->gadget);
342 ret = gether_register_netdev(net);
343 if (ret)
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100344 goto error_rndis;
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100345
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100346 if (can_support_ecm(cdev->gadget)) {
347 struct f_ecm_opts *ecm_opts;
348
349 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100350 ecm_opts->bound = true;
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100351 } else {
352 struct f_gether_opts *geth_opts;
353
354 geth_opts = container_of(fi_geth, struct f_gether_opts,
355 func_inst);
356 geth_opts->bound = true;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100357 }
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100358
359 rndis_borrow_net(fi_rndis, net);
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100360#endif
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200361
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200362 ret = usb_string_ids_tab(cdev, gfs_strings);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200363 if (unlikely(ret < 0))
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100364 goto error_rndis;
Sebastian Andrzej Siewiord33f74f2012-09-10 15:01:57 +0200365 gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200366
Andrzej Pietrasiewicz34169052013-03-11 16:32:14 +0100367 for (i = func_num; i--; ) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100368 ret = functionfs_bind(ffs_tab[i]->ffs_data, cdev);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200369 if (unlikely(ret < 0)) {
370 while (++i < func_num)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100371 functionfs_unbind(ffs_tab[i]->ffs_data);
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100372 goto error_rndis;
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200373 }
374 }
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200375
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200376 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
377 struct gfs_configuration *c = gfs_configurations + i;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200378 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200379
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200380 c->c.label = gfs_strings[sid].s;
381 c->c.iConfiguration = gfs_strings[sid].id;
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200382 c->c.bConfigurationValue = 1 + i;
383 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200384
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200385 ret = usb_add_config(cdev, &c->c, gfs_do_config);
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200386 if (unlikely(ret < 0))
387 goto error_unbind;
388 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200389 usb_composite_overwrite_options(cdev, &coverwrite);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200390 return 0;
391
392error_unbind:
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200393 for (i = 0; i < func_num; i++)
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100394 functionfs_unbind(ffs_tab[i]->ffs_data);
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100395error_rndis:
396#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
397 usb_put_function_instance(fi_rndis);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200398error:
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100399#endif
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100400#if defined CONFIG_USB_FUNCTIONFS_ETH
401 if (can_support_ecm(cdev->gadget))
402 usb_put_function_instance(fi_ecm);
403 else
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100404 usb_put_function_instance(fi_geth);
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100405#endif
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200406 return ret;
407}
408
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200409/*
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100410 * It is assumed that gfs_unbind is called from a context where ffs_lock is held
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200411 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200412static int gfs_unbind(struct usb_composite_dev *cdev)
413{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200414 int i;
415
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200416 ENTER();
417
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100418
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100419#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
420 usb_put_function(f_rndis);
421 usb_put_function_instance(fi_rndis);
422#endif
423
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100424#if defined CONFIG_USB_FUNCTIONFS_ETH
425 if (can_support_ecm(cdev->gadget)) {
426 usb_put_function(f_ecm);
427 usb_put_function_instance(fi_ecm);
428 } else {
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100429 usb_put_function(f_geth);
430 usb_put_function_instance(fi_geth);
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100431 }
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100432#endif
Andrzej Pietrasiewicz1ec8f002013-12-03 15:15:22 +0100433
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200434 /*
435 * We may have been called in an error recovery from
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200436 * composite_bind() after gfs_unbind() failure so we need to
Andrzej Pietrasiewicz1ec8f002013-12-03 15:15:22 +0100437 * check if instance's ffs_data is not NULL since gfs_bind() handles
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200438 * all error recovery itself. I'd rather we werent called
439 * from composite on orror recovery, but what you're gonna
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200440 * do...?
441 */
Andrzej Pietrasiewicz34169052013-03-11 16:32:14 +0100442 for (i = func_num; i--; )
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100443 if (ffs_tab[i]->ffs_data)
444 functionfs_unbind(ffs_tab[i]->ffs_data);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200445
446 return 0;
447}
448
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200449/*
450 * It is assumed that gfs_do_config is called from a context where
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100451 * ffs_lock is held
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200452 */
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200453static int gfs_do_config(struct usb_configuration *c)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200454{
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200455 struct gfs_configuration *gc =
456 container_of(c, struct gfs_configuration, c);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200457 int i;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200458 int ret;
459
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200460 if (missing_funcs)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200461 return -ENODEV;
462
463 if (gadget_is_otg(c->cdev->gadget)) {
464 c->descriptors = gfs_otg_desc;
465 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
466 }
467
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200468 if (gc->eth) {
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100469 ret = gc->eth(c);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200470 if (unlikely(ret < 0))
471 return ret;
472 }
473
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200474 for (i = 0; i < func_num; i++) {
Andrzej Pietrasiewicz4b187fc2013-12-03 15:15:32 +0100475 ret = functionfs_bind_config(c->cdev, c, ffs_tab[i]->ffs_data);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200476 if (unlikely(ret < 0))
477 return ret;
478 }
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200479
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200480 /*
481 * After previous do_configs there may be some invalid
Michal Nazarewiczf588c0d2010-06-14 10:43:34 +0200482 * pointers in c->interface array. This happens every time
483 * a user space function with fewer interfaces than a user
484 * space function that was run before the new one is run. The
485 * compasit's set_config() assumes that if there is no more
486 * then MAX_CONFIG_INTERFACES interfaces in a configuration
487 * then there is a NULL pointer after the last interface in
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200488 * c->interface array. We need to make sure this is true.
489 */
Michal Nazarewiczf588c0d2010-06-14 10:43:34 +0200490 if (c->next_interface_id < ARRAY_SIZE(c->interface))
491 c->interface[c->next_interface_id] = NULL;
492
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200493 return 0;
494}
495
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200496#ifdef CONFIG_USB_FUNCTIONFS_ETH
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200497
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100498static int eth_bind_config(struct usb_configuration *c)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200499{
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100500 int status = 0;
501
502 if (can_support_ecm(c->cdev->gadget)) {
503 f_ecm = usb_get_function(fi_ecm);
504 if (IS_ERR(f_ecm))
505 return PTR_ERR(f_ecm);
506
507 status = usb_add_function(c, f_ecm);
508 if (status < 0)
509 usb_put_function(f_ecm);
510
511 } else {
Andrzej Pietrasiewicz85aec592013-12-03 15:15:25 +0100512 f_geth = usb_get_function(fi_geth);
513 if (IS_ERR(f_geth))
514 return PTR_ERR(f_geth);
515
516 status = usb_add_function(c, f_geth);
517 if (status < 0)
518 usb_put_function(f_geth);
Andrzej Pietrasiewiczf212ad42013-12-03 15:15:23 +0100519 }
520 return status;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200521}
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200522
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200523#endif
Andrzej Pietrasiewicz6e257b12013-12-03 15:15:27 +0100524
525#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
526
527static int bind_rndis_config(struct usb_configuration *c)
528{
529 int status = 0;
530
531 f_rndis = usb_get_function(fi_rndis);
532 if (IS_ERR(f_rndis))
533 return PTR_ERR(f_rndis);
534
535 status = usb_add_function(c, f_rndis);
536 if (status < 0)
537 usb_put_function(f_rndis);
538
539 return status;
540}
541
542#endif