composite.c 29 KB

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