1 // SPDX-License-Identifier: GPL-2.0+
3 * f_sourcesink.c - USB peripheral source/sink configuration driver
5 * Copyright (C) 2003-2008 David Brownell
6 * Copyright (C) 2008 by Nokia Corporation
9 /* #define VERBOSE_DEBUG */
11 #include <linux/slab.h>
12 #include <linux/kernel.h>
13 #include <linux/device.h>
14 #include <linux/module.h>
15 #include <linux/usb/composite.h>
16 #include <linux/err.h>
22 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
25 * This just sinks bulk packets OUT to the peripheral and sources them IN
26 * to the host, optionally with specific data patterns for integrity tests.
27 * As such it supports basic functionality and load tests.
29 * In terms of control messaging, this supports all the standard requests
30 * plus two that support control-OUT tests. If the optional "autoresume"
31 * mode is enabled, it provides good functional coverage for the "USBCV"
32 * test harness from USB-IF.
35 struct usb_function function;
38 struct usb_ep *out_ep;
39 struct usb_ep *iso_in_ep;
40 struct usb_ep *iso_out_ep;
44 unsigned isoc_interval;
45 unsigned isoc_maxpacket;
47 unsigned isoc_maxburst;
53 static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
55 return container_of(f, struct f_sourcesink, function);
58 /*-------------------------------------------------------------------------*/
60 static struct usb_interface_descriptor source_sink_intf_alt0 = {
61 .bLength = USB_DT_INTERFACE_SIZE,
62 .bDescriptorType = USB_DT_INTERFACE,
64 .bAlternateSetting = 0,
66 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
67 /* .iInterface = DYNAMIC */
70 static struct usb_interface_descriptor source_sink_intf_alt1 = {
71 .bLength = USB_DT_INTERFACE_SIZE,
72 .bDescriptorType = USB_DT_INTERFACE,
74 .bAlternateSetting = 1,
76 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
77 /* .iInterface = DYNAMIC */
80 /* full speed support: */
82 static struct usb_endpoint_descriptor fs_source_desc = {
83 .bLength = USB_DT_ENDPOINT_SIZE,
84 .bDescriptorType = USB_DT_ENDPOINT,
86 .bEndpointAddress = USB_DIR_IN,
87 .bmAttributes = USB_ENDPOINT_XFER_BULK,
90 static struct usb_endpoint_descriptor fs_sink_desc = {
91 .bLength = USB_DT_ENDPOINT_SIZE,
92 .bDescriptorType = USB_DT_ENDPOINT,
94 .bEndpointAddress = USB_DIR_OUT,
95 .bmAttributes = USB_ENDPOINT_XFER_BULK,
98 static struct usb_endpoint_descriptor fs_iso_source_desc = {
99 .bLength = USB_DT_ENDPOINT_SIZE,
100 .bDescriptorType = USB_DT_ENDPOINT,
102 .bEndpointAddress = USB_DIR_IN,
103 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
104 .wMaxPacketSize = cpu_to_le16(1023),
108 static struct usb_endpoint_descriptor fs_iso_sink_desc = {
109 .bLength = USB_DT_ENDPOINT_SIZE,
110 .bDescriptorType = USB_DT_ENDPOINT,
112 .bEndpointAddress = USB_DIR_OUT,
113 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
114 .wMaxPacketSize = cpu_to_le16(1023),
118 static struct usb_descriptor_header *fs_source_sink_descs[] = {
119 (struct usb_descriptor_header *) &source_sink_intf_alt0,
120 (struct usb_descriptor_header *) &fs_sink_desc,
121 (struct usb_descriptor_header *) &fs_source_desc,
122 (struct usb_descriptor_header *) &source_sink_intf_alt1,
123 #define FS_ALT_IFC_1_OFFSET 3
124 (struct usb_descriptor_header *) &fs_sink_desc,
125 (struct usb_descriptor_header *) &fs_source_desc,
126 (struct usb_descriptor_header *) &fs_iso_sink_desc,
127 (struct usb_descriptor_header *) &fs_iso_source_desc,
131 /* high speed support: */
133 static struct usb_endpoint_descriptor hs_source_desc = {
134 .bLength = USB_DT_ENDPOINT_SIZE,
135 .bDescriptorType = USB_DT_ENDPOINT,
137 .bmAttributes = USB_ENDPOINT_XFER_BULK,
138 .wMaxPacketSize = cpu_to_le16(512),
141 static struct usb_endpoint_descriptor hs_sink_desc = {
142 .bLength = USB_DT_ENDPOINT_SIZE,
143 .bDescriptorType = USB_DT_ENDPOINT,
145 .bmAttributes = USB_ENDPOINT_XFER_BULK,
146 .wMaxPacketSize = cpu_to_le16(512),
149 static struct usb_endpoint_descriptor hs_iso_source_desc = {
150 .bLength = USB_DT_ENDPOINT_SIZE,
151 .bDescriptorType = USB_DT_ENDPOINT,
153 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
154 .wMaxPacketSize = cpu_to_le16(1024),
158 static struct usb_endpoint_descriptor hs_iso_sink_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_descriptor_header *hs_source_sink_descs[] = {
168 (struct usb_descriptor_header *) &source_sink_intf_alt0,
169 (struct usb_descriptor_header *) &hs_source_desc,
170 (struct usb_descriptor_header *) &hs_sink_desc,
171 (struct usb_descriptor_header *) &source_sink_intf_alt1,
172 #define HS_ALT_IFC_1_OFFSET 3
173 (struct usb_descriptor_header *) &hs_source_desc,
174 (struct usb_descriptor_header *) &hs_sink_desc,
175 (struct usb_descriptor_header *) &hs_iso_source_desc,
176 (struct usb_descriptor_header *) &hs_iso_sink_desc,
180 /* super speed support: */
182 static struct usb_endpoint_descriptor ss_source_desc = {
183 .bLength = USB_DT_ENDPOINT_SIZE,
184 .bDescriptorType = USB_DT_ENDPOINT,
186 .bmAttributes = USB_ENDPOINT_XFER_BULK,
187 .wMaxPacketSize = cpu_to_le16(1024),
190 static struct usb_ss_ep_comp_descriptor ss_source_comp_desc = {
191 .bLength = USB_DT_SS_EP_COMP_SIZE,
192 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
196 .wBytesPerInterval = 0,
199 static struct usb_endpoint_descriptor ss_sink_desc = {
200 .bLength = USB_DT_ENDPOINT_SIZE,
201 .bDescriptorType = USB_DT_ENDPOINT,
203 .bmAttributes = USB_ENDPOINT_XFER_BULK,
204 .wMaxPacketSize = cpu_to_le16(1024),
207 static struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = {
208 .bLength = USB_DT_SS_EP_COMP_SIZE,
209 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
213 .wBytesPerInterval = 0,
216 static struct usb_endpoint_descriptor ss_iso_source_desc = {
217 .bLength = USB_DT_ENDPOINT_SIZE,
218 .bDescriptorType = USB_DT_ENDPOINT,
220 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
221 .wMaxPacketSize = cpu_to_le16(1024),
225 static struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc = {
226 .bLength = USB_DT_SS_EP_COMP_SIZE,
227 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
231 .wBytesPerInterval = cpu_to_le16(1024),
234 static struct usb_endpoint_descriptor ss_iso_sink_desc = {
235 .bLength = USB_DT_ENDPOINT_SIZE,
236 .bDescriptorType = USB_DT_ENDPOINT,
238 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
239 .wMaxPacketSize = cpu_to_le16(1024),
243 static struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = {
244 .bLength = USB_DT_SS_EP_COMP_SIZE,
245 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
249 .wBytesPerInterval = cpu_to_le16(1024),
252 static struct usb_descriptor_header *ss_source_sink_descs[] = {
253 (struct usb_descriptor_header *) &source_sink_intf_alt0,
254 (struct usb_descriptor_header *) &ss_source_desc,
255 (struct usb_descriptor_header *) &ss_source_comp_desc,
256 (struct usb_descriptor_header *) &ss_sink_desc,
257 (struct usb_descriptor_header *) &ss_sink_comp_desc,
258 (struct usb_descriptor_header *) &source_sink_intf_alt1,
259 #define SS_ALT_IFC_1_OFFSET 5
260 (struct usb_descriptor_header *) &ss_source_desc,
261 (struct usb_descriptor_header *) &ss_source_comp_desc,
262 (struct usb_descriptor_header *) &ss_sink_desc,
263 (struct usb_descriptor_header *) &ss_sink_comp_desc,
264 (struct usb_descriptor_header *) &ss_iso_source_desc,
265 (struct usb_descriptor_header *) &ss_iso_source_comp_desc,
266 (struct usb_descriptor_header *) &ss_iso_sink_desc,
267 (struct usb_descriptor_header *) &ss_iso_sink_comp_desc,
271 /* function-specific strings: */
273 static struct usb_string strings_sourcesink[] = {
274 [0].s = "source and sink data",
275 { } /* end of list */
278 static struct usb_gadget_strings stringtab_sourcesink = {
279 .language = 0x0409, /* en-us */
280 .strings = strings_sourcesink,
283 static struct usb_gadget_strings *sourcesink_strings[] = {
284 &stringtab_sourcesink,
288 /*-------------------------------------------------------------------------*/
290 static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len)
292 return alloc_ep_req(ep, len);
295 static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
299 value = usb_ep_disable(ep);
301 DBG(cdev, "disable %s --> %d\n", ep->name, value);
304 void disable_endpoints(struct usb_composite_dev *cdev,
305 struct usb_ep *in, struct usb_ep *out,
306 struct usb_ep *iso_in, struct usb_ep *iso_out)
308 disable_ep(cdev, in);
309 disable_ep(cdev, out);
311 disable_ep(cdev, iso_in);
313 disable_ep(cdev, iso_out);
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);
324 /* allocate interface ID(s) */
325 id = usb_interface_id(c, f);
328 source_sink_intf_alt0.bInterfaceNumber = id;
329 source_sink_intf_alt1.bInterfaceNumber = id;
331 /* allocate bulk endpoints */
332 ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
335 ERROR(cdev, "%s: can't autoconfigure on %s\n",
336 f->name, cdev->gadget->name);
340 ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
344 /* sanity check the isoc module parameters */
345 if (ss->isoc_interval < 1)
346 ss->isoc_interval = 1;
347 if (ss->isoc_interval > 16)
348 ss->isoc_interval = 16;
349 if (ss->isoc_mult > 2)
351 if (ss->isoc_maxburst > 15)
352 ss->isoc_maxburst = 15;
354 /* fill in the FS isoc descriptors from the module parameters */
355 fs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket > 1023 ?
356 1023 : ss->isoc_maxpacket;
357 fs_iso_source_desc.bInterval = ss->isoc_interval;
358 fs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket > 1023 ?
359 1023 : ss->isoc_maxpacket;
360 fs_iso_sink_desc.bInterval = ss->isoc_interval;
362 /* allocate iso endpoints */
363 ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
367 ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
368 if (!ss->iso_out_ep) {
369 usb_ep_autoconfig_release(ss->iso_in_ep);
370 ss->iso_in_ep = NULL;
373 * We still want to work even if the UDC doesn't have isoc
374 * endpoints, so null out the alt interface that contains
377 fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
378 hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
379 ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
382 if (ss->isoc_maxpacket > 1024)
383 ss->isoc_maxpacket = 1024;
385 /* support high speed hardware */
386 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
387 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
390 * Fill in the HS isoc descriptors from the module parameters.
391 * We assume that the user knows what they are doing and won't
392 * give parameters that their UDC doesn't support.
394 hs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket;
395 hs_iso_source_desc.wMaxPacketSize |= ss->isoc_mult << 11;
396 hs_iso_source_desc.bInterval = ss->isoc_interval;
397 hs_iso_source_desc.bEndpointAddress =
398 fs_iso_source_desc.bEndpointAddress;
400 hs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket;
401 hs_iso_sink_desc.wMaxPacketSize |= ss->isoc_mult << 11;
402 hs_iso_sink_desc.bInterval = ss->isoc_interval;
403 hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
405 /* support super speed hardware */
406 ss_source_desc.bEndpointAddress =
407 fs_source_desc.bEndpointAddress;
408 ss_sink_desc.bEndpointAddress =
409 fs_sink_desc.bEndpointAddress;
412 * Fill in the SS isoc descriptors from the module parameters.
413 * We assume that the user knows what they are doing and won't
414 * give parameters that their UDC doesn't support.
416 ss_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket;
417 ss_iso_source_desc.bInterval = ss->isoc_interval;
418 ss_iso_source_comp_desc.bmAttributes = ss->isoc_mult;
419 ss_iso_source_comp_desc.bMaxBurst = ss->isoc_maxburst;
420 ss_iso_source_comp_desc.wBytesPerInterval = ss->isoc_maxpacket *
421 (ss->isoc_mult + 1) * (ss->isoc_maxburst + 1);
422 ss_iso_source_desc.bEndpointAddress =
423 fs_iso_source_desc.bEndpointAddress;
425 ss_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket;
426 ss_iso_sink_desc.bInterval = ss->isoc_interval;
427 ss_iso_sink_comp_desc.bmAttributes = ss->isoc_mult;
428 ss_iso_sink_comp_desc.bMaxBurst = ss->isoc_maxburst;
429 ss_iso_sink_comp_desc.wBytesPerInterval = ss->isoc_maxpacket *
430 (ss->isoc_mult + 1) * (ss->isoc_maxburst + 1);
431 ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
433 ret = usb_assign_descriptors(f, fs_source_sink_descs,
434 hs_source_sink_descs, ss_source_sink_descs,
435 ss_source_sink_descs);
439 DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
440 (gadget_is_superspeed(c->cdev->gadget) ? "super" :
441 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
442 f->name, ss->in_ep->name, ss->out_ep->name,
443 ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
444 ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
449 sourcesink_free_func(struct usb_function *f)
451 struct f_ss_opts *opts;
453 opts = container_of(f->fi, struct f_ss_opts, func_inst);
455 mutex_lock(&opts->lock);
457 mutex_unlock(&opts->lock);
459 usb_free_all_descriptors(f);
460 kfree(func_to_ss(f));
463 /* optionally require specific source/sink data patterns */
464 static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
468 struct usb_composite_dev *cdev = ss->function.config->cdev;
469 int max_packet_size = le16_to_cpu(ss->out_ep->desc->wMaxPacketSize);
471 if (ss->pattern == 2)
474 for (i = 0; i < req->actual; i++, buf++) {
475 switch (ss->pattern) {
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 % max_packet_size) % 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)
506 int max_packet_size = le16_to_cpu(ep->desc->wMaxPacketSize);
507 struct f_sourcesink *ss = ep->driver_data;
509 switch (ss->pattern) {
511 memset(req->buf, 0, req->length);
514 for (i = 0; i < req->length; i++)
515 *buf++ = (u8) ((i % max_packet_size) % 63);
522 static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
524 struct usb_composite_dev *cdev;
525 struct f_sourcesink *ss = ep->driver_data;
526 int status = req->status;
528 /* driver_data will be null if ep has been disabled */
532 cdev = ss->function.config->cdev;
536 case 0: /* normal completion? */
537 if (ep == ss->out_ep) {
538 check_read_data(ss, req);
539 if (ss->pattern != 2)
540 memset(req->buf, 0x55, req->length);
544 /* this endpoint is normally active while we're configured */
545 case -ECONNABORTED: /* hardware forced ep reset */
546 case -ECONNRESET: /* request dequeued */
547 case -ESHUTDOWN: /* disconnect from host */
548 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
549 req->actual, req->length);
550 if (ep == ss->out_ep)
551 check_read_data(ss, req);
552 free_ep_req(ep, req);
555 case -EOVERFLOW: /* buffer overrun on read means that
556 * we didn't provide a big enough
561 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
562 status, req->actual, req->length);
565 case -EREMOTEIO: /* short read */
569 status = usb_ep_queue(ep, req, GFP_ATOMIC);
571 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
572 ep->name, req->length, status);
574 /* FIXME recover later ... somehow */
578 static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
579 bool is_iso, int speed)
582 struct usb_request *req;
583 int i, size, qlen, status = 0;
587 case USB_SPEED_SUPER:
588 size = ss->isoc_maxpacket *
589 (ss->isoc_mult + 1) *
590 (ss->isoc_maxburst + 1);
593 size = ss->isoc_maxpacket * (ss->isoc_mult + 1);
596 size = ss->isoc_maxpacket > 1023 ?
597 1023 : ss->isoc_maxpacket;
600 ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
603 ep = is_in ? ss->in_ep : ss->out_ep;
604 qlen = ss->bulk_qlen;
608 for (i = 0; i < qlen; i++) {
609 req = ss_alloc_ep_req(ep, size);
613 req->complete = source_sink_complete;
615 reinit_write_data(ep, req);
616 else if (ss->pattern != 2)
617 memset(req->buf, 0x55, req->length);
619 status = usb_ep_queue(ep, req, GFP_ATOMIC);
621 struct usb_composite_dev *cdev;
623 cdev = ss->function.config->cdev;
624 ERROR(cdev, "start %s%s %s --> %d\n",
625 is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
627 free_ep_req(ep, req);
635 static void disable_source_sink(struct f_sourcesink *ss)
637 struct usb_composite_dev *cdev;
639 cdev = ss->function.config->cdev;
640 disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
642 VDBG(cdev, "%s disabled\n", ss->function.name);
646 enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
650 int speed = cdev->gadget->speed;
653 /* one bulk endpoint writes (sources) zeroes IN (to the host) */
655 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
658 result = usb_ep_enable(ep);
661 ep->driver_data = ss;
663 result = source_sink_start_ep(ss, true, false, speed);
671 /* one bulk endpoint reads (sinks) anything OUT (from 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, false, false, speed);
692 /* one iso endpoint writes (sources) zeroes IN (to the host) */
695 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
698 result = usb_ep_enable(ep);
701 ep->driver_data = ss;
703 result = source_sink_start_ep(ss, true, true, speed);
713 /* one iso endpoint reads (sinks) anything OUT (from the host) */
716 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
719 result = usb_ep_enable(ep);
722 ep->driver_data = ss;
724 result = source_sink_start_ep(ss, false, true, speed);
733 DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
737 static int sourcesink_set_alt(struct usb_function *f,
738 unsigned intf, unsigned alt)
740 struct f_sourcesink *ss = func_to_ss(f);
741 struct usb_composite_dev *cdev = f->config->cdev;
743 disable_source_sink(ss);
744 return enable_source_sink(cdev, ss, alt);
747 static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
749 struct f_sourcesink *ss = func_to_ss(f);
754 static void sourcesink_disable(struct usb_function *f)
756 struct f_sourcesink *ss = func_to_ss(f);
758 disable_source_sink(ss);
761 /*-------------------------------------------------------------------------*/
763 static int sourcesink_setup(struct usb_function *f,
764 const struct usb_ctrlrequest *ctrl)
766 struct usb_configuration *c = f->config;
767 struct usb_request *req = c->cdev->req;
768 int value = -EOPNOTSUPP;
769 u16 w_index = le16_to_cpu(ctrl->wIndex);
770 u16 w_value = le16_to_cpu(ctrl->wValue);
771 u16 w_length = le16_to_cpu(ctrl->wLength);
773 req->length = USB_COMP_EP0_BUFSIZ;
775 /* composite driver infrastructure handles everything except
776 * the two control test requests.
778 switch (ctrl->bRequest) {
781 * These are the same vendor-specific requests supported by
782 * Intel's USB 2.0 compliance test devices. We exceed that
783 * device spec by allowing multiple-packet requests.
785 * NOTE: the Control-OUT data stays in req->buf ... better
786 * would be copying it into a scratch buffer, so that other
787 * requests may safely intervene.
789 case 0x5b: /* control WRITE test -- fill the buffer */
790 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
792 if (w_value || w_index)
794 /* just read that many bytes into the buffer */
795 if (w_length > req->length)
799 case 0x5c: /* control READ test -- return the buffer */
800 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
802 if (w_value || w_index)
804 /* expect those bytes are still in the buffer; send back */
805 if (w_length > req->length)
813 "unknown control req%02x.%02x v%04x i%04x l%d\n",
814 ctrl->bRequestType, ctrl->bRequest,
815 w_value, w_index, w_length);
818 /* respond with data transfer or status phase? */
820 VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
821 ctrl->bRequestType, ctrl->bRequest,
822 w_value, w_index, w_length);
825 value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
827 ERROR(c->cdev, "source/sink response, err %d\n",
831 /* device either stalls (value < 0) or reports success */
835 static struct usb_function *source_sink_alloc_func(
836 struct usb_function_instance *fi)
838 struct f_sourcesink *ss;
839 struct f_ss_opts *ss_opts;
841 ss = kzalloc(sizeof(*ss), GFP_KERNEL);
843 return ERR_PTR(-ENOMEM);
845 ss_opts = container_of(fi, struct f_ss_opts, func_inst);
847 mutex_lock(&ss_opts->lock);
849 mutex_unlock(&ss_opts->lock);
851 ss->pattern = ss_opts->pattern;
852 ss->isoc_interval = ss_opts->isoc_interval;
853 ss->isoc_maxpacket = ss_opts->isoc_maxpacket;
854 ss->isoc_mult = ss_opts->isoc_mult;
855 ss->isoc_maxburst = ss_opts->isoc_maxburst;
856 ss->buflen = ss_opts->bulk_buflen;
857 ss->bulk_qlen = ss_opts->bulk_qlen;
858 ss->iso_qlen = ss_opts->iso_qlen;
860 ss->function.name = "source/sink";
861 ss->function.bind = sourcesink_bind;
862 ss->function.set_alt = sourcesink_set_alt;
863 ss->function.get_alt = sourcesink_get_alt;
864 ss->function.disable = sourcesink_disable;
865 ss->function.setup = sourcesink_setup;
866 ss->function.strings = sourcesink_strings;
868 ss->function.free_func = sourcesink_free_func;
870 return &ss->function;
873 static inline struct f_ss_opts *to_f_ss_opts(struct config_item *item)
875 return container_of(to_config_group(item), struct f_ss_opts,
879 static void ss_attr_release(struct config_item *item)
881 struct f_ss_opts *ss_opts = to_f_ss_opts(item);
883 usb_put_function_instance(&ss_opts->func_inst);
886 static struct configfs_item_operations ss_item_ops = {
887 .release = ss_attr_release,
890 static ssize_t f_ss_opts_pattern_show(struct config_item *item, char *page)
892 struct f_ss_opts *opts = to_f_ss_opts(item);
895 mutex_lock(&opts->lock);
896 result = sprintf(page, "%u\n", opts->pattern);
897 mutex_unlock(&opts->lock);
902 static ssize_t f_ss_opts_pattern_store(struct config_item *item,
903 const char *page, size_t len)
905 struct f_ss_opts *opts = to_f_ss_opts(item);
909 mutex_lock(&opts->lock);
915 ret = kstrtou8(page, 0, &num);
919 if (num != 0 && num != 1 && num != 2) {
927 mutex_unlock(&opts->lock);
931 CONFIGFS_ATTR(f_ss_opts_, pattern);
933 static ssize_t f_ss_opts_isoc_interval_show(struct config_item *item, char *page)
935 struct f_ss_opts *opts = to_f_ss_opts(item);
938 mutex_lock(&opts->lock);
939 result = sprintf(page, "%u\n", opts->isoc_interval);
940 mutex_unlock(&opts->lock);
945 static ssize_t f_ss_opts_isoc_interval_store(struct config_item *item,
946 const char *page, size_t len)
948 struct f_ss_opts *opts = to_f_ss_opts(item);
952 mutex_lock(&opts->lock);
958 ret = kstrtou8(page, 0, &num);
967 opts->isoc_interval = num;
970 mutex_unlock(&opts->lock);
974 CONFIGFS_ATTR(f_ss_opts_, isoc_interval);
976 static ssize_t f_ss_opts_isoc_maxpacket_show(struct config_item *item, char *page)
978 struct f_ss_opts *opts = to_f_ss_opts(item);
981 mutex_lock(&opts->lock);
982 result = sprintf(page, "%u\n", opts->isoc_maxpacket);
983 mutex_unlock(&opts->lock);
988 static ssize_t f_ss_opts_isoc_maxpacket_store(struct config_item *item,
989 const char *page, size_t len)
991 struct f_ss_opts *opts = to_f_ss_opts(item);
995 mutex_lock(&opts->lock);
1001 ret = kstrtou16(page, 0, &num);
1010 opts->isoc_maxpacket = num;
1013 mutex_unlock(&opts->lock);
1017 CONFIGFS_ATTR(f_ss_opts_, isoc_maxpacket);
1019 static ssize_t f_ss_opts_isoc_mult_show(struct config_item *item, char *page)
1021 struct f_ss_opts *opts = to_f_ss_opts(item);
1024 mutex_lock(&opts->lock);
1025 result = sprintf(page, "%u\n", opts->isoc_mult);
1026 mutex_unlock(&opts->lock);
1031 static ssize_t f_ss_opts_isoc_mult_store(struct config_item *item,
1032 const char *page, size_t len)
1034 struct f_ss_opts *opts = to_f_ss_opts(item);
1038 mutex_lock(&opts->lock);
1044 ret = kstrtou8(page, 0, &num);
1053 opts->isoc_mult = num;
1056 mutex_unlock(&opts->lock);
1060 CONFIGFS_ATTR(f_ss_opts_, isoc_mult);
1062 static ssize_t f_ss_opts_isoc_maxburst_show(struct config_item *item, char *page)
1064 struct f_ss_opts *opts = to_f_ss_opts(item);
1067 mutex_lock(&opts->lock);
1068 result = sprintf(page, "%u\n", opts->isoc_maxburst);
1069 mutex_unlock(&opts->lock);
1074 static ssize_t f_ss_opts_isoc_maxburst_store(struct config_item *item,
1075 const char *page, size_t len)
1077 struct f_ss_opts *opts = to_f_ss_opts(item);
1081 mutex_lock(&opts->lock);
1087 ret = kstrtou8(page, 0, &num);
1096 opts->isoc_maxburst = num;
1099 mutex_unlock(&opts->lock);
1103 CONFIGFS_ATTR(f_ss_opts_, isoc_maxburst);
1105 static ssize_t f_ss_opts_bulk_buflen_show(struct config_item *item, char *page)
1107 struct f_ss_opts *opts = to_f_ss_opts(item);
1110 mutex_lock(&opts->lock);
1111 result = sprintf(page, "%u\n", opts->bulk_buflen);
1112 mutex_unlock(&opts->lock);
1117 static ssize_t f_ss_opts_bulk_buflen_store(struct config_item *item,
1118 const char *page, size_t len)
1120 struct f_ss_opts *opts = to_f_ss_opts(item);
1124 mutex_lock(&opts->lock);
1130 ret = kstrtou32(page, 0, &num);
1134 opts->bulk_buflen = num;
1137 mutex_unlock(&opts->lock);
1141 CONFIGFS_ATTR(f_ss_opts_, bulk_buflen);
1143 static ssize_t f_ss_opts_bulk_qlen_show(struct config_item *item, char *page)
1145 struct f_ss_opts *opts = to_f_ss_opts(item);
1148 mutex_lock(&opts->lock);
1149 result = sprintf(page, "%u\n", opts->bulk_qlen);
1150 mutex_unlock(&opts->lock);
1155 static ssize_t f_ss_opts_bulk_qlen_store(struct config_item *item,
1156 const char *page, size_t len)
1158 struct f_ss_opts *opts = to_f_ss_opts(item);
1162 mutex_lock(&opts->lock);
1168 ret = kstrtou32(page, 0, &num);
1172 opts->bulk_qlen = num;
1175 mutex_unlock(&opts->lock);
1179 CONFIGFS_ATTR(f_ss_opts_, bulk_qlen);
1181 static ssize_t f_ss_opts_iso_qlen_show(struct config_item *item, char *page)
1183 struct f_ss_opts *opts = to_f_ss_opts(item);
1186 mutex_lock(&opts->lock);
1187 result = sprintf(page, "%u\n", opts->iso_qlen);
1188 mutex_unlock(&opts->lock);
1193 static ssize_t f_ss_opts_iso_qlen_store(struct config_item *item,
1194 const char *page, size_t len)
1196 struct f_ss_opts *opts = to_f_ss_opts(item);
1200 mutex_lock(&opts->lock);
1206 ret = kstrtou32(page, 0, &num);
1210 opts->iso_qlen = num;
1213 mutex_unlock(&opts->lock);
1217 CONFIGFS_ATTR(f_ss_opts_, iso_qlen);
1219 static struct configfs_attribute *ss_attrs[] = {
1220 &f_ss_opts_attr_pattern,
1221 &f_ss_opts_attr_isoc_interval,
1222 &f_ss_opts_attr_isoc_maxpacket,
1223 &f_ss_opts_attr_isoc_mult,
1224 &f_ss_opts_attr_isoc_maxburst,
1225 &f_ss_opts_attr_bulk_buflen,
1226 &f_ss_opts_attr_bulk_qlen,
1227 &f_ss_opts_attr_iso_qlen,
1231 static const struct config_item_type ss_func_type = {
1232 .ct_item_ops = &ss_item_ops,
1233 .ct_attrs = ss_attrs,
1234 .ct_owner = THIS_MODULE,
1237 static void source_sink_free_instance(struct usb_function_instance *fi)
1239 struct f_ss_opts *ss_opts;
1241 ss_opts = container_of(fi, struct f_ss_opts, func_inst);
1245 static struct usb_function_instance *source_sink_alloc_inst(void)
1247 struct f_ss_opts *ss_opts;
1249 ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL);
1251 return ERR_PTR(-ENOMEM);
1252 mutex_init(&ss_opts->lock);
1253 ss_opts->func_inst.free_func_inst = source_sink_free_instance;
1254 ss_opts->isoc_interval = GZERO_ISOC_INTERVAL;
1255 ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET;
1256 ss_opts->bulk_buflen = GZERO_BULK_BUFLEN;
1257 ss_opts->bulk_qlen = GZERO_SS_BULK_QLEN;
1258 ss_opts->iso_qlen = GZERO_SS_ISO_QLEN;
1260 config_group_init_type_name(&ss_opts->func_inst.group, "",
1263 return &ss_opts->func_inst;
1265 DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst,
1266 source_sink_alloc_func);
1268 static int __init sslb_modinit(void)
1272 ret = usb_function_register(&SourceSinkusb_func);
1277 usb_function_unregister(&SourceSinkusb_func);
1280 static void __exit sslb_modexit(void)
1282 usb_function_unregister(&SourceSinkusb_func);
1285 module_init(sslb_modinit);
1286 module_exit(sslb_modexit);
1288 MODULE_LICENSE("GPL");