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