linux内核驱动之helloWord本地驱动
来源:互联网 发布:手机hifi播放软件 编辑:程序博客网 时间:2024/05/21 11:35
linux内核驱动模块之hello
###################################################################
/* hello.c */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
MODULE_LICENSE("HELLO");
MODULE_AUTHOR("WENSION");
#define DEV_NAME "Hello"
static ssize_t HelloRead(struct file*,char *,size_t,loff_t*);
static ssize_t HelloWrite(struct file*,const char *,size_t,loff_t*);
static int char_major = 0;
static int Char_Data = 0; //"Test_Char"设备的全局变量
struct file_operations hello_fops =
{
.read = HelloRead,
.write= HelloWrite
};
static int hello_init(void)
{
// printk(KERN_ALERT "Hello, world\n");
int ret;
ret = register_chrdev(char_major,DEV_NAME,&hello_fops);
//注册设备驱动
if(ret<0)
{
printk(KERN_ALERT "HelloDev Reg Faile!\n");
}
else
{
printk(KERN_ALERT "HelloDev Reg Success!\n");
char_major = ret;
printk(KERN_ALERT "Major = %d\n",char_major);
}
return ret;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, Hello world\n");
unregister_chrdev(char_major,DEV_NAME); //注销设备驱动
return;
}
/*设备驱动读函数*/
static ssize_t HelloRead(struct file *filp,char *buf,size_t len,loff_t *off)
{
printk(KERN_ALERT "Hello Read Data\n");
if(copy_to_user(buf,&Char_Data,sizeof(int)))
{
return -EFAULT;
}
return sizeof(int);
}
/*设备驱动写函数*/
static ssize_t HelloWrite(struct file *filp,const char *buf,size_t len,loff_t *off)
{
printk(KERN_ALERT "Hello Write Data\n");
if(copy_from_user(&Char_Data,buf,sizeof(int)))
{
return -EFAULT;
}
return sizeof(int);
}
module_init(hello_init);
module_exit(hello_exit);
###################################################################
Makefile
###################################################################
obj-m += hello.o
CURRENT_PATH:=$(shell pwd)
LINUX_KERNEL_PATH:=/lib/modules/$(shell uname -r)/build
all:
$(MAKE) -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
clean:
rm -rf *.cmd *.o *.mod.c *.ko *.symvers *.order
###################################################################
加载内核驱动
###################################################################
sudo insmod hello.ko
查看内核分配的主设备号:
cat /proc/devices | grep Hello
250 Hello
创建设备节点:
sudo mknod -m 666 /dev/hello c 250 0
###################################################################
驱动应用程序
###################################################################
/*test_app.c*/
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#define DEV_NAME "/dev/hello"
int main()
{
int fd,num;
//打开设备文件
fd = open(DEV_NAME,O_RDWR,S_IRUSR | S_IWUSR);
if(fd<0)
{
printf("Open Hello Device Fail!\n");
return -1;
}
//读取当前设备
read(fd,&num,sizeof(int));
printf("The Hello Device is:%d\n",num);
printf("Please input a number written to chardev: ");
scanf("%d",&num);
//写入数值到当前设备
write(fd,&num,sizeof(int));
//读取写入数值
read(fd,&num,sizeof(int));
printf("The Hello Device is:%d\n",num);
close(fd);
return 0;
}
查看模块加载信信息:
dmesg | tail -n 10
cat /var/log/syslog | grep Hello
- linux内核驱动之helloWord本地驱动
- linux驱动开发之helloword
- linux驱动开发之旅(一)--helloword
- 嵌入式Linux驱动开发之helloword心得
- linux驱动设计----helloword
- linux字符设备驱动helloword
- Linux内核模块驱动之---led驱动
- Linux驱动之内核定时器驱动设计
- linux内核驱动之修改wifi驱动
- Linux内核设备驱动之GIC驱动
- Linux内核驱动之字符驱动
- linux驱动之内核定时器驱动设计
- Linux内核之设备驱动
- Linux内核驱动之设备
- linux驱动之内核编译
- Linux 驱动之内核定时器
- linux内核之字符驱动
- linux内核驱动之定时器
- jQuery的节点操作
- ng-options如何应用到多维数组
- Android APK常驻内存解决方案
- hello World Spring Boot
- Remove Element:删除数组中指定元素
- linux内核驱动之helloWord本地驱动
- python 游标、MySQL连接池、设计表
- Detecting Text in Natural Image with Connectionist Text Proposal Network
- 哈希表
- 动态规划学习:数塔问题详尽分析
- 大咖|英特尔中国研究院院长宋继强:我们是如何与李宇春打造全球第一支三维人脸特效的音乐视频的
- 联合国曝光AI杀手机器人,这可能是你见过最恐怖的视频
- 谷歌Chrome浏览器开发者工具教程—JS调试篇
- GPU 与 CPU?什么是 GPU 计算?|NVIDIA