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>
18 * kbuild is not very cooperative with respect to linking separately
19 * compiled library objects into one module. So for now we won't use
20 * separate compilation ... ensuring init/exit sections work to shrink
21 * the runtime footprint, and giving us at least some parts of what
22 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
24 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
25 # if defined USB_ETH_RNDIS
28 # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
29 # define USB_ETH_RNDIS y
33 # include "f_subset.c"
40 static u8 gfs_hostaddr
[ETH_ALEN
];
41 # ifdef CONFIG_USB_FUNCTIONFS_ETH
42 static int eth_bind_config(struct usb_configuration
*c
, u8 ethaddr
[ETH_ALEN
]);
45 # define gether_cleanup() do { } while (0)
46 # define gether_setup(gadget, hostaddr) ((int)0)
47 # define gfs_hostaddr NULL
52 #define DRIVER_NAME "g_ffs"
53 #define DRIVER_DESC "USB Function Filesystem"
54 #define DRIVER_VERSION "24 Aug 2004"
56 MODULE_DESCRIPTION(DRIVER_DESC
);
57 MODULE_AUTHOR("Michal Nazarewicz");
58 MODULE_LICENSE("GPL");
60 #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
61 #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
63 #define GFS_MAX_DEVS 10
69 struct ffs_data
*ffs_data
;
72 USB_GADGET_COMPOSITE_OPTIONS();
74 static struct usb_device_descriptor gfs_dev_desc
= {
75 .bLength
= sizeof gfs_dev_desc
,
76 .bDescriptorType
= USB_DT_DEVICE
,
78 .bcdUSB
= cpu_to_le16(0x0200),
79 .bDeviceClass
= USB_CLASS_PER_INTERFACE
,
81 .idVendor
= cpu_to_le16(GFS_VENDOR_ID
),
82 .idProduct
= cpu_to_le16(GFS_PRODUCT_ID
),
85 static char *func_names
[GFS_MAX_DEVS
];
86 static unsigned int func_num
;
88 module_param_named(bDeviceClass
, gfs_dev_desc
.bDeviceClass
, byte
, 0644);
89 MODULE_PARM_DESC(bDeviceClass
, "USB Device class");
90 module_param_named(bDeviceSubClass
, gfs_dev_desc
.bDeviceSubClass
, byte
, 0644);
91 MODULE_PARM_DESC(bDeviceSubClass
, "USB Device subclass");
92 module_param_named(bDeviceProtocol
, gfs_dev_desc
.bDeviceProtocol
, byte
, 0644);
93 MODULE_PARM_DESC(bDeviceProtocol
, "USB Device protocol");
94 module_param_array_named(functions
, func_names
, charp
, &func_num
, 0);
95 MODULE_PARM_DESC(functions
, "USB Functions list");
97 static const struct usb_descriptor_header
*gfs_otg_desc
[] = {
98 (const struct usb_descriptor_header
*)
99 &(const struct usb_otg_descriptor
) {
100 .bLength
= sizeof(struct usb_otg_descriptor
),
101 .bDescriptorType
= USB_DT_OTG
,
104 * REVISIT SRP-only hardware is possible, although
105 * it would not be called "OTG" ...
107 .bmAttributes
= USB_OTG_SRP
| USB_OTG_HNP
,
113 /* String IDs are assigned dynamically */
114 static struct usb_string gfs_strings
[] = {
115 [USB_GADGET_MANUFACTURER_IDX
].s
= "",
116 [USB_GADGET_PRODUCT_IDX
].s
= DRIVER_DESC
,
117 [USB_GADGET_SERIAL_IDX
].s
= "",
118 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
119 { .s
= "FunctionFS + RNDIS" },
121 #ifdef CONFIG_USB_FUNCTIONFS_ETH
122 { .s
= "FunctionFS + ECM" },
124 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
125 { .s
= "FunctionFS" },
127 { } /* end of list */
130 static struct usb_gadget_strings
*gfs_dev_strings
[] = {
131 &(struct usb_gadget_strings
) {
132 .language
= 0x0409, /* en-us */
133 .strings
= gfs_strings
,
138 struct gfs_configuration
{
139 struct usb_configuration c
;
140 int (*eth
)(struct usb_configuration
*c
, u8
*ethaddr
);
141 } gfs_configurations
[] = {
142 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
144 .eth
= rndis_bind_config
,
148 #ifdef CONFIG_USB_FUNCTIONFS_ETH
150 .eth
= eth_bind_config
,
154 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
160 static int gfs_bind(struct usb_composite_dev
*cdev
);
161 static int gfs_unbind(struct usb_composite_dev
*cdev
);
162 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 DEFINE_MUTEX(gfs_lock
);
174 static unsigned int missing_funcs
;
175 static bool gfs_ether_setup
;
176 static bool gfs_registered
;
177 static bool gfs_single_func
;
178 static struct gfs_ffs_obj
*ffs_tab
;
180 static int __init
gfs_init(void)
187 gfs_single_func
= true;
191 ffs_tab
= kcalloc(func_num
, sizeof *ffs_tab
, GFP_KERNEL
);
195 if (!gfs_single_func
)
196 for (i
= 0; i
< func_num
; i
++)
197 ffs_tab
[i
].name
= func_names
[i
];
199 missing_funcs
= func_num
;
201 return functionfs_init();
203 module_init(gfs_init
);
205 static void __exit
gfs_exit(void)
208 mutex_lock(&gfs_lock
);
211 usb_composite_unregister(&gfs_driver
);
212 gfs_registered
= false;
214 functionfs_cleanup();
216 mutex_unlock(&gfs_lock
);
219 module_exit(gfs_exit
);
221 static struct gfs_ffs_obj
*gfs_find_dev(const char *dev_name
)
230 for (i
= 0; i
< func_num
; i
++)
231 if (strcmp(ffs_tab
[i
].name
, dev_name
) == 0)
237 static int functionfs_ready_callback(struct ffs_data
*ffs
)
239 struct gfs_ffs_obj
*ffs_obj
;
243 mutex_lock(&gfs_lock
);
245 ffs_obj
= ffs
->private_data
;
251 if (WARN_ON(ffs_obj
->desc_ready
)) {
255 ffs_obj
->desc_ready
= true;
256 ffs_obj
->ffs_data
= ffs
;
258 if (--missing_funcs
) {
263 if (gfs_registered
) {
267 gfs_registered
= true;
269 ret
= usb_composite_probe(&gfs_driver
);
270 if (unlikely(ret
< 0))
271 gfs_registered
= false;
274 mutex_unlock(&gfs_lock
);
278 static void functionfs_closed_callback(struct ffs_data
*ffs
)
280 struct gfs_ffs_obj
*ffs_obj
;
283 mutex_lock(&gfs_lock
);
285 ffs_obj
= ffs
->private_data
;
289 ffs_obj
->desc_ready
= false;
293 usb_composite_unregister(&gfs_driver
);
294 gfs_registered
= false;
297 mutex_unlock(&gfs_lock
);
300 static void *functionfs_acquire_dev_callback(const char *dev_name
)
302 struct gfs_ffs_obj
*ffs_dev
;
305 mutex_lock(&gfs_lock
);
307 ffs_dev
= gfs_find_dev(dev_name
);
309 ffs_dev
= ERR_PTR(-ENODEV
);
313 if (ffs_dev
->mounted
) {
314 ffs_dev
= ERR_PTR(-EBUSY
);
317 ffs_dev
->mounted
= true;
320 mutex_unlock(&gfs_lock
);
324 static void functionfs_release_dev_callback(struct ffs_data
*ffs_data
)
326 struct gfs_ffs_obj
*ffs_dev
;
329 mutex_lock(&gfs_lock
);
331 ffs_dev
= ffs_data
->private_data
;
333 ffs_dev
->mounted
= false;
335 mutex_unlock(&gfs_lock
);
339 * It is assumed that gfs_bind is called from a context where gfs_lock is held
341 static int gfs_bind(struct usb_composite_dev
*cdev
)
350 ret
= gether_setup(cdev
->gadget
, gfs_hostaddr
);
351 if (unlikely(ret
< 0))
353 gfs_ether_setup
= true;
355 ret
= usb_string_ids_tab(cdev
, gfs_strings
);
356 if (unlikely(ret
< 0))
358 gfs_dev_desc
.iProduct
= gfs_strings
[USB_GADGET_PRODUCT_IDX
].id
;
360 for (i
= func_num
; --i
; ) {
361 ret
= functionfs_bind(ffs_tab
[i
].ffs_data
, cdev
);
362 if (unlikely(ret
< 0)) {
363 while (++i
< func_num
)
364 functionfs_unbind(ffs_tab
[i
].ffs_data
);
369 for (i
= 0; i
< ARRAY_SIZE(gfs_configurations
); ++i
) {
370 struct gfs_configuration
*c
= gfs_configurations
+ i
;
371 int sid
= USB_GADGET_FIRST_AVAIL_IDX
+ i
;
373 c
->c
.label
= gfs_strings
[sid
].s
;
374 c
->c
.iConfiguration
= gfs_strings
[sid
].id
;
375 c
->c
.bConfigurationValue
= 1 + i
;
376 c
->c
.bmAttributes
= USB_CONFIG_ATT_SELFPOWER
;
378 ret
= usb_add_config(cdev
, &c
->c
, gfs_do_config
);
379 if (unlikely(ret
< 0))
382 usb_composite_overwrite_options(cdev
, &coverwrite
);
386 for (i
= 0; i
< func_num
; i
++)
387 functionfs_unbind(ffs_tab
[i
].ffs_data
);
391 gfs_ether_setup
= false;
396 * It is assumed that gfs_unbind is called from a context where gfs_lock is held
398 static int gfs_unbind(struct usb_composite_dev
*cdev
)
405 * We may have been called in an error recovery from
406 * composite_bind() after gfs_unbind() failure so we need to
407 * check if gfs_ffs_data is not NULL since gfs_bind() handles
408 * all error recovery itself. I'd rather we werent called
409 * from composite on orror recovery, but what you're gonna
414 gfs_ether_setup
= false;
416 for (i
= func_num
; --i
; )
417 if (ffs_tab
[i
].ffs_data
)
418 functionfs_unbind(ffs_tab
[i
].ffs_data
);
424 * It is assumed that gfs_do_config is called from a context where
427 static int gfs_do_config(struct usb_configuration
*c
)
429 struct gfs_configuration
*gc
=
430 container_of(c
, struct gfs_configuration
, c
);
437 if (gadget_is_otg(c
->cdev
->gadget
)) {
438 c
->descriptors
= gfs_otg_desc
;
439 c
->bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
443 ret
= gc
->eth(c
, gfs_hostaddr
);
444 if (unlikely(ret
< 0))
448 for (i
= 0; i
< func_num
; i
++) {
449 ret
= functionfs_bind_config(c
->cdev
, c
, ffs_tab
[i
].ffs_data
);
450 if (unlikely(ret
< 0))
455 * After previous do_configs there may be some invalid
456 * pointers in c->interface array. This happens every time
457 * a user space function with fewer interfaces than a user
458 * space function that was run before the new one is run. The
459 * compasit's set_config() assumes that if there is no more
460 * then MAX_CONFIG_INTERFACES interfaces in a configuration
461 * then there is a NULL pointer after the last interface in
462 * c->interface array. We need to make sure this is true.
464 if (c
->next_interface_id
< ARRAY_SIZE(c
->interface
))
465 c
->interface
[c
->next_interface_id
] = NULL
;
470 #ifdef CONFIG_USB_FUNCTIONFS_ETH
472 static int eth_bind_config(struct usb_configuration
*c
, u8 ethaddr
[ETH_ALEN
])
474 return can_support_ecm(c
->cdev
->gadget
)
475 ? ecm_bind_config(c
, ethaddr
)
476 : geth_bind_config(c
, ethaddr
);