configfs.c 24 KB

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