字符設(shè)備驅(qū)動(dòng)模型淺析
在linux系統(tǒng)中,很多驅(qū)動(dòng)是字符型驅(qū)動(dòng),有些是直接編譯集成在內(nèi)核中,另一些是單獨(dú)編譯成“。ko”動(dòng)態(tài)加載的。其實(shí)字符驅(qū)動(dòng)只是個(gè)外殼,用于內(nèi)核與應(yīng)用程序間通信,無非是調(diào)用open,release,read,write和ioctl等例程。所以根據(jù)應(yīng)用不同,字符驅(qū)動(dòng)能會(huì)調(diào)用其他驅(qū)動(dòng)模塊,如i2c、spi和v4l2等,于是字符驅(qū)動(dòng)還可分WDT驅(qū)動(dòng)、RTC驅(qū)動(dòng)和MTD驅(qū)動(dòng)等。所以在分析其他驅(qū)動(dòng)模塊之前有必要好好分析下字符設(shè)備驅(qū)動(dòng)模型。本篇文章要講的就是字符設(shè)備驅(qū)動(dòng)模型,也就是字符設(shè)備驅(qū)動(dòng)是怎么注冊(cè)和注銷的,怎么生成設(shè)備節(jié)點(diǎn)的,怎么和應(yīng)用程序關(guān)聯(lián)的,例程調(diào)用具體如何實(shí)現(xiàn)的等等。
一、字符設(shè)備驅(qū)動(dòng)的注冊(cè)和注銷
對(duì)于寫過linux-2.6內(nèi)核(本文采用linux-2.6.18內(nèi)核)字符驅(qū)動(dòng)的程序員來說,對(duì)下面這段程序的形式肯定不陌生。int result;
/*
* Register the driver in the kernel
* Dynmically get the major number for the driver using
* alloc_chrdev_region function
*/
result = alloc_chrdev_region( 0, 1,“testchar”);
/* if it fails return error */
if (result dev = 1;
base->range = ~0; /*初始的范圍很大*/
base->get = base_probe; /*保存函數(shù)指針*/
for (i = 0; i probes[i] = base; /*所有指針都指向同一個(gè)base */
p->lock = lock;
return p;
}.
復(fù)制代碼該函數(shù)只是分配了一個(gè)結(jié)構(gòu)體struct kobj_map,并做了初始化,保存了函數(shù)指針base_probe和全局鎖lock。
下面就按照驅(qū)動(dòng)注冊(cè)流程一個(gè)個(gè)解析這些例程調(diào)用吧。首先是alloc_chrdev_region()函數(shù),解析它之前,先看看結(jié)構(gòu)體(定義了255個(gè)結(jié)構(gòu)體指針),static struct char_device_struct {
/*被255整除后相同的設(shè)備號(hào)鏈成一個(gè)單向鏈表*/
struct char_device_struct *next;
unsigned int major; /*主設(shè)備號(hào)*/
unsigned int baseminor; /*次設(shè)備起始號(hào)*/
int minorct; /*次設(shè)備號(hào)范圍*/
char name[64]; /*驅(qū)動(dòng)的名字*/
struct file_operations *fops; /*保存文件操作指針,目前沒有使用*/
struct cdev *cdev; /* will die */ /*目前沒有使用*/
} *chrdevs[CHRDEV_MAJOR_HASH_SIZE]; /* CHRDEV_MAJOR_HASH_SIZE = 255 */
復(fù)制代碼它的作用僅僅是用于注冊(cè)字符設(shè)備驅(qū)動(dòng),保存已經(jīng)注冊(cè)字符驅(qū)動(dòng)的一些信息,如主次設(shè)備號(hào),次設(shè)備號(hào)的數(shù)量,驅(qū)動(dòng)的名字等,便于字符設(shè)備驅(qū)動(dòng)注冊(cè)時(shí)索引查找。
alloc_chrdev_region()函數(shù)很簡(jiǎn)單,通過調(diào)用__register_chrdev_region()來實(shí)現(xiàn),通過英語注釋你也可以明白,這個(gè)函數(shù)有兩個(gè)作用,一是,如果主設(shè)備號(hào)為0,則分配一個(gè)最近的主設(shè)備號(hào),返回給調(diào)用者;二是,如果主設(shè)備號(hào)不為0,則占用好該主設(shè)備號(hào)對(duì)應(yīng)的位置,返回給調(diào)用者。如下,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;
cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
if (cd == NULL)
return ERR_PTR(-ENOMEM);
mutex_lock( /*這下看到了吧,加鎖,就允許你一個(gè)人進(jìn)來*/
/* temporary */
if (major == 0) { /*如果主設(shè)備號(hào)為零,則找一個(gè)最近空閑的號(hào)碼分配*/
for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i——) {
if (chrdevs[i] == NULL)
break;
}
if (i == 0) {
ret = -EBUSY;
goto out;
}
major = i;
ret = major;
}
/*這些不用說你懂的*/
cd->major = major;
cd->baseminor = baseminor;
cd->minorct = minorct;
strncpy(cd->name,name, 64);
i = major_to_index(major);
/*如果主設(shè)備號(hào)不為0,則占用好該主設(shè)備號(hào)對(duì)應(yīng)的位置*/
for (cp = *cp; cp =
if ((*cp)->major > major ||
((*cp)->major == major (*cp)->baseminor >= baseminor))
break;
if (*cp (*cp)->major == major
(*cp)->baseminor next = *cp;
*cp = cd;
mutex_unlock( /*開鎖,隊(duì)列里的下一個(gè)人可以進(jìn)來了*/
return cd;
out:
mutex_unlock(
kfree(cd);
return ERR_PTR(ret);
}
復(fù)制代碼接著是cdev_init()函數(shù),先說說cdev的結(jié)構(gòu)體,struct cdev {
struct kobject kobj; /*不多解釋了,看看鄙人前面寫的文章吧*/
struct module *owner; /*模塊鎖定和加載時(shí)用得著*/
const struct file_operations *ops; /*保存文件操作例程結(jié)構(gòu)體*/
struct list_head list; /* open時(shí),會(huì)將其inode加到該鏈表中,方便判別是否空閑*/
dev_t dev; /*設(shè)備號(hào)*/
unsigned int count;
};
復(fù)制代碼cdev結(jié)構(gòu)體把字符設(shè)備驅(qū)動(dòng)和文件系統(tǒng)相關(guān)聯(lián),后面解析字符設(shè)備驅(qū)動(dòng)怎樣運(yùn)行的時(shí)候會(huì)詳談。
cdev_init()函數(shù)如下,void cdev_init(struct cdev *cdev, const struct file_operations *fops)
{
memset(cdev, 0, sizeof *cdev);
INIT_LIST_HEAD(
cdev->kobj.ktype = /*卸載驅(qū)動(dòng)時(shí)會(huì)用到,別急,后面詳講*/
kobject_init(
cdev->ops = fops; /*用戶寫的字符設(shè)備驅(qū)動(dòng)fops就保存在這了*/
}.
復(fù)制代碼你也看到了,該函數(shù)就是對(duì)變量做了初始化,關(guān)于kobject的解析,建議你看看鄙人博客上寫的《Linux設(shè)備模型淺析之設(shè)備篇》和《Linux設(shè)備模型淺析之驅(qū)動(dòng)篇》兩篇文章,這里就不詳談了。
用戶的fops,在本文中是test_fops,一般形式是這樣的,
static const struct file_operations test_fops = {
。owner = THIS_MODULE,
。open = test_fops_open,
。release = test_fops_release,
。ioctl = test_fops_ioctl,
。read = test_fops_read,
。write = test_fops_write,
};
評(píng)論