[Use the driver code to achieve the following requirements: the application reads the value of the number variable through the blocked io model]

Drive application layer code

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

int main(int argc, char const *argv[])
{<!-- -->
    int a, b;
    char buf[128] = {<!-- -->0};

    int fd_led1 = open("/dev/led0", O_RDWR); //corresponding key1 button
    if (fd_led1 < 0)
    {<!-- -->
        printf("Failed to open LED1 device file\\
");
        exit(-1);
    }

    while (1)
    {<!-- -->
        // read from terminal
        printf("Please select KEY1 light function\\
");
        printf("0(off) 1(on)>");
        scanf("%d", &a);
        printf("Please enter the key to be controlled\\
");
        printf("1(KEY1) 2(KEY2) 3(KEY3)>");
        scanf("%d", &b);
        if (a == 1) // turn on the light
        {<!-- -->
switch (b)
{<!-- -->
case 1:
while(1){<!-- -->
           //ioctl(fd_led1, LED_ON, b);
           //ioctl(fd_led1, LED_ON, b);
//memset(buf,0,sizeof(buf));//clear
       read(fd_led1,buf,sizeof(buf));//read data
        printf("number:%c\\
",buf[0]);
sleep(1);
}

break;
case 2:
           //ioctl(fd_led2, LED_ON, b);
break;
case 3:
           // ioctl(fd_led3, LED_ON, b);
break;
}
        }
        else if (a == 0) // turn off the light
        {<!-- -->
switch (b)
{<!-- -->
case 1:
           //ioctl(fd_led1, LED_OFF, b);
           //ioctl(fd_led1, LED_ON, b);
break;
case 2:
           //ioctl(fd_led2, LED_OFF, b);
break;
case 3:
           //ioctl(fd_led3, LED_OFF, b);
break;
}
        }
    }
    close(fd_led1);
// close(fd_led2);
// close(fd_led3);
    return 0;
}

Driver code

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include "head.h"
struct cdev *cdev; //first address of character device space
unsigned int major=500; //Static application device number
unsigned int minor=0;//The initial value of the minor device number
dev_t devno; //Dynamic application device number
struct class *cls; //Receive the address of the registration structure
struct device *dev; //device number

/*mykeys{
    interrupt-parent=< & gpiof>;
    interrupts=<9 0>,<7 0>,<8 0>; //9 indicates the index information when referencing the interrupted parent node 0 indicates the default setting
};*/

//unsigned int irqno;//a device
int irqno; //interrupt
struct gpio_desc *gpiono; //gpio
//struct gpio_desc *gpiono2;
//struct gpio_desc *gpiono3;
struct device_node *dnode;

char number = 0; //define number
char kbuf[128] = {<!-- -->0}; //The buffer in the driver

int mycdev_open(struct inode *inode, struct file *file)
{<!-- -->
// unsigned int aaa = MINOR(inode->i_rdev); // get the secondary device number aaa
// file->private_data = (void *)aaa;

    printk("%s:%s:%d\\
", __FILE__, __func__, __LINE__);
    return 0;
}

// interrupt handler
irqreturn_t myirq_handler(int irqno, void *dev_id)
{<!-- -->
printk("key1 interrupt\\
");
//Invert the state of the light
gpiod_set_value(gpiono, !gpiod_get_value(gpiono));

//Send the data of number to the kbuf buffer
number = !number;

/*
    unsigned int arg = (unsigned int) dev_id;
    switch(arg)
    {
        case 0:
            printk("key1 interrupt\\
");
//Invert the state of the light
gpiod_set_value(gpiono, !gpiod_get_value(gpiono));

//Send the data of number to the kbuf buffer
number = !number;

            break;
        case 1:
            printk("key2 interrupt\\
");
//Invert the state of the light
            break;
        case 2:
            printk("key3 interrupt\\
");
//Invert the state of the light
            break;
    }*/
    return IRQ_HANDLED;
}

//read of the application callback
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{<!-- -->
    int ret;
    if(sizeof(kbuf)<size)
    size=sizeof(kbuf);
// wait_event_interruptible(wq_head,condition);//Switch the process to sleep
//
kbuf[0] = number;
printk("****************************************%c\\
", number);
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {<!-- -->
        printk("copy_to_user filed\\
");
        return -EIO;
    }

    printk("%s:%s:%d\\
", __FILE__, __func__, __LINE__);
// condition=0;//Indicates that the next hardware data is not ready
    return 0;
}

/*
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{

myirq_handler(irqno, NULL); // Button 1 soft interrupt
    return 0;
}
*/
int mycdev_close(struct inode *inode, struct file *file)
{<!-- -->
    printk("%s:%s:%d\\
", __FILE__, __func__, __LINE__);
    return 0;
}

// Define the operation method structure variable and assign it
struct file_operations fops = {<!-- -->

    .open = mycdev_open,
   // .unlocked_ioctl = mycdev_ioctl,
.read = mycdev_read,
    .release = mycdev_close,
};

static int __init mycdev_init(void) //register address mapping and initialization
{<!-- -->
    int ret; //ret returns an error code
int ret1;
\t
    //1. Allocate character device driver object space cdev_alloc
    cdev=cdev_alloc(); //The first address of character device space
    if(cdev==NULL)
    {<!-- -->
        printk("Failed to apply for character device driver object space\\
");
        ret=-EFAULT;
        goto out1;
    }
    printk("Character device driver object application is successful\\
");

    //2. Partial initialization of the character device driver object cdev_init
    cdev_init(cdev, & fops);

    //3. Apply for device number register_chrdev_region/alloc_chrdev_region
    if(major>0)//Static application device number
    {<!-- -->
        ret=register_chrdev_region(MKDEV(major,minor),1,"led0"); //The device number needs to be combined, the number of secondary devices, and the device file name
        if(ret)
        {<!-- -->
            printk("**********Failed to statically specify the device number**********\\
");
            goto out2;
        }
    }
    else//Dynamic application device number
    {<!-- -->
        ret=alloc_chrdev_region( & amp;devno,minor,1,"led0"); //Dynamically apply for device number, minor device number, device quantity, file name
         if(ret)
        {<!-- -->
            printk("Dynamic application for device number failed\\
");
            goto out2;
        }
        major=MAJOR(devno); //Get the major device number according to the device number
        minor=MINOR(devno); //Get the minor device number according to the device number
    }
    printk("Apply for device number successfully\\
");

    //4. Register character device driver object cdev_add()
    ret=cdev_add(cdev,MKDEV(major,minor),1); //Character device, device number, device quantity
    if(ret)
    {<!-- -->
        printk("Failed to register character device driver object\\
");
        goto out3;
    }
    printk("Register character device driver object successfully\\
");

    //5. Submit directory up
    cls=class_create(THIS_MODULE,"led0"); //Pointer to itself, file name
    if(IS_ERR(cls))
    {<!-- -->
        printk("Failed to submit directory up\\
");
        ret=-PTR_ERR(cls);
        goto out4;
    }
    printk("submit directory successfully\\
");

    //6. Submit the device node up
    dev=device_create(cls,NULL,MKDEV(major,0),NULL,"led0"); //create device node
    if(IS_ERR(dev))
    {<!-- -->
        printk("Failed to submit node information up\\
");
        ret=-PTR_ERR(dev);
        goto out5;
    }
    printk("Successful submission of device node information\\
");

//gpio subsystem
    // 1. Parse the device tree node
   dnode=of_find_node_by_name(NULL,"myleds");
   if(dnode==NULL)
   {<!-- -->
    printk("Failed to parse device tree node\\
");
    return -ENOMEM;
   }
   printk("parse device tree node successfully\\
");
   // 2. Analyze the gpio number according to the device tree node and apply for the corresponding led
    gpiono=gpiod_get_from_of_node(dnode,"led1",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {<!-- -->
        printk("Failed to parse the device number\\
");
        return -PTR_ERR(gpiono);
    }
   printk("Apply for gpio number successfully\\
");
   // 3. Turn on the light
   gpiod_set_value(gpiono,1);


//soft interrupt
   //Parse the device tree node

    dnode=of_find_node_by_name(NULL,"mykeys");
    if(dnode==NULL)
    {<!-- -->
        printk("Failed to parse device tree node\\
");
        return -ENXIO;
    }
    printk("Device tree node parsed successfully\\
");
    
        // Get soft interrupt number
        irqno = irq_of_parse_and_map(dnode, 0);
        if (!irqno)
        {<!-- -->
            printk("Soft interrupt number acquisition failed\\
");
            return -ENOMEM;
        }
        printk("Soft interrupt number obtained successfully irqno=%d\\
", irqno);
        // register interrupt
 // ret1 = request_irq(irqno, myirq_handler, IRQF_TRIGGER_FALLING, "key", (void *)1);
        ret1 = request_irq(irqno, myirq_handler, IRQF_TRIGGER_FALLING, "key", NULL);
        if (ret1)
        {<!-- -->
            printk("Failed to register driver\\
");
            return ret1;
        }
        printk("key1 interruption registration successful\\
");
  
    return 0;

out5:
    //Destroy the device information submitted above
    device_destroy(cls,MKDEV(major,0));
    class_destroy(cls);
out4:
    cdev_del(cdev);
out3:
    unregister_chrdev_region(MKDEV(major,minor),1);
out2:
    kfree(cdev);
out1:
    return ret;

}
static void __exit mycdev_exit(void)
{<!-- -->
//gpio subsystem
// 4. Turn off the lights
    gpiod_set_value(gpiono,0);
    // 5. Release the gpio number
    gpiod_put(gpiono);


    // logout interrupt
    free_irq(irqno, NULL);
    //1. Destroy device information device_destroy
    device_destroy(cls,MKDEV(major,0));
    //2. Destroy the directory class_destroy
    class_destroy(cls);
    //3. Logout object cdev_del()
    cdev_del(cdev);
    //4. Release the device number unregister_chrdev_region()
    unregister_chrdev_region(MKDEV(major,minor),1);
    //5. Release object space kfree()
    kfree(cdev);

// Unregister the character device driver
    unregister_chrdev(major, "led0");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");