一个非常适合参考的字符驱动模板
【摘要】 #include <linux/module.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/kernel.h>
#in...
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include "gpio.h" /* */
struct gpio_cdev
{
struct cdev cdev;
int count;
struct class *gpio_class;
struct device *gpio_device;
};
/* 用户进程利用在对设备文件进行诸如read/write操作的时候,系统调用通过设备文件的主设备号找到相应的设备驱动程序,然后读取这个数据结构相应的函数指针,接着把控制权交给该函数,这是Linux的设备驱动程序工作的基本原理。
*/ /* 打开 */
int gpio_open(struct inode * inode , struct file * filp )
{
printk("63-->[%s] %s %d : gpio_open\n", __FILE__, __func__, __LINE__);
return 0;
}
/* 关闭 */
int gpio_release(struct inode *inode, struct file *filp)
{
printk("63-->[%s] %s %d : gpio_release\n", __FILE__, __func__, __LINE__);
return 0;
}
/*读设备*/
ssize_t gpio_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
//包含头文件#include <linux/string.h> ,可使用内核字符串处理函数
// char kernel_buf[64] = {"this is kernel space data"};
char kernel_buf[64] = {0}; memset(kernel_buf, 0, sizeof(kernel_buf));
strncpy(kernel_buf, "63-->this is kernel space data", sizeof(kernel_buf));
kernel_buf[63] = '\0'; if (copy_to_user(buf, kernel_buf, strlen(kernel_buf)))
{
//copy_to_user失败返回未写入的字节数
printk("63-->[%s] %s %d : gpio_get failed!\n", __FILE__, __func__, __LINE__);
return -EFAULT;
}
else
{
//copy_to_user成功返回0
printk("63-->[%s] %s %d : kernel_buf = %s\n", __FILE__, __func__, __LINE__, kernel_buf);
} return 0;
} /*写设备*/
ssize_t gpio_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
{ //copy_from_user(void *to, const void __user *from, unsigned long count);
char kernel_buf[64] = {0}; if (copy_from_user(kernel_buf, buf, sizeof(kernel_buf)))
{
printk("63-->[%s] %s %d : gpio_set failed!\n", __FILE__, __func__, __LINE__);
return -EFAULT;
}
printk("63-->[%s] %s %d : kernel_buf = %s\n", __FILE__, __func__, __LINE__, kernel_buf); return 0;
} /*ioctl函数*/
// static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
static long gpio_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg)
{
static gpio_opt gpio;
int n = 0; if (copy_from_user(&gpio, (gpio_opt *)arg, sizeof(gpio_opt)))
return -EFAULT; switch(cmd){
case IOCTL_GPIO_GET: gpio.gpio_status = 2; n = copy_to_user((gpio_opt *)arg, &gpio, sizeof(gpio_opt)); if (n == 0) { printk("63-->[%s] %s %d : gpio.gpio_num = %d gpio.gpio_status = %d\n", __FILE__, __func__, __LINE__, gpio.gpio_num, gpio.gpio_status); } else { printk("63-->[%s] %s %d : gpio_get failed!\n", __FILE__, __func__, __LINE__); return -EFAULT; } break;
case IOCTL_GPIO_SET: if((gpio.gpio_num == 111) && (gpio.gpio_status == 1))//匹配条件也可修改 { //可添加set操作 printk("63-->[%s] %s %d : gpio.gpio_num = %d gpio.gpio_status = %d\n", __FILE__, __func__, __LINE__, gpio.gpio_num, gpio.gpio_status); } else { printk("63-->[%s] %s %d : gpio_set failed!\n", __FILE__, __func__, __LINE__); } break;
default: return -ENOTTY; /*不能支持的命令*/ } return 0;
} // /* 用来修改文件当前的读写位置 */
// loff_t gpio_llseek(struct file *filp, loff_t *f_pos, int n)
// {
// } /* */
static struct file_operations gpio_fops = {
.owner = THIS_MODULE,
.open = gpio_open,
.read = gpio_read,
.write = gpio_write,
.release = gpio_release,
/* 32位系统32位的内核 */
//.unlocked_ioctl = gpio_ioctl,
/* 32位系统64位的内核 */
// .compat_ioctl = gpio_ioctl,
//.llseek = gpio_llseek,
}; static struct gpio_cdev *gpio_dev = NULL;
dev_t gpio_devno;
static int gpio_major = 111;
static int gpio_minor = 0;
/* 模块初始化 */
static int __init gpio_init(void)
{
int ret = 0; printk("63-->[%s] %s %d : gpio_init \n", __FILE__, __func__, __LINE__);
/* 动态分配cdev */
gpio_dev = kmalloc(sizeof(struct gpio_cdev), GFP_KERNEL);
if(gpio_dev == NULL)
{
printk("[%s] %s %d : kmalloc failed!\n", __FILE__, __func__, __LINE__);
return -ENOMEM;
}
memset(gpio_dev, 0, sizeof(struct gpio_cdev)); /* 构建设备号 */
gpio_devno = MKDEV(gpio_major, 0); /* 如果gpio_major不为0则静态分配设备号,否则动态分配设备号 */
if(gpio_major)
{
ret = register_chrdev_region(gpio_devno, 1, "63gpio_dev");
if(ret < 0)
{ printk("[%s] %s %d : register_chrdev_region failed!\n", __FILE__, __func__, __LINE__); goto err_register_chrdev_region;
} }
else
{
ret = alloc_chrdev_region(gpio_devno, 0, 1, "63gpio_dev");
if(ret < 0)
{ printk("[%s] %s %d : alloc_chrdev_region failed!\n", __FILE__, __func__, __LINE__); goto err_alloc_chrdev_region;
}
} /* 从设备号中提取主设备号和次设备号 */
gpio_major = MAJOR(gpio_devno);
gpio_minor = MINOR(gpio_devno); /* 初始化cdev,建立cdev与file_operations之间的联系 */
cdev_init(&(gpio_dev->cdev), &gpio_fops);
gpio_dev->cdev.owner = THIS_MODULE; /* 向内核注册cdev结构体 */
ret = cdev_add(&(gpio_dev->cdev), gpio_devno, 1);
if(ret < 0)
{
printk("[%s] %s %d : cdev_add failed!\n", __FILE__, __func__, __LINE__);
goto err_cdev_add;
} /* 自动创建设备节点 */ /* 创建设备类 */
gpio_dev->gpio_class = class_create(THIS_MODULE, "63gpio_class");
if(IS_ERR(gpio_dev->gpio_class))
{
ret = PTR_ERR(gpio_dev->gpio_class);
printk("[%s] %s %d : class_create failed! ret = %d\n", __FILE__, __func__, __LINE__, ret);
goto err_class_create;
} /* 创建设备节点 */
gpio_dev->gpio_device = device_create(gpio_dev->gpio_class, NULL, gpio_devno, "gpio_dev", 0);
if(IS_ERR(gpio_dev->gpio_device))
{
ret = PTR_ERR(gpio_dev->gpio_device);
printk("[%s] %s %d : device_create failed! ret = %d\n", __FILE__, __func__, __LINE__, ret);
goto err_device_create;
} err_device_create:
device_destroy(gpio_dev->gpio_class, gpio_devno);
err_class_create:
class_destroy(gpio_dev->gpio_class);
err_cdev_add:
cdev_del(&(gpio_dev->cdev));
err_alloc_chrdev_region:
kfree(gpio_dev);
err_register_chrdev_region:
unregister_chrdev_region(MKDEV(gpio_devno, 0), 1);
return -1; return 0;
}
/* 模块卸载 */
void __exit gpio_exit(void)
{
device_destroy(gpio_dev->gpio_class, gpio_devno);
class_destroy(gpio_dev->gpio_class);
cdev_del(&(gpio_dev->cdev));
kfree(gpio_dev);
unregister_chrdev_region(MKDEV(gpio_devno, 0), 1); printk(KERN_ALERT "63-->ptdrv gpio exit\n");
} module_init(gpio_init);
module_exit(gpio_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("IS_ME");
MODULE_DESCRIPTION("gpio driver");
- 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
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
文章来源: blog.csdn.net,作者:IM-STONE,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/doubleintfloat/article/details/117905579
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)