g_ffs.c 10 KB

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