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[] = {
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);
136 } gfs_configurations[] = {
137 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
139 .eth = bind_rndis_config,
143 #ifdef CONFIG_USB_FUNCTIONFS_ETH
145 .eth = eth_bind_config,
149 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
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 = {
166 .dev = &gfs_dev_desc,
167 .strings = gfs_dev_strings,
168 .max_speed = USB_SPEED_HIGH,
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
182 #ifdef CONFIG_USB_FUNCTIONFS_ETH
186 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
191 #define N_CONF ARRAY_SIZE(f_ffs)
193 static int __init gfs_init(void)
195 struct f_fs_opts *opts;
202 gfs_single_func = true;
207 * Allocate in one chunk for easier maintenance
209 f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL);
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);
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]);
230 opts = to_f_fs_opts(fi_ffs[i]);
232 ret = ffs_single_dev(opts->dev);
234 ret = ffs_name_dev(opts->dev, func_names[i]);
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;
249 usb_put_function_instance(fi_ffs[i--]);
255 module_init(gfs_init);
257 static void __exit gfs_exit(void)
264 usb_composite_unregister(&gfs_driver);
265 gfs_registered = false;
269 for (i = 0; i < func_num; i++)
270 usb_put_function_instance(fi_ffs[i]);
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);
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)
302 gfs_registered = true;
304 ret = usb_composite_probe(&gfs_driver);
305 if (unlikely(ret < 0))
306 gfs_registered = false;
312 * The caller of this function takes ffs_lock
314 static void functionfs_closed_callback(struct ffs_data *ffs)
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;
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");
343 return PTR_ERR(fi_ecm);
344 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
347 struct f_gether_opts *geth_opts;
349 fi_geth = usb_get_function_instance("geth");
351 return PTR_ERR(fi_geth);
352 geth_opts = container_of(fi_geth, struct f_gether_opts,
354 net = geth_opts->net;
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);
367 rndis_opts = container_of(fi_rndis, struct f_rndis_opts,
369 #ifndef CONFIG_USB_FUNCTIONFS_ETH
370 net = rndis_opts->net;
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);
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);
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;
395 struct f_gether_opts *geth_opts;
397 geth_opts = container_of(fi_geth, struct f_gether_opts,
399 geth_opts->bound = true;
402 rndis_borrow_net(fi_rndis, net);
405 /* TODO: gstrings_attach? */
406 ret = usb_string_ids_tab(cdev, gfs_strings);
407 if (unlikely(ret < 0))
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;
422 ret = usb_add_config(cdev, &c->c, gfs_do_config);
423 if (unlikely(ret < 0))
426 usb_composite_overwrite_options(cdev, &coverwrite);
432 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
433 usb_put_function_instance(fi_rndis);
436 #if defined CONFIG_USB_FUNCTIONFS_ETH
437 if (can_support_ecm(cdev->gadget))
438 usb_put_function_instance(fi_ecm);
440 usb_put_function_instance(fi_geth);
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)
455 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
456 usb_put_function(f_rndis);
457 usb_put_function_instance(fi_rndis);
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);
465 usb_put_function(f_geth);
466 usb_put_function_instance(fi_geth);
469 for (i = 0; i < N_CONF * func_num; ++i)
470 usb_put_function(*(f_ffs[0] + i));
476 * It is assumed that gfs_do_config is called from a context where
479 static int gfs_do_config(struct usb_configuration *c)
481 struct gfs_configuration *gc =
482 container_of(c, struct gfs_configuration, c);
489 if (gadget_is_otg(c->cdev->gadget)) {
490 c->descriptors = gfs_otg_desc;
491 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
496 if (unlikely(ret < 0))
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]);
506 ret = usb_add_function(c, f_ffs[gc->num][i]);
508 usb_put_function(f_ffs[gc->num][i]);
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;
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]);
536 #ifdef CONFIG_USB_FUNCTIONFS_ETH
538 static int eth_bind_config(struct usb_configuration *c)
542 if (can_support_ecm(c->cdev->gadget)) {
543 f_ecm = usb_get_function(fi_ecm);
545 return PTR_ERR(f_ecm);
547 status = usb_add_function(c, f_ecm);
549 usb_put_function(f_ecm);
552 f_geth = usb_get_function(fi_geth);
554 return PTR_ERR(f_geth);
556 status = usb_add_function(c, f_geth);
558 usb_put_function(f_geth);
565 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
567 static int bind_rndis_config(struct usb_configuration *c)
571 f_rndis = usb_get_function(fi_rndis);
573 return PTR_ERR(f_rndis);
575 status = usb_add_function(c, f_rndis);
577 usb_put_function(f_rndis);