g_ffs.c 10 KB

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