xref: /linux/drivers/usb/gadget/function/f_sourcesink.c (revision 4949009eb8d40a441dcddcd96e101e77d31cf1b2)
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 "gadget_chips.h"
24 #include "u_f.h"
25 
26 #define USB_MS_TO_SS_INTERVAL(x) USB_MS_TO_HS_INTERVAL(x)
27 
28 enum eptype {
29 	EP_CONTROL = 0,
30 	EP_BULK,
31 	EP_ISOC,
32 	EP_INTERRUPT,
33 };
34 
35 /*
36  * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
37  * controller drivers.
38  *
39  * This just sinks bulk packets OUT to the peripheral and sources them IN
40  * to the host, optionally with specific data patterns for integrity tests.
41  * As such it supports basic functionality and load tests.
42  *
43  * In terms of control messaging, this supports all the standard requests
44  * plus two that support control-OUT tests.  If the optional "autoresume"
45  * mode is enabled, it provides good functional coverage for the "USBCV"
46  * test harness from USB-IF.
47  *
48  * Note that because this doesn't queue more than one request at a time,
49  * some other function must be used to test queueing logic.  The network
50  * link (g_ether) is the best overall option for that, since its TX and RX
51  * queues are relatively independent, will receive a range of packet sizes,
52  * and can often be made to run out completely.  Those issues are important
53  * when stress testing peripheral controller drivers.
54  *
55  *
56  * This is currently packaged as a configuration driver, which can't be
57  * combined with other functions to make composite devices.  However, it
58  * can be combined with other independent configurations.
59  */
60 struct f_sourcesink {
61 	struct usb_function	function;
62 
63 	struct usb_ep		*in_ep;
64 	struct usb_ep		*out_ep;
65 	struct usb_ep		*iso_in_ep;
66 	struct usb_ep		*iso_out_ep;
67 	struct usb_ep		*int_in_ep;
68 	struct usb_ep		*int_out_ep;
69 	int			cur_alt;
70 };
71 
72 static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
73 {
74 	return container_of(f, struct f_sourcesink, function);
75 }
76 
77 static unsigned pattern;
78 static unsigned isoc_interval;
79 static unsigned isoc_maxpacket;
80 static unsigned isoc_mult;
81 static unsigned isoc_maxburst;
82 static unsigned int_interval; /* In ms */
83 static unsigned int_maxpacket;
84 static unsigned int_mult;
85 static unsigned int_maxburst;
86 static unsigned buflen;
87 
88 /*-------------------------------------------------------------------------*/
89 
90 static struct usb_interface_descriptor source_sink_intf_alt0 = {
91 	.bLength =		USB_DT_INTERFACE_SIZE,
92 	.bDescriptorType =	USB_DT_INTERFACE,
93 
94 	.bAlternateSetting =	0,
95 	.bNumEndpoints =	2,
96 	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
97 	/* .iInterface		= DYNAMIC */
98 };
99 
100 static struct usb_interface_descriptor source_sink_intf_alt1 = {
101 	.bLength =		USB_DT_INTERFACE_SIZE,
102 	.bDescriptorType =	USB_DT_INTERFACE,
103 
104 	.bAlternateSetting =	1,
105 	.bNumEndpoints =	4,
106 	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
107 	/* .iInterface		= DYNAMIC */
108 };
109 
110 static struct usb_interface_descriptor source_sink_intf_alt2 = {
111 	.bLength =		USB_DT_INTERFACE_SIZE,
112 	.bDescriptorType =	USB_DT_INTERFACE,
113 
114 	.bAlternateSetting =	2,
115 	.bNumEndpoints =	2,
116 	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
117 	/* .iInterface		= DYNAMIC */
118 };
119 
120 /* full speed support: */
121 
122 static struct usb_endpoint_descriptor fs_source_desc = {
123 	.bLength =		USB_DT_ENDPOINT_SIZE,
124 	.bDescriptorType =	USB_DT_ENDPOINT,
125 
126 	.bEndpointAddress =	USB_DIR_IN,
127 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
128 };
129 
130 static struct usb_endpoint_descriptor fs_sink_desc = {
131 	.bLength =		USB_DT_ENDPOINT_SIZE,
132 	.bDescriptorType =	USB_DT_ENDPOINT,
133 
134 	.bEndpointAddress =	USB_DIR_OUT,
135 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
136 };
137 
138 static struct usb_endpoint_descriptor fs_iso_source_desc = {
139 	.bLength =		USB_DT_ENDPOINT_SIZE,
140 	.bDescriptorType =	USB_DT_ENDPOINT,
141 
142 	.bEndpointAddress =	USB_DIR_IN,
143 	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
144 	.wMaxPacketSize =	cpu_to_le16(1023),
145 	.bInterval =		4,
146 };
147 
148 static struct usb_endpoint_descriptor fs_iso_sink_desc = {
149 	.bLength =		USB_DT_ENDPOINT_SIZE,
150 	.bDescriptorType =	USB_DT_ENDPOINT,
151 
152 	.bEndpointAddress =	USB_DIR_OUT,
153 	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
154 	.wMaxPacketSize =	cpu_to_le16(1023),
155 	.bInterval =		4,
156 };
157 
158 static struct usb_endpoint_descriptor fs_int_source_desc = {
159 	.bLength =		USB_DT_ENDPOINT_SIZE,
160 	.bDescriptorType =	USB_DT_ENDPOINT,
161 
162 	.bEndpointAddress =	USB_DIR_IN,
163 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
164 	.wMaxPacketSize =	cpu_to_le16(64),
165 	.bInterval =		GZERO_INT_INTERVAL,
166 };
167 
168 static struct usb_endpoint_descriptor fs_int_sink_desc = {
169 	.bLength =		USB_DT_ENDPOINT_SIZE,
170 	.bDescriptorType =	USB_DT_ENDPOINT,
171 
172 	.bEndpointAddress =	USB_DIR_OUT,
173 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
174 	.wMaxPacketSize =	cpu_to_le16(64),
175 	.bInterval =		GZERO_INT_INTERVAL,
176 };
177 
178 static struct usb_descriptor_header *fs_source_sink_descs[] = {
179 	(struct usb_descriptor_header *) &source_sink_intf_alt0,
180 	(struct usb_descriptor_header *) &fs_sink_desc,
181 	(struct usb_descriptor_header *) &fs_source_desc,
182 	(struct usb_descriptor_header *) &source_sink_intf_alt1,
183 #define FS_ALT_IFC_1_OFFSET	3
184 	(struct usb_descriptor_header *) &fs_sink_desc,
185 	(struct usb_descriptor_header *) &fs_source_desc,
186 	(struct usb_descriptor_header *) &fs_iso_sink_desc,
187 	(struct usb_descriptor_header *) &fs_iso_source_desc,
188 	(struct usb_descriptor_header *) &source_sink_intf_alt2,
189 #define FS_ALT_IFC_2_OFFSET	8
190 	(struct usb_descriptor_header *) &fs_int_sink_desc,
191 	(struct usb_descriptor_header *) &fs_int_source_desc,
192 	NULL,
193 };
194 
195 /* high speed support: */
196 
197 static struct usb_endpoint_descriptor hs_source_desc = {
198 	.bLength =		USB_DT_ENDPOINT_SIZE,
199 	.bDescriptorType =	USB_DT_ENDPOINT,
200 
201 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
202 	.wMaxPacketSize =	cpu_to_le16(512),
203 };
204 
205 static struct usb_endpoint_descriptor hs_sink_desc = {
206 	.bLength =		USB_DT_ENDPOINT_SIZE,
207 	.bDescriptorType =	USB_DT_ENDPOINT,
208 
209 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
210 	.wMaxPacketSize =	cpu_to_le16(512),
211 };
212 
213 static struct usb_endpoint_descriptor hs_iso_source_desc = {
214 	.bLength =		USB_DT_ENDPOINT_SIZE,
215 	.bDescriptorType =	USB_DT_ENDPOINT,
216 
217 	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
218 	.wMaxPacketSize =	cpu_to_le16(1024),
219 	.bInterval =		4,
220 };
221 
222 static struct usb_endpoint_descriptor hs_iso_sink_desc = {
223 	.bLength =		USB_DT_ENDPOINT_SIZE,
224 	.bDescriptorType =	USB_DT_ENDPOINT,
225 
226 	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
227 	.wMaxPacketSize =	cpu_to_le16(1024),
228 	.bInterval =		4,
229 };
230 
231 static struct usb_endpoint_descriptor hs_int_source_desc = {
232 	.bLength =		USB_DT_ENDPOINT_SIZE,
233 	.bDescriptorType =	USB_DT_ENDPOINT,
234 
235 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
236 	.wMaxPacketSize =	cpu_to_le16(1024),
237 	.bInterval =		USB_MS_TO_HS_INTERVAL(GZERO_INT_INTERVAL),
238 };
239 
240 static struct usb_endpoint_descriptor hs_int_sink_desc = {
241 	.bLength =		USB_DT_ENDPOINT_SIZE,
242 	.bDescriptorType =	USB_DT_ENDPOINT,
243 
244 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
245 	.wMaxPacketSize =	cpu_to_le16(1024),
246 	.bInterval =		USB_MS_TO_HS_INTERVAL(GZERO_INT_INTERVAL),
247 };
248 
249 static struct usb_descriptor_header *hs_source_sink_descs[] = {
250 	(struct usb_descriptor_header *) &source_sink_intf_alt0,
251 	(struct usb_descriptor_header *) &hs_source_desc,
252 	(struct usb_descriptor_header *) &hs_sink_desc,
253 	(struct usb_descriptor_header *) &source_sink_intf_alt1,
254 #define HS_ALT_IFC_1_OFFSET	3
255 	(struct usb_descriptor_header *) &hs_source_desc,
256 	(struct usb_descriptor_header *) &hs_sink_desc,
257 	(struct usb_descriptor_header *) &hs_iso_source_desc,
258 	(struct usb_descriptor_header *) &hs_iso_sink_desc,
259 	(struct usb_descriptor_header *) &source_sink_intf_alt2,
260 #define HS_ALT_IFC_2_OFFSET	8
261 	(struct usb_descriptor_header *) &hs_int_source_desc,
262 	(struct usb_descriptor_header *) &hs_int_sink_desc,
263 	NULL,
264 };
265 
266 /* super speed support: */
267 
268 static struct usb_endpoint_descriptor ss_source_desc = {
269 	.bLength =		USB_DT_ENDPOINT_SIZE,
270 	.bDescriptorType =	USB_DT_ENDPOINT,
271 
272 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
273 	.wMaxPacketSize =	cpu_to_le16(1024),
274 };
275 
276 static struct usb_ss_ep_comp_descriptor ss_source_comp_desc = {
277 	.bLength =		USB_DT_SS_EP_COMP_SIZE,
278 	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
279 
280 	.bMaxBurst =		0,
281 	.bmAttributes =		0,
282 	.wBytesPerInterval =	0,
283 };
284 
285 static struct usb_endpoint_descriptor ss_sink_desc = {
286 	.bLength =		USB_DT_ENDPOINT_SIZE,
287 	.bDescriptorType =	USB_DT_ENDPOINT,
288 
289 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
290 	.wMaxPacketSize =	cpu_to_le16(1024),
291 };
292 
293 static struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = {
294 	.bLength =		USB_DT_SS_EP_COMP_SIZE,
295 	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
296 
297 	.bMaxBurst =		0,
298 	.bmAttributes =		0,
299 	.wBytesPerInterval =	0,
300 };
301 
302 static struct usb_endpoint_descriptor ss_iso_source_desc = {
303 	.bLength =		USB_DT_ENDPOINT_SIZE,
304 	.bDescriptorType =	USB_DT_ENDPOINT,
305 
306 	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
307 	.wMaxPacketSize =	cpu_to_le16(1024),
308 	.bInterval =		4,
309 };
310 
311 static struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc = {
312 	.bLength =		USB_DT_SS_EP_COMP_SIZE,
313 	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
314 
315 	.bMaxBurst =		0,
316 	.bmAttributes =		0,
317 	.wBytesPerInterval =	cpu_to_le16(1024),
318 };
319 
320 static struct usb_endpoint_descriptor ss_iso_sink_desc = {
321 	.bLength =		USB_DT_ENDPOINT_SIZE,
322 	.bDescriptorType =	USB_DT_ENDPOINT,
323 
324 	.bmAttributes =		USB_ENDPOINT_XFER_ISOC,
325 	.wMaxPacketSize =	cpu_to_le16(1024),
326 	.bInterval =		4,
327 };
328 
329 static struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = {
330 	.bLength =		USB_DT_SS_EP_COMP_SIZE,
331 	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
332 
333 	.bMaxBurst =		0,
334 	.bmAttributes =		0,
335 	.wBytesPerInterval =	cpu_to_le16(1024),
336 };
337 
338 static struct usb_endpoint_descriptor ss_int_source_desc = {
339 	.bLength =		USB_DT_ENDPOINT_SIZE,
340 	.bDescriptorType =	USB_DT_ENDPOINT,
341 
342 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
343 	.wMaxPacketSize =	cpu_to_le16(1024),
344 	.bInterval =		USB_MS_TO_SS_INTERVAL(GZERO_INT_INTERVAL),
345 };
346 
347 struct usb_ss_ep_comp_descriptor ss_int_source_comp_desc = {
348 	.bLength =		USB_DT_SS_EP_COMP_SIZE,
349 	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
350 
351 	.bMaxBurst =		0,
352 	.bmAttributes =		0,
353 	.wBytesPerInterval =	cpu_to_le16(1024),
354 };
355 
356 static struct usb_endpoint_descriptor ss_int_sink_desc = {
357 	.bLength =		USB_DT_ENDPOINT_SIZE,
358 	.bDescriptorType =	USB_DT_ENDPOINT,
359 
360 	.bmAttributes =		USB_ENDPOINT_XFER_INT,
361 	.wMaxPacketSize =	cpu_to_le16(1024),
362 	.bInterval =		USB_MS_TO_SS_INTERVAL(GZERO_INT_INTERVAL),
363 };
364 
365 struct usb_ss_ep_comp_descriptor ss_int_sink_comp_desc = {
366 	.bLength =		USB_DT_SS_EP_COMP_SIZE,
367 	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
368 
369 	.bMaxBurst =		0,
370 	.bmAttributes =		0,
371 	.wBytesPerInterval =	cpu_to_le16(1024),
372 };
373 
374 static struct usb_descriptor_header *ss_source_sink_descs[] = {
375 	(struct usb_descriptor_header *) &source_sink_intf_alt0,
376 	(struct usb_descriptor_header *) &ss_source_desc,
377 	(struct usb_descriptor_header *) &ss_source_comp_desc,
378 	(struct usb_descriptor_header *) &ss_sink_desc,
379 	(struct usb_descriptor_header *) &ss_sink_comp_desc,
380 	(struct usb_descriptor_header *) &source_sink_intf_alt1,
381 #define SS_ALT_IFC_1_OFFSET	5
382 	(struct usb_descriptor_header *) &ss_source_desc,
383 	(struct usb_descriptor_header *) &ss_source_comp_desc,
384 	(struct usb_descriptor_header *) &ss_sink_desc,
385 	(struct usb_descriptor_header *) &ss_sink_comp_desc,
386 	(struct usb_descriptor_header *) &ss_iso_source_desc,
387 	(struct usb_descriptor_header *) &ss_iso_source_comp_desc,
388 	(struct usb_descriptor_header *) &ss_iso_sink_desc,
389 	(struct usb_descriptor_header *) &ss_iso_sink_comp_desc,
390 	(struct usb_descriptor_header *) &source_sink_intf_alt2,
391 #define SS_ALT_IFC_2_OFFSET	14
392 	(struct usb_descriptor_header *) &ss_int_source_desc,
393 	(struct usb_descriptor_header *) &ss_int_source_comp_desc,
394 	(struct usb_descriptor_header *) &ss_int_sink_desc,
395 	(struct usb_descriptor_header *) &ss_int_sink_comp_desc,
396 	NULL,
397 };
398 
399 /* function-specific strings: */
400 
401 static struct usb_string strings_sourcesink[] = {
402 	[0].s = "source and sink data",
403 	{  }			/* end of list */
404 };
405 
406 static struct usb_gadget_strings stringtab_sourcesink = {
407 	.language	= 0x0409,	/* en-us */
408 	.strings	= strings_sourcesink,
409 };
410 
411 static struct usb_gadget_strings *sourcesink_strings[] = {
412 	&stringtab_sourcesink,
413 	NULL,
414 };
415 
416 /*-------------------------------------------------------------------------*/
417 static const char *get_ep_string(enum eptype ep_type)
418 {
419 	switch (ep_type) {
420 	case EP_ISOC:
421 		return "ISOC-";
422 	case EP_INTERRUPT:
423 		return "INTERRUPT-";
424 	case EP_CONTROL:
425 		return "CTRL-";
426 	case EP_BULK:
427 		return "BULK-";
428 	default:
429 		return "UNKNOWN-";
430 	}
431 }
432 
433 static inline struct usb_request *ss_alloc_ep_req(struct usb_ep *ep, int len)
434 {
435 	return alloc_ep_req(ep, len, buflen);
436 }
437 
438 void free_ep_req(struct usb_ep *ep, struct usb_request *req)
439 {
440 	kfree(req->buf);
441 	usb_ep_free_request(ep, req);
442 }
443 
444 static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
445 {
446 	int			value;
447 
448 	if (ep->driver_data) {
449 		value = usb_ep_disable(ep);
450 		if (value < 0)
451 			DBG(cdev, "disable %s --> %d\n",
452 					ep->name, value);
453 		ep->driver_data = NULL;
454 	}
455 }
456 
457 void disable_endpoints(struct usb_composite_dev *cdev,
458 		struct usb_ep *in, struct usb_ep *out,
459 		struct usb_ep *iso_in, struct usb_ep *iso_out,
460 		struct usb_ep *int_in, struct usb_ep *int_out)
461 {
462 	disable_ep(cdev, in);
463 	disable_ep(cdev, out);
464 	if (iso_in)
465 		disable_ep(cdev, iso_in);
466 	if (iso_out)
467 		disable_ep(cdev, iso_out);
468 	if (int_in)
469 		disable_ep(cdev, int_in);
470 	if (int_out)
471 		disable_ep(cdev, int_out);
472 }
473 
474 static int
475 sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
476 {
477 	struct usb_composite_dev *cdev = c->cdev;
478 	struct f_sourcesink	*ss = func_to_ss(f);
479 	int	id;
480 	int ret;
481 
482 	/* allocate interface ID(s) */
483 	id = usb_interface_id(c, f);
484 	if (id < 0)
485 		return id;
486 	source_sink_intf_alt0.bInterfaceNumber = id;
487 	source_sink_intf_alt1.bInterfaceNumber = id;
488 	source_sink_intf_alt2.bInterfaceNumber = id;
489 
490 	/* allocate bulk endpoints */
491 	ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
492 	if (!ss->in_ep) {
493 autoconf_fail:
494 		ERROR(cdev, "%s: can't autoconfigure on %s\n",
495 			f->name, cdev->gadget->name);
496 		return -ENODEV;
497 	}
498 	ss->in_ep->driver_data = cdev;	/* claim */
499 
500 	ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
501 	if (!ss->out_ep)
502 		goto autoconf_fail;
503 	ss->out_ep->driver_data = cdev;	/* claim */
504 
505 	/* sanity check the isoc module parameters */
506 	if (isoc_interval < 1)
507 		isoc_interval = 1;
508 	if (isoc_interval > 16)
509 		isoc_interval = 16;
510 	if (isoc_mult > 2)
511 		isoc_mult = 2;
512 	if (isoc_maxburst > 15)
513 		isoc_maxburst = 15;
514 
515 	/* fill in the FS isoc descriptors from the module parameters */
516 	fs_iso_source_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
517 						1023 : isoc_maxpacket;
518 	fs_iso_source_desc.bInterval = isoc_interval;
519 	fs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
520 						1023 : isoc_maxpacket;
521 	fs_iso_sink_desc.bInterval = isoc_interval;
522 
523 	/* allocate iso endpoints */
524 	ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
525 	if (!ss->iso_in_ep)
526 		goto no_iso;
527 	ss->iso_in_ep->driver_data = cdev;	/* claim */
528 
529 	ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
530 	if (ss->iso_out_ep) {
531 		ss->iso_out_ep->driver_data = cdev;	/* claim */
532 	} else {
533 		ss->iso_in_ep->driver_data = NULL;
534 		ss->iso_in_ep = NULL;
535 no_iso:
536 		/*
537 		 * We still want to work even if the UDC doesn't have isoc
538 		 * endpoints, so null out the alt interface that contains
539 		 * them and continue.
540 		 */
541 		fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
542 		hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
543 		ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
544 	}
545 
546 	if (isoc_maxpacket > 1024)
547 		isoc_maxpacket = 1024;
548 
549 	/* sanity check the interrupt module parameters */
550 	if (int_interval < 1)
551 		int_interval = 1;
552 	if (int_interval > 4096)
553 		int_interval = 4096;
554 	if (int_mult > 2)
555 		int_mult = 2;
556 	if (int_maxburst > 15)
557 		int_maxburst = 15;
558 
559 	/* fill in the FS interrupt descriptors from the module parameters */
560 	fs_int_source_desc.wMaxPacketSize = int_maxpacket > 64 ?
561 						64 : int_maxpacket;
562 	fs_int_source_desc.bInterval = int_interval > 255 ?
563 						255 : int_interval;
564 	fs_int_sink_desc.wMaxPacketSize = int_maxpacket > 64 ?
565 						64 : int_maxpacket;
566 	fs_int_sink_desc.bInterval = int_interval > 255 ?
567 						255 : int_interval;
568 
569 	/* allocate int endpoints */
570 	ss->int_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_int_source_desc);
571 	if (!ss->int_in_ep)
572 		goto no_int;
573 	ss->int_in_ep->driver_data = cdev;	/* claim */
574 
575 	ss->int_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_int_sink_desc);
576 	if (ss->int_out_ep) {
577 		ss->int_out_ep->driver_data = cdev;	/* claim */
578 	} else {
579 		ss->int_in_ep->driver_data = NULL;
580 		ss->int_in_ep = NULL;
581 no_int:
582 		fs_source_sink_descs[FS_ALT_IFC_2_OFFSET] = NULL;
583 		hs_source_sink_descs[HS_ALT_IFC_2_OFFSET] = NULL;
584 		ss_source_sink_descs[SS_ALT_IFC_2_OFFSET] = NULL;
585 	}
586 
587 	if (int_maxpacket > 1024)
588 		int_maxpacket = 1024;
589 
590 	/* support high speed hardware */
591 	hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
592 	hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
593 
594 	/*
595 	 * Fill in the HS isoc and interrupt descriptors from the module
596 	 * parameters. We assume that the user knows what they are doing and
597 	 * won't give parameters that their UDC doesn't support.
598 	 */
599 	hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
600 	hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11;
601 	hs_iso_source_desc.bInterval = isoc_interval;
602 	hs_iso_source_desc.bEndpointAddress =
603 		fs_iso_source_desc.bEndpointAddress;
604 
605 	hs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
606 	hs_iso_sink_desc.wMaxPacketSize |= isoc_mult << 11;
607 	hs_iso_sink_desc.bInterval = isoc_interval;
608 	hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
609 
610 	hs_int_source_desc.wMaxPacketSize = int_maxpacket;
611 	hs_int_source_desc.wMaxPacketSize |= int_mult << 11;
612 	hs_int_source_desc.bInterval = USB_MS_TO_HS_INTERVAL(int_interval);
613 	hs_int_source_desc.bEndpointAddress =
614 		fs_int_source_desc.bEndpointAddress;
615 
616 	hs_int_sink_desc.wMaxPacketSize = int_maxpacket;
617 	hs_int_sink_desc.wMaxPacketSize |= int_mult << 11;
618 	hs_int_sink_desc.bInterval = USB_MS_TO_HS_INTERVAL(int_interval);
619 	hs_int_sink_desc.bEndpointAddress = fs_int_sink_desc.bEndpointAddress;
620 
621 	/* support super speed hardware */
622 	ss_source_desc.bEndpointAddress =
623 		fs_source_desc.bEndpointAddress;
624 	ss_sink_desc.bEndpointAddress =
625 		fs_sink_desc.bEndpointAddress;
626 
627 	/*
628 	 * Fill in the SS isoc and interrupt descriptors from the module
629 	 * parameters. We assume that the user knows what they are doing and
630 	 * won't give parameters that their UDC doesn't support.
631 	 */
632 	ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
633 	ss_iso_source_desc.bInterval = isoc_interval;
634 	ss_iso_source_comp_desc.bmAttributes = isoc_mult;
635 	ss_iso_source_comp_desc.bMaxBurst = isoc_maxburst;
636 	ss_iso_source_comp_desc.wBytesPerInterval =
637 		isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
638 	ss_iso_source_desc.bEndpointAddress =
639 		fs_iso_source_desc.bEndpointAddress;
640 
641 	ss_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
642 	ss_iso_sink_desc.bInterval = isoc_interval;
643 	ss_iso_sink_comp_desc.bmAttributes = isoc_mult;
644 	ss_iso_sink_comp_desc.bMaxBurst = isoc_maxburst;
645 	ss_iso_sink_comp_desc.wBytesPerInterval =
646 		isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
647 	ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
648 
649 	ss_int_source_desc.wMaxPacketSize = int_maxpacket;
650 	ss_int_source_desc.bInterval = USB_MS_TO_SS_INTERVAL(int_interval);
651 	ss_int_source_comp_desc.bmAttributes = int_mult;
652 	ss_int_source_comp_desc.bMaxBurst = int_maxburst;
653 	ss_int_source_comp_desc.wBytesPerInterval =
654 		int_maxpacket * (int_mult + 1) * (int_maxburst + 1);
655 	ss_int_source_desc.bEndpointAddress =
656 		fs_int_source_desc.bEndpointAddress;
657 
658 	ss_int_sink_desc.wMaxPacketSize = int_maxpacket;
659 	ss_int_sink_desc.bInterval = USB_MS_TO_SS_INTERVAL(int_interval);
660 	ss_int_sink_comp_desc.bmAttributes = int_mult;
661 	ss_int_sink_comp_desc.bMaxBurst = int_maxburst;
662 	ss_int_sink_comp_desc.wBytesPerInterval =
663 		int_maxpacket * (int_mult + 1) * (int_maxburst + 1);
664 	ss_int_sink_desc.bEndpointAddress = fs_int_sink_desc.bEndpointAddress;
665 
666 	ret = usb_assign_descriptors(f, fs_source_sink_descs,
667 			hs_source_sink_descs, ss_source_sink_descs);
668 	if (ret)
669 		return ret;
670 
671 	DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s, "
672 			"INT-IN/%s, INT-OUT/%s\n",
673 	    (gadget_is_superspeed(c->cdev->gadget) ? "super" :
674 	     (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
675 			f->name, ss->in_ep->name, ss->out_ep->name,
676 			ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
677 			ss->iso_out_ep ? ss->iso_out_ep->name : "<none>",
678 			ss->int_in_ep ? ss->int_in_ep->name : "<none>",
679 			ss->int_out_ep ? ss->int_out_ep->name : "<none>");
680 	return 0;
681 }
682 
683 static void
684 sourcesink_free_func(struct usb_function *f)
685 {
686 	struct f_ss_opts *opts;
687 
688 	opts = container_of(f->fi, struct f_ss_opts, func_inst);
689 
690 	mutex_lock(&opts->lock);
691 	opts->refcnt--;
692 	mutex_unlock(&opts->lock);
693 
694 	usb_free_all_descriptors(f);
695 	kfree(func_to_ss(f));
696 }
697 
698 /* optionally require specific source/sink data patterns  */
699 static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
700 {
701 	unsigned		i;
702 	u8			*buf = req->buf;
703 	struct usb_composite_dev *cdev = ss->function.config->cdev;
704 
705 	if (pattern == 2)
706 		return 0;
707 
708 	for (i = 0; i < req->actual; i++, buf++) {
709 		switch (pattern) {
710 
711 		/* all-zeroes has no synchronization issues */
712 		case 0:
713 			if (*buf == 0)
714 				continue;
715 			break;
716 
717 		/* "mod63" stays in sync with short-terminated transfers,
718 		 * OR otherwise when host and gadget agree on how large
719 		 * each usb transfer request should be.  Resync is done
720 		 * with set_interface or set_config.  (We *WANT* it to
721 		 * get quickly out of sync if controllers or their drivers
722 		 * stutter for any reason, including buffer duplication...)
723 		 */
724 		case 1:
725 			if (*buf == (u8)(i % 63))
726 				continue;
727 			break;
728 		}
729 		ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
730 		usb_ep_set_halt(ss->out_ep);
731 		return -EINVAL;
732 	}
733 	return 0;
734 }
735 
736 static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
737 {
738 	unsigned	i;
739 	u8		*buf = req->buf;
740 
741 	switch (pattern) {
742 	case 0:
743 		memset(req->buf, 0, req->length);
744 		break;
745 	case 1:
746 		for  (i = 0; i < req->length; i++)
747 			*buf++ = (u8) (i % 63);
748 		break;
749 	case 2:
750 		break;
751 	}
752 }
753 
754 static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
755 {
756 	struct usb_composite_dev	*cdev;
757 	struct f_sourcesink		*ss = ep->driver_data;
758 	int				status = req->status;
759 
760 	/* driver_data will be null if ep has been disabled */
761 	if (!ss)
762 		return;
763 
764 	cdev = ss->function.config->cdev;
765 
766 	switch (status) {
767 
768 	case 0:				/* normal completion? */
769 		if (ep == ss->out_ep) {
770 			check_read_data(ss, req);
771 			if (pattern != 2)
772 				memset(req->buf, 0x55, req->length);
773 		}
774 		break;
775 
776 	/* this endpoint is normally active while we're configured */
777 	case -ECONNABORTED:		/* hardware forced ep reset */
778 	case -ECONNRESET:		/* request dequeued */
779 	case -ESHUTDOWN:		/* disconnect from host */
780 		VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
781 				req->actual, req->length);
782 		if (ep == ss->out_ep)
783 			check_read_data(ss, req);
784 		free_ep_req(ep, req);
785 		return;
786 
787 	case -EOVERFLOW:		/* buffer overrun on read means that
788 					 * we didn't provide a big enough
789 					 * buffer.
790 					 */
791 	default:
792 #if 1
793 		DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
794 				status, req->actual, req->length);
795 #endif
796 	case -EREMOTEIO:		/* short read */
797 		break;
798 	}
799 
800 	status = usb_ep_queue(ep, req, GFP_ATOMIC);
801 	if (status) {
802 		ERROR(cdev, "kill %s:  resubmit %d bytes --> %d\n",
803 				ep->name, req->length, status);
804 		usb_ep_set_halt(ep);
805 		/* FIXME recover later ... somehow */
806 	}
807 }
808 
809 static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
810 		enum eptype ep_type, int speed)
811 {
812 	struct usb_ep		*ep;
813 	struct usb_request	*req;
814 	int			i, size, status;
815 
816 	for (i = 0; i < 8; i++) {
817 		switch (ep_type) {
818 		case EP_ISOC:
819 			switch (speed) {
820 			case USB_SPEED_SUPER:
821 				size = isoc_maxpacket * (isoc_mult + 1) *
822 						(isoc_maxburst + 1);
823 				break;
824 			case USB_SPEED_HIGH:
825 				size = isoc_maxpacket * (isoc_mult + 1);
826 				break;
827 			default:
828 				size = isoc_maxpacket > 1023 ?
829 						1023 : isoc_maxpacket;
830 				break;
831 			}
832 			ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
833 			req = ss_alloc_ep_req(ep, size);
834 			break;
835 		case EP_INTERRUPT:
836 			switch (speed) {
837 			case USB_SPEED_SUPER:
838 				size = int_maxpacket * (int_mult + 1) *
839 						(int_maxburst + 1);
840 				break;
841 			case USB_SPEED_HIGH:
842 				size = int_maxpacket * (int_mult + 1);
843 				break;
844 			default:
845 				size = int_maxpacket > 1023 ?
846 						1023 : int_maxpacket;
847 				break;
848 			}
849 			ep = is_in ? ss->int_in_ep : ss->int_out_ep;
850 			req = ss_alloc_ep_req(ep, size);
851 			break;
852 		default:
853 			ep = is_in ? ss->in_ep : ss->out_ep;
854 			req = ss_alloc_ep_req(ep, 0);
855 			break;
856 		}
857 
858 		if (!req)
859 			return -ENOMEM;
860 
861 		req->complete = source_sink_complete;
862 		if (is_in)
863 			reinit_write_data(ep, req);
864 		else if (pattern != 2)
865 			memset(req->buf, 0x55, req->length);
866 
867 		status = usb_ep_queue(ep, req, GFP_ATOMIC);
868 		if (status) {
869 			struct usb_composite_dev	*cdev;
870 
871 			cdev = ss->function.config->cdev;
872 			ERROR(cdev, "start %s%s %s --> %d\n",
873 				get_ep_string(ep_type), is_in ? "IN" : "OUT",
874 				ep->name, status);
875 			free_ep_req(ep, req);
876 		}
877 
878 		if (!(ep_type == EP_ISOC))
879 			break;
880 	}
881 
882 	return status;
883 }
884 
885 static void disable_source_sink(struct f_sourcesink *ss)
886 {
887 	struct usb_composite_dev	*cdev;
888 
889 	cdev = ss->function.config->cdev;
890 	disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
891 			ss->iso_out_ep, ss->int_in_ep, ss->int_out_ep);
892 	VDBG(cdev, "%s disabled\n", ss->function.name);
893 }
894 
895 static int
896 enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
897 		int alt)
898 {
899 	int					result = 0;
900 	int					speed = cdev->gadget->speed;
901 	struct usb_ep				*ep;
902 
903 	if (alt == 2) {
904 		/* Configure for periodic interrupt endpoint */
905 		ep = ss->int_in_ep;
906 		if (ep) {
907 			result = config_ep_by_speed(cdev->gadget,
908 					&(ss->function), ep);
909 			if (result)
910 				return result;
911 
912 			result = usb_ep_enable(ep);
913 			if (result < 0)
914 				return result;
915 
916 			ep->driver_data = ss;
917 			result = source_sink_start_ep(ss, true, EP_INTERRUPT,
918 					speed);
919 			if (result < 0) {
920 fail1:
921 				ep = ss->int_in_ep;
922 				if (ep) {
923 					usb_ep_disable(ep);
924 					ep->driver_data = NULL;
925 				}
926 				return result;
927 			}
928 		}
929 
930 		/*
931 		 * one interrupt endpoint reads (sinks) anything OUT (from the
932 		 * host)
933 		 */
934 		ep = ss->int_out_ep;
935 		if (ep) {
936 			result = config_ep_by_speed(cdev->gadget,
937 					&(ss->function), ep);
938 			if (result)
939 				goto fail1;
940 
941 			result = usb_ep_enable(ep);
942 			if (result < 0)
943 				goto fail1;
944 
945 			ep->driver_data = ss;
946 			result = source_sink_start_ep(ss, false, EP_INTERRUPT,
947 					speed);
948 			if (result < 0) {
949 				ep = ss->int_out_ep;
950 				usb_ep_disable(ep);
951 				ep->driver_data = NULL;
952 				goto fail1;
953 			}
954 		}
955 
956 		goto out;
957 	}
958 
959 	/* one bulk endpoint writes (sources) zeroes IN (to the host) */
960 	ep = ss->in_ep;
961 	result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
962 	if (result)
963 		return result;
964 	result = usb_ep_enable(ep);
965 	if (result < 0)
966 		return result;
967 	ep->driver_data = ss;
968 
969 	result = source_sink_start_ep(ss, true, EP_BULK, speed);
970 	if (result < 0) {
971 fail:
972 		ep = ss->in_ep;
973 		usb_ep_disable(ep);
974 		ep->driver_data = NULL;
975 		return result;
976 	}
977 
978 	/* one bulk endpoint reads (sinks) anything OUT (from the host) */
979 	ep = ss->out_ep;
980 	result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
981 	if (result)
982 		goto fail;
983 	result = usb_ep_enable(ep);
984 	if (result < 0)
985 		goto fail;
986 	ep->driver_data = ss;
987 
988 	result = source_sink_start_ep(ss, false, EP_BULK, speed);
989 	if (result < 0) {
990 fail2:
991 		ep = ss->out_ep;
992 		usb_ep_disable(ep);
993 		ep->driver_data = NULL;
994 		goto fail;
995 	}
996 
997 	if (alt == 0)
998 		goto out;
999 
1000 	/* one iso endpoint writes (sources) zeroes IN (to the host) */
1001 	ep = ss->iso_in_ep;
1002 	if (ep) {
1003 		result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
1004 		if (result)
1005 			goto fail2;
1006 		result = usb_ep_enable(ep);
1007 		if (result < 0)
1008 			goto fail2;
1009 		ep->driver_data = ss;
1010 
1011 		result = source_sink_start_ep(ss, true, EP_ISOC, speed);
1012 		if (result < 0) {
1013 fail3:
1014 			ep = ss->iso_in_ep;
1015 			if (ep) {
1016 				usb_ep_disable(ep);
1017 				ep->driver_data = NULL;
1018 			}
1019 			goto fail2;
1020 		}
1021 	}
1022 
1023 	/* one iso endpoint reads (sinks) anything OUT (from the host) */
1024 	ep = ss->iso_out_ep;
1025 	if (ep) {
1026 		result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
1027 		if (result)
1028 			goto fail3;
1029 		result = usb_ep_enable(ep);
1030 		if (result < 0)
1031 			goto fail3;
1032 		ep->driver_data = ss;
1033 
1034 		result = source_sink_start_ep(ss, false, EP_ISOC, speed);
1035 		if (result < 0) {
1036 			usb_ep_disable(ep);
1037 			ep->driver_data = NULL;
1038 			goto fail3;
1039 		}
1040 	}
1041 
1042 out:
1043 	ss->cur_alt = alt;
1044 
1045 	DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
1046 	return result;
1047 }
1048 
1049 static int sourcesink_set_alt(struct usb_function *f,
1050 		unsigned intf, unsigned alt)
1051 {
1052 	struct f_sourcesink		*ss = func_to_ss(f);
1053 	struct usb_composite_dev	*cdev = f->config->cdev;
1054 
1055 	if (ss->in_ep->driver_data)
1056 		disable_source_sink(ss);
1057 	else if (alt == 2 && ss->int_in_ep->driver_data)
1058 		disable_source_sink(ss);
1059 	return enable_source_sink(cdev, ss, alt);
1060 }
1061 
1062 static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
1063 {
1064 	struct f_sourcesink		*ss = func_to_ss(f);
1065 
1066 	return ss->cur_alt;
1067 }
1068 
1069 static void sourcesink_disable(struct usb_function *f)
1070 {
1071 	struct f_sourcesink	*ss = func_to_ss(f);
1072 
1073 	disable_source_sink(ss);
1074 }
1075 
1076 /*-------------------------------------------------------------------------*/
1077 
1078 static int sourcesink_setup(struct usb_function *f,
1079 		const struct usb_ctrlrequest *ctrl)
1080 {
1081 	struct usb_configuration        *c = f->config;
1082 	struct usb_request	*req = c->cdev->req;
1083 	int			value = -EOPNOTSUPP;
1084 	u16			w_index = le16_to_cpu(ctrl->wIndex);
1085 	u16			w_value = le16_to_cpu(ctrl->wValue);
1086 	u16			w_length = le16_to_cpu(ctrl->wLength);
1087 
1088 	req->length = USB_COMP_EP0_BUFSIZ;
1089 
1090 	/* composite driver infrastructure handles everything except
1091 	 * the two control test requests.
1092 	 */
1093 	switch (ctrl->bRequest) {
1094 
1095 	/*
1096 	 * These are the same vendor-specific requests supported by
1097 	 * Intel's USB 2.0 compliance test devices.  We exceed that
1098 	 * device spec by allowing multiple-packet requests.
1099 	 *
1100 	 * NOTE:  the Control-OUT data stays in req->buf ... better
1101 	 * would be copying it into a scratch buffer, so that other
1102 	 * requests may safely intervene.
1103 	 */
1104 	case 0x5b:	/* control WRITE test -- fill the buffer */
1105 		if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
1106 			goto unknown;
1107 		if (w_value || w_index)
1108 			break;
1109 		/* just read that many bytes into the buffer */
1110 		if (w_length > req->length)
1111 			break;
1112 		value = w_length;
1113 		break;
1114 	case 0x5c:	/* control READ test -- return the buffer */
1115 		if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
1116 			goto unknown;
1117 		if (w_value || w_index)
1118 			break;
1119 		/* expect those bytes are still in the buffer; send back */
1120 		if (w_length > req->length)
1121 			break;
1122 		value = w_length;
1123 		break;
1124 
1125 	default:
1126 unknown:
1127 		VDBG(c->cdev,
1128 			"unknown control req%02x.%02x v%04x i%04x l%d\n",
1129 			ctrl->bRequestType, ctrl->bRequest,
1130 			w_value, w_index, w_length);
1131 	}
1132 
1133 	/* respond with data transfer or status phase? */
1134 	if (value >= 0) {
1135 		VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
1136 			ctrl->bRequestType, ctrl->bRequest,
1137 			w_value, w_index, w_length);
1138 		req->zero = 0;
1139 		req->length = value;
1140 		value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
1141 		if (value < 0)
1142 			ERROR(c->cdev, "source/sink response, err %d\n",
1143 					value);
1144 	}
1145 
1146 	/* device either stalls (value < 0) or reports success */
1147 	return value;
1148 }
1149 
1150 static struct usb_function *source_sink_alloc_func(
1151 		struct usb_function_instance *fi)
1152 {
1153 	struct f_sourcesink     *ss;
1154 	struct f_ss_opts	*ss_opts;
1155 
1156 	ss = kzalloc(sizeof(*ss), GFP_KERNEL);
1157 	if (!ss)
1158 		return NULL;
1159 
1160 	ss_opts =  container_of(fi, struct f_ss_opts, func_inst);
1161 
1162 	mutex_lock(&ss_opts->lock);
1163 	ss_opts->refcnt++;
1164 	mutex_unlock(&ss_opts->lock);
1165 
1166 	pattern = ss_opts->pattern;
1167 	isoc_interval = ss_opts->isoc_interval;
1168 	isoc_maxpacket = ss_opts->isoc_maxpacket;
1169 	isoc_mult = ss_opts->isoc_mult;
1170 	isoc_maxburst = ss_opts->isoc_maxburst;
1171 	int_interval = ss_opts->int_interval;
1172 	int_maxpacket = ss_opts->int_maxpacket;
1173 	int_mult = ss_opts->int_mult;
1174 	int_maxburst = ss_opts->int_maxburst;
1175 	buflen = ss_opts->bulk_buflen;
1176 
1177 	ss->function.name = "source/sink";
1178 	ss->function.bind = sourcesink_bind;
1179 	ss->function.set_alt = sourcesink_set_alt;
1180 	ss->function.get_alt = sourcesink_get_alt;
1181 	ss->function.disable = sourcesink_disable;
1182 	ss->function.setup = sourcesink_setup;
1183 	ss->function.strings = sourcesink_strings;
1184 
1185 	ss->function.free_func = sourcesink_free_func;
1186 
1187 	return &ss->function;
1188 }
1189 
1190 static inline struct f_ss_opts *to_f_ss_opts(struct config_item *item)
1191 {
1192 	return container_of(to_config_group(item), struct f_ss_opts,
1193 			    func_inst.group);
1194 }
1195 
1196 CONFIGFS_ATTR_STRUCT(f_ss_opts);
1197 CONFIGFS_ATTR_OPS(f_ss_opts);
1198 
1199 static void ss_attr_release(struct config_item *item)
1200 {
1201 	struct f_ss_opts *ss_opts = to_f_ss_opts(item);
1202 
1203 	usb_put_function_instance(&ss_opts->func_inst);
1204 }
1205 
1206 static struct configfs_item_operations ss_item_ops = {
1207 	.release		= ss_attr_release,
1208 	.show_attribute		= f_ss_opts_attr_show,
1209 	.store_attribute	= f_ss_opts_attr_store,
1210 };
1211 
1212 static ssize_t f_ss_opts_pattern_show(struct f_ss_opts *opts, char *page)
1213 {
1214 	int result;
1215 
1216 	mutex_lock(&opts->lock);
1217 	result = sprintf(page, "%d", opts->pattern);
1218 	mutex_unlock(&opts->lock);
1219 
1220 	return result;
1221 }
1222 
1223 static ssize_t f_ss_opts_pattern_store(struct f_ss_opts *opts,
1224 				       const char *page, size_t len)
1225 {
1226 	int ret;
1227 	u8 num;
1228 
1229 	mutex_lock(&opts->lock);
1230 	if (opts->refcnt) {
1231 		ret = -EBUSY;
1232 		goto end;
1233 	}
1234 
1235 	ret = kstrtou8(page, 0, &num);
1236 	if (ret)
1237 		goto end;
1238 
1239 	if (num != 0 && num != 1 && num != 2) {
1240 		ret = -EINVAL;
1241 		goto end;
1242 	}
1243 
1244 	opts->pattern = num;
1245 	ret = len;
1246 end:
1247 	mutex_unlock(&opts->lock);
1248 	return ret;
1249 }
1250 
1251 static struct f_ss_opts_attribute f_ss_opts_pattern =
1252 	__CONFIGFS_ATTR(pattern, S_IRUGO | S_IWUSR,
1253 			f_ss_opts_pattern_show,
1254 			f_ss_opts_pattern_store);
1255 
1256 static ssize_t f_ss_opts_isoc_interval_show(struct f_ss_opts *opts, char *page)
1257 {
1258 	int result;
1259 
1260 	mutex_lock(&opts->lock);
1261 	result = sprintf(page, "%d", opts->isoc_interval);
1262 	mutex_unlock(&opts->lock);
1263 
1264 	return result;
1265 }
1266 
1267 static ssize_t f_ss_opts_isoc_interval_store(struct f_ss_opts *opts,
1268 				       const char *page, size_t len)
1269 {
1270 	int ret;
1271 	u8 num;
1272 
1273 	mutex_lock(&opts->lock);
1274 	if (opts->refcnt) {
1275 		ret = -EBUSY;
1276 		goto end;
1277 	}
1278 
1279 	ret = kstrtou8(page, 0, &num);
1280 	if (ret)
1281 		goto end;
1282 
1283 	if (num > 16) {
1284 		ret = -EINVAL;
1285 		goto end;
1286 	}
1287 
1288 	opts->isoc_interval = num;
1289 	ret = len;
1290 end:
1291 	mutex_unlock(&opts->lock);
1292 	return ret;
1293 }
1294 
1295 static struct f_ss_opts_attribute f_ss_opts_isoc_interval =
1296 	__CONFIGFS_ATTR(isoc_interval, S_IRUGO | S_IWUSR,
1297 			f_ss_opts_isoc_interval_show,
1298 			f_ss_opts_isoc_interval_store);
1299 
1300 static ssize_t f_ss_opts_isoc_maxpacket_show(struct f_ss_opts *opts, char *page)
1301 {
1302 	int result;
1303 
1304 	mutex_lock(&opts->lock);
1305 	result = sprintf(page, "%d", opts->isoc_maxpacket);
1306 	mutex_unlock(&opts->lock);
1307 
1308 	return result;
1309 }
1310 
1311 static ssize_t f_ss_opts_isoc_maxpacket_store(struct f_ss_opts *opts,
1312 				       const char *page, size_t len)
1313 {
1314 	int ret;
1315 	u16 num;
1316 
1317 	mutex_lock(&opts->lock);
1318 	if (opts->refcnt) {
1319 		ret = -EBUSY;
1320 		goto end;
1321 	}
1322 
1323 	ret = kstrtou16(page, 0, &num);
1324 	if (ret)
1325 		goto end;
1326 
1327 	if (num > 1024) {
1328 		ret = -EINVAL;
1329 		goto end;
1330 	}
1331 
1332 	opts->isoc_maxpacket = num;
1333 	ret = len;
1334 end:
1335 	mutex_unlock(&opts->lock);
1336 	return ret;
1337 }
1338 
1339 static struct f_ss_opts_attribute f_ss_opts_isoc_maxpacket =
1340 	__CONFIGFS_ATTR(isoc_maxpacket, S_IRUGO | S_IWUSR,
1341 			f_ss_opts_isoc_maxpacket_show,
1342 			f_ss_opts_isoc_maxpacket_store);
1343 
1344 static ssize_t f_ss_opts_isoc_mult_show(struct f_ss_opts *opts, char *page)
1345 {
1346 	int result;
1347 
1348 	mutex_lock(&opts->lock);
1349 	result = sprintf(page, "%d", opts->isoc_mult);
1350 	mutex_unlock(&opts->lock);
1351 
1352 	return result;
1353 }
1354 
1355 static ssize_t f_ss_opts_isoc_mult_store(struct f_ss_opts *opts,
1356 				       const char *page, size_t len)
1357 {
1358 	int ret;
1359 	u8 num;
1360 
1361 	mutex_lock(&opts->lock);
1362 	if (opts->refcnt) {
1363 		ret = -EBUSY;
1364 		goto end;
1365 	}
1366 
1367 	ret = kstrtou8(page, 0, &num);
1368 	if (ret)
1369 		goto end;
1370 
1371 	if (num > 2) {
1372 		ret = -EINVAL;
1373 		goto end;
1374 	}
1375 
1376 	opts->isoc_mult = num;
1377 	ret = len;
1378 end:
1379 	mutex_unlock(&opts->lock);
1380 	return ret;
1381 }
1382 
1383 static struct f_ss_opts_attribute f_ss_opts_isoc_mult =
1384 	__CONFIGFS_ATTR(isoc_mult, S_IRUGO | S_IWUSR,
1385 			f_ss_opts_isoc_mult_show,
1386 			f_ss_opts_isoc_mult_store);
1387 
1388 static ssize_t f_ss_opts_isoc_maxburst_show(struct f_ss_opts *opts, char *page)
1389 {
1390 	int result;
1391 
1392 	mutex_lock(&opts->lock);
1393 	result = sprintf(page, "%d", opts->isoc_maxburst);
1394 	mutex_unlock(&opts->lock);
1395 
1396 	return result;
1397 }
1398 
1399 static ssize_t f_ss_opts_isoc_maxburst_store(struct f_ss_opts *opts,
1400 				       const char *page, size_t len)
1401 {
1402 	int ret;
1403 	u8 num;
1404 
1405 	mutex_lock(&opts->lock);
1406 	if (opts->refcnt) {
1407 		ret = -EBUSY;
1408 		goto end;
1409 	}
1410 
1411 	ret = kstrtou8(page, 0, &num);
1412 	if (ret)
1413 		goto end;
1414 
1415 	if (num > 15) {
1416 		ret = -EINVAL;
1417 		goto end;
1418 	}
1419 
1420 	opts->isoc_maxburst = num;
1421 	ret = len;
1422 end:
1423 	mutex_unlock(&opts->lock);
1424 	return ret;
1425 }
1426 
1427 static struct f_ss_opts_attribute f_ss_opts_isoc_maxburst =
1428 	__CONFIGFS_ATTR(isoc_maxburst, S_IRUGO | S_IWUSR,
1429 			f_ss_opts_isoc_maxburst_show,
1430 			f_ss_opts_isoc_maxburst_store);
1431 
1432 static ssize_t f_ss_opts_bulk_buflen_show(struct f_ss_opts *opts, char *page)
1433 {
1434 	int result;
1435 
1436 	mutex_lock(&opts->lock);
1437 	result = sprintf(page, "%d", opts->bulk_buflen);
1438 	mutex_unlock(&opts->lock);
1439 
1440 	return result;
1441 }
1442 
1443 static ssize_t f_ss_opts_bulk_buflen_store(struct f_ss_opts *opts,
1444 					   const char *page, size_t len)
1445 {
1446 	int ret;
1447 	u32 num;
1448 
1449 	mutex_lock(&opts->lock);
1450 	if (opts->refcnt) {
1451 		ret = -EBUSY;
1452 		goto end;
1453 	}
1454 
1455 	ret = kstrtou32(page, 0, &num);
1456 	if (ret)
1457 		goto end;
1458 
1459 	opts->bulk_buflen = num;
1460 	ret = len;
1461 end:
1462 	mutex_unlock(&opts->lock);
1463 	return ret;
1464 }
1465 
1466 static struct f_ss_opts_attribute f_ss_opts_bulk_buflen =
1467 	__CONFIGFS_ATTR(buflen, S_IRUGO | S_IWUSR,
1468 			f_ss_opts_bulk_buflen_show,
1469 			f_ss_opts_bulk_buflen_store);
1470 
1471 static ssize_t f_ss_opts_int_interval_show(struct f_ss_opts *opts, char *page)
1472 {
1473 	int result;
1474 
1475 	mutex_lock(&opts->lock);
1476 	result = sprintf(page, "%d", opts->int_interval);
1477 	mutex_unlock(&opts->lock);
1478 
1479 	return result;
1480 }
1481 
1482 static ssize_t f_ss_opts_int_interval_store(struct f_ss_opts *opts,
1483 				       const char *page, size_t len)
1484 {
1485 	int ret;
1486 	u32 num;
1487 
1488 	mutex_lock(&opts->lock);
1489 	if (opts->refcnt) {
1490 		ret = -EBUSY;
1491 		goto end;
1492 	}
1493 
1494 	ret = kstrtou32(page, 0, &num);
1495 	if (ret)
1496 		goto end;
1497 
1498 	if (num > 4096) {
1499 		ret = -EINVAL;
1500 		goto end;
1501 	}
1502 
1503 	opts->int_interval = num;
1504 	ret = len;
1505 end:
1506 	mutex_unlock(&opts->lock);
1507 	return ret;
1508 }
1509 
1510 static struct f_ss_opts_attribute f_ss_opts_int_interval =
1511 	__CONFIGFS_ATTR(int_interval, S_IRUGO | S_IWUSR,
1512 			f_ss_opts_int_interval_show,
1513 			f_ss_opts_int_interval_store);
1514 
1515 static ssize_t f_ss_opts_int_maxpacket_show(struct f_ss_opts *opts, char *page)
1516 {
1517 	int result;
1518 
1519 	mutex_lock(&opts->lock);
1520 	result = sprintf(page, "%d", opts->int_maxpacket);
1521 	mutex_unlock(&opts->lock);
1522 
1523 	return result;
1524 }
1525 
1526 static ssize_t f_ss_opts_int_maxpacket_store(struct f_ss_opts *opts,
1527 				       const char *page, size_t len)
1528 {
1529 	int ret;
1530 	u16 num;
1531 
1532 	mutex_lock(&opts->lock);
1533 	if (opts->refcnt) {
1534 		ret = -EBUSY;
1535 		goto end;
1536 	}
1537 
1538 	ret = kstrtou16(page, 0, &num);
1539 	if (ret)
1540 		goto end;
1541 
1542 	if (num > 1024) {
1543 		ret = -EINVAL;
1544 		goto end;
1545 	}
1546 
1547 	opts->int_maxpacket = num;
1548 	ret = len;
1549 end:
1550 	mutex_unlock(&opts->lock);
1551 	return ret;
1552 }
1553 
1554 static struct f_ss_opts_attribute f_ss_opts_int_maxpacket =
1555 	__CONFIGFS_ATTR(int_maxpacket, S_IRUGO | S_IWUSR,
1556 			f_ss_opts_int_maxpacket_show,
1557 			f_ss_opts_int_maxpacket_store);
1558 
1559 static ssize_t f_ss_opts_int_mult_show(struct f_ss_opts *opts, char *page)
1560 {
1561 	int result;
1562 
1563 	mutex_lock(&opts->lock);
1564 	result = sprintf(page, "%d", opts->int_mult);
1565 	mutex_unlock(&opts->lock);
1566 
1567 	return result;
1568 }
1569 
1570 static ssize_t f_ss_opts_int_mult_store(struct f_ss_opts *opts,
1571 				       const char *page, size_t len)
1572 {
1573 	int ret;
1574 	u8 num;
1575 
1576 	mutex_lock(&opts->lock);
1577 	if (opts->refcnt) {
1578 		ret = -EBUSY;
1579 		goto end;
1580 	}
1581 
1582 	ret = kstrtou8(page, 0, &num);
1583 	if (ret)
1584 		goto end;
1585 
1586 	if (num > 2) {
1587 		ret = -EINVAL;
1588 		goto end;
1589 	}
1590 
1591 	opts->int_mult = num;
1592 	ret = len;
1593 end:
1594 	mutex_unlock(&opts->lock);
1595 	return ret;
1596 }
1597 
1598 static struct f_ss_opts_attribute f_ss_opts_int_mult =
1599 	__CONFIGFS_ATTR(int_mult, S_IRUGO | S_IWUSR,
1600 			f_ss_opts_int_mult_show,
1601 			f_ss_opts_int_mult_store);
1602 
1603 static ssize_t f_ss_opts_int_maxburst_show(struct f_ss_opts *opts, char *page)
1604 {
1605 	int result;
1606 
1607 	mutex_lock(&opts->lock);
1608 	result = sprintf(page, "%d", opts->int_maxburst);
1609 	mutex_unlock(&opts->lock);
1610 
1611 	return result;
1612 }
1613 
1614 static ssize_t f_ss_opts_int_maxburst_store(struct f_ss_opts *opts,
1615 				       const char *page, size_t len)
1616 {
1617 	int ret;
1618 	u8 num;
1619 
1620 	mutex_lock(&opts->lock);
1621 	if (opts->refcnt) {
1622 		ret = -EBUSY;
1623 		goto end;
1624 	}
1625 
1626 	ret = kstrtou8(page, 0, &num);
1627 	if (ret)
1628 		goto end;
1629 
1630 	if (num > 15) {
1631 		ret = -EINVAL;
1632 		goto end;
1633 	}
1634 
1635 	opts->int_maxburst = num;
1636 	ret = len;
1637 end:
1638 	mutex_unlock(&opts->lock);
1639 	return ret;
1640 }
1641 
1642 static struct f_ss_opts_attribute f_ss_opts_int_maxburst =
1643 	__CONFIGFS_ATTR(int_maxburst, S_IRUGO | S_IWUSR,
1644 			f_ss_opts_int_maxburst_show,
1645 			f_ss_opts_int_maxburst_store);
1646 
1647 static struct configfs_attribute *ss_attrs[] = {
1648 	&f_ss_opts_pattern.attr,
1649 	&f_ss_opts_isoc_interval.attr,
1650 	&f_ss_opts_isoc_maxpacket.attr,
1651 	&f_ss_opts_isoc_mult.attr,
1652 	&f_ss_opts_isoc_maxburst.attr,
1653 	&f_ss_opts_bulk_buflen.attr,
1654 	&f_ss_opts_int_interval.attr,
1655 	&f_ss_opts_int_maxpacket.attr,
1656 	&f_ss_opts_int_mult.attr,
1657 	&f_ss_opts_int_maxburst.attr,
1658 	NULL,
1659 };
1660 
1661 static struct config_item_type ss_func_type = {
1662 	.ct_item_ops    = &ss_item_ops,
1663 	.ct_attrs	= ss_attrs,
1664 	.ct_owner       = THIS_MODULE,
1665 };
1666 
1667 static void source_sink_free_instance(struct usb_function_instance *fi)
1668 {
1669 	struct f_ss_opts *ss_opts;
1670 
1671 	ss_opts = container_of(fi, struct f_ss_opts, func_inst);
1672 	kfree(ss_opts);
1673 }
1674 
1675 static struct usb_function_instance *source_sink_alloc_inst(void)
1676 {
1677 	struct f_ss_opts *ss_opts;
1678 
1679 	ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL);
1680 	if (!ss_opts)
1681 		return ERR_PTR(-ENOMEM);
1682 	mutex_init(&ss_opts->lock);
1683 	ss_opts->func_inst.free_func_inst = source_sink_free_instance;
1684 	ss_opts->isoc_interval = GZERO_ISOC_INTERVAL;
1685 	ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET;
1686 	ss_opts->bulk_buflen = GZERO_BULK_BUFLEN;
1687 	ss_opts->int_interval = GZERO_INT_INTERVAL;
1688 	ss_opts->int_maxpacket = GZERO_INT_MAXPACKET;
1689 
1690 	config_group_init_type_name(&ss_opts->func_inst.group, "",
1691 				    &ss_func_type);
1692 
1693 	return &ss_opts->func_inst;
1694 }
1695 DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst,
1696 		source_sink_alloc_func);
1697 
1698 static int __init sslb_modinit(void)
1699 {
1700 	int ret;
1701 
1702 	ret = usb_function_register(&SourceSinkusb_func);
1703 	if (ret)
1704 		return ret;
1705 	ret = lb_modinit();
1706 	if (ret)
1707 		usb_function_unregister(&SourceSinkusb_func);
1708 	return ret;
1709 }
1710 static void __exit sslb_modexit(void)
1711 {
1712 	usb_function_unregister(&SourceSinkusb_func);
1713 	lb_modexit();
1714 }
1715 module_init(sslb_modinit);
1716 module_exit(sslb_modexit);
1717 
1718 MODULE_LICENSE("GPL");
1719