作业:通过platform总线驱动实现
a.应用程序通过阻塞的io模型来读取number变量的值
b.number是内核驱动中的一个变量
c.number的值随着按键按下而改变(按键中断) 例如number=0 按下按键number=1 ,再次按下按键number=0
d.在按下按键的时候需要同时将led1的状态取反
e.驱动中需要编写字符设备驱动
f.驱动中需要自动创建设备节点
g.这个驱动需要的所有设备信息放在设备树的同一个节点中
驱动程序:my_platform.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of_gpio.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0;
dev_t devno;
module_param(major, uint, 0664); //方便命令行传递major的值
struct class *cls;
struct device *dev;
struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono;
wait_queue_head_t wq_head; //定义一个等待队列头
unsigned int number = 0;
unsigned int condition = 0;
//定义中断处理函数
irqreturn_t key_handler(int irq, void *dev)
{
gpiod_set_value(gpiono, !gpiod_get_value(gpiono));
if (number == 0)
{
number = 1;
condition = 1;
wake_up_interruptible(&wq_head);
}
else
{
number = 0;
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;
//判断IO方式
if (file->f_flags & O_NONBLOCK) //非阻塞
{
}
else
{ //阻塞
wait_event_interruptible(wq_head, condition); //先检查condition再将进程休眠
}
ret = copy_to_user(ubuf, &number, 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)
{
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,
.release = mycdev_close,
};
//封装probe函数
int pdrv_probe(struct platform_device *pdev)
{
//初始化等待队列
init_waitqueue_head(&wq_head);
int ret;
//1.为字符设备驱动对象申请空间
cdev = cdev_alloc();
if (cdev == NULL)
{
printk("申请字符设备驱动对象空间失败\n");
ret = -EFAULT;
goto out1;
}
printk("申请字符设备驱动对象空间成功\n");
//2.初始化字符设备驱动对象
cdev_init(cdev, &fops);
printk("初始化字符设备驱动对象成功\n");
//3.申请设备号
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");
//4.注册字符设备驱动对象
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");
//platform
//获取MEM类型的资源
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL)
{
printk("获取MEM类型资源失败\n");
return -ENXIO;
}
//获取中断类型的资源
irqno = platform_get_irq(pdev, 0);
if (irqno < 0)
{
printk("获取中断类型资源失败\n");
return -ENXIO;
}
//设备树匹配成功后,设备树结点指针可以通过pdev->dev.of_node获取
//基于设备树节点信息获取gpio_desc对象指针
gpiono = gpiod_get_from_of_node(pdev->dev.of_node, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono))
{
printk("解析GPIO管脚信息失败\n");
return -ENXIO;
}
printk("解析GPIO管脚信息成功\n");
//注册按键中断
ret = request_irq(irqno, key_handler, IRQF_TRIGGER_FALLING, "key_init", NULL);
if (ret < 0)
{
printk("注册按键%d中断失败\n", i);
return ret;
}
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;
}
//封装remove函数
int pdrv_remove(struct platform_device *pdev)
{
//注销中断
free_irq(irqno, NULL);
//释放GPIO信息
gpiod_put(gpiono);
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);
return 0;
}
//构建设备树匹配表
struct of_device_id oftable[] = {
{.compatible = "hqyj,myplatform"},
{}, //防止数组越界
};
//定义platform驱动信息对象并初始化
struct platform_driver pdrv = {
.probe = pdrv_probe,
.remove = pdrv_remove,
.driver = {
.name = "bbbbb",
.of_match_table = oftable, //用于设备树匹配
},
};
//一键注册platform宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
应用程序:test.c
#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(){
unsigned int number;
int fd = open("/dev/myled0", O_RDWR);
if(fd < 0){
printf("设备文件打开失败\n");
exit(-1);
}
while(1){
read(fd, &number, sizeof(number));
printf("number = %d\n", number);
}
close(fd);
return 0;
}