您的位置 首页 系统

ARM-Linux驱动–MTD驱动剖析(二)

主机:GentooLinux11.2withlinuxkernel3.0.6硬件平台:FL2440(S3C2440)withlinuxkernel2.6.351、mtd_notifier结构体//..

主机:Gentoo Linux 11.2 with linux kernel 3.0.6

硬件渠道:FL2440(S3C2440)with linux kernel 2.6.35

1、mtd_notifier结构体

  1. //MTD设备告诉结构体
  2. structmtd_notifier{
  3. void(*add)(structmtd_info*mtd);//参加MTD原始/字符/块设备时碑文
  4. void(*remove)(structmtd_info*mtd);//移除MTD原始/字符/块设备时碑文
  5. structlist_headlist;//list是双向链表,界说在include/linux/list.h
  6. };

而struct list_head界说在/include/linux/list.h中,内核中其宏界说和函数如下

INIT_LIST_HEAD(ptr) 初始化ptr节点为表头,将前趋与后继都指向自己。
LIST_HEAD(name) 声明并初始化双向循环链表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)
在链表中头节点后刺进元素new,调用__list_add()完成。
static inline void list_add_tail(struct list_head *new, struct list_head *head)
在链表结尾刺进元素new,调用__list_add()完成。

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移动到链表结尾。

static inline int list_empty(const struct list_head *head)
测验链表是否为空。

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不为空的情况下,调用__list_splice()完成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的界说在内核源文件include/linux/list.h中:
#define list_entry(ptr, type, member)
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
b. 其功用是依据list_head型指针ptr换算成其宿主结构的开端地址,该宿主结构是type型的,而ptr在其宿主结构中界说为member成员。

2、add_mtd_device函数

  1. /**
  2. *add_mtd_device-registeranMTDdevice
  3. *@mtd:pointertonewMTDdeviceinfostructure
  4. *
  5. *AddadevicetothelistofMTDdevicespresentinthesystem,and
  6. *notifyeachcurrentlyactiveMTDuserofitsarrival.Returns
  7. *zeroonsuccessor1onfailure,whichcurrentlywillonlyhappen
  8. *ifthereisinsufficientmemoryorasysfserror.
  9. */
  10. //添加MTD设备函数,将MTD设备参加MTD设备链表,并告诉一切的MTDuser该MTD设备。回来0一共成功,回来1一共犯错(内存不足或文件体系过错)
  11. intadd_mtd_device(structmtd_info*mtd)
  12. {
  13. structmtd_notifier*not;//界说一个MTD设备告诉器
  14. inti,error;
  15. //下面是设置mtd_info结构体信息
  16. if(!mtd->backing_dev_info){
  17. switch(mtd->type){
  18. caseMTD_RAM://MTD_RAM界说在include/mtd/mtd-abi.h
  19. mtd->backing_dev_info=&mtd_bdi_rw_mappable;
  20. break;
  21. caseMTD_ROM:
  22. mtd->backing_dev_info=&mtd_bdi_ro_mappable;
  23. break;
  24. default:
  25. mtd->backing_dev_info=&mtd_bdi_unmappable;
  26. break;
  27. }
  28. }
  29. BUG_ON(mtd->writesize==0);
  30. mutex_lock(&mtd_table_mutex);//给操作mtd_table加锁
  31. do{
  32. if(!idr_pre_get(&mtd_idr,GFP_KERNEL))//为mtd_idr分配内存
  33. gotofail_locked;
  34. error=idr_get_new(&mtd_idr,mtd,&i);//将id号和mtd_idr相关
  35. }while(error==-EAGAIN);
  36. if(error)
  37. gotofail_locked;
  38. mtd->index=i;
  39. mtd->usecount=0;
  40. if(is_power_of_2(mtd->erasesize))
  41. mtd->erasesize_shift=ffs(mtd->erasesize)-1;
  42. else
  43. mtd->erasesize_shift=0;
  44. if(is_power_of_2(mtd->writesize))
  45. mtd->writesize_shift=ffs(mtd->writesize)-1;
  46. else
  47. mtd->writesize_shift=0;
  48. mtd->erasesize_mask=(1<erasesize_shift)-1;
  49. mtd->writesize_mask=(1<writesize_shift)-1;
  50. /*Somechipsalwayspoweruplocked.Unlockthemnow*/
  51. if((mtd->flags&MTD_WRITEABLE)
  52. &&(mtd->flags&MTD_POWERUP_LOCK)&&mtd->unlock){
  53. if(mtd->unlock(mtd,0,mtd->size))
  54. printk(KERN_WARNING
  55. “%s:unlockfailed,writesmaynotwork\n”,
  56. mtd->name);
  57. }
  58. /*Callershouldhavesetdev.parenttomatchthe
  59. *physicaldevice.
  60. */
  61. mtd->dev.type=&mtd_devtype;
  62. mtd->dev.class=&mtd_class;
  63. mtd->dev.devt=MTD_DEVT(i);
  64. //设置mtd设备名
  65. dev_set_name(&mtd->dev,”mtd%d”,i);
  66. //设置mtd设备信息mtd_info
  67. dev_set_drvdata(&mtd->dev,mtd);
  68. //注册设备
  69. if(device_register(&mtd->dev)!=0)
  70. gotofail_added;
  71. //创立设备
  72. if(MTD_DEVT(i))
  73. device_create(&mtd_class,mtd->dev.parent,
  74. MTD_DEVT(i)+1,
  75. NULL,”mtd%dro”,i);
  76. DEBUG(0,”mtd:Givingoutdevice%dto%s\n”,i,mtd->name);
  77. /*Noneedtogetarefcountonthemodulecontaining
  78. thenotifier,sinceweholdthemtd_table_mutex*/
  79. //遍历list链表将每个mtd_notifier碑文add()函数,对新参加的mtd设备操作,告诉一切的MTDuser新的MTD设备的到来
  80. list_for_each_entry(not,&mtd_notifiers,list)
  81. not->add(mtd);
  82. //解锁信号量
  83. mutex_unlock(&mtd_table_mutex);
  84. /*We_know_wearentbeingremoved,because
  85. ourcallerisstillholdingushere.Sonone
  86. ofthistry_nonsense,andnobitchingaboutit
  87. either.:)*/
  88. __module_get(THIS_MODULE);
  89. return0;
  90. fail_added:
  91. idr_remove(&mtd_idr,i);
  92. fail_locked:
  93. mutex_unlock(&mtd_table_mutex);
  94. return1;
  95. }

其间用到的IDR机制如下:

(1)取得idr
要在代码中运用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机制的中心结构体 */
(2)为idr分配内存
int idr_pre_get(struct idr *idp, unsigned int gfp_mask);
每次经过idr取得ID号之前,需求先分配内存。
回来0一共过错,非零值代表正常
(3)分配ID号并将ID号和指针相关
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: 由内核主动分配的ID号
ptr: 和ID号相相关的指针
start_id: 开端ID号。内核在分配ID号时,会从start_id开端。假如为I2C节点分配ID号,能够将设备地址作为start_id
函数调用正常回来0,假如没有ID能够分配,则回来-ENOSPC
在实践中,上述函数常常选用如下办法运用:
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号查找对应的指针
void *idr_find(struct idr *idp, int id);
回来值是和给定id相相关的指针,假如没有,则回来NULL
(5)删去ID
要删去一个ID,运用:
void idr_remove(struct idr *idp, int id);
经过上面这些办法,内核代码能够为子设备,inode生成对应的ID号。这些函数都界说在lib/idr.c中

3、del_mtd_device函数

  1. /**
  2. *del_mtd_device-unregisteranMTDdevice
  3. *@mtd:pointertoMTDdeviceinfostructure
  4. *
  5. *RemoveadevicefromthelistofMTDdevicespresentinthesystem,
  6. *andnotifyeachcurrentlyactiveMTDuserofitsdeparture.
  7. *Returnszeroonsuccessor1onfailure,whichcurrentlywillhappen
  8. *iftherequesteddevicedoesnotappeartobepresentinthelist.
  9. */
  10. //删去mtd设备函数。
  11. //从MTD设备的链表中移除该MTD设备信息,并告诉体系中一切的MTDuser该MTD设备的移除。
  12. //回来0一共成功,回来1一共犯错(该设备信息不存在设备链表中)
  13. intdel_mtd_device(structmtd_info*mtd)
  14. {
  15. intret;
  16. structmtd_notifier*not;//界说一个mtd_notifier指针
  17. mutex_lock(&mtd_table_mutex);
  18. if(idr_find(&mtd_idr,mtd->index)!=mtd){
  19. ret=-ENODEV;
  20. gotoout_error;
  21. }
  22. /*Noneedtogetarefcountonthemodulecontaining
  23. thenotifier,sinceweholdthemtd_table_mutex*/
  24. //遍历list链表,并使每个mtd_notifier碑文remove函数,告诉每个MTDuser该设备的移除
  25. list_for_each_entry(not,&mtd_notifiers,list)
  26. not->remove(mtd);
  27. if(mtd->usecount){
  28. printk(KERN_NOTICE”RemovingMTDdevice#%d(%s)withusecount%d\n”,
  29. mtd->index,mtd->name,mtd->usecount);
  30. ret=-EBUSY;
  31. }else{
  32. device_unregister(&mtd->dev);//移除MTD设备
  33. idr_remove(&mtd_idr,mtd->index);//移除mtd的id号并开释已分配的内存
  34. module_put(THIS_MODULE);
  35. ret=0;
  36. }
  37. out_error:
  38. mutex_unlock(&mtd_table_mutex);
  39. returnret;
  40. }

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->owner))
  25. gotoout_unlock;
  26. if(mtd->get_device){
  27. err=mtd->get_device(mtd);
  28. if(err)
  29. gotoout_put;
  30. }
  31. mtd->usecount++;
  32. mutex_unlock(&mtd_table_mutex);
  33. returnmtd;
  34. out_put:
  35. module_put(mtd->owner);
  36. out_unlock:
  37. mutex_unlock(&mtd_table_mutex);
  38. returnERR_PTR(err);
  39. }

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qianrushi/xitong/264549.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部