configfs.c 39 KB

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