composite.c 46 KB

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