composite.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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 1024
  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 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 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 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 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 ssize_t composite_show_suspended(struct device *dev,
  786. struct device_attribute *attr,
  787. char *buf)
  788. {
  789. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  790. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  791. return sprintf(buf, "%d\n", cdev->suspended);
  792. }
  793. static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
  794. static void
  795. composite_unbind(struct usb_gadget *gadget)
  796. {
  797. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  798. /* composite_disconnect() must already have been called
  799. * by the underlying peripheral controller driver!
  800. * so there's no i/o concurrency that could affect the
  801. * state protected by cdev->lock.
  802. */
  803. WARN_ON(cdev->config);
  804. while (!list_empty(&cdev->configs)) {
  805. struct usb_configuration *c;
  806. c = list_first_entry(&cdev->configs,
  807. struct usb_configuration, list);
  808. while (!list_empty(&c->functions)) {
  809. struct usb_function *f;
  810. f = list_first_entry(&c->functions,
  811. struct usb_function, list);
  812. list_del(&f->list);
  813. if (f->unbind) {
  814. DBG(cdev, "unbind function '%s'/%p\n",
  815. f->name, f);
  816. f->unbind(c, f);
  817. /* may free memory for "f" */
  818. }
  819. }
  820. list_del(&c->list);
  821. if (c->unbind) {
  822. DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
  823. c->unbind(c);
  824. /* may free memory for "c" */
  825. }
  826. }
  827. if (composite->unbind)
  828. composite->unbind(cdev);
  829. if (cdev->req) {
  830. kfree(cdev->req->buf);
  831. usb_ep_free_request(gadget->ep0, cdev->req);
  832. }
  833. kfree(cdev);
  834. set_gadget_data(gadget, NULL);
  835. device_remove_file(&gadget->dev, &dev_attr_suspended);
  836. composite = NULL;
  837. }
  838. static void
  839. string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
  840. {
  841. struct usb_string *str = tab->strings;
  842. for (str = tab->strings; str->s; str++) {
  843. if (str->id == id) {
  844. str->s = s;
  845. return;
  846. }
  847. }
  848. }
  849. static void
  850. string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
  851. {
  852. while (*tab) {
  853. string_override_one(*tab, id, s);
  854. tab++;
  855. }
  856. }
  857. static int composite_bind(struct usb_gadget *gadget)
  858. {
  859. struct usb_composite_dev *cdev;
  860. int status = -ENOMEM;
  861. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  862. if (!cdev)
  863. return status;
  864. spin_lock_init(&cdev->lock);
  865. cdev->gadget = gadget;
  866. set_gadget_data(gadget, cdev);
  867. INIT_LIST_HEAD(&cdev->configs);
  868. /* preallocate control response and buffer */
  869. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  870. if (!cdev->req)
  871. goto fail;
  872. cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
  873. if (!cdev->req->buf)
  874. goto fail;
  875. cdev->req->complete = composite_setup_complete;
  876. gadget->ep0->driver_data = cdev;
  877. cdev->bufsiz = USB_BUFSIZ;
  878. cdev->driver = composite;
  879. usb_gadget_set_selfpowered(gadget);
  880. /* interface and string IDs start at zero via kzalloc.
  881. * we force endpoints to start unassigned; few controller
  882. * drivers will zero ep->driver_data.
  883. */
  884. usb_ep_autoconfig_reset(cdev->gadget);
  885. /* standardized runtime overrides for device ID data */
  886. if (idVendor)
  887. cdev->desc.idVendor = cpu_to_le16(idVendor);
  888. if (idProduct)
  889. cdev->desc.idProduct = cpu_to_le16(idProduct);
  890. if (bcdDevice)
  891. cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
  892. /* composite gadget needs to assign strings for whole device (like
  893. * serial number), register function drivers, potentially update
  894. * power state and consumption, etc
  895. */
  896. status = composite->bind(cdev);
  897. if (status < 0)
  898. goto fail;
  899. cdev->desc = *composite->dev;
  900. cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  901. /* strings can't be assigned before bind() allocates the
  902. * releavnt identifiers
  903. */
  904. if (cdev->desc.iManufacturer && iManufacturer)
  905. string_override(composite->strings,
  906. cdev->desc.iManufacturer, iManufacturer);
  907. if (cdev->desc.iProduct && iProduct)
  908. string_override(composite->strings,
  909. cdev->desc.iProduct, iProduct);
  910. if (cdev->desc.iSerialNumber && iSerialNumber)
  911. string_override(composite->strings,
  912. cdev->desc.iSerialNumber, iSerialNumber);
  913. status = device_create_file(&gadget->dev, &dev_attr_suspended);
  914. if (status)
  915. goto fail;
  916. INFO(cdev, "%s ready\n", composite->name);
  917. return 0;
  918. fail:
  919. composite_unbind(gadget);
  920. return status;
  921. }
  922. /*-------------------------------------------------------------------------*/
  923. static void
  924. composite_suspend(struct usb_gadget *gadget)
  925. {
  926. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  927. struct usb_function *f;
  928. /* REVISIT: should we have config level
  929. * suspend/resume callbacks?
  930. */
  931. DBG(cdev, "suspend\n");
  932. if (cdev->config) {
  933. list_for_each_entry(f, &cdev->config->functions, list) {
  934. if (f->suspend)
  935. f->suspend(f);
  936. }
  937. }
  938. if (composite->suspend)
  939. composite->suspend(cdev);
  940. cdev->suspended = 1;
  941. }
  942. static void
  943. composite_resume(struct usb_gadget *gadget)
  944. {
  945. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  946. struct usb_function *f;
  947. /* REVISIT: should we have config level
  948. * suspend/resume callbacks?
  949. */
  950. DBG(cdev, "resume\n");
  951. if (composite->resume)
  952. composite->resume(cdev);
  953. if (cdev->config) {
  954. list_for_each_entry(f, &cdev->config->functions, list) {
  955. if (f->resume)
  956. f->resume(f);
  957. }
  958. }
  959. cdev->suspended = 0;
  960. }
  961. /*-------------------------------------------------------------------------*/
  962. static struct usb_gadget_driver composite_driver = {
  963. .speed = USB_SPEED_HIGH,
  964. .bind = composite_bind,
  965. .unbind = composite_unbind,
  966. .setup = composite_setup,
  967. .disconnect = composite_disconnect,
  968. .suspend = composite_suspend,
  969. .resume = composite_resume,
  970. .driver = {
  971. .owner = THIS_MODULE,
  972. },
  973. };
  974. /**
  975. * usb_composite_register() - register a composite driver
  976. * @driver: the driver to register
  977. * Context: single threaded during gadget setup
  978. *
  979. * This function is used to register drivers using the composite driver
  980. * framework. The return value is zero, or a negative errno value.
  981. * Those values normally come from the driver's @bind method, which does
  982. * all the work of setting up the driver to match the hardware.
  983. *
  984. * On successful return, the gadget is ready to respond to requests from
  985. * the host, unless one of its components invokes usb_gadget_disconnect()
  986. * while it was binding. That would usually be done in order to wait for
  987. * some userspace participation.
  988. */
  989. int usb_composite_register(struct usb_composite_driver *driver)
  990. {
  991. if (!driver || !driver->dev || !driver->bind || composite)
  992. return -EINVAL;
  993. if (!driver->name)
  994. driver->name = "composite";
  995. composite_driver.function = (char *) driver->name;
  996. composite_driver.driver.name = driver->name;
  997. composite = driver;
  998. return usb_gadget_register_driver(&composite_driver);
  999. }
  1000. /**
  1001. * usb_composite_unregister() - unregister a composite driver
  1002. * @driver: the driver to unregister
  1003. *
  1004. * This function is used to unregister drivers using the composite
  1005. * driver framework.
  1006. */
  1007. void usb_composite_unregister(struct usb_composite_driver *driver)
  1008. {
  1009. if (composite != driver)
  1010. return;
  1011. usb_gadget_unregister_driver(&composite_driver);
  1012. }