composite.c 64 KB

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