驱动第六次作业

 应用test.t

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char const *argv[])
{

    int number;
    int fd = open("/dev/myled0", O_RDWR);
    if (fd < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    printf("打开设备文件成功\n");
    while (1)
    {

        read(fd, &number, sizeof(number));
        printf("number:%d\n", number);
    }
    close(fd);
    return 0;
}

驱动pdrv.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/interrupt.h>

struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono;

struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0;
unsigned int number = 0;
unsigned int condition = 0;
struct class *cls;
struct device *dev;
dev_t devno;
// 定义等待队列头
wait_queue_head_t wq_head;
/*******************************************************/
// 中断处理函数
irqreturn_t myirq_handler(int irqno, void *dev_id)
{
    number = number ^ 1;
    gpiod_set_value(gpiono, number);
    printk("key1 interrupt\n");
    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 __user *ubuf, size_t size, loff_t *lof)
{
    int ret;
    if (sizeof(number) < size)
        size = sizeof(number);
    wait_event_interruptible(wq_head, condition);

    printk("******number=%d\n", number);
    ret = copy_to_user(ubuf, (void *)&number, size);
    if (ret)
    {
        printk("copy_to_user failed");
        return -EIO;
    }

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    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,

};

/***********************************************************/

int chrdev_re(void)
{
    int ret;
    // 1.申请字符设备驱动对象空间
    cdev = cdev_alloc();
    if (cdev == NULL)
    {
        printk("申请字符设备驱动对象空间失败\n");
        ret = -EFAULT;
        goto out1;
    }
    printk("申请字符设备驱动对象空间成功\n");
    // 2.字符设备驱动对象部分初始化
    cdev_init(cdev, &fops);
    // 3.申请设备号
    if (major > 0)
    {
        ret = register_chrdev_region(MKDEV(major, minor), 1, "myled");
        if (ret)
        {
            printk("申请设备号失败\n");
            goto out2;
        }
    }
    else
    {
        ret = alloc_chrdev_region(&devno, minor, 1, "myled");
        if (ret)
        {
            printk("申请设备号失败\n");
            goto out2;
        }
        major = MAJOR(devno);
        minor = MINOR(devno);
    }
    printk("申请设备号成功\n");
    // 注册字符设备驱动对象
    ret = cdev_add(cdev, MKDEV(major, minor), 1);
    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");
    // 向上提交设备节点信息
    dev = device_create(cls, NULL, MKDEV(major, minor), NULL, "myled0");
    if (IS_ERR(dev))
    {
        printk("向上提交设备节点信息失败\n");
        ret = -PTR_ERR(dev);
        goto out5;
    }
    return 0;
out5:
    device_destroy(cls, MKDEV(major, minor));
    class_destroy(cls);
out4:
    cdev_del(cdev);
out3:
    unregister_chrdev_region(MKDEV(major, minor), 1);
out2:
    kfree(cdev);
out1:
    return ret;
}

/************************************************************/
// probe函数,匹配设备成功执行
int pdrv_probe(struct platform_device *pdev)
{
    int ret;

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);

    // 初始化等待队列头
    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);
    irqno = platform_get_irq(pdev, 0);
    if (irqno < 0)
    {
        printk("获取中断资源失败\n");
        return irqno;
    }
    printk("中断类型资源为%d\n", irqno);

    ret = request_irq(irqno, myirq_handler, IRQF_TRIGGER_FALLING, "key1", NULL);
    if (ret)
    {
        printk("注册驱动失败");
        return ret;
    }
    printk("key中断注册成功\n");

    // 获取gpio信息
    // pdev->dev.of_node 设备树匹配之后会把设备树节点结构体首地址赋值给dev的of_node成员
    gpiono = gpiod_get_from_of_node(pdev->dev.of_node, "led1", 0, GPIOD_OUT_LOW, NULL);
    if (IS_ERR(gpiono))
    {
        printk("解析GPIO信息失败\n");
        return PTR_ERR(gpiono);
    }
    // 亮灯
    gpiod_set_value(gpiono, 0);

    // 申请设备号并注册
    chrdev_re();

    return 0;
}
/*******************************************************************/
// remove设备和驱动分离时执行
int pdrv_remove(struct platform_device *pdrv)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    // 灭灯
    gpiod_set_value(gpiono, 0);
    // 释放gpio信息
    gpiod_put(gpiono);
    // 1.销毁设备节点信息
    device_destroy(cls, MKDEV(major, minor));
    // 2.销毁目录
    class_destroy(cls);
    // 3.注销字符设备驱动对象
    cdev_del(cdev);
    // 4.释放设备号
    unregister_chrdev_region(MKDEV(major, minor), 1);
    // 5.释放字符设备驱动对象空间
    kfree(cdev);

    // 注销中断号
    free_irq(irqno, NULL);
    return 0;
}

struct of_device_id oftable[] = {
    {
        .compatible = "hqyj,myplatform",
    },
    {
        .compatible = "hqyj,myplatform1",
    },
    {},
};

/*
// 构建名字表
struct platform_device_id idtable[] = {
    {"aaaa", 0},
    {"bbbb", 1},
    {"cccc", 2},
    {"dddd", 3},
    {}, // 防止越界
};
*/
struct platform_driver pdrv = {
    .probe = pdrv_probe,
    .remove = pdrv_remove,
    .driver = {
        .name = "aaaa",
        .of_match_table = oftable, // 设置设备匹配
    },
    //    .id_table = idtable, // 设置名字表匹配
};
/*
static int __init mycdev_init(void)
{
    platform_driver_register(&pdrv);
    return 0;
}

static void __exit mycdev_exit(void)
{
    platform_driver_unregister(&pdrv);
}

module_init(mycdev_init);
module_exit(mycdev_exit);
*/
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

现象

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值