composite.c 64 KB

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