# johnfanv2/lenovolegionlinux architecture

> Architecture diagram of the GitHub repository johnfanv2/lenovolegionlinux, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/johnfanv2/lenovolegionlinux
- Repository: https://github.com/johnfanv2/lenovolegionlinux

## Overview

#### 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: [`kernel_module/legion-laptop.c`](https://github.com/johnfanv2/lenovolegionlinux/blob/main/kernel_module/legion-laptop.c) (legion-laptop driver), [`extra/service/legiond/modules/setapply.c`](https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/service/legiond/modules/setapply.c) (Preset application), [`python/legion_linux/legion_linux/legion.py`](https://github.com/johnfanv2/lenovolegionlinux/blob/main/python/legion_linux/legion_linux/legion.py) (Python hardware API), [`extra/smartfan/smartfan.sh`](https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/smartfan/smartfan.sh) (SmartFan controller)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_kernel["Kernel driver"]
  node_module_makefile["Module build workflow<br/>Makefile"]
  node_dkms["DKMS integration<br/>DKMS config<br/>[dkms.conf]"]
  node_driver{{"legion-laptop driver<br/>kernel module<br/>[legion-laptop.c]"}}
  node_firmware(("Laptop EC and ACPI<br/>hardware firmware"))
  node_linux_interfaces["Standard Linux controls<br/>kernel interfaces"]
end

subgraph group_controls["User controls"]
  node_python_api["Python hardware API<br/>Python domain layer<br/>[legion.py]"]
  node_cli["Command-line control<br/>Python CLI<br/>[legion_cli.py]"]
  node_gui["Desktop fan-curve UI<br/>PyQt GUI<br/>[legion_gui.py]"]
end

subgraph group_automation["Automation paths"]
  node_legiond_service["legiond service unit<br/>service integration<br/>[legiond.service]"]
  node_legiond{{"legiond daemon<br/>C daemon<br/>[legiond.c]"}}
  node_powerstate["Power-state monitor<br/>daemon module<br/>[powerstate.c]"]
  node_preset_apply["Preset application<br/>daemon module<br/>[setapply.c]"]
  node_profiles["AC/battery profiles<br/>policy data"]
  node_vendor_tools["Optional vendor tuning<br/>external tools"]
  node_smartfan_service["SmartFan service<br/>systemd service<br/>[smartfan.service]"]
  node_smartfan{{"SmartFan controller<br/>shell service<br/>[smartfan.sh]"}}
  node_workmode["SmartFan mode selection<br/>shell helper<br/>[workmode.sh]"]
  node_acpi_call["acpi_call interface<br/>kernel extension"]
end

subgraph group_delivery["Build and delivery"]
  node_packaging["Package builds<br/>packaging"]
  node_ci["CI and releases<br/>GitHub Actions<br/>[build.yml]"]
end

node_module_makefile -->|"builds and installs"| node_driver
node_dkms -->|"rebuilds"| node_driver
node_driver -->|"uses EC/ACPI"| node_firmware
node_driver -->|"exports"| node_linux_interfaces
node_python_api -->|"reads and writes"| node_linux_interfaces
node_cli -->|"uses"| node_python_api
node_gui -->|"uses"| node_python_api
node_legiond_service -->|"runs"| node_legiond
node_legiond -->|"monitors through"| node_powerstate
node_powerstate -->|"selects on state change"| node_preset_apply
node_profiles -->|"supplies presets"| node_preset_apply
node_preset_apply -->|"applies controls"| node_linux_interfaces
node_preset_apply -.->|"optionally invokes"| node_vendor_tools
node_smartfan_service -->|"runs"| node_smartfan
node_workmode -->|"selects mode"| node_smartfan
node_smartfan -->|"uses"| node_acpi_call
node_acpi_call -->|"calls firmware"| node_firmware
node_packaging -.->|"packages"| node_module_makefile
node_packaging -.->|"packages"| node_python_api
node_ci -->|"builds and publishes"| node_packaging

