Challenge

Project 1 - Develop the full featured char driver as a loaded module

Project Overview:

Kconfig Build System Integration: The module should be added to the Kernel configuration Kconfig build system with a tristate option.

Character Driver Features:

Open: Only one user is allowed to open the device. Others should be blocked.

Read and Write Operations: Store data to dynamically allocated memory and read from it.

Seek: Ability to move the offset of read/write location.

Max Memory Size: Configurable through compile time and load time.

Blocked Read/Write: Wait for data availability during read and for enough memory during write.

/proc/char_driver_size: Create a proc file reflecting the size of memory allocated to the char driver.

Goals:

Capability Building: Enable students to write and build loadable Linux kernel modules.

Advanced Features: Implement advanced capabilities in the character device module.

Key Highlights:

Linux Kernel Module Management: Compilation, installation, and removal.

Character Device Creation: Using mknod and major & minor numbers.

User-Space Interaction: Utilizing applications like cat and echo.

Concept Understanding: Modules, Makefile, major & minor numbers, memory allocation techniques.

Deliverables:

Linux Kernel Module (.ko format)

Makefile: Used for compiling the kernel module.

/proc/char_driver_size: Virtual file system to display memory allocation.

User Application: For interaction with character devices and printing to the console.

Expected Outcomes:

Module Compilation and Installation: Ability to compile and install the Linux kernel module.

Utilization of Kernel Module Utilities: Use of insmod, modprobe, and rmmod.

Character Driver Creation: Creation and linking with the device driver.

Read Mechanisms: Understanding of different read mechanisms including blocked and non-blocked reads.

Procfs Understanding: Learn about the virtual file system like procfs.

#include

#include

#include

#include

#include

#include

#include

#define DEVICE_NAME "char_driver"

#define BUF_SIZE 1024

static int major_number;

static char device_buffer[BUF_SIZE];

static int device_open = 0;

static int buf_size = BUF_SIZE;

static DEFINE_MUTEX(device_mutex);

static ssize_t device_read(struct file *, char *, size_t, loff_t *);

static ssize_t device_write(struct file *, const char *, size_t, loff_t *);

static loff_t device_llseek(struct file *, loff_t, int);

static int device_open_fn(struct inode *, struct file *);

static int device_release_fn(struct inode *, struct file *);

static const struct file_operations fops = {

    .read = device_read,

    .write = device_write,

    .llseek = device_llseek,

    .open = device_open_fn,

    .release = device_release_fn

};

static ssize_t device_read(struct file *filp, char *buffer, size_t len, loff_t *offset) {

    if (mutex_lock_interruptible(&device_mutex))

        return -ERESTARTSYS;

    if (*offset >= buf_size) {

        mutex_unlock(&device_mutex);

        return 0;

    }

    if (*offset + len > buf_size)

        len = buf_size - *offset;

    if (copy_to_user(buffer, device_buffer + *offset, len)) {

        mutex_unlock(&device_mutex);

        return -EFAULT;

    }

    *offset += len;

    mutex_unlock(&device_mutex);

    return len;

}

static ssize_t device_write(struct file *filp, const char *buffer, size_t len, loff_t *offset) {

    if (mutex_lock_interruptible(&device_mutex))

        return -ERESTARTSYS;

    if (*offset >= buf_size) {

        mutex_unlock(&device_mutex);

        return -ENOSPC;

    }

    if (*offset + len > buf_size)

        len = buf_size - *offset;

    if (copy_from_user(device_buffer + *offset, buffer, len)) {

        mutex_unlock(&device_mutex);

        return -EFAULT;

    }

    *offset += len;

    mutex_unlock(&device_mutex);

    return len;

}

static loff_t device_llseek(struct file *filp, loff_t offset, int whence) {

    loff_t new_pos;

    switch (whence) {

        case 0: /* SEEK_SET */

            new_pos = offset;

            break;

        case 1: /* SEEK_CUR */

            new_pos = filp->f_pos + offset;

            break;

        case 2: /* SEEK_END */

            new_pos = buf_size + offset;

            break;

        default:

            return -EINVAL;

    }

    if (new_pos < 0 || new_pos > buf_size)

        return -EINVAL;

    filp->f_pos = new_pos;

    return new_pos;

}

static int device_open_fn(struct inode *inode, struct file *file) {

    if (mutex_trylock(&device_mutex) == 0) {

        printk(KERN_ALERT "char_driver: Device in use by another process\n");

        return -EBUSY;

    }

    if (device_open)

        return -EBUSY;

    device_open++;

    try_module_get(THIS_MODULE);

    return 0;

}

static int device_release_fn(struct inode *inode, struct file *file) {

    mutex_unlock(&device_mutex);

    device_open--;

    module_put(THIS_MODULE);

    return 0;

}

static ssize_t size_read(struct file *file, char __user *buf, size_t len, loff_t *offset) {

    char temp[20];

    int size_length = snprintf(temp, sizeof(temp), "%d\n", buf_size);

    return simple_read_from_buffer(buf, len, offset, temp, size_length);

}

static ssize_t size_write(struct file *file, const char __user *buf, size_t len, loff_t *offset) {

    char temp[20];

    int ret = simple_write_to_buffer(temp, sizeof(temp), offset, buf, len);

    if (ret < 0)

        return ret;

    sscanf(temp, "%d", &buf_size);

    return len;

}

static const struct file_operations proc_fops = {

    .read = size_read,

    .write = size_write,

};

static int __init char_driver_init(void) {

    major_number = register_chrdev(0, DEVICE_NAME, &fops);

    if (major_number < 0) {

        printk(KERN_ALERT "char_driver: failed to register a major number\n");

        return major_number;

    }

    printk(KERN_INFO "char_driver: registered correctly with major number %d\n", major_number);

    proc_create("char_driver_size", 0666, NULL, &proc_fops);

    return 0;

}

static void __exit char_driver_exit(void) {

    remove_proc_entry("char_driver_size", NULL);

    unregister_chrdev(major_number, DEVICE_NAME);

    printk(KERN_INFO "char_driver: unregistered\n");

}

module_init(char_driver_init);

module_exit(char_driver_exit);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("Your Name");

MODULE_DESCRIPTION("A simple character device driver");

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.