#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "light_ring.h" extern struct kobject *vfiec_kobj; static struct kobject *backlight_kobj = NULL; static int brightness_val = 0; static int max_brightness_val = 1; #define BACKLIGHT_PATH "/sys/class/backlight/intel_backlight/brightness" // 从sysfs读取当前亮度值 static int read_current_brightness(void) { struct file *filp = NULL; char buf[32] = {0}; loff_t pos = 0; int ret = -EIO; int brightness = -1; filp = filp_open(BACKLIGHT_PATH, O_RDONLY, 0); if (IS_ERR(filp)) { pr_err("Failed to open backlight file: %ld\n", PTR_ERR(filp)); return -EIO; } ret = kernel_read(filp, buf, sizeof(buf) - 1, &pos); if (ret > 0) { buf[ret] = '\0'; if (kstrtoint(buf, 10, &brightness) != 0) { pr_err("Failed to parse brightness value\n"); brightness = -EINVAL; } } filp_close(filp, NULL); return brightness; } static int read_max_brightness(void) { struct file *filp = NULL; loff_t pos = 0; int max_brightness; filp = filp_open("/sys/class/backlight/intel_backlight/max_brightness", O_RDONLY, 0); if (!IS_ERR(filp)) { char max_buf[32] = {0}; kernel_read(filp, max_buf, sizeof(max_buf) - 1, &pos); if (kstrtoint(max_buf, 10, &max_brightness) != 0) { filp_close(filp, NULL); return -EINVAL; } filp_close(filp, NULL); } return max_brightness; } // 向sysfs写入亮度值 static int write_brightness_value(int value) { struct file *filp = NULL; char buf[32] = {0}; loff_t pos = 0; int ret = -EIO; int max_brightness; // 先读取最大值进行边界检查 filp = filp_open("/sys/class/backlight/intel_backlight/max_brightness", O_RDONLY, 0); if (!IS_ERR(filp)) { char max_buf[32] = {0}; kernel_read(filp, max_buf, sizeof(max_buf) - 1, &pos); if (kstrtoint(max_buf, 10, &max_brightness) == 0) { if (value < 0 || value > max_brightness) { pr_err("Brightness value %d out of range [0, %d]\n", value, max_brightness); filp_close(filp, NULL); return -EINVAL; } } filp_close(filp, NULL); } // 写入亮度值 filp = filp_open(BACKLIGHT_PATH, O_WRONLY, 0); if (IS_ERR(filp)) { pr_err("Failed to open backlight file for write: %ld\n", PTR_ERR(filp)); return -EIO; } snprintf(buf, sizeof(buf), "%d", value); ret = kernel_write(filp, buf, strlen(buf), &pos); filp_close(filp, NULL); return (ret > 0) ? 0 : -EIO; } /* ==================== mode ==================== */ static ssize_t brightness_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { static int val = 0; brightness_val = read_current_brightness(); if(max_brightness_val == 0 || max_brightness_val > 960) { max_brightness_val = 960; } val = brightness_val*100/max_brightness_val; if(val > 100) { val = 100; } return sprintf(buf, "%d\n", val); } static ssize_t brightness_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { int ret = 0; long val; if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 100) { return -EINVAL; } brightness_val = val*max_brightness_val/100; if(brightness_val > 960) { brightness_val = 960; } ret = write_brightness_value(brightness_val); return count; } static struct kobj_attribute brightness = __ATTR(brightness, 0644, brightness_show, brightness_store); /* ==================== 属性组 ==================== */ static struct attribute *backlight_attrs[] = { &brightness.attr, NULL, }; static struct attribute_group backlight_attr_group = { .attrs = backlight_attrs, }; int backlight_init(void) { int ret; #ifndef C181 return 0; #endif max_brightness_val = read_max_brightness(); if(max_brightness_val <= 0) { pr_err("Failed to read max brightness value\n"); return -EIO; } brightness_val = read_current_brightness(); /* 创建 /sys/kernel/vfiec/lightring */ backlight_kobj = kobject_create_and_add("backlight", vfiec_kobj); if (!backlight_kobj) { ret = -ENOMEM; } /* 创建属性文件 */ ret = sysfs_create_group(backlight_kobj, &backlight_attr_group); if (ret) { pr_err("Faibacklight to create sysfs group: %d\n", ret); } return ret; } void backlight_exit(void) { #ifndef C181 return ; #endif sysfs_remove_group(backlight_kobj, &backlight_attr_group); kobject_put(backlight_kobj); }