EmbLogic's Blog

Character Driver

Done with write call

RCS File

head 1.8;
access;
symbols;
locks; strict;
comment @ * @;
1.8
date 2013.07.14.10.18.16; author Ashish; state Exp;
branches;
next 1.7;

1.7
date 2013.07.13.07.47.07; author Ashish; state Exp;
branches;
next 1.6;

1.6
date 2013.07.11.08.29.33; author Ashish; state Exp;
branches;
next 1.5;

1.5
date 2013.07.11.05.31.28; author Ashish; state Exp;
branches;
next 1.4;

1.4
date 2013.07.09.10.25.39; author Ashish; state Exp;
branches;
next 1.3;

1.3
date 2013.07.09.10.15.33; author Ashish; state Exp;
branches;
next 1.2;

1.2
date 2013.07.09.08.29.36; author Ashish; state Exp;
branches;
next 1.1;

1.1
date 2013.07.08.09.52.37; author Ashish; state Exp;
branches;
next ;
desc
@Done with tri
trim.
@
1.8
log
@Some modification with refrence to maximum no. of bytes written in a particular scull
Write fn is already done.
moving to read fn.
@
text
@#include”header.h”

MODULE_LICENSE(“GPL”);
//—————————trim Fn————————————————//
int scull_trim(struct Sculldev *fsculldev)
{
if(!fsculldev)
{
printk(KERN_ERR”Error:No Sculldev\n”);
}
printk(“No error “);
return 0;
}

//————————————-SCULL OPEN———————————
int scull_open(struct inode *inodep,struct file *filep)
{
struct Sculldev *pdev;

printk(“\nScull open function start\n”);
pdev=container_of(inodep->i_cdev,struct Sculldev,c_dev); //mapping RAM memory on Device:Sculldev
filep->private_data=pdev; //preserving it on file as lsculldev is lost
if((filep->f_flags & O_ACCMODE)==O_WRONLY)
{
printk(KERN_ALERT “FILE OPEN “);
scull_trim(pdev);
}
printk(“Scull open function complete\n”);
return 0;
}

//—————————Scull realease———————————————
int scull_release(struct inode *node,struct file *filep)
{

printk(“Scull release function start\n”);
printk(“Scull release function complete\n”);
return 0;
}
//——————————Scull Write———————————————–
ssize_t scull_write(struct file *filep,const char __user *ubuff,size_t size,loff_t *loff )
{
struct Sculldev *lsculldev;
struct ScullQset *lscullqset;
int lv,mnobps,nobpq,noq; //nobps=no.of bytes per scull //noq is no. of quantums required.
int ret=noq=0;
int ls=nobpq=mnobps=0; //no. of bytes per quantum
int nobns=0; //no. if byte in next scull
int nobtw=size; //no. of bytes to write
int nobsw=0;int sno=1; //sno is scull no
int nosqs=0; // no. of scull
mnobps=quantum_size * qset_size; //maximum no. of byteis scull
printk(“\nStarting %s”,__func__);
lsculldev=filep->private_data;
if(!lsculldev)
{
printk(KERN_ALERT “ERROR lsculldev Not declared \n “);
}
noq=size/quantum_size; //no of quantum
if(size % quantum_size)
{
noq++;
}
printk(“no of Quantums are %d”,noq);

nosqs=size/(quantum_size * qset_size); //////this is no. of item required
if(size%(quantum_size * qset_size))
{
nosqs++;
}
printk(“No of SQSET require is %d “,nosqs); //finding how much Qset requires
lsculldev->scullqset=create_sqset(nosqs); //creating ScullQset in lsculldc which point to ScullQset
lscullqset=lsculldev->scullqset; //passing whole ScullQset structure
create_qset(lscullqset,nosqs); //passing whole Scullqset structure and no scull qset require
create_quantum(lscullqset,noq);
nobtw=size;
printk(“\n NO of bytes per quantum is %d “,nobpq);

// ret=copy_from_user(lscullqset->data[lv],ubuff,size);
//printk(“\n quantum no is and data : %s”,(char *)lscullqset->data[lv]);
printk(“\n NO of bytes to write is %d “,nobtw);

while(nosqs!=0)
{

if(nobtw>mnobps)
{
nobns=nobtw-mnobps; //no. of byte to write in next shell
nobtw=mnobps;
ls=noq-quantum_size; //in ls we store no. of quantums want in next shell
noq=quantum_size;
}
for(lv=0;lv<noq;lv++)

{
if(nobtw>quantum_size)
{
nobpq=quantum_size;
}
else
{
nobpq=nobtw;
}
ret=copy_from_user(lscullqset->data[lv],ubuff+nobsw,nobpq);//writing 8 bytes in a quantum
printk(“\nScull is %d quantum no is %d and data : %s”,sno,lv,(char *)lscullqset->data[lv]);
nobsw=nobsw+(nobpq-ret); //no of Bytes Sucessfull Written
nobtw=nobtw+(nobpq-ret);
}
noq=ls;
nobtw=nobns;
nosqs–;
sno++;

}
return size-ret ;

printk(“\nLO JI HOGAYA End %s”,__func__);

}
//==========================Creating ScullQset==========================================================
struct ScullQset *create_sqset(int fnosqs) //we use pointer here becaz it returns a an address …pointer
{
struct ScullQset *lscullqset;
struct ScullQset *start;
int flag=0;
int lv;
int count=0;
printk(“\n WE ARE In CREATE SCullQSET “);

for(lv=0;lv<fnosqs;lv++)
{
if(flag==0)
{
lscullqset=start=(struct ScullQset *)kmalloc(sizeof(struct ScullQset),GFP_KERNEL);//as in linklist we preserve first nodee and allocating memory to first node
flag++;
}
else
{
lscullqset->next=(struct ScullQset *)kmalloc(sizeof(struct ScullQset ),GFP_KERNEL);
lscullqset=lscullqset->next;
}
count++;
}
printk(“\nNo. of ScullQset created is %d”,count);
return start;
}

//===================================Creating Qset======================================>>>>>>.>
int create_qset(struct ScullQset *fscullqset,int fnosqs)
{
int lv;
printk(“\n We are in %s\n”,__func__);
for(lv=0;lv<fnosqs;lv++) //it make qset in more than one scull
{
fscullqset->data=kmalloc(sizeof(int *)*qset_size,GFP_KERNEL); //allocating memory to the data pointer of qset data
fscullqset=fscullqset->next; //pointing to next scullqset …
}
printk(“Success fully Ended %s”,__func__);
return 0;
}

//++++++++++++++++++++++++++++++Creating Quantum+++++++++++++++++++++++++++++++++++++++++++

int create_quantum(struct ScullQset *fscullqset,int fnoq)
{
int lv;int lnoq;int count=0;
printk(“Enter in %s”,__func__);
while(fscullqset!=NULL)
{ if(fnoq>quantum_size)
lnoq=quantum_size;
else
lnoq=fnoq;
for(lv=0;lv<lnoq;lv++)
{
fscullqset->data[lv]=kmalloc(quantum_size,GFP_KERNEL); //we are allocating memory in qset ke data me of quantum_size
count++; // no of quantum created
// printk(KERN_ALERT”quantum number=%u”,&fscullqset->data[lv]);
}
fnoq=fnoq-quantum_size;
fscullqset=fscullqset->next;;
}
printk(“\n no of quantum created %d”,count);

printk(“\n Exiting from %s”,__func__);
return 0;

}

 

//—————————Read Function——————————————————————
ssize_t scull_read(struct file *filep,char __user *ubuff,size_t size,loff_t *loff )
{
//int ret;
printk(“\n Starting %s “,__func__);
//ret=copy_from_user(lscullqset->data[],ubuff,size);
printk(“\nEnd %s”,__func__);
return 0;
}

//=====================================Scull init========================================================
void scull_init(void)
{
printk(“starting__func_\n”);
sculldev->quantum_size=QUANTUM_SIZE;
sculldev->qset_size=QSET_SIZE;
sculldev->data_size=DATA_SIZE;
sculldev->device_size=DEVICE_SIZE;
printk(“Quantum Size is %d “,sculldev->quantum_size);
printk(“\n %s”,__func__);
}

