composite.c 29 KB

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