click node_module_makefile "https://github.com/johnfanv2/lenovolegionlinux/blob/main/kernel_module/Makefile"
click node_dkms "https://github.com/johnfanv2/lenovolegionlinux/blob/main/kernel_module/dkms.conf"
click node_driver "https://github.com/johnfanv2/lenovolegionlinux/blob/main/kernel_module/legion-laptop.c"
click node_python_api "https://github.com/johnfanv2/lenovolegionlinux/blob/main/python/legion_linux/legion_linux/legion.py"
click node_cli "https://github.com/johnfanv2/lenovolegionlinux/blob/main/python/legion_linux/legion_linux/legion_cli.py"
click node_gui "https://github.com/johnfanv2/lenovolegionlinux/blob/main/python/legion_linux/legion_linux/legion_gui.py"
click node_legiond_service "https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/service/legiond.service"
click node_legiond "https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/service/legiond/legiond.c"
click node_powerstate "https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/service/legiond/modules/powerstate.c"
click node_preset_apply "https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/service/legiond/modules/setapply.c"
click node_smartfan_service "https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/smartfan/smartfan.service"
click node_smartfan "https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/smartfan/smartfan.sh"
click node_workmode "https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/smartfan/workmode.sh"
click node_ci "https://github.com/johnfanv2/lenovolegionlinux/blob/main/.github/workflows/build.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_module_makefile,node_dkms,node_driver,node_firmware,node_linux_interfaces toneBlue
class node_python_api,node_cli,node_gui toneAmber
class node_legiond_service,node_legiond,node_powerstate,node_preset_apply,node_profiles,node_vendor_tools,node_smartfan_service,node_smartfan,node_workmode,node_acpi_call toneMint
class node_packaging,node_ci toneRose
```

## Components

### Kernel driver

Primary Linux hardware-control boundary.

- [Module build workflow](https://github.com/johnfanv2/lenovolegionlinux/blob/main/kernel_module/Makefile): Makefile · Builds, installs, reloads, and supports DKMS workflows. · `kernel_module/Makefile`
- [DKMS integration](https://github.com/johnfanv2/lenovolegionlinux/blob/main/kernel_module/dkms.conf): DKMS config · Rebuilds the module for new kernels. · `kernel_module/dkms.conf`
- [legion-laptop driver](https://github.com/johnfanv2/lenovolegionlinux/blob/main/kernel_module/legion-laptop.c): kernel module · Probes supported models and mediates EC and ACPI operations. · `kernel_module/legion-laptop.c`
- **Laptop EC and ACPI**: hardware firmware · Proprietary embedded-controller and firmware mechanisms.
- **Standard Linux controls**: kernel interfaces · hwmon/PWM, sysfs, debugfs, and platform-profile interfaces.

### User controls

- [Python hardware API](https://github.com/johnfanv2/lenovolegionlinux/blob/main/python/legion_linux/legion_linux/legion.py): Python domain layer · Discovers and reads or writes driver and existing kernel interfaces. · `python/legion_linux/legion_linux/legion.py`
- [Command-line control](https://github.com/johnfanv2/lenovolegionlinux/blob/main/python/legion_linux/legion_linux/legion_cli.py): Python CLI · `python/legion_linux/legion_linux/legion_cli.py`
- [Desktop fan-curve UI](https://github.com/johnfanv2/lenovolegionlinux/blob/main/python/legion_linux/legion_linux/legion_gui.py): PyQt GUI · Edits, applies, imports, and persists fan-curve presets. · `python/legion_linux/legion_linux/legion_gui.py`

### Automation paths

- [legiond service unit](https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/service/legiond.service): service integration · Runs profile automation under systemd or companion init support. · `extra/service/legiond.service`
- [legiond daemon](https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/service/legiond/legiond.c): C daemon · Monitors power and platform-profile state, then applies matching presets. · `extra/service/legiond/legiond.c`
- [Power-state monitor](https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/service/legiond/modules/powerstate.c): daemon module · Uses inotify to track power source and platform-profile state. · `extra/service/legiond/modules/powerstate.c`
- [Preset application](https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/service/legiond/modules/setapply.c): daemon module · `extra/service/legiond/modules/setapply.c`
- **AC/battery profiles**: policy data · Example fan-profile and policy data.
- **Optional vendor tuning**: external tools · CPU/GPU actions may invoke RyzenADJ, undervolting, or GPU utilities.
- [SmartFan service](https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/smartfan/smartfan.service): systemd service · Alternative Legion 7 Gen 10+ control path. · `extra/smartfan/smartfan.service`
- [SmartFan controller](https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/smartfan/smartfan.sh): shell service · Independent control implementation using acpi_call. · `extra/smartfan/smartfan.sh`
- [SmartFan mode selection](https://github.com/johnfanv2/lenovolegionlinux/blob/main/extra/smartfan/workmode.sh): shell helper · `extra/smartfan/workmode.sh`
- **acpi_call interface**: kernel extension · Alternative firmware-control boundary for SmartFan.

### Build and delivery

- **Package builds**: packaging · Native and Python distribution packaging.
- [CI and releases](https://github.com/johnfanv2/lenovolegionlinux/blob/main/.github/workflows/build.yml): GitHub Actions · Builds, validates, and publishes deliverables. · `.github/workflows/build.yml`

## Connections

- 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
