f_sourcesink.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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. /*
  23. * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
  24. * controller drivers.
  25. *
  26. * This just sinks bulk packets OUT to the peripheral and sources them IN
  27. * to the host, optionally with specific data patterns for integrity tests.
  28. * As such it supports basic functionality and load tests.
  29. *
  30. * In terms of control messaging, this supports all the standard requests
  31. * plus two that support control-OUT tests. If the optional "autoresume"
  32. * mode is enabled, it provides good functional coverage for the "USBCV"
  33. * test harness from USB-IF.
  34. *
  35. * Note that because this doesn't queue more than one request at a time,
  36. * some other function must be used to test queueing logic. The network
  37. * link (g_ether) is the best overall option for that, since its TX and RX
  38. * queues are relatively independent, will receive a range of packet sizes,
  39. * and can often be made to run out completely. Those issues are important
  40. * when stress testing peripheral controller drivers.
  41. *
  42. *
  43. * This is currently packaged as a configuration driver, which can't be
  44. * combined with other functions to make composite devices. However, it
  45. * can be combined with other independent configurations.
  46. */
  47. struct f_sourcesink {
  48. struct usb_function function;
  49. struct usb_ep *in_ep;
  50. struct usb_ep *out_ep;
  51. struct usb_ep *iso_in_ep;
  52. struct usb_ep *iso_out_ep;
  53. int cur_alt;
  54. };
  55. static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
  56. {
  57. return container_of(f, struct f_sourcesink, function);
  58. }
  59. static unsigned pattern;
  60. static unsigned isoc_interval;
  61. static unsigned isoc_maxpacket;
  62. static unsigned isoc_mult;
  63. static unsigned isoc_maxburst;
  64. static unsigned buflen;
  65. /*-------------------------------------------------------------------------*/
  66. static struct usb_interface_descriptor source_sink_intf_alt0 = {
  67. .bLength = USB_DT_INTERFACE_SIZE,
  68. .bDescriptorType = USB_DT_INTERFACE,
  69. .bAlternateSetting = 0,
  70. .bNumEndpoints = 2,
  71. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  72. /* .iInterface = DYNAMIC */
  73. };
  74. static struct usb_interface_descriptor source_sink_intf_alt1 = {
  75. .bLength = USB_DT_INTERFACE_SIZE,
  76. .bDescriptorType = USB_DT_INTERFACE,
  77. .bAlternateSetting = 1,
  78. .bNumEndpoints = 4,
  79. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  80. /* .iInterface = DYNAMIC */
  81. };
  82. /* full speed support: */
  83. static struct usb_endpoint_descriptor fs_source_desc = {
  84. .bLength = USB_DT_ENDPOINT_SIZE,
  85. .bDescriptorType = USB_DT_ENDPOINT,
  86. .bEndpointAddress = USB_DIR_IN,
  87. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  88. };
  89. static struct usb_endpoint_descriptor fs_sink_desc = {
  90. .bLength = USB_DT_ENDPOINT_SIZE,
  91. .bDescriptorType = USB_DT_ENDPOINT,
  92. .bEndpointAddress = USB_DIR_OUT,
  93. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  94. };
  95. static struct usb_endpoint_descriptor fs_iso_source_desc = {
  96. .bLength = USB_DT_ENDPOINT_SIZE,
  97. .bDescriptorType = USB_DT_ENDPOINT,
  98. .bEndpointAddress = USB_DIR_IN,
  99. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  100. .wMaxPacketSize = cpu_to_le16(1023),
  101. .bInterval = 4,
  102. };
  103. static struct usb_endpoint_descriptor fs_iso_sink_desc = {
  104. .bLength = USB_DT_ENDPOINT_SIZE,
  105. .bDescriptorType = USB_DT_ENDPOINT,
  106. .bEndpointAddress = USB_DIR_OUT,
  107. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  108. .wMaxPacketSize = cpu_to_le16(1023),
  109. .bInterval = 4,
  110. };
  111. static struct usb_descriptor_header *fs_source_sink_descs[] = {
  112. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  113. (struct usb_descriptor_header *) &fs_sink_desc,
  114. (struct usb_descriptor_header *) &fs_source_desc,
  115. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  116. #define FS_ALT_IFC_1_OFFSET 3
  117. (struct usb_descriptor_header *) &fs_sink_desc,
  118. (struct usb_descriptor_header *) &fs_source_desc,
  119. (struct usb_descriptor_header *) &fs_iso_sink_desc,
  120. (struct usb_descriptor_header *) &fs_iso_source_desc,
  121. NULL,
  122. };
  123. /* high speed support: */
  124. static struct usb_endpoint_descriptor hs_source_desc = {
  125. .bLength = USB_DT_ENDPOINT_SIZE,
  126. .bDescriptorType = USB_DT_ENDPOINT,
  127. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  128. .wMaxPacketSize = cpu_to_le16(512),
  129. };
  130. static struct usb_endpoint_descriptor hs_sink_desc = {
  131. .bLength = USB_DT_ENDPOINT_SIZE,
  132. .bDescriptorType = USB_DT_ENDPOINT,
  133. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  134. .wMaxPacketSize = cpu_to_le16(512),
  135. };
  136. static struct usb_endpoint_descriptor hs_iso_source_desc = {
  137. .bLength = USB_DT_ENDPOINT_SIZE,
  138. .bDescriptorType = USB_DT_ENDPOINT,
  139. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  140. .wMaxPacketSize = cpu_to_le16(1024),
  141. .bInterval = 4,
  142. };
  143. static struct usb_endpoint_descriptor hs_iso_sink_desc = {
  144. .bLength = USB_DT_ENDPOINT_SIZE,
  145. .bDescriptorType = USB_DT_ENDPOINT,
  146. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  147. .wMaxPacketSize = cpu_to_le16(1024),
  148. .bInterval = 4,
  149. };
  150. static struct usb_descriptor_header *hs_source_sink_descs[] = {
  151. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  152. (struct usb_descriptor_header *) &hs_source_desc,
  153. (struct usb_descriptor_header *) &hs_sink_desc,
  154. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  155. #define HS_ALT_IFC_1_OFFSET 3
  156. (struct usb_descriptor_header *) &hs_source_desc,
  157. (struct usb_descriptor_header *) &hs_sink_desc,
  158. (struct usb_descriptor_header *) &hs_iso_source_desc,
  159. (struct usb_descriptor_header *) &hs_iso_sink_desc,
  160. NULL,
  161. };
  162. /* super speed support: */
  163. static struct usb_endpoint_descriptor ss_source_desc = {
  164. .bLength = USB_DT_ENDPOINT_SIZE,
  165. .bDescriptorType = USB_DT_ENDPOINT,
  166. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  167. .wMaxPacketSize = cpu_to_le16(1024),
  168. };
  169. static struct usb_ss_ep_comp_descriptor ss_source_comp_desc = {
  170. .bLength = USB_DT_SS_EP_COMP_SIZE,
  171. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  172. .bMaxBurst = 0,
  173. .bmAttributes = 0,
  174. .wBytesPerInterval = 0,
  175. };
  176. static struct usb_endpoint_descriptor ss_sink_desc = {
  177. .bLength = USB_DT_ENDPOINT_SIZE,
  178. .bDescriptorType = USB_DT_ENDPOINT,
  179. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  180. .wMaxPacketSize = cpu_to_le16(1024),
  181. };
  182. static struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = {
  183. .bLength = USB_DT_SS_EP_COMP_SIZE,
  184. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  185. .bMaxBurst = 0,
  186. .bmAttributes = 0,
  187. .wBytesPerInterval = 0,
  188. };
  189. static struct usb_endpoint_descriptor ss_iso_source_desc = {
  190. .bLength = USB_DT_ENDPOINT_SIZE,
  191. .bDescriptorType = USB_DT_ENDPOINT,
  192. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  193. .wMaxPacketSize = cpu_to_le16(1024),
  194. .bInterval = 4,
  195. };
  196. static struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc = {
  197. .bLength = USB_DT_SS_EP_COMP_SIZE,
  198. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  199. .bMaxBurst = 0,
  200. .bmAttributes = 0,
  201. .wBytesPerInterval = cpu_to_le16(1024),
  202. };
  203. static struct usb_endpoint_descriptor ss_iso_sink_desc = {
  204. .bLength = USB_DT_ENDPOINT_SIZE,
  205. .bDescriptorType = USB_DT_ENDPOINT,
  206. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  207. .wMaxPacketSize = cpu_to_le16(1024),
  208. .bInterval = 4,
  209. };
  210. static struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = {
  211. .bLength = USB_DT_SS_EP_COMP_SIZE,
  212. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  213. .bMaxBurst = 0,
  214. .bmAttributes = 0,
  215. .wBytesPerInterval = cpu_to_le16(1024),
  216. };
  217. static struct usb_descriptor_header *ss_source_sink_descs[] = {
  218. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  219. (struct usb_descriptor_header *) &ss_source_desc,
  220. (struct usb_descriptor_header *) &ss_source_comp_desc,
  221. (struct usb_descriptor_header *) &ss_sink_desc,
  222. (struct usb_descriptor_header *) &ss_sink_comp_desc,
  223. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  224. #define SS_ALT_IFC_1_OFFSET 5
  225. (struct usb_descriptor_header *) &ss_source_desc,
  226. (struct usb_descriptor_header *) &ss_source_comp_desc,
  227. (struct usb_descriptor_header *) &ss_sink_desc,
  228. (struct usb_descriptor_header *) &ss_sink_comp_desc,
  229. (struct usb_descriptor_header *) &ss_iso_source_desc,
  230. (struct usb_descriptor_header *) &ss_iso_source_comp_desc,
  231. (struct usb_descriptor_header *) &ss_iso_sink_desc,
  232. (struct usb_descriptor_header *) &ss_iso_sink_comp_desc,
  233. NULL,
  234. };
  235. /* function-specific strings: */
  236. static struct usb_string strings_sourcesink[] = {
  237. [0].s = "source and sink data",
  238. { } /* end of list */
  239. };
  240. static struct usb_gadget_strings stringtab_sourcesink = {
  241. .language = 0x0409, /* en-us */
  242. .strings = strings_sourcesink,
  243. };
  244. static struct usb_gadget_strings *sourcesink_strings[] = {
  245. &stringtab_sourcesink,
  246. NULL,
  247. };
  248. /*-------------------------------------------------------------------------*/
  249. static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len)
  250. {
  251. return alloc_ep_req(ep, len, buflen);
  252. }
  253. void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  254. {
  255. kfree(req->buf);
  256. usb_ep_free_request(ep, req);
  257. }
  258. static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
  259. {
  260. int value;
  261. if (ep->driver_data) {
  262. value = usb_ep_disable(ep);
  263. if (value < 0)
  264. DBG(cdev, "disable %s --> %d\n",
  265. ep->name, value);
  266. ep->driver_data = NULL;
  267. }
  268. }
  269. void disable_endpoints(struct usb_composite_dev *cdev,
  270. struct usb_ep *in, struct usb_ep *out,
  271. struct usb_ep *iso_in, struct usb_ep *iso_out)
  272. {
  273. disable_ep(cdev, in);
  274. disable_ep(cdev, out);
  275. if (iso_in)
  276. disable_ep(cdev, iso_in);
  277. if (iso_out)
  278. disable_ep(cdev, iso_out);
  279. }
  280. static int
  281. sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
  282. {
  283. struct usb_composite_dev *cdev = c->cdev;
  284. struct f_sourcesink *ss = func_to_ss(f);
  285. int id;
  286. int ret;
  287. /* allocate interface ID(s) */
  288. id = usb_interface_id(c, f);
  289. if (id < 0)
  290. return id;
  291. source_sink_intf_alt0.bInterfaceNumber = id;
  292. source_sink_intf_alt1.bInterfaceNumber = id;
  293. /* allocate bulk endpoints */
  294. ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
  295. if (!ss->in_ep) {
  296. autoconf_fail:
  297. ERROR(cdev, "%s: can't autoconfigure on %s\n",
  298. f->name, cdev->gadget->name);
  299. return -ENODEV;
  300. }
  301. ss->in_ep->driver_data = cdev; /* claim */
  302. ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
  303. if (!ss->out_ep)
  304. goto autoconf_fail;
  305. ss->out_ep->driver_data = cdev; /* claim */
  306. /* sanity check the isoc module parameters */
  307. if (isoc_interval < 1)
  308. isoc_interval = 1;
  309. if (isoc_interval > 16)
  310. isoc_interval = 16;
  311. if (isoc_mult > 2)
  312. isoc_mult = 2;
  313. if (isoc_maxburst > 15)
  314. isoc_maxburst = 15;
  315. /* fill in the FS isoc descriptors from the module parameters */
  316. fs_iso_source_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
  317. 1023 : isoc_maxpacket;
  318. fs_iso_source_desc.bInterval = isoc_interval;
  319. fs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
  320. 1023 : isoc_maxpacket;
  321. fs_iso_sink_desc.bInterval = isoc_interval;
  322. /* allocate iso endpoints */
  323. ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
  324. if (!ss->iso_in_ep)
  325. goto no_iso;
  326. ss->iso_in_ep->driver_data = cdev; /* claim */
  327. ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
  328. if (ss->iso_out_ep) {
  329. ss->iso_out_ep->driver_data = cdev; /* claim */
  330. } else {
  331. ss->iso_in_ep->driver_data = NULL;
  332. ss->iso_in_ep = NULL;
  333. no_iso:
  334. /*
  335. * We still want to work even if the UDC doesn't have isoc
  336. * endpoints, so null out the alt interface that contains
  337. * them and continue.
  338. */
  339. fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
  340. hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
  341. ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
  342. }
  343. if (isoc_maxpacket > 1024)
  344. isoc_maxpacket = 1024;
  345. /* support high speed hardware */
  346. hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  347. hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  348. /*
  349. * Fill in the HS isoc descriptors from the module parameters.
  350. * We assume that the user knows what they are doing and won't
  351. * give parameters that their UDC doesn't support.
  352. */
  353. hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
  354. hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11;
  355. hs_iso_source_desc.bInterval = isoc_interval;
  356. hs_iso_source_desc.bEndpointAddress =
  357. fs_iso_source_desc.bEndpointAddress;
  358. hs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
  359. hs_iso_sink_desc.wMaxPacketSize |= isoc_mult << 11;
  360. hs_iso_sink_desc.bInterval = isoc_interval;
  361. hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
  362. /* support super speed hardware */
  363. ss_source_desc.bEndpointAddress =
  364. fs_source_desc.bEndpointAddress;
  365. ss_sink_desc.bEndpointAddress =
  366. fs_sink_desc.bEndpointAddress;
  367. /*
  368. * Fill in the SS isoc descriptors from the module parameters.
  369. * We assume that the user knows what they are doing and won't
  370. * give parameters that their UDC doesn't support.
  371. */
  372. ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
  373. ss_iso_source_desc.bInterval = isoc_interval;
  374. ss_iso_source_comp_desc.bmAttributes = isoc_mult;
  375. ss_iso_source_comp_desc.bMaxBurst = isoc_maxburst;
  376. ss_iso_source_comp_desc.wBytesPerInterval =
  377. isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
  378. ss_iso_source_desc.bEndpointAddress =
  379. fs_iso_source_desc.bEndpointAddress;
  380. ss_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
  381. ss_iso_sink_desc.bInterval = isoc_interval;
  382. ss_iso_sink_comp_desc.bmAttributes = isoc_mult;
  383. ss_iso_sink_comp_desc.bMaxBurst = isoc_maxburst;
  384. ss_iso_sink_comp_desc.wBytesPerInterval =
  385. isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
  386. ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
  387. ret = usb_assign_descriptors(f, fs_source_sink_descs,
  388. hs_source_sink_descs, ss_source_sink_descs);
  389. if (ret)
  390. return ret;
  391. DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
  392. (gadget_is_superspeed(c->cdev->gadget) ? "super" :
  393. (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
  394. f->name, ss->in_ep->name, ss->out_ep->name,
  395. ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
  396. ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
  397. return 0;
  398. }
  399. static void
  400. sourcesink_free_func(struct usb_function *f)
  401. {
  402. struct f_ss_opts *opts;
  403. opts = container_of(f->fi, struct f_ss_opts, func_inst);
  404. mutex_lock(&opts->lock);
  405. opts->refcnt--;
  406. mutex_unlock(&opts->lock);
  407. usb_free_all_descriptors(f);
  408. kfree(func_to_ss(f));
  409. }
  410. /* optionally require specific source/sink data patterns */
  411. static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
  412. {
  413. unsigned i;
  414. u8 *buf = req->buf;
  415. struct usb_composite_dev *cdev = ss->function.config->cdev;
  416. if (pattern == 2)
  417. return 0;
  418. for (i = 0; i < req->actual; i++, buf++) {
  419. switch (pattern) {
  420. /* all-zeroes has no synchronization issues */
  421. case 0:
  422. if (*buf == 0)
  423. continue;
  424. break;
  425. /* "mod63" stays in sync with short-terminated transfers,
  426. * OR otherwise when host and gadget agree on how large
  427. * each usb transfer request should be. Resync is done
  428. * with set_interface or set_config. (We *WANT* it to
  429. * get quickly out of sync if controllers or their drivers
  430. * stutter for any reason, including buffer duplication...)
  431. */
  432. case 1:
  433. if (*buf == (u8)(i % 63))
  434. continue;
  435. break;
  436. }
  437. ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
  438. usb_ep_set_halt(ss->out_ep);
  439. return -EINVAL;
  440. }
  441. return 0;
  442. }
  443. static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
  444. {
  445. unsigned i;
  446. u8 *buf = req->buf;
  447. switch (pattern) {
  448. case 0:
  449. memset(req->buf, 0, req->length);
  450. break;
  451. case 1:
  452. for (i = 0; i < req->length; i++)
  453. *buf++ = (u8) (i % 63);
  454. break;
  455. case 2:
  456. break;
  457. }
  458. }
  459. static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
  460. {
  461. struct usb_composite_dev *cdev;
  462. struct f_sourcesink *ss = ep->driver_data;
  463. int status = req->status;
  464. /* driver_data will be null if ep has been disabled */
  465. if (!ss)
  466. return;
  467. cdev = ss->function.config->cdev;
  468. switch (status) {
  469. case 0: /* normal completion? */
  470. if (ep == ss->out_ep) {
  471. check_read_data(ss, req);
  472. if (pattern != 2)
  473. memset(req->buf, 0x55, req->length);
  474. }
  475. break;
  476. /* this endpoint is normally active while we're configured */
  477. case -ECONNABORTED: /* hardware forced ep reset */
  478. case -ECONNRESET: /* request dequeued */
  479. case -ESHUTDOWN: /* disconnect from host */
  480. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  481. req->actual, req->length);
  482. if (ep == ss->out_ep)
  483. check_read_data(ss, req);
  484. free_ep_req(ep, req);
  485. return;
  486. case -EOVERFLOW: /* buffer overrun on read means that
  487. * we didn't provide a big enough
  488. * buffer.
  489. */
  490. default:
  491. #if 1
  492. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  493. status, req->actual, req->length);
  494. #endif
  495. case -EREMOTEIO: /* short read */
  496. break;
  497. }
  498. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  499. if (status) {
  500. ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
  501. ep->name, req->length, status);
  502. usb_ep_set_halt(ep);
  503. /* FIXME recover later ... somehow */
  504. }
  505. }
  506. static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
  507. bool is_iso, int speed)
  508. {
  509. struct usb_ep *ep;
  510. struct usb_request *req;
  511. int i, size, status;
  512. for (i = 0; i < 8; i++) {
  513. if (is_iso) {
  514. switch (speed) {
  515. case USB_SPEED_SUPER:
  516. size = isoc_maxpacket * (isoc_mult + 1) *
  517. (isoc_maxburst + 1);
  518. break;
  519. case USB_SPEED_HIGH:
  520. size = isoc_maxpacket * (isoc_mult + 1);
  521. break;
  522. default:
  523. size = isoc_maxpacket > 1023 ?
  524. 1023 : isoc_maxpacket;
  525. break;
  526. }
  527. ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
  528. req = ss_alloc_ep_req(ep, size);
  529. } else {
  530. ep = is_in ? ss->in_ep : ss->out_ep;
  531. req = ss_alloc_ep_req(ep, 0);
  532. }
  533. if (!req)
  534. return -ENOMEM;
  535. req->complete = source_sink_complete;
  536. if (is_in)
  537. reinit_write_data(ep, req);
  538. else if (pattern != 2)
  539. memset(req->buf, 0x55, req->length);
  540. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  541. if (status) {
  542. struct usb_composite_dev *cdev;
  543. cdev = ss->function.config->cdev;
  544. ERROR(cdev, "start %s%s %s --> %d\n",
  545. is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
  546. ep->name, status);
  547. free_ep_req(ep, req);
  548. }
  549. if (!is_iso)
  550. break;
  551. }
  552. return status;
  553. }
  554. static void disable_source_sink(struct f_sourcesink *ss)
  555. {
  556. struct usb_composite_dev *cdev;
  557. cdev = ss->function.config->cdev;
  558. disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
  559. ss->iso_out_ep);
  560. VDBG(cdev, "%s disabled\n", ss->function.name);
  561. }
  562. static int
  563. enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
  564. int alt)
  565. {
  566. int result = 0;
  567. int speed = cdev->gadget->speed;
  568. struct usb_ep *ep;
  569. /* one bulk endpoint writes (sources) zeroes IN (to the host) */
  570. ep = ss->in_ep;
  571. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  572. if (result)
  573. return result;
  574. result = usb_ep_enable(ep);
  575. if (result < 0)
  576. return result;
  577. ep->driver_data = ss;
  578. result = source_sink_start_ep(ss, true, false, speed);
  579. if (result < 0) {
  580. fail:
  581. ep = ss->in_ep;
  582. usb_ep_disable(ep);
  583. ep->driver_data = NULL;
  584. return result;
  585. }
  586. /* one bulk endpoint reads (sinks) anything OUT (from the host) */
  587. ep = ss->out_ep;
  588. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  589. if (result)
  590. goto fail;
  591. result = usb_ep_enable(ep);
  592. if (result < 0)
  593. goto fail;
  594. ep->driver_data = ss;
  595. result = source_sink_start_ep(ss, false, false, speed);
  596. if (result < 0) {
  597. fail2:
  598. ep = ss->out_ep;
  599. usb_ep_disable(ep);
  600. ep->driver_data = NULL;
  601. goto fail;
  602. }
  603. if (alt == 0)
  604. goto out;
  605. /* one iso endpoint writes (sources) zeroes IN (to the host) */
  606. ep = ss->iso_in_ep;
  607. if (ep) {
  608. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  609. if (result)
  610. goto fail2;
  611. result = usb_ep_enable(ep);
  612. if (result < 0)
  613. goto fail2;
  614. ep->driver_data = ss;
  615. result = source_sink_start_ep(ss, true, true, speed);
  616. if (result < 0) {
  617. fail3:
  618. ep = ss->iso_in_ep;
  619. if (ep) {
  620. usb_ep_disable(ep);
  621. ep->driver_data = NULL;
  622. }
  623. goto fail2;
  624. }
  625. }
  626. /* one iso endpoint reads (sinks) anything OUT (from the host) */
  627. ep = ss->iso_out_ep;
  628. if (ep) {
  629. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  630. if (result)
  631. goto fail3;
  632. result = usb_ep_enable(ep);
  633. if (result < 0)
  634. goto fail3;
  635. ep->driver_data = ss;
  636. result = source_sink_start_ep(ss, false, true, speed);
  637. if (result < 0) {
  638. usb_ep_disable(ep);
  639. ep->driver_data = NULL;
  640. goto fail3;
  641. }
  642. }
  643. out:
  644. ss->cur_alt = alt;
  645. DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
  646. return result;
  647. }
  648. static int sourcesink_set_alt(struct usb_function *f,
  649. unsigned intf, unsigned alt)
  650. {
  651. struct f_sourcesink *ss = func_to_ss(f);
  652. struct usb_composite_dev *cdev = f->config->cdev;
  653. if (ss->in_ep->driver_data)
  654. disable_source_sink(ss);
  655. return enable_source_sink(cdev, ss, alt);
  656. }
  657. static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
  658. {
  659. struct f_sourcesink *ss = func_to_ss(f);
  660. return ss->cur_alt;
  661. }
  662. static void sourcesink_disable(struct usb_function *f)
  663. {
  664. struct f_sourcesink *ss = func_to_ss(f);
  665. disable_source_sink(ss);
  666. }
  667. /*-------------------------------------------------------------------------*/
  668. static int sourcesink_setup(struct usb_function *f,
  669. const struct usb_ctrlrequest *ctrl)
  670. {
  671. struct usb_configuration *c = f->config;
  672. struct usb_request *req = c->cdev->req;
  673. int value = -EOPNOTSUPP;
  674. u16 w_index = le16_to_cpu(ctrl->wIndex);
  675. u16 w_value = le16_to_cpu(ctrl->wValue);
  676. u16 w_length = le16_to_cpu(ctrl->wLength);
  677. req->length = USB_COMP_EP0_BUFSIZ;
  678. /* composite driver infrastructure handles everything except
  679. * the two control test requests.
  680. */
  681. switch (ctrl->bRequest) {
  682. /*
  683. * These are the same vendor-specific requests supported by
  684. * Intel's USB 2.0 compliance test devices. We exceed that
  685. * device spec by allowing multiple-packet requests.
  686. *
  687. * NOTE: the Control-OUT data stays in req->buf ... better
  688. * would be copying it into a scratch buffer, so that other
  689. * requests may safely intervene.
  690. */
  691. case 0x5b: /* control WRITE test -- fill the buffer */
  692. if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  693. goto unknown;
  694. if (w_value || w_index)
  695. break;
  696. /* just read that many bytes into the buffer */
  697. if (w_length > req->length)
  698. break;
  699. value = w_length;
  700. break;
  701. case 0x5c: /* control READ test -- return the buffer */
  702. if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  703. goto unknown;
  704. if (w_value || w_index)
  705. break;
  706. /* expect those bytes are still in the buffer; send back */
  707. if (w_length > req->length)
  708. break;
  709. value = w_length;
  710. break;
  711. default:
  712. unknown:
  713. VDBG(c->cdev,
  714. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  715. ctrl->bRequestType, ctrl->bRequest,
  716. w_value, w_index, w_length);
  717. }
  718. /* respond with data transfer or status phase? */
  719. if (value >= 0) {
  720. VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
  721. ctrl->bRequestType, ctrl->bRequest,
  722. w_value, w_index, w_length);
  723. req->zero = 0;
  724. req->length = value;
  725. value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
  726. if (value < 0)
  727. ERROR(c->cdev, "source/sink response, err %d\n",
  728. value);
  729. }
  730. /* device either stalls (value < 0) or reports success */
  731. return value;
  732. }
  733. static struct usb_function *source_sink_alloc_func(
  734. struct usb_function_instance *fi)
  735. {
  736. struct f_sourcesink *ss;
  737. struct f_ss_opts *ss_opts;
  738. ss = kzalloc(sizeof(*ss), GFP_KERNEL);
  739. if (!ss)
  740. return NULL;
  741. ss_opts = container_of(fi, struct f_ss_opts, func_inst);
  742. mutex_lock(&ss_opts->lock);
  743. ss_opts->refcnt++;
  744. mutex_unlock(&ss_opts->lock);
  745. pattern = ss_opts->pattern;
  746. isoc_interval = ss_opts->isoc_interval;
  747. isoc_maxpacket = ss_opts->isoc_maxpacket;
  748. isoc_mult = ss_opts->isoc_mult;
  749. isoc_maxburst = ss_opts->isoc_maxburst;
  750. buflen = ss_opts->bulk_buflen;
  751. ss->function.name = "source/sink";
  752. ss->function.bind = sourcesink_bind;
  753. ss->function.set_alt = sourcesink_set_alt;
  754. ss->function.get_alt = sourcesink_get_alt;
  755. ss->function.disable = sourcesink_disable;
  756. ss->function.setup = sourcesink_setup;
  757. ss->function.strings = sourcesink_strings;
  758. ss->function.free_func = sourcesink_free_func;
  759. return &ss->function;
  760. }
  761. static inline struct f_ss_opts *to_f_ss_opts(struct config_item *item)
  762. {
  763. return container_of(to_config_group(item), struct f_ss_opts,
  764. func_inst.group);
  765. }
  766. CONFIGFS_ATTR_STRUCT(f_ss_opts);
  767. CONFIGFS_ATTR_OPS(f_ss_opts);
  768. static void ss_attr_release(struct config_item *item)
  769. {
  770. struct f_ss_opts *ss_opts = to_f_ss_opts(item);
  771. usb_put_function_instance(&ss_opts->func_inst);
  772. }
  773. static struct configfs_item_operations ss_item_ops = {
  774. .release = ss_attr_release,
  775. .show_attribute = f_ss_opts_attr_show,
  776. .store_attribute = f_ss_opts_attr_store,
  777. };
  778. static ssize_t f_ss_opts_pattern_show(struct f_ss_opts *opts, char *page)
  779. {
  780. int result;
  781. mutex_lock(&opts->lock);
  782. result = sprintf(page, "%u", opts->pattern);
  783. mutex_unlock(&opts->lock);
  784. return result;
  785. }
  786. static ssize_t f_ss_opts_pattern_store(struct f_ss_opts *opts,
  787. const char *page, size_t len)
  788. {
  789. int ret;
  790. u8 num;
  791. mutex_lock(&opts->lock);
  792. if (opts->refcnt) {
  793. ret = -EBUSY;
  794. goto end;
  795. }
  796. ret = kstrtou8(page, 0, &num);
  797. if (ret)
  798. goto end;
  799. if (num != 0 && num != 1 && num != 2) {
  800. ret = -EINVAL;
  801. goto end;
  802. }
  803. opts->pattern = num;
  804. ret = len;
  805. end:
  806. mutex_unlock(&opts->lock);
  807. return ret;
  808. }
  809. static struct f_ss_opts_attribute f_ss_opts_pattern =
  810. __CONFIGFS_ATTR(pattern, S_IRUGO | S_IWUSR,
  811. f_ss_opts_pattern_show,
  812. f_ss_opts_pattern_store);
  813. static ssize_t f_ss_opts_isoc_interval_show(struct f_ss_opts *opts, char *page)
  814. {
  815. int result;
  816. mutex_lock(&opts->lock);
  817. result = sprintf(page, "%u", opts->isoc_interval);
  818. mutex_unlock(&opts->lock);
  819. return result;
  820. }
  821. static ssize_t f_ss_opts_isoc_interval_store(struct f_ss_opts *opts,
  822. const char *page, size_t len)
  823. {
  824. int ret;
  825. u8 num;
  826. mutex_lock(&opts->lock);
  827. if (opts->refcnt) {
  828. ret = -EBUSY;
  829. goto end;
  830. }
  831. ret = kstrtou8(page, 0, &num);
  832. if (ret)
  833. goto end;
  834. if (num > 16) {
  835. ret = -EINVAL;
  836. goto end;
  837. }
  838. opts->isoc_interval = num;
  839. ret = len;
  840. end:
  841. mutex_unlock(&opts->lock);
  842. return ret;
  843. }
  844. static struct f_ss_opts_attribute f_ss_opts_isoc_interval =
  845. __CONFIGFS_ATTR(isoc_interval, S_IRUGO | S_IWUSR,
  846. f_ss_opts_isoc_interval_show,
  847. f_ss_opts_isoc_interval_store);
  848. static ssize_t f_ss_opts_isoc_maxpacket_show(struct f_ss_opts *opts, char *page)
  849. {
  850. int result;
  851. mutex_lock(&opts->lock);
  852. result = sprintf(page, "%u", opts->isoc_maxpacket);
  853. mutex_unlock(&opts->lock);
  854. return result;
  855. }
  856. static ssize_t f_ss_opts_isoc_maxpacket_store(struct f_ss_opts *opts,
  857. const char *page, size_t len)
  858. {
  859. int ret;
  860. u16 num;
  861. mutex_lock(&opts->lock);
  862. if (opts->refcnt) {
  863. ret = -EBUSY;
  864. goto end;
  865. }
  866. ret = kstrtou16(page, 0, &num);
  867. if (ret)
  868. goto end;
  869. if (num > 1024) {
  870. ret = -EINVAL;
  871. goto end;
  872. }
  873. opts->isoc_maxpacket = num;
  874. ret = len;
  875. end:
  876. mutex_unlock(&opts->lock);
  877. return ret;
  878. }
  879. static struct f_ss_opts_attribute f_ss_opts_isoc_maxpacket =
  880. __CONFIGFS_ATTR(isoc_maxpacket, S_IRUGO | S_IWUSR,
  881. f_ss_opts_isoc_maxpacket_show,
  882. f_ss_opts_isoc_maxpacket_store);
  883. static ssize_t f_ss_opts_isoc_mult_show(struct f_ss_opts *opts, char *page)
  884. {
  885. int result;
  886. mutex_lock(&opts->lock);
  887. result = sprintf(page, "%u", opts->isoc_mult);
  888. mutex_unlock(&opts->lock);
  889. return result;
  890. }
  891. static ssize_t f_ss_opts_isoc_mult_store(struct f_ss_opts *opts,
  892. const char *page, size_t len)
  893. {
  894. int ret;
  895. u8 num;
  896. mutex_lock(&opts->lock);
  897. if (opts->refcnt) {
  898. ret = -EBUSY;
  899. goto end;
  900. }
  901. ret = kstrtou8(page, 0, &num);
  902. if (ret)
  903. goto end;
  904. if (num > 2) {
  905. ret = -EINVAL;
  906. goto end;
  907. }
  908. opts->isoc_mult = num;
  909. ret = len;
  910. end:
  911. mutex_unlock(&opts->lock);
  912. return ret;
  913. }
  914. static struct f_ss_opts_attribute f_ss_opts_isoc_mult =
  915. __CONFIGFS_ATTR(isoc_mult, S_IRUGO | S_IWUSR,
  916. f_ss_opts_isoc_mult_show,
  917. f_ss_opts_isoc_mult_store);
  918. static ssize_t f_ss_opts_isoc_maxburst_show(struct f_ss_opts *opts, char *page)
  919. {
  920. int result;
  921. mutex_lock(&opts->lock);
  922. result = sprintf(page, "%u", opts->isoc_maxburst);
  923. mutex_unlock(&opts->lock);
  924. return result;
  925. }
  926. static ssize_t f_ss_opts_isoc_maxburst_store(struct f_ss_opts *opts,
  927. const char *page, size_t len)
  928. {
  929. int ret;
  930. u8 num;
  931. mutex_lock(&opts->lock);
  932. if (opts->refcnt) {
  933. ret = -EBUSY;
  934. goto end;
  935. }
  936. ret = kstrtou8(page, 0, &num);
  937. if (ret)
  938. goto end;
  939. if (num > 15) {
  940. ret = -EINVAL;
  941. goto end;
  942. }
  943. opts->isoc_maxburst = num;
  944. ret = len;
  945. end:
  946. mutex_unlock(&opts->lock);
  947. return ret;
  948. }
  949. static struct f_ss_opts_attribute f_ss_opts_isoc_maxburst =
  950. __CONFIGFS_ATTR(isoc_maxburst, S_IRUGO | S_IWUSR,
  951. f_ss_opts_isoc_maxburst_show,
  952. f_ss_opts_isoc_maxburst_store);
  953. static ssize_t f_ss_opts_bulk_buflen_show(struct f_ss_opts *opts, char *page)
  954. {
  955. int result;
  956. mutex_lock(&opts->lock);
  957. result = sprintf(page, "%u", opts->bulk_buflen);
  958. mutex_unlock(&opts->lock);
  959. return result;
  960. }
  961. static ssize_t f_ss_opts_bulk_buflen_store(struct f_ss_opts *opts,
  962. const char *page, size_t len)
  963. {
  964. int ret;
  965. u32 num;
  966. mutex_lock(&opts->lock);
  967. if (opts->refcnt) {
  968. ret = -EBUSY;
  969. goto end;
  970. }
  971. ret = kstrtou32(page, 0, &num);
  972. if (ret)
  973. goto end;
  974. opts->bulk_buflen = num;
  975. ret = len;
  976. end:
  977. mutex_unlock(&opts->lock);
  978. return ret;
  979. }
  980. static struct f_ss_opts_attribute f_ss_opts_bulk_buflen =
  981. __CONFIGFS_ATTR(buflen, S_IRUGO | S_IWUSR,
  982. f_ss_opts_bulk_buflen_show,
  983. f_ss_opts_bulk_buflen_store);
  984. static struct configfs_attribute *ss_attrs[] = {
  985. &f_ss_opts_pattern.attr,
  986. &f_ss_opts_isoc_interval.attr,
  987. &f_ss_opts_isoc_maxpacket.attr,
  988. &f_ss_opts_isoc_mult.attr,
  989. &f_ss_opts_isoc_maxburst.attr,
  990. &f_ss_opts_bulk_buflen.attr,
  991. NULL,
  992. };
  993. static struct config_item_type ss_func_type = {
  994. .ct_item_ops = &ss_item_ops,
  995. .ct_attrs = ss_attrs,
  996. .ct_owner = THIS_MODULE,
  997. };
  998. static void source_sink_free_instance(struct usb_function_instance *fi)
  999. {
  1000. struct f_ss_opts *ss_opts;
  1001. ss_opts = container_of(fi, struct f_ss_opts, func_inst);
  1002. kfree(ss_opts);
  1003. }
  1004. static struct usb_function_instance *source_sink_alloc_inst(void)
  1005. {
  1006. struct f_ss_opts *ss_opts;
  1007. ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL);
  1008. if (!ss_opts)
  1009. return ERR_PTR(-ENOMEM);
  1010. mutex_init(&ss_opts->lock);
  1011. ss_opts->func_inst.free_func_inst = source_sink_free_instance;
  1012. ss_opts->isoc_interval = GZERO_ISOC_INTERVAL;
  1013. ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET;
  1014. ss_opts->bulk_buflen = GZERO_BULK_BUFLEN;
  1015. config_group_init_type_name(&ss_opts->func_inst.group, "",
  1016. &ss_func_type);
  1017. return &ss_opts->func_inst;
  1018. }
  1019. DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst,
  1020. source_sink_alloc_func);
  1021. static int __init sslb_modinit(void)
  1022. {
  1023. int ret;
  1024. ret = usb_function_register(&SourceSinkusb_func);
  1025. if (ret)
  1026. return ret;
  1027. ret = lb_modinit();
  1028. if (ret)
  1029. usb_function_unregister(&SourceSinkusb_func);
  1030. return ret;
  1031. }
  1032. static void __exit sslb_modexit(void)
  1033. {
  1034. usb_function_unregister(&SourceSinkusb_func);
  1035. lb_modexit();
  1036. }
  1037. module_init(sslb_modinit);
  1038. module_exit(sslb_modexit);
  1039. MODULE_LICENSE("GPL");