linux内核头文件 cdev.h 解析
遇到一個內核API——cdev_init 就找到這里來了。
作者也不寫個API的說明。。。以后用到其它的API再update。。。no zuo no die...
沒想到一個小時之后就update 了哈。。。。。
update:2014年07月29日 凌晨
Char Device Registration
??????????????????? As we mentioned, the kernel uses structures of type struct cdev to represent char devices internally. Before the kernel invokes your device’s operations, you must allocate and register one or more of these structures.
先申請cdev結構體
??????????????????? To do so, your code should include <linux/cdev.h>, where the structure and its associated helper functions are defined.
??????????????????? There are two ways of allocating and initializing one of these structures. If you wish to obtain a standal one cdev structure at runtime, you may do so with code such as:
struct cdev *my_cdev = cdev_alloc(); my_cdev->ops = &my_fops;初始化cdev結構體
???????????????? Chances are, however, that you will want to embed the cdev structure within a device-specific structure of your own; that is what scull does. In that case, you should initialize the structure that you have already allocated with:
void cdev_init(struct cdev *cdev, struct file_operations *fops);
???????????????? Either way, there is one other struct cdev field that you need to initialize. Like the file_operations structure, struct cdev has an owner field that should be set to THIS_MODULE .(這里是,比如,struct cdev* dev; dev->owener = THIS_MODULE.)
cdev結構體初始化完事之后,就須要把設備增加到內核中了,調用cdev_add
???????????????? Once the cdev? structure is set up, the final step is to tell the kernel about it with a call to: int
cdev_add(struct cdev *dev, dev_t num, unsigned int count);???????????????? Here,dev is the cdev structure,num is the first device number to which this device responds, and count is the number of device numbers that should be associated with the device. Often count is one, but there are situations where it makes sense to have more than one device number correspond to a specific device. Consider, for example, the SCSI tape driver, which allows user space to select operating modes (such as density) by assigning multiple minor numbers to each physical device.
ATTENTION!
??????????????? There are a couple of important things to keep in mind when using cdev_add . The first is that this call can fail. If it returns a negative error code, your device has not been added to the system. It almost always succeeds, however, and that brings up the other point: as soon as cdev_add returns, your device is “live” and its operations
can be called by the kernel. You should not call cdev_add until your driver is completely ready to handle operations on the device.
不用cdev設備的話就調用cdev_dev
?????????????? To remove a char device from the system, call:
Clearly, you should not access the cdev structure after passing it to cdev_del .
還有幾個API沒用到,有緣遇到再說吧。。。哈哈哈
轉載于:https://www.cnblogs.com/gcczhongduan/p/4209248.html
總結
以上是生活随笔為你收集整理的linux内核头文件 cdev.h 解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android_launcher的源码详
- 下一篇: position的高级使用