f_sourcesink.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 "g_zero.h"
  17. #include "gadget_chips.h"
  18. /*
  19. * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
  20. * controller drivers.
  21. *
  22. * This just sinks bulk packets OUT to the peripheral and sources them IN
  23. * to the host, optionally with specific data patterns for integrity tests.
  24. * As such it supports basic functionality and load tests.
  25. *
  26. * In terms of control messaging, this supports all the standard requests
  27. * plus two that support control-OUT tests. If the optional "autoresume"
  28. * mode is enabled, it provides good functional coverage for the "USBCV"
  29. * test harness from USB-IF.
  30. *
  31. * Note that because this doesn't queue more than one request at a time,
  32. * some other function must be used to test queueing logic. The network
  33. * link (g_ether) is the best overall option for that, since its TX and RX
  34. * queues are relatively independent, will receive a range of packet sizes,
  35. * and can often be made to run out completely. Those issues are important
  36. * when stress testing peripheral controller drivers.
  37. *
  38. *
  39. * This is currently packaged as a configuration driver, which can't be
  40. * combined with other functions to make composite devices. However, it
  41. * can be combined with other independent configurations.
  42. */
  43. struct f_sourcesink {
  44. struct usb_function function;
  45. struct usb_ep *in_ep;
  46. struct usb_ep *out_ep;
  47. };
  48. static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
  49. {
  50. return container_of(f, struct f_sourcesink, function);
  51. }
  52. static unsigned pattern;
  53. module_param(pattern, uint, 0);
  54. MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63 ");
  55. /*-------------------------------------------------------------------------*/
  56. static struct usb_interface_descriptor source_sink_intf = {
  57. .bLength = sizeof source_sink_intf,
  58. .bDescriptorType = USB_DT_INTERFACE,
  59. .bNumEndpoints = 2,
  60. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  61. /* .iInterface = DYNAMIC */
  62. };
  63. /* full speed support: */
  64. static struct usb_endpoint_descriptor fs_source_desc = {
  65. .bLength = USB_DT_ENDPOINT_SIZE,
  66. .bDescriptorType = USB_DT_ENDPOINT,
  67. .bEndpointAddress = USB_DIR_IN,
  68. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  69. };
  70. static struct usb_endpoint_descriptor fs_sink_desc = {
  71. .bLength = USB_DT_ENDPOINT_SIZE,
  72. .bDescriptorType = USB_DT_ENDPOINT,
  73. .bEndpointAddress = USB_DIR_OUT,
  74. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  75. };
  76. static struct usb_descriptor_header *fs_source_sink_descs[] = {
  77. (struct usb_descriptor_header *) &source_sink_intf,
  78. (struct usb_descriptor_header *) &fs_sink_desc,
  79. (struct usb_descriptor_header *) &fs_source_desc,
  80. NULL,
  81. };
  82. /* high speed support: */
  83. static struct usb_endpoint_descriptor hs_source_desc = {
  84. .bLength = USB_DT_ENDPOINT_SIZE,
  85. .bDescriptorType = USB_DT_ENDPOINT,
  86. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  87. .wMaxPacketSize = cpu_to_le16(512),
  88. };
  89. static struct usb_endpoint_descriptor hs_sink_desc = {
  90. .bLength = USB_DT_ENDPOINT_SIZE,
  91. .bDescriptorType = USB_DT_ENDPOINT,
  92. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  93. .wMaxPacketSize = cpu_to_le16(512),
  94. };
  95. static struct usb_descriptor_header *hs_source_sink_descs[] = {
  96. (struct usb_descriptor_header *) &source_sink_intf,
  97. (struct usb_descriptor_header *) &hs_source_desc,
  98. (struct usb_descriptor_header *) &hs_sink_desc,
  99. NULL,
  100. };
  101. /* super speed support: */
  102. static struct usb_endpoint_descriptor ss_source_desc = {
  103. .bLength = USB_DT_ENDPOINT_SIZE,
  104. .bDescriptorType = USB_DT_ENDPOINT,
  105. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  106. .wMaxPacketSize = cpu_to_le16(1024),
  107. };
  108. struct usb_ss_ep_comp_descriptor ss_source_comp_desc = {
  109. .bLength = USB_DT_SS_EP_COMP_SIZE,
  110. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  111. .bMaxBurst = 0,
  112. .bmAttributes = 0,
  113. .wBytesPerInterval = 0,
  114. };
  115. static struct usb_endpoint_descriptor ss_sink_desc = {
  116. .bLength = USB_DT_ENDPOINT_SIZE,
  117. .bDescriptorType = USB_DT_ENDPOINT,
  118. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  119. .wMaxPacketSize = cpu_to_le16(1024),
  120. };
  121. struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = {
  122. .bLength = USB_DT_SS_EP_COMP_SIZE,
  123. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  124. .bMaxBurst = 0,
  125. .bmAttributes = 0,
  126. .wBytesPerInterval = 0,
  127. };
  128. static struct usb_descriptor_header *ss_source_sink_descs[] = {
  129. (struct usb_descriptor_header *) &source_sink_intf,
  130. (struct usb_descriptor_header *) &ss_source_desc,
  131. (struct usb_descriptor_header *) &ss_source_comp_desc,
  132. (struct usb_descriptor_header *) &ss_sink_desc,
  133. (struct usb_descriptor_header *) &ss_sink_comp_desc,
  134. NULL,
  135. };
  136. /* function-specific strings: */
  137. static struct usb_string strings_sourcesink[] = {
  138. [0].s = "source and sink data",
  139. { } /* end of list */
  140. };
  141. static struct usb_gadget_strings stringtab_sourcesink = {
  142. .language = 0x0409, /* en-us */
  143. .strings = strings_sourcesink,
  144. };
  145. static struct usb_gadget_strings *sourcesink_strings[] = {
  146. &stringtab_sourcesink,
  147. NULL,
  148. };
  149. /*-------------------------------------------------------------------------*/
  150. static int __init
  151. sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
  152. {
  153. struct usb_composite_dev *cdev = c->cdev;
  154. struct f_sourcesink *ss = func_to_ss(f);
  155. int id;
  156. /* allocate interface ID(s) */
  157. id = usb_interface_id(c, f);
  158. if (id < 0)
  159. return id;
  160. source_sink_intf.bInterfaceNumber = id;
  161. /* allocate endpoints */
  162. ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
  163. if (!ss->in_ep) {
  164. autoconf_fail:
  165. ERROR(cdev, "%s: can't autoconfigure on %s\n",
  166. f->name, cdev->gadget->name);
  167. return -ENODEV;
  168. }
  169. ss->in_ep->driver_data = cdev; /* claim */
  170. ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
  171. if (!ss->out_ep)
  172. goto autoconf_fail;
  173. ss->out_ep->driver_data = cdev; /* claim */
  174. /* support high speed hardware */
  175. if (gadget_is_dualspeed(c->cdev->gadget)) {
  176. hs_source_desc.bEndpointAddress =
  177. fs_source_desc.bEndpointAddress;
  178. hs_sink_desc.bEndpointAddress =
  179. fs_sink_desc.bEndpointAddress;
  180. f->hs_descriptors = hs_source_sink_descs;
  181. }
  182. /* support super speed hardware */
  183. if (gadget_is_superspeed(c->cdev->gadget)) {
  184. ss_source_desc.bEndpointAddress =
  185. fs_source_desc.bEndpointAddress;
  186. ss_sink_desc.bEndpointAddress =
  187. fs_sink_desc.bEndpointAddress;
  188. f->ss_descriptors = ss_source_sink_descs;
  189. }
  190. DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
  191. (gadget_is_superspeed(c->cdev->gadget) ? "super" :
  192. (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
  193. f->name, ss->in_ep->name, ss->out_ep->name);
  194. return 0;
  195. }
  196. static void
  197. sourcesink_unbind(struct usb_configuration *c, struct usb_function *f)
  198. {
  199. kfree(func_to_ss(f));
  200. }
  201. /* optionally require specific source/sink data patterns */
  202. static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
  203. {
  204. unsigned i;
  205. u8 *buf = req->buf;
  206. struct usb_composite_dev *cdev = ss->function.config->cdev;
  207. for (i = 0; i < req->actual; i++, buf++) {
  208. switch (pattern) {
  209. /* all-zeroes has no synchronization issues */
  210. case 0:
  211. if (*buf == 0)
  212. continue;
  213. break;
  214. /* "mod63" stays in sync with short-terminated transfers,
  215. * OR otherwise when host and gadget agree on how large
  216. * each usb transfer request should be. Resync is done
  217. * with set_interface or set_config. (We *WANT* it to
  218. * get quickly out of sync if controllers or their drivers
  219. * stutter for any reason, including buffer duplcation...)
  220. */
  221. case 1:
  222. if (*buf == (u8)(i % 63))
  223. continue;
  224. break;
  225. }
  226. ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
  227. usb_ep_set_halt(ss->out_ep);
  228. return -EINVAL;
  229. }
  230. return 0;
  231. }
  232. static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
  233. {
  234. unsigned i;
  235. u8 *buf = req->buf;
  236. switch (pattern) {
  237. case 0:
  238. memset(req->buf, 0, req->length);
  239. break;
  240. case 1:
  241. for (i = 0; i < req->length; i++)
  242. *buf++ = (u8) (i % 63);
  243. break;
  244. }
  245. }
  246. static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
  247. {
  248. struct f_sourcesink *ss = ep->driver_data;
  249. struct usb_composite_dev *cdev = ss->function.config->cdev;
  250. int status = req->status;
  251. switch (status) {
  252. case 0: /* normal completion? */
  253. if (ep == ss->out_ep) {
  254. check_read_data(ss, req);
  255. memset(req->buf, 0x55, req->length);
  256. } else
  257. reinit_write_data(ep, req);
  258. break;
  259. /* this endpoint is normally active while we're configured */
  260. case -ECONNABORTED: /* hardware forced ep reset */
  261. case -ECONNRESET: /* request dequeued */
  262. case -ESHUTDOWN: /* disconnect from host */
  263. VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
  264. req->actual, req->length);
  265. if (ep == ss->out_ep)
  266. check_read_data(ss, req);
  267. free_ep_req(ep, req);
  268. return;
  269. case -EOVERFLOW: /* buffer overrun on read means that
  270. * we didn't provide a big enough
  271. * buffer.
  272. */
  273. default:
  274. #if 1
  275. DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
  276. status, req->actual, req->length);
  277. #endif
  278. case -EREMOTEIO: /* short read */
  279. break;
  280. }
  281. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  282. if (status) {
  283. ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
  284. ep->name, req->length, status);
  285. usb_ep_set_halt(ep);
  286. /* FIXME recover later ... somehow */
  287. }
  288. }
  289. static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in)
  290. {
  291. struct usb_ep *ep;
  292. struct usb_request *req;
  293. int status;
  294. ep = is_in ? ss->in_ep : ss->out_ep;
  295. req = alloc_ep_req(ep);
  296. if (!req)
  297. return -ENOMEM;
  298. req->complete = source_sink_complete;
  299. if (is_in)
  300. reinit_write_data(ep, req);
  301. else
  302. memset(req->buf, 0x55, req->length);
  303. status = usb_ep_queue(ep, req, GFP_ATOMIC);
  304. if (status) {
  305. struct usb_composite_dev *cdev;
  306. cdev = ss->function.config->cdev;
  307. ERROR(cdev, "start %s %s --> %d\n",
  308. is_in ? "IN" : "OUT",
  309. ep->name, status);
  310. free_ep_req(ep, req);
  311. }
  312. return status;
  313. }
  314. static void disable_source_sink(struct f_sourcesink *ss)
  315. {
  316. struct usb_composite_dev *cdev;
  317. cdev = ss->function.config->cdev;
  318. disable_endpoints(cdev, ss->in_ep, ss->out_ep);
  319. VDBG(cdev, "%s disabled\n", ss->function.name);
  320. }
  321. static int
  322. enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss)
  323. {
  324. int result = 0;
  325. struct usb_ep *ep;
  326. /* one endpoint writes (sources) zeroes IN (to the host) */
  327. ep = ss->in_ep;
  328. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  329. if (result)
  330. return result;
  331. result = usb_ep_enable(ep);
  332. if (result < 0)
  333. return result;
  334. ep->driver_data = ss;
  335. result = source_sink_start_ep(ss, true);
  336. if (result < 0) {
  337. fail:
  338. ep = ss->in_ep;
  339. usb_ep_disable(ep);
  340. ep->driver_data = NULL;
  341. return result;
  342. }
  343. /* one endpoint reads (sinks) anything OUT (from the host) */
  344. ep = ss->out_ep;
  345. result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
  346. if (result)
  347. goto fail;
  348. result = usb_ep_enable(ep);
  349. if (result < 0)
  350. goto fail;
  351. ep->driver_data = ss;
  352. result = source_sink_start_ep(ss, false);
  353. if (result < 0) {
  354. usb_ep_disable(ep);
  355. ep->driver_data = NULL;
  356. goto fail;
  357. }
  358. DBG(cdev, "%s enabled\n", ss->function.name);
  359. return result;
  360. }
  361. static int sourcesink_set_alt(struct usb_function *f,
  362. unsigned intf, unsigned alt)
  363. {
  364. struct f_sourcesink *ss = func_to_ss(f);
  365. struct usb_composite_dev *cdev = f->config->cdev;
  366. /* we know alt is zero */
  367. if (ss->in_ep->driver_data)
  368. disable_source_sink(ss);
  369. return enable_source_sink(cdev, ss);
  370. }
  371. static void sourcesink_disable(struct usb_function *f)
  372. {
  373. struct f_sourcesink *ss = func_to_ss(f);
  374. disable_source_sink(ss);
  375. }
  376. /*-------------------------------------------------------------------------*/
  377. static int __init sourcesink_bind_config(struct usb_configuration *c)
  378. {
  379. struct f_sourcesink *ss;
  380. int status;
  381. ss = kzalloc(sizeof *ss, GFP_KERNEL);
  382. if (!ss)
  383. return -ENOMEM;
  384. ss->function.name = "source/sink";
  385. ss->function.descriptors = fs_source_sink_descs;
  386. ss->function.bind = sourcesink_bind;
  387. ss->function.unbind = sourcesink_unbind;
  388. ss->function.set_alt = sourcesink_set_alt;
  389. ss->function.disable = sourcesink_disable;
  390. status = usb_add_function(c, &ss->function);
  391. if (status)
  392. kfree(ss);
  393. return status;
  394. }
  395. static int sourcesink_setup(struct usb_configuration *c,
  396. const struct usb_ctrlrequest *ctrl)
  397. {
  398. struct usb_request *req = c->cdev->req;
  399. int value = -EOPNOTSUPP;
  400. u16 w_index = le16_to_cpu(ctrl->wIndex);
  401. u16 w_value = le16_to_cpu(ctrl->wValue);
  402. u16 w_length = le16_to_cpu(ctrl->wLength);
  403. req->length = USB_BUFSIZ;
  404. /* composite driver infrastructure handles everything except
  405. * the two control test requests.
  406. */
  407. switch (ctrl->bRequest) {
  408. /*
  409. * These are the same vendor-specific requests supported by
  410. * Intel's USB 2.0 compliance test devices. We exceed that
  411. * device spec by allowing multiple-packet requests.
  412. *
  413. * NOTE: the Control-OUT data stays in req->buf ... better
  414. * would be copying it into a scratch buffer, so that other
  415. * requests may safely intervene.
  416. */
  417. case 0x5b: /* control WRITE test -- fill the buffer */
  418. if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  419. goto unknown;
  420. if (w_value || w_index)
  421. break;
  422. /* just read that many bytes into the buffer */
  423. if (w_length > req->length)
  424. break;
  425. value = w_length;
  426. break;
  427. case 0x5c: /* control READ test -- return the buffer */
  428. if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  429. goto unknown;
  430. if (w_value || w_index)
  431. break;
  432. /* expect those bytes are still in the buffer; send back */
  433. if (w_length > req->length)
  434. break;
  435. value = w_length;
  436. break;
  437. default:
  438. unknown:
  439. VDBG(c->cdev,
  440. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  441. ctrl->bRequestType, ctrl->bRequest,
  442. w_value, w_index, w_length);
  443. }
  444. /* respond with data transfer or status phase? */
  445. if (value >= 0) {
  446. VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
  447. ctrl->bRequestType, ctrl->bRequest,
  448. w_value, w_index, w_length);
  449. req->zero = 0;
  450. req->length = value;
  451. value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
  452. if (value < 0)
  453. ERROR(c->cdev, "source/sinkc response, err %d\n",
  454. value);
  455. }
  456. /* device either stalls (value < 0) or reports success */
  457. return value;
  458. }
  459. static struct usb_configuration sourcesink_driver = {
  460. .label = "source/sink",
  461. .strings = sourcesink_strings,
  462. .setup = sourcesink_setup,
  463. .bConfigurationValue = 3,
  464. .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
  465. /* .iConfiguration = DYNAMIC */
  466. };
  467. /**
  468. * sourcesink_add - add a source/sink testing configuration to a device
  469. * @cdev: the device to support the configuration
  470. */
  471. int __init sourcesink_add(struct usb_composite_dev *cdev, bool autoresume)
  472. {
  473. int id;
  474. /* allocate string ID(s) */
  475. id = usb_string_id(cdev);
  476. if (id < 0)
  477. return id;
  478. strings_sourcesink[0].id = id;
  479. source_sink_intf.iInterface = id;
  480. sourcesink_driver.iConfiguration = id;
  481. /* support autoresume for remote wakeup testing */
  482. if (autoresume)
  483. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  484. /* support OTG systems */
  485. if (gadget_is_otg(cdev->gadget)) {
  486. sourcesink_driver.descriptors = otg_desc;
  487. sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  488. }
  489. return usb_add_config(cdev, &sourcesink_driver, sourcesink_bind_config);
  490. }