-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpcd.c
More file actions
225 lines (169 loc) · 5.64 KB
/
pcd.c
File metadata and controls
225 lines (169 loc) · 5.64 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/kdev_t.h>
#include <linux/uaccess.h>
#include <linux/err.h>
#include <linux/mutex.h>
#define DEV_MEM_SIZE 512
/** @brief This modify pr_info for printing the function name */
#undef pr_fmt
#define pr_fmt(fmt) "%s : " fmt,__func__
static DEFINE_MUTEX(pcd_lock);
/** @brief pseudo device's memory */
char device_buffer[DEV_MEM_SIZE];
/** @brief This holds the device number */
dev_t device_number;
/** @brief cdev variable */
struct cdev pcd_cdev;
loff_t pcd_lseek(struct file* p_file, loff_t offset, int whence){
loff_t temp;
pr_info("lseek requested\n");
pr_info("current value of the file position = %lld\n", p_file->f_pos);
switch(whence){
case SEEK_SET:
if((offset > DEV_MEM_SIZE) || (offset < 0))
return -EINVAL;
p_file->f_pos = offset;
break;
case SEEK_CUR:
temp = p_file->f_pos + offset;
if((temp > DEV_MEM_SIZE) || (temp < 0))
return -EINVAL;
p_file->f_pos = temp;
break;
case SEEK_END:
temp = DEV_MEM_SIZE + offset;
if((temp > DEV_MEM_SIZE) || (temp < 0))
return -EINVAL;
p_file->f_pos = temp;
break;
default:
return -EINVAL;
}
pr_info("new value of the file position = %lld\n", p_file->f_pos);
return p_file->f_pos;
}
ssize_t pcd_read(struct file* p_file, char __user* buff, size_t count, loff_t* f_pos)
{
if(mutex_lock_interruptible(&pcd_lock)){
return -EINTR;
}
pr_info("read requested for %zu bytes\n", count);
pr_info("current file position = %lld\n", *f_pos);
/* Adjust the count */
if((*f_pos + count) > DEV_MEM_SIZE)
count = DEV_MEM_SIZE - *f_pos;
/* Copy to user */
if(copy_to_user(buff, &device_buffer[*f_pos], count))
return -EFAULT;
/* Update the current file position */
*f_pos += count;
pr_info("number of bytes successfully read = %zu\n", count);
pr_info("updated file position = %lld\n", *f_pos);
mutex_unlock(&pcd_lock);
/* Return number of bytes which have been successfully read */
return count;
}
ssize_t pcd_write(struct file* p_file, const char __user* buff, size_t count, loff_t* f_pos)
{
if(mutex_lock_interruptible(&pcd_lock)){
return -EINTR;
}
pr_info("write requested for %zu bytes\n", count);
pr_info("current file position = %lld\n", *f_pos);
/* Adjust the count */
if((*f_pos + count) > DEV_MEM_SIZE)
count = DEV_MEM_SIZE - *f_pos;
if(!count){
pr_err("No space left on the device\n");
return -ENOMEM;
}
/* Copy from user */
if(copy_from_user(&device_buffer[*f_pos], buff, count))
return -EFAULT;
/* Update the current file position */
*f_pos += count;
pr_info("number of bytes successfully written = %zu\n", count);
pr_info("updated file position = %lld\n", *f_pos);
mutex_unlock(&pcd_lock);
/* Return number of bytes which have been successfully written */
return count;
}
int pcd_open(struct inode* inode, struct file* p_file)
{
pr_info("open was successful\n");
return 0;
}
int pcd_release(struct inode* inode, struct file* p_file){
pr_info("release was successful\n");
return 0;
}
/* File operations of the driver */
struct file_operations pcd_fops = {.open = pcd_open,
.write = pcd_write,
.read = pcd_read,
.llseek = pcd_lseek,
.release = pcd_release,
.owner = THIS_MODULE};
struct class* class_pcd;
struct device* device_pcd;
static int __init pcd_driver_init(void){
int ret;
/* Dynamically allocate a device number */
ret = alloc_chrdev_region(&device_number, 0, 1, "pcd_devices");
if(ret < 0){
pr_err("Alloc chrdev failed\n");
goto out;
}
pr_info("Device number <major>:<minor> = %d:%d\n", MAJOR(device_number), MINOR(device_number));
/* Initialize the cdev structure with fops */
cdev_init(&pcd_cdev, &pcd_fops);
/* Register a device (cdev structure) with VFS */
pcd_cdev.owner = THIS_MODULE;
ret = cdev_add(&pcd_cdev, device_number, 1);
if(ret < 0){
pr_err("cdev add failed\n");
goto unreg_chrdev;
}
/* Create device class under /sys/class/ */
class_pcd = class_create(THIS_MODULE, "pcd_class");
/* Check if the pointer is erroneus */
if(IS_ERR(class_pcd)){
pr_err("Class creation failed\n");
/* Convert pointer to error code */
ret = PTR_ERR(class_pcd);
goto cdev_del;
}
/* Populate the sysfs with device information */
device_pcd = device_create(class_pcd, NULL, device_number, NULL, "pcd");
if(IS_ERR(device_pcd)){
pr_err("Device create failed\n");
ret = PTR_ERR(device_pcd);
goto class_del;
}
pr_info("Module init was successful");
return 0;
class_del:
class_destroy(class_pcd);
cdev_del:
cdev_del(&pcd_cdev);
unreg_chrdev:
unregister_chrdev_region(device_number, 1);
out:
pr_info("Module insertion failed\n");
return ret;
}
static void __exit pcd_driver_cleanup(void){
device_destroy(class_pcd, device_number);
class_destroy(class_pcd);
cdev_del(&pcd_cdev);
unregister_chrdev_region(device_number, 1);
pr_info("module unloaded\n");
}
module_init(pcd_driver_init);
module_exit(pcd_driver_cleanup);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("maherme");
MODULE_DESCRIPTION("A pseudo character driver");