-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfib_dev.c
71 lines (62 loc) · 1.54 KB
/
fib_dev.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
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#define DEVICE_NAME "fibonacci"
static int Major;
static int device_open(struct inode *inode, struct file *filp)
{
printk(KERN_INFO "[device_open] device opened\n");
return 0;
}
static int device_release(struct inode *inode, struct file *filp)
{
printk(KERN_INFO "[device_release] device closed\n");
return 0;
}
static ssize_t device_read(struct file *filp,
char* buffer, size_t length,
loff_t *offset)
{
printk(KERN_INFO "[device_read] device read %d\n", *offset);
// memoization for fibonacci
int arr[2] = {0, 1};
int i;
for(i = 2; i <= *offset; i++)
arr[i%2] = arr[0] + arr[1];
return arr[(*offset)%2];
}
loff_t device_llseek(struct file *filp, loff_t off, int whence)
{
printk(KERN_INFO "[device_llseek] llseek is called\n");
void *dev = filp->private_data;
loff_t newpos;
switch(whence) {
case 0: /* SEEK_SET */
newpos = off;
break;
default: /* can't happen */
return -EINVAL;
}
filp->f_pos = newpos;
return newpos;
}
static struct file_operations fops = {
.read = device_read,
.open = device_open,
.release = device_release,
.llseek = device_llseek
};
int init_module(void)
{
Major = register_chrdev(0, DEVICE_NAME, &fops);
if (Major < 0) {
printk(KERN_ALERT "Registering char device failed with %d\n", Major);
return Major;
}
printk(KERN_INFO "[init_module] 'mknod /dev/%s c %d 0'\n", DEVICE_NAME, Major);
return 0;
}
void cleanup_module(void)
{
unregister_chrdev(Major, DEVICE_NAME);
}