IOCTL Linux设备驱动程序 [英] IOCTL Linux device driver
问题描述
谁能解释我,
- 什么是
IOCTL
? - 它是用来干什么的?
- 我如何使用它?
- 为什么我不能定义与
IOCTL
相同功能的新函数?
- What is
IOCTL
? - What is it used for?
- How can I use it?
- Why can't I define new function that does the same work as
IOCTL
?
推荐答案
一个ioctl
,表示输入-输出控制"是一种特定于设备的系统调用. Linux(300-400)中只有很少的系统调用,不足以表达设备可能具有的所有独特功能.因此,驱动程序可以定义一个ioctl,该ioctl允许用户空间应用程序向其发送订单.但是,ioctl并不是很灵活,并且会变得有些混乱(数十个魔术数字"都可以正常工作...或不起作用),并且在将缓冲区传递到内核时也可能是不安全的-错误的处理可能会破坏事情很容易.
An ioctl
, which means "input-output control" is a kind of device-specific system call. There are only a few system calls in Linux (300-400), which are not enough to express all the unique functions devices may have. So a driver can define an ioctl which allows a userspace application to send it orders. However, ioctls are not very flexible and tend to get a bit cluttered (dozens of "magic numbers" which just work... or not), and can also be insecure, as you pass a buffer into the kernel - bad handling can break things easily.
另一个选择是sysfs
界面,在该界面中您可以在/sys/
下设置文件并对其进行读/写操作,以获取驱动程序中的信息.如何进行设置的示例:
An alternative is the sysfs
interface, where you set up a file under /sys/
and read/write that to get information from and to the driver. An example of how to set this up:
static ssize_t mydrvr_version_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(buf, "%s\n", DRIVER_RELEASE);
}
static DEVICE_ATTR(version, S_IRUGO, mydrvr_version_show, NULL);
在驱动程序安装过程中:
And during driver setup:
device_create_file(dev, &dev_attr_version);
然后,您将在/sys/
中拥有一个用于设备的文件,例如,用于块驱动程序的/sys/block/myblk/version
.
You would then have a file for your device in /sys/
, for example, /sys/block/myblk/version
for a block driver.
另一种更常用的方法是netlink,它是一种IPC(进程间通信)方法,用于通过BSD套接字接口与驱动程序进行通信.例如,这由WiFi驱动程序使用.然后,您可以使用libnl
或libnl3
库从用户空间与其进行通信.
Another method for heavier use is netlink, which is an IPC (inter-process communication) method to talk to your driver over a BSD socket interface. This is used, for example, by the WiFi drivers. You then communicate with it from userspace using the libnl
or libnl3
libraries.
这篇关于IOCTL Linux设备驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!