pro1.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
char buf[128]={0};
int fd1,fd2,epfd;
struct epoll_event event; //用于操作epoll
struct epoll_event events[10]; //存放就绪事件描述符的数组
//创建epoll句柄
epfd = epoll_create(1);
if(epfd < 0)
{
printf("epoll_create filed\n");
exit(-1);
}
//打开设备文件
fd1 = open("/dev/input/mouse0",O_RDWR);
if(fd1 < 0)
{
printf("打开鼠标设备失败\n");
return -1;;
}
fd2 = open("/dev/myled0",O_RDWR);
if(fd1 < 0)
{
printf("打开设备失败\n");
return -1;;
}
//添加准备就绪事件进去epoll
event.events = EPOLLIN;//读事件
event.data.fd = fd1;
if(epoll_ctl(epfd,EPOLL_CTL_ADD,fd1,&event) < 0)
{
printf("epoll_ctl add filed\n");
}
event.events = EPOLLIN;//读事件
event.data.fd = fd2;
if(epoll_ctl(epfd,EPOLL_CTL_ADD,fd2,&event) < 0)
{
printf("epoll_ctl add filed\n");
}
//监听事件是否发生
while(1)
{
//如果成功,ret接受返回的事件个数,把就绪事件放在events数组中
int ret = epoll_wait(epfd,events,10,-1);
if(ret < 0)
{
printf("epoll_wait filed\n");
exit(-1);
}
int i;
//循环遍历数组,做事件的处理
for(i=0;i
pro2.c
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
char buf[128]="hello world";
int a,b;
int fd=open("/dev/myled0",O_RDWR);
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
write(fd,buf,sizeof(buf));//模拟硬件就绪事件
}
mycdev.c
#include
#include
#include
#include
#include
#include
#include
#include
int major;
char kbuf[128] = {0};
unsigned int *vir_rcc;
struct class *cls;
struct device *dev;
//定义等待队列
wait_queue_head_t wq_head;
unsigned int condition=0;
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if(file->f_flags&O_NONBLOCK)//用户程序以非阻塞读写数据
{}
else
{
wait_event_interruptible(wq_head,condition);//判断condition的值,为假则进程休眠
}
//将获取到的硬件数据拷贝到用户
int ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("copy_to_user filed\n");
return -EIO;
}
condition=0;//表示下一次数据没有准备就绪
return 0;
}
//封装POLL方法
__poll_t mycdev_poll(struct file *file,struct poll_table_struct *wait)
{
__poll_t mask = 0;
//向上提交等待队列头
poll_wait(file,&wq_head,wait);
//根据数据是否就绪给定一个合适的返回值
if(condition)
mask = POLLIN;
return mask;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
int ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
condition=1;//表示硬件数据就绪
wake_up_interruptible(&wq_head);//唤醒休眠的进程
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
// 定义操作方法结构体变量并赋值
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.poll = mycdev_poll,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
//初始化等待队列
init_waitqueue_head(&wq_head);
// 字符设备驱动注册
major = register_chrdev(0, "mychrdev", &fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功:major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "mychrdev");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
// 向上提交设备节点信息
int i; // 向上提交三次设备节点信息
for (i = 0; i < 3; i++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
if (IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
return -PTR_ERR(dev);
}
}
printk("向上提交设备节点成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
// 销毁设备节点信息
int i;
for (i = 0; i < 3; i++)
{
device_destroy(cls, MKDEV(major, i));
}
// 销毁目录
class_destroy(cls);
// 注销字符设备驱动
unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");