composite.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  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, 0);
  36. MODULE_PARM_DESC(idVendor, "USB Vendor ID");
  37. static ushort idProduct;
  38. module_param(idProduct, ushort, 0);
  39. MODULE_PARM_DESC(idProduct, "USB Product ID");
  40. static ushort bcdDevice;
  41. module_param(bcdDevice, ushort, 0);
  42. MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
  43. static char *iManufacturer;
  44. module_param(iManufacturer, charp, 0);
  45. MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
  46. static char *iProduct;
  47. module_param(iProduct, charp, 0);
  48. MODULE_PARM_DESC(iProduct, "USB Product string");
  49. static char *iSerialNumber;
  50. module_param(iSerialNumber, charp, 0);
  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. status = bind(config);
  649. if (status < 0) {
  650. list_del(&config->list);
  651. config->cdev = NULL;
  652. } else {
  653. unsigned i;
  654. DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
  655. config->bConfigurationValue, config,
  656. config->superspeed ? " super" : "",
  657. config->highspeed ? " high" : "",
  658. config->fullspeed
  659. ? (gadget_is_dualspeed(cdev->gadget)
  660. ? " full"
  661. : " full/low")
  662. : "");
  663. for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
  664. struct usb_function *f = config->interface[i];
  665. if (!f)
  666. continue;
  667. DBG(cdev, " interface %d = %s/%p\n",
  668. i, f->name, f);
  669. }
  670. }
  671. /* set_alt(), or next bind(), sets up
  672. * ep->driver_data as needed.
  673. */
  674. usb_ep_autoconfig_reset(cdev->gadget);
  675. done:
  676. if (status)
  677. DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
  678. config->bConfigurationValue, status);
  679. return status;
  680. }
  681. /*-------------------------------------------------------------------------*/
  682. /* We support strings in multiple languages ... string descriptor zero
  683. * says which languages are supported. The typical case will be that
  684. * only one language (probably English) is used, with I18N handled on
  685. * the host side.
  686. */
  687. static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
  688. {
  689. const struct usb_gadget_strings *s;
  690. u16 language;
  691. __le16 *tmp;
  692. while (*sp) {
  693. s = *sp;
  694. language = cpu_to_le16(s->language);
  695. for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
  696. if (*tmp == language)
  697. goto repeat;
  698. }
  699. *tmp++ = language;
  700. repeat:
  701. sp++;
  702. }
  703. }
  704. static int lookup_string(
  705. struct usb_gadget_strings **sp,
  706. void *buf,
  707. u16 language,
  708. int id
  709. )
  710. {
  711. struct usb_gadget_strings *s;
  712. int value;
  713. while (*sp) {
  714. s = *sp++;
  715. if (s->language != language)
  716. continue;
  717. value = usb_gadget_get_string(s, id, buf);
  718. if (value > 0)
  719. return value;
  720. }
  721. return -EINVAL;
  722. }
  723. static int get_string(struct usb_composite_dev *cdev,
  724. void *buf, u16 language, int id)
  725. {
  726. struct usb_configuration *c;
  727. struct usb_function *f;
  728. int len;
  729. const char *str;
  730. /* Yes, not only is USB's I18N support probably more than most
  731. * folk will ever care about ... also, it's all supported here.
  732. * (Except for UTF8 support for Unicode's "Astral Planes".)
  733. */
  734. /* 0 == report all available language codes */
  735. if (id == 0) {
  736. struct usb_string_descriptor *s = buf;
  737. struct usb_gadget_strings **sp;
  738. memset(s, 0, 256);
  739. s->bDescriptorType = USB_DT_STRING;
  740. sp = composite->strings;
  741. if (sp)
  742. collect_langs(sp, s->wData);
  743. list_for_each_entry(c, &cdev->configs, list) {
  744. sp = c->strings;
  745. if (sp)
  746. collect_langs(sp, s->wData);
  747. list_for_each_entry(f, &c->functions, list) {
  748. sp = f->strings;
  749. if (sp)
  750. collect_langs(sp, s->wData);
  751. }
  752. }
  753. for (len = 0; len <= 126 && s->wData[len]; len++)
  754. continue;
  755. if (!len)
  756. return -EINVAL;
  757. s->bLength = 2 * (len + 1);
  758. return s->bLength;
  759. }
  760. /* Otherwise, look up and return a specified string. First
  761. * check if the string has not been overridden.
  762. */
  763. if (cdev->manufacturer_override == id)
  764. str = iManufacturer ?: composite->iManufacturer ?:
  765. composite_manufacturer;
  766. else if (cdev->product_override == id)
  767. str = iProduct ?: composite->iProduct;
  768. else if (cdev->serial_override == id)
  769. str = iSerialNumber;
  770. else
  771. str = NULL;
  772. if (str) {
  773. struct usb_gadget_strings strings = {
  774. .language = language,
  775. .strings = &(struct usb_string) { 0xff, str }
  776. };
  777. return usb_gadget_get_string(&strings, 0xff, buf);
  778. }
  779. /* String IDs are device-scoped, so we look up each string
  780. * table we're told about. These lookups are infrequent;
  781. * simpler-is-better here.
  782. */
  783. if (composite->strings) {
  784. len = lookup_string(composite->strings, buf, language, id);
  785. if (len > 0)
  786. return len;
  787. }
  788. list_for_each_entry(c, &cdev->configs, list) {
  789. if (c->strings) {
  790. len = lookup_string(c->strings, buf, language, id);
  791. if (len > 0)
  792. return len;
  793. }
  794. list_for_each_entry(f, &c->functions, list) {
  795. if (!f->strings)
  796. continue;
  797. len = lookup_string(f->strings, buf, language, id);
  798. if (len > 0)
  799. return len;
  800. }
  801. }
  802. return -EINVAL;
  803. }
  804. /**
  805. * usb_string_id() - allocate an unused string ID
  806. * @cdev: the device whose string descriptor IDs are being allocated
  807. * Context: single threaded during gadget setup
  808. *
  809. * @usb_string_id() is called from bind() callbacks to allocate
  810. * string IDs. Drivers for functions, configurations, or gadgets will
  811. * then store that ID in the appropriate descriptors and string table.
  812. *
  813. * All string identifier should be allocated using this,
  814. * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
  815. * that for example different functions don't wrongly assign different
  816. * meanings to the same identifier.
  817. */
  818. int usb_string_id(struct usb_composite_dev *cdev)
  819. {
  820. if (cdev->next_string_id < 254) {
  821. /* string id 0 is reserved by USB spec for list of
  822. * supported languages */
  823. /* 255 reserved as well? -- mina86 */
  824. cdev->next_string_id++;
  825. return cdev->next_string_id;
  826. }
  827. return -ENODEV;
  828. }
  829. /**
  830. * usb_string_ids() - allocate unused string IDs in batch
  831. * @cdev: the device whose string descriptor IDs are being allocated
  832. * @str: an array of usb_string objects to assign numbers to
  833. * Context: single threaded during gadget setup
  834. *
  835. * @usb_string_ids() is called from bind() callbacks to allocate
  836. * string IDs. Drivers for functions, configurations, or gadgets will
  837. * then copy IDs from the string table to the appropriate descriptors
  838. * and string table for other languages.
  839. *
  840. * All string identifier should be allocated using this,
  841. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  842. * example different functions don't wrongly assign different meanings
  843. * to the same identifier.
  844. */
  845. int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
  846. {
  847. int next = cdev->next_string_id;
  848. for (; str->s; ++str) {
  849. if (unlikely(next >= 254))
  850. return -ENODEV;
  851. str->id = ++next;
  852. }
  853. cdev->next_string_id = next;
  854. return 0;
  855. }
  856. /**
  857. * usb_string_ids_n() - allocate unused string IDs in batch
  858. * @c: the device whose string descriptor IDs are being allocated
  859. * @n: number of string IDs to allocate
  860. * Context: single threaded during gadget setup
  861. *
  862. * Returns the first requested ID. This ID and next @n-1 IDs are now
  863. * valid IDs. At least provided that @n is non-zero because if it
  864. * is, returns last requested ID which is now very useful information.
  865. *
  866. * @usb_string_ids_n() is called from bind() callbacks to allocate
  867. * string IDs. Drivers for functions, configurations, or gadgets will
  868. * then store that ID in the appropriate descriptors and string table.
  869. *
  870. * All string identifier should be allocated using this,
  871. * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
  872. * example different functions don't wrongly assign different meanings
  873. * to the same identifier.
  874. */
  875. int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
  876. {
  877. unsigned next = c->next_string_id;
  878. if (unlikely(n > 254 || (unsigned)next + n > 254))
  879. return -ENODEV;
  880. c->next_string_id += n;
  881. return next + 1;
  882. }
  883. /*-------------------------------------------------------------------------*/
  884. static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
  885. {
  886. if (req->status || req->actual != req->length)
  887. DBG((struct usb_composite_dev *) ep->driver_data,
  888. "setup complete --> %d, %d/%d\n",
  889. req->status, req->actual, req->length);
  890. }
  891. /*
  892. * The setup() callback implements all the ep0 functionality that's
  893. * not handled lower down, in hardware or the hardware driver(like
  894. * device and endpoint feature flags, and their status). It's all
  895. * housekeeping for the gadget function we're implementing. Most of
  896. * the work is in config and function specific setup.
  897. */
  898. static int
  899. composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  900. {
  901. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  902. struct usb_request *req = cdev->req;
  903. int value = -EOPNOTSUPP;
  904. int status = 0;
  905. u16 w_index = le16_to_cpu(ctrl->wIndex);
  906. u8 intf = w_index & 0xFF;
  907. u16 w_value = le16_to_cpu(ctrl->wValue);
  908. u16 w_length = le16_to_cpu(ctrl->wLength);
  909. struct usb_function *f = NULL;
  910. u8 endp;
  911. /* partial re-init of the response message; the function or the
  912. * gadget might need to intercept e.g. a control-OUT completion
  913. * when we delegate to it.
  914. */
  915. req->zero = 0;
  916. req->complete = composite_setup_complete;
  917. req->length = 0;
  918. gadget->ep0->driver_data = cdev;
  919. switch (ctrl->bRequest) {
  920. /* we handle all standard USB descriptors */
  921. case USB_REQ_GET_DESCRIPTOR:
  922. if (ctrl->bRequestType != USB_DIR_IN)
  923. goto unknown;
  924. switch (w_value >> 8) {
  925. case USB_DT_DEVICE:
  926. cdev->desc.bNumConfigurations =
  927. count_configs(cdev, USB_DT_DEVICE);
  928. cdev->desc.bMaxPacketSize0 =
  929. cdev->gadget->ep0->maxpacket;
  930. if (gadget_is_superspeed(gadget)) {
  931. if (gadget->speed >= USB_SPEED_SUPER) {
  932. cdev->desc.bcdUSB = cpu_to_le16(0x0300);
  933. cdev->desc.bMaxPacketSize0 = 9;
  934. } else {
  935. cdev->desc.bcdUSB = cpu_to_le16(0x0210);
  936. }
  937. }
  938. value = min(w_length, (u16) sizeof cdev->desc);
  939. memcpy(req->buf, &cdev->desc, value);
  940. break;
  941. case USB_DT_DEVICE_QUALIFIER:
  942. if (!gadget_is_dualspeed(gadget) ||
  943. gadget->speed >= USB_SPEED_SUPER)
  944. break;
  945. device_qual(cdev);
  946. value = min_t(int, w_length,
  947. sizeof(struct usb_qualifier_descriptor));
  948. break;
  949. case USB_DT_OTHER_SPEED_CONFIG:
  950. if (!gadget_is_dualspeed(gadget) ||
  951. gadget->speed >= USB_SPEED_SUPER)
  952. break;
  953. /* FALLTHROUGH */
  954. case USB_DT_CONFIG:
  955. value = config_desc(cdev, w_value);
  956. if (value >= 0)
  957. value = min(w_length, (u16) value);
  958. break;
  959. case USB_DT_STRING:
  960. value = get_string(cdev, req->buf,
  961. w_index, w_value & 0xff);
  962. if (value >= 0)
  963. value = min(w_length, (u16) value);
  964. break;
  965. case USB_DT_BOS:
  966. if (gadget_is_superspeed(gadget)) {
  967. value = bos_desc(cdev);
  968. value = min(w_length, (u16) value);
  969. }
  970. break;
  971. }
  972. break;
  973. /* any number of configs can work */
  974. case USB_REQ_SET_CONFIGURATION:
  975. if (ctrl->bRequestType != 0)
  976. goto unknown;
  977. if (gadget_is_otg(gadget)) {
  978. if (gadget->a_hnp_support)
  979. DBG(cdev, "HNP available\n");
  980. else if (gadget->a_alt_hnp_support)
  981. DBG(cdev, "HNP on another port\n");
  982. else
  983. VDBG(cdev, "HNP inactive\n");
  984. }
  985. spin_lock(&cdev->lock);
  986. value = set_config(cdev, ctrl, w_value);
  987. spin_unlock(&cdev->lock);
  988. break;
  989. case USB_REQ_GET_CONFIGURATION:
  990. if (ctrl->bRequestType != USB_DIR_IN)
  991. goto unknown;
  992. if (cdev->config)
  993. *(u8 *)req->buf = cdev->config->bConfigurationValue;
  994. else
  995. *(u8 *)req->buf = 0;
  996. value = min(w_length, (u16) 1);
  997. break;
  998. /* function drivers must handle get/set altsetting; if there's
  999. * no get() method, we know only altsetting zero works.
  1000. */
  1001. case USB_REQ_SET_INTERFACE:
  1002. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  1003. goto unknown;
  1004. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1005. break;
  1006. f = cdev->config->interface[intf];
  1007. if (!f)
  1008. break;
  1009. if (w_value && !f->set_alt)
  1010. break;
  1011. value = f->set_alt(f, w_index, w_value);
  1012. if (value == USB_GADGET_DELAYED_STATUS) {
  1013. DBG(cdev,
  1014. "%s: interface %d (%s) requested delayed status\n",
  1015. __func__, intf, f->name);
  1016. cdev->delayed_status++;
  1017. DBG(cdev, "delayed_status count %d\n",
  1018. cdev->delayed_status);
  1019. }
  1020. break;
  1021. case USB_REQ_GET_INTERFACE:
  1022. if (ctrl->bRequestType != (USB_DIR_IN|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. /* lots of interfaces only need altsetting zero... */
  1030. value = f->get_alt ? f->get_alt(f, w_index) : 0;
  1031. if (value < 0)
  1032. break;
  1033. *((u8 *)req->buf) = value;
  1034. value = min(w_length, (u16) 1);
  1035. break;
  1036. /*
  1037. * USB 3.0 additions:
  1038. * Function driver should handle get_status request. If such cb
  1039. * wasn't supplied we respond with default value = 0
  1040. * Note: function driver should supply such cb only for the first
  1041. * interface of the function
  1042. */
  1043. case USB_REQ_GET_STATUS:
  1044. if (!gadget_is_superspeed(gadget))
  1045. goto unknown;
  1046. if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
  1047. goto unknown;
  1048. value = 2; /* This is the length of the get_status reply */
  1049. put_unaligned_le16(0, req->buf);
  1050. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1051. break;
  1052. f = cdev->config->interface[intf];
  1053. if (!f)
  1054. break;
  1055. status = f->get_status ? f->get_status(f) : 0;
  1056. if (status < 0)
  1057. break;
  1058. put_unaligned_le16(status & 0x0000ffff, req->buf);
  1059. break;
  1060. /*
  1061. * Function drivers should handle SetFeature/ClearFeature
  1062. * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
  1063. * only for the first interface of the function
  1064. */
  1065. case USB_REQ_CLEAR_FEATURE:
  1066. case USB_REQ_SET_FEATURE:
  1067. if (!gadget_is_superspeed(gadget))
  1068. goto unknown;
  1069. if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
  1070. goto unknown;
  1071. switch (w_value) {
  1072. case USB_INTRF_FUNC_SUSPEND:
  1073. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1074. break;
  1075. f = cdev->config->interface[intf];
  1076. if (!f)
  1077. break;
  1078. value = 0;
  1079. if (f->func_suspend)
  1080. value = f->func_suspend(f, w_index >> 8);
  1081. if (value < 0) {
  1082. ERROR(cdev,
  1083. "func_suspend() returned error %d\n",
  1084. value);
  1085. value = 0;
  1086. }
  1087. break;
  1088. }
  1089. break;
  1090. default:
  1091. unknown:
  1092. VDBG(cdev,
  1093. "non-core control req%02x.%02x v%04x i%04x l%d\n",
  1094. ctrl->bRequestType, ctrl->bRequest,
  1095. w_value, w_index, w_length);
  1096. /* functions always handle their interfaces and endpoints...
  1097. * punt other recipients (other, WUSB, ...) to the current
  1098. * configuration code.
  1099. *
  1100. * REVISIT it could make sense to let the composite device
  1101. * take such requests too, if that's ever needed: to work
  1102. * in config 0, etc.
  1103. */
  1104. switch (ctrl->bRequestType & USB_RECIP_MASK) {
  1105. case USB_RECIP_INTERFACE:
  1106. if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
  1107. break;
  1108. f = cdev->config->interface[intf];
  1109. break;
  1110. case USB_RECIP_ENDPOINT:
  1111. endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
  1112. list_for_each_entry(f, &cdev->config->functions, list) {
  1113. if (test_bit(endp, f->endpoints))
  1114. break;
  1115. }
  1116. if (&f->list == &cdev->config->functions)
  1117. f = NULL;
  1118. break;
  1119. }
  1120. if (f && f->setup)
  1121. value = f->setup(f, ctrl);
  1122. else {
  1123. struct usb_configuration *c;
  1124. c = cdev->config;
  1125. if (c && c->setup)
  1126. value = c->setup(c, ctrl);
  1127. }
  1128. goto done;
  1129. }
  1130. /* respond with data transfer before status phase? */
  1131. if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
  1132. req->length = value;
  1133. req->zero = value < w_length;
  1134. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  1135. if (value < 0) {
  1136. DBG(cdev, "ep_queue --> %d\n", value);
  1137. req->status = 0;
  1138. composite_setup_complete(gadget->ep0, req);
  1139. }
  1140. } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
  1141. WARN(cdev,
  1142. "%s: Delayed status not supported for w_length != 0",
  1143. __func__);
  1144. }
  1145. done:
  1146. /* device either stalls (value < 0) or reports success */
  1147. return value;
  1148. }
  1149. static void composite_disconnect(struct usb_gadget *gadget)
  1150. {
  1151. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1152. unsigned long flags;
  1153. /* REVISIT: should we have config and device level
  1154. * disconnect callbacks?
  1155. */
  1156. spin_lock_irqsave(&cdev->lock, flags);
  1157. if (cdev->config)
  1158. reset_config(cdev);
  1159. if (composite->disconnect)
  1160. composite->disconnect(cdev);
  1161. spin_unlock_irqrestore(&cdev->lock, flags);
  1162. }
  1163. /*-------------------------------------------------------------------------*/
  1164. static ssize_t composite_show_suspended(struct device *dev,
  1165. struct device_attribute *attr,
  1166. char *buf)
  1167. {
  1168. struct usb_gadget *gadget = dev_to_usb_gadget(dev);
  1169. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1170. return sprintf(buf, "%d\n", cdev->suspended);
  1171. }
  1172. static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
  1173. static void
  1174. composite_unbind(struct usb_gadget *gadget)
  1175. {
  1176. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1177. /* composite_disconnect() must already have been called
  1178. * by the underlying peripheral controller driver!
  1179. * so there's no i/o concurrency that could affect the
  1180. * state protected by cdev->lock.
  1181. */
  1182. WARN_ON(cdev->config);
  1183. while (!list_empty(&cdev->configs)) {
  1184. struct usb_configuration *c;
  1185. c = list_first_entry(&cdev->configs,
  1186. struct usb_configuration, list);
  1187. while (!list_empty(&c->functions)) {
  1188. struct usb_function *f;
  1189. f = list_first_entry(&c->functions,
  1190. struct usb_function, list);
  1191. list_del(&f->list);
  1192. if (f->unbind) {
  1193. DBG(cdev, "unbind function '%s'/%p\n",
  1194. f->name, f);
  1195. f->unbind(c, f);
  1196. /* may free memory for "f" */
  1197. }
  1198. }
  1199. list_del(&c->list);
  1200. if (c->unbind) {
  1201. DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
  1202. c->unbind(c);
  1203. /* may free memory for "c" */
  1204. }
  1205. }
  1206. if (composite->unbind)
  1207. composite->unbind(cdev);
  1208. if (cdev->req) {
  1209. kfree(cdev->req->buf);
  1210. usb_ep_free_request(gadget->ep0, cdev->req);
  1211. }
  1212. device_remove_file(&gadget->dev, &dev_attr_suspended);
  1213. kfree(cdev);
  1214. set_gadget_data(gadget, NULL);
  1215. composite = NULL;
  1216. }
  1217. static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
  1218. {
  1219. if (!*desc) {
  1220. int ret = usb_string_id(cdev);
  1221. if (unlikely(ret < 0))
  1222. WARNING(cdev, "failed to override string ID\n");
  1223. else
  1224. *desc = ret;
  1225. }
  1226. return *desc;
  1227. }
  1228. static int composite_bind(struct usb_gadget *gadget)
  1229. {
  1230. struct usb_composite_dev *cdev;
  1231. int status = -ENOMEM;
  1232. cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
  1233. if (!cdev)
  1234. return status;
  1235. spin_lock_init(&cdev->lock);
  1236. cdev->gadget = gadget;
  1237. set_gadget_data(gadget, cdev);
  1238. INIT_LIST_HEAD(&cdev->configs);
  1239. /* preallocate control response and buffer */
  1240. cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  1241. if (!cdev->req)
  1242. goto fail;
  1243. cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
  1244. if (!cdev->req->buf)
  1245. goto fail;
  1246. cdev->req->complete = composite_setup_complete;
  1247. gadget->ep0->driver_data = cdev;
  1248. cdev->bufsiz = USB_BUFSIZ;
  1249. cdev->driver = composite;
  1250. /*
  1251. * As per USB compliance update, a device that is actively drawing
  1252. * more than 100mA from USB must report itself as bus-powered in
  1253. * the GetStatus(DEVICE) call.
  1254. */
  1255. if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
  1256. usb_gadget_set_selfpowered(gadget);
  1257. /* interface and string IDs start at zero via kzalloc.
  1258. * we force endpoints to start unassigned; few controller
  1259. * drivers will zero ep->driver_data.
  1260. */
  1261. usb_ep_autoconfig_reset(cdev->gadget);
  1262. /* composite gadget needs to assign strings for whole device (like
  1263. * serial number), register function drivers, potentially update
  1264. * power state and consumption, etc
  1265. */
  1266. status = composite_gadget_bind(cdev);
  1267. if (status < 0)
  1268. goto fail;
  1269. cdev->desc = *composite->dev;
  1270. /* standardized runtime overrides for device ID data */
  1271. if (idVendor)
  1272. cdev->desc.idVendor = cpu_to_le16(idVendor);
  1273. if (idProduct)
  1274. cdev->desc.idProduct = cpu_to_le16(idProduct);
  1275. if (bcdDevice)
  1276. cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
  1277. /* string overrides */
  1278. if (iManufacturer || !cdev->desc.iManufacturer) {
  1279. if (!iManufacturer && !composite->iManufacturer &&
  1280. !*composite_manufacturer)
  1281. snprintf(composite_manufacturer,
  1282. sizeof composite_manufacturer,
  1283. "%s %s with %s",
  1284. init_utsname()->sysname,
  1285. init_utsname()->release,
  1286. gadget->name);
  1287. cdev->manufacturer_override =
  1288. override_id(cdev, &cdev->desc.iManufacturer);
  1289. }
  1290. if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
  1291. cdev->product_override =
  1292. override_id(cdev, &cdev->desc.iProduct);
  1293. if (iSerialNumber)
  1294. cdev->serial_override =
  1295. override_id(cdev, &cdev->desc.iSerialNumber);
  1296. /* has userspace failed to provide a serial number? */
  1297. if (composite->needs_serial && !cdev->desc.iSerialNumber)
  1298. WARNING(cdev, "userspace failed to provide iSerialNumber\n");
  1299. /* finish up */
  1300. status = device_create_file(&gadget->dev, &dev_attr_suspended);
  1301. if (status)
  1302. goto fail;
  1303. INFO(cdev, "%s ready\n", composite->name);
  1304. return 0;
  1305. fail:
  1306. composite_unbind(gadget);
  1307. return status;
  1308. }
  1309. /*-------------------------------------------------------------------------*/
  1310. static void
  1311. composite_suspend(struct usb_gadget *gadget)
  1312. {
  1313. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1314. struct usb_function *f;
  1315. /* REVISIT: should we have config level
  1316. * suspend/resume callbacks?
  1317. */
  1318. DBG(cdev, "suspend\n");
  1319. if (cdev->config) {
  1320. list_for_each_entry(f, &cdev->config->functions, list) {
  1321. if (f->suspend)
  1322. f->suspend(f);
  1323. }
  1324. }
  1325. if (composite->suspend)
  1326. composite->suspend(cdev);
  1327. cdev->suspended = 1;
  1328. usb_gadget_vbus_draw(gadget, 2);
  1329. }
  1330. static void
  1331. composite_resume(struct usb_gadget *gadget)
  1332. {
  1333. struct usb_composite_dev *cdev = get_gadget_data(gadget);
  1334. struct usb_function *f;
  1335. u8 maxpower;
  1336. /* REVISIT: should we have config level
  1337. * suspend/resume callbacks?
  1338. */
  1339. DBG(cdev, "resume\n");
  1340. if (composite->resume)
  1341. composite->resume(cdev);
  1342. if (cdev->config) {
  1343. list_for_each_entry(f, &cdev->config->functions, list) {
  1344. if (f->resume)
  1345. f->resume(f);
  1346. }
  1347. maxpower = cdev->config->bMaxPower;
  1348. usb_gadget_vbus_draw(gadget, maxpower ?
  1349. (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
  1350. }
  1351. cdev->suspended = 0;
  1352. }
  1353. /*-------------------------------------------------------------------------*/
  1354. static struct usb_gadget_driver composite_driver = {
  1355. #ifdef CONFIG_USB_GADGET_SUPERSPEED
  1356. .max_speed = USB_SPEED_SUPER,
  1357. #else
  1358. .max_speed = USB_SPEED_HIGH,
  1359. #endif
  1360. .unbind = composite_unbind,
  1361. .setup = composite_setup,
  1362. .disconnect = composite_disconnect,
  1363. .suspend = composite_suspend,
  1364. .resume = composite_resume,
  1365. .driver = {
  1366. .owner = THIS_MODULE,
  1367. },
  1368. };
  1369. /**
  1370. * usb_composite_probe() - register a composite driver
  1371. * @driver: the driver to register
  1372. * @bind: the callback used to allocate resources that are shared across the
  1373. * whole device, such as string IDs, and add its configurations using
  1374. * @usb_add_config(). This may fail by returning a negative errno
  1375. * value; it should return zero on successful initialization.
  1376. * Context: single threaded during gadget setup
  1377. *
  1378. * This function is used to register drivers using the composite driver
  1379. * framework. The return value is zero, or a negative errno value.
  1380. * Those values normally come from the driver's @bind method, which does
  1381. * all the work of setting up the driver to match the hardware.
  1382. *
  1383. * On successful return, the gadget is ready to respond to requests from
  1384. * the host, unless one of its components invokes usb_gadget_disconnect()
  1385. * while it was binding. That would usually be done in order to wait for
  1386. * some userspace participation.
  1387. */
  1388. int usb_composite_probe(struct usb_composite_driver *driver,
  1389. int (*bind)(struct usb_composite_dev *cdev))
  1390. {
  1391. if (!driver || !driver->dev || !bind || composite)
  1392. return -EINVAL;
  1393. if (!driver->name)
  1394. driver->name = "composite";
  1395. if (!driver->iProduct)
  1396. driver->iProduct = driver->name;
  1397. composite_driver.function = (char *) driver->name;
  1398. composite_driver.driver.name = driver->name;
  1399. composite_driver.max_speed =
  1400. min_t(u8, composite_driver.max_speed, driver->max_speed);
  1401. composite = driver;
  1402. composite_gadget_bind = bind;
  1403. return usb_gadget_probe_driver(&composite_driver, composite_bind);
  1404. }
  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. if (composite != driver)
  1415. return;
  1416. usb_gadget_unregister_driver(&composite_driver);
  1417. }
  1418. /**
  1419. * usb_composite_setup_continue() - Continue with the control transfer
  1420. * @cdev: the composite device who's control transfer was kept waiting
  1421. *
  1422. * This function must be called by the USB function driver to continue
  1423. * with the control transfer's data/status stage in case it had requested to
  1424. * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
  1425. * can request the composite framework to delay the setup request's data/status
  1426. * stages by returning USB_GADGET_DELAYED_STATUS.
  1427. */
  1428. void usb_composite_setup_continue(struct usb_composite_dev *cdev)
  1429. {
  1430. int value;
  1431. struct usb_request *req = cdev->req;
  1432. unsigned long flags;
  1433. DBG(cdev, "%s\n", __func__);
  1434. spin_lock_irqsave(&cdev->lock, flags);
  1435. if (cdev->delayed_status == 0) {
  1436. WARN(cdev, "%s: Unexpected call\n", __func__);
  1437. } else if (--cdev->delayed_status == 0) {
  1438. DBG(cdev, "%s: Completing delayed status\n", __func__);
  1439. req->length = 0;
  1440. value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  1441. if (value < 0) {
  1442. DBG(cdev, "ep_queue --> %d\n", value);
  1443. req->status = 0;
  1444. composite_setup_complete(cdev->gadget->ep0, req);
  1445. }
  1446. }
  1447. spin_unlock_irqrestore(&cdev->lock, flags);
  1448. }