composite.c 61 KB

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