//======================================start=======================================================
static int start(void)
{
printk(KERN_ALERT “WELCOME TO DEVICE DRIVER\n”);
if(!majorno)
{
ret = alloc_chrdev_region(&dev, minorno, nod, DRIVER_NAME);
if(ret==0)
printk(KERN_ALERT “Registration done\n”);
else
printk(“Registration failure\n”);
majorno = MAJOR(dev); //MAJOR Is A Micro to fetch Major no. stored in dev //DEV has major +minor no. =20 bit (major no 12 + minor no 20 )
minorno = MINOR(dev);
}
printk(“Major number assigned is___ %d\n”,majorno);
printk(“Minor number assigned is___ %d\n”,minorno);
sculldev= kmalloc(sizeof(struct Sculldev) * nod, GFP_KERNEL);
memset(sculldev, ”,sizeof(sculldev)*nod);
dev=MKDEV(majorno,minorno);
cdev_init(&sculldev->c_dev, &fop);
sculldev->c_dev.owner=THIS_MODULE; //we goto c_dev owner through sculldec
sculldev->c_dev.ops=&fop;
sculldev->c_dev.count=nod;
cdev_add(&sculldev->c_dev, dev, nod); //c_dev main dev ka add
minorno = MAJOR(sculldev->c_dev.dev);
majorno = MINOR(sculldev->c_dev.dev);
printk(“SEC MINOR value____ %d\n”,minorno);
printk(“SEC MAJOR value____ %d\n”,majorno);
scull_init();
printk(KERN_ALERT “Ho gaya Ji”);
return 0;
}
//=====================================Exit===================================================
static void end(void)
{
printk(KERN_ALERT “bye\n”);
unregister_chrdev_region(dev, nod);
printk(“Unregistration done\n”);
}

module_init(start);
module_exit(end);
@
1.7
log
@Completed Writing in diffrent Quantums as well as in diffrent Scullqset
@
text
@d47 8
a54 9
int noq=0;int nobw=0; //nobw =no. of bytes wrriten
int ret=0;
int lv,ls; //noq is no. of quantums required.
int nobpq=0;
int nobns=0; //no. if byte in next scull
int nobtw=size;
int nobsw=0;int sno=1; //sno is scull no

