configfs.c 38 KB

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