# SPDX-License-Identifier: GPL-2.0
#
# Makefile for the MHI stack
#

ifeq ($(DEBUG),y)
  DEBFLAGS = -O2 -g -DMHINET_DEBUG # "-O" is needed to expand inlines
else
  DEBFLAGS = -O2
endif

ccflags-$(DEBUG):=$(DEBFLAGS)

# core layer
obj-m := host/ wwan/ qrtr/

KVER      ?= $(shell uname -r)
KERNELDIR ?= /lib/modules/$(KVER)/build
MODLIB    ?= /lib/modules/$(KVER)
PWD       := $(shell pwd)

# Pull the target kernel's configuration in as make variables so the CONFIG_*
# symbols can be used directly below. auto.conf is kbuild's make-syntax copy of
# .config and is what the kernel's own Makefile includes; .config and the
# distro's /boot/config-* are the same "CONFIG_x=y" format and serve as
# fallbacks. Note that during "make all" kbuild has already defined these.
KCONFIG ?= $(firstword $(wildcard \
	$(KERNELDIR)/include/config/auto.conf \
	$(KERNELDIR)/.config \
	/boot/config-$(KVER)))
-include $(KCONFIG)

# Module compression must match the running kernel, otherwise depmod/modprobe
# will not pick the modules up. Can be overridden on the command line, e.g.
#   make install MOD_COMPRESS=none
ifeq ($(MOD_COMPRESS),)
  ifeq ($(CONFIG_MODULE_COMPRESS_ZSTD),y)
    MOD_COMPRESS := zstd
  else ifeq ($(CONFIG_MODULE_COMPRESS_XZ),y)
    MOD_COMPRESS := xz
  else ifeq ($(CONFIG_MODULE_COMPRESS_GZIP),y)
    MOD_COMPRESS := gzip
  else
    MOD_COMPRESS := none
  endif
endif

ifeq ($(MOD_COMPRESS),zstd)
  MOD_SUFFIX := .zst
  MOD_COMPRESS_CMD := zstd -q -f -T0 --ultra -22
else ifeq ($(MOD_COMPRESS),xz)
  MOD_SUFFIX := .xz
  MOD_COMPRESS_CMD := xz -q -f -k --check=crc32 --lzma2=dict=1MiB
else ifeq ($(MOD_COMPRESS),gzip)
  MOD_SUFFIX := .gz
  MOD_COMPRESS_CMD := gzip -n -f -k
else ifeq ($(MOD_COMPRESS),none)
  MOD_SUFFIX :=
  MOD_COMPRESS_CMD :=
else
  $(error unsupported MOD_COMPRESS='$(MOD_COMPRESS)', expected one of zstd, xz, gzip, none)
endif

# <built module>:<install directory relative to $(MODLIB)>
MHI_MODULES := \
	host/mhi.ko:kernel/drivers/bus/mhi/host \
	host/mhi_pci_semtech.ko:kernel/drivers/bus/mhi/host \
	host/uci.ko:kernel/drivers/bus/mhi/host \
	wwan/mhi_wwan_ctrl.ko:kernel/drivers/net/wwan \
	wwan/mhi_wwan_mbim.ko:kernel/drivers/net/wwan \
	wwan/mhi_dtr.ko:kernel/drivers/net/wwan \
	qrtr/qrtr-mhi.ko:kernel/net/qrtr

all:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) ccflags-y="-DCONFIG_MHI_BUS_DEBUG -DTRPATH=$(PWD)/host"

adb: clean
	$(MAKE) -C $(KERNELDIR) M=$(PWD) ccflags-y="-DCONFIG_MHI_BUS_DEBUG -DTRPATH=$(PWD)/host -DADB_SUPPORT"

centos:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) ccflags-y="-DCENTOS"

clean:
	rm -rf *.symvers *.order
	cd host; rm -rf *.o *.cmd *.ko *.mod.c .tmp_versions *.o.ur-safe *.symvers *.order .cache.mk .mhi* .built-in* built-in.a *.ko.zst *.ko.gz *.ko.xz *.mod
	cd wwan; rm -rf *.o *.cmd *.ko *.mod.c .tmp_versions *.o.ur-safe *.symvers *.order .cache.mk .mhi* .built-in* built-in.a *.ko.zst *.ko.gz *.ko.xz *.mod
	cd qrtr; rm -rf *.o *.cmd *.ko *.mod.c .tmp_versions *.o.ur-safe *.symvers *.order .cache.mk .mhi* .built-in* built-in.a *.ko.zst *.ko.gz *.ko.xz *.mod

install:
	@echo "  INSTALL $(MODLIB) (module compression: $(MOD_COMPRESS))"
	@if [ -z "$(KCONFIG)" ]; then \
		echo "  WARNING: no kernel config found, assuming uncompressed modules"; \
	fi
	@set -e; \
	for entry in $(MHI_MODULES); do \
		src=$${entry%%:*}; \
		dst=$(MODLIB)/$${entry##*:}; \
		mod=$${src##*/}; \
		if [ ! -f "$$src" ]; then \
			echo "  ERROR: $$src is missing, run 'make' first" >&2; \
			exit 1; \
		fi; \
		sudo install -d "$$dst"; \
		if [ -n "$(MOD_SUFFIX)" ]; then \
			$(MOD_COMPRESS_CMD) "$$src"; \
			sudo install -m 644 "$$src$(MOD_SUFFIX)" "$$dst/$$mod$(MOD_SUFFIX)"; \
		else \
			sudo install -m 644 "$$src" "$$dst/$$mod"; \
		fi; \
		for stale in "" .gz .xz .zst; do \
			[ "$$stale" = "$(MOD_SUFFIX)" ] || sudo rm -f "$$dst/$$mod$$stale"; \
		done; \
	done
	sudo depmod

uninstall:
	@set -e; \
	for entry in $(MHI_MODULES); do \
		src=$${entry%%:*}; \
		dst=$(MODLIB)/$${entry##*:}; \
		mod=$${src##*/}; \
		sudo rm -f "$$dst/$$mod" "$$dst/$$mod".gz "$$dst/$$mod".xz "$$dst/$$mod".zst; \
	done
	sudo depmod

