Tuesday, September 30, 2008

Char Drivers: Preliminaries

This is what I could gather from Linux Device Drivers Version 3. I have consulted a few other books but this one is lightyear ahead of others and should be the one point reference.

Important Data structures :-
1. file structure :
[*] Defined by linux/fs.h
[*] represents an open file [ every open file in linux has an associated struct file in kernel space]
[*] created by kernel on "open" and is passed to any driver method/operation that operated on the file. When all the instances of the file are closed, the kernel releases the data structure.

This is what the structure looks like

struct file
{
struct list_head f_list;
struct dentry *f_dentry;
struct file_operations *f_op;
atomic_t f_count;
unsigned int f_flags;
mode_t f_mode;
loff_t f_pos;
unsigned long f_reada, f_ramax, f_raend, f_ralen, f_rawin;
struct fown_struct f_owner;
unsigned int f_uid, f_gid;
int f_error;
unsigned long f_version ;
void *private_data;
};

Of these f_op is the most important data structure and our next target.

2. File Operations
[*] Collection of function pointers which point to a function in the driver. These functions implement a specific operation e.g read , write , open , close etc. [ If you dont know what function pointers are or how to use them, you have no business reading any further.]
[*] For unsupported operations NULL should be used.
Interesting point
suppose we have
struct file *filp;
then filp ->f_op is not saved by the kernel. This means that the file operations associated by a file can be changed.These new methods will be effective after the return to the caller.
Example:
struct file_operations nemesis_fops = {
.owner = THIS_MODULE,
.llseek = nemesis_llseek,
.read = nemesis_read,
.write = nemesis_write,
.ioctl = nemesis_ioctl,
.open = nemesis_open,
.release = nemesis_release,
};

3. Inode structure
[*] It is used by the kernel to represent files.
[*] file structure only represents an open file. Therefore for a single file , there can be numerous file structure representing miltiple open descriptors but they all point to a single inode structure

No comments: