g_ffs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. /*
  15. * kbuild is not very cooperative with respect to linking separately
  16. * compiled library objects into one module. So for now we won't use
  17. * separate compilation ... ensuring init/exit sections work to shrink
  18. * the runtime footprint, and giving us at least some parts of what
  19. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  20. */
  21. #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
  22. # if defined USB_ETH_RNDIS
  23. # undef USB_ETH_RNDIS
  24. # endif
  25. # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  26. # define USB_ETH_RNDIS y
  27. # endif
  28. #define USBF_ECM_INCLUDED
  29. # include "f_ecm.c"
  30. #define USB_FSUBSET_INCLUDED
  31. # include "f_subset.c"
  32. # ifdef USB_ETH_RNDIS
  33. # define USB_FRNDIS_INCLUDED
  34. # include "f_rndis.c"
  35. # include "rndis.h"
  36. # endif
  37. # include "u_ether.h"
  38. static u8 gfs_host_mac[ETH_ALEN];
  39. static struct eth_dev *the_dev;
  40. # ifdef CONFIG_USB_FUNCTIONFS_ETH
  41. static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
  42. struct eth_dev *dev);
  43. # endif
  44. #else
  45. # define the_dev NULL
  46. # define gether_cleanup(dev) do { } while (0)
  47. # define gfs_host_mac NULL
  48. struct eth_dev;
  49. #endif
  50. #include "f_fs.c"
  51. #define DRIVER_NAME "g_ffs"
  52. #define DRIVER_DESC "USB Function Filesystem"
  53. #define DRIVER_VERSION "24 Aug 2004"
  54. MODULE_DESCRIPTION(DRIVER_DESC);
  55. MODULE_AUTHOR("Michal Nazarewicz");
  56. MODULE_LICENSE("GPL");
  57. #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
  58. #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
  59. #define GFS_MAX_DEVS 10
  60. struct gfs_ffs_obj {
  61. const char *name;
  62. bool mounted;
  63. bool desc_ready;
  64. struct ffs_data *ffs_data;
  65. };
  66. USB_GADGET_COMPOSITE_OPTIONS();
  67. #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
  68. USB_ETHERNET_MODULE_PARAMETERS();
  69. #endif
  70. static struct usb_device_descriptor gfs_dev_desc = {
  71. .bLength = sizeof gfs_dev_desc,
  72. .bDescriptorType = USB_DT_DEVICE,
  73. .bcdUSB = cpu_to_le16(0x0200),
  74. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  75. .idVendor = cpu_to_le16(GFS_VENDOR_ID),
  76. .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
  77. };
  78. static char *func_names[GFS_MAX_DEVS];
  79. static unsigned int func_num;
  80. module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
  81. MODULE_PARM_DESC(bDeviceClass, "USB Device class");
  82. module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
  83. MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
  84. module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
  85. MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
  86. module_param_array_named(functions, func_names, charp, &func_num, 0);
  87. MODULE_PARM_DESC(functions, "USB Functions list");
  88. static const struct usb_descriptor_header *gfs_otg_desc[] = {
  89. (const struct usb_descriptor_header *)
  90. &(const struct usb_otg_descriptor) {
  91. .bLength = sizeof(struct usb_otg_descriptor),
  92. .bDescriptorType = USB_DT_OTG,
  93. /*
  94. * REVISIT SRP-only hardware is possible, although
  95. * it would not be called "OTG" ...
  96. */
  97. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  98. },
  99. NULL
  100. };
  101. /* String IDs are assigned dynamically */
  102. static struct usb_string gfs_strings[] = {
  103. [USB_GADGET_MANUFACTURER_IDX].s = "",
  104. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  105. [USB_GADGET_SERIAL_IDX].s = "",
  106. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  107. { .s = "FunctionFS + RNDIS" },
  108. #endif
  109. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  110. { .s = "FunctionFS + ECM" },
  111. #endif
  112. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  113. { .s = "FunctionFS" },
  114. #endif
  115. { } /* end of list */
  116. };
  117. static struct usb_gadget_strings *gfs_dev_strings[] = {
  118. &(struct usb_gadget_strings) {
  119. .language = 0x0409, /* en-us */
  120. .strings = gfs_strings,
  121. },
  122. NULL,
  123. };
  124. struct gfs_configuration {
  125. struct usb_configuration c;
  126. int (*eth)(struct usb_configuration *c, u8 *ethaddr,
  127. struct eth_dev *dev);
  128. } gfs_configurations[] = {
  129. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  130. {
  131. .eth = rndis_bind_config,
  132. },
  133. #endif
  134. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  135. {
  136. .eth = eth_bind_config,
  137. },
  138. #endif
  139. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  140. {
  141. },
  142. #endif
  143. };
  144. static int gfs_bind(struct usb_composite_dev *cdev);
  145. static int gfs_unbind(struct usb_composite_dev *cdev);
  146. static int gfs_do_config(struct usb_configuration *c);
  147. static __refdata struct usb_composite_driver gfs_driver = {
  148. .name = DRIVER_NAME,
  149. .dev = &gfs_dev_desc,
  150. .strings = gfs_dev_strings,
  151. .max_speed = USB_SPEED_HIGH,
  152. .bind = gfs_bind,
  153. .unbind = gfs_unbind,
  154. };
  155. static DEFINE_MUTEX(gfs_lock);
  156. static unsigned int missing_funcs;
  157. static bool gfs_ether_setup;
  158. static bool gfs_registered;
  159. static bool gfs_single_func;
  160. static struct gfs_ffs_obj *ffs_tab;
  161. static int __init gfs_init(void)
  162. {
  163. int i;
  164. ENTER();
  165. if (!func_num) {
  166. gfs_single_func = true;
  167. func_num = 1;
  168. }
  169. ffs_tab = kcalloc(func_num, sizeof *ffs_tab, GFP_KERNEL);
  170. if (!ffs_tab)
  171. return -ENOMEM;
  172. if (!gfs_single_func)
  173. for (i = 0; i < func_num; i++)
  174. ffs_tab[i].name = func_names[i];
  175. missing_funcs = func_num;
  176. return functionfs_init();
  177. }
  178. module_init(gfs_init);
  179. static void __exit gfs_exit(void)
  180. {
  181. ENTER();
  182. mutex_lock(&gfs_lock);
  183. if (gfs_registered)
  184. usb_composite_unregister(&gfs_driver);
  185. gfs_registered = false;
  186. functionfs_cleanup();
  187. mutex_unlock(&gfs_lock);
  188. kfree(ffs_tab);
  189. }
  190. module_exit(gfs_exit);
  191. static struct gfs_ffs_obj *gfs_find_dev(const char *dev_name)
  192. {
  193. int i;
  194. ENTER();
  195. if (gfs_single_func)
  196. return &ffs_tab[0];
  197. for (i = 0; i < func_num; i++)
  198. if (strcmp(ffs_tab[i].name, dev_name) == 0)
  199. return &ffs_tab[i];
  200. return NULL;
  201. }
  202. static int functionfs_ready_callback(struct ffs_data *ffs)
  203. {
  204. struct gfs_ffs_obj *ffs_obj;
  205. int ret;
  206. ENTER();
  207. mutex_lock(&gfs_lock);
  208. ffs_obj = ffs->private_data;
  209. if (!ffs_obj) {
  210. ret = -EINVAL;
  211. goto done;
  212. }
  213. if (WARN_ON(ffs_obj->desc_ready)) {
  214. ret = -EBUSY;
  215. goto done;
  216. }
  217. ffs_obj->desc_ready = true;
  218. ffs_obj->ffs_data = ffs;
  219. if (--missing_funcs) {
  220. ret = 0;
  221. goto done;
  222. }
  223. if (gfs_registered) {
  224. ret = -EBUSY;
  225. goto done;
  226. }
  227. gfs_registered = true;
  228. ret = usb_composite_probe(&gfs_driver);
  229. if (unlikely(ret < 0))
  230. gfs_registered = false;
  231. done:
  232. mutex_unlock(&gfs_lock);
  233. return ret;
  234. }
  235. static void functionfs_closed_callback(struct ffs_data *ffs)
  236. {
  237. struct gfs_ffs_obj *ffs_obj;
  238. ENTER();
  239. mutex_lock(&gfs_lock);
  240. ffs_obj = ffs->private_data;
  241. if (!ffs_obj)
  242. goto done;
  243. ffs_obj->desc_ready = false;
  244. missing_funcs++;
  245. if (gfs_registered)
  246. usb_composite_unregister(&gfs_driver);
  247. gfs_registered = false;
  248. done:
  249. mutex_unlock(&gfs_lock);
  250. }
  251. static void *functionfs_acquire_dev_callback(const char *dev_name)
  252. {
  253. struct gfs_ffs_obj *ffs_dev;
  254. ENTER();
  255. mutex_lock(&gfs_lock);
  256. ffs_dev = gfs_find_dev(dev_name);
  257. if (!ffs_dev) {
  258. ffs_dev = ERR_PTR(-ENODEV);
  259. goto done;
  260. }
  261. if (ffs_dev->mounted) {
  262. ffs_dev = ERR_PTR(-EBUSY);
  263. goto done;
  264. }
  265. ffs_dev->mounted = true;
  266. done:
  267. mutex_unlock(&gfs_lock);
  268. return ffs_dev;
  269. }
  270. static void functionfs_release_dev_callback(struct ffs_data *ffs_data)
  271. {
  272. struct gfs_ffs_obj *ffs_dev;
  273. ENTER();
  274. mutex_lock(&gfs_lock);
  275. ffs_dev = ffs_data->private_data;
  276. if (ffs_dev)
  277. ffs_dev->mounted = false;
  278. mutex_unlock(&gfs_lock);
  279. }
  280. /*
  281. * It is assumed that gfs_bind is called from a context where gfs_lock is held
  282. */
  283. static int gfs_bind(struct usb_composite_dev *cdev)
  284. {
  285. int ret, i;
  286. ENTER();
  287. if (missing_funcs)
  288. return -ENODEV;
  289. #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
  290. the_dev = gether_setup(cdev->gadget, dev_addr, host_addr, gfs_host_mac,
  291. qmult);
  292. #endif
  293. if (IS_ERR(the_dev)) {
  294. ret = PTR_ERR(the_dev);
  295. goto error_quick;
  296. }
  297. gfs_ether_setup = true;
  298. ret = usb_string_ids_tab(cdev, gfs_strings);
  299. if (unlikely(ret < 0))
  300. goto error;
  301. gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
  302. for (i = func_num; i--; ) {
  303. ret = functionfs_bind(ffs_tab[i].ffs_data, cdev);
  304. if (unlikely(ret < 0)) {
  305. while (++i < func_num)
  306. functionfs_unbind(ffs_tab[i].ffs_data);
  307. goto error;
  308. }
  309. }
  310. for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
  311. struct gfs_configuration *c = gfs_configurations + i;
  312. int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
  313. c->c.label = gfs_strings[sid].s;
  314. c->c.iConfiguration = gfs_strings[sid].id;
  315. c->c.bConfigurationValue = 1 + i;
  316. c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
  317. ret = usb_add_config(cdev, &c->c, gfs_do_config);
  318. if (unlikely(ret < 0))
  319. goto error_unbind;
  320. }
  321. usb_composite_overwrite_options(cdev, &coverwrite);
  322. return 0;
  323. error_unbind:
  324. for (i = 0; i < func_num; i++)
  325. functionfs_unbind(ffs_tab[i].ffs_data);
  326. error:
  327. gether_cleanup(the_dev);
  328. error_quick:
  329. gfs_ether_setup = false;
  330. return ret;
  331. }
  332. /*
  333. * It is assumed that gfs_unbind is called from a context where gfs_lock is held
  334. */
  335. static int gfs_unbind(struct usb_composite_dev *cdev)
  336. {
  337. int i;
  338. ENTER();
  339. /*
  340. * We may have been called in an error recovery from
  341. * composite_bind() after gfs_unbind() failure so we need to
  342. * check if gfs_ffs_data is not NULL since gfs_bind() handles
  343. * all error recovery itself. I'd rather we werent called
  344. * from composite on orror recovery, but what you're gonna
  345. * do...?
  346. */
  347. if (gfs_ether_setup)
  348. gether_cleanup(the_dev);
  349. gfs_ether_setup = false;
  350. for (i = func_num; i--; )
  351. if (ffs_tab[i].ffs_data)
  352. functionfs_unbind(ffs_tab[i].ffs_data);
  353. return 0;
  354. }
  355. /*
  356. * It is assumed that gfs_do_config is called from a context where
  357. * gfs_lock is held
  358. */
  359. static int gfs_do_config(struct usb_configuration *c)
  360. {
  361. struct gfs_configuration *gc =
  362. container_of(c, struct gfs_configuration, c);
  363. int i;
  364. int ret;
  365. if (missing_funcs)
  366. return -ENODEV;
  367. if (gadget_is_otg(c->cdev->gadget)) {
  368. c->descriptors = gfs_otg_desc;
  369. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  370. }
  371. if (gc->eth) {
  372. ret = gc->eth(c, gfs_host_mac, the_dev);
  373. if (unlikely(ret < 0))
  374. return ret;
  375. }
  376. for (i = 0; i < func_num; i++) {
  377. ret = functionfs_bind_config(c->cdev, c, ffs_tab[i].ffs_data);
  378. if (unlikely(ret < 0))
  379. return ret;
  380. }
  381. /*
  382. * After previous do_configs there may be some invalid
  383. * pointers in c->interface array. This happens every time
  384. * a user space function with fewer interfaces than a user
  385. * space function that was run before the new one is run. The
  386. * compasit's set_config() assumes that if there is no more
  387. * then MAX_CONFIG_INTERFACES interfaces in a configuration
  388. * then there is a NULL pointer after the last interface in
  389. * c->interface array. We need to make sure this is true.
  390. */
  391. if (c->next_interface_id < ARRAY_SIZE(c->interface))
  392. c->interface[c->next_interface_id] = NULL;
  393. return 0;
  394. }
  395. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  396. static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
  397. struct eth_dev *dev)
  398. {
  399. return can_support_ecm(c->cdev->gadget)
  400. ? ecm_bind_config(c, ethaddr, dev)
  401. : geth_bind_config(c, ethaddr, dev);
  402. }
  403. #endif