Merge branch 'staging-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[pandora-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/slab.h>
25 #include <linux/kernel.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 /* super speed support: */
135
136 static struct usb_endpoint_descriptor ss_source_desc = {
137         .bLength =              USB_DT_ENDPOINT_SIZE,
138         .bDescriptorType =      USB_DT_ENDPOINT,
139
140         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
141         .wMaxPacketSize =       cpu_to_le16(1024),
142 };
143
144 struct usb_ss_ep_comp_descriptor ss_source_comp_desc = {
145         .bLength =              USB_DT_SS_EP_COMP_SIZE,
146         .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,
147         .bMaxBurst =            0,
148         .bmAttributes =         0,
149         .wBytesPerInterval =    0,
150 };
151
152 static struct usb_endpoint_descriptor ss_sink_desc = {
153         .bLength =              USB_DT_ENDPOINT_SIZE,
154         .bDescriptorType =      USB_DT_ENDPOINT,
155
156         .bmAttributes =         USB_ENDPOINT_XFER_BULK,
157         .wMaxPacketSize =       cpu_to_le16(1024),
158 };
159
160 struct usb_ss_ep_comp_descriptor ss_sink_comp_desc = {
161         .bLength =              USB_DT_SS_EP_COMP_SIZE,
162         .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,
163         .bMaxBurst =            0,
164         .bmAttributes =         0,
165         .wBytesPerInterval =    0,
166 };
167
168 static struct usb_descriptor_header *ss_source_sink_descs[] = {
169         (struct usb_descriptor_header *) &source_sink_intf,
170         (struct usb_descriptor_header *) &ss_source_desc,
171         (struct usb_descriptor_header *) &ss_source_comp_desc,
172         (struct usb_descriptor_header *) &ss_sink_desc,
173         (struct usb_descriptor_header *) &ss_sink_comp_desc,
174         NULL,
175 };
176
177 /* function-specific strings: */
178
179 static struct usb_string strings_sourcesink[] = {
180         [0].s = "source and sink data",
181         {  }                    /* end of list */
182 };
183
184 static struct usb_gadget_strings stringtab_sourcesink = {
185         .language       = 0x0409,       /* en-us */
186         .strings        = strings_sourcesink,
187 };
188
189 static struct usb_gadget_strings *sourcesink_strings[] = {
190         &stringtab_sourcesink,
191         NULL,
192 };
193
194 /*-------------------------------------------------------------------------*/
195
196 static int __init
197 sourcesink_bind(struct usb_configuration *c, struct usb_function *f)
198 {
199         struct usb_composite_dev *cdev = c->cdev;
200         struct f_sourcesink     *ss = func_to_ss(f);
201         int     id;
202
203         /* allocate interface ID(s) */
204         id = usb_interface_id(c, f);
205         if (id < 0)
206                 return id;
207         source_sink_intf.bInterfaceNumber = id;
208
209         /* allocate endpoints */
210         ss->in_ep = usb_ep_autoconfig(cdev->gadget, &fs_source_desc);
211         if (!ss->in_ep) {
212 autoconf_fail:
213                 ERROR(cdev, "%s: can't autoconfigure on %s\n",
214                         f->name, cdev->gadget->name);
215                 return -ENODEV;
216         }
217         ss->in_ep->driver_data = cdev;  /* claim */
218
219         ss->out_ep = usb_ep_autoconfig(cdev->gadget, &fs_sink_desc);
220         if (!ss->out_ep)
221                 goto autoconf_fail;
222         ss->out_ep->driver_data = cdev; /* claim */
223
224         /* support high speed hardware */
225         if (gadget_is_dualspeed(c->cdev->gadget)) {
226                 hs_source_desc.bEndpointAddress =
227                                 fs_source_desc.bEndpointAddress;
228                 hs_sink_desc.bEndpointAddress =
229                                 fs_sink_desc.bEndpointAddress;
230                 f->hs_descriptors = hs_source_sink_descs;
231         }
232
233         /* support super speed hardware */
234         if (gadget_is_superspeed(c->cdev->gadget)) {
235                 ss_source_desc.bEndpointAddress =
236                                 fs_source_desc.bEndpointAddress;
237                 ss_sink_desc.bEndpointAddress =
238                                 fs_sink_desc.bEndpointAddress;
239                 f->ss_descriptors = ss_source_sink_descs;
240         }
241
242         DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
243             (gadget_is_superspeed(c->cdev->gadget) ? "super" :
244              (gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
245                         f->name, ss->in_ep->name, ss->out_ep->name);
246         return 0;
247 }
248
249 static void
250 sourcesink_unbind(struct usb_configuration *c, struct usb_function *f)
251 {
252         kfree(func_to_ss(f));
253 }
254
255 /* optionally require specific source/sink data patterns  */
256 static int check_read_data(struct f_sourcesink *ss, struct usb_request *req)
257 {
258         unsigned                i;
259         u8                      *buf = req->buf;
260         struct usb_composite_dev *cdev = ss->function.config->cdev;
261
262         for (i = 0; i < req->actual; i++, buf++) {
263                 switch (pattern) {
264
265                 /* all-zeroes has no synchronization issues */
266                 case 0:
267                         if (*buf == 0)
268                                 continue;
269                         break;
270
271                 /* "mod63" stays in sync with short-terminated transfers,
272                  * OR otherwise when host and gadget agree on how large
273                  * each usb transfer request should be.  Resync is done
274                  * with set_interface or set_config.  (We *WANT* it to
275                  * get quickly out of sync if controllers or their drivers
276                  * stutter for any reason, including buffer duplcation...)
277                  */
278                 case 1:
279                         if (*buf == (u8)(i % 63))
280                                 continue;
281                         break;
282                 }
283                 ERROR(cdev, "bad OUT byte, buf[%d] = %d\n", i, *buf);
284                 usb_ep_set_halt(ss->out_ep);
285                 return -EINVAL;
286         }
287         return 0;
288 }
289
290 static void reinit_write_data(struct usb_ep *ep, struct usb_request *req)
291 {
292         unsigned        i;
293         u8              *buf = req->buf;
294
295         switch (pattern) {
296         case 0:
297                 memset(req->buf, 0, req->length);
298                 break;
299         case 1:
300                 for  (i = 0; i < req->length; i++)
301                         *buf++ = (u8) (i % 63);
302                 break;
303         }
304 }
305
306 static void source_sink_complete(struct usb_ep *ep, struct usb_request *req)
307 {
308         struct f_sourcesink     *ss = ep->driver_data;
309         struct usb_composite_dev *cdev = ss->function.config->cdev;
310         int                     status = req->status;
311
312         switch (status) {
313
314         case 0:                         /* normal completion? */
315                 if (ep == ss->out_ep) {
316                         check_read_data(ss, req);
317                         memset(req->buf, 0x55, req->length);
318                 } else
319                         reinit_write_data(ep, req);
320                 break;
321
322         /* this endpoint is normally active while we're configured */
323         case -ECONNABORTED:             /* hardware forced ep reset */
324         case -ECONNRESET:               /* request dequeued */
325         case -ESHUTDOWN:                /* disconnect from host */
326                 VDBG(cdev, "%s gone (%d), %d/%d\n", ep->name, status,
327                                 req->actual, req->length);
328                 if (ep == ss->out_ep)
329                         check_read_data(ss, req);
330                 free_ep_req(ep, req);
331                 return;
332
333         case -EOVERFLOW:                /* buffer overrun on read means that
334                                          * we didn't provide a big enough
335                                          * buffer.
336                                          */
337         default:
338 #if 1
339                 DBG(cdev, "%s complete --> %d, %d/%d\n", ep->name,
340                                 status, req->actual, req->length);
341 #endif
342         case -EREMOTEIO:                /* short read */
343                 break;
344         }
345
346         status = usb_ep_queue(ep, req, GFP_ATOMIC);
347         if (status) {
348                 ERROR(cdev, "kill %s:  resubmit %d bytes --> %d\n",
349                                 ep->name, req->length, status);
350                 usb_ep_set_halt(ep);
351                 /* FIXME recover later ... somehow */
352         }
353 }
354
355 static int source_sink_start_ep(struct f_sourcesink *ss, bool is_in)
356 {
357         struct usb_ep           *ep;
358         struct usb_request      *req;
359         int                     status;
360
361         ep = is_in ? ss->in_ep : ss->out_ep;
362         req = alloc_ep_req(ep);
363         if (!req)
364                 return -ENOMEM;
365
366         req->complete = source_sink_complete;
367         if (is_in)
368                 reinit_write_data(ep, req);
369         else
370                 memset(req->buf, 0x55, req->length);
371
372         status = usb_ep_queue(ep, req, GFP_ATOMIC);
373         if (status) {
374                 struct usb_composite_dev        *cdev;
375
376                 cdev = ss->function.config->cdev;
377                 ERROR(cdev, "start %s %s --> %d\n",
378                                 is_in ? "IN" : "OUT",
379                                 ep->name, status);
380                 free_ep_req(ep, req);
381         }
382
383         return status;
384 }
385
386 static void disable_source_sink(struct f_sourcesink *ss)
387 {
388         struct usb_composite_dev        *cdev;
389
390         cdev = ss->function.config->cdev;
391         disable_endpoints(cdev, ss->in_ep, ss->out_ep);
392         VDBG(cdev, "%s disabled\n", ss->function.name);
393 }
394
395 static int
396 enable_source_sink(struct usb_composite_dev *cdev, struct f_sourcesink *ss)
397 {
398         int                                     result = 0;
399         struct usb_ep                           *ep;
400
401         /* one endpoint writes (sources) zeroes IN (to the host) */
402         ep = ss->in_ep;
403         result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
404         if (result)
405                 return result;
406         result = usb_ep_enable(ep);
407         if (result < 0)
408                 return result;
409         ep->driver_data = ss;
410
411         result = source_sink_start_ep(ss, true);
412         if (result < 0) {
413 fail:
414                 ep = ss->in_ep;
415                 usb_ep_disable(ep);
416                 ep->driver_data = NULL;
417                 return result;
418         }
419
420         /* one endpoint reads (sinks) anything OUT (from the host) */
421         ep = ss->out_ep;
422         result = config_ep_by_speed(cdev->gadget, &(ss->function), ep);
423         if (result)
424                 goto fail;
425         result = usb_ep_enable(ep);
426         if (result < 0)
427                 goto fail;
428         ep->driver_data = ss;
429
430         result = source_sink_start_ep(ss, false);
431         if (result < 0) {
432                 usb_ep_disable(ep);
433                 ep->driver_data = NULL;
434                 goto fail;
435         }
436
437         DBG(cdev, "%s enabled\n", ss->function.name);
438         return result;
439 }
440
441 static int sourcesink_set_alt(struct usb_function *f,
442                 unsigned intf, unsigned alt)
443 {
444         struct f_sourcesink     *ss = func_to_ss(f);
445         struct usb_composite_dev *cdev = f->config->cdev;
446
447         /* we know alt is zero */
448         if (ss->in_ep->driver_data)
449                 disable_source_sink(ss);
450         return enable_source_sink(cdev, ss);
451 }
452
453 static void sourcesink_disable(struct usb_function *f)
454 {
455         struct f_sourcesink     *ss = func_to_ss(f);
456
457         disable_source_sink(ss);
458 }
459
460 /*-------------------------------------------------------------------------*/
461
462 static int __init sourcesink_bind_config(struct usb_configuration *c)
463 {
464         struct f_sourcesink     *ss;
465         int                     status;
466
467         ss = kzalloc(sizeof *ss, GFP_KERNEL);
468         if (!ss)
469                 return -ENOMEM;
470
471         ss->function.name = "source/sink";
472         ss->function.descriptors = fs_source_sink_descs;
473         ss->function.bind = sourcesink_bind;
474         ss->function.unbind = sourcesink_unbind;
475         ss->function.set_alt = sourcesink_set_alt;
476         ss->function.disable = sourcesink_disable;
477
478         status = usb_add_function(c, &ss->function);
479         if (status)
480                 kfree(ss);
481         return status;
482 }
483
484 static int sourcesink_setup(struct usb_configuration *c,
485                 const struct usb_ctrlrequest *ctrl)
486 {
487         struct usb_request      *req = c->cdev->req;
488         int                     value = -EOPNOTSUPP;
489         u16                     w_index = le16_to_cpu(ctrl->wIndex);
490         u16                     w_value = le16_to_cpu(ctrl->wValue);
491         u16                     w_length = le16_to_cpu(ctrl->wLength);
492
493         req->length = USB_BUFSIZ;
494
495         /* composite driver infrastructure handles everything except
496          * the two control test requests.
497          */
498         switch (ctrl->bRequest) {
499
500         /*
501          * These are the same vendor-specific requests supported by
502          * Intel's USB 2.0 compliance test devices.  We exceed that
503          * device spec by allowing multiple-packet requests.
504          *
505          * NOTE:  the Control-OUT data stays in req->buf ... better
506          * would be copying it into a scratch buffer, so that other
507          * requests may safely intervene.
508          */
509         case 0x5b:      /* control WRITE test -- fill the buffer */
510                 if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
511                         goto unknown;
512                 if (w_value || w_index)
513                         break;
514                 /* just read that many bytes into the buffer */
515                 if (w_length > req->length)
516                         break;
517                 value = w_length;
518                 break;
519         case 0x5c:      /* control READ test -- return the buffer */
520                 if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
521                         goto unknown;
522                 if (w_value || w_index)
523                         break;
524                 /* expect those bytes are still in the buffer; send back */
525                 if (w_length > req->length)
526                         break;
527                 value = w_length;
528                 break;
529
530         default:
531 unknown:
532                 VDBG(c->cdev,
533                         "unknown control req%02x.%02x v%04x i%04x l%d\n",
534                         ctrl->bRequestType, ctrl->bRequest,
535                         w_value, w_index, w_length);
536         }
537
538         /* respond with data transfer or status phase? */
539         if (value >= 0) {
540                 VDBG(c->cdev, "source/sink req%02x.%02x v%04x i%04x l%d\n",
541                         ctrl->bRequestType, ctrl->bRequest,
542                         w_value, w_index, w_length);
543                 req->zero = 0;
544                 req->length = value;
545                 value = usb_ep_queue(c->cdev->gadget->ep0, req, GFP_ATOMIC);
546                 if (value < 0)
547                         ERROR(c->cdev, "source/sinkc response, err %d\n",
548                                         value);
549         }
550
551         /* device either stalls (value < 0) or reports success */
552         return value;
553 }
554
555 static struct usb_configuration sourcesink_driver = {
556         .label          = "source/sink",
557         .strings        = sourcesink_strings,
558         .setup          = sourcesink_setup,
559         .bConfigurationValue = 3,
560         .bmAttributes   = USB_CONFIG_ATT_SELFPOWER,
561         /* .iConfiguration = DYNAMIC */
562 };
563
564 /**
565  * sourcesink_add - add a source/sink testing configuration to a device
566  * @cdev: the device to support the configuration
567  */
568 int __init sourcesink_add(struct usb_composite_dev *cdev, bool autoresume)
569 {
570         int id;
571
572         /* allocate string ID(s) */
573         id = usb_string_id(cdev);
574         if (id < 0)
575                 return id;
576         strings_sourcesink[0].id = id;
577
578         source_sink_intf.iInterface = id;
579         sourcesink_driver.iConfiguration = id;
580
581         /* support autoresume for remote wakeup testing */
582         if (autoresume)
583                 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
584
585         /* support OTG systems */
586         if (gadget_is_otg(cdev->gadget)) {
587                 sourcesink_driver.descriptors = otg_desc;
588                 sourcesink_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
589         }
590
591         return usb_add_config(cdev, &sourcesink_driver, sourcesink_bind_config);
592 }