驱动day7

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "led.h"

/*  myleds{
    led1=<&gpioe 10 0>;
    led2=<&gpiof 10 0>;
    led3=<&gpioe 8 0>;
};*/

int gpiono1;
int gpiono2;
int gpiono3;

struct device_node *dnode;

int major;
struct class *cls;
struct device *dev;

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 arg)
{
    int which;
    int ret = copy_from_user(&which, (void *)arg, 4);
    if (ret)
    {
        printk("copy_from_user failed\n");
        return ret;
    }
    switch (cmd)
    {
    case LED_ON: // 开灯
        switch (which)
        {
        case 1:
            // 开灯
            gpio_set_value(gpiono1, 1);
            break;
        case 2:
            gpio_set_value(gpiono2, 1);
            break;
        case 3:
            gpio_set_value(gpiono3, 1);
            break;
        }

        break;
    case LED_OFF: // 关灯
        switch (which)
        {
        case 1:
            // 灭灯
            gpio_set_value(gpiono1, 0);
            break;
        case 2:
            gpio_set_value(gpiono2, 0);
            break;
        case 3:
            gpio_set_value(gpiono3, 0);
            break;
        }

        break;
    }
    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,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};

static int __init mycdev_init(void)
{
    // 字符设备驱动注册
    major = register_chrdev(0, "mycdev", &fops);
    if (major < 0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功:major=%d\n", major);
    // 向上提交目录
    cls = class_create(THIS_MODULE, "mycdev");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");
    // 向上提交设备节点信息
    dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mycdev");
    if (IS_ERR(dev))
    {
        printk("向上提交设备节点失败\n");
        return -PTR_ERR(dev);
    }
    printk("向上提交设备节点成功\n");

    // 解析设备树节点
    dnode = of_find_node_by_name(NULL, "myleds");
    if (dnode == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENOMEM;
    }
    printk("解析设备树节点成功\n");
    // LED1
    //  根据设备树节点解析出gpio编号
    gpiono1 = of_get_named_gpio(dnode, "led1", 0);
    if (gpiono1 < 0)
    {
        printk("解析gpio1编号失败\n");
        return -EIO;
    }
    printk("解析gpio1编号成功\n");
    // 申请gpio编号登记
    int ret1 = gpio_request(gpiono1, NULL);
    if (ret1)
    {
        printk("gpio1编号申请失败\n");
        return -EIO;
    }
    printk("gpio1编号申请成功%d\n", gpiono1);
    // 设置管脚为输出
    gpio_direction_output(gpiono1, 0);

    // LED2
    //  根据设备树节点解析出gpio编号
    gpiono2 = of_get_named_gpio(dnode, "led2", 0);
    if (gpiono2 < 0)
    {
        printk("解析gpio2编号失败\n");
        return -EIO;
    }
    printk("解析gpio2编号成功\n");
    // 申请gpio编号登记
    int ret2 = gpio_request(gpiono2, NULL);
    if (ret2)
    {
        printk("gpio2编号申请失败\n");
        return -EIO;
    }
    printk("gpio2编号申请成功%d\n", gpiono2);
    // 设置管脚为输出
    gpio_direction_output(gpiono2, 0);

    // LED3
    //  根据设备树节点解析出gpio编号
    gpiono3 = of_get_named_gpio(dnode, "led3", 0);
    if (gpiono3 < 0)
    {
        printk("解析gpio3编号失败\n");
        return -EIO;
    }
    printk("解析gpio3编号成功\n");
    // 申请gpio编号登记
    int ret3 = gpio_request(gpiono3, NULL);
    if (ret3)
    {
        printk("gpio3编号申请失败\n");
        return -EIO;
    }
    printk("gpio3编号申请成功%d\n", gpiono3);
    // 设置管脚为输出
    gpio_direction_output(gpiono3, 0);

    return 0;
}
static void __exit mycdev_exit(void)
{
    // 灭灯
    gpio_set_value(gpiono1, 0);
    // 释放gpio编号
    gpio_free(gpiono1);
    gpio_free(gpiono2);
    gpio_free(gpiono3);
    // 销毁设备节点信息
    device_destroy(cls, MKDEV(major, 0));
    // 销毁目录空间
    class_destroy(cls);
    // 注销字符设备驱动
    unregister_chrdev(major, "mychrdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

驱动day7_第1张图片

 

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