Commit | Line | Data |
---|---|---|
f176a5d8 MN |
1 | /* |
2 | * multi.c -- Multifunction Composite driver | |
3 | * | |
4 | * Copyright (C) 2008 David Brownell | |
5 | * Copyright (C) 2008 Nokia Corporation | |
6 | * Copyright (C) 2009 Samsung Electronics | |
54b8360f | 7 | * Author: Michal Nazarewicz (mina86@mina86.com) |
f176a5d8 MN |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
f176a5d8 MN |
13 | */ |
14 | ||
15 | ||
16 | #include <linux/kernel.h> | |
279cc49a | 17 | #include <linux/module.h> |
f176a5d8 | 18 | |
59835ad7 | 19 | #include "u_serial.h" |
396cda90 MN |
20 | #if defined USB_ETH_RNDIS |
21 | # undef USB_ETH_RNDIS | |
22 | #endif | |
dbe4a99d | 23 | #ifdef CONFIG_USB_G_MULTI_RNDIS |
396cda90 | 24 | # define USB_ETH_RNDIS y |
f176a5d8 MN |
25 | #endif |
26 | ||
27 | ||
28 | #define DRIVER_DESC "Multifunction Composite Gadget" | |
f176a5d8 | 29 | |
279cc49a MN |
30 | MODULE_DESCRIPTION(DRIVER_DESC); |
31 | MODULE_AUTHOR("Michal Nazarewicz"); | |
32 | MODULE_LICENSE("GPL"); | |
f176a5d8 | 33 | |
f176a5d8 | 34 | |
279cc49a | 35 | /***************************** All the files... *****************************/ |
f176a5d8 MN |
36 | |
37 | /* | |
38 | * kbuild is not very cooperative with respect to linking separately | |
39 | * compiled library objects into one module. So for now we won't use | |
40 | * separate compilation ... ensuring init/exit sections work to shrink | |
41 | * the runtime footprint, and giving us at least some parts of what | |
42 | * a "gcc --combine ... part1.c part2.c part3.c ... " build would. | |
43 | */ | |
e5eaa0dc | 44 | #define USB_FMS_INCLUDED |
279cc49a | 45 | #include "f_mass_storage.c" |
f176a5d8 | 46 | |
fee562a6 | 47 | #define USBF_ECM_INCLUDED |
f176a5d8 | 48 | #include "f_ecm.c" |
396cda90 | 49 | #ifdef USB_ETH_RNDIS |
f466c635 | 50 | # define USB_FRNDIS_INCLUDED |
f176a5d8 | 51 | # include "f_rndis.c" |
cbbd14a9 | 52 | # include "rndis.h" |
f176a5d8 | 53 | #endif |
f1a1823f | 54 | #include "u_ether.h" |
f176a5d8 | 55 | |
7d16e8d3 | 56 | USB_GADGET_COMPOSITE_OPTIONS(); |
279cc49a | 57 | |
f1a1823f AP |
58 | USB_ETHERNET_MODULE_PARAMETERS(); |
59 | ||
279cc49a MN |
60 | /***************************** Device Descriptor ****************************/ |
61 | ||
1c6529e9 MN |
62 | #define MULTI_VENDOR_NUM 0x1d6b /* Linux Foundation */ |
63 | #define MULTI_PRODUCT_NUM 0x0104 /* Multifunction Composite Gadget */ | |
279cc49a MN |
64 | |
65 | ||
66 | enum { | |
67 | __MULTI_NO_CONFIG, | |
68 | #ifdef CONFIG_USB_G_MULTI_RNDIS | |
69 | MULTI_RNDIS_CONFIG_NUM, | |
70 | #endif | |
71 | #ifdef CONFIG_USB_G_MULTI_CDC | |
72 | MULTI_CDC_CONFIG_NUM, | |
73 | #endif | |
74 | }; | |
75 | ||
f176a5d8 MN |
76 | |
77 | static struct usb_device_descriptor device_desc = { | |
78 | .bLength = sizeof device_desc, | |
79 | .bDescriptorType = USB_DT_DEVICE, | |
80 | ||
81 | .bcdUSB = cpu_to_le16(0x0200), | |
82 | ||
279cc49a | 83 | .bDeviceClass = USB_CLASS_MISC /* 0xEF */, |
f176a5d8 MN |
84 | .bDeviceSubClass = 2, |
85 | .bDeviceProtocol = 1, | |
f176a5d8 MN |
86 | |
87 | /* Vendor and product id can be overridden by module parameters. */ | |
88 | .idVendor = cpu_to_le16(MULTI_VENDOR_NUM), | |
89 | .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM), | |
f176a5d8 MN |
90 | }; |
91 | ||
f176a5d8 MN |
92 | |
93 | static const struct usb_descriptor_header *otg_desc[] = { | |
279cc49a MN |
94 | (struct usb_descriptor_header *) &(struct usb_otg_descriptor){ |
95 | .bLength = sizeof(struct usb_otg_descriptor), | |
96 | .bDescriptorType = USB_DT_OTG, | |
97 | ||
98 | /* | |
99 | * REVISIT SRP-only hardware is possible, although | |
100 | * it would not be called "OTG" ... | |
101 | */ | |
102 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, | |
103 | }, | |
f176a5d8 MN |
104 | NULL, |
105 | }; | |
106 | ||
107 | ||
279cc49a | 108 | enum { |
276e2e4f | 109 | MULTI_STRING_RNDIS_CONFIG_IDX = USB_GADGET_FIRST_AVAIL_IDX, |
279cc49a | 110 | MULTI_STRING_CDC_CONFIG_IDX, |
279cc49a | 111 | }; |
f176a5d8 | 112 | |
f176a5d8 | 113 | static struct usb_string strings_dev[] = { |
276e2e4f | 114 | [USB_GADGET_MANUFACTURER_IDX].s = "", |
d33f74fc | 115 | [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC, |
276e2e4f | 116 | [USB_GADGET_SERIAL_IDX].s = "", |
279cc49a | 117 | [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS", |
279cc49a | 118 | [MULTI_STRING_CDC_CONFIG_IDX].s = "Multifunction with CDC ECM", |
f176a5d8 MN |
119 | { } /* end of list */ |
120 | }; | |
121 | ||
f176a5d8 | 122 | static struct usb_gadget_strings *dev_strings[] = { |
279cc49a MN |
123 | &(struct usb_gadget_strings){ |
124 | .language = 0x0409, /* en-us */ | |
125 | .strings = strings_dev, | |
126 | }, | |
f176a5d8 MN |
127 | NULL, |
128 | }; | |
129 | ||
f176a5d8 MN |
130 | |
131 | ||
132 | ||
133 | /****************************** Configurations ******************************/ | |
134 | ||
279cc49a | 135 | static struct fsg_module_parameters fsg_mod_data = { .stall = 1 }; |
6fdc5dd2 AP |
136 | #ifdef CONFIG_USB_GADGET_DEBUG_FILES |
137 | ||
138 | static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS; | |
139 | ||
140 | #else | |
141 | ||
142 | /* | |
143 | * Number of buffers we will use. | |
144 | * 2 is usually enough for good buffering pipeline | |
145 | */ | |
146 | #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS | |
147 | ||
148 | #endif /* CONFIG_USB_DEBUG */ | |
149 | ||
279cc49a MN |
150 | FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data); |
151 | ||
152 | static struct fsg_common fsg_common; | |
153 | ||
f1a1823f | 154 | static u8 host_mac[ETH_ALEN]; |
f176a5d8 | 155 | |
59835ad7 | 156 | static struct usb_function_instance *fi_acm; |
d6a01439 | 157 | static struct eth_dev *the_dev; |
f176a5d8 | 158 | |
279cc49a | 159 | /********** RNDIS **********/ |
f176a5d8 | 160 | |
396cda90 | 161 | #ifdef USB_ETH_RNDIS |
59835ad7 | 162 | static struct usb_function *f_acm_rndis; |
f176a5d8 | 163 | |
c9bfff9c | 164 | static __init int rndis_do_config(struct usb_configuration *c) |
f176a5d8 MN |
165 | { |
166 | int ret; | |
167 | ||
168 | if (gadget_is_otg(c->cdev->gadget)) { | |
169 | c->descriptors = otg_desc; | |
170 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
171 | } | |
172 | ||
f1a1823f | 173 | ret = rndis_bind_config(c, host_mac, the_dev); |
f176a5d8 MN |
174 | if (ret < 0) |
175 | return ret; | |
176 | ||
59835ad7 | 177 | f_acm_rndis = usb_get_function(fi_acm); |
8abc6fa1 AP |
178 | if (IS_ERR(f_acm_rndis)) |
179 | return PTR_ERR(f_acm_rndis); | |
59835ad7 SAS |
180 | |
181 | ret = usb_add_function(c, f_acm_rndis); | |
182 | if (ret) | |
183 | goto err_conf; | |
f176a5d8 | 184 | |
279cc49a | 185 | ret = fsg_bind_config(c->cdev, c, &fsg_common); |
f176a5d8 | 186 | if (ret < 0) |
59835ad7 | 187 | goto err_fsg; |
f176a5d8 MN |
188 | |
189 | return 0; | |
59835ad7 SAS |
190 | err_fsg: |
191 | usb_remove_function(c, f_acm_rndis); | |
192 | err_conf: | |
193 | usb_put_function(f_acm_rndis); | |
59835ad7 | 194 | return ret; |
f176a5d8 MN |
195 | } |
196 | ||
780cc0f3 | 197 | static __ref int rndis_config_register(struct usb_composite_dev *cdev) |
279cc49a MN |
198 | { |
199 | static struct usb_configuration config = { | |
279cc49a MN |
200 | .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM, |
201 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
202 | }; | |
203 | ||
204 | config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s; | |
205 | config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id; | |
206 | ||
c9bfff9c | 207 | return usb_add_config(cdev, &config, rndis_do_config); |
279cc49a MN |
208 | } |
209 | ||
210 | #else | |
211 | ||
780cc0f3 | 212 | static __ref int rndis_config_register(struct usb_composite_dev *cdev) |
279cc49a MN |
213 | { |
214 | return 0; | |
215 | } | |
f176a5d8 MN |
216 | |
217 | #endif | |
218 | ||
279cc49a MN |
219 | |
220 | /********** CDC ECM **********/ | |
221 | ||
f176a5d8 | 222 | #ifdef CONFIG_USB_G_MULTI_CDC |
59835ad7 | 223 | static struct usb_function *f_acm_multi; |
f176a5d8 | 224 | |
c9bfff9c | 225 | static __init int cdc_do_config(struct usb_configuration *c) |
f176a5d8 MN |
226 | { |
227 | int ret; | |
228 | ||
229 | if (gadget_is_otg(c->cdev->gadget)) { | |
230 | c->descriptors = otg_desc; | |
231 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
232 | } | |
233 | ||
f1a1823f | 234 | ret = ecm_bind_config(c, host_mac, the_dev); |
f176a5d8 MN |
235 | if (ret < 0) |
236 | return ret; | |
237 | ||
59835ad7 SAS |
238 | /* implicit port_num is zero */ |
239 | f_acm_multi = usb_get_function(fi_acm); | |
240 | if (IS_ERR(f_acm_multi)) | |
8abc6fa1 | 241 | return PTR_ERR(f_acm_multi); |
59835ad7 SAS |
242 | |
243 | ret = usb_add_function(c, f_acm_multi); | |
244 | if (ret) | |
245 | goto err_conf; | |
f176a5d8 | 246 | |
279cc49a | 247 | ret = fsg_bind_config(c->cdev, c, &fsg_common); |
f176a5d8 | 248 | if (ret < 0) |
59835ad7 | 249 | goto err_fsg; |
f176a5d8 MN |
250 | |
251 | return 0; | |
59835ad7 SAS |
252 | err_fsg: |
253 | usb_remove_function(c, f_acm_multi); | |
254 | err_conf: | |
255 | usb_put_function(f_acm_multi); | |
59835ad7 | 256 | return ret; |
f176a5d8 MN |
257 | } |
258 | ||
780cc0f3 | 259 | static __ref int cdc_config_register(struct usb_composite_dev *cdev) |
279cc49a MN |
260 | { |
261 | static struct usb_configuration config = { | |
279cc49a MN |
262 | .bConfigurationValue = MULTI_CDC_CONFIG_NUM, |
263 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
264 | }; | |
265 | ||
266 | config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s; | |
267 | config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id; | |
268 | ||
c9bfff9c | 269 | return usb_add_config(cdev, &config, cdc_do_config); |
279cc49a MN |
270 | } |
271 | ||
272 | #else | |
273 | ||
780cc0f3 | 274 | static __ref int cdc_config_register(struct usb_composite_dev *cdev) |
279cc49a MN |
275 | { |
276 | return 0; | |
277 | } | |
f176a5d8 MN |
278 | |
279 | #endif | |
280 | ||
281 | ||
282 | ||
283 | /****************************** Gadget Bind ******************************/ | |
284 | ||
279cc49a | 285 | static int __ref multi_bind(struct usb_composite_dev *cdev) |
f176a5d8 MN |
286 | { |
287 | struct usb_gadget *gadget = cdev->gadget; | |
ed9cbda6 | 288 | int status; |
f176a5d8 MN |
289 | |
290 | if (!can_support_ecm(cdev->gadget)) { | |
291 | dev_err(&gadget->dev, "controller '%s' not usable\n", | |
292 | gadget->name); | |
293 | return -EINVAL; | |
294 | } | |
295 | ||
296 | /* set up network link layer */ | |
f1a1823f AP |
297 | the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, host_mac, |
298 | qmult); | |
d6a01439 SAS |
299 | if (IS_ERR(the_dev)) |
300 | return PTR_ERR(the_dev); | |
f176a5d8 MN |
301 | |
302 | /* set up serial link layer */ | |
59835ad7 SAS |
303 | fi_acm = usb_get_function_instance("acm"); |
304 | if (IS_ERR(fi_acm)) { | |
305 | status = PTR_ERR(fi_acm); | |
c4ed4ac1 | 306 | goto fail0; |
59835ad7 SAS |
307 | } |
308 | ||
f176a5d8 | 309 | /* set up mass storage function */ |
279cc49a MN |
310 | { |
311 | void *retp; | |
6fdc5dd2 AP |
312 | retp = fsg_common_from_params(&fsg_common, cdev, &fsg_mod_data, |
313 | fsg_num_buffers); | |
279cc49a MN |
314 | if (IS_ERR(retp)) { |
315 | status = PTR_ERR(retp); | |
316 | goto fail1; | |
317 | } | |
f176a5d8 MN |
318 | } |
319 | ||
a99d8a45 | 320 | /* allocate string IDs */ |
279cc49a MN |
321 | status = usb_string_ids_tab(cdev, strings_dev); |
322 | if (unlikely(status < 0)) | |
f176a5d8 | 323 | goto fail2; |
d33f74fc | 324 | device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id; |
f176a5d8 | 325 | |
279cc49a MN |
326 | /* register configurations */ |
327 | status = rndis_config_register(cdev); | |
328 | if (unlikely(status < 0)) | |
f176a5d8 | 329 | goto fail2; |
f176a5d8 | 330 | |
279cc49a MN |
331 | status = cdc_config_register(cdev); |
332 | if (unlikely(status < 0)) | |
f176a5d8 | 333 | goto fail2; |
7d16e8d3 | 334 | usb_composite_overwrite_options(cdev, &coverwrite); |
f176a5d8 | 335 | |
279cc49a MN |
336 | /* we're done */ |
337 | dev_info(&gadget->dev, DRIVER_DESC "\n"); | |
338 | fsg_common_put(&fsg_common); | |
f176a5d8 MN |
339 | return 0; |
340 | ||
279cc49a MN |
341 | |
342 | /* error recovery */ | |
f176a5d8 | 343 | fail2: |
279cc49a | 344 | fsg_common_put(&fsg_common); |
f176a5d8 | 345 | fail1: |
59835ad7 | 346 | usb_put_function_instance(fi_acm); |
f176a5d8 | 347 | fail0: |
d6a01439 | 348 | gether_cleanup(the_dev); |
f176a5d8 MN |
349 | return status; |
350 | } | |
351 | ||
352 | static int __exit multi_unbind(struct usb_composite_dev *cdev) | |
353 | { | |
59835ad7 SAS |
354 | #ifdef CONFIG_USB_G_MULTI_CDC |
355 | usb_put_function(f_acm_multi); | |
356 | #endif | |
357 | #ifdef USB_ETH_RNDIS | |
358 | usb_put_function(f_acm_rndis); | |
359 | #endif | |
360 | usb_put_function_instance(fi_acm); | |
d6a01439 | 361 | gether_cleanup(the_dev); |
f176a5d8 MN |
362 | return 0; |
363 | } | |
364 | ||
365 | ||
366 | /****************************** Some noise ******************************/ | |
367 | ||
368 | ||
c2ec75c2 | 369 | static __refdata struct usb_composite_driver multi_driver = { |
f176a5d8 MN |
370 | .name = "g_multi", |
371 | .dev = &device_desc, | |
372 | .strings = dev_strings, | |
35a0e0bf | 373 | .max_speed = USB_SPEED_HIGH, |
03e42bd5 | 374 | .bind = multi_bind, |
f176a5d8 | 375 | .unbind = __exit_p(multi_unbind), |
a99d8a45 | 376 | .needs_serial = 1, |
f176a5d8 MN |
377 | }; |
378 | ||
f176a5d8 | 379 | |
279cc49a | 380 | static int __init multi_init(void) |
f176a5d8 | 381 | { |
03e42bd5 | 382 | return usb_composite_probe(&multi_driver); |
f176a5d8 | 383 | } |
279cc49a | 384 | module_init(multi_init); |
f176a5d8 | 385 | |
279cc49a | 386 | static void __exit multi_exit(void) |
f176a5d8 MN |
387 | { |
388 | usb_composite_unregister(&multi_driver); | |
389 | } | |
279cc49a | 390 | module_exit(multi_exit); |