f_sourcesink.c 30 KB

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