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. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
22 | /* #define VERBOSE_DEBUG */ | |
23 | ||
24 | #include <linux/kernel.h> | |
a400cadc DB |
25 | #include <linux/device.h> |
26 | ||
27 | #include "g_zero.h" | |
28 | #include "gadget_chips.h" | |
29 | ||
30 | ||
31 | /* | |
32 | * SOURCE/SINK FUNCTION ... a primary testing vehicle for USB peripheral | |
33 | * controller drivers. | |
34 | * | |
35 | * This just sinks bulk packets OUT to the peripheral and sources them IN | |
36 | * to the host, optionally with specific data patterns for integrity tests. | |
37 | * As such it supports basic functionality and load tests. | |
38 | * | |
39 | * In terms of control messaging, this supports all the standard requests | |
40 | * plus two that support control-OUT tests. If the optional "autoresume" | |
41 | * mode is enabled, it provides good functional coverage for the "USBCV" | |
42 | * test harness from USB-IF. | |
43 | * | |
44 | * Note that because this doesn't queue more than one request at a time, | |
45 | * some other function must be used to test queueing logic. The network | |
46 | * link (g_ether) is the best overall option for that, since its TX and RX | |
47 | * queues are relatively independent, will receive a range of packet sizes, | |
48 | * and can often be made to run out completely. Those issues are important | |
49 | * when stress testing peripheral controller drivers. | |
50 | * | |
51 | * | |
52 | * This is currently packaged as a configuration driver, which can't be | |
53 | * combined with other functions to make composite devices. However, it | |
54 | * can be combined with other independent configurations. | |
55 | */ | |
56 | struct f_sourcesink { | |
57 | struct usb_function function; | |
58 | ||
59 | struct usb_ep *in_ep; | |
60 | struct usb_ep *out_ep; | |
a400cadc DB |
61 | }; |
62 | ||
63 | static inline struct f_sourcesink *func_to_ss(struct usb_function *f) | |
64 | { | |
65 | return container_of(f, struct f_sourcesink, function); | |
66 | } | |
67 | ||
a400cadc DB |
68 | static unsigned pattern; |
69 | module_param(pattern, uint, 0); | |
70 | MODULE_PARM_DESC(pattern, "0 = all zeroes, 1 = mod63 "); | |
71 | ||
72 | /*-------------------------------------------------------------------------*/ | |
73 | ||
74 | static struct usb_interface_descriptor source_sink_intf = { | |
75 | .bLength = sizeof source_sink_intf, | |
76 | .bDescriptorType = USB_DT_INTERFACE, | |
77 | ||
78 | .bNumEndpoints = 2, | |
79 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC, | |
80 | /* .iInterface = DYNAMIC */ | |
81 | }; | |
82 | ||
83 | /* full speed support: */ | |
84 | ||
85 | static struct usb_endpoint_descriptor fs_source_desc = { | |
86 | .bLength = USB_DT_ENDPOINT_SIZE, | |
87 | .bDescriptorType = USB_DT_ENDPOINT, | |
88 | ||
89 | .bEndpointAddress = USB_DIR_IN, | |
90 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
91 | }; | |
92 | ||
93 | static struct usb_endpoint_descriptor fs_sink_desc = { | |
94 | .bLength = USB_DT_ENDPOINT_SIZE, | |
95 | .bDescriptorType = USB_DT_ENDPOINT, | |
96 | ||
97 | .bEndpointAddress = USB_DIR_OUT, | |
98 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
99 | }; | |
100 | ||
101 | static struct usb_descriptor_header *fs_source_sink_descs[] = { | |
102 | (struct usb_descriptor_header *) &source_sink_intf, | |
103 | (struct usb_descriptor_header *) &fs_sink_desc, | |
104 | (struct usb_descriptor_header *) &fs_source_desc, | |
105 | NULL, | |
106 | }; | |
107 | ||
108 | /* high speed support: */ | |
109 | ||
110 | static struct usb_endpoint_descriptor hs_source_desc = { | |
111 | .bLength = USB_DT_ENDPOINT_SIZE, | |
112 | .bDescriptorType = USB_DT_ENDPOINT, | |
113 | ||
114 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
551509d2 | 115 | .wMaxPacketSize = cpu_to_le16(512), |
a400cadc DB |
116 | }; |
117 | ||
118 | static struct usb_endpoint_descriptor hs_sink_desc = { | |
119 | .bLength = USB_DT_ENDPOINT_SIZE, | |
120 | .bDescriptorType = USB_DT_ENDPOINT, | |
121 | ||
122 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
551509d2 | 123 | .wMaxPacketSize = cpu_to_le16(512), |
a400cadc DB |
124 | }; |
125 | ||
126 | static struct usb_descriptor_header *hs_source_sink_descs[] = { | |
127 | (struct usb_descriptor_header *) &source_sink_intf, | |
128 | (struct usb_descriptor_header *) &hs_source_desc, | |
129 | (struct usb_descriptor_header *) &hs_sink_desc, | |
130 | NULL, | |
131 | }; | |
132 | ||
133 | /* function-specific strings: */ | |
134 | ||
135 | static struct usb_string strings_sourcesink[] = { | |
136 | [0].s = "source and sink data", | |
137 | { } /* end of list */ | |
138 | }; | |
139 | ||
140 | static struct usb_gadget_strings stringtab_sourcesink = { | |
141 | .language = 0x0409, /* en-us */ | |
142 | .strings = strings_sourcesink, | |
143 | }; | |
144 | ||
145 | static struct usb_gadget_strings *sourcesink_strings[] = { | |
146 | &stringtab_sourcesink, | |
147 | NULL, | |
148 | }; | |
149 | ||
150 | /*-------------------------------------------------------------------------*/ | |
151 | ||
a400cadc DB |
152 | static int __init |
153 | sourcesink_bind(struct usb_configuration *c, struct usb_function *f) | |
154 | { | |
155 | struct usb_composite_dev *cdev = c->cdev; | |
156 | struct f_sourcesink *ss = func_to_ss(f); | |
157 | int id; | |
158 | ||
159 | /* allocate interface ID(s) */ | |
160 | id = usb_interface_id(c, f); | |
161 | if (id < 0) | |
162 | return id; | |
163 | source_sink_intf.bInterfaceNumber = id; | |
164 | ||
165 | /* allocate endpoints */ | |
166 | ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc); | |
167 | if (!ss->in_ep) { | |
168 | autoconf_fail: | |
169 | ERROR(cdev, "%s: can't autoconfigure on %s\n", | |
170 | f->name, cdev->gadget->name); | |
171 | return -ENODEV; | |
172 | } | |
173 | ss->in_ep->driver_data = cdev; /* claim */ | |
174 | ||
175 | ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc); | |
176 | if (!ss->out_ep) | |
177 | goto autoconf_fail; | |
178 | ss->out_ep->driver_data = cdev; /* claim */ | |
179 | ||
a400cadc DB |
180 | /* support high speed hardware */ |
181 | if (gadget_is_dualspeed(c->cdev->gadget)) { | |
182 | hs_source_desc.bEndpointAddress = | |
183 | fs_source_desc.bEndpointAddress; | |
184 | hs_sink_desc.bEndpointAddress = | |
185 | fs_sink_desc.bEndpointAddress; | |
186 | f->hs_descriptors = hs_source_sink_descs; | |
187 | } | |
188 | ||
189 | DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n", | |
190 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", | |
191 | f->name, ss->in_ep->name, ss->out_ep->name); | |
192 | return 0; | |
193 | } | |
194 | ||
195 | static void | |
196 | sourcesink_unbind(struct usb_configuration *c, struct usb_function *f) | |
197 | { | |
198 | kfree(func_to_ss(f)); | |
199 | } | |
200 | ||
201 | /* optionally require specific source/sink data patterns */ | |
202 | static int check_read_data(struct f_sourcesink *ss, struct usb_request *req) | |
203 | { | |
204 | unsigned i; | |
205 | u8 *buf = req->buf; | |
206 | struct usb_composite_dev *cdev = ss->function.config->cdev; | |
207 | ||
208 | for (i = 0; i < req->actual; i++, buf++) { | |
209 | switch (pattern) { | |
210 | ||
211 | /* all-zeroes has no synchronization issues */ | |
212 | case 0: | |
213 | if (*buf == 0) | |
214 | continue; | |
215 | break; | |
216 | ||
217 | /* "mod63" stays in sync with short-terminated transfers, | |
218 | * OR otherwise when host and gadget agree on how large | |
219 | * each usb transfer request should be. Resync is done | |
220 | * with set_interface or set_config. (We *WANT* it to | |
221 | * get quickly out of sync if controllers or their drivers | |
222 | * stutter for any reason, including buffer duplcation...) | |
223 | */ | |
224 | case 1: | |
225 | if (*buf == (u8)(i % 63)) | |
226 | continue; | |
227 | break; | |
228 | } | |
229 | ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf); | |
230 | usb_ep_set_halt(ss->out_ep); | |
231 | return -EINVAL; | |
232 | } | |
233 | return 0; | |
234 | } | |
235 | ||
236 | static void reinit_write_data(struct usb_ep *ep, struct usb_request *req) | |
237 | { | |
238 | unsigned i; | |
239 | u8 *buf = req->buf; | |
240 | ||
241 | switch (pattern) { | |
242 | case 0: | |
243 | memset(req->buf, 0, req->length); | |
244 | break; | |
245 | case 1: | |
246 | for (i = 0; i < req->length; i++) | |
247 | *buf++ = (u8) (i % 63); | |
248 | break; | |
249 | } | |
250 | } | |
251 | ||
252 | static void source_sink_complete(struct usb_ep *ep, struct usb_request *req) | |
253 | { | |
254 | struct f_sourcesink *ss = ep->driver_data; | |
255 | struct usb_composite_dev *cdev = ss->function.config->cdev; | |
256 | int status = req->status; | |
257 | ||
258 | switch (status) { | |
259 | ||
260 | case 0: /* normal completion? */ | |
261 | if (ep == ss->out_ep) { | |
262 | check_read_data(ss, req); | |
263 | memset(req->buf, 0x55, req->length); | |
264 | } else | |
265 | reinit_write_data(ep, req); | |
266 | break; | |
267 | ||
268 | /* this endpoint is normally active while we're configured */ | |
269 | case -ECONNABORTED: /* hardware forced ep reset */ | |
270 | case -ECONNRESET: /* request dequeued */ | |
271 | case -ESHUTDOWN: /* disconnect from host */ | |
272 | VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status, | |
273 | req->actual, req->length); | |
274 | if (ep == ss->out_ep) | |
275 | check_read_data(ss, req); | |
276 | free_ep_req(ep, req); | |
277 | return; | |
278 | ||
279 | case -EOVERFLOW: /* buffer overrun on read means that | |
280 | * we didn't provide a big enough | |
281 | * buffer. | |
282 | */ | |
283 | default: | |
284 | #if 1 | |
285 | DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name, | |
286 | status, req->actual, req->length); | |
287 | #endif | |
288 | case -EREMOTEIO: /* short read */ | |
289 | break; | |
290 | } | |
291 | ||
292 | status = usb_ep_queue(ep, req, GFP_ATOMIC); | |
293 | if (status) { | |
294 | ERROR(cdev, "kill %s: resubmit %d bytes --> %d\n", | |
295 | ep->name, req->length, status); | |
296 | usb_ep_set_halt(ep); | |
297 | /* FIXME recover later ... somehow */ | |
298 | } | |
299 | } | |
300 | ||
301 | static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in) | |
302 | { | |
303 | struct usb_ep *ep; | |
304 | struct usb_request *req; | |
305 | int status; | |
306 | ||
307 | ep = is_in ? ss->in_ep : ss->out_ep; | |
308 | req = alloc_ep_req(ep); | |
309 | if (!req) | |
310 | return -ENOMEM; | |
311 | ||
312 | req->complete = source_sink_complete; | |
313 | if (is_in) | |
314 | reinit_write_data(ep, req); | |
315 | else | |
316 | memset(req->buf, 0x55, req->length); | |
317 | ||
318 | status = usb_ep_queue(ep, req, GFP_ATOMIC); | |
319 | if (status) { | |
320 | struct usb_composite_dev *cdev; | |
321 | ||
322 | cdev = ss->function.config->cdev; | |
323 | ERROR(cdev, "start %s %s --> %d\n", | |
324 | is_in ? "IN" : "OUT", | |
325 | ep->name, status); | |
326 | free_ep_req(ep, req); | |
327 | } | |
328 | ||
329 | return status; | |
330 | } | |
331 | ||
332 | static void disable_source_sink(struct f_sourcesink *ss) | |
333 | { | |
334 | struct usb_composite_dev *cdev; | |
335 | ||
336 | cdev = ss->function.config->cdev; | |
337 | disable_endpoints(cdev, ss->in_ep, ss->out_ep); | |
a400cadc DB |
338 | VDBG(cdev, "%s disabled\n", ss->function.name); |
339 | } | |
340 | ||
341 | static int | |
342 | enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss) | |
343 | { | |
344 | int result = 0; | |
345 | const struct usb_endpoint_descriptor *src, *sink; | |
346 | struct usb_ep *ep; | |
347 | ||
348 | src = ep_choose(cdev->gadget, &hs_source_desc, &fs_source_desc); | |
349 | sink = ep_choose(cdev->gadget, &hs_sink_desc, &fs_sink_desc); | |
350 | ||
351 | /* one endpoint writes (sources) zeroes IN (to the host) */ | |
352 | ep = ss->in_ep; | |
353 | result = usb_ep_enable(ep, src); | |
354 | if (result < 0) | |
355 | return result; | |
356 | ep->driver_data = ss; | |
357 | ||
358 | result = source_sink_start_ep(ss, true); | |
359 | if (result < 0) { | |
360 | fail: | |
361 | ep = ss->in_ep; | |
362 | usb_ep_disable(ep); | |
363 | ep->driver_data = NULL; | |
364 | return result; | |
365 | } | |
366 | ||
367 | /* one endpoint reads (sinks) anything OUT (from the host) */ | |
368 | ep = ss->out_ep; | |
369 | result = usb_ep_enable(ep, sink); | |
370 | if (result < 0) | |
371 | goto fail; | |
372 | ep->driver_data = ss; | |
373 | ||
374 | result = source_sink_start_ep(ss, false); | |
375 | if (result < 0) { | |
376 | usb_ep_disable(ep); | |
377 | ep->driver_data = NULL; | |
378 | goto fail; | |
379 | } | |
380 | ||
381 | DBG(cdev, "%s enabled\n", ss->function.name); | |
382 | return result; | |
383 | } | |
384 | ||
385 | static int sourcesink_set_alt(struct usb_function *f, | |
386 | unsigned intf, unsigned alt) | |
387 | { | |
388 | struct f_sourcesink *ss = func_to_ss(f); | |
389 | struct usb_composite_dev *cdev = f->config->cdev; | |
390 | ||
391 | /* we know alt is zero */ | |
392 | if (ss->in_ep->driver_data) | |
393 | disable_source_sink(ss); | |
394 | return enable_source_sink(cdev, ss); | |
395 | } | |
396 | ||
397 | static void sourcesink_disable(struct usb_function *f) | |
398 | { | |
399 | struct f_sourcesink *ss = func_to_ss(f); | |
400 | ||
401 | disable_source_sink(ss); | |
402 | } | |
403 | ||
a400cadc DB |
404 | /*-------------------------------------------------------------------------*/ |
405 | ||
406 | static int __init sourcesink_bind_config(struct usb_configuration *c) | |
407 | { | |
408 | struct f_sourcesink *ss; | |
409 | int status; | |
410 | ||
411 | ss = kzalloc(sizeof *ss, GFP_KERNEL); | |
412 | if (!ss) | |
413 | return -ENOMEM; | |
414 | ||
415 | ss->function.name = "source/sink"; | |
416 | ss->function.descriptors = fs_source_sink_descs; | |
417 | ss->function.bind = sourcesink_bind; | |
418 | ss->function.unbind = sourcesink_unbind; | |
419 | ss->function.set_alt = sourcesink_set_alt; | |
420 | ss->function.disable = sourcesink_disable; | |
a400cadc DB |
421 | |
422 | status = usb_add_function(c, &ss->function); | |
423 | if (status) | |
424 | kfree(ss); | |
425 | return status; | |
426 | } | |
427 | ||
428 | static int sourcesink_setup(struct usb_configuration *c, | |
429 | const struct usb_ctrlrequest *ctrl) | |
430 | { | |
431 | struct usb_request *req = c->cdev->req; | |
432 | int value = -EOPNOTSUPP; | |
433 | u16 w_index = le16_to_cpu(ctrl->wIndex); | |
434 | u16 w_value = le16_to_cpu(ctrl->wValue); | |
435 | u16 w_length = le16_to_cpu(ctrl->wLength); | |
436 | ||
437 | /* composite driver infrastructure handles everything except | |
438 | * the two control test requests. | |
439 | */ | |
440 | switch (ctrl->bRequest) { | |
441 | ||
442 | /* | |
443 | * These are the same vendor-specific requests supported by | |
444 | * Intel's USB 2.0 compliance test devices. We exceed that | |
445 | * device spec by allowing multiple-packet requests. | |
446 | * | |
447 | * NOTE: the Control-OUT data stays in req->buf ... better | |
448 | * would be copying it into a scratch buffer, so that other | |
449 | * requests may safely intervene. | |
450 | */ | |
451 | case 0x5b: /* control WRITE test -- fill the buffer */ | |
452 | if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR)) | |
453 | goto unknown; | |
454 | if (w_value || w_index) | |
455 | break; | |
456 | /* just read that many bytes into the buffer */ | |
457 | if (w_length > req->length) | |
458 | break; | |
459 | value = w_length; | |
460 | break; | |
461 | case 0x5c: /* control READ test -- return the buffer */ | |
462 | if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR)) | |
463 | goto unknown; | |
464 | if (w_value || w_index) | |
465 | break; | |
466 | /* expect those bytes are still in the buffer; send back */ | |
467 | if (w_length > req->length) | |
468 | break; | |
469 | value = w_length; | |
470 | break; | |
471 | ||
472 | default: | |
473 | unknown: | |
474 | VDBG(c->cdev, | |
475 | "unknown control req%02x.%02x v%04x i%04x l%d\n", | |
476 | ctrl->bRequestType, ctrl->bRequest, | |
477 | w_value, w_index, w_length); | |
478 | } | |
479 | ||
480 | /* respond with data transfer or status phase? */ | |
481 | if (value >= 0) { | |
482 | VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n", | |
483 | ctrl->bRequestType, ctrl->bRequest, | |
484 | w_value, w_index, w_length); | |
485 | req->zero = 0; | |
486 | req->length = value; | |
487 | value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC); | |
488 | if (value < 0) | |
489 | ERROR(c->cdev, "source/sinkc response, err %d\n", | |
490 | value); | |
491 | } | |
492 | ||
493 | /* device either stalls (value < 0) or reports success */ | |
494 | return value; | |
495 | } | |
496 | ||
497 | static struct usb_configuration sourcesink_driver = { | |
498 | .label = "source/sink", | |
499 | .strings = sourcesink_strings, | |
500 | .bind = sourcesink_bind_config, | |
501 | .setup = sourcesink_setup, | |
502 | .bConfigurationValue = 3, | |
503 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | |
a400cadc DB |
504 | /* .iConfiguration = DYNAMIC */ |
505 | }; | |
506 | ||
507 | /** | |
508 | * sourcesink_add - add a source/sink testing configuration to a device | |
509 | * @cdev: the device to support the configuration | |
510 | */ | |
ab943a2e | 511 | int __init sourcesink_add(struct usb_composite_dev *cdev, bool autoresume) |
a400cadc DB |
512 | { |
513 | int id; | |
514 | ||
515 | /* allocate string ID(s) */ | |
516 | id = usb_string_id(cdev); | |
517 | if (id < 0) | |
518 | return id; | |
519 | strings_sourcesink[0].id = id; | |
520 | ||
521 | source_sink_intf.iInterface = id; | |
522 | sourcesink_driver.iConfiguration = id; | |
523 | ||
524 | /* support autoresume for remote wakeup testing */ | |
525 | if (autoresume) | |
526 | sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
527 | ||
528 | /* support OTG systems */ | |
529 | if (gadget_is_otg(cdev->gadget)) { | |
530 | sourcesink_driver.descriptors = otg_desc; | |
531 | sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP; | |
532 | } | |
533 | ||
534 | return usb_add_config(cdev, &sourcesink_driver); | |
535 | } |