composite.c 46 KB

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