Tag: Linux device drivers

  • Build Kernel Module Binary: 5 Powerful Steps for Beginners


    Learn step-by-step how to build kernel module binary in Linux. This beginner-friendly guide covers examples, commands, and real-world use cases.

    Build Kernel Module Binary Introduction

    Have you ever wondered how Linux can talk to new hardware without reinstalling or recompiling the entire operating system? The secret lies in kernel modules. These small, loadable pieces of code allow you to extend the Linux kernel’s functionality on the fly.

    Whether you’re writing a device driver, experimenting with system-level programming, or preparing for embedded systems interviews, learning how to build a kernel module binary is an essential skill.

    In this article, we’ll walk through:

    • What kernel modules are
    • How kernel module binaries work
    • Step-by-step process to build one
    • Common issues and solutions
    • Real-world applications

    By the end, you’ll know exactly how to write, compile, and load your own kernel module into Linux.

    What is a Kernel Module?

    A kernel module is essentially a plugin for the Linux kernel. Instead of rebuilding the entire kernel, you can load a module when needed.

    Key benefits of kernel modules:

    • Add functionality without rebooting
    • Keep the kernel modular and lightweight
    • Simplify hardware driver development
    • Debug or extend system behavior dynamically

    For example, when you connect a new USB device, Linux loads the appropriate USB driver module automatically.

    Kernel Module Binary Explained

    When you compile a kernel module, the output is a file with the .ko extension, known as a kernel object file. This is the binary format that the Linux kernel understands.

    Unlike user-space applications, which generate executables (a.out, .elf, etc.), kernel modules produce .ko files. These files are then inserted into the running kernel using commands like insmod or modprobe.

    Think of it like this:

    • Source code (.c) → Compiler + Kernel headers → Kernel module binary (.ko)

    Step-by-Step Guide: Build a Kernel Module Binary

    Let’s build a simple “Hello World” kernel module.

    Write the Kernel Module Code

    Create a new file called hello_module.c:

    #include <linux/init.h>
    #include <linux/module.h>
    
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("Nish");
    MODULE_DESCRIPTION("A simple Hello World Kernel Module");
    
    static int __init hello_init(void) {
        printk(KERN_INFO "Hello, Kernel World!\n");
        return 0;
    }
    
    static void __exit hello_exit(void) {
        printk(KERN_INFO "Goodbye, Kernel World!\n");
    }
    
    module_init(hello_init);
    module_exit(hello_exit);
    

    Create a Makefile

    A Makefile tells the kernel build system how to compile the module.

    obj-m += hello_module.o
    
    all:
    	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
    
    clean:
    	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
    

    Compile the Module

    Run the following in your terminal:

    make
    

    This generates:

    hello_module.ko
    

    This .ko file is your kernel module binary.

    Load and Test the Module

    Insert the module into the kernel:

    sudo insmod hello_module.ko
    

    Check system logs to see the message:

    dmesg | tail
    

    Remove the module:

    sudo rmmod hello_module
    

    Common Issues and Fixes of Build Kernel Module Binary

    IssueCauseFix
    No rule to make target 'modules'Kernel headers missingInstall with sudo apt install linux-headers-$(uname -r)
    Invalid module formatKernel version mismatchRecompile with the correct kernel headers
    Operation not permittedPermissions issueUse sudo for loading/unloading modules
    Module not found in /lib/modules/Wrong pathVerify uname -r and re-run make

    Real-World Applications of Kernel Modules

    Kernel modules aren’t just academic exercises. They power many real-world scenarios, such as:

    • Device Drivers: For USB, audio, graphics, and network cards
    • Security: Firewalls, intrusion detection, access monitoring
    • Performance Monitoring: Debugging tools, profiling utilities
    • Embedded Systems: Custom hardware drivers for IoT devices

    Example: In embedded projects, if you’re connecting a custom sensor to a BeagleBone Black or Raspberry Pi, you’ll likely write a kernel module binary to interface with it.

    FAQs of Build Kernel Module Binary

    Q1: Do I need to recompile the entire kernel to build a module?
    No. You only need the kernel headers for your current kernel version.

    Q2: Can kernel modules crash my system?
    Yes, poorly written modules can cause kernel panics. Always test on a non-critical machine.

    Q3: What’s the difference between insmod and modprobe?

    • insmod loads a module directly.
    • modprobe resolves dependencies and is preferred.

    Q4: Can I write kernel modules in C++?
    Technically yes, but the Linux kernel is written in C, and using C is strongly recommended.

    Q5: Where do kernel modules get stored?
    Typically in /lib/modules/$(uname -r)/.

    Conclusion of Build Kernel Module Binary

    Build Kernel Module Binary is one of the first steps into the exciting world of Linux kernel development. By writing a simple module, compiling it into a .ko binary, and inserting it into the kernel, you’ve just extended the Linux OS at runtime.

    This knowledge is crucial for:

    • Embedded software engineers
    • Linux developers
    • System programmers
    • Students preparing for interviews

    The more you practice, the more comfortable you’ll become with Linux internals and device driver development.

    If you found this helpful, you might also enjoy our detailed guide on Kernel Configuration and Compilation

  • Master I2C Bus on Linux: A Beginner’s Guide (2026)

    I2C Bus on Linux beginner-friendly guide with advantages, disadvantages, applications, and FAQs for embedded systems and IoT projects.

    If you are starting your journey in embedded systems or Linux device drivers, you will often come across the term I2C bus. It plays a major role in connecting sensors, displays, and other peripheral devices to Linux-based systems like Raspberry Pi, BeagleBone, or custom embedded boards. Let’s break it down in a beginner-friendly way.

    What is the I2C Bus?

    I2C (Inter-Integrated Circuit) is a two-wire communication protocol used to connect low-speed devices to processors or microcontrollers. It only uses:

    • SDA (Serial Data Line) – for data transfer
    • SCL (Serial Clock Line) – for synchronization

    This simplicity makes it perfect for embedded systems where multiple devices need to communicate with the CPU without requiring many pins.

    How I2C Works in Linux

    Linux has built-in support for I2C. You can interact with devices connected over I2C through:

    1. Kernel Drivers – Device drivers provide higher-level access.
    2. User Space Tools – Linux offers command-line utilities to check and communicate with I2C devices.

    Common I2C Tools in Linux

    • i2cdetect – Scan and detect I2C devices connected to your system.
    • i2cget – Read data from a device register.
    • i2cset – Write data to a device register.
    • i2cdump – Dump all registers of an I2C device.

    These commands are part of the i2c-tools package, which you can install on most Linux distributions.

    Real-World Example

    Imagine you have a temperature sensor connected via I2C to a Raspberry Pi.

    • You run i2cdetect -y 1 to find its address.
    • Then use i2cget to read the sensor’s register values.
    • Finally, you can display the temperature on a screen or log it into a file.

    This makes I2C very popular in IoT projects, robotics, and automotive systems.

    Advantages of Using I2C on Linux

    Simple two-wire communication
    Supports multiple devices on the same bus
    Built-in Linux support with tools and drivers
    Widely used in embedded and industrial applications

    Disadvantages of I2C Bus on Linux

    Limited Speed – I2C is slower compared to SPI or UART.
    Short Distance Communication – It works best for short distances (within a board).
    Complexity with Many Devices – The more devices you connect, the higher the risk of address conflicts.
    Power Consumption – Pull-up resistors increase power usage in some cases.

    Applications of I2C Bus on Linux

    The I2C Bus on Linux is widely used in real-world projects:

    • IoT Devices – connecting temperature sensors, humidity sensors, and motion detectors.
    • Display Modules – driving small OLED or LCD screens.
    • Embedded Boards – BeagleBone, Raspberry Pi, and STM32 projects.
    • Automotive Systems – reading sensor data in Linux-based automotive ECUs.
    • Industrial Monitoring – logging pressure, temperature, and humidity in factories.

    Conclusion

    The I2C bus on Linux is a beginner-friendly communication method that allows easy connection of multiple devices using only two wires. Whether you’re working on a Raspberry Pi project, BeagleBone board, or professional embedded platform, learning I2C will help you connect sensors, displays, and other peripherals effortlessly.

    If you are just starting out, experiment with the i2c-tools package and try scanning and reading from a real sensor. It’s one of the best ways to get hands-on experience with Linux hardware communication.

    🔥 40% OFF
    I2C Interview Questions Bank

    📘 50 I²C Interview Questions Bank

    Prepare like a pro for your next embedded interview! This curated question bank covers everything from I²C fundamentals to advanced debugging concepts — ideal for freshers & professionals.

    $5  👉 $3
    ⬇️ Download Now

    Frequently Asked Questions (FAQ) on I2C Bus on Linux

    1. What is the I2C Bus on Linux?

    The I2C Bus on Linux is a two-wire communication system used to connect multiple devices like sensors, displays, and chips to Linux-based boards. Linux provides built-in drivers and tools to scan, read, and write data over the I2C bus.

    2. How do I check I2C devices in Linux?

    You can use the i2c-tools package. The most common command is:

    i2cdetect -y 1
    

    This will scan the I2C bus and show connected devices with their addresses.

    3. What are the advantages of using I2C Bus on Linux?

    • Requires only two wires (SDA & SCL)
    • Supports multiple devices on the same bus
    • Easy to use with built-in Linux support
    • Widely used in embedded and IoT applications

    4. What are the disadvantages of I2C Bus on Linux?

    • Slower compared to SPI or UART
    • Best for short-distance communication only
    • Possible address conflicts when many devices are connected
    • Slightly higher power consumption due to pull-up resistors

    5. What are common applications of I2C Bus on Linux?

    I2C is used in Raspberry Pi projects, BeagleBone boards, STM32 microcontrollers, and more. Applications include reading sensor data (temperature, humidity, motion), driving OLED/LCD displays, IoT projects, and automotive systems.

    6. Which Linux tools are used for I2C communication?

    The most common tools are:

    • i2cdetect – scans devices
    • i2cget – reads data from a device
    • i2cset – writes data to a device
    • i2cdump – dumps register values

    7. Can I use I2C on Raspberry Pi with Linux?

    Yes. Raspberry Pi has built-in support for I2C. After enabling it in the Raspberry Pi configuration, you can use i2c-tools to communicate with connected devices.