Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * f_sourcesink.c - USB peripheral source/sink configuration driver
   4 *
   5 * Copyright (C) 2003-2008 David Brownell
   6 * Copyright (C) 2008 by Nokia Corporation
 
 
 
 
 
   7 */
   8
   9/* #define VERBOSE_DEBUG */
  10
  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>
  17
  18#include "g_zero.h"
  19#include "u_f.h"
  20
  21/*
  22 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
  23 * controller drivers.
  24 *
  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.
  28 *
  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.
  33 */
  34struct f_sourcesink {
  35	struct usb_function	function;
  36
  37	struct usb_ep		*in_ep;
  38	struct usb_ep		*out_ep;
  39	struct usb_ep		*iso_in_ep;
  40	struct usb_ep		*iso_out_ep;
  41	int			cur_alt;
  42
  43	unsigned pattern;
  44	unsigned isoc_interval;
  45	unsigned isoc_maxpacket;
  46	unsigned isoc_mult;
  47	unsigned isoc_maxburst;
  48	unsigned buflen;
  49	unsigned bulk_qlen;
  50	unsigned iso_qlen;
  51};
  52
  53static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
  54{
  55	return container_of(f, struct f_sourcesink, function);
  56}
  57
  58/*-------------------------------------------------------------------------*/
  59
  60static struct usb_interface_descriptor source_sink_intf_alt0 = {
  61	.bLength =		USB_DT_INTERFACE_SIZE,
  62	.bDescriptorType =	USB_DT_INTERFACE,
  63
  64	.bAlternateSetting =	0,
  65	.bNumEndpoints =	2,
  66	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
  67	/* .iInterface		= DYNAMIC */
  68};
  69
  70static struct usb_interface_descriptor source_sink_intf_alt1 = {
  71	.bLength =		USB_DT_INTERFACE_SIZE,
  72	.bDescriptorType =	USB_DT_INTERFACE,
  73
  74	.bAlternateSetting =	1,
  75	.bNumEndpoints =	4,
  76	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
  77	/* .iInterface		= DYNAMIC */
  78};
  79
  80/* full speed support: */
  81
  82static struct usb_endpoint_descriptor fs_source_desc = {
  83	.bLength =		USB_DT_ENDPOINT_SIZE,
  84	.bDescriptorType =	USB_DT_ENDPOINT,
  85
  86	.bEndpointAddress =	USB_DIR_IN,
  87	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
  88};
  89
  90static struct usb_endpoint_descriptor fs_sink_desc = {
  91	.bLength =		USB_DT_ENDPOINT_SIZE,
  92	.bDescriptorType =	USB_DT_ENDPOINT,
  93
  94	.bEndpointAddress =	USB_DIR_OUT,
  95	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
  96};
  97
  98static struct usb_endpoint_descriptor fs_iso_source_desc = {
  99	.bLength =		USB_DT_ENDPOINT_SIZE,
 100	.bDescriptorType =	USB_DT_ENDPOINT,
 101
 102	.bEndpointAddress =	USB_DIR_IN,
 103	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
 104	.wMaxPacketSize =	cpu_to_le16(1023),
 105	.bInterval =		4,
 106};
 107
 108static struct usb_endpoint_descriptor fs_iso_sink_desc = {
 109	.bLength =		USB_DT_ENDPOINT_SIZE,
 110	.bDescriptorType =	USB_DT_ENDPOINT,
 111
 112	.bEndpointAddress =	USB_DIR_OUT,
 113	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
 114	.wMaxPacketSize =	cpu_to_le16(1023),
 115	.bInterval =		4,
 116};
 117
 118static 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,
 128	NULL,
 129};
 130
 131/* high speed support: */
 132
 133static struct usb_endpoint_descriptor hs_source_desc = {
 134	.bLength =		USB_DT_ENDPOINT_SIZE,
 135	.bDescriptorType =	USB_DT_ENDPOINT,
 136
 137	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
 138	.wMaxPacketSize =	cpu_to_le16(512),
 139};
 140
 141static struct usb_endpoint_descriptor hs_sink_desc = {
 142	.bLength =		USB_DT_ENDPOINT_SIZE,
 143	.bDescriptorType =	USB_DT_ENDPOINT,
 144
 145	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
 146	.wMaxPacketSize =	cpu_to_le16(512),
 147};
 148
 149static struct usb_endpoint_descriptor hs_iso_source_desc = {
 150	.bLength =		USB_DT_ENDPOINT_SIZE,
 151	.bDescriptorType =	USB_DT_ENDPOINT,
 152
 153	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
 154	.wMaxPacketSize =	cpu_to_le16(1024),
 155	.bInterval =		4,
 156};
 157
 158static struct usb_endpoint_descriptor hs_iso_sink_desc = {
 159	.bLength =		USB_DT_ENDPOINT_SIZE,
 160	.bDescriptorType =	USB_DT_ENDPOINT,
 161
 162	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
 163	.wMaxPacketSize =	cpu_to_le16(1024),
 164	.bInterval =		4,
 165};
 166
 167static 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,
 177	NULL,
 178};
 179
 180/* super speed support: */
 181
 182static struct usb_endpoint_descriptor ss_source_desc = {
 183	.bLength =		USB_DT_ENDPOINT_SIZE,
 184	.bDescriptorType =	USB_DT_ENDPOINT,
 185
 186	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
 187	.wMaxPacketSize =	cpu_to_le16(1024),
 188};
 189
 190static 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,
 193
 194	.bMaxBurst =		0,
 195	.bmAttributes =		0,
 196	.wBytesPerInterval =	0,
 197};
 198
 199static struct usb_endpoint_descriptor ss_sink_desc = {
 200	.bLength =		USB_DT_ENDPOINT_SIZE,
 201	.bDescriptorType =	USB_DT_ENDPOINT,
 202
 203	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
 204	.wMaxPacketSize =	cpu_to_le16(1024),
 205};
 206
 207static 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,
 210
 211	.bMaxBurst =		0,
 212	.bmAttributes =		0,
 213	.wBytesPerInterval =	0,
 214};
 215
 216static struct usb_endpoint_descriptor ss_iso_source_desc = {
 217	.bLength =		USB_DT_ENDPOINT_SIZE,
 218	.bDescriptorType =	USB_DT_ENDPOINT,
 219
 220	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
 221	.wMaxPacketSize =	cpu_to_le16(1024),
 222	.bInterval =		4,
 223};
 224
 225static 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,
 228
 229	.bMaxBurst =		0,
 230	.bmAttributes =		0,
 231	.wBytesPerInterval =	cpu_to_le16(1024),
 232};
 233
 234static struct usb_endpoint_descriptor ss_iso_sink_desc = {
 235	.bLength =		USB_DT_ENDPOINT_SIZE,
 236	.bDescriptorType =	USB_DT_ENDPOINT,
 237
 238	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
 239	.wMaxPacketSize =	cpu_to_le16(1024),
 240	.bInterval =		4,
 241};
 242
 243static 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,
 246
 247	.bMaxBurst =		0,
 248	.bmAttributes =		0,
 249	.wBytesPerInterval =	cpu_to_le16(1024),
 250};
 251
 252static 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,
 268	NULL,
 269};
 270
 271/* function-specific strings: */
 272
 273static struct usb_string strings_sourcesink[] = {
 274	[0].s = "source and sink data",
 275	{  }			/* end of list */
 276};
 277
 278static struct usb_gadget_strings stringtab_sourcesink = {
 279	.language	= 0x0409,	/* en-us */
 280	.strings	= strings_sourcesink,
 281};
 282
 283static struct usb_gadget_strings *sourcesink_strings[] = {
 284	&stringtab_sourcesink,
 285	NULL,
 286};
 287
 288/*-------------------------------------------------------------------------*/
 289
 290static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len)
 291{
 292	return alloc_ep_req(ep, len);
 293}
 294
 295static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
 296{
 297	int			value;
 298
 299	value = usb_ep_disable(ep);
 300	if (value < 0)
 301		DBG(cdev, "disable %s --> %d\n", ep->name, value);
 302}
 303
 304void 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)
 307{
 308	disable_ep(cdev, in);
 309	disable_ep(cdev, out);
 310	if (iso_in)
 311		disable_ep(cdev, iso_in);
 312	if (iso_out)
 313		disable_ep(cdev, iso_out);
 314}
 315
 316static int
 317sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
 318{
 319	struct usb_composite_dev *cdev = c->cdev;
 320	struct f_sourcesink	*ss = func_to_ss(f);
 321	int	id;
 322	int ret;
 323
 324	/* allocate interface ID(s) */
 325	id = usb_interface_id(c, f);
 326	if (id < 0)
 327		return id;
 328	source_sink_intf_alt0.bInterfaceNumber = id;
 329	source_sink_intf_alt1.bInterfaceNumber = id;
 330
 331	/* allocate bulk endpoints */
 332	ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
 333	if (!ss->in_ep) {
 334autoconf_fail:
 335		ERROR(cdev, "%s: can't autoconfigure on %s\n",
 336			f->name, cdev->gadget->name);
 337		return -ENODEV;
 338	}
 339
 340	ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
 341	if (!ss->out_ep)
 342		goto autoconf_fail;
 343
 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)
 350		ss->isoc_mult = 2;
 351	if (ss->isoc_maxburst > 15)
 352		ss->isoc_maxburst = 15;
 353
 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;
 361
 362	/* allocate iso endpoints */
 363	ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
 364	if (!ss->iso_in_ep)
 365		goto no_iso;
 366
 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;
 371no_iso:
 372		/*
 373		 * We still want to work even if the UDC doesn't have isoc
 374		 * endpoints, so null out the alt interface that contains
 375		 * them and continue.
 376		 */
 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;
 380	}
 381
 382	if (ss->isoc_maxpacket > 1024)
 383		ss->isoc_maxpacket = 1024;
 384
 385	/* support high speed hardware */
 386	hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
 387	hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
 388
 389	/*
 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.
 393	 */
 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;
 399
 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;
 404
 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;
 410
 411	/*
 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.
 415	 */
 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;
 424
 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;
 432
 433	ret = usb_assign_descriptors(f, fs_source_sink_descs,
 434			hs_source_sink_descs, ss_source_sink_descs,
 435			ss_source_sink_descs);
 436	if (ret)
 437		return ret;
 438
 439	DBG(cdev, "%s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
 
 
 440			f->name, ss->in_ep->name, ss->out_ep->name,
 441			ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
 442			ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
 443	return 0;
 444}
 445
 446static void
 447sourcesink_free_func(struct usb_function *f)
 448{
 449	struct f_ss_opts *opts;
 450
 451	opts = container_of(f->fi, struct f_ss_opts, func_inst);
 452
 453	mutex_lock(&opts->lock);
 454	opts->refcnt--;
 455	mutex_unlock(&opts->lock);
 456
 457	usb_free_all_descriptors(f);
 458	kfree(func_to_ss(f));
 459}
 460
 461/* optionally require specific source/sink data patterns  */
 462static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
 463{
 464	unsigned		i;
 465	u8			*buf = req->buf;
 466	struct usb_composite_dev *cdev = ss->function.config->cdev;
 467	int max_packet_size = le16_to_cpu(ss->out_ep->desc->wMaxPacketSize);
 468
 469	if (ss->pattern == 2)
 470		return 0;
 471
 472	for (i = 0; i < req->actual; i++, buf++) {
 473		switch (ss->pattern) {
 474
 475		/* all-zeroes has no synchronization issues */
 476		case 0:
 477			if (*buf == 0)
 478				continue;
 479			break;
 480
 481		/* "mod63" stays in sync with short-terminated transfers,
 482		 * OR otherwise when host and gadget agree on how large
 483		 * each usb transfer request should be.  Resync is done
 484		 * with set_interface or set_config.  (We *WANT* it to
 485		 * get quickly out of sync if controllers or their drivers
 486		 * stutter for any reason, including buffer duplication...)
 487		 */
 488		case 1:
 489			if (*buf == (u8)((i % max_packet_size) % 63))
 490				continue;
 491			break;
 492		}
 493		ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
 494		usb_ep_set_halt(ss->out_ep);
 495		return -EINVAL;
 496	}
 497	return 0;
 498}
 499
 500static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
 501{
 502	unsigned	i;
 503	u8		*buf = req->buf;
 504	int max_packet_size = le16_to_cpu(ep->desc->wMaxPacketSize);
 505	struct f_sourcesink *ss = ep->driver_data;
 506
 507	switch (ss->pattern) {
 508	case 0:
 509		memset(req->buf, 0, req->length);
 510		break;
 511	case 1:
 512		for  (i = 0; i < req->length; i++)
 513			*buf++ = (u8) ((i % max_packet_size) % 63);
 514		break;
 515	case 2:
 516		break;
 517	}
 518}
 519
 520static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
 521{
 522	struct usb_composite_dev	*cdev;
 523	struct f_sourcesink		*ss = ep->driver_data;
 524	int				status = req->status;
 525
 526	/* driver_data will be null if ep has been disabled */
 527	if (!ss)
 528		return;
 529
 530	cdev = ss->function.config->cdev;
 531
 532	switch (status) {
 533
 534	case 0:				/* normal completion? */
 535		if (ep == ss->out_ep) {
 536			check_read_data(ss, req);
 537			if (ss->pattern != 2)
 538				memset(req->buf, 0x55, req->length);
 539		}
 540		break;
 541
 542	/* this endpoint is normally active while we're configured */
 543	case -ECONNABORTED:		/* hardware forced ep reset */
 544	case -ECONNRESET:		/* request dequeued */
 545	case -ESHUTDOWN:		/* disconnect from host */
 546		VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
 547				req->actual, req->length);
 548		if (ep == ss->out_ep)
 549			check_read_data(ss, req);
 550		free_ep_req(ep, req);
 551		return;
 552
 553	case -EOVERFLOW:		/* buffer overrun on read means that
 554					 * we didn't provide a big enough
 555					 * buffer.
 556					 */
 557	default:
 558#if 1
 559		DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
 560				status, req->actual, req->length);
 561		break;
 562#endif
 563	case -EREMOTEIO:		/* short read */
 564		break;
 565	}
 566
 567	status = usb_ep_queue(ep, req, GFP_ATOMIC);
 568	if (status) {
 569		ERROR(cdev, "kill %s:  resubmit %d bytes --> %d\n",
 570				ep->name, req->length, status);
 571		usb_ep_set_halt(ep);
 572		/* FIXME recover later ... somehow */
 573	}
 574}
 575
 576static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
 577		bool is_iso, int speed)
 578{
 579	struct usb_ep		*ep;
 580	struct usb_request	*req;
 581	int			i, size, qlen, status = 0;
 582
 583	if (is_iso) {
 584		switch (speed) {
 585		case USB_SPEED_SUPER_PLUS:
 586		case USB_SPEED_SUPER:
 587			size = ss->isoc_maxpacket *
 588					(ss->isoc_mult + 1) *
 589					(ss->isoc_maxburst + 1);
 590			break;
 591		case USB_SPEED_HIGH:
 592			size = ss->isoc_maxpacket * (ss->isoc_mult + 1);
 593			break;
 594		default:
 595			size = ss->isoc_maxpacket > 1023 ?
 596					1023 : ss->isoc_maxpacket;
 597			break;
 598		}
 599		ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
 600		qlen = ss->iso_qlen;
 601	} else {
 602		ep = is_in ? ss->in_ep : ss->out_ep;
 603		qlen = ss->bulk_qlen;
 604		size = ss->buflen;
 605	}
 606
 607	for (i = 0; i < qlen; i++) {
 608		req = ss_alloc_ep_req(ep, size);
 609		if (!req)
 610			return -ENOMEM;
 611
 612		req->complete = source_sink_complete;
 613		if (is_in)
 614			reinit_write_data(ep, req);
 615		else if (ss->pattern != 2)
 616			memset(req->buf, 0x55, req->length);
 617
 618		status = usb_ep_queue(ep, req, GFP_ATOMIC);
 619		if (status) {
 620			struct usb_composite_dev	*cdev;
 621
 622			cdev = ss->function.config->cdev;
 623			ERROR(cdev, "start %s%s %s --> %d\n",
 624			      is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
 625			      ep->name, status);
 626			free_ep_req(ep, req);
 627			return status;
 628		}
 629	}
 630
 631	return status;
 632}
 633
 634static void disable_source_sink(struct f_sourcesink *ss)
 635{
 636	struct usb_composite_dev	*cdev;
 637
 638	cdev = ss->function.config->cdev;
 639	disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
 640			ss->iso_out_ep);
 641	VDBG(cdev, "%s disabled\n", ss->function.name);
 642}
 643
 644static int
 645enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
 646		int alt)
 647{
 648	int					result = 0;
 649	int					speed = cdev->gadget->speed;
 650	struct usb_ep				*ep;
 651
 652	/* one bulk endpoint writes (sources) zeroes IN (to the host) */
 653	ep = ss->in_ep;
 654	result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
 655	if (result)
 656		return result;
 657	result = usb_ep_enable(ep);
 658	if (result < 0)
 659		return result;
 660	ep->driver_data = ss;
 661
 662	result = source_sink_start_ep(ss, true, false, speed);
 663	if (result < 0) {
 664fail:
 665		ep = ss->in_ep;
 666		usb_ep_disable(ep);
 667		return result;
 668	}
 669
 670	/* one bulk endpoint reads (sinks) anything OUT (from the host) */
 671	ep = ss->out_ep;
 672	result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
 673	if (result)
 674		goto fail;
 675	result = usb_ep_enable(ep);
 676	if (result < 0)
 677		goto fail;
 678	ep->driver_data = ss;
 679
 680	result = source_sink_start_ep(ss, false, false, speed);
 681	if (result < 0) {
 682fail2:
 683		ep = ss->out_ep;
 684		usb_ep_disable(ep);
 685		goto fail;
 686	}
 687
 688	if (alt == 0)
 689		goto out;
 690
 691	/* one iso endpoint writes (sources) zeroes IN (to the host) */
 692	ep = ss->iso_in_ep;
 693	if (ep) {
 694		result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
 695		if (result)
 696			goto fail2;
 697		result = usb_ep_enable(ep);
 698		if (result < 0)
 699			goto fail2;
 700		ep->driver_data = ss;
 701
 702		result = source_sink_start_ep(ss, true, true, speed);
 703		if (result < 0) {
 704fail3:
 705			ep = ss->iso_in_ep;
 706			if (ep)
 707				usb_ep_disable(ep);
 708			goto fail2;
 709		}
 710	}
 711
 712	/* one iso endpoint reads (sinks) anything OUT (from the host) */
 713	ep = ss->iso_out_ep;
 714	if (ep) {
 715		result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
 716		if (result)
 717			goto fail3;
 718		result = usb_ep_enable(ep);
 719		if (result < 0)
 720			goto fail3;
 721		ep->driver_data = ss;
 722
 723		result = source_sink_start_ep(ss, false, true, speed);
 724		if (result < 0) {
 725			usb_ep_disable(ep);
 726			goto fail3;
 727		}
 728	}
 729out:
 730	ss->cur_alt = alt;
 731
 732	DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
 733	return result;
 734}
 735
 736static int sourcesink_set_alt(struct usb_function *f,
 737		unsigned intf, unsigned alt)
 738{
 739	struct f_sourcesink		*ss = func_to_ss(f);
 740	struct usb_composite_dev	*cdev = f->config->cdev;
 741
 742	disable_source_sink(ss);
 743	return enable_source_sink(cdev, ss, alt);
 744}
 745
 746static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
 747{
 748	struct f_sourcesink		*ss = func_to_ss(f);
 749
 750	return ss->cur_alt;
 751}
 752
 753static void sourcesink_disable(struct usb_function *f)
 754{
 755	struct f_sourcesink	*ss = func_to_ss(f);
 756
 757	disable_source_sink(ss);
 758}
 759
 760/*-------------------------------------------------------------------------*/
 761
 762static int sourcesink_setup(struct usb_function *f,
 763		const struct usb_ctrlrequest *ctrl)
 764{
 765	struct usb_configuration        *c = f->config;
 766	struct usb_request	*req = c->cdev->req;
 767	int			value = -EOPNOTSUPP;
 768	u16			w_index = le16_to_cpu(ctrl->wIndex);
 769	u16			w_value = le16_to_cpu(ctrl->wValue);
 770	u16			w_length = le16_to_cpu(ctrl->wLength);
 771
 772	req->length = USB_COMP_EP0_BUFSIZ;
 773
 774	/* composite driver infrastructure handles everything except
 775	 * the two control test requests.
 776	 */
 777	switch (ctrl->bRequest) {
 778
 779	/*
 780	 * These are the same vendor-specific requests supported by
 781	 * Intel's USB 2.0 compliance test devices.  We exceed that
 782	 * device spec by allowing multiple-packet requests.
 783	 *
 784	 * NOTE:  the Control-OUT data stays in req->buf ... better
 785	 * would be copying it into a scratch buffer, so that other
 786	 * requests may safely intervene.
 787	 */
 788	case 0x5b:	/* control WRITE test -- fill the buffer */
 789		if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
 790			goto unknown;
 791		if (w_value || w_index)
 792			break;
 793		/* just read that many bytes into the buffer */
 794		if (w_length > req->length)
 795			break;
 796		value = w_length;
 797		break;
 798	case 0x5c:	/* control READ test -- return the buffer */
 799		if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
 800			goto unknown;
 801		if (w_value || w_index)
 802			break;
 803		/* expect those bytes are still in the buffer; send back */
 804		if (w_length > req->length)
 805			break;
 806		value = w_length;
 807		break;
 808
 809	default:
 810unknown:
 811		VDBG(c->cdev,
 812			"unknown control req%02x.%02x v%04x i%04x l%d\n",
 813			ctrl->bRequestType, ctrl->bRequest,
 814			w_value, w_index, w_length);
 815	}
 816
 817	/* respond with data transfer or status phase? */
 818	if (value >= 0) {
 819		VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
 820			ctrl->bRequestType, ctrl->bRequest,
 821			w_value, w_index, w_length);
 822		req->zero = 0;
 823		req->length = value;
 824		value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
 825		if (value < 0)
 826			ERROR(c->cdev, "source/sink response, err %d\n",
 827					value);
 828	}
 829
 830	/* device either stalls (value < 0) or reports success */
 831	return value;
 832}
 833
 834static struct usb_function *source_sink_alloc_func(
 835		struct usb_function_instance *fi)
 836{
 837	struct f_sourcesink     *ss;
 838	struct f_ss_opts	*ss_opts;
 839
 840	ss = kzalloc(sizeof(*ss), GFP_KERNEL);
 841	if (!ss)
 842		return ERR_PTR(-ENOMEM);
 843
 844	ss_opts =  container_of(fi, struct f_ss_opts, func_inst);
 845
 846	mutex_lock(&ss_opts->lock);
 847	ss_opts->refcnt++;
 848	mutex_unlock(&ss_opts->lock);
 849
 850	ss->pattern = ss_opts->pattern;
 851	ss->isoc_interval = ss_opts->isoc_interval;
 852	ss->isoc_maxpacket = ss_opts->isoc_maxpacket;
 853	ss->isoc_mult = ss_opts->isoc_mult;
 854	ss->isoc_maxburst = ss_opts->isoc_maxburst;
 855	ss->buflen = ss_opts->bulk_buflen;
 856	ss->bulk_qlen = ss_opts->bulk_qlen;
 857	ss->iso_qlen = ss_opts->iso_qlen;
 858
 859	ss->function.name = "source/sink";
 860	ss->function.bind = sourcesink_bind;
 861	ss->function.set_alt = sourcesink_set_alt;
 862	ss->function.get_alt = sourcesink_get_alt;
 863	ss->function.disable = sourcesink_disable;
 864	ss->function.setup = sourcesink_setup;
 865	ss->function.strings = sourcesink_strings;
 866
 867	ss->function.free_func = sourcesink_free_func;
 868
 869	return &ss->function;
 870}
 871
 872static inline struct f_ss_opts *to_f_ss_opts(struct config_item *item)
 873{
 874	return container_of(to_config_group(item), struct f_ss_opts,
 875			    func_inst.group);
 876}
 877
 878static void ss_attr_release(struct config_item *item)
 879{
 880	struct f_ss_opts *ss_opts = to_f_ss_opts(item);
 881
 882	usb_put_function_instance(&ss_opts->func_inst);
 883}
 884
 885static struct configfs_item_operations ss_item_ops = {
 886	.release		= ss_attr_release,
 887};
 888
 889static ssize_t f_ss_opts_pattern_show(struct config_item *item, char *page)
 890{
 891	struct f_ss_opts *opts = to_f_ss_opts(item);
 892	int result;
 893
 894	mutex_lock(&opts->lock);
 895	result = sprintf(page, "%u\n", opts->pattern);
 896	mutex_unlock(&opts->lock);
 897
 898	return result;
 899}
 900
 901static ssize_t f_ss_opts_pattern_store(struct config_item *item,
 902				       const char *page, size_t len)
 903{
 904	struct f_ss_opts *opts = to_f_ss_opts(item);
 905	int ret;
 906	u8 num;
 907
 908	mutex_lock(&opts->lock);
 909	if (opts->refcnt) {
 910		ret = -EBUSY;
 911		goto end;
 912	}
 913
 914	ret = kstrtou8(page, 0, &num);
 915	if (ret)
 916		goto end;
 917
 918	if (num != 0 && num != 1 && num != 2) {
 919		ret = -EINVAL;
 920		goto end;
 921	}
 922
 923	opts->pattern = num;
 924	ret = len;
 925end:
 926	mutex_unlock(&opts->lock);
 927	return ret;
 928}
 929
 930CONFIGFS_ATTR(f_ss_opts_, pattern);
 931
 932static ssize_t f_ss_opts_isoc_interval_show(struct config_item *item, char *page)
 933{
 934	struct f_ss_opts *opts = to_f_ss_opts(item);
 935	int result;
 936
 937	mutex_lock(&opts->lock);
 938	result = sprintf(page, "%u\n", opts->isoc_interval);
 939	mutex_unlock(&opts->lock);
 940
 941	return result;
 942}
 943
 944static ssize_t f_ss_opts_isoc_interval_store(struct config_item *item,
 945				       const char *page, size_t len)
 946{
 947	struct f_ss_opts *opts = to_f_ss_opts(item);
 948	int ret;
 949	u8 num;
 950
 951	mutex_lock(&opts->lock);
 952	if (opts->refcnt) {
 953		ret = -EBUSY;
 954		goto end;
 955	}
 956
 957	ret = kstrtou8(page, 0, &num);
 958	if (ret)
 959		goto end;
 960
 961	if (num > 16) {
 962		ret = -EINVAL;
 963		goto end;
 964	}
 965
 966	opts->isoc_interval = num;
 967	ret = len;
 968end:
 969	mutex_unlock(&opts->lock);
 970	return ret;
 971}
 972
 973CONFIGFS_ATTR(f_ss_opts_, isoc_interval);
 974
 975static ssize_t f_ss_opts_isoc_maxpacket_show(struct config_item *item, char *page)
 976{
 977	struct f_ss_opts *opts = to_f_ss_opts(item);
 978	int result;
 979
 980	mutex_lock(&opts->lock);
 981	result = sprintf(page, "%u\n", opts->isoc_maxpacket);
 982	mutex_unlock(&opts->lock);
 983
 984	return result;
 985}
 986
 987static ssize_t f_ss_opts_isoc_maxpacket_store(struct config_item *item,
 988				       const char *page, size_t len)
 989{
 990	struct f_ss_opts *opts = to_f_ss_opts(item);
 991	int ret;
 992	u16 num;
 993
 994	mutex_lock(&opts->lock);
 995	if (opts->refcnt) {
 996		ret = -EBUSY;
 997		goto end;
 998	}
 999
1000	ret = kstrtou16(page, 0, &num);
1001	if (ret)
1002		goto end;
1003
1004	if (num > 1024) {
1005		ret = -EINVAL;
1006		goto end;
1007	}
1008
1009	opts->isoc_maxpacket = num;
1010	ret = len;
1011end:
1012	mutex_unlock(&opts->lock);
1013	return ret;
1014}
1015
1016CONFIGFS_ATTR(f_ss_opts_, isoc_maxpacket);
1017
1018static ssize_t f_ss_opts_isoc_mult_show(struct config_item *item, char *page)
1019{
1020	struct f_ss_opts *opts = to_f_ss_opts(item);
1021	int result;
1022
1023	mutex_lock(&opts->lock);
1024	result = sprintf(page, "%u\n", opts->isoc_mult);
1025	mutex_unlock(&opts->lock);
1026
1027	return result;
1028}
1029
1030static ssize_t f_ss_opts_isoc_mult_store(struct config_item *item,
1031				       const char *page, size_t len)
1032{
1033	struct f_ss_opts *opts = to_f_ss_opts(item);
1034	int ret;
1035	u8 num;
1036
1037	mutex_lock(&opts->lock);
1038	if (opts->refcnt) {
1039		ret = -EBUSY;
1040		goto end;
1041	}
1042
1043	ret = kstrtou8(page, 0, &num);
1044	if (ret)
1045		goto end;
1046
1047	if (num > 2) {
1048		ret = -EINVAL;
1049		goto end;
1050	}
1051
1052	opts->isoc_mult = num;
1053	ret = len;
1054end:
1055	mutex_unlock(&opts->lock);
1056	return ret;
1057}
1058
1059CONFIGFS_ATTR(f_ss_opts_, isoc_mult);
1060
1061static ssize_t f_ss_opts_isoc_maxburst_show(struct config_item *item, char *page)
1062{
1063	struct f_ss_opts *opts = to_f_ss_opts(item);
1064	int result;
1065
1066	mutex_lock(&opts->lock);
1067	result = sprintf(page, "%u\n", opts->isoc_maxburst);
1068	mutex_unlock(&opts->lock);
1069
1070	return result;
1071}
1072
1073static ssize_t f_ss_opts_isoc_maxburst_store(struct config_item *item,
1074				       const char *page, size_t len)
1075{
1076	struct f_ss_opts *opts = to_f_ss_opts(item);
1077	int ret;
1078	u8 num;
1079
1080	mutex_lock(&opts->lock);
1081	if (opts->refcnt) {
1082		ret = -EBUSY;
1083		goto end;
1084	}
1085
1086	ret = kstrtou8(page, 0, &num);
1087	if (ret)
1088		goto end;
1089
1090	if (num > 15) {
1091		ret = -EINVAL;
1092		goto end;
1093	}
1094
1095	opts->isoc_maxburst = num;
1096	ret = len;
1097end:
1098	mutex_unlock(&opts->lock);
1099	return ret;
1100}
1101
1102CONFIGFS_ATTR(f_ss_opts_, isoc_maxburst);
1103
1104static ssize_t f_ss_opts_bulk_buflen_show(struct config_item *item, char *page)
1105{
1106	struct f_ss_opts *opts = to_f_ss_opts(item);
1107	int result;
1108
1109	mutex_lock(&opts->lock);
1110	result = sprintf(page, "%u\n", opts->bulk_buflen);
1111	mutex_unlock(&opts->lock);
1112
1113	return result;
1114}
1115
1116static ssize_t f_ss_opts_bulk_buflen_store(struct config_item *item,
1117					   const char *page, size_t len)
1118{
1119	struct f_ss_opts *opts = to_f_ss_opts(item);
1120	int ret;
1121	u32 num;
1122
1123	mutex_lock(&opts->lock);
1124	if (opts->refcnt) {
1125		ret = -EBUSY;
1126		goto end;
1127	}
1128
1129	ret = kstrtou32(page, 0, &num);
1130	if (ret)
1131		goto end;
1132
1133	opts->bulk_buflen = num;
1134	ret = len;
1135end:
1136	mutex_unlock(&opts->lock);
1137	return ret;
1138}
1139
1140CONFIGFS_ATTR(f_ss_opts_, bulk_buflen);
1141
1142static ssize_t f_ss_opts_bulk_qlen_show(struct config_item *item, char *page)
1143{
1144	struct f_ss_opts *opts = to_f_ss_opts(item);
1145	int result;
1146
1147	mutex_lock(&opts->lock);
1148	result = sprintf(page, "%u\n", opts->bulk_qlen);
1149	mutex_unlock(&opts->lock);
1150
1151	return result;
1152}
1153
1154static ssize_t f_ss_opts_bulk_qlen_store(struct config_item *item,
1155					   const char *page, size_t len)
1156{
1157	struct f_ss_opts *opts = to_f_ss_opts(item);
1158	int ret;
1159	u32 num;
1160
1161	mutex_lock(&opts->lock);
1162	if (opts->refcnt) {
1163		ret = -EBUSY;
1164		goto end;
1165	}
1166
1167	ret = kstrtou32(page, 0, &num);
1168	if (ret)
1169		goto end;
1170
1171	opts->bulk_qlen = num;
1172	ret = len;
1173end:
1174	mutex_unlock(&opts->lock);
1175	return ret;
1176}
1177
1178CONFIGFS_ATTR(f_ss_opts_, bulk_qlen);
1179
1180static ssize_t f_ss_opts_iso_qlen_show(struct config_item *item, char *page)
1181{
1182	struct f_ss_opts *opts = to_f_ss_opts(item);
1183	int result;
1184
1185	mutex_lock(&opts->lock);
1186	result = sprintf(page, "%u\n", opts->iso_qlen);
1187	mutex_unlock(&opts->lock);
1188
1189	return result;
1190}
1191
1192static ssize_t f_ss_opts_iso_qlen_store(struct config_item *item,
1193					   const char *page, size_t len)
1194{
1195	struct f_ss_opts *opts = to_f_ss_opts(item);
1196	int ret;
1197	u32 num;
1198
1199	mutex_lock(&opts->lock);
1200	if (opts->refcnt) {
1201		ret = -EBUSY;
1202		goto end;
1203	}
1204
1205	ret = kstrtou32(page, 0, &num);
1206	if (ret)
1207		goto end;
1208
1209	opts->iso_qlen = num;
1210	ret = len;
1211end:
1212	mutex_unlock(&opts->lock);
1213	return ret;
1214}
1215
1216CONFIGFS_ATTR(f_ss_opts_, iso_qlen);
1217
1218static struct configfs_attribute *ss_attrs[] = {
1219	&f_ss_opts_attr_pattern,
1220	&f_ss_opts_attr_isoc_interval,
1221	&f_ss_opts_attr_isoc_maxpacket,
1222	&f_ss_opts_attr_isoc_mult,
1223	&f_ss_opts_attr_isoc_maxburst,
1224	&f_ss_opts_attr_bulk_buflen,
1225	&f_ss_opts_attr_bulk_qlen,
1226	&f_ss_opts_attr_iso_qlen,
1227	NULL,
1228};
1229
1230static const struct config_item_type ss_func_type = {
1231	.ct_item_ops    = &ss_item_ops,
1232	.ct_attrs	= ss_attrs,
1233	.ct_owner       = THIS_MODULE,
1234};
1235
1236static void source_sink_free_instance(struct usb_function_instance *fi)
1237{
1238	struct f_ss_opts *ss_opts;
1239
1240	ss_opts = container_of(fi, struct f_ss_opts, func_inst);
1241	kfree(ss_opts);
1242}
1243
1244static struct usb_function_instance *source_sink_alloc_inst(void)
1245{
1246	struct f_ss_opts *ss_opts;
1247
1248	ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL);
1249	if (!ss_opts)
1250		return ERR_PTR(-ENOMEM);
1251	mutex_init(&ss_opts->lock);
1252	ss_opts->func_inst.free_func_inst = source_sink_free_instance;
1253	ss_opts->isoc_interval = GZERO_ISOC_INTERVAL;
1254	ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET;
1255	ss_opts->bulk_buflen = GZERO_BULK_BUFLEN;
1256	ss_opts->bulk_qlen = GZERO_SS_BULK_QLEN;
1257	ss_opts->iso_qlen = GZERO_SS_ISO_QLEN;
1258
1259	config_group_init_type_name(&ss_opts->func_inst.group, "",
1260				    &ss_func_type);
1261
1262	return &ss_opts->func_inst;
1263}
1264DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst,
1265		source_sink_alloc_func);
1266
1267static int __init sslb_modinit(void)
1268{
1269	int ret;
1270
1271	ret = usb_function_register(&SourceSinkusb_func);
1272	if (ret)
1273		return ret;
1274	ret = lb_modinit();
1275	if (ret)
1276		usb_function_unregister(&SourceSinkusb_func);
1277	return ret;
1278}
1279static void __exit sslb_modexit(void)
1280{
1281	usb_function_unregister(&SourceSinkusb_func);
1282	lb_modexit();
1283}
1284module_init(sslb_modinit);
1285module_exit(sslb_modexit);
1286
1287MODULE_LICENSE("GPL");
v4.10.11
 
   1/*
   2 * f_sourcesink.c - USB peripheral source/sink configuration driver
   3 *
   4 * Copyright (C) 2003-2008 David Brownell
   5 * Copyright (C) 2008 by Nokia Corporation
   6 *
   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.
  11 */
  12
  13/* #define VERBOSE_DEBUG */
  14
  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>
  21
  22#include "g_zero.h"
  23#include "u_f.h"
  24
  25/*
  26 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
  27 * controller drivers.
  28 *
  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.
  32 *
  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.
  37 */
  38struct f_sourcesink {
  39	struct usb_function	function;
  40
  41	struct usb_ep		*in_ep;
  42	struct usb_ep		*out_ep;
  43	struct usb_ep		*iso_in_ep;
  44	struct usb_ep		*iso_out_ep;
  45	int			cur_alt;
  46
  47	unsigned pattern;
  48	unsigned isoc_interval;
  49	unsigned isoc_maxpacket;
  50	unsigned isoc_mult;
  51	unsigned isoc_maxburst;
  52	unsigned buflen;
  53	unsigned bulk_qlen;
  54	unsigned iso_qlen;
  55};
  56
  57static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
  58{
  59	return container_of(f, struct f_sourcesink, function);
  60}
  61
  62/*-------------------------------------------------------------------------*/
  63
  64static struct usb_interface_descriptor source_sink_intf_alt0 = {
  65	.bLength =		USB_DT_INTERFACE_SIZE,
  66	.bDescriptorType =	USB_DT_INTERFACE,
  67
  68	.bAlternateSetting =	0,
  69	.bNumEndpoints =	2,
  70	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
  71	/* .iInterface		= DYNAMIC */
  72};
  73
  74static struct usb_interface_descriptor source_sink_intf_alt1 = {
  75	.bLength =		USB_DT_INTERFACE_SIZE,
  76	.bDescriptorType =	USB_DT_INTERFACE,
  77
  78	.bAlternateSetting =	1,
  79	.bNumEndpoints =	4,
  80	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
  81	/* .iInterface		= DYNAMIC */
  82};
  83
  84/* full speed support: */
  85
  86static struct usb_endpoint_descriptor fs_source_desc = {
  87	.bLength =		USB_DT_ENDPOINT_SIZE,
  88	.bDescriptorType =	USB_DT_ENDPOINT,
  89
  90	.bEndpointAddress =	USB_DIR_IN,
  91	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
  92};
  93
  94static struct usb_endpoint_descriptor fs_sink_desc = {
  95	.bLength =		USB_DT_ENDPOINT_SIZE,
  96	.bDescriptorType =	USB_DT_ENDPOINT,
  97
  98	.bEndpointAddress =	USB_DIR_OUT,
  99	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
 100};
 101
 102static struct usb_endpoint_descriptor fs_iso_source_desc = {
 103	.bLength =		USB_DT_ENDPOINT_SIZE,
 104	.bDescriptorType =	USB_DT_ENDPOINT,
 105
 106	.bEndpointAddress =	USB_DIR_IN,
 107	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
 108	.wMaxPacketSize =	cpu_to_le16(1023),
 109	.bInterval =		4,
 110};
 111
 112static struct usb_endpoint_descriptor fs_iso_sink_desc = {
 113	.bLength =		USB_DT_ENDPOINT_SIZE,
 114	.bDescriptorType =	USB_DT_ENDPOINT,
 115
 116	.bEndpointAddress =	USB_DIR_OUT,
 117	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
 118	.wMaxPacketSize =	cpu_to_le16(1023),
 119	.bInterval =		4,
 120};
 121
 122static 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,
 132	NULL,
 133};
 134
 135/* high speed support: */
 136
 137static struct usb_endpoint_descriptor hs_source_desc = {
 138	.bLength =		USB_DT_ENDPOINT_SIZE,
 139	.bDescriptorType =	USB_DT_ENDPOINT,
 140
 141	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
 142	.wMaxPacketSize =	cpu_to_le16(512),
 143};
 144
 145static struct usb_endpoint_descriptor hs_sink_desc = {
 146	.bLength =		USB_DT_ENDPOINT_SIZE,
 147	.bDescriptorType =	USB_DT_ENDPOINT,
 148
 149	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
 150	.wMaxPacketSize =	cpu_to_le16(512),
 151};
 152
 153static struct usb_endpoint_descriptor hs_iso_source_desc = {
 154	.bLength =		USB_DT_ENDPOINT_SIZE,
 155	.bDescriptorType =	USB_DT_ENDPOINT,
 156
 157	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
 158	.wMaxPacketSize =	cpu_to_le16(1024),
 159	.bInterval =		4,
 160};
 161
 162static struct usb_endpoint_descriptor hs_iso_sink_desc = {
 163	.bLength =		USB_DT_ENDPOINT_SIZE,
 164	.bDescriptorType =	USB_DT_ENDPOINT,
 165
 166	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
 167	.wMaxPacketSize =	cpu_to_le16(1024),
 168	.bInterval =		4,
 169};
 170
 171static 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,
 181	NULL,
 182};
 183
 184/* super speed support: */
 185
 186static struct usb_endpoint_descriptor ss_source_desc = {
 187	.bLength =		USB_DT_ENDPOINT_SIZE,
 188	.bDescriptorType =	USB_DT_ENDPOINT,
 189
 190	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
 191	.wMaxPacketSize =	cpu_to_le16(1024),
 192};
 193
 194static 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,
 197
 198	.bMaxBurst =		0,
 199	.bmAttributes =		0,
 200	.wBytesPerInterval =	0,
 201};
 202
 203static struct usb_endpoint_descriptor ss_sink_desc = {
 204	.bLength =		USB_DT_ENDPOINT_SIZE,
 205	.bDescriptorType =	USB_DT_ENDPOINT,
 206
 207	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
 208	.wMaxPacketSize =	cpu_to_le16(1024),
 209};
 210
 211static 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,
 214
 215	.bMaxBurst =		0,
 216	.bmAttributes =		0,
 217	.wBytesPerInterval =	0,
 218};
 219
 220static struct usb_endpoint_descriptor ss_iso_source_desc = {
 221	.bLength =		USB_DT_ENDPOINT_SIZE,
 222	.bDescriptorType =	USB_DT_ENDPOINT,
 223
 224	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
 225	.wMaxPacketSize =	cpu_to_le16(1024),
 226	.bInterval =		4,
 227};
 228
 229static 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,
 232
 233	.bMaxBurst =		0,
 234	.bmAttributes =		0,
 235	.wBytesPerInterval =	cpu_to_le16(1024),
 236};
 237
 238static struct usb_endpoint_descriptor ss_iso_sink_desc = {
 239	.bLength =		USB_DT_ENDPOINT_SIZE,
 240	.bDescriptorType =	USB_DT_ENDPOINT,
 241
 242	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
 243	.wMaxPacketSize =	cpu_to_le16(1024),
 244	.bInterval =		4,
 245};
 246
 247static 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,
 250
 251	.bMaxBurst =		0,
 252	.bmAttributes =		0,
 253	.wBytesPerInterval =	cpu_to_le16(1024),
 254};
 255
 256static 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,
 272	NULL,
 273};
 274
 275/* function-specific strings: */
 276
 277static struct usb_string strings_sourcesink[] = {
 278	[0].s = "source and sink data",
 279	{  }			/* end of list */
 280};
 281
 282static struct usb_gadget_strings stringtab_sourcesink = {
 283	.language	= 0x0409,	/* en-us */
 284	.strings	= strings_sourcesink,
 285};
 286
 287static struct usb_gadget_strings *sourcesink_strings[] = {
 288	&stringtab_sourcesink,
 289	NULL,
 290};
 291
 292/*-------------------------------------------------------------------------*/
 293
 294static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len)
 295{
 296	return alloc_ep_req(ep, len);
 297}
 298
 299static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
 300{
 301	int			value;
 302
 303	value = usb_ep_disable(ep);
 304	if (value < 0)
 305		DBG(cdev, "disable %s --> %d\n", ep->name, value);
 306}
 307
 308void 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)
 311{
 312	disable_ep(cdev, in);
 313	disable_ep(cdev, out);
 314	if (iso_in)
 315		disable_ep(cdev, iso_in);
 316	if (iso_out)
 317		disable_ep(cdev, iso_out);
 318}
 319
 320static int
 321sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
 322{
 323	struct usb_composite_dev *cdev = c->cdev;
 324	struct f_sourcesink	*ss = func_to_ss(f);
 325	int	id;
 326	int ret;
 327
 328	/* allocate interface ID(s) */
 329	id = usb_interface_id(c, f);
 330	if (id < 0)
 331		return id;
 332	source_sink_intf_alt0.bInterfaceNumber = id;
 333	source_sink_intf_alt1.bInterfaceNumber = id;
 334
 335	/* allocate bulk endpoints */
 336	ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
 337	if (!ss->in_ep) {
 338autoconf_fail:
 339		ERROR(cdev, "%s: can't autoconfigure on %s\n",
 340			f->name, cdev->gadget->name);
 341		return -ENODEV;
 342	}
 343
 344	ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
 345	if (!ss->out_ep)
 346		goto autoconf_fail;
 347
 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)
 354		ss->isoc_mult = 2;
 355	if (ss->isoc_maxburst > 15)
 356		ss->isoc_maxburst = 15;
 357
 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;
 365
 366	/* allocate iso endpoints */
 367	ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
 368	if (!ss->iso_in_ep)
 369		goto no_iso;
 370
 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;
 375no_iso:
 376		/*
 377		 * We still want to work even if the UDC doesn't have isoc
 378		 * endpoints, so null out the alt interface that contains
 379		 * them and continue.
 380		 */
 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;
 384	}
 385
 386	if (ss->isoc_maxpacket > 1024)
 387		ss->isoc_maxpacket = 1024;
 388
 389	/* support high speed hardware */
 390	hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
 391	hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
 392
 393	/*
 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.
 397	 */
 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;
 403
 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;
 408
 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;
 414
 415	/*
 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.
 419	 */
 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;
 428
 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;
 436
 437	ret = usb_assign_descriptors(f, fs_source_sink_descs,
 438			hs_source_sink_descs, ss_source_sink_descs, NULL);
 
 439	if (ret)
 440		return ret;
 441
 442	DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
 443	    (gadget_is_superspeed(c->cdev->gadget) ? "super" :
 444	     (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
 445			f->name, ss->in_ep->name, ss->out_ep->name,
 446			ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
 447			ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
 448	return 0;
 449}
 450
 451static void
 452sourcesink_free_func(struct usb_function *f)
 453{
 454	struct f_ss_opts *opts;
 455
 456	opts = container_of(f->fi, struct f_ss_opts, func_inst);
 457
 458	mutex_lock(&opts->lock);
 459	opts->refcnt--;
 460	mutex_unlock(&opts->lock);
 461
 462	usb_free_all_descriptors(f);
 463	kfree(func_to_ss(f));
 464}
 465
 466/* optionally require specific source/sink data patterns  */
 467static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
 468{
 469	unsigned		i;
 470	u8			*buf = req->buf;
 471	struct usb_composite_dev *cdev = ss->function.config->cdev;
 472	int max_packet_size = le16_to_cpu(ss->out_ep->desc->wMaxPacketSize);
 473
 474	if (ss->pattern == 2)
 475		return 0;
 476
 477	for (i = 0; i < req->actual; i++, buf++) {
 478		switch (ss->pattern) {
 479
 480		/* all-zeroes has no synchronization issues */
 481		case 0:
 482			if (*buf == 0)
 483				continue;
 484			break;
 485
 486		/* "mod63" stays in sync with short-terminated transfers,
 487		 * OR otherwise when host and gadget agree on how large
 488		 * each usb transfer request should be.  Resync is done
 489		 * with set_interface or set_config.  (We *WANT* it to
 490		 * get quickly out of sync if controllers or their drivers
 491		 * stutter for any reason, including buffer duplication...)
 492		 */
 493		case 1:
 494			if (*buf == (u8)((i % max_packet_size) % 63))
 495				continue;
 496			break;
 497		}
 498		ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
 499		usb_ep_set_halt(ss->out_ep);
 500		return -EINVAL;
 501	}
 502	return 0;
 503}
 504
 505static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
 506{
 507	unsigned	i;
 508	u8		*buf = req->buf;
 509	int max_packet_size = le16_to_cpu(ep->desc->wMaxPacketSize);
 510	struct f_sourcesink *ss = ep->driver_data;
 511
 512	switch (ss->pattern) {
 513	case 0:
 514		memset(req->buf, 0, req->length);
 515		break;
 516	case 1:
 517		for  (i = 0; i < req->length; i++)
 518			*buf++ = (u8) ((i % max_packet_size) % 63);
 519		break;
 520	case 2:
 521		break;
 522	}
 523}
 524
 525static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
 526{
 527	struct usb_composite_dev	*cdev;
 528	struct f_sourcesink		*ss = ep->driver_data;
 529	int				status = req->status;
 530
 531	/* driver_data will be null if ep has been disabled */
 532	if (!ss)
 533		return;
 534
 535	cdev = ss->function.config->cdev;
 536
 537	switch (status) {
 538
 539	case 0:				/* normal completion? */
 540		if (ep == ss->out_ep) {
 541			check_read_data(ss, req);
 542			if (ss->pattern != 2)
 543				memset(req->buf, 0x55, req->length);
 544		}
 545		break;
 546
 547	/* this endpoint is normally active while we're configured */
 548	case -ECONNABORTED:		/* hardware forced ep reset */
 549	case -ECONNRESET:		/* request dequeued */
 550	case -ESHUTDOWN:		/* disconnect from host */
 551		VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
 552				req->actual, req->length);
 553		if (ep == ss->out_ep)
 554			check_read_data(ss, req);
 555		free_ep_req(ep, req);
 556		return;
 557
 558	case -EOVERFLOW:		/* buffer overrun on read means that
 559					 * we didn't provide a big enough
 560					 * buffer.
 561					 */
 562	default:
 563#if 1
 564		DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
 565				status, req->actual, req->length);
 
 566#endif
 567	case -EREMOTEIO:		/* short read */
 568		break;
 569	}
 570
 571	status = usb_ep_queue(ep, req, GFP_ATOMIC);
 572	if (status) {
 573		ERROR(cdev, "kill %s:  resubmit %d bytes --> %d\n",
 574				ep->name, req->length, status);
 575		usb_ep_set_halt(ep);
 576		/* FIXME recover later ... somehow */
 577	}
 578}
 579
 580static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
 581		bool is_iso, int speed)
 582{
 583	struct usb_ep		*ep;
 584	struct usb_request	*req;
 585	int			i, size, qlen, status = 0;
 586
 587	if (is_iso) {
 588		switch (speed) {
 
 589		case USB_SPEED_SUPER:
 590			size = ss->isoc_maxpacket *
 591					(ss->isoc_mult + 1) *
 592					(ss->isoc_maxburst + 1);
 593			break;
 594		case USB_SPEED_HIGH:
 595			size = ss->isoc_maxpacket * (ss->isoc_mult + 1);
 596			break;
 597		default:
 598			size = ss->isoc_maxpacket > 1023 ?
 599					1023 : ss->isoc_maxpacket;
 600			break;
 601		}
 602		ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
 603		qlen = ss->iso_qlen;
 604	} else {
 605		ep = is_in ? ss->in_ep : ss->out_ep;
 606		qlen = ss->bulk_qlen;
 607		size = ss->buflen;
 608	}
 609
 610	for (i = 0; i < qlen; i++) {
 611		req = ss_alloc_ep_req(ep, size);
 612		if (!req)
 613			return -ENOMEM;
 614
 615		req->complete = source_sink_complete;
 616		if (is_in)
 617			reinit_write_data(ep, req);
 618		else if (ss->pattern != 2)
 619			memset(req->buf, 0x55, req->length);
 620
 621		status = usb_ep_queue(ep, req, GFP_ATOMIC);
 622		if (status) {
 623			struct usb_composite_dev	*cdev;
 624
 625			cdev = ss->function.config->cdev;
 626			ERROR(cdev, "start %s%s %s --> %d\n",
 627			      is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
 628			      ep->name, status);
 629			free_ep_req(ep, req);
 630			return status;
 631		}
 632	}
 633
 634	return status;
 635}
 636
 637static void disable_source_sink(struct f_sourcesink *ss)
 638{
 639	struct usb_composite_dev	*cdev;
 640
 641	cdev = ss->function.config->cdev;
 642	disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
 643			ss->iso_out_ep);
 644	VDBG(cdev, "%s disabled\n", ss->function.name);
 645}
 646
 647static int
 648enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
 649		int alt)
 650{
 651	int					result = 0;
 652	int					speed = cdev->gadget->speed;
 653	struct usb_ep				*ep;
 654
 655	/* one bulk endpoint writes (sources) zeroes IN (to the host) */
 656	ep = ss->in_ep;
 657	result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
 658	if (result)
 659		return result;
 660	result = usb_ep_enable(ep);
 661	if (result < 0)
 662		return result;
 663	ep->driver_data = ss;
 664
 665	result = source_sink_start_ep(ss, true, false, speed);
 666	if (result < 0) {
 667fail:
 668		ep = ss->in_ep;
 669		usb_ep_disable(ep);
 670		return result;
 671	}
 672
 673	/* one bulk endpoint reads (sinks) anything OUT (from the host) */
 674	ep = ss->out_ep;
 675	result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
 676	if (result)
 677		goto fail;
 678	result = usb_ep_enable(ep);
 679	if (result < 0)
 680		goto fail;
 681	ep->driver_data = ss;
 682
 683	result = source_sink_start_ep(ss, false, false, speed);
 684	if (result < 0) {
 685fail2:
 686		ep = ss->out_ep;
 687		usb_ep_disable(ep);
 688		goto fail;
 689	}
 690
 691	if (alt == 0)
 692		goto out;
 693
 694	/* one iso endpoint writes (sources) zeroes IN (to the host) */
 695	ep = ss->iso_in_ep;
 696	if (ep) {
 697		result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
 698		if (result)
 699			goto fail2;
 700		result = usb_ep_enable(ep);
 701		if (result < 0)
 702			goto fail2;
 703		ep->driver_data = ss;
 704
 705		result = source_sink_start_ep(ss, true, true, speed);
 706		if (result < 0) {
 707fail3:
 708			ep = ss->iso_in_ep;
 709			if (ep)
 710				usb_ep_disable(ep);
 711			goto fail2;
 712		}
 713	}
 714
 715	/* one iso endpoint reads (sinks) anything OUT (from the host) */
 716	ep = ss->iso_out_ep;
 717	if (ep) {
 718		result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
 719		if (result)
 720			goto fail3;
 721		result = usb_ep_enable(ep);
 722		if (result < 0)
 723			goto fail3;
 724		ep->driver_data = ss;
 725
 726		result = source_sink_start_ep(ss, false, true, speed);
 727		if (result < 0) {
 728			usb_ep_disable(ep);
 729			goto fail3;
 730		}
 731	}
 732out:
 733	ss->cur_alt = alt;
 734
 735	DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
 736	return result;
 737}
 738
 739static int sourcesink_set_alt(struct usb_function *f,
 740		unsigned intf, unsigned alt)
 741{
 742	struct f_sourcesink		*ss = func_to_ss(f);
 743	struct usb_composite_dev	*cdev = f->config->cdev;
 744
 745	disable_source_sink(ss);
 746	return enable_source_sink(cdev, ss, alt);
 747}
 748
 749static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
 750{
 751	struct f_sourcesink		*ss = func_to_ss(f);
 752
 753	return ss->cur_alt;
 754}
 755
 756static void sourcesink_disable(struct usb_function *f)
 757{
 758	struct f_sourcesink	*ss = func_to_ss(f);
 759
 760	disable_source_sink(ss);
 761}
 762
 763/*-------------------------------------------------------------------------*/
 764
 765static int sourcesink_setup(struct usb_function *f,
 766		const struct usb_ctrlrequest *ctrl)
 767{
 768	struct usb_configuration        *c = f->config;
 769	struct usb_request	*req = c->cdev->req;
 770	int			value = -EOPNOTSUPP;
 771	u16			w_index = le16_to_cpu(ctrl->wIndex);
 772	u16			w_value = le16_to_cpu(ctrl->wValue);
 773	u16			w_length = le16_to_cpu(ctrl->wLength);
 774
 775	req->length = USB_COMP_EP0_BUFSIZ;
 776
 777	/* composite driver infrastructure handles everything except
 778	 * the two control test requests.
 779	 */
 780	switch (ctrl->bRequest) {
 781
 782	/*
 783	 * These are the same vendor-specific requests supported by
 784	 * Intel's USB 2.0 compliance test devices.  We exceed that
 785	 * device spec by allowing multiple-packet requests.
 786	 *
 787	 * NOTE:  the Control-OUT data stays in req->buf ... better
 788	 * would be copying it into a scratch buffer, so that other
 789	 * requests may safely intervene.
 790	 */
 791	case 0x5b:	/* control WRITE test -- fill the buffer */
 792		if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
 793			goto unknown;
 794		if (w_value || w_index)
 795			break;
 796		/* just read that many bytes into the buffer */
 797		if (w_length > req->length)
 798			break;
 799		value = w_length;
 800		break;
 801	case 0x5c:	/* control READ test -- return the buffer */
 802		if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
 803			goto unknown;
 804		if (w_value || w_index)
 805			break;
 806		/* expect those bytes are still in the buffer; send back */
 807		if (w_length > req->length)
 808			break;
 809		value = w_length;
 810		break;
 811
 812	default:
 813unknown:
 814		VDBG(c->cdev,
 815			"unknown control req%02x.%02x v%04x i%04x l%d\n",
 816			ctrl->bRequestType, ctrl->bRequest,
 817			w_value, w_index, w_length);
 818	}
 819
 820	/* respond with data transfer or status phase? */
 821	if (value >= 0) {
 822		VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
 823			ctrl->bRequestType, ctrl->bRequest,
 824			w_value, w_index, w_length);
 825		req->zero = 0;
 826		req->length = value;
 827		value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
 828		if (value < 0)
 829			ERROR(c->cdev, "source/sink response, err %d\n",
 830					value);
 831	}
 832
 833	/* device either stalls (value < 0) or reports success */
 834	return value;
 835}
 836
 837static struct usb_function *source_sink_alloc_func(
 838		struct usb_function_instance *fi)
 839{
 840	struct f_sourcesink     *ss;
 841	struct f_ss_opts	*ss_opts;
 842
 843	ss = kzalloc(sizeof(*ss), GFP_KERNEL);
 844	if (!ss)
 845		return NULL;
 846
 847	ss_opts =  container_of(fi, struct f_ss_opts, func_inst);
 848
 849	mutex_lock(&ss_opts->lock);
 850	ss_opts->refcnt++;
 851	mutex_unlock(&ss_opts->lock);
 852
 853	ss->pattern = ss_opts->pattern;
 854	ss->isoc_interval = ss_opts->isoc_interval;
 855	ss->isoc_maxpacket = ss_opts->isoc_maxpacket;
 856	ss->isoc_mult = ss_opts->isoc_mult;
 857	ss->isoc_maxburst = ss_opts->isoc_maxburst;
 858	ss->buflen = ss_opts->bulk_buflen;
 859	ss->bulk_qlen = ss_opts->bulk_qlen;
 860	ss->iso_qlen = ss_opts->iso_qlen;
 861
 862	ss->function.name = "source/sink";
 863	ss->function.bind = sourcesink_bind;
 864	ss->function.set_alt = sourcesink_set_alt;
 865	ss->function.get_alt = sourcesink_get_alt;
 866	ss->function.disable = sourcesink_disable;
 867	ss->function.setup = sourcesink_setup;
 868	ss->function.strings = sourcesink_strings;
 869
 870	ss->function.free_func = sourcesink_free_func;
 871
 872	return &ss->function;
 873}
 874
 875static inline struct f_ss_opts *to_f_ss_opts(struct config_item *item)
 876{
 877	return container_of(to_config_group(item), struct f_ss_opts,
 878			    func_inst.group);
 879}
 880
 881static void ss_attr_release(struct config_item *item)
 882{
 883	struct f_ss_opts *ss_opts = to_f_ss_opts(item);
 884
 885	usb_put_function_instance(&ss_opts->func_inst);
 886}
 887
 888static struct configfs_item_operations ss_item_ops = {
 889	.release		= ss_attr_release,
 890};
 891
 892static ssize_t f_ss_opts_pattern_show(struct config_item *item, char *page)
 893{
 894	struct f_ss_opts *opts = to_f_ss_opts(item);
 895	int result;
 896
 897	mutex_lock(&opts->lock);
 898	result = sprintf(page, "%u\n", opts->pattern);
 899	mutex_unlock(&opts->lock);
 900
 901	return result;
 902}
 903
 904static ssize_t f_ss_opts_pattern_store(struct config_item *item,
 905				       const char *page, size_t len)
 906{
 907	struct f_ss_opts *opts = to_f_ss_opts(item);
 908	int ret;
 909	u8 num;
 910
 911	mutex_lock(&opts->lock);
 912	if (opts->refcnt) {
 913		ret = -EBUSY;
 914		goto end;
 915	}
 916
 917	ret = kstrtou8(page, 0, &num);
 918	if (ret)
 919		goto end;
 920
 921	if (num != 0 && num != 1 && num != 2) {
 922		ret = -EINVAL;
 923		goto end;
 924	}
 925
 926	opts->pattern = num;
 927	ret = len;
 928end:
 929	mutex_unlock(&opts->lock);
 930	return ret;
 931}
 932
 933CONFIGFS_ATTR(f_ss_opts_, pattern);
 934
 935static ssize_t f_ss_opts_isoc_interval_show(struct config_item *item, char *page)
 936{
 937	struct f_ss_opts *opts = to_f_ss_opts(item);
 938	int result;
 939
 940	mutex_lock(&opts->lock);
 941	result = sprintf(page, "%u\n", opts->isoc_interval);
 942	mutex_unlock(&opts->lock);
 943
 944	return result;
 945}
 946
 947static ssize_t f_ss_opts_isoc_interval_store(struct config_item *item,
 948				       const char *page, size_t len)
 949{
 950	struct f_ss_opts *opts = to_f_ss_opts(item);
 951	int ret;
 952	u8 num;
 953
 954	mutex_lock(&opts->lock);
 955	if (opts->refcnt) {
 956		ret = -EBUSY;
 957		goto end;
 958	}
 959
 960	ret = kstrtou8(page, 0, &num);
 961	if (ret)
 962		goto end;
 963
 964	if (num > 16) {
 965		ret = -EINVAL;
 966		goto end;
 967	}
 968
 969	opts->isoc_interval = num;
 970	ret = len;
 971end:
 972	mutex_unlock(&opts->lock);
 973	return ret;
 974}
 975
 976CONFIGFS_ATTR(f_ss_opts_, isoc_interval);
 977
 978static ssize_t f_ss_opts_isoc_maxpacket_show(struct config_item *item, char *page)
 979{
 980	struct f_ss_opts *opts = to_f_ss_opts(item);
 981	int result;
 982
 983	mutex_lock(&opts->lock);
 984	result = sprintf(page, "%u\n", opts->isoc_maxpacket);
 985	mutex_unlock(&opts->lock);
 986
 987	return result;
 988}
 989
 990static ssize_t f_ss_opts_isoc_maxpacket_store(struct config_item *item,
 991				       const char *page, size_t len)
 992{
 993	struct f_ss_opts *opts = to_f_ss_opts(item);
 994	int ret;
 995	u16 num;
 996
 997	mutex_lock(&opts->lock);
 998	if (opts->refcnt) {
 999		ret = -EBUSY;
1000		goto end;
1001	}
1002
1003	ret = kstrtou16(page, 0, &num);
1004	if (ret)
1005		goto end;
1006
1007	if (num > 1024) {
1008		ret = -EINVAL;
1009		goto end;
1010	}
1011
1012	opts->isoc_maxpacket = num;
1013	ret = len;
1014end:
1015	mutex_unlock(&opts->lock);
1016	return ret;
1017}
1018
1019CONFIGFS_ATTR(f_ss_opts_, isoc_maxpacket);
1020
1021static ssize_t f_ss_opts_isoc_mult_show(struct config_item *item, char *page)
1022{
1023	struct f_ss_opts *opts = to_f_ss_opts(item);
1024	int result;
1025
1026	mutex_lock(&opts->lock);
1027	result = sprintf(page, "%u\n", opts->isoc_mult);
1028	mutex_unlock(&opts->lock);
1029
1030	return result;
1031}
1032
1033static ssize_t f_ss_opts_isoc_mult_store(struct config_item *item,
1034				       const char *page, size_t len)
1035{
1036	struct f_ss_opts *opts = to_f_ss_opts(item);
1037	int ret;
1038	u8 num;
1039
1040	mutex_lock(&opts->lock);
1041	if (opts->refcnt) {
1042		ret = -EBUSY;
1043		goto end;
1044	}
1045
1046	ret = kstrtou8(page, 0, &num);
1047	if (ret)
1048		goto end;
1049
1050	if (num > 2) {
1051		ret = -EINVAL;
1052		goto end;
1053	}
1054
1055	opts->isoc_mult = num;
1056	ret = len;
1057end:
1058	mutex_unlock(&opts->lock);
1059	return ret;
1060}
1061
1062CONFIGFS_ATTR(f_ss_opts_, isoc_mult);
1063
1064static ssize_t f_ss_opts_isoc_maxburst_show(struct config_item *item, char *page)
1065{
1066	struct f_ss_opts *opts = to_f_ss_opts(item);
1067	int result;
1068
1069	mutex_lock(&opts->lock);
1070	result = sprintf(page, "%u\n", opts->isoc_maxburst);
1071	mutex_unlock(&opts->lock);
1072
1073	return result;
1074}
1075
1076static ssize_t f_ss_opts_isoc_maxburst_store(struct config_item *item,
1077				       const char *page, size_t len)
1078{
1079	struct f_ss_opts *opts = to_f_ss_opts(item);
1080	int ret;
1081	u8 num;
1082
1083	mutex_lock(&opts->lock);
1084	if (opts->refcnt) {
1085		ret = -EBUSY;
1086		goto end;
1087	}
1088
1089	ret = kstrtou8(page, 0, &num);
1090	if (ret)
1091		goto end;
1092
1093	if (num > 15) {
1094		ret = -EINVAL;
1095		goto end;
1096	}
1097
1098	opts->isoc_maxburst = num;
1099	ret = len;
1100end:
1101	mutex_unlock(&opts->lock);
1102	return ret;
1103}
1104
1105CONFIGFS_ATTR(f_ss_opts_, isoc_maxburst);
1106
1107static ssize_t f_ss_opts_bulk_buflen_show(struct config_item *item, char *page)
1108{
1109	struct f_ss_opts *opts = to_f_ss_opts(item);
1110	int result;
1111
1112	mutex_lock(&opts->lock);
1113	result = sprintf(page, "%u\n", opts->bulk_buflen);
1114	mutex_unlock(&opts->lock);
1115
1116	return result;
1117}
1118
1119static ssize_t f_ss_opts_bulk_buflen_store(struct config_item *item,
1120					   const char *page, size_t len)
1121{
1122	struct f_ss_opts *opts = to_f_ss_opts(item);
1123	int ret;
1124	u32 num;
1125
1126	mutex_lock(&opts->lock);
1127	if (opts->refcnt) {
1128		ret = -EBUSY;
1129		goto end;
1130	}
1131
1132	ret = kstrtou32(page, 0, &num);
1133	if (ret)
1134		goto end;
1135
1136	opts->bulk_buflen = num;
1137	ret = len;
1138end:
1139	mutex_unlock(&opts->lock);
1140	return ret;
1141}
1142
1143CONFIGFS_ATTR(f_ss_opts_, bulk_buflen);
1144
1145static ssize_t f_ss_opts_bulk_qlen_show(struct config_item *item, char *page)
1146{
1147	struct f_ss_opts *opts = to_f_ss_opts(item);
1148	int result;
1149
1150	mutex_lock(&opts->lock);
1151	result = sprintf(page, "%u\n", opts->bulk_qlen);
1152	mutex_unlock(&opts->lock);
1153
1154	return result;
1155}
1156
1157static ssize_t f_ss_opts_bulk_qlen_store(struct config_item *item,
1158					   const char *page, size_t len)
1159{
1160	struct f_ss_opts *opts = to_f_ss_opts(item);
1161	int ret;
1162	u32 num;
1163
1164	mutex_lock(&opts->lock);
1165	if (opts->refcnt) {
1166		ret = -EBUSY;
1167		goto end;
1168	}
1169
1170	ret = kstrtou32(page, 0, &num);
1171	if (ret)
1172		goto end;
1173
1174	opts->bulk_qlen = num;
1175	ret = len;
1176end:
1177	mutex_unlock(&opts->lock);
1178	return ret;
1179}
1180
1181CONFIGFS_ATTR(f_ss_opts_, bulk_qlen);
1182
1183static ssize_t f_ss_opts_iso_qlen_show(struct config_item *item, char *page)
1184{
1185	struct f_ss_opts *opts = to_f_ss_opts(item);
1186	int result;
1187
1188	mutex_lock(&opts->lock);
1189	result = sprintf(page, "%u\n", opts->iso_qlen);
1190	mutex_unlock(&opts->lock);
1191
1192	return result;
1193}
1194
1195static ssize_t f_ss_opts_iso_qlen_store(struct config_item *item,
1196					   const char *page, size_t len)
1197{
1198	struct f_ss_opts *opts = to_f_ss_opts(item);
1199	int ret;
1200	u32 num;
1201
1202	mutex_lock(&opts->lock);
1203	if (opts->refcnt) {
1204		ret = -EBUSY;
1205		goto end;
1206	}
1207
1208	ret = kstrtou32(page, 0, &num);
1209	if (ret)
1210		goto end;
1211
1212	opts->iso_qlen = num;
1213	ret = len;
1214end:
1215	mutex_unlock(&opts->lock);
1216	return ret;
1217}
1218
1219CONFIGFS_ATTR(f_ss_opts_, iso_qlen);
1220
1221static struct configfs_attribute *ss_attrs[] = {
1222	&f_ss_opts_attr_pattern,
1223	&f_ss_opts_attr_isoc_interval,
1224	&f_ss_opts_attr_isoc_maxpacket,
1225	&f_ss_opts_attr_isoc_mult,
1226	&f_ss_opts_attr_isoc_maxburst,
1227	&f_ss_opts_attr_bulk_buflen,
1228	&f_ss_opts_attr_bulk_qlen,
1229	&f_ss_opts_attr_iso_qlen,
1230	NULL,
1231};
1232
1233static struct config_item_type ss_func_type = {
1234	.ct_item_ops    = &ss_item_ops,
1235	.ct_attrs	= ss_attrs,
1236	.ct_owner       = THIS_MODULE,
1237};
1238
1239static void source_sink_free_instance(struct usb_function_instance *fi)
1240{
1241	struct f_ss_opts *ss_opts;
1242
1243	ss_opts = container_of(fi, struct f_ss_opts, func_inst);
1244	kfree(ss_opts);
1245}
1246
1247static struct usb_function_instance *source_sink_alloc_inst(void)
1248{
1249	struct f_ss_opts *ss_opts;
1250
1251	ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL);
1252	if (!ss_opts)
1253		return ERR_PTR(-ENOMEM);
1254	mutex_init(&ss_opts->lock);
1255	ss_opts->func_inst.free_func_inst = source_sink_free_instance;
1256	ss_opts->isoc_interval = GZERO_ISOC_INTERVAL;
1257	ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET;
1258	ss_opts->bulk_buflen = GZERO_BULK_BUFLEN;
1259	ss_opts->bulk_qlen = GZERO_SS_BULK_QLEN;
1260	ss_opts->iso_qlen = GZERO_SS_ISO_QLEN;
1261
1262	config_group_init_type_name(&ss_opts->func_inst.group, "",
1263				    &ss_func_type);
1264
1265	return &ss_opts->func_inst;
1266}
1267DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst,
1268		source_sink_alloc_func);
1269
1270static int __init sslb_modinit(void)
1271{
1272	int ret;
1273
1274	ret = usb_function_register(&SourceSinkusb_func);
1275	if (ret)
1276		return ret;
1277	ret = lb_modinit();
1278	if (ret)
1279		usb_function_unregister(&SourceSinkusb_func);
1280	return ret;
1281}
1282static void __exit sslb_modexit(void)
1283{
1284	usb_function_unregister(&SourceSinkusb_func);
1285	lb_modexit();
1286}
1287module_init(sslb_modinit);
1288module_exit(sslb_modexit);
1289
1290MODULE_LICENSE("GPL");