int nosqs=0; // no. of scull
d88 1
a88 1
if(nobtw>64)
d90 3
a92 5
nobns=nobtw-64; //no. of byte to write in next shell
nobtw=64;
// printk(“\n no. of byte to write in first shell : %d”,nobtw);
// printk(“\n no. of byte to write in first shell : %d”,nobtw);
ls=noq-quantum_size;
d96 1
a96 1
// ret=copy_from_user(scullqset->data[lv],ubuff+nobsw,nobpq); //for writing everytjing in a single quantum
d99 2
a100 1
{ nobpq=quantum_size;
d107 2
a108 2
printk(“\nScull is %d quantum no is %d and data : %s”,sno,lv,(char *)lscullqset->data[lv]);
nobsw=nobsw+(nobpq-ret);
d111 3
a113 5

// printk(“\n no. of byte to write in next shell : %d”,nobns);
noq=ls;
nobtw=nobns;
nosqs–;
@
1.6
log
@maked Qset
@
text
@d47 9
a55 3
int noq=0;
// int ret; //noq is no. of quantums required.
int nosqs=0; // no. of scull
d77 47
a123 1
create_qset(lscullqset,nosqs); //passing whole Scullqset structure and no scull qset require
d125 1
a125 3
printk(“\nLO JI HOGAYA End %s”,__func__);
return 0;
}
d129 1
a129 1
struct ScullQset *lsculldev;
d132 2
d135 4
a138 1
if(flag==0)
d140 1
a140 1
lsculldev=start=(struct ScullQset *)kmalloc(sizeof(struct ScullQset *),GFP_KERNEL);//as in linklist we preserve first nodee and allocating memory to first node
d144 3
a146 3
{
lsculldev->next=(struct ScullQset *)kmalloc(sizeof(struct ScullQset *),GFP_KERNEL);
lsculldev=lsculldev->next;
d148 3
d159 1
a159 1
for(lv=0;lv<fnosqs;lv++)
d162 1
a162 1
fscullqset=fscullqset->next;
d168 29
d203 1
a203 10

 

 

 
printk(“\nEnd %s”,__func__);
@
1.5
log
@done with finding quantum size
\.
@
text
@d4 1
a4 1

d15 1
a15 1

d32 1
d42 1
a42 1

d46 3
a48 1
int noq=0; //noq is no. of quantums required.
d68 4
a71 3

lsculldev->scullqset=create_sqset(nosqs);

d73 1
a73 1
printk(“\nLO JI HOGAYA End %s”,__func__);
d76 19
d96 2
a97 1
struct ScullQset *create_sqset(int fnosqs) //we use pointer here becaz it returns a an address …pointer
d99 19
a117 2
struct ScullQset *lsculldev;
struct ScullQset *start;
a118 1
int flag=0;
a119 1
printk(“\n WE ARE In CREATE SQSET “);
a121 10
if(flag==0)
{
lsculldev=start=(struct ScullQset *)kmalloc(sizeof(struct ScullQset *),GFP_KERNEL);
return start;
}
else
{
lsculldev=(struct ScullQset *)kmalloc(sizeof(struct ScullQset *),GFP_KERNEL);
return lsculldev;
}
a123 1
}
a124 3
ssize_t scull_read(struct file *filep,char __user *ubuff,size_t size,loff_t *loff )
{
printk(“\n Starting %s “,__func__);
d129 1
d140 2
d173 1
a173 1

@
1.4
log
@in write define lsculldev
@
text
@d45 2
d53 17
a69 1
printk(“\nEnd %s”,__func__);
d72 24
@
1.3
log
@finally witth trim
@
text
@d44 8
a51 2
printk(“\nStarting %s”,__func__);
printk(“\nEnd %s”,__func__);
@
1.2
log
@./
/
@
text
@d7 3
a9 3
if(!fsculldev)
{
printk(KERN_ERR”Error:No Sculldev\n”);
d11 1
d18 10
a27 13
// struct Sculldev *lsculldev;

printk(“\nScull open function start\n”);
// container_of(inodep->i_cdev,struct Sculldev,c_dev); //mapping RAM memory on Device:Sculldev
// filep->private_data=lsculldev; //preserving it on file as lsculldev is lost
// if((filep->f_flags && O_ACCMODE)==O_WRONLY)
// {
// printk(KERN_ALERT “FILE OPEN “);
// scull_trim(lsculldev);
// }

 

d45 1
d52 1
d68 5
a72 6
printk(KERN_ALERT “WELCOME TO DEVICE DRIVER\n”);

if(!majorno)
{
ret = alloc_chrdev_region(&dev, minorno, nod, DRIVER_NAME);
if(ret==0)
d79 2
a80 9
// else if(majorno==0)
// {
// dev=MKDEV(majorno,minorno+1);
// ret=register_chrdev_region(dev,nod,DRIVER_NAME);
// }

printk(“Major number assigned is___ %d\n”,majorno);
printk(“Minor number assigned is___ %d\n”,minorno);

a82 1

d85 1
a85 1
sculldev->c_dev.owner=”Ashish”; //we goto c_dev owner through sculldec
a92 1

a94 17

//scull_open();

/* dev= MKDEV(majorno,i);
cdev_init(&sculldev->c_dev, &fop);
sculldev->c_dev.owner=”Ashish”; //we goto c_dev owner through sculldec
sculldev->c_dev.ops=&fop;
sculldev->c_dev.count=nod;
cdev_add(&sculldev->c_dev, dev, nod); //c_dev main dev ka add
majorno = MAJOR(sculldev->c_dev.dev);
minorno = MINOR(sculldev->c_dev.dev);

printk(” MAJOR value____ %d\n”,majorno);
printk(” MINOR value____ %d\n”,minorno);
*/
a97 1

a100 2

a101 1

@
1.1
log
@Initial revision
@
text
@d17 1
a17 1
struct Sculldev *lsculldev;
d20 7
a26 7
container_of(inodep->i_cdev,struct Sculldev,c_dev); //mapping RAM memory on Device:Sculldev
filep->private_data=lsculldev; //preserving it on file as lsculldev is lost
if((filep->f_flags && O_ACCMODE)==O_WRONLY)
{
printk(KERN_ALERT “FILE OPEN “);
scull_trim(lsculldev);
}
@

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>