blob: fbfdb53a2db5f8466368b0e18b9c85c90f5fe9f4 [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
24# if defined USB_ETH_RNDIS
25# undef USB_ETH_RNDIS
26# endif
27# ifdef CONFIG_USB_FUNCTIONFS_RNDIS
28# define USB_ETH_RNDIS y
29# endif
30
31# include "f_ecm.c"
32# include "f_subset.c"
33# ifdef USB_ETH_RNDIS
34# include "f_rndis.c"
Andrzej Pietrasiewiczcbbd14a2013-05-24 10:23:02 +020035# include "rndis.h"
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020036# endif
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020037# include "u_ether.h"
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020038
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020039static u8 gfs_host_mac[ETH_ALEN];
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +010040static struct eth_dev *the_dev;
Michal Nazarewiczf8dae532010-06-25 16:29:27 +020041# ifdef CONFIG_USB_FUNCTIONFS_ETH
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +010042static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
43 struct eth_dev *dev);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020044# endif
Michal Nazarewiczf8dae532010-06-25 16:29:27 +020045#else
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +010046# define the_dev NULL
47# define gether_cleanup(dev) do { } while (0)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020048# define gfs_host_mac NULL
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +010049struct eth_dev;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020050#endif
51
52#include "f_fs.c"
53
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020054#define DRIVER_NAME "g_ffs"
55#define DRIVER_DESC "USB Function Filesystem"
56#define DRIVER_VERSION "24 Aug 2004"
57
58MODULE_DESCRIPTION(DRIVER_DESC);
59MODULE_AUTHOR("Michal Nazarewicz");
60MODULE_LICENSE("GPL");
61
Michal Nazarewiczfc19de62010-08-12 17:43:48 +020062#define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
63#define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020064
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +020065#define GFS_MAX_DEVS 10
66
67struct gfs_ffs_obj {
68 const char *name;
69 bool mounted;
70 bool desc_ready;
71 struct ffs_data *ffs_data;
72};
73
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020074USB_GADGET_COMPOSITE_OPTIONS();
75
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020076USB_ETHERNET_MODULE_PARAMETERS();
77
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020078static struct usb_device_descriptor gfs_dev_desc = {
79 .bLength = sizeof gfs_dev_desc,
80 .bDescriptorType = USB_DT_DEVICE,
81
82 .bcdUSB = cpu_to_le16(0x0200),
83 .bDeviceClass = USB_CLASS_PER_INTERFACE,
84
Michal Nazarewiczfc19de62010-08-12 17:43:48 +020085 .idVendor = cpu_to_le16(GFS_VENDOR_ID),
86 .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020087};
88
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +020089static char *func_names[GFS_MAX_DEVS];
90static unsigned int func_num;
91
Michal Nazarewiczfc19de62010-08-12 17:43:48 +020092module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
93MODULE_PARM_DESC(bDeviceClass, "USB Device class");
94module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
95MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
96module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
97MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +020098module_param_array_named(functions, func_names, charp, &func_num, 0);
99MODULE_PARM_DESC(functions, "USB Functions list");
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200100
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200101static const struct usb_descriptor_header *gfs_otg_desc[] = {
102 (const struct usb_descriptor_header *)
103 &(const struct usb_otg_descriptor) {
104 .bLength = sizeof(struct usb_otg_descriptor),
105 .bDescriptorType = USB_DT_OTG,
106
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200107 /*
108 * REVISIT SRP-only hardware is possible, although
109 * it would not be called "OTG" ...
110 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200111 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
112 },
113
114 NULL
115};
116
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100117/* String IDs are assigned dynamically */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200118static struct usb_string gfs_strings[] = {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200119 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewiord33f74f2012-09-10 15:01:57 +0200120 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200121 [USB_GADGET_SERIAL_IDX].s = "",
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200122#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200123 { .s = "FunctionFS + RNDIS" },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200124#endif
125#ifdef CONFIG_USB_FUNCTIONFS_ETH
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200126 { .s = "FunctionFS + ECM" },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200127#endif
128#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200129 { .s = "FunctionFS" },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200130#endif
131 { } /* end of list */
132};
133
134static struct usb_gadget_strings *gfs_dev_strings[] = {
135 &(struct usb_gadget_strings) {
136 .language = 0x0409, /* en-us */
137 .strings = gfs_strings,
138 },
139 NULL,
140};
141
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200142struct gfs_configuration {
143 struct usb_configuration c;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100144 int (*eth)(struct usb_configuration *c, u8 *ethaddr,
145 struct eth_dev *dev);
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200146} gfs_configurations[] = {
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200147#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200148 {
149 .eth = rndis_bind_config,
150 },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200151#endif
152
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200153#ifdef CONFIG_USB_FUNCTIONFS_ETH
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200154 {
155 .eth = eth_bind_config,
156 },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200157#endif
158
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200159#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200160 {
161 },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200162#endif
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200163};
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200164
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200165static int gfs_bind(struct usb_composite_dev *cdev);
166static int gfs_unbind(struct usb_composite_dev *cdev);
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200167static int gfs_do_config(struct usb_configuration *c);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200168
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200169static __refdata struct usb_composite_driver gfs_driver = {
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200170 .name = DRIVER_NAME,
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200171 .dev = &gfs_dev_desc,
172 .strings = gfs_dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300173 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200174 .bind = gfs_bind,
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200175 .unbind = gfs_unbind,
176};
177
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200178static DEFINE_MUTEX(gfs_lock);
179static unsigned int missing_funcs;
180static bool gfs_ether_setup;
181static bool gfs_registered;
182static bool gfs_single_func;
183static struct gfs_ffs_obj *ffs_tab;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200184
Andrzej Pietrasiewicz8545e602012-03-12 12:55:42 +0100185static int __init gfs_init(void)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200186{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200187 int i;
188
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200189 ENTER();
190
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200191 if (!func_num) {
192 gfs_single_func = true;
193 func_num = 1;
194 }
195
196 ffs_tab = kcalloc(func_num, sizeof *ffs_tab, GFP_KERNEL);
197 if (!ffs_tab)
198 return -ENOMEM;
199
200 if (!gfs_single_func)
201 for (i = 0; i < func_num; i++)
202 ffs_tab[i].name = func_names[i];
203
204 missing_funcs = func_num;
205
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200206 return functionfs_init();
207}
208module_init(gfs_init);
209
Andrzej Pietrasiewicz8545e602012-03-12 12:55:42 +0100210static void __exit gfs_exit(void)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200211{
212 ENTER();
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200213 mutex_lock(&gfs_lock);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200214
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200215 if (gfs_registered)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200216 usb_composite_unregister(&gfs_driver);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200217 gfs_registered = false;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200218
219 functionfs_cleanup();
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200220
221 mutex_unlock(&gfs_lock);
222 kfree(ffs_tab);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200223}
224module_exit(gfs_exit);
225
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200226static struct gfs_ffs_obj *gfs_find_dev(const char *dev_name)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200227{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200228 int i;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200229
230 ENTER();
231
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200232 if (gfs_single_func)
233 return &ffs_tab[0];
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200234
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200235 for (i = 0; i < func_num; i++)
236 if (strcmp(ffs_tab[i].name, dev_name) == 0)
237 return &ffs_tab[i];
238
239 return NULL;
240}
241
242static int functionfs_ready_callback(struct ffs_data *ffs)
243{
244 struct gfs_ffs_obj *ffs_obj;
245 int ret;
246
247 ENTER();
248 mutex_lock(&gfs_lock);
249
250 ffs_obj = ffs->private_data;
251 if (!ffs_obj) {
252 ret = -EINVAL;
253 goto done;
254 }
255
256 if (WARN_ON(ffs_obj->desc_ready)) {
257 ret = -EBUSY;
258 goto done;
259 }
260 ffs_obj->desc_ready = true;
261 ffs_obj->ffs_data = ffs;
262
263 if (--missing_funcs) {
264 ret = 0;
265 goto done;
266 }
267
268 if (gfs_registered) {
269 ret = -EBUSY;
270 goto done;
271 }
272 gfs_registered = true;
273
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200274 ret = usb_composite_probe(&gfs_driver);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200275 if (unlikely(ret < 0))
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200276 gfs_registered = false;
277
278done:
279 mutex_unlock(&gfs_lock);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200280 return ret;
281}
282
283static void functionfs_closed_callback(struct ffs_data *ffs)
284{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200285 struct gfs_ffs_obj *ffs_obj;
286
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200287 ENTER();
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200288 mutex_lock(&gfs_lock);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200289
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200290 ffs_obj = ffs->private_data;
291 if (!ffs_obj)
292 goto done;
293
294 ffs_obj->desc_ready = false;
295 missing_funcs++;
296
297 if (gfs_registered)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200298 usb_composite_unregister(&gfs_driver);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200299 gfs_registered = false;
300
301done:
302 mutex_unlock(&gfs_lock);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200303}
304
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200305static void *functionfs_acquire_dev_callback(const char *dev_name)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200306{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200307 struct gfs_ffs_obj *ffs_dev;
308
309 ENTER();
310 mutex_lock(&gfs_lock);
311
312 ffs_dev = gfs_find_dev(dev_name);
313 if (!ffs_dev) {
314 ffs_dev = ERR_PTR(-ENODEV);
315 goto done;
316 }
317
318 if (ffs_dev->mounted) {
319 ffs_dev = ERR_PTR(-EBUSY);
320 goto done;
321 }
322 ffs_dev->mounted = true;
323
324done:
325 mutex_unlock(&gfs_lock);
326 return ffs_dev;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200327}
328
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200329static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
330{
331 struct gfs_ffs_obj *ffs_dev;
332
333 ENTER();
334 mutex_lock(&gfs_lock);
335
336 ffs_dev = ffs_data->private_data;
337 if (ffs_dev)
338 ffs_dev->mounted = false;
339
340 mutex_unlock(&gfs_lock);
341}
342
343/*
344 * It is assumed that gfs_bind is called from a context where gfs_lock is held
345 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200346static int gfs_bind(struct usb_composite_dev *cdev)
347{
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200348 int ret, i;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200349
350 ENTER();
351
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200352 if (missing_funcs)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200353 return -ENODEV;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100354#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200355 the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, gfs_host_mac,
356 qmult);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100357#endif
358 if (IS_ERR(the_dev)) {
359 ret = PTR_ERR(the_dev);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200360 goto error_quick;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100361 }
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200362 gfs_ether_setup = true;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200363
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200364 ret = usb_string_ids_tab(cdev, gfs_strings);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200365 if (unlikely(ret < 0))
366 goto error;
Sebastian Andrzej Siewiord33f74f2012-09-10 15:01:57 +0200367 gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200368
Andrzej Pietrasiewicz34169052013-03-11 16:32:14 +0100369 for (i = func_num; i--; ) {
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200370 ret = functionfs_bind(ffs_tab[i].ffs_data, cdev);
371 if (unlikely(ret < 0)) {
372 while (++i < func_num)
373 functionfs_unbind(ffs_tab[i].ffs_data);
374 goto error;
375 }
376 }
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200377
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200378 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
379 struct gfs_configuration *c = gfs_configurations + i;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200380 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200381
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200382 c->c.label = gfs_strings[sid].s;
383 c->c.iConfiguration = gfs_strings[sid].id;
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200384 c->c.bConfigurationValue = 1 + i;
385 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200386
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200387 ret = usb_add_config(cdev, &c->c, gfs_do_config);
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200388 if (unlikely(ret < 0))
389 goto error_unbind;
390 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200391 usb_composite_overwrite_options(cdev, &coverwrite);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200392 return 0;
393
394error_unbind:
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200395 for (i = 0; i < func_num; i++)
396 functionfs_unbind(ffs_tab[i].ffs_data);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200397error:
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100398 gether_cleanup(the_dev);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200399error_quick:
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200400 gfs_ether_setup = false;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200401 return ret;
402}
403
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200404/*
405 * It is assumed that gfs_unbind is called from a context where gfs_lock is held
406 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200407static int gfs_unbind(struct usb_composite_dev *cdev)
408{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200409 int i;
410
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200411 ENTER();
412
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200413 /*
414 * We may have been called in an error recovery from
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200415 * composite_bind() after gfs_unbind() failure so we need to
416 * check if gfs_ffs_data is not NULL since gfs_bind() handles
417 * all error recovery itself. I'd rather we werent called
418 * from composite on orror recovery, but what you're gonna
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200419 * do...?
420 */
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200421 if (gfs_ether_setup)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100422 gether_cleanup(the_dev);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200423 gfs_ether_setup = false;
424
Andrzej Pietrasiewicz34169052013-03-11 16:32:14 +0100425 for (i = func_num; i--; )
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200426 if (ffs_tab[i].ffs_data)
427 functionfs_unbind(ffs_tab[i].ffs_data);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200428
429 return 0;
430}
431
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200432/*
433 * It is assumed that gfs_do_config is called from a context where
434 * gfs_lock is held
435 */
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200436static int gfs_do_config(struct usb_configuration *c)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200437{
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200438 struct gfs_configuration *gc =
439 container_of(c, struct gfs_configuration, c);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200440 int i;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200441 int ret;
442
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200443 if (missing_funcs)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200444 return -ENODEV;
445
446 if (gadget_is_otg(c->cdev->gadget)) {
447 c->descriptors = gfs_otg_desc;
448 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
449 }
450
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200451 if (gc->eth) {
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200452 ret = gc->eth(c, gfs_host_mac, the_dev);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200453 if (unlikely(ret < 0))
454 return ret;
455 }
456
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200457 for (i = 0; i < func_num; i++) {
458 ret = functionfs_bind_config(c->cdev, c, ffs_tab[i].ffs_data);
459 if (unlikely(ret < 0))
460 return ret;
461 }
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200462
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200463 /*
464 * After previous do_configs there may be some invalid
Michal Nazarewiczf588c0d2010-06-14 10:43:34 +0200465 * pointers in c->interface array. This happens every time
466 * a user space function with fewer interfaces than a user
467 * space function that was run before the new one is run. The
468 * compasit's set_config() assumes that if there is no more
469 * then MAX_CONFIG_INTERFACES interfaces in a configuration
470 * then there is a NULL pointer after the last interface in
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200471 * c->interface array. We need to make sure this is true.
472 */
Michal Nazarewiczf588c0d2010-06-14 10:43:34 +0200473 if (c->next_interface_id < ARRAY_SIZE(c->interface))
474 c->interface[c->next_interface_id] = NULL;
475
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200476 return 0;
477}
478
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200479#ifdef CONFIG_USB_FUNCTIONFS_ETH
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200480
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100481static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
482 struct eth_dev *dev)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200483{
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200484 return can_support_ecm(c->cdev->gadget)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100485 ? ecm_bind_config(c, ethaddr, dev)
486 : geth_bind_config(c, ethaddr, dev);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200487}
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200488
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200489#endif