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.
39 struct usb_function function;
42 struct usb_ep *out_ep;
43 struct usb_ep *iso_in_ep;
44 struct usb_ep *iso_out_ep;
48 unsigned isoc_interval;
49 unsigned isoc_maxpacket;
51 unsigned isoc_maxburst;
57 static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
59 return container_of(f, struct f_sourcesink, function);
62 /*-------------------------------------------------------------------------*/
64 static struct usb_interface_descriptor source_sink_intf_alt0 = {
65 .bLength = USB_DT_INTERFACE_SIZE,
66 .bDescriptorType = USB_DT_INTERFACE,
68 .bAlternateSetting = 0,
70 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
71 /* .iInterface = DYNAMIC */
74 static struct usb_interface_descriptor source_sink_intf_alt1 = {
75 .bLength = USB_DT_INTERFACE_SIZE,
76 .bDescriptorType = USB_DT_INTERFACE,
78 .bAlternateSetting = 1,
80 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
81 /* .iInterface = DYNAMIC */
84 /* full speed support: */
86 static struct usb_endpoint_descriptor fs_source_desc = {
87 .bLength = USB_DT_ENDPOINT_SIZE,
88 .bDescriptorType = USB_DT_ENDPOINT,
90 .bEndpointAddress = USB_DIR_IN,
91 .bmAttributes = USB_ENDPOINT_XFER_BULK,
94 static struct usb_endpoint_descriptor fs_sink_desc = {
95 .bLength = USB_DT_ENDPOINT_SIZE,
96 .bDescriptorType = USB_DT_ENDPOINT,
98 .bEndpointAddress = USB_DIR_OUT,
99 .bmAttributes = USB_ENDPOINT_XFER_BULK,
102 static struct usb_endpoint_descriptor fs_iso_source_desc = {
103 .bLength = USB_DT_ENDPOINT_SIZE,
104 .bDescriptorType = USB_DT_ENDPOINT,
106 .bEndpointAddress = USB_DIR_IN,
107 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
108 .wMaxPacketSize = cpu_to_le16(1023),
112 static struct usb_endpoint_descriptor fs_iso_sink_desc = {
113 .bLength = USB_DT_ENDPOINT_SIZE,
114 .bDescriptorType = USB_DT_ENDPOINT,
116 .bEndpointAddress = USB_DIR_OUT,
117 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
118 .wMaxPacketSize = cpu_to_le16(1023),
122 static struct usb_descriptor_header *fs_source_sink_descs[] = {
123 (struct usb_descriptor_header *) &source_sink_intf_alt0,
124 (struct usb_descriptor_header *) &fs_sink_desc,
125 (struct usb_descriptor_header *) &fs_source_desc,
126 (struct usb_descriptor_header *) &source_sink_intf_alt1,
127 #define FS_ALT_IFC_1_OFFSET 3
128 (struct usb_descriptor_header *) &fs_sink_desc,
129 (struct usb_descriptor_header *) &fs_source_desc,
130 (struct usb_descriptor_header *) &fs_iso_sink_desc,
131 (struct usb_descriptor_header *) &fs_iso_source_desc,
135 /* high speed support: */
137 static struct usb_endpoint_descriptor hs_source_desc = {
138 .bLength = USB_DT_ENDPOINT_SIZE,
139 .bDescriptorType = USB_DT_ENDPOINT,
141 .bmAttributes = USB_ENDPOINT_XFER_BULK,
142 .wMaxPacketSize = cpu_to_le16(512),
145 static struct usb_endpoint_descriptor hs_sink_desc = {
146 .bLength = USB_DT_ENDPOINT_SIZE,
147 .bDescriptorType = USB_DT_ENDPOINT,
149 .bmAttributes = USB_ENDPOINT_XFER_BULK,
150 .wMaxPacketSize = cpu_to_le16(512),
153 static struct usb_endpoint_descriptor hs_iso_source_desc = {
154 .bLength = USB_DT_ENDPOINT_SIZE,
155 .bDescriptorType = USB_DT_ENDPOINT,
157 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
158 .wMaxPacketSize = cpu_to_le16(1024),
162 static struct usb_endpoint_descriptor hs_iso_sink_desc = {
163 .bLength = USB_DT_ENDPOINT_SIZE,
164 .bDescriptorType = USB_DT_ENDPOINT,
166 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
167 .wMaxPacketSize = cpu_to_le16(1024),
171 static struct usb_descriptor_header *hs_source_sink_descs[] = {
172 (struct usb_descriptor_header *) &source_sink_intf_alt0,
173 (struct usb_descriptor_header *) &hs_source_desc,
174 (struct usb_descriptor_header *) &hs_sink_desc,
175 (struct usb_descriptor_header *) &source_sink_intf_alt1,
176 #define HS_ALT_IFC_1_OFFSET 3
177 (struct usb_descriptor_header *) &hs_source_desc,
178 (struct usb_descriptor_header *) &hs_sink_desc,
179 (struct usb_descriptor_header *) &hs_iso_source_desc,
180 (struct usb_descriptor_header *) &hs_iso_sink_desc,
184 /* super speed support: */
186 static struct usb_endpoint_descriptor ss_source_desc = {
187 .bLength = USB_DT_ENDPOINT_SIZE,
188 .bDescriptorType = USB_DT_ENDPOINT,
190 .bmAttributes = USB_ENDPOINT_XFER_BULK,
191 .wMaxPacketSize = cpu_to_le16(1024),
194 static struct usb_ss_ep_comp_descriptor ss_source_comp_desc = {
195 .bLength = USB_DT_SS_EP_COMP_SIZE,
196 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
200 .wBytesPerInterval = 0,
203 static struct usb_endpoint_descriptor ss_sink_desc = {
204 .bLength = USB_DT_ENDPOINT_SIZE,
205 .bDescriptorType = USB_DT_ENDPOINT,
207 .bmAttributes = USB_ENDPOINT_XFER_BULK,
208 .wMaxPacketSize = cpu_to_le16(1024),
211 static struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = {
212 .bLength = USB_DT_SS_EP_COMP_SIZE,
213 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
217 .wBytesPerInterval = 0,
220 static struct usb_endpoint_descriptor ss_iso_source_desc = {
221 .bLength = USB_DT_ENDPOINT_SIZE,
222 .bDescriptorType = USB_DT_ENDPOINT,
224 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
225 .wMaxPacketSize = cpu_to_le16(1024),
229 static struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc = {
230 .bLength = USB_DT_SS_EP_COMP_SIZE,
231 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
235 .wBytesPerInterval = cpu_to_le16(1024),
238 static struct usb_endpoint_descriptor ss_iso_sink_desc = {
239 .bLength = USB_DT_ENDPOINT_SIZE,
240 .bDescriptorType = USB_DT_ENDPOINT,
242 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
243 .wMaxPacketSize = cpu_to_le16(1024),
247 static struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = {
248 .bLength = USB_DT_SS_EP_COMP_SIZE,
249 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
253 .wBytesPerInterval = cpu_to_le16(1024),
256 static struct usb_descriptor_header *ss_source_sink_descs[] = {
257 (struct usb_descriptor_header *) &source_sink_intf_alt0,
258 (struct usb_descriptor_header *) &ss_source_desc,
259 (struct usb_descriptor_header *) &ss_source_comp_desc,
260 (struct usb_descriptor_header *) &ss_sink_desc,
261 (struct usb_descriptor_header *) &ss_sink_comp_desc,
262 (struct usb_descriptor_header *) &source_sink_intf_alt1,
263 #define SS_ALT_IFC_1_OFFSET 5
264 (struct usb_descriptor_header *) &ss_source_desc,
265 (struct usb_descriptor_header *) &ss_source_comp_desc,
266 (struct usb_descriptor_header *) &ss_sink_desc,
267 (struct usb_descriptor_header *) &ss_sink_comp_desc,
268 (struct usb_descriptor_header *) &ss_iso_source_desc,
269 (struct usb_descriptor_header *) &ss_iso_source_comp_desc,
270 (struct usb_descriptor_header *) &ss_iso_sink_desc,
271 (struct usb_descriptor_header *) &ss_iso_sink_comp_desc,
275 /* function-specific strings: */
277 static struct usb_string strings_sourcesink[] = {
278 [0].s = "source and sink data",
279 { } /* end of list */
282 static struct usb_gadget_strings stringtab_sourcesink = {
283 .language = 0x0409, /* en-us */
284 .strings = strings_sourcesink,
287 static struct usb_gadget_strings *sourcesink_strings[] = {
288 &stringtab_sourcesink,
292 /*-------------------------------------------------------------------------*/
294 static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len)
296 return alloc_ep_req(ep, len);
299 static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
303 value = usb_ep_disable(ep);
305 DBG(cdev, "disable %s --> %d\n", ep->name, value);
308 void disable_endpoints(struct usb_composite_dev *cdev,
309 struct usb_ep *in, struct usb_ep *out,
310 struct usb_ep *iso_in, struct usb_ep *iso_out)
312 disable_ep(cdev, in);
313 disable_ep(cdev, out);
315 disable_ep(cdev, iso_in);
317 disable_ep(cdev, iso_out);
321 sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
323 struct usb_composite_dev *cdev = c->cdev;
324 struct f_sourcesink *ss = func_to_ss(f);
328 /* allocate interface ID(s) */
329 id = usb_interface_id(c, f);
332 source_sink_intf_alt0.bInterfaceNumber = id;
333 source_sink_intf_alt1.bInterfaceNumber = id;
335 /* allocate bulk endpoints */
336 ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
339 ERROR(cdev, "%s: can't autoconfigure on %s\n",
340 f->name, cdev->gadget->name);
344 ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
348 /* sanity check the isoc module parameters */
349 if (ss->isoc_interval < 1)
350 ss->isoc_interval = 1;
351 if (ss->isoc_interval > 16)
352 ss->isoc_interval = 16;
353 if (ss->isoc_mult > 2)
355 if (ss->isoc_maxburst > 15)
356 ss->isoc_maxburst = 15;
358 /* fill in the FS isoc descriptors from the module parameters */
359 fs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket > 1023 ?
360 1023 : ss->isoc_maxpacket;
361 fs_iso_source_desc.bInterval = ss->isoc_interval;
362 fs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket > 1023 ?
363 1023 : ss->isoc_maxpacket;
364 fs_iso_sink_desc.bInterval = ss->isoc_interval;
366 /* allocate iso endpoints */
367 ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
371 ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
372 if (!ss->iso_out_ep) {
373 usb_ep_autoconfig_release(ss->iso_in_ep);
374 ss->iso_in_ep = NULL;
377 * We still want to work even if the UDC doesn't have isoc
378 * endpoints, so null out the alt interface that contains
381 fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
382 hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
383 ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
386 if (ss->isoc_maxpacket > 1024)
387 ss->isoc_maxpacket = 1024;
389 /* support high speed hardware */
390 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
391 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
394 * Fill in the HS isoc descriptors from the module parameters.
395 * We assume that the user knows what they are doing and won't
396 * give parameters that their UDC doesn't support.
398 hs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket;
399 hs_iso_source_desc.wMaxPacketSize |= ss->isoc_mult << 11;
400 hs_iso_source_desc.bInterval = ss->isoc_interval;
401 hs_iso_source_desc.bEndpointAddress =
402 fs_iso_source_desc.bEndpointAddress;
404 hs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket;
405 hs_iso_sink_desc.wMaxPacketSize |= ss->isoc_mult << 11;
406 hs_iso_sink_desc.bInterval = ss->isoc_interval;
407 hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
409 /* support super speed hardware */
410 ss_source_desc.bEndpointAddress =
411 fs_source_desc.bEndpointAddress;
412 ss_sink_desc.bEndpointAddress =
413 fs_sink_desc.bEndpointAddress;
416 * Fill in the SS isoc descriptors from the module parameters.
417 * We assume that the user knows what they are doing and won't
418 * give parameters that their UDC doesn't support.
420 ss_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket;
421 ss_iso_source_desc.bInterval = ss->isoc_interval;
422 ss_iso_source_comp_desc.bmAttributes = ss->isoc_mult;
423 ss_iso_source_comp_desc.bMaxBurst = ss->isoc_maxburst;
424 ss_iso_source_comp_desc.wBytesPerInterval = ss->isoc_maxpacket *
425 (ss->isoc_mult + 1) * (ss->isoc_maxburst + 1);
426 ss_iso_source_desc.bEndpointAddress =
427 fs_iso_source_desc.bEndpointAddress;
429 ss_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket;
430 ss_iso_sink_desc.bInterval = ss->isoc_interval;
431 ss_iso_sink_comp_desc.bmAttributes = ss->isoc_mult;
432 ss_iso_sink_comp_desc.bMaxBurst = ss->isoc_maxburst;
433 ss_iso_sink_comp_desc.wBytesPerInterval = ss->isoc_maxpacket *
434 (ss->isoc_mult + 1) * (ss->isoc_maxburst + 1);
435 ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
437 ret = usb_assign_descriptors(f, fs_source_sink_descs,
438 hs_source_sink_descs, ss_source_sink_descs,
439 ss_source_sink_descs);
443 DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
444 (gadget_is_superspeed(c->cdev->gadget) ? "super" :
445 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
446 f->name, ss->in_ep->name, ss->out_ep->name,
447 ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
448 ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
453 sourcesink_free_func(struct usb_function *f)
455 struct f_ss_opts *opts;
457 opts = container_of(f->fi, struct f_ss_opts, func_inst);
459 mutex_lock(&opts->lock);
461 mutex_unlock(&opts->lock);
463 usb_free_all_descriptors(f);
464 kfree(func_to_ss(f));
467 /* optionally require specific source/sink data patterns */
468 static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
472 struct usb_composite_dev *cdev = ss->function.config->cdev;
473 int max_packet_size = le16_to_cpu(ss->out_ep->desc->wMaxPacketSize);
475 if (ss->pattern == 2)
478 for (i = 0; i < req->actual; i++, buf++) {
479 switch (ss->pattern) {
481 /* all-zeroes has no synchronization issues */
487 /* "mod63" stays in sync with short-terminated transfers,
488 * OR otherwise when host and gadget agree on how large
489 * each usb transfer request should be. Resync is done
490 * with set_interface or set_config. (We *WANT* it to
491 * get quickly out of sync if controllers or their drivers
492 * stutter for any reason, including buffer duplication...)
495 if (*buf == (u8)((i % max_packet_size) % 63))
499 ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
500 usb_ep_set_halt(ss->out_ep);
506 static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
510 int max_packet_size = le16_to_cpu(ep->desc->wMaxPacketSize);
511 struct f_sourcesink *ss = ep->driver_data;
513 switch (ss->pattern) {
515 memset(req->buf, 0, req->length);
518 for (i = 0; i < req->length; i++)
519 *buf++ = (u8) ((i % max_packet_size) % 63);
526 static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
528 struct usb_composite_dev *cdev;
529 struct f_sourcesink *ss = ep->driver_data;
530 int status = req->status;
532 /* driver_data will be null if ep has been disabled */
536 cdev = ss->function.config->cdev;
540 case 0: /* normal completion? */
541 if (ep == ss->out_ep) {
542 check_read_data(ss, req);
543 if (ss->pattern != 2)
544 memset(req->buf, 0x55, req->length);
548 /* this endpoint is normally active while we're configured */
549 case -ECONNABORTED: /* hardware forced ep reset */
550 case -ECONNRESET: /* request dequeued */
551 case -ESHUTDOWN: /* disconnect from host */
552 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
553 req->actual, req->length);
554 if (ep == ss->out_ep)
555 check_read_data(ss, req);
556 free_ep_req(ep, req);
559 case -EOVERFLOW: /* buffer overrun on read means that
560 * we didn't provide a big enough
565 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
566 status, req->actual, req->length);
568 case -EREMOTEIO: /* short read */
572 status = usb_ep_queue(ep, req, GFP_ATOMIC);
574 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
575 ep->name, req->length, status);
577 /* FIXME recover later ... somehow */
581 static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
582 bool is_iso, int speed)
585 struct usb_request *req;
586 int i, size, qlen, status = 0;
590 case USB_SPEED_SUPER_PLUS:
591 case USB_SPEED_SUPER:
592 size = ss->isoc_maxpacket *
593 (ss->isoc_mult + 1) *
594 (ss->isoc_maxburst + 1);
597 size = ss->isoc_maxpacket * (ss->isoc_mult + 1);
600 size = ss->isoc_maxpacket > 1023 ?
601 1023 : ss->isoc_maxpacket;
604 ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
607 ep = is_in ? ss->in_ep : ss->out_ep;
608 qlen = ss->bulk_qlen;
612 for (i = 0; i < qlen; i++) {
613 req = ss_alloc_ep_req(ep, size);
617 req->complete = source_sink_complete;
619 reinit_write_data(ep, req);
620 else if (ss->pattern != 2)
621 memset(req->buf, 0x55, req->length);
623 status = usb_ep_queue(ep, req, GFP_ATOMIC);
625 struct usb_composite_dev *cdev;
627 cdev = ss->function.config->cdev;
628 ERROR(cdev, "start %s%s %s --> %d\n",
629 is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
631 free_ep_req(ep, req);
639 static void disable_source_sink(struct f_sourcesink *ss)
641 struct usb_composite_dev *cdev;
643 cdev = ss->function.config->cdev;
644 disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
646 VDBG(cdev, "%s disabled\n", ss->function.name);
650 enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
654 int speed = cdev->gadget->speed;
657 /* one bulk endpoint writes (sources) zeroes IN (to the host) */
659 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
662 result = usb_ep_enable(ep);
665 ep->driver_data = ss;
667 result = source_sink_start_ep(ss, true, false, speed);
675 /* one bulk endpoint reads (sinks) anything OUT (from the host) */
677 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
680 result = usb_ep_enable(ep);
683 ep->driver_data = ss;
685 result = source_sink_start_ep(ss, false, false, speed);
696 /* one iso endpoint writes (sources) zeroes IN (to the host) */
699 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
702 result = usb_ep_enable(ep);
705 ep->driver_data = ss;
707 result = source_sink_start_ep(ss, true, true, speed);
717 /* one iso endpoint reads (sinks) anything OUT (from the host) */
720 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
723 result = usb_ep_enable(ep);
726 ep->driver_data = ss;
728 result = source_sink_start_ep(ss, false, true, speed);
737 DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
741 static int sourcesink_set_alt(struct usb_function *f,
742 unsigned intf, unsigned alt)
744 struct f_sourcesink *ss = func_to_ss(f);
745 struct usb_composite_dev *cdev = f->config->cdev;
747 disable_source_sink(ss);
748 return enable_source_sink(cdev, ss, alt);
751 static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
753 struct f_sourcesink *ss = func_to_ss(f);
758 static void sourcesink_disable(struct usb_function *f)
760 struct f_sourcesink *ss = func_to_ss(f);
762 disable_source_sink(ss);
765 /*-------------------------------------------------------------------------*/
767 static int sourcesink_setup(struct usb_function *f,
768 const struct usb_ctrlrequest *ctrl)
770 struct usb_configuration *c = f->config;
771 struct usb_request *req = c->cdev->req;
772 int value = -EOPNOTSUPP;
773 u16 w_index = le16_to_cpu(ctrl->wIndex);
774 u16 w_value = le16_to_cpu(ctrl->wValue);
775 u16 w_length = le16_to_cpu(ctrl->wLength);
777 req->length = USB_COMP_EP0_BUFSIZ;
779 /* composite driver infrastructure handles everything except
780 * the two control test requests.
782 switch (ctrl->bRequest) {
785 * These are the same vendor-specific requests supported by
786 * Intel's USB 2.0 compliance test devices. We exceed that
787 * device spec by allowing multiple-packet requests.
789 * NOTE: the Control-OUT data stays in req->buf ... better
790 * would be copying it into a scratch buffer, so that other
791 * requests may safely intervene.
793 case 0x5b: /* control WRITE test -- fill the buffer */
794 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
796 if (w_value || w_index)
798 /* just read that many bytes into the buffer */
799 if (w_length > req->length)
803 case 0x5c: /* control READ test -- return the buffer */
804 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
806 if (w_value || w_index)
808 /* expect those bytes are still in the buffer; send back */
809 if (w_length > req->length)
817 "unknown control req%02x.%02x v%04x i%04x l%d\n",
818 ctrl->bRequestType, ctrl->bRequest,
819 w_value, w_index, w_length);
822 /* respond with data transfer or status phase? */
824 VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
825 ctrl->bRequestType, ctrl->bRequest,
826 w_value, w_index, w_length);
829 value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
831 ERROR(c->cdev, "source/sink response, err %d\n",
835 /* device either stalls (value < 0) or reports success */
839 static struct usb_function *source_sink_alloc_func(
840 struct usb_function_instance *fi)
842 struct f_sourcesink *ss;
843 struct f_ss_opts *ss_opts;
845 ss = kzalloc(sizeof(*ss), GFP_KERNEL);
847 return ERR_PTR(-ENOMEM);
849 ss_opts = container_of(fi, struct f_ss_opts, func_inst);
851 mutex_lock(&ss_opts->lock);
853 mutex_unlock(&ss_opts->lock);
855 ss->pattern = ss_opts->pattern;
856 ss->isoc_interval = ss_opts->isoc_interval;
857 ss->isoc_maxpacket = ss_opts->isoc_maxpacket;
858 ss->isoc_mult = ss_opts->isoc_mult;
859 ss->isoc_maxburst = ss_opts->isoc_maxburst;
860 ss->buflen = ss_opts->bulk_buflen;
861 ss->bulk_qlen = ss_opts->bulk_qlen;
862 ss->iso_qlen = ss_opts->iso_qlen;
864 ss->function.name = "source/sink";
865 ss->function.bind = sourcesink_bind;
866 ss->function.set_alt = sourcesink_set_alt;
867 ss->function.get_alt = sourcesink_get_alt;
868 ss->function.disable = sourcesink_disable;
869 ss->function.setup = sourcesink_setup;
870 ss->function.strings = sourcesink_strings;
872 ss->function.free_func = sourcesink_free_func;
874 return &ss->function;
877 static inline struct f_ss_opts *to_f_ss_opts(struct config_item *item)
879 return container_of(to_config_group(item), struct f_ss_opts,
883 static void ss_attr_release(struct config_item *item)
885 struct f_ss_opts *ss_opts = to_f_ss_opts(item);
887 usb_put_function_instance(&ss_opts->func_inst);
890 static struct configfs_item_operations ss_item_ops = {
891 .release = ss_attr_release,
894 static ssize_t f_ss_opts_pattern_show(struct config_item *item, char *page)
896 struct f_ss_opts *opts = to_f_ss_opts(item);
899 mutex_lock(&opts->lock);
900 result = sprintf(page, "%u\n", opts->pattern);
901 mutex_unlock(&opts->lock);
906 static ssize_t f_ss_opts_pattern_store(struct config_item *item,
907 const char *page, size_t len)
909 struct f_ss_opts *opts = to_f_ss_opts(item);
913 mutex_lock(&opts->lock);
919 ret = kstrtou8(page, 0, &num);
923 if (num != 0 && num != 1 && num != 2) {
931 mutex_unlock(&opts->lock);
935 CONFIGFS_ATTR(f_ss_opts_, pattern);
937 static ssize_t f_ss_opts_isoc_interval_show(struct config_item *item, char *page)
939 struct f_ss_opts *opts = to_f_ss_opts(item);
942 mutex_lock(&opts->lock);
943 result = sprintf(page, "%u\n", opts->isoc_interval);
944 mutex_unlock(&opts->lock);
949 static ssize_t f_ss_opts_isoc_interval_store(struct config_item *item,
950 const char *page, size_t len)
952 struct f_ss_opts *opts = to_f_ss_opts(item);
956 mutex_lock(&opts->lock);
962 ret = kstrtou8(page, 0, &num);
971 opts->isoc_interval = num;
974 mutex_unlock(&opts->lock);
978 CONFIGFS_ATTR(f_ss_opts_, isoc_interval);
980 static ssize_t f_ss_opts_isoc_maxpacket_show(struct config_item *item, char *page)
982 struct f_ss_opts *opts = to_f_ss_opts(item);
985 mutex_lock(&opts->lock);
986 result = sprintf(page, "%u\n", opts->isoc_maxpacket);
987 mutex_unlock(&opts->lock);
992 static ssize_t f_ss_opts_isoc_maxpacket_store(struct config_item *item,
993 const char *page, size_t len)
995 struct f_ss_opts *opts = to_f_ss_opts(item);
999 mutex_lock(&opts->lock);
1005 ret = kstrtou16(page, 0, &num);
1014 opts->isoc_maxpacket = num;
1017 mutex_unlock(&opts->lock);
1021 CONFIGFS_ATTR(f_ss_opts_, isoc_maxpacket);
1023 static ssize_t f_ss_opts_isoc_mult_show(struct config_item *item, char *page)
1025 struct f_ss_opts *opts = to_f_ss_opts(item);
1028 mutex_lock(&opts->lock);
1029 result = sprintf(page, "%u\n", opts->isoc_mult);
1030 mutex_unlock(&opts->lock);
1035 static ssize_t f_ss_opts_isoc_mult_store(struct config_item *item,
1036 const char *page, size_t len)
1038 struct f_ss_opts *opts = to_f_ss_opts(item);
1042 mutex_lock(&opts->lock);
1048 ret = kstrtou8(page, 0, &num);
1057 opts->isoc_mult = num;
1060 mutex_unlock(&opts->lock);
1064 CONFIGFS_ATTR(f_ss_opts_, isoc_mult);
1066 static ssize_t f_ss_opts_isoc_maxburst_show(struct config_item *item, char *page)
1068 struct f_ss_opts *opts = to_f_ss_opts(item);
1071 mutex_lock(&opts->lock);
1072 result = sprintf(page, "%u\n", opts->isoc_maxburst);
1073 mutex_unlock(&opts->lock);
1078 static ssize_t f_ss_opts_isoc_maxburst_store(struct config_item *item,
1079 const char *page, size_t len)
1081 struct f_ss_opts *opts = to_f_ss_opts(item);
1085 mutex_lock(&opts->lock);
1091 ret = kstrtou8(page, 0, &num);
1100 opts->isoc_maxburst = num;
1103 mutex_unlock(&opts->lock);
1107 CONFIGFS_ATTR(f_ss_opts_, isoc_maxburst);
1109 static ssize_t f_ss_opts_bulk_buflen_show(struct config_item *item, char *page)
1111 struct f_ss_opts *opts = to_f_ss_opts(item);
1114 mutex_lock(&opts->lock);
1115 result = sprintf(page, "%u\n", opts->bulk_buflen);
1116 mutex_unlock(&opts->lock);
1121 static ssize_t f_ss_opts_bulk_buflen_store(struct config_item *item,
1122 const char *page, size_t len)
1124 struct f_ss_opts *opts = to_f_ss_opts(item);
1128 mutex_lock(&opts->lock);
1134 ret = kstrtou32(page, 0, &num);
1138 opts->bulk_buflen = num;
1141 mutex_unlock(&opts->lock);
1145 CONFIGFS_ATTR(f_ss_opts_, bulk_buflen);
1147 static ssize_t f_ss_opts_bulk_qlen_show(struct config_item *item, char *page)
1149 struct f_ss_opts *opts = to_f_ss_opts(item);
1152 mutex_lock(&opts->lock);
1153 result = sprintf(page, "%u\n", opts->bulk_qlen);
1154 mutex_unlock(&opts->lock);
1159 static ssize_t f_ss_opts_bulk_qlen_store(struct config_item *item,
1160 const char *page, size_t len)
1162 struct f_ss_opts *opts = to_f_ss_opts(item);
1166 mutex_lock(&opts->lock);
1172 ret = kstrtou32(page, 0, &num);
1176 opts->bulk_qlen = num;
1179 mutex_unlock(&opts->lock);
1183 CONFIGFS_ATTR(f_ss_opts_, bulk_qlen);
1185 static ssize_t f_ss_opts_iso_qlen_show(struct config_item *item, char *page)
1187 struct f_ss_opts *opts = to_f_ss_opts(item);
1190 mutex_lock(&opts->lock);
1191 result = sprintf(page, "%u\n", opts->iso_qlen);
1192 mutex_unlock(&opts->lock);
1197 static ssize_t f_ss_opts_iso_qlen_store(struct config_item *item,
1198 const char *page, size_t len)
1200 struct f_ss_opts *opts = to_f_ss_opts(item);
1204 mutex_lock(&opts->lock);
1210 ret = kstrtou32(page, 0, &num);
1214 opts->iso_qlen = num;
1217 mutex_unlock(&opts->lock);
1221 CONFIGFS_ATTR(f_ss_opts_, iso_qlen);
1223 static struct configfs_attribute *ss_attrs[] = {
1224 &f_ss_opts_attr_pattern,
1225 &f_ss_opts_attr_isoc_interval,
1226 &f_ss_opts_attr_isoc_maxpacket,
1227 &f_ss_opts_attr_isoc_mult,
1228 &f_ss_opts_attr_isoc_maxburst,
1229 &f_ss_opts_attr_bulk_buflen,
1230 &f_ss_opts_attr_bulk_qlen,
1231 &f_ss_opts_attr_iso_qlen,
1235 static struct config_item_type ss_func_type = {
1236 .ct_item_ops = &ss_item_ops,
1237 .ct_attrs = ss_attrs,
1238 .ct_owner = THIS_MODULE,
1241 static void source_sink_free_instance(struct usb_function_instance *fi)
1243 struct f_ss_opts *ss_opts;
1245 ss_opts = container_of(fi, struct f_ss_opts, func_inst);
1249 static struct usb_function_instance *source_sink_alloc_inst(void)
1251 struct f_ss_opts *ss_opts;
1253 ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL);
1255 return ERR_PTR(-ENOMEM);
1256 mutex_init(&ss_opts->lock);
1257 ss_opts->func_inst.free_func_inst = source_sink_free_instance;
1258 ss_opts->isoc_interval = GZERO_ISOC_INTERVAL;
1259 ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET;
1260 ss_opts->bulk_buflen = GZERO_BULK_BUFLEN;
1261 ss_opts->bulk_qlen = GZERO_SS_BULK_QLEN;
1262 ss_opts->iso_qlen = GZERO_SS_ISO_QLEN;
1264 config_group_init_type_name(&ss_opts->func_inst.group, "",
1267 return &ss_opts->func_inst;
1269 DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst,
1270 source_sink_alloc_func);
1272 static int __init sslb_modinit(void)
1276 ret = usb_function_register(&SourceSinkusb_func);
1281 usb_function_unregister(&SourceSinkusb_func);
1284 static void __exit sslb_modexit(void)
1286 usb_function_unregister(&SourceSinkusb_func);
1289 module_init(sslb_modinit);
1290 module_exit(sslb_modexit);
1292 MODULE_LICENSE("GPL");