-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcdata.c
138 lines (107 loc) · 2.94 KB
/
cdata.c
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
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/wait.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include "cdata_ioctl.h"
#ifdef CONFIG_SMP
#define __SMP__
#endif
#define CDATA_MAJOR 121
struct cdata_t {
char *buf;
int index;
wait_queue_head_t wq;
struct timer_list timer;
};
void flush_buffer(unsigned long priv)
{
struct cdata_t *cdata = (struct cdata_t *)priv;
cdata->index = 0;
wake_up(&cdata->wq);
}
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(64, GFP_KERNEL);
cdata->index = 0;
init_waitqueue_head (&cdata->wq);
init_timer(&cdata->timer);
filp->private_data = (void *)cdata;
return 0;
}
static int cdata_ioctl(struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
printk(KERN_ALERT "cdata: in cdata_ioctl()\n");
}
static ssize_t cdata_read(struct file *filp, char *buf,
size_t size, loff_t *off)
{
printk(KERN_ALERT "cdata: in cdata_read()\n");
}
static ssize_t cdata_write(struct file *filp, const char *buf,
size_t size, loff_t *off)
{
struct cdata_t *cdata = (struct cdata_t *)filp->private_data;
struct timer_list *timer = &cdata->timer;
int i;
for (i = 0; i < size; i++) {
copy_from_user(&cdata->buf[cdata->index], &buf[i], 1);
cdata->index++;
if (cdata->index >= 64) {
printk(KERN_ALERT "cdata: buffer full\n");
// schedule execution (deferred execution)
timer->expires = jiffies + 5*HZ;
timer->function = flush_buffer;
timer->data = (unsigned long)cdata;
add_timer(timer);
interruptible_sleep_on(&cdata->wq);
// Ok, buffer is empty
printk(KERN_ALERT "cdata: buffer empty\n");
}
}
return 0;
}
static int cdata_release(struct inode *inode, struct file *filp)
{
struct cdata_t *cdata = (struct cdata_t *)filp->private_data;
printk(KERN_ALERT "cdata: in cdata_release()\n");
printk(KERN_ALERT "cdata: flush buf = %s\n", cdata->buf);
return 0;
}
struct file_operations cdata_fops = {
owner: THIS_MODULE,
open: cdata_open,
release: cdata_release,
ioctl: cdata_ioctl,
read: cdata_read,
write: cdata_write,
};
static struct miscdevice cdata_misc = {
minor: 20,
name: "cdata-test",
fops: &cdata_fops,
};
int my_init_module(void)
{
if (misc_register(&cdata_misc)) {
printk(KERN_ALERT "cdata: register failed\n");
return -1;
}
printk(KERN_ALERT "cdata module: registered.\n");
return 0;
}
void my_cleanup_module(void)
{
misc_deregister(&cdata_misc);
printk(KERN_ALERT "cdata module: unregisterd.\n");
}
module_init(my_init_module);
module_exit(my_cleanup_module);
MODULE_LICENSE("GPL");