]>
Commit | Line | Data |
---|---|---|
a400cadc DB |
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. | |
a400cadc DB |
11 | */ |
12 | ||
13 | /* #define VERBOSE_DEBUG */ | |
14 | ||
5a0e3ad6 | 15 | #include <linux/slab.h> |
a400cadc | 16 | #include <linux/kernel.h> |
a400cadc | 17 | #include <linux/device.h> |
6eb0de82 | 18 | #include <linux/module.h> |
a400cadc DB |
19 | |
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 | */ | |
49 | struct f_sourcesink { | |
50 | struct usb_function function; | |
51 | ||
52 | struct usb_ep *in_ep; | |
53 | struct usb_ep *out_ep; | |
b4036ccd PZ |
54 | struct usb_ep *iso_in_ep; |
55 | struct usb_ep *iso_out_ep; | |
56 | int cur_alt; | |
a400cadc DB |
57 | }; |
58 | ||
59 | static inline struct f_sourcesink *func_to_ss(struct usb_function *f) | |
60 | { | |
61 | return container_of(f, struct f_sourcesink, function); | |
62 | } | |
63 | ||
a400cadc | 64 | static unsigned pattern; |
b4036ccd PZ |
65 | module_param(pattern, uint, S_IRUGO|S_IWUSR); |
66 | MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63, 2 = none"); | |
67 | ||
68 | static unsigned isoc_interval = 4; | |
69 | module_param(isoc_interval, uint, S_IRUGO|S_IWUSR); | |
70 | MODULE_PARM_DESC(isoc_interval, "1 - 16"); | |
71 | ||
72 | static unsigned isoc_maxpacket = 1024; | |
73 | module_param(isoc_maxpacket, uint, S_IRUGO|S_IWUSR); | |
74 | MODULE_PARM_DESC(isoc_maxpacket, "0 - 1023 (fs), 0 - 1024 (hs/ss)"); | |
75 | ||
76 | static unsigned isoc_mult; | |
77 | module_param(isoc_mult, uint, S_IRUGO|S_IWUSR); | |
78 | MODULE_PARM_DESC(isoc_mult, "0 - 2 (hs/ss only)"); | |
79 | ||
80 | static unsigned isoc_maxburst; | |
81 | module_param(isoc_maxburst, uint, S_IRUGO|S_IWUSR); | |
82 | MODULE_PARM_DESC(isoc_maxburst, "0 - 15 (ss only)"); | |
a400cadc DB |
83 | |
84 | /*-------------------------------------------------------------------------*/ | |
85 | ||
b4036ccd PZ |
86 | static struct usb_interface_descriptor source_sink_intf_alt0 = { |
87 | .bLength = USB_DT_INTERFACE_SIZE, | |
a400cadc DB |
88 | .bDescriptorType = USB_DT_INTERFACE, |
89 | ||
b4036ccd | 90 | .bAlternateSetting = 0, |
a400cadc DB |
91 | .bNumEndpoints = 2, |
92 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
b4036ccd PZ |
93 | /* .iInterface = DYNAMIC */ |
94 | }; | |
95 | ||
96 | static 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 */ | |
a400cadc DB |
104 | }; |
105 | ||
106 | /* full speed support: */ | |
107 | ||
108 | static 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 | ||
116 | static 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 | ||
b4036ccd PZ |
124 | static 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 | ||
134 | static 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 | ||
a400cadc | 144 | static struct usb_descriptor_header *fs_source_sink_descs[] = { |
b4036ccd | 145 | (struct usb_descriptor_header *) &source_sink_intf_alt0, |
a400cadc DB |
146 | (struct usb_descriptor_header *) &fs_sink_desc, |
147 | (struct usb_descriptor_header *) &fs_source_desc, | |
b4036ccd PZ |
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, | |
a400cadc DB |
154 | NULL, |
155 | }; | |
156 | ||
157 | /* high speed support: */ | |
158 | ||
159 | static 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, | |
551509d2 | 164 | .wMaxPacketSize = cpu_to_le16(512), |
a400cadc DB |
165 | }; |
166 | ||
167 | static 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, | |
551509d2 | 172 | .wMaxPacketSize = cpu_to_le16(512), |
a400cadc DB |
173 | }; |
174 | ||
b4036ccd PZ |
175 | static 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 | ||
184 | static 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 | ||
a400cadc | 193 | static struct usb_descriptor_header *hs_source_sink_descs[] = { |
b4036ccd | 194 | (struct usb_descriptor_header *) &source_sink_intf_alt0, |
a400cadc DB |
195 | (struct usb_descriptor_header *) &hs_source_desc, |
196 | (struct usb_descriptor_header *) &hs_sink_desc, | |
b4036ccd PZ |
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, | |
a400cadc DB |
203 | NULL, |
204 | }; | |
205 | ||
57c97c02 AB |
206 | /* super speed support: */ |
207 | ||
208 | static 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 | ||
216 | struct usb_ss_ep_comp_descriptor ss_source_comp_desc = { | |
217 | .bLength = USB_DT_SS_EP_COMP_SIZE, | |
218 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
b4036ccd | 219 | |
57c97c02 AB |
220 | .bMaxBurst = 0, |
221 | .bmAttributes = 0, | |
222 | .wBytesPerInterval = 0, | |
223 | }; | |
224 | ||
225 | static 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 | ||
233 | struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = { | |
234 | .bLength = USB_DT_SS_EP_COMP_SIZE, | |
235 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
b4036ccd | 236 | |
57c97c02 AB |
237 | .bMaxBurst = 0, |
238 | .bmAttributes = 0, | |
239 | .wBytesPerInterval = 0, | |
240 | }; | |
241 | ||
b4036ccd PZ |
242 | static 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 | ||
251 | struct 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 | ||
260 | static 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 | ||
269 | struct 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 | ||
57c97c02 | 278 | static struct usb_descriptor_header *ss_source_sink_descs[] = { |
b4036ccd | 279 | (struct usb_descriptor_header *) &source_sink_intf_alt0, |
57c97c02 AB |
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, | |
b4036ccd PZ |
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, | |
57c97c02 AB |
294 | NULL, |
295 | }; | |
296 | ||
a400cadc DB |
297 | /* function-specific strings: */ |
298 | ||
299 | static struct usb_string strings_sourcesink[] = { | |
300 | [0].s = "source and sink data", | |
301 | { } /* end of list */ | |
302 | }; | |
303 | ||
304 | static struct usb_gadget_strings stringtab_sourcesink = { | |
305 | .language = 0x0409, /* en-us */ | |
306 | .strings = strings_sourcesink, | |
307 | }; | |
308 | ||
309 | static struct usb_gadget_strings *sourcesink_strings[] = { | |
310 | &stringtab_sourcesink, | |
311 | NULL, | |
312 | }; | |
313 | ||
314 | /*-------------------------------------------------------------------------*/ | |
315 | ||
a400cadc DB |
316 | static int __init |
317 | sourcesink_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; | |
10287bae | 322 | int ret; |
a400cadc DB |
323 | |
324 | /* allocate interface ID(s) */ | |
325 | id = usb_interface_id(c, f); | |
326 | if (id < 0) | |
327 | return id; | |
b4036ccd PZ |
328 | source_sink_intf_alt0.bInterfaceNumber = id; |
329 | source_sink_intf_alt1.bInterfaceNumber = id; | |
a400cadc | 330 | |
eeae5407 SAS |
331 | /* allocate string ID(s) */ |
332 | id = usb_string_id(cdev); | |
333 | if (id < 0) | |
334 | return id; | |
335 | strings_sourcesink[0].id = id; | |
336 | source_sink_intf_alt0.iInterface = id; | |
337 | source_sink_intf_alt1.iInterface = id; | |
338 | ||
b4036ccd | 339 | /* allocate bulk endpoints */ |
a400cadc DB |
340 | ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc); |
341 | if (!ss->in_ep) { | |
342 | autoconf_fail: | |
343 | ERROR(cdev, "%s: can't autoconfigure on %s\n", | |
344 | f->name, cdev->gadget->name); | |
345 | return -ENODEV; | |
346 | } | |
347 | ss->in_ep->driver_data = cdev; /* claim */ | |
348 | ||
349 | ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc); | |
350 | if (!ss->out_ep) | |
351 | goto autoconf_fail; | |
352 | ss->out_ep->driver_data = cdev; /* claim */ | |
353 | ||
b4036ccd PZ |
354 | /* sanity check the isoc module parameters */ |
355 | if (isoc_interval < 1) | |
356 | isoc_interval = 1; | |
357 | if (isoc_interval > 16) | |
358 | isoc_interval = 16; | |
359 | if (isoc_mult > 2) | |
360 | isoc_mult = 2; | |
361 | if (isoc_maxburst > 15) | |
362 | isoc_maxburst = 15; | |
363 | ||
364 | /* fill in the FS isoc descriptors from the module parameters */ | |
365 | fs_iso_source_desc.wMaxPacketSize = isoc_maxpacket > 1023 ? | |
366 | 1023 : isoc_maxpacket; | |
367 | fs_iso_source_desc.bInterval = isoc_interval; | |
368 | fs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket > 1023 ? | |
369 | 1023 : isoc_maxpacket; | |
370 | fs_iso_sink_desc.bInterval = isoc_interval; | |
371 | ||
372 | /* allocate iso endpoints */ | |
373 | ss->iso_in_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_source_desc); | |
374 | if (!ss->iso_in_ep) | |
375 | goto no_iso; | |
376 | ss->iso_in_ep->driver_data = cdev; /* claim */ | |
377 | ||
378 | ss->iso_out_ep = usb_ep_autoconfig(cdev->gadget, &fs_iso_sink_desc); | |
379 | if (ss->iso_out_ep) { | |
380 | ss->iso_out_ep->driver_data = cdev; /* claim */ | |
381 | } else { | |
382 | ss->iso_in_ep->driver_data = NULL; | |
383 | ss->iso_in_ep = NULL; | |
384 | no_iso: | |
385 | /* | |
386 | * We still want to work even if the UDC doesn't have isoc | |
387 | * endpoints, so null out the alt interface that contains | |
388 | * them and continue. | |
389 | */ | |
390 | fs_source_sink_descs[FS_ALT_IFC_1_OFFSET] = NULL; | |
391 | hs_source_sink_descs[HS_ALT_IFC_1_OFFSET] = NULL; | |
392 | ss_source_sink_descs[SS_ALT_IFC_1_OFFSET] = NULL; | |
393 | } | |
394 | ||
395 | if (isoc_maxpacket > 1024) | |
396 | isoc_maxpacket = 1024; | |
397 | ||
a400cadc | 398 | /* support high speed hardware */ |
10287bae SAS |
399 | hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress; |
400 | hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress; | |
401 | ||
402 | /* | |
403 | * Fill in the HS isoc descriptors from the module parameters. | |
404 | * We assume that the user knows what they are doing and won't | |
405 | * give parameters that their UDC doesn't support. | |
406 | */ | |
407 | hs_iso_source_desc.wMaxPacketSize = isoc_maxpacket; | |
408 | hs_iso_source_desc.wMaxPacketSize |= isoc_mult << 11; | |
409 | hs_iso_source_desc.bInterval = isoc_interval; | |
410 | hs_iso_source_desc.bEndpointAddress = | |
411 | fs_iso_source_desc.bEndpointAddress; | |
412 | ||
413 | hs_iso_sink_desc.wMaxPacketSize = isoc_maxpacket; | |
414 | hs_iso_sink_desc.wMaxPacketSize |= isoc_mult << 11; | |
415 | hs_iso_sink_desc.bInterval = isoc_interval; | |
416 | hs_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress; | |
a400cadc | 417 | |
57c97c02 | 418 | /* support super speed hardware */ |
10287bae SAS |
419 | ss_source_desc.bEndpointAddress = |
420 | fs_source_desc.bEndpointAddress; | |
421 | ss_sink_desc.bEndpointAddress = | |
422 | fs_sink_desc.bEndpointAddress; | |
423 | ||
424 | /* | |
425 | * Fill in the SS isoc descriptors from the module parameters. | |
426 | * We assume that the user knows what they are doing and won't | |
427 | * give parameters that their UDC doesn't support. | |
428 | */ | |
429 | ss_iso_source_desc.wMaxPacketSize = isoc_maxpacket; | |
430 | ss_iso_source_desc.bInterval = isoc_interval; | |
431 | ss_iso_source_comp_desc.bmAttributes = isoc_mult; | |
432 | ss_iso_source_comp_desc.bMaxBurst = isoc_maxburst; | |
433 | ss_iso_source_comp_desc.wBytesPerInterval = | |
434 | isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1); | |
435 | ss_iso_source_desc.bEndpointAddress = | |
436 | fs_iso_source_desc.bEndpointAddress; | |
437 | ||
438 | ss_iso_sink_desc.wMaxPacketSize = isoc_maxpacket; | |
439 | ss_iso_sink_desc.bInterval = isoc_interval; | |
440 | ss_iso_sink_comp_desc.bmAttributes = isoc_mult; | |
441 | ss_iso_sink_comp_desc.bMaxBurst = isoc_maxburst; | |
442 | ss_iso_sink_comp_desc.wBytesPerInterval = | |
443 | isoc_maxpacket * (isoc_mult + 1) * (isoc_maxburst + 1); | |
444 | ss_iso_sink_desc.bEndpointAddress = fs_iso_sink_desc.bEndpointAddress; | |
445 | ||
446 | ret = usb_assign_descriptors(f, fs_source_sink_descs, | |
447 | hs_source_sink_descs, ss_source_sink_descs); | |
448 | if (ret) | |
449 | return ret; | |
57c97c02 | 450 | |
b4036ccd | 451 | DBG(cdev, "%s speed %s: IN/%s, OUT/%s, ISO-IN/%s, ISO-OUT/%s\n", |
57c97c02 AB |
452 | (gadget_is_superspeed(c->cdev->gadget) ? "super" : |
453 | (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")), | |
b4036ccd PZ |
454 | f->name, ss->in_ep->name, ss->out_ep->name, |
455 | ss->iso_in_ep ? ss->iso_in_ep->name : "<none>", | |
456 | ss->iso_out_ep ? ss->iso_out_ep->name : "<none>"); | |
a400cadc DB |
457 | return 0; |
458 | } | |
459 | ||
544aca39 SAS |
460 | static struct usb_function *global_ss_func; |
461 | ||
a400cadc DB |
462 | static void |
463 | sourcesink_unbind(struct usb_configuration *c, struct usb_function *f) | |
464 | { | |
10287bae | 465 | usb_free_all_descriptors(f); |
a400cadc | 466 | kfree(func_to_ss(f)); |
544aca39 | 467 | global_ss_func = NULL; |
a400cadc DB |
468 | } |
469 | ||
470 | /* optionally require specific source/sink data patterns */ | |
471 | static int check_read_data(struct f_sourcesink *ss, struct usb_request *req) | |
472 | { | |
473 | unsigned i; | |
474 | u8 *buf = req->buf; | |
475 | struct usb_composite_dev *cdev = ss->function.config->cdev; | |
476 | ||
b4036ccd PZ |
477 | if (pattern == 2) |
478 | return 0; | |
479 | ||
a400cadc DB |
480 | for (i = 0; i < req->actual; i++, buf++) { |
481 | switch (pattern) { | |
482 | ||
483 | /* all-zeroes has no synchronization issues */ | |
484 | case 0: | |
485 | if (*buf == 0) | |
486 | continue; | |
487 | break; | |
488 | ||
489 | /* "mod63" stays in sync with short-terminated transfers, | |
490 | * OR otherwise when host and gadget agree on how large | |
491 | * each usb transfer request should be. Resync is done | |
492 | * with set_interface or set_config. (We *WANT* it to | |
493 | * get quickly out of sync if controllers or their drivers | |
b4036ccd | 494 | * stutter for any reason, including buffer duplication...) |
a400cadc DB |
495 | */ |
496 | case 1: | |
497 | if (*buf == (u8)(i % 63)) | |
498 | continue; | |
499 | break; | |
500 | } | |
501 | ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf); | |
502 | usb_ep_set_halt(ss->out_ep); | |
503 | return -EINVAL; | |
504 | } | |
505 | return 0; | |
506 | } | |
507 | ||
508 | static void reinit_write_data(struct usb_ep *ep, struct usb_request *req) | |
509 | { | |
510 | unsigned i; | |
511 | u8 *buf = req->buf; | |
512 | ||
513 | switch (pattern) { | |
514 | case 0: | |
515 | memset(req->buf, 0, req->length); | |
516 | break; | |
517 | case 1: | |
518 | for (i = 0; i < req->length; i++) | |
519 | *buf++ = (u8) (i % 63); | |
520 | break; | |
b4036ccd PZ |
521 | case 2: |
522 | break; | |
a400cadc DB |
523 | } |
524 | } | |
525 | ||
526 | static void source_sink_complete(struct usb_ep *ep, struct usb_request *req) | |
527 | { | |
b4036ccd PZ |
528 | struct usb_composite_dev *cdev; |
529 | struct f_sourcesink *ss = ep->driver_data; | |
530 | int status = req->status; | |
531 | ||
532 | /* driver_data will be null if ep has been disabled */ | |
533 | if (!ss) | |
534 | return; | |
535 | ||
536 | cdev = ss->function.config->cdev; | |
a400cadc DB |
537 | |
538 | switch (status) { | |
539 | ||
540 | case 0: /* normal completion? */ | |
541 | if (ep == ss->out_ep) { | |
542 | check_read_data(ss, req); | |
b4036ccd PZ |
543 | if (pattern != 2) |
544 | memset(req->buf, 0x55, req->length); | |
32c9cf22 | 545 | } |
a400cadc DB |
546 | break; |
547 | ||
548 | /* this endpoint is normally active while we're configured */ | |
549 | case -ECONNABORTED: /* hardware forced ep reset */ | |
550 | case -ECONNRESET: /* request dequeued */ | |
551 | case -ESHUTDOWN: /* disconnect from host */ | |
552 | VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status, | |
553 | req->actual, req->length); | |
554 | if (ep == ss->out_ep) | |
555 | check_read_data(ss, req); | |
556 | free_ep_req(ep, req); | |
557 | return; | |
558 | ||
559 | case -EOVERFLOW: /* buffer overrun on read means that | |
560 | * we didn't provide a big enough | |
561 | * buffer. | |
562 | */ | |
563 | default: | |
564 | #if 1 | |
565 | DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name, | |
566 | status, req->actual, req->length); | |
567 | #endif | |
568 | case -EREMOTEIO: /* short read */ | |
569 | break; | |
570 | } | |
571 | ||
572 | status = usb_ep_queue(ep, req, GFP_ATOMIC); | |
573 | if (status) { | |
574 | ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n", | |
575 | ep->name, req->length, status); | |
576 | usb_ep_set_halt(ep); | |
577 | /* FIXME recover later ... somehow */ | |
578 | } | |
579 | } | |
580 | ||
b4036ccd PZ |
581 | static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in, |
582 | bool is_iso, int speed) | |
a400cadc DB |
583 | { |
584 | struct usb_ep *ep; | |
585 | struct usb_request *req; | |
b4036ccd PZ |
586 | int i, size, status; |
587 | ||
588 | for (i = 0; i < 8; i++) { | |
589 | if (is_iso) { | |
590 | switch (speed) { | |
591 | case USB_SPEED_SUPER: | |
592 | size = isoc_maxpacket * (isoc_mult + 1) * | |
593 | (isoc_maxburst + 1); | |
594 | break; | |
595 | case USB_SPEED_HIGH: | |
596 | size = isoc_maxpacket * (isoc_mult + 1); | |
597 | break; | |
598 | default: | |
599 | size = isoc_maxpacket > 1023 ? | |
600 | 1023 : isoc_maxpacket; | |
601 | break; | |
602 | } | |
603 | ep = is_in ? ss->iso_in_ep : ss->iso_out_ep; | |
604 | req = alloc_ep_req(ep, size); | |
605 | } else { | |
606 | ep = is_in ? ss->in_ep : ss->out_ep; | |
607 | req = alloc_ep_req(ep, 0); | |
608 | } | |
a400cadc | 609 | |
b4036ccd PZ |
610 | if (!req) |
611 | return -ENOMEM; | |
a400cadc | 612 | |
b4036ccd PZ |
613 | req->complete = source_sink_complete; |
614 | if (is_in) | |
615 | reinit_write_data(ep, req); | |
616 | else if (pattern != 2) | |
617 | memset(req->buf, 0x55, req->length); | |
a400cadc | 618 | |
b4036ccd PZ |
619 | status = usb_ep_queue(ep, req, GFP_ATOMIC); |
620 | if (status) { | |
621 | struct usb_composite_dev *cdev; | |
a400cadc | 622 | |
b4036ccd PZ |
623 | cdev = ss->function.config->cdev; |
624 | ERROR(cdev, "start %s%s %s --> %d\n", | |
625 | is_iso ? "ISO-" : "", is_in ? "IN" : "OUT", | |
626 | ep->name, status); | |
627 | free_ep_req(ep, req); | |
628 | } | |
629 | ||
630 | if (!is_iso) | |
631 | break; | |
a400cadc DB |
632 | } |
633 | ||
634 | return status; | |
635 | } | |
636 | ||
637 | static void disable_source_sink(struct f_sourcesink *ss) | |
638 | { | |
639 | struct usb_composite_dev *cdev; | |
640 | ||
641 | cdev = ss->function.config->cdev; | |
b4036ccd PZ |
642 | disable_endpoints(cdev, ss->in_ep, ss->out_ep, ss->iso_in_ep, |
643 | ss->iso_out_ep); | |
a400cadc DB |
644 | VDBG(cdev, "%s disabled\n", ss->function.name); |
645 | } | |
646 | ||
647 | static int | |
b4036ccd PZ |
648 | enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss, |
649 | int alt) | |
a400cadc DB |
650 | { |
651 | int result = 0; | |
b4036ccd | 652 | int speed = cdev->gadget->speed; |
a400cadc DB |
653 | struct usb_ep *ep; |
654 | ||
b4036ccd | 655 | /* one bulk endpoint writes (sources) zeroes IN (to the host) */ |
a400cadc | 656 | ep = ss->in_ep; |
ea2a1df7 TB |
657 | result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); |
658 | if (result) | |
659 | return result; | |
72c973dd | 660 | result = usb_ep_enable(ep); |
a400cadc DB |
661 | if (result < 0) |
662 | return result; | |
663 | ep->driver_data = ss; | |
664 | ||
b4036ccd | 665 | result = source_sink_start_ep(ss, true, false, speed); |
a400cadc DB |
666 | if (result < 0) { |
667 | fail: | |
668 | ep = ss->in_ep; | |
669 | usb_ep_disable(ep); | |
670 | ep->driver_data = NULL; | |
671 | return result; | |
672 | } | |
673 | ||
b4036ccd | 674 | /* one bulk endpoint reads (sinks) anything OUT (from the host) */ |
a400cadc | 675 | ep = ss->out_ep; |
ea2a1df7 TB |
676 | result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); |
677 | if (result) | |
678 | goto fail; | |
72c973dd | 679 | result = usb_ep_enable(ep); |
a400cadc DB |
680 | if (result < 0) |
681 | goto fail; | |
682 | ep->driver_data = ss; | |
683 | ||
b4036ccd | 684 | result = source_sink_start_ep(ss, false, false, speed); |
a400cadc | 685 | if (result < 0) { |
b4036ccd PZ |
686 | fail2: |
687 | ep = ss->out_ep; | |
a400cadc DB |
688 | usb_ep_disable(ep); |
689 | ep->driver_data = NULL; | |
690 | goto fail; | |
691 | } | |
692 | ||
b4036ccd PZ |
693 | if (alt == 0) |
694 | goto out; | |
695 | ||
696 | /* one iso endpoint writes (sources) zeroes IN (to the host) */ | |
697 | ep = ss->iso_in_ep; | |
698 | if (ep) { | |
699 | result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); | |
700 | if (result) | |
701 | goto fail2; | |
702 | result = usb_ep_enable(ep); | |
703 | if (result < 0) | |
704 | goto fail2; | |
705 | ep->driver_data = ss; | |
706 | ||
707 | result = source_sink_start_ep(ss, true, true, speed); | |
708 | if (result < 0) { | |
709 | fail3: | |
710 | ep = ss->iso_in_ep; | |
711 | if (ep) { | |
712 | usb_ep_disable(ep); | |
713 | ep->driver_data = NULL; | |
714 | } | |
715 | goto fail2; | |
716 | } | |
717 | } | |
718 | ||
719 | /* one iso endpoint reads (sinks) anything OUT (from the host) */ | |
720 | ep = ss->iso_out_ep; | |
721 | if (ep) { | |
722 | result = config_ep_by_speed(cdev->gadget, &(ss->function), ep); | |
723 | if (result) | |
724 | goto fail3; | |
725 | result = usb_ep_enable(ep); | |
726 | if (result < 0) | |
727 | goto fail3; | |
728 | ep->driver_data = ss; | |
729 | ||
730 | result = source_sink_start_ep(ss, false, true, speed); | |
731 | if (result < 0) { | |
732 | usb_ep_disable(ep); | |
733 | ep->driver_data = NULL; | |
734 | goto fail3; | |
735 | } | |
736 | } | |
737 | out: | |
738 | ss->cur_alt = alt; | |
739 | ||
740 | DBG(cdev, "%s enabled, alt intf %d\n", ss->function.name, alt); | |
a400cadc DB |
741 | return result; |
742 | } | |
743 | ||
744 | static int sourcesink_set_alt(struct usb_function *f, | |
745 | unsigned intf, unsigned alt) | |
746 | { | |
b4036ccd PZ |
747 | struct f_sourcesink *ss = func_to_ss(f); |
748 | struct usb_composite_dev *cdev = f->config->cdev; | |
a400cadc | 749 | |
a400cadc DB |
750 | if (ss->in_ep->driver_data) |
751 | disable_source_sink(ss); | |
b4036ccd PZ |
752 | return enable_source_sink(cdev, ss, alt); |
753 | } | |
754 | ||
755 | static int sourcesink_get_alt(struct usb_function *f, unsigned intf) | |
756 | { | |
757 | struct f_sourcesink *ss = func_to_ss(f); | |
758 | ||
759 | return ss->cur_alt; | |
a400cadc DB |
760 | } |
761 | ||
762 | static void sourcesink_disable(struct usb_function *f) | |
763 | { | |
764 | struct f_sourcesink *ss = func_to_ss(f); | |
765 | ||
766 | disable_source_sink(ss); | |
767 | } | |
768 | ||
a400cadc | 769 | /*-------------------------------------------------------------------------*/ |
544aca39 | 770 | static int sourcesink_setup(struct usb_function *f, |
a400cadc DB |
771 | const struct usb_ctrlrequest *ctrl) |
772 | { | |
544aca39 | 773 | struct usb_configuration *c = f->config; |
a400cadc DB |
774 | struct usb_request *req = c->cdev->req; |
775 | int value = -EOPNOTSUPP; | |
776 | u16 w_index = le16_to_cpu(ctrl->wIndex); | |
777 | u16 w_value = le16_to_cpu(ctrl->wValue); | |
778 | u16 w_length = le16_to_cpu(ctrl->wLength); | |
779 | ||
e13f17ff | 780 | req->length = USB_COMP_EP0_BUFSIZ; |
5030ec73 | 781 | |
a400cadc DB |
782 | /* composite driver infrastructure handles everything except |
783 | * the two control test requests. | |
784 | */ | |
785 | switch (ctrl->bRequest) { | |
786 | ||
787 | /* | |
788 | * These are the same vendor-specific requests supported by | |
789 | * Intel's USB 2.0 compliance test devices. We exceed that | |
790 | * device spec by allowing multiple-packet requests. | |
791 | * | |
792 | * NOTE: the Control-OUT data stays in req->buf ... better | |
793 | * would be copying it into a scratch buffer, so that other | |
794 | * requests may safely intervene. | |
795 | */ | |
796 | case 0x5b: /* control WRITE test -- fill the buffer */ | |
797 | if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR)) | |
798 | goto unknown; | |
799 | if (w_value || w_index) | |
800 | break; | |
801 | /* just read that many bytes into the buffer */ | |
802 | if (w_length > req->length) | |
803 | break; | |
804 | value = w_length; | |
805 | break; | |
806 | case 0x5c: /* control READ test -- return the buffer */ | |
807 | if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR)) | |
808 | goto unknown; | |
809 | if (w_value || w_index) | |
810 | break; | |
811 | /* expect those bytes are still in the buffer; send back */ | |
812 | if (w_length > req->length) | |
813 | break; | |
814 | value = w_length; | |
815 | break; | |
816 | ||
817 | default: | |
818 | unknown: | |
819 | VDBG(c->cdev, | |
820 | "unknown control req%02x.%02x v%04x i%04x l%d\n", | |
821 | ctrl->bRequestType, ctrl->bRequest, | |
822 | w_value, w_index, w_length); | |
823 | } | |
824 | ||
825 | /* respond with data transfer or status phase? */ | |
826 | if (value >= 0) { | |
827 | VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n", | |
828 | ctrl->bRequestType, ctrl->bRequest, | |
829 | w_value, w_index, w_length); | |
830 | req->zero = 0; | |
831 | req->length = value; | |
832 | value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC); | |
833 | if (value < 0) | |
b4036ccd | 834 | ERROR(c->cdev, "source/sink response, err %d\n", |
a400cadc DB |
835 | value); |
836 | } | |
837 | ||
838 | /* device either stalls (value < 0) or reports success */ | |
839 | return value; | |
840 | } | |
841 | ||
544aca39 SAS |
842 | static int __init sourcesink_bind_config(struct usb_configuration *c) |
843 | { | |
844 | struct f_sourcesink *ss; | |
845 | int status; | |
846 | ||
847 | ss = kzalloc(sizeof(*ss), GFP_KERNEL); | |
848 | if (!ss) | |
849 | return -ENOMEM; | |
850 | ||
851 | global_ss_func = &ss->function; | |
852 | ||
853 | ss->function.name = "source/sink"; | |
854 | ss->function.bind = sourcesink_bind; | |
855 | ss->function.unbind = sourcesink_unbind; | |
856 | ss->function.set_alt = sourcesink_set_alt; | |
857 | ss->function.get_alt = sourcesink_get_alt; | |
858 | ss->function.disable = sourcesink_disable; | |
859 | ss->function.setup = sourcesink_setup; | |
860 | ||
861 | status = usb_add_function(c, &ss->function); | |
862 | if (status) | |
863 | kfree(ss); | |
864 | return status; | |
865 | } | |
866 | ||
867 | static int ss_config_setup(struct usb_configuration *c, | |
868 | const struct usb_ctrlrequest *ctrl) | |
869 | { | |
870 | if (!global_ss_func) | |
871 | return -EOPNOTSUPP; | |
872 | switch (ctrl->bRequest) { | |
873 | case 0x5b: | |
874 | case 0x5c: | |
875 | return global_ss_func->setup(global_ss_func, ctrl); | |
876 | default: | |
877 | return -EOPNOTSUPP; | |
878 | } | |
879 | } |