-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcd_n.c
351 lines (274 loc) · 8.51 KB
/
pcd_n.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#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>
#undef pr_fmt
#define pr_fmt(fmt) "%s :" fmt,__func__
#define NO_OF_DEVICES 4
#define MEM_SIZE_MAX_PCDEV1 1024
#define MEM_SIZE_MAX_PCDEV2 512
#define MEM_SIZE_MAX_PCDEV3 1024
#define MEM_SIZE_MAX_PCDEV4 512
/* pseudo device's memory */
char device_buffer_pcdev1[MEM_SIZE_MAX_PCDEV1];
char device_buffer_pcdev2[MEM_SIZE_MAX_PCDEV2];
char device_buffer_pcdev3[MEM_SIZE_MAX_PCDEV3];
char device_buffer_pcdev4[MEM_SIZE_MAX_PCDEV4];
/* Device priavte data structure */
struct pcdev_private_data
{
char *buffer;
unsigned size;
const char *serial_number;
int perm;
struct cdev cdev;
};
/* Driver private data structure */
struct pcdrv_private_data
{
int total_devices;
/* this holds the device number */
dev_t device_number;
struct class *class_pcd;
struct device *device_pcd;
struct pcdev_private_data pcdev_data[NO_OF_DEVICES];
};
#define RDONLY 0X01
#define WRONLY 0X10
#define RDWR 0X11
struct pcdrv_private_data pcdrv_data =
{
.total_devices = NO_OF_DEVICES,
.pcdev_data = {
[0] = {
.buffer = device_buffer_pcdev1,
.size = MEM_SIZE_MAX_PCDEV1,
.serial_number = "PCDEV1XYZ123",
.perm = RDONLY /* RDONLY */
},
[1] = {
.buffer = device_buffer_pcdev2,
.size = MEM_SIZE_MAX_PCDEV2,
.serial_number = "PCDEV2XYZ123",
.perm = WRONLY /* WRONLY */
},
[2] = {
.buffer = device_buffer_pcdev3,
.size = MEM_SIZE_MAX_PCDEV3,
.serial_number = "PCDEV3XYZ123",
.perm = RDWR /* RDWR */
},
[3] = {
.buffer = device_buffer_pcdev4,
.size = MEM_SIZE_MAX_PCDEV4,
.serial_number = "PCDEV4XYZ123",
.perm = RDWR /* RDWR */
}
}
};
loff_t pcd_lseek(struct file *filp, loff_t offset, int whence);
ssize_t pcd_read(struct file *filp, char __user *buff, size_t count, loff_t *f_pos);
ssize_t pcd_write(struct file *filp, const char __user *buff, size_t count, loff_t *f_pos);
int pcd_open(struct inode *inode, struct file *filp);
int pcd_release(struct inode *inode, struct file *filp);
int check_permission(int dev_perm, int access_mode);
loff_t pcd_lseek(struct file *filp, loff_t offset, int whence)
{
struct pcdev_private_data *pcdev_data = (struct pcdev_private_data*)filp->private_data;
int max_size = pcdev_data->size;
loff_t temp;
pr_info("lseek requested \n");
pr_info("Current value of file postion = %lld \n", filp->f_pos);
switch(whence)
{
case SEEK_SET:
if( (offset > max_size) || (offset < 0))
return -EINVAL;
filp->f_pos = offset;
break;
case SEEK_CUR:
temp = filp->f_pos + offset;
if( (temp > max_size) || (temp < 0) )
return -EINVAL;
filp->f_pos = temp;
break;
case SEEK_END:
temp = max_size + offset;
if( (temp > max_size) || (temp < 0) )
return -EINVAL;
filp->f_pos = temp;
break;
default:
return - EINVAL;
}
pr_info("New value of the file postion = %lld \n ", filp->f_pos);
return filp->f_pos;
}
ssize_t pcd_read(struct file *filp, char __user *buff, size_t count, loff_t *f_pos)
{
struct pcdev_private_data *pcdev_data = (struct pcdev_private_data*)filp->private_data;
int max_size = pcdev_data->size;
pr_info("read requested for %zu bytes \n",count);
pr_info("current file position = %lld",*f_pos);
/* Adjust the 'count' */
if((*f_pos + count) > max_size)
count = max_size - *f_pos;
/* copy to user */
if(copy_to_user(buff,pcdev_data->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);
/* return number of bytes which have been successfully read*/
return count;
}
ssize_t pcd_write(struct file *filp, const char __user *buff, size_t count, loff_t *f_pos)
{
struct pcdev_private_data *pcdev_data = (struct pcdev_private_data*)filp->private_data;
int max_size = pcdev_data->size;
pr_info("write requested for %zu bytes \n",count);
pr_info("current file position = %lld",*f_pos);
/* Adjust the 'count' */
if((*f_pos + count) > max_size)
count = max_size - *f_pos;
if(!count){
pr_err("No space left on the device \n");
return -ENOMEM;
}
/* copy from user */
if(copy_from_user(pcdev_data->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);
/* return number of bytes which have been successfully written */
return count;
}
int check_permission(int dev_perm, int access_mode)
{
if(dev_perm == RDWR)
return 0;
/* ensures readonly access*/
if ((dev_perm == RDONLY) & ((access_mode & FMODE_READ) && !(access_mode & FMODE_WRITE)))
return 0;
/* ensures writeonly access */
if ((dev_perm == WRONLY) & ((access_mode & FMODE_WRITE) && !(access_mode & FMODE_READ)))
return 0;
return -EPERM;
}
int pcd_open(struct inode *inode, struct file *filp)
{
int ret;
int minor_n;
struct pcdev_private_data *pcdev_data;
/* find out ton which device file open was attempted by the user */
minor_n = MINOR(inode->i_rdev);
pr_info("minor access = %d\n", minor_n);
/* get devices's private data struture */
pcdev_data = container_of(inode->i_cdev, struct pcdev_private_data, cdev);
/* to supply device private data to other methods of the driver */
filp->private_data = pcdev_data;
/* check permission */
pr_info(" perm = %d , access_mode = %d ", pcdev_data->perm,filp->f_mode);
ret = check_permission(pcdev_data->perm,filp->f_mode);
(!ret)?pr_info("open was successful \n"):pr_info("open was unsucessful \n");
return ret;
}
int pcd_release(struct inode *inode, struct file *filp)
{
pr_info("close 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
};
static int __init pcd_driver_init(void)
{
int ret;
int i;
/* Dynamically allocate a device number */
ret = alloc_chrdev_region(&pcdrv_data.device_number,0,NO_OF_DEVICES,"pcd_devices");
if(ret < 0)
{
pr_err("Alloc chrdev failed \n");
goto out;
}
/* create device class under /sys/class/ */
pcdrv_data.class_pcd = class_create("pcd_class");
if(IS_ERR(pcdrv_data.class_pcd))
{
pr_err("Class creation failed \n");
ret = PTR_ERR(pcdrv_data.class_pcd);
goto unreg_chrdev;
}
for(i=0;i<NO_OF_DEVICES;i++)
{
pr_info("Device number <major>:<minor> = %d:%d\n",MAJOR(pcdrv_data.device_number+i),MINOR(pcdrv_data.device_number+i));
/* Initialize the cdev structure with fops */
cdev_init(&pcdrv_data.pcdev_data[i].cdev,&pcd_fops);
/* Register cdev structure with the VFS */
pcdrv_data.pcdev_data[i].cdev.owner = THIS_MODULE;
ret = cdev_add(&pcdrv_data.pcdev_data[i].cdev,pcdrv_data.device_number+i,1);
if(ret < 0)
{
pr_err("Cdev add failed \n");
goto cdev_del;
}
/* populate the sysfs with device information*/
pcdrv_data.device_pcd = device_create(pcdrv_data.class_pcd, NULL, pcdrv_data.device_number+i, NULL, "pcdev-%d",i+1);
if(IS_ERR(pcdrv_data.device_pcd))
{
pr_err("Device create failed \n");
ret = PTR_ERR(pcdrv_data.device_pcd);
goto class_del;
}
pr_info("Device creation complete \n");
}
pr_info("Out of for loop \n");
pr_info("Module init was successful \n ");
pr_info(" Right before return \n");
return 0;
cdev_del:
class_del:
for(;i>=0;i--)
{
device_destroy(pcdrv_data.class_pcd,pcdrv_data.device_number+i);
cdev_del(&pcdrv_data.pcdev_data[i].cdev);
}
class_destroy(pcdrv_data.class_pcd);
unreg_chrdev:
unregister_chrdev_region(pcdrv_data.device_number,NO_OF_DEVICES);
out:
pr_info("Module Insertion failed \n");
return ret;
}
static void __exit pcd_driver_cleanup(void)
{
int i;
for(i=0;i<NO_OF_DEVICES;i++)
{
device_destroy(pcdrv_data.class_pcd,pcdrv_data.device_number+i);
cdev_del(&pcdrv_data.pcdev_data[i].cdev);
}
class_destroy(pcdrv_data.class_pcd);
unregister_chrdev_region(pcdrv_data.device_number,NO_OF_DEVICES);
pr_info("module unloaded \n");
}
module_init(pcd_driver_init);
module_exit(pcd_driver_cleanup);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("KARAN");
MODULE_DESCRIPTION("A pseudo character driver which handles n devices");