Coder Social home page Coder Social logo

dev-driver's Introduction

1a

module_init: when the module is loaded by the kernel

module_exit: when the module is removed from memory / unloaded

1b

This part assumes that your module is a driver otherwise it is a matter of building your module outside the kernel directory, with the target destination set to the kernel source directory

building

update modules Makefile to include your module. e.g. the hello module lives in linux-4.15/drivers/hello/

Append the Makefile in drivers/ with obj-m += hello/

Then append the Makefile in hello/ with obj-m += hello.o

finally build with

$ make modules

installing module

as per the previous assignment, we can install the module with

$ make modules_install

loading module

not sure if 'installing the module' meant 'loading' so this is included for completeness sake

$ cd /lib/modules/4.15.0/kernel/drivers/hello/ #or where ever your install your module
$ insmod hello.ko

1c

printk message of hello module

1d

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
+ #include <linux/moduleparam.h>

MODULE_LICENSE("GPL");

+ static char *who = "world";
+ module_param(who, charp, S_IRUGO);

static int hello_init(void) {
-    printk(KERN_ALERT "Hello, world\n");
+    printk(KERN_ALERT "Hello, %s\n", who);
    return 0;
}

static void hello_exit(void) {
-    printk(KERN_ALERT "Goodbye, cruel world\n");
+    printk(KERN_ALERT "Goodbye, %s\n", who);
}

module_init(hello_init);
module_exit(hello_exit);

parameterized module

Note: dmesg -c was called prior to loading the module

2a

$ mknod /dev/onebyte c 61 0

2b

device list

2c

Code for read

static ssize_t onebyte_read(struct file *filep, char __user *buf, size_t count, loff_t *f_pos) {
    if (*f_pos + count > CAPACITY) {
        count = CAPACITY - *f_pos;
    }

    if (copy_to_user(buf, onebyte_data, count) != 0) {
        return -EFAULT;
    }
    *f_pos += count;
    return count;
}

Code for write

static ssize_t onebyte_write(struct file *filep, const char __user *buf, size_t count, loff_t *f_pos) {
    if (*f_pos > CAPACITY) {
        return 0;
    }

    if (copy_from_user(onebyte_data, buf, CAPACITY) != 0) {
        return -EFAULT;
    }


    if (count > CAPACITY) {
        return -ENOSPC;
    }

    return CAPACITY;
}

Test cases test cases

Commits log

View Github repo

commit log

dev-driver's People

Contributors

madsonic avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.