xref: /openbmc/linux/drivers/usb/gadget/legacy/g_ffs.c (revision c819e2cf)
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 #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 
35 USB_ETHERNET_MODULE_PARAMETERS();
36 
37 #  ifdef CONFIG_USB_FUNCTIONFS_ETH
38 static int eth_bind_config(struct usb_configuration *c);
39 static struct usb_function_instance *fi_ecm;
40 static struct usb_function *f_ecm;
41 static struct usb_function_instance *fi_geth;
42 static struct usb_function *f_geth;
43 #  endif
44 #  ifdef CONFIG_USB_FUNCTIONFS_RNDIS
45 static int bind_rndis_config(struct usb_configuration *c);
46 static struct usb_function_instance *fi_rndis;
47 static 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 
57 MODULE_DESCRIPTION(DRIVER_DESC);
58 MODULE_AUTHOR("Michal Nazarewicz");
59 MODULE_LICENSE("GPL");
60 
61 #define GFS_VENDOR_ID	0x1d6b	/* Linux Foundation */
62 #define GFS_PRODUCT_ID	0x0105	/* FunctionFS Gadget */
63 
64 #define GFS_MAX_DEVS	10
65 
66 USB_GADGET_COMPOSITE_OPTIONS();
67 
68 static struct usb_device_descriptor gfs_dev_desc = {
69 	.bLength		= sizeof gfs_dev_desc,
70 	.bDescriptorType	= USB_DT_DEVICE,
71 
72 	.bcdUSB			= cpu_to_le16(0x0200),
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 
79 static char *func_names[GFS_MAX_DEVS];
80 static unsigned int func_num;
81 
82 module_param_named(bDeviceClass,    gfs_dev_desc.bDeviceClass,    byte,   0644);
83 MODULE_PARM_DESC(bDeviceClass, "USB Device class");
84 module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte,   0644);
85 MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
86 module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte,   0644);
87 MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
88 module_param_array_named(functions, func_names, charp, &func_num, 0);
89 MODULE_PARM_DESC(functions, "USB Functions list");
90 
91 static const struct usb_descriptor_header *gfs_otg_desc[] = {
92 	(const struct usb_descriptor_header *)
93 	&(const struct usb_otg_descriptor) {
94 		.bLength		= sizeof(struct usb_otg_descriptor),
95 		.bDescriptorType	= USB_DT_OTG,
96 
97 		/*
98 		 * REVISIT SRP-only hardware is possible, although
99 		 * it would not be called "OTG" ...
100 		 */
101 		.bmAttributes		= USB_OTG_SRP | USB_OTG_HNP,
102 	},
103 
104 	NULL
105 };
106 
107 /* String IDs are assigned dynamically */
108 static struct usb_string gfs_strings[] = {
109 	[USB_GADGET_MANUFACTURER_IDX].s = "",
110 	[USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
111 	[USB_GADGET_SERIAL_IDX].s = "",
112 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
113 	{ .s = "FunctionFS + RNDIS" },
114 #endif
115 #ifdef CONFIG_USB_FUNCTIONFS_ETH
116 	{ .s = "FunctionFS + ECM" },
117 #endif
118 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
119 	{ .s = "FunctionFS" },
120 #endif
121 	{  } /* end of list */
122 };
123 
124 static struct usb_gadget_strings *gfs_dev_strings[] = {
125 	&(struct usb_gadget_strings) {
126 		.language	= 0x0409,	/* en-us */
127 		.strings	= gfs_strings,
128 	},
129 	NULL,
130 };
131 
132 struct gfs_configuration {
133 	struct usb_configuration c;
134 	int (*eth)(struct usb_configuration *c);
135 	int num;
136 } gfs_configurations[] = {
137 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
138 	{
139 		.eth		= bind_rndis_config,
140 	},
141 #endif
142 
143 #ifdef CONFIG_USB_FUNCTIONFS_ETH
144 	{
145 		.eth		= eth_bind_config,
146 	},
147 #endif
148 
149 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
150 	{
151 	},
152 #endif
153 };
154 
155 static void *functionfs_acquire_dev(struct ffs_dev *dev);
156 static void functionfs_release_dev(struct ffs_dev *dev);
157 static int functionfs_ready_callback(struct ffs_data *ffs);
158 static void functionfs_closed_callback(struct ffs_data *ffs);
159 static int gfs_bind(struct usb_composite_dev *cdev);
160 static int gfs_unbind(struct usb_composite_dev *cdev);
161 static int gfs_do_config(struct usb_configuration *c);
162 
163 
164 static __refdata struct usb_composite_driver gfs_driver = {
165 	.name		= DRIVER_NAME,
166 	.dev		= &gfs_dev_desc,
167 	.strings	= gfs_dev_strings,
168 	.max_speed	= USB_SPEED_HIGH,
169 	.bind		= gfs_bind,
170 	.unbind		= gfs_unbind,
171 };
172 
173 static unsigned int missing_funcs;
174 static bool gfs_registered;
175 static bool gfs_single_func;
176 static struct usb_function_instance **fi_ffs;
177 static struct usb_function **f_ffs[] = {
178 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
179 	NULL,
180 #endif
181 
182 #ifdef CONFIG_USB_FUNCTIONFS_ETH
183 	NULL,
184 #endif
185 
186 #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
187 	NULL,
188 #endif
189 };
190 
191 #define N_CONF ARRAY_SIZE(f_ffs)
192 
193 static int __init gfs_init(void)
194 {
195 	struct f_fs_opts *opts;
196 	int i;
197 	int ret = 0;
198 
199 	ENTER();
200 
201 	if (func_num < 2) {
202 		gfs_single_func = true;
203 		func_num = 1;
204 	}
205 
206 	/*
207 	 * Allocate in one chunk for easier maintenance
208 	 */
209 	f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL);
210 	if (!f_ffs[0]) {
211 		ret = -ENOMEM;
212 		goto no_func;
213 	}
214 	for (i = 1; i < N_CONF; ++i)
215 		f_ffs[i] = f_ffs[0] + i * func_num;
216 
217 	fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL);
218 	if (!fi_ffs) {
219 		ret = -ENOMEM;
220 		goto no_func;
221 	}
222 
223 	for (i = 0; i < func_num; i++) {
224 		fi_ffs[i] = usb_get_function_instance("ffs");
225 		if (IS_ERR(fi_ffs[i])) {
226 			ret = PTR_ERR(fi_ffs[i]);
227 			--i;
228 			goto no_dev;
229 		}
230 		opts = to_f_fs_opts(fi_ffs[i]);
231 		if (gfs_single_func)
232 			ret = ffs_single_dev(opts->dev);
233 		else
234 			ret = ffs_name_dev(opts->dev, func_names[i]);
235 		if (ret)
236 			goto no_dev;
237 		opts->dev->ffs_ready_callback = functionfs_ready_callback;
238 		opts->dev->ffs_closed_callback = functionfs_closed_callback;
239 		opts->dev->ffs_acquire_dev_callback = functionfs_acquire_dev;
240 		opts->dev->ffs_release_dev_callback = functionfs_release_dev;
241 		opts->no_configfs = true;
242 	}
243 
244 	missing_funcs = func_num;
245 
246 	return 0;
247 no_dev:
248 	while (i >= 0)
249 		usb_put_function_instance(fi_ffs[i--]);
250 	kfree(fi_ffs);
251 no_func:
252 	kfree(f_ffs[0]);
253 	return ret;
254 }
255 module_init(gfs_init);
256 
257 static void __exit gfs_exit(void)
258 {
259 	int i;
260 
261 	ENTER();
262 
263 	if (gfs_registered)
264 		usb_composite_unregister(&gfs_driver);
265 	gfs_registered = false;
266 
267 	kfree(f_ffs[0]);
268 
269 	for (i = 0; i < func_num; i++)
270 		usb_put_function_instance(fi_ffs[i]);
271 
272 	kfree(fi_ffs);
273 }
274 module_exit(gfs_exit);
275 
276 static void *functionfs_acquire_dev(struct ffs_dev *dev)
277 {
278 	if (!try_module_get(THIS_MODULE))
279 		return ERR_PTR(-ENOENT);
280 
281 	return 0;
282 }
283 
284 static void functionfs_release_dev(struct ffs_dev *dev)
285 {
286 	module_put(THIS_MODULE);
287 }
288 
289 /*
290  * The caller of this function takes ffs_lock
291  */
292 static int functionfs_ready_callback(struct ffs_data *ffs)
293 {
294 	int ret = 0;
295 
296 	if (--missing_funcs)
297 		return 0;
298 
299 	if (gfs_registered)
300 		return -EBUSY;
301 
302 	gfs_registered = true;
303 
304 	ret = usb_composite_probe(&gfs_driver);
305 	if (unlikely(ret < 0))
306 		gfs_registered = false;
307 
308 	return ret;
309 }
310 
311 /*
312  * The caller of this function takes ffs_lock
313  */
314 static void functionfs_closed_callback(struct ffs_data *ffs)
315 {
316 	missing_funcs++;
317 
318 	if (gfs_registered)
319 		usb_composite_unregister(&gfs_driver);
320 	gfs_registered = false;
321 }
322 
323 /*
324  * It is assumed that gfs_bind is called from a context where ffs_lock is held
325  */
326 static int gfs_bind(struct usb_composite_dev *cdev)
327 {
328 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
329 	struct net_device *net;
330 #endif
331 	int ret, i;
332 
333 	ENTER();
334 
335 	if (missing_funcs)
336 		return -ENODEV;
337 #if defined CONFIG_USB_FUNCTIONFS_ETH
338 	if (can_support_ecm(cdev->gadget)) {
339 		struct f_ecm_opts *ecm_opts;
340 
341 		fi_ecm = usb_get_function_instance("ecm");
342 		if (IS_ERR(fi_ecm))
343 			return PTR_ERR(fi_ecm);
344 		ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
345 		net = ecm_opts->net;
346 	} else {
347 		struct f_gether_opts *geth_opts;
348 
349 		fi_geth = usb_get_function_instance("geth");
350 		if (IS_ERR(fi_geth))
351 			return PTR_ERR(fi_geth);
352 		geth_opts = container_of(fi_geth, struct f_gether_opts,
353 					 func_inst);
354 		net = geth_opts->net;
355 	}
356 #endif
357 
358 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
359 	{
360 		struct f_rndis_opts *rndis_opts;
361 
362 		fi_rndis = usb_get_function_instance("rndis");
363 		if (IS_ERR(fi_rndis)) {
364 			ret = PTR_ERR(fi_rndis);
365 			goto error;
366 		}
367 		rndis_opts = container_of(fi_rndis, struct f_rndis_opts,
368 					  func_inst);
369 #ifndef CONFIG_USB_FUNCTIONFS_ETH
370 		net = rndis_opts->net;
371 #endif
372 	}
373 #endif
374 
375 #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
376 	gether_set_qmult(net, qmult);
377 	if (!gether_set_host_addr(net, host_addr))
378 		pr_info("using host ethernet address: %s", host_addr);
379 	if (!gether_set_dev_addr(net, dev_addr))
380 		pr_info("using self ethernet address: %s", dev_addr);
381 #endif
382 
383 #if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
384 	gether_set_gadget(net, cdev->gadget);
385 	ret = gether_register_netdev(net);
386 	if (ret)
387 		goto error_rndis;
388 
389 	if (can_support_ecm(cdev->gadget)) {
390 		struct f_ecm_opts *ecm_opts;
391 
392 		ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
393 		ecm_opts->bound = true;
394 	} else {
395 		struct f_gether_opts *geth_opts;
396 
397 		geth_opts = container_of(fi_geth, struct f_gether_opts,
398 					 func_inst);
399 		geth_opts->bound = true;
400 	}
401 
402 	rndis_borrow_net(fi_rndis, net);
403 #endif
404 
405 	/* TODO: gstrings_attach? */
406 	ret = usb_string_ids_tab(cdev, gfs_strings);
407 	if (unlikely(ret < 0))
408 		goto error_rndis;
409 	gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
410 
411 	for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
412 		struct gfs_configuration *c = gfs_configurations + i;
413 		int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
414 
415 		c->c.label			= gfs_strings[sid].s;
416 		c->c.iConfiguration		= gfs_strings[sid].id;
417 		c->c.bConfigurationValue	= 1 + i;
418 		c->c.bmAttributes		= USB_CONFIG_ATT_SELFPOWER;
419 
420 		c->num = i;
421 
422 		ret = usb_add_config(cdev, &c->c, gfs_do_config);
423 		if (unlikely(ret < 0))
424 			goto error_unbind;
425 	}
426 	usb_composite_overwrite_options(cdev, &coverwrite);
427 	return 0;
428 
429 /* TODO */
430 error_unbind:
431 error_rndis:
432 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
433 	usb_put_function_instance(fi_rndis);
434 error:
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  * It is assumed that gfs_unbind is called from a context where ffs_lock is held
447  */
448 static 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 	return 0;
473 }
474 
475 /*
476  * It is assumed that gfs_do_config is called from a context where
477  * ffs_lock is held
478  */
479 static int gfs_do_config(struct usb_configuration *c)
480 {
481 	struct gfs_configuration *gc =
482 		container_of(c, struct gfs_configuration, c);
483 	int i;
484 	int ret;
485 
486 	if (missing_funcs)
487 		return -ENODEV;
488 
489 	if (gadget_is_otg(c->cdev->gadget)) {
490 		c->descriptors = gfs_otg_desc;
491 		c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
492 	}
493 
494 	if (gc->eth) {
495 		ret = gc->eth(c);
496 		if (unlikely(ret < 0))
497 			return ret;
498 	}
499 
500 	for (i = 0; i < func_num; i++) {
501 		f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]);
502 		if (IS_ERR(f_ffs[gc->num][i])) {
503 			ret = PTR_ERR(f_ffs[gc->num][i]);
504 			goto error;
505 		}
506 		ret = usb_add_function(c, f_ffs[gc->num][i]);
507 		if (ret < 0) {
508 			usb_put_function(f_ffs[gc->num][i]);
509 			goto error;
510 		}
511 	}
512 
513 	/*
514 	 * After previous do_configs there may be some invalid
515 	 * pointers in c->interface array.  This happens every time
516 	 * a user space function with fewer interfaces than a user
517 	 * space function that was run before the new one is run.  The
518 	 * compasit's set_config() assumes that if there is no more
519 	 * then MAX_CONFIG_INTERFACES interfaces in a configuration
520 	 * then there is a NULL pointer after the last interface in
521 	 * c->interface array.  We need to make sure this is true.
522 	 */
523 	if (c->next_interface_id < ARRAY_SIZE(c->interface))
524 		c->interface[c->next_interface_id] = NULL;
525 
526 	return 0;
527 error:
528 	while (--i >= 0) {
529 		if (!IS_ERR(f_ffs[gc->num][i]))
530 			usb_remove_function(c, f_ffs[gc->num][i]);
531 		usb_put_function(f_ffs[gc->num][i]);
532 	}
533 	return ret;
534 }
535 
536 #ifdef CONFIG_USB_FUNCTIONFS_ETH
537 
538 static int eth_bind_config(struct usb_configuration *c)
539 {
540 	int status = 0;
541 
542 	if (can_support_ecm(c->cdev->gadget)) {
543 		f_ecm = usb_get_function(fi_ecm);
544 		if (IS_ERR(f_ecm))
545 			return PTR_ERR(f_ecm);
546 
547 		status = usb_add_function(c, f_ecm);
548 		if (status < 0)
549 			usb_put_function(f_ecm);
550 
551 	} else {
552 		f_geth = usb_get_function(fi_geth);
553 		if (IS_ERR(f_geth))
554 			return PTR_ERR(f_geth);
555 
556 		status = usb_add_function(c, f_geth);
557 		if (status < 0)
558 			usb_put_function(f_geth);
559 	}
560 	return status;
561 }
562 
563 #endif
564 
565 #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
566 
567 static int bind_rndis_config(struct usb_configuration *c)
568 {
569 	int status = 0;
570 
571 	f_rndis = usb_get_function(fi_rndis);
572 	if (IS_ERR(f_rndis))
573 		return PTR_ERR(f_rndis);
574 
575 	status = usb_add_function(c, f_rndis);
576 	if (status < 0)
577 		usb_put_function(f_rndis);
578 
579 	return status;
580 }
581 
582 #endif
583