Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded Linux: kernel drivers vs user space drivers?

Is there a best approach (or at least some advantages and disadvantages) when choosing between the use of kernel space or user space drivers in Linux?

For example, let's say I'm developing a board for sensing humidity built on the Sensirion SHT21 sensor. My application will read a sample from the sensor and then present it in JSON form for a web application to consume.

In order to "talk" with the SHT21 sensor I can either:

  • instantiate the I2C device with echo sht21 0x40 > /sys/class/i2c-adapter/i2c-0/new_device and access the humidity readings via hwmon, parse the output and then use it in my application
  • write my own software which performs the required (according to datasheet) write() and read() operations against /dev/i2c-0 and calculate the humidity myself, then use it in my application

The first approach makes use of the sht21 kernel driver, the latter works entirely in user space.

Which one should I go for? How should I choose?

like image 593
Paul Morneau Avatar asked Dec 04 '25 07:12

Paul Morneau


1 Answers

Of the top of my head:

Userland approach pros:

  • faster to develop / easier to debug
  • if buggy and crash, cannot crash your whole system

Userland approach cons:

  • "performances" - which I'll leave as a very vague concept here and today...

In the case of your application, putting that in perspective:

  • since we can safely bet that humidity does not change dramatically in short amount of time,
  • and/or your sensor have some non negligible hysteresis anyway (would it be only for mechanical reasons, ex a drop of water fall on it, it will not dissapear in a millisecond),
  • ...and you probably don't plan to send humidity measures every millisecond - do you?
  • ...and even if you did, most of the latency (as "vs performance") will be from that part that will make it a JSON, send it to the server (both being clearly jobs for userland), and - though that may be none of your business, this is still part of the use case - networking condition and processing time by the by the server,

...all in all, I would 200% go with the userland approach.

Kernel space may be technically more "fun" or "rewarding", but engineering put "pragmatic" before "fun". :-)

like image 189
jbm Avatar answered Dec 06 '25 23:12

jbm