composite.c 35 KB

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