Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * g_ffs.c -- user mode file system API for USB composite function controllers
3 *
4 * Copyright (C) 2010 Samsung Electronics
5 * Author: Michal Nazarewicz <mina86@mina86.com>
6 *
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.
11 */
12
13#define pr_fmt(fmt) "g_ffs: " fmt
14
15#include <linux/module.h>
16/*
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.
22 */
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
30
31# include "f_ecm.c"
32# include "f_subset.c"
33# ifdef USB_ETH_RNDIS
34# include "f_rndis.c"
35# include "rndis.c"
36# endif
37# include "u_ether.c"
38
39static u8 gfs_hostaddr[ETH_ALEN];
40static struct eth_dev *the_dev;
41# ifdef CONFIG_USB_FUNCTIONFS_ETH
42static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
43 struct eth_dev *dev);
44# endif
45#else
46# define the_dev NULL
47# define gether_cleanup(dev) do { } while (0)
48# define gfs_hostaddr NULL
49struct eth_dev;
50#endif
51
52#include "f_fs.c"
53
54#define DRIVER_NAME "g_ffs"
55#define DRIVER_DESC "USB Function Filesystem"
56#define DRIVER_VERSION "24 Aug 2004"
57
58MODULE_DESCRIPTION(DRIVER_DESC);
59MODULE_AUTHOR("Michal Nazarewicz");
60MODULE_LICENSE("GPL");
61
62#define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
63#define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
64
65#define GFS_MAX_DEVS 10
66
67struct gfs_ffs_obj {
68 const char *name;
69 bool mounted;
70 bool desc_ready;
71 struct ffs_data *ffs_data;
72};
73
74USB_GADGET_COMPOSITE_OPTIONS();
75
76static struct usb_device_descriptor gfs_dev_desc = {
77 .bLength = sizeof gfs_dev_desc,
78 .bDescriptorType = USB_DT_DEVICE,
79
80 .bcdUSB = cpu_to_le16(0x0200),
81 .bDeviceClass = USB_CLASS_PER_INTERFACE,
82
83 .idVendor = cpu_to_le16(GFS_VENDOR_ID),
84 .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
85};
86
87static char *func_names[GFS_MAX_DEVS];
88static unsigned int func_num;
89
90module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
91MODULE_PARM_DESC(bDeviceClass, "USB Device class");
92module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
93MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
94module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
95MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
96module_param_array_named(functions, func_names, charp, &func_num, 0);
97MODULE_PARM_DESC(functions, "USB Functions list");
98
99static const struct usb_descriptor_header *gfs_otg_desc[] = {
100 (const struct usb_descriptor_header *)
101 &(const struct usb_otg_descriptor) {
102 .bLength = sizeof(struct usb_otg_descriptor),
103 .bDescriptorType = USB_DT_OTG,
104
105 /*
106 * REVISIT SRP-only hardware is possible, although
107 * it would not be called "OTG" ...
108 */
109 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
110 },
111
112 NULL
113};
114
115/* String IDs are assigned dynamically */
116static struct usb_string gfs_strings[] = {
117 [USB_GADGET_MANUFACTURER_IDX].s = "",
118 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
119 [USB_GADGET_SERIAL_IDX].s = "",
120#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
121 { .s = "FunctionFS + RNDIS" },
122#endif
123#ifdef CONFIG_USB_FUNCTIONFS_ETH
124 { .s = "FunctionFS + ECM" },
125#endif
126#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
127 { .s = "FunctionFS" },
128#endif
129 { } /* end of list */
130};
131
132static struct usb_gadget_strings *gfs_dev_strings[] = {
133 &(struct usb_gadget_strings) {
134 .language = 0x0409, /* en-us */
135 .strings = gfs_strings,
136 },
137 NULL,
138};
139
140struct gfs_configuration {
141 struct usb_configuration c;
142 int (*eth)(struct usb_configuration *c, u8 *ethaddr,
143 struct eth_dev *dev);
144} gfs_configurations[] = {
145#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
146 {
147 .eth = rndis_bind_config,
148 },
149#endif
150
151#ifdef CONFIG_USB_FUNCTIONFS_ETH
152 {
153 .eth = eth_bind_config,
154 },
155#endif
156
157#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
158 {
159 },
160#endif
161};
162
163static int gfs_bind(struct usb_composite_dev *cdev);
164static int gfs_unbind(struct usb_composite_dev *cdev);
165static int gfs_do_config(struct usb_configuration *c);
166
167static __refdata struct usb_composite_driver gfs_driver = {
168 .name = DRIVER_NAME,
169 .dev = &gfs_dev_desc,
170 .strings = gfs_dev_strings,
171 .max_speed = USB_SPEED_HIGH,
172 .bind = gfs_bind,
173 .unbind = gfs_unbind,
174};
175
176static DEFINE_MUTEX(gfs_lock);
177static unsigned int missing_funcs;
178static bool gfs_ether_setup;
179static bool gfs_registered;
180static bool gfs_single_func;
181static struct gfs_ffs_obj *ffs_tab;
182
183static int __init gfs_init(void)
184{
185 int i;
186
187 ENTER();
188
189 if (!func_num) {
190 gfs_single_func = true;
191 func_num = 1;
192 }
193
194 ffs_tab = kcalloc(func_num, sizeof *ffs_tab, GFP_KERNEL);
195 if (!ffs_tab)
196 return -ENOMEM;
197
198 if (!gfs_single_func)
199 for (i = 0; i < func_num; i++)
200 ffs_tab[i].name = func_names[i];
201
202 missing_funcs = func_num;
203
204 return functionfs_init();
205}
206module_init(gfs_init);
207
208static void __exit gfs_exit(void)
209{
210 ENTER();
211 mutex_lock(&gfs_lock);
212
213 if (gfs_registered)
214 usb_composite_unregister(&gfs_driver);
215 gfs_registered = false;
216
217 functionfs_cleanup();
218
219 mutex_unlock(&gfs_lock);
220 kfree(ffs_tab);
221}
222module_exit(gfs_exit);
223
224static struct gfs_ffs_obj *gfs_find_dev(const char *dev_name)
225{
226 int i;
227
228 ENTER();
229
230 if (gfs_single_func)
231 return &ffs_tab[0];
232
233 for (i = 0; i < func_num; i++)
234 if (strcmp(ffs_tab[i].name, dev_name) == 0)
235 return &ffs_tab[i];
236
237 return NULL;
238}
239
240static int functionfs_ready_callback(struct ffs_data *ffs)
241{
242 struct gfs_ffs_obj *ffs_obj;
243 int ret;
244
245 ENTER();
246 mutex_lock(&gfs_lock);
247
248 ffs_obj = ffs->private_data;
249 if (!ffs_obj) {
250 ret = -EINVAL;
251 goto done;
252 }
253
254 if (WARN_ON(ffs_obj->desc_ready)) {
255 ret = -EBUSY;
256 goto done;
257 }
258 ffs_obj->desc_ready = true;
259 ffs_obj->ffs_data = ffs;
260
261 if (--missing_funcs) {
262 ret = 0;
263 goto done;
264 }
265
266 if (gfs_registered) {
267 ret = -EBUSY;
268 goto done;
269 }
270 gfs_registered = true;
271
272 ret = usb_composite_probe(&gfs_driver);
273 if (unlikely(ret < 0))
274 gfs_registered = false;
275
276done:
277 mutex_unlock(&gfs_lock);
278 return ret;
279}
280
281static void functionfs_closed_callback(struct ffs_data *ffs)
282{
283 struct gfs_ffs_obj *ffs_obj;
284
285 ENTER();
286 mutex_lock(&gfs_lock);
287
288 ffs_obj = ffs->private_data;
289 if (!ffs_obj)
290 goto done;
291
292 ffs_obj->desc_ready = false;
293 missing_funcs++;
294
295 if (gfs_registered)
296 usb_composite_unregister(&gfs_driver);
297 gfs_registered = false;
298
299done:
300 mutex_unlock(&gfs_lock);
301}
302
303static void *functionfs_acquire_dev_callback(const char *dev_name)
304{
305 struct gfs_ffs_obj *ffs_dev;
306
307 ENTER();
308 mutex_lock(&gfs_lock);
309
310 ffs_dev = gfs_find_dev(dev_name);
311 if (!ffs_dev) {
312 ffs_dev = ERR_PTR(-ENODEV);
313 goto done;
314 }
315
316 if (ffs_dev->mounted) {
317 ffs_dev = ERR_PTR(-EBUSY);
318 goto done;
319 }
320 ffs_dev->mounted = true;
321
322done:
323 mutex_unlock(&gfs_lock);
324 return ffs_dev;
325}
326
327static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
328{
329 struct gfs_ffs_obj *ffs_dev;
330
331 ENTER();
332 mutex_lock(&gfs_lock);
333
334 ffs_dev = ffs_data->private_data;
335 if (ffs_dev)
336 ffs_dev->mounted = false;
337
338 mutex_unlock(&gfs_lock);
339}
340
341/*
342 * It is assumed that gfs_bind is called from a context where gfs_lock is held
343 */
344static int gfs_bind(struct usb_composite_dev *cdev)
345{
346 int ret, i;
347
348 ENTER();
349
350 if (missing_funcs)
351 return -ENODEV;
352#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
353 the_dev = gether_setup(cdev->gadget, gfs_hostaddr);
354#endif
355 if (IS_ERR(the_dev)) {
356 ret = PTR_ERR(the_dev);
357 goto error_quick;
358 }
359 gfs_ether_setup = true;
360
361 ret = usb_string_ids_tab(cdev, gfs_strings);
362 if (unlikely(ret < 0))
363 goto error;
364 gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
365
366 for (i = func_num; i--; ) {
367 ret = functionfs_bind(ffs_tab[i].ffs_data, cdev);
368 if (unlikely(ret < 0)) {
369 while (++i < func_num)
370 functionfs_unbind(ffs_tab[i].ffs_data);
371 goto error;
372 }
373 }
374
375 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
376 struct gfs_configuration *c = gfs_configurations + i;
377 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
378
379 c->c.label = gfs_strings[sid].s;
380 c->c.iConfiguration = gfs_strings[sid].id;
381 c->c.bConfigurationValue = 1 + i;
382 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
383
384 ret = usb_add_config(cdev, &c->c, gfs_do_config);
385 if (unlikely(ret < 0))
386 goto error_unbind;
387 }
388 usb_composite_overwrite_options(cdev, &coverwrite);
389 return 0;
390
391error_unbind:
392 for (i = 0; i < func_num; i++)
393 functionfs_unbind(ffs_tab[i].ffs_data);
394error:
395 gether_cleanup(the_dev);
396error_quick:
397 gfs_ether_setup = false;
398 return ret;
399}
400
401/*
402 * It is assumed that gfs_unbind is called from a context where gfs_lock is held
403 */
404static int gfs_unbind(struct usb_composite_dev *cdev)
405{
406 int i;
407
408 ENTER();
409
410 /*
411 * We may have been called in an error recovery from
412 * composite_bind() after gfs_unbind() failure so we need to
413 * check if gfs_ffs_data is not NULL since gfs_bind() handles
414 * all error recovery itself. I'd rather we werent called
415 * from composite on orror recovery, but what you're gonna
416 * do...?
417 */
418 if (gfs_ether_setup)
419 gether_cleanup(the_dev);
420 gfs_ether_setup = false;
421
422 for (i = func_num; i--; )
423 if (ffs_tab[i].ffs_data)
424 functionfs_unbind(ffs_tab[i].ffs_data);
425
426 return 0;
427}
428
429/*
430 * It is assumed that gfs_do_config is called from a context where
431 * gfs_lock is held
432 */
433static int gfs_do_config(struct usb_configuration *c)
434{
435 struct gfs_configuration *gc =
436 container_of(c, struct gfs_configuration, c);
437 int i;
438 int ret;
439
440 if (missing_funcs)
441 return -ENODEV;
442
443 if (gadget_is_otg(c->cdev->gadget)) {
444 c->descriptors = gfs_otg_desc;
445 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
446 }
447
448 if (gc->eth) {
449 ret = gc->eth(c, gfs_hostaddr, the_dev);
450 if (unlikely(ret < 0))
451 return ret;
452 }
453
454 for (i = 0; i < func_num; i++) {
455 ret = functionfs_bind_config(c->cdev, c, ffs_tab[i].ffs_data);
456 if (unlikely(ret < 0))
457 return ret;
458 }
459
460 /*
461 * After previous do_configs there may be some invalid
462 * pointers in c->interface array. This happens every time
463 * a user space function with fewer interfaces than a user
464 * space function that was run before the new one is run. The
465 * compasit's set_config() assumes that if there is no more
466 * then MAX_CONFIG_INTERFACES interfaces in a configuration
467 * then there is a NULL pointer after the last interface in
468 * c->interface array. We need to make sure this is true.
469 */
470 if (c->next_interface_id < ARRAY_SIZE(c->interface))
471 c->interface[c->next_interface_id] = NULL;
472
473 return 0;
474}
475
476#ifdef CONFIG_USB_FUNCTIONFS_ETH
477
478static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
479 struct eth_dev *dev)
480{
481 return can_support_ecm(c->cdev->gadget)
482 ? ecm_bind_config(c, ethaddr, dev)
483 : geth_bind_config(c, ethaddr, dev);
484}
485
486#endif