简体   繁体   English

嵌入式Linux:内核驱动程序与用户空间驱动程序

[英]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? 在Linux中使用内核空间或用户空间驱动程序时,是否有最佳方法(或至少是一些优点和缺点)?

For example, let's say I'm developing a board for sensing humidity built on the Sensirion SHT21 sensor. 例如,假设我正在开发一种用于感应Sensirion SHT21传感器上的湿度的板。 My application will read a sample from the sensor and then present it in JSON form for a web application to consume. 我的应用程序将从传感器中读取一个样本,然后以JSON形式呈现它以供Web应用程序使用。

In order to "talk" with the SHT21 sensor I can either: 为了与SHT21传感器“对话”,我可以:

  • 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 使用echo sht21 0x40 > /sys/class/i2c-adapter/i2c-0/new_device实例化I2C设备并通过hwmon访问湿度读数,解析输出然后在我的应用程序中使用它
  • 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 编写我自己的软件,对/dev/i2c-0执行所需的(根据数据表) write()read()操作并自己计算湿度,然后在我的应用程序中使用它

The first approach makes use of the sht21 kernel driver, the latter works entirely in user space. 第一种方法使用sht21内核驱动程序,后者完全在用户空间中工作。

Which one should I go for? 我应该去哪一个? How should I choose? 我该如何选择?

Of the top of my head: 在我的头顶:

Userland approach pros: Userland方法专业人士:

  • faster to develop / easier to debug 更快地开发/更容易调试
  • if buggy and crash, cannot crash your whole system 如果越野车和车祸,不能崩溃你的整个系统

Userland approach cons: Userland方法缺点:

  • "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, ...即使你这样做,大多数延迟(作为“vs performance”)将来自将使其成为JSON的那部分,将其发送到服务器(两者都显然是userland的工作),并且 - 尽管如此可能不属于您的业务,这仍然是用例的一部分 - 服务器的网络状况和处理时间,

...all in all, I would 200% go with the userland approach. ......总而言之,我会200%采用用户态方法。

Kernel space may be technically more "fun" or "rewarding", but engineering put "pragmatic" before "fun". 内核空间在技术上可能更“有趣”或“有意义”,但工程学在“有趣”之前将“务实”放在一边。 :-) :-)

In both cases your driving what happens from user space, option 2 implies writing a driver which I would avoid, if it already exists your good. 在这两种情况下,你驾驶从用户空间发生的事情,选项2意味着写一个我会避免的驱动程序,如果它已经存在你的好处。

If there's a driver that parses and makes sense of some the data that's being used in production use it if it's easier to use or has a large user base. 如果有一个驱动程序解析并理解某些正在生产中使用的数据,那么如果它更容易使用或拥有大量用户群,则使用它。 If it's trivial to parse the raw data and you're truly squeezed for space (I doubt this because you running a kernel) then write your own parser etc. 如果解析原始数据是微不足道的并且你真的被挤压空间(我怀疑这是因为你运行了一个内核)然后编写你自己的解析器等。

To be short what's the least work ie the least complex, do that. 简而言之,即做最少的工作,即最不复杂的工作,做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM