aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/misc/diag_bridge_test.c
blob: 5bc0828b4f421df312a6255cfe7f2e22558e9bac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/debugfs.h>
#include <linux/crc-ccitt.h>
#include <mach/diag_bridge.h>

#define DRIVER_DESC	"USB host diag bridge driver test"
#define DRIVER_VERSION	"1.0"

#define RD_BUF_SIZE	2048
#define DIAG_TEST_CONNECTED	0

struct diag_test_dev {
	char *read_buf;
	struct work_struct read_w;
	unsigned long	flags;

	struct diag_bridge_ops	ops;
};
static struct diag_test_dev *__dev;
static struct dentry *dent;

static void
diag_test_read_complete_cb(void *d, char *buf, size_t size, size_t actual)
{
	if (actual < 0) {
		pr_err("%s: read complete err\n", __func__);
		return;
	}

	print_hex_dump(KERN_INFO, "to_host:", 0, 1, 1, buf, actual, false);
}
static void diag_test_read_work(struct work_struct *w)
{
	struct diag_test_dev *dev =
		container_of(w, struct diag_test_dev, read_w);

	memset(dev->read_buf, 0, RD_BUF_SIZE);
	diag_bridge_read(dev->read_buf, RD_BUF_SIZE);
}

static void
diag_test_write_complete_cb(void *d, char *buf, size_t size, size_t actual)
{
	struct diag_test_dev *dev = d;

	if (actual > 0)
		schedule_work(&dev->read_w);
}

#if defined(CONFIG_DEBUG_FS)
#define DEBUG_BUF_SIZE	1024
static ssize_t send_ping_cmd(struct file *file, const char __user *ubuf,
				 size_t count, loff_t *ppos)
{
	struct diag_test_dev	*dev = __dev;
	unsigned char		*buf;
	int			temp = sizeof(unsigned char) * 4;

	if (!dev)
		return -ENODEV;

	buf = kmalloc(temp, GFP_KERNEL);
	if (!buf) {
		pr_err("%s: unable to allocate mem for ping cmd\n",
				__func__);
		return -ENOMEM;
	}

	/* hdlc encoded ping command */
	buf[0] = 0x0C;
	buf[1] = 0x14;
	buf[2] = 0x3A;
	buf[3] = 0x7E;

	diag_bridge_write(buf, temp);

	return count;
}

const struct file_operations diag_test_ping_ops = {
	.write = send_ping_cmd,
};

static void diag_test_debug_init(void)
{
	struct dentry *dfile;

	dent = debugfs_create_dir("diag_test", 0);
	if (IS_ERR(dent))
		return;

	dfile = debugfs_create_file("send_ping", 0444, dent,
			0, &diag_test_ping_ops);
	if (!dfile || IS_ERR(dfile))
		debugfs_remove(dent);
}
#else
static void diag_test_debug_init(void) { }
#endif

static int diag_test_remove(struct platform_device *pdev)
{
	diag_bridge_close();

	if (dent) {
		debugfs_remove_recursive(dent);
		dent = NULL;
	}

	return 0;
}

static int diag_test_probe(struct platform_device *pdev)
{
	struct diag_test_dev	*dev = __dev;
	int			ret = 0;

	pr_info("%s:\n", __func__);

	ret = diag_bridge_open(&dev->ops);
	if (ret)
		pr_err("diag open failed: %d", ret);


	diag_test_debug_init();

	return ret;
}

static struct platform_driver diag_test = {
	.remove = diag_test_remove,
	.probe	= diag_test_probe,
	.driver = {
		.name = "diag_bridge",
		.owner = THIS_MODULE,
	},
};

static int __init diag_test_init(void)
{
	struct diag_test_dev	*dev;
	int ret = 0;

	pr_info("%s\n", __func__);

	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev)
		return -ENOMEM;

	__dev = dev;

	dev->ops.read_complete_cb = diag_test_read_complete_cb;
	dev->ops.write_complete_cb = diag_test_write_complete_cb;
	dev->read_buf = kmalloc(RD_BUF_SIZE, GFP_KERNEL);
	if (!dev->read_buf) {
		pr_err("%s: unable to allocate read buffer\n", __func__);
		kfree(dev);
		return -ENOMEM;
	}

	dev->ops.ctxt = dev;
	INIT_WORK(&dev->read_w, diag_test_read_work);

	ret = platform_driver_register(&diag_test);
	if (ret)
		pr_err("%s: platform driver %s register failed %d\n",
				__func__, diag_test.driver.name, ret);

	return ret;
}

static void __exit diag_test_exit(void)
{
	struct diag_test_dev *dev = __dev;

	pr_info("%s:\n", __func__);

	if (test_bit(DIAG_TEST_CONNECTED, &dev->flags))
		diag_bridge_close();

	kfree(dev->read_buf);
	kfree(dev);

}

module_init(diag_test_init);
module_exit(diag_test_exit);

MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_VERSION(DRIVER_VERSION);
MODULE_LICENSE("GPL v2");