composite.c 46 KB

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