2 * g_ffs.c -- user mode file system API for USB composite function controllers
4 * Copyright (C) 2010 Samsung Electronics
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
23 # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
24 # define USB_ETH_RNDIS y
28 # include "u_gether.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;
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;
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,
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" },
115 #ifdef CONFIG_USB_FUNCTIONFS_ETH
116 { .s = "FunctionFS + ECM" },
118 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
119 { .s = "FunctionFS" },
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,
132 struct gfs_configuration {
133 struct usb_configuration c;
134 int (*eth)(struct usb_configuration *c);
138 static struct gfs_configuration gfs_configurations[] = {
139 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
141 .eth = bind_rndis_config,
145 #ifdef CONFIG_USB_FUNCTIONFS_ETH
147 .eth = eth_bind_config,
151 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
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);
166 static __refdata struct usb_composite_driver gfs_driver = {
168 .dev = &gfs_dev_desc,
169 .strings = gfs_dev_strings,
170 .max_speed = USB_SPEED_HIGH,
172 .unbind = gfs_unbind,
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
184 #ifdef CONFIG_USB_FUNCTIONFS_ETH
188 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
193 #define N_CONF ARRAY_SIZE(f_ffs)
195 static int __init gfs_init(void)
197 struct f_fs_opts *opts;
204 gfs_single_func = true;
209 * Allocate in one chunk for easier maintenance
211 f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL);
216 for (i = 1; i < N_CONF; ++i)
217 f_ffs[i] = f_ffs[0] + i * func_num;
219 fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL);
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]);
232 opts = to_f_fs_opts(fi_ffs[i]);
234 ret = ffs_single_dev(opts->dev);
236 ret = ffs_name_dev(opts->dev, func_names[i]);
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;
246 missing_funcs = func_num;
251 usb_put_function_instance(fi_ffs[i--]);
257 module_init(gfs_init);
259 static void __exit gfs_exit(void)
266 usb_composite_unregister(&gfs_driver);
267 gfs_registered = false;
271 for (i = 0; i < func_num; i++)
272 usb_put_function_instance(fi_ffs[i]);
276 module_exit(gfs_exit);
278 static void *functionfs_acquire_dev(struct ffs_dev *dev)
280 if (!try_module_get(THIS_MODULE))
281 return ERR_PTR(-ENOENT);
286 static void functionfs_release_dev(struct ffs_dev *dev)
288 module_put(THIS_MODULE);
292 * The caller of this function takes ffs_lock
294 static int functionfs_ready_callback(struct ffs_data *ffs)
304 gfs_registered = true;
306 ret = usb_composite_probe(&gfs_driver);
307 if (unlikely(ret < 0))
308 gfs_registered = false;
314 * The caller of this function takes ffs_lock
316 static void functionfs_closed_callback(struct ffs_data *ffs)
321 usb_composite_unregister(&gfs_driver);
322 gfs_registered = false;
326 * It is assumed that gfs_bind is called from a context where ffs_lock is held
328 static int gfs_bind(struct usb_composite_dev *cdev)
330 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
331 struct net_device *net;
339 #if defined CONFIG_USB_FUNCTIONFS_ETH
340 if (can_support_ecm(cdev->gadget)) {
341 struct f_ecm_opts *ecm_opts;
343 fi_ecm = usb_get_function_instance("ecm");
345 return PTR_ERR(fi_ecm);
346 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
349 struct f_gether_opts *geth_opts;
351 fi_geth = usb_get_function_instance("geth");
353 return PTR_ERR(fi_geth);
354 geth_opts = container_of(fi_geth, struct f_gether_opts,
356 net = geth_opts->net;
360 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
362 struct f_rndis_opts *rndis_opts;
364 fi_rndis = usb_get_function_instance("rndis");
365 if (IS_ERR(fi_rndis)) {
366 ret = PTR_ERR(fi_rndis);
369 rndis_opts = container_of(fi_rndis, struct f_rndis_opts,
371 #ifndef CONFIG_USB_FUNCTIONFS_ETH
372 net = rndis_opts->net;
377 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
378 gether_set_qmult(net, qmult);
379 if (!gether_set_host_addr(net, host_addr))
380 pr_info("using host ethernet address: %s", host_addr);
381 if (!gether_set_dev_addr(net, dev_addr))
382 pr_info("using self ethernet address: %s", dev_addr);
385 #if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
386 gether_set_gadget(net, cdev->gadget);
387 ret = gether_register_netdev(net);
391 if (can_support_ecm(cdev->gadget)) {
392 struct f_ecm_opts *ecm_opts;
394 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
395 ecm_opts->bound = true;
397 struct f_gether_opts *geth_opts;
399 geth_opts = container_of(fi_geth, struct f_gether_opts,
401 geth_opts->bound = true;
404 rndis_borrow_net(fi_rndis, net);
407 /* TODO: gstrings_attach? */
408 ret = usb_string_ids_tab(cdev, gfs_strings);
409 if (unlikely(ret < 0))
411 gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
413 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
414 struct gfs_configuration *c = gfs_configurations + i;
415 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
417 c->c.label = gfs_strings[sid].s;
418 c->c.iConfiguration = gfs_strings[sid].id;
419 c->c.bConfigurationValue = 1 + i;
420 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
424 ret = usb_add_config(cdev, &c->c, gfs_do_config);
425 if (unlikely(ret < 0))
428 usb_composite_overwrite_options(cdev, &coverwrite);
434 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
435 usb_put_function_instance(fi_rndis);
438 #if defined CONFIG_USB_FUNCTIONFS_ETH
439 if (can_support_ecm(cdev->gadget))
440 usb_put_function_instance(fi_ecm);
442 usb_put_function_instance(fi_geth);
448 * It is assumed that gfs_unbind is called from a context where ffs_lock is held
450 static int gfs_unbind(struct usb_composite_dev *cdev)
457 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
458 usb_put_function(f_rndis);
459 usb_put_function_instance(fi_rndis);
462 #if defined CONFIG_USB_FUNCTIONFS_ETH
463 if (can_support_ecm(cdev->gadget)) {
464 usb_put_function(f_ecm);
465 usb_put_function_instance(fi_ecm);
467 usb_put_function(f_geth);
468 usb_put_function_instance(fi_geth);
471 for (i = 0; i < N_CONF * func_num; ++i)
472 usb_put_function(*(f_ffs[0] + i));
478 * It is assumed that gfs_do_config is called from a context where
481 static int gfs_do_config(struct usb_configuration *c)
483 struct gfs_configuration *gc =
484 container_of(c, struct gfs_configuration, c);
491 if (gadget_is_otg(c->cdev->gadget)) {
492 c->descriptors = gfs_otg_desc;
493 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
498 if (unlikely(ret < 0))
502 for (i = 0; i < func_num; i++) {
503 f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]);
504 if (IS_ERR(f_ffs[gc->num][i])) {
505 ret = PTR_ERR(f_ffs[gc->num][i]);
508 ret = usb_add_function(c, f_ffs[gc->num][i]);
510 usb_put_function(f_ffs[gc->num][i]);
516 * After previous do_configs there may be some invalid
517 * pointers in c->interface array. This happens every time
518 * a user space function with fewer interfaces than a user
519 * space function that was run before the new one is run. The
520 * compasit's set_config() assumes that if there is no more
521 * then MAX_CONFIG_INTERFACES interfaces in a configuration
522 * then there is a NULL pointer after the last interface in
523 * c->interface array. We need to make sure this is true.
525 if (c->next_interface_id < ARRAY_SIZE(c->interface))
526 c->interface[c->next_interface_id] = NULL;
531 if (!IS_ERR(f_ffs[gc->num][i]))
532 usb_remove_function(c, f_ffs[gc->num][i]);
533 usb_put_function(f_ffs[gc->num][i]);
538 #ifdef CONFIG_USB_FUNCTIONFS_ETH
540 static int eth_bind_config(struct usb_configuration *c)
544 if (can_support_ecm(c->cdev->gadget)) {
545 f_ecm = usb_get_function(fi_ecm);
547 return PTR_ERR(f_ecm);
549 status = usb_add_function(c, f_ecm);
551 usb_put_function(f_ecm);
554 f_geth = usb_get_function(fi_geth);
556 return PTR_ERR(f_geth);
558 status = usb_add_function(c, f_geth);
560 usb_put_function(f_geth);
567 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
569 static int bind_rndis_config(struct usb_configuration *c)
573 f_rndis = usb_get_function(fi_rndis);
575 return PTR_ERR(f_rndis);
577 status = usb_add_function(c, f_rndis);
579 usb_put_function(f_rndis);