-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.c
More file actions
67 lines (44 loc) · 1.46 KB
/
device.c
File metadata and controls
67 lines (44 loc) · 1.46 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
#include "device.h"
static int device_open(struct inode *inode, struct file *file)
{
int dev_id=MINOR(file->f_path.dentry->d_inode->i_rdev);
if (Device_Open_Flag[dev_id])
return -EBUSY;
Device_Open_Flag[dev_id]++;
Message_Ptr[dev_id] = Message[dev_id];
try_module_get(THIS_MODULE);
return SUCCESS;
}
static int device_release(struct inode *inode, struct file *file)
{
int dev_id=MINOR(file->f_path.dentry->d_inode->i_rdev);
Device_Open_Flag[dev_id]--;
module_put(THIS_MODULE);
return SUCCESS;
}
static ssize_t device_read(struct file *file,
char __user *buffer, size_t length, loff_t *offset)
{
int dev_id=MINOR(file->f_path.dentry->d_inode->i_rdev);
int bytes_read = 0;
if (*Message_Ptr[dev_id] == 0)
return 0;
while (length && *Message_Ptr[dev_id]) {
put_user(*(Message_Ptr[dev_id]++), buffer++);
length--;
bytes_read++;
}
printk("Information was read from the device %s", DEVICE_NAME[dev_id]);
return bytes_read;
}
static ssize_t device_write(struct file *file,
const char __user * buffer, size_t length, loff_t * offset)
{
int dev_id=MINOR(file->f_path.dentry->d_inode->i_rdev);
int i;
for (i = 0; i < length && i < BUF_LEN; i++)
get_user(Message[dev_id][i], buffer + i);
Message_Ptr[dev_id] = Message[dev_id];
printk("Device %s got new information", DEVICE_NAME[dev_id]);
return i;
}