简体   繁体   English

内核设备驱动程序或用户空间程序

[英]kernel device driver or userspace program

I'm currently using a SAMA5D31-EK board running Linux 3.10.0+ to control some hardware devices. 我目前正在使用运行Linux 3.10.0+的SAMA5D31-EK板来控制某些硬件设备。 I'm using GPIOs, I2C, PWM and UARTS available in that board. 我正在使用该板上的GPIO,I2C,PWM和UART。 Some devices are controlled with just a GPIO line while others need an UART a PWM and 3 GPIOs. 有些设备仅通过GPIO线控制,而其他设备则需要UART,PWM和3个GPIO。 So far I'm using an userspace program to control those hardware devices - basically a stepper motor, an ADC and a alphanumeric LCD display. 到目前为止,我正在使用用户空间程序来控制那些硬件设备-基本上是步进电机,ADC和字母数字LCD显示器。

What would be the advantges of developping a kernel device driver to control those devices? 开发内核设备驱动程序来控制那些设备的优势是什么? So far (using a userspace program) the only limitation I've found is speed: since I have to bit bang some GPIOs, the result is a bit slow. 到目前为止(使用用户空间程序),我发现的唯一限制是速度:由于我不得不对一些GPIO进行一些改动,因此结果有点慢。

I assume that you have the platform-specific drivers available for the I2C/GPIO/PWM/UART interfaces on your board(it should be part of BSP[Board-support-package] ). 我假设您的板上具有I2C / GPIO / PWM / UART接口可用的特定于平台的驱动程序(它应该是BSP [Board-support-package]的一部分)。

It is just that you don't want to use the Kernel device driver framework and want to do things from the user-space. 只是您不想使用内核设备驱动程序框架,而是想从用户空间执行操作。 I'd been in this situation hence I know, how tempting it could be,especially , if you are not well-versed in Kernel device drivers. 我曾经遇到过这种情况,因此我知道,如果您不熟悉内核设备驱动程序,那将是多么诱人。

a. 一种。 SPEED : You mentioned it. 速度 :您提到了。 But, you probably didn't grasp the reason completely. 但是,您可能没有完全理解原因。

Speed efficiency comes from avoiding the Context-switching between Kernel and User-space process. 速度效率来自避免内核和用户空间进程之间的上下文切换。 Here is an example: 这是一个例子:

/* A loop in kernel code which reads a register 100 time */
for (i = 0 ; i < 100 ; i++ )
{
  __kernel_read_reg(...);
}

/* A loop in User-space code which reads a register 100 time */
for ( i= 0 ; i < 100; i++)
{
   __user_read_reg(...);
}

Functionality wise both *_read_reg() is same. 在功能上,两个* _read_reg()相同。 Assuming that __user_read_reg() will go through a typical-system-call procedure,it has to do a Context-switch for every single __user_read_reg(...) which is too costly. 假设__user_read_reg()将通过典型的系统调用过程,则它必须为每个单独的__user_read_reg(...)进行上下文切换,这会导致成本过高。

You may argue, "We can mmap() the hardware registers and avoid system call for such operations". 您可能会争论:“我们可以mmap()硬件寄存器,并避免系统调用此类操作”。 Of course, you could do that, but the point I was making is: What is close to hardware (for example: a register read or write or handling an interrupt) should be done as fast as possible. 当然,您可以这样做,但是我要说的是:与硬件接近的事物(例如:寄存器的读取或写入或处理中断)应尽快完成。 Latencies involved in context-switching will impact the performance. 上下文切换所涉及的延迟将影响性能。

b. Existing/Tested/Well-built subsystems: 现有/经过测试/完善的子系统:

If you see an I2C subsystem in the Linux Kernel, it provides a well-tested, robust framework which could be easily-reused. 如果您在Linux内核中看到一个I2C子系统,它提供了一个经过充分测试的健壮框架,可以轻松地重用。 You don't have to write full I2C subsytem (handling all device types, speed, various configuration etc ) in the user-space. 您不必在用户空间中编写完整的I2C子系统(处理所有设备类型,速度,各种配置等)。 Re-using" what is already done could be one big advantage while going for kernel device drivers. 在使用内核设备驱动程序时,“重用”已完成的操作可能是一大优势。

c. C。 Move from Polling-based approach to Interrupt-based mechanism 从基于轮询的方法过渡到基于中断的机制

If you are not handling interrupts in Kernel driver,You must be using some sort of polling-mechanism in the user-space process. 如果不在内核驱动程序中处理中断,则必须在用户空间进程中使用某种轮询机制。 Depending on the system, it might not be very reliable way of handling the hardware-changes.Definitely not accurate/reliable for fast devices. 根据系统的不同,它可能不是处理硬件更改的非常可靠的方法。对于快速的设备来说绝对不是准确/可靠的。

Interrupt-based mechanism , in general, where you handle the critical changes as fast as possible( Hardware interrupt context) and move the non-critical work-load either to user-space or some other kernel mechanism is more reliable way of handling devices. 通常,基于中断的机制会以更快的速度处理关键更改(硬件中断上下文),并将非关键工作负载移至用户空间或其他内核机制,这是处理设备的更可靠方法。

Of-course, there could be several more arguments and counter-arguments besides above three. 当然,除了以上三个之外,可能还会有更多其他论点和反论点。

Another thread which might be of interest to you is here: Userspace vs kernel space driver 您可能感兴趣的另一个线程在这里: 用户空间与内核空间驱动程序

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

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