How johnfanv2/lenovolegionlinux works
Purpose and Entry Points
LenovoLegionLinux provides Linux hardware support and controls for Lenovo Legion laptops: EC-backed fan curves and sensors, firmware settings, and platform power profiles. The primary runtime is an out-of-tree kernel module at kernel_module/legion-laptop.c, built/loaded via kernel_module/Makefile. User-facing entry points are the Python CLI and GUI at python/legion_linux/legion_linux/legion_cli.py and python/legion_linux/legion_linux/legion_gui.py.
Kernel Hardware Driver
kernel_module/legion-laptop.c is the architectural core. It probes supported DMI/BIOS combinations, interfaces with laptop ACPI/embedded-controller mechanisms, and protects model-specific features through allowlists. kernel_module/Makefile supports build, install, reload, DKMS-related, and kernel-patch workflows; kernel_module/dkms.conf enables rebuilds against new kernels.
Linux Interface Boundary
The driver translates proprietary firmware/EC operations into standard Linux kernel interfaces: hwmon sensor and PWM fan-curve attributes, debugfs fan-curve inspection, sysfs feature controls, and ACPI platform-profile integration. This is the central compatibility boundary: desktop tools and scripts consume kernel-standard files rather than hardware protocols. The driver also exposes controls such as fan lock/unlock, charge conservation, Fn lock, touchpad, and related model-dependent settings.
Python Control Layer
python/legion_linux/legion_linux/legion.py centralizes discovery and reading/writing of the driver’s sysfs/hwmon/debugfs interfaces, plus applicable existing kernel interfaces. It is the shared domain/API layer for both front ends. legion_cli.py offers scriptable commands, while the PyQt GUI in legion_gui.py edits, applies, imports, and persists fan-curve presets. Both require appropriate privileges because their writes ultimately target kernel control files.
Profile Automation Daemon
The C-based legiond service monitors power source and platform-profile state with inotify, selects a matching AC/battery fan preset, and applies it to hardware. Its composition is visible in extra/service/legiond/legiond.c, extra/service/legiond/modules/setapply.c, and extra/service/legiond/modules/powerstate.c. Service integration supports systemd/OpenRC through extra/service/legiond.service and companion files; example policy/profile data resides in extra/service/profiles/. Optional configured actions invoke external CPU/GPU tuning tools such as RyzenADJ, Intel undervolting utilities, nvidia-smi, or rocm-smi.
Alternative SmartFan Path
For Legion 7 Gen 10+ systems, SmartFan is an independent shell-service path that uses acpi_call rather than the full kernel module. extra/smartfan/smartfan.sh implements control behavior, extra/smartfan/smartfan.service integrates it with systemd, and extra/smartfan/workmode.sh provides mode selection. It is a separate hardware-control boundary, not a frontend over the standard driver interfaces.
Delivery and Runtime Infrastructure
Distribution packaging spans native package specs and Python packaging under deploy/build_packages/, while containerized build/test environments cover Arch, Fedora, SUSE, and Ubuntu in deploy/. GitHub Actions workflows in .github/workflows/build.yml and release automation validate and publish deliverables. Runtime dependencies materially include a compatible Linux kernel/headers, DKMS where used, PyQt and YAML for Python UI/configuration, systemd or OpenRC for automation, and optional acpi_call or GPU/CPU vendor tools.
Main entry points: legion-laptop.c legion-laptop driver · setapply.c Preset application · legion.py Python hardware API · smartfan.sh SmartFan controller
Components 20
Kernel driver
Primary Linux hardware-control boundary.
- Module build workflow · Builds, installs, reloads, and supports DKMS workflows.
kernel_module/Makefile - DKMS integration · Rebuilds the module for new kernels.
kernel_module/dkms.conf - legion-laptop driver · Probes supported models and mediates EC and ACPI operations.
kernel_module/legion-laptop.c - Laptop EC and ACPI · Proprietary embedded-controller and firmware mechanisms.
- Standard Linux controls · hwmon/PWM, sysfs, debugfs, and platform-profile interfaces.
User controls
- Python hardware API · Discovers and reads or writes driver and existing kernel interfaces.
python/legion_linux/legion_linux/legion.py - Command-line control
python/legion_linux/legion_linux/legion_cli.py - Desktop fan-curve UI · Edits, applies, imports, and persists fan-curve presets.
python/legion_linux/legion_linux/legion_gui.py
Automation paths
- legiond service unit · Runs profile automation under systemd or companion init support.
extra/service/legiond.service - legiond daemon · Monitors power and platform-profile state, then applies matching presets.
extra/service/legiond/legiond.c - Power-state monitor · Uses inotify to track power source and platform-profile state.
extra/service/legiond/modules/powerstate.c - Preset application
extra/service/legiond/modules/setapply.c - AC/battery profiles · Example fan-profile and policy data.
- Optional vendor tuning · CPU/GPU actions may invoke RyzenADJ, undervolting, or GPU utilities.
- SmartFan service · Alternative Legion 7 Gen 10+ control path.
extra/smartfan/smartfan.service - SmartFan controller · Independent control implementation using acpi_call.
extra/smartfan/smartfan.sh - SmartFan mode selection
extra/smartfan/workmode.sh - acpi_call interface · Alternative firmware-control boundary for SmartFan.
Build and delivery
- Package builds · Native and Python distribution packaging.
- CI and releases · Builds, validates, and publishes deliverables.
.github/workflows/build.yml
Connections 20
- Module build workflow → legion-laptop driver: builds and installs
- DKMS integration → legion-laptop driver: rebuilds
- legion-laptop driver → Laptop EC and ACPI: uses EC/ACPI
- legion-laptop driver → Standard Linux controls: exports
- Python hardware API → Standard Linux controls: reads and writes
- Command-line control → Python hardware API: uses
- Desktop fan-curve UI → Python hardware API: uses
- legiond service unit → legiond daemon: runs
- legiond daemon → Power-state monitor: monitors through
- Power-state monitor → Preset application: selects on state change
- AC/battery profiles → Preset application: supplies presets
- Preset application → Standard Linux controls: applies controls
- Preset application → Optional vendor tuning: optionally invokes
- SmartFan service → SmartFan controller: runs
- SmartFan mode selection → SmartFan controller: selects mode
- SmartFan controller → acpi_call interface: uses
- acpi_call interface → Laptop EC and ACPI: calls firmware
- Package builds → Module build workflow: packages
- Package builds → Python hardware API: packages
- CI and releases → Package builds: builds and publishes