ARM-Linux驱动--MTD驱动分析(二)
时间:11-20
来源:互联网
点击:
- device_unregister(&mtd->r(&mtd->dev);//移除MTD设备
- idr_remove(&mtd_idr,mtd->index);//移除mtd的id号并释放已分配的内存
- module_put(THIS_MODULE);
- ret=0;
- }
- out_error:
- mutex_unlock(&mtd_table_mutex);
- returnret;
- }
4、register_mtd_user函数
- /**
- *register_mtd_user-registerauserofMTDdevices.
- *@new:pointertonotifierinfostructure
- *
- *Registersapairofcallbacksfunctiontobecalleduponaddition
- *orremovalofMTDdevices.Causestheaddcallbacktobeimmediately
- *invokedforeachMTDdevicecurrentlypresentinthesystem.
- */
- //MTD原始设备使用者注册MTD设备(具体的字符设备或块设备)
- //参数是新的mtd通知器,将其加入mtd_notifiers队列,然后
- 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);
- //对每个MTD原始设备执行add函数
- mtd_for_each_device(mtd)
- new->add(mtd);
- mutex_unlock(&mtd_table_mutex);
- }
5、unregister_mtd_user函数
- /**
- *unregister_mtd_user-unregisterauserofMTDdevices.
- *@old:pointertonotifierinfostructure
- *
- *Removesacallbackfunctionpairfromthelistofuserstobe
- *notifieduponadditionorremovalofMTDdevices.Causesthe
- *removecallbacktobeimmediatelyinvokedforeachMTDdevice
- *currentlypresentinthesystem.
- */
- //删除MTD设备。
- //通知所有该MTD原始设备的MTD设备执行remove()函数,将被删除的MTD设备的通知器从mtd_notifier队列中删除
- intunregister_mtd_user(structmtd_notifier*old)
- {
- structmtd_info*mtd;
- mutex_lock(&mtd_table_mutex);
- module_put(THIS_MODULE);
- //通知所有该MTD原始设备的MTD设备执行remove()函数
- mtd_for_each_device(mtd)
- old->remove(mtd);
- //将被删除的MTD设备的通知器从mtd_notifier队列中删除
- list_del(&old->list);
- mutex_unlock(&mtd_table_mutex);
- return0;
- }
6、获取MTD设备的操作指针,只是参数不同,一个是按照设备地址,另一个是安装设备的名称来获取MTD设备的操作地址
struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
struct mtd_info *get_mtd_device_nm(const char *name)
下面现分析第一个函数
- /**
- *get_mtd_device-obtainavalidatedhandleforanMTDdevice
- *@mtd:lastknownaddressoftherequiredMTDdevice
- *@num:internaldevicenumberoftherequiredMTDdevice
- *
- *GivenanumberandNULLaddress,returnthenumthentryinthedevice
- *table,ifany.Givenanaddressandnum==-1,searchthedevicetable
- *foradevicewiththataddressandreturnifitsstillpresent.Given
- *both,returnthenumthdriveronlyifitsaddressmatches.Return
- *errorcodeifnot.
- */
- //根据设备地址来获取MTD设备的操作地址
- 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个设备,若不空,返回地址,若为空,返回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);
- //错误处理
- if(err)
- ret=ERR_PTR(err);
- out:
- mutex_unlock(&mtd_table_mutex);//解锁互斥信号量
- 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原始设备的使用者计数器
- return0;
- }
第二个函数
- /**
- *get_mtd_device_nm-obtainavalidatedhandleforanMTDdeviceby
- *devicename
- *@name:MTDdevicenametoopen
- *
- *ThisfunctionreturnsMTDdevicedescriptionstructureincaseof
- *successandanerrorcodeincaseoffailure.
- */
- //通过设备名来获得相应的MTD原始设备的操作地址
- //该函数和上面的函数类似,不过就是通过循环比较MTD设备的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->if(
ARM-Linux驱动MTD驱动分 相关文章:
- ARM-Linux驱动--MTD驱动分析(一)(11-20)
- ARM-Linux驱动--MTD驱动分析(三)(11-20)
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)