fbfdb53a2db5f8466368b0e18b9c85c90f5fe9f4
[firefly-linux-kernel-4.4.55.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.h"
36 #  endif
37 #  include "u_ether.h"
38
39 static u8 gfs_host_mac[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_host_mac 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 USB_ETHERNET_MODULE_PARAMETERS();
77
78 static 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
85         .idVendor               = cpu_to_le16(GFS_VENDOR_ID),
86         .idProduct              = cpu_to_le16(GFS_PRODUCT_ID),
87 };
88
89 static char *func_names[GFS_MAX_DEVS];
90 static unsigned int func_num;
91
92 module_param_named(bDeviceClass,    gfs_dev_desc.bDeviceClass,    byte,   0644);
93 MODULE_PARM_DESC(bDeviceClass, "USB Device class");
94 module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte,   0644);
95 MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
96 module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte,   0644);
97 MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
98 module_param_array_named(functions, func_names, charp, &func_num, 0);
99 MODULE_PARM_DESC(functions, "USB Functions list");
100
101 static 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
107                 /*
108                  * REVISIT SRP-only hardware is possible, although
109                  * it would not be called "OTG" ...
110                  */
111                 .bmAttributes           = USB_OTG_SRP | USB_OTG_HNP,
112         },
113
114         NULL
115 };
116
117 /* String IDs are assigned dynamically */
118 static struct usb_string gfs_strings[] = {
119         [USB_GADGET_MANUFACTURER_IDX].s = "",
120         [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
121         [USB_GADGET_SERIAL_IDX].s = "",
122 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
123         { .s = "FunctionFS + RNDIS" },
124 #endif
125 #ifdef CONFIG_USB_FUNCTIONFS_ETH
126         { .s = "FunctionFS + ECM" },
127 #endif
128 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
129         { .s = "FunctionFS" },
130 #endif
131         {  } /* end of list */
132 };
133
134 static 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
142 struct gfs_configuration {
143         struct usb_configuration c;
144         int (*eth)(struct usb_configuration *c, u8 *ethaddr,
145                         struct eth_dev *dev);
146 } gfs_configurations[] = {
147 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
148         {
149                 .eth            = rndis_bind_config,
150         },
151 #endif
152
153 #ifdef CONFIG_USB_FUNCTIONFS_ETH
154         {
155                 .eth            = eth_bind_config,
156         },
157 #endif
158
159 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
160         {
161         },
162 #endif
163 };
164
165 static int gfs_bind(struct usb_composite_dev *cdev);
166 static int gfs_unbind(struct usb_composite_dev *cdev);
167 static int gfs_do_config(struct usb_configuration *c);
168
169 static __refdata struct usb_composite_driver gfs_driver = {
170         .name           = DRIVER_NAME,
171         .dev            = &gfs_dev_desc,
172         .strings        = gfs_dev_strings,
173         .max_speed      = USB_SPEED_HIGH,
174         .bind           = gfs_bind,
175         .unbind         = gfs_unbind,
176 };
177
178 static DEFINE_MUTEX(gfs_lock);
179 static unsigned int missing_funcs;
180 static bool gfs_ether_setup;
181 static bool gfs_registered;
182 static bool gfs_single_func;
183 static struct gfs_ffs_obj *ffs_tab;
184
185 static int __init gfs_init(void)
186 {
187         int i;
188
189         ENTER();
190
191         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
206         return functionfs_init();
207 }
208 module_init(gfs_init);
209
210 static void __exit gfs_exit(void)
211 {
212         ENTER();
213         mutex_lock(&gfs_lock);
214
215         if (gfs_registered)
216                 usb_composite_unregister(&gfs_driver);
217         gfs_registered = false;
218
219         functionfs_cleanup();
220
221         mutex_unlock(&gfs_lock);
222         kfree(ffs_tab);
223 }
224 module_exit(gfs_exit);
225
226 static struct gfs_ffs_obj *gfs_find_dev(const char *dev_name)
227 {
228         int i;
229
230         ENTER();
231
232         if (gfs_single_func)
233                 return &ffs_tab[0];
234
235         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
242 static 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
274         ret = usb_composite_probe(&gfs_driver);
275         if (unlikely(ret < 0))
276                 gfs_registered = false;
277
278 done:
279         mutex_unlock(&gfs_lock);
280         return ret;
281 }
282
283 static void functionfs_closed_callback(struct ffs_data *ffs)
284 {
285         struct gfs_ffs_obj *ffs_obj;
286
287         ENTER();
288         mutex_lock(&gfs_lock);
289
290         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)
298                 usb_composite_unregister(&gfs_driver);
299         gfs_registered = false;
300
301 done:
302         mutex_unlock(&gfs_lock);
303 }
304
305 static void *functionfs_acquire_dev_callback(const char *dev_name)
306 {
307         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
324 done:
325         mutex_unlock(&gfs_lock);
326         return ffs_dev;
327 }
328
329 static 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  */
346 static int gfs_bind(struct usb_composite_dev *cdev)
347 {
348         int ret, i;
349
350         ENTER();
351
352         if (missing_funcs)
353                 return -ENODEV;
354 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
355         the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, gfs_host_mac,
356                                qmult);
357 #endif
358         if (IS_ERR(the_dev)) {
359                 ret = PTR_ERR(the_dev);
360                 goto error_quick;
361         }
362         gfs_ether_setup = true;
363
364         ret = usb_string_ids_tab(cdev, gfs_strings);
365         if (unlikely(ret < 0))
366                 goto error;
367         gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
368
369         for (i = func_num; i--; ) {
370                 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         }
377
378         for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
379                 struct gfs_configuration *c = gfs_configurations + i;
380                 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
381
382                 c->c.label                      = gfs_strings[sid].s;
383                 c->c.iConfiguration             = gfs_strings[sid].id;
384                 c->c.bConfigurationValue        = 1 + i;
385                 c->c.bmAttributes               = USB_CONFIG_ATT_SELFPOWER;
386
387                 ret = usb_add_config(cdev, &c->c, gfs_do_config);
388                 if (unlikely(ret < 0))
389                         goto error_unbind;
390         }
391         usb_composite_overwrite_options(cdev, &coverwrite);
392         return 0;
393
394 error_unbind:
395         for (i = 0; i < func_num; i++)
396                 functionfs_unbind(ffs_tab[i].ffs_data);
397 error:
398         gether_cleanup(the_dev);
399 error_quick:
400         gfs_ether_setup = false;
401         return ret;
402 }
403
404 /*
405  * It is assumed that gfs_unbind is called from a context where gfs_lock is held
406  */
407 static int gfs_unbind(struct usb_composite_dev *cdev)
408 {
409         int i;
410
411         ENTER();
412
413         /*
414          * We may have been called in an error recovery from
415          * 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
419          * do...?
420          */
421         if (gfs_ether_setup)
422                 gether_cleanup(the_dev);
423         gfs_ether_setup = false;
424
425         for (i = func_num; i--; )
426                 if (ffs_tab[i].ffs_data)
427                         functionfs_unbind(ffs_tab[i].ffs_data);
428
429         return 0;
430 }
431
432 /*
433  * It is assumed that gfs_do_config is called from a context where
434  * gfs_lock is held
435  */
436 static int gfs_do_config(struct usb_configuration *c)
437 {
438         struct gfs_configuration *gc =
439                 container_of(c, struct gfs_configuration, c);
440         int i;
441         int ret;
442
443         if (missing_funcs)
444                 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
451         if (gc->eth) {
452                 ret = gc->eth(c, gfs_host_mac, the_dev);
453                 if (unlikely(ret < 0))
454                         return ret;
455         }
456
457         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         }
462
463         /*
464          * After previous do_configs there may be some invalid
465          * 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
471          * c->interface array.  We need to make sure this is true.
472          */
473         if (c->next_interface_id < ARRAY_SIZE(c->interface))
474                 c->interface[c->next_interface_id] = NULL;
475
476         return 0;
477 }
478
479 #ifdef CONFIG_USB_FUNCTIONFS_ETH
480
481 static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
482                 struct eth_dev *dev)
483 {
484         return can_support_ecm(c->cdev->gadget)
485                 ? ecm_bind_config(c, ethaddr, dev)
486                 : geth_bind_config(c, ethaddr, dev);
487 }
488
489 #endif