composite.c 46 KB

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