-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcdata.c
More file actions
216 lines (175 loc) · 4.26 KB
/
cdata.c
File metadata and controls
216 lines (175 loc) · 4.26 KB
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
#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/vmalloc.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/miscdevice.h>
#include <linux/input.h>
#include <linux/wait.h>
#include <linux/workqueue.h>
#include <linux/platform_device.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include "cdata_ioctl.h"
#define CDATA_MAJOR 121
#define BUF_SIZE 32
struct cdata_t {
char *buf;
unsigned int idx;
wait_queue_head_t writeable;
#if 0
struct work_struct work;
#endif
struct timer_list timer;
};
#if 0
void flush_data(struct work_struct *work);
#endif
void flush_data(unsigned long);
#if 0
void flush_data(struct work_struct *work)
#endif
void flush_data(unsigned long priv)
{
#if 0
struct cdata_t *cdata = container_of(work, struct cdata_t, work);
#endif
struct cdata_t *cdata = (struct cdata_t *)priv;
char *buf = cdata->buf;
int idx = cdata->idx;
buf[idx] = '\0';
printk(KERN_ALERT "buf = %s\n", buf);
cdata->idx = 0;
wake_up(&cdata->writeable);
}
static int cdata_open(struct inode *inode, struct file *filp)
{
struct cdata_t *cdata;
cdata = (struct cdata_t *)kmalloc(sizeof(struct cdata_t), GFP_KERNEL);
cdata->buf = (char *)kmalloc(BUF_SIZE, GFP_KERNEL);
cdata->idx = 0;
init_waitqueue_head(&cdata->writeable);
#if 0
INIT_WORK(&cdata->work, flush_data);
#endif
init_timer(&cdata->timer);
filp->private_data = (void *)cdata;
printk(KERN_ALERT "cdata in open: filp = %p\n", filp);
return 0;
}
static int cdata_close(struct inode *inode, struct file *filp)
{
struct cdata_t *cdata = (struct cdata_t *)filp->private_data;
char *buf;
unsigned int idx;
buf = cdata->buf;
idx = cdata->idx;
buf[idx] = '\0';
printk(KERN_ALERT "cdata in close: buf = %s\n", buf);
kfree(cdata->buf);
kfree(cdata);
return 0;
}
static int cdata_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
struct cdata_t *cdata = (struct cdata_t *)filp->private_data;
char *buf;
unsigned int idx;
buf = cdata->buf;
idx = cdata->idx;
switch (cmd) {
case IOCTL_EMPTY:
printk(KERN_ALERT "in ioctl: IOCTL_EMPTY\n");
idx = 0;
return 0;
case IOCTL_SYNC:
printk(KERN_ALERT "in ioctl: IOCTL_SYNC\n");
return 0;
default:
return -ENOTTY;
}
return 0;
}
static ssize_t cdata_write(struct file *filp, const char __user *user,
size_t len, loff_t *off)
{
struct cdata_t *cdata = (struct cdata_t *)filp->private_data;
int idx;
int i;
for (i = 0; i < len; i++) {
if (cdata->idx >= BUF_SIZE) {
#if 0
schedule_work(&cdata->work);
#endif
cdata->timer.expires = jiffies + 1;
cdata->timer.function = flush_data;
cdata->timer.data = (unsigned long)cdata;
add_timer(&cdata->timer);
}
wait_event_interruptible(cdata->writeable,
!(cdata->idx >= BUF_SIZE));
idx = cdata->idx;
copy_from_user(&cdata->buf[idx], &user[i], 1);
idx++;
cdata->idx = idx;
}
return len;
}
static const struct file_operations cdata_fops = {
owner: THIS_MODULE,
open: cdata_open,
release: cdata_close,
write: cdata_write,
unlocked_ioctl: cdata_ioctl
};
static struct miscdevice cdata_miscdev = {
.minor = 199,
.name = "cdata-misc",
.fops = &cdata_fops,
};
static int cdata_plat_probe(struct platform_device *pdev)
{
int ret;
ret = misc_register(&cdata_miscdev);
if (ret < 0) {
printk(KERN_ALERT "misc_register failed\n");
goto exit;
}
return 0;
exit:
return -1;
}
static int cdata_plat_remove(struct platform_device *pdev)
{
misc_deregister(&cdata_miscdev);
return 0;
}
static struct platform_driver cdata_plat_driver = {
.driver = {
.name = "cdata",
.owner = THIS_MODULE
},
.probe = cdata_plat_probe,
.remove = cdata_plat_remove,
};
static int __init cdata_init_module(void)
{
platform_driver_register(&cdata_plat_driver);
printk(KERN_ALERT "cdata module: registerd.\n");
}
void cdata_cleanup_module(void)
{
platform_driver_unregister(&cdata_plat_driver);
printk(KERN_ALERT "cdata module: unregisterd.\n");
}
module_init(cdata_init_module);
module_exit(cdata_cleanup_module);
MODULE_LICENSE("GPL");