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("\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 编程 ------ 通过设备节点调用驱动的更多相关文章
- I.MX6 linux eGalaxTouch 自动获取设备节点
I.MX6 linux eGalaxTouch 自动获取设备节点 \\\\\\\\\\\\\\-*- 目录 -*-///////////// | 一. 需求: | 二. /proc/bus/input ...
- Linux Shell 判断块设备节点是否存在
/************************************************************************* * Linux Shell 判断块设备节点是否存在 ...
- Linux加载DTS设备节点的过程(以高通8974平台为例)
DTS是Device Tree Source的缩写,用来描述设备的硬件细节.在过去的ARM Linux中,arch/arm/plat-xxx和arch/arm/mach-xxx中充斥着大量的垃圾代码, ...
- Linux /dev 自动创建设备节点
#include <linux/module.h> #include <linux/module.h> #include <linux/kernel.h> #inc ...
- Linux platform平台总线、平台设备、平台驱动
平台总线(platform_bus)的需求来源? 随着soc的升级,S3C2440->S3C6410->S5PV210->4412,以前的程序就得重新写一遍,做着大量的重复工作, 人 ...
- Linux 内核驱动自动创建设备节点并挂载设备
*注:本文来自http://blog.csdn.net/lwj103862095/article/details/17470573 一.首先需要在最开始定义两个数据结构: static struct ...
- linux驱动开发(四) 字符设备驱动框架(自动创建设备节点)
代码如下 #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> # ...
- linux字符设备驱动中内核如何调用驱动入口函数 一点记录
/* 内核如何调用驱动入口函数 ? *//* 答: 使用module_init()函数,module_init()函数定义一个结构体,这个结构体里面有一个函数指针,指向first_drv_init() ...
- Linux设备驱动实现自己主动创建设备节点
#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #inclu ...
随机推荐
- Jenkins+PowerShell持续集成环境搭建(七)构建触发器
Jenkins 有三种类型的构建触发器,如下图: Build after other projects are built:在其他项目构建后构建: Build periodically:定时构建: P ...
- Nginx TSL/SSL优化握手性能
L:131
- Nginx 缓存针对打开的文件句柄与原文件信息
L:108 open_file_cache syntax: open_file_cache off; open_file_cache max=N[inactive=time](inactive表示 ...
- 【BZOJ4325】【NOIP2015】斗地主 搜索
题目描述 就是给你一副牌,问你最少几次能出完. 详细规则见规则 \(n\leq 23\) 题解 NOIP的数据非常水,错误一大堆的程序都能AC. 因为顺子对答案的影响最大,所以先枚举顺子进行搜索. 接 ...
- THUSC2017题解
THUSC2017题解 题目都是在LOJ上交的. chocolate LOJ#2977巧克力 这题看着就让人想起了百度之星复赛的\(T5\),就是这题. 因为种类的个数很多,所以把每个种类随意\(ra ...
- Nowcoder | [题解-N210]牛客OI月赛2-提高组
比赛连接戳这里^_^ 我才不会说这是我出的题(逃) 周赛题解\((2018.10.14)\) \(T1\) \(25\sim50\)分做法\(:\)直接爆搜 作为一个良心仁慈又可爱的出题人当然\(T1 ...
- js排序算法总结
快速排序 大致分三步: 1.找基准(一般是以中间项为基准) 2.遍历数组,小于基准的放在left,大于基准的放在right 3.递归 快速排序的平均时间复杂度是O(nlogn),最差情况是O(n²). ...
- python服务器文件上传下载+GUI【tkinter】
大概就是一个通过应用程序来和服务器打交道的这么一个,小东西 1.GUI 用的是tkinter # -*- coding: UTF-8 -*- from tkinter import * import ...
- ssh-key 与 git账户配置以及多账户配置,以及通信方式从https切换到ssh
参考:http://www.cnblogs.com/dubaokun/p/3550870.html 在使用git的时候,git与远程服务器是一般通过ssh传输的(也支持ftp,https),我们在管理 ...
- [HEOI2014]平衡(整数划分数)
下课了,露露.花花和萱萱在课桌上用正三棱柱教具和尺子摆起了一个“跷跷板”. 这个“跷跷板”的结构是这样的:底部是一个侧面平行于地平面的正三棱柱教具,上面 摆着一个尺子,尺子上摆着若干个相同的橡皮.尺子 ...