blob: 9820960dfc5c21dfb46c23d8f64395e482389555 [file] [log] [blame]
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +02001/*
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"
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +020045#include "config.c"
46#include "epautoconf.c"
47#include "u_serial.c"
48#include "f_acm.c"
49#include "f_mass_storage.c"
50
51/*-------------------------------------------------------------------------*/
52
53static struct usb_device_descriptor device_desc = {
54 .bLength = sizeof device_desc,
55 .bDescriptorType = USB_DT_DEVICE,
56
57 .bcdUSB = cpu_to_le16(0x0200),
58
59 .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
60 .bDeviceSubClass = 2,
61 .bDeviceProtocol = 1,
62
63 /* .bMaxPacketSize0 = f(hardware) */
64
65 /* Vendor and product id can be overridden by module parameters. */
66 .idVendor = cpu_to_le16(ACM_MS_VENDOR_NUM),
67 .idProduct = cpu_to_le16(ACM_MS_PRODUCT_NUM),
68 /* .bcdDevice = f(hardware) */
69 /* .iManufacturer = DYNAMIC */
70 /* .iProduct = DYNAMIC */
71 /* NO SERIAL NUMBER */
72 /*.bNumConfigurations = DYNAMIC*/
73};
74
75static struct usb_otg_descriptor otg_descriptor = {
76 .bLength = sizeof otg_descriptor,
77 .bDescriptorType = USB_DT_OTG,
78
79 /*
80 * REVISIT SRP-only hardware is possible, although
81 * it would not be called "OTG" ...
82 */
83 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
84};
85
86static const struct usb_descriptor_header *otg_desc[] = {
87 (struct usb_descriptor_header *) &otg_descriptor,
88 NULL,
89};
90
91
92/* string IDs are assigned dynamically */
93
94#define STRING_MANUFACTURER_IDX 0
95#define STRING_PRODUCT_IDX 1
96
97static char manufacturer[50];
98
99static struct usb_string strings_dev[] = {
100 [STRING_MANUFACTURER_IDX].s = manufacturer,
101 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
102 { } /* end of list */
103};
104
105static struct usb_gadget_strings stringtab_dev = {
106 .language = 0x0409, /* en-us */
107 .strings = strings_dev,
108};
109
110static struct usb_gadget_strings *dev_strings[] = {
111 &stringtab_dev,
112 NULL,
113};
114
115/****************************** Configurations ******************************/
116
117static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
118FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
119
120static struct fsg_common fsg_common;
121
122/*-------------------------------------------------------------------------*/
123
124/*
125 * We _always_ have both ACM and mass storage functions.
126 */
127static int __init acm_ms_do_config(struct usb_configuration *c)
128{
129 int status;
130
131 if (gadget_is_otg(c->cdev->gadget)) {
132 c->descriptors = otg_desc;
133 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
134 }
135
136
137 status = acm_bind_config(c, 0);
138 if (status < 0)
139 return status;
140
141 status = fsg_bind_config(c->cdev, c, &fsg_common);
142 if (status < 0)
143 return status;
144
145 return 0;
146}
147
148static struct usb_configuration acm_ms_config_driver = {
149 .label = DRIVER_DESC,
150 .bConfigurationValue = 1,
151 /* .iConfiguration = DYNAMIC */
152 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
153};
154
155/*-------------------------------------------------------------------------*/
156
157static int __init acm_ms_bind(struct usb_composite_dev *cdev)
158{
159 int gcnum;
160 struct usb_gadget *gadget = cdev->gadget;
161 int status;
162 void *retp;
163
164 /* set up serial link layer */
165 status = gserial_setup(cdev->gadget, 1);
166 if (status < 0)
167 return status;
168
169 /* set up mass storage function */
170 retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
171 if (IS_ERR(retp)) {
172 status = PTR_ERR(retp);
173 goto fail0;
174 }
175
176 /* set bcdDevice */
177 gcnum = usb_gadget_controller_number(gadget);
178 if (gcnum >= 0) {
179 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
180 } else {
181 WARNING(cdev, "controller '%s' not recognized; trying %s\n",
182 gadget->name,
183 acm_ms_config_driver.label);
184 device_desc.bcdDevice =
185 cpu_to_le16(0x0300 | 0x0099);
186 }
187
188 /*
189 * Allocate string descriptor numbers ... note that string
190 * contents can be overridden by the composite_dev glue.
191 */
192
193 /* device descriptor strings: manufacturer, product */
194 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
195 init_utsname()->sysname, init_utsname()->release,
196 gadget->name);
197 status = usb_string_id(cdev);
198 if (status < 0)
199 goto fail1;
200 strings_dev[STRING_MANUFACTURER_IDX].id = status;
201 device_desc.iManufacturer = status;
202
203 status = usb_string_id(cdev);
204 if (status < 0)
205 goto fail1;
206 strings_dev[STRING_PRODUCT_IDX].id = status;
207 device_desc.iProduct = status;
208
209 /* register our configuration */
210 status = usb_add_config(cdev, &acm_ms_config_driver, acm_ms_do_config);
211 if (status < 0)
212 goto fail1;
213
214 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
215 DRIVER_DESC);
216 fsg_common_put(&fsg_common);
217 return 0;
218
219 /* error recovery */
220fail1:
221 fsg_common_put(&fsg_common);
222fail0:
223 gserial_cleanup();
224 return status;
225}
226
227static int __exit acm_ms_unbind(struct usb_composite_dev *cdev)
228{
229 gserial_cleanup();
230
231 return 0;
232}
233
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200234static __refdata struct usb_composite_driver acm_ms_driver = {
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +0200235 .name = "g_acm_ms",
236 .dev = &device_desc,
Steve Bennett6f472092012-06-22 14:16:19 +1000237 .max_speed = USB_SPEED_SUPER,
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +0200238 .strings = dev_strings,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200239 .bind = acm_ms_bind,
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +0200240 .unbind = __exit_p(acm_ms_unbind),
241};
242
243MODULE_DESCRIPTION(DRIVER_DESC);
244MODULE_AUTHOR("Klaus Schwarzkopf <schwarzkopf@sensortherm.de>");
245MODULE_LICENSE("GPL v2");
246
247static int __init init(void)
248{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200249 return usb_composite_probe(&acm_ms_driver);
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +0200250}
251module_init(init);
252
253static void __exit cleanup(void)
254{
255 usb_composite_unregister(&acm_ms_driver);
256}
257module_exit(cleanup);