composite.c 47 KB

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