1、驱动程序
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <linux/poll.h>
struct cdev *cdev;
char kbuf[128]={0};
unsigned int major=0;
unsigned int minor=0;
dev_t devno;
module_param(major,uint,0664);//方便在命令行传递major的值
struct class*cls;
struct device *dev;
unsigned int condition=0;
//定义一个等待队列头
wait_queue_head_t wq_head;
// 封装操作方法
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)
{unsigned long ret;if (size > sizeof(kbuf)) // 用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小size = sizeof(kbuf);if(file->f_flags&O_NONBLOCK)//非阻塞IO{}else{//wait_event_interruptible(wq_head,condition);}ret = copy_to_user(ubuf, kbuf, size);if (ret){printk("copy_to_user err\n");return -EIO;}condition=0;return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{unsigned long ret;if (size > sizeof(kbuf)) // 用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小size = sizeof(kbuf);ret = copy_from_user(kbuf, ubuf, size);if (ret){printk("copy_from_user err\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;
}
//封装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;
}
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);int ret;//为字符设备驱动对象申请空间cdev=cdev_alloc();if(cdev==NULL){printk("字符设备驱动对象申请空间失败\n");ret=-EFAULT;goto out1;}printk("申请对象空间成功\n");//初始化字符设备驱动对象cdev_init(cdev,&fops);//申请设备号if(major>0)//静态指定设备号{ret=register_chrdev_region(MKDEV(major,minor),3,"myled");if(ret){printk("静态申请设备号失败\n");goto out2;}}else if(major==0)//动态申请设备号{ret=alloc_chrdev_region(&devno,minor,3,"myled");if(ret){printk("动态申请设备号失败\n");goto out2;}major=MAJOR(devno);//获取主设备号minor=MINOR(devno);//获取次设备号}printk("申请设备号成功\n");//注册字符设备驱动对象ret=cdev_add(cdev,MKDEV(major,minor),3);if(ret){printk("注册字符设备驱动对象失败\n");goto out3;}printk("注册字符设备驱动对象成功\n");//向上提交目录信息cls=class_create(THIS_MODULE,"myled");if(IS_ERR(cls)){printk("向上提交目录失败\n");ret=-PTR_ERR(cls);goto out4;}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");ret=-PTR_ERR(dev);goto out5;}}printk("向上提交设备信息成功\n");return 0;
out5://释放前一次提交成功的设备信息for(--i;i>=0;i--){device_destroy(cls,MKDEV(major,i));}class_destroy(cls);//释放目录
out4:cdev_del(cdev);
out3:unregister_chrdev_region(MKDEV(major,minor),3);
out2:kfree(cdev);
out1:return ret;
}static void __exit mycdev_exit(void)
{ //取消地址映射//注销字符设备驱动//释放节点信息int i;for(i=0;i<3;i++){device_destroy(cls,MKDEV(major,i));}//销毁目录class_destroy(cls);//注销驱动对象cdev_del(cdev);//释放设备号unregister_chrdev_region(MKDEV(major,minor),3);//释放对象空间kfree(cdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
2、测试程序鼠标设备事件
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>int main(int argc, char const *argv[])
{char buf[128] = {0};int fd1,fd2;fd1 = open("/dev/input/mouse0", O_RDWR);if (fd1 < 0){printf("打开鼠标设备文件失败\n");exit(-1);}fd2 = open("/dev/myled0",O_RDWR);if(fd2 < 0){printf("打开设备文件失败\n");}//定义事件集合fd_set readfds;while (1){//清空事件集合FD_ZERO(&readfds);//将要监听的文件描述符添加到刻度集合FD_SET(fd1,&readfds);FD_SET(fd2,&readfds);//等待事件就绪printf("%s\n",buf);int ret = select(fd2+1,&readfds,NULL,NULL,NULL);if(ret<0){printf("select err\n");exit(-1);}//判断事件是否发生//没发生事件会清除刻度集合,只需要判断文件描述符是否在集合中即可if(FD_ISSET(fd1,&readfds)){read(fd1,buf,sizeof(buf));printf("鼠标事件发生%s\n",buf);}if(FD_ISSET(fd2,&readfds)){read(fd2,buf,sizeof(buf));printf("自定义设备发生%s\n",buf);}}close(fd1);close(fd2);return 0;
}
3、自定义事件设备
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>int main(int argc, char const *argv[])
{char buf[128] = {"hello world"};int fd = open("/dev/myled0", O_RDWR);if (fd < 0){printf("打开设备文件失败\n");exit(-1);}write(fd,buf,sizeof(buf));close(fd);return 0;
}
4测试结果