c++ Linux下的设备驱动开发
我想获得有关使用 c++ for Linux box 编写图形设备驱动程序和音频设备驱动程序的更多详细信息。 我是开发设备驱动程序的新手,请向我提供相同的开发/文档详细信息。
谢谢
-普拉文
I wanted to get more details for writing Graphics device drivers and audio device drivers using c++ for Linux box.
I am newbie at developing device drivers , Please provide me development/documentation details for the same.
Thanks
-Pravin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
迟到了这一页,这个问题本身已经由 Chris Stratton 回答了,但重要的是要纠正 Chris Becke 在这里提出的一些对不熟悉 C++ 的人来说常见的误解:
这是为了解决此处发布的误解。不过要补充的是:
1)新手 C++ 程序员可能会胡说八道,但是新手 C 程序员试图自己实现多态性和继承,就像在内核中一次又一次地调用它一样,而不是这样调用它,会做很多效率低下且不可调试的事情废话。
2)也就是说,在基础C++中唯一可以创建的是虚拟指针(如果您需要它并指定“虚拟”),然后C程序员通常也只是创建这样一个指针,自己操作它,添加查找表并获得很多由于它,更难的错误会出现。与 C++ 中一样,如果您不提及“虚拟”,那么您甚至不会获得此指针。继承和封装当然是完全没有开销的。
3) 如果您不明确请求特殊功能,C++ 会创建与 C 相同数量的 asm 和内存,但在一种常见情况下 C++ 更高效 - 在传递函数指针时。如果您使用 C++ 的仿函数,您可以内联指向的函数。这在嵌入式应用程序中非常有用。
4) 如果嵌入式 RT 使用 C++,为什么 linux 不使用?只是因为神话,所以请仔细阅读此消息,并参考 scott meyers 或更好的是 asm 本身。我在 RT 工作了 20 年,当我 14 年前转行时,我对 C++ 也有同样的不信任,但事实并没有证实任何这种不信任。
TL;DR - 用 C++ 编写高效且通常情况下更高效的代码非常容易,关于这个主题的研究、许多行业经验和书籍比比皆是。
Coming to this page late, the question itself has been answered by Chris Stratton, but it's important to correct a couple of things Chris Becke put here that are common misconceptions with people that are not familiar with C++:
This is to address the misconceptions posted here. To add more however:
1) A novice C++ programmer may do nonsense, but a novice C programmer trying to implement by himself polymorphism and inheritance as done time and time again in the kernel just without calling it as such, will do lots more inefficient undebuggable nonsense.
2) Saying that, the only thing that may be created in base C++ is a virtual pointer IF YOU NEED IT and specify "virtual", and then also C programmers usually just create such a pointer manipulate it by themselves add lookup tables and get much harder bugs down the line due to it. As always in C++, if you don't mention "virtual" then you don't even get this pointer. Inheritance and encapsulation are of course completely free of overhead.
3) C++ creates the same amount of asm and memory as C if you don't EXPLICITLY request special features, but there is a common case when C++ is more efficient - when passing function pointers. If you use C++'s functors you can inline the pointed function. This is EXTREMELY useful in embedded apps.
4) If embedded RT uses C++ why linux doesn't? Just because of myths, so please do read this message carefully, and refer to scott meyers or better yet the asm itself. I am 20 years in RT and had the same disbelief in C++ when I switch 14 years ago, but the facts do not confirm any such distrust.
TL;DR - it's very easy to write as efficient and in a common case more efficient code in C++, studies, much industry experience and books are abound on this subject.
Linux 内核设备驱动程序是用 C 而不是 C++ 编写的。
大多数设备驱动程序都是通过特殊的设备文件(/dev/yourdevice0)访问的,可以在该文件上执行控制以及读写操作。
用户模式客户端程序和用户模式驱动程序打开设备文件并将其用作与内核模式驱动程序对话的途径。这些用户模式驱动程序可以用 C++ 或任何其他语言编写。
一般来说,最好的入门方法是拥有一个需要驱动程序的设备,并了解编写它所需的内容。通常,最好的方法是为相关设备或具有类似接口范例的设备找到现有驱动程序,然后首先对其进行修改,直到它适用于您的新设备或同样适用。
Linux kernel device drivers are written in C rather than C++.
Most device drivers are accessed via a special device file (/dev/yourdevice0) on which control as well as read and write operations can be performed.
User mode client programs and user mode drivers open the device file and use it as a pathway to talk to the kernel mode driver. These user mode drivers could conceivably be written in C++ or any other language.
Generally the best way to get started is to have a device which needs a driver, and learn what you need to in order to write it. And often the best way to do that is to find an existing driver for either a related device, or one with similar interface paradigms, and start by modifying that until it works for your new device instead or as well.
由于内核中没有 C++ 运行时,您很快就会遇到问题。我想您可以创建一个在内核中运行的 C++ 运行时,但这需要一些相当好的技能。比用 C 编写驱动程序的技能要高得多。
而且,你会立即被 Linux 内核开发人员打倒。我的意思是真的放下。他们会狠狠地攻击你,让你永远无法从中恢复过来。你很可能会说“去死吧 Linux 和他们的精英混蛋”。
我不想听起来消极,但与你从其他人那里听到的声音相比,我的声音温和且合适。
As there is no C++ runtime in the kernel, you will run into problems quickly. I suppose you could make a C++ runtime to run inside the kernel, but it would require some pretty good skills. Much greater skills than writing the driver in C.
Also, you would be put down instantly by Linux kernel developers. I mean REALLY put down. They'd flame you so bad, you'd never recover from it. Chances are that you would say "Screw Linux and their elitist bastards".
I don't want to sound negative, but I'm a mild and suitable voice in comparison to what you'd hear from others.
Linux 驱动程序是用 C 语言开发的。如果您想了解有关 Linux 驱动程序开发的更多信息,您应该阅读这本免费电子书:http ://lwn.net/Kernel/LDD3/
还提供所有 pdf 章节的 tarball:http://lwn.net/images /pdf/LDD3/ldd3_pdf.tar.bz2
Linux drivers are developed in C. If you want to learn more about Linux drivers development, you should read this free eBook: http://lwn.net/Kernel/LDD3/
A tarball of all pdf chapters is also available: http://lwn.net/images/pdf/LDD3/ldd3_pdf.tar.bz2
用于编写(内核模式)设备驱动程序的语言是 C,而不是 C++,最终原因很简单:C++ 是一种不适合用来编写驱动程序软件的语言。这样做的副作用是,内核模式下没有可用的 C++ 运行时。
至于为什么 C++ 不合适: 至少有两个原因:
我认为还有一些其他方面我忘记了,但是,ideomatic c++ 违反了对驱动程序的许多限制。这就是为什么 C 是首选的原因。
C, not C++ is the language for writing (kernel mode) device drivers, and the reason ultimately is simple: C++ is an inappropriate language to use to write driver software. As a side effect of this, there is no c++ runtime available in kernel mode.
As to why c++ is inappropriate: There are at least two reasons:
I think there are some other aspects I am forgetting, but, ideomatic c++ violates a number of constraints placed on drivers. Which is why C is preferred.
这是一篇旧帖子,但我决定写一个答案,因为没有答案解释如何做到这一点而不适得其反。
简短回答:
答案是“是的,你可以”......付出巨大的努力。让我们忽略 Linus Trovald 关于 C++ 的意见
长答案:
我的建议是写它当你真正需要它时,使用 C++ 。
在内核中运行C++的方式存在很多陷阱。
我最近研究了这个主题,这是我的总结:
没有
new
和delete
的实现这是最容易克服的事情。
内核上的堆栈大小小于 8 kb(32 位)和 16kb(64 位)。
哎呀...不让堆栈溢出将是一个挑战。
不允许以下内容
这是让 C++ 读取内核头的 PIA。如果你喜欢挑战,请至少阅读Linux 内核中的 C++ 在你走之前。不要忘记在将旧代码升级到新内核时每次都必须执行此操作。
如果您想了解更深入的知识,请查看以下文章。
Linux 内核中的 C++ (2016)< /p>
< a href="https://pograph.wordpress.com/2009/04/05/porting-cpp-code-to-linux-kernel/" rel="nofollow noreferrer">将 C++ 代码移植到 Linux 内核 (2009)
OSDev.org 中的 C++ 文章
此外 korisk 在 github 用于准系统内核模块。
结论
再次,我真诚地认为,在开始之前评估在内核模块中运行 C++ 的工作量。
第三次,最好在开始之前评估一下在内核模块中运行 C++ 的工作量!
This is an old post, but I decide to write an answer since there is no answer explaining about how to do it without getting backfired.
Short Answer:
The answer is "yes, you can" … with tremendous effort. Let's just ignore Linus Trovald's opinion about C++
Long Answer:
My suggestion is writing it in C++ when you REALLY need it.
There are many pitfalls in the way of running C++ in kernel.
I recently study this subject and here’s my summarize:
There is no implementation for
new
anddelete
It is the easiest thing to overcome.
The stack size on kernel is less than 8 kb (for 32bit) and 16kb (for 64bit).
Opps...not getting stack overrun would be challenging.
Following are not allowed
It’s PIA to let C++ read a kernel header. If you like challenges, please at least read C++ in the Linux kernel before you go. Don't forget it has to be done every time while upgrading old code to a newer kernel.
If you’d like to know more in-depth knowledge, check out following articles.
C++ in the Linux kernel (2016)
Porting C++ code to Linux kernel (2009)
C++ article in OSDev.org
Also korisk has a demo repo in github for a barebone kernel module.
Conclusion
Again, my sincere opinion, assess the effort for running C++ in kernel module before you go.
Third time, it's better to assess the effort for running C++ in kernel module before you go!
不幸的是,当前的 Linux 头文件无法在 C++ 中编译,因为它们使用 new 作为变量名,声明 false、true 并具有其他一些内容问题。
您可以在 Linux 模块中使用 C++,但如果不包含 Linux 头文件,它就毫无用处。所以你不能远离最简单的 hello-world 模块。
Unfortunately, the current Linux header files are not compilable in C++ as they use
new
as a variable name, declarefalse
,true
and have some other issues.You can use C++ in a Linux module but it's useless without including Linux headers. So you can't go far away from the simplest hello-world module.