multi.c 11 KB

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