configfs.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. #include <linux/configfs.h>
  2. #include <linux/module.h>
  3. #include <linux/slab.h>
  4. #include <linux/device.h>
  5. #include <linux/nls.h>
  6. #include <linux/usb/composite.h>
  7. #include <linux/usb/gadget_configfs.h>
  8. #include "configfs.h"
  9. #include "u_f.h"
  10. #include "u_os_desc.h"
  11. int check_user_usb_string(const char *name,
  12. struct usb_gadget_strings *stringtab_dev)
  13. {
  14. unsigned primary_lang;
  15. unsigned sub_lang;
  16. u16 num;
  17. int ret;
  18. ret = kstrtou16(name, 0, &num);
  19. if (ret)
  20. return ret;
  21. primary_lang = num & 0x3ff;
  22. sub_lang = num >> 10;
  23. /* simple sanity check for valid langid */
  24. switch (primary_lang) {
  25. case 0:
  26. case 0x62 ... 0xfe:
  27. case 0x100 ... 0x3ff:
  28. return -EINVAL;
  29. }
  30. if (!sub_lang)
  31. return -EINVAL;
  32. stringtab_dev->language = num;
  33. return 0;
  34. }
  35. #define MAX_NAME_LEN 40
  36. #define MAX_USB_STRING_LANGS 2
  37. static const struct usb_descriptor_header *otg_desc[2];
  38. struct gadget_info {
  39. struct config_group group;
  40. struct config_group functions_group;
  41. struct config_group configs_group;
  42. struct config_group strings_group;
  43. struct config_group os_desc_group;
  44. struct mutex lock;
  45. struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
  46. struct list_head string_list;
  47. struct list_head available_func;
  48. struct usb_composite_driver composite;
  49. struct usb_composite_dev cdev;
  50. bool use_os_desc;
  51. char b_vendor_code;
  52. char qw_sign[OS_STRING_QW_SIGN_LEN];
  53. };
  54. static inline struct gadget_info *to_gadget_info(struct config_item *item)
  55. {
  56. return container_of(to_config_group(item), struct gadget_info, group);
  57. }
  58. struct config_usb_cfg {
  59. struct config_group group;
  60. struct config_group strings_group;
  61. struct list_head string_list;
  62. struct usb_configuration c;
  63. struct list_head func_list;
  64. struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
  65. };
  66. static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
  67. {
  68. return container_of(to_config_group(item), struct config_usb_cfg,
  69. group);
  70. }
  71. struct gadget_strings {
  72. struct usb_gadget_strings stringtab_dev;
  73. struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
  74. char *manufacturer;
  75. char *product;
  76. char *serialnumber;
  77. struct config_group group;
  78. struct list_head list;
  79. };
  80. struct os_desc {
  81. struct config_group group;
  82. };
  83. struct gadget_config_name {
  84. struct usb_gadget_strings stringtab_dev;
  85. struct usb_string strings;
  86. char *configuration;
  87. struct config_group group;
  88. struct list_head list;
  89. };
  90. static int usb_string_copy(const char *s, char **s_copy)
  91. {
  92. int ret;
  93. char *str;
  94. char *copy = *s_copy;
  95. ret = strlen(s);
  96. if (ret > 126)
  97. return -EOVERFLOW;
  98. str = kstrdup(s, GFP_KERNEL);
  99. if (!str)
  100. return -ENOMEM;
  101. if (str[ret - 1] == '\n')
  102. str[ret - 1] = '\0';
  103. kfree(copy);
  104. *s_copy = str;
  105. return 0;
  106. }
  107. #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
  108. static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
  109. char *page) \
  110. { \
  111. return sprintf(page, "0x%02x\n", \
  112. to_gadget_info(item)->cdev.desc.__name); \
  113. }
  114. #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
  115. static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
  116. char *page) \
  117. { \
  118. return sprintf(page, "0x%04x\n", \
  119. le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
  120. }
  121. #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
  122. static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
  123. const char *page, size_t len) \
  124. { \
  125. u8 val; \
  126. int ret; \
  127. ret = kstrtou8(page, 0, &val); \
  128. if (ret) \
  129. return ret; \
  130. to_gadget_info(item)->cdev.desc._name = val; \
  131. return len; \
  132. }
  133. #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
  134. static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
  135. const char *page, size_t len) \
  136. { \
  137. u16 val; \
  138. int ret; \
  139. ret = kstrtou16(page, 0, &val); \
  140. if (ret) \
  141. return ret; \
  142. to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
  143. return len; \
  144. }
  145. #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
  146. GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
  147. GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
  148. GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
  149. GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
  150. GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
  151. GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
  152. GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
  153. GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
  154. GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
  155. GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
  156. static ssize_t is_valid_bcd(u16 bcd_val)
  157. {
  158. if ((bcd_val & 0xf) > 9)
  159. return -EINVAL;
  160. if (((bcd_val >> 4) & 0xf) > 9)
  161. return -EINVAL;
  162. if (((bcd_val >> 8) & 0xf) > 9)
  163. return -EINVAL;
  164. if (((bcd_val >> 12) & 0xf) > 9)
  165. return -EINVAL;
  166. return 0;
  167. }
  168. static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
  169. const char *page, size_t len)
  170. {
  171. u16 bcdDevice;
  172. int ret;
  173. ret = kstrtou16(page, 0, &bcdDevice);
  174. if (ret)
  175. return ret;
  176. ret = is_valid_bcd(bcdDevice);
  177. if (ret)
  178. return ret;
  179. to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
  180. return len;
  181. }
  182. static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
  183. const char *page, size_t len)
  184. {
  185. u16 bcdUSB;
  186. int ret;
  187. ret = kstrtou16(page, 0, &bcdUSB);
  188. if (ret)
  189. return ret;
  190. ret = is_valid_bcd(bcdUSB);
  191. if (ret)
  192. return ret;
  193. to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
  194. return len;
  195. }
  196. static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
  197. {
  198. char *udc_name = to_gadget_info(item)->composite.gadget_driver.udc_name;
  199. return sprintf(page, "%s\n", udc_name ?: "");
  200. }
  201. static int unregister_gadget(struct gadget_info *gi)
  202. {
  203. int ret;
  204. if (!gi->composite.gadget_driver.udc_name)
  205. return -ENODEV;
  206. ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
  207. if (ret)
  208. return ret;
  209. kfree(gi->composite.gadget_driver.udc_name);
  210. gi->composite.gadget_driver.udc_name = NULL;
  211. return 0;
  212. }
  213. static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
  214. const char *page, size_t len)
  215. {
  216. struct gadget_info *gi = to_gadget_info(item);
  217. char *name;
  218. int ret;
  219. name = kstrdup(page, GFP_KERNEL);
  220. if (!name)
  221. return -ENOMEM;
  222. if (name[len - 1] == '\n')
  223. name[len - 1] = '\0';
  224. mutex_lock(&gi->lock);
  225. if (!strlen(name)) {
  226. ret = unregister_gadget(gi);
  227. if (ret)
  228. goto err;
  229. } else {
  230. if (gi->composite.gadget_driver.udc_name) {
  231. ret = -EBUSY;
  232. goto err;
  233. }
  234. gi->composite.gadget_driver.udc_name = name;
  235. ret = usb_gadget_probe_driver(&gi->composite.gadget_driver);
  236. if (ret) {
  237. gi->composite.gadget_driver.udc_name = NULL;
  238. goto err;
  239. }
  240. }
  241. mutex_unlock(&gi->lock);
  242. return len;
  243. err:
  244. kfree(name);
  245. mutex_unlock(&gi->lock);
  246. return ret;
  247. }
  248. CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
  249. CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
  250. CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
  251. CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
  252. CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
  253. CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
  254. CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
  255. CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
  256. CONFIGFS_ATTR(gadget_dev_desc_, UDC);
  257. static struct configfs_attribute *gadget_root_attrs[] = {
  258. &gadget_dev_desc_attr_bDeviceClass,
  259. &gadget_dev_desc_attr_bDeviceSubClass,
  260. &gadget_dev_desc_attr_bDeviceProtocol,
  261. &gadget_dev_desc_attr_bMaxPacketSize0,
  262. &gadget_dev_desc_attr_idVendor,
  263. &gadget_dev_desc_attr_idProduct,
  264. &gadget_dev_desc_attr_bcdDevice,
  265. &gadget_dev_desc_attr_bcdUSB,
  266. &gadget_dev_desc_attr_UDC,
  267. NULL,
  268. };
  269. static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
  270. {
  271. return container_of(to_config_group(item), struct gadget_strings,
  272. group);
  273. }
  274. static inline struct gadget_config_name *to_gadget_config_name(
  275. struct config_item *item)
  276. {
  277. return container_of(to_config_group(item), struct gadget_config_name,
  278. group);
  279. }
  280. static inline struct usb_function_instance *to_usb_function_instance(
  281. struct config_item *item)
  282. {
  283. return container_of(to_config_group(item),
  284. struct usb_function_instance, group);
  285. }
  286. static void gadget_info_attr_release(struct config_item *item)
  287. {
  288. struct gadget_info *gi = to_gadget_info(item);
  289. WARN_ON(!list_empty(&gi->cdev.configs));
  290. WARN_ON(!list_empty(&gi->string_list));
  291. WARN_ON(!list_empty(&gi->available_func));
  292. kfree(gi->composite.gadget_driver.function);
  293. kfree(gi);
  294. }
  295. static struct configfs_item_operations gadget_root_item_ops = {
  296. .release = gadget_info_attr_release,
  297. };
  298. static void gadget_config_attr_release(struct config_item *item)
  299. {
  300. struct config_usb_cfg *cfg = to_config_usb_cfg(item);
  301. WARN_ON(!list_empty(&cfg->c.functions));
  302. list_del(&cfg->c.list);
  303. kfree(cfg->c.label);
  304. kfree(cfg);
  305. }
  306. static int config_usb_cfg_link(
  307. struct config_item *usb_cfg_ci,
  308. struct config_item *usb_func_ci)
  309. {
  310. struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
  311. struct usb_composite_dev *cdev = cfg->c.cdev;
  312. struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
  313. struct config_group *group = to_config_group(usb_func_ci);
  314. struct usb_function_instance *fi = container_of(group,
  315. struct usb_function_instance, group);
  316. struct usb_function_instance *a_fi;
  317. struct usb_function *f;
  318. int ret;
  319. mutex_lock(&gi->lock);
  320. /*
  321. * Make sure this function is from within our _this_ gadget and not
  322. * from another gadget or a random directory.
  323. * Also a function instance can only be linked once.
  324. */
  325. list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
  326. if (a_fi == fi)
  327. break;
  328. }
  329. if (a_fi != fi) {
  330. ret = -EINVAL;
  331. goto out;
  332. }
  333. list_for_each_entry(f, &cfg->func_list, list) {
  334. if (f->fi == fi) {
  335. ret = -EEXIST;
  336. goto out;
  337. }
  338. }
  339. f = usb_get_function(fi);
  340. if (IS_ERR(f)) {
  341. ret = PTR_ERR(f);
  342. goto out;
  343. }
  344. /* stash the function until we bind it to the gadget */
  345. list_add_tail(&f->list, &cfg->func_list);
  346. ret = 0;
  347. out:
  348. mutex_unlock(&gi->lock);
  349. return ret;
  350. }
  351. static void config_usb_cfg_unlink(
  352. struct config_item *usb_cfg_ci,
  353. struct config_item *usb_func_ci)
  354. {
  355. struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
  356. struct usb_composite_dev *cdev = cfg->c.cdev;
  357. struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
  358. struct config_group *group = to_config_group(usb_func_ci);
  359. struct usb_function_instance *fi = container_of(group,
  360. struct usb_function_instance, group);
  361. struct usb_function *f;
  362. /*
  363. * ideally I would like to forbid to unlink functions while a gadget is
  364. * bound to an UDC. Since this isn't possible at the moment, we simply
  365. * force an unbind, the function is available here and then we can
  366. * remove the function.
  367. */
  368. mutex_lock(&gi->lock);
  369. if (gi->composite.gadget_driver.udc_name)
  370. unregister_gadget(gi);
  371. WARN_ON(gi->composite.gadget_driver.udc_name);
  372. list_for_each_entry(f, &cfg->func_list, list) {
  373. if (f->fi == fi) {
  374. list_del(&f->list);
  375. usb_put_function(f);
  376. mutex_unlock(&gi->lock);
  377. return;
  378. }
  379. }
  380. mutex_unlock(&gi->lock);
  381. WARN(1, "Unable to locate function to unbind\n");
  382. }
  383. static struct configfs_item_operations gadget_config_item_ops = {
  384. .release = gadget_config_attr_release,
  385. .allow_link = config_usb_cfg_link,
  386. .drop_link = config_usb_cfg_unlink,
  387. };
  388. static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
  389. char *page)
  390. {
  391. return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
  392. }
  393. static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
  394. const char *page, size_t len)
  395. {
  396. u16 val;
  397. int ret;
  398. ret = kstrtou16(page, 0, &val);
  399. if (ret)
  400. return ret;
  401. if (DIV_ROUND_UP(val, 8) > 0xff)
  402. return -ERANGE;
  403. to_config_usb_cfg(item)->c.MaxPower = val;
  404. return len;
  405. }
  406. static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
  407. char *page)
  408. {
  409. return sprintf(page, "0x%02x\n",
  410. to_config_usb_cfg(item)->c.bmAttributes);
  411. }
  412. static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
  413. const char *page, size_t len)
  414. {
  415. u8 val;
  416. int ret;
  417. ret = kstrtou8(page, 0, &val);
  418. if (ret)
  419. return ret;
  420. if (!(val & USB_CONFIG_ATT_ONE))
  421. return -EINVAL;
  422. if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
  423. USB_CONFIG_ATT_WAKEUP))
  424. return -EINVAL;
  425. to_config_usb_cfg(item)->c.bmAttributes = val;
  426. return len;
  427. }
  428. CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
  429. CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
  430. static struct configfs_attribute *gadget_config_attrs[] = {
  431. &gadget_config_desc_attr_MaxPower,
  432. &gadget_config_desc_attr_bmAttributes,
  433. NULL,
  434. };
  435. static struct config_item_type gadget_config_type = {
  436. .ct_item_ops = &gadget_config_item_ops,
  437. .ct_attrs = gadget_config_attrs,
  438. .ct_owner = THIS_MODULE,
  439. };
  440. static struct config_item_type gadget_root_type = {
  441. .ct_item_ops = &gadget_root_item_ops,
  442. .ct_attrs = gadget_root_attrs,
  443. .ct_owner = THIS_MODULE,
  444. };
  445. static void composite_init_dev(struct usb_composite_dev *cdev)
  446. {
  447. spin_lock_init(&cdev->lock);
  448. INIT_LIST_HEAD(&cdev->configs);
  449. INIT_LIST_HEAD(&cdev->gstrings);
  450. }
  451. static struct config_group *function_make(
  452. struct config_group *group,
  453. const char *name)
  454. {
  455. struct gadget_info *gi;
  456. struct usb_function_instance *fi;
  457. char buf[MAX_NAME_LEN];
  458. char *func_name;
  459. char *instance_name;
  460. int ret;
  461. ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
  462. if (ret >= MAX_NAME_LEN)
  463. return ERR_PTR(-ENAMETOOLONG);
  464. func_name = buf;
  465. instance_name = strchr(func_name, '.');
  466. if (!instance_name) {
  467. pr_err("Unable to locate . in FUNC.INSTANCE\n");
  468. return ERR_PTR(-EINVAL);
  469. }
  470. *instance_name = '\0';
  471. instance_name++;
  472. fi = usb_get_function_instance(func_name);
  473. if (IS_ERR(fi))
  474. return ERR_CAST(fi);
  475. ret = config_item_set_name(&fi->group.cg_item, "%s", name);
  476. if (ret) {
  477. usb_put_function_instance(fi);
  478. return ERR_PTR(ret);
  479. }
  480. if (fi->set_inst_name) {
  481. ret = fi->set_inst_name(fi, instance_name);
  482. if (ret) {
  483. usb_put_function_instance(fi);
  484. return ERR_PTR(ret);
  485. }
  486. }
  487. gi = container_of(group, struct gadget_info, functions_group);
  488. mutex_lock(&gi->lock);
  489. list_add_tail(&fi->cfs_list, &gi->available_func);
  490. mutex_unlock(&gi->lock);
  491. return &fi->group;
  492. }
  493. static void function_drop(
  494. struct config_group *group,
  495. struct config_item *item)
  496. {
  497. struct usb_function_instance *fi = to_usb_function_instance(item);
  498. struct gadget_info *gi;
  499. gi = container_of(group, struct gadget_info, functions_group);
  500. mutex_lock(&gi->lock);
  501. list_del(&fi->cfs_list);
  502. mutex_unlock(&gi->lock);
  503. config_item_put(item);
  504. }
  505. static struct configfs_group_operations functions_ops = {
  506. .make_group = &function_make,
  507. .drop_item = &function_drop,
  508. };
  509. static struct config_item_type functions_type = {
  510. .ct_group_ops = &functions_ops,
  511. .ct_owner = THIS_MODULE,
  512. };
  513. GS_STRINGS_RW(gadget_config_name, configuration);
  514. static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
  515. &gadget_config_name_attr_configuration,
  516. NULL,
  517. };
  518. static void gadget_config_name_attr_release(struct config_item *item)
  519. {
  520. struct gadget_config_name *cn = to_gadget_config_name(item);
  521. kfree(cn->configuration);
  522. list_del(&cn->list);
  523. kfree(cn);
  524. }
  525. USB_CONFIG_STRING_RW_OPS(gadget_config_name);
  526. USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
  527. static struct config_group *config_desc_make(
  528. struct config_group *group,
  529. const char *name)
  530. {
  531. struct gadget_info *gi;
  532. struct config_usb_cfg *cfg;
  533. char buf[MAX_NAME_LEN];
  534. char *num_str;
  535. u8 num;
  536. int ret;
  537. gi = container_of(group, struct gadget_info, configs_group);
  538. ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
  539. if (ret >= MAX_NAME_LEN)
  540. return ERR_PTR(-ENAMETOOLONG);
  541. num_str = strchr(buf, '.');
  542. if (!num_str) {
  543. pr_err("Unable to locate . in name.bConfigurationValue\n");
  544. return ERR_PTR(-EINVAL);
  545. }
  546. *num_str = '\0';
  547. num_str++;
  548. if (!strlen(buf))
  549. return ERR_PTR(-EINVAL);
  550. ret = kstrtou8(num_str, 0, &num);
  551. if (ret)
  552. return ERR_PTR(ret);
  553. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  554. if (!cfg)
  555. return ERR_PTR(-ENOMEM);
  556. cfg->c.label = kstrdup(buf, GFP_KERNEL);
  557. if (!cfg->c.label) {
  558. ret = -ENOMEM;
  559. goto err;
  560. }
  561. cfg->c.bConfigurationValue = num;
  562. cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
  563. cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
  564. INIT_LIST_HEAD(&cfg->string_list);
  565. INIT_LIST_HEAD(&cfg->func_list);
  566. config_group_init_type_name(&cfg->group, name,
  567. &gadget_config_type);
  568. config_group_init_type_name(&cfg->strings_group, "strings",
  569. &gadget_config_name_strings_type);
  570. configfs_add_default_group(&cfg->strings_group, &cfg->group);
  571. ret = usb_add_config_only(&gi->cdev, &cfg->c);
  572. if (ret)
  573. goto err;
  574. return &cfg->group;
  575. err:
  576. kfree(cfg->c.label);
  577. kfree(cfg);
  578. return ERR_PTR(ret);
  579. }
  580. static void config_desc_drop(
  581. struct config_group *group,
  582. struct config_item *item)
  583. {
  584. config_item_put(item);
  585. }
  586. static struct configfs_group_operations config_desc_ops = {
  587. .make_group = &config_desc_make,
  588. .drop_item = &config_desc_drop,
  589. };
  590. static struct config_item_type config_desc_type = {
  591. .ct_group_ops = &config_desc_ops,
  592. .ct_owner = THIS_MODULE,
  593. };
  594. GS_STRINGS_RW(gadget_strings, manufacturer);
  595. GS_STRINGS_RW(gadget_strings, product);
  596. GS_STRINGS_RW(gadget_strings, serialnumber);
  597. static struct configfs_attribute *gadget_strings_langid_attrs[] = {
  598. &gadget_strings_attr_manufacturer,
  599. &gadget_strings_attr_product,
  600. &gadget_strings_attr_serialnumber,
  601. NULL,
  602. };
  603. static void gadget_strings_attr_release(struct config_item *item)
  604. {
  605. struct gadget_strings *gs = to_gadget_strings(item);
  606. kfree(gs->manufacturer);
  607. kfree(gs->product);
  608. kfree(gs->serialnumber);
  609. list_del(&gs->list);
  610. kfree(gs);
  611. }
  612. USB_CONFIG_STRING_RW_OPS(gadget_strings);
  613. USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
  614. static inline struct os_desc *to_os_desc(struct config_item *item)
  615. {
  616. return container_of(to_config_group(item), struct os_desc, group);
  617. }
  618. static inline struct gadget_info *os_desc_item_to_gadget_info(
  619. struct config_item *item)
  620. {
  621. return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
  622. }
  623. static ssize_t os_desc_use_show(struct config_item *item, char *page)
  624. {
  625. return sprintf(page, "%d",
  626. os_desc_item_to_gadget_info(item)->use_os_desc);
  627. }
  628. static ssize_t os_desc_use_store(struct config_item *item, const char *page,
  629. size_t len)
  630. {
  631. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  632. int ret;
  633. bool use;
  634. mutex_lock(&gi->lock);
  635. ret = strtobool(page, &use);
  636. if (!ret) {
  637. gi->use_os_desc = use;
  638. ret = len;
  639. }
  640. mutex_unlock(&gi->lock);
  641. return ret;
  642. }
  643. static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
  644. {
  645. return sprintf(page, "%d",
  646. os_desc_item_to_gadget_info(item)->b_vendor_code);
  647. }
  648. static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
  649. const char *page, size_t len)
  650. {
  651. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  652. int ret;
  653. u8 b_vendor_code;
  654. mutex_lock(&gi->lock);
  655. ret = kstrtou8(page, 0, &b_vendor_code);
  656. if (!ret) {
  657. gi->b_vendor_code = b_vendor_code;
  658. ret = len;
  659. }
  660. mutex_unlock(&gi->lock);
  661. return ret;
  662. }
  663. static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
  664. {
  665. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  666. memcpy(page, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
  667. return OS_STRING_QW_SIGN_LEN;
  668. }
  669. static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
  670. size_t len)
  671. {
  672. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  673. int res, l;
  674. l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
  675. if (page[l - 1] == '\n')
  676. --l;
  677. mutex_lock(&gi->lock);
  678. res = utf8s_to_utf16s(page, l,
  679. UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
  680. OS_STRING_QW_SIGN_LEN);
  681. if (res > 0)
  682. res = len;
  683. mutex_unlock(&gi->lock);
  684. return res;
  685. }
  686. CONFIGFS_ATTR(os_desc_, use);
  687. CONFIGFS_ATTR(os_desc_, b_vendor_code);
  688. CONFIGFS_ATTR(os_desc_, qw_sign);
  689. static struct configfs_attribute *os_desc_attrs[] = {
  690. &os_desc_attr_use,
  691. &os_desc_attr_b_vendor_code,
  692. &os_desc_attr_qw_sign,
  693. NULL,
  694. };
  695. static void os_desc_attr_release(struct config_item *item)
  696. {
  697. struct os_desc *os_desc = to_os_desc(item);
  698. kfree(os_desc);
  699. }
  700. static int os_desc_link(struct config_item *os_desc_ci,
  701. struct config_item *usb_cfg_ci)
  702. {
  703. struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
  704. struct gadget_info, os_desc_group);
  705. struct usb_composite_dev *cdev = &gi->cdev;
  706. struct config_usb_cfg *c_target =
  707. container_of(to_config_group(usb_cfg_ci),
  708. struct config_usb_cfg, group);
  709. struct usb_configuration *c;
  710. int ret;
  711. mutex_lock(&gi->lock);
  712. list_for_each_entry(c, &cdev->configs, list) {
  713. if (c == &c_target->c)
  714. break;
  715. }
  716. if (c != &c_target->c) {
  717. ret = -EINVAL;
  718. goto out;
  719. }
  720. if (cdev->os_desc_config) {
  721. ret = -EBUSY;
  722. goto out;
  723. }
  724. cdev->os_desc_config = &c_target->c;
  725. ret = 0;
  726. out:
  727. mutex_unlock(&gi->lock);
  728. return ret;
  729. }
  730. static void os_desc_unlink(struct config_item *os_desc_ci,
  731. struct config_item *usb_cfg_ci)
  732. {
  733. struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
  734. struct gadget_info, os_desc_group);
  735. struct usb_composite_dev *cdev = &gi->cdev;
  736. mutex_lock(&gi->lock);
  737. if (gi->composite.gadget_driver.udc_name)
  738. unregister_gadget(gi);
  739. cdev->os_desc_config = NULL;
  740. WARN_ON(gi->composite.gadget_driver.udc_name);
  741. mutex_unlock(&gi->lock);
  742. }
  743. static struct configfs_item_operations os_desc_ops = {
  744. .release = os_desc_attr_release,
  745. .allow_link = os_desc_link,
  746. .drop_link = os_desc_unlink,
  747. };
  748. static struct config_item_type os_desc_type = {
  749. .ct_item_ops = &os_desc_ops,
  750. .ct_attrs = os_desc_attrs,
  751. .ct_owner = THIS_MODULE,
  752. };
  753. static inline struct usb_os_desc_ext_prop
  754. *to_usb_os_desc_ext_prop(struct config_item *item)
  755. {
  756. return container_of(item, struct usb_os_desc_ext_prop, item);
  757. }
  758. static ssize_t ext_prop_type_show(struct config_item *item, char *page)
  759. {
  760. return sprintf(page, "%d", to_usb_os_desc_ext_prop(item)->type);
  761. }
  762. static ssize_t ext_prop_type_store(struct config_item *item,
  763. const char *page, size_t len)
  764. {
  765. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  766. struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
  767. u8 type;
  768. int ret;
  769. if (desc->opts_mutex)
  770. mutex_lock(desc->opts_mutex);
  771. ret = kstrtou8(page, 0, &type);
  772. if (ret)
  773. goto end;
  774. if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
  775. ret = -EINVAL;
  776. goto end;
  777. }
  778. if ((ext_prop->type == USB_EXT_PROP_BINARY ||
  779. ext_prop->type == USB_EXT_PROP_LE32 ||
  780. ext_prop->type == USB_EXT_PROP_BE32) &&
  781. (type == USB_EXT_PROP_UNICODE ||
  782. type == USB_EXT_PROP_UNICODE_ENV ||
  783. type == USB_EXT_PROP_UNICODE_LINK))
  784. ext_prop->data_len <<= 1;
  785. else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
  786. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  787. ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
  788. (type == USB_EXT_PROP_BINARY ||
  789. type == USB_EXT_PROP_LE32 ||
  790. type == USB_EXT_PROP_BE32))
  791. ext_prop->data_len >>= 1;
  792. ext_prop->type = type;
  793. ret = len;
  794. end:
  795. if (desc->opts_mutex)
  796. mutex_unlock(desc->opts_mutex);
  797. return ret;
  798. }
  799. static ssize_t ext_prop_data_show(struct config_item *item, char *page)
  800. {
  801. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  802. int len = ext_prop->data_len;
  803. if (ext_prop->type == USB_EXT_PROP_UNICODE ||
  804. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  805. ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
  806. len >>= 1;
  807. memcpy(page, ext_prop->data, len);
  808. return len;
  809. }
  810. static ssize_t ext_prop_data_store(struct config_item *item,
  811. const char *page, size_t len)
  812. {
  813. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  814. struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
  815. char *new_data;
  816. size_t ret_len = len;
  817. if (page[len - 1] == '\n' || page[len - 1] == '\0')
  818. --len;
  819. new_data = kmemdup(page, len, GFP_KERNEL);
  820. if (!new_data)
  821. return -ENOMEM;
  822. if (desc->opts_mutex)
  823. mutex_lock(desc->opts_mutex);
  824. kfree(ext_prop->data);
  825. ext_prop->data = new_data;
  826. desc->ext_prop_len -= ext_prop->data_len;
  827. ext_prop->data_len = len;
  828. desc->ext_prop_len += ext_prop->data_len;
  829. if (ext_prop->type == USB_EXT_PROP_UNICODE ||
  830. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  831. ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
  832. desc->ext_prop_len -= ext_prop->data_len;
  833. ext_prop->data_len <<= 1;
  834. ext_prop->data_len += 2;
  835. desc->ext_prop_len += ext_prop->data_len;
  836. }
  837. if (desc->opts_mutex)
  838. mutex_unlock(desc->opts_mutex);
  839. return ret_len;
  840. }
  841. CONFIGFS_ATTR(ext_prop_, type);
  842. CONFIGFS_ATTR(ext_prop_, data);
  843. static struct configfs_attribute *ext_prop_attrs[] = {
  844. &ext_prop_attr_type,
  845. &ext_prop_attr_data,
  846. NULL,
  847. };
  848. static void usb_os_desc_ext_prop_release(struct config_item *item)
  849. {
  850. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  851. kfree(ext_prop); /* frees a whole chunk */
  852. }
  853. static struct configfs_item_operations ext_prop_ops = {
  854. .release = usb_os_desc_ext_prop_release,
  855. };
  856. static struct config_item *ext_prop_make(
  857. struct config_group *group,
  858. const char *name)
  859. {
  860. struct usb_os_desc_ext_prop *ext_prop;
  861. struct config_item_type *ext_prop_type;
  862. struct usb_os_desc *desc;
  863. char *vlabuf;
  864. vla_group(data_chunk);
  865. vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
  866. vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
  867. vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
  868. if (!vlabuf)
  869. return ERR_PTR(-ENOMEM);
  870. ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
  871. ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
  872. desc = container_of(group, struct usb_os_desc, group);
  873. ext_prop_type->ct_item_ops = &ext_prop_ops;
  874. ext_prop_type->ct_attrs = ext_prop_attrs;
  875. ext_prop_type->ct_owner = desc->owner;
  876. config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
  877. ext_prop->name = kstrdup(name, GFP_KERNEL);
  878. if (!ext_prop->name) {
  879. kfree(vlabuf);
  880. return ERR_PTR(-ENOMEM);
  881. }
  882. desc->ext_prop_len += 14;
  883. ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
  884. if (desc->opts_mutex)
  885. mutex_lock(desc->opts_mutex);
  886. desc->ext_prop_len += ext_prop->name_len;
  887. list_add_tail(&ext_prop->entry, &desc->ext_prop);
  888. ++desc->ext_prop_count;
  889. if (desc->opts_mutex)
  890. mutex_unlock(desc->opts_mutex);
  891. return &ext_prop->item;
  892. }
  893. static void ext_prop_drop(struct config_group *group, struct config_item *item)
  894. {
  895. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  896. struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
  897. if (desc->opts_mutex)
  898. mutex_lock(desc->opts_mutex);
  899. list_del(&ext_prop->entry);
  900. --desc->ext_prop_count;
  901. kfree(ext_prop->name);
  902. desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
  903. if (desc->opts_mutex)
  904. mutex_unlock(desc->opts_mutex);
  905. config_item_put(item);
  906. }
  907. static struct configfs_group_operations interf_grp_ops = {
  908. .make_item = &ext_prop_make,
  909. .drop_item = &ext_prop_drop,
  910. };
  911. static ssize_t interf_grp_compatible_id_show(struct config_item *item,
  912. char *page)
  913. {
  914. memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
  915. return 8;
  916. }
  917. static ssize_t interf_grp_compatible_id_store(struct config_item *item,
  918. const char *page, size_t len)
  919. {
  920. struct usb_os_desc *desc = to_usb_os_desc(item);
  921. int l;
  922. l = min_t(int, 8, len);
  923. if (page[l - 1] == '\n')
  924. --l;
  925. if (desc->opts_mutex)
  926. mutex_lock(desc->opts_mutex);
  927. memcpy(desc->ext_compat_id, page, l);
  928. if (desc->opts_mutex)
  929. mutex_unlock(desc->opts_mutex);
  930. return len;
  931. }
  932. static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
  933. char *page)
  934. {
  935. memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
  936. return 8;
  937. }
  938. static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
  939. const char *page, size_t len)
  940. {
  941. struct usb_os_desc *desc = to_usb_os_desc(item);
  942. int l;
  943. l = min_t(int, 8, len);
  944. if (page[l - 1] == '\n')
  945. --l;
  946. if (desc->opts_mutex)
  947. mutex_lock(desc->opts_mutex);
  948. memcpy(desc->ext_compat_id + 8, page, l);
  949. if (desc->opts_mutex)
  950. mutex_unlock(desc->opts_mutex);
  951. return len;
  952. }
  953. CONFIGFS_ATTR(interf_grp_, compatible_id);
  954. CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
  955. static struct configfs_attribute *interf_grp_attrs[] = {
  956. &interf_grp_attr_compatible_id,
  957. &interf_grp_attr_sub_compatible_id,
  958. NULL
  959. };
  960. int usb_os_desc_prepare_interf_dir(struct config_group *parent,
  961. int n_interf,
  962. struct usb_os_desc **desc,
  963. char **names,
  964. struct module *owner)
  965. {
  966. struct config_group *os_desc_group;
  967. struct config_item_type *os_desc_type, *interface_type;
  968. vla_group(data_chunk);
  969. vla_item(data_chunk, struct config_group, os_desc_group, 1);
  970. vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
  971. vla_item(data_chunk, struct config_item_type, interface_type, 1);
  972. char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
  973. if (!vlabuf)
  974. return -ENOMEM;
  975. os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
  976. os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
  977. interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
  978. os_desc_type->ct_owner = owner;
  979. config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
  980. configfs_add_default_group(os_desc_group, parent);
  981. interface_type->ct_group_ops = &interf_grp_ops;
  982. interface_type->ct_attrs = interf_grp_attrs;
  983. interface_type->ct_owner = owner;
  984. while (n_interf--) {
  985. struct usb_os_desc *d;
  986. d = desc[n_interf];
  987. d->owner = owner;
  988. config_group_init_type_name(&d->group, "", interface_type);
  989. config_item_set_name(&d->group.cg_item, "interface.%s",
  990. names[n_interf]);
  991. configfs_add_default_group(&d->group, os_desc_group);
  992. }
  993. return 0;
  994. }
  995. EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
  996. static int configfs_do_nothing(struct usb_composite_dev *cdev)
  997. {
  998. WARN_ON(1);
  999. return -EINVAL;
  1000. }
  1001. int composite_dev_prepare(struct usb_composite_driver *composite,
  1002. struct usb_composite_dev *dev);
  1003. int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
  1004. struct usb_ep *ep0);
  1005. static void purge_configs_funcs(struct gadget_info *gi)
  1006. {
  1007. struct usb_configuration *c;
  1008. list_for_each_entry(c, &gi->cdev.configs, list) {
  1009. struct usb_function *f, *tmp;
  1010. struct config_usb_cfg *cfg;
  1011. cfg = container_of(c, struct config_usb_cfg, c);
  1012. list_for_each_entry_safe(f, tmp, &c->functions, list) {
  1013. list_move_tail(&f->list, &cfg->func_list);
  1014. if (f->unbind) {
  1015. dev_dbg(&gi->cdev.gadget->dev,
  1016. "unbind function '%s'/%p\n",
  1017. f->name, f);
  1018. f->unbind(c, f);
  1019. }
  1020. }
  1021. c->next_interface_id = 0;
  1022. memset(c->interface, 0, sizeof(c->interface));
  1023. c->superspeed_plus = 0;
  1024. c->superspeed = 0;
  1025. c->highspeed = 0;
  1026. c->fullspeed = 0;
  1027. }
  1028. }
  1029. static int configfs_composite_bind(struct usb_gadget *gadget,
  1030. struct usb_gadget_driver *gdriver)
  1031. {
  1032. struct usb_composite_driver *composite = to_cdriver(gdriver);
  1033. struct gadget_info *gi = container_of(composite,
  1034. struct gadget_info, composite);
  1035. struct usb_composite_dev *cdev = &gi->cdev;
  1036. struct usb_configuration *c;
  1037. struct usb_string *s;
  1038. unsigned i;
  1039. int ret;
  1040. /* the gi->lock is hold by the caller */
  1041. cdev->gadget = gadget;
  1042. set_gadget_data(gadget, cdev);
  1043. ret = composite_dev_prepare(composite, cdev);
  1044. if (ret)
  1045. return ret;
  1046. /* and now the gadget bind */
  1047. ret = -EINVAL;
  1048. if (list_empty(&gi->cdev.configs)) {
  1049. pr_err("Need at least one configuration in %s.\n",
  1050. gi->composite.name);
  1051. goto err_comp_cleanup;
  1052. }
  1053. list_for_each_entry(c, &gi->cdev.configs, list) {
  1054. struct config_usb_cfg *cfg;
  1055. cfg = container_of(c, struct config_usb_cfg, c);
  1056. if (list_empty(&cfg->func_list)) {
  1057. pr_err("Config %s/%d of %s needs at least one function.\n",
  1058. c->label, c->bConfigurationValue,
  1059. gi->composite.name);
  1060. goto err_comp_cleanup;
  1061. }
  1062. }
  1063. /* init all strings */
  1064. if (!list_empty(&gi->string_list)) {
  1065. struct gadget_strings *gs;
  1066. i = 0;
  1067. list_for_each_entry(gs, &gi->string_list, list) {
  1068. gi->gstrings[i] = &gs->stringtab_dev;
  1069. gs->stringtab_dev.strings = gs->strings;
  1070. gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
  1071. gs->manufacturer;
  1072. gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
  1073. gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
  1074. i++;
  1075. }
  1076. gi->gstrings[i] = NULL;
  1077. s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
  1078. USB_GADGET_FIRST_AVAIL_IDX);
  1079. if (IS_ERR(s)) {
  1080. ret = PTR_ERR(s);
  1081. goto err_comp_cleanup;
  1082. }
  1083. gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
  1084. gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
  1085. gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
  1086. }
  1087. if (gi->use_os_desc) {
  1088. cdev->use_os_string = true;
  1089. cdev->b_vendor_code = gi->b_vendor_code;
  1090. memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
  1091. }
  1092. if (gadget_is_otg(gadget) && !otg_desc[0]) {
  1093. struct usb_descriptor_header *usb_desc;
  1094. usb_desc = usb_otg_descriptor_alloc(gadget);
  1095. if (!usb_desc) {
  1096. ret = -ENOMEM;
  1097. goto err_comp_cleanup;
  1098. }
  1099. usb_otg_descriptor_init(gadget, usb_desc);
  1100. otg_desc[0] = usb_desc;
  1101. otg_desc[1] = NULL;
  1102. }
  1103. /* Go through all configs, attach all functions */
  1104. list_for_each_entry(c, &gi->cdev.configs, list) {
  1105. struct config_usb_cfg *cfg;
  1106. struct usb_function *f;
  1107. struct usb_function *tmp;
  1108. struct gadget_config_name *cn;
  1109. if (gadget_is_otg(gadget))
  1110. c->descriptors = otg_desc;
  1111. cfg = container_of(c, struct config_usb_cfg, c);
  1112. if (!list_empty(&cfg->string_list)) {
  1113. i = 0;
  1114. list_for_each_entry(cn, &cfg->string_list, list) {
  1115. cfg->gstrings[i] = &cn->stringtab_dev;
  1116. cn->stringtab_dev.strings = &cn->strings;
  1117. cn->strings.s = cn->configuration;
  1118. i++;
  1119. }
  1120. cfg->gstrings[i] = NULL;
  1121. s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
  1122. if (IS_ERR(s)) {
  1123. ret = PTR_ERR(s);
  1124. goto err_comp_cleanup;
  1125. }
  1126. c->iConfiguration = s[0].id;
  1127. }
  1128. list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
  1129. list_del(&f->list);
  1130. ret = usb_add_function(c, f);
  1131. if (ret) {
  1132. list_add(&f->list, &cfg->func_list);
  1133. goto err_purge_funcs;
  1134. }
  1135. }
  1136. usb_ep_autoconfig_reset(cdev->gadget);
  1137. }
  1138. if (cdev->use_os_string) {
  1139. ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
  1140. if (ret)
  1141. goto err_purge_funcs;
  1142. }
  1143. usb_ep_autoconfig_reset(cdev->gadget);
  1144. return 0;
  1145. err_purge_funcs:
  1146. purge_configs_funcs(gi);
  1147. err_comp_cleanup:
  1148. composite_dev_cleanup(cdev);
  1149. return ret;
  1150. }
  1151. static void configfs_composite_unbind(struct usb_gadget *gadget)
  1152. {
  1153. struct usb_composite_dev *cdev;
  1154. struct gadget_info *gi;
  1155. /* the gi->lock is hold by the caller */
  1156. cdev = get_gadget_data(gadget);
  1157. gi = container_of(cdev, struct gadget_info, cdev);
  1158. kfree(otg_desc[0]);
  1159. otg_desc[0] = NULL;
  1160. purge_configs_funcs(gi);
  1161. composite_dev_cleanup(cdev);
  1162. usb_ep_autoconfig_reset(cdev->gadget);
  1163. cdev->gadget = NULL;
  1164. set_gadget_data(gadget, NULL);
  1165. }
  1166. static const struct usb_gadget_driver configfs_driver_template = {
  1167. .bind = configfs_composite_bind,
  1168. .unbind = configfs_composite_unbind,
  1169. .setup = composite_setup,
  1170. .reset = composite_disconnect,
  1171. .disconnect = composite_disconnect,
  1172. .suspend = composite_suspend,
  1173. .resume = composite_resume,
  1174. .max_speed = USB_SPEED_SUPER,
  1175. .driver = {
  1176. .owner = THIS_MODULE,
  1177. .name = "configfs-gadget",
  1178. },
  1179. .match_existing_only = 1,
  1180. };
  1181. static struct config_group *gadgets_make(
  1182. struct config_group *group,
  1183. const char *name)
  1184. {
  1185. struct gadget_info *gi;
  1186. gi = kzalloc(sizeof(*gi), GFP_KERNEL);
  1187. if (!gi)
  1188. return ERR_PTR(-ENOMEM);
  1189. config_group_init_type_name(&gi->group, name, &gadget_root_type);
  1190. config_group_init_type_name(&gi->functions_group, "functions",
  1191. &functions_type);
  1192. configfs_add_default_group(&gi->functions_group, &gi->group);
  1193. config_group_init_type_name(&gi->configs_group, "configs",
  1194. &config_desc_type);
  1195. configfs_add_default_group(&gi->configs_group, &gi->group);
  1196. config_group_init_type_name(&gi->strings_group, "strings",
  1197. &gadget_strings_strings_type);
  1198. configfs_add_default_group(&gi->strings_group, &gi->group);
  1199. config_group_init_type_name(&gi->os_desc_group, "os_desc",
  1200. &os_desc_type);
  1201. configfs_add_default_group(&gi->os_desc_group, &gi->group);
  1202. gi->composite.bind = configfs_do_nothing;
  1203. gi->composite.unbind = configfs_do_nothing;
  1204. gi->composite.suspend = NULL;
  1205. gi->composite.resume = NULL;
  1206. gi->composite.max_speed = USB_SPEED_SUPER;
  1207. mutex_init(&gi->lock);
  1208. INIT_LIST_HEAD(&gi->string_list);
  1209. INIT_LIST_HEAD(&gi->available_func);
  1210. composite_init_dev(&gi->cdev);
  1211. gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
  1212. gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
  1213. gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
  1214. gi->composite.gadget_driver = configfs_driver_template;
  1215. gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
  1216. gi->composite.name = gi->composite.gadget_driver.function;
  1217. if (!gi->composite.gadget_driver.function)
  1218. goto err;
  1219. return &gi->group;
  1220. err:
  1221. kfree(gi);
  1222. return ERR_PTR(-ENOMEM);
  1223. }
  1224. static void gadgets_drop(struct config_group *group, struct config_item *item)
  1225. {
  1226. config_item_put(item);
  1227. }
  1228. static struct configfs_group_operations gadgets_ops = {
  1229. .make_group = &gadgets_make,
  1230. .drop_item = &gadgets_drop,
  1231. };
  1232. static struct config_item_type gadgets_type = {
  1233. .ct_group_ops = &gadgets_ops,
  1234. .ct_owner = THIS_MODULE,
  1235. };
  1236. static struct configfs_subsystem gadget_subsys = {
  1237. .su_group = {
  1238. .cg_item = {
  1239. .ci_namebuf = "usb_gadget",
  1240. .ci_type = &gadgets_type,
  1241. },
  1242. },
  1243. .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
  1244. };
  1245. void unregister_gadget_item(struct config_item *item)
  1246. {
  1247. struct gadget_info *gi = to_gadget_info(item);
  1248. mutex_lock(&gi->lock);
  1249. unregister_gadget(gi);
  1250. mutex_unlock(&gi->lock);
  1251. }
  1252. EXPORT_SYMBOL_GPL(unregister_gadget_item);
  1253. static int __init gadget_cfs_init(void)
  1254. {
  1255. int ret;
  1256. config_group_init(&gadget_subsys.su_group);
  1257. ret = configfs_register_subsystem(&gadget_subsys);
  1258. return ret;
  1259. }
  1260. module_init(gadget_cfs_init);
  1261. static void __exit gadget_cfs_exit(void)
  1262. {
  1263. configfs_unregister_subsystem(&gadget_subsys);
  1264. }
  1265. module_exit(gadget_cfs_exit);