f_sourcesink.c 30 KB

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