platform驱动实现
test.c
#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[])
{
int num;
int fd = open("/dev/myplatform", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while (1)
{
read(fd,&num,sizeof(num));//读取数据
printf("num = %d\n",num);
}
close(fd);
return 0;
}
myplatform.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#define CNAME "myplatform"
struct resource *res;
unsigned int irqno;
unsigned int major = 0;
struct class *cls;
struct device *dev;
struct gpio_desc *gpiono;
int num = 0;
// 定义等待队列头
wait_queue_head_t wq_head;
unsigned int condition = 0;
// 中断处理函数
irqreturn_t myirq_handler(int irqno, void *dev_id)
{
gpiod_set_value(gpiono, !gpiod_get_value(gpiono));
num = !num;
condition = 1; // 表示硬件数据就绪
wake_up_interruptible(&wq_head); // 唤醒休眠的进程
return IRQ_HANDLED;
}
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)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if (file->f_flags & O_NONBLOCK)
{
// 非阻塞
return -EINVAL;
}
else
{
// 阻塞
ret = wait_event_interruptible(wq_head, condition); // 将进程切换为休眠
if (ret < 0)
{
printk("receive signal......\n");
return ret;
}
}
if (sizeof(num) < size)
size = sizeof(num);
ret = copy_to_user(ubuf, &num, size);
if (ret)
{
printk("copy_to_user failed\n");
return -EIO;
}
condition = 0; // 表示下一次硬件数据没有准备好
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,
.release = mycdev_close,
};
// probe函数,匹配设备成功执行
int pdrv_probe(struct platform_device *pdev)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
// 注册字符设备驱动
major = register_chrdev(0, CNAME, &fops);
if (major < 0)
{
printk("注册字符设备驱动失败\n");
ret = major;
goto ERR1;
}
printk("注册字符设备驱动成功\n");
// 创建设备节点
// 向上提交目录
cls = class_create(THIS_MODULE, CNAME);
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret = PTR_ERR(cls);
goto ERR2;
}
printk("向上提交目录成功\n");
// 向上提交设备节点信息
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, CNAME);
if (IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
ret = PTR_ERR(dev);
goto ERR3;
}
printk("向上提交设备节点信息成功\n");
// 初始化等待队列头
init_waitqueue_head(&wq_head);
// 获取设备信息
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL)
{
printk("获取资源失败\n");
return -ENXIO;
}
printk("获取资源信息成功 %x\n", res->start);
// 获取gpio结构体信息
gpiono = gpiod_get_from_of_node(pdev->dev.of_node, "led1", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono))
{
printk("解析GPIO信息失败\n");
ret = PTR_ERR(gpiono);
goto ERR4;
}
printk("解析GPIO信息成功\n");
// 获取软中断号
irqno = platform_get_irq(pdev, 0);
if (irqno < 0)
{
printk("获取中断资源失败\n");
ret = irqno;
goto ERR5;
}
printk("中断类型资源为%d\n", irqno);
// 注册中断
ret = request_irq(irqno, myirq_handler, IRQF_TRIGGER_FALLING, CNAME, NULL);
if (ret)
{
printk("注册中断失败\n");
goto ERR5;
}
printk("注册中断成功\n");
return 0;
ERR5:
gpiod_put(gpiono);
ERR4:
device_destroy(cls, MKDEV(major, 0));
ERR3:
class_destroy(cls);
ERR2:
unregister_chrdev(major, CNAME);
ERR1:
return ret;
}
// remove 设备和驱动分离时执行
int pdrv_remove(struct platform_device *pdev)
{
// 灭灯
gpiod_set_value(gpiono, 0);
// 释放gpio编号
gpiod_put(gpiono);
// 注销中断
free_irq(irqno, NULL);
// 销毁设备节点信息
device_destroy(cls, MKDEV(major, 0));
// 销毁目录信息
class_destroy(cls);
// 销毁字符设备驱动
// 字符设备驱动的注销
unregister_chrdev(major, CNAME);
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
// 构建名字表
struct of_device_id oftable[] = {
{
.compatible = "zyh,myplatform",
},
{
.compatible = "zyh,myplatform1",
},
{},
};
struct platform_driver pdrv = {
.probe = pdrv_probe,
.remove = pdrv_remove,
.driver = {
.name = "aaaaa",
.of_match_table = oftable, // 设置设备树匹配
},
};
// 一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
实验现象