configfs.c 39 KB

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