composite.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * composite.c - infrastructure for Composite USB Gadgets
  4. *
  5. * Copyright (C) 2006-2008 David Brownell
  6. */
  7. /* #define VERBOSE_DEBUG */
  8. #include <linux/kallsyms.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/device.h>
  13. #include <linux/utsname.h>
  14. #include <linux/usb/composite.h>
  15. #include <linux/usb/otg.h>
  16. #include <asm/unaligned.h>
  17. #include "u_os_desc.h"
  18. /**
  19. * struct usb_os_string - represents OS String to be reported by a gadget
  20. * @bLength: total length of the entire descritor, always 0x12
  21. * @bDescriptorType: USB_DT_STRING
  22. * @qwSignature: the OS String proper
  23. * @bMS_VendorCode: code used by the host for subsequent requests
  24. * @bPad: not used, must be zero
  25. */
  26. struct usb_os_string {
  27. __u8 bLength;
  28. __u8 bDescriptorType;
  29. __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
  30. __u8 bMS_VendorCode;
  31. __u8 bPad;
  32. } __packed;
  33. /*
  34. * The code in this file is utility code, used to build a gadget driver
  35. * from one or more "function" drivers, one or more "configuration"
  36. * objects, and a "usb_composite_driver" by gluing them together along
  37. * with the relevant device-wide data.
  38. */
  39. static struct usb_gadget_strings **get_containers_gs(
  40. struct usb_gadget_string_container *uc)
  41. {
  42. return (struct usb_gadget_strings **)uc->stash;
  43. }
  44. /**
  45. * function_descriptors() - get function descriptors for speed
  46. * @f: the function
  47. * @speed: the speed
  48. *
  49. * Returns the descriptors or NULL if not set.
  50. */
  51. static struct usb_descriptor_header **
  52. function_descriptors(struct usb_function *f,
  53. enum usb_device_speed speed)
  54. {
  55. struct usb_descriptor_header **descriptors;
  56. /*
  57. * NOTE: we try to help gadget drivers which might not be setting
  58. * max_speed appropriately.
  59. */
  60. switch (speed) {
  61. case USB_SPEED_SUPER_PLUS:
  62. descriptors = f->ssp_descriptors;
  63. if (descriptors)
  64. break;
  65. /* FALLTHROUGH */
  66. case USB_SPEED_SUPER:
  67. descriptors = f->ss_descriptors;
  68. if (descriptors)
  69. break;
  70. /* FALLTHROUGH */
  71. case USB_SPEED_HIGH:
  72. descriptors = f->hs_descriptors;
  73. if (descriptors)
  74. break;
  75. /* FALLTHROUGH */
  76. default:
  77. descriptors = f->fs_descriptors;
  78. }
  79. /*
  80. * if we can't find any descriptors at all, then this gadget deserves to
  81. * Oops with a NULL pointer dereference
  82. */
  83. return descriptors;
  84. }
  85. /**
  86. * next_ep_desc() - advance to the next EP descriptor
  87. * @t: currect pointer within descriptor array
  88. *
  89. * Return: next EP descriptor or NULL
  90. *
  91. * Iterate over @t until either EP descriptor found or
  92. * NULL (that indicates end of list) encountered
  93. */
  94. static struct usb_descriptor_header**
  95. next_ep_desc(struct usb_descriptor_header **t)
  96. {
  97. for (; *t; t++) {
  98. if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
  99. return t;
  100. }
  101. return NULL;
  102. }
  103. /*
  104. * for_each_ep_desc()- iterate over endpoint descriptors in the
  105. * descriptors list
  106. * @start: pointer within descriptor array.
  107. * @ep_desc: endpoint descriptor to use as the loop cursor
  108. */
  109. #define for_each_ep_desc(start, ep_desc) \
  110. for (ep_desc = next_ep_desc(start); \
  111. ep_desc; ep_desc = next_ep_desc(ep_desc+1))
  112. /**
  113. * config_ep_by_speed() - configures the given endpoint
  114. * according to gadget speed.
  115. * @g: pointer to the gadget
  116. * @f: usb function
  117. * @_ep: the endpoint to configure
  118. *
  119. * Return: error code, 0 on success
  120. *
  121. * This function chooses the right descriptors for a given
  122. * endpoint according to gadget speed and saves it in the
  123. * endpoint desc field. If the endpoint already has a descriptor
  124. * assigned to it - overwrites it with currently corresponding
  125. * descriptor. The endpoint maxpacket field is updated according
  126. * to the chosen descriptor.
  127. * Note: the supplied function should hold all the descriptors
  128. * for supported speeds
  129. */
  130. int config_ep_by_speed(struct usb_gadget *g,
  131. struct usb_function *f,
  132. struct usb_ep *_ep)
  133. {
  134. struct usb_endpoint_descriptor *chosen_desc = NULL;
  135. struct usb_descriptor_header **speed_desc = NULL;
  136. struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
  137. int want_comp_desc = 0;
  138. struct usb_descriptor_header **d_spd; /* cursor for speed desc */
  139. if (!g || !f || !_ep)
  140. return -EIO;
  141. /* select desired speed */
  142. switch (g->speed) {
  143. case USB_SPEED_SUPER_PLUS:
  144. if (gadget_is_superspeed_plus(g)) {
  145. speed_desc = f->ssp_descriptors;
  146. want_comp_desc = 1;
  147. break;
  148. }
  149. /* fall through */
  150. case USB_SPEED_SUPER:
  151. if (gadget_is_superspeed(g)) {
  152. speed_desc = f->ss_descriptors;
  153. want_comp_desc = 1;
  154. break;
  155. }
  156. /* fall through */
  157. case USB_SPEED_HIGH:
  158. if (gadget_is_dualspeed(g)) {
  159. speed_desc = f->hs_descriptors;
  160. break;
  161. }
  162. /* fall through */
  163. default:
  164. speed_desc = f->fs_descriptors;
  165. }
  166. /* find descriptors */
  167. for_each_ep_desc(speed_desc, d_spd) {
  168. chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
  169. if (chosen_desc->bEndpointAddress == _ep->address)
  170. goto ep_found;
  171. }
  172. return -EIO;
  173. ep_found:
  174. /* commit results */
  175. _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
  176. _ep->desc = chosen_desc;
  177. _ep->comp_desc = NULL;
  178. _ep->maxburst = 0;
  179. _ep->mult = 1;
  180. if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) ||
  181. usb_endpoint_xfer_int(_ep->desc)))
  182. _ep->mult = usb_endpoint_maxp_mult(_ep->desc);
  183. if (!want_comp_desc)
  184. return 0;
  185. /*
  186. * Companion descriptor should follow EP descriptor
  187. * USB 3.0 spec, #9.6.7
  188. */
  189. comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
  190. if (!comp_desc ||
  191. (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
  192. return -EIO;
  193. _ep->comp_desc = comp_desc;
  194. if (g->speed >= USB_SPEED_SUPER) {
  195. switch (usb_endpoint_type(_ep->desc)) {
  196. case USB_ENDPOINT_XFER_ISOC:
  197. /* mult: bits 1:0 of bmAttributes */
  198. _ep->mult = (comp_desc->bmAttributes & 0x3) + 1;
  199. /* fall through */
  200. case USB_ENDPOINT_XFER_BULK:
  201. case USB_ENDPOINT_XFER_INT:
  202. _ep->maxburst = comp_desc->bMaxBurst + 1;
  203. break;
  204. default:
  205. if (comp_desc->bMaxBurst != 0) {
  206. struct usb_composite_dev *cdev;
  207. cdev = get_gadget_data(g);
  208. ERROR(cdev, "ep0 bMaxBurst must be 0\n");
  209. }
  210. _ep->maxburst = 1;
  211. break;
  212. }
  213. }
  214. return 0;
  215. }
  216. EXPORT_SYMBOL_GPL(config_ep_by_speed);
  217. /**
  218. * usb_add_function() - add a function to a configuration
  219. * @config: the configuration
  220. * @function: the function being added
  221. * Context: single threaded during gadget setup
  222. *
  223. * After initialization, each configuration must have one or more
  224. * functions added to it. Adding a function involves calling its @bind()
  225. * method to allocate resources such as interface and string identifiers
  226. * and endpoints.
  227. *
  228. * This function returns the value of the function's bind(), which is
  229. * zero for success else a negative errno value.
  230. */
  231. int usb_add_function(struct usb_configuration *config,
  232. struct usb_function *function)
  233. {
  234. int value = -EINVAL;
  235. DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
  236. function->name, function,
  237. config->label, config);
  238. if (!function->set_alt || !function->disable)
  239. goto done;
  240. function->config = config;
  241. list_add_tail(&function->list, &config->functions);
  242. if (function->bind_deactivated) {
  243. value = usb_function_deactivate(function);
  244. if (value)
  245. goto done;
  246. }
  247. /* REVISIT *require* function->bind? */
  248. if (function->bind) {
  249. value = function->bind(config, function);
  250. if (value < 0) {
  251. list_del(&function->list);
  252. function->config = NULL;
  253. }
  254. } else
  255. value = 0;
  256. /* We allow configurations that don't work at both speeds.
  257. * If we run into a lowspeed Linux system, treat it the same
  258. * as full speed ... it's the function drivers that will need
  259. * to avoid bulk and ISO transfers.
  260. */
  261. if (!config->fullspeed && function->fs_descriptors)
  262. config->fullspeed = true;
  263. if (!config->highspeed && function->hs_descriptors)
  264. config->highspeed = true;
  265. if (!config->superspeed && function->ss_descriptors)
  266. config->superspeed = true;
  267. if (!config->superspeed_plus && function->ssp_descriptors)
  268. config->superspeed_plus = true;
  269. done:
  270. if (value)
  271. DBG(config->cdev, "adding '%s'/%p --> %d\n",
  272. function->name, function, value);
  273. return value;
  274. }
  275. EXPORT_SYMBOL_GPL(usb_add_function);
  276. void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
  277. {
  278. if (f->disable)
  279. f->disable(f);
  280. bitmap_zero(f->endpoints, 32);
  281. list_del(&f->list);
  282. if (f->unbind)
  283. f->unbind(c, f);
  284. if (f->bind_deactivated)
  285. usb_function_activate(f);
  286. }
  287. EXPORT_SYMBOL_GPL(usb_remove_function);
  288. /**
  289. * usb_function_deactivate - prevent function and gadget enumeration
  290. * @function: the function that isn't yet ready to respond
  291. *
  292. * Blocks response of the gadget driver to host enumeration by
  293. * preventing the data line pullup from being activated. This is
  294. * normally called during @bind() processing to change from the
  295. * initial "ready to respond" state, or when a required resource
  296. * becomes available.
  297. *
  298. * For example, drivers that serve as a passthrough to a userspace
  299. * daemon can block enumeration unless that daemon (such as an OBEX,
  300. * MTP, or print server) is ready to handle host requests.
  301. *
  302. * Not all systems support software control of their USB peripheral
  303. * data pullups.
  304. *
  305. * Returns zero on success, else negative errno.
  306. */
  307. int usb_function_deactivate(struct usb_function *function)
  308. {
  309. struct usb_composite_dev *cdev = function->config->cdev;
  310. unsigned long flags;
  311. int status = 0;
  312. spin_lock_irqsave(&cdev->lock, flags);
  313. if (cdev->deactivations == 0)
  314. status = usb_gadget_deactivate(cdev->gadget);
  315. if (status == 0)
  316. cdev->deactivations++;
  317. spin_unlock_irqrestore(&cdev->lock, flags);
  318. return status;
  319. }
  320. EXPORT_SYMBOL_GPL(usb_function_deactivate);
  321. /**
  322. * usb_function_activate - allow function and gadget enumeration
  323. * @function: function on which usb_function_activate() was called
  324. *
  325. * Reverses effect of usb_function_deactivate(). If no more functions
  326. * are delaying their activation, the gadget driver will respond to
  327. * host enumeration procedures.
  328. *
  329. * Returns zero on success, else negative errno.
  330. */
  331. int usb_function_activate(struct usb_function *function)
  332. {
  333. struct usb_composite_dev *cdev = function->config->cdev;
  334. unsigned long flags;
  335. int status = 0;
  336. spin_lock_irqsave(&cdev->lock, flags);
  337. if (WARN_ON(cdev->deactivations == 0))
  338. status = -EINVAL;
  339. else {
  340. cdev->deactivations--;
  341. if (cdev->deactivations == 0)
  342. status = usb_gadget_activate(cdev->gadget);
  343. }
  344. spin_unlock_irqrestore(&cdev->lock, flags);
  345. return status;
  346. }
  347. EXPORT_SYMBOL_GPL(usb_function_activate);
  348. /**
  349. * usb_interface_id() - allocate an unused interface ID
  350. * @config: configuration associated with the interface
  351. * @function: function handling the interface
  352. * Context: single threaded during gadget setup
  353. *
  354. * usb_interface_id() is called from usb_function.bind() callbacks to
  355. * allocate new interface IDs. The function driver will then store that
  356. * ID in interface, association, CDC union, and other descriptors. It
  357. * will also handle any control requests targeted at that interface,
  358. * particularly changing its altsetting via set_alt(). There may
  359. * also be class-specific or vendor-specific requests to handle.
  360. *
  361. * All interface identifier should be allocated using this routine, to
  362. * ensure that for example different functions don't wrongly assign
  363. * different meanings to the same identifier. Note that since interface
  364. * identifiers are configuration-specific, functions used in more than
  365. * one configuration (or more than once in a given configuration) need
  366. * multiple versions of the relevant descriptors.
  367. *
  368. * Returns the interface ID which was allocated; or -ENODEV if no
  369. * more interface IDs can be allocated.
  370. */
  371. int usb_interface_id(struct usb_configuration *config,
  372. struct usb_function *function)
  373. {
  374. unsigned id = config->next_interface_id;
  375. if (id < MAX_CONFIG_INTERFACES) {
  376. config->interface[id] = function;
  377. config->next_interface_id = id + 1;
  378. return id;
  379. }
  380. return -ENODEV;
  381. }
  382. EXPORT_SYMBOL_GPL(usb_interface_id);
  383. static u8 encode_bMaxPower(enum usb_device_speed speed,
  384. struct usb_configuration *c)
  385. {
  386. unsigned val;
  387. if (c->MaxPower)
  388. val = c->MaxPower;
  389. else
  390. val = CONFIG_USB_GADGET_VBUS_DRAW;
  391. if (!val)
  392. return 0;
  393. switch (speed) {
  394. case USB_SPEED_SUPER:
  395. return DIV_ROUND_UP(val, 8);
  396. default:
  397. return DIV_ROUND_UP(val, 2);
  398. }
  399. }
  400. static int config_buf(struct usb_configuration *config,
  401. enum usb_device_speed speed, void *buf, u8 type)
  402. {
  403. struct usb_config_descriptor *c = buf;
  404. void *next = buf + USB_DT_CONFIG_SIZE;
  405. int len;
  406. struct usb_function *f;
  407. int status;
  408. len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
  409. /* write the config descriptor */
  410. c = buf;
  411. c->bLength = USB_DT_CONFIG_SIZE;
  412. c->bDescriptorType = type;
  413. /* wTotalLength is written later */
  414. c->bNumInterfaces = config->next_interface_id;
  415. c->bConfigurationValue = config->bConfigurationValue;
  416. c->iConfiguration = config->iConfiguration;
  417. c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
  418. c->bMaxPower = encode_bMaxPower(speed, config);
  419. /* There may be e.g. OTG descriptors */
  420. if (config->descriptors) {
  421. status = usb_descriptor_fillbuf(next, len,
  422. config->descriptors);
  423. if (status < 0)
  424. return status;
  425. len -= status;
  426. next += status;
  427. }
  428. /* add each function's descriptors */
  429. list_for_each_entry(f, &config->functions, list) {
  430. struct usb_descriptor_header **descriptors;
  431. descriptors = function_descriptors(f, speed);
  432. if (!descriptors)
  433. continue;
  434. status = usb_descriptor_fillbuf(next, len,
  435. (const struct usb_descriptor_header **) descriptors);
  436. if (status < 0)
  437. return status;
  438. len -= status;
  439. next += status;
  440. }
  441. len = next - buf;
  442. c->wTotalLength = cpu_to_le16(len);
  443. return len;
  444. }
  445. static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
  446. {
  447. struct usb_gadget *gadget = cdev->gadget;
  448. struct usb_configuration *c;
  449. struct list_head *pos;
  450. u8 type = w_value >> 8;
  451. enum usb_device_speed speed = USB_SPEED_UNKNOWN;
  452. if (gadget->speed >= USB_SPEED_SUPER)
  453. speed = gadget->speed;
  454. else if (gadget_is_dualspeed(gadget)) {
  455. int hs = 0;
  456. if (gadget->speed == USB_SPEED_HIGH)
  457. hs = 1;
  458. if (type == USB_DT_OTHER_SPEED_CONFIG)
  459. hs = !hs;
  460. if (hs)
  461. speed = USB_SPEED_HIGH;
  462. }
  463. /* This is a lookup by config *INDEX* */
  464. w_value &= 0xff;
  465. pos = &cdev->configs;
  466. c = cdev->os_desc_config;
  467. if (c)
  468. goto check_config;
  469. while ((pos = pos->next) != &cdev->configs) {
  470. c = list_entry(pos, typeof(*c), list);
  471. /* skip OS Descriptors config which is handled separately */
  472. if (c == cdev->os_desc_config)
  473. continue;
  474. check_config:
  475. /* ignore configs that won't work at this speed */
  476. switch (speed) {
  477. case USB_SPEED_SUPER_PLUS:
  478. if (!c->superspeed_plus)
  479. continue;
  480. break;
  481. case USB_SPEED_SUPER:
  482. if (!c->superspeed)
  483. continue;
  484. break;
  485. case USB_SPEED_HIGH:
  486. if (!c->highspeed)
  487. continue;
  488. break;
  489. default:
  490. if (!c->fullspeed)
  491. continue;
  492. }
  493. if (w_value == 0)
  494. return config_buf(c, speed, cdev->req->buf, type);
  495. w_value--;
  496. }
  497. return -EINVAL;
  498. }
  499. static int count_configs(struct usb_composite_dev *cdev, unsigned type)
  500. {
  501. struct usb_gadget *gadget = cdev->gadget;
  502. struct usb_configuration *c;
  503. unsigned count = 0;
  504. int hs = 0;
  505. int ss = 0;
  506. int ssp = 0;
  507. if (gadget_is_dualspeed(gadget)) {
  508. if (gadget->speed == USB_SPEED_HIGH)
  509. hs = 1;
  510. if (gadget->speed == USB_SPEED_SUPER)
  511. ss = 1;
  512. if (gadget->speed == USB_SPEED_SUPER_PLUS)
  513. ssp = 1;
  514. if (type == USB_DT_DEVICE_QUALIFIER)
  515. hs = !hs;
  516. }
  517. list_for_each_entry(c, &cdev->configs, list) {
  518. /* ignore configs that won't work at this speed */
  519. if (ssp) {
  520. if (!c->superspeed_plus)
  521. continue;
  522. } else if (ss) {
  523. if (!c->superspeed)
  524. continue;
  525. } else if (hs) {
  526. if (!c->highspeed)
  527. continue;
  528. } else {
  529. if (!c->fullspeed)
  530. continue;
  531. }
  532. count++;
  533. }
  534. return count;
  535. }
  536. /**
  537. * bos_desc() - prepares the BOS descriptor.
  538. * @cdev: pointer to usb_composite device to generate the bos
  539. * descriptor for
  540. *
  541. * This function generates the BOS (Binary Device Object)
  542. * descriptor and its device capabilities descriptors. The BOS
  543. * descriptor should be supported by a SuperSpeed device.
  544. */
  545. static int bos_desc(struct usb_composite_dev *cdev)
  546. {
  547. struct usb_ext_cap_descriptor *usb_ext;
  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. if (gadget_is_superspeed(cdev->gadget)) {
  570. struct usb_ss_cap_descriptor *ss_cap;
  571. ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  572. bos->bNumDeviceCaps++;
  573. le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
  574. ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
  575. ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  576. ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
  577. ss_cap->bmAttributes = 0; /* LTM is not supported yet */
  578. ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
  579. USB_FULL_SPEED_OPERATION |
  580. USB_HIGH_SPEED_OPERATION |
  581. USB_5GBPS_OPERATION);
  582. ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
  583. /* Get Controller configuration */
  584. if (cdev->gadget->ops->get_config_params) {
  585. cdev->gadget->ops->get_config_params(
  586. &dcd_config_params);
  587. } else {
  588. dcd_config_params.bU1devExitLat =
  589. USB_DEFAULT_U1_DEV_EXIT_LAT;
  590. dcd_config_params.bU2DevExitLat =
  591. cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
  592. }
  593. ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
  594. ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
  595. }
  596. /* The SuperSpeedPlus USB Device Capability descriptor */
  597. if (gadget_is_superspeed_plus(cdev->gadget)) {
  598. struct usb_ssp_cap_descriptor *ssp_cap;
  599. ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  600. bos->bNumDeviceCaps++;
  601. /*
  602. * Report typical values.
  603. */
  604. le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(1));
  605. ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(1);
  606. ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  607. ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
  608. ssp_cap->bReserved = 0;
  609. ssp_cap->wReserved = 0;
  610. /* SSAC = 1 (2 attributes) */
  611. ssp_cap->bmAttributes = cpu_to_le32(1);
  612. /* Min RX/TX Lane Count = 1 */
  613. ssp_cap->wFunctionalitySupport =
  614. cpu_to_le16((1 << 8) | (1 << 12));
  615. /*
  616. * bmSublinkSpeedAttr[0]:
  617. * ST = Symmetric, RX
  618. * LSE = 3 (Gbps)
  619. * LP = 1 (SuperSpeedPlus)
  620. * LSM = 10 (10 Gbps)
  621. */
  622. ssp_cap->bmSublinkSpeedAttr[0] =
  623. cpu_to_le32((3 << 4) | (1 << 14) | (0xa << 16));
  624. /*
  625. * bmSublinkSpeedAttr[1] =
  626. * ST = Symmetric, TX
  627. * LSE = 3 (Gbps)
  628. * LP = 1 (SuperSpeedPlus)
  629. * LSM = 10 (10 Gbps)
  630. */
  631. ssp_cap->bmSublinkSpeedAttr[1] =
  632. cpu_to_le32((3 << 4) | (1 << 14) |
  633. (0xa << 16) | (1 << 7));
  634. }
  635. return le16_to_cpu(bos->wTotalLength);
  636. }
  637. static void device_qual(struct usb_composite_dev *cdev)
  638. {
  639. struct usb_qualifier_descriptor *qual = cdev->req->buf;
  640. qual->bLength = sizeof(*qual);
  641. qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  642. /* POLICY: same bcdUSB and device type info at both speeds */
  643. qual->bcdUSB = cdev->desc.bcdUSB;
  644. qual->bDeviceClass = cdev->desc.bDeviceClass;
  645. qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
  646. qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
  647. /* ASSUME same EP0 fifo size at both speeds */
  648. qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
  649. qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
  650. qual->bRESERVED = 0;
  651. }
  652. /*-------------------------------------------------------------------------*/
  653. static void reset_config(struct usb_composite_dev *cdev)
  654. {
  655. struct usb_function *f;
  656. DBG(cdev, "reset config\n");
  657. list_for_each_entry(f, &cdev->config->functions, list) {
  658. if (f->disable)
  659. f->disable(f);
  660. bitmap_zero(f->endpoints, 32);
  661. }
  662. cdev->config = NULL;
  663. cdev->delayed_status = 0;
  664. }
  665. static int set_config(struct usb_composite_dev *cdev,
  666. const struct usb_ctrlrequest *ctrl, unsigned number)
  667. {
  668. struct usb_gadget *gadget = cdev->gadget;
  669. struct usb_configuration *c = NULL;
  670. int result = -EINVAL;
  671. unsigned power = gadget_is_otg(gadget) ? 8 : 100;
  672. int tmp;
  673. if (number) {
  674. list_for_each_entry(c, &cdev->configs, list) {
  675. if (c->bConfigurationValue == number) {
  676. /*
  677. * We disable the FDs of the previous
  678. * configuration only if the new configuration
  679. * is a valid one
  680. */
  681. if (cdev->config)
  682. reset_config(cdev);
  683. result = 0;
  684. break;
  685. }
  686. }
  687. if (result < 0)
  688. goto done;
  689. } else { /* Zero configuration value - need to reset the config */
  690. if (cdev->config)
  691. reset_config(cdev);
  692. result = 0;
  693. }
  694. INFO(cdev, "%s config #%d: %s\n",
  695. usb_speed_string(gadget->speed),
  696. number, c ? c->label : "unconfigured");
  697. if (!c)
  698. goto done;
  699. usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
  700. cdev->config = c;
  701. /* Initialize all interfaces by setting them to altsetting zero. */
  702. for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
  703. struct usb_function *f = c->interface[tmp];
  704. struct usb_descriptor_header **descriptors;
  705. if (!f)
  706. break;
  707. /*
  708. * Record which endpoints are used by the function. This is used
  709. * to dispatch control requests targeted at that endpoint to the
  710. * function's setup callback instead of the current
  711. * configuration's setup callback.
  712. */
  713. descriptors = function_descriptors(f, gadget->speed);
  714. for (; *descriptors; ++descriptors) {
  715. struct usb_endpoint_descriptor *ep;
  716. int addr;
  717. if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
  718. continue;
  719. ep = (struct usb_endpoint_descriptor *)*descriptors;
  720. addr = ((ep->bEndpointAddress & 0x80) >> 3)
  721. | (ep->bEndpointAddress & 0x0f);
  722. set_bit(addr, f->endpoints);
  723. }
  724. result = f->set_alt(f, tmp, 0);
  725. if (result < 0) {
  726. DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
  727. tmp, f->name, f, result);
  728. reset_config(cdev);
  729. goto done;
  730. }
  731. if (result == USB_GADGET_DELAYED_STATUS) {
  732. DBG(cdev,
  733. "%s: interface %d (%s) requested delayed status\n",
  734. __func__, tmp, f->name);
  735. cdev->delayed_status++;
  736. DBG(cdev, "delayed_status count %d\n",
  737. cdev->delayed_status);
  738. }
  739. }
  740. /* when we return, be sure our power usage is valid */
  741. power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
  742. done:
  743. usb_gadget_vbus_draw(gadget, power);
  744. if (result >= 0 && cdev->delayed_status)
  745. result = USB_GADGET_DELAYED_STATUS;
  746. return result;
  747. }
  748. int usb_add_config_only(struct usb_composite_dev *cdev,
  749. struct usb_configuration *config)
  750. {
  751. struct usb_configuration *c;
  752. if (!config->bConfigurationValue)
  753. return -EINVAL;
  754. /* Prevent duplicate configuration identifiers */
  755. list_for_each_entry(c, &cdev->configs, list) {
  756. if (c->bConfigurationValue == config->bConfigurationValue)
  757. return -EBUSY;
  758. }
  759. config->cdev = cdev;
  760. list_add_tail(&config->list, &cdev->configs);
  761. INIT_LIST_HEAD(&config->functions);
  762. config->next_interface_id = 0;
  763. memset(config->interface, 0, sizeof(config->interface));
  764. return 0;
  765. }
  766. EXPORT_SYMBOL_GPL(usb_add_config_only);
  767. /**
  768. * usb_add_config() - add a configuration to a device.
  769. * @cdev: wraps the USB gadget
  770. * @config: the configuration, with bConfigurationValue assigned
  771. * @bind: the configuration's bind function
  772. * Context: single threaded during gadget setup
  773. *
  774. * One of the main tasks of a composite @bind() routine is to
  775. * add each of the configurations it supports, using this routine.
  776. *
  777. * This function returns the value of the configuration's @bind(), which
  778. * is zero for success else a negative errno value. Binding configurations
  779. * assigns global resources including string IDs, and per-configuration
  780. * resources such as interface IDs and endpoints.
  781. */
  782. int usb_add_config(struct usb_composite_dev *cdev,
  783. struct usb_configuration *config,
  784. int (*bind)(struct usb_configuration *))
  785. {
  786. int status = -EINVAL;
  787. if (!bind)
  788. goto done;
  789. DBG(cdev, "adding config #%u '%s'/%p\n",
  790. config->bConfigurationValue,
  791. config->label, config);
  792. status = usb_add_config_only(cdev, config);
  793. if (status)
  794. goto done;
  795. status = bind(config);
  796. if (status < 0) {
  797. while (!list_empty(&config->functions)) {
  798. struct usb_function *f;
  799. f = list_first_entry(&config->functions,
  800. struct usb_function, list);
  801. list_del(&f->list);
  802. if (f->unbind) {
  803. DBG(cdev, "unbind function '%s'/%p\n",
  804. f->name, f);
  805. f->unbind(config, f);
  806. /* may free memory for "f" */
  807. }
  808. }
  809. list_del(&config->list);
  810. config->cdev = NULL;
  811. } else {
  812. unsigned i;
  813. DBG(cdev, "cfg %d/%p speeds:%s%s%s%s\n",
  814. config->bConfigurationValue, config,
  815. config->superspeed_plus ? " superplus" : "",
  816. config->superspeed ? " super" : "",
  817. config->highspeed ? " high" : "",
  818. config->fullspeed
  819. ? (gadget_is_dualspeed(cdev->gadget)
  820. ? " full"
  821. : " full/low")
  822. : "");
  823. for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
  824. struct usb_function *f = config->interface[i];
  825. if (!f)
  826. continue;
  827. DBG(cdev, " interface %d = %s/%p\n",
  828. i, f->name, f);
  829. }
  830. }
  831. /* set_alt(), or next bind(), sets up ep->claimed as needed */
  832. usb_ep_autoconfig_reset(cdev->gadget);
  833. done:
  834. if (status)
  835. DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
  836. config->bConfigurationValue, status);
  837. return status;
  838. }
  839. EXPORT_SYMBOL_GPL(usb_add_config);
  840. static void remove_config(struct usb_composite_dev *cdev,
  841. struct usb_configuration *config)
  842. {
  843. while (!list_empty(&config->functions)) {
  844. struct usb_function *f;
  845. f = list_first_entry(&config->functions,
  846. struct usb_function, list);
  847. usb_remove_function(config, f);
  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. if (gadget->lpm_capable)
  1415. cdev->desc.bcdUSB = cpu_to_le16(0x0201);
  1416. else
  1417. cdev->desc.bcdUSB = cpu_to_le16(0x0200);
  1418. }
  1419. value = min(w_length, (u16) sizeof cdev->desc);
  1420. memcpy(req->buf, &cdev->desc, value);
  1421. break;
  1422. case USB_DT_DEVICE_QUALIFIER:
  1423. if (!gadget_is_dualspeed(gadget) ||
  1424. gadget->speed >= USB_SPEED_SUPER)
  1425. break;
  1426. device_qual(cdev);
  1427. value = min_t(int, w_length,
  1428. sizeof(struct usb_qualifier_descriptor));
  1429. break;
  1430. case USB_DT_OTHER_SPEED_CONFIG:
  1431. if (!gadget_is_dualspeed(gadget) ||
  1432. gadget->speed >= USB_SPEED_SUPER)
  1433. break;
  1434. /* FALLTHROUGH */
  1435. case USB_DT_CONFIG:
  1436. value = config_desc(cdev, w_value);
  1437. if (value >= 0)
  1438. value = min(w_length, (u16) value);
  1439. break;
  1440. case USB_DT_STRING:
  1441. value = get_string(cdev, req->buf,
  1442. w_index, w_value & 0xff);
  1443. if (value >= 0)
  1444. value = min(w_length, (u16) value);
  1445. break;
  1446. case USB_DT_BOS:
  1447. if (gadget_is_superspeed(gadget) ||
  1448. gadget->lpm_capable) {
  1449. value = bos_desc(cdev);
  1450. value = min(w_length, (u16) value);
  1451. }
  1452. break;
  1453. case USB_DT_OTG:
  1454. if (gadget_is_otg(gadget)) {
  1455. struct usb_configuration *config;
  1456. int otg_desc_len = 0;
  1457. if (cdev->config)
  1458. config = cdev->config;
  1459. else
  1460. config = list_first_entry(
  1461. &cdev->configs,
  1462. struct usb_configuration, list);
  1463. if (!config)
  1464. goto done;
  1465. if (gadget->otg_caps &&
  1466. (gadget->otg_caps->otg_rev >= 0x0200))
  1467. otg_desc_len += sizeof(
  1468. struct usb_otg20_descriptor);
  1469. else
  1470. otg_desc_len += sizeof(
  1471. struct usb_otg_descriptor);
  1472. value = min_t(int, w_length, otg_desc_len);
  1473. memcpy(req->buf, config->descriptors[0], value);
  1474. }
  1475. break;
  1476. }
  1477. break;
  1478. /* any number of configs can work */
  1479. case USB_REQ_SET_CONFIGURATION:
  1480. if (ctrl->bRequestType != 0)
  1481. goto unknown;
  1482. if (gadget_is_otg(gadget)) {
  1483. if (gadget->a_hnp_support)
  1484. DBG(cdev, "HNP available\n");
  1485. else if (gadget->a_alt_hnp_support)
  1486. DBG(cdev, "HNP on another port\n");
  1487. else
  1488. VDBG(cdev, "HNP inactive\n");
  1489. }
  1490. spin_lock(&cdev->lock);
  1491. value = set_config(cdev, ctrl, w_value);
  1492. spin_unlock(&cdev->lock);
  1493. break;
  1494. case USB_REQ_GET_CONFIGURATION:
  1495. if (ctrl->bRequestType != USB_DIR_IN)
  1496. goto unknown;
  1497. if (cdev->config)
  1498. *(u8 *)req->buf = cdev->config->bConfigurationValue;
  1499. else
  1500. *(u8 *)req->buf = 0;
  1501. value = min(w_length, (u16) 1);
  1502. break;
  1503. /* function drivers must handle get/set altsetting */
  1504. case USB_REQ_SET_INTERFACE:
  1505. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  1506. goto unknown;
  1507. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1508. break;
  1509. f = cdev->config->interface[intf];
  1510. if (!f)
  1511. break;
  1512. /*
  1513. * If there's no get_alt() method, we know only altsetting zero
  1514. * works. There is no need to check if set_alt() is not NULL
  1515. * as we check this in usb_add_function().
  1516. */
  1517. if (w_value && !f->get_alt)
  1518. break;
  1519. value = f->set_alt(f, w_index, w_value);
  1520. if (value == USB_GADGET_DELAYED_STATUS) {
  1521. DBG(cdev,
  1522. "%s: interface %d (%s) requested delayed status\n",
  1523. __func__, intf, f->name);
  1524. cdev->delayed_status++;
  1525. DBG(cdev, "delayed_status count %d\n",
  1526. cdev->delayed_status);
  1527. }
  1528. break;
  1529. case USB_REQ_GET_INTERFACE:
  1530. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  1531. goto unknown;
  1532. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1533. break;
  1534. f = cdev->config->interface[intf];
  1535. if (!f)
  1536. break;
  1537. /* lots of interfaces only need altsetting zero... */
  1538. value = f->get_alt ? f->get_alt(f, w_index) : 0;
  1539. if (value < 0)
  1540. break;
  1541. *((u8 *)req->buf) = value;
  1542. value = min(w_length, (u16) 1);
  1543. break;
  1544. case USB_REQ_GET_STATUS:
  1545. if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
  1546. (w_index == OTG_STS_SELECTOR)) {
  1547. if (ctrl->bRequestType != (USB_DIR_IN |
  1548. USB_RECIP_DEVICE))
  1549. goto unknown;
  1550. *((u8 *)req->buf) = gadget->host_request_flag;
  1551. value = 1;
  1552. break;
  1553. }
  1554. /*
  1555. * USB 3.0 additions:
  1556. * Function driver should handle get_status request. If such cb
  1557. * wasn't supplied we respond with default value = 0
  1558. * Note: function driver should supply such cb only for the
  1559. * first interface of the function
  1560. */
  1561. if (!gadget_is_superspeed(gadget))
  1562. goto unknown;
  1563. if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
  1564. goto unknown;
  1565. value = 2; /* This is the length of the get_status reply */
  1566. put_unaligned_le16(0, req->buf);
  1567. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1568. break;
  1569. f = cdev->config->interface[intf];
  1570. if (!f)
  1571. break;
  1572. status = f->get_status ? f->get_status(f) : 0;
  1573. if (status < 0)
  1574. break;
  1575. put_unaligned_le16(status & 0x0000ffff, req->buf);
  1576. break;
  1577. /*
  1578. * Function drivers should handle SetFeature/ClearFeature
  1579. * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
  1580. * only for the first interface of the function
  1581. */
  1582. case USB_REQ_CLEAR_FEATURE:
  1583. case USB_REQ_SET_FEATURE:
  1584. if (!gadget_is_superspeed(gadget))
  1585. goto unknown;
  1586. if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
  1587. goto unknown;
  1588. switch (w_value) {
  1589. case USB_INTRF_FUNC_SUSPEND:
  1590. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1591. break;
  1592. f = cdev->config->interface[intf];
  1593. if (!f)
  1594. break;
  1595. value = 0;
  1596. if (f->func_suspend)
  1597. value = f->func_suspend(f, w_index >> 8);
  1598. if (value < 0) {
  1599. ERROR(cdev,
  1600. "func_suspend() returned error %d\n",
  1601. value);
  1602. value = 0;
  1603. }
  1604. break;
  1605. }
  1606. break;
  1607. default:
  1608. unknown:
  1609. /*
  1610. * OS descriptors handling
  1611. */
  1612. if (cdev->use_os_string && cdev->os_desc_config &&
  1613. (ctrl->bRequestType & USB_TYPE_VENDOR) &&
  1614. ctrl->bRequest == cdev->b_vendor_code) {
  1615. struct usb_request *req;
  1616. struct usb_configuration *os_desc_cfg;
  1617. u8 *buf;
  1618. int interface;
  1619. int count = 0;
  1620. req = cdev->os_desc_req;
  1621. req->context = cdev;
  1622. req->complete = composite_setup_complete;
  1623. buf = req->buf;
  1624. os_desc_cfg = cdev->os_desc_config;
  1625. memset(buf, 0, w_length);
  1626. buf[5] = 0x01;
  1627. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1628. case USB_RECIP_DEVICE:
  1629. if (w_index != 0x4 || (w_value >> 8))
  1630. break;
  1631. buf[6] = w_index;
  1632. if (w_length == 0x10) {
  1633. /* Number of ext compat interfaces */
  1634. count = count_ext_compat(os_desc_cfg);
  1635. buf[8] = count;
  1636. count *= 24; /* 24 B/ext compat desc */
  1637. count += 16; /* header */
  1638. put_unaligned_le32(count, buf);
  1639. value = w_length;
  1640. } else {
  1641. /* "extended compatibility ID"s */
  1642. count = count_ext_compat(os_desc_cfg);
  1643. buf[8] = count;
  1644. count *= 24; /* 24 B/ext compat desc */
  1645. count += 16; /* header */
  1646. put_unaligned_le32(count, buf);
  1647. buf += 16;
  1648. fill_ext_compat(os_desc_cfg, buf);
  1649. value = w_length;
  1650. }
  1651. break;
  1652. case USB_RECIP_INTERFACE:
  1653. if (w_index != 0x5 || (w_value >> 8))
  1654. break;
  1655. interface = w_value & 0xFF;
  1656. buf[6] = w_index;
  1657. if (w_length == 0x0A) {
  1658. count = count_ext_prop(os_desc_cfg,
  1659. interface);
  1660. put_unaligned_le16(count, buf + 8);
  1661. count = len_ext_prop(os_desc_cfg,
  1662. interface);
  1663. put_unaligned_le32(count, buf);
  1664. value = w_length;
  1665. } else {
  1666. count = count_ext_prop(os_desc_cfg,
  1667. interface);
  1668. put_unaligned_le16(count, buf + 8);
  1669. count = len_ext_prop(os_desc_cfg,
  1670. interface);
  1671. put_unaligned_le32(count, buf);
  1672. buf += 10;
  1673. value = fill_ext_prop(os_desc_cfg,
  1674. interface, buf);
  1675. if (value < 0)
  1676. return value;
  1677. value = w_length;
  1678. }
  1679. break;
  1680. }
  1681. if (value >= 0) {
  1682. req->length = value;
  1683. req->context = cdev;
  1684. req->zero = value < w_length;
  1685. value = composite_ep0_queue(cdev, req,
  1686. GFP_ATOMIC);
  1687. if (value < 0) {
  1688. DBG(cdev, "ep_queue --> %d\n", value);
  1689. req->status = 0;
  1690. composite_setup_complete(gadget->ep0,
  1691. req);
  1692. }
  1693. }
  1694. return value;
  1695. }
  1696. VDBG(cdev,
  1697. "non-core control req%02x.%02x v%04x i%04x l%d\n",
  1698. ctrl->bRequestType, ctrl->bRequest,
  1699. w_value, w_index, w_length);
  1700. /* functions always handle their interfaces and endpoints...
  1701. * punt other recipients (other, WUSB, ...) to the current
  1702. * configuration code.
  1703. */
  1704. if (cdev->config) {
  1705. list_for_each_entry(f, &cdev->config->functions, list)
  1706. if (f->req_match &&
  1707. f->req_match(f, ctrl, false))
  1708. goto try_fun_setup;
  1709. } else {
  1710. struct usb_configuration *c;
  1711. list_for_each_entry(c, &cdev->configs, list)
  1712. list_for_each_entry(f, &c->functions, list)
  1713. if (f->req_match &&
  1714. f->req_match(f, ctrl, true))
  1715. goto try_fun_setup;
  1716. }
  1717. f = NULL;
  1718. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1719. case USB_RECIP_INTERFACE:
  1720. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1721. break;
  1722. f = cdev->config->interface[intf];
  1723. break;
  1724. case USB_RECIP_ENDPOINT:
  1725. if (!cdev->config)
  1726. break;
  1727. endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
  1728. list_for_each_entry(f, &cdev->config->functions, list) {
  1729. if (test_bit(endp, f->endpoints))
  1730. break;
  1731. }
  1732. if (&f->list == &cdev->config->functions)
  1733. f = NULL;
  1734. break;
  1735. }
  1736. try_fun_setup:
  1737. if (f && f->setup)
  1738. value = f->setup(f, ctrl);
  1739. else {
  1740. struct usb_configuration *c;
  1741. c = cdev->config;
  1742. if (!c)
  1743. goto done;
  1744. /* try current config's setup */
  1745. if (c->setup) {
  1746. value = c->setup(c, ctrl);
  1747. goto done;
  1748. }
  1749. /* try the only function in the current config */
  1750. if (!list_is_singular(&c->functions))
  1751. goto done;
  1752. f = list_first_entry(&c->functions, struct usb_function,
  1753. list);
  1754. if (f->setup)
  1755. value = f->setup(f, ctrl);
  1756. }
  1757. goto done;
  1758. }
  1759. /* respond with data transfer before status phase? */
  1760. if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
  1761. req->length = value;
  1762. req->context = cdev;
  1763. req->zero = value < w_length;
  1764. value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
  1765. if (value < 0) {
  1766. DBG(cdev, "ep_queue --> %d\n", value);
  1767. req->status = 0;
  1768. composite_setup_complete(gadget->ep0, req);
  1769. }
  1770. } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
  1771. WARN(cdev,
  1772. "%s: Delayed status not supported for w_length != 0",
  1773. __func__);
  1774. }
  1775. done:
  1776. /* device either stalls (value < 0) or reports success */
  1777. return value;
  1778. }
  1779. void composite_disconnect(struct usb_gadget *gadget)
  1780. {
  1781. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1782. unsigned long flags;
  1783. /* REVISIT: should we have config and device level
  1784. * disconnect callbacks?
  1785. */
  1786. spin_lock_irqsave(&cdev->lock, flags);
  1787. if (cdev->config)
  1788. reset_config(cdev);
  1789. if (cdev->driver->disconnect)
  1790. cdev->driver->disconnect(cdev);
  1791. spin_unlock_irqrestore(&cdev->lock, flags);
  1792. }
  1793. /*-------------------------------------------------------------------------*/
  1794. static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
  1795. char *buf)
  1796. {
  1797. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  1798. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1799. return sprintf(buf, "%d\n", cdev->suspended);
  1800. }
  1801. static DEVICE_ATTR_RO(suspended);
  1802. static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
  1803. {
  1804. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1805. struct usb_gadget_strings *gstr = cdev->driver->strings[0];
  1806. struct usb_string *dev_str = gstr->strings;
  1807. /* composite_disconnect() must already have been called
  1808. * by the underlying peripheral controller driver!
  1809. * so there's no i/o concurrency that could affect the
  1810. * state protected by cdev->lock.
  1811. */
  1812. WARN_ON(cdev->config);
  1813. while (!list_empty(&cdev->configs)) {
  1814. struct usb_configuration *c;
  1815. c = list_first_entry(&cdev->configs,
  1816. struct usb_configuration, list);
  1817. remove_config(cdev, c);
  1818. }
  1819. if (cdev->driver->unbind && unbind_driver)
  1820. cdev->driver->unbind(cdev);
  1821. composite_dev_cleanup(cdev);
  1822. if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
  1823. dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
  1824. kfree(cdev->def_manufacturer);
  1825. kfree(cdev);
  1826. set_gadget_data(gadget, NULL);
  1827. }
  1828. static void composite_unbind(struct usb_gadget *gadget)
  1829. {
  1830. __composite_unbind(gadget, true);
  1831. }
  1832. static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
  1833. const struct usb_device_descriptor *old)
  1834. {
  1835. __le16 idVendor;
  1836. __le16 idProduct;
  1837. __le16 bcdDevice;
  1838. u8 iSerialNumber;
  1839. u8 iManufacturer;
  1840. u8 iProduct;
  1841. /*
  1842. * these variables may have been set in
  1843. * usb_composite_overwrite_options()
  1844. */
  1845. idVendor = new->idVendor;
  1846. idProduct = new->idProduct;
  1847. bcdDevice = new->bcdDevice;
  1848. iSerialNumber = new->iSerialNumber;
  1849. iManufacturer = new->iManufacturer;
  1850. iProduct = new->iProduct;
  1851. *new = *old;
  1852. if (idVendor)
  1853. new->idVendor = idVendor;
  1854. if (idProduct)
  1855. new->idProduct = idProduct;
  1856. if (bcdDevice)
  1857. new->bcdDevice = bcdDevice;
  1858. else
  1859. new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
  1860. if (iSerialNumber)
  1861. new->iSerialNumber = iSerialNumber;
  1862. if (iManufacturer)
  1863. new->iManufacturer = iManufacturer;
  1864. if (iProduct)
  1865. new->iProduct = iProduct;
  1866. }
  1867. int composite_dev_prepare(struct usb_composite_driver *composite,
  1868. struct usb_composite_dev *cdev)
  1869. {
  1870. struct usb_gadget *gadget = cdev->gadget;
  1871. int ret = -ENOMEM;
  1872. /* preallocate control response and buffer */
  1873. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  1874. if (!cdev->req)
  1875. return -ENOMEM;
  1876. cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
  1877. if (!cdev->req->buf)
  1878. goto fail;
  1879. ret = device_create_file(&gadget->dev, &dev_attr_suspended);
  1880. if (ret)
  1881. goto fail_dev;
  1882. cdev->req->complete = composite_setup_complete;
  1883. cdev->req->context = cdev;
  1884. gadget->ep0->driver_data = cdev;
  1885. cdev->driver = composite;
  1886. /*
  1887. * As per USB compliance update, a device that is actively drawing
  1888. * more than 100mA from USB must report itself as bus-powered in
  1889. * the GetStatus(DEVICE) call.
  1890. */
  1891. if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
  1892. usb_gadget_set_selfpowered(gadget);
  1893. /* interface and string IDs start at zero via kzalloc.
  1894. * we force endpoints to start unassigned; few controller
  1895. * drivers will zero ep->driver_data.
  1896. */
  1897. usb_ep_autoconfig_reset(gadget);
  1898. return 0;
  1899. fail_dev:
  1900. kfree(cdev->req->buf);
  1901. fail:
  1902. usb_ep_free_request(gadget->ep0, cdev->req);
  1903. cdev->req = NULL;
  1904. return ret;
  1905. }
  1906. int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
  1907. struct usb_ep *ep0)
  1908. {
  1909. int ret = 0;
  1910. cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
  1911. if (!cdev->os_desc_req) {
  1912. ret = -ENOMEM;
  1913. goto end;
  1914. }
  1915. /* OS feature descriptor length <= 4kB */
  1916. cdev->os_desc_req->buf = kmalloc(4096, GFP_KERNEL);
  1917. if (!cdev->os_desc_req->buf) {
  1918. ret = -ENOMEM;
  1919. usb_ep_free_request(ep0, cdev->os_desc_req);
  1920. goto end;
  1921. }
  1922. cdev->os_desc_req->context = cdev;
  1923. cdev->os_desc_req->complete = composite_setup_complete;
  1924. end:
  1925. return ret;
  1926. }
  1927. void composite_dev_cleanup(struct usb_composite_dev *cdev)
  1928. {
  1929. struct usb_gadget_string_container *uc, *tmp;
  1930. list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
  1931. list_del(&uc->list);
  1932. kfree(uc);
  1933. }
  1934. if (cdev->os_desc_req) {
  1935. if (cdev->os_desc_pending)
  1936. usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
  1937. kfree(cdev->os_desc_req->buf);
  1938. usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
  1939. }
  1940. if (cdev->req) {
  1941. if (cdev->setup_pending)
  1942. usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
  1943. kfree(cdev->req->buf);
  1944. usb_ep_free_request(cdev->gadget->ep0, cdev->req);
  1945. }
  1946. cdev->next_string_id = 0;
  1947. device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
  1948. }
  1949. static int composite_bind(struct usb_gadget *gadget,
  1950. struct usb_gadget_driver *gdriver)
  1951. {
  1952. struct usb_composite_dev *cdev;
  1953. struct usb_composite_driver *composite = to_cdriver(gdriver);
  1954. int status = -ENOMEM;
  1955. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  1956. if (!cdev)
  1957. return status;
  1958. spin_lock_init(&cdev->lock);
  1959. cdev->gadget = gadget;
  1960. set_gadget_data(gadget, cdev);
  1961. INIT_LIST_HEAD(&cdev->configs);
  1962. INIT_LIST_HEAD(&cdev->gstrings);
  1963. status = composite_dev_prepare(composite, cdev);
  1964. if (status)
  1965. goto fail;
  1966. /* composite gadget needs to assign strings for whole device (like
  1967. * serial number), register function drivers, potentially update
  1968. * power state and consumption, etc
  1969. */
  1970. status = composite->bind(cdev);
  1971. if (status < 0)
  1972. goto fail;
  1973. if (cdev->use_os_string) {
  1974. status = composite_os_desc_req_prepare(cdev, gadget->ep0);
  1975. if (status)
  1976. goto fail;
  1977. }
  1978. update_unchanged_dev_desc(&cdev->desc, composite->dev);
  1979. /* has userspace failed to provide a serial number? */
  1980. if (composite->needs_serial && !cdev->desc.iSerialNumber)
  1981. WARNING(cdev, "userspace failed to provide iSerialNumber\n");
  1982. INFO(cdev, "%s ready\n", composite->name);
  1983. return 0;
  1984. fail:
  1985. __composite_unbind(gadget, false);
  1986. return status;
  1987. }
  1988. /*-------------------------------------------------------------------------*/
  1989. void composite_suspend(struct usb_gadget *gadget)
  1990. {
  1991. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1992. struct usb_function *f;
  1993. /* REVISIT: should we have config level
  1994. * suspend/resume callbacks?
  1995. */
  1996. DBG(cdev, "suspend\n");
  1997. if (cdev->config) {
  1998. list_for_each_entry(f, &cdev->config->functions, list) {
  1999. if (f->suspend)
  2000. f->suspend(f);
  2001. }
  2002. }
  2003. if (cdev->driver->suspend)
  2004. cdev->driver->suspend(cdev);
  2005. cdev->suspended = 1;
  2006. usb_gadget_vbus_draw(gadget, 2);
  2007. }
  2008. void composite_resume(struct usb_gadget *gadget)
  2009. {
  2010. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  2011. struct usb_function *f;
  2012. u16 maxpower;
  2013. /* REVISIT: should we have config level
  2014. * suspend/resume callbacks?
  2015. */
  2016. DBG(cdev, "resume\n");
  2017. if (cdev->driver->resume)
  2018. cdev->driver->resume(cdev);
  2019. if (cdev->config) {
  2020. list_for_each_entry(f, &cdev->config->functions, list) {
  2021. if (f->resume)
  2022. f->resume(f);
  2023. }
  2024. maxpower = cdev->config->MaxPower;
  2025. usb_gadget_vbus_draw(gadget, maxpower ?
  2026. maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
  2027. }
  2028. cdev->suspended = 0;
  2029. }
  2030. /*-------------------------------------------------------------------------*/
  2031. static const struct usb_gadget_driver composite_driver_template = {
  2032. .bind = composite_bind,
  2033. .unbind = composite_unbind,
  2034. .setup = composite_setup,
  2035. .reset = composite_disconnect,
  2036. .disconnect = composite_disconnect,
  2037. .suspend = composite_suspend,
  2038. .resume = composite_resume,
  2039. .driver = {
  2040. .owner = THIS_MODULE,
  2041. },
  2042. };
  2043. /**
  2044. * usb_composite_probe() - register a composite driver
  2045. * @driver: the driver to register
  2046. *
  2047. * Context: single threaded during gadget setup
  2048. *
  2049. * This function is used to register drivers using the composite driver
  2050. * framework. The return value is zero, or a negative errno value.
  2051. * Those values normally come from the driver's @bind method, which does
  2052. * all the work of setting up the driver to match the hardware.
  2053. *
  2054. * On successful return, the gadget is ready to respond to requests from
  2055. * the host, unless one of its components invokes usb_gadget_disconnect()
  2056. * while it was binding. That would usually be done in order to wait for
  2057. * some userspace participation.
  2058. */
  2059. int usb_composite_probe(struct usb_composite_driver *driver)
  2060. {
  2061. struct usb_gadget_driver *gadget_driver;
  2062. if (!driver || !driver->dev || !driver->bind)
  2063. return -EINVAL;
  2064. if (!driver->name)
  2065. driver->name = "composite";
  2066. driver->gadget_driver = composite_driver_template;
  2067. gadget_driver = &driver->gadget_driver;
  2068. gadget_driver->function = (char *) driver->name;
  2069. gadget_driver->driver.name = driver->name;
  2070. gadget_driver->max_speed = driver->max_speed;
  2071. return usb_gadget_probe_driver(gadget_driver);
  2072. }
  2073. EXPORT_SYMBOL_GPL(usb_composite_probe);
  2074. /**
  2075. * usb_composite_unregister() - unregister a composite driver
  2076. * @driver: the driver to unregister
  2077. *
  2078. * This function is used to unregister drivers using the composite
  2079. * driver framework.
  2080. */
  2081. void usb_composite_unregister(struct usb_composite_driver *driver)
  2082. {
  2083. usb_gadget_unregister_driver(&driver->gadget_driver);
  2084. }
  2085. EXPORT_SYMBOL_GPL(usb_composite_unregister);
  2086. /**
  2087. * usb_composite_setup_continue() - Continue with the control transfer
  2088. * @cdev: the composite device who's control transfer was kept waiting
  2089. *
  2090. * This function must be called by the USB function driver to continue
  2091. * with the control transfer's data/status stage in case it had requested to
  2092. * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
  2093. * can request the composite framework to delay the setup request's data/status
  2094. * stages by returning USB_GADGET_DELAYED_STATUS.
  2095. */
  2096. void usb_composite_setup_continue(struct usb_composite_dev *cdev)
  2097. {
  2098. int value;
  2099. struct usb_request *req = cdev->req;
  2100. unsigned long flags;
  2101. DBG(cdev, "%s\n", __func__);
  2102. spin_lock_irqsave(&cdev->lock, flags);
  2103. if (cdev->delayed_status == 0) {
  2104. WARN(cdev, "%s: Unexpected call\n", __func__);
  2105. } else if (--cdev->delayed_status == 0) {
  2106. DBG(cdev, "%s: Completing delayed status\n", __func__);
  2107. req->length = 0;
  2108. req->context = cdev;
  2109. value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
  2110. if (value < 0) {
  2111. DBG(cdev, "ep_queue --> %d\n", value);
  2112. req->status = 0;
  2113. composite_setup_complete(cdev->gadget->ep0, req);
  2114. }
  2115. }
  2116. spin_unlock_irqrestore(&cdev->lock, flags);
  2117. }
  2118. EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
  2119. static char *composite_default_mfr(struct usb_gadget *gadget)
  2120. {
  2121. return kasprintf(GFP_KERNEL, "%s %s with %s", init_utsname()->sysname,
  2122. init_utsname()->release, gadget->name);
  2123. }
  2124. void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
  2125. struct usb_composite_overwrite *covr)
  2126. {
  2127. struct usb_device_descriptor *desc = &cdev->desc;
  2128. struct usb_gadget_strings *gstr = cdev->driver->strings[0];
  2129. struct usb_string *dev_str = gstr->strings;
  2130. if (covr->idVendor)
  2131. desc->idVendor = cpu_to_le16(covr->idVendor);
  2132. if (covr->idProduct)
  2133. desc->idProduct = cpu_to_le16(covr->idProduct);
  2134. if (covr->bcdDevice)
  2135. desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
  2136. if (covr->serial_number) {
  2137. desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
  2138. dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
  2139. }
  2140. if (covr->manufacturer) {
  2141. desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
  2142. dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
  2143. } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
  2144. desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
  2145. cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
  2146. dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
  2147. }
  2148. if (covr->product) {
  2149. desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
  2150. dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
  2151. }
  2152. }
  2153. EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
  2154. MODULE_LICENSE("GPL");
  2155. MODULE_AUTHOR("David Brownell");