g_ffs.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 <m.nazarewicz@samsung.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. static struct usb_device_descriptor gfs_dev_desc = {
  59. .bLength = sizeof gfs_dev_desc,
  60. .bDescriptorType = USB_DT_DEVICE,
  61. .bcdUSB = cpu_to_le16(0x0200),
  62. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  63. .idVendor = cpu_to_le16(GFS_VENDOR_ID),
  64. .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
  65. };
  66. module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
  67. MODULE_PARM_DESC(bDeviceClass, "USB Device class");
  68. module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
  69. MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
  70. module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
  71. MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
  72. static const struct usb_descriptor_header *gfs_otg_desc[] = {
  73. (const struct usb_descriptor_header *)
  74. &(const struct usb_otg_descriptor) {
  75. .bLength = sizeof(struct usb_otg_descriptor),
  76. .bDescriptorType = USB_DT_OTG,
  77. /*
  78. * REVISIT SRP-only hardware is possible, although
  79. * it would not be called "OTG" ...
  80. */
  81. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  82. },
  83. NULL
  84. };
  85. /* String IDs are assigned dynamically */
  86. static struct usb_string gfs_strings[] = {
  87. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  88. { .s = "FunctionFS + RNDIS" },
  89. #endif
  90. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  91. { .s = "FunctionFS + ECM" },
  92. #endif
  93. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  94. { .s = "FunctionFS" },
  95. #endif
  96. { } /* end of list */
  97. };
  98. static struct usb_gadget_strings *gfs_dev_strings[] = {
  99. &(struct usb_gadget_strings) {
  100. .language = 0x0409, /* en-us */
  101. .strings = gfs_strings,
  102. },
  103. NULL,
  104. };
  105. struct gfs_configuration {
  106. struct usb_configuration c;
  107. int (*eth)(struct usb_configuration *c, u8 *ethaddr);
  108. } gfs_configurations[] = {
  109. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  110. {
  111. .eth = rndis_bind_config,
  112. },
  113. #endif
  114. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  115. {
  116. .eth = eth_bind_config,
  117. },
  118. #endif
  119. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  120. {
  121. },
  122. #endif
  123. };
  124. static int gfs_bind(struct usb_composite_dev *cdev);
  125. static int gfs_unbind(struct usb_composite_dev *cdev);
  126. static int gfs_do_config(struct usb_configuration *c);
  127. static struct usb_composite_driver gfs_driver = {
  128. .name = DRIVER_NAME,
  129. .dev = &gfs_dev_desc,
  130. .strings = gfs_dev_strings,
  131. .max_speed = USB_SPEED_HIGH,
  132. .unbind = gfs_unbind,
  133. .iProduct = DRIVER_DESC,
  134. };
  135. static struct ffs_data *gfs_ffs_data;
  136. static unsigned long gfs_registered;
  137. static int gfs_init(void)
  138. {
  139. ENTER();
  140. return functionfs_init();
  141. }
  142. module_init(gfs_init);
  143. static void gfs_exit(void)
  144. {
  145. ENTER();
  146. if (test_and_clear_bit(0, &gfs_registered))
  147. usb_composite_unregister(&gfs_driver);
  148. functionfs_cleanup();
  149. }
  150. module_exit(gfs_exit);
  151. static int functionfs_ready_callback(struct ffs_data *ffs)
  152. {
  153. int ret;
  154. ENTER();
  155. if (WARN_ON(test_and_set_bit(0, &gfs_registered)))
  156. return -EBUSY;
  157. gfs_ffs_data = ffs;
  158. ret = usb_composite_probe(&gfs_driver, gfs_bind);
  159. if (unlikely(ret < 0))
  160. clear_bit(0, &gfs_registered);
  161. return ret;
  162. }
  163. static void functionfs_closed_callback(struct ffs_data *ffs)
  164. {
  165. ENTER();
  166. if (test_and_clear_bit(0, &gfs_registered))
  167. usb_composite_unregister(&gfs_driver);
  168. }
  169. static int functionfs_check_dev_callback(const char *dev_name)
  170. {
  171. return 0;
  172. }
  173. static int gfs_bind(struct usb_composite_dev *cdev)
  174. {
  175. int ret, i;
  176. ENTER();
  177. if (WARN_ON(!gfs_ffs_data))
  178. return -ENODEV;
  179. ret = gether_setup(cdev->gadget, gfs_hostaddr);
  180. if (unlikely(ret < 0))
  181. goto error_quick;
  182. ret = usb_string_ids_tab(cdev, gfs_strings);
  183. if (unlikely(ret < 0))
  184. goto error;
  185. ret = functionfs_bind(gfs_ffs_data, cdev);
  186. if (unlikely(ret < 0))
  187. goto error;
  188. for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
  189. struct gfs_configuration *c = gfs_configurations + i;
  190. c->c.label = gfs_strings[i].s;
  191. c->c.iConfiguration = gfs_strings[i].id;
  192. c->c.bConfigurationValue = 1 + i;
  193. c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
  194. ret = usb_add_config(cdev, &c->c, gfs_do_config);
  195. if (unlikely(ret < 0))
  196. goto error_unbind;
  197. }
  198. return 0;
  199. error_unbind:
  200. functionfs_unbind(gfs_ffs_data);
  201. error:
  202. gether_cleanup();
  203. error_quick:
  204. gfs_ffs_data = NULL;
  205. return ret;
  206. }
  207. static int gfs_unbind(struct usb_composite_dev *cdev)
  208. {
  209. ENTER();
  210. /*
  211. * We may have been called in an error recovery from
  212. * composite_bind() after gfs_unbind() failure so we need to
  213. * check if gfs_ffs_data is not NULL since gfs_bind() handles
  214. * all error recovery itself. I'd rather we werent called
  215. * from composite on orror recovery, but what you're gonna
  216. * do...?
  217. */
  218. if (gfs_ffs_data) {
  219. gether_cleanup();
  220. functionfs_unbind(gfs_ffs_data);
  221. gfs_ffs_data = NULL;
  222. }
  223. return 0;
  224. }
  225. static int gfs_do_config(struct usb_configuration *c)
  226. {
  227. struct gfs_configuration *gc =
  228. container_of(c, struct gfs_configuration, c);
  229. int ret;
  230. if (WARN_ON(!gfs_ffs_data))
  231. return -ENODEV;
  232. if (gadget_is_otg(c->cdev->gadget)) {
  233. c->descriptors = gfs_otg_desc;
  234. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  235. }
  236. if (gc->eth) {
  237. ret = gc->eth(c, gfs_hostaddr);
  238. if (unlikely(ret < 0))
  239. return ret;
  240. }
  241. ret = functionfs_bind_config(c->cdev, c, gfs_ffs_data);
  242. if (unlikely(ret < 0))
  243. return ret;
  244. /*
  245. * After previous do_configs there may be some invalid
  246. * pointers in c->interface array. This happens every time
  247. * a user space function with fewer interfaces than a user
  248. * space function that was run before the new one is run. The
  249. * compasit's set_config() assumes that if there is no more
  250. * then MAX_CONFIG_INTERFACES interfaces in a configuration
  251. * then there is a NULL pointer after the last interface in
  252. * c->interface array. We need to make sure this is true.
  253. */
  254. if (c->next_interface_id < ARRAY_SIZE(c->interface))
  255. c->interface[c->next_interface_id] = NULL;
  256. return 0;
  257. }
  258. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  259. static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
  260. {
  261. return can_support_ecm(c->cdev->gadget)
  262. ? ecm_bind_config(c, ethaddr)
  263. : geth_bind_config(c, ethaddr);
  264. }
  265. #endif