diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..3d877b5 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,27 @@ +{ + "env": { + "KERNEL_VERSION":"6.10.4-200.fc40.x86_64" + }, + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/lib/modules/${KERNEL_VERSION}/build/include/", + "/lib/modules/${KERNEL_VERSION}/build/arch/x86/include/", + "/lib/modules/${KERNEL_VERSION}/build/arch/x86/include/generated" + ], + "defines": [ + "__GNUC__", + "__KERNEL__", + "MODULE", + "KBUILD_MODNAME=\"UniversalIO\"" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "linux-gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..87453db --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,19 @@ +{ + "files.associations": { + "kernel.h": "c", + "slab.h": "c", + "i2c.h": "c", + "i2c_engine.h": "c", + "module.h": "c", + "spi_engine.h": "c", + "spi.h": "c", + "i2c-smbus.h": "c", + "i2c-dev.h": "c", + "compiler.h": "c", + "input.h": "c", + "init.h": "c", + "lib.h": "c", + "uart_engine.h": "c" + }, + "C_Cpp.clang_format_style": "{ BasedOnStyle: Google, UseTab: Always, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Linux, AllowShortIfStatementsOnASingleLine: true, IndentCaseLabels: false, ColumnLimit: 120}" +} \ No newline at end of file diff --git a/driver/linux/.gitignore b/driver/linux/.gitignore new file mode 100644 index 0000000..da1803b --- /dev/null +++ b/driver/linux/.gitignore @@ -0,0 +1,12 @@ +# Binaries and objects generated from goodbye.c +*.cmd +*.ko +*.mod.c +*.o +*.mod +Module.symvers +modules.order +.tmp_versions/ + +# Babel artifacts +babel_client \ No newline at end of file diff --git a/driver/linux/Makefile b/driver/linux/Makefile new file mode 100644 index 0000000..9e7027b --- /dev/null +++ b/driver/linux/Makefile @@ -0,0 +1,25 @@ +obj-m += UniversalIO.o +UniversalIO-objs += ./src/main.o ./src/i2c_engine.o ./src/spi_engine.o ./src/uart_engine.o +CONFIG_MODULE_SIG=n +EXTRA_CFLAGS := -I$(src)/include +I2C_BUS = 4 + +reload: build + @-sudo rmmod UniversalIO.ko + @sudo insmod UniversalIO.ko + @sudo dmesg | grep "UniversalIO" | tail -n 5 + +build: + @make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules + +clean: + @make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean + +sendTest: + sudo i2ctransfer -y $(I2C_BUS) w6500@0x50 0x64- + +style: + @/lib/modules/$(shell uname -r)/build/scripts/checkpatch.pl -q --no-tree -f src/*.c include/*.h + +dmesg: + @sudo dmesg \ No newline at end of file diff --git a/driver/linux/include/i2c_engine.h b/driver/linux/include/i2c_engine.h new file mode 100644 index 0000000..c9a3124 --- /dev/null +++ b/driver/linux/include/i2c_engine.h @@ -0,0 +1,6 @@ +/* SPDX-License-Identifier: */ +#pragma once +#include "lib.h" +#include +extern struct i2c_algorithm algo; +extern struct i2c_adapter adapter; diff --git a/driver/linux/include/lib.h b/driver/linux/include/lib.h new file mode 100644 index 0000000..94f6dd4 --- /dev/null +++ b/driver/linux/include/lib.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: */ +#pragma once +#include + +#ifdef pr_fmt +#undef pr_fmt +#endif +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt diff --git a/driver/linux/include/spi_engine.h b/driver/linux/include/spi_engine.h new file mode 100644 index 0000000..d664d4f --- /dev/null +++ b/driver/linux/include/spi_engine.h @@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: */ +#pragma once +#include "lib.h" +#include diff --git a/driver/linux/include/uart_engine.h b/driver/linux/include/uart_engine.h new file mode 100644 index 0000000..b42043c --- /dev/null +++ b/driver/linux/include/uart_engine.h @@ -0,0 +1,3 @@ +/* SPDX-License-Identifier: */ +#pragma once +#include "lib.h" diff --git a/driver/linux/src/i2c_engine.c b/driver/linux/src/i2c_engine.c new file mode 100644 index 0000000..d0eee2b --- /dev/null +++ b/driver/linux/src/i2c_engine.c @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "i2c_engine.h" +// static int i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num); +// static u32 i2c_func(struct i2c_adapter *adap); + +static int i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) +{ + pr_info("[i2c-%d] xfer!\n", adap->nr); + pr_info("[i2c-%d] xfer num %d!\n", adap->nr, num); + pr_info("[i2c-%d] xfer len %d!\n", adap->nr, msgs->len); + // int len = msgs->len; + return 1; +} + +static u32 i2c_func(struct i2c_adapter *adap) +{ + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL_ALL; +} + +struct i2c_algorithm algo = { + .functionality = i2c_func, + .master_xfer = i2c_xfer, + .smbus_xfer = NULL}; + +struct i2c_adapter adapter = { + .class = I2C_CLASS_HWMON, + .owner = THIS_MODULE, + .algo = &algo}; diff --git a/driver/linux/src/main.c b/driver/linux/src/main.c new file mode 100644 index 0000000..1aed3a3 --- /dev/null +++ b/driver/linux/src/main.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "lib.h" +#include "i2c_engine.h" +#include "spi_engine.h" +#include "uart_engine.h" + +static int __init my_i2c_init(void) +{ + snprintf(adapter.name, sizeof(adapter.name), "My I2C Adapter"); + + if (i2c_add_adapter(&adapter)) { + // i2c_put_adapter(&adapter); + return -ENODEV; + } + pr_info("hello i2c-%d!\n", adapter.nr); + + return 0; +} + +static void __exit my_i2c_exit(void) +{ + pr_info("goodbye i2c-%d!\n", adapter.nr); + i2c_del_adapter(&adapter); +} + +module_init(my_i2c_init); +module_exit(my_i2c_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Author"); +MODULE_DESCRIPTION("A simple I2C adapter driver"); diff --git a/driver/linux/src/spi_engine.c b/driver/linux/src/spi_engine.c new file mode 100644 index 0000000..ccc3497 --- /dev/null +++ b/driver/linux/src/spi_engine.c @@ -0,0 +1,2 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "spi_engine.h" diff --git a/driver/linux/src/uart_engine.c b/driver/linux/src/uart_engine.c new file mode 100644 index 0000000..7deedc3 --- /dev/null +++ b/driver/linux/src/uart_engine.c @@ -0,0 +1,2 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "uart_engine.h"