blob: d38a073efd5d2ea43ff16f2d6387b6eb82602961 [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
Andrzej Pietrasiewiczfee562a2013-05-23 10:32:03 +020031#define USBF_ECM_INCLUDED
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020032# include "f_ecm.c"
33# include "f_subset.c"
34# ifdef USB_ETH_RNDIS
35# include "f_rndis.c"
Andrzej Pietrasiewiczcbbd14a2013-05-24 10:23:02 +020036# include "rndis.h"
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020037# endif
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020038# include "u_ether.h"
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020039
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020040static u8 gfs_host_mac[ETH_ALEN];
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +010041static struct eth_dev *the_dev;
Michal Nazarewiczf8dae532010-06-25 16:29:27 +020042# ifdef CONFIG_USB_FUNCTIONFS_ETH
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +010043static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
44 struct eth_dev *dev);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020045# endif
Michal Nazarewiczf8dae532010-06-25 16:29:27 +020046#else
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +010047# define the_dev NULL
48# define gether_cleanup(dev) do { } while (0)
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020049# define gfs_host_mac NULL
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +010050struct eth_dev;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020051#endif
52
53#include "f_fs.c"
54
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020055#define DRIVER_NAME "g_ffs"
56#define DRIVER_DESC "USB Function Filesystem"
57#define DRIVER_VERSION "24 Aug 2004"
58
59MODULE_DESCRIPTION(DRIVER_DESC);
60MODULE_AUTHOR("Michal Nazarewicz");
61MODULE_LICENSE("GPL");
62
Michal Nazarewiczfc19de62010-08-12 17:43:48 +020063#define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
64#define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020065
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +020066#define GFS_MAX_DEVS 10
67
68struct gfs_ffs_obj {
69 const char *name;
70 bool mounted;
71 bool desc_ready;
72 struct ffs_data *ffs_data;
73};
74
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020075USB_GADGET_COMPOSITE_OPTIONS();
76
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +020077USB_ETHERNET_MODULE_PARAMETERS();
78
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020079static struct usb_device_descriptor gfs_dev_desc = {
80 .bLength = sizeof gfs_dev_desc,
81 .bDescriptorType = USB_DT_DEVICE,
82
83 .bcdUSB = cpu_to_le16(0x0200),
84 .bDeviceClass = USB_CLASS_PER_INTERFACE,
85
Michal Nazarewiczfc19de62010-08-12 17:43:48 +020086 .idVendor = cpu_to_le16(GFS_VENDOR_ID),
87 .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
Michal Nazarewiczc6c56002010-05-05 12:53:15 +020088};
89
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +020090static char *func_names[GFS_MAX_DEVS];
91static unsigned int func_num;
92
Michal Nazarewiczfc19de62010-08-12 17:43:48 +020093module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
94MODULE_PARM_DESC(bDeviceClass, "USB Device class");
95module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
96MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
97module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
98MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +020099module_param_array_named(functions, func_names, charp, &func_num, 0);
100MODULE_PARM_DESC(functions, "USB Functions list");
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200101
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200102static const struct usb_descriptor_header *gfs_otg_desc[] = {
103 (const struct usb_descriptor_header *)
104 &(const struct usb_otg_descriptor) {
105 .bLength = sizeof(struct usb_otg_descriptor),
106 .bDescriptorType = USB_DT_OTG,
107
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200108 /*
109 * REVISIT SRP-only hardware is possible, although
110 * it would not be called "OTG" ...
111 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200112 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
113 },
114
115 NULL
116};
117
Michal Nazarewicz5ab54cf2010-11-12 14:29:28 +0100118/* String IDs are assigned dynamically */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200119static struct usb_string gfs_strings[] = {
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200120 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewiord33f74f2012-09-10 15:01:57 +0200121 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200122 [USB_GADGET_SERIAL_IDX].s = "",
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200123#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200124 { .s = "FunctionFS + RNDIS" },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200125#endif
126#ifdef CONFIG_USB_FUNCTIONFS_ETH
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200127 { .s = "FunctionFS + ECM" },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200128#endif
129#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200130 { .s = "FunctionFS" },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200131#endif
132 { } /* end of list */
133};
134
135static struct usb_gadget_strings *gfs_dev_strings[] = {
136 &(struct usb_gadget_strings) {
137 .language = 0x0409, /* en-us */
138 .strings = gfs_strings,
139 },
140 NULL,
141};
142
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200143struct gfs_configuration {
144 struct usb_configuration c;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100145 int (*eth)(struct usb_configuration *c, u8 *ethaddr,
146 struct eth_dev *dev);
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200147} gfs_configurations[] = {
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200148#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200149 {
150 .eth = rndis_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_ETH
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200155 {
156 .eth = eth_bind_config,
157 },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200158#endif
159
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200160#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200161 {
162 },
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200163#endif
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200164};
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200165
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200166static int gfs_bind(struct usb_composite_dev *cdev);
167static int gfs_unbind(struct usb_composite_dev *cdev);
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200168static int gfs_do_config(struct usb_configuration *c);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200169
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200170static __refdata struct usb_composite_driver gfs_driver = {
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200171 .name = DRIVER_NAME,
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200172 .dev = &gfs_dev_desc,
173 .strings = gfs_dev_strings,
Tatyana Brokhman35a0e0b2011-06-29 16:41:49 +0300174 .max_speed = USB_SPEED_HIGH,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200175 .bind = gfs_bind,
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200176 .unbind = gfs_unbind,
177};
178
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200179static DEFINE_MUTEX(gfs_lock);
180static unsigned int missing_funcs;
181static bool gfs_ether_setup;
182static bool gfs_registered;
183static bool gfs_single_func;
184static struct gfs_ffs_obj *ffs_tab;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200185
Andrzej Pietrasiewicz8545e602012-03-12 12:55:42 +0100186static int __init gfs_init(void)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200187{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200188 int i;
189
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200190 ENTER();
191
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200192 if (!func_num) {
193 gfs_single_func = true;
194 func_num = 1;
195 }
196
197 ffs_tab = kcalloc(func_num, sizeof *ffs_tab, GFP_KERNEL);
198 if (!ffs_tab)
199 return -ENOMEM;
200
201 if (!gfs_single_func)
202 for (i = 0; i < func_num; i++)
203 ffs_tab[i].name = func_names[i];
204
205 missing_funcs = func_num;
206
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200207 return functionfs_init();
208}
209module_init(gfs_init);
210
Andrzej Pietrasiewicz8545e602012-03-12 12:55:42 +0100211static void __exit gfs_exit(void)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200212{
213 ENTER();
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200214 mutex_lock(&gfs_lock);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200215
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200216 if (gfs_registered)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200217 usb_composite_unregister(&gfs_driver);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200218 gfs_registered = false;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200219
220 functionfs_cleanup();
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200221
222 mutex_unlock(&gfs_lock);
223 kfree(ffs_tab);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200224}
225module_exit(gfs_exit);
226
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200227static struct gfs_ffs_obj *gfs_find_dev(const char *dev_name)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200228{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200229 int i;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200230
231 ENTER();
232
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200233 if (gfs_single_func)
234 return &ffs_tab[0];
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200235
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200236 for (i = 0; i < func_num; i++)
237 if (strcmp(ffs_tab[i].name, dev_name) == 0)
238 return &ffs_tab[i];
239
240 return NULL;
241}
242
243static int functionfs_ready_callback(struct ffs_data *ffs)
244{
245 struct gfs_ffs_obj *ffs_obj;
246 int ret;
247
248 ENTER();
249 mutex_lock(&gfs_lock);
250
251 ffs_obj = ffs->private_data;
252 if (!ffs_obj) {
253 ret = -EINVAL;
254 goto done;
255 }
256
257 if (WARN_ON(ffs_obj->desc_ready)) {
258 ret = -EBUSY;
259 goto done;
260 }
261 ffs_obj->desc_ready = true;
262 ffs_obj->ffs_data = ffs;
263
264 if (--missing_funcs) {
265 ret = 0;
266 goto done;
267 }
268
269 if (gfs_registered) {
270 ret = -EBUSY;
271 goto done;
272 }
273 gfs_registered = true;
274
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200275 ret = usb_composite_probe(&gfs_driver);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200276 if (unlikely(ret < 0))
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200277 gfs_registered = false;
278
279done:
280 mutex_unlock(&gfs_lock);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200281 return ret;
282}
283
284static void functionfs_closed_callback(struct ffs_data *ffs)
285{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200286 struct gfs_ffs_obj *ffs_obj;
287
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200288 ENTER();
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200289 mutex_lock(&gfs_lock);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200290
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200291 ffs_obj = ffs->private_data;
292 if (!ffs_obj)
293 goto done;
294
295 ffs_obj->desc_ready = false;
296 missing_funcs++;
297
298 if (gfs_registered)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200299 usb_composite_unregister(&gfs_driver);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200300 gfs_registered = false;
301
302done:
303 mutex_unlock(&gfs_lock);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200304}
305
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200306static void *functionfs_acquire_dev_callback(const char *dev_name)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200307{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200308 struct gfs_ffs_obj *ffs_dev;
309
310 ENTER();
311 mutex_lock(&gfs_lock);
312
313 ffs_dev = gfs_find_dev(dev_name);
314 if (!ffs_dev) {
315 ffs_dev = ERR_PTR(-ENODEV);
316 goto done;
317 }
318
319 if (ffs_dev->mounted) {
320 ffs_dev = ERR_PTR(-EBUSY);
321 goto done;
322 }
323 ffs_dev->mounted = true;
324
325done:
326 mutex_unlock(&gfs_lock);
327 return ffs_dev;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200328}
329
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200330static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
331{
332 struct gfs_ffs_obj *ffs_dev;
333
334 ENTER();
335 mutex_lock(&gfs_lock);
336
337 ffs_dev = ffs_data->private_data;
338 if (ffs_dev)
339 ffs_dev->mounted = false;
340
341 mutex_unlock(&gfs_lock);
342}
343
344/*
345 * It is assumed that gfs_bind is called from a context where gfs_lock is held
346 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200347static int gfs_bind(struct usb_composite_dev *cdev)
348{
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200349 int ret, i;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200350
351 ENTER();
352
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200353 if (missing_funcs)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200354 return -ENODEV;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100355#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200356 the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, gfs_host_mac,
357 qmult);
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100358#endif
359 if (IS_ERR(the_dev)) {
360 ret = PTR_ERR(the_dev);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200361 goto error_quick;
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100362 }
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200363 gfs_ether_setup = true;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200364
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200365 ret = usb_string_ids_tab(cdev, gfs_strings);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200366 if (unlikely(ret < 0))
367 goto error;
Sebastian Andrzej Siewiord33f74f2012-09-10 15:01:57 +0200368 gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200369
Andrzej Pietrasiewicz34169052013-03-11 16:32:14 +0100370 for (i = func_num; i--; ) {
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200371 ret = functionfs_bind(ffs_tab[i].ffs_data, cdev);
372 if (unlikely(ret < 0)) {
373 while (++i < func_num)
374 functionfs_unbind(ffs_tab[i].ffs_data);
375 goto error;
376 }
377 }
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200378
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200379 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
380 struct gfs_configuration *c = gfs_configurations + i;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200381 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200382
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200383 c->c.label = gfs_strings[sid].s;
384 c->c.iConfiguration = gfs_strings[sid].id;
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200385 c->c.bConfigurationValue = 1 + i;
386 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200387
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200388 ret = usb_add_config(cdev, &c->c, gfs_do_config);
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200389 if (unlikely(ret < 0))
390 goto error_unbind;
391 }
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200392 usb_composite_overwrite_options(cdev, &coverwrite);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200393 return 0;
394
395error_unbind:
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200396 for (i = 0; i < func_num; i++)
397 functionfs_unbind(ffs_tab[i].ffs_data);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200398error:
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100399 gether_cleanup(the_dev);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200400error_quick:
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200401 gfs_ether_setup = false;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200402 return ret;
403}
404
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200405/*
406 * It is assumed that gfs_unbind is called from a context where gfs_lock is held
407 */
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200408static int gfs_unbind(struct usb_composite_dev *cdev)
409{
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200410 int i;
411
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200412 ENTER();
413
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200414 /*
415 * We may have been called in an error recovery from
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200416 * composite_bind() after gfs_unbind() failure so we need to
417 * check if gfs_ffs_data is not NULL since gfs_bind() handles
418 * all error recovery itself. I'd rather we werent called
419 * from composite on orror recovery, but what you're gonna
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200420 * do...?
421 */
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200422 if (gfs_ether_setup)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100423 gether_cleanup(the_dev);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200424 gfs_ether_setup = false;
425
Andrzej Pietrasiewicz34169052013-03-11 16:32:14 +0100426 for (i = func_num; i--; )
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200427 if (ffs_tab[i].ffs_data)
428 functionfs_unbind(ffs_tab[i].ffs_data);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200429
430 return 0;
431}
432
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200433/*
434 * It is assumed that gfs_do_config is called from a context where
435 * gfs_lock is held
436 */
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200437static int gfs_do_config(struct usb_configuration *c)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200438{
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200439 struct gfs_configuration *gc =
440 container_of(c, struct gfs_configuration, c);
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200441 int i;
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200442 int ret;
443
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200444 if (missing_funcs)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200445 return -ENODEV;
446
447 if (gadget_is_otg(c->cdev->gadget)) {
448 c->descriptors = gfs_otg_desc;
449 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
450 }
451
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200452 if (gc->eth) {
Andrzej Pietrasiewiczf1a18232013-05-23 09:22:03 +0200453 ret = gc->eth(c, gfs_host_mac, the_dev);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200454 if (unlikely(ret < 0))
455 return ret;
456 }
457
Andrzej Pietrasiewicz581791f2012-05-14 15:51:52 +0200458 for (i = 0; i < func_num; i++) {
459 ret = functionfs_bind_config(c->cdev, c, ffs_tab[i].ffs_data);
460 if (unlikely(ret < 0))
461 return ret;
462 }
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200463
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200464 /*
465 * After previous do_configs there may be some invalid
Michal Nazarewiczf588c0d2010-06-14 10:43:34 +0200466 * pointers in c->interface array. This happens every time
467 * a user space function with fewer interfaces than a user
468 * space function that was run before the new one is run. The
469 * compasit's set_config() assumes that if there is no more
470 * then MAX_CONFIG_INTERFACES interfaces in a configuration
471 * then there is a NULL pointer after the last interface in
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200472 * c->interface array. We need to make sure this is true.
473 */
Michal Nazarewiczf588c0d2010-06-14 10:43:34 +0200474 if (c->next_interface_id < ARRAY_SIZE(c->interface))
475 c->interface[c->next_interface_id] = NULL;
476
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200477 return 0;
478}
479
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200480#ifdef CONFIG_USB_FUNCTIONFS_ETH
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200481
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100482static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
483 struct eth_dev *dev)
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200484{
Michal Nazarewiczf8dae532010-06-25 16:29:27 +0200485 return can_support_ecm(c->cdev->gadget)
Sebastian Andrzej Siewiord6a01432012-12-23 21:10:12 +0100486 ? ecm_bind_config(c, ethaddr, dev)
487 : geth_bind_config(c, ethaddr, dev);
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200488}
Michal Nazarewiczfc19de62010-08-12 17:43:48 +0200489
Michal Nazarewiczc6c56002010-05-05 12:53:15 +0200490#endif