blob: 35cbe72835144d1d244f9d7cb5e59bca06438540 [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>
Sebastian Andrzej Siewior721e2e92012-09-06 20:11:27 +020018#include <linux/module.h>
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +020019
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
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +020044#include "f_acm.c"
45#include "f_mass_storage.c"
46
47/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +020048USB_GADGET_COMPOSITE_OPTIONS();
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +020049
50static struct usb_device_descriptor device_desc = {
51 .bLength = sizeof device_desc,
52 .bDescriptorType = USB_DT_DEVICE,
53
54 .bcdUSB = cpu_to_le16(0x0200),
55
56 .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
57 .bDeviceSubClass = 2,
58 .bDeviceProtocol = 1,
59
60 /* .bMaxPacketSize0 = f(hardware) */
61
62 /* Vendor and product id can be overridden by module parameters. */
63 .idVendor = cpu_to_le16(ACM_MS_VENDOR_NUM),
64 .idProduct = cpu_to_le16(ACM_MS_PRODUCT_NUM),
65 /* .bcdDevice = f(hardware) */
66 /* .iManufacturer = DYNAMIC */
67 /* .iProduct = DYNAMIC */
68 /* NO SERIAL NUMBER */
69 /*.bNumConfigurations = DYNAMIC*/
70};
71
72static struct usb_otg_descriptor otg_descriptor = {
73 .bLength = sizeof otg_descriptor,
74 .bDescriptorType = USB_DT_OTG,
75
76 /*
77 * REVISIT SRP-only hardware is possible, although
78 * it would not be called "OTG" ...
79 */
80 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
81};
82
83static const struct usb_descriptor_header *otg_desc[] = {
84 (struct usb_descriptor_header *) &otg_descriptor,
85 NULL,
86};
87
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +020088/* string IDs are assigned dynamically */
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +020089static struct usb_string strings_dev[] = {
Sebastian Andrzej Siewiorcc2683c2012-09-10 15:01:58 +020090 [USB_GADGET_MANUFACTURER_IDX].s = "",
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +020091 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
92 [USB_GADGET_SERIAL_IDX].s = "",
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +020093 { } /* end of list */
94};
95
96static struct usb_gadget_strings stringtab_dev = {
97 .language = 0x0409, /* en-us */
98 .strings = strings_dev,
99};
100
101static struct usb_gadget_strings *dev_strings[] = {
102 &stringtab_dev,
103 NULL,
104};
105
106/****************************** Configurations ******************************/
107
108static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
109FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
110
111static struct fsg_common fsg_common;
112
113/*-------------------------------------------------------------------------*/
114
115/*
116 * We _always_ have both ACM and mass storage functions.
117 */
118static int __init acm_ms_do_config(struct usb_configuration *c)
119{
120 int status;
121
122 if (gadget_is_otg(c->cdev->gadget)) {
123 c->descriptors = otg_desc;
124 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
125 }
126
127
128 status = acm_bind_config(c, 0);
129 if (status < 0)
130 return status;
131
132 status = fsg_bind_config(c->cdev, c, &fsg_common);
133 if (status < 0)
134 return status;
135
136 return 0;
137}
138
139static struct usb_configuration acm_ms_config_driver = {
140 .label = DRIVER_DESC,
141 .bConfigurationValue = 1,
142 /* .iConfiguration = DYNAMIC */
143 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
144};
145
146/*-------------------------------------------------------------------------*/
147
148static int __init acm_ms_bind(struct usb_composite_dev *cdev)
149{
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +0200150 struct usb_gadget *gadget = cdev->gadget;
151 int status;
152 void *retp;
153
154 /* set up serial link layer */
155 status = gserial_setup(cdev->gadget, 1);
156 if (status < 0)
157 return status;
158
159 /* set up mass storage function */
160 retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data);
161 if (IS_ERR(retp)) {
162 status = PTR_ERR(retp);
163 goto fail0;
164 }
165
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +0200166 /*
167 * Allocate string descriptor numbers ... note that string
168 * contents can be overridden by the composite_dev glue.
169 */
Sebastian Andrzej Siewiore1f15cc2012-09-06 20:11:16 +0200170 status = usb_string_ids_tab(cdev, strings_dev);
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +0200171 if (status < 0)
172 goto fail1;
Sebastian Andrzej Siewior276e2e42012-09-06 20:11:21 +0200173 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
174 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +0200175
176 /* register our configuration */
177 status = usb_add_config(cdev, &acm_ms_config_driver, acm_ms_do_config);
178 if (status < 0)
179 goto fail1;
180
Sebastian Andrzej Siewior7d16e8d2012-09-10 15:01:53 +0200181 usb_composite_overwrite_options(cdev, &coverwrite);
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +0200182 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
183 DRIVER_DESC);
184 fsg_common_put(&fsg_common);
185 return 0;
186
187 /* error recovery */
188fail1:
189 fsg_common_put(&fsg_common);
190fail0:
191 gserial_cleanup();
192 return status;
193}
194
195static int __exit acm_ms_unbind(struct usb_composite_dev *cdev)
196{
197 gserial_cleanup();
198
199 return 0;
200}
201
Sebastian Andrzej Siewiorc2ec75c2012-09-06 20:11:03 +0200202static __refdata struct usb_composite_driver acm_ms_driver = {
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +0200203 .name = "g_acm_ms",
204 .dev = &device_desc,
Steve Bennett6f472092012-06-22 14:16:19 +1000205 .max_speed = USB_SPEED_SUPER,
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +0200206 .strings = dev_strings,
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200207 .bind = acm_ms_bind,
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +0200208 .unbind = __exit_p(acm_ms_unbind),
209};
210
211MODULE_DESCRIPTION(DRIVER_DESC);
212MODULE_AUTHOR("Klaus Schwarzkopf <schwarzkopf@sensortherm.de>");
213MODULE_LICENSE("GPL v2");
214
215static int __init init(void)
216{
Sebastian Andrzej Siewior03e42bd2012-09-06 20:11:04 +0200217 return usb_composite_probe(&acm_ms_driver);
Klaus Schwarzkopffa3ae0c2011-10-10 10:32:23 +0200218}
219module_init(init);
220
221static void __exit cleanup(void)
222{
223 usb_composite_unregister(&acm_ms_driver);
224}
225module_exit(cleanup);