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 int 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 0;
  379. }
  380. }
  381. mutex_unlock(&gi->lock);
  382. WARN(1, "Unable to locate function to unbind\n");
  383. return 0;
  384. }
  385. static struct configfs_item_operations gadget_config_item_ops = {
  386. .release = gadget_config_attr_release,
  387. .allow_link = config_usb_cfg_link,
  388. .drop_link = config_usb_cfg_unlink,
  389. };
  390. static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
  391. char *page)
  392. {
  393. return sprintf(page, "%u\n", to_config_usb_cfg(item)->c.MaxPower);
  394. }
  395. static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
  396. const char *page, size_t len)
  397. {
  398. u16 val;
  399. int ret;
  400. ret = kstrtou16(page, 0, &val);
  401. if (ret)
  402. return ret;
  403. if (DIV_ROUND_UP(val, 8) > 0xff)
  404. return -ERANGE;
  405. to_config_usb_cfg(item)->c.MaxPower = val;
  406. return len;
  407. }
  408. static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
  409. char *page)
  410. {
  411. return sprintf(page, "0x%02x\n",
  412. to_config_usb_cfg(item)->c.bmAttributes);
  413. }
  414. static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
  415. const char *page, size_t len)
  416. {
  417. u8 val;
  418. int ret;
  419. ret = kstrtou8(page, 0, &val);
  420. if (ret)
  421. return ret;
  422. if (!(val & USB_CONFIG_ATT_ONE))
  423. return -EINVAL;
  424. if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
  425. USB_CONFIG_ATT_WAKEUP))
  426. return -EINVAL;
  427. to_config_usb_cfg(item)->c.bmAttributes = val;
  428. return len;
  429. }
  430. CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
  431. CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
  432. static struct configfs_attribute *gadget_config_attrs[] = {
  433. &gadget_config_desc_attr_MaxPower,
  434. &gadget_config_desc_attr_bmAttributes,
  435. NULL,
  436. };
  437. static struct config_item_type gadget_config_type = {
  438. .ct_item_ops = &gadget_config_item_ops,
  439. .ct_attrs = gadget_config_attrs,
  440. .ct_owner = THIS_MODULE,
  441. };
  442. static struct config_item_type gadget_root_type = {
  443. .ct_item_ops = &gadget_root_item_ops,
  444. .ct_attrs = gadget_root_attrs,
  445. .ct_owner = THIS_MODULE,
  446. };
  447. static void composite_init_dev(struct usb_composite_dev *cdev)
  448. {
  449. spin_lock_init(&cdev->lock);
  450. INIT_LIST_HEAD(&cdev->configs);
  451. INIT_LIST_HEAD(&cdev->gstrings);
  452. }
  453. static struct config_group *function_make(
  454. struct config_group *group,
  455. const char *name)
  456. {
  457. struct gadget_info *gi;
  458. struct usb_function_instance *fi;
  459. char buf[MAX_NAME_LEN];
  460. char *func_name;
  461. char *instance_name;
  462. int ret;
  463. ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
  464. if (ret >= MAX_NAME_LEN)
  465. return ERR_PTR(-ENAMETOOLONG);
  466. func_name = buf;
  467. instance_name = strchr(func_name, '.');
  468. if (!instance_name) {
  469. pr_err("Unable to locate . in FUNC.INSTANCE\n");
  470. return ERR_PTR(-EINVAL);
  471. }
  472. *instance_name = '\0';
  473. instance_name++;
  474. fi = usb_get_function_instance(func_name);
  475. if (IS_ERR(fi))
  476. return ERR_CAST(fi);
  477. ret = config_item_set_name(&fi->group.cg_item, "%s", name);
  478. if (ret) {
  479. usb_put_function_instance(fi);
  480. return ERR_PTR(ret);
  481. }
  482. if (fi->set_inst_name) {
  483. ret = fi->set_inst_name(fi, instance_name);
  484. if (ret) {
  485. usb_put_function_instance(fi);
  486. return ERR_PTR(ret);
  487. }
  488. }
  489. gi = container_of(group, struct gadget_info, functions_group);
  490. mutex_lock(&gi->lock);
  491. list_add_tail(&fi->cfs_list, &gi->available_func);
  492. mutex_unlock(&gi->lock);
  493. return &fi->group;
  494. }
  495. static void function_drop(
  496. struct config_group *group,
  497. struct config_item *item)
  498. {
  499. struct usb_function_instance *fi = to_usb_function_instance(item);
  500. struct gadget_info *gi;
  501. gi = container_of(group, struct gadget_info, functions_group);
  502. mutex_lock(&gi->lock);
  503. list_del(&fi->cfs_list);
  504. mutex_unlock(&gi->lock);
  505. config_item_put(item);
  506. }
  507. static struct configfs_group_operations functions_ops = {
  508. .make_group = &function_make,
  509. .drop_item = &function_drop,
  510. };
  511. static struct config_item_type functions_type = {
  512. .ct_group_ops = &functions_ops,
  513. .ct_owner = THIS_MODULE,
  514. };
  515. GS_STRINGS_RW(gadget_config_name, configuration);
  516. static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
  517. &gadget_config_name_attr_configuration,
  518. NULL,
  519. };
  520. static void gadget_config_name_attr_release(struct config_item *item)
  521. {
  522. struct gadget_config_name *cn = to_gadget_config_name(item);
  523. kfree(cn->configuration);
  524. list_del(&cn->list);
  525. kfree(cn);
  526. }
  527. USB_CONFIG_STRING_RW_OPS(gadget_config_name);
  528. USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
  529. static struct config_group *config_desc_make(
  530. struct config_group *group,
  531. const char *name)
  532. {
  533. struct gadget_info *gi;
  534. struct config_usb_cfg *cfg;
  535. char buf[MAX_NAME_LEN];
  536. char *num_str;
  537. u8 num;
  538. int ret;
  539. gi = container_of(group, struct gadget_info, configs_group);
  540. ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
  541. if (ret >= MAX_NAME_LEN)
  542. return ERR_PTR(-ENAMETOOLONG);
  543. num_str = strchr(buf, '.');
  544. if (!num_str) {
  545. pr_err("Unable to locate . in name.bConfigurationValue\n");
  546. return ERR_PTR(-EINVAL);
  547. }
  548. *num_str = '\0';
  549. num_str++;
  550. if (!strlen(buf))
  551. return ERR_PTR(-EINVAL);
  552. ret = kstrtou8(num_str, 0, &num);
  553. if (ret)
  554. return ERR_PTR(ret);
  555. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  556. if (!cfg)
  557. return ERR_PTR(-ENOMEM);
  558. cfg->c.label = kstrdup(buf, GFP_KERNEL);
  559. if (!cfg->c.label) {
  560. ret = -ENOMEM;
  561. goto err;
  562. }
  563. cfg->c.bConfigurationValue = num;
  564. cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
  565. cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
  566. INIT_LIST_HEAD(&cfg->string_list);
  567. INIT_LIST_HEAD(&cfg->func_list);
  568. config_group_init_type_name(&cfg->group, name,
  569. &gadget_config_type);
  570. config_group_init_type_name(&cfg->strings_group, "strings",
  571. &gadget_config_name_strings_type);
  572. configfs_add_default_group(&cfg->strings_group, &cfg->group);
  573. ret = usb_add_config_only(&gi->cdev, &cfg->c);
  574. if (ret)
  575. goto err;
  576. return &cfg->group;
  577. err:
  578. kfree(cfg->c.label);
  579. kfree(cfg);
  580. return ERR_PTR(ret);
  581. }
  582. static void config_desc_drop(
  583. struct config_group *group,
  584. struct config_item *item)
  585. {
  586. config_item_put(item);
  587. }
  588. static struct configfs_group_operations config_desc_ops = {
  589. .make_group = &config_desc_make,
  590. .drop_item = &config_desc_drop,
  591. };
  592. static struct config_item_type config_desc_type = {
  593. .ct_group_ops = &config_desc_ops,
  594. .ct_owner = THIS_MODULE,
  595. };
  596. GS_STRINGS_RW(gadget_strings, manufacturer);
  597. GS_STRINGS_RW(gadget_strings, product);
  598. GS_STRINGS_RW(gadget_strings, serialnumber);
  599. static struct configfs_attribute *gadget_strings_langid_attrs[] = {
  600. &gadget_strings_attr_manufacturer,
  601. &gadget_strings_attr_product,
  602. &gadget_strings_attr_serialnumber,
  603. NULL,
  604. };
  605. static void gadget_strings_attr_release(struct config_item *item)
  606. {
  607. struct gadget_strings *gs = to_gadget_strings(item);
  608. kfree(gs->manufacturer);
  609. kfree(gs->product);
  610. kfree(gs->serialnumber);
  611. list_del(&gs->list);
  612. kfree(gs);
  613. }
  614. USB_CONFIG_STRING_RW_OPS(gadget_strings);
  615. USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
  616. static inline struct os_desc *to_os_desc(struct config_item *item)
  617. {
  618. return container_of(to_config_group(item), struct os_desc, group);
  619. }
  620. static inline struct gadget_info *os_desc_item_to_gadget_info(
  621. struct config_item *item)
  622. {
  623. return to_gadget_info(to_os_desc(item)->group.cg_item.ci_parent);
  624. }
  625. static ssize_t os_desc_use_show(struct config_item *item, char *page)
  626. {
  627. return sprintf(page, "%d",
  628. os_desc_item_to_gadget_info(item)->use_os_desc);
  629. }
  630. static ssize_t os_desc_use_store(struct config_item *item, const char *page,
  631. size_t len)
  632. {
  633. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  634. int ret;
  635. bool use;
  636. mutex_lock(&gi->lock);
  637. ret = strtobool(page, &use);
  638. if (!ret) {
  639. gi->use_os_desc = use;
  640. ret = len;
  641. }
  642. mutex_unlock(&gi->lock);
  643. return ret;
  644. }
  645. static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
  646. {
  647. return sprintf(page, "%d",
  648. os_desc_item_to_gadget_info(item)->b_vendor_code);
  649. }
  650. static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
  651. const char *page, size_t len)
  652. {
  653. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  654. int ret;
  655. u8 b_vendor_code;
  656. mutex_lock(&gi->lock);
  657. ret = kstrtou8(page, 0, &b_vendor_code);
  658. if (!ret) {
  659. gi->b_vendor_code = b_vendor_code;
  660. ret = len;
  661. }
  662. mutex_unlock(&gi->lock);
  663. return ret;
  664. }
  665. static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
  666. {
  667. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  668. memcpy(page, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
  669. return OS_STRING_QW_SIGN_LEN;
  670. }
  671. static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
  672. size_t len)
  673. {
  674. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  675. int res, l;
  676. l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
  677. if (page[l - 1] == '\n')
  678. --l;
  679. mutex_lock(&gi->lock);
  680. res = utf8s_to_utf16s(page, l,
  681. UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
  682. OS_STRING_QW_SIGN_LEN);
  683. if (res > 0)
  684. res = len;
  685. mutex_unlock(&gi->lock);
  686. return res;
  687. }
  688. CONFIGFS_ATTR(os_desc_, use);
  689. CONFIGFS_ATTR(os_desc_, b_vendor_code);
  690. CONFIGFS_ATTR(os_desc_, qw_sign);
  691. static struct configfs_attribute *os_desc_attrs[] = {
  692. &os_desc_attr_use,
  693. &os_desc_attr_b_vendor_code,
  694. &os_desc_attr_qw_sign,
  695. NULL,
  696. };
  697. static void os_desc_attr_release(struct config_item *item)
  698. {
  699. struct os_desc *os_desc = to_os_desc(item);
  700. kfree(os_desc);
  701. }
  702. static int os_desc_link(struct config_item *os_desc_ci,
  703. struct config_item *usb_cfg_ci)
  704. {
  705. struct gadget_info *gi = container_of(to_config_group(os_desc_ci),
  706. struct gadget_info, os_desc_group);
  707. struct usb_composite_dev *cdev = &gi->cdev;
  708. struct config_usb_cfg *c_target =
  709. container_of(to_config_group(usb_cfg_ci),
  710. struct config_usb_cfg, group);
  711. struct usb_configuration *c;
  712. int ret;
  713. mutex_lock(&gi->lock);
  714. list_for_each_entry(c, &cdev->configs, list) {
  715. if (c == &c_target->c)
  716. break;
  717. }
  718. if (c != &c_target->c) {
  719. ret = -EINVAL;
  720. goto out;
  721. }
  722. if (cdev->os_desc_config) {
  723. ret = -EBUSY;
  724. goto out;
  725. }
  726. cdev->os_desc_config = &c_target->c;
  727. ret = 0;
  728. out:
  729. mutex_unlock(&gi->lock);
  730. return ret;
  731. }
  732. static int os_desc_unlink(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. mutex_lock(&gi->lock);
  739. if (gi->composite.gadget_driver.udc_name)
  740. unregister_gadget(gi);
  741. cdev->os_desc_config = NULL;
  742. WARN_ON(gi->composite.gadget_driver.udc_name);
  743. mutex_unlock(&gi->lock);
  744. return 0;
  745. }
  746. static struct configfs_item_operations os_desc_ops = {
  747. .release = os_desc_attr_release,
  748. .allow_link = os_desc_link,
  749. .drop_link = os_desc_unlink,
  750. };
  751. static struct config_item_type os_desc_type = {
  752. .ct_item_ops = &os_desc_ops,
  753. .ct_attrs = os_desc_attrs,
  754. .ct_owner = THIS_MODULE,
  755. };
  756. static inline struct usb_os_desc_ext_prop
  757. *to_usb_os_desc_ext_prop(struct config_item *item)
  758. {
  759. return container_of(item, struct usb_os_desc_ext_prop, item);
  760. }
  761. static ssize_t ext_prop_type_show(struct config_item *item, char *page)
  762. {
  763. return sprintf(page, "%d", to_usb_os_desc_ext_prop(item)->type);
  764. }
  765. static ssize_t ext_prop_type_store(struct config_item *item,
  766. const char *page, size_t len)
  767. {
  768. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  769. struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
  770. u8 type;
  771. int ret;
  772. if (desc->opts_mutex)
  773. mutex_lock(desc->opts_mutex);
  774. ret = kstrtou8(page, 0, &type);
  775. if (ret)
  776. goto end;
  777. if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI) {
  778. ret = -EINVAL;
  779. goto end;
  780. }
  781. if ((ext_prop->type == USB_EXT_PROP_BINARY ||
  782. ext_prop->type == USB_EXT_PROP_LE32 ||
  783. ext_prop->type == USB_EXT_PROP_BE32) &&
  784. (type == USB_EXT_PROP_UNICODE ||
  785. type == USB_EXT_PROP_UNICODE_ENV ||
  786. type == USB_EXT_PROP_UNICODE_LINK))
  787. ext_prop->data_len <<= 1;
  788. else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
  789. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  790. ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
  791. (type == USB_EXT_PROP_BINARY ||
  792. type == USB_EXT_PROP_LE32 ||
  793. type == USB_EXT_PROP_BE32))
  794. ext_prop->data_len >>= 1;
  795. ext_prop->type = type;
  796. ret = len;
  797. end:
  798. if (desc->opts_mutex)
  799. mutex_unlock(desc->opts_mutex);
  800. return ret;
  801. }
  802. static ssize_t ext_prop_data_show(struct config_item *item, char *page)
  803. {
  804. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  805. int len = ext_prop->data_len;
  806. if (ext_prop->type == USB_EXT_PROP_UNICODE ||
  807. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  808. ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
  809. len >>= 1;
  810. memcpy(page, ext_prop->data, len);
  811. return len;
  812. }
  813. static ssize_t ext_prop_data_store(struct config_item *item,
  814. const char *page, size_t len)
  815. {
  816. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  817. struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
  818. char *new_data;
  819. size_t ret_len = len;
  820. if (page[len - 1] == '\n' || page[len - 1] == '\0')
  821. --len;
  822. new_data = kmemdup(page, len, GFP_KERNEL);
  823. if (!new_data)
  824. return -ENOMEM;
  825. if (desc->opts_mutex)
  826. mutex_lock(desc->opts_mutex);
  827. kfree(ext_prop->data);
  828. ext_prop->data = new_data;
  829. desc->ext_prop_len -= ext_prop->data_len;
  830. ext_prop->data_len = len;
  831. desc->ext_prop_len += ext_prop->data_len;
  832. if (ext_prop->type == USB_EXT_PROP_UNICODE ||
  833. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  834. ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
  835. desc->ext_prop_len -= ext_prop->data_len;
  836. ext_prop->data_len <<= 1;
  837. ext_prop->data_len += 2;
  838. desc->ext_prop_len += ext_prop->data_len;
  839. }
  840. if (desc->opts_mutex)
  841. mutex_unlock(desc->opts_mutex);
  842. return ret_len;
  843. }
  844. CONFIGFS_ATTR(ext_prop_, type);
  845. CONFIGFS_ATTR(ext_prop_, data);
  846. static struct configfs_attribute *ext_prop_attrs[] = {
  847. &ext_prop_attr_type,
  848. &ext_prop_attr_data,
  849. NULL,
  850. };
  851. static void usb_os_desc_ext_prop_release(struct config_item *item)
  852. {
  853. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  854. kfree(ext_prop); /* frees a whole chunk */
  855. }
  856. static struct configfs_item_operations ext_prop_ops = {
  857. .release = usb_os_desc_ext_prop_release,
  858. };
  859. static struct config_item *ext_prop_make(
  860. struct config_group *group,
  861. const char *name)
  862. {
  863. struct usb_os_desc_ext_prop *ext_prop;
  864. struct config_item_type *ext_prop_type;
  865. struct usb_os_desc *desc;
  866. char *vlabuf;
  867. vla_group(data_chunk);
  868. vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
  869. vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
  870. vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
  871. if (!vlabuf)
  872. return ERR_PTR(-ENOMEM);
  873. ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
  874. ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
  875. desc = container_of(group, struct usb_os_desc, group);
  876. ext_prop_type->ct_item_ops = &ext_prop_ops;
  877. ext_prop_type->ct_attrs = ext_prop_attrs;
  878. ext_prop_type->ct_owner = desc->owner;
  879. config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
  880. ext_prop->name = kstrdup(name, GFP_KERNEL);
  881. if (!ext_prop->name) {
  882. kfree(vlabuf);
  883. return ERR_PTR(-ENOMEM);
  884. }
  885. desc->ext_prop_len += 14;
  886. ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
  887. if (desc->opts_mutex)
  888. mutex_lock(desc->opts_mutex);
  889. desc->ext_prop_len += ext_prop->name_len;
  890. list_add_tail(&ext_prop->entry, &desc->ext_prop);
  891. ++desc->ext_prop_count;
  892. if (desc->opts_mutex)
  893. mutex_unlock(desc->opts_mutex);
  894. return &ext_prop->item;
  895. }
  896. static void ext_prop_drop(struct config_group *group, struct config_item *item)
  897. {
  898. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  899. struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
  900. if (desc->opts_mutex)
  901. mutex_lock(desc->opts_mutex);
  902. list_del(&ext_prop->entry);
  903. --desc->ext_prop_count;
  904. kfree(ext_prop->name);
  905. desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
  906. if (desc->opts_mutex)
  907. mutex_unlock(desc->opts_mutex);
  908. config_item_put(item);
  909. }
  910. static struct configfs_group_operations interf_grp_ops = {
  911. .make_item = &ext_prop_make,
  912. .drop_item = &ext_prop_drop,
  913. };
  914. static ssize_t interf_grp_compatible_id_show(struct config_item *item,
  915. char *page)
  916. {
  917. memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
  918. return 8;
  919. }
  920. static ssize_t interf_grp_compatible_id_store(struct config_item *item,
  921. const char *page, size_t len)
  922. {
  923. struct usb_os_desc *desc = to_usb_os_desc(item);
  924. int l;
  925. l = min_t(int, 8, len);
  926. if (page[l - 1] == '\n')
  927. --l;
  928. if (desc->opts_mutex)
  929. mutex_lock(desc->opts_mutex);
  930. memcpy(desc->ext_compat_id, page, l);
  931. if (desc->opts_mutex)
  932. mutex_unlock(desc->opts_mutex);
  933. return len;
  934. }
  935. static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
  936. char *page)
  937. {
  938. memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
  939. return 8;
  940. }
  941. static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
  942. const char *page, size_t len)
  943. {
  944. struct usb_os_desc *desc = to_usb_os_desc(item);
  945. int l;
  946. l = min_t(int, 8, len);
  947. if (page[l - 1] == '\n')
  948. --l;
  949. if (desc->opts_mutex)
  950. mutex_lock(desc->opts_mutex);
  951. memcpy(desc->ext_compat_id + 8, page, l);
  952. if (desc->opts_mutex)
  953. mutex_unlock(desc->opts_mutex);
  954. return len;
  955. }
  956. CONFIGFS_ATTR(interf_grp_, compatible_id);
  957. CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
  958. static struct configfs_attribute *interf_grp_attrs[] = {
  959. &interf_grp_attr_compatible_id,
  960. &interf_grp_attr_sub_compatible_id,
  961. NULL
  962. };
  963. struct config_group *usb_os_desc_prepare_interf_dir(
  964. 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 ERR_PTR(-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 os_desc_group;
  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);