Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux device driver: is the code shared between multiple devices using the same driver?

let's imagine we have some simple linux device driver, with:

static const struct of_device_id driver_match[] = {
     { .compatible = "vendor,device1" },
     { .compatible = "vendor,device2" },
     {},
};

so that device1 and device2 will bind to the same driver. We also have the following function inside the driver:

static int some_function(int never_mind)
{
    static int count = 0;
    // print "count" to the logs
    ....
}

which is invoked couple of times during probe() function of the device. Let's assume that devices are initialized "in the same time". My question is how it would work? Will device1 and device2 use the same some_function object and the same count variable or will they get completely different instances?

Is there some good source/book available about how actually the linux driver/module is executed? I could only find many tutorials about "How to write drivers".

And I apologize that I have to ask this question instead of preparing basic driver and check this myself, but I'm waiting for my first linux-capable SoC and could not resist getting this knowledge faster :(

like image 711
kashpersky Avatar asked Aug 23 '26 21:08

kashpersky


1 Answers

Will device1 and device2 use the same some_function object and the same count variable or will they get completely different instances?

Yes, they will use the same function and data. The Linux kernel is a monolithic kernel where all symbols (drivers and core functionality) are loaded into memory in which every other part of the kernel has access. Init/probe can happen on different cores, so you'll need to properly protect or atomically access any shared data. In your example, a simple atomic increment/decrement would do the trick.

like image 73
Larry Schiefer Avatar answered Aug 26 '26 10:08

Larry Schiefer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!