composite.c 46 KB

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