驱动程序如下,加载驱动后,会在/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("\ncmd is %d, arg is %ld\n", cmd, arg);
return ;
}
static int hello_fs_release(struct inode *inode, struct file *file)
{
printk(KERN_EMERG "\nhello_fs_release\n");
return ;
}
static int hello_fs_open(struct inode *inode, struct file *file)
{
printk(KERN_EMERG "\nhello_fs_open\n");
return ;
}
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 "\nhello_probe\n");
misc_register(&hello_miscdevice);
return ;
} static int hello_remove(struct platform_device *p)
{
printk(KERN_EMERG "\nhello_remove\n");
misc_deregister(&hello_miscdevice);
return ;
} static void hello_shutdown(struct platform_device *p)
{
printk(KERN_EMERG "\nhello_shutdown\n");
} static int hello_suspend(struct platform_device *p, pm_message_t state)
{
printk(KERN_EMERG "\nhello_suspend\n"); return ;
} static int hello_resume(struct platform_device *p)
{
printk(KERN_EMERG "\nhello_resume\n"); return ;
} 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 "\nhello world enter~\n\n");
platform_driver_register(&hello_driver);
return ;
} static void hello_exit(void) //rmmod xxx( note: not xxx.ko ), execute it
{
printk(KERN_EMERG "\nhello world exit~\n\n");
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 < )
{
printf("APP---->can't open \"%s\"\n",hello_node);
}else
{
printf("APP--->open \"%s\" successfully\n", hello_node);
ioctl(fd,,);
} close(fd);
}

linux c 编程 ------ 通过设备节点调用驱动的更多相关文章

  1. I.MX6 linux eGalaxTouch 自动获取设备节点

    I.MX6 linux eGalaxTouch 自动获取设备节点 \\\\\\\\\\\\\\-*- 目录 -*-///////////// | 一. 需求: | 二. /proc/bus/input ...

  2. Linux Shell 判断块设备节点是否存在

    /************************************************************************* * Linux Shell 判断块设备节点是否存在 ...

  3. Linux加载DTS设备节点的过程(以高通8974平台为例)

    DTS是Device Tree Source的缩写,用来描述设备的硬件细节.在过去的ARM Linux中,arch/arm/plat-xxx和arch/arm/mach-xxx中充斥着大量的垃圾代码, ...

  4. Linux /dev 自动创建设备节点

    #include <linux/module.h> #include <linux/module.h> #include <linux/kernel.h> #inc ...

  5. Linux platform平台总线、平台设备、平台驱动

    平台总线(platform_bus)的需求来源? 随着soc的升级,S3C2440->S3C6410->S5PV210->4412,以前的程序就得重新写一遍,做着大量的重复工作, 人 ...

  6. Linux 内核驱动自动创建设备节点并挂载设备

    *注:本文来自http://blog.csdn.net/lwj103862095/article/details/17470573 一.首先需要在最开始定义两个数据结构: static struct ...

  7. linux驱动开发(四) 字符设备驱动框架(自动创建设备节点)

    代码如下 #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> # ...

  8. linux字符设备驱动中内核如何调用驱动入口函数 一点记录

    /* 内核如何调用驱动入口函数 ? *//* 答: 使用module_init()函数,module_init()函数定义一个结构体,这个结构体里面有一个函数指针,指向first_drv_init() ...

  9. Linux设备驱动实现自己主动创建设备节点

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

随机推荐

  1. BZOJ2616 SPOJ PERIODNI(笛卡尔树+树形dp)

    考虑建一棵小根堆笛卡尔树,即每次在当前区间中找到最小值,以最小值为界分割区间,由当前最小值所在位置向两边区间最小值所在位置连边,递归建树.那么该笛卡尔树中的一棵子树对应序列的一个连续区间,且根的权值是 ...

  2. 微信小程序——代码片段汇集

    导航栏 作者:beatzcs       链接:https://www.jianshu.com/p/c681007a6287 这个导航虽然已经很完善了,不过还是要根据自己的来进行修改的 tabs.wx ...

  3. Codeforces191 C. Fools and Roads

    传送门:>Here< 题意:给出一颗树,和K次操作.每次操作给出a,b,代表从a到b的路径上所有边的权值都+1(边权最开始全部为0).最后依次输出每条边最终的权值 解题思路: 由于n非常大 ...

  4. YC的基本创业建议

    原文出处:https://blog.ycombinator.com/ycs-essential-startup-advice/ 我们给初创公司的许多建议都是战术性的; 意味着在日常或周到周的基础上有所 ...

  5. 【ARC072E】Alice in linear land DP

    题目大意 有一个人要去直线上\(lm\)远处的地方,他会依次给他的机器发出\(n\)个指令.第\(i\)个指令为\(d_i\).他的机器收到一个指令\(x\)后,如果向目的地方向前进\(xm\)后比当 ...

  6. post提交的数据几种编码格式

    1.背景介绍 HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端 ...

  7. LVS-Keepalived高可用集群(DR)

    LVS-DR+Keepalived高可用集群 -------client------------------主LVS--------------------从LVS------------------ ...

  8. BZOJ3133[Baltic2013]ballmachine

    题目描述 https://www.lydsy.com/JudgeOnline/problem.php?id=3133 题解 还是分两个操作来说吧. 先看第一个操作,放球,可以发现,对于祖先节点和后代节 ...

  9. 百度地图API示例:使用vue添加删除覆盖物

    1.index.html <script type="text/javascript" src="http://api.map.baidu.com/api?v=2. ...

  10. tyvj/joyoi 1305 最大子序和

    带了一个转化的单调队列裸题. 转化为前缀和相减即可. 有一点需要注意:从0开始入队而不是1,因为要统计第一个. (从网上找的对拍程序,结果别人写错了) /** freopen("in.in& ...