configfs.c 37 KB

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