📂

Lecture on Linux Device Numbers and Files

Jul 10, 2024

Lecture on Linux Device Numbers and Files

Introduction

  • Topic: Using Linux device numbers and device files.
  • Resource: Source code will be available on a GitHub page.

Overview of the /dev Directory

  • Contains devices on the system.
  • Device connection to the kernel via device numbers.

Device Numbers

  • Major Device Number: Indicates the driver associated with the device.
  • Minor Device Number: Used by the kernel to distinguish between different devices handled by the same driver.

Linux Kernel Module

  • Allocate or register device numbers to link device files and kernel modules.
  • Device File Operations:
    • open calls the opening method in the kernel module.
    • write calls the write method in the kernel module.

Implementation Steps

  1. Setup: Copy folder & move module to dev_number.c.
  2. Include File System Header: #include <linux/fs.h>.
  3. Description: Updated to "Registers a device number and implements some callback functions".

Callback Functions

  • Opening Function: Called when device file is opened.

    • Type: static int
    • Example Name: driver_open
    • Arguments: struct inode, struct file
    • Returns 0 on success, negative value on error
    • Example: static int driver_open(struct inode *inode, struct file *file_instance) { printk(KERN_INFO "open was called"); return 0; }
  • Closing Function: Called when device file is closed.

    • Type: static int
    • Example Name: driver_close
    • Arguments: struct inode, struct file
    • Returns 0 on success, negative value on error
    • Example: static int driver_close(struct inode *inode, struct file *file_instance) { printk(KERN_INFO "close was called"); return 0; }
  • Define Operations: Struct containing file operations.

    • Example: static struct file_operations fops = { .owner = THIS_MODULE, .open = driver_open, .release = driver_close, };

Register Device Number

  1. Declare integer variable for return value.
  2. Pick a device number from /proc/devices.
    • Example: Major = 90, Minor = 0
  3. Call register_chrdev function.
    • Pass major number, device name, and file operations.
    • Example: int major_number = register_chrdev(90, "my_dev_number", &fops);
  4. Check return value.
    • If successful: Major number is assigned.
    • On error: Return negative value.

Unregister Device Number

  • Use unregister_chrdev to free device number.
    • Example: unregister_chrdev(90, "my_dev_number");

Testing

  1. Compile Module: Ensure no errors.
  2. Create Device File: Use mknod command.
    • Example: mknod /dev/my_device c 90 0
  3. Create a Test Program: Open and close device file.
    • Example Program (test.c): #include <stdio.h> #include <fcntl.h> int main() { int fd = open("/dev/my_device", O_RDONLY); if (fd < 0) { perror("Failed to open device"); return 1; } close(fd); return 0; }
  4. Check Kernel Logs: Verify messages from open/close functions.
    • Example: dmesg command.

Conclusion

  • Device files and numbers are crucial in linking hardware with the Linux kernel.
  • Upcoming: Programmatically creating device files within kernel modules.