composite.c 46 KB

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