composite.c 46 KB

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