blob: d7c93a39c968895399f1b0727068eada6967758c [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* drivers/usb/function/loopback.c
2 *
3 * Simple Loopback Function Device
4 *
5 * Copyright (C) 2007 Google, Inc.
6 * Author: Brian Swetland <swetland@google.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
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 */
18
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/kernel.h>
22
23#include "usb_function.h"
24
25struct loopback_context
26{
27 struct usb_endpoint *out;
28 struct usb_endpoint *in;
29 struct usb_request *req_out;
30 struct usb_request *req_in;
31};
32
33static struct loopback_context _context;
34
35static void loopback_bind(struct usb_endpoint **ept, void *_ctxt)
36{
37 struct loopback_context *ctxt = _ctxt;
38
39 ctxt->out = ept[0];
40 ctxt->in = ept[1];
41
42 printk(KERN_INFO "loopback_bind() %p, %p\n", ctxt->out, ctxt->in);
43
44 ctxt->req_out = usb_ept_alloc_req(ctxt->out, 4096);
45 ctxt->req_in = usb_ept_alloc_req(ctxt->in, 4096);
46}
47
48static void loopback_queue_in(struct loopback_context *ctxt, void *data, unsigned len);
49static void loopback_queue_out(struct loopback_context *ctxt);
50
51static void loopback_in_complete(struct usb_endpoint *ept, struct usb_request *req)
52{
53 struct loopback_context *ctxt = req->context;
54 printk(KERN_INFO "loopback_out_complete (%d)\n", req->actual);
55 loopback_queue_out(ctxt);
56}
57
58static void loopback_out_complete(struct usb_endpoint *ept, struct usb_request *req)
59{
60 struct loopback_context *ctxt = req->context;
61 printk(KERN_INFO "loopback_in_complete (%d)\n", req->actual);
62
63 if (req->status == 0) {
64 loopback_queue_in(ctxt, req->buf, req->actual);
65 } else {
66 loopback_queue_out(ctxt);
67 }
68}
69
70static void loopback_queue_out(struct loopback_context *ctxt)
71{
72 struct usb_request *req = ctxt->req_out;
73
74 req->complete = loopback_out_complete;
75 req->context = ctxt;
76 req->length = 4096;
77
78 usb_ept_queue_xfer(ctxt->out, req);
79}
80
81static void loopback_queue_in(struct loopback_context *ctxt, void *data, unsigned len)
82{
83 struct usb_request *req = ctxt->req_in;
84
85 memcpy(req->buf, data, len);
86 req->complete = loopback_in_complete;
87 req->context = ctxt;
88 req->length = len;
89
90 usb_ept_queue_xfer(ctxt->in, req);
91}
92
93static void loopback_configure(int configured, void *_ctxt)
94{
95 struct loopback_context *ctxt = _ctxt;
96 printk(KERN_INFO "loopback_configure() %d\n", configured);
97
98 if (configured) {
99 loopback_queue_out(ctxt);
100 } else {
101 /* all pending requests will be canceled */
102 }
103}
104
105static struct usb_function usb_func_loopback = {
106 .bind = loopback_bind,
107 .configure = loopback_configure,
108
109 .name = "loopback",
110 .context = &_context,
111
112 .ifc_class = 0xff,
113 .ifc_subclass = 0xff,
114 .ifc_protocol = 0xff,
115
116 .ifc_name = "loopback",
117
118 .ifc_ept_count = 2,
119 .ifc_ept_type = { EPT_BULK_OUT, EPT_BULK_IN },
120};
121
122static int __init loopback_init(void)
123{
124 printk(KERN_INFO "loopback_init()\n");
125 return usb_function_register(&usb_func_loopback);
126}
127
128module_init(loopback_init);