SPIsvc Kernel Module Example

Spisvc module is used by spiService application, and provides a good example of using dependencies on applications and modules, loading the module on demand from an application, and using scripts for installation and removal of the modules.

Note
This module is already installed on the WP76 and WP77 modules, if you wish to try adding it manually remove the entry from the default.sdef first.

The install scripts, source code and mdef file have been provided:

legato/drivers/spisvc
    ├── scripts
    │   ├── install.sh
    │   └── remove.sh
    ├── spisvc.c
    └── spisvc.mdef

The spiService application is dependant on the spisvc kernel module being loaded into the system before the app starts. To set up the dependency the following has to be added to the requires: kernelModules: section of legato/apps/platformServices/spiService.adef.

requires:
{
#if ${LE_CONFIG_FEATURE_SPISVC} = y
    kernelModules:
    {
        $LEGATO_ROOT/drivers/spisvc/spisvc.mdef
    }
#endif
}

The spisvc only needs to be loaded once the spiService is started and can be removed when the spiService is stopped. To do this set load: type to manual in the spisvc.mdef

sources:
{
    spisvc.c
}

load: manual

scripts:
{
    install: $CURDIR/scripts/install.sh
    remove: $CURDIR/scripts/remove.sh
}

The source files are added to the sources: section of the .mdef:

sources:
{
    spisvc.c
}

Install and remove scripts must be used if you want to customize the load and unload behavior of a module. spisvc contains dependencies on another kernel module spidev that is already included in the Linux Distribution. To set the dependency it must be set in the install and removal script.

Install Script:

#!/bin/sh
KO_PATH=$1

modprobe -q spidev
insmod "$KO_PATH"

# Make 10 attempts to check whether dev file exists
# Sleep 1s in between

for i in $(seq 1 10)
do
  if [ ! "$(find /dev/spidev* 2> /dev/null | wc -l)" -eq "0" ]
  then
    exit 0
  fi
    sleep 1
done

# return error if device file hasn't been created after timeout
if [ "$i" -eq "10" ]
then
  exit 1
fi

exit 0

Removal Script:

#!/bin/sh

KO_PATH=$1

rmmod $KO_PATH
modprobe -rq spidev

exit 0

The last step is to add the spiService to the apps: section in your .sdef as well as add spisvc.mdef to the kernelModules: section of your .sdef.

apps:
{
    $LEGATO_ROOT/apps/platformServices/spiService
}

#if ${LE_CONFIG_FEATURE_SPISVC} = y
kernelModules:
{
    $LEGATO_ROOT/drivers/spisvc/spisvc.mdef
}
#endif

You should now be able to build your system including the kernel module and update your target with the new system including the spisvc module.

Once the new system is installed on your target you should be able to find the spisvc files in the following location:

/legato/systems/current/modules/
    ├── files
    │   └── spisvc
    │       └── scripts
    │           ├── install.sh
    │           └── remove.sh
    └── spisvc.ko