Steward
分享是一種喜悅、更是一種幸福
驅動程式 - Linux Device Driver (LDD) - 使用範例 - C/C++ (Debian) - Hardware Monitoring
main.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/slab.h>
struct simple_data {
int temp;
};
static umode_t simple_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
{
if (type == hwmon_temp && attr == hwmon_temp_input) {
return 0444;
}
return 0;
}
static int simple_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val)
{
struct simple_data *d = dev_get_drvdata(dev);
if (type == hwmon_temp && attr == hwmon_temp_input) {
*val = d->temp;
return 0;
}
return -EOPNOTSUPP;
}
static const struct hwmon_ops simple_hwmon_ops = {
.is_visible = simple_is_visible,
.read = simple_read,
};
static const struct hwmon_channel_info *simple_info[] = {
HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
NULL
};
static const struct hwmon_chip_info simple_chip_info = {
.ops = &simple_hwmon_ops,
.info = simple_info,
};
static int simple_probe(struct platform_device *pdev)
{
struct simple_data *d = NULL;
struct device *hwmon_dev = NULL;
d = devm_kzalloc(&pdev->dev, sizeof(*d), GFP_KERNEL);
d->temp = 9999;
dev_set_drvdata(&pdev->dev, d);
hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, "simple_hwmon", d, &simple_chip_info, NULL);
return 0;
}
static int simple_remove(struct platform_device *pdev)
{
return 0;
}
static struct platform_driver simple_driver = {
.probe = simple_probe,
.remove = simple_remove,
.driver = {
.name = "simple_hwmon",
},
};
static struct platform_device *simple_pdev = NULL;
static int __init simple_init(void)
{
simple_pdev = platform_device_register_simple("simple_hwmon", -1, NULL, 0);
return platform_driver_register(&simple_driver);
}
static void __exit simple_exit(void)
{
platform_driver_unregister(&simple_driver);
platform_device_unregister(simple_pdev);
}
module_init(simple_init);
module_exit(simple_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Steward");
MODULE_DESCRIPTION("Simple hwmon driver");
完成
$ cat /sys/class/hwmon/hwmon1/name
simple_hwmon
$ cat /sys/class/hwmon/hwmon1/temp1_input
9999