]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/usb/gadget/function/f_sourcesink.c
Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/nab/target...
[mirror_ubuntu-artful-kernel.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 void free_ep_req(struct usb_ep *ep, struct usb_request *req)
307 {
308 kfree(req->buf);
309 usb_ep_free_request(ep, req);
310 }
311
312 static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
313 {
314 int value;
315
316 value = usb_ep_disable(ep);
317 if (value < 0)
318 DBG(cdev, "disable %s --> %d\n", ep->name, value);
319 }
320
321 void disable_endpoints(struct usb_composite_dev *cdev,
322 struct usb_ep *in, struct usb_ep *out,
323 struct usb_ep *iso_in, struct usb_ep *iso_out)
324 {
325 disable_ep(cdev, in);
326 disable_ep(cdev, out);
327 if (iso_in)
328 disable_ep(cdev, iso_in);
329 if (iso_out)
330 disable_ep(cdev, iso_out);
331 }
332
333 static int
334 sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
335 {
336 struct usb_composite_dev *cdev = c->cdev;
337 struct f_sourcesink *ss = func_to_ss(f);
338 int id;
339 int ret;
340
341 /* allocate interface ID(s) */
342 id = usb_interface_id(c, f);
343 if (id < 0)
344 return id;
345 source_sink_intf_alt0.bInterfaceNumber = id;
346 source_sink_intf_alt1.bInterfaceNumber = id;
347
348 /* allocate bulk endpoints */
349 ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
350 if (!ss->in_ep) {
351 autoconf_fail:
352 ERROR(cdev, "%s: can't autoconfigure on %s\n",
353 f->name, cdev->gadget->name);
354 return -ENODEV;
355 }
356
357 ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
358 if (!ss->out_ep)
359 goto autoconf_fail;
360
361 /* sanity check the isoc module parameters */
362 if (ss->isoc_interval < 1)
363 ss->isoc_interval = 1;
364 if (ss->isoc_interval > 16)
365 ss->isoc_interval = 16;
366 if (ss->isoc_mult > 2)
367 ss->isoc_mult = 2;
368 if (ss->isoc_maxburst > 15)
369 ss->isoc_maxburst = 15;
370
371 /* fill in the FS isoc descriptors from the module parameters */
372 fs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket > 1023 ?
373 1023 : ss->isoc_maxpacket;
374 fs_iso_source_desc.bInterval = ss->isoc_interval;
375 fs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket > 1023 ?
376 1023 : ss->isoc_maxpacket;
377 fs_iso_sink_desc.bInterval = ss->isoc_interval;
378
379 /* allocate iso endpoints */
380 ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
381 if (!ss->iso_in_ep)
382 goto no_iso;
383
384 ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
385 if (!ss->iso_out_ep) {
386 usb_ep_autoconfig_release(ss->iso_in_ep);
387 ss->iso_in_ep = NULL;
388 no_iso:
389 /*
390 * We still want to work even if the UDC doesn't have isoc
391 * endpoints, so null out the alt interface that contains
392 * them and continue.
393 */
394 fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
395 hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
396 ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
397 }
398
399 if (ss->isoc_maxpacket > 1024)
400 ss->isoc_maxpacket = 1024;
401
402 /* support high speed hardware */
403 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
404 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
405
406 /*
407 * Fill in the HS isoc descriptors from the module parameters.
408 * We assume that the user knows what they are doing and won't
409 * give parameters that their UDC doesn't support.
410 */
411 hs_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket;
412 hs_iso_source_desc.wMaxPacketSize |= ss->isoc_mult << 11;
413 hs_iso_source_desc.bInterval = ss->isoc_interval;
414 hs_iso_source_desc.bEndpointAddress =
415 fs_iso_source_desc.bEndpointAddress;
416
417 hs_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket;
418 hs_iso_sink_desc.wMaxPacketSize |= ss->isoc_mult << 11;
419 hs_iso_sink_desc.bInterval = ss->isoc_interval;
420 hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
421
422 /* support super speed hardware */
423 ss_source_desc.bEndpointAddress =
424 fs_source_desc.bEndpointAddress;
425 ss_sink_desc.bEndpointAddress =
426 fs_sink_desc.bEndpointAddress;
427
428 /*
429 * Fill in the SS isoc descriptors from the module parameters.
430 * We assume that the user knows what they are doing and won't
431 * give parameters that their UDC doesn't support.
432 */
433 ss_iso_source_desc.wMaxPacketSize = ss->isoc_maxpacket;
434 ss_iso_source_desc.bInterval = ss->isoc_interval;
435 ss_iso_source_comp_desc.bmAttributes = ss->isoc_mult;
436 ss_iso_source_comp_desc.bMaxBurst = ss->isoc_maxburst;
437 ss_iso_source_comp_desc.wBytesPerInterval = ss->isoc_maxpacket *
438 (ss->isoc_mult + 1) * (ss->isoc_maxburst + 1);
439 ss_iso_source_desc.bEndpointAddress =
440 fs_iso_source_desc.bEndpointAddress;
441
442 ss_iso_sink_desc.wMaxPacketSize = ss->isoc_maxpacket;
443 ss_iso_sink_desc.bInterval = ss->isoc_interval;
444 ss_iso_sink_comp_desc.bmAttributes = ss->isoc_mult;
445 ss_iso_sink_comp_desc.bMaxBurst = ss->isoc_maxburst;
446 ss_iso_sink_comp_desc.wBytesPerInterval = ss->isoc_maxpacket *
447 (ss->isoc_mult + 1) * (ss->isoc_maxburst + 1);
448 ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
449
450 ret = usb_assign_descriptors(f, fs_source_sink_descs,
451 hs_source_sink_descs, ss_source_sink_descs);
452 if (ret)
453 return ret;
454
455 DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
456 (gadget_is_superspeed(c->cdev->gadget) ? "super" :
457 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
458 f->name, ss->in_ep->name, ss->out_ep->name,
459 ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
460 ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
461 return 0;
462 }
463
464 static void
465 sourcesink_free_func(struct usb_function *f)
466 {
467 struct f_ss_opts *opts;
468
469 opts = container_of(f->fi, struct f_ss_opts, func_inst);
470
471 mutex_lock(&opts->lock);
472 opts->refcnt--;
473 mutex_unlock(&opts->lock);
474
475 usb_free_all_descriptors(f);
476 kfree(func_to_ss(f));
477 }
478
479 /* optionally require specific source/sink data patterns */
480 static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
481 {
482 unsigned i;
483 u8 *buf = req->buf;
484 struct usb_composite_dev *cdev = ss->function.config->cdev;
485 int max_packet_size = le16_to_cpu(ss->out_ep->desc->wMaxPacketSize);
486
487 if (ss->pattern == 2)
488 return 0;
489
490 for (i = 0; i < req->actual; i++, buf++) {
491 switch (ss->pattern) {
492
493 /* all-zeroes has no synchronization issues */
494 case 0:
495 if (*buf == 0)
496 continue;
497 break;
498
499 /* "mod63" stays in sync with short-terminated transfers,
500 * OR otherwise when host and gadget agree on how large
501 * each usb transfer request should be. Resync is done
502 * with set_interface or set_config. (We *WANT* it to
503 * get quickly out of sync if controllers or their drivers
504 * stutter for any reason, including buffer duplication...)
505 */
506 case 1:
507 if (*buf == (u8)((i % max_packet_size) % 63))
508 continue;
509 break;
510 }
511 ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
512 usb_ep_set_halt(ss->out_ep);
513 return -EINVAL;
514 }
515 return 0;
516 }
517
518 static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
519 {
520 unsigned i;
521 u8 *buf = req->buf;
522 int max_packet_size = le16_to_cpu(ep->desc->wMaxPacketSize);
523 struct f_sourcesink *ss = ep->driver_data;
524
525 switch (ss->pattern) {
526 case 0:
527 memset(req->buf, 0, req->length);
528 break;
529 case 1:
530 for (i = 0; i < req->length; i++)
531 *buf++ = (u8) ((i % max_packet_size) % 63);
532 break;
533 case 2:
534 break;
535 }
536 }
537
538 static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
539 {
540 struct usb_composite_dev *cdev;
541 struct f_sourcesink *ss = ep->driver_data;
542 int status = req->status;
543
544 /* driver_data will be null if ep has been disabled */
545 if (!ss)
546 return;
547
548 cdev = ss->function.config->cdev;
549
550 switch (status) {
551
552 case 0: /* normal completion? */
553 if (ep == ss->out_ep) {
554 check_read_data(ss, req);
555 if (ss->pattern != 2)
556 memset(req->buf, 0x55, req->length);
557 }
558 break;
559
560 /* this endpoint is normally active while we're configured */
561 case -ECONNABORTED: /* hardware forced ep reset */
562 case -ECONNRESET: /* request dequeued */
563 case -ESHUTDOWN: /* disconnect from host */
564 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
565 req->actual, req->length);
566 if (ep == ss->out_ep)
567 check_read_data(ss, req);
568 free_ep_req(ep, req);
569 return;
570
571 case -EOVERFLOW: /* buffer overrun on read means that
572 * we didn't provide a big enough
573 * buffer.
574 */
575 default:
576 #if 1
577 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
578 status, req->actual, req->length);
579 #endif
580 case -EREMOTEIO: /* short read */
581 break;
582 }
583
584 status = usb_ep_queue(ep, req, GFP_ATOMIC);
585 if (status) {
586 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
587 ep->name, req->length, status);
588 usb_ep_set_halt(ep);
589 /* FIXME recover later ... somehow */
590 }
591 }
592
593 static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
594 bool is_iso, int speed)
595 {
596 struct usb_ep *ep;
597 struct usb_request *req;
598 int i, size, status;
599
600 for (i = 0; i < 8; i++) {
601 if (is_iso) {
602 switch (speed) {
603 case USB_SPEED_SUPER:
604 size = ss->isoc_maxpacket *
605 (ss->isoc_mult + 1) *
606 (ss->isoc_maxburst + 1);
607 break;
608 case USB_SPEED_HIGH:
609 size = ss->isoc_maxpacket * (ss->isoc_mult + 1);
610 break;
611 default:
612 size = ss->isoc_maxpacket > 1023 ?
613 1023 : ss->isoc_maxpacket;
614 break;
615 }
616 ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
617 req = ss_alloc_ep_req(ep, size);
618 } else {
619 ep = is_in ? ss->in_ep : ss->out_ep;
620 req = ss_alloc_ep_req(ep, 0);
621 }
622
623 if (!req)
624 return -ENOMEM;
625
626 req->complete = source_sink_complete;
627 if (is_in)
628 reinit_write_data(ep, req);
629 else if (ss->pattern != 2)
630 memset(req->buf, 0x55, req->length);
631
632 status = usb_ep_queue(ep, req, GFP_ATOMIC);
633 if (status) {
634 struct usb_composite_dev *cdev;
635
636 cdev = ss->function.config->cdev;
637 ERROR(cdev, "start %s%s %s --> %d\n",
638 is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
639 ep->name, status);
640 free_ep_req(ep, req);
641 }
642
643 if (!is_iso)
644 break;
645 }
646
647 return status;
648 }
649
650 static void disable_source_sink(struct f_sourcesink *ss)
651 {
652 struct usb_composite_dev *cdev;
653
654 cdev = ss->function.config->cdev;
655 disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
656 ss->iso_out_ep);
657 VDBG(cdev, "%s disabled\n", ss->function.name);
658 }
659
660 static int
661 enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
662 int alt)
663 {
664 int result = 0;
665 int speed = cdev->gadget->speed;
666 struct usb_ep *ep;
667
668 /* one bulk endpoint writes (sources) zeroes IN (to the host) */
669 ep = ss->in_ep;
670 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
671 if (result)
672 return result;
673 result = usb_ep_enable(ep);
674 if (result < 0)
675 return result;
676 ep->driver_data = ss;
677
678 result = source_sink_start_ep(ss, true, false, speed);
679 if (result < 0) {
680 fail:
681 ep = ss->in_ep;
682 usb_ep_disable(ep);
683 return result;
684 }
685
686 /* one bulk endpoint reads (sinks) anything OUT (from the host) */
687 ep = ss->out_ep;
688 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
689 if (result)
690 goto fail;
691 result = usb_ep_enable(ep);
692 if (result < 0)
693 goto fail;
694 ep->driver_data = ss;
695
696 result = source_sink_start_ep(ss, false, false, speed);
697 if (result < 0) {
698 fail2:
699 ep = ss->out_ep;
700 usb_ep_disable(ep);
701 goto fail;
702 }
703
704 if (alt == 0)
705 goto out;
706
707 /* one iso endpoint writes (sources) zeroes IN (to the host) */
708 ep = ss->iso_in_ep;
709 if (ep) {
710 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
711 if (result)
712 goto fail2;
713 result = usb_ep_enable(ep);
714 if (result < 0)
715 goto fail2;
716 ep->driver_data = ss;
717
718 result = source_sink_start_ep(ss, true, true, speed);
719 if (result < 0) {
720 fail3:
721 ep = ss->iso_in_ep;
722 if (ep)
723 usb_ep_disable(ep);
724 goto fail2;
725 }
726 }
727
728 /* one iso endpoint reads (sinks) anything OUT (from the host) */
729 ep = ss->iso_out_ep;
730 if (ep) {
731 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
732 if (result)
733 goto fail3;
734 result = usb_ep_enable(ep);
735 if (result < 0)
736 goto fail3;
737 ep->driver_data = ss;
738
739 result = source_sink_start_ep(ss, false, true, speed);
740 if (result < 0) {
741 usb_ep_disable(ep);
742 goto fail3;
743 }
744 }
745 out:
746 ss->cur_alt = alt;
747
748 DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
749 return result;
750 }
751
752 static int sourcesink_set_alt(struct usb_function *f,
753 unsigned intf, unsigned alt)
754 {
755 struct f_sourcesink *ss = func_to_ss(f);
756 struct usb_composite_dev *cdev = f->config->cdev;
757
758 disable_source_sink(ss);
759 return enable_source_sink(cdev, ss, alt);
760 }
761
762 static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
763 {
764 struct f_sourcesink *ss = func_to_ss(f);
765
766 return ss->cur_alt;
767 }
768
769 static void sourcesink_disable(struct usb_function *f)
770 {
771 struct f_sourcesink *ss = func_to_ss(f);
772
773 disable_source_sink(ss);
774 }
775
776 /*-------------------------------------------------------------------------*/
777
778 static int sourcesink_setup(struct usb_function *f,
779 const struct usb_ctrlrequest *ctrl)
780 {
781 struct usb_configuration *c = f->config;
782 struct usb_request *req = c->cdev->req;
783 int value = -EOPNOTSUPP;
784 u16 w_index = le16_to_cpu(ctrl->wIndex);
785 u16 w_value = le16_to_cpu(ctrl->wValue);
786 u16 w_length = le16_to_cpu(ctrl->wLength);
787
788 req->length = USB_COMP_EP0_BUFSIZ;
789
790 /* composite driver infrastructure handles everything except
791 * the two control test requests.
792 */
793 switch (ctrl->bRequest) {
794
795 /*
796 * These are the same vendor-specific requests supported by
797 * Intel's USB 2.0 compliance test devices. We exceed that
798 * device spec by allowing multiple-packet requests.
799 *
800 * NOTE: the Control-OUT data stays in req->buf ... better
801 * would be copying it into a scratch buffer, so that other
802 * requests may safely intervene.
803 */
804 case 0x5b: /* control WRITE test -- fill the buffer */
805 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
806 goto unknown;
807 if (w_value || w_index)
808 break;
809 /* just read that many bytes into the buffer */
810 if (w_length > req->length)
811 break;
812 value = w_length;
813 break;
814 case 0x5c: /* control READ test -- return the buffer */
815 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
816 goto unknown;
817 if (w_value || w_index)
818 break;
819 /* expect those bytes are still in the buffer; send back */
820 if (w_length > req->length)
821 break;
822 value = w_length;
823 break;
824
825 default:
826 unknown:
827 VDBG(c->cdev,
828 "unknown control req%02x.%02x v%04x i%04x l%d\n",
829 ctrl->bRequestType, ctrl->bRequest,
830 w_value, w_index, w_length);
831 }
832
833 /* respond with data transfer or status phase? */
834 if (value >= 0) {
835 VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
836 ctrl->bRequestType, ctrl->bRequest,
837 w_value, w_index, w_length);
838 req->zero = 0;
839 req->length = value;
840 value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
841 if (value < 0)
842 ERROR(c->cdev, "source/sink response, err %d\n",
843 value);
844 }
845
846 /* device either stalls (value < 0) or reports success */
847 return value;
848 }
849
850 static struct usb_function *source_sink_alloc_func(
851 struct usb_function_instance *fi)
852 {
853 struct f_sourcesink *ss;
854 struct f_ss_opts *ss_opts;
855
856 ss = kzalloc(sizeof(*ss), GFP_KERNEL);
857 if (!ss)
858 return NULL;
859
860 ss_opts = container_of(fi, struct f_ss_opts, func_inst);
861
862 mutex_lock(&ss_opts->lock);
863 ss_opts->refcnt++;
864 mutex_unlock(&ss_opts->lock);
865
866 ss->pattern = ss_opts->pattern;
867 ss->isoc_interval = ss_opts->isoc_interval;
868 ss->isoc_maxpacket = ss_opts->isoc_maxpacket;
869 ss->isoc_mult = ss_opts->isoc_mult;
870 ss->isoc_maxburst = ss_opts->isoc_maxburst;
871 ss->buflen = ss_opts->bulk_buflen;
872
873 ss->function.name = "source/sink";
874 ss->function.bind = sourcesink_bind;
875 ss->function.set_alt = sourcesink_set_alt;
876 ss->function.get_alt = sourcesink_get_alt;
877 ss->function.disable = sourcesink_disable;
878 ss->function.setup = sourcesink_setup;
879 ss->function.strings = sourcesink_strings;
880
881 ss->function.free_func = sourcesink_free_func;
882
883 return &ss->function;
884 }
885
886 static inline struct f_ss_opts *to_f_ss_opts(struct config_item *item)
887 {
888 return container_of(to_config_group(item), struct f_ss_opts,
889 func_inst.group);
890 }
891
892 static void ss_attr_release(struct config_item *item)
893 {
894 struct f_ss_opts *ss_opts = to_f_ss_opts(item);
895
896 usb_put_function_instance(&ss_opts->func_inst);
897 }
898
899 static struct configfs_item_operations ss_item_ops = {
900 .release = ss_attr_release,
901 };
902
903 static ssize_t f_ss_opts_pattern_show(struct config_item *item, char *page)
904 {
905 struct f_ss_opts *opts = to_f_ss_opts(item);
906 int result;
907
908 mutex_lock(&opts->lock);
909 result = sprintf(page, "%u\n", opts->pattern);
910 mutex_unlock(&opts->lock);
911
912 return result;
913 }
914
915 static ssize_t f_ss_opts_pattern_store(struct config_item *item,
916 const char *page, size_t len)
917 {
918 struct f_ss_opts *opts = to_f_ss_opts(item);
919 int ret;
920 u8 num;
921
922 mutex_lock(&opts->lock);
923 if (opts->refcnt) {
924 ret = -EBUSY;
925 goto end;
926 }
927
928 ret = kstrtou8(page, 0, &num);
929 if (ret)
930 goto end;
931
932 if (num != 0 && num != 1 && num != 2) {
933 ret = -EINVAL;
934 goto end;
935 }
936
937 opts->pattern = num;
938 ret = len;
939 end:
940 mutex_unlock(&opts->lock);
941 return ret;
942 }
943
944 CONFIGFS_ATTR(f_ss_opts_, pattern);
945
946 static ssize_t f_ss_opts_isoc_interval_show(struct config_item *item, char *page)
947 {
948 struct f_ss_opts *opts = to_f_ss_opts(item);
949 int result;
950
951 mutex_lock(&opts->lock);
952 result = sprintf(page, "%u\n", opts->isoc_interval);
953 mutex_unlock(&opts->lock);
954
955 return result;
956 }
957
958 static ssize_t f_ss_opts_isoc_interval_store(struct config_item *item,
959 const char *page, size_t len)
960 {
961 struct f_ss_opts *opts = to_f_ss_opts(item);
962 int ret;
963 u8 num;
964
965 mutex_lock(&opts->lock);
966 if (opts->refcnt) {
967 ret = -EBUSY;
968 goto end;
969 }
970
971 ret = kstrtou8(page, 0, &num);
972 if (ret)
973 goto end;
974
975 if (num > 16) {
976 ret = -EINVAL;
977 goto end;
978 }
979
980 opts->isoc_interval = num;
981 ret = len;
982 end:
983 mutex_unlock(&opts->lock);
984 return ret;
985 }
986
987 CONFIGFS_ATTR(f_ss_opts_, isoc_interval);
988
989 static ssize_t f_ss_opts_isoc_maxpacket_show(struct config_item *item, char *page)
990 {
991 struct f_ss_opts *opts = to_f_ss_opts(item);
992 int result;
993
994 mutex_lock(&opts->lock);
995 result = sprintf(page, "%u\n", opts->isoc_maxpacket);
996 mutex_unlock(&opts->lock);
997
998 return result;
999 }
1000
1001 static ssize_t f_ss_opts_isoc_maxpacket_store(struct config_item *item,
1002 const char *page, size_t len)
1003 {
1004 struct f_ss_opts *opts = to_f_ss_opts(item);
1005 int ret;
1006 u16 num;
1007
1008 mutex_lock(&opts->lock);
1009 if (opts->refcnt) {
1010 ret = -EBUSY;
1011 goto end;
1012 }
1013
1014 ret = kstrtou16(page, 0, &num);
1015 if (ret)
1016 goto end;
1017
1018 if (num > 1024) {
1019 ret = -EINVAL;
1020 goto end;
1021 }
1022
1023 opts->isoc_maxpacket = num;
1024 ret = len;
1025 end:
1026 mutex_unlock(&opts->lock);
1027 return ret;
1028 }
1029
1030 CONFIGFS_ATTR(f_ss_opts_, isoc_maxpacket);
1031
1032 static ssize_t f_ss_opts_isoc_mult_show(struct config_item *item, char *page)
1033 {
1034 struct f_ss_opts *opts = to_f_ss_opts(item);
1035 int result;
1036
1037 mutex_lock(&opts->lock);
1038 result = sprintf(page, "%u\n", opts->isoc_mult);
1039 mutex_unlock(&opts->lock);
1040
1041 return result;
1042 }
1043
1044 static ssize_t f_ss_opts_isoc_mult_store(struct config_item *item,
1045 const char *page, size_t len)
1046 {
1047 struct f_ss_opts *opts = to_f_ss_opts(item);
1048 int ret;
1049 u8 num;
1050
1051 mutex_lock(&opts->lock);
1052 if (opts->refcnt) {
1053 ret = -EBUSY;
1054 goto end;
1055 }
1056
1057 ret = kstrtou8(page, 0, &num);
1058 if (ret)
1059 goto end;
1060
1061 if (num > 2) {
1062 ret = -EINVAL;
1063 goto end;
1064 }
1065
1066 opts->isoc_mult = num;
1067 ret = len;
1068 end:
1069 mutex_unlock(&opts->lock);
1070 return ret;
1071 }
1072
1073 CONFIGFS_ATTR(f_ss_opts_, isoc_mult);
1074
1075 static ssize_t f_ss_opts_isoc_maxburst_show(struct config_item *item, char *page)
1076 {
1077 struct f_ss_opts *opts = to_f_ss_opts(item);
1078 int result;
1079
1080 mutex_lock(&opts->lock);
1081 result = sprintf(page, "%u\n", opts->isoc_maxburst);
1082 mutex_unlock(&opts->lock);
1083
1084 return result;
1085 }
1086
1087 static ssize_t f_ss_opts_isoc_maxburst_store(struct config_item *item,
1088 const char *page, size_t len)
1089 {
1090 struct f_ss_opts *opts = to_f_ss_opts(item);
1091 int ret;
1092 u8 num;
1093
1094 mutex_lock(&opts->lock);
1095 if (opts->refcnt) {
1096 ret = -EBUSY;
1097 goto end;
1098 }
1099
1100 ret = kstrtou8(page, 0, &num);
1101 if (ret)
1102 goto end;
1103
1104 if (num > 15) {
1105 ret = -EINVAL;
1106 goto end;
1107 }
1108
1109 opts->isoc_maxburst = num;
1110 ret = len;
1111 end:
1112 mutex_unlock(&opts->lock);
1113 return ret;
1114 }
1115
1116 CONFIGFS_ATTR(f_ss_opts_, isoc_maxburst);
1117
1118 static ssize_t f_ss_opts_bulk_buflen_show(struct config_item *item, char *page)
1119 {
1120 struct f_ss_opts *opts = to_f_ss_opts(item);
1121 int result;
1122
1123 mutex_lock(&opts->lock);
1124 result = sprintf(page, "%u\n", opts->bulk_buflen);
1125 mutex_unlock(&opts->lock);
1126
1127 return result;
1128 }
1129
1130 static ssize_t f_ss_opts_bulk_buflen_store(struct config_item *item,
1131 const char *page, size_t len)
1132 {
1133 struct f_ss_opts *opts = to_f_ss_opts(item);
1134 int ret;
1135 u32 num;
1136
1137 mutex_lock(&opts->lock);
1138 if (opts->refcnt) {
1139 ret = -EBUSY;
1140 goto end;
1141 }
1142
1143 ret = kstrtou32(page, 0, &num);
1144 if (ret)
1145 goto end;
1146
1147 opts->bulk_buflen = num;
1148 ret = len;
1149 end:
1150 mutex_unlock(&opts->lock);
1151 return ret;
1152 }
1153
1154 CONFIGFS_ATTR(f_ss_opts_, bulk_buflen);
1155
1156 static struct configfs_attribute *ss_attrs[] = {
1157 &f_ss_opts_attr_pattern,
1158 &f_ss_opts_attr_isoc_interval,
1159 &f_ss_opts_attr_isoc_maxpacket,
1160 &f_ss_opts_attr_isoc_mult,
1161 &f_ss_opts_attr_isoc_maxburst,
1162 &f_ss_opts_attr_bulk_buflen,
1163 NULL,
1164 };
1165
1166 static struct config_item_type ss_func_type = {
1167 .ct_item_ops = &ss_item_ops,
1168 .ct_attrs = ss_attrs,
1169 .ct_owner = THIS_MODULE,
1170 };
1171
1172 static void source_sink_free_instance(struct usb_function_instance *fi)
1173 {
1174 struct f_ss_opts *ss_opts;
1175
1176 ss_opts = container_of(fi, struct f_ss_opts, func_inst);
1177 kfree(ss_opts);
1178 }
1179
1180 static struct usb_function_instance *source_sink_alloc_inst(void)
1181 {
1182 struct f_ss_opts *ss_opts;
1183
1184 ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL);
1185 if (!ss_opts)
1186 return ERR_PTR(-ENOMEM);
1187 mutex_init(&ss_opts->lock);
1188 ss_opts->func_inst.free_func_inst = source_sink_free_instance;
1189 ss_opts->isoc_interval = GZERO_ISOC_INTERVAL;
1190 ss_opts->isoc_maxpacket = GZERO_ISOC_MAXPACKET;
1191 ss_opts->bulk_buflen = GZERO_BULK_BUFLEN;
1192
1193 config_group_init_type_name(&ss_opts->func_inst.group, "",
1194 &ss_func_type);
1195
1196 return &ss_opts->func_inst;
1197 }
1198 DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst,
1199 source_sink_alloc_func);
1200
1201 static int __init sslb_modinit(void)
1202 {
1203 int ret;
1204
1205 ret = usb_function_register(&SourceSinkusb_func);
1206 if (ret)
1207 return ret;
1208 ret = lb_modinit();
1209 if (ret)
1210 usb_function_unregister(&SourceSinkusb_func);
1211 return ret;
1212 }
1213 static void __exit sslb_modexit(void)
1214 {
1215 usb_function_unregister(&SourceSinkusb_func);
1216 lb_modexit();
1217 }
1218 module_init(sslb_modinit);
1219 module_exit(sslb_modexit);
1220
1221 MODULE_LICENSE("GPL");