微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > ARM-Linux驱动--MTD驱动分析(二)

ARM-Linux驱动--MTD驱动分析(二)

时间:11-20 来源:互联网 点击:

  1. device_unregister(&mtd->r(&mtd->dev);//移除MTD设备
  2. idr_remove(&mtd_idr,mtd->index);//移除mtd的id号并释放已分配的内存
  3. module_put(THIS_MODULE);
  4. ret=0;
  5. }
  6. out_error:
  7. mutex_unlock(&mtd_table_mutex);
  8. returnret;
  9. }


4、register_mtd_user函数

  1. /**
  2. *register_mtd_user-registerauserofMTDdevices.
  3. *@new:pointertonotifierinfostructure
  4. *
  5. *Registersapairofcallbacksfunctiontobecalleduponaddition
  6. *orremovalofMTDdevices.Causestheaddcallbacktobeimmediately
  7. *invokedforeachMTDdevicecurrentlypresentinthesystem.
  8. */
  9. //MTD原始设备使用者注册MTD设备(具体的字符设备或块设备)
  10. //参数是新的mtd通知器,将其加入mtd_notifiers队列,然后
  11. voidregister_mtd_user(structmtd_notifier*new)
  12. {
  13. structmtd_info*mtd;
  14. mutex_lock(&mtd_table_mutex);
  15. //将new->list头插mtd_notifiers入链表
  16. list_add(&new->list,&mtd_notifiers);
  17. __module_get(THIS_MODULE);
  18. //对每个MTD原始设备执行add函数
  19. mtd_for_each_device(mtd)
  20. new->add(mtd);
  21. mutex_unlock(&mtd_table_mutex);
  22. }


5、unregister_mtd_user函数

  1. /**
  2. *unregister_mtd_user-unregisterauserofMTDdevices.
  3. *@old:pointertonotifierinfostructure
  4. *
  5. *Removesacallbackfunctionpairfromthelistofuserstobe
  6. *notifieduponadditionorremovalofMTDdevices.Causesthe
  7. *removecallbacktobeimmediatelyinvokedforeachMTDdevice
  8. *currentlypresentinthesystem.
  9. */
  10. //删除MTD设备。
  11. //通知所有该MTD原始设备的MTD设备执行remove()函数,将被删除的MTD设备的通知器从mtd_notifier队列中删除
  12. intunregister_mtd_user(structmtd_notifier*old)
  13. {
  14. structmtd_info*mtd;
  15. mutex_lock(&mtd_table_mutex);
  16. module_put(THIS_MODULE);
  17. //通知所有该MTD原始设备的MTD设备执行remove()函数
  18. mtd_for_each_device(mtd)
  19. old->remove(mtd);
  20. //将被删除的MTD设备的通知器从mtd_notifier队列中删除
  21. list_del(&old->list);
  22. mutex_unlock(&mtd_table_mutex);
  23. return0;
  24. }


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)

下面现分析第一个函数

  1. /**
  2. *get_mtd_device-obtainavalidatedhandleforanMTDdevice
  3. *@mtd:lastknownaddressoftherequiredMTDdevice
  4. *@num:internaldevicenumberoftherequiredMTDdevice
  5. *
  6. *GivenanumberandNULLaddress,returnthenumthentryinthedevice
  7. *table,ifany.Givenanaddressandnum==-1,searchthedevicetable
  8. *foradevicewiththataddressandreturnifitsstillpresent.Given
  9. *both,returnthenumthdriveronlyifitsaddressmatches.Return
  10. *errorcodeifnot.
  11. */
  12. //根据设备地址来获取MTD设备的操作地址
  13. structmtd_info*get_mtd_device(structmtd_info*mtd,intnum)
  14. {
  15. structmtd_info*ret=NULL,*other;
  16. interr=-ENODEV;
  17. //给mtd_table加锁,以便互斥访问
  18. mutex_lock(&mtd_table_mutex);
  19. if(num==-1){//num=-1&&链表不空,则返回mtd的地址
  20. mtd_for_each_device(other){
  21. if(other==mtd){
  22. ret=mtd;
  23. break;
  24. }
  25. }
  26. }elseif(num>=0){//num>=0,查找第num个设备,若不空,返回地址,若为空,返回NULL
  27. ret=idr_find(&mtd_idr,num);
  28. if(mtd&&mtd!=ret)
  29. ret=NULL;
  30. }
  31. if(!ret){
  32. ret=ERR_PTR(err);
  33. gotoout;
  34. }
  35. err=__get_mtd_device(ret);
  36. //错误处理
  37. if(err)
  38. ret=ERR_PTR(err);
  39. out:
  40. mutex_unlock(&mtd_table_mutex);//解锁互斥信号量
  41. returnret;
  42. }
  43. int__get_mtd_device(structmtd_info*mtd)
  44. {
  45. interr;
  46. if(!try_module_get(mtd->owner))
  47. return-ENODEV;
  48. if(mtd->get_device){
  49. err=mtd->get_device(mtd);
  50. if(err){
  51. module_put(mtd->owner);
  52. returnerr;
  53. }
  54. }
  55. mtd->usecount++;//增加该MTD原始设备的使用者计数器
  56. return0;
  57. }


第二个函数

  1. /**
  2. *get_mtd_device_nm-obtainavalidatedhandleforanMTDdeviceby
  3. *devicename
  4. *@name:MTDdevicenametoopen
  5. *
  6. *ThisfunctionreturnsMTDdevicedescriptionstructureincaseof
  7. *successandanerrorcodeincaseoffailure.
  8. */
  9. //通过设备名来获得相应的MTD原始设备的操作地址
  10. //该函数和上面的函数类似,不过就是通过循环比较MTD设备的name字段来返回
  11. structmtd_info*get_mtd_device_nm(constchar*name)
  12. {
  13. interr=-ENODEV;
  14. structmtd_info*mtd=NULL,*other;
  15. mutex_lock(&mtd_table_mutex);
  16. mtd_for_each_device(other){
  17. if(!strcmp(name,other->name)){
  18. mtd=other;
  19. break;
  20. }
  21. }
  22. if(!mtd)
  23. gotoout_unlock;
  24. if(!try_module_get(mtd->if(

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top