Commit | Line | Data |
---|---|---|
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 | ||
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 | */ | |
e5eaa0dc | 43 | #define USB_FMS_INCLUDED |
fa3ae0c1 KS |
44 | #include "f_mass_storage.c" |
45 | ||
46 | /*-------------------------------------------------------------------------*/ | |
7d16e8d3 | 47 | USB_GADGET_COMPOSITE_OPTIONS(); |
fa3ae0c1 KS |
48 | |
49 | static struct usb_device_descriptor device_desc = { | |
50 | .bLength = sizeof device_desc, | |
51 | .bDescriptorType = USB_DT_DEVICE, | |
52 | ||
53 | .bcdUSB = cpu_to_le16(0x0200), | |
54 | ||
55 | .bDeviceClass = USB_CLASS_MISC /* 0xEF */, | |
56 | .bDeviceSubClass = 2, | |
57 | .bDeviceProtocol = 1, | |
58 | ||
59 | /* .bMaxPacketSize0 = f(hardware) */ | |
60 | ||
61 | /* Vendor and product id can be overridden by module parameters. */ | |
62 | .idVendor = cpu_to_le16(ACM_MS_VENDOR_NUM), | |
63 | .idProduct = cpu_to_le16(ACM_MS_PRODUCT_NUM), | |
64 | /* .bcdDevice = f(hardware) */ | |
65 | /* .iManufacturer = DYNAMIC */ | |
66 | /* .iProduct = DYNAMIC */ | |
67 | /* NO SERIAL NUMBER */ | |
68 | /*.bNumConfigurations = DYNAMIC*/ | |
69 | }; | |
70 | ||
71 | static struct usb_otg_descriptor otg_descriptor = { | |
72 | .bLength = sizeof otg_descriptor, | |
73 | .bDescriptorType = USB_DT_OTG, | |
74 | ||
75 | /* | |
76 | * REVISIT SRP-only hardware is possible, although | |
77 | * it would not be called "OTG" ... | |
78 | */ | |
79 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, | |
80 | }; | |
81 | ||
82 | static const struct usb_descriptor_header *otg_desc[] = { | |
83 | (struct usb_descriptor_header *) &otg_descriptor, | |
84 | NULL, | |
85 | }; | |
86 | ||
fa3ae0c1 | 87 | /* string IDs are assigned dynamically */ |
fa3ae0c1 | 88 | static struct usb_string strings_dev[] = { |
cc2683c3 | 89 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
276e2e4f SAS |
90 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
91 | [USB_GADGET_SERIAL_IDX].s = "", | |
fa3ae0c1 KS |
92 | { } /* end of list */ |
93 | }; | |
94 | ||
95 | static struct usb_gadget_strings stringtab_dev = { | |
96 | .language = 0x0409, /* en-us */ | |
97 | .strings = strings_dev, | |
98 | }; | |
99 | ||
100 | static struct usb_gadget_strings *dev_strings[] = { | |
101 | &stringtab_dev, | |
102 | NULL, | |
103 | }; | |
104 | ||
105 | /****************************** Configurations ******************************/ | |
106 | ||
107 | static struct fsg_module_parameters fsg_mod_data = { .stall = 1 }; | |
6fdc5dd2 AP |
108 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES |
109 | ||
110 | static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS; | |
111 | ||
112 | #else | |
113 | ||
114 | /* | |
115 | * Number of buffers we will use. | |
116 | * 2 is usually enough for good buffering pipeline | |
117 | */ | |
118 | #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS | |
119 | ||
120 | #endif /* CONFIG_USB_DEBUG */ | |
121 | ||
fa3ae0c1 KS |
122 | FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data); |
123 | ||
124 | static struct fsg_common fsg_common; | |
125 | ||
126 | /*-------------------------------------------------------------------------*/ | |
5f72bbfd SAS |
127 | static struct usb_function *f_acm; |
128 | static struct usb_function_instance *f_acm_inst; | |
fa3ae0c1 KS |
129 | /* |
130 | * We _always_ have both ACM and mass storage functions. | |
131 | */ | |
132 | static int __init acm_ms_do_config(struct usb_configuration *c) | |
133 | { | |
134 | int status; | |
135 | ||
136 | if (gadget_is_otg(c->cdev->gadget)) { | |
137 | c->descriptors = otg_desc; | |
138 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
139 | } | |
140 | ||
5f72bbfd SAS |
141 | f_acm_inst = usb_get_function_instance("acm"); |
142 | if (IS_ERR(f_acm_inst)) | |
143 | return PTR_ERR(f_acm_inst); | |
144 | ||
5f72bbfd SAS |
145 | f_acm = usb_get_function(f_acm_inst); |
146 | if (IS_ERR(f_acm)) { | |
147 | status = PTR_ERR(f_acm); | |
148 | goto err_func; | |
149 | } | |
fa3ae0c1 | 150 | |
5f72bbfd | 151 | status = usb_add_function(c, f_acm); |
fa3ae0c1 | 152 | if (status < 0) |
5f72bbfd | 153 | goto err_conf; |
fa3ae0c1 KS |
154 | |
155 | status = fsg_bind_config(c->cdev, c, &fsg_common); | |
156 | if (status < 0) | |
5f72bbfd | 157 | goto err_fsg; |
fa3ae0c1 KS |
158 | |
159 | return 0; | |
5f72bbfd SAS |
160 | err_fsg: |
161 | usb_remove_function(c, f_acm); | |
162 | err_conf: | |
163 | usb_put_function(f_acm); | |
164 | err_func: | |
165 | usb_put_function_instance(f_acm_inst); | |
166 | return status; | |
fa3ae0c1 KS |
167 | } |
168 | ||
169 | static struct usb_configuration acm_ms_config_driver = { | |
170 | .label = DRIVER_DESC, | |
171 | .bConfigurationValue = 1, | |
172 | /* .iConfiguration = DYNAMIC */ | |
173 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
174 | }; | |
175 | ||
176 | /*-------------------------------------------------------------------------*/ | |
177 | ||
178 | static int __init acm_ms_bind(struct usb_composite_dev *cdev) | |
179 | { | |
fa3ae0c1 KS |
180 | struct usb_gadget *gadget = cdev->gadget; |
181 | int status; | |
182 | void *retp; | |
183 | ||
fa3ae0c1 | 184 | /* set up mass storage function */ |
6fdc5dd2 AP |
185 | retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data, |
186 | fsg_num_buffers); | |
fa3ae0c1 KS |
187 | if (IS_ERR(retp)) { |
188 | status = PTR_ERR(retp); | |
c4ed4ac1 | 189 | return PTR_ERR(retp); |
fa3ae0c1 KS |
190 | } |
191 | ||
fa3ae0c1 KS |
192 | /* |
193 | * Allocate string descriptor numbers ... note that string | |
194 | * contents can be overridden by the composite_dev glue. | |
195 | */ | |
e1f15ccb | 196 | status = usb_string_ids_tab(cdev, strings_dev); |
fa3ae0c1 KS |
197 | if (status < 0) |
198 | goto fail1; | |
276e2e4f SAS |
199 | device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id; |
200 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; | |
fa3ae0c1 KS |
201 | |
202 | /* register our configuration */ | |
203 | status = usb_add_config(cdev, &acm_ms_config_driver, acm_ms_do_config); | |
204 | if (status < 0) | |
205 | goto fail1; | |
206 | ||
7d16e8d3 | 207 | usb_composite_overwrite_options(cdev, &coverwrite); |
fa3ae0c1 KS |
208 | dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n", |
209 | DRIVER_DESC); | |
210 | fsg_common_put(&fsg_common); | |
211 | return 0; | |
212 | ||
213 | /* error recovery */ | |
214 | fail1: | |
215 | fsg_common_put(&fsg_common); | |
fa3ae0c1 KS |
216 | return status; |
217 | } | |
218 | ||
219 | static int __exit acm_ms_unbind(struct usb_composite_dev *cdev) | |
220 | { | |
5f72bbfd SAS |
221 | usb_put_function(f_acm); |
222 | usb_put_function_instance(f_acm_inst); | |
fa3ae0c1 KS |
223 | return 0; |
224 | } | |
225 | ||
c2ec75c2 | 226 | static __refdata struct usb_composite_driver acm_ms_driver = { |
fa3ae0c1 KS |
227 | .name = "g_acm_ms", |
228 | .dev = &device_desc, | |
6f47209b | 229 | .max_speed = USB_SPEED_SUPER, |
fa3ae0c1 | 230 | .strings = dev_strings, |
03e42bd5 | 231 | .bind = acm_ms_bind, |
fa3ae0c1 KS |
232 | .unbind = __exit_p(acm_ms_unbind), |
233 | }; | |
234 | ||
235 | MODULE_DESCRIPTION(DRIVER_DESC); | |
236 | MODULE_AUTHOR("Klaus Schwarzkopf <schwarzkopf@sensortherm.de>"); | |
237 | MODULE_LICENSE("GPL v2"); | |
238 | ||
239 | static int __init init(void) | |
240 | { | |
03e42bd5 | 241 | return usb_composite_probe(&acm_ms_driver); |
fa3ae0c1 KS |
242 | } |
243 | module_init(init); | |
244 | ||
245 | static void __exit cleanup(void) | |
246 | { | |
247 | usb_composite_unregister(&acm_ms_driver); | |
248 | } | |
249 | module_exit(cleanup); |