g_ffs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * g_ffs.c -- user mode file system API for USB composite function controllers
  4. *
  5. * Copyright (C) 2010 Samsung Electronics
  6. * Author: Michal Nazarewicz <mina86@mina86.com>
  7. */
  8. #define pr_fmt(fmt) "g_ffs: " fmt
  9. #include <linux/module.h>
  10. #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
  11. #include <linux/netdevice.h>
  12. # if defined USB_ETH_RNDIS
  13. # undef USB_ETH_RNDIS
  14. # endif
  15. # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  16. # define USB_ETH_RNDIS y
  17. # endif
  18. # include "u_ecm.h"
  19. # include "u_gether.h"
  20. # ifdef USB_ETH_RNDIS
  21. # include "u_rndis.h"
  22. # include "rndis.h"
  23. # endif
  24. # include "u_ether.h"
  25. USB_ETHERNET_MODULE_PARAMETERS();
  26. # ifdef CONFIG_USB_FUNCTIONFS_ETH
  27. static int eth_bind_config(struct usb_configuration *c);
  28. static struct usb_function_instance *fi_ecm;
  29. static struct usb_function *f_ecm;
  30. static struct usb_function_instance *fi_geth;
  31. static struct usb_function *f_geth;
  32. # endif
  33. # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  34. static int bind_rndis_config(struct usb_configuration *c);
  35. static struct usb_function_instance *fi_rndis;
  36. static struct usb_function *f_rndis;
  37. # endif
  38. #endif
  39. #include "u_fs.h"
  40. #define DRIVER_NAME "g_ffs"
  41. #define DRIVER_DESC "USB Function Filesystem"
  42. #define DRIVER_VERSION "24 Aug 2004"
  43. MODULE_DESCRIPTION(DRIVER_DESC);
  44. MODULE_AUTHOR("Michal Nazarewicz");
  45. MODULE_LICENSE("GPL");
  46. #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
  47. #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
  48. #define GFS_MAX_DEVS 10
  49. USB_GADGET_COMPOSITE_OPTIONS();
  50. static struct usb_device_descriptor gfs_dev_desc = {
  51. .bLength = sizeof gfs_dev_desc,
  52. .bDescriptorType = USB_DT_DEVICE,
  53. /* .bcdUSB = DYNAMIC */
  54. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  55. .idVendor = cpu_to_le16(GFS_VENDOR_ID),
  56. .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
  57. };
  58. static char *func_names[GFS_MAX_DEVS];
  59. static unsigned int func_num;
  60. module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
  61. MODULE_PARM_DESC(bDeviceClass, "USB Device class");
  62. module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
  63. MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
  64. module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
  65. MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
  66. module_param_array_named(functions, func_names, charp, &func_num, 0);
  67. MODULE_PARM_DESC(functions, "USB Functions list");
  68. static const struct usb_descriptor_header *gfs_otg_desc[2];
  69. /* String IDs are assigned dynamically */
  70. static struct usb_string gfs_strings[] = {
  71. [USB_GADGET_MANUFACTURER_IDX].s = "",
  72. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  73. [USB_GADGET_SERIAL_IDX].s = "",
  74. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  75. { .s = "FunctionFS + RNDIS" },
  76. #endif
  77. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  78. { .s = "FunctionFS + ECM" },
  79. #endif
  80. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  81. { .s = "FunctionFS" },
  82. #endif
  83. { } /* end of list */
  84. };
  85. static struct usb_gadget_strings *gfs_dev_strings[] = {
  86. &(struct usb_gadget_strings) {
  87. .language = 0x0409, /* en-us */
  88. .strings = gfs_strings,
  89. },
  90. NULL,
  91. };
  92. struct gfs_configuration {
  93. struct usb_configuration c;
  94. int (*eth)(struct usb_configuration *c);
  95. int num;
  96. };
  97. static struct gfs_configuration gfs_configurations[] = {
  98. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  99. {
  100. .eth = bind_rndis_config,
  101. },
  102. #endif
  103. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  104. {
  105. .eth = eth_bind_config,
  106. },
  107. #endif
  108. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  109. {
  110. },
  111. #endif
  112. };
  113. static void *functionfs_acquire_dev(struct ffs_dev *dev);
  114. static void functionfs_release_dev(struct ffs_dev *dev);
  115. static int functionfs_ready_callback(struct ffs_data *ffs);
  116. static void functionfs_closed_callback(struct ffs_data *ffs);
  117. static int gfs_bind(struct usb_composite_dev *cdev);
  118. static int gfs_unbind(struct usb_composite_dev *cdev);
  119. static int gfs_do_config(struct usb_configuration *c);
  120. static struct usb_composite_driver gfs_driver = {
  121. .name = DRIVER_NAME,
  122. .dev = &gfs_dev_desc,
  123. .strings = gfs_dev_strings,
  124. .max_speed = USB_SPEED_SUPER,
  125. .bind = gfs_bind,
  126. .unbind = gfs_unbind,
  127. };
  128. static unsigned int missing_funcs;
  129. static bool gfs_registered;
  130. static bool gfs_single_func;
  131. static struct usb_function_instance **fi_ffs;
  132. static struct usb_function **f_ffs[] = {
  133. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  134. NULL,
  135. #endif
  136. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  137. NULL,
  138. #endif
  139. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  140. NULL,
  141. #endif
  142. };
  143. #define N_CONF ARRAY_SIZE(f_ffs)
  144. static int __init gfs_init(void)
  145. {
  146. struct f_fs_opts *opts;
  147. int i;
  148. int ret = 0;
  149. ENTER();
  150. if (func_num < 2) {
  151. gfs_single_func = true;
  152. func_num = 1;
  153. }
  154. /*
  155. * Allocate in one chunk for easier maintenance
  156. */
  157. f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL);
  158. if (!f_ffs[0]) {
  159. ret = -ENOMEM;
  160. goto no_func;
  161. }
  162. for (i = 1; i < N_CONF; ++i)
  163. f_ffs[i] = f_ffs[0] + i * func_num;
  164. fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL);
  165. if (!fi_ffs) {
  166. ret = -ENOMEM;
  167. goto no_func;
  168. }
  169. for (i = 0; i < func_num; i++) {
  170. fi_ffs[i] = usb_get_function_instance("ffs");
  171. if (IS_ERR(fi_ffs[i])) {
  172. ret = PTR_ERR(fi_ffs[i]);
  173. --i;
  174. goto no_dev;
  175. }
  176. opts = to_f_fs_opts(fi_ffs[i]);
  177. if (gfs_single_func)
  178. ret = ffs_single_dev(opts->dev);
  179. else
  180. ret = ffs_name_dev(opts->dev, func_names[i]);
  181. if (ret)
  182. goto no_dev;
  183. opts->dev->ffs_ready_callback = functionfs_ready_callback;
  184. opts->dev->ffs_closed_callback = functionfs_closed_callback;
  185. opts->dev->ffs_acquire_dev_callback = functionfs_acquire_dev;
  186. opts->dev->ffs_release_dev_callback = functionfs_release_dev;
  187. opts->no_configfs = true;
  188. }
  189. missing_funcs = func_num;
  190. return 0;
  191. no_dev:
  192. while (i >= 0)
  193. usb_put_function_instance(fi_ffs[i--]);
  194. kfree(fi_ffs);
  195. no_func:
  196. kfree(f_ffs[0]);
  197. return ret;
  198. }
  199. module_init(gfs_init);
  200. static void __exit gfs_exit(void)
  201. {
  202. int i;
  203. ENTER();
  204. if (gfs_registered)
  205. usb_composite_unregister(&gfs_driver);
  206. gfs_registered = false;
  207. kfree(f_ffs[0]);
  208. for (i = 0; i < func_num; i++)
  209. usb_put_function_instance(fi_ffs[i]);
  210. kfree(fi_ffs);
  211. }
  212. module_exit(gfs_exit);
  213. static void *functionfs_acquire_dev(struct ffs_dev *dev)
  214. {
  215. if (!try_module_get(THIS_MODULE))
  216. return ERR_PTR(-ENOENT);
  217. return NULL;
  218. }
  219. static void functionfs_release_dev(struct ffs_dev *dev)
  220. {
  221. module_put(THIS_MODULE);
  222. }
  223. /*
  224. * The caller of this function takes ffs_lock
  225. */
  226. static int functionfs_ready_callback(struct ffs_data *ffs)
  227. {
  228. int ret = 0;
  229. if (--missing_funcs)
  230. return 0;
  231. if (gfs_registered)
  232. return -EBUSY;
  233. gfs_registered = true;
  234. ret = usb_composite_probe(&gfs_driver);
  235. if (unlikely(ret < 0)) {
  236. ++missing_funcs;
  237. gfs_registered = false;
  238. }
  239. return ret;
  240. }
  241. /*
  242. * The caller of this function takes ffs_lock
  243. */
  244. static void functionfs_closed_callback(struct ffs_data *ffs)
  245. {
  246. missing_funcs++;
  247. if (gfs_registered)
  248. usb_composite_unregister(&gfs_driver);
  249. gfs_registered = false;
  250. }
  251. /*
  252. * It is assumed that gfs_bind is called from a context where ffs_lock is held
  253. */
  254. static int gfs_bind(struct usb_composite_dev *cdev)
  255. {
  256. #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
  257. struct net_device *net;
  258. #endif
  259. int ret, i;
  260. ENTER();
  261. if (missing_funcs)
  262. return -ENODEV;
  263. #if defined CONFIG_USB_FUNCTIONFS_ETH
  264. if (can_support_ecm(cdev->gadget)) {
  265. struct f_ecm_opts *ecm_opts;
  266. fi_ecm = usb_get_function_instance("ecm");
  267. if (IS_ERR(fi_ecm))
  268. return PTR_ERR(fi_ecm);
  269. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  270. net = ecm_opts->net;
  271. } else {
  272. struct f_gether_opts *geth_opts;
  273. fi_geth = usb_get_function_instance("geth");
  274. if (IS_ERR(fi_geth))
  275. return PTR_ERR(fi_geth);
  276. geth_opts = container_of(fi_geth, struct f_gether_opts,
  277. func_inst);
  278. net = geth_opts->net;
  279. }
  280. #endif
  281. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  282. {
  283. fi_rndis = usb_get_function_instance("rndis");
  284. if (IS_ERR(fi_rndis)) {
  285. ret = PTR_ERR(fi_rndis);
  286. goto error;
  287. }
  288. #ifndef CONFIG_USB_FUNCTIONFS_ETH
  289. net = container_of(fi_rndis, struct f_rndis_opts,
  290. func_inst)->net;
  291. #endif
  292. }
  293. #endif
  294. #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
  295. gether_set_qmult(net, qmult);
  296. if (!gether_set_host_addr(net, host_addr))
  297. pr_info("using host ethernet address: %s", host_addr);
  298. if (!gether_set_dev_addr(net, dev_addr))
  299. pr_info("using self ethernet address: %s", dev_addr);
  300. #endif
  301. #if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
  302. gether_set_gadget(net, cdev->gadget);
  303. ret = gether_register_netdev(net);
  304. if (ret)
  305. goto error_rndis;
  306. if (can_support_ecm(cdev->gadget)) {
  307. struct f_ecm_opts *ecm_opts;
  308. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  309. ecm_opts->bound = true;
  310. } else {
  311. struct f_gether_opts *geth_opts;
  312. geth_opts = container_of(fi_geth, struct f_gether_opts,
  313. func_inst);
  314. geth_opts->bound = true;
  315. }
  316. rndis_borrow_net(fi_rndis, net);
  317. #endif
  318. /* TODO: gstrings_attach? */
  319. ret = usb_string_ids_tab(cdev, gfs_strings);
  320. if (unlikely(ret < 0))
  321. goto error_rndis;
  322. gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
  323. if (gadget_is_otg(cdev->gadget) && !gfs_otg_desc[0]) {
  324. struct usb_descriptor_header *usb_desc;
  325. usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
  326. if (!usb_desc)
  327. goto error_rndis;
  328. usb_otg_descriptor_init(cdev->gadget, usb_desc);
  329. gfs_otg_desc[0] = usb_desc;
  330. gfs_otg_desc[1] = NULL;
  331. }
  332. for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
  333. struct gfs_configuration *c = gfs_configurations + i;
  334. int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
  335. c->c.label = gfs_strings[sid].s;
  336. c->c.iConfiguration = gfs_strings[sid].id;
  337. c->c.bConfigurationValue = 1 + i;
  338. c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
  339. c->num = i;
  340. ret = usb_add_config(cdev, &c->c, gfs_do_config);
  341. if (unlikely(ret < 0))
  342. goto error_unbind;
  343. }
  344. usb_composite_overwrite_options(cdev, &coverwrite);
  345. return 0;
  346. /* TODO */
  347. error_unbind:
  348. kfree(gfs_otg_desc[0]);
  349. gfs_otg_desc[0] = NULL;
  350. error_rndis:
  351. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  352. usb_put_function_instance(fi_rndis);
  353. error:
  354. #endif
  355. #if defined CONFIG_USB_FUNCTIONFS_ETH
  356. if (can_support_ecm(cdev->gadget))
  357. usb_put_function_instance(fi_ecm);
  358. else
  359. usb_put_function_instance(fi_geth);
  360. #endif
  361. return ret;
  362. }
  363. /*
  364. * It is assumed that gfs_unbind is called from a context where ffs_lock is held
  365. */
  366. static int gfs_unbind(struct usb_composite_dev *cdev)
  367. {
  368. int i;
  369. ENTER();
  370. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  371. usb_put_function(f_rndis);
  372. usb_put_function_instance(fi_rndis);
  373. #endif
  374. #if defined CONFIG_USB_FUNCTIONFS_ETH
  375. if (can_support_ecm(cdev->gadget)) {
  376. usb_put_function(f_ecm);
  377. usb_put_function_instance(fi_ecm);
  378. } else {
  379. usb_put_function(f_geth);
  380. usb_put_function_instance(fi_geth);
  381. }
  382. #endif
  383. for (i = 0; i < N_CONF * func_num; ++i)
  384. usb_put_function(*(f_ffs[0] + i));
  385. kfree(gfs_otg_desc[0]);
  386. gfs_otg_desc[0] = NULL;
  387. return 0;
  388. }
  389. /*
  390. * It is assumed that gfs_do_config is called from a context where
  391. * ffs_lock is held
  392. */
  393. static int gfs_do_config(struct usb_configuration *c)
  394. {
  395. struct gfs_configuration *gc =
  396. container_of(c, struct gfs_configuration, c);
  397. int i;
  398. int ret;
  399. if (missing_funcs)
  400. return -ENODEV;
  401. if (gadget_is_otg(c->cdev->gadget)) {
  402. c->descriptors = gfs_otg_desc;
  403. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  404. }
  405. if (gc->eth) {
  406. ret = gc->eth(c);
  407. if (unlikely(ret < 0))
  408. return ret;
  409. }
  410. for (i = 0; i < func_num; i++) {
  411. f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]);
  412. if (IS_ERR(f_ffs[gc->num][i])) {
  413. ret = PTR_ERR(f_ffs[gc->num][i]);
  414. goto error;
  415. }
  416. ret = usb_add_function(c, f_ffs[gc->num][i]);
  417. if (ret < 0) {
  418. usb_put_function(f_ffs[gc->num][i]);
  419. goto error;
  420. }
  421. }
  422. /*
  423. * After previous do_configs there may be some invalid
  424. * pointers in c->interface array. This happens every time
  425. * a user space function with fewer interfaces than a user
  426. * space function that was run before the new one is run. The
  427. * compasit's set_config() assumes that if there is no more
  428. * then MAX_CONFIG_INTERFACES interfaces in a configuration
  429. * then there is a NULL pointer after the last interface in
  430. * c->interface array. We need to make sure this is true.
  431. */
  432. if (c->next_interface_id < ARRAY_SIZE(c->interface))
  433. c->interface[c->next_interface_id] = NULL;
  434. return 0;
  435. error:
  436. while (--i >= 0) {
  437. if (!IS_ERR(f_ffs[gc->num][i]))
  438. usb_remove_function(c, f_ffs[gc->num][i]);
  439. usb_put_function(f_ffs[gc->num][i]);
  440. }
  441. return ret;
  442. }
  443. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  444. static int eth_bind_config(struct usb_configuration *c)
  445. {
  446. int status = 0;
  447. if (can_support_ecm(c->cdev->gadget)) {
  448. f_ecm = usb_get_function(fi_ecm);
  449. if (IS_ERR(f_ecm))
  450. return PTR_ERR(f_ecm);
  451. status = usb_add_function(c, f_ecm);
  452. if (status < 0)
  453. usb_put_function(f_ecm);
  454. } else {
  455. f_geth = usb_get_function(fi_geth);
  456. if (IS_ERR(f_geth))
  457. return PTR_ERR(f_geth);
  458. status = usb_add_function(c, f_geth);
  459. if (status < 0)
  460. usb_put_function(f_geth);
  461. }
  462. return status;
  463. }
  464. #endif
  465. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  466. static int bind_rndis_config(struct usb_configuration *c)
  467. {
  468. int status = 0;
  469. f_rndis = usb_get_function(fi_rndis);
  470. if (IS_ERR(f_rndis))
  471. return PTR_ERR(f_rndis);
  472. status = usb_add_function(c, f_rndis);
  473. if (status < 0)
  474. usb_put_function(f_rndis);
  475. return status;
  476. }
  477. #endif