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>
19 #include <linux/usb/composite.h>
20 #include <linux/err.h>
26 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
29 * This just sinks bulk packets OUT to the peripheral and sources them IN
30 * to the host, optionally with specific data patterns for integrity tests.
31 * As such it supports basic functionality and load tests.
33 * In terms of control messaging, this supports all the standard requests
34 * plus two that support control-OUT tests. If the optional "autoresume"
35 * mode is enabled, it provides good functional coverage for the "USBCV"
36 * test harness from USB-IF.
38 * Note that because this doesn't queue more than one request at a time,
39 * some other function must be used to test queueing logic. The network
40 * link (g_ether) is the best overall option for that, since its TX and RX
41 * queues are relatively independent, will receive a range of packet sizes,
42 * and can often be made to run out completely. Those issues are important
43 * when stress testing peripheral controller drivers.
46 struct usb_function function
;
49 struct usb_ep
*out_ep
;
50 struct usb_ep
*iso_in_ep
;
51 struct usb_ep
*iso_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 static unsigned isoc_interval
;
62 static unsigned isoc_maxpacket
;
63 static unsigned isoc_mult
;
64 static unsigned isoc_maxburst
;
65 static unsigned buflen
;
67 /*-------------------------------------------------------------------------*/
69 static struct usb_interface_descriptor source_sink_intf_alt0
= {
70 .bLength
= USB_DT_INTERFACE_SIZE
,
71 .bDescriptorType
= USB_DT_INTERFACE
,
73 .bAlternateSetting
= 0,
75 .bInterfaceClass
= USB_CLASS_VENDOR_SPEC
,
76 /* .iInterface = DYNAMIC */
79 static struct usb_interface_descriptor source_sink_intf_alt1
= {
80 .bLength
= USB_DT_INTERFACE_SIZE
,
81 .bDescriptorType
= USB_DT_INTERFACE
,
83 .bAlternateSetting
= 1,
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_endpoint_descriptor fs_iso_source_desc
= {
108 .bLength
= USB_DT_ENDPOINT_SIZE
,
109 .bDescriptorType
= USB_DT_ENDPOINT
,
111 .bEndpointAddress
= USB_DIR_IN
,
112 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
113 .wMaxPacketSize
= cpu_to_le16(1023),
117 static struct usb_endpoint_descriptor fs_iso_sink_desc
= {
118 .bLength
= USB_DT_ENDPOINT_SIZE
,
119 .bDescriptorType
= USB_DT_ENDPOINT
,
121 .bEndpointAddress
= USB_DIR_OUT
,
122 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
123 .wMaxPacketSize
= cpu_to_le16(1023),
127 static struct usb_descriptor_header
*fs_source_sink_descs
[] = {
128 (struct usb_descriptor_header
*) &source_sink_intf_alt0
,
129 (struct usb_descriptor_header
*) &fs_sink_desc
,
130 (struct usb_descriptor_header
*) &fs_source_desc
,
131 (struct usb_descriptor_header
*) &source_sink_intf_alt1
,
132 #define FS_ALT_IFC_1_OFFSET 3
133 (struct usb_descriptor_header
*) &fs_sink_desc
,
134 (struct usb_descriptor_header
*) &fs_source_desc
,
135 (struct usb_descriptor_header
*) &fs_iso_sink_desc
,
136 (struct usb_descriptor_header
*) &fs_iso_source_desc
,
140 /* high speed support: */
142 static struct usb_endpoint_descriptor hs_source_desc
= {
143 .bLength
= USB_DT_ENDPOINT_SIZE
,
144 .bDescriptorType
= USB_DT_ENDPOINT
,
146 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
147 .wMaxPacketSize
= cpu_to_le16(512),
150 static struct usb_endpoint_descriptor hs_sink_desc
= {
151 .bLength
= USB_DT_ENDPOINT_SIZE
,
152 .bDescriptorType
= USB_DT_ENDPOINT
,
154 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
155 .wMaxPacketSize
= cpu_to_le16(512),
158 static struct usb_endpoint_descriptor hs_iso_source_desc
= {
159 .bLength
= USB_DT_ENDPOINT_SIZE
,
160 .bDescriptorType
= USB_DT_ENDPOINT
,
162 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
163 .wMaxPacketSize
= cpu_to_le16(1024),
167 static struct usb_endpoint_descriptor hs_iso_sink_desc
= {
168 .bLength
= USB_DT_ENDPOINT_SIZE
,
169 .bDescriptorType
= USB_DT_ENDPOINT
,
171 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
172 .wMaxPacketSize
= cpu_to_le16(1024),
176 static struct usb_descriptor_header
*hs_source_sink_descs
[] = {
177 (struct usb_descriptor_header
*) &source_sink_intf_alt0
,
178 (struct usb_descriptor_header
*) &hs_source_desc
,
179 (struct usb_descriptor_header
*) &hs_sink_desc
,
180 (struct usb_descriptor_header
*) &source_sink_intf_alt1
,
181 #define HS_ALT_IFC_1_OFFSET 3
182 (struct usb_descriptor_header
*) &hs_source_desc
,
183 (struct usb_descriptor_header
*) &hs_sink_desc
,
184 (struct usb_descriptor_header
*) &hs_iso_source_desc
,
185 (struct usb_descriptor_header
*) &hs_iso_sink_desc
,
189 /* super speed support: */
191 static struct usb_endpoint_descriptor ss_source_desc
= {
192 .bLength
= USB_DT_ENDPOINT_SIZE
,
193 .bDescriptorType
= USB_DT_ENDPOINT
,
195 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
196 .wMaxPacketSize
= cpu_to_le16(1024),
199 static struct usb_ss_ep_comp_descriptor ss_source_comp_desc
= {
200 .bLength
= USB_DT_SS_EP_COMP_SIZE
,
201 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
205 .wBytesPerInterval
= 0,
208 static struct usb_endpoint_descriptor ss_sink_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 static struct usb_ss_ep_comp_descriptor ss_sink_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_iso_source_desc
= {
226 .bLength
= USB_DT_ENDPOINT_SIZE
,
227 .bDescriptorType
= USB_DT_ENDPOINT
,
229 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
230 .wMaxPacketSize
= cpu_to_le16(1024),
234 static struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc
= {
235 .bLength
= USB_DT_SS_EP_COMP_SIZE
,
236 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
240 .wBytesPerInterval
= cpu_to_le16(1024),
243 static struct usb_endpoint_descriptor ss_iso_sink_desc
= {
244 .bLength
= USB_DT_ENDPOINT_SIZE
,
245 .bDescriptorType
= USB_DT_ENDPOINT
,
247 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
248 .wMaxPacketSize
= cpu_to_le16(1024),
252 static struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc
= {
253 .bLength
= USB_DT_SS_EP_COMP_SIZE
,
254 .bDescriptorType
= USB_DT_SS_ENDPOINT_COMP
,
258 .wBytesPerInterval
= cpu_to_le16(1024),
261 static struct usb_descriptor_header
*ss_source_sink_descs
[] = {
262 (struct usb_descriptor_header
*) &source_sink_intf_alt0
,
263 (struct usb_descriptor_header
*) &ss_source_desc
,
264 (struct usb_descriptor_header
*) &ss_source_comp_desc
,
265 (struct usb_descriptor_header
*) &ss_sink_desc
,
266 (struct usb_descriptor_header
*) &ss_sink_comp_desc
,
267 (struct usb_descriptor_header
*) &source_sink_intf_alt1
,
268 #define SS_ALT_IFC_1_OFFSET 5
269 (struct usb_descriptor_header
*) &ss_source_desc
,
270 (struct usb_descriptor_header
*) &ss_source_comp_desc
,
271 (struct usb_descriptor_header
*) &ss_sink_desc
,
272 (struct usb_descriptor_header
*) &ss_sink_comp_desc
,
273 (struct usb_descriptor_header
*) &ss_iso_source_desc
,
274 (struct usb_descriptor_header
*) &ss_iso_source_comp_desc
,
275 (struct usb_descriptor_header
*) &ss_iso_sink_desc
,
276 (struct usb_descriptor_header
*) &ss_iso_sink_comp_desc
,
280 /* function-specific strings: */
282 static struct usb_string strings_sourcesink
[] = {
283 [0].s
= "source and sink data",
284 { } /* end of list */
287 static struct usb_gadget_strings stringtab_sourcesink
= {
288 .language
= 0x0409, /* en-us */
289 .strings
= strings_sourcesink
,
292 static struct usb_gadget_strings
*sourcesink_strings
[] = {
293 &stringtab_sourcesink
,
297 /*-------------------------------------------------------------------------*/
299 static inline struct usb_request
*ss_alloc_ep_req(struct usb_ep
*ep
, int len
)
301 return alloc_ep_req(ep
, len
, buflen
);
304 void free_ep_req(struct usb_ep
*ep
, struct usb_request
*req
)
307 usb_ep_free_request(ep
, req
);
310 static void disable_ep(struct usb_composite_dev
*cdev
, struct usb_ep
*ep
)
314 if (ep
->driver_data
) {
315 value
= usb_ep_disable(ep
);
317 DBG(cdev
, "disable %s --> %d\n",
319 ep
->driver_data
= NULL
;
323 void disable_endpoints(struct usb_composite_dev
*cdev
,
324 struct usb_ep
*in
, struct usb_ep
*out
,
325 struct usb_ep
*iso_in
, struct usb_ep
*iso_out
)
327 disable_ep(cdev
, in
);
328 disable_ep(cdev
, out
);
330 disable_ep(cdev
, iso_in
);
332 disable_ep(cdev
, iso_out
);
336 sourcesink_bind(struct usb_configuration
*c
, struct usb_function
*f
)
338 struct usb_composite_dev
*cdev
= c
->cdev
;
339 struct f_sourcesink
*ss
= func_to_ss(f
);
343 /* allocate interface ID(s) */
344 id
= usb_interface_id(c
, f
);
347 source_sink_intf_alt0
.bInterfaceNumber
= id
;
348 source_sink_intf_alt1
.bInterfaceNumber
= id
;
350 /* allocate bulk endpoints */
351 ss
->in_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_source_desc
);
354 ERROR(cdev
, "%s: can't autoconfigure on %s\n",
355 f
->name
, cdev
->gadget
->name
);
358 ss
->in_ep
->driver_data
= cdev
; /* claim */
360 ss
->out_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_sink_desc
);
363 ss
->out_ep
->driver_data
= cdev
; /* claim */
365 /* sanity check the isoc module parameters */
366 if (isoc_interval
< 1)
368 if (isoc_interval
> 16)
372 if (isoc_maxburst
> 15)
375 /* fill in the FS isoc descriptors from the module parameters */
376 fs_iso_source_desc
.wMaxPacketSize
= isoc_maxpacket
> 1023 ?
377 1023 : isoc_maxpacket
;
378 fs_iso_source_desc
.bInterval
= isoc_interval
;
379 fs_iso_sink_desc
.wMaxPacketSize
= isoc_maxpacket
> 1023 ?
380 1023 : isoc_maxpacket
;
381 fs_iso_sink_desc
.bInterval
= isoc_interval
;
383 /* allocate iso endpoints */
384 ss
->iso_in_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_iso_source_desc
);
387 ss
->iso_in_ep
->driver_data
= cdev
; /* claim */
389 ss
->iso_out_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_iso_sink_desc
);
390 if (ss
->iso_out_ep
) {
391 ss
->iso_out_ep
->driver_data
= cdev
; /* claim */
393 ss
->iso_in_ep
->driver_data
= NULL
;
394 ss
->iso_in_ep
= NULL
;
397 * We still want to work even if the UDC doesn't have isoc
398 * endpoints, so null out the alt interface that contains
401 fs_source_sink_descs
[FS_ALT_IFC_1_OFFSET
] = NULL
;
402 hs_source_sink_descs
[HS_ALT_IFC_1_OFFSET
] = NULL
;
403 ss_source_sink_descs
[SS_ALT_IFC_1_OFFSET
] = NULL
;
406 if (isoc_maxpacket
> 1024)
407 isoc_maxpacket
= 1024;
409 /* support high speed hardware */
410 hs_source_desc
.bEndpointAddress
= fs_source_desc
.bEndpointAddress
;
411 hs_sink_desc
.bEndpointAddress
= fs_sink_desc
.bEndpointAddress
;
414 * Fill in the HS isoc descriptors from the module parameters.
415 * We assume that the user knows what they are doing and won't
416 * give parameters that their UDC doesn't support.
418 hs_iso_source_desc
.wMaxPacketSize
= isoc_maxpacket
;
419 hs_iso_source_desc
.wMaxPacketSize
|= isoc_mult
<< 11;
420 hs_iso_source_desc
.bInterval
= isoc_interval
;
421 hs_iso_source_desc
.bEndpointAddress
=
422 fs_iso_source_desc
.bEndpointAddress
;
424 hs_iso_sink_desc
.wMaxPacketSize
= isoc_maxpacket
;
425 hs_iso_sink_desc
.wMaxPacketSize
|= isoc_mult
<< 11;
426 hs_iso_sink_desc
.bInterval
= isoc_interval
;
427 hs_iso_sink_desc
.bEndpointAddress
= fs_iso_sink_desc
.bEndpointAddress
;
429 /* support super speed hardware */
430 ss_source_desc
.bEndpointAddress
=
431 fs_source_desc
.bEndpointAddress
;
432 ss_sink_desc
.bEndpointAddress
=
433 fs_sink_desc
.bEndpointAddress
;
436 * Fill in the SS isoc descriptors from the module parameters.
437 * We assume that the user knows what they are doing and won't
438 * give parameters that their UDC doesn't support.
440 ss_iso_source_desc
.wMaxPacketSize
= isoc_maxpacket
;
441 ss_iso_source_desc
.bInterval
= isoc_interval
;
442 ss_iso_source_comp_desc
.bmAttributes
= isoc_mult
;
443 ss_iso_source_comp_desc
.bMaxBurst
= isoc_maxburst
;
444 ss_iso_source_comp_desc
.wBytesPerInterval
=
445 isoc_maxpacket
* (isoc_mult
+ 1) * (isoc_maxburst
+ 1);
446 ss_iso_source_desc
.bEndpointAddress
=
447 fs_iso_source_desc
.bEndpointAddress
;
449 ss_iso_sink_desc
.wMaxPacketSize
= isoc_maxpacket
;
450 ss_iso_sink_desc
.bInterval
= isoc_interval
;
451 ss_iso_sink_comp_desc
.bmAttributes
= isoc_mult
;
452 ss_iso_sink_comp_desc
.bMaxBurst
= isoc_maxburst
;
453 ss_iso_sink_comp_desc
.wBytesPerInterval
=
454 isoc_maxpacket
* (isoc_mult
+ 1) * (isoc_maxburst
+ 1);
455 ss_iso_sink_desc
.bEndpointAddress
= fs_iso_sink_desc
.bEndpointAddress
;
457 ret
= usb_assign_descriptors(f
, fs_source_sink_descs
,
458 hs_source_sink_descs
, ss_source_sink_descs
);
462 DBG(cdev
, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
463 (gadget_is_superspeed(c
->cdev
->gadget
) ? "super" :
464 (gadget_is_dualspeed(c
->cdev
->gadget
) ? "dual" : "full")),
465 f
->name
, ss
->in_ep
->name
, ss
->out_ep
->name
,
466 ss
->iso_in_ep
? ss
->iso_in_ep
->name
: "<none>",
467 ss
->iso_out_ep
? ss
->iso_out_ep
->name
: "<none>");
472 sourcesink_free_func(struct usb_function
*f
)
474 struct f_ss_opts
*opts
;
476 opts
= container_of(f
->fi
, struct f_ss_opts
, func_inst
);
478 mutex_lock(&opts
->lock
);
480 mutex_unlock(&opts
->lock
);
482 usb_free_all_descriptors(f
);
483 kfree(func_to_ss(f
));
486 /* optionally require specific source/sink data patterns */
487 static int check_read_data(struct f_sourcesink
*ss
, struct usb_request
*req
)
491 struct usb_composite_dev
*cdev
= ss
->function
.config
->cdev
;
496 for (i
= 0; i
< req
->actual
; i
++, buf
++) {
499 /* all-zeroes has no synchronization issues */
505 /* "mod63" stays in sync with short-terminated transfers,
506 * OR otherwise when host and gadget agree on how large
507 * each usb transfer request should be. Resync is done
508 * with set_interface or set_config. (We *WANT* it to
509 * get quickly out of sync if controllers or their drivers
510 * stutter for any reason, including buffer duplication...)
513 if (*buf
== (u8
)(i
% 63))
517 ERROR(cdev
, "bad OUT byte, buf[%d] = %d\n", i
, *buf
);
518 usb_ep_set_halt(ss
->out_ep
);
524 static void reinit_write_data(struct usb_ep
*ep
, struct usb_request
*req
)
531 memset(req
->buf
, 0, req
->length
);
534 for (i
= 0; i
< req
->length
; i
++)
535 *buf
++ = (u8
) (i
% 63);
542 static void source_sink_complete(struct usb_ep
*ep
, struct usb_request
*req
)
544 struct usb_composite_dev
*cdev
;
545 struct f_sourcesink
*ss
= ep
->driver_data
;
546 int status
= req
->status
;
548 /* driver_data will be null if ep has been disabled */
552 cdev
= ss
->function
.config
->cdev
;
556 case 0: /* normal completion? */
557 if (ep
== ss
->out_ep
) {
558 check_read_data(ss
, req
);
560 memset(req
->buf
, 0x55, req
->length
);
564 /* this endpoint is normally active while we're configured */
565 case -ECONNABORTED
: /* hardware forced ep reset */
566 case -ECONNRESET
: /* request dequeued */
567 case -ESHUTDOWN
: /* disconnect from host */
568 VDBG(cdev
, "%s gone (%d), %d/%d\n", ep
->name
, status
,
569 req
->actual
, req
->length
);
570 if (ep
== ss
->out_ep
)
571 check_read_data(ss
, req
);
572 free_ep_req(ep
, req
);
575 case -EOVERFLOW
: /* buffer overrun on read means that
576 * we didn't provide a big enough
581 DBG(cdev
, "%s complete --> %d, %d/%d\n", ep
->name
,
582 status
, req
->actual
, req
->length
);
584 case -EREMOTEIO
: /* short read */
588 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
590 ERROR(cdev
, "kill %s: resubmit %d bytes --> %d\n",
591 ep
->name
, req
->length
, status
);
593 /* FIXME recover later ... somehow */
597 static int source_sink_start_ep(struct f_sourcesink
*ss
, bool is_in
,
598 bool is_iso
, int speed
)
601 struct usb_request
*req
;
604 for (i
= 0; i
< 8; i
++) {
607 case USB_SPEED_SUPER
:
608 size
= isoc_maxpacket
* (isoc_mult
+ 1) *
612 size
= isoc_maxpacket
* (isoc_mult
+ 1);
615 size
= isoc_maxpacket
> 1023 ?
616 1023 : isoc_maxpacket
;
619 ep
= is_in
? ss
->iso_in_ep
: ss
->iso_out_ep
;
620 req
= ss_alloc_ep_req(ep
, size
);
622 ep
= is_in
? ss
->in_ep
: ss
->out_ep
;
623 req
= ss_alloc_ep_req(ep
, 0);
629 req
->complete
= source_sink_complete
;
631 reinit_write_data(ep
, req
);
632 else if (pattern
!= 2)
633 memset(req
->buf
, 0x55, req
->length
);
635 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
637 struct usb_composite_dev
*cdev
;
639 cdev
= ss
->function
.config
->cdev
;
640 ERROR(cdev
, "start %s%s %s --> %d\n",
641 is_iso
? "ISO-" : "", is_in
? "IN" : "OUT",
643 free_ep_req(ep
, req
);
653 static void disable_source_sink(struct f_sourcesink
*ss
)
655 struct usb_composite_dev
*cdev
;
657 cdev
= ss
->function
.config
->cdev
;
658 disable_endpoints(cdev
, ss
->in_ep
, ss
->out_ep
, ss
->iso_in_ep
,
660 VDBG(cdev
, "%s disabled\n", ss
->function
.name
);
664 enable_source_sink(struct usb_composite_dev
*cdev
, struct f_sourcesink
*ss
,
668 int speed
= cdev
->gadget
->speed
;
671 /* one bulk endpoint writes (sources) zeroes IN (to the host) */
673 result
= config_ep_by_speed(cdev
->gadget
, &(ss
->function
), ep
);
676 result
= usb_ep_enable(ep
);
679 ep
->driver_data
= ss
;
681 result
= source_sink_start_ep(ss
, true, false, speed
);
686 ep
->driver_data
= NULL
;
690 /* one bulk endpoint reads (sinks) anything OUT (from the host) */
692 result
= config_ep_by_speed(cdev
->gadget
, &(ss
->function
), ep
);
695 result
= usb_ep_enable(ep
);
698 ep
->driver_data
= ss
;
700 result
= source_sink_start_ep(ss
, false, false, speed
);
705 ep
->driver_data
= NULL
;
712 /* one iso endpoint writes (sources) zeroes IN (to the host) */
715 result
= config_ep_by_speed(cdev
->gadget
, &(ss
->function
), ep
);
718 result
= usb_ep_enable(ep
);
721 ep
->driver_data
= ss
;
723 result
= source_sink_start_ep(ss
, true, true, speed
);
729 ep
->driver_data
= NULL
;
735 /* one iso endpoint reads (sinks) anything OUT (from the host) */
738 result
= config_ep_by_speed(cdev
->gadget
, &(ss
->function
), ep
);
741 result
= usb_ep_enable(ep
);
744 ep
->driver_data
= ss
;
746 result
= source_sink_start_ep(ss
, false, true, speed
);
749 ep
->driver_data
= NULL
;
756 DBG(cdev
, "%s enabled, alt intf %d\n", ss
->function
.name
, alt
);
760 static int sourcesink_set_alt(struct usb_function
*f
,
761 unsigned intf
, unsigned alt
)
763 struct f_sourcesink
*ss
= func_to_ss(f
);
764 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
766 if (ss
->in_ep
->driver_data
)
767 disable_source_sink(ss
);
768 return enable_source_sink(cdev
, ss
, alt
);
771 static int sourcesink_get_alt(struct usb_function
*f
, unsigned intf
)
773 struct f_sourcesink
*ss
= func_to_ss(f
);
778 static void sourcesink_disable(struct usb_function
*f
)
780 struct f_sourcesink
*ss
= func_to_ss(f
);
782 disable_source_sink(ss
);
785 /*-------------------------------------------------------------------------*/
787 static int sourcesink_setup(struct usb_function
*f
,
788 const struct usb_ctrlrequest
*ctrl
)
790 struct usb_configuration
*c
= f
->config
;
791 struct usb_request
*req
= c
->cdev
->req
;
792 int value
= -EOPNOTSUPP
;
793 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
794 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
795 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
797 req
->length
= USB_COMP_EP0_BUFSIZ
;
799 /* composite driver infrastructure handles everything except
800 * the two control test requests.
802 switch (ctrl
->bRequest
) {
805 * These are the same vendor-specific requests supported by
806 * Intel's USB 2.0 compliance test devices. We exceed that
807 * device spec by allowing multiple-packet requests.
809 * NOTE: the Control-OUT data stays in req->buf ... better
810 * would be copying it into a scratch buffer, so that other
811 * requests may safely intervene.
813 case 0x5b: /* control WRITE test -- fill the buffer */
814 if (ctrl
->bRequestType
!= (USB_DIR_OUT
|USB_TYPE_VENDOR
))
816 if (w_value
|| w_index
)
818 /* just read that many bytes into the buffer */
819 if (w_length
> req
->length
)
823 case 0x5c: /* control READ test -- return the buffer */
824 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_TYPE_VENDOR
))
826 if (w_value
|| w_index
)
828 /* expect those bytes are still in the buffer; send back */
829 if (w_length
> req
->length
)
837 "unknown control req%02x.%02x v%04x i%04x l%d\n",
838 ctrl
->bRequestType
, ctrl
->bRequest
,
839 w_value
, w_index
, w_length
);
842 /* respond with data transfer or status phase? */
844 VDBG(c
->cdev
, "source/sink req%02x.%02x v%04x i%04x l%d\n",
845 ctrl
->bRequestType
, ctrl
->bRequest
,
846 w_value
, w_index
, w_length
);
849 value
= usb_ep_queue(c
->cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
851 ERROR(c
->cdev
, "source/sink response, err %d\n",
855 /* device either stalls (value < 0) or reports success */
859 static struct usb_function
*source_sink_alloc_func(
860 struct usb_function_instance
*fi
)
862 struct f_sourcesink
*ss
;
863 struct f_ss_opts
*ss_opts
;
865 ss
= kzalloc(sizeof(*ss
), GFP_KERNEL
);
869 ss_opts
= container_of(fi
, struct f_ss_opts
, func_inst
);
871 mutex_lock(&ss_opts
->lock
);
873 mutex_unlock(&ss_opts
->lock
);
875 pattern
= ss_opts
->pattern
;
876 isoc_interval
= ss_opts
->isoc_interval
;
877 isoc_maxpacket
= ss_opts
->isoc_maxpacket
;
878 isoc_mult
= ss_opts
->isoc_mult
;
879 isoc_maxburst
= ss_opts
->isoc_maxburst
;
880 buflen
= ss_opts
->bulk_buflen
;
882 ss
->function
.name
= "source/sink";
883 ss
->function
.bind
= sourcesink_bind
;
884 ss
->function
.set_alt
= sourcesink_set_alt
;
885 ss
->function
.get_alt
= sourcesink_get_alt
;
886 ss
->function
.disable
= sourcesink_disable
;
887 ss
->function
.setup
= sourcesink_setup
;
888 ss
->function
.strings
= sourcesink_strings
;
890 ss
->function
.free_func
= sourcesink_free_func
;
892 return &ss
->function
;
895 static inline struct f_ss_opts
*to_f_ss_opts(struct config_item
*item
)
897 return container_of(to_config_group(item
), struct f_ss_opts
,
901 CONFIGFS_ATTR_STRUCT(f_ss_opts
);
902 CONFIGFS_ATTR_OPS(f_ss_opts
);
904 static void ss_attr_release(struct config_item
*item
)
906 struct f_ss_opts
*ss_opts
= to_f_ss_opts(item
);
908 usb_put_function_instance(&ss_opts
->func_inst
);
911 static struct configfs_item_operations ss_item_ops
= {
912 .release
= ss_attr_release
,
913 .show_attribute
= f_ss_opts_attr_show
,
914 .store_attribute
= f_ss_opts_attr_store
,
917 static ssize_t
f_ss_opts_pattern_show(struct f_ss_opts
*opts
, char *page
)
921 mutex_lock(&opts
->lock
);
922 result
= sprintf(page
, "%u", opts
->pattern
);
923 mutex_unlock(&opts
->lock
);
928 static ssize_t
f_ss_opts_pattern_store(struct f_ss_opts
*opts
,
929 const char *page
, size_t len
)
934 mutex_lock(&opts
->lock
);
940 ret
= kstrtou8(page
, 0, &num
);
944 if (num
!= 0 && num
!= 1 && num
!= 2) {
952 mutex_unlock(&opts
->lock
);
956 static struct f_ss_opts_attribute f_ss_opts_pattern
=
957 __CONFIGFS_ATTR(pattern
, S_IRUGO
| S_IWUSR
,
958 f_ss_opts_pattern_show
,
959 f_ss_opts_pattern_store
);
961 static ssize_t
f_ss_opts_isoc_interval_show(struct f_ss_opts
*opts
, char *page
)
965 mutex_lock(&opts
->lock
);
966 result
= sprintf(page
, "%u", opts
->isoc_interval
);
967 mutex_unlock(&opts
->lock
);
972 static ssize_t
f_ss_opts_isoc_interval_store(struct f_ss_opts
*opts
,
973 const char *page
, size_t len
)
978 mutex_lock(&opts
->lock
);
984 ret
= kstrtou8(page
, 0, &num
);
993 opts
->isoc_interval
= num
;
996 mutex_unlock(&opts
->lock
);
1000 static struct f_ss_opts_attribute f_ss_opts_isoc_interval
=
1001 __CONFIGFS_ATTR(isoc_interval
, S_IRUGO
| S_IWUSR
,
1002 f_ss_opts_isoc_interval_show
,
1003 f_ss_opts_isoc_interval_store
);
1005 static ssize_t
f_ss_opts_isoc_maxpacket_show(struct f_ss_opts
*opts
, char *page
)
1009 mutex_lock(&opts
->lock
);
1010 result
= sprintf(page
, "%u", opts
->isoc_maxpacket
);
1011 mutex_unlock(&opts
->lock
);
1016 static ssize_t
f_ss_opts_isoc_maxpacket_store(struct f_ss_opts
*opts
,
1017 const char *page
, size_t len
)
1022 mutex_lock(&opts
->lock
);
1028 ret
= kstrtou16(page
, 0, &num
);
1037 opts
->isoc_maxpacket
= num
;
1040 mutex_unlock(&opts
->lock
);
1044 static struct f_ss_opts_attribute f_ss_opts_isoc_maxpacket
=
1045 __CONFIGFS_ATTR(isoc_maxpacket
, S_IRUGO
| S_IWUSR
,
1046 f_ss_opts_isoc_maxpacket_show
,
1047 f_ss_opts_isoc_maxpacket_store
);
1049 static ssize_t
f_ss_opts_isoc_mult_show(struct f_ss_opts
*opts
, char *page
)
1053 mutex_lock(&opts
->lock
);
1054 result
= sprintf(page
, "%u", opts
->isoc_mult
);
1055 mutex_unlock(&opts
->lock
);
1060 static ssize_t
f_ss_opts_isoc_mult_store(struct f_ss_opts
*opts
,
1061 const char *page
, size_t len
)
1066 mutex_lock(&opts
->lock
);
1072 ret
= kstrtou8(page
, 0, &num
);
1081 opts
->isoc_mult
= num
;
1084 mutex_unlock(&opts
->lock
);
1088 static struct f_ss_opts_attribute f_ss_opts_isoc_mult
=
1089 __CONFIGFS_ATTR(isoc_mult
, S_IRUGO
| S_IWUSR
,
1090 f_ss_opts_isoc_mult_show
,
1091 f_ss_opts_isoc_mult_store
);
1093 static ssize_t
f_ss_opts_isoc_maxburst_show(struct f_ss_opts
*opts
, char *page
)
1097 mutex_lock(&opts
->lock
);
1098 result
= sprintf(page
, "%u", opts
->isoc_maxburst
);
1099 mutex_unlock(&opts
->lock
);
1104 static ssize_t
f_ss_opts_isoc_maxburst_store(struct f_ss_opts
*opts
,
1105 const char *page
, size_t len
)
1110 mutex_lock(&opts
->lock
);
1116 ret
= kstrtou8(page
, 0, &num
);
1125 opts
->isoc_maxburst
= num
;
1128 mutex_unlock(&opts
->lock
);
1132 static struct f_ss_opts_attribute f_ss_opts_isoc_maxburst
=
1133 __CONFIGFS_ATTR(isoc_maxburst
, S_IRUGO
| S_IWUSR
,
1134 f_ss_opts_isoc_maxburst_show
,
1135 f_ss_opts_isoc_maxburst_store
);
1137 static ssize_t
f_ss_opts_bulk_buflen_show(struct f_ss_opts
*opts
, char *page
)
1141 mutex_lock(&opts
->lock
);
1142 result
= sprintf(page
, "%u", opts
->bulk_buflen
);
1143 mutex_unlock(&opts
->lock
);
1148 static ssize_t
f_ss_opts_bulk_buflen_store(struct f_ss_opts
*opts
,
1149 const char *page
, size_t len
)
1154 mutex_lock(&opts
->lock
);
1160 ret
= kstrtou32(page
, 0, &num
);
1164 opts
->bulk_buflen
= num
;
1167 mutex_unlock(&opts
->lock
);
1171 static struct f_ss_opts_attribute f_ss_opts_bulk_buflen
=
1172 __CONFIGFS_ATTR(buflen
, S_IRUGO
| S_IWUSR
,
1173 f_ss_opts_bulk_buflen_show
,
1174 f_ss_opts_bulk_buflen_store
);
1176 static struct configfs_attribute
*ss_attrs
[] = {
1177 &f_ss_opts_pattern
.attr
,
1178 &f_ss_opts_isoc_interval
.attr
,
1179 &f_ss_opts_isoc_maxpacket
.attr
,
1180 &f_ss_opts_isoc_mult
.attr
,
1181 &f_ss_opts_isoc_maxburst
.attr
,
1182 &f_ss_opts_bulk_buflen
.attr
,
1186 static struct config_item_type ss_func_type
= {
1187 .ct_item_ops
= &ss_item_ops
,
1188 .ct_attrs
= ss_attrs
,
1189 .ct_owner
= THIS_MODULE
,
1192 static void source_sink_free_instance(struct usb_function_instance
*fi
)
1194 struct f_ss_opts
*ss_opts
;
1196 ss_opts
= container_of(fi
, struct f_ss_opts
, func_inst
);
1200 static struct usb_function_instance
*source_sink_alloc_inst(void)
1202 struct f_ss_opts
*ss_opts
;
1204 ss_opts
= kzalloc(sizeof(*ss_opts
), GFP_KERNEL
);
1206 return ERR_PTR(-ENOMEM
);
1207 mutex_init(&ss_opts
->lock
);
1208 ss_opts
->func_inst
.free_func_inst
= source_sink_free_instance
;
1209 ss_opts
->isoc_interval
= GZERO_ISOC_INTERVAL
;
1210 ss_opts
->isoc_maxpacket
= GZERO_ISOC_MAXPACKET
;
1211 ss_opts
->bulk_buflen
= GZERO_BULK_BUFLEN
;
1213 config_group_init_type_name(&ss_opts
->func_inst
.group
, "",
1216 return &ss_opts
->func_inst
;
1218 DECLARE_USB_FUNCTION(SourceSink
, source_sink_alloc_inst
,
1219 source_sink_alloc_func
);
1221 static int __init
sslb_modinit(void)
1225 ret
= usb_function_register(&SourceSinkusb_func
);
1230 usb_function_unregister(&SourceSinkusb_func
);
1233 static void __exit
sslb_modexit(void)
1235 usb_function_unregister(&SourceSinkusb_func
);
1238 module_init(sslb_modinit
);
1239 module_exit(sslb_modexit
);
1241 MODULE_LICENSE("GPL");