composite.c 64 KB

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