Quick Reference

Quick Reference

This chapter introduced the following symbols and header files. The list of the fields in struct file_operations and struct file is not repeated here. 本章介绍了以下符号和头文件。 struct file_operations 和 struct file 中的字段列表在此不再赘述。

#include
dev_t

dev_t is the type used to represent device numbers within the kernel. dev_t 是用于在内核中表示设备号的类型。

int MAJOR(dev_t dev);
int MINOR(dev_t dev);

Macros that extract the major and minor numbers from a device number. 从设备编号中提取主要和次要编号的宏。

dev_t MKDEV(unsigned int major, unsigned int minor);

Macro that builds a dev_t data item from the major and minor numbers. 从主要和次要数字构建 dev_t 数据项的宏。

#include

The "filesystem" header is the header required for writing device drivers. Many important functions and data structures are declared in here. “文件系统”头文件是编写设备驱动程序所需的头文件。 许多重要的函数和数据结构都在这里声明。

int register_chrdev_region(dev_t first, unsigned int count, char *name)
int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name)
void unregister_chrdev_region(dev_t first, unsigned int count);

Functions that allow a driver to allocate and free ranges of device numbers. register_chrdev_region should be used when the desired major number is known in advance; for dynamic allocation, use alloc_chrdev_region instead. 允许驱动程序分配和释放设备号范围的函数。 register_chrdev_region 应在预先知道所需的主编号时使用; 对于动态分配,请改用 alloc_chrdev_region。

int register_chrdev(unsigned int major, const char *name, struct file_operations *fops);

The old (pre-2.6) char device registration routine. It is emulated in the 2.6 kernel but should not be used for new code. If the major number is not 0, it is used unchanged; otherwise a dynamic number is assigned for this device. 旧的(2.6 之前的)字符设备注册例程。 它在 2.6 内核中被模拟,但不应用于新代码。 如果主编号不为0,则原样使用; 否则将为此设备分配一个动态编号。

int unregister_chrdev(unsigned int major, const char *name);

Function that undoes a registration made with register_chrdev. Both major and the name string must contain the same values that were used to register the driver. 撤消使用 register_chrdev 进行的注册的函数。 主要和名称字符串都必须包含用于注册驱动程序的相同值。

struct file_operations;
struct file;
struct inode;

Three important data structures used by most device drivers. The file_operations structure holds a char driver's methods; struct file represents an open file, and struct inode represents a file on disk. 大多数设备驱动程序使用的三个重要数据结构。 file_operations 结构包含一个 char 驱动程序的方法; struct file 表示打开的文件,struct inode 表示磁盘上的文件。

#include
struct cdev *cdev_alloc(void);
void cdev_init(struct cdev *dev, struct file_operations *fops);
int cdev_add(struct cdev *dev, dev_t num, unsigned int count);
void cdev_del(struct cdev *dev);

Functions for the management of cdev structures, which represent char devices within the kernel. 用于管理 cdev 结构的函数,cdev表示内核中的字符设备。

#include
container_of(pointer, type, field);

A convenience macro that may be used to obtain a pointer to a structure from a pointer to some other structure contained within it. 一个便利宏,可用于从指向其中包含的某个其他结构的指针获取指向该结构的指针。

#include

This include file declares functions used by kernel code to move data to and from user space. 这个包含文件声明了内核代码用来将数据移入和移出用户空间的函数。

unsigned long copy_from_user (void *to, const void *from, unsigned long count);
unsigned long copy_to_user (void *to, const void *from, unsigned long count);

Copy data between user space and kernel space. 在用户空间和内核空间之间复制数据。


[1] Even better device information can usually be obtained from sysfs, generally mounted on /sys on 2.6-based systems. Getting scull to export information via sysfs is beyond the scope of this chapter, however; we'll return to this topic in Chapter 14. 更好的设备信息通常可以从 sysfs 获得,通常安装在基于 2.6 的系统上的 /sys 上。 但是,如何通过 sysfs 导出 scull 信息超出了本章的范围; 我们将在第 14 章回到这个话题。

[2] The Linux Standard Base specifies that init scripts should be placed in /etc/init.d, but some distributions still place them elsewhere. In addition, if your script is to be run at boot time, you need to make a link to it from the appropriate run-level directory (i.e., .../rc3.d). Linux 标准库指定初始化脚本应放在 /etc/init.d 中,但某些发行版仍将它们放在其他位置。 此外,如果您的脚本要在引导时运行,您需要从适当的运行级别目录(即 .../rc3.d)创建一个指向它的链接。

[3] Though certain kernel developers have threatened to do exactly that in the future. 尽管某些内核开发人员已经威胁要在未来这样做。

[4] The init script scull.init doesn't accept driver options on the command line, but it supports a configuration file, because it's designed for automatic use at boot and shutdown time. 初始化脚本 scull.init 不接受命令行上的驱动程序选项,但它支持配置文件,因为它是为在启动和关闭时自动使用而设计的。

[5] Note that release isn't invoked every time a process calls close. Whenever a file structure is shared (for example, after a fork or a dup), release won't be invoked until all copies are closed. If you need to flush pending data when any copy is closed, you should implement the flush method. 请注意,每次进程调用 close 时都不会调用 release。 每当共享文件结构时(例如,在 fork 或 dup 之后),直到所有副本都关闭后才会调用 release。 如果您需要在关闭任何副本时刷新挂起的数据,您应该实现 flush 方法。

[6] There is an older mechanism that avoids the use of cdev structures (which we discuss in Section 3.4.2). New code should use the newer technique, however. 有一种较旧的机制可以避免使用 cdev 结构(我们将在第 3.4.2 节中讨论)。 然而,新代码应该使用更新的技术。

[7] The other flavors of the device are closed by different functions because scull_open substituted a different filp->f_op for each device. We'll discuss these as we introduce each flavor. 该设备的其他风格由不同的函数关闭,因为 scull_open 为每个设备替换了不同的 filp->f_op。 我们将在介绍每种风格时讨论这些内容。

你可能感兴趣的:(Linux,kernel,linux)