-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProc_and_cdev_test.c
275 lines (230 loc) · 5.82 KB
/
Proc_and_cdev_test.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
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
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/i2c.h>
#include <linux/cdev.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/proc_fs.h>
#include <linux/platform_device.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#define DIR_NAME "test_proc"
#define ENTRY_NAME "rw_test"
#define DEVICE_NAME "charp-dev"
#define DEVICE_NUM 2
#define GLOBALMEM_SIZE 0x1000
#define GLOBALMEM_MAJOR 0
struct globalmem_dev{
struct cdev cdev;
unsigned char cdev_mem[GLOBALMEM_SIZE];
unsigned char proc_mem[GLOBALMEM_SIZE];
};
struct class *globalmem_cdev_class;
static int globalmem_major = GLOBALMEM_MAJOR;
static struct proc_dir_entry *test_dir = NULL;
static struct proc_dir_entry *test_entry = NULL;
static struct globalmem_dev *globalmem_devp= NULL;
static int proc_open(struct inode *inode, struct file *filp)
{
filp->private_data = globalmem_devp;
return 0;
}
static ssize_t proc_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
{
int ret = 0;
unsigned long count = size;
unsigned long p = *ppos;
struct globalmem_dev *dev = filp->private_data;
if(count > GLOBALMEM_SIZE)
return -EFAULT;
if(count > GLOBALMEM_SIZE - p)
count = GLOBALMEM_SIZE - p;
if(copy_to_user(buf, dev->proc_mem, count))
{
return -EINVAL;
}
else
{
*ppos += count;
ret = count;
}
printk(KERN_INFO "read %ld bytes(s) from %ld\n", count, p);
return ret;
}
static ssize_t proc_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
{
int ret = 0;
unsigned long count = size;
unsigned long p = *ppos;
struct globalmem_dev *dev = filp->private_data;
if(count > GLOBALMEM_SIZE)
return -EFAULT;
if(count > GLOBALMEM_SIZE - p)
count = GLOBALMEM_SIZE - p;
if(copy_from_user(dev->proc_mem, buf, count))
{
return -EINVAL;
}
else
{
*ppos += count;
ret = count;
}
printk(KERN_INFO "write %ld bytes(s) to %ld\n", count, p);
return ret;
}
static int proc_release(struct inode *inode, struct file *filp)
{
return 0;
}
static const struct file_operations proc_fops = {
.owner = THIS_MODULE,
.open = proc_open,
.read = proc_read,
.write = proc_write,
.release = proc_release,
};
static int globalmem_setup_proc(void)
{
test_dir = proc_mkdir(DIR_NAME, NULL);
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)/* 从3.10内核后架构有转变 */
test_entry = create_proc_entry(ENTRY_NAME, 0666, test_dir);
test_entry->proc_fops = &proc_fops;
#else
test_entry = proc_create(ENTRY_NAME, 0666, test_dir, &proc_fops);
#endif
return 0;
}
static int cdev_open(struct inode *inode, struct file *filp)
{
struct globalmem_dev *dev = NULL;
dev = container_of(inode->i_cdev, struct globalmem_dev, cdev);
filp->private_data = dev;
return 0;
}
static ssize_t cdev_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
{
int ret = 0;
unsigned long count = size;
unsigned long p = *ppos;
struct globalmem_dev *dev = filp->private_data;
if(count > GLOBALMEM_SIZE)
return -EFAULT;
if(count > GLOBALMEM_SIZE - p)
count = GLOBALMEM_SIZE - p;
if(copy_to_user(buf, dev->cdev_mem, count))
{
return -EINVAL;
}
else
{
*ppos += count;
ret = count;
}
printk(KERN_INFO "read %ld bytes(s) from %ld\n", count, p);
return ret;
}
static ssize_t cdev_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
{
int ret = 0;
unsigned long count = size;
unsigned long p = *ppos;
struct globalmem_dev *dev = filp->private_data;
if(count > GLOBALMEM_SIZE)
return -EFAULT;
if(count > GLOBALMEM_SIZE - p)
count = GLOBALMEM_SIZE - p;
if(copy_from_user(dev->cdev_mem, buf, count))
{
return -EINVAL;
}
else
{
*ppos += count;
ret = count;
}
printk(KERN_INFO "write %ld bytes(s) to %ld\n", count, p);
return ret;
}
static int cdev_close(struct inode *inode, struct file *filp)
{
return 0;
}
static const struct file_operations cdev_fops = {
.owner = THIS_MODULE,
.open = cdev_open,
.read = cdev_read,
.write = cdev_write,
.release = cdev_close,
};
static int globalmem_setup_cdev(struct globalmem_dev * dev, int index)
{
int ret = 0;
char name[100];
dev_t devno = MKDEV(globalmem_major, index);
if(globalmem_major)
{
register_chrdev_region(devno, 1, DEVICE_NAME);
}
else
{
ret = alloc_chrdev_region(&devno, 0, 1, DEVICE_NAME);
globalmem_major = MAJOR(devno);
}
cdev_init(&dev->cdev, &cdev_fops);
ret = cdev_add(&dev->cdev, devno, 1);
if(ret)
printk(KERN_NOTICE "Error %d adding globalmem cdev %d", ret, index);
sprintf(name, "%s-class-%d", DEVICE_NAME, index);
globalmem_cdev_class = class_create(THIS_MODULE, name);
if(IS_ERR(globalmem_cdev_class))
{
printk("Err: failed in creating class./n");
return -1;
}
sprintf(name, "%s-%d", DEVICE_NAME, index);
device_create(globalmem_cdev_class, NULL, devno, NULL, name);
return 0;
}
static int __init proc_module_init(void)
{
int ret, i;
globalmem_devp = kmalloc(sizeof(struct globalmem_dev), GFP_KERNEL);
if(NULL == globalmem_devp)
{
printk("kmalloc globalmem_dev error\n");
ret = -ENOMEM;
goto fail_malloc;
}
memset(globalmem_devp, 0, sizeof(struct globalmem_dev));
globalmem_setup_proc();
for(i=0; i < DEVICE_NUM; i++)
{
globalmem_setup_cdev(globalmem_devp + i, i);
}
return 0;
fail_malloc:
unregister_chrdev_region(MKDEV(globalmem_major, 0), 1);
return ret;
}
static void __exit proc_module_exit(void)
{
remove_proc_entry(ENTRY_NAME, test_dir);
remove_proc_entry(DIR_NAME, NULL);
cdev_del(&globalmem_devp[0].cdev);
cdev_del(&globalmem_devp[1].cdev);
kfree(globalmem_devp);
unregister_chrdev_region(MKDEV(globalmem_major, 0), 2);
}
module_init(proc_module_init);
module_exit(proc_module_exit);
MODULE_AUTHOR("kiwei");
MODULE_DESCRIPTION("test proc_file and cdev");
MODULE_LICENSE("GPL");