Skip to content

Commit

Permalink
initial build for create device node
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffpeng3 committed Aug 21, 2024
1 parent 12d6a87 commit 8e899d3
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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}"
}
12 changes: 12 additions & 0 deletions driver/linux/.gitignore
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions driver/linux/Makefile
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions driver/linux/include/i2c_engine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* SPDX-License-Identifier: <SPDX License Expression> */
#pragma once
#include "lib.h"
#include <linux/i2c.h>
extern struct i2c_algorithm algo;
extern struct i2c_adapter adapter;
8 changes: 8 additions & 0 deletions driver/linux/include/lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* SPDX-License-Identifier: <SPDX License Expression> */
#pragma once
#include <linux/module.h>

#ifdef pr_fmt
#undef pr_fmt
#endif
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4 changes: 4 additions & 0 deletions driver/linux/include/spi_engine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* SPDX-License-Identifier: <SPDX License Expression> */
#pragma once
#include "lib.h"
#include <linux/spi/spi.h>
3 changes: 3 additions & 0 deletions driver/linux/include/uart_engine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* SPDX-License-Identifier: <SPDX License Expression> */
#pragma once
#include "lib.h"
28 changes: 28 additions & 0 deletions driver/linux/src/i2c_engine.c
Original file line number Diff line number Diff line change
@@ -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};
31 changes: 31 additions & 0 deletions driver/linux/src/main.c
Original file line number Diff line number Diff line change
@@ -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");
2 changes: 2 additions & 0 deletions driver/linux/src/spi_engine.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// SPDX-License-Identifier: GPL-2.0
#include "spi_engine.h"
2 changes: 2 additions & 0 deletions driver/linux/src/uart_engine.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// SPDX-License-Identifier: GPL-2.0
#include "uart_engine.h"

0 comments on commit 8e899d3

Please sign in to comment.