platform平台设备驱动简化示例代码

driver.c:

复制代码
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/platform_device.h>

#define DRIVER_NAME "my_dev"

static int my_probe(struct device *dev)
{
    printk("driver found device !!\n");
    return 0;
}

static int my_remove(struct device *dev)
{
    printk("driver found device unpluged !!\n");
    return 0;
}

struct platform_driver my_driver={
    .probe=my_probe,
    .remove=my_remove,
    .driver={                    //platform_driver内嵌device_driver
            .owner=THIS_MODULE,
            .name =DRIVER_NAME,//最重要的是对name的赋值,因为在匹配的时候要用!!!!
    },
};

static int __init my_driver_init(void)
{    
    int ret =0;

    /*注册平台驱动*/
    ret=platform_driver_register(&my_driver);

    return ret;
}

static void __exit my_driver_exit(void)
{
    /*注销平台驱动*/
    platform_driver_unregister(&my_driver);
}

MODULE_AUTHOR("mhb@SEU");
MODULE_LICENSE("GPL");

module_init(my_driver_init);
module_exit(my_driver_exit);
复制代码

device.c:

复制代码
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/platform_device.h>
#include <linux/device.h>

#define DEVICE_NAME "my_dev"

struct platform_device *my_device;

static int __init my_device_init(void)
{
    int ret=0;

    /*分配结构体内存*/
    my_device=platform_device_alloc(DEVICE_NAME,-1);

    /*注册平台设备*/
    ret=platform_device_add(my_device);//在platform_device_alloc中已经init了,所以调用add而不是register
    if(ret)
        platform_device_put(my_device);/*注册失败,释放相关内存*/

    return ret;
}

static void __exit my_device_exit(void)
{
    platform_device_unregister(my_device);
}

MODULE_AUTHOR("mhb@SEU");
MODULE_LICENSE("GPL");

module_init(my_device_init);
module_exit(my_device_exit);
复制代码

http://www.cnblogs.com/hello2mhb/p/3321167.html

posted @   山岚2013  阅读(256)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示