Merge git://www.linux-watchdog.org/linux-watchdog
[linux/fpc-iii.git] / drivers / usb / gadget / g_ffs.c
blob5327c82472eda8034a54ca664392baf5875b0a7a
1 /*
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
25 # undef USB_ETH_RNDIS
26 # endif
27 # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
28 # define USB_ETH_RNDIS y
29 # endif
31 #define USBF_ECM_INCLUDED
32 # include "f_ecm.c"
33 #define USB_FSUBSET_INCLUDED
34 # include "f_subset.c"
35 # ifdef USB_ETH_RNDIS
36 # define USB_FRNDIS_INCLUDED
37 # include "f_rndis.c"
38 # include "rndis.h"
39 # endif
40 # include "u_ether.h"
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],
46 struct eth_dev *dev);
47 # endif
48 #else
49 # define the_dev NULL
50 # define gether_cleanup(dev) do { } while (0)
51 # define gfs_host_mac NULL
52 struct eth_dev;
53 #endif
55 #include "f_fs.c"
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
70 struct gfs_ffs_obj {
71 const char *name;
72 bool mounted;
73 bool desc_ready;
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,
117 NULL
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" },
127 #endif
128 #ifdef CONFIG_USB_FUNCTIONFS_ETH
129 { .s = "FunctionFS + ECM" },
130 #endif
131 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
132 { .s = "FunctionFS" },
133 #endif
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,
142 NULL,
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,
154 #endif
156 #ifdef CONFIG_USB_FUNCTIONFS_ETH
158 .eth = eth_bind_config,
160 #endif
162 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
165 #endif
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 = {
173 .name = DRIVER_NAME,
174 .dev = &gfs_dev_desc,
175 .strings = gfs_dev_strings,
176 .max_speed = USB_SPEED_HIGH,
177 .bind = gfs_bind,
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)
190 int i;
192 ENTER();
194 if (!func_num) {
195 gfs_single_func = true;
196 func_num = 1;
199 ffs_tab = kcalloc(func_num, sizeof *ffs_tab, GFP_KERNEL);
200 if (!ffs_tab)
201 return -ENOMEM;
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)
215 ENTER();
216 mutex_lock(&gfs_lock);
218 if (gfs_registered)
219 usb_composite_unregister(&gfs_driver);
220 gfs_registered = false;
222 functionfs_cleanup();
224 mutex_unlock(&gfs_lock);
225 kfree(ffs_tab);
227 module_exit(gfs_exit);
229 static struct gfs_ffs_obj *gfs_find_dev(const char *dev_name)
231 int i;
233 ENTER();
235 if (gfs_single_func)
236 return &ffs_tab[0];
238 for (i = 0; i < func_num; i++)
239 if (strcmp(ffs_tab[i].name, dev_name) == 0)
240 return &ffs_tab[i];
242 return NULL;
245 static int functionfs_ready_callback(struct ffs_data *ffs)
247 struct gfs_ffs_obj *ffs_obj;
248 int ret;
250 ENTER();
251 mutex_lock(&gfs_lock);
253 ffs_obj = ffs->private_data;
254 if (!ffs_obj) {
255 ret = -EINVAL;
256 goto done;
259 if (WARN_ON(ffs_obj->desc_ready)) {
260 ret = -EBUSY;
261 goto done;
263 ffs_obj->desc_ready = true;
264 ffs_obj->ffs_data = ffs;
266 if (--missing_funcs) {
267 ret = 0;
268 goto done;
271 if (gfs_registered) {
272 ret = -EBUSY;
273 goto done;
275 gfs_registered = true;
277 ret = usb_composite_probe(&gfs_driver);
278 if (unlikely(ret < 0))
279 gfs_registered = false;
281 done:
282 mutex_unlock(&gfs_lock);
283 return ret;
286 static void functionfs_closed_callback(struct ffs_data *ffs)
288 struct gfs_ffs_obj *ffs_obj;
290 ENTER();
291 mutex_lock(&gfs_lock);
293 ffs_obj = ffs->private_data;
294 if (!ffs_obj)
295 goto done;
297 ffs_obj->desc_ready = false;
298 missing_funcs++;
300 if (gfs_registered)
301 usb_composite_unregister(&gfs_driver);
302 gfs_registered = false;
304 done:
305 mutex_unlock(&gfs_lock);
308 static void *functionfs_acquire_dev_callback(const char *dev_name)
310 struct gfs_ffs_obj *ffs_dev;
312 ENTER();
313 mutex_lock(&gfs_lock);
315 ffs_dev = gfs_find_dev(dev_name);
316 if (!ffs_dev) {
317 ffs_dev = ERR_PTR(-ENODEV);
318 goto done;
321 if (ffs_dev->mounted) {
322 ffs_dev = ERR_PTR(-EBUSY);
323 goto done;
325 ffs_dev->mounted = true;
327 done:
328 mutex_unlock(&gfs_lock);
329 return ffs_dev;
332 static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
334 struct gfs_ffs_obj *ffs_dev;
336 ENTER();
337 mutex_lock(&gfs_lock);
339 ffs_dev = ffs_data->private_data;
340 if (ffs_dev)
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)
351 int ret, i;
353 ENTER();
355 if (missing_funcs)
356 return -ENODEV;
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,
359 qmult);
360 #endif
361 if (IS_ERR(the_dev)) {
362 ret = PTR_ERR(the_dev);
363 goto error_quick;
365 gfs_ether_setup = true;
367 ret = usb_string_ids_tab(cdev, gfs_strings);
368 if (unlikely(ret < 0))
369 goto error;
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);
377 goto error;
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))
392 goto error_unbind;
394 usb_composite_overwrite_options(cdev, &coverwrite);
395 return 0;
397 error_unbind:
398 for (i = 0; i < func_num; i++)
399 functionfs_unbind(ffs_tab[i].ffs_data);
400 error:
401 gether_cleanup(the_dev);
402 error_quick:
403 gfs_ether_setup = false;
404 return ret;
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)
412 int i;
414 ENTER();
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
422 * do...?
424 if (gfs_ether_setup)
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);
432 return 0;
436 * It is assumed that gfs_do_config is called from a context where
437 * gfs_lock is held
439 static int gfs_do_config(struct usb_configuration *c)
441 struct gfs_configuration *gc =
442 container_of(c, struct gfs_configuration, c);
443 int i;
444 int ret;
446 if (missing_funcs)
447 return -ENODEV;
449 if (gadget_is_otg(c->cdev->gadget)) {
450 c->descriptors = gfs_otg_desc;
451 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
454 if (gc->eth) {
455 ret = gc->eth(c, gfs_host_mac, the_dev);
456 if (unlikely(ret < 0))
457 return ret;
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))
463 return ret;
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;
479 return 0;
482 #ifdef CONFIG_USB_FUNCTIONFS_ETH
484 static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
485 struct eth_dev *dev)
487 return can_support_ecm(c->cdev->gadget)
488 ? ecm_bind_config(c, ethaddr, dev)
489 : geth_bind_config(c, ethaddr, dev);
492 #endif