#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include<linux/slab.h>
#include<linux/of_irq.h>
#include<linux/interrupt.h>
#include<linux/gpio.h>
#include<linux/of_gpio.h>
#include<linux/platform_device.h>
#include<linux/mod_devicetable.h>
#include<linux/of.h>
#define CNAME "myzhondduan"
struct resource *res;
//定义队列头
wait_queue_head_t wq_head;
unsigned int condition=0;//判断是否有数据准备好的标识变量
//定义一个指向设备节点的指针
struct device_node *node1;
struct property *pr1;//属性结构体指针
struct gpio_desc *desc;
/*myleds{
led1=<&gpioe 10 0>; //&gpioe表示引用的是gpioe控制器 //10表示gpioe10
//0表示默认状态
led2=<&gpiof 10 0>;
led3=<&gpioe 8 0>;
}; */
//定义一个指向设备节点的指针
struct device_node *node;
struct property *pr;//属性结构体指针
int gpiono1;
/* myirq{
interrupt-parent=<&gpiof>;
interrupts=<7 0>, <8 0>,<9 0>;
};
*/
struct device_node *node;
unsigned int irqno;//接收软中断号
char count = '0';
int major;//定义变量接收主设备号
char kbuf[128]={};//定义数组用于存放和用户之间拷贝的数据
struct class *cls;//句柄
struct device *dev;
//中断处理函数
irqreturn_t irq_handler(int irq, void *dev)
{
if(count == '0')
count = '1';
else
count = '0';
kbuf[0] = count;
//电位反转
gpiod_set_value(desc,!gpiod_get_value(desc));
//唤醒
condition=1;
wake_up_interruptible(&wq_head);
return IRQ_HANDLED;
}
int pdrv_probe(struct platform_device *pdr)
{
//获取设备信息
printk("%s:%d\n",__FILE__,__LINE__);
res=platform_get_resource(pdr,IORESOURCE_MEM,0);
if(res==NULL)
{
printk("获取资源失败\n");
return -ENODATA;
}
irqno=platform_get_irq(pdr,0);
if(irqno<0)
{
printk("获取资源失败\n");
return irqno;
}
printk("%#x %d\n",res->start,irqno);
//获取GPIO编号
desc=gpiod_get_from_of_node(pdr->dev.of_node,"led1",0,GPIOD_OUT_HIGH,NULL);
if(IS_ERR(desc))
{
printk("获取GPIO编号失败\n");
return PTR_ERR(desc);
}
return 0;
}
int pdrv_remove(struct platform_device *pdr)
{
printk("%s:%d\n",__FILE__,__LINE__);
return 0;
}
//定义compatible表
struct of_device_id oftable[]={
{.compatible="hqyj,platform"},
{},
};
MODULE_DEVICE_TABLE(of,oftable);
//定义并初始化对象
struct platform_driver pdrv ={
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name= "test",
.of_match_table= oftable,//设备树匹配
},
};
//对应的是open()
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//read()
ssize_t mycdev_read(struct file * file, char __user *ubuf, size_t size, loff_t *loff)
{
//size参数是用户期待读到的字节长度
int ret;
if(file->f_flags&O_NONBLOCK)
{
//非阻塞
return -EINVAL;
}
else{
//阻塞
ret=wait_event_interruptible(wq_head,condition);
if(ret)
{
printk("接收阻塞休眠\n");
return ret;
}
}
//把内核里的数据拷贝给用户
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_to_user(ubuf,kbuf,size);
if(ret)
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
//condition=0;
condition=0;
return size;
}
//write()
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_from_user(kbuf,ubuf,size);
if(ret)
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
return size;
}
//close()
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,
.release=mycdev_close,
};
//入口函数,当驱动安装的时候执行
static int __init demo_init(void)
{
int ret;
//注册对象
platform_driver_register(&pdrv);
//动态注册字符设备驱动
major=register_chrdev(0,CNAME,&fops);
if(major<0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n",major);
//向上提交节点目录
cls=class_create(THIS_MODULE,"ZD");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return PTR_ERR(cls);
}
printk("向上提交目录成功\n");
//创建设备节点
dev=device_create(cls,NULL,MKDEV(major,0),NULL,"myshebeishu");
if(IS_ERR(dev))
{
printk("创建节点失败\n");
return PTR_ERR(dev);
}
printk("创建节点成功\n");
//注册中断
ret=request_irq(irqno,irq_handler,IRQF_TRIGGER_FALLING,"key2_inte",NULL);
if(ret)
{
printk("注册中断失败\n");
return ret;
}
printk("注册中断成功\n");
//初始化对头
init_waitqueue_head(&wq_head);
return 0;
}
//出口函数,卸载驱动的时候执行
static void __exit demo_exit(void)
{
//注销
platform_driver_unregister(&pdrv);
//销毁节点
device_destroy(cls,MKDEV(major,0));
//销毁目录
class_destroy(cls);
//注销字符设备驱动
unregister_chrdev(major,CNAME);
//注销中断
free_irq(irqno,NULL);
//释放GPIO编号
gpiod_set_value(desc,0);
gpiod_put(desc);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include <sys/ioctl.h>
int main(int argc, char const *argv[])
{
char buf[128]={};
int fd=open("/dev/myshebeishu",O_RDWR);
if(fd<0)
{
printf("设备文件打开失败\n");
exit(-1);
}
printf("设备文件打开成功\n");
while(1)
{
read(fd,buf,sizeof(buf));
printf("%s\n",buf);
}
close(fd);
return 0;
}