composite.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  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. /* #define VERBOSE_DEBUG */
  12. #include <linux/kallsyms.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/device.h>
  16. #include <linux/utsname.h>
  17. #include <linux/usb/composite.h>
  18. #include <asm/unaligned.h>
  19. /*
  20. * The code in this file is utility code, used to build a gadget driver
  21. * from one or more "function" drivers, one or more "configuration"
  22. * objects, and a "usb_composite_driver" by gluing them together along
  23. * with the relevant device-wide data.
  24. */
  25. /* big enough to hold our biggest descriptor */
  26. #define USB_BUFSIZ 1024
  27. static struct usb_composite_driver *composite;
  28. static int (*composite_gadget_bind)(struct usb_composite_dev *cdev);
  29. /* Some systems will need runtime overrides for the product identifiers
  30. * published in the device descriptor, either numbers or strings or both.
  31. * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
  32. */
  33. static ushort idVendor;
  34. module_param(idVendor, ushort, 0);
  35. MODULE_PARM_DESC(idVendor, "USB Vendor ID");
  36. static ushort idProduct;
  37. module_param(idProduct, ushort, 0);
  38. MODULE_PARM_DESC(idProduct, "USB Product ID");
  39. static ushort bcdDevice;
  40. module_param(bcdDevice, ushort, 0);
  41. MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
  42. static char *iManufacturer;
  43. module_param(iManufacturer, charp, 0);
  44. MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
  45. static char *iProduct;
  46. module_param(iProduct, charp, 0);
  47. MODULE_PARM_DESC(iProduct, "USB Product string");
  48. static char *iSerialNumber;
  49. module_param(iSerialNumber, charp, 0);
  50. MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
  51. static char composite_manufacturer[50];
  52. /*-------------------------------------------------------------------------*/
  53. /**
  54. * next_ep_desc() - advance to the next EP descriptor
  55. * @t: currect pointer within descriptor array
  56. *
  57. * Return: next EP descriptor or NULL
  58. *
  59. * Iterate over @t until either EP descriptor found or
  60. * NULL (that indicates end of list) encountered
  61. */
  62. static struct usb_descriptor_header**
  63. next_ep_desc(struct usb_descriptor_header **t)
  64. {
  65. for (; *t; t++) {
  66. if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
  67. return t;
  68. }
  69. return NULL;
  70. }
  71. /*
  72. * for_each_ep_desc()- iterate over endpoint descriptors in the
  73. * descriptors list
  74. * @start: pointer within descriptor array.
  75. * @ep_desc: endpoint descriptor to use as the loop cursor
  76. */
  77. #define for_each_ep_desc(start, ep_desc) \
  78. for (ep_desc = next_ep_desc(start); \
  79. ep_desc; ep_desc = next_ep_desc(ep_desc+1))
  80. /**
  81. * config_ep_by_speed() - configures the given endpoint
  82. * according to gadget speed.
  83. * @g: pointer to the gadget
  84. * @f: usb function
  85. * @_ep: the endpoint to configure
  86. *
  87. * Return: error code, 0 on success
  88. *
  89. * This function chooses the right descriptors for a given
  90. * endpoint according to gadget speed and saves it in the
  91. * endpoint desc field. If the endpoint already has a descriptor
  92. * assigned to it - overwrites it with currently corresponding
  93. * descriptor. The endpoint maxpacket field is updated according
  94. * to the chosen descriptor.
  95. * Note: the supplied function should hold all the descriptors
  96. * for supported speeds
  97. */
  98. int config_ep_by_speed(struct usb_gadget *g,
  99. struct usb_function *f,
  100. struct usb_ep *_ep)
  101. {
  102. struct usb_endpoint_descriptor *chosen_desc = NULL;
  103. struct usb_descriptor_header **speed_desc = NULL;
  104. struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
  105. int want_comp_desc = 0;
  106. struct usb_descriptor_header **d_spd; /* cursor for speed desc */
  107. if (!g || !f || !_ep)
  108. return -EIO;
  109. /* select desired speed */
  110. switch (g->speed) {
  111. case USB_SPEED_SUPER:
  112. if (gadget_is_superspeed(g)) {
  113. speed_desc = f->ss_descriptors;
  114. want_comp_desc = 1;
  115. break;
  116. }
  117. /* else: Fall trough */
  118. case USB_SPEED_HIGH:
  119. if (gadget_is_dualspeed(g)) {
  120. speed_desc = f->hs_descriptors;
  121. break;
  122. }
  123. /* else: fall through */
  124. default:
  125. speed_desc = f->descriptors;
  126. }
  127. /* find descriptors */
  128. for_each_ep_desc(speed_desc, d_spd) {
  129. chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
  130. if (chosen_desc->bEndpointAddress == _ep->address)
  131. goto ep_found;
  132. }
  133. return -EIO;
  134. ep_found:
  135. /* commit results */
  136. _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
  137. _ep->desc = chosen_desc;
  138. _ep->comp_desc = NULL;
  139. _ep->maxburst = 0;
  140. _ep->mult = 0;
  141. if (!want_comp_desc)
  142. return 0;
  143. /*
  144. * Companion descriptor should follow EP descriptor
  145. * USB 3.0 spec, #9.6.7
  146. */
  147. comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
  148. if (!comp_desc ||
  149. (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
  150. return -EIO;
  151. _ep->comp_desc = comp_desc;
  152. if (g->speed == USB_SPEED_SUPER) {
  153. switch (usb_endpoint_type(_ep->desc)) {
  154. case USB_ENDPOINT_XFER_BULK:
  155. case USB_ENDPOINT_XFER_INT:
  156. _ep->maxburst = comp_desc->bMaxBurst;
  157. break;
  158. case USB_ENDPOINT_XFER_ISOC:
  159. /* mult: bits 1:0 of bmAttributes */
  160. _ep->mult = comp_desc->bmAttributes & 0x3;
  161. break;
  162. default:
  163. /* Do nothing for control endpoints */
  164. break;
  165. }
  166. }
  167. return 0;
  168. }
  169. /**
  170. * usb_add_function() - add a function to a configuration
  171. * @config: the configuration
  172. * @function: the function being added
  173. * Context: single threaded during gadget setup
  174. *
  175. * After initialization, each configuration must have one or more
  176. * functions added to it. Adding a function involves calling its @bind()
  177. * method to allocate resources such as interface and string identifiers
  178. * and endpoints.
  179. *
  180. * This function returns the value of the function's bind(), which is
  181. * zero for success else a negative errno value.
  182. */
  183. int usb_add_function(struct usb_configuration *config,
  184. struct usb_function *function)
  185. {
  186. int value = -EINVAL;
  187. DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
  188. function->name, function,
  189. config->label, config);
  190. if (!function->set_alt || !function->disable)
  191. goto done;
  192. function->config = config;
  193. list_add_tail(&function->list, &config->functions);
  194. /* REVISIT *require* function->bind? */
  195. if (function->bind) {
  196. value = function->bind(config, function);
  197. if (value < 0) {
  198. list_del(&function->list);
  199. function->config = NULL;
  200. }
  201. } else
  202. value = 0;
  203. /* We allow configurations that don't work at both speeds.
  204. * If we run into a lowspeed Linux system, treat it the same
  205. * as full speed ... it's the function drivers that will need
  206. * to avoid bulk and ISO transfers.
  207. */
  208. if (!config->fullspeed && function->descriptors)
  209. config->fullspeed = true;
  210. if (!config->highspeed && function->hs_descriptors)
  211. config->highspeed = true;
  212. if (!config->superspeed && function->ss_descriptors)
  213. config->superspeed = true;
  214. done:
  215. if (value)
  216. DBG(config->cdev, "adding '%s'/%p --> %d\n",
  217. function->name, function, value);
  218. return value;
  219. }
  220. /**
  221. * usb_function_deactivate - prevent function and gadget enumeration
  222. * @function: the function that isn't yet ready to respond
  223. *
  224. * Blocks response of the gadget driver to host enumeration by
  225. * preventing the data line pullup from being activated. This is
  226. * normally called during @bind() processing to change from the
  227. * initial "ready to respond" state, or when a required resource
  228. * becomes available.
  229. *
  230. * For example, drivers that serve as a passthrough to a userspace
  231. * daemon can block enumeration unless that daemon (such as an OBEX,
  232. * MTP, or print server) is ready to handle host requests.
  233. *
  234. * Not all systems support software control of their USB peripheral
  235. * data pullups.
  236. *
  237. * Returns zero on success, else negative errno.
  238. */
  239. int usb_function_deactivate(struct usb_function *function)
  240. {
  241. struct usb_composite_dev *cdev = function->config->cdev;
  242. unsigned long flags;
  243. int status = 0;
  244. spin_lock_irqsave(&cdev->lock, flags);
  245. if (cdev->deactivations == 0)
  246. status = usb_gadget_disconnect(cdev->gadget);
  247. if (status == 0)
  248. cdev->deactivations++;
  249. spin_unlock_irqrestore(&cdev->lock, flags);
  250. return status;
  251. }
  252. /**
  253. * usb_function_activate - allow function and gadget enumeration
  254. * @function: function on which usb_function_activate() was called
  255. *
  256. * Reverses effect of usb_function_deactivate(). If no more functions
  257. * are delaying their activation, the gadget driver will respond to
  258. * host enumeration procedures.
  259. *
  260. * Returns zero on success, else negative errno.
  261. */
  262. int usb_function_activate(struct usb_function *function)
  263. {
  264. struct usb_composite_dev *cdev = function->config->cdev;
  265. int status = 0;
  266. spin_lock(&cdev->lock);
  267. if (WARN_ON(cdev->deactivations == 0))
  268. status = -EINVAL;
  269. else {
  270. cdev->deactivations--;
  271. if (cdev->deactivations == 0)
  272. status = usb_gadget_connect(cdev->gadget);
  273. }
  274. spin_unlock(&cdev->lock);
  275. return status;
  276. }
  277. /**
  278. * usb_interface_id() - allocate an unused interface ID
  279. * @config: configuration associated with the interface
  280. * @function: function handling the interface
  281. * Context: single threaded during gadget setup
  282. *
  283. * usb_interface_id() is called from usb_function.bind() callbacks to
  284. * allocate new interface IDs. The function driver will then store that
  285. * ID in interface, association, CDC union, and other descriptors. It
  286. * will also handle any control requests targeted at that interface,
  287. * particularly changing its altsetting via set_alt(). There may
  288. * also be class-specific or vendor-specific requests to handle.
  289. *
  290. * All interface identifier should be allocated using this routine, to
  291. * ensure that for example different functions don't wrongly assign
  292. * different meanings to the same identifier. Note that since interface
  293. * identifiers are configuration-specific, functions used in more than
  294. * one configuration (or more than once in a given configuration) need
  295. * multiple versions of the relevant descriptors.
  296. *
  297. * Returns the interface ID which was allocated; or -ENODEV if no
  298. * more interface IDs can be allocated.
  299. */
  300. int usb_interface_id(struct usb_configuration *config,
  301. struct usb_function *function)
  302. {
  303. unsigned id = config->next_interface_id;
  304. if (id < MAX_CONFIG_INTERFACES) {
  305. config->interface[id] = function;
  306. config->next_interface_id = id + 1;
  307. return id;
  308. }
  309. return -ENODEV;
  310. }
  311. static int config_buf(struct usb_configuration *config,
  312. enum usb_device_speed speed, void *buf, u8 type)
  313. {
  314. struct usb_config_descriptor *c = buf;
  315. void *next = buf + USB_DT_CONFIG_SIZE;
  316. int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
  317. struct usb_function *f;
  318. int status;
  319. /* write the config descriptor */
  320. c = buf;
  321. c->bLength = USB_DT_CONFIG_SIZE;
  322. c->bDescriptorType = type;
  323. /* wTotalLength is written later */
  324. c->bNumInterfaces = config->next_interface_id;
  325. c->bConfigurationValue = config->bConfigurationValue;
  326. c->iConfiguration = config->iConfiguration;
  327. c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
  328. c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
  329. /* There may be e.g. OTG descriptors */
  330. if (config->descriptors) {
  331. status = usb_descriptor_fillbuf(next, len,
  332. config->descriptors);
  333. if (status < 0)
  334. return status;
  335. len -= status;
  336. next += status;
  337. }
  338. /* add each function's descriptors */
  339. list_for_each_entry(f, &config->functions, list) {
  340. struct usb_descriptor_header **descriptors;
  341. switch (speed) {
  342. case USB_SPEED_SUPER:
  343. descriptors = f->ss_descriptors;
  344. break;
  345. case USB_SPEED_HIGH:
  346. descriptors = f->hs_descriptors;
  347. break;
  348. default:
  349. descriptors = f->descriptors;
  350. }
  351. if (!descriptors)
  352. continue;
  353. status = usb_descriptor_fillbuf(next, len,
  354. (const struct usb_descriptor_header **) descriptors);
  355. if (status < 0)
  356. return status;
  357. len -= status;
  358. next += status;
  359. }
  360. len = next - buf;
  361. c->wTotalLength = cpu_to_le16(len);
  362. return len;
  363. }
  364. static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
  365. {
  366. struct usb_gadget *gadget = cdev->gadget;
  367. struct usb_configuration *c;
  368. u8 type = w_value >> 8;
  369. enum usb_device_speed speed = USB_SPEED_UNKNOWN;
  370. if (gadget->speed == USB_SPEED_SUPER)
  371. speed = gadget->speed;
  372. else if (gadget_is_dualspeed(gadget)) {
  373. int hs = 0;
  374. if (gadget->speed == USB_SPEED_HIGH)
  375. hs = 1;
  376. if (type == USB_DT_OTHER_SPEED_CONFIG)
  377. hs = !hs;
  378. if (hs)
  379. speed = USB_SPEED_HIGH;
  380. }
  381. /* This is a lookup by config *INDEX* */
  382. w_value &= 0xff;
  383. list_for_each_entry(c, &cdev->configs, list) {
  384. /* ignore configs that won't work at this speed */
  385. switch (speed) {
  386. case USB_SPEED_SUPER:
  387. if (!c->superspeed)
  388. continue;
  389. break;
  390. case USB_SPEED_HIGH:
  391. if (!c->highspeed)
  392. continue;
  393. break;
  394. default:
  395. if (!c->fullspeed)
  396. continue;
  397. }
  398. if (w_value == 0)
  399. return config_buf(c, speed, cdev->req->buf, type);
  400. w_value--;
  401. }
  402. return -EINVAL;
  403. }
  404. static int count_configs(struct usb_composite_dev *cdev, unsigned type)
  405. {
  406. struct usb_gadget *gadget = cdev->gadget;
  407. struct usb_configuration *c;
  408. unsigned count = 0;
  409. int hs = 0;
  410. int ss = 0;
  411. if (gadget_is_dualspeed(gadget)) {
  412. if (gadget->speed == USB_SPEED_HIGH)
  413. hs = 1;
  414. if (gadget->speed == USB_SPEED_SUPER)
  415. ss = 1;
  416. if (type == USB_DT_DEVICE_QUALIFIER)
  417. hs = !hs;
  418. }
  419. list_for_each_entry(c, &cdev->configs, list) {
  420. /* ignore configs that won't work at this speed */
  421. if (ss) {
  422. if (!c->superspeed)
  423. continue;
  424. } else if (hs) {
  425. if (!c->highspeed)
  426. continue;
  427. } else {
  428. if (!c->fullspeed)
  429. continue;
  430. }
  431. count++;
  432. }
  433. return count;
  434. }
  435. /**
  436. * bos_desc() - prepares the BOS descriptor.
  437. * @cdev: pointer to usb_composite device to generate the bos
  438. * descriptor for
  439. *
  440. * This function generates the BOS (Binary Device Object)
  441. * descriptor and its device capabilities descriptors. The BOS
  442. * descriptor should be supported by a SuperSpeed device.
  443. */
  444. static int bos_desc(struct usb_composite_dev *cdev)
  445. {
  446. struct usb_ext_cap_descriptor *usb_ext;
  447. struct usb_ss_cap_descriptor *ss_cap;
  448. struct usb_dcd_config_params dcd_config_params;
  449. struct usb_bos_descriptor *bos = cdev->req->buf;
  450. bos->bLength = USB_DT_BOS_SIZE;
  451. bos->bDescriptorType = USB_DT_BOS;
  452. bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
  453. bos->bNumDeviceCaps = 0;
  454. /*
  455. * A SuperSpeed device shall include the USB2.0 extension descriptor
  456. * and shall support LPM when operating in USB2.0 HS mode.
  457. */
  458. usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  459. bos->bNumDeviceCaps++;
  460. le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
  461. usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
  462. usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  463. usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
  464. usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT);
  465. /*
  466. * The Superspeed USB Capability descriptor shall be implemented by all
  467. * SuperSpeed devices.
  468. */
  469. ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  470. bos->bNumDeviceCaps++;
  471. le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
  472. ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
  473. ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  474. ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
  475. ss_cap->bmAttributes = 0; /* LTM is not supported yet */
  476. ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
  477. USB_FULL_SPEED_OPERATION |
  478. USB_HIGH_SPEED_OPERATION |
  479. USB_5GBPS_OPERATION);
  480. ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
  481. /* Get Controller configuration */
  482. if (cdev->gadget->ops->get_config_params)
  483. cdev->gadget->ops->get_config_params(&dcd_config_params);
  484. else {
  485. dcd_config_params.bU1devExitLat = USB_DEFULT_U1_DEV_EXIT_LAT;
  486. dcd_config_params.bU2DevExitLat =
  487. cpu_to_le16(USB_DEFULT_U2_DEV_EXIT_LAT);
  488. }
  489. ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
  490. ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
  491. return le16_to_cpu(bos->wTotalLength);
  492. }
  493. static void device_qual(struct usb_composite_dev *cdev)
  494. {
  495. struct usb_qualifier_descriptor *qual = cdev->req->buf;
  496. qual->bLength = sizeof(*qual);
  497. qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  498. /* POLICY: same bcdUSB and device type info at both speeds */
  499. qual->bcdUSB = cdev->desc.bcdUSB;
  500. qual->bDeviceClass = cdev->desc.bDeviceClass;
  501. qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
  502. qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
  503. /* ASSUME same EP0 fifo size at both speeds */
  504. qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
  505. qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
  506. qual->bRESERVED = 0;
  507. }
  508. /*-------------------------------------------------------------------------*/
  509. static void reset_config(struct usb_composite_dev *cdev)
  510. {
  511. struct usb_function *f;
  512. DBG(cdev, "reset config\n");
  513. list_for_each_entry(f, &cdev->config->functions, list) {
  514. if (f->disable)
  515. f->disable(f);
  516. bitmap_zero(f->endpoints, 32);
  517. }
  518. cdev->config = NULL;
  519. }
  520. static int set_config(struct usb_composite_dev *cdev,
  521. const struct usb_ctrlrequest *ctrl, unsigned number)
  522. {
  523. struct usb_gadget *gadget = cdev->gadget;
  524. struct usb_configuration *c = NULL;
  525. int result = -EINVAL;
  526. unsigned power = gadget_is_otg(gadget) ? 8 : 100;
  527. int tmp;
  528. if (number) {
  529. list_for_each_entry(c, &cdev->configs, list) {
  530. if (c->bConfigurationValue == number) {
  531. /*
  532. * We disable the FDs of the previous
  533. * configuration only if the new configuration
  534. * is a valid one
  535. */
  536. if (cdev->config)
  537. reset_config(cdev);
  538. result = 0;
  539. break;
  540. }
  541. }
  542. if (result < 0)
  543. goto done;
  544. } else { /* Zero configuration value - need to reset the config */
  545. if (cdev->config)
  546. reset_config(cdev);
  547. result = 0;
  548. }
  549. INFO(cdev, "%s speed config #%d: %s\n",
  550. ({ char *speed;
  551. switch (gadget->speed) {
  552. case USB_SPEED_LOW:
  553. speed = "low";
  554. break;
  555. case USB_SPEED_FULL:
  556. speed = "full";
  557. break;
  558. case USB_SPEED_HIGH:
  559. speed = "high";
  560. break;
  561. case USB_SPEED_SUPER:
  562. speed = "super";
  563. break;
  564. default:
  565. speed = "?";
  566. break;
  567. } ; speed; }), number, c ? c->label : "unconfigured");
  568. if (!c)
  569. goto done;
  570. cdev->config = c;
  571. /* Initialize all interfaces by setting them to altsetting zero. */
  572. for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
  573. struct usb_function *f = c->interface[tmp];
  574. struct usb_descriptor_header **descriptors;
  575. if (!f)
  576. break;
  577. /*
  578. * Record which endpoints are used by the function. This is used
  579. * to dispatch control requests targeted at that endpoint to the
  580. * function's setup callback instead of the current
  581. * configuration's setup callback.
  582. */
  583. switch (gadget->speed) {
  584. case USB_SPEED_SUPER:
  585. descriptors = f->ss_descriptors;
  586. break;
  587. case USB_SPEED_HIGH:
  588. descriptors = f->hs_descriptors;
  589. break;
  590. default:
  591. descriptors = f->descriptors;
  592. }
  593. for (; *descriptors; ++descriptors) {
  594. struct usb_endpoint_descriptor *ep;
  595. int addr;
  596. if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
  597. continue;
  598. ep = (struct usb_endpoint_descriptor *)*descriptors;
  599. addr = ((ep->bEndpointAddress & 0x80) >> 3)
  600. | (ep->bEndpointAddress & 0x0f);
  601. set_bit(addr, f->endpoints);
  602. }
  603. result = f->set_alt(f, tmp, 0);
  604. if (result < 0) {
  605. DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
  606. tmp, f->name, f, result);
  607. reset_config(cdev);
  608. goto done;
  609. }
  610. if (result == USB_GADGET_DELAYED_STATUS) {
  611. DBG(cdev,
  612. "%s: interface %d (%s) requested delayed status\n",
  613. __func__, tmp, f->name);
  614. cdev->delayed_status++;
  615. DBG(cdev, "delayed_status count %d\n",
  616. cdev->delayed_status);
  617. }
  618. }
  619. /* when we return, be sure our power usage is valid */
  620. power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
  621. done:
  622. usb_gadget_vbus_draw(gadget, power);
  623. if (result >= 0 && cdev->delayed_status)
  624. result = USB_GADGET_DELAYED_STATUS;
  625. return result;
  626. }
  627. /**
  628. * usb_add_config() - add a configuration to a device.
  629. * @cdev: wraps the USB gadget
  630. * @config: the configuration, with bConfigurationValue assigned
  631. * @bind: the configuration's bind function
  632. * Context: single threaded during gadget setup
  633. *
  634. * One of the main tasks of a composite @bind() routine is to
  635. * add each of the configurations it supports, using this routine.
  636. *
  637. * This function returns the value of the configuration's @bind(), which
  638. * is zero for success else a negative errno value. Binding configurations
  639. * assigns global resources including string IDs, and per-configuration
  640. * resources such as interface IDs and endpoints.
  641. */
  642. int usb_add_config(struct usb_composite_dev *cdev,
  643. struct usb_configuration *config,
  644. int (*bind)(struct usb_configuration *))
  645. {
  646. int status = -EINVAL;
  647. struct usb_configuration *c;
  648. DBG(cdev, "adding config #%u '%s'/%p\n",
  649. config->bConfigurationValue,
  650. config->label, config);
  651. if (!config->bConfigurationValue || !bind)
  652. goto done;
  653. /* Prevent duplicate configuration identifiers */
  654. list_for_each_entry(c, &cdev->configs, list) {
  655. if (c->bConfigurationValue == config->bConfigurationValue) {
  656. status = -EBUSY;
  657. goto done;
  658. }
  659. }
  660. config->cdev = cdev;
  661. list_add_tail(&config->list, &cdev->configs);
  662. INIT_LIST_HEAD(&config->functions);
  663. config->next_interface_id = 0;
  664. status = bind(config);
  665. if (status < 0) {
  666. list_del(&config->list);
  667. config->cdev = NULL;
  668. } else {
  669. unsigned i;
  670. DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
  671. config->bConfigurationValue, config,
  672. config->superspeed ? " super" : "",
  673. config->highspeed ? " high" : "",
  674. config->fullspeed
  675. ? (gadget_is_dualspeed(cdev->gadget)
  676. ? " full"
  677. : " full/low")
  678. : "");
  679. for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
  680. struct usb_function *f = config->interface[i];
  681. if (!f)
  682. continue;
  683. DBG(cdev, " interface %d = %s/%p\n",
  684. i, f->name, f);
  685. }
  686. }
  687. /* set_alt(), or next bind(), sets up
  688. * ep->driver_data as needed.
  689. */
  690. usb_ep_autoconfig_reset(cdev->gadget);
  691. done:
  692. if (status)
  693. DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
  694. config->bConfigurationValue, status);
  695. return status;
  696. }
  697. /*-------------------------------------------------------------------------*/
  698. /* We support strings in multiple languages ... string descriptor zero
  699. * says which languages are supported. The typical case will be that
  700. * only one language (probably English) is used, with I18N handled on
  701. * the host side.
  702. */
  703. static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
  704. {
  705. const struct usb_gadget_strings *s;
  706. u16 language;
  707. __le16 *tmp;
  708. while (*sp) {
  709. s = *sp;
  710. language = cpu_to_le16(s->language);
  711. for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
  712. if (*tmp == language)
  713. goto repeat;
  714. }
  715. *tmp++ = language;
  716. repeat:
  717. sp++;
  718. }
  719. }
  720. static int lookup_string(
  721. struct usb_gadget_strings **sp,
  722. void *buf,
  723. u16 language,
  724. int id
  725. )
  726. {
  727. struct usb_gadget_strings *s;
  728. int value;
  729. while (*sp) {
  730. s = *sp++;
  731. if (s->language != language)
  732. continue;
  733. value = usb_gadget_get_string(s, id, buf);
  734. if (value > 0)
  735. return value;
  736. }
  737. return -EINVAL;
  738. }
  739. static int get_string(struct usb_composite_dev *cdev,
  740. void *buf, u16 language, int id)
  741. {
  742. struct usb_configuration *c;
  743. struct usb_function *f;
  744. int len;
  745. const char *str;
  746. /* Yes, not only is USB's I18N support probably more than most
  747. * folk will ever care about ... also, it's all supported here.
  748. * (Except for UTF8 support for Unicode's "Astral Planes".)
  749. */
  750. /* 0 == report all available language codes */
  751. if (id == 0) {
  752. struct usb_string_descriptor *s = buf;
  753. struct usb_gadget_strings **sp;
  754. memset(s, 0, 256);
  755. s->bDescriptorType = USB_DT_STRING;
  756. sp = composite->strings;
  757. if (sp)
  758. collect_langs(sp, s->wData);
  759. list_for_each_entry(c, &cdev->configs, list) {
  760. sp = c->strings;
  761. if (sp)
  762. collect_langs(sp, s->wData);
  763. list_for_each_entry(f, &c->functions, list) {
  764. sp = f->strings;
  765. if (sp)
  766. collect_langs(sp, s->wData);
  767. }
  768. }
  769. for (len = 0; len <= 126 && s->wData[len]; len++)
  770. continue;
  771. if (!len)
  772. return -EINVAL;
  773. s->bLength = 2 * (len + 1);
  774. return s->bLength;
  775. }
  776. /* Otherwise, look up and return a specified string. First
  777. * check if the string has not been overridden.
  778. */
  779. if (cdev->manufacturer_override == id)
  780. str = iManufacturer ?: composite->iManufacturer ?:
  781. composite_manufacturer;
  782. else if (cdev->product_override == id)
  783. str = iProduct ?: composite->iProduct;
  784. else if (cdev->serial_override == id)
  785. str = iSerialNumber;
  786. else
  787. str = NULL;
  788. if (str) {
  789. struct usb_gadget_strings strings = {
  790. .language = language,
  791. .strings = &(struct usb_string) { 0xff, str }
  792. };
  793. return usb_gadget_get_string(&strings, 0xff, buf);
  794. }
  795. /* String IDs are device-scoped, so we look up each string
  796. * table we're told about. These lookups are infrequent;
  797. * simpler-is-better here.
  798. */
  799. if (composite->strings) {
  800. len = lookup_string(composite->strings, buf, language, id);
  801. if (len > 0)
  802. return len;
  803. }
  804. list_for_each_entry(c, &cdev->configs, list) {
  805. if (c->strings) {
  806. len = lookup_string(c->strings, buf, language, id);
  807. if (len > 0)
  808. return len;
  809. }
  810. list_for_each_entry(f, &c->functions, list) {
  811. if (!f->strings)
  812. continue;
  813. len = lookup_string(f->strings, buf, language, id);
  814. if (len > 0)
  815. return len;
  816. }
  817. }
  818. return -EINVAL;
  819. }
  820. /**
  821. * usb_string_id() - allocate an unused string ID
  822. * @cdev: the device whose string descriptor IDs are being allocated
  823. * Context: single threaded during gadget setup
  824. *
  825. * @usb_string_id() is called from bind() callbacks to allocate
  826. * string IDs. Drivers for functions, configurations, or gadgets will
  827. * then store that ID in the appropriate descriptors and string table.
  828. *
  829. * All string identifier should be allocated using this,
  830. * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
  831. * that for example different functions don't wrongly assign different
  832. * meanings to the same identifier.
  833. */
  834. int usb_string_id(struct usb_composite_dev *cdev)
  835. {
  836. if (cdev->next_string_id < 254) {
  837. /* string id 0 is reserved by USB spec for list of
  838. * supported languages */
  839. /* 255 reserved as well? -- mina86 */
  840. cdev->next_string_id++;
  841. return cdev->next_string_id;
  842. }
  843. return -ENODEV;
  844. }
  845. /**
  846. * usb_string_ids() - allocate unused string IDs in batch
  847. * @cdev: the device whose string descriptor IDs are being allocated
  848. * @str: an array of usb_string objects to assign numbers to
  849. * Context: single threaded during gadget setup
  850. *
  851. * @usb_string_ids() is called from bind() callbacks to allocate
  852. * string IDs. Drivers for functions, configurations, or gadgets will
  853. * then copy IDs from the string table to the appropriate descriptors
  854. * and string table for other languages.
  855. *
  856. * All string identifier should be allocated using this,
  857. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  858. * example different functions don't wrongly assign different meanings
  859. * to the same identifier.
  860. */
  861. int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
  862. {
  863. int next = cdev->next_string_id;
  864. for (; str->s; ++str) {
  865. if (unlikely(next >= 254))
  866. return -ENODEV;
  867. str->id = ++next;
  868. }
  869. cdev->next_string_id = next;
  870. return 0;
  871. }
  872. /**
  873. * usb_string_ids_n() - allocate unused string IDs in batch
  874. * @c: the device whose string descriptor IDs are being allocated
  875. * @n: number of string IDs to allocate
  876. * Context: single threaded during gadget setup
  877. *
  878. * Returns the first requested ID. This ID and next @n-1 IDs are now
  879. * valid IDs. At least provided that @n is non-zero because if it
  880. * is, returns last requested ID which is now very useful information.
  881. *
  882. * @usb_string_ids_n() is called from bind() callbacks to allocate
  883. * string IDs. Drivers for functions, configurations, or gadgets will
  884. * then store that ID in the appropriate descriptors and string table.
  885. *
  886. * All string identifier should be allocated using this,
  887. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  888. * example different functions don't wrongly assign different meanings
  889. * to the same identifier.
  890. */
  891. int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
  892. {
  893. unsigned next = c->next_string_id;
  894. if (unlikely(n > 254 || (unsigned)next + n > 254))
  895. return -ENODEV;
  896. c->next_string_id += n;
  897. return next + 1;
  898. }
  899. /*-------------------------------------------------------------------------*/
  900. static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
  901. {
  902. if (req->status || req->actual != req->length)
  903. DBG((struct usb_composite_dev *) ep->driver_data,
  904. "setup complete --> %d, %d/%d\n",
  905. req->status, req->actual, req->length);
  906. }
  907. /*
  908. * The setup() callback implements all the ep0 functionality that's
  909. * not handled lower down, in hardware or the hardware driver(like
  910. * device and endpoint feature flags, and their status). It's all
  911. * housekeeping for the gadget function we're implementing. Most of
  912. * the work is in config and function specific setup.
  913. */
  914. static int
  915. composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  916. {
  917. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  918. struct usb_request *req = cdev->req;
  919. int value = -EOPNOTSUPP;
  920. int status = 0;
  921. u16 w_index = le16_to_cpu(ctrl->wIndex);
  922. u8 intf = w_index & 0xFF;
  923. u16 w_value = le16_to_cpu(ctrl->wValue);
  924. u16 w_length = le16_to_cpu(ctrl->wLength);
  925. struct usb_function *f = NULL;
  926. u8 endp;
  927. /* partial re-init of the response message; the function or the
  928. * gadget might need to intercept e.g. a control-OUT completion
  929. * when we delegate to it.
  930. */
  931. req->zero = 0;
  932. req->complete = composite_setup_complete;
  933. req->length = 0;
  934. gadget->ep0->driver_data = cdev;
  935. switch (ctrl->bRequest) {
  936. /* we handle all standard USB descriptors */
  937. case USB_REQ_GET_DESCRIPTOR:
  938. if (ctrl->bRequestType != USB_DIR_IN)
  939. goto unknown;
  940. switch (w_value >> 8) {
  941. case USB_DT_DEVICE:
  942. cdev->desc.bNumConfigurations =
  943. count_configs(cdev, USB_DT_DEVICE);
  944. cdev->desc.bMaxPacketSize0 =
  945. cdev->gadget->ep0->maxpacket;
  946. if (gadget_is_superspeed(gadget)) {
  947. if (gadget->speed >= USB_SPEED_SUPER) {
  948. cdev->desc.bcdUSB = cpu_to_le16(0x0300);
  949. cdev->desc.bMaxPacketSize0 = 9;
  950. } else {
  951. cdev->desc.bcdUSB = cpu_to_le16(0x0210);
  952. }
  953. }
  954. value = min(w_length, (u16) sizeof cdev->desc);
  955. memcpy(req->buf, &cdev->desc, value);
  956. break;
  957. case USB_DT_DEVICE_QUALIFIER:
  958. if (!gadget_is_dualspeed(gadget) ||
  959. gadget->speed >= USB_SPEED_SUPER)
  960. break;
  961. device_qual(cdev);
  962. value = min_t(int, w_length,
  963. sizeof(struct usb_qualifier_descriptor));
  964. break;
  965. case USB_DT_OTHER_SPEED_CONFIG:
  966. if (!gadget_is_dualspeed(gadget) ||
  967. gadget->speed >= USB_SPEED_SUPER)
  968. break;
  969. /* FALLTHROUGH */
  970. case USB_DT_CONFIG:
  971. value = config_desc(cdev, w_value);
  972. if (value >= 0)
  973. value = min(w_length, (u16) value);
  974. break;
  975. case USB_DT_STRING:
  976. value = get_string(cdev, req->buf,
  977. w_index, w_value & 0xff);
  978. if (value >= 0)
  979. value = min(w_length, (u16) value);
  980. break;
  981. case USB_DT_BOS:
  982. if (gadget_is_superspeed(gadget)) {
  983. value = bos_desc(cdev);
  984. value = min(w_length, (u16) value);
  985. }
  986. break;
  987. }
  988. break;
  989. /* any number of configs can work */
  990. case USB_REQ_SET_CONFIGURATION:
  991. if (ctrl->bRequestType != 0)
  992. goto unknown;
  993. if (gadget_is_otg(gadget)) {
  994. if (gadget->a_hnp_support)
  995. DBG(cdev, "HNP available\n");
  996. else if (gadget->a_alt_hnp_support)
  997. DBG(cdev, "HNP on another port\n");
  998. else
  999. VDBG(cdev, "HNP inactive\n");
  1000. }
  1001. spin_lock(&cdev->lock);
  1002. value = set_config(cdev, ctrl, w_value);
  1003. spin_unlock(&cdev->lock);
  1004. break;
  1005. case USB_REQ_GET_CONFIGURATION:
  1006. if (ctrl->bRequestType != USB_DIR_IN)
  1007. goto unknown;
  1008. if (cdev->config)
  1009. *(u8 *)req->buf = cdev->config->bConfigurationValue;
  1010. else
  1011. *(u8 *)req->buf = 0;
  1012. value = min(w_length, (u16) 1);
  1013. break;
  1014. /* function drivers must handle get/set altsetting; if there's
  1015. * no get() method, we know only altsetting zero works.
  1016. */
  1017. case USB_REQ_SET_INTERFACE:
  1018. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  1019. goto unknown;
  1020. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1021. break;
  1022. f = cdev->config->interface[intf];
  1023. if (!f)
  1024. break;
  1025. if (w_value && !f->set_alt)
  1026. break;
  1027. value = f->set_alt(f, w_index, w_value);
  1028. if (value == USB_GADGET_DELAYED_STATUS) {
  1029. DBG(cdev,
  1030. "%s: interface %d (%s) requested delayed status\n",
  1031. __func__, intf, f->name);
  1032. cdev->delayed_status++;
  1033. DBG(cdev, "delayed_status count %d\n",
  1034. cdev->delayed_status);
  1035. }
  1036. break;
  1037. case USB_REQ_GET_INTERFACE:
  1038. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  1039. goto unknown;
  1040. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1041. break;
  1042. f = cdev->config->interface[intf];
  1043. if (!f)
  1044. break;
  1045. /* lots of interfaces only need altsetting zero... */
  1046. value = f->get_alt ? f->get_alt(f, w_index) : 0;
  1047. if (value < 0)
  1048. break;
  1049. *((u8 *)req->buf) = value;
  1050. value = min(w_length, (u16) 1);
  1051. break;
  1052. /*
  1053. * USB 3.0 additions:
  1054. * Function driver should handle get_status request. If such cb
  1055. * wasn't supplied we respond with default value = 0
  1056. * Note: function driver should supply such cb only for the first
  1057. * interface of the function
  1058. */
  1059. case USB_REQ_GET_STATUS:
  1060. if (!gadget_is_superspeed(gadget))
  1061. goto unknown;
  1062. if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
  1063. goto unknown;
  1064. value = 2; /* This is the length of the get_status reply */
  1065. put_unaligned_le16(0, req->buf);
  1066. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1067. break;
  1068. f = cdev->config->interface[intf];
  1069. if (!f)
  1070. break;
  1071. status = f->get_status ? f->get_status(f) : 0;
  1072. if (status < 0)
  1073. break;
  1074. put_unaligned_le16(status & 0x0000ffff, req->buf);
  1075. break;
  1076. /*
  1077. * Function drivers should handle SetFeature/ClearFeature
  1078. * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
  1079. * only for the first interface of the function
  1080. */
  1081. case USB_REQ_CLEAR_FEATURE:
  1082. case USB_REQ_SET_FEATURE:
  1083. if (!gadget_is_superspeed(gadget))
  1084. goto unknown;
  1085. if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
  1086. goto unknown;
  1087. switch (w_value) {
  1088. case USB_INTRF_FUNC_SUSPEND:
  1089. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1090. break;
  1091. f = cdev->config->interface[intf];
  1092. if (!f)
  1093. break;
  1094. value = 0;
  1095. if (f->func_suspend)
  1096. value = f->func_suspend(f, w_index >> 8);
  1097. if (value < 0) {
  1098. ERROR(cdev,
  1099. "func_suspend() returned error %d\n",
  1100. value);
  1101. value = 0;
  1102. }
  1103. break;
  1104. }
  1105. break;
  1106. default:
  1107. unknown:
  1108. VDBG(cdev,
  1109. "non-core control req%02x.%02x v%04x i%04x l%d\n",
  1110. ctrl->bRequestType, ctrl->bRequest,
  1111. w_value, w_index, w_length);
  1112. /* functions always handle their interfaces and endpoints...
  1113. * punt other recipients (other, WUSB, ...) to the current
  1114. * configuration code.
  1115. *
  1116. * REVISIT it could make sense to let the composite device
  1117. * take such requests too, if that's ever needed: to work
  1118. * in config 0, etc.
  1119. */
  1120. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1121. case USB_RECIP_INTERFACE:
  1122. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1123. break;
  1124. f = cdev->config->interface[intf];
  1125. break;
  1126. case USB_RECIP_ENDPOINT:
  1127. endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
  1128. list_for_each_entry(f, &cdev->config->functions, list) {
  1129. if (test_bit(endp, f->endpoints))
  1130. break;
  1131. }
  1132. if (&f->list == &cdev->config->functions)
  1133. f = NULL;
  1134. break;
  1135. }
  1136. if (f && f->setup)
  1137. value = f->setup(f, ctrl);
  1138. else {
  1139. struct usb_configuration *c;
  1140. c = cdev->config;
  1141. if (c && c->setup)
  1142. value = c->setup(c, ctrl);
  1143. }
  1144. goto done;
  1145. }
  1146. /* respond with data transfer before status phase? */
  1147. if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
  1148. req->length = value;
  1149. req->zero = value < w_length;
  1150. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  1151. if (value < 0) {
  1152. DBG(cdev, "ep_queue --> %d\n", value);
  1153. req->status = 0;
  1154. composite_setup_complete(gadget->ep0, req);
  1155. }
  1156. } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
  1157. WARN(cdev,
  1158. "%s: Delayed status not supported for w_length != 0",
  1159. __func__);
  1160. }
  1161. done:
  1162. /* device either stalls (value < 0) or reports success */
  1163. return value;
  1164. }
  1165. static void composite_disconnect(struct usb_gadget *gadget)
  1166. {
  1167. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1168. unsigned long flags;
  1169. /* REVISIT: should we have config and device level
  1170. * disconnect callbacks?
  1171. */
  1172. spin_lock_irqsave(&cdev->lock, flags);
  1173. if (cdev->config)
  1174. reset_config(cdev);
  1175. if (composite->disconnect)
  1176. composite->disconnect(cdev);
  1177. spin_unlock_irqrestore(&cdev->lock, flags);
  1178. }
  1179. /*-------------------------------------------------------------------------*/
  1180. static ssize_t composite_show_suspended(struct device *dev,
  1181. struct device_attribute *attr,
  1182. char *buf)
  1183. {
  1184. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  1185. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1186. return sprintf(buf, "%d\n", cdev->suspended);
  1187. }
  1188. static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
  1189. static void
  1190. composite_unbind(struct usb_gadget *gadget)
  1191. {
  1192. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1193. /* composite_disconnect() must already have been called
  1194. * by the underlying peripheral controller driver!
  1195. * so there's no i/o concurrency that could affect the
  1196. * state protected by cdev->lock.
  1197. */
  1198. WARN_ON(cdev->config);
  1199. while (!list_empty(&cdev->configs)) {
  1200. struct usb_configuration *c;
  1201. c = list_first_entry(&cdev->configs,
  1202. struct usb_configuration, list);
  1203. while (!list_empty(&c->functions)) {
  1204. struct usb_function *f;
  1205. f = list_first_entry(&c->functions,
  1206. struct usb_function, list);
  1207. list_del(&f->list);
  1208. if (f->unbind) {
  1209. DBG(cdev, "unbind function '%s'/%p\n",
  1210. f->name, f);
  1211. f->unbind(c, f);
  1212. /* may free memory for "f" */
  1213. }
  1214. }
  1215. list_del(&c->list);
  1216. if (c->unbind) {
  1217. DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
  1218. c->unbind(c);
  1219. /* may free memory for "c" */
  1220. }
  1221. }
  1222. if (composite->unbind)
  1223. composite->unbind(cdev);
  1224. if (cdev->req) {
  1225. kfree(cdev->req->buf);
  1226. usb_ep_free_request(gadget->ep0, cdev->req);
  1227. }
  1228. device_remove_file(&gadget->dev, &dev_attr_suspended);
  1229. kfree(cdev);
  1230. set_gadget_data(gadget, NULL);
  1231. composite = NULL;
  1232. }
  1233. static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
  1234. {
  1235. if (!*desc) {
  1236. int ret = usb_string_id(cdev);
  1237. if (unlikely(ret < 0))
  1238. WARNING(cdev, "failed to override string ID\n");
  1239. else
  1240. *desc = ret;
  1241. }
  1242. return *desc;
  1243. }
  1244. static int composite_bind(struct usb_gadget *gadget)
  1245. {
  1246. struct usb_composite_dev *cdev;
  1247. int status = -ENOMEM;
  1248. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  1249. if (!cdev)
  1250. return status;
  1251. spin_lock_init(&cdev->lock);
  1252. cdev->gadget = gadget;
  1253. set_gadget_data(gadget, cdev);
  1254. INIT_LIST_HEAD(&cdev->configs);
  1255. /* preallocate control response and buffer */
  1256. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  1257. if (!cdev->req)
  1258. goto fail;
  1259. cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
  1260. if (!cdev->req->buf)
  1261. goto fail;
  1262. cdev->req->complete = composite_setup_complete;
  1263. gadget->ep0->driver_data = cdev;
  1264. cdev->bufsiz = USB_BUFSIZ;
  1265. cdev->driver = composite;
  1266. /*
  1267. * As per USB compliance update, a device that is actively drawing
  1268. * more than 100mA from USB must report itself as bus-powered in
  1269. * the GetStatus(DEVICE) call.
  1270. */
  1271. if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
  1272. usb_gadget_set_selfpowered(gadget);
  1273. /* interface and string IDs start at zero via kzalloc.
  1274. * we force endpoints to start unassigned; few controller
  1275. * drivers will zero ep->driver_data.
  1276. */
  1277. usb_ep_autoconfig_reset(cdev->gadget);
  1278. /* composite gadget needs to assign strings for whole device (like
  1279. * serial number), register function drivers, potentially update
  1280. * power state and consumption, etc
  1281. */
  1282. status = composite_gadget_bind(cdev);
  1283. if (status < 0)
  1284. goto fail;
  1285. cdev->desc = *composite->dev;
  1286. /* standardized runtime overrides for device ID data */
  1287. if (idVendor)
  1288. cdev->desc.idVendor = cpu_to_le16(idVendor);
  1289. if (idProduct)
  1290. cdev->desc.idProduct = cpu_to_le16(idProduct);
  1291. if (bcdDevice)
  1292. cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
  1293. /* string overrides */
  1294. if (iManufacturer || !cdev->desc.iManufacturer) {
  1295. if (!iManufacturer && !composite->iManufacturer &&
  1296. !*composite_manufacturer)
  1297. snprintf(composite_manufacturer,
  1298. sizeof composite_manufacturer,
  1299. "%s %s with %s",
  1300. init_utsname()->sysname,
  1301. init_utsname()->release,
  1302. gadget->name);
  1303. cdev->manufacturer_override =
  1304. override_id(cdev, &cdev->desc.iManufacturer);
  1305. }
  1306. if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
  1307. cdev->product_override =
  1308. override_id(cdev, &cdev->desc.iProduct);
  1309. if (iSerialNumber)
  1310. cdev->serial_override =
  1311. override_id(cdev, &cdev->desc.iSerialNumber);
  1312. /* has userspace failed to provide a serial number? */
  1313. if (composite->needs_serial && !cdev->desc.iSerialNumber)
  1314. WARNING(cdev, "userspace failed to provide iSerialNumber\n");
  1315. /* finish up */
  1316. status = device_create_file(&gadget->dev, &dev_attr_suspended);
  1317. if (status)
  1318. goto fail;
  1319. INFO(cdev, "%s ready\n", composite->name);
  1320. return 0;
  1321. fail:
  1322. composite_unbind(gadget);
  1323. return status;
  1324. }
  1325. /*-------------------------------------------------------------------------*/
  1326. static void
  1327. composite_suspend(struct usb_gadget *gadget)
  1328. {
  1329. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1330. struct usb_function *f;
  1331. /* REVISIT: should we have config level
  1332. * suspend/resume callbacks?
  1333. */
  1334. DBG(cdev, "suspend\n");
  1335. if (cdev->config) {
  1336. list_for_each_entry(f, &cdev->config->functions, list) {
  1337. if (f->suspend)
  1338. f->suspend(f);
  1339. }
  1340. }
  1341. if (composite->suspend)
  1342. composite->suspend(cdev);
  1343. cdev->suspended = 1;
  1344. usb_gadget_vbus_draw(gadget, 2);
  1345. }
  1346. static void
  1347. composite_resume(struct usb_gadget *gadget)
  1348. {
  1349. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1350. struct usb_function *f;
  1351. u8 maxpower;
  1352. /* REVISIT: should we have config level
  1353. * suspend/resume callbacks?
  1354. */
  1355. DBG(cdev, "resume\n");
  1356. if (composite->resume)
  1357. composite->resume(cdev);
  1358. if (cdev->config) {
  1359. list_for_each_entry(f, &cdev->config->functions, list) {
  1360. if (f->resume)
  1361. f->resume(f);
  1362. }
  1363. maxpower = cdev->config->bMaxPower;
  1364. usb_gadget_vbus_draw(gadget, maxpower ?
  1365. (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
  1366. }
  1367. cdev->suspended = 0;
  1368. }
  1369. /*-------------------------------------------------------------------------*/
  1370. static struct usb_gadget_driver composite_driver = {
  1371. #ifdef CONFIG_USB_GADGET_SUPERSPEED
  1372. .speed = USB_SPEED_SUPER,
  1373. #else
  1374. .speed = USB_SPEED_HIGH,
  1375. #endif
  1376. .unbind = composite_unbind,
  1377. .setup = composite_setup,
  1378. .disconnect = composite_disconnect,
  1379. .suspend = composite_suspend,
  1380. .resume = composite_resume,
  1381. .driver = {
  1382. .owner = THIS_MODULE,
  1383. },
  1384. };
  1385. /**
  1386. * usb_composite_probe() - register a composite driver
  1387. * @driver: the driver to register
  1388. * @bind: the callback used to allocate resources that are shared across the
  1389. * whole device, such as string IDs, and add its configurations using
  1390. * @usb_add_config(). This may fail by returning a negative errno
  1391. * value; it should return zero on successful initialization.
  1392. * Context: single threaded during gadget setup
  1393. *
  1394. * This function is used to register drivers using the composite driver
  1395. * framework. The return value is zero, or a negative errno value.
  1396. * Those values normally come from the driver's @bind method, which does
  1397. * all the work of setting up the driver to match the hardware.
  1398. *
  1399. * On successful return, the gadget is ready to respond to requests from
  1400. * the host, unless one of its components invokes usb_gadget_disconnect()
  1401. * while it was binding. That would usually be done in order to wait for
  1402. * some userspace participation.
  1403. */
  1404. int usb_composite_probe(struct usb_composite_driver *driver,
  1405. int (*bind)(struct usb_composite_dev *cdev))
  1406. {
  1407. if (!driver || !driver->dev || !bind || composite)
  1408. return -EINVAL;
  1409. if (!driver->name)
  1410. driver->name = "composite";
  1411. if (!driver->iProduct)
  1412. driver->iProduct = driver->name;
  1413. composite_driver.function = (char *) driver->name;
  1414. composite_driver.driver.name = driver->name;
  1415. composite_driver.speed = min((u8)composite_driver.speed,
  1416. (u8)driver->max_speed);
  1417. composite = driver;
  1418. composite_gadget_bind = bind;
  1419. return usb_gadget_probe_driver(&composite_driver, composite_bind);
  1420. }
  1421. /**
  1422. * usb_composite_unregister() - unregister a composite driver
  1423. * @driver: the driver to unregister
  1424. *
  1425. * This function is used to unregister drivers using the composite
  1426. * driver framework.
  1427. */
  1428. void usb_composite_unregister(struct usb_composite_driver *driver)
  1429. {
  1430. if (composite != driver)
  1431. return;
  1432. usb_gadget_unregister_driver(&composite_driver);
  1433. }
  1434. /**
  1435. * usb_composite_setup_continue() - Continue with the control transfer
  1436. * @cdev: the composite device who's control transfer was kept waiting
  1437. *
  1438. * This function must be called by the USB function driver to continue
  1439. * with the control transfer's data/status stage in case it had requested to
  1440. * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
  1441. * can request the composite framework to delay the setup request's data/status
  1442. * stages by returning USB_GADGET_DELAYED_STATUS.
  1443. */
  1444. void usb_composite_setup_continue(struct usb_composite_dev *cdev)
  1445. {
  1446. int value;
  1447. struct usb_request *req = cdev->req;
  1448. unsigned long flags;
  1449. DBG(cdev, "%s\n", __func__);
  1450. spin_lock_irqsave(&cdev->lock, flags);
  1451. if (cdev->delayed_status == 0) {
  1452. WARN(cdev, "%s: Unexpected call\n", __func__);
  1453. } else if (--cdev->delayed_status == 0) {
  1454. DBG(cdev, "%s: Completing delayed status\n", __func__);
  1455. req->length = 0;
  1456. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  1457. if (value < 0) {
  1458. DBG(cdev, "ep_queue --> %d\n", value);
  1459. req->status = 0;
  1460. composite_setup_complete(cdev->gadget->ep0, req);
  1461. }
  1462. }
  1463. spin_unlock_irqrestore(&cdev->lock, flags);
  1464. }