1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/slab.h>
16#include <linux/kernel.h>
17#include <linux/device.h>
18#include <linux/module.h>
19
20#include "g_zero.h"
21#include "gadget_chips.h"
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49struct f_sourcesink {
50 struct usb_function function;
51
52 struct usb_ep *in_ep;
53 struct usb_ep *out_ep;
54 struct usb_ep *iso_in_ep;
55 struct usb_ep *iso_out_ep;
56 int cur_alt;
57};
58
59static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
60{
61 return container_of(f, struct f_sourcesink, function);
62}
63
64static unsigned pattern;
65module_param(pattern, uint, S_IRUGO|S_IWUSR);
66MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63, 2 = none");
67
68static unsigned isoc_interval = 4;
69module_param(isoc_interval, uint, S_IRUGO|S_IWUSR);
70MODULE_PARM_DESC(isoc_interval, "1 - 16");
71
72static unsigned isoc_maxpacket = 1024;
73module_param(isoc_maxpacket, uint, S_IRUGO|S_IWUSR);
74MODULE_PARM_DESC(isoc_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)");
75
76static unsigned isoc_mult;
77module_param(isoc_mult, uint, S_IRUGO|S_IWUSR);
78MODULE_PARM_DESC(isoc_mult, "0 - 2 (hs/ss only)");
79
80static unsigned isoc_maxburst;
81module_param(isoc_maxburst, uint, S_IRUGO|S_IWUSR);
82MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)");
83
84
85
86static struct usb_interface_descriptor source_sink_intf_alt0 = {
87 .bLength = USB_DT_INTERFACE_SIZE,
88 .bDescriptorType = USB_DT_INTERFACE,
89
90 .bAlternateSetting = 0,
91 .bNumEndpoints = 2,
92 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
93
94};
95
96static struct usb_interface_descriptor source_sink_intf_alt1 = {
97 .bLength = USB_DT_INTERFACE_SIZE,
98 .bDescriptorType = USB_DT_INTERFACE,
99
100 .bAlternateSetting = 1,
101 .bNumEndpoints = 4,
102 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
103
104};
105
106
107
108static struct usb_endpoint_descriptor fs_source_desc = {
109 .bLength = USB_DT_ENDPOINT_SIZE,
110 .bDescriptorType = USB_DT_ENDPOINT,
111
112 .bEndpointAddress = USB_DIR_IN,
113 .bmAttributes = USB_ENDPOINT_XFER_BULK,
114};
115
116static struct usb_endpoint_descriptor fs_sink_desc = {
117 .bLength = USB_DT_ENDPOINT_SIZE,
118 .bDescriptorType = USB_DT_ENDPOINT,
119
120 .bEndpointAddress = USB_DIR_OUT,
121 .bmAttributes = USB_ENDPOINT_XFER_BULK,
122};
123
124static struct usb_endpoint_descriptor fs_iso_source_desc = {
125 .bLength = USB_DT_ENDPOINT_SIZE,
126 .bDescriptorType = USB_DT_ENDPOINT,
127
128 .bEndpointAddress = USB_DIR_IN,
129 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
130 .wMaxPacketSize = cpu_to_le16(1023),
131 .bInterval = 4,
132};
133
134static struct usb_endpoint_descriptor fs_iso_sink_desc = {
135 .bLength = USB_DT_ENDPOINT_SIZE,
136 .bDescriptorType = USB_DT_ENDPOINT,
137
138 .bEndpointAddress = USB_DIR_OUT,
139 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
140 .wMaxPacketSize = cpu_to_le16(1023),
141 .bInterval = 4,
142};
143
144static struct usb_descriptor_header *fs_source_sink_descs[] = {
145 (struct usb_descriptor_header *) &source_sink_intf_alt0,
146 (struct usb_descriptor_header *) &fs_sink_desc,
147 (struct usb_descriptor_header *) &fs_source_desc,
148 (struct usb_descriptor_header *) &source_sink_intf_alt1,
149#define FS_ALT_IFC_1_OFFSET 3
150 (struct usb_descriptor_header *) &fs_sink_desc,
151 (struct usb_descriptor_header *) &fs_source_desc,
152 (struct usb_descriptor_header *) &fs_iso_sink_desc,
153 (struct usb_descriptor_header *) &fs_iso_source_desc,
154 NULL,
155};
156
157
158
159static struct usb_endpoint_descriptor hs_source_desc = {
160 .bLength = USB_DT_ENDPOINT_SIZE,
161 .bDescriptorType = USB_DT_ENDPOINT,
162
163 .bmAttributes = USB_ENDPOINT_XFER_BULK,
164 .wMaxPacketSize = cpu_to_le16(512),
165};
166
167static struct usb_endpoint_descriptor hs_sink_desc = {
168 .bLength = USB_DT_ENDPOINT_SIZE,
169 .bDescriptorType = USB_DT_ENDPOINT,
170
171 .bmAttributes = USB_ENDPOINT_XFER_BULK,
172 .wMaxPacketSize = cpu_to_le16(512),
173};
174
175static struct usb_endpoint_descriptor hs_iso_source_desc = {
176 .bLength = USB_DT_ENDPOINT_SIZE,
177 .bDescriptorType = USB_DT_ENDPOINT,
178
179 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
180 .wMaxPacketSize = cpu_to_le16(1024),
181 .bInterval = 4,
182};
183
184static struct usb_endpoint_descriptor hs_iso_sink_desc = {
185 .bLength = USB_DT_ENDPOINT_SIZE,
186 .bDescriptorType = USB_DT_ENDPOINT,
187
188 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
189 .wMaxPacketSize = cpu_to_le16(1024),
190 .bInterval = 4,
191};
192
193static struct usb_descriptor_header *hs_source_sink_descs[] = {
194 (struct usb_descriptor_header *) &source_sink_intf_alt0,
195 (struct usb_descriptor_header *) &hs_source_desc,
196 (struct usb_descriptor_header *) &hs_sink_desc,
197 (struct usb_descriptor_header *) &source_sink_intf_alt1,
198#define HS_ALT_IFC_1_OFFSET 3
199 (struct usb_descriptor_header *) &hs_source_desc,
200 (struct usb_descriptor_header *) &hs_sink_desc,
201 (struct usb_descriptor_header *) &hs_iso_source_desc,
202 (struct usb_descriptor_header *) &hs_iso_sink_desc,
203 NULL,
204};
205
206
207
208static struct usb_endpoint_descriptor ss_source_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
216struct usb_ss_ep_comp_descriptor ss_source_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
225static struct usb_endpoint_descriptor ss_sink_desc = {
226 .bLength = USB_DT_ENDPOINT_SIZE,
227 .bDescriptorType = USB_DT_ENDPOINT,
228
229 .bmAttributes = USB_ENDPOINT_XFER_BULK,
230 .wMaxPacketSize = cpu_to_le16(1024),
231};
232
233struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = {
234 .bLength = USB_DT_SS_EP_COMP_SIZE,
235 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
236
237 .bMaxBurst = 0,
238 .bmAttributes = 0,
239 .wBytesPerInterval = 0,
240};
241
242static struct usb_endpoint_descriptor ss_iso_source_desc = {
243 .bLength = USB_DT_ENDPOINT_SIZE,
244 .bDescriptorType = USB_DT_ENDPOINT,
245
246 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
247 .wMaxPacketSize = cpu_to_le16(1024),
248 .bInterval = 4,
249};
250
251struct usb_ss_ep_comp_descriptor ss_iso_source_comp_desc = {
252 .bLength = USB_DT_SS_EP_COMP_SIZE,
253 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
254
255 .bMaxBurst = 0,
256 .bmAttributes = 0,
257 .wBytesPerInterval = cpu_to_le16(1024),
258};
259
260static struct usb_endpoint_descriptor ss_iso_sink_desc = {
261 .bLength = USB_DT_ENDPOINT_SIZE,
262 .bDescriptorType = USB_DT_ENDPOINT,
263
264 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
265 .wMaxPacketSize = cpu_to_le16(1024),
266 .bInterval = 4,
267};
268
269struct usb_ss_ep_comp_descriptor ss_iso_sink_comp_desc = {
270 .bLength = USB_DT_SS_EP_COMP_SIZE,
271 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
272
273 .bMaxBurst = 0,
274 .bmAttributes = 0,
275 .wBytesPerInterval = cpu_to_le16(1024),
276};
277
278static struct usb_descriptor_header *ss_source_sink_descs[] = {
279 (struct usb_descriptor_header *) &source_sink_intf_alt0,
280 (struct usb_descriptor_header *) &ss_source_desc,
281 (struct usb_descriptor_header *) &ss_source_comp_desc,
282 (struct usb_descriptor_header *) &ss_sink_desc,
283 (struct usb_descriptor_header *) &ss_sink_comp_desc,
284 (struct usb_descriptor_header *) &source_sink_intf_alt1,
285#define SS_ALT_IFC_1_OFFSET 5
286 (struct usb_descriptor_header *) &ss_source_desc,
287 (struct usb_descriptor_header *) &ss_source_comp_desc,
288 (struct usb_descriptor_header *) &ss_sink_desc,
289 (struct usb_descriptor_header *) &ss_sink_comp_desc,
290 (struct usb_descriptor_header *) &ss_iso_source_desc,
291 (struct usb_descriptor_header *) &ss_iso_source_comp_desc,
292 (struct usb_descriptor_header *) &ss_iso_sink_desc,
293 (struct usb_descriptor_header *) &ss_iso_sink_comp_desc,
294 NULL,
295};
296
297
298
299static struct usb_string strings_sourcesink[] = {
300 [0].s = "source and sink data",
301 { }
302};
303
304static struct usb_gadget_strings stringtab_sourcesink = {
305 .language = 0x0409,
306 .strings = strings_sourcesink,
307};
308
309static struct usb_gadget_strings *sourcesink_strings[] = {
310 &stringtab_sourcesink,
311 NULL,
312};
313
314
315
316static int __init
317sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
318{
319 struct usb_composite_dev *cdev = c->cdev;
320 struct f_sourcesink *ss = func_to_ss(f);
321 int id;
322
323
324 id = usb_interface_id(c, f);
325 if (id < 0)
326 return id;
327 source_sink_intf_alt0.bInterfaceNumber = id;
328 source_sink_intf_alt1.bInterfaceNumber = id;
329
330
331 ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
332 if (!ss->in_ep) {
333autoconf_fail:
334 ERROR(cdev, "%s: can't autoconfigure on %s\n",
335 f->name, cdev->gadget->name);
336 return -ENODEV;
337 }
338 ss->in_ep->driver_data = cdev;
339
340 ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
341 if (!ss->out_ep)
342 goto autoconf_fail;
343 ss->out_ep->driver_data = cdev;
344
345
346 if (isoc_interval < 1)
347 isoc_interval = 1;
348 if (isoc_interval > 16)
349 isoc_interval = 16;
350 if (isoc_mult > 2)
351 isoc_mult = 2;
352 if (isoc_maxburst > 15)
353 isoc_maxburst = 15;
354
355
356 fs_iso_source_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
357 1023 : isoc_maxpacket;
358 fs_iso_source_desc.bInterval = isoc_interval;
359 fs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
360 1023 : isoc_maxpacket;
361 fs_iso_sink_desc.bInterval = isoc_interval;
362
363
364 ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
365 if (!ss->iso_in_ep)
366 goto no_iso;
367 ss->iso_in_ep->driver_data = cdev;
368
369 ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
370 if (ss->iso_out_ep) {
371 ss->iso_out_ep->driver_data = cdev;
372 } else {
373 ss->iso_in_ep->driver_data = NULL;
374 ss->iso_in_ep = NULL;
375no_iso:
376
377
378
379
380
381 fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
382 hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
383 ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
384 }
385
386 if (isoc_maxpacket > 1024)
387 isoc_maxpacket = 1024;
388
389
390 if (gadget_is_dualspeed(c->cdev->gadget)) {
391 hs_source_desc.bEndpointAddress =
392 fs_source_desc.bEndpointAddress;
393 hs_sink_desc.bEndpointAddress =
394 fs_sink_desc.bEndpointAddress;
395
396
397
398
399
400
401 hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
402 hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11;
403 hs_iso_source_desc.bInterval = isoc_interval;
404 hs_iso_source_desc.bEndpointAddress =
405 fs_iso_source_desc.bEndpointAddress;
406
407 hs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
408 hs_iso_sink_desc.wMaxPacketSize |= isoc_mult << 11;
409 hs_iso_sink_desc.bInterval = isoc_interval;
410 hs_iso_sink_desc.bEndpointAddress =
411 fs_iso_sink_desc.bEndpointAddress;
412
413 f->hs_descriptors = hs_source_sink_descs;
414 }
415
416
417 if (gadget_is_superspeed(c->cdev->gadget)) {
418 ss_source_desc.bEndpointAddress =
419 fs_source_desc.bEndpointAddress;
420 ss_sink_desc.bEndpointAddress =
421 fs_sink_desc.bEndpointAddress;
422
423
424
425
426
427
428 ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
429 ss_iso_source_desc.bInterval = isoc_interval;
430 ss_iso_source_comp_desc.bmAttributes = isoc_mult;
431 ss_iso_source_comp_desc.bMaxBurst = isoc_maxburst;
432 ss_iso_source_comp_desc.wBytesPerInterval =
433 isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
434 ss_iso_source_desc.bEndpointAddress =
435 fs_iso_source_desc.bEndpointAddress;
436
437 ss_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
438 ss_iso_sink_desc.bInterval = isoc_interval;
439 ss_iso_sink_comp_desc.bmAttributes = isoc_mult;
440 ss_iso_sink_comp_desc.bMaxBurst = isoc_maxburst;
441 ss_iso_sink_comp_desc.wBytesPerInterval =
442 isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
443 ss_iso_sink_desc.bEndpointAddress =
444 fs_iso_sink_desc.bEndpointAddress;
445
446 f->ss_descriptors = ss_source_sink_descs;
447 }
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
458static void
459sourcesink_unbind(struct usb_configuration *c, struct usb_function *f)
460{
461 kfree(func_to_ss(f));
462}
463
464
465static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
466{
467 unsigned i;
468 u8 *buf = req->buf;
469 struct usb_composite_dev *cdev = ss->function.config->cdev;
470
471 if (pattern == 2)
472 return 0;
473
474 for (i = 0; i < req->actual; i++, buf++) {
475 switch (pattern) {
476
477
478 case 0:
479 if (*buf == 0)
480 continue;
481 break;
482
483
484
485
486
487
488
489
490 case 1:
491 if (*buf == (u8)(i % 63))
492 continue;
493 break;
494 }
495 ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
496 usb_ep_set_halt(ss->out_ep);
497 return -EINVAL;
498 }
499 return 0;
500}
501
502static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
503{
504 unsigned i;
505 u8 *buf = req->buf;
506
507 switch (pattern) {
508 case 0:
509 memset(req->buf, 0, req->length);
510 break;
511 case 1:
512 for (i = 0; i < req->length; i++)
513 *buf++ = (u8) (i % 63);
514 break;
515 case 2:
516 break;
517 }
518}
519
520static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
521{
522 struct usb_composite_dev *cdev;
523 struct f_sourcesink *ss = ep->driver_data;
524 int status = req->status;
525
526
527 if (!ss)
528 return;
529
530 cdev = ss->function.config->cdev;
531
532 switch (status) {
533
534 case 0:
535 if (ep == ss->out_ep) {
536 check_read_data(ss, req);
537 if (pattern != 2)
538 memset(req->buf, 0x55, req->length);
539 } else
540 reinit_write_data(ep, req);
541 break;
542
543
544 case -ECONNABORTED:
545 case -ECONNRESET:
546 case -ESHUTDOWN:
547 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
548 req->actual, req->length);
549 if (ep == ss->out_ep)
550 check_read_data(ss, req);
551 free_ep_req(ep, req);
552 return;
553
554 case -EOVERFLOW:
555
556
557
558 default:
559#if 1
560 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
561 status, req->actual, req->length);
562#endif
563 case -EREMOTEIO:
564 break;
565 }
566
567 status = usb_ep_queue(ep, req, GFP_ATOMIC);
568 if (status) {
569 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
570 ep->name, req->length, status);
571 usb_ep_set_halt(ep);
572
573 }
574}
575
576static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
577 bool is_iso, int speed)
578{
579 struct usb_ep *ep;
580 struct usb_request *req;
581 int i, size, status;
582
583 for (i = 0; i < 8; i++) {
584 if (is_iso) {
585 switch (speed) {
586 case USB_SPEED_SUPER:
587 size = isoc_maxpacket * (isoc_mult + 1) *
588 (isoc_maxburst + 1);
589 break;
590 case USB_SPEED_HIGH:
591 size = isoc_maxpacket * (isoc_mult + 1);
592 break;
593 default:
594 size = isoc_maxpacket > 1023 ?
595 1023 : isoc_maxpacket;
596 break;
597 }
598 ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
599 req = alloc_ep_req(ep, size);
600 } else {
601 ep = is_in ? ss->in_ep : ss->out_ep;
602 req = alloc_ep_req(ep, 0);
603 }
604
605 if (!req)
606 return -ENOMEM;
607
608 req->complete = source_sink_complete;
609 if (is_in)
610 reinit_write_data(ep, req);
611 else if (pattern != 2)
612 memset(req->buf, 0x55, req->length);
613
614 status = usb_ep_queue(ep, req, GFP_ATOMIC);
615 if (status) {
616 struct usb_composite_dev *cdev;
617
618 cdev = ss->function.config->cdev;
619 ERROR(cdev, "start %s%s %s --> %d\n",
620 is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
621 ep->name, status);
622 free_ep_req(ep, req);
623 }
624
625 if (!is_iso)
626 break;
627 }
628
629 return status;
630}
631
632static void disable_source_sink(struct f_sourcesink *ss)
633{
634 struct usb_composite_dev *cdev;
635
636 cdev = ss->function.config->cdev;
637 disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
638 ss->iso_out_ep);
639 VDBG(cdev, "%s disabled\n", ss->function.name);
640}
641
642static int
643enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
644 int alt)
645{
646 int result = 0;
647 int speed = cdev->gadget->speed;
648 struct usb_ep *ep;
649
650
651 ep = ss->in_ep;
652 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
653 if (result)
654 return result;
655 result = usb_ep_enable(ep);
656 if (result < 0)
657 return result;
658 ep->driver_data = ss;
659
660 result = source_sink_start_ep(ss, true, false, speed);
661 if (result < 0) {
662fail:
663 ep = ss->in_ep;
664 usb_ep_disable(ep);
665 ep->driver_data = NULL;
666 return result;
667 }
668
669
670 ep = ss->out_ep;
671 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
672 if (result)
673 goto fail;
674 result = usb_ep_enable(ep);
675 if (result < 0)
676 goto fail;
677 ep->driver_data = ss;
678
679 result = source_sink_start_ep(ss, false, false, speed);
680 if (result < 0) {
681fail2:
682 ep = ss->out_ep;
683 usb_ep_disable(ep);
684 ep->driver_data = NULL;
685 goto fail;
686 }
687
688 if (alt == 0)
689 goto out;
690
691
692 ep = ss->iso_in_ep;
693 if (ep) {
694 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
695 if (result)
696 goto fail2;
697 result = usb_ep_enable(ep);
698 if (result < 0)
699 goto fail2;
700 ep->driver_data = ss;
701
702 result = source_sink_start_ep(ss, true, true, speed);
703 if (result < 0) {
704fail3:
705 ep = ss->iso_in_ep;
706 if (ep) {
707 usb_ep_disable(ep);
708 ep->driver_data = NULL;
709 }
710 goto fail2;
711 }
712 }
713
714
715 ep = ss->iso_out_ep;
716 if (ep) {
717 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
718 if (result)
719 goto fail3;
720 result = usb_ep_enable(ep);
721 if (result < 0)
722 goto fail3;
723 ep->driver_data = ss;
724
725 result = source_sink_start_ep(ss, false, true, speed);
726 if (result < 0) {
727 usb_ep_disable(ep);
728 ep->driver_data = NULL;
729 goto fail3;
730 }
731 }
732out:
733 ss->cur_alt = alt;
734
735 DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
736 return result;
737}
738
739static int sourcesink_set_alt(struct usb_function *f,
740 unsigned intf, unsigned alt)
741{
742 struct f_sourcesink *ss = func_to_ss(f);
743 struct usb_composite_dev *cdev = f->config->cdev;
744
745 if (ss->in_ep->driver_data)
746 disable_source_sink(ss);
747 return enable_source_sink(cdev, ss, alt);
748}
749
750static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
751{
752 struct f_sourcesink *ss = func_to_ss(f);
753
754 return ss->cur_alt;
755}
756
757static void sourcesink_disable(struct usb_function *f)
758{
759 struct f_sourcesink *ss = func_to_ss(f);
760
761 disable_source_sink(ss);
762}
763
764
765
766static int __init sourcesink_bind_config(struct usb_configuration *c)
767{
768 struct f_sourcesink *ss;
769 int status;
770
771 ss = kzalloc(sizeof *ss, GFP_KERNEL);
772 if (!ss)
773 return -ENOMEM;
774
775 ss->function.name = "source/sink";
776 ss->function.descriptors = fs_source_sink_descs;
777 ss->function.bind = sourcesink_bind;
778 ss->function.unbind = sourcesink_unbind;
779 ss->function.set_alt = sourcesink_set_alt;
780 ss->function.get_alt = sourcesink_get_alt;
781 ss->function.disable = sourcesink_disable;
782
783 status = usb_add_function(c, &ss->function);
784 if (status)
785 kfree(ss);
786 return status;
787}
788
789static int sourcesink_setup(struct usb_configuration *c,
790 const struct usb_ctrlrequest *ctrl)
791{
792 struct usb_request *req = c->cdev->req;
793 int value = -EOPNOTSUPP;
794 u16 w_index = le16_to_cpu(ctrl->wIndex);
795 u16 w_value = le16_to_cpu(ctrl->wValue);
796 u16 w_length = le16_to_cpu(ctrl->wLength);
797
798 req->length = USB_BUFSIZ;
799
800
801
802
803 switch (ctrl->bRequest) {
804
805
806
807
808
809
810
811
812
813
814 case 0x5b:
815 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
816 goto unknown;
817 if (w_value || w_index)
818 break;
819
820 if (w_length > req->length)
821 break;
822 value = w_length;
823 break;
824 case 0x5c:
825 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
826 goto unknown;
827 if (w_value || w_index)
828 break;
829
830 if (w_length > req->length)
831 break;
832 value = w_length;
833 break;
834
835 default:
836unknown:
837 VDBG(c->cdev,
838 "unknown control req%02x.%02x v%04x i%04x l%d\n",
839 ctrl->bRequestType, ctrl->bRequest,
840 w_value, w_index, w_length);
841 }
842
843
844 if (value >= 0) {
845 VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
846 ctrl->bRequestType, ctrl->bRequest,
847 w_value, w_index, w_length);
848 req->zero = 0;
849 req->length = value;
850 value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
851 if (value < 0)
852 ERROR(c->cdev, "source/sink response, err %d\n",
853 value);
854 }
855
856
857 return value;
858}
859
860static struct usb_configuration sourcesink_driver = {
861 .label = "source/sink",
862 .strings = sourcesink_strings,
863 .setup = sourcesink_setup,
864 .bConfigurationValue = 3,
865 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
866
867};
868
869
870
871
872
873int __init sourcesink_add(struct usb_composite_dev *cdev, bool autoresume)
874{
875 int id;
876
877
878 id = usb_string_id(cdev);
879 if (id < 0)
880 return id;
881 strings_sourcesink[0].id = id;
882
883 source_sink_intf_alt0.iInterface = id;
884 source_sink_intf_alt1.iInterface = id;
885 sourcesink_driver.iConfiguration = id;
886
887
888 if (autoresume)
889 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
890
891
892 if (gadget_is_otg(cdev->gadget)) {
893 sourcesink_driver.descriptors = otg_desc;
894 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
895 }
896
897 return usb_add_config(cdev, &sourcesink_driver, sourcesink_bind_config);
898}
899