configfs.c 37 KB

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