composite.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
  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. #include "u_os_desc.h"
  21. /**
  22. * struct usb_os_string - represents OS String to be reported by a gadget
  23. * @bLength: total length of the entire descritor, always 0x12
  24. * @bDescriptorType: USB_DT_STRING
  25. * @qwSignature: the OS String proper
  26. * @bMS_VendorCode: code used by the host for subsequent requests
  27. * @bPad: not used, must be zero
  28. */
  29. struct usb_os_string {
  30. __u8 bLength;
  31. __u8 bDescriptorType;
  32. __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
  33. __u8 bMS_VendorCode;
  34. __u8 bPad;
  35. } __packed;
  36. /*
  37. * The code in this file is utility code, used to build a gadget driver
  38. * from one or more "function" drivers, one or more "configuration"
  39. * objects, and a "usb_composite_driver" by gluing them together along
  40. * with the relevant device-wide data.
  41. */
  42. static struct usb_gadget_strings **get_containers_gs(
  43. struct usb_gadget_string_container *uc)
  44. {
  45. return (struct usb_gadget_strings **)uc->stash;
  46. }
  47. /**
  48. * next_ep_desc() - advance to the next EP descriptor
  49. * @t: currect pointer within descriptor array
  50. *
  51. * Return: next EP descriptor or NULL
  52. *
  53. * Iterate over @t until either EP descriptor found or
  54. * NULL (that indicates end of list) encountered
  55. */
  56. static struct usb_descriptor_header**
  57. next_ep_desc(struct usb_descriptor_header **t)
  58. {
  59. for (; *t; t++) {
  60. if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
  61. return t;
  62. }
  63. return NULL;
  64. }
  65. /*
  66. * for_each_ep_desc()- iterate over endpoint descriptors in the
  67. * descriptors list
  68. * @start: pointer within descriptor array.
  69. * @ep_desc: endpoint descriptor to use as the loop cursor
  70. */
  71. #define for_each_ep_desc(start, ep_desc) \
  72. for (ep_desc = next_ep_desc(start); \
  73. ep_desc; ep_desc = next_ep_desc(ep_desc+1))
  74. /**
  75. * config_ep_by_speed() - configures the given endpoint
  76. * according to gadget speed.
  77. * @g: pointer to the gadget
  78. * @f: usb function
  79. * @_ep: the endpoint to configure
  80. *
  81. * Return: error code, 0 on success
  82. *
  83. * This function chooses the right descriptors for a given
  84. * endpoint according to gadget speed and saves it in the
  85. * endpoint desc field. If the endpoint already has a descriptor
  86. * assigned to it - overwrites it with currently corresponding
  87. * descriptor. The endpoint maxpacket field is updated according
  88. * to the chosen descriptor.
  89. * Note: the supplied function should hold all the descriptors
  90. * for supported speeds
  91. */
  92. int config_ep_by_speed(struct usb_gadget *g,
  93. struct usb_function *f,
  94. struct usb_ep *_ep)
  95. {
  96. struct usb_composite_dev *cdev = get_gadget_data(g);
  97. struct usb_endpoint_descriptor *chosen_desc = NULL;
  98. struct usb_descriptor_header **speed_desc = NULL;
  99. struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
  100. int want_comp_desc = 0;
  101. struct usb_descriptor_header **d_spd; /* cursor for speed desc */
  102. if (!g || !f || !_ep)
  103. return -EIO;
  104. /* select desired speed */
  105. switch (g->speed) {
  106. case USB_SPEED_SUPER:
  107. if (gadget_is_superspeed(g)) {
  108. speed_desc = f->ss_descriptors;
  109. want_comp_desc = 1;
  110. break;
  111. }
  112. /* else: Fall trough */
  113. case USB_SPEED_HIGH:
  114. if (gadget_is_dualspeed(g)) {
  115. speed_desc = f->hs_descriptors;
  116. break;
  117. }
  118. /* else: fall through */
  119. default:
  120. speed_desc = f->fs_descriptors;
  121. }
  122. /* find descriptors */
  123. for_each_ep_desc(speed_desc, d_spd) {
  124. chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
  125. if (chosen_desc->bEndpointAddress == _ep->address)
  126. goto ep_found;
  127. }
  128. return -EIO;
  129. ep_found:
  130. /* commit results */
  131. _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
  132. _ep->desc = chosen_desc;
  133. _ep->comp_desc = NULL;
  134. _ep->maxburst = 0;
  135. _ep->mult = 0;
  136. if (!want_comp_desc)
  137. return 0;
  138. /*
  139. * Companion descriptor should follow EP descriptor
  140. * USB 3.0 spec, #9.6.7
  141. */
  142. comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
  143. if (!comp_desc ||
  144. (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
  145. return -EIO;
  146. _ep->comp_desc = comp_desc;
  147. if (g->speed == USB_SPEED_SUPER) {
  148. switch (usb_endpoint_type(_ep->desc)) {
  149. case USB_ENDPOINT_XFER_ISOC:
  150. /* mult: bits 1:0 of bmAttributes */
  151. _ep->mult = comp_desc->bmAttributes & 0x3;
  152. case USB_ENDPOINT_XFER_BULK:
  153. case USB_ENDPOINT_XFER_INT:
  154. _ep->maxburst = comp_desc->bMaxBurst + 1;
  155. break;
  156. default:
  157. if (comp_desc->bMaxBurst != 0)
  158. ERROR(cdev, "ep0 bMaxBurst must be 0\n");
  159. _ep->maxburst = 1;
  160. break;
  161. }
  162. }
  163. return 0;
  164. }
  165. EXPORT_SYMBOL_GPL(config_ep_by_speed);
  166. /**
  167. * usb_add_function() - add a function to a configuration
  168. * @config: the configuration
  169. * @function: the function being added
  170. * Context: single threaded during gadget setup
  171. *
  172. * After initialization, each configuration must have one or more
  173. * functions added to it. Adding a function involves calling its @bind()
  174. * method to allocate resources such as interface and string identifiers
  175. * and endpoints.
  176. *
  177. * This function returns the value of the function's bind(), which is
  178. * zero for success else a negative errno value.
  179. */
  180. int usb_add_function(struct usb_configuration *config,
  181. struct usb_function *function)
  182. {
  183. int value = -EINVAL;
  184. DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
  185. function->name, function,
  186. config->label, config);
  187. if (!function->set_alt || !function->disable)
  188. goto done;
  189. function->config = config;
  190. list_add_tail(&function->list, &config->functions);
  191. /* REVISIT *require* function->bind? */
  192. if (function->bind) {
  193. value = function->bind(config, function);
  194. if (value < 0) {
  195. list_del(&function->list);
  196. function->config = NULL;
  197. }
  198. } else
  199. value = 0;
  200. /* We allow configurations that don't work at both speeds.
  201. * If we run into a lowspeed Linux system, treat it the same
  202. * as full speed ... it's the function drivers that will need
  203. * to avoid bulk and ISO transfers.
  204. */
  205. if (!config->fullspeed && function->fs_descriptors)
  206. config->fullspeed = true;
  207. if (!config->highspeed && function->hs_descriptors)
  208. config->highspeed = true;
  209. if (!config->superspeed && function->ss_descriptors)
  210. config->superspeed = true;
  211. done:
  212. if (value)
  213. DBG(config->cdev, "adding '%s'/%p --> %d\n",
  214. function->name, function, value);
  215. return value;
  216. }
  217. EXPORT_SYMBOL_GPL(usb_add_function);
  218. void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
  219. {
  220. if (f->disable)
  221. f->disable(f);
  222. bitmap_zero(f->endpoints, 32);
  223. list_del(&f->list);
  224. if (f->unbind)
  225. f->unbind(c, f);
  226. }
  227. EXPORT_SYMBOL_GPL(usb_remove_function);
  228. /**
  229. * usb_function_deactivate - prevent function and gadget enumeration
  230. * @function: the function that isn't yet ready to respond
  231. *
  232. * Blocks response of the gadget driver to host enumeration by
  233. * preventing the data line pullup from being activated. This is
  234. * normally called during @bind() processing to change from the
  235. * initial "ready to respond" state, or when a required resource
  236. * becomes available.
  237. *
  238. * For example, drivers that serve as a passthrough to a userspace
  239. * daemon can block enumeration unless that daemon (such as an OBEX,
  240. * MTP, or print server) is ready to handle host requests.
  241. *
  242. * Not all systems support software control of their USB peripheral
  243. * data pullups.
  244. *
  245. * Returns zero on success, else negative errno.
  246. */
  247. int usb_function_deactivate(struct usb_function *function)
  248. {
  249. struct usb_composite_dev *cdev = function->config->cdev;
  250. unsigned long flags;
  251. int status = 0;
  252. spin_lock_irqsave(&cdev->lock, flags);
  253. if (cdev->deactivations == 0)
  254. status = usb_gadget_disconnect(cdev->gadget);
  255. if (status == 0)
  256. cdev->deactivations++;
  257. spin_unlock_irqrestore(&cdev->lock, flags);
  258. return status;
  259. }
  260. EXPORT_SYMBOL_GPL(usb_function_deactivate);
  261. /**
  262. * usb_function_activate - allow function and gadget enumeration
  263. * @function: function on which usb_function_activate() was called
  264. *
  265. * Reverses effect of usb_function_deactivate(). If no more functions
  266. * are delaying their activation, the gadget driver will respond to
  267. * host enumeration procedures.
  268. *
  269. * Returns zero on success, else negative errno.
  270. */
  271. int usb_function_activate(struct usb_function *function)
  272. {
  273. struct usb_composite_dev *cdev = function->config->cdev;
  274. unsigned long flags;
  275. int status = 0;
  276. spin_lock_irqsave(&cdev->lock, flags);
  277. if (WARN_ON(cdev->deactivations == 0))
  278. status = -EINVAL;
  279. else {
  280. cdev->deactivations--;
  281. if (cdev->deactivations == 0)
  282. status = usb_gadget_connect(cdev->gadget);
  283. }
  284. spin_unlock_irqrestore(&cdev->lock, flags);
  285. return status;
  286. }
  287. EXPORT_SYMBOL_GPL(usb_function_activate);
  288. /**
  289. * usb_interface_id() - allocate an unused interface ID
  290. * @config: configuration associated with the interface
  291. * @function: function handling the interface
  292. * Context: single threaded during gadget setup
  293. *
  294. * usb_interface_id() is called from usb_function.bind() callbacks to
  295. * allocate new interface IDs. The function driver will then store that
  296. * ID in interface, association, CDC union, and other descriptors. It
  297. * will also handle any control requests targeted at that interface,
  298. * particularly changing its altsetting via set_alt(). There may
  299. * also be class-specific or vendor-specific requests to handle.
  300. *
  301. * All interface identifier should be allocated using this routine, to
  302. * ensure that for example different functions don't wrongly assign
  303. * different meanings to the same identifier. Note that since interface
  304. * identifiers are configuration-specific, functions used in more than
  305. * one configuration (or more than once in a given configuration) need
  306. * multiple versions of the relevant descriptors.
  307. *
  308. * Returns the interface ID which was allocated; or -ENODEV if no
  309. * more interface IDs can be allocated.
  310. */
  311. int usb_interface_id(struct usb_configuration *config,
  312. struct usb_function *function)
  313. {
  314. unsigned id = config->next_interface_id;
  315. if (id < MAX_CONFIG_INTERFACES) {
  316. config->interface[id] = function;
  317. config->next_interface_id = id + 1;
  318. return id;
  319. }
  320. return -ENODEV;
  321. }
  322. EXPORT_SYMBOL_GPL(usb_interface_id);
  323. static u8 encode_bMaxPower(enum usb_device_speed speed,
  324. struct usb_configuration *c)
  325. {
  326. unsigned val;
  327. if (c->MaxPower)
  328. val = c->MaxPower;
  329. else
  330. val = CONFIG_USB_GADGET_VBUS_DRAW;
  331. if (!val)
  332. return 0;
  333. switch (speed) {
  334. case USB_SPEED_SUPER:
  335. return DIV_ROUND_UP(val, 8);
  336. default:
  337. return DIV_ROUND_UP(val, 2);
  338. }
  339. }
  340. static int config_buf(struct usb_configuration *config,
  341. enum usb_device_speed speed, void *buf, u8 type)
  342. {
  343. struct usb_config_descriptor *c = buf;
  344. void *next = buf + USB_DT_CONFIG_SIZE;
  345. int len;
  346. struct usb_function *f;
  347. int status;
  348. len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
  349. /* write the config descriptor */
  350. c = buf;
  351. c->bLength = USB_DT_CONFIG_SIZE;
  352. c->bDescriptorType = type;
  353. /* wTotalLength is written later */
  354. c->bNumInterfaces = config->next_interface_id;
  355. c->bConfigurationValue = config->bConfigurationValue;
  356. c->iConfiguration = config->iConfiguration;
  357. c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
  358. c->bMaxPower = encode_bMaxPower(speed, config);
  359. /* There may be e.g. OTG descriptors */
  360. if (config->descriptors) {
  361. status = usb_descriptor_fillbuf(next, len,
  362. config->descriptors);
  363. if (status < 0)
  364. return status;
  365. len -= status;
  366. next += status;
  367. }
  368. /* add each function's descriptors */
  369. list_for_each_entry(f, &config->functions, list) {
  370. struct usb_descriptor_header **descriptors;
  371. switch (speed) {
  372. case USB_SPEED_SUPER:
  373. descriptors = f->ss_descriptors;
  374. break;
  375. case USB_SPEED_HIGH:
  376. descriptors = f->hs_descriptors;
  377. break;
  378. default:
  379. descriptors = f->fs_descriptors;
  380. }
  381. if (!descriptors)
  382. continue;
  383. status = usb_descriptor_fillbuf(next, len,
  384. (const struct usb_descriptor_header **) descriptors);
  385. if (status < 0)
  386. return status;
  387. len -= status;
  388. next += status;
  389. }
  390. len = next - buf;
  391. c->wTotalLength = cpu_to_le16(len);
  392. return len;
  393. }
  394. static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
  395. {
  396. struct usb_gadget *gadget = cdev->gadget;
  397. struct usb_configuration *c;
  398. struct list_head *pos;
  399. u8 type = w_value >> 8;
  400. enum usb_device_speed speed = USB_SPEED_UNKNOWN;
  401. if (gadget->speed == USB_SPEED_SUPER)
  402. speed = gadget->speed;
  403. else if (gadget_is_dualspeed(gadget)) {
  404. int hs = 0;
  405. if (gadget->speed == USB_SPEED_HIGH)
  406. hs = 1;
  407. if (type == USB_DT_OTHER_SPEED_CONFIG)
  408. hs = !hs;
  409. if (hs)
  410. speed = USB_SPEED_HIGH;
  411. }
  412. /* This is a lookup by config *INDEX* */
  413. w_value &= 0xff;
  414. pos = &cdev->configs;
  415. c = cdev->os_desc_config;
  416. if (c)
  417. goto check_config;
  418. while ((pos = pos->next) != &cdev->configs) {
  419. c = list_entry(pos, typeof(*c), list);
  420. /* skip OS Descriptors config which is handled separately */
  421. if (c == cdev->os_desc_config)
  422. continue;
  423. check_config:
  424. /* ignore configs that won't work at this speed */
  425. switch (speed) {
  426. case USB_SPEED_SUPER:
  427. if (!c->superspeed)
  428. continue;
  429. break;
  430. case USB_SPEED_HIGH:
  431. if (!c->highspeed)
  432. continue;
  433. break;
  434. default:
  435. if (!c->fullspeed)
  436. continue;
  437. }
  438. if (w_value == 0)
  439. return config_buf(c, speed, cdev->req->buf, type);
  440. w_value--;
  441. }
  442. return -EINVAL;
  443. }
  444. static int count_configs(struct usb_composite_dev *cdev, unsigned type)
  445. {
  446. struct usb_gadget *gadget = cdev->gadget;
  447. struct usb_configuration *c;
  448. unsigned count = 0;
  449. int hs = 0;
  450. int ss = 0;
  451. if (gadget_is_dualspeed(gadget)) {
  452. if (gadget->speed == USB_SPEED_HIGH)
  453. hs = 1;
  454. if (gadget->speed == USB_SPEED_SUPER)
  455. ss = 1;
  456. if (type == USB_DT_DEVICE_QUALIFIER)
  457. hs = !hs;
  458. }
  459. list_for_each_entry(c, &cdev->configs, list) {
  460. /* ignore configs that won't work at this speed */
  461. if (ss) {
  462. if (!c->superspeed)
  463. continue;
  464. } else if (hs) {
  465. if (!c->highspeed)
  466. continue;
  467. } else {
  468. if (!c->fullspeed)
  469. continue;
  470. }
  471. count++;
  472. }
  473. return count;
  474. }
  475. /**
  476. * bos_desc() - prepares the BOS descriptor.
  477. * @cdev: pointer to usb_composite device to generate the bos
  478. * descriptor for
  479. *
  480. * This function generates the BOS (Binary Device Object)
  481. * descriptor and its device capabilities descriptors. The BOS
  482. * descriptor should be supported by a SuperSpeed device.
  483. */
  484. static int bos_desc(struct usb_composite_dev *cdev)
  485. {
  486. struct usb_ext_cap_descriptor *usb_ext;
  487. struct usb_ss_cap_descriptor *ss_cap;
  488. struct usb_dcd_config_params dcd_config_params;
  489. struct usb_bos_descriptor *bos = cdev->req->buf;
  490. bos->bLength = USB_DT_BOS_SIZE;
  491. bos->bDescriptorType = USB_DT_BOS;
  492. bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
  493. bos->bNumDeviceCaps = 0;
  494. /*
  495. * A SuperSpeed device shall include the USB2.0 extension descriptor
  496. * and shall support LPM when operating in USB2.0 HS mode.
  497. */
  498. usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  499. bos->bNumDeviceCaps++;
  500. le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
  501. usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
  502. usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  503. usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
  504. usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
  505. /*
  506. * The Superspeed USB Capability descriptor shall be implemented by all
  507. * SuperSpeed devices.
  508. */
  509. ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
  510. bos->bNumDeviceCaps++;
  511. le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
  512. ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
  513. ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
  514. ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
  515. ss_cap->bmAttributes = 0; /* LTM is not supported yet */
  516. ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
  517. USB_FULL_SPEED_OPERATION |
  518. USB_HIGH_SPEED_OPERATION |
  519. USB_5GBPS_OPERATION);
  520. ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
  521. /* Get Controller configuration */
  522. if (cdev->gadget->ops->get_config_params)
  523. cdev->gadget->ops->get_config_params(&dcd_config_params);
  524. else {
  525. dcd_config_params.bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
  526. dcd_config_params.bU2DevExitLat =
  527. cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
  528. }
  529. ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
  530. ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
  531. return le16_to_cpu(bos->wTotalLength);
  532. }
  533. static void device_qual(struct usb_composite_dev *cdev)
  534. {
  535. struct usb_qualifier_descriptor *qual = cdev->req->buf;
  536. qual->bLength = sizeof(*qual);
  537. qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  538. /* POLICY: same bcdUSB and device type info at both speeds */
  539. qual->bcdUSB = cdev->desc.bcdUSB;
  540. qual->bDeviceClass = cdev->desc.bDeviceClass;
  541. qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
  542. qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
  543. /* ASSUME same EP0 fifo size at both speeds */
  544. qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
  545. qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
  546. qual->bRESERVED = 0;
  547. }
  548. /*-------------------------------------------------------------------------*/
  549. static void reset_config(struct usb_composite_dev *cdev)
  550. {
  551. struct usb_function *f;
  552. DBG(cdev, "reset config\n");
  553. list_for_each_entry(f, &cdev->config->functions, list) {
  554. if (f->disable)
  555. f->disable(f);
  556. bitmap_zero(f->endpoints, 32);
  557. }
  558. cdev->config = NULL;
  559. cdev->delayed_status = 0;
  560. }
  561. static int set_config(struct usb_composite_dev *cdev,
  562. const struct usb_ctrlrequest *ctrl, unsigned number)
  563. {
  564. struct usb_gadget *gadget = cdev->gadget;
  565. struct usb_configuration *c = NULL;
  566. int result = -EINVAL;
  567. unsigned power = gadget_is_otg(gadget) ? 8 : 100;
  568. int tmp;
  569. if (number) {
  570. list_for_each_entry(c, &cdev->configs, list) {
  571. if (c->bConfigurationValue == number) {
  572. /*
  573. * We disable the FDs of the previous
  574. * configuration only if the new configuration
  575. * is a valid one
  576. */
  577. if (cdev->config)
  578. reset_config(cdev);
  579. result = 0;
  580. break;
  581. }
  582. }
  583. if (result < 0)
  584. goto done;
  585. } else { /* Zero configuration value - need to reset the config */
  586. if (cdev->config)
  587. reset_config(cdev);
  588. result = 0;
  589. }
  590. INFO(cdev, "%s config #%d: %s\n",
  591. usb_speed_string(gadget->speed),
  592. number, c ? c->label : "unconfigured");
  593. if (!c)
  594. goto done;
  595. usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
  596. cdev->config = c;
  597. /* Initialize all interfaces by setting them to altsetting zero. */
  598. for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
  599. struct usb_function *f = c->interface[tmp];
  600. struct usb_descriptor_header **descriptors;
  601. if (!f)
  602. break;
  603. /*
  604. * Record which endpoints are used by the function. This is used
  605. * to dispatch control requests targeted at that endpoint to the
  606. * function's setup callback instead of the current
  607. * configuration's setup callback.
  608. */
  609. switch (gadget->speed) {
  610. case USB_SPEED_SUPER:
  611. descriptors = f->ss_descriptors;
  612. break;
  613. case USB_SPEED_HIGH:
  614. descriptors = f->hs_descriptors;
  615. break;
  616. default:
  617. descriptors = f->fs_descriptors;
  618. }
  619. for (; *descriptors; ++descriptors) {
  620. struct usb_endpoint_descriptor *ep;
  621. int addr;
  622. if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
  623. continue;
  624. ep = (struct usb_endpoint_descriptor *)*descriptors;
  625. addr = ((ep->bEndpointAddress & 0x80) >> 3)
  626. | (ep->bEndpointAddress & 0x0f);
  627. set_bit(addr, f->endpoints);
  628. }
  629. result = f->set_alt(f, tmp, 0);
  630. if (result < 0) {
  631. DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
  632. tmp, f->name, f, result);
  633. reset_config(cdev);
  634. goto done;
  635. }
  636. if (result == USB_GADGET_DELAYED_STATUS) {
  637. DBG(cdev,
  638. "%s: interface %d (%s) requested delayed status\n",
  639. __func__, tmp, f->name);
  640. cdev->delayed_status++;
  641. DBG(cdev, "delayed_status count %d\n",
  642. cdev->delayed_status);
  643. }
  644. }
  645. /* when we return, be sure our power usage is valid */
  646. power = c->MaxPower ? c->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
  647. done:
  648. usb_gadget_vbus_draw(gadget, power);
  649. if (result >= 0 && cdev->delayed_status)
  650. result = USB_GADGET_DELAYED_STATUS;
  651. return result;
  652. }
  653. int usb_add_config_only(struct usb_composite_dev *cdev,
  654. struct usb_configuration *config)
  655. {
  656. struct usb_configuration *c;
  657. if (!config->bConfigurationValue)
  658. return -EINVAL;
  659. /* Prevent duplicate configuration identifiers */
  660. list_for_each_entry(c, &cdev->configs, list) {
  661. if (c->bConfigurationValue == config->bConfigurationValue)
  662. return -EBUSY;
  663. }
  664. config->cdev = cdev;
  665. list_add_tail(&config->list, &cdev->configs);
  666. INIT_LIST_HEAD(&config->functions);
  667. config->next_interface_id = 0;
  668. memset(config->interface, 0, sizeof(config->interface));
  669. return 0;
  670. }
  671. EXPORT_SYMBOL_GPL(usb_add_config_only);
  672. /**
  673. * usb_add_config() - add a configuration to a device.
  674. * @cdev: wraps the USB gadget
  675. * @config: the configuration, with bConfigurationValue assigned
  676. * @bind: the configuration's bind function
  677. * Context: single threaded during gadget setup
  678. *
  679. * One of the main tasks of a composite @bind() routine is to
  680. * add each of the configurations it supports, using this routine.
  681. *
  682. * This function returns the value of the configuration's @bind(), which
  683. * is zero for success else a negative errno value. Binding configurations
  684. * assigns global resources including string IDs, and per-configuration
  685. * resources such as interface IDs and endpoints.
  686. */
  687. int usb_add_config(struct usb_composite_dev *cdev,
  688. struct usb_configuration *config,
  689. int (*bind)(struct usb_configuration *))
  690. {
  691. int status = -EINVAL;
  692. if (!bind)
  693. goto done;
  694. DBG(cdev, "adding config #%u '%s'/%p\n",
  695. config->bConfigurationValue,
  696. config->label, config);
  697. status = usb_add_config_only(cdev, config);
  698. if (status)
  699. goto done;
  700. status = bind(config);
  701. if (status < 0) {
  702. while (!list_empty(&config->functions)) {
  703. struct usb_function *f;
  704. f = list_first_entry(&config->functions,
  705. struct usb_function, list);
  706. list_del(&f->list);
  707. if (f->unbind) {
  708. DBG(cdev, "unbind function '%s'/%p\n",
  709. f->name, f);
  710. f->unbind(config, f);
  711. /* may free memory for "f" */
  712. }
  713. }
  714. list_del(&config->list);
  715. config->cdev = NULL;
  716. } else {
  717. unsigned i;
  718. DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
  719. config->bConfigurationValue, config,
  720. config->superspeed ? " super" : "",
  721. config->highspeed ? " high" : "",
  722. config->fullspeed
  723. ? (gadget_is_dualspeed(cdev->gadget)
  724. ? " full"
  725. : " full/low")
  726. : "");
  727. for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
  728. struct usb_function *f = config->interface[i];
  729. if (!f)
  730. continue;
  731. DBG(cdev, " interface %d = %s/%p\n",
  732. i, f->name, f);
  733. }
  734. }
  735. /* set_alt(), or next bind(), sets up
  736. * ep->driver_data as needed.
  737. */
  738. usb_ep_autoconfig_reset(cdev->gadget);
  739. done:
  740. if (status)
  741. DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
  742. config->bConfigurationValue, status);
  743. return status;
  744. }
  745. EXPORT_SYMBOL_GPL(usb_add_config);
  746. static void remove_config(struct usb_composite_dev *cdev,
  747. struct usb_configuration *config)
  748. {
  749. while (!list_empty(&config->functions)) {
  750. struct usb_function *f;
  751. f = list_first_entry(&config->functions,
  752. struct usb_function, list);
  753. list_del(&f->list);
  754. if (f->unbind) {
  755. DBG(cdev, "unbind function '%s'/%p\n", f->name, f);
  756. f->unbind(config, f);
  757. /* may free memory for "f" */
  758. }
  759. }
  760. list_del(&config->list);
  761. if (config->unbind) {
  762. DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
  763. config->unbind(config);
  764. /* may free memory for "c" */
  765. }
  766. }
  767. /**
  768. * usb_remove_config() - remove a configuration from a device.
  769. * @cdev: wraps the USB gadget
  770. * @config: the configuration
  771. *
  772. * Drivers must call usb_gadget_disconnect before calling this function
  773. * to disconnect the device from the host and make sure the host will not
  774. * try to enumerate the device while we are changing the config list.
  775. */
  776. void usb_remove_config(struct usb_composite_dev *cdev,
  777. struct usb_configuration *config)
  778. {
  779. unsigned long flags;
  780. spin_lock_irqsave(&cdev->lock, flags);
  781. if (cdev->config == config)
  782. reset_config(cdev);
  783. spin_unlock_irqrestore(&cdev->lock, flags);
  784. remove_config(cdev, config);
  785. }
  786. /*-------------------------------------------------------------------------*/
  787. /* We support strings in multiple languages ... string descriptor zero
  788. * says which languages are supported. The typical case will be that
  789. * only one language (probably English) is used, with I18N handled on
  790. * the host side.
  791. */
  792. static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
  793. {
  794. const struct usb_gadget_strings *s;
  795. __le16 language;
  796. __le16 *tmp;
  797. while (*sp) {
  798. s = *sp;
  799. language = cpu_to_le16(s->language);
  800. for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
  801. if (*tmp == language)
  802. goto repeat;
  803. }
  804. *tmp++ = language;
  805. repeat:
  806. sp++;
  807. }
  808. }
  809. static int lookup_string(
  810. struct usb_gadget_strings **sp,
  811. void *buf,
  812. u16 language,
  813. int id
  814. )
  815. {
  816. struct usb_gadget_strings *s;
  817. int value;
  818. while (*sp) {
  819. s = *sp++;
  820. if (s->language != language)
  821. continue;
  822. value = usb_gadget_get_string(s, id, buf);
  823. if (value > 0)
  824. return value;
  825. }
  826. return -EINVAL;
  827. }
  828. static int get_string(struct usb_composite_dev *cdev,
  829. void *buf, u16 language, int id)
  830. {
  831. struct usb_composite_driver *composite = cdev->driver;
  832. struct usb_gadget_string_container *uc;
  833. struct usb_configuration *c;
  834. struct usb_function *f;
  835. int len;
  836. /* Yes, not only is USB's I18N support probably more than most
  837. * folk will ever care about ... also, it's all supported here.
  838. * (Except for UTF8 support for Unicode's "Astral Planes".)
  839. */
  840. /* 0 == report all available language codes */
  841. if (id == 0) {
  842. struct usb_string_descriptor *s = buf;
  843. struct usb_gadget_strings **sp;
  844. memset(s, 0, 256);
  845. s->bDescriptorType = USB_DT_STRING;
  846. sp = composite->strings;
  847. if (sp)
  848. collect_langs(sp, s->wData);
  849. list_for_each_entry(c, &cdev->configs, list) {
  850. sp = c->strings;
  851. if (sp)
  852. collect_langs(sp, s->wData);
  853. list_for_each_entry(f, &c->functions, list) {
  854. sp = f->strings;
  855. if (sp)
  856. collect_langs(sp, s->wData);
  857. }
  858. }
  859. list_for_each_entry(uc, &cdev->gstrings, list) {
  860. struct usb_gadget_strings **sp;
  861. sp = get_containers_gs(uc);
  862. collect_langs(sp, s->wData);
  863. }
  864. for (len = 0; len <= 126 && s->wData[len]; len++)
  865. continue;
  866. if (!len)
  867. return -EINVAL;
  868. s->bLength = 2 * (len + 1);
  869. return s->bLength;
  870. }
  871. if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
  872. struct usb_os_string *b = buf;
  873. b->bLength = sizeof(*b);
  874. b->bDescriptorType = USB_DT_STRING;
  875. compiletime_assert(
  876. sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
  877. "qwSignature size must be equal to qw_sign");
  878. memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
  879. b->bMS_VendorCode = cdev->b_vendor_code;
  880. b->bPad = 0;
  881. return sizeof(*b);
  882. }
  883. list_for_each_entry(uc, &cdev->gstrings, list) {
  884. struct usb_gadget_strings **sp;
  885. sp = get_containers_gs(uc);
  886. len = lookup_string(sp, buf, language, id);
  887. if (len > 0)
  888. return len;
  889. }
  890. /* String IDs are device-scoped, so we look up each string
  891. * table we're told about. These lookups are infrequent;
  892. * simpler-is-better here.
  893. */
  894. if (composite->strings) {
  895. len = lookup_string(composite->strings, buf, language, id);
  896. if (len > 0)
  897. return len;
  898. }
  899. list_for_each_entry(c, &cdev->configs, list) {
  900. if (c->strings) {
  901. len = lookup_string(c->strings, buf, language, id);
  902. if (len > 0)
  903. return len;
  904. }
  905. list_for_each_entry(f, &c->functions, list) {
  906. if (!f->strings)
  907. continue;
  908. len = lookup_string(f->strings, buf, language, id);
  909. if (len > 0)
  910. return len;
  911. }
  912. }
  913. return -EINVAL;
  914. }
  915. /**
  916. * usb_string_id() - allocate an unused string ID
  917. * @cdev: the device whose string descriptor IDs are being allocated
  918. * Context: single threaded during gadget setup
  919. *
  920. * @usb_string_id() is called from bind() callbacks to allocate
  921. * string IDs. Drivers for functions, configurations, or gadgets will
  922. * then store that ID in the appropriate descriptors and string table.
  923. *
  924. * All string identifier should be allocated using this,
  925. * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
  926. * that for example different functions don't wrongly assign different
  927. * meanings to the same identifier.
  928. */
  929. int usb_string_id(struct usb_composite_dev *cdev)
  930. {
  931. if (cdev->next_string_id < 254) {
  932. /* string id 0 is reserved by USB spec for list of
  933. * supported languages */
  934. /* 255 reserved as well? -- mina86 */
  935. cdev->next_string_id++;
  936. return cdev->next_string_id;
  937. }
  938. return -ENODEV;
  939. }
  940. EXPORT_SYMBOL_GPL(usb_string_id);
  941. /**
  942. * usb_string_ids() - allocate unused string IDs in batch
  943. * @cdev: the device whose string descriptor IDs are being allocated
  944. * @str: an array of usb_string objects to assign numbers to
  945. * Context: single threaded during gadget setup
  946. *
  947. * @usb_string_ids() is called from bind() callbacks to allocate
  948. * string IDs. Drivers for functions, configurations, or gadgets will
  949. * then copy IDs from the string table to the appropriate descriptors
  950. * and string table for other languages.
  951. *
  952. * All string identifier should be allocated using this,
  953. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  954. * example different functions don't wrongly assign different meanings
  955. * to the same identifier.
  956. */
  957. int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
  958. {
  959. int next = cdev->next_string_id;
  960. for (; str->s; ++str) {
  961. if (unlikely(next >= 254))
  962. return -ENODEV;
  963. str->id = ++next;
  964. }
  965. cdev->next_string_id = next;
  966. return 0;
  967. }
  968. EXPORT_SYMBOL_GPL(usb_string_ids_tab);
  969. static struct usb_gadget_string_container *copy_gadget_strings(
  970. struct usb_gadget_strings **sp, unsigned n_gstrings,
  971. unsigned n_strings)
  972. {
  973. struct usb_gadget_string_container *uc;
  974. struct usb_gadget_strings **gs_array;
  975. struct usb_gadget_strings *gs;
  976. struct usb_string *s;
  977. unsigned mem;
  978. unsigned n_gs;
  979. unsigned n_s;
  980. void *stash;
  981. mem = sizeof(*uc);
  982. mem += sizeof(void *) * (n_gstrings + 1);
  983. mem += sizeof(struct usb_gadget_strings) * n_gstrings;
  984. mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
  985. uc = kmalloc(mem, GFP_KERNEL);
  986. if (!uc)
  987. return ERR_PTR(-ENOMEM);
  988. gs_array = get_containers_gs(uc);
  989. stash = uc->stash;
  990. stash += sizeof(void *) * (n_gstrings + 1);
  991. for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
  992. struct usb_string *org_s;
  993. gs_array[n_gs] = stash;
  994. gs = gs_array[n_gs];
  995. stash += sizeof(struct usb_gadget_strings);
  996. gs->language = sp[n_gs]->language;
  997. gs->strings = stash;
  998. org_s = sp[n_gs]->strings;
  999. for (n_s = 0; n_s < n_strings; n_s++) {
  1000. s = stash;
  1001. stash += sizeof(struct usb_string);
  1002. if (org_s->s)
  1003. s->s = org_s->s;
  1004. else
  1005. s->s = "";
  1006. org_s++;
  1007. }
  1008. s = stash;
  1009. s->s = NULL;
  1010. stash += sizeof(struct usb_string);
  1011. }
  1012. gs_array[n_gs] = NULL;
  1013. return uc;
  1014. }
  1015. /**
  1016. * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
  1017. * @cdev: the device whose string descriptor IDs are being allocated
  1018. * and attached.
  1019. * @sp: an array of usb_gadget_strings to attach.
  1020. * @n_strings: number of entries in each usb_strings array (sp[]->strings)
  1021. *
  1022. * This function will create a deep copy of usb_gadget_strings and usb_string
  1023. * and attach it to the cdev. The actual string (usb_string.s) will not be
  1024. * copied but only a referenced will be made. The struct usb_gadget_strings
  1025. * array may contain multiple languges and should be NULL terminated.
  1026. * The ->language pointer of each struct usb_gadget_strings has to contain the
  1027. * same amount of entries.
  1028. * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
  1029. * usb_string entry of es-ES containts the translation of the first usb_string
  1030. * entry of en-US. Therefore both entries become the same id assign.
  1031. */
  1032. struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
  1033. struct usb_gadget_strings **sp, unsigned n_strings)
  1034. {
  1035. struct usb_gadget_string_container *uc;
  1036. struct usb_gadget_strings **n_gs;
  1037. unsigned n_gstrings = 0;
  1038. unsigned i;
  1039. int ret;
  1040. for (i = 0; sp[i]; i++)
  1041. n_gstrings++;
  1042. if (!n_gstrings)
  1043. return ERR_PTR(-EINVAL);
  1044. uc = copy_gadget_strings(sp, n_gstrings, n_strings);
  1045. if (IS_ERR(uc))
  1046. return ERR_CAST(uc);
  1047. n_gs = get_containers_gs(uc);
  1048. ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
  1049. if (ret)
  1050. goto err;
  1051. for (i = 1; i < n_gstrings; i++) {
  1052. struct usb_string *m_s;
  1053. struct usb_string *s;
  1054. unsigned n;
  1055. m_s = n_gs[0]->strings;
  1056. s = n_gs[i]->strings;
  1057. for (n = 0; n < n_strings; n++) {
  1058. s->id = m_s->id;
  1059. s++;
  1060. m_s++;
  1061. }
  1062. }
  1063. list_add_tail(&uc->list, &cdev->gstrings);
  1064. return n_gs[0]->strings;
  1065. err:
  1066. kfree(uc);
  1067. return ERR_PTR(ret);
  1068. }
  1069. EXPORT_SYMBOL_GPL(usb_gstrings_attach);
  1070. /**
  1071. * usb_string_ids_n() - allocate unused string IDs in batch
  1072. * @c: the device whose string descriptor IDs are being allocated
  1073. * @n: number of string IDs to allocate
  1074. * Context: single threaded during gadget setup
  1075. *
  1076. * Returns the first requested ID. This ID and next @n-1 IDs are now
  1077. * valid IDs. At least provided that @n is non-zero because if it
  1078. * is, returns last requested ID which is now very useful information.
  1079. *
  1080. * @usb_string_ids_n() is called from bind() callbacks to allocate
  1081. * string IDs. Drivers for functions, configurations, or gadgets will
  1082. * then store that ID in the appropriate descriptors and string table.
  1083. *
  1084. * All string identifier should be allocated using this,
  1085. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  1086. * example different functions don't wrongly assign different meanings
  1087. * to the same identifier.
  1088. */
  1089. int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
  1090. {
  1091. unsigned next = c->next_string_id;
  1092. if (unlikely(n > 254 || (unsigned)next + n > 254))
  1093. return -ENODEV;
  1094. c->next_string_id += n;
  1095. return next + 1;
  1096. }
  1097. EXPORT_SYMBOL_GPL(usb_string_ids_n);
  1098. /*-------------------------------------------------------------------------*/
  1099. static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
  1100. {
  1101. struct usb_composite_dev *cdev;
  1102. if (req->status || req->actual != req->length)
  1103. DBG((struct usb_composite_dev *) ep->driver_data,
  1104. "setup complete --> %d, %d/%d\n",
  1105. req->status, req->actual, req->length);
  1106. /*
  1107. * REVIST The same ep0 requests are shared with function drivers
  1108. * so they don't have to maintain the same ->complete() stubs.
  1109. *
  1110. * Because of that, we need to check for the validity of ->context
  1111. * here, even though we know we've set it to something useful.
  1112. */
  1113. if (!req->context)
  1114. return;
  1115. cdev = req->context;
  1116. if (cdev->req == req)
  1117. cdev->setup_pending = false;
  1118. else if (cdev->os_desc_req == req)
  1119. cdev->os_desc_pending = false;
  1120. else
  1121. WARN(1, "unknown request %p\n", req);
  1122. }
  1123. static int composite_ep0_queue(struct usb_composite_dev *cdev,
  1124. struct usb_request *req, gfp_t gfp_flags)
  1125. {
  1126. int ret;
  1127. ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
  1128. if (ret == 0) {
  1129. if (cdev->req == req)
  1130. cdev->setup_pending = true;
  1131. else if (cdev->os_desc_req == req)
  1132. cdev->os_desc_pending = true;
  1133. else
  1134. WARN(1, "unknown request %p\n", req);
  1135. }
  1136. return ret;
  1137. }
  1138. static int count_ext_compat(struct usb_configuration *c)
  1139. {
  1140. int i, res;
  1141. res = 0;
  1142. for (i = 0; i < c->next_interface_id; ++i) {
  1143. struct usb_function *f;
  1144. int j;
  1145. f = c->interface[i];
  1146. for (j = 0; j < f->os_desc_n; ++j) {
  1147. struct usb_os_desc *d;
  1148. if (i != f->os_desc_table[j].if_id)
  1149. continue;
  1150. d = f->os_desc_table[j].os_desc;
  1151. if (d && d->ext_compat_id)
  1152. ++res;
  1153. }
  1154. }
  1155. BUG_ON(res > 255);
  1156. return res;
  1157. }
  1158. static void fill_ext_compat(struct usb_configuration *c, u8 *buf)
  1159. {
  1160. int i, count;
  1161. count = 16;
  1162. for (i = 0; i < c->next_interface_id; ++i) {
  1163. struct usb_function *f;
  1164. int j;
  1165. f = c->interface[i];
  1166. for (j = 0; j < f->os_desc_n; ++j) {
  1167. struct usb_os_desc *d;
  1168. if (i != f->os_desc_table[j].if_id)
  1169. continue;
  1170. d = f->os_desc_table[j].os_desc;
  1171. if (d && d->ext_compat_id) {
  1172. *buf++ = i;
  1173. *buf++ = 0x01;
  1174. memcpy(buf, d->ext_compat_id, 16);
  1175. buf += 22;
  1176. } else {
  1177. ++buf;
  1178. *buf = 0x01;
  1179. buf += 23;
  1180. }
  1181. count += 24;
  1182. if (count >= 4096)
  1183. return;
  1184. }
  1185. }
  1186. }
  1187. static int count_ext_prop(struct usb_configuration *c, int interface)
  1188. {
  1189. struct usb_function *f;
  1190. int j;
  1191. f = c->interface[interface];
  1192. for (j = 0; j < f->os_desc_n; ++j) {
  1193. struct usb_os_desc *d;
  1194. if (interface != f->os_desc_table[j].if_id)
  1195. continue;
  1196. d = f->os_desc_table[j].os_desc;
  1197. if (d && d->ext_compat_id)
  1198. return d->ext_prop_count;
  1199. }
  1200. return 0;
  1201. }
  1202. static int len_ext_prop(struct usb_configuration *c, int interface)
  1203. {
  1204. struct usb_function *f;
  1205. struct usb_os_desc *d;
  1206. int j, res;
  1207. res = 10; /* header length */
  1208. f = c->interface[interface];
  1209. for (j = 0; j < f->os_desc_n; ++j) {
  1210. if (interface != f->os_desc_table[j].if_id)
  1211. continue;
  1212. d = f->os_desc_table[j].os_desc;
  1213. if (d)
  1214. return min(res + d->ext_prop_len, 4096);
  1215. }
  1216. return res;
  1217. }
  1218. static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
  1219. {
  1220. struct usb_function *f;
  1221. struct usb_os_desc *d;
  1222. struct usb_os_desc_ext_prop *ext_prop;
  1223. int j, count, n, ret;
  1224. u8 *start = buf;
  1225. f = c->interface[interface];
  1226. for (j = 0; j < f->os_desc_n; ++j) {
  1227. if (interface != f->os_desc_table[j].if_id)
  1228. continue;
  1229. d = f->os_desc_table[j].os_desc;
  1230. if (d)
  1231. list_for_each_entry(ext_prop, &d->ext_prop, entry) {
  1232. /* 4kB minus header length */
  1233. n = buf - start;
  1234. if (n >= 4086)
  1235. return 0;
  1236. count = ext_prop->data_len +
  1237. ext_prop->name_len + 14;
  1238. if (count > 4086 - n)
  1239. return -EINVAL;
  1240. usb_ext_prop_put_size(buf, count);
  1241. usb_ext_prop_put_type(buf, ext_prop->type);
  1242. ret = usb_ext_prop_put_name(buf, ext_prop->name,
  1243. ext_prop->name_len);
  1244. if (ret < 0)
  1245. return ret;
  1246. switch (ext_prop->type) {
  1247. case USB_EXT_PROP_UNICODE:
  1248. case USB_EXT_PROP_UNICODE_ENV:
  1249. case USB_EXT_PROP_UNICODE_LINK:
  1250. usb_ext_prop_put_unicode(buf, ret,
  1251. ext_prop->data,
  1252. ext_prop->data_len);
  1253. break;
  1254. case USB_EXT_PROP_BINARY:
  1255. usb_ext_prop_put_binary(buf, ret,
  1256. ext_prop->data,
  1257. ext_prop->data_len);
  1258. break;
  1259. case USB_EXT_PROP_LE32:
  1260. /* not implemented */
  1261. case USB_EXT_PROP_BE32:
  1262. /* not implemented */
  1263. default:
  1264. return -EINVAL;
  1265. }
  1266. buf += count;
  1267. }
  1268. }
  1269. return 0;
  1270. }
  1271. /*
  1272. * The setup() callback implements all the ep0 functionality that's
  1273. * not handled lower down, in hardware or the hardware driver(like
  1274. * device and endpoint feature flags, and their status). It's all
  1275. * housekeeping for the gadget function we're implementing. Most of
  1276. * the work is in config and function specific setup.
  1277. */
  1278. int
  1279. composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  1280. {
  1281. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1282. struct usb_request *req = cdev->req;
  1283. int value = -EOPNOTSUPP;
  1284. int status = 0;
  1285. u16 w_index = le16_to_cpu(ctrl->wIndex);
  1286. u8 intf = w_index & 0xFF;
  1287. u16 w_value = le16_to_cpu(ctrl->wValue);
  1288. u16 w_length = le16_to_cpu(ctrl->wLength);
  1289. struct usb_function *f = NULL;
  1290. u8 endp;
  1291. /* partial re-init of the response message; the function or the
  1292. * gadget might need to intercept e.g. a control-OUT completion
  1293. * when we delegate to it.
  1294. */
  1295. req->zero = 0;
  1296. req->context = cdev;
  1297. req->complete = composite_setup_complete;
  1298. req->length = 0;
  1299. gadget->ep0->driver_data = cdev;
  1300. switch (ctrl->bRequest) {
  1301. /* we handle all standard USB descriptors */
  1302. case USB_REQ_GET_DESCRIPTOR:
  1303. if (ctrl->bRequestType != USB_DIR_IN)
  1304. goto unknown;
  1305. switch (w_value >> 8) {
  1306. case USB_DT_DEVICE:
  1307. cdev->desc.bNumConfigurations =
  1308. count_configs(cdev, USB_DT_DEVICE);
  1309. cdev->desc.bMaxPacketSize0 =
  1310. cdev->gadget->ep0->maxpacket;
  1311. if (gadget_is_superspeed(gadget)) {
  1312. if (gadget->speed >= USB_SPEED_SUPER) {
  1313. cdev->desc.bcdUSB = cpu_to_le16(0x0300);
  1314. cdev->desc.bMaxPacketSize0 = 9;
  1315. } else {
  1316. cdev->desc.bcdUSB = cpu_to_le16(0x0210);
  1317. }
  1318. }
  1319. value = min(w_length, (u16) sizeof cdev->desc);
  1320. memcpy(req->buf, &cdev->desc, value);
  1321. break;
  1322. case USB_DT_DEVICE_QUALIFIER:
  1323. if (!gadget_is_dualspeed(gadget) ||
  1324. gadget->speed >= USB_SPEED_SUPER)
  1325. break;
  1326. device_qual(cdev);
  1327. value = min_t(int, w_length,
  1328. sizeof(struct usb_qualifier_descriptor));
  1329. break;
  1330. case USB_DT_OTHER_SPEED_CONFIG:
  1331. if (!gadget_is_dualspeed(gadget) ||
  1332. gadget->speed >= USB_SPEED_SUPER)
  1333. break;
  1334. /* FALLTHROUGH */
  1335. case USB_DT_CONFIG:
  1336. value = config_desc(cdev, w_value);
  1337. if (value >= 0)
  1338. value = min(w_length, (u16) value);
  1339. break;
  1340. case USB_DT_STRING:
  1341. value = get_string(cdev, req->buf,
  1342. w_index, w_value & 0xff);
  1343. if (value >= 0)
  1344. value = min(w_length, (u16) value);
  1345. break;
  1346. case USB_DT_BOS:
  1347. if (gadget_is_superspeed(gadget)) {
  1348. value = bos_desc(cdev);
  1349. value = min(w_length, (u16) value);
  1350. }
  1351. break;
  1352. }
  1353. break;
  1354. /* any number of configs can work */
  1355. case USB_REQ_SET_CONFIGURATION:
  1356. if (ctrl->bRequestType != 0)
  1357. goto unknown;
  1358. if (gadget_is_otg(gadget)) {
  1359. if (gadget->a_hnp_support)
  1360. DBG(cdev, "HNP available\n");
  1361. else if (gadget->a_alt_hnp_support)
  1362. DBG(cdev, "HNP on another port\n");
  1363. else
  1364. VDBG(cdev, "HNP inactive\n");
  1365. }
  1366. spin_lock(&cdev->lock);
  1367. value = set_config(cdev, ctrl, w_value);
  1368. spin_unlock(&cdev->lock);
  1369. break;
  1370. case USB_REQ_GET_CONFIGURATION:
  1371. if (ctrl->bRequestType != USB_DIR_IN)
  1372. goto unknown;
  1373. if (cdev->config)
  1374. *(u8 *)req->buf = cdev->config->bConfigurationValue;
  1375. else
  1376. *(u8 *)req->buf = 0;
  1377. value = min(w_length, (u16) 1);
  1378. break;
  1379. /* function drivers must handle get/set altsetting; if there's
  1380. * no get() method, we know only altsetting zero works.
  1381. */
  1382. case USB_REQ_SET_INTERFACE:
  1383. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  1384. goto unknown;
  1385. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1386. break;
  1387. f = cdev->config->interface[intf];
  1388. if (!f)
  1389. break;
  1390. if (w_value && !f->set_alt)
  1391. break;
  1392. value = f->set_alt(f, w_index, w_value);
  1393. if (value == USB_GADGET_DELAYED_STATUS) {
  1394. DBG(cdev,
  1395. "%s: interface %d (%s) requested delayed status\n",
  1396. __func__, intf, f->name);
  1397. cdev->delayed_status++;
  1398. DBG(cdev, "delayed_status count %d\n",
  1399. cdev->delayed_status);
  1400. }
  1401. break;
  1402. case USB_REQ_GET_INTERFACE:
  1403. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  1404. goto unknown;
  1405. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1406. break;
  1407. f = cdev->config->interface[intf];
  1408. if (!f)
  1409. break;
  1410. /* lots of interfaces only need altsetting zero... */
  1411. value = f->get_alt ? f->get_alt(f, w_index) : 0;
  1412. if (value < 0)
  1413. break;
  1414. *((u8 *)req->buf) = value;
  1415. value = min(w_length, (u16) 1);
  1416. break;
  1417. /*
  1418. * USB 3.0 additions:
  1419. * Function driver should handle get_status request. If such cb
  1420. * wasn't supplied we respond with default value = 0
  1421. * Note: function driver should supply such cb only for the first
  1422. * interface of the function
  1423. */
  1424. case USB_REQ_GET_STATUS:
  1425. if (!gadget_is_superspeed(gadget))
  1426. goto unknown;
  1427. if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
  1428. goto unknown;
  1429. value = 2; /* This is the length of the get_status reply */
  1430. put_unaligned_le16(0, req->buf);
  1431. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1432. break;
  1433. f = cdev->config->interface[intf];
  1434. if (!f)
  1435. break;
  1436. status = f->get_status ? f->get_status(f) : 0;
  1437. if (status < 0)
  1438. break;
  1439. put_unaligned_le16(status & 0x0000ffff, req->buf);
  1440. break;
  1441. /*
  1442. * Function drivers should handle SetFeature/ClearFeature
  1443. * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
  1444. * only for the first interface of the function
  1445. */
  1446. case USB_REQ_CLEAR_FEATURE:
  1447. case USB_REQ_SET_FEATURE:
  1448. if (!gadget_is_superspeed(gadget))
  1449. goto unknown;
  1450. if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
  1451. goto unknown;
  1452. switch (w_value) {
  1453. case USB_INTRF_FUNC_SUSPEND:
  1454. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1455. break;
  1456. f = cdev->config->interface[intf];
  1457. if (!f)
  1458. break;
  1459. value = 0;
  1460. if (f->func_suspend)
  1461. value = f->func_suspend(f, w_index >> 8);
  1462. if (value < 0) {
  1463. ERROR(cdev,
  1464. "func_suspend() returned error %d\n",
  1465. value);
  1466. value = 0;
  1467. }
  1468. break;
  1469. }
  1470. break;
  1471. default:
  1472. unknown:
  1473. /*
  1474. * OS descriptors handling
  1475. */
  1476. if (cdev->use_os_string && cdev->os_desc_config &&
  1477. (ctrl->bRequestType & USB_TYPE_VENDOR) &&
  1478. ctrl->bRequest == cdev->b_vendor_code) {
  1479. struct usb_request *req;
  1480. struct usb_configuration *os_desc_cfg;
  1481. u8 *buf;
  1482. int interface;
  1483. int count = 0;
  1484. req = cdev->os_desc_req;
  1485. req->context = cdev;
  1486. req->complete = composite_setup_complete;
  1487. buf = req->buf;
  1488. os_desc_cfg = cdev->os_desc_config;
  1489. memset(buf, 0, w_length);
  1490. buf[5] = 0x01;
  1491. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1492. case USB_RECIP_DEVICE:
  1493. if (w_index != 0x4 || (w_value >> 8))
  1494. break;
  1495. buf[6] = w_index;
  1496. if (w_length == 0x10) {
  1497. /* Number of ext compat interfaces */
  1498. count = count_ext_compat(os_desc_cfg);
  1499. buf[8] = count;
  1500. count *= 24; /* 24 B/ext compat desc */
  1501. count += 16; /* header */
  1502. put_unaligned_le32(count, buf);
  1503. value = w_length;
  1504. } else {
  1505. /* "extended compatibility ID"s */
  1506. count = count_ext_compat(os_desc_cfg);
  1507. buf[8] = count;
  1508. count *= 24; /* 24 B/ext compat desc */
  1509. count += 16; /* header */
  1510. put_unaligned_le32(count, buf);
  1511. buf += 16;
  1512. fill_ext_compat(os_desc_cfg, buf);
  1513. value = w_length;
  1514. }
  1515. break;
  1516. case USB_RECIP_INTERFACE:
  1517. if (w_index != 0x5 || (w_value >> 8))
  1518. break;
  1519. interface = w_value & 0xFF;
  1520. buf[6] = w_index;
  1521. if (w_length == 0x0A) {
  1522. count = count_ext_prop(os_desc_cfg,
  1523. interface);
  1524. put_unaligned_le16(count, buf + 8);
  1525. count = len_ext_prop(os_desc_cfg,
  1526. interface);
  1527. put_unaligned_le32(count, buf);
  1528. value = w_length;
  1529. } else {
  1530. count = count_ext_prop(os_desc_cfg,
  1531. interface);
  1532. put_unaligned_le16(count, buf + 8);
  1533. count = len_ext_prop(os_desc_cfg,
  1534. interface);
  1535. put_unaligned_le32(count, buf);
  1536. buf += 10;
  1537. value = fill_ext_prop(os_desc_cfg,
  1538. interface, buf);
  1539. if (value < 0)
  1540. return value;
  1541. value = w_length;
  1542. }
  1543. break;
  1544. }
  1545. req->length = value;
  1546. req->context = cdev;
  1547. req->zero = value < w_length;
  1548. value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
  1549. if (value < 0) {
  1550. DBG(cdev, "ep_queue --> %d\n", value);
  1551. req->status = 0;
  1552. composite_setup_complete(gadget->ep0, req);
  1553. }
  1554. return value;
  1555. }
  1556. VDBG(cdev,
  1557. "non-core control req%02x.%02x v%04x i%04x l%d\n",
  1558. ctrl->bRequestType, ctrl->bRequest,
  1559. w_value, w_index, w_length);
  1560. /* functions always handle their interfaces and endpoints...
  1561. * punt other recipients (other, WUSB, ...) to the current
  1562. * configuration code.
  1563. *
  1564. * REVISIT it could make sense to let the composite device
  1565. * take such requests too, if that's ever needed: to work
  1566. * in config 0, etc.
  1567. */
  1568. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1569. case USB_RECIP_INTERFACE:
  1570. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1571. break;
  1572. f = cdev->config->interface[intf];
  1573. break;
  1574. case USB_RECIP_ENDPOINT:
  1575. endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
  1576. list_for_each_entry(f, &cdev->config->functions, list) {
  1577. if (test_bit(endp, f->endpoints))
  1578. break;
  1579. }
  1580. if (&f->list == &cdev->config->functions)
  1581. f = NULL;
  1582. break;
  1583. }
  1584. if (f && f->setup)
  1585. value = f->setup(f, ctrl);
  1586. else {
  1587. struct usb_configuration *c;
  1588. c = cdev->config;
  1589. if (!c)
  1590. goto done;
  1591. /* try current config's setup */
  1592. if (c->setup) {
  1593. value = c->setup(c, ctrl);
  1594. goto done;
  1595. }
  1596. /* try the only function in the current config */
  1597. if (!list_is_singular(&c->functions))
  1598. goto done;
  1599. f = list_first_entry(&c->functions, struct usb_function,
  1600. list);
  1601. if (f->setup)
  1602. value = f->setup(f, ctrl);
  1603. }
  1604. goto done;
  1605. }
  1606. /* respond with data transfer before status phase? */
  1607. if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
  1608. req->length = value;
  1609. req->context = cdev;
  1610. req->zero = value < w_length;
  1611. value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
  1612. if (value < 0) {
  1613. DBG(cdev, "ep_queue --> %d\n", value);
  1614. req->status = 0;
  1615. composite_setup_complete(gadget->ep0, req);
  1616. }
  1617. } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
  1618. WARN(cdev,
  1619. "%s: Delayed status not supported for w_length != 0",
  1620. __func__);
  1621. }
  1622. done:
  1623. /* device either stalls (value < 0) or reports success */
  1624. return value;
  1625. }
  1626. void composite_disconnect(struct usb_gadget *gadget)
  1627. {
  1628. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1629. unsigned long flags;
  1630. /* REVISIT: should we have config and device level
  1631. * disconnect callbacks?
  1632. */
  1633. spin_lock_irqsave(&cdev->lock, flags);
  1634. if (cdev->config)
  1635. reset_config(cdev);
  1636. if (cdev->driver->disconnect)
  1637. cdev->driver->disconnect(cdev);
  1638. spin_unlock_irqrestore(&cdev->lock, flags);
  1639. }
  1640. /*-------------------------------------------------------------------------*/
  1641. static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
  1642. char *buf)
  1643. {
  1644. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  1645. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1646. return sprintf(buf, "%d\n", cdev->suspended);
  1647. }
  1648. static DEVICE_ATTR_RO(suspended);
  1649. static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
  1650. {
  1651. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1652. /* composite_disconnect() must already have been called
  1653. * by the underlying peripheral controller driver!
  1654. * so there's no i/o concurrency that could affect the
  1655. * state protected by cdev->lock.
  1656. */
  1657. WARN_ON(cdev->config);
  1658. while (!list_empty(&cdev->configs)) {
  1659. struct usb_configuration *c;
  1660. c = list_first_entry(&cdev->configs,
  1661. struct usb_configuration, list);
  1662. remove_config(cdev, c);
  1663. }
  1664. if (cdev->driver->unbind && unbind_driver)
  1665. cdev->driver->unbind(cdev);
  1666. composite_dev_cleanup(cdev);
  1667. kfree(cdev->def_manufacturer);
  1668. kfree(cdev);
  1669. set_gadget_data(gadget, NULL);
  1670. }
  1671. static void composite_unbind(struct usb_gadget *gadget)
  1672. {
  1673. __composite_unbind(gadget, true);
  1674. }
  1675. static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
  1676. const struct usb_device_descriptor *old)
  1677. {
  1678. __le16 idVendor;
  1679. __le16 idProduct;
  1680. __le16 bcdDevice;
  1681. u8 iSerialNumber;
  1682. u8 iManufacturer;
  1683. u8 iProduct;
  1684. /*
  1685. * these variables may have been set in
  1686. * usb_composite_overwrite_options()
  1687. */
  1688. idVendor = new->idVendor;
  1689. idProduct = new->idProduct;
  1690. bcdDevice = new->bcdDevice;
  1691. iSerialNumber = new->iSerialNumber;
  1692. iManufacturer = new->iManufacturer;
  1693. iProduct = new->iProduct;
  1694. *new = *old;
  1695. if (idVendor)
  1696. new->idVendor = idVendor;
  1697. if (idProduct)
  1698. new->idProduct = idProduct;
  1699. if (bcdDevice)
  1700. new->bcdDevice = bcdDevice;
  1701. else
  1702. new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
  1703. if (iSerialNumber)
  1704. new->iSerialNumber = iSerialNumber;
  1705. if (iManufacturer)
  1706. new->iManufacturer = iManufacturer;
  1707. if (iProduct)
  1708. new->iProduct = iProduct;
  1709. }
  1710. int composite_dev_prepare(struct usb_composite_driver *composite,
  1711. struct usb_composite_dev *cdev)
  1712. {
  1713. struct usb_gadget *gadget = cdev->gadget;
  1714. int ret = -ENOMEM;
  1715. /* preallocate control response and buffer */
  1716. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  1717. if (!cdev->req)
  1718. return -ENOMEM;
  1719. cdev->req->buf = kmalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
  1720. if (!cdev->req->buf)
  1721. goto fail;
  1722. ret = device_create_file(&gadget->dev, &dev_attr_suspended);
  1723. if (ret)
  1724. goto fail_dev;
  1725. cdev->req->complete = composite_setup_complete;
  1726. cdev->req->context = cdev;
  1727. gadget->ep0->driver_data = cdev;
  1728. cdev->driver = composite;
  1729. /*
  1730. * As per USB compliance update, a device that is actively drawing
  1731. * more than 100mA from USB must report itself as bus-powered in
  1732. * the GetStatus(DEVICE) call.
  1733. */
  1734. if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
  1735. usb_gadget_set_selfpowered(gadget);
  1736. /* interface and string IDs start at zero via kzalloc.
  1737. * we force endpoints to start unassigned; few controller
  1738. * drivers will zero ep->driver_data.
  1739. */
  1740. usb_ep_autoconfig_reset(gadget);
  1741. return 0;
  1742. fail_dev:
  1743. kfree(cdev->req->buf);
  1744. fail:
  1745. usb_ep_free_request(gadget->ep0, cdev->req);
  1746. cdev->req = NULL;
  1747. return ret;
  1748. }
  1749. int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
  1750. struct usb_ep *ep0)
  1751. {
  1752. int ret = 0;
  1753. cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
  1754. if (!cdev->os_desc_req) {
  1755. ret = PTR_ERR(cdev->os_desc_req);
  1756. goto end;
  1757. }
  1758. /* OS feature descriptor length <= 4kB */
  1759. cdev->os_desc_req->buf = kmalloc(4096, GFP_KERNEL);
  1760. if (!cdev->os_desc_req->buf) {
  1761. ret = PTR_ERR(cdev->os_desc_req->buf);
  1762. kfree(cdev->os_desc_req);
  1763. goto end;
  1764. }
  1765. cdev->os_desc_req->context = cdev;
  1766. cdev->os_desc_req->complete = composite_setup_complete;
  1767. end:
  1768. return ret;
  1769. }
  1770. void composite_dev_cleanup(struct usb_composite_dev *cdev)
  1771. {
  1772. struct usb_gadget_string_container *uc, *tmp;
  1773. list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
  1774. list_del(&uc->list);
  1775. kfree(uc);
  1776. }
  1777. if (cdev->os_desc_req) {
  1778. if (cdev->os_desc_pending)
  1779. usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
  1780. kfree(cdev->os_desc_req->buf);
  1781. usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
  1782. }
  1783. if (cdev->req) {
  1784. if (cdev->setup_pending)
  1785. usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
  1786. kfree(cdev->req->buf);
  1787. usb_ep_free_request(cdev->gadget->ep0, cdev->req);
  1788. }
  1789. cdev->next_string_id = 0;
  1790. device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
  1791. }
  1792. static int composite_bind(struct usb_gadget *gadget,
  1793. struct usb_gadget_driver *gdriver)
  1794. {
  1795. struct usb_composite_dev *cdev;
  1796. struct usb_composite_driver *composite = to_cdriver(gdriver);
  1797. int status = -ENOMEM;
  1798. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  1799. if (!cdev)
  1800. return status;
  1801. spin_lock_init(&cdev->lock);
  1802. cdev->gadget = gadget;
  1803. set_gadget_data(gadget, cdev);
  1804. INIT_LIST_HEAD(&cdev->configs);
  1805. INIT_LIST_HEAD(&cdev->gstrings);
  1806. status = composite_dev_prepare(composite, cdev);
  1807. if (status)
  1808. goto fail;
  1809. /* composite gadget needs to assign strings for whole device (like
  1810. * serial number), register function drivers, potentially update
  1811. * power state and consumption, etc
  1812. */
  1813. status = composite->bind(cdev);
  1814. if (status < 0)
  1815. goto fail;
  1816. if (cdev->use_os_string) {
  1817. status = composite_os_desc_req_prepare(cdev, gadget->ep0);
  1818. if (status)
  1819. goto fail;
  1820. }
  1821. update_unchanged_dev_desc(&cdev->desc, composite->dev);
  1822. /* has userspace failed to provide a serial number? */
  1823. if (composite->needs_serial && !cdev->desc.iSerialNumber)
  1824. WARNING(cdev, "userspace failed to provide iSerialNumber\n");
  1825. INFO(cdev, "%s ready\n", composite->name);
  1826. return 0;
  1827. fail:
  1828. __composite_unbind(gadget, false);
  1829. return status;
  1830. }
  1831. /*-------------------------------------------------------------------------*/
  1832. void composite_suspend(struct usb_gadget *gadget)
  1833. {
  1834. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1835. struct usb_function *f;
  1836. /* REVISIT: should we have config level
  1837. * suspend/resume callbacks?
  1838. */
  1839. DBG(cdev, "suspend\n");
  1840. if (cdev->config) {
  1841. list_for_each_entry(f, &cdev->config->functions, list) {
  1842. if (f->suspend)
  1843. f->suspend(f);
  1844. }
  1845. }
  1846. if (cdev->driver->suspend)
  1847. cdev->driver->suspend(cdev);
  1848. cdev->suspended = 1;
  1849. usb_gadget_vbus_draw(gadget, 2);
  1850. }
  1851. void composite_resume(struct usb_gadget *gadget)
  1852. {
  1853. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1854. struct usb_function *f;
  1855. u16 maxpower;
  1856. /* REVISIT: should we have config level
  1857. * suspend/resume callbacks?
  1858. */
  1859. DBG(cdev, "resume\n");
  1860. if (cdev->driver->resume)
  1861. cdev->driver->resume(cdev);
  1862. if (cdev->config) {
  1863. list_for_each_entry(f, &cdev->config->functions, list) {
  1864. if (f->resume)
  1865. f->resume(f);
  1866. }
  1867. maxpower = cdev->config->MaxPower;
  1868. usb_gadget_vbus_draw(gadget, maxpower ?
  1869. maxpower : CONFIG_USB_GADGET_VBUS_DRAW);
  1870. }
  1871. cdev->suspended = 0;
  1872. }
  1873. /*-------------------------------------------------------------------------*/
  1874. static const struct usb_gadget_driver composite_driver_template = {
  1875. .bind = composite_bind,
  1876. .unbind = composite_unbind,
  1877. .setup = composite_setup,
  1878. .reset = composite_disconnect,
  1879. .disconnect = composite_disconnect,
  1880. .suspend = composite_suspend,
  1881. .resume = composite_resume,
  1882. .driver = {
  1883. .owner = THIS_MODULE,
  1884. },
  1885. };
  1886. /**
  1887. * usb_composite_probe() - register a composite driver
  1888. * @driver: the driver to register
  1889. *
  1890. * Context: single threaded during gadget setup
  1891. *
  1892. * This function is used to register drivers using the composite driver
  1893. * framework. The return value is zero, or a negative errno value.
  1894. * Those values normally come from the driver's @bind method, which does
  1895. * all the work of setting up the driver to match the hardware.
  1896. *
  1897. * On successful return, the gadget is ready to respond to requests from
  1898. * the host, unless one of its components invokes usb_gadget_disconnect()
  1899. * while it was binding. That would usually be done in order to wait for
  1900. * some userspace participation.
  1901. */
  1902. int usb_composite_probe(struct usb_composite_driver *driver)
  1903. {
  1904. struct usb_gadget_driver *gadget_driver;
  1905. if (!driver || !driver->dev || !driver->bind)
  1906. return -EINVAL;
  1907. if (!driver->name)
  1908. driver->name = "composite";
  1909. driver->gadget_driver = composite_driver_template;
  1910. gadget_driver = &driver->gadget_driver;
  1911. gadget_driver->function = (char *) driver->name;
  1912. gadget_driver->driver.name = driver->name;
  1913. gadget_driver->max_speed = driver->max_speed;
  1914. return usb_gadget_probe_driver(gadget_driver);
  1915. }
  1916. EXPORT_SYMBOL_GPL(usb_composite_probe);
  1917. /**
  1918. * usb_composite_unregister() - unregister a composite driver
  1919. * @driver: the driver to unregister
  1920. *
  1921. * This function is used to unregister drivers using the composite
  1922. * driver framework.
  1923. */
  1924. void usb_composite_unregister(struct usb_composite_driver *driver)
  1925. {
  1926. usb_gadget_unregister_driver(&driver->gadget_driver);
  1927. }
  1928. EXPORT_SYMBOL_GPL(usb_composite_unregister);
  1929. /**
  1930. * usb_composite_setup_continue() - Continue with the control transfer
  1931. * @cdev: the composite device who's control transfer was kept waiting
  1932. *
  1933. * This function must be called by the USB function driver to continue
  1934. * with the control transfer's data/status stage in case it had requested to
  1935. * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
  1936. * can request the composite framework to delay the setup request's data/status
  1937. * stages by returning USB_GADGET_DELAYED_STATUS.
  1938. */
  1939. void usb_composite_setup_continue(struct usb_composite_dev *cdev)
  1940. {
  1941. int value;
  1942. struct usb_request *req = cdev->req;
  1943. unsigned long flags;
  1944. DBG(cdev, "%s\n", __func__);
  1945. spin_lock_irqsave(&cdev->lock, flags);
  1946. if (cdev->delayed_status == 0) {
  1947. WARN(cdev, "%s: Unexpected call\n", __func__);
  1948. } else if (--cdev->delayed_status == 0) {
  1949. DBG(cdev, "%s: Completing delayed status\n", __func__);
  1950. req->length = 0;
  1951. req->context = cdev;
  1952. value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
  1953. if (value < 0) {
  1954. DBG(cdev, "ep_queue --> %d\n", value);
  1955. req->status = 0;
  1956. composite_setup_complete(cdev->gadget->ep0, req);
  1957. }
  1958. }
  1959. spin_unlock_irqrestore(&cdev->lock, flags);
  1960. }
  1961. EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
  1962. static char *composite_default_mfr(struct usb_gadget *gadget)
  1963. {
  1964. char *mfr;
  1965. int len;
  1966. len = snprintf(NULL, 0, "%s %s with %s", init_utsname()->sysname,
  1967. init_utsname()->release, gadget->name);
  1968. len++;
  1969. mfr = kmalloc(len, GFP_KERNEL);
  1970. if (!mfr)
  1971. return NULL;
  1972. snprintf(mfr, len, "%s %s with %s", init_utsname()->sysname,
  1973. init_utsname()->release, gadget->name);
  1974. return mfr;
  1975. }
  1976. void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
  1977. struct usb_composite_overwrite *covr)
  1978. {
  1979. struct usb_device_descriptor *desc = &cdev->desc;
  1980. struct usb_gadget_strings *gstr = cdev->driver->strings[0];
  1981. struct usb_string *dev_str = gstr->strings;
  1982. if (covr->idVendor)
  1983. desc->idVendor = cpu_to_le16(covr->idVendor);
  1984. if (covr->idProduct)
  1985. desc->idProduct = cpu_to_le16(covr->idProduct);
  1986. if (covr->bcdDevice)
  1987. desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
  1988. if (covr->serial_number) {
  1989. desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
  1990. dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
  1991. }
  1992. if (covr->manufacturer) {
  1993. desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
  1994. dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
  1995. } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
  1996. desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
  1997. cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
  1998. dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
  1999. }
  2000. if (covr->product) {
  2001. desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
  2002. dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
  2003. }
  2004. }
  2005. EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
  2006. MODULE_LICENSE("GPL");
  2007. MODULE_AUTHOR("David Brownell");