timer driver 源码
#include "linux/device/class.h"
#include "linux/export.h"
#include "linux/uaccess.h"
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/timer.h>
#define MY_TIMER_NAME "my_timer" /* 设备名 */
#define MY_TIMER_NUM 1 /* 设备数目 */
static void kernel_timer_expires(struct timer_list *t);
static DEFINE_TIMER(test_timer2, kernel_timer_expires);
typedef struct {
dev_t dev_id; /* 设备号 */
struct cdev c_dev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 主设备号 */
int minor; /* 次设备号 */
struct timer_list test_timer1; /* timer1 */
} new_chrdev_t;
static new_chrdev_t new_chrdev;
static void kernel_timer_expires(struct timer_list *t)
{
if (t == &(new_chrdev.test_timer1)) {
printk("Timer 1 is coming\r\n");
mod_timer(&(new_chrdev.test_timer1),
jiffies + 1 * HZ); // 重新设置timer1 1s timeout
} else if (t == &test_timer2) {
printk("Timer 2 is coming\r\n");
mod_timer(&test_timer2, jiffies + 2 * HZ); // 重新设置timer2 2s timeout
} else {
printk("Err: timer is unknown!!!\r\n");
}
}
static int my_timer_open(struct inode *inode, struct file *file)
{
printk("k: my_timer open\r\n");
//初始化timer1
timer_setup(&(new_chrdev.test_timer1), kernel_timer_expires, 0);
//启动timer1、timer2
mod_timer(&(new_chrdev.test_timer1), jiffies + 1 * HZ); // 1s timeout
mod_timer(&(test_timer2), jiffies + 2 * HZ); // 2s timeout
return 0;
}
static int my_timer_release(struct inode *inode, struct file *file)
{
printk("k: my_timer release\r\n");
return 0;
}
static struct file_operations my_timer_fops = {
.owner = THIS_MODULE,
.open = my_timer_open,
.release = my_timer_release,
};
static int __init my_timer_init(void)
{
int err = 0;
err = alloc_chrdev_region(&new_chrdev.dev_id, 0, MY_TIMER_NUM,
MY_TIMER_NAME);
if (err < 0) {
printk("k: alloc chrdev region failed err = %d\r\n", err);
goto err_chrdev;
}
/* get major and minor */
new_chrdev.major = MAJOR(new_chrdev.dev_id);
new_chrdev.minor = MINOR(new_chrdev.dev_id);
printk("k: newcheled major=%d,minor=%d\r\n", new_chrdev.major,
new_chrdev.minor);
new_chrdev.c_dev.owner = THIS_MODULE;
cdev_init(&new_chrdev.c_dev, &my_timer_fops);
err = cdev_add(&new_chrdev.c_dev, new_chrdev.dev_id, MY_TIMER_NUM);
if (err < 0) {
printk("k: cdev add failed err = %d\r\n", err);
goto err_cdev_add;
}
new_chrdev.class = class_create(MY_TIMER_NAME);
if (IS_ERR(new_chrdev.class)) {
err = PTR_ERR(new_chrdev.class);
goto err_class_create;
}
new_chrdev.device = device_create(new_chrdev.class, NULL, new_chrdev.dev_id,
NULL, MY_TIMER_NAME);
if (IS_ERR(new_chrdev.device)) {
err = PTR_ERR(new_chrdev.device);
goto err_device_create;
}
printk("k: base module init\r\n");
return 0;
err_device_create:
class_destroy(new_chrdev.class);
err_class_create:
cdev_del(&new_chrdev.c_dev);
err_cdev_add:
unregister_chrdev_region(new_chrdev.dev_id, MY_TIMER_NUM);
err_chrdev:
return err;
}
static void __exit my_timer_exit(void)
{
device_destroy(new_chrdev.class, new_chrdev.dev_id);
class_destroy(new_chrdev.class);
cdev_del(&new_chrdev.c_dev);
unregister_chrdev_region(new_chrdev.dev_id, MY_TIMER_NUM);
del_timer_sync(&(new_chrdev.test_timer1));
del_timer_sync(&test_timer2);
printk("k: base module exit!\r\n");
}
module_init(my_timer_init);
module_exit(my_timer_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("tyustli");
MODULE_INFO(intree, "Y"); /* loading out-of-tree module taints kernel */
timer app 源码
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
int main(int argc, char *argv[])
{
int fd, retvalue;
char *filename;
if (argc != 3) {
printf("u: error Usage!\r\n");
return -1;
}
filename = argv[1];
/* 打开驱动文件 */
fd = open(filename, O_RDWR);
if (fd < 0) {
printf("u: can't open file %s\r\n", filename);
return -1;
}
/* 关闭设备 */
retvalue = close(fd);
if (retvalue < 0) {
printf("u: can't close file %s\r\n", filename);
return -1;
}
return 0;
}
编译
../my_module_build.sh ../0014_timer/
查看模块 ko 文件
ls /lib/modules/6.5.7+/
my_module.ko
设备模块安装
modprobe my_module
k: newcheled major=248,minor=0
k: base module init
查看设备
~ # ls /sys/class/
ata_device graphics mmc_host rtc vc
ata_link hwmon mtd scsi_device virtio-ports
ata_port i2c-adapter my_timer scsi_disk vtconsole
backlight input net scsi_host wakeup
bdi leds power_supply sound
block mdio_bus pps tty
devlink mem ptp ubi
drm misc regulator usbmon
应用程序
ls /lib/modules/6.5.7+/
my_app
使用 timer
lib/modules/6.5.7+/my_app /dev/my_timer 1
效果
Timer 1 is coming
Timer 1 is coming
Timer 2 is coming
Timer 1 is coming
Timer 1 is coming
Timer 2 is coming
Timer 1 is coming
Timer 1 is coming
退出
Ctrl + a
x