22_backlightLinux内核模块

01_basicLinux内核模块_the kernel was built by-CSDN博客https://blog.csdn.net/m0_37132481/article/details/136157384

效果

复制代码
# cat backlight.c
#define pr_fmt(fmt) "hello-backlight# %s " fmt, __func__

#include <linux/module.h>
#include <linux/component.h>
#include <linux/platform_device.h>
#include <linux/backlight.h>

#define DRIVER_NAME "demo-backlight"

struct hello_backlight_data {
        struct backlight_device *backlight;
};

// called by cat /sys/class/backlight/demo-backlight/actual_brightness
static int hello_backlight_get_brightness(struct backlight_device *bl)
{
        pr_info("get brightness\n");
        return 0;
}

// called by echo 1000 >  /sys/class/backlight/demo-backlight/brightness
static int hello_backlight_update_status(struct backlight_device *bl)
{
        pr_info("set brightness\n");
        return 0;
}

static const struct backlight_ops backlight_ops = {
        .update_status  = hello_backlight_update_status,
        .get_brightness = hello_backlight_get_brightness,
};

static int hello_backlight_probe(struct platform_device *pdev)
{
        struct backlight_properties props;
        struct hello_backlight_data *data;

        data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
        if (data == NULL)
                return -ENOMEM;

        props.type = BACKLIGHT_RAW;
        props.power = FB_BLANK_UNBLANK;
        props.max_brightness = 1000;
        props.brightness = 600;
        data->backlight = devm_backlight_device_register(&pdev->dev, DRIVER_NAME, &pdev->dev, data, &backlight_ops, &props);

        if (IS_ERR(data->backlight)) {
                return PTR_ERR(data->backlight);
        }

        platform_set_drvdata(pdev, data);
        return 0;
}

static void hello_backlight_remove(struct platform_device *pdev)
{
        platform_set_drvdata(pdev, NULL);
}

static struct platform_driver hello_backlight_driver = {
        .driver = {
                .name = DRIVER_NAME,
        },
        .probe = hello_backlight_probe,
        .remove = hello_backlight_remove,
};

static struct platform_device *pdev;

static int __init hello_backlight_init(void)
{
        int err;

        err = platform_driver_register(&hello_backlight_driver);
        if (err) {
                return err;
        }
        pdev = platform_device_register_simple(DRIVER_NAME, 0, NULL, 0);
        if (!pdev) {
                goto err;
        }

        return 0;
err:
        platform_driver_unregister(&hello_backlight_driver);
        return -1;
}

static void __exit hello_backlight_exit(void)
{
        platform_device_unregister(pdev);
        platform_driver_unregister(&hello_backlight_driver);
}

module_init(hello_backlight_init)
module_exit(hello_backlight_exit)

MODULE_LICENSE("GPL");
相关推荐
爆米花byh1 小时前
在RockyLinux9环境下安装Rust1.100.0环境
linux·rust
爆米花byh1 小时前
在RockyLinux9环境下安装Manticore Search32.1.3单机版
linux·服务器·搜索引擎
大今野1 小时前
技能六 · Linux(CentOS 7)操作系统章节习题
linux·运维·centos·专转本
我命由我123451 小时前
Android Gradle - Android Gradle 配置
android·java·linux·java-ee·kotlin·android studio·android-studio
机智阳1 小时前
系统优化(高级)
linux·运维·编辑器
.道阻且长.2 小时前
Linux基础IO:缓冲区 和 库函数的实现
linux·运维·服务器
皓月盈江3 小时前
Windows 复制文件夹到 Linux 后显示“带锁”解决方法
linux·运维·windows·linux文件·文件带锁·文件夹带锁
byte轻骑兵3 小时前
异构混合环境远程控制工具适配对比(Windows / Mac / Linux / 国产系统 / arm64 / 鸿蒙)
linux·windows·macos·远程办公
jiecy4 小时前
用 AI 手搓海康视频播放插件:NVR 在 Linux 网页下实时播放、回放
linux·运维·服务器
草莓熊Lotso4 小时前
Qt 进阶控件实战:下拉框、列表、表格 + 旋钮 / 滑动条全攻略
大数据·c语言·开发语言·c++·人工智能·qt