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.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* #define VERBOSE_DEBUG */
24 #include <linux/kernel.h>
25 #include <linux/utsname.h>
26 #include <linux/device.h>
29 #include "gadget_chips.h"
33 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
36 * This just sinks bulk packets OUT to the peripheral and sources them IN
37 * to the host, optionally with specific data patterns for integrity tests.
38 * As such it supports basic functionality and load tests.
40 * In terms of control messaging, this supports all the standard requests
41 * plus two that support control-OUT tests. If the optional "autoresume"
42 * mode is enabled, it provides good functional coverage for the "USBCV"
43 * test harness from USB-IF.
45 * Note that because this doesn't queue more than one request at a time,
46 * some other function must be used to test queueing logic. The network
47 * link (g_ether) is the best overall option for that, since its TX and RX
48 * queues are relatively independent, will receive a range of packet sizes,
49 * and can often be made to run out completely. Those issues are important
50 * when stress testing peripheral controller drivers.
53 * This is currently packaged as a configuration driver, which can't be
54 * combined with other functions to make composite devices. However, it
55 * can be combined with other independent configurations.
58 struct usb_function function;
61 struct usb_ep *out_ep;
62 struct timer_list resume;
65 static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
67 return container_of(f, struct f_sourcesink, function);
70 static unsigned autoresume;
71 module_param(autoresume, uint, 0);
72 MODULE_PARM_DESC(autoresume, "zero, or seconds before remote wakeup");
74 static unsigned pattern;
75 module_param(pattern, uint, 0);
76 MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63 ");
78 /*-------------------------------------------------------------------------*/
80 static struct usb_interface_descriptor source_sink_intf = {
81 .bLength = sizeof source_sink_intf,
82 .bDescriptorType = USB_DT_INTERFACE,
85 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
86 /* .iInterface = DYNAMIC */
89 /* full speed support: */
91 static struct usb_endpoint_descriptor fs_source_desc = {
92 .bLength = USB_DT_ENDPOINT_SIZE,
93 .bDescriptorType = USB_DT_ENDPOINT,
95 .bEndpointAddress = USB_DIR_IN,
96 .bmAttributes = USB_ENDPOINT_XFER_BULK,
99 static struct usb_endpoint_descriptor fs_sink_desc = {
100 .bLength = USB_DT_ENDPOINT_SIZE,
101 .bDescriptorType = USB_DT_ENDPOINT,
103 .bEndpointAddress = USB_DIR_OUT,
104 .bmAttributes = USB_ENDPOINT_XFER_BULK,
107 static struct usb_descriptor_header *fs_source_sink_descs[] = {
108 (struct usb_descriptor_header *) &source_sink_intf,
109 (struct usb_descriptor_header *) &fs_sink_desc,
110 (struct usb_descriptor_header *) &fs_source_desc,
114 /* high speed support: */
116 static struct usb_endpoint_descriptor hs_source_desc = {
117 .bLength = USB_DT_ENDPOINT_SIZE,
118 .bDescriptorType = USB_DT_ENDPOINT,
120 .bmAttributes = USB_ENDPOINT_XFER_BULK,
121 .wMaxPacketSize = __constant_cpu_to_le16(512),
124 static struct usb_endpoint_descriptor hs_sink_desc = {
125 .bLength = USB_DT_ENDPOINT_SIZE,
126 .bDescriptorType = USB_DT_ENDPOINT,
128 .bmAttributes = USB_ENDPOINT_XFER_BULK,
129 .wMaxPacketSize = __constant_cpu_to_le16(512),
132 static struct usb_descriptor_header *hs_source_sink_descs[] = {
133 (struct usb_descriptor_header *) &source_sink_intf,
134 (struct usb_descriptor_header *) &hs_source_desc,
135 (struct usb_descriptor_header *) &hs_sink_desc,
139 /* function-specific strings: */
141 static struct usb_string strings_sourcesink[] = {
142 [0].s = "source and sink data",
143 { } /* end of list */
146 static struct usb_gadget_strings stringtab_sourcesink = {
147 .language = 0x0409, /* en-us */
148 .strings = strings_sourcesink,
151 static struct usb_gadget_strings *sourcesink_strings[] = {
152 &stringtab_sourcesink,
156 /*-------------------------------------------------------------------------*/
158 static void sourcesink_autoresume(unsigned long _c)
160 struct usb_composite_dev *cdev = (void *)_c;
161 struct usb_gadget *g = cdev->gadget;
163 /* Normally the host would be woken up for something
164 * more significant than just a timer firing; likely
165 * because of some direct user request.
167 if (g->speed != USB_SPEED_UNKNOWN) {
168 int status = usb_gadget_wakeup(g);
169 DBG(cdev, "%s --> %d\n", __func__, status);
174 sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
176 struct usb_composite_dev *cdev = c->cdev;
177 struct f_sourcesink *ss = func_to_ss(f);
180 /* allocate interface ID(s) */
181 id = usb_interface_id(c, f);
184 source_sink_intf.bInterfaceNumber = id;
186 /* allocate endpoints */
187 ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
190 ERROR(cdev, "%s: can't autoconfigure on %s\n",
191 f->name, cdev->gadget->name);
194 ss->in_ep->driver_data = cdev; /* claim */
196 ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
199 ss->out_ep->driver_data = cdev; /* claim */
201 setup_timer(&ss->resume, sourcesink_autoresume,
202 (unsigned long) c->cdev);
204 /* support high speed hardware */
205 if (gadget_is_dualspeed(c->cdev->gadget)) {
206 hs_source_desc.bEndpointAddress =
207 fs_source_desc.bEndpointAddress;
208 hs_sink_desc.bEndpointAddress =
209 fs_sink_desc.bEndpointAddress;
210 f->hs_descriptors = hs_source_sink_descs;
213 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
214 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
215 f->name, ss->in_ep->name, ss->out_ep->name);
220 sourcesink_unbind(struct usb_configuration *c, struct usb_function *f)
222 kfree(func_to_ss(f));
225 /* optionally require specific source/sink data patterns */
226 static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
230 struct usb_composite_dev *cdev = ss->function.config->cdev;
232 for (i = 0; i < req->actual; i++, buf++) {
235 /* all-zeroes has no synchronization issues */
241 /* "mod63" stays in sync with short-terminated transfers,
242 * OR otherwise when host and gadget agree on how large
243 * each usb transfer request should be. Resync is done
244 * with set_interface or set_config. (We *WANT* it to
245 * get quickly out of sync if controllers or their drivers
246 * stutter for any reason, including buffer duplcation...)
249 if (*buf == (u8)(i % 63))
253 ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
254 usb_ep_set_halt(ss->out_ep);
260 static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
267 memset(req->buf, 0, req->length);
270 for (i = 0; i < req->length; i++)
271 *buf++ = (u8) (i % 63);
276 static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
278 struct f_sourcesink *ss = ep->driver_data;
279 struct usb_composite_dev *cdev = ss->function.config->cdev;
280 int status = req->status;
284 case 0: /* normal completion? */
285 if (ep == ss->out_ep) {
286 check_read_data(ss, req);
287 memset(req->buf, 0x55, req->length);
289 reinit_write_data(ep, req);
292 /* this endpoint is normally active while we're configured */
293 case -ECONNABORTED: /* hardware forced ep reset */
294 case -ECONNRESET: /* request dequeued */
295 case -ESHUTDOWN: /* disconnect from host */
296 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
297 req->actual, req->length);
298 if (ep == ss->out_ep)
299 check_read_data(ss, req);
300 free_ep_req(ep, req);
303 case -EOVERFLOW: /* buffer overrun on read means that
304 * we didn't provide a big enough
309 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
310 status, req->actual, req->length);
312 case -EREMOTEIO: /* short read */
316 status = usb_ep_queue(ep, req, GFP_ATOMIC);
318 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
319 ep->name, req->length, status);
321 /* FIXME recover later ... somehow */
325 static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in)
328 struct usb_request *req;
331 ep = is_in ? ss->in_ep : ss->out_ep;
332 req = alloc_ep_req(ep);
336 req->complete = source_sink_complete;
338 reinit_write_data(ep, req);
340 memset(req->buf, 0x55, req->length);
342 status = usb_ep_queue(ep, req, GFP_ATOMIC);
344 struct usb_composite_dev *cdev;
346 cdev = ss->function.config->cdev;
347 ERROR(cdev, "start %s %s --> %d\n",
348 is_in ? "IN" : "OUT",
350 free_ep_req(ep, req);
356 static void disable_source_sink(struct f_sourcesink *ss)
358 struct usb_composite_dev *cdev;
360 cdev = ss->function.config->cdev;
361 disable_endpoints(cdev, ss->in_ep, ss->out_ep);
362 del_timer(&ss->resume);
363 VDBG(cdev, "%s disabled\n", ss->function.name);
367 enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss)
370 const struct usb_endpoint_descriptor *src, *sink;
373 src = ep_choose(cdev->gadget, &hs_source_desc, &fs_source_desc);
374 sink = ep_choose(cdev->gadget, &hs_sink_desc, &fs_sink_desc);
376 /* one endpoint writes (sources) zeroes IN (to the host) */
378 result = usb_ep_enable(ep, src);
381 ep->driver_data = ss;
383 result = source_sink_start_ep(ss, true);
388 ep->driver_data = NULL;
392 /* one endpoint reads (sinks) anything OUT (from the host) */
394 result = usb_ep_enable(ep, sink);
397 ep->driver_data = ss;
399 result = source_sink_start_ep(ss, false);
402 ep->driver_data = NULL;
406 DBG(cdev, "%s enabled\n", ss->function.name);
410 static int sourcesink_set_alt(struct usb_function *f,
411 unsigned intf, unsigned alt)
413 struct f_sourcesink *ss = func_to_ss(f);
414 struct usb_composite_dev *cdev = f->config->cdev;
416 /* we know alt is zero */
417 if (ss->in_ep->driver_data)
418 disable_source_sink(ss);
419 return enable_source_sink(cdev, ss);
422 static void sourcesink_disable(struct usb_function *f)
424 struct f_sourcesink *ss = func_to_ss(f);
426 disable_source_sink(ss);
429 static void sourcesink_suspend(struct usb_function *f)
431 struct f_sourcesink *ss = func_to_ss(f);
432 struct usb_composite_dev *cdev = f->config->cdev;
434 if (cdev->gadget->speed == USB_SPEED_UNKNOWN)
438 mod_timer(&ss->resume, jiffies + (HZ * autoresume));
439 DBG(cdev, "suspend, wakeup in %d seconds\n", autoresume);
441 DBG(cdev, "%s\n", __func__);
444 static void sourcesink_resume(struct usb_function *f)
446 struct f_sourcesink *ss = func_to_ss(f);
447 struct usb_composite_dev *cdev = f->config->cdev;
449 DBG(cdev, "%s\n", __func__);
450 del_timer(&ss->resume);
453 /*-------------------------------------------------------------------------*/
455 static int __init sourcesink_bind_config(struct usb_configuration *c)
457 struct f_sourcesink *ss;
460 ss = kzalloc(sizeof *ss, GFP_KERNEL);
464 ss->function.name = "source/sink";
465 ss->function.descriptors = fs_source_sink_descs;
466 ss->function.bind = sourcesink_bind;
467 ss->function.unbind = sourcesink_unbind;
468 ss->function.set_alt = sourcesink_set_alt;
469 ss->function.disable = sourcesink_disable;
470 ss->function.suspend = sourcesink_suspend;
471 ss->function.resume = sourcesink_resume;
473 status = usb_add_function(c, &ss->function);
479 static int sourcesink_setup(struct usb_configuration *c,
480 const struct usb_ctrlrequest *ctrl)
482 struct usb_request *req = c->cdev->req;
483 int value = -EOPNOTSUPP;
484 u16 w_index = le16_to_cpu(ctrl->wIndex);
485 u16 w_value = le16_to_cpu(ctrl->wValue);
486 u16 w_length = le16_to_cpu(ctrl->wLength);
488 /* composite driver infrastructure handles everything except
489 * the two control test requests.
491 switch (ctrl->bRequest) {
494 * These are the same vendor-specific requests supported by
495 * Intel's USB 2.0 compliance test devices. We exceed that
496 * device spec by allowing multiple-packet requests.
498 * NOTE: the Control-OUT data stays in req->buf ... better
499 * would be copying it into a scratch buffer, so that other
500 * requests may safely intervene.
502 case 0x5b: /* control WRITE test -- fill the buffer */
503 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
505 if (w_value || w_index)
507 /* just read that many bytes into the buffer */
508 if (w_length > req->length)
512 case 0x5c: /* control READ test -- return the buffer */
513 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
515 if (w_value || w_index)
517 /* expect those bytes are still in the buffer; send back */
518 if (w_length > req->length)
526 "unknown control req%02x.%02x v%04x i%04x l%d\n",
527 ctrl->bRequestType, ctrl->bRequest,
528 w_value, w_index, w_length);
531 /* respond with data transfer or status phase? */
533 VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
534 ctrl->bRequestType, ctrl->bRequest,
535 w_value, w_index, w_length);
538 value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
540 ERROR(c->cdev, "source/sinkc response, err %d\n",
544 /* device either stalls (value < 0) or reports success */
548 static struct usb_configuration sourcesink_driver = {
549 .label = "source/sink",
550 .strings = sourcesink_strings,
551 .bind = sourcesink_bind_config,
552 .setup = sourcesink_setup,
553 .bConfigurationValue = 3,
554 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
555 /* .iConfiguration = DYNAMIC */
559 * sourcesink_add - add a source/sink testing configuration to a device
560 * @cdev: the device to support the configuration
562 int __init sourcesink_add(struct usb_composite_dev *cdev)
566 /* allocate string ID(s) */
567 id = usb_string_id(cdev);
570 strings_sourcesink[0].id = id;
572 source_sink_intf.iInterface = id;
573 sourcesink_driver.iConfiguration = id;
575 /* support autoresume for remote wakeup testing */
577 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
579 /* support OTG systems */
580 if (gadget_is_otg(cdev->gadget)) {
581 sourcesink_driver.descriptors = otg_desc;
582 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
585 return usb_add_config(cdev, &sourcesink_driver);