Quantcast

Maximum PC

It is currently Sun Nov 23, 2014 11:56 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: linux device driver help
PostPosted: Sat Nov 20, 2004 7:36 pm 
Team Member Top 50
Team Member Top 50
User avatar

Joined: Fri Sep 03, 2004 12:40 pm
Posts: 315
Location: NJ
hello everyone. i have to create a device driver in linux (slackware) that functions as a first-in first-out device. the device has to function as a named pipe and allow two or more processes to communicate. to test the fifo i am supposed to run the following commands

$ echo "test > /dev/fifo
$ cat /dev/fifo
test
$ cat /dev/fifo
(no output, since the buffer is empty)

i've written the following code, but i have no idea how to write the function for read and write for the device. any help would be greatly appreciated.

Code:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>

/* device name */

#define DEV_NAME "CSC330DRVR"

int fifo_open(struct inode *inode, struct file *filp);
int fifo_release(struct inode *inode, struct file *filp);
ssize_t fifo_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
ssize_t fifo_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos);

int major = 0;

struct file_operations fops = {
   .read = fifo_read,
   .write = fifo_write,
   .open = fifo_open,
   .release = fifo_release
};

int init_module(void)
{
   major = register_chrdev(0, DEV_NAME, &fops);
   printk("registered device with major number %i\n", major);
   return 0;
}

void cleanup_module(void)
{
   unregister_chrdev(major, DEV_NAME);
}

int fifo_open(struct inode *inode, struct file *filp)
{
   MOD_INC_USE_COUNT;

   return 0;
}

int fifo_release(struct inode *inode, struct file *filp)
{
   MOD_DEC_USE_COUNT;

   return 0;
}

ssize_t fifo_read(struct file *filp, char *buff, size_t count, loff_t *f_pos)
{
        ???
}

ssize_t fifo_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
        ???
}


Top
  Profile  
 
 Post subject:
PostPosted: Mon Nov 22, 2004 5:11 pm 
Team Member Top 50
Team Member Top 50
User avatar

Joined: Fri Sep 03, 2004 12:40 pm
Posts: 315
Location: NJ
well no body got back to me on this one, but I ended up figuring it out. I figured i'd post the final code so that people could see it if they were interested.

Code:
#include <linux/modversions.h>
#include <linux/kernel.h>
#include <linux/config.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/wait.h>
#include <asm/uaccess.h>
#include <linux/string.h>
#include <asm/io.h>
#include <asm/msr.h>

#define DEV_NAME "myfifo"

int fifo_open(struct inode *inode, struct file *filp);
int fifo_release(struct inode *inode, struct file *filp);
ssize_t fifo_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
ssize_t fifo_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos);

int major = 0;            
static char devchar[255];   
static char *dev_Ptr;      

struct file_operations fops = {
   .read = fifo_read,
   .write = fifo_write,
   .open = fifo_open,
   .release = fifo_release
};

int init_module(void)
{
   major = register_chrdev(0, DEV_NAME, &fops);
   printk("registered device with major number %i\n", major);

   memset(devchar,0,255);   
               
   return 0;
}

void cleanup_module(void)
{
   unregister_chrdev(major, DEV_NAME);
}

int fifo_open(struct inode *inode, struct file *filp)
{
   dev_Ptr = devchar;   
   MOD_INC_USE_COUNT;   
   return 0;
}

int fifo_release(struct inode *inode, struct file *filp)
{
   MOD_DEC_USE_COUNT;   

   return 0;
}

ssize_t fifo_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
   int bytes_read = 0;         

   if(*dev_Ptr == 0)         
   {
      return 0;
   }

   while(count && *dev_Ptr)   
   {
      put_user(*(dev_Ptr++), buf++);
      count--;
      bytes_read++;
   }
   
   memset(devchar,0,255);      

   return bytes_read;         
}

ssize_t fifo_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
   int bytes_written = 0;      

   while((count || *dev_Ptr))   
   {
      get_user(*(dev_Ptr++), buf++);
      bytes_written++;
      count--;
   } //End while

   return bytes_written;      
}



Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.