composite.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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,
  581. * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
  582. * that for example different functions don't wrongly assign different
  583. * meanings to the same identifier.
  584. */
  585. int usb_string_id(struct usb_composite_dev *cdev)
  586. {
  587. if (cdev->next_string_id < 254) {
  588. /* string id 0 is reserved by USB spec for list of
  589. * supported languages */
  590. /* 255 reserved as well? -- mina86 */
  591. cdev->next_string_id++;
  592. return cdev->next_string_id;
  593. }
  594. return -ENODEV;
  595. }
  596. /**
  597. * usb_string_ids() - allocate unused string IDs in batch
  598. * @cdev: the device whose string descriptor IDs are being allocated
  599. * @str: an array of usb_string objects to assign numbers to
  600. * Context: single threaded during gadget setup
  601. *
  602. * @usb_string_ids() is called from bind() callbacks to allocate
  603. * string IDs. Drivers for functions, configurations, or gadgets will
  604. * then copy IDs from the string table to the appropriate descriptors
  605. * and string table for other languages.
  606. *
  607. * All string identifier should be allocated using this,
  608. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  609. * example different functions don't wrongly assign different meanings
  610. * to the same identifier.
  611. */
  612. int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
  613. {
  614. int next = cdev->next_string_id;
  615. for (; str->s; ++str) {
  616. if (unlikely(next >= 254))
  617. return -ENODEV;
  618. str->id = ++next;
  619. }
  620. cdev->next_string_id = next;
  621. return 0;
  622. }
  623. /**
  624. * usb_string_ids_n() - allocate unused string IDs in batch
  625. * @cdev: the device whose string descriptor IDs are being allocated
  626. * @n: number of string IDs to allocate
  627. * Context: single threaded during gadget setup
  628. *
  629. * Returns the first requested ID. This ID and next @n-1 IDs are now
  630. * valid IDs. At least providind that @n is non zore because if it
  631. * is, returns last requested ID which is now very useful information.
  632. *
  633. * @usb_string_ids_n() is called from bind() callbacks to allocate
  634. * string IDs. Drivers for functions, configurations, or gadgets will
  635. * then store that ID in the appropriate descriptors and string table.
  636. *
  637. * All string identifier should be allocated using this,
  638. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  639. * example different functions don't wrongly assign different meanings
  640. * to the same identifier.
  641. */
  642. int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
  643. {
  644. unsigned next = c->next_string_id;
  645. if (unlikely(n > 254 || (unsigned)next + n > 254))
  646. return -ENODEV;
  647. c->next_string_id += n;
  648. return next + 1;
  649. }
  650. /*-------------------------------------------------------------------------*/
  651. static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
  652. {
  653. if (req->status || req->actual != req->length)
  654. DBG((struct usb_composite_dev *) ep->driver_data,
  655. "setup complete --> %d, %d/%d\n",
  656. req->status, req->actual, req->length);
  657. }
  658. /*
  659. * The setup() callback implements all the ep0 functionality that's
  660. * not handled lower down, in hardware or the hardware driver(like
  661. * device and endpoint feature flags, and their status). It's all
  662. * housekeeping for the gadget function we're implementing. Most of
  663. * the work is in config and function specific setup.
  664. */
  665. static int
  666. composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  667. {
  668. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  669. struct usb_request *req = cdev->req;
  670. int value = -EOPNOTSUPP;
  671. u16 w_index = le16_to_cpu(ctrl->wIndex);
  672. u8 intf = w_index & 0xFF;
  673. u16 w_value = le16_to_cpu(ctrl->wValue);
  674. u16 w_length = le16_to_cpu(ctrl->wLength);
  675. struct usb_function *f = NULL;
  676. u8 endp;
  677. /* partial re-init of the response message; the function or the
  678. * gadget might need to intercept e.g. a control-OUT completion
  679. * when we delegate to it.
  680. */
  681. req->zero = 0;
  682. req->complete = composite_setup_complete;
  683. req->length = USB_BUFSIZ;
  684. gadget->ep0->driver_data = cdev;
  685. switch (ctrl->bRequest) {
  686. /* we handle all standard USB descriptors */
  687. case USB_REQ_GET_DESCRIPTOR:
  688. if (ctrl->bRequestType != USB_DIR_IN)
  689. goto unknown;
  690. switch (w_value >> 8) {
  691. case USB_DT_DEVICE:
  692. cdev->desc.bNumConfigurations =
  693. count_configs(cdev, USB_DT_DEVICE);
  694. value = min(w_length, (u16) sizeof cdev->desc);
  695. memcpy(req->buf, &cdev->desc, value);
  696. break;
  697. case USB_DT_DEVICE_QUALIFIER:
  698. if (!gadget_is_dualspeed(gadget))
  699. break;
  700. device_qual(cdev);
  701. value = min_t(int, w_length,
  702. sizeof(struct usb_qualifier_descriptor));
  703. break;
  704. case USB_DT_OTHER_SPEED_CONFIG:
  705. if (!gadget_is_dualspeed(gadget))
  706. break;
  707. /* FALLTHROUGH */
  708. case USB_DT_CONFIG:
  709. value = config_desc(cdev, w_value);
  710. if (value >= 0)
  711. value = min(w_length, (u16) value);
  712. break;
  713. case USB_DT_STRING:
  714. value = get_string(cdev, req->buf,
  715. w_index, w_value & 0xff);
  716. if (value >= 0)
  717. value = min(w_length, (u16) value);
  718. break;
  719. }
  720. break;
  721. /* any number of configs can work */
  722. case USB_REQ_SET_CONFIGURATION:
  723. if (ctrl->bRequestType != 0)
  724. goto unknown;
  725. if (gadget_is_otg(gadget)) {
  726. if (gadget->a_hnp_support)
  727. DBG(cdev, "HNP available\n");
  728. else if (gadget->a_alt_hnp_support)
  729. DBG(cdev, "HNP on another port\n");
  730. else
  731. VDBG(cdev, "HNP inactive\n");
  732. }
  733. spin_lock(&cdev->lock);
  734. value = set_config(cdev, ctrl, w_value);
  735. spin_unlock(&cdev->lock);
  736. break;
  737. case USB_REQ_GET_CONFIGURATION:
  738. if (ctrl->bRequestType != USB_DIR_IN)
  739. goto unknown;
  740. if (cdev->config)
  741. *(u8 *)req->buf = cdev->config->bConfigurationValue;
  742. else
  743. *(u8 *)req->buf = 0;
  744. value = min(w_length, (u16) 1);
  745. break;
  746. /* function drivers must handle get/set altsetting; if there's
  747. * no get() method, we know only altsetting zero works.
  748. */
  749. case USB_REQ_SET_INTERFACE:
  750. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  751. goto unknown;
  752. if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
  753. break;
  754. f = cdev->config->interface[intf];
  755. if (!f)
  756. break;
  757. if (w_value && !f->set_alt)
  758. break;
  759. value = f->set_alt(f, w_index, w_value);
  760. break;
  761. case USB_REQ_GET_INTERFACE:
  762. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  763. goto unknown;
  764. if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
  765. break;
  766. f = cdev->config->interface[intf];
  767. if (!f)
  768. break;
  769. /* lots of interfaces only need altsetting zero... */
  770. value = f->get_alt ? f->get_alt(f, w_index) : 0;
  771. if (value < 0)
  772. break;
  773. *((u8 *)req->buf) = value;
  774. value = min(w_length, (u16) 1);
  775. break;
  776. default:
  777. unknown:
  778. VDBG(cdev,
  779. "non-core control req%02x.%02x v%04x i%04x l%d\n",
  780. ctrl->bRequestType, ctrl->bRequest,
  781. w_value, w_index, w_length);
  782. /* functions always handle their interfaces and endpoints...
  783. * punt other recipients (other, WUSB, ...) to the current
  784. * configuration code.
  785. *
  786. * REVISIT it could make sense to let the composite device
  787. * take such requests too, if that's ever needed: to work
  788. * in config 0, etc.
  789. */
  790. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  791. case USB_RECIP_INTERFACE:
  792. f = cdev->config->interface[intf];
  793. break;
  794. case USB_RECIP_ENDPOINT:
  795. endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
  796. list_for_each_entry(f, &cdev->config->functions, list) {
  797. if (test_bit(endp, f->endpoints))
  798. break;
  799. }
  800. if (&f->list == &cdev->config->functions)
  801. f = NULL;
  802. break;
  803. }
  804. if (f && f->setup)
  805. value = f->setup(f, ctrl);
  806. else {
  807. struct usb_configuration *c;
  808. c = cdev->config;
  809. if (c && c->setup)
  810. value = c->setup(c, ctrl);
  811. }
  812. goto done;
  813. }
  814. /* respond with data transfer before status phase? */
  815. if (value >= 0) {
  816. req->length = value;
  817. req->zero = value < w_length;
  818. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  819. if (value < 0) {
  820. DBG(cdev, "ep_queue --> %d\n", value);
  821. req->status = 0;
  822. composite_setup_complete(gadget->ep0, req);
  823. }
  824. }
  825. done:
  826. /* device either stalls (value < 0) or reports success */
  827. return value;
  828. }
  829. static void composite_disconnect(struct usb_gadget *gadget)
  830. {
  831. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  832. unsigned long flags;
  833. /* REVISIT: should we have config and device level
  834. * disconnect callbacks?
  835. */
  836. spin_lock_irqsave(&cdev->lock, flags);
  837. if (cdev->config)
  838. reset_config(cdev);
  839. spin_unlock_irqrestore(&cdev->lock, flags);
  840. }
  841. /*-------------------------------------------------------------------------*/
  842. static ssize_t composite_show_suspended(struct device *dev,
  843. struct device_attribute *attr,
  844. char *buf)
  845. {
  846. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  847. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  848. return sprintf(buf, "%d\n", cdev->suspended);
  849. }
  850. static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
  851. static void
  852. composite_unbind(struct usb_gadget *gadget)
  853. {
  854. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  855. /* composite_disconnect() must already have been called
  856. * by the underlying peripheral controller driver!
  857. * so there's no i/o concurrency that could affect the
  858. * state protected by cdev->lock.
  859. */
  860. WARN_ON(cdev->config);
  861. while (!list_empty(&cdev->configs)) {
  862. struct usb_configuration *c;
  863. c = list_first_entry(&cdev->configs,
  864. struct usb_configuration, list);
  865. while (!list_empty(&c->functions)) {
  866. struct usb_function *f;
  867. f = list_first_entry(&c->functions,
  868. struct usb_function, list);
  869. list_del(&f->list);
  870. if (f->unbind) {
  871. DBG(cdev, "unbind function '%s'/%p\n",
  872. f->name, f);
  873. f->unbind(c, f);
  874. /* may free memory for "f" */
  875. }
  876. }
  877. list_del(&c->list);
  878. if (c->unbind) {
  879. DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
  880. c->unbind(c);
  881. /* may free memory for "c" */
  882. }
  883. }
  884. if (composite->unbind)
  885. composite->unbind(cdev);
  886. if (cdev->req) {
  887. kfree(cdev->req->buf);
  888. usb_ep_free_request(gadget->ep0, cdev->req);
  889. }
  890. kfree(cdev);
  891. set_gadget_data(gadget, NULL);
  892. device_remove_file(&gadget->dev, &dev_attr_suspended);
  893. composite = NULL;
  894. }
  895. static void
  896. string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
  897. {
  898. struct usb_string *str = tab->strings;
  899. for (str = tab->strings; str->s; str++) {
  900. if (str->id == id) {
  901. str->s = s;
  902. return;
  903. }
  904. }
  905. }
  906. static void
  907. string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
  908. {
  909. while (*tab) {
  910. string_override_one(*tab, id, s);
  911. tab++;
  912. }
  913. }
  914. static int composite_bind(struct usb_gadget *gadget)
  915. {
  916. struct usb_composite_dev *cdev;
  917. int status = -ENOMEM;
  918. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  919. if (!cdev)
  920. return status;
  921. spin_lock_init(&cdev->lock);
  922. cdev->gadget = gadget;
  923. set_gadget_data(gadget, cdev);
  924. INIT_LIST_HEAD(&cdev->configs);
  925. /* preallocate control response and buffer */
  926. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  927. if (!cdev->req)
  928. goto fail;
  929. cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
  930. if (!cdev->req->buf)
  931. goto fail;
  932. cdev->req->complete = composite_setup_complete;
  933. gadget->ep0->driver_data = cdev;
  934. cdev->bufsiz = USB_BUFSIZ;
  935. cdev->driver = composite;
  936. usb_gadget_set_selfpowered(gadget);
  937. /* interface and string IDs start at zero via kzalloc.
  938. * we force endpoints to start unassigned; few controller
  939. * drivers will zero ep->driver_data.
  940. */
  941. usb_ep_autoconfig_reset(cdev->gadget);
  942. /* standardized runtime overrides for device ID data */
  943. if (idVendor)
  944. cdev->desc.idVendor = cpu_to_le16(idVendor);
  945. if (idProduct)
  946. cdev->desc.idProduct = cpu_to_le16(idProduct);
  947. if (bcdDevice)
  948. cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
  949. /* composite gadget needs to assign strings for whole device (like
  950. * serial number), register function drivers, potentially update
  951. * power state and consumption, etc
  952. */
  953. status = composite->bind(cdev);
  954. if (status < 0)
  955. goto fail;
  956. cdev->desc = *composite->dev;
  957. cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  958. /* strings can't be assigned before bind() allocates the
  959. * releavnt identifiers
  960. */
  961. if (cdev->desc.iManufacturer && iManufacturer)
  962. string_override(composite->strings,
  963. cdev->desc.iManufacturer, iManufacturer);
  964. if (cdev->desc.iProduct && iProduct)
  965. string_override(composite->strings,
  966. cdev->desc.iProduct, iProduct);
  967. if (cdev->desc.iSerialNumber && iSerialNumber)
  968. string_override(composite->strings,
  969. cdev->desc.iSerialNumber, iSerialNumber);
  970. status = device_create_file(&gadget->dev, &dev_attr_suspended);
  971. if (status)
  972. goto fail;
  973. INFO(cdev, "%s ready\n", composite->name);
  974. return 0;
  975. fail:
  976. composite_unbind(gadget);
  977. return status;
  978. }
  979. /*-------------------------------------------------------------------------*/
  980. static void
  981. composite_suspend(struct usb_gadget *gadget)
  982. {
  983. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  984. struct usb_function *f;
  985. /* REVISIT: should we have config level
  986. * suspend/resume callbacks?
  987. */
  988. DBG(cdev, "suspend\n");
  989. if (cdev->config) {
  990. list_for_each_entry(f, &cdev->config->functions, list) {
  991. if (f->suspend)
  992. f->suspend(f);
  993. }
  994. }
  995. if (composite->suspend)
  996. composite->suspend(cdev);
  997. cdev->suspended = 1;
  998. }
  999. static void
  1000. composite_resume(struct usb_gadget *gadget)
  1001. {
  1002. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1003. struct usb_function *f;
  1004. /* REVISIT: should we have config level
  1005. * suspend/resume callbacks?
  1006. */
  1007. DBG(cdev, "resume\n");
  1008. if (composite->resume)
  1009. composite->resume(cdev);
  1010. if (cdev->config) {
  1011. list_for_each_entry(f, &cdev->config->functions, list) {
  1012. if (f->resume)
  1013. f->resume(f);
  1014. }
  1015. }
  1016. cdev->suspended = 0;
  1017. }
  1018. /*-------------------------------------------------------------------------*/
  1019. static struct usb_gadget_driver composite_driver = {
  1020. .speed = USB_SPEED_HIGH,
  1021. .bind = composite_bind,
  1022. .unbind = composite_unbind,
  1023. .setup = composite_setup,
  1024. .disconnect = composite_disconnect,
  1025. .suspend = composite_suspend,
  1026. .resume = composite_resume,
  1027. .driver = {
  1028. .owner = THIS_MODULE,
  1029. },
  1030. };
  1031. /**
  1032. * usb_composite_register() - register a composite driver
  1033. * @driver: the driver to register
  1034. * Context: single threaded during gadget setup
  1035. *
  1036. * This function is used to register drivers using the composite driver
  1037. * framework. The return value is zero, or a negative errno value.
  1038. * Those values normally come from the driver's @bind method, which does
  1039. * all the work of setting up the driver to match the hardware.
  1040. *
  1041. * On successful return, the gadget is ready to respond to requests from
  1042. * the host, unless one of its components invokes usb_gadget_disconnect()
  1043. * while it was binding. That would usually be done in order to wait for
  1044. * some userspace participation.
  1045. */
  1046. int usb_composite_register(struct usb_composite_driver *driver)
  1047. {
  1048. if (!driver || !driver->dev || !driver->bind || composite)
  1049. return -EINVAL;
  1050. if (!driver->name)
  1051. driver->name = "composite";
  1052. composite_driver.function = (char *) driver->name;
  1053. composite_driver.driver.name = driver->name;
  1054. composite = driver;
  1055. return usb_gadget_register_driver(&composite_driver);
  1056. }
  1057. /**
  1058. * usb_composite_unregister() - unregister a composite driver
  1059. * @driver: the driver to unregister
  1060. *
  1061. * This function is used to unregister drivers using the composite
  1062. * driver framework.
  1063. */
  1064. void usb_composite_unregister(struct usb_composite_driver *driver)
  1065. {
  1066. if (composite != driver)
  1067. return;
  1068. usb_gadget_unregister_driver(&composite_driver);
  1069. }