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 USB_ETHERNET_MODULE_PARAMETERS();
81 static struct usb_device_descriptor gfs_dev_desc
= {
82 .bLength
= sizeof gfs_dev_desc
,
83 .bDescriptorType
= USB_DT_DEVICE
,
85 .bcdUSB
= cpu_to_le16(0x0200),
86 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
88 .idVendor
= cpu_to_le16(GFS_VENDOR_ID
),
89 .idProduct
= cpu_to_le16(GFS_PRODUCT_ID
),
92 static char *func_names
[GFS_MAX_DEVS
];
93 static unsigned int func_num
;
95 module_param_named(bDeviceClass
, gfs_dev_desc
.bDeviceClass
, byte
, 0644);
96 MODULE_PARM_DESC(bDeviceClass
, "USB Device class");
97 module_param_named(bDeviceSubClass
, gfs_dev_desc
.bDeviceSubClass
, byte
, 0644);
98 MODULE_PARM_DESC(bDeviceSubClass
, "USB Device subclass");
99 module_param_named(bDeviceProtocol
, gfs_dev_desc
.bDeviceProtocol
, byte
, 0644);
100 MODULE_PARM_DESC(bDeviceProtocol
, "USB Device protocol");
101 module_param_array_named(functions
, func_names
, charp
, &func_num
, 0);
102 MODULE_PARM_DESC(functions
, "USB Functions list");
104 static const struct usb_descriptor_header
*gfs_otg_desc
[] = {
105 (const struct usb_descriptor_header
*)
106 &(const struct usb_otg_descriptor
) {
107 .bLength
= sizeof(struct usb_otg_descriptor
),
108 .bDescriptorType
= USB_DT_OTG
,
111 * REVISIT SRP-only hardware is possible, although
112 * it would not be called "OTG" ...
114 .bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
,
120 /* String IDs are assigned dynamically */
121 static struct usb_string gfs_strings
[] = {
122 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
123 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
124 [USB_GADGET_SERIAL_IDX
].s
= "",
125 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
126 { .s
= "FunctionFS + RNDIS" },
128 #ifdef CONFIG_USB_FUNCTIONFS_ETH
129 { .s
= "FunctionFS + ECM" },
131 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
132 { .s
= "FunctionFS" },
134 { } /* end of list */
137 static struct usb_gadget_strings
*gfs_dev_strings
[] = {
138 &(struct usb_gadget_strings
) {
139 .language
= 0x0409, /* en-us */
140 .strings
= gfs_strings
,
145 struct gfs_configuration
{
146 struct usb_configuration c
;
147 int (*eth
)(struct usb_configuration
*c
, u8
*ethaddr
,
148 struct eth_dev
*dev
);
149 } gfs_configurations
[] = {
150 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
152 .eth
= rndis_bind_config
,
156 #ifdef CONFIG_USB_FUNCTIONFS_ETH
158 .eth
= eth_bind_config
,
162 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
168 static int gfs_bind(struct usb_composite_dev
*cdev
);
169 static int gfs_unbind(struct usb_composite_dev
*cdev
);
170 static int gfs_do_config(struct usb_configuration
*c
);
172 static __refdata
struct usb_composite_driver gfs_driver
= {
174 .dev
= &gfs_dev_desc
,
175 .strings
= gfs_dev_strings
,
176 .max_speed
= USB_SPEED_HIGH
,
178 .unbind
= gfs_unbind
,
181 static DEFINE_MUTEX(gfs_lock
);
182 static unsigned int missing_funcs
;
183 static bool gfs_ether_setup
;
184 static bool gfs_registered
;
185 static bool gfs_single_func
;
186 static struct gfs_ffs_obj
*ffs_tab
;
188 static int __init
gfs_init(void)
195 gfs_single_func
= true;
199 ffs_tab
= kcalloc(func_num
, sizeof *ffs_tab
, GFP_KERNEL
);
203 if (!gfs_single_func
)
204 for (i
= 0; i
< func_num
; i
++)
205 ffs_tab
[i
].name
= func_names
[i
];
207 missing_funcs
= func_num
;
209 return functionfs_init();
211 module_init(gfs_init
);
213 static void __exit
gfs_exit(void)
216 mutex_lock(&gfs_lock
);
219 usb_composite_unregister(&gfs_driver
);
220 gfs_registered
= false;
222 functionfs_cleanup();
224 mutex_unlock(&gfs_lock
);
227 module_exit(gfs_exit
);
229 static struct gfs_ffs_obj
*gfs_find_dev(const char *dev_name
)
238 for (i
= 0; i
< func_num
; i
++)
239 if (strcmp(ffs_tab
[i
].name
, dev_name
) == 0)
245 static int functionfs_ready_callback(struct ffs_data
*ffs
)
247 struct gfs_ffs_obj
*ffs_obj
;
251 mutex_lock(&gfs_lock
);
253 ffs_obj
= ffs
->private_data
;
259 if (WARN_ON(ffs_obj
->desc_ready
)) {
263 ffs_obj
->desc_ready
= true;
264 ffs_obj
->ffs_data
= ffs
;
266 if (--missing_funcs
) {
271 if (gfs_registered
) {
275 gfs_registered
= true;
277 ret
= usb_composite_probe(&gfs_driver
);
278 if (unlikely(ret
< 0))
279 gfs_registered
= false;
282 mutex_unlock(&gfs_lock
);
286 static void functionfs_closed_callback(struct ffs_data
*ffs
)
288 struct gfs_ffs_obj
*ffs_obj
;
291 mutex_lock(&gfs_lock
);
293 ffs_obj
= ffs
->private_data
;
297 ffs_obj
->desc_ready
= false;
301 usb_composite_unregister(&gfs_driver
);
302 gfs_registered
= false;
305 mutex_unlock(&gfs_lock
);
308 static void *functionfs_acquire_dev_callback(const char *dev_name
)
310 struct gfs_ffs_obj
*ffs_dev
;
313 mutex_lock(&gfs_lock
);
315 ffs_dev
= gfs_find_dev(dev_name
);
317 ffs_dev
= ERR_PTR(-ENODEV
);
321 if (ffs_dev
->mounted
) {
322 ffs_dev
= ERR_PTR(-EBUSY
);
325 ffs_dev
->mounted
= true;
328 mutex_unlock(&gfs_lock
);
332 static void functionfs_release_dev_callback(struct ffs_data
*ffs_data
)
334 struct gfs_ffs_obj
*ffs_dev
;
337 mutex_lock(&gfs_lock
);
339 ffs_dev
= ffs_data
->private_data
;
341 ffs_dev
->mounted
= false;
343 mutex_unlock(&gfs_lock
);
347 * It is assumed that gfs_bind is called from a context where gfs_lock is held
349 static int gfs_bind(struct usb_composite_dev
*cdev
)
357 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
358 the_dev
= gether_setup(cdev
->gadget
, dev_addr
, host_addr
, gfs_host_mac
,
361 if (IS_ERR(the_dev
)) {
362 ret
= PTR_ERR(the_dev
);
365 gfs_ether_setup
= true;
367 ret
= usb_string_ids_tab(cdev
, gfs_strings
);
368 if (unlikely(ret
< 0))
370 gfs_dev_desc
.iProduct
= gfs_strings
[USB_GADGET_PRODUCT_IDX
].id
;
372 for (i
= func_num
; i
--; ) {
373 ret
= functionfs_bind(ffs_tab
[i
].ffs_data
, cdev
);
374 if (unlikely(ret
< 0)) {
375 while (++i
< func_num
)
376 functionfs_unbind(ffs_tab
[i
].ffs_data
);
381 for (i
= 0; i
< ARRAY_SIZE(gfs_configurations
); ++i
) {
382 struct gfs_configuration
*c
= gfs_configurations
+ i
;
383 int sid
= USB_GADGET_FIRST_AVAIL_IDX
+ i
;
385 c
->c
.label
= gfs_strings
[sid
].s
;
386 c
->c
.iConfiguration
= gfs_strings
[sid
].id
;
387 c
->c
.bConfigurationValue
= 1 + i
;
388 c
->c
.bmAttributes
= USB_CONFIG_ATT_SELFPOWER
;
390 ret
= usb_add_config(cdev
, &c
->c
, gfs_do_config
);
391 if (unlikely(ret
< 0))
394 usb_composite_overwrite_options(cdev
, &coverwrite
);
398 for (i
= 0; i
< func_num
; i
++)
399 functionfs_unbind(ffs_tab
[i
].ffs_data
);
401 gether_cleanup(the_dev
);
403 gfs_ether_setup
= false;
408 * It is assumed that gfs_unbind is called from a context where gfs_lock is held
410 static int gfs_unbind(struct usb_composite_dev
*cdev
)
417 * We may have been called in an error recovery from
418 * composite_bind() after gfs_unbind() failure so we need to
419 * check if gfs_ffs_data is not NULL since gfs_bind() handles
420 * all error recovery itself. I'd rather we werent called
421 * from composite on orror recovery, but what you're gonna
425 gether_cleanup(the_dev
);
426 gfs_ether_setup
= false;
428 for (i
= func_num
; i
--; )
429 if (ffs_tab
[i
].ffs_data
)
430 functionfs_unbind(ffs_tab
[i
].ffs_data
);
436 * It is assumed that gfs_do_config is called from a context where
439 static int gfs_do_config(struct usb_configuration
*c
)
441 struct gfs_configuration
*gc
=
442 container_of(c
, struct gfs_configuration
, c
);
449 if (gadget_is_otg(c
->cdev
->gadget
)) {
450 c
->descriptors
= gfs_otg_desc
;
451 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
455 ret
= gc
->eth(c
, gfs_host_mac
, the_dev
);
456 if (unlikely(ret
< 0))
460 for (i
= 0; i
< func_num
; i
++) {
461 ret
= functionfs_bind_config(c
->cdev
, c
, ffs_tab
[i
].ffs_data
);
462 if (unlikely(ret
< 0))
467 * After previous do_configs there may be some invalid
468 * pointers in c->interface array. This happens every time
469 * a user space function with fewer interfaces than a user
470 * space function that was run before the new one is run. The
471 * compasit's set_config() assumes that if there is no more
472 * then MAX_CONFIG_INTERFACES interfaces in a configuration
473 * then there is a NULL pointer after the last interface in
474 * c->interface array. We need to make sure this is true.
476 if (c
->next_interface_id
< ARRAY_SIZE(c
->interface
))
477 c
->interface
[c
->next_interface_id
] = NULL
;
482 #ifdef CONFIG_USB_FUNCTIONFS_ETH
484 static int eth_bind_config(struct usb_configuration
*c
, u8 ethaddr
[ETH_ALEN
],
487 return can_support_ecm(c
->cdev
->gadget
)
488 ? ecm_bind_config(c
, ethaddr
, dev
)
489 : geth_bind_config(c
, ethaddr
, dev
);