在dev目录创建一个字符设备驱动的流程
Posted 夕相待
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在dev目录创建一个字符设备驱动的流程相关的知识,希望对你有一定的参考价值。
1.struct file_operations 字符设备文件接口
1: static int mpu_open(struct inode *inode, struct file *file)
2: {
3: return 0;
4: }
5:
6:
7:
8: static int mpu_close(struct inode *inode, struct file *file)
9: {
10: return 0;
11: }
12:
13:
14: static long mpu_ioctl ( struct file *filp, unsigned int cmd, unsigned long arg)
15: {
16: return 0;
17: }
18:
19: static ssize_t mpu_read(struct file * filp, char __user *buffer, size_t size, loff_t *off)
20: {
21: return 0;
22: }
23:
24: static ssize_t mpu_write(struct file *filp, const char __user *buffer, size_t size, loff_t *off)
25: {
26: return 0;
27: }
//结构
1: struct file_operations mpu_ops = {
2: .owner = THIS_MODULE,
3: .open = mpu_open,
4: .unlocked_ioctl = mpu_ioctl,
5: .release = mpu_close,
6: .read = mpu_read,
7: .write = mpu_write,
8: };
2.向内核注册字符设备框架
1: //根据设备号与设备名注册字符设备
2: ret = register_chrdev(253, "dt-mpu", &mpu_ops);
3: if (ret < 0) {
4: printk("Unable to register character device %d!\n", 253);
5: return ret;
6: }
3.在/sys/class下面注册一个类
1: mcls = class_create(THIS_MODULE, "dt-mpu");
2: if (!mcls) {
3: printk("class_create dt-mpu failure...\n");
4: goto un_register;
5: }
4.在/dev/目录创建dt-mpu设备文件
//创建一个设备节点,节点名为DEVICE_NAME dev = device_create(mcls, NULL, MKDEV(253, 0), NULL, "dt-mpu"); if (!dev) { printk("device_create dt-mpu failure...\n"); goto des_class; }
以上是关于在dev目录创建一个字符设备驱动的流程的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp 快速代码片段,用于在统计(阻止)/ dev / rdsk中的设备时验证fstat64和stat64的行为。