g_ffs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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[] = {
  73. (const struct usb_descriptor_header *)
  74. &(const struct usb_otg_descriptor) {
  75. .bLength = sizeof(struct usb_otg_descriptor),
  76. .bDescriptorType = USB_DT_OTG,
  77. /*
  78. * REVISIT SRP-only hardware is possible, although
  79. * it would not be called "OTG" ...
  80. */
  81. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  82. },
  83. NULL
  84. };
  85. /* String IDs are assigned dynamically */
  86. static struct usb_string gfs_strings[] = {
  87. [USB_GADGET_MANUFACTURER_IDX].s = "",
  88. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  89. [USB_GADGET_SERIAL_IDX].s = "",
  90. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  91. { .s = "FunctionFS + RNDIS" },
  92. #endif
  93. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  94. { .s = "FunctionFS + ECM" },
  95. #endif
  96. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  97. { .s = "FunctionFS" },
  98. #endif
  99. { } /* end of list */
  100. };
  101. static struct usb_gadget_strings *gfs_dev_strings[] = {
  102. &(struct usb_gadget_strings) {
  103. .language = 0x0409, /* en-us */
  104. .strings = gfs_strings,
  105. },
  106. NULL,
  107. };
  108. struct gfs_configuration {
  109. struct usb_configuration c;
  110. int (*eth)(struct usb_configuration *c);
  111. int num;
  112. } gfs_configurations[] = {
  113. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  114. {
  115. .eth = bind_rndis_config,
  116. },
  117. #endif
  118. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  119. {
  120. .eth = eth_bind_config,
  121. },
  122. #endif
  123. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  124. {
  125. },
  126. #endif
  127. };
  128. static void *functionfs_acquire_dev(struct ffs_dev *dev);
  129. static void functionfs_release_dev(struct ffs_dev *dev);
  130. static int functionfs_ready_callback(struct ffs_data *ffs);
  131. static void functionfs_closed_callback(struct ffs_data *ffs);
  132. static int gfs_bind(struct usb_composite_dev *cdev);
  133. static int gfs_unbind(struct usb_composite_dev *cdev);
  134. static int gfs_do_config(struct usb_configuration *c);
  135. static __refdata struct usb_composite_driver gfs_driver = {
  136. .name = DRIVER_NAME,
  137. .dev = &gfs_dev_desc,
  138. .strings = gfs_dev_strings,
  139. .max_speed = USB_SPEED_HIGH,
  140. .bind = gfs_bind,
  141. .unbind = gfs_unbind,
  142. };
  143. static unsigned int missing_funcs;
  144. static bool gfs_registered;
  145. static bool gfs_single_func;
  146. static struct usb_function_instance **fi_ffs;
  147. static struct usb_function **f_ffs[] = {
  148. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  149. NULL,
  150. #endif
  151. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  152. NULL,
  153. #endif
  154. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  155. NULL,
  156. #endif
  157. };
  158. #define N_CONF ARRAY_SIZE(f_ffs)
  159. static int __init gfs_init(void)
  160. {
  161. struct f_fs_opts *opts;
  162. int i;
  163. int ret = 0;
  164. ENTER();
  165. if (func_num < 2) {
  166. gfs_single_func = true;
  167. func_num = 1;
  168. }
  169. /*
  170. * Allocate in one chunk for easier maintenance
  171. */
  172. f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL);
  173. if (!f_ffs[0]) {
  174. ret = -ENOMEM;
  175. goto no_func;
  176. }
  177. for (i = 1; i < N_CONF; ++i)
  178. f_ffs[i] = f_ffs[0] + i * func_num;
  179. fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL);
  180. if (!fi_ffs) {
  181. ret = -ENOMEM;
  182. goto no_func;
  183. }
  184. for (i = 0; i < func_num; i++) {
  185. fi_ffs[i] = usb_get_function_instance("ffs");
  186. if (IS_ERR(fi_ffs[i])) {
  187. ret = PTR_ERR(fi_ffs[i]);
  188. --i;
  189. goto no_dev;
  190. }
  191. opts = to_f_fs_opts(fi_ffs[i]);
  192. if (gfs_single_func)
  193. ret = ffs_single_dev(opts->dev);
  194. else
  195. ret = ffs_name_dev(opts->dev, func_names[i]);
  196. if (ret)
  197. goto no_dev;
  198. opts->dev->ffs_ready_callback = functionfs_ready_callback;
  199. opts->dev->ffs_closed_callback = functionfs_closed_callback;
  200. opts->dev->ffs_acquire_dev_callback = functionfs_acquire_dev;
  201. opts->dev->ffs_release_dev_callback = functionfs_release_dev;
  202. opts->no_configfs = true;
  203. }
  204. missing_funcs = func_num;
  205. return 0;
  206. no_dev:
  207. while (i >= 0)
  208. usb_put_function_instance(fi_ffs[i--]);
  209. kfree(fi_ffs);
  210. no_func:
  211. kfree(f_ffs[0]);
  212. return ret;
  213. }
  214. module_init(gfs_init);
  215. static void __exit gfs_exit(void)
  216. {
  217. int i;
  218. ENTER();
  219. if (gfs_registered)
  220. usb_composite_unregister(&gfs_driver);
  221. gfs_registered = false;
  222. kfree(f_ffs[0]);
  223. for (i = 0; i < func_num; i++)
  224. usb_put_function_instance(fi_ffs[i]);
  225. kfree(fi_ffs);
  226. }
  227. module_exit(gfs_exit);
  228. static void *functionfs_acquire_dev(struct ffs_dev *dev)
  229. {
  230. if (!try_module_get(THIS_MODULE))
  231. return ERR_PTR(-ENODEV);
  232. return 0;
  233. }
  234. static void functionfs_release_dev(struct ffs_dev *dev)
  235. {
  236. module_put(THIS_MODULE);
  237. }
  238. /*
  239. * The caller of this function takes ffs_lock
  240. */
  241. static int functionfs_ready_callback(struct ffs_data *ffs)
  242. {
  243. int ret = 0;
  244. if (--missing_funcs)
  245. return 0;
  246. if (gfs_registered)
  247. return -EBUSY;
  248. gfs_registered = true;
  249. ret = usb_composite_probe(&gfs_driver);
  250. if (unlikely(ret < 0))
  251. gfs_registered = false;
  252. return ret;
  253. }
  254. /*
  255. * The caller of this function takes ffs_lock
  256. */
  257. static void functionfs_closed_callback(struct ffs_data *ffs)
  258. {
  259. missing_funcs++;
  260. if (gfs_registered)
  261. usb_composite_unregister(&gfs_driver);
  262. gfs_registered = false;
  263. }
  264. /*
  265. * It is assumed that gfs_bind is called from a context where ffs_lock is held
  266. */
  267. static int gfs_bind(struct usb_composite_dev *cdev)
  268. {
  269. #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
  270. struct net_device *net;
  271. #endif
  272. int ret, i;
  273. ENTER();
  274. if (missing_funcs)
  275. return -ENODEV;
  276. #if defined CONFIG_USB_FUNCTIONFS_ETH
  277. if (can_support_ecm(cdev->gadget)) {
  278. struct f_ecm_opts *ecm_opts;
  279. fi_ecm = usb_get_function_instance("ecm");
  280. if (IS_ERR(fi_ecm))
  281. return PTR_ERR(fi_ecm);
  282. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  283. net = ecm_opts->net;
  284. } else {
  285. struct f_gether_opts *geth_opts;
  286. fi_geth = usb_get_function_instance("geth");
  287. if (IS_ERR(fi_geth))
  288. return PTR_ERR(fi_geth);
  289. geth_opts = container_of(fi_geth, struct f_gether_opts,
  290. func_inst);
  291. net = geth_opts->net;
  292. }
  293. #endif
  294. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  295. {
  296. struct f_rndis_opts *rndis_opts;
  297. fi_rndis = usb_get_function_instance("rndis");
  298. if (IS_ERR(fi_rndis)) {
  299. ret = PTR_ERR(fi_rndis);
  300. goto error;
  301. }
  302. rndis_opts = container_of(fi_rndis, struct f_rndis_opts,
  303. func_inst);
  304. #ifndef CONFIG_USB_FUNCTIONFS_ETH
  305. net = rndis_opts->net;
  306. #endif
  307. }
  308. #endif
  309. #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
  310. gether_set_qmult(net, qmult);
  311. if (!gether_set_host_addr(net, host_addr))
  312. pr_info("using host ethernet address: %s", host_addr);
  313. if (!gether_set_dev_addr(net, dev_addr))
  314. pr_info("using self ethernet address: %s", dev_addr);
  315. #endif
  316. #if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
  317. gether_set_gadget(net, cdev->gadget);
  318. ret = gether_register_netdev(net);
  319. if (ret)
  320. goto error_rndis;
  321. if (can_support_ecm(cdev->gadget)) {
  322. struct f_ecm_opts *ecm_opts;
  323. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  324. ecm_opts->bound = true;
  325. } else {
  326. struct f_gether_opts *geth_opts;
  327. geth_opts = container_of(fi_geth, struct f_gether_opts,
  328. func_inst);
  329. geth_opts->bound = true;
  330. }
  331. rndis_borrow_net(fi_rndis, net);
  332. #endif
  333. /* TODO: gstrings_attach? */
  334. ret = usb_string_ids_tab(cdev, gfs_strings);
  335. if (unlikely(ret < 0))
  336. goto error_rndis;
  337. gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
  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. error_rndis:
  355. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  356. usb_put_function_instance(fi_rndis);
  357. error:
  358. #endif
  359. #if defined CONFIG_USB_FUNCTIONFS_ETH
  360. if (can_support_ecm(cdev->gadget))
  361. usb_put_function_instance(fi_ecm);
  362. else
  363. usb_put_function_instance(fi_geth);
  364. #endif
  365. return ret;
  366. }
  367. /*
  368. * It is assumed that gfs_unbind is called from a context where ffs_lock is held
  369. */
  370. static int gfs_unbind(struct usb_composite_dev *cdev)
  371. {
  372. int i;
  373. ENTER();
  374. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  375. usb_put_function(f_rndis);
  376. usb_put_function_instance(fi_rndis);
  377. #endif
  378. #if defined CONFIG_USB_FUNCTIONFS_ETH
  379. if (can_support_ecm(cdev->gadget)) {
  380. usb_put_function(f_ecm);
  381. usb_put_function_instance(fi_ecm);
  382. } else {
  383. usb_put_function(f_geth);
  384. usb_put_function_instance(fi_geth);
  385. }
  386. #endif
  387. for (i = 0; i < N_CONF * func_num; ++i)
  388. usb_put_function(*(f_ffs[0] + i));
  389. return 0;
  390. }
  391. /*
  392. * It is assumed that gfs_do_config is called from a context where
  393. * ffs_lock is held
  394. */
  395. static int gfs_do_config(struct usb_configuration *c)
  396. {
  397. struct gfs_configuration *gc =
  398. container_of(c, struct gfs_configuration, c);
  399. int i;
  400. int ret;
  401. if (missing_funcs)
  402. return -ENODEV;
  403. if (gadget_is_otg(c->cdev->gadget)) {
  404. c->descriptors = gfs_otg_desc;
  405. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  406. }
  407. if (gc->eth) {
  408. ret = gc->eth(c);
  409. if (unlikely(ret < 0))
  410. return ret;
  411. }
  412. for (i = 0; i < func_num; i++) {
  413. f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]);
  414. if (IS_ERR(f_ffs[gc->num][i])) {
  415. ret = PTR_ERR(f_ffs[gc->num][i]);
  416. goto error;
  417. }
  418. ret = usb_add_function(c, f_ffs[gc->num][i]);
  419. if (ret < 0) {
  420. usb_put_function(f_ffs[gc->num][i]);
  421. goto error;
  422. }
  423. }
  424. /*
  425. * After previous do_configs there may be some invalid
  426. * pointers in c->interface array. This happens every time
  427. * a user space function with fewer interfaces than a user
  428. * space function that was run before the new one is run. The
  429. * compasit's set_config() assumes that if there is no more
  430. * then MAX_CONFIG_INTERFACES interfaces in a configuration
  431. * then there is a NULL pointer after the last interface in
  432. * c->interface array. We need to make sure this is true.
  433. */
  434. if (c->next_interface_id < ARRAY_SIZE(c->interface))
  435. c->interface[c->next_interface_id] = NULL;
  436. return 0;
  437. error:
  438. while (--i >= 0) {
  439. if (!IS_ERR(f_ffs[gc->num][i]))
  440. usb_remove_function(c, f_ffs[gc->num][i]);
  441. usb_put_function(f_ffs[gc->num][i]);
  442. }
  443. return ret;
  444. }
  445. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  446. static int eth_bind_config(struct usb_configuration *c)
  447. {
  448. int status = 0;
  449. if (can_support_ecm(c->cdev->gadget)) {
  450. f_ecm = usb_get_function(fi_ecm);
  451. if (IS_ERR(f_ecm))
  452. return PTR_ERR(f_ecm);
  453. status = usb_add_function(c, f_ecm);
  454. if (status < 0)
  455. usb_put_function(f_ecm);
  456. } else {
  457. f_geth = usb_get_function(fi_geth);
  458. if (IS_ERR(f_geth))
  459. return PTR_ERR(f_geth);
  460. status = usb_add_function(c, f_geth);
  461. if (status < 0)
  462. usb_put_function(f_geth);
  463. }
  464. return status;
  465. }
  466. #endif
  467. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  468. static int bind_rndis_config(struct usb_configuration *c)
  469. {
  470. int status = 0;
  471. f_rndis = usb_get_function(fi_rndis);
  472. if (IS_ERR(f_rndis))
  473. return PTR_ERR(f_rndis);
  474. status = usb_add_function(c, f_rndis);
  475. if (status < 0)
  476. usb_put_function(f_rndis);
  477. return status;
  478. }
  479. #endif