first commit (probably doesnt work)

This commit is contained in:
Lauchmelder23 2022-06-06 21:59:25 +02:00
commit ec9d8234ea
7 changed files with 188 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.vscode/
*.ko

15
Makefile Normal file
View file

@ -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

14
install.sh Executable file
View file

@ -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

36
src/io.c Normal file
View file

@ -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

96
src/lcd.c Normal file
View file

@ -0,0 +1,96 @@
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/fs.h>
#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);
}

18
src/lcd.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef _LCD_H
#define _LCD_H
#include <linux/cdev.h>
#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

7
unload.sh Executable file
View file

@ -0,0 +1,7 @@
#!/bin/bash
module=lcd
device=lcd
rmmod $module || exit 1
rm -rf /dev/$device