composite.c 55 KB

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