platform架构完成,在应用层通过ioctl控制LED灯流水,当按键KEY1按下让风扇转动

 实现过程:

驱动代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

// 开关灯命令码
#define LED1 _IOW('a', 1, int)
#define LED2 _IOW('a', 2, int)
#define LED3 _IOW('a', 3, int)

struct cdev *cdev;      // 驱动设备对象
dev_t devno;            // 设备号
unsigned int major = 0; // 动态态指定主设备号
unsigned int minor = 0; // 次设备号

struct class *cls;  // 目录信息
struct device *dev; // 设备节点信息

struct device_node *led_node, *interrupt_node, *fnode; // 灯节点,中断节点,灯节点的子节点
struct gpio_desc *gpio_num[4] = {0};                   // gpio编号
char *led[4] = {"led1", "led2", "led3", "fan"};        // 键名
struct resource *res;                                  // 设备资源
int irq_num;                                           // 按键中断号
int i, ret;

// 中断处理函数
irqreturn_t irq_handler(int irq_num, void *arg)
{
    gpiod_set_value(gpio_num[3], !gpiod_get_value(gpio_num[3]));
    return IRQ_HANDLED;
}

int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long args)
{
    switch (cmd)
    {
    case LED1:
        gpiod_set_value(gpio_num[0], !gpiod_get_value(gpio_num[0]));
        break;
    case LED2:
        gpiod_set_value(gpio_num[1], !gpiod_get_value(gpio_num[1]));
        break;
    case LED3:
        gpiod_set_value(gpio_num[2], !gpiod_get_value(gpio_num[2]));
        break;
    default:
        break;
    }
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
const struct file_operations fops = {
    .open = mycdev_open,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};

int pdrv_probe(struct platform_device *pdev)
{
    printk("设备安装\n");
    // 1.分配对象
    cdev = cdev_alloc();
    if (cdev == NULL)
    {
        printk("分配对象空间失败\n");
        ret = -ENOMEM;
        goto ERR1;
    }
    printk("分配对象空间成功\n");
    // 2.初始化驱动对象
    cdev_init(cdev, &fops);
    // 申请设备号,动态申请
    ret = alloc_chrdev_region(&devno, minor, 3, "mycdev");
    if (ret)
    {
        printk("动态申请设备号失败\n");
        goto ERR2;
    }
    major = MAJOR(devno); // 获取主设备号
    minor = MINOR(devno); // 获取次设备号

    // 3.注册对象
    ret = cdev_add(cdev, MKDEV(major, minor), 3);
    if (ret)
    {
        printk("驱动对象注册进内核失败\n");
        goto ERR3;
    }
    printk("驱动对象注册进内核成功\n");
    // 向上提交目录
    cls = class_create(THIS_MODULE, "mycdev");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        goto ERR4;
    }
    printk("向上提交目录成功\n");
    // 4.向上提交设备节点
    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "mycdev%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交节点信息失败\n");
            goto ERR5;
        }
    }
    printk("向上提交设备节点成功\n");

    irq_num = platform_get_irq(pdev, 0);
    if (irq_num < 0)
    {
        printk("获取中断资源失败\n");
        return -ENODATA;
    }
    printk("获取中断资源:%d\n", irq_num);
    request_irq(irq_num, irq_handler, IRQF_TRIGGER_FALLING, "myirq", NULL);
    printk("注册中断号成功\n");

    for (i = 0; i < 4; i++)
    {
        // 解析编号
        gpio_num[i] = gpiod_get_from_of_node(pdev->dev.of_node, led[i], 0, GPIOD_OUT_LOW, NULL);
        if (IS_ERR(gpio_num[i]))
        {
            printk("解析编号gpio_num%d失败\n", i);
            return PTR_ERR(gpio_num[i]);
        }
        printk("解析编号gpio_num%d成功\n", i);
    }
    return 0;

ERR5:
    // 将前面提交成功的设备信息销毁
    for (--i; i >= 0; i--)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    // 销毁目录
    class_destroy(cls);
ERR4:
    cdev_del(cdev);
ERR3:
    unregister_chrdev_region(MKDEV(major, minor), 3);
ERR2:
    kfree(cdev);
ERR1:
    return 0;
}

int pdrv_remove(struct platform_device *pdev)
{
    // 释放资源
    printk("设备卸载\n");
    for (i = 0; i < 4; i++)
    {
        gpiod_set_value(gpio_num[i], 0);
    }

    for (i = 0; i < 4; i++)
    {
        gpiod_put(gpio_num[i]);
    }
    free_irq(irq_num, NULL);

    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,platform"},
    {}, // 防止越界
};

// 初始化驱动对象
struct platform_driver pdrv = {
    .probe = pdrv_probe,
    .remove = pdrv_remove,
    .driver = {
        .name = "mppdev",
        .of_match_table = oftable,
    },
};
// 一键宏注册销毁对象
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

 

应用层代码:

#include 
#include 
#include 
#include 
#include 
#include 

// 灯号命令码
#define LED1 _IOW('a', 1, int)
#define LED2 _IOW('a', 2, int)
#define LED3 _IOW('a', 3, int)

int main(int argc, const char *argv[])
{
    int fd;
    fd = open("/dev/mycdev0", O_RDWR);
    if (fd < 0)
    {
        printf("打开失败\n");
        return -1;
    }
    while (1)
    {
        ioctl(fd, LED1);
        usleep(200000);
        ioctl(fd, LED2);
        usleep(200000);
        ioctl(fd, LED3);
        usleep(200000);
    };
    close(fd);
    return 0;
}

你可能感兴趣的:(linux,运维,服务器)