configfs.c 37 KB

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