Simple Char driver
#include <linux/module.h>
#include <linux/kernel.h> /* printk() */
#include <linux/moduleparam.h>
#include <asm/uaccess.h>
#include <asm/pgtable.h>
#include <linux/fs.h>
#include <linux/gfp.h>
#include <linux/cdev.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
static void *buffer;
static int buf_size;
static ssize_t
acme_read(struct file *file, char __user *buf,size_t count,loff_t *ppos)
{
int remaining_bytes;
/* Number of bytes left to read in the open file */
remaining_bytes = min(buf_size - (int) (*ppos), (int) count);
if (remaining_bytes == 0) {
/* All read, returning 0 (End Of File) */
return 0;
}
if (copy_to_user(buf /* to */, *ppos+buffer /* from */, remaining_bytes)) {
return -EFAULT;
} else {
/* Increase the position in the open file */
*ppos += remaining_bytes;
return remaining_bytes;
}
}
static ssize_t
acme_write(struct file *file,const char __user *buf,size_t count,loff_t *ppos)
{
int remaining_bytes;
/* Number of bytes not written yet in the device */
remaining_bytes = buf_size - (*ppos);
if (count > remaining_bytes) {
/* Can't write beyond the end of the device */
return -EIO;
}
if (copy_from_user(*ppos+buffer /* to */, buf /* from */, count)) {
return -EFAULT;
} else {
/* Increase the position in the open file */
*ppos += count;
return count;
}
}
static struct file_operations acme_fops =
{
.owner = THIS_MODULE,
.read = acme_read,
.write = acme_write,
};
static int acme_count = 1;
static dev_t acme_dev;
static struct cdev *acme_cdev;
static int
hello_init (void)
{
buffer = kzalloc(8192,GFP_USER);
buf_size = 8192;
if(!buffer)
{
printk (KERN_INFO "kzalloc error.\n");
return -1;
}
if(alloc_chrdev_region(&acme_dev,0,acme_count,"acme1"))
{
printk (KERN_INFO "alloc chrdev error.\n");
return -1;
}
acme_cdev=cdev_alloc();
if(!acme_cdev)
{
printk (KERN_INFO "cdev alloc error.\n");
return -1;
}
acme_cdev->ops = &acme_fops;
acme_cdev->owner = THIS_MODULE;
if(cdev_add(acme_cdev,acme_dev,acme_count))
{
printk (KERN_INFO "cdev add error.\n");
return -1;
}
return 0;
}
static void
hello_cleanup (void)
{
printk (KERN_INFO "hello unloaded succefully.\n");
}
module_init (hello_init);
module_exit (hello_cleanup);
MODULE_LICENSE("GPL");
Recent Stories
Top DiscoverSDK Experts
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
X
Compare Products
Select up to three two products to compare by clicking on the compare icon () of each product.
{{compareToolModel.Error}}Now comparing:
{{product.ProductName | createSubstring:25}} X
{{CommentsModel.TotalCount}} Comments
Your Comment