composite.c 51 KB

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