configfs.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. #include <linux/configfs.h>
  2. #include <linux/module.h>
  3. #include <linux/slab.h>
  4. #include <linux/device.h>
  5. #include <linux/usb/composite.h>
  6. #include <linux/usb/gadget_configfs.h>
  7. int check_user_usb_string(const char *name,
  8. struct usb_gadget_strings *stringtab_dev)
  9. {
  10. unsigned primary_lang;
  11. unsigned sub_lang;
  12. u16 num;
  13. int ret;
  14. ret = kstrtou16(name, 0, &num);
  15. if (ret)
  16. return ret;
  17. primary_lang = num & 0x3ff;
  18. sub_lang = num >> 10;
  19. /* simple sanity check for valid langid */
  20. switch (primary_lang) {
  21. case 0:
  22. case 0x62 ... 0xfe:
  23. case 0x100 ... 0x3ff:
  24. return -EINVAL;
  25. }
  26. if (!sub_lang)
  27. return -EINVAL;
  28. stringtab_dev->language = num;
  29. return 0;
  30. }
  31. #define MAX_NAME_LEN 40
  32. #define MAX_USB_STRING_LANGS 2
  33. struct gadget_info {
  34. struct config_group group;
  35. struct config_group functions_group;
  36. struct config_group configs_group;
  37. struct config_group strings_group;
  38. struct config_group *default_groups[4];
  39. struct mutex lock;
  40. struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
  41. struct list_head string_list;
  42. struct list_head available_func;
  43. const char *udc_name;
  44. #ifdef CONFIG_USB_OTG
  45. struct usb_otg_descriptor otg;
  46. #endif
  47. struct usb_composite_driver composite;
  48. struct usb_composite_dev cdev;
  49. };
  50. struct config_usb_cfg {
  51. struct config_group group;
  52. struct config_group strings_group;
  53. struct config_group *default_groups[2];
  54. struct list_head string_list;
  55. struct usb_configuration c;
  56. struct list_head func_list;
  57. struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
  58. };
  59. struct gadget_strings {
  60. struct usb_gadget_strings stringtab_dev;
  61. struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
  62. char *manufacturer;
  63. char *product;
  64. char *serialnumber;
  65. struct config_group group;
  66. struct list_head list;
  67. };
  68. struct gadget_config_name {
  69. struct usb_gadget_strings stringtab_dev;
  70. struct usb_string strings;
  71. char *configuration;
  72. struct config_group group;
  73. struct list_head list;
  74. };
  75. static int usb_string_copy(const char *s, char **s_copy)
  76. {
  77. int ret;
  78. char *str;
  79. char *copy = *s_copy;
  80. ret = strlen(s);
  81. if (ret > 126)
  82. return -EOVERFLOW;
  83. str = kstrdup(s, GFP_KERNEL);
  84. if (!str)
  85. return -ENOMEM;
  86. if (str[ret - 1] == '\n')
  87. str[ret - 1] = '\0';
  88. kfree(copy);
  89. *s_copy = str;
  90. return 0;
  91. }
  92. CONFIGFS_ATTR_STRUCT(gadget_info);
  93. CONFIGFS_ATTR_STRUCT(config_usb_cfg);
  94. #define GI_DEVICE_DESC_ITEM_ATTR(name) \
  95. static struct gadget_info_attribute gadget_cdev_desc_##name = \
  96. __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
  97. gadget_dev_desc_##name##_show, \
  98. gadget_dev_desc_##name##_store)
  99. #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
  100. static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
  101. char *page) \
  102. { \
  103. return sprintf(page, "0x%02x\n", gi->cdev.desc.__name); \
  104. }
  105. #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
  106. static ssize_t gadget_dev_desc_##__name##_show(struct gadget_info *gi, \
  107. char *page) \
  108. { \
  109. return sprintf(page, "0x%04x\n", le16_to_cpup(&gi->cdev.desc.__name)); \
  110. }
  111. #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
  112. static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
  113. const char *page, size_t len) \
  114. { \
  115. u8 val; \
  116. int ret; \
  117. ret = kstrtou8(page, 0, &val); \
  118. if (ret) \
  119. return ret; \
  120. gi->cdev.desc._name = val; \
  121. return len; \
  122. }
  123. #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
  124. static ssize_t gadget_dev_desc_##_name##_store(struct gadget_info *gi, \
  125. const char *page, size_t len) \
  126. { \
  127. u16 val; \
  128. int ret; \
  129. ret = kstrtou16(page, 0, &val); \
  130. if (ret) \
  131. return ret; \
  132. gi->cdev.desc._name = cpu_to_le16p(&val); \
  133. return len; \
  134. }
  135. #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
  136. GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
  137. GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
  138. GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
  139. GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
  140. GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
  141. GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
  142. GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
  143. GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
  144. GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
  145. GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
  146. static ssize_t is_valid_bcd(u16 bcd_val)
  147. {
  148. if ((bcd_val & 0xf) > 9)
  149. return -EINVAL;
  150. if (((bcd_val >> 4) & 0xf) > 9)
  151. return -EINVAL;
  152. if (((bcd_val >> 8) & 0xf) > 9)
  153. return -EINVAL;
  154. if (((bcd_val >> 12) & 0xf) > 9)
  155. return -EINVAL;
  156. return 0;
  157. }
  158. static ssize_t gadget_dev_desc_bcdDevice_store(struct gadget_info *gi,
  159. const char *page, size_t len)
  160. {
  161. u16 bcdDevice;
  162. int ret;
  163. ret = kstrtou16(page, 0, &bcdDevice);
  164. if (ret)
  165. return ret;
  166. ret = is_valid_bcd(bcdDevice);
  167. if (ret)
  168. return ret;
  169. gi->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
  170. return len;
  171. }
  172. static ssize_t gadget_dev_desc_bcdUSB_store(struct gadget_info *gi,
  173. const char *page, size_t len)
  174. {
  175. u16 bcdUSB;
  176. int ret;
  177. ret = kstrtou16(page, 0, &bcdUSB);
  178. if (ret)
  179. return ret;
  180. ret = is_valid_bcd(bcdUSB);
  181. if (ret)
  182. return ret;
  183. gi->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
  184. return len;
  185. }
  186. static ssize_t gadget_dev_desc_UDC_show(struct gadget_info *gi, char *page)
  187. {
  188. return sprintf(page, "%s\n", gi->udc_name ?: "");
  189. }
  190. static int unregister_gadget(struct gadget_info *gi)
  191. {
  192. int ret;
  193. if (!gi->udc_name)
  194. return -ENODEV;
  195. ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
  196. if (ret)
  197. return ret;
  198. kfree(gi->udc_name);
  199. gi->udc_name = NULL;
  200. return 0;
  201. }
  202. static ssize_t gadget_dev_desc_UDC_store(struct gadget_info *gi,
  203. const char *page, size_t len)
  204. {
  205. char *name;
  206. int ret;
  207. name = kstrdup(page, GFP_KERNEL);
  208. if (!name)
  209. return -ENOMEM;
  210. if (name[len - 1] == '\n')
  211. name[len - 1] = '\0';
  212. mutex_lock(&gi->lock);
  213. if (!strlen(name)) {
  214. ret = unregister_gadget(gi);
  215. if (ret)
  216. goto err;
  217. } else {
  218. if (gi->udc_name) {
  219. ret = -EBUSY;
  220. goto err;
  221. }
  222. ret = udc_attach_driver(name, &gi->composite.gadget_driver);
  223. if (ret)
  224. goto err;
  225. gi->udc_name = name;
  226. }
  227. mutex_unlock(&gi->lock);
  228. return len;
  229. err:
  230. kfree(name);
  231. mutex_unlock(&gi->lock);
  232. return ret;
  233. }
  234. GI_DEVICE_DESC_ITEM_ATTR(bDeviceClass);
  235. GI_DEVICE_DESC_ITEM_ATTR(bDeviceSubClass);
  236. GI_DEVICE_DESC_ITEM_ATTR(bDeviceProtocol);
  237. GI_DEVICE_DESC_ITEM_ATTR(bMaxPacketSize0);
  238. GI_DEVICE_DESC_ITEM_ATTR(idVendor);
  239. GI_DEVICE_DESC_ITEM_ATTR(idProduct);
  240. GI_DEVICE_DESC_ITEM_ATTR(bcdDevice);
  241. GI_DEVICE_DESC_ITEM_ATTR(bcdUSB);
  242. GI_DEVICE_DESC_ITEM_ATTR(UDC);
  243. static struct configfs_attribute *gadget_root_attrs[] = {
  244. &gadget_cdev_desc_bDeviceClass.attr,
  245. &gadget_cdev_desc_bDeviceSubClass.attr,
  246. &gadget_cdev_desc_bDeviceProtocol.attr,
  247. &gadget_cdev_desc_bMaxPacketSize0.attr,
  248. &gadget_cdev_desc_idVendor.attr,
  249. &gadget_cdev_desc_idProduct.attr,
  250. &gadget_cdev_desc_bcdDevice.attr,
  251. &gadget_cdev_desc_bcdUSB.attr,
  252. &gadget_cdev_desc_UDC.attr,
  253. NULL,
  254. };
  255. static inline struct gadget_info *to_gadget_info(struct config_item *item)
  256. {
  257. return container_of(to_config_group(item), struct gadget_info, group);
  258. }
  259. static inline struct gadget_strings *to_gadget_strings(struct config_item *item)
  260. {
  261. return container_of(to_config_group(item), struct gadget_strings,
  262. group);
  263. }
  264. static inline struct gadget_config_name *to_gadget_config_name(
  265. struct config_item *item)
  266. {
  267. return container_of(to_config_group(item), struct gadget_config_name,
  268. group);
  269. }
  270. static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
  271. {
  272. return container_of(to_config_group(item), struct config_usb_cfg,
  273. group);
  274. }
  275. static inline struct usb_function_instance *to_usb_function_instance(
  276. struct config_item *item)
  277. {
  278. return container_of(to_config_group(item),
  279. struct usb_function_instance, group);
  280. }
  281. static void gadget_info_attr_release(struct config_item *item)
  282. {
  283. struct gadget_info *gi = to_gadget_info(item);
  284. WARN_ON(!list_empty(&gi->cdev.configs));
  285. WARN_ON(!list_empty(&gi->string_list));
  286. WARN_ON(!list_empty(&gi->available_func));
  287. kfree(gi->composite.gadget_driver.function);
  288. kfree(gi);
  289. }
  290. CONFIGFS_ATTR_OPS(gadget_info);
  291. static struct configfs_item_operations gadget_root_item_ops = {
  292. .release = gadget_info_attr_release,
  293. .show_attribute = gadget_info_attr_show,
  294. .store_attribute = gadget_info_attr_store,
  295. };
  296. static void gadget_config_attr_release(struct config_item *item)
  297. {
  298. struct config_usb_cfg *cfg = to_config_usb_cfg(item);
  299. WARN_ON(!list_empty(&cfg->c.functions));
  300. list_del(&cfg->c.list);
  301. kfree(cfg->c.label);
  302. kfree(cfg);
  303. }
  304. static int config_usb_cfg_link(
  305. struct config_item *usb_cfg_ci,
  306. struct config_item *usb_func_ci)
  307. {
  308. struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
  309. struct usb_composite_dev *cdev = cfg->c.cdev;
  310. struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
  311. struct config_group *group = to_config_group(usb_func_ci);
  312. struct usb_function_instance *fi = container_of(group,
  313. struct usb_function_instance, group);
  314. struct usb_function_instance *a_fi;
  315. struct usb_function *f;
  316. int ret;
  317. mutex_lock(&gi->lock);
  318. /*
  319. * Make sure this function is from within our _this_ gadget and not
  320. * from another gadget or a random directory.
  321. * Also a function instance can only be linked once.
  322. */
  323. list_for_each_entry(a_fi, &gi->available_func, cfs_list) {
  324. if (a_fi == fi)
  325. break;
  326. }
  327. if (a_fi != fi) {
  328. ret = -EINVAL;
  329. goto out;
  330. }
  331. list_for_each_entry(f, &cfg->func_list, list) {
  332. if (f->fi == fi) {
  333. ret = -EEXIST;
  334. goto out;
  335. }
  336. }
  337. f = usb_get_function(fi);
  338. if (IS_ERR(f)) {
  339. ret = PTR_ERR(f);
  340. goto out;
  341. }
  342. /* stash the function until we bind it to the gadget */
  343. list_add_tail(&f->list, &cfg->func_list);
  344. ret = 0;
  345. out:
  346. mutex_unlock(&gi->lock);
  347. return ret;
  348. }
  349. static int config_usb_cfg_unlink(
  350. struct config_item *usb_cfg_ci,
  351. struct config_item *usb_func_ci)
  352. {
  353. struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
  354. struct usb_composite_dev *cdev = cfg->c.cdev;
  355. struct gadget_info *gi = container_of(cdev, struct gadget_info, cdev);
  356. struct config_group *group = to_config_group(usb_func_ci);
  357. struct usb_function_instance *fi = container_of(group,
  358. struct usb_function_instance, group);
  359. struct usb_function *f;
  360. /*
  361. * ideally I would like to forbid to unlink functions while a gadget is
  362. * bound to an UDC. Since this isn't possible at the moment, we simply
  363. * force an unbind, the function is available here and then we can
  364. * remove the function.
  365. */
  366. mutex_lock(&gi->lock);
  367. if (gi->udc_name)
  368. unregister_gadget(gi);
  369. WARN_ON(gi->udc_name);
  370. list_for_each_entry(f, &cfg->func_list, list) {
  371. if (f->fi == fi) {
  372. list_del(&f->list);
  373. usb_put_function(f);
  374. mutex_unlock(&gi->lock);
  375. return 0;
  376. }
  377. }
  378. mutex_unlock(&gi->lock);
  379. WARN(1, "Unable to locate function to unbind\n");
  380. return 0;
  381. }
  382. CONFIGFS_ATTR_OPS(config_usb_cfg);
  383. static struct configfs_item_operations gadget_config_item_ops = {
  384. .release = gadget_config_attr_release,
  385. .show_attribute = config_usb_cfg_attr_show,
  386. .store_attribute = config_usb_cfg_attr_store,
  387. .allow_link = config_usb_cfg_link,
  388. .drop_link = config_usb_cfg_unlink,
  389. };
  390. static ssize_t gadget_config_desc_MaxPower_show(struct config_usb_cfg *cfg,
  391. char *page)
  392. {
  393. return sprintf(page, "%u\n", cfg->c.MaxPower);
  394. }
  395. static ssize_t gadget_config_desc_MaxPower_store(struct config_usb_cfg *cfg,
  396. const char *page, size_t len)
  397. {
  398. u16 val;
  399. int ret;
  400. ret = kstrtou16(page, 0, &val);
  401. if (ret)
  402. return ret;
  403. if (DIV_ROUND_UP(val, 8) > 0xff)
  404. return -ERANGE;
  405. cfg->c.MaxPower = val;
  406. return len;
  407. }
  408. static ssize_t gadget_config_desc_bmAttributes_show(struct config_usb_cfg *cfg,
  409. char *page)
  410. {
  411. return sprintf(page, "0x%02x\n", cfg->c.bmAttributes);
  412. }
  413. static ssize_t gadget_config_desc_bmAttributes_store(struct config_usb_cfg *cfg,
  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. cfg->c.bmAttributes = val;
  427. return len;
  428. }
  429. #define CFG_CONFIG_DESC_ITEM_ATTR(name) \
  430. static struct config_usb_cfg_attribute gadget_usb_cfg_##name = \
  431. __CONFIGFS_ATTR(name, S_IRUGO | S_IWUSR, \
  432. gadget_config_desc_##name##_show, \
  433. gadget_config_desc_##name##_store)
  434. CFG_CONFIG_DESC_ITEM_ATTR(MaxPower);
  435. CFG_CONFIG_DESC_ITEM_ATTR(bmAttributes);
  436. static struct configfs_attribute *gadget_config_attrs[] = {
  437. &gadget_usb_cfg_MaxPower.attr,
  438. &gadget_usb_cfg_bmAttributes.attr,
  439. NULL,
  440. };
  441. static struct config_item_type gadget_config_type = {
  442. .ct_item_ops = &gadget_config_item_ops,
  443. .ct_attrs = gadget_config_attrs,
  444. .ct_owner = THIS_MODULE,
  445. };
  446. static struct config_item_type gadget_root_type = {
  447. .ct_item_ops = &gadget_root_item_ops,
  448. .ct_attrs = gadget_root_attrs,
  449. .ct_owner = THIS_MODULE,
  450. };
  451. static void composite_init_dev(struct usb_composite_dev *cdev)
  452. {
  453. spin_lock_init(&cdev->lock);
  454. INIT_LIST_HEAD(&cdev->configs);
  455. INIT_LIST_HEAD(&cdev->gstrings);
  456. }
  457. static struct config_group *function_make(
  458. struct config_group *group,
  459. const char *name)
  460. {
  461. struct gadget_info *gi;
  462. struct usb_function_instance *fi;
  463. char buf[MAX_NAME_LEN];
  464. char *func_name;
  465. char *instance_name;
  466. int ret;
  467. ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
  468. if (ret >= MAX_NAME_LEN)
  469. return ERR_PTR(-ENAMETOOLONG);
  470. func_name = buf;
  471. instance_name = strchr(func_name, '.');
  472. if (!instance_name) {
  473. pr_err("Unable to locate . in FUNC.INSTANCE\n");
  474. return ERR_PTR(-EINVAL);
  475. }
  476. *instance_name = '\0';
  477. instance_name++;
  478. fi = usb_get_function_instance(func_name);
  479. if (IS_ERR(fi))
  480. return ERR_PTR(PTR_ERR(fi));
  481. ret = config_item_set_name(&fi->group.cg_item, name);
  482. if (ret) {
  483. usb_put_function_instance(fi);
  484. return ERR_PTR(ret);
  485. }
  486. gi = container_of(group, struct gadget_info, functions_group);
  487. mutex_lock(&gi->lock);
  488. list_add_tail(&fi->cfs_list, &gi->available_func);
  489. mutex_unlock(&gi->lock);
  490. return &fi->group;
  491. }
  492. static void function_drop(
  493. struct config_group *group,
  494. struct config_item *item)
  495. {
  496. struct usb_function_instance *fi = to_usb_function_instance(item);
  497. struct gadget_info *gi;
  498. gi = container_of(group, struct gadget_info, functions_group);
  499. mutex_lock(&gi->lock);
  500. list_del(&fi->cfs_list);
  501. mutex_unlock(&gi->lock);
  502. config_item_put(item);
  503. }
  504. static struct configfs_group_operations functions_ops = {
  505. .make_group = &function_make,
  506. .drop_item = &function_drop,
  507. };
  508. static struct config_item_type functions_type = {
  509. .ct_group_ops = &functions_ops,
  510. .ct_owner = THIS_MODULE,
  511. };
  512. CONFIGFS_ATTR_STRUCT(gadget_config_name);
  513. GS_STRINGS_RW(gadget_config_name, configuration);
  514. static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
  515. &gadget_config_name_configuration.attr,
  516. NULL,
  517. };
  518. static void gadget_config_name_attr_release(struct config_item *item)
  519. {
  520. struct gadget_config_name *cn = to_gadget_config_name(item);
  521. kfree(cn->configuration);
  522. list_del(&cn->list);
  523. kfree(cn);
  524. }
  525. USB_CONFIG_STRING_RW_OPS(gadget_config_name);
  526. USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
  527. static struct config_group *config_desc_make(
  528. struct config_group *group,
  529. const char *name)
  530. {
  531. struct gadget_info *gi;
  532. struct config_usb_cfg *cfg;
  533. char buf[MAX_NAME_LEN];
  534. char *num_str;
  535. u8 num;
  536. int ret;
  537. gi = container_of(group, struct gadget_info, configs_group);
  538. ret = snprintf(buf, MAX_NAME_LEN, "%s", name);
  539. if (ret >= MAX_NAME_LEN)
  540. return ERR_PTR(-ENAMETOOLONG);
  541. num_str = strchr(buf, '.');
  542. if (!num_str) {
  543. pr_err("Unable to locate . in name.bConfigurationValue\n");
  544. return ERR_PTR(-EINVAL);
  545. }
  546. *num_str = '\0';
  547. num_str++;
  548. if (!strlen(buf))
  549. return ERR_PTR(-EINVAL);
  550. ret = kstrtou8(num_str, 0, &num);
  551. if (ret)
  552. return ERR_PTR(ret);
  553. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  554. if (!cfg)
  555. return ERR_PTR(-ENOMEM);
  556. cfg->c.label = kstrdup(buf, GFP_KERNEL);
  557. if (!cfg->c.label) {
  558. ret = -ENOMEM;
  559. goto err;
  560. }
  561. cfg->c.bConfigurationValue = num;
  562. cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
  563. cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
  564. INIT_LIST_HEAD(&cfg->string_list);
  565. INIT_LIST_HEAD(&cfg->func_list);
  566. cfg->group.default_groups = cfg->default_groups;
  567. cfg->default_groups[0] = &cfg->strings_group;
  568. config_group_init_type_name(&cfg->group, name,
  569. &gadget_config_type);
  570. config_group_init_type_name(&cfg->strings_group, "strings",
  571. &gadget_config_name_strings_type);
  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. CONFIGFS_ATTR_STRUCT(gadget_strings);
  596. GS_STRINGS_RW(gadget_strings, manufacturer);
  597. GS_STRINGS_RW(gadget_strings, product);
  598. GS_STRINGS_RW(gadget_strings, serialnumber);
  599. static struct configfs_attribute *gadget_strings_langid_attrs[] = {
  600. &gadget_strings_manufacturer.attr,
  601. &gadget_strings_product.attr,
  602. &gadget_strings_serialnumber.attr,
  603. NULL,
  604. };
  605. static void gadget_strings_attr_release(struct config_item *item)
  606. {
  607. struct gadget_strings *gs = to_gadget_strings(item);
  608. kfree(gs->manufacturer);
  609. kfree(gs->product);
  610. kfree(gs->serialnumber);
  611. list_del(&gs->list);
  612. kfree(gs);
  613. }
  614. USB_CONFIG_STRING_RW_OPS(gadget_strings);
  615. USB_CONFIG_STRINGS_LANG(gadget_strings, gadget_info);
  616. static int configfs_do_nothing(struct usb_composite_dev *cdev)
  617. {
  618. WARN_ON(1);
  619. return -EINVAL;
  620. }
  621. int composite_dev_prepare(struct usb_composite_driver *composite,
  622. struct usb_composite_dev *dev);
  623. static void purge_configs_funcs(struct gadget_info *gi)
  624. {
  625. struct usb_configuration *c;
  626. list_for_each_entry(c, &gi->cdev.configs, list) {
  627. struct usb_function *f, *tmp;
  628. struct config_usb_cfg *cfg;
  629. cfg = container_of(c, struct config_usb_cfg, c);
  630. list_for_each_entry_safe(f, tmp, &c->functions, list) {
  631. list_move_tail(&f->list, &cfg->func_list);
  632. if (f->unbind) {
  633. dev_err(&gi->cdev.gadget->dev, "unbind function"
  634. " '%s'/%p\n", f->name, f);
  635. f->unbind(c, f);
  636. }
  637. }
  638. c->next_interface_id = 0;
  639. c->superspeed = 0;
  640. c->highspeed = 0;
  641. c->fullspeed = 0;
  642. }
  643. }
  644. static int configfs_composite_bind(struct usb_gadget *gadget,
  645. struct usb_gadget_driver *gdriver)
  646. {
  647. struct usb_composite_driver *composite = to_cdriver(gdriver);
  648. struct gadget_info *gi = container_of(composite,
  649. struct gadget_info, composite);
  650. struct usb_composite_dev *cdev = &gi->cdev;
  651. struct usb_configuration *c;
  652. struct usb_string *s;
  653. unsigned i;
  654. int ret;
  655. /* the gi->lock is hold by the caller */
  656. cdev->gadget = gadget;
  657. set_gadget_data(gadget, cdev);
  658. ret = composite_dev_prepare(composite, cdev);
  659. if (ret)
  660. return ret;
  661. /* and now the gadget bind */
  662. ret = -EINVAL;
  663. if (list_empty(&gi->cdev.configs)) {
  664. pr_err("Need atleast one configuration in %s.\n",
  665. gi->composite.name);
  666. goto err_comp_cleanup;
  667. }
  668. list_for_each_entry(c, &gi->cdev.configs, list) {
  669. struct config_usb_cfg *cfg;
  670. cfg = container_of(c, struct config_usb_cfg, c);
  671. if (list_empty(&cfg->func_list)) {
  672. pr_err("Config %s/%d of %s needs atleast one function.\n",
  673. c->label, c->bConfigurationValue,
  674. gi->composite.name);
  675. goto err_comp_cleanup;
  676. }
  677. }
  678. /* init all strings */
  679. if (!list_empty(&gi->string_list)) {
  680. struct gadget_strings *gs;
  681. i = 0;
  682. list_for_each_entry(gs, &gi->string_list, list) {
  683. gi->gstrings[i] = &gs->stringtab_dev;
  684. gs->stringtab_dev.strings = gs->strings;
  685. gs->strings[USB_GADGET_MANUFACTURER_IDX].s =
  686. gs->manufacturer;
  687. gs->strings[USB_GADGET_PRODUCT_IDX].s = gs->product;
  688. gs->strings[USB_GADGET_SERIAL_IDX].s = gs->serialnumber;
  689. i++;
  690. }
  691. gi->gstrings[i] = NULL;
  692. s = usb_gstrings_attach(&gi->cdev, gi->gstrings,
  693. USB_GADGET_FIRST_AVAIL_IDX);
  694. if (IS_ERR(s)) {
  695. ret = PTR_ERR(s);
  696. goto err_comp_cleanup;
  697. }
  698. gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
  699. gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
  700. gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
  701. }
  702. /* Go through all configs, attach all functions */
  703. list_for_each_entry(c, &gi->cdev.configs, list) {
  704. struct config_usb_cfg *cfg;
  705. struct usb_function *f;
  706. struct usb_function *tmp;
  707. struct gadget_config_name *cn;
  708. cfg = container_of(c, struct config_usb_cfg, c);
  709. if (!list_empty(&cfg->string_list)) {
  710. i = 0;
  711. list_for_each_entry(cn, &cfg->string_list, list) {
  712. cfg->gstrings[i] = &cn->stringtab_dev;
  713. cn->stringtab_dev.strings = &cn->strings;
  714. cn->strings.s = cn->configuration;
  715. i++;
  716. }
  717. cfg->gstrings[i] = NULL;
  718. s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
  719. if (IS_ERR(s)) {
  720. ret = PTR_ERR(s);
  721. goto err_comp_cleanup;
  722. }
  723. c->iConfiguration = s[0].id;
  724. }
  725. list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
  726. list_del(&f->list);
  727. ret = usb_add_function(c, f);
  728. if (ret)
  729. goto err_purge_funcs;
  730. }
  731. usb_ep_autoconfig_reset(cdev->gadget);
  732. }
  733. usb_ep_autoconfig_reset(cdev->gadget);
  734. return 0;
  735. err_purge_funcs:
  736. purge_configs_funcs(gi);
  737. err_comp_cleanup:
  738. composite_dev_cleanup(cdev);
  739. return ret;
  740. }
  741. static void configfs_composite_unbind(struct usb_gadget *gadget)
  742. {
  743. struct usb_composite_dev *cdev;
  744. struct gadget_info *gi;
  745. /* the gi->lock is hold by the caller */
  746. cdev = get_gadget_data(gadget);
  747. gi = container_of(cdev, struct gadget_info, cdev);
  748. purge_configs_funcs(gi);
  749. composite_dev_cleanup(cdev);
  750. usb_ep_autoconfig_reset(cdev->gadget);
  751. cdev->gadget = NULL;
  752. set_gadget_data(gadget, NULL);
  753. }
  754. static const struct usb_gadget_driver configfs_driver_template = {
  755. .bind = configfs_composite_bind,
  756. .unbind = configfs_composite_unbind,
  757. .setup = composite_setup,
  758. .disconnect = composite_disconnect,
  759. .max_speed = USB_SPEED_SUPER,
  760. .driver = {
  761. .owner = THIS_MODULE,
  762. .name = "configfs-gadget",
  763. },
  764. };
  765. static struct config_group *gadgets_make(
  766. struct config_group *group,
  767. const char *name)
  768. {
  769. struct gadget_info *gi;
  770. gi = kzalloc(sizeof(*gi), GFP_KERNEL);
  771. if (!gi)
  772. return ERR_PTR(-ENOMEM);
  773. gi->group.default_groups = gi->default_groups;
  774. gi->group.default_groups[0] = &gi->functions_group;
  775. gi->group.default_groups[1] = &gi->configs_group;
  776. gi->group.default_groups[2] = &gi->strings_group;
  777. config_group_init_type_name(&gi->functions_group, "functions",
  778. &functions_type);
  779. config_group_init_type_name(&gi->configs_group, "configs",
  780. &config_desc_type);
  781. config_group_init_type_name(&gi->strings_group, "strings",
  782. &gadget_strings_strings_type);
  783. gi->composite.bind = configfs_do_nothing;
  784. gi->composite.unbind = configfs_do_nothing;
  785. gi->composite.suspend = NULL;
  786. gi->composite.resume = NULL;
  787. gi->composite.max_speed = USB_SPEED_SUPER;
  788. mutex_init(&gi->lock);
  789. INIT_LIST_HEAD(&gi->string_list);
  790. INIT_LIST_HEAD(&gi->available_func);
  791. composite_init_dev(&gi->cdev);
  792. gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
  793. gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
  794. gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
  795. gi->composite.gadget_driver = configfs_driver_template;
  796. gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
  797. gi->composite.name = gi->composite.gadget_driver.function;
  798. if (!gi->composite.gadget_driver.function)
  799. goto err;
  800. #ifdef CONFIG_USB_OTG
  801. gi->otg.bLength = sizeof(struct usb_otg_descriptor);
  802. gi->otg.bDescriptorType = USB_DT_OTG;
  803. gi->otg.bmAttributes = USB_OTG_SRP | USB_OTG_HNP;
  804. #endif
  805. config_group_init_type_name(&gi->group, name,
  806. &gadget_root_type);
  807. return &gi->group;
  808. err:
  809. kfree(gi);
  810. return ERR_PTR(-ENOMEM);
  811. }
  812. static void gadgets_drop(struct config_group *group, struct config_item *item)
  813. {
  814. config_item_put(item);
  815. }
  816. static struct configfs_group_operations gadgets_ops = {
  817. .make_group = &gadgets_make,
  818. .drop_item = &gadgets_drop,
  819. };
  820. static struct config_item_type gadgets_type = {
  821. .ct_group_ops = &gadgets_ops,
  822. .ct_owner = THIS_MODULE,
  823. };
  824. static struct configfs_subsystem gadget_subsys = {
  825. .su_group = {
  826. .cg_item = {
  827. .ci_namebuf = "usb_gadget",
  828. .ci_type = &gadgets_type,
  829. },
  830. },
  831. .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
  832. };
  833. static int __init gadget_cfs_init(void)
  834. {
  835. int ret;
  836. config_group_init(&gadget_subsys.su_group);
  837. ret = configfs_register_subsystem(&gadget_subsys);
  838. return ret;
  839. }
  840. module_init(gadget_cfs_init);
  841. static void __exit gadget_cfs_exit(void)
  842. {
  843. configfs_unregister_subsystem(&gadget_subsys);
  844. }
  845. module_exit(gadget_cfs_exit);