驱动 day7

#include 
#include 
#include 
#include 
#include
#include
#include
unsigned int major; // 设备号
char kbuf[128] = {0};

struct gpio_desc *gpiono_1;
struct gpio_desc *gpiono_2;
struct gpio_desc *gpiono_3;

struct device_node *dnode;

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;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *iof)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *iof)
{
    int ret;
    // 从用户拷贝
    if (size > sizeof(kbuf))
    {
        size = sizeof(kbuf);
    }
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret)
    {
        printk("copy from user filed\n");
        return -EIO;
    }
    switch (kbuf[0])
    {
        case '1':   gpiod_set_value(gpiono_1,1); break;
        case '2':   gpiod_set_value(gpiono_2,1); break;
        case '3':   gpiod_set_value(gpiono_3,1); break;
        case '4':   gpiod_set_value(gpiono_1,0); break;
        case '5':   gpiod_set_value(gpiono_2,0); break;
        case '6':   gpiod_set_value(gpiono_3,0); break;           
    }
    return 0;
}
// 定义一个操作方法结构体变量并且初始化
struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .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编号,并向内核申请该gpio编号,同时设置该gpio编号的默认状态
    gpiono_1=gpiod_get_from_of_node(dnode,"led1",0,GPIOD_OUT_LOW,NULL);
    gpiono_2=gpiod_get_from_of_node(dnode,"led2",0,GPIOD_OUT_LOW,NULL);
    gpiono_3=gpiod_get_from_of_node(dnode,"led3",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono_1) | IS_ERR(gpiono_2) | IS_ERR(gpiono_3))
    {
        printk("解析设备号失败\n");
        return -PTR_ERR(gpiono_1);
    }
   printk("申请gpio编号成功\n");
    return 0;
}
static void __exit mycdev_exit(void)
{
     //灭灯
    gpiod_set_value(gpiono_1,0);
    gpiod_set_value(gpiono_2,0);
    gpiod_set_value(gpiono_3,0);

    //释放gpio编号
    gpiod_put(gpiono_1);
    gpiod_put(gpiono_2);
    gpiod_put(gpiono_3);
    // 字符设备驱动的注销
    unregister_chrdev(major, "mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

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

int main(int argc, const char *argv[])
{
    char buf[128] = {0};
    int fd = open("/dev/mycdev", O_RDWR); // 为了调用驱动中的mycdev_open
    if (fd < 0)
    {
        printf("打开设备文件失败");
        exit(-1);
    }
    while (1)
    {
        printf("请输入控制码选择灯控制开关)\n");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) - 1] = '\0'; // 用'\0'替代\n
        write(fd, buf, sizeof(buf));
    }

    close(fd);
    return 0;
}

你可能感兴趣的:(学习日报,驱动,c语言)