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
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[2];
93 /* String IDs are assigned dynamically */
94 static struct usb_string gfs_strings[] = {
95 [USB_GADGET_MANUFACTURER_IDX].s = "",
96 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
97 [USB_GADGET_SERIAL_IDX].s = "",
98 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
99 { .s = "FunctionFS + RNDIS" },
101 #ifdef CONFIG_USB_FUNCTIONFS_ETH
102 { .s = "FunctionFS + ECM" },
104 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
105 { .s = "FunctionFS" },
107 { } /* end of list */
110 static struct usb_gadget_strings *gfs_dev_strings[] = {
111 &(struct usb_gadget_strings) {
112 .language = 0x0409, /* en-us */
113 .strings = gfs_strings,
118 struct gfs_configuration {
119 struct usb_configuration c;
120 int (*eth)(struct usb_configuration *c);
124 static struct gfs_configuration gfs_configurations[] = {
125 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
127 .eth = bind_rndis_config,
131 #ifdef CONFIG_USB_FUNCTIONFS_ETH
133 .eth = eth_bind_config,
137 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
143 static void *functionfs_acquire_dev(struct ffs_dev *dev);
144 static void functionfs_release_dev(struct ffs_dev *dev);
145 static int functionfs_ready_callback(struct ffs_data *ffs);
146 static void functionfs_closed_callback(struct ffs_data *ffs);
147 static int gfs_bind(struct usb_composite_dev *cdev);
148 static int gfs_unbind(struct usb_composite_dev *cdev);
149 static int gfs_do_config(struct usb_configuration *c);
152 static struct usb_composite_driver gfs_driver = {
154 .dev = &gfs_dev_desc,
155 .strings = gfs_dev_strings,
156 .max_speed = USB_SPEED_HIGH,
158 .unbind = gfs_unbind,
161 static unsigned int missing_funcs;
162 static bool gfs_registered;
163 static bool gfs_single_func;
164 static struct usb_function_instance **fi_ffs;
165 static struct usb_function **f_ffs[] = {
166 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
170 #ifdef CONFIG_USB_FUNCTIONFS_ETH
174 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
179 #define N_CONF ARRAY_SIZE(f_ffs)
181 static int __init gfs_init(void)
183 struct f_fs_opts *opts;
190 gfs_single_func = true;
195 * Allocate in one chunk for easier maintenance
197 f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL);
202 for (i = 1; i < N_CONF; ++i)
203 f_ffs[i] = f_ffs[0] + i * func_num;
205 fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL);
211 for (i = 0; i < func_num; i++) {
212 fi_ffs[i] = usb_get_function_instance("ffs");
213 if (IS_ERR(fi_ffs[i])) {
214 ret = PTR_ERR(fi_ffs[i]);
218 opts = to_f_fs_opts(fi_ffs[i]);
220 ret = ffs_single_dev(opts->dev);
222 ret = ffs_name_dev(opts->dev, func_names[i]);
225 opts->dev->ffs_ready_callback = functionfs_ready_callback;
226 opts->dev->ffs_closed_callback = functionfs_closed_callback;
227 opts->dev->ffs_acquire_dev_callback = functionfs_acquire_dev;
228 opts->dev->ffs_release_dev_callback = functionfs_release_dev;
229 opts->no_configfs = true;
232 missing_funcs = func_num;
237 usb_put_function_instance(fi_ffs[i--]);
243 module_init(gfs_init);
245 static void __exit gfs_exit(void)
252 usb_composite_unregister(&gfs_driver);
253 gfs_registered = false;
257 for (i = 0; i < func_num; i++)
258 usb_put_function_instance(fi_ffs[i]);
262 module_exit(gfs_exit);
264 static void *functionfs_acquire_dev(struct ffs_dev *dev)
266 if (!try_module_get(THIS_MODULE))
267 return ERR_PTR(-ENOENT);
272 static void functionfs_release_dev(struct ffs_dev *dev)
274 module_put(THIS_MODULE);
278 * The caller of this function takes ffs_lock
280 static int functionfs_ready_callback(struct ffs_data *ffs)
290 gfs_registered = true;
292 ret = usb_composite_probe(&gfs_driver);
293 if (unlikely(ret < 0)) {
295 gfs_registered = false;
302 * The caller of this function takes ffs_lock
304 static void functionfs_closed_callback(struct ffs_data *ffs)
309 usb_composite_unregister(&gfs_driver);
310 gfs_registered = false;
314 * It is assumed that gfs_bind is called from a context where ffs_lock is held
316 static int gfs_bind(struct usb_composite_dev *cdev)
318 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
319 struct net_device *net;
327 #if defined CONFIG_USB_FUNCTIONFS_ETH
328 if (can_support_ecm(cdev->gadget)) {
329 struct f_ecm_opts *ecm_opts;
331 fi_ecm = usb_get_function_instance("ecm");
333 return PTR_ERR(fi_ecm);
334 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
337 struct f_gether_opts *geth_opts;
339 fi_geth = usb_get_function_instance("geth");
341 return PTR_ERR(fi_geth);
342 geth_opts = container_of(fi_geth, struct f_gether_opts,
344 net = geth_opts->net;
348 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
350 struct f_rndis_opts *rndis_opts;
352 fi_rndis = usb_get_function_instance("rndis");
353 if (IS_ERR(fi_rndis)) {
354 ret = PTR_ERR(fi_rndis);
357 rndis_opts = container_of(fi_rndis, struct f_rndis_opts,
359 #ifndef CONFIG_USB_FUNCTIONFS_ETH
360 net = rndis_opts->net;
365 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
366 gether_set_qmult(net, qmult);
367 if (!gether_set_host_addr(net, host_addr))
368 pr_info("using host ethernet address: %s", host_addr);
369 if (!gether_set_dev_addr(net, dev_addr))
370 pr_info("using self ethernet address: %s", dev_addr);
373 #if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
374 gether_set_gadget(net, cdev->gadget);
375 ret = gether_register_netdev(net);
379 if (can_support_ecm(cdev->gadget)) {
380 struct f_ecm_opts *ecm_opts;
382 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
383 ecm_opts->bound = true;
385 struct f_gether_opts *geth_opts;
387 geth_opts = container_of(fi_geth, struct f_gether_opts,
389 geth_opts->bound = true;
392 rndis_borrow_net(fi_rndis, net);
395 /* TODO: gstrings_attach? */
396 ret = usb_string_ids_tab(cdev, gfs_strings);
397 if (unlikely(ret < 0))
399 gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
401 if (gadget_is_otg(cdev->gadget) && !gfs_otg_desc[0]) {
402 struct usb_descriptor_header *usb_desc;
404 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
407 usb_otg_descriptor_init(cdev->gadget, usb_desc);
408 gfs_otg_desc[0] = usb_desc;
409 gfs_otg_desc[1] = NULL;
412 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
413 struct gfs_configuration *c = gfs_configurations + i;
414 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
416 c->c.label = gfs_strings[sid].s;
417 c->c.iConfiguration = gfs_strings[sid].id;
418 c->c.bConfigurationValue = 1 + i;
419 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
423 ret = usb_add_config(cdev, &c->c, gfs_do_config);
424 if (unlikely(ret < 0))
427 usb_composite_overwrite_options(cdev, &coverwrite);
432 kfree(gfs_otg_desc[0]);
433 gfs_otg_desc[0] = NULL;
435 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
436 usb_put_function_instance(fi_rndis);
439 #if defined CONFIG_USB_FUNCTIONFS_ETH
440 if (can_support_ecm(cdev->gadget))
441 usb_put_function_instance(fi_ecm);
443 usb_put_function_instance(fi_geth);
449 * It is assumed that gfs_unbind is called from a context where ffs_lock is held
451 static int gfs_unbind(struct usb_composite_dev *cdev)
458 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
459 usb_put_function(f_rndis);
460 usb_put_function_instance(fi_rndis);
463 #if defined CONFIG_USB_FUNCTIONFS_ETH
464 if (can_support_ecm(cdev->gadget)) {
465 usb_put_function(f_ecm);
466 usb_put_function_instance(fi_ecm);
468 usb_put_function(f_geth);
469 usb_put_function_instance(fi_geth);
472 for (i = 0; i < N_CONF * func_num; ++i)
473 usb_put_function(*(f_ffs[0] + i));
475 kfree(gfs_otg_desc[0]);
476 gfs_otg_desc[0] = NULL;
482 * It is assumed that gfs_do_config is called from a context where
485 static int gfs_do_config(struct usb_configuration *c)
487 struct gfs_configuration *gc =
488 container_of(c, struct gfs_configuration, c);
495 if (gadget_is_otg(c->cdev->gadget)) {
496 c->descriptors = gfs_otg_desc;
497 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
502 if (unlikely(ret < 0))
506 for (i = 0; i < func_num; i++) {
507 f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]);
508 if (IS_ERR(f_ffs[gc->num][i])) {
509 ret = PTR_ERR(f_ffs[gc->num][i]);
512 ret = usb_add_function(c, f_ffs[gc->num][i]);
514 usb_put_function(f_ffs[gc->num][i]);
520 * After previous do_configs there may be some invalid
521 * pointers in c->interface array. This happens every time
522 * a user space function with fewer interfaces than a user
523 * space function that was run before the new one is run. The
524 * compasit's set_config() assumes that if there is no more
525 * then MAX_CONFIG_INTERFACES interfaces in a configuration
526 * then there is a NULL pointer after the last interface in
527 * c->interface array. We need to make sure this is true.
529 if (c->next_interface_id < ARRAY_SIZE(c->interface))
530 c->interface[c->next_interface_id] = NULL;
535 if (!IS_ERR(f_ffs[gc->num][i]))
536 usb_remove_function(c, f_ffs[gc->num][i]);
537 usb_put_function(f_ffs[gc->num][i]);
542 #ifdef CONFIG_USB_FUNCTIONFS_ETH
544 static int eth_bind_config(struct usb_configuration *c)
548 if (can_support_ecm(c->cdev->gadget)) {
549 f_ecm = usb_get_function(fi_ecm);
551 return PTR_ERR(f_ecm);
553 status = usb_add_function(c, f_ecm);
555 usb_put_function(f_ecm);
558 f_geth = usb_get_function(fi_geth);
560 return PTR_ERR(f_geth);
562 status = usb_add_function(c, f_geth);
564 usb_put_function(f_geth);
571 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
573 static int bind_rndis_config(struct usb_configuration *c)
577 f_rndis = usb_get_function(fi_rndis);
579 return PTR_ERR(f_rndis);
581 status = usb_add_function(c, f_rndis);
583 usb_put_function(f_rndis);