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