EmbLogic's Blog

DEVICE DRIVER

successfully done write and read for multiple qset and multiple quantum and for single qset here is rcs file

amit.c
head 1.8;
access;
symbols;
locks
amitdalal:1.8; strict;
comment @ * @;

1.8
date 2013.07.11.17.37.19; author amitdalal; state Exp;
branches;
next 1.7;

1.7
date 2013.07.11.06.05.24; author amitdalal; state Exp;
branches;
next 1.6;

1.6
date 2013.07.11.05.37.03; author amitdalal; state Exp;
branches;
next 1.5;

1.5
date 2013.07.11.05.18.10; author amitdalal; state Exp;
branches;
next 1.4;

1.4
date 2013.07.11.00.41.53; author amitdalal; state Exp;
branches;
next 1.3;

1.3
date 2013.07.11.00.19.58; author amitdalal; state Exp;
branches;
next 1.2;

1.2
date 2013.07.11.00.00.36; author amitdalal; state Exp;
branches;
next 1.1;

1.1
date 2013.07.10.21.22.34; author amitdalal; state Exp;
branches;
next ;

desc
@added function of copy from user
printk the no of byte writen successfully for one quantum
@

1.8
log
@modified code for write to get required no of qset
add lnoq variable
@
text
@#include”header.h”
int create_qset(struct Scull_qset *qset,int nosqs) //creating qset and allocating memory to qset(64 bytes)
{
int lv;
printk(KERN_ALERT”BEGIN:SUCCESSFULLY : %s\n”,__func__);

if(!qset)
printk(KERN_ERR “error in creatin qset\n”);
for(lv=0;lvdata=kmalloc(sizeof(int *)*qset_size,GFP_KERNEL); //allocating memory to the *qset_size
qset=qset->next;
}
printk(KERN_ALERT”END:SUCCESSFULLY : %s\n”,__func__);
return 0;
}

int create_quantum(struct Scull_qset *qset,int noq) //creating quantum and locating memory to quantum(8 quantum per qset)
{
int i,count=0,lnoq=0;

printk(KERN_ALERT”BEGIN:SUCCESSFULLY : %s\n”,__func__);
if(!qset)
printk(KERN_ERR “error in creating quantum\n”);

while(qset!=NULL)
{
if(noq>quantum_size)
lnoq=quantum_size;
else
lnoq=noq;
for(i=0;idata[i]=kmalloc(quantum_size,GFP_KERNEL); //allocate memory to quantum
printk(KERN_ALERT”quantum number=%p”,&qset->data[i]);
count++;
}

noq=noq-quantum_size;
qset=qset->next;
}
printk(KERN_ALERT”quantum number=%d”,count);
printk(KERN_ALERT”END:SUCCESSFULLY : %s\n”,__func__);
return 0;
}

struct Scull_qset* create_scull_qset(int fnosqs) //creating scull_qset and allocating memory to it
{

struct Scull_qset *pdev,*start; //taking two pointer
int flag=0,i=0;
start=NULL;
printk(KERN_ALERT”BEGIN:SUCCESSFULLY : %s\n”,__func__);
for(i=0;inext=(struct Scull_qset *)(kmalloc(sizeof(struct Scull_qset),GFP_KERNEL)); //allocating memory to the next scull_qset
pdev=pdev->next;
}
}
printk(KERN_ALERT”END: SUCCESSFULLY: %s\n”,__func__);
return start;
}

