f_sourcesink.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  1. /*
  2. * f_sourcesink.c - USB peripheral source/sink configuration driver
  3. *
  4. * Copyright (C) 2003-2008 David Brownell
  5. * Copyright (C) 2008 by Nokia Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. /* #define VERBOSE_DEBUG */
  13. #include <linux/slab.h>
  14. #include <linux/kernel.h>
  15. #include <linux/device.h>
  16. #include <linux/module.h>
  17. #include <linux/usb/composite.h>
  18. #include <linux/err.h>
  19. #include "g_zero.h"
  20. #include "gadget_chips.h"
  21. #include "u_f.h"
  22. #define USB_MS_TO_SS_INTERVAL(x) USB_MS_TO_HS_INTERVAL(x)
  23. enum eptype {
  24. EP_CONTROL = 0,
  25. EP_BULK,
  26. EP_ISOC,
  27. EP_INTERRUPT,
  28. };
  29. /*
  30. * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
  31. * controller drivers.
  32. *
  33. * This just sinks bulk packets OUT to the peripheral and sources them IN
  34. * to the host, optionally with specific data patterns for integrity tests.
  35. * As such it supports basic functionality and load tests.
  36. *
  37. * In terms of control messaging, this supports all the standard requests
  38. * plus two that support control-OUT tests. If the optional "autoresume"
  39. * mode is enabled, it provides good functional coverage for the "USBCV"
  40. * test harness from USB-IF.
  41. *
  42. * Note that because this doesn't queue more than one request at a time,
  43. * some other function must be used to test queueing logic. The network
  44. * link (g_ether) is the best overall option for that, since its TX and RX
  45. * queues are relatively independent, will receive a range of packet sizes,
  46. * and can often be made to run out completely. Those issues are important
  47. * when stress testing peripheral controller drivers.
  48. *
  49. *
  50. * This is currently packaged as a configuration driver, which can't be
  51. * combined with other functions to make composite devices. However, it
  52. * can be combined with other independent configurations.
  53. */
  54. struct f_sourcesink {
  55. struct usb_function function;
  56. struct usb_ep *in_ep;
  57. struct usb_ep *out_ep;
  58. struct usb_ep *iso_in_ep;
  59. struct usb_ep *iso_out_ep;
  60. struct usb_ep *int_in_ep;
  61. struct usb_ep *int_out_ep;
  62. int cur_alt;
  63. };
  64. static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
  65. {
  66. return container_of(f, struct f_sourcesink, function);
  67. }
  68. static unsigned pattern;
  69. static unsigned isoc_interval;
  70. static unsigned isoc_maxpacket;
  71. static unsigned isoc_mult;
  72. static unsigned isoc_maxburst;
  73. static unsigned int_interval; /* In ms */
  74. static unsigned int_maxpacket;
  75. static unsigned int_mult;
  76. static unsigned int_maxburst;
  77. static unsigned buflen;
  78. /*-------------------------------------------------------------------------*/
  79. static struct usb_interface_descriptor source_sink_intf_alt0 = {
  80. .bLength = USB_DT_INTERFACE_SIZE,
  81. .bDescriptorType = USB_DT_INTERFACE,
  82. .bAlternateSetting = 0,
  83. .bNumEndpoints = 2,
  84. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  85. /* .iInterface = DYNAMIC */
  86. };
  87. static struct usb_interface_descriptor source_sink_intf_alt1 = {
  88. .bLength = USB_DT_INTERFACE_SIZE,
  89. .bDescriptorType = USB_DT_INTERFACE,
  90. .bAlternateSetting = 1,
  91. .bNumEndpoints = 4,
  92. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  93. /* .iInterface = DYNAMIC */
  94. };
  95. static struct usb_interface_descriptor source_sink_intf_alt2 = {
  96. .bLength = USB_DT_INTERFACE_SIZE,
  97. .bDescriptorType = USB_DT_INTERFACE,
  98. .bAlternateSetting = 2,
  99. .bNumEndpoints = 2,
  100. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  101. /* .iInterface = DYNAMIC */
  102. };
  103. /* full speed support: */
  104. static struct usb_endpoint_descriptor fs_source_desc = {
  105. .bLength = USB_DT_ENDPOINT_SIZE,
  106. .bDescriptorType = USB_DT_ENDPOINT,
  107. .bEndpointAddress = USB_DIR_IN,
  108. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  109. };
  110. static struct usb_endpoint_descriptor fs_sink_desc = {
  111. .bLength = USB_DT_ENDPOINT_SIZE,
  112. .bDescriptorType = USB_DT_ENDPOINT,
  113. .bEndpointAddress = USB_DIR_OUT,
  114. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  115. };
  116. static struct usb_endpoint_descriptor fs_iso_source_desc = {
  117. .bLength = USB_DT_ENDPOINT_SIZE,
  118. .bDescriptorType = USB_DT_ENDPOINT,
  119. .bEndpointAddress = USB_DIR_IN,
  120. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  121. .wMaxPacketSize = cpu_to_le16(1023),
  122. .bInterval = 4,
  123. };
  124. static struct usb_endpoint_descriptor fs_iso_sink_desc = {
  125. .bLength = USB_DT_ENDPOINT_SIZE,
  126. .bDescriptorType = USB_DT_ENDPOINT,
  127. .bEndpointAddress = USB_DIR_OUT,
  128. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  129. .wMaxPacketSize = cpu_to_le16(1023),
  130. .bInterval = 4,
  131. };
  132. static struct usb_endpoint_descriptor fs_int_source_desc = {
  133. .bLength = USB_DT_ENDPOINT_SIZE,
  134. .bDescriptorType = USB_DT_ENDPOINT,
  135. .bEndpointAddress = USB_DIR_IN,
  136. .bmAttributes = USB_ENDPOINT_XFER_INT,
  137. .wMaxPacketSize = cpu_to_le16(64),
  138. .bInterval = GZERO_INT_INTERVAL,
  139. };
  140. static struct usb_endpoint_descriptor fs_int_sink_desc = {
  141. .bLength = USB_DT_ENDPOINT_SIZE,
  142. .bDescriptorType = USB_DT_ENDPOINT,
  143. .bEndpointAddress = USB_DIR_OUT,
  144. .bmAttributes = USB_ENDPOINT_XFER_INT,
  145. .wMaxPacketSize = cpu_to_le16(64),
  146. .bInterval = GZERO_INT_INTERVAL,
  147. };
  148. static struct usb_descriptor_header *fs_source_sink_descs[] = {
  149. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  150. (struct usb_descriptor_header *) &fs_sink_desc,
  151. (struct usb_descriptor_header *) &fs_source_desc,
  152. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  153. #define FS_ALT_IFC_1_OFFSET 3
  154. (struct usb_descriptor_header *) &fs_sink_desc,
  155. (struct usb_descriptor_header *) &fs_source_desc,
  156. (struct usb_descriptor_header *) &fs_iso_sink_desc,
  157. (struct usb_descriptor_header *) &fs_iso_source_desc,
  158. (struct usb_descriptor_header *) &source_sink_intf_alt2,
  159. #define FS_ALT_IFC_2_OFFSET 8
  160. (struct usb_descriptor_header *) &fs_int_sink_desc,
  161. (struct usb_descriptor_header *) &fs_int_source_desc,
  162. NULL,
  163. };
  164. /* high speed support: */
  165. static struct usb_endpoint_descriptor hs_source_desc = {
  166. .bLength = USB_DT_ENDPOINT_SIZE,
  167. .bDescriptorType = USB_DT_ENDPOINT,
  168. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  169. .wMaxPacketSize = cpu_to_le16(512),
  170. };
  171. static struct usb_endpoint_descriptor hs_sink_desc = {
  172. .bLength = USB_DT_ENDPOINT_SIZE,
  173. .bDescriptorType = USB_DT_ENDPOINT,
  174. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  175. .wMaxPacketSize = cpu_to_le16(512),
  176. };
  177. static struct usb_endpoint_descriptor hs_iso_source_desc = {
  178. .bLength = USB_DT_ENDPOINT_SIZE,
  179. .bDescriptorType = USB_DT_ENDPOINT,
  180. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  181. .wMaxPacketSize = cpu_to_le16(1024),
  182. .bInterval = 4,
  183. };
  184. static struct usb_endpoint_descriptor hs_iso_sink_desc = {
  185. .bLength = USB_DT_ENDPOINT_SIZE,
  186. .bDescriptorType = USB_DT_ENDPOINT,
  187. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  188. .wMaxPacketSize = cpu_to_le16(1024),
  189. .bInterval = 4,
  190. };
  191. static struct usb_endpoint_descriptor hs_int_source_desc = {
  192. .bLength = USB_DT_ENDPOINT_SIZE,
  193. .bDescriptorType = USB_DT_ENDPOINT,
  194. .bmAttributes = USB_ENDPOINT_XFER_INT,
  195. .wMaxPacketSize = cpu_to_le16(1024),
  196. .bInterval = USB_MS_TO_HS_INTERVAL(GZERO_INT_INTERVAL),
  197. };
  198. static struct usb_endpoint_descriptor hs_int_sink_desc = {
  199. .bLength = USB_DT_ENDPOINT_SIZE,
  200. .bDescriptorType = USB_DT_ENDPOINT,
  201. .bmAttributes = USB_ENDPOINT_XFER_INT,
  202. .wMaxPacketSize = cpu_to_le16(1024),
  203. .bInterval = USB_MS_TO_HS_INTERVAL(GZERO_INT_INTERVAL),
  204. };
  205. static struct usb_descriptor_header *hs_source_sink_descs[] = {
  206. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  207. (struct usb_descriptor_header *) &hs_source_desc,
  208. (struct usb_descriptor_header *) &hs_sink_desc,
  209. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  210. #define HS_ALT_IFC_1_OFFSET 3
  211. (struct usb_descriptor_header *) &hs_source_desc,
  212. (struct usb_descriptor_header *) &hs_sink_desc,
  213. (struct usb_descriptor_header *) &hs_iso_source_desc,
  214. (struct usb_descriptor_header *) &hs_iso_sink_desc,
  215. (struct usb_descriptor_header *) &source_sink_intf_alt2,
  216. #define HS_ALT_IFC_2_OFFSET 8
  217. (struct usb_descriptor_header *) &hs_int_source_desc,
  218. (struct usb_descriptor_header *) &hs_int_sink_desc,
  219. NULL,
  220. };
  221. /* super speed support: */
  222. static struct usb_endpoint_descriptor ss_source_desc = {
  223. .bLength = USB_DT_ENDPOINT_SIZE,
  224. .bDescriptorType = USB_DT_ENDPOINT,
  225. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  226. .wMaxPacketSize = cpu_to_le16(1024),
  227. };
  228. static struct usb_ss_ep_comp_descriptor ss_source_comp_desc = {
  229. .bLength = USB_DT_SS_EP_COMP_SIZE,
  230. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  231. .bMaxBurst = 0,
  232. .bmAttributes = 0,
  233. .wBytesPerInterval = 0,
  234. };
  235. static struct usb_endpoint_descriptor ss_sink_desc = {
  236. .bLength = USB_DT_ENDPOINT_SIZE,
  237. .bDescriptorType = USB_DT_ENDPOINT,
  238. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  239. .wMaxPacketSize = cpu_to_le16(1024),
  240. };
  241. static struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = {
  242. .bLength = USB_DT_SS_EP_COMP_SIZE,
  243. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  244. .bMaxBurst = 0,
  245. .bmAttributes = 0,
  246. .wBytesPerInterval = 0,
  247. };
  248. static struct usb_endpoint_descriptor ss_iso_source_desc = {
  249. .bLength = USB_DT_ENDPOINT_SIZE,
  250. .bDescriptorType = USB_DT_ENDPOINT,
  251. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  252. .wMaxPacketSize = cpu_to_le16(1024),
  253. .bInterval = 4,
  254. };
  255. static struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc = {
  256. .bLength = USB_DT_SS_EP_COMP_SIZE,
  257. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  258. .bMaxBurst = 0,
  259. .bmAttributes = 0,
  260. .wBytesPerInterval = cpu_to_le16(1024),
  261. };
  262. static struct usb_endpoint_descriptor ss_iso_sink_desc = {
  263. .bLength = USB_DT_ENDPOINT_SIZE,
  264. .bDescriptorType = USB_DT_ENDPOINT,
  265. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  266. .wMaxPacketSize = cpu_to_le16(1024),
  267. .bInterval = 4,
  268. };
  269. static struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = {
  270. .bLength = USB_DT_SS_EP_COMP_SIZE,
  271. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  272. .bMaxBurst = 0,
  273. .bmAttributes = 0,
  274. .wBytesPerInterval = cpu_to_le16(1024),
  275. };
  276. static struct usb_endpoint_descriptor ss_int_source_desc = {
  277. .bLength = USB_DT_ENDPOINT_SIZE,
  278. .bDescriptorType = USB_DT_ENDPOINT,
  279. .bmAttributes = USB_ENDPOINT_XFER_INT,
  280. .wMaxPacketSize = cpu_to_le16(1024),
  281. .bInterval = USB_MS_TO_SS_INTERVAL(GZERO_INT_INTERVAL),
  282. };
  283. struct usb_ss_ep_comp_descriptor ss_int_source_comp_desc = {
  284. .bLength = USB_DT_SS_EP_COMP_SIZE,
  285. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  286. .bMaxBurst = 0,
  287. .bmAttributes = 0,
  288. .wBytesPerInterval = cpu_to_le16(1024),
  289. };
  290. static struct usb_endpoint_descriptor ss_int_sink_desc = {
  291. .bLength = USB_DT_ENDPOINT_SIZE,
  292. .bDescriptorType = USB_DT_ENDPOINT,
  293. .bmAttributes = USB_ENDPOINT_XFER_INT,
  294. .wMaxPacketSize = cpu_to_le16(1024),
  295. .bInterval = USB_MS_TO_SS_INTERVAL(GZERO_INT_INTERVAL),
  296. };
  297. struct usb_ss_ep_comp_descriptor ss_int_sink_comp_desc = {
  298. .bLength = USB_DT_SS_EP_COMP_SIZE,
  299. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  300. .bMaxBurst = 0,
  301. .bmAttributes = 0,
  302. .wBytesPerInterval = cpu_to_le16(1024),
  303. };
  304. static struct usb_descriptor_header *ss_source_sink_descs[] = {
  305. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  306. (struct usb_descriptor_header *) &ss_source_desc,
  307. (struct usb_descriptor_header *) &ss_source_comp_desc,
  308. (struct usb_descriptor_header *) &ss_sink_desc,
  309. (struct usb_descriptor_header *) &ss_sink_comp_desc,
  310. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  311. #define SS_ALT_IFC_1_OFFSET 5
  312. (struct usb_descriptor_header *) &ss_source_desc,
  313. (struct usb_descriptor_header *) &ss_source_comp_desc,
  314. (struct usb_descriptor_header *) &ss_sink_desc,
  315. (struct usb_descriptor_header *) &ss_sink_comp_desc,
  316. (struct usb_descriptor_header *) &ss_iso_source_desc,
  317. (struct usb_descriptor_header *) &ss_iso_source_comp_desc,
  318. (struct usb_descriptor_header *) &ss_iso_sink_desc,
  319. (struct usb_descriptor_header *) &ss_iso_sink_comp_desc,
  320. (struct usb_descriptor_header *) &source_sink_intf_alt2,
  321. #define SS_ALT_IFC_2_OFFSET 14
  322. (struct usb_descriptor_header *) &ss_int_source_desc,
  323. (struct usb_descriptor_header *) &ss_int_source_comp_desc,
  324. (struct usb_descriptor_header *) &ss_int_sink_desc,
  325. (struct usb_descriptor_header *) &ss_int_sink_comp_desc,
  326. NULL,
  327. };
  328. /* function-specific strings: */
  329. static struct usb_string strings_sourcesink[] = {
  330. [0].s = "source and sink data",
  331. { } /* end of list */
  332. };
  333. static struct usb_gadget_strings stringtab_sourcesink = {
  334. .language = 0x0409, /* en-us */
  335. .strings = strings_sourcesink,
  336. };
  337. static struct usb_gadget_strings *sourcesink_strings[] = {
  338. &stringtab_sourcesink,
  339. NULL,
  340. };
  341. /*-------------------------------------------------------------------------*/
  342. static const char *get_ep_string(enum eptype ep_type)
  343. {
  344. switch (ep_type) {
  345. case EP_ISOC:
  346. return "ISOC-";
  347. case EP_INTERRUPT:
  348. return "INTERRUPT-";
  349. case EP_CONTROL:
  350. return "CTRL-";
  351. case EP_BULK:
  352. return "BULK-";
  353. default:
  354. return "UNKNOWN-";
  355. }
  356. }
  357. static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len)
  358. {
  359. return alloc_ep_req(ep, len, buflen);
  360. }
  361. void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  362. {
  363. kfree(req->buf);
  364. usb_ep_free_request(ep, req);
  365. }
  366. static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
  367. {
  368. int value;
  369. if (ep->driver_data) {
  370. value = usb_ep_disable(ep);
  371. if (value < 0)
  372. DBG(cdev, "disable %s --> %d\n",
  373. ep->name, value);
  374. ep->driver_data = NULL;
  375. }
  376. }
  377. void disable_endpoints(struct usb_composite_dev *cdev,
  378. struct usb_ep *in, struct usb_ep *out,
  379. struct usb_ep *iso_in, struct usb_ep *iso_out,
  380. struct usb_ep *int_in, struct usb_ep *int_out)
  381. {
  382. disable_ep(cdev, in);
  383. disable_ep(cdev, out);
  384. if (iso_in)
  385. disable_ep(cdev, iso_in);
  386. if (iso_out)
  387. disable_ep(cdev, iso_out);
  388. if (int_in)
  389. disable_ep(cdev, int_in);
  390. if (int_out)
  391. disable_ep(cdev, int_out);
  392. }
  393. static int
  394. sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
  395. {
  396. struct usb_composite_dev *cdev = c->cdev;
  397. struct f_sourcesink *ss = func_to_ss(f);
  398. int id;
  399. int ret;
  400. /* allocate interface ID(s) */
  401. id = usb_interface_id(c, f);
  402. if (id < 0)
  403. return id;
  404. source_sink_intf_alt0.bInterfaceNumber = id;
  405. source_sink_intf_alt1.bInterfaceNumber = id;
  406. source_sink_intf_alt2.bInterfaceNumber = id;
  407. /* allocate bulk endpoints */
  408. ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
  409. if (!ss->in_ep) {
  410. autoconf_fail:
  411. ERROR(cdev, "%s: can't autoconfigure on %s\n",
  412. f->name, cdev->gadget->name);
  413. return -ENODEV;
  414. }
  415. ss->in_ep->driver_data = cdev; /* claim */
  416. ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
  417. if (!ss->out_ep)
  418. goto autoconf_fail;
  419. ss->out_ep->driver_data = cdev; /* claim */
  420. /* sanity check the isoc module parameters */
  421. if (isoc_interval < 1)
  422. isoc_interval = 1;
  423. if (isoc_interval > 16)
  424. isoc_interval = 16;
  425. if (isoc_mult > 2)
  426. isoc_mult = 2;
  427. if (isoc_maxburst > 15)
  428. isoc_maxburst = 15;
  429. /* fill in the FS isoc descriptors from the module parameters */
  430. fs_iso_source_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
  431. 1023 : isoc_maxpacket;
  432. fs_iso_source_desc.bInterval = isoc_interval;
  433. fs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
  434. 1023 : isoc_maxpacket;
  435. fs_iso_sink_desc.bInterval = isoc_interval;
  436. /* allocate iso endpoints */
  437. ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
  438. if (!ss->iso_in_ep)
  439. goto no_iso;
  440. ss->iso_in_ep->driver_data = cdev; /* claim */
  441. ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
  442. if (ss->iso_out_ep) {
  443. ss->iso_out_ep->driver_data = cdev; /* claim */
  444. } else {
  445. ss->iso_in_ep->driver_data = NULL;
  446. ss->iso_in_ep = NULL;
  447. no_iso:
  448. /*
  449. * We still want to work even if the UDC doesn't have isoc
  450. * endpoints, so null out the alt interface that contains
  451. * them and continue.
  452. */
  453. fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
  454. hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
  455. ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
  456. }
  457. if (isoc_maxpacket > 1024)
  458. isoc_maxpacket = 1024;
  459. /* sanity check the interrupt module parameters */
  460. if (int_interval < 1)
  461. int_interval = 1;
  462. if (int_interval > 4096)
  463. int_interval = 4096;
  464. if (int_mult > 2)
  465. int_mult = 2;
  466. if (int_maxburst > 15)
  467. int_maxburst = 15;
  468. /* fill in the FS interrupt descriptors from the module parameters */
  469. fs_int_source_desc.wMaxPacketSize = int_maxpacket > 64 ?
  470. 64 : int_maxpacket;
  471. fs_int_source_desc.bInterval = int_interval > 255 ?
  472. 255 : int_interval;
  473. fs_int_sink_desc.wMaxPacketSize = int_maxpacket > 64 ?
  474. 64 : int_maxpacket;
  475. fs_int_sink_desc.bInterval = int_interval > 255 ?
  476. 255 : int_interval;
  477. /* allocate int endpoints */
  478. ss->int_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_int_source_desc);
  479. if (!ss->int_in_ep)
  480. goto no_int;
  481. ss->int_in_ep->driver_data = cdev; /* claim */
  482. ss->int_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_int_sink_desc);
  483. if (ss->int_out_ep) {
  484. ss->int_out_ep->driver_data = cdev; /* claim */
  485. } else {
  486. ss->int_in_ep->driver_data = NULL;
  487. ss->int_in_ep = NULL;
  488. no_int:
  489. fs_source_sink_descs[FS_ALT_IFC_2_OFFSET] = NULL;
  490. hs_source_sink_descs[HS_ALT_IFC_2_OFFSET] = NULL;
  491. ss_source_sink_descs[SS_ALT_IFC_2_OFFSET] = NULL;
  492. }
  493. if (int_maxpacket > 1024)
  494. int_maxpacket = 1024;
  495. /* support high speed hardware */
  496. hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  497. hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  498. /*
  499. * Fill in the HS isoc and interrupt descriptors from the module
  500. * parameters. We assume that the user knows what they are doing and
  501. * won't give parameters that their UDC doesn't support.
  502. */
  503. hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
  504. hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11;
  505. hs_iso_source_desc.bInterval = isoc_interval;
  506. hs_iso_source_desc.bEndpointAddress =
  507. fs_iso_source_desc.bEndpointAddress;
  508. hs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
  509. hs_iso_sink_desc.wMaxPacketSize |= isoc_mult << 11;
  510. hs_iso_sink_desc.bInterval = isoc_interval;
  511. hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
  512. hs_int_source_desc.wMaxPacketSize = int_maxpacket;
  513. hs_int_source_desc.wMaxPacketSize |= int_mult << 11;
  514. hs_int_source_desc.bInterval = USB_MS_TO_HS_INTERVAL(int_interval);
  515. hs_int_source_desc.bEndpointAddress =
  516. fs_int_source_desc.bEndpointAddress;
  517. hs_int_sink_desc.wMaxPacketSize = int_maxpacket;
  518. hs_int_sink_desc.wMaxPacketSize |= int_mult << 11;
  519. hs_int_sink_desc.bInterval = USB_MS_TO_HS_INTERVAL(int_interval);
  520. hs_int_sink_desc.bEndpointAddress = fs_int_sink_desc.bEndpointAddress;
  521. /* support super speed hardware */
  522. ss_source_desc.bEndpointAddress =
  523. fs_source_desc.bEndpointAddress;
  524. ss_sink_desc.bEndpointAddress =
  525. fs_sink_desc.bEndpointAddress;
  526. /*
  527. * Fill in the SS isoc and interrupt descriptors from the module
  528. * parameters. We assume that the user knows what they are doing and
  529. * won't give parameters that their UDC doesn't support.
  530. */
  531. ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
  532. ss_iso_source_desc.bInterval = isoc_interval;
  533. ss_iso_source_comp_desc.bmAttributes = isoc_mult;
  534. ss_iso_source_comp_desc.bMaxBurst = isoc_maxburst;
  535. ss_iso_source_comp_desc.wBytesPerInterval =
  536. isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
  537. ss_iso_source_desc.bEndpointAddress =
  538. fs_iso_source_desc.bEndpointAddress;
  539. ss_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
  540. ss_iso_sink_desc.bInterval = isoc_interval;
  541. ss_iso_sink_comp_desc.bmAttributes = isoc_mult;
  542. ss_iso_sink_comp_desc.bMaxBurst = isoc_maxburst;
  543. ss_iso_sink_comp_desc.wBytesPerInterval =
  544. isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
  545. ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
  546. ss_int_source_desc.wMaxPacketSize = int_maxpacket;
  547. ss_int_source_desc.bInterval = USB_MS_TO_SS_INTERVAL(int_interval);
  548. ss_int_source_comp_desc.bmAttributes = int_mult;
  549. ss_int_source_comp_desc.bMaxBurst = int_maxburst;
  550. ss_int_source_comp_desc.wBytesPerInterval =
  551. int_maxpacket * (int_mult + 1) * (int_maxburst + 1);
  552. ss_int_source_desc.bEndpointAddress =
  553. fs_int_source_desc.bEndpointAddress;
  554. ss_int_sink_desc.wMaxPacketSize = int_maxpacket;
  555. ss_int_sink_desc.bInterval = USB_MS_TO_SS_INTERVAL(int_interval);
  556. ss_int_sink_comp_desc.bmAttributes = int_mult;
  557. ss_int_sink_comp_desc.bMaxBurst = int_maxburst;
  558. ss_int_sink_comp_desc.wBytesPerInterval =
  559. int_maxpacket * (int_mult + 1) * (int_maxburst + 1);
  560. ss_int_sink_desc.bEndpointAddress = fs_int_sink_desc.bEndpointAddress;
  561. ret = usb_assign_descriptors(f, fs_source_sink_descs,
  562. hs_source_sink_descs, ss_source_sink_descs);
  563. if (ret)
  564. return ret;
  565. DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s, "
  566. "INT-IN/%s, INT-OUT/%s\n",
  567. (gadget_is_superspeed(c->cdev->gadget) ? "super" :
  568. (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
  569. f->name, ss->in_ep->name, ss->out_ep->name,
  570. ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
  571. ss->iso_out_ep ? ss->iso_out_ep->name : "<none>",
  572. ss->int_in_ep ? ss->int_in_ep->name : "<none>",
  573. ss->int_out_ep ? ss->int_out_ep->name : "<none>");
  574. return 0;
  575. }
  576. static void
  577. sourcesink_free_func(struct usb_function *f)
  578. {
  579. struct f_ss_opts *opts;
  580. opts = container_of(f->fi, struct f_ss_opts, func_inst);
  581. mutex_lock(&opts->lock);
  582. opts->refcnt--;
  583. mutex_unlock(&opts->lock);
  584. usb_free_all_descriptors(f);
  585. kfree(func_to_ss(f));
  586. }
  587. /* optionally require specific source/sink data patterns */
  588. static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
  589. {
  590. unsigned i;
  591. u8 *buf = req->buf;
  592. struct usb_composite_dev *cdev = ss->function.config->cdev;
  593. if (pattern == 2)
  594. return 0;
  595. for (i = 0; i < req->actual; i++, buf++) {
  596. switch (pattern) {
  597. /* all-zeroes has no synchronization issues */
  598. case 0:
  599. if (*buf == 0)
  600. continue;
  601. break;
  602. /* "mod63" stays in sync with short-terminated transfers,
  603. * OR otherwise when host and gadget agree on how large
  604. * each usb transfer request should be. Resync is done
  605. * with set_interface or set_config. (We *WANT* it to
  606. * get quickly out of sync if controllers or their drivers
  607. * stutter for any reason, including buffer duplication...)
  608. */
  609. case 1:
  610. if (*buf == (u8)(i % 63))
  611. continue;
  612. break;
  613. }
  614. ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
  615. usb_ep_set_halt(ss->out_ep);
  616. return -EINVAL;
  617. }
  618. return 0;
  619. }
  620. static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
  621. {
  622. unsigned i;
  623. u8 *buf = req->buf;
  624. switch (pattern) {
  625. case 0:
  626. memset(req->buf, 0, req->length);
  627. break;
  628. case 1:
  629. for (i = 0; i < req->length; i++)
  630. *buf++ = (u8) (i % 63);
  631. break;
  632. case 2:
  633. break;
  634. }
  635. }
  636. static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
  637. {
  638. struct usb_composite_dev *cdev;
  639. struct f_sourcesink *ss = ep->driver_data;
  640. int status = req->status;
  641. /* driver_data will be null if ep has been disabled */
  642. if (!ss)
  643. return;
  644. cdev = ss->function.config->cdev;
  645. switch (status) {
  646. case 0: /* normal completion? */
  647. if (ep == ss->out_ep) {
  648. check_read_data(ss, req);
  649. if (pattern != 2)
  650. memset(req->buf, 0x55, req->length);
  651. }
  652. break;
  653. /* this endpoint is normally active while we're configured */
  654. case -ECONNABORTED: /* hardware forced ep reset */
  655. case -ECONNRESET: /* request dequeued */
  656. case -ESHUTDOWN: /* disconnect from host */
  657. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  658. req->actual, req->length);
  659. if (ep == ss->out_ep)
  660. check_read_data(ss, req);
  661. free_ep_req(ep, req);
  662. return;
  663. case -EOVERFLOW: /* buffer overrun on read means that
  664. * we didn't provide a big enough
  665. * buffer.
  666. */
  667. default:
  668. #if 1
  669. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  670. status, req->actual, req->length);
  671. #endif
  672. case -EREMOTEIO: /* short read */
  673. break;
  674. }
  675. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  676. if (status) {
  677. ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
  678. ep->name, req->length, status);
  679. usb_ep_set_halt(ep);
  680. /* FIXME recover later ... somehow */
  681. }
  682. }
  683. static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
  684. enum eptype ep_type, int speed)
  685. {
  686. struct usb_ep *ep;
  687. struct usb_request *req;
  688. int i, size, status;
  689. for (i = 0; i < 8; i++) {
  690. switch (ep_type) {
  691. case EP_ISOC:
  692. switch (speed) {
  693. case USB_SPEED_SUPER:
  694. size = isoc_maxpacket * (isoc_mult + 1) *
  695. (isoc_maxburst + 1);
  696. break;
  697. case USB_SPEED_HIGH:
  698. size = isoc_maxpacket * (isoc_mult + 1);
  699. break;
  700. default:
  701. size = isoc_maxpacket > 1023 ?
  702. 1023 : isoc_maxpacket;
  703. break;
  704. }
  705. ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
  706. req = ss_alloc_ep_req(ep, size);
  707. break;
  708. case EP_INTERRUPT:
  709. switch (speed) {
  710. case USB_SPEED_SUPER:
  711. size = int_maxpacket * (int_mult + 1) *
  712. (int_maxburst + 1);
  713. break;
  714. case USB_SPEED_HIGH:
  715. size = int_maxpacket * (int_mult + 1);
  716. break;
  717. default:
  718. size = int_maxpacket > 1023 ?
  719. 1023 : int_maxpacket;
  720. break;
  721. }
  722. ep = is_in ? ss->int_in_ep : ss->int_out_ep;
  723. req = ss_alloc_ep_req(ep, size);
  724. break;
  725. default:
  726. ep = is_in ? ss->in_ep : ss->out_ep;
  727. req = ss_alloc_ep_req(ep, 0);
  728. break;
  729. }
  730. if (!req)
  731. return -ENOMEM;
  732. req->complete = source_sink_complete;
  733. if (is_in)
  734. reinit_write_data(ep, req);
  735. else if (pattern != 2)
  736. memset(req->buf, 0x55, req->length);
  737. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  738. if (status) {
  739. struct usb_composite_dev *cdev;
  740. cdev = ss->function.config->cdev;
  741. ERROR(cdev, "start %s%s %s --> %d\n",
  742. get_ep_string(ep_type), is_in ? "IN" : "OUT",
  743. ep->name, status);
  744. free_ep_req(ep, req);
  745. }
  746. if (!(ep_type == EP_ISOC))
  747. break;
  748. }
  749. return status;
  750. }
  751. static void disable_source_sink(struct f_sourcesink *ss)
  752. {
  753. struct usb_composite_dev *cdev;
  754. cdev = ss->function.config->cdev;
  755. disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
  756. ss->iso_out_ep, ss->int_in_ep, ss->int_out_ep);
  757. VDBG(cdev, "%s disabled\n", ss->function.name);
  758. }
  759. static int
  760. enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
  761. int alt)
  762. {
  763. int result = 0;
  764. int speed = cdev->gadget->speed;
  765. struct usb_ep *ep;
  766. if (alt == 2) {
  767. /* Configure for periodic interrupt endpoint */
  768. ep = ss->int_in_ep;
  769. if (ep) {
  770. result = config_ep_by_speed(cdev->gadget,
  771. &(ss->function), ep);
  772. if (result)
  773. return result;
  774. result = usb_ep_enable(ep);
  775. if (result < 0)
  776. return result;
  777. ep->driver_data = ss;
  778. result = source_sink_start_ep(ss, true, EP_INTERRUPT,
  779. speed);
  780. if (result < 0) {
  781. fail1:
  782. ep = ss->int_in_ep;
  783. if (ep) {
  784. usb_ep_disable(ep);
  785. ep->driver_data = NULL;
  786. }
  787. return result;
  788. }
  789. }
  790. /*
  791. * one interrupt endpoint reads (sinks) anything OUT (from the
  792. * host)
  793. */
  794. ep = ss->int_out_ep;
  795. if (ep) {
  796. result = config_ep_by_speed(cdev->gadget,
  797. &(ss->function), ep);
  798. if (result)
  799. goto fail1;
  800. result = usb_ep_enable(ep);
  801. if (result < 0)
  802. goto fail1;
  803. ep->driver_data = ss;
  804. result = source_sink_start_ep(ss, false, EP_INTERRUPT,
  805. speed);
  806. if (result < 0) {
  807. ep = ss->int_out_ep;
  808. usb_ep_disable(ep);
  809. ep->driver_data = NULL;
  810. goto fail1;
  811. }
  812. }
  813. goto out;
  814. }
  815. /* one bulk endpoint writes (sources) zeroes IN (to the host) */
  816. ep = ss->in_ep;
  817. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  818. if (result)
  819. return result;
  820. result = usb_ep_enable(ep);
  821. if (result < 0)
  822. return result;
  823. ep->driver_data = ss;
  824. result = source_sink_start_ep(ss, true, EP_BULK, speed);
  825. if (result < 0) {
  826. fail:
  827. ep = ss->in_ep;
  828. usb_ep_disable(ep);
  829. ep->driver_data = NULL;
  830. return result;
  831. }
  832. /* one bulk endpoint reads (sinks) anything OUT (from the host) */
  833. ep = ss->out_ep;
  834. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  835. if (result)
  836. goto fail;
  837. result = usb_ep_enable(ep);
  838. if (result < 0)
  839. goto fail;
  840. ep->driver_data = ss;
  841. result = source_sink_start_ep(ss, false, EP_BULK, speed);
  842. if (result < 0) {
  843. fail2:
  844. ep = ss->out_ep;
  845. usb_ep_disable(ep);
  846. ep->driver_data = NULL;
  847. goto fail;
  848. }
  849. if (alt == 0)
  850. goto out;
  851. /* one iso endpoint writes (sources) zeroes IN (to the host) */
  852. ep = ss->iso_in_ep;
  853. if (ep) {
  854. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  855. if (result)
  856. goto fail2;
  857. result = usb_ep_enable(ep);
  858. if (result < 0)
  859. goto fail2;
  860. ep->driver_data = ss;
  861. result = source_sink_start_ep(ss, true, EP_ISOC, speed);
  862. if (result < 0) {
  863. fail3:
  864. ep = ss->iso_in_ep;
  865. if (ep) {
  866. usb_ep_disable(ep);
  867. ep->driver_data = NULL;
  868. }
  869. goto fail2;
  870. }
  871. }
  872. /* one iso endpoint reads (sinks) anything OUT (from the host) */
  873. ep = ss->iso_out_ep;
  874. if (ep) {
  875. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  876. if (result)
  877. goto fail3;
  878. result = usb_ep_enable(ep);
  879. if (result < 0)
  880. goto fail3;
  881. ep->driver_data = ss;
  882. result = source_sink_start_ep(ss, false, EP_ISOC, speed);
  883. if (result < 0) {
  884. usb_ep_disable(ep);
  885. ep->driver_data = NULL;
  886. goto fail3;
  887. }
  888. }
  889. out:
  890. ss->cur_alt = alt;
  891. DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
  892. return result;
  893. }
  894. static int sourcesink_set_alt(struct usb_function *f,
  895. unsigned intf, unsigned alt)
  896. {
  897. struct f_sourcesink *ss = func_to_ss(f);
  898. struct usb_composite_dev *cdev = f->config->cdev;
  899. if (ss->in_ep->driver_data)
  900. disable_source_sink(ss);
  901. else if (alt == 2 && ss->int_in_ep->driver_data)
  902. disable_source_sink(ss);
  903. return enable_source_sink(cdev, ss, alt);
  904. }
  905. static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
  906. {
  907. struct f_sourcesink *ss = func_to_ss(f);
  908. return ss->cur_alt;
  909. }
  910. static void sourcesink_disable(struct usb_function *f)
  911. {
  912. struct f_sourcesink *ss = func_to_ss(f);
  913. disable_source_sink(ss);
  914. }
  915. /*-------------------------------------------------------------------------*/
  916. static int sourcesink_setup(struct usb_function *f,
  917. const struct usb_ctrlrequest *ctrl)
  918. {
  919. struct usb_configuration *c = f->config;
  920. struct usb_request *req = c->cdev->req;
  921. int value = -EOPNOTSUPP;
  922. u16 w_index = le16_to_cpu(ctrl->wIndex);
  923. u16 w_value = le16_to_cpu(ctrl->wValue);
  924. u16 w_length = le16_to_cpu(ctrl->wLength);
  925. req->length = USB_COMP_EP0_BUFSIZ;
  926. /* composite driver infrastructure handles everything except
  927. * the two control test requests.
  928. */
  929. switch (ctrl->bRequest) {
  930. /*
  931. * These are the same vendor-specific requests supported by
  932. * Intel's USB 2.0 compliance test devices. We exceed that
  933. * device spec by allowing multiple-packet requests.
  934. *
  935. * NOTE: the Control-OUT data stays in req->buf ... better
  936. * would be copying it into a scratch buffer, so that other
  937. * requests may safely intervene.
  938. */
  939. case 0x5b: /* control WRITE test -- fill the buffer */
  940. if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  941. goto unknown;
  942. if (w_value || w_index)
  943. break;
  944. /* just read that many bytes into the buffer */
  945. if (w_length > req->length)
  946. break;
  947. value = w_length;
  948. break;
  949. case 0x5c: /* control READ test -- return the buffer */
  950. if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  951. goto unknown;
  952. if (w_value || w_index)
  953. break;
  954. /* expect those bytes are still in the buffer; send back */
  955. if (w_length > req->length)
  956. break;
  957. value = w_length;
  958. break;
  959. default:
  960. unknown:
  961. VDBG(c->cdev,
  962. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  963. ctrl->bRequestType, ctrl->bRequest,
  964. w_value, w_index, w_length);
  965. }
  966. /* respond with data transfer or status phase? */
  967. if (value >= 0) {
  968. VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
  969. ctrl->bRequestType, ctrl->bRequest,
  970. w_value, w_index, w_length);
  971. req->zero = 0;
  972. req->length = value;
  973. value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
  974. if (value < 0)
  975. ERROR(c->cdev, "source/sink response, err %d\n",
  976. value);
  977. }
  978. /* device either stalls (value < 0) or reports success */
  979. return value;
  980. }
  981. static struct usb_function *source_sink_alloc_func(
  982. struct usb_function_instance *fi)
  983. {
  984. struct f_sourcesink *ss;
  985. struct f_ss_opts *ss_opts;
  986. ss = kzalloc(sizeof(*ss), GFP_KERNEL);
  987. if (!ss)
  988. return NULL;
  989. ss_opts = container_of(fi, struct f_ss_opts, func_inst);
  990. mutex_lock(&ss_opts->lock);
  991. ss_opts->refcnt++;
  992. mutex_unlock(&ss_opts->lock);
  993. pattern = ss_opts->pattern;
  994. isoc_interval = ss_opts->isoc_interval;
  995. isoc_maxpacket = ss_opts->isoc_maxpacket;
  996. isoc_mult = ss_opts->isoc_mult;
  997. isoc_maxburst = ss_opts->isoc_maxburst;
  998. int_interval = ss_opts->int_interval;
  999. int_maxpacket = ss_opts->int_maxpacket;
  1000. int_mult = ss_opts->int_mult;
  1001. int_maxburst = ss_opts->int_maxburst;
  1002. buflen = ss_opts->bulk_buflen;
  1003. ss->function.name = "source/sink";
  1004. ss->function.bind = sourcesink_bind;
  1005. ss->function.set_alt = sourcesink_set_alt;
  1006. ss->function.get_alt = sourcesink_get_alt;
  1007. ss->function.disable = sourcesink_disable;
  1008. ss->function.setup = sourcesink_setup;
  1009. ss->function.strings = sourcesink_strings;
  1010. ss->function.free_func = sourcesink_free_func;
  1011. return &ss->function;
  1012. }
  1013. static inline struct f_ss_opts *to_f_ss_opts(struct config_item *item)
  1014. {
  1015. return container_of(to_config_group(item), struct f_ss_opts,
  1016. func_inst.group);
  1017. }
  1018. CONFIGFS_ATTR_STRUCT(f_ss_opts);
  1019. CONFIGFS_ATTR_OPS(f_ss_opts);
  1020. static void ss_attr_release(struct config_item *item)
  1021. {
  1022. struct f_ss_opts *ss_opts = to_f_ss_opts(item);
  1023. usb_put_function_instance(&ss_opts->func_inst);
  1024. }
  1025. static struct configfs_item_operations ss_item_ops = {
  1026. .release = ss_attr_release,
  1027. .show_attribute = f_ss_opts_attr_show,
  1028. .store_attribute = f_ss_opts_attr_store,
  1029. };
  1030. static ssize_t f_ss_opts_pattern_show(struct f_ss_opts *opts, char *page)
  1031. {
  1032. int result;
  1033. mutex_lock(&opts->lock);
  1034. result = sprintf(page, "%d", opts->pattern);
  1035. mutex_unlock(&opts->lock);
  1036. return result;
  1037. }
  1038. static ssize_t f_ss_opts_pattern_store(struct f_ss_opts *opts,
  1039. const char *page, size_t len)
  1040. {
  1041. int ret;
  1042. u8 num;
  1043. mutex_lock(&opts->lock);
  1044. if (opts->refcnt) {
  1045. ret = -EBUSY;
  1046. goto end;
  1047. }
  1048. ret = kstrtou8(page, 0, &num);
  1049. if (ret)
  1050. goto end;
  1051. if (num != 0 && num != 1 && num != 2) {
  1052. ret = -EINVAL;
  1053. goto end;
  1054. }
  1055. opts->pattern = num;
  1056. ret = len;
  1057. end:
  1058. mutex_unlock(&opts->lock);
  1059. return ret;
  1060. }
  1061. static struct f_ss_opts_attribute f_ss_opts_pattern =
  1062. __CONFIGFS_ATTR(pattern, S_IRUGO | S_IWUSR,
  1063. f_ss_opts_pattern_show,
  1064. f_ss_opts_pattern_store);
  1065. static ssize_t f_ss_opts_isoc_interval_show(struct f_ss_opts *opts, char *page)
  1066. {
  1067. int result;
  1068. mutex_lock(&opts->lock);
  1069. result = sprintf(page, "%d", opts->isoc_interval);
  1070. mutex_unlock(&opts->lock);
  1071. return result;
  1072. }
  1073. static ssize_t f_ss_opts_isoc_interval_store(struct f_ss_opts *opts,
  1074. const char *page, size_t len)
  1075. {
  1076. int ret;
  1077. u8 num;
  1078. mutex_lock(&opts->lock);
  1079. if (opts->refcnt) {
  1080. ret = -EBUSY;
  1081. goto end;
  1082. }
  1083. ret = kstrtou8(page, 0, &num);
  1084. if (ret)
  1085. goto end;
  1086. if (num > 16) {
  1087. ret = -EINVAL;
  1088. goto end;
  1089. }
  1090. opts->isoc_interval = num;
  1091. ret = len;
  1092. end:
  1093. mutex_unlock(&opts->lock);
  1094. return ret;
  1095. }
  1096. static struct f_ss_opts_attribute f_ss_opts_isoc_interval =
  1097. __CONFIGFS_ATTR(isoc_interval, S_IRUGO | S_IWUSR,
  1098. f_ss_opts_isoc_interval_show,
  1099. f_ss_opts_isoc_interval_store);
  1100. static ssize_t f_ss_opts_isoc_maxpacket_show(struct f_ss_opts *opts, char *page)
  1101. {
  1102. int result;
  1103. mutex_lock(&opts->lock);
  1104. result = sprintf(page, "%d", opts->isoc_maxpacket);
  1105. mutex_unlock(&opts->lock);
  1106. return result;
  1107. }
  1108. static ssize_t f_ss_opts_isoc_maxpacket_store(struct f_ss_opts *opts,
  1109. const char *page, size_t len)
  1110. {
  1111. int ret;
  1112. u16 num;
  1113. mutex_lock(&opts->lock);
  1114. if (opts->refcnt) {
  1115. ret = -EBUSY;
  1116. goto end;
  1117. }
  1118. ret = kstrtou16(page, 0, &num);
  1119. if (ret)
  1120. goto end;
  1121. if (num > 1024) {
  1122. ret = -EINVAL;
  1123. goto end;
  1124. }
  1125. opts->isoc_maxpacket = num;
  1126. ret = len;
  1127. end:
  1128. mutex_unlock(&opts->lock);
  1129. return ret;
  1130. }
  1131. static struct f_ss_opts_attribute f_ss_opts_isoc_maxpacket =
  1132. __CONFIGFS_ATTR(isoc_maxpacket, S_IRUGO | S_IWUSR,
  1133. f_ss_opts_isoc_maxpacket_show,
  1134. f_ss_opts_isoc_maxpacket_store);
  1135. static ssize_t f_ss_opts_isoc_mult_show(struct f_ss_opts *opts, char *page)
  1136. {
  1137. int result;
  1138. mutex_lock(&opts->lock);
  1139. result = sprintf(page, "%d", opts->isoc_mult);
  1140. mutex_unlock(&opts->lock);
  1141. return result;
  1142. }
  1143. static ssize_t f_ss_opts_isoc_mult_store(struct f_ss_opts *opts,
  1144. const char *page, size_t len)
  1145. {
  1146. int ret;
  1147. u8 num;
  1148. mutex_lock(&opts->lock);
  1149. if (opts->refcnt) {
  1150. ret = -EBUSY;
  1151. goto end;
  1152. }
  1153. ret = kstrtou8(page, 0, &num);
  1154. if (ret)
  1155. goto end;
  1156. if (num > 2) {
  1157. ret = -EINVAL;
  1158. goto end;
  1159. }
  1160. opts->isoc_mult = num;
  1161. ret = len;
  1162. end:
  1163. mutex_unlock(&opts->lock);
  1164. return ret;
  1165. }
  1166. static struct f_ss_opts_attribute f_ss_opts_isoc_mult =
  1167. __CONFIGFS_ATTR(isoc_mult, S_IRUGO | S_IWUSR,
  1168. f_ss_opts_isoc_mult_show,
  1169. f_ss_opts_isoc_mult_store);
  1170. static ssize_t f_ss_opts_isoc_maxburst_show(struct f_ss_opts *opts, char *page)
  1171. {
  1172. int result;
  1173. mutex_lock(&opts->lock);
  1174. result = sprintf(page, "%d", opts->isoc_maxburst);
  1175. mutex_unlock(&opts->lock);
  1176. return result;
  1177. }
  1178. static ssize_t f_ss_opts_isoc_maxburst_store(struct f_ss_opts *opts,
  1179. const char *page, size_t len)
  1180. {
  1181. int ret;
  1182. u8 num;
  1183. mutex_lock(&opts->lock);
  1184. if (opts->refcnt) {
  1185. ret = -EBUSY;
  1186. goto end;
  1187. }
  1188. ret = kstrtou8(page, 0, &num);
  1189. if (ret)
  1190. goto end;
  1191. if (num > 15) {
  1192. ret = -EINVAL;
  1193. goto end;
  1194. }
  1195. opts->isoc_maxburst = num;
  1196. ret = len;
  1197. end:
  1198. mutex_unlock(&opts->lock);
  1199. return ret;
  1200. }
  1201. static struct f_ss_opts_attribute f_ss_opts_isoc_maxburst =
  1202. __CONFIGFS_ATTR(isoc_maxburst, S_IRUGO | S_IWUSR,
  1203. f_ss_opts_isoc_maxburst_show,
  1204. f_ss_opts_isoc_maxburst_store);
  1205. static ssize_t f_ss_opts_bulk_buflen_show(struct f_ss_opts *opts, char *page)
  1206. {
  1207. int result;
  1208. mutex_lock(&opts->lock);
  1209. result = sprintf(page, "%d", opts->bulk_buflen);
  1210. mutex_unlock(&opts->lock);
  1211. return result;
  1212. }
  1213. static ssize_t f_ss_opts_bulk_buflen_store(struct f_ss_opts *opts,
  1214. const char *page, size_t len)
  1215. {
  1216. int ret;
  1217. u32 num;
  1218. mutex_lock(&opts->lock);
  1219. if (opts->refcnt) {
  1220. ret = -EBUSY;
  1221. goto end;
  1222. }
  1223. ret = kstrtou32(page, 0, &num);
  1224. if (ret)
  1225. goto end;
  1226. opts->bulk_buflen = num;
  1227. ret = len;
  1228. end:
  1229. mutex_unlock(&opts->lock);
  1230. return ret;
  1231. }
  1232. static struct f_ss_opts_attribute f_ss_opts_bulk_buflen =
  1233. __CONFIGFS_ATTR(buflen, S_IRUGO | S_IWUSR,
  1234. f_ss_opts_bulk_buflen_show,
  1235. f_ss_opts_bulk_buflen_store);
  1236. static ssize_t f_ss_opts_int_interval_show(struct f_ss_opts *opts, char *page)
  1237. {
  1238. int result;
  1239. mutex_lock(&opts->lock);
  1240. result = sprintf(page, "%d", opts->int_interval);
  1241. mutex_unlock(&opts->lock);
  1242. return result;
  1243. }
  1244. static ssize_t f_ss_opts_int_interval_store(struct f_ss_opts *opts,
  1245. const char *page, size_t len)
  1246. {
  1247. int ret;
  1248. u32 num;
  1249. mutex_lock(&opts->lock);
  1250. if (opts->refcnt) {
  1251. ret = -EBUSY;
  1252. goto end;
  1253. }
  1254. ret = kstrtou32(page, 0, &num);
  1255. if (ret)
  1256. goto end;
  1257. if (num > 4096) {
  1258. ret = -EINVAL;
  1259. goto end;
  1260. }
  1261. opts->int_interval = num;
  1262. ret = len;
  1263. end:
  1264. mutex_unlock(&opts->lock);
  1265. return ret;
  1266. }
  1267. static struct f_ss_opts_attribute f_ss_opts_int_interval =
  1268. __CONFIGFS_ATTR(int_interval, S_IRUGO | S_IWUSR,
  1269. f_ss_opts_int_interval_show,
  1270. f_ss_opts_int_interval_store);
  1271. static ssize_t f_ss_opts_int_maxpacket_show(struct f_ss_opts *opts, char *page)
  1272. {
  1273. int result;
  1274. mutex_lock(&opts->lock);
  1275. result = sprintf(page, "%d", opts->int_maxpacket);
  1276. mutex_unlock(&opts->lock);
  1277. return result;
  1278. }
  1279. static ssize_t f_ss_opts_int_maxpacket_store(struct f_ss_opts *opts,
  1280. const char *page, size_t len)
  1281. {
  1282. int ret;
  1283. u16 num;
  1284. mutex_lock(&opts->lock);
  1285. if (opts->refcnt) {
  1286. ret = -EBUSY;
  1287. goto end;
  1288. }
  1289. ret = kstrtou16(page, 0, &num);
  1290. if (ret)
  1291. goto end;
  1292. if (num > 1024) {
  1293. ret = -EINVAL;
  1294. goto end;
  1295. }
  1296. opts->int_maxpacket = num;
  1297. ret = len;
  1298. end:
  1299. mutex_unlock(&opts->lock);
  1300. return ret;
  1301. }
  1302. static struct f_ss_opts_attribute f_ss_opts_int_maxpacket =
  1303. __CONFIGFS_ATTR(int_maxpacket, S_IRUGO | S_IWUSR,
  1304. f_ss_opts_int_maxpacket_show,
  1305. f_ss_opts_int_maxpacket_store);
  1306. static ssize_t f_ss_opts_int_mult_show(struct f_ss_opts *opts, char *page)
  1307. {
  1308. int result;
  1309. mutex_lock(&opts->lock);
  1310. result = sprintf(page, "%d", opts->int_mult);
  1311. mutex_unlock(&opts->lock);
  1312. return result;
  1313. }
  1314. static ssize_t f_ss_opts_int_mult_store(struct f_ss_opts *opts,
  1315. const char *page, size_t len)
  1316. {
  1317. int ret;
  1318. u8 num;
  1319. mutex_lock(&opts->lock);
  1320. if (opts->refcnt) {
  1321. ret = -EBUSY;
  1322. goto end;
  1323. }
  1324. ret = kstrtou8(page, 0, &num);
  1325. if (ret)
  1326. goto end;
  1327. if (num > 2) {
  1328. ret = -EINVAL;
  1329. goto end;
  1330. }
  1331. opts->int_mult = num;
  1332. ret = len;
  1333. end:
  1334. mutex_unlock(&opts->lock);
  1335. return ret;
  1336. }
  1337. static struct f_ss_opts_attribute f_ss_opts_int_mult =
  1338. __CONFIGFS_ATTR(int_mult, S_IRUGO | S_IWUSR,
  1339. f_ss_opts_int_mult_show,
  1340. f_ss_opts_int_mult_store);
  1341. static ssize_t f_ss_opts_int_maxburst_show(struct f_ss_opts *opts, char *page)
  1342. {
  1343. int result;
  1344. mutex_lock(&opts->lock);
  1345. result = sprintf(page, "%d", opts->int_maxburst);
  1346. mutex_unlock(&opts->lock);
  1347. return result;
  1348. }
  1349. static ssize_t f_ss_opts_int_maxburst_store(struct f_ss_opts *opts,
  1350. const char *page, size_t len)
  1351. {
  1352. int ret;
  1353. u8 num;
  1354. mutex_lock(&opts->lock);
  1355. if (opts->refcnt) {
  1356. ret = -EBUSY;
  1357. goto end;
  1358. }
  1359. ret = kstrtou8(page, 0, &num);
  1360. if (ret)
  1361. goto end;
  1362. if (num > 15) {
  1363. ret = -EINVAL;
  1364. goto end;
  1365. }
  1366. opts->int_maxburst = num;
  1367. ret = len;
  1368. end:
  1369. mutex_unlock(&opts->lock);
  1370. return ret;
  1371. }
  1372. static struct f_ss_opts_attribute f_ss_opts_int_maxburst =
  1373. __CONFIGFS_ATTR(int_maxburst, S_IRUGO | S_IWUSR,
  1374. f_ss_opts_int_maxburst_show,
  1375. f_ss_opts_int_maxburst_store);
  1376. static struct configfs_attribute *ss_attrs[] = {
  1377. &f_ss_opts_pattern.attr,
  1378. &f_ss_opts_isoc_interval.attr,
  1379. &f_ss_opts_isoc_maxpacket.attr,
  1380. &f_ss_opts_isoc_mult.attr,
  1381. &f_ss_opts_isoc_maxburst.attr,
  1382. &f_ss_opts_bulk_buflen.attr,
  1383. &f_ss_opts_int_interval.attr,
  1384. &f_ss_opts_int_maxpacket.attr,
  1385. &f_ss_opts_int_mult.attr,
  1386. &f_ss_opts_int_maxburst.attr,
  1387. NULL,
  1388. };
  1389. static struct config_item_type ss_func_type = {
  1390. .ct_item_ops = &ss_item_ops,
  1391. .ct_attrs = ss_attrs,
  1392. .ct_owner = THIS_MODULE,
  1393. };
  1394. static void source_sink_free_instance(struct usb_function_instance *fi)
  1395. {
  1396. struct f_ss_opts *ss_opts;
  1397. ss_opts = container_of(fi, struct f_ss_opts, func_inst);
  1398. kfree(ss_opts);
  1399. }
  1400. static struct usb_function_instance *source_sink_alloc_inst(void)
  1401. {
  1402. struct f_ss_opts *ss_opts;
  1403. ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL);
  1404. if (!ss_opts)
  1405. return ERR_PTR(-ENOMEM);
  1406. mutex_init(&ss_opts->lock);
  1407. ss_opts->func_inst.free_func_inst = source_sink_free_instance;
  1408. ss_opts->isoc_interval = GZERO_ISOC_INTERVAL;
  1409. ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET;
  1410. ss_opts->bulk_buflen = GZERO_BULK_BUFLEN;
  1411. ss_opts->int_interval = GZERO_INT_INTERVAL;
  1412. ss_opts->int_maxpacket = GZERO_INT_MAXPACKET;
  1413. config_group_init_type_name(&ss_opts->func_inst.group, "",
  1414. &ss_func_type);
  1415. return &ss_opts->func_inst;
  1416. }
  1417. DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst,
  1418. source_sink_alloc_func);
  1419. static int __init sslb_modinit(void)
  1420. {
  1421. int ret;
  1422. ret = usb_function_register(&SourceSinkusb_func);
  1423. if (ret)
  1424. return ret;
  1425. ret = lb_modinit();
  1426. if (ret)
  1427. usb_function_unregister(&SourceSinkusb_func);
  1428. return ret;
  1429. }
  1430. static void __exit sslb_modexit(void)
  1431. {
  1432. usb_function_unregister(&SourceSinkusb_func);
  1433. lb_modexit();
  1434. }
  1435. module_init(sslb_modinit);
  1436. module_exit(sslb_modexit);
  1437. MODULE_LICENSE("GPL");