multi.c 12 KB

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