composite.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. /*
  2. * composite.c - infrastructure for Composite USB Gadgets
  3. *
  4. * Copyright (C) 2006-2008 David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. /* #define VERBOSE_DEBUG */
  12. #include <linux/kallsyms.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/device.h>
  17. #include <linux/utsname.h>
  18. #include <linux/usb/composite.h>
  19. #include <asm/unaligned.h>
  20. /*
  21. * The code in this file is utility code, used to build a gadget driver
  22. * from one or more "function" drivers, one or more "configuration"
  23. * objects, and a "usb_composite_driver" by gluing them together along
  24. * with the relevant device-wide data.
  25. */
  26. static struct usb_gadget_strings **get_containers_gs(
  27. struct usb_gadget_string_container *uc)
  28. {
  29. return (struct usb_gadget_strings **)uc->stash;
  30. }
  31. /**
  32. * next_ep_desc() - advance to the next EP descriptor
  33. * @t: currect pointer within descriptor array
  34. *
  35. * Return: next EP descriptor or NULL
  36. *
  37. * Iterate over @t until either EP descriptor found or
  38. * NULL (that indicates end of list) encountered
  39. */
  40. static struct usb_descriptor_header**
  41. next_ep_desc(struct usb_descriptor_header **t)
  42. {
  43. for (; *t; t++) {
  44. if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
  45. return t;
  46. }
  47. return NULL;
  48. }
  49. /*
  50. * for_each_ep_desc()- iterate over endpoint descriptors in the
  51. * descriptors list
  52. * @start: pointer within descriptor array.
  53. * @ep_desc: endpoint descriptor to use as the loop cursor
  54. */
  55. #define for_each_ep_desc(start, ep_desc) \
  56. for (ep_desc = next_ep_desc(start); \
  57. ep_desc; ep_desc = next_ep_desc(ep_desc+1))
  58. /**
  59. * config_ep_by_speed() - configures the given endpoint
  60. * according to gadget speed.
  61. * @g: pointer to the gadget
  62. * @f: usb function
  63. * @_ep: the endpoint to configure
  64. *
  65. * Return: error code, 0 on success
  66. *
  67. * This function chooses the right descriptors for a given
  68. * endpoint according to gadget speed and saves it in the
  69. * endpoint desc field. If the endpoint already has a descriptor
  70. * assigned to it - overwrites it with currently corresponding
  71. * descriptor. The endpoint maxpacket field is updated according
  72. * to the chosen descriptor.
  73. * Note: the supplied function should hold all the descriptors
  74. * for supported speeds
  75. */
  76. int config_ep_by_speed(struct usb_gadget *g,
  77. struct usb_function *f,
  78. struct usb_ep *_ep)
  79. {
  80. struct usb_composite_dev *cdev = get_gadget_data(g);
  81. struct usb_endpoint_descriptor *chosen_desc = NULL;
  82. struct usb_descriptor_header **speed_desc = NULL;
  83. struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
  84. int want_comp_desc = 0;
  85. struct usb_descriptor_header **d_spd; /* cursor for speed desc */
  86. if (!g || !f || !_ep)
  87. return -EIO;
  88. /* select desired speed */
  89. switch (g->speed) {
  90. case USB_SPEED_SUPER:
  91. if (gadget_is_superspeed(g)) {
  92. speed_desc = f->ss_descriptors;
  93. want_comp_desc = 1;
  94. break;
  95. }
  96. /* else: Fall trough */
  97. case USB_SPEED_HIGH:
  98. if (gadget_is_dualspeed(g)) {
  99. speed_desc = f->hs_descriptors;
  100. break;
  101. }
  102. /* else: fall through */
  103. default:
  104. speed_desc = f->fs_descriptors;
  105. }
  106. /* find descriptors */
  107. for_each_ep_desc(speed_desc, d_spd) {
  108. chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
  109. if (chosen_desc->bEndpointAddress == _ep->address)
  110. goto ep_found;
  111. }
  112. return -EIO;
  113. ep_found:
  114. /* commit results */
  115. _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
  116. _ep->desc = chosen_desc;
  117. _ep->comp_desc = NULL;
  118. _ep->maxburst = 0;
  119. _ep->mult = 0;
  120. if (!want_comp_desc)
  121. return 0;
  122. /*
  123. * Companion descriptor should follow EP descriptor
  124. * USB 3.0 spec, #9.6.7
  125. */
  126. comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
  127. if (!comp_desc ||
  128. (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
  129. return -EIO;
  130. _ep->comp_desc = comp_desc;
  131. if (g->speed == USB_SPEED_SUPER) {
  132. switch (usb_endpoint_type(_ep->desc)) {
  133. case USB_ENDPOINT_XFER_ISOC:
  134. /* mult: bits 1:0 of bmAttributes */
  135. _ep->mult = comp_desc->bmAttributes & 0x3;
  136. case USB_ENDPOINT_XFER_BULK:
  137. case USB_ENDPOINT_XFER_INT:
  138. _ep->maxburst = comp_desc->bMaxBurst + 1;
  139. break;
  140. default:
  141. if (comp_desc->bMaxBurst != 0)
  142. ERROR(cdev, "ep0 bMaxBurst must be 0\n");
  143. _ep->maxburst = 1;
  144. break;
  145. }
  146. }
  147. return 0;
  148. }
  149. EXPORT_SYMBOL_GPL(config_ep_by_speed);
  150. /**
  151. * usb_add_function() - add a function to a configuration
  152. * @config: the configuration
  153. * @function: the function being added
  154. * Context: single threaded during gadget setup
  155. *
  156. * After initialization, each configuration must have one or more
  157. * functions added to it. Adding a function involves calling its @bind()
  158. * method to allocate resources such as interface and string identifiers
  159. * and endpoints.
  160. *
  161. * This function returns the value of the function's bind(), which is
  162. * zero for success else a negative errno value.
  163. */
  164. int usb_add_function(struct usb_configuration *config,
  165. struct usb_function *function)
  166. {
  167. int value = -EINVAL;
  168. DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
  169. function->name, function,
  170. config->label, config);
  171. if (!function->set_alt || !function->disable)
  172. goto done;
  173. function->config = config;
  174. list_add_tail(&function->list, &config->functions);
  175. /* REVISIT *require* function->bind? */
  176. if (function->bind) {
  177. value = function->bind(config, function);
  178. if (value < 0) {
  179. list_del(&function->list);
  180. function->config = NULL;
  181. }
  182. } else
  183. value = 0;
  184. /* We allow configurations that don't work at both speeds.
  185. * If we run into a lowspeed Linux system, treat it the same
  186. * as full speed ... it's the function drivers that will need
  187. * to avoid bulk and ISO transfers.
  188. */
  189. if (!config->fullspeed && function->fs_descriptors)
  190. config->fullspeed = true;
  191. if (!config->highspeed && function->hs_descriptors)
  192. config->highspeed = true;
  193. if (!config->superspeed && function->ss_descriptors)
  194. config->superspeed = true;
  195. done:
  196. if (value)
  197. DBG(config->cdev, "adding '%s'/%p --> %d\n",
  198. function->name, function, value);
  199. return value;
  200. }
  201. EXPORT_SYMBOL_GPL(usb_add_function);
  202. void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
  203. {
  204. if (f->disable)
  205. f->disable(f);
  206. bitmap_zero(f->endpoints, 32);
  207. list_del(&f->list);
  208. if (f->unbind)
  209. f->unbind(c, f);
  210. }
  211. EXPORT_SYMBOL_GPL(usb_remove_function);
  212. /**
  213. * usb_function_deactivate - prevent function and gadget enumeration
  214. * @function: the function that isn't yet ready to respond
  215. *
  216. * Blocks response of the gadget driver to host enumeration by
  217. * preventing the data line pullup from being activated. This is
  218. * normally called during @bind() processing to change from the
  219. * initial "ready to respond" state, or when a required resource
  220. * becomes available.
  221. *
  222. * For example, drivers that serve as a passthrough to a userspace
  223. * daemon can block enumeration unless that daemon (such as an OBEX,
  224. * MTP, or print server) is ready to handle host requests.
  225. *
  226. * Not all systems support software control of their USB peripheral
  227. * data pullups.
  228. *
  229. * Returns zero on success, else negative errno.
  230. */
  231. int usb_function_deactivate(struct usb_function *function)
  232. {
  233. struct usb_composite_dev *cdev = function->config->cdev;
  234. unsigned long flags;
  235. int status = 0;
  236. spin_lock_irqsave(&cdev->lock, flags);
  237. if (cdev->deactivations == 0)
  238. status = usb_gadget_disconnect(cdev->gadget);
  239. if (status == 0)
  240. cdev->deactivations++;
  241. spin_unlock_irqrestore(&cdev->lock, flags);
  242. return status;
  243. }
  244. EXPORT_SYMBOL_GPL(usb_function_deactivate);
  245. /**
  246. * usb_function_activate - allow function and gadget enumeration
  247. * @function: function on which usb_function_activate() was called
  248. *
  249. * Reverses effect of usb_function_deactivate(). If no more functions
  250. * are delaying their activation, the gadget driver will respond to
  251. * host enumeration procedures.
  252. *
  253. * Returns zero on success, else negative errno.
  254. */
  255. int usb_function_activate(struct usb_function *function)
  256. {
  257. struct usb_composite_dev *cdev = function->config->cdev;
  258. unsigned long flags;
  259. int status = 0;
  260. spin_lock_irqsave(&cdev->lock, flags);
  261. if (WARN_ON(cdev->deactivations == 0))
  262. status = -EINVAL;
  263. else {
  264. cdev->deactivations--;
  265. if (cdev->deactivations == 0)
  266. status = usb_gadget_connect(cdev->gadget);
  267. }
  268. spin_unlock_irqrestore(&cdev->lock, flags);
  269. return status;
  270. }
  271. EXPORT_SYMBOL_GPL(usb_function_activate);
  272. /**
  273. * usb_interface_id() - allocate an unused interface ID
  274. * @config: configuration associated with the interface
  275. * @function: function handling the interface
  276. * Context: single threaded during gadget setup
  277. *
  278. * usb_interface_id() is called from usb_function.bind() callbacks to
  279. * allocate new interface IDs. The function driver will then store that
  280. * ID in interface, association, CDC union, and other descriptors. It
  281. * will also handle any control requests targeted at that interface,
  282. * particularly changing its altsetting via set_alt(). There may
  283. * also be class-specific or vendor-specific requests to handle.
  284. *
  285. * All interface identifier should be allocated using this routine, to
  286. * ensure that for example different functions don't wrongly assign
  287. * different meanings to the same identifier. Note that since interface
  288. * identifiers are configuration-specific, functions used in more than
  289. * one configuration (or more than once in a given configuration) need
  290. * multiple versions of the relevant descriptors.
  291. *
  292. * Returns the interface ID which was allocated; or -ENODEV if no
  293. * more interface IDs can be allocated.
  294. */
  295. int usb_interface_id(struct usb_configuration *config,
  296. struct usb_function *function)
  297. {
  298. unsigned id = config->next_interface_id;
  299. if (id < MAX_CONFIG_INTERFACES) {
  300. config->interface[id] = function;
  301. config->next_interface_id = id + 1;
  302. return id;
  303. }
  304. return -ENODEV;
  305. }
  306. EXPORT_SYMBOL_GPL(usb_interface_id);
  307. static u8 encode_bMaxPower(enum usb_device_speed speed,
  308. struct usb_configuration *c)
  309. {
  310. unsigned val;
  311. if (c->MaxPower)
  312. val = c->MaxPower;
  313. else
  314. val = CONFIG_USB_GADGET_VBUS_DRAW;
  315. if (!val)
  316. return 0;
  317. switch (speed) {
  318. case USB_SPEED_SUPER:
  319. return DIV_ROUND_UP(val, 8);
  320. default:
  321. return DIV_ROUND_UP(val, 2);
  322. };
  323. }
  324. static int config_buf(struct usb_configuration *config,
  325. enum usb_device_speed speed, void *buf, u8 type)
  326. {
  327. struct usb_config_descriptor *c = buf;
  328. void *next = buf + USB_DT_CONFIG_SIZE;
  329. int len;
  330. struct usb_function *f;
  331. int status;
  332. len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
  333. /* write the config descriptor */
  334. c = buf;
  335. c->bLength = USB_DT_CONFIG_SIZE;
  336. c->bDescriptorType = type;
  337. /* wTotalLength is written later */
  338. c->bNumInterfaces = config->next_interface_id;
  339. c->bConfigurationValue = config->bConfigurationValue;
  340. c->iConfiguration = config->iConfiguration;
  341. c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
  342. c->bMaxPower = encode_bMaxPower(speed, config);
  343. /* There may be e.g. OTG descriptors */
  344. if (config->descriptors) {
  345. status = usb_descriptor_fillbuf(next, len,
  346. config->descriptors);
  347. if (status < 0)
  348. return status;
  349. len -= status;
  350. next += status;
  351. }
  352. /* add each function's descriptors */
  353. list_for_each_entry(f, &config->functions, list) {
  354. struct usb_descriptor_header **descriptors;
  355. switch (speed) {
  356. case USB_SPEED_SUPER:
  357. descriptors = f->ss_descriptors;
  358. break;
  359. case USB_SPEED_HIGH:
  360. descriptors = f->hs_descriptors;
  361. break;
  362. default:
  363. descriptors = f->fs_descriptors;
  364. }
  365. if (!descriptors)
  366. continue;
  367. status = usb_descriptor_fillbuf(next, len,
  368. (const struct usb_descriptor_header **) descriptors);
  369. if (status < 0)
  370. return status;
  371. len -= status;
  372. next += status;
  373. }
  374. len = next - buf;
  375. c->wTotalLength = cpu_to_le16(len);
  376. return len;
  377. }
  378. static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
  379. {
  380. struct usb_gadget *gadget = cdev->gadget;
  381. struct usb_configuration *c;
  382. u8 type = w_value >> 8;
  383. enum usb_device_speed speed = USB_SPEED_UNKNOWN;
  384. if (gadget->speed == USB_SPEED_SUPER)
  385. speed = gadget->speed;
  386. else if (gadget_is_dualspeed(gadget)) {
  387. int hs = 0;
  388. if (gadget->speed == USB_SPEED_HIGH)
  389. hs = 1;
  390. if (type == USB_DT_OTHER_SPEED_CONFIG)
  391. hs = !hs;
  392. if (hs)
  393. speed = USB_SPEED_HIGH;
  394. }
  395. /* This is a lookup by config *INDEX* */
  396. w_value &= 0xff;
  397. list_for_each_entry(c, &cdev->configs, list) {
  398. /* ignore configs that won't work at this speed */
  399. switch (speed) {
  400. case USB_SPEED_SUPER:
  401. if (!c->superspeed)
  402. continue;
  403. break;
  404. case USB_SPEED_HIGH:
  405. if (!c->highspeed)
  406. continue;
  407. break;
  408. default:
  409. if (!c->fullspeed)
  410. continue;
  411. }
  412. if (w_value == 0)
  413. return config_buf(c, speed, cdev->req->buf, type);
  414. w_value--;
  415. }
  416. return -EINVAL;
  417. }
  418. static int count_configs(struct usb_composite_dev *cdev, unsigned type)
  419. {
  420. struct usb_gadget *gadget = cdev->gadget;
  421. struct usb_configuration *c;
  422. unsigned count = 0;
  423. int hs = 0;
  424. int ss = 0;
  425. if (gadget_is_dualspeed(gadget)) {
  426. if (gadget->speed == USB_SPEED_HIGH)
  427. hs = 1;
  428. if (gadget->speed == USB_SPEED_SUPER)
  429. ss = 1;
  430. if (type == USB_DT_DEVICE_QUALIFIER)
  431. hs = !hs;
  432. }
  433. list_for_each_entry(c, &cdev->configs, list) {
  434. /* ignore configs that won't work at this speed */
  435. if (ss) {
  436. if (!c->superspeed)
  437. continue;
  438. } else if (hs) {
  439. if (!c->highspeed)
  440. continue;
  441. } else {
  442. if (!c->fullspeed)
  443. continue;
  444. }
  445. count++;
  446. }
  447. return count;
  448. }
  449. /**
  450. * bos_desc() - prepares the BOS descriptor.
  451. * @cdev: pointer to usb_composite device to generate the bos
  452. * descriptor for
  453. *
  454. * This function generates the BOS (Binary Device Object)
  455. * descriptor and its device capabilities descriptors. The BOS
  456. * descriptor should be supported by a SuperSpeed device.
  457. */
  458. static int bos_desc(struct usb_composite_dev *cdev)
  459. {
  460. struct usb_ext_cap_descriptor *usb_ext;
  461. struct usb_ss_cap_descriptor *ss_cap;
  462. struct usb_dcd_config_params dcd_config_params;
  463. struct usb_bos_descriptor *bos = cdev->req->buf;
  464. bos->bLength = USB_DT_BOS_SIZE;
  465. bos->bDescriptorType = USB_DT_BOS;
  466. bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
  467. bos->bNumDeviceCaps = 0;
  468. /*
  469. * A SuperSpeed device shall include the USB2.0 extension descriptor
  470. * and shall support LPM when operating in USB2.0 HS mode.
  471. */
  472. usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  473. bos->bNumDeviceCaps++;
  474. le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
  475. usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
  476. usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  477. usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
  478. usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT);
  479. /*
  480. * The Superspeed USB Capability descriptor shall be implemented by all
  481. * SuperSpeed devices.
  482. */
  483. ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  484. bos->bNumDeviceCaps++;
  485. le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
  486. ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
  487. ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  488. ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
  489. ss_cap->bmAttributes = 0; /* LTM is not supported yet */
  490. ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
  491. USB_FULL_SPEED_OPERATION |
  492. USB_HIGH_SPEED_OPERATION |
  493. USB_5GBPS_OPERATION);
  494. ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
  495. /* Get Controller configuration */
  496. if (cdev->gadget->ops->get_config_params)
  497. cdev->gadget->ops->get_config_params(&dcd_config_params);
  498. else {
  499. dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
  500. dcd_config_params.bU2DevExitLat =
  501. cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
  502. }
  503. ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
  504. ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
  505. return le16_to_cpu(bos->wTotalLength);
  506. }
  507. static void device_qual(struct usb_composite_dev *cdev)
  508. {
  509. struct usb_qualifier_descriptor *qual = cdev->req->buf;
  510. qual->bLength = sizeof(*qual);
  511. qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  512. /* POLICY: same bcdUSB and device type info at both speeds */
  513. qual->bcdUSB = cdev->desc.bcdUSB;
  514. qual->bDeviceClass = cdev->desc.bDeviceClass;
  515. qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
  516. qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
  517. /* ASSUME same EP0 fifo size at both speeds */
  518. qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
  519. qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
  520. qual->bRESERVED = 0;
  521. }
  522. /*-------------------------------------------------------------------------*/
  523. static void reset_config(struct usb_composite_dev *cdev)
  524. {
  525. struct usb_function *f;
  526. DBG(cdev, "reset config\n");
  527. list_for_each_entry(f, &cdev->config->functions, list) {
  528. if (f->disable)
  529. f->disable(f);
  530. bitmap_zero(f->endpoints, 32);
  531. }
  532. cdev->config = NULL;
  533. }
  534. static int set_config(struct usb_composite_dev *cdev,
  535. const struct usb_ctrlrequest *ctrl, unsigned number)
  536. {
  537. struct usb_gadget *gadget = cdev->gadget;
  538. struct usb_configuration *c = NULL;
  539. int result = -EINVAL;
  540. unsigned power = gadget_is_otg(gadget) ? 8 : 100;
  541. int tmp;
  542. if (number) {
  543. list_for_each_entry(c, &cdev->configs, list) {
  544. if (c->bConfigurationValue == number) {
  545. /*
  546. * We disable the FDs of the previous
  547. * configuration only if the new configuration
  548. * is a valid one
  549. */
  550. if (cdev->config)
  551. reset_config(cdev);
  552. result = 0;
  553. break;
  554. }
  555. }
  556. if (result < 0)
  557. goto done;
  558. } else { /* Zero configuration value - need to reset the config */
  559. if (cdev->config)
  560. reset_config(cdev);
  561. result = 0;
  562. }
  563. INFO(cdev, "%s config #%d: %s\n",
  564. usb_speed_string(gadget->speed),
  565. number, c ? c->label : "unconfigured");
  566. if (!c)
  567. goto done;
  568. cdev->config = c;
  569. /* Initialize all interfaces by setting them to altsetting zero. */
  570. for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
  571. struct usb_function *f = c->interface[tmp];
  572. struct usb_descriptor_header **descriptors;
  573. if (!f)
  574. break;
  575. /*
  576. * Record which endpoints are used by the function. This is used
  577. * to dispatch control requests targeted at that endpoint to the
  578. * function's setup callback instead of the current
  579. * configuration's setup callback.
  580. */
  581. switch (gadget->speed) {
  582. case USB_SPEED_SUPER:
  583. descriptors = f->ss_descriptors;
  584. break;
  585. case USB_SPEED_HIGH:
  586. descriptors = f->hs_descriptors;
  587. break;
  588. default:
  589. descriptors = f->fs_descriptors;
  590. }
  591. for (; *descriptors; ++descriptors) {
  592. struct usb_endpoint_descriptor *ep;
  593. int addr;
  594. if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
  595. continue;
  596. ep = (struct usb_endpoint_descriptor *)*descriptors;
  597. addr = ((ep->bEndpointAddress & 0x80) >> 3)
  598. | (ep->bEndpointAddress & 0x0f);
  599. set_bit(addr, f->endpoints);
  600. }
  601. result = f->set_alt(f, tmp, 0);
  602. if (result < 0) {
  603. DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
  604. tmp, f->name, f, result);
  605. reset_config(cdev);
  606. goto done;
  607. }
  608. if (result == USB_GADGET_DELAYED_STATUS) {
  609. DBG(cdev,
  610. "%s: interface %d (%s) requested delayed status\n",
  611. __func__, tmp, f->name);
  612. cdev->delayed_status++;
  613. DBG(cdev, "delayed_status count %d\n",
  614. cdev->delayed_status);
  615. }
  616. }
  617. /* when we return, be sure our power usage is valid */
  618. power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
  619. done:
  620. usb_gadget_vbus_draw(gadget, power);
  621. if (result >= 0 && cdev->delayed_status)
  622. result = USB_GADGET_DELAYED_STATUS;
  623. return result;
  624. }
  625. int usb_add_config_only(struct usb_composite_dev *cdev,
  626. struct usb_configuration *config)
  627. {
  628. struct usb_configuration *c;
  629. if (!config->bConfigurationValue)
  630. return -EINVAL;
  631. /* Prevent duplicate configuration identifiers */
  632. list_for_each_entry(c, &cdev->configs, list) {
  633. if (c->bConfigurationValue == config->bConfigurationValue)
  634. return -EBUSY;
  635. }
  636. config->cdev = cdev;
  637. list_add_tail(&config->list, &cdev->configs);
  638. INIT_LIST_HEAD(&config->functions);
  639. config->next_interface_id = 0;
  640. memset(config->interface, 0, sizeof(config->interface));
  641. return 0;
  642. }
  643. EXPORT_SYMBOL_GPL(usb_add_config_only);
  644. /**
  645. * usb_add_config() - add a configuration to a device.
  646. * @cdev: wraps the USB gadget
  647. * @config: the configuration, with bConfigurationValue assigned
  648. * @bind: the configuration's bind function
  649. * Context: single threaded during gadget setup
  650. *
  651. * One of the main tasks of a composite @bind() routine is to
  652. * add each of the configurations it supports, using this routine.
  653. *
  654. * This function returns the value of the configuration's @bind(), which
  655. * is zero for success else a negative errno value. Binding configurations
  656. * assigns global resources including string IDs, and per-configuration
  657. * resources such as interface IDs and endpoints.
  658. */
  659. int usb_add_config(struct usb_composite_dev *cdev,
  660. struct usb_configuration *config,
  661. int (*bind)(struct usb_configuration *))
  662. {
  663. int status = -EINVAL;
  664. if (!bind)
  665. goto done;
  666. DBG(cdev, "adding config #%u '%s'/%p\n",
  667. config->bConfigurationValue,
  668. config->label, config);
  669. status = usb_add_config_only(cdev, config);
  670. if (status)
  671. goto done;
  672. status = bind(config);
  673. if (status < 0) {
  674. while (!list_empty(&config->functions)) {
  675. struct usb_function *f;
  676. f = list_first_entry(&config->functions,
  677. struct usb_function, list);
  678. list_del(&f->list);
  679. if (f->unbind) {
  680. DBG(cdev, "unbind function '%s'/%p\n",
  681. f->name, f);
  682. f->unbind(config, f);
  683. /* may free memory for "f" */
  684. }
  685. }
  686. list_del(&config->list);
  687. config->cdev = NULL;
  688. } else {
  689. unsigned i;
  690. DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
  691. config->bConfigurationValue, config,
  692. config->superspeed ? " super" : "",
  693. config->highspeed ? " high" : "",
  694. config->fullspeed
  695. ? (gadget_is_dualspeed(cdev->gadget)
  696. ? " full"
  697. : " full/low")
  698. : "");
  699. for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
  700. struct usb_function *f = config->interface[i];
  701. if (!f)
  702. continue;
  703. DBG(cdev, " interface %d = %s/%p\n",
  704. i, f->name, f);
  705. }
  706. }
  707. /* set_alt(), or next bind(), sets up
  708. * ep->driver_data as needed.
  709. */
  710. usb_ep_autoconfig_reset(cdev->gadget);
  711. done:
  712. if (status)
  713. DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
  714. config->bConfigurationValue, status);
  715. return status;
  716. }
  717. EXPORT_SYMBOL_GPL(usb_add_config);
  718. static void remove_config(struct usb_composite_dev *cdev,
  719. struct usb_configuration *config)
  720. {
  721. while (!list_empty(&config->functions)) {
  722. struct usb_function *f;
  723. f = list_first_entry(&config->functions,
  724. struct usb_function, list);
  725. list_del(&f->list);
  726. if (f->unbind) {
  727. DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
  728. f->unbind(config, f);
  729. /* may free memory for "f" */
  730. }
  731. }
  732. list_del(&config->list);
  733. if (config->unbind) {
  734. DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
  735. config->unbind(config);
  736. /* may free memory for "c" */
  737. }
  738. }
  739. /**
  740. * usb_remove_config() - remove a configuration from a device.
  741. * @cdev: wraps the USB gadget
  742. * @config: the configuration
  743. *
  744. * Drivers must call usb_gadget_disconnect before calling this function
  745. * to disconnect the device from the host and make sure the host will not
  746. * try to enumerate the device while we are changing the config list.
  747. */
  748. void usb_remove_config(struct usb_composite_dev *cdev,
  749. struct usb_configuration *config)
  750. {
  751. unsigned long flags;
  752. spin_lock_irqsave(&cdev->lock, flags);
  753. if (cdev->config == config)
  754. reset_config(cdev);
  755. spin_unlock_irqrestore(&cdev->lock, flags);
  756. remove_config(cdev, config);
  757. }
  758. /*-------------------------------------------------------------------------*/
  759. /* We support strings in multiple languages ... string descriptor zero
  760. * says which languages are supported. The typical case will be that
  761. * only one language (probably English) is used, with I18N handled on
  762. * the host side.
  763. */
  764. static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
  765. {
  766. const struct usb_gadget_strings *s;
  767. __le16 language;
  768. __le16 *tmp;
  769. while (*sp) {
  770. s = *sp;
  771. language = cpu_to_le16(s->language);
  772. for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
  773. if (*tmp == language)
  774. goto repeat;
  775. }
  776. *tmp++ = language;
  777. repeat:
  778. sp++;
  779. }
  780. }
  781. static int lookup_string(
  782. struct usb_gadget_strings **sp,
  783. void *buf,
  784. u16 language,
  785. int id
  786. )
  787. {
  788. struct usb_gadget_strings *s;
  789. int value;
  790. while (*sp) {
  791. s = *sp++;
  792. if (s->language != language)
  793. continue;
  794. value = usb_gadget_get_string(s, id, buf);
  795. if (value > 0)
  796. return value;
  797. }
  798. return -EINVAL;
  799. }
  800. static int get_string(struct usb_composite_dev *cdev,
  801. void *buf, u16 language, int id)
  802. {
  803. struct usb_composite_driver *composite = cdev->driver;
  804. struct usb_gadget_string_container *uc;
  805. struct usb_configuration *c;
  806. struct usb_function *f;
  807. int len;
  808. /* Yes, not only is USB's I18N support probably more than most
  809. * folk will ever care about ... also, it's all supported here.
  810. * (Except for UTF8 support for Unicode's "Astral Planes".)
  811. */
  812. /* 0 == report all available language codes */
  813. if (id == 0) {
  814. struct usb_string_descriptor *s = buf;
  815. struct usb_gadget_strings **sp;
  816. memset(s, 0, 256);
  817. s->bDescriptorType = USB_DT_STRING;
  818. sp = composite->strings;
  819. if (sp)
  820. collect_langs(sp, s->wData);
  821. list_for_each_entry(c, &cdev->configs, list) {
  822. sp = c->strings;
  823. if (sp)
  824. collect_langs(sp, s->wData);
  825. list_for_each_entry(f, &c->functions, list) {
  826. sp = f->strings;
  827. if (sp)
  828. collect_langs(sp, s->wData);
  829. }
  830. }
  831. list_for_each_entry(uc, &cdev->gstrings, list) {
  832. struct usb_gadget_strings **sp;
  833. sp = get_containers_gs(uc);
  834. collect_langs(sp, s->wData);
  835. }
  836. for (len = 0; len <= 126 && s->wData[len]; len++)
  837. continue;
  838. if (!len)
  839. return -EINVAL;
  840. s->bLength = 2 * (len + 1);
  841. return s->bLength;
  842. }
  843. list_for_each_entry(uc, &cdev->gstrings, list) {
  844. struct usb_gadget_strings **sp;
  845. sp = get_containers_gs(uc);
  846. len = lookup_string(sp, buf, language, id);
  847. if (len > 0)
  848. return len;
  849. }
  850. /* String IDs are device-scoped, so we look up each string
  851. * table we're told about. These lookups are infrequent;
  852. * simpler-is-better here.
  853. */
  854. if (composite->strings) {
  855. len = lookup_string(composite->strings, buf, language, id);
  856. if (len > 0)
  857. return len;
  858. }
  859. list_for_each_entry(c, &cdev->configs, list) {
  860. if (c->strings) {
  861. len = lookup_string(c->strings, buf, language, id);
  862. if (len > 0)
  863. return len;
  864. }
  865. list_for_each_entry(f, &c->functions, list) {
  866. if (!f->strings)
  867. continue;
  868. len = lookup_string(f->strings, buf, language, id);
  869. if (len > 0)
  870. return len;
  871. }
  872. }
  873. return -EINVAL;
  874. }
  875. /**
  876. * usb_string_id() - allocate an unused string ID
  877. * @cdev: the device whose string descriptor IDs are being allocated
  878. * Context: single threaded during gadget setup
  879. *
  880. * @usb_string_id() is called from bind() callbacks to allocate
  881. * string IDs. Drivers for functions, configurations, or gadgets will
  882. * then store that ID in the appropriate descriptors and string table.
  883. *
  884. * All string identifier should be allocated using this,
  885. * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
  886. * that for example different functions don't wrongly assign different
  887. * meanings to the same identifier.
  888. */
  889. int usb_string_id(struct usb_composite_dev *cdev)
  890. {
  891. if (cdev->next_string_id < 254) {
  892. /* string id 0 is reserved by USB spec for list of
  893. * supported languages */
  894. /* 255 reserved as well? -- mina86 */
  895. cdev->next_string_id++;
  896. return cdev->next_string_id;
  897. }
  898. return -ENODEV;
  899. }
  900. EXPORT_SYMBOL_GPL(usb_string_id);
  901. /**
  902. * usb_string_ids() - allocate unused string IDs in batch
  903. * @cdev: the device whose string descriptor IDs are being allocated
  904. * @str: an array of usb_string objects to assign numbers to
  905. * Context: single threaded during gadget setup
  906. *
  907. * @usb_string_ids() is called from bind() callbacks to allocate
  908. * string IDs. Drivers for functions, configurations, or gadgets will
  909. * then copy IDs from the string table to the appropriate descriptors
  910. * and string table for other languages.
  911. *
  912. * All string identifier should be allocated using this,
  913. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  914. * example different functions don't wrongly assign different meanings
  915. * to the same identifier.
  916. */
  917. int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
  918. {
  919. int next = cdev->next_string_id;
  920. for (; str->s; ++str) {
  921. if (unlikely(next >= 254))
  922. return -ENODEV;
  923. str->id = ++next;
  924. }
  925. cdev->next_string_id = next;
  926. return 0;
  927. }
  928. EXPORT_SYMBOL_GPL(usb_string_ids_tab);
  929. static struct usb_gadget_string_container *copy_gadget_strings(
  930. struct usb_gadget_strings **sp, unsigned n_gstrings,
  931. unsigned n_strings)
  932. {
  933. struct usb_gadget_string_container *uc;
  934. struct usb_gadget_strings **gs_array;
  935. struct usb_gadget_strings *gs;
  936. struct usb_string *s;
  937. unsigned mem;
  938. unsigned n_gs;
  939. unsigned n_s;
  940. void *stash;
  941. mem = sizeof(*uc);
  942. mem += sizeof(void *) * (n_gstrings + 1);
  943. mem += sizeof(struct usb_gadget_strings) * n_gstrings;
  944. mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
  945. uc = kmalloc(mem, GFP_KERNEL);
  946. if (!uc)
  947. return ERR_PTR(-ENOMEM);
  948. gs_array = get_containers_gs(uc);
  949. stash = uc->stash;
  950. stash += sizeof(void *) * (n_gstrings + 1);
  951. for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
  952. struct usb_string *org_s;
  953. gs_array[n_gs] = stash;
  954. gs = gs_array[n_gs];
  955. stash += sizeof(struct usb_gadget_strings);
  956. gs->language = sp[n_gs]->language;
  957. gs->strings = stash;
  958. org_s = sp[n_gs]->strings;
  959. for (n_s = 0; n_s < n_strings; n_s++) {
  960. s = stash;
  961. stash += sizeof(struct usb_string);
  962. if (org_s->s)
  963. s->s = org_s->s;
  964. else
  965. s->s = "";
  966. org_s++;
  967. }
  968. s = stash;
  969. s->s = NULL;
  970. stash += sizeof(struct usb_string);
  971. }
  972. gs_array[n_gs] = NULL;
  973. return uc;
  974. }
  975. /**
  976. * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
  977. * @cdev: the device whose string descriptor IDs are being allocated
  978. * and attached.
  979. * @sp: an array of usb_gadget_strings to attach.
  980. * @n_strings: number of entries in each usb_strings array (sp[]->strings)
  981. *
  982. * This function will create a deep copy of usb_gadget_strings and usb_string
  983. * and attach it to the cdev. The actual string (usb_string.s) will not be
  984. * copied but only a referenced will be made. The struct usb_gadget_strings
  985. * array may contain multiple languges and should be NULL terminated.
  986. * The ->language pointer of each struct usb_gadget_strings has to contain the
  987. * same amount of entries.
  988. * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
  989. * usb_string entry of es-ES containts the translation of the first usb_string
  990. * entry of en-US. Therefore both entries become the same id assign.
  991. */
  992. struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
  993. struct usb_gadget_strings **sp, unsigned n_strings)
  994. {
  995. struct usb_gadget_string_container *uc;
  996. struct usb_gadget_strings **n_gs;
  997. unsigned n_gstrings = 0;
  998. unsigned i;
  999. int ret;
  1000. for (i = 0; sp[i]; i++)
  1001. n_gstrings++;
  1002. if (!n_gstrings)
  1003. return ERR_PTR(-EINVAL);
  1004. uc = copy_gadget_strings(sp, n_gstrings, n_strings);
  1005. if (IS_ERR(uc))
  1006. return ERR_PTR(PTR_ERR(uc));
  1007. n_gs = get_containers_gs(uc);
  1008. ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
  1009. if (ret)
  1010. goto err;
  1011. for (i = 1; i < n_gstrings; i++) {
  1012. struct usb_string *m_s;
  1013. struct usb_string *s;
  1014. unsigned n;
  1015. m_s = n_gs[0]->strings;
  1016. s = n_gs[i]->strings;
  1017. for (n = 0; n < n_strings; n++) {
  1018. s->id = m_s->id;
  1019. s++;
  1020. m_s++;
  1021. }
  1022. }
  1023. list_add_tail(&uc->list, &cdev->gstrings);
  1024. return n_gs[0]->strings;
  1025. err:
  1026. kfree(uc);
  1027. return ERR_PTR(ret);
  1028. }
  1029. EXPORT_SYMBOL_GPL(usb_gstrings_attach);
  1030. /**
  1031. * usb_string_ids_n() - allocate unused string IDs in batch
  1032. * @c: the device whose string descriptor IDs are being allocated
  1033. * @n: number of string IDs to allocate
  1034. * Context: single threaded during gadget setup
  1035. *
  1036. * Returns the first requested ID. This ID and next @n-1 IDs are now
  1037. * valid IDs. At least provided that @n is non-zero because if it
  1038. * is, returns last requested ID which is now very useful information.
  1039. *
  1040. * @usb_string_ids_n() is called from bind() callbacks to allocate
  1041. * string IDs. Drivers for functions, configurations, or gadgets will
  1042. * then store that ID in the appropriate descriptors and string table.
  1043. *
  1044. * All string identifier should be allocated using this,
  1045. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  1046. * example different functions don't wrongly assign different meanings
  1047. * to the same identifier.
  1048. */
  1049. int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
  1050. {
  1051. unsigned next = c->next_string_id;
  1052. if (unlikely(n > 254 || (unsigned)next + n > 254))
  1053. return -ENODEV;
  1054. c->next_string_id += n;
  1055. return next + 1;
  1056. }
  1057. EXPORT_SYMBOL_GPL(usb_string_ids_n);
  1058. /*-------------------------------------------------------------------------*/
  1059. static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
  1060. {
  1061. if (req->status || req->actual != req->length)
  1062. DBG((struct usb_composite_dev *) ep->driver_data,
  1063. "setup complete --> %d, %d/%d\n",
  1064. req->status, req->actual, req->length);
  1065. }
  1066. /*
  1067. * The setup() callback implements all the ep0 functionality that's
  1068. * not handled lower down, in hardware or the hardware driver(like
  1069. * device and endpoint feature flags, and their status). It's all
  1070. * housekeeping for the gadget function we're implementing. Most of
  1071. * the work is in config and function specific setup.
  1072. */
  1073. int
  1074. composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  1075. {
  1076. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1077. struct usb_request *req = cdev->req;
  1078. int value = -EOPNOTSUPP;
  1079. int status = 0;
  1080. u16 w_index = le16_to_cpu(ctrl->wIndex);
  1081. u8 intf = w_index & 0xFF;
  1082. u16 w_value = le16_to_cpu(ctrl->wValue);
  1083. u16 w_length = le16_to_cpu(ctrl->wLength);
  1084. struct usb_function *f = NULL;
  1085. u8 endp;
  1086. /* partial re-init of the response message; the function or the
  1087. * gadget might need to intercept e.g. a control-OUT completion
  1088. * when we delegate to it.
  1089. */
  1090. req->zero = 0;
  1091. req->complete = composite_setup_complete;
  1092. req->length = 0;
  1093. gadget->ep0->driver_data = cdev;
  1094. switch (ctrl->bRequest) {
  1095. /* we handle all standard USB descriptors */
  1096. case USB_REQ_GET_DESCRIPTOR:
  1097. if (ctrl->bRequestType != USB_DIR_IN)
  1098. goto unknown;
  1099. switch (w_value >> 8) {
  1100. case USB_DT_DEVICE:
  1101. cdev->desc.bNumConfigurations =
  1102. count_configs(cdev, USB_DT_DEVICE);
  1103. cdev->desc.bMaxPacketSize0 =
  1104. cdev->gadget->ep0->maxpacket;
  1105. if (gadget_is_superspeed(gadget)) {
  1106. if (gadget->speed >= USB_SPEED_SUPER) {
  1107. cdev->desc.bcdUSB = cpu_to_le16(0x0300);
  1108. cdev->desc.bMaxPacketSize0 = 9;
  1109. } else {
  1110. cdev->desc.bcdUSB = cpu_to_le16(0x0210);
  1111. }
  1112. }
  1113. value = min(w_length, (u16) sizeof cdev->desc);
  1114. memcpy(req->buf, &cdev->desc, value);
  1115. break;
  1116. case USB_DT_DEVICE_QUALIFIER:
  1117. if (!gadget_is_dualspeed(gadget) ||
  1118. gadget->speed >= USB_SPEED_SUPER)
  1119. break;
  1120. device_qual(cdev);
  1121. value = min_t(int, w_length,
  1122. sizeof(struct usb_qualifier_descriptor));
  1123. break;
  1124. case USB_DT_OTHER_SPEED_CONFIG:
  1125. if (!gadget_is_dualspeed(gadget) ||
  1126. gadget->speed >= USB_SPEED_SUPER)
  1127. break;
  1128. /* FALLTHROUGH */
  1129. case USB_DT_CONFIG:
  1130. value = config_desc(cdev, w_value);
  1131. if (value >= 0)
  1132. value = min(w_length, (u16) value);
  1133. break;
  1134. case USB_DT_STRING:
  1135. value = get_string(cdev, req->buf,
  1136. w_index, w_value & 0xff);
  1137. if (value >= 0)
  1138. value = min(w_length, (u16) value);
  1139. break;
  1140. case USB_DT_BOS:
  1141. if (gadget_is_superspeed(gadget)) {
  1142. value = bos_desc(cdev);
  1143. value = min(w_length, (u16) value);
  1144. }
  1145. break;
  1146. }
  1147. break;
  1148. /* any number of configs can work */
  1149. case USB_REQ_SET_CONFIGURATION:
  1150. if (ctrl->bRequestType != 0)
  1151. goto unknown;
  1152. if (gadget_is_otg(gadget)) {
  1153. if (gadget->a_hnp_support)
  1154. DBG(cdev, "HNP available\n");
  1155. else if (gadget->a_alt_hnp_support)
  1156. DBG(cdev, "HNP on another port\n");
  1157. else
  1158. VDBG(cdev, "HNP inactive\n");
  1159. }
  1160. spin_lock(&cdev->lock);
  1161. value = set_config(cdev, ctrl, w_value);
  1162. spin_unlock(&cdev->lock);
  1163. break;
  1164. case USB_REQ_GET_CONFIGURATION:
  1165. if (ctrl->bRequestType != USB_DIR_IN)
  1166. goto unknown;
  1167. if (cdev->config)
  1168. *(u8 *)req->buf = cdev->config->bConfigurationValue;
  1169. else
  1170. *(u8 *)req->buf = 0;
  1171. value = min(w_length, (u16) 1);
  1172. break;
  1173. /* function drivers must handle get/set altsetting; if there's
  1174. * no get() method, we know only altsetting zero works.
  1175. */
  1176. case USB_REQ_SET_INTERFACE:
  1177. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  1178. goto unknown;
  1179. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1180. break;
  1181. f = cdev->config->interface[intf];
  1182. if (!f)
  1183. break;
  1184. if (w_value && !f->set_alt)
  1185. break;
  1186. value = f->set_alt(f, w_index, w_value);
  1187. if (value == USB_GADGET_DELAYED_STATUS) {
  1188. DBG(cdev,
  1189. "%s: interface %d (%s) requested delayed status\n",
  1190. __func__, intf, f->name);
  1191. cdev->delayed_status++;
  1192. DBG(cdev, "delayed_status count %d\n",
  1193. cdev->delayed_status);
  1194. }
  1195. break;
  1196. case USB_REQ_GET_INTERFACE:
  1197. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  1198. goto unknown;
  1199. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1200. break;
  1201. f = cdev->config->interface[intf];
  1202. if (!f)
  1203. break;
  1204. /* lots of interfaces only need altsetting zero... */
  1205. value = f->get_alt ? f->get_alt(f, w_index) : 0;
  1206. if (value < 0)
  1207. break;
  1208. *((u8 *)req->buf) = value;
  1209. value = min(w_length, (u16) 1);
  1210. break;
  1211. /*
  1212. * USB 3.0 additions:
  1213. * Function driver should handle get_status request. If such cb
  1214. * wasn't supplied we respond with default value = 0
  1215. * Note: function driver should supply such cb only for the first
  1216. * interface of the function
  1217. */
  1218. case USB_REQ_GET_STATUS:
  1219. if (!gadget_is_superspeed(gadget))
  1220. goto unknown;
  1221. if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
  1222. goto unknown;
  1223. value = 2; /* This is the length of the get_status reply */
  1224. put_unaligned_le16(0, req->buf);
  1225. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1226. break;
  1227. f = cdev->config->interface[intf];
  1228. if (!f)
  1229. break;
  1230. status = f->get_status ? f->get_status(f) : 0;
  1231. if (status < 0)
  1232. break;
  1233. put_unaligned_le16(status & 0x0000ffff, req->buf);
  1234. break;
  1235. /*
  1236. * Function drivers should handle SetFeature/ClearFeature
  1237. * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
  1238. * only for the first interface of the function
  1239. */
  1240. case USB_REQ_CLEAR_FEATURE:
  1241. case USB_REQ_SET_FEATURE:
  1242. if (!gadget_is_superspeed(gadget))
  1243. goto unknown;
  1244. if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
  1245. goto unknown;
  1246. switch (w_value) {
  1247. case USB_INTRF_FUNC_SUSPEND:
  1248. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1249. break;
  1250. f = cdev->config->interface[intf];
  1251. if (!f)
  1252. break;
  1253. value = 0;
  1254. if (f->func_suspend)
  1255. value = f->func_suspend(f, w_index >> 8);
  1256. if (value < 0) {
  1257. ERROR(cdev,
  1258. "func_suspend() returned error %d\n",
  1259. value);
  1260. value = 0;
  1261. }
  1262. break;
  1263. }
  1264. break;
  1265. default:
  1266. unknown:
  1267. VDBG(cdev,
  1268. "non-core control req%02x.%02x v%04x i%04x l%d\n",
  1269. ctrl->bRequestType, ctrl->bRequest,
  1270. w_value, w_index, w_length);
  1271. /* functions always handle their interfaces and endpoints...
  1272. * punt other recipients (other, WUSB, ...) to the current
  1273. * configuration code.
  1274. *
  1275. * REVISIT it could make sense to let the composite device
  1276. * take such requests too, if that's ever needed: to work
  1277. * in config 0, etc.
  1278. */
  1279. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1280. case USB_RECIP_INTERFACE:
  1281. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1282. break;
  1283. f = cdev->config->interface[intf];
  1284. break;
  1285. case USB_RECIP_ENDPOINT:
  1286. endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
  1287. list_for_each_entry(f, &cdev->config->functions, list) {
  1288. if (test_bit(endp, f->endpoints))
  1289. break;
  1290. }
  1291. if (&f->list == &cdev->config->functions)
  1292. f = NULL;
  1293. break;
  1294. }
  1295. if (f && f->setup)
  1296. value = f->setup(f, ctrl);
  1297. else {
  1298. struct usb_configuration *c;
  1299. c = cdev->config;
  1300. if (c && c->setup)
  1301. value = c->setup(c, ctrl);
  1302. }
  1303. goto done;
  1304. }
  1305. /* respond with data transfer before status phase? */
  1306. if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
  1307. req->length = value;
  1308. req->zero = value < w_length;
  1309. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  1310. if (value < 0) {
  1311. DBG(cdev, "ep_queue --> %d\n", value);
  1312. req->status = 0;
  1313. composite_setup_complete(gadget->ep0, req);
  1314. }
  1315. } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
  1316. WARN(cdev,
  1317. "%s: Delayed status not supported for w_length != 0",
  1318. __func__);
  1319. }
  1320. done:
  1321. /* device either stalls (value < 0) or reports success */
  1322. return value;
  1323. }
  1324. void composite_disconnect(struct usb_gadget *gadget)
  1325. {
  1326. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1327. unsigned long flags;
  1328. /* REVISIT: should we have config and device level
  1329. * disconnect callbacks?
  1330. */
  1331. spin_lock_irqsave(&cdev->lock, flags);
  1332. if (cdev->config)
  1333. reset_config(cdev);
  1334. if (cdev->driver->disconnect)
  1335. cdev->driver->disconnect(cdev);
  1336. spin_unlock_irqrestore(&cdev->lock, flags);
  1337. }
  1338. /*-------------------------------------------------------------------------*/
  1339. static ssize_t composite_show_suspended(struct device *dev,
  1340. struct device_attribute *attr,
  1341. char *buf)
  1342. {
  1343. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  1344. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1345. return sprintf(buf, "%d\n", cdev->suspended);
  1346. }
  1347. static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
  1348. static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
  1349. {
  1350. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1351. /* composite_disconnect() must already have been called
  1352. * by the underlying peripheral controller driver!
  1353. * so there's no i/o concurrency that could affect the
  1354. * state protected by cdev->lock.
  1355. */
  1356. WARN_ON(cdev->config);
  1357. while (!list_empty(&cdev->configs)) {
  1358. struct usb_configuration *c;
  1359. c = list_first_entry(&cdev->configs,
  1360. struct usb_configuration, list);
  1361. remove_config(cdev, c);
  1362. }
  1363. if (cdev->driver->unbind && unbind_driver)
  1364. cdev->driver->unbind(cdev);
  1365. composite_dev_cleanup(cdev);
  1366. kfree(cdev->def_manufacturer);
  1367. kfree(cdev);
  1368. set_gadget_data(gadget, NULL);
  1369. }
  1370. static void composite_unbind(struct usb_gadget *gadget)
  1371. {
  1372. __composite_unbind(gadget, true);
  1373. }
  1374. static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
  1375. const struct usb_device_descriptor *old)
  1376. {
  1377. __le16 idVendor;
  1378. __le16 idProduct;
  1379. __le16 bcdDevice;
  1380. u8 iSerialNumber;
  1381. u8 iManufacturer;
  1382. u8 iProduct;
  1383. /*
  1384. * these variables may have been set in
  1385. * usb_composite_overwrite_options()
  1386. */
  1387. idVendor = new->idVendor;
  1388. idProduct = new->idProduct;
  1389. bcdDevice = new->bcdDevice;
  1390. iSerialNumber = new->iSerialNumber;
  1391. iManufacturer = new->iManufacturer;
  1392. iProduct = new->iProduct;
  1393. *new = *old;
  1394. if (idVendor)
  1395. new->idVendor = idVendor;
  1396. if (idProduct)
  1397. new->idProduct = idProduct;
  1398. if (bcdDevice)
  1399. new->bcdDevice = bcdDevice;
  1400. else
  1401. new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
  1402. if (iSerialNumber)
  1403. new->iSerialNumber = iSerialNumber;
  1404. if (iManufacturer)
  1405. new->iManufacturer = iManufacturer;
  1406. if (iProduct)
  1407. new->iProduct = iProduct;
  1408. }
  1409. int composite_dev_prepare(struct usb_composite_driver *composite,
  1410. struct usb_composite_dev *cdev)
  1411. {
  1412. struct usb_gadget *gadget = cdev->gadget;
  1413. int ret = -ENOMEM;
  1414. /* preallocate control response and buffer */
  1415. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  1416. if (!cdev->req)
  1417. return -ENOMEM;
  1418. cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
  1419. if (!cdev->req->buf)
  1420. goto fail;
  1421. ret = device_create_file(&gadget->dev, &dev_attr_suspended);
  1422. if (ret)
  1423. goto fail_dev;
  1424. cdev->req->complete = composite_setup_complete;
  1425. gadget->ep0->driver_data = cdev;
  1426. cdev->driver = composite;
  1427. /*
  1428. * As per USB compliance update, a device that is actively drawing
  1429. * more than 100mA from USB must report itself as bus-powered in
  1430. * the GetStatus(DEVICE) call.
  1431. */
  1432. if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
  1433. usb_gadget_set_selfpowered(gadget);
  1434. /* interface and string IDs start at zero via kzalloc.
  1435. * we force endpoints to start unassigned; few controller
  1436. * drivers will zero ep->driver_data.
  1437. */
  1438. usb_ep_autoconfig_reset(gadget);
  1439. return 0;
  1440. fail_dev:
  1441. kfree(cdev->req->buf);
  1442. fail:
  1443. usb_ep_free_request(gadget->ep0, cdev->req);
  1444. cdev->req = NULL;
  1445. return ret;
  1446. }
  1447. void composite_dev_cleanup(struct usb_composite_dev *cdev)
  1448. {
  1449. struct usb_gadget_string_container *uc, *tmp;
  1450. list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
  1451. list_del(&uc->list);
  1452. kfree(uc);
  1453. }
  1454. if (cdev->req) {
  1455. kfree(cdev->req->buf);
  1456. usb_ep_free_request(cdev->gadget->ep0, cdev->req);
  1457. }
  1458. device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
  1459. }
  1460. static int composite_bind(struct usb_gadget *gadget,
  1461. struct usb_gadget_driver *gdriver)
  1462. {
  1463. struct usb_composite_dev *cdev;
  1464. struct usb_composite_driver *composite = to_cdriver(gdriver);
  1465. int status = -ENOMEM;
  1466. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  1467. if (!cdev)
  1468. return status;
  1469. spin_lock_init(&cdev->lock);
  1470. cdev->gadget = gadget;
  1471. set_gadget_data(gadget, cdev);
  1472. INIT_LIST_HEAD(&cdev->configs);
  1473. INIT_LIST_HEAD(&cdev->gstrings);
  1474. status = composite_dev_prepare(composite, cdev);
  1475. if (status)
  1476. goto fail;
  1477. /* composite gadget needs to assign strings for whole device (like
  1478. * serial number), register function drivers, potentially update
  1479. * power state and consumption, etc
  1480. */
  1481. status = composite->bind(cdev);
  1482. if (status < 0)
  1483. goto fail;
  1484. update_unchanged_dev_desc(&cdev->desc, composite->dev);
  1485. /* has userspace failed to provide a serial number? */
  1486. if (composite->needs_serial && !cdev->desc.iSerialNumber)
  1487. WARNING(cdev, "userspace failed to provide iSerialNumber\n");
  1488. INFO(cdev, "%s ready\n", composite->name);
  1489. return 0;
  1490. fail:
  1491. __composite_unbind(gadget, false);
  1492. return status;
  1493. }
  1494. /*-------------------------------------------------------------------------*/
  1495. static void
  1496. composite_suspend(struct usb_gadget *gadget)
  1497. {
  1498. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1499. struct usb_function *f;
  1500. /* REVISIT: should we have config level
  1501. * suspend/resume callbacks?
  1502. */
  1503. DBG(cdev, "suspend\n");
  1504. if (cdev->config) {
  1505. list_for_each_entry(f, &cdev->config->functions, list) {
  1506. if (f->suspend)
  1507. f->suspend(f);
  1508. }
  1509. }
  1510. if (cdev->driver->suspend)
  1511. cdev->driver->suspend(cdev);
  1512. cdev->suspended = 1;
  1513. usb_gadget_vbus_draw(gadget, 2);
  1514. }
  1515. static void
  1516. composite_resume(struct usb_gadget *gadget)
  1517. {
  1518. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1519. struct usb_function *f;
  1520. u8 maxpower;
  1521. /* REVISIT: should we have config level
  1522. * suspend/resume callbacks?
  1523. */
  1524. DBG(cdev, "resume\n");
  1525. if (cdev->driver->resume)
  1526. cdev->driver->resume(cdev);
  1527. if (cdev->config) {
  1528. list_for_each_entry(f, &cdev->config->functions, list) {
  1529. if (f->resume)
  1530. f->resume(f);
  1531. }
  1532. maxpower = cdev->config->MaxPower;
  1533. usb_gadget_vbus_draw(gadget, maxpower ?
  1534. maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
  1535. }
  1536. cdev->suspended = 0;
  1537. }
  1538. /*-------------------------------------------------------------------------*/
  1539. static const struct usb_gadget_driver composite_driver_template = {
  1540. .bind = composite_bind,
  1541. .unbind = composite_unbind,
  1542. .setup = composite_setup,
  1543. .disconnect = composite_disconnect,
  1544. .suspend = composite_suspend,
  1545. .resume = composite_resume,
  1546. .driver = {
  1547. .owner = THIS_MODULE,
  1548. },
  1549. };
  1550. /**
  1551. * usb_composite_probe() - register a composite driver
  1552. * @driver: the driver to register
  1553. *
  1554. * Context: single threaded during gadget setup
  1555. *
  1556. * This function is used to register drivers using the composite driver
  1557. * framework. The return value is zero, or a negative errno value.
  1558. * Those values normally come from the driver's @bind method, which does
  1559. * all the work of setting up the driver to match the hardware.
  1560. *
  1561. * On successful return, the gadget is ready to respond to requests from
  1562. * the host, unless one of its components invokes usb_gadget_disconnect()
  1563. * while it was binding. That would usually be done in order to wait for
  1564. * some userspace participation.
  1565. */
  1566. int usb_composite_probe(struct usb_composite_driver *driver)
  1567. {
  1568. struct usb_gadget_driver *gadget_driver;
  1569. if (!driver || !driver->dev || !driver->bind)
  1570. return -EINVAL;
  1571. if (!driver->name)
  1572. driver->name = "composite";
  1573. driver->gadget_driver = composite_driver_template;
  1574. gadget_driver = &driver->gadget_driver;
  1575. gadget_driver->function = (char *) driver->name;
  1576. gadget_driver->driver.name = driver->name;
  1577. gadget_driver->max_speed = driver->max_speed;
  1578. return usb_gadget_probe_driver(gadget_driver);
  1579. }
  1580. EXPORT_SYMBOL_GPL(usb_composite_probe);
  1581. /**
  1582. * usb_composite_unregister() - unregister a composite driver
  1583. * @driver: the driver to unregister
  1584. *
  1585. * This function is used to unregister drivers using the composite
  1586. * driver framework.
  1587. */
  1588. void usb_composite_unregister(struct usb_composite_driver *driver)
  1589. {
  1590. usb_gadget_unregister_driver(&driver->gadget_driver);
  1591. }
  1592. EXPORT_SYMBOL_GPL(usb_composite_unregister);
  1593. /**
  1594. * usb_composite_setup_continue() - Continue with the control transfer
  1595. * @cdev: the composite device who's control transfer was kept waiting
  1596. *
  1597. * This function must be called by the USB function driver to continue
  1598. * with the control transfer's data/status stage in case it had requested to
  1599. * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
  1600. * can request the composite framework to delay the setup request's data/status
  1601. * stages by returning USB_GADGET_DELAYED_STATUS.
  1602. */
  1603. void usb_composite_setup_continue(struct usb_composite_dev *cdev)
  1604. {
  1605. int value;
  1606. struct usb_request *req = cdev->req;
  1607. unsigned long flags;
  1608. DBG(cdev, "%s\n", __func__);
  1609. spin_lock_irqsave(&cdev->lock, flags);
  1610. if (cdev->delayed_status == 0) {
  1611. WARN(cdev, "%s: Unexpected call\n", __func__);
  1612. } else if (--cdev->delayed_status == 0) {
  1613. DBG(cdev, "%s: Completing delayed status\n", __func__);
  1614. req->length = 0;
  1615. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  1616. if (value < 0) {
  1617. DBG(cdev, "ep_queue --> %d\n", value);
  1618. req->status = 0;
  1619. composite_setup_complete(cdev->gadget->ep0, req);
  1620. }
  1621. }
  1622. spin_unlock_irqrestore(&cdev->lock, flags);
  1623. }
  1624. EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
  1625. static char *composite_default_mfr(struct usb_gadget *gadget)
  1626. {
  1627. char *mfr;
  1628. int len;
  1629. len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
  1630. init_utsname()->release, gadget->name);
  1631. len++;
  1632. mfr = kmalloc(len, GFP_KERNEL);
  1633. if (!mfr)
  1634. return NULL;
  1635. snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
  1636. init_utsname()->release, gadget->name);
  1637. return mfr;
  1638. }
  1639. void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
  1640. struct usb_composite_overwrite *covr)
  1641. {
  1642. struct usb_device_descriptor *desc = &cdev->desc;
  1643. struct usb_gadget_strings *gstr = cdev->driver->strings[0];
  1644. struct usb_string *dev_str = gstr->strings;
  1645. if (covr->idVendor)
  1646. desc->idVendor = cpu_to_le16(covr->idVendor);
  1647. if (covr->idProduct)
  1648. desc->idProduct = cpu_to_le16(covr->idProduct);
  1649. if (covr->bcdDevice)
  1650. desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
  1651. if (covr->serial_number) {
  1652. desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
  1653. dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
  1654. }
  1655. if (covr->manufacturer) {
  1656. desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
  1657. dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
  1658. } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
  1659. desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
  1660. cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
  1661. dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
  1662. }
  1663. if (covr->product) {
  1664. desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
  1665. dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
  1666. }
  1667. }
  1668. EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
  1669. MODULE_LICENSE("GPL");
  1670. MODULE_AUTHOR("David Brownell");