ssize_t scull_read(struct file *filep,char __user *buff,size_t count, loff_t *offp)
{
struct Sculldev *pdev;
struct Scull_qset *qset;
int noq,nosqs,i,ret=0,loop=0;
int nobpq,noctr,nocsr;
noq=nosqs=nobpq=noctr=0;
nobpq=quantum_size;
nocsr=0;
noctr=count;

printk(KERN_ALERT”read open SUCCESSFULLY : %s\n”,__func__);
pdev=filep->private_data; //fetch the device
printk(KERN_ALERT”write open SUCCESSFULLY: %s\n”,__func__);
if(!pdev)
{
printk(“no private data”);
}
nosqs=count/(quantum_size*qset_size); //finding no. of scull_qset required
if(count%(quantum_size*qset_size))
nosqs++;
if(!pdev->scull_qset)
{
printk(“no scull_qset found\n”);
}
noq=count/quantum_size;
if(count%quantum_size);
noq++;
qset=pdev->scull_qset;

while(nosqs)
{
if(noq>quantum_size)
{
loop=quantum_size;
}
else
{
loop=noq;
}
for(i=0;iquantum_size) //if noctw>quantum_size ,nobpq is equql to the quantum_size that is 8
nobpq=quantum_size;
else
nobpq=noctr; //else nobpq is equal to the noctw

ret=copy_to_user(buff+nocsr,qset->data[i],nobpq); //copy data from user space
nocsr=nocsr+(nobpq-ret);
noctr=noctr-(nobpq-ret);
printk(KERN_ALERT”no. of byte successfully read=%d\n”,nocsr);
}
nosqs–;
qset=qset->next;
noq=noq-quantum_size;
}
printk(KERN_ALERT”no.of qset=%d\n”,loop);
printk(KERN_ALERT”END:read close SUCCESSFULLY: %s\n”,__func__);
return count-ret;

}

ssize_t scull_write(struct file *filep,const char __user *buff,size_t count, loff_t *offp)
{

struct Sculldev *pdev;
struct Scull_qset *qset;
int noq,nosqs,i,ret=0,loop=0;
int nobpq,noctw,nocsw;
noq=nosqs=nobpq=noctw=nocsw=0;

pdev=filep->private_data; //fetch the device
printk(KERN_ALERT”write open SUCCESSFULLY: %s\n”,__func__);
if(!pdev)
{
printk(“no private data”);
}
nosqs=count/(quantum_size*qset_size); //finding no. of scull_qset required
if(count % (quantum_size*qset_size))
nosqs++;
pdev->scull_qset=create_scull_qset(nosqs); //creating scull_qset(Sculldev *pdev pointing towards *scull_qset)
if(!pdev->scull_qset)
{
printk(“no scull_qset found\n”);
}
noq=count/quantum_size;
if(count%quantum_size)
noq++;

qset=pdev->scull_qset;
create_qset(qset,nosqs); //creating _qset
create_quantum(qset,noq); //creating _quantum
nobpq=quantum_size;
nocsw=0;
noctw=count;

while(nosqs)
{
if(noq>quantum_size)
{
loop=quantum_size;
}
else
{
loop=noq;
}
for(i=0;iquantum_size) //if noctw>quantum_size ,nobpq is equql to the quantum_size that is 8
nobpq=quantum_size;
else
nobpq=noctw; //else nobpq is equal to the noctw

ret=copy_from_user(qset->data[i],buff+nocsw,nobpq); //copy data from user space
printk(KERN_ALERT”quantum=%d->write=%s\n”,i,(char *)qset->data[i]); //data write in quantum
nocsw=nocsw+(nobpq-ret);
noctw=noctw-(nobpq-ret);
printk(KERN_ALERT”no. of byte successfully write=%d\n”,nocsw);
printk(KERN_ALERT”no. of byte not write=%d\n”,ret);
}
nosqs–;
qset=qset->next;
noq=noq-quantum_size;
}
printk(KERN_ALERT”no.of qset=%d\n”,loop);

printk(KERN_ALERT”write close SUCCESSFULLY : %s\n”,__func__);
return count-ret;
}

int scull_open(struct inode *inodep, struct file *filep)
{

struct Sculldev* pdev;
printk(KERN_ALERT”OPEN SUCCESSFULLY :%s\n”,__func__);
pdev=(struct Sculldev *)container_of(inodep->i_cdev, struct Sculldev, var_dev);// map the memeory allocated to Sculldev to VFS which is going to return pointer
filep->private_data =pdev; //all information stored in pdev pointer conatining address

if((filep->f_flags && O_ACCMODE) == O_WRONLY)
{

printk(KERN_ALERT”FILE OPEN\n”);
scull_trim(pdev);
}

printk(KERN_ALERT”close SUCCESSFULLY: %s\n”,__func__);
return 0;
}

int scull_trim(struct Sculldev *fsculldev)
{
printk(KERN_ALERT”BEGIN :%s\n”,__func__);
if(!fsculldev)
{
printk(KERN_ERR”no scull found\n”);
}
if(!fsculldev->scull_qset)
{
printk(KERN_INFO”no scullqset found\n”);
}
printk(KERN_ALERT”END: %s\n”,__func__);
return 0;
}

int scull_release(struct inode *inodep, struct file *filep)
{
printk(KERN_ALERT”OPEN SUCCESSFULLY release: %s\n”,__func__);
printk(KERN_ALERT”close SUCCESSFULLY release: %s\n”,__func__);
return 0;
}
void sculldev_initialisation(void)
{
int a,lv;
for(lv=0;lv<nod;lv++)
{
dev=MKDEV(majorno,lv); //lv=newminorno+1(allocating value of minorno and majorno to your dev)
printk("sculldev initialisation start :%s\n",__func__);
cdev_init(&sculldev[lv].var_dev, &fops);
sculldev[lv].var_dev.dev = dev; //storing that value of dev into dev which is defined in cdev.. through sculldev
sculldev[lv].var_dev.owner=THIS_MODULE;
sculldev[lv].var_dev.ops=&fops;
sculldev[lv].var_dev.count=nod;
sculldev[lv].open_count=0;

a=cdev_add(&sculldev[lv].var_dev,sculldev[lv].var_dev.dev, nod);

sculldev[lv].quantum_size=quantum_size;
sculldev[lv].device_size=device_size;
sculldev[lv].data_size=data_size;
sculldev[lv].qset_size=qset_size;

majorno=MAJOR(dev);
minorno=MINOR(dev);
printk("value of a is—–%d\n",a);
printk("majorno assigned is—–%d\n",majorno);
printk("minorno assigned is—–%d\n",minorno);
}
printk("quantum size—–%d\n",quantum_size);
printk("device size—–%d\n",device_size);
printk("data size—–%d\n",data_size);
printk("qset size—–%d\n",qset_size);
printk("scull initiallisation complete :%s\n",__func__);
}

static int entry_init(void)
{
printk(KERN_ALERT "BEGIN: %s\n",__func__);
if(!majorno) //because majorno value is 0
{
ret=alloc_chrdev_region(&dev,minorno,nod,DRIVER_NAME); //when we are registering one device
if(ret==0)
{
printk("allocation is ok\n" );
}
}
else
{
register_chrdev_region(dev,nod,DRIVER_NAME); //when we are registering more than 1 device
printk("\nIt is in else region\n");
}
minorno = MINOR(dev);
majorno = MAJOR(dev);
printk(KERN_ALERT "END: %s\n",__func__);

memory();
sculldev_initialisation();
return 0;
}

void memory(void)

{
printk(KERN_ALERT "BEGIN: %s\n",__func__);
sculldev=kmalloc(sizeof(struct Sculldev)*nod , GFP_KERNEL); //allocate memory to Sculldev
memset(sculldev,'',nod *sizeof(struct Sculldev));

if(sculldev==0)
{
printk("memory allocation failed");
}
else
{
printk("memory allocation is done\n");
}
printk(KERN_ALERT "END: %s\n",__func__);

}

static void cleanup(void)
{
int lv;
printk(KERN_ALERT "BEGIN: %s\n",__func__);
printk(KERN_ALERT"\ngood bye\n");
for(lv=0;lv<nod;lv++)
{
//dev=MKDEV(majorno,lv);
cdev_del(&sculldev[lv].var_dev);
unregister_chrdev_region(sculldev[lv].var_dev.dev ,nod);
//unregister_chrdev_region(dev,nod);
}
kfree(sculldev); //free allocated memory to Sculldev
printk(KERN_ALERT"END:%s\n",__func__);
}
module_init(entry_init);
module_exit(cleanup);

@

