#include <linux/module.h>
#include <linux/init.h>
#include <linux/of.h>
#include <linux/slab.h>

struct device_node *nd; /* 设备节点 */
struct property *comppro;

static int __init helloworld_init(void) {
    int err = 0;
    u8 i = 0;
    u8 count = 0;
    u32 * valueByProperty = 0; // 将 u32* 改为 u32 类型
    pr_info("Hello world initialization!\n");

    // 从根节点查找 my_backlight 节点
    nd = of_find_node_by_path("/my_backlight");
    if (nd == NULL) {
        printk("my_backlight node not found!\n");
        return -EINVAL;
    } else {
        printk("my_backlight node found!\n");
    }

    // 查找 compatible 属性
    comppro = of_find_property(nd, "compatible", NULL);
    if (comppro == NULL) {
        printk("Compatible property not found!\n");
        return -EINVAL;
    } else {
        printk("compatible = %s\n", (char *)comppro->value);
    }

#if 0
    // 读取 brightness-levels 属性的第 1 个元素
    err = of_property_read_u32_index(nd, "brightness-levels", 1, &valueByProperty);
    if (err == 0) {
        printk(KERN_INFO "valueByProperty: %u\n", valueByProperty);
    } else {
        printk(KERN_ERR "Failed to read brightness-levels property, error code: %d\n", err);
    }
#elif 0
    // 获取 brightness-levels 数组的元素数量
    count = of_property_count_elems_of_size(nd, "brightness-levels", sizeof(u32));
    if (count < 0) {
        printk(KERN_ERR "Failed to get element count for brightness-levels\n");
        return count;
    }

    printk(KERN_INFO "brightness-levels count: %d\n", count);

    // 逐个读取 brightness-levels 数组的元素
    for (i = 0; i < count; i++) {
        err = of_property_read_u32_index(nd, "brightness-levels", i, &valueByProperty);
        if (err) {
            printk(KERN_ERR "Failed to read brightness-levels at index %d, error code: %d\n", i, err);
            return err;
        }
        printk(KERN_INFO "brightness-levels[%d]: %u\n", i, value);
    }
#else
 // 获取 brightness-levels 数组的元素数量
    count = of_property_count_elems_of_size(nd, "brightness-levels", sizeof(u32));
    if (count < 0) {
        printk(KERN_ERR "Failed to get element count for brightness-levels\n");
        return count;
    }
     printk(KERN_INFO "brightness-levels count: %d\n", count);

     // 分配内存以存储 brightness-levels 的值
     valueByProperty = kmalloc_array(count, sizeof(u32), GFP_KERNEL);
     if (!valueByProperty) {
         printk(KERN_ERR "Failed to allocate memory for brightness-levels\n");
        return -ENOMEM;
     }

     // 读取 brightness-levels 属性的所有值到数组
     err = of_property_read_u32_array(nd, "brightness-levels", valueByProperty, count);
     if (err) {
         printk(KERN_ERR "Failed to read brightness-levels array, error code: %d\n", err);
         kfree(valueByProperty);
         return err;
     }

     // 打印 brightness-levels 数组的值
     for (i = 0; i < count; i++) {
         printk(KERN_INFO "brightness-levels[%d]: %u\n", i, valueByProperty[i]);
      }

        // 释放分配的内存
        kfree(valueByProperty);
#endif

    return 0;
}

static void __exit helloworld_exit(void) {
    pr_info("Hello world exit\n");
}

module_init(helloworld_init);
module_exit(helloworld_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("John Madieu <john.madieu@gmail.com>");
MODULE_DESCRIPTION("Linux kernel module skeleton");
