commit | author | age
|
b8e5f6
|
1 |
/* |
G |
2 |
* Copyright (C) 2024 LingYun IoT System Studio |
|
3 |
* Author: Guo Wenxue <guowenxue@gmail.com> |
|
4 |
* |
|
5 |
* A character skeleton driver example in linux kernel. |
|
6 |
*/ |
f5c330
|
7 |
|
G |
8 |
#include <linux/module.h> |
|
9 |
#include <linux/init.h> |
|
10 |
#include <linux/kernel.h> /* printk() */ |
|
11 |
#include <linux/fs.h> /* everything... */ |
|
12 |
#include <linux/errno.h> /* error codes */ |
|
13 |
#include <linux/types.h> /* size_t */ |
|
14 |
#include <linux/cdev.h> /* cdev */ |
b8e5f6
|
15 |
#include <linux/slab.h> /* kmalloc() */ |
f5c330
|
16 |
#include <linux/version.h> /* kernel version code */ |
b8e5f6
|
17 |
#include <linux/uaccess.h> /* copy_from/to_user() */ |
f5c330
|
18 |
#include <linux/moduleparam.h> |
G |
19 |
|
|
20 |
/* device name and major number */ |
|
21 |
#define DEV_NAME "chrdev" |
b8e5f6
|
22 |
int dev_major = 0; |
f5c330
|
23 |
module_param(dev_major, int, S_IRUGO); |
G |
24 |
|
b8e5f6
|
25 |
#define BUF_SIZE 1024 |
f5c330
|
26 |
typedef struct chrdev_s |
G |
27 |
{ |
|
28 |
struct cdev cdev; |
|
29 |
char *data; /* data buffer */ |
|
30 |
uint32_t size; /* data buffer size */ |
|
31 |
uint32_t bytes; /* data bytes in the buffer */ |
|
32 |
} chrdev_t; |
|
33 |
|
|
34 |
static struct chrdev_s dev; |
|
35 |
|
|
36 |
static ssize_t chrdev_read (struct file *file, char __user *buf, size_t count, loff_t *f_pos) |
|
37 |
{ |
|
38 |
struct chrdev_s *dev = file->private_data; |
|
39 |
ssize_t nbytes; |
|
40 |
ssize_t rv = 0; |
|
41 |
|
|
42 |
/* no data in buffer */ |
|
43 |
if( !dev->bytes ) |
|
44 |
return 0; |
|
45 |
|
|
46 |
/* copy data to user space */ |
|
47 |
nbytes = count>dev->bytes ? dev->bytes : count; |
|
48 |
if( copy_to_user(buf, dev->data, nbytes) ) |
|
49 |
{ |
|
50 |
rv = -EFAULT; |
|
51 |
goto out; |
|
52 |
} |
|
53 |
|
|
54 |
/* update return value and data bytes in buffer */ |
|
55 |
rv = nbytes; |
|
56 |
dev->bytes -= nbytes; |
|
57 |
|
|
58 |
out: |
|
59 |
return rv; |
|
60 |
} |
|
61 |
|
|
62 |
static ssize_t chrdev_write (struct file *file, const char __user *buf, size_t count, loff_t *f_pos) |
|
63 |
{ |
|
64 |
struct chrdev_s *dev = file->private_data; |
|
65 |
ssize_t nbytes; |
|
66 |
ssize_t rv = 0; |
|
67 |
|
|
68 |
/* no space left */ |
|
69 |
if( dev->bytes >= dev->size ) |
|
70 |
return -ENOSPC; |
|
71 |
|
|
72 |
/* check copy data bytes */ |
|
73 |
if( dev->size - dev->bytes < count) |
|
74 |
nbytes = dev->size - dev->bytes; |
|
75 |
else |
|
76 |
nbytes = count; |
|
77 |
|
|
78 |
/* copy data from user space */ |
|
79 |
if( copy_from_user(&dev->data[dev->bytes], buf, nbytes) ) |
|
80 |
{ |
|
81 |
rv = -EFAULT; |
|
82 |
goto out; |
|
83 |
} |
|
84 |
|
|
85 |
/* update return value and data bytes in buffer */ |
|
86 |
rv = nbytes; |
|
87 |
dev->bytes += nbytes; |
|
88 |
|
|
89 |
out: |
|
90 |
return rv; |
|
91 |
} |
|
92 |
|
b8e5f6
|
93 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,0,0) |
G |
94 |
#define access_ok_wrapper(type,arg,cmd) access_ok(type, arg, cmd) |
|
95 |
#else |
|
96 |
#define access_ok_wrapper(type,arg,cmd) access_ok(arg, cmd) |
|
97 |
#endif |
|
98 |
|
|
99 |
/* ioctl definitions, use 'c' as magic number */ |
|
100 |
#define CHR_MAGIC 'c' |
|
101 |
#define CHR_MAXNR 2 |
|
102 |
#define CMD_READ _IOR(CHR_MAGIC, 0, int) |
|
103 |
#define CMD_WRITE _IOW(CHR_MAGIC, 1, int) |
|
104 |
|
|
105 |
static long chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
f5c330
|
106 |
{ |
b8e5f6
|
107 |
static int value = 0xdeadbeef; |
G |
108 |
int rv = 0; |
f5c330
|
109 |
|
G |
110 |
/* |
|
111 |
* extract the type and number bitfields, and don't decode |
|
112 |
* wrong cmds: return ENOTTY (inappropriate ioctl) before access_ok() |
|
113 |
*/ |
b8e5f6
|
114 |
if (_IOC_TYPE(cmd) != CHR_MAGIC) return -ENOTTY; |
G |
115 |
if (_IOC_NR(cmd) > CHR_MAXNR) return -ENOTTY; |
f5c330
|
116 |
|
G |
117 |
/* |
|
118 |
* the direction is a bitmask, and VERIFY_WRITE catches R/W transfers. |
|
119 |
* `Type' is user-oriented, while access_ok is kernel-oriented, |
|
120 |
* so the concept of "read" and "write" is reversed |
|
121 |
*/ |
|
122 |
if (_IOC_DIR(cmd) & _IOC_READ) |
|
123 |
rv = !access_ok_wrapper(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd)); |
|
124 |
else if (_IOC_DIR(cmd) & _IOC_WRITE) |
|
125 |
rv = !access_ok_wrapper(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd)); |
|
126 |
|
b8e5f6
|
127 |
if (rv) |
G |
128 |
return -EFAULT; |
|
129 |
|
|
130 |
switch (cmd) { |
|
131 |
case CMD_READ: |
|
132 |
if (copy_to_user((int __user *)arg, &value, sizeof(value))) |
|
133 |
return -EFAULT; |
f5c330
|
134 |
break; |
G |
135 |
|
b8e5f6
|
136 |
case CMD_WRITE: |
G |
137 |
if (copy_from_user(&value, (int __user *)arg, sizeof(value))) |
|
138 |
return -EFAULT; |
f5c330
|
139 |
break; |
G |
140 |
|
|
141 |
default: |
b8e5f6
|
142 |
return -EINVAL; |
f5c330
|
143 |
} |
G |
144 |
|
b8e5f6
|
145 |
return 0; |
f5c330
|
146 |
} |
G |
147 |
|
|
148 |
static int chrdev_open (struct inode *inode, struct file *file) |
|
149 |
{ |
|
150 |
struct chrdev_s *dev; /* device struct address */ |
|
151 |
|
|
152 |
/* get the device struct address by container_of() */ |
|
153 |
dev = container_of(inode->i_cdev, struct chrdev_s, cdev); |
|
154 |
|
|
155 |
/* save the device struct address for other methods */ |
|
156 |
file->private_data = dev; |
|
157 |
|
|
158 |
return 0; |
|
159 |
} |
|
160 |
|
|
161 |
static int chrdev_close (struct inode *node, struct file *file) |
|
162 |
{ |
|
163 |
return 0; |
|
164 |
} |
|
165 |
|
|
166 |
static struct file_operations chrdev_fops = { |
b8e5f6
|
167 |
.owner = THIS_MODULE, |
G |
168 |
.open = chrdev_open, /* open() implementation */ |
|
169 |
.read = chrdev_read, /* read() implementation */ |
|
170 |
.write = chrdev_write, /* write() implementation */ |
|
171 |
.unlocked_ioctl = chrdev_ioctl, /* ioctl() implementation */ |
|
172 |
.release = chrdev_close, /* close() implementation */ |
f5c330
|
173 |
}; |
G |
174 |
|
|
175 |
static int __init chrdev_init(void) |
|
176 |
{ |
|
177 |
dev_t devno; |
|
178 |
int rv; |
|
179 |
|
|
180 |
/* malloc and initial device read/write buffer */ |
b8e5f6
|
181 |
dev.data = kmalloc(BUF_SIZE, GFP_KERNEL); |
f5c330
|
182 |
if( !dev.data ) |
G |
183 |
{ |
|
184 |
printk(KERN_ERR " %s driver kmalloc() failed\n", DEV_NAME); |
|
185 |
return -ENOMEM; |
|
186 |
} |
b8e5f6
|
187 |
dev.size = BUF_SIZE; |
f5c330
|
188 |
dev.bytes = 0; |
G |
189 |
memset(dev.data, 0, dev.size); |
|
190 |
|
b8e5f6
|
191 |
/* allocate device number */ |
f5c330
|
192 |
if(0 != dev_major) |
G |
193 |
{ |
|
194 |
devno = MKDEV(dev_major, 0); |
|
195 |
rv = register_chrdev_region(devno, 1, DEV_NAME); |
|
196 |
} |
|
197 |
else |
|
198 |
{ |
|
199 |
rv = alloc_chrdev_region(&devno, 0, 1, DEV_NAME); |
|
200 |
dev_major = MAJOR(devno); |
|
201 |
} |
|
202 |
|
|
203 |
if(rv < 0) |
|
204 |
{ |
|
205 |
printk(KERN_ERR "%s driver can't use major %d\n", DEV_NAME, dev_major); |
|
206 |
return -ENODEV; |
|
207 |
} |
|
208 |
|
b8e5f6
|
209 |
/* initialize cdev and setup fops */ |
f5c330
|
210 |
cdev_init(&dev.cdev, &chrdev_fops); |
G |
211 |
dev.cdev.owner = THIS_MODULE; |
b8e5f6
|
212 |
|
G |
213 |
/* register cdev to linux kernel */ |
f5c330
|
214 |
rv = cdev_add(&dev.cdev, devno, 1); |
G |
215 |
if( rv ) |
|
216 |
{ |
|
217 |
rv = -ENODEV; |
|
218 |
printk(KERN_ERR "%s driver regist failed, rv=%d\n", DEV_NAME, rv); |
|
219 |
goto failed1; |
|
220 |
} |
|
221 |
|
|
222 |
printk(KERN_INFO "%s driver on major[%d] installed.\n", DEV_NAME, dev_major); |
|
223 |
return 0; |
|
224 |
|
|
225 |
failed1: |
|
226 |
unregister_chrdev_region(devno, 1); |
|
227 |
|
|
228 |
printk(KERN_ERR "%s driver installed failed.\n", DEV_NAME); |
|
229 |
return rv; |
|
230 |
} |
|
231 |
|
|
232 |
static void __exit chrdev_exit(void) |
|
233 |
{ |
|
234 |
cdev_del(&dev.cdev); |
|
235 |
unregister_chrdev_region(MKDEV(dev_major,0), 1); |
|
236 |
|
|
237 |
kfree(dev.data); |
|
238 |
|
|
239 |
printk(KERN_INFO "%s driver removed!\n", DEV_NAME); |
|
240 |
return; |
|
241 |
} |
|
242 |
|
|
243 |
module_init(chrdev_init); |
|
244 |
module_exit(chrdev_exit); |
|
245 |
|
|
246 |
MODULE_LICENSE("Dual BSD/GPL"); |
|
247 |
MODULE_AUTHOR("GuoWenxue <guowenxue@gmail.com>"); |