blob: bac04b0c628eb40d642b9097c55e82666c97a231 [file] [log] [blame]
David Brownella400cad2008-06-19 17:55:23 -07001/*
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.
David Brownella400cad2008-06-19 17:55:23 -070011 */
12
13/* #define VERBOSE_DEBUG */
14
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
David Brownella400cad2008-06-19 17:55:23 -070016#include <linux/kernel.h>
David Brownella400cad2008-06-19 17:55:23 -070017#include <linux/device.h>
Paul Gortmaker6eb0de82011-07-03 16:09:31 -040018#include <linux/module.h>
David Brownella400cad2008-06-19 17:55:23 -070019
20#include "g_zero.h"
21#include "gadget_chips.h"
22
23
24/*
25 * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral
26 * controller drivers.
27 *
28 * This just sinks bulk packets OUT to the peripheral and sources them IN
29 * to the host, optionally with specific data patterns for integrity tests.
30 * As such it supports basic functionality and load tests.
31 *
32 * In terms of control messaging, this supports all the standard requests
33 * plus two that support control-OUT tests. If the optional "autoresume"
34 * mode is enabled, it provides good functional coverage for the "USBCV"
35 * test harness from USB-IF.
36 *
37 * Note that because this doesn't queue more than one request at a time,
38 * some other function must be used to test queueing logic. The network
39 * link (g_ether) is the best overall option for that, since its TX and RX
40 * queues are relatively independent, will receive a range of packet sizes,
41 * and can often be made to run out completely. Those issues are important
42 * when stress testing peripheral controller drivers.
43 *
44 *
45 * This is currently packaged as a configuration driver, which can't be
46 * combined with other functions to make composite devices. However, it
47 * can be combined with other independent configurations.
48 */
49struct f_sourcesink {
50 struct usb_function function;
51
52 struct usb_ep *in_ep;
53 struct usb_ep *out_ep;
Paul Zimmermanb4036cc2012-04-16 14:19:06 -070054 struct usb_ep *iso_in_ep;
55 struct usb_ep *iso_out_ep;
56 int cur_alt;
David Brownella400cad2008-06-19 17:55:23 -070057};
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
David Brownella400cad2008-06-19 17:55:23 -070064static unsigned pattern;
Paul Zimmermanb4036cc2012-04-16 14:19:06 -070065module_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)");
David Brownella400cad2008-06-19 17:55:23 -070083
84/*-------------------------------------------------------------------------*/
85
Paul Zimmermanb4036cc2012-04-16 14:19:06 -070086static struct usb_interface_descriptor source_sink_intf_alt0 = {
87 .bLength = USB_DT_INTERFACE_SIZE,
David Brownella400cad2008-06-19 17:55:23 -070088 .bDescriptorType = USB_DT_INTERFACE,
89
Paul Zimmermanb4036cc2012-04-16 14:19:06 -070090 .bAlternateSetting = 0,
David Brownella400cad2008-06-19 17:55:23 -070091 .bNumEndpoints = 2,
92 .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
Paul Zimmermanb4036cc2012-04-16 14:19:06 -070093 /* .iInterface = DYNAMIC */
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 /* .iInterface = DYNAMIC */
David Brownella400cad2008-06-19 17:55:23 -0700104};
105
106/* full speed support: */
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
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700124static 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
David Brownella400cad2008-06-19 17:55:23 -0700144static struct usb_descriptor_header *fs_source_sink_descs[] = {
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700145 (struct usb_descriptor_header *) &source_sink_intf_alt0,
David Brownella400cad2008-06-19 17:55:23 -0700146 (struct usb_descriptor_header *) &fs_sink_desc,
147 (struct usb_descriptor_header *) &fs_source_desc,
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700148 (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,
David Brownella400cad2008-06-19 17:55:23 -0700154 NULL,
155};
156
157/* high speed support: */
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,
Harvey Harrison551509d2009-02-11 14:11:36 -0800164 .wMaxPacketSize = cpu_to_le16(512),
David Brownella400cad2008-06-19 17:55:23 -0700165};
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,
Harvey Harrison551509d2009-02-11 14:11:36 -0800172 .wMaxPacketSize = cpu_to_le16(512),
David Brownella400cad2008-06-19 17:55:23 -0700173};
174
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700175static 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
David Brownella400cad2008-06-19 17:55:23 -0700193static struct usb_descriptor_header *hs_source_sink_descs[] = {
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700194 (struct usb_descriptor_header *) &source_sink_intf_alt0,
David Brownella400cad2008-06-19 17:55:23 -0700195 (struct usb_descriptor_header *) &hs_source_desc,
196 (struct usb_descriptor_header *) &hs_sink_desc,
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700197 (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,
David Brownella400cad2008-06-19 17:55:23 -0700203 NULL,
204};
205
Amit Blay57c97c02011-07-03 17:29:31 +0300206/* super speed support: */
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,
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700219
Amit Blay57c97c02011-07-03 17:29:31 +0300220 .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,
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700236
Amit Blay57c97c02011-07-03 17:29:31 +0300237 .bMaxBurst = 0,
238 .bmAttributes = 0,
239 .wBytesPerInterval = 0,
240};
241
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700242static 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
Amit Blay57c97c02011-07-03 17:29:31 +0300278static struct usb_descriptor_header *ss_source_sink_descs[] = {
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700279 (struct usb_descriptor_header *) &source_sink_intf_alt0,
Amit Blay57c97c02011-07-03 17:29:31 +0300280 (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,
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700284 (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,
Amit Blay57c97c02011-07-03 17:29:31 +0300294 NULL,
295};
296
David Brownella400cad2008-06-19 17:55:23 -0700297/* function-specific strings: */
298
299static struct usb_string strings_sourcesink[] = {
300 [0].s = "source and sink data",
301 { } /* end of list */
302};
303
304static struct usb_gadget_strings stringtab_sourcesink = {
305 .language = 0x0409, /* en-us */
306 .strings = strings_sourcesink,
307};
308
309static struct usb_gadget_strings *sourcesink_strings[] = {
310 &stringtab_sourcesink,
311 NULL,
312};
313
314/*-------------------------------------------------------------------------*/
315
David Brownella400cad2008-06-19 17:55:23 -0700316static 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;
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200322 int ret;
David Brownella400cad2008-06-19 17:55:23 -0700323
324 /* allocate interface ID(s) */
325 id = usb_interface_id(c, f);
326 if (id < 0)
327 return id;
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700328 source_sink_intf_alt0.bInterfaceNumber = id;
329 source_sink_intf_alt1.bInterfaceNumber = id;
David Brownella400cad2008-06-19 17:55:23 -0700330
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700331 /* allocate bulk endpoints */
David Brownella400cad2008-06-19 17:55:23 -0700332 ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
333 if (!ss->in_ep) {
334autoconf_fail:
335 ERROR(cdev, "%s: can't autoconfigure on %s\n",
336 f->name, cdev->gadget->name);
337 return -ENODEV;
338 }
339 ss->in_ep->driver_data = cdev; /* claim */
340
341 ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
342 if (!ss->out_ep)
343 goto autoconf_fail;
344 ss->out_ep->driver_data = cdev; /* claim */
345
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700346 /* sanity check the isoc module parameters */
347 if (isoc_interval < 1)
348 isoc_interval = 1;
349 if (isoc_interval > 16)
350 isoc_interval = 16;
351 if (isoc_mult > 2)
352 isoc_mult = 2;
353 if (isoc_maxburst > 15)
354 isoc_maxburst = 15;
355
356 /* fill in the FS isoc descriptors from the module parameters */
357 fs_iso_source_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
358 1023 : isoc_maxpacket;
359 fs_iso_source_desc.bInterval = isoc_interval;
360 fs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket > 1023 ?
361 1023 : isoc_maxpacket;
362 fs_iso_sink_desc.bInterval = isoc_interval;
363
364 /* allocate iso endpoints */
365 ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc);
366 if (!ss->iso_in_ep)
367 goto no_iso;
368 ss->iso_in_ep->driver_data = cdev; /* claim */
369
370 ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc);
371 if (ss->iso_out_ep) {
372 ss->iso_out_ep->driver_data = cdev; /* claim */
373 } else {
374 ss->iso_in_ep->driver_data = NULL;
375 ss->iso_in_ep = NULL;
376no_iso:
377 /*
378 * We still want to work even if the UDC doesn't have isoc
379 * endpoints, so null out the alt interface that contains
380 * them and continue.
381 */
382 fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL;
383 hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL;
384 ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL;
385 }
386
387 if (isoc_maxpacket > 1024)
388 isoc_maxpacket = 1024;
389
David Brownella400cad2008-06-19 17:55:23 -0700390 /* support high speed hardware */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200391 hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
392 hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700393
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200394 /*
395 * Fill in the HS isoc descriptors from the module parameters.
396 * We assume that the user knows what they are doing and won't
397 * give parameters that their UDC doesn't support.
398 */
399 hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
400 hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11;
401 hs_iso_source_desc.bInterval = isoc_interval;
402 hs_iso_source_desc.bEndpointAddress =
403 fs_iso_source_desc.bEndpointAddress;
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700404
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200405 hs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
406 hs_iso_sink_desc.wMaxPacketSize |= isoc_mult << 11;
407 hs_iso_sink_desc.bInterval = isoc_interval;
408 hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
David Brownella400cad2008-06-19 17:55:23 -0700409
Amit Blay57c97c02011-07-03 17:29:31 +0300410 /* support super speed hardware */
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200411 ss_source_desc.bEndpointAddress =
412 fs_source_desc.bEndpointAddress;
413 ss_sink_desc.bEndpointAddress =
414 fs_sink_desc.bEndpointAddress;
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700415
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200416 /*
417 * Fill in the SS isoc descriptors from the module parameters.
418 * We assume that the user knows what they are doing and won't
419 * give parameters that their UDC doesn't support.
420 */
421 ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket;
422 ss_iso_source_desc.bInterval = isoc_interval;
423 ss_iso_source_comp_desc.bmAttributes = isoc_mult;
424 ss_iso_source_comp_desc.bMaxBurst = isoc_maxburst;
425 ss_iso_source_comp_desc.wBytesPerInterval =
426 isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
427 ss_iso_source_desc.bEndpointAddress =
428 fs_iso_source_desc.bEndpointAddress;
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700429
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200430 ss_iso_sink_desc.wMaxPacketSize = isoc_maxpacket;
431 ss_iso_sink_desc.bInterval = isoc_interval;
432 ss_iso_sink_comp_desc.bmAttributes = isoc_mult;
433 ss_iso_sink_comp_desc.bMaxBurst = isoc_maxburst;
434 ss_iso_sink_comp_desc.wBytesPerInterval =
435 isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1);
436 ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress;
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700437
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200438 ret = usb_assign_descriptors(f, fs_source_sink_descs,
439 hs_source_sink_descs, ss_source_sink_descs);
440 if (ret)
441 return ret;
Amit Blay57c97c02011-07-03 17:29:31 +0300442
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700443 DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n",
Amit Blay57c97c02011-07-03 17:29:31 +0300444 (gadget_is_superspeed(c->cdev->gadget) ? "super" :
445 (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700446 f->name, ss->in_ep->name, ss->out_ep->name,
447 ss->iso_in_ep ? ss->iso_in_ep->name : "<none>",
448 ss->iso_out_ep ? ss->iso_out_ep->name : "<none>");
David Brownella400cad2008-06-19 17:55:23 -0700449 return 0;
450}
451
Sebastian Andrzej Siewior544aca32012-12-23 21:09:57 +0100452static struct usb_function *global_ss_func;
453
David Brownella400cad2008-06-19 17:55:23 -0700454static void
455sourcesink_unbind(struct usb_configuration *c, struct usb_function *f)
456{
Sebastian Andrzej Siewior10287ba2012-10-22 22:15:06 +0200457 usb_free_all_descriptors(f);
David Brownella400cad2008-06-19 17:55:23 -0700458 kfree(func_to_ss(f));
Sebastian Andrzej Siewior544aca32012-12-23 21:09:57 +0100459 global_ss_func = NULL;
David Brownella400cad2008-06-19 17:55:23 -0700460}
461
462/* optionally require specific source/sink data patterns */
463static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
464{
465 unsigned i;
466 u8 *buf = req->buf;
467 struct usb_composite_dev *cdev = ss->function.config->cdev;
468
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700469 if (pattern == 2)
470 return 0;
471
David Brownella400cad2008-06-19 17:55:23 -0700472 for (i = 0; i < req->actual; i++, buf++) {
473 switch (pattern) {
474
475 /* all-zeroes has no synchronization issues */
476 case 0:
477 if (*buf == 0)
478 continue;
479 break;
480
481 /* "mod63" stays in sync with short-terminated transfers,
482 * OR otherwise when host and gadget agree on how large
483 * each usb transfer request should be. Resync is done
484 * with set_interface or set_config. (We *WANT* it to
485 * get quickly out of sync if controllers or their drivers
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700486 * stutter for any reason, including buffer duplication...)
David Brownella400cad2008-06-19 17:55:23 -0700487 */
488 case 1:
489 if (*buf == (u8)(i % 63))
490 continue;
491 break;
492 }
493 ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
494 usb_ep_set_halt(ss->out_ep);
495 return -EINVAL;
496 }
497 return 0;
498}
499
500static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
501{
502 unsigned i;
503 u8 *buf = req->buf;
504
505 switch (pattern) {
506 case 0:
507 memset(req->buf, 0, req->length);
508 break;
509 case 1:
510 for (i = 0; i < req->length; i++)
511 *buf++ = (u8) (i % 63);
512 break;
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700513 case 2:
514 break;
David Brownella400cad2008-06-19 17:55:23 -0700515 }
516}
517
518static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
519{
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700520 struct usb_composite_dev *cdev;
521 struct f_sourcesink *ss = ep->driver_data;
522 int status = req->status;
523
524 /* driver_data will be null if ep has been disabled */
525 if (!ss)
526 return;
527
528 cdev = ss->function.config->cdev;
David Brownella400cad2008-06-19 17:55:23 -0700529
530 switch (status) {
531
532 case 0: /* normal completion? */
533 if (ep == ss->out_ep) {
534 check_read_data(ss, req);
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700535 if (pattern != 2)
536 memset(req->buf, 0x55, req->length);
Armando Visconti32c9cf22012-12-13 15:11:19 +0100537 }
David Brownella400cad2008-06-19 17:55:23 -0700538 break;
539
540 /* this endpoint is normally active while we're configured */
541 case -ECONNABORTED: /* hardware forced ep reset */
542 case -ECONNRESET: /* request dequeued */
543 case -ESHUTDOWN: /* disconnect from host */
544 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
545 req->actual, req->length);
546 if (ep == ss->out_ep)
547 check_read_data(ss, req);
548 free_ep_req(ep, req);
549 return;
550
551 case -EOVERFLOW: /* buffer overrun on read means that
552 * we didn't provide a big enough
553 * buffer.
554 */
555 default:
556#if 1
557 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
558 status, req->actual, req->length);
559#endif
560 case -EREMOTEIO: /* short read */
561 break;
562 }
563
564 status = usb_ep_queue(ep, req, GFP_ATOMIC);
565 if (status) {
566 ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n",
567 ep->name, req->length, status);
568 usb_ep_set_halt(ep);
569 /* FIXME recover later ... somehow */
570 }
571}
572
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700573static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in,
574 bool is_iso, int speed)
David Brownella400cad2008-06-19 17:55:23 -0700575{
576 struct usb_ep *ep;
577 struct usb_request *req;
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700578 int i, size, status;
David Brownella400cad2008-06-19 17:55:23 -0700579
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700580 for (i = 0; i < 8; i++) {
581 if (is_iso) {
582 switch (speed) {
583 case USB_SPEED_SUPER:
584 size = isoc_maxpacket * (isoc_mult + 1) *
585 (isoc_maxburst + 1);
586 break;
587 case USB_SPEED_HIGH:
588 size = isoc_maxpacket * (isoc_mult + 1);
589 break;
590 default:
591 size = isoc_maxpacket > 1023 ?
592 1023 : isoc_maxpacket;
593 break;
594 }
595 ep = is_in ? ss->iso_in_ep : ss->iso_out_ep;
596 req = alloc_ep_req(ep, size);
597 } else {
598 ep = is_in ? ss->in_ep : ss->out_ep;
599 req = alloc_ep_req(ep, 0);
600 }
David Brownella400cad2008-06-19 17:55:23 -0700601
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700602 if (!req)
603 return -ENOMEM;
David Brownella400cad2008-06-19 17:55:23 -0700604
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700605 req->complete = source_sink_complete;
606 if (is_in)
607 reinit_write_data(ep, req);
608 else if (pattern != 2)
609 memset(req->buf, 0x55, req->length);
David Brownella400cad2008-06-19 17:55:23 -0700610
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700611 status = usb_ep_queue(ep, req, GFP_ATOMIC);
612 if (status) {
613 struct usb_composite_dev *cdev;
614
615 cdev = ss->function.config->cdev;
616 ERROR(cdev, "start %s%s %s --> %d\n",
617 is_iso ? "ISO-" : "", is_in ? "IN" : "OUT",
618 ep->name, status);
619 free_ep_req(ep, req);
620 }
621
622 if (!is_iso)
623 break;
David Brownella400cad2008-06-19 17:55:23 -0700624 }
625
626 return status;
627}
628
629static void disable_source_sink(struct f_sourcesink *ss)
630{
631 struct usb_composite_dev *cdev;
632
633 cdev = ss->function.config->cdev;
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700634 disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep,
635 ss->iso_out_ep);
David Brownella400cad2008-06-19 17:55:23 -0700636 VDBG(cdev, "%s disabled\n", ss->function.name);
637}
638
639static int
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700640enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss,
641 int alt)
David Brownella400cad2008-06-19 17:55:23 -0700642{
643 int result = 0;
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700644 int speed = cdev->gadget->speed;
David Brownella400cad2008-06-19 17:55:23 -0700645 struct usb_ep *ep;
646
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700647 /* one bulk endpoint writes (sources) zeroes IN (to the host) */
David Brownella400cad2008-06-19 17:55:23 -0700648 ep = ss->in_ep;
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300649 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
650 if (result)
651 return result;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300652 result = usb_ep_enable(ep);
David Brownella400cad2008-06-19 17:55:23 -0700653 if (result < 0)
654 return result;
655 ep->driver_data = ss;
656
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700657 result = source_sink_start_ep(ss, true, false, speed);
David Brownella400cad2008-06-19 17:55:23 -0700658 if (result < 0) {
659fail:
660 ep = ss->in_ep;
661 usb_ep_disable(ep);
662 ep->driver_data = NULL;
663 return result;
664 }
665
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700666 /* one bulk endpoint reads (sinks) anything OUT (from the host) */
David Brownella400cad2008-06-19 17:55:23 -0700667 ep = ss->out_ep;
Tatyana Brokhmanea2a1df2011-06-28 16:33:50 +0300668 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
669 if (result)
670 goto fail;
Tatyana Brokhman72c973d2011-06-28 16:33:48 +0300671 result = usb_ep_enable(ep);
David Brownella400cad2008-06-19 17:55:23 -0700672 if (result < 0)
673 goto fail;
674 ep->driver_data = ss;
675
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700676 result = source_sink_start_ep(ss, false, false, speed);
David Brownella400cad2008-06-19 17:55:23 -0700677 if (result < 0) {
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700678fail2:
679 ep = ss->out_ep;
David Brownella400cad2008-06-19 17:55:23 -0700680 usb_ep_disable(ep);
681 ep->driver_data = NULL;
682 goto fail;
683 }
684
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700685 if (alt == 0)
686 goto out;
687
688 /* one iso endpoint writes (sources) zeroes IN (to the host) */
689 ep = ss->iso_in_ep;
690 if (ep) {
691 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
692 if (result)
693 goto fail2;
694 result = usb_ep_enable(ep);
695 if (result < 0)
696 goto fail2;
697 ep->driver_data = ss;
698
699 result = source_sink_start_ep(ss, true, true, speed);
700 if (result < 0) {
701fail3:
702 ep = ss->iso_in_ep;
703 if (ep) {
704 usb_ep_disable(ep);
705 ep->driver_data = NULL;
706 }
707 goto fail2;
708 }
709 }
710
711 /* one iso endpoint reads (sinks) anything OUT (from the host) */
712 ep = ss->iso_out_ep;
713 if (ep) {
714 result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
715 if (result)
716 goto fail3;
717 result = usb_ep_enable(ep);
718 if (result < 0)
719 goto fail3;
720 ep->driver_data = ss;
721
722 result = source_sink_start_ep(ss, false, true, speed);
723 if (result < 0) {
724 usb_ep_disable(ep);
725 ep->driver_data = NULL;
726 goto fail3;
727 }
728 }
729out:
730 ss->cur_alt = alt;
731
732 DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt);
David Brownella400cad2008-06-19 17:55:23 -0700733 return result;
734}
735
736static int sourcesink_set_alt(struct usb_function *f,
737 unsigned intf, unsigned alt)
738{
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700739 struct f_sourcesink *ss = func_to_ss(f);
740 struct usb_composite_dev *cdev = f->config->cdev;
David Brownella400cad2008-06-19 17:55:23 -0700741
David Brownella400cad2008-06-19 17:55:23 -0700742 if (ss->in_ep->driver_data)
743 disable_source_sink(ss);
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700744 return enable_source_sink(cdev, ss, alt);
745}
746
747static int sourcesink_get_alt(struct usb_function *f, unsigned intf)
748{
749 struct f_sourcesink *ss = func_to_ss(f);
750
751 return ss->cur_alt;
David Brownella400cad2008-06-19 17:55:23 -0700752}
753
754static void sourcesink_disable(struct usb_function *f)
755{
756 struct f_sourcesink *ss = func_to_ss(f);
757
758 disable_source_sink(ss);
759}
760
David Brownella400cad2008-06-19 17:55:23 -0700761/*-------------------------------------------------------------------------*/
Sebastian Andrzej Siewior544aca32012-12-23 21:09:57 +0100762static int sourcesink_setup(struct usb_function *f,
David Brownella400cad2008-06-19 17:55:23 -0700763 const struct usb_ctrlrequest *ctrl)
764{
Sebastian Andrzej Siewior544aca32012-12-23 21:09:57 +0100765 struct usb_configuration *c = f->config;
David Brownella400cad2008-06-19 17:55:23 -0700766 struct usb_request *req = c->cdev->req;
767 int value = -EOPNOTSUPP;
768 u16 w_index = le16_to_cpu(ctrl->wIndex);
769 u16 w_value = le16_to_cpu(ctrl->wValue);
770 u16 w_length = le16_to_cpu(ctrl->wLength);
771
Sebastian Andrzej Siewiore13f17f2012-09-10 15:01:51 +0200772 req->length = USB_COMP_EP0_BUFSIZ;
Bob Liu5030ec72011-06-23 17:09:49 -0400773
David Brownella400cad2008-06-19 17:55:23 -0700774 /* composite driver infrastructure handles everything except
775 * the two control test requests.
776 */
777 switch (ctrl->bRequest) {
778
779 /*
780 * These are the same vendor-specific requests supported by
781 * Intel's USB 2.0 compliance test devices. We exceed that
782 * device spec by allowing multiple-packet requests.
783 *
784 * NOTE: the Control-OUT data stays in req->buf ... better
785 * would be copying it into a scratch buffer, so that other
786 * requests may safely intervene.
787 */
788 case 0x5b: /* control WRITE test -- fill the buffer */
789 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
790 goto unknown;
791 if (w_value || w_index)
792 break;
793 /* just read that many bytes into the buffer */
794 if (w_length > req->length)
795 break;
796 value = w_length;
797 break;
798 case 0x5c: /* control READ test -- return the buffer */
799 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
800 goto unknown;
801 if (w_value || w_index)
802 break;
803 /* expect those bytes are still in the buffer; send back */
804 if (w_length > req->length)
805 break;
806 value = w_length;
807 break;
808
809 default:
810unknown:
811 VDBG(c->cdev,
812 "unknown control req%02x.%02x v%04x i%04x l%d\n",
813 ctrl->bRequestType, ctrl->bRequest,
814 w_value, w_index, w_length);
815 }
816
817 /* respond with data transfer or status phase? */
818 if (value >= 0) {
819 VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
820 ctrl->bRequestType, ctrl->bRequest,
821 w_value, w_index, w_length);
822 req->zero = 0;
823 req->length = value;
824 value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
825 if (value < 0)
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700826 ERROR(c->cdev, "source/sink response, err %d\n",
David Brownella400cad2008-06-19 17:55:23 -0700827 value);
828 }
829
830 /* device either stalls (value < 0) or reports success */
831 return value;
832}
833
Sebastian Andrzej Siewior544aca32012-12-23 21:09:57 +0100834static int __init sourcesink_bind_config(struct usb_configuration *c)
835{
836 struct f_sourcesink *ss;
837 int status;
838
839 ss = kzalloc(sizeof(*ss), GFP_KERNEL);
840 if (!ss)
841 return -ENOMEM;
842
843 global_ss_func = &ss->function;
844
845 ss->function.name = "source/sink";
846 ss->function.bind = sourcesink_bind;
847 ss->function.unbind = sourcesink_unbind;
848 ss->function.set_alt = sourcesink_set_alt;
849 ss->function.get_alt = sourcesink_get_alt;
850 ss->function.disable = sourcesink_disable;
851 ss->function.setup = sourcesink_setup;
852
853 status = usb_add_function(c, &ss->function);
854 if (status)
855 kfree(ss);
856 return status;
857}
858
859static int ss_config_setup(struct usb_configuration *c,
860 const struct usb_ctrlrequest *ctrl)
861{
862 if (!global_ss_func)
863 return -EOPNOTSUPP;
864 switch (ctrl->bRequest) {
865 case 0x5b:
866 case 0x5c:
867 return global_ss_func->setup(global_ss_func, ctrl);
868 default:
869 return -EOPNOTSUPP;
870 }
871}
872
David Brownella400cad2008-06-19 17:55:23 -0700873static struct usb_configuration sourcesink_driver = {
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700874 .label = "source/sink",
875 .strings = sourcesink_strings,
Sebastian Andrzej Siewior544aca32012-12-23 21:09:57 +0100876 .setup = ss_config_setup,
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700877 .bConfigurationValue = 3,
878 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
879 /* .iConfiguration = DYNAMIC */
David Brownella400cad2008-06-19 17:55:23 -0700880};
881
882/**
883 * sourcesink_add - add a source/sink testing configuration to a device
884 * @cdev: the device to support the configuration
885 */
David Brownellab943a22009-03-19 14:16:09 -0700886int __init sourcesink_add(struct usb_composite_dev *cdev, bool autoresume)
David Brownella400cad2008-06-19 17:55:23 -0700887{
888 int id;
889
890 /* allocate string ID(s) */
891 id = usb_string_id(cdev);
892 if (id < 0)
893 return id;
894 strings_sourcesink[0].id = id;
895
Paul Zimmermanb4036cc2012-04-16 14:19:06 -0700896 source_sink_intf_alt0.iInterface = id;
897 source_sink_intf_alt1.iInterface = id;
David Brownella400cad2008-06-19 17:55:23 -0700898 sourcesink_driver.iConfiguration = id;
899
900 /* support autoresume for remote wakeup testing */
901 if (autoresume)
902 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
903
904 /* support OTG systems */
905 if (gadget_is_otg(cdev->gadget)) {
906 sourcesink_driver.descriptors = otg_desc;
907 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
908 }
909
Uwe Kleine-Königc9bfff92010-08-12 17:43:55 +0200910 return usb_add_config(cdev, &sourcesink_driver, sourcesink_bind_config);
David Brownella400cad2008-06-19 17:55:23 -0700911}