g_ffs.c 8.2 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. .max_speed = USB_SPEED_HIGH,
  141. .unbind = gfs_unbind,
  142. .iProduct = DRIVER_DESC,
  143. };
  144. static struct ffs_data *gfs_ffs_data;
  145. static unsigned long gfs_registered;
  146. static int gfs_init(void)
  147. {
  148. ENTER();
  149. return functionfs_init();
  150. }
  151. module_init(gfs_init);
  152. static void gfs_exit(void)
  153. {
  154. ENTER();
  155. if (test_and_clear_bit(0, &gfs_registered))
  156. usb_composite_unregister(&gfs_driver);
  157. functionfs_cleanup();
  158. }
  159. module_exit(gfs_exit);
  160. static int functionfs_ready_callback(struct ffs_data *ffs)
  161. {
  162. int ret;
  163. ENTER();
  164. if (WARN_ON(test_and_set_bit(0, &gfs_registered)))
  165. return -EBUSY;
  166. gfs_ffs_data = ffs;
  167. ret = usb_composite_probe(&gfs_driver, gfs_bind);
  168. if (unlikely(ret < 0))
  169. clear_bit(0, &gfs_registered);
  170. return ret;
  171. }
  172. static void functionfs_closed_callback(struct ffs_data *ffs)
  173. {
  174. ENTER();
  175. if (test_and_clear_bit(0, &gfs_registered))
  176. usb_composite_unregister(&gfs_driver);
  177. }
  178. static int functionfs_check_dev_callback(const char *dev_name)
  179. {
  180. return 0;
  181. }
  182. static int gfs_bind(struct usb_composite_dev *cdev)
  183. {
  184. int ret, i;
  185. ENTER();
  186. if (WARN_ON(!gfs_ffs_data))
  187. return -ENODEV;
  188. ret = gether_setup(cdev->gadget, gfs_hostaddr);
  189. if (unlikely(ret < 0))
  190. goto error_quick;
  191. ret = usb_string_ids_tab(cdev, gfs_strings);
  192. if (unlikely(ret < 0))
  193. goto error;
  194. ret = functionfs_bind(gfs_ffs_data, cdev);
  195. if (unlikely(ret < 0))
  196. goto error;
  197. for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
  198. struct gfs_configuration *c = gfs_configurations + i;
  199. c->c.label = gfs_strings[i].s;
  200. c->c.iConfiguration = gfs_strings[i].id;
  201. c->c.bConfigurationValue = 1 + i;
  202. c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
  203. ret = usb_add_config(cdev, &c->c, gfs_do_config);
  204. if (unlikely(ret < 0))
  205. goto error_unbind;
  206. }
  207. return 0;
  208. error_unbind:
  209. functionfs_unbind(gfs_ffs_data);
  210. error:
  211. gether_cleanup();
  212. error_quick:
  213. gfs_ffs_data = NULL;
  214. return ret;
  215. }
  216. static int gfs_unbind(struct usb_composite_dev *cdev)
  217. {
  218. ENTER();
  219. /*
  220. * We may have been called in an error recovery from
  221. * composite_bind() after gfs_unbind() failure so we need to
  222. * check if gfs_ffs_data is not NULL since gfs_bind() handles
  223. * all error recovery itself. I'd rather we werent called
  224. * from composite on orror recovery, but what you're gonna
  225. * do...?
  226. */
  227. if (gfs_ffs_data) {
  228. gether_cleanup();
  229. functionfs_unbind(gfs_ffs_data);
  230. gfs_ffs_data = NULL;
  231. }
  232. return 0;
  233. }
  234. static int gfs_do_config(struct usb_configuration *c)
  235. {
  236. struct gfs_configuration *gc =
  237. container_of(c, struct gfs_configuration, c);
  238. int ret;
  239. if (WARN_ON(!gfs_ffs_data))
  240. return -ENODEV;
  241. if (gadget_is_otg(c->cdev->gadget)) {
  242. c->descriptors = gfs_otg_desc;
  243. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  244. }
  245. if (gc->eth) {
  246. ret = gc->eth(c, gfs_hostaddr);
  247. if (unlikely(ret < 0))
  248. return ret;
  249. }
  250. ret = functionfs_bind_config(c->cdev, c, gfs_ffs_data);
  251. if (unlikely(ret < 0))
  252. return ret;
  253. /*
  254. * After previous do_configs there may be some invalid
  255. * pointers in c->interface array. This happens every time
  256. * a user space function with fewer interfaces than a user
  257. * space function that was run before the new one is run. The
  258. * compasit's set_config() assumes that if there is no more
  259. * then MAX_CONFIG_INTERFACES interfaces in a configuration
  260. * then there is a NULL pointer after the last interface in
  261. * c->interface array. We need to make sure this is true.
  262. */
  263. if (c->next_interface_id < ARRAY_SIZE(c->interface))
  264. c->interface[c->next_interface_id] = NULL;
  265. return 0;
  266. }
  267. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  268. static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
  269. {
  270. return can_support_ecm(c->cdev->gadget)
  271. ? ecm_bind_config(c, ethaddr)
  272. : geth_bind_config(c, ethaddr);
  273. }
  274. #endif