-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev_driver.c
More file actions
executable file
·177 lines (141 loc) · 3.88 KB
/
Copy pathdev_driver.c
File metadata and controls
executable file
·177 lines (141 loc) · 3.88 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
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
/*
Linux Device driver module that changes brightness of the screen on mosue scroll.
*/
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/kdev_t.h>
#include <linux/unistd.h>
#include <linux/init.h>
// #include <linux/buffer_head.h>
// #include <asm/segment.h>
#include <asm/uaccess.h>
static int times = 0;
char brightness_buff[10], buff[10], *devName = "Mouse_Brightness_controller";
int k, btn_left, btn_right, btn_middle;
struct file *filehandle1, *filehandle2;
/*
The following functions help in reading/writing to files
within a Linux kernel module. File I/O should be avoided when possible.
Call VFS level functions instead of the syscall handler directly.
*/
struct file* file_open(const char* path, int flags, int rights)
{
struct file* filp = NULL;
int err = 0;
filp = filp_open(path, flags, rights);
if (IS_ERR(filp))
{
printk("Error opening file\n");
err = PTR_ERR(filp);
return NULL;
}
printk("File (%s) opened\n", path);
return filp;
}
void file_close(struct file* file)
{
filp_close(file, NULL);
}
int file_read(struct file* file, unsigned long long offset, unsigned char* data, unsigned int size, char* location)
{
int ret = kernel_read(file, (void*)data, size, &offset);
printk("File (%s) read\n", location );
return ret;
}
int file_write(struct file* file, unsigned long long offset, unsigned char* data, unsigned int size)
{
printk("Data - ( %s) written\n", data);
return kernel_write(file, (void*)data, size, &offset);
}
int dev_open(struct inode *dev_inode, struct file *fp){
times++;
printk("Device opened %d times\n", times);
return 0;
}
ssize_t dev_read(struct file *fp, char __user* buffer, size_t length, loff_t *offset){
printk("inside funtion %s\n", __FUNCTION__);
return 0;
}
ssize_t dev_write(struct file *fp, const char __user* buffer, size_t length, loff_t *offset){
if(copy_from_user(buff, buffer, length) != 0){
printk("Error opening write\n");
return -EFAULT;
}
printk("Inside funtion %s, the data gotten is %s\n", __FUNCTION__, buff);
int brightness = 0, i, n;
char *brightnessController = "/sys/class/backlight/intel_backlight/brightness";
filehandle1 = file_open(brightnessController, 0, 0);
file_read(filehandle1, 0, brightness_buff, 6, brightnessController);
printk("Current brightness: %s\n", brightness_buff);
i = 0;
while(i < 6 && (brightness_buff[i] >= 48 && brightness_buff[i] <58)){
brightness*=10;
brightness += (brightness_buff[i++] - 48);
}
if (buff[0] == '0')
{
printk("Up-scroll detected, Brightness increased\n");
if (brightness < 7000)
brightness += 500;
printk(brightness_buff);
}
else if (buff[0] == '1')
{
printk("Down-scroll detected, Brightness decreased\n");
if (brightness > 500)
brightness -= 500;
printk(brightness_buff);
}
else
{
printk("No data\n");
}
i = 0;
memset(brightness_buff, 0, 10);
memset(buff, 0, 10);
while(brightness){
brightness_buff[i++] = brightness%10 + 48;
brightness/=10;
}
n = i-1;
while(i >= 0){
buff[n-i] = brightness_buff[i];
i--;
}
printk("Updated brightness: %s\n", buff);
file_close(filehandle1);
filehandle1 = file_open(brightnessController, O_WRONLY, 0);
file_write(filehandle1, 0, buff, 6);
file_close(filehandle1);
return length;
}
int dev_close(struct inode *dev_inode, struct file *fp){
printk("inside funtion %s\n", __FUNCTION__);
return 0;
}
static struct file_operations fops =
{
.owner = THIS_MODULE,
.read = dev_read,
.open = dev_open,
.write = dev_write,
.release = dev_close,
};
static int __init dev_init(void){
if(register_chrdev(45, devName, &fops) < 0)
printk("Failed\n");
else
printk("Device registered");
return 0;
}
static void __exit dev_exit(void){
printk("Device removed!");
unregister_chrdev(45, devName);
}
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Mouse Driver");
MODULE_AUTHOR("Custom");