composite.c 31 KB

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