composite.c 45 KB

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