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};
55
56static inline struct f_sourcesink *func_to_ss(struct usb_function *f)
57{
58 return container_of(f, struct f_sourcesink, function);
59}
60
61static unsigned pattern;
62module_param(pattern, uint, 0);
63MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63 ");
64
65
66
67static struct usb_interface_descriptor source_sink_intf = {
68 .bLength = sizeof source_sink_intf,
69 .bDescriptorType = USB_DT_INTERFACE,
70
71 .bNumEndpoints = 2,
72 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
73
74};
75
76
77
78static struct usb_endpoint_descriptor fs_source_desc = {
79 .bLength = USB_DT_ENDPOINT_SIZE,
80 .bDescriptorType = USB_DT_ENDPOINT,
81
82 .bEndpointAddress = USB_DIR_IN,
83 .bmAttributes = USB_ENDPOINT_XFER_BULK,
84};
85
86static struct usb_endpoint_descriptor fs_sink_desc = {
87 .bLength = USB_DT_ENDPOINT_SIZE,
88 .bDescriptorType = USB_DT_ENDPOINT,
89
90 .bEndpointAddress = USB_DIR_OUT,
91 .bmAttributes = USB_ENDPOINT_XFER_BULK,
92};
93
94static struct usb_descriptor_header *fs_source_sink_descs[] = {
95 (struct usb_descriptor_header *) &source_sink_intf,
96 (struct usb_descriptor_header *) &fs_sink_desc,
97 (struct usb_descriptor_header *) &fs_source_desc,
98 NULL,
99};
100
101
102
103static struct usb_endpoint_descriptor hs_source_desc = {
104 .bLength = USB_DT_ENDPOINT_SIZE,
105 .bDescriptorType = USB_DT_ENDPOINT,
106
107 .bmAttributes = USB_ENDPOINT_XFER_BULK,
108 .wMaxPacketSize = cpu_to_le16(512),
109};
110
111static struct usb_endpoint_descriptor hs_sink_desc = {
112 .bLength = USB_DT_ENDPOINT_SIZE,
113 .bDescriptorType = USB_DT_ENDPOINT,
114
115 .bmAttributes = USB_ENDPOINT_XFER_BULK,
116 .wMaxPacketSize = cpu_to_le16(512),
117};
118
119static struct usb_descriptor_header *hs_source_sink_descs[] = {
120 (struct usb_descriptor_header *) &source_sink_intf,
121 (struct usb_descriptor_header *) &hs_source_desc,
122 (struct usb_descriptor_header *) &hs_sink_desc,
123 NULL,
124};
125
126
127
128static struct usb_endpoint_descriptor ss_source_desc = {
129 .bLength = USB_DT_ENDPOINT_SIZE,
130 .bDescriptorType = USB_DT_ENDPOINT,
131
132 .bmAttributes = USB_ENDPOINT_XFER_BULK,
133 .wMaxPacketSize = cpu_to_le16(1024),
134};
135
136struct usb_ss_ep_comp_descriptor ss_source_comp_desc = {
137 .bLength = USB_DT_SS_EP_COMP_SIZE,
138 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
139 .bMaxBurst = 0,
140 .bmAttributes = 0,
141 .wBytesPerInterval = 0,
142};
143
144static struct usb_endpoint_descriptor ss_sink_desc = {
145 .bLength = USB_DT_ENDPOINT_SIZE,
146 .bDescriptorType = USB_DT_ENDPOINT,
147
148 .bmAttributes = USB_ENDPOINT_XFER_BULK,
149 .wMaxPacketSize = cpu_to_le16(1024),
150};
151
152struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = {
153 .bLength = USB_DT_SS_EP_COMP_SIZE,
154 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
155 .bMaxBurst = 0,
156 .bmAttributes = 0,
157 .wBytesPerInterval = 0,
158};
159
160static struct usb_descriptor_header *ss_source_sink_descs[] = {
161 (struct usb_descriptor_header *) &source_sink_intf,
162 (struct usb_descriptor_header *) &ss_source_desc,
163 (struct usb_descriptor_header *) &ss_source_comp_desc,
164 (struct usb_descriptor_header *) &ss_sink_desc,
165 (struct usb_descriptor_header *) &ss_sink_comp_desc,
166 NULL,
167};
168
169
170
171static struct usb_string strings_sourcesink[] = {
172 [0].s = "source and sink data",
173 { }
174};
175
176static struct usb_gadget_strings stringtab_sourcesink = {
177 .language = 0x0409,
178 .strings = strings_sourcesink,
179};
180
181static struct usb_gadget_strings *sourcesink_strings[] = {
182 &stringtab_sourcesink,
183 NULL,
184};
185
186
187
188static int __init
189sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
190{
191 struct usb_composite_dev *cdev = c->cdev;
192 struct f_sourcesink *ss = func_to_ss(f);
193 int id;
194
195
196 id = usb_interface_id(c, f);
197 if (id < 0)
198 return id;
199 source_sink_intf.bInterfaceNumber = id;
200
201
202 ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
203 if (!ss->in_ep) {
204autoconf_fail:
205 ERROR(cdev, "%s: can't autoconfigure on %s\n",
206 f->name, cdev->gadget->name);
207 return -ENODEV;
208 }
209 ss->in_ep->driver_data = cdev;
210
211 ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
212 if (!ss->out_ep)
213 goto autoconf_fail;
214 ss->out_ep->driver_data = cdev;
215
216
217 if (gadget_is_dualspeed(c->cdev->gadget)) {
218 hs_source_desc.bEndpointAddress =
219 fs_source_desc.bEndpointAddress;
220 hs_sink_desc.bEndpointAddress =
221 fs_sink_desc.bEndpointAddress;
222 f->hs_descriptors = hs_source_sink_descs;
223 }
224
225
226 if (gadget_is_superspeed(c->cdev->gadget)) {
227 ss_source_desc.bEndpointAddress =
228 fs_source_desc.bEndpointAddress;
229 ss_sink_desc.bEndpointAddress =
230 fs_sink_desc.bEndpointAddress;
231 f->ss_descriptors = ss_source_sink_descs;
232 }
233
234 DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
235 (gadget_is_superspeed(c->cdev->gadget) ? "super" :
236 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
237 f->name, ss->in_ep->name, ss->out_ep->name);
238 return 0;
239}
240
241static void
242sourcesink_unbind(struct usb_configuration *c, struct usb_function *f)
243{
244 kfree(func_to_ss(f));
245}
246
247
248static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
249{
250 unsigned i;
251 u8 *buf = req->buf;
252 struct usb_composite_dev *cdev = ss->function.config->cdev;
253
254 for (i = 0; i < req->actual; i++, buf++) {
255 switch (pattern) {
256
257
258 case 0:
259 if (*buf == 0)
260 continue;
261 break;
262
263
264
265
266
267
268
269
270 case 1:
271 if (*buf == (u8)(i % 63))
272 continue;
273 break;
274 }
275 ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
276 usb_ep_set_halt(ss->out_ep);
277 return -EINVAL;
278 }
279 return 0;
280}
281
282static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
283{
284 unsigned i;
285 u8 *buf = req->buf;
286
287 switch (pattern) {
288 case 0:
289 memset(req->buf, 0, req->length);
290 break;
291 case 1:
292 for (i = 0; i < req->length; i++)
293 *buf++ = (u8) (i % 63);
294 break;
295 }
296}
297
298static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
299{
300 struct f_sourcesink *ss = ep->driver_data;
301 struct usb_composite_dev *cdev = ss->function.config->cdev;
302 int status = req->status;
303
304 switch (status) {
305
306 case 0:
307 if (ep == ss->out_ep) {
308 check_read_data(ss, req);
309 memset(req->buf, 0x55, req->length);
310 } else
311 reinit_write_data(ep, req);
312 break;
313
314
315 case -ECONNABORTED:
316 case -ECONNRESET:
317 case -ESHUTDOWN:
318 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
319 req->actual, req->length);
320 if (ep == ss->out_ep)
321 check_read_data(ss, req);
322 free_ep_req(ep, req);
323 return;
324
325 case -EOVERFLOW:
326
327
328
329 default:
330#if 1
331 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
332 status, req->actual, req->length);
333#endif
334 case -EREMOTEIO:
335 break;
336 }
337
338 status = usb_ep_queue(ep, req, GFP_ATOMIC);
339 if (status) {
340 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
341 ep->name, req->length, status);
342 usb_ep_set_halt(ep);
343
344 }
345}
346
347static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in)
348{
349 struct usb_ep *ep;
350 struct usb_request *req;
351 int status;
352
353 ep = is_in ? ss->in_ep : ss->out_ep;
354 req = alloc_ep_req(ep);
355 if (!req)
356 return -ENOMEM;
357
358 req->complete = source_sink_complete;
359 if (is_in)
360 reinit_write_data(ep, req);
361 else
362 memset(req->buf, 0x55, req->length);
363
364 status = usb_ep_queue(ep, req, GFP_ATOMIC);
365 if (status) {
366 struct usb_composite_dev *cdev;
367
368 cdev = ss->function.config->cdev;
369 ERROR(cdev, "start %s %s --> %d\n",
370 is_in ? "IN" : "OUT",
371 ep->name, status);
372 free_ep_req(ep, req);
373 }
374
375 return status;
376}
377
378static void disable_source_sink(struct f_sourcesink *ss)
379{
380 struct usb_composite_dev *cdev;
381
382 cdev = ss->function.config->cdev;
383 disable_endpoints(cdev, ss->in_ep, ss->out_ep);
384 VDBG(cdev, "%s disabled\n", ss->function.name);
385}
386
387static int
388enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss)
389{
390 int result = 0;
391 struct usb_ep *ep;
392
393
394 ep = ss->in_ep;
395 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
396 if (result)
397 return result;
398 result = usb_ep_enable(ep);
399 if (result < 0)
400 return result;
401 ep->driver_data = ss;
402
403 result = source_sink_start_ep(ss, true);
404 if (result < 0) {
405fail:
406 ep = ss->in_ep;
407 usb_ep_disable(ep);
408 ep->driver_data = NULL;
409 return result;
410 }
411
412
413 ep = ss->out_ep;
414 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
415 if (result)
416 goto fail;
417 result = usb_ep_enable(ep);
418 if (result < 0)
419 goto fail;
420 ep->driver_data = ss;
421
422 result = source_sink_start_ep(ss, false);
423 if (result < 0) {
424 usb_ep_disable(ep);
425 ep->driver_data = NULL;
426 goto fail;
427 }
428
429 DBG(cdev, "%s enabled\n", ss->function.name);
430 return result;
431}
432
433static int sourcesink_set_alt(struct usb_function *f,
434 unsigned intf, unsigned alt)
435{
436 struct f_sourcesink *ss = func_to_ss(f);
437 struct usb_composite_dev *cdev = f->config->cdev;
438
439
440 if (ss->in_ep->driver_data)
441 disable_source_sink(ss);
442 return enable_source_sink(cdev, ss);
443}
444
445static void sourcesink_disable(struct usb_function *f)
446{
447 struct f_sourcesink *ss = func_to_ss(f);
448
449 disable_source_sink(ss);
450}
451
452
453
454static int __init sourcesink_bind_config(struct usb_configuration *c)
455{
456 struct f_sourcesink *ss;
457 int status;
458
459 ss = kzalloc(sizeof *ss, GFP_KERNEL);
460 if (!ss)
461 return -ENOMEM;
462
463 ss->function.name = "source/sink";
464 ss->function.descriptors = fs_source_sink_descs;
465 ss->function.bind = sourcesink_bind;
466 ss->function.unbind = sourcesink_unbind;
467 ss->function.set_alt = sourcesink_set_alt;
468 ss->function.disable = sourcesink_disable;
469
470 status = usb_add_function(c, &ss->function);
471 if (status)
472 kfree(ss);
473 return status;
474}
475
476static int sourcesink_setup(struct usb_configuration *c,
477 const struct usb_ctrlrequest *ctrl)
478{
479 struct usb_request *req = c->cdev->req;
480 int value = -EOPNOTSUPP;
481 u16 w_index = le16_to_cpu(ctrl->wIndex);
482 u16 w_value = le16_to_cpu(ctrl->wValue);
483 u16 w_length = le16_to_cpu(ctrl->wLength);
484
485 req->length = USB_BUFSIZ;
486
487
488
489
490 switch (ctrl->bRequest) {
491
492
493
494
495
496
497
498
499
500
501 case 0x5b:
502 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
503 goto unknown;
504 if (w_value || w_index)
505 break;
506
507 if (w_length > req->length)
508 break;
509 value = w_length;
510 break;
511 case 0x5c:
512 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
513 goto unknown;
514 if (w_value || w_index)
515 break;
516
517 if (w_length > req->length)
518 break;
519 value = w_length;
520 break;
521
522 default:
523unknown:
524 VDBG(c->cdev,
525 "unknown control req%02x.%02x v%04x i%04x l%d\n",
526 ctrl->bRequestType, ctrl->bRequest,
527 w_value, w_index, w_length);
528 }
529
530
531 if (value >= 0) {
532 VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
533 ctrl->bRequestType, ctrl->bRequest,
534 w_value, w_index, w_length);
535 req->zero = 0;
536 req->length = value;
537 value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
538 if (value < 0)
539 ERROR(c->cdev, "source/sinkc response, err %d\n",
540 value);
541 }
542
543
544 return value;
545}
546
547static struct usb_configuration sourcesink_driver = {
548 .label = "source/sink",
549 .strings = sourcesink_strings,
550 .setup = sourcesink_setup,
551 .bConfigurationValue = 3,
552 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
553
554};
555
556
557
558
559
560int __init sourcesink_add(struct usb_composite_dev *cdev, bool autoresume)
561{
562 int id;
563
564
565 id = usb_string_id(cdev);
566 if (id < 0)
567 return id;
568 strings_sourcesink[0].id = id;
569
570 source_sink_intf.iInterface = id;
571 sourcesink_driver.iConfiguration = id;
572
573
574 if (autoresume)
575 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
576
577
578 if (gadget_is_otg(cdev->gadget)) {
579 sourcesink_driver.descriptors = otg_desc;
580 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
581 }
582
583 return usb_add_config(cdev, &sourcesink_driver, sourcesink_bind_config);
584}
585