configfs.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/configfs.h>
  3. #include <linux/module.h>
  4. #include <linux/slab.h>
  5. #include <linux/device.h>
  6. #include <linux/kstrtox.h>
  7. #include <linux/nls.h>
  8. #include <linux/usb/composite.h>
  9. #include <linux/usb/func_utils.h>
  10. #include <linux/usb/gadget_configfs.h>
  11. #include <linux/usb/webusb.h>
  12. #include "configfs.h"
  13. #include "u_os_desc.h"
  14. static int check_user_usb_string(const char *name,
  15. struct usb_gadget_strings *stringtab_dev)
  16. {
  17. u16 num;
  18. int ret;
  19. ret = kstrtou16(name, 0, &num);
  20. if (ret)
  21. return ret;
  22. if (!usb_validate_langid(num))
  23. return -EINVAL;
  24. stringtab_dev->language = num;
  25. return 0;
  26. }
  27. #define MAX_NAME_LEN 40
  28. #define MAX_USB_STRING_LANGS 2
  29. static const struct usb_descriptor_header *otg_desc[2];
  30. struct gadget_info {
  31. struct config_group group;
  32. struct config_group functions_group;
  33. struct config_group configs_group;
  34. struct config_group strings_group;
  35. struct config_group os_desc_group;
  36. struct config_group webusb_group;
  37. struct mutex lock;
  38. struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
  39. struct list_head string_list;
  40. struct list_head available_func;
  41. struct usb_composite_driver composite;
  42. struct usb_composite_dev cdev;
  43. bool use_os_desc;
  44. char b_vendor_code;
  45. char qw_sign[OS_STRING_QW_SIGN_LEN];
  46. bool use_webusb;
  47. u16 bcd_webusb_version;
  48. u8 b_webusb_vendor_code;
  49. char landing_page[WEBUSB_URL_RAW_MAX_LENGTH];
  50. spinlock_t spinlock;
  51. bool unbind;
  52. };
  53. static inline struct gadget_info *to_gadget_info(struct config_item *item)
  54. {
  55. return container_of(to_config_group(item), struct gadget_info, group);
  56. }
  57. struct config_usb_cfg {
  58. struct config_group group;
  59. struct config_group strings_group;
  60. struct list_head string_list;
  61. struct usb_configuration c;
  62. struct list_head func_list;
  63. struct usb_gadget_strings *gstrings[MAX_USB_STRING_LANGS + 1];
  64. };
  65. static inline struct config_usb_cfg *to_config_usb_cfg(struct config_item *item)
  66. {
  67. return container_of(to_config_group(item), struct config_usb_cfg,
  68. group);
  69. }
  70. static inline struct gadget_info *cfg_to_gadget_info(struct config_usb_cfg *cfg)
  71. {
  72. return container_of(cfg->c.cdev, struct gadget_info, cdev);
  73. }
  74. struct gadget_language {
  75. struct usb_gadget_strings stringtab_dev;
  76. struct usb_string strings[USB_GADGET_FIRST_AVAIL_IDX];
  77. char *manufacturer;
  78. char *product;
  79. char *serialnumber;
  80. struct config_group group;
  81. struct list_head list;
  82. struct list_head gadget_strings;
  83. unsigned int nstrings;
  84. };
  85. struct gadget_config_name {
  86. struct usb_gadget_strings stringtab_dev;
  87. struct usb_string strings;
  88. char *configuration;
  89. struct config_group group;
  90. struct list_head list;
  91. };
  92. #define USB_MAX_STRING_WITH_NULL_LEN (USB_MAX_STRING_LEN+1)
  93. static int usb_string_copy(const char *s, char **s_copy)
  94. {
  95. int ret;
  96. char *str;
  97. char *copy = *s_copy;
  98. ret = strlen(s);
  99. if (ret > USB_MAX_STRING_LEN)
  100. return -EOVERFLOW;
  101. if (ret < 1)
  102. return -EINVAL;
  103. if (copy) {
  104. str = copy;
  105. } else {
  106. str = kmalloc(USB_MAX_STRING_WITH_NULL_LEN, GFP_KERNEL);
  107. if (!str)
  108. return -ENOMEM;
  109. }
  110. strcpy(str, s);
  111. if (str[ret - 1] == '\n')
  112. str[ret - 1] = '\0';
  113. *s_copy = str;
  114. return 0;
  115. }
  116. #define GI_DEVICE_DESC_SIMPLE_R_u8(__name) \
  117. static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
  118. char *page) \
  119. { \
  120. return sprintf(page, "0x%02x\n", \
  121. to_gadget_info(item)->cdev.desc.__name); \
  122. }
  123. #define GI_DEVICE_DESC_SIMPLE_R_u16(__name) \
  124. static ssize_t gadget_dev_desc_##__name##_show(struct config_item *item, \
  125. char *page) \
  126. { \
  127. return sprintf(page, "0x%04x\n", \
  128. le16_to_cpup(&to_gadget_info(item)->cdev.desc.__name)); \
  129. }
  130. #define GI_DEVICE_DESC_SIMPLE_W_u8(_name) \
  131. static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
  132. const char *page, size_t len) \
  133. { \
  134. u8 val; \
  135. int ret; \
  136. ret = kstrtou8(page, 0, &val); \
  137. if (ret) \
  138. return ret; \
  139. to_gadget_info(item)->cdev.desc._name = val; \
  140. return len; \
  141. }
  142. #define GI_DEVICE_DESC_SIMPLE_W_u16(_name) \
  143. static ssize_t gadget_dev_desc_##_name##_store(struct config_item *item, \
  144. const char *page, size_t len) \
  145. { \
  146. u16 val; \
  147. int ret; \
  148. ret = kstrtou16(page, 0, &val); \
  149. if (ret) \
  150. return ret; \
  151. to_gadget_info(item)->cdev.desc._name = cpu_to_le16p(&val); \
  152. return len; \
  153. }
  154. #define GI_DEVICE_DESC_SIMPLE_RW(_name, _type) \
  155. GI_DEVICE_DESC_SIMPLE_R_##_type(_name) \
  156. GI_DEVICE_DESC_SIMPLE_W_##_type(_name)
  157. GI_DEVICE_DESC_SIMPLE_R_u16(bcdUSB);
  158. GI_DEVICE_DESC_SIMPLE_RW(bDeviceClass, u8);
  159. GI_DEVICE_DESC_SIMPLE_RW(bDeviceSubClass, u8);
  160. GI_DEVICE_DESC_SIMPLE_RW(bDeviceProtocol, u8);
  161. GI_DEVICE_DESC_SIMPLE_RW(bMaxPacketSize0, u8);
  162. GI_DEVICE_DESC_SIMPLE_RW(idVendor, u16);
  163. GI_DEVICE_DESC_SIMPLE_RW(idProduct, u16);
  164. GI_DEVICE_DESC_SIMPLE_R_u16(bcdDevice);
  165. static ssize_t is_valid_bcd(u16 bcd_val)
  166. {
  167. if ((bcd_val & 0xf) > 9)
  168. return -EINVAL;
  169. if (((bcd_val >> 4) & 0xf) > 9)
  170. return -EINVAL;
  171. if (((bcd_val >> 8) & 0xf) > 9)
  172. return -EINVAL;
  173. if (((bcd_val >> 12) & 0xf) > 9)
  174. return -EINVAL;
  175. return 0;
  176. }
  177. static ssize_t gadget_dev_desc_bcdDevice_store(struct config_item *item,
  178. const char *page, size_t len)
  179. {
  180. u16 bcdDevice;
  181. int ret;
  182. ret = kstrtou16(page, 0, &bcdDevice);
  183. if (ret)
  184. return ret;
  185. ret = is_valid_bcd(bcdDevice);
  186. if (ret)
  187. return ret;
  188. to_gadget_info(item)->cdev.desc.bcdDevice = cpu_to_le16(bcdDevice);
  189. return len;
  190. }
  191. static ssize_t gadget_dev_desc_bcdUSB_store(struct config_item *item,
  192. const char *page, size_t len)
  193. {
  194. u16 bcdUSB;
  195. int ret;
  196. ret = kstrtou16(page, 0, &bcdUSB);
  197. if (ret)
  198. return ret;
  199. ret = is_valid_bcd(bcdUSB);
  200. if (ret)
  201. return ret;
  202. to_gadget_info(item)->cdev.desc.bcdUSB = cpu_to_le16(bcdUSB);
  203. return len;
  204. }
  205. static ssize_t gadget_dev_desc_UDC_show(struct config_item *item, char *page)
  206. {
  207. struct gadget_info *gi = to_gadget_info(item);
  208. char *udc_name;
  209. int ret;
  210. mutex_lock(&gi->lock);
  211. udc_name = gi->composite.gadget_driver.udc_name;
  212. ret = sprintf(page, "%s\n", udc_name ?: "");
  213. mutex_unlock(&gi->lock);
  214. return ret;
  215. }
  216. static int unregister_gadget(struct gadget_info *gi)
  217. {
  218. int ret;
  219. if (!gi->composite.gadget_driver.udc_name)
  220. return -ENODEV;
  221. ret = usb_gadget_unregister_driver(&gi->composite.gadget_driver);
  222. if (ret)
  223. return ret;
  224. kfree(gi->composite.gadget_driver.udc_name);
  225. gi->composite.gadget_driver.udc_name = NULL;
  226. return 0;
  227. }
  228. static ssize_t gadget_dev_desc_UDC_store(struct config_item *item,
  229. const char *page, size_t len)
  230. {
  231. struct gadget_info *gi = to_gadget_info(item);
  232. char *name;
  233. int ret;
  234. if (strlen(page) < len)
  235. return -EOVERFLOW;
  236. name = kstrdup(page, GFP_KERNEL);
  237. if (!name)
  238. return -ENOMEM;
  239. if (name[len - 1] == '\n')
  240. name[len - 1] = '\0';
  241. mutex_lock(&gi->lock);
  242. if (!strlen(name)) {
  243. ret = unregister_gadget(gi);
  244. if (ret)
  245. goto err;
  246. kfree(name);
  247. } else {
  248. if (gi->composite.gadget_driver.udc_name) {
  249. ret = -EBUSY;
  250. goto err;
  251. }
  252. gi->composite.gadget_driver.udc_name = name;
  253. ret = usb_gadget_register_driver(&gi->composite.gadget_driver);
  254. if (ret) {
  255. gi->composite.gadget_driver.udc_name = NULL;
  256. goto err;
  257. }
  258. }
  259. mutex_unlock(&gi->lock);
  260. return len;
  261. err:
  262. kfree(name);
  263. mutex_unlock(&gi->lock);
  264. return ret;
  265. }
  266. static ssize_t gadget_dev_desc_max_speed_show(struct config_item *item,
  267. char *page)
  268. {
  269. enum usb_device_speed speed = to_gadget_info(item)->composite.max_speed;
  270. return sprintf(page, "%s\n", usb_speed_string(speed));
  271. }
  272. static ssize_t gadget_dev_desc_max_speed_store(struct config_item *item,
  273. const char *page, size_t len)
  274. {
  275. struct gadget_info *gi = to_gadget_info(item);
  276. mutex_lock(&gi->lock);
  277. /* Prevent changing of max_speed after the driver is binded */
  278. if (gi->composite.gadget_driver.udc_name)
  279. goto err;
  280. if (strncmp(page, "super-speed-plus", 16) == 0)
  281. gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
  282. else if (strncmp(page, "super-speed", 11) == 0)
  283. gi->composite.max_speed = USB_SPEED_SUPER;
  284. else if (strncmp(page, "high-speed", 10) == 0)
  285. gi->composite.max_speed = USB_SPEED_HIGH;
  286. else if (strncmp(page, "full-speed", 10) == 0)
  287. gi->composite.max_speed = USB_SPEED_FULL;
  288. else if (strncmp(page, "low-speed", 9) == 0)
  289. gi->composite.max_speed = USB_SPEED_LOW;
  290. else
  291. goto err;
  292. gi->composite.gadget_driver.max_speed = gi->composite.max_speed;
  293. mutex_unlock(&gi->lock);
  294. return len;
  295. err:
  296. mutex_unlock(&gi->lock);
  297. return -EINVAL;
  298. }
  299. CONFIGFS_ATTR(gadget_dev_desc_, bDeviceClass);
  300. CONFIGFS_ATTR(gadget_dev_desc_, bDeviceSubClass);
  301. CONFIGFS_ATTR(gadget_dev_desc_, bDeviceProtocol);
  302. CONFIGFS_ATTR(gadget_dev_desc_, bMaxPacketSize0);
  303. CONFIGFS_ATTR(gadget_dev_desc_, idVendor);
  304. CONFIGFS_ATTR(gadget_dev_desc_, idProduct);
  305. CONFIGFS_ATTR(gadget_dev_desc_, bcdDevice);
  306. CONFIGFS_ATTR(gadget_dev_desc_, bcdUSB);
  307. CONFIGFS_ATTR(gadget_dev_desc_, UDC);
  308. CONFIGFS_ATTR(gadget_dev_desc_, max_speed);
  309. static struct configfs_attribute *gadget_root_attrs[] = {
  310. &gadget_dev_desc_attr_bDeviceClass,
  311. &gadget_dev_desc_attr_bDeviceSubClass,
  312. &gadget_dev_desc_attr_bDeviceProtocol,
  313. &gadget_dev_desc_attr_bMaxPacketSize0,
  314. &gadget_dev_desc_attr_idVendor,
  315. &gadget_dev_desc_attr_idProduct,
  316. &gadget_dev_desc_attr_bcdDevice,
  317. &gadget_dev_desc_attr_bcdUSB,
  318. &gadget_dev_desc_attr_UDC,
  319. &gadget_dev_desc_attr_max_speed,
  320. NULL,
  321. };
  322. static inline struct gadget_language *to_gadget_language(struct config_item *item)
  323. {
  324. return container_of(to_config_group(item), struct gadget_language,
  325. group);
  326. }
  327. static inline struct gadget_config_name *to_gadget_config_name(
  328. struct config_item *item)
  329. {
  330. return container_of(to_config_group(item), struct gadget_config_name,
  331. group);
  332. }
  333. static inline struct usb_function_instance *to_usb_function_instance(
  334. struct config_item *item)
  335. {
  336. return container_of(to_config_group(item),
  337. struct usb_function_instance, group);
  338. }
  339. static void gadget_info_attr_release(struct config_item *item)
  340. {
  341. struct gadget_info *gi = to_gadget_info(item);
  342. WARN_ON(!list_empty(&gi->cdev.configs));
  343. WARN_ON(!list_empty(&gi->string_list));
  344. WARN_ON(!list_empty(&gi->available_func));
  345. kfree(gi->composite.gadget_driver.function);
  346. kfree(gi->composite.gadget_driver.driver.name);
  347. kfree(gi);
  348. }
  349. static struct configfs_item_operations gadget_root_item_ops = {
  350. .release = gadget_info_attr_release,
  351. };
  352. static void gadget_config_attr_release(struct config_item *item)
  353. {
  354. struct config_usb_cfg *cfg = to_config_usb_cfg(item);
  355. WARN_ON(!list_empty(&cfg->c.functions));
  356. list_del(&cfg->c.list);
  357. kfree(cfg->c.label);
  358. kfree(cfg);
  359. }
  360. static int config_usb_cfg_link(
  361. struct config_item *usb_cfg_ci,
  362. struct config_item *usb_func_ci)
  363. {
  364. struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
  365. struct gadget_info *gi = cfg_to_gadget_info(cfg);
  366. struct usb_function_instance *fi =
  367. to_usb_function_instance(usb_func_ci);
  368. struct usb_function_instance *a_fi = NULL, *iter;
  369. struct usb_function *f;
  370. int ret;
  371. mutex_lock(&gi->lock);
  372. /*
  373. * Make sure this function is from within our _this_ gadget and not
  374. * from another gadget or a random directory.
  375. * Also a function instance can only be linked once.
  376. */
  377. if (gi->composite.gadget_driver.udc_name) {
  378. ret = -EINVAL;
  379. goto out;
  380. }
  381. list_for_each_entry(iter, &gi->available_func, cfs_list) {
  382. if (iter != fi)
  383. continue;
  384. a_fi = iter;
  385. break;
  386. }
  387. if (!a_fi) {
  388. ret = -EINVAL;
  389. goto out;
  390. }
  391. list_for_each_entry(f, &cfg->func_list, list) {
  392. if (f->fi == fi) {
  393. ret = -EEXIST;
  394. goto out;
  395. }
  396. }
  397. f = usb_get_function(fi);
  398. if (IS_ERR(f)) {
  399. ret = PTR_ERR(f);
  400. goto out;
  401. }
  402. /* stash the function until we bind it to the gadget */
  403. list_add_tail(&f->list, &cfg->func_list);
  404. ret = 0;
  405. out:
  406. mutex_unlock(&gi->lock);
  407. return ret;
  408. }
  409. static void config_usb_cfg_unlink(
  410. struct config_item *usb_cfg_ci,
  411. struct config_item *usb_func_ci)
  412. {
  413. struct config_usb_cfg *cfg = to_config_usb_cfg(usb_cfg_ci);
  414. struct gadget_info *gi = cfg_to_gadget_info(cfg);
  415. struct usb_function_instance *fi =
  416. to_usb_function_instance(usb_func_ci);
  417. struct usb_function *f;
  418. /*
  419. * ideally I would like to forbid to unlink functions while a gadget is
  420. * bound to an UDC. Since this isn't possible at the moment, we simply
  421. * force an unbind, the function is available here and then we can
  422. * remove the function.
  423. */
  424. mutex_lock(&gi->lock);
  425. if (gi->composite.gadget_driver.udc_name)
  426. unregister_gadget(gi);
  427. WARN_ON(gi->composite.gadget_driver.udc_name);
  428. list_for_each_entry(f, &cfg->func_list, list) {
  429. if (f->fi == fi) {
  430. list_del(&f->list);
  431. usb_put_function(f);
  432. mutex_unlock(&gi->lock);
  433. return;
  434. }
  435. }
  436. mutex_unlock(&gi->lock);
  437. WARN(1, "Unable to locate function to unbind\n");
  438. }
  439. static struct configfs_item_operations gadget_config_item_ops = {
  440. .release = gadget_config_attr_release,
  441. .allow_link = config_usb_cfg_link,
  442. .drop_link = config_usb_cfg_unlink,
  443. };
  444. static ssize_t gadget_config_desc_MaxPower_show(struct config_item *item,
  445. char *page)
  446. {
  447. struct config_usb_cfg *cfg = to_config_usb_cfg(item);
  448. return sprintf(page, "%u\n", cfg->c.MaxPower);
  449. }
  450. static ssize_t gadget_config_desc_MaxPower_store(struct config_item *item,
  451. const char *page, size_t len)
  452. {
  453. struct config_usb_cfg *cfg = to_config_usb_cfg(item);
  454. u16 val;
  455. int ret;
  456. ret = kstrtou16(page, 0, &val);
  457. if (ret)
  458. return ret;
  459. if (DIV_ROUND_UP(val, 8) > 0xff)
  460. return -ERANGE;
  461. cfg->c.MaxPower = val;
  462. return len;
  463. }
  464. static ssize_t gadget_config_desc_bmAttributes_show(struct config_item *item,
  465. char *page)
  466. {
  467. struct config_usb_cfg *cfg = to_config_usb_cfg(item);
  468. return sprintf(page, "0x%02x\n", cfg->c.bmAttributes);
  469. }
  470. static ssize_t gadget_config_desc_bmAttributes_store(struct config_item *item,
  471. const char *page, size_t len)
  472. {
  473. struct config_usb_cfg *cfg = to_config_usb_cfg(item);
  474. u8 val;
  475. int ret;
  476. ret = kstrtou8(page, 0, &val);
  477. if (ret)
  478. return ret;
  479. if (!(val & USB_CONFIG_ATT_ONE))
  480. return -EINVAL;
  481. if (val & ~(USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER |
  482. USB_CONFIG_ATT_WAKEUP))
  483. return -EINVAL;
  484. cfg->c.bmAttributes = val;
  485. return len;
  486. }
  487. CONFIGFS_ATTR(gadget_config_desc_, MaxPower);
  488. CONFIGFS_ATTR(gadget_config_desc_, bmAttributes);
  489. static struct configfs_attribute *gadget_config_attrs[] = {
  490. &gadget_config_desc_attr_MaxPower,
  491. &gadget_config_desc_attr_bmAttributes,
  492. NULL,
  493. };
  494. static const struct config_item_type gadget_config_type = {
  495. .ct_item_ops = &gadget_config_item_ops,
  496. .ct_attrs = gadget_config_attrs,
  497. .ct_owner = THIS_MODULE,
  498. };
  499. static const struct config_item_type gadget_root_type = {
  500. .ct_item_ops = &gadget_root_item_ops,
  501. .ct_attrs = gadget_root_attrs,
  502. .ct_owner = THIS_MODULE,
  503. };
  504. static void composite_init_dev(struct usb_composite_dev *cdev)
  505. {
  506. spin_lock_init(&cdev->lock);
  507. INIT_LIST_HEAD(&cdev->configs);
  508. INIT_LIST_HEAD(&cdev->gstrings);
  509. }
  510. static struct config_group *function_make(
  511. struct config_group *group,
  512. const char *name)
  513. {
  514. struct gadget_info *gi;
  515. struct usb_function_instance *fi;
  516. char buf[MAX_NAME_LEN];
  517. char *func_name;
  518. char *instance_name;
  519. int ret;
  520. if (strlen(name) >= MAX_NAME_LEN)
  521. return ERR_PTR(-ENAMETOOLONG);
  522. scnprintf(buf, MAX_NAME_LEN, "%s", name);
  523. func_name = buf;
  524. instance_name = strchr(func_name, '.');
  525. if (!instance_name) {
  526. pr_err("Unable to locate . in FUNC.INSTANCE\n");
  527. return ERR_PTR(-EINVAL);
  528. }
  529. *instance_name = '\0';
  530. instance_name++;
  531. fi = usb_get_function_instance(func_name);
  532. if (IS_ERR(fi))
  533. return ERR_CAST(fi);
  534. ret = config_item_set_name(&fi->group.cg_item, "%s", name);
  535. if (ret) {
  536. usb_put_function_instance(fi);
  537. return ERR_PTR(ret);
  538. }
  539. if (fi->set_inst_name) {
  540. ret = fi->set_inst_name(fi, instance_name);
  541. if (ret) {
  542. usb_put_function_instance(fi);
  543. return ERR_PTR(ret);
  544. }
  545. }
  546. gi = container_of(group, struct gadget_info, functions_group);
  547. mutex_lock(&gi->lock);
  548. list_add_tail(&fi->cfs_list, &gi->available_func);
  549. mutex_unlock(&gi->lock);
  550. return &fi->group;
  551. }
  552. static void function_drop(
  553. struct config_group *group,
  554. struct config_item *item)
  555. {
  556. struct usb_function_instance *fi = to_usb_function_instance(item);
  557. struct gadget_info *gi;
  558. gi = container_of(group, struct gadget_info, functions_group);
  559. mutex_lock(&gi->lock);
  560. list_del(&fi->cfs_list);
  561. mutex_unlock(&gi->lock);
  562. config_item_put(item);
  563. }
  564. static struct configfs_group_operations functions_ops = {
  565. .make_group = &function_make,
  566. .drop_item = &function_drop,
  567. };
  568. static const struct config_item_type functions_type = {
  569. .ct_group_ops = &functions_ops,
  570. .ct_owner = THIS_MODULE,
  571. };
  572. GS_STRINGS_RW(gadget_config_name, configuration);
  573. static struct configfs_attribute *gadget_config_name_langid_attrs[] = {
  574. &gadget_config_name_attr_configuration,
  575. NULL,
  576. };
  577. static void gadget_config_name_attr_release(struct config_item *item)
  578. {
  579. struct gadget_config_name *cn = to_gadget_config_name(item);
  580. kfree(cn->configuration);
  581. list_del(&cn->list);
  582. kfree(cn);
  583. }
  584. USB_CONFIG_STRING_RW_OPS(gadget_config_name);
  585. USB_CONFIG_STRINGS_LANG(gadget_config_name, config_usb_cfg);
  586. static struct config_group *config_desc_make(
  587. struct config_group *group,
  588. const char *name)
  589. {
  590. struct gadget_info *gi;
  591. struct config_usb_cfg *cfg;
  592. char buf[MAX_NAME_LEN];
  593. char *num_str;
  594. u8 num;
  595. int ret;
  596. gi = container_of(group, struct gadget_info, configs_group);
  597. if (strlen(name) >= MAX_NAME_LEN)
  598. return ERR_PTR(-ENAMETOOLONG);
  599. scnprintf(buf, MAX_NAME_LEN, "%s", name);
  600. num_str = strchr(buf, '.');
  601. if (!num_str) {
  602. pr_err("Unable to locate . in name.bConfigurationValue\n");
  603. return ERR_PTR(-EINVAL);
  604. }
  605. *num_str = '\0';
  606. num_str++;
  607. if (!strlen(buf))
  608. return ERR_PTR(-EINVAL);
  609. ret = kstrtou8(num_str, 0, &num);
  610. if (ret)
  611. return ERR_PTR(ret);
  612. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  613. if (!cfg)
  614. return ERR_PTR(-ENOMEM);
  615. cfg->c.label = kstrdup(buf, GFP_KERNEL);
  616. if (!cfg->c.label) {
  617. ret = -ENOMEM;
  618. goto err;
  619. }
  620. cfg->c.bConfigurationValue = num;
  621. cfg->c.MaxPower = CONFIG_USB_GADGET_VBUS_DRAW;
  622. cfg->c.bmAttributes = USB_CONFIG_ATT_ONE;
  623. INIT_LIST_HEAD(&cfg->string_list);
  624. INIT_LIST_HEAD(&cfg->func_list);
  625. config_group_init_type_name(&cfg->group, name,
  626. &gadget_config_type);
  627. config_group_init_type_name(&cfg->strings_group, "strings",
  628. &gadget_config_name_strings_type);
  629. configfs_add_default_group(&cfg->strings_group, &cfg->group);
  630. ret = usb_add_config_only(&gi->cdev, &cfg->c);
  631. if (ret)
  632. goto err;
  633. return &cfg->group;
  634. err:
  635. kfree(cfg->c.label);
  636. kfree(cfg);
  637. return ERR_PTR(ret);
  638. }
  639. static void config_desc_drop(
  640. struct config_group *group,
  641. struct config_item *item)
  642. {
  643. config_item_put(item);
  644. }
  645. static struct configfs_group_operations config_desc_ops = {
  646. .make_group = &config_desc_make,
  647. .drop_item = &config_desc_drop,
  648. };
  649. static const struct config_item_type config_desc_type = {
  650. .ct_group_ops = &config_desc_ops,
  651. .ct_owner = THIS_MODULE,
  652. };
  653. GS_STRINGS_RW(gadget_language, manufacturer);
  654. GS_STRINGS_RW(gadget_language, product);
  655. GS_STRINGS_RW(gadget_language, serialnumber);
  656. static struct configfs_attribute *gadget_language_langid_attrs[] = {
  657. &gadget_language_attr_manufacturer,
  658. &gadget_language_attr_product,
  659. &gadget_language_attr_serialnumber,
  660. NULL,
  661. };
  662. static void gadget_language_attr_release(struct config_item *item)
  663. {
  664. struct gadget_language *gs = to_gadget_language(item);
  665. kfree(gs->manufacturer);
  666. kfree(gs->product);
  667. kfree(gs->serialnumber);
  668. list_del(&gs->list);
  669. kfree(gs);
  670. }
  671. static struct configfs_item_operations gadget_language_langid_item_ops = {
  672. .release = gadget_language_attr_release,
  673. };
  674. static ssize_t gadget_string_id_show(struct config_item *item, char *page)
  675. {
  676. struct gadget_string *string = to_gadget_string(item);
  677. int ret;
  678. ret = sprintf(page, "%u\n", string->usb_string.id);
  679. return ret;
  680. }
  681. CONFIGFS_ATTR_RO(gadget_string_, id);
  682. static ssize_t gadget_string_s_show(struct config_item *item, char *page)
  683. {
  684. struct gadget_string *string = to_gadget_string(item);
  685. int ret;
  686. ret = sysfs_emit(page, "%s\n", string->string);
  687. return ret;
  688. }
  689. static ssize_t gadget_string_s_store(struct config_item *item, const char *page,
  690. size_t len)
  691. {
  692. struct gadget_string *string = to_gadget_string(item);
  693. int size = min(sizeof(string->string), len + 1);
  694. ssize_t cpy_len;
  695. if (len > USB_MAX_STRING_LEN)
  696. return -EINVAL;
  697. cpy_len = strscpy(string->string, page, size);
  698. if (cpy_len > 0 && string->string[cpy_len - 1] == '\n')
  699. string->string[cpy_len - 1] = 0;
  700. return len;
  701. }
  702. CONFIGFS_ATTR(gadget_string_, s);
  703. static struct configfs_attribute *gadget_string_attrs[] = {
  704. &gadget_string_attr_id,
  705. &gadget_string_attr_s,
  706. NULL,
  707. };
  708. static void gadget_string_release(struct config_item *item)
  709. {
  710. struct gadget_string *string = to_gadget_string(item);
  711. kfree(string);
  712. }
  713. static struct configfs_item_operations gadget_string_item_ops = {
  714. .release = gadget_string_release,
  715. };
  716. static const struct config_item_type gadget_string_type = {
  717. .ct_item_ops = &gadget_string_item_ops,
  718. .ct_attrs = gadget_string_attrs,
  719. .ct_owner = THIS_MODULE,
  720. };
  721. static struct config_item *gadget_language_string_make(struct config_group *group,
  722. const char *name)
  723. {
  724. struct gadget_language *language;
  725. struct gadget_string *string;
  726. language = to_gadget_language(&group->cg_item);
  727. string = kzalloc(sizeof(*string), GFP_KERNEL);
  728. if (!string)
  729. return ERR_PTR(-ENOMEM);
  730. string->usb_string.id = language->nstrings++;
  731. string->usb_string.s = string->string;
  732. list_add_tail(&string->list, &language->gadget_strings);
  733. config_item_init_type_name(&string->item, name, &gadget_string_type);
  734. return &string->item;
  735. }
  736. static void gadget_language_string_drop(struct config_group *group,
  737. struct config_item *item)
  738. {
  739. struct gadget_language *language;
  740. struct gadget_string *string;
  741. unsigned int i = USB_GADGET_FIRST_AVAIL_IDX;
  742. language = to_gadget_language(&group->cg_item);
  743. string = to_gadget_string(item);
  744. list_del(&string->list);
  745. language->nstrings--;
  746. /* Reset the ids for the language's strings to guarantee a continuous set */
  747. list_for_each_entry(string, &language->gadget_strings, list)
  748. string->usb_string.id = i++;
  749. }
  750. static struct configfs_group_operations gadget_language_langid_group_ops = {
  751. .make_item = gadget_language_string_make,
  752. .drop_item = gadget_language_string_drop,
  753. };
  754. static const struct config_item_type gadget_language_type = {
  755. .ct_item_ops = &gadget_language_langid_item_ops,
  756. .ct_group_ops = &gadget_language_langid_group_ops,
  757. .ct_attrs = gadget_language_langid_attrs,
  758. .ct_owner = THIS_MODULE,
  759. };
  760. static struct config_group *gadget_language_make(struct config_group *group,
  761. const char *name)
  762. {
  763. struct gadget_info *gi;
  764. struct gadget_language *gs;
  765. struct gadget_language *new;
  766. int langs = 0;
  767. int ret;
  768. new = kzalloc(sizeof(*new), GFP_KERNEL);
  769. if (!new)
  770. return ERR_PTR(-ENOMEM);
  771. ret = check_user_usb_string(name, &new->stringtab_dev);
  772. if (ret)
  773. goto err;
  774. config_group_init_type_name(&new->group, name,
  775. &gadget_language_type);
  776. gi = container_of(group, struct gadget_info, strings_group);
  777. ret = -EEXIST;
  778. list_for_each_entry(gs, &gi->string_list, list) {
  779. if (gs->stringtab_dev.language == new->stringtab_dev.language)
  780. goto err;
  781. langs++;
  782. }
  783. ret = -EOVERFLOW;
  784. if (langs >= MAX_USB_STRING_LANGS)
  785. goto err;
  786. list_add_tail(&new->list, &gi->string_list);
  787. INIT_LIST_HEAD(&new->gadget_strings);
  788. /* We have the default manufacturer, product and serialnumber strings */
  789. new->nstrings = 3;
  790. return &new->group;
  791. err:
  792. kfree(new);
  793. return ERR_PTR(ret);
  794. }
  795. static void gadget_language_drop(struct config_group *group,
  796. struct config_item *item)
  797. {
  798. config_item_put(item);
  799. }
  800. static struct configfs_group_operations gadget_language_group_ops = {
  801. .make_group = &gadget_language_make,
  802. .drop_item = &gadget_language_drop,
  803. };
  804. static const struct config_item_type gadget_language_strings_type = {
  805. .ct_group_ops = &gadget_language_group_ops,
  806. .ct_owner = THIS_MODULE,
  807. };
  808. static inline struct gadget_info *webusb_item_to_gadget_info(
  809. struct config_item *item)
  810. {
  811. return container_of(to_config_group(item),
  812. struct gadget_info, webusb_group);
  813. }
  814. static ssize_t webusb_use_show(struct config_item *item, char *page)
  815. {
  816. return sysfs_emit(page, "%d\n",
  817. webusb_item_to_gadget_info(item)->use_webusb);
  818. }
  819. static ssize_t webusb_use_store(struct config_item *item, const char *page,
  820. size_t len)
  821. {
  822. struct gadget_info *gi = webusb_item_to_gadget_info(item);
  823. int ret;
  824. bool use;
  825. ret = kstrtobool(page, &use);
  826. if (ret)
  827. return ret;
  828. mutex_lock(&gi->lock);
  829. gi->use_webusb = use;
  830. mutex_unlock(&gi->lock);
  831. return len;
  832. }
  833. static ssize_t webusb_bcdVersion_show(struct config_item *item, char *page)
  834. {
  835. return sysfs_emit(page, "0x%04x\n",
  836. webusb_item_to_gadget_info(item)->bcd_webusb_version);
  837. }
  838. static ssize_t webusb_bcdVersion_store(struct config_item *item,
  839. const char *page, size_t len)
  840. {
  841. struct gadget_info *gi = webusb_item_to_gadget_info(item);
  842. u16 bcdVersion;
  843. int ret;
  844. ret = kstrtou16(page, 0, &bcdVersion);
  845. if (ret)
  846. return ret;
  847. ret = is_valid_bcd(bcdVersion);
  848. if (ret)
  849. return ret;
  850. mutex_lock(&gi->lock);
  851. gi->bcd_webusb_version = bcdVersion;
  852. mutex_unlock(&gi->lock);
  853. return len;
  854. }
  855. static ssize_t webusb_bVendorCode_show(struct config_item *item, char *page)
  856. {
  857. return sysfs_emit(page, "0x%02x\n",
  858. webusb_item_to_gadget_info(item)->b_webusb_vendor_code);
  859. }
  860. static ssize_t webusb_bVendorCode_store(struct config_item *item,
  861. const char *page, size_t len)
  862. {
  863. struct gadget_info *gi = webusb_item_to_gadget_info(item);
  864. int ret;
  865. u8 b_vendor_code;
  866. ret = kstrtou8(page, 0, &b_vendor_code);
  867. if (ret)
  868. return ret;
  869. mutex_lock(&gi->lock);
  870. gi->b_webusb_vendor_code = b_vendor_code;
  871. mutex_unlock(&gi->lock);
  872. return len;
  873. }
  874. static ssize_t webusb_landingPage_show(struct config_item *item, char *page)
  875. {
  876. return sysfs_emit(page, "%s\n", webusb_item_to_gadget_info(item)->landing_page);
  877. }
  878. static ssize_t webusb_landingPage_store(struct config_item *item, const char *page,
  879. size_t len)
  880. {
  881. struct gadget_info *gi = webusb_item_to_gadget_info(item);
  882. unsigned int bytes_to_strip = 0;
  883. int l = len;
  884. if (!len)
  885. return len;
  886. if (page[l - 1] == '\n') {
  887. --l;
  888. ++bytes_to_strip;
  889. }
  890. if (l > sizeof(gi->landing_page)) {
  891. pr_err("webusb: landingPage URL too long\n");
  892. return -EINVAL;
  893. }
  894. // validation
  895. if (strncasecmp(page, "https://", 8) == 0)
  896. bytes_to_strip = 8;
  897. else if (strncasecmp(page, "http://", 7) == 0)
  898. bytes_to_strip = 7;
  899. else
  900. bytes_to_strip = 0;
  901. if (l > U8_MAX - WEBUSB_URL_DESCRIPTOR_HEADER_LENGTH + bytes_to_strip) {
  902. pr_err("webusb: landingPage URL %d bytes too long for given URL scheme\n",
  903. l - U8_MAX + WEBUSB_URL_DESCRIPTOR_HEADER_LENGTH - bytes_to_strip);
  904. return -EINVAL;
  905. }
  906. mutex_lock(&gi->lock);
  907. // ensure 0 bytes are set, in case the new landing page is shorter then the old one.
  908. memcpy_and_pad(gi->landing_page, sizeof(gi->landing_page), page, l, 0);
  909. mutex_unlock(&gi->lock);
  910. return len;
  911. }
  912. CONFIGFS_ATTR(webusb_, use);
  913. CONFIGFS_ATTR(webusb_, bVendorCode);
  914. CONFIGFS_ATTR(webusb_, bcdVersion);
  915. CONFIGFS_ATTR(webusb_, landingPage);
  916. static struct configfs_attribute *webusb_attrs[] = {
  917. &webusb_attr_use,
  918. &webusb_attr_bcdVersion,
  919. &webusb_attr_bVendorCode,
  920. &webusb_attr_landingPage,
  921. NULL,
  922. };
  923. static const struct config_item_type webusb_type = {
  924. .ct_attrs = webusb_attrs,
  925. .ct_owner = THIS_MODULE,
  926. };
  927. static inline struct gadget_info *os_desc_item_to_gadget_info(
  928. struct config_item *item)
  929. {
  930. return container_of(to_config_group(item),
  931. struct gadget_info, os_desc_group);
  932. }
  933. static ssize_t os_desc_use_show(struct config_item *item, char *page)
  934. {
  935. return sprintf(page, "%d\n",
  936. os_desc_item_to_gadget_info(item)->use_os_desc);
  937. }
  938. static ssize_t os_desc_use_store(struct config_item *item, const char *page,
  939. size_t len)
  940. {
  941. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  942. int ret;
  943. bool use;
  944. ret = kstrtobool(page, &use);
  945. if (ret)
  946. return ret;
  947. mutex_lock(&gi->lock);
  948. gi->use_os_desc = use;
  949. mutex_unlock(&gi->lock);
  950. return len;
  951. }
  952. static ssize_t os_desc_b_vendor_code_show(struct config_item *item, char *page)
  953. {
  954. return sprintf(page, "0x%02x\n",
  955. os_desc_item_to_gadget_info(item)->b_vendor_code);
  956. }
  957. static ssize_t os_desc_b_vendor_code_store(struct config_item *item,
  958. const char *page, size_t len)
  959. {
  960. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  961. int ret;
  962. u8 b_vendor_code;
  963. ret = kstrtou8(page, 0, &b_vendor_code);
  964. if (ret)
  965. return ret;
  966. mutex_lock(&gi->lock);
  967. gi->b_vendor_code = b_vendor_code;
  968. mutex_unlock(&gi->lock);
  969. return len;
  970. }
  971. static ssize_t os_desc_qw_sign_show(struct config_item *item, char *page)
  972. {
  973. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  974. int res;
  975. res = utf16s_to_utf8s((wchar_t *) gi->qw_sign, OS_STRING_QW_SIGN_LEN,
  976. UTF16_LITTLE_ENDIAN, page, PAGE_SIZE - 1);
  977. page[res++] = '\n';
  978. return res;
  979. }
  980. static ssize_t os_desc_qw_sign_store(struct config_item *item, const char *page,
  981. size_t len)
  982. {
  983. struct gadget_info *gi = os_desc_item_to_gadget_info(item);
  984. int res, l;
  985. if (!len)
  986. return len;
  987. l = min((int)len, OS_STRING_QW_SIGN_LEN >> 1);
  988. if (page[l - 1] == '\n')
  989. --l;
  990. mutex_lock(&gi->lock);
  991. res = utf8s_to_utf16s(page, l,
  992. UTF16_LITTLE_ENDIAN, (wchar_t *) gi->qw_sign,
  993. OS_STRING_QW_SIGN_LEN);
  994. if (res > 0)
  995. res = len;
  996. mutex_unlock(&gi->lock);
  997. return res;
  998. }
  999. CONFIGFS_ATTR(os_desc_, use);
  1000. CONFIGFS_ATTR(os_desc_, b_vendor_code);
  1001. CONFIGFS_ATTR(os_desc_, qw_sign);
  1002. static struct configfs_attribute *os_desc_attrs[] = {
  1003. &os_desc_attr_use,
  1004. &os_desc_attr_b_vendor_code,
  1005. &os_desc_attr_qw_sign,
  1006. NULL,
  1007. };
  1008. static int os_desc_link(struct config_item *os_desc_ci,
  1009. struct config_item *usb_cfg_ci)
  1010. {
  1011. struct gadget_info *gi = os_desc_item_to_gadget_info(os_desc_ci);
  1012. struct usb_composite_dev *cdev = &gi->cdev;
  1013. struct config_usb_cfg *c_target = to_config_usb_cfg(usb_cfg_ci);
  1014. struct usb_configuration *c = NULL, *iter;
  1015. int ret;
  1016. mutex_lock(&gi->lock);
  1017. list_for_each_entry(iter, &cdev->configs, list) {
  1018. if (iter != &c_target->c)
  1019. continue;
  1020. c = iter;
  1021. break;
  1022. }
  1023. if (!c) {
  1024. ret = -EINVAL;
  1025. goto out;
  1026. }
  1027. if (cdev->os_desc_config) {
  1028. ret = -EBUSY;
  1029. goto out;
  1030. }
  1031. cdev->os_desc_config = &c_target->c;
  1032. ret = 0;
  1033. out:
  1034. mutex_unlock(&gi->lock);
  1035. return ret;
  1036. }
  1037. static void os_desc_unlink(struct config_item *os_desc_ci,
  1038. struct config_item *usb_cfg_ci)
  1039. {
  1040. struct gadget_info *gi = os_desc_item_to_gadget_info(os_desc_ci);
  1041. struct usb_composite_dev *cdev = &gi->cdev;
  1042. mutex_lock(&gi->lock);
  1043. if (gi->composite.gadget_driver.udc_name)
  1044. unregister_gadget(gi);
  1045. cdev->os_desc_config = NULL;
  1046. WARN_ON(gi->composite.gadget_driver.udc_name);
  1047. mutex_unlock(&gi->lock);
  1048. }
  1049. static struct configfs_item_operations os_desc_ops = {
  1050. .allow_link = os_desc_link,
  1051. .drop_link = os_desc_unlink,
  1052. };
  1053. static const struct config_item_type os_desc_type = {
  1054. .ct_item_ops = &os_desc_ops,
  1055. .ct_attrs = os_desc_attrs,
  1056. .ct_owner = THIS_MODULE,
  1057. };
  1058. static inline struct usb_os_desc_ext_prop
  1059. *to_usb_os_desc_ext_prop(struct config_item *item)
  1060. {
  1061. return container_of(item, struct usb_os_desc_ext_prop, item);
  1062. }
  1063. static ssize_t ext_prop_type_show(struct config_item *item, char *page)
  1064. {
  1065. return sprintf(page, "%d\n", to_usb_os_desc_ext_prop(item)->type);
  1066. }
  1067. static ssize_t ext_prop_type_store(struct config_item *item,
  1068. const char *page, size_t len)
  1069. {
  1070. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  1071. struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
  1072. u8 type;
  1073. int ret;
  1074. ret = kstrtou8(page, 0, &type);
  1075. if (ret)
  1076. return ret;
  1077. if (type < USB_EXT_PROP_UNICODE || type > USB_EXT_PROP_UNICODE_MULTI)
  1078. return -EINVAL;
  1079. if (desc->opts_mutex)
  1080. mutex_lock(desc->opts_mutex);
  1081. if ((ext_prop->type == USB_EXT_PROP_BINARY ||
  1082. ext_prop->type == USB_EXT_PROP_LE32 ||
  1083. ext_prop->type == USB_EXT_PROP_BE32) &&
  1084. (type == USB_EXT_PROP_UNICODE ||
  1085. type == USB_EXT_PROP_UNICODE_ENV ||
  1086. type == USB_EXT_PROP_UNICODE_LINK))
  1087. ext_prop->data_len <<= 1;
  1088. else if ((ext_prop->type == USB_EXT_PROP_UNICODE ||
  1089. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  1090. ext_prop->type == USB_EXT_PROP_UNICODE_LINK) &&
  1091. (type == USB_EXT_PROP_BINARY ||
  1092. type == USB_EXT_PROP_LE32 ||
  1093. type == USB_EXT_PROP_BE32))
  1094. ext_prop->data_len >>= 1;
  1095. ext_prop->type = type;
  1096. if (desc->opts_mutex)
  1097. mutex_unlock(desc->opts_mutex);
  1098. return len;
  1099. }
  1100. static ssize_t ext_prop_data_show(struct config_item *item, char *page)
  1101. {
  1102. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  1103. int len = ext_prop->data_len;
  1104. if (ext_prop->type == USB_EXT_PROP_UNICODE ||
  1105. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  1106. ext_prop->type == USB_EXT_PROP_UNICODE_LINK)
  1107. len >>= 1;
  1108. memcpy(page, ext_prop->data, len);
  1109. return len;
  1110. }
  1111. static ssize_t ext_prop_data_store(struct config_item *item,
  1112. const char *page, size_t len)
  1113. {
  1114. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  1115. struct usb_os_desc *desc = to_usb_os_desc(ext_prop->item.ci_parent);
  1116. char *new_data;
  1117. size_t ret_len = len;
  1118. if (page[len - 1] == '\n' || page[len - 1] == '\0')
  1119. --len;
  1120. new_data = kmemdup(page, len, GFP_KERNEL);
  1121. if (!new_data)
  1122. return -ENOMEM;
  1123. if (desc->opts_mutex)
  1124. mutex_lock(desc->opts_mutex);
  1125. kfree(ext_prop->data);
  1126. ext_prop->data = new_data;
  1127. desc->ext_prop_len -= ext_prop->data_len;
  1128. ext_prop->data_len = len;
  1129. desc->ext_prop_len += ext_prop->data_len;
  1130. if (ext_prop->type == USB_EXT_PROP_UNICODE ||
  1131. ext_prop->type == USB_EXT_PROP_UNICODE_ENV ||
  1132. ext_prop->type == USB_EXT_PROP_UNICODE_LINK) {
  1133. desc->ext_prop_len -= ext_prop->data_len;
  1134. ext_prop->data_len <<= 1;
  1135. ext_prop->data_len += 2;
  1136. desc->ext_prop_len += ext_prop->data_len;
  1137. }
  1138. if (desc->opts_mutex)
  1139. mutex_unlock(desc->opts_mutex);
  1140. return ret_len;
  1141. }
  1142. CONFIGFS_ATTR(ext_prop_, type);
  1143. CONFIGFS_ATTR(ext_prop_, data);
  1144. static struct configfs_attribute *ext_prop_attrs[] = {
  1145. &ext_prop_attr_type,
  1146. &ext_prop_attr_data,
  1147. NULL,
  1148. };
  1149. static void usb_os_desc_ext_prop_release(struct config_item *item)
  1150. {
  1151. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  1152. kfree(ext_prop); /* frees a whole chunk */
  1153. }
  1154. static struct configfs_item_operations ext_prop_ops = {
  1155. .release = usb_os_desc_ext_prop_release,
  1156. };
  1157. static struct config_item *ext_prop_make(
  1158. struct config_group *group,
  1159. const char *name)
  1160. {
  1161. struct usb_os_desc_ext_prop *ext_prop;
  1162. struct config_item_type *ext_prop_type;
  1163. struct usb_os_desc *desc;
  1164. char *vlabuf;
  1165. vla_group(data_chunk);
  1166. vla_item(data_chunk, struct usb_os_desc_ext_prop, ext_prop, 1);
  1167. vla_item(data_chunk, struct config_item_type, ext_prop_type, 1);
  1168. vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
  1169. if (!vlabuf)
  1170. return ERR_PTR(-ENOMEM);
  1171. ext_prop = vla_ptr(vlabuf, data_chunk, ext_prop);
  1172. ext_prop_type = vla_ptr(vlabuf, data_chunk, ext_prop_type);
  1173. desc = container_of(group, struct usb_os_desc, group);
  1174. ext_prop_type->ct_item_ops = &ext_prop_ops;
  1175. ext_prop_type->ct_attrs = ext_prop_attrs;
  1176. ext_prop_type->ct_owner = desc->owner;
  1177. config_item_init_type_name(&ext_prop->item, name, ext_prop_type);
  1178. ext_prop->name = kstrdup(name, GFP_KERNEL);
  1179. if (!ext_prop->name) {
  1180. kfree(vlabuf);
  1181. return ERR_PTR(-ENOMEM);
  1182. }
  1183. desc->ext_prop_len += 14;
  1184. ext_prop->name_len = 2 * strlen(ext_prop->name) + 2;
  1185. if (desc->opts_mutex)
  1186. mutex_lock(desc->opts_mutex);
  1187. desc->ext_prop_len += ext_prop->name_len;
  1188. list_add_tail(&ext_prop->entry, &desc->ext_prop);
  1189. ++desc->ext_prop_count;
  1190. if (desc->opts_mutex)
  1191. mutex_unlock(desc->opts_mutex);
  1192. return &ext_prop->item;
  1193. }
  1194. static void ext_prop_drop(struct config_group *group, struct config_item *item)
  1195. {
  1196. struct usb_os_desc_ext_prop *ext_prop = to_usb_os_desc_ext_prop(item);
  1197. struct usb_os_desc *desc = to_usb_os_desc(&group->cg_item);
  1198. if (desc->opts_mutex)
  1199. mutex_lock(desc->opts_mutex);
  1200. list_del(&ext_prop->entry);
  1201. --desc->ext_prop_count;
  1202. kfree(ext_prop->name);
  1203. desc->ext_prop_len -= (ext_prop->name_len + ext_prop->data_len + 14);
  1204. if (desc->opts_mutex)
  1205. mutex_unlock(desc->opts_mutex);
  1206. config_item_put(item);
  1207. }
  1208. static struct configfs_group_operations interf_grp_ops = {
  1209. .make_item = &ext_prop_make,
  1210. .drop_item = &ext_prop_drop,
  1211. };
  1212. static ssize_t interf_grp_compatible_id_show(struct config_item *item,
  1213. char *page)
  1214. {
  1215. memcpy(page, to_usb_os_desc(item)->ext_compat_id, 8);
  1216. return 8;
  1217. }
  1218. static ssize_t interf_grp_compatible_id_store(struct config_item *item,
  1219. const char *page, size_t len)
  1220. {
  1221. struct usb_os_desc *desc = to_usb_os_desc(item);
  1222. int l;
  1223. l = min_t(int, 8, len);
  1224. if (page[l - 1] == '\n')
  1225. --l;
  1226. if (desc->opts_mutex)
  1227. mutex_lock(desc->opts_mutex);
  1228. memcpy(desc->ext_compat_id, page, l);
  1229. if (desc->opts_mutex)
  1230. mutex_unlock(desc->opts_mutex);
  1231. return len;
  1232. }
  1233. static ssize_t interf_grp_sub_compatible_id_show(struct config_item *item,
  1234. char *page)
  1235. {
  1236. memcpy(page, to_usb_os_desc(item)->ext_compat_id + 8, 8);
  1237. return 8;
  1238. }
  1239. static ssize_t interf_grp_sub_compatible_id_store(struct config_item *item,
  1240. const char *page, size_t len)
  1241. {
  1242. struct usb_os_desc *desc = to_usb_os_desc(item);
  1243. int l;
  1244. l = min_t(int, 8, len);
  1245. if (page[l - 1] == '\n')
  1246. --l;
  1247. if (desc->opts_mutex)
  1248. mutex_lock(desc->opts_mutex);
  1249. memcpy(desc->ext_compat_id + 8, page, l);
  1250. if (desc->opts_mutex)
  1251. mutex_unlock(desc->opts_mutex);
  1252. return len;
  1253. }
  1254. CONFIGFS_ATTR(interf_grp_, compatible_id);
  1255. CONFIGFS_ATTR(interf_grp_, sub_compatible_id);
  1256. static struct configfs_attribute *interf_grp_attrs[] = {
  1257. &interf_grp_attr_compatible_id,
  1258. &interf_grp_attr_sub_compatible_id,
  1259. NULL
  1260. };
  1261. struct config_group *usb_os_desc_prepare_interf_dir(
  1262. struct config_group *parent,
  1263. int n_interf,
  1264. struct usb_os_desc **desc,
  1265. char **names,
  1266. struct module *owner)
  1267. {
  1268. struct config_group *os_desc_group;
  1269. struct config_item_type *os_desc_type, *interface_type;
  1270. vla_group(data_chunk);
  1271. vla_item(data_chunk, struct config_group, os_desc_group, 1);
  1272. vla_item(data_chunk, struct config_item_type, os_desc_type, 1);
  1273. vla_item(data_chunk, struct config_item_type, interface_type, 1);
  1274. char *vlabuf = kzalloc(vla_group_size(data_chunk), GFP_KERNEL);
  1275. if (!vlabuf)
  1276. return ERR_PTR(-ENOMEM);
  1277. os_desc_group = vla_ptr(vlabuf, data_chunk, os_desc_group);
  1278. os_desc_type = vla_ptr(vlabuf, data_chunk, os_desc_type);
  1279. interface_type = vla_ptr(vlabuf, data_chunk, interface_type);
  1280. os_desc_type->ct_owner = owner;
  1281. config_group_init_type_name(os_desc_group, "os_desc", os_desc_type);
  1282. configfs_add_default_group(os_desc_group, parent);
  1283. interface_type->ct_group_ops = &interf_grp_ops;
  1284. interface_type->ct_attrs = interf_grp_attrs;
  1285. interface_type->ct_owner = owner;
  1286. while (n_interf--) {
  1287. struct usb_os_desc *d;
  1288. d = desc[n_interf];
  1289. d->owner = owner;
  1290. config_group_init_type_name(&d->group, "", interface_type);
  1291. config_item_set_name(&d->group.cg_item, "interface.%s",
  1292. names[n_interf]);
  1293. configfs_add_default_group(&d->group, os_desc_group);
  1294. }
  1295. return os_desc_group;
  1296. }
  1297. EXPORT_SYMBOL(usb_os_desc_prepare_interf_dir);
  1298. static int configfs_do_nothing(struct usb_composite_dev *cdev)
  1299. {
  1300. WARN_ON(1);
  1301. return -EINVAL;
  1302. }
  1303. int composite_dev_prepare(struct usb_composite_driver *composite,
  1304. struct usb_composite_dev *dev);
  1305. int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
  1306. struct usb_ep *ep0);
  1307. static void purge_configs_funcs(struct gadget_info *gi)
  1308. {
  1309. struct usb_configuration *c;
  1310. list_for_each_entry(c, &gi->cdev.configs, list) {
  1311. struct usb_function *f, *tmp;
  1312. struct config_usb_cfg *cfg;
  1313. cfg = container_of(c, struct config_usb_cfg, c);
  1314. list_for_each_entry_safe_reverse(f, tmp, &c->functions, list) {
  1315. list_move(&f->list, &cfg->func_list);
  1316. if (f->unbind) {
  1317. dev_dbg(&gi->cdev.gadget->dev,
  1318. "unbind function '%s'/%p\n",
  1319. f->name, f);
  1320. f->unbind(c, f);
  1321. }
  1322. }
  1323. c->next_interface_id = 0;
  1324. memset(c->interface, 0, sizeof(c->interface));
  1325. c->superspeed_plus = 0;
  1326. c->superspeed = 0;
  1327. c->highspeed = 0;
  1328. c->fullspeed = 0;
  1329. }
  1330. }
  1331. static struct usb_string *
  1332. configfs_attach_gadget_strings(struct gadget_info *gi)
  1333. {
  1334. struct usb_gadget_strings **gadget_strings;
  1335. struct gadget_language *language;
  1336. struct gadget_string *string;
  1337. unsigned int nlangs = 0;
  1338. struct list_head *iter;
  1339. struct usb_string *us;
  1340. unsigned int i = 0;
  1341. int nstrings = -1;
  1342. unsigned int j;
  1343. list_for_each(iter, &gi->string_list)
  1344. nlangs++;
  1345. /* Bail out early if no languages are configured */
  1346. if (!nlangs)
  1347. return NULL;
  1348. gadget_strings = kcalloc(nlangs + 1, /* including NULL terminator */
  1349. sizeof(struct usb_gadget_strings *), GFP_KERNEL);
  1350. if (!gadget_strings)
  1351. return ERR_PTR(-ENOMEM);
  1352. list_for_each_entry(language, &gi->string_list, list) {
  1353. struct usb_string *stringtab;
  1354. if (nstrings == -1) {
  1355. nstrings = language->nstrings;
  1356. } else if (nstrings != language->nstrings) {
  1357. pr_err("languages must contain the same number of strings\n");
  1358. us = ERR_PTR(-EINVAL);
  1359. goto cleanup;
  1360. }
  1361. stringtab = kcalloc(language->nstrings + 1, sizeof(struct usb_string),
  1362. GFP_KERNEL);
  1363. if (!stringtab) {
  1364. us = ERR_PTR(-ENOMEM);
  1365. goto cleanup;
  1366. }
  1367. stringtab[USB_GADGET_MANUFACTURER_IDX].id = USB_GADGET_MANUFACTURER_IDX;
  1368. stringtab[USB_GADGET_MANUFACTURER_IDX].s = language->manufacturer;
  1369. stringtab[USB_GADGET_PRODUCT_IDX].id = USB_GADGET_PRODUCT_IDX;
  1370. stringtab[USB_GADGET_PRODUCT_IDX].s = language->product;
  1371. stringtab[USB_GADGET_SERIAL_IDX].id = USB_GADGET_SERIAL_IDX;
  1372. stringtab[USB_GADGET_SERIAL_IDX].s = language->serialnumber;
  1373. j = USB_GADGET_FIRST_AVAIL_IDX;
  1374. list_for_each_entry(string, &language->gadget_strings, list) {
  1375. memcpy(&stringtab[j], &string->usb_string, sizeof(struct usb_string));
  1376. j++;
  1377. }
  1378. language->stringtab_dev.strings = stringtab;
  1379. gadget_strings[i] = &language->stringtab_dev;
  1380. i++;
  1381. }
  1382. us = usb_gstrings_attach(&gi->cdev, gadget_strings, nstrings);
  1383. cleanup:
  1384. list_for_each_entry(language, &gi->string_list, list) {
  1385. kfree(language->stringtab_dev.strings);
  1386. language->stringtab_dev.strings = NULL;
  1387. }
  1388. kfree(gadget_strings);
  1389. return us;
  1390. }
  1391. static int configfs_composite_bind(struct usb_gadget *gadget,
  1392. struct usb_gadget_driver *gdriver)
  1393. {
  1394. struct usb_composite_driver *composite = to_cdriver(gdriver);
  1395. struct gadget_info *gi = container_of(composite,
  1396. struct gadget_info, composite);
  1397. struct usb_composite_dev *cdev = &gi->cdev;
  1398. struct usb_configuration *c;
  1399. struct usb_string *s;
  1400. unsigned i;
  1401. int ret;
  1402. /* the gi->lock is hold by the caller */
  1403. gi->unbind = 0;
  1404. cdev->gadget = gadget;
  1405. set_gadget_data(gadget, cdev);
  1406. ret = composite_dev_prepare(composite, cdev);
  1407. if (ret)
  1408. return ret;
  1409. /* and now the gadget bind */
  1410. ret = -EINVAL;
  1411. if (list_empty(&gi->cdev.configs)) {
  1412. pr_err("Need at least one configuration in %s.\n",
  1413. gi->composite.name);
  1414. goto err_comp_cleanup;
  1415. }
  1416. list_for_each_entry(c, &gi->cdev.configs, list) {
  1417. struct config_usb_cfg *cfg;
  1418. cfg = container_of(c, struct config_usb_cfg, c);
  1419. if (list_empty(&cfg->func_list)) {
  1420. pr_err("Config %s/%d of %s needs at least one function.\n",
  1421. c->label, c->bConfigurationValue,
  1422. gi->composite.name);
  1423. goto err_comp_cleanup;
  1424. }
  1425. }
  1426. /* init all strings */
  1427. if (!list_empty(&gi->string_list)) {
  1428. s = configfs_attach_gadget_strings(gi);
  1429. if (IS_ERR(s)) {
  1430. ret = PTR_ERR(s);
  1431. goto err_comp_cleanup;
  1432. }
  1433. gi->cdev.desc.iManufacturer = s[USB_GADGET_MANUFACTURER_IDX].id;
  1434. gi->cdev.desc.iProduct = s[USB_GADGET_PRODUCT_IDX].id;
  1435. gi->cdev.desc.iSerialNumber = s[USB_GADGET_SERIAL_IDX].id;
  1436. gi->cdev.usb_strings = s;
  1437. }
  1438. if (gi->use_webusb) {
  1439. cdev->use_webusb = true;
  1440. cdev->bcd_webusb_version = gi->bcd_webusb_version;
  1441. cdev->b_webusb_vendor_code = gi->b_webusb_vendor_code;
  1442. memcpy(cdev->landing_page, gi->landing_page, WEBUSB_URL_RAW_MAX_LENGTH);
  1443. }
  1444. if (gi->use_os_desc) {
  1445. cdev->use_os_string = true;
  1446. cdev->b_vendor_code = gi->b_vendor_code;
  1447. memcpy(cdev->qw_sign, gi->qw_sign, OS_STRING_QW_SIGN_LEN);
  1448. } else {
  1449. cdev->use_os_string = false;
  1450. }
  1451. if (gadget_is_otg(gadget) && !otg_desc[0]) {
  1452. struct usb_descriptor_header *usb_desc;
  1453. usb_desc = usb_otg_descriptor_alloc(gadget);
  1454. if (!usb_desc) {
  1455. ret = -ENOMEM;
  1456. goto err_comp_cleanup;
  1457. }
  1458. usb_otg_descriptor_init(gadget, usb_desc);
  1459. otg_desc[0] = usb_desc;
  1460. otg_desc[1] = NULL;
  1461. }
  1462. /* Go through all configs, attach all functions */
  1463. list_for_each_entry(c, &gi->cdev.configs, list) {
  1464. struct config_usb_cfg *cfg;
  1465. struct usb_function *f;
  1466. struct usb_function *tmp;
  1467. struct gadget_config_name *cn;
  1468. if (gadget_is_otg(gadget))
  1469. c->descriptors = otg_desc;
  1470. /* Properly configure the bmAttributes wakeup bit */
  1471. check_remote_wakeup_config(gadget, c);
  1472. cfg = container_of(c, struct config_usb_cfg, c);
  1473. if (!list_empty(&cfg->string_list)) {
  1474. i = 0;
  1475. list_for_each_entry(cn, &cfg->string_list, list) {
  1476. cfg->gstrings[i] = &cn->stringtab_dev;
  1477. cn->stringtab_dev.strings = &cn->strings;
  1478. cn->strings.s = cn->configuration;
  1479. i++;
  1480. }
  1481. cfg->gstrings[i] = NULL;
  1482. s = usb_gstrings_attach(&gi->cdev, cfg->gstrings, 1);
  1483. if (IS_ERR(s)) {
  1484. ret = PTR_ERR(s);
  1485. goto err_comp_cleanup;
  1486. }
  1487. c->iConfiguration = s[0].id;
  1488. }
  1489. list_for_each_entry_safe(f, tmp, &cfg->func_list, list) {
  1490. list_del(&f->list);
  1491. ret = usb_add_function(c, f);
  1492. if (ret) {
  1493. list_add(&f->list, &cfg->func_list);
  1494. goto err_purge_funcs;
  1495. }
  1496. }
  1497. ret = usb_gadget_check_config(cdev->gadget);
  1498. if (ret)
  1499. goto err_purge_funcs;
  1500. usb_ep_autoconfig_reset(cdev->gadget);
  1501. }
  1502. if (cdev->use_os_string) {
  1503. ret = composite_os_desc_req_prepare(cdev, gadget->ep0);
  1504. if (ret)
  1505. goto err_purge_funcs;
  1506. }
  1507. usb_ep_autoconfig_reset(cdev->gadget);
  1508. return 0;
  1509. err_purge_funcs:
  1510. purge_configs_funcs(gi);
  1511. err_comp_cleanup:
  1512. composite_dev_cleanup(cdev);
  1513. return ret;
  1514. }
  1515. static void configfs_composite_unbind(struct usb_gadget *gadget)
  1516. {
  1517. struct usb_composite_dev *cdev;
  1518. struct gadget_info *gi;
  1519. unsigned long flags;
  1520. /* the gi->lock is hold by the caller */
  1521. cdev = get_gadget_data(gadget);
  1522. gi = container_of(cdev, struct gadget_info, cdev);
  1523. spin_lock_irqsave(&gi->spinlock, flags);
  1524. gi->unbind = 1;
  1525. spin_unlock_irqrestore(&gi->spinlock, flags);
  1526. kfree(otg_desc[0]);
  1527. otg_desc[0] = NULL;
  1528. purge_configs_funcs(gi);
  1529. composite_dev_cleanup(cdev);
  1530. usb_ep_autoconfig_reset(cdev->gadget);
  1531. spin_lock_irqsave(&gi->spinlock, flags);
  1532. cdev->gadget = NULL;
  1533. cdev->deactivations = 0;
  1534. gadget->deactivated = false;
  1535. set_gadget_data(gadget, NULL);
  1536. spin_unlock_irqrestore(&gi->spinlock, flags);
  1537. }
  1538. static int configfs_composite_setup(struct usb_gadget *gadget,
  1539. const struct usb_ctrlrequest *ctrl)
  1540. {
  1541. struct usb_composite_dev *cdev;
  1542. struct gadget_info *gi;
  1543. unsigned long flags;
  1544. int ret;
  1545. cdev = get_gadget_data(gadget);
  1546. if (!cdev)
  1547. return 0;
  1548. gi = container_of(cdev, struct gadget_info, cdev);
  1549. spin_lock_irqsave(&gi->spinlock, flags);
  1550. cdev = get_gadget_data(gadget);
  1551. if (!cdev || gi->unbind) {
  1552. spin_unlock_irqrestore(&gi->spinlock, flags);
  1553. return 0;
  1554. }
  1555. ret = composite_setup(gadget, ctrl);
  1556. spin_unlock_irqrestore(&gi->spinlock, flags);
  1557. return ret;
  1558. }
  1559. static void configfs_composite_disconnect(struct usb_gadget *gadget)
  1560. {
  1561. struct usb_composite_dev *cdev;
  1562. struct gadget_info *gi;
  1563. unsigned long flags;
  1564. cdev = get_gadget_data(gadget);
  1565. if (!cdev)
  1566. return;
  1567. gi = container_of(cdev, struct gadget_info, cdev);
  1568. spin_lock_irqsave(&gi->spinlock, flags);
  1569. cdev = get_gadget_data(gadget);
  1570. if (!cdev || gi->unbind) {
  1571. spin_unlock_irqrestore(&gi->spinlock, flags);
  1572. return;
  1573. }
  1574. composite_disconnect(gadget);
  1575. spin_unlock_irqrestore(&gi->spinlock, flags);
  1576. }
  1577. static void configfs_composite_reset(struct usb_gadget *gadget)
  1578. {
  1579. struct usb_composite_dev *cdev;
  1580. struct gadget_info *gi;
  1581. unsigned long flags;
  1582. cdev = get_gadget_data(gadget);
  1583. if (!cdev)
  1584. return;
  1585. gi = container_of(cdev, struct gadget_info, cdev);
  1586. spin_lock_irqsave(&gi->spinlock, flags);
  1587. cdev = get_gadget_data(gadget);
  1588. if (!cdev || gi->unbind) {
  1589. spin_unlock_irqrestore(&gi->spinlock, flags);
  1590. return;
  1591. }
  1592. composite_reset(gadget);
  1593. spin_unlock_irqrestore(&gi->spinlock, flags);
  1594. }
  1595. static void configfs_composite_suspend(struct usb_gadget *gadget)
  1596. {
  1597. struct usb_composite_dev *cdev;
  1598. struct gadget_info *gi;
  1599. unsigned long flags;
  1600. cdev = get_gadget_data(gadget);
  1601. if (!cdev)
  1602. return;
  1603. gi = container_of(cdev, struct gadget_info, cdev);
  1604. spin_lock_irqsave(&gi->spinlock, flags);
  1605. cdev = get_gadget_data(gadget);
  1606. if (!cdev || gi->unbind) {
  1607. spin_unlock_irqrestore(&gi->spinlock, flags);
  1608. return;
  1609. }
  1610. composite_suspend(gadget);
  1611. spin_unlock_irqrestore(&gi->spinlock, flags);
  1612. }
  1613. static void configfs_composite_resume(struct usb_gadget *gadget)
  1614. {
  1615. struct usb_composite_dev *cdev;
  1616. struct gadget_info *gi;
  1617. unsigned long flags;
  1618. cdev = get_gadget_data(gadget);
  1619. if (!cdev)
  1620. return;
  1621. gi = container_of(cdev, struct gadget_info, cdev);
  1622. spin_lock_irqsave(&gi->spinlock, flags);
  1623. cdev = get_gadget_data(gadget);
  1624. if (!cdev || gi->unbind) {
  1625. spin_unlock_irqrestore(&gi->spinlock, flags);
  1626. return;
  1627. }
  1628. composite_resume(gadget);
  1629. spin_unlock_irqrestore(&gi->spinlock, flags);
  1630. }
  1631. static const struct usb_gadget_driver configfs_driver_template = {
  1632. .bind = configfs_composite_bind,
  1633. .unbind = configfs_composite_unbind,
  1634. .setup = configfs_composite_setup,
  1635. .reset = configfs_composite_reset,
  1636. .disconnect = configfs_composite_disconnect,
  1637. .suspend = configfs_composite_suspend,
  1638. .resume = configfs_composite_resume,
  1639. .max_speed = USB_SPEED_SUPER_PLUS,
  1640. .driver = {
  1641. .owner = THIS_MODULE,
  1642. },
  1643. .match_existing_only = 1,
  1644. };
  1645. static struct config_group *gadgets_make(
  1646. struct config_group *group,
  1647. const char *name)
  1648. {
  1649. struct gadget_info *gi;
  1650. gi = kzalloc(sizeof(*gi), GFP_KERNEL);
  1651. if (!gi)
  1652. return ERR_PTR(-ENOMEM);
  1653. config_group_init_type_name(&gi->group, name, &gadget_root_type);
  1654. config_group_init_type_name(&gi->functions_group, "functions",
  1655. &functions_type);
  1656. configfs_add_default_group(&gi->functions_group, &gi->group);
  1657. config_group_init_type_name(&gi->configs_group, "configs",
  1658. &config_desc_type);
  1659. configfs_add_default_group(&gi->configs_group, &gi->group);
  1660. config_group_init_type_name(&gi->strings_group, "strings",
  1661. &gadget_language_strings_type);
  1662. configfs_add_default_group(&gi->strings_group, &gi->group);
  1663. config_group_init_type_name(&gi->os_desc_group, "os_desc",
  1664. &os_desc_type);
  1665. configfs_add_default_group(&gi->os_desc_group, &gi->group);
  1666. config_group_init_type_name(&gi->webusb_group, "webusb",
  1667. &webusb_type);
  1668. configfs_add_default_group(&gi->webusb_group, &gi->group);
  1669. gi->composite.bind = configfs_do_nothing;
  1670. gi->composite.unbind = configfs_do_nothing;
  1671. gi->composite.suspend = NULL;
  1672. gi->composite.resume = NULL;
  1673. gi->composite.max_speed = USB_SPEED_SUPER_PLUS;
  1674. spin_lock_init(&gi->spinlock);
  1675. mutex_init(&gi->lock);
  1676. INIT_LIST_HEAD(&gi->string_list);
  1677. INIT_LIST_HEAD(&gi->available_func);
  1678. composite_init_dev(&gi->cdev);
  1679. gi->cdev.desc.bLength = USB_DT_DEVICE_SIZE;
  1680. gi->cdev.desc.bDescriptorType = USB_DT_DEVICE;
  1681. gi->cdev.desc.bcdDevice = cpu_to_le16(get_default_bcdDevice());
  1682. gi->composite.gadget_driver = configfs_driver_template;
  1683. gi->composite.gadget_driver.driver.name = kasprintf(GFP_KERNEL,
  1684. "configfs-gadget.%s", name);
  1685. if (!gi->composite.gadget_driver.driver.name)
  1686. goto err;
  1687. gi->composite.gadget_driver.function = kstrdup(name, GFP_KERNEL);
  1688. gi->composite.name = gi->composite.gadget_driver.function;
  1689. if (!gi->composite.gadget_driver.function)
  1690. goto out_free_driver_name;
  1691. return &gi->group;
  1692. out_free_driver_name:
  1693. kfree(gi->composite.gadget_driver.driver.name);
  1694. err:
  1695. kfree(gi);
  1696. return ERR_PTR(-ENOMEM);
  1697. }
  1698. static void gadgets_drop(struct config_group *group, struct config_item *item)
  1699. {
  1700. config_item_put(item);
  1701. }
  1702. static struct configfs_group_operations gadgets_ops = {
  1703. .make_group = &gadgets_make,
  1704. .drop_item = &gadgets_drop,
  1705. };
  1706. static const struct config_item_type gadgets_type = {
  1707. .ct_group_ops = &gadgets_ops,
  1708. .ct_owner = THIS_MODULE,
  1709. };
  1710. static struct configfs_subsystem gadget_subsys = {
  1711. .su_group = {
  1712. .cg_item = {
  1713. .ci_namebuf = "usb_gadget",
  1714. .ci_type = &gadgets_type,
  1715. },
  1716. },
  1717. .su_mutex = __MUTEX_INITIALIZER(gadget_subsys.su_mutex),
  1718. };
  1719. void unregister_gadget_item(struct config_item *item)
  1720. {
  1721. struct gadget_info *gi = to_gadget_info(item);
  1722. mutex_lock(&gi->lock);
  1723. unregister_gadget(gi);
  1724. mutex_unlock(&gi->lock);
  1725. }
  1726. EXPORT_SYMBOL_GPL(unregister_gadget_item);
  1727. static int __init gadget_cfs_init(void)
  1728. {
  1729. int ret;
  1730. config_group_init(&gadget_subsys.su_group);
  1731. ret = configfs_register_subsystem(&gadget_subsys);
  1732. return ret;
  1733. }
  1734. module_init(gadget_cfs_init);
  1735. static void __exit gadget_cfs_exit(void)
  1736. {
  1737. configfs_unregister_subsystem(&gadget_subsys);
  1738. }
  1739. module_exit(gadget_cfs_exit);