-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmine.c
More file actions
95 lines (84 loc) · 2.22 KB
/
mine.c
File metadata and controls
95 lines (84 loc) · 2.22 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
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/fs.h>
#include<linux/slab.h>
#include<linux/types.h>
#include<asm/types.h>
#include<asm/uaccess.h>
#include<linux/cdev.h>
MODULE_LICENSE("Dual BSD/GPL");
#define MAJOR_NO 12
int memory_open(struct inode *inode,struct file*filp);
void memory_close();
ssize_t memory_read(struct file * filep,char* buff,size_t count,loff_t* f_pos);
ssize_t memory_write(struct file*filp,char * buff,size_t count,loff_t * f_pos);
void memory_exit();
int memory_init();
/*The file ops structure definition for my driver Currently dealing only with open close read and write
*/
struct file_operations memory_fops={
read:memory_read,
write:memory_write,
open:memory_open,
release:memory_close
};
struct mydev {
dev_t devno;
int maximum;
char * buffer;
struct cdev mycdev;
size_t size;
}mydev;
int __init module_start_up(){
mydev.devno=MKDEV(MAJOR_NO,12);
cdev_init(&(mydev.mycdev),&memory_fops);
int result=cdev_add(&(mydev.mycdev),mydev.devno,12);
if(result<0){
printk(KERN_ALERT "Error registering device");
return result;
}
mydev.maximum=1024*1024;
mydev.size=0;
mydev.buffer=kmalloc(mydev.maximum,GFP_KERNEL);
if(!mydev.buffer){
result=-ENOMEM;
memory_close();
return result;
}
memset(mydev.buffer,0,mydev.maximum);
return 0;
}
void memory_close(){
cdev_del(&(mydev.mycdev));
if(mydev.buffer)
kfree(mydev.buffer);
}
ssize_t memory_read(struct file * filep,char * buff,size_t count,loff_t * f_pos){
int copysize;
if(buff)
return -ENOMEM;//don't ask me why i really need to think about this return value
if(mydev.size>count )
copysize=count;
else
copysize=mydev.size;
int k=copy_to_user(mydev.buffer,buff,copysize);
return k;
}
ssize_t memory_write(struct file * filep,char * buff,size_t count,loff_t * f_pos){
if(!buff)
return -ENOMEM;// again food for thought
if(count+mydev.size>mydev.maximum)
return -ENOMEM;
int k=copy_from_user(mydev.buffer,buff,count);
return k;
}
int memory_open(struct inode * inode ,struct file * filep){
return 0;
}
void __exit module_stop(){
printk(KERN_INFO "Goof Bye world");
memory_close();
}
module_init(module_start_up);
module_exit(module_stop);