我如何学习为 Linux 编写字符设备驱动程序?
Linux下如何编写字符设备驱动?
How to write char device drivers in Linux?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Linux下如何编写字符设备驱动?
How to write char device drivers in Linux?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
一个很好的例子是 Linux“softdog”,或者软件看门狗定时器。加载后,它将监视特殊设备的写入情况,并根据这些写入的频率采取行动。
它还向您展示了如何实现基本的 ioctl 接口,这非常有用。
要查看的文件是 drivers/watchdog/softdog.c
如果您通过示例学习,那么这是一个非常好的开始。其他人建议的基本字符设备(空、随机等)也很好,但没有充分演示您需要如何实现 ioctl() 接口。
附带说明一下,我相信驱动程序是由 Alan Cox 编写的。如果您要从示例中学习,那么研究顶级维护人员的工作绝不是一个坏主意。您可以非常确定该驱动程序也说明了遵守正确的 Linux 标准。
就驱动程序而言(在 Linux 中),字符驱动程序是最容易编写的,也是最有价值的,因为您可以看到代码运行得非常快。祝你好运,黑客快乐。
A very good example is the Linux "softdog", or software watchdog timer. When loaded, it will watch a special device for writes and take action depending on the frequency of those writes.
It also shows you how to implement a rudamentary ioctl interface, which is very useful.
The file to look at is drivers/watchdog/softdog.c
If you learn by example, that is a very good one to start with. The basic character devices (null, random, etc) as others suggest are also good, but do not adequately demonstrate how you need to implement an ioctl() interface.
A side note, I believe the driver was written by Alan Cox. If your going to learn from example, its never a bad idea to study the work of a top level maintainer. You can be pretty sure that the driver also illustrates adhering to proper Linux standards.
As far as drivers go (in Linux), character drivers are the easiest to write and also the most rewarding, as you can see your code working very quickly. Good luck and happy hacking.
阅读这本书:O'Reilly 出版的Linux 设备驱动程序。
对我帮助很大。
Read this book: Linux Device Drivers published by O'Reilly.
Helped me a lot.
到目前为止,我最喜欢的学习内核工作原理的书(我已经阅读了其中的大部分)是:
Linux内核开发(第2版)
这本书相当短,先读它,然后再看O'Reilly关于驱动程序的书。
My favorite book for learning how the kernel works, BY FAR (and I've read most of them) is:
Linux Kernel Development (2nd Edition)
This book is fairly short, read it first, then read the O'Reilly book on drivers.
阅读 Linux 设备驱动程序第三版。好的事情是开始编码。我只是粘贴一个简单的字符驱动程序,以便您可以开始编码。
这是 make 文件。
加载脚本。确保主编号为 251,否则进行相应更改。
卸载脚本,
也是一个测试设备运行情况的程序
Read linux device driver 3rd edition. And the good thing is start coding. I am just pasting a simple char driver so that you can start coding.
and this is the make file.
load script. make sure the major number is 251 or else change it accordingly.
unload script,
also a c program to test the operation of your device
看看标准内核中一些非常简单的标准 - “null”、“zero”、“mem”、“random”等。他们展示了简单的实现。
显然,如果您正在驱动真正的硬件,那就更复杂了 - 您需要了解如何与硬件以及设备的子系统 API(PCI、USB 等)进行交互。您可能还需要了解如何使用中断、内核定时器等。
Have a look at some of the really simple standard ones - "null", "zero", "mem", "random", etc, in the standard kernel. They show the simple implementation.
Obviously if you're driving real hardware it's more complicated- you need to understand how to interface with the hardware as well as the subsystem APIs (PCI, USB etc) for your device. You might need to understand how to use interrupts, kernel timers etc as well.
只需从这里检查字符驱动程序框架http: //www.linuxforu.com/2011/02/linux-character-drivers/....继续阅读这里的所有主题,彻底理解。(这有点像教程 - 所以按照所说的进行操作)。
了解“copy_to_user”和“copy_from_user”函数如何工作,您可以在驱动程序函数回调的读/写部分中使用它们。
完成此操作后,开始阅读基本的“tty”驱动程序。
首先重点关注驱动程序注册架构,这意味着:-
完成上述操作后,看看您想要驱动程序实现什么功能(策略),然后想办法实现该策略(称为机制)——策略和机制允许您区分驱动程序的各个方面。
你会成功的。
在编写机制时,永远不要忘记它为用户空间中的应用程序提供了什么。
Just check the character driver skeleton from here http://www.linuxforu.com/2011/02/linux-character-drivers/....Go ahead and read all the topics here, understand thoroughly.(this is kinda a tutorial-so play along as said).
See how "copy_to_user" and "copy_from_user" functions work, which you can use in read/write part of the driver function callbacks.
Once you are done with this, start reading a basic "tty" driver.
Focus, more on the driver registration architecture first, which means:-
Once you are done with the above, see what functionality you want with the driver(policy), then think of way to implement that policy(called mechanism)- the policy and mechanism allows you to distinguish between various aspects of the driver.
and you will be through.
When writing mechanism, never forget what it offers to the applications in user space.