A single call back for many files

When a large number of almost identical files is used, it's quite inconvenient to use a separate call back function for each file. A better approach is to have a single call back function that distinguishes between the files by using the data field in struct proc_dir_entry. First of all, the data field has to be initialised:

struct proc_dir_entry* entry;
struct my_file_data *file_data;

file_data = kmalloc(sizeof(struct my_file_data), GFP_KERNEL);
entry->data = file_data;
      

The data field is a void *, so it can be initialised with anything.

Now that the data field is set, the read_proc and write_proc can use it to distinguish between files because they get it passed into their data parameter:

int foo_read_func(char *page, char **start, off_t off,
                  int count, int *eof, void *data)
{
        int len;

        if(data == file_data) {
                /* special case for this file */
        } else {
                /* normal processing */
        }

        return len;
}
      

Be sure to free the data data field when removing the procfs entry.