composite.c 47 KB

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