simple string operations driver.
-------------------------------------


This program is intended to do get a string from user ( max of 30 characters) and write into the device. And read back the  string from the device. The driver code internally uses memory to store the string when write operation is called on the  device and give the string when read is performed.


Steps:
-------

- A module is made to register with kernel for the file /dev/myown withe major number 254.

- Then open/write function were implemeneted for file_operations structure.

- A character device files was created with the major number 254

  mknod /dev/myown c 254 0

- 30 bytes of memory was alloted to a char pointer in iniatialise module and all 30 bytes were set to value 0.

- In write function copy_from_user it used to store the string from user space to kernel space variable which was alloted 30  bytes.

- In read function copy_to_user function is used to copy the string from kernel memory to user space variable passed to the  read function call ad return the number of bytes read and move the offset value telling the position of the file pointer.

- A application programs was written to open the file /dev/mywon and write a string got from user into the device file.

- Two ways to test the program.
- A application program was written to open the file and read 10 characters from the file and put in a buffer and  print it.
- cat /dev/myown   --> Will print the whole message entered by the user.



Observation:
----------------

- When i initally started this driver code, in read function implementation i returned always 2 to tell that whoever reads my  device file i return 2 bytes. Which made to run the infinetely when i did cat /de/myown. it was displaying infinetly 2  characters on screen. Becoz cat issues read call on the file until it returns zero, so my driver code always returned 2 so it  ran continuously.[ plz check my posts link in my home page.]

- Then the read function is modified in such a way to return the number of characters read and updated the file offset  pointer to the postion until the postion in the file till where we read characters. Then cat on that file wroked fine.

Driver code:
----------------

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <asm/uaccess.h>

static int sample_open(struct inode *inode, struct file* filp);
static int sample_write(struct file* filep, char* data,int len,loff_t *off);
static int sample_read(struct file* filep, char* data,int len,loff_t *off);

struct file_operations rwstruct={
open:sample_open,
write:sample_write,
read:sample_read,
owner:THIS_MODULE
};

static int major,msglen=0;
char *memory;

int init_module(void)
{
        printk(KERN_EMERG "Inside init module \n");
        major = register_chrdev(254,"myown",&rwstruct);
        printk(KERN_EMERG "Major number is %d\n",major);
        memory = kmalloc(30,GFP_KERNEL);
        memset(memory,0,30);
        return 0;
}

void cleanup_module(void)
{
        printk(KERN_EMERG "Inside cleanup\n");
        unregister_chrdev(254,"myown");
        kfree(memory);
}

static int sample_open(struct inode *inode,struct file* filep)
{
        printk(KERN_EMERG "Inside sample_open \n");
        int type = MINOR(inode->i_rdev);
        printk(KERN_EMERG "Minor number is %d\n",type);
        return 0;
}

static int sample_read(struct file* filep, char* data,int len, loff_t *off)
{
        printk(KERN_EMERG "Inside sample_read length is %d:\n",len);

        if(*off >= msglen)      /* Intially Off will be set to 0 begining of file */
        return 0;
        if(*off+len >= msglen)
        len = msglen - *off;

        copy_to_user(data,memory+*off,len);   /* copy to user space memory */

        *off += len;
        return len;
}

static int sample_write(struct file* filep, char *data,int len,loff_t *off)
{
        printk(KERN_EMERG "Inside sample_write len is %d :string %c",len,*memory);
        copy_from_user(memory,data,len);
msglen = len;
        return 0;
}

Application code:
-----------------------

#include<stdio.h>
#include<unistd.h>

int main()
{

int fd;
char a[30];
fd = open("/dev/myown",O_WRONLY);

printf("fd value is %d\n",fd);
printf("Enter the string :");
gets(a);
printf("String lenght is %d \n",strlen(a));
write(fd,a,strlen(a));

close(fd);
}
Hosted by www.Geocities.ws

1