在Linux设备驱动程序的静态功能 [英] Static functions in Linux device driver

查看:157
本文介绍了在Linux设备驱动程序的静态功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在大多数设备驱动每一个功能是静态的?由于静态函数不是文件范围之外可见。那么,如何做这些驱动器的功能得到用户空间应用程序叫什么名字?

Why is it that every function in most device drivers are static? As static functions are not visible outside of the file scope. Then, how do these driver function get called by user space applications?

推荐答案

记住比用C一切都是地址。这意味着你可以调用一个函数,如果你有地址。内核有一个名为 EXPORT_SYMBOL 一个宏,做到了这一点。它导出,这样驱动程序功能,而无需将页眉的声明,因为这些功能有时不知道在编译时调用的函数的地址。在这种情况下,静态预选赛刚刚作出保证,他们只是通过这种方法叫,而不是从其他文件可能包括司机code(在某些情况下,这不是一个好主意,包括司机code头和直接调用它们)。

Remember than in C everything is addresses. That means you can call a function if you have the address. The kernel has a macro named EXPORT_SYMBOL that does just that. It exports the address of a function so that driver functions can be called without having to place header declarations since those functions are sometimes not know at compile time. In cases like this the static qualifier is just made to ensure that they are only called through this method and not from other files that may include that driver code (in some cases it's not a good idea to include driver code headers and call them directly).

编辑:既然有人指出,我没有覆盖用户空间

Since it was pointed out that I did not cover userspace.

驱动函数通常不通过用户空间直接调用(除执行x86的指令SYSCALL其中做一些小动作,有时保存上下文切换)。因此,这里的static关键字没什么区别。它不仅使在内核空间的差别。正如@Cong王指出,功能通常被放置到函数指针的结构,使得它们可以通过简单地具有结构指向该结构(如file_ops,调度,文件系统,网络code等被称为.. )。

Driver functions are usually not called through userspace directly (except for x86 implementation of SYSCALL instruction which does some little tricks to save the context switch sometimes). So the static keyword here makes no difference. It only makes a difference in kernel space. As pointed out by @Cong Wang, functions are usually place into a structure of function pointers so that they may be called by simply having structures point to this structure (such as file_ops, schedulers, filesystems, network code, etc...).

这篇关于在Linux设备驱动程序的静态功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