composite.c 76 KB

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