zoukankan      html  css  js  c++  java
  • linux c 编程 ------ 通过设备节点调用驱动

     驱动程序如下,加载驱动后,会在/dev文件夹下生成一个文件hello_device_node,是此驱动的设备节点

    #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"
    #define NODE_NAME "hello_device_node"
    
    MODULE_LICENSE("Dual BSD/GPL"); // required
    MODULE_AUTHOR("liuShuiDeng");
    
    
    static long hello_fs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
    {
        printk("
    cmd is %d, arg is %ld
    ", cmd, arg);
        return 0;
    }
    static int hello_fs_release(struct inode *inode, struct file *file)
    {
        printk(KERN_EMERG "
    hello_fs_release
    ");
        return 0;
    }
    static int hello_fs_open(struct inode *inode, struct file *file)
    {
        printk(KERN_EMERG "
    hello_fs_open
    ");
        return 0;
    }
    static struct file_operations hello_fops = {
        .owner = THIS_MODULE,
        .open = hello_fs_open,
        .release = hello_fs_release,
        .unlocked_ioctl = hello_fs_ioctl,
    };
    static struct miscdevice hello_miscdevice = {
        .minor = MISC_DYNAMIC_MINOR,
        .name = NODE_NAME,
        .fops = &hello_fops,
    };
    static int hello_probe(struct platform_device *p)
    {
        printk(KERN_EMERG "
    hello_probe
    ");
        misc_register(&hello_miscdevice);
        return 0;
    }
    
    static int hello_remove(struct platform_device *p)
    {
        printk(KERN_EMERG "
    hello_remove
    ");
        misc_deregister(&hello_miscdevice);
        return 0;
    }
    
    static void hello_shutdown(struct platform_device *p)
    {
        printk(KERN_EMERG "
    hello_shutdown
    ");
    }
    
    static int hello_suspend(struct platform_device *p, pm_message_t state)
    {
        printk(KERN_EMERG "
    hello_suspend
    ");
        
        return 0;
    }
    
    static int hello_resume(struct platform_device *p)
    {
        printk(KERN_EMERG "
    hello_resume
    ");
        
        return 0;
    }
    
    static 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) //insmod xxx.ko, execute it
    {
        printk(KERN_EMERG "
    hello world enter~
    
    ");
        platform_driver_register(&hello_driver);
        return 0;    
    }
    
    static void hello_exit(void) //rmmod xxx( note: not xxx.ko ), execute it
    {
        printk(KERN_EMERG "
    hello world exit~
    
    ");
        platform_driver_unregister(&hello_driver);
    }
    
    
    
    module_init(hello_init);
    module_exit(hello_exit);

    应用程序如下

    编译驱动程序的编译器和编译应用程序的编译器建议用同一个

    编译应用程序指令:arm-none-linux-gnueabi-gcc -o invoke_hello invoke_hello.c

    修改权限指令:chmod 777 invoke_hello

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <sys/ioctl.h>
    
    
    void main()
    {
        int fd;
        char *hello_node = "/dev/hello_device_node";
    
        fd = open(hello_node, O_RDWR|O_NDELAY);
        if(fd < 0)
        {
            printf("APP---->can't open "%s"
    ",hello_node);
        }else
        {
            printf("APP--->open "%s" successfully
    ", hello_node);
            ioctl(fd,1,2);
        }
    
        close(fd);
    }
  • 相关阅读:
    GIT使用入门
    源代码的下载和编译:
    搭建Android开发环境
    ndroid系统移植与驱动开发概述
    python 通过2个字典中的key比较 如果key一样2个字典中的v和vaule重新组成新的的字典
    Git 多分支开发合并
    Python清除字典中值为空的键值对
    01 Java基本数据类型、包装类、装箱拆箱、parseXxx()、String.valueOf()
    00 Java开发准备
    关于程序书写风格的一些漏见
  • 原文地址:https://www.cnblogs.com/god-of-death/p/9502652.html
Copyright © 2011-2022 走看看