g_ffs.c 11 KB

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