g_ffs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 <mina86@mina86.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. #define pr_fmt(fmt) "g_ffs: " fmt
  13. #include <linux/module.h>
  14. #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
  15. #include <linux/netdevice.h>
  16. # if defined USB_ETH_RNDIS
  17. # undef USB_ETH_RNDIS
  18. # endif
  19. # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  20. # define USB_ETH_RNDIS y
  21. # endif
  22. # include "u_ecm.h"
  23. # include "u_gether.h"
  24. # ifdef USB_ETH_RNDIS
  25. # include "u_rndis.h"
  26. # include "rndis.h"
  27. # endif
  28. # include "u_ether.h"
  29. USB_ETHERNET_MODULE_PARAMETERS();
  30. # ifdef CONFIG_USB_FUNCTIONFS_ETH
  31. static int eth_bind_config(struct usb_configuration *c);
  32. static struct usb_function_instance *fi_ecm;
  33. static struct usb_function *f_ecm;
  34. static struct usb_function_instance *fi_geth;
  35. static struct usb_function *f_geth;
  36. # endif
  37. # ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  38. static int bind_rndis_config(struct usb_configuration *c);
  39. static struct usb_function_instance *fi_rndis;
  40. static struct usb_function *f_rndis;
  41. # endif
  42. #endif
  43. #include "u_fs.h"
  44. #define DRIVER_NAME "g_ffs"
  45. #define DRIVER_DESC "USB Function Filesystem"
  46. #define DRIVER_VERSION "24 Aug 2004"
  47. MODULE_DESCRIPTION(DRIVER_DESC);
  48. MODULE_AUTHOR("Michal Nazarewicz");
  49. MODULE_LICENSE("GPL");
  50. #define GFS_VENDOR_ID 0x1d6b /* Linux Foundation */
  51. #define GFS_PRODUCT_ID 0x0105 /* FunctionFS Gadget */
  52. #define GFS_MAX_DEVS 10
  53. USB_GADGET_COMPOSITE_OPTIONS();
  54. static struct usb_device_descriptor gfs_dev_desc = {
  55. .bLength = sizeof gfs_dev_desc,
  56. .bDescriptorType = USB_DT_DEVICE,
  57. .bcdUSB = cpu_to_le16(0x0200),
  58. .bDeviceClass = USB_CLASS_PER_INTERFACE,
  59. .idVendor = cpu_to_le16(GFS_VENDOR_ID),
  60. .idProduct = cpu_to_le16(GFS_PRODUCT_ID),
  61. };
  62. static char *func_names[GFS_MAX_DEVS];
  63. static unsigned int func_num;
  64. module_param_named(bDeviceClass, gfs_dev_desc.bDeviceClass, byte, 0644);
  65. MODULE_PARM_DESC(bDeviceClass, "USB Device class");
  66. module_param_named(bDeviceSubClass, gfs_dev_desc.bDeviceSubClass, byte, 0644);
  67. MODULE_PARM_DESC(bDeviceSubClass, "USB Device subclass");
  68. module_param_named(bDeviceProtocol, gfs_dev_desc.bDeviceProtocol, byte, 0644);
  69. MODULE_PARM_DESC(bDeviceProtocol, "USB Device protocol");
  70. module_param_array_named(functions, func_names, charp, &func_num, 0);
  71. MODULE_PARM_DESC(functions, "USB Functions list");
  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. /*
  78. * REVISIT SRP-only hardware is possible, although
  79. * it would not be called "OTG" ...
  80. */
  81. .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
  82. },
  83. NULL
  84. };
  85. /* String IDs are assigned dynamically */
  86. static struct usb_string gfs_strings[] = {
  87. [USB_GADGET_MANUFACTURER_IDX].s = "",
  88. [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
  89. [USB_GADGET_SERIAL_IDX].s = "",
  90. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  91. { .s = "FunctionFS + RNDIS" },
  92. #endif
  93. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  94. { .s = "FunctionFS + ECM" },
  95. #endif
  96. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  97. { .s = "FunctionFS" },
  98. #endif
  99. { } /* end of list */
  100. };
  101. static struct usb_gadget_strings *gfs_dev_strings[] = {
  102. &(struct usb_gadget_strings) {
  103. .language = 0x0409, /* en-us */
  104. .strings = gfs_strings,
  105. },
  106. NULL,
  107. };
  108. struct gfs_configuration {
  109. struct usb_configuration c;
  110. int (*eth)(struct usb_configuration *c);
  111. int num;
  112. };
  113. static struct gfs_configuration gfs_configurations[] = {
  114. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  115. {
  116. .eth = bind_rndis_config,
  117. },
  118. #endif
  119. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  120. {
  121. .eth = eth_bind_config,
  122. },
  123. #endif
  124. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  125. {
  126. },
  127. #endif
  128. };
  129. static void *functionfs_acquire_dev(struct ffs_dev *dev);
  130. static void functionfs_release_dev(struct ffs_dev *dev);
  131. static int functionfs_ready_callback(struct ffs_data *ffs);
  132. static void functionfs_closed_callback(struct ffs_data *ffs);
  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. .bind = gfs_bind,
  142. .unbind = gfs_unbind,
  143. };
  144. static unsigned int missing_funcs;
  145. static bool gfs_registered;
  146. static bool gfs_single_func;
  147. static struct usb_function_instance **fi_ffs;
  148. static struct usb_function **f_ffs[] = {
  149. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  150. NULL,
  151. #endif
  152. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  153. NULL,
  154. #endif
  155. #ifdef CONFIG_USB_FUNCTIONFS_GENERIC
  156. NULL,
  157. #endif
  158. };
  159. #define N_CONF ARRAY_SIZE(f_ffs)
  160. static int __init gfs_init(void)
  161. {
  162. struct f_fs_opts *opts;
  163. int i;
  164. int ret = 0;
  165. ENTER();
  166. if (func_num < 2) {
  167. gfs_single_func = true;
  168. func_num = 1;
  169. }
  170. /*
  171. * Allocate in one chunk for easier maintenance
  172. */
  173. f_ffs[0] = kcalloc(func_num * N_CONF, sizeof(*f_ffs), GFP_KERNEL);
  174. if (!f_ffs[0]) {
  175. ret = -ENOMEM;
  176. goto no_func;
  177. }
  178. for (i = 1; i < N_CONF; ++i)
  179. f_ffs[i] = f_ffs[0] + i * func_num;
  180. fi_ffs = kcalloc(func_num, sizeof(*fi_ffs), GFP_KERNEL);
  181. if (!fi_ffs) {
  182. ret = -ENOMEM;
  183. goto no_func;
  184. }
  185. for (i = 0; i < func_num; i++) {
  186. fi_ffs[i] = usb_get_function_instance("ffs");
  187. if (IS_ERR(fi_ffs[i])) {
  188. ret = PTR_ERR(fi_ffs[i]);
  189. --i;
  190. goto no_dev;
  191. }
  192. opts = to_f_fs_opts(fi_ffs[i]);
  193. if (gfs_single_func)
  194. ret = ffs_single_dev(opts->dev);
  195. else
  196. ret = ffs_name_dev(opts->dev, func_names[i]);
  197. if (ret)
  198. goto no_dev;
  199. opts->dev->ffs_ready_callback = functionfs_ready_callback;
  200. opts->dev->ffs_closed_callback = functionfs_closed_callback;
  201. opts->dev->ffs_acquire_dev_callback = functionfs_acquire_dev;
  202. opts->dev->ffs_release_dev_callback = functionfs_release_dev;
  203. opts->no_configfs = true;
  204. }
  205. missing_funcs = func_num;
  206. return 0;
  207. no_dev:
  208. while (i >= 0)
  209. usb_put_function_instance(fi_ffs[i--]);
  210. kfree(fi_ffs);
  211. no_func:
  212. kfree(f_ffs[0]);
  213. return ret;
  214. }
  215. module_init(gfs_init);
  216. static void __exit gfs_exit(void)
  217. {
  218. int i;
  219. ENTER();
  220. if (gfs_registered)
  221. usb_composite_unregister(&gfs_driver);
  222. gfs_registered = false;
  223. kfree(f_ffs[0]);
  224. for (i = 0; i < func_num; i++)
  225. usb_put_function_instance(fi_ffs[i]);
  226. kfree(fi_ffs);
  227. }
  228. module_exit(gfs_exit);
  229. static void *functionfs_acquire_dev(struct ffs_dev *dev)
  230. {
  231. if (!try_module_get(THIS_MODULE))
  232. return ERR_PTR(-ENOENT);
  233. return NULL;
  234. }
  235. static void functionfs_release_dev(struct ffs_dev *dev)
  236. {
  237. module_put(THIS_MODULE);
  238. }
  239. /*
  240. * The caller of this function takes ffs_lock
  241. */
  242. static int functionfs_ready_callback(struct ffs_data *ffs)
  243. {
  244. int ret = 0;
  245. if (--missing_funcs)
  246. return 0;
  247. if (gfs_registered)
  248. return -EBUSY;
  249. gfs_registered = true;
  250. ret = usb_composite_probe(&gfs_driver);
  251. if (unlikely(ret < 0)) {
  252. ++missing_funcs;
  253. gfs_registered = false;
  254. }
  255. return ret;
  256. }
  257. /*
  258. * The caller of this function takes ffs_lock
  259. */
  260. static void functionfs_closed_callback(struct ffs_data *ffs)
  261. {
  262. missing_funcs++;
  263. if (gfs_registered)
  264. usb_composite_unregister(&gfs_driver);
  265. gfs_registered = false;
  266. }
  267. /*
  268. * It is assumed that gfs_bind is called from a context where ffs_lock is held
  269. */
  270. static int gfs_bind(struct usb_composite_dev *cdev)
  271. {
  272. #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
  273. struct net_device *net;
  274. #endif
  275. int ret, i;
  276. ENTER();
  277. if (missing_funcs)
  278. return -ENODEV;
  279. #if defined CONFIG_USB_FUNCTIONFS_ETH
  280. if (can_support_ecm(cdev->gadget)) {
  281. struct f_ecm_opts *ecm_opts;
  282. fi_ecm = usb_get_function_instance("ecm");
  283. if (IS_ERR(fi_ecm))
  284. return PTR_ERR(fi_ecm);
  285. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  286. net = ecm_opts->net;
  287. } else {
  288. struct f_gether_opts *geth_opts;
  289. fi_geth = usb_get_function_instance("geth");
  290. if (IS_ERR(fi_geth))
  291. return PTR_ERR(fi_geth);
  292. geth_opts = container_of(fi_geth, struct f_gether_opts,
  293. func_inst);
  294. net = geth_opts->net;
  295. }
  296. #endif
  297. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  298. {
  299. struct f_rndis_opts *rndis_opts;
  300. fi_rndis = usb_get_function_instance("rndis");
  301. if (IS_ERR(fi_rndis)) {
  302. ret = PTR_ERR(fi_rndis);
  303. goto error;
  304. }
  305. rndis_opts = container_of(fi_rndis, struct f_rndis_opts,
  306. func_inst);
  307. #ifndef CONFIG_USB_FUNCTIONFS_ETH
  308. net = rndis_opts->net;
  309. #endif
  310. }
  311. #endif
  312. #if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS
  313. gether_set_qmult(net, qmult);
  314. if (!gether_set_host_addr(net, host_addr))
  315. pr_info("using host ethernet address: %s", host_addr);
  316. if (!gether_set_dev_addr(net, dev_addr))
  317. pr_info("using self ethernet address: %s", dev_addr);
  318. #endif
  319. #if defined CONFIG_USB_FUNCTIONFS_RNDIS && defined CONFIG_USB_FUNCTIONFS_ETH
  320. gether_set_gadget(net, cdev->gadget);
  321. ret = gether_register_netdev(net);
  322. if (ret)
  323. goto error_rndis;
  324. if (can_support_ecm(cdev->gadget)) {
  325. struct f_ecm_opts *ecm_opts;
  326. ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
  327. ecm_opts->bound = true;
  328. } else {
  329. struct f_gether_opts *geth_opts;
  330. geth_opts = container_of(fi_geth, struct f_gether_opts,
  331. func_inst);
  332. geth_opts->bound = true;
  333. }
  334. rndis_borrow_net(fi_rndis, net);
  335. #endif
  336. /* TODO: gstrings_attach? */
  337. ret = usb_string_ids_tab(cdev, gfs_strings);
  338. if (unlikely(ret < 0))
  339. goto error_rndis;
  340. gfs_dev_desc.iProduct = gfs_strings[USB_GADGET_PRODUCT_IDX].id;
  341. for (i = 0; i < ARRAY_SIZE(gfs_configurations); ++i) {
  342. struct gfs_configuration *c = gfs_configurations + i;
  343. int sid = USB_GADGET_FIRST_AVAIL_IDX + i;
  344. c->c.label = gfs_strings[sid].s;
  345. c->c.iConfiguration = gfs_strings[sid].id;
  346. c->c.bConfigurationValue = 1 + i;
  347. c->c.bmAttributes = USB_CONFIG_ATT_SELFPOWER;
  348. c->num = i;
  349. ret = usb_add_config(cdev, &c->c, gfs_do_config);
  350. if (unlikely(ret < 0))
  351. goto error_unbind;
  352. }
  353. usb_composite_overwrite_options(cdev, &coverwrite);
  354. return 0;
  355. /* TODO */
  356. error_unbind:
  357. error_rndis:
  358. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  359. usb_put_function_instance(fi_rndis);
  360. error:
  361. #endif
  362. #if defined CONFIG_USB_FUNCTIONFS_ETH
  363. if (can_support_ecm(cdev->gadget))
  364. usb_put_function_instance(fi_ecm);
  365. else
  366. usb_put_function_instance(fi_geth);
  367. #endif
  368. return ret;
  369. }
  370. /*
  371. * It is assumed that gfs_unbind is called from a context where ffs_lock is held
  372. */
  373. static int gfs_unbind(struct usb_composite_dev *cdev)
  374. {
  375. int i;
  376. ENTER();
  377. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  378. usb_put_function(f_rndis);
  379. usb_put_function_instance(fi_rndis);
  380. #endif
  381. #if defined CONFIG_USB_FUNCTIONFS_ETH
  382. if (can_support_ecm(cdev->gadget)) {
  383. usb_put_function(f_ecm);
  384. usb_put_function_instance(fi_ecm);
  385. } else {
  386. usb_put_function(f_geth);
  387. usb_put_function_instance(fi_geth);
  388. }
  389. #endif
  390. for (i = 0; i < N_CONF * func_num; ++i)
  391. usb_put_function(*(f_ffs[0] + i));
  392. return 0;
  393. }
  394. /*
  395. * It is assumed that gfs_do_config is called from a context where
  396. * ffs_lock is held
  397. */
  398. static int gfs_do_config(struct usb_configuration *c)
  399. {
  400. struct gfs_configuration *gc =
  401. container_of(c, struct gfs_configuration, c);
  402. int i;
  403. int ret;
  404. if (missing_funcs)
  405. return -ENODEV;
  406. if (gadget_is_otg(c->cdev->gadget)) {
  407. c->descriptors = gfs_otg_desc;
  408. c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  409. }
  410. if (gc->eth) {
  411. ret = gc->eth(c);
  412. if (unlikely(ret < 0))
  413. return ret;
  414. }
  415. for (i = 0; i < func_num; i++) {
  416. f_ffs[gc->num][i] = usb_get_function(fi_ffs[i]);
  417. if (IS_ERR(f_ffs[gc->num][i])) {
  418. ret = PTR_ERR(f_ffs[gc->num][i]);
  419. goto error;
  420. }
  421. ret = usb_add_function(c, f_ffs[gc->num][i]);
  422. if (ret < 0) {
  423. usb_put_function(f_ffs[gc->num][i]);
  424. goto error;
  425. }
  426. }
  427. /*
  428. * After previous do_configs there may be some invalid
  429. * pointers in c->interface array. This happens every time
  430. * a user space function with fewer interfaces than a user
  431. * space function that was run before the new one is run. The
  432. * compasit's set_config() assumes that if there is no more
  433. * then MAX_CONFIG_INTERFACES interfaces in a configuration
  434. * then there is a NULL pointer after the last interface in
  435. * c->interface array. We need to make sure this is true.
  436. */
  437. if (c->next_interface_id < ARRAY_SIZE(c->interface))
  438. c->interface[c->next_interface_id] = NULL;
  439. return 0;
  440. error:
  441. while (--i >= 0) {
  442. if (!IS_ERR(f_ffs[gc->num][i]))
  443. usb_remove_function(c, f_ffs[gc->num][i]);
  444. usb_put_function(f_ffs[gc->num][i]);
  445. }
  446. return ret;
  447. }
  448. #ifdef CONFIG_USB_FUNCTIONFS_ETH
  449. static int eth_bind_config(struct usb_configuration *c)
  450. {
  451. int status = 0;
  452. if (can_support_ecm(c->cdev->gadget)) {
  453. f_ecm = usb_get_function(fi_ecm);
  454. if (IS_ERR(f_ecm))
  455. return PTR_ERR(f_ecm);
  456. status = usb_add_function(c, f_ecm);
  457. if (status < 0)
  458. usb_put_function(f_ecm);
  459. } else {
  460. f_geth = usb_get_function(fi_geth);
  461. if (IS_ERR(f_geth))
  462. return PTR_ERR(f_geth);
  463. status = usb_add_function(c, f_geth);
  464. if (status < 0)
  465. usb_put_function(f_geth);
  466. }
  467. return status;
  468. }
  469. #endif
  470. #ifdef CONFIG_USB_FUNCTIONFS_RNDIS
  471. static int bind_rndis_config(struct usb_configuration *c)
  472. {
  473. int status = 0;
  474. f_rndis = usb_get_function(fi_rndis);
  475. if (IS_ERR(f_rndis))
  476. return PTR_ERR(f_rndis);
  477. status = usb_add_function(c, f_rndis);
  478. if (status < 0)
  479. usb_put_function(f_rndis);
  480. return status;
  481. }
  482. #endif