1/*
2 * multi.c -- Multifunction Composite driver
3 *
4 * Copyright (C) 2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
6 * Copyright (C) 2009 Samsung Electronics
7 * Author: Michal Nazarewicz (mina86@mina86.com)
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 */
14
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/netdevice.h>
19
20#include "u_serial.h"
21#if defined USB_ETH_RNDIS
22#  undef USB_ETH_RNDIS
23#endif
24#ifdef CONFIG_USB_G_MULTI_RNDIS
25#  define USB_ETH_RNDIS y
26#endif
27
28
29#define DRIVER_DESC		"Multifunction Composite Gadget"
30
31MODULE_DESCRIPTION(DRIVER_DESC);
32MODULE_AUTHOR("Michal Nazarewicz");
33MODULE_LICENSE("GPL");
34
35
36#include "f_mass_storage.h"
37
38#include "u_ecm.h"
39#ifdef USB_ETH_RNDIS
40#  include "u_rndis.h"
41#  include "rndis.h"
42#endif
43#include "u_ether.h"
44
45USB_GADGET_COMPOSITE_OPTIONS();
46
47USB_ETHERNET_MODULE_PARAMETERS();
48
49/***************************** Device Descriptor ****************************/
50
51#define MULTI_VENDOR_NUM	0x1d6b	/* Linux Foundation */
52#define MULTI_PRODUCT_NUM	0x0104	/* Multifunction Composite Gadget */
53
54
55enum {
56	__MULTI_NO_CONFIG,
57#ifdef CONFIG_USB_G_MULTI_RNDIS
58	MULTI_RNDIS_CONFIG_NUM,
59#endif
60#ifdef CONFIG_USB_G_MULTI_CDC
61	MULTI_CDC_CONFIG_NUM,
62#endif
63};
64
65
66static struct usb_device_descriptor device_desc = {
67	.bLength =		sizeof device_desc,
68	.bDescriptorType =	USB_DT_DEVICE,
69
70	.bcdUSB =		cpu_to_le16(0x0200),
71
72	.bDeviceClass =		USB_CLASS_MISC /* 0xEF */,
73	.bDeviceSubClass =	2,
74	.bDeviceProtocol =	1,
75
76	/* Vendor and product id can be overridden by module parameters.  */
77	.idVendor =		cpu_to_le16(MULTI_VENDOR_NUM),
78	.idProduct =		cpu_to_le16(MULTI_PRODUCT_NUM),
79};
80
81
82static const struct usb_descriptor_header *otg_desc[] = {
83	(struct usb_descriptor_header *) &(struct usb_otg_descriptor){
84		.bLength =		sizeof(struct usb_otg_descriptor),
85		.bDescriptorType =	USB_DT_OTG,
86
87		/*
88		 * REVISIT SRP-only hardware is possible, although
89		 * it would not be called "OTG" ...
90		 */
91		.bmAttributes =		USB_OTG_SRP | USB_OTG_HNP,
92	},
93	NULL,
94};
95
96
97enum {
98	MULTI_STRING_RNDIS_CONFIG_IDX = USB_GADGET_FIRST_AVAIL_IDX,
99	MULTI_STRING_CDC_CONFIG_IDX,
100};
101
102static struct usb_string strings_dev[] = {
103	[USB_GADGET_MANUFACTURER_IDX].s = "",
104	[USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
105	[USB_GADGET_SERIAL_IDX].s = "",
106	[MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS",
107	[MULTI_STRING_CDC_CONFIG_IDX].s   = "Multifunction with CDC ECM",
108	{  } /* end of list */
109};
110
111static struct usb_gadget_strings *dev_strings[] = {
112	&(struct usb_gadget_strings){
113		.language	= 0x0409,	/* en-us */
114		.strings	= strings_dev,
115	},
116	NULL,
117};
118
119
120
121
122/****************************** Configurations ******************************/
123
124static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
125#ifdef CONFIG_USB_GADGET_DEBUG_FILES
126
127static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
128
129#else
130
131/*
132 * Number of buffers we will use.
133 * 2 is usually enough for good buffering pipeline
134 */
135#define fsg_num_buffers	CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
136
137#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
138
139FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
140
141static struct usb_function_instance *fi_acm;
142static struct usb_function_instance *fi_msg;
143
144/********** RNDIS **********/
145
146#ifdef USB_ETH_RNDIS
147static struct usb_function_instance *fi_rndis;
148static struct usb_function *f_acm_rndis;
149static struct usb_function *f_rndis;
150static struct usb_function *f_msg_rndis;
151
152static int rndis_do_config(struct usb_configuration *c)
153{
154	int ret;
155
156	if (gadget_is_otg(c->cdev->gadget)) {
157		c->descriptors = otg_desc;
158		c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
159	}
160
161	f_rndis = usb_get_function(fi_rndis);
162	if (IS_ERR(f_rndis))
163		return PTR_ERR(f_rndis);
164
165	ret = usb_add_function(c, f_rndis);
166	if (ret < 0)
167		goto err_func_rndis;
168
169	f_acm_rndis = usb_get_function(fi_acm);
170	if (IS_ERR(f_acm_rndis)) {
171		ret = PTR_ERR(f_acm_rndis);
172		goto err_func_acm;
173	}
174
175	ret = usb_add_function(c, f_acm_rndis);
176	if (ret)
177		goto err_conf;
178
179	f_msg_rndis = usb_get_function(fi_msg);
180	if (IS_ERR(f_msg_rndis)) {
181		ret = PTR_ERR(f_msg_rndis);
182		goto err_fsg;
183	}
184
185	ret = usb_add_function(c, f_msg_rndis);
186	if (ret)
187		goto err_run;
188
189	return 0;
190err_run:
191	usb_put_function(f_msg_rndis);
192err_fsg:
193	usb_remove_function(c, f_acm_rndis);
194err_conf:
195	usb_put_function(f_acm_rndis);
196err_func_acm:
197	usb_remove_function(c, f_rndis);
198err_func_rndis:
199	usb_put_function(f_rndis);
200	return ret;
201}
202
203static __ref int rndis_config_register(struct usb_composite_dev *cdev)
204{
205	static struct usb_configuration config = {
206		.bConfigurationValue	= MULTI_RNDIS_CONFIG_NUM,
207		.bmAttributes		= USB_CONFIG_ATT_SELFPOWER,
208	};
209
210	config.label          = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
211	config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
212
213	return usb_add_config(cdev, &config, rndis_do_config);
214}
215
216#else
217
218static __ref int rndis_config_register(struct usb_composite_dev *cdev)
219{
220	return 0;
221}
222
223#endif
224
225
226/********** CDC ECM **********/
227
228#ifdef CONFIG_USB_G_MULTI_CDC
229static struct usb_function_instance *fi_ecm;
230static struct usb_function *f_acm_multi;
231static struct usb_function *f_ecm;
232static struct usb_function *f_msg_multi;
233
234static int cdc_do_config(struct usb_configuration *c)
235{
236	int ret;
237
238	if (gadget_is_otg(c->cdev->gadget)) {
239		c->descriptors = otg_desc;
240		c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
241	}
242
243	f_ecm = usb_get_function(fi_ecm);
244	if (IS_ERR(f_ecm))
245		return PTR_ERR(f_ecm);
246
247	ret = usb_add_function(c, f_ecm);
248	if (ret < 0)
249		goto err_func_ecm;
250
251	/* implicit port_num is zero */
252	f_acm_multi = usb_get_function(fi_acm);
253	if (IS_ERR(f_acm_multi)) {
254		ret = PTR_ERR(f_acm_multi);
255		goto err_func_acm;
256	}
257
258	ret = usb_add_function(c, f_acm_multi);
259	if (ret)
260		goto err_conf;
261
262	f_msg_multi = usb_get_function(fi_msg);
263	if (IS_ERR(f_msg_multi)) {
264		ret = PTR_ERR(f_msg_multi);
265		goto err_fsg;
266	}
267
268	ret = usb_add_function(c, f_msg_multi);
269	if (ret)
270		goto err_run;
271
272	return 0;
273err_run:
274	usb_put_function(f_msg_multi);
275err_fsg:
276	usb_remove_function(c, f_acm_multi);
277err_conf:
278	usb_put_function(f_acm_multi);
279err_func_acm:
280	usb_remove_function(c, f_ecm);
281err_func_ecm:
282	usb_put_function(f_ecm);
283	return ret;
284}
285
286static __ref int cdc_config_register(struct usb_composite_dev *cdev)
287{
288	static struct usb_configuration config = {
289		.bConfigurationValue	= MULTI_CDC_CONFIG_NUM,
290		.bmAttributes		= USB_CONFIG_ATT_SELFPOWER,
291	};
292
293	config.label          = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
294	config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
295
296	return usb_add_config(cdev, &config, cdc_do_config);
297}
298
299#else
300
301static __ref int cdc_config_register(struct usb_composite_dev *cdev)
302{
303	return 0;
304}
305
306#endif
307
308
309
310/****************************** Gadget Bind ******************************/
311
312static int __ref multi_bind(struct usb_composite_dev *cdev)
313{
314	struct usb_gadget *gadget = cdev->gadget;
315#ifdef CONFIG_USB_G_MULTI_CDC
316	struct f_ecm_opts *ecm_opts;
317#endif
318#ifdef USB_ETH_RNDIS
319	struct f_rndis_opts *rndis_opts;
320#endif
321	struct fsg_opts *fsg_opts;
322	struct fsg_config config;
323	int status;
324
325	if (!can_support_ecm(cdev->gadget)) {
326		dev_err(&gadget->dev, "controller '%s' not usable\n",
327			gadget->name);
328		return -EINVAL;
329	}
330
331#ifdef CONFIG_USB_G_MULTI_CDC
332	fi_ecm = usb_get_function_instance("ecm");
333	if (IS_ERR(fi_ecm))
334		return PTR_ERR(fi_ecm);
335
336	ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
337
338	gether_set_qmult(ecm_opts->net, qmult);
339	if (!gether_set_host_addr(ecm_opts->net, host_addr))
340		pr_info("using host ethernet address: %s", host_addr);
341	if (!gether_set_dev_addr(ecm_opts->net, dev_addr))
342		pr_info("using self ethernet address: %s", dev_addr);
343#endif
344
345#ifdef USB_ETH_RNDIS
346	fi_rndis = usb_get_function_instance("rndis");
347	if (IS_ERR(fi_rndis)) {
348		status = PTR_ERR(fi_rndis);
349		goto fail;
350	}
351
352	rndis_opts = container_of(fi_rndis, struct f_rndis_opts, func_inst);
353
354	gether_set_qmult(rndis_opts->net, qmult);
355	if (!gether_set_host_addr(rndis_opts->net, host_addr))
356		pr_info("using host ethernet address: %s", host_addr);
357	if (!gether_set_dev_addr(rndis_opts->net, dev_addr))
358		pr_info("using self ethernet address: %s", dev_addr);
359#endif
360
361#if (defined CONFIG_USB_G_MULTI_CDC && defined USB_ETH_RNDIS)
362	/*
363	 * If both ecm and rndis are selected then:
364	 *	1) rndis borrows the net interface from ecm
365	 *	2) since the interface is shared it must not be bound
366	 *	twice - in ecm's _and_ rndis' binds, so do it here.
367	 */
368	gether_set_gadget(ecm_opts->net, cdev->gadget);
369	status = gether_register_netdev(ecm_opts->net);
370	if (status)
371		goto fail0;
372
373	rndis_borrow_net(fi_rndis, ecm_opts->net);
374	ecm_opts->bound = true;
375#endif
376
377	/* set up serial link layer */
378	fi_acm = usb_get_function_instance("acm");
379	if (IS_ERR(fi_acm)) {
380		status = PTR_ERR(fi_acm);
381		goto fail0;
382	}
383
384	/* set up mass storage function */
385	fi_msg = usb_get_function_instance("mass_storage");
386	if (IS_ERR(fi_msg)) {
387		status = PTR_ERR(fi_msg);
388		goto fail1;
389	}
390	fsg_config_from_params(&config, &fsg_mod_data, fsg_num_buffers);
391	fsg_opts = fsg_opts_from_func_inst(fi_msg);
392
393	fsg_opts->no_configfs = true;
394	status = fsg_common_set_num_buffers(fsg_opts->common, fsg_num_buffers);
395	if (status)
396		goto fail2;
397
398	status = fsg_common_set_cdev(fsg_opts->common, cdev, config.can_stall);
399	if (status)
400		goto fail_set_cdev;
401
402	fsg_common_set_sysfs(fsg_opts->common, true);
403	status = fsg_common_create_luns(fsg_opts->common, &config);
404	if (status)
405		goto fail_set_cdev;
406
407	fsg_common_set_inquiry_string(fsg_opts->common, config.vendor_name,
408				      config.product_name);
409
410	/* allocate string IDs */
411	status = usb_string_ids_tab(cdev, strings_dev);
412	if (unlikely(status < 0))
413		goto fail_string_ids;
414	device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
415
416	/* register configurations */
417	status = rndis_config_register(cdev);
418	if (unlikely(status < 0))
419		goto fail_string_ids;
420
421	status = cdc_config_register(cdev);
422	if (unlikely(status < 0))
423		goto fail_string_ids;
424	usb_composite_overwrite_options(cdev, &coverwrite);
425
426	/* we're done */
427	dev_info(&gadget->dev, DRIVER_DESC "\n");
428	return 0;
429
430
431	/* error recovery */
432fail_string_ids:
433	fsg_common_remove_luns(fsg_opts->common);
434fail_set_cdev:
435	fsg_common_free_buffers(fsg_opts->common);
436fail2:
437	usb_put_function_instance(fi_msg);
438fail1:
439	usb_put_function_instance(fi_acm);
440fail0:
441#ifdef USB_ETH_RNDIS
442	usb_put_function_instance(fi_rndis);
443fail:
444#endif
445#ifdef CONFIG_USB_G_MULTI_CDC
446	usb_put_function_instance(fi_ecm);
447#endif
448	return status;
449}
450
451static int multi_unbind(struct usb_composite_dev *cdev)
452{
453#ifdef CONFIG_USB_G_MULTI_CDC
454	usb_put_function(f_msg_multi);
455#endif
456#ifdef USB_ETH_RNDIS
457	usb_put_function(f_msg_rndis);
458#endif
459	usb_put_function_instance(fi_msg);
460#ifdef CONFIG_USB_G_MULTI_CDC
461	usb_put_function(f_acm_multi);
462#endif
463#ifdef USB_ETH_RNDIS
464	usb_put_function(f_acm_rndis);
465#endif
466	usb_put_function_instance(fi_acm);
467#ifdef USB_ETH_RNDIS
468	usb_put_function(f_rndis);
469	usb_put_function_instance(fi_rndis);
470#endif
471#ifdef CONFIG_USB_G_MULTI_CDC
472	usb_put_function(f_ecm);
473	usb_put_function_instance(fi_ecm);
474#endif
475	return 0;
476}
477
478
479/****************************** Some noise ******************************/
480
481
482static struct usb_composite_driver multi_driver = {
483	.name		= "g_multi",
484	.dev		= &device_desc,
485	.strings	= dev_strings,
486	.max_speed	= USB_SPEED_HIGH,
487	.bind		= multi_bind,
488	.unbind		= multi_unbind,
489	.needs_serial	= 1,
490};
491
492module_usb_composite_driver(multi_driver);
493