08_生成设备节点

itop4412学习记录

08_生成设备节点

8.1 申明

• Linux到2.6版本的时候,改动巨大,网上有些资料是针对以前的版本的,大家看到老版本相关的资料,直接跳过即可。

• 现在2.6版本以前的基本都废弃了,不用管了,学了也没有。学习要“

以始为终”,学了之后是为了在实际工作中应用的,那么工作中已经用不到的知识就不要浪费时间了。

8.2 杂项设备

(1)为什么用杂项设备

• 杂项设备可以说是对一部分字符设备的封装,还有一部分不好归类驱动也归到杂项设备。

• 为什么会引入杂项设备?

• 第一、节省主设备号

– 如果所有的驱动都是用字符设备,那么所有的设备号很快就用完了,总共就255个主设备号。

• 第二、驱动写起来相对简单

– 如果直接使用封装好的杂项设备,那么就可以减少一步注册主设备号的过程

(2)杂项设备初始化文件

• 杂项设备初始化部分源文件“drivers/char/ misc.c”,这一部分通过Makefile可知,是强制编译的。而且是Linux官方(不是三星官方)出来的时候就带的,为了一些简单的驱动更容易实现。

• 这部分了解即可,里面的内容也比较简单,就是给字符驱动做一个简单的封装

(3)注册文件

• 杂项设备注册头文件

– include/linux/miscdevice.h

• 结构体miscdevice以及注册函数如下所示

 

• 常用的参数

– .minor设备号

– .name生成设备节点的名称

– .fops指向一个设备节点文件

(4)内核文件的结构体

• Linux中一切皆文件,上层调用底层也是通过读取文件的方式

– 注册设备节点,本质也是新建一个特殊的文件,包含文件名,打开、关闭、操作等函数

• 包含文件结构体的头文件是“include/linux/fs.h ”

• 文件的结构体file_operations如下所示

 

• 文件的结构体file_operations参数很多,根据需求选择。

• 必选的是参数是

– .owner一般是THIS_MODULE,

– .open打开文件函数

– .release关闭文件函数

• 这里在必选之外使用参数(为了介绍接下来的GPIO的操作)

– .unlocked_ioctl对GPIO的操作,应用向底层驱动传值

8.3 生成杂项设备的设备节点

• 驱动代码,在probe_linux_module基础上写devicenode_linux_module驱动

–写代码的时候,注意一下函数调用顺序

• 编译,在开发板上加载驱动生成设备节点

– 在/dev中查看是否生成了设备节点

devicenode_linux_module.c程序代码:

#include <linux/init.h>
#include <linux/module.h>

#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>

#define DRIVER_NAME "hello_ctl"
#define DEVICE_NAME "hello_ctl123"
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("TOPEET");

static long hello_ioctl( struct file *files, unsigned int cmd, unsigned long arg){


        printk("cmd is %d,arg is %d\n",cmd,arg);
        return 0;
}

static int hello_release(struct inode *inode, struct file *file){
        printk(KERN_EMERG "hello release\n");
        return 0;
}

static int hello_open(struct inode *inode, struct file *file){
        printk(KERN_EMERG "hello open\n");
        return 0;
}

static struct file_operations hello_ops = {
        .owner = THIS_MODULE,
        .open = hello_open,
        .release = hello_release,
        .unlocked_ioctl = hello_ioctl,
};

static  struct miscdevice hello_dev = {
        .minor = MISC_DYNAMIC_MINOR,
        .name = DEVICE_NAME,
        .fops = &hello_ops,
};


static int hello_probe(struct platform_device *pdv){

        printk(KERN_EMERG "\tinitialized\n");
        misc_register(&hello_dev);

        return 0;
}

static int hello_remove(struct platform_device *pdv){

        printk(KERN_EMERG "\tremove\n");
        misc_deregister(&hello_dev);
        return 0;
}

static void hello_shutdown(struct platform_device *pdv){

        ;
}

static int hello_suspend(struct platform_device *pdv,pm_message_t pmt){

        return 0;
}

static int hello_resume(struct platform_device *pdv){

        return 0;
}

struct platform_driver hello_driver = {
        .probe = hello_probe,
        .remove = hello_remove,
        .shutdown = hello_shutdown,
        .suspend = hello_suspend,
        .resume = hello_resume,
        .driver = {
                .name = DRIVER_NAME,
                .owner = THIS_MODULE,
        }
};


static int hello_init(void)
{
        int DriverState;

        printk(KERN_EMERG "HELLO WORLD enter!\n");
        DriverState = platform_driver_register(&hello_driver);

        printk(KERN_EMERG "\tDriverState is %d\n",DriverState);
        return 0;
}


static void hello_exit(void)
{
        printk(KERN_EMERG "HELLO WORLD exit!\n");

        platform_driver_unregister(&hello_driver);
}

module_init(hello_init);
module_exit(hello_exit);

代码测试结果如下:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snaking616

你的鼓励是我最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值