I want to store a specific data for each node (/dev/foo[1-99]
) since I want to map each of these nodes to a specific HW.
I started with using file->private_data
but each call to open sets it to NULL
.
Is there something similar that can be persistent between open()
calls?
The answer to your question is "No" (sort of), but you have misunderstood the usage of file->private_data
. It is something you can set in your open
file operation handler so that your other file operation handler functions can use it later. Your open
file operation handler can use the device number in inode->i_rdev
to decide which of your hardware devices is being opened, and set file->private_data
to point to your private data structure for that device.
Note that inode->i_rdev
is a combination of "major" and "minor" device numbers. You can use either MAJOR(inode->i_rdev)
or imajor(inode)
to extract the major part, and either MINOR(inode->i_rdev)
or iminor(inode)
to extract the minor part. This major/minor split may or may not be useful to you, depending on how you registered the devices. In any case, your driver will have registered a range of device numbers, and the inode->i_rdev
value will be within that range.
The "(sort of)" in my first paragraph is because the range of inode->i_rdev
values that your driver sees is persistent until it unregisters them (and then it will no longer see them!).