f_sourcesink.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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. unsigned pattern;
  49. unsigned isoc_interval;
  50. unsigned isoc_maxpacket;
  51. unsigned isoc_mult;
  52. unsigned isoc_maxburst;
  53. unsigned buflen;
  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. /*-------------------------------------------------------------------------*/
  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. struct f_sourcesink *ss = ep->driver_data;
  246. return alloc_ep_req(ep, len, ss->buflen);
  247. }
  248. void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  249. {
  250. kfree(req->buf);
  251. usb_ep_free_request(ep, req);
  252. }
  253. static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
  254. {
  255. int value;
  256. value = usb_ep_disable(ep);
  257. if (value < 0)
  258. DBG(cdev, "disable %s --> %d\n", ep->name, value);
  259. }
  260. void disable_endpoints(struct usb_composite_dev *cdev,
  261. struct usb_ep *in, struct usb_ep *out,
  262. struct usb_ep *iso_in, struct usb_ep *iso_out)
  263. {
  264. disable_ep(cdev, in);
  265. disable_ep(cdev, out);
  266. if (iso_in)
  267. disable_ep(cdev, iso_in);
  268. if (iso_out)
  269. disable_ep(cdev, iso_out);
  270. }
  271. static int
  272. sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
  273. {
  274. struct usb_composite_dev *cdev = c->cdev;
  275. struct f_sourcesink *ss = func_to_ss(f);
  276. int id;
  277. int ret;
  278. /* allocate interface ID(s) */
  279. id = usb_interface_id(c, f);
  280. if (id < 0)
  281. return id;
  282. source_sink_intf_alt0.bInterfaceNumber = id;
  283. source_sink_intf_alt1.bInterfaceNumber = id;
  284. /* allocate bulk endpoints */
  285. ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
  286. if (!ss->in_ep) {
  287. autoconf_fail:
  288. ERROR(cdev, "%s: can't autoconfigure on %s\n",
  289. f->name, cdev->gadget->name);
  290. return -ENODEV;
  291. }
  292. ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
  293. if (!ss->out_ep)
  294. goto autoconf_fail;
  295. /* sanity check the isoc module parameters */
  296. if (ss->isoc_interval < 1)
  297. ss->isoc_interval = 1;
  298. if (ss->isoc_interval > 16)
  299. ss->isoc_interval = 16;
  300. if (ss->isoc_mult > 2)
  301. ss->isoc_mult = 2;
  302. if (ss->isoc_maxburst > 15)
  303. ss->isoc_maxburst = 15;
  304. /* fill in the FS isoc descriptors from the module parameters */
  305. fs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket > 1023 ?
  306. 1023 : ss->isoc_maxpacket;
  307. fs_iso_source_desc.bInterval = ss->isoc_interval;
  308. fs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket > 1023 ?
  309. 1023 : ss->isoc_maxpacket;
  310. fs_iso_sink_desc.bInterval = ss->isoc_interval;
  311. /* allocate iso endpoints */
  312. ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
  313. if (!ss->iso_in_ep)
  314. goto no_iso;
  315. ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
  316. if (!ss->iso_out_ep) {
  317. usb_ep_autoconfig_release(ss->iso_in_ep);
  318. ss->iso_in_ep = NULL;
  319. no_iso:
  320. /*
  321. * We still want to work even if the UDC doesn't have isoc
  322. * endpoints, so null out the alt interface that contains
  323. * them and continue.
  324. */
  325. fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
  326. hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
  327. ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
  328. }
  329. if (ss->isoc_maxpacket > 1024)
  330. ss->isoc_maxpacket = 1024;
  331. /* support high speed hardware */
  332. hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  333. hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  334. /*
  335. * Fill in the HS isoc descriptors from the module parameters.
  336. * We assume that the user knows what they are doing and won't
  337. * give parameters that their UDC doesn't support.
  338. */
  339. hs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket;
  340. hs_iso_source_desc.wMaxPacketSize |= ss->isoc_mult << 11;
  341. hs_iso_source_desc.bInterval = ss->isoc_interval;
  342. hs_iso_source_desc.bEndpointAddress =
  343. fs_iso_source_desc.bEndpointAddress;
  344. hs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket;
  345. hs_iso_sink_desc.wMaxPacketSize |= ss->isoc_mult << 11;
  346. hs_iso_sink_desc.bInterval = ss->isoc_interval;
  347. hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
  348. /* support super speed hardware */
  349. ss_source_desc.bEndpointAddress =
  350. fs_source_desc.bEndpointAddress;
  351. ss_sink_desc.bEndpointAddress =
  352. fs_sink_desc.bEndpointAddress;
  353. /*
  354. * Fill in the SS isoc descriptors from the module parameters.
  355. * We assume that the user knows what they are doing and won't
  356. * give parameters that their UDC doesn't support.
  357. */
  358. ss_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket;
  359. ss_iso_source_desc.bInterval = ss->isoc_interval;
  360. ss_iso_source_comp_desc.bmAttributes = ss->isoc_mult;
  361. ss_iso_source_comp_desc.bMaxBurst = ss->isoc_maxburst;
  362. ss_iso_source_comp_desc.wBytesPerInterval = ss->isoc_maxpacket *
  363. (ss->isoc_mult + 1) * (ss->isoc_maxburst + 1);
  364. ss_iso_source_desc.bEndpointAddress =
  365. fs_iso_source_desc.bEndpointAddress;
  366. ss_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket;
  367. ss_iso_sink_desc.bInterval = ss->isoc_interval;
  368. ss_iso_sink_comp_desc.bmAttributes = ss->isoc_mult;
  369. ss_iso_sink_comp_desc.bMaxBurst = ss->isoc_maxburst;
  370. ss_iso_sink_comp_desc.wBytesPerInterval = ss->isoc_maxpacket *
  371. (ss->isoc_mult + 1) * (ss->isoc_maxburst + 1);
  372. ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
  373. ret = usb_assign_descriptors(f, fs_source_sink_descs,
  374. hs_source_sink_descs, ss_source_sink_descs);
  375. if (ret)
  376. return ret;
  377. DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
  378. (gadget_is_superspeed(c->cdev->gadget) ? "super" :
  379. (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
  380. f->name, ss->in_ep->name, ss->out_ep->name,
  381. ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
  382. ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
  383. return 0;
  384. }
  385. static void
  386. sourcesink_free_func(struct usb_function *f)
  387. {
  388. struct f_ss_opts *opts;
  389. opts = container_of(f->fi, struct f_ss_opts, func_inst);
  390. mutex_lock(&opts->lock);
  391. opts->refcnt--;
  392. mutex_unlock(&opts->lock);
  393. usb_free_all_descriptors(f);
  394. kfree(func_to_ss(f));
  395. }
  396. /* optionally require specific source/sink data patterns */
  397. static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
  398. {
  399. unsigned i;
  400. u8 *buf = req->buf;
  401. struct usb_composite_dev *cdev = ss->function.config->cdev;
  402. int max_packet_size = le16_to_cpu(ss->out_ep->desc->wMaxPacketSize);
  403. if (ss->pattern == 2)
  404. return 0;
  405. for (i = 0; i < req->actual; i++, buf++) {
  406. switch (ss->pattern) {
  407. /* all-zeroes has no synchronization issues */
  408. case 0:
  409. if (*buf == 0)
  410. continue;
  411. break;
  412. /* "mod63" stays in sync with short-terminated transfers,
  413. * OR otherwise when host and gadget agree on how large
  414. * each usb transfer request should be. Resync is done
  415. * with set_interface or set_config. (We *WANT* it to
  416. * get quickly out of sync if controllers or their drivers
  417. * stutter for any reason, including buffer duplication...)
  418. */
  419. case 1:
  420. if (*buf == (u8)((i % max_packet_size) % 63))
  421. continue;
  422. break;
  423. }
  424. ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
  425. usb_ep_set_halt(ss->out_ep);
  426. return -EINVAL;
  427. }
  428. return 0;
  429. }
  430. static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
  431. {
  432. unsigned i;
  433. u8 *buf = req->buf;
  434. int max_packet_size = le16_to_cpu(ep->desc->wMaxPacketSize);
  435. struct f_sourcesink *ss = ep->driver_data;
  436. switch (ss->pattern) {
  437. case 0:
  438. memset(req->buf, 0, req->length);
  439. break;
  440. case 1:
  441. for (i = 0; i < req->length; i++)
  442. *buf++ = (u8) ((i % max_packet_size) % 63);
  443. break;
  444. case 2:
  445. break;
  446. }
  447. }
  448. static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
  449. {
  450. struct usb_composite_dev *cdev;
  451. struct f_sourcesink *ss = ep->driver_data;
  452. int status = req->status;
  453. /* driver_data will be null if ep has been disabled */
  454. if (!ss)
  455. return;
  456. cdev = ss->function.config->cdev;
  457. switch (status) {
  458. case 0: /* normal completion? */
  459. if (ep == ss->out_ep) {
  460. check_read_data(ss, req);
  461. if (ss->pattern != 2)
  462. memset(req->buf, 0x55, req->length);
  463. }
  464. break;
  465. /* this endpoint is normally active while we're configured */
  466. case -ECONNABORTED: /* hardware forced ep reset */
  467. case -ECONNRESET: /* request dequeued */
  468. case -ESHUTDOWN: /* disconnect from host */
  469. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  470. req->actual, req->length);
  471. if (ep == ss->out_ep)
  472. check_read_data(ss, req);
  473. free_ep_req(ep, req);
  474. return;
  475. case -EOVERFLOW: /* buffer overrun on read means that
  476. * we didn't provide a big enough
  477. * buffer.
  478. */
  479. default:
  480. #if 1
  481. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  482. status, req->actual, req->length);
  483. #endif
  484. case -EREMOTEIO: /* short read */
  485. break;
  486. }
  487. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  488. if (status) {
  489. ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
  490. ep->name, req->length, status);
  491. usb_ep_set_halt(ep);
  492. /* FIXME recover later ... somehow */
  493. }
  494. }
  495. static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
  496. bool is_iso, int speed)
  497. {
  498. struct usb_ep *ep;
  499. struct usb_request *req;
  500. int i, size, status;
  501. for (i = 0; i < 8; i++) {
  502. if (is_iso) {
  503. switch (speed) {
  504. case USB_SPEED_SUPER:
  505. size = ss->isoc_maxpacket *
  506. (ss->isoc_mult + 1) *
  507. (ss->isoc_maxburst + 1);
  508. break;
  509. case USB_SPEED_HIGH:
  510. size = ss->isoc_maxpacket * (ss->isoc_mult + 1);
  511. break;
  512. default:
  513. size = ss->isoc_maxpacket > 1023 ?
  514. 1023 : ss->isoc_maxpacket;
  515. break;
  516. }
  517. ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
  518. req = ss_alloc_ep_req(ep, size);
  519. } else {
  520. ep = is_in ? ss->in_ep : ss->out_ep;
  521. req = ss_alloc_ep_req(ep, 0);
  522. }
  523. if (!req)
  524. return -ENOMEM;
  525. req->complete = source_sink_complete;
  526. if (is_in)
  527. reinit_write_data(ep, req);
  528. else if (ss->pattern != 2)
  529. memset(req->buf, 0x55, req->length);
  530. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  531. if (status) {
  532. struct usb_composite_dev *cdev;
  533. cdev = ss->function.config->cdev;
  534. ERROR(cdev, "start %s%s %s --> %d\n",
  535. is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
  536. ep->name, status);
  537. free_ep_req(ep, req);
  538. }
  539. if (!is_iso)
  540. break;
  541. }
  542. return status;
  543. }
  544. static void disable_source_sink(struct f_sourcesink *ss)
  545. {
  546. struct usb_composite_dev *cdev;
  547. cdev = ss->function.config->cdev;
  548. disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
  549. ss->iso_out_ep);
  550. VDBG(cdev, "%s disabled\n", ss->function.name);
  551. }
  552. static int
  553. enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
  554. int alt)
  555. {
  556. int result = 0;
  557. int speed = cdev->gadget->speed;
  558. struct usb_ep *ep;
  559. /* one bulk endpoint writes (sources) zeroes IN (to the host) */
  560. ep = ss->in_ep;
  561. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  562. if (result)
  563. return result;
  564. result = usb_ep_enable(ep);
  565. if (result < 0)
  566. return result;
  567. ep->driver_data = ss;
  568. result = source_sink_start_ep(ss, true, false, speed);
  569. if (result < 0) {
  570. fail:
  571. ep = ss->in_ep;
  572. usb_ep_disable(ep);
  573. return result;
  574. }
  575. /* one bulk endpoint reads (sinks) anything OUT (from the host) */
  576. ep = ss->out_ep;
  577. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  578. if (result)
  579. goto fail;
  580. result = usb_ep_enable(ep);
  581. if (result < 0)
  582. goto fail;
  583. ep->driver_data = ss;
  584. result = source_sink_start_ep(ss, false, false, speed);
  585. if (result < 0) {
  586. fail2:
  587. ep = ss->out_ep;
  588. usb_ep_disable(ep);
  589. goto fail;
  590. }
  591. if (alt == 0)
  592. goto out;
  593. /* one iso endpoint writes (sources) zeroes IN (to the host) */
  594. ep = ss->iso_in_ep;
  595. if (ep) {
  596. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  597. if (result)
  598. goto fail2;
  599. result = usb_ep_enable(ep);
  600. if (result < 0)
  601. goto fail2;
  602. ep->driver_data = ss;
  603. result = source_sink_start_ep(ss, true, true, speed);
  604. if (result < 0) {
  605. fail3:
  606. ep = ss->iso_in_ep;
  607. if (ep)
  608. usb_ep_disable(ep);
  609. goto fail2;
  610. }
  611. }
  612. /* one iso endpoint reads (sinks) anything OUT (from the host) */
  613. ep = ss->iso_out_ep;
  614. if (ep) {
  615. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  616. if (result)
  617. goto fail3;
  618. result = usb_ep_enable(ep);
  619. if (result < 0)
  620. goto fail3;
  621. ep->driver_data = ss;
  622. result = source_sink_start_ep(ss, false, true, speed);
  623. if (result < 0) {
  624. usb_ep_disable(ep);
  625. goto fail3;
  626. }
  627. }
  628. out:
  629. ss->cur_alt = alt;
  630. DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
  631. return result;
  632. }
  633. static int sourcesink_set_alt(struct usb_function *f,
  634. unsigned intf, unsigned alt)
  635. {
  636. struct f_sourcesink *ss = func_to_ss(f);
  637. struct usb_composite_dev *cdev = f->config->cdev;
  638. disable_source_sink(ss);
  639. return enable_source_sink(cdev, ss, alt);
  640. }
  641. static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
  642. {
  643. struct f_sourcesink *ss = func_to_ss(f);
  644. return ss->cur_alt;
  645. }
  646. static void sourcesink_disable(struct usb_function *f)
  647. {
  648. struct f_sourcesink *ss = func_to_ss(f);
  649. disable_source_sink(ss);
  650. }
  651. /*-------------------------------------------------------------------------*/
  652. static int sourcesink_setup(struct usb_function *f,
  653. const struct usb_ctrlrequest *ctrl)
  654. {
  655. struct usb_configuration *c = f->config;
  656. struct usb_request *req = c->cdev->req;
  657. int value = -EOPNOTSUPP;
  658. u16 w_index = le16_to_cpu(ctrl->wIndex);
  659. u16 w_value = le16_to_cpu(ctrl->wValue);
  660. u16 w_length = le16_to_cpu(ctrl->wLength);
  661. req->length = USB_COMP_EP0_BUFSIZ;
  662. /* composite driver infrastructure handles everything except
  663. * the two control test requests.
  664. */
  665. switch (ctrl->bRequest) {
  666. /*
  667. * These are the same vendor-specific requests supported by
  668. * Intel's USB 2.0 compliance test devices. We exceed that
  669. * device spec by allowing multiple-packet requests.
  670. *
  671. * NOTE: the Control-OUT data stays in req->buf ... better
  672. * would be copying it into a scratch buffer, so that other
  673. * requests may safely intervene.
  674. */
  675. case 0x5b: /* control WRITE test -- fill the buffer */
  676. if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  677. goto unknown;
  678. if (w_value || w_index)
  679. break;
  680. /* just read that many bytes into the buffer */
  681. if (w_length > req->length)
  682. break;
  683. value = w_length;
  684. break;
  685. case 0x5c: /* control READ test -- return the buffer */
  686. if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  687. goto unknown;
  688. if (w_value || w_index)
  689. break;
  690. /* expect those bytes are still in the buffer; send back */
  691. if (w_length > req->length)
  692. break;
  693. value = w_length;
  694. break;
  695. default:
  696. unknown:
  697. VDBG(c->cdev,
  698. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  699. ctrl->bRequestType, ctrl->bRequest,
  700. w_value, w_index, w_length);
  701. }
  702. /* respond with data transfer or status phase? */
  703. if (value >= 0) {
  704. VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
  705. ctrl->bRequestType, ctrl->bRequest,
  706. w_value, w_index, w_length);
  707. req->zero = 0;
  708. req->length = value;
  709. value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
  710. if (value < 0)
  711. ERROR(c->cdev, "source/sink response, err %d\n",
  712. value);
  713. }
  714. /* device either stalls (value < 0) or reports success */
  715. return value;
  716. }
  717. static struct usb_function *source_sink_alloc_func(
  718. struct usb_function_instance *fi)
  719. {
  720. struct f_sourcesink *ss;
  721. struct f_ss_opts *ss_opts;
  722. ss = kzalloc(sizeof(*ss), GFP_KERNEL);
  723. if (!ss)
  724. return NULL;
  725. ss_opts = container_of(fi, struct f_ss_opts, func_inst);
  726. mutex_lock(&ss_opts->lock);
  727. ss_opts->refcnt++;
  728. mutex_unlock(&ss_opts->lock);
  729. ss->pattern = ss_opts->pattern;
  730. ss->isoc_interval = ss_opts->isoc_interval;
  731. ss->isoc_maxpacket = ss_opts->isoc_maxpacket;
  732. ss->isoc_mult = ss_opts->isoc_mult;
  733. ss->isoc_maxburst = ss_opts->isoc_maxburst;
  734. ss->buflen = ss_opts->bulk_buflen;
  735. ss->function.name = "source/sink";
  736. ss->function.bind = sourcesink_bind;
  737. ss->function.set_alt = sourcesink_set_alt;
  738. ss->function.get_alt = sourcesink_get_alt;
  739. ss->function.disable = sourcesink_disable;
  740. ss->function.setup = sourcesink_setup;
  741. ss->function.strings = sourcesink_strings;
  742. ss->function.free_func = sourcesink_free_func;
  743. return &ss->function;
  744. }
  745. static inline struct f_ss_opts *to_f_ss_opts(struct config_item *item)
  746. {
  747. return container_of(to_config_group(item), struct f_ss_opts,
  748. func_inst.group);
  749. }
  750. CONFIGFS_ATTR_STRUCT(f_ss_opts);
  751. CONFIGFS_ATTR_OPS(f_ss_opts);
  752. static void ss_attr_release(struct config_item *item)
  753. {
  754. struct f_ss_opts *ss_opts = to_f_ss_opts(item);
  755. usb_put_function_instance(&ss_opts->func_inst);
  756. }
  757. static struct configfs_item_operations ss_item_ops = {
  758. .release = ss_attr_release,
  759. .show_attribute = f_ss_opts_attr_show,
  760. .store_attribute = f_ss_opts_attr_store,
  761. };
  762. static ssize_t f_ss_opts_pattern_show(struct f_ss_opts *opts, char *page)
  763. {
  764. int result;
  765. mutex_lock(&opts->lock);
  766. result = sprintf(page, "%u\n", opts->pattern);
  767. mutex_unlock(&opts->lock);
  768. return result;
  769. }
  770. static ssize_t f_ss_opts_pattern_store(struct f_ss_opts *opts,
  771. const char *page, size_t len)
  772. {
  773. int ret;
  774. u8 num;
  775. mutex_lock(&opts->lock);
  776. if (opts->refcnt) {
  777. ret = -EBUSY;
  778. goto end;
  779. }
  780. ret = kstrtou8(page, 0, &num);
  781. if (ret)
  782. goto end;
  783. if (num != 0 && num != 1 && num != 2) {
  784. ret = -EINVAL;
  785. goto end;
  786. }
  787. opts->pattern = num;
  788. ret = len;
  789. end:
  790. mutex_unlock(&opts->lock);
  791. return ret;
  792. }
  793. static struct f_ss_opts_attribute f_ss_opts_pattern =
  794. __CONFIGFS_ATTR(pattern, S_IRUGO | S_IWUSR,
  795. f_ss_opts_pattern_show,
  796. f_ss_opts_pattern_store);
  797. static ssize_t f_ss_opts_isoc_interval_show(struct f_ss_opts *opts, char *page)
  798. {
  799. int result;
  800. mutex_lock(&opts->lock);
  801. result = sprintf(page, "%u\n", opts->isoc_interval);
  802. mutex_unlock(&opts->lock);
  803. return result;
  804. }
  805. static ssize_t f_ss_opts_isoc_interval_store(struct f_ss_opts *opts,
  806. const char *page, size_t len)
  807. {
  808. int ret;
  809. u8 num;
  810. mutex_lock(&opts->lock);
  811. if (opts->refcnt) {
  812. ret = -EBUSY;
  813. goto end;
  814. }
  815. ret = kstrtou8(page, 0, &num);
  816. if (ret)
  817. goto end;
  818. if (num > 16) {
  819. ret = -EINVAL;
  820. goto end;
  821. }
  822. opts->isoc_interval = num;
  823. ret = len;
  824. end:
  825. mutex_unlock(&opts->lock);
  826. return ret;
  827. }
  828. static struct f_ss_opts_attribute f_ss_opts_isoc_interval =
  829. __CONFIGFS_ATTR(isoc_interval, S_IRUGO | S_IWUSR,
  830. f_ss_opts_isoc_interval_show,
  831. f_ss_opts_isoc_interval_store);
  832. static ssize_t f_ss_opts_isoc_maxpacket_show(struct f_ss_opts *opts, char *page)
  833. {
  834. int result;
  835. mutex_lock(&opts->lock);
  836. result = sprintf(page, "%u\n", opts->isoc_maxpacket);
  837. mutex_unlock(&opts->lock);
  838. return result;
  839. }
  840. static ssize_t f_ss_opts_isoc_maxpacket_store(struct f_ss_opts *opts,
  841. const char *page, size_t len)
  842. {
  843. int ret;
  844. u16 num;
  845. mutex_lock(&opts->lock);
  846. if (opts->refcnt) {
  847. ret = -EBUSY;
  848. goto end;
  849. }
  850. ret = kstrtou16(page, 0, &num);
  851. if (ret)
  852. goto end;
  853. if (num > 1024) {
  854. ret = -EINVAL;
  855. goto end;
  856. }
  857. opts->isoc_maxpacket = num;
  858. ret = len;
  859. end:
  860. mutex_unlock(&opts->lock);
  861. return ret;
  862. }
  863. static struct f_ss_opts_attribute f_ss_opts_isoc_maxpacket =
  864. __CONFIGFS_ATTR(isoc_maxpacket, S_IRUGO | S_IWUSR,
  865. f_ss_opts_isoc_maxpacket_show,
  866. f_ss_opts_isoc_maxpacket_store);
  867. static ssize_t f_ss_opts_isoc_mult_show(struct f_ss_opts *opts, char *page)
  868. {
  869. int result;
  870. mutex_lock(&opts->lock);
  871. result = sprintf(page, "%u\n", opts->isoc_mult);
  872. mutex_unlock(&opts->lock);
  873. return result;
  874. }
  875. static ssize_t f_ss_opts_isoc_mult_store(struct f_ss_opts *opts,
  876. const char *page, size_t len)
  877. {
  878. int ret;
  879. u8 num;
  880. mutex_lock(&opts->lock);
  881. if (opts->refcnt) {
  882. ret = -EBUSY;
  883. goto end;
  884. }
  885. ret = kstrtou8(page, 0, &num);
  886. if (ret)
  887. goto end;
  888. if (num > 2) {
  889. ret = -EINVAL;
  890. goto end;
  891. }
  892. opts->isoc_mult = num;
  893. ret = len;
  894. end:
  895. mutex_unlock(&opts->lock);
  896. return ret;
  897. }
  898. static struct f_ss_opts_attribute f_ss_opts_isoc_mult =
  899. __CONFIGFS_ATTR(isoc_mult, S_IRUGO | S_IWUSR,
  900. f_ss_opts_isoc_mult_show,
  901. f_ss_opts_isoc_mult_store);
  902. static ssize_t f_ss_opts_isoc_maxburst_show(struct f_ss_opts *opts, char *page)
  903. {
  904. int result;
  905. mutex_lock(&opts->lock);
  906. result = sprintf(page, "%u\n", opts->isoc_maxburst);
  907. mutex_unlock(&opts->lock);
  908. return result;
  909. }
  910. static ssize_t f_ss_opts_isoc_maxburst_store(struct f_ss_opts *opts,
  911. const char *page, size_t len)
  912. {
  913. int ret;
  914. u8 num;
  915. mutex_lock(&opts->lock);
  916. if (opts->refcnt) {
  917. ret = -EBUSY;
  918. goto end;
  919. }
  920. ret = kstrtou8(page, 0, &num);
  921. if (ret)
  922. goto end;
  923. if (num > 15) {
  924. ret = -EINVAL;
  925. goto end;
  926. }
  927. opts->isoc_maxburst = num;
  928. ret = len;
  929. end:
  930. mutex_unlock(&opts->lock);
  931. return ret;
  932. }
  933. static struct f_ss_opts_attribute f_ss_opts_isoc_maxburst =
  934. __CONFIGFS_ATTR(isoc_maxburst, S_IRUGO | S_IWUSR,
  935. f_ss_opts_isoc_maxburst_show,
  936. f_ss_opts_isoc_maxburst_store);
  937. static ssize_t f_ss_opts_bulk_buflen_show(struct f_ss_opts *opts, char *page)
  938. {
  939. int result;
  940. mutex_lock(&opts->lock);
  941. result = sprintf(page, "%u\n", opts->bulk_buflen);
  942. mutex_unlock(&opts->lock);
  943. return result;
  944. }
  945. static ssize_t f_ss_opts_bulk_buflen_store(struct f_ss_opts *opts,
  946. const char *page, size_t len)
  947. {
  948. int ret;
  949. u32 num;
  950. mutex_lock(&opts->lock);
  951. if (opts->refcnt) {
  952. ret = -EBUSY;
  953. goto end;
  954. }
  955. ret = kstrtou32(page, 0, &num);
  956. if (ret)
  957. goto end;
  958. opts->bulk_buflen = num;
  959. ret = len;
  960. end:
  961. mutex_unlock(&opts->lock);
  962. return ret;
  963. }
  964. static struct f_ss_opts_attribute f_ss_opts_bulk_buflen =
  965. __CONFIGFS_ATTR(buflen, S_IRUGO | S_IWUSR,
  966. f_ss_opts_bulk_buflen_show,
  967. f_ss_opts_bulk_buflen_store);
  968. static struct configfs_attribute *ss_attrs[] = {
  969. &f_ss_opts_pattern.attr,
  970. &f_ss_opts_isoc_interval.attr,
  971. &f_ss_opts_isoc_maxpacket.attr,
  972. &f_ss_opts_isoc_mult.attr,
  973. &f_ss_opts_isoc_maxburst.attr,
  974. &f_ss_opts_bulk_buflen.attr,
  975. NULL,
  976. };
  977. static struct config_item_type ss_func_type = {
  978. .ct_item_ops = &ss_item_ops,
  979. .ct_attrs = ss_attrs,
  980. .ct_owner = THIS_MODULE,
  981. };
  982. static void source_sink_free_instance(struct usb_function_instance *fi)
  983. {
  984. struct f_ss_opts *ss_opts;
  985. ss_opts = container_of(fi, struct f_ss_opts, func_inst);
  986. kfree(ss_opts);
  987. }
  988. static struct usb_function_instance *source_sink_alloc_inst(void)
  989. {
  990. struct f_ss_opts *ss_opts;
  991. ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL);
  992. if (!ss_opts)
  993. return ERR_PTR(-ENOMEM);
  994. mutex_init(&ss_opts->lock);
  995. ss_opts->func_inst.free_func_inst = source_sink_free_instance;
  996. ss_opts->isoc_interval = GZERO_ISOC_INTERVAL;
  997. ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET;
  998. ss_opts->bulk_buflen = GZERO_BULK_BUFLEN;
  999. config_group_init_type_name(&ss_opts->func_inst.group, "",
  1000. &ss_func_type);
  1001. return &ss_opts->func_inst;
  1002. }
  1003. DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst,
  1004. source_sink_alloc_func);
  1005. static int __init sslb_modinit(void)
  1006. {
  1007. int ret;
  1008. ret = usb_function_register(&SourceSinkusb_func);
  1009. if (ret)
  1010. return ret;
  1011. ret = lb_modinit();
  1012. if (ret)
  1013. usb_function_unregister(&SourceSinkusb_func);
  1014. return ret;
  1015. }
  1016. static void __exit sslb_modexit(void)
  1017. {
  1018. usb_function_unregister(&SourceSinkusb_func);
  1019. lb_modexit();
  1020. }
  1021. module_init(sslb_modinit);
  1022. module_exit(sslb_modexit);
  1023. MODULE_LICENSE("GPL");