g_ffs.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #define pr_fmt(fmt) "g_ffs: " fmt
  22. #include <linux/module.h>
  23. #include <linux/utsname.h>
  24. /*
  25. * kbuild is not very cooperative with respect to linking separately
  26. * compiled library objects into one module. So for now we won't use
  27. * separate compilation ... ensuring init/exit sections work to shrink
  28. * the runtime footprint, and giving us at least some parts of what
  29. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  30. */
  31. #include "composite.c"
  32. #include "usbstring.c"
  33. #include "config.c"
  34. #include "epautoconf.c"
  35. #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
  36. # if defined USB_ETH_RNDIS
  37. # undef USB_ETH_RNDIS
  38. # endif
  39. # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  40. # define USB_ETH_RNDIS y
  41. # endif
  42. # include "f_ecm.c"
  43. # include "f_subset.c"
  44. # ifdef USB_ETH_RNDIS
  45. # include "f_rndis.c"
  46. # include "rndis.c"
  47. # endif
  48. # include "u_ether.c"
  49. static u8 gfs_hostaddr[ETH_ALEN];
  50. # ifdef CONFIG_USB_FUNCTIONFS_ETH
  51. static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
  52. # endif
  53. #else
  54. # define gether_cleanup() do { } while (0)
  55. # define gether_setup(gadget, hostaddr) ((int)0)
  56. # define gfs_hostaddr NULL
  57. #endif
  58. #include "f_fs.c"
  59. #define DRIVER_NAME "g_ffs"
  60. #define DRIVER_DESC "USB Function Filesystem"
  61. #define DRIVER_VERSION "24 Aug 2004"
  62. MODULE_DESCRIPTION(DRIVER_DESC);
  63. MODULE_AUTHOR("Michal Nazarewicz");
  64. MODULE_LICENSE("GPL");
  65. #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
  66. #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
  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. 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. 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 struct usb_composite_driver gfs_driver = {
  137. .name = DRIVER_NAME,
  138. .dev = &gfs_dev_desc,
  139. .strings = gfs_dev_strings,
  140. .unbind = gfs_unbind,
  141. .iProduct = DRIVER_DESC,
  142. };
  143. static struct ffs_data *gfs_ffs_data;
  144. static unsigned long gfs_registered;
  145. static int gfs_init(void)
  146. {
  147. ENTER();
  148. return functionfs_init();
  149. }
  150. module_init(gfs_init);
  151. static void gfs_exit(void)
  152. {
  153. ENTER();
  154. if (test_and_clear_bit(0, &gfs_registered))
  155. usb_composite_unregister(&gfs_driver);
  156. functionfs_cleanup();
  157. }
  158. module_exit(gfs_exit);
  159. static int functionfs_ready_callback(struct ffs_data *ffs)
  160. {
  161. int ret;
  162. ENTER();
  163. if (WARN_ON(test_and_set_bit(0, &gfs_registered)))
  164. return -EBUSY;
  165. gfs_ffs_data = ffs;
  166. ret = usb_composite_probe(&gfs_driver, gfs_bind);
  167. if (unlikely(ret < 0))
  168. clear_bit(0, &gfs_registered);
  169. return ret;
  170. }
  171. static void functionfs_closed_callback(struct ffs_data *ffs)
  172. {
  173. ENTER();
  174. if (test_and_clear_bit(0, &gfs_registered))
  175. usb_composite_unregister(&gfs_driver);
  176. }
  177. static int functionfs_check_dev_callback(const char *dev_name)
  178. {
  179. return 0;
  180. }
  181. static int gfs_bind(struct usb_composite_dev *cdev)
  182. {
  183. int ret, i;
  184. ENTER();
  185. if (WARN_ON(!gfs_ffs_data))
  186. return -ENODEV;
  187. ret = gether_setup(cdev->gadget, gfs_hostaddr);
  188. if (unlikely(ret < 0))
  189. goto error_quick;
  190. ret = usb_string_ids_tab(cdev, gfs_strings);
  191. if (unlikely(ret < 0))
  192. goto error;
  193. ret = functionfs_bind(gfs_ffs_data, cdev);
  194. if (unlikely(ret < 0))
  195. goto error;
  196. for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
  197. struct gfs_configuration *c = gfs_configurations + i;
  198. c->c.label = gfs_strings[i].s;
  199. c->c.iConfiguration = gfs_strings[i].id;
  200. c->c.bConfigurationValue = 1 + i;
  201. c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
  202. ret = usb_add_config(cdev, &c->c, gfs_do_config);
  203. if (unlikely(ret < 0))
  204. goto error_unbind;
  205. }
  206. return 0;
  207. error_unbind:
  208. functionfs_unbind(gfs_ffs_data);
  209. error:
  210. gether_cleanup();
  211. error_quick:
  212. gfs_ffs_data = NULL;
  213. return ret;
  214. }
  215. static int gfs_unbind(struct usb_composite_dev *cdev)
  216. {
  217. ENTER();
  218. /*
  219. * We may have been called in an error recovery from
  220. * composite_bind() after gfs_unbind() failure so we need to
  221. * check if gfs_ffs_data is not NULL since gfs_bind() handles
  222. * all error recovery itself. I'd rather we werent called
  223. * from composite on orror recovery, but what you're gonna
  224. * do...?
  225. */
  226. if (gfs_ffs_data) {
  227. gether_cleanup();
  228. functionfs_unbind(gfs_ffs_data);
  229. gfs_ffs_data = NULL;
  230. }
  231. return 0;
  232. }
  233. static int gfs_do_config(struct usb_configuration *c)
  234. {
  235. struct gfs_configuration *gc =
  236. container_of(c, struct gfs_configuration, c);
  237. int ret;
  238. if (WARN_ON(!gfs_ffs_data))
  239. return -ENODEV;
  240. if (gadget_is_otg(c->cdev->gadget)) {
  241. c->descriptors = gfs_otg_desc;
  242. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  243. }
  244. if (gc->eth) {
  245. ret = gc->eth(c, gfs_hostaddr);
  246. if (unlikely(ret < 0))
  247. return ret;
  248. }
  249. ret = functionfs_bind_config(c->cdev, c, gfs_ffs_data);
  250. if (unlikely(ret < 0))
  251. return ret;
  252. /*
  253. * After previous do_configs there may be some invalid
  254. * pointers in c->interface array. This happens every time
  255. * a user space function with fewer interfaces than a user
  256. * space function that was run before the new one is run. The
  257. * compasit's set_config() assumes that if there is no more
  258. * then MAX_CONFIG_INTERFACES interfaces in a configuration
  259. * then there is a NULL pointer after the last interface in
  260. * c->interface array. We need to make sure this is true.
  261. */
  262. if (c->next_interface_id < ARRAY_SIZE(c->interface))
  263. c->interface[c->next_interface_id] = NULL;
  264. return 0;
  265. }
  266. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  267. static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
  268. {
  269. return can_support_ecm(c->cdev->gadget)
  270. ? ecm_bind_config(c, ethaddr)
  271. : geth_bind_config(c, ethaddr);
  272. }
  273. #endif