composite.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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 <linux/bitops.h>
  10. #include <linux/usb/composite.h>
  11. #define USB_BUFSIZ 4096
  12. static struct usb_composite_driver *composite;
  13. /**
  14. * usb_add_function() - add a function to a configuration
  15. * @config: the configuration
  16. * @function: the function being added
  17. * Context: single threaded during gadget setup
  18. *
  19. * After initialization, each configuration must have one or more
  20. * functions added to it. Adding a function involves calling its @bind()
  21. * method to allocate resources such as interface and string identifiers
  22. * and endpoints.
  23. *
  24. * This function returns the value of the function's bind(), which is
  25. * zero for success else a negative errno value.
  26. */
  27. int usb_add_function(struct usb_configuration *config,
  28. struct usb_function *function)
  29. {
  30. int value = -EINVAL;
  31. debug("adding '%s'/%p to config '%s'/%p\n",
  32. function->name, function,
  33. config->label, config);
  34. if (!function->set_alt || !function->disable)
  35. goto done;
  36. function->config = config;
  37. list_add_tail(&function->list, &config->functions);
  38. if (function->bind) {
  39. value = function->bind(config, function);
  40. if (value < 0) {
  41. list_del(&function->list);
  42. function->config = NULL;
  43. }
  44. } else
  45. value = 0;
  46. if (!config->fullspeed && function->descriptors)
  47. config->fullspeed = 1;
  48. if (!config->highspeed && function->hs_descriptors)
  49. config->highspeed = 1;
  50. done:
  51. if (value)
  52. debug("adding '%s'/%p --> %d\n",
  53. function->name, function, value);
  54. return value;
  55. }
  56. /**
  57. * usb_function_deactivate - prevent function and gadget enumeration
  58. * @function: the function that isn't yet ready to respond
  59. *
  60. * Blocks response of the gadget driver to host enumeration by
  61. * preventing the data line pullup from being activated. This is
  62. * normally called during @bind() processing to change from the
  63. * initial "ready to respond" state, or when a required resource
  64. * becomes available.
  65. *
  66. * For example, drivers that serve as a passthrough to a userspace
  67. * daemon can block enumeration unless that daemon (such as an OBEX,
  68. * MTP, or print server) is ready to handle host requests.
  69. *
  70. * Not all systems support software control of their USB peripheral
  71. * data pullups.
  72. *
  73. * Returns zero on success, else negative errno.
  74. */
  75. int usb_function_deactivate(struct usb_function *function)
  76. {
  77. struct usb_composite_dev *cdev = function->config->cdev;
  78. int status = 0;
  79. if (cdev->deactivations == 0)
  80. status = usb_gadget_disconnect(cdev->gadget);
  81. if (status == 0)
  82. cdev->deactivations++;
  83. return status;
  84. }
  85. /**
  86. * usb_function_activate - allow function and gadget enumeration
  87. * @function: function on which usb_function_activate() was called
  88. *
  89. * Reverses effect of usb_function_deactivate(). If no more functions
  90. * are delaying their activation, the gadget driver will respond to
  91. * host enumeration procedures.
  92. *
  93. * Returns zero on success, else negative errno.
  94. */
  95. int usb_function_activate(struct usb_function *function)
  96. {
  97. struct usb_composite_dev *cdev = function->config->cdev;
  98. int status = 0;
  99. if (cdev->deactivations == 0)
  100. status = -EINVAL;
  101. else {
  102. cdev->deactivations--;
  103. if (cdev->deactivations == 0)
  104. status = usb_gadget_connect(cdev->gadget);
  105. }
  106. return status;
  107. }
  108. /**
  109. * usb_interface_id() - allocate an unused interface ID
  110. * @config: configuration associated with the interface
  111. * @function: function handling the interface
  112. * Context: single threaded during gadget setup
  113. *
  114. * usb_interface_id() is called from usb_function.bind() callbacks to
  115. * allocate new interface IDs. The function driver will then store that
  116. * ID in interface, association, CDC union, and other descriptors. It
  117. * will also handle any control requests targetted at that interface,
  118. * particularly changing its altsetting via set_alt(). There may
  119. * also be class-specific or vendor-specific requests to handle.
  120. *
  121. * All interface identifier should be allocated using this routine, to
  122. * ensure that for example different functions don't wrongly assign
  123. * different meanings to the same identifier. Note that since interface
  124. * identifers are configuration-specific, functions used in more than
  125. * one configuration (or more than once in a given configuration) need
  126. * multiple versions of the relevant descriptors.
  127. *
  128. * Returns the interface ID which was allocated; or -ENODEV if no
  129. * more interface IDs can be allocated.
  130. */
  131. int usb_interface_id(struct usb_configuration *config,
  132. struct usb_function *function)
  133. {
  134. unsigned char id = config->next_interface_id;
  135. if (id < MAX_CONFIG_INTERFACES) {
  136. config->interface[id] = function;
  137. config->next_interface_id = id + 1;
  138. return id;
  139. }
  140. return -ENODEV;
  141. }
  142. static int config_buf(struct usb_configuration *config,
  143. enum usb_device_speed speed, void *buf, u8 type)
  144. {
  145. int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
  146. void *next = buf + USB_DT_CONFIG_SIZE;
  147. struct usb_descriptor_header **descriptors;
  148. struct usb_config_descriptor *c;
  149. int status;
  150. struct usb_function *f;
  151. /* write the config descriptor */
  152. c = buf;
  153. c->bLength = USB_DT_CONFIG_SIZE;
  154. c->bDescriptorType = type;
  155. c->bNumInterfaces = config->next_interface_id;
  156. c->bConfigurationValue = config->bConfigurationValue;
  157. c->iConfiguration = config->iConfiguration;
  158. c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
  159. c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
  160. /* There may be e.g. OTG descriptors */
  161. if (config->descriptors) {
  162. status = usb_descriptor_fillbuf(next, len,
  163. config->descriptors);
  164. if (status < 0)
  165. return status;
  166. len -= status;
  167. next += status;
  168. }
  169. /* add each function's descriptors */
  170. list_for_each_entry(f, &config->functions, list) {
  171. if (speed == USB_SPEED_HIGH)
  172. descriptors = f->hs_descriptors;
  173. else
  174. descriptors = f->descriptors;
  175. if (!descriptors)
  176. continue;
  177. status = usb_descriptor_fillbuf(next, len,
  178. (const struct usb_descriptor_header **) descriptors);
  179. if (status < 0)
  180. return status;
  181. len -= status;
  182. next += status;
  183. }
  184. len = next - buf;
  185. c->wTotalLength = cpu_to_le16(len);
  186. return len;
  187. }
  188. static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
  189. {
  190. enum usb_device_speed speed = USB_SPEED_UNKNOWN;
  191. struct usb_gadget *gadget = cdev->gadget;
  192. u8 type = w_value >> 8;
  193. int hs = 0;
  194. struct usb_configuration *c;
  195. if (gadget_is_dualspeed(gadget)) {
  196. if (gadget->speed == USB_SPEED_HIGH)
  197. hs = 1;
  198. if (type == USB_DT_OTHER_SPEED_CONFIG)
  199. hs = !hs;
  200. if (hs)
  201. speed = USB_SPEED_HIGH;
  202. }
  203. w_value &= 0xff;
  204. list_for_each_entry(c, &cdev->configs, list) {
  205. if (speed == USB_SPEED_HIGH) {
  206. if (!c->highspeed)
  207. continue;
  208. } else {
  209. if (!c->fullspeed)
  210. continue;
  211. }
  212. if (w_value == 0)
  213. return config_buf(c, speed, cdev->req->buf, type);
  214. w_value--;
  215. }
  216. return -EINVAL;
  217. }
  218. static int count_configs(struct usb_composite_dev *cdev, unsigned type)
  219. {
  220. struct usb_gadget *gadget = cdev->gadget;
  221. unsigned count = 0;
  222. int hs = 0;
  223. struct usb_configuration *c;
  224. if (gadget_is_dualspeed(gadget)) {
  225. if (gadget->speed == USB_SPEED_HIGH)
  226. hs = 1;
  227. if (type == USB_DT_DEVICE_QUALIFIER)
  228. hs = !hs;
  229. }
  230. list_for_each_entry(c, &cdev->configs, list) {
  231. /* ignore configs that won't work at this speed */
  232. if (hs) {
  233. if (!c->highspeed)
  234. continue;
  235. } else {
  236. if (!c->fullspeed)
  237. continue;
  238. }
  239. count++;
  240. }
  241. return count;
  242. }
  243. static void device_qual(struct usb_composite_dev *cdev)
  244. {
  245. struct usb_qualifier_descriptor *qual = cdev->req->buf;
  246. qual->bLength = sizeof(*qual);
  247. qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  248. /* POLICY: same bcdUSB and device type info at both speeds */
  249. qual->bcdUSB = cdev->desc.bcdUSB;
  250. qual->bDeviceClass = cdev->desc.bDeviceClass;
  251. qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
  252. qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
  253. /* ASSUME same EP0 fifo size at both speeds */
  254. qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
  255. qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
  256. qual->bRESERVED = 0;
  257. }
  258. static void reset_config(struct usb_composite_dev *cdev)
  259. {
  260. struct usb_function *f;
  261. debug("%s:\n", __func__);
  262. list_for_each_entry(f, &cdev->config->functions, list) {
  263. if (f->disable)
  264. f->disable(f);
  265. bitmap_zero(f->endpoints, 32);
  266. }
  267. cdev->config = NULL;
  268. }
  269. static int set_config(struct usb_composite_dev *cdev,
  270. const struct usb_ctrlrequest *ctrl, unsigned number)
  271. {
  272. struct usb_gadget *gadget = cdev->gadget;
  273. unsigned power = gadget_is_otg(gadget) ? 8 : 100;
  274. struct usb_descriptor_header **descriptors;
  275. int result = -EINVAL;
  276. struct usb_endpoint_descriptor *ep;
  277. struct usb_configuration *c = NULL;
  278. int addr;
  279. int tmp;
  280. struct usb_function *f;
  281. if (cdev->config)
  282. reset_config(cdev);
  283. if (number) {
  284. list_for_each_entry(c, &cdev->configs, list) {
  285. if (c->bConfigurationValue == number) {
  286. result = 0;
  287. break;
  288. }
  289. }
  290. if (result < 0)
  291. goto done;
  292. } else
  293. result = 0;
  294. debug("%s: %s speed config #%d: %s\n", __func__,
  295. ({ char *speed;
  296. switch (gadget->speed) {
  297. case USB_SPEED_LOW:
  298. speed = "low";
  299. break;
  300. case USB_SPEED_FULL:
  301. speed = "full";
  302. break;
  303. case USB_SPEED_HIGH:
  304. speed = "high";
  305. break;
  306. default:
  307. speed = "?";
  308. break;
  309. };
  310. speed;
  311. }), number, c ? c->label : "unconfigured");
  312. if (!c)
  313. goto done;
  314. cdev->config = c;
  315. /* Initialize all interfaces by setting them to altsetting zero. */
  316. for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
  317. f = c->interface[tmp];
  318. if (!f)
  319. break;
  320. /*
  321. * Record which endpoints are used by the function. This is used
  322. * to dispatch control requests targeted at that endpoint to the
  323. * function's setup callback instead of the current
  324. * configuration's setup callback.
  325. */
  326. if (gadget->speed == USB_SPEED_HIGH)
  327. descriptors = f->hs_descriptors;
  328. else
  329. descriptors = f->descriptors;
  330. for (; *descriptors; ++descriptors) {
  331. if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
  332. continue;
  333. ep = (struct usb_endpoint_descriptor *)*descriptors;
  334. addr = ((ep->bEndpointAddress & 0x80) >> 3)
  335. | (ep->bEndpointAddress & 0x0f);
  336. generic_set_bit(addr, f->endpoints);
  337. }
  338. result = f->set_alt(f, tmp, 0);
  339. if (result < 0) {
  340. debug("interface %d (%s/%p) alt 0 --> %d\n",
  341. tmp, f->name, f, result);
  342. reset_config(cdev);
  343. goto done;
  344. }
  345. }
  346. /* when we return, be sure our power usage is valid */
  347. power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
  348. done:
  349. usb_gadget_vbus_draw(gadget, power);
  350. return result;
  351. }
  352. /**
  353. * usb_add_config() - add a configuration to a device.
  354. * @cdev: wraps the USB gadget
  355. * @config: the configuration, with bConfigurationValue assigned
  356. * Context: single threaded during gadget setup
  357. *
  358. * One of the main tasks of a composite driver's bind() routine is to
  359. * add each of the configurations it supports, using this routine.
  360. *
  361. * This function returns the value of the configuration's bind(), which
  362. * is zero for success else a negative errno value. Binding configurations
  363. * assigns global resources including string IDs, and per-configuration
  364. * resources such as interface IDs and endpoints.
  365. */
  366. int usb_add_config(struct usb_composite_dev *cdev,
  367. struct usb_configuration *config)
  368. {
  369. int status = -EINVAL;
  370. struct usb_configuration *c;
  371. struct usb_function *f;
  372. unsigned int i;
  373. debug("%s: adding config #%u '%s'/%p\n", __func__,
  374. config->bConfigurationValue,
  375. config->label, config);
  376. if (!config->bConfigurationValue || !config->bind)
  377. goto done;
  378. /* Prevent duplicate configuration identifiers */
  379. list_for_each_entry(c, &cdev->configs, list) {
  380. if (c->bConfigurationValue == config->bConfigurationValue) {
  381. status = -EBUSY;
  382. goto done;
  383. }
  384. }
  385. config->cdev = cdev;
  386. list_add_tail(&config->list, &cdev->configs);
  387. INIT_LIST_HEAD(&config->functions);
  388. config->next_interface_id = 0;
  389. status = config->bind(config);
  390. if (status < 0) {
  391. list_del(&config->list);
  392. config->cdev = NULL;
  393. } else {
  394. debug("cfg %d/%p speeds:%s%s\n",
  395. config->bConfigurationValue, config,
  396. config->highspeed ? " high" : "",
  397. config->fullspeed
  398. ? (gadget_is_dualspeed(cdev->gadget)
  399. ? " full"
  400. : " full/low")
  401. : "");
  402. for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
  403. f = config->interface[i];
  404. if (!f)
  405. continue;
  406. debug("%s: interface %d = %s/%p\n",
  407. __func__, i, f->name, f);
  408. }
  409. }
  410. usb_ep_autoconfig_reset(cdev->gadget);
  411. done:
  412. if (status)
  413. debug("added config '%s'/%u --> %d\n", config->label,
  414. config->bConfigurationValue, status);
  415. return status;
  416. }
  417. /*
  418. * We support strings in multiple languages ... string descriptor zero
  419. * says which languages are supported. The typical case will be that
  420. * only one language (probably English) is used, with I18N handled on
  421. * the host side.
  422. */
  423. static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
  424. {
  425. const struct usb_gadget_strings *s;
  426. u16 language;
  427. __le16 *tmp;
  428. while (*sp) {
  429. s = *sp;
  430. language = cpu_to_le16(s->language);
  431. for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
  432. if (*tmp == language)
  433. goto repeat;
  434. }
  435. *tmp++ = language;
  436. repeat:
  437. sp++;
  438. }
  439. }
  440. static int lookup_string(
  441. struct usb_gadget_strings **sp,
  442. void *buf,
  443. u16 language,
  444. int id
  445. )
  446. {
  447. int value;
  448. struct usb_gadget_strings *s;
  449. while (*sp) {
  450. s = *sp++;
  451. if (s->language != language)
  452. continue;
  453. value = usb_gadget_get_string(s, id, buf);
  454. if (value > 0)
  455. return value;
  456. }
  457. return -EINVAL;
  458. }
  459. static int get_string(struct usb_composite_dev *cdev,
  460. void *buf, u16 language, int id)
  461. {
  462. struct usb_string_descriptor *s = buf;
  463. struct usb_gadget_strings **sp;
  464. int len;
  465. struct usb_configuration *c;
  466. struct usb_function *f;
  467. /*
  468. * Yes, not only is USB's I18N support probably more than most
  469. * folk will ever care about ... also, it's all supported here.
  470. * (Except for UTF8 support for Unicode's "Astral Planes".)
  471. */
  472. /* 0 == report all available language codes */
  473. if (id == 0) {
  474. memset(s, 0, 256);
  475. s->bDescriptorType = USB_DT_STRING;
  476. sp = composite->strings;
  477. if (sp)
  478. collect_langs(sp, s->wData);
  479. list_for_each_entry(c, &cdev->configs, list) {
  480. sp = c->strings;
  481. if (sp)
  482. collect_langs(sp, s->wData);
  483. list_for_each_entry(f, &c->functions, list) {
  484. sp = f->strings;
  485. if (sp)
  486. collect_langs(sp, s->wData);
  487. }
  488. }
  489. for (len = 0; len <= 126 && s->wData[len]; len++)
  490. continue;
  491. if (!len)
  492. return -EINVAL;
  493. s->bLength = 2 * (len + 1);
  494. return s->bLength;
  495. }
  496. /*
  497. * Otherwise, look up and return a specified string. String IDs
  498. * are device-scoped, so we look up each string table we're told
  499. * about. These lookups are infrequent; simpler-is-better here.
  500. */
  501. if (composite->strings) {
  502. len = lookup_string(composite->strings, buf, language, id);
  503. if (len > 0)
  504. return len;
  505. }
  506. list_for_each_entry(c, &cdev->configs, list) {
  507. if (c->strings) {
  508. len = lookup_string(c->strings, buf, language, id);
  509. if (len > 0)
  510. return len;
  511. }
  512. list_for_each_entry(f, &c->functions, list) {
  513. if (!f->strings)
  514. continue;
  515. len = lookup_string(f->strings, buf, language, id);
  516. if (len > 0)
  517. return len;
  518. }
  519. }
  520. return -EINVAL;
  521. }
  522. /**
  523. * usb_string_id() - allocate an unused string ID
  524. * @cdev: the device whose string descriptor IDs are being allocated
  525. * Context: single threaded during gadget setup
  526. *
  527. * @usb_string_id() is called from bind() callbacks to allocate
  528. * string IDs. Drivers for functions, configurations, or gadgets will
  529. * then store that ID in the appropriate descriptors and string table.
  530. *
  531. * All string identifier should be allocated using this,
  532. * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
  533. * that for example different functions don't wrongly assign different
  534. * meanings to the same identifier.
  535. */
  536. int usb_string_id(struct usb_composite_dev *cdev)
  537. {
  538. if (cdev->next_string_id < 254) {
  539. /*
  540. * string id 0 is reserved by USB spec for list of
  541. * supported languages
  542. * 255 reserved as well? -- mina86
  543. */
  544. cdev->next_string_id++;
  545. return cdev->next_string_id;
  546. }
  547. return -ENODEV;
  548. }
  549. /**
  550. * usb_string_ids() - allocate unused string IDs in batch
  551. * @cdev: the device whose string descriptor IDs are being allocated
  552. * @str: an array of usb_string objects to assign numbers to
  553. * Context: single threaded during gadget setup
  554. *
  555. * @usb_string_ids() is called from bind() callbacks to allocate
  556. * string IDs. Drivers for functions, configurations, or gadgets will
  557. * then copy IDs from the string table to the appropriate descriptors
  558. * and string table for other languages.
  559. *
  560. * All string identifier should be allocated using this,
  561. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  562. * example different functions don't wrongly assign different meanings
  563. * to the same identifier.
  564. */
  565. int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
  566. {
  567. u8 next = cdev->next_string_id;
  568. for (; str->s; ++str) {
  569. if (next >= 254)
  570. return -ENODEV;
  571. str->id = ++next;
  572. }
  573. cdev->next_string_id = next;
  574. return 0;
  575. }
  576. /**
  577. * usb_string_ids_n() - allocate unused string IDs in batch
  578. * @c: the device whose string descriptor IDs are being allocated
  579. * @n: number of string IDs to allocate
  580. * Context: single threaded during gadget setup
  581. *
  582. * Returns the first requested ID. This ID and next @n-1 IDs are now
  583. * valid IDs. At least provided that @n is non-zero because if it
  584. * is, returns last requested ID which is now very useful information.
  585. *
  586. * @usb_string_ids_n() is called from bind() callbacks to allocate
  587. * string IDs. Drivers for functions, configurations, or gadgets will
  588. * then store that ID in the appropriate descriptors and string table.
  589. *
  590. * All string identifier should be allocated using this,
  591. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  592. * example different functions don't wrongly assign different meanings
  593. * to the same identifier.
  594. */
  595. int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
  596. {
  597. u8 next = c->next_string_id;
  598. if (n > 254 || next + n > 254)
  599. return -ENODEV;
  600. c->next_string_id += n;
  601. return next + 1;
  602. }
  603. static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
  604. {
  605. if (req->status || req->actual != req->length)
  606. debug("%s: setup complete --> %d, %d/%d\n", __func__,
  607. req->status, req->actual, req->length);
  608. }
  609. /*
  610. * The setup() callback implements all the ep0 functionality that's
  611. * not handled lower down, in hardware or the hardware driver(like
  612. * device and endpoint feature flags, and their status). It's all
  613. * housekeeping for the gadget function we're implementing. Most of
  614. * the work is in config and function specific setup.
  615. */
  616. static int
  617. composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  618. {
  619. u16 w_length = le16_to_cpu(ctrl->wLength);
  620. u16 w_index = le16_to_cpu(ctrl->wIndex);
  621. u16 w_value = le16_to_cpu(ctrl->wValue);
  622. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  623. u8 intf = w_index & 0xFF;
  624. int value = -EOPNOTSUPP;
  625. struct usb_request *req = cdev->req;
  626. struct usb_function *f = NULL;
  627. int standard;
  628. u8 endp;
  629. struct usb_configuration *c;
  630. /*
  631. * partial re-init of the response message; the function or the
  632. * gadget might need to intercept e.g. a control-OUT completion
  633. * when we delegate to it.
  634. */
  635. req->zero = 0;
  636. req->complete = composite_setup_complete;
  637. req->length = USB_BUFSIZ;
  638. gadget->ep0->driver_data = cdev;
  639. standard = (ctrl->bRequestType & USB_TYPE_MASK)
  640. == USB_TYPE_STANDARD;
  641. if (!standard)
  642. goto unknown;
  643. switch (ctrl->bRequest) {
  644. /* we handle all standard USB descriptors */
  645. case USB_REQ_GET_DESCRIPTOR:
  646. if (ctrl->bRequestType != USB_DIR_IN)
  647. goto unknown;
  648. switch (w_value >> 8) {
  649. case USB_DT_DEVICE:
  650. cdev->desc.bNumConfigurations =
  651. count_configs(cdev, USB_DT_DEVICE);
  652. cdev->desc.bMaxPacketSize0 =
  653. cdev->gadget->ep0->maxpacket;
  654. value = min(w_length, (u16) sizeof cdev->desc);
  655. memcpy(req->buf, &cdev->desc, value);
  656. break;
  657. case USB_DT_DEVICE_QUALIFIER:
  658. if (!gadget_is_dualspeed(gadget))
  659. break;
  660. device_qual(cdev);
  661. value = min_t(int, w_length,
  662. sizeof(struct usb_qualifier_descriptor));
  663. break;
  664. case USB_DT_OTHER_SPEED_CONFIG:
  665. if (!gadget_is_dualspeed(gadget))
  666. break;
  667. case USB_DT_CONFIG:
  668. value = config_desc(cdev, w_value);
  669. if (value >= 0)
  670. value = min(w_length, (u16) value);
  671. break;
  672. case USB_DT_STRING:
  673. value = get_string(cdev, req->buf,
  674. w_index, w_value & 0xff);
  675. if (value >= 0)
  676. value = min(w_length, (u16) value);
  677. break;
  678. case USB_DT_BOS:
  679. /*
  680. * The USB compliance test (USB 2.0 Command Verifier)
  681. * issues this request. We should not run into the
  682. * default path here. But return for now until
  683. * the superspeed support is added.
  684. */
  685. break;
  686. default:
  687. goto unknown;
  688. }
  689. break;
  690. /* any number of configs can work */
  691. case USB_REQ_SET_CONFIGURATION:
  692. if (ctrl->bRequestType != 0)
  693. goto unknown;
  694. if (gadget_is_otg(gadget)) {
  695. if (gadget->a_hnp_support)
  696. debug("HNP available\n");
  697. else if (gadget->a_alt_hnp_support)
  698. debug("HNP on another port\n");
  699. else
  700. debug("HNP inactive\n");
  701. }
  702. value = set_config(cdev, ctrl, w_value);
  703. break;
  704. case USB_REQ_GET_CONFIGURATION:
  705. if (ctrl->bRequestType != USB_DIR_IN)
  706. goto unknown;
  707. if (cdev->config)
  708. *(u8 *)req->buf = cdev->config->bConfigurationValue;
  709. else
  710. *(u8 *)req->buf = 0;
  711. value = min(w_length, (u16) 1);
  712. break;
  713. /*
  714. * function drivers must handle get/set altsetting; if there's
  715. * no get() method, we know only altsetting zero works.
  716. */
  717. case USB_REQ_SET_INTERFACE:
  718. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  719. goto unknown;
  720. if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
  721. break;
  722. f = cdev->config->interface[intf];
  723. if (!f)
  724. break;
  725. if (w_value && !f->set_alt)
  726. break;
  727. value = f->set_alt(f, w_index, w_value);
  728. break;
  729. case USB_REQ_GET_INTERFACE:
  730. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  731. goto unknown;
  732. if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
  733. break;
  734. f = cdev->config->interface[intf];
  735. if (!f)
  736. break;
  737. /* lots of interfaces only need altsetting zero... */
  738. value = f->get_alt ? f->get_alt(f, w_index) : 0;
  739. if (value < 0)
  740. break;
  741. *((u8 *)req->buf) = value;
  742. value = min(w_length, (u16) 1);
  743. break;
  744. default:
  745. unknown:
  746. debug("non-core control req%02x.%02x v%04x i%04x l%d\n",
  747. ctrl->bRequestType, ctrl->bRequest,
  748. w_value, w_index, w_length);
  749. if (!cdev->config)
  750. goto done;
  751. /*
  752. * functions always handle their interfaces and endpoints...
  753. * punt other recipients (other, WUSB, ...) to the current
  754. * configuration code.
  755. */
  756. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  757. case USB_RECIP_INTERFACE:
  758. f = cdev->config->interface[intf];
  759. break;
  760. case USB_RECIP_ENDPOINT:
  761. endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
  762. list_for_each_entry(f, &cdev->config->functions, list) {
  763. if (test_bit(endp, f->endpoints))
  764. break;
  765. }
  766. if (&f->list == &cdev->config->functions)
  767. f = NULL;
  768. break;
  769. /*
  770. * dfu-util (version 0.5) sets bmRequestType.Receipent = Device
  771. * for non-standard request (w_value = 0x21,
  772. * bRequest = GET_DESCRIPTOR in this case).
  773. * When only one interface is registered (as it is done now),
  774. * then this request shall be handled as it was requested for
  775. * interface.
  776. *
  777. * In the below code it is checked if only one interface is
  778. * present and proper function for it is extracted. Due to that
  779. * function's setup (f->setup) is called to handle this
  780. * special non-standard request.
  781. */
  782. case USB_RECIP_DEVICE:
  783. debug("cdev->config->next_interface_id: %d intf: %d\n",
  784. cdev->config->next_interface_id, intf);
  785. if (cdev->config->next_interface_id == 1)
  786. f = cdev->config->interface[intf];
  787. break;
  788. }
  789. if (f && f->setup)
  790. value = f->setup(f, ctrl);
  791. else {
  792. c = cdev->config;
  793. if (c->setup)
  794. value = c->setup(c, ctrl);
  795. }
  796. goto done;
  797. }
  798. /* respond with data transfer before status phase? */
  799. if (value >= 0) {
  800. req->length = value;
  801. req->zero = value < w_length;
  802. value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
  803. if (value < 0) {
  804. debug("ep_queue --> %d\n", value);
  805. req->status = 0;
  806. composite_setup_complete(gadget->ep0, req);
  807. }
  808. }
  809. done:
  810. /* device either stalls (value < 0) or reports success */
  811. return value;
  812. }
  813. static void composite_disconnect(struct usb_gadget *gadget)
  814. {
  815. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  816. if (cdev->config)
  817. reset_config(cdev);
  818. if (composite->disconnect)
  819. composite->disconnect(cdev);
  820. }
  821. static void composite_unbind(struct usb_gadget *gadget)
  822. {
  823. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  824. struct usb_configuration *c;
  825. struct usb_function *f;
  826. /*
  827. * composite_disconnect() must already have been called
  828. * by the underlying peripheral controller driver!
  829. * so there's no i/o concurrency that could affect the
  830. * state protected by cdev->lock.
  831. */
  832. BUG_ON(cdev->config);
  833. while (!list_empty(&cdev->configs)) {
  834. c = list_first_entry(&cdev->configs,
  835. struct usb_configuration, list);
  836. while (!list_empty(&c->functions)) {
  837. f = list_first_entry(&c->functions,
  838. struct usb_function, list);
  839. list_del(&f->list);
  840. if (f->unbind) {
  841. debug("unbind function '%s'/%p\n",
  842. f->name, f);
  843. f->unbind(c, f);
  844. }
  845. }
  846. list_del(&c->list);
  847. if (c->unbind) {
  848. debug("unbind config '%s'/%p\n", c->label, c);
  849. c->unbind(c);
  850. }
  851. free(c);
  852. }
  853. if (composite->unbind)
  854. composite->unbind(cdev);
  855. if (cdev->req) {
  856. kfree(cdev->req->buf);
  857. usb_ep_free_request(gadget->ep0, cdev->req);
  858. }
  859. kfree(cdev);
  860. set_gadget_data(gadget, NULL);
  861. composite = NULL;
  862. }
  863. static int composite_bind(struct usb_gadget *gadget)
  864. {
  865. int status = -ENOMEM;
  866. struct usb_composite_dev *cdev;
  867. cdev = calloc(sizeof *cdev, 1);
  868. if (!cdev)
  869. return status;
  870. cdev->gadget = gadget;
  871. set_gadget_data(gadget, cdev);
  872. INIT_LIST_HEAD(&cdev->configs);
  873. /* preallocate control response and buffer */
  874. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  875. if (!cdev->req)
  876. goto fail;
  877. cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ);
  878. if (!cdev->req->buf)
  879. goto fail;
  880. cdev->req->complete = composite_setup_complete;
  881. gadget->ep0->driver_data = cdev;
  882. cdev->bufsiz = USB_BUFSIZ;
  883. cdev->driver = composite;
  884. usb_gadget_set_selfpowered(gadget);
  885. usb_ep_autoconfig_reset(cdev->gadget);
  886. status = composite->bind(cdev);
  887. if (status < 0)
  888. goto fail;
  889. memcpy(&cdev->desc, composite->dev,
  890. sizeof(struct usb_device_descriptor));
  891. cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  892. debug("%s: ready\n", composite->name);
  893. return 0;
  894. fail:
  895. composite_unbind(gadget);
  896. return status;
  897. }
  898. static void
  899. composite_suspend(struct usb_gadget *gadget)
  900. {
  901. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  902. struct usb_function *f;
  903. debug("%s: suspend\n", __func__);
  904. if (cdev->config) {
  905. list_for_each_entry(f, &cdev->config->functions, list) {
  906. if (f->suspend)
  907. f->suspend(f);
  908. }
  909. }
  910. if (composite->suspend)
  911. composite->suspend(cdev);
  912. cdev->suspended = 1;
  913. }
  914. static void
  915. composite_resume(struct usb_gadget *gadget)
  916. {
  917. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  918. struct usb_function *f;
  919. debug("%s: resume\n", __func__);
  920. if (composite->resume)
  921. composite->resume(cdev);
  922. if (cdev->config) {
  923. list_for_each_entry(f, &cdev->config->functions, list) {
  924. if (f->resume)
  925. f->resume(f);
  926. }
  927. }
  928. cdev->suspended = 0;
  929. }
  930. static struct usb_gadget_driver composite_driver = {
  931. .speed = USB_SPEED_HIGH,
  932. .bind = composite_bind,
  933. .unbind = composite_unbind,
  934. .setup = composite_setup,
  935. .reset = composite_disconnect,
  936. .disconnect = composite_disconnect,
  937. .suspend = composite_suspend,
  938. .resume = composite_resume,
  939. };
  940. /**
  941. * usb_composite_register() - register a composite driver
  942. * @driver: the driver to register
  943. * Context: single threaded during gadget setup
  944. *
  945. * This function is used to register drivers using the composite driver
  946. * framework. The return value is zero, or a negative errno value.
  947. * Those values normally come from the driver's @bind method, which does
  948. * all the work of setting up the driver to match the hardware.
  949. *
  950. * On successful return, the gadget is ready to respond to requests from
  951. * the host, unless one of its components invokes usb_gadget_disconnect()
  952. * while it was binding. That would usually be done in order to wait for
  953. * some userspace participation.
  954. */
  955. int usb_composite_register(struct usb_composite_driver *driver)
  956. {
  957. int res;
  958. if (!driver || !driver->dev || !driver->bind || composite)
  959. return -EINVAL;
  960. if (!driver->name)
  961. driver->name = "composite";
  962. composite = driver;
  963. res = usb_gadget_register_driver(&composite_driver);
  964. if (res != 0)
  965. composite = NULL;
  966. return res;
  967. }
  968. /**
  969. * usb_composite_unregister() - unregister a composite driver
  970. * @driver: the driver to unregister
  971. *
  972. * This function is used to unregister drivers using the composite
  973. * driver framework.
  974. */
  975. void usb_composite_unregister(struct usb_composite_driver *driver)
  976. {
  977. if (composite != driver)
  978. return;
  979. usb_gadget_unregister_driver(&composite_driver);
  980. composite = NULL;
  981. }