-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.c
116 lines (103 loc) · 2.58 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
/* Necessary includes for device drivers */
#include <linux/init.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/uaccess.h> /* copy_from/to_user */
#include <linux/uaccess.h>
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, const 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;
int memory_init(void)
{
int result;
/* Registering device */
result = register_chrdev(memory_major, "memory", &memory_fops);
if (result < 0)
{
printk("<1>memory: cannot obtain major number %d\n", memory_major);
return result;
}
/* Allocating memory for the buffer */
memory_buffer = kmalloc(4, GFP_KERNEL);
if (!memory_buffer)
{
result = -ENOMEM;
goto fail;
}
/*Set buffer to 0 by default */
memset(memory_buffer, 0, 1);
printk("<1>Inserting memory module\n");
return 0;
fail:
memory_exit();
return result;
}
void memory_exit(void)
{
/* Freeing the major number */
unregister_chrdev(memory_major, "memory");
/* Freeing buffer memory */
if (memory_buffer)
{
kfree(memory_buffer);
}
printk("<1>Removing memory module\n");
}
int memory_open(struct inode *inode, struct file *filp)
{
/* Success */
return 0;
}
int memory_release(struct inode *inode, struct file *filp)
{
/* Success */
return 0;
}
ssize_t memory_read(struct file *filp, char *buf,size_t count, loff_t *f_pos)
{
/* Changing reading position as best suits */
if (*f_pos == 0)
{
*f_pos+=1;
/* Transfering data to user space */
copy_to_user(buf,memory_buffer,4);
return 4;
}
else
{
return 0;
}
}
ssize_t memory_write( struct file *filp, const char *buf, size_t count, loff_t *f_pos) {
copy_from_user(memory_buffer,buf,4);
return 4;
}