composite.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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. bitmap_zero(f->endpoints, 32);
  326. }
  327. cdev->config = NULL;
  328. }
  329. static int set_config(struct usb_composite_dev *cdev,
  330. const struct usb_ctrlrequest *ctrl, unsigned number)
  331. {
  332. struct usb_gadget *gadget = cdev->gadget;
  333. struct usb_configuration *c = NULL;
  334. int result = -EINVAL;
  335. unsigned power = gadget_is_otg(gadget) ? 8 : 100;
  336. int tmp;
  337. if (cdev->config)
  338. reset_config(cdev);
  339. if (number) {
  340. list_for_each_entry(c, &cdev->configs, list) {
  341. if (c->bConfigurationValue == number) {
  342. result = 0;
  343. break;
  344. }
  345. }
  346. if (result < 0)
  347. goto done;
  348. } else
  349. result = 0;
  350. INFO(cdev, "%s speed config #%d: %s\n",
  351. ({ char *speed;
  352. switch (gadget->speed) {
  353. case USB_SPEED_LOW: speed = "low"; break;
  354. case USB_SPEED_FULL: speed = "full"; break;
  355. case USB_SPEED_HIGH: speed = "high"; break;
  356. default: speed = "?"; break;
  357. } ; speed; }), number, c ? c->label : "unconfigured");
  358. if (!c)
  359. goto done;
  360. cdev->config = c;
  361. /* Initialize all interfaces by setting them to altsetting zero. */
  362. for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
  363. struct usb_function *f = c->interface[tmp];
  364. struct usb_descriptor_header **descriptors;
  365. if (!f)
  366. break;
  367. /*
  368. * Record which endpoints are used by the function. This is used
  369. * to dispatch control requests targeted at that endpoint to the
  370. * function's setup callback instead of the current
  371. * configuration's setup callback.
  372. */
  373. if (gadget->speed == USB_SPEED_HIGH)
  374. descriptors = f->hs_descriptors;
  375. else
  376. descriptors = f->descriptors;
  377. for (; *descriptors; ++descriptors) {
  378. struct usb_endpoint_descriptor *ep;
  379. int addr;
  380. if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
  381. continue;
  382. ep = (struct usb_endpoint_descriptor *)*descriptors;
  383. addr = ((ep->bEndpointAddress & 0x80) >> 3)
  384. | (ep->bEndpointAddress & 0x0f);
  385. set_bit(addr, f->endpoints);
  386. }
  387. result = f->set_alt(f, tmp, 0);
  388. if (result < 0) {
  389. DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
  390. tmp, f->name, f, result);
  391. reset_config(cdev);
  392. goto done;
  393. }
  394. }
  395. /* when we return, be sure our power usage is valid */
  396. power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
  397. done:
  398. usb_gadget_vbus_draw(gadget, power);
  399. return result;
  400. }
  401. /**
  402. * usb_add_config() - add a configuration to a device.
  403. * @cdev: wraps the USB gadget
  404. * @config: the configuration, with bConfigurationValue assigned
  405. * Context: single threaded during gadget setup
  406. *
  407. * One of the main tasks of a composite driver's bind() routine is to
  408. * add each of the configurations it supports, using this routine.
  409. *
  410. * This function returns the value of the configuration's bind(), which
  411. * is zero for success else a negative errno value. Binding configurations
  412. * assigns global resources including string IDs, and per-configuration
  413. * resources such as interface IDs and endpoints.
  414. */
  415. int __init usb_add_config(struct usb_composite_dev *cdev,
  416. struct usb_configuration *config)
  417. {
  418. int status = -EINVAL;
  419. struct usb_configuration *c;
  420. DBG(cdev, "adding config #%u '%s'/%p\n",
  421. config->bConfigurationValue,
  422. config->label, config);
  423. if (!config->bConfigurationValue || !config->bind)
  424. goto done;
  425. /* Prevent duplicate configuration identifiers */
  426. list_for_each_entry(c, &cdev->configs, list) {
  427. if (c->bConfigurationValue == config->bConfigurationValue) {
  428. status = -EBUSY;
  429. goto done;
  430. }
  431. }
  432. config->cdev = cdev;
  433. list_add_tail(&config->list, &cdev->configs);
  434. INIT_LIST_HEAD(&config->functions);
  435. config->next_interface_id = 0;
  436. status = config->bind(config);
  437. if (status < 0) {
  438. list_del(&config->list);
  439. config->cdev = NULL;
  440. } else {
  441. unsigned i;
  442. DBG(cdev, "cfg %d/%p speeds:%s%s\n",
  443. config->bConfigurationValue, config,
  444. config->highspeed ? " high" : "",
  445. config->fullspeed
  446. ? (gadget_is_dualspeed(cdev->gadget)
  447. ? " full"
  448. : " full/low")
  449. : "");
  450. for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
  451. struct usb_function *f = config->interface[i];
  452. if (!f)
  453. continue;
  454. DBG(cdev, " interface %d = %s/%p\n",
  455. i, f->name, f);
  456. }
  457. }
  458. /* set_alt(), or next config->bind(), sets up
  459. * ep->driver_data as needed.
  460. */
  461. usb_ep_autoconfig_reset(cdev->gadget);
  462. done:
  463. if (status)
  464. DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
  465. config->bConfigurationValue, status);
  466. return status;
  467. }
  468. /*-------------------------------------------------------------------------*/
  469. /* We support strings in multiple languages ... string descriptor zero
  470. * says which languages are supported. The typical case will be that
  471. * only one language (probably English) is used, with I18N handled on
  472. * the host side.
  473. */
  474. static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
  475. {
  476. const struct usb_gadget_strings *s;
  477. u16 language;
  478. __le16 *tmp;
  479. while (*sp) {
  480. s = *sp;
  481. language = cpu_to_le16(s->language);
  482. for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
  483. if (*tmp == language)
  484. goto repeat;
  485. }
  486. *tmp++ = language;
  487. repeat:
  488. sp++;
  489. }
  490. }
  491. static int lookup_string(
  492. struct usb_gadget_strings **sp,
  493. void *buf,
  494. u16 language,
  495. int id
  496. )
  497. {
  498. struct usb_gadget_strings *s;
  499. int value;
  500. while (*sp) {
  501. s = *sp++;
  502. if (s->language != language)
  503. continue;
  504. value = usb_gadget_get_string(s, id, buf);
  505. if (value > 0)
  506. return value;
  507. }
  508. return -EINVAL;
  509. }
  510. static int get_string(struct usb_composite_dev *cdev,
  511. void *buf, u16 language, int id)
  512. {
  513. struct usb_configuration *c;
  514. struct usb_function *f;
  515. int len;
  516. /* Yes, not only is USB's I18N support probably more than most
  517. * folk will ever care about ... also, it's all supported here.
  518. * (Except for UTF8 support for Unicode's "Astral Planes".)
  519. */
  520. /* 0 == report all available language codes */
  521. if (id == 0) {
  522. struct usb_string_descriptor *s = buf;
  523. struct usb_gadget_strings **sp;
  524. memset(s, 0, 256);
  525. s->bDescriptorType = USB_DT_STRING;
  526. sp = composite->strings;
  527. if (sp)
  528. collect_langs(sp, s->wData);
  529. list_for_each_entry(c, &cdev->configs, list) {
  530. sp = c->strings;
  531. if (sp)
  532. collect_langs(sp, s->wData);
  533. list_for_each_entry(f, &c->functions, list) {
  534. sp = f->strings;
  535. if (sp)
  536. collect_langs(sp, s->wData);
  537. }
  538. }
  539. for (len = 0; len <= 126 && s->wData[len]; len++)
  540. continue;
  541. if (!len)
  542. return -EINVAL;
  543. s->bLength = 2 * (len + 1);
  544. return s->bLength;
  545. }
  546. /* Otherwise, look up and return a specified string. String IDs
  547. * are device-scoped, so we look up each string table we're told
  548. * about. These lookups are infrequent; simpler-is-better here.
  549. */
  550. if (composite->strings) {
  551. len = lookup_string(composite->strings, buf, language, id);
  552. if (len > 0)
  553. return len;
  554. }
  555. list_for_each_entry(c, &cdev->configs, list) {
  556. if (c->strings) {
  557. len = lookup_string(c->strings, buf, language, id);
  558. if (len > 0)
  559. return len;
  560. }
  561. list_for_each_entry(f, &c->functions, list) {
  562. if (!f->strings)
  563. continue;
  564. len = lookup_string(f->strings, buf, language, id);
  565. if (len > 0)
  566. return len;
  567. }
  568. }
  569. return -EINVAL;
  570. }
  571. /**
  572. * usb_string_id() - allocate an unused string ID
  573. * @cdev: the device whose string descriptor IDs are being allocated
  574. * Context: single threaded during gadget setup
  575. *
  576. * @usb_string_id() is called from bind() callbacks to allocate
  577. * string IDs. Drivers for functions, configurations, or gadgets will
  578. * then store that ID in the appropriate descriptors and string table.
  579. *
  580. * All string identifier should be allocated using this routine, to
  581. * ensure that for example different functions don't wrongly assign
  582. * different meanings to the same identifier.
  583. */
  584. int __init usb_string_id(struct usb_composite_dev *cdev)
  585. {
  586. if (cdev->next_string_id < 254) {
  587. /* string id 0 is reserved */
  588. cdev->next_string_id++;
  589. return cdev->next_string_id;
  590. }
  591. return -ENODEV;
  592. }
  593. /*-------------------------------------------------------------------------*/
  594. static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
  595. {
  596. if (req->status || req->actual != req->length)
  597. DBG((struct usb_composite_dev *) ep->driver_data,
  598. "setup complete --> %d, %d/%d\n",
  599. req->status, req->actual, req->length);
  600. }
  601. /*
  602. * The setup() callback implements all the ep0 functionality that's
  603. * not handled lower down, in hardware or the hardware driver(like
  604. * device and endpoint feature flags, and their status). It's all
  605. * housekeeping for the gadget function we're implementing. Most of
  606. * the work is in config and function specific setup.
  607. */
  608. static int
  609. composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  610. {
  611. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  612. struct usb_request *req = cdev->req;
  613. int value = -EOPNOTSUPP;
  614. u16 w_index = le16_to_cpu(ctrl->wIndex);
  615. u8 intf = w_index & 0xFF;
  616. u16 w_value = le16_to_cpu(ctrl->wValue);
  617. u16 w_length = le16_to_cpu(ctrl->wLength);
  618. struct usb_function *f = NULL;
  619. u8 endp;
  620. /* partial re-init of the response message; the function or the
  621. * gadget might need to intercept e.g. a control-OUT completion
  622. * when we delegate to it.
  623. */
  624. req->zero = 0;
  625. req->complete = composite_setup_complete;
  626. req->length = USB_BUFSIZ;
  627. gadget->ep0->driver_data = cdev;
  628. switch (ctrl->bRequest) {
  629. /* we handle all standard USB descriptors */
  630. case USB_REQ_GET_DESCRIPTOR:
  631. if (ctrl->bRequestType != USB_DIR_IN)
  632. goto unknown;
  633. switch (w_value >> 8) {
  634. case USB_DT_DEVICE:
  635. cdev->desc.bNumConfigurations =
  636. count_configs(cdev, USB_DT_DEVICE);
  637. value = min(w_length, (u16) sizeof cdev->desc);
  638. memcpy(req->buf, &cdev->desc, value);
  639. break;
  640. case USB_DT_DEVICE_QUALIFIER:
  641. if (!gadget_is_dualspeed(gadget))
  642. break;
  643. device_qual(cdev);
  644. value = min_t(int, w_length,
  645. sizeof(struct usb_qualifier_descriptor));
  646. break;
  647. case USB_DT_OTHER_SPEED_CONFIG:
  648. if (!gadget_is_dualspeed(gadget))
  649. break;
  650. /* FALLTHROUGH */
  651. case USB_DT_CONFIG:
  652. value = config_desc(cdev, w_value);
  653. if (value >= 0)
  654. value = min(w_length, (u16) value);
  655. break;
  656. case USB_DT_STRING:
  657. value = get_string(cdev, req->buf,
  658. w_index, w_value & 0xff);
  659. if (value >= 0)
  660. value = min(w_length, (u16) value);
  661. break;
  662. }
  663. break;
  664. /* any number of configs can work */
  665. case USB_REQ_SET_CONFIGURATION:
  666. if (ctrl->bRequestType != 0)
  667. goto unknown;
  668. if (gadget_is_otg(gadget)) {
  669. if (gadget->a_hnp_support)
  670. DBG(cdev, "HNP available\n");
  671. else if (gadget->a_alt_hnp_support)
  672. DBG(cdev, "HNP on another port\n");
  673. else
  674. VDBG(cdev, "HNP inactive\n");
  675. }
  676. spin_lock(&cdev->lock);
  677. value = set_config(cdev, ctrl, w_value);
  678. spin_unlock(&cdev->lock);
  679. break;
  680. case USB_REQ_GET_CONFIGURATION:
  681. if (ctrl->bRequestType != USB_DIR_IN)
  682. goto unknown;
  683. if (cdev->config)
  684. *(u8 *)req->buf = cdev->config->bConfigurationValue;
  685. else
  686. *(u8 *)req->buf = 0;
  687. value = min(w_length, (u16) 1);
  688. break;
  689. /* function drivers must handle get/set altsetting; if there's
  690. * no get() method, we know only altsetting zero works.
  691. */
  692. case USB_REQ_SET_INTERFACE:
  693. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  694. goto unknown;
  695. if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
  696. break;
  697. f = cdev->config->interface[intf];
  698. if (!f)
  699. break;
  700. if (w_value && !f->set_alt)
  701. break;
  702. value = f->set_alt(f, w_index, w_value);
  703. break;
  704. case USB_REQ_GET_INTERFACE:
  705. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  706. goto unknown;
  707. if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
  708. break;
  709. f = cdev->config->interface[intf];
  710. if (!f)
  711. break;
  712. /* lots of interfaces only need altsetting zero... */
  713. value = f->get_alt ? f->get_alt(f, w_index) : 0;
  714. if (value < 0)
  715. break;
  716. *((u8 *)req->buf) = value;
  717. value = min(w_length, (u16) 1);
  718. break;
  719. default:
  720. unknown:
  721. VDBG(cdev,
  722. "non-core control req%02x.%02x v%04x i%04x l%d\n",
  723. ctrl->bRequestType, ctrl->bRequest,
  724. w_value, w_index, w_length);
  725. /* functions always handle their interfaces and endpoints...
  726. * punt other recipients (other, WUSB, ...) to the current
  727. * configuration code.
  728. *
  729. * REVISIT it could make sense to let the composite device
  730. * take such requests too, if that's ever needed: to work
  731. * in config 0, etc.
  732. */
  733. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  734. case USB_RECIP_INTERFACE:
  735. f = cdev->config->interface[intf];
  736. break;
  737. case USB_RECIP_ENDPOINT:
  738. endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
  739. list_for_each_entry(f, &cdev->config->functions, list) {
  740. if (test_bit(endp, f->endpoints))
  741. break;
  742. }
  743. if (&f->list == &cdev->config->functions)
  744. f = NULL;
  745. break;
  746. }
  747. if (f && f->setup)
  748. value = f->setup(f, ctrl);
  749. else {
  750. struct usb_configuration *c;
  751. c = cdev->config;
  752. if (c && c->setup)
  753. value = c->setup(c, ctrl);
  754. }
  755. goto done;
  756. }
  757. /* respond with data transfer before status phase? */
  758. if (value >= 0) {
  759. req->length = value;
  760. req->zero = value < w_length;
  761. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  762. if (value < 0) {
  763. DBG(cdev, "ep_queue --> %d\n", value);
  764. req->status = 0;
  765. composite_setup_complete(gadget->ep0, req);
  766. }
  767. }
  768. done:
  769. /* device either stalls (value < 0) or reports success */
  770. return value;
  771. }
  772. static void composite_disconnect(struct usb_gadget *gadget)
  773. {
  774. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  775. unsigned long flags;
  776. /* REVISIT: should we have config and device level
  777. * disconnect callbacks?
  778. */
  779. spin_lock_irqsave(&cdev->lock, flags);
  780. if (cdev->config)
  781. reset_config(cdev);
  782. spin_unlock_irqrestore(&cdev->lock, flags);
  783. }
  784. /*-------------------------------------------------------------------------*/
  785. static void /* __init_or_exit */
  786. composite_unbind(struct usb_gadget *gadget)
  787. {
  788. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  789. /* composite_disconnect() must already have been called
  790. * by the underlying peripheral controller driver!
  791. * so there's no i/o concurrency that could affect the
  792. * state protected by cdev->lock.
  793. */
  794. WARN_ON(cdev->config);
  795. while (!list_empty(&cdev->configs)) {
  796. struct usb_configuration *c;
  797. c = list_first_entry(&cdev->configs,
  798. struct usb_configuration, list);
  799. while (!list_empty(&c->functions)) {
  800. struct usb_function *f;
  801. f = list_first_entry(&c->functions,
  802. struct usb_function, list);
  803. list_del(&f->list);
  804. if (f->unbind) {
  805. DBG(cdev, "unbind function '%s'/%p\n",
  806. f->name, f);
  807. f->unbind(c, f);
  808. /* may free memory for "f" */
  809. }
  810. }
  811. list_del(&c->list);
  812. if (c->unbind) {
  813. DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
  814. c->unbind(c);
  815. /* may free memory for "c" */
  816. }
  817. }
  818. if (composite->unbind)
  819. composite->unbind(cdev);
  820. if (cdev->req) {
  821. kfree(cdev->req->buf);
  822. usb_ep_free_request(gadget->ep0, cdev->req);
  823. }
  824. kfree(cdev);
  825. set_gadget_data(gadget, NULL);
  826. composite = NULL;
  827. }
  828. static void __init
  829. string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
  830. {
  831. struct usb_string *str = tab->strings;
  832. for (str = tab->strings; str->s; str++) {
  833. if (str->id == id) {
  834. str->s = s;
  835. return;
  836. }
  837. }
  838. }
  839. static void __init
  840. string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
  841. {
  842. while (*tab) {
  843. string_override_one(*tab, id, s);
  844. tab++;
  845. }
  846. }
  847. static int __init composite_bind(struct usb_gadget *gadget)
  848. {
  849. struct usb_composite_dev *cdev;
  850. int status = -ENOMEM;
  851. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  852. if (!cdev)
  853. return status;
  854. spin_lock_init(&cdev->lock);
  855. cdev->gadget = gadget;
  856. set_gadget_data(gadget, cdev);
  857. INIT_LIST_HEAD(&cdev->configs);
  858. /* preallocate control response and buffer */
  859. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  860. if (!cdev->req)
  861. goto fail;
  862. cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
  863. if (!cdev->req->buf)
  864. goto fail;
  865. cdev->req->complete = composite_setup_complete;
  866. gadget->ep0->driver_data = cdev;
  867. cdev->bufsiz = USB_BUFSIZ;
  868. cdev->driver = composite;
  869. usb_gadget_set_selfpowered(gadget);
  870. /* interface and string IDs start at zero via kzalloc.
  871. * we force endpoints to start unassigned; few controller
  872. * drivers will zero ep->driver_data.
  873. */
  874. usb_ep_autoconfig_reset(cdev->gadget);
  875. /* composite gadget needs to assign strings for whole device (like
  876. * serial number), register function drivers, potentially update
  877. * power state and consumption, etc
  878. */
  879. status = composite->bind(cdev);
  880. if (status < 0)
  881. goto fail;
  882. cdev->desc = *composite->dev;
  883. cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  884. /* standardized runtime overrides for device ID data */
  885. if (idVendor)
  886. cdev->desc.idVendor = cpu_to_le16(idVendor);
  887. if (idProduct)
  888. cdev->desc.idProduct = cpu_to_le16(idProduct);
  889. if (bcdDevice)
  890. cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
  891. /* strings can't be assigned before bind() allocates the
  892. * releavnt identifiers
  893. */
  894. if (cdev->desc.iManufacturer && iManufacturer)
  895. string_override(composite->strings,
  896. cdev->desc.iManufacturer, iManufacturer);
  897. if (cdev->desc.iProduct && iProduct)
  898. string_override(composite->strings,
  899. cdev->desc.iProduct, iProduct);
  900. if (cdev->desc.iSerialNumber && iSerialNumber)
  901. string_override(composite->strings,
  902. cdev->desc.iSerialNumber, iSerialNumber);
  903. INFO(cdev, "%s ready\n", composite->name);
  904. return 0;
  905. fail:
  906. composite_unbind(gadget);
  907. return status;
  908. }
  909. /*-------------------------------------------------------------------------*/
  910. static void
  911. composite_suspend(struct usb_gadget *gadget)
  912. {
  913. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  914. struct usb_function *f;
  915. /* REVISIT: should we have config level
  916. * suspend/resume callbacks?
  917. */
  918. DBG(cdev, "suspend\n");
  919. if (cdev->config) {
  920. list_for_each_entry(f, &cdev->config->functions, list) {
  921. if (f->suspend)
  922. f->suspend(f);
  923. }
  924. }
  925. if (composite->suspend)
  926. composite->suspend(cdev);
  927. }
  928. static void
  929. composite_resume(struct usb_gadget *gadget)
  930. {
  931. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  932. struct usb_function *f;
  933. /* REVISIT: should we have config level
  934. * suspend/resume callbacks?
  935. */
  936. DBG(cdev, "resume\n");
  937. if (composite->resume)
  938. composite->resume(cdev);
  939. if (cdev->config) {
  940. list_for_each_entry(f, &cdev->config->functions, list) {
  941. if (f->resume)
  942. f->resume(f);
  943. }
  944. }
  945. }
  946. /*-------------------------------------------------------------------------*/
  947. static struct usb_gadget_driver composite_driver = {
  948. .speed = USB_SPEED_HIGH,
  949. .bind = composite_bind,
  950. /* .unbind = __exit_p(composite_unbind), */
  951. .unbind = composite_unbind,
  952. .setup = composite_setup,
  953. .disconnect = composite_disconnect,
  954. .suspend = composite_suspend,
  955. .resume = composite_resume,
  956. .driver = {
  957. .owner = THIS_MODULE,
  958. },
  959. };
  960. /**
  961. * usb_composite_register() - register a composite driver
  962. * @driver: the driver to register
  963. * Context: single threaded during gadget setup
  964. *
  965. * This function is used to register drivers using the composite driver
  966. * framework. The return value is zero, or a negative errno value.
  967. * Those values normally come from the driver's @bind method, which does
  968. * all the work of setting up the driver to match the hardware.
  969. *
  970. * On successful return, the gadget is ready to respond to requests from
  971. * the host, unless one of its components invokes usb_gadget_disconnect()
  972. * while it was binding. That would usually be done in order to wait for
  973. * some userspace participation.
  974. */
  975. int __init usb_composite_register(struct usb_composite_driver *driver)
  976. {
  977. if (!driver || !driver->dev || !driver->bind || composite)
  978. return -EINVAL;
  979. if (!driver->name)
  980. driver->name = "composite";
  981. composite_driver.function = (char *) driver->name;
  982. composite_driver.driver.name = driver->name;
  983. composite = driver;
  984. return usb_gadget_register_driver(&composite_driver);
  985. }
  986. /**
  987. * usb_composite_unregister() - unregister a composite driver
  988. * @driver: the driver to unregister
  989. *
  990. * This function is used to unregister drivers using the composite
  991. * driver framework.
  992. */
  993. void /* __exit */ usb_composite_unregister(struct usb_composite_driver *driver)
  994. {
  995. if (composite != driver)
  996. return;
  997. usb_gadget_unregister_driver(&composite_driver);
  998. }