zoukankan      html  css  js  c++  java
  • 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 !!
    ");
        return 0;
    }
    
    static int my_remove(struct device *dev)
    {
        printk("driver found device unpluged !!
    ");
        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状态码
    Binary String Matching
    三个数从小到大排序
    java控制台输入带空格的字符串
    括号配对问题
    最小生成树之Prim算法(最原始最详细入门)
    hdu 1850 Being a Good Boy in Spring Festival(尼姆博弈)
    hdu 1848 Fibonacci again and again(尼姆博弈)
    hdu 1847 Good Luck in CET-4 Everybody!(入门SG值)
    hdu 1527 取石子游戏(威佐夫博弈)
  • 原文地址:https://www.cnblogs.com/hello2mhb/p/3321167.html
Copyright © 2011-2022 走看看