multi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. struct fsg_opts *fsg_opts;
  103. int ret;
  104. if (gadget_is_otg(c->cdev->gadget)) {
  105. c->descriptors = otg_desc;
  106. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  107. }
  108. f_rndis = usb_get_function(fi_rndis);
  109. if (IS_ERR(f_rndis))
  110. return PTR_ERR(f_rndis);
  111. ret = usb_add_function(c, f_rndis);
  112. if (ret < 0)
  113. goto err_func_rndis;
  114. f_acm_rndis = usb_get_function(fi_acm);
  115. if (IS_ERR(f_acm_rndis)) {
  116. ret = PTR_ERR(f_acm_rndis);
  117. goto err_func_acm;
  118. }
  119. ret = usb_add_function(c, f_acm_rndis);
  120. if (ret)
  121. goto err_conf;
  122. f_msg_rndis = usb_get_function(fi_msg);
  123. if (IS_ERR(f_msg_rndis)) {
  124. ret = PTR_ERR(f_msg_rndis);
  125. goto err_fsg;
  126. }
  127. fsg_opts = fsg_opts_from_func_inst(fi_msg);
  128. ret = fsg_common_run_thread(fsg_opts->common);
  129. if (ret)
  130. goto err_run;
  131. ret = usb_add_function(c, f_msg_rndis);
  132. if (ret)
  133. goto err_run;
  134. return 0;
  135. err_run:
  136. usb_put_function(f_msg_rndis);
  137. err_fsg:
  138. usb_remove_function(c, f_acm_rndis);
  139. err_conf:
  140. usb_put_function(f_acm_rndis);
  141. err_func_acm:
  142. usb_remove_function(c, f_rndis);
  143. err_func_rndis:
  144. usb_put_function(f_rndis);
  145. return ret;
  146. }
  147. static __ref int rndis_config_register(struct usb_composite_dev *cdev)
  148. {
  149. static struct usb_configuration config = {
  150. .bConfigurationValue = MULTI_RNDIS_CONFIG_NUM,
  151. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  152. };
  153. config.label = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].s;
  154. config.iConfiguration = strings_dev[MULTI_STRING_RNDIS_CONFIG_IDX].id;
  155. return usb_add_config(cdev, &config, rndis_do_config);
  156. }
  157. #else
  158. static __ref int rndis_config_register(struct usb_composite_dev *cdev)
  159. {
  160. return 0;
  161. }
  162. #endif
  163. /********** CDC ECM **********/
  164. #ifdef CONFIG_USB_G_MULTI_CDC
  165. static struct usb_function_instance *fi_ecm;
  166. static struct usb_function *f_acm_multi;
  167. static struct usb_function *f_ecm;
  168. static struct usb_function *f_msg_multi;
  169. static int cdc_do_config(struct usb_configuration *c)
  170. {
  171. struct fsg_opts *fsg_opts;
  172. int ret;
  173. if (gadget_is_otg(c->cdev->gadget)) {
  174. c->descriptors = otg_desc;
  175. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  176. }
  177. f_ecm = usb_get_function(fi_ecm);
  178. if (IS_ERR(f_ecm))
  179. return PTR_ERR(f_ecm);
  180. ret = usb_add_function(c, f_ecm);
  181. if (ret < 0)
  182. goto err_func_ecm;
  183. /* implicit port_num is zero */
  184. f_acm_multi = usb_get_function(fi_acm);
  185. if (IS_ERR(f_acm_multi)) {
  186. ret = PTR_ERR(f_acm_multi);
  187. goto err_func_acm;
  188. }
  189. ret = usb_add_function(c, f_acm_multi);
  190. if (ret)
  191. goto err_conf;
  192. f_msg_multi = usb_get_function(fi_msg);
  193. if (IS_ERR(f_msg_multi)) {
  194. ret = PTR_ERR(f_msg_multi);
  195. goto err_fsg;
  196. }
  197. fsg_opts = fsg_opts_from_func_inst(fi_msg);
  198. ret = fsg_common_run_thread(fsg_opts->common);
  199. if (ret)
  200. goto err_run;
  201. ret = usb_add_function(c, f_msg_multi);
  202. if (ret)
  203. goto err_run;
  204. return 0;
  205. err_run:
  206. usb_put_function(f_msg_multi);
  207. err_fsg:
  208. usb_remove_function(c, f_acm_multi);
  209. err_conf:
  210. usb_put_function(f_acm_multi);
  211. err_func_acm:
  212. usb_remove_function(c, f_ecm);
  213. err_func_ecm:
  214. usb_put_function(f_ecm);
  215. return ret;
  216. }
  217. static __ref int cdc_config_register(struct usb_composite_dev *cdev)
  218. {
  219. static struct usb_configuration config = {
  220. .bConfigurationValue = MULTI_CDC_CONFIG_NUM,
  221. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  222. };
  223. config.label = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].s;
  224. config.iConfiguration = strings_dev[MULTI_STRING_CDC_CONFIG_IDX].id;
  225. return usb_add_config(cdev, &config, cdc_do_config);
  226. }
  227. #else
  228. static __ref int cdc_config_register(struct usb_composite_dev *cdev)
  229. {
  230. return 0;
  231. }
  232. #endif
  233. /****************************** Gadget Bind ******************************/
  234. static int __ref multi_bind(struct usb_composite_dev *cdev)
  235. {
  236. struct usb_gadget *gadget = cdev->gadget;
  237. #ifdef CONFIG_USB_G_MULTI_CDC
  238. struct f_ecm_opts *ecm_opts;
  239. #endif
  240. #ifdef USB_ETH_RNDIS
  241. struct f_rndis_opts *rndis_opts;
  242. #endif
  243. struct fsg_opts *fsg_opts;
  244. struct fsg_config config;
  245. int status;
  246. if (!can_support_ecm(cdev->gadget)) {
  247. dev_err(&gadget->dev, "controller '%s' not usable\n",
  248. gadget->name);
  249. return -EINVAL;
  250. }
  251. #ifdef CONFIG_USB_G_MULTI_CDC
  252. fi_ecm = usb_get_function_instance("ecm");
  253. if (IS_ERR(fi_ecm))
  254. return PTR_ERR(fi_ecm);
  255. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  256. gether_set_qmult(ecm_opts->net, qmult);
  257. if (!gether_set_host_addr(ecm_opts->net, host_addr))
  258. pr_info("using host ethernet address: %s", host_addr);
  259. if (!gether_set_dev_addr(ecm_opts->net, dev_addr))
  260. pr_info("using self ethernet address: %s", dev_addr);
  261. #endif
  262. #ifdef USB_ETH_RNDIS
  263. fi_rndis = usb_get_function_instance("rndis");
  264. if (IS_ERR(fi_rndis)) {
  265. status = PTR_ERR(fi_rndis);
  266. goto fail;
  267. }
  268. rndis_opts = container_of(fi_rndis, struct f_rndis_opts, func_inst);
  269. gether_set_qmult(rndis_opts->net, qmult);
  270. if (!gether_set_host_addr(rndis_opts->net, host_addr))
  271. pr_info("using host ethernet address: %s", host_addr);
  272. if (!gether_set_dev_addr(rndis_opts->net, dev_addr))
  273. pr_info("using self ethernet address: %s", dev_addr);
  274. #endif
  275. #if (defined CONFIG_USB_G_MULTI_CDC && defined USB_ETH_RNDIS)
  276. /*
  277. * If both ecm and rndis are selected then:
  278. * 1) rndis borrows the net interface from ecm
  279. * 2) since the interface is shared it must not be bound
  280. * twice - in ecm's _and_ rndis' binds, so do it here.
  281. */
  282. gether_set_gadget(ecm_opts->net, cdev->gadget);
  283. status = gether_register_netdev(ecm_opts->net);
  284. if (status)
  285. goto fail0;
  286. rndis_borrow_net(fi_rndis, ecm_opts->net);
  287. ecm_opts->bound = true;
  288. #endif
  289. /* set up serial link layer */
  290. fi_acm = usb_get_function_instance("acm");
  291. if (IS_ERR(fi_acm)) {
  292. status = PTR_ERR(fi_acm);
  293. goto fail0;
  294. }
  295. /* set up mass storage function */
  296. fi_msg = usb_get_function_instance("mass_storage");
  297. if (IS_ERR(fi_msg)) {
  298. status = PTR_ERR(fi_msg);
  299. goto fail1;
  300. }
  301. fsg_config_from_params(&config, &fsg_mod_data, fsg_num_buffers);
  302. fsg_opts = fsg_opts_from_func_inst(fi_msg);
  303. fsg_opts->no_configfs = true;
  304. status = fsg_common_set_num_buffers(fsg_opts->common, fsg_num_buffers);
  305. if (status)
  306. goto fail2;
  307. status = fsg_common_set_cdev(fsg_opts->common, cdev, config.can_stall);
  308. if (status)
  309. goto fail_set_cdev;
  310. fsg_common_set_sysfs(fsg_opts->common, true);
  311. status = fsg_common_create_luns(fsg_opts->common, &config);
  312. if (status)
  313. goto fail_set_cdev;
  314. fsg_common_set_inquiry_string(fsg_opts->common, config.vendor_name,
  315. config.product_name);
  316. /* allocate string IDs */
  317. status = usb_string_ids_tab(cdev, strings_dev);
  318. if (unlikely(status < 0))
  319. goto fail_string_ids;
  320. device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
  321. if (gadget_is_otg(gadget) && !otg_desc[0]) {
  322. struct usb_descriptor_header *usb_desc;
  323. usb_desc = usb_otg_descriptor_alloc(gadget);
  324. if (!usb_desc)
  325. goto fail_string_ids;
  326. usb_otg_descriptor_init(gadget, usb_desc);
  327. otg_desc[0] = usb_desc;
  328. otg_desc[1] = NULL;
  329. }
  330. /* register configurations */
  331. status = rndis_config_register(cdev);
  332. if (unlikely(status < 0))
  333. goto fail_otg_desc;
  334. status = cdc_config_register(cdev);
  335. if (unlikely(status < 0))
  336. goto fail_otg_desc;
  337. usb_composite_overwrite_options(cdev, &coverwrite);
  338. /* we're done */
  339. dev_info(&gadget->dev, DRIVER_DESC "\n");
  340. return 0;
  341. /* error recovery */
  342. fail_otg_desc:
  343. kfree(otg_desc[0]);
  344. otg_desc[0] = NULL;
  345. fail_string_ids:
  346. fsg_common_remove_luns(fsg_opts->common);
  347. fail_set_cdev:
  348. fsg_common_free_buffers(fsg_opts->common);
  349. fail2:
  350. usb_put_function_instance(fi_msg);
  351. fail1:
  352. usb_put_function_instance(fi_acm);
  353. fail0:
  354. #ifdef USB_ETH_RNDIS
  355. usb_put_function_instance(fi_rndis);
  356. fail:
  357. #endif
  358. #ifdef CONFIG_USB_G_MULTI_CDC
  359. usb_put_function_instance(fi_ecm);
  360. #endif
  361. return status;
  362. }
  363. static int multi_unbind(struct usb_composite_dev *cdev)
  364. {
  365. #ifdef CONFIG_USB_G_MULTI_CDC
  366. usb_put_function(f_msg_multi);
  367. #endif
  368. #ifdef USB_ETH_RNDIS
  369. usb_put_function(f_msg_rndis);
  370. #endif
  371. usb_put_function_instance(fi_msg);
  372. #ifdef CONFIG_USB_G_MULTI_CDC
  373. usb_put_function(f_acm_multi);
  374. #endif
  375. #ifdef USB_ETH_RNDIS
  376. usb_put_function(f_acm_rndis);
  377. #endif
  378. usb_put_function_instance(fi_acm);
  379. #ifdef USB_ETH_RNDIS
  380. usb_put_function(f_rndis);
  381. usb_put_function_instance(fi_rndis);
  382. #endif
  383. #ifdef CONFIG_USB_G_MULTI_CDC
  384. usb_put_function(f_ecm);
  385. usb_put_function_instance(fi_ecm);
  386. #endif
  387. kfree(otg_desc[0]);
  388. otg_desc[0] = NULL;
  389. return 0;
  390. }
  391. /****************************** Some noise ******************************/
  392. static struct usb_composite_driver multi_driver = {
  393. .name = "g_multi",
  394. .dev = &device_desc,
  395. .strings = dev_strings,
  396. .max_speed = USB_SPEED_HIGH,
  397. .bind = multi_bind,
  398. .unbind = multi_unbind,
  399. .needs_serial = 1,
  400. };
  401. module_usb_composite_driver(multi_driver);