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 unsigned isoc_interval
;
56 unsigned isoc_maxpacket
;
58 unsigned isoc_maxburst
;
62 static inline struct f_sourcesink
*func_to_ss(struct usb_function
*f
)
64 return container_of(f
, struct f_sourcesink
, function
);
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 struct f_sourcesink
*ss
= ep
->driver_data
;
303 return alloc_ep_req(ep
, len
, ss
->buflen
);
306 static void disable_ep(struct usb_composite_dev
*cdev
, struct usb_ep
*ep
)
310 value
= usb_ep_disable(ep
);
312 DBG(cdev
, "disable %s --> %d\n", ep
->name
, value
);
315 void disable_endpoints(struct usb_composite_dev
*cdev
,
316 struct usb_ep
*in
, struct usb_ep
*out
,
317 struct usb_ep
*iso_in
, struct usb_ep
*iso_out
)
319 disable_ep(cdev
, in
);
320 disable_ep(cdev
, out
);
322 disable_ep(cdev
, iso_in
);
324 disable_ep(cdev
, iso_out
);
328 sourcesink_bind(struct usb_configuration
*c
, struct usb_function
*f
)
330 struct usb_composite_dev
*cdev
= c
->cdev
;
331 struct f_sourcesink
*ss
= func_to_ss(f
);
335 /* allocate interface ID(s) */
336 id
= usb_interface_id(c
, f
);
339 source_sink_intf_alt0
.bInterfaceNumber
= id
;
340 source_sink_intf_alt1
.bInterfaceNumber
= id
;
342 /* allocate bulk endpoints */
343 ss
->in_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_source_desc
);
346 ERROR(cdev
, "%s: can't autoconfigure on %s\n",
347 f
->name
, cdev
->gadget
->name
);
351 ss
->out_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_sink_desc
);
355 /* sanity check the isoc module parameters */
356 if (ss
->isoc_interval
< 1)
357 ss
->isoc_interval
= 1;
358 if (ss
->isoc_interval
> 16)
359 ss
->isoc_interval
= 16;
360 if (ss
->isoc_mult
> 2)
362 if (ss
->isoc_maxburst
> 15)
363 ss
->isoc_maxburst
= 15;
365 /* fill in the FS isoc descriptors from the module parameters */
366 fs_iso_source_desc
.wMaxPacketSize
= ss
->isoc_maxpacket
> 1023 ?
367 1023 : ss
->isoc_maxpacket
;
368 fs_iso_source_desc
.bInterval
= ss
->isoc_interval
;
369 fs_iso_sink_desc
.wMaxPacketSize
= ss
->isoc_maxpacket
> 1023 ?
370 1023 : ss
->isoc_maxpacket
;
371 fs_iso_sink_desc
.bInterval
= ss
->isoc_interval
;
373 /* allocate iso endpoints */
374 ss
->iso_in_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_iso_source_desc
);
378 ss
->iso_out_ep
= usb_ep_autoconfig(cdev
->gadget
, &fs_iso_sink_desc
);
379 if (!ss
->iso_out_ep
) {
380 usb_ep_autoconfig_release(ss
->iso_in_ep
);
381 ss
->iso_in_ep
= NULL
;
384 * We still want to work even if the UDC doesn't have isoc
385 * endpoints, so null out the alt interface that contains
388 fs_source_sink_descs
[FS_ALT_IFC_1_OFFSET
] = NULL
;
389 hs_source_sink_descs
[HS_ALT_IFC_1_OFFSET
] = NULL
;
390 ss_source_sink_descs
[SS_ALT_IFC_1_OFFSET
] = NULL
;
393 if (ss
->isoc_maxpacket
> 1024)
394 ss
->isoc_maxpacket
= 1024;
396 /* support high speed hardware */
397 hs_source_desc
.bEndpointAddress
= fs_source_desc
.bEndpointAddress
;
398 hs_sink_desc
.bEndpointAddress
= fs_sink_desc
.bEndpointAddress
;
401 * Fill in the HS isoc descriptors from the module parameters.
402 * We assume that the user knows what they are doing and won't
403 * give parameters that their UDC doesn't support.
405 hs_iso_source_desc
.wMaxPacketSize
= ss
->isoc_maxpacket
;
406 hs_iso_source_desc
.wMaxPacketSize
|= ss
->isoc_mult
<< 11;
407 hs_iso_source_desc
.bInterval
= ss
->isoc_interval
;
408 hs_iso_source_desc
.bEndpointAddress
=
409 fs_iso_source_desc
.bEndpointAddress
;
411 hs_iso_sink_desc
.wMaxPacketSize
= ss
->isoc_maxpacket
;
412 hs_iso_sink_desc
.wMaxPacketSize
|= ss
->isoc_mult
<< 11;
413 hs_iso_sink_desc
.bInterval
= ss
->isoc_interval
;
414 hs_iso_sink_desc
.bEndpointAddress
= fs_iso_sink_desc
.bEndpointAddress
;
416 /* support super speed hardware */
417 ss_source_desc
.bEndpointAddress
=
418 fs_source_desc
.bEndpointAddress
;
419 ss_sink_desc
.bEndpointAddress
=
420 fs_sink_desc
.bEndpointAddress
;
423 * Fill in the SS isoc descriptors from the module parameters.
424 * We assume that the user knows what they are doing and won't
425 * give parameters that their UDC doesn't support.
427 ss_iso_source_desc
.wMaxPacketSize
= ss
->isoc_maxpacket
;
428 ss_iso_source_desc
.bInterval
= ss
->isoc_interval
;
429 ss_iso_source_comp_desc
.bmAttributes
= ss
->isoc_mult
;
430 ss_iso_source_comp_desc
.bMaxBurst
= ss
->isoc_maxburst
;
431 ss_iso_source_comp_desc
.wBytesPerInterval
= ss
->isoc_maxpacket
*
432 (ss
->isoc_mult
+ 1) * (ss
->isoc_maxburst
+ 1);
433 ss_iso_source_desc
.bEndpointAddress
=
434 fs_iso_source_desc
.bEndpointAddress
;
436 ss_iso_sink_desc
.wMaxPacketSize
= ss
->isoc_maxpacket
;
437 ss_iso_sink_desc
.bInterval
= ss
->isoc_interval
;
438 ss_iso_sink_comp_desc
.bmAttributes
= ss
->isoc_mult
;
439 ss_iso_sink_comp_desc
.bMaxBurst
= ss
->isoc_maxburst
;
440 ss_iso_sink_comp_desc
.wBytesPerInterval
= ss
->isoc_maxpacket
*
441 (ss
->isoc_mult
+ 1) * (ss
->isoc_maxburst
+ 1);
442 ss_iso_sink_desc
.bEndpointAddress
= fs_iso_sink_desc
.bEndpointAddress
;
444 ret
= usb_assign_descriptors(f
, fs_source_sink_descs
,
445 hs_source_sink_descs
, 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_free_func(struct usb_function
*f
)
461 struct f_ss_opts
*opts
;
463 opts
= container_of(f
->fi
, struct f_ss_opts
, func_inst
);
465 mutex_lock(&opts
->lock
);
467 mutex_unlock(&opts
->lock
);
469 usb_free_all_descriptors(f
);
470 kfree(func_to_ss(f
));
473 /* optionally require specific source/sink data patterns */
474 static int check_read_data(struct f_sourcesink
*ss
, struct usb_request
*req
)
478 struct usb_composite_dev
*cdev
= ss
->function
.config
->cdev
;
479 int max_packet_size
= le16_to_cpu(ss
->out_ep
->desc
->wMaxPacketSize
);
481 if (ss
->pattern
== 2)
484 for (i
= 0; i
< req
->actual
; i
++, buf
++) {
485 switch (ss
->pattern
) {
487 /* all-zeroes has no synchronization issues */
493 /* "mod63" stays in sync with short-terminated transfers,
494 * OR otherwise when host and gadget agree on how large
495 * each usb transfer request should be. Resync is done
496 * with set_interface or set_config. (We *WANT* it to
497 * get quickly out of sync if controllers or their drivers
498 * stutter for any reason, including buffer duplication...)
501 if (*buf
== (u8
)((i
% max_packet_size
) % 63))
505 ERROR(cdev
, "bad OUT byte, buf[%d] = %d\n", i
, *buf
);
506 usb_ep_set_halt(ss
->out_ep
);
512 static void reinit_write_data(struct usb_ep
*ep
, struct usb_request
*req
)
516 int max_packet_size
= le16_to_cpu(ep
->desc
->wMaxPacketSize
);
517 struct f_sourcesink
*ss
= ep
->driver_data
;
519 switch (ss
->pattern
) {
521 memset(req
->buf
, 0, req
->length
);
524 for (i
= 0; i
< req
->length
; i
++)
525 *buf
++ = (u8
) ((i
% max_packet_size
) % 63);
532 static void source_sink_complete(struct usb_ep
*ep
, struct usb_request
*req
)
534 struct usb_composite_dev
*cdev
;
535 struct f_sourcesink
*ss
= ep
->driver_data
;
536 int status
= req
->status
;
538 /* driver_data will be null if ep has been disabled */
542 cdev
= ss
->function
.config
->cdev
;
546 case 0: /* normal completion? */
547 if (ep
== ss
->out_ep
) {
548 check_read_data(ss
, req
);
549 if (ss
->pattern
!= 2)
550 memset(req
->buf
, 0x55, req
->length
);
554 /* this endpoint is normally active while we're configured */
555 case -ECONNABORTED
: /* hardware forced ep reset */
556 case -ECONNRESET
: /* request dequeued */
557 case -ESHUTDOWN
: /* disconnect from host */
558 VDBG(cdev
, "%s gone (%d), %d/%d\n", ep
->name
, status
,
559 req
->actual
, req
->length
);
560 if (ep
== ss
->out_ep
)
561 check_read_data(ss
, req
);
562 free_ep_req(ep
, req
);
565 case -EOVERFLOW
: /* buffer overrun on read means that
566 * we didn't provide a big enough
571 DBG(cdev
, "%s complete --> %d, %d/%d\n", ep
->name
,
572 status
, req
->actual
, req
->length
);
574 case -EREMOTEIO
: /* short read */
578 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
580 ERROR(cdev
, "kill %s: resubmit %d bytes --> %d\n",
581 ep
->name
, req
->length
, status
);
583 /* FIXME recover later ... somehow */
587 static int source_sink_start_ep(struct f_sourcesink
*ss
, bool is_in
,
588 bool is_iso
, int speed
)
591 struct usb_request
*req
;
594 for (i
= 0; i
< 8; i
++) {
597 case USB_SPEED_SUPER
:
598 size
= ss
->isoc_maxpacket
*
599 (ss
->isoc_mult
+ 1) *
600 (ss
->isoc_maxburst
+ 1);
603 size
= ss
->isoc_maxpacket
* (ss
->isoc_mult
+ 1);
606 size
= ss
->isoc_maxpacket
> 1023 ?
607 1023 : ss
->isoc_maxpacket
;
610 ep
= is_in
? ss
->iso_in_ep
: ss
->iso_out_ep
;
611 req
= ss_alloc_ep_req(ep
, size
);
613 ep
= is_in
? ss
->in_ep
: ss
->out_ep
;
614 req
= ss_alloc_ep_req(ep
, 0);
620 req
->complete
= source_sink_complete
;
622 reinit_write_data(ep
, req
);
623 else if (ss
->pattern
!= 2)
624 memset(req
->buf
, 0x55, req
->length
);
626 status
= usb_ep_queue(ep
, req
, GFP_ATOMIC
);
628 struct usb_composite_dev
*cdev
;
630 cdev
= ss
->function
.config
->cdev
;
631 ERROR(cdev
, "start %s%s %s --> %d\n",
632 is_iso
? "ISO-" : "", is_in
? "IN" : "OUT",
634 free_ep_req(ep
, req
);
644 static void disable_source_sink(struct f_sourcesink
*ss
)
646 struct usb_composite_dev
*cdev
;
648 cdev
= ss
->function
.config
->cdev
;
649 disable_endpoints(cdev
, ss
->in_ep
, ss
->out_ep
, ss
->iso_in_ep
,
651 VDBG(cdev
, "%s disabled\n", ss
->function
.name
);
655 enable_source_sink(struct usb_composite_dev
*cdev
, struct f_sourcesink
*ss
,
659 int speed
= cdev
->gadget
->speed
;
662 /* one bulk endpoint writes (sources) zeroes IN (to the host) */
664 result
= config_ep_by_speed(cdev
->gadget
, &(ss
->function
), ep
);
667 result
= usb_ep_enable(ep
);
670 ep
->driver_data
= ss
;
672 result
= source_sink_start_ep(ss
, true, false, speed
);
680 /* one bulk endpoint reads (sinks) anything OUT (from the host) */
682 result
= config_ep_by_speed(cdev
->gadget
, &(ss
->function
), ep
);
685 result
= usb_ep_enable(ep
);
688 ep
->driver_data
= ss
;
690 result
= source_sink_start_ep(ss
, false, false, speed
);
701 /* one iso endpoint writes (sources) zeroes IN (to the host) */
704 result
= config_ep_by_speed(cdev
->gadget
, &(ss
->function
), ep
);
707 result
= usb_ep_enable(ep
);
710 ep
->driver_data
= ss
;
712 result
= source_sink_start_ep(ss
, true, true, speed
);
722 /* one iso endpoint reads (sinks) anything OUT (from the host) */
725 result
= config_ep_by_speed(cdev
->gadget
, &(ss
->function
), ep
);
728 result
= usb_ep_enable(ep
);
731 ep
->driver_data
= ss
;
733 result
= source_sink_start_ep(ss
, false, true, speed
);
742 DBG(cdev
, "%s enabled, alt intf %d\n", ss
->function
.name
, alt
);
746 static int sourcesink_set_alt(struct usb_function
*f
,
747 unsigned intf
, unsigned alt
)
749 struct f_sourcesink
*ss
= func_to_ss(f
);
750 struct usb_composite_dev
*cdev
= f
->config
->cdev
;
752 disable_source_sink(ss
);
753 return enable_source_sink(cdev
, ss
, alt
);
756 static int sourcesink_get_alt(struct usb_function
*f
, unsigned intf
)
758 struct f_sourcesink
*ss
= func_to_ss(f
);
763 static void sourcesink_disable(struct usb_function
*f
)
765 struct f_sourcesink
*ss
= func_to_ss(f
);
767 disable_source_sink(ss
);
770 /*-------------------------------------------------------------------------*/
772 static int sourcesink_setup(struct usb_function
*f
,
773 const struct usb_ctrlrequest
*ctrl
)
775 struct usb_configuration
*c
= f
->config
;
776 struct usb_request
*req
= c
->cdev
->req
;
777 int value
= -EOPNOTSUPP
;
778 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
779 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
780 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
782 req
->length
= USB_COMP_EP0_BUFSIZ
;
784 /* composite driver infrastructure handles everything except
785 * the two control test requests.
787 switch (ctrl
->bRequest
) {
790 * These are the same vendor-specific requests supported by
791 * Intel's USB 2.0 compliance test devices. We exceed that
792 * device spec by allowing multiple-packet requests.
794 * NOTE: the Control-OUT data stays in req->buf ... better
795 * would be copying it into a scratch buffer, so that other
796 * requests may safely intervene.
798 case 0x5b: /* control WRITE test -- fill the buffer */
799 if (ctrl
->bRequestType
!= (USB_DIR_OUT
|USB_TYPE_VENDOR
))
801 if (w_value
|| w_index
)
803 /* just read that many bytes into the buffer */
804 if (w_length
> req
->length
)
808 case 0x5c: /* control READ test -- return the buffer */
809 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_TYPE_VENDOR
))
811 if (w_value
|| w_index
)
813 /* expect those bytes are still in the buffer; send back */
814 if (w_length
> req
->length
)
822 "unknown control req%02x.%02x v%04x i%04x l%d\n",
823 ctrl
->bRequestType
, ctrl
->bRequest
,
824 w_value
, w_index
, w_length
);
827 /* respond with data transfer or status phase? */
829 VDBG(c
->cdev
, "source/sink req%02x.%02x v%04x i%04x l%d\n",
830 ctrl
->bRequestType
, ctrl
->bRequest
,
831 w_value
, w_index
, w_length
);
834 value
= usb_ep_queue(c
->cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
836 ERROR(c
->cdev
, "source/sink response, err %d\n",
840 /* device either stalls (value < 0) or reports success */
844 static struct usb_function
*source_sink_alloc_func(
845 struct usb_function_instance
*fi
)
847 struct f_sourcesink
*ss
;
848 struct f_ss_opts
*ss_opts
;
850 ss
= kzalloc(sizeof(*ss
), GFP_KERNEL
);
854 ss_opts
= container_of(fi
, struct f_ss_opts
, func_inst
);
856 mutex_lock(&ss_opts
->lock
);
858 mutex_unlock(&ss_opts
->lock
);
860 ss
->pattern
= ss_opts
->pattern
;
861 ss
->isoc_interval
= ss_opts
->isoc_interval
;
862 ss
->isoc_maxpacket
= ss_opts
->isoc_maxpacket
;
863 ss
->isoc_mult
= ss_opts
->isoc_mult
;
864 ss
->isoc_maxburst
= ss_opts
->isoc_maxburst
;
865 ss
->buflen
= ss_opts
->bulk_buflen
;
867 ss
->function
.name
= "source/sink";
868 ss
->function
.bind
= sourcesink_bind
;
869 ss
->function
.set_alt
= sourcesink_set_alt
;
870 ss
->function
.get_alt
= sourcesink_get_alt
;
871 ss
->function
.disable
= sourcesink_disable
;
872 ss
->function
.setup
= sourcesink_setup
;
873 ss
->function
.strings
= sourcesink_strings
;
875 ss
->function
.free_func
= sourcesink_free_func
;
877 return &ss
->function
;
880 static inline struct f_ss_opts
*to_f_ss_opts(struct config_item
*item
)
882 return container_of(to_config_group(item
), struct f_ss_opts
,
886 static void ss_attr_release(struct config_item
*item
)
888 struct f_ss_opts
*ss_opts
= to_f_ss_opts(item
);
890 usb_put_function_instance(&ss_opts
->func_inst
);
893 static struct configfs_item_operations ss_item_ops
= {
894 .release
= ss_attr_release
,
897 static ssize_t
f_ss_opts_pattern_show(struct config_item
*item
, char *page
)
899 struct f_ss_opts
*opts
= to_f_ss_opts(item
);
902 mutex_lock(&opts
->lock
);
903 result
= sprintf(page
, "%u\n", opts
->pattern
);
904 mutex_unlock(&opts
->lock
);
909 static ssize_t
f_ss_opts_pattern_store(struct config_item
*item
,
910 const char *page
, size_t len
)
912 struct f_ss_opts
*opts
= to_f_ss_opts(item
);
916 mutex_lock(&opts
->lock
);
922 ret
= kstrtou8(page
, 0, &num
);
926 if (num
!= 0 && num
!= 1 && num
!= 2) {
934 mutex_unlock(&opts
->lock
);
938 CONFIGFS_ATTR(f_ss_opts_
, pattern
);
940 static ssize_t
f_ss_opts_isoc_interval_show(struct config_item
*item
, char *page
)
942 struct f_ss_opts
*opts
= to_f_ss_opts(item
);
945 mutex_lock(&opts
->lock
);
946 result
= sprintf(page
, "%u\n", opts
->isoc_interval
);
947 mutex_unlock(&opts
->lock
);
952 static ssize_t
f_ss_opts_isoc_interval_store(struct config_item
*item
,
953 const char *page
, size_t len
)
955 struct f_ss_opts
*opts
= to_f_ss_opts(item
);
959 mutex_lock(&opts
->lock
);
965 ret
= kstrtou8(page
, 0, &num
);
974 opts
->isoc_interval
= num
;
977 mutex_unlock(&opts
->lock
);
981 CONFIGFS_ATTR(f_ss_opts_
, isoc_interval
);
983 static ssize_t
f_ss_opts_isoc_maxpacket_show(struct config_item
*item
, char *page
)
985 struct f_ss_opts
*opts
= to_f_ss_opts(item
);
988 mutex_lock(&opts
->lock
);
989 result
= sprintf(page
, "%u\n", opts
->isoc_maxpacket
);
990 mutex_unlock(&opts
->lock
);
995 static ssize_t
f_ss_opts_isoc_maxpacket_store(struct config_item
*item
,
996 const char *page
, size_t len
)
998 struct f_ss_opts
*opts
= to_f_ss_opts(item
);
1002 mutex_lock(&opts
->lock
);
1008 ret
= kstrtou16(page
, 0, &num
);
1017 opts
->isoc_maxpacket
= num
;
1020 mutex_unlock(&opts
->lock
);
1024 CONFIGFS_ATTR(f_ss_opts_
, isoc_maxpacket
);
1026 static ssize_t
f_ss_opts_isoc_mult_show(struct config_item
*item
, char *page
)
1028 struct f_ss_opts
*opts
= to_f_ss_opts(item
);
1031 mutex_lock(&opts
->lock
);
1032 result
= sprintf(page
, "%u\n", opts
->isoc_mult
);
1033 mutex_unlock(&opts
->lock
);
1038 static ssize_t
f_ss_opts_isoc_mult_store(struct config_item
*item
,
1039 const char *page
, size_t len
)
1041 struct f_ss_opts
*opts
= to_f_ss_opts(item
);
1045 mutex_lock(&opts
->lock
);
1051 ret
= kstrtou8(page
, 0, &num
);
1060 opts
->isoc_mult
= num
;
1063 mutex_unlock(&opts
->lock
);
1067 CONFIGFS_ATTR(f_ss_opts_
, isoc_mult
);
1069 static ssize_t
f_ss_opts_isoc_maxburst_show(struct config_item
*item
, char *page
)
1071 struct f_ss_opts
*opts
= to_f_ss_opts(item
);
1074 mutex_lock(&opts
->lock
);
1075 result
= sprintf(page
, "%u\n", opts
->isoc_maxburst
);
1076 mutex_unlock(&opts
->lock
);
1081 static ssize_t
f_ss_opts_isoc_maxburst_store(struct config_item
*item
,
1082 const char *page
, size_t len
)
1084 struct f_ss_opts
*opts
= to_f_ss_opts(item
);
1088 mutex_lock(&opts
->lock
);
1094 ret
= kstrtou8(page
, 0, &num
);
1103 opts
->isoc_maxburst
= num
;
1106 mutex_unlock(&opts
->lock
);
1110 CONFIGFS_ATTR(f_ss_opts_
, isoc_maxburst
);
1112 static ssize_t
f_ss_opts_bulk_buflen_show(struct config_item
*item
, char *page
)
1114 struct f_ss_opts
*opts
= to_f_ss_opts(item
);
1117 mutex_lock(&opts
->lock
);
1118 result
= sprintf(page
, "%u\n", opts
->bulk_buflen
);
1119 mutex_unlock(&opts
->lock
);
1124 static ssize_t
f_ss_opts_bulk_buflen_store(struct config_item
*item
,
1125 const char *page
, size_t len
)
1127 struct f_ss_opts
*opts
= to_f_ss_opts(item
);
1131 mutex_lock(&opts
->lock
);
1137 ret
= kstrtou32(page
, 0, &num
);
1141 opts
->bulk_buflen
= num
;
1144 mutex_unlock(&opts
->lock
);
1148 CONFIGFS_ATTR(f_ss_opts_
, bulk_buflen
);
1150 static struct configfs_attribute
*ss_attrs
[] = {
1151 &f_ss_opts_attr_pattern
,
1152 &f_ss_opts_attr_isoc_interval
,
1153 &f_ss_opts_attr_isoc_maxpacket
,
1154 &f_ss_opts_attr_isoc_mult
,
1155 &f_ss_opts_attr_isoc_maxburst
,
1156 &f_ss_opts_attr_bulk_buflen
,
1160 static struct config_item_type ss_func_type
= {
1161 .ct_item_ops
= &ss_item_ops
,
1162 .ct_attrs
= ss_attrs
,
1163 .ct_owner
= THIS_MODULE
,
1166 static void source_sink_free_instance(struct usb_function_instance
*fi
)
1168 struct f_ss_opts
*ss_opts
;
1170 ss_opts
= container_of(fi
, struct f_ss_opts
, func_inst
);
1174 static struct usb_function_instance
*source_sink_alloc_inst(void)
1176 struct f_ss_opts
*ss_opts
;
1178 ss_opts
= kzalloc(sizeof(*ss_opts
), GFP_KERNEL
);
1180 return ERR_PTR(-ENOMEM
);
1181 mutex_init(&ss_opts
->lock
);
1182 ss_opts
->func_inst
.free_func_inst
= source_sink_free_instance
;
1183 ss_opts
->isoc_interval
= GZERO_ISOC_INTERVAL
;
1184 ss_opts
->isoc_maxpacket
= GZERO_ISOC_MAXPACKET
;
1185 ss_opts
->bulk_buflen
= GZERO_BULK_BUFLEN
;
1187 config_group_init_type_name(&ss_opts
->func_inst
.group
, "",
1190 return &ss_opts
->func_inst
;
1192 DECLARE_USB_FUNCTION(SourceSink
, source_sink_alloc_inst
,
1193 source_sink_alloc_func
);
1195 static int __init
sslb_modinit(void)
1199 ret
= usb_function_register(&SourceSinkusb_func
);
1204 usb_function_unregister(&SourceSinkusb_func
);
1207 static void __exit
sslb_modexit(void)
1209 usb_function_unregister(&SourceSinkusb_func
);
1212 module_init(sslb_modinit
);
1213 module_exit(sslb_modexit
);
1215 MODULE_LICENSE("GPL");