Thread: Using file_operations struct for device driver

  1. #1
    Registered User
    Join Date
    Feb 2009

    Using file_operations struct for device driver


    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:

    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:

    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    It might be, if you told us which OS / Compiler / DDK / driver / any other useful implementation specific info you might have.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    I looks like a Linux device driver structure. And yes, if you know the original function, then you should be able to call it.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM