-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.c
152 lines (119 loc) · 3.19 KB
/
memory.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
/* A simple character device, "memory", that allows a character to be read */
/* or written from memory */
/* Necessary includes for device drivers */
#include <linux/init.h>
// #include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/fs.h> /* everything... */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/proc_fs.h>
#include <linux/fcntl.h> /* O_ACCMODE */
//#include <asm/system.h> /* cli(), *_flags */
#include <asm/uaccess.h> /* copy_from/to_user */
MODULE_LICENSE("Dual BSD/GPL");
/* Declaration of memory.c functions */
int memory_open(struct inode *inode, struct file *filp);
int memory_release(struct inode *inode, struct file *filp);
ssize_t memory_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
ssize_t memory_write(struct file *filp, char *buf, size_t count, loff_t *f_pos);
void memory_exit(void);
int memory_init(void);
/* Structure that declares the usual file */
/* access functions */
struct file_operations memory_fops = {
read: memory_read,
write: memory_write,
open: memory_open,
release: memory_release
};
/* Declaration of the init and exit functions */
module_init(memory_init);
module_exit(memory_exit);
/* Global variables of the driver */
/* Major number */
int memory_major = 60;
/* Buffer to store data */
char *memory_buffer;
/* Memory device initialization function */
int memory_init(void)
{
int result;
/* Registering device */
result = register_chrdev(memory_major, "memory", &memory_fops);
if (result < 0) {
printk(KERN_ERR "mem_dev: cannot obtain major number %d\n", memory_major);
return result;
}
/* Allocating memory for the buffer */
memory_buffer = kmalloc(1, GFP_KERNEL);
if (!memory_buffer) {
result = -ENOMEM;
goto fail;
}
memset(memory_buffer, 0, 1);
printk(KERN_ERR "Inserting memory module\n");
return 0;
fail:
memory_exit();
return result;
}
/* Memory device exit function */
void memory_exit(void)
{
/* Freeing the major number */
unregister_chrdev(memory_major, "memory");
/* Freeing buffer memory */
if (memory_buffer) {
kfree(memory_buffer);
}
printk(KERN_ERR "Removing memory module\n");
}
/* Memory device open function */
int memory_open(struct inode *inode, struct file *filp)
{
/* Success */
return 0;
}
/* Memory device release function */
int memory_release(struct inode *inode, struct file *filp)
{
/* Success */
return 0;
}
/* Memory device read function */
ssize_t memory_read(struct file *filp, char *buf,
size_t count, loff_t *f_pos)
{
ssize_t retval = 0;
/* Transfering data to user space */
if (copy_to_user(buf,memory_buffer,1)) {
retval = -EFAULT;
goto out;
}
if (*f_pos == 0) {
*f_pos += 1;
retval = 1;
}
else
retval = 0;
out:
return retval;
}
/* Memory device write function */
ssize_t memory_write( struct file *filp, char *buf,
size_t count, loff_t *f_pos)
{
ssize_t retval = 0;
char *temp;
temp = buf + count - 1;
if (copy_from_user(memory_buffer,temp,1)) {
retval = -EFAULT;
goto out;
}
retval = 1;
out:
return retval;
}