composite.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * composite.c - infrastructure for Composite USB Gadgets
  4. *
  5. * Copyright (C) 2006-2008 David Brownell
  6. * U-Boot porting: Lukasz Majewski <l.majewski@samsung.com>
  7. */
  8. #undef DEBUG
  9. #include <log.h>
  10. #include <dm/devres.h>
  11. #include <linux/bitops.h>
  12. #include <linux/bug.h>
  13. #include <linux/usb/composite.h>
  14. #include "u_os_desc.h"
  15. #define USB_BUFSIZ 4096
  16. /* Helper type for accessing packed u16 pointers */
  17. typedef struct { __le16 val; } __packed __le16_packed;
  18. static struct usb_composite_driver *composite;
  19. static struct usb_configuration *os_desc_config;
  20. /* Microsoft OS String Descriptor */
  21. static char qw_sign_buf[OS_STRING_QW_SIGN_LEN / 2] = {'M', 'S', 'F', 'T', '1', '0', '0'};
  22. static inline void le16_add_cpu_packed(__le16_packed *var, u16 val)
  23. {
  24. var->val = cpu_to_le16(le16_to_cpu(var->val) + val);
  25. }
  26. /**
  27. * struct usb_os_string - represents OS String to be reported by a gadget
  28. * @bLength: total length of the entire descritor, always 0x12
  29. * @bDescriptorType: USB_DT_STRING
  30. * @qwSignature: the OS String proper
  31. * @bMS_VendorCode: code used by the host for subsequent requests
  32. * @bPad: not used, must be zero
  33. */
  34. struct usb_os_string {
  35. __u8 bLength;
  36. __u8 bDescriptorType;
  37. __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
  38. __u8 bMS_VendorCode;
  39. __u8 bPad;
  40. } __packed;
  41. /**
  42. * usb_add_function() - add a function to a configuration
  43. * @config: the configuration
  44. * @function: the function being added
  45. * Context: single threaded during gadget setup
  46. *
  47. * After initialization, each configuration must have one or more
  48. * functions added to it. Adding a function involves calling its @bind()
  49. * method to allocate resources such as interface and string identifiers
  50. * and endpoints.
  51. *
  52. * This function returns the value of the function's bind(), which is
  53. * zero for success else a negative errno value.
  54. */
  55. int usb_add_function(struct usb_configuration *config,
  56. struct usb_function *function)
  57. {
  58. int value = -EINVAL;
  59. debug("adding '%s'/%p to config '%s'/%p\n",
  60. function->name, function,
  61. config->label, config);
  62. if (!function->set_alt || !function->disable)
  63. goto done;
  64. function->config = config;
  65. list_add_tail(&function->list, &config->functions);
  66. if (function->bind) {
  67. value = function->bind(config, function);
  68. if (value < 0) {
  69. list_del(&function->list);
  70. function->config = NULL;
  71. }
  72. } else
  73. value = 0;
  74. if (!config->fullspeed && function->descriptors)
  75. config->fullspeed = 1;
  76. if (!config->highspeed && function->hs_descriptors)
  77. config->highspeed = 1;
  78. if (!config->superspeed && function->ss_descriptors)
  79. config->superspeed = 1;
  80. done:
  81. if (value)
  82. debug("adding '%s'/%p --> %d\n",
  83. function->name, function, value);
  84. return value;
  85. }
  86. /**
  87. * usb_function_deactivate - prevent function and gadget enumeration
  88. * @function: the function that isn't yet ready to respond
  89. *
  90. * Blocks response of the gadget driver to host enumeration by
  91. * preventing the data line pullup from being activated. This is
  92. * normally called during @bind() processing to change from the
  93. * initial "ready to respond" state, or when a required resource
  94. * becomes available.
  95. *
  96. * For example, drivers that serve as a passthrough to a userspace
  97. * daemon can block enumeration unless that daemon (such as an OBEX,
  98. * MTP, or print server) is ready to handle host requests.
  99. *
  100. * Not all systems support software control of their USB peripheral
  101. * data pullups.
  102. *
  103. * Returns zero on success, else negative errno.
  104. */
  105. int usb_function_deactivate(struct usb_function *function)
  106. {
  107. struct usb_composite_dev *cdev = function->config->cdev;
  108. int status = 0;
  109. if (cdev->deactivations == 0)
  110. status = usb_gadget_disconnect(cdev->gadget);
  111. if (status == 0)
  112. cdev->deactivations++;
  113. return status;
  114. }
  115. /**
  116. * usb_function_activate - allow function and gadget enumeration
  117. * @function: function on which usb_function_activate() was called
  118. *
  119. * Reverses effect of usb_function_deactivate(). If no more functions
  120. * are delaying their activation, the gadget driver will respond to
  121. * host enumeration procedures.
  122. *
  123. * Returns zero on success, else negative errno.
  124. */
  125. int usb_function_activate(struct usb_function *function)
  126. {
  127. struct usb_composite_dev *cdev = function->config->cdev;
  128. int status = 0;
  129. if (cdev->deactivations == 0)
  130. status = -EINVAL;
  131. else {
  132. cdev->deactivations--;
  133. if (cdev->deactivations == 0)
  134. status = usb_gadget_connect(cdev->gadget);
  135. }
  136. return status;
  137. }
  138. /**
  139. * usb_interface_id() - allocate an unused interface ID
  140. * @config: configuration associated with the interface
  141. * @function: function handling the interface
  142. * Context: single threaded during gadget setup
  143. *
  144. * usb_interface_id() is called from usb_function.bind() callbacks to
  145. * allocate new interface IDs. The function driver will then store that
  146. * ID in interface, association, CDC union, and other descriptors. It
  147. * will also handle any control requests targetted at that interface,
  148. * particularly changing its altsetting via set_alt(). There may
  149. * also be class-specific or vendor-specific requests to handle.
  150. *
  151. * All interface identifier should be allocated using this routine, to
  152. * ensure that for example different functions don't wrongly assign
  153. * different meanings to the same identifier. Note that since interface
  154. * identifers are configuration-specific, functions used in more than
  155. * one configuration (or more than once in a given configuration) need
  156. * multiple versions of the relevant descriptors.
  157. *
  158. * Returns the interface ID which was allocated; or -ENODEV if no
  159. * more interface IDs can be allocated.
  160. */
  161. int usb_interface_id(struct usb_configuration *config,
  162. struct usb_function *function)
  163. {
  164. unsigned char id = config->next_interface_id;
  165. if (id < MAX_CONFIG_INTERFACES) {
  166. config->interface[id] = function;
  167. config->next_interface_id = id + 1;
  168. return id;
  169. }
  170. return -ENODEV;
  171. }
  172. static int config_buf(struct usb_configuration *config,
  173. enum usb_device_speed speed, void *buf, u8 type)
  174. {
  175. int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
  176. void *next = buf + USB_DT_CONFIG_SIZE;
  177. struct usb_descriptor_header **descriptors;
  178. struct usb_config_descriptor *c;
  179. int status;
  180. struct usb_function *f;
  181. /* write the config descriptor */
  182. c = buf;
  183. c->bLength = USB_DT_CONFIG_SIZE;
  184. c->bDescriptorType = type;
  185. c->bNumInterfaces = config->next_interface_id;
  186. c->bConfigurationValue = config->bConfigurationValue;
  187. c->iConfiguration = config->iConfiguration;
  188. c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
  189. c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
  190. /* There may be e.g. OTG descriptors */
  191. if (config->descriptors) {
  192. status = usb_descriptor_fillbuf(next, len,
  193. config->descriptors);
  194. if (status < 0)
  195. return status;
  196. len -= status;
  197. next += status;
  198. }
  199. /* add each function's descriptors */
  200. list_for_each_entry(f, &config->functions, list) {
  201. if (speed == USB_SPEED_SUPER)
  202. descriptors = f->ss_descriptors;
  203. else if (speed == USB_SPEED_HIGH)
  204. descriptors = f->hs_descriptors;
  205. else
  206. descriptors = f->descriptors;
  207. if (!descriptors)
  208. continue;
  209. status = usb_descriptor_fillbuf(next, len,
  210. (const struct usb_descriptor_header **) descriptors);
  211. if (status < 0)
  212. return status;
  213. len -= status;
  214. next += status;
  215. }
  216. len = next - buf;
  217. c->wTotalLength = cpu_to_le16(len);
  218. return len;
  219. }
  220. static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
  221. {
  222. enum usb_device_speed speed = USB_SPEED_UNKNOWN;
  223. struct usb_gadget *gadget = cdev->gadget;
  224. u8 type = w_value >> 8;
  225. int hs = 0;
  226. struct usb_configuration *c;
  227. struct list_head *pos;
  228. if (gadget_is_superspeed(gadget)) {
  229. speed = gadget->speed;
  230. } else if (gadget_is_dualspeed(gadget)) {
  231. if (gadget->speed == USB_SPEED_HIGH)
  232. hs = 1;
  233. if (type == USB_DT_OTHER_SPEED_CONFIG)
  234. hs = !hs;
  235. if (hs)
  236. speed = USB_SPEED_HIGH;
  237. }
  238. w_value &= 0xff;
  239. pos = &cdev->configs;
  240. c = cdev->os_desc_config;
  241. if (c)
  242. goto check_config;
  243. while ((pos = pos->next) != &cdev->configs) {
  244. c = list_entry(pos, typeof(*c), list);
  245. /* skip OS Descriptors config which is handled separately */
  246. if (c == cdev->os_desc_config)
  247. continue;
  248. check_config:
  249. if (speed == USB_SPEED_SUPER) {
  250. if (!c->superspeed)
  251. continue;
  252. } else if (speed == USB_SPEED_HIGH) {
  253. if (!c->highspeed)
  254. continue;
  255. } else {
  256. if (!c->fullspeed)
  257. continue;
  258. }
  259. if (w_value == 0)
  260. return config_buf(c, speed, cdev->req->buf, type);
  261. w_value--;
  262. }
  263. return -EINVAL;
  264. }
  265. static int count_configs(struct usb_composite_dev *cdev, unsigned type)
  266. {
  267. struct usb_gadget *gadget = cdev->gadget;
  268. unsigned count = 0;
  269. int hs = 0;
  270. int ss = 0;
  271. struct usb_configuration *c;
  272. if (gadget->speed == USB_SPEED_SUPER)
  273. ss = 1;
  274. if (gadget_is_dualspeed(gadget)) {
  275. if (gadget->speed == USB_SPEED_HIGH)
  276. hs = 1;
  277. if (type == USB_DT_DEVICE_QUALIFIER)
  278. hs = !hs;
  279. }
  280. list_for_each_entry(c, &cdev->configs, list) {
  281. /* ignore configs that won't work at this speed */
  282. if (ss) {
  283. if (!c->superspeed)
  284. continue;
  285. } else if (hs) {
  286. if (!c->highspeed)
  287. continue;
  288. } else {
  289. if (!c->fullspeed)
  290. continue;
  291. }
  292. count++;
  293. }
  294. return count;
  295. }
  296. static void device_qual(struct usb_composite_dev *cdev)
  297. {
  298. struct usb_qualifier_descriptor *qual = cdev->req->buf;
  299. qual->bLength = sizeof(*qual);
  300. qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  301. /* POLICY: same bcdUSB and device type info at both speeds */
  302. qual->bcdUSB = cdev->desc.bcdUSB;
  303. qual->bDeviceClass = cdev->desc.bDeviceClass;
  304. qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
  305. qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
  306. /* ASSUME same EP0 fifo size at both speeds */
  307. qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
  308. qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
  309. qual->bRESERVED = 0;
  310. }
  311. static void reset_config(struct usb_composite_dev *cdev)
  312. {
  313. struct usb_function *f;
  314. debug("%s:\n", __func__);
  315. list_for_each_entry(f, &cdev->config->functions, list) {
  316. if (f->disable)
  317. f->disable(f);
  318. bitmap_zero(f->endpoints, 32);
  319. }
  320. cdev->config = NULL;
  321. }
  322. static int set_config(struct usb_composite_dev *cdev,
  323. const struct usb_ctrlrequest *ctrl, unsigned number)
  324. {
  325. struct usb_gadget *gadget = cdev->gadget;
  326. unsigned power = gadget_is_otg(gadget) ? 8 : 100;
  327. struct usb_descriptor_header **descriptors;
  328. int result = -EINVAL;
  329. struct usb_endpoint_descriptor *ep;
  330. struct usb_configuration *c = NULL;
  331. int addr;
  332. int tmp;
  333. struct usb_function *f;
  334. if (cdev->config)
  335. reset_config(cdev);
  336. if (number) {
  337. list_for_each_entry(c, &cdev->configs, list) {
  338. if (c->bConfigurationValue == number) {
  339. result = 0;
  340. break;
  341. }
  342. }
  343. if (result < 0)
  344. goto done;
  345. } else
  346. result = 0;
  347. debug("%s: %s speed config #%d: %s\n", __func__,
  348. ({ char *speed;
  349. switch (gadget->speed) {
  350. case USB_SPEED_LOW:
  351. speed = "low";
  352. break;
  353. case USB_SPEED_FULL:
  354. speed = "full";
  355. break;
  356. case USB_SPEED_HIGH:
  357. speed = "high";
  358. break;
  359. case USB_SPEED_SUPER:
  360. speed = "super";
  361. break;
  362. default:
  363. speed = "?";
  364. break;
  365. };
  366. speed;
  367. }), number, c ? c->label : "unconfigured");
  368. if (!c)
  369. goto done;
  370. cdev->config = c;
  371. /* Initialize all interfaces by setting them to altsetting zero. */
  372. for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
  373. f = c->interface[tmp];
  374. if (!f)
  375. break;
  376. /*
  377. * Record which endpoints are used by the function. This is used
  378. * to dispatch control requests targeted at that endpoint to the
  379. * function's setup callback instead of the current
  380. * configuration's setup callback.
  381. */
  382. if (gadget->speed == USB_SPEED_SUPER)
  383. descriptors = f->ss_descriptors;
  384. else if (gadget->speed == USB_SPEED_HIGH)
  385. descriptors = f->hs_descriptors;
  386. else
  387. descriptors = f->descriptors;
  388. for (; *descriptors; ++descriptors) {
  389. if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
  390. continue;
  391. ep = (struct usb_endpoint_descriptor *)*descriptors;
  392. addr = ((ep->bEndpointAddress & 0x80) >> 3)
  393. | (ep->bEndpointAddress & 0x0f);
  394. generic_set_bit(addr, f->endpoints);
  395. }
  396. result = f->set_alt(f, tmp, 0);
  397. if (result < 0) {
  398. debug("interface %d (%s/%p) alt 0 --> %d\n",
  399. tmp, f->name, f, result);
  400. reset_config(cdev);
  401. goto done;
  402. }
  403. }
  404. /* when we return, be sure our power usage is valid */
  405. power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
  406. done:
  407. usb_gadget_vbus_draw(gadget, power);
  408. return result;
  409. }
  410. /**
  411. * usb_add_config() - add a configuration to a device.
  412. * @cdev: wraps the USB gadget
  413. * @config: the configuration, with bConfigurationValue assigned
  414. * Context: single threaded during gadget setup
  415. *
  416. * One of the main tasks of a composite driver's bind() routine is to
  417. * add each of the configurations it supports, using this routine.
  418. *
  419. * This function returns the value of the configuration's bind(), which
  420. * is zero for success else a negative errno value. Binding configurations
  421. * assigns global resources including string IDs, and per-configuration
  422. * resources such as interface IDs and endpoints.
  423. */
  424. int usb_add_config(struct usb_composite_dev *cdev,
  425. struct usb_configuration *config)
  426. {
  427. int status = -EINVAL;
  428. struct usb_configuration *c;
  429. struct usb_function *f;
  430. unsigned int i;
  431. debug("%s: adding config #%u '%s'/%p\n", __func__,
  432. config->bConfigurationValue,
  433. config->label, config);
  434. if (!config->bConfigurationValue || !config->bind)
  435. goto done;
  436. /* Prevent duplicate configuration identifiers */
  437. list_for_each_entry(c, &cdev->configs, list) {
  438. if (c->bConfigurationValue == config->bConfigurationValue) {
  439. status = -EBUSY;
  440. goto done;
  441. }
  442. }
  443. config->cdev = cdev;
  444. list_add_tail(&config->list, &cdev->configs);
  445. INIT_LIST_HEAD(&config->functions);
  446. config->next_interface_id = 0;
  447. status = config->bind(config);
  448. if (status < 0) {
  449. list_del(&config->list);
  450. config->cdev = NULL;
  451. } else {
  452. debug("cfg %d/%p speeds:%s%s%s\n",
  453. config->bConfigurationValue, config,
  454. config->superspeed ? " super" : "",
  455. config->highspeed ? " high" : "",
  456. config->fullspeed
  457. ? (gadget_is_dualspeed(cdev->gadget)
  458. ? " full"
  459. : " full/low")
  460. : "");
  461. for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
  462. f = config->interface[i];
  463. if (!f)
  464. continue;
  465. debug("%s: interface %d = %s/%p\n",
  466. __func__, i, f->name, f);
  467. }
  468. }
  469. /*
  470. * If one function of config is not super speed capable,
  471. * force the gadget to be high speed so controller driver
  472. * can init HW to be USB 2.0
  473. */
  474. if (gadget_is_superspeed(cdev->gadget)) {
  475. list_for_each_entry(f, &config->functions, list) {
  476. if (!f->ss_descriptors)
  477. cdev->gadget->max_speed =
  478. USB_SPEED_HIGH;
  479. }
  480. }
  481. usb_ep_autoconfig_reset(cdev->gadget);
  482. os_desc_config = config;
  483. cdev->os_desc_config = os_desc_config;
  484. done:
  485. if (status)
  486. debug("added config '%s'/%u --> %d\n", config->label,
  487. config->bConfigurationValue, status);
  488. return status;
  489. }
  490. /*
  491. * We support strings in multiple languages ... string descriptor zero
  492. * says which languages are supported. The typical case will be that
  493. * only one language (probably English) is used, with I18N handled on
  494. * the host side.
  495. */
  496. static void collect_langs(struct usb_gadget_strings **sp, void *buf)
  497. {
  498. const struct usb_gadget_strings *s;
  499. u16 language;
  500. __le16_packed *tmp;
  501. __le16_packed *end = (buf + 252);
  502. while (*sp) {
  503. s = *sp;
  504. language = cpu_to_le16(s->language);
  505. for (tmp = buf; tmp->val && tmp < end; tmp++) {
  506. if (tmp->val == language)
  507. goto repeat;
  508. }
  509. tmp->val = language;
  510. repeat:
  511. sp++;
  512. }
  513. }
  514. static int lookup_string(
  515. struct usb_gadget_strings **sp,
  516. void *buf,
  517. u16 language,
  518. int id
  519. )
  520. {
  521. int value;
  522. struct usb_gadget_strings *s;
  523. while (*sp) {
  524. s = *sp++;
  525. if (s->language != language)
  526. continue;
  527. value = usb_gadget_get_string(s, id, buf);
  528. if (value > 0)
  529. return value;
  530. }
  531. return -EINVAL;
  532. }
  533. static int get_string(struct usb_composite_dev *cdev,
  534. void *buf, u16 language, int id)
  535. {
  536. struct usb_string_descriptor *s = buf;
  537. struct usb_gadget_strings **sp;
  538. int len;
  539. struct usb_configuration *c;
  540. struct usb_function *f;
  541. /*
  542. * Yes, not only is USB's I18N support probably more than most
  543. * folk will ever care about ... also, it's all supported here.
  544. * (Except for UTF8 support for Unicode's "Astral Planes".)
  545. */
  546. /* 0 == report all available language codes */
  547. if (id == 0) {
  548. memset(s, 0, 256);
  549. s->bDescriptorType = USB_DT_STRING;
  550. sp = composite->strings;
  551. if (sp)
  552. collect_langs(sp, s->wData);
  553. list_for_each_entry(c, &cdev->configs, list) {
  554. sp = c->strings;
  555. if (sp)
  556. collect_langs(sp, s->wData);
  557. list_for_each_entry(f, &c->functions, list) {
  558. sp = f->strings;
  559. if (sp)
  560. collect_langs(sp, s->wData);
  561. }
  562. }
  563. for (len = 0; len <= 126 && s->wData[len]; len++)
  564. continue;
  565. if (!len)
  566. return -EINVAL;
  567. s->bLength = 2 * (len + 1);
  568. return s->bLength;
  569. }
  570. if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
  571. struct usb_os_string *b = buf;
  572. b->bLength = sizeof(*b);
  573. b->bDescriptorType = USB_DT_STRING;
  574. memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
  575. b->bMS_VendorCode = cdev->b_vendor_code;
  576. b->bPad = 0;
  577. return sizeof(*b);
  578. }
  579. /*
  580. * Otherwise, look up and return a specified string. String IDs
  581. * are device-scoped, so we look up each string table we're told
  582. * about. These lookups are infrequent; simpler-is-better here.
  583. */
  584. if (composite->strings) {
  585. len = lookup_string(composite->strings, buf, language, id);
  586. if (len > 0)
  587. return len;
  588. }
  589. list_for_each_entry(c, &cdev->configs, list) {
  590. if (c->strings) {
  591. len = lookup_string(c->strings, buf, language, id);
  592. if (len > 0)
  593. return len;
  594. }
  595. list_for_each_entry(f, &c->functions, list) {
  596. if (!f->strings)
  597. continue;
  598. len = lookup_string(f->strings, buf, language, id);
  599. if (len > 0)
  600. return len;
  601. }
  602. }
  603. return -EINVAL;
  604. }
  605. /**
  606. * usb_string_id() - allocate an unused string ID
  607. * @cdev: the device whose string descriptor IDs are being allocated
  608. * Context: single threaded during gadget setup
  609. *
  610. * @usb_string_id() is called from bind() callbacks to allocate
  611. * string IDs. Drivers for functions, configurations, or gadgets will
  612. * then store that ID in the appropriate descriptors and string table.
  613. *
  614. * All string identifier should be allocated using this,
  615. * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
  616. * that for example different functions don't wrongly assign different
  617. * meanings to the same identifier.
  618. */
  619. int usb_string_id(struct usb_composite_dev *cdev)
  620. {
  621. if (cdev->next_string_id < 254) {
  622. /*
  623. * string id 0 is reserved by USB spec for list of
  624. * supported languages
  625. * 255 reserved as well? -- mina86
  626. */
  627. cdev->next_string_id++;
  628. return cdev->next_string_id;
  629. }
  630. return -ENODEV;
  631. }
  632. /**
  633. * usb_string_ids() - allocate unused string IDs in batch
  634. * @cdev: the device whose string descriptor IDs are being allocated
  635. * @str: an array of usb_string objects to assign numbers to
  636. * Context: single threaded during gadget setup
  637. *
  638. * @usb_string_ids() is called from bind() callbacks to allocate
  639. * string IDs. Drivers for functions, configurations, or gadgets will
  640. * then copy IDs from the string table to the appropriate descriptors
  641. * and string table for other languages.
  642. *
  643. * All string identifier should be allocated using this,
  644. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  645. * example different functions don't wrongly assign different meanings
  646. * to the same identifier.
  647. */
  648. int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
  649. {
  650. u8 next = cdev->next_string_id;
  651. for (; str->s; ++str) {
  652. if (next >= 254)
  653. return -ENODEV;
  654. str->id = ++next;
  655. }
  656. cdev->next_string_id = next;
  657. return 0;
  658. }
  659. /**
  660. * usb_string_ids_n() - allocate unused string IDs in batch
  661. * @c: the device whose string descriptor IDs are being allocated
  662. * @n: number of string IDs to allocate
  663. * Context: single threaded during gadget setup
  664. *
  665. * Returns the first requested ID. This ID and next @n-1 IDs are now
  666. * valid IDs. At least provided that @n is non-zero because if it
  667. * is, returns last requested ID which is now very useful information.
  668. *
  669. * @usb_string_ids_n() is called from bind() callbacks to allocate
  670. * string IDs. Drivers for functions, configurations, or gadgets will
  671. * then store that ID in the appropriate descriptors and string table.
  672. *
  673. * All string identifier should be allocated using this,
  674. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  675. * example different functions don't wrongly assign different meanings
  676. * to the same identifier.
  677. */
  678. int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
  679. {
  680. u8 next = c->next_string_id;
  681. if (n > 254 || next + n > 254)
  682. return -ENODEV;
  683. c->next_string_id += n;
  684. return next + 1;
  685. }
  686. static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
  687. {
  688. if (req->status || req->actual != req->length)
  689. debug("%s: setup complete --> %d, %d/%d\n", __func__,
  690. req->status, req->actual, req->length);
  691. }
  692. static int bos_desc(struct usb_composite_dev *cdev)
  693. {
  694. struct usb_ext_cap_descriptor *usb_ext;
  695. struct usb_dcd_config_params dcd_config_params;
  696. struct usb_bos_descriptor *bos = cdev->req->buf;
  697. bos->bLength = USB_DT_BOS_SIZE;
  698. bos->bDescriptorType = USB_DT_BOS;
  699. bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
  700. bos->bNumDeviceCaps = 0;
  701. /*
  702. * A SuperSpeed device shall include the USB2.0 extension descriptor
  703. * and shall support LPM when operating in USB2.0 HS mode.
  704. */
  705. usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  706. bos->bNumDeviceCaps++;
  707. le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength,
  708. USB_DT_USB_EXT_CAP_SIZE);
  709. usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
  710. usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  711. usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
  712. usb_ext->bmAttributes =
  713. cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
  714. /*
  715. * The Superspeed USB Capability descriptor shall be implemented
  716. * by all SuperSpeed devices.
  717. */
  718. if (gadget_is_superspeed(cdev->gadget)) {
  719. struct usb_ss_cap_descriptor *ss_cap;
  720. ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  721. bos->bNumDeviceCaps++;
  722. le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength,
  723. USB_DT_USB_SS_CAP_SIZE);
  724. ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
  725. ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  726. ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
  727. ss_cap->bmAttributes = 0; /* LTM is not supported yet */
  728. ss_cap->wSpeedSupported =
  729. cpu_to_le16(USB_LOW_SPEED_OPERATION |
  730. USB_FULL_SPEED_OPERATION |
  731. USB_HIGH_SPEED_OPERATION |
  732. USB_5GBPS_OPERATION);
  733. ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
  734. /* Get Controller configuration */
  735. if (cdev->gadget->ops->get_config_params) {
  736. cdev->gadget->ops->get_config_params(
  737. &dcd_config_params);
  738. } else {
  739. dcd_config_params.bU1devExitLat =
  740. USB_DEFAULT_U1_DEV_EXIT_LAT;
  741. dcd_config_params.bU2DevExitLat =
  742. cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
  743. }
  744. ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
  745. ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
  746. }
  747. return le16_to_cpu(bos->wTotalLength);
  748. }
  749. static int count_ext_compat(struct usb_configuration *c)
  750. {
  751. int i, res;
  752. res = 0;
  753. for (i = 0; i < c->next_interface_id; ++i) {
  754. struct usb_function *f;
  755. int j;
  756. f = c->interface[i];
  757. for (j = 0; j < f->os_desc_n; ++j) {
  758. struct usb_os_desc *d;
  759. if (i != f->os_desc_table[j].if_id)
  760. continue;
  761. d = f->os_desc_table[j].os_desc;
  762. if (d && d->ext_compat_id)
  763. ++res;
  764. }
  765. }
  766. BUG_ON(res > 255);
  767. return res;
  768. }
  769. static void fill_ext_compat(struct usb_configuration *c, u8 *buf)
  770. {
  771. int i, count;
  772. count = 16;
  773. for (i = 0; i < c->next_interface_id; ++i) {
  774. struct usb_function *f;
  775. int j;
  776. f = c->interface[i];
  777. for (j = 0; j < f->os_desc_n; ++j) {
  778. struct usb_os_desc *d;
  779. if (i != f->os_desc_table[j].if_id)
  780. continue;
  781. d = f->os_desc_table[j].os_desc;
  782. if (d && d->ext_compat_id) {
  783. *buf++ = i;
  784. *buf++ = 0x01;
  785. memcpy(buf, d->ext_compat_id, 16);
  786. buf += 22;
  787. } else {
  788. ++buf;
  789. *buf = 0x01;
  790. buf += 23;
  791. }
  792. count += 24;
  793. if (count >= 4096)
  794. return;
  795. }
  796. }
  797. }
  798. static int count_ext_prop(struct usb_configuration *c, int interface)
  799. {
  800. struct usb_function *f;
  801. int j;
  802. f = c->interface[interface];
  803. for (j = 0; j < f->os_desc_n; ++j) {
  804. struct usb_os_desc *d;
  805. if (interface != f->os_desc_table[j].if_id)
  806. continue;
  807. d = f->os_desc_table[j].os_desc;
  808. if (d && d->ext_compat_id)
  809. return d->ext_prop_count;
  810. }
  811. return 0;
  812. }
  813. static int len_ext_prop(struct usb_configuration *c, int interface)
  814. {
  815. struct usb_function *f;
  816. struct usb_os_desc *d;
  817. int j, res;
  818. res = 10; /* header length */
  819. f = c->interface[interface];
  820. for (j = 0; j < f->os_desc_n; ++j) {
  821. if (interface != f->os_desc_table[j].if_id)
  822. continue;
  823. d = f->os_desc_table[j].os_desc;
  824. if (d)
  825. return min(res + d->ext_prop_len, 4096);
  826. }
  827. return res;
  828. }
  829. static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
  830. {
  831. struct usb_function *f;
  832. struct usb_os_desc *d;
  833. struct usb_os_desc_ext_prop *ext_prop;
  834. int j, count, n, ret;
  835. u8 *start = buf;
  836. f = c->interface[interface];
  837. for (j = 0; j < f->os_desc_n; ++j) {
  838. if (interface != f->os_desc_table[j].if_id)
  839. continue;
  840. d = f->os_desc_table[j].os_desc;
  841. if (d)
  842. list_for_each_entry(ext_prop, &d->ext_prop, entry) {
  843. /* 4kB minus header length */
  844. n = buf - start;
  845. if (n >= 4086)
  846. return 0;
  847. count = ext_prop->data_len +
  848. ext_prop->name_len + 14;
  849. if (count > 4086 - n)
  850. return -EINVAL;
  851. usb_ext_prop_put_size(buf, count);
  852. usb_ext_prop_put_type(buf, ext_prop->type);
  853. ret = usb_ext_prop_put_name(buf, ext_prop->name,
  854. ext_prop->name_len);
  855. if (ret < 0)
  856. return ret;
  857. switch (ext_prop->type) {
  858. case USB_EXT_PROP_UNICODE:
  859. case USB_EXT_PROP_UNICODE_ENV:
  860. case USB_EXT_PROP_UNICODE_LINK:
  861. usb_ext_prop_put_unicode(buf, ret,
  862. ext_prop->data,
  863. ext_prop->data_len);
  864. break;
  865. case USB_EXT_PROP_BINARY:
  866. usb_ext_prop_put_binary(buf, ret,
  867. ext_prop->data,
  868. ext_prop->data_len);
  869. break;
  870. case USB_EXT_PROP_LE32:
  871. /* not implemented */
  872. case USB_EXT_PROP_BE32:
  873. /* not implemented */
  874. default:
  875. return -EINVAL;
  876. }
  877. buf += count;
  878. }
  879. }
  880. return 0;
  881. }
  882. /*
  883. * The setup() callback implements all the ep0 functionality that's
  884. * not handled lower down, in hardware or the hardware driver(like
  885. * device and endpoint feature flags, and their status). It's all
  886. * housekeeping for the gadget function we're implementing. Most of
  887. * the work is in config and function specific setup.
  888. */
  889. static int
  890. composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  891. {
  892. u16 w_length = le16_to_cpu(ctrl->wLength);
  893. u16 w_index = le16_to_cpu(ctrl->wIndex);
  894. u16 w_value = le16_to_cpu(ctrl->wValue);
  895. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  896. u8 intf = w_index & 0xFF;
  897. int value = -EOPNOTSUPP;
  898. struct usb_request *req = cdev->req;
  899. struct usb_function *f = NULL;
  900. int standard;
  901. u8 endp;
  902. struct usb_configuration *c;
  903. /*
  904. * partial re-init of the response message; the function or the
  905. * gadget might need to intercept e.g. a control-OUT completion
  906. * when we delegate to it.
  907. */
  908. req->zero = 0;
  909. req->complete = composite_setup_complete;
  910. req->length = USB_BUFSIZ;
  911. gadget->ep0->driver_data = cdev;
  912. standard = (ctrl->bRequestType & USB_TYPE_MASK)
  913. == USB_TYPE_STANDARD;
  914. if (!standard)
  915. goto unknown;
  916. switch (ctrl->bRequest) {
  917. /* we handle all standard USB descriptors */
  918. case USB_REQ_GET_DESCRIPTOR:
  919. if (ctrl->bRequestType != USB_DIR_IN)
  920. goto unknown;
  921. switch (w_value >> 8) {
  922. case USB_DT_DEVICE:
  923. cdev->desc.bNumConfigurations =
  924. count_configs(cdev, USB_DT_DEVICE);
  925. cdev->desc.bMaxPacketSize0 =
  926. cdev->gadget->ep0->maxpacket;
  927. if (gadget->speed >= USB_SPEED_SUPER) {
  928. cdev->desc.bcdUSB = cpu_to_le16(0x0310);
  929. cdev->desc.bMaxPacketSize0 = 9;
  930. } else {
  931. cdev->desc.bcdUSB = cpu_to_le16(0x0200);
  932. }
  933. value = min(w_length, (u16) sizeof cdev->desc);
  934. memcpy(req->buf, &cdev->desc, value);
  935. break;
  936. case USB_DT_DEVICE_QUALIFIER:
  937. if (!gadget_is_dualspeed(gadget) ||
  938. gadget->speed >= USB_SPEED_SUPER)
  939. break;
  940. device_qual(cdev);
  941. value = min_t(int, w_length,
  942. sizeof(struct usb_qualifier_descriptor));
  943. break;
  944. case USB_DT_OTHER_SPEED_CONFIG:
  945. if (!gadget_is_dualspeed(gadget) ||
  946. gadget->speed >= USB_SPEED_SUPER)
  947. break;
  948. case USB_DT_CONFIG:
  949. value = config_desc(cdev, w_value);
  950. if (value >= 0)
  951. value = min(w_length, (u16) value);
  952. break;
  953. case USB_DT_STRING:
  954. value = get_string(cdev, req->buf,
  955. w_index, w_value & 0xff);
  956. if (value >= 0)
  957. value = min(w_length, (u16) value);
  958. break;
  959. case USB_DT_BOS:
  960. /*
  961. * Super speed connection should support BOS, and
  962. * USB compliance test (USB 2.0 Command Verifier)
  963. * also issues this request, return for now for
  964. * USB 2.0 connection.
  965. */
  966. if (gadget->speed >= USB_SPEED_SUPER) {
  967. value = bos_desc(cdev);
  968. value = min(w_length, (u16)value);
  969. }
  970. break;
  971. default:
  972. goto unknown;
  973. }
  974. break;
  975. /* any number of configs can work */
  976. case USB_REQ_SET_CONFIGURATION:
  977. if (ctrl->bRequestType != 0)
  978. goto unknown;
  979. if (gadget_is_otg(gadget)) {
  980. if (gadget->a_hnp_support)
  981. debug("HNP available\n");
  982. else if (gadget->a_alt_hnp_support)
  983. debug("HNP on another port\n");
  984. else
  985. debug("HNP inactive\n");
  986. }
  987. value = set_config(cdev, ctrl, w_value);
  988. break;
  989. case USB_REQ_GET_CONFIGURATION:
  990. if (ctrl->bRequestType != USB_DIR_IN)
  991. goto unknown;
  992. if (cdev->config)
  993. *(u8 *)req->buf = cdev->config->bConfigurationValue;
  994. else
  995. *(u8 *)req->buf = 0;
  996. value = min(w_length, (u16) 1);
  997. break;
  998. /*
  999. * function drivers must handle get/set altsetting; if there's
  1000. * no get() method, we know only altsetting zero works.
  1001. */
  1002. case USB_REQ_SET_INTERFACE:
  1003. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  1004. goto unknown;
  1005. if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
  1006. break;
  1007. f = cdev->config->interface[intf];
  1008. if (!f)
  1009. break;
  1010. if (w_value && !f->set_alt)
  1011. break;
  1012. value = f->set_alt(f, w_index, w_value);
  1013. break;
  1014. case USB_REQ_GET_INTERFACE:
  1015. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  1016. goto unknown;
  1017. if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
  1018. break;
  1019. f = cdev->config->interface[intf];
  1020. if (!f)
  1021. break;
  1022. /* lots of interfaces only need altsetting zero... */
  1023. value = f->get_alt ? f->get_alt(f, w_index) : 0;
  1024. if (value < 0)
  1025. break;
  1026. *((u8 *)req->buf) = value;
  1027. value = min(w_length, (u16) 1);
  1028. break;
  1029. default:
  1030. unknown:
  1031. /*
  1032. * OS descriptors handling
  1033. */
  1034. if (CONFIG_IS_ENABLED(USB_GADGET_OS_DESCRIPTORS) && cdev->use_os_string &&
  1035. cdev->os_desc_config && (ctrl->bRequestType & USB_TYPE_VENDOR) &&
  1036. ctrl->bRequest == cdev->b_vendor_code) {
  1037. struct usb_configuration *os_desc_cfg;
  1038. u8 *buf;
  1039. int interface;
  1040. int count = 0;
  1041. buf = req->buf;
  1042. os_desc_cfg = cdev->os_desc_config;
  1043. memset(buf, 0, w_length);
  1044. buf[5] = 0x01;
  1045. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1046. case USB_RECIP_DEVICE:
  1047. if (w_index != 0x4 || (w_value >> 8))
  1048. break;
  1049. buf[6] = w_index;
  1050. if (w_length == 0x10) {
  1051. /* Number of ext compat interfaces */
  1052. count = count_ext_compat(os_desc_cfg);
  1053. buf[8] = count;
  1054. count *= 24; /* 24 B/ext compat desc */
  1055. count += 16; /* header */
  1056. put_unaligned_le32(count, buf);
  1057. value = w_length;
  1058. } else {
  1059. /* "extended compatibility ID"s */
  1060. count = count_ext_compat(os_desc_cfg);
  1061. buf[8] = count;
  1062. count *= 24; /* 24 B/ext compat desc */
  1063. count += 16; /* header */
  1064. put_unaligned_le32(count, buf);
  1065. buf += 16;
  1066. fill_ext_compat(os_desc_cfg, buf);
  1067. value = w_length;
  1068. }
  1069. break;
  1070. case USB_RECIP_INTERFACE:
  1071. if (w_index != 0x5 || (w_value >> 8))
  1072. break;
  1073. interface = w_value & 0xFF;
  1074. buf[6] = w_index;
  1075. if (w_length == 0x0A) {
  1076. count = count_ext_prop(os_desc_cfg,
  1077. interface);
  1078. put_unaligned_le16(count, buf + 8);
  1079. count = len_ext_prop(os_desc_cfg,
  1080. interface);
  1081. put_unaligned_le32(count, buf);
  1082. value = w_length;
  1083. } else {
  1084. count = count_ext_prop(os_desc_cfg,
  1085. interface);
  1086. put_unaligned_le16(count, buf + 8);
  1087. count = len_ext_prop(os_desc_cfg,
  1088. interface);
  1089. put_unaligned_le32(count, buf);
  1090. buf += 10;
  1091. value = fill_ext_prop(os_desc_cfg,
  1092. interface, buf);
  1093. if (value < 0)
  1094. return value;
  1095. value = w_length;
  1096. }
  1097. break;
  1098. }
  1099. if (value >= 0) {
  1100. req->length = value;
  1101. req->zero = value < w_length;
  1102. value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
  1103. if (value < 0) {
  1104. debug("ep_queue --> %d\n", value);
  1105. req->status = 0;
  1106. composite_setup_complete(gadget->ep0, req);
  1107. }
  1108. }
  1109. return value;
  1110. }
  1111. debug("non-core control req%02x.%02x v%04x i%04x l%d\n",
  1112. ctrl->bRequestType, ctrl->bRequest,
  1113. w_value, w_index, w_length);
  1114. if (!cdev->config)
  1115. goto done;
  1116. /*
  1117. * functions always handle their interfaces and endpoints...
  1118. * punt other recipients (other, WUSB, ...) to the current
  1119. * configuration code.
  1120. */
  1121. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1122. case USB_RECIP_INTERFACE:
  1123. f = cdev->config->interface[intf];
  1124. break;
  1125. case USB_RECIP_ENDPOINT:
  1126. endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
  1127. list_for_each_entry(f, &cdev->config->functions, list) {
  1128. if (test_bit(endp, f->endpoints))
  1129. break;
  1130. }
  1131. if (&f->list == &cdev->config->functions)
  1132. f = NULL;
  1133. break;
  1134. /*
  1135. * dfu-util (version 0.5) sets bmRequestType.Receipent = Device
  1136. * for non-standard request (w_value = 0x21,
  1137. * bRequest = GET_DESCRIPTOR in this case).
  1138. * When only one interface is registered (as it is done now),
  1139. * then this request shall be handled as it was requested for
  1140. * interface.
  1141. *
  1142. * In the below code it is checked if only one interface is
  1143. * present and proper function for it is extracted. Due to that
  1144. * function's setup (f->setup) is called to handle this
  1145. * special non-standard request.
  1146. */
  1147. case USB_RECIP_DEVICE:
  1148. debug("cdev->config->next_interface_id: %d intf: %d\n",
  1149. cdev->config->next_interface_id, intf);
  1150. if (cdev->config->next_interface_id == 1)
  1151. f = cdev->config->interface[intf];
  1152. break;
  1153. }
  1154. if (f && f->setup)
  1155. value = f->setup(f, ctrl);
  1156. else {
  1157. c = cdev->config;
  1158. if (c->setup)
  1159. value = c->setup(c, ctrl);
  1160. }
  1161. goto done;
  1162. }
  1163. /* respond with data transfer before status phase? */
  1164. if (value >= 0) {
  1165. req->length = value;
  1166. req->zero = value < w_length;
  1167. value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
  1168. if (value < 0) {
  1169. debug("ep_queue --> %d\n", value);
  1170. req->status = 0;
  1171. composite_setup_complete(gadget->ep0, req);
  1172. }
  1173. }
  1174. done:
  1175. /* device either stalls (value < 0) or reports success */
  1176. return value;
  1177. }
  1178. static void composite_disconnect(struct usb_gadget *gadget)
  1179. {
  1180. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1181. if (cdev->config)
  1182. reset_config(cdev);
  1183. if (composite->disconnect)
  1184. composite->disconnect(cdev);
  1185. }
  1186. static void composite_unbind(struct usb_gadget *gadget)
  1187. {
  1188. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1189. struct usb_configuration *c;
  1190. struct usb_function *f;
  1191. /*
  1192. * composite_disconnect() must already have been called
  1193. * by the underlying peripheral controller driver!
  1194. * so there's no i/o concurrency that could affect the
  1195. * state protected by cdev->lock.
  1196. */
  1197. #ifdef __UBOOT__
  1198. assert_noisy(!cdev->config);
  1199. #else
  1200. BUG_ON(cdev->config);
  1201. #endif
  1202. while (!list_empty(&cdev->configs)) {
  1203. c = list_first_entry(&cdev->configs,
  1204. struct usb_configuration, list);
  1205. while (!list_empty(&c->functions)) {
  1206. f = list_first_entry(&c->functions,
  1207. struct usb_function, list);
  1208. list_del(&f->list);
  1209. if (f->unbind) {
  1210. debug("unbind function '%s'/%p\n",
  1211. f->name, f);
  1212. f->unbind(c, f);
  1213. }
  1214. }
  1215. list_del(&c->list);
  1216. if (c->unbind) {
  1217. debug("unbind config '%s'/%p\n", c->label, c);
  1218. c->unbind(c);
  1219. }
  1220. free(c);
  1221. }
  1222. if (composite->unbind)
  1223. composite->unbind(cdev);
  1224. if (cdev->req) {
  1225. kfree(cdev->req->buf);
  1226. usb_ep_free_request(gadget->ep0, cdev->req);
  1227. }
  1228. kfree(cdev);
  1229. set_gadget_data(gadget, NULL);
  1230. composite = NULL;
  1231. }
  1232. static int composite_bind(struct usb_gadget *gadget)
  1233. {
  1234. int status = -ENOMEM;
  1235. struct usb_composite_dev *cdev;
  1236. cdev = calloc(sizeof *cdev, 1);
  1237. if (!cdev)
  1238. return status;
  1239. cdev->gadget = gadget;
  1240. set_gadget_data(gadget, cdev);
  1241. INIT_LIST_HEAD(&cdev->configs);
  1242. /* preallocate control response and buffer */
  1243. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  1244. if (!cdev->req)
  1245. goto fail;
  1246. cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ);
  1247. if (!cdev->req->buf)
  1248. goto fail;
  1249. cdev->req->complete = composite_setup_complete;
  1250. gadget->ep0->driver_data = cdev;
  1251. cdev->bufsiz = USB_BUFSIZ;
  1252. cdev->driver = composite;
  1253. usb_gadget_set_selfpowered(gadget);
  1254. usb_ep_autoconfig_reset(cdev->gadget);
  1255. status = composite->bind(cdev);
  1256. if (status < 0)
  1257. goto fail;
  1258. memcpy(&cdev->desc, composite->dev,
  1259. sizeof(struct usb_device_descriptor));
  1260. cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  1261. if (cdev->use_os_string) {
  1262. /* TODO: Do we want to pass this via platform? */
  1263. cdev->b_vendor_code = 0x40;
  1264. /* Microsoft OS String Descriptor */
  1265. utf8_to_utf16le(qw_sign_buf, (__le16 *)cdev->qw_sign,
  1266. OS_STRING_QW_SIGN_LEN / 2);
  1267. }
  1268. debug("%s: ready\n", composite->name);
  1269. return 0;
  1270. fail:
  1271. composite_unbind(gadget);
  1272. return status;
  1273. }
  1274. static void
  1275. composite_suspend(struct usb_gadget *gadget)
  1276. {
  1277. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1278. struct usb_function *f;
  1279. debug("%s: suspend\n", __func__);
  1280. if (cdev->config) {
  1281. list_for_each_entry(f, &cdev->config->functions, list) {
  1282. if (f->suspend)
  1283. f->suspend(f);
  1284. }
  1285. }
  1286. if (composite->suspend)
  1287. composite->suspend(cdev);
  1288. cdev->suspended = 1;
  1289. }
  1290. static void
  1291. composite_resume(struct usb_gadget *gadget)
  1292. {
  1293. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1294. struct usb_function *f;
  1295. debug("%s: resume\n", __func__);
  1296. if (composite->resume)
  1297. composite->resume(cdev);
  1298. if (cdev->config) {
  1299. list_for_each_entry(f, &cdev->config->functions, list) {
  1300. if (f->resume)
  1301. f->resume(f);
  1302. }
  1303. }
  1304. cdev->suspended = 0;
  1305. }
  1306. static struct usb_gadget_driver composite_driver = {
  1307. .speed = USB_SPEED_SUPER,
  1308. .bind = composite_bind,
  1309. .unbind = composite_unbind,
  1310. .setup = composite_setup,
  1311. .reset = composite_disconnect,
  1312. .disconnect = composite_disconnect,
  1313. .suspend = composite_suspend,
  1314. .resume = composite_resume,
  1315. };
  1316. /**
  1317. * usb_composite_register() - register a composite driver
  1318. * @driver: the driver to register
  1319. * Context: single threaded during gadget setup
  1320. *
  1321. * This function is used to register drivers using the composite driver
  1322. * framework. The return value is zero, or a negative errno value.
  1323. * Those values normally come from the driver's @bind method, which does
  1324. * all the work of setting up the driver to match the hardware.
  1325. *
  1326. * On successful return, the gadget is ready to respond to requests from
  1327. * the host, unless one of its components invokes usb_gadget_disconnect()
  1328. * while it was binding. That would usually be done in order to wait for
  1329. * some userspace participation.
  1330. */
  1331. int usb_composite_register(struct usb_composite_driver *driver)
  1332. {
  1333. int res;
  1334. if (!driver || !driver->dev || !driver->bind || composite)
  1335. return -EINVAL;
  1336. if (!driver->name)
  1337. driver->name = "composite";
  1338. composite = driver;
  1339. res = usb_gadget_register_driver(&composite_driver);
  1340. if (res != 0)
  1341. composite = NULL;
  1342. return res;
  1343. }
  1344. /**
  1345. * usb_composite_unregister() - unregister a composite driver
  1346. * @driver: the driver to unregister
  1347. *
  1348. * This function is used to unregister drivers using the composite
  1349. * driver framework.
  1350. */
  1351. void usb_composite_unregister(struct usb_composite_driver *driver)
  1352. {
  1353. if (composite != driver)
  1354. return;
  1355. usb_gadget_unregister_driver(&composite_driver);
  1356. composite = NULL;
  1357. }