#!/bin/bash

# Copyright 2026  Stuart Winter, Surrey, UK.
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
#

#############################################################################
# Script: /tools/load_subsyst
# Purpose: Load Kernel modules for a sub system
#          This is to help onboard new Hardware Models and is designed to be
#          run from within the Slackware Installer and/or the OS InitRD.
#          There is duplicated functionality within the OS InitRD and
#          Slackware Installer's "/load_kernel_modules" script. This version
#          is intended to be used when manual control is required, as it's
#          easier to control this from the shell than changing the Kernel
#          cmdline options. However, in some cases, the system won't come
#          up far enough to permit remote or even local access.
#          In such cases, you'd probably need to use the cmdline option
#          "kmod_load_subsyst_soc" and use this script for the rest.
# Author : Stuart Winter <mozes@slackware.com>
# Date...: 20-Jan-2026
#############################################################################
# Usage:
# load_subsyst platform
# load_subsyst usb
# load_subsyst pci
#############################################################################

# Only load a module if it's not loaded already.
# This prevents repeated messages.
function modprobe_once() {
   local modname="$1"
   [[ -z "$modname" ]] && return 0
   # Kernel reports loaded modules with '_' not '-'
   local norm="${modname//-/_}"
   if ! awk '{print $1}' /proc/modules | grep -Eq "^${norm}$"; then
      # modprobe accepts either form, but use the original name
      # Dry-run first to ensure it can be properly loaded:
      #modprobe -nq "${modname}" && modprobe "${modname}"
      modprobe "${modname}"
   fi
}

# Load the correct modules by its alias in /sys - e.g. pci:, of:, usb: )
function modprobe_by_alias() {
    local alias="$1"
    [[ -z "$alias" ]] && return 0
    #modprobe_once "$alias" 2>/dev/null
    #modprobe -qn "${alias}" && modprobe -q "${alias}" 2>/dev/null
    modprobe "${alias}"
}

# Walk a sysfs bus device directory and load modules by modalias:
function modprobe_bus_by_modalias() {
   local busdir="$1"
   local hwdevice alias
   for hwdevice in "${busdir}"/*; do
      if [[ -r "${hwdevice}/modalias" ]]; then
         alias="$(cat "${hwdevice}/modalias")"
         modprobe_by_alias "${alias}" >/dev/null 2>&1
      fi
   done
}

# Sanity check that the specified bus exists:
[ ! -d "/sys/bus/${1}/devices" ] && {
   echo "ERROR: Specified bus not found" ; exit 1 ;}

echo "Bringing up sub system: ${1} ... "
modprobe_bus_by_modalias /sys/bus/${1}/devices
