PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / usb / gadget / g_ffs.c
blobfe12e6a2744896a08f9cb55039c45824756f9f52
1 /*
2 * g_ffs.c -- user mode file system API for USB composite function controllers
4 * Copyright (C) 2010 Samsung Electronics
5 * Author: Michal Nazarewicz <mina86@mina86.com>
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.
13 #define pr_fmt(fmt) "g_ffs: " fmt
15 #include <linux/module.h>
17 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
18 #include <linux/netdevice.h>
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
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"
35 USB_ETHERNET_MODULE_PARAMETERS();
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
51 #include "u_fs.h"
53 #define DRIVER_NAME "g_ffs"
54 #define DRIVER_DESC "USB Function Filesystem"
55 #define DRIVER_VERSION "24 Aug 2004"
57 MODULE_DESCRIPTION(DRIVER_DESC);
58 MODULE_AUTHOR("Michal Nazarewicz");
59 MODULE_LICENSE("GPL");
61 #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
62 #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
64 #define GFS_MAX_DEVS 10
66 USB_GADGET_COMPOSITE_OPTIONS();
68 static struct usb_device_descriptor gfs_dev_desc = {
69 .bLength = sizeof gfs_dev_desc,
70 .bDescriptorType = USB_DT_DEVICE,
72 .bcdUSB = cpu_to_le16(0x0200),
73 .bDeviceClass = USB_CLASS_PER_INTERFACE,
75 .idVendor = cpu_to_le16(GFS_VENDOR_ID),
76 .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
79 static char *func_names[GFS_MAX_DEVS];
80 static unsigned int func_num;
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");
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,
98 * REVISIT SRP-only hardware is possible, although
99 * it would not be called "OTG" ...
101 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
104 NULL
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 */
124 static struct usb_gadget_strings *gfs_dev_strings[] = {
125 &(struct usb_gadget_strings) {
126 .language = 0x0409, /* en-us */
127 .strings = gfs_strings,
129 NULL,
132 struct gfs_configuration {
133 struct usb_configuration c;
134 int (*eth)(struct usb_configuration *c);
135 int num;
136 } gfs_configurations[] = {
137 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
139 .eth = bind_rndis_config,
141 #endif
143 #ifdef CONFIG_USB_FUNCTIONFS_ETH
145 .eth = eth_bind_config,
147 #endif
149 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
152 #endif
155 static void *functionfs_acquire_dev(struct ffs_dev *dev);
156 static void functionfs_release_dev(struct ffs_dev *dev);
157 static int functionfs_ready_callback(struct ffs_data *ffs);
158 static void functionfs_closed_callback(struct ffs_data *ffs);
159 static int gfs_bind(struct usb_composite_dev *cdev);
160 static int gfs_unbind(struct usb_composite_dev *cdev);
161 static int gfs_do_config(struct usb_configuration *c);
164 static __refdata struct usb_composite_driver gfs_driver = {
165 .name = DRIVER_NAME,
166 .dev = &gfs_dev_desc,
167 .strings = gfs_dev_strings,
168 .max_speed = USB_SPEED_HIGH,
169 .bind = gfs_bind,
170 .unbind = gfs_unbind,
173 static unsigned int missing_funcs;
174 static bool gfs_registered;
175 static bool gfs_single_func;
176 static struct usb_function_instance **fi_ffs;
177 static struct usb_function **f_ffs[] = {
178 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
179 NULL,
180 #endif
182 #ifdef CONFIG_USB_FUNCTIONFS_ETH
183 NULL,
184 #endif
186 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
187 NULL,
188 #endif
191 #define N_CONF ARRAY_SIZE(f_ffs)
193 static int __init gfs_init(void)
195 struct f_fs_opts *opts;
196 int i;
197 int ret = 0;
199 ENTER();
201 if (func_num < 2) {
202 gfs_single_func = true;
203 func_num = 1;
207 * Allocate in one chunk for easier maintenance
209 f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL);
210 if (!f_ffs[0]) {
211 ret = -ENOMEM;
212 goto no_func;
214 for (i = 1; i < N_CONF; ++i)
215 f_ffs[i] = f_ffs[0] + i * func_num;
217 fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL);
218 if (!fi_ffs) {
219 ret = -ENOMEM;
220 goto no_func;
223 for (i = 0; i < func_num; i++) {
224 fi_ffs[i] = usb_get_function_instance("ffs");
225 if (IS_ERR(fi_ffs[i])) {
226 ret = PTR_ERR(fi_ffs[i]);
227 --i;
228 goto no_dev;
230 opts = to_f_fs_opts(fi_ffs[i]);
231 if (gfs_single_func)
232 ret = ffs_single_dev(opts->dev);
233 else
234 ret = ffs_name_dev(opts->dev, func_names[i]);
235 if (ret)
236 goto no_dev;
237 opts->dev->ffs_ready_callback = functionfs_ready_callback;
238 opts->dev->ffs_closed_callback = functionfs_closed_callback;
239 opts->dev->ffs_acquire_dev_callback = functionfs_acquire_dev;
240 opts->dev->ffs_release_dev_callback = functionfs_release_dev;
241 opts->no_configfs = true;
244 missing_funcs = func_num;
246 return 0;
247 no_dev:
248 while (i >= 0)
249 usb_put_function_instance(fi_ffs[i--]);
250 kfree(fi_ffs);
251 no_func:
252 kfree(f_ffs[0]);
253 return ret;
255 module_init(gfs_init);
257 static void __exit gfs_exit(void)
259 int i;
261 ENTER();
263 if (gfs_registered)
264 usb_composite_unregister(&gfs_driver);
265 gfs_registered = false;
267 kfree(f_ffs[0]);
269 for (i = 0; i < func_num; i++)
270 usb_put_function_instance(fi_ffs[i]);
272 kfree(fi_ffs);
274 module_exit(gfs_exit);
276 static void *functionfs_acquire_dev(struct ffs_dev *dev)
278 if (!try_module_get(THIS_MODULE))
279 return ERR_PTR(-ENODEV);
281 return 0;
284 static void functionfs_release_dev(struct ffs_dev *dev)
286 module_put(THIS_MODULE);
290 * The caller of this function takes ffs_lock
292 static int functionfs_ready_callback(struct ffs_data *ffs)
294 int ret = 0;
296 if (--missing_funcs)
297 return 0;
299 if (gfs_registered)
300 return -EBUSY;
302 gfs_registered = true;
304 ret = usb_composite_probe(&gfs_driver);
305 if (unlikely(ret < 0))
306 gfs_registered = false;
308 return ret;
312 * The caller of this function takes ffs_lock
314 static void functionfs_closed_callback(struct ffs_data *ffs)
316 missing_funcs++;
318 if (gfs_registered)
319 usb_composite_unregister(&gfs_driver);
320 gfs_registered = false;
324 * It is assumed that gfs_bind is called from a context where ffs_lock is held
326 static int gfs_bind(struct usb_composite_dev *cdev)
328 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
329 struct net_device *net;
330 #endif
331 int ret, i;
333 ENTER();
335 if (missing_funcs)
336 return -ENODEV;
337 #if defined CONFIG_USB_FUNCTIONFS_ETH
338 if (can_support_ecm(cdev->gadget)) {
339 struct f_ecm_opts *ecm_opts;
341 fi_ecm = usb_get_function_instance("ecm");
342 if (IS_ERR(fi_ecm))
343 return PTR_ERR(fi_ecm);
344 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
345 net = ecm_opts->net;
346 } else {
347 struct f_gether_opts *geth_opts;
349 fi_geth = usb_get_function_instance("geth");
350 if (IS_ERR(fi_geth))
351 return PTR_ERR(fi_geth);
352 geth_opts = container_of(fi_geth, struct f_gether_opts,
353 func_inst);
354 net = geth_opts->net;
356 #endif
358 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
360 struct f_rndis_opts *rndis_opts;
362 fi_rndis = usb_get_function_instance("rndis");
363 if (IS_ERR(fi_rndis)) {
364 ret = PTR_ERR(fi_rndis);
365 goto error;
367 rndis_opts = container_of(fi_rndis, struct f_rndis_opts,
368 func_inst);
369 #ifndef CONFIG_USB_FUNCTIONFS_ETH
370 net = rndis_opts->net;
371 #endif
373 #endif
375 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
376 gether_set_qmult(net, qmult);
377 if (!gether_set_host_addr(net, host_addr))
378 pr_info("using host ethernet address: %s", host_addr);
379 if (!gether_set_dev_addr(net, dev_addr))
380 pr_info("using self ethernet address: %s", dev_addr);
381 #endif
383 #if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
384 gether_set_gadget(net, cdev->gadget);
385 ret = gether_register_netdev(net);
386 if (ret)
387 goto error_rndis;
389 if (can_support_ecm(cdev->gadget)) {
390 struct f_ecm_opts *ecm_opts;
392 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
393 ecm_opts->bound = true;
394 } else {
395 struct f_gether_opts *geth_opts;
397 geth_opts = container_of(fi_geth, struct f_gether_opts,
398 func_inst);
399 geth_opts->bound = true;
402 rndis_borrow_net(fi_rndis, net);
403 #endif
405 /* TODO: gstrings_attach? */
406 ret = usb_string_ids_tab(cdev, gfs_strings);
407 if (unlikely(ret < 0))
408 goto error_rndis;
409 gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
411 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
412 struct gfs_configuration *c = gfs_configurations + i;
413 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
415 c->c.label = gfs_strings[sid].s;
416 c->c.iConfiguration = gfs_strings[sid].id;
417 c->c.bConfigurationValue = 1 + i;
418 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
420 c->num = i;
422 ret = usb_add_config(cdev, &c->c, gfs_do_config);
423 if (unlikely(ret < 0))
424 goto error_unbind;
426 usb_composite_overwrite_options(cdev, &coverwrite);
427 return 0;
429 /* TODO */
430 error_unbind:
431 error_rndis:
432 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
433 usb_put_function_instance(fi_rndis);
434 error:
435 #endif
436 #if defined CONFIG_USB_FUNCTIONFS_ETH
437 if (can_support_ecm(cdev->gadget))
438 usb_put_function_instance(fi_ecm);
439 else
440 usb_put_function_instance(fi_geth);
441 #endif
442 return ret;
446 * It is assumed that gfs_unbind is called from a context where ffs_lock is held
448 static int gfs_unbind(struct usb_composite_dev *cdev)
450 int i;
452 ENTER();
455 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
456 usb_put_function(f_rndis);
457 usb_put_function_instance(fi_rndis);
458 #endif
460 #if defined CONFIG_USB_FUNCTIONFS_ETH
461 if (can_support_ecm(cdev->gadget)) {
462 usb_put_function(f_ecm);
463 usb_put_function_instance(fi_ecm);
464 } else {
465 usb_put_function(f_geth);
466 usb_put_function_instance(fi_geth);
468 #endif
469 for (i = 0; i < N_CONF * func_num; ++i)
470 usb_put_function(*(f_ffs[0] + i));
472 return 0;
476 * It is assumed that gfs_do_config is called from a context where
477 * ffs_lock is held
479 static int gfs_do_config(struct usb_configuration *c)
481 struct gfs_configuration *gc =
482 container_of(c, struct gfs_configuration, c);
483 int i;
484 int ret;
486 if (missing_funcs)
487 return -ENODEV;
489 if (gadget_is_otg(c->cdev->gadget)) {
490 c->descriptors = gfs_otg_desc;
491 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
494 if (gc->eth) {
495 ret = gc->eth(c);
496 if (unlikely(ret < 0))
497 return ret;
500 for (i = 0; i < func_num; i++) {
501 f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]);
502 if (IS_ERR(f_ffs[gc->num][i])) {
503 ret = PTR_ERR(f_ffs[gc->num][i]);
504 goto error;
506 ret = usb_add_function(c, f_ffs[gc->num][i]);
507 if (ret < 0) {
508 usb_put_function(f_ffs[gc->num][i]);
509 goto error;
514 * After previous do_configs there may be some invalid
515 * pointers in c->interface array. This happens every time
516 * a user space function with fewer interfaces than a user
517 * space function that was run before the new one is run. The
518 * compasit's set_config() assumes that if there is no more
519 * then MAX_CONFIG_INTERFACES interfaces in a configuration
520 * then there is a NULL pointer after the last interface in
521 * c->interface array. We need to make sure this is true.
523 if (c->next_interface_id < ARRAY_SIZE(c->interface))
524 c->interface[c->next_interface_id] = NULL;
526 return 0;
527 error:
528 while (--i >= 0) {
529 if (!IS_ERR(f_ffs[gc->num][i]))
530 usb_remove_function(c, f_ffs[gc->num][i]);
531 usb_put_function(f_ffs[gc->num][i]);
533 return ret;
536 #ifdef CONFIG_USB_FUNCTIONFS_ETH
538 static int eth_bind_config(struct usb_configuration *c)
540 int status = 0;
542 if (can_support_ecm(c->cdev->gadget)) {
543 f_ecm = usb_get_function(fi_ecm);
544 if (IS_ERR(f_ecm))
545 return PTR_ERR(f_ecm);
547 status = usb_add_function(c, f_ecm);
548 if (status < 0)
549 usb_put_function(f_ecm);
551 } else {
552 f_geth = usb_get_function(fi_geth);
553 if (IS_ERR(f_geth))
554 return PTR_ERR(f_geth);
556 status = usb_add_function(c, f_geth);
557 if (status < 0)
558 usb_put_function(f_geth);
560 return status;
563 #endif
565 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
567 static int bind_rndis_config(struct usb_configuration *c)
569 int status = 0;
571 f_rndis = usb_get_function(fi_rndis);
572 if (IS_ERR(f_rndis))
573 return PTR_ERR(f_rndis);
575 status = usb_add_function(c, f_rndis);
576 if (status < 0)
577 usb_put_function(f_rndis);
579 return status;
582 #endif