Wednesday, November 11, 2015
data transfer from kernel to user buffer using driver in linux
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/major.h>
#include <linux/capability.h>
#include <asm/uaccess.h>
static ssize_t sample_char_read(struct file * file, char __user * buf,
size_t count, loff_t *ppos)
{
int err=0;
char kbuf[32] = "Reading sample char device\n";
printk("sample_char_read size(%ld)\n", count);
err = copy_to_user((void *) buf, kbuf, strlen(kbuf));
if (err)
err = -EFAULT;
kbuf[32] = '\0';
return count;
}
static ssize_t sample_char_write(struct file * file, char __user * buf,
size_t count, loff_t *ppos)
{
int err=0;
char kbuf[100];
printk("sample_char_write size(%ld)\n", count);
err = copy_from_user((void *) kbuf, buf, count);
if (err)
err = -EFAULT;
kbuf[count] = '\0';
printk("The data writen :\n");
printk("%s\n",kbuf);
return count;
}
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,
};
int major_no;
int init_module(void)
{
printk("\nLoading the sample char device driver 2\n");
major_no = register_chrdev(0, "sample_char", &sample_char_fops);
printk("major no = %d\n",major_no);
return 0;
}
void cleanup_module(void)
{
unregister_chrdev(major_no,"sample_char");
printk("\nUnloading the sample char device driver 2\n");
}
Labels:
device driver
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment