From 10d8f5d4b09688ff020f8316a51017a2d8b4ec39 Mon Sep 17 00:00:00 2001
From: red-black-bonsai <5213811-red-black-bonsai@users.noreply.gitlab.com>
Date: Thu, 23 Jan 2020 13:42:02 +0100
Subject: [PATCH] medion-kai: get wifi working (!904)

---
 device/device-medion-kai/APKBUILD             | 21 +++++++--
 device/device-medion-kai/modules-load.conf    |  5 ++
 device/device-medion-kai/nvram_4330.txt       | 46 +++++++++++++++++++
 device/linux-medion-kai/APKBUILD              | 18 ++++++--
 .../linux-medion-kai/config-medion-kai.armv7  |  4 +-
 5 files changed, 86 insertions(+), 8 deletions(-)
 create mode 100644 device/device-medion-kai/modules-load.conf
 create mode 100644 device/device-medion-kai/nvram_4330.txt

diff --git a/device/device-medion-kai/APKBUILD b/device/device-medion-kai/APKBUILD
index 6b35d9c1dc5..34d85ef9045 100644
--- a/device/device-medion-kai/APKBUILD
+++ b/device/device-medion-kai/APKBUILD
@@ -2,7 +2,7 @@
 # Contributor: red-black-bonsai <red-black-bonsai@gmx.net>
 pkgname="device-medion-kai"
 pkgdesc="Lenovo Medion Lifetab S9714"
-pkgver=0.1
+pkgver=0.2
 pkgrel=0
 url="https://postmarketos.org"
 license="MIT"
@@ -10,7 +10,10 @@ arch="armv7"
 options="!check !archcheck"
 depends="postmarketos-base linux-medion-kai mkbootimg mesa-dri-swrast"
 makedepends="devicepkg-dev"
