Skip to content

Commit

Permalink
Improve install/uninstall thoroughness
Browse files Browse the repository at this point in the history
Support debug builds of the driver to be installable through dkms.
  • Loading branch information
Gnarus-G committed Feb 22, 2025
1 parent 5486489 commit c6e2dba
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 105 deletions.
25 changes: 9 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
ifneq ($(CC),clang)
CC=gcc
else
export LLVM=1
endif
DRIVERDIR?=`pwd`/driver

MODULEDIR?=/lib/modules/`uname -r`/kernel/drivers/usb

default: build

debug: EXTRA_CFLAGS := -DDEBUG
debug: default

build:
$(MAKE) CC=$(CC) EXTRA_CFLAGS=$(EXTRA_CFLAGS) -C $(DRIVERDIR)
$(MAKE) EXTRA_CFLAGS='$(EXTRA_CFLAGS)' -C $(DRIVERDIR)

build_debug: EXTRA_CFLAGS = -g -DDEBUG
build_debug: build

install_debug: debug install
install_debug: build_debug install

install: default
install: build
@sudo insmod $(DRIVERDIR)/maccel.ko;

@mkdir -p $(MODULEDIR)
Expand All @@ -34,10 +26,10 @@ uninstall: clean
@sudo rm -fv $(MODULEDIR)/maccel.ko
@sudo rmmod maccel

refresh: default uninstall
reinstall: uninstall
@sudo make install

refresh_debug: default uninstall
reinstall_debug: uninstall
@sudo make install_debug

build_cli:
Expand All @@ -62,4 +54,5 @@ udev_trigger:
udevadm trigger --subsystem-match=usb --subsystem-match=input --subsystem-match=hid --attr-match=bInterfaceClass=03 --attr-match=bInterfaceSubClass=01 --attr-match=bInterfaceProtocol=02

clean:
@rm -rf src pkg maccel maccel*.zst maccel-dkms*.log*
$(MAKE) -C $(DRIVERDIR) clean
71 changes: 39 additions & 32 deletions PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,53 @@ install=maccel.install
depends=("dkms")
makedepends=("git" "cargo")

# DEBUG_CFLAGS="$DEBUG_CFLAGS -DDEBUG"
options=(!debug)

source=("git+https://github.com/Gnarus-G/maccel.git")
sha256sums=("SKIP")

prepare() {
export RUSTUP_TOOLCHAIN=stable
platform="$(rustc -vV | sed -n 's/host: //p')"
cargo fetch --locked --target "${platform}" --manifest-path="${srcdir}/maccel/cli/Cargo.toml"
export RUSTUP_TOOLCHAIN=stable

platform="$(rustc -vV | sed -n 's/host: //p')"

cargo fetch --locked --target "${platform}" --manifest-path="${srcdir}/maccel/cli/Cargo.toml"
}

build() {
export RUSTUP_TOOLCHAIN=stable
export CARGO_TARGET_DIR=target
# Build the CLI
cargo build --profile=release-with-debug --frozen --all-features --manifest-path="${srcdir}/maccel/cli/Cargo.toml"
export RUSTUP_TOOLCHAIN=stable
export CARGO_TARGET_DIR=target

# Build the CLI
cargo build --profile=release-with-debug --frozen --all-features --manifest-path="${srcdir}/maccel/cli/Cargo.toml"
}

package() {
# Add group
install -Dm 644 "${srcdir}/maccel/maccel.sysusers" "${pkgdir}/usr/lib/sysusers.d/${_pkgname}.conf"

# Install Driver
install -Dm 644 "${srcdir}/maccel/dkms.conf" "${pkgdir}/usr/src/${_pkgname}-${pkgver}/dkms.conf"

# Set name and version
sed -e "s/@_PKGNAME@/${_pkgname}/" \
-e "s/@PKGVER@/${pkgver}/" \
-i "${pkgdir}/usr/src/${_pkgname}-${pkgver}/dkms.conf"

cp -r "${srcdir}/maccel/driver/." "${pkgdir}/usr/src/${_pkgname}-${pkgver}/"

# Install CLI
install -Dm 755 "${srcdir}/target/release-with-debug/maccel" "${pkgdir}/usr/bin/maccel"

# Install udev rules
install -Dm 644 "${srcdir}/maccel/udev_rules/99-maccel.rules" "${pkgdir}/usr/lib/udev/rules.d/99-maccel.rules"
install -Dm 755 "${srcdir}/maccel/udev_rules/maccel_param_ownership_and_resets" "${pkgdir}/usr/lib/udev/maccel_param_ownership_and_resets"

# Install License
install -Dm 644 "${srcdir}/maccel/LICENSE" "${pkgdir}/usr/share/licenses/${_pkgname}/LICENSE"
# Add group
install -Dm 644 "${srcdir}/maccel/maccel.sysusers" "${pkgdir}/usr/lib/sysusers.d/${_pkgname}.conf"

# Install Driver
install -Dm 644 "${srcdir}/maccel/dkms.conf" "${pkgdir}/usr/src/${_pkgname}-${pkgver}/dkms.conf"

# Escape path separators from debug flags values
EXTRA_CFLAGS=$(echo ${DEBUG_CFLAGS} | sed -e "s/\//\\\\\\//g")

# Set name and version
sed -e "s/@_PKGNAME@/${_pkgname}/" \
-e "s/@PKGVER@/${pkgver}/" \
-e "s/@EXTRA_CFLAGS@/'${EXTRA_CFLAGS}'/" \
-i "${pkgdir}/usr/src/${_pkgname}-${pkgver}/dkms.conf"

cp -r "${srcdir}/maccel/driver/." "${pkgdir}/usr/src/${_pkgname}-${pkgver}/"

# Install CLI
install -Dm 755 "${srcdir}/target/release-with-debug/maccel" "${pkgdir}/usr/bin/maccel"

# Install udev rules
install -Dm 644 "${srcdir}/maccel/udev_rules/99-maccel.rules" "${pkgdir}/usr/lib/udev/rules.d/99-maccel.rules"
install -Dm 755 "${srcdir}/maccel/udev_rules/maccel_param_ownership_and_resets" "${pkgdir}/usr/lib/udev/maccel_param_ownership_and_resets"

# Install License
install -Dm 644 "${srcdir}/maccel/LICENSE" "${pkgdir}/usr/share/licenses/${_pkgname}/LICENSE"
}
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ depending on with which your distro's kernel was built.
curl -fsSL https://www.maccel.org/install.sh | sudo sh
```

If you choose to build the cli from source, you'll need [`cargo`](https://www.rust-lang.org/tools/install)

### Arch (PKGBUILD)

```sh
Expand Down Expand Up @@ -123,6 +125,22 @@ For example you might encounter such an error:
And you'll have to find a version of `gcc` that matches. This will be more or less annoying
depending on your distro and/or how familiar you are with it.

### Miscellaneous

If you notice any weird behavior and are looking to investigate it,
then try a debug build of the driver. Run this modified install command.

```sh
curl -fsSL https://www.maccel.org/install.sh | sudo DEBUG=1 sh
```
Watch the extra log messages flowing though:

```sh
dmesg -w
```

This debugging experience might be lacking still. Feel free to report any issue

## References

- https://lwn.net/Kernel/LDD3/
Expand All @@ -138,6 +156,8 @@ depending on your distro and/or how familiar you are with it.
- https://linux-kernel-labs.github.io/refs/heads/master/labs/device_drivers.html
- https://www.youtube.com/watch?v=oX9ZwMQL2f4
- https://gist.github.com/fstiehle/17fca11d7d1b4c2b8dfd982e1cf39caf
- https://man.archlinux.org/man/PKGBUILD.5#OPTIONS_AND_DIRECTIVES
- https://stackoverflow.com/questions/669452/are-double-square-brackets-preferable-over-single-square-brackets-in-b

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion dkms.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PACKAGE_NAME="@_PKGNAME@"
PACKAGE_VERSION="@PKGVER@"
MAKE[0]="make KVER=$kernelver"
MAKE[0]="make KVER=$kernelver EXTRA_CFLAGS=@EXTRA_CFLAGS@"
CLEAN="make clean"
BUILT_MODULE_NAME[0]="@_PKGNAME@"
DEST_MODULE_LOCATION[0]="/kernel/drivers/usb"
Expand Down
13 changes: 12 additions & 1 deletion driver/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@ obj-m += maccel.o
KVER ?= $(shell uname -r)
KDIR ?= /lib/modules/$(KVER)/build

override EXTRA_CFLAGS += -DFIXEDPT_BITS=32

ifneq ($(CC),clang)
CC=gcc
else
export LLVM=1
endif

build:
$(MAKE) -C $(KDIR) M=$(CURDIR)
$(MAKE) CC=$(CC) EXTRA_CFLAGS='$(EXTRA_CFLAGS)' -C $(KDIR) M=$(CURDIR)

build_debug: EXTRA_CFLAGS += -g -DDEBUG
build_debug: build

