composite.c 39 KB

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