-source="deviceinfo"
+subpackages="$pkgname-nonfree-firmware:nonfree_firmware"
+source="deviceinfo
+	nvram_4330.txt
+	modules-load.conf"
 
 build() {
 	devicepkg_build $startdir $pkgname
@@ -18,7 +21,19 @@ build() {
 
 package() {
 	devicepkg_package $startdir $pkgname
+
+	install -Dm644 "$srcdir"/modules-load.conf \
+		"$pkgdir"/etc/modules-load.d/00-$pkgname.conf
 }
 
+nonfree_firmware() {
+	pkgdesc="Wifi firmware"
+	depends="firmware-aosp-broadcom-wlan"
+	mkdir "$subpkgdir"
+	install -D -m644 "$srcdir"/nvram_4330.txt \
+		"$subpkgdir"/lib/firmware/postmarketos/bcmdhd/bcm4330/nvram_4330.txt
+}
 
-sha512sums="8ec9bbe89a25cd1f986a70e985f6c045027300ed1a1c0323da3fddf3a9b223eaf0210a9b98fc7188196427ea9078a853ef78457a730494d166bdb1e0d84ea764  deviceinfo"
+sha512sums="8ec9bbe89a25cd1f986a70e985f6c045027300ed1a1c0323da3fddf3a9b223eaf0210a9b98fc7188196427ea9078a853ef78457a730494d166bdb1e0d84ea764  deviceinfo
+0024476dd9309982bcb90ceed8a6600f6ae3b13a2c7b4b5e8b0063e1d817ac76f65c8e6ca43ae0a176b9dd9a5f48d9ffc07f76d71bba34c51f352c8d1ae9eff6  nvram_4330.txt
+0ccd4cdc19fefb4c91fe5e11344e72a57c88e4f361240df7dcb26e2ee810c83e3ae84e70baf80bbcfa03e47151bd4729503ad8833b50ecd9a84de5a4c4c008dc  modules-load.conf"
diff --git a/device/device-medion-kai/modules-load.conf b/device/device-medion-kai/modules-load.conf
new file mode 100644
index 00000000000..d9860b3c51f
--- /dev/null
+++ b/device/device-medion-kai/modules-load.conf
@@ -0,0 +1,5 @@
+# This file contains the names of kernel modules that should be loaded
+# at boot time, one per line. Lines beginning with "#" are ignored.
+
+# wifi module
+bcmdhd
diff --git a/device/device-medion-kai/nvram_4330.txt b/device/device-medion-kai/nvram_4330.txt
new file mode 100644
index 00000000000..123cc2895ea
--- /dev/null
+++ b/device/device-medion-kai/nvram_4330.txt
@@ -0,0 +1,46 @@
+# bcm94330wlsdgb.txt
+manfid=0x2d0
+prodid=0x0552
+vendid=0x14e4
+devid=0x4360
+boardtype=0x0552
+boardrev=0x11
+# this design has 2.4GHz SP3T switch
+boardflags=0x00080200
+nocrc=1
+xtalfreq=37400
+boardnum=22
+#macaddr=00:11:22:33:44:55
+ag0=255
+aa2g=1
+ccode=EU
+#NC028 PA
+pa0b0=5198
+pa0b1=-625
+pa0b2=-177
+rssismf2g=0xa
+rssismc2g=0x3
+rssisav2g=0x7
+maxp2ga0=0x4c
+# 2.4G Tx Power offsets
+cck2gpo=0x5555
+ofdm2gpo=0x77777777
+mcs2gpo0=0xBBBB
+mcs2gpo1=0xBBBB
+sromrev=3
+#il0macaddr=00:11:22:33:44:55
+wl0id=0x431b
+cckPwrOffset=5
+swctrlmap_2g=0x24142414,0x22122212,0x22122414,0x111212,0x1ff
+rfreg033=0x19
+rfreg033_cck=0x1f
+dacrate2g=160
+txalpfbyp2g=1
+bphyscale=17
+cckPwrIdxCorr=-15
+pacalidx2g=45
+tcgaintbl=1
+pacalath2g=180
+pacalidx2g1=55
+muxenab=0x10
+rssi_offset=10
diff --git a/device/linux-medion-kai/APKBUILD b/device/linux-medion-kai/APKBUILD
index 9f2a9ceb256..d816de8a760 100644
--- a/device/linux-medion-kai/APKBUILD
+++ b/device/linux-medion-kai/APKBUILD
@@ -4,7 +4,7 @@
 
 pkgname="linux-medion-kai"
 pkgver=3.1.10
-pkgrel=0
+pkgrel=1
 pkgdesc="Lenovo Medion Lifetab S9714 kernel fork"
 arch="armv7"
 _carch="arm"
@@ -43,7 +43,11 @@ prepare() {
 build() {
 	unset LDFLAGS
 	make ARCH="$_carch" CC="${CC:-gcc}" \
-		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-postmarketOS"
+		KBUILD_BUILD_VERSION="$((pkgrel + 1))-postmarketOS"
+	# the 'no-pic' flag is necessary to avoid the
+	# error 'Unknown symbol _GLOBAL_OFFSET_TABLE_ (err 0)' when loading the module
+	make ARCH="$_carch" CC="${CC:-gcc}" CFLAGS_MODULE="-fno-pic" \
+		KBUILD_BUILD_VERSION="$((pkgrel + 1))-postmarketOS" modules
 }
 
 package() {
@@ -64,10 +68,18 @@ package() {
 		error "Could not find zImage in $PWD!"
 		return 1
 	fi
+
+	# Modules
+	cd "$builddir"
+	unset LDFLAGS
+	make ARCH="$_carch" CC="${CC:-gcc}" \
+		KBUILD_BUILD_VERSION="$((pkgrel + 1))-postmarketOS" \
+		INSTALL_MOD_PATH="$pkgdir" INSTALL_MOD_STRIP=1 \
+		modules_install
 }
 
 sha512sums="c14af0ed4ada9d91b9fd42ca9150f6e57f8504d6236ea582732cdebedc530f031add7afbb57b4df16dbf6af4287ca60ad56b28e4d3c0c3c58ab6a3aba0b95dcc  linux-medion-kai-d10779497812934afcadc0da2940caf91f03ed0e.tar.gz
-ef5f1ce6566621c0e4b75f231a0fa9eec0cf39a5a4c665c90df1c1c92990e4b84a9e5c5bb2369c3e675a2cd39def0de19f168643d2b2c80a3bb2cb69f0f545e0  config-medion-kai.armv7
+2b613e713000fc1ea1795c5be68078d7fe3114116831f24e087304483bff2ea97a43b739cb9dcce34c040f9136b694454e84272c4697361ec620cec7b79c9783  config-medion-kai.armv7
 77eba606a71eafb36c32e9c5fe5e77f5e4746caac292440d9fb720763d766074a964db1c12bc76fe583c5d1a5c864219c59941f5e53adad182dbc70bf2bc14a7  gcc7-give-up-on-ilog2-const-optimizations.patch
 197d40a214ada87fcb2dfc0ae4911704b9a93354b75179cd6b4aadbb627a37ec262cf516921c84a8b1806809b70a7b440cdc8310a4a55fca5d2c0baa988e3967  gcc8-fix-put-user.patch
 ad0182a483791fc88e058838bc331b2f04a75ba291e763767babdb815efadfc3b4fda97e69e2e3f00a426cabea088e35297a92bd287592597d1e309be68ee92c  kernel-use-the-gnu89-standard-explicitly.patch
diff --git a/device/linux-medion-kai/config-medion-kai.armv7 b/device/linux-medion-kai/config-medion-kai.armv7
index 291ad163b00..1f60046aea4 100644
--- a/device/linux-medion-kai/config-medion-kai.armv7
+++ b/device/linux-medion-kai/config-medion-kai.armv7
@@ -1289,8 +1289,8 @@ CONFIG_BCMDHD_INSMOD_NO_FW_LOAD=y
 # CONFIG_BCMDHD_CUSTOM_REGULATORY_DOMAIN is not set
 # CONFIG_SD8797 is not set
 # CONFIG_BCMDHD_FXN_125_120 is not set
-CONFIG_BCMDHD_FXN_FW_PATH="/system/vendor/firmware/bcm4330/fw_bcmdhd.bin"
-CONFIG_BCMDHD_FXN_NVRAM_PATH="/system/etc/nvram_4330.txt"
+CONFIG_BCMDHD_FXN_FW_PATH="/lib/firmware/postmarketos/bcmdhd/bcm4330/fw_bcm4330_bg.bin"
+CONFIG_BCMDHD_FXN_NVRAM_PATH="/lib/firmware/postmarketos/bcmdhd/bcm4330/nvram_4330.txt"
 CONFIG_BCMDHD_FXN_195_89=m
 # CONFIG_HOSTAP is not set
 # CONFIG_IPW2100 is not set
-- 
GitLab