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
32 # include "f_subset.c"
39 static u8 gfs_host_mac[ETH_ALEN];
40 static struct eth_dev *the_dev;
41 # ifdef CONFIG_USB_FUNCTIONFS_ETH
42 static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
47 # define gether_cleanup(dev) do { } while (0)
48 # define gfs_host_mac NULL
54 #define DRIVER_NAME "g_ffs"
55 #define DRIVER_DESC "USB Function Filesystem"
56 #define DRIVER_VERSION "24 Aug 2004"
58 MODULE_DESCRIPTION(DRIVER_DESC);
59 MODULE_AUTHOR("Michal Nazarewicz");
60 MODULE_LICENSE("GPL");
62 #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
63 #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
65 #define GFS_MAX_DEVS 10
71 struct ffs_data *ffs_data;
74 USB_GADGET_COMPOSITE_OPTIONS();
76 USB_ETHERNET_MODULE_PARAMETERS();
78 static struct usb_device_descriptor gfs_dev_desc = {
79 .bLength = sizeof gfs_dev_desc,
80 .bDescriptorType = USB_DT_DEVICE,
82 .bcdUSB = cpu_to_le16(0x0200),
83 .bDeviceClass = USB_CLASS_PER_INTERFACE,
85 .idVendor = cpu_to_le16(GFS_VENDOR_ID),
86 .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
89 static char *func_names[GFS_MAX_DEVS];
90 static unsigned int func_num;
92 module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
93 MODULE_PARM_DESC(bDeviceClass, "USB Device class");
94 module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
95 MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
96 module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
97 MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
98 module_param_array_named(functions, func_names, charp, &func_num, 0);
99 MODULE_PARM_DESC(functions, "USB Functions list");
101 static const struct usb_descriptor_header *gfs_otg_desc[] = {
102 (const struct usb_descriptor_header *)
103 &(const struct usb_otg_descriptor) {
104 .bLength = sizeof(struct usb_otg_descriptor),
105 .bDescriptorType = USB_DT_OTG,
108 * REVISIT SRP-only hardware is possible, although
109 * it would not be called "OTG" ...
111 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
117 /* String IDs are assigned dynamically */
118 static struct usb_string gfs_strings[] = {
119 [USB_GADGET_MANUFACTURER_IDX].s = "",
120 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
121 [USB_GADGET_SERIAL_IDX].s = "",
122 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
123 { .s = "FunctionFS + RNDIS" },
125 #ifdef CONFIG_USB_FUNCTIONFS_ETH
126 { .s = "FunctionFS + ECM" },
128 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
129 { .s = "FunctionFS" },
131 { } /* end of list */
134 static struct usb_gadget_strings *gfs_dev_strings[] = {
135 &(struct usb_gadget_strings) {
136 .language = 0x0409, /* en-us */
137 .strings = gfs_strings,
142 struct gfs_configuration {
143 struct usb_configuration c;
144 int (*eth)(struct usb_configuration *c, u8 *ethaddr,
145 struct eth_dev *dev);
146 } gfs_configurations[] = {
147 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
149 .eth = rndis_bind_config,
153 #ifdef CONFIG_USB_FUNCTIONFS_ETH
155 .eth = eth_bind_config,
159 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
165 static int gfs_bind(struct usb_composite_dev *cdev);
166 static int gfs_unbind(struct usb_composite_dev *cdev);
167 static int gfs_do_config(struct usb_configuration *c);
169 static __refdata struct usb_composite_driver gfs_driver = {
171 .dev = &gfs_dev_desc,
172 .strings = gfs_dev_strings,
173 .max_speed = USB_SPEED_HIGH,
175 .unbind = gfs_unbind,
178 static DEFINE_MUTEX(gfs_lock);
179 static unsigned int missing_funcs;
180 static bool gfs_ether_setup;
181 static bool gfs_registered;
182 static bool gfs_single_func;
183 static struct gfs_ffs_obj *ffs_tab;
185 static int __init gfs_init(void)
192 gfs_single_func = true;
196 ffs_tab = kcalloc(func_num, sizeof *ffs_tab, GFP_KERNEL);
200 if (!gfs_single_func)
201 for (i = 0; i < func_num; i++)
202 ffs_tab[i].name = func_names[i];
204 missing_funcs = func_num;
206 return functionfs_init();
208 module_init(gfs_init);
210 static void __exit gfs_exit(void)
213 mutex_lock(&gfs_lock);
216 usb_composite_unregister(&gfs_driver);
217 gfs_registered = false;
219 functionfs_cleanup();
221 mutex_unlock(&gfs_lock);
224 module_exit(gfs_exit);
226 static struct gfs_ffs_obj *gfs_find_dev(const char *dev_name)
235 for (i = 0; i < func_num; i++)
236 if (strcmp(ffs_tab[i].name, dev_name) == 0)
242 static int functionfs_ready_callback(struct ffs_data *ffs)
244 struct gfs_ffs_obj *ffs_obj;
248 mutex_lock(&gfs_lock);
250 ffs_obj = ffs->private_data;
256 if (WARN_ON(ffs_obj->desc_ready)) {
260 ffs_obj->desc_ready = true;
261 ffs_obj->ffs_data = ffs;
263 if (--missing_funcs) {
268 if (gfs_registered) {
272 gfs_registered = true;
274 ret = usb_composite_probe(&gfs_driver);
275 if (unlikely(ret < 0))
276 gfs_registered = false;
279 mutex_unlock(&gfs_lock);
283 static void functionfs_closed_callback(struct ffs_data *ffs)
285 struct gfs_ffs_obj *ffs_obj;
288 mutex_lock(&gfs_lock);
290 ffs_obj = ffs->private_data;
294 ffs_obj->desc_ready = false;
298 usb_composite_unregister(&gfs_driver);
299 gfs_registered = false;
302 mutex_unlock(&gfs_lock);
305 static void *functionfs_acquire_dev_callback(const char *dev_name)
307 struct gfs_ffs_obj *ffs_dev;
310 mutex_lock(&gfs_lock);
312 ffs_dev = gfs_find_dev(dev_name);
314 ffs_dev = ERR_PTR(-ENODEV);
318 if (ffs_dev->mounted) {
319 ffs_dev = ERR_PTR(-EBUSY);
322 ffs_dev->mounted = true;
325 mutex_unlock(&gfs_lock);
329 static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
331 struct gfs_ffs_obj *ffs_dev;
334 mutex_lock(&gfs_lock);
336 ffs_dev = ffs_data->private_data;
338 ffs_dev->mounted = false;
340 mutex_unlock(&gfs_lock);
344 * It is assumed that gfs_bind is called from a context where gfs_lock is held
346 static int gfs_bind(struct usb_composite_dev *cdev)
354 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
355 the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, gfs_host_mac,
358 if (IS_ERR(the_dev)) {
359 ret = PTR_ERR(the_dev);
362 gfs_ether_setup = true;
364 ret = usb_string_ids_tab(cdev, gfs_strings);
365 if (unlikely(ret < 0))
367 gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
369 for (i = func_num; i--; ) {
370 ret = functionfs_bind(ffs_tab[i].ffs_data, cdev);
371 if (unlikely(ret < 0)) {
372 while (++i < func_num)
373 functionfs_unbind(ffs_tab[i].ffs_data);
378 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
379 struct gfs_configuration *c = gfs_configurations + i;
380 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
382 c->c.label = gfs_strings[sid].s;
383 c->c.iConfiguration = gfs_strings[sid].id;
384 c->c.bConfigurationValue = 1 + i;
385 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
387 ret = usb_add_config(cdev, &c->c, gfs_do_config);
388 if (unlikely(ret < 0))
391 usb_composite_overwrite_options(cdev, &coverwrite);
395 for (i = 0; i < func_num; i++)
396 functionfs_unbind(ffs_tab[i].ffs_data);
398 gether_cleanup(the_dev);
400 gfs_ether_setup = false;
405 * It is assumed that gfs_unbind is called from a context where gfs_lock is held
407 static int gfs_unbind(struct usb_composite_dev *cdev)
414 * We may have been called in an error recovery from
415 * composite_bind() after gfs_unbind() failure so we need to
416 * check if gfs_ffs_data is not NULL since gfs_bind() handles
417 * all error recovery itself. I'd rather we werent called
418 * from composite on orror recovery, but what you're gonna
422 gether_cleanup(the_dev);
423 gfs_ether_setup = false;
425 for (i = func_num; i--; )
426 if (ffs_tab[i].ffs_data)
427 functionfs_unbind(ffs_tab[i].ffs_data);
433 * It is assumed that gfs_do_config is called from a context where
436 static int gfs_do_config(struct usb_configuration *c)
438 struct gfs_configuration *gc =
439 container_of(c, struct gfs_configuration, c);
446 if (gadget_is_otg(c->cdev->gadget)) {
447 c->descriptors = gfs_otg_desc;
448 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
452 ret = gc->eth(c, gfs_host_mac, the_dev);
453 if (unlikely(ret < 0))
457 for (i = 0; i < func_num; i++) {
458 ret = functionfs_bind_config(c->cdev, c, ffs_tab[i].ffs_data);
459 if (unlikely(ret < 0))
464 * After previous do_configs there may be some invalid
465 * pointers in c->interface array. This happens every time
466 * a user space function with fewer interfaces than a user
467 * space function that was run before the new one is run. The
468 * compasit's set_config() assumes that if there is no more
469 * then MAX_CONFIG_INTERFACES interfaces in a configuration
470 * then there is a NULL pointer after the last interface in
471 * c->interface array. We need to make sure this is true.
473 if (c->next_interface_id < ARRAY_SIZE(c->interface))
474 c->interface[c->next_interface_id] = NULL;
479 #ifdef CONFIG_USB_FUNCTIONFS_ETH
481 static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
484 return can_support_ecm(c->cdev->gadget)
485 ? ecm_bind_config(c, ethaddr, dev)
486 : geth_bind_config(c, ethaddr, dev);