composite.c 45 KB

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