g_ffs.c 13 KB

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