composite.c 62 KB

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