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