composite.c 29 KB

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