composite.c 64 KB

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