usb: gadget: move usb_gadget_controller_number() into a .c file and libcomposite
[deliverable/linux.git] / drivers / usb / gadget / g_ffs.c
CommitLineData
5ab54cf7
MN
1/*
2 * g_ffs.c -- user mode file system API for USB composite function controllers
3 *
4 * Copyright (C) 2010 Samsung Electronics
54b8360f 5 * Author: Michal Nazarewicz <mina86@mina86.com>
5ab54cf7
MN
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
5ab54cf7
MN
11 */
12
aa02f172
MN
13#define pr_fmt(fmt) "g_ffs: " fmt
14
c6c56008
MN
15#include <linux/module.h>
16#include <linux/utsname.h>
17
c6c56008
MN
18/*
19 * kbuild is not very cooperative with respect to linking separately
20 * compiled library objects into one module. So for now we won't use
21 * separate compilation ... ensuring init/exit sections work to shrink
22 * the runtime footprint, and giving us at least some parts of what
23 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
24 */
25
26#include "composite.c"
c6c56008
MN
27
28#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
29# if defined USB_ETH_RNDIS
30# undef USB_ETH_RNDIS
31# endif
32# ifdef CONFIG_USB_FUNCTIONFS_RNDIS
33# define USB_ETH_RNDIS y
34# endif
35
36# include "f_ecm.c"
37# include "f_subset.c"
38# ifdef USB_ETH_RNDIS
39# include "f_rndis.c"
40# include "rndis.c"
41# endif
42# include "u_ether.c"
43
44static u8 gfs_hostaddr[ETH_ALEN];
f8dae531
MN
45# ifdef CONFIG_USB_FUNCTIONFS_ETH
46static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
c6c56008 47# endif
f8dae531 48#else
c6c56008
MN
49# define gether_cleanup() do { } while (0)
50# define gether_setup(gadget, hostaddr) ((int)0)
f8dae531 51# define gfs_hostaddr NULL
c6c56008
MN
52#endif
53
54#include "f_fs.c"
55
c6c56008
MN
56#define DRIVER_NAME "g_ffs"
57#define DRIVER_DESC "USB Function Filesystem"
58#define DRIVER_VERSION "24 Aug 2004"
59
60MODULE_DESCRIPTION(DRIVER_DESC);
61MODULE_AUTHOR("Michal Nazarewicz");
62MODULE_LICENSE("GPL");
63
fc19de61
MN
64#define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
65#define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
c6c56008 66
581791f5
AP
67#define GFS_MAX_DEVS 10
68
69struct gfs_ffs_obj {
70 const char *name;
71 bool mounted;
72 bool desc_ready;
73 struct ffs_data *ffs_data;
74};
75
c6c56008
MN
76static struct usb_device_descriptor gfs_dev_desc = {
77 .bLength = sizeof gfs_dev_desc,
78 .bDescriptorType = USB_DT_DEVICE,
79
80 .bcdUSB = cpu_to_le16(0x0200),
81 .bDeviceClass = USB_CLASS_PER_INTERFACE,
82
fc19de61
MN
83 .idVendor = cpu_to_le16(GFS_VENDOR_ID),
84 .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
c6c56008
MN
85};
86
581791f5
AP
87static char *func_names[GFS_MAX_DEVS];
88static unsigned int func_num;
89
fc19de61
MN
90module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
91MODULE_PARM_DESC(bDeviceClass, "USB Device class");
92module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
93MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
94module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
95MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
581791f5
AP
96module_param_array_named(functions, func_names, charp, &func_num, 0);
97MODULE_PARM_DESC(functions, "USB Functions list");
c6c56008 98
c6c56008
MN
99static const struct usb_descriptor_header *gfs_otg_desc[] = {
100 (const struct usb_descriptor_header *)
101 &(const struct usb_otg_descriptor) {
102 .bLength = sizeof(struct usb_otg_descriptor),
103 .bDescriptorType = USB_DT_OTG,
104
fc19de61
MN
105 /*
106 * REVISIT SRP-only hardware is possible, although
107 * it would not be called "OTG" ...
108 */
c6c56008
MN
109 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
110 },
111
112 NULL
113};
114
5ab54cf7 115/* String IDs are assigned dynamically */
c6c56008 116static struct usb_string gfs_strings[] = {
c6c56008 117#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
f8dae531 118 { .s = "FunctionFS + RNDIS" },
c6c56008
MN
119#endif
120#ifdef CONFIG_USB_FUNCTIONFS_ETH
f8dae531 121 { .s = "FunctionFS + ECM" },
c6c56008
MN
122#endif
123#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
f8dae531 124 { .s = "FunctionFS" },
c6c56008
MN
125#endif
126 { } /* end of list */
127};
128
129static struct usb_gadget_strings *gfs_dev_strings[] = {
130 &(struct usb_gadget_strings) {
131 .language = 0x0409, /* en-us */
132 .strings = gfs_strings,
133 },
134 NULL,
135};
136
f8dae531
MN
137struct gfs_configuration {
138 struct usb_configuration c;
139 int (*eth)(struct usb_configuration *c, u8 *ethaddr);
140} gfs_configurations[] = {
c6c56008 141#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
f8dae531
MN
142 {
143 .eth = rndis_bind_config,
144 },
c6c56008
MN
145#endif
146
c6c56008 147#ifdef CONFIG_USB_FUNCTIONFS_ETH
f8dae531
MN
148 {
149 .eth = eth_bind_config,
150 },
c6c56008
MN
151#endif
152
c6c56008 153#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
f8dae531
MN
154 {
155 },
c6c56008 156#endif
f8dae531 157};
c6c56008 158
c6c56008
MN
159static int gfs_bind(struct usb_composite_dev *cdev);
160static int gfs_unbind(struct usb_composite_dev *cdev);
f8dae531 161static int gfs_do_config(struct usb_configuration *c);
c6c56008 162
c2ec75c2 163static __refdata struct usb_composite_driver gfs_driver = {
fc19de61 164 .name = DRIVER_NAME,
c6c56008
MN
165 .dev = &gfs_dev_desc,
166 .strings = gfs_dev_strings,
35a0e0bf 167 .max_speed = USB_SPEED_HIGH,
03e42bd5 168 .bind = gfs_bind,
c6c56008 169 .unbind = gfs_unbind,
fc19de61 170 .iProduct = DRIVER_DESC,
c6c56008
MN
171};
172
581791f5
AP
173static DEFINE_MUTEX(gfs_lock);
174static unsigned int missing_funcs;
175static bool gfs_ether_setup;
176static bool gfs_registered;
177static bool gfs_single_func;
178static struct gfs_ffs_obj *ffs_tab;
c6c56008 179
8545e603 180static int __init gfs_init(void)
c6c56008 181{
581791f5
AP
182 int i;
183
c6c56008
MN
184 ENTER();
185
581791f5
AP
186 if (!func_num) {
187 gfs_single_func = true;
188 func_num = 1;
189 }
190
191 ffs_tab = kcalloc(func_num, sizeof *ffs_tab, GFP_KERNEL);
192 if (!ffs_tab)
193 return -ENOMEM;
194
195 if (!gfs_single_func)
196 for (i = 0; i < func_num; i++)
197 ffs_tab[i].name = func_names[i];
198
199 missing_funcs = func_num;
200
c6c56008
MN
201 return functionfs_init();
202}
203module_init(gfs_init);
204
8545e603 205static void __exit gfs_exit(void)
c6c56008
MN
206{
207 ENTER();
581791f5 208 mutex_lock(&gfs_lock);
c6c56008 209
581791f5 210 if (gfs_registered)
c6c56008 211 usb_composite_unregister(&gfs_driver);
581791f5 212 gfs_registered = false;
c6c56008
MN
213
214 functionfs_cleanup();
581791f5
AP
215
216 mutex_unlock(&gfs_lock);
217 kfree(ffs_tab);
c6c56008
MN
218}
219module_exit(gfs_exit);
220
581791f5
AP
221static struct gfs_ffs_obj *gfs_find_dev(const char *dev_name)
222{
223 int i;
224
225 ENTER();
226
227 if (gfs_single_func)
228 return &ffs_tab[0];
229
230 for (i = 0; i < func_num; i++)
231 if (strcmp(ffs_tab[i].name, dev_name) == 0)
232 return &ffs_tab[i];
233
234 return NULL;
235}
236
c6c56008
MN
237static int functionfs_ready_callback(struct ffs_data *ffs)
238{
581791f5 239 struct gfs_ffs_obj *ffs_obj;
c6c56008
MN
240 int ret;
241
242 ENTER();
581791f5
AP
243 mutex_lock(&gfs_lock);
244
245 ffs_obj = ffs->private_data;
246 if (!ffs_obj) {
247 ret = -EINVAL;
248 goto done;
249 }
250
251 if (WARN_ON(ffs_obj->desc_ready)) {
252 ret = -EBUSY;
253 goto done;
254 }
255 ffs_obj->desc_ready = true;
256 ffs_obj->ffs_data = ffs;
c6c56008 257
581791f5
AP
258 if (--missing_funcs) {
259 ret = 0;
260 goto done;
261 }
262
263 if (gfs_registered) {
264 ret = -EBUSY;
265 goto done;
266 }
267 gfs_registered = true;
c6c56008 268
03e42bd5 269 ret = usb_composite_probe(&gfs_driver);
c6c56008 270 if (unlikely(ret < 0))
581791f5
AP
271 gfs_registered = false;
272
273done:
274 mutex_unlock(&gfs_lock);
c6c56008
MN
275 return ret;
276}
277
278static void functionfs_closed_callback(struct ffs_data *ffs)
279{
581791f5
AP
280 struct gfs_ffs_obj *ffs_obj;
281
c6c56008 282 ENTER();
581791f5 283 mutex_lock(&gfs_lock);
c6c56008 284
581791f5
AP
285 ffs_obj = ffs->private_data;
286 if (!ffs_obj)
287 goto done;
288
289 ffs_obj->desc_ready = false;
290 missing_funcs++;
291
292 if (gfs_registered)
c6c56008 293 usb_composite_unregister(&gfs_driver);
581791f5
AP
294 gfs_registered = false;
295
296done:
297 mutex_unlock(&gfs_lock);
c6c56008
MN
298}
299
581791f5 300static void *functionfs_acquire_dev_callback(const char *dev_name)
c6c56008 301{
581791f5
AP
302 struct gfs_ffs_obj *ffs_dev;
303
304 ENTER();
305 mutex_lock(&gfs_lock);
306
307 ffs_dev = gfs_find_dev(dev_name);
308 if (!ffs_dev) {
309 ffs_dev = ERR_PTR(-ENODEV);
310 goto done;
311 }
312
313 if (ffs_dev->mounted) {
314 ffs_dev = ERR_PTR(-EBUSY);
315 goto done;
316 }
317 ffs_dev->mounted = true;
318
319done:
320 mutex_unlock(&gfs_lock);
321 return ffs_dev;
c6c56008
MN
322}
323
581791f5
AP
324static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
325{
326 struct gfs_ffs_obj *ffs_dev;
327
328 ENTER();
329 mutex_lock(&gfs_lock);
330
331 ffs_dev = ffs_data->private_data;
332 if (ffs_dev)
333 ffs_dev->mounted = false;
334
335 mutex_unlock(&gfs_lock);
336}
337
338/*
339 * It is assumed that gfs_bind is called from a context where gfs_lock is held
340 */
c6c56008
MN
341static int gfs_bind(struct usb_composite_dev *cdev)
342{
f8dae531 343 int ret, i;
c6c56008
MN
344
345 ENTER();
346
581791f5 347 if (missing_funcs)
c6c56008
MN
348 return -ENODEV;
349
350 ret = gether_setup(cdev->gadget, gfs_hostaddr);
351 if (unlikely(ret < 0))
352 goto error_quick;
581791f5 353 gfs_ether_setup = true;
c6c56008 354
f8dae531 355 ret = usb_string_ids_tab(cdev, gfs_strings);
c6c56008
MN
356 if (unlikely(ret < 0))
357 goto error;
c6c56008 358
581791f5
AP
359 for (i = func_num; --i; ) {
360 ret = functionfs_bind(ffs_tab[i].ffs_data, cdev);
361 if (unlikely(ret < 0)) {
362 while (++i < func_num)
363 functionfs_unbind(ffs_tab[i].ffs_data);
364 goto error;
365 }
366 }
c6c56008 367
f8dae531
MN
368 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
369 struct gfs_configuration *c = gfs_configurations + i;
c6c56008 370
fc19de61
MN
371 c->c.label = gfs_strings[i].s;
372 c->c.iConfiguration = gfs_strings[i].id;
f8dae531
MN
373 c->c.bConfigurationValue = 1 + i;
374 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
c6c56008 375
c9bfff9c 376 ret = usb_add_config(cdev, &c->c, gfs_do_config);
f8dae531
MN
377 if (unlikely(ret < 0))
378 goto error_unbind;
379 }
c6c56008
MN
380
381 return 0;
382
383error_unbind:
581791f5
AP
384 for (i = 0; i < func_num; i++)
385 functionfs_unbind(ffs_tab[i].ffs_data);
c6c56008
MN
386error:
387 gether_cleanup();
388error_quick:
581791f5 389 gfs_ether_setup = false;
c6c56008
MN
390 return ret;
391}
392
581791f5
AP
393/*
394 * It is assumed that gfs_unbind is called from a context where gfs_lock is held
395 */
c6c56008
MN
396static int gfs_unbind(struct usb_composite_dev *cdev)
397{
581791f5
AP
398 int i;
399
c6c56008
MN
400 ENTER();
401
fc19de61
MN
402 /*
403 * We may have been called in an error recovery from
c6c56008
MN
404 * composite_bind() after gfs_unbind() failure so we need to
405 * check if gfs_ffs_data is not NULL since gfs_bind() handles
406 * all error recovery itself. I'd rather we werent called
407 * from composite on orror recovery, but what you're gonna
fc19de61
MN
408 * do...?
409 */
581791f5 410 if (gfs_ether_setup)
c6c56008 411 gether_cleanup();
581791f5
AP
412 gfs_ether_setup = false;
413
414 for (i = func_num; --i; )
415 if (ffs_tab[i].ffs_data)
416 functionfs_unbind(ffs_tab[i].ffs_data);
c6c56008
MN
417
418 return 0;
419}
420
581791f5
AP
421/*
422 * It is assumed that gfs_do_config is called from a context where
423 * gfs_lock is held
424 */
f8dae531 425static int gfs_do_config(struct usb_configuration *c)
c6c56008 426{
f8dae531
MN
427 struct gfs_configuration *gc =
428 container_of(c, struct gfs_configuration, c);
581791f5 429 int i;
c6c56008
MN
430 int ret;
431
581791f5 432 if (missing_funcs)
c6c56008
MN
433 return -ENODEV;
434
435 if (gadget_is_otg(c->cdev->gadget)) {
436 c->descriptors = gfs_otg_desc;
437 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
438 }
439
f8dae531
MN
440 if (gc->eth) {
441 ret = gc->eth(c, gfs_hostaddr);
c6c56008
MN
442 if (unlikely(ret < 0))
443 return ret;
444 }
445
581791f5
AP
446 for (i = 0; i < func_num; i++) {
447 ret = functionfs_bind_config(c->cdev, c, ffs_tab[i].ffs_data);
448 if (unlikely(ret < 0))
449 return ret;
450 }
c6c56008 451
fc19de61
MN
452 /*
453 * After previous do_configs there may be some invalid
f588c0db
MN
454 * pointers in c->interface array. This happens every time
455 * a user space function with fewer interfaces than a user
456 * space function that was run before the new one is run. The
457 * compasit's set_config() assumes that if there is no more
458 * then MAX_CONFIG_INTERFACES interfaces in a configuration
459 * then there is a NULL pointer after the last interface in
fc19de61
MN
460 * c->interface array. We need to make sure this is true.
461 */
f588c0db
MN
462 if (c->next_interface_id < ARRAY_SIZE(c->interface))
463 c->interface[c->next_interface_id] = NULL;
464
c6c56008
MN
465 return 0;
466}
467
c6c56008 468#ifdef CONFIG_USB_FUNCTIONFS_ETH
fc19de61 469
f8dae531 470static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
c6c56008 471{
f8dae531
MN
472 return can_support_ecm(c->cdev->gadget)
473 ? ecm_bind_config(c, ethaddr)
474 : geth_bind_config(c, ethaddr);
c6c56008 475}
fc19de61 476
c6c56008 477#endif
This page took 0.199461 seconds and 5 git commands to generate.