]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/usb/gadget/g_ffs.c
Merge branch 'drm-nouveau-next' of git://anongit.freedesktop.org/git/nouveau/linux-2.6
[karo-tx-linux.git] / drivers / usb / gadget / g_ffs.c
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 #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"
41
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
54
55 #include "f_fs.c"
56
57 #define DRIVER_NAME     "g_ffs"
58 #define DRIVER_DESC     "USB Function Filesystem"
59 #define DRIVER_VERSION  "24 Aug 2004"
60
61 MODULE_DESCRIPTION(DRIVER_DESC);
62 MODULE_AUTHOR("Michal Nazarewicz");
63 MODULE_LICENSE("GPL");
64
65 #define GFS_VENDOR_ID   0x1d6b  /* Linux Foundation */
66 #define GFS_PRODUCT_ID  0x0105  /* FunctionFS Gadget */
67
68 #define GFS_MAX_DEVS    10
69
70 struct gfs_ffs_obj {
71         const char *name;
72         bool mounted;
73         bool desc_ready;
74         struct ffs_data *ffs_data;
75 };
76
77 USB_GADGET_COMPOSITE_OPTIONS();
78
79 USB_ETHERNET_MODULE_PARAMETERS();
80
81 static struct usb_device_descriptor gfs_dev_desc = {
82         .bLength                = sizeof gfs_dev_desc,
83         .bDescriptorType        = USB_DT_DEVICE,
84
85         .bcdUSB                 = cpu_to_le16(0x0200),
86         .bDeviceClass           = USB_CLASS_PER_INTERFACE,
87
88         .idVendor               = cpu_to_le16(GFS_VENDOR_ID),
89         .idProduct              = cpu_to_le16(GFS_PRODUCT_ID),
90 };
91
92 static char *func_names[GFS_MAX_DEVS];
93 static unsigned int func_num;
94
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");
103
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,
109
110                 /*
111                  * REVISIT SRP-only hardware is possible, although
112                  * it would not be called "OTG" ...
113                  */
114                 .bmAttributes           = USB_OTG_SRP | USB_OTG_HNP,
115         },
116
117         NULL
118 };
119
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 */
135 };
136
137 static struct usb_gadget_strings *gfs_dev_strings[] = {
138         &(struct usb_gadget_strings) {
139                 .language       = 0x0409,       /* en-us */
140                 .strings        = gfs_strings,
141         },
142         NULL,
143 };
144
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
151         {
152                 .eth            = rndis_bind_config,
153         },
154 #endif
155
156 #ifdef CONFIG_USB_FUNCTIONFS_ETH
157         {
158                 .eth            = eth_bind_config,
159         },
160 #endif
161
162 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
163         {
164         },
165 #endif
166 };
167
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);
171
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,
179 };
180
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;
187
188 static int __init gfs_init(void)
189 {
190         int i;
191
192         ENTER();
193
194         if (!func_num) {
195                 gfs_single_func = true;
196                 func_num = 1;
197         }
198
199         ffs_tab = kcalloc(func_num, sizeof *ffs_tab, GFP_KERNEL);
200         if (!ffs_tab)
201                 return -ENOMEM;
202
203         if (!gfs_single_func)
204                 for (i = 0; i < func_num; i++)
205                         ffs_tab[i].name = func_names[i];
206
207         missing_funcs = func_num;
208
209         return functionfs_init();
210 }
211 module_init(gfs_init);
212
213 static void __exit gfs_exit(void)
214 {
215         ENTER();
216         mutex_lock(&gfs_lock);
217
218         if (gfs_registered)
219                 usb_composite_unregister(&gfs_driver);
220         gfs_registered = false;
221
222         functionfs_cleanup();
223
224         mutex_unlock(&gfs_lock);
225         kfree(ffs_tab);
226 }
227 module_exit(gfs_exit);
228
229 static struct gfs_ffs_obj *gfs_find_dev(const char *dev_name)
230 {
231         int i;
232
233         ENTER();
234
235         if (gfs_single_func)
236                 return &ffs_tab[0];
237
238         for (i = 0; i < func_num; i++)
239                 if (strcmp(ffs_tab[i].name, dev_name) == 0)
240                         return &ffs_tab[i];
241
242         return NULL;
243 }
244
245 static int functionfs_ready_callback(struct ffs_data *ffs)
246 {
247         struct gfs_ffs_obj *ffs_obj;
248         int ret;
249
250         ENTER();
251         mutex_lock(&gfs_lock);
252
253         ffs_obj = ffs->private_data;
254         if (!ffs_obj) {
255                 ret = -EINVAL;
256                 goto done;
257         }
258
259         if (WARN_ON(ffs_obj->desc_ready)) {
260                 ret = -EBUSY;
261                 goto done;
262         }
263         ffs_obj->desc_ready = true;
264         ffs_obj->ffs_data = ffs;
265
266         if (--missing_funcs) {
267                 ret = 0;
268                 goto done;
269         }
270
271         if (gfs_registered) {
272                 ret = -EBUSY;
273                 goto done;
274         }
275         gfs_registered = true;
276
277         ret = usb_composite_probe(&gfs_driver);
278         if (unlikely(ret < 0))
279                 gfs_registered = false;
280
281 done:
282         mutex_unlock(&gfs_lock);
283         return ret;
284 }
285
286 static void functionfs_closed_callback(struct ffs_data *ffs)
287 {
288         struct gfs_ffs_obj *ffs_obj;
289
290         ENTER();
291         mutex_lock(&gfs_lock);
292
293         ffs_obj = ffs->private_data;
294         if (!ffs_obj)
295                 goto done;
296
297         ffs_obj->desc_ready = false;
298         missing_funcs++;
299
300         if (gfs_registered)
301                 usb_composite_unregister(&gfs_driver);
302         gfs_registered = false;
303
304 done:
305         mutex_unlock(&gfs_lock);
306 }
307
308 static void *functionfs_acquire_dev_callback(const char *dev_name)
309 {
310         struct gfs_ffs_obj *ffs_dev;
311
312         ENTER();
313         mutex_lock(&gfs_lock);
314
315         ffs_dev = gfs_find_dev(dev_name);
316         if (!ffs_dev) {
317                 ffs_dev = ERR_PTR(-ENODEV);
318                 goto done;
319         }
320
321         if (ffs_dev->mounted) {
322                 ffs_dev = ERR_PTR(-EBUSY);
323                 goto done;
324         }
325         ffs_dev->mounted = true;
326
327 done:
328         mutex_unlock(&gfs_lock);
329         return ffs_dev;
330 }
331
332 static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
333 {
334         struct gfs_ffs_obj *ffs_dev;
335
336         ENTER();
337         mutex_lock(&gfs_lock);
338
339         ffs_dev = ffs_data->private_data;
340         if (ffs_dev)
341                 ffs_dev->mounted = false;
342
343         mutex_unlock(&gfs_lock);
344 }
345
346 /*
347  * It is assumed that gfs_bind is called from a context where gfs_lock is held
348  */
349 static int gfs_bind(struct usb_composite_dev *cdev)
350 {
351         int ret, i;
352
353         ENTER();
354
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;
364         }
365         gfs_ether_setup = true;
366
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;
371
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;
378                 }
379         }
380
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;
384
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;
389
390                 ret = usb_add_config(cdev, &c->c, gfs_do_config);
391                 if (unlikely(ret < 0))
392                         goto error_unbind;
393         }
394         usb_composite_overwrite_options(cdev, &coverwrite);
395         return 0;
396
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;
405 }
406
407 /*
408  * It is assumed that gfs_unbind is called from a context where gfs_lock is held
409  */
410 static int gfs_unbind(struct usb_composite_dev *cdev)
411 {
412         int i;
413
414         ENTER();
415
416         /*
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...?
423          */
424         if (gfs_ether_setup)
425                 gether_cleanup(the_dev);
426         gfs_ether_setup = false;
427
428         for (i = func_num; i--; )
429                 if (ffs_tab[i].ffs_data)
430                         functionfs_unbind(ffs_tab[i].ffs_data);
431
432         return 0;
433 }
434
435 /*
436  * It is assumed that gfs_do_config is called from a context where
437  * gfs_lock is held
438  */
439 static int gfs_do_config(struct usb_configuration *c)
440 {
441         struct gfs_configuration *gc =
442                 container_of(c, struct gfs_configuration, c);
443         int i;
444         int ret;
445
446         if (missing_funcs)
447                 return -ENODEV;
448
449         if (gadget_is_otg(c->cdev->gadget)) {
450                 c->descriptors = gfs_otg_desc;
451                 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
452         }
453
454         if (gc->eth) {
455                 ret = gc->eth(c, gfs_host_mac, the_dev);
456                 if (unlikely(ret < 0))
457                         return ret;
458         }
459
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;
464         }
465
466         /*
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.
475          */
476         if (c->next_interface_id < ARRAY_SIZE(c->interface))
477                 c->interface[c->next_interface_id] = NULL;
478
479         return 0;
480 }
481
482 #ifdef CONFIG_USB_FUNCTIONFS_ETH
483
484 static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
485                 struct eth_dev *dev)
486 {
487         return can_support_ecm(c->cdev->gadget)
488                 ? ecm_bind_config(c, ethaddr, dev)
489                 : geth_bind_config(c, ethaddr, dev);
490 }
491
492 #endif