configfs.c 39 KB

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