configfs.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  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. kfree(name);
  230. } else {
  231. if (gi->composite.gadget_driver.udc_name) {
  232. ret = -EBUSY;
  233. goto err;
  234. }
  235. gi->composite.gadget_driver.udc_name = name;
  236. ret = usb_gadget_probe_driver(&gi->composite.gadget_driver);
  237. if (ret) {
  238. gi->composite.gadget_driver.udc_name = NULL;
  239. goto err;
  240. }
  241. }
  242. mutex_unlock(&gi->lock);
  243. return len;
  244. err:
  245. kfree(name);
  246. mutex_unlock(&gi->lock);
  247. return ret;
  248. }
  249. CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
  250. CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
  251. CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
  252. CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
  253. CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
  254. CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
  255. CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
  256. CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
  257. CONFIGFS_ATTR(gadget_dev_desc_, UDC);
  258. static struct configfs_attribute *gadget_root_attrs[] = {
  259. &gadget_dev_desc_attr_bDeviceClass,
  260. &gadget_dev_desc_attr_bDeviceSubClass,
  261. &gadget_dev_desc_attr_bDeviceProtocol,
  262. &gadget_dev_desc_attr_bMaxPacketSize0,
  263. &gadget_dev_desc_attr_idVendor,
  264. &gadget_dev_desc_attr_idProduct,
  265. &gadget_dev_desc_attr_bcdDevice,
  266. &gadget_dev_desc_attr_bcdUSB,
  267. &gadget_dev_desc_attr_UDC,
  268. NULL,
  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 usb_function_instance *to_usb_function_instance(
  282. struct config_item *item)
  283. {
  284. return container_of(to_config_group(item),
  285. struct usb_function_instance, group);
  286. }
  287. static void gadget_info_attr_release(struct config_item *item)
  288. {
  289. struct gadget_info *gi = to_gadget_info(item);
  290. WARN_ON(!list_empty(&gi->cdev.configs));
  291. WARN_ON(!list_empty(&gi->string_list));
  292. WARN_ON(!list_empty(&gi->available_func));
  293. kfree(gi->composite.gadget_driver.function);
  294. kfree(gi);
  295. }
  296. static struct configfs_item_operations gadget_root_item_ops = {
  297. .release = gadget_info_attr_release,
  298. };
  299. static void gadget_config_attr_release(struct config_item *item)
  300. {
  301. struct config_usb_cfg *cfg = to_config_usb_cfg(item);
  302. WARN_ON(!list_empty(&cfg->c.functions));
  303. list_del(&cfg->c.list);
  304. kfree(cfg->c.label);
  305. kfree(cfg);
  306. }
  307. static int config_usb_cfg_link(
  308. struct config_item *usb_cfg_ci,
  309. struct config_item *usb_func_ci)
  310. {
  311. struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
  312. struct usb_composite_dev *cdev = cfg->c.cdev;
  313. struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
  314. struct config_group *group = to_config_group(usb_func_ci);
  315. struct usb_function_instance *fi = container_of(group,
  316. struct usb_function_instance, group);
  317. struct usb_function_instance *a_fi;
  318. struct usb_function *f;
  319. int ret;
  320. mutex_lock(&gi->lock);
  321. /*
  322. * Make sure this function is from within our _this_ gadget and not
  323. * from another gadget or a random directory.
  324. * Also a function instance can only be linked once.
  325. */
  326. list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
  327. if (a_fi == fi)
  328. break;
  329. }
  330. if (a_fi != fi) {
  331. ret = -EINVAL;
  332. goto out;
  333. }
  334. list_for_each_entry(f, &cfg->func_list, list) {
  335. if (f->fi == fi) {
  336. ret = -EEXIST;
  337. goto out;
  338. }
  339. }
  340. f = usb_get_function(fi);
  341. if (IS_ERR(f)) {
  342. ret = PTR_ERR(f);
  343. goto out;
  344. }
  345. /* stash the function until we bind it to the gadget */
  346. list_add_tail(&f->list, &cfg->func_list);
  347. ret = 0;
  348. out:
  349. mutex_unlock(&gi->lock);
  350. return ret;
  351. }
  352. static void config_usb_cfg_unlink(
  353. struct config_item *usb_cfg_ci,
  354. struct config_item *usb_func_ci)
  355. {
  356. struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
  357. struct usb_composite_dev *cdev = cfg->c.cdev;
  358. struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
  359. struct config_group *group = to_config_group(usb_func_ci);
  360. struct usb_function_instance *fi = container_of(group,
  361. struct usb_function_instance, group);
  362. struct usb_function *f;
  363. /*
  364. * ideally I would like to forbid to unlink functions while a gadget is
  365. * bound to an UDC. Since this isn't possible at the moment, we simply
  366. * force an unbind, the function is available here and then we can
  367. * remove the function.
  368. */
  369. mutex_lock(&gi->lock);
  370. if (gi->composite.gadget_driver.udc_name)
  371. unregister_gadget(gi);
  372. WARN_ON(gi->composite.gadget_driver.udc_name);
  373. list_for_each_entry(f, &cfg->func_list, list) {
  374. if (f->fi == fi) {
  375. list_del(&f->list);
  376. usb_put_function(f);
  377. mutex_unlock(&gi->lock);
  378. return;
  379. }
  380. }
  381. mutex_unlock(&gi->lock);
  382. WARN(1, "Unable to locate function to unbind\n");
  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\n",
  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, "0x%02x\n",
  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. int res;
  668. res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
  669. UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
  670. page[res++] = '\n';
  671. return res;
  672. }
  673. static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
  674. size_t len)
  675. {
  676. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  677. int res, l;
  678. l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
  679. if (page[l - 1] == '\n')
  680. --l;
  681. mutex_lock(&gi->lock);
  682. res = utf8s_to_utf16s(page, l,
  683. UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
  684. OS_STRING_QW_SIGN_LEN);
  685. if (res > 0)
  686. res = len;
  687. mutex_unlock(&gi->lock);
  688. return res;
  689. }
  690. CONFIGFS_ATTR(os_desc_, use);
  691. CONFIGFS_ATTR(os_desc_, b_vendor_code);
  692. CONFIGFS_ATTR(os_desc_, qw_sign);
  693. static struct configfs_attribute *os_desc_attrs[] = {
  694. &os_desc_attr_use,
  695. &os_desc_attr_b_vendor_code,
  696. &os_desc_attr_qw_sign,
  697. NULL,
  698. };
  699. static void os_desc_attr_release(struct config_item *item)
  700. {
  701. struct os_desc *os_desc = to_os_desc(item);
  702. kfree(os_desc);
  703. }
  704. static int os_desc_link(struct config_item *os_desc_ci,
  705. struct config_item *usb_cfg_ci)
  706. {
  707. struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
  708. struct gadget_info, os_desc_group);
  709. struct usb_composite_dev *cdev = &gi->cdev;
  710. struct config_usb_cfg *c_target =
  711. container_of(to_config_group(usb_cfg_ci),
  712. struct config_usb_cfg, group);
  713. struct usb_configuration *c;
  714. int ret;
  715. mutex_lock(&gi->lock);
  716. list_for_each_entry(c, &cdev->configs, list) {
  717. if (c == &c_target->c)
  718. break;
  719. }
  720. if (c != &c_target->c) {
  721. ret = -EINVAL;
  722. goto out;
  723. }
  724. if (cdev->os_desc_config) {
  725. ret = -EBUSY;
  726. goto out;
  727. }
  728. cdev->os_desc_config = &c_target->c;
  729. ret = 0;
  730. out:
  731. mutex_unlock(&gi->lock);
  732. return ret;
  733. }
  734. static void os_desc_unlink(struct config_item *os_desc_ci,
  735. struct config_item *usb_cfg_ci)
  736. {
  737. struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
  738. struct gadget_info, os_desc_group);
  739. struct usb_composite_dev *cdev = &gi->cdev;
  740. mutex_lock(&gi->lock);
  741. if (gi->composite.gadget_driver.udc_name)
  742. unregister_gadget(gi);
  743. cdev->os_desc_config = NULL;
  744. WARN_ON(gi->composite.gadget_driver.udc_name);
  745. mutex_unlock(&gi->lock);
  746. }
  747. static struct configfs_item_operations os_desc_ops = {
  748. .release = os_desc_attr_release,
  749. .allow_link = os_desc_link,
  750. .drop_link = os_desc_unlink,
  751. };
  752. static struct config_item_type os_desc_type = {
  753. .ct_item_ops = &os_desc_ops,
  754. .ct_attrs = os_desc_attrs,
  755. .ct_owner = THIS_MODULE,
  756. };
  757. static inline struct usb_os_desc_ext_prop
  758. *to_usb_os_desc_ext_prop(struct config_item *item)
  759. {
  760. return container_of(item, struct usb_os_desc_ext_prop, item);
  761. }
  762. static ssize_t ext_prop_type_show(struct config_item *item, char *page)
  763. {
  764. return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
  765. }
  766. static ssize_t ext_prop_type_store(struct config_item *item,
  767. const char *page, size_t len)
  768. {
  769. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  770. struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
  771. u8 type;
  772. int ret;
  773. if (desc->opts_mutex)
  774. mutex_lock(desc->opts_mutex);
  775. ret = kstrtou8(page, 0, &type);
  776. if (ret)
  777. goto end;
  778. if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
  779. ret = -EINVAL;
  780. goto end;
  781. }
  782. if ((ext_prop->type == USB_EXT_PROP_BINARY ||
  783. ext_prop->type == USB_EXT_PROP_LE32 ||
  784. ext_prop->type == USB_EXT_PROP_BE32) &&
  785. (type == USB_EXT_PROP_UNICODE ||
  786. type == USB_EXT_PROP_UNICODE_ENV ||
  787. type == USB_EXT_PROP_UNICODE_LINK))
  788. ext_prop->data_len <<= 1;
  789. else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
  790. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  791. ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
  792. (type == USB_EXT_PROP_BINARY ||
  793. type == USB_EXT_PROP_LE32 ||
  794. type == USB_EXT_PROP_BE32))
  795. ext_prop->data_len >>= 1;
  796. ext_prop->type = type;
  797. ret = len;
  798. end:
  799. if (desc->opts_mutex)
  800. mutex_unlock(desc->opts_mutex);
  801. return ret;
  802. }
  803. static ssize_t ext_prop_data_show(struct config_item *item, char *page)
  804. {
  805. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  806. int len = ext_prop->data_len;
  807. if (ext_prop->type == USB_EXT_PROP_UNICODE ||
  808. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  809. ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
  810. len >>= 1;
  811. memcpy(page, ext_prop->data, len);
  812. return len;
  813. }
  814. static ssize_t ext_prop_data_store(struct config_item *item,
  815. const char *page, size_t len)
  816. {
  817. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  818. struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
  819. char *new_data;
  820. size_t ret_len = len;
  821. if (page[len - 1] == '\n' || page[len - 1] == '\0')
  822. --len;
  823. new_data = kmemdup(page, len, GFP_KERNEL);
  824. if (!new_data)
  825. return -ENOMEM;
  826. if (desc->opts_mutex)
  827. mutex_lock(desc->opts_mutex);
  828. kfree(ext_prop->data);
  829. ext_prop->data = new_data;
  830. desc->ext_prop_len -= ext_prop->data_len;
  831. ext_prop->data_len = len;
  832. desc->ext_prop_len += ext_prop->data_len;
  833. if (ext_prop->type == USB_EXT_PROP_UNICODE ||
  834. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  835. ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
  836. desc->ext_prop_len -= ext_prop->data_len;
  837. ext_prop->data_len <<= 1;
  838. ext_prop->data_len += 2;
  839. desc->ext_prop_len += ext_prop->data_len;
  840. }
  841. if (desc->opts_mutex)
  842. mutex_unlock(desc->opts_mutex);
  843. return ret_len;
  844. }
  845. CONFIGFS_ATTR(ext_prop_, type);
  846. CONFIGFS_ATTR(ext_prop_, data);
  847. static struct configfs_attribute *ext_prop_attrs[] = {
  848. &ext_prop_attr_type,
  849. &ext_prop_attr_data,
  850. NULL,
  851. };
  852. static void usb_os_desc_ext_prop_release(struct config_item *item)
  853. {
  854. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  855. kfree(ext_prop); /* frees a whole chunk */
  856. }
  857. static struct configfs_item_operations ext_prop_ops = {
  858. .release = usb_os_desc_ext_prop_release,
  859. };
  860. static struct config_item *ext_prop_make(
  861. struct config_group *group,
  862. const char *name)
  863. {
  864. struct usb_os_desc_ext_prop *ext_prop;
  865. struct config_item_type *ext_prop_type;
  866. struct usb_os_desc *desc;
  867. char *vlabuf;
  868. vla_group(data_chunk);
  869. vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
  870. vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
  871. vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
  872. if (!vlabuf)
  873. return ERR_PTR(-ENOMEM);
  874. ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
  875. ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
  876. desc = container_of(group, struct usb_os_desc, group);
  877. ext_prop_type->ct_item_ops = &ext_prop_ops;
  878. ext_prop_type->ct_attrs = ext_prop_attrs;
  879. ext_prop_type->ct_owner = desc->owner;
  880. config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
  881. ext_prop->name = kstrdup(name, GFP_KERNEL);
  882. if (!ext_prop->name) {
  883. kfree(vlabuf);
  884. return ERR_PTR(-ENOMEM);
  885. }
  886. desc->ext_prop_len += 14;
  887. ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
  888. if (desc->opts_mutex)
  889. mutex_lock(desc->opts_mutex);
  890. desc->ext_prop_len += ext_prop->name_len;
  891. list_add_tail(&ext_prop->entry, &desc->ext_prop);
  892. ++desc->ext_prop_count;
  893. if (desc->opts_mutex)
  894. mutex_unlock(desc->opts_mutex);
  895. return &ext_prop->item;
  896. }
  897. static void ext_prop_drop(struct config_group *group, struct config_item *item)
  898. {
  899. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  900. struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
  901. if (desc->opts_mutex)
  902. mutex_lock(desc->opts_mutex);
  903. list_del(&ext_prop->entry);
  904. --desc->ext_prop_count;
  905. kfree(ext_prop->name);
  906. desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
  907. if (desc->opts_mutex)
  908. mutex_unlock(desc->opts_mutex);
  909. config_item_put(item);
  910. }
  911. static struct configfs_group_operations interf_grp_ops = {
  912. .make_item = &ext_prop_make,
  913. .drop_item = &ext_prop_drop,
  914. };
  915. static ssize_t interf_grp_compatible_id_show(struct config_item *item,
  916. char *page)
  917. {
  918. memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
  919. return 8;
  920. }
  921. static ssize_t interf_grp_compatible_id_store(struct config_item *item,
  922. const char *page, size_t len)
  923. {
  924. struct usb_os_desc *desc = to_usb_os_desc(item);
  925. int l;
  926. l = min_t(int, 8, len);
  927. if (page[l - 1] == '\n')
  928. --l;
  929. if (desc->opts_mutex)
  930. mutex_lock(desc->opts_mutex);
  931. memcpy(desc->ext_compat_id, page, l);
  932. if (desc->opts_mutex)
  933. mutex_unlock(desc->opts_mutex);
  934. return len;
  935. }
  936. static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
  937. char *page)
  938. {
  939. memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
  940. return 8;
  941. }
  942. static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
  943. const char *page, size_t len)
  944. {
  945. struct usb_os_desc *desc = to_usb_os_desc(item);
  946. int l;
  947. l = min_t(int, 8, len);
  948. if (page[l - 1] == '\n')
  949. --l;
  950. if (desc->opts_mutex)
  951. mutex_lock(desc->opts_mutex);
  952. memcpy(desc->ext_compat_id + 8, page, l);
  953. if (desc->opts_mutex)
  954. mutex_unlock(desc->opts_mutex);
  955. return len;
  956. }
  957. CONFIGFS_ATTR(interf_grp_, compatible_id);
  958. CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
  959. static struct configfs_attribute *interf_grp_attrs[] = {
  960. &interf_grp_attr_compatible_id,
  961. &interf_grp_attr_sub_compatible_id,
  962. NULL
  963. };
  964. int usb_os_desc_prepare_interf_dir(struct config_group *parent,
  965. int n_interf,
  966. struct usb_os_desc **desc,
  967. char **names,
  968. struct module *owner)
  969. {
  970. struct config_group *os_desc_group;
  971. struct config_item_type *os_desc_type, *interface_type;
  972. vla_group(data_chunk);
  973. vla_item(data_chunk, struct config_group, os_desc_group, 1);
  974. vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
  975. vla_item(data_chunk, struct config_item_type, interface_type, 1);
  976. char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
  977. if (!vlabuf)
  978. return -ENOMEM;
  979. os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
  980. os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
  981. interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
  982. os_desc_type->ct_owner = owner;
  983. config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
  984. configfs_add_default_group(os_desc_group, parent);
  985. interface_type->ct_group_ops = &interf_grp_ops;
  986. interface_type->ct_attrs = interf_grp_attrs;
  987. interface_type->ct_owner = owner;
  988. while (n_interf--) {
  989. struct usb_os_desc *d;
  990. d = desc[n_interf];
  991. d->owner = owner;
  992. config_group_init_type_name(&d->group, "", interface_type);
  993. config_item_set_name(&d->group.cg_item, "interface.%s",
  994. names[n_interf]);
  995. configfs_add_default_group(&d->group, os_desc_group);
  996. }
  997. return 0;
  998. }
  999. EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
  1000. static int configfs_do_nothing(struct usb_composite_dev *cdev)
  1001. {
  1002. WARN_ON(1);
  1003. return -EINVAL;
  1004. }
  1005. int composite_dev_prepare(struct usb_composite_driver *composite,
  1006. struct usb_composite_dev *dev);
  1007. int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
  1008. struct usb_ep *ep0);
  1009. static void purge_configs_funcs(struct gadget_info *gi)
  1010. {
  1011. struct usb_configuration *c;
  1012. list_for_each_entry(c, &gi->cdev.configs, list) {
  1013. struct usb_function *f, *tmp;
  1014. struct config_usb_cfg *cfg;
  1015. cfg = container_of(c, struct config_usb_cfg, c);
  1016. list_for_each_entry_safe(f, tmp, &c->functions, list) {
  1017. list_move_tail(&f->list, &cfg->func_list);
  1018. if (f->unbind) {
  1019. dev_dbg(&gi->cdev.gadget->dev,
  1020. "unbind function '%s'/%p\n",
  1021. f->name, f);
  1022. f->unbind(c, f);
  1023. }
  1024. }
  1025. c->next_interface_id = 0;
  1026. memset(c->interface, 0, sizeof(c->interface));
  1027. c->superspeed_plus = 0;
  1028. c->superspeed = 0;
  1029. c->highspeed = 0;
  1030. c->fullspeed = 0;
  1031. }
  1032. }
  1033. static int configfs_composite_bind(struct usb_gadget *gadget,
  1034. struct usb_gadget_driver *gdriver)
  1035. {
  1036. struct usb_composite_driver *composite = to_cdriver(gdriver);
  1037. struct gadget_info *gi = container_of(composite,
  1038. struct gadget_info, composite);
  1039. struct usb_composite_dev *cdev = &gi->cdev;
  1040. struct usb_configuration *c;
  1041. struct usb_string *s;
  1042. unsigned i;
  1043. int ret;
  1044. /* the gi->lock is hold by the caller */
  1045. cdev->gadget = gadget;
  1046. set_gadget_data(gadget, cdev);
  1047. ret = composite_dev_prepare(composite, cdev);
  1048. if (ret)
  1049. return ret;
  1050. /* and now the gadget bind */
  1051. ret = -EINVAL;
  1052. if (list_empty(&gi->cdev.configs)) {
  1053. pr_err("Need at least one configuration in %s.\n",
  1054. gi->composite.name);
  1055. goto err_comp_cleanup;
  1056. }
  1057. list_for_each_entry(c, &gi->cdev.configs, list) {
  1058. struct config_usb_cfg *cfg;
  1059. cfg = container_of(c, struct config_usb_cfg, c);
  1060. if (list_empty(&cfg->func_list)) {
  1061. pr_err("Config %s/%d of %s needs at least one function.\n",
  1062. c->label, c->bConfigurationValue,
  1063. gi->composite.name);
  1064. goto err_comp_cleanup;
  1065. }
  1066. }
  1067. /* init all strings */
  1068. if (!list_empty(&gi->string_list)) {
  1069. struct gadget_strings *gs;
  1070. i = 0;
  1071. list_for_each_entry(gs, &gi->string_list, list) {
  1072. gi->gstrings[i] = &gs->stringtab_dev;
  1073. gs->stringtab_dev.strings = gs->strings;
  1074. gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
  1075. gs->manufacturer;
  1076. gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
  1077. gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
  1078. i++;
  1079. }
  1080. gi->gstrings[i] = NULL;
  1081. s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
  1082. USB_GADGET_FIRST_AVAIL_IDX);
  1083. if (IS_ERR(s)) {
  1084. ret = PTR_ERR(s);
  1085. goto err_comp_cleanup;
  1086. }
  1087. gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
  1088. gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
  1089. gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
  1090. }
  1091. if (gi->use_os_desc) {
  1092. cdev->use_os_string = true;
  1093. cdev->b_vendor_code = gi->b_vendor_code;
  1094. memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
  1095. }
  1096. if (gadget_is_otg(gadget) && !otg_desc[0]) {
  1097. struct usb_descriptor_header *usb_desc;
  1098. usb_desc = usb_otg_descriptor_alloc(gadget);
  1099. if (!usb_desc) {
  1100. ret = -ENOMEM;
  1101. goto err_comp_cleanup;
  1102. }
  1103. usb_otg_descriptor_init(gadget, usb_desc);
  1104. otg_desc[0] = usb_desc;
  1105. otg_desc[1] = NULL;
  1106. }
  1107. /* Go through all configs, attach all functions */
  1108. list_for_each_entry(c, &gi->cdev.configs, list) {
  1109. struct config_usb_cfg *cfg;
  1110. struct usb_function *f;
  1111. struct usb_function *tmp;
  1112. struct gadget_config_name *cn;
  1113. if (gadget_is_otg(gadget))
  1114. c->descriptors = otg_desc;
  1115. cfg = container_of(c, struct config_usb_cfg, c);
  1116. if (!list_empty(&cfg->string_list)) {
  1117. i = 0;
  1118. list_for_each_entry(cn, &cfg->string_list, list) {
  1119. cfg->gstrings[i] = &cn->stringtab_dev;
  1120. cn->stringtab_dev.strings = &cn->strings;
  1121. cn->strings.s = cn->configuration;
  1122. i++;
  1123. }
  1124. cfg->gstrings[i] = NULL;
  1125. s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
  1126. if (IS_ERR(s)) {
  1127. ret = PTR_ERR(s);
  1128. goto err_comp_cleanup;
  1129. }
  1130. c->iConfiguration = s[0].id;
  1131. }
  1132. list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
  1133. list_del(&f->list);
  1134. ret = usb_add_function(c, f);
  1135. if (ret) {
  1136. list_add(&f->list, &cfg->func_list);
  1137. goto err_purge_funcs;
  1138. }
  1139. }
  1140. usb_ep_autoconfig_reset(cdev->gadget);
  1141. }
  1142. if (cdev->use_os_string) {
  1143. ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
  1144. if (ret)
  1145. goto err_purge_funcs;
  1146. }
  1147. usb_ep_autoconfig_reset(cdev->gadget);
  1148. return 0;
  1149. err_purge_funcs:
  1150. purge_configs_funcs(gi);
  1151. err_comp_cleanup:
  1152. composite_dev_cleanup(cdev);
  1153. return ret;
  1154. }
  1155. static void configfs_composite_unbind(struct usb_gadget *gadget)
  1156. {
  1157. struct usb_composite_dev *cdev;
  1158. struct gadget_info *gi;
  1159. /* the gi->lock is hold by the caller */
  1160. cdev = get_gadget_data(gadget);
  1161. gi = container_of(cdev, struct gadget_info, cdev);
  1162. kfree(otg_desc[0]);
  1163. otg_desc[0] = NULL;
  1164. purge_configs_funcs(gi);
  1165. composite_dev_cleanup(cdev);
  1166. usb_ep_autoconfig_reset(cdev->gadget);
  1167. cdev->gadget = NULL;
  1168. set_gadget_data(gadget, NULL);
  1169. }
  1170. static const struct usb_gadget_driver configfs_driver_template = {
  1171. .bind = configfs_composite_bind,
  1172. .unbind = configfs_composite_unbind,
  1173. .setup = composite_setup,
  1174. .reset = composite_disconnect,
  1175. .disconnect = composite_disconnect,
  1176. .suspend = composite_suspend,
  1177. .resume = composite_resume,
  1178. .max_speed = USB_SPEED_SUPER,
  1179. .driver = {
  1180. .owner = THIS_MODULE,
  1181. .name = "configfs-gadget",
  1182. },
  1183. .match_existing_only = 1,
  1184. };
  1185. static struct config_group *gadgets_make(
  1186. struct config_group *group,
  1187. const char *name)
  1188. {
  1189. struct gadget_info *gi;
  1190. gi = kzalloc(sizeof(*gi), GFP_KERNEL);
  1191. if (!gi)
  1192. return ERR_PTR(-ENOMEM);
  1193. config_group_init_type_name(&gi->group, name, &gadget_root_type);
  1194. config_group_init_type_name(&gi->functions_group, "functions",
  1195. &functions_type);
  1196. configfs_add_default_group(&gi->functions_group, &gi->group);
  1197. config_group_init_type_name(&gi->configs_group, "configs",
  1198. &config_desc_type);
  1199. configfs_add_default_group(&gi->configs_group, &gi->group);
  1200. config_group_init_type_name(&gi->strings_group, "strings",
  1201. &gadget_strings_strings_type);
  1202. configfs_add_default_group(&gi->strings_group, &gi->group);
  1203. config_group_init_type_name(&gi->os_desc_group, "os_desc",
  1204. &os_desc_type);
  1205. configfs_add_default_group(&gi->os_desc_group, &gi->group);
  1206. gi->composite.bind = configfs_do_nothing;
  1207. gi->composite.unbind = configfs_do_nothing;
  1208. gi->composite.suspend = NULL;
  1209. gi->composite.resume = NULL;
  1210. gi->composite.max_speed = USB_SPEED_SUPER;
  1211. mutex_init(&gi->lock);
  1212. INIT_LIST_HEAD(&gi->string_list);
  1213. INIT_LIST_HEAD(&gi->available_func);
  1214. composite_init_dev(&gi->cdev);
  1215. gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
  1216. gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
  1217. gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
  1218. gi->composite.gadget_driver = configfs_driver_template;
  1219. gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
  1220. gi->composite.name = gi->composite.gadget_driver.function;
  1221. if (!gi->composite.gadget_driver.function)
  1222. goto err;
  1223. return &gi->group;
  1224. err:
  1225. kfree(gi);
  1226. return ERR_PTR(-ENOMEM);
  1227. }
  1228. static void gadgets_drop(struct config_group *group, struct config_item *item)
  1229. {
  1230. config_item_put(item);
  1231. }
  1232. static struct configfs_group_operations gadgets_ops = {
  1233. .make_group = &gadgets_make,
  1234. .drop_item = &gadgets_drop,
  1235. };
  1236. static struct config_item_type gadgets_type = {
  1237. .ct_group_ops = &gadgets_ops,
  1238. .ct_owner = THIS_MODULE,
  1239. };
  1240. static struct configfs_subsystem gadget_subsys = {
  1241. .su_group = {
  1242. .cg_item = {
  1243. .ci_namebuf = "usb_gadget",
  1244. .ci_type = &gadgets_type,
  1245. },
  1246. },
  1247. .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
  1248. };
  1249. void unregister_gadget_item(struct config_item *item)
  1250. {
  1251. struct gadget_info *gi = to_gadget_info(item);
  1252. mutex_lock(&gi->lock);
  1253. unregister_gadget(gi);
  1254. mutex_unlock(&gi->lock);
  1255. }
  1256. EXPORT_SYMBOL_GPL(unregister_gadget_item);
  1257. static int __init gadget_cfs_init(void)
  1258. {
  1259. int ret;
  1260. config_group_init(&gadget_subsys.su_group);
  1261. ret = configfs_register_subsystem(&gadget_subsys);
  1262. return ret;
  1263. }
  1264. module_init(gadget_cfs_init);
  1265. static void __exit gadget_cfs_exit(void)
  1266. {
  1267. configfs_unregister_subsystem(&gadget_subsys);
  1268. }
  1269. module_exit(gadget_cfs_exit);