g_ffs.c 11 KB

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