1
2
3
4
5
6
7
8
9
10
11
12
13#define pr_fmt(fmt) "g_ffs: " fmt
14
15#include <linux/module.h>
16
17#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
18#include <linux/netdevice.h>
19
20# if defined USB_ETH_RNDIS
21# undef USB_ETH_RNDIS
22# endif
23# ifdef CONFIG_USB_FUNCTIONFS_RNDIS
24# define USB_ETH_RNDIS y
25# endif
26
27# include "u_ecm.h"
28# include "u_gether.h"
29# ifdef USB_ETH_RNDIS
30# include "u_rndis.h"
31# include "rndis.h"
32# endif
33# include "u_ether.h"
34
35USB_ETHERNET_MODULE_PARAMETERS();
36
37# ifdef CONFIG_USB_FUNCTIONFS_ETH
38static int eth_bind_config(struct usb_configuration *c);
39static struct usb_function_instance *fi_ecm;
40static struct usb_function *f_ecm;
41static struct usb_function_instance *fi_geth;
42static struct usb_function *f_geth;
43# endif
44# ifdef CONFIG_USB_FUNCTIONFS_RNDIS
45static int bind_rndis_config(struct usb_configuration *c);
46static struct usb_function_instance *fi_rndis;
47static struct usb_function *f_rndis;
48# endif
49#endif
50
51#include "u_fs.h"
52
53#define DRIVER_NAME "g_ffs"
54#define DRIVER_DESC "USB Function Filesystem"
55#define DRIVER_VERSION "24 Aug 2004"
56
57MODULE_DESCRIPTION(DRIVER_DESC);
58MODULE_AUTHOR("Michal Nazarewicz");
59MODULE_LICENSE("GPL");
60
61#define GFS_VENDOR_ID 0x1d6b
62#define GFS_PRODUCT_ID 0x0105
63
64#define GFS_MAX_DEVS 10
65
66USB_GADGET_COMPOSITE_OPTIONS();
67
68static struct usb_device_descriptor gfs_dev_desc = {
69 .bLength = sizeof gfs_dev_desc,
70 .bDescriptorType = USB_DT_DEVICE,
71
72
73 .bDeviceClass = USB_CLASS_PER_INTERFACE,
74
75 .idVendor = cpu_to_le16(GFS_VENDOR_ID),
76 .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
77};
78
79static char *func_names[GFS_MAX_DEVS];
80static unsigned int func_num;
81
82module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
83MODULE_PARM_DESC(bDeviceClass, "USB Device class");
84module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
85MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
86module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
87MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
88module_param_array_named(functions, func_names, charp, &func_num, 0);
89MODULE_PARM_DESC(functions, "USB Functions list");
90
91static const struct usb_descriptor_header *gfs_otg_desc[2];
92
93
94static struct usb_string gfs_strings[] = {
95 [USB_GADGET_MANUFACTURER_IDX].s = "",
96 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
97 [USB_GADGET_SERIAL_IDX].s = "",
98#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
99 { .s = "FunctionFS + RNDIS" },
100#endif
101#ifdef CONFIG_USB_FUNCTIONFS_ETH
102 { .s = "FunctionFS + ECM" },
103#endif
104#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
105 { .s = "FunctionFS" },
106#endif
107 { }
108};
109
110static struct usb_gadget_strings *gfs_dev_strings[] = {
111 &(struct usb_gadget_strings) {
112 .language = 0x0409,
113 .strings = gfs_strings,
114 },
115 NULL,
116};
117
118struct gfs_configuration {
119 struct usb_configuration c;
120 int (*eth)(struct usb_configuration *c);
121 int num;
122};
123
124static struct gfs_configuration gfs_configurations[] = {
125#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
126 {
127 .eth = bind_rndis_config,
128 },
129#endif
130
131#ifdef CONFIG_USB_FUNCTIONFS_ETH
132 {
133 .eth = eth_bind_config,
134 },
135#endif
136
137#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
138 {
139 },
140#endif
141};
142
143static void *functionfs_acquire_dev(struct ffs_dev *dev);
144static void functionfs_release_dev(struct ffs_dev *dev);
145static int functionfs_ready_callback(struct ffs_data *ffs);
146static void functionfs_closed_callback(struct ffs_data *ffs);
147static int gfs_bind(struct usb_composite_dev *cdev);
148static int gfs_unbind(struct usb_composite_dev *cdev);
149static int gfs_do_config(struct usb_configuration *c);
150
151
152static struct usb_composite_driver gfs_driver = {
153 .name = DRIVER_NAME,
154 .dev = &gfs_dev_desc,
155 .strings = gfs_dev_strings,
156 .max_speed = USB_SPEED_HIGH,
157 .bind = gfs_bind,
158 .unbind = gfs_unbind,
159};
160
161static unsigned int missing_funcs;
162static bool gfs_registered;
163static bool gfs_single_func;
164static struct usb_function_instance **fi_ffs;
165static struct usb_function **f_ffs[] = {
166#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
167 NULL,
168#endif
169
170#ifdef CONFIG_USB_FUNCTIONFS_ETH
171 NULL,
172#endif
173
174#ifdef CONFIG_USB_FUNCTIONFS_GENERIC
175 NULL,
176#endif
177};
178
179#define N_CONF ARRAY_SIZE(f_ffs)
180
181static int __init gfs_init(void)
182{
183 struct f_fs_opts *opts;
184 int i;
185 int ret = 0;
186
187 ENTER();
188
189 if (func_num < 2) {
190 gfs_single_func = true;
191 func_num = 1;
192 }
193
194
195
196
197 f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL);
198 if (!f_ffs[0]) {
199 ret = -ENOMEM;
200 goto no_func;
201 }
202 for (i = 1; i < N_CONF; ++i)
203 f_ffs[i] = f_ffs[0] + i * func_num;
204
205 fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL);
206 if (!fi_ffs) {
207 ret = -ENOMEM;
208 goto no_func;
209 }
210
211 for (i = 0; i < func_num; i++) {
212 fi_ffs[i] = usb_get_function_instance("ffs");
213 if (IS_ERR(fi_ffs[i])) {
214 ret = PTR_ERR(fi_ffs[i]);
215 --i;
216 goto no_dev;
217 }
218 opts = to_f_fs_opts(fi_ffs[i]);
219 if (gfs_single_func)
220 ret = ffs_single_dev(opts->dev);
221 else
222 ret = ffs_name_dev(opts->dev, func_names[i]);
223 if (ret)
224 goto no_dev;
225 opts->dev->ffs_ready_callback = functionfs_ready_callback;
226 opts->dev->ffs_closed_callback = functionfs_closed_callback;
227 opts->dev->ffs_acquire_dev_callback = functionfs_acquire_dev;
228 opts->dev->ffs_release_dev_callback = functionfs_release_dev;
229 opts->no_configfs = true;
230 }
231
232 missing_funcs = func_num;
233
234 return 0;
235no_dev:
236 while (i >= 0)
237 usb_put_function_instance(fi_ffs[i--]);
238 kfree(fi_ffs);
239no_func:
240 kfree(f_ffs[0]);
241 return ret;
242}
243module_init(gfs_init);
244
245static void __exit gfs_exit(void)
246{
247 int i;
248
249 ENTER();
250
251 if (gfs_registered)
252 usb_composite_unregister(&gfs_driver);
253 gfs_registered = false;
254
255 kfree(f_ffs[0]);
256
257 for (i = 0; i < func_num; i++)
258 usb_put_function_instance(fi_ffs[i]);
259
260 kfree(fi_ffs);
261}
262module_exit(gfs_exit);
263
264static void *functionfs_acquire_dev(struct ffs_dev *dev)
265{
266 if (!try_module_get(THIS_MODULE))
267 return ERR_PTR(-ENOENT);
268
269 return NULL;
270}
271
272static void functionfs_release_dev(struct ffs_dev *dev)
273{
274 module_put(THIS_MODULE);
275}
276
277
278
279
280static int functionfs_ready_callback(struct ffs_data *ffs)
281{
282 int ret = 0;
283
284 if (--missing_funcs)
285 return 0;
286
287 if (gfs_registered)
288 return -EBUSY;
289
290 gfs_registered = true;
291
292 ret = usb_composite_probe(&gfs_driver);
293 if (unlikely(ret < 0)) {
294 ++missing_funcs;
295 gfs_registered = false;
296 }
297
298 return ret;
299}
300
301
302
303
304static void functionfs_closed_callback(struct ffs_data *ffs)
305{
306 missing_funcs++;
307
308 if (gfs_registered)
309 usb_composite_unregister(&gfs_driver);
310 gfs_registered = false;
311}
312
313
314
315
316static int gfs_bind(struct usb_composite_dev *cdev)
317{
318#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
319 struct net_device *net;
320#endif
321 int ret, i;
322
323 ENTER();
324
325 if (missing_funcs)
326 return -ENODEV;
327#if defined CONFIG_USB_FUNCTIONFS_ETH
328 if (can_support_ecm(cdev->gadget)) {
329 struct f_ecm_opts *ecm_opts;
330
331 fi_ecm = usb_get_function_instance("ecm");
332 if (IS_ERR(fi_ecm))
333 return PTR_ERR(fi_ecm);
334 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
335 net = ecm_opts->net;
336 } else {
337 struct f_gether_opts *geth_opts;
338
339 fi_geth = usb_get_function_instance("geth");
340 if (IS_ERR(fi_geth))
341 return PTR_ERR(fi_geth);
342 geth_opts = container_of(fi_geth, struct f_gether_opts,
343 func_inst);
344 net = geth_opts->net;
345 }
346#endif
347
348#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
349 {
350 fi_rndis = usb_get_function_instance("rndis");
351 if (IS_ERR(fi_rndis)) {
352 ret = PTR_ERR(fi_rndis);
353 goto error;
354 }
355#ifndef CONFIG_USB_FUNCTIONFS_ETH
356 net = container_of(fi_rndis, struct f_rndis_opts,
357 func_inst)->net;
358#endif
359 }
360#endif
361
362#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
363 gether_set_qmult(net, qmult);
364 if (!gether_set_host_addr(net, host_addr))
365 pr_info("using host ethernet address: %s", host_addr);
366 if (!gether_set_dev_addr(net, dev_addr))
367 pr_info("using self ethernet address: %s", dev_addr);
368#endif
369
370#if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
371 gether_set_gadget(net, cdev->gadget);
372 ret = gether_register_netdev(net);
373 if (ret)
374 goto error_rndis;
375
376 if (can_support_ecm(cdev->gadget)) {
377 struct f_ecm_opts *ecm_opts;
378
379 ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
380 ecm_opts->bound = true;
381 } else {
382 struct f_gether_opts *geth_opts;
383
384 geth_opts = container_of(fi_geth, struct f_gether_opts,
385 func_inst);
386 geth_opts->bound = true;
387 }
388
389 rndis_borrow_net(fi_rndis, net);
390#endif
391
392
393 ret = usb_string_ids_tab(cdev, gfs_strings);
394 if (unlikely(ret < 0))
395 goto error_rndis;
396 gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
397
398 if (gadget_is_otg(cdev->gadget) && !gfs_otg_desc[0]) {
399 struct usb_descriptor_header *usb_desc;
400
401 usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
402 if (!usb_desc)
403 goto error_rndis;
404 usb_otg_descriptor_init(cdev->gadget, usb_desc);
405 gfs_otg_desc[0] = usb_desc;
406 gfs_otg_desc[1] = NULL;
407 }
408
409 for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
410 struct gfs_configuration *c = gfs_configurations + i;
411 int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
412
413 c->c.label = gfs_strings[sid].s;
414 c->c.iConfiguration = gfs_strings[sid].id;
415 c->c.bConfigurationValue = 1 + i;
416 c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
417
418 c->num = i;
419
420 ret = usb_add_config(cdev, &c->c, gfs_do_config);
421 if (unlikely(ret < 0))
422 goto error_unbind;
423 }
424 usb_composite_overwrite_options(cdev, &coverwrite);
425 return 0;
426
427
428error_unbind:
429 kfree(gfs_otg_desc[0]);
430 gfs_otg_desc[0] = NULL;
431error_rndis:
432#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
433 usb_put_function_instance(fi_rndis);
434error:
435#endif
436#if defined CONFIG_USB_FUNCTIONFS_ETH
437 if (can_support_ecm(cdev->gadget))
438 usb_put_function_instance(fi_ecm);
439 else
440 usb_put_function_instance(fi_geth);
441#endif
442 return ret;
443}
444
445
446
447
448static int gfs_unbind(struct usb_composite_dev *cdev)
449{
450 int i;
451
452 ENTER();
453
454
455#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
456 usb_put_function(f_rndis);
457 usb_put_function_instance(fi_rndis);
458#endif
459
460#if defined CONFIG_USB_FUNCTIONFS_ETH
461 if (can_support_ecm(cdev->gadget)) {
462 usb_put_function(f_ecm);
463 usb_put_function_instance(fi_ecm);
464 } else {
465 usb_put_function(f_geth);
466 usb_put_function_instance(fi_geth);
467 }
468#endif
469 for (i = 0; i < N_CONF * func_num; ++i)
470 usb_put_function(*(f_ffs[0] + i));
471
472 kfree(gfs_otg_desc[0]);
473 gfs_otg_desc[0] = NULL;
474
475 return 0;
476}
477
478
479
480
481
482static int gfs_do_config(struct usb_configuration *c)
483{
484 struct gfs_configuration *gc =
485 container_of(c, struct gfs_configuration, c);
486 int i;
487 int ret;
488
489 if (missing_funcs)
490 return -ENODEV;
491
492 if (gadget_is_otg(c->cdev->gadget)) {
493 c->descriptors = gfs_otg_desc;
494 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
495 }
496
497 if (gc->eth) {
498 ret = gc->eth(c);
499 if (unlikely(ret < 0))
500 return ret;
501 }
502
503 for (i = 0; i < func_num; i++) {
504 f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]);
505 if (IS_ERR(f_ffs[gc->num][i])) {
506 ret = PTR_ERR(f_ffs[gc->num][i]);
507 goto error;
508 }
509 ret = usb_add_function(c, f_ffs[gc->num][i]);
510 if (ret < 0) {
511 usb_put_function(f_ffs[gc->num][i]);
512 goto error;
513 }
514 }
515
516
517
518
519
520
521
522
523
524
525
526 if (c->next_interface_id < ARRAY_SIZE(c->interface))
527 c->interface[c->next_interface_id] = NULL;
528
529 return 0;
530error:
531 while (--i >= 0) {
532 if (!IS_ERR(f_ffs[gc->num][i]))
533 usb_remove_function(c, f_ffs[gc->num][i]);
534 usb_put_function(f_ffs[gc->num][i]);
535 }
536 return ret;
537}
538
539#ifdef CONFIG_USB_FUNCTIONFS_ETH
540
541static int eth_bind_config(struct usb_configuration *c)
542{
543 int status = 0;
544
545 if (can_support_ecm(c->cdev->gadget)) {
546 f_ecm = usb_get_function(fi_ecm);
547 if (IS_ERR(f_ecm))
548 return PTR_ERR(f_ecm);
549
550 status = usb_add_function(c, f_ecm);
551 if (status < 0)
552 usb_put_function(f_ecm);
553
554 } else {
555 f_geth = usb_get_function(fi_geth);
556 if (IS_ERR(f_geth))
557 return PTR_ERR(f_geth);
558
559 status = usb_add_function(c, f_geth);
560 if (status < 0)
561 usb_put_function(f_geth);
562 }
563 return status;
564}
565
566#endif
567
568#ifdef CONFIG_USB_FUNCTIONFS_RNDIS
569
570static int bind_rndis_config(struct usb_configuration *c)
571{
572 int status = 0;
573
574 f_rndis = usb_get_function(fi_rndis);
575 if (IS_ERR(f_rndis))
576 return PTR_ERR(f_rndis);
577
578 status = usb_add_function(c, f_rndis);
579 if (status < 0)
580 usb_put_function(f_rndis);
581
582 return status;
583}
584
585#endif
586