composite.c 27 KB

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