如下要求
a.应用程序通过阻塞的io模型来读取number变量的值b.number是内核驱动中的一个变量
c.number的值随着按键按下而改变(按键中断)
例如number=O按下按键number=1 ,再次按下按键number=Od .在按下按键的时候需要同时将led1的状态取反
e .驱动中需要编写字符设备驱动
f.驱动中需要自动创建设备节点
g.这个驱动需要的所有设备信息放在设备树的同一个节点中
驱动代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/of_irq.h>
#include <linux/uaccess.h>
int major; // 主设备号
struct class *cls;
struct device *dev;
struct gpio_desc *gpiono; // 存储gpio相关信息
unsigned int irqno; // 软中断号
wait_queue_head_t wq_head; // 等待队列头
unsigned int condition = 0;
unsigned int number = 0;
char kbuf[10] = "";
// 中断处理函数
irqreturn_t irq_handler(int irqno, void *arg)
{
number = gpiod_get_value(gpiono);
number = !number;
gpiod_set_value(gpiono, number);
condition = 1;
wake_up_interruptible(&wq_head);
return IRQ_HANDLED;
}
// 封装操作方法
int my_open(struct inode *inode, struct file *file)
{
return 0;
}
ssize_t my_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
int ret;
if (file->f_flags & O_NONBLOCK) // 不阻塞
{
return -EINVAL;
}
else // 阻塞
{
wait_event_interruptible(wq_head, condition);
}
if (number == 1)
kbuf[0] = '1';
else if (number == 0)
kbuf[0] = '0';
ret = copy_to_user(ubuf, kbuf, sizeof(kbuf));
if (ret < 0)
{
printk("copy_to_user failed\n");
return -1;
}
condition = 0;
return 0;
}
int my_close(struct inode *inode, struct file *file)
{
return 0;
}
// 定义并初始化操作方法结构体
struct file_operations fops = {
.open = my_open,
.read = my_read,
.release = my_close,
};
// probe函数,设备匹配成功后执行
int pdrv_probe(struct platform_device *pdev)
{
int ret;
printk("设备与驱动匹配成功\n");
// 初始化等待队列头
init_waitqueue_head(&wq_head);
// 注册字符设备驱动
major = register_chrdev(0, "myplatform", &fops);
if (major < 0)
{
printk("注册字符设备驱动失败\n");
ret = major;
goto out1;
}
printk("注册字符设备驱动成功\n");
// 向上提交设备节点目录
cls = class_create(THIS_MODULE, "myplatform");
if (IS_ERR(cls))
{
printk("向上提交设备节点目录失败\n");
ret = -PTR_ERR(cls);
goto out2;
}
printk("向上提交设备节点目录成功\n");
// 向上提交设备节点信息
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myplatform0");
if (IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
ret = -PTR_ERR(dev);
goto out3;
}
printk("向上提交设备节点信息成功\n");
// 获取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 out4;
}
printk("获取gpio信息成功\n");
// 获取软中断号
irqno = platform_get_irq(pdev, 0);
if (irqno < 0)
{
printk("获取中断资源失败\n");
ret = irqno;
goto out5;
}
printk("获取中断类型资源成功,irqno = %d\n", irqno);
// 注册中断
ret = request_irq(irqno, irq_handler, IRQF_TRIGGER_FALLING, "myplatform", NULL);
if (ret < 0)
{
printk("注册中断失败\n");
goto out5;
}
printk("注册中断成功\n");
return 0;
out5:
gpiod_put(gpiono);
out4:
device_destroy(cls, MKDEV(major, 0));
out3:
class_destroy(cls);
out2:
unregister_chrdev(major, "myplatform");
out1:
return ret;
}
// remove函数,设备与驱动分离时执行
int pdrv_remove(struct platform_device *pdev)
{
// 灭灯
gpiod_set_value(gpiono, 0);
free_irq(irqno, NULL);
// 释放gpio信息
gpiod_put(gpiono);
device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
unregister_chrdev(major, "myplatform");
return 0;
}
// 设备树匹配的表
struct of_device_id oftable[] =
{
{.compatible = "hqyj,myplatform"},
{.compatible = "hqyj,myplatform1"},
{},
};
// platform驱动信息对象结构体
struct platform_driver pdrv = {
.probe = pdrv_probe,
.remove = pdrv_remove,
.driver = {
.name = "myplatform",
.of_match_table = oftable,
},
};
// 一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
应用程序
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
char buf[10] = "";
int ret;
int fd = open("/dev/myplatform0",O_RDWR);
if(fd < 0)
{
printf("打开设备文件失败\n");
return -1;
}
printf("打开设备文件成功\n");
while(1)
{
ret = read(fd,buf,sizeof(buf));
if(ret < 0)
{
printf("读取失败\n");
return -1;
}
printf("number = %c\n",buf[0]);
}
close(fd);
return 0;
}