Skip to content

Instantly share code, notes, and snippets.

@sanketplus
Created December 6, 2017 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanketplus/24888eb3d18cff52653f3340c024a53c to your computer and use it in GitHub Desktop.
Save sanketplus/24888eb3d18cff52653f3340c024a53c to your computer and use it in GitHub Desktop.
miscdevice.c
#define DEBUG
#include<linux/init.h>
#include<linux/module.h>
#include<linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
static char message[12] = {0};
static char *id = "7b5250202546";
static int err;
static int edu_open(struct inode *inode, struct file *file)
{
pr_debug("EDU Dev opened!\n");
return 0;
}
static int edu_release(struct inode *inode, struct file *file)
{
pr_debug("EDU Dev closed\n");
return 0;
}
static ssize_t edu_read(struct file *filp,
char *buffer, size_t length, loff_t *offset)
{
const char *id = "7b5250202546";
int bytes_read = 0;
pr_debug("EDU Dev read\n");
copy_to_user(buffer, id, length);
pr_debug("EDU Dev read over\n");
return length;
}
static ssize_t edu_write(struct file *file,
const char *buffer, size_t length, loff_t *offset)
{
pr_debug("EDU Dev write\n");
copy_from_user(message, buffer, 12);
pr_debug("Got this: %s\n", message);
err = strcmp(id, message);
pr_debug("cmp result: %d\n", err);
if (err == 0) {
pr_debug("write success\n");
return length;
}
pr_debug("write failed\n");
return -EINVAL;
}
static const struct file_operations edu_opts = {
.owner = THIS_MODULE,
.write = edu_write,
.open = edu_open,
.read = edu_read,
.release = edu_release,
};
struct miscdevice edu_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "eudyptula",
.fops = &edu_opts,
};
static int __init hello_init(void)
{
int error;
pr_debug("Hello World!\n");
error = misc_register(&edu_device);
if (error) {
pr_err("Dev registration failed :(\n");
return error;
}
pr_debug("Hello Registered :D\n");
return 0;
}
static void __exit hello_exit(void)
{
misc_deregister(&edu_device);
pr_debug("Bye World!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Sanket Patel");
MODULE_DESCRIPTION("A hello world module");
MODULE_VERSION("0.1");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment