字元裝置號管理
在《深入Linux裝置驅動程式機制》書中
在第二章講解字元裝置的時候,個人覺得比較有收穫的主要是兩個方面的知識:
1、字元裝置號的管理原理(char_device_struct)
2、字元裝置驅動的file_operation中的函式如何與file結構體中的相應結構對應上,並被應用程式呼叫。
對於以上兩個主要的知識點,我覺得書上的條理已經很清楚的,很容易看懂,我在這裡複述就多餘了。我把學到的兩個知識點用圖的方式總結出來,供大家參考。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1、字元裝置號的管理原理
重點在於核心在管理時所依賴的資料結構char_device_struct以及全域性的散列表chrdevs。
還有就是要知道核心對於裝置號的註冊與登出和驅動功能的實現是沒有必然的聯絡的。裝置號的管理是一個獨立的機制,以避免驅動在使用裝置號的時候發生衝突,導致驅動中的file_operation對應錯誤,進而出現應用層操作錯誤的裝置。因為應用層對裝置的操作就是通過裝置號對應到具體的file_operation的。
2、字元裝置驅動的file_operation中的函式如何與file結構體中的相應結構對應上,並被應用程式呼叫。
這部分的內容主要是要熟悉open函式的呼叫流程,驅動中的file_operation結構體就是在open函式中通過裝置號與程序相關的file結構體中相應函式進行對應的。在完成了open操作之後,其他的檔案操作就可以直接呼叫驅動file_operation中的函數了。
核心對於char_device_struct結構體的管理方式和裝置號的cdev_map是類似的,都是通過主裝置號作為雜湊表的key來索引。
static struct char_device_struct {
struct char_device_struct *next;
unsigned int major; // 主裝置號
unsigned int baseminor; // 起始次裝置號
int minorct; // 裝置編號的範圍大小
char name[64]; // 處理該裝置編號範圍內的裝置驅動的名稱
struct file_operations *fops; // 沒有使用
struct cdev *cdev; // 指向字元裝置驅動程式描述符的指標
} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
struct kobj_map {
struct probe {
struct probe *next; /* 這樣形成了連結串列結構 */
dev_t dev; /* 裝置號 */
unsigned long range; /* 從次裝置號開始連續的裝置數量 */
struct module *owner;
kobj_probe_t *get;
int (*lock) (dev_t, void *);
void *data; /* 指向struct cdev物件 *///指向當前正要加入系統的裝置物件指標p。
} *probes[255];
struct mutex *lock;
}
所以,簡單地說,裝置驅動程式通過呼叫cdev_add把它所管理的裝置物件的指標嵌入到一個型別為struct probe的節點之中,然後再把該節點加入到cdev_map所實現的雜湊連結串列中。
對系統而言,當裝置驅動程式成功呼叫了cdevadd之後,就意味著一個字元裝置物件已經加入到了系統,在需要的時候,系統就可以找到它。對使用者態的程式而言,cdev_add呼叫之後,就已經可以通過檔案系統的介面呼叫到我們的驅動程式,本章稍後將會詳細描述這一過程。
不過在開始檔案系統如何通過cdev-map來使用驅動程式提供的服務這個話題之前,我們要來看看與cdev_add相對應的另一個函式cdevdel。其實光通過這個畫數名,讀者想必也相到這個函式的作用了:在cdev_add中我們動態分配了struct probe型別的節點,那麼當對應的裝置從系統中移除時,顯然需要將它們從連結串列中刪除並釋放節點所佔用的記憶體空間。在cdev-map所管理的連結串列中查詢對應的裝置節點時使用了裝置號。
===================================================================================
/*
* linux/fs/char_dev.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*/
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/major.h>
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/smp_lock.h>
#include <linux/seq_file.h>
#include <linux/kobject.h>
#include <linux/kobj_map.h>
#include <linux/cdev.h>
#include <linux/mutex.h>
#include <linux/backing-dev.h>
#ifdef CONFIG_KMOD
#include <linux/kmod.h>
#endif
#include "internal.h"
/*
* capabilities for /dev/mem, /dev/kmem and similar directly mappable character
* devices
* - permits shared-mmap for read, write and/or exec
* - does not permit private mmap in NOMMU mode (can't do COW)
* - no readahead or I/O queue unplugging required
*/
struct backing_dev_info directly_mappable_cdev_bdi = {
.capabilities = (
#ifdef CONFIG_MMU
/* permit private copies of the data to be taken */
BDI_CAP_MAP_COPY |
#endif
/* permit direct mmap, for read, write or exec */
BDI_CAP_MAP_DIRECT |
BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP),
};
static struct kobj_map *cdev_map;
static DEFINE_MUTEX(chrdevs_lock);
static struct char_device_struct {
struct char_device_struct *next;
unsigned int major;
unsigned int baseminor;
int minorct;
char name[64];
struct file_operations *fops;
struct cdev *cdev;
/* will die */
} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
/* index in the above */
static inline int major_to_index(int major)
{
/* [CGW]: 根據主裝置號,轉換成對應的索引
* 即主裝置號就是索引號
*/
return major % CHRDEV_MAJOR_HASH_SIZE;
}
#ifdef CONFIG_PROC_FS
void chrdev_show(struct seq_file *f, off_t offset)
{
struct char_device_struct *cd;
/* [CGW]: 根據offset (相當於索引),找到對應裝置 */
if (offset < CHRDEV_MAJOR_HASH_SIZE) {
/* [CGW]: 上鎖 */
mutex_lock(&chrdevs_lock);
/* [CGW]: 列印該裝置項下連結串列中所有節點的主裝置號,和裝置名 */
for (cd = chrdevs[offset]; cd; cd = cd->next)
seq_printf(f, "%3d %s\n", cd->major, cd->name);
/* [CGW]: 解鎖 */
mutex_unlock(&chrdevs_lock);
}
}
#endif /* CONFIG_PROC_FS */
/*
* Register a single major with a specified minor range.
*
* If major == 0 this functions will dynamically allocate a major and return
* its number.
*
* If major > 0 this function will attempt to reserve the passed range of
* minors and will return zero on success.
*
* Returns a -ve errno on failure.
*/
static struct char_device_struct *
__register_chrdev_region(unsigned int major, unsigned int baseminor,
int minorct, const char *name)
{
struct char_device_struct *cd, **cp;
int ret = 0;
int i;
/* [cgw]: 分配一塊char_device_struct大小的記憶體塊 */
cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
/* [cgw]: 分配失敗 */
if (cd == NULL)
return ERR_PTR(-ENOMEM);
/*[cgw]: 上鎖,進入臨界區*/
mutex_lock(&chrdevs_lock);
/* temporary */
/*[cgw]: 如果主裝置號為0,則從最大的裝置號開始,往下查詢第一個未被
*註冊的裝置
*/
if (major == 0) {
/*[cgw]: 從大到小開始查詢*/
for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
/* [cgw]: 找到第一個未被註冊的裝置 */
if (chrdevs[i] == NULL)
break;
}
/* [cgw]:未找到空位 */
if (i == 0) {
ret = -EBUSY;
goto out;
}
/* [cgw]: 以該空位的序號為主裝置號 */
major = i;
ret = major;
}
/* [cgw]: 手工分配一個主裝置號,和次裝置號基址 */
cd->major = major;
/* [cgw]: 手工分配次裝置號基址 */
cd->baseminor = baseminor;
/* [cgw]: 分配minorct個次裝置號 */
cd->minorct = minorct;
/* [cgw]: 分配裝置名 */
strncpy(cd->name,name, 64);
/* [cgw]: 找到主裝置號在裝置列表中的索引 */
i = major_to_index(major);
/* [cgw]: 當前分配的裝置號比較裝置列表,判斷該裝置號是否
* 合法
*/
for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
/* [cgw]: 當前分配的主裝置號,小於該主裝置號索引對應的,裝置列表
* 中的主裝置號,合法
* 這個裡有點不解,從i = major_to_index(major);看出,主裝置號和該主
* 裝置號對應的索引是相等的,為什麼(*cp)->major > major是合法呢
*/
if ((*cp)->major > major ||
/* [cgw]: 當前分配的主裝置號,等於該主裝置號索引對應的,裝置列表
* 中的主裝置號,並且符合以下條件之一,當前分配的次裝置號,等於
* 小於該主裝置號索引對應的,裝置列表中的次裝置號。或者,當前分
* 配的次裝置號,小於該主裝置號索引對應的,裝置列表中的次裝置基
* 址以後minorct個次裝置號
*/
((*cp)->major == major &&
(((*cp)->baseminor >= baseminor) ||
((*cp)->baseminor + (*cp)->minorct > baseminor))))
break;
/* Check for overlapping minor ranges. */
/* [cgw]: 當前分配的主裝置號,等於該主裝置號索引對應的,裝置列表
* 中的主裝置號,判斷次裝置號是否在範圍內
*/
if (*cp && (*cp)->major == major) {
int old_min = (*cp)->baseminor;
int old_max = (*cp)->baseminor + (*cp)->minorct - 1;
int new_min = baseminor;
int new_max = baseminor + minorct - 1;
/* New driver overlaps from the left. */
if (new_max >= old_min && new_max <= old_max) {
ret = -EBUSY;
goto out;
}
/* New driver overlaps from the right. */
if (new_min <= old_max && new_min >= old_min) {
ret = -EBUSY;
goto out;
}
}
/* [cgw]: 新加入的裝置, 新增到該主裝置號連結串列 */
cd->next = *cp;
/* [cgw]: 裝置列表指標指向新加入裝置*/
*cp = cd;
/* [cgw]: 解鎖,退出臨界區*/
mutex_unlock(&chrdevs_lock);
return cd;
out:
/* [cgw]: 解鎖,退出臨界區*/
mutex_unlock(&chrdevs_lock);
/* [cgw]: 釋放為新裝置建立的記憶體*/
kfree(cd);
return ERR_PTR(ret);
}
static struct char_device_struct *
__unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
{
struct char_device_struct *cd = NULL, **cp;
/* [CGW]: 根據主裝置號,找出該主裝置號所在列表中的索引*/
int i = major_to_index(major);
/* [CGW]: 上鎖,進入臨界區 */
mutex_lock(&chrdevs_lock);
/* [CGW]: 根據索引,找出該主裝置號所在列表項 */
for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
/* [CGW]:主裝置號所在列表項中, 在主裝置號對應的連結串列中,查詢判斷該主裝置號,
* 次裝置號基址,次裝置號個數是否已經被註冊
*/
if ((*cp)->major == major &&
(*cp)->baseminor == baseminor &&
(*cp)->minorct == minorct)
/* [CGW]: 已經被註冊,停止查詢 */
break;
/* [CGW]: 主裝置號所在連結串列中的節點
* (注意: 裝置列表中,每個裝置號都對應一個連結串列,該連結串列用於存放此裝置號)
*/
if (*cp) {
/* [CGW]: 取出該節點 */
cd = *cp;
/* [CGW]: 更新cp,指向下一個節點*/
*cp = cd->next;
}
/* [CGW]: 解鎖,退出臨界區 */
mutex_unlock(&chrdevs_lock);
/* [CGW]: 返回該裝置(節點) */
return cd;
}
/**
* register_chrdev_region() - register a range of device numbers
* @from: the first in the desired range of device numbers; must include
* the major number.
* @count: the number of consecutive device numbers required
* @name: the name of the device or driver.
*
* Return value is zero on success, a negative error code on failure.
*/
int register_chrdev_region(dev_t from, unsigned count, const char *name)
{
struct char_device_struct *cd;
dev_t to = from + count;
dev_t n, next;
/* [CGW]: 分配count個連續的裝置 */
for (n = from; n < to; n = next) {
/* [CGW]: 主裝置號+1遞增 */
next = MKDEV(MAJOR(n)+1, 0);
if (next > to)
next = to;
/* [CGW]: 根據主、次裝置號基址,分配next - n個連續次裝置號的裝置,
* 並根據主裝置號分配裝置名
* 如果MINOR(n)為0,next-n的值應恆為256,未驗證!!!
*/
cd = __register_chrdev_region(MAJOR(n), MINOR(n),
next - n, name);
/* [CGW]: 分配失敗 */
if (IS_ERR(cd))
goto fail;
}
return 0;
fail:
/* [CGW]: 當前分配到了第n個裝置就失敗了*/
to = n;
/* [CGW]: 登出剛剛分配的所有裝置 */
for (n = from; n < to; n = next) {
next = MKDEV(MAJOR(n)+1, 0);
/* [CGW]: 對應的記憶體空間 */
kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
}
/* [CGW]: 返回這個分配失敗的裝置指標 */
return PTR_ERR(cd);
}
/**
* alloc_chrdev_region() - register a range of char device numbers
* @dev: output parameter for first assigned number
* @baseminor: first of the requested range of minor numbers
* @count: the number of minor numbers required
* @name: the name of the associated device or driver
*
* Allocates a range of char device numbers. The major number will be
* chosen dynamically, and returned (along with the first minor number)
* in @dev. Returns zero or a negative error code.
*/
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
const char *name)
{
struct char_device_struct *cd;
/* [CGW]: 自動分配一個裝置,因為主裝置號為0
* 以baseminor為基址,分配count個次裝置號
*/
cd = __register_chrdev_region(0, baseminor, count, name);
/* [CGW]: 分配失敗 */
if (IS_ERR(cd))
return PTR_ERR(cd);
/* [CGW]: 返回裝置號 */
*dev = MKDEV(cd->major, cd->baseminor);
return 0;
}
/**
* register_chrdev() - Register a major number for character devices.
* @major: major device number or 0 for dynamic allocation
* @name: name of this range of devices
* @fops: file operations associated with this devices
*
* If @major == 0 this functions will dynamically allocate a major and return
* its number.
*
* If @major > 0 this function will attempt to reserve a device with the given
* major number and will return zero on success.
*
* Returns a -ve errno on failure.
*
* The name of this device has nothing to do with the name of the device in
* /dev. It only helps to keep track of the different owners of devices. If
* your module name has only one type of devices it's ok to use e.g. the name
* of the module here.
*
* This function registers a range of 256 minor numbers. The first minor number
* is 0.
*/
int register_chrdev(unsigned int major, const char *name,
const struct file_operations *fops)
{
struct char_device_struct *cd;
struct cdev *cdev;
char *s;
int err = -ENOMEM;
/* [cgw]: 分配一個裝置,次裝置號為0~255 */
cd = __register_chrdev_region(major, 0, 256, name);
if (IS_ERR(cd))
return PTR_ERR(cd);
/* [cgw]:分配一個cdev結構體 */
cdev = cdev_alloc();
if (!cdev)
goto out2;
cdev->owner = fops->owner;
cdev->ops = fops;
/* [cgw]: 設定kobject的名字 */
kobject_set_name(&cdev->kobj, "%s", name);
/* [cgw]: 把kobject的名字kobj->name中的'/'替換成'!' */
for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
*s = '!';
/* [cgw]: 新增一個字元裝置到系統*/
err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
if (err)
goto out;
/* [cgw]: 設定char_device_struct中的cdev指標 */
cd->cdev = cdev;
return major ? 0 : cd->major;
out:
/* [cgw]: kobect 引用計數-1 */
kobject_put(&cdev->kobj);
out2:
/* [cgw]: 釋放剛註冊的裝置 */
kfree(__unregister_chrdev_region(cd->major, 0, 256));
return err;
}
/**
* unregister_chrdev_region() - return a range of device numbers
* @from: the first in the range of numbers to unregister
* @count: the number of device numbers to unregister
*
* This function will unregister a range of @count device numbers,
* starting with @from. The caller should normally be the one who
* allocated those numbers in the first place...
*/
void unregister_chrdev_region(dev_t from, unsigned count)
{
dev_t to = from + count;
dev_t n, next;
/* [CGW]: 登出所有從from到to的count個裝置 */
for (n = from; n < to; n = next) {
/* [CGW]: 查詢下一裝置號*/
next = MKDEV(MAJOR(n)+1, 0);
if (next > to)
next = to;
/* [CGW]: 登出所有剛才註冊的裝置 */
kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
}
}
int unregister_chrdev(unsigned int major, const char *name)
{
struct char_device_struct *cd;
/* [CGW]: 根據主裝置號,找到對應裝置列表項 */
cd = __unregister_chrdev_region(major, 0, 256);
/* [CGW]: 該裝置項有效,並且被註冊 */
if (cd && cd->cdev)
/* [CGW]: 登出該裝置 */
cdev_del(cd->cdev);
/* [CGW]: 釋放該裝置佔用的記憶體空間 */
kfree(cd);
return 0;
}
static DEFINE_SPINLOCK(cdev_lock);
static struct kobject *cdev_get(struct cdev *p)
{
struct module *owner = p->owner;
struct kobject *kobj;
/* [cgw]:cdev_get uses try_module_get to attempt to increment that module's
* usage count. If that operation succeeds, kobject_get is used to increment the
* kobject's reference count as well---<Linux Device Drivers>
* try_module_get(owner)增加owner (THIS_MODULE)引用計數
*/
/* [cgw]: module使用計數+1 */
if (owner && !try_module_get(owner))
return NULL;
/* [cgw]: kobj引用計數+1 */
kobj = kobject_get(&p->kobj);
/* [cgw]: kobj指標返回失敗 */
if (!kobj)
/* [cgw]: module使用計數-1 */
module_put(owner);
return kobj;
}
void cdev_put(struct cdev *p)
{
/* [cgw]: cdev指標不為空 */
if (p) {
/* [cgw]: 獲得模組指標 */
struct module *owner = p->owner;
/* [cgw]: kobj引用計數-1 */
kobject_put(&p->kobj);
/* [cgw]: module使用計數-1 */
module_put(owner);
}
}
/*
* Called every time a character special file is opened
*/
int chrdev_open(struct inode * inode, struct file * filp)
{
struct cdev *p;
struct cdev *new = NULL;
int ret = 0;
/* [cgw]: 進入臨界區 */
spin_lock(&cdev_lock);
/* [cgw]: 從inode中得到一個字元裝置cdev指標 */
p = inode->i_cdev;
/* [cgw]: struct cdev指標為空 */
if (!p) {
struct kobject *kobj;
int idx;
/* [cgw]: 進入臨界區 */
spin_unlock(&cdev_lock);
/* [cgw]: 看看cdev_map的probes[inode->i_rdev]連結串列是否有inode->i_rdev這個裝置
* 並返回這個裝置的kobj
*/
kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
/* [cgw]: kobj為空,錯誤 */
if (!kobj)
return -ENXIO;
/* [cgw]: 根據返回的kobj,找出包含這個kobj的struct cdev指標 */
new = container_of(kobj, struct cdev, kobj);
/* [cgw]: 進入臨界區 */
spin_lock(&cdev_lock);
/* [cgw]: 從inode中得到一個字元裝置cdev指標 */
p = inode->i_cdev;
/* [cgw]: struct cdev指標為空 */
if (!p) {
/* [cgw]: 把這個struct cdev指標填裝到inode->i_cdev */
inode->i_cdev = p = new;
/* [cgw]: 記錄對應的索引 */
inode->i_cindex = idx;
/* [cgw]: 把inode->i_devices插入到p->list */
list_add(&inode->i_devices, &p->list);
/* [cgw]: 清除new指標 */
new = NULL;
/* [cgw]: 返回cdev中kobj指標為空,錯誤 */
} else if (!cdev_get(p))
ret = -ENXIO;
/* [cgw]: 返回cdev中kobj指標為空,錯誤 */
} else if (!cdev_get(p))
ret = -ENXIO;
/* [cgw]: 退出臨界區 */
spin_unlock(&cdev_lock);
/* [cgw]: 實際上是cdev->kobj引用計數-1,module使用計數-1 */
cdev_put(new);
if (ret)
return ret;
/* [cgw]: module使用計數+1,並返回cdev->ops指標 */
filp->f_op = fops_get(p->ops);
/* [cgw]: filp->f_op指標為空,失敗 */
if (!filp->f_op) {
/* [cgw]: 實際上是cdev->kobj引用計數-1,module使用計數-1 */
cdev_put(p);
return -ENXIO;
}
/* [cgw]: 呼叫filp->f_op->open,開啟的是使用者驅動程式中定義的
* file_operations中的open函式
*/
if (filp->f_op->open) {
/* [cgw]: 上鎖 */
lock_kernel();
/* [cgw]: 呼叫filp->f_op->open */
ret = filp->f_op->open(inode,filp);
/* [cgw]: 解鎖 */
unlock_kernel();
}
/* [cgw]: 呼叫filp->f_op->open失敗 */
if (ret)
/* [cgw]: 實際上是cdev->kobj引用計數-1,module使用計數-1 */
cdev_put(p);
return ret;
}
void cd_forget(struct inode *inode)
{
/* [cgw]: 進入臨界區 */
spin_lock(&cdev_lock);
/* [cgw]: 從連結串列刪除一個inode->i_devices節點,
* 並重新初始化這個連結串列
*/
list_del_init(&inode->i_devices);
/* [cgw]: inode->i_cdev指標清0 */
inode->i_cdev = NULL;
/* [cgw]: 退出臨界區 */
spin_unlock(&cdev_lock);
}
static void cdev_purge(struct cdev *cdev)
{
/* [cgw]: 進入臨界區 */
spin_lock(&cdev_lock);
/* [cgw]: 測試cdev->list這個連結串列是否為空
*
*/
while (!list_empty(&cdev->list)) {
struct inode *inode;
/* [cgw]: 找出包含cdev->list的struct inode結構體的指標 */
inode = container_of(cdev->list.next, struct inode, i_devices);
/* [cgw]: 從連結串列刪除一個inode->i_devices節點,
* 並重新初始化這個連結串列
*/
list_del_init(&inode->i_devices);
/* [cgw]: inode->i_cdev指標清0 */
inode->i_cdev = NULL;
}
/* [cgw]: 退出臨界區 */
spin_unlock(&cdev_lock);
}
/*
* Dummy default file-operations: the only thing this does
* is contain the open that then fills in the correct operations
* depending on the special file...
*/
const struct file_operations def_chr_fops = {
.open = chrdev_open,
};
static struct kobject *exact_match(dev_t dev, int *part, void *data)
{
struct cdev *p = data;
/* [cgw]: 返回cdev中kobj成員指標 */
return &p->kobj;
}
static int exact_lock(dev_t dev, void *data)
{
struct cdev *p = data;
/* [cgw]: data中kobj引用計數+1,並返回kobj指標 */
return cdev_get(p) ? 0 : -1;
}
/**
* cdev_add() - add a char device to the system
* @p: the cdev structure for the device
* @dev: the first device number for which this device is responsible
* @count: the number of consecutive minor numbers corresponding to this
* device
*
* cdev_add() adds the device represented by @p to the system, making it
* live immediately. A negative error code is returned on failure.
*/
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
{
/* [cgw]: 分配一個dev裝置號給p->dev */
p->dev = dev;
/* [cgw]: 分配count個連續的次裝置號
* 這裡實際是分配count裝置,只是次裝置號不一樣,主裝置號都一樣
*/
p->count = count;
/* [cgw]: 把新加入的裝置填裝到一個probe結構,並把這個probe插入到
* 對應probes[MAJOR(dev)]連結串列,即probes[]中每一個元素都是一個連結串列
*/
return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
}
static void cdev_unmap(dev_t dev, unsigned count)
{
/* [cgw]: 從probes[MAJOR(dev)]連結串列中刪除一個節點(probe) */
kobj_unmap(cdev_map, dev, count);
}
/**
* cdev_del() - remove a cdev from the system
* @p: the cdev structure to be removed
*
* cdev_del() removes @p from the system, possibly freeing the structure
* itself.
*/
void cdev_del(struct cdev *p)
{
/* [cgw]: 從probes[MAJOR(p->dev)]連結串列中刪除一個節點(probe)
*
*/
cdev_unmap(p->dev, p->count);
/* [cgw]: kobj引用計數-1 */
kobject_put(&p->kobj);
}
static void cdev_default_release(struct kobject *kobj)
{
/* [cgw]: 找到包含kobj的結構體struct cdev的指標 */
struct cdev *p = container_of(kobj, struct cdev, kobj);
/* [cgw]: 從cdev->list連結串列中刪除cdev */
cdev_purge(p);
}
static void cdev_dynamic_release(struct kobject *kobj)
{
/* [cgw]: 找到包含kobj的結構體struct cdev的指標 */
struct cdev *p = container_of(kobj, struct cdev, kobj);
/* [cgw]: 從cdev->list連結串列中刪除cdev */
cdev_purge(p);
/* [cgw]: 釋放這個cdev的記憶體空間 */
kfree(p);
}
static struct kobj_type ktype_cdev_default = {
.release = cdev_default_release,
};
static struct kobj_type ktype_cdev_dynamic = {
.release = cdev_dynamic_release,
};
/**
* cdev_alloc() - allocate a cdev structure
*
* Allocates and returns a cdev structure, or NULL on failure.
*/
struct cdev *cdev_alloc(void)
{
/* [cgw]: 分配一個cdev結構體 */
struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
/* [cgw]: 分配cdev結構體成功 */
if (p) {
/* [cgw]: 分配一個kobj.ktype結構體,指向&ktype_cdev_dynamic
* 為這個驅動制定一個統一的行為,提供釋放kobj的方法
*/
p->kobj.ktype = &ktype_cdev_dynamic;
/* [cgw]: 初始化連結串列,把這個cdev插入連結串列頭 */
INIT_LIST_HEAD(&p->list);
/* [cgw]: 初始化kobject,每個物件都有一個kobject */
kobject_init(&p->kobj);
}
return p;
}
/**
* cdev_init() - initialize a cdev structure
* @cdev: the structure to initialize
* @fops: the file_operations for this device
*
* Initializes @cdev, remembering @fops, making it ready to add to the
* system with cdev_add().
*/
void cdev_init(struct cdev *cdev, const struct file_operations *fops)
{
/* [cgw]: cdev結構體清零 */
memset(cdev, 0, sizeof *cdev);
/* [cgw]: 初始化連結串列,把這個cdev插入連結串列頭 */
INIT_LIST_HEAD(&cdev->list);
/* [cgw]: 分配一個kobj.ktype結構體,指向&ktype_cdev_default
* 為這個驅動制定一個預設的統一的行為,提供恢復預設kobj的方法
* 沒有釋放kobj記憶體空間
*/
cdev->kobj.ktype = &ktype_cdev_default;
/* [cgw]: 初始化kobject,每個物件都有一個kobject */
kobject_init(&cdev->kobj);
/* [cgw]: cdev->ops指向驅動程式中的file_operations結構體 */
cdev->ops = fops;
}
static struct kobject *base_probe(dev_t dev, int *part, void *data)
{
if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
/* Make old-style 2.4 aliases work */
request_module("char-major-%d", MAJOR(dev));
return NULL;
}
void __init chrdev_init(void)
{
/*[cgw]: 初始化cdev_map變數 */
cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
}
/* Let modules do char dev stuff */
EXPORT_SYMBOL(register_chrdev_region);
EXPORT_SYMBOL(unregister_chrdev_region);
EXPORT_SYMBOL(alloc_chrdev_region);
EXPORT_SYMBOL(cdev_init);
EXPORT_SYMBOL(cdev_alloc);
EXPORT_SYMBOL(cdev_del);
EXPORT_SYMBOL(cdev_add);
EXPORT_SYMBOL(register_chrdev);
EXPORT_SYMBOL(unregister_chrdev);
EXPORT_SYMBOL(directly_mappable_cdev_bdi);