-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofd.c
126 lines (107 loc) · 2.88 KB
/
ofd.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
/*
* ofd.c - A hello world driver
*/
#include <linux/kernel.h> /* printk defn */
#include <linux/version.h>
#include <linux/module.h>
#include <linux/types.h> /* dev_t defn */
#include <linux/kdev_t.h> /* MAJOR, MINOR macro defns */
#include <linux/fs.h> /* alloc_chrdev_region defn */
#include <linux/device.h>
#include <linux/cdev.h> /* cdev_add and cdev_init */
#include <linux/uaccess.h> /* copy_to_user and copy_from_user */
//#include "/home/lym/kernel_src/devel/tools/lib/lockdep/uinclude/linux/kern_levels.h" /* defines the kernel log-levels */
static dev_t first; /* Global var. for first dev number */
static struct cdev c_dev; /* Global variable for the char device structure */
static struct class *cl; /* Global variable for the device class */
/*
* Open and Close
*/
static int ofd_open(struct inode *i, struct file *filp)
{
printk(KERN_INFO "Driver: open()\n");
return 0;
}
static int ofd_close(struct inode *i, struct file *filp)
{
printk(KERN_INFO "Driver: close()\n");
return 0;
}
/*
* Data Management
*/
static char c;
static ssize_t ofd_read(struct file *filp, char __user *buf, size_t len,
loff_t *off)
{
printk(KERN_INFO "Driver: read()\n");
if (*off == 0) {
if (copy_to_user(buf, &c, 1) != 0)
return -EFAULT;
else {
(*off)++;
return 1;
}
}
else {
return 0;
}
}
static ssize_t ofd_write(struct file *filp, const char __user *buf,
size_t len, loff_t *off)
{
printk(KERN_INFO "Driver: write()\n");
if (copy_from_user(&c, buf + (len - 1), 1) != 0)
return -EFAULT;
else
printk("%s", &c);
return len;
}
/*
* Add the device-specific file operations to the file_operations structure
*/
static struct file_operations ofd_fops = {
.owner = THIS_MODULE,
.open = ofd_open,
.release = ofd_close,
.read = ofd_read,
.write = ofd_write
};
static int __init ofd_init(void) /* constructor */
{
printk(KERN_INFO "Bonjour! ofd registred");
if (alloc_chrdev_region(&first, 0, 1, "trivial_dev") < 0)
return -1;
if ((cl = class_create(THIS_MODULE, "chardrv")) == NULL) {
unregister_chrdev_region(first, 1);
return -1;
}
if (device_create(cl, NULL, first, NULL, "mynull") == NULL) {
class_destroy(cl);
unregister_chrdev_region(first, 1);
return -1;
}
/* initialize the char device structure */
cdev_init(&c_dev, &ofd_fops);
if (cdev_add(&c_dev, first, 1) == -1) {
device_destroy(cl, first);
class_destroy(cl);
unregister_chrdev_region(first, 1);
return -1;
}
//printk(KERN_INFO "<Major, Minor>: <%d, %d>\n", MAJOR(first), MINOR(first));
return 0;
}
static void __exit ofd_exit(void) /* destructor */
{
cdev_del(&c_dev);
device_destroy(cl, first);
class_destroy(cl);
unregister_chrdev_region(first, 1);
printk(KERN_INFO "Au revour! ofd unregistered");
}
module_init(ofd_init);
module_exit(ofd_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Salym Senyonga <[email protected]>");
MODULE_DESCRIPTION("Hello world Driver");