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");
相关推荐
是隼人5 小时前
buuctf-pwn ciscn_2019_n_5题解(学习过程持续 更新)
c语言·学习·安全·pwn入门·ctf入门
奇树谦6 小时前
Seaweed中filer.sync原理
linux·服务器·网络
Huangjin007_6 小时前
【Linux 系统篇(十六)】进程(四) : 僵尸进程、孤儿进程、进程优先级
linux·运维·服务器
瞬间&永恒~6 小时前
【Kubernetes】(十五)维护与升级、ETCD 备份和恢复
linux·运维·docker·云原生·kubernetes
susplus6 小时前
【linux应用软件编程】进程间的通信方式2【网络通信从入门到UDP编程】
linux·udp·ip
MSTcheng.6 小时前
【Linux】Linux学习第三弹——Linux基本指令2
linux·windows·学习·ubuntu·操作系统
希望奇迹很安静6 小时前
Linux基本使用命令
linux·运维·服务器
邪修king6 小时前
Re:Linux 系统篇(十七):进程篇(六):环境变量深度解析|命令行参数、环境变量表、4 种获取方式与继承机制万字详解
linux·服务器·c++
张小姐的猫7 小时前
【C++开发脚手架】 —— Jsoncpp上手
java·linux·开发语言·网络·c++·tcp/ip·udp
昌原的儿子LEO7 小时前
Linux网络编程:TCP/UDP与HTTP协议核心笔记
linux·网络·tcp/ip