summaryrefslogtreecommitdiff
path: root/drivers/char/mux.c
blob: 6b846e68aff6dba2aba86c83209e840a3f3918f7 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (C) 2012 Spreadtrum Communications Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/poll.h>
#include <linux/platform_device.h>
#include <linux/slab.h>

#include <linux/sprdmux.h>

struct mux_device {
	struct mux_init_data	*init;
	int			major;
	int			minor;
	struct cdev		cdev;
};

struct mux_channel {
	int		mux_id;
	int		line;
};

static struct class		*mux_class;

static int mux_open(struct inode *inode, struct file *filp)
{
	int minor = iminor(filp->f_path.dentry->d_inode);
	struct mux_device *mux;
	struct mux_channel *channel;
	int rval, mux_id, line;

	mux = container_of(inode->i_cdev, struct mux_device, cdev);

	if (NULL == mux) {
		printk(KERN_ERR "MUX: Error %s mux_dev is NULL\n", __FUNCTION__);
		return -ENODEV;
	}

	mux_id = mux->init->id;

	rval = ts0710_mux_status(mux_id);
	if (rval != 0) {
		printk(KERN_ERR "MUX: Error %s [%d] mux_status is Not OK\n", __FUNCTION__, mux->init->id);
		filp->private_data = NULL;
		return -ENODEV;
	}

	printk(KERN_ERR "MUX: %s mux_id  = %d, minor = %d, mux->minor = %d\n", __FUNCTION__, mux_id , minor, mux->minor);

	line = minor - mux->minor;

	rval = ts0710_mux_open(mux_id, line);
	if (rval != 0) {
		printk(KERN_ERR "MUX: Error %s id[%d] line[%d] mux_open is Not OK\n", __FUNCTION__, mux->init->id, line);
		filp->private_data = NULL;
		return rval;
	}

	channel = kmalloc(sizeof(struct mux_channel), GFP_KERNEL);
	if (!channel) {
		return -ENOMEM;
	}

	channel->mux_id = mux_id;
	channel->line = line;

	filp->private_data = channel;

	return 0;
}

static int mux_release(struct inode *inode, struct file *filp)
{
	struct mux_channel *channel = filp->private_data;

	if (channel == NULL) {
		printk(KERN_ERR "MUX: Error %s channel is NULL\n", __FUNCTION__);
		return -ENODEV;
	}

	ts0710_mux_close(channel->mux_id, channel->line);

	kfree(channel);

	return 0;
}

static ssize_t mux_read(struct file *filp,
		char __user *buf, size_t count, loff_t *ppos)
{
	int timeout = -1;
	struct mux_channel *channel = filp->private_data;

	if (channel == NULL) {
		printk(KERN_ERR "MUX: Error %s channel is NULL\n", __FUNCTION__);
		return -ENODEV;
	}

	if (filp->f_flags & O_NONBLOCK) {
		timeout = 0;
	}

	return ts0710_mux_read(channel->mux_id, channel->line, buf, count, timeout);

}

static ssize_t mux_write(struct file *filp,
		const char __user *buf, size_t count, loff_t *ppos)
{
	int timeout = -1;
	struct mux_channel *channel = filp->private_data;

	if (channel == NULL) {
		printk(KERN_ERR "MUX: Error %s channel is NULL\n", __FUNCTION__);
		return -ENODEV;
	}

	if (filp->f_flags & O_NONBLOCK) {
		timeout = 0;
	}

	return ts0710_mux_write(channel->mux_id, channel->line, buf, count, timeout);

}

static unsigned int mux_poll(struct file *filp, poll_table *wait)
{
	struct mux_channel *channel = filp->private_data;

	if (channel == NULL) {
		printk(KERN_ERR "MUX: Error %s channel is NULL\n", __FUNCTION__);
		return -ENODEV;
	}

	return ts0710_mux_poll_wait(channel->mux_id, channel->line, filp, wait);
}

static long mux_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	struct mux_channel *channel = filp->private_data;

	if (channel == NULL) {
		printk(KERN_ERR "MUX: Error %s channel is NULL\n", __FUNCTION__);
		return -ENODEV;
	}

	return ts0710_mux_mux_ioctl(channel->mux_id, channel->line, cmd, arg);
}

static const struct file_operations mux_fops = {
	.open		= mux_open,
	.release	= mux_release,
	.read		= mux_read,
	.write		= mux_write,
	.poll		= mux_poll,
	.unlocked_ioctl	= mux_ioctl,
	.owner		= THIS_MODULE,
	.llseek		= default_llseek,
};

static int  mux_probe(struct platform_device *pdev)
{
	dev_t devid;
	int i, rval;
	struct mux_device *mux;
	struct mux_init_data *init = pdev->dev.platform_data;

	mux = kzalloc(sizeof(struct mux_device), GFP_KERNEL);
	if (mux == NULL) {
		printk(KERN_ERR "MUX: Error %s no memory for mux\n", __FUNCTION__);
		return -ENOMEM;
	}

	rval = alloc_chrdev_region(&devid, 0, init->num, init->name);
	if (rval != 0) {
		kfree(mux);
		printk(KERN_ERR "MUX: Error %s no memory for dev_region\n", __FUNCTION__);
		return rval;
	}

	cdev_init(&(mux->cdev), &mux_fops);

	rval = cdev_add(&(mux->cdev), devid, init->num);
	if (rval != 0) {
		kfree(mux);
		unregister_chrdev_region(devid, init->num);
		printk(KERN_ERR "MUX: Error %s add error\n", __FUNCTION__);
		return rval;
	}

	mux->major = MAJOR(devid);
	mux->minor = MINOR(devid);

	if (init->num > 1) {
		for (i = 0; i < init->num; i++) {
			device_create(mux_class, NULL,
				MKDEV(mux->major, mux->minor + i),
				NULL, "%s%d", init->name, i);
		}
	} else {
		device_create(mux_class, NULL,
			MKDEV(mux->major, mux->minor),
			NULL, "%s", init->name);
	}

	mux->init = init;

	platform_set_drvdata(pdev, mux);

	return 0;
}

static int  mux_remove(struct platform_device *pdev)
{
	int i;
	struct mux_device *mux = platform_get_drvdata(pdev);

	for (i = 0; i < mux->init->num; i++) {
		device_destroy(mux_class, MKDEV(mux->major, mux->minor + i));
	}

	cdev_del(&(mux->cdev));

	unregister_chrdev_region(MKDEV(mux->major, mux->minor), mux->init->num);

	kfree(mux);

	platform_set_drvdata(pdev, NULL);

	return 0;
}

static struct platform_driver mux_driver = {
	.driver = {
		.owner = THIS_MODULE,
		.name = "mux",
	},
	.probe = mux_probe,
	.remove = mux_remove,
};

static int __init mux_init(void)
{
	printk(KERN_ERR "MUX: %s entered\n", __FUNCTION__);

	ts0710_mux_init();

	mux_class = class_create(THIS_MODULE, "mux");
	if (IS_ERR(mux_class))
		return PTR_ERR(mux_class);

	return platform_driver_register(&mux_driver);
}

static void __exit mux_exit(void)
{
	printk(KERN_ERR "MUX: %s entered\n", __FUNCTION__);
	ts0710_mux_exit();
	class_destroy(mux_class);
	platform_driver_unregister(&mux_driver);
}

module_init(mux_init);
module_exit(mux_exit);

MODULE_AUTHOR("Wu Jiaoyou");
MODULE_DESCRIPTION("mux driver");
MODULE_LICENSE("GPL");