composite.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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 = USB_BUFSIZ;
  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 || w_index >= 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 || w_index >= 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)
  818. f = cdev->config->interface[intf];
  819. break;
  820. case USB_RECIP_ENDPOINT:
  821. endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
  822. list_for_each_entry(f, &cdev->config->functions, list) {
  823. if (test_bit(endp, f->endpoints))
  824. break;
  825. }
  826. if (&f->list == &cdev->config->functions)
  827. f = NULL;
  828. break;
  829. }
  830. if (f && f->setup)
  831. value = f->setup(f, ctrl);
  832. else {
  833. struct usb_configuration *c;
  834. c = cdev->config;
  835. if (c && c->setup)
  836. value = c->setup(c, ctrl);
  837. }
  838. goto done;
  839. }
  840. /* respond with data transfer before status phase? */
  841. if (value >= 0) {
  842. req->length = value;
  843. req->zero = value < w_length;
  844. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  845. if (value < 0) {
  846. DBG(cdev, "ep_queue --> %d\n", value);
  847. req->status = 0;
  848. composite_setup_complete(gadget->ep0, req);
  849. }
  850. }
  851. done:
  852. /* device either stalls (value < 0) or reports success */
  853. return value;
  854. }
  855. static void composite_disconnect(struct usb_gadget *gadget)
  856. {
  857. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  858. unsigned long flags;
  859. /* REVISIT: should we have config and device level
  860. * disconnect callbacks?
  861. */
  862. spin_lock_irqsave(&cdev->lock, flags);
  863. if (cdev->config)
  864. reset_config(cdev);
  865. if (composite->disconnect)
  866. composite->disconnect(cdev);
  867. spin_unlock_irqrestore(&cdev->lock, flags);
  868. }
  869. /*-------------------------------------------------------------------------*/
  870. static ssize_t composite_show_suspended(struct device *dev,
  871. struct device_attribute *attr,
  872. char *buf)
  873. {
  874. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  875. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  876. return sprintf(buf, "%d\n", cdev->suspended);
  877. }
  878. static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
  879. static void
  880. composite_unbind(struct usb_gadget *gadget)
  881. {
  882. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  883. /* composite_disconnect() must already have been called
  884. * by the underlying peripheral controller driver!
  885. * so there's no i/o concurrency that could affect the
  886. * state protected by cdev->lock.
  887. */
  888. WARN_ON(cdev->config);
  889. while (!list_empty(&cdev->configs)) {
  890. struct usb_configuration *c;
  891. c = list_first_entry(&cdev->configs,
  892. struct usb_configuration, list);
  893. while (!list_empty(&c->functions)) {
  894. struct usb_function *f;
  895. f = list_first_entry(&c->functions,
  896. struct usb_function, list);
  897. list_del(&f->list);
  898. if (f->unbind) {
  899. DBG(cdev, "unbind function '%s'/%p\n",
  900. f->name, f);
  901. f->unbind(c, f);
  902. /* may free memory for "f" */
  903. }
  904. }
  905. list_del(&c->list);
  906. if (c->unbind) {
  907. DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
  908. c->unbind(c);
  909. /* may free memory for "c" */
  910. }
  911. }
  912. if (composite->unbind)
  913. composite->unbind(cdev);
  914. if (cdev->req) {
  915. kfree(cdev->req->buf);
  916. usb_ep_free_request(gadget->ep0, cdev->req);
  917. }
  918. device_remove_file(&gadget->dev, &dev_attr_suspended);
  919. kfree(cdev);
  920. set_gadget_data(gadget, NULL);
  921. composite = NULL;
  922. }
  923. static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
  924. {
  925. if (!*desc) {
  926. int ret = usb_string_id(cdev);
  927. if (unlikely(ret < 0))
  928. WARNING(cdev, "failed to override string ID\n");
  929. else
  930. *desc = ret;
  931. }
  932. return *desc;
  933. }
  934. static int composite_bind(struct usb_gadget *gadget)
  935. {
  936. struct usb_composite_dev *cdev;
  937. int status = -ENOMEM;
  938. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  939. if (!cdev)
  940. return status;
  941. spin_lock_init(&cdev->lock);
  942. cdev->gadget = gadget;
  943. set_gadget_data(gadget, cdev);
  944. INIT_LIST_HEAD(&cdev->configs);
  945. /* preallocate control response and buffer */
  946. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  947. if (!cdev->req)
  948. goto fail;
  949. cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
  950. if (!cdev->req->buf)
  951. goto fail;
  952. cdev->req->complete = composite_setup_complete;
  953. gadget->ep0->driver_data = cdev;
  954. cdev->bufsiz = USB_BUFSIZ;
  955. cdev->driver = composite;
  956. /*
  957. * As per USB compliance update, a device that is actively drawing
  958. * more than 100mA from USB must report itself as bus-powered in
  959. * the GetStatus(DEVICE) call.
  960. */
  961. if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
  962. usb_gadget_set_selfpowered(gadget);
  963. /* interface and string IDs start at zero via kzalloc.
  964. * we force endpoints to start unassigned; few controller
  965. * drivers will zero ep->driver_data.
  966. */
  967. usb_ep_autoconfig_reset(cdev->gadget);
  968. /* composite gadget needs to assign strings for whole device (like
  969. * serial number), register function drivers, potentially update
  970. * power state and consumption, etc
  971. */
  972. status = composite_gadget_bind(cdev);
  973. if (status < 0)
  974. goto fail;
  975. cdev->desc = *composite->dev;
  976. cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  977. /* standardized runtime overrides for device ID data */
  978. if (idVendor)
  979. cdev->desc.idVendor = cpu_to_le16(idVendor);
  980. if (idProduct)
  981. cdev->desc.idProduct = cpu_to_le16(idProduct);
  982. if (bcdDevice)
  983. cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
  984. /* string overrides */
  985. if (iManufacturer || !cdev->desc.iManufacturer) {
  986. if (!iManufacturer && !composite->iManufacturer &&
  987. !*composite_manufacturer)
  988. snprintf(composite_manufacturer,
  989. sizeof composite_manufacturer,
  990. "%s %s with %s",
  991. init_utsname()->sysname,
  992. init_utsname()->release,
  993. gadget->name);
  994. cdev->manufacturer_override =
  995. override_id(cdev, &cdev->desc.iManufacturer);
  996. }
  997. if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
  998. cdev->product_override =
  999. override_id(cdev, &cdev->desc.iProduct);
  1000. if (iSerialNumber)
  1001. cdev->serial_override =
  1002. override_id(cdev, &cdev->desc.iSerialNumber);
  1003. /* has userspace failed to provide a serial number? */
  1004. if (composite->needs_serial && !cdev->desc.iSerialNumber)
  1005. WARNING(cdev, "userspace failed to provide iSerialNumber\n");
  1006. /* finish up */
  1007. status = device_create_file(&gadget->dev, &dev_attr_suspended);
  1008. if (status)
  1009. goto fail;
  1010. INFO(cdev, "%s ready\n", composite->name);
  1011. return 0;
  1012. fail:
  1013. composite_unbind(gadget);
  1014. return status;
  1015. }
  1016. /*-------------------------------------------------------------------------*/
  1017. static void
  1018. composite_suspend(struct usb_gadget *gadget)
  1019. {
  1020. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1021. struct usb_function *f;
  1022. /* REVISIT: should we have config level
  1023. * suspend/resume callbacks?
  1024. */
  1025. DBG(cdev, "suspend\n");
  1026. if (cdev->config) {
  1027. list_for_each_entry(f, &cdev->config->functions, list) {
  1028. if (f->suspend)
  1029. f->suspend(f);
  1030. }
  1031. }
  1032. if (composite->suspend)
  1033. composite->suspend(cdev);
  1034. cdev->suspended = 1;
  1035. usb_gadget_vbus_draw(gadget, 2);
  1036. }
  1037. static void
  1038. composite_resume(struct usb_gadget *gadget)
  1039. {
  1040. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1041. struct usb_function *f;
  1042. u8 maxpower;
  1043. /* REVISIT: should we have config level
  1044. * suspend/resume callbacks?
  1045. */
  1046. DBG(cdev, "resume\n");
  1047. if (composite->resume)
  1048. composite->resume(cdev);
  1049. if (cdev->config) {
  1050. list_for_each_entry(f, &cdev->config->functions, list) {
  1051. if (f->resume)
  1052. f->resume(f);
  1053. }
  1054. maxpower = cdev->config->bMaxPower;
  1055. usb_gadget_vbus_draw(gadget, maxpower ?
  1056. (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
  1057. }
  1058. cdev->suspended = 0;
  1059. }
  1060. /*-------------------------------------------------------------------------*/
  1061. static struct usb_gadget_driver composite_driver = {
  1062. .speed = USB_SPEED_HIGH,
  1063. .unbind = composite_unbind,
  1064. .setup = composite_setup,
  1065. .disconnect = composite_disconnect,
  1066. .suspend = composite_suspend,
  1067. .resume = composite_resume,
  1068. .driver = {
  1069. .owner = THIS_MODULE,
  1070. },
  1071. };
  1072. /**
  1073. * usb_composite_probe() - register a composite driver
  1074. * @driver: the driver to register
  1075. * @bind: the callback used to allocate resources that are shared across the
  1076. * whole device, such as string IDs, and add its configurations using
  1077. * @usb_add_config(). This may fail by returning a negative errno
  1078. * value; it should return zero on successful initialization.
  1079. * Context: single threaded during gadget setup
  1080. *
  1081. * This function is used to register drivers using the composite driver
  1082. * framework. The return value is zero, or a negative errno value.
  1083. * Those values normally come from the driver's @bind method, which does
  1084. * all the work of setting up the driver to match the hardware.
  1085. *
  1086. * On successful return, the gadget is ready to respond to requests from
  1087. * the host, unless one of its components invokes usb_gadget_disconnect()
  1088. * while it was binding. That would usually be done in order to wait for
  1089. * some userspace participation.
  1090. */
  1091. extern int usb_composite_probe(struct usb_composite_driver *driver,
  1092. int (*bind)(struct usb_composite_dev *cdev))
  1093. {
  1094. if (!driver || !driver->dev || !bind || composite)
  1095. return -EINVAL;
  1096. if (!driver->iProduct)
  1097. driver->iProduct = driver->name;
  1098. if (!driver->name)
  1099. driver->name = "composite";
  1100. composite_driver.function = (char *) driver->name;
  1101. composite_driver.driver.name = driver->name;
  1102. composite = driver;
  1103. composite_gadget_bind = bind;
  1104. return usb_gadget_probe_driver(&composite_driver, composite_bind);
  1105. }
  1106. /**
  1107. * usb_composite_unregister() - unregister a composite driver
  1108. * @driver: the driver to unregister
  1109. *
  1110. * This function is used to unregister drivers using the composite
  1111. * driver framework.
  1112. */
  1113. void usb_composite_unregister(struct usb_composite_driver *driver)
  1114. {
  1115. if (composite != driver)
  1116. return;
  1117. usb_gadget_unregister_driver(&composite_driver);
  1118. }