multi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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
  7. * Author: Michal Nazarewicz (mina86@mina86.com)
  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.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/netdevice.h>
  17. #include "u_serial.h"
  18. #if defined USB_ETH_RNDIS
  19. # undef USB_ETH_RNDIS
  20. #endif
  21. #ifdef CONFIG_USB_G_MULTI_RNDIS
  22. # define USB_ETH_RNDIS y
  23. #endif
  24. #define DRIVER_DESC "Multifunction Composite Gadget"
  25. MODULE_DESCRIPTION(DRIVER_DESC);
  26. MODULE_AUTHOR("Michal Nazarewicz");
  27. MODULE_LICENSE("GPL");
  28. #include "f_mass_storage.h"
  29. #include "u_ecm.h"
  30. #ifdef USB_ETH_RNDIS
  31. # include "u_rndis.h"
  32. # include "rndis.h"
  33. #endif
  34. #include "u_ether.h"
  35. USB_GADGET_COMPOSITE_OPTIONS();
  36. USB_ETHERNET_MODULE_PARAMETERS();
  37. /***************************** Device Descriptor ****************************/
  38. #define MULTI_VENDOR_NUM 0x1d6b /* Linux Foundation */
  39. #define MULTI_PRODUCT_NUM 0x0104 /* Multifunction Composite Gadget */
  40. enum {
  41. __MULTI_NO_CONFIG,
  42. #ifdef CONFIG_USB_G_MULTI_RNDIS
  43. MULTI_RNDIS_CONFIG_NUM,
  44. #endif
  45. #ifdef CONFIG_USB_G_MULTI_CDC
  46. MULTI_CDC_CONFIG_NUM,
  47. #endif
  48. };
  49. static struct usb_device_descriptor device_desc = {
  50. .bLength = sizeof device_desc,
  51. .bDescriptorType = USB_DT_DEVICE,
  52. .bcdUSB = cpu_to_le16(0x0200),
  53. .bDeviceClass = USB_CLASS_MISC /* 0xEF */,
  54. .bDeviceSubClass = 2,
  55. .bDeviceProtocol = 1,
  56. /* Vendor and product id can be overridden by module parameters. */
  57. .idVendor = cpu_to_le16(MULTI_VENDOR_NUM),
  58. .idProduct = cpu_to_le16(MULTI_PRODUCT_NUM),
  59. };
  60. static const struct usb_descriptor_header *otg_desc[2];
  61. enum {
  62. MULTI_STRING_RNDIS_CONFIG_IDX = USB_GADGET_FIRST_AVAIL_IDX,
  63. MULTI_STRING_CDC_CONFIG_IDX,
  64. };
  65. static struct usb_string strings_dev[] = {
  66. [USB_GADGET_MANUFACTURER_IDX].s = "",
  67. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  68. [USB_GADGET_SERIAL_IDX].s = "",
  69. [MULTI_STRING_RNDIS_CONFIG_IDX].s = "Multifunction with RNDIS",
  70. [MULTI_STRING_CDC_CONFIG_IDX].s = "Multifunction with CDC ECM",
  71. { } /* end of list */
  72. };
  73. static struct usb_gadget_strings *dev_strings[] = {
  74. &(struct usb_gadget_strings){
  75. .language = 0x0409, /* en-us */
  76. .strings = strings_dev,
  77. },
  78. NULL,
  79. };
  80. /****************************** Configurations ******************************/
  81. static struct fsg_module_parameters fsg_mod_data = { .stall = 1 };
  82. #ifdef CONFIG_USB_GADGET_DEBUG_FILES
  83. static unsigned int fsg_num_buffers = CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS;
  84. #else
  85. /*
  86. * Number of buffers we will use.
  87. * 2 is usually enough for good buffering pipeline
  88. */
  89. #define fsg_num_buffers CONFIG_USB_GADGET_STORAGE_NUM_BUFFERS
  90. #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
  91. FSG_MODULE_PARAMETERS(/* no prefix */, fsg_mod_data);
  92. static struct usb_function_instance *fi_acm;
  93. static struct usb_function_instance *fi_msg;
  94. /********** RNDIS **********/
  95. #ifdef USB_ETH_RNDIS
  96. static struct usb_function_instance *fi_rndis;
  97. static struct usb_function *f_acm_rndis;
  98. static struct usb_function *f_rndis;
  99. static struct usb_function *f_msg_rndis;
  100. static int rndis_do_config(struct usb_configuration *c)
  101. {
  102. int ret;
  103. if (gadget_is_otg(c->cdev->gadget)) {
  104. c->descriptors = otg_desc;
  105. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  106. }
  107. f_rndis = usb_get_function(fi_rndis);
  108. if (IS_ERR(f_rndis))
  109. return PTR_ERR(f_rndis);
  110. ret = usb_add_function(c, f_rndis);
  111. if (ret < 0)
  112. goto err_func_rndis;
  113. f_acm_rndis = usb_get_function(fi_acm);
  114. if (IS_ERR(f_acm_rndis)) {
  115. ret = PTR_ERR(f_acm_rndis);
  116. goto err_func_acm;
  117. }
  118. ret = usb_add_function(c, f_acm_rndis);
  119. if (ret)
  120. goto err_conf;
  121. f_msg_rndis = usb_get_function(fi_msg);
  122. if (IS_ERR(f_msg_rndis)) {
  123. ret = PTR_ERR(f_msg_rndis);
  124. goto err_fsg;
  125. }
  126. ret = usb_add_function(c, f_msg_rndis);
  127. if (ret)
  128. goto err_run;
  129. return 0;
  130. err_run:
  131. usb_put_function(f_msg_rndis);
  132. err_fsg:
  133. usb_remove_function(c, f_acm_rndis);
  134. err_conf:
  135. usb_put_function(f_acm_rndis);
  136. err_func_acm:
  137. usb_remove_function(c, f_rndis);
  138. err_func_rndis:
  139. usb_put_function(f_rndis);
  140. return ret;
  141. }
  142. static __ref int rndis_config_register(struct usb_composite_dev *cdev)
  143. {
  144. static struct usb_configuration config = {
  145. .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
  146. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  147. };
  148. config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
  149. config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
  150. return usb_add_config(cdev, &config, rndis_do_config);
  151. }
  152. #else
  153. static __ref int rndis_config_register(struct usb_composite_dev *cdev)
  154. {
  155. return 0;
  156. }
  157. #endif
  158. /********** CDC ECM **********/
  159. #ifdef CONFIG_USB_G_MULTI_CDC
  160. static struct usb_function_instance *fi_ecm;
  161. static struct usb_function *f_acm_multi;
  162. static struct usb_function *f_ecm;
  163. static struct usb_function *f_msg_multi;
  164. static int cdc_do_config(struct usb_configuration *c)
  165. {
  166. int ret;
  167. if (gadget_is_otg(c->cdev->gadget)) {
  168. c->descriptors = otg_desc;
  169. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  170. }
  171. f_ecm = usb_get_function(fi_ecm);
  172. if (IS_ERR(f_ecm))
  173. return PTR_ERR(f_ecm);
  174. ret = usb_add_function(c, f_ecm);
  175. if (ret < 0)
  176. goto err_func_ecm;
  177. /* implicit port_num is zero */
  178. f_acm_multi = usb_get_function(fi_acm);
  179. if (IS_ERR(f_acm_multi)) {
  180. ret = PTR_ERR(f_acm_multi);
  181. goto err_func_acm;
  182. }
  183. ret = usb_add_function(c, f_acm_multi);
  184. if (ret)
  185. goto err_conf;
  186. f_msg_multi = usb_get_function(fi_msg);
  187. if (IS_ERR(f_msg_multi)) {
  188. ret = PTR_ERR(f_msg_multi);
  189. goto err_fsg;
  190. }
  191. ret = usb_add_function(c, f_msg_multi);
  192. if (ret)
  193. goto err_run;
  194. return 0;
  195. err_run:
  196. usb_put_function(f_msg_multi);
  197. err_fsg:
  198. usb_remove_function(c, f_acm_multi);
  199. err_conf:
  200. usb_put_function(f_acm_multi);
  201. err_func_acm:
  202. usb_remove_function(c, f_ecm);
  203. err_func_ecm:
  204. usb_put_function(f_ecm);
  205. return ret;
  206. }
  207. static __ref int cdc_config_register(struct usb_composite_dev *cdev)
  208. {
  209. static struct usb_configuration config = {
  210. .bConfigurationValue = MULTI_CDC_CONFIG_NUM,
  211. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  212. };
  213. config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
  214. config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
  215. return usb_add_config(cdev, &config, cdc_do_config);
  216. }
  217. #else
  218. static __ref int cdc_config_register(struct usb_composite_dev *cdev)
  219. {
  220. return 0;
  221. }
  222. #endif
  223. /****************************** Gadget Bind ******************************/
  224. static int __ref multi_bind(struct usb_composite_dev *cdev)
  225. {
  226. struct usb_gadget *gadget = cdev->gadget;
  227. #ifdef CONFIG_USB_G_MULTI_CDC
  228. struct f_ecm_opts *ecm_opts;
  229. #endif
  230. #ifdef USB_ETH_RNDIS
  231. struct f_rndis_opts *rndis_opts;
  232. #endif
  233. struct fsg_opts *fsg_opts;
  234. struct fsg_config config;
  235. int status;
  236. if (!can_support_ecm(cdev->gadget)) {
  237. dev_err(&gadget->dev, "controller '%s' not usable\n",
  238. gadget->name);
  239. return -EINVAL;
  240. }
  241. #ifdef CONFIG_USB_G_MULTI_CDC
  242. fi_ecm = usb_get_function_instance("ecm");
  243. if (IS_ERR(fi_ecm))
  244. return PTR_ERR(fi_ecm);
  245. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  246. gether_set_qmult(ecm_opts->net, qmult);
  247. if (!gether_set_host_addr(ecm_opts->net, host_addr))
  248. pr_info("using host ethernet address: %s", host_addr);
  249. if (!gether_set_dev_addr(ecm_opts->net, dev_addr))
  250. pr_info("using self ethernet address: %s", dev_addr);
  251. #endif
  252. #ifdef USB_ETH_RNDIS
  253. fi_rndis = usb_get_function_instance("rndis");
  254. if (IS_ERR(fi_rndis)) {
  255. status = PTR_ERR(fi_rndis);
  256. goto fail;
  257. }
  258. rndis_opts = container_of(fi_rndis, struct f_rndis_opts, func_inst);
  259. gether_set_qmult(rndis_opts->net, qmult);
  260. if (!gether_set_host_addr(rndis_opts->net, host_addr))
  261. pr_info("using host ethernet address: %s", host_addr);
  262. if (!gether_set_dev_addr(rndis_opts->net, dev_addr))
  263. pr_info("using self ethernet address: %s", dev_addr);
  264. #endif
  265. #if (defined CONFIG_USB_G_MULTI_CDC && defined USB_ETH_RNDIS)
  266. /*
  267. * If both ecm and rndis are selected then:
  268. * 1) rndis borrows the net interface from ecm
  269. * 2) since the interface is shared it must not be bound
  270. * twice - in ecm's _and_ rndis' binds, so do it here.
  271. */
  272. gether_set_gadget(ecm_opts->net, cdev->gadget);
  273. status = gether_register_netdev(ecm_opts->net);
  274. if (status)
  275. goto fail0;
  276. rndis_borrow_net(fi_rndis, ecm_opts->net);
  277. ecm_opts->bound = true;
  278. #endif
  279. /* set up serial link layer */
  280. fi_acm = usb_get_function_instance("acm");
  281. if (IS_ERR(fi_acm)) {
  282. status = PTR_ERR(fi_acm);
  283. goto fail0;
  284. }
  285. /* set up mass storage function */
  286. fi_msg = usb_get_function_instance("mass_storage");
  287. if (IS_ERR(fi_msg)) {
  288. status = PTR_ERR(fi_msg);
  289. goto fail1;
  290. }
  291. fsg_config_from_params(&config, &fsg_mod_data, fsg_num_buffers);
  292. fsg_opts = fsg_opts_from_func_inst(fi_msg);
  293. fsg_opts->no_configfs = true;
  294. status = fsg_common_set_num_buffers(fsg_opts->common, fsg_num_buffers);
  295. if (status)
  296. goto fail2;
  297. status = fsg_common_set_cdev(fsg_opts->common, cdev, config.can_stall);
  298. if (status)
  299. goto fail_set_cdev;
  300. fsg_common_set_sysfs(fsg_opts->common, true);
  301. status = fsg_common_create_luns(fsg_opts->common, &config);
  302. if (status)
  303. goto fail_set_cdev;
  304. fsg_common_set_inquiry_string(fsg_opts->common, config.vendor_name,
  305. config.product_name);
  306. /* allocate string IDs */
  307. status = usb_string_ids_tab(cdev, strings_dev);
  308. if (unlikely(status < 0))
  309. goto fail_string_ids;
  310. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  311. if (gadget_is_otg(gadget) && !otg_desc[0]) {
  312. struct usb_descriptor_header *usb_desc;
  313. usb_desc = usb_otg_descriptor_alloc(gadget);
  314. if (!usb_desc)
  315. goto fail_string_ids;
  316. usb_otg_descriptor_init(gadget, usb_desc);
  317. otg_desc[0] = usb_desc;
  318. otg_desc[1] = NULL;
  319. }
  320. /* register configurations */
  321. status = rndis_config_register(cdev);
  322. if (unlikely(status < 0))
  323. goto fail_otg_desc;
  324. status = cdc_config_register(cdev);
  325. if (unlikely(status < 0))
  326. goto fail_otg_desc;
  327. usb_composite_overwrite_options(cdev, &coverwrite);
  328. /* we're done */
  329. dev_info(&gadget->dev, DRIVER_DESC "\n");
  330. return 0;
  331. /* error recovery */
  332. fail_otg_desc:
  333. kfree(otg_desc[0]);
  334. otg_desc[0] = NULL;
  335. fail_string_ids:
  336. fsg_common_remove_luns(fsg_opts->common);
  337. fail_set_cdev:
  338. fsg_common_free_buffers(fsg_opts->common);
  339. fail2:
  340. usb_put_function_instance(fi_msg);
  341. fail1:
  342. usb_put_function_instance(fi_acm);
  343. fail0:
  344. #ifdef USB_ETH_RNDIS
  345. usb_put_function_instance(fi_rndis);
  346. fail:
  347. #endif
  348. #ifdef CONFIG_USB_G_MULTI_CDC
  349. usb_put_function_instance(fi_ecm);
  350. #endif
  351. return status;
  352. }
  353. static int multi_unbind(struct usb_composite_dev *cdev)
  354. {
  355. #ifdef CONFIG_USB_G_MULTI_CDC
  356. usb_put_function(f_msg_multi);
  357. #endif
  358. #ifdef USB_ETH_RNDIS
  359. usb_put_function(f_msg_rndis);
  360. #endif
  361. usb_put_function_instance(fi_msg);
  362. #ifdef CONFIG_USB_G_MULTI_CDC
  363. usb_put_function(f_acm_multi);
  364. #endif
  365. #ifdef USB_ETH_RNDIS
  366. usb_put_function(f_acm_rndis);
  367. #endif
  368. usb_put_function_instance(fi_acm);
  369. #ifdef USB_ETH_RNDIS
  370. usb_put_function(f_rndis);
  371. usb_put_function_instance(fi_rndis);
  372. #endif
  373. #ifdef CONFIG_USB_G_MULTI_CDC
  374. usb_put_function(f_ecm);
  375. usb_put_function_instance(fi_ecm);
  376. #endif
  377. kfree(otg_desc[0]);
  378. otg_desc[0] = NULL;
  379. return 0;
  380. }
  381. /****************************** Some noise ******************************/
  382. static struct usb_composite_driver multi_driver = {
  383. .name = "g_multi",
  384. .dev = &device_desc,
  385. .strings = dev_strings,
  386. .max_speed = USB_SPEED_HIGH,
  387. .bind = multi_bind,
  388. .unbind = multi_unbind,
  389. .needs_serial = 1,
  390. };
  391. module_usb_composite_driver(multi_driver);