g_ffs.c 11 KB

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