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 * kbuild is not very cooperative with respect to linking separately
18 * compiled library objects into one module. So for now we won't use
19 * separate compilation ... ensuring init/exit sections work to shrink
20 * the runtime footprint, and giving us at least some parts of what
21 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
23 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
24 # if defined USB_ETH_RNDIS
27 # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
28 # define USB_ETH_RNDIS y
31 #define USBF_ECM_INCLUDED
33 #define USB_FSUBSET_INCLUDED
34 # include "f_subset.c"
36 # define USB_FRNDIS_INCLUDED
42 static u8 gfs_host_mac[ETH_ALEN];
43 static struct eth_dev *the_dev;
44 # ifdef CONFIG_USB_FUNCTIONFS_ETH
45 static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
50 # define gether_cleanup(dev) do { } while (0)
51 # define gfs_host_mac NULL
57 #define DRIVER_NAME "g_ffs"
58 #define DRIVER_DESC "USB Function Filesystem"
59 #define DRIVER_VERSION "24 Aug 2004"
61 MODULE_DESCRIPTION(DRIVER_DESC);
62 MODULE_AUTHOR("Michal Nazarewicz");
63 MODULE_LICENSE("GPL");
65 #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
66 #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
68 #define GFS_MAX_DEVS 10
74 struct ffs_data *ffs_data;
77 USB_GADGET_COMPOSITE_OPTIONS();
79 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
80 USB_ETHERNET_MODULE_PARAMETERS();
83 static struct usb_device_descriptor gfs_dev_desc = {
84 .bLength = sizeof gfs_dev_desc,
85 .bDescriptorType = USB_DT_DEVICE,
87 .bcdUSB = cpu_to_le16(0x0200),
88 .bDeviceClass = USB_CLASS_PER_INTERFACE,
90 .idVendor = cpu_to_le16(GFS_VENDOR_ID),
91 .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
94 static char *func_names[GFS_MAX_DEVS];
95 static unsigned int func_num;
97 module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
98 MODULE_PARM_DESC(bDeviceClass, "USB Device class");
99 module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
100 MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
101 module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
102 MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
103 module_param_array_named(functions, func_names, charp, &func_num, 0);
104 MODULE_PARM_DESC(functions, "USB Functions list");
106 static const struct usb_descriptor_header *gfs_otg_desc[] = {
107 (const struct usb_descriptor_header *)
108 &(const struct usb_otg_descriptor) {
109 .bLength = sizeof(struct usb_otg_descriptor),
110 .bDescriptorType = USB_DT_OTG,
113 * REVISIT SRP-only hardware is possible, although
114 * it would not be called "OTG" ...
116 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
122 /* String IDs are assigned dynamically */
123 static struct usb_string gfs_strings[] = {
124 [USB_GADGET_MANUFACTURER_IDX].s = "",
125 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
126 [USB_GADGET_SERIAL_IDX].s = "",
127 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
128 { .s = "FunctionFS + RNDIS" },
130 #ifdef CONFIG_USB_FUNCTIONFS_ETH
131 { .s = "FunctionFS + ECM" },
133 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
134 { .s = "FunctionFS" },
136 { } /* end of list */
139 static struct usb_gadget_strings *gfs_dev_strings[] = {
140 &(struct usb_gadget_strings) {
141 .language = 0x0409, /* en-us */
142 .strings = gfs_strings,
147 struct gfs_configuration {
148 struct usb_configuration c;
149 int (*eth)(struct usb_configuration *c, u8 *ethaddr,
150 struct eth_dev *dev);
151 } gfs_configurations[] = {
152 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
154 .eth = rndis_bind_config,
158 #ifdef CONFIG_USB_FUNCTIONFS_ETH
160 .eth = eth_bind_config,
164 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
170 static int gfs_bind(struct usb_composite_dev *cdev);
171 static int gfs_unbind(struct usb_composite_dev *cdev);
172 static int gfs_do_config(struct usb_configuration *c);
174 static __refdata struct usb_composite_driver gfs_driver = {
176 .dev = &gfs_dev_desc,
177 .strings = gfs_dev_strings,
178 .max_speed = USB_SPEED_HIGH,
180 .unbind = gfs_unbind,
183 static DEFINE_MUTEX(gfs_lock);
184 static unsigned int missing_funcs;
185 static bool gfs_ether_setup;
186 static bool gfs_registered;
187 static bool gfs_single_func;
188 static struct gfs_ffs_obj *ffs_tab;
190 static int __init gfs_init(void)
197 gfs_single_func = true;
201 ffs_tab = kcalloc(func_num, sizeof *ffs_tab, GFP_KERNEL);
205 if (!gfs_single_func)
206 for (i = 0; i < func_num; i++)
207 ffs_tab[i].name = func_names[i];
209 missing_funcs = func_num;
211 return functionfs_init();
213 module_init(gfs_init);
215 static void __exit gfs_exit(void)
218 mutex_lock(&gfs_lock);
221 usb_composite_unregister(&gfs_driver);
222 gfs_registered = false;
224 functionfs_cleanup();
226 mutex_unlock(&gfs_lock);
229 module_exit(gfs_exit);
231 static struct gfs_ffs_obj *gfs_find_dev(const char *dev_name)
240 for (i = 0; i < func_num; i++)
241 if (strcmp(ffs_tab[i].name, dev_name) == 0)
247 static int functionfs_ready_callback(struct ffs_data *ffs)
249 struct gfs_ffs_obj *ffs_obj;
253 mutex_lock(&gfs_lock);
255 ffs_obj = ffs->private_data;
261 if (WARN_ON(ffs_obj->desc_ready)) {
265 ffs_obj->desc_ready = true;
266 ffs_obj->ffs_data = ffs;
268 if (--missing_funcs) {
273 if (gfs_registered) {
277 gfs_registered = true;
279 ret = usb_composite_probe(&gfs_driver);
280 if (unlikely(ret < 0))
281 gfs_registered = false;
284 mutex_unlock(&gfs_lock);
288 static void functionfs_closed_callback(struct ffs_data *ffs)
290 struct gfs_ffs_obj *ffs_obj;
293 mutex_lock(&gfs_lock);
295 ffs_obj = ffs->private_data;
299 ffs_obj->desc_ready = false;
303 usb_composite_unregister(&gfs_driver);
304 gfs_registered = false;
307 mutex_unlock(&gfs_lock);
310 static void *functionfs_acquire_dev_callback(const char *dev_name)
312 struct gfs_ffs_obj *ffs_dev;
315 mutex_lock(&gfs_lock);
317 ffs_dev = gfs_find_dev(dev_name);
319 ffs_dev = ERR_PTR(-ENODEV);
323 if (ffs_dev->mounted) {
324 ffs_dev = ERR_PTR(-EBUSY);
327 ffs_dev->mounted = true;
330 mutex_unlock(&gfs_lock);
334 static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
336 struct gfs_ffs_obj *ffs_dev;
339 mutex_lock(&gfs_lock);
341 ffs_dev = ffs_data->private_data;
343 ffs_dev->mounted = false;
345 mutex_unlock(&gfs_lock);
349 * It is assumed that gfs_bind is called from a context where gfs_lock is held
351 static int gfs_bind(struct usb_composite_dev *cdev)
359 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
360 the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, gfs_host_mac,
363 if (IS_ERR(the_dev)) {
364 ret = PTR_ERR(the_dev);
367 gfs_ether_setup = true;
369 ret = usb_string_ids_tab(cdev, gfs_strings);
370 if (unlikely(ret < 0))
372 gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
374 for (i = func_num; i--; ) {
375 ret = functionfs_bind(ffs_tab[i].ffs_data, cdev);
376 if (unlikely(ret < 0)) {
377 while (++i < func_num)
378 functionfs_unbind(ffs_tab[i].ffs_data);
383 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
384 struct gfs_configuration *c = gfs_configurations + i;
385 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
387 c->c.label = gfs_strings[sid].s;
388 c->c.iConfiguration = gfs_strings[sid].id;
389 c->c.bConfigurationValue = 1 + i;
390 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
392 ret = usb_add_config(cdev, &c->c, gfs_do_config);
393 if (unlikely(ret < 0))
396 usb_composite_overwrite_options(cdev, &coverwrite);
400 for (i = 0; i < func_num; i++)
401 functionfs_unbind(ffs_tab[i].ffs_data);
403 gether_cleanup(the_dev);
405 gfs_ether_setup = false;
410 * It is assumed that gfs_unbind is called from a context where gfs_lock is held
412 static int gfs_unbind(struct usb_composite_dev *cdev)
419 * We may have been called in an error recovery from
420 * composite_bind() after gfs_unbind() failure so we need to
421 * check if gfs_ffs_data is not NULL since gfs_bind() handles
422 * all error recovery itself. I'd rather we werent called
423 * from composite on orror recovery, but what you're gonna
427 gether_cleanup(the_dev);
428 gfs_ether_setup = false;
430 for (i = func_num; i--; )
431 if (ffs_tab[i].ffs_data)
432 functionfs_unbind(ffs_tab[i].ffs_data);
438 * It is assumed that gfs_do_config is called from a context where
441 static int gfs_do_config(struct usb_configuration *c)
443 struct gfs_configuration *gc =
444 container_of(c, struct gfs_configuration, c);
451 if (gadget_is_otg(c->cdev->gadget)) {
452 c->descriptors = gfs_otg_desc;
453 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
457 ret = gc->eth(c, gfs_host_mac, the_dev);
458 if (unlikely(ret < 0))
462 for (i = 0; i < func_num; i++) {
463 ret = functionfs_bind_config(c->cdev, c, ffs_tab[i].ffs_data);
464 if (unlikely(ret < 0))
469 * After previous do_configs there may be some invalid
470 * pointers in c->interface array. This happens every time
471 * a user space function with fewer interfaces than a user
472 * space function that was run before the new one is run. The
473 * compasit's set_config() assumes that if there is no more
474 * then MAX_CONFIG_INTERFACES interfaces in a configuration
475 * then there is a NULL pointer after the last interface in
476 * c->interface array. We need to make sure this is true.
478 if (c->next_interface_id < ARRAY_SIZE(c->interface))
479 c->interface[c->next_interface_id] = NULL;
484 #ifdef CONFIG_USB_FUNCTIONFS_ETH
486 static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
489 return can_support_ecm(c->cdev->gadget)
490 ? ecm_bind_config(c, ethaddr, dev)
491 : geth_bind_config(c, ethaddr, dev);