composite.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  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. /**
  27. * next_ep_desc() - advance to the next EP descriptor
  28. * @t: currect pointer within descriptor array
  29. *
  30. * Return: next EP descriptor or NULL
  31. *
  32. * Iterate over @t until either EP descriptor found or
  33. * NULL (that indicates end of list) encountered
  34. */
  35. static struct usb_descriptor_header**
  36. next_ep_desc(struct usb_descriptor_header **t)
  37. {
  38. for (; *t; t++) {
  39. if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
  40. return t;
  41. }
  42. return NULL;
  43. }
  44. /*
  45. * for_each_ep_desc()- iterate over endpoint descriptors in the
  46. * descriptors list
  47. * @start: pointer within descriptor array.
  48. * @ep_desc: endpoint descriptor to use as the loop cursor
  49. */
  50. #define for_each_ep_desc(start, ep_desc) \
  51. for (ep_desc = next_ep_desc(start); \
  52. ep_desc; ep_desc = next_ep_desc(ep_desc+1))
  53. /**
  54. * config_ep_by_speed() - configures the given endpoint
  55. * according to gadget speed.
  56. * @g: pointer to the gadget
  57. * @f: usb function
  58. * @_ep: the endpoint to configure
  59. *
  60. * Return: error code, 0 on success
  61. *
  62. * This function chooses the right descriptors for a given
  63. * endpoint according to gadget speed and saves it in the
  64. * endpoint desc field. If the endpoint already has a descriptor
  65. * assigned to it - overwrites it with currently corresponding
  66. * descriptor. The endpoint maxpacket field is updated according
  67. * to the chosen descriptor.
  68. * Note: the supplied function should hold all the descriptors
  69. * for supported speeds
  70. */
  71. int config_ep_by_speed(struct usb_gadget *g,
  72. struct usb_function *f,
  73. struct usb_ep *_ep)
  74. {
  75. struct usb_composite_dev *cdev = get_gadget_data(g);
  76. struct usb_endpoint_descriptor *chosen_desc = NULL;
  77. struct usb_descriptor_header **speed_desc = NULL;
  78. struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
  79. int want_comp_desc = 0;
  80. struct usb_descriptor_header **d_spd; /* cursor for speed desc */
  81. if (!g || !f || !_ep)
  82. return -EIO;
  83. /* select desired speed */
  84. switch (g->speed) {
  85. case USB_SPEED_SUPER:
  86. if (gadget_is_superspeed(g)) {
  87. speed_desc = f->ss_descriptors;
  88. want_comp_desc = 1;
  89. break;
  90. }
  91. /* else: Fall trough */
  92. case USB_SPEED_HIGH:
  93. if (gadget_is_dualspeed(g)) {
  94. speed_desc = f->hs_descriptors;
  95. break;
  96. }
  97. /* else: fall through */
  98. default:
  99. speed_desc = f->fs_descriptors;
  100. }
  101. /* find descriptors */
  102. for_each_ep_desc(speed_desc, d_spd) {
  103. chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
  104. if (chosen_desc->bEndpointAddress == _ep->address)
  105. goto ep_found;
  106. }
  107. return -EIO;
  108. ep_found:
  109. /* commit results */
  110. _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
  111. _ep->desc = chosen_desc;
  112. _ep->comp_desc = NULL;
  113. _ep->maxburst = 0;
  114. _ep->mult = 0;
  115. if (!want_comp_desc)
  116. return 0;
  117. /*
  118. * Companion descriptor should follow EP descriptor
  119. * USB 3.0 spec, #9.6.7
  120. */
  121. comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
  122. if (!comp_desc ||
  123. (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
  124. return -EIO;
  125. _ep->comp_desc = comp_desc;
  126. if (g->speed == USB_SPEED_SUPER) {
  127. switch (usb_endpoint_type(_ep->desc)) {
  128. case USB_ENDPOINT_XFER_ISOC:
  129. /* mult: bits 1:0 of bmAttributes */
  130. _ep->mult = comp_desc->bmAttributes & 0x3;
  131. case USB_ENDPOINT_XFER_BULK:
  132. case USB_ENDPOINT_XFER_INT:
  133. _ep->maxburst = comp_desc->bMaxBurst + 1;
  134. break;
  135. default:
  136. if (comp_desc->bMaxBurst != 0)
  137. ERROR(cdev, "ep0 bMaxBurst must be 0\n");
  138. _ep->maxburst = 1;
  139. break;
  140. }
  141. }
  142. return 0;
  143. }
  144. EXPORT_SYMBOL_GPL(config_ep_by_speed);
  145. /**
  146. * usb_add_function() - add a function to a configuration
  147. * @config: the configuration
  148. * @function: the function being added
  149. * Context: single threaded during gadget setup
  150. *
  151. * After initialization, each configuration must have one or more
  152. * functions added to it. Adding a function involves calling its @bind()
  153. * method to allocate resources such as interface and string identifiers
  154. * and endpoints.
  155. *
  156. * This function returns the value of the function's bind(), which is
  157. * zero for success else a negative errno value.
  158. */
  159. int usb_add_function(struct usb_configuration *config,
  160. struct usb_function *function)
  161. {
  162. int value = -EINVAL;
  163. DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
  164. function->name, function,
  165. config->label, config);
  166. if (!function->set_alt || !function->disable)
  167. goto done;
  168. function->config = config;
  169. list_add_tail(&function->list, &config->functions);
  170. /* REVISIT *require* function->bind? */
  171. if (function->bind) {
  172. value = function->bind(config, function);
  173. if (value < 0) {
  174. list_del(&function->list);
  175. function->config = NULL;
  176. }
  177. } else
  178. value = 0;
  179. /* We allow configurations that don't work at both speeds.
  180. * If we run into a lowspeed Linux system, treat it the same
  181. * as full speed ... it's the function drivers that will need
  182. * to avoid bulk and ISO transfers.
  183. */
  184. if (!config->fullspeed && function->fs_descriptors)
  185. config->fullspeed = true;
  186. if (!config->highspeed && function->hs_descriptors)
  187. config->highspeed = true;
  188. if (!config->superspeed && function->ss_descriptors)
  189. config->superspeed = true;
  190. done:
  191. if (value)
  192. DBG(config->cdev, "adding '%s'/%p --> %d\n",
  193. function->name, function, value);
  194. return value;
  195. }
  196. EXPORT_SYMBOL_GPL(usb_add_function);
  197. void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
  198. {
  199. if (f->disable)
  200. f->disable(f);
  201. bitmap_zero(f->endpoints, 32);
  202. list_del(&f->list);
  203. if (f->unbind)
  204. f->unbind(c, f);
  205. }
  206. EXPORT_SYMBOL_GPL(usb_remove_function);
  207. /**
  208. * usb_function_deactivate - prevent function and gadget enumeration
  209. * @function: the function that isn't yet ready to respond
  210. *
  211. * Blocks response of the gadget driver to host enumeration by
  212. * preventing the data line pullup from being activated. This is
  213. * normally called during @bind() processing to change from the
  214. * initial "ready to respond" state, or when a required resource
  215. * becomes available.
  216. *
  217. * For example, drivers that serve as a passthrough to a userspace
  218. * daemon can block enumeration unless that daemon (such as an OBEX,
  219. * MTP, or print server) is ready to handle host requests.
  220. *
  221. * Not all systems support software control of their USB peripheral
  222. * data pullups.
  223. *
  224. * Returns zero on success, else negative errno.
  225. */
  226. int usb_function_deactivate(struct usb_function *function)
  227. {
  228. struct usb_composite_dev *cdev = function->config->cdev;
  229. unsigned long flags;
  230. int status = 0;
  231. spin_lock_irqsave(&cdev->lock, flags);
  232. if (cdev->deactivations == 0)
  233. status = usb_gadget_disconnect(cdev->gadget);
  234. if (status == 0)
  235. cdev->deactivations++;
  236. spin_unlock_irqrestore(&cdev->lock, flags);
  237. return status;
  238. }
  239. EXPORT_SYMBOL_GPL(usb_function_deactivate);
  240. /**
  241. * usb_function_activate - allow function and gadget enumeration
  242. * @function: function on which usb_function_activate() was called
  243. *
  244. * Reverses effect of usb_function_deactivate(). If no more functions
  245. * are delaying their activation, the gadget driver will respond to
  246. * host enumeration procedures.
  247. *
  248. * Returns zero on success, else negative errno.
  249. */
  250. int usb_function_activate(struct usb_function *function)
  251. {
  252. struct usb_composite_dev *cdev = function->config->cdev;
  253. unsigned long flags;
  254. int status = 0;
  255. spin_lock_irqsave(&cdev->lock, flags);
  256. if (WARN_ON(cdev->deactivations == 0))
  257. status = -EINVAL;
  258. else {
  259. cdev->deactivations--;
  260. if (cdev->deactivations == 0)
  261. status = usb_gadget_connect(cdev->gadget);
  262. }
  263. spin_unlock_irqrestore(&cdev->lock, flags);
  264. return status;
  265. }
  266. EXPORT_SYMBOL_GPL(usb_function_activate);
  267. /**
  268. * usb_interface_id() - allocate an unused interface ID
  269. * @config: configuration associated with the interface
  270. * @function: function handling the interface
  271. * Context: single threaded during gadget setup
  272. *
  273. * usb_interface_id() is called from usb_function.bind() callbacks to
  274. * allocate new interface IDs. The function driver will then store that
  275. * ID in interface, association, CDC union, and other descriptors. It
  276. * will also handle any control requests targeted at that interface,
  277. * particularly changing its altsetting via set_alt(). There may
  278. * also be class-specific or vendor-specific requests to handle.
  279. *
  280. * All interface identifier should be allocated using this routine, to
  281. * ensure that for example different functions don't wrongly assign
  282. * different meanings to the same identifier. Note that since interface
  283. * identifiers are configuration-specific, functions used in more than
  284. * one configuration (or more than once in a given configuration) need
  285. * multiple versions of the relevant descriptors.
  286. *
  287. * Returns the interface ID which was allocated; or -ENODEV if no
  288. * more interface IDs can be allocated.
  289. */
  290. int usb_interface_id(struct usb_configuration *config,
  291. struct usb_function *function)
  292. {
  293. unsigned id = config->next_interface_id;
  294. if (id < MAX_CONFIG_INTERFACES) {
  295. config->interface[id] = function;
  296. config->next_interface_id = id + 1;
  297. return id;
  298. }
  299. return -ENODEV;
  300. }
  301. EXPORT_SYMBOL_GPL(usb_interface_id);
  302. static u8 encode_bMaxPower(enum usb_device_speed speed,
  303. struct usb_configuration *c)
  304. {
  305. unsigned val;
  306. if (c->MaxPower)
  307. val = c->MaxPower;
  308. else
  309. val = CONFIG_USB_GADGET_VBUS_DRAW;
  310. if (!val)
  311. return 0;
  312. switch (speed) {
  313. case USB_SPEED_SUPER:
  314. return DIV_ROUND_UP(val, 8);
  315. default:
  316. return DIV_ROUND_UP(val, 2);
  317. };
  318. }
  319. static int config_buf(struct usb_configuration *config,
  320. enum usb_device_speed speed, void *buf, u8 type)
  321. {
  322. struct usb_config_descriptor *c = buf;
  323. void *next = buf + USB_DT_CONFIG_SIZE;
  324. int len;
  325. struct usb_function *f;
  326. int status;
  327. len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
  328. /* write the config descriptor */
  329. c = buf;
  330. c->bLength = USB_DT_CONFIG_SIZE;
  331. c->bDescriptorType = type;
  332. /* wTotalLength is written later */
  333. c->bNumInterfaces = config->next_interface_id;
  334. c->bConfigurationValue = config->bConfigurationValue;
  335. c->iConfiguration = config->iConfiguration;
  336. c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
  337. c->bMaxPower = encode_bMaxPower(speed, config);
  338. /* There may be e.g. OTG descriptors */
  339. if (config->descriptors) {
  340. status = usb_descriptor_fillbuf(next, len,
  341. config->descriptors);
  342. if (status < 0)
  343. return status;
  344. len -= status;
  345. next += status;
  346. }
  347. /* add each function's descriptors */
  348. list_for_each_entry(f, &config->functions, list) {
  349. struct usb_descriptor_header **descriptors;
  350. switch (speed) {
  351. case USB_SPEED_SUPER:
  352. descriptors = f->ss_descriptors;
  353. break;
  354. case USB_SPEED_HIGH:
  355. descriptors = f->hs_descriptors;
  356. break;
  357. default:
  358. descriptors = f->fs_descriptors;
  359. }
  360. if (!descriptors)
  361. continue;
  362. status = usb_descriptor_fillbuf(next, len,
  363. (const struct usb_descriptor_header **) descriptors);
  364. if (status < 0)
  365. return status;
  366. len -= status;
  367. next += status;
  368. }
  369. len = next - buf;
  370. c->wTotalLength = cpu_to_le16(len);
  371. return len;
  372. }
  373. static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
  374. {
  375. struct usb_gadget *gadget = cdev->gadget;
  376. struct usb_configuration *c;
  377. u8 type = w_value >> 8;
  378. enum usb_device_speed speed = USB_SPEED_UNKNOWN;
  379. if (gadget->speed == USB_SPEED_SUPER)
  380. speed = gadget->speed;
  381. else if (gadget_is_dualspeed(gadget)) {
  382. int hs = 0;
  383. if (gadget->speed == USB_SPEED_HIGH)
  384. hs = 1;
  385. if (type == USB_DT_OTHER_SPEED_CONFIG)
  386. hs = !hs;
  387. if (hs)
  388. speed = USB_SPEED_HIGH;
  389. }
  390. /* This is a lookup by config *INDEX* */
  391. w_value &= 0xff;
  392. list_for_each_entry(c, &cdev->configs, list) {
  393. /* ignore configs that won't work at this speed */
  394. switch (speed) {
  395. case USB_SPEED_SUPER:
  396. if (!c->superspeed)
  397. continue;
  398. break;
  399. case USB_SPEED_HIGH:
  400. if (!c->highspeed)
  401. continue;
  402. break;
  403. default:
  404. if (!c->fullspeed)
  405. continue;
  406. }
  407. if (w_value == 0)
  408. return config_buf(c, speed, cdev->req->buf, type);
  409. w_value--;
  410. }
  411. return -EINVAL;
  412. }
  413. static int count_configs(struct usb_composite_dev *cdev, unsigned type)
  414. {
  415. struct usb_gadget *gadget = cdev->gadget;
  416. struct usb_configuration *c;
  417. unsigned count = 0;
  418. int hs = 0;
  419. int ss = 0;
  420. if (gadget_is_dualspeed(gadget)) {
  421. if (gadget->speed == USB_SPEED_HIGH)
  422. hs = 1;
  423. if (gadget->speed == USB_SPEED_SUPER)
  424. ss = 1;
  425. if (type == USB_DT_DEVICE_QUALIFIER)
  426. hs = !hs;
  427. }
  428. list_for_each_entry(c, &cdev->configs, list) {
  429. /* ignore configs that won't work at this speed */
  430. if (ss) {
  431. if (!c->superspeed)
  432. continue;
  433. } else if (hs) {
  434. if (!c->highspeed)
  435. continue;
  436. } else {
  437. if (!c->fullspeed)
  438. continue;
  439. }
  440. count++;
  441. }
  442. return count;
  443. }
  444. /**
  445. * bos_desc() - prepares the BOS descriptor.
  446. * @cdev: pointer to usb_composite device to generate the bos
  447. * descriptor for
  448. *
  449. * This function generates the BOS (Binary Device Object)
  450. * descriptor and its device capabilities descriptors. The BOS
  451. * descriptor should be supported by a SuperSpeed device.
  452. */
  453. static int bos_desc(struct usb_composite_dev *cdev)
  454. {
  455. struct usb_ext_cap_descriptor *usb_ext;
  456. struct usb_ss_cap_descriptor *ss_cap;
  457. struct usb_dcd_config_params dcd_config_params;
  458. struct usb_bos_descriptor *bos = cdev->req->buf;
  459. bos->bLength = USB_DT_BOS_SIZE;
  460. bos->bDescriptorType = USB_DT_BOS;
  461. bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
  462. bos->bNumDeviceCaps = 0;
  463. /*
  464. * A SuperSpeed device shall include the USB2.0 extension descriptor
  465. * and shall support LPM when operating in USB2.0 HS mode.
  466. */
  467. usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  468. bos->bNumDeviceCaps++;
  469. le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
  470. usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
  471. usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  472. usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
  473. usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT);
  474. /*
  475. * The Superspeed USB Capability descriptor shall be implemented by all
  476. * SuperSpeed devices.
  477. */
  478. ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  479. bos->bNumDeviceCaps++;
  480. le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
  481. ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
  482. ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  483. ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
  484. ss_cap->bmAttributes = 0; /* LTM is not supported yet */
  485. ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
  486. USB_FULL_SPEED_OPERATION |
  487. USB_HIGH_SPEED_OPERATION |
  488. USB_5GBPS_OPERATION);
  489. ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
  490. /* Get Controller configuration */
  491. if (cdev->gadget->ops->get_config_params)
  492. cdev->gadget->ops->get_config_params(&dcd_config_params);
  493. else {
  494. dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
  495. dcd_config_params.bU2DevExitLat =
  496. cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
  497. }
  498. ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
  499. ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
  500. return le16_to_cpu(bos->wTotalLength);
  501. }
  502. static void device_qual(struct usb_composite_dev *cdev)
  503. {
  504. struct usb_qualifier_descriptor *qual = cdev->req->buf;
  505. qual->bLength = sizeof(*qual);
  506. qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  507. /* POLICY: same bcdUSB and device type info at both speeds */
  508. qual->bcdUSB = cdev->desc.bcdUSB;
  509. qual->bDeviceClass = cdev->desc.bDeviceClass;
  510. qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
  511. qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
  512. /* ASSUME same EP0 fifo size at both speeds */
  513. qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
  514. qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
  515. qual->bRESERVED = 0;
  516. }
  517. /*-------------------------------------------------------------------------*/
  518. static void reset_config(struct usb_composite_dev *cdev)
  519. {
  520. struct usb_function *f;
  521. DBG(cdev, "reset config\n");
  522. list_for_each_entry(f, &cdev->config->functions, list) {
  523. if (f->disable)
  524. f->disable(f);
  525. bitmap_zero(f->endpoints, 32);
  526. }
  527. cdev->config = NULL;
  528. }
  529. static int set_config(struct usb_composite_dev *cdev,
  530. const struct usb_ctrlrequest *ctrl, unsigned number)
  531. {
  532. struct usb_gadget *gadget = cdev->gadget;
  533. struct usb_configuration *c = NULL;
  534. int result = -EINVAL;
  535. unsigned power = gadget_is_otg(gadget) ? 8 : 100;
  536. int tmp;
  537. if (number) {
  538. list_for_each_entry(c, &cdev->configs, list) {
  539. if (c->bConfigurationValue == number) {
  540. /*
  541. * We disable the FDs of the previous
  542. * configuration only if the new configuration
  543. * is a valid one
  544. */
  545. if (cdev->config)
  546. reset_config(cdev);
  547. result = 0;
  548. break;
  549. }
  550. }
  551. if (result < 0)
  552. goto done;
  553. } else { /* Zero configuration value - need to reset the config */
  554. if (cdev->config)
  555. reset_config(cdev);
  556. result = 0;
  557. }
  558. INFO(cdev, "%s config #%d: %s\n",
  559. usb_speed_string(gadget->speed),
  560. number, c ? c->label : "unconfigured");
  561. if (!c)
  562. goto done;
  563. cdev->config = c;
  564. /* Initialize all interfaces by setting them to altsetting zero. */
  565. for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
  566. struct usb_function *f = c->interface[tmp];
  567. struct usb_descriptor_header **descriptors;
  568. if (!f)
  569. break;
  570. /*
  571. * Record which endpoints are used by the function. This is used
  572. * to dispatch control requests targeted at that endpoint to the
  573. * function's setup callback instead of the current
  574. * configuration's setup callback.
  575. */
  576. switch (gadget->speed) {
  577. case USB_SPEED_SUPER:
  578. descriptors = f->ss_descriptors;
  579. break;
  580. case USB_SPEED_HIGH:
  581. descriptors = f->hs_descriptors;
  582. break;
  583. default:
  584. descriptors = f->fs_descriptors;
  585. }
  586. for (; *descriptors; ++descriptors) {
  587. struct usb_endpoint_descriptor *ep;
  588. int addr;
  589. if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
  590. continue;
  591. ep = (struct usb_endpoint_descriptor *)*descriptors;
  592. addr = ((ep->bEndpointAddress & 0x80) >> 3)
  593. | (ep->bEndpointAddress & 0x0f);
  594. set_bit(addr, f->endpoints);
  595. }
  596. result = f->set_alt(f, tmp, 0);
  597. if (result < 0) {
  598. DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
  599. tmp, f->name, f, result);
  600. reset_config(cdev);
  601. goto done;
  602. }
  603. if (result == USB_GADGET_DELAYED_STATUS) {
  604. DBG(cdev,
  605. "%s: interface %d (%s) requested delayed status\n",
  606. __func__, tmp, f->name);
  607. cdev->delayed_status++;
  608. DBG(cdev, "delayed_status count %d\n",
  609. cdev->delayed_status);
  610. }
  611. }
  612. /* when we return, be sure our power usage is valid */
  613. power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
  614. done:
  615. usb_gadget_vbus_draw(gadget, power);
  616. if (result >= 0 && cdev->delayed_status)
  617. result = USB_GADGET_DELAYED_STATUS;
  618. return result;
  619. }
  620. int usb_add_config_only(struct usb_composite_dev *cdev,
  621. struct usb_configuration *config)
  622. {
  623. struct usb_configuration *c;
  624. if (!config->bConfigurationValue)
  625. return -EINVAL;
  626. /* Prevent duplicate configuration identifiers */
  627. list_for_each_entry(c, &cdev->configs, list) {
  628. if (c->bConfigurationValue == config->bConfigurationValue)
  629. return -EBUSY;
  630. }
  631. config->cdev = cdev;
  632. list_add_tail(&config->list, &cdev->configs);
  633. INIT_LIST_HEAD(&config->functions);
  634. config->next_interface_id = 0;
  635. memset(config->interface, 0, sizeof(config->interface));
  636. return 0;
  637. }
  638. EXPORT_SYMBOL_GPL(usb_add_config_only);
  639. /**
  640. * usb_add_config() - add a configuration to a device.
  641. * @cdev: wraps the USB gadget
  642. * @config: the configuration, with bConfigurationValue assigned
  643. * @bind: the configuration's bind function
  644. * Context: single threaded during gadget setup
  645. *
  646. * One of the main tasks of a composite @bind() routine is to
  647. * add each of the configurations it supports, using this routine.
  648. *
  649. * This function returns the value of the configuration's @bind(), which
  650. * is zero for success else a negative errno value. Binding configurations
  651. * assigns global resources including string IDs, and per-configuration
  652. * resources such as interface IDs and endpoints.
  653. */
  654. int usb_add_config(struct usb_composite_dev *cdev,
  655. struct usb_configuration *config,
  656. int (*bind)(struct usb_configuration *))
  657. {
  658. int status = -EINVAL;
  659. if (!bind)
  660. goto done;
  661. DBG(cdev, "adding config #%u '%s'/%p\n",
  662. config->bConfigurationValue,
  663. config->label, config);
  664. status = usb_add_config_only(cdev, config);
  665. if (status)
  666. goto done;
  667. status = bind(config);
  668. if (status < 0) {
  669. while (!list_empty(&config->functions)) {
  670. struct usb_function *f;
  671. f = list_first_entry(&config->functions,
  672. struct usb_function, list);
  673. list_del(&f->list);
  674. if (f->unbind) {
  675. DBG(cdev, "unbind function '%s'/%p\n",
  676. f->name, f);
  677. f->unbind(config, f);
  678. /* may free memory for "f" */
  679. }
  680. }
  681. list_del(&config->list);
  682. config->cdev = NULL;
  683. } else {
  684. unsigned i;
  685. DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
  686. config->bConfigurationValue, config,
  687. config->superspeed ? " super" : "",
  688. config->highspeed ? " high" : "",
  689. config->fullspeed
  690. ? (gadget_is_dualspeed(cdev->gadget)
  691. ? " full"
  692. : " full/low")
  693. : "");
  694. for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
  695. struct usb_function *f = config->interface[i];
  696. if (!f)
  697. continue;
  698. DBG(cdev, " interface %d = %s/%p\n",
  699. i, f->name, f);
  700. }
  701. }
  702. /* set_alt(), or next bind(), sets up
  703. * ep->driver_data as needed.
  704. */
  705. usb_ep_autoconfig_reset(cdev->gadget);
  706. done:
  707. if (status)
  708. DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
  709. config->bConfigurationValue, status);
  710. return status;
  711. }
  712. EXPORT_SYMBOL_GPL(usb_add_config);
  713. static void remove_config(struct usb_composite_dev *cdev,
  714. struct usb_configuration *config)
  715. {
  716. while (!list_empty(&config->functions)) {
  717. struct usb_function *f;
  718. f = list_first_entry(&config->functions,
  719. struct usb_function, list);
  720. list_del(&f->list);
  721. if (f->unbind) {
  722. DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
  723. f->unbind(config, f);
  724. /* may free memory for "f" */
  725. }
  726. }
  727. list_del(&config->list);
  728. if (config->unbind) {
  729. DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
  730. config->unbind(config);
  731. /* may free memory for "c" */
  732. }
  733. }
  734. /**
  735. * usb_remove_config() - remove a configuration from a device.
  736. * @cdev: wraps the USB gadget
  737. * @config: the configuration
  738. *
  739. * Drivers must call usb_gadget_disconnect before calling this function
  740. * to disconnect the device from the host and make sure the host will not
  741. * try to enumerate the device while we are changing the config list.
  742. */
  743. void usb_remove_config(struct usb_composite_dev *cdev,
  744. struct usb_configuration *config)
  745. {
  746. unsigned long flags;
  747. spin_lock_irqsave(&cdev->lock, flags);
  748. if (cdev->config == config)
  749. reset_config(cdev);
  750. spin_unlock_irqrestore(&cdev->lock, flags);
  751. remove_config(cdev, config);
  752. }
  753. /*-------------------------------------------------------------------------*/
  754. /* We support strings in multiple languages ... string descriptor zero
  755. * says which languages are supported. The typical case will be that
  756. * only one language (probably English) is used, with I18N handled on
  757. * the host side.
  758. */
  759. static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
  760. {
  761. const struct usb_gadget_strings *s;
  762. __le16 language;
  763. __le16 *tmp;
  764. while (*sp) {
  765. s = *sp;
  766. language = cpu_to_le16(s->language);
  767. for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
  768. if (*tmp == language)
  769. goto repeat;
  770. }
  771. *tmp++ = language;
  772. repeat:
  773. sp++;
  774. }
  775. }
  776. static int lookup_string(
  777. struct usb_gadget_strings **sp,
  778. void *buf,
  779. u16 language,
  780. int id
  781. )
  782. {
  783. struct usb_gadget_strings *s;
  784. int value;
  785. while (*sp) {
  786. s = *sp++;
  787. if (s->language != language)
  788. continue;
  789. value = usb_gadget_get_string(s, id, buf);
  790. if (value > 0)
  791. return value;
  792. }
  793. return -EINVAL;
  794. }
  795. static int get_string(struct usb_composite_dev *cdev,
  796. void *buf, u16 language, int id)
  797. {
  798. struct usb_composite_driver *composite = cdev->driver;
  799. struct usb_configuration *c;
  800. struct usb_function *f;
  801. int len;
  802. /* Yes, not only is USB's I18N support probably more than most
  803. * folk will ever care about ... also, it's all supported here.
  804. * (Except for UTF8 support for Unicode's "Astral Planes".)
  805. */
  806. /* 0 == report all available language codes */
  807. if (id == 0) {
  808. struct usb_string_descriptor *s = buf;
  809. struct usb_gadget_strings **sp;
  810. memset(s, 0, 256);
  811. s->bDescriptorType = USB_DT_STRING;
  812. sp = composite->strings;
  813. if (sp)
  814. collect_langs(sp, s->wData);
  815. list_for_each_entry(c, &cdev->configs, list) {
  816. sp = c->strings;
  817. if (sp)
  818. collect_langs(sp, s->wData);
  819. list_for_each_entry(f, &c->functions, list) {
  820. sp = f->strings;
  821. if (sp)
  822. collect_langs(sp, s->wData);
  823. }
  824. }
  825. for (len = 0; len <= 126 && s->wData[len]; len++)
  826. continue;
  827. if (!len)
  828. return -EINVAL;
  829. s->bLength = 2 * (len + 1);
  830. return s->bLength;
  831. }
  832. /* String IDs are device-scoped, so we look up each string
  833. * table we're told about. These lookups are infrequent;
  834. * simpler-is-better here.
  835. */
  836. if (composite->strings) {
  837. len = lookup_string(composite->strings, buf, language, id);
  838. if (len > 0)
  839. return len;
  840. }
  841. list_for_each_entry(c, &cdev->configs, list) {
  842. if (c->strings) {
  843. len = lookup_string(c->strings, buf, language, id);
  844. if (len > 0)
  845. return len;
  846. }
  847. list_for_each_entry(f, &c->functions, list) {
  848. if (!f->strings)
  849. continue;
  850. len = lookup_string(f->strings, buf, language, id);
  851. if (len > 0)
  852. return len;
  853. }
  854. }
  855. return -EINVAL;
  856. }
  857. /**
  858. * usb_string_id() - allocate an unused string ID
  859. * @cdev: the device whose string descriptor IDs are being allocated
  860. * Context: single threaded during gadget setup
  861. *
  862. * @usb_string_id() is called from bind() callbacks to allocate
  863. * string IDs. Drivers for functions, configurations, or gadgets will
  864. * then store that ID in the appropriate descriptors and string table.
  865. *
  866. * All string identifier should be allocated using this,
  867. * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
  868. * that for example different functions don't wrongly assign different
  869. * meanings to the same identifier.
  870. */
  871. int usb_string_id(struct usb_composite_dev *cdev)
  872. {
  873. if (cdev->next_string_id < 254) {
  874. /* string id 0 is reserved by USB spec for list of
  875. * supported languages */
  876. /* 255 reserved as well? -- mina86 */
  877. cdev->next_string_id++;
  878. return cdev->next_string_id;
  879. }
  880. return -ENODEV;
  881. }
  882. EXPORT_SYMBOL_GPL(usb_string_id);
  883. /**
  884. * usb_string_ids() - allocate unused string IDs in batch
  885. * @cdev: the device whose string descriptor IDs are being allocated
  886. * @str: an array of usb_string objects to assign numbers to
  887. * Context: single threaded during gadget setup
  888. *
  889. * @usb_string_ids() is called from bind() callbacks to allocate
  890. * string IDs. Drivers for functions, configurations, or gadgets will
  891. * then copy IDs from the string table to the appropriate descriptors
  892. * and string table for other languages.
  893. *
  894. * All string identifier should be allocated using this,
  895. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  896. * example different functions don't wrongly assign different meanings
  897. * to the same identifier.
  898. */
  899. int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
  900. {
  901. int next = cdev->next_string_id;
  902. for (; str->s; ++str) {
  903. if (unlikely(next >= 254))
  904. return -ENODEV;
  905. str->id = ++next;
  906. }
  907. cdev->next_string_id = next;
  908. return 0;
  909. }
  910. EXPORT_SYMBOL_GPL(usb_string_ids_tab);
  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. EXPORT_SYMBOL_GPL(usb_string_ids_n);
  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. 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. 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 (cdev->driver->disconnect)
  1216. cdev->driver->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 __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
  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 && unbind_driver)
  1245. cdev->driver->unbind(cdev);
  1246. composite_dev_cleanup(cdev);
  1247. kfree(cdev->def_manufacturer);
  1248. kfree(cdev);
  1249. set_gadget_data(gadget, NULL);
  1250. }
  1251. static void composite_unbind(struct usb_gadget *gadget)
  1252. {
  1253. __composite_unbind(gadget, true);
  1254. }
  1255. static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
  1256. const struct usb_device_descriptor *old)
  1257. {
  1258. __le16 idVendor;
  1259. __le16 idProduct;
  1260. __le16 bcdDevice;
  1261. u8 iSerialNumber;
  1262. u8 iManufacturer;
  1263. u8 iProduct;
  1264. /*
  1265. * these variables may have been set in
  1266. * usb_composite_overwrite_options()
  1267. */
  1268. idVendor = new->idVendor;
  1269. idProduct = new->idProduct;
  1270. bcdDevice = new->bcdDevice;
  1271. iSerialNumber = new->iSerialNumber;
  1272. iManufacturer = new->iManufacturer;
  1273. iProduct = new->iProduct;
  1274. *new = *old;
  1275. if (idVendor)
  1276. new->idVendor = idVendor;
  1277. if (idProduct)
  1278. new->idProduct = idProduct;
  1279. if (bcdDevice)
  1280. new->bcdDevice = bcdDevice;
  1281. else
  1282. new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
  1283. if (iSerialNumber)
  1284. new->iSerialNumber = iSerialNumber;
  1285. if (iManufacturer)
  1286. new->iManufacturer = iManufacturer;
  1287. if (iProduct)
  1288. new->iProduct = iProduct;
  1289. }
  1290. int composite_dev_prepare(struct usb_composite_driver *composite,
  1291. struct usb_composite_dev *cdev)
  1292. {
  1293. struct usb_gadget *gadget = cdev->gadget;
  1294. int ret = -ENOMEM;
  1295. /* preallocate control response and buffer */
  1296. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  1297. if (!cdev->req)
  1298. return -ENOMEM;
  1299. cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
  1300. if (!cdev->req->buf)
  1301. goto fail;
  1302. ret = device_create_file(&gadget->dev, &dev_attr_suspended);
  1303. if (ret)
  1304. goto fail_dev;
  1305. cdev->req->complete = composite_setup_complete;
  1306. gadget->ep0->driver_data = cdev;
  1307. cdev->driver = composite;
  1308. /*
  1309. * As per USB compliance update, a device that is actively drawing
  1310. * more than 100mA from USB must report itself as bus-powered in
  1311. * the GetStatus(DEVICE) call.
  1312. */
  1313. if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
  1314. usb_gadget_set_selfpowered(gadget);
  1315. /* interface and string IDs start at zero via kzalloc.
  1316. * we force endpoints to start unassigned; few controller
  1317. * drivers will zero ep->driver_data.
  1318. */
  1319. usb_ep_autoconfig_reset(gadget);
  1320. return 0;
  1321. fail_dev:
  1322. kfree(cdev->req->buf);
  1323. fail:
  1324. usb_ep_free_request(gadget->ep0, cdev->req);
  1325. cdev->req = NULL;
  1326. return ret;
  1327. }
  1328. void composite_dev_cleanup(struct usb_composite_dev *cdev)
  1329. {
  1330. if (cdev->req) {
  1331. kfree(cdev->req->buf);
  1332. usb_ep_free_request(cdev->gadget->ep0, cdev->req);
  1333. }
  1334. device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
  1335. }
  1336. static int composite_bind(struct usb_gadget *gadget,
  1337. struct usb_gadget_driver *gdriver)
  1338. {
  1339. struct usb_composite_dev *cdev;
  1340. struct usb_composite_driver *composite = to_cdriver(gdriver);
  1341. int status = -ENOMEM;
  1342. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  1343. if (!cdev)
  1344. return status;
  1345. spin_lock_init(&cdev->lock);
  1346. cdev->gadget = gadget;
  1347. set_gadget_data(gadget, cdev);
  1348. INIT_LIST_HEAD(&cdev->configs);
  1349. status = composite_dev_prepare(composite, cdev);
  1350. if (status)
  1351. goto fail;
  1352. /* composite gadget needs to assign strings for whole device (like
  1353. * serial number), register function drivers, potentially update
  1354. * power state and consumption, etc
  1355. */
  1356. status = composite->bind(cdev);
  1357. if (status < 0)
  1358. goto fail;
  1359. update_unchanged_dev_desc(&cdev->desc, composite->dev);
  1360. /* has userspace failed to provide a serial number? */
  1361. if (composite->needs_serial && !cdev->desc.iSerialNumber)
  1362. WARNING(cdev, "userspace failed to provide iSerialNumber\n");
  1363. INFO(cdev, "%s ready\n", composite->name);
  1364. return 0;
  1365. fail:
  1366. __composite_unbind(gadget, false);
  1367. return status;
  1368. }
  1369. /*-------------------------------------------------------------------------*/
  1370. static void
  1371. composite_suspend(struct usb_gadget *gadget)
  1372. {
  1373. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1374. struct usb_function *f;
  1375. /* REVISIT: should we have config level
  1376. * suspend/resume callbacks?
  1377. */
  1378. DBG(cdev, "suspend\n");
  1379. if (cdev->config) {
  1380. list_for_each_entry(f, &cdev->config->functions, list) {
  1381. if (f->suspend)
  1382. f->suspend(f);
  1383. }
  1384. }
  1385. if (cdev->driver->suspend)
  1386. cdev->driver->suspend(cdev);
  1387. cdev->suspended = 1;
  1388. usb_gadget_vbus_draw(gadget, 2);
  1389. }
  1390. static void
  1391. composite_resume(struct usb_gadget *gadget)
  1392. {
  1393. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1394. struct usb_function *f;
  1395. u8 maxpower;
  1396. /* REVISIT: should we have config level
  1397. * suspend/resume callbacks?
  1398. */
  1399. DBG(cdev, "resume\n");
  1400. if (cdev->driver->resume)
  1401. cdev->driver->resume(cdev);
  1402. if (cdev->config) {
  1403. list_for_each_entry(f, &cdev->config->functions, list) {
  1404. if (f->resume)
  1405. f->resume(f);
  1406. }
  1407. maxpower = cdev->config->MaxPower;
  1408. usb_gadget_vbus_draw(gadget, maxpower ?
  1409. maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
  1410. }
  1411. cdev->suspended = 0;
  1412. }
  1413. /*-------------------------------------------------------------------------*/
  1414. static const struct usb_gadget_driver composite_driver_template = {
  1415. .bind = composite_bind,
  1416. .unbind = composite_unbind,
  1417. .setup = composite_setup,
  1418. .disconnect = composite_disconnect,
  1419. .suspend = composite_suspend,
  1420. .resume = composite_resume,
  1421. .driver = {
  1422. .owner = THIS_MODULE,
  1423. },
  1424. };
  1425. /**
  1426. * usb_composite_probe() - register a composite driver
  1427. * @driver: the driver to register
  1428. * @bind: the callback used to allocate resources that are shared across the
  1429. * whole device, such as string IDs, and add its configurations using
  1430. * @usb_add_config(). This may fail by returning a negative errno
  1431. * value; it should return zero on successful initialization.
  1432. * Context: single threaded during gadget setup
  1433. *
  1434. * This function is used to register drivers using the composite driver
  1435. * framework. The return value is zero, or a negative errno value.
  1436. * Those values normally come from the driver's @bind method, which does
  1437. * all the work of setting up the driver to match the hardware.
  1438. *
  1439. * On successful return, the gadget is ready to respond to requests from
  1440. * the host, unless one of its components invokes usb_gadget_disconnect()
  1441. * while it was binding. That would usually be done in order to wait for
  1442. * some userspace participation.
  1443. */
  1444. int usb_composite_probe(struct usb_composite_driver *driver)
  1445. {
  1446. struct usb_gadget_driver *gadget_driver;
  1447. if (!driver || !driver->dev || !driver->bind)
  1448. return -EINVAL;
  1449. if (!driver->name)
  1450. driver->name = "composite";
  1451. driver->gadget_driver = composite_driver_template;
  1452. gadget_driver = &driver->gadget_driver;
  1453. gadget_driver->function = (char *) driver->name;
  1454. gadget_driver->driver.name = driver->name;
  1455. gadget_driver->max_speed = driver->max_speed;
  1456. return usb_gadget_probe_driver(gadget_driver);
  1457. }
  1458. EXPORT_SYMBOL_GPL(usb_composite_probe);
  1459. /**
  1460. * usb_composite_unregister() - unregister a composite driver
  1461. * @driver: the driver to unregister
  1462. *
  1463. * This function is used to unregister drivers using the composite
  1464. * driver framework.
  1465. */
  1466. void usb_composite_unregister(struct usb_composite_driver *driver)
  1467. {
  1468. usb_gadget_unregister_driver(&driver->gadget_driver);
  1469. }
  1470. EXPORT_SYMBOL_GPL(usb_composite_unregister);
  1471. /**
  1472. * usb_composite_setup_continue() - Continue with the control transfer
  1473. * @cdev: the composite device who's control transfer was kept waiting
  1474. *
  1475. * This function must be called by the USB function driver to continue
  1476. * with the control transfer's data/status stage in case it had requested to
  1477. * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
  1478. * can request the composite framework to delay the setup request's data/status
  1479. * stages by returning USB_GADGET_DELAYED_STATUS.
  1480. */
  1481. void usb_composite_setup_continue(struct usb_composite_dev *cdev)
  1482. {
  1483. int value;
  1484. struct usb_request *req = cdev->req;
  1485. unsigned long flags;
  1486. DBG(cdev, "%s\n", __func__);
  1487. spin_lock_irqsave(&cdev->lock, flags);
  1488. if (cdev->delayed_status == 0) {
  1489. WARN(cdev, "%s: Unexpected call\n", __func__);
  1490. } else if (--cdev->delayed_status == 0) {
  1491. DBG(cdev, "%s: Completing delayed status\n", __func__);
  1492. req->length = 0;
  1493. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  1494. if (value < 0) {
  1495. DBG(cdev, "ep_queue --> %d\n", value);
  1496. req->status = 0;
  1497. composite_setup_complete(cdev->gadget->ep0, req);
  1498. }
  1499. }
  1500. spin_unlock_irqrestore(&cdev->lock, flags);
  1501. }
  1502. EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
  1503. static char *composite_default_mfr(struct usb_gadget *gadget)
  1504. {
  1505. char *mfr;
  1506. int len;
  1507. len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
  1508. init_utsname()->release, gadget->name);
  1509. len++;
  1510. mfr = kmalloc(len, GFP_KERNEL);
  1511. if (!mfr)
  1512. return NULL;
  1513. snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
  1514. init_utsname()->release, gadget->name);
  1515. return mfr;
  1516. }
  1517. void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
  1518. struct usb_composite_overwrite *covr)
  1519. {
  1520. struct usb_device_descriptor *desc = &cdev->desc;
  1521. struct usb_gadget_strings *gstr = cdev->driver->strings[0];
  1522. struct usb_string *dev_str = gstr->strings;
  1523. if (covr->idVendor)
  1524. desc->idVendor = cpu_to_le16(covr->idVendor);
  1525. if (covr->idProduct)
  1526. desc->idProduct = cpu_to_le16(covr->idProduct);
  1527. if (covr->bcdDevice)
  1528. desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
  1529. if (covr->serial_number) {
  1530. desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
  1531. dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
  1532. }
  1533. if (covr->manufacturer) {
  1534. desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
  1535. dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
  1536. } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
  1537. desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
  1538. cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
  1539. dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
  1540. }
  1541. if (covr->product) {
  1542. desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
  1543. dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
  1544. }
  1545. }
  1546. EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
  1547. MODULE_LICENSE("GPL");
  1548. MODULE_AUTHOR("David Brownell");