configfs.c 47 KB

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