6576186f211a71a0ae24e658f1b73567fd9ad1fe
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / usb / gadget / g_ffs.c
1 /*
2  * g_ffs.c -- user mode file system API for USB composite function controllers
3  *
4  * Copyright (C) 2010 Samsung Electronics
5  * Author: Michal Nazarewicz <mina86@mina86.com>
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.
11  */
12
13 #define pr_fmt(fmt) "g_ffs: " fmt
14
15 #include <linux/module.h>
16 /*
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  */
23 #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"
35 #    include "rndis.c"
36 #  endif
37 #  include "u_ether.c"
38
39 static u8 gfs_hostaddr[ETH_ALEN];
40 static struct eth_dev *the_dev;
41 #  ifdef CONFIG_USB_FUNCTIONFS_ETH
42 static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
43                 struct eth_dev *dev);
44 #  endif
45 #else
46 #  define the_dev       NULL
47 #  define gether_cleanup(dev) do { } while (0)
48 #  define gfs_hostaddr NULL
49 struct eth_dev;
50 #endif
51
52 #include "f_fs.c"
53
54 #define DRIVER_NAME     "g_ffs"
55 #define DRIVER_DESC     "USB Function Filesystem"
56 #define DRIVER_VERSION  "24 Aug 2004"
57
58 MODULE_DESCRIPTION(DRIVER_DESC);
59 MODULE_AUTHOR("Michal Nazarewicz");
60 MODULE_LICENSE("GPL");
61
62 #define GFS_VENDOR_ID   0x1d6b  /* Linux Foundation */
63 #define GFS_PRODUCT_ID  0x0105  /* FunctionFS Gadget */
64
65 #define GFS_MAX_DEVS    10
66
67 struct gfs_ffs_obj {
68         const char *name;
69         bool mounted;
70         bool desc_ready;
71         struct ffs_data *ffs_data;
72 };
73
74 USB_GADGET_COMPOSITE_OPTIONS();
75
76 static 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
83         .idVendor               = cpu_to_le16(GFS_VENDOR_ID),
84         .idProduct              = cpu_to_le16(GFS_PRODUCT_ID),
85 };
86
87 static char *func_names[GFS_MAX_DEVS];
88 static unsigned int func_num;
89
90 module_param_named(bDeviceClass,    gfs_dev_desc.bDeviceClass,    byte,   0644);
91 MODULE_PARM_DESC(bDeviceClass, "USB Device class");
92 module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte,   0644);
93 MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
94 module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte,   0644);
95 MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
96 module_param_array_named(functions, func_names, charp, &func_num, 0);
97 MODULE_PARM_DESC(functions, "USB Functions list");
98
99 static 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
105                 /*
106                  * REVISIT SRP-only hardware is possible, although
107                  * it would not be called "OTG" ...
108                  */
109                 .bmAttributes           = USB_OTG_SRP | USB_OTG_HNP,
110         },
111
112         NULL
113 };
114
115 /* String IDs are assigned dynamically */
116 static struct usb_string gfs_strings[] = {
117         [USB_GADGET_MANUFACTURER_IDX].s = "",
118         [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
119         [USB_GADGET_SERIAL_IDX].s = "",
120 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
121         { .s = "FunctionFS + RNDIS" },
122 #endif
123 #ifdef CONFIG_USB_FUNCTIONFS_ETH
124         { .s = "FunctionFS + ECM" },
125 #endif
126 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
127         { }, /* set dynamically based on module param */
128 #endif
129         {  } /* end of list */
130 };
131
132 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
133 static char *generic_ffs_config_str = "FunctionFS";
134 module_param_named(generic_ffs_config_str,
135                    generic_ffs_config_str,
136                    charp, S_IRUGO);
137 MODULE_PARM_DESC(generic_ffs_config_str,
138                  "Configuration string of generic FFS config");
139 #endif
140 static struct usb_gadget_strings *gfs_dev_strings[] = {
141         &(struct usb_gadget_strings) {
142                 .language       = 0x0409,       /* en-us */
143                 .strings        = gfs_strings,
144         },
145         NULL,
146 };
147
148 struct gfs_configuration {
149         struct usb_configuration c;
150         int (*eth)(struct usb_configuration *c, u8 *ethaddr,
151                         struct eth_dev *dev);
152 } gfs_configurations[] = {
153 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
154         {
155                 .eth            = rndis_bind_config,
156         },
157 #endif
158
159 #ifdef CONFIG_USB_FUNCTIONFS_ETH
160         {
161                 .eth            = eth_bind_config,
162         },
163 #endif
164
165 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
166         {
167         },
168 #endif
169 };
170
171 static int gfs_bind(struct usb_composite_dev *cdev);
172 static int gfs_unbind(struct usb_composite_dev *cdev);
173 static int gfs_do_config(struct usb_configuration *c);
174
175 static __refdata struct usb_composite_driver gfs_driver = {
176         .name           = DRIVER_NAME,
177         .dev            = &gfs_dev_desc,
178         .strings        = gfs_dev_strings,
179         .max_speed      = USB_SPEED_HIGH,
180         .bind           = gfs_bind,
181         .unbind         = gfs_unbind,
182 };
183
184 static DEFINE_MUTEX(gfs_lock);
185 static unsigned int missing_funcs;
186 static bool gfs_ether_setup;
187 static bool gfs_registered;
188 static bool gfs_single_func;
189 static struct gfs_ffs_obj *ffs_tab;
190
191 static int __init gfs_init(void)
192 {
193         int i;
194
195         ENTER();
196
197 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
198         /* Find first (but not last) config with a NULL str */
199         for (i = 0; i < ARRAY_SIZE(gfs_strings) - 1 ; ++i)
200                 if (!gfs_strings[i].s) {
201                         gfs_strings[i].s = generic_ffs_config_str;
202                         break;
203                 }
204 #endif
205         if (!func_num) {
206                 gfs_single_func = true;
207                 func_num = 1;
208         }
209
210         ffs_tab = kcalloc(func_num, sizeof *ffs_tab, GFP_KERNEL);
211         if (!ffs_tab)
212                 return -ENOMEM;
213
214         if (!gfs_single_func)
215                 for (i = 0; i < func_num; i++)
216                         ffs_tab[i].name = func_names[i];
217
218         missing_funcs = func_num;
219
220         return functionfs_init();
221 }
222 module_init(gfs_init);
223
224 static void __exit gfs_exit(void)
225 {
226         ENTER();
227         mutex_lock(&gfs_lock);
228
229         if (gfs_registered)
230                 usb_composite_unregister(&gfs_driver);
231         gfs_registered = false;
232
233         functionfs_cleanup();
234
235         mutex_unlock(&gfs_lock);
236         kfree(ffs_tab);
237 }
238 module_exit(gfs_exit);
239
240 static struct gfs_ffs_obj *gfs_find_dev(const char *dev_name)
241 {
242         int i;
243
244         ENTER();
245
246         if (gfs_single_func)
247                 return &ffs_tab[0];
248
249         for (i = 0; i < func_num; i++)
250                 if (strcmp(ffs_tab[i].name, dev_name) == 0)
251                         return &ffs_tab[i];
252
253         return NULL;
254 }
255
256 static int functionfs_ready_callback(struct ffs_data *ffs)
257 {
258         struct gfs_ffs_obj *ffs_obj;
259         int ret;
260
261         ENTER();
262         mutex_lock(&gfs_lock);
263
264         ffs_obj = ffs->private_data;
265         if (!ffs_obj) {
266                 ret = -EINVAL;
267                 goto done;
268         }
269
270         if (WARN_ON(ffs_obj->desc_ready)) {
271                 ret = -EBUSY;
272                 goto done;
273         }
274         ffs_obj->desc_ready = true;
275         ffs_obj->ffs_data = ffs;
276
277         if (--missing_funcs) {
278                 ret = 0;
279                 goto done;
280         }
281
282         if (gfs_registered) {
283                 ret = -EBUSY;
284                 goto done;
285         }
286         gfs_registered = true;
287
288         ret = usb_composite_probe(&gfs_driver);
289         if (unlikely(ret < 0))
290                 gfs_registered = false;
291
292 done:
293         mutex_unlock(&gfs_lock);
294         return ret;
295 }
296
297 static void functionfs_closed_callback(struct ffs_data *ffs)
298 {
299         struct gfs_ffs_obj *ffs_obj;
300
301         ENTER();
302         mutex_lock(&gfs_lock);
303
304         ffs_obj = ffs->private_data;
305         if (!ffs_obj)
306                 goto done;
307
308         ffs_obj->desc_ready = false;
309         missing_funcs++;
310
311         if (gfs_registered)
312                 usb_composite_unregister(&gfs_driver);
313         gfs_registered = false;
314
315 done:
316         mutex_unlock(&gfs_lock);
317 }
318
319 static void *functionfs_acquire_dev_callback(const char *dev_name)
320 {
321         struct gfs_ffs_obj *ffs_dev;
322
323         ENTER();
324         mutex_lock(&gfs_lock);
325
326         ffs_dev = gfs_find_dev(dev_name);
327         if (!ffs_dev) {
328                 ffs_dev = ERR_PTR(-ENODEV);
329                 goto done;
330         }
331
332         if (ffs_dev->mounted) {
333                 ffs_dev = ERR_PTR(-EBUSY);
334                 goto done;
335         }
336         ffs_dev->mounted = true;
337
338 done:
339         mutex_unlock(&gfs_lock);
340         return ffs_dev;
341 }
342
343 static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
344 {
345         struct gfs_ffs_obj *ffs_dev;
346
347         ENTER();
348         mutex_lock(&gfs_lock);
349
350         ffs_dev = ffs_data->private_data;
351         if (ffs_dev)
352                 ffs_dev->mounted = false;
353
354         mutex_unlock(&gfs_lock);
355 }
356
357 /*
358  * It is assumed that gfs_bind is called from a context where gfs_lock is held
359  */
360 static int gfs_bind(struct usb_composite_dev *cdev)
361 {
362         int ret, i;
363
364         ENTER();
365
366         if (missing_funcs)
367                 return -ENODEV;
368 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
369         the_dev = gether_setup(cdev->gadget, gfs_hostaddr);
370 #endif
371         if (IS_ERR(the_dev)) {
372                 ret = PTR_ERR(the_dev);
373                 goto error_quick;
374         }
375         gfs_ether_setup = true;
376
377         ret = usb_string_ids_tab(cdev, gfs_strings);
378         if (unlikely(ret < 0))
379                 goto error;
380         gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
381
382         for (i = func_num; i--; ) {
383                 ret = functionfs_bind(ffs_tab[i].ffs_data, cdev);
384                 if (unlikely(ret < 0)) {
385                         while (++i < func_num)
386                                 functionfs_unbind(ffs_tab[i].ffs_data);
387                         goto error;
388                 }
389         }
390
391         for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
392                 struct gfs_configuration *c = gfs_configurations + i;
393                 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
394
395                 c->c.label                      = gfs_strings[sid].s;
396                 c->c.iConfiguration             = gfs_strings[sid].id;
397                 c->c.bConfigurationValue        = 1 + i;
398                 c->c.bmAttributes               = USB_CONFIG_ATT_SELFPOWER;
399
400                 ret = usb_add_config(cdev, &c->c, gfs_do_config);
401                 if (unlikely(ret < 0))
402                         goto error_unbind;
403         }
404         usb_composite_overwrite_options(cdev, &coverwrite);
405         return 0;
406
407 error_unbind:
408         for (i = 0; i < func_num; i++)
409                 functionfs_unbind(ffs_tab[i].ffs_data);
410 error:
411         gether_cleanup(the_dev);
412 error_quick:
413         gfs_ether_setup = false;
414         return ret;
415 }
416
417 /*
418  * It is assumed that gfs_unbind is called from a context where gfs_lock is held
419  */
420 static int gfs_unbind(struct usb_composite_dev *cdev)
421 {
422         int i;
423
424         ENTER();
425
426         /*
427          * We may have been called in an error recovery from
428          * composite_bind() after gfs_unbind() failure so we need to
429          * check if gfs_ffs_data is not NULL since gfs_bind() handles
430          * all error recovery itself.  I'd rather we werent called
431          * from composite on orror recovery, but what you're gonna
432          * do...?
433          */
434         if (gfs_ether_setup)
435                 gether_cleanup(the_dev);
436         gfs_ether_setup = false;
437
438         for (i = func_num; i--; )
439                 if (ffs_tab[i].ffs_data)
440                         functionfs_unbind(ffs_tab[i].ffs_data);
441
442         return 0;
443 }
444
445 /*
446  * It is assumed that gfs_do_config is called from a context where
447  * gfs_lock is held
448  */
449 static int gfs_do_config(struct usb_configuration *c)
450 {
451         struct gfs_configuration *gc =
452                 container_of(c, struct gfs_configuration, c);
453         int i;
454         int ret;
455
456         if (missing_funcs)
457                 return -ENODEV;
458
459         if (gadget_is_otg(c->cdev->gadget)) {
460                 c->descriptors = gfs_otg_desc;
461                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
462         }
463
464         if (gc->eth) {
465                 ret = gc->eth(c, gfs_hostaddr, the_dev);
466                 if (unlikely(ret < 0))
467                         return ret;
468         }
469
470         for (i = 0; i < func_num; i++) {
471                 ret = functionfs_bind_config(c->cdev, c, ffs_tab[i].ffs_data);
472                 if (unlikely(ret < 0))
473                         return ret;
474         }
475
476         /*
477          * After previous do_configs there may be some invalid
478          * pointers in c->interface array.  This happens every time
479          * a user space function with fewer interfaces than a user
480          * space function that was run before the new one is run.  The
481          * compasit's set_config() assumes that if there is no more
482          * then MAX_CONFIG_INTERFACES interfaces in a configuration
483          * then there is a NULL pointer after the last interface in
484          * c->interface array.  We need to make sure this is true.
485          */
486         if (c->next_interface_id < ARRAY_SIZE(c->interface))
487                 c->interface[c->next_interface_id] = NULL;
488
489         return 0;
490 }
491
492 #ifdef CONFIG_USB_FUNCTIONFS_ETH
493
494 static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
495                 struct eth_dev *dev)
496 {
497         return can_support_ecm(c->cdev->gadget)
498                 ? ecm_bind_config(c, ethaddr, dev)
499                 : geth_bind_config(c, ethaddr, dev);
500 }
501
502 #endif