g_ffs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. #else
  30. # if !defined CONFIG_USB_FUNCTIONFS_GENERIC
  31. # define CONFIG_USB_FUNCTIONFS_GENERIC
  32. # endif
  33. # define gether_cleanup() do { } while (0)
  34. # define gether_setup(gadget, hostaddr) ((int)0)
  35. #endif
  36. #include "f_fs.c"
  37. #define DRIVER_NAME "g_ffs"
  38. #define DRIVER_DESC "USB Function Filesystem"
  39. #define DRIVER_VERSION "24 Aug 2004"
  40. MODULE_DESCRIPTION(DRIVER_DESC);
  41. MODULE_AUTHOR("Michal Nazarewicz");
  42. MODULE_LICENSE("GPL");
  43. static unsigned short gfs_vendor_id = 0x0525; /* XXX NetChip */
  44. static unsigned short gfs_product_id = 0xa4ac; /* XXX */
  45. static struct usb_device_descriptor gfs_dev_desc = {
  46. .bLength = sizeof gfs_dev_desc,
  47. .bDescriptorType = USB_DT_DEVICE,
  48. .bcdUSB = cpu_to_le16(0x0200),
  49. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  50. /* Vendor and product id can be overridden by module parameters. */
  51. /* .idVendor = cpu_to_le16(gfs_vendor_id), */
  52. /* .idProduct = cpu_to_le16(gfs_product_id), */
  53. /* .bcdDevice = f(hardware) */
  54. /* .iManufacturer = DYNAMIC */
  55. /* .iProduct = DYNAMIC */
  56. /* NO SERIAL NUMBER */
  57. .bNumConfigurations = 1,
  58. };
  59. #define GFS_MODULE_PARAM_DESC(name, field) \
  60. 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.")
  61. module_param_named(usb_class, gfs_dev_desc.bDeviceClass, byte, 0644);
  62. GFS_MODULE_PARAM_DESC(usb_class, bDeviceClass);
  63. module_param_named(usb_subclass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
  64. GFS_MODULE_PARAM_DESC(usb_subclass, bDeviceSubClass);
  65. module_param_named(usb_protocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
  66. GFS_MODULE_PARAM_DESC(usb_protocol, bDeviceProtocol);
  67. module_param_named(usb_vendor, gfs_vendor_id, ushort, 0644);
  68. GFS_MODULE_PARAM_DESC(usb_vendor, idVendor);
  69. module_param_named(usb_product, gfs_product_id, ushort, 0644);
  70. GFS_MODULE_PARAM_DESC(usb_product, idProduct);
  71. static const struct usb_descriptor_header *gfs_otg_desc[] = {
  72. (const struct usb_descriptor_header *)
  73. &(const struct usb_otg_descriptor) {
  74. .bLength = sizeof(struct usb_otg_descriptor),
  75. .bDescriptorType = USB_DT_OTG,
  76. /* REVISIT SRP-only hardware is possible, although
  77. * it would not be called "OTG" ... */
  78. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  79. },
  80. NULL
  81. };
  82. /* string IDs are assigned dynamically */
  83. enum {
  84. GFS_STRING_MANUFACTURER_IDX,
  85. GFS_STRING_PRODUCT_IDX,
  86. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  87. GFS_STRING_RNDIS_CONFIG_IDX,
  88. #endif
  89. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  90. GFS_STRING_ECM_CONFIG_IDX,
  91. #endif
  92. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  93. GFS_STRING_GENERIC_CONFIG_IDX,
  94. #endif
  95. };
  96. static char gfs_manufacturer[50];
  97. static const char gfs_driver_desc[] = DRIVER_DESC;
  98. static const char gfs_short_name[] = DRIVER_NAME;
  99. static struct usb_string gfs_strings[] = {
  100. [GFS_STRING_MANUFACTURER_IDX].s = gfs_manufacturer,
  101. [GFS_STRING_PRODUCT_IDX].s = gfs_driver_desc,
  102. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  103. [GFS_STRING_RNDIS_CONFIG_IDX].s = "FunctionFS + RNDIS",
  104. #endif
  105. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  106. [GFS_STRING_ECM_CONFIG_IDX].s = "FunctionFS + ECM",
  107. #endif
  108. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  109. [GFS_STRING_GENERIC_CONFIG_IDX].s = "FunctionFS",
  110. #endif
  111. { } /* end of list */
  112. };
  113. static struct usb_gadget_strings *gfs_dev_strings[] = {
  114. &(struct usb_gadget_strings) {
  115. .language = 0x0409, /* en-us */
  116. .strings = gfs_strings,
  117. },
  118. NULL,
  119. };
  120. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  121. static int gfs_do_rndis_config(struct usb_configuration *c);
  122. static struct usb_configuration gfs_rndis_config_driver = {
  123. .label = "FunctionFS + RNDIS",
  124. .bind = gfs_do_rndis_config,
  125. .bConfigurationValue = 1,
  126. /* .iConfiguration = DYNAMIC */
  127. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  128. };
  129. # define gfs_add_rndis_config(cdev) \
  130. usb_add_config(cdev, &gfs_rndis_config_driver)
  131. #else
  132. # define gfs_add_rndis_config(cdev) 0
  133. #endif
  134. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  135. static int gfs_do_ecm_config(struct usb_configuration *c);
  136. static struct usb_configuration gfs_ecm_config_driver = {
  137. .label = "FunctionFS + ECM",
  138. .bind = gfs_do_ecm_config,
  139. .bConfigurationValue = 1,
  140. /* .iConfiguration = DYNAMIC */
  141. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  142. };
  143. # define gfs_add_ecm_config(cdev) \
  144. usb_add_config(cdev, &gfs_ecm_config_driver)
  145. #else
  146. # define gfs_add_ecm_config(cdev) 0
  147. #endif
  148. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  149. static int gfs_do_generic_config(struct usb_configuration *c);
  150. static struct usb_configuration gfs_generic_config_driver = {
  151. .label = "FunctionFS",
  152. .bind = gfs_do_generic_config,
  153. .bConfigurationValue = 2,
  154. /* .iConfiguration = DYNAMIC */
  155. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  156. };
  157. # define gfs_add_generic_config(cdev) \
  158. usb_add_config(cdev, &gfs_generic_config_driver)
  159. #else
  160. # define gfs_add_generic_config(cdev) 0
  161. #endif
  162. static int gfs_bind(struct usb_composite_dev *cdev);
  163. static int gfs_unbind(struct usb_composite_dev *cdev);
  164. static struct usb_composite_driver gfs_driver = {
  165. .name = gfs_short_name,
  166. .dev = &gfs_dev_desc,
  167. .strings = gfs_dev_strings,
  168. .bind = gfs_bind,
  169. .unbind = gfs_unbind,
  170. };
  171. static struct ffs_data *gfs_ffs_data;
  172. static unsigned long gfs_registered;
  173. static int gfs_init(void)
  174. {
  175. ENTER();
  176. return functionfs_init();
  177. }
  178. module_init(gfs_init);
  179. static void gfs_exit(void)
  180. {
  181. ENTER();
  182. if (test_and_clear_bit(0, &gfs_registered))
  183. usb_composite_unregister(&gfs_driver);
  184. functionfs_cleanup();
  185. }
  186. module_exit(gfs_exit);
  187. static int functionfs_ready_callback(struct ffs_data *ffs)
  188. {
  189. int ret;
  190. ENTER();
  191. if (WARN_ON(test_and_set_bit(0, &gfs_registered)))
  192. return -EBUSY;
  193. gfs_ffs_data = ffs;
  194. ret = usb_composite_register(&gfs_driver);
  195. if (unlikely(ret < 0))
  196. clear_bit(0, &gfs_registered);
  197. return ret;
  198. }
  199. static void functionfs_closed_callback(struct ffs_data *ffs)
  200. {
  201. ENTER();
  202. if (test_and_clear_bit(0, &gfs_registered))
  203. usb_composite_unregister(&gfs_driver);
  204. }
  205. static int functionfs_check_dev_callback(const char *dev_name)
  206. {
  207. return 0;
  208. }
  209. static int gfs_bind(struct usb_composite_dev *cdev)
  210. {
  211. int ret;
  212. ENTER();
  213. if (WARN_ON(!gfs_ffs_data))
  214. return -ENODEV;
  215. ret = gether_setup(cdev->gadget, gfs_hostaddr);
  216. if (unlikely(ret < 0))
  217. goto error_quick;
  218. gfs_dev_desc.idVendor = cpu_to_le16(gfs_vendor_id);
  219. gfs_dev_desc.idProduct = cpu_to_le16(gfs_product_id);
  220. snprintf(gfs_manufacturer, sizeof gfs_manufacturer, "%s %s with %s",
  221. init_utsname()->sysname, init_utsname()->release,
  222. cdev->gadget->name);
  223. ret = usb_string_id(cdev);
  224. if (unlikely(ret < 0))
  225. goto error;
  226. gfs_strings[GFS_STRING_MANUFACTURER_IDX].id = ret;
  227. gfs_dev_desc.iManufacturer = ret;
  228. ret = usb_string_id(cdev);
  229. if (unlikely(ret < 0))
  230. goto error;
  231. gfs_strings[GFS_STRING_PRODUCT_IDX].id = ret;
  232. gfs_dev_desc.iProduct = ret;
  233. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  234. ret = usb_string_id(cdev);
  235. if (unlikely(ret < 0))
  236. goto error;
  237. gfs_strings[GFS_STRING_RNDIS_CONFIG_IDX].id = ret;
  238. gfs_rndis_config_driver.iConfiguration = ret;
  239. #endif
  240. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  241. ret = usb_string_id(cdev);
  242. if (unlikely(ret < 0))
  243. goto error;
  244. gfs_strings[GFS_STRING_ECM_CONFIG_IDX].id = ret;
  245. gfs_ecm_config_driver.iConfiguration = ret;
  246. #endif
  247. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  248. ret = usb_string_id(cdev);
  249. if (unlikely(ret < 0))
  250. goto error;
  251. gfs_strings[GFS_STRING_GENERIC_CONFIG_IDX].id = ret;
  252. gfs_generic_config_driver.iConfiguration = ret;
  253. #endif
  254. ret = functionfs_bind(gfs_ffs_data, cdev);
  255. if (unlikely(ret < 0))
  256. goto error;
  257. ret = gfs_add_rndis_config(cdev);
  258. if (unlikely(ret < 0))
  259. goto error_unbind;
  260. ret = gfs_add_ecm_config(cdev);
  261. if (unlikely(ret < 0))
  262. goto error_unbind;
  263. ret = gfs_add_generic_config(cdev);
  264. if (unlikely(ret < 0))
  265. goto error_unbind;
  266. return 0;
  267. error_unbind:
  268. functionfs_unbind(gfs_ffs_data);
  269. error:
  270. gether_cleanup();
  271. error_quick:
  272. gfs_ffs_data = NULL;
  273. return ret;
  274. }
  275. static int gfs_unbind(struct usb_composite_dev *cdev)
  276. {
  277. ENTER();
  278. /* We may have been called in an error recovery frem
  279. * composite_bind() after gfs_unbind() failure so we need to
  280. * check if gfs_ffs_data is not NULL since gfs_bind() handles
  281. * all error recovery itself. I'd rather we werent called
  282. * from composite on orror recovery, but what you're gonna
  283. * do...? */
  284. if (gfs_ffs_data) {
  285. gether_cleanup();
  286. functionfs_unbind(gfs_ffs_data);
  287. gfs_ffs_data = NULL;
  288. }
  289. return 0;
  290. }
  291. static int __gfs_do_config(struct usb_configuration *c,
  292. int (*eth)(struct usb_configuration *c, u8 *ethaddr),
  293. u8 *ethaddr)
  294. {
  295. int ret;
  296. if (WARN_ON(!gfs_ffs_data))
  297. return -ENODEV;
  298. if (gadget_is_otg(c->cdev->gadget)) {
  299. c->descriptors = gfs_otg_desc;
  300. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  301. }
  302. if (eth) {
  303. ret = eth(c, ethaddr);
  304. if (unlikely(ret < 0))
  305. return ret;
  306. }
  307. ret = functionfs_add(c->cdev, c, gfs_ffs_data);
  308. if (unlikely(ret < 0))
  309. return ret;
  310. /* After previous do_configs there may be some invalid
  311. * pointers in c->interface array. This happens every time
  312. * a user space function with fewer interfaces than a user
  313. * space function that was run before the new one is run. The
  314. * compasit's set_config() assumes that if there is no more
  315. * then MAX_CONFIG_INTERFACES interfaces in a configuration
  316. * then there is a NULL pointer after the last interface in
  317. * c->interface array. We need to make sure this is true. */
  318. if (c->next_interface_id < ARRAY_SIZE(c->interface))
  319. c->interface[c->next_interface_id] = NULL;
  320. return 0;
  321. }
  322. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  323. static int gfs_do_rndis_config(struct usb_configuration *c)
  324. {
  325. ENTER();
  326. return __gfs_do_config(c, rndis_bind_config, gfs_hostaddr);
  327. }
  328. #endif
  329. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  330. static int gfs_do_ecm_config(struct usb_configuration *c)
  331. {
  332. ENTER();
  333. return __gfs_do_config(c,
  334. can_support_ecm(c->cdev->gadget)
  335. ? ecm_bind_config : geth_bind_config,
  336. gfs_hostaddr);
  337. }
  338. #endif
  339. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  340. static int gfs_do_generic_config(struct usb_configuration *c)
  341. {
  342. ENTER();
  343. return __gfs_do_config(c, NULL, NULL);
  344. }
  345. #endif