composite.c 27 KB

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