dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / usb / gadget / function / f_sourcesink.c
blob67b24398993898dd556f8e6a921f0ad093d2bc09
1 /*
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>
22 #include "g_zero.h"
23 #include "u_f.h"
26 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
27 * controller drivers.
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.
45 struct f_sourcesink {
46 struct usb_function function;
48 struct usb_ep *in_ep;
49 struct usb_ep *out_ep;
50 struct usb_ep *iso_in_ep;
51 struct usb_ep *iso_out_ep;
52 int cur_alt;
54 unsigned pattern;
55 unsigned isoc_interval;
56 unsigned isoc_maxpacket;
57 unsigned isoc_mult;
58 unsigned isoc_maxburst;
59 unsigned buflen;
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,
74 .bNumEndpoints = 2,
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,
84 .bNumEndpoints = 4,
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),
114 .bInterval = 4,
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),
124 .bInterval = 4,
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,
137 NULL,
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),
164 .bInterval = 4,
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),
173 .bInterval = 4,
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,
186 NULL,
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,
203 .bMaxBurst = 0,
204 .bmAttributes = 0,
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,
220 .bMaxBurst = 0,
221 .bmAttributes = 0,
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),
231 .bInterval = 4,
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,
238 .bMaxBurst = 0,
239 .bmAttributes = 0,
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),
249 .bInterval = 4,
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,
256 .bMaxBurst = 0,
257 .bmAttributes = 0,
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,
277 NULL,
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,
294 NULL,
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)
308 int value;
310 value = usb_ep_disable(ep);
311 if (value < 0)
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);
321 if (iso_in)
322 disable_ep(cdev, iso_in);
323 if (iso_out)
324 disable_ep(cdev, iso_out);
327 static int
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);
332 int id;
333 int ret;
335 /* allocate interface ID(s) */
336 id = usb_interface_id(c, f);
337 if (id < 0)
338 return id;
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);
344 if (!ss->in_ep) {
345 autoconf_fail:
346 ERROR(cdev, "%s: can't autoconfigure on %s\n",
347 f->name, cdev->gadget->name);
348 return -ENODEV;
351 ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
352 if (!ss->out_ep)
353 goto autoconf_fail;
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)
361 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);
375 if (!ss->iso_in_ep)
376 goto no_iso;
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;
382 no_iso:
384 * We still want to work even if the UDC doesn't have isoc
385 * endpoints, so null out the alt interface that contains
386 * them and continue.
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);
446 if (ret)
447 return ret;
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>");
455 return 0;
458 static void
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);
466 opts->refcnt--;
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)
476 unsigned i;
477 u8 *buf = req->buf;
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)
482 return 0;
484 for (i = 0; i < req->actual; i++, buf++) {
485 switch (ss->pattern) {
487 /* all-zeroes has no synchronization issues */
488 case 0:
489 if (*buf == 0)
490 continue;
491 break;
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...)
500 case 1:
501 if (*buf == (u8)((i % max_packet_size) % 63))
502 continue;
503 break;
505 ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
506 usb_ep_set_halt(ss->out_ep);
507 return -EINVAL;
509 return 0;
512 static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
514 unsigned i;
515 u8 *buf = req->buf;
516 int max_packet_size = le16_to_cpu(ep->desc->wMaxPacketSize);
517 struct f_sourcesink *ss = ep->driver_data;
519 switch (ss->pattern) {
520 case 0:
521 memset(req->buf, 0, req->length);
522 break;
523 case 1:
524 for (i = 0; i < req->length; i++)
525 *buf++ = (u8) ((i % max_packet_size) % 63);
526 break;
527 case 2:
528 break;
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 */
539 if (!ss)
540 return;
542 cdev = ss->function.config->cdev;
544 switch (status) {
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);
552 break;
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);
563 return;
565 case -EOVERFLOW: /* buffer overrun on read means that
566 * we didn't provide a big enough
567 * buffer.
569 default:
570 #if 1
571 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
572 status, req->actual, req->length);
573 #endif
574 case -EREMOTEIO: /* short read */
575 break;
578 status = usb_ep_queue(ep, req, GFP_ATOMIC);
579 if (status) {
580 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
581 ep->name, req->length, status);
582 usb_ep_set_halt(ep);
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)
590 struct usb_ep *ep;
591 struct usb_request *req;
592 int i, size, status;
594 for (i = 0; i < 8; i++) {
595 if (is_iso) {
596 switch (speed) {
597 case USB_SPEED_SUPER:
598 size = ss->isoc_maxpacket *
599 (ss->isoc_mult + 1) *
600 (ss->isoc_maxburst + 1);
601 break;
602 case USB_SPEED_HIGH:
603 size = ss->isoc_maxpacket * (ss->isoc_mult + 1);
604 break;
605 default:
606 size = ss->isoc_maxpacket > 1023 ?
607 1023 : ss->isoc_maxpacket;
608 break;
610 ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
611 req = ss_alloc_ep_req(ep, size);
612 } else {
613 ep = is_in ? ss->in_ep : ss->out_ep;
614 req = ss_alloc_ep_req(ep, 0);
617 if (!req)
618 return -ENOMEM;
620 req->complete = source_sink_complete;
621 if (is_in)
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);
627 if (status) {
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",
633 ep->name, status);
634 free_ep_req(ep, req);
637 if (!is_iso)
638 break;
641 return status;
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,
650 ss->iso_out_ep);
651 VDBG(cdev, "%s disabled\n", ss->function.name);
654 static int
655 enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
656 int alt)
658 int result = 0;
659 int speed = cdev->gadget->speed;
660 struct usb_ep *ep;
662 /* one bulk endpoint writes (sources) zeroes IN (to the host) */
663 ep = ss->in_ep;
664 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
665 if (result)
666 return result;
667 result = usb_ep_enable(ep);
668 if (result < 0)
669 return result;
670 ep->driver_data = ss;
672 result = source_sink_start_ep(ss, true, false, speed);
673 if (result < 0) {
674 fail:
675 ep = ss->in_ep;
676 usb_ep_disable(ep);
677 return result;
680 /* one bulk endpoint reads (sinks) anything OUT (from the host) */
681 ep = ss->out_ep;
682 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
683 if (result)
684 goto fail;
685 result = usb_ep_enable(ep);
686 if (result < 0)
687 goto fail;
688 ep->driver_data = ss;
690 result = source_sink_start_ep(ss, false, false, speed);
691 if (result < 0) {
692 fail2:
693 ep = ss->out_ep;
694 usb_ep_disable(ep);
695 goto fail;
698 if (alt == 0)
699 goto out;
701 /* one iso endpoint writes (sources) zeroes IN (to the host) */
702 ep = ss->iso_in_ep;
703 if (ep) {
704 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
705 if (result)
706 goto fail2;
707 result = usb_ep_enable(ep);
708 if (result < 0)
709 goto fail2;
710 ep->driver_data = ss;
712 result = source_sink_start_ep(ss, true, true, speed);
713 if (result < 0) {
714 fail3:
715 ep = ss->iso_in_ep;
716 if (ep)
717 usb_ep_disable(ep);
718 goto fail2;
722 /* one iso endpoint reads (sinks) anything OUT (from the host) */
723 ep = ss->iso_out_ep;
724 if (ep) {
725 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
726 if (result)
727 goto fail3;
728 result = usb_ep_enable(ep);
729 if (result < 0)
730 goto fail3;
731 ep->driver_data = ss;
733 result = source_sink_start_ep(ss, false, true, speed);
734 if (result < 0) {
735 usb_ep_disable(ep);
736 goto fail3;
739 out:
740 ss->cur_alt = alt;
742 DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
743 return result;
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);
760 return ss->cur_alt;
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))
800 goto unknown;
801 if (w_value || w_index)
802 break;
803 /* just read that many bytes into the buffer */
804 if (w_length > req->length)
805 break;
806 value = w_length;
807 break;
808 case 0x5c: /* control READ test -- return the buffer */
809 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
810 goto unknown;
811 if (w_value || w_index)
812 break;
813 /* expect those bytes are still in the buffer; send back */
814 if (w_length > req->length)
815 break;
816 value = w_length;
817 break;
819 default:
820 unknown:
821 VDBG(c->cdev,
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? */
828 if (value >= 0) {
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);
832 req->zero = 0;
833 req->length = value;
834 value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
835 if (value < 0)
836 ERROR(c->cdev, "source/sink response, err %d\n",
837 value);
840 /* device either stalls (value < 0) or reports success */
841 return value;
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);
851 if (!ss)
852 return NULL;
854 ss_opts = container_of(fi, struct f_ss_opts, func_inst);
856 mutex_lock(&ss_opts->lock);
857 ss_opts->refcnt++;
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,
883 func_inst.group);
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);
900 int result;
902 mutex_lock(&opts->lock);
903 result = sprintf(page, "%u\n", opts->pattern);
904 mutex_unlock(&opts->lock);
906 return result;
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);
913 int ret;
914 u8 num;
916 mutex_lock(&opts->lock);
917 if (opts->refcnt) {
918 ret = -EBUSY;
919 goto end;
922 ret = kstrtou8(page, 0, &num);
923 if (ret)
924 goto end;
926 if (num != 0 && num != 1 && num != 2) {
927 ret = -EINVAL;
928 goto end;
931 opts->pattern = num;
932 ret = len;
933 end:
934 mutex_unlock(&opts->lock);
935 return ret;
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);
943 int result;
945 mutex_lock(&opts->lock);
946 result = sprintf(page, "%u\n", opts->isoc_interval);
947 mutex_unlock(&opts->lock);
949 return result;
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);
956 int ret;
957 u8 num;
959 mutex_lock(&opts->lock);
960 if (opts->refcnt) {
961 ret = -EBUSY;
962 goto end;
965 ret = kstrtou8(page, 0, &num);
966 if (ret)
967 goto end;
969 if (num > 16) {
970 ret = -EINVAL;
971 goto end;
974 opts->isoc_interval = num;
975 ret = len;
976 end:
977 mutex_unlock(&opts->lock);
978 return ret;
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);
986 int result;
988 mutex_lock(&opts->lock);
989 result = sprintf(page, "%u\n", opts->isoc_maxpacket);
990 mutex_unlock(&opts->lock);
992 return result;
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);
999 int ret;
1000 u16 num;
1002 mutex_lock(&opts->lock);
1003 if (opts->refcnt) {
1004 ret = -EBUSY;
1005 goto end;
1008 ret = kstrtou16(page, 0, &num);
1009 if (ret)
1010 goto end;
1012 if (num > 1024) {
1013 ret = -EINVAL;
1014 goto end;
1017 opts->isoc_maxpacket = num;
1018 ret = len;
1019 end:
1020 mutex_unlock(&opts->lock);
1021 return ret;
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);
1029 int result;
1031 mutex_lock(&opts->lock);
1032 result = sprintf(page, "%u\n", opts->isoc_mult);
1033 mutex_unlock(&opts->lock);
1035 return result;
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);
1042 int ret;
1043 u8 num;
1045 mutex_lock(&opts->lock);
1046 if (opts->refcnt) {
1047 ret = -EBUSY;
1048 goto end;
1051 ret = kstrtou8(page, 0, &num);
1052 if (ret)
1053 goto end;
1055 if (num > 2) {
1056 ret = -EINVAL;
1057 goto end;
1060 opts->isoc_mult = num;
1061 ret = len;
1062 end:
1063 mutex_unlock(&opts->lock);
1064 return ret;
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);
1072 int result;
1074 mutex_lock(&opts->lock);
1075 result = sprintf(page, "%u\n", opts->isoc_maxburst);
1076 mutex_unlock(&opts->lock);
1078 return result;
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);
1085 int ret;
1086 u8 num;
1088 mutex_lock(&opts->lock);
1089 if (opts->refcnt) {
1090 ret = -EBUSY;
1091 goto end;
1094 ret = kstrtou8(page, 0, &num);
1095 if (ret)
1096 goto end;
1098 if (num > 15) {
1099 ret = -EINVAL;
1100 goto end;
1103 opts->isoc_maxburst = num;
1104 ret = len;
1105 end:
1106 mutex_unlock(&opts->lock);
1107 return ret;
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);
1115 int result;
1117 mutex_lock(&opts->lock);
1118 result = sprintf(page, "%u\n", opts->bulk_buflen);
1119 mutex_unlock(&opts->lock);
1121 return result;
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);
1128 int ret;
1129 u32 num;
1131 mutex_lock(&opts->lock);
1132 if (opts->refcnt) {
1133 ret = -EBUSY;
1134 goto end;
1137 ret = kstrtou32(page, 0, &num);
1138 if (ret)
1139 goto end;
1141 opts->bulk_buflen = num;
1142 ret = len;
1143 end:
1144 mutex_unlock(&opts->lock);
1145 return ret;
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,
1157 NULL,
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);
1171 kfree(ss_opts);
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);
1179 if (!ss_opts)
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, "",
1188 &ss_func_type);
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)
1197 int ret;
1199 ret = usb_function_register(&SourceSinkusb_func);
1200 if (ret)
1201 return ret;
1202 ret = lb_modinit();
1203 if (ret)
1204 usb_function_unregister(&SourceSinkusb_func);
1205 return ret;
1207 static void __exit sslb_modexit(void)
1209 usb_function_unregister(&SourceSinkusb_func);
1210 lb_modexit();
1212 module_init(sslb_modinit);
1213 module_exit(sslb_modexit);
1215 MODULE_LICENSE("GPL");