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 youropen
file operation handler so that your other file operation handler functions can use it later. Youropen
file operation handler can use the device number ininode->i_rdev
to decide which of your hardware devices is being opened, and setfile->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 eitherMAJOR(inode->i_rdev)
orimajor(inode)
to extract the major part, and eitherMINOR(inode->i_rdev)
oriminor(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 theinode->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!).