composite.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  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. memset(config->interface, '\0', sizeof(config->interface));
  452. status = bind(config);
  453. if (status < 0) {
  454. list_del(&config->list);
  455. config->cdev = NULL;
  456. } else {
  457. unsigned i;
  458. DBG(cdev, "cfg %d/%p speeds:%s%s\n",
  459. config->bConfigurationValue, config,
  460. config->highspeed ? " high" : "",
  461. config->fullspeed
  462. ? (gadget_is_dualspeed(cdev->gadget)
  463. ? " full"
  464. : " full/low")
  465. : "");
  466. for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
  467. struct usb_function *f = config->interface[i];
  468. if (!f)
  469. continue;
  470. DBG(cdev, " interface %d = %s/%p\n",
  471. i, f->name, f);
  472. }
  473. }
  474. /* set_alt(), or next bind(), sets up
  475. * ep->driver_data as needed.
  476. */
  477. usb_ep_autoconfig_reset(cdev->gadget);
  478. done:
  479. if (status)
  480. DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
  481. config->bConfigurationValue, status);
  482. return status;
  483. }
  484. static int remove_config(struct usb_composite_dev *cdev,
  485. struct usb_configuration *config)
  486. {
  487. while (!list_empty(&config->functions)) {
  488. struct usb_function *f;
  489. f = list_first_entry(&config->functions,
  490. struct usb_function, list);
  491. list_del(&f->list);
  492. if (f->unbind) {
  493. DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
  494. f->unbind(config, f);
  495. /* may free memory for "f" */
  496. }
  497. }
  498. list_del(&config->list);
  499. if (config->unbind) {
  500. DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
  501. config->unbind(config);
  502. /* may free memory for "c" */
  503. }
  504. return 0;
  505. }
  506. int usb_remove_config(struct usb_composite_dev *cdev,
  507. struct usb_configuration *config)
  508. {
  509. unsigned long flags;
  510. spin_lock_irqsave(&cdev->lock, flags);
  511. if (cdev->config == config)
  512. reset_config(cdev);
  513. spin_unlock_irqrestore(&cdev->lock, flags);
  514. return remove_config(cdev, config);
  515. }
  516. /*-------------------------------------------------------------------------*/
  517. /* We support strings in multiple languages ... string descriptor zero
  518. * says which languages are supported. The typical case will be that
  519. * only one language (probably English) is used, with I18N handled on
  520. * the host side.
  521. */
  522. static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
  523. {
  524. const struct usb_gadget_strings *s;
  525. u16 language;
  526. __le16 *tmp;
  527. while (*sp) {
  528. s = *sp;
  529. language = cpu_to_le16(s->language);
  530. for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
  531. if (*tmp == language)
  532. goto repeat;
  533. }
  534. *tmp++ = language;
  535. repeat:
  536. sp++;
  537. }
  538. }
  539. static int lookup_string(
  540. struct usb_gadget_strings **sp,
  541. void *buf,
  542. u16 language,
  543. int id
  544. )
  545. {
  546. struct usb_gadget_strings *s;
  547. int value;
  548. while (*sp) {
  549. s = *sp++;
  550. if (s->language != language)
  551. continue;
  552. value = usb_gadget_get_string(s, id, buf);
  553. if (value > 0)
  554. return value;
  555. }
  556. return -EINVAL;
  557. }
  558. static int get_string(struct usb_composite_dev *cdev,
  559. void *buf, u16 language, int id)
  560. {
  561. struct usb_configuration *c;
  562. struct usb_function *f;
  563. int len;
  564. const char *str;
  565. /* Yes, not only is USB's I18N support probably more than most
  566. * folk will ever care about ... also, it's all supported here.
  567. * (Except for UTF8 support for Unicode's "Astral Planes".)
  568. */
  569. /* 0 == report all available language codes */
  570. if (id == 0) {
  571. struct usb_string_descriptor *s = buf;
  572. struct usb_gadget_strings **sp;
  573. memset(s, 0, 256);
  574. s->bDescriptorType = USB_DT_STRING;
  575. sp = composite->strings;
  576. if (sp)
  577. collect_langs(sp, s->wData);
  578. list_for_each_entry(c, &cdev->configs, list) {
  579. sp = c->strings;
  580. if (sp)
  581. collect_langs(sp, s->wData);
  582. list_for_each_entry(f, &c->functions, list) {
  583. sp = f->strings;
  584. if (sp)
  585. collect_langs(sp, s->wData);
  586. }
  587. }
  588. for (len = 0; len <= 126 && s->wData[len]; len++)
  589. continue;
  590. if (!len)
  591. return -EINVAL;
  592. s->bLength = 2 * (len + 1);
  593. return s->bLength;
  594. }
  595. /* Otherwise, look up and return a specified string. First
  596. * check if the string has not been overridden.
  597. */
  598. if (cdev->manufacturer_override == id)
  599. str = iManufacturer ?: composite->iManufacturer ?:
  600. composite_manufacturer;
  601. else if (cdev->product_override == id)
  602. str = iProduct ?: composite->iProduct;
  603. else if (cdev->serial_override == id)
  604. str = iSerialNumber;
  605. else
  606. str = NULL;
  607. if (str) {
  608. struct usb_gadget_strings strings = {
  609. .language = language,
  610. .strings = &(struct usb_string) { 0xff, str }
  611. };
  612. return usb_gadget_get_string(&strings, 0xff, buf);
  613. }
  614. /* String IDs are device-scoped, so we look up each string
  615. * table we're told about. These lookups are infrequent;
  616. * simpler-is-better here.
  617. */
  618. if (composite->strings) {
  619. len = lookup_string(composite->strings, buf, language, id);
  620. if (len > 0)
  621. return len;
  622. }
  623. list_for_each_entry(c, &cdev->configs, list) {
  624. if (c->strings) {
  625. len = lookup_string(c->strings, buf, language, id);
  626. if (len > 0)
  627. return len;
  628. }
  629. list_for_each_entry(f, &c->functions, list) {
  630. if (!f->strings)
  631. continue;
  632. len = lookup_string(f->strings, buf, language, id);
  633. if (len > 0)
  634. return len;
  635. }
  636. }
  637. return -EINVAL;
  638. }
  639. /**
  640. * usb_string_id() - allocate an unused string ID
  641. * @cdev: the device whose string descriptor IDs are being allocated
  642. * Context: single threaded during gadget setup
  643. *
  644. * @usb_string_id() is called from bind() callbacks to allocate
  645. * string IDs. Drivers for functions, configurations, or gadgets will
  646. * then store that ID in the appropriate descriptors and string table.
  647. *
  648. * All string identifier should be allocated using this,
  649. * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
  650. * that for example different functions don't wrongly assign different
  651. * meanings to the same identifier.
  652. */
  653. int usb_string_id(struct usb_composite_dev *cdev)
  654. {
  655. if (cdev->next_string_id < 254) {
  656. /* string id 0 is reserved by USB spec for list of
  657. * supported languages */
  658. /* 255 reserved as well? -- mina86 */
  659. cdev->next_string_id++;
  660. return cdev->next_string_id;
  661. }
  662. return -ENODEV;
  663. }
  664. /**
  665. * usb_string_ids() - allocate unused string IDs in batch
  666. * @cdev: the device whose string descriptor IDs are being allocated
  667. * @str: an array of usb_string objects to assign numbers to
  668. * Context: single threaded during gadget setup
  669. *
  670. * @usb_string_ids() is called from bind() callbacks to allocate
  671. * string IDs. Drivers for functions, configurations, or gadgets will
  672. * then copy IDs from the string table to the appropriate descriptors
  673. * and string table for other languages.
  674. *
  675. * All string identifier should be allocated using this,
  676. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  677. * example different functions don't wrongly assign different meanings
  678. * to the same identifier.
  679. */
  680. int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
  681. {
  682. int next = cdev->next_string_id;
  683. for (; str->s; ++str) {
  684. if (unlikely(next >= 254))
  685. return -ENODEV;
  686. str->id = ++next;
  687. }
  688. cdev->next_string_id = next;
  689. return 0;
  690. }
  691. /**
  692. * usb_string_ids_n() - allocate unused string IDs in batch
  693. * @c: the device whose string descriptor IDs are being allocated
  694. * @n: number of string IDs to allocate
  695. * Context: single threaded during gadget setup
  696. *
  697. * Returns the first requested ID. This ID and next @n-1 IDs are now
  698. * valid IDs. At least provided that @n is non-zero because if it
  699. * is, returns last requested ID which is now very useful information.
  700. *
  701. * @usb_string_ids_n() is called from bind() callbacks to allocate
  702. * string IDs. Drivers for functions, configurations, or gadgets will
  703. * then store that ID in the appropriate descriptors and string table.
  704. *
  705. * All string identifier should be allocated using this,
  706. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  707. * example different functions don't wrongly assign different meanings
  708. * to the same identifier.
  709. */
  710. int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
  711. {
  712. unsigned next = c->next_string_id;
  713. if (unlikely(n > 254 || (unsigned)next + n > 254))
  714. return -ENODEV;
  715. c->next_string_id += n;
  716. return next + 1;
  717. }
  718. /*-------------------------------------------------------------------------*/
  719. static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
  720. {
  721. if (req->status || req->actual != req->length)
  722. DBG((struct usb_composite_dev *) ep->driver_data,
  723. "setup complete --> %d, %d/%d\n",
  724. req->status, req->actual, req->length);
  725. }
  726. /*
  727. * The setup() callback implements all the ep0 functionality that's
  728. * not handled lower down, in hardware or the hardware driver(like
  729. * device and endpoint feature flags, and their status). It's all
  730. * housekeeping for the gadget function we're implementing. Most of
  731. * the work is in config and function specific setup.
  732. */
  733. static int
  734. composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  735. {
  736. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  737. struct usb_request *req = cdev->req;
  738. int value = -EOPNOTSUPP;
  739. u16 w_index = le16_to_cpu(ctrl->wIndex);
  740. u8 intf = w_index & 0xFF;
  741. u16 w_value = le16_to_cpu(ctrl->wValue);
  742. u16 w_length = le16_to_cpu(ctrl->wLength);
  743. struct usb_function *f = NULL;
  744. u8 endp;
  745. /* partial re-init of the response message; the function or the
  746. * gadget might need to intercept e.g. a control-OUT completion
  747. * when we delegate to it.
  748. */
  749. req->zero = 0;
  750. req->complete = composite_setup_complete;
  751. req->length = 0;
  752. gadget->ep0->driver_data = cdev;
  753. switch (ctrl->bRequest) {
  754. /* we handle all standard USB descriptors */
  755. case USB_REQ_GET_DESCRIPTOR:
  756. if (ctrl->bRequestType != USB_DIR_IN)
  757. goto unknown;
  758. switch (w_value >> 8) {
  759. case USB_DT_DEVICE:
  760. cdev->desc.bNumConfigurations =
  761. count_configs(cdev, USB_DT_DEVICE);
  762. value = min(w_length, (u16) sizeof cdev->desc);
  763. memcpy(req->buf, &cdev->desc, value);
  764. break;
  765. case USB_DT_DEVICE_QUALIFIER:
  766. if (!gadget_is_dualspeed(gadget))
  767. break;
  768. device_qual(cdev);
  769. value = min_t(int, w_length,
  770. sizeof(struct usb_qualifier_descriptor));
  771. break;
  772. case USB_DT_OTHER_SPEED_CONFIG:
  773. if (!gadget_is_dualspeed(gadget))
  774. break;
  775. /* FALLTHROUGH */
  776. case USB_DT_CONFIG:
  777. value = config_desc(cdev, w_value);
  778. if (value >= 0)
  779. value = min(w_length, (u16) value);
  780. break;
  781. case USB_DT_STRING:
  782. value = get_string(cdev, req->buf,
  783. w_index, w_value & 0xff);
  784. if (value >= 0)
  785. value = min(w_length, (u16) value);
  786. break;
  787. }
  788. break;
  789. /* any number of configs can work */
  790. case USB_REQ_SET_CONFIGURATION:
  791. if (ctrl->bRequestType != 0)
  792. goto unknown;
  793. if (gadget_is_otg(gadget)) {
  794. if (gadget->a_hnp_support)
  795. DBG(cdev, "HNP available\n");
  796. else if (gadget->a_alt_hnp_support)
  797. DBG(cdev, "HNP on another port\n");
  798. else
  799. VDBG(cdev, "HNP inactive\n");
  800. }
  801. spin_lock(&cdev->lock);
  802. value = set_config(cdev, ctrl, w_value);
  803. spin_unlock(&cdev->lock);
  804. break;
  805. case USB_REQ_GET_CONFIGURATION:
  806. if (ctrl->bRequestType != USB_DIR_IN)
  807. goto unknown;
  808. if (cdev->config)
  809. *(u8 *)req->buf = cdev->config->bConfigurationValue;
  810. else
  811. *(u8 *)req->buf = 0;
  812. value = min(w_length, (u16) 1);
  813. break;
  814. /* function drivers must handle get/set altsetting; if there's
  815. * no get() method, we know only altsetting zero works.
  816. */
  817. case USB_REQ_SET_INTERFACE:
  818. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  819. goto unknown;
  820. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  821. break;
  822. f = cdev->config->interface[intf];
  823. if (!f)
  824. break;
  825. if (w_value && !f->set_alt)
  826. break;
  827. value = f->set_alt(f, w_index, w_value);
  828. if (value == USB_GADGET_DELAYED_STATUS) {
  829. DBG(cdev,
  830. "%s: interface %d (%s) requested delayed status\n",
  831. __func__, intf, f->name);
  832. cdev->delayed_status++;
  833. DBG(cdev, "delayed_status count %d\n",
  834. cdev->delayed_status);
  835. }
  836. break;
  837. case USB_REQ_GET_INTERFACE:
  838. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  839. goto unknown;
  840. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  841. break;
  842. f = cdev->config->interface[intf];
  843. if (!f)
  844. break;
  845. /* lots of interfaces only need altsetting zero... */
  846. value = f->get_alt ? f->get_alt(f, w_index) : 0;
  847. if (value < 0)
  848. break;
  849. *((u8 *)req->buf) = value;
  850. value = min(w_length, (u16) 1);
  851. break;
  852. default:
  853. unknown:
  854. VDBG(cdev,
  855. "non-core control req%02x.%02x v%04x i%04x l%d\n",
  856. ctrl->bRequestType, ctrl->bRequest,
  857. w_value, w_index, w_length);
  858. /* functions always handle their interfaces and endpoints...
  859. * punt other recipients (other, WUSB, ...) to the current
  860. * configuration code.
  861. *
  862. * REVISIT it could make sense to let the composite device
  863. * take such requests too, if that's ever needed: to work
  864. * in config 0, etc.
  865. */
  866. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  867. case USB_RECIP_INTERFACE:
  868. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  869. break;
  870. f = cdev->config->interface[intf];
  871. break;
  872. case USB_RECIP_ENDPOINT:
  873. endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
  874. list_for_each_entry(f, &cdev->config->functions, list) {
  875. if (test_bit(endp, f->endpoints))
  876. break;
  877. }
  878. if (&f->list == &cdev->config->functions)
  879. f = NULL;
  880. break;
  881. }
  882. if (f && f->setup)
  883. value = f->setup(f, ctrl);
  884. else {
  885. struct usb_configuration *c;
  886. c = cdev->config;
  887. if (c && c->setup)
  888. value = c->setup(c, ctrl);
  889. }
  890. goto done;
  891. }
  892. /* respond with data transfer before status phase? */
  893. if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
  894. req->length = value;
  895. req->zero = value < w_length;
  896. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  897. if (value < 0) {
  898. DBG(cdev, "ep_queue --> %d\n", value);
  899. req->status = 0;
  900. composite_setup_complete(gadget->ep0, req);
  901. }
  902. } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
  903. WARN(cdev,
  904. "%s: Delayed status not supported for w_length != 0",
  905. __func__);
  906. }
  907. done:
  908. /* device either stalls (value < 0) or reports success */
  909. return value;
  910. }
  911. static void composite_disconnect(struct usb_gadget *gadget)
  912. {
  913. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  914. unsigned long flags;
  915. /* REVISIT: should we have config and device level
  916. * disconnect callbacks?
  917. */
  918. spin_lock_irqsave(&cdev->lock, flags);
  919. if (cdev->config)
  920. reset_config(cdev);
  921. if (composite->disconnect)
  922. composite->disconnect(cdev);
  923. spin_unlock_irqrestore(&cdev->lock, flags);
  924. }
  925. /*-------------------------------------------------------------------------*/
  926. static ssize_t composite_show_suspended(struct device *dev,
  927. struct device_attribute *attr,
  928. char *buf)
  929. {
  930. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  931. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  932. return sprintf(buf, "%d\n", cdev->suspended);
  933. }
  934. static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
  935. static void
  936. composite_unbind(struct usb_gadget *gadget)
  937. {
  938. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  939. /* composite_disconnect() must already have been called
  940. * by the underlying peripheral controller driver!
  941. * so there's no i/o concurrency that could affect the
  942. * state protected by cdev->lock.
  943. */
  944. WARN_ON(cdev->config);
  945. while (!list_empty(&cdev->configs)) {
  946. struct usb_configuration *c;
  947. c = list_first_entry(&cdev->configs,
  948. struct usb_configuration, list);
  949. remove_config(cdev, c);
  950. }
  951. if (composite->unbind)
  952. composite->unbind(cdev);
  953. if (cdev->req) {
  954. kfree(cdev->req->buf);
  955. usb_ep_free_request(gadget->ep0, cdev->req);
  956. }
  957. device_remove_file(&gadget->dev, &dev_attr_suspended);
  958. kfree(cdev);
  959. set_gadget_data(gadget, NULL);
  960. composite = NULL;
  961. }
  962. static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
  963. {
  964. if (!*desc) {
  965. int ret = usb_string_id(cdev);
  966. if (unlikely(ret < 0))
  967. WARNING(cdev, "failed to override string ID\n");
  968. else
  969. *desc = ret;
  970. }
  971. return *desc;
  972. }
  973. static int composite_bind(struct usb_gadget *gadget)
  974. {
  975. struct usb_composite_dev *cdev;
  976. int status = -ENOMEM;
  977. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  978. if (!cdev)
  979. return status;
  980. spin_lock_init(&cdev->lock);
  981. cdev->gadget = gadget;
  982. set_gadget_data(gadget, cdev);
  983. INIT_LIST_HEAD(&cdev->configs);
  984. /* preallocate control response and buffer */
  985. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  986. if (!cdev->req)
  987. goto fail;
  988. cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
  989. if (!cdev->req->buf)
  990. goto fail;
  991. cdev->req->complete = composite_setup_complete;
  992. gadget->ep0->driver_data = cdev;
  993. cdev->bufsiz = USB_BUFSIZ;
  994. cdev->driver = composite;
  995. /*
  996. * As per USB compliance update, a device that is actively drawing
  997. * more than 100mA from USB must report itself as bus-powered in
  998. * the GetStatus(DEVICE) call.
  999. */
  1000. if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
  1001. usb_gadget_set_selfpowered(gadget);
  1002. /* interface and string IDs start at zero via kzalloc.
  1003. * we force endpoints to start unassigned; few controller
  1004. * drivers will zero ep->driver_data.
  1005. */
  1006. usb_ep_autoconfig_reset(cdev->gadget);
  1007. /* composite gadget needs to assign strings for whole device (like
  1008. * serial number), register function drivers, potentially update
  1009. * power state and consumption, etc
  1010. */
  1011. status = composite_gadget_bind(cdev);
  1012. if (status < 0)
  1013. goto fail;
  1014. cdev->desc = *composite->dev;
  1015. cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  1016. /* standardized runtime overrides for device ID data */
  1017. if (idVendor)
  1018. cdev->desc.idVendor = cpu_to_le16(idVendor);
  1019. if (idProduct)
  1020. cdev->desc.idProduct = cpu_to_le16(idProduct);
  1021. if (bcdDevice)
  1022. cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
  1023. /* string overrides */
  1024. if (iManufacturer || !cdev->desc.iManufacturer) {
  1025. if (!iManufacturer && !composite->iManufacturer &&
  1026. !*composite_manufacturer)
  1027. snprintf(composite_manufacturer,
  1028. sizeof composite_manufacturer,
  1029. "%s %s with %s",
  1030. init_utsname()->sysname,
  1031. init_utsname()->release,
  1032. gadget->name);
  1033. cdev->manufacturer_override =
  1034. override_id(cdev, &cdev->desc.iManufacturer);
  1035. }
  1036. if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
  1037. cdev->product_override =
  1038. override_id(cdev, &cdev->desc.iProduct);
  1039. if (iSerialNumber)
  1040. cdev->serial_override =
  1041. override_id(cdev, &cdev->desc.iSerialNumber);
  1042. /* has userspace failed to provide a serial number? */
  1043. if (composite->needs_serial && !cdev->desc.iSerialNumber)
  1044. WARNING(cdev, "userspace failed to provide iSerialNumber\n");
  1045. /* finish up */
  1046. status = device_create_file(&gadget->dev, &dev_attr_suspended);
  1047. if (status)
  1048. goto fail;
  1049. INFO(cdev, "%s ready\n", composite->name);
  1050. return 0;
  1051. fail:
  1052. composite_unbind(gadget);
  1053. return status;
  1054. }
  1055. /*-------------------------------------------------------------------------*/
  1056. static void
  1057. composite_suspend(struct usb_gadget *gadget)
  1058. {
  1059. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1060. struct usb_function *f;
  1061. /* REVISIT: should we have config level
  1062. * suspend/resume callbacks?
  1063. */
  1064. DBG(cdev, "suspend\n");
  1065. if (cdev->config) {
  1066. list_for_each_entry(f, &cdev->config->functions, list) {
  1067. if (f->suspend)
  1068. f->suspend(f);
  1069. }
  1070. }
  1071. if (composite->suspend)
  1072. composite->suspend(cdev);
  1073. cdev->suspended = 1;
  1074. usb_gadget_vbus_draw(gadget, 2);
  1075. }
  1076. static void
  1077. composite_resume(struct usb_gadget *gadget)
  1078. {
  1079. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1080. struct usb_function *f;
  1081. u8 maxpower;
  1082. /* REVISIT: should we have config level
  1083. * suspend/resume callbacks?
  1084. */
  1085. DBG(cdev, "resume\n");
  1086. if (composite->resume)
  1087. composite->resume(cdev);
  1088. if (cdev->config) {
  1089. list_for_each_entry(f, &cdev->config->functions, list) {
  1090. if (f->resume)
  1091. f->resume(f);
  1092. }
  1093. maxpower = cdev->config->bMaxPower;
  1094. usb_gadget_vbus_draw(gadget, maxpower ?
  1095. (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
  1096. }
  1097. cdev->suspended = 0;
  1098. }
  1099. /*-------------------------------------------------------------------------*/
  1100. static struct usb_gadget_driver composite_driver = {
  1101. .speed = USB_SPEED_HIGH,
  1102. .unbind = composite_unbind,
  1103. .setup = composite_setup,
  1104. .disconnect = composite_disconnect,
  1105. .suspend = composite_suspend,
  1106. .resume = composite_resume,
  1107. .driver = {
  1108. .owner = THIS_MODULE,
  1109. },
  1110. };
  1111. /**
  1112. * usb_composite_probe() - register a composite driver
  1113. * @driver: the driver to register
  1114. * @bind: the callback used to allocate resources that are shared across the
  1115. * whole device, such as string IDs, and add its configurations using
  1116. * @usb_add_config(). This may fail by returning a negative errno
  1117. * value; it should return zero on successful initialization.
  1118. * Context: single threaded during gadget setup
  1119. *
  1120. * This function is used to register drivers using the composite driver
  1121. * framework. The return value is zero, or a negative errno value.
  1122. * Those values normally come from the driver's @bind method, which does
  1123. * all the work of setting up the driver to match the hardware.
  1124. *
  1125. * On successful return, the gadget is ready to respond to requests from
  1126. * the host, unless one of its components invokes usb_gadget_disconnect()
  1127. * while it was binding. That would usually be done in order to wait for
  1128. * some userspace participation.
  1129. */
  1130. int usb_composite_probe(struct usb_composite_driver *driver,
  1131. int (*bind)(struct usb_composite_dev *cdev))
  1132. {
  1133. if (!driver || !driver->dev || !bind || composite)
  1134. return -EINVAL;
  1135. if (!driver->name)
  1136. driver->name = "composite";
  1137. if (!driver->iProduct)
  1138. driver->iProduct = driver->name;
  1139. composite_driver.function = (char *) driver->name;
  1140. composite_driver.driver.name = driver->name;
  1141. composite = driver;
  1142. composite_gadget_bind = bind;
  1143. return usb_gadget_probe_driver(&composite_driver, composite_bind);
  1144. }
  1145. /**
  1146. * usb_composite_unregister() - unregister a composite driver
  1147. * @driver: the driver to unregister
  1148. *
  1149. * This function is used to unregister drivers using the composite
  1150. * driver framework.
  1151. */
  1152. void usb_composite_unregister(struct usb_composite_driver *driver)
  1153. {
  1154. if (composite != driver)
  1155. return;
  1156. usb_gadget_unregister_driver(&composite_driver);
  1157. }
  1158. /**
  1159. * usb_composite_setup_continue() - Continue with the control transfer
  1160. * @cdev: the composite device who's control transfer was kept waiting
  1161. *
  1162. * This function must be called by the USB function driver to continue
  1163. * with the control transfer's data/status stage in case it had requested to
  1164. * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
  1165. * can request the composite framework to delay the setup request's data/status
  1166. * stages by returning USB_GADGET_DELAYED_STATUS.
  1167. */
  1168. void usb_composite_setup_continue(struct usb_composite_dev *cdev)
  1169. {
  1170. int value;
  1171. struct usb_request *req = cdev->req;
  1172. unsigned long flags;
  1173. DBG(cdev, "%s\n", __func__);
  1174. spin_lock_irqsave(&cdev->lock, flags);
  1175. if (cdev->delayed_status == 0) {
  1176. WARN(cdev, "%s: Unexpected call\n", __func__);
  1177. } else if (--cdev->delayed_status == 0) {
  1178. DBG(cdev, "%s: Completing delayed status\n", __func__);
  1179. req->length = 0;
  1180. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  1181. if (value < 0) {
  1182. DBG(cdev, "ep_queue --> %d\n", value);
  1183. req->status = 0;
  1184. composite_setup_complete(cdev->gadget->ep0, req);
  1185. }
  1186. }
  1187. spin_unlock_irqrestore(&cdev->lock, flags);
  1188. }