composite.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * composite.c - infrastructure for Composite USB Gadgets
  4. *
  5. * Copyright (C) 2006-2008 David Brownell
  6. */
  7. /* #define VERBOSE_DEBUG */
  8. #include <linux/kallsyms.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/device.h>
  13. #include <linux/utsname.h>
  14. #include <linux/usb/composite.h>
  15. #include <linux/usb/otg.h>
  16. #include <asm/unaligned.h>
  17. #include "u_os_desc.h"
  18. /**
  19. * struct usb_os_string - represents OS String to be reported by a gadget
  20. * @bLength: total length of the entire descritor, always 0x12
  21. * @bDescriptorType: USB_DT_STRING
  22. * @qwSignature: the OS String proper
  23. * @bMS_VendorCode: code used by the host for subsequent requests
  24. * @bPad: not used, must be zero
  25. */
  26. struct usb_os_string {
  27. __u8 bLength;
  28. __u8 bDescriptorType;
  29. __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
  30. __u8 bMS_VendorCode;
  31. __u8 bPad;
  32. } __packed;
  33. /*
  34. * The code in this file is utility code, used to build a gadget driver
  35. * from one or more "function" drivers, one or more "configuration"
  36. * objects, and a "usb_composite_driver" by gluing them together along
  37. * with the relevant device-wide data.
  38. */
  39. static struct usb_gadget_strings **get_containers_gs(
  40. struct usb_gadget_string_container *uc)
  41. {
  42. return (struct usb_gadget_strings **)uc->stash;
  43. }
  44. /**
  45. * function_descriptors() - get function descriptors for speed
  46. * @f: the function
  47. * @speed: the speed
  48. *
  49. * Returns the descriptors or NULL if not set.
  50. */
  51. static struct usb_descriptor_header **
  52. function_descriptors(struct usb_function *f,
  53. enum usb_device_speed speed)
  54. {
  55. struct usb_descriptor_header **descriptors;
  56. /*
  57. * NOTE: we try to help gadget drivers which might not be setting
  58. * max_speed appropriately.
  59. */
  60. switch (speed) {
  61. case USB_SPEED_SUPER_PLUS:
  62. descriptors = f->ssp_descriptors;
  63. if (descriptors)
  64. break;
  65. /* FALLTHROUGH */
  66. case USB_SPEED_SUPER:
  67. descriptors = f->ss_descriptors;
  68. if (descriptors)
  69. break;
  70. /* FALLTHROUGH */
  71. case USB_SPEED_HIGH:
  72. descriptors = f->hs_descriptors;
  73. if (descriptors)
  74. break;
  75. /* FALLTHROUGH */
  76. default:
  77. descriptors = f->fs_descriptors;
  78. }
  79. /*
  80. * if we can't find any descriptors at all, then this gadget deserves to
  81. * Oops with a NULL pointer dereference
  82. */
  83. return descriptors;
  84. }
  85. /**
  86. * next_desc() - advance to the next desc_type descriptor
  87. * @t: currect pointer within descriptor array
  88. * @desc_type: descriptor type
  89. *
  90. * Return: next desc_type descriptor or NULL
  91. *
  92. * Iterate over @t until either desc_type descriptor found or
  93. * NULL (that indicates end of list) encountered
  94. */
  95. static struct usb_descriptor_header**
  96. next_desc(struct usb_descriptor_header **t, u8 desc_type)
  97. {
  98. for (; *t; t++) {
  99. if ((*t)->bDescriptorType == desc_type)
  100. return t;
  101. }
  102. return NULL;
  103. }
  104. /*
  105. * for_each_desc() - iterate over desc_type descriptors in the
  106. * descriptors list
  107. * @start: pointer within descriptor array.
  108. * @iter_desc: desc_type descriptor to use as the loop cursor
  109. * @desc_type: wanted descriptr type
  110. */
  111. #define for_each_desc(start, iter_desc, desc_type) \
  112. for (iter_desc = next_desc(start, desc_type); \
  113. iter_desc; iter_desc = next_desc(iter_desc + 1, desc_type))
  114. /**
  115. * config_ep_by_speed_and_alt() - configures the given endpoint
  116. * according to gadget speed.
  117. * @g: pointer to the gadget
  118. * @f: usb function
  119. * @_ep: the endpoint to configure
  120. * @alt: alternate setting number
  121. *
  122. * Return: error code, 0 on success
  123. *
  124. * This function chooses the right descriptors for a given
  125. * endpoint according to gadget speed and saves it in the
  126. * endpoint desc field. If the endpoint already has a descriptor
  127. * assigned to it - overwrites it with currently corresponding
  128. * descriptor. The endpoint maxpacket field is updated according
  129. * to the chosen descriptor.
  130. * Note: the supplied function should hold all the descriptors
  131. * for supported speeds
  132. */
  133. int config_ep_by_speed_and_alt(struct usb_gadget *g,
  134. struct usb_function *f,
  135. struct usb_ep *_ep,
  136. u8 alt)
  137. {
  138. struct usb_endpoint_descriptor *chosen_desc = NULL;
  139. struct usb_interface_descriptor *int_desc = NULL;
  140. struct usb_descriptor_header **speed_desc = NULL;
  141. struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
  142. int want_comp_desc = 0;
  143. struct usb_descriptor_header **d_spd; /* cursor for speed desc */
  144. if (!g || !f || !_ep)
  145. return -EIO;
  146. /* select desired speed */
  147. switch (g->speed) {
  148. case USB_SPEED_SUPER_PLUS:
  149. if (gadget_is_superspeed_plus(g)) {
  150. speed_desc = f->ssp_descriptors;
  151. want_comp_desc = 1;
  152. break;
  153. }
  154. /* fall through */
  155. case USB_SPEED_SUPER:
  156. if (gadget_is_superspeed(g)) {
  157. speed_desc = f->ss_descriptors;
  158. want_comp_desc = 1;
  159. break;
  160. }
  161. /* fall through */
  162. case USB_SPEED_HIGH:
  163. if (gadget_is_dualspeed(g)) {
  164. speed_desc = f->hs_descriptors;
  165. break;
  166. }
  167. /* fall through */
  168. default:
  169. speed_desc = f->fs_descriptors;
  170. }
  171. /* find correct alternate setting descriptor */
  172. for_each_desc(speed_desc, d_spd, USB_DT_INTERFACE) {
  173. int_desc = (struct usb_interface_descriptor *)*d_spd;
  174. if (int_desc->bAlternateSetting == alt) {
  175. speed_desc = d_spd;
  176. goto intf_found;
  177. }
  178. }
  179. return -EIO;
  180. intf_found:
  181. /* find descriptors */
  182. for_each_desc(speed_desc, d_spd, USB_DT_ENDPOINT) {
  183. chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
  184. if (chosen_desc->bEndpointAddress == _ep->address)
  185. goto ep_found;
  186. }
  187. return -EIO;
  188. ep_found:
  189. /* commit results */
  190. _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
  191. _ep->desc = chosen_desc;
  192. _ep->comp_desc = NULL;
  193. _ep->maxburst = 0;
  194. _ep->mult = 1;
  195. if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) ||
  196. usb_endpoint_xfer_int(_ep->desc)))
  197. _ep->mult = usb_endpoint_maxp_mult(_ep->desc);
  198. if (!want_comp_desc)
  199. return 0;
  200. /*
  201. * Companion descriptor should follow EP descriptor
  202. * USB 3.0 spec, #9.6.7
  203. */
  204. comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
  205. if (!comp_desc ||
  206. (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
  207. return -EIO;
  208. _ep->comp_desc = comp_desc;
  209. if (g->speed >= USB_SPEED_SUPER) {
  210. switch (usb_endpoint_type(_ep->desc)) {
  211. case USB_ENDPOINT_XFER_ISOC:
  212. /* mult: bits 1:0 of bmAttributes */
  213. _ep->mult = (comp_desc->bmAttributes & 0x3) + 1;
  214. /* fall through */
  215. case USB_ENDPOINT_XFER_BULK:
  216. case USB_ENDPOINT_XFER_INT:
  217. _ep->maxburst = comp_desc->bMaxBurst + 1;
  218. break;
  219. default:
  220. if (comp_desc->bMaxBurst != 0) {
  221. struct usb_composite_dev *cdev;
  222. cdev = get_gadget_data(g);
  223. ERROR(cdev, "ep0 bMaxBurst must be 0\n");
  224. }
  225. _ep->maxburst = 1;
  226. break;
  227. }
  228. }
  229. return 0;
  230. }
  231. EXPORT_SYMBOL_GPL(config_ep_by_speed_and_alt);
  232. /**
  233. * config_ep_by_speed() - configures the given endpoint
  234. * according to gadget speed.
  235. * @g: pointer to the gadget
  236. * @f: usb function
  237. * @_ep: the endpoint to configure
  238. *
  239. * Return: error code, 0 on success
  240. *
  241. * This function chooses the right descriptors for a given
  242. * endpoint according to gadget speed and saves it in the
  243. * endpoint desc field. If the endpoint already has a descriptor
  244. * assigned to it - overwrites it with currently corresponding
  245. * descriptor. The endpoint maxpacket field is updated according
  246. * to the chosen descriptor.
  247. * Note: the supplied function should hold all the descriptors
  248. * for supported speeds
  249. */
  250. int config_ep_by_speed(struct usb_gadget *g,
  251. struct usb_function *f,
  252. struct usb_ep *_ep)
  253. {
  254. return config_ep_by_speed_and_alt(g, f, _ep, 0);
  255. }
  256. EXPORT_SYMBOL_GPL(config_ep_by_speed);
  257. /**
  258. * usb_add_function() - add a function to a configuration
  259. * @config: the configuration
  260. * @function: the function being added
  261. * Context: single threaded during gadget setup
  262. *
  263. * After initialization, each configuration must have one or more
  264. * functions added to it. Adding a function involves calling its @bind()
  265. * method to allocate resources such as interface and string identifiers
  266. * and endpoints.
  267. *
  268. * This function returns the value of the function's bind(), which is
  269. * zero for success else a negative errno value.
  270. */
  271. int usb_add_function(struct usb_configuration *config,
  272. struct usb_function *function)
  273. {
  274. int value = -EINVAL;
  275. DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
  276. function->name, function,
  277. config->label, config);
  278. if (!function->set_alt || !function->disable)
  279. goto done;
  280. function->config = config;
  281. list_add_tail(&function->list, &config->functions);
  282. if (function->bind_deactivated) {
  283. value = usb_function_deactivate(function);
  284. if (value)
  285. goto done;
  286. }
  287. /* REVISIT *require* function->bind? */
  288. if (function->bind) {
  289. value = function->bind(config, function);
  290. if (value < 0) {
  291. list_del(&function->list);
  292. function->config = NULL;
  293. }
  294. } else
  295. value = 0;
  296. /* We allow configurations that don't work at both speeds.
  297. * If we run into a lowspeed Linux system, treat it the same
  298. * as full speed ... it's the function drivers that will need
  299. * to avoid bulk and ISO transfers.
  300. */
  301. if (!config->fullspeed && function->fs_descriptors)
  302. config->fullspeed = true;
  303. if (!config->highspeed && function->hs_descriptors)
  304. config->highspeed = true;
  305. if (!config->superspeed && function->ss_descriptors)
  306. config->superspeed = true;
  307. if (!config->superspeed_plus && function->ssp_descriptors)
  308. config->superspeed_plus = true;
  309. done:
  310. if (value)
  311. DBG(config->cdev, "adding '%s'/%p --> %d\n",
  312. function->name, function, value);
  313. return value;
  314. }
  315. EXPORT_SYMBOL_GPL(usb_add_function);
  316. void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
  317. {
  318. if (f->disable)
  319. f->disable(f);
  320. bitmap_zero(f->endpoints, 32);
  321. list_del(&f->list);
  322. if (f->unbind)
  323. f->unbind(c, f);
  324. if (f->bind_deactivated)
  325. usb_function_activate(f);
  326. }
  327. EXPORT_SYMBOL_GPL(usb_remove_function);
  328. /**
  329. * usb_function_deactivate - prevent function and gadget enumeration
  330. * @function: the function that isn't yet ready to respond
  331. *
  332. * Blocks response of the gadget driver to host enumeration by
  333. * preventing the data line pullup from being activated. This is
  334. * normally called during @bind() processing to change from the
  335. * initial "ready to respond" state, or when a required resource
  336. * becomes available.
  337. *
  338. * For example, drivers that serve as a passthrough to a userspace
  339. * daemon can block enumeration unless that daemon (such as an OBEX,
  340. * MTP, or print server) is ready to handle host requests.
  341. *
  342. * Not all systems support software control of their USB peripheral
  343. * data pullups.
  344. *
  345. * Returns zero on success, else negative errno.
  346. */
  347. int usb_function_deactivate(struct usb_function *function)
  348. {
  349. struct usb_composite_dev *cdev = function->config->cdev;
  350. unsigned long flags;
  351. int status = 0;
  352. spin_lock_irqsave(&cdev->lock, flags);
  353. if (cdev->deactivations == 0) {
  354. spin_unlock_irqrestore(&cdev->lock, flags);
  355. status = usb_gadget_deactivate(cdev->gadget);
  356. spin_lock_irqsave(&cdev->lock, flags);
  357. }
  358. if (status == 0)
  359. cdev->deactivations++;
  360. spin_unlock_irqrestore(&cdev->lock, flags);
  361. return status;
  362. }
  363. EXPORT_SYMBOL_GPL(usb_function_deactivate);
  364. /**
  365. * usb_function_activate - allow function and gadget enumeration
  366. * @function: function on which usb_function_activate() was called
  367. *
  368. * Reverses effect of usb_function_deactivate(). If no more functions
  369. * are delaying their activation, the gadget driver will respond to
  370. * host enumeration procedures.
  371. *
  372. * Returns zero on success, else negative errno.
  373. */
  374. int usb_function_activate(struct usb_function *function)
  375. {
  376. struct usb_composite_dev *cdev = function->config->cdev;
  377. unsigned long flags;
  378. int status = 0;
  379. spin_lock_irqsave(&cdev->lock, flags);
  380. if (WARN_ON(cdev->deactivations == 0))
  381. status = -EINVAL;
  382. else {
  383. cdev->deactivations--;
  384. if (cdev->deactivations == 0) {
  385. spin_unlock_irqrestore(&cdev->lock, flags);
  386. status = usb_gadget_activate(cdev->gadget);
  387. spin_lock_irqsave(&cdev->lock, flags);
  388. }
  389. }
  390. spin_unlock_irqrestore(&cdev->lock, flags);
  391. return status;
  392. }
  393. EXPORT_SYMBOL_GPL(usb_function_activate);
  394. /**
  395. * usb_interface_id() - allocate an unused interface ID
  396. * @config: configuration associated with the interface
  397. * @function: function handling the interface
  398. * Context: single threaded during gadget setup
  399. *
  400. * usb_interface_id() is called from usb_function.bind() callbacks to
  401. * allocate new interface IDs. The function driver will then store that
  402. * ID in interface, association, CDC union, and other descriptors. It
  403. * will also handle any control requests targeted at that interface,
  404. * particularly changing its altsetting via set_alt(). There may
  405. * also be class-specific or vendor-specific requests to handle.
  406. *
  407. * All interface identifier should be allocated using this routine, to
  408. * ensure that for example different functions don't wrongly assign
  409. * different meanings to the same identifier. Note that since interface
  410. * identifiers are configuration-specific, functions used in more than
  411. * one configuration (or more than once in a given configuration) need
  412. * multiple versions of the relevant descriptors.
  413. *
  414. * Returns the interface ID which was allocated; or -ENODEV if no
  415. * more interface IDs can be allocated.
  416. */
  417. int usb_interface_id(struct usb_configuration *config,
  418. struct usb_function *function)
  419. {
  420. unsigned id = config->next_interface_id;
  421. if (id < MAX_CONFIG_INTERFACES) {
  422. config->interface[id] = function;
  423. config->next_interface_id = id + 1;
  424. return id;
  425. }
  426. return -ENODEV;
  427. }
  428. EXPORT_SYMBOL_GPL(usb_interface_id);
  429. static u8 encode_bMaxPower(enum usb_device_speed speed,
  430. struct usb_configuration *c)
  431. {
  432. unsigned val;
  433. if (c->MaxPower)
  434. val = c->MaxPower;
  435. else
  436. val = CONFIG_USB_GADGET_VBUS_DRAW;
  437. if (!val)
  438. return 0;
  439. if (speed < USB_SPEED_SUPER)
  440. return min(val, 500U) / 2;
  441. else
  442. /*
  443. * USB 3.x supports up to 900mA, but since 900 isn't divisible
  444. * by 8 the integral division will effectively cap to 896mA.
  445. */
  446. return min(val, 900U) / 8;
  447. }
  448. static int config_buf(struct usb_configuration *config,
  449. enum usb_device_speed speed, void *buf, u8 type)
  450. {
  451. struct usb_config_descriptor *c = buf;
  452. void *next = buf + USB_DT_CONFIG_SIZE;
  453. int len;
  454. struct usb_function *f;
  455. int status;
  456. len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
  457. /* write the config descriptor */
  458. c = buf;
  459. c->bLength = USB_DT_CONFIG_SIZE;
  460. c->bDescriptorType = type;
  461. /* wTotalLength is written later */
  462. c->bNumInterfaces = config->next_interface_id;
  463. c->bConfigurationValue = config->bConfigurationValue;
  464. c->iConfiguration = config->iConfiguration;
  465. c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
  466. c->bMaxPower = encode_bMaxPower(speed, config);
  467. /* There may be e.g. OTG descriptors */
  468. if (config->descriptors) {
  469. status = usb_descriptor_fillbuf(next, len,
  470. config->descriptors);
  471. if (status < 0)
  472. return status;
  473. len -= status;
  474. next += status;
  475. }
  476. /* add each function's descriptors */
  477. list_for_each_entry(f, &config->functions, list) {
  478. struct usb_descriptor_header **descriptors;
  479. descriptors = function_descriptors(f, speed);
  480. if (!descriptors)
  481. continue;
  482. status = usb_descriptor_fillbuf(next, len,
  483. (const struct usb_descriptor_header **) descriptors);
  484. if (status < 0)
  485. return status;
  486. len -= status;
  487. next += status;
  488. }
  489. len = next - buf;
  490. c->wTotalLength = cpu_to_le16(len);
  491. return len;
  492. }
  493. static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
  494. {
  495. struct usb_gadget *gadget = cdev->gadget;
  496. struct usb_configuration *c;
  497. struct list_head *pos;
  498. u8 type = w_value >> 8;
  499. enum usb_device_speed speed = USB_SPEED_UNKNOWN;
  500. if (gadget->speed >= USB_SPEED_SUPER)
  501. speed = gadget->speed;
  502. else if (gadget_is_dualspeed(gadget)) {
  503. int hs = 0;
  504. if (gadget->speed == USB_SPEED_HIGH)
  505. hs = 1;
  506. if (type == USB_DT_OTHER_SPEED_CONFIG)
  507. hs = !hs;
  508. if (hs)
  509. speed = USB_SPEED_HIGH;
  510. }
  511. /* This is a lookup by config *INDEX* */
  512. w_value &= 0xff;
  513. pos = &cdev->configs;
  514. c = cdev->os_desc_config;
  515. if (c)
  516. goto check_config;
  517. while ((pos = pos->next) != &cdev->configs) {
  518. c = list_entry(pos, typeof(*c), list);
  519. /* skip OS Descriptors config which is handled separately */
  520. if (c == cdev->os_desc_config)
  521. continue;
  522. check_config:
  523. /* ignore configs that won't work at this speed */
  524. switch (speed) {
  525. case USB_SPEED_SUPER_PLUS:
  526. if (!c->superspeed_plus)
  527. continue;
  528. break;
  529. case USB_SPEED_SUPER:
  530. if (!c->superspeed)
  531. continue;
  532. break;
  533. case USB_SPEED_HIGH:
  534. if (!c->highspeed)
  535. continue;
  536. break;
  537. default:
  538. if (!c->fullspeed)
  539. continue;
  540. }
  541. if (w_value == 0)
  542. return config_buf(c, speed, cdev->req->buf, type);
  543. w_value--;
  544. }
  545. return -EINVAL;
  546. }
  547. static int count_configs(struct usb_composite_dev *cdev, unsigned type)
  548. {
  549. struct usb_gadget *gadget = cdev->gadget;
  550. struct usb_configuration *c;
  551. unsigned count = 0;
  552. int hs = 0;
  553. int ss = 0;
  554. int ssp = 0;
  555. if (gadget_is_dualspeed(gadget)) {
  556. if (gadget->speed == USB_SPEED_HIGH)
  557. hs = 1;
  558. if (gadget->speed == USB_SPEED_SUPER)
  559. ss = 1;
  560. if (gadget->speed == USB_SPEED_SUPER_PLUS)
  561. ssp = 1;
  562. if (type == USB_DT_DEVICE_QUALIFIER)
  563. hs = !hs;
  564. }
  565. list_for_each_entry(c, &cdev->configs, list) {
  566. /* ignore configs that won't work at this speed */
  567. if (ssp) {
  568. if (!c->superspeed_plus)
  569. continue;
  570. } else if (ss) {
  571. if (!c->superspeed)
  572. continue;
  573. } else if (hs) {
  574. if (!c->highspeed)
  575. continue;
  576. } else {
  577. if (!c->fullspeed)
  578. continue;
  579. }
  580. count++;
  581. }
  582. return count;
  583. }
  584. /**
  585. * bos_desc() - prepares the BOS descriptor.
  586. * @cdev: pointer to usb_composite device to generate the bos
  587. * descriptor for
  588. *
  589. * This function generates the BOS (Binary Device Object)
  590. * descriptor and its device capabilities descriptors. The BOS
  591. * descriptor should be supported by a SuperSpeed device.
  592. */
  593. static int bos_desc(struct usb_composite_dev *cdev)
  594. {
  595. struct usb_ext_cap_descriptor *usb_ext;
  596. struct usb_dcd_config_params dcd_config_params;
  597. struct usb_bos_descriptor *bos = cdev->req->buf;
  598. bos->bLength = USB_DT_BOS_SIZE;
  599. bos->bDescriptorType = USB_DT_BOS;
  600. bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
  601. bos->bNumDeviceCaps = 0;
  602. /*
  603. * A SuperSpeed device shall include the USB2.0 extension descriptor
  604. * and shall support LPM when operating in USB2.0 HS mode.
  605. */
  606. usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  607. bos->bNumDeviceCaps++;
  608. le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
  609. usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
  610. usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  611. usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
  612. usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
  613. /*
  614. * The Superspeed USB Capability descriptor shall be implemented by all
  615. * SuperSpeed devices.
  616. */
  617. if (gadget_is_superspeed(cdev->gadget)) {
  618. struct usb_ss_cap_descriptor *ss_cap;
  619. ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  620. bos->bNumDeviceCaps++;
  621. le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
  622. ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
  623. ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  624. ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
  625. ss_cap->bmAttributes = 0; /* LTM is not supported yet */
  626. ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
  627. USB_FULL_SPEED_OPERATION |
  628. USB_HIGH_SPEED_OPERATION |
  629. USB_5GBPS_OPERATION);
  630. ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
  631. /* Get Controller configuration */
  632. if (cdev->gadget->ops->get_config_params) {
  633. cdev->gadget->ops->get_config_params(
  634. &dcd_config_params);
  635. } else {
  636. dcd_config_params.bU1devExitLat =
  637. USB_DEFAULT_U1_DEV_EXIT_LAT;
  638. dcd_config_params.bU2DevExitLat =
  639. cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
  640. }
  641. ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
  642. ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
  643. }
  644. /* The SuperSpeedPlus USB Device Capability descriptor */
  645. if (gadget_is_superspeed_plus(cdev->gadget)) {
  646. struct usb_ssp_cap_descriptor *ssp_cap;
  647. ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  648. bos->bNumDeviceCaps++;
  649. /*
  650. * Report typical values.
  651. */
  652. le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
  653. ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
  654. ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  655. ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
  656. ssp_cap->bReserved = 0;
  657. ssp_cap->wReserved = 0;
  658. /* SSAC = 1 (2 attributes) */
  659. ssp_cap->bmAttributes = cpu_to_le32(1);
  660. /* Min RX/TX Lane Count = 1 */
  661. ssp_cap->wFunctionalitySupport =
  662. cpu_to_le16((1 << 8) | (1 << 12));
  663. /*
  664. * bmSublinkSpeedAttr[0]:
  665. * ST = Symmetric, RX
  666. * LSE = 3 (Gbps)
  667. * LP = 1 (SuperSpeedPlus)
  668. * LSM = 10 (10 Gbps)
  669. */
  670. ssp_cap->bmSublinkSpeedAttr[0] =
  671. cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16));
  672. /*
  673. * bmSublinkSpeedAttr[1] =
  674. * ST = Symmetric, TX
  675. * LSE = 3 (Gbps)
  676. * LP = 1 (SuperSpeedPlus)
  677. * LSM = 10 (10 Gbps)
  678. */
  679. ssp_cap->bmSublinkSpeedAttr[1] =
  680. cpu_to_le32((3 << 4) | (1 << 14) |
  681. (0xa << 16) | (1 << 7));
  682. }
  683. return le16_to_cpu(bos->wTotalLength);
  684. }
  685. static void device_qual(struct usb_composite_dev *cdev)
  686. {
  687. struct usb_qualifier_descriptor *qual = cdev->req->buf;
  688. qual->bLength = sizeof(*qual);
  689. qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  690. /* POLICY: same bcdUSB and device type info at both speeds */
  691. qual->bcdUSB = cdev->desc.bcdUSB;
  692. qual->bDeviceClass = cdev->desc.bDeviceClass;
  693. qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
  694. qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
  695. /* ASSUME same EP0 fifo size at both speeds */
  696. qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
  697. qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
  698. qual->bRESERVED = 0;
  699. }
  700. /*-------------------------------------------------------------------------*/
  701. static void reset_config(struct usb_composite_dev *cdev)
  702. {
  703. struct usb_function *f;
  704. DBG(cdev, "reset config\n");
  705. list_for_each_entry(f, &cdev->config->functions, list) {
  706. if (f->disable)
  707. f->disable(f);
  708. bitmap_zero(f->endpoints, 32);
  709. }
  710. cdev->config = NULL;
  711. cdev->delayed_status = 0;
  712. }
  713. static int set_config(struct usb_composite_dev *cdev,
  714. const struct usb_ctrlrequest *ctrl, unsigned number)
  715. {
  716. struct usb_gadget *gadget = cdev->gadget;
  717. struct usb_configuration *c = NULL;
  718. int result = -EINVAL;
  719. unsigned power = gadget_is_otg(gadget) ? 8 : 100;
  720. int tmp;
  721. if (number) {
  722. list_for_each_entry(c, &cdev->configs, list) {
  723. if (c->bConfigurationValue == number) {
  724. /*
  725. * We disable the FDs of the previous
  726. * configuration only if the new configuration
  727. * is a valid one
  728. */
  729. if (cdev->config)
  730. reset_config(cdev);
  731. result = 0;
  732. break;
  733. }
  734. }
  735. if (result < 0)
  736. goto done;
  737. } else { /* Zero configuration value - need to reset the config */
  738. if (cdev->config)
  739. reset_config(cdev);
  740. result = 0;
  741. }
  742. INFO(cdev, "%s config #%d: %s\n",
  743. usb_speed_string(gadget->speed),
  744. number, c ? c->label : "unconfigured");
  745. if (!c)
  746. goto done;
  747. usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
  748. cdev->config = c;
  749. /* Initialize all interfaces by setting them to altsetting zero. */
  750. for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
  751. struct usb_function *f = c->interface[tmp];
  752. struct usb_descriptor_header **descriptors;
  753. if (!f)
  754. break;
  755. /*
  756. * Record which endpoints are used by the function. This is used
  757. * to dispatch control requests targeted at that endpoint to the
  758. * function's setup callback instead of the current
  759. * configuration's setup callback.
  760. */
  761. descriptors = function_descriptors(f, gadget->speed);
  762. for (; *descriptors; ++descriptors) {
  763. struct usb_endpoint_descriptor *ep;
  764. int addr;
  765. if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
  766. continue;
  767. ep = (struct usb_endpoint_descriptor *)*descriptors;
  768. addr = ((ep->bEndpointAddress & 0x80) >> 3)
  769. | (ep->bEndpointAddress & 0x0f);
  770. set_bit(addr, f->endpoints);
  771. }
  772. result = f->set_alt(f, tmp, 0);
  773. if (result < 0) {
  774. DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
  775. tmp, f->name, f, result);
  776. reset_config(cdev);
  777. goto done;
  778. }
  779. if (result == USB_GADGET_DELAYED_STATUS) {
  780. DBG(cdev,
  781. "%s: interface %d (%s) requested delayed status\n",
  782. __func__, tmp, f->name);
  783. cdev->delayed_status++;
  784. DBG(cdev, "delayed_status count %d\n",
  785. cdev->delayed_status);
  786. }
  787. }
  788. /* when we return, be sure our power usage is valid */
  789. power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
  790. if (gadget->speed < USB_SPEED_SUPER)
  791. power = min(power, 500U);
  792. else
  793. power = min(power, 900U);
  794. done:
  795. if (power <= USB_SELF_POWER_VBUS_MAX_DRAW)
  796. usb_gadget_set_selfpowered(gadget);
  797. else
  798. usb_gadget_clear_selfpowered(gadget);
  799. usb_gadget_vbus_draw(gadget, power);
  800. if (result >= 0 && cdev->delayed_status)
  801. result = USB_GADGET_DELAYED_STATUS;
  802. return result;
  803. }
  804. int usb_add_config_only(struct usb_composite_dev *cdev,
  805. struct usb_configuration *config)
  806. {
  807. struct usb_configuration *c;
  808. if (!config->bConfigurationValue)
  809. return -EINVAL;
  810. /* Prevent duplicate configuration identifiers */
  811. list_for_each_entry(c, &cdev->configs, list) {
  812. if (c->bConfigurationValue == config->bConfigurationValue)
  813. return -EBUSY;
  814. }
  815. config->cdev = cdev;
  816. list_add_tail(&config->list, &cdev->configs);
  817. INIT_LIST_HEAD(&config->functions);
  818. config->next_interface_id = 0;
  819. memset(config->interface, 0, sizeof(config->interface));
  820. return 0;
  821. }
  822. EXPORT_SYMBOL_GPL(usb_add_config_only);
  823. /**
  824. * usb_add_config() - add a configuration to a device.
  825. * @cdev: wraps the USB gadget
  826. * @config: the configuration, with bConfigurationValue assigned
  827. * @bind: the configuration's bind function
  828. * Context: single threaded during gadget setup
  829. *
  830. * One of the main tasks of a composite @bind() routine is to
  831. * add each of the configurations it supports, using this routine.
  832. *
  833. * This function returns the value of the configuration's @bind(), which
  834. * is zero for success else a negative errno value. Binding configurations
  835. * assigns global resources including string IDs, and per-configuration
  836. * resources such as interface IDs and endpoints.
  837. */
  838. int usb_add_config(struct usb_composite_dev *cdev,
  839. struct usb_configuration *config,
  840. int (*bind)(struct usb_configuration *))
  841. {
  842. int status = -EINVAL;
  843. if (!bind)
  844. goto done;
  845. DBG(cdev, "adding config #%u '%s'/%p\n",
  846. config->bConfigurationValue,
  847. config->label, config);
  848. status = usb_add_config_only(cdev, config);
  849. if (status)
  850. goto done;
  851. status = bind(config);
  852. if (status < 0) {
  853. while (!list_empty(&config->functions)) {
  854. struct usb_function *f;
  855. f = list_first_entry(&config->functions,
  856. struct usb_function, list);
  857. list_del(&f->list);
  858. if (f->unbind) {
  859. DBG(cdev, "unbind function '%s'/%p\n",
  860. f->name, f);
  861. f->unbind(config, f);
  862. /* may free memory for "f" */
  863. }
  864. }
  865. list_del(&config->list);
  866. config->cdev = NULL;
  867. } else {
  868. unsigned i;
  869. DBG(cdev, "cfg %d/%p speeds:%s%s%s%s\n",
  870. config->bConfigurationValue, config,
  871. config->superspeed_plus ? " superplus" : "",
  872. config->superspeed ? " super" : "",
  873. config->highspeed ? " high" : "",
  874. config->fullspeed
  875. ? (gadget_is_dualspeed(cdev->gadget)
  876. ? " full"
  877. : " full/low")
  878. : "");
  879. for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
  880. struct usb_function *f = config->interface[i];
  881. if (!f)
  882. continue;
  883. DBG(cdev, " interface %d = %s/%p\n",
  884. i, f->name, f);
  885. }
  886. }
  887. /* set_alt(), or next bind(), sets up ep->claimed as needed */
  888. usb_ep_autoconfig_reset(cdev->gadget);
  889. done:
  890. if (status)
  891. DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
  892. config->bConfigurationValue, status);
  893. return status;
  894. }
  895. EXPORT_SYMBOL_GPL(usb_add_config);
  896. static void remove_config(struct usb_composite_dev *cdev,
  897. struct usb_configuration *config)
  898. {
  899. while (!list_empty(&config->functions)) {
  900. struct usb_function *f;
  901. f = list_first_entry(&config->functions,
  902. struct usb_function, list);
  903. usb_remove_function(config, f);
  904. }
  905. list_del(&config->list);
  906. if (config->unbind) {
  907. DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
  908. config->unbind(config);
  909. /* may free memory for "c" */
  910. }
  911. }
  912. /**
  913. * usb_remove_config() - remove a configuration from a device.
  914. * @cdev: wraps the USB gadget
  915. * @config: the configuration
  916. *
  917. * Drivers must call usb_gadget_disconnect before calling this function
  918. * to disconnect the device from the host and make sure the host will not
  919. * try to enumerate the device while we are changing the config list.
  920. */
  921. void usb_remove_config(struct usb_composite_dev *cdev,
  922. struct usb_configuration *config)
  923. {
  924. unsigned long flags;
  925. spin_lock_irqsave(&cdev->lock, flags);
  926. if (cdev->config == config)
  927. reset_config(cdev);
  928. spin_unlock_irqrestore(&cdev->lock, flags);
  929. remove_config(cdev, config);
  930. }
  931. /*-------------------------------------------------------------------------*/
  932. /* We support strings in multiple languages ... string descriptor zero
  933. * says which languages are supported. The typical case will be that
  934. * only one language (probably English) is used, with i18n handled on
  935. * the host side.
  936. */
  937. static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
  938. {
  939. const struct usb_gadget_strings *s;
  940. __le16 language;
  941. __le16 *tmp;
  942. while (*sp) {
  943. s = *sp;
  944. language = cpu_to_le16(s->language);
  945. for (tmp = buf; *tmp && tmp < &buf[USB_MAX_STRING_LEN]; tmp++) {
  946. if (*tmp == language)
  947. goto repeat;
  948. }
  949. *tmp++ = language;
  950. repeat:
  951. sp++;
  952. }
  953. }
  954. static int lookup_string(
  955. struct usb_gadget_strings **sp,
  956. void *buf,
  957. u16 language,
  958. int id
  959. )
  960. {
  961. struct usb_gadget_strings *s;
  962. int value;
  963. while (*sp) {
  964. s = *sp++;
  965. if (s->language != language)
  966. continue;
  967. value = usb_gadget_get_string(s, id, buf);
  968. if (value > 0)
  969. return value;
  970. }
  971. return -EINVAL;
  972. }
  973. static int get_string(struct usb_composite_dev *cdev,
  974. void *buf, u16 language, int id)
  975. {
  976. struct usb_composite_driver *composite = cdev->driver;
  977. struct usb_gadget_string_container *uc;
  978. struct usb_configuration *c;
  979. struct usb_function *f;
  980. int len;
  981. /* Yes, not only is USB's i18n support probably more than most
  982. * folk will ever care about ... also, it's all supported here.
  983. * (Except for UTF8 support for Unicode's "Astral Planes".)
  984. */
  985. /* 0 == report all available language codes */
  986. if (id == 0) {
  987. struct usb_string_descriptor *s = buf;
  988. struct usb_gadget_strings **sp;
  989. memset(s, 0, 256);
  990. s->bDescriptorType = USB_DT_STRING;
  991. sp = composite->strings;
  992. if (sp)
  993. collect_langs(sp, s->wData);
  994. list_for_each_entry(c, &cdev->configs, list) {
  995. sp = c->strings;
  996. if (sp)
  997. collect_langs(sp, s->wData);
  998. list_for_each_entry(f, &c->functions, list) {
  999. sp = f->strings;
  1000. if (sp)
  1001. collect_langs(sp, s->wData);
  1002. }
  1003. }
  1004. list_for_each_entry(uc, &cdev->gstrings, list) {
  1005. struct usb_gadget_strings **sp;
  1006. sp = get_containers_gs(uc);
  1007. collect_langs(sp, s->wData);
  1008. }
  1009. for (len = 0; len <= USB_MAX_STRING_LEN && s->wData[len]; len++)
  1010. continue;
  1011. if (!len)
  1012. return -EINVAL;
  1013. s->bLength = 2 * (len + 1);
  1014. return s->bLength;
  1015. }
  1016. if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
  1017. struct usb_os_string *b = buf;
  1018. b->bLength = sizeof(*b);
  1019. b->bDescriptorType = USB_DT_STRING;
  1020. compiletime_assert(
  1021. sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
  1022. "qwSignature size must be equal to qw_sign");
  1023. memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
  1024. b->bMS_VendorCode = cdev->b_vendor_code;
  1025. b->bPad = 0;
  1026. return sizeof(*b);
  1027. }
  1028. list_for_each_entry(uc, &cdev->gstrings, list) {
  1029. struct usb_gadget_strings **sp;
  1030. sp = get_containers_gs(uc);
  1031. len = lookup_string(sp, buf, language, id);
  1032. if (len > 0)
  1033. return len;
  1034. }
  1035. /* String IDs are device-scoped, so we look up each string
  1036. * table we're told about. These lookups are infrequent;
  1037. * simpler-is-better here.
  1038. */
  1039. if (composite->strings) {
  1040. len = lookup_string(composite->strings, buf, language, id);
  1041. if (len > 0)
  1042. return len;
  1043. }
  1044. list_for_each_entry(c, &cdev->configs, list) {
  1045. if (c->strings) {
  1046. len = lookup_string(c->strings, buf, language, id);
  1047. if (len > 0)
  1048. return len;
  1049. }
  1050. list_for_each_entry(f, &c->functions, list) {
  1051. if (!f->strings)
  1052. continue;
  1053. len = lookup_string(f->strings, buf, language, id);
  1054. if (len > 0)
  1055. return len;
  1056. }
  1057. }
  1058. return -EINVAL;
  1059. }
  1060. /**
  1061. * usb_string_id() - allocate an unused string ID
  1062. * @cdev: the device whose string descriptor IDs are being allocated
  1063. * Context: single threaded during gadget setup
  1064. *
  1065. * @usb_string_id() is called from bind() callbacks to allocate
  1066. * string IDs. Drivers for functions, configurations, or gadgets will
  1067. * then store that ID in the appropriate descriptors and string table.
  1068. *
  1069. * All string identifier should be allocated using this,
  1070. * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
  1071. * that for example different functions don't wrongly assign different
  1072. * meanings to the same identifier.
  1073. */
  1074. int usb_string_id(struct usb_composite_dev *cdev)
  1075. {
  1076. if (cdev->next_string_id < 254) {
  1077. /* string id 0 is reserved by USB spec for list of
  1078. * supported languages */
  1079. /* 255 reserved as well? -- mina86 */
  1080. cdev->next_string_id++;
  1081. return cdev->next_string_id;
  1082. }
  1083. return -ENODEV;
  1084. }
  1085. EXPORT_SYMBOL_GPL(usb_string_id);
  1086. /**
  1087. * usb_string_ids() - allocate unused string IDs in batch
  1088. * @cdev: the device whose string descriptor IDs are being allocated
  1089. * @str: an array of usb_string objects to assign numbers to
  1090. * Context: single threaded during gadget setup
  1091. *
  1092. * @usb_string_ids() is called from bind() callbacks to allocate
  1093. * string IDs. Drivers for functions, configurations, or gadgets will
  1094. * then copy IDs from the string table to the appropriate descriptors
  1095. * and string table for other languages.
  1096. *
  1097. * All string identifier should be allocated using this,
  1098. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  1099. * example different functions don't wrongly assign different meanings
  1100. * to the same identifier.
  1101. */
  1102. int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
  1103. {
  1104. int next = cdev->next_string_id;
  1105. for (; str->s; ++str) {
  1106. if (unlikely(next >= 254))
  1107. return -ENODEV;
  1108. str->id = ++next;
  1109. }
  1110. cdev->next_string_id = next;
  1111. return 0;
  1112. }
  1113. EXPORT_SYMBOL_GPL(usb_string_ids_tab);
  1114. static struct usb_gadget_string_container *copy_gadget_strings(
  1115. struct usb_gadget_strings **sp, unsigned n_gstrings,
  1116. unsigned n_strings)
  1117. {
  1118. struct usb_gadget_string_container *uc;
  1119. struct usb_gadget_strings **gs_array;
  1120. struct usb_gadget_strings *gs;
  1121. struct usb_string *s;
  1122. unsigned mem;
  1123. unsigned n_gs;
  1124. unsigned n_s;
  1125. void *stash;
  1126. mem = sizeof(*uc);
  1127. mem += sizeof(void *) * (n_gstrings + 1);
  1128. mem += sizeof(struct usb_gadget_strings) * n_gstrings;
  1129. mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
  1130. uc = kmalloc(mem, GFP_KERNEL);
  1131. if (!uc)
  1132. return ERR_PTR(-ENOMEM);
  1133. gs_array = get_containers_gs(uc);
  1134. stash = uc->stash;
  1135. stash += sizeof(void *) * (n_gstrings + 1);
  1136. for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
  1137. struct usb_string *org_s;
  1138. gs_array[n_gs] = stash;
  1139. gs = gs_array[n_gs];
  1140. stash += sizeof(struct usb_gadget_strings);
  1141. gs->language = sp[n_gs]->language;
  1142. gs->strings = stash;
  1143. org_s = sp[n_gs]->strings;
  1144. for (n_s = 0; n_s < n_strings; n_s++) {
  1145. s = stash;
  1146. stash += sizeof(struct usb_string);
  1147. if (org_s->s)
  1148. s->s = org_s->s;
  1149. else
  1150. s->s = "";
  1151. org_s++;
  1152. }
  1153. s = stash;
  1154. s->s = NULL;
  1155. stash += sizeof(struct usb_string);
  1156. }
  1157. gs_array[n_gs] = NULL;
  1158. return uc;
  1159. }
  1160. /**
  1161. * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
  1162. * @cdev: the device whose string descriptor IDs are being allocated
  1163. * and attached.
  1164. * @sp: an array of usb_gadget_strings to attach.
  1165. * @n_strings: number of entries in each usb_strings array (sp[]->strings)
  1166. *
  1167. * This function will create a deep copy of usb_gadget_strings and usb_string
  1168. * and attach it to the cdev. The actual string (usb_string.s) will not be
  1169. * copied but only a referenced will be made. The struct usb_gadget_strings
  1170. * array may contain multiple languages and should be NULL terminated.
  1171. * The ->language pointer of each struct usb_gadget_strings has to contain the
  1172. * same amount of entries.
  1173. * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
  1174. * usb_string entry of es-ES contains the translation of the first usb_string
  1175. * entry of en-US. Therefore both entries become the same id assign.
  1176. */
  1177. struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
  1178. struct usb_gadget_strings **sp, unsigned n_strings)
  1179. {
  1180. struct usb_gadget_string_container *uc;
  1181. struct usb_gadget_strings **n_gs;
  1182. unsigned n_gstrings = 0;
  1183. unsigned i;
  1184. int ret;
  1185. for (i = 0; sp[i]; i++)
  1186. n_gstrings++;
  1187. if (!n_gstrings)
  1188. return ERR_PTR(-EINVAL);
  1189. uc = copy_gadget_strings(sp, n_gstrings, n_strings);
  1190. if (IS_ERR(uc))
  1191. return ERR_CAST(uc);
  1192. n_gs = get_containers_gs(uc);
  1193. ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
  1194. if (ret)
  1195. goto err;
  1196. for (i = 1; i < n_gstrings; i++) {
  1197. struct usb_string *m_s;
  1198. struct usb_string *s;
  1199. unsigned n;
  1200. m_s = n_gs[0]->strings;
  1201. s = n_gs[i]->strings;
  1202. for (n = 0; n < n_strings; n++) {
  1203. s->id = m_s->id;
  1204. s++;
  1205. m_s++;
  1206. }
  1207. }
  1208. list_add_tail(&uc->list, &cdev->gstrings);
  1209. return n_gs[0]->strings;
  1210. err:
  1211. kfree(uc);
  1212. return ERR_PTR(ret);
  1213. }
  1214. EXPORT_SYMBOL_GPL(usb_gstrings_attach);
  1215. /**
  1216. * usb_string_ids_n() - allocate unused string IDs in batch
  1217. * @c: the device whose string descriptor IDs are being allocated
  1218. * @n: number of string IDs to allocate
  1219. * Context: single threaded during gadget setup
  1220. *
  1221. * Returns the first requested ID. This ID and next @n-1 IDs are now
  1222. * valid IDs. At least provided that @n is non-zero because if it
  1223. * is, returns last requested ID which is now very useful information.
  1224. *
  1225. * @usb_string_ids_n() is called from bind() callbacks to allocate
  1226. * string IDs. Drivers for functions, configurations, or gadgets will
  1227. * then store that ID in the appropriate descriptors and string table.
  1228. *
  1229. * All string identifier should be allocated using this,
  1230. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  1231. * example different functions don't wrongly assign different meanings
  1232. * to the same identifier.
  1233. */
  1234. int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
  1235. {
  1236. unsigned next = c->next_string_id;
  1237. if (unlikely(n > 254 || (unsigned)next + n > 254))
  1238. return -ENODEV;
  1239. c->next_string_id += n;
  1240. return next + 1;
  1241. }
  1242. EXPORT_SYMBOL_GPL(usb_string_ids_n);
  1243. /*-------------------------------------------------------------------------*/
  1244. static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
  1245. {
  1246. struct usb_composite_dev *cdev;
  1247. if (req->status || req->actual != req->length)
  1248. DBG((struct usb_composite_dev *) ep->driver_data,
  1249. "setup complete --> %d, %d/%d\n",
  1250. req->status, req->actual, req->length);
  1251. /*
  1252. * REVIST The same ep0 requests are shared with function drivers
  1253. * so they don't have to maintain the same ->complete() stubs.
  1254. *
  1255. * Because of that, we need to check for the validity of ->context
  1256. * here, even though we know we've set it to something useful.
  1257. */
  1258. if (!req->context)
  1259. return;
  1260. cdev = req->context;
  1261. if (cdev->req == req)
  1262. cdev->setup_pending = false;
  1263. else if (cdev->os_desc_req == req)
  1264. cdev->os_desc_pending = false;
  1265. else
  1266. WARN(1, "unknown request %p\n", req);
  1267. }
  1268. static int composite_ep0_queue(struct usb_composite_dev *cdev,
  1269. struct usb_request *req, gfp_t gfp_flags)
  1270. {
  1271. int ret;
  1272. ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
  1273. if (ret == 0) {
  1274. if (cdev->req == req)
  1275. cdev->setup_pending = true;
  1276. else if (cdev->os_desc_req == req)
  1277. cdev->os_desc_pending = true;
  1278. else
  1279. WARN(1, "unknown request %p\n", req);
  1280. }
  1281. return ret;
  1282. }
  1283. static int count_ext_compat(struct usb_configuration *c)
  1284. {
  1285. int i, res;
  1286. res = 0;
  1287. for (i = 0; i < c->next_interface_id; ++i) {
  1288. struct usb_function *f;
  1289. int j;
  1290. f = c->interface[i];
  1291. for (j = 0; j < f->os_desc_n; ++j) {
  1292. struct usb_os_desc *d;
  1293. if (i != f->os_desc_table[j].if_id)
  1294. continue;
  1295. d = f->os_desc_table[j].os_desc;
  1296. if (d && d->ext_compat_id)
  1297. ++res;
  1298. }
  1299. }
  1300. BUG_ON(res > 255);
  1301. return res;
  1302. }
  1303. static int fill_ext_compat(struct usb_configuration *c, u8 *buf)
  1304. {
  1305. int i, count;
  1306. count = 16;
  1307. buf += 16;
  1308. for (i = 0; i < c->next_interface_id; ++i) {
  1309. struct usb_function *f;
  1310. int j;
  1311. f = c->interface[i];
  1312. for (j = 0; j < f->os_desc_n; ++j) {
  1313. struct usb_os_desc *d;
  1314. if (i != f->os_desc_table[j].if_id)
  1315. continue;
  1316. d = f->os_desc_table[j].os_desc;
  1317. if (d && d->ext_compat_id) {
  1318. *buf++ = i;
  1319. *buf++ = 0x01;
  1320. memcpy(buf, d->ext_compat_id, 16);
  1321. buf += 22;
  1322. } else {
  1323. ++buf;
  1324. *buf = 0x01;
  1325. buf += 23;
  1326. }
  1327. count += 24;
  1328. if (count + 24 >= USB_COMP_EP0_OS_DESC_BUFSIZ)
  1329. return count;
  1330. }
  1331. }
  1332. return count;
  1333. }
  1334. static int count_ext_prop(struct usb_configuration *c, int interface)
  1335. {
  1336. struct usb_function *f;
  1337. int j;
  1338. f = c->interface[interface];
  1339. for (j = 0; j < f->os_desc_n; ++j) {
  1340. struct usb_os_desc *d;
  1341. if (interface != f->os_desc_table[j].if_id)
  1342. continue;
  1343. d = f->os_desc_table[j].os_desc;
  1344. if (d && d->ext_compat_id)
  1345. return d->ext_prop_count;
  1346. }
  1347. return 0;
  1348. }
  1349. static int len_ext_prop(struct usb_configuration *c, int interface)
  1350. {
  1351. struct usb_function *f;
  1352. struct usb_os_desc *d;
  1353. int j, res;
  1354. res = 10; /* header length */
  1355. f = c->interface[interface];
  1356. for (j = 0; j < f->os_desc_n; ++j) {
  1357. if (interface != f->os_desc_table[j].if_id)
  1358. continue;
  1359. d = f->os_desc_table[j].os_desc;
  1360. if (d)
  1361. return min(res + d->ext_prop_len, 4096);
  1362. }
  1363. return res;
  1364. }
  1365. static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
  1366. {
  1367. struct usb_function *f;
  1368. struct usb_os_desc *d;
  1369. struct usb_os_desc_ext_prop *ext_prop;
  1370. int j, count, n, ret;
  1371. f = c->interface[interface];
  1372. count = 10; /* header length */
  1373. buf += 10;
  1374. for (j = 0; j < f->os_desc_n; ++j) {
  1375. if (interface != f->os_desc_table[j].if_id)
  1376. continue;
  1377. d = f->os_desc_table[j].os_desc;
  1378. if (d)
  1379. list_for_each_entry(ext_prop, &d->ext_prop, entry) {
  1380. n = ext_prop->data_len +
  1381. ext_prop->name_len + 14;
  1382. if (count + n >= USB_COMP_EP0_OS_DESC_BUFSIZ)
  1383. return count;
  1384. usb_ext_prop_put_size(buf, n);
  1385. usb_ext_prop_put_type(buf, ext_prop->type);
  1386. ret = usb_ext_prop_put_name(buf, ext_prop->name,
  1387. ext_prop->name_len);
  1388. if (ret < 0)
  1389. return ret;
  1390. switch (ext_prop->type) {
  1391. case USB_EXT_PROP_UNICODE:
  1392. case USB_EXT_PROP_UNICODE_ENV:
  1393. case USB_EXT_PROP_UNICODE_LINK:
  1394. usb_ext_prop_put_unicode(buf, ret,
  1395. ext_prop->data,
  1396. ext_prop->data_len);
  1397. break;
  1398. case USB_EXT_PROP_BINARY:
  1399. usb_ext_prop_put_binary(buf, ret,
  1400. ext_prop->data,
  1401. ext_prop->data_len);
  1402. break;
  1403. case USB_EXT_PROP_LE32:
  1404. /* not implemented */
  1405. case USB_EXT_PROP_BE32:
  1406. /* not implemented */
  1407. default:
  1408. return -EINVAL;
  1409. }
  1410. buf += n;
  1411. count += n;
  1412. }
  1413. }
  1414. return count;
  1415. }
  1416. int usb_dev_ready_notify(int is_ready);
  1417. /*
  1418. * The setup() callback implements all the ep0 functionality that's
  1419. * not handled lower down, in hardware or the hardware driver(like
  1420. * device and endpoint feature flags, and their status). It's all
  1421. * housekeeping for the gadget function we're implementing. Most of
  1422. * the work is in config and function specific setup.
  1423. */
  1424. int
  1425. composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  1426. {
  1427. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1428. struct usb_request *req = cdev->req;
  1429. int value = -EOPNOTSUPP;
  1430. int status = 0;
  1431. u16 w_index = le16_to_cpu(ctrl->wIndex);
  1432. u8 intf = w_index & 0xFF;
  1433. u16 w_value = le16_to_cpu(ctrl->wValue);
  1434. u16 w_length = le16_to_cpu(ctrl->wLength);
  1435. struct usb_function *f = NULL;
  1436. u8 endp;
  1437. /* partial re-init of the response message; the function or the
  1438. * gadget might need to intercept e.g. a control-OUT completion
  1439. * when we delegate to it.
  1440. */
  1441. req->zero = 0;
  1442. req->context = cdev;
  1443. req->complete = composite_setup_complete;
  1444. req->length = 0;
  1445. gadget->ep0->driver_data = cdev;
  1446. /*
  1447. * Don't let non-standard requests match any of the cases below
  1448. * by accident.
  1449. */
  1450. if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
  1451. goto unknown;
  1452. switch (ctrl->bRequest) {
  1453. /* we handle all standard USB descriptors */
  1454. case USB_REQ_GET_DESCRIPTOR:
  1455. if (ctrl->bRequestType != USB_DIR_IN)
  1456. goto unknown;
  1457. switch (w_value >> 8) {
  1458. case USB_DT_DEVICE:
  1459. cdev->desc.bNumConfigurations =
  1460. count_configs(cdev, USB_DT_DEVICE);
  1461. cdev->desc.bMaxPacketSize0 =
  1462. cdev->gadget->ep0->maxpacket;
  1463. if (gadget_is_superspeed(gadget)) {
  1464. if (gadget->speed >= USB_SPEED_SUPER) {
  1465. cdev->desc.bcdUSB = cpu_to_le16(0x0320);
  1466. cdev->desc.bMaxPacketSize0 = 9;
  1467. } else {
  1468. cdev->desc.bcdUSB = cpu_to_le16(0x0210);
  1469. }
  1470. } else {
  1471. if (gadget->lpm_capable)
  1472. cdev->desc.bcdUSB = cpu_to_le16(0x0201);
  1473. else
  1474. cdev->desc.bcdUSB = cpu_to_le16(0x0200);
  1475. }
  1476. value = min(w_length, (u16) sizeof cdev->desc);
  1477. memcpy(req->buf, &cdev->desc, value);
  1478. break;
  1479. case USB_DT_DEVICE_QUALIFIER:
  1480. if (!gadget_is_dualspeed(gadget) ||
  1481. gadget->speed >= USB_SPEED_SUPER)
  1482. break;
  1483. device_qual(cdev);
  1484. value = min_t(int, w_length,
  1485. sizeof(struct usb_qualifier_descriptor));
  1486. break;
  1487. case USB_DT_OTHER_SPEED_CONFIG:
  1488. if (!gadget_is_dualspeed(gadget) ||
  1489. gadget->speed >= USB_SPEED_SUPER)
  1490. break;
  1491. /* FALLTHROUGH */
  1492. case USB_DT_CONFIG:
  1493. value = config_desc(cdev, w_value);
  1494. if (value >= 0)
  1495. value = min(w_length, (u16) value);
  1496. break;
  1497. case USB_DT_STRING:
  1498. value = get_string(cdev, req->buf,
  1499. w_index, w_value & 0xff);
  1500. if (value >= 0)
  1501. value = min(w_length, (u16) value);
  1502. break;
  1503. case USB_DT_BOS:
  1504. if (gadget_is_superspeed(gadget) ||
  1505. gadget->lpm_capable) {
  1506. value = bos_desc(cdev);
  1507. value = min(w_length, (u16) value);
  1508. }
  1509. break;
  1510. case USB_DT_OTG:
  1511. if (gadget_is_otg(gadget)) {
  1512. struct usb_configuration *config;
  1513. int otg_desc_len = 0;
  1514. if (cdev->config)
  1515. config = cdev->config;
  1516. else
  1517. config = list_first_entry(
  1518. &cdev->configs,
  1519. struct usb_configuration, list);
  1520. if (!config)
  1521. goto done;
  1522. if (gadget->otg_caps &&
  1523. (gadget->otg_caps->otg_rev >= 0x0200))
  1524. otg_desc_len += sizeof(
  1525. struct usb_otg20_descriptor);
  1526. else
  1527. otg_desc_len += sizeof(
  1528. struct usb_otg_descriptor);
  1529. value = min_t(int, w_length, otg_desc_len);
  1530. memcpy(req->buf, config->descriptors[0], value);
  1531. }
  1532. break;
  1533. }
  1534. break;
  1535. /* any number of configs can work */
  1536. case USB_REQ_SET_CONFIGURATION:
  1537. if (ctrl->bRequestType != 0)
  1538. goto unknown;
  1539. if (gadget_is_otg(gadget)) {
  1540. if (gadget->a_hnp_support)
  1541. DBG(cdev, "HNP available\n");
  1542. else if (gadget->a_alt_hnp_support)
  1543. DBG(cdev, "HNP on another port\n");
  1544. else
  1545. VDBG(cdev, "HNP inactive\n");
  1546. }
  1547. spin_lock(&cdev->lock);
  1548. value = set_config(cdev, ctrl, w_value);
  1549. spin_unlock(&cdev->lock);
  1550. break;
  1551. case USB_REQ_GET_CONFIGURATION:
  1552. if (ctrl->bRequestType != USB_DIR_IN)
  1553. goto unknown;
  1554. if (cdev->config)
  1555. *(u8 *)req->buf = cdev->config->bConfigurationValue;
  1556. else
  1557. *(u8 *)req->buf = 0;
  1558. value = min(w_length, (u16) 1);
  1559. break;
  1560. /* function drivers must handle get/set altsetting */
  1561. case USB_REQ_SET_INTERFACE:
  1562. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  1563. goto unknown;
  1564. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1565. break;
  1566. f = cdev->config->interface[intf];
  1567. if (!f)
  1568. break;
  1569. /*
  1570. * If there's no get_alt() method, we know only altsetting zero
  1571. * works. There is no need to check if set_alt() is not NULL
  1572. * as we check this in usb_add_function().
  1573. */
  1574. if (w_value && !f->get_alt)
  1575. break;
  1576. spin_lock(&cdev->lock);
  1577. value = f->set_alt(f, w_index, w_value);
  1578. if (value == USB_GADGET_DELAYED_STATUS) {
  1579. DBG(cdev,
  1580. "%s: interface %d (%s) requested delayed status\n",
  1581. __func__, intf, f->name);
  1582. cdev->delayed_status++;
  1583. DBG(cdev, "delayed_status count %d\n",
  1584. cdev->delayed_status);
  1585. }
  1586. spin_unlock(&cdev->lock);
  1587. break;
  1588. case USB_REQ_GET_INTERFACE:
  1589. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  1590. goto unknown;
  1591. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1592. break;
  1593. f = cdev->config->interface[intf];
  1594. if (!f)
  1595. break;
  1596. /* lots of interfaces only need altsetting zero... */
  1597. value = f->get_alt ? f->get_alt(f, w_index) : 0;
  1598. if (value < 0)
  1599. break;
  1600. *((u8 *)req->buf) = value;
  1601. value = min(w_length, (u16) 1);
  1602. break;
  1603. case USB_REQ_GET_STATUS:
  1604. if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
  1605. (w_index == OTG_STS_SELECTOR)) {
  1606. if (ctrl->bRequestType != (USB_DIR_IN |
  1607. USB_RECIP_DEVICE))
  1608. goto unknown;
  1609. *((u8 *)req->buf) = gadget->host_request_flag;
  1610. value = 1;
  1611. break;
  1612. }
  1613. /*
  1614. * USB 3.0 additions:
  1615. * Function driver should handle get_status request. If such cb
  1616. * wasn't supplied we respond with default value = 0
  1617. * Note: function driver should supply such cb only for the
  1618. * first interface of the function
  1619. */
  1620. if (!gadget_is_superspeed(gadget))
  1621. goto unknown;
  1622. if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
  1623. goto unknown;
  1624. value = 2; /* This is the length of the get_status reply */
  1625. put_unaligned_le16(0, req->buf);
  1626. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1627. break;
  1628. f = cdev->config->interface[intf];
  1629. if (!f)
  1630. break;
  1631. status = f->get_status ? f->get_status(f) : 0;
  1632. if (status < 0)
  1633. break;
  1634. put_unaligned_le16(status & 0x0000ffff, req->buf);
  1635. break;
  1636. /*
  1637. * Function drivers should handle SetFeature/ClearFeature
  1638. * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
  1639. * only for the first interface of the function
  1640. */
  1641. case USB_REQ_CLEAR_FEATURE:
  1642. case USB_REQ_SET_FEATURE:
  1643. if (!gadget_is_superspeed(gadget))
  1644. goto unknown;
  1645. if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
  1646. goto unknown;
  1647. switch (w_value) {
  1648. case USB_INTRF_FUNC_SUSPEND:
  1649. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1650. break;
  1651. f = cdev->config->interface[intf];
  1652. if (!f)
  1653. break;
  1654. value = 0;
  1655. if (f->func_suspend)
  1656. value = f->func_suspend(f, w_index >> 8);
  1657. if (value < 0) {
  1658. ERROR(cdev,
  1659. "func_suspend() returned error %d\n",
  1660. value);
  1661. value = 0;
  1662. }
  1663. break;
  1664. }
  1665. break;
  1666. default:
  1667. unknown:
  1668. /*
  1669. * OS descriptors handling
  1670. */
  1671. if (cdev->use_os_string && cdev->os_desc_config &&
  1672. (ctrl->bRequestType & USB_TYPE_VENDOR) &&
  1673. ctrl->bRequest == cdev->b_vendor_code) {
  1674. struct usb_configuration *os_desc_cfg;
  1675. u8 *buf;
  1676. int interface;
  1677. int count = 0;
  1678. req = cdev->os_desc_req;
  1679. req->context = cdev;
  1680. req->complete = composite_setup_complete;
  1681. buf = req->buf;
  1682. os_desc_cfg = cdev->os_desc_config;
  1683. w_length = min_t(u16, w_length, USB_COMP_EP0_OS_DESC_BUFSIZ);
  1684. memset(buf, 0, w_length);
  1685. buf[5] = 0x01;
  1686. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1687. case USB_RECIP_DEVICE:
  1688. if (w_index != 0x4 || (w_value >> 8))
  1689. break;
  1690. buf[6] = w_index;
  1691. /* Number of ext compat interfaces */
  1692. count = count_ext_compat(os_desc_cfg);
  1693. buf[8] = count;
  1694. count *= 24; /* 24 B/ext compat desc */
  1695. count += 16; /* header */
  1696. put_unaligned_le32(count, buf);
  1697. value = w_length;
  1698. if (w_length > 0x10) {
  1699. value = fill_ext_compat(os_desc_cfg, buf);
  1700. value = min_t(u16, w_length, value);
  1701. }
  1702. break;
  1703. case USB_RECIP_INTERFACE:
  1704. if (w_index != 0x5 || (w_value >> 8))
  1705. break;
  1706. interface = w_value & 0xFF;
  1707. buf[6] = w_index;
  1708. count = count_ext_prop(os_desc_cfg,
  1709. interface);
  1710. put_unaligned_le16(count, buf + 8);
  1711. count = len_ext_prop(os_desc_cfg,
  1712. interface);
  1713. put_unaligned_le32(count, buf);
  1714. value = w_length;
  1715. if (w_length > 0x0A) {
  1716. value = fill_ext_prop(os_desc_cfg,
  1717. interface, buf);
  1718. if (value >= 0)
  1719. value = min_t(u16, w_length, value);
  1720. }
  1721. break;
  1722. }
  1723. goto check_value;
  1724. }
  1725. VDBG(cdev,
  1726. "non-core control req%02x.%02x v%04x i%04x l%d\n",
  1727. ctrl->bRequestType, ctrl->bRequest,
  1728. w_value, w_index, w_length);
  1729. /* functions always handle their interfaces and endpoints...
  1730. * punt other recipients (other, WUSB, ...) to the current
  1731. * configuration code.
  1732. */
  1733. if (cdev->config) {
  1734. list_for_each_entry(f, &cdev->config->functions, list)
  1735. if (f->req_match &&
  1736. f->req_match(f, ctrl, false))
  1737. goto try_fun_setup;
  1738. } else {
  1739. struct usb_configuration *c;
  1740. list_for_each_entry(c, &cdev->configs, list)
  1741. list_for_each_entry(f, &c->functions, list)
  1742. if (f->req_match &&
  1743. f->req_match(f, ctrl, true))
  1744. goto try_fun_setup;
  1745. }
  1746. f = NULL;
  1747. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1748. case USB_RECIP_INTERFACE:
  1749. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1750. break;
  1751. f = cdev->config->interface[intf];
  1752. break;
  1753. case USB_RECIP_ENDPOINT:
  1754. if (!cdev->config)
  1755. break;
  1756. endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
  1757. list_for_each_entry(f, &cdev->config->functions, list) {
  1758. if (test_bit(endp, f->endpoints))
  1759. break;
  1760. }
  1761. if (&f->list == &cdev->config->functions)
  1762. f = NULL;
  1763. break;
  1764. }
  1765. try_fun_setup:
  1766. if (f && f->setup)
  1767. value = f->setup(f, ctrl);
  1768. else {
  1769. struct usb_configuration *c;
  1770. c = cdev->config;
  1771. if (!c)
  1772. goto done;
  1773. /* try current config's setup */
  1774. if (c->setup) {
  1775. value = c->setup(c, ctrl);
  1776. goto done;
  1777. }
  1778. #ifdef CONFIG_USB_CONFIGFS_F_APPLE_PTP_SIM
  1779. if (ctrl->bRequestType == 0x40 && ctrl->bRequest == 0x51) {
  1780. usb_dev_ready_notify(1);
  1781. value = 0;
  1782. goto check_value;
  1783. }
  1784. #endif
  1785. /* try the only function in the current config */
  1786. if (!list_is_singular(&c->functions))
  1787. goto done;
  1788. f = list_first_entry(&c->functions, struct usb_function,
  1789. list);
  1790. if (f->setup)
  1791. value = f->setup(f, ctrl);
  1792. }
  1793. goto done;
  1794. }
  1795. check_value:
  1796. /* respond with data transfer before status phase? */
  1797. if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
  1798. req->length = value;
  1799. req->context = cdev;
  1800. req->zero = value < w_length;
  1801. value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
  1802. if (value < 0) {
  1803. DBG(cdev, "ep_queue --> %d\n", value);
  1804. req->status = 0;
  1805. composite_setup_complete(gadget->ep0, req);
  1806. }
  1807. } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
  1808. WARN(cdev,
  1809. "%s: Delayed status not supported for w_length != 0",
  1810. __func__);
  1811. }
  1812. done:
  1813. /* device either stalls (value < 0) or reports success */
  1814. return value;
  1815. }
  1816. void composite_disconnect(struct usb_gadget *gadget)
  1817. {
  1818. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1819. unsigned long flags;
  1820. /* REVISIT: should we have config and device level
  1821. * disconnect callbacks?
  1822. */
  1823. spin_lock_irqsave(&cdev->lock, flags);
  1824. cdev->suspended = 0;
  1825. if (cdev->config)
  1826. reset_config(cdev);
  1827. if (cdev->driver->disconnect)
  1828. cdev->driver->disconnect(cdev);
  1829. spin_unlock_irqrestore(&cdev->lock, flags);
  1830. }
  1831. /*-------------------------------------------------------------------------*/
  1832. static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
  1833. char *buf)
  1834. {
  1835. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  1836. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1837. return sprintf(buf, "%d\n", cdev->suspended);
  1838. }
  1839. static DEVICE_ATTR_RO(suspended);
  1840. static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
  1841. {
  1842. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1843. struct usb_gadget_strings *gstr = cdev->driver->strings[0];
  1844. struct usb_string *dev_str = gstr->strings;
  1845. /* composite_disconnect() must already have been called
  1846. * by the underlying peripheral controller driver!
  1847. * so there's no i/o concurrency that could affect the
  1848. * state protected by cdev->lock.
  1849. */
  1850. WARN_ON(cdev->config);
  1851. while (!list_empty(&cdev->configs)) {
  1852. struct usb_configuration *c;
  1853. c = list_first_entry(&cdev->configs,
  1854. struct usb_configuration, list);
  1855. remove_config(cdev, c);
  1856. }
  1857. if (cdev->driver->unbind && unbind_driver)
  1858. cdev->driver->unbind(cdev);
  1859. composite_dev_cleanup(cdev);
  1860. if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
  1861. dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
  1862. kfree(cdev->def_manufacturer);
  1863. kfree(cdev);
  1864. set_gadget_data(gadget, NULL);
  1865. }
  1866. static void composite_unbind(struct usb_gadget *gadget)
  1867. {
  1868. __composite_unbind(gadget, true);
  1869. }
  1870. static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
  1871. const struct usb_device_descriptor *old)
  1872. {
  1873. __le16 idVendor;
  1874. __le16 idProduct;
  1875. __le16 bcdDevice;
  1876. u8 iSerialNumber;
  1877. u8 iManufacturer;
  1878. u8 iProduct;
  1879. /*
  1880. * these variables may have been set in
  1881. * usb_composite_overwrite_options()
  1882. */
  1883. idVendor = new->idVendor;
  1884. idProduct = new->idProduct;
  1885. bcdDevice = new->bcdDevice;
  1886. iSerialNumber = new->iSerialNumber;
  1887. iManufacturer = new->iManufacturer;
  1888. iProduct = new->iProduct;
  1889. *new = *old;
  1890. if (idVendor)
  1891. new->idVendor = idVendor;
  1892. if (idProduct)
  1893. new->idProduct = idProduct;
  1894. if (bcdDevice)
  1895. new->bcdDevice = bcdDevice;
  1896. else
  1897. new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
  1898. if (iSerialNumber)
  1899. new->iSerialNumber = iSerialNumber;
  1900. if (iManufacturer)
  1901. new->iManufacturer = iManufacturer;
  1902. if (iProduct)
  1903. new->iProduct = iProduct;
  1904. }
  1905. int composite_dev_prepare(struct usb_composite_driver *composite,
  1906. struct usb_composite_dev *cdev)
  1907. {
  1908. struct usb_gadget *gadget = cdev->gadget;
  1909. int ret = -ENOMEM;
  1910. /* preallocate control response and buffer */
  1911. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  1912. if (!cdev->req)
  1913. return -ENOMEM;
  1914. cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
  1915. if (!cdev->req->buf)
  1916. goto fail;
  1917. ret = device_create_file(&gadget->dev, &dev_attr_suspended);
  1918. if (ret)
  1919. goto fail_dev;
  1920. cdev->req->complete = composite_setup_complete;
  1921. cdev->req->context = cdev;
  1922. gadget->ep0->driver_data = cdev;
  1923. cdev->driver = composite;
  1924. /*
  1925. * As per USB compliance update, a device that is actively drawing
  1926. * more than 100mA from USB must report itself as bus-powered in
  1927. * the GetStatus(DEVICE) call.
  1928. */
  1929. if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
  1930. usb_gadget_set_selfpowered(gadget);
  1931. /* interface and string IDs start at zero via kzalloc.
  1932. * we force endpoints to start unassigned; few controller
  1933. * drivers will zero ep->driver_data.
  1934. */
  1935. usb_ep_autoconfig_reset(gadget);
  1936. return 0;
  1937. fail_dev:
  1938. kfree(cdev->req->buf);
  1939. fail:
  1940. usb_ep_free_request(gadget->ep0, cdev->req);
  1941. cdev->req = NULL;
  1942. return ret;
  1943. }
  1944. int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
  1945. struct usb_ep *ep0)
  1946. {
  1947. int ret = 0;
  1948. cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
  1949. if (!cdev->os_desc_req) {
  1950. ret = -ENOMEM;
  1951. goto end;
  1952. }
  1953. cdev->os_desc_req->buf = kmalloc(USB_COMP_EP0_OS_DESC_BUFSIZ,
  1954. GFP_KERNEL);
  1955. if (!cdev->os_desc_req->buf) {
  1956. ret = -ENOMEM;
  1957. usb_ep_free_request(ep0, cdev->os_desc_req);
  1958. goto end;
  1959. }
  1960. cdev->os_desc_req->context = cdev;
  1961. cdev->os_desc_req->complete = composite_setup_complete;
  1962. end:
  1963. return ret;
  1964. }
  1965. void composite_dev_cleanup(struct usb_composite_dev *cdev)
  1966. {
  1967. struct usb_gadget_string_container *uc, *tmp;
  1968. struct usb_ep *ep, *tmp_ep;
  1969. list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
  1970. list_del(&uc->list);
  1971. kfree(uc);
  1972. }
  1973. if (cdev->os_desc_req) {
  1974. if (cdev->os_desc_pending)
  1975. usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
  1976. kfree(cdev->os_desc_req->buf);
  1977. cdev->os_desc_req->buf = NULL;
  1978. usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
  1979. cdev->os_desc_req = NULL;
  1980. }
  1981. if (cdev->req) {
  1982. if (cdev->setup_pending)
  1983. usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
  1984. kfree(cdev->req->buf);
  1985. cdev->req->buf = NULL;
  1986. usb_ep_free_request(cdev->gadget->ep0, cdev->req);
  1987. cdev->req = NULL;
  1988. }
  1989. cdev->next_string_id = 0;
  1990. device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
  1991. /*
  1992. * Some UDC backends have a dynamic EP allocation scheme.
  1993. *
  1994. * In that case, the dispose() callback is used to notify the
  1995. * backend that the EPs are no longer in use.
  1996. *
  1997. * Note: The UDC backend can remove the EP from the ep_list as
  1998. * a result, so we need to use the _safe list iterator.
  1999. */
  2000. list_for_each_entry_safe(ep, tmp_ep,
  2001. &cdev->gadget->ep_list, ep_list) {
  2002. if (ep->ops->dispose)
  2003. ep->ops->dispose(ep);
  2004. }
  2005. }
  2006. static int composite_bind(struct usb_gadget *gadget,
  2007. struct usb_gadget_driver *gdriver)
  2008. {
  2009. struct usb_composite_dev *cdev;
  2010. struct usb_composite_driver *composite = to_cdriver(gdriver);
  2011. int status = -ENOMEM;
  2012. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  2013. if (!cdev)
  2014. return status;
  2015. spin_lock_init(&cdev->lock);
  2016. cdev->gadget = gadget;
  2017. set_gadget_data(gadget, cdev);
  2018. INIT_LIST_HEAD(&cdev->configs);
  2019. INIT_LIST_HEAD(&cdev->gstrings);
  2020. status = composite_dev_prepare(composite, cdev);
  2021. if (status)
  2022. goto fail;
  2023. /* composite gadget needs to assign strings for whole device (like
  2024. * serial number), register function drivers, potentially update
  2025. * power state and consumption, etc
  2026. */
  2027. status = composite->bind(cdev);
  2028. if (status < 0)
  2029. goto fail;
  2030. if (cdev->use_os_string) {
  2031. status = composite_os_desc_req_prepare(cdev, gadget->ep0);
  2032. if (status)
  2033. goto fail;
  2034. }
  2035. update_unchanged_dev_desc(&cdev->desc, composite->dev);
  2036. /* has userspace failed to provide a serial number? */
  2037. if (composite->needs_serial && !cdev->desc.iSerialNumber)
  2038. WARNING(cdev, "userspace failed to provide iSerialNumber\n");
  2039. INFO(cdev, "%s ready\n", composite->name);
  2040. return 0;
  2041. fail:
  2042. __composite_unbind(gadget, false);
  2043. return status;
  2044. }
  2045. /*-------------------------------------------------------------------------*/
  2046. void composite_suspend(struct usb_gadget *gadget)
  2047. {
  2048. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  2049. struct usb_function *f;
  2050. /* REVISIT: should we have config level
  2051. * suspend/resume callbacks?
  2052. */
  2053. DBG(cdev, "suspend\n");
  2054. if (cdev->config) {
  2055. list_for_each_entry(f, &cdev->config->functions, list) {
  2056. if (f->suspend)
  2057. f->suspend(f);
  2058. }
  2059. }
  2060. if (cdev->driver->suspend)
  2061. cdev->driver->suspend(cdev);
  2062. cdev->suspended = 1;
  2063. usb_gadget_set_selfpowered(gadget);
  2064. usb_gadget_vbus_draw(gadget, 2);
  2065. }
  2066. void composite_resume(struct usb_gadget *gadget)
  2067. {
  2068. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  2069. struct usb_function *f;
  2070. unsigned maxpower;
  2071. /* REVISIT: should we have config level
  2072. * suspend/resume callbacks?
  2073. */
  2074. DBG(cdev, "resume\n");
  2075. if (cdev->driver->resume)
  2076. cdev->driver->resume(cdev);
  2077. if (cdev->config) {
  2078. list_for_each_entry(f, &cdev->config->functions, list) {
  2079. if (f->resume)
  2080. f->resume(f);
  2081. }
  2082. maxpower = cdev->config->MaxPower ?
  2083. cdev->config->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
  2084. if (gadget->speed < USB_SPEED_SUPER)
  2085. maxpower = min(maxpower, 500U);
  2086. else
  2087. maxpower = min(maxpower, 900U);
  2088. if (maxpower > USB_SELF_POWER_VBUS_MAX_DRAW)
  2089. usb_gadget_clear_selfpowered(gadget);
  2090. usb_gadget_vbus_draw(gadget, maxpower);
  2091. }
  2092. cdev->suspended = 0;
  2093. }
  2094. /*-------------------------------------------------------------------------*/
  2095. static const struct usb_gadget_driver composite_driver_template = {
  2096. .bind = composite_bind,
  2097. .unbind = composite_unbind,
  2098. .setup = composite_setup,
  2099. .reset = composite_disconnect,
  2100. .disconnect = composite_disconnect,
  2101. .suspend = composite_suspend,
  2102. .resume = composite_resume,
  2103. .driver = {
  2104. .owner = THIS_MODULE,
  2105. },
  2106. };
  2107. /**
  2108. * usb_composite_probe() - register a composite driver
  2109. * @driver: the driver to register
  2110. *
  2111. * Context: single threaded during gadget setup
  2112. *
  2113. * This function is used to register drivers using the composite driver
  2114. * framework. The return value is zero, or a negative errno value.
  2115. * Those values normally come from the driver's @bind method, which does
  2116. * all the work of setting up the driver to match the hardware.
  2117. *
  2118. * On successful return, the gadget is ready to respond to requests from
  2119. * the host, unless one of its components invokes usb_gadget_disconnect()
  2120. * while it was binding. That would usually be done in order to wait for
  2121. * some userspace participation.
  2122. */
  2123. int usb_composite_probe(struct usb_composite_driver *driver)
  2124. {
  2125. struct usb_gadget_driver *gadget_driver;
  2126. if (!driver || !driver->dev || !driver->bind)
  2127. return -EINVAL;
  2128. if (!driver->name)
  2129. driver->name = "composite";
  2130. driver->gadget_driver = composite_driver_template;
  2131. gadget_driver = &driver->gadget_driver;
  2132. gadget_driver->function = (char *) driver->name;
  2133. gadget_driver->driver.name = driver->name;
  2134. gadget_driver->max_speed = driver->max_speed;
  2135. return usb_gadget_probe_driver(gadget_driver);
  2136. }
  2137. EXPORT_SYMBOL_GPL(usb_composite_probe);
  2138. /**
  2139. * usb_composite_unregister() - unregister a composite driver
  2140. * @driver: the driver to unregister
  2141. *
  2142. * This function is used to unregister drivers using the composite
  2143. * driver framework.
  2144. */
  2145. void usb_composite_unregister(struct usb_composite_driver *driver)
  2146. {
  2147. usb_gadget_unregister_driver(&driver->gadget_driver);
  2148. }
  2149. EXPORT_SYMBOL_GPL(usb_composite_unregister);
  2150. /**
  2151. * usb_composite_setup_continue() - Continue with the control transfer
  2152. * @cdev: the composite device who's control transfer was kept waiting
  2153. *
  2154. * This function must be called by the USB function driver to continue
  2155. * with the control transfer's data/status stage in case it had requested to
  2156. * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
  2157. * can request the composite framework to delay the setup request's data/status
  2158. * stages by returning USB_GADGET_DELAYED_STATUS.
  2159. */
  2160. void usb_composite_setup_continue(struct usb_composite_dev *cdev)
  2161. {
  2162. int value;
  2163. struct usb_request *req = cdev->req;
  2164. unsigned long flags;
  2165. DBG(cdev, "%s\n", __func__);
  2166. spin_lock_irqsave(&cdev->lock, flags);
  2167. if (cdev->delayed_status == 0) {
  2168. WARN(cdev, "%s: Unexpected call\n", __func__);
  2169. } else if (--cdev->delayed_status == 0) {
  2170. DBG(cdev, "%s: Completing delayed status\n", __func__);
  2171. req->length = 0;
  2172. req->context = cdev;
  2173. value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
  2174. if (value < 0) {
  2175. DBG(cdev, "ep_queue --> %d\n", value);
  2176. req->status = 0;
  2177. composite_setup_complete(cdev->gadget->ep0, req);
  2178. }
  2179. }
  2180. spin_unlock_irqrestore(&cdev->lock, flags);
  2181. }
  2182. EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
  2183. static char *composite_default_mfr(struct usb_gadget *gadget)
  2184. {
  2185. return kasprintf(GFP_KERNEL, "%s %s with %s", init_utsname()->sysname,
  2186. init_utsname()->release, gadget->name);
  2187. }
  2188. void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
  2189. struct usb_composite_overwrite *covr)
  2190. {
  2191. struct usb_device_descriptor *desc = &cdev->desc;
  2192. struct usb_gadget_strings *gstr = cdev->driver->strings[0];
  2193. struct usb_string *dev_str = gstr->strings;
  2194. if (covr->idVendor)
  2195. desc->idVendor = cpu_to_le16(covr->idVendor);
  2196. if (covr->idProduct)
  2197. desc->idProduct = cpu_to_le16(covr->idProduct);
  2198. if (covr->bcdDevice)
  2199. desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
  2200. if (covr->serial_number) {
  2201. desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
  2202. dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
  2203. }
  2204. if (covr->manufacturer) {
  2205. desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
  2206. dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
  2207. } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
  2208. desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
  2209. cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
  2210. dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
  2211. }
  2212. if (covr->product) {
  2213. desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
  2214. dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
  2215. }
  2216. }
  2217. EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
  2218. MODULE_LICENSE("GPL");
  2219. MODULE_AUTHOR("David Brownell");