ARM-Linux驅(qū)動(dòng)--MTD驅(qū)動(dòng)分析(二)
硬件平臺(tái):FL2440(S3C2440)with linux kernel 2.6.35
1、mtd_notifier結(jié)構(gòu)體
- //MTD設(shè)備通知結(jié)構(gòu)體
- structmtd_notifier{
- void(*add)(structmtd_info*mtd);//加入MTD原始/字符/塊設(shè)備時(shí)執(zhí)行
- void(*remove)(structmtd_info*mtd);//移除MTD原始/字符/塊設(shè)備時(shí)執(zhí)行
- structlist_headlist;//list是雙向鏈表,定義在include/linux/list.h
- };
INIT_LIST_HEAD(ptr) 初始化ptr節(jié)點(diǎn)為表頭,將前趨與后繼都指向自己。
LIST_HEAD(name) 聲明并初始化雙向循環(huán)鏈表name。
static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
向鏈表中在prev與next之間插入元素new
static inline void list_add(struct list_head *new, struct list_head *head)
在鏈表中頭節(jié)點(diǎn)后插入元素new,調(diào)用__list_add()實(shí)現(xiàn)。
static inline void list_add_tail(struct list_head *new, struct list_head *head)
在鏈表末尾插入元素new,調(diào)用__list_add()實(shí)現(xiàn)。
static inline void __list_del(struct list_head * prev, struct list_head * next)
刪除鏈表中prev與next之間的元素。
static inline void list_del(struct list_head *entry)
刪除鏈表中的元素entry。
static inline void list_del_init(struct list_head *entry)
從鏈表中刪除元素entry,并將其初始化為新的鏈表。
static inline void list_move(struct list_head *list, struct list_head *head)
從鏈表中刪除list元素,并將其加入head鏈表。
static inline void list_move_tail(struct list_head *list, struct list_head *head)
把list移動(dòng)到鏈表末尾。
static inline int list_empty(const struct list_head *head)
測(cè)試鏈表是否為空。
static inline void __list_splice(struct list_head *list, struct list_head *head)
將鏈表list與head合并。
static inline void list_splice(struct list_head *list, struct list_head *head)
在list不為空的情況下,調(diào)用__list_splice()實(shí)現(xiàn)list與head的合并。
static inline void list_splice_init(struct list_head *list, struct list_head *head)
將兩鏈表合并,并將list初始化。
list_entry(ptr, type, member)
list_entry的定義是怎么回事?
a. list_entry的定義在內(nèi)核源文件include/linux/list.h中:
#define list_entry(ptr, type, member)
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
b. 其功能是根據(jù)list_head型指針ptr換算成其宿主結(jié)構(gòu)的起始地址,該宿主結(jié)構(gòu)是type型的,而ptr在其宿主結(jié)構(gòu)中定義為member成員。
2、add_mtd_device函數(shù)
- /**
- *add_mtd_device-registeranMTDdevice
- *@mtd:pointertonewMTDdeviceinfostructure
- *
- *AddadevicetothelistofMTDdevicespresentinthesystem,and
- *notifyeachcurrentlyactiveMTDuserofitsarrival.Returns
- *zeroonsuccessor1onfailure,whichcurrentlywillonlyhappen
- *ifthereisinsufficientmemoryorasysfserror.
- */
- //添加MTD設(shè)備函數(shù),將MTD設(shè)備加入MTD設(shè)備鏈表,并通知所有的MTDuser該MTD設(shè)備。返回0表示成功,返回1表示出錯(cuò)(內(nèi)存不足或文件系統(tǒng)錯(cuò)誤)
- intadd_mtd_device(structmtd_info*mtd)
- {
- structmtd_notifier*not;//定義一個(gè)MTD設(shè)備通知器
- inti,error;
- //下面是設(shè)置mtd_info結(jié)構(gòu)體信息
- if(!mtd->backing_dev_info){
- switch(mtd->type){
- caseMTD_RAM://MTD_RAM定義在include/mtd/mtd-abi.h
- mtd->backing_dev_info=&mtd_bdi_rw_mappable;
- break;
- caseMTD_ROM:
- mtd->backing_dev_info=&mtd_bdi_ro_mappable;
- break;
- default:
- mtd->backing_dev_info=&mtd_bdi_unmappable;
- break;
- }
- }
- BUG_ON(mtd->writesize==0);
- mutex_lock(&mtd_table_mutex);//給操作mtd_table加鎖
- do{
- if(!idr_pre_get(&mtd_idr,GFP_KERNEL))//為mtd_idr分配內(nèi)存
- gotofail_locked;
- error=idr_get_new(&mtd_idr,mtd,&i);//將id號(hào)和mtd_idr關(guān)聯(lián)
- }while(error==-EAGAIN);
- if(error)
- gotofail_locked;
- mtd->index=i;
- mtd->usecount=0;
- if(is_power_of_2(mtd->erasesize))
- mtd->erasesize_shift=ffs(mtd->erasesize)-1;
- else
- mtd->erasesize_shift=0;
- if(is_power_of_2(mtd->writesize))
- mtd->writesize_shift=ffs(mtd->writesize)-1;
- else
- mtd->writesize_shift=0;
- mtd->erasesize_mask=(1<
erasesize_shift)-1; - mtd->writesize_mask=(1<
writesize_shift)-1; - /*Somechipsalwayspoweruplocked.Unlockthemnow*/
- if((mtd->flags&MTD_WRITEABLE)
- &&(mtd->flags&MTD_POWERUP_LOCK)&&mtd->unlock){
- if(mtd->unlock(mtd,0,mtd->size))
- printk(KERN_WARNING
- "%s:unlockfailed,writesmaynotworkn",
- mtd->name);
- }
- /*Callershouldhavesetdev.parenttomatchthe
- *physicaldevice.
- */
- mtd->dev.type=&mtd_devtype;
- mtd->dev.class=&mtd_class;
- mtd->dev.devt=MTD_DEVT(i);
- //設(shè)置mtd設(shè)備名
- dev_set_name(&mtd->dev,"mtd%d",i);
- //設(shè)置mtd設(shè)備信息mtd_info
- dev_set_drvdata(&mtd->dev,mtd);
- //注冊(cè)設(shè)備
- if(device_register(&mtd->dev)!=0)
- gotofail_added;
- //創(chuàng)建設(shè)備
- if(MTD_DEVT(i))
- device_create(&mtd_class,mtd->dev.parent,
- MTD_DEVT(i)+1,
- NULL,"mtd%dro",i);
- DEBUG(0,"mtd:Givingoutdevice%dto%sn",i,mtd->name);
- /*Noneedtogetarefcountonthemodulecontaining
- thenotifier,sinceweholdthemtd_table_mutex*/
- //遍歷list鏈表將每個(gè)mtd_notifier執(zhí)行add()函數(shù),對(duì)新加入的mtd設(shè)備操作,通知所有的MTDuser新的MTD設(shè)備的到來
- list_for_each_entry(not,&mtd_notifiers,list)
- not->add(mtd);
- //解鎖信號(hào)量
- mutex_unlock(&mtd_table_mutex);
- /*We_know_wearentbeingremoved,because
- ourcallerisstillholdingushere.Sonone
- ofthistry_nonsense,andnobitchingaboutit
- either.:)*/
- __module_get(THIS_MODULE);
- return0;
- fail_added:
- idr_remove(&mtd_idr,i);
- fail_locked:
- mutex_unlock(&mtd_table_mutex);
- return1;
- }
其中用到的IDR機(jī)制如下:
(1)獲得idr
要在代碼中使用idr,首先要包括
void idr_init(struct idr *idp);
其中idr定義如下:
struct idr {
struct idr_layer *top;
struct idr_layer *id_free;
int layers;
int id_free_cnt;
spinlock_t lock;
};
/* idr是idr機(jī)制的核心結(jié)構(gòu)體 */
(2)為idr分配內(nèi)存
int idr_pre_get(struct idr *idp, unsigned int gfp_mask);
每次通過idr獲得ID號(hào)之前,需要先分配內(nèi)存。
返回0表示錯(cuò)誤,非零值代表正常
(3)分配ID號(hào)并將ID號(hào)和指針關(guān)聯(lián)
int idr_get_new(struct idr *idp, void *ptr, int *id);
int idr_get_new_above(struct idr *idp, void *ptr, int start_id, int *id);
idp: 之前通過idr_init初始化的idr指針
id: 由內(nèi)核自動(dòng)分配的ID號(hào)
ptr: 和ID號(hào)相關(guān)聯(lián)的指針
start_id: 起始ID號(hào)。內(nèi)核在分配ID號(hào)時(shí),會(huì)從start_id開始。如果為I2C節(jié)點(diǎn)分配ID號(hào),可以將設(shè)備地址作為start_id
函數(shù)調(diào)用正常返回0,如果沒有ID可以分配,則返回-ENOSPC
在實(shí)際中,上述函數(shù)常常采用如下方式使用:
again:
if (idr_pre_get(&my_idr, GFP_KERNEL) == 0) {
/* No memory, give up entirely */
}
spin_lock(&my_lock);
result = idr_get_new(&my_idr, &target, &id);
if (result == -EAGAIN) {
sigh();
spin_unlock(&my_lock);
goto again;
}
(4)通過ID號(hào)搜索對(duì)應(yīng)的指針
void *idr_find(struct idr *idp, int id);
返回值是和給定id相關(guān)聯(lián)的指針,如果沒有,則返回NULL
(5)刪除ID
要?jiǎng)h除一個(gè)ID,使用:
void idr_remove(struct idr *idp, int id);
通過上面這些方法,內(nèi)核代碼可以為子設(shè)備,inode生成對(duì)應(yīng)的ID號(hào)。這些函數(shù)都定義在lib/idr.c中
- /**
- *del_mtd_device-unregisteranMTDdevice
- *@mtd:pointertoMTDdeviceinfostructure
- *
- *RemoveadevicefromthelistofMTDdevicespresentinthesystem,
- *andnotifyeachcurrentlyactiveMTDuserofitsdeparture.
- *Returnszeroonsuccessor1onfailure,whichcurrentlywillhappen
- *iftherequesteddevicedoesnotappeartobepresentinthelist.
- */
- //刪除mtd設(shè)備函數(shù)。
- //從MTD設(shè)備的鏈表中移除該MTD設(shè)備信息,并通知系統(tǒng)中所有的MTDuser該MTD設(shè)備的移除。
- //返回0表示成功,返回1表示出錯(cuò)(該設(shè)備信息不存在設(shè)備鏈表中)
- intdel_mtd_device(structmtd_info*mtd)
- {
- intret;
- structmtd_notifier*not;//定義一個(gè)mtd_notifier指針
- mutex_lock(&mtd_table_mutex);
- if(idr_find(&mtd_idr,mtd->index)!=mtd){
- ret=-ENODEV;
- gotoout_error;
- }
- /*Noneedtogetarefcountonthemodulecontaining
- thenotifier,sinceweholdthemtd_table_mutex*/
- //遍歷list鏈表,并使每個(gè)mtd_notifier執(zhí)行remove函數(shù),通知每個(gè)MTDuser該設(shè)備的移除
- list_for_each_entry(not,&mtd_notifiers,list)
- not->remove(mtd);
- if(mtd->usecount){
- printk(KERN_NOTICE"RemovingMTDdevice#%d(%s)withusecount%dn",
- mtd->index,mtd->name,mtd->usecount);
- ret=-EBUSY;
- }else{
- device_unregister(&mtd->dev);//移除MTD設(shè)備
- idr_remove(&mtd_idr,mtd->index);//移除mtd的id號(hào)并釋放已分配的內(nèi)存
- module_put(THIS_MODULE);
- ret=0;
- }
- out_error:
- mutex_unlock(&mtd_table_mutex);
- returnret;
- }
4、register_mtd_user函數(shù)
- /**
- *register_mtd_user-registerauserofMTDdevices.
- *@new:pointertonotifierinfostructure
- *
- *Registersapairofcallbacksfunctiontobecalleduponaddition
- *orremovalofMTDdevices.Causestheaddcallbacktobeimmediately
- *invokedforeachMTDdevicecurrentlypresentinthesystem.
- */
- //MTD原始設(shè)備使用者注冊(cè)MTD設(shè)備(具體的字符設(shè)備或塊設(shè)備)
- //參數(shù)是新的mtd通知器,將其加入mtd_notifiers隊(duì)列,然后
- voidregister_mtd_user(structmtd_notifier*new)
- {
- structmtd_info*mtd;
- mutex_lock(&mtd_table_mutex);
- //將new->list頭插mtd_notifiers入鏈表
- list_add(&new->list,&mtd_notifiers);
- __module_get(THIS_MODULE);
- //對(duì)每個(gè)MTD原始設(shè)備執(zhí)行add函數(shù)
- mtd_for_each_device(mtd)
- new->add(mtd);
- mutex_unlock(&mtd_table_mutex);
- }
5、unregister_mtd_user函數(shù)
- /**
- *unregister_mtd_user-unregisterauserofMTDdevices.
- *@old:pointertonotifierinfostructure
- *
- *Removesacallbackfunctionpairfromthelistofuserstobe
- *notifieduponadditionorremovalofMTDdevices.Causesthe
- *removecallbacktobeimmediatelyinvokedforeachMTDdevice
- *currentlypresentinthesystem.
- */
- //刪除MTD設(shè)備。
- //通知所有該MTD原始設(shè)備的MTD設(shè)備執(zhí)行remove()函數(shù),將被刪除的MTD設(shè)備的通知器從mtd_notifier隊(duì)列中刪除
- intunregister_mtd_user(structmtd_notifier*old)
- {
- structmtd_info*mtd;
- mutex_lock(&mtd_table_mutex);
- module_put(THIS_MODULE);
- //通知所有該MTD原始設(shè)備的MTD設(shè)備執(zhí)行remove()函數(shù)
- mtd_for_each_device(mtd)
- old->remove(mtd);
- //將被刪除的MTD設(shè)備的通知器從mtd_notifier隊(duì)列中刪除
- list_del(&old->list);
- mutex_unlock(&mtd_table_mutex);
- return0;
- }
6、獲取MTD設(shè)備的操作指針,只是參數(shù)不同,一個(gè)是按照設(shè)備地址,另一個(gè)是安裝設(shè)備的名稱來獲取MTD設(shè)備的操作地址
struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
struct mtd_info *get_mtd_device_nm(const char *name)
下面現(xiàn)分析第一個(gè)函數(shù)
- /**
- *get_mtd_device-obtainavalidatedhandleforanMTDdevice
- *@mtd:lastknownaddressoftherequiredMTDdevice
- *@num:internaldevicenumberoftherequiredMTDdevice
- *
- *GivenanumberandNULLaddress,returnthenumthentryinthedevice
- *table,ifany.Givenanaddressandnum==-1,searchthedevicetable
- *foradevicewiththataddressandreturnifitsstillpresent.Given
- *both,returnthenumthdriveronlyifitsaddressmatches.Return
- *errorcodeifnot.
- */
- //根據(jù)設(shè)備地址來獲取MTD設(shè)備的操作地址
- structmtd_info*get_mtd_device(structmtd_info*mtd,intnum)
- {
- structmtd_info*ret=NULL,*other;
- interr=-ENODEV;
- //給mtd_table加鎖,以便互斥訪問
- mutex_lock(&mtd_table_mutex);
- if(num==-1){//num=-1&&鏈表不空,則返回mtd的地址
- mtd_for_each_device(other){
- if(other==mtd){
- ret=mtd;
- break;
- }
- }
- }elseif(num>=0){//num>=0,查找第num個(gè)設(shè)備,若不空,返回地址,若為空,返回NULL
- ret=idr_find(&mtd_idr,num);
- if(mtd&&mtd!=ret)
- ret=NULL;
- }
- if(!ret){
- ret=ERR_PTR(err);
- gotoout;
- }
- err=__get_mtd_device(ret);
- //錯(cuò)誤處理
- if(err)
- ret=ERR_PTR(err);
- out:
- mutex_unlock(&mtd_table_mutex);//解鎖互斥信號(hào)量
- returnret;
- }
- int__get_mtd_device(structmtd_info*mtd)
- {
- interr;
- if(!try_module_get(mtd->owner))
- return-ENODEV;
- if(mtd->get_device){
- err=mtd->get_device(mtd);
- if(err){
- module_put(mtd->owner);
- returnerr;
- }
- }
- mtd->usecount++;//增加該MTD原始設(shè)備的使用者計(jì)數(shù)器
- return0;
- }
第二個(gè)函數(shù)
- /**
- *get_mtd_device_nm-obtainavalidatedhandleforanMTDdeviceby
- *devicename
- *@name:MTDdevicenametoopen
- *
- *ThisfunctionreturnsMTDdevicedescriptionstructureincaseof
- *successandanerrorcodeincaseoffailure.
- */
- //通過設(shè)備名來獲得相應(yīng)的MTD原始設(shè)備的操作地址
- //該函數(shù)和上面的函數(shù)類似,不過就是通過循環(huán)比較MTD設(shè)備的name字段來返回
- structmtd_info*get_mtd_device_nm(constchar*name)
- {
- interr=-ENODEV;
- structmtd_info*mtd=NULL,*other;
- mutex_lock(&mtd_table_mutex);
- mtd_for_each_device(other){
- if(!strcmp(name,other->name)){
- mtd=other;
- break;
- }
- }
- if(!mtd)
- gotoout_unlock;
- if(!try_module_get(mtd->owner))
- gotoout_unlock;
- if(mtd->get_device){
- err=mtd->get_device(mtd);
- if(err)
- gotoout_put;
- }
- mtd->usecount++;
- mutex_unlock(&mtd_table_mutex);
- returnmtd;
- out_put:
- module_put(mtd->owner);
- out_unlock:
- mutex_unlock(&mtd_table_mutex);
- returnERR_PTR(err);
- }
評(píng)論