Created
August 22, 2013 22:24
-
-
Save 17twenty/6313566 to your computer and use it in GitHub Desktop.
Simple Misc Driver Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple Makefile to build a simple misc driver | |
# Nick Glynn <Nick.Glynn@feabhas.com> | |
# | |
obj-m += misc_example.o | |
KDIR := /lib/modules/$(shell uname -r)/build | |
PWD := $(shell pwd) | |
CC := $(CROSS_COMPILE)gcc | |
all: | |
$(MAKE) -C $(KDIR) M=${shell pwd} modules | |
clean: | |
-$(MAKE) -C $(KDIR) M=${shell pwd} clean || true | |
-rm *.o *.ko *.mod.{c,o} modules.order Module.symvers || true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <linux/miscdevice.h> | |
#include <linux/fs.h> | |
#include <linux/kernel.h> | |
#include <linux/module.h> | |
static int sample_open(struct inode *inode, struct file *file) | |
{ | |
pr_info("I have been awoken\n"); | |
return 0; | |
} | |
static int sample_close(struct inode *inodep, struct file *filp) | |
{ | |
pr_info("Sleepy time\n"); | |
return 0; | |
} | |
static ssize_t sample_write(struct file *file, const char __user *buf, | |
size_t len, loff_t *ppos) | |
{ | |
pr_info("Yummy - I just ate %d bytes\n", len); | |
return len; /* But we don't actually do anything with the data */ | |
} | |
static const struct file_operations sample_fops = { | |
.owner = THIS_MODULE, | |
.write = sample_write, | |
.open = sample_open, | |
.release = sample_close, | |
.llseek = no_llseek, | |
}; | |
struct miscdevice sample_device = { | |
.minor = MISC_DYNAMIC_MINOR, | |
.name = "simple_misc", | |
.fops = &sample_fops, | |
}; | |
static int __init misc_init(void) | |
{ | |
int error; | |
error = misc_register(&sample_device); | |
if (error) { | |
pr_err("can't misc_register :(\n"); | |
return error; | |
} | |
pr_info("I'm in\n"); | |
return 0; | |
} | |
static void __exit misc_exit(void) | |
{ | |
misc_deregister(&sample_device); | |
pr_info("I'm out\n"); | |
} | |
module_init(misc_init) | |
module_exit(misc_exit) | |
MODULE_DESCRIPTION("Simple Misc Driver"); | |
MODULE_AUTHOR("Nick Glynn <n.s.glynn@gmail.com>"); | |
MODULE_LICENSE("GPL"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment