Sunday, 18 May 2014

simple character driver using misc in linux

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/major.h>
#include <linux/capability.h>
#include <asm/uaccess.h>
#include <linux/miscdevice.h>


static ssize_t sample_char_read(struct file * file, char __user * buf,
                       size_t count, loff_t *ppos)
{
       printk("sample_char_read size(%ld)\n", count);
       return 0;
}

static ssize_t sample_char_write(struct file *filp, const char *buf,
                 size_t size, loff_t *offp)
{
       printk("sample_char_write size(%ld)\n", size);
       return size;

}

int sample_char_open(struct inode *inode, struct file *filp)
{
       printk("sample_char_open\n");
       return 0;
}

int sample_char_release(struct inode *inode, struct file *filp)
{

       printk("sample_char_release\n");
       return 0;
}

static struct file_operations sample_char_fops = {
       read:          sample_char_read,
       write:          sample_char_write,
       open:          sample_char_open,
       release:        sample_char_release,
};

static struct miscdevice misc_dev = {
  .minor =  MISC_DYNAMIC_MINOR,
        .name = "sample_misc_dev",
        .fops =  &sample_char_fops,
        .mode = S_IRWXUGO,
};

int init_module(void)
{
 int ret = 0;
 printk("\nLoading the sample misc char device driver\n");
 ret = misc_register(&misc_dev);
 if (ret)
 printk("Unable to register misc dev\n");
 return ret;
}

void cleanup_module(void)
{
 printk("\nUnloading the sample char device driver\n");
 misc_deregister(&misc_dev);
}

No comments:

Post a Comment