f_sourcesink.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. int ret;
  262. /* allocate interface ID(s) */
  263. id = usb_interface_id(c, f);
  264. if (id < 0)
  265. return id;
  266. source_sink_intf_alt0.bInterfaceNumber = id;
  267. source_sink_intf_alt1.bInterfaceNumber = id;
  268. /* allocate bulk endpoints */
  269. ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
  270. if (!ss->in_ep) {
  271. autoconf_fail:
  272. ERROR(cdev, "%s: can't autoconfigure on %s\n",
  273. f->name, cdev->gadget->name);
  274. return -ENODEV;
  275. }
  276. ss->in_ep->driver_data = cdev; /* claim */
  277. ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
  278. if (!ss->out_ep)
  279. goto autoconf_fail;
  280. ss->out_ep->driver_data = cdev; /* claim */
  281. /* sanity check the isoc module parameters */
  282. if (isoc_interval < 1)
  283. isoc_interval = 1;
  284. if (isoc_interval > 16)
  285. isoc_interval = 16;
  286. if (isoc_mult > 2)
  287. isoc_mult = 2;
  288. if (isoc_maxburst > 15)
  289. isoc_maxburst = 15;
  290. /* fill in the FS isoc descriptors from the module parameters */
  291. fs_iso_source_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
  292. 1023 : isoc_maxpacket;
  293. fs_iso_source_desc.bInterval = isoc_interval;
  294. fs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
  295. 1023 : isoc_maxpacket;
  296. fs_iso_sink_desc.bInterval = isoc_interval;
  297. /* allocate iso endpoints */
  298. ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
  299. if (!ss->iso_in_ep)
  300. goto no_iso;
  301. ss->iso_in_ep->driver_data = cdev; /* claim */
  302. ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
  303. if (ss->iso_out_ep) {
  304. ss->iso_out_ep->driver_data = cdev; /* claim */
  305. } else {
  306. ss->iso_in_ep->driver_data = NULL;
  307. ss->iso_in_ep = NULL;
  308. no_iso:
  309. /*
  310. * We still want to work even if the UDC doesn't have isoc
  311. * endpoints, so null out the alt interface that contains
  312. * them and continue.
  313. */
  314. fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
  315. hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
  316. ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
  317. }
  318. if (isoc_maxpacket > 1024)
  319. isoc_maxpacket = 1024;
  320. /* support high speed hardware */
  321. hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  322. hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  323. /*
  324. * Fill in the HS isoc descriptors from the module parameters.
  325. * We assume that the user knows what they are doing and won't
  326. * give parameters that their UDC doesn't support.
  327. */
  328. hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
  329. hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11;
  330. hs_iso_source_desc.bInterval = isoc_interval;
  331. hs_iso_source_desc.bEndpointAddress =
  332. fs_iso_source_desc.bEndpointAddress;
  333. hs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
  334. hs_iso_sink_desc.wMaxPacketSize |= isoc_mult << 11;
  335. hs_iso_sink_desc.bInterval = isoc_interval;
  336. hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
  337. /* support super speed hardware */
  338. ss_source_desc.bEndpointAddress =
  339. fs_source_desc.bEndpointAddress;
  340. ss_sink_desc.bEndpointAddress =
  341. fs_sink_desc.bEndpointAddress;
  342. /*
  343. * Fill in the SS isoc descriptors from the module parameters.
  344. * We assume that the user knows what they are doing and won't
  345. * give parameters that their UDC doesn't support.
  346. */
  347. ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
  348. ss_iso_source_desc.bInterval = isoc_interval;
  349. ss_iso_source_comp_desc.bmAttributes = isoc_mult;
  350. ss_iso_source_comp_desc.bMaxBurst = isoc_maxburst;
  351. ss_iso_source_comp_desc.wBytesPerInterval =
  352. isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
  353. ss_iso_source_desc.bEndpointAddress =
  354. fs_iso_source_desc.bEndpointAddress;
  355. ss_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
  356. ss_iso_sink_desc.bInterval = isoc_interval;
  357. ss_iso_sink_comp_desc.bmAttributes = isoc_mult;
  358. ss_iso_sink_comp_desc.bMaxBurst = isoc_maxburst;
  359. ss_iso_sink_comp_desc.wBytesPerInterval =
  360. isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
  361. ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
  362. ret = usb_assign_descriptors(f, fs_source_sink_descs,
  363. hs_source_sink_descs, ss_source_sink_descs);
  364. if (ret)
  365. return ret;
  366. DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
  367. (gadget_is_superspeed(c->cdev->gadget) ? "super" :
  368. (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
  369. f->name, ss->in_ep->name, ss->out_ep->name,
  370. ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
  371. ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
  372. return 0;
  373. }
  374. static struct usb_function *global_ss_func;
  375. static void
  376. sourcesink_unbind(struct usb_configuration *c, struct usb_function *f)
  377. {
  378. usb_free_all_descriptors(f);
  379. kfree(func_to_ss(f));
  380. global_ss_func = NULL;
  381. }
  382. /* optionally require specific source/sink data patterns */
  383. static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
  384. {
  385. unsigned i;
  386. u8 *buf = req->buf;
  387. struct usb_composite_dev *cdev = ss->function.config->cdev;
  388. if (pattern == 2)
  389. return 0;
  390. for (i = 0; i < req->actual; i++, buf++) {
  391. switch (pattern) {
  392. /* all-zeroes has no synchronization issues */
  393. case 0:
  394. if (*buf == 0)
  395. continue;
  396. break;
  397. /* "mod63" stays in sync with short-terminated transfers,
  398. * OR otherwise when host and gadget agree on how large
  399. * each usb transfer request should be. Resync is done
  400. * with set_interface or set_config. (We *WANT* it to
  401. * get quickly out of sync if controllers or their drivers
  402. * stutter for any reason, including buffer duplication...)
  403. */
  404. case 1:
  405. if (*buf == (u8)(i % 63))
  406. continue;
  407. break;
  408. }
  409. ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
  410. usb_ep_set_halt(ss->out_ep);
  411. return -EINVAL;
  412. }
  413. return 0;
  414. }
  415. static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
  416. {
  417. unsigned i;
  418. u8 *buf = req->buf;
  419. switch (pattern) {
  420. case 0:
  421. memset(req->buf, 0, req->length);
  422. break;
  423. case 1:
  424. for (i = 0; i < req->length; i++)
  425. *buf++ = (u8) (i % 63);
  426. break;
  427. case 2:
  428. break;
  429. }
  430. }
  431. static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
  432. {
  433. struct usb_composite_dev *cdev;
  434. struct f_sourcesink *ss = ep->driver_data;
  435. int status = req->status;
  436. /* driver_data will be null if ep has been disabled */
  437. if (!ss)
  438. return;
  439. cdev = ss->function.config->cdev;
  440. switch (status) {
  441. case 0: /* normal completion? */
  442. if (ep == ss->out_ep) {
  443. check_read_data(ss, req);
  444. if (pattern != 2)
  445. memset(req->buf, 0x55, req->length);
  446. }
  447. break;
  448. /* this endpoint is normally active while we're configured */
  449. case -ECONNABORTED: /* hardware forced ep reset */
  450. case -ECONNRESET: /* request dequeued */
  451. case -ESHUTDOWN: /* disconnect from host */
  452. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  453. req->actual, req->length);
  454. if (ep == ss->out_ep)
  455. check_read_data(ss, req);
  456. free_ep_req(ep, req);
  457. return;
  458. case -EOVERFLOW: /* buffer overrun on read means that
  459. * we didn't provide a big enough
  460. * buffer.
  461. */
  462. default:
  463. #if 1
  464. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  465. status, req->actual, req->length);
  466. #endif
  467. case -EREMOTEIO: /* short read */
  468. break;
  469. }
  470. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  471. if (status) {
  472. ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
  473. ep->name, req->length, status);
  474. usb_ep_set_halt(ep);
  475. /* FIXME recover later ... somehow */
  476. }
  477. }
  478. static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
  479. bool is_iso, int speed)
  480. {
  481. struct usb_ep *ep;
  482. struct usb_request *req;
  483. int i, size, status;
  484. for (i = 0; i < 8; i++) {
  485. if (is_iso) {
  486. switch (speed) {
  487. case USB_SPEED_SUPER:
  488. size = isoc_maxpacket * (isoc_mult + 1) *
  489. (isoc_maxburst + 1);
  490. break;
  491. case USB_SPEED_HIGH:
  492. size = isoc_maxpacket * (isoc_mult + 1);
  493. break;
  494. default:
  495. size = isoc_maxpacket > 1023 ?
  496. 1023 : isoc_maxpacket;
  497. break;
  498. }
  499. ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
  500. req = alloc_ep_req(ep, size);
  501. } else {
  502. ep = is_in ? ss->in_ep : ss->out_ep;
  503. req = alloc_ep_req(ep, 0);
  504. }
  505. if (!req)
  506. return -ENOMEM;
  507. req->complete = source_sink_complete;
  508. if (is_in)
  509. reinit_write_data(ep, req);
  510. else if (pattern != 2)
  511. memset(req->buf, 0x55, req->length);
  512. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  513. if (status) {
  514. struct usb_composite_dev *cdev;
  515. cdev = ss->function.config->cdev;
  516. ERROR(cdev, "start %s%s %s --> %d\n",
  517. is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
  518. ep->name, status);
  519. free_ep_req(ep, req);
  520. }
  521. if (!is_iso)
  522. break;
  523. }
  524. return status;
  525. }
  526. static void disable_source_sink(struct f_sourcesink *ss)
  527. {
  528. struct usb_composite_dev *cdev;
  529. cdev = ss->function.config->cdev;
  530. disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
  531. ss->iso_out_ep);
  532. VDBG(cdev, "%s disabled\n", ss->function.name);
  533. }
  534. static int
  535. enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
  536. int alt)
  537. {
  538. int result = 0;
  539. int speed = cdev->gadget->speed;
  540. struct usb_ep *ep;
  541. /* one bulk endpoint writes (sources) zeroes IN (to the host) */
  542. ep = ss->in_ep;
  543. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  544. if (result)
  545. return result;
  546. result = usb_ep_enable(ep);
  547. if (result < 0)
  548. return result;
  549. ep->driver_data = ss;
  550. result = source_sink_start_ep(ss, true, false, speed);
  551. if (result < 0) {
  552. fail:
  553. ep = ss->in_ep;
  554. usb_ep_disable(ep);
  555. ep->driver_data = NULL;
  556. return result;
  557. }
  558. /* one bulk endpoint reads (sinks) anything OUT (from the host) */
  559. ep = ss->out_ep;
  560. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  561. if (result)
  562. goto fail;
  563. result = usb_ep_enable(ep);
  564. if (result < 0)
  565. goto fail;
  566. ep->driver_data = ss;
  567. result = source_sink_start_ep(ss, false, false, speed);
  568. if (result < 0) {
  569. fail2:
  570. ep = ss->out_ep;
  571. usb_ep_disable(ep);
  572. ep->driver_data = NULL;
  573. goto fail;
  574. }
  575. if (alt == 0)
  576. goto out;
  577. /* one iso endpoint writes (sources) zeroes IN (to the host) */
  578. ep = ss->iso_in_ep;
  579. if (ep) {
  580. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  581. if (result)
  582. goto fail2;
  583. result = usb_ep_enable(ep);
  584. if (result < 0)
  585. goto fail2;
  586. ep->driver_data = ss;
  587. result = source_sink_start_ep(ss, true, true, speed);
  588. if (result < 0) {
  589. fail3:
  590. ep = ss->iso_in_ep;
  591. if (ep) {
  592. usb_ep_disable(ep);
  593. ep->driver_data = NULL;
  594. }
  595. goto fail2;
  596. }
  597. }
  598. /* one iso endpoint reads (sinks) anything OUT (from the host) */
  599. ep = ss->iso_out_ep;
  600. if (ep) {
  601. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  602. if (result)
  603. goto fail3;
  604. result = usb_ep_enable(ep);
  605. if (result < 0)
  606. goto fail3;
  607. ep->driver_data = ss;
  608. result = source_sink_start_ep(ss, false, true, speed);
  609. if (result < 0) {
  610. usb_ep_disable(ep);
  611. ep->driver_data = NULL;
  612. goto fail3;
  613. }
  614. }
  615. out:
  616. ss->cur_alt = alt;
  617. DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
  618. return result;
  619. }
  620. static int sourcesink_set_alt(struct usb_function *f,
  621. unsigned intf, unsigned alt)
  622. {
  623. struct f_sourcesink *ss = func_to_ss(f);
  624. struct usb_composite_dev *cdev = f->config->cdev;
  625. if (ss->in_ep->driver_data)
  626. disable_source_sink(ss);
  627. return enable_source_sink(cdev, ss, alt);
  628. }
  629. static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
  630. {
  631. struct f_sourcesink *ss = func_to_ss(f);
  632. return ss->cur_alt;
  633. }
  634. static void sourcesink_disable(struct usb_function *f)
  635. {
  636. struct f_sourcesink *ss = func_to_ss(f);
  637. disable_source_sink(ss);
  638. }
  639. /*-------------------------------------------------------------------------*/
  640. static int sourcesink_setup(struct usb_function *f,
  641. const struct usb_ctrlrequest *ctrl)
  642. {
  643. struct usb_configuration *c = f->config;
  644. struct usb_request *req = c->cdev->req;
  645. int value = -EOPNOTSUPP;
  646. u16 w_index = le16_to_cpu(ctrl->wIndex);
  647. u16 w_value = le16_to_cpu(ctrl->wValue);
  648. u16 w_length = le16_to_cpu(ctrl->wLength);
  649. req->length = USB_COMP_EP0_BUFSIZ;
  650. /* composite driver infrastructure handles everything except
  651. * the two control test requests.
  652. */
  653. switch (ctrl->bRequest) {
  654. /*
  655. * These are the same vendor-specific requests supported by
  656. * Intel's USB 2.0 compliance test devices. We exceed that
  657. * device spec by allowing multiple-packet requests.
  658. *
  659. * NOTE: the Control-OUT data stays in req->buf ... better
  660. * would be copying it into a scratch buffer, so that other
  661. * requests may safely intervene.
  662. */
  663. case 0x5b: /* control WRITE test -- fill the buffer */
  664. if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  665. goto unknown;
  666. if (w_value || w_index)
  667. break;
  668. /* just read that many bytes into the buffer */
  669. if (w_length > req->length)
  670. break;
  671. value = w_length;
  672. break;
  673. case 0x5c: /* control READ test -- return the buffer */
  674. if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  675. goto unknown;
  676. if (w_value || w_index)
  677. break;
  678. /* expect those bytes are still in the buffer; send back */
  679. if (w_length > req->length)
  680. break;
  681. value = w_length;
  682. break;
  683. default:
  684. unknown:
  685. VDBG(c->cdev,
  686. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  687. ctrl->bRequestType, ctrl->bRequest,
  688. w_value, w_index, w_length);
  689. }
  690. /* respond with data transfer or status phase? */
  691. if (value >= 0) {
  692. VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
  693. ctrl->bRequestType, ctrl->bRequest,
  694. w_value, w_index, w_length);
  695. req->zero = 0;
  696. req->length = value;
  697. value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
  698. if (value < 0)
  699. ERROR(c->cdev, "source/sink response, err %d\n",
  700. value);
  701. }
  702. /* device either stalls (value < 0) or reports success */
  703. return value;
  704. }
  705. static int __init sourcesink_bind_config(struct usb_configuration *c)
  706. {
  707. struct f_sourcesink *ss;
  708. int status;
  709. ss = kzalloc(sizeof(*ss), GFP_KERNEL);
  710. if (!ss)
  711. return -ENOMEM;
  712. global_ss_func = &ss->function;
  713. ss->function.name = "source/sink";
  714. ss->function.bind = sourcesink_bind;
  715. ss->function.unbind = sourcesink_unbind;
  716. ss->function.set_alt = sourcesink_set_alt;
  717. ss->function.get_alt = sourcesink_get_alt;
  718. ss->function.disable = sourcesink_disable;
  719. ss->function.setup = sourcesink_setup;
  720. status = usb_add_function(c, &ss->function);
  721. if (status)
  722. kfree(ss);
  723. return status;
  724. }
  725. static int ss_config_setup(struct usb_configuration *c,
  726. const struct usb_ctrlrequest *ctrl)
  727. {
  728. if (!global_ss_func)
  729. return -EOPNOTSUPP;
  730. switch (ctrl->bRequest) {
  731. case 0x5b:
  732. case 0x5c:
  733. return global_ss_func->setup(global_ss_func, ctrl);
  734. default:
  735. return -EOPNOTSUPP;
  736. }
  737. }
  738. static struct usb_configuration sourcesink_driver = {
  739. .label = "source/sink",
  740. .strings = sourcesink_strings,
  741. .setup = ss_config_setup,
  742. .bConfigurationValue = 3,
  743. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  744. /* .iConfiguration = DYNAMIC */
  745. };
  746. /**
  747. * sourcesink_add - add a source/sink testing configuration to a device
  748. * @cdev: the device to support the configuration
  749. */
  750. int __init sourcesink_add(struct usb_composite_dev *cdev, bool autoresume)
  751. {
  752. int id;
  753. /* allocate string ID(s) */
  754. id = usb_string_id(cdev);
  755. if (id < 0)
  756. return id;
  757. strings_sourcesink[0].id = id;
  758. source_sink_intf_alt0.iInterface = id;
  759. source_sink_intf_alt1.iInterface = id;
  760. sourcesink_driver.iConfiguration = id;
  761. /* support autoresume for remote wakeup testing */
  762. if (autoresume)
  763. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  764. /* support OTG systems */
  765. if (gadget_is_otg(cdev->gadget)) {
  766. sourcesink_driver.descriptors = otg_desc;
  767. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  768. }
  769. return usb_add_config(cdev, &sourcesink_driver, sourcesink_bind_config);
  770. }