composite.c 33 KB

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