Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I'm currently trying to write a somewhat modified device driver and what I need to do is instead of using the default read/write functions for the file_operations struct, I ...
  1. #1
    Just Joined!
    Join Date
    Feb 2009
    Posts
    2

    File Operations in C Help (for Device Driver)

    Hi,

    I'm currently trying to write a somewhat modified device driver and what I need to do is instead of using the default read/write functions for the file_operations struct, I need to use my own read and write.

    I was able to successfully point the file operations structure to my own read and write functions using the syntax:

    Code:
    static struct file_operations my_fops = {
    .read = my_read,
    .write = my_write
    };
    These register fine and my functions get called. But there's one main problem, I really don't want to rewrite a whole new read/write function since all I need to do is add one additional section of code to the function at the top to simply modify the buffer, so I don't need to redo the whole read/write.

    Is there a way for me to, after entering my functions for read/write (my_read, my_write) and modifying the buffer, then call the default read/write file_operations functions to do the read/write for me simply with the modified buffer I made?

    Since the file_operations struct automatically points to its default read and write functions I've been trying to find out what those functions were or where they were located so I could just call them myself but I'm having no luck. And once I make the file_operations struct point to my own read and write I have no idea how to find the read/write functions that were going to be used before I changed them.

    Ideally, I'd like the code to look something like this:

    Code:
    static ssize_t my_read(struct file *filp, char *buffer, size_t count, loff_t *offset)
    {
         Modify buffer (change it's information/characters)
    
         return default_read(filp, buffer, count, offset);
    }
    This seems like it is possible to me but I just can't find the default read and write functions to call them, does anyone know where they are or how to use them?

    Thank you for any help

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    If your overwriting the original values...just copy them somewhere before you over write them and then call then before you exit ...the variable for the read should be defined something like

    typedef ssize_t (*origread)(struct file*, char*,size_t,loff_t);

    origread orig_read = the original value;


    Is this what your looking for?...Gerard4143

  3. #3
    Just Joined!
    Join Date
    Feb 2009
    Posts
    2
    Hmm, not really. See I don't think I'm "overwriting" the value per say. I think I'm simply assigning it to a different value than what it would be otherwise. Like the original declaration of the struct is simply:

    static struct file_operations my_fops;

    And it like automatically points to its default read/write functions. But when I say .read and .write, I'm making it point somewhere else. So it's not like I'm overwriting anything, but it's that it's originally pointing nowhere and instead I'm pointing it to my functions and because of that later on it doesn't get pointed to its default read/write functions.

    It's kind of confusing lol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...