composite.c 47 KB

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