composite.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /* #define VERBOSE_DEBUG */
  21. #include <linux/kallsyms.h>
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/device.h>
  25. #include <linux/utsname.h>
  26. #include <linux/usb/composite.h>
  27. /*
  28. * The code in this file is utility code, used to build a gadget driver
  29. * from one or more "function" drivers, one or more "configuration"
  30. * objects, and a "usb_composite_driver" by gluing them together along
  31. * with the relevant device-wide data.
  32. */
  33. /* big enough to hold our biggest descriptor */
  34. #define USB_BUFSIZ 1024
  35. static struct usb_composite_driver *composite;
  36. static int (*composite_gadget_bind)(struct usb_composite_dev *cdev);
  37. /* Some systems will need runtime overrides for the product identifiers
  38. * published in the device descriptor, either numbers or strings or both.
  39. * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
  40. */
  41. static ushort idVendor;
  42. module_param(idVendor, ushort, 0);
  43. MODULE_PARM_DESC(idVendor, "USB Vendor ID");
  44. static ushort idProduct;
  45. module_param(idProduct, ushort, 0);
  46. MODULE_PARM_DESC(idProduct, "USB Product ID");
  47. static ushort bcdDevice;
  48. module_param(bcdDevice, ushort, 0);
  49. MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
  50. static char *iManufacturer;
  51. module_param(iManufacturer, charp, 0);
  52. MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
  53. static char *iProduct;
  54. module_param(iProduct, charp, 0);
  55. MODULE_PARM_DESC(iProduct, "USB Product string");
  56. static char *iSerialNumber;
  57. module_param(iSerialNumber, charp, 0);
  58. MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
  59. static char composite_manufacturer[50];
  60. /*-------------------------------------------------------------------------*/
  61. /**
  62. * usb_add_function() - add a function to a configuration
  63. * @config: the configuration
  64. * @function: the function being added
  65. * Context: single threaded during gadget setup
  66. *
  67. * After initialization, each configuration must have one or more
  68. * functions added to it. Adding a function involves calling its @bind()
  69. * method to allocate resources such as interface and string identifiers
  70. * and endpoints.
  71. *
  72. * This function returns the value of the function's bind(), which is
  73. * zero for success else a negative errno value.
  74. */
  75. int usb_add_function(struct usb_configuration *config,
  76. struct usb_function *function)
  77. {
  78. int value = -EINVAL;
  79. DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
  80. function->name, function,
  81. config->label, config);
  82. if (!function->set_alt || !function->disable)
  83. goto done;
  84. function->config = config;
  85. list_add_tail(&function->list, &config->functions);
  86. /* REVISIT *require* function->bind? */
  87. if (function->bind) {
  88. value = function->bind(config, function);
  89. if (value < 0) {
  90. list_del(&function->list);
  91. function->config = NULL;
  92. }
  93. } else
  94. value = 0;
  95. /* We allow configurations that don't work at both speeds.
  96. * If we run into a lowspeed Linux system, treat it the same
  97. * as full speed ... it's the function drivers that will need
  98. * to avoid bulk and ISO transfers.
  99. */
  100. if (!config->fullspeed && function->descriptors)
  101. config->fullspeed = true;
  102. if (!config->highspeed && function->hs_descriptors)
  103. config->highspeed = true;
  104. done:
  105. if (value)
  106. DBG(config->cdev, "adding '%s'/%p --> %d\n",
  107. function->name, function, value);
  108. return value;
  109. }
  110. /**
  111. * usb_function_deactivate - prevent function and gadget enumeration
  112. * @function: the function that isn't yet ready to respond
  113. *
  114. * Blocks response of the gadget driver to host enumeration by
  115. * preventing the data line pullup from being activated. This is
  116. * normally called during @bind() processing to change from the
  117. * initial "ready to respond" state, or when a required resource
  118. * becomes available.
  119. *
  120. * For example, drivers that serve as a passthrough to a userspace
  121. * daemon can block enumeration unless that daemon (such as an OBEX,
  122. * MTP, or print server) is ready to handle host requests.
  123. *
  124. * Not all systems support software control of their USB peripheral
  125. * data pullups.
  126. *
  127. * Returns zero on success, else negative errno.
  128. */
  129. int usb_function_deactivate(struct usb_function *function)
  130. {
  131. struct usb_composite_dev *cdev = function->config->cdev;
  132. unsigned long flags;
  133. int status = 0;
  134. spin_lock_irqsave(&cdev->lock, flags);
  135. if (cdev->deactivations == 0)
  136. status = usb_gadget_disconnect(cdev->gadget);
  137. if (status == 0)
  138. cdev->deactivations++;
  139. spin_unlock_irqrestore(&cdev->lock, flags);
  140. return status;
  141. }
  142. /**
  143. * usb_function_activate - allow function and gadget enumeration
  144. * @function: function on which usb_function_activate() was called
  145. *
  146. * Reverses effect of usb_function_deactivate(). If no more functions
  147. * are delaying their activation, the gadget driver will respond to
  148. * host enumeration procedures.
  149. *
  150. * Returns zero on success, else negative errno.
  151. */
  152. int usb_function_activate(struct usb_function *function)
  153. {
  154. struct usb_composite_dev *cdev = function->config->cdev;
  155. int status = 0;
  156. spin_lock(&cdev->lock);
  157. if (WARN_ON(cdev->deactivations == 0))
  158. status = -EINVAL;
  159. else {
  160. cdev->deactivations--;
  161. if (cdev->deactivations == 0)
  162. status = usb_gadget_connect(cdev->gadget);
  163. }
  164. spin_unlock(&cdev->lock);
  165. return status;
  166. }
  167. /**
  168. * usb_interface_id() - allocate an unused interface ID
  169. * @config: configuration associated with the interface
  170. * @function: function handling the interface
  171. * Context: single threaded during gadget setup
  172. *
  173. * usb_interface_id() is called from usb_function.bind() callbacks to
  174. * allocate new interface IDs. The function driver will then store that
  175. * ID in interface, association, CDC union, and other descriptors. It
  176. * will also handle any control requests targeted at that interface,
  177. * particularly changing its altsetting via set_alt(). There may
  178. * also be class-specific or vendor-specific requests to handle.
  179. *
  180. * All interface identifier should be allocated using this routine, to
  181. * ensure that for example different functions don't wrongly assign
  182. * different meanings to the same identifier. Note that since interface
  183. * identifiers are configuration-specific, functions used in more than
  184. * one configuration (or more than once in a given configuration) need
  185. * multiple versions of the relevant descriptors.
  186. *
  187. * Returns the interface ID which was allocated; or -ENODEV if no
  188. * more interface IDs can be allocated.
  189. */
  190. int usb_interface_id(struct usb_configuration *config,
  191. struct usb_function *function)
  192. {
  193. unsigned id = config->next_interface_id;
  194. if (id < MAX_CONFIG_INTERFACES) {
  195. config->interface[id] = function;
  196. config->next_interface_id = id + 1;
  197. return id;
  198. }
  199. return -ENODEV;
  200. }
  201. static int config_buf(struct usb_configuration *config,
  202. enum usb_device_speed speed, void *buf, u8 type)
  203. {
  204. struct usb_config_descriptor *c = buf;
  205. void *next = buf + USB_DT_CONFIG_SIZE;
  206. int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
  207. struct usb_function *f;
  208. int status;
  209. /* write the config descriptor */
  210. c = buf;
  211. c->bLength = USB_DT_CONFIG_SIZE;
  212. c->bDescriptorType = type;
  213. /* wTotalLength is written later */
  214. c->bNumInterfaces = config->next_interface_id;
  215. c->bConfigurationValue = config->bConfigurationValue;
  216. c->iConfiguration = config->iConfiguration;
  217. c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
  218. c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
  219. /* There may be e.g. OTG descriptors */
  220. if (config->descriptors) {
  221. status = usb_descriptor_fillbuf(next, len,
  222. config->descriptors);
  223. if (status < 0)
  224. return status;
  225. len -= status;
  226. next += status;
  227. }
  228. /* add each function's descriptors */
  229. list_for_each_entry(f, &config->functions, list) {
  230. struct usb_descriptor_header **descriptors;
  231. if (speed == USB_SPEED_HIGH)
  232. descriptors = f->hs_descriptors;
  233. else
  234. descriptors = f->descriptors;
  235. if (!descriptors)
  236. continue;
  237. status = usb_descriptor_fillbuf(next, len,
  238. (const struct usb_descriptor_header **) descriptors);
  239. if (status < 0)
  240. return status;
  241. len -= status;
  242. next += status;
  243. }
  244. len = next - buf;
  245. c->wTotalLength = cpu_to_le16(len);
  246. return len;
  247. }
  248. static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
  249. {
  250. struct usb_gadget *gadget = cdev->gadget;
  251. struct usb_configuration *c;
  252. u8 type = w_value >> 8;
  253. enum usb_device_speed speed = USB_SPEED_UNKNOWN;
  254. if (gadget_is_dualspeed(gadget)) {
  255. int hs = 0;
  256. if (gadget->speed == USB_SPEED_HIGH)
  257. hs = 1;
  258. if (type == USB_DT_OTHER_SPEED_CONFIG)
  259. hs = !hs;
  260. if (hs)
  261. speed = USB_SPEED_HIGH;
  262. }
  263. /* This is a lookup by config *INDEX* */
  264. w_value &= 0xff;
  265. list_for_each_entry(c, &cdev->configs, list) {
  266. /* ignore configs that won't work at this speed */
  267. if (speed == USB_SPEED_HIGH) {
  268. if (!c->highspeed)
  269. continue;
  270. } else {
  271. if (!c->fullspeed)
  272. continue;
  273. }
  274. if (w_value == 0)
  275. return config_buf(c, speed, cdev->req->buf, type);
  276. w_value--;
  277. }
  278. return -EINVAL;
  279. }
  280. static int count_configs(struct usb_composite_dev *cdev, unsigned type)
  281. {
  282. struct usb_gadget *gadget = cdev->gadget;
  283. struct usb_configuration *c;
  284. unsigned count = 0;
  285. int hs = 0;
  286. if (gadget_is_dualspeed(gadget)) {
  287. if (gadget->speed == USB_SPEED_HIGH)
  288. hs = 1;
  289. if (type == USB_DT_DEVICE_QUALIFIER)
  290. hs = !hs;
  291. }
  292. list_for_each_entry(c, &cdev->configs, list) {
  293. /* ignore configs that won't work at this speed */
  294. if (hs) {
  295. if (!c->highspeed)
  296. continue;
  297. } else {
  298. if (!c->fullspeed)
  299. continue;
  300. }
  301. count++;
  302. }
  303. return count;
  304. }
  305. static void device_qual(struct usb_composite_dev *cdev)
  306. {
  307. struct usb_qualifier_descriptor *qual = cdev->req->buf;
  308. qual->bLength = sizeof(*qual);
  309. qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  310. /* POLICY: same bcdUSB and device type info at both speeds */
  311. qual->bcdUSB = cdev->desc.bcdUSB;
  312. qual->bDeviceClass = cdev->desc.bDeviceClass;
  313. qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
  314. qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
  315. /* ASSUME same EP0 fifo size at both speeds */
  316. qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
  317. qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
  318. qual->bRESERVED = 0;
  319. }
  320. /*-------------------------------------------------------------------------*/
  321. static void reset_config(struct usb_composite_dev *cdev)
  322. {
  323. struct usb_function *f;
  324. DBG(cdev, "reset config\n");
  325. list_for_each_entry(f, &cdev->config->functions, list) {
  326. if (f->disable)
  327. f->disable(f);
  328. bitmap_zero(f->endpoints, 32);
  329. }
  330. cdev->config = NULL;
  331. }
  332. static int set_config(struct usb_composite_dev *cdev,
  333. const struct usb_ctrlrequest *ctrl, unsigned number)
  334. {
  335. struct usb_gadget *gadget = cdev->gadget;
  336. struct usb_configuration *c = NULL;
  337. int result = -EINVAL;
  338. unsigned power = gadget_is_otg(gadget) ? 8 : 100;
  339. int tmp;
  340. if (cdev->config)
  341. reset_config(cdev);
  342. if (number) {
  343. list_for_each_entry(c, &cdev->configs, list) {
  344. if (c->bConfigurationValue == number) {
  345. result = 0;
  346. break;
  347. }
  348. }
  349. if (result < 0)
  350. goto done;
  351. } else
  352. result = 0;
  353. INFO(cdev, "%s speed config #%d: %s\n",
  354. ({ char *speed;
  355. switch (gadget->speed) {
  356. case USB_SPEED_LOW: speed = "low"; break;
  357. case USB_SPEED_FULL: speed = "full"; break;
  358. case USB_SPEED_HIGH: speed = "high"; break;
  359. default: speed = "?"; break;
  360. } ; speed; }), number, c ? c->label : "unconfigured");
  361. if (!c)
  362. goto done;
  363. cdev->config = c;
  364. /* Initialize all interfaces by setting them to altsetting zero. */
  365. for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
  366. struct usb_function *f = c->interface[tmp];
  367. struct usb_descriptor_header **descriptors;
  368. if (!f)
  369. break;
  370. /*
  371. * Record which endpoints are used by the function. This is used
  372. * to dispatch control requests targeted at that endpoint to the
  373. * function's setup callback instead of the current
  374. * configuration's setup callback.
  375. */
  376. if (gadget->speed == USB_SPEED_HIGH)
  377. descriptors = f->hs_descriptors;
  378. else
  379. descriptors = f->descriptors;
  380. for (; *descriptors; ++descriptors) {
  381. struct usb_endpoint_descriptor *ep;
  382. int addr;
  383. if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
  384. continue;
  385. ep = (struct usb_endpoint_descriptor *)*descriptors;
  386. addr = ((ep->bEndpointAddress & 0x80) >> 3)
  387. | (ep->bEndpointAddress & 0x0f);
  388. set_bit(addr, f->endpoints);
  389. }
  390. result = f->set_alt(f, tmp, 0);
  391. if (result < 0) {
  392. DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
  393. tmp, f->name, f, result);
  394. reset_config(cdev);
  395. goto done;
  396. }
  397. if (result == USB_GADGET_DELAYED_STATUS) {
  398. DBG(cdev,
  399. "%s: interface %d (%s) requested delayed status\n",
  400. __func__, tmp, f->name);
  401. cdev->delayed_status++;
  402. DBG(cdev, "delayed_status count %d\n",
  403. cdev->delayed_status);
  404. }
  405. }
  406. /* when we return, be sure our power usage is valid */
  407. power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
  408. done:
  409. usb_gadget_vbus_draw(gadget, power);
  410. if (result >= 0 && cdev->delayed_status)
  411. result = USB_GADGET_DELAYED_STATUS;
  412. return result;
  413. }
  414. /**
  415. * usb_add_config() - add a configuration to a device.
  416. * @cdev: wraps the USB gadget
  417. * @config: the configuration, with bConfigurationValue assigned
  418. * @bind: the configuration's bind function
  419. * Context: single threaded during gadget setup
  420. *
  421. * One of the main tasks of a composite @bind() routine is to
  422. * add each of the configurations it supports, using this routine.
  423. *
  424. * This function returns the value of the configuration's @bind(), which
  425. * is zero for success else a negative errno value. Binding configurations
  426. * assigns global resources including string IDs, and per-configuration
  427. * resources such as interface IDs and endpoints.
  428. */
  429. int usb_add_config(struct usb_composite_dev *cdev,
  430. struct usb_configuration *config,
  431. int (*bind)(struct usb_configuration *))
  432. {
  433. int status = -EINVAL;
  434. struct usb_configuration *c;
  435. DBG(cdev, "adding config #%u '%s'/%p\n",
  436. config->bConfigurationValue,
  437. config->label, config);
  438. if (!config->bConfigurationValue || !bind)
  439. goto done;
  440. /* Prevent duplicate configuration identifiers */
  441. list_for_each_entry(c, &cdev->configs, list) {
  442. if (c->bConfigurationValue == config->bConfigurationValue) {
  443. status = -EBUSY;
  444. goto done;
  445. }
  446. }
  447. config->cdev = cdev;
  448. list_add_tail(&config->list, &cdev->configs);
  449. INIT_LIST_HEAD(&config->functions);
  450. config->next_interface_id = 0;
  451. status = bind(config);
  452. if (status < 0) {
  453. list_del(&config->list);
  454. config->cdev = NULL;
  455. } else {
  456. unsigned i;
  457. DBG(cdev, "cfg %d/%p speeds:%s%s\n",
  458. config->bConfigurationValue, config,
  459. config->highspeed ? " high" : "",
  460. config->fullspeed
  461. ? (gadget_is_dualspeed(cdev->gadget)
  462. ? " full"
  463. : " full/low")
  464. : "");
  465. for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
  466. struct usb_function *f = config->interface[i];
  467. if (!f)
  468. continue;
  469. DBG(cdev, " interface %d = %s/%p\n",
  470. i, f->name, f);
  471. }
  472. }
  473. /* set_alt(), or next bind(), sets up
  474. * ep->driver_data as needed.
  475. */
  476. usb_ep_autoconfig_reset(cdev->gadget);
  477. done:
  478. if (status)
  479. DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
  480. config->bConfigurationValue, status);
  481. return status;
  482. }
  483. /*-------------------------------------------------------------------------*/
  484. /* We support strings in multiple languages ... string descriptor zero
  485. * says which languages are supported. The typical case will be that
  486. * only one language (probably English) is used, with I18N handled on
  487. * the host side.
  488. */
  489. static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
  490. {
  491. const struct usb_gadget_strings *s;
  492. u16 language;
  493. __le16 *tmp;
  494. while (*sp) {
  495. s = *sp;
  496. language = cpu_to_le16(s->language);
  497. for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
  498. if (*tmp == language)
  499. goto repeat;
  500. }
  501. *tmp++ = language;
  502. repeat:
  503. sp++;
  504. }
  505. }
  506. static int lookup_string(
  507. struct usb_gadget_strings **sp,
  508. void *buf,
  509. u16 language,
  510. int id
  511. )
  512. {
  513. struct usb_gadget_strings *s;
  514. int value;
  515. while (*sp) {
  516. s = *sp++;
  517. if (s->language != language)
  518. continue;
  519. value = usb_gadget_get_string(s, id, buf);
  520. if (value > 0)
  521. return value;
  522. }
  523. return -EINVAL;
  524. }
  525. static int get_string(struct usb_composite_dev *cdev,
  526. void *buf, u16 language, int id)
  527. {
  528. struct usb_configuration *c;
  529. struct usb_function *f;
  530. int len;
  531. const char *str;
  532. /* Yes, not only is USB's I18N support probably more than most
  533. * folk will ever care about ... also, it's all supported here.
  534. * (Except for UTF8 support for Unicode's "Astral Planes".)
  535. */
  536. /* 0 == report all available language codes */
  537. if (id == 0) {
  538. struct usb_string_descriptor *s = buf;
  539. struct usb_gadget_strings **sp;
  540. memset(s, 0, 256);
  541. s->bDescriptorType = USB_DT_STRING;
  542. sp = composite->strings;
  543. if (sp)
  544. collect_langs(sp, s->wData);
  545. list_for_each_entry(c, &cdev->configs, list) {
  546. sp = c->strings;
  547. if (sp)
  548. collect_langs(sp, s->wData);
  549. list_for_each_entry(f, &c->functions, list) {
  550. sp = f->strings;
  551. if (sp)
  552. collect_langs(sp, s->wData);
  553. }
  554. }
  555. for (len = 0; len <= 126 && s->wData[len]; len++)
  556. continue;
  557. if (!len)
  558. return -EINVAL;
  559. s->bLength = 2 * (len + 1);
  560. return s->bLength;
  561. }
  562. /* Otherwise, look up and return a specified string. First
  563. * check if the string has not been overridden.
  564. */
  565. if (cdev->manufacturer_override == id)
  566. str = iManufacturer ?: composite->iManufacturer ?:
  567. composite_manufacturer;
  568. else if (cdev->product_override == id)
  569. str = iProduct ?: composite->iProduct;
  570. else if (cdev->serial_override == id)
  571. str = iSerialNumber;
  572. else
  573. str = NULL;
  574. if (str) {
  575. struct usb_gadget_strings strings = {
  576. .language = language,
  577. .strings = &(struct usb_string) { 0xff, str }
  578. };
  579. return usb_gadget_get_string(&strings, 0xff, buf);
  580. }
  581. /* String IDs are device-scoped, so we look up each string
  582. * table we're told about. These lookups are infrequent;
  583. * simpler-is-better here.
  584. */
  585. if (composite->strings) {
  586. len = lookup_string(composite->strings, buf, language, id);
  587. if (len > 0)
  588. return len;
  589. }
  590. list_for_each_entry(c, &cdev->configs, list) {
  591. if (c->strings) {
  592. len = lookup_string(c->strings, buf, language, id);
  593. if (len > 0)
  594. return len;
  595. }
  596. list_for_each_entry(f, &c->functions, list) {
  597. if (!f->strings)
  598. continue;
  599. len = lookup_string(f->strings, buf, language, id);
  600. if (len > 0)
  601. return len;
  602. }
  603. }
  604. return -EINVAL;
  605. }
  606. /**
  607. * usb_string_id() - allocate an unused string ID
  608. * @cdev: the device whose string descriptor IDs are being allocated
  609. * Context: single threaded during gadget setup
  610. *
  611. * @usb_string_id() is called from bind() callbacks to allocate
  612. * string IDs. Drivers for functions, configurations, or gadgets will
  613. * then store that ID in the appropriate descriptors and string table.
  614. *
  615. * All string identifier should be allocated using this,
  616. * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
  617. * that for example different functions don't wrongly assign different
  618. * meanings to the same identifier.
  619. */
  620. int usb_string_id(struct usb_composite_dev *cdev)
  621. {
  622. if (cdev->next_string_id < 254) {
  623. /* string id 0 is reserved by USB spec for list of
  624. * supported languages */
  625. /* 255 reserved as well? -- mina86 */
  626. cdev->next_string_id++;
  627. return cdev->next_string_id;
  628. }
  629. return -ENODEV;
  630. }
  631. /**
  632. * usb_string_ids() - allocate unused string IDs in batch
  633. * @cdev: the device whose string descriptor IDs are being allocated
  634. * @str: an array of usb_string objects to assign numbers to
  635. * Context: single threaded during gadget setup
  636. *
  637. * @usb_string_ids() is called from bind() callbacks to allocate
  638. * string IDs. Drivers for functions, configurations, or gadgets will
  639. * then copy IDs from the string table to the appropriate descriptors
  640. * and string table for other languages.
  641. *
  642. * All string identifier should be allocated using this,
  643. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  644. * example different functions don't wrongly assign different meanings
  645. * to the same identifier.
  646. */
  647. int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
  648. {
  649. int next = cdev->next_string_id;
  650. for (; str->s; ++str) {
  651. if (unlikely(next >= 254))
  652. return -ENODEV;
  653. str->id = ++next;
  654. }
  655. cdev->next_string_id = next;
  656. return 0;
  657. }
  658. /**
  659. * usb_string_ids_n() - allocate unused string IDs in batch
  660. * @c: the device whose string descriptor IDs are being allocated
  661. * @n: number of string IDs to allocate
  662. * Context: single threaded during gadget setup
  663. *
  664. * Returns the first requested ID. This ID and next @n-1 IDs are now
  665. * valid IDs. At least provided that @n is non-zero because if it
  666. * is, returns last requested ID which is now very useful information.
  667. *
  668. * @usb_string_ids_n() is called from bind() callbacks to allocate
  669. * string IDs. Drivers for functions, configurations, or gadgets will
  670. * then store that ID in the appropriate descriptors and string table.
  671. *
  672. * All string identifier should be allocated using this,
  673. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  674. * example different functions don't wrongly assign different meanings
  675. * to the same identifier.
  676. */
  677. int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
  678. {
  679. unsigned next = c->next_string_id;
  680. if (unlikely(n > 254 || (unsigned)next + n > 254))
  681. return -ENODEV;
  682. c->next_string_id += n;
  683. return next + 1;
  684. }
  685. /*-------------------------------------------------------------------------*/
  686. static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
  687. {
  688. if (req->status || req->actual != req->length)
  689. DBG((struct usb_composite_dev *) ep->driver_data,
  690. "setup complete --> %d, %d/%d\n",
  691. req->status, req->actual, req->length);
  692. }
  693. /*
  694. * The setup() callback implements all the ep0 functionality that's
  695. * not handled lower down, in hardware or the hardware driver(like
  696. * device and endpoint feature flags, and their status). It's all
  697. * housekeeping for the gadget function we're implementing. Most of
  698. * the work is in config and function specific setup.
  699. */
  700. static int
  701. composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  702. {
  703. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  704. struct usb_request *req = cdev->req;
  705. int value = -EOPNOTSUPP;
  706. u16 w_index = le16_to_cpu(ctrl->wIndex);
  707. u8 intf = w_index & 0xFF;
  708. u16 w_value = le16_to_cpu(ctrl->wValue);
  709. u16 w_length = le16_to_cpu(ctrl->wLength);
  710. struct usb_function *f = NULL;
  711. u8 endp;
  712. /* partial re-init of the response message; the function or the
  713. * gadget might need to intercept e.g. a control-OUT completion
  714. * when we delegate to it.
  715. */
  716. req->zero = 0;
  717. req->complete = composite_setup_complete;
  718. req->length = 0;
  719. gadget->ep0->driver_data = cdev;
  720. switch (ctrl->bRequest) {
  721. /* we handle all standard USB descriptors */
  722. case USB_REQ_GET_DESCRIPTOR:
  723. if (ctrl->bRequestType != USB_DIR_IN)
  724. goto unknown;
  725. switch (w_value >> 8) {
  726. case USB_DT_DEVICE:
  727. cdev->desc.bNumConfigurations =
  728. count_configs(cdev, USB_DT_DEVICE);
  729. value = min(w_length, (u16) sizeof cdev->desc);
  730. memcpy(req->buf, &cdev->desc, value);
  731. break;
  732. case USB_DT_DEVICE_QUALIFIER:
  733. if (!gadget_is_dualspeed(gadget))
  734. break;
  735. device_qual(cdev);
  736. value = min_t(int, w_length,
  737. sizeof(struct usb_qualifier_descriptor));
  738. break;
  739. case USB_DT_OTHER_SPEED_CONFIG:
  740. if (!gadget_is_dualspeed(gadget))
  741. break;
  742. /* FALLTHROUGH */
  743. case USB_DT_CONFIG:
  744. value = config_desc(cdev, w_value);
  745. if (value >= 0)
  746. value = min(w_length, (u16) value);
  747. break;
  748. case USB_DT_STRING:
  749. value = get_string(cdev, req->buf,
  750. w_index, w_value & 0xff);
  751. if (value >= 0)
  752. value = min(w_length, (u16) value);
  753. break;
  754. }
  755. break;
  756. /* any number of configs can work */
  757. case USB_REQ_SET_CONFIGURATION:
  758. if (ctrl->bRequestType != 0)
  759. goto unknown;
  760. if (gadget_is_otg(gadget)) {
  761. if (gadget->a_hnp_support)
  762. DBG(cdev, "HNP available\n");
  763. else if (gadget->a_alt_hnp_support)
  764. DBG(cdev, "HNP on another port\n");
  765. else
  766. VDBG(cdev, "HNP inactive\n");
  767. }
  768. spin_lock(&cdev->lock);
  769. value = set_config(cdev, ctrl, w_value);
  770. spin_unlock(&cdev->lock);
  771. break;
  772. case USB_REQ_GET_CONFIGURATION:
  773. if (ctrl->bRequestType != USB_DIR_IN)
  774. goto unknown;
  775. if (cdev->config)
  776. *(u8 *)req->buf = cdev->config->bConfigurationValue;
  777. else
  778. *(u8 *)req->buf = 0;
  779. value = min(w_length, (u16) 1);
  780. break;
  781. /* function drivers must handle get/set altsetting; if there's
  782. * no get() method, we know only altsetting zero works.
  783. */
  784. case USB_REQ_SET_INTERFACE:
  785. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  786. goto unknown;
  787. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  788. break;
  789. f = cdev->config->interface[intf];
  790. if (!f)
  791. break;
  792. if (w_value && !f->set_alt)
  793. break;
  794. value = f->set_alt(f, w_index, w_value);
  795. if (value == USB_GADGET_DELAYED_STATUS) {
  796. DBG(cdev,
  797. "%s: interface %d (%s) requested delayed status\n",
  798. __func__, intf, f->name);
  799. cdev->delayed_status++;
  800. DBG(cdev, "delayed_status count %d\n",
  801. cdev->delayed_status);
  802. }
  803. break;
  804. case USB_REQ_GET_INTERFACE:
  805. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  806. goto unknown;
  807. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  808. break;
  809. f = cdev->config->interface[intf];
  810. if (!f)
  811. break;
  812. /* lots of interfaces only need altsetting zero... */
  813. value = f->get_alt ? f->get_alt(f, w_index) : 0;
  814. if (value < 0)
  815. break;
  816. *((u8 *)req->buf) = value;
  817. value = min(w_length, (u16) 1);
  818. break;
  819. default:
  820. unknown:
  821. VDBG(cdev,
  822. "non-core control req%02x.%02x v%04x i%04x l%d\n",
  823. ctrl->bRequestType, ctrl->bRequest,
  824. w_value, w_index, w_length);
  825. /* functions always handle their interfaces and endpoints...
  826. * punt other recipients (other, WUSB, ...) to the current
  827. * configuration code.
  828. *
  829. * REVISIT it could make sense to let the composite device
  830. * take such requests too, if that's ever needed: to work
  831. * in config 0, etc.
  832. */
  833. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  834. case USB_RECIP_INTERFACE:
  835. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  836. break;
  837. f = cdev->config->interface[intf];
  838. break;
  839. case USB_RECIP_ENDPOINT:
  840. endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
  841. list_for_each_entry(f, &cdev->config->functions, list) {
  842. if (test_bit(endp, f->endpoints))
  843. break;
  844. }
  845. if (&f->list == &cdev->config->functions)
  846. f = NULL;
  847. break;
  848. }
  849. if (f && f->setup)
  850. value = f->setup(f, ctrl);
  851. else {
  852. struct usb_configuration *c;
  853. c = cdev->config;
  854. if (c && c->setup)
  855. value = c->setup(c, ctrl);
  856. }
  857. goto done;
  858. }
  859. /* respond with data transfer before status phase? */
  860. if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
  861. req->length = value;
  862. req->zero = value < w_length;
  863. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  864. if (value < 0) {
  865. DBG(cdev, "ep_queue --> %d\n", value);
  866. req->status = 0;
  867. composite_setup_complete(gadget->ep0, req);
  868. }
  869. } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
  870. WARN(cdev,
  871. "%s: Delayed status not supported for w_length != 0",
  872. __func__);
  873. }
  874. done:
  875. /* device either stalls (value < 0) or reports success */
  876. return value;
  877. }
  878. static void composite_disconnect(struct usb_gadget *gadget)
  879. {
  880. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  881. unsigned long flags;
  882. /* REVISIT: should we have config and device level
  883. * disconnect callbacks?
  884. */
  885. spin_lock_irqsave(&cdev->lock, flags);
  886. if (cdev->config)
  887. reset_config(cdev);
  888. if (composite->disconnect)
  889. composite->disconnect(cdev);
  890. spin_unlock_irqrestore(&cdev->lock, flags);
  891. }
  892. /*-------------------------------------------------------------------------*/
  893. static ssize_t composite_show_suspended(struct device *dev,
  894. struct device_attribute *attr,
  895. char *buf)
  896. {
  897. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  898. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  899. return sprintf(buf, "%d\n", cdev->suspended);
  900. }
  901. static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
  902. static void
  903. composite_unbind(struct usb_gadget *gadget)
  904. {
  905. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  906. /* composite_disconnect() must already have been called
  907. * by the underlying peripheral controller driver!
  908. * so there's no i/o concurrency that could affect the
  909. * state protected by cdev->lock.
  910. */
  911. WARN_ON(cdev->config);
  912. while (!list_empty(&cdev->configs)) {
  913. struct usb_configuration *c;
  914. c = list_first_entry(&cdev->configs,
  915. struct usb_configuration, list);
  916. while (!list_empty(&c->functions)) {
  917. struct usb_function *f;
  918. f = list_first_entry(&c->functions,
  919. struct usb_function, list);
  920. list_del(&f->list);
  921. if (f->unbind) {
  922. DBG(cdev, "unbind function '%s'/%p\n",
  923. f->name, f);
  924. f->unbind(c, f);
  925. /* may free memory for "f" */
  926. }
  927. }
  928. list_del(&c->list);
  929. if (c->unbind) {
  930. DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
  931. c->unbind(c);
  932. /* may free memory for "c" */
  933. }
  934. }
  935. if (composite->unbind)
  936. composite->unbind(cdev);
  937. if (cdev->req) {
  938. kfree(cdev->req->buf);
  939. usb_ep_free_request(gadget->ep0, cdev->req);
  940. }
  941. device_remove_file(&gadget->dev, &dev_attr_suspended);
  942. kfree(cdev);
  943. set_gadget_data(gadget, NULL);
  944. composite = NULL;
  945. }
  946. static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
  947. {
  948. if (!*desc) {
  949. int ret = usb_string_id(cdev);
  950. if (unlikely(ret < 0))
  951. WARNING(cdev, "failed to override string ID\n");
  952. else
  953. *desc = ret;
  954. }
  955. return *desc;
  956. }
  957. static int composite_bind(struct usb_gadget *gadget)
  958. {
  959. struct usb_composite_dev *cdev;
  960. int status = -ENOMEM;
  961. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  962. if (!cdev)
  963. return status;
  964. spin_lock_init(&cdev->lock);
  965. cdev->gadget = gadget;
  966. set_gadget_data(gadget, cdev);
  967. INIT_LIST_HEAD(&cdev->configs);
  968. /* preallocate control response and buffer */
  969. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  970. if (!cdev->req)
  971. goto fail;
  972. cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
  973. if (!cdev->req->buf)
  974. goto fail;
  975. cdev->req->complete = composite_setup_complete;
  976. gadget->ep0->driver_data = cdev;
  977. cdev->bufsiz = USB_BUFSIZ;
  978. cdev->driver = composite;
  979. /*
  980. * As per USB compliance update, a device that is actively drawing
  981. * more than 100mA from USB must report itself as bus-powered in
  982. * the GetStatus(DEVICE) call.
  983. */
  984. if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
  985. usb_gadget_set_selfpowered(gadget);
  986. /* interface and string IDs start at zero via kzalloc.
  987. * we force endpoints to start unassigned; few controller
  988. * drivers will zero ep->driver_data.
  989. */
  990. usb_ep_autoconfig_reset(cdev->gadget);
  991. /* composite gadget needs to assign strings for whole device (like
  992. * serial number), register function drivers, potentially update
  993. * power state and consumption, etc
  994. */
  995. status = composite_gadget_bind(cdev);
  996. if (status < 0)
  997. goto fail;
  998. cdev->desc = *composite->dev;
  999. cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  1000. /* standardized runtime overrides for device ID data */
  1001. if (idVendor)
  1002. cdev->desc.idVendor = cpu_to_le16(idVendor);
  1003. if (idProduct)
  1004. cdev->desc.idProduct = cpu_to_le16(idProduct);
  1005. if (bcdDevice)
  1006. cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
  1007. /* string overrides */
  1008. if (iManufacturer || !cdev->desc.iManufacturer) {
  1009. if (!iManufacturer && !composite->iManufacturer &&
  1010. !*composite_manufacturer)
  1011. snprintf(composite_manufacturer,
  1012. sizeof composite_manufacturer,
  1013. "%s %s with %s",
  1014. init_utsname()->sysname,
  1015. init_utsname()->release,
  1016. gadget->name);
  1017. cdev->manufacturer_override =
  1018. override_id(cdev, &cdev->desc.iManufacturer);
  1019. }
  1020. if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
  1021. cdev->product_override =
  1022. override_id(cdev, &cdev->desc.iProduct);
  1023. if (iSerialNumber)
  1024. cdev->serial_override =
  1025. override_id(cdev, &cdev->desc.iSerialNumber);
  1026. /* has userspace failed to provide a serial number? */
  1027. if (composite->needs_serial && !cdev->desc.iSerialNumber)
  1028. WARNING(cdev, "userspace failed to provide iSerialNumber\n");
  1029. /* finish up */
  1030. status = device_create_file(&gadget->dev, &dev_attr_suspended);
  1031. if (status)
  1032. goto fail;
  1033. INFO(cdev, "%s ready\n", composite->name);
  1034. return 0;
  1035. fail:
  1036. composite_unbind(gadget);
  1037. return status;
  1038. }
  1039. /*-------------------------------------------------------------------------*/
  1040. static void
  1041. composite_suspend(struct usb_gadget *gadget)
  1042. {
  1043. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1044. struct usb_function *f;
  1045. /* REVISIT: should we have config level
  1046. * suspend/resume callbacks?
  1047. */
  1048. DBG(cdev, "suspend\n");
  1049. if (cdev->config) {
  1050. list_for_each_entry(f, &cdev->config->functions, list) {
  1051. if (f->suspend)
  1052. f->suspend(f);
  1053. }
  1054. }
  1055. if (composite->suspend)
  1056. composite->suspend(cdev);
  1057. cdev->suspended = 1;
  1058. usb_gadget_vbus_draw(gadget, 2);
  1059. }
  1060. static void
  1061. composite_resume(struct usb_gadget *gadget)
  1062. {
  1063. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1064. struct usb_function *f;
  1065. u8 maxpower;
  1066. /* REVISIT: should we have config level
  1067. * suspend/resume callbacks?
  1068. */
  1069. DBG(cdev, "resume\n");
  1070. if (composite->resume)
  1071. composite->resume(cdev);
  1072. if (cdev->config) {
  1073. list_for_each_entry(f, &cdev->config->functions, list) {
  1074. if (f->resume)
  1075. f->resume(f);
  1076. }
  1077. maxpower = cdev->config->bMaxPower;
  1078. usb_gadget_vbus_draw(gadget, maxpower ?
  1079. (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
  1080. }
  1081. cdev->suspended = 0;
  1082. }
  1083. /*-------------------------------------------------------------------------*/
  1084. static struct usb_gadget_driver composite_driver = {
  1085. .speed = USB_SPEED_HIGH,
  1086. .unbind = composite_unbind,
  1087. .setup = composite_setup,
  1088. .disconnect = composite_disconnect,
  1089. .suspend = composite_suspend,
  1090. .resume = composite_resume,
  1091. .driver = {
  1092. .owner = THIS_MODULE,
  1093. },
  1094. };
  1095. /**
  1096. * usb_composite_probe() - register a composite driver
  1097. * @driver: the driver to register
  1098. * @bind: the callback used to allocate resources that are shared across the
  1099. * whole device, such as string IDs, and add its configurations using
  1100. * @usb_add_config(). This may fail by returning a negative errno
  1101. * value; it should return zero on successful initialization.
  1102. * Context: single threaded during gadget setup
  1103. *
  1104. * This function is used to register drivers using the composite driver
  1105. * framework. The return value is zero, or a negative errno value.
  1106. * Those values normally come from the driver's @bind method, which does
  1107. * all the work of setting up the driver to match the hardware.
  1108. *
  1109. * On successful return, the gadget is ready to respond to requests from
  1110. * the host, unless one of its components invokes usb_gadget_disconnect()
  1111. * while it was binding. That would usually be done in order to wait for
  1112. * some userspace participation.
  1113. */
  1114. int usb_composite_probe(struct usb_composite_driver *driver,
  1115. int (*bind)(struct usb_composite_dev *cdev))
  1116. {
  1117. if (!driver || !driver->dev || !bind || composite)
  1118. return -EINVAL;
  1119. if (!driver->name)
  1120. driver->name = "composite";
  1121. if (!driver->iProduct)
  1122. driver->iProduct = driver->name;
  1123. composite_driver.function = (char *) driver->name;
  1124. composite_driver.driver.name = driver->name;
  1125. composite = driver;
  1126. composite_gadget_bind = bind;
  1127. return usb_gadget_probe_driver(&composite_driver, composite_bind);
  1128. }
  1129. /**
  1130. * usb_composite_unregister() - unregister a composite driver
  1131. * @driver: the driver to unregister
  1132. *
  1133. * This function is used to unregister drivers using the composite
  1134. * driver framework.
  1135. */
  1136. void usb_composite_unregister(struct usb_composite_driver *driver)
  1137. {
  1138. if (composite != driver)
  1139. return;
  1140. usb_gadget_unregister_driver(&composite_driver);
  1141. }
  1142. /**
  1143. * usb_composite_setup_continue() - Continue with the control transfer
  1144. * @cdev: the composite device who's control transfer was kept waiting
  1145. *
  1146. * This function must be called by the USB function driver to continue
  1147. * with the control transfer's data/status stage in case it had requested to
  1148. * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
  1149. * can request the composite framework to delay the setup request's data/status
  1150. * stages by returning USB_GADGET_DELAYED_STATUS.
  1151. */
  1152. void usb_composite_setup_continue(struct usb_composite_dev *cdev)
  1153. {
  1154. int value;
  1155. struct usb_request *req = cdev->req;
  1156. unsigned long flags;
  1157. DBG(cdev, "%s\n", __func__);
  1158. spin_lock_irqsave(&cdev->lock, flags);
  1159. if (cdev->delayed_status == 0) {
  1160. WARN(cdev, "%s: Unexpected call\n", __func__);
  1161. } else if (--cdev->delayed_status == 0) {
  1162. DBG(cdev, "%s: Completing delayed status\n", __func__);
  1163. req->length = 0;
  1164. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  1165. if (value < 0) {
  1166. DBG(cdev, "ep_queue --> %d\n", value);
  1167. req->status = 0;
  1168. composite_setup_complete(cdev->gadget->ep0, req);
  1169. }
  1170. }
  1171. spin_unlock_irqrestore(&cdev->lock, flags);
  1172. }