Repository navigation
Expand file tree
/
Copy pathexample.c
More file actions
174 lines (145 loc) · 4.62 KB
/
Copy pathexample.c
File metadata and controls
174 lines (145 loc) · 4.62 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
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <asm/uaccess.h>
#include <linux/string.h>
#include <linux/errno.h>
MODULE_LICENSE("Dual BSD/GPL");
static char data[] = "0123456789\r\n";
/* The lock for device data. */
static rwlock_t lock;
static int example_open(struct inode *inode, struct file *filp) {
printk(KERN_DEBUG "EXAMPLE: open\n");
/* Initial lock. */
rwlock_init(&lock);
/* Map the data location to the file data pointer. */
filp->private_data = data;
return 0;
}
static int example_close(struct inode *inode, struct file *filp) {
printk(KERN_DEBUG "EXAMPLE: close\n");
/* Release the mapping of file data address. */
if(filp->private_data) {
filp->private_data = NULL;
}
return 0;
}
static ssize_t example_read(struct file *filp, char __user *buf, size_t size, loff_t *f_pos) {
size_t count;
uint8_t byte;
uint8_t *data_p;
printk(KERN_DEBUG "EXAMPLE: read (size=%zu)\n", size);
data_p = filp->private_data;
/* Get the lock for reading. */
read_lock(&lock);
/* Read from the device data to user space. */
for(count = 0; (count < size) && (*f_pos) < strlen(data); ++(*f_pos), ++count) {
byte = data_p[*f_pos];
if(copy_to_user(buf + count, &byte, 1) != 0) {
break;
}
printk(KERN_DEBUG "EXAMPLE: read (buf[%zu]=%02x)\n", count, (unsigned)byte);
}
/* Release the lock for reading. */
read_unlock(&lock);
return count;
}
static ssize_t example_write(struct file *filp, const char __user *buf, size_t size, loff_t *f_pos) {
size_t count;
ssize_t ret;
uint8_t byte;
uint8_t *data_p;
printk(KERN_DEBUG "EXAMPLE: write (size=%zu)\n", size);
data_p = filp->private_data;
/* Get the lock for writing. */
write_lock(&lock);
/* Write from user space to the device. */
for(count = 0; (count < size) && (*f_pos) < strlen(data); ++(*f_pos), ++count) {
if(copy_from_user(&byte, buf + count, 1) != 0) {
break;
}
data_p[*f_pos] = byte;
printk(KERN_DEBUG "EXAMPLE: write (buf[%zu]=%02x)\n", count, (unsigned)byte);
}
/* Release the lock for writing. */
write_unlock(&lock);
if((count == 0) && ((*f_pos) >= strlen(data))) {
ret = -ENOBUFS;
}
else {
ret = count;
}
return ret;
}
static struct file_operations example_fops = {
.open = example_open,
.release = example_close,
.read = example_read,
.write = example_write,
};
#define EXAMPLE_NAME "example"
static unsigned int example_major;
static unsigned int example_devs = 2;
static struct cdev example_cdev;
static struct class *example_sys_class = NULL;
static int example_init(void) {
dev_t dev;
int alloc_ret, cdev_err;
printk(KERN_DEBUG "EXAMPLE: init\n");
/* Allocate a character device. */
alloc_ret = alloc_chrdev_region(&dev, 0, example_devs, EXAMPLE_NAME);
if(alloc_ret) {
printk(KERN_DEBUG "EXAMPLE: Failed to allocate a character device\n");
return -1;
}
/* Initial the character device ddriver. */
example_major = MAJOR(dev);
cdev_init(&example_cdev, &example_fops);
example_cdev.owner = THIS_MODULE;
/* Add the character device driver into system. */
dev = MKDEV(example_major, 0);
cdev_err = cdev_add(&example_cdev, dev, example_devs);
if(cdev_err) {
printk(KERN_DEBUG "EXAMPLE: Failed to register a character device\n");
/* Release the allocated character device. */
if(alloc_ret == 0) {
unregister_chrdev_region(dev, example_devs);
}
return -1;
}
printk(KERN_DEBUG "EXAMPLE: %s driver(major %d) installed.\n", EXAMPLE_NAME, example_major);
/* Create device class. */
example_sys_class = class_create(THIS_MODULE, EXAMPLE_NAME);
if(IS_ERR(example_sys_class)) {
printk(KERN_DEBUG "EXAMPLE: Failed to create a class of device.\n");
/* Release the added character device. */
if(cdev_err == 0)
cdev_del(&example_cdev);
/* Release the allocated character device. */
if(alloc_ret == 0)
unregister_chrdev_region(dev, example_devs);
return -1;
}
printk(KERN_DEBUG "EXAMPLE: %s class created.\n", EXAMPLE_NAME);
/* Create device node. */
device_create(example_sys_class, NULL, dev, NULL, EXAMPLE_NAME);
printk(KERN_DEBUG "EXAMPLE: %s device node created.\n", EXAMPLE_NAME);
return 0;
}
static void example_exit(void) {
dev_t dev = MKDEV(example_major, 0);
printk(KERN_DEBUG "EXAMPLE: exit\n");
/* Destory device nodes. */
device_destroy(example_sys_class, dev);
/* Delete device class. */
class_destroy(example_sys_class);
/* Delete the character device driver from system. */
cdev_del(&example_cdev);
/* Unregister the allocated character device. */
unregister_chrdev_region(dev, example_devs);
printk(KERN_DEBUG "EXAMPLE: %s driver removed.\n", EXAMPLE_NAME);
}
module_init(example_init);
module_exit(example_exit);