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