From ec9d8234eadddab38fec9b32fe72b47763fe4011 Mon Sep 17 00:00:00 2001 From: Lauchmelder23 Date: Mon, 6 Jun 2022 21:59:25 +0200 Subject: [PATCH] first commit (probably doesnt work) --- .gitignore | 2 ++ Makefile | 15 +++++++++ install.sh | 14 ++++++++ src/io.c | 36 ++++++++++++++++++++ src/lcd.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lcd.h | 18 ++++++++++ unload.sh | 7 ++++ 7 files changed, 188 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100755 install.sh create mode 100644 src/io.c create mode 100644 src/lcd.c create mode 100644 src/lcd.h create mode 100755 unload.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..67cfed1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +*.ko diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..75e147e --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +obj-m = lcd.o +lcd-y = src/lcd.o src/io.o + +KVERSION = $(shell uname -r) + +all: module clean_int + +module: + make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules + +clean_int: + rm */*.o *.o */.*.cmd .*.cmd *.mod *.c *.symvers *.order + +clean: + make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..fb635c6 --- /dev/null +++ b/install.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +module=lcd +device=lcd + +/sbin/insmod ./$module.ko $* || exit 1 + +rm -rf /dev/$device + +major=$(awk "\$2 == \"$module\" { print \$1 }" /proc/devices) +echo $major + +mknod /dev/$device c $major 0 +chmod 664 /dev/$device diff --git a/src/io.c b/src/io.c new file mode 100644 index 0000000..e184417 --- /dev/null +++ b/src/io.c @@ -0,0 +1,36 @@ +#ifndef LCD_IO_H_ +#define LCD_IO_H_ + +#include "lcd.h" + +loff_t lcd_llseek(struct file* filp, loff_t where, int whence) +{ + return 0; +} + +ssize_t lcd_read(struct file* filp, char __user* buf, size_t count, loff_t* off) +{ + return 0; +} + +ssize_t lcd_write(struct file* filp, const char __user* buf, size_t size, loff_t* off) +{ + return 0; +} + +long lcd_ioctl(struct file* filp, unsigned int cmd, unsigned long arg) +{ + return 0; +} + +int lcd_open(struct inode* inode, struct file* file) +{ + return 0; +} + +int lcd_release(struct inode* inode, struct file* file) +{ + return 0; +} + +#endif \ No newline at end of file diff --git a/src/lcd.c b/src/lcd.c new file mode 100644 index 0000000..e88517f --- /dev/null +++ b/src/lcd.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include + +#include "lcd.h" + +MODULE_LICENSE("GPL"); + +int lcd_major = LCD_MAJOR; +int lcd_minor = LCD_MINOR; + +module_param(lcd_major, int, S_IRUGO); +module_param(lcd_minor, int, S_IRUGO); + +static struct lcd_dev lcddev = { 0 }; +static int __init lcd_init_cdev(void); +static void __exit lcd_exit_cdev(void); + +extern loff_t lcd_llseek (struct file*, loff_t, int); +extern ssize_t lcd_read (struct file*, char __user*, size_t, loff_t*); +extern ssize_t lcd_write (struct file*, const char __user*, size_t, loff_t*); +extern long lcd_ioctl (struct file*, unsigned int, unsigned long); +extern int lcd_open (struct inode*, struct file*); +extern int lcd_release (struct inode*, struct file*); + +static struct file_operations lcd_fops = +{ + .owner = THIS_MODULE, + .llseek = lcd_llseek, + .read = lcd_read, + .write = lcd_write, + .unlocked_ioctl = lcd_ioctl, + .open = lcd_open, + .release = lcd_release +}; + +static int __init lcd_init_module(void) +{ + int result = 0; + + if(lcd_major) + { + lcddev.devno = MKDEV(lcd_major, lcd_minor); + result = register_chrdev_region(lcddev.devno, 1, "lcd"); + } + else + { + result = alloc_chrdev_region(&lcddev.devno, lcd_minor, 1, "lcd"); + lcd_major = MAJOR(lcddev.devno); + } + + if(result < 0) + { + printk(KERN_ERR "%s: Failed to register character device\n", THIS_MODULE->name); + return result; + } + + result = lcd_init_cdev(); + if(result < 0) + return result; + + printk(KERN_INFO "%s: Module loaded!\n", THIS_MODULE->name); + return result; +} + +static void __exit lcd_exit_module(void) +{ + lcd_exit_cdev(); + unregister_chrdev_region(lcddev.devno, 1); + + printk(KERN_INFO "%s: Module removed!\n", THIS_MODULE->name); +} + +module_init(lcd_init_module); +module_exit(lcd_exit_module); + +static int __init lcd_init_cdev(void) +{ + int result; + + cdev_init(&lcddev.dev, &lcd_fops); + lcddev.dev.owner = THIS_MODULE; + lcddev.dev.ops = NULL; + + result = cdev_add(&lcddev.dev, lcddev.devno, 1); + if(result < 0) + printk(KERN_ERR "%s: Failed to add device\n", THIS_MODULE->name); + + return result; +} + +static void __exit lcd_exit_cdev(void) +{ + cdev_del(&lcddev.dev); +} diff --git a/src/lcd.h b/src/lcd.h new file mode 100644 index 0000000..ca7a1bc --- /dev/null +++ b/src/lcd.h @@ -0,0 +1,18 @@ +#ifndef _LCD_H +#define _LCD_H + +#include + +#define LCD_MAJOR 0 +#define LCD_MINOR 0 + +extern int lcd_major; +extern int lcd_minor; + +struct lcd_dev +{ + dev_t devno; + struct cdev dev; +}; + +#endif \ No newline at end of file diff --git a/unload.sh b/unload.sh new file mode 100755 index 0000000..1b8b0b8 --- /dev/null +++ b/unload.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +module=lcd +device=lcd + +rmmod $module || exit 1 +rm -rf /dev/$device