2 * f_sourcesink.c - USB peripheral source/sink configuration driver
4 * Copyright (C) 2003-2008 David Brownell
5 * Copyright (C) 2008 by Nokia Corporation
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.
13 /* #define VERBOSE_DEBUG */
15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #include <linux/device.h>
20 #include "gadget_chips.h"
24 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
27 * This just sinks bulk packets OUT to the peripheral and sources them IN
28 * to the host, optionally with specific data patterns for integrity tests.
29 * As such it supports basic functionality and load tests.
31 * In terms of control messaging, this supports all the standard requests
32 * plus two that support control-OUT tests. If the optional "autoresume"
33 * mode is enabled, it provides good functional coverage for the "USBCV"
34 * test harness from USB-IF.
36 * Note that because this doesn't queue more than one request at a time,
37 * some other function must be used to test queueing logic. The network
38 * link (g_ether) is the best overall option for that, since its TX and RX
39 * queues are relatively independent, will receive a range of packet sizes,
40 * and can often be made to run out completely. Those issues are important
41 * when stress testing peripheral controller drivers.
44 * This is currently packaged as a configuration driver, which can't be
45 * combined with other functions to make composite devices. However, it
46 * can be combined with other independent configurations.
49 struct usb_function function;
52 struct usb_ep *out_ep;
55 static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
57 return container_of(f, struct f_sourcesink, function);
60 static unsigned pattern;
61 module_param(pattern, uint, 0);
62 MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63 ");
64 /*-------------------------------------------------------------------------*/
66 static struct usb_interface_descriptor source_sink_intf = {
67 .bLength = sizeof source_sink_intf,
68 .bDescriptorType = USB_DT_INTERFACE,
71 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
72 /* .iInterface = DYNAMIC */
75 /* full speed support: */
77 static struct usb_endpoint_descriptor fs_source_desc = {
78 .bLength = USB_DT_ENDPOINT_SIZE,
79 .bDescriptorType = USB_DT_ENDPOINT,
81 .bEndpointAddress = USB_DIR_IN,
82 .bmAttributes = USB_ENDPOINT_XFER_BULK,
85 static struct usb_endpoint_descriptor fs_sink_desc = {
86 .bLength = USB_DT_ENDPOINT_SIZE,
87 .bDescriptorType = USB_DT_ENDPOINT,
89 .bEndpointAddress = USB_DIR_OUT,
90 .bmAttributes = USB_ENDPOINT_XFER_BULK,
93 static struct usb_descriptor_header *fs_source_sink_descs[] = {
94 (struct usb_descriptor_header *) &source_sink_intf,
95 (struct usb_descriptor_header *) &fs_sink_desc,
96 (struct usb_descriptor_header *) &fs_source_desc,
100 /* high speed support: */
102 static struct usb_endpoint_descriptor hs_source_desc = {
103 .bLength = USB_DT_ENDPOINT_SIZE,
104 .bDescriptorType = USB_DT_ENDPOINT,
106 .bmAttributes = USB_ENDPOINT_XFER_BULK,
107 .wMaxPacketSize = cpu_to_le16(512),
110 static struct usb_endpoint_descriptor hs_sink_desc = {
111 .bLength = USB_DT_ENDPOINT_SIZE,
112 .bDescriptorType = USB_DT_ENDPOINT,
114 .bmAttributes = USB_ENDPOINT_XFER_BULK,
115 .wMaxPacketSize = cpu_to_le16(512),
118 static struct usb_descriptor_header *hs_source_sink_descs[] = {
119 (struct usb_descriptor_header *) &source_sink_intf,
120 (struct usb_descriptor_header *) &hs_source_desc,
121 (struct usb_descriptor_header *) &hs_sink_desc,
125 /* super speed support: */
127 static struct usb_endpoint_descriptor ss_source_desc = {
128 .bLength = USB_DT_ENDPOINT_SIZE,
129 .bDescriptorType = USB_DT_ENDPOINT,
131 .bmAttributes = USB_ENDPOINT_XFER_BULK,
132 .wMaxPacketSize = cpu_to_le16(1024),
135 struct usb_ss_ep_comp_descriptor ss_source_comp_desc = {
136 .bLength = USB_DT_SS_EP_COMP_SIZE,
137 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
140 .wBytesPerInterval = 0,
143 static struct usb_endpoint_descriptor ss_sink_desc = {
144 .bLength = USB_DT_ENDPOINT_SIZE,
145 .bDescriptorType = USB_DT_ENDPOINT,
147 .bmAttributes = USB_ENDPOINT_XFER_BULK,
148 .wMaxPacketSize = cpu_to_le16(1024),
151 struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = {
152 .bLength = USB_DT_SS_EP_COMP_SIZE,
153 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
156 .wBytesPerInterval = 0,
159 static struct usb_descriptor_header *ss_source_sink_descs[] = {
160 (struct usb_descriptor_header *) &source_sink_intf,
161 (struct usb_descriptor_header *) &ss_source_desc,
162 (struct usb_descriptor_header *) &ss_source_comp_desc,
163 (struct usb_descriptor_header *) &ss_sink_desc,
164 (struct usb_descriptor_header *) &ss_sink_comp_desc,
168 /* function-specific strings: */
170 static struct usb_string strings_sourcesink[] = {
171 [0].s = "source and sink data",
172 { } /* end of list */
175 static struct usb_gadget_strings stringtab_sourcesink = {
176 .language = 0x0409, /* en-us */
177 .strings = strings_sourcesink,
180 static struct usb_gadget_strings *sourcesink_strings[] = {
181 &stringtab_sourcesink,
185 /*-------------------------------------------------------------------------*/
188 sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
190 struct usb_composite_dev *cdev = c->cdev;
191 struct f_sourcesink *ss = func_to_ss(f);
194 /* allocate interface ID(s) */
195 id = usb_interface_id(c, f);
198 source_sink_intf.bInterfaceNumber = id;
200 /* allocate endpoints */
201 ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
204 ERROR(cdev, "%s: can't autoconfigure on %s\n",
205 f->name, cdev->gadget->name);
208 ss->in_ep->driver_data = cdev; /* claim */
210 ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
213 ss->out_ep->driver_data = cdev; /* claim */
215 /* support high speed hardware */
216 if (gadget_is_dualspeed(c->cdev->gadget)) {
217 hs_source_desc.bEndpointAddress =
218 fs_source_desc.bEndpointAddress;
219 hs_sink_desc.bEndpointAddress =
220 fs_sink_desc.bEndpointAddress;
221 f->hs_descriptors = hs_source_sink_descs;
224 /* support super speed hardware */
225 if (gadget_is_superspeed(c->cdev->gadget)) {
226 ss_source_desc.bEndpointAddress =
227 fs_source_desc.bEndpointAddress;
228 ss_sink_desc.bEndpointAddress =
229 fs_sink_desc.bEndpointAddress;
230 f->ss_descriptors = ss_source_sink_descs;
233 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
234 (gadget_is_superspeed(c->cdev->gadget) ? "super" :
235 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
236 f->name, ss->in_ep->name, ss->out_ep->name);
241 sourcesink_unbind(struct usb_configuration *c, struct usb_function *f)
243 kfree(func_to_ss(f));
246 /* optionally require specific source/sink data patterns */
247 static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
251 struct usb_composite_dev *cdev = ss->function.config->cdev;
253 for (i = 0; i < req->actual; i++, buf++) {
256 /* all-zeroes has no synchronization issues */
262 /* "mod63" stays in sync with short-terminated transfers,
263 * OR otherwise when host and gadget agree on how large
264 * each usb transfer request should be. Resync is done
265 * with set_interface or set_config. (We *WANT* it to
266 * get quickly out of sync if controllers or their drivers
267 * stutter for any reason, including buffer duplcation...)
270 if (*buf == (u8)(i % 63))
274 ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
275 usb_ep_set_halt(ss->out_ep);
281 static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
288 memset(req->buf, 0, req->length);
291 for (i = 0; i < req->length; i++)
292 *buf++ = (u8) (i % 63);
297 static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
299 struct f_sourcesink *ss = ep->driver_data;
300 struct usb_composite_dev *cdev = ss->function.config->cdev;
301 int status = req->status;
305 case 0: /* normal completion? */
306 if (ep == ss->out_ep) {
307 check_read_data(ss, req);
308 memset(req->buf, 0x55, req->length);
310 reinit_write_data(ep, req);
313 /* this endpoint is normally active while we're configured */
314 case -ECONNABORTED: /* hardware forced ep reset */
315 case -ECONNRESET: /* request dequeued */
316 case -ESHUTDOWN: /* disconnect from host */
317 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
318 req->actual, req->length);
319 if (ep == ss->out_ep)
320 check_read_data(ss, req);
321 free_ep_req(ep, req);
324 case -EOVERFLOW: /* buffer overrun on read means that
325 * we didn't provide a big enough
330 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
331 status, req->actual, req->length);
333 case -EREMOTEIO: /* short read */
337 status = usb_ep_queue(ep, req, GFP_ATOMIC);
339 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
340 ep->name, req->length, status);
342 /* FIXME recover later ... somehow */
346 static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in)
349 struct usb_request *req;
352 ep = is_in ? ss->in_ep : ss->out_ep;
353 req = alloc_ep_req(ep);
357 req->complete = source_sink_complete;
359 reinit_write_data(ep, req);
361 memset(req->buf, 0x55, req->length);
363 status = usb_ep_queue(ep, req, GFP_ATOMIC);
365 struct usb_composite_dev *cdev;
367 cdev = ss->function.config->cdev;
368 ERROR(cdev, "start %s %s --> %d\n",
369 is_in ? "IN" : "OUT",
371 free_ep_req(ep, req);
377 static void disable_source_sink(struct f_sourcesink *ss)
379 struct usb_composite_dev *cdev;
381 cdev = ss->function.config->cdev;
382 disable_endpoints(cdev, ss->in_ep, ss->out_ep);
383 VDBG(cdev, "%s disabled\n", ss->function.name);
387 enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss)
392 /* one endpoint writes (sources) zeroes IN (to the host) */
394 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
397 result = usb_ep_enable(ep);
400 ep->driver_data = ss;
402 result = source_sink_start_ep(ss, true);
407 ep->driver_data = NULL;
411 /* one endpoint reads (sinks) anything OUT (from the host) */
413 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
416 result = usb_ep_enable(ep);
419 ep->driver_data = ss;
421 result = source_sink_start_ep(ss, false);
424 ep->driver_data = NULL;
428 DBG(cdev, "%s enabled\n", ss->function.name);
432 static int sourcesink_set_alt(struct usb_function *f,
433 unsigned intf, unsigned alt)
435 struct f_sourcesink *ss = func_to_ss(f);
436 struct usb_composite_dev *cdev = f->config->cdev;
438 /* we know alt is zero */
439 if (ss->in_ep->driver_data)
440 disable_source_sink(ss);
441 return enable_source_sink(cdev, ss);
444 static void sourcesink_disable(struct usb_function *f)
446 struct f_sourcesink *ss = func_to_ss(f);
448 disable_source_sink(ss);
451 /*-------------------------------------------------------------------------*/
453 static int __init sourcesink_bind_config(struct usb_configuration *c)
455 struct f_sourcesink *ss;
458 ss = kzalloc(sizeof *ss, GFP_KERNEL);
462 ss->function.name = "source/sink";
463 ss->function.descriptors = fs_source_sink_descs;
464 ss->function.bind = sourcesink_bind;
465 ss->function.unbind = sourcesink_unbind;
466 ss->function.set_alt = sourcesink_set_alt;
467 ss->function.disable = sourcesink_disable;
469 status = usb_add_function(c, &ss->function);
475 static int sourcesink_setup(struct usb_configuration *c,
476 const struct usb_ctrlrequest *ctrl)
478 struct usb_request *req = c->cdev->req;
479 int value = -EOPNOTSUPP;
480 u16 w_index = le16_to_cpu(ctrl->wIndex);
481 u16 w_value = le16_to_cpu(ctrl->wValue);
482 u16 w_length = le16_to_cpu(ctrl->wLength);
484 req->length = USB_BUFSIZ;
486 /* composite driver infrastructure handles everything except
487 * the two control test requests.
489 switch (ctrl->bRequest) {
492 * These are the same vendor-specific requests supported by
493 * Intel's USB 2.0 compliance test devices. We exceed that
494 * device spec by allowing multiple-packet requests.
496 * NOTE: the Control-OUT data stays in req->buf ... better
497 * would be copying it into a scratch buffer, so that other
498 * requests may safely intervene.
500 case 0x5b: /* control WRITE test -- fill the buffer */
501 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
503 if (w_value || w_index)
505 /* just read that many bytes into the buffer */
506 if (w_length > req->length)
510 case 0x5c: /* control READ test -- return the buffer */
511 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
513 if (w_value || w_index)
515 /* expect those bytes are still in the buffer; send back */
516 if (w_length > req->length)
524 "unknown control req%02x.%02x v%04x i%04x l%d\n",
525 ctrl->bRequestType, ctrl->bRequest,
526 w_value, w_index, w_length);
529 /* respond with data transfer or status phase? */
531 VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
532 ctrl->bRequestType, ctrl->bRequest,
533 w_value, w_index, w_length);
536 value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
538 ERROR(c->cdev, "source/sinkc response, err %d\n",
542 /* device either stalls (value < 0) or reports success */
546 static struct usb_configuration sourcesink_driver = {
547 .label = "source/sink",
548 .strings = sourcesink_strings,
549 .setup = sourcesink_setup,
550 .bConfigurationValue = 3,
551 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
552 /* .iConfiguration = DYNAMIC */
556 * sourcesink_add - add a source/sink testing configuration to a device
557 * @cdev: the device to support the configuration
559 int __init sourcesink_add(struct usb_composite_dev *cdev, bool autoresume)
563 /* allocate string ID(s) */
564 id = usb_string_id(cdev);
567 strings_sourcesink[0].id = id;
569 source_sink_intf.iInterface = id;
570 sourcesink_driver.iConfiguration = id;
572 /* support autoresume for remote wakeup testing */
574 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
576 /* support OTG systems */
577 if (gadget_is_otg(cdev->gadget)) {
578 sourcesink_driver.descriptors = otg_desc;
579 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
582 return usb_add_config(cdev, &sourcesink_driver, sourcesink_bind_config);