f_sourcesink.c 24 KB

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