composite.c 63 KB

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