#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#define MAJORNO 300
#define MINORNO 0
#define CHAR_DEV_NAME "kk_cdrv"
#define MAX_LENGTH 4000
#define SUCCESS 0
static char char_device_buf[MAX_LENGTH];
struct cdev *kk_cdev;
dev_t mydev;
int count=1,inuse=0;
static int char_dev_open(struct inode *inode,
struct file *file)
{
if(inuse)
{
printk(KERN_INFO "\nDevice busy %s\n",CHAR_DEV_NAME);
return -EBUSY;
}
inuse=1;
printk(KERN_INFO "Open operation invoked \n");
return SUCCESS;
}
static int char_dev_release(struct inode *inode, struct file *file)
{
inuse=0;
return SUCCESS;
}
static ssize_t char_dev_write(struct file *file, const char *buf, size_t lbuf, loff_t *ppos)
{
int nbytes = lbuf - copy_from_user (char_device_buf + *ppos, buf, lbuf);
*ppos += nbytes;
printk (KERN_INFO "\n Rec'vd data from app %s , nbytes=%d\n",char_device_buf,nbytes);
return nbytes;
}
static struct file_operations char_dev_fops = {
.owner = THIS_MODULE,
.write = char_dev_write,
.open = char_dev_open,
.release = char_dev_release
};
static int __init char_dev_init(void)
{
int ret;
mydev = MKDEV(MAJORNO,MINORNO);
register_chrdev_region(mydev,count,CHAR_DEV_NAME);
kk_cdev= cdev_alloc();
cdev_init(kk_cdev,&char_dev_fops);
ret=cdev_add(kk_cdev,mydev,count);
if( ret < 0 ) {
printk("Error registering device driver\n");
return ret;
}
printk(KERN_INFO"\nDevice Registered %s\n",CHAR_DEV_NAME);
memset(char_device_buf,'\0',MAX_LENGTH);
return 0;
}
static void __exit char_dev_exit(void)
{
cdev_del(kk_cdev);
unregister_chrdev_region(mydev,1);
printk(KERN_INFO "\n Driver unregistered \n");
}
module_init(char_dev_init);
module_exit(char_dev_exit);
MODULE_AUTHOR("kk");
MODULE_DESCRIPTION("Character Device Driver - Test");
MODULE_LICENSE("GPL");
/* End of code */
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <asm/current.h>
#define CHAR_DEV_NAME "kk_cdrv"
#define MAX_LENGTH 4000
#define SUCCESS 0
static char *char_device_buf;
struct cdev *kk_cdev;
dev_t mydev;
static int char_dev_open(struct inode *inode,
struct file *file)
{
printk(KERN_INFO "open operation invoked \n");
return SUCCESS;
}
static int char_dev_release(struct inode *inode,
struct file *file)
{
return SUCCESS;
}
static ssize_t char_dev_write(struct file *file,
const char *buf,
size_t lbuf,
loff_t *ppos)
{
int nbytes; /* Number of bytes written */
nbytes = lbuf - copy_from_user( char_device_buf + *ppos, /* to */
buf, /* from */
lbuf ); /* how many bytes */
*ppos += nbytes;
printk(KERN_INFO "Rec'vd from App data %s of length %d \n",char_device_buf, nbytes);
return nbytes;
}
static struct file_operations char_dev_fops = {
.owner = THIS_MODULE,
.write = char_dev_write,
.open = char_dev_open,
.release = char_dev_release,
};
static __init int char_dev_init(void)
{
int ret,count=1;
if (alloc_chrdev_region (&mydev, 0, count, CHAR_DEV_NAME) < 0) {
printk (KERN_ERR "failed to reserve major/minor range\n");
return -1;
}
if (!(kk_cdev = cdev_alloc ())) {
printk (KERN_ERR "cdev_alloc() failed\n");
unregister_chrdev_region (mydev, count);
return -1;
}
cdev_init(kk_cdev,&char_dev_fops);
ret=cdev_add(kk_cdev,mydev,count);
if( ret < 0 ) {
printk(KERN_INFO "Error registering device driver\n");
cdev_del (kk_cdev);
unregister_chrdev_region (mydev, count);
return -1;
}
printk(KERN_INFO"\nDevice Registered: %s\n",CHAR_DEV_NAME);
printk (KERN_INFO "Major number = %d, Minor number = %d\n", MAJOR (mydev),MINOR (mydev));
char_device_buf =(char *)kmalloc(MAX_LENGTH,GFP_KERNEL);
return 0;
}
static __exit void char_dev_exit(void)
{
cdev_del(kk_cdev);
unregister_chrdev_region(mydev,1);
kfree(char_device_buf);
printk(KERN_INFO "\n Driver unregistered \n");
}
module_init(char_dev_init);
module_exit(char_dev_exit);
MODULE_AUTHOR("kk");
MODULE_DESCRIPTION("Character Device Driver - Test");
MODULE_LICENSE("GPL");
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
obj-m :=chr_drv_skel.o
obj-m += chr_drv_dynamic.o
KDIR= /lib/modules/$(shell uname -r)/build
all:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
rm -rf $(wildcard *.o *.ko mo* Mo* *.mod.c )
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
int main()
{
int fd, i;
ssize_t ret;
char my_buf[12]="Hello world";
fd = open( "/dev/kk_cdrv", O_RDWR );
if(fd<0)
printf("failed acquiring file descriptor return status %d\n",fd);
/* write the contents of my buffer into the device */
ret = write( fd, my_buf, 12 );
ret= read(fd,my_buf,3000);
if(ret<0)
printf("read operation failed with return status %d\n",ret);
close(fd);
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#define MAJORNO 300
#define MINORNO 0
#define CHAR_DEV_NAME "kk_cdrv"
#define MAX_LENGTH 4000
#define SUCCESS 0
static char char_device_buf[MAX_LENGTH];
struct cdev *kk_cdev;
dev_t mydev;
int count=1,inuse=0;
static int char_dev_open(struct inode *inode,
struct file *file)
{
if(inuse)
{
printk(KERN_INFO "\nDevice busy %s\n",CHAR_DEV_NAME);
return -EBUSY;
}
inuse=1;
printk(KERN_INFO "Open operation invoked \n");
return SUCCESS;
}
static int char_dev_release(struct inode *inode, struct file *file)
{
inuse=0;
return SUCCESS;
}
static ssize_t char_dev_write(struct file *file, const char *buf, size_t lbuf, loff_t *ppos)
{
int nbytes = lbuf - copy_from_user (char_device_buf + *ppos, buf, lbuf);
*ppos += nbytes;
printk (KERN_INFO "\n Rec'vd data from app %s , nbytes=%d\n",char_device_buf,nbytes);
return nbytes;
}
static struct file_operations char_dev_fops = {
.owner = THIS_MODULE,
.write = char_dev_write,
.open = char_dev_open,
.release = char_dev_release
};
static int __init char_dev_init(void)
{
int ret;
mydev = MKDEV(MAJORNO,MINORNO);
register_chrdev_region(mydev,count,CHAR_DEV_NAME);
kk_cdev= cdev_alloc();
cdev_init(kk_cdev,&char_dev_fops);
ret=cdev_add(kk_cdev,mydev,count);
if( ret < 0 ) {
printk("Error registering device driver\n");
return ret;
}
printk(KERN_INFO"\nDevice Registered %s\n",CHAR_DEV_NAME);
memset(char_device_buf,'\0',MAX_LENGTH);
return 0;
}
static void __exit char_dev_exit(void)
{
cdev_del(kk_cdev);
unregister_chrdev_region(mydev,1);
printk(KERN_INFO "\n Driver unregistered \n");
}
module_init(char_dev_init);
module_exit(char_dev_exit);
MODULE_AUTHOR("kk");
MODULE_DESCRIPTION("Character Device Driver - Test");
MODULE_LICENSE("GPL");
/* End of code */
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <asm/current.h>
#define CHAR_DEV_NAME "kk_cdrv"
#define MAX_LENGTH 4000
#define SUCCESS 0
static char *char_device_buf;
struct cdev *kk_cdev;
dev_t mydev;
static int char_dev_open(struct inode *inode,
struct file *file)
{
printk(KERN_INFO "open operation invoked \n");
return SUCCESS;
}
static int char_dev_release(struct inode *inode,
struct file *file)
{
return SUCCESS;
}
static ssize_t char_dev_write(struct file *file,
const char *buf,
size_t lbuf,
loff_t *ppos)
{
int nbytes; /* Number of bytes written */
nbytes = lbuf - copy_from_user( char_device_buf + *ppos, /* to */
buf, /* from */
lbuf ); /* how many bytes */
*ppos += nbytes;
printk(KERN_INFO "Rec'vd from App data %s of length %d \n",char_device_buf, nbytes);
return nbytes;
}
static struct file_operations char_dev_fops = {
.owner = THIS_MODULE,
.write = char_dev_write,
.open = char_dev_open,
.release = char_dev_release,
};
static __init int char_dev_init(void)
{
int ret,count=1;
if (alloc_chrdev_region (&mydev, 0, count, CHAR_DEV_NAME) < 0) {
printk (KERN_ERR "failed to reserve major/minor range\n");
return -1;
}
if (!(kk_cdev = cdev_alloc ())) {
printk (KERN_ERR "cdev_alloc() failed\n");
unregister_chrdev_region (mydev, count);
return -1;
}
cdev_init(kk_cdev,&char_dev_fops);
ret=cdev_add(kk_cdev,mydev,count);
if( ret < 0 ) {
printk(KERN_INFO "Error registering device driver\n");
cdev_del (kk_cdev);
unregister_chrdev_region (mydev, count);
return -1;
}
printk(KERN_INFO"\nDevice Registered: %s\n",CHAR_DEV_NAME);
printk (KERN_INFO "Major number = %d, Minor number = %d\n", MAJOR (mydev),MINOR (mydev));
char_device_buf =(char *)kmalloc(MAX_LENGTH,GFP_KERNEL);
return 0;
}
static __exit void char_dev_exit(void)
{
cdev_del(kk_cdev);
unregister_chrdev_region(mydev,1);
kfree(char_device_buf);
printk(KERN_INFO "\n Driver unregistered \n");
}
module_init(char_dev_init);
module_exit(char_dev_exit);
MODULE_AUTHOR("kk");
MODULE_DESCRIPTION("Character Device Driver - Test");
MODULE_LICENSE("GPL");
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
obj-m :=chr_drv_skel.o
obj-m += chr_drv_dynamic.o
KDIR= /lib/modules/$(shell uname -r)/build
all:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
rm -rf $(wildcard *.o *.ko mo* Mo* *.mod.c )
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
int main()
{
int fd, i;
ssize_t ret;
char my_buf[12]="Hello world";
fd = open( "/dev/kk_cdrv", O_RDWR );
if(fd<0)
printf("failed acquiring file descriptor return status %d\n",fd);
/* write the contents of my buffer into the device */
ret = write( fd, my_buf, 12 );
ret= read(fd,my_buf,3000);
if(ret<0)
printf("read operation failed with return status %d\n",ret);
close(fd);
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
No comments:
Post a Comment
Please share yout thoughts