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>
18 #include <linux/module.h>
21 #include "gadget_chips.h"
25 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
28 * This just sinks bulk packets OUT to the peripheral and sources them IN
29 * to the host, optionally with specific data patterns for integrity tests.
30 * As such it supports basic functionality and load tests.
32 * In terms of control messaging, this supports all the standard requests
33 * plus two that support control-OUT tests. If the optional "autoresume"
34 * mode is enabled, it provides good functional coverage for the "USBCV"
35 * test harness from USB-IF.
37 * Note that because this doesn't queue more than one request at a time,
38 * some other function must be used to test queueing logic. The network
39 * link (g_ether) is the best overall option for that, since its TX and RX
40 * queues are relatively independent, will receive a range of packet sizes,
41 * and can often be made to run out completely. Those issues are important
42 * when stress testing peripheral controller drivers.
45 * This is currently packaged as a configuration driver, which can't be
46 * combined with other functions to make composite devices. However, it
47 * can be combined with other independent configurations.
50 struct usb_function function
;
53 struct usb_ep
*out_ep
;
54 struct usb_ep
*iso_in_ep
;
55 struct usb_ep
*iso_out_ep
;
59 static inline struct f_sourcesink
*func_to_ss(struct usb_function
*f
)
61 return container_of(f
, struct f_sourcesink
, function
);
64 static unsigned pattern
;
65 module_param(pattern
, uint
, S_IRUGO
|S_IWUSR
);
66 MODULE_PARM_DESC(pattern
, "0 = all zeroes, 1 = mod63, 2 = none");
68 static unsigned isoc_interval
= 4;
69 module_param(isoc_interval
, uint
, S_IRUGO
|S_IWUSR
);
70 MODULE_PARM_DESC(isoc_interval
, "1 - 16");
72 static unsigned isoc_maxpacket
= 1024;
73 module_param(isoc_maxpacket
, uint
, S_IRUGO
|S_IWUSR
);
74 MODULE_PARM_DESC(isoc_maxpacket
, "0 - 1023 (fs), 0 - 1024 (hs/ss)");
76 static unsigned isoc_mult
;
77 module_param(isoc_mult
, uint
, S_IRUGO
|S_IWUSR
);
78 MODULE_PARM_DESC(isoc_mult
, "0 - 2 (hs/ss only)");
80 static unsigned isoc_maxburst
;
81 module_param(isoc_maxburst
, uint
, S_IRUGO
|S_IWUSR
);
82 MODULE_PARM_DESC(isoc_maxburst
, "0 - 15 (ss only)");
84 /*-------------------------------------------------------------------------*/
86 static struct usb_interface_descriptor source_sink_intf_alt0
= {
87 .bLength
= USB_DT_INTERFACE_SIZE
,
88 .bDescriptorType
= USB_DT_INTERFACE
,
90 .bAlternateSetting
= 0,
92 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
93 /* .iInterface = DYNAMIC */
96 static struct usb_interface_descriptor source_sink_intf_alt1
= {
97 .bLength
= USB_DT_INTERFACE_SIZE
,
98 .bDescriptorType
= USB_DT_INTERFACE
,
100 .bAlternateSetting
= 1,
102 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
103 /* .iInterface = DYNAMIC */
106 /* full speed support: */
108 static struct usb_endpoint_descriptor fs_source_desc
= {
109 .bLength
= USB_DT_ENDPOINT_SIZE
,
110 .bDescriptorType
= USB_DT_ENDPOINT
,
112 .bEndpointAddress
= USB_DIR_IN
,
113 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
116 static struct usb_endpoint_descriptor fs_sink_desc
= {
117 .bLength
= USB_DT_ENDPOINT_SIZE
,
118 .bDescriptorType
= USB_DT_ENDPOINT
,
120 .bEndpointAddress
= USB_DIR_OUT
,
121 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
124 static struct usb_endpoint_descriptor fs_iso_source_desc
= {
125 .bLength
= USB_DT_ENDPOINT_SIZE
,
126 .bDescriptorType
= USB_DT_ENDPOINT
,
128 .bEndpointAddress
= USB_DIR_IN
,
129 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
130 .wMaxPacketSize
= cpu_to_le16(1023),
134 static struct usb_endpoint_descriptor fs_iso_sink_desc
= {
135 .bLength
= USB_DT_ENDPOINT_SIZE
,
136 .bDescriptorType
= USB_DT_ENDPOINT
,
138 .bEndpointAddress
= USB_DIR_OUT
,
139 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
140 .wMaxPacketSize
= cpu_to_le16(1023),
144 static struct usb_descriptor_header
*fs_source_sink_descs
[] = {
145 (struct usb_descriptor_header
*) &source_sink_intf_alt0
,
146 (struct usb_descriptor_header
*) &fs_sink_desc
,
147 (struct usb_descriptor_header
*) &fs_source_desc
,
148 (struct usb_descriptor_header
*) &source_sink_intf_alt1
,
149 #define FS_ALT_IFC_1_OFFSET 3
150 (struct usb_descriptor_header
*) &fs_sink_desc
,
151 (struct usb_descriptor_header
*) &fs_source_desc
,
152 (struct usb_descriptor_header
*) &fs_iso_sink_desc
,
153 (struct usb_descriptor_header
*) &fs_iso_source_desc
,
157 /* high speed support: */
159 static struct usb_endpoint_descriptor hs_source_desc
= {
160 .bLength
= USB_DT_ENDPOINT_SIZE
,
161 .bDescriptorType
= USB_DT_ENDPOINT
,
163 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
164 .wMaxPacketSize
= cpu_to_le16(512),
167 static struct usb_endpoint_descriptor hs_sink_desc
= {
168 .bLength
= USB_DT_ENDPOINT_SIZE
,
169 .bDescriptorType
= USB_DT_ENDPOINT
,
171 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
172 .wMaxPacketSize
= cpu_to_le16(512),
175 static struct usb_endpoint_descriptor hs_iso_source_desc
= {
176 .bLength
= USB_DT_ENDPOINT_SIZE
,
177 .bDescriptorType
= USB_DT_ENDPOINT
,
179 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
180 .wMaxPacketSize
= cpu_to_le16(1024),
184 static struct usb_endpoint_descriptor hs_iso_sink_desc
= {
185 .bLength
= USB_DT_ENDPOINT_SIZE
,
186 .bDescriptorType
= USB_DT_ENDPOINT
,
188 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
189 .wMaxPacketSize
= cpu_to_le16(1024),
193 static struct usb_descriptor_header
*hs_source_sink_descs
[] = {
194 (struct usb_descriptor_header
*) &source_sink_intf_alt0
,
195 (struct usb_descriptor_header
*) &hs_source_desc
,
196 (struct usb_descriptor_header
*) &hs_sink_desc
,
197 (struct usb_descriptor_header
*) &source_sink_intf_alt1
,
198 #define HS_ALT_IFC_1_OFFSET 3
199 (struct usb_descriptor_header
*) &hs_source_desc
,
200 (struct usb_descriptor_header
*) &hs_sink_desc
,
201 (struct usb_descriptor_header
*) &hs_iso_source_desc
,
202 (struct usb_descriptor_header
*) &hs_iso_sink_desc
,
206 /* super speed support: */
208 static struct usb_endpoint_descriptor ss_source_desc
= {
209 .bLength
= USB_DT_ENDPOINT_SIZE
,
210 .bDescriptorType
= USB_DT_ENDPOINT
,
212 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
213 .wMaxPacketSize
= cpu_to_le16(1024),
216 struct usb_ss_ep_comp_descriptor ss_source_comp_desc
= {
217 .bLength
= USB_DT_SS_EP_COMP_SIZE
,
218 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
222 .wBytesPerInterval
= 0,
225 static struct usb_endpoint_descriptor ss_sink_desc
= {
226 .bLength
= USB_DT_ENDPOINT_SIZE
,
227 .bDescriptorType
= USB_DT_ENDPOINT
,
229 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
230 .wMaxPacketSize
= cpu_to_le16(1024),
233 struct usb_ss_ep_comp_descriptor ss_sink_comp_desc
= {
234 .bLength
= USB_DT_SS_EP_COMP_SIZE
,
235 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
239 .wBytesPerInterval
= 0,
242 static struct usb_endpoint_descriptor ss_iso_source_desc
= {
243 .bLength
= USB_DT_ENDPOINT_SIZE
,
244 .bDescriptorType
= USB_DT_ENDPOINT
,
246 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
247 .wMaxPacketSize
= cpu_to_le16(1024),
251 struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc
= {
252 .bLength
= USB_DT_SS_EP_COMP_SIZE
,
253 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
257 .wBytesPerInterval
= cpu_to_le16(1024),
260 static struct usb_endpoint_descriptor ss_iso_sink_desc
= {
261 .bLength
= USB_DT_ENDPOINT_SIZE
,
262 .bDescriptorType
= USB_DT_ENDPOINT
,
264 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
265 .wMaxPacketSize
= cpu_to_le16(1024),
269 struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc
= {
270 .bLength
= USB_DT_SS_EP_COMP_SIZE
,
271 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
275 .wBytesPerInterval
= cpu_to_le16(1024),
278 static struct usb_descriptor_header
*ss_source_sink_descs
[] = {
279 (struct usb_descriptor_header
*) &source_sink_intf_alt0
,
280 (struct usb_descriptor_header
*) &ss_source_desc
,
281 (struct usb_descriptor_header
*) &ss_source_comp_desc
,
282 (struct usb_descriptor_header
*) &ss_sink_desc
,
283 (struct usb_descriptor_header
*) &ss_sink_comp_desc
,
284 (struct usb_descriptor_header
*) &source_sink_intf_alt1
,
285 #define SS_ALT_IFC_1_OFFSET 5
286 (struct usb_descriptor_header
*) &ss_source_desc
,
287 (struct usb_descriptor_header
*) &ss_source_comp_desc
,
288 (struct usb_descriptor_header
*) &ss_sink_desc
,
289 (struct usb_descriptor_header
*) &ss_sink_comp_desc
,
290 (struct usb_descriptor_header
*) &ss_iso_source_desc
,
291 (struct usb_descriptor_header
*) &ss_iso_source_comp_desc
,
292 (struct usb_descriptor_header
*) &ss_iso_sink_desc
,
293 (struct usb_descriptor_header
*) &ss_iso_sink_comp_desc
,
297 /* function-specific strings: */
299 static struct usb_string strings_sourcesink
[] = {
300 [0].s
= "source and sink data",
301 { } /* end of list */
304 static struct usb_gadget_strings stringtab_sourcesink
= {
305 .language
= 0x0409, /* en-us */
306 .strings
= strings_sourcesink
,
309 static struct usb_gadget_strings
*sourcesink_strings
[] = {
310 &stringtab_sourcesink
,
314 /*-------------------------------------------------------------------------*/
317 sourcesink_bind(struct usb_configuration
*c
, struct usb_function
*f
)
319 struct usb_composite_dev
*cdev
= c
->cdev
;
320 struct f_sourcesink
*ss
= func_to_ss(f
);
323 /* allocate interface ID(s) */
324 id
= usb_interface_id(c
, f
);
327 source_sink_intf_alt0
.bInterfaceNumber
= id
;
328 source_sink_intf_alt1
.bInterfaceNumber
= id
;
330 /* allocate bulk endpoints */
331 ss
->in_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_source_desc
);
334 ERROR(cdev
, "%s: can't autoconfigure on %s\n",
335 f
->name
, cdev
->gadget
->name
);
338 ss
->in_ep
->driver_data
= cdev
; /* claim */
340 ss
->out_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_sink_desc
);
343 ss
->out_ep
->driver_data
= cdev
; /* claim */
345 /* sanity check the isoc module parameters */
346 if (isoc_interval
< 1)
348 if (isoc_interval
> 16)
352 if (isoc_maxburst
> 15)
355 /* fill in the FS isoc descriptors from the module parameters */
356 fs_iso_source_desc
.wMaxPacketSize
= isoc_maxpacket
> 1023 ?
357 1023 : isoc_maxpacket
;
358 fs_iso_source_desc
.bInterval
= isoc_interval
;
359 fs_iso_sink_desc
.wMaxPacketSize
= isoc_maxpacket
> 1023 ?
360 1023 : isoc_maxpacket
;
361 fs_iso_sink_desc
.bInterval
= isoc_interval
;
363 /* allocate iso endpoints */
364 ss
->iso_in_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_iso_source_desc
);
367 ss
->iso_in_ep
->driver_data
= cdev
; /* claim */
369 ss
->iso_out_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_iso_sink_desc
);
370 if (ss
->iso_out_ep
) {
371 ss
->iso_out_ep
->driver_data
= cdev
; /* claim */
373 ss
->iso_in_ep
->driver_data
= NULL
;
374 ss
->iso_in_ep
= NULL
;
377 * We still want to work even if the UDC doesn't have isoc
378 * endpoints, so null out the alt interface that contains
381 fs_source_sink_descs
[FS_ALT_IFC_1_OFFSET
] = NULL
;
382 hs_source_sink_descs
[HS_ALT_IFC_1_OFFSET
] = NULL
;
383 ss_source_sink_descs
[SS_ALT_IFC_1_OFFSET
] = NULL
;
386 if (isoc_maxpacket
> 1024)
387 isoc_maxpacket
= 1024;
389 /* support high speed hardware */
390 if (gadget_is_dualspeed(c
->cdev
->gadget
)) {
391 hs_source_desc
.bEndpointAddress
=
392 fs_source_desc
.bEndpointAddress
;
393 hs_sink_desc
.bEndpointAddress
=
394 fs_sink_desc
.bEndpointAddress
;
397 * Fill in the HS isoc descriptors from the module parameters.
398 * We assume that the user knows what they are doing and won't
399 * give parameters that their UDC doesn't support.
401 hs_iso_source_desc
.wMaxPacketSize
= isoc_maxpacket
;
402 hs_iso_source_desc
.wMaxPacketSize
|= isoc_mult
<< 11;
403 hs_iso_source_desc
.bInterval
= isoc_interval
;
404 hs_iso_source_desc
.bEndpointAddress
=
405 fs_iso_source_desc
.bEndpointAddress
;
407 hs_iso_sink_desc
.wMaxPacketSize
= isoc_maxpacket
;
408 hs_iso_sink_desc
.wMaxPacketSize
|= isoc_mult
<< 11;
409 hs_iso_sink_desc
.bInterval
= isoc_interval
;
410 hs_iso_sink_desc
.bEndpointAddress
=
411 fs_iso_sink_desc
.bEndpointAddress
;
413 f
->hs_descriptors
= hs_source_sink_descs
;
416 /* support super speed hardware */
417 if (gadget_is_superspeed(c
->cdev
->gadget
)) {
418 ss_source_desc
.bEndpointAddress
=
419 fs_source_desc
.bEndpointAddress
;
420 ss_sink_desc
.bEndpointAddress
=
421 fs_sink_desc
.bEndpointAddress
;
424 * Fill in the SS isoc descriptors from the module parameters.
425 * We assume that the user knows what they are doing and won't
426 * give parameters that their UDC doesn't support.
428 ss_iso_source_desc
.wMaxPacketSize
= isoc_maxpacket
;
429 ss_iso_source_desc
.bInterval
= isoc_interval
;
430 ss_iso_source_comp_desc
.bmAttributes
= isoc_mult
;
431 ss_iso_source_comp_desc
.bMaxBurst
= isoc_maxburst
;
432 ss_iso_source_comp_desc
.wBytesPerInterval
=
433 isoc_maxpacket
* (isoc_mult
+ 1) * (isoc_maxburst
+ 1);
434 ss_iso_source_desc
.bEndpointAddress
=
435 fs_iso_source_desc
.bEndpointAddress
;
437 ss_iso_sink_desc
.wMaxPacketSize
= isoc_maxpacket
;
438 ss_iso_sink_desc
.bInterval
= isoc_interval
;
439 ss_iso_sink_comp_desc
.bmAttributes
= isoc_mult
;
440 ss_iso_sink_comp_desc
.bMaxBurst
= isoc_maxburst
;
441 ss_iso_sink_comp_desc
.wBytesPerInterval
=
442 isoc_maxpacket
* (isoc_mult
+ 1) * (isoc_maxburst
+ 1);
443 ss_iso_sink_desc
.bEndpointAddress
=
444 fs_iso_sink_desc
.bEndpointAddress
;
446 f
->ss_descriptors
= ss_source_sink_descs
;
449 DBG(cdev
, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
450 (gadget_is_superspeed(c
->cdev
->gadget
) ? "super" :
451 (gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full")),
452 f
->name
, ss
->in_ep
->name
, ss
->out_ep
->name
,
453 ss
->iso_in_ep
? ss
->iso_in_ep
->name
: "<none>",
454 ss
->iso_out_ep
? ss
->iso_out_ep
->name
: "<none>");
459 sourcesink_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
461 kfree(func_to_ss(f
));
464 /* optionally require specific source/sink data patterns */
465 static int check_read_data(struct f_sourcesink
*ss
, struct usb_request
*req
)
469 struct usb_composite_dev
*cdev
= ss
->function
.config
->cdev
;
474 for (i
= 0; i
< req
->actual
; i
++, buf
++) {
477 /* all-zeroes has no synchronization issues */
483 /* "mod63" stays in sync with short-terminated transfers,
484 * OR otherwise when host and gadget agree on how large
485 * each usb transfer request should be. Resync is done
486 * with set_interface or set_config. (We *WANT* it to
487 * get quickly out of sync if controllers or their drivers
488 * stutter for any reason, including buffer duplication...)
491 if (*buf
== (u8
)(i
% 63))
495 ERROR(cdev
, "bad OUT byte, buf[%d] = %d\n", i
, *buf
);
496 usb_ep_set_halt(ss
->out_ep
);
502 static void reinit_write_data(struct usb_ep
*ep
, struct usb_request
*req
)
509 memset(req
->buf
, 0, req
->length
);
512 for (i
= 0; i
< req
->length
; i
++)
513 *buf
++ = (u8
) (i
% 63);
520 static void source_sink_complete(struct usb_ep
*ep
, struct usb_request
*req
)
522 struct usb_composite_dev
*cdev
;
523 struct f_sourcesink
*ss
= ep
->driver_data
;
524 int status
= req
->status
;
526 /* driver_data will be null if ep has been disabled */
530 cdev
= ss
->function
.config
->cdev
;
534 case 0: /* normal completion? */
535 if (ep
== ss
->out_ep
) {
536 check_read_data(ss
, req
);
538 memset(req
->buf
, 0x55, req
->length
);
540 reinit_write_data(ep
, req
);
543 /* this endpoint is normally active while we're configured */
544 case -ECONNABORTED
: /* hardware forced ep reset */
545 case -ECONNRESET
: /* request dequeued */
546 case -ESHUTDOWN
: /* disconnect from host */
547 VDBG(cdev
, "%s gone (%d), %d/%d\n", ep
->name
, status
,
548 req
->actual
, req
->length
);
549 if (ep
== ss
->out_ep
)
550 check_read_data(ss
, req
);
551 free_ep_req(ep
, req
);
554 case -EOVERFLOW
: /* buffer overrun on read means that
555 * we didn't provide a big enough
560 DBG(cdev
, "%s complete --> %d, %d/%d\n", ep
->name
,
561 status
, req
->actual
, req
->length
);
563 case -EREMOTEIO
: /* short read */
567 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
569 ERROR(cdev
, "kill %s: resubmit %d bytes --> %d\n",
570 ep
->name
, req
->length
, status
);
572 /* FIXME recover later ... somehow */
576 static int source_sink_start_ep(struct f_sourcesink
*ss
, bool is_in
,
577 bool is_iso
, int speed
)
580 struct usb_request
*req
;
583 for (i
= 0; i
< 8; i
++) {
586 case USB_SPEED_SUPER
:
587 size
= isoc_maxpacket
* (isoc_mult
+ 1) *
591 size
= isoc_maxpacket
* (isoc_mult
+ 1);
594 size
= isoc_maxpacket
> 1023 ?
595 1023 : isoc_maxpacket
;
598 ep
= is_in
? ss
->iso_in_ep
: ss
->iso_out_ep
;
599 req
= alloc_ep_req(ep
, size
);
601 ep
= is_in
? ss
->in_ep
: ss
->out_ep
;
602 req
= alloc_ep_req(ep
, 0);
608 req
->complete
= source_sink_complete
;
610 reinit_write_data(ep
, req
);
611 else if (pattern
!= 2)
612 memset(req
->buf
, 0x55, req
->length
);
614 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
616 struct usb_composite_dev
*cdev
;
618 cdev
= ss
->function
.config
->cdev
;
619 ERROR(cdev
, "start %s%s %s --> %d\n",
620 is_iso
? "ISO-" : "", is_in
? "IN" : "OUT",
622 free_ep_req(ep
, req
);
632 static void disable_source_sink(struct f_sourcesink
*ss
)
634 struct usb_composite_dev
*cdev
;
636 cdev
= ss
->function
.config
->cdev
;
637 disable_endpoints(cdev
, ss
->in_ep
, ss
->out_ep
, ss
->iso_in_ep
,
639 VDBG(cdev
, "%s disabled\n", ss
->function
.name
);
643 enable_source_sink(struct usb_composite_dev
*cdev
, struct f_sourcesink
*ss
,
647 int speed
= cdev
->gadget
->speed
;
650 /* one bulk endpoint writes (sources) zeroes IN (to the host) */
652 result
= config_ep_by_speed(cdev
->gadget
, &(ss
->function
), ep
);
655 result
= usb_ep_enable(ep
);
658 ep
->driver_data
= ss
;
660 result
= source_sink_start_ep(ss
, true, false, speed
);
665 ep
->driver_data
= NULL
;
669 /* one bulk endpoint reads (sinks) anything OUT (from the host) */
671 result
= config_ep_by_speed(cdev
->gadget
, &(ss
->function
), ep
);
674 result
= usb_ep_enable(ep
);
677 ep
->driver_data
= ss
;
679 result
= source_sink_start_ep(ss
, false, false, speed
);
684 ep
->driver_data
= NULL
;
691 /* one iso endpoint writes (sources) zeroes IN (to the host) */
694 result
= config_ep_by_speed(cdev
->gadget
, &(ss
->function
), ep
);
697 result
= usb_ep_enable(ep
);
700 ep
->driver_data
= ss
;
702 result
= source_sink_start_ep(ss
, true, true, speed
);
708 ep
->driver_data
= NULL
;
714 /* one iso endpoint reads (sinks) anything OUT (from the host) */
717 result
= config_ep_by_speed(cdev
->gadget
, &(ss
->function
), ep
);
720 result
= usb_ep_enable(ep
);
723 ep
->driver_data
= ss
;
725 result
= source_sink_start_ep(ss
, false, true, speed
);
728 ep
->driver_data
= NULL
;
735 DBG(cdev
, "%s enabled, alt intf %d\n", ss
->function
.name
, alt
);
739 static int sourcesink_set_alt(struct usb_function
*f
,
740 unsigned intf
, unsigned alt
)
742 struct f_sourcesink
*ss
= func_to_ss(f
);
743 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
745 if (ss
->in_ep
->driver_data
)
746 disable_source_sink(ss
);
747 return enable_source_sink(cdev
, ss
, alt
);
750 static int sourcesink_get_alt(struct usb_function
*f
, unsigned intf
)
752 struct f_sourcesink
*ss
= func_to_ss(f
);
757 static void sourcesink_disable(struct usb_function
*f
)
759 struct f_sourcesink
*ss
= func_to_ss(f
);
761 disable_source_sink(ss
);
764 /*-------------------------------------------------------------------------*/
766 static int __init
sourcesink_bind_config(struct usb_configuration
*c
)
768 struct f_sourcesink
*ss
;
771 ss
= kzalloc(sizeof *ss
, GFP_KERNEL
);
775 ss
->function
.name
= "source/sink";
776 ss
->function
.descriptors
= fs_source_sink_descs
;
777 ss
->function
.bind
= sourcesink_bind
;
778 ss
->function
.unbind
= sourcesink_unbind
;
779 ss
->function
.set_alt
= sourcesink_set_alt
;
780 ss
->function
.get_alt
= sourcesink_get_alt
;
781 ss
->function
.disable
= sourcesink_disable
;
783 status
= usb_add_function(c
, &ss
->function
);
789 static int sourcesink_setup(struct usb_configuration
*c
,
790 const struct usb_ctrlrequest
*ctrl
)
792 struct usb_request
*req
= c
->cdev
->req
;
793 int value
= -EOPNOTSUPP
;
794 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
795 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
796 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
798 req
->length
= USB_BUFSIZ
;
800 /* composite driver infrastructure handles everything except
801 * the two control test requests.
803 switch (ctrl
->bRequest
) {
806 * These are the same vendor-specific requests supported by
807 * Intel's USB 2.0 compliance test devices. We exceed that
808 * device spec by allowing multiple-packet requests.
810 * NOTE: the Control-OUT data stays in req->buf ... better
811 * would be copying it into a scratch buffer, so that other
812 * requests may safely intervene.
814 case 0x5b: /* control WRITE test -- fill the buffer */
815 if (ctrl
->bRequestType
!= (USB_DIR_OUT
|USB_TYPE_VENDOR
))
817 if (w_value
|| w_index
)
819 /* just read that many bytes into the buffer */
820 if (w_length
> req
->length
)
824 case 0x5c: /* control READ test -- return the buffer */
825 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_TYPE_VENDOR
))
827 if (w_value
|| w_index
)
829 /* expect those bytes are still in the buffer; send back */
830 if (w_length
> req
->length
)
838 "unknown control req%02x.%02x v%04x i%04x l%d\n",
839 ctrl
->bRequestType
, ctrl
->bRequest
,
840 w_value
, w_index
, w_length
);
843 /* respond with data transfer or status phase? */
845 VDBG(c
->cdev
, "source/sink req%02x.%02x v%04x i%04x l%d\n",
846 ctrl
->bRequestType
, ctrl
->bRequest
,
847 w_value
, w_index
, w_length
);
850 value
= usb_ep_queue(c
->cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
852 ERROR(c
->cdev
, "source/sink response, err %d\n",
856 /* device either stalls (value < 0) or reports success */
860 static struct usb_configuration sourcesink_driver
= {
861 .label
= "source/sink",
862 .strings
= sourcesink_strings
,
863 .setup
= sourcesink_setup
,
864 .bConfigurationValue
= 3,
865 .bmAttributes
= USB_CONFIG_ATT_SELFPOWER
,
866 /* .iConfiguration = DYNAMIC */
870 * sourcesink_add - add a source/sink testing configuration to a device
871 * @cdev: the device to support the configuration
873 int __init
sourcesink_add(struct usb_composite_dev
*cdev
, bool autoresume
)
877 /* allocate string ID(s) */
878 id
= usb_string_id(cdev
);
881 strings_sourcesink
[0].id
= id
;
883 source_sink_intf_alt0
.iInterface
= id
;
884 source_sink_intf_alt1
.iInterface
= id
;
885 sourcesink_driver
.iConfiguration
= id
;
887 /* support autoresume for remote wakeup testing */
889 sourcesink_driver
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
891 /* support OTG systems */
892 if (gadget_is_otg(cdev
->gadget
)) {
893 sourcesink_driver
.descriptors
= otg_desc
;
894 sourcesink_driver
.bmAttributes
|= USB_CONFIG_ATT_WAKEUP
;
897 return usb_add_config(cdev
, &sourcesink_driver
, sourcesink_bind_config
);