(五) 驱动注册

1. platform_driver_registere函数

  将驱动注册到平台需要用到结构体”platform_driver_register”,下面就该函数做一下简单介绍:
 “platform_driver_register”和“platform_driver_unregister”用户注册和卸载驱动。在linux目录中,使用命令”vim include/linux/platform _device.h”,查找一下“platform_driver_register”,如下:

extern int platform_driver_register(struct platform_driver*);
extern void platform_driver_unregister(struct platform_driver*);

这两个函数都调用“platform_driver“类型结构体。在linux目录中使用命令”vim include/linux/plartform_device.h”中:

struct platform_driver {
        int (*probe)(struct platform_device *);
        int (*remove)(struct platform_device *);
        void (*shutdown)(struct platform_device *);
        int (*suspend)(struct platform_device *, pm_message_t state);
        int (*resume)(struct platform_device *);
        struct device_driver driver;
        const struct platform_device_id *id_table;
 };

  通常大多数驱动都会用到这个结构体,所以非常重要。该结构体中包含了一组操作函数和一个struct device_driver成员,在驱动中首先要做的是定义platform_driver的函数,并创建这个结构的一个对象实例,然后在init()函数中调用platform_driver_register()向系统注册我们的驱动。
  函数int(probe)(struct platform_device )主要是进行设备的探测和初始化,例如我们想调用一个GPIO,首先需要探测这个GPIO是否被占用,如果占用了初始化失败,驱动注册也就失败,如果没有占用,那就需要去占用它。该函数还会添加设备节点的函数,如果初始化成功需要添加设备节点。
  函数int (remove)(struct platform_device )用于移除设备节点释放软硬件资源。
  函数void (shutdown)(struct platform_device );int (suspend)(struct platform_device , pm_message_t state);int (resume)(struct platform_device ); 从字面上比较好理解。
  结构体struct device_driver driver主要包含两个参数,一个是name参数,驱动名称(需要和设备驱动结构中的name参数一样);一个是owner,一般是THIS_MODULE。

1.1 probe函数 

paltform_driver结构体中的参数probe指向platform_driver_probe函数:

extern int platform_driver_probe(struct platform_driver *driver,int(*probe)(struct platform_device *));

platform_driver_probe参数在前面注册的HELL_CTL结构体中。

1.2 程序清单
#include <linux/init.h>
#include <linux/module.h>
/*驱动注册的头文件,包含驱动的结构体和注册和卸载的函数*/
#include <linux/platform_device.h>

#define DRIVER_NAME "hello_ctl"

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("flywang606");

static int hello_probe(struct platform_device *pdv){

    printk(KERN_EMERG "\tinitialized\n");

    return 0;
}

static int hello_remove(struct platform_device *pdv){

    return 0;
}

static void hello_shutdown(struct platform_device *pdv){

    ;
}

static int hello_suspend(struct platform_device *pdv){

    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 init!\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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值