From 5f67ca4a550a4f729b7206e392b795a016d365aa Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Wed, 17 Apr 2024 23:36:12 +0800
Subject: [PATCH] Driver:IGKBoard-IMX8MP: Add driver build shell script to add new rtl8723du WiFi/BT driver support

---
 drivers/igkboard-imx8mp/user/hello.c  |   35 +++++++
 drivers/igkboard-imx8mp/user/Makefile |   40 ++++++++
 drivers/igkboard-imx8mp/build.sh      |  205 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 280 insertions(+), 0 deletions(-)

diff --git a/drivers/igkboard-imx8mp/build.sh b/drivers/igkboard-imx8mp/build.sh
new file mode 100755
index 0000000..c552efd
--- /dev/null
+++ b/drivers/igkboard-imx8mp/build.sh
@@ -0,0 +1,205 @@
+#!/bin/bash
+
+# this project absolute path
+PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
+
+# top project absolute path
+TOP_PATH=$(realpath $PRJ_PATH/../../)
+
+# binaries build prefix install path
+PRFX_PATH=$PRJ_PATH/install
+
+# binaries finally install path if needed
+#INST_PATH=/tftp
+
+# download taballs path
+TARBALL_PATH=$PRJ_PATH/tarballs
+
+# config file path
+CONF_FILE=$TOP_PATH/config.json
+
+# shell script will exit once get command error
+set -e
+
+#+-------------------------+
+#| Shell script functions  |
+#+-------------------------+
+
+function pr_error() {
+    echo -e "\033[40;31m $1 \033[0m"
+}
+
+function pr_warn() {
+    echo -e "\033[40;33m $1 \033[0m"
+}
+
+function pr_info() {
+    echo -e "\033[40;32m $1 \033[0m"
+}
+
+# decompress a packet to destination path
+function do_unpack()
+{
+    tarball=$1
+    dstpath=`pwd`
+
+    if [[ $# == 2 ]] ; then
+        dstpath=$2
+    fi
+
+    pr_info "decompress $tarball => $dstpath"
+
+    mkdir -p $dstpath
+    case $tarball in
+        *.tar.gz)
+            tar -xzf $tarball -C $dstpath
+            ;;
+
+        *.tar.bz2)
+            tar -xjf $tarball -C $dstpath
+            ;;
+
+        *.tar.xz)
+            tar -xJf $tarball -C $dstpath
+            ;;
+
+        *.tar.zst)
+            tar -I zstd -xf $tarball -C $dstpath
+            ;;
+
+        *.tar)
+            tar -xf $tarball -C $dstpath
+            ;;
+
+        *.zip)
+            unzip -qo $tarball -d $dstpath
+            ;;
+
+        *)
+            pr_error "decompress Unsupport packet: $tarball"
+            return 1;
+            ;;
+    esac
+}
+
+# parser configure file and export environment variable
+function export_env()
+{
+    export BOARD=`jq -r ".bsp.board" $CONF_FILE | tr 'A-Z' 'a-z'`
+    export CROSS_COMPILE=`jq -r ".bsp.cortexAtool" $CONF_FILE`
+    export BSP_VER=`jq -r ".bsp.version" $CONF_FILE | tr 'A-Z' 'a-z'`
+    export BSP_URL=`jq -r ".bsp.giturl" $CONF_FILE`
+
+    export KER_SRC=linux-imx
+    export KER_PATH=$TOP_PATH/kernel/linux-imx
+    export DRV_PATH=$TOP_PATH/kernel/install
+
+    export JOBS=`cat /proc/cpuinfo | grep processor | wc -l`
+
+    if [[ $BOARD =~ mx6ull ]] ; then
+        export ARCH=arm
+    else
+        export ARCH=arm64
+    fi
+}
+
+function build_user()
+{
+    SRC=user
+
+    if [ ! -d ${SRC} ] ; then
+        return ;
+    fi
+
+    pr_warn "Build for users driver"
+    cd ${SRC}
+
+    sed -i "s|^CROSS_COMPILE ?=.*|CROSS_COMPILE ?= ${CROSS_COMPILE}|g" Makefile
+    make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} LINUX_SRC=${KER_PATH} DRV_PATH=${DRV_PATH}/
+
+    cd ${PRJ_PATH}
+}
+
+function build_rtl8723du()
+{
+    SRC=rtl8723du
+
+    cd $PRJ_PATH
+
+    if [ -d $SRC ] ; then
+        pr_info "$SRC USB WiFi driver source code fetched already"
+    else
+        pr_info "start fetch $SRC USB WiFi drver source code"
+
+        if [[ $BSP_URL =~ github.com ]] ; then
+            git clone https://github.com/lwfinger/rtw88.git --depth=1 $SRC
+        else
+            mkdir -p $TARBALL_PATH
+
+            # Download source code packet
+            if [ ! -s $TARBALL_PATH/$SRC.tar.xz ] ; then
+                wget $BSP_URL/imx/bsp/misc/$SRC.tar.xz -P $TARBALL_PATH
+            fi
+
+            # decompress source code packet
+            do_unpack $TARBALL_PATH/$SRC.tar.xz
+        fi
+    fi
+
+    cd $SRC
+
+    pr_warn "start update $SRC USB WiFi driver source code"
+
+    KER_VER=`echo $BSP_VER|awk -F"-" '{print $2}'`
+
+    sed -i "s|^KVER.*|KVER ?= $KER_VER|g" Makefile
+    sed -i "s|^KSRC.*|KSRC := ${KER_PATH}|g" Makefile
+    sed -i "s|^MODDESTDIR.*|MODDESTDIR := ${DRV_PATH}/lib/modules/${KER_VER}-dirty/extra/|g" Makefile
+    sed -i "s|^FIRMWAREDIR.*|FIRMWAREDIR := ${DRV_PATH}/lib/firmware/|g" Makefile
+	sed -i '/depmod -a .*/d' Makefile
+
+    pr_warn "start build $SRC USB WiFi driver"
+    make -j ${JOBS}
+	make install
+    depmod -a -b ${DRV_PATH}/ ${KER_VER}-dirty
+}
+
+
+function do_build()
+{
+    cd $PRJ_PATH
+
+    build_user
+
+    build_rtl8723du
+}
+
+function do_clean()
+{
+    cd $PRJ_PATH
+
+    rm -rf rtl8188fu rtl8723du tarballs
+
+    if [ -d user ] ; then
+        cd user
+        make clean;
+    fi
+
+}
+
+#+-------------------------+
+#| Shell script body entry |
+#+-------------------------+
+
+export_env
+
+if [[ $# == 1 && $1 == -c ]] ;then
+    pr_warn "start clean drivers"
+    do_clean
+    exit;
+fi
+
+pr_warn "start build linux driver for ${BOARD}"
+
+do_build
+
diff --git a/drivers/igkboard-imx8mp/user/Makefile b/drivers/igkboard-imx8mp/user/Makefile
new file mode 100644
index 0000000..f7679a3
--- /dev/null
+++ b/drivers/igkboard-imx8mp/user/Makefile
@@ -0,0 +1,40 @@
+#*********************************************************************************
+#      Copyright:  (C) 2021 LingYun IoT System Studio <www.weike-iot.com>
+#                  All rights reserved.
+#
+#       Filename:  Makefile
+#    Description:  This Makefile used to compile all the drivers here
+#
+#        Version:  1.0.0(18/12/2021~)
+#                  Author:  Guo Wenxue <guowenxue@gmail.com>
+#      ChangeLog:  1, Release initial version on "18/12/2021 01:29:33 PM"
+#
+#********************************************************************************/
+
+ARCH ?= arm
+CROSS_COMPILE ?= /opt/gcc-aarch64-10.3-2021.07/bin/aarch64-none-linux-gnu-
+
+LINUX_SRC ?= ${shell pwd}/../../../kernel/linux-imx/
+DRV_PATH ?= ${shell pwd}/../../../kernel/install/
+
+EXTRA_INSTPATH=/tftp
+
+PWD := $(shell pwd)
+
+obj-m += hello.o
+
+modules:
+	@echo ${LINUX_SRC}
+	@make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} -C $(LINUX_SRC) M=$(PWD) modules
+	@make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} -C $(LINUX_SRC) M=$(PWD) modules_install INSTALL_MOD_PATH=${DRV_PATH} INSTALL_MOD_STRIP=1
+	@make clear
+
+install:
+	cp -af *.ko ${EXTRA_INSTPATH}
+
+clear:
+	@rm -f *.o *.mod* .*.cmd *.symvers *.order
+
+clean: clear
+	@rm -f  *.ko
+
diff --git a/drivers/igkboard-imx8mp/user/hello.c b/drivers/igkboard-imx8mp/user/hello.c
new file mode 100644
index 0000000..49acb63
--- /dev/null
+++ b/drivers/igkboard-imx8mp/user/hello.c
@@ -0,0 +1,35 @@
+/*********************************************************************************
+ *      Copyright:  (C) 2021 LingYun IoT System Studio <www.weike-iot.com>
+ *                  All rights reserved.
+ *
+ *       Filename:  hello.c
+ *    Description:  This file is the linux kernel sample hello module 
+ *                 
+ *        Version:  1.0.0(18/12/2021~)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "18/12/2021 10:50:26 AM"
+ *                 
+ ********************************************************************************/
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/kernel.h>
+
+static __init int hello_init(void)
+{
+    printk(KERN_ALERT "Hello, LingYun IoT System Studio!\n");
+    return 0;
+}
+
+static __exit void hello_exit(void)
+{
+    printk(KERN_ALERT "Goodbye, I have found a good job!\n");
+}
+
+module_init(hello_init);
+module_exit(hello_exit);
+
+MODULE_AUTHOR("GuoWenxue <guowenxue@gmail.com>");
+MODULE_DESCRIPTION("Linux Kernel hello module");
+MODULE_LICENSE("Dual BSD/GPL");
+MODULE_INFO(intree, "Y");

--
Gitblit v1.9.1