驱动程序如下,加载驱动后,会在/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);
}

最新文章

  1. mongoDB数据库和Spring MVC的整合
  2. iOS开发笔记14:微博/微信登录与分享、微信/支付宝支付
  3. 关于 jquery 选择器的 深入理解 -1
  4. cordova 环境搭建
  5. 使用KNN对MNIST数据集进行实验
  6. SharePoint 2010中列表Add和Delete事件解析
  7. 加密算法 - RSA算法二
  8. centos+nginx+uwsgi+virtualenv+flask 多站点环境搭建
  9. [LeetCode] Knight Probability in Chessboard 棋盘上骑士的可能性
  10. 深入理解Java虚拟机之JVM垃圾回收随笔
  11. oracle之 any、some、all 解析
  12. Tomcat-servlet基础
  13. 多模块项目提示“Module ** must not contain source root **. The root already belongs to module **”的解决办法
  14. [转载]web服务器
  15. Leetcode 1014. 在 D 天内送达包裹的能力
  16. 7款精美HTML5应用
  17. Caffe搭建:常见问题解决办法和ubuntu使用中遇到问题(持续更新)
  18. javascript cookie操作.
  19. Kubernetes学习之路(十)之资源清单定义
  20. eclipse 启动不起来(不能创建VM)

热门文章

  1. Spring validator常用注解
  2. poj-3080(kmp+暴力枚举)
  3. 【嵌入式】Arduino编程基础到应用全解析
  4. python 模块之-shelve
  5. CSS初步学习
  6. 一种HBase表数据迁移方法的优化
  7. 【JSOI2008】火星人prefix 哈希 非旋转treap
  8. jwt实现
  9. Ubuntu解压
  10. MT【257】任意存在并存