configfs.c 40 KB

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