composite.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  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. cdev->next_string_id = 0;
  1459. device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
  1460. }
  1461. static int composite_bind(struct usb_gadget *gadget,
  1462. struct usb_gadget_driver *gdriver)
  1463. {
  1464. struct usb_composite_dev *cdev;
  1465. struct usb_composite_driver *composite = to_cdriver(gdriver);
  1466. int status = -ENOMEM;
  1467. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  1468. if (!cdev)
  1469. return status;
  1470. spin_lock_init(&cdev->lock);
  1471. cdev->gadget = gadget;
  1472. set_gadget_data(gadget, cdev);
  1473. INIT_LIST_HEAD(&cdev->configs);
  1474. INIT_LIST_HEAD(&cdev->gstrings);
  1475. status = composite_dev_prepare(composite, cdev);
  1476. if (status)
  1477. goto fail;
  1478. /* composite gadget needs to assign strings for whole device (like
  1479. * serial number), register function drivers, potentially update
  1480. * power state and consumption, etc
  1481. */
  1482. status = composite->bind(cdev);
  1483. if (status < 0)
  1484. goto fail;
  1485. update_unchanged_dev_desc(&cdev->desc, composite->dev);
  1486. /* has userspace failed to provide a serial number? */
  1487. if (composite->needs_serial && !cdev->desc.iSerialNumber)
  1488. WARNING(cdev, "userspace failed to provide iSerialNumber\n");
  1489. INFO(cdev, "%s ready\n", composite->name);
  1490. return 0;
  1491. fail:
  1492. __composite_unbind(gadget, false);
  1493. return status;
  1494. }
  1495. /*-------------------------------------------------------------------------*/
  1496. static void
  1497. composite_suspend(struct usb_gadget *gadget)
  1498. {
  1499. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1500. struct usb_function *f;
  1501. /* REVISIT: should we have config level
  1502. * suspend/resume callbacks?
  1503. */
  1504. DBG(cdev, "suspend\n");
  1505. if (cdev->config) {
  1506. list_for_each_entry(f, &cdev->config->functions, list) {
  1507. if (f->suspend)
  1508. f->suspend(f);
  1509. }
  1510. }
  1511. if (cdev->driver->suspend)
  1512. cdev->driver->suspend(cdev);
  1513. cdev->suspended = 1;
  1514. usb_gadget_vbus_draw(gadget, 2);
  1515. }
  1516. static void
  1517. composite_resume(struct usb_gadget *gadget)
  1518. {
  1519. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1520. struct usb_function *f;
  1521. u8 maxpower;
  1522. /* REVISIT: should we have config level
  1523. * suspend/resume callbacks?
  1524. */
  1525. DBG(cdev, "resume\n");
  1526. if (cdev->driver->resume)
  1527. cdev->driver->resume(cdev);
  1528. if (cdev->config) {
  1529. list_for_each_entry(f, &cdev->config->functions, list) {
  1530. if (f->resume)
  1531. f->resume(f);
  1532. }
  1533. maxpower = cdev->config->MaxPower;
  1534. usb_gadget_vbus_draw(gadget, maxpower ?
  1535. maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
  1536. }
  1537. cdev->suspended = 0;
  1538. }
  1539. /*-------------------------------------------------------------------------*/
  1540. static const struct usb_gadget_driver composite_driver_template = {
  1541. .bind = composite_bind,
  1542. .unbind = composite_unbind,
  1543. .setup = composite_setup,
  1544. .disconnect = composite_disconnect,
  1545. .suspend = composite_suspend,
  1546. .resume = composite_resume,
  1547. .driver = {
  1548. .owner = THIS_MODULE,
  1549. },
  1550. };
  1551. /**
  1552. * usb_composite_probe() - register a composite driver
  1553. * @driver: the driver to register
  1554. *
  1555. * Context: single threaded during gadget setup
  1556. *
  1557. * This function is used to register drivers using the composite driver
  1558. * framework. The return value is zero, or a negative errno value.
  1559. * Those values normally come from the driver's @bind method, which does
  1560. * all the work of setting up the driver to match the hardware.
  1561. *
  1562. * On successful return, the gadget is ready to respond to requests from
  1563. * the host, unless one of its components invokes usb_gadget_disconnect()
  1564. * while it was binding. That would usually be done in order to wait for
  1565. * some userspace participation.
  1566. */
  1567. int usb_composite_probe(struct usb_composite_driver *driver)
  1568. {
  1569. struct usb_gadget_driver *gadget_driver;
  1570. if (!driver || !driver->dev || !driver->bind)
  1571. return -EINVAL;
  1572. if (!driver->name)
  1573. driver->name = "composite";
  1574. driver->gadget_driver = composite_driver_template;
  1575. gadget_driver = &driver->gadget_driver;
  1576. gadget_driver->function = (char *) driver->name;
  1577. gadget_driver->driver.name = driver->name;
  1578. gadget_driver->max_speed = driver->max_speed;
  1579. return usb_gadget_probe_driver(gadget_driver);
  1580. }
  1581. EXPORT_SYMBOL_GPL(usb_composite_probe);
  1582. /**
  1583. * usb_composite_unregister() - unregister a composite driver
  1584. * @driver: the driver to unregister
  1585. *
  1586. * This function is used to unregister drivers using the composite
  1587. * driver framework.
  1588. */
  1589. void usb_composite_unregister(struct usb_composite_driver *driver)
  1590. {
  1591. usb_gadget_unregister_driver(&driver->gadget_driver);
  1592. }
  1593. EXPORT_SYMBOL_GPL(usb_composite_unregister);
  1594. /**
  1595. * usb_composite_setup_continue() - Continue with the control transfer
  1596. * @cdev: the composite device who's control transfer was kept waiting
  1597. *
  1598. * This function must be called by the USB function driver to continue
  1599. * with the control transfer's data/status stage in case it had requested to
  1600. * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
  1601. * can request the composite framework to delay the setup request's data/status
  1602. * stages by returning USB_GADGET_DELAYED_STATUS.
  1603. */
  1604. void usb_composite_setup_continue(struct usb_composite_dev *cdev)
  1605. {
  1606. int value;
  1607. struct usb_request *req = cdev->req;
  1608. unsigned long flags;
  1609. DBG(cdev, "%s\n", __func__);
  1610. spin_lock_irqsave(&cdev->lock, flags);
  1611. if (cdev->delayed_status == 0) {
  1612. WARN(cdev, "%s: Unexpected call\n", __func__);
  1613. } else if (--cdev->delayed_status == 0) {
  1614. DBG(cdev, "%s: Completing delayed status\n", __func__);
  1615. req->length = 0;
  1616. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  1617. if (value < 0) {
  1618. DBG(cdev, "ep_queue --> %d\n", value);
  1619. req->status = 0;
  1620. composite_setup_complete(cdev->gadget->ep0, req);
  1621. }
  1622. }
  1623. spin_unlock_irqrestore(&cdev->lock, flags);
  1624. }
  1625. EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
  1626. static char *composite_default_mfr(struct usb_gadget *gadget)
  1627. {
  1628. char *mfr;
  1629. int len;
  1630. len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
  1631. init_utsname()->release, gadget->name);
  1632. len++;
  1633. mfr = kmalloc(len, GFP_KERNEL);
  1634. if (!mfr)
  1635. return NULL;
  1636. snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
  1637. init_utsname()->release, gadget->name);
  1638. return mfr;
  1639. }
  1640. void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
  1641. struct usb_composite_overwrite *covr)
  1642. {
  1643. struct usb_device_descriptor *desc = &cdev->desc;
  1644. struct usb_gadget_strings *gstr = cdev->driver->strings[0];
  1645. struct usb_string *dev_str = gstr->strings;
  1646. if (covr->idVendor)
  1647. desc->idVendor = cpu_to_le16(covr->idVendor);
  1648. if (covr->idProduct)
  1649. desc->idProduct = cpu_to_le16(covr->idProduct);
  1650. if (covr->bcdDevice)
  1651. desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
  1652. if (covr->serial_number) {
  1653. desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
  1654. dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
  1655. }
  1656. if (covr->manufacturer) {
  1657. desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
  1658. dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
  1659. } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
  1660. desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
  1661. cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
  1662. dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
  1663. }
  1664. if (covr->product) {
  1665. desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
  1666. dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
  1667. }
  1668. }
  1669. EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
  1670. MODULE_LICENSE("GPL");
  1671. MODULE_AUTHOR("David Brownell");