g_ffs.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. static unsigned short gfs_vendor_id = 0x0525; /* XXX NetChip */
  45. static unsigned short gfs_product_id = 0xa4ac; /* XXX */
  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. /* Vendor and product id can be overridden by module parameters. */
  52. /* .idVendor = cpu_to_le16(gfs_vendor_id), */
  53. /* .idProduct = cpu_to_le16(gfs_product_id), */
  54. /* .bcdDevice = f(hardware) */
  55. /* .iManufacturer = DYNAMIC */
  56. /* .iProduct = DYNAMIC */
  57. /* NO SERIAL NUMBER */
  58. .bNumConfigurations = 1,
  59. };
  60. #define GFS_MODULE_PARAM_DESC(name, field) \
  61. MODULE_PARM_DESC(name, "Value of the " #field " field of the device descriptor sent to the host. Takes effect only prior to the user-space driver registering to the FunctionFS.")
  62. module_param_named(usb_class, gfs_dev_desc.bDeviceClass, byte, 0644);
  63. GFS_MODULE_PARAM_DESC(usb_class, bDeviceClass);
  64. module_param_named(usb_subclass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
  65. GFS_MODULE_PARAM_DESC(usb_subclass, bDeviceSubClass);
  66. module_param_named(usb_protocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
  67. GFS_MODULE_PARAM_DESC(usb_protocol, bDeviceProtocol);
  68. module_param_named(usb_vendor, gfs_vendor_id, ushort, 0644);
  69. GFS_MODULE_PARAM_DESC(usb_vendor, idVendor);
  70. module_param_named(usb_product, gfs_product_id, ushort, 0644);
  71. GFS_MODULE_PARAM_DESC(usb_product, idProduct);
  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. /* REVISIT SRP-only hardware is possible, although
  78. * it would not be called "OTG" ... */
  79. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  80. },
  81. NULL
  82. };
  83. /* string IDs are assigned dynamically */
  84. enum {
  85. GFS_STRING_MANUFACTURER_IDX,
  86. GFS_STRING_PRODUCT_IDX,
  87. GFS_STRING_FIRST_CONFIG_IDX,
  88. };
  89. static char gfs_manufacturer[50];
  90. static const char gfs_driver_desc[] = DRIVER_DESC;
  91. static const char gfs_short_name[] = DRIVER_NAME;
  92. static struct usb_string gfs_strings[] = {
  93. [GFS_STRING_MANUFACTURER_IDX].s = gfs_manufacturer,
  94. [GFS_STRING_PRODUCT_IDX].s = gfs_driver_desc,
  95. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  96. { .s = "FunctionFS + RNDIS" },
  97. #endif
  98. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  99. { .s = "FunctionFS + ECM" },
  100. #endif
  101. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  102. { .s = "FunctionFS" },
  103. #endif
  104. { } /* end of list */
  105. };
  106. static struct usb_gadget_strings *gfs_dev_strings[] = {
  107. &(struct usb_gadget_strings) {
  108. .language = 0x0409, /* en-us */
  109. .strings = gfs_strings,
  110. },
  111. NULL,
  112. };
  113. struct gfs_configuration {
  114. struct usb_configuration c;
  115. int (*eth)(struct usb_configuration *c, u8 *ethaddr);
  116. } gfs_configurations[] = {
  117. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  118. {
  119. .eth = rndis_bind_config,
  120. },
  121. #endif
  122. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  123. {
  124. .eth = eth_bind_config,
  125. },
  126. #endif
  127. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  128. {
  129. },
  130. #endif
  131. };
  132. static int gfs_bind(struct usb_composite_dev *cdev);
  133. static int gfs_unbind(struct usb_composite_dev *cdev);
  134. static int gfs_do_config(struct usb_configuration *c);
  135. static struct usb_composite_driver gfs_driver = {
  136. .name = gfs_short_name,
  137. .dev = &gfs_dev_desc,
  138. .strings = gfs_dev_strings,
  139. .bind = gfs_bind,
  140. .unbind = gfs_unbind,
  141. };
  142. static struct ffs_data *gfs_ffs_data;
  143. static unsigned long gfs_registered;
  144. static int gfs_init(void)
  145. {
  146. ENTER();
  147. return functionfs_init();
  148. }
  149. module_init(gfs_init);
  150. static void gfs_exit(void)
  151. {
  152. ENTER();
  153. if (test_and_clear_bit(0, &gfs_registered))
  154. usb_composite_unregister(&gfs_driver);
  155. functionfs_cleanup();
  156. }
  157. module_exit(gfs_exit);
  158. static int functionfs_ready_callback(struct ffs_data *ffs)
  159. {
  160. int ret;
  161. ENTER();
  162. if (WARN_ON(test_and_set_bit(0, &gfs_registered)))
  163. return -EBUSY;
  164. gfs_ffs_data = ffs;
  165. ret = usb_composite_register(&gfs_driver);
  166. if (unlikely(ret < 0))
  167. clear_bit(0, &gfs_registered);
  168. return ret;
  169. }
  170. static void functionfs_closed_callback(struct ffs_data *ffs)
  171. {
  172. ENTER();
  173. if (test_and_clear_bit(0, &gfs_registered))
  174. usb_composite_unregister(&gfs_driver);
  175. }
  176. static int functionfs_check_dev_callback(const char *dev_name)
  177. {
  178. return 0;
  179. }
  180. static int gfs_bind(struct usb_composite_dev *cdev)
  181. {
  182. int ret, i;
  183. ENTER();
  184. if (WARN_ON(!gfs_ffs_data))
  185. return -ENODEV;
  186. ret = gether_setup(cdev->gadget, gfs_hostaddr);
  187. if (unlikely(ret < 0))
  188. goto error_quick;
  189. gfs_dev_desc.idVendor = cpu_to_le16(gfs_vendor_id);
  190. gfs_dev_desc.idProduct = cpu_to_le16(gfs_product_id);
  191. snprintf(gfs_manufacturer, sizeof gfs_manufacturer, "%s %s with %s",
  192. init_utsname()->sysname, init_utsname()->release,
  193. cdev->gadget->name);
  194. ret = usb_string_ids_tab(cdev, gfs_strings);
  195. if (unlikely(ret < 0))
  196. goto error;
  197. gfs_dev_desc.iManufacturer = gfs_strings[GFS_STRING_MANUFACTURER_IDX].id;
  198. gfs_dev_desc.iProduct = gfs_strings[GFS_STRING_PRODUCT_IDX].id;
  199. ret = functionfs_bind(gfs_ffs_data, cdev);
  200. if (unlikely(ret < 0))
  201. goto error;
  202. for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
  203. struct gfs_configuration *c = gfs_configurations + i;
  204. ret = GFS_STRING_FIRST_CONFIG_IDX + i;
  205. c->c.label = gfs_strings[ret].s;
  206. c->c.iConfiguration = gfs_strings[ret].id;
  207. c->c.bind = gfs_do_config;
  208. c->c.bConfigurationValue = 1 + i;
  209. c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
  210. ret = usb_add_config(cdev, &c->c);
  211. if (unlikely(ret < 0))
  212. goto error_unbind;
  213. }
  214. return 0;
  215. error_unbind:
  216. functionfs_unbind(gfs_ffs_data);
  217. error:
  218. gether_cleanup();
  219. error_quick:
  220. gfs_ffs_data = NULL;
  221. return ret;
  222. }
  223. static int gfs_unbind(struct usb_composite_dev *cdev)
  224. {
  225. ENTER();
  226. /* We may have been called in an error recovery frem
  227. * composite_bind() after gfs_unbind() failure so we need to
  228. * check if gfs_ffs_data is not NULL since gfs_bind() handles
  229. * all error recovery itself. I'd rather we werent called
  230. * from composite on orror recovery, but what you're gonna
  231. * do...? */
  232. if (gfs_ffs_data) {
  233. gether_cleanup();
  234. functionfs_unbind(gfs_ffs_data);
  235. gfs_ffs_data = NULL;
  236. }
  237. return 0;
  238. }
  239. static int gfs_do_config(struct usb_configuration *c)
  240. {
  241. struct gfs_configuration *gc =
  242. container_of(c, struct gfs_configuration, c);
  243. int ret;
  244. if (WARN_ON(!gfs_ffs_data))
  245. return -ENODEV;
  246. if (gadget_is_otg(c->cdev->gadget)) {
  247. c->descriptors = gfs_otg_desc;
  248. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  249. }
  250. if (gc->eth) {
  251. ret = gc->eth(c, gfs_hostaddr);
  252. if (unlikely(ret < 0))
  253. return ret;
  254. }
  255. ret = functionfs_bind_config(c->cdev, c, gfs_ffs_data);
  256. if (unlikely(ret < 0))
  257. return ret;
  258. /* After previous do_configs there may be some invalid
  259. * pointers in c->interface array. This happens every time
  260. * a user space function with fewer interfaces than a user
  261. * space function that was run before the new one is run. The
  262. * compasit's set_config() assumes that if there is no more
  263. * then MAX_CONFIG_INTERFACES interfaces in a configuration
  264. * then there is a NULL pointer after the last interface in
  265. * c->interface array. We need to make sure this is true. */
  266. if (c->next_interface_id < ARRAY_SIZE(c->interface))
  267. c->interface[c->next_interface_id] = NULL;
  268. return 0;
  269. }
  270. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  271. static int eth_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
  272. {
  273. return can_support_ecm(c->cdev->gadget)
  274. ? ecm_bind_config(c, ethaddr)
  275. : geth_bind_config(c, ethaddr);
  276. }
  277. #endif