configfs.c 38 KB

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