1.7
log
@add loop variable in loop for reading
remove some bug in reading loop
@
text
@d2 1
a2 1
int create_qset(struct scull_qset *qset,int nosps) //creating qset and allocating memory to qset(64 bytes)
d9 1
a9 1
for(lv=0;lv<qset;lv++)
d18 1
a18 1
int create_quantum(struct scull_qset *qset,int noq) //creating quantum and locating memory to quantum(8 quantum per qset)
d20 1
a20 1
int i,count;
d25 8
a32 1
for(i=0;inext=(struct scull_qset *)(kmalloc(sizeof(struct scull_qset),GFP_KERNEL)); //allocating memory to the next scull_qset
d66 1
a72 1
printk(KERN_ALERT”read open SUCCESSFULLY : %s\n”,__func__);
d74 4
a77 4
struct scull_qset *qset;
int noq,nosqs,i,ret,loop;
int nobpq,noctw,nocsw;
noq=nosqs=nobpq=noctw=nocsw=0;
d79 2
a80 2
nocsw=0;
noctw=count;
d82 1
d90 1
a90 1
if(count%(quantum_size*qset_size));
a91 1
pdev->scull_qset=create_scull_qset(nosqs); //creating scull_qset(Sculldev *pdev pointing towards *scull_qset)
d100 1
a100 3
create_qset(qset,nosqs); //creating _qset
create_quantum(qset,noq); //creating _quantum

d111 1
a111 1
for(i=0;iquantum_size) //if noctw>quantum_size ,nobpq is equql to the quantum_size that is 8
d116 1
a116 1
nobpq=noctw; //else nobpq is equal to the noctw
d118 4
a121 5
ret=copy_to_user(buff+nocsw,qset->data[i],nobpq); //copy data from user space
printk(KERN_ALERT”quantum=%d->read=%s\n”,i,qset->data[i]); //data write in quantum
nocsw=nocsw+(nobpq-ret);
noctw=noctw-(nobpq-ret);
printk(KERN_ALERT”no. of byte successfully read=%d\n”,nocsw);
d128 3
a130 2
printk(KERN_ALERT”read close SUCCESSFULLY: %s\n”,__func__);
return count-ret;
d134 1
d139 2
a140 2
struct scull_qset *qset;
int noq,nosqs,i,ret,loop;
d151 1
a151 1
if(count%(quantum_size*qset_size));
d159 1
a159 1
if(count%quantum_size);
d161 1
d191 1
d328 1
a328 1
static void exit(void)
d344 1
a344 1
module_exit(exit);
@

1.6
log
@remove some bug from write function
added loop variable to specify no of quantum in 2nd qset
@
text
@a1 1

@

1.5
log
@creating read function
use copy to user function
modifing the copy to user function
@
text
@a130 3
nobpq=quantum_size;
nocsw=0;
noctw=count;
d152 3
d166 1
a166 1
for(i=0;idata=kmalloc(sizeof(int *)*qset_size,GFP_KERNEL); //allocating memory to the *qset_size
qset=qset->next;
d19 1
a19 1
int create_quantum(struct scull_qset *qset,int noq)
d29 1
a29 1
printk(KERN_ALERT”quantum number=%u”,&qset->data[i]);
d37 1
a37 1
struct scull_qset* create_scull_qset(int fnosqs)
d60 58
d119 1
a119 1
return 0;
d122 1
d131 4
d156 1
a156 5
printk(KERN_ALERT”write SUCCESSFULLY : %d\n”,count);
nobpq=quantum_size;
nocsw=0;
noctw=count;
while(nosqs)
d166 1
a166 1
for(i=0;i)
d100 1
a100 1

d104 2
a105 2

}*/
d117 5
a122 1
printk(KERN_ALERT”no. of byte successfully write=%d\n”,nocsw);
@

1.1
log
@Initial revision
@
text
@d93 4
a96 1
for(i=0;idata[i],buff,count); //copy data from user space
printk(KERN_ALERT”write in quantum=%s\n”,(char *)qset->data[i]); //data write in quantum
d118 1
d121 1
a121 1
return count;
@

header.h

head 1.2;
access;
symbols;
locks
amitdalal:1.2; strict;
comment @ * @;

1.2
date 2013.07.11.17.39.26; author amitdalal; state Exp;
branches;
next 1.1;

1.1
date 2013.07.10.21.25.25; author amitdalal; state Exp;
branches;
next ;

desc
@added asm header .h for copy from user
@

1.2
log
@remove init and cleanup prototype
@
text
@#include //need for registring( for everything)
#include
#include //needed for KERN_ALERT
#include //needed for kmalloc and flag of kmalloc
#include //needed cdev structure
#include
#include
#include
MODULE_LICENSE(“GPL”);
MODULE_AUTHOR(“AMIT DALAL”);
#define DRIVER_NAME “DALAL DRIVER”
#ifndef DEBUG
#define DEBUG
#endif
#define QUANTUM_SIZE 8
#define DATA_SIZE 0
#define DEVICE_SIZE 4096
#define QSET_SIZE 8

#ifndef MAJORNO
#define MAJORNO 0
#endif

#ifndef MINORNO
#define MINORNO 0
#endif

#ifndef NOD
#define NOD 10
#endif
dev_t dev;
int ret;
int minorno=MINORNO;
int majorno=MAJORNO;
int nod=NOD;
int quantum_size=QUANTUM_SIZE;
int qset_size=QSET_SIZE;
int data_size=DATA_SIZE;
int device_size=DEVICE_SIZE;

int scull_release(struct inode *, struct file *);
int scull_open(struct inode *, struct file *);
void sculldev_initialisation(void);
void memory(void);
ssize_t scull_read(struct file *,char __user *,size_t , loff_t *);
ssize_t scull_write(struct file *,const char __user *,size_t , loff_t *);

struct Sculldev //user defined structure Sculldev
{
struct Scull_qset *scull_qset; //pointing to 1st quqntum set
int quantum_size; //current quantum size
int qset_size; //current array size
int data_size; //amount of data stored here
int device_size;
int open_count;
struct cdev var_dev; //cdev structure defined in cdev header file
}*sculldev; //used for pointing Sculldev

int scull_trim(struct Sculldev *);
struct Scull_qset
{
struct Scull_qset *next;
void **data;
};
struct file_operations fops=
{
open:scull_open,
read:scull_read,
write:scull_write,
release:scull_release

};
struct Scull_qset* create_scull_qset(int);

@

1.1
log
@Initial revision
@
text
@d39 1
a39 1
int device_size=DEVICE_SIZE;
d50 1
a50 1
struct scull_qset *scull_qset; //pointing to 1st quqntum set
d60 1
a60 1
struct scull_qset
d62 1
a62 1
struct scull_qset *next;
d73 1
a73 1
struct scull_qset* create_scull_qset(int);
@

app.c
head 1.2;
access;
symbols;
locks
amitdalal:1.2; strict;
comment @ * @;

1.2
date 2013.07.11.00.32.38; author amitdalal; state Exp;
branches;
next 1.1;

1.1
date 2013.07.11.00.04.48; author amitdalal; state Exp;
branches;
next ;

desc
@extended the message in buff
@

1.2
log
@extended the message to check whether my multiple qset is working or not for above 64 byte
@
text
@#include
#include
#include
int main(int argc , char *argv[])
{
int fd,count,len;
char buff[]=”AMIT DALAL have great time in emblogic embedded technologies pvt ltd,noida,INDIA”;
if(argc!= 2)
{
perror(“error\n”);
}
else
{
//fd=open(argv[1],O_RDONLY);
fd=open(argv[1],O_WRONLY);
if(fd<0)
{
perror("error\n");
}
len=strlen(buff);
//count=read(fd,buff,len);
count=write(fd,buff,len);

if(count==len)
{
printf("successfully write\n");
}
if(count<len)
{
printf("partially write\n");
}
if(count==-1)
{
printf("some error in writing\n");
}
if(count==0)
{
printf("nothing write\n");
}
printf("no of byte write successfully=%d\n",count);
printf("fd open successfully=%d\n",fd);
printf("length is=%d\n",len);
close(fd);
return 0;
}

}
@

1.1
log
@Initial revision
@
text
@d7 1
a7 1
char buff[]="amit dalal have great time in emblogic pvt ltd,noida";
@

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>