110 lines
3.4 KiB
C
110 lines
3.4 KiB
C
#include <linux/module.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/init.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/uaccess.h>
|
|
#include <linux/ioctl.h>
|
|
#include <linux/device.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/stat.h>
|
|
#include <linux/fs.h>
|
|
#include <linux/err.h>
|
|
|
|
#define DEVICE_NAME "temp_monitor"
|
|
#define IOCTL_GET_TEMP _IOR('t', 1, int)
|
|
#define TEMP_SENSOR_PATH "/sys/class/thermal/thermal_zone0/temp"
|
|
|
|
static int major_number;
|
|
static struct class* temp_monitor_class = NULL;
|
|
static struct device* temp_monitor_device = NULL;
|
|
|
|
static int device_open(struct inode *inode, struct file *file) {
|
|
return 0;
|
|
}
|
|
|
|
static int device_release(struct inode *inode, struct file *file) {
|
|
return 0;
|
|
}
|
|
|
|
static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
|
|
switch (cmd) {
|
|
case IOCTL_GET_TEMP: {
|
|
int temp = 0;
|
|
struct file *f;
|
|
char *buf;
|
|
loff_t pos = 0;
|
|
|
|
// Check if the temperature file exists
|
|
f = filp_open(TEMP_SENSOR_PATH, O_RDONLY, 0);
|
|
if (IS_ERR(f)) {
|
|
printk(KERN_INFO "temp_monitor: Temperature file not found\n");
|
|
} else {
|
|
buf = kmalloc(16, GFP_KERNEL);
|
|
if (!buf) {
|
|
filp_close(f, NULL);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
// Read the temperature
|
|
kernel_read(f, buf, 16, &pos);
|
|
sscanf(buf, "%d", &temp);
|
|
temp /= 1000; // Convert to degrees Celsius
|
|
filp_close(f, NULL);
|
|
kfree(buf);
|
|
}
|
|
|
|
if (copy_to_user((int *)arg, &temp, sizeof(temp))) {
|
|
return -EACCES;
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static struct file_operations fops = {
|
|
.open = device_open,
|
|
.release = device_release,
|
|
.unlocked_ioctl = device_ioctl,
|
|
};
|
|
|
|
static int __init temp_monitor_init(void) {
|
|
major_number = register_chrdev(0, DEVICE_NAME, &fops);
|
|
if (major_number < 0) {
|
|
printk(KERN_ALERT "temp_monitor: Failed to register a major number\n");
|
|
return major_number;
|
|
}
|
|
temp_monitor_class = class_create(THIS_MODULE->name); // Updated this line
|
|
if (IS_ERR(temp_monitor_class)) {
|
|
unregister_chrdev(major_number, DEVICE_NAME);
|
|
printk(KERN_ALERT "temp_monitor: Failed to register device class\n");
|
|
return PTR_ERR(temp_monitor_class);
|
|
}
|
|
temp_monitor_device = device_create(temp_monitor_class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME);
|
|
if (IS_ERR(temp_monitor_device)) {
|
|
class_destroy(temp_monitor_class);
|
|
unregister_chrdev(major_number, DEVICE_NAME);
|
|
printk(KERN_ALERT "temp_monitor: Failed to create the device\n");
|
|
return PTR_ERR(temp_monitor_device);
|
|
}
|
|
printk(KERN_INFO "temp_monitor: Device initialized with major number %d\n", major_number);
|
|
return 0;
|
|
}
|
|
|
|
static void __exit temp_monitor_exit(void) {
|
|
device_destroy(temp_monitor_class, MKDEV(major_number, 0));
|
|
class_unregister(temp_monitor_class);
|
|
class_destroy(temp_monitor_class);
|
|
unregister_chrdev(major_number, DEVICE_NAME);
|
|
printk(KERN_INFO "temp_monitor: Device removed\n");
|
|
}
|
|
|
|
module_init(temp_monitor_init);
|
|
module_exit(temp_monitor_exit);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_AUTHOR("Your Name");
|
|
MODULE_DESCRIPTION("A CPU Temperature Monitoring Kernel Module");
|