Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / usb / gadget / legacy / 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 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
18 #include <linux/netdevice.h>
19
20 #  if defined USB_ETH_RNDIS
21 #    undef USB_ETH_RNDIS
22 #  endif
23 #  ifdef CONFIG_USB_FUNCTIONFS_RNDIS
24 #    define USB_ETH_RNDIS y
25 #  endif
26
27 #  include "u_ecm.h"
28 #  include "u_gether.h"
29 #  ifdef USB_ETH_RNDIS
30 #    include "u_rndis.h"
31 #    include "rndis.h"
32 #  endif
33 #  include "u_ether.h"
34
35 USB_ETHERNET_MODULE_PARAMETERS();
36
37 #  ifdef CONFIG_USB_FUNCTIONFS_ETH
38 static int eth_bind_config(struct usb_configuration *c);
39 static struct usb_function_instance *fi_ecm;
40 static struct usb_function *f_ecm;
41 static struct usb_function_instance *fi_geth;
42 static struct usb_function *f_geth;
43 #  endif
44 #  ifdef CONFIG_USB_FUNCTIONFS_RNDIS
45 static int bind_rndis_config(struct usb_configuration *c);
46 static struct usb_function_instance *fi_rndis;
47 static struct usb_function *f_rndis;
48 #  endif
49 #endif
50
51 #include "u_fs.h"
52
53 #define DRIVER_NAME     "g_ffs"
54 #define DRIVER_DESC     "USB Function Filesystem"
55 #define DRIVER_VERSION  "24 Aug 2004"
56
57 MODULE_DESCRIPTION(DRIVER_DESC);
58 MODULE_AUTHOR("Michal Nazarewicz");
59 MODULE_LICENSE("GPL");
60
61 #define GFS_VENDOR_ID   0x1d6b  /* Linux Foundation */
62 #define GFS_PRODUCT_ID  0x0105  /* FunctionFS Gadget */
63
64 #define GFS_MAX_DEVS    10
65
66 USB_GADGET_COMPOSITE_OPTIONS();
67
68 static struct usb_device_descriptor gfs_dev_desc = {
69         .bLength                = sizeof gfs_dev_desc,
70         .bDescriptorType        = USB_DT_DEVICE,
71
72         .bcdUSB                 = cpu_to_le16(0x0200),
73         .bDeviceClass           = USB_CLASS_PER_INTERFACE,
74
75         .idVendor               = cpu_to_le16(GFS_VENDOR_ID),
76         .idProduct              = cpu_to_le16(GFS_PRODUCT_ID),
77 };
78
79 static char *func_names[GFS_MAX_DEVS];
80 static unsigned int func_num;
81
82 module_param_named(bDeviceClass,    gfs_dev_desc.bDeviceClass,    byte,   0644);
83 MODULE_PARM_DESC(bDeviceClass, "USB Device class");
84 module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte,   0644);
85 MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
86 module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte,   0644);
87 MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
88 module_param_array_named(functions, func_names, charp, &func_num, 0);
89 MODULE_PARM_DESC(functions, "USB Functions list");
90
91 static const struct usb_descriptor_header *gfs_otg_desc[] = {
92         (const struct usb_descriptor_header *)
93         &(const struct usb_otg_descriptor) {
94                 .bLength                = sizeof(struct usb_otg_descriptor),
95                 .bDescriptorType        = USB_DT_OTG,
96
97                 /*
98                  * REVISIT SRP-only hardware is possible, although
99                  * it would not be called "OTG" ...
100                  */
101                 .bmAttributes           = USB_OTG_SRP | USB_OTG_HNP,
102         },
103
104         NULL
105 };
106
107 /* String IDs are assigned dynamically */
108 static struct usb_string gfs_strings[] = {
109         [USB_GADGET_MANUFACTURER_IDX].s = "",
110         [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
111         [USB_GADGET_SERIAL_IDX].s = "",
112 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
113         { .s = "FunctionFS + RNDIS" },
114 #endif
115 #ifdef CONFIG_USB_FUNCTIONFS_ETH
116         { .s = "FunctionFS + ECM" },
117 #endif
118 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
119         { .s = "FunctionFS" },
120 #endif
121         {  } /* end of list */
122 };
123
124 static struct usb_gadget_strings *gfs_dev_strings[] = {
125         &(struct usb_gadget_strings) {
126                 .language       = 0x0409,       /* en-us */
127                 .strings        = gfs_strings,
128         },
129         NULL,
130 };
131
132 struct gfs_configuration {
133         struct usb_configuration c;
134         int (*eth)(struct usb_configuration *c);
135         int num;
136 };
137
138 static struct gfs_configuration gfs_configurations[] = {
139 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
140         {
141                 .eth            = bind_rndis_config,
142         },
143 #endif
144
145 #ifdef CONFIG_USB_FUNCTIONFS_ETH
146         {
147                 .eth            = eth_bind_config,
148         },
149 #endif
150
151 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
152         {
153         },
154 #endif
155 };
156
157 static void *functionfs_acquire_dev(struct ffs_dev *dev);
158 static void functionfs_release_dev(struct ffs_dev *dev);
159 static int functionfs_ready_callback(struct ffs_data *ffs);
160 static void functionfs_closed_callback(struct ffs_data *ffs);
161 static int gfs_bind(struct usb_composite_dev *cdev);
162 static int gfs_unbind(struct usb_composite_dev *cdev);
163 static int gfs_do_config(struct usb_configuration *c);
164
165
166 static struct usb_composite_driver gfs_driver = {
167         .name           = DRIVER_NAME,
168         .dev            = &gfs_dev_desc,
169         .strings        = gfs_dev_strings,
170         .max_speed      = USB_SPEED_HIGH,
171         .bind           = gfs_bind,
172         .unbind         = gfs_unbind,
173 };
174
175 static unsigned int missing_funcs;
176 static bool gfs_registered;
177 static bool gfs_single_func;
178 static struct usb_function_instance **fi_ffs;
179 static struct usb_function **f_ffs[] = {
180 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
181         NULL,
182 #endif
183
184 #ifdef CONFIG_USB_FUNCTIONFS_ETH
185         NULL,
186 #endif
187
188 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
189         NULL,
190 #endif
191 };
192
193 #define N_CONF ARRAY_SIZE(f_ffs)
194
195 static int __init gfs_init(void)
196 {
197         struct f_fs_opts *opts;
198         int i;
199         int ret = 0;
200
201         ENTER();
202
203         if (func_num < 2) {
204                 gfs_single_func = true;
205                 func_num = 1;
206         }
207
208         /*
209          * Allocate in one chunk for easier maintenance
210          */
211         f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL);
212         if (!f_ffs[0]) {
213                 ret = -ENOMEM;
214                 goto no_func;
215         }
216         for (i = 1; i < N_CONF; ++i)
217                 f_ffs[i] = f_ffs[0] + i * func_num;
218
219         fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL);
220         if (!fi_ffs) {
221                 ret = -ENOMEM;
222                 goto no_func;
223         }
224
225         for (i = 0; i < func_num; i++) {
226                 fi_ffs[i] = usb_get_function_instance("ffs");
227                 if (IS_ERR(fi_ffs[i])) {
228                         ret = PTR_ERR(fi_ffs[i]);
229                         --i;
230                         goto no_dev;
231                 }
232                 opts = to_f_fs_opts(fi_ffs[i]);
233                 if (gfs_single_func)
234                         ret = ffs_single_dev(opts->dev);
235                 else
236                         ret = ffs_name_dev(opts->dev, func_names[i]);
237                 if (ret)
238                         goto no_dev;
239                 opts->dev->ffs_ready_callback = functionfs_ready_callback;
240                 opts->dev->ffs_closed_callback = functionfs_closed_callback;
241                 opts->dev->ffs_acquire_dev_callback = functionfs_acquire_dev;
242                 opts->dev->ffs_release_dev_callback = functionfs_release_dev;
243                 opts->no_configfs = true;
244         }
245
246         missing_funcs = func_num;
247
248         return 0;
249 no_dev:
250         while (i >= 0)
251                 usb_put_function_instance(fi_ffs[i--]);
252         kfree(fi_ffs);
253 no_func:
254         kfree(f_ffs[0]);
255         return ret;
256 }
257 module_init(gfs_init);
258
259 static void __exit gfs_exit(void)
260 {
261         int i;
262
263         ENTER();
264
265         if (gfs_registered)
266                 usb_composite_unregister(&gfs_driver);
267         gfs_registered = false;
268
269         kfree(f_ffs[0]);
270
271         for (i = 0; i < func_num; i++)
272                 usb_put_function_instance(fi_ffs[i]);
273
274         kfree(fi_ffs);
275 }
276 module_exit(gfs_exit);
277
278 static void *functionfs_acquire_dev(struct ffs_dev *dev)
279 {
280         if (!try_module_get(THIS_MODULE))
281                 return ERR_PTR(-ENOENT);
282         
283         return NULL;
284 }
285
286 static void functionfs_release_dev(struct ffs_dev *dev)
287 {
288         module_put(THIS_MODULE);
289 }
290
291 /*
292  * The caller of this function takes ffs_lock 
293  */
294 static int functionfs_ready_callback(struct ffs_data *ffs)
295 {
296         int ret = 0;
297
298         if (--missing_funcs)
299                 return 0;
300
301         if (gfs_registered)
302                 return -EBUSY;
303
304         gfs_registered = true;
305
306         ret = usb_composite_probe(&gfs_driver);
307         if (unlikely(ret < 0)) {
308                 ++missing_funcs;
309                 gfs_registered = false;
310         }
311         
312         return ret;
313 }
314
315 /*
316  * The caller of this function takes ffs_lock 
317  */
318 static void functionfs_closed_callback(struct ffs_data *ffs)
319 {
320         missing_funcs++;
321
322         if (gfs_registered)
323                 usb_composite_unregister(&gfs_driver);
324         gfs_registered = false;
325 }
326
327 /*
328  * It is assumed that gfs_bind is called from a context where ffs_lock is held
329  */
330 static int gfs_bind(struct usb_composite_dev *cdev)
331 {
332 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
333         struct net_device *net;
334 #endif
335         int ret, i;
336
337         ENTER();
338
339         if (missing_funcs)
340                 return -ENODEV;
341 #if defined CONFIG_USB_FUNCTIONFS_ETH
342         if (can_support_ecm(cdev->gadget)) {
343                 struct f_ecm_opts *ecm_opts;
344
345                 fi_ecm = usb_get_function_instance("ecm");
346                 if (IS_ERR(fi_ecm))
347                         return PTR_ERR(fi_ecm);
348                 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
349                 net = ecm_opts->net;
350         } else {
351                 struct f_gether_opts *geth_opts;
352
353                 fi_geth = usb_get_function_instance("geth");
354                 if (IS_ERR(fi_geth))
355                         return PTR_ERR(fi_geth);
356                 geth_opts = container_of(fi_geth, struct f_gether_opts,
357                                          func_inst);
358                 net = geth_opts->net;
359         }
360 #endif
361
362 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
363         {
364                 struct f_rndis_opts *rndis_opts;
365
366                 fi_rndis = usb_get_function_instance("rndis");
367                 if (IS_ERR(fi_rndis)) {
368                         ret = PTR_ERR(fi_rndis);
369                         goto error;
370                 }
371                 rndis_opts = container_of(fi_rndis, struct f_rndis_opts,
372                                           func_inst);
373 #ifndef CONFIG_USB_FUNCTIONFS_ETH
374                 net = rndis_opts->net;
375 #endif
376         }
377 #endif
378
379 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
380         gether_set_qmult(net, qmult);
381         if (!gether_set_host_addr(net, host_addr))
382                 pr_info("using host ethernet address: %s", host_addr);
383         if (!gether_set_dev_addr(net, dev_addr))
384                 pr_info("using self ethernet address: %s", dev_addr);
385 #endif
386
387 #if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
388         gether_set_gadget(net, cdev->gadget);
389         ret = gether_register_netdev(net);
390         if (ret)
391                 goto error_rndis;
392
393         if (can_support_ecm(cdev->gadget)) {
394                 struct f_ecm_opts *ecm_opts;
395
396                 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
397                 ecm_opts->bound = true;
398         } else {
399                 struct f_gether_opts *geth_opts;
400
401                 geth_opts = container_of(fi_geth, struct f_gether_opts,
402                                          func_inst);
403                 geth_opts->bound = true;
404         }
405
406         rndis_borrow_net(fi_rndis, net);
407 #endif
408
409         /* TODO: gstrings_attach? */
410         ret = usb_string_ids_tab(cdev, gfs_strings);
411         if (unlikely(ret < 0))
412                 goto error_rndis;
413         gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
414
415         for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
416                 struct gfs_configuration *c = gfs_configurations + i;
417                 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
418
419                 c->c.label                      = gfs_strings[sid].s;
420                 c->c.iConfiguration             = gfs_strings[sid].id;
421                 c->c.bConfigurationValue        = 1 + i;
422                 c->c.bmAttributes               = USB_CONFIG_ATT_SELFPOWER;
423
424                 c->num = i;
425
426                 ret = usb_add_config(cdev, &c->c, gfs_do_config);
427                 if (unlikely(ret < 0))
428                         goto error_unbind;
429         }
430         usb_composite_overwrite_options(cdev, &coverwrite);
431         return 0;
432
433 /* TODO */
434 error_unbind:
435 error_rndis:
436 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
437         usb_put_function_instance(fi_rndis);
438 error:
439 #endif
440 #if defined CONFIG_USB_FUNCTIONFS_ETH
441         if (can_support_ecm(cdev->gadget))
442                 usb_put_function_instance(fi_ecm);
443         else
444                 usb_put_function_instance(fi_geth);
445 #endif
446         return ret;
447 }
448
449 /*
450  * It is assumed that gfs_unbind is called from a context where ffs_lock is held
451  */
452 static int gfs_unbind(struct usb_composite_dev *cdev)
453 {
454         int i;
455
456         ENTER();
457
458
459 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
460         usb_put_function(f_rndis);
461         usb_put_function_instance(fi_rndis);
462 #endif
463
464 #if defined CONFIG_USB_FUNCTIONFS_ETH
465         if (can_support_ecm(cdev->gadget)) {
466                 usb_put_function(f_ecm);
467                 usb_put_function_instance(fi_ecm);
468         } else {
469                 usb_put_function(f_geth);
470                 usb_put_function_instance(fi_geth);
471         }
472 #endif
473         for (i = 0; i < N_CONF * func_num; ++i)
474                 usb_put_function(*(f_ffs[0] + i));
475
476         return 0;
477 }
478
479 /*
480  * It is assumed that gfs_do_config is called from a context where
481  * ffs_lock is held
482  */
483 static int gfs_do_config(struct usb_configuration *c)
484 {
485         struct gfs_configuration *gc =
486                 container_of(c, struct gfs_configuration, c);
487         int i;
488         int ret;
489
490         if (missing_funcs)
491                 return -ENODEV;
492
493         if (gadget_is_otg(c->cdev->gadget)) {
494                 c->descriptors = gfs_otg_desc;
495                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
496         }
497
498         if (gc->eth) {
499                 ret = gc->eth(c);
500                 if (unlikely(ret < 0))
501                         return ret;
502         }
503
504         for (i = 0; i < func_num; i++) {
505                 f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]);
506                 if (IS_ERR(f_ffs[gc->num][i])) {
507                         ret = PTR_ERR(f_ffs[gc->num][i]);
508                         goto error;
509                 }
510                 ret = usb_add_function(c, f_ffs[gc->num][i]);
511                 if (ret < 0) {
512                         usb_put_function(f_ffs[gc->num][i]);
513                         goto error;
514                 }
515         }
516
517         /*
518          * After previous do_configs there may be some invalid
519          * pointers in c->interface array.  This happens every time
520          * a user space function with fewer interfaces than a user
521          * space function that was run before the new one is run.  The
522          * compasit's set_config() assumes that if there is no more
523          * then MAX_CONFIG_INTERFACES interfaces in a configuration
524          * then there is a NULL pointer after the last interface in
525          * c->interface array.  We need to make sure this is true.
526          */
527         if (c->next_interface_id < ARRAY_SIZE(c->interface))
528                 c->interface[c->next_interface_id] = NULL;
529
530         return 0;
531 error:
532         while (--i >= 0) {
533                 if (!IS_ERR(f_ffs[gc->num][i]))
534                         usb_remove_function(c, f_ffs[gc->num][i]);
535                 usb_put_function(f_ffs[gc->num][i]);
536         }
537         return ret;
538 }
539
540 #ifdef CONFIG_USB_FUNCTIONFS_ETH
541
542 static int eth_bind_config(struct usb_configuration *c)
543 {
544         int status = 0;
545
546         if (can_support_ecm(c->cdev->gadget)) {
547                 f_ecm = usb_get_function(fi_ecm);
548                 if (IS_ERR(f_ecm))
549                         return PTR_ERR(f_ecm);
550
551                 status = usb_add_function(c, f_ecm);
552                 if (status < 0)
553                         usb_put_function(f_ecm);
554
555         } else {
556                 f_geth = usb_get_function(fi_geth);
557                 if (IS_ERR(f_geth))
558                         return PTR_ERR(f_geth);
559
560                 status = usb_add_function(c, f_geth);
561                 if (status < 0)
562                         usb_put_function(f_geth);
563         }
564         return status;
565 }
566
567 #endif
568
569 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
570
571 static int bind_rndis_config(struct usb_configuration *c)
572 {
573         int status = 0;
574
575         f_rndis = usb_get_function(fi_rndis);
576         if (IS_ERR(f_rndis))
577                 return PTR_ERR(f_rndis);
578
579         status = usb_add_function(c, f_rndis);
580         if (status < 0)
581                 usb_put_function(f_rndis);
582
583         return status;
584 }
585
586 #endif