g_ffs.c 11 KB

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