f_sourcesink.c 30 KB

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