g_ffs.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include <linux/module.h>
  2. #include <linux/utsname.h>
  3. /*
  4. * kbuild is not very cooperative with respect to linking separately
  5. * compiled library objects into one module. So for now we won't use
  6. * separate compilation ... ensuring init/exit sections work to shrink
  7. * the runtime footprint, and giving us at least some parts of what
  8. * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  9. */
  10. #include "composite.c"
  11. #include "usbstring.c"
  12. #include "config.c"
  13. #include "epautoconf.c"
  14. #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
  15. # if defined USB_ETH_RNDIS
  16. # undef USB_ETH_RNDIS
  17. # endif
  18. # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  19. # define USB_ETH_RNDIS y
  20. # endif
  21. # include "f_ecm.c"
  22. # include "f_subset.c"
  23. # ifdef USB_ETH_RNDIS
  24. # include "f_rndis.c"
  25. # include "rndis.c"
  26. # endif
  27. # include "u_ether.c"
  28. static u8 gfs_hostaddr[ETH_ALEN];
  29. # ifdef CONFIG_USB_FUNCTIONFS_ETH
  30. static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]);
  31. # endif
  32. #else
  33. # define gether_cleanup() do { } while (0)
  34. # define gether_setup(gadget, hostaddr) ((int)0)
  35. # define gfs_hostaddr NULL
  36. #endif
  37. #include "f_fs.c"
  38. #define DRIVER_NAME "g_ffs"
  39. #define DRIVER_DESC "USB Function Filesystem"
  40. #define DRIVER_VERSION "24 Aug 2004"
  41. MODULE_DESCRIPTION(DRIVER_DESC);
  42. MODULE_AUTHOR("Michal Nazarewicz");
  43. MODULE_LICENSE("GPL");
  44. #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
  45. #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
  46. static struct usb_device_descriptor gfs_dev_desc = {
  47. .bLength = sizeof gfs_dev_desc,
  48. .bDescriptorType = USB_DT_DEVICE,
  49. .bcdUSB = cpu_to_le16(0x0200),
  50. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  51. .idVendor = cpu_to_le16(GFS_VENDOR_ID),
  52. .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
  53. };
  54. module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
  55. MODULE_PARM_DESC(bDeviceClass, "USB Device class");
  56. module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
  57. MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
  58. module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
  59. MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
  60. static const struct usb_descriptor_header *gfs_otg_desc[] = {
  61. (const struct usb_descriptor_header *)
  62. &(const struct usb_otg_descriptor) {
  63. .bLength = sizeof(struct usb_otg_descriptor),
  64. .bDescriptorType = USB_DT_OTG,
  65. /*
  66. * REVISIT SRP-only hardware is possible, although
  67. * it would not be called "OTG" ...
  68. */
  69. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  70. },
  71. NULL
  72. };
  73. /* string IDs are assigned dynamically */
  74. static struct usb_string gfs_strings[] = {
  75. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  76. { .s = "FunctionFS + RNDIS" },
  77. #endif
  78. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  79. { .s = "FunctionFS + ECM" },
  80. #endif
  81. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  82. { .s = "FunctionFS" },
  83. #endif
  84. { } /* end of list */
  85. };
  86. static struct usb_gadget_strings *gfs_dev_strings[] = {
  87. &(struct usb_gadget_strings) {
  88. .language = 0x0409, /* en-us */
  89. .strings = gfs_strings,
  90. },
  91. NULL,
  92. };
  93. struct gfs_configuration {
  94. struct usb_configuration c;
  95. int (*eth)(struct usb_configuration *c, u8 *ethaddr);
  96. } gfs_configurations[] = {
  97. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  98. {
  99. .eth = rndis_bind_config,
  100. },
  101. #endif
  102. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  103. {
  104. .eth = eth_bind_config,
  105. },
  106. #endif
  107. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  108. {
  109. },
  110. #endif
  111. };
  112. static int gfs_bind(struct usb_composite_dev *cdev);
  113. static int gfs_unbind(struct usb_composite_dev *cdev);
  114. static int gfs_do_config(struct usb_configuration *c);
  115. static struct usb_composite_driver gfs_driver = {
  116. .name = DRIVER_NAME,
  117. .dev = &gfs_dev_desc,
  118. .strings = gfs_dev_strings,
  119. .unbind = gfs_unbind,
  120. .iProduct = DRIVER_DESC,
  121. };
  122. static struct ffs_data *gfs_ffs_data;
  123. static unsigned long gfs_registered;
  124. static int gfs_init(void)
  125. {
  126. ENTER();
  127. return functionfs_init();
  128. }
  129. module_init(gfs_init);
  130. static void gfs_exit(void)
  131. {
  132. ENTER();
  133. if (test_and_clear_bit(0, &gfs_registered))
  134. usb_composite_unregister(&gfs_driver);
  135. functionfs_cleanup();
  136. }
  137. module_exit(gfs_exit);
  138. static int functionfs_ready_callback(struct ffs_data *ffs)
  139. {
  140. int ret;
  141. ENTER();
  142. if (WARN_ON(test_and_set_bit(0, &gfs_registered)))
  143. return -EBUSY;
  144. gfs_ffs_data = ffs;
  145. ret = usb_composite_probe(&gfs_driver, gfs_bind);
  146. if (unlikely(ret < 0))
  147. clear_bit(0, &gfs_registered);
  148. return ret;
  149. }
  150. static void functionfs_closed_callback(struct ffs_data *ffs)
  151. {
  152. ENTER();
  153. if (test_and_clear_bit(0, &gfs_registered))
  154. usb_composite_unregister(&gfs_driver);
  155. }
  156. static int functionfs_check_dev_callback(const char *dev_name)
  157. {
  158. return 0;
  159. }
  160. static int gfs_bind(struct usb_composite_dev *cdev)
  161. {
  162. int ret, i;
  163. ENTER();
  164. if (WARN_ON(!gfs_ffs_data))
  165. return -ENODEV;
  166. ret = gether_setup(cdev->gadget, gfs_hostaddr);
  167. if (unlikely(ret < 0))
  168. goto error_quick;
  169. ret = usb_string_ids_tab(cdev, gfs_strings);
  170. if (unlikely(ret < 0))
  171. goto error;
  172. ret = functionfs_bind(gfs_ffs_data, cdev);
  173. if (unlikely(ret < 0))
  174. goto error;
  175. for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
  176. struct gfs_configuration *c = gfs_configurations + i;
  177. c->c.label = gfs_strings[i].s;
  178. c->c.iConfiguration = gfs_strings[i].id;
  179. c->c.bConfigurationValue = 1 + i;
  180. c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
  181. ret = usb_add_config(cdev, &c->c, gfs_do_config);
  182. if (unlikely(ret < 0))
  183. goto error_unbind;
  184. }
  185. return 0;
  186. error_unbind:
  187. functionfs_unbind(gfs_ffs_data);
  188. error:
  189. gether_cleanup();
  190. error_quick:
  191. gfs_ffs_data = NULL;
  192. return ret;
  193. }
  194. static int gfs_unbind(struct usb_composite_dev *cdev)
  195. {
  196. ENTER();
  197. /*
  198. * We may have been called in an error recovery from
  199. * composite_bind() after gfs_unbind() failure so we need to
  200. * check if gfs_ffs_data is not NULL since gfs_bind() handles
  201. * all error recovery itself. I'd rather we werent called
  202. * from composite on orror recovery, but what you're gonna
  203. * do...?
  204. */
  205. if (gfs_ffs_data) {
  206. gether_cleanup();
  207. functionfs_unbind(gfs_ffs_data);
  208. gfs_ffs_data = NULL;
  209. }
  210. return 0;
  211. }
  212. static int gfs_do_config(struct usb_configuration *c)
  213. {
  214. struct gfs_configuration *gc =
  215. container_of(c, struct gfs_configuration, c);
  216. int ret;
  217. if (WARN_ON(!gfs_ffs_data))
  218. return -ENODEV;
  219. if (gadget_is_otg(c->cdev->gadget)) {
  220. c->descriptors = gfs_otg_desc;
  221. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  222. }
  223. if (gc->eth) {
  224. ret = gc->eth(c, gfs_hostaddr);
  225. if (unlikely(ret < 0))
  226. return ret;
  227. }
  228. ret = functionfs_bind_config(c->cdev, c, gfs_ffs_data);
  229. if (unlikely(ret < 0))
  230. return ret;
  231. /*
  232. * After previous do_configs there may be some invalid
  233. * pointers in c->interface array. This happens every time
  234. * a user space function with fewer interfaces than a user
  235. * space function that was run before the new one is run. The
  236. * compasit's set_config() assumes that if there is no more
  237. * then MAX_CONFIG_INTERFACES interfaces in a configuration
  238. * then there is a NULL pointer after the last interface in
  239. * c->interface array. We need to make sure this is true.
  240. */
  241. if (c->next_interface_id < ARRAY_SIZE(c->interface))
  242. c->interface[c->next_interface_id] = NULL;
  243. return 0;
  244. }
  245. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  246. static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
  247. {
  248. return can_support_ecm(c->cdev->gadget)
  249. ? ecm_bind_config(c, ethaddr)
  250. : geth_bind_config(c, ethaddr);
  251. }
  252. #endif