Merge tag 'for_linux-3.9' of git://git.kernel.org/pub/scm/linux/kernel/git/jwessel...
[deliverable/linux.git] / drivers / usb / gadget / 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 "gadget_chips.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 *
46 * This is currently packaged as a configuration driver, which can't be
47 * combined with other functions to make composite devices. However, it
48 * can be combined with other independent configurations.
49 */
50 struct f_sourcesink {
51 struct usb_function function;
52
53 struct usb_ep *in_ep;
54 struct usb_ep *out_ep;
55 struct usb_ep *iso_in_ep;
56 struct usb_ep *iso_out_ep;
57 int cur_alt;
58 };
59
60 static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
61 {
62 return container_of(f, struct f_sourcesink, function);
63 }
64
65 static unsigned pattern;
66 static unsigned isoc_interval;
67 static unsigned isoc_maxpacket;
68 static unsigned isoc_mult;
69 static unsigned isoc_maxburst;
70 static unsigned buflen;
71
72 /*-------------------------------------------------------------------------*/
73
74 static struct usb_interface_descriptor source_sink_intf_alt0 = {
75 .bLength = USB_DT_INTERFACE_SIZE,
76 .bDescriptorType = USB_DT_INTERFACE,
77
78 .bAlternateSetting = 0,
79 .bNumEndpoints = 2,
80 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
81 /* .iInterface = DYNAMIC */
82 };
83
84 static struct usb_interface_descriptor source_sink_intf_alt1 = {
85 .bLength = USB_DT_INTERFACE_SIZE,
86 .bDescriptorType = USB_DT_INTERFACE,
87
88 .bAlternateSetting = 1,
89 .bNumEndpoints = 4,
90 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
91 /* .iInterface = DYNAMIC */
92 };
93
94 /* full speed support: */
95
96 static struct usb_endpoint_descriptor fs_source_desc = {
97 .bLength = USB_DT_ENDPOINT_SIZE,
98 .bDescriptorType = USB_DT_ENDPOINT,
99
100 .bEndpointAddress = USB_DIR_IN,
101 .bmAttributes = USB_ENDPOINT_XFER_BULK,
102 };
103
104 static struct usb_endpoint_descriptor fs_sink_desc = {
105 .bLength = USB_DT_ENDPOINT_SIZE,
106 .bDescriptorType = USB_DT_ENDPOINT,
107
108 .bEndpointAddress = USB_DIR_OUT,
109 .bmAttributes = USB_ENDPOINT_XFER_BULK,
110 };
111
112 static struct usb_endpoint_descriptor fs_iso_source_desc = {
113 .bLength = USB_DT_ENDPOINT_SIZE,
114 .bDescriptorType = USB_DT_ENDPOINT,
115
116 .bEndpointAddress = USB_DIR_IN,
117 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
118 .wMaxPacketSize = cpu_to_le16(1023),
119 .bInterval = 4,
120 };
121
122 static struct usb_endpoint_descriptor fs_iso_sink_desc = {
123 .bLength = USB_DT_ENDPOINT_SIZE,
124 .bDescriptorType = USB_DT_ENDPOINT,
125
126 .bEndpointAddress = USB_DIR_OUT,
127 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
128 .wMaxPacketSize = cpu_to_le16(1023),
129 .bInterval = 4,
130 };
131
132 static struct usb_descriptor_header *fs_source_sink_descs[] = {
133 (struct usb_descriptor_header *) &source_sink_intf_alt0,
134 (struct usb_descriptor_header *) &fs_sink_desc,
135 (struct usb_descriptor_header *) &fs_source_desc,
136 (struct usb_descriptor_header *) &source_sink_intf_alt1,
137 #define FS_ALT_IFC_1_OFFSET 3
138 (struct usb_descriptor_header *) &fs_sink_desc,
139 (struct usb_descriptor_header *) &fs_source_desc,
140 (struct usb_descriptor_header *) &fs_iso_sink_desc,
141 (struct usb_descriptor_header *) &fs_iso_source_desc,
142 NULL,
143 };
144
145 /* high speed support: */
146
147 static struct usb_endpoint_descriptor hs_source_desc = {
148 .bLength = USB_DT_ENDPOINT_SIZE,
149 .bDescriptorType = USB_DT_ENDPOINT,
150
151 .bmAttributes = USB_ENDPOINT_XFER_BULK,
152 .wMaxPacketSize = cpu_to_le16(512),
153 };
154
155 static struct usb_endpoint_descriptor hs_sink_desc = {
156 .bLength = USB_DT_ENDPOINT_SIZE,
157 .bDescriptorType = USB_DT_ENDPOINT,
158
159 .bmAttributes = USB_ENDPOINT_XFER_BULK,
160 .wMaxPacketSize = cpu_to_le16(512),
161 };
162
163 static struct usb_endpoint_descriptor hs_iso_source_desc = {
164 .bLength = USB_DT_ENDPOINT_SIZE,
165 .bDescriptorType = USB_DT_ENDPOINT,
166
167 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
168 .wMaxPacketSize = cpu_to_le16(1024),
169 .bInterval = 4,
170 };
171
172 static struct usb_endpoint_descriptor hs_iso_sink_desc = {
173 .bLength = USB_DT_ENDPOINT_SIZE,
174 .bDescriptorType = USB_DT_ENDPOINT,
175
176 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
177 .wMaxPacketSize = cpu_to_le16(1024),
178 .bInterval = 4,
179 };
180
181 static struct usb_descriptor_header *hs_source_sink_descs[] = {
182 (struct usb_descriptor_header *) &source_sink_intf_alt0,
183 (struct usb_descriptor_header *) &hs_source_desc,
184 (struct usb_descriptor_header *) &hs_sink_desc,
185 (struct usb_descriptor_header *) &source_sink_intf_alt1,
186 #define HS_ALT_IFC_1_OFFSET 3
187 (struct usb_descriptor_header *) &hs_source_desc,
188 (struct usb_descriptor_header *) &hs_sink_desc,
189 (struct usb_descriptor_header *) &hs_iso_source_desc,
190 (struct usb_descriptor_header *) &hs_iso_sink_desc,
191 NULL,
192 };
193
194 /* super speed support: */
195
196 static struct usb_endpoint_descriptor ss_source_desc = {
197 .bLength = USB_DT_ENDPOINT_SIZE,
198 .bDescriptorType = USB_DT_ENDPOINT,
199
200 .bmAttributes = USB_ENDPOINT_XFER_BULK,
201 .wMaxPacketSize = cpu_to_le16(1024),
202 };
203
204 struct usb_ss_ep_comp_descriptor ss_source_comp_desc = {
205 .bLength = USB_DT_SS_EP_COMP_SIZE,
206 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
207
208 .bMaxBurst = 0,
209 .bmAttributes = 0,
210 .wBytesPerInterval = 0,
211 };
212
213 static struct usb_endpoint_descriptor ss_sink_desc = {
214 .bLength = USB_DT_ENDPOINT_SIZE,
215 .bDescriptorType = USB_DT_ENDPOINT,
216
217 .bmAttributes = USB_ENDPOINT_XFER_BULK,
218 .wMaxPacketSize = cpu_to_le16(1024),
219 };
220
221 struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = {
222 .bLength = USB_DT_SS_EP_COMP_SIZE,
223 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
224
225 .bMaxBurst = 0,
226 .bmAttributes = 0,
227 .wBytesPerInterval = 0,
228 };
229
230 static struct usb_endpoint_descriptor ss_iso_source_desc = {
231 .bLength = USB_DT_ENDPOINT_SIZE,
232 .bDescriptorType = USB_DT_ENDPOINT,
233
234 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
235 .wMaxPacketSize = cpu_to_le16(1024),
236 .bInterval = 4,
237 };
238
239 struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc = {
240 .bLength = USB_DT_SS_EP_COMP_SIZE,
241 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
242
243 .bMaxBurst = 0,
244 .bmAttributes = 0,
245 .wBytesPerInterval = cpu_to_le16(1024),
246 };
247
248 static struct usb_endpoint_descriptor ss_iso_sink_desc = {
249 .bLength = USB_DT_ENDPOINT_SIZE,
250 .bDescriptorType = USB_DT_ENDPOINT,
251
252 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
253 .wMaxPacketSize = cpu_to_le16(1024),
254 .bInterval = 4,
255 };
256
257 struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = {
258 .bLength = USB_DT_SS_EP_COMP_SIZE,
259 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
260
261 .bMaxBurst = 0,
262 .bmAttributes = 0,
263 .wBytesPerInterval = cpu_to_le16(1024),
264 };
265
266 static struct usb_descriptor_header *ss_source_sink_descs[] = {
267 (struct usb_descriptor_header *) &source_sink_intf_alt0,
268 (struct usb_descriptor_header *) &ss_source_desc,
269 (struct usb_descriptor_header *) &ss_source_comp_desc,
270 (struct usb_descriptor_header *) &ss_sink_desc,
271 (struct usb_descriptor_header *) &ss_sink_comp_desc,
272 (struct usb_descriptor_header *) &source_sink_intf_alt1,
273 #define SS_ALT_IFC_1_OFFSET 5
274 (struct usb_descriptor_header *) &ss_source_desc,
275 (struct usb_descriptor_header *) &ss_source_comp_desc,
276 (struct usb_descriptor_header *) &ss_sink_desc,
277 (struct usb_descriptor_header *) &ss_sink_comp_desc,
278 (struct usb_descriptor_header *) &ss_iso_source_desc,
279 (struct usb_descriptor_header *) &ss_iso_source_comp_desc,
280 (struct usb_descriptor_header *) &ss_iso_sink_desc,
281 (struct usb_descriptor_header *) &ss_iso_sink_comp_desc,
282 NULL,
283 };
284
285 /* function-specific strings: */
286
287 static struct usb_string strings_sourcesink[] = {
288 [0].s = "source and sink data",
289 { } /* end of list */
290 };
291
292 static struct usb_gadget_strings stringtab_sourcesink = {
293 .language = 0x0409, /* en-us */
294 .strings = strings_sourcesink,
295 };
296
297 static struct usb_gadget_strings *sourcesink_strings[] = {
298 &stringtab_sourcesink,
299 NULL,
300 };
301
302 /*-------------------------------------------------------------------------*/
303
304 struct usb_request *alloc_ep_req(struct usb_ep *ep, int len)
305 {
306 struct usb_request *req;
307
308 req = usb_ep_alloc_request(ep, GFP_ATOMIC);
309 if (req) {
310 if (len)
311 req->length = len;
312 else
313 req->length = buflen;
314 req->buf = kmalloc(req->length, GFP_ATOMIC);
315 if (!req->buf) {
316 usb_ep_free_request(ep, req);
317 req = NULL;
318 }
319 }
320 return req;
321 }
322
323 void free_ep_req(struct usb_ep *ep, struct usb_request *req)
324 {
325 kfree(req->buf);
326 usb_ep_free_request(ep, req);
327 }
328
329 static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
330 {
331 int value;
332
333 if (ep->driver_data) {
334 value = usb_ep_disable(ep);
335 if (value < 0)
336 DBG(cdev, "disable %s --> %d\n",
337 ep->name, value);
338 ep->driver_data = NULL;
339 }
340 }
341
342 void disable_endpoints(struct usb_composite_dev *cdev,
343 struct usb_ep *in, struct usb_ep *out,
344 struct usb_ep *iso_in, struct usb_ep *iso_out)
345 {
346 disable_ep(cdev, in);
347 disable_ep(cdev, out);
348 if (iso_in)
349 disable_ep(cdev, iso_in);
350 if (iso_out)
351 disable_ep(cdev, iso_out);
352 }
353
354 static int
355 sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
356 {
357 struct usb_composite_dev *cdev = c->cdev;
358 struct f_sourcesink *ss = func_to_ss(f);
359 int id;
360 int ret;
361
362 /* allocate interface ID(s) */
363 id = usb_interface_id(c, f);
364 if (id < 0)
365 return id;
366 source_sink_intf_alt0.bInterfaceNumber = id;
367 source_sink_intf_alt1.bInterfaceNumber = id;
368
369 /* allocate bulk endpoints */
370 ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
371 if (!ss->in_ep) {
372 autoconf_fail:
373 ERROR(cdev, "%s: can't autoconfigure on %s\n",
374 f->name, cdev->gadget->name);
375 return -ENODEV;
376 }
377 ss->in_ep->driver_data = cdev; /* claim */
378
379 ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
380 if (!ss->out_ep)
381 goto autoconf_fail;
382 ss->out_ep->driver_data = cdev; /* claim */
383
384 /* sanity check the isoc module parameters */
385 if (isoc_interval < 1)
386 isoc_interval = 1;
387 if (isoc_interval > 16)
388 isoc_interval = 16;
389 if (isoc_mult > 2)
390 isoc_mult = 2;
391 if (isoc_maxburst > 15)
392 isoc_maxburst = 15;
393
394 /* fill in the FS isoc descriptors from the module parameters */
395 fs_iso_source_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
396 1023 : isoc_maxpacket;
397 fs_iso_source_desc.bInterval = isoc_interval;
398 fs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
399 1023 : isoc_maxpacket;
400 fs_iso_sink_desc.bInterval = isoc_interval;
401
402 /* allocate iso endpoints */
403 ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
404 if (!ss->iso_in_ep)
405 goto no_iso;
406 ss->iso_in_ep->driver_data = cdev; /* claim */
407
408 ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
409 if (ss->iso_out_ep) {
410 ss->iso_out_ep->driver_data = cdev; /* claim */
411 } else {
412 ss->iso_in_ep->driver_data = NULL;
413 ss->iso_in_ep = NULL;
414 no_iso:
415 /*
416 * We still want to work even if the UDC doesn't have isoc
417 * endpoints, so null out the alt interface that contains
418 * them and continue.
419 */
420 fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
421 hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
422 ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
423 }
424
425 if (isoc_maxpacket > 1024)
426 isoc_maxpacket = 1024;
427
428 /* support high speed hardware */
429 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
430 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
431
432 /*
433 * Fill in the HS isoc descriptors from the module parameters.
434 * We assume that the user knows what they are doing and won't
435 * give parameters that their UDC doesn't support.
436 */
437 hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
438 hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11;
439 hs_iso_source_desc.bInterval = isoc_interval;
440 hs_iso_source_desc.bEndpointAddress =
441 fs_iso_source_desc.bEndpointAddress;
442
443 hs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
444 hs_iso_sink_desc.wMaxPacketSize |= isoc_mult << 11;
445 hs_iso_sink_desc.bInterval = isoc_interval;
446 hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
447
448 /* support super speed hardware */
449 ss_source_desc.bEndpointAddress =
450 fs_source_desc.bEndpointAddress;
451 ss_sink_desc.bEndpointAddress =
452 fs_sink_desc.bEndpointAddress;
453
454 /*
455 * Fill in the SS isoc descriptors from the module parameters.
456 * We assume that the user knows what they are doing and won't
457 * give parameters that their UDC doesn't support.
458 */
459 ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
460 ss_iso_source_desc.bInterval = isoc_interval;
461 ss_iso_source_comp_desc.bmAttributes = isoc_mult;
462 ss_iso_source_comp_desc.bMaxBurst = isoc_maxburst;
463 ss_iso_source_comp_desc.wBytesPerInterval =
464 isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
465 ss_iso_source_desc.bEndpointAddress =
466 fs_iso_source_desc.bEndpointAddress;
467
468 ss_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
469 ss_iso_sink_desc.bInterval = isoc_interval;
470 ss_iso_sink_comp_desc.bmAttributes = isoc_mult;
471 ss_iso_sink_comp_desc.bMaxBurst = isoc_maxburst;
472 ss_iso_sink_comp_desc.wBytesPerInterval =
473 isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
474 ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
475
476 ret = usb_assign_descriptors(f, fs_source_sink_descs,
477 hs_source_sink_descs, ss_source_sink_descs);
478 if (ret)
479 return ret;
480
481 DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
482 (gadget_is_superspeed(c->cdev->gadget) ? "super" :
483 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
484 f->name, ss->in_ep->name, ss->out_ep->name,
485 ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
486 ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
487 return 0;
488 }
489
490 static void
491 sourcesink_free_func(struct usb_function *f)
492 {
493 usb_free_all_descriptors(f);
494 kfree(func_to_ss(f));
495 }
496
497 /* optionally require specific source/sink data patterns */
498 static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
499 {
500 unsigned i;
501 u8 *buf = req->buf;
502 struct usb_composite_dev *cdev = ss->function.config->cdev;
503
504 if (pattern == 2)
505 return 0;
506
507 for (i = 0; i < req->actual; i++, buf++) {
508 switch (pattern) {
509
510 /* all-zeroes has no synchronization issues */
511 case 0:
512 if (*buf == 0)
513 continue;
514 break;
515
516 /* "mod63" stays in sync with short-terminated transfers,
517 * OR otherwise when host and gadget agree on how large
518 * each usb transfer request should be. Resync is done
519 * with set_interface or set_config. (We *WANT* it to
520 * get quickly out of sync if controllers or their drivers
521 * stutter for any reason, including buffer duplication...)
522 */
523 case 1:
524 if (*buf == (u8)(i % 63))
525 continue;
526 break;
527 }
528 ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
529 usb_ep_set_halt(ss->out_ep);
530 return -EINVAL;
531 }
532 return 0;
533 }
534
535 static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
536 {
537 unsigned i;
538 u8 *buf = req->buf;
539
540 switch (pattern) {
541 case 0:
542 memset(req->buf, 0, req->length);
543 break;
544 case 1:
545 for (i = 0; i < req->length; i++)
546 *buf++ = (u8) (i % 63);
547 break;
548 case 2:
549 break;
550 }
551 }
552
553 static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
554 {
555 struct usb_composite_dev *cdev;
556 struct f_sourcesink *ss = ep->driver_data;
557 int status = req->status;
558
559 /* driver_data will be null if ep has been disabled */
560 if (!ss)
561 return;
562
563 cdev = ss->function.config->cdev;
564
565 switch (status) {
566
567 case 0: /* normal completion? */
568 if (ep == ss->out_ep) {
569 check_read_data(ss, req);
570 if (pattern != 2)
571 memset(req->buf, 0x55, req->length);
572 }
573 break;
574
575 /* this endpoint is normally active while we're configured */
576 case -ECONNABORTED: /* hardware forced ep reset */
577 case -ECONNRESET: /* request dequeued */
578 case -ESHUTDOWN: /* disconnect from host */
579 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
580 req->actual, req->length);
581 if (ep == ss->out_ep)
582 check_read_data(ss, req);
583 free_ep_req(ep, req);
584 return;
585
586 case -EOVERFLOW: /* buffer overrun on read means that
587 * we didn't provide a big enough
588 * buffer.
589 */
590 default:
591 #if 1
592 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
593 status, req->actual, req->length);
594 #endif
595 case -EREMOTEIO: /* short read */
596 break;
597 }
598
599 status = usb_ep_queue(ep, req, GFP_ATOMIC);
600 if (status) {
601 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
602 ep->name, req->length, status);
603 usb_ep_set_halt(ep);
604 /* FIXME recover later ... somehow */
605 }
606 }
607
608 static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
609 bool is_iso, int speed)
610 {
611 struct usb_ep *ep;
612 struct usb_request *req;
613 int i, size, status;
614
615 for (i = 0; i < 8; i++) {
616 if (is_iso) {
617 switch (speed) {
618 case USB_SPEED_SUPER:
619 size = isoc_maxpacket * (isoc_mult + 1) *
620 (isoc_maxburst + 1);
621 break;
622 case USB_SPEED_HIGH:
623 size = isoc_maxpacket * (isoc_mult + 1);
624 break;
625 default:
626 size = isoc_maxpacket > 1023 ?
627 1023 : isoc_maxpacket;
628 break;
629 }
630 ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
631 req = alloc_ep_req(ep, size);
632 } else {
633 ep = is_in ? ss->in_ep : ss->out_ep;
634 req = alloc_ep_req(ep, 0);
635 }
636
637 if (!req)
638 return -ENOMEM;
639
640 req->complete = source_sink_complete;
641 if (is_in)
642 reinit_write_data(ep, req);
643 else if (pattern != 2)
644 memset(req->buf, 0x55, req->length);
645
646 status = usb_ep_queue(ep, req, GFP_ATOMIC);
647 if (status) {
648 struct usb_composite_dev *cdev;
649
650 cdev = ss->function.config->cdev;
651 ERROR(cdev, "start %s%s %s --> %d\n",
652 is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
653 ep->name, status);
654 free_ep_req(ep, req);
655 }
656
657 if (!is_iso)
658 break;
659 }
660
661 return status;
662 }
663
664 static void disable_source_sink(struct f_sourcesink *ss)
665 {
666 struct usb_composite_dev *cdev;
667
668 cdev = ss->function.config->cdev;
669 disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
670 ss->iso_out_ep);
671 VDBG(cdev, "%s disabled\n", ss->function.name);
672 }
673
674 static int
675 enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
676 int alt)
677 {
678 int result = 0;
679 int speed = cdev->gadget->speed;
680 struct usb_ep *ep;
681
682 /* one bulk endpoint writes (sources) zeroes IN (to the host) */
683 ep = ss->in_ep;
684 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
685 if (result)
686 return result;
687 result = usb_ep_enable(ep);
688 if (result < 0)
689 return result;
690 ep->driver_data = ss;
691
692 result = source_sink_start_ep(ss, true, false, speed);
693 if (result < 0) {
694 fail:
695 ep = ss->in_ep;
696 usb_ep_disable(ep);
697 ep->driver_data = NULL;
698 return result;
699 }
700
701 /* one bulk endpoint reads (sinks) anything OUT (from the host) */
702 ep = ss->out_ep;
703 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
704 if (result)
705 goto fail;
706 result = usb_ep_enable(ep);
707 if (result < 0)
708 goto fail;
709 ep->driver_data = ss;
710
711 result = source_sink_start_ep(ss, false, false, speed);
712 if (result < 0) {
713 fail2:
714 ep = ss->out_ep;
715 usb_ep_disable(ep);
716 ep->driver_data = NULL;
717 goto fail;
718 }
719
720 if (alt == 0)
721 goto out;
722
723 /* one iso endpoint writes (sources) zeroes IN (to the host) */
724 ep = ss->iso_in_ep;
725 if (ep) {
726 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
727 if (result)
728 goto fail2;
729 result = usb_ep_enable(ep);
730 if (result < 0)
731 goto fail2;
732 ep->driver_data = ss;
733
734 result = source_sink_start_ep(ss, true, true, speed);
735 if (result < 0) {
736 fail3:
737 ep = ss->iso_in_ep;
738 if (ep) {
739 usb_ep_disable(ep);
740 ep->driver_data = NULL;
741 }
742 goto fail2;
743 }
744 }
745
746 /* one iso endpoint reads (sinks) anything OUT (from the host) */
747 ep = ss->iso_out_ep;
748 if (ep) {
749 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
750 if (result)
751 goto fail3;
752 result = usb_ep_enable(ep);
753 if (result < 0)
754 goto fail3;
755 ep->driver_data = ss;
756
757 result = source_sink_start_ep(ss, false, true, speed);
758 if (result < 0) {
759 usb_ep_disable(ep);
760 ep->driver_data = NULL;
761 goto fail3;
762 }
763 }
764 out:
765 ss->cur_alt = alt;
766
767 DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
768 return result;
769 }
770
771 static int sourcesink_set_alt(struct usb_function *f,
772 unsigned intf, unsigned alt)
773 {
774 struct f_sourcesink *ss = func_to_ss(f);
775 struct usb_composite_dev *cdev = f->config->cdev;
776
777 if (ss->in_ep->driver_data)
778 disable_source_sink(ss);
779 return enable_source_sink(cdev, ss, alt);
780 }
781
782 static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
783 {
784 struct f_sourcesink *ss = func_to_ss(f);
785
786 return ss->cur_alt;
787 }
788
789 static void sourcesink_disable(struct usb_function *f)
790 {
791 struct f_sourcesink *ss = func_to_ss(f);
792
793 disable_source_sink(ss);
794 }
795
796 /*-------------------------------------------------------------------------*/
797
798 static int sourcesink_setup(struct usb_function *f,
799 const struct usb_ctrlrequest *ctrl)
800 {
801 struct usb_configuration *c = f->config;
802 struct usb_request *req = c->cdev->req;
803 int value = -EOPNOTSUPP;
804 u16 w_index = le16_to_cpu(ctrl->wIndex);
805 u16 w_value = le16_to_cpu(ctrl->wValue);
806 u16 w_length = le16_to_cpu(ctrl->wLength);
807
808 req->length = USB_COMP_EP0_BUFSIZ;
809
810 /* composite driver infrastructure handles everything except
811 * the two control test requests.
812 */
813 switch (ctrl->bRequest) {
814
815 /*
816 * These are the same vendor-specific requests supported by
817 * Intel's USB 2.0 compliance test devices. We exceed that
818 * device spec by allowing multiple-packet requests.
819 *
820 * NOTE: the Control-OUT data stays in req->buf ... better
821 * would be copying it into a scratch buffer, so that other
822 * requests may safely intervene.
823 */
824 case 0x5b: /* control WRITE test -- fill the buffer */
825 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
826 goto unknown;
827 if (w_value || w_index)
828 break;
829 /* just read that many bytes into the buffer */
830 if (w_length > req->length)
831 break;
832 value = w_length;
833 break;
834 case 0x5c: /* control READ test -- return the buffer */
835 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
836 goto unknown;
837 if (w_value || w_index)
838 break;
839 /* expect those bytes are still in the buffer; send back */
840 if (w_length > req->length)
841 break;
842 value = w_length;
843 break;
844
845 default:
846 unknown:
847 VDBG(c->cdev,
848 "unknown control req%02x.%02x v%04x i%04x l%d\n",
849 ctrl->bRequestType, ctrl->bRequest,
850 w_value, w_index, w_length);
851 }
852
853 /* respond with data transfer or status phase? */
854 if (value >= 0) {
855 VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
856 ctrl->bRequestType, ctrl->bRequest,
857 w_value, w_index, w_length);
858 req->zero = 0;
859 req->length = value;
860 value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
861 if (value < 0)
862 ERROR(c->cdev, "source/sink response, err %d\n",
863 value);
864 }
865
866 /* device either stalls (value < 0) or reports success */
867 return value;
868 }
869
870 static struct usb_function *source_sink_alloc_func(
871 struct usb_function_instance *fi)
872 {
873 struct f_sourcesink *ss;
874 struct f_ss_opts *ss_opts;
875
876 ss = kzalloc(sizeof(*ss), GFP_KERNEL);
877 if (!ss)
878 return NULL;
879
880 ss_opts = container_of(fi, struct f_ss_opts, func_inst);
881 pattern = ss_opts->pattern;
882 isoc_interval = ss_opts->isoc_interval;
883 isoc_maxpacket = ss_opts->isoc_maxpacket;
884 isoc_mult = ss_opts->isoc_mult;
885 isoc_maxburst = ss_opts->isoc_maxburst;
886 buflen = ss_opts->bulk_buflen;
887
888 ss->function.name = "source/sink";
889 ss->function.bind = sourcesink_bind;
890 ss->function.set_alt = sourcesink_set_alt;
891 ss->function.get_alt = sourcesink_get_alt;
892 ss->function.disable = sourcesink_disable;
893 ss->function.setup = sourcesink_setup;
894 ss->function.strings = sourcesink_strings;
895
896 ss->function.free_func = sourcesink_free_func;
897
898 return &ss->function;
899 }
900
901 static void acm_free_instance(struct usb_function_instance *fi)
902 {
903 struct f_ss_opts *ss_opts;
904
905 ss_opts = container_of(fi, struct f_ss_opts, func_inst);
906 kfree(ss_opts);
907 }
908
909 static struct usb_function_instance *source_sink_alloc_inst(void)
910 {
911 struct f_ss_opts *ss_opts;
912
913 ss_opts = kzalloc(sizeof(*ss_opts), GFP_KERNEL);
914 if (!ss_opts)
915 return ERR_PTR(-ENOMEM);
916 ss_opts->func_inst.free_func_inst = acm_free_instance;
917 return &ss_opts->func_inst;
918 }
919 DECLARE_USB_FUNCTION(SourceSink, source_sink_alloc_inst,
920 source_sink_alloc_func);
921
922 static int __init sslb_modinit(void)
923 {
924 int ret;
925
926 ret = usb_function_register(&SourceSinkusb_func);
927 if (ret)
928 return ret;
929 ret = lb_modinit();
930 if (ret)
931 usb_function_unregister(&SourceSinkusb_func);
932 return ret;
933 }
934 static void __exit sslb_modexit(void)
935 {
936 usb_function_unregister(&SourceSinkusb_func);
937 lb_modexit();
938 }
939 module_init(sslb_modinit);
940 module_exit(sslb_modexit);
941
942 MODULE_LICENSE("GPL");
This page took 0.075798 seconds and 5 git commands to generate.