feat(lab03): add ex5 - sysfs
This commit is contained in:
33
src/02-driver/sysfs/.clangd
Normal file
33
src/02-driver/sysfs/.clangd
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
CompileFlags:
|
||||||
|
Add:
|
||||||
|
# Architecture and cross-compilation
|
||||||
|
- "--target=aarch64-linux-gnu"
|
||||||
|
|
||||||
|
# Exclude standard library
|
||||||
|
- "-nostdinc"
|
||||||
|
|
||||||
|
# Mandatory kernel definitions
|
||||||
|
- "-D__KERNEL__"
|
||||||
|
- "-DMODULE"
|
||||||
|
- "-DCONFIG_CC_HAS_K_CONSTRAINT=1"
|
||||||
|
|
||||||
|
# Force-included files
|
||||||
|
- "-include"
|
||||||
|
- "/buildroot/output/build/linux-5.15.148/include/linux/compiler-version.h"
|
||||||
|
- "-include"
|
||||||
|
- "/buildroot/output/build/linux-5.15.148/include/linux/kconfig.h"
|
||||||
|
- "-include"
|
||||||
|
- "/buildroot/output/build/linux-5.15.148/include/linux/compiler_types.h"
|
||||||
|
|
||||||
|
# Kernel include paths
|
||||||
|
- "-I/buildroot/output/build/linux-5.15.148/arch/arm64/include"
|
||||||
|
- "-I/buildroot/output/build/linux-5.15.148/arch/arm64/include/generated"
|
||||||
|
- "-I/buildroot/output/build/linux-5.15.148/include"
|
||||||
|
- "-I/buildroot/output/build/linux-5.15.148/arch/arm64/include/uapi"
|
||||||
|
- "-I/buildroot/output/build/linux-5.15.148/arch/arm64/include/generated/uapi"
|
||||||
|
- "-I/buildroot/output/build/linux-5.15.148/include/uapi"
|
||||||
|
- "-I/buildroot/output/build/linux-5.15.148/include/generated/uapi"
|
||||||
|
|
||||||
|
# GCC compiler system include path
|
||||||
|
- "-isystem"
|
||||||
|
- "/buildroot/output/host/lib/gcc/aarch64-buildroot-linux-gnu/11.3.0/include"
|
||||||
22
src/02-driver/sysfs/Makefile
Normal file
22
src/02-driver/sysfs/Makefile
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
# Part executed when called from kernel build system:
|
||||||
|
ifneq ($(KERNELRELEASE),)
|
||||||
|
obj-m += mymodule.o ## name of the generated module
|
||||||
|
|
||||||
|
mymodule-objs := skeleton.o ## list of objects needed for that module
|
||||||
|
|
||||||
|
# Part executed when called from standard make in module source directory:
|
||||||
|
else
|
||||||
|
include ../../buildroot_path
|
||||||
|
include ../../kernel_settings
|
||||||
|
PWD := $(shell pwd)
|
||||||
|
|
||||||
|
all:
|
||||||
|
$(MAKE) -C $(KDIR) M=$(PWD) ARCH=$(CPU) CROSS_COMPILE=$(TOOLS) modules
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(MAKE) -C $(KDIR) M=$(PWD) clean
|
||||||
|
|
||||||
|
install:
|
||||||
|
$(MAKE) -C $(KDIR) M=$(PWD) INSTALL_MOD_PATH=$(MODPATH) modules_install
|
||||||
|
|
||||||
|
endif
|
||||||
60
src/02-driver/sysfs/skeleton.c
Normal file
60
src/02-driver/sysfs/skeleton.c
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#include <linux/init.h> /* needed for macros */
|
||||||
|
#include <linux/kernel.h> /* needed for debugging */
|
||||||
|
#include <linux/module.h> /* needed by all modules */
|
||||||
|
|
||||||
|
#include <linux/cdev.h> /* needed for char device driver */
|
||||||
|
#include <linux/fs.h> /* needed for device drivers */
|
||||||
|
#include <linux/uaccess.h> /* needed to copy data to/from user */
|
||||||
|
|
||||||
|
#include <linux/device.h> /* needed for sysfs handling */
|
||||||
|
#include <linux/miscdevice.h>
|
||||||
|
#include <linux/platform_device.h> /* needed for sysfs handling */
|
||||||
|
|
||||||
|
static int val;
|
||||||
|
|
||||||
|
ssize_t sysfs_show_val(struct device* dev, struct device_attribute* attr, char* buf) {
|
||||||
|
pr_info("sysfs_show_val: val=%d\n", val);
|
||||||
|
sprintf(buf, "%d\n", val);
|
||||||
|
return strlen(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t sysfs_store_val(struct device* dev, struct device_attribute* attr, const char* buf, size_t count) {
|
||||||
|
pr_info("sysfs_store_val: buf=%s\n", buf);
|
||||||
|
val = simple_strtol(buf, 0, 10);
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
DEVICE_ATTR(val, 0664, sysfs_show_val, sysfs_store_val);
|
||||||
|
|
||||||
|
static struct class* sysfs_class;
|
||||||
|
static struct device* sysfs_device;
|
||||||
|
|
||||||
|
static int __init skeleton_init(void) {
|
||||||
|
|
||||||
|
int status = 0;
|
||||||
|
|
||||||
|
sysfs_class = class_create(THIS_MODULE, "my_sysfs_class");
|
||||||
|
sysfs_device = device_create(sysfs_class, NULL, 0, NULL, "my_sysfs_device");
|
||||||
|
if (status == 0) status = device_create_file(sysfs_device, &dev_attr_val);
|
||||||
|
|
||||||
|
pr_info("Linux module skeleton loaded\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void __exit skeleton_exit(void) {
|
||||||
|
|
||||||
|
device_remove_file(sysfs_device, &dev_attr_val);
|
||||||
|
device_destroy(sysfs_class, 0);
|
||||||
|
class_destroy(sysfs_class);
|
||||||
|
|
||||||
|
pr_info("Linux module skeleton unloaded\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
module_init (skeleton_init);
|
||||||
|
module_exit (skeleton_exit);
|
||||||
|
|
||||||
|
MODULE_AUTHOR("Fastium <fastium.pro@proton.me>");
|
||||||
|
MODULE_AUTHOR("Klagarge <remi@heredero.ch>");
|
||||||
|
MODULE_DESCRIPTION ("Module pilot charachter oriented");
|
||||||
|
MODULE_LICENSE ("GPL");
|
||||||
Reference in New Issue
Block a user