f_sourcesink.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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. /*
  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. *
  42. * This is currently packaged as a configuration driver, which can't be
  43. * combined with other functions to make composite devices. However, it
  44. * can be combined with other independent configurations.
  45. */
  46. struct f_sourcesink {
  47. struct usb_function function;
  48. struct usb_ep *in_ep;
  49. struct usb_ep *out_ep;
  50. struct usb_ep *iso_in_ep;
  51. struct usb_ep *iso_out_ep;
  52. int cur_alt;
  53. };
  54. static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
  55. {
  56. return container_of(f, struct f_sourcesink, function);
  57. }
  58. static unsigned pattern;
  59. static unsigned isoc_interval;
  60. static unsigned isoc_maxpacket;
  61. static unsigned isoc_mult;
  62. static unsigned isoc_maxburst;
  63. static unsigned buflen;
  64. /*-------------------------------------------------------------------------*/
  65. static struct usb_interface_descriptor source_sink_intf_alt0 = {
  66. .bLength = USB_DT_INTERFACE_SIZE,
  67. .bDescriptorType = USB_DT_INTERFACE,
  68. .bAlternateSetting = 0,
  69. .bNumEndpoints = 2,
  70. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  71. /* .iInterface = DYNAMIC */
  72. };
  73. static struct usb_interface_descriptor source_sink_intf_alt1 = {
  74. .bLength = USB_DT_INTERFACE_SIZE,
  75. .bDescriptorType = USB_DT_INTERFACE,
  76. .bAlternateSetting = 1,
  77. .bNumEndpoints = 4,
  78. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  79. /* .iInterface = DYNAMIC */
  80. };
  81. /* full speed support: */
  82. static struct usb_endpoint_descriptor fs_source_desc = {
  83. .bLength = USB_DT_ENDPOINT_SIZE,
  84. .bDescriptorType = USB_DT_ENDPOINT,
  85. .bEndpointAddress = USB_DIR_IN,
  86. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  87. };
  88. static struct usb_endpoint_descriptor fs_sink_desc = {
  89. .bLength = USB_DT_ENDPOINT_SIZE,
  90. .bDescriptorType = USB_DT_ENDPOINT,
  91. .bEndpointAddress = USB_DIR_OUT,
  92. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  93. };
  94. static struct usb_endpoint_descriptor fs_iso_source_desc = {
  95. .bLength = USB_DT_ENDPOINT_SIZE,
  96. .bDescriptorType = USB_DT_ENDPOINT,
  97. .bEndpointAddress = USB_DIR_IN,
  98. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  99. .wMaxPacketSize = cpu_to_le16(1023),
  100. .bInterval = 4,
  101. };
  102. static struct usb_endpoint_descriptor fs_iso_sink_desc = {
  103. .bLength = USB_DT_ENDPOINT_SIZE,
  104. .bDescriptorType = USB_DT_ENDPOINT,
  105. .bEndpointAddress = USB_DIR_OUT,
  106. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  107. .wMaxPacketSize = cpu_to_le16(1023),
  108. .bInterval = 4,
  109. };
  110. static struct usb_descriptor_header *fs_source_sink_descs[] = {
  111. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  112. (struct usb_descriptor_header *) &fs_sink_desc,
  113. (struct usb_descriptor_header *) &fs_source_desc,
  114. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  115. #define FS_ALT_IFC_1_OFFSET 3
  116. (struct usb_descriptor_header *) &fs_sink_desc,
  117. (struct usb_descriptor_header *) &fs_source_desc,
  118. (struct usb_descriptor_header *) &fs_iso_sink_desc,
  119. (struct usb_descriptor_header *) &fs_iso_source_desc,
  120. NULL,
  121. };
  122. /* high speed support: */
  123. static struct usb_endpoint_descriptor hs_source_desc = {
  124. .bLength = USB_DT_ENDPOINT_SIZE,
  125. .bDescriptorType = USB_DT_ENDPOINT,
  126. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  127. .wMaxPacketSize = cpu_to_le16(512),
  128. };
  129. static struct usb_endpoint_descriptor hs_sink_desc = {
  130. .bLength = USB_DT_ENDPOINT_SIZE,
  131. .bDescriptorType = USB_DT_ENDPOINT,
  132. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  133. .wMaxPacketSize = cpu_to_le16(512),
  134. };
  135. static struct usb_endpoint_descriptor hs_iso_source_desc = {
  136. .bLength = USB_DT_ENDPOINT_SIZE,
  137. .bDescriptorType = USB_DT_ENDPOINT,
  138. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  139. .wMaxPacketSize = cpu_to_le16(1024),
  140. .bInterval = 4,
  141. };
  142. static struct usb_endpoint_descriptor hs_iso_sink_desc = {
  143. .bLength = USB_DT_ENDPOINT_SIZE,
  144. .bDescriptorType = USB_DT_ENDPOINT,
  145. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  146. .wMaxPacketSize = cpu_to_le16(1024),
  147. .bInterval = 4,
  148. };
  149. static struct usb_descriptor_header *hs_source_sink_descs[] = {
  150. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  151. (struct usb_descriptor_header *) &hs_source_desc,
  152. (struct usb_descriptor_header *) &hs_sink_desc,
  153. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  154. #define HS_ALT_IFC_1_OFFSET 3
  155. (struct usb_descriptor_header *) &hs_source_desc,
  156. (struct usb_descriptor_header *) &hs_sink_desc,
  157. (struct usb_descriptor_header *) &hs_iso_source_desc,
  158. (struct usb_descriptor_header *) &hs_iso_sink_desc,
  159. NULL,
  160. };
  161. /* super speed support: */
  162. static struct usb_endpoint_descriptor ss_source_desc = {
  163. .bLength = USB_DT_ENDPOINT_SIZE,
  164. .bDescriptorType = USB_DT_ENDPOINT,
  165. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  166. .wMaxPacketSize = cpu_to_le16(1024),
  167. };
  168. struct usb_ss_ep_comp_descriptor ss_source_comp_desc = {
  169. .bLength = USB_DT_SS_EP_COMP_SIZE,
  170. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  171. .bMaxBurst = 0,
  172. .bmAttributes = 0,
  173. .wBytesPerInterval = 0,
  174. };
  175. static struct usb_endpoint_descriptor ss_sink_desc = {
  176. .bLength = USB_DT_ENDPOINT_SIZE,
  177. .bDescriptorType = USB_DT_ENDPOINT,
  178. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  179. .wMaxPacketSize = cpu_to_le16(1024),
  180. };
  181. struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = {
  182. .bLength = USB_DT_SS_EP_COMP_SIZE,
  183. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  184. .bMaxBurst = 0,
  185. .bmAttributes = 0,
  186. .wBytesPerInterval = 0,
  187. };
  188. static struct usb_endpoint_descriptor ss_iso_source_desc = {
  189. .bLength = USB_DT_ENDPOINT_SIZE,
  190. .bDescriptorType = USB_DT_ENDPOINT,
  191. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  192. .wMaxPacketSize = cpu_to_le16(1024),
  193. .bInterval = 4,
  194. };
  195. struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc = {
  196. .bLength = USB_DT_SS_EP_COMP_SIZE,
  197. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  198. .bMaxBurst = 0,
  199. .bmAttributes = 0,
  200. .wBytesPerInterval = cpu_to_le16(1024),
  201. };
  202. static struct usb_endpoint_descriptor ss_iso_sink_desc = {
  203. .bLength = USB_DT_ENDPOINT_SIZE,
  204. .bDescriptorType = USB_DT_ENDPOINT,
  205. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  206. .wMaxPacketSize = cpu_to_le16(1024),
  207. .bInterval = 4,
  208. };
  209. struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = {
  210. .bLength = USB_DT_SS_EP_COMP_SIZE,
  211. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  212. .bMaxBurst = 0,
  213. .bmAttributes = 0,
  214. .wBytesPerInterval = cpu_to_le16(1024),
  215. };
  216. static struct usb_descriptor_header *ss_source_sink_descs[] = {
  217. (struct usb_descriptor_header *) &source_sink_intf_alt0,
  218. (struct usb_descriptor_header *) &ss_source_desc,
  219. (struct usb_descriptor_header *) &ss_source_comp_desc,
  220. (struct usb_descriptor_header *) &ss_sink_desc,
  221. (struct usb_descriptor_header *) &ss_sink_comp_desc,
  222. (struct usb_descriptor_header *) &source_sink_intf_alt1,
  223. #define SS_ALT_IFC_1_OFFSET 5
  224. (struct usb_descriptor_header *) &ss_source_desc,
  225. (struct usb_descriptor_header *) &ss_source_comp_desc,
  226. (struct usb_descriptor_header *) &ss_sink_desc,
  227. (struct usb_descriptor_header *) &ss_sink_comp_desc,
  228. (struct usb_descriptor_header *) &ss_iso_source_desc,
  229. (struct usb_descriptor_header *) &ss_iso_source_comp_desc,
  230. (struct usb_descriptor_header *) &ss_iso_sink_desc,
  231. (struct usb_descriptor_header *) &ss_iso_sink_comp_desc,
  232. NULL,
  233. };
  234. /* function-specific strings: */
  235. static struct usb_string strings_sourcesink[] = {
  236. [0].s = "source and sink data",
  237. { } /* end of list */
  238. };
  239. static struct usb_gadget_strings stringtab_sourcesink = {
  240. .language = 0x0409, /* en-us */
  241. .strings = strings_sourcesink,
  242. };
  243. static struct usb_gadget_strings *sourcesink_strings[] = {
  244. &stringtab_sourcesink,
  245. NULL,
  246. };
  247. /*-------------------------------------------------------------------------*/
  248. struct usb_request *alloc_ep_req(struct usb_ep *ep, int len)
  249. {
  250. struct usb_request *req;
  251. req = usb_ep_alloc_request(ep, GFP_ATOMIC);
  252. if (req) {
  253. if (len)
  254. req->length = len;
  255. else
  256. req->length = buflen;
  257. req->buf = kmalloc(req->length, GFP_ATOMIC);
  258. if (!req->buf) {
  259. usb_ep_free_request(ep, req);
  260. req = NULL;
  261. }
  262. }
  263. return req;
  264. }
  265. void free_ep_req(struct usb_ep *ep, struct usb_request *req)
  266. {
  267. kfree(req->buf);
  268. usb_ep_free_request(ep, req);
  269. }
  270. static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
  271. {
  272. int value;
  273. if (ep->driver_data) {
  274. value = usb_ep_disable(ep);
  275. if (value < 0)
  276. DBG(cdev, "disable %s --> %d\n",
  277. ep->name, value);
  278. ep->driver_data = NULL;
  279. }
  280. }
  281. void disable_endpoints(struct usb_composite_dev *cdev,
  282. struct usb_ep *in, struct usb_ep *out,
  283. struct usb_ep *iso_in, struct usb_ep *iso_out)
  284. {
  285. disable_ep(cdev, in);
  286. disable_ep(cdev, out);
  287. if (iso_in)
  288. disable_ep(cdev, iso_in);
  289. if (iso_out)
  290. disable_ep(cdev, iso_out);
  291. }
  292. static int
  293. sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
  294. {
  295. struct usb_composite_dev *cdev = c->cdev;
  296. struct f_sourcesink *ss = func_to_ss(f);
  297. int id;
  298. int ret;
  299. /* allocate interface ID(s) */
  300. id = usb_interface_id(c, f);
  301. if (id < 0)
  302. return id;
  303. source_sink_intf_alt0.bInterfaceNumber = id;
  304. source_sink_intf_alt1.bInterfaceNumber = id;
  305. /* allocate bulk endpoints */
  306. ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
  307. if (!ss->in_ep) {
  308. autoconf_fail:
  309. ERROR(cdev, "%s: can't autoconfigure on %s\n",
  310. f->name, cdev->gadget->name);
  311. return -ENODEV;
  312. }
  313. ss->in_ep->driver_data = cdev; /* claim */
  314. ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
  315. if (!ss->out_ep)
  316. goto autoconf_fail;
  317. ss->out_ep->driver_data = cdev; /* claim */
  318. /* sanity check the isoc module parameters */
  319. if (isoc_interval < 1)
  320. isoc_interval = 1;
  321. if (isoc_interval > 16)
  322. isoc_interval = 16;
  323. if (isoc_mult > 2)
  324. isoc_mult = 2;
  325. if (isoc_maxburst > 15)
  326. isoc_maxburst = 15;
  327. /* fill in the FS isoc descriptors from the module parameters */
  328. fs_iso_source_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
  329. 1023 : isoc_maxpacket;
  330. fs_iso_source_desc.bInterval = isoc_interval;
  331. fs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
  332. 1023 : isoc_maxpacket;
  333. fs_iso_sink_desc.bInterval = isoc_interval;
  334. /* allocate iso endpoints */
  335. ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
  336. if (!ss->iso_in_ep)
  337. goto no_iso;
  338. ss->iso_in_ep->driver_data = cdev; /* claim */
  339. ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
  340. if (ss->iso_out_ep) {
  341. ss->iso_out_ep->driver_data = cdev; /* claim */
  342. } else {
  343. ss->iso_in_ep->driver_data = NULL;
  344. ss->iso_in_ep = NULL;
  345. no_iso:
  346. /*
  347. * We still want to work even if the UDC doesn't have isoc
  348. * endpoints, so null out the alt interface that contains
  349. * them and continue.
  350. */
  351. fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
  352. hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
  353. ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
  354. }
  355. if (isoc_maxpacket > 1024)
  356. isoc_maxpacket = 1024;
  357. /* support high speed hardware */
  358. hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  359. hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  360. /*
  361. * Fill in the HS isoc descriptors from the module parameters.
  362. * We assume that the user knows what they are doing and won't
  363. * give parameters that their UDC doesn't support.
  364. */
  365. hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
  366. hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11;
  367. hs_iso_source_desc.bInterval = isoc_interval;
  368. hs_iso_source_desc.bEndpointAddress =
  369. fs_iso_source_desc.bEndpointAddress;
  370. hs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
  371. hs_iso_sink_desc.wMaxPacketSize |= isoc_mult << 11;
  372. hs_iso_sink_desc.bInterval = isoc_interval;
  373. hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
  374. /* support super speed hardware */
  375. ss_source_desc.bEndpointAddress =
  376. fs_source_desc.bEndpointAddress;
  377. ss_sink_desc.bEndpointAddress =
  378. fs_sink_desc.bEndpointAddress;
  379. /*
  380. * Fill in the SS isoc descriptors from the module parameters.
  381. * We assume that the user knows what they are doing and won't
  382. * give parameters that their UDC doesn't support.
  383. */
  384. ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
  385. ss_iso_source_desc.bInterval = isoc_interval;
  386. ss_iso_source_comp_desc.bmAttributes = isoc_mult;
  387. ss_iso_source_comp_desc.bMaxBurst = isoc_maxburst;
  388. ss_iso_source_comp_desc.wBytesPerInterval =
  389. isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
  390. ss_iso_source_desc.bEndpointAddress =
  391. fs_iso_source_desc.bEndpointAddress;
  392. ss_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
  393. ss_iso_sink_desc.bInterval = isoc_interval;
  394. ss_iso_sink_comp_desc.bmAttributes = isoc_mult;
  395. ss_iso_sink_comp_desc.bMaxBurst = isoc_maxburst;
  396. ss_iso_sink_comp_desc.wBytesPerInterval =
  397. isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
  398. ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
  399. ret = usb_assign_descriptors(f, fs_source_sink_descs,
  400. hs_source_sink_descs, ss_source_sink_descs);
  401. if (ret)
  402. return ret;
  403. DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
  404. (gadget_is_superspeed(c->cdev->gadget) ? "super" :
  405. (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
  406. f->name, ss->in_ep->name, ss->out_ep->name,
  407. ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
  408. ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
  409. return 0;
  410. }
  411. static void
  412. sourcesink_free_func(struct usb_function *f)
  413. {
  414. usb_free_all_descriptors(f);
  415. kfree(func_to_ss(f));
  416. }
  417. /* optionally require specific source/sink data patterns */
  418. static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
  419. {
  420. unsigned i;
  421. u8 *buf = req->buf;
  422. struct usb_composite_dev *cdev = ss->function.config->cdev;
  423. if (pattern == 2)
  424. return 0;
  425. for (i = 0; i < req->actual; i++, buf++) {
  426. switch (pattern) {
  427. /* all-zeroes has no synchronization issues */
  428. case 0:
  429. if (*buf == 0)
  430. continue;
  431. break;
  432. /* "mod63" stays in sync with short-terminated transfers,
  433. * OR otherwise when host and gadget agree on how large
  434. * each usb transfer request should be. Resync is done
  435. * with set_interface or set_config. (We *WANT* it to
  436. * get quickly out of sync if controllers or their drivers
  437. * stutter for any reason, including buffer duplication...)
  438. */
  439. case 1:
  440. if (*buf == (u8)(i % 63))
  441. continue;
  442. break;
  443. }
  444. ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
  445. usb_ep_set_halt(ss->out_ep);
  446. return -EINVAL;
  447. }
  448. return 0;
  449. }
  450. static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
  451. {
  452. unsigned i;
  453. u8 *buf = req->buf;
  454. switch (pattern) {
  455. case 0:
  456. memset(req->buf, 0, req->length);
  457. break;
  458. case 1:
  459. for (i = 0; i < req->length; i++)
  460. *buf++ = (u8) (i % 63);
  461. break;
  462. case 2:
  463. break;
  464. }
  465. }
  466. static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
  467. {
  468. struct usb_composite_dev *cdev;
  469. struct f_sourcesink *ss = ep->driver_data;
  470. int status = req->status;
  471. /* driver_data will be null if ep has been disabled */
  472. if (!ss)
  473. return;
  474. cdev = ss->function.config->cdev;
  475. switch (status) {
  476. case 0: /* normal completion? */
  477. if (ep == ss->out_ep) {
  478. check_read_data(ss, req);
  479. if (pattern != 2)
  480. memset(req->buf, 0x55, req->length);
  481. }
  482. break;
  483. /* this endpoint is normally active while we're configured */
  484. case -ECONNABORTED: /* hardware forced ep reset */
  485. case -ECONNRESET: /* request dequeued */
  486. case -ESHUTDOWN: /* disconnect from host */
  487. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  488. req->actual, req->length);
  489. if (ep == ss->out_ep)
  490. check_read_data(ss, req);
  491. free_ep_req(ep, req);
  492. return;
  493. case -EOVERFLOW: /* buffer overrun on read means that
  494. * we didn't provide a big enough
  495. * buffer.
  496. */
  497. default:
  498. #if 1
  499. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  500. status, req->actual, req->length);
  501. #endif
  502. case -EREMOTEIO: /* short read */
  503. break;
  504. }
  505. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  506. if (status) {
  507. ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
  508. ep->name, req->length, status);
  509. usb_ep_set_halt(ep);
  510. /* FIXME recover later ... somehow */
  511. }
  512. }
  513. static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
  514. bool is_iso, int speed)
  515. {
  516. struct usb_ep *ep;
  517. struct usb_request *req;
  518. int i, size, status;
  519. for (i = 0; i < 8; i++) {
  520. if (is_iso) {
  521. switch (speed) {
  522. case USB_SPEED_SUPER:
  523. size = isoc_maxpacket * (isoc_mult + 1) *
  524. (isoc_maxburst + 1);
  525. break;
  526. case USB_SPEED_HIGH:
  527. size = isoc_maxpacket * (isoc_mult + 1);
  528. break;
  529. default:
  530. size = isoc_maxpacket > 1023 ?
  531. 1023 : isoc_maxpacket;
  532. break;
  533. }
  534. ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
  535. req = alloc_ep_req(ep, size);
  536. } else {
  537. ep = is_in ? ss->in_ep : ss->out_ep;
  538. req = alloc_ep_req(ep, 0);
  539. }
  540. if (!req)
  541. return -ENOMEM;
  542. req->complete = source_sink_complete;
  543. if (is_in)
  544. reinit_write_data(ep, req);
  545. else if (pattern != 2)
  546. memset(req->buf, 0x55, req->length);
  547. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  548. if (status) {
  549. struct usb_composite_dev *cdev;
  550. cdev = ss->function.config->cdev;
  551. ERROR(cdev, "start %s%s %s --> %d\n",
  552. is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
  553. ep->name, status);
  554. free_ep_req(ep, req);
  555. }
  556. if (!is_iso)
  557. break;
  558. }
  559. return status;
  560. }
  561. static void disable_source_sink(struct f_sourcesink *ss)
  562. {
  563. struct usb_composite_dev *cdev;
  564. cdev = ss->function.config->cdev;
  565. disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
  566. ss->iso_out_ep);
  567. VDBG(cdev, "%s disabled\n", ss->function.name);
  568. }
  569. static int
  570. enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
  571. int alt)
  572. {
  573. int result = 0;
  574. int speed = cdev->gadget->speed;
  575. struct usb_ep *ep;
  576. /* one bulk endpoint writes (sources) zeroes IN (to the host) */
  577. ep = ss->in_ep;
  578. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  579. if (result)
  580. return result;
  581. result = usb_ep_enable(ep);
  582. if (result < 0)
  583. return result;
  584. ep->driver_data = ss;
  585. result = source_sink_start_ep(ss, true, false, speed);
  586. if (result < 0) {
  587. fail:
  588. ep = ss->in_ep;
  589. usb_ep_disable(ep);
  590. ep->driver_data = NULL;
  591. return result;
  592. }
  593. /* one bulk endpoint reads (sinks) anything OUT (from the host) */
  594. ep = ss->out_ep;
  595. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  596. if (result)
  597. goto fail;
  598. result = usb_ep_enable(ep);
  599. if (result < 0)
  600. goto fail;
  601. ep->driver_data = ss;
  602. result = source_sink_start_ep(ss, false, false, speed);
  603. if (result < 0) {
  604. fail2:
  605. ep = ss->out_ep;
  606. usb_ep_disable(ep);
  607. ep->driver_data = NULL;
  608. goto fail;
  609. }
  610. if (alt == 0)
  611. goto out;
  612. /* one iso endpoint writes (sources) zeroes IN (to the host) */
  613. ep = ss->iso_in_ep;
  614. if (ep) {
  615. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  616. if (result)
  617. goto fail2;
  618. result = usb_ep_enable(ep);
  619. if (result < 0)
  620. goto fail2;
  621. ep->driver_data = ss;
  622. result = source_sink_start_ep(ss, true, true, speed);
  623. if (result < 0) {
  624. fail3:
  625. ep = ss->iso_in_ep;
  626. if (ep) {
  627. usb_ep_disable(ep);
  628. ep->driver_data = NULL;
  629. }
  630. goto fail2;
  631. }
  632. }
  633. /* one iso endpoint reads (sinks) anything OUT (from the host) */
  634. ep = ss->iso_out_ep;
  635. if (ep) {
  636. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  637. if (result)
  638. goto fail3;
  639. result = usb_ep_enable(ep);
  640. if (result < 0)
  641. goto fail3;
  642. ep->driver_data = ss;
  643. result = source_sink_start_ep(ss, false, true, speed);
  644. if (result < 0) {
  645. usb_ep_disable(ep);
  646. ep->driver_data = NULL;
  647. goto fail3;
  648. }
  649. }
  650. out:
  651. ss->cur_alt = alt;
  652. DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
  653. return result;
  654. }
  655. static int sourcesink_set_alt(struct usb_function *f,
  656. unsigned intf, unsigned alt)
  657. {
  658. struct f_sourcesink *ss = func_to_ss(f);
  659. struct usb_composite_dev *cdev = f->config->cdev;
  660. if (ss->in_ep->driver_data)
  661. disable_source_sink(ss);
  662. return enable_source_sink(cdev, ss, alt);
  663. }
  664. static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
  665. {
  666. struct f_sourcesink *ss = func_to_ss(f);
  667. return ss->cur_alt;
  668. }
  669. static void sourcesink_disable(struct usb_function *f)
  670. {
  671. struct f_sourcesink *ss = func_to_ss(f);
  672. disable_source_sink(ss);
  673. }
  674. /*-------------------------------------------------------------------------*/
  675. static int sourcesink_setup(struct usb_function *f,
  676. const struct usb_ctrlrequest *ctrl)
  677. {
  678. struct usb_configuration *c = f->config;
  679. struct usb_request *req = c->cdev->req;
  680. int value = -EOPNOTSUPP;
  681. u16 w_index = le16_to_cpu(ctrl->wIndex);
  682. u16 w_value = le16_to_cpu(ctrl->wValue);
  683. u16 w_length = le16_to_cpu(ctrl->wLength);
  684. req->length = USB_COMP_EP0_BUFSIZ;
  685. /* composite driver infrastructure handles everything except
  686. * the two control test requests.
  687. */
  688. switch (ctrl->bRequest) {
  689. /*
  690. * These are the same vendor-specific requests supported by
  691. * Intel's USB 2.0 compliance test devices. We exceed that
  692. * device spec by allowing multiple-packet requests.
  693. *
  694. * NOTE: the Control-OUT data stays in req->buf ... better
  695. * would be copying it into a scratch buffer, so that other
  696. * requests may safely intervene.
  697. */
  698. case 0x5b: /* control WRITE test -- fill the buffer */
  699. if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  700. goto unknown;
  701. if (w_value || w_index)
  702. break;
  703. /* just read that many bytes into the buffer */
  704. if (w_length > req->length)
  705. break;
  706. value = w_length;
  707. break;
  708. case 0x5c: /* control READ test -- return the buffer */
  709. if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  710. goto unknown;
  711. if (w_value || w_index)
  712. break;
  713. /* expect those bytes are still in the buffer; send back */
  714. if (w_length > req->length)
  715. break;
  716. value = w_length;
  717. break;
  718. default:
  719. unknown:
  720. VDBG(c->cdev,
  721. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  722. ctrl->bRequestType, ctrl->bRequest,
  723. w_value, w_index, w_length);
  724. }
  725. /* respond with data transfer or status phase? */
  726. if (value >= 0) {
  727. VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
  728. ctrl->bRequestType, ctrl->bRequest,
  729. w_value, w_index, w_length);
  730. req->zero = 0;
  731. req->length = value;
  732. value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
  733. if (value < 0)
  734. ERROR(c->cdev, "source/sink response, err %d\n",
  735. value);
  736. }
  737. /* device either stalls (value < 0) or reports success */
  738. return value;
  739. }
  740. static struct usb_function *source_sink_alloc_func(
  741. struct usb_function_instance *fi)
  742. {
  743. struct f_sourcesink *ss;
  744. struct f_ss_opts *ss_opts;
  745. ss = kzalloc(sizeof(*ss), GFP_KERNEL);
  746. if (!ss)
  747. return NULL;
  748. ss_opts = container_of(fi, struct f_ss_opts, func_inst);
  749. pattern = ss_opts->pattern;
  750. isoc_interval = ss_opts->isoc_interval;
  751. isoc_maxpacket = ss_opts->isoc_maxpacket;
  752. isoc_mult = ss_opts->isoc_mult;
  753. isoc_maxburst = ss_opts->isoc_maxburst;
  754. buflen = ss_opts->bulk_buflen;
  755. ss->function.name = "source/sink";
  756. ss->function.bind = sourcesink_bind;
  757. ss->function.set_alt = sourcesink_set_alt;
  758. ss->function.get_alt = sourcesink_get_alt;
  759. ss->function.disable = sourcesink_disable;
  760. ss->function.setup = sourcesink_setup;
  761. ss->function.strings = sourcesink_strings;
  762. ss->function.free_func = sourcesink_free_func;
  763. return &ss->function;
  764. }
  765. static void acm_free_instance(struct usb_function_instance *fi)
  766. {
  767. struct f_ss_opts *ss_opts;
  768. ss_opts = container_of(fi, struct f_ss_opts, func_inst);
  769. kfree(ss_opts);
  770. }
  771. static struct usb_function_instance *source_sink_alloc_inst(void)
  772. {
  773. struct f_ss_opts *ss_opts;
  774. ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL);
  775. if (!ss_opts)
  776. return ERR_PTR(-ENOMEM);
  777. ss_opts->func_inst.free_func_inst = acm_free_instance;
  778. return &ss_opts->func_inst;
  779. }
  780. DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst,
  781. source_sink_alloc_func);
  782. static int __init sslb_modinit(void)
  783. {
  784. int ret;
  785. ret = usb_function_register(&SourceSinkusb_func);
  786. if (ret)
  787. return ret;
  788. ret = lb_modinit();
  789. if (ret)
  790. usb_function_unregister(&SourceSinkusb_func);
  791. return ret;
  792. }
  793. static void __exit sslb_modexit(void)
  794. {
  795. usb_function_unregister(&SourceSinkusb_func);
  796. lb_modexit();
  797. }
  798. module_init(sslb_modinit);
  799. module_exit(sslb_modexit);
  800. MODULE_LICENSE("GPL");