Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions module2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
obj-m += counter.o

ABS_PATH_TO_VROOT ?= ../initramfs
PATH_TO_MODULE_BUILD=$(ABS_PATH_TO_VROOT)/lib/modules/6.7.4/build

all:
make -C "$(PATH_TO_MODULE_BUILD)" M="$(PWD)" modules
clean:
make -C "$(PATH_TO_MODULE_BUILD)" M="$(PWD)" clean
11 changes: 11 additions & 0 deletions module2/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cp -r ../module2 ../../ || exit
cd ../../module2 || exit
make clean || exit
make all || exit
cp counter.ko ../initramfs/lib/modules/6.7.4/ || exit
cd ../initramfs || exit
find . | cpio -ov --format=newc | gzip -9 > ../try/initramfs.gz || exit
cd ..
# cleanup
rm -r module2
rm initramfs/lib/modules/6.7.4/counter.ko
61 changes: 61 additions & 0 deletions module2/counter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// Created by mikhail on 21.05.24.
//
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/time.h>
#include <linux/interrupt.h>

MODULE_LICENSE("MIT");
MODULE_AUTHOR("Me");
MODULE_DESCRIPTION("Key press counter");

// Counter

static atomic_t press_count = ATOMIC_INIT(0);

irqreturn_t irq_handler(int irq, void* dev_id) {
atomic_inc(&press_count);
return IRQ_NONE;
}

static struct timer_list timer;

// Timer callback

static void reschedule_timer(void) {
mod_timer(&timer, jiffies + msecs_to_jiffies(60000));
}

void callback(struct timer_list* _) {
int number = atomic_read(&press_count);
pr_info("Characters typed: %d\n", number);
reschedule_timer();
}

// Module funcs

static int __init counter_init(void) {
timer_setup(&timer, callback, 0);
reschedule_timer();
int err = request_irq(1, // PS/2 irq
irq_handler,
IRQF_SHARED,
"Key counter",
(void*) irq_handler);
if (err) {
del_timer(&timer);
pr_err("request_irq failed with errcode %d\n.", err);
return err;
}
return 0;
}

static void __exit counter_exit(void) {
free_irq(1, (void*) irq_handler);
del_timer(&timer);
}

module_init(counter_init);
module_exit(counter_exit);