configfs.c 24 KB

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