#!/bin/sh
#
# Copyright (c) 2020, The ULBSD Project
# Copyright (c) 2020, The NomadBSD Project
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $Id: ulbsd-gfx-intel 34 2020-07-27 21:12:11Z sv $

path_intel_cfg="/usr/local/etc/X11/xorg.conf.d/10-intel.conf"
path_drm_legacy="/usr/local/share/ulbsd/drivers/drm_legacy"

i915_drm_legacy_ids() {
cat <<END_DRM_LEGACY_IDS
0x3577
0x2562
0x3582
0x358e
0x2572
0x2582
0x258a
0x2592
0x2772
0x27a2
0x27ae
0x2972
0x2982
0x2992
0x29a2
0x29b2
0x29c2
0x29d2
0x2a02
0x2a12
0x2a42
0x2e02
0x2e12
0x2e22
0x2e32
0x2e42
0x2e92
0xa001
0xa011
0x0042
0x0046
END_DRM_LEGACY_IDS
}

intel_xorg_cfg() {
cat << intel_xorg_cfg_END
Section "Device"
    Identifier "Intel Graphics"
    Driver     "intel"
    Option     "AccelMethod"    "uxa"
    Option     "TripleBuffer"   "true"
    Option     "HotPlug"        "true"
EndSection
intel_xorg_cfg_END
}

modesetting_xorg_cfg() {
cat << ms_xorg_cfg_END
Section "Device"
    Identifier "Intel Graphics"
    Driver     "modesetting"
    BusID      "PCI:$busID"
EndSection
ms_xorg_cfg_END
}

unit=$(sysctl -n hw.pci.default_vgapci_unit)
[ -z "$unit" -o "$unit" =  "-1" ] && unit=0
config=$(pciconf -lv | grep ^vgapci${unit} | head -1 | \
	sed -E 's#.* chip=0x([0-9a-z]{4})([0-9a-z]{4}).*$#\1:\2#')
busID=$(pciconf -lv | grep ^vgapci${unit} | head -1 | \
	sed -E 's/^vgapci[0-9]@pci[0-9]:([0-9:]+):.*$/\1/')
vendor=$(echo "$config" | cut -d: -f2)
device=$(echo "$config" | cut -d: -f1)
[ "$vendor" = 8086 ] || exit 1
if i915_drm_legacy_ids | grep -iq ${device}; then
	intel_xorg_cfg > "${path_intel_cfg}"
	sysrc kld_list+="${path_drm_legacy}/boot/modules/i915kms.ko"
	kldload -n "${path_drm_legacy}/boot/modules/i915kms.ko"
	echo "drm_legacy/i915kms.ko" > /var/run/gfxdriver
else
	modesetting_xorg_cfg > "${path_intel_cfg}"
	sysrc kld_list+=/boot/modules/i915kms.ko
	kldload -n /boot/modules/i915kms.ko
	echo "modules/i915kms.ko" > /var/run/gfxdriver
fi
exit 0
