1/*
2 * acm_ms.c -- Composite driver, with ACM and mass storage support
3 *
4 * Copyright (C) 2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
6 * Author: David Brownell
7 * Modified: Klaus Schwarzkopf <schwarzkopf@sensortherm.de>
8 *
9 * Heavily based on multi.c and cdc2.c
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
17#include <linux/kernel.h>
18#include <linux/utsname.h>
19
20#include "u_serial.h"
21
22#define DRIVER_DESC		"Composite Gadget (ACM + MS)"
23#define DRIVER_VERSION		"2011/10/10"
24
25/*-------------------------------------------------------------------------*/
26
27/*
28 * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
29 * Instead:  allocate your own, using normal USB-IF procedures.
30 */
31#define ACM_MS_VENDOR_NUM	0x1d6b	/* Linux Foundation */
32#define ACM_MS_PRODUCT_NUM	0x0106	/* Composite Gadget: ACM + MS*/
33
34/*-------------------------------------------------------------------------*/
35
36/*
37 * Kbuild is not very cooperative with respect to linking separately
38 * compiled library objects into one module.  So for now we won't use
39 * separate compilation ... ensuring init/exit sections work to shrink
40 * the runtime footprint, and giving us at least some parts of what
41 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
42 */
43
44#include "composite.c"
45#include "usbstring.c"
46#include "config.c"
47#include "epautoconf.c"
48#include "u_serial.c"
49#include "f_acm.c"
50#include "f_mass_storage.c"
51
52/*-------------------------------------------------------------------------*/
53
54static struct usb_device_descriptor device_desc = {
55	.bLength =		sizeof device_desc,
56	.bDescriptorType =	USB_DT_DEVICE,
57
58	.bcdUSB =		cpu_to_le16(0x0200),
59
60	.bDeviceClass =		USB_CLASS_MISC /* 0xEF */,
61	.bDeviceSubClass =	2,
62	.bDeviceProtocol =	1,
63
64	/* .bMaxPacketSize0 = f(hardware) */
65
66	/* Vendor and product id can be overridden by module parameters.  */
67	.idVendor =		cpu_to_le16(ACM_MS_VENDOR_NUM),
68	.idProduct =		cpu_to_le16(ACM_MS_PRODUCT_NUM),
69	/* .bcdDevice = f(hardware) */
70	/* .iManufacturer = DYNAMIC */
71	/* .iProduct = DYNAMIC */
72	/* NO SERIAL NUMBER */
73	/*.bNumConfigurations =	DYNAMIC*/
74};
75
76static struct usb_otg_descriptor otg_descriptor = {
77	.bLength =		sizeof otg_descriptor,
78	.bDescriptorType =	USB_DT_OTG,
79
80	/*
81	 * REVISIT SRP-only hardware is possible, although
82	 * it would not be called "OTG" ...
83	 */
84	.bmAttributes =		USB_OTG_SRP | USB_OTG_HNP,
85};
86
87static const struct usb_descriptor_header *otg_desc[] = {
88	(struct usb_descriptor_header *) &otg_descriptor,
89	NULL,
90};
91
92
93/* string IDs are assigned dynamically */
94
95#define STRING_MANUFACTURER_IDX		0
96#define STRING_PRODUCT_IDX		1
97
98static char manufacturer[50];
99
100static struct usb_string strings_dev[] = {
101	[STRING_MANUFACTURER_IDX].s = manufacturer,
102	[STRING_PRODUCT_IDX].s = DRIVER_DESC,
103	{  } /* end of list */
104};
105
106static struct usb_gadget_strings stringtab_dev = {
107	.language	= 0x0409,	/* en-us */
108	.strings	= strings_dev,
109};
110
111static struct usb_gadget_strings *dev_strings[] = {
112	&stringtab_dev,
113	NULL,
114};
115
116/****************************** Configurations ******************************/
117
118static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
119FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
120
121static struct fsg_common fsg_common;
122
123/*-------------------------------------------------------------------------*/
124
125/*
126 * We _always_ have both ACM and mass storage functions.
127 */
128static int __init acm_ms_do_config(struct usb_configuration *c)
129{
130	int	status;
131
132	if (gadget_is_otg(c->cdev->gadget)) {
133		c->descriptors = otg_desc;
134		c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
135	}
136
137
138	status = acm_bind_config(c, 0);
139	if (status < 0)
140		return status;
141
142	status = fsg_bind_config(c->cdev, c, &fsg_common);
143	if (status < 0)
144		return status;
145
146	return 0;
147}
148
149static struct usb_configuration acm_ms_config_driver = {
150	.label			= DRIVER_DESC,
151	.bConfigurationValue	= 1,
152	/* .iConfiguration = DYNAMIC */
153	.bmAttributes		= USB_CONFIG_ATT_SELFPOWER,
154};
155
156/*-------------------------------------------------------------------------*/
157
158static int __init acm_ms_bind(struct usb_composite_dev *cdev)
159{
160	int			gcnum;
161	struct usb_gadget	*gadget = cdev->gadget;
162	int			status;
163	void			*retp;
164
165	/* set up serial link layer */
166	status = gserial_setup(cdev->gadget, 1);
167	if (status < 0)
168		return status;
169
170	/* set up mass storage function */
171	retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
172	if (IS_ERR(retp)) {
173		status = PTR_ERR(retp);
174		goto fail0;
175	}
176
177	/* set bcdDevice */
178	gcnum = usb_gadget_controller_number(gadget);
179	if (gcnum >= 0) {
180		device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
181	} else {
182		WARNING(cdev, "controller '%s' not recognized; trying %s\n",
183				gadget->name,
184				acm_ms_config_driver.label);
185		device_desc.bcdDevice =
186			cpu_to_le16(0x0300 | 0x0099);
187	}
188
189	/*
190	 * Allocate string descriptor numbers ... note that string
191	 * contents can be overridden by the composite_dev glue.
192	 */
193
194	/* device descriptor strings: manufacturer, product */
195	snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
196		init_utsname()->sysname, init_utsname()->release,
197		gadget->name);
198	status = usb_string_id(cdev);
199	if (status < 0)
200		goto fail1;
201	strings_dev[STRING_MANUFACTURER_IDX].id = status;
202	device_desc.iManufacturer = status;
203
204	status = usb_string_id(cdev);
205	if (status < 0)
206		goto fail1;
207	strings_dev[STRING_PRODUCT_IDX].id = status;
208	device_desc.iProduct = status;
209
210	/* register our configuration */
211	status = usb_add_config(cdev, &acm_ms_config_driver, acm_ms_do_config);
212	if (status < 0)
213		goto fail1;
214
215	dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
216			DRIVER_DESC);
217	fsg_common_put(&fsg_common);
218	return 0;
219
220	/* error recovery */
221fail1:
222	fsg_common_put(&fsg_common);
223fail0:
224	gserial_cleanup();
225	return status;
226}
227
228static int __exit acm_ms_unbind(struct usb_composite_dev *cdev)
229{
230	gserial_cleanup();
231
232	return 0;
233}
234
235static struct usb_composite_driver acm_ms_driver = {
236	.name		= "g_acm_ms",
237	.dev		= &device_desc,
238	.strings	= dev_strings,
239	.unbind		= __exit_p(acm_ms_unbind),
240};
241
242MODULE_DESCRIPTION(DRIVER_DESC);
243MODULE_AUTHOR("Klaus Schwarzkopf <schwarzkopf@sensortherm.de>");
244MODULE_LICENSE("GPL v2");
245
246static int __init init(void)
247{
248	return usb_composite_probe(&acm_ms_driver, acm_ms_bind);
249}
250module_init(init);
251
252static void __exit cleanup(void)
253{
254	usb_composite_unregister(&acm_ms_driver);
255}
256module_exit(cleanup);
257