configfs.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  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 int 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 0;
  378. }
  379. }
  380. mutex_unlock(&gi->lock);
  381. WARN(1, "Unable to locate function to unbind\n");
  382. return 0;
  383. }
  384. static struct configfs_item_operations gadget_config_item_ops = {
  385. .release = gadget_config_attr_release,
  386. .allow_link = config_usb_cfg_link,
  387. .drop_link = config_usb_cfg_unlink,
  388. };
  389. static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
  390. char *page)
  391. {
  392. return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
  393. }
  394. static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
  395. const char *page, size_t len)
  396. {
  397. u16 val;
  398. int ret;
  399. ret = kstrtou16(page, 0, &val);
  400. if (ret)
  401. return ret;
  402. if (DIV_ROUND_UP(val, 8) > 0xff)
  403. return -ERANGE;
  404. to_config_usb_cfg(item)->c.MaxPower = val;
  405. return len;
  406. }
  407. static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
  408. char *page)
  409. {
  410. return sprintf(page, "0x%02x\n",
  411. to_config_usb_cfg(item)->c.bmAttributes);
  412. }
  413. static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
  414. const char *page, size_t len)
  415. {
  416. u8 val;
  417. int ret;
  418. ret = kstrtou8(page, 0, &val);
  419. if (ret)
  420. return ret;
  421. if (!(val & USB_CONFIG_ATT_ONE))
  422. return -EINVAL;
  423. if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
  424. USB_CONFIG_ATT_WAKEUP))
  425. return -EINVAL;
  426. to_config_usb_cfg(item)->c.bmAttributes = val;
  427. return len;
  428. }
  429. CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
  430. CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
  431. static struct configfs_attribute *gadget_config_attrs[] = {
  432. &gadget_config_desc_attr_MaxPower,
  433. &gadget_config_desc_attr_bmAttributes,
  434. NULL,
  435. };
  436. static struct config_item_type gadget_config_type = {
  437. .ct_item_ops = &gadget_config_item_ops,
  438. .ct_attrs = gadget_config_attrs,
  439. .ct_owner = THIS_MODULE,
  440. };
  441. static struct config_item_type gadget_root_type = {
  442. .ct_item_ops = &gadget_root_item_ops,
  443. .ct_attrs = gadget_root_attrs,
  444. .ct_owner = THIS_MODULE,
  445. };
  446. static void composite_init_dev(struct usb_composite_dev *cdev)
  447. {
  448. spin_lock_init(&cdev->lock);
  449. INIT_LIST_HEAD(&cdev->configs);
  450. INIT_LIST_HEAD(&cdev->gstrings);
  451. }
  452. static struct config_group *function_make(
  453. struct config_group *group,
  454. const char *name)
  455. {
  456. struct gadget_info *gi;
  457. struct usb_function_instance *fi;
  458. char buf[MAX_NAME_LEN];
  459. char *func_name;
  460. char *instance_name;
  461. int ret;
  462. ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
  463. if (ret >= MAX_NAME_LEN)
  464. return ERR_PTR(-ENAMETOOLONG);
  465. func_name = buf;
  466. instance_name = strchr(func_name, '.');
  467. if (!instance_name) {
  468. pr_err("Unable to locate . in FUNC.INSTANCE\n");
  469. return ERR_PTR(-EINVAL);
  470. }
  471. *instance_name = '\0';
  472. instance_name++;
  473. fi = usb_get_function_instance(func_name);
  474. if (IS_ERR(fi))
  475. return ERR_CAST(fi);
  476. ret = config_item_set_name(&fi->group.cg_item, "%s", name);
  477. if (ret) {
  478. usb_put_function_instance(fi);
  479. return ERR_PTR(ret);
  480. }
  481. if (fi->set_inst_name) {
  482. ret = fi->set_inst_name(fi, instance_name);
  483. if (ret) {
  484. usb_put_function_instance(fi);
  485. return ERR_PTR(ret);
  486. }
  487. }
  488. gi = container_of(group, struct gadget_info, functions_group);
  489. mutex_lock(&gi->lock);
  490. list_add_tail(&fi->cfs_list, &gi->available_func);
  491. mutex_unlock(&gi->lock);
  492. return &fi->group;
  493. }
  494. static void function_drop(
  495. struct config_group *group,
  496. struct config_item *item)
  497. {
  498. struct usb_function_instance *fi = to_usb_function_instance(item);
  499. struct gadget_info *gi;
  500. gi = container_of(group, struct gadget_info, functions_group);
  501. mutex_lock(&gi->lock);
  502. list_del(&fi->cfs_list);
  503. mutex_unlock(&gi->lock);
  504. config_item_put(item);
  505. }
  506. static struct configfs_group_operations functions_ops = {
  507. .make_group = &function_make,
  508. .drop_item = &function_drop,
  509. };
  510. static struct config_item_type functions_type = {
  511. .ct_group_ops = &functions_ops,
  512. .ct_owner = THIS_MODULE,
  513. };
  514. GS_STRINGS_RW(gadget_config_name, configuration);
  515. static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
  516. &gadget_config_name_attr_configuration,
  517. NULL,
  518. };
  519. static void gadget_config_name_attr_release(struct config_item *item)
  520. {
  521. struct gadget_config_name *cn = to_gadget_config_name(item);
  522. kfree(cn->configuration);
  523. list_del(&cn->list);
  524. kfree(cn);
  525. }
  526. USB_CONFIG_STRING_RW_OPS(gadget_config_name);
  527. USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
  528. static struct config_group *config_desc_make(
  529. struct config_group *group,
  530. const char *name)
  531. {
  532. struct gadget_info *gi;
  533. struct config_usb_cfg *cfg;
  534. char buf[MAX_NAME_LEN];
  535. char *num_str;
  536. u8 num;
  537. int ret;
  538. gi = container_of(group, struct gadget_info, configs_group);
  539. ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
  540. if (ret >= MAX_NAME_LEN)
  541. return ERR_PTR(-ENAMETOOLONG);
  542. num_str = strchr(buf, '.');
  543. if (!num_str) {
  544. pr_err("Unable to locate . in name.bConfigurationValue\n");
  545. return ERR_PTR(-EINVAL);
  546. }
  547. *num_str = '\0';
  548. num_str++;
  549. if (!strlen(buf))
  550. return ERR_PTR(-EINVAL);
  551. ret = kstrtou8(num_str, 0, &num);
  552. if (ret)
  553. return ERR_PTR(ret);
  554. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  555. if (!cfg)
  556. return ERR_PTR(-ENOMEM);
  557. cfg->c.label = kstrdup(buf, GFP_KERNEL);
  558. if (!cfg->c.label) {
  559. ret = -ENOMEM;
  560. goto err;
  561. }
  562. cfg->c.bConfigurationValue = num;
  563. cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
  564. cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
  565. INIT_LIST_HEAD(&cfg->string_list);
  566. INIT_LIST_HEAD(&cfg->func_list);
  567. config_group_init_type_name(&cfg->group, name,
  568. &gadget_config_type);
  569. config_group_init_type_name(&cfg->strings_group, "strings",
  570. &gadget_config_name_strings_type);
  571. configfs_add_default_group(&cfg->strings_group, &cfg->group);
  572. ret = usb_add_config_only(&gi->cdev, &cfg->c);
  573. if (ret)
  574. goto err;
  575. return &cfg->group;
  576. err:
  577. kfree(cfg->c.label);
  578. kfree(cfg);
  579. return ERR_PTR(ret);
  580. }
  581. static void config_desc_drop(
  582. struct config_group *group,
  583. struct config_item *item)
  584. {
  585. config_item_put(item);
  586. }
  587. static struct configfs_group_operations config_desc_ops = {
  588. .make_group = &config_desc_make,
  589. .drop_item = &config_desc_drop,
  590. };
  591. static struct config_item_type config_desc_type = {
  592. .ct_group_ops = &config_desc_ops,
  593. .ct_owner = THIS_MODULE,
  594. };
  595. GS_STRINGS_RW(gadget_strings, manufacturer);
  596. GS_STRINGS_RW(gadget_strings, product);
  597. GS_STRINGS_RW(gadget_strings, serialnumber);
  598. static struct configfs_attribute *gadget_strings_langid_attrs[] = {
  599. &gadget_strings_attr_manufacturer,
  600. &gadget_strings_attr_product,
  601. &gadget_strings_attr_serialnumber,
  602. NULL,
  603. };
  604. static void gadget_strings_attr_release(struct config_item *item)
  605. {
  606. struct gadget_strings *gs = to_gadget_strings(item);
  607. kfree(gs->manufacturer);
  608. kfree(gs->product);
  609. kfree(gs->serialnumber);
  610. list_del(&gs->list);
  611. kfree(gs);
  612. }
  613. USB_CONFIG_STRING_RW_OPS(gadget_strings);
  614. USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
  615. static inline struct os_desc *to_os_desc(struct config_item *item)
  616. {
  617. return container_of(to_config_group(item), struct os_desc, group);
  618. }
  619. static inline struct gadget_info *os_desc_item_to_gadget_info(
  620. struct config_item *item)
  621. {
  622. return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
  623. }
  624. static ssize_t os_desc_use_show(struct config_item *item, char *page)
  625. {
  626. return sprintf(page, "%d",
  627. os_desc_item_to_gadget_info(item)->use_os_desc);
  628. }
  629. static ssize_t os_desc_use_store(struct config_item *item, const char *page,
  630. size_t len)
  631. {
  632. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  633. int ret;
  634. bool use;
  635. mutex_lock(&gi->lock);
  636. ret = strtobool(page, &use);
  637. if (!ret) {
  638. gi->use_os_desc = use;
  639. ret = len;
  640. }
  641. mutex_unlock(&gi->lock);
  642. return ret;
  643. }
  644. static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
  645. {
  646. return sprintf(page, "%d",
  647. os_desc_item_to_gadget_info(item)->b_vendor_code);
  648. }
  649. static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
  650. const char *page, size_t len)
  651. {
  652. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  653. int ret;
  654. u8 b_vendor_code;
  655. mutex_lock(&gi->lock);
  656. ret = kstrtou8(page, 0, &b_vendor_code);
  657. if (!ret) {
  658. gi->b_vendor_code = b_vendor_code;
  659. ret = len;
  660. }
  661. mutex_unlock(&gi->lock);
  662. return ret;
  663. }
  664. static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
  665. {
  666. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  667. memcpy(page, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
  668. return OS_STRING_QW_SIGN_LEN;
  669. }
  670. static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
  671. size_t len)
  672. {
  673. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  674. int res, l;
  675. l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
  676. if (page[l - 1] == '\n')
  677. --l;
  678. mutex_lock(&gi->lock);
  679. res = utf8s_to_utf16s(page, l,
  680. UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
  681. OS_STRING_QW_SIGN_LEN);
  682. if (res > 0)
  683. res = len;
  684. mutex_unlock(&gi->lock);
  685. return res;
  686. }
  687. CONFIGFS_ATTR(os_desc_, use);
  688. CONFIGFS_ATTR(os_desc_, b_vendor_code);
  689. CONFIGFS_ATTR(os_desc_, qw_sign);
  690. static struct configfs_attribute *os_desc_attrs[] = {
  691. &os_desc_attr_use,
  692. &os_desc_attr_b_vendor_code,
  693. &os_desc_attr_qw_sign,
  694. NULL,
  695. };
  696. static void os_desc_attr_release(struct config_item *item)
  697. {
  698. struct os_desc *os_desc = to_os_desc(item);
  699. kfree(os_desc);
  700. }
  701. static int os_desc_link(struct config_item *os_desc_ci,
  702. struct config_item *usb_cfg_ci)
  703. {
  704. struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
  705. struct gadget_info, os_desc_group);
  706. struct usb_composite_dev *cdev = &gi->cdev;
  707. struct config_usb_cfg *c_target =
  708. container_of(to_config_group(usb_cfg_ci),
  709. struct config_usb_cfg, group);
  710. struct usb_configuration *c;
  711. int ret;
  712. mutex_lock(&gi->lock);
  713. list_for_each_entry(c, &cdev->configs, list) {
  714. if (c == &c_target->c)
  715. break;
  716. }
  717. if (c != &c_target->c) {
  718. ret = -EINVAL;
  719. goto out;
  720. }
  721. if (cdev->os_desc_config) {
  722. ret = -EBUSY;
  723. goto out;
  724. }
  725. cdev->os_desc_config = &c_target->c;
  726. ret = 0;
  727. out:
  728. mutex_unlock(&gi->lock);
  729. return ret;
  730. }
  731. static int os_desc_unlink(struct config_item *os_desc_ci,
  732. struct config_item *usb_cfg_ci)
  733. {
  734. struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
  735. struct gadget_info, os_desc_group);
  736. struct usb_composite_dev *cdev = &gi->cdev;
  737. mutex_lock(&gi->lock);
  738. if (gi->composite.gadget_driver.udc_name)
  739. unregister_gadget(gi);
  740. cdev->os_desc_config = NULL;
  741. WARN_ON(gi->composite.gadget_driver.udc_name);
  742. mutex_unlock(&gi->lock);
  743. return 0;
  744. }
  745. static struct configfs_item_operations os_desc_ops = {
  746. .release = os_desc_attr_release,
  747. .allow_link = os_desc_link,
  748. .drop_link = os_desc_unlink,
  749. };
  750. static struct config_item_type os_desc_type = {
  751. .ct_item_ops = &os_desc_ops,
  752. .ct_attrs = os_desc_attrs,
  753. .ct_owner = THIS_MODULE,
  754. };
  755. static inline struct usb_os_desc_ext_prop
  756. *to_usb_os_desc_ext_prop(struct config_item *item)
  757. {
  758. return container_of(item, struct usb_os_desc_ext_prop, item);
  759. }
  760. static ssize_t ext_prop_type_show(struct config_item *item, char *page)
  761. {
  762. return sprintf(page, "%d", to_usb_os_desc_ext_prop(item)->type);
  763. }
  764. static ssize_t ext_prop_type_store(struct config_item *item,
  765. const char *page, size_t len)
  766. {
  767. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  768. struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
  769. u8 type;
  770. int ret;
  771. if (desc->opts_mutex)
  772. mutex_lock(desc->opts_mutex);
  773. ret = kstrtou8(page, 0, &type);
  774. if (ret)
  775. goto end;
  776. if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
  777. ret = -EINVAL;
  778. goto end;
  779. }
  780. if ((ext_prop->type == USB_EXT_PROP_BINARY ||
  781. ext_prop->type == USB_EXT_PROP_LE32 ||
  782. ext_prop->type == USB_EXT_PROP_BE32) &&
  783. (type == USB_EXT_PROP_UNICODE ||
  784. type == USB_EXT_PROP_UNICODE_ENV ||
  785. type == USB_EXT_PROP_UNICODE_LINK))
  786. ext_prop->data_len <<= 1;
  787. else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
  788. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  789. ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
  790. (type == USB_EXT_PROP_BINARY ||
  791. type == USB_EXT_PROP_LE32 ||
  792. type == USB_EXT_PROP_BE32))
  793. ext_prop->data_len >>= 1;
  794. ext_prop->type = type;
  795. ret = len;
  796. end:
  797. if (desc->opts_mutex)
  798. mutex_unlock(desc->opts_mutex);
  799. return ret;
  800. }
  801. static ssize_t ext_prop_data_show(struct config_item *item, char *page)
  802. {
  803. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  804. int len = ext_prop->data_len;
  805. if (ext_prop->type == USB_EXT_PROP_UNICODE ||
  806. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  807. ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
  808. len >>= 1;
  809. memcpy(page, ext_prop->data, len);
  810. return len;
  811. }
  812. static ssize_t ext_prop_data_store(struct config_item *item,
  813. const char *page, size_t len)
  814. {
  815. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  816. struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
  817. char *new_data;
  818. size_t ret_len = len;
  819. if (page[len - 1] == '\n' || page[len - 1] == '\0')
  820. --len;
  821. new_data = kmemdup(page, len, GFP_KERNEL);
  822. if (!new_data)
  823. return -ENOMEM;
  824. if (desc->opts_mutex)
  825. mutex_lock(desc->opts_mutex);
  826. kfree(ext_prop->data);
  827. ext_prop->data = new_data;
  828. desc->ext_prop_len -= ext_prop->data_len;
  829. ext_prop->data_len = len;
  830. desc->ext_prop_len += ext_prop->data_len;
  831. if (ext_prop->type == USB_EXT_PROP_UNICODE ||
  832. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  833. ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
  834. desc->ext_prop_len -= ext_prop->data_len;
  835. ext_prop->data_len <<= 1;
  836. ext_prop->data_len += 2;
  837. desc->ext_prop_len += ext_prop->data_len;
  838. }
  839. if (desc->opts_mutex)
  840. mutex_unlock(desc->opts_mutex);
  841. return ret_len;
  842. }
  843. CONFIGFS_ATTR(ext_prop_, type);
  844. CONFIGFS_ATTR(ext_prop_, data);
  845. static struct configfs_attribute *ext_prop_attrs[] = {
  846. &ext_prop_attr_type,
  847. &ext_prop_attr_data,
  848. NULL,
  849. };
  850. static void usb_os_desc_ext_prop_release(struct config_item *item)
  851. {
  852. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  853. kfree(ext_prop); /* frees a whole chunk */
  854. }
  855. static struct configfs_item_operations ext_prop_ops = {
  856. .release = usb_os_desc_ext_prop_release,
  857. };
  858. static struct config_item *ext_prop_make(
  859. struct config_group *group,
  860. const char *name)
  861. {
  862. struct usb_os_desc_ext_prop *ext_prop;
  863. struct config_item_type *ext_prop_type;
  864. struct usb_os_desc *desc;
  865. char *vlabuf;
  866. vla_group(data_chunk);
  867. vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
  868. vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
  869. vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
  870. if (!vlabuf)
  871. return ERR_PTR(-ENOMEM);
  872. ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
  873. ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
  874. desc = container_of(group, struct usb_os_desc, group);
  875. ext_prop_type->ct_item_ops = &ext_prop_ops;
  876. ext_prop_type->ct_attrs = ext_prop_attrs;
  877. ext_prop_type->ct_owner = desc->owner;
  878. config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
  879. ext_prop->name = kstrdup(name, GFP_KERNEL);
  880. if (!ext_prop->name) {
  881. kfree(vlabuf);
  882. return ERR_PTR(-ENOMEM);
  883. }
  884. desc->ext_prop_len += 14;
  885. ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
  886. if (desc->opts_mutex)
  887. mutex_lock(desc->opts_mutex);
  888. desc->ext_prop_len += ext_prop->name_len;
  889. list_add_tail(&ext_prop->entry, &desc->ext_prop);
  890. ++desc->ext_prop_count;
  891. if (desc->opts_mutex)
  892. mutex_unlock(desc->opts_mutex);
  893. return &ext_prop->item;
  894. }
  895. static void ext_prop_drop(struct config_group *group, struct config_item *item)
  896. {
  897. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  898. struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
  899. if (desc->opts_mutex)
  900. mutex_lock(desc->opts_mutex);
  901. list_del(&ext_prop->entry);
  902. --desc->ext_prop_count;
  903. kfree(ext_prop->name);
  904. desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
  905. if (desc->opts_mutex)
  906. mutex_unlock(desc->opts_mutex);
  907. config_item_put(item);
  908. }
  909. static struct configfs_group_operations interf_grp_ops = {
  910. .make_item = &ext_prop_make,
  911. .drop_item = &ext_prop_drop,
  912. };
  913. static ssize_t interf_grp_compatible_id_show(struct config_item *item,
  914. char *page)
  915. {
  916. memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
  917. return 8;
  918. }
  919. static ssize_t interf_grp_compatible_id_store(struct config_item *item,
  920. const char *page, size_t len)
  921. {
  922. struct usb_os_desc *desc = to_usb_os_desc(item);
  923. int l;
  924. l = min_t(int, 8, len);
  925. if (page[l - 1] == '\n')
  926. --l;
  927. if (desc->opts_mutex)
  928. mutex_lock(desc->opts_mutex);
  929. memcpy(desc->ext_compat_id, page, l);
  930. if (desc->opts_mutex)
  931. mutex_unlock(desc->opts_mutex);
  932. return len;
  933. }
  934. static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
  935. char *page)
  936. {
  937. memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
  938. return 8;
  939. }
  940. static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
  941. const char *page, size_t len)
  942. {
  943. struct usb_os_desc *desc = to_usb_os_desc(item);
  944. int l;
  945. l = min_t(int, 8, len);
  946. if (page[l - 1] == '\n')
  947. --l;
  948. if (desc->opts_mutex)
  949. mutex_lock(desc->opts_mutex);
  950. memcpy(desc->ext_compat_id + 8, page, l);
  951. if (desc->opts_mutex)
  952. mutex_unlock(desc->opts_mutex);
  953. return len;
  954. }
  955. CONFIGFS_ATTR(interf_grp_, compatible_id);
  956. CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
  957. static struct configfs_attribute *interf_grp_attrs[] = {
  958. &interf_grp_attr_compatible_id,
  959. &interf_grp_attr_sub_compatible_id,
  960. NULL
  961. };
  962. struct config_group *usb_os_desc_prepare_interf_dir(
  963. struct config_group *parent,
  964. int n_interf,
  965. struct usb_os_desc **desc,
  966. char **names,
  967. struct module *owner)
  968. {
  969. struct config_group *os_desc_group;
  970. struct config_item_type *os_desc_type, *interface_type;
  971. vla_group(data_chunk);
  972. vla_item(data_chunk, struct config_group, os_desc_group, 1);
  973. vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
  974. vla_item(data_chunk, struct config_item_type, interface_type, 1);
  975. char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
  976. if (!vlabuf)
  977. return ERR_PTR(-ENOMEM);
  978. os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
  979. os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
  980. interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
  981. os_desc_type->ct_owner = owner;
  982. config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
  983. configfs_add_default_group(os_desc_group, parent);
  984. interface_type->ct_group_ops = &interf_grp_ops;
  985. interface_type->ct_attrs = interf_grp_attrs;
  986. interface_type->ct_owner = owner;
  987. while (n_interf--) {
  988. struct usb_os_desc *d;
  989. d = desc[n_interf];
  990. d->owner = owner;
  991. config_group_init_type_name(&d->group, "", interface_type);
  992. config_item_set_name(&d->group.cg_item, "interface.%s",
  993. names[n_interf]);
  994. configfs_add_default_group(&d->group, os_desc_group);
  995. }
  996. return os_desc_group;
  997. }
  998. EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
  999. static int configfs_do_nothing(struct usb_composite_dev *cdev)
  1000. {
  1001. WARN_ON(1);
  1002. return -EINVAL;
  1003. }
  1004. int composite_dev_prepare(struct usb_composite_driver *composite,
  1005. struct usb_composite_dev *dev);
  1006. int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
  1007. struct usb_ep *ep0);
  1008. static void purge_configs_funcs(struct gadget_info *gi)
  1009. {
  1010. struct usb_configuration *c;
  1011. list_for_each_entry(c, &gi->cdev.configs, list) {
  1012. struct usb_function *f, *tmp;
  1013. struct config_usb_cfg *cfg;
  1014. cfg = container_of(c, struct config_usb_cfg, c);
  1015. list_for_each_entry_safe(f, tmp, &c->functions, list) {
  1016. list_move_tail(&f->list, &cfg->func_list);
  1017. if (f->unbind) {
  1018. dev_dbg(&gi->cdev.gadget->dev,
  1019. "unbind function '%s'/%p\n",
  1020. f->name, f);
  1021. f->unbind(c, f);
  1022. }
  1023. }
  1024. c->next_interface_id = 0;
  1025. memset(c->interface, 0, sizeof(c->interface));
  1026. c->superspeed_plus = 0;
  1027. c->superspeed = 0;
  1028. c->highspeed = 0;
  1029. c->fullspeed = 0;
  1030. }
  1031. }
  1032. static int configfs_composite_bind(struct usb_gadget *gadget,
  1033. struct usb_gadget_driver *gdriver)
  1034. {
  1035. struct usb_composite_driver *composite = to_cdriver(gdriver);
  1036. struct gadget_info *gi = container_of(composite,
  1037. struct gadget_info, composite);
  1038. struct usb_composite_dev *cdev = &gi->cdev;
  1039. struct usb_configuration *c;
  1040. struct usb_string *s;
  1041. unsigned i;
  1042. int ret;
  1043. /* the gi->lock is hold by the caller */
  1044. cdev->gadget = gadget;
  1045. set_gadget_data(gadget, cdev);
  1046. ret = composite_dev_prepare(composite, cdev);
  1047. if (ret)
  1048. return ret;
  1049. /* and now the gadget bind */
  1050. ret = -EINVAL;
  1051. if (list_empty(&gi->cdev.configs)) {
  1052. pr_err("Need at least one configuration in %s.\n",
  1053. gi->composite.name);
  1054. goto err_comp_cleanup;
  1055. }
  1056. list_for_each_entry(c, &gi->cdev.configs, list) {
  1057. struct config_usb_cfg *cfg;
  1058. cfg = container_of(c, struct config_usb_cfg, c);
  1059. if (list_empty(&cfg->func_list)) {
  1060. pr_err("Config %s/%d of %s needs at least one function.\n",
  1061. c->label, c->bConfigurationValue,
  1062. gi->composite.name);
  1063. goto err_comp_cleanup;
  1064. }
  1065. }
  1066. /* init all strings */
  1067. if (!list_empty(&gi->string_list)) {
  1068. struct gadget_strings *gs;
  1069. i = 0;
  1070. list_for_each_entry(gs, &gi->string_list, list) {
  1071. gi->gstrings[i] = &gs->stringtab_dev;
  1072. gs->stringtab_dev.strings = gs->strings;
  1073. gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
  1074. gs->manufacturer;
  1075. gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
  1076. gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
  1077. i++;
  1078. }
  1079. gi->gstrings[i] = NULL;
  1080. s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
  1081. USB_GADGET_FIRST_AVAIL_IDX);
  1082. if (IS_ERR(s)) {
  1083. ret = PTR_ERR(s);
  1084. goto err_comp_cleanup;
  1085. }
  1086. gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
  1087. gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
  1088. gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
  1089. }
  1090. if (gi->use_os_desc) {
  1091. cdev->use_os_string = true;
  1092. cdev->b_vendor_code = gi->b_vendor_code;
  1093. memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
  1094. }
  1095. if (gadget_is_otg(gadget) && !otg_desc[0]) {
  1096. struct usb_descriptor_header *usb_desc;
  1097. usb_desc = usb_otg_descriptor_alloc(gadget);
  1098. if (!usb_desc) {
  1099. ret = -ENOMEM;
  1100. goto err_comp_cleanup;
  1101. }
  1102. usb_otg_descriptor_init(gadget, usb_desc);
  1103. otg_desc[0] = usb_desc;
  1104. otg_desc[1] = NULL;
  1105. }
  1106. /* Go through all configs, attach all functions */
  1107. list_for_each_entry(c, &gi->cdev.configs, list) {
  1108. struct config_usb_cfg *cfg;
  1109. struct usb_function *f;
  1110. struct usb_function *tmp;
  1111. struct gadget_config_name *cn;
  1112. if (gadget_is_otg(gadget))
  1113. c->descriptors = otg_desc;
  1114. cfg = container_of(c, struct config_usb_cfg, c);
  1115. if (!list_empty(&cfg->string_list)) {
  1116. i = 0;
  1117. list_for_each_entry(cn, &cfg->string_list, list) {
  1118. cfg->gstrings[i] = &cn->stringtab_dev;
  1119. cn->stringtab_dev.strings = &cn->strings;
  1120. cn->strings.s = cn->configuration;
  1121. i++;
  1122. }
  1123. cfg->gstrings[i] = NULL;
  1124. s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
  1125. if (IS_ERR(s)) {
  1126. ret = PTR_ERR(s);
  1127. goto err_comp_cleanup;
  1128. }
  1129. c->iConfiguration = s[0].id;
  1130. }
  1131. list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
  1132. list_del(&f->list);
  1133. ret = usb_add_function(c, f);
  1134. if (ret) {
  1135. list_add(&f->list, &cfg->func_list);
  1136. goto err_purge_funcs;
  1137. }
  1138. }
  1139. usb_ep_autoconfig_reset(cdev->gadget);
  1140. }
  1141. if (cdev->use_os_string) {
  1142. ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
  1143. if (ret)
  1144. goto err_purge_funcs;
  1145. }
  1146. usb_ep_autoconfig_reset(cdev->gadget);
  1147. return 0;
  1148. err_purge_funcs:
  1149. purge_configs_funcs(gi);
  1150. err_comp_cleanup:
  1151. composite_dev_cleanup(cdev);
  1152. return ret;
  1153. }
  1154. static void configfs_composite_unbind(struct usb_gadget *gadget)
  1155. {
  1156. struct usb_composite_dev *cdev;
  1157. struct gadget_info *gi;
  1158. /* the gi->lock is hold by the caller */
  1159. cdev = get_gadget_data(gadget);
  1160. gi = container_of(cdev, struct gadget_info, cdev);
  1161. kfree(otg_desc[0]);
  1162. otg_desc[0] = NULL;
  1163. purge_configs_funcs(gi);
  1164. composite_dev_cleanup(cdev);
  1165. usb_ep_autoconfig_reset(cdev->gadget);
  1166. cdev->gadget = NULL;
  1167. set_gadget_data(gadget, NULL);
  1168. }
  1169. static const struct usb_gadget_driver configfs_driver_template = {
  1170. .bind = configfs_composite_bind,
  1171. .unbind = configfs_composite_unbind,
  1172. .setup = composite_setup,
  1173. .reset = composite_disconnect,
  1174. .disconnect = composite_disconnect,
  1175. .suspend = composite_suspend,
  1176. .resume = composite_resume,
  1177. .max_speed = USB_SPEED_SUPER,
  1178. .driver = {
  1179. .owner = THIS_MODULE,
  1180. .name = "configfs-gadget",
  1181. },
  1182. .match_existing_only = 1,
  1183. };
  1184. static struct config_group *gadgets_make(
  1185. struct config_group *group,
  1186. const char *name)
  1187. {
  1188. struct gadget_info *gi;
  1189. gi = kzalloc(sizeof(*gi), GFP_KERNEL);
  1190. if (!gi)
  1191. return ERR_PTR(-ENOMEM);
  1192. config_group_init_type_name(&gi->group, name, &gadget_root_type);
  1193. config_group_init_type_name(&gi->functions_group, "functions",
  1194. &functions_type);
  1195. configfs_add_default_group(&gi->functions_group, &gi->group);
  1196. config_group_init_type_name(&gi->configs_group, "configs",
  1197. &config_desc_type);
  1198. configfs_add_default_group(&gi->configs_group, &gi->group);
  1199. config_group_init_type_name(&gi->strings_group, "strings",
  1200. &gadget_strings_strings_type);
  1201. configfs_add_default_group(&gi->strings_group, &gi->group);
  1202. config_group_init_type_name(&gi->os_desc_group, "os_desc",
  1203. &os_desc_type);
  1204. configfs_add_default_group(&gi->os_desc_group, &gi->group);
  1205. gi->composite.bind = configfs_do_nothing;
  1206. gi->composite.unbind = configfs_do_nothing;
  1207. gi->composite.suspend = NULL;
  1208. gi->composite.resume = NULL;
  1209. gi->composite.max_speed = USB_SPEED_SUPER;
  1210. mutex_init(&gi->lock);
  1211. INIT_LIST_HEAD(&gi->string_list);
  1212. INIT_LIST_HEAD(&gi->available_func);
  1213. composite_init_dev(&gi->cdev);
  1214. gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
  1215. gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
  1216. gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
  1217. gi->composite.gadget_driver = configfs_driver_template;
  1218. gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
  1219. gi->composite.name = gi->composite.gadget_driver.function;
  1220. if (!gi->composite.gadget_driver.function)
  1221. goto err;
  1222. return &gi->group;
  1223. err:
  1224. kfree(gi);
  1225. return ERR_PTR(-ENOMEM);
  1226. }
  1227. static void gadgets_drop(struct config_group *group, struct config_item *item)
  1228. {
  1229. config_item_put(item);
  1230. }
  1231. static struct configfs_group_operations gadgets_ops = {
  1232. .make_group = &gadgets_make,
  1233. .drop_item = &gadgets_drop,
  1234. };
  1235. static struct config_item_type gadgets_type = {
  1236. .ct_group_ops = &gadgets_ops,
  1237. .ct_owner = THIS_MODULE,
  1238. };
  1239. static struct configfs_subsystem gadget_subsys = {
  1240. .su_group = {
  1241. .cg_item = {
  1242. .ci_namebuf = "usb_gadget",
  1243. .ci_type = &gadgets_type,
  1244. },
  1245. },
  1246. .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
  1247. };
  1248. void unregister_gadget_item(struct config_item *item)
  1249. {
  1250. struct gadget_info *gi = to_gadget_info(item);
  1251. mutex_lock(&gi->lock);
  1252. unregister_gadget(gi);
  1253. mutex_unlock(&gi->lock);
  1254. }
  1255. EXPORT_SYMBOL_GPL(unregister_gadget_item);
  1256. static int __init gadget_cfs_init(void)
  1257. {
  1258. int ret;
  1259. config_group_init(&gadget_subsys.su_group);
  1260. ret = configfs_register_subsystem(&gadget_subsys);
  1261. return ret;
  1262. }
  1263. module_init(gadget_cfs_init);
  1264. static void __exit gadget_cfs_exit(void)
  1265. {
  1266. configfs_unregister_subsystem(&gadget_subsys);
  1267. }
  1268. module_exit(gadget_cfs_exit);