usb: gadget: g_ffs: Allow to set bmAttributes of configuration
[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
141 static int bmAttrs_overwrite = -1;
142
143 module_param_named(bmAttrs_overwrite, bmAttrs_overwrite, int, 0644);
144 MODULE_PARM_DESC(bmAttrs_overwrite,
145                  "Overwrite default bmAttrributes value. -1 to use default.");
146
147 static struct usb_gadget_strings *gfs_dev_strings[] = {
148         &(struct usb_gadget_strings) {
149                 .language       = 0x0409,       /* en-us */
150                 .strings        = gfs_strings,
151         },
152         NULL,
153 };
154
155 struct gfs_configuration {
156         struct usb_configuration c;
157         int (*eth)(struct usb_configuration *c, u8 *ethaddr,
158                         struct eth_dev *dev);
159 } gfs_configurations[] = {
160 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
161         {
162                 .eth            = rndis_bind_config,
163         },
164 #endif
165
166 #ifdef CONFIG_USB_FUNCTIONFS_ETH
167         {
168                 .eth            = eth_bind_config,
169         },
170 #endif
171
172 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
173         {
174         },
175 #endif
176 };
177
178 static int gfs_bind(struct usb_composite_dev *cdev);
179 static int gfs_unbind(struct usb_composite_dev *cdev);
180 static int gfs_do_config(struct usb_configuration *c);
181
182 static __refdata struct usb_composite_driver gfs_driver = {
183         .name           = DRIVER_NAME,
184         .dev            = &gfs_dev_desc,
185         .strings        = gfs_dev_strings,
186         .max_speed      = USB_SPEED_HIGH,
187         .bind           = gfs_bind,
188         .unbind         = gfs_unbind,
189 };
190
191 static DEFINE_MUTEX(gfs_lock);
192 static unsigned int missing_funcs;
193 static bool gfs_ether_setup;
194 static bool gfs_registered;
195 static bool gfs_single_func;
196 static struct gfs_ffs_obj *ffs_tab;
197
198 static int __init gfs_init(void)
199 {
200         int i;
201
202         ENTER();
203
204 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
205         /* Find first (but not last) config with a NULL str */
206         for (i = 0; i < ARRAY_SIZE(gfs_strings) - 1 ; ++i)
207                 if (!gfs_strings[i].s) {
208                         gfs_strings[i].s = generic_ffs_config_str;
209                         break;
210                 }
211 #endif
212         if (bmAttrs_overwrite > 255) {
213                 pr_info("Invalid value of bmAttributes for configs.");
214                 return -EINVAL;
215         }
216
217         if (!func_num) {
218                 gfs_single_func = true;
219                 func_num = 1;
220         }
221
222         ffs_tab = kcalloc(func_num, sizeof *ffs_tab, GFP_KERNEL);
223         if (!ffs_tab)
224                 return -ENOMEM;
225
226         if (!gfs_single_func)
227                 for (i = 0; i < func_num; i++)
228                         ffs_tab[i].name = func_names[i];
229
230         missing_funcs = func_num;
231
232         return functionfs_init();
233 }
234 module_init(gfs_init);
235
236 static void __exit gfs_exit(void)
237 {
238         ENTER();
239         mutex_lock(&gfs_lock);
240
241         if (gfs_registered)
242                 usb_composite_unregister(&gfs_driver);
243         gfs_registered = false;
244
245         functionfs_cleanup();
246
247         mutex_unlock(&gfs_lock);
248         kfree(ffs_tab);
249 }
250 module_exit(gfs_exit);
251
252 static struct gfs_ffs_obj *gfs_find_dev(const char *dev_name)
253 {
254         int i;
255
256         ENTER();
257
258         if (gfs_single_func)
259                 return &ffs_tab[0];
260
261         for (i = 0; i < func_num; i++)
262                 if (strcmp(ffs_tab[i].name, dev_name) == 0)
263                         return &ffs_tab[i];
264
265         return NULL;
266 }
267
268 static int functionfs_ready_callback(struct ffs_data *ffs)
269 {
270         struct gfs_ffs_obj *ffs_obj;
271         int ret;
272
273         ENTER();
274         mutex_lock(&gfs_lock);
275
276         ffs_obj = ffs->private_data;
277         if (!ffs_obj) {
278                 ret = -EINVAL;
279                 goto done;
280         }
281
282         if (WARN_ON(ffs_obj->desc_ready)) {
283                 ret = -EBUSY;
284                 goto done;
285         }
286         ffs_obj->desc_ready = true;
287         ffs_obj->ffs_data = ffs;
288
289         if (--missing_funcs) {
290                 ret = 0;
291                 goto done;
292         }
293
294         if (gfs_registered) {
295                 ret = -EBUSY;
296                 goto done;
297         }
298         gfs_registered = true;
299
300         ret = usb_composite_probe(&gfs_driver);
301         if (unlikely(ret < 0))
302                 gfs_registered = false;
303
304 done:
305         mutex_unlock(&gfs_lock);
306         return ret;
307 }
308
309 static void functionfs_closed_callback(struct ffs_data *ffs)
310 {
311         struct gfs_ffs_obj *ffs_obj;
312
313         ENTER();
314         mutex_lock(&gfs_lock);
315
316         ffs_obj = ffs->private_data;
317         if (!ffs_obj)
318                 goto done;
319
320         ffs_obj->desc_ready = false;
321         missing_funcs++;
322
323         if (gfs_registered)
324                 usb_composite_unregister(&gfs_driver);
325         gfs_registered = false;
326
327 done:
328         mutex_unlock(&gfs_lock);
329 }
330
331 static void *functionfs_acquire_dev_callback(const char *dev_name)
332 {
333         struct gfs_ffs_obj *ffs_dev;
334
335         ENTER();
336         mutex_lock(&gfs_lock);
337
338         ffs_dev = gfs_find_dev(dev_name);
339         if (!ffs_dev) {
340                 ffs_dev = ERR_PTR(-ENODEV);
341                 goto done;
342         }
343
344         if (ffs_dev->mounted) {
345                 ffs_dev = ERR_PTR(-EBUSY);
346                 goto done;
347         }
348         ffs_dev->mounted = true;
349
350 done:
351         mutex_unlock(&gfs_lock);
352         return ffs_dev;
353 }
354
355 static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
356 {
357         struct gfs_ffs_obj *ffs_dev;
358
359         ENTER();
360         mutex_lock(&gfs_lock);
361
362         ffs_dev = ffs_data->private_data;
363         if (ffs_dev)
364                 ffs_dev->mounted = false;
365
366         mutex_unlock(&gfs_lock);
367 }
368
369 /*
370  * It is assumed that gfs_bind is called from a context where gfs_lock is held
371  */
372 static int gfs_bind(struct usb_composite_dev *cdev)
373 {
374         int ret, i;
375
376         ENTER();
377
378         if (missing_funcs)
379                 return -ENODEV;
380 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
381         the_dev = gether_setup(cdev->gadget, gfs_hostaddr);
382 #endif
383         if (IS_ERR(the_dev)) {
384                 ret = PTR_ERR(the_dev);
385                 goto error_quick;
386         }
387         gfs_ether_setup = true;
388
389         ret = usb_string_ids_tab(cdev, gfs_strings);
390         if (unlikely(ret < 0))
391                 goto error;
392         gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
393
394         for (i = func_num; i--; ) {
395                 ret = functionfs_bind(ffs_tab[i].ffs_data, cdev);
396                 if (unlikely(ret < 0)) {
397                         while (++i < func_num)
398                                 functionfs_unbind(ffs_tab[i].ffs_data);
399                         goto error;
400                 }
401         }
402
403         for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
404                 struct gfs_configuration *c = gfs_configurations + i;
405                 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
406
407                 c->c.label                      = gfs_strings[sid].s;
408                 c->c.iConfiguration             = gfs_strings[sid].id;
409                 c->c.bConfigurationValue        = 1 + i;
410                 c->c.bmAttributes               = bmAttrs_overwrite >= 0 ?
411                         (unsigned char)bmAttrs_overwrite
412                         : USB_CONFIG_ATT_SELFPOWER;
413
414                 ret = usb_add_config(cdev, &c->c, gfs_do_config);
415                 if (unlikely(ret < 0))
416                         goto error_unbind;
417         }
418         usb_composite_overwrite_options(cdev, &coverwrite);
419         return 0;
420
421 error_unbind:
422         for (i = 0; i < func_num; i++)
423                 functionfs_unbind(ffs_tab[i].ffs_data);
424 error:
425         gether_cleanup(the_dev);
426 error_quick:
427         gfs_ether_setup = false;
428         return ret;
429 }
430
431 /*
432  * It is assumed that gfs_unbind is called from a context where gfs_lock is held
433  */
434 static int gfs_unbind(struct usb_composite_dev *cdev)
435 {
436         int i;
437
438         ENTER();
439
440         /*
441          * We may have been called in an error recovery from
442          * composite_bind() after gfs_unbind() failure so we need to
443          * check if gfs_ffs_data is not NULL since gfs_bind() handles
444          * all error recovery itself.  I'd rather we werent called
445          * from composite on orror recovery, but what you're gonna
446          * do...?
447          */
448         if (gfs_ether_setup)
449                 gether_cleanup(the_dev);
450         gfs_ether_setup = false;
451
452         for (i = func_num; i--; )
453                 if (ffs_tab[i].ffs_data)
454                         functionfs_unbind(ffs_tab[i].ffs_data);
455
456         return 0;
457 }
458
459 /*
460  * It is assumed that gfs_do_config is called from a context where
461  * gfs_lock is held
462  */
463 static int gfs_do_config(struct usb_configuration *c)
464 {
465         struct gfs_configuration *gc =
466                 container_of(c, struct gfs_configuration, c);
467         int i;
468         int ret;
469
470         if (missing_funcs)
471                 return -ENODEV;
472
473         if (gadget_is_otg(c->cdev->gadget) && bmAttrs_overwrite < 0) {
474                 c->descriptors = gfs_otg_desc;
475                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
476         }
477
478         if (gc->eth) {
479                 ret = gc->eth(c, gfs_hostaddr, the_dev);
480                 if (unlikely(ret < 0))
481                         return ret;
482         }
483
484         for (i = 0; i < func_num; i++) {
485                 ret = functionfs_bind_config(c->cdev, c, ffs_tab[i].ffs_data);
486                 if (unlikely(ret < 0))
487                         return ret;
488         }
489
490         /*
491          * After previous do_configs there may be some invalid
492          * pointers in c->interface array.  This happens every time
493          * a user space function with fewer interfaces than a user
494          * space function that was run before the new one is run.  The
495          * compasit's set_config() assumes that if there is no more
496          * then MAX_CONFIG_INTERFACES interfaces in a configuration
497          * then there is a NULL pointer after the last interface in
498          * c->interface array.  We need to make sure this is true.
499          */
500         if (c->next_interface_id < ARRAY_SIZE(c->interface))
501                 c->interface[c->next_interface_id] = NULL;
502
503         return 0;
504 }
505
506 #ifdef CONFIG_USB_FUNCTIONFS_ETH
507
508 static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
509                 struct eth_dev *dev)
510 {
511         return can_support_ecm(c->cdev->gadget)
512                 ? ecm_bind_config(c, ethaddr, dev)
513                 : geth_bind_config(c, ethaddr, dev);
514 }
515
516 #endif