日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux内核头文件 cdev.h 解析

發布時間:2024/8/24 linux 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux内核头文件 cdev.h 解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.



遇到一個內核API——cdev_init 就找到這里來了。



#ifndef _LINUX_CDEV_H #define _LINUX_CDEV_H#include <linux/kobject.h #include <linux/kdev_t.h> #include <linux/list.h>struct file_operations; struct inode; struct module;struct cdev {struct kobject kobj;struct module *owner;const struct file_operations *ops;struct list_head list;dev_t dev;unsigned int count; };void cdev_init(struct cdev *, const struct file_operations *); //初始化字符設備能夠進行的文件操作——file_operations 結構體記錄了全部能夠對cdev結構體描寫敘述的字符設備進行的操作struct cdev *cdev_alloc(void);void cdev_put(struct cdev *p);int cdev_add(struct cdev *, dev_t, unsigned);void cdev_del(struct cdev *);void cd_forget(struct inode *);extern struct backing_dev_info directly_mappable_cdev_bdi;#endif

作者也不寫個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:

void cdev_del(struct cdev *dev);
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 解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。