从模型分析到Flash驱动模板(4)

add_mtd_device()
--395-->设置MTD设备的名字
--396-->设置私有数据,将mtd地址藏到device->device_private->void* driver_data
--408-->遍历所有的mtd_notifier,将其添加到通知链

mtd_add_partition()

通过将一个mtd_part对象注册到内核,将mtd_info对象注册到内核,即为一个设备添加一个分区。

537 int mtd_add_partition(struct mtd_info *master, const char *name, 538 long long offset, long long length) 539 { 540 struct mtd_partition part; 541 struct mtd_part *p, *new; 542 uint64_t start, end; 543 int ret = 0; 545 /* the direct offset is expected */ 546 if (offset == MTDPART_OFS_APPEND || 547 offset == MTDPART_OFS_NXTBLK) 548 return -EINVAL; 549 550 if (length == MTDPART_SIZ_FULL) 551 length = master->size - offset; 552 553 if (length <= 0) 554 return -EINVAL; 555 556 part.name = name; 557 part.size = length; 558 part.offset = offset; 559 part.mask_flags = 0; 560 part.ecclayout = NULL; 561 562 new = allocate_partition(master, &part, -1, offset); 563 if (IS_ERR(new)) 564 return PTR_ERR(new); 565 566 start = offset; 567 end = offset + length; 568 569 mutex_lock(&mtd_partitions_mutex); 570 list_for_each_entry(p, &mtd_partitions, list) 571 if (p->master == master) { 572 if ((start >= p->offset) && 573 (start < (p->offset + p->mtd.size))) 574 goto err_inv; 575 576 if ((end >= p->offset) && 577 (end < (p->offset + p->mtd.size))) 578 goto err_inv; 579 } 580 581 list_add(&new->list, &mtd_partitions); 582 mutex_unlock(&mtd_partitions_mutex); 583 584 add_mtd_device(&new->mtd); 585 586 return ret; 591 } add_mtd_partitions()

添加一个分区表到内核,一个MTD设备一个分区表

626 int add_mtd_partitions(struct mtd_info *master, 627 const struct mtd_partition *parts, 628 int nbparts) 629 { 630 struct mtd_part *slave; 631 uint64_t cur_offset = 0; 632 int i; 636 for (i = 0; i < nbparts; i++) { 637 slave = allocate_partition(master, parts + i, i, cur_offset); 642 list_add(&slave->list, &mtd_partitions); 645 add_mtd_device(&slave->mtd); 647 cur_offset = slave->offset + slave->mtd.size; 648 } 649 650 return 0; 651 } 用户空间编程

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8840177b6c5801da96185883ebeb051f.html