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