clean:
$(MAKE) -C $(KDIR) M=$(CURDIR) clean
Expand Down
3 changes: 1 addition & 2 deletions driver/dbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
if (DEBUG_TEST) \
printk(KERN_INFO "%s:%d:%s(): " #fmt "\n", __FILE__, __LINE__, __func__, \
__VA_ARGS__); \
} \
while (0)
} while (0)
#else
#define dbg_std(fmt, ...) \
do { \
Expand Down
92 changes: 63 additions & 29 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# MACCEL_BRANCH
#!/bin/sh

BRANCH=${BRANCH:-""}
DEBUG=${DEBUG:-0}
BUILD_CLI_FROM_SOURCE=${BUILD_CLI_FROM_SOURCE:-0}

set -e

Expand Down Expand Up @@ -53,10 +57,10 @@ setup_dirs() {
rm -rf /opt/maccel && mkdir -p /opt/maccel
cd /opt/maccel

if [[ -n $MACCEL_BRANCH ]]; then
print_bold "Will do an install, using the branch: $MACCEL_BRANCH\n"
if [ -n "$BRANCH" ]; then
print_bold "Will do an install, using the branch: $BRANCH\n"
git clone --depth 1 --no-single-branch https://github.com/Gnarus-G/maccel.git .
git switch $MACCEL_BRANCH
git switch $BRANCH
else
git clone --depth 1 https://github.com/Gnarus-G/maccel.git .
fi
Expand Down Expand Up @@ -88,31 +92,61 @@ version_update_warning() {
fi
}

install_driver_dkms() {
# Install Driver
install -Dm 644 "$(pwd)/dkms.conf" "/usr/src/maccel-${VERSION}/dkms.conf"

# Set name and version
sudo sed -e "s/@_PKGNAME@/maccel/" \
-e "s/@PKGVER@/${VERSION}/" \
-i "/usr/src/maccel-${VERSION}/dkms.conf"

sudo cp -r "$(pwd)/driver/." "/usr/src/maccel-${VERSION}/"

sudo dkms install "maccel/${VERSION}"

print_bold $(print_green "[Required]")
print_bold ' Make sure to run `modprobe maccel` after install\n'
print_bold $(print_green "[Required]")
print_bold ' unless you have `modprobe_on_install` added to your dkms config.\n'
install_driver_dkms() {
dkms_version=$(cat PKGBUILD | grep "pkgver=" | grep -oP '\d.\d.\d')

# Uninstall any old ones
test -n "$(sudo dkms status maccel | grep 'maccel')" && {
maccel_dkms_status=$(sudo dkms status maccel | grep 'maccel')
sudo rmmod maccel;
curr_dkms_vesions=$(echo $maccel_dkms_status | grep -oP '\d.\d.\d');
echo $curr_dkms_vesions | xargs -I {} sudo dkms remove maccel/{};
}

# Install Driver
install -Dm 644 "$(pwd)/dkms.conf" "/usr/src/maccel-${dkms_version}/dkms.conf"

DEBUG_CFLAGS=""
if [ $DEBUG -eq 1 ]; then
print_bold "Debug build enabled\n"
DEBUG_CFLAGS="-g -DDEBUG"
fi

# Set name and version
sudo sed -e "s/@_PKGNAME@/maccel/" \
-e "s/@PKGVER@/${dkms_version}/" \
-e "s/@EXTRA_CFLAGS@/'${DEBUG_CFLAGS}'/" \
-i "/usr/src/maccel-${dkms_version}/dkms.conf"

sudo cp -r "$(pwd)/driver/." "/usr/src/maccel-${dkms_version}/"

sudo dkms install --force "maccel/${dkms_version}"

# Note(Gnarus):
# This wouldn't ok in the .install file as noted in https://wiki.archlinux.org/title/DKMS_package_guidelines#Module_loading_automatically_in_.install
# But I think it's ok here.
sudo modprobe maccel
}

install_cli() {
curl -fsSL https://github.com/Gnarus-G/maccel/releases/download/v$VERSION/maccel-cli.tar.gz -o maccel-cli.tar.gz
tar -zxvf maccel-cli.tar.gz maccel_v$VERSION/maccel
mkdir -p bin
sudo install -m 755 -v -D maccel_v$VERSION/maccel* bin/
sudo ln -vfs $(pwd)/bin/maccel* /usr/local/bin/
if [ $(getconf LONG_BIT) -lt 64 ]; then
BUILD_CLI_FROM_SOURCE=1
fi

if [ $BUILD_CLI_FROM_SOURCE -eq 1 ]; then
export RUSTUP_TOOLCHAIN=stable
cargo build --release --manifest-path=cli/Cargo.toml
sudo install -m 755 `pwd`/cli/target/release/maccel /usr/local/bin/maccel
else
print_bold "Preparing to download and install the CLI tool...\n"
printf "If you want to build the CLI tool from source, then next time run: \n"
print_bold " curl -fsSL https://maccel.org/install.sh | sudo BUILD_CLI_FROM_SOURCE=1 sh \n"
curl -fsSL https://github.com/Gnarus-G/maccel/releases/download/v$VERSION/maccel-cli.tar.gz -o maccel-cli.tar.gz
tar -zxvf maccel-cli.tar.gz maccel_v$VERSION/maccel
mkdir -p bin
sudo install -m 755 -v -D maccel_v$VERSION/maccel* bin/
sudo ln -vfs $(pwd)/bin/maccel* /usr/local/bin/
fi

sudo groupadd -f maccel
}
Expand All @@ -124,8 +158,8 @@ install_udev_rules() {
}

trigger_udev_rules() {
udevadm control --reload-rules
udevadm trigger --subsystem-match=usb --subsystem-match=input --subsystem-match=hid --attr-match=bInterfaceClass=03 --attr-match=bInterfaceSubClass=01 --attr-match=bInterfaceProtocol=02
sudo udevadm control --reload-rules
sudo udevadm trigger --subsystem-match=usb --subsystem-match=input --subsystem-match=hid --attr-match=bInterfaceClass=03 --attr-match=bInterfaceSubClass=01 --attr-match=bInterfaceProtocol=02
}

# ---- Install Process ----
Expand Down Expand Up @@ -162,6 +196,6 @@ print_bold ' Add yourself to the "maccel" group\n'
print_bold $(print_green "[Recommended]")
print_bold ' usermod -aG maccel $USER\n'

if [[ -n "$ATTENTION" ]]; then
if [ -n "$ATTENTION" ]; then
printf "\n$ATTENTION\n"
fi
1 change: 1 addition & 0 deletions maccel.install
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
post_install() {
udevadm control --reload-rules
udevadm trigger --subsystem-match=usb --subsystem-match=input --subsystem-match=hid --attr-match=bInterfaceClass=03 --attr-match=bInterfaceSubClass=01 --attr-match=bInterfaceProtocol=02

echo ''
echo ' Add yourself to the "maccel" group and install the module!'
Expand Down
18 changes: 9 additions & 9 deletions udev_rules/maccel_param_ownership_and_resets
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#!/bin/bash
#!/bin/sh

PATH='/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin'

LOG_DIR=/var/opt/maccel/logs
mkdir -p $LOG_DIR

chown -v :maccel /sys/module/maccel/parameters/* &> $LOG_DIR/chown;
chown -v :maccel /dev/maccel &> $LOG_DIR/chown;
chmod g+r /dev/maccel &> $LOG_DIR/chmod;
chown -v :maccel /sys/module/maccel/parameters/* &>$LOG_DIR/chown
chown -v :maccel /dev/maccel &>$LOG_DIR/chown
chmod g+r /dev/maccel &>$LOG_DIR/chmod

# For persisting parameters values across reboots
RESET_SCRIPTS_DIR=/var/opt/maccel/resets
mkdir -p $RESET_SCRIPTS_DIR
chown -v :maccel $RESET_SCRIPTS_DIR
chmod -v g+w "$RESET_SCRIPTS_DIR"
ls $RESET_SCRIPTS_DIR/set_last_*_value.sh | xargs cat | sh &> $LOG_DIR/reset-scripts
ls $RESET_SCRIPTS_DIR/* | xargs chown -v :maccel &> $LOG_DIR/reset-scripts
ls $RESET_SCRIPTS_DIR/* | xargs chmod -v g+w &> $LOG_DIR/reset-scripts
chown -v :maccel $RESET_SCRIPTS_DIR
chmod -v g+w "$RESET_SCRIPTS_DIR"
ls $RESET_SCRIPTS_DIR/set_last_*_value.sh | xargs cat | sh &>$LOG_DIR/reset-scripts
ls $RESET_SCRIPTS_DIR/* | xargs chown -v :maccel &>$LOG_DIR/reset-scripts
ls $RESET_SCRIPTS_DIR/* | xargs chmod -v g+w &>$LOG_DIR/reset-scripts
Loading

0 comments on commit c6e2dba

Please sign in to comment.