Merge branch 'for-linus-4.3' of git://git.kernel.org/pub/scm/linux/kernel/git/mason...
[deliverable/linux.git] / drivers / usb / gadget / legacy / acm_ms.c
CommitLineData
fa3ae0c1
KS
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>
721e2e91 18#include <linux/module.h>
fa3ae0c1
KS
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
e6c661ef 34#include "f_mass_storage.h"
fa3ae0c1
KS
35
36/*-------------------------------------------------------------------------*/
7d16e8d3 37USB_GADGET_COMPOSITE_OPTIONS();
fa3ae0c1
KS
38
39static struct usb_device_descriptor device_desc = {
40 .bLength = sizeof device_desc,
41 .bDescriptorType = USB_DT_DEVICE,
42
43 .bcdUSB = cpu_to_le16(0x0200),
44
45 .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
46 .bDeviceSubClass = 2,
47 .bDeviceProtocol = 1,
48
49 /* .bMaxPacketSize0 = f(hardware) */
50
51 /* Vendor and product id can be overridden by module parameters. */
52 .idVendor = cpu_to_le16(ACM_MS_VENDOR_NUM),
53 .idProduct = cpu_to_le16(ACM_MS_PRODUCT_NUM),
54 /* .bcdDevice = f(hardware) */
55 /* .iManufacturer = DYNAMIC */
56 /* .iProduct = DYNAMIC */
57 /* NO SERIAL NUMBER */
58 /*.bNumConfigurations = DYNAMIC*/
59};
60
578aa8a2 61static const struct usb_descriptor_header *otg_desc[2];
fa3ae0c1 62
fa3ae0c1 63/* string IDs are assigned dynamically */
fa3ae0c1 64static struct usb_string strings_dev[] = {
cc2683c3 65 [USB_GADGET_MANUFACTURER_IDX].s = "",
276e2e4f
SAS
66 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
67 [USB_GADGET_SERIAL_IDX].s = "",
fa3ae0c1
KS
68 { } /* end of list */
69};
70
71static struct usb_gadget_strings stringtab_dev = {
72 .language = 0x0409, /* en-us */
73 .strings = strings_dev,
74};
75
76static struct usb_gadget_strings *dev_strings[] = {
77 &stringtab_dev,
78 NULL,
79};
80
81/****************************** Configurations ******************************/
82
83static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
6fdc5dd2
AP
84#ifdef CONFIG_USB_GADGET_DEBUG_FILES
85
86static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
87
88#else
89
90/*
91 * Number of buffers we will use.
92 * 2 is usually enough for good buffering pipeline
93 */
94#define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
95
0c8b1992 96#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
6fdc5dd2 97
fa3ae0c1
KS
98FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
99
fa3ae0c1 100/*-------------------------------------------------------------------------*/
5f72bbfd
SAS
101static struct usb_function *f_acm;
102static struct usb_function_instance *f_acm_inst;
e6c661ef
AP
103
104static struct usb_function_instance *fi_msg;
105static struct usb_function *f_msg;
106
fa3ae0c1
KS
107/*
108 * We _always_ have both ACM and mass storage functions.
109 */
c94e289f 110static int acm_ms_do_config(struct usb_configuration *c)
fa3ae0c1 111{
e6c661ef 112 struct fsg_opts *opts;
fa3ae0c1
KS
113 int status;
114
115 if (gadget_is_otg(c->cdev->gadget)) {
116 c->descriptors = otg_desc;
117 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
118 }
119
e6c661ef 120 opts = fsg_opts_from_func_inst(fi_msg);
5f72bbfd 121
5f72bbfd 122 f_acm = usb_get_function(f_acm_inst);
e6c661ef
AP
123 if (IS_ERR(f_acm))
124 return PTR_ERR(f_acm);
125
126 f_msg = usb_get_function(fi_msg);
127 if (IS_ERR(f_msg)) {
128 status = PTR_ERR(f_msg);
129 goto put_acm;
5f72bbfd 130 }
fa3ae0c1 131
5f72bbfd 132 status = usb_add_function(c, f_acm);
fa3ae0c1 133 if (status < 0)
e6c661ef 134 goto put_msg;
fa3ae0c1 135
e6c661ef
AP
136 status = fsg_common_run_thread(opts->common);
137 if (status)
138 goto remove_acm;
139
140 status = usb_add_function(c, f_msg);
141 if (status)
142 goto remove_acm;
fa3ae0c1
KS
143
144 return 0;
e6c661ef 145remove_acm:
5f72bbfd 146 usb_remove_function(c, f_acm);
e6c661ef
AP
147put_msg:
148 usb_put_function(f_msg);
149put_acm:
5f72bbfd 150 usb_put_function(f_acm);
5f72bbfd 151 return status;
fa3ae0c1
KS
152}
153
154static struct usb_configuration acm_ms_config_driver = {
155 .label = DRIVER_DESC,
156 .bConfigurationValue = 1,
157 /* .iConfiguration = DYNAMIC */
158 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
159};
160
161/*-------------------------------------------------------------------------*/
162
c94e289f 163static int acm_ms_bind(struct usb_composite_dev *cdev)
fa3ae0c1 164{
fa3ae0c1 165 struct usb_gadget *gadget = cdev->gadget;
e6c661ef
AP
166 struct fsg_opts *opts;
167 struct fsg_config config;
fa3ae0c1 168 int status;
fa3ae0c1 169
e6c661ef
AP
170 f_acm_inst = usb_get_function_instance("acm");
171 if (IS_ERR(f_acm_inst))
172 return PTR_ERR(f_acm_inst);
173
174 fi_msg = usb_get_function_instance("mass_storage");
175 if (IS_ERR(fi_msg)) {
176 status = PTR_ERR(fi_msg);
177 goto fail_get_msg;
fa3ae0c1
KS
178 }
179
e6c661ef
AP
180 /* set up mass storage function */
181 fsg_config_from_params(&config, &fsg_mod_data, fsg_num_buffers);
182 opts = fsg_opts_from_func_inst(fi_msg);
183
184 opts->no_configfs = true;
185 status = fsg_common_set_num_buffers(opts->common, fsg_num_buffers);
186 if (status)
187 goto fail;
188
e6c661ef
AP
189 status = fsg_common_set_cdev(opts->common, cdev, config.can_stall);
190 if (status)
191 goto fail_set_cdev;
192
193 fsg_common_set_sysfs(opts->common, true);
194 status = fsg_common_create_luns(opts->common, &config);
195 if (status)
196 goto fail_set_cdev;
197
198 fsg_common_set_inquiry_string(opts->common, config.vendor_name,
199 config.product_name);
fa3ae0c1
KS
200 /*
201 * Allocate string descriptor numbers ... note that string
202 * contents can be overridden by the composite_dev glue.
203 */
e1f15ccb 204 status = usb_string_ids_tab(cdev, strings_dev);
fa3ae0c1 205 if (status < 0)
e6c661ef 206 goto fail_string_ids;
276e2e4f
SAS
207 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
208 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
fa3ae0c1 209
578aa8a2
LJ
210 if (gadget_is_otg(gadget) && !otg_desc[0]) {
211 struct usb_descriptor_header *usb_desc;
212
213 usb_desc = usb_otg_descriptor_alloc(gadget);
214 if (!usb_desc)
215 goto fail_string_ids;
216 usb_otg_descriptor_init(gadget, usb_desc);
217 otg_desc[0] = usb_desc;
218 otg_desc[1] = NULL;
219 }
220
fa3ae0c1
KS
221 /* register our configuration */
222 status = usb_add_config(cdev, &acm_ms_config_driver, acm_ms_do_config);
223 if (status < 0)
578aa8a2 224 goto fail_otg_desc;
fa3ae0c1 225
7d16e8d3 226 usb_composite_overwrite_options(cdev, &coverwrite);
fa3ae0c1
KS
227 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
228 DRIVER_DESC);
fa3ae0c1
KS
229 return 0;
230
231 /* error recovery */
578aa8a2
LJ
232fail_otg_desc:
233 kfree(otg_desc[0]);
234 otg_desc[0] = NULL;
e6c661ef
AP
235fail_string_ids:
236 fsg_common_remove_luns(opts->common);
237fail_set_cdev:
e6c661ef
AP
238 fsg_common_free_buffers(opts->common);
239fail:
240 usb_put_function_instance(fi_msg);
241fail_get_msg:
242 usb_put_function_instance(f_acm_inst);
fa3ae0c1
KS
243 return status;
244}
245
c94e289f 246static int acm_ms_unbind(struct usb_composite_dev *cdev)
fa3ae0c1 247{
e6c661ef
AP
248 usb_put_function(f_msg);
249 usb_put_function_instance(fi_msg);
5f72bbfd
SAS
250 usb_put_function(f_acm);
251 usb_put_function_instance(f_acm_inst);
578aa8a2
LJ
252 kfree(otg_desc[0]);
253 otg_desc[0] = NULL;
254
fa3ae0c1
KS
255 return 0;
256}
257
c94e289f 258static struct usb_composite_driver acm_ms_driver = {
fa3ae0c1
KS
259 .name = "g_acm_ms",
260 .dev = &device_desc,
6f47209b 261 .max_speed = USB_SPEED_SUPER,
fa3ae0c1 262 .strings = dev_strings,
03e42bd5 263 .bind = acm_ms_bind,
c94e289f 264 .unbind = acm_ms_unbind,
fa3ae0c1
KS
265};
266
909346a8
TK
267module_usb_composite_driver(acm_ms_driver);
268
fa3ae0c1
KS
269MODULE_DESCRIPTION(DRIVER_DESC);
270MODULE_AUTHOR("Klaus Schwarzkopf <schwarzkopf@sensortherm.de>");
271MODULE_LICENSE("GPL v2");
This page took 0.366259 seconds and 5 git commands to generate.