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/device.h>
28 #include "gadget_chips.h"
32 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
35 * This just sinks bulk packets OUT to the peripheral and sources them IN
36 * to the host, optionally with specific data patterns for integrity tests.
37 * As such it supports basic functionality and load tests.
39 * In terms of control messaging, this supports all the standard requests
40 * plus two that support control-OUT tests. If the optional "autoresume"
41 * mode is enabled, it provides good functional coverage for the "USBCV"
42 * test harness from USB-IF.
44 * Note that because this doesn't queue more than one request at a time,
45 * some other function must be used to test queueing logic. The network
46 * link (g_ether) is the best overall option for that, since its TX and RX
47 * queues are relatively independent, will receive a range of packet sizes,
48 * and can often be made to run out completely. Those issues are important
49 * when stress testing peripheral controller drivers.
52 * This is currently packaged as a configuration driver, which can't be
53 * combined with other functions to make composite devices. However, it
54 * can be combined with other independent configurations.
57 struct usb_function function
;
60 struct usb_ep
*out_ep
;
63 static inline struct f_sourcesink
*func_to_ss(struct usb_function
*f
)
65 return container_of(f
, struct f_sourcesink
, function
);
68 static unsigned pattern
;
69 module_param(pattern
, uint
, 0);
70 MODULE_PARM_DESC(pattern
, "0 = all zeroes, 1 = mod63 ");
72 /*-------------------------------------------------------------------------*/
74 static struct usb_interface_descriptor source_sink_intf
= {
75 .bLength
= sizeof source_sink_intf
,
76 .bDescriptorType
= USB_DT_INTERFACE
,
79 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
80 /* .iInterface = DYNAMIC */
83 /* full speed support: */
85 static struct usb_endpoint_descriptor fs_source_desc
= {
86 .bLength
= USB_DT_ENDPOINT_SIZE
,
87 .bDescriptorType
= USB_DT_ENDPOINT
,
89 .bEndpointAddress
= USB_DIR_IN
,
90 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
93 static struct usb_endpoint_descriptor fs_sink_desc
= {
94 .bLength
= USB_DT_ENDPOINT_SIZE
,
95 .bDescriptorType
= USB_DT_ENDPOINT
,
97 .bEndpointAddress
= USB_DIR_OUT
,
98 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
101 static struct usb_descriptor_header
*fs_source_sink_descs
[] = {
102 (struct usb_descriptor_header
*) &source_sink_intf
,
103 (struct usb_descriptor_header
*) &fs_sink_desc
,
104 (struct usb_descriptor_header
*) &fs_source_desc
,
108 /* high speed support: */
110 static struct usb_endpoint_descriptor hs_source_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_endpoint_descriptor hs_sink_desc
= {
119 .bLength
= USB_DT_ENDPOINT_SIZE
,
120 .bDescriptorType
= USB_DT_ENDPOINT
,
122 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
123 .wMaxPacketSize
= cpu_to_le16(512),
126 static struct usb_descriptor_header
*hs_source_sink_descs
[] = {
127 (struct usb_descriptor_header
*) &source_sink_intf
,
128 (struct usb_descriptor_header
*) &hs_source_desc
,
129 (struct usb_descriptor_header
*) &hs_sink_desc
,
133 /* function-specific strings: */
135 static struct usb_string strings_sourcesink
[] = {
136 [0].s
= "source and sink data",
137 { } /* end of list */
140 static struct usb_gadget_strings stringtab_sourcesink
= {
141 .language
= 0x0409, /* en-us */
142 .strings
= strings_sourcesink
,
145 static struct usb_gadget_strings
*sourcesink_strings
[] = {
146 &stringtab_sourcesink
,
150 /*-------------------------------------------------------------------------*/
153 sourcesink_bind(struct usb_configuration
*c
, struct usb_function
*f
)
155 struct usb_composite_dev
*cdev
= c
->cdev
;
156 struct f_sourcesink
*ss
= func_to_ss(f
);
159 /* allocate interface ID(s) */
160 id
= usb_interface_id(c
, f
);
163 source_sink_intf
.bInterfaceNumber
= id
;
165 /* allocate endpoints */
166 ss
->in_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_source_desc
);
169 ERROR(cdev
, "%s: can't autoconfigure on %s\n",
170 f
->name
, cdev
->gadget
->name
);
173 ss
->in_ep
->driver_data
= cdev
; /* claim */
175 ss
->out_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_sink_desc
);
178 ss
->out_ep
->driver_data
= cdev
; /* claim */
180 /* support high speed hardware */
181 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
182 hs_source_desc
.bEndpointAddress
=
183 fs_source_desc
.bEndpointAddress
;
184 hs_sink_desc
.bEndpointAddress
=
185 fs_sink_desc
.bEndpointAddress
;
186 f
->hs_descriptors
= hs_source_sink_descs
;
189 DBG(cdev
, "%s speed %s: IN/%s, OUT/%s\n",
190 gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full",
191 f
->name
, ss
->in_ep
->name
, ss
->out_ep
->name
);
196 sourcesink_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
198 kfree(func_to_ss(f
));
201 /* optionally require specific source/sink data patterns */
202 static int check_read_data(struct f_sourcesink
*ss
, struct usb_request
*req
)
206 struct usb_composite_dev
*cdev
= ss
->function
.config
->cdev
;
208 for (i
= 0; i
< req
->actual
; i
++, buf
++) {
211 /* all-zeroes has no synchronization issues */
217 /* "mod63" stays in sync with short-terminated transfers,
218 * OR otherwise when host and gadget agree on how large
219 * each usb transfer request should be. Resync is done
220 * with set_interface or set_config. (We *WANT* it to
221 * get quickly out of sync if controllers or their drivers
222 * stutter for any reason, including buffer duplcation...)
225 if (*buf
== (u8
)(i
% 63))
229 ERROR(cdev
, "bad OUT byte, buf[%d] = %d\n", i
, *buf
);
230 usb_ep_set_halt(ss
->out_ep
);
236 static void reinit_write_data(struct usb_ep
*ep
, struct usb_request
*req
)
243 memset(req
->buf
, 0, req
->length
);
246 for (i
= 0; i
< req
->length
; i
++)
247 *buf
++ = (u8
) (i
% 63);
252 static void source_sink_complete(struct usb_ep
*ep
, struct usb_request
*req
)
254 struct f_sourcesink
*ss
= ep
->driver_data
;
255 struct usb_composite_dev
*cdev
= ss
->function
.config
->cdev
;
256 int status
= req
->status
;
260 case 0: /* normal completion? */
261 if (ep
== ss
->out_ep
) {
262 check_read_data(ss
, req
);
263 memset(req
->buf
, 0x55, req
->length
);
265 reinit_write_data(ep
, req
);
268 /* this endpoint is normally active while we're configured */
269 case -ECONNABORTED
: /* hardware forced ep reset */
270 case -ECONNRESET
: /* request dequeued */
271 case -ESHUTDOWN
: /* disconnect from host */
272 VDBG(cdev
, "%s gone (%d), %d/%d\n", ep
->name
, status
,
273 req
->actual
, req
->length
);
274 if (ep
== ss
->out_ep
)
275 check_read_data(ss
, req
);
276 free_ep_req(ep
, req
);
279 case -EOVERFLOW
: /* buffer overrun on read means that
280 * we didn't provide a big enough
285 DBG(cdev
, "%s complete --> %d, %d/%d\n", ep
->name
,
286 status
, req
->actual
, req
->length
);
288 case -EREMOTEIO
: /* short read */
292 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
294 ERROR(cdev
, "kill %s: resubmit %d bytes --> %d\n",
295 ep
->name
, req
->length
, status
);
297 /* FIXME recover later ... somehow */
301 static int source_sink_start_ep(struct f_sourcesink
*ss
, bool is_in
)
304 struct usb_request
*req
;
307 ep
= is_in
? ss
->in_ep
: ss
->out_ep
;
308 req
= alloc_ep_req(ep
);
312 req
->complete
= source_sink_complete
;
314 reinit_write_data(ep
, req
);
316 memset(req
->buf
, 0x55, req
->length
);
318 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
320 struct usb_composite_dev
*cdev
;
322 cdev
= ss
->function
.config
->cdev
;
323 ERROR(cdev
, "start %s %s --> %d\n",
324 is_in
? "IN" : "OUT",
326 free_ep_req(ep
, req
);
332 static void disable_source_sink(struct f_sourcesink
*ss
)
334 struct usb_composite_dev
*cdev
;
336 cdev
= ss
->function
.config
->cdev
;
337 disable_endpoints(cdev
, ss
->in_ep
, ss
->out_ep
);
338 VDBG(cdev
, "%s disabled\n", ss
->function
.name
);
342 enable_source_sink(struct usb_composite_dev
*cdev
, struct f_sourcesink
*ss
)
345 const struct usb_endpoint_descriptor
*src
, *sink
;
348 src
= ep_choose(cdev
->gadget
, &hs_source_desc
, &fs_source_desc
);
349 sink
= ep_choose(cdev
->gadget
, &hs_sink_desc
, &fs_sink_desc
);
351 /* one endpoint writes (sources) zeroes IN (to the host) */
353 result
= usb_ep_enable(ep
, src
);
356 ep
->driver_data
= ss
;
358 result
= source_sink_start_ep(ss
, true);
363 ep
->driver_data
= NULL
;
367 /* one endpoint reads (sinks) anything OUT (from the host) */
369 result
= usb_ep_enable(ep
, sink
);
372 ep
->driver_data
= ss
;
374 result
= source_sink_start_ep(ss
, false);
377 ep
->driver_data
= NULL
;
381 DBG(cdev
, "%s enabled\n", ss
->function
.name
);
385 static int sourcesink_set_alt(struct usb_function
*f
,
386 unsigned intf
, unsigned alt
)
388 struct f_sourcesink
*ss
= func_to_ss(f
);
389 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
391 /* we know alt is zero */
392 if (ss
->in_ep
->driver_data
)
393 disable_source_sink(ss
);
394 return enable_source_sink(cdev
, ss
);
397 static void sourcesink_disable(struct usb_function
*f
)
399 struct f_sourcesink
*ss
= func_to_ss(f
);
401 disable_source_sink(ss
);
404 /*-------------------------------------------------------------------------*/
406 static int __init
sourcesink_bind_config(struct usb_configuration
*c
)
408 struct f_sourcesink
*ss
;
411 ss
= kzalloc(sizeof *ss
, GFP_KERNEL
);
415 ss
->function
.name
= "source/sink";
416 ss
->function
.descriptors
= fs_source_sink_descs
;
417 ss
->function
.bind
= sourcesink_bind
;
418 ss
->function
.unbind
= sourcesink_unbind
;
419 ss
->function
.set_alt
= sourcesink_set_alt
;
420 ss
->function
.disable
= sourcesink_disable
;
422 status
= usb_add_function(c
, &ss
->function
);
428 static int sourcesink_setup(struct usb_configuration
*c
,
429 const struct usb_ctrlrequest
*ctrl
)
431 struct usb_request
*req
= c
->cdev
->req
;
432 int value
= -EOPNOTSUPP
;
433 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
434 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
435 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
437 /* composite driver infrastructure handles everything except
438 * the two control test requests.
440 switch (ctrl
->bRequest
) {
443 * These are the same vendor-specific requests supported by
444 * Intel's USB 2.0 compliance test devices. We exceed that
445 * device spec by allowing multiple-packet requests.
447 * NOTE: the Control-OUT data stays in req->buf ... better
448 * would be copying it into a scratch buffer, so that other
449 * requests may safely intervene.
451 case 0x5b: /* control WRITE test -- fill the buffer */
452 if (ctrl
->bRequestType
!= (USB_DIR_OUT
|USB_TYPE_VENDOR
))
454 if (w_value
|| w_index
)
456 /* just read that many bytes into the buffer */
457 if (w_length
> req
->length
)
461 case 0x5c: /* control READ test -- return the buffer */
462 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_TYPE_VENDOR
))
464 if (w_value
|| w_index
)
466 /* expect those bytes are still in the buffer; send back */
467 if (w_length
> req
->length
)
475 "unknown control req%02x.%02x v%04x i%04x l%d\n",
476 ctrl
->bRequestType
, ctrl
->bRequest
,
477 w_value
, w_index
, w_length
);
480 /* respond with data transfer or status phase? */
482 VDBG(c
->cdev
, "source/sink req%02x.%02x v%04x i%04x l%d\n",
483 ctrl
->bRequestType
, ctrl
->bRequest
,
484 w_value
, w_index
, w_length
);
487 value
= usb_ep_queue(c
->cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
489 ERROR(c
->cdev
, "source/sinkc response, err %d\n",
493 /* device either stalls (value < 0) or reports success */
497 static struct usb_configuration sourcesink_driver
= {
498 .label
= "source/sink",
499 .strings
= sourcesink_strings
,
500 .bind
= sourcesink_bind_config
,
501 .setup
= sourcesink_setup
,
502 .bConfigurationValue
= 3,
503 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
504 /* .iConfiguration = DYNAMIC */
508 * sourcesink_add - add a source/sink testing configuration to a device
509 * @cdev: the device to support the configuration
511 int __init
sourcesink_add(struct usb_composite_dev
*cdev
, bool autoresume
)
515 /* allocate string ID(s) */
516 id
= usb_string_id(cdev
);
519 strings_sourcesink
[0].id
= id
;
521 source_sink_intf
.iInterface
= id
;
522 sourcesink_driver
.iConfiguration
= id
;
524 /* support autoresume for remote wakeup testing */
526 sourcesink_driver
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
528 /* support OTG systems */
529 if (gadget_is_otg(cdev
->gadget
)) {
530 sourcesink_driver
.descriptors
= otg_desc
;
531 sourcesink_driver
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
534 return usb_add_config(cdev
, &sourcesink_driver
);