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

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

时间:11-20 来源:互联网 点击:
主机:Gentoo Linux 11.2 with linux kernel 3.0.6

硬件平台:FL2440(S3C2440)with linux kernel 2.6.35

本文分析MTD设备的分区管理机制

分区管理实际上是将一个MTD设备分成几个分区,将其作为单独的MTD原始设备进行管理。

1、分区的结构体描述结构体mtd_part

  1. /*Ourpartitionnodestructure*/
  2. //分区结构信息
  3. structmtd_part{
  4. structmtd_infomtd;//mtd_info数据结构,会被加入mtd_table中
  5. structmtd_info*master;//该分区的主分区
  6. uint64_toffset;//该分区的偏移地址
  7. structlist_headlist;
  8. };


2、分区链表mtd_partitions

  1. /*Ourpartitionlinkedlist*/
  2. //声明mtd_partitions链表
  3. staticLIST_HEAD(mtd_partitions);


3、add_mtd_partitions函数

  1. /*
  2. *Thisfunction,givenamasterMTDobjectandapartitiontable,creates
  3. *andregistersslaveMTDobjectswhichareboundtothemasteraccordingto
  4. *thepartitiondefinitions.
  5. *
  6. *Wedontregisterthemaster,orexpectthecallertohavedoneso,
  7. *forreasonsofdataintegrity.
  8. */
  9. //根据一个MTD主设备和分区表,创建新的主设备下的副设备并记录到分区表中
  10. //这里我们不将注射被注册到分区表中,只注册副设备到到分区表中
  11. intadd_mtd_partitions(structmtd_info*master,
  12. conststructmtd_partition*parts,
  13. intnbparts)
  14. {
  15. structmtd_part*slave;
  16. uint64_tcur_offset=0;
  17. inti;
  18. printk(KERN_NOTICE"Creating%dMTDpartitionson\"%s\":\n",nbparts,master->name);
  19. for(i=0;i
  20. slave=add_one_partition(master,parts+i,i,cur_offset);
  21. if(!slave)
  22. return-ENOMEM;
  23. cur_offset=slave->offset+slave->mtd.size;
  24. }
  25. return0;
  26. }
  27. EXPORT_SYMBOL(add_mtd_partitions);


而add_one_partition函数实现如下:

  1. //创建一个分区
  2. staticstructmtd_part*add_one_partition(structmtd_info*master,
  3. conststructmtd_partition*part,intpartno,
  4. uint64_tcur_offset)
  5. {
  6. structmtd_part*slave;
  7. /*allocatethepartitionstructure*/
  8. slave=kzalloc(sizeof(*slave),GFP_KERNEL);//分配内存
  9. if(!slave){
  10. printk(KERN_ERR"memoryallocationerrorwhilecreatingpartitionsfor\"%s\"\n",
  11. master->name);
  12. del_mtd_partitions(master);
  13. returnNULL;
  14. }
  15. list_add(&slave->list,&mtd_partitions);//将原始设备表添加到分区表中
  16. /*setuptheMTDobjectforthispartition*/
  17. //大部分根据master相应的信息设置MTD分区slave的信息
  18. slave->mtd.type=master->type;
  19. slave->mtd.flags=master->flags&~part->mask_flags;
  20. slave->mtd.size=part->size;
  21. slave->mtd.writesize=master->writesize;
  22. slave->mtd.oobsize=master->oobsize;
  23. slave->mtd.oobavail=master->oobavail;
  24. slave->mtd.subpage_sft=master->subpage_sft;
  25. slave->mtd.name=part->name;
  26. slave->mtd.owner=master->owner;
  27. slave->mtd.backing_dev_info=master->backing_dev_info;
  28. /*NOTE:wedontarrangeMTDsasatree;itdbeerror-prone
  29. *tohavethesamedatabeintwodifferentpartitions.
  30. */
  31. slave->mtd.dev.parent=master->dev.parent;
  32. slave->mtd.read=part_read;
  33. slave->mtd.write=part_write;
  34. if(master->panic_write)
  35. slave->mtd.panic_write=part_panic_write;
  36. if(master->point&&master->unpoint){
  37. slave->mtd.point=part_point;
  38. slave->mtd.unpoint=part_unpoint;
  39. }
  40. if(master->get_unmapped_area)
  41. slave->mtd.get_unmapped_area=part_get_unmapped_area;
  42. if(master->read_oob)
  43. slave->mtd.read_oob=part_read_oob;
  44. if(master->write_oob)
  45. slave->mtd.write_oob=part_write_oob;
  46. if(master->read_user_prot_reg)
  47. slave->mtd.read_user_prot_reg=part_read_user_prot_reg;
  48. if(master->read_fact_prot_reg)
  49. slave->mtd.read_fact_prot_reg=part_read_fact_prot_reg;
  50. if(master->write_user_prot_reg)
  51. slave->mtd.write_user_prot_reg=part_write_user_prot_reg;
  52. if(master->lock_user_prot_reg)
  53. slave->mtd.lock_user_prot_reg=part_lock_user_prot_reg;
  54. if(master->get_user_prot_info)
  55. slave->mtd.get_user_prot_info=part_get_user_prot_info;
  56. if(master->get_fact_prot_info)
  57. slave->mtd.get_fact_prot_info=part_get_fact_prot_info;
  58. if(master->sync)
  59. slave->mtd.sync=part_sync;
  60. if(!partno&&!master->dev.class&&master->suspend&&master->resume){
  61. slave->mtd.suspend=part_suspend;
  62. slave->mtd.resume=part_resume;
  63. }
  64. if(master->writev)
  65. slave->mtd.writev=part_writev;
  66. if(master->lock)
  67. slave->mtd.lock=part_lock;
  68. if(master->unlock)
  69. slave->slave-

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

网站地图

Top