composite.c 45 KB

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