composite.c 29 KB

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