-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathioctl.c
More file actions
70 lines (59 loc) · 1.58 KB
/
ioctl.c
File metadata and controls
70 lines (59 loc) · 1.58 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
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#define MY_MACIG 'G'
#define READ_IOCTL _IOR(MY_MACIG, 0, int)
#define WRITE_IOCTL _IOW(MY_MACIG, 1, int)
static int major;
static char msg[200];
static ssize_t device_read(struct file *filp, char __user *buffer, size_t length, loff_t *offset)
{
return simple_read_from_buffer(buffer, length, offset, msg, 200);
}
static ssize_t device_write(struct file *filp, const char __user *buff, size_t len, loff_t *off)
{
if (len > 199)
return -EINVAL;
copy_from_user(msg, buff, len);
msg[len] = '\0';
return len;
}
char buf[200];
int device_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg) {
int len = 200;
switch(cmd) {
case READ_IOCTL:
copy_to_user((char *)arg, buf, 200);
break;
case WRITE_IOCTL:
copy_from_user(buf, (char *)arg, len);
break;
default:
return -ENOTTY;
}
return len;
}
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.ioctl = device_ioctl,
};
static int __init cdevexample_module_init(void)
{
major = register_chrdev(0, "my_device", &fops);
if (major < 0) {
printk ("Registering the character device failed with %d\n", major);
return major;
}
printk("cdev example: assigned major: %d\n", major);
printk("create node with mknod /dev/cdev_example c %d 0\n", major);
return 0;
}
static void __exit cdevexample_module_exit(void)
{
unregister_chrdev(major, "my_device");
}
module_init(cdevexample_module_init);
module_exit(cdevexample_module_exit);
MODULE_LICENSE("GPL");