composite.c 69 KB

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