From Eratic Earthworm, 2 Months ago, written in Plain Text.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <init.h>
#include <slab.h>
#include <cdev.h>
#define MYDEV_NAME "mycdrv"
#define KBUF_SIZE (size_t) (10*PAGE_SIZE)
static char *kbuf;
static dev_t first;
static unsigned int count = 1;
static int my_major = 700, my_minor = 0;
static struct cdev *my_cdev;
static int mycdrv_open (struct inode *inode, struct file *file)
{
printk(KERN_INFO "OPENING device: %s\n\n", MYDEV_NAME);
return 0;
}
static int mycdrv_release (struct inode *inode, struct file *file)
{
printk(KERN_INFO "CLOSING device: %s\n\n", MYDEV_NAME);
return 0;
}
static ssize_t;
mycdrv_read (struct file *file, char __user * buf, size_t lbuf, loff_t * ppos)
{
int nbytes = lbuf - copy_to_user(buf, kbuf + *ppos, lbuf);
*ppos += nbytes;
prink(KERN_INFO "\n READING function, nbytes=%d, pos %d\n", nbytes, (int)*ppos);
return nbytes;
}
static ssize_t;
mycdrv_write(struct file *file, char __user * buf, size_t lbuf, loff_t * ppos)
{
int nbytes = lbuf - copy_from_user(buf, kbuf + *ppos, lbuf);
*ppos += nbytes;
prink(KERN_INFO "\n WRITING function, nbytes=%d, pos %d\n", nbytes, (int)*ppos);
return nbytes;
}
static const struct file_operations mycdrv_fops = {
.owner = THIS_MODULE,
.read = mycdrv_read,
.write = mycdrv_write,
.open = mycdrv_open,
.release = mycdrv_release,
};
static int __init my_init (void)
{
kbuf = kmalloc(KBUF_SIZE, GFP_KERNEL);
first = MKDEV(my_major, my_minor);
register_chrdev_region(first, count, MYDEV_NAME);
my_cdev = cdev_alloc();
cdev_init(my_cdev, &mycdrv_fops);
cdev_add(my_cdev, first, count);
printk(KERN_INFO "\nSucceeded in registering character device %s\n", MYDEV_NAME);
return 0;
}
static void __exit my_exit (void)
{
cdev_del(my_cdev);
unregister_chrdev_region(first, count);
printk(KERN_INFO "\ndevice unregistered\n");
kfree(kbuf);
}
module_init(my_init);
module_exit(my_exit);
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}