본문 바로가기

Programming/Linux_Kernel

register_mtd_user() - partition에 융통성 있는 driver 구현하기


안녕하세요.

linux device 를 작성하면서 간혹 특정 mtd partition 에 의존하는 코드를 구현할 필요가 있습니다.
이때 해당 partition num 를 hard coding 한다면,
partition table 이 변경될때마다 쫒아다니면서 일일이 수정해야 겠지요.

하지만 지금 소개시켜 드리는 함수를 잘 이용하시면,
시스템 초기화시는 물론 시스템 운용중에 변경되는 partition 에 대해서도 유동적으로 대처할 수 있는
융통성 있는 device 를 구현할 수 있습니다.

일단 man page 정보를 먼저 봅시다.




NAME

register_mtd_user - register a 'user' of MTD devices.  

SYNOPSIS

"SYNOPSIS"

void register_mtd_user (struct mtd_notifier * new);  

ARGUMENTS

new
pointer to notifier info structure
 

DESCRIPTION

Registers a pair of callbacks function to be called upon addition or removal of MTD devices. Causes the 'add' callback to be immediately invoked for each MTD device currently present in the system.



예 크게 복잡하지 않지요.

mtd partiton 이 추가되거나 제거될때 제가 등록한 함수를 호출해 주게 됩니다.
또한 이미 등록된 partiton 이 있다면 등록 즉시 호출해 줍니다.

아래와 같이 구현하시면 됩니다.

static struct mtd_notifier A_notifier = {
 .add = A_notify_add,
 .remove = A_notify_remove,
};

static void A_notify_add(struct mtd_info *mtd)
{
 
 if (!strcmp(mtd->name, "my_partition")) {
  printk("my_partition partition is %d \n", mtd->index);
  g_mtd_index = mtd->index;
 } else
  return;

 if (mtd->size < (mtd->erasesize * 2)) {
  printk(KERN_ERR "MTD partition %d not big enough for my job\n",
    mtd->index);
  return;
 }

 cxt->mtd = mtd;
 cxt->ready = 1;

 printk(KERN_INFO "Attached to MTD device %d\n", mtd->index);
}


MTD partition 이 add 되었을때 이름을 검색해서 이름이 "my_partition"인 파티션을 찾고 있습니다.
찾았을때는 부가적으로 적당한 size 인지도 검사를 하고 있지요.

이제 이것을 등록만 시켜주면 됩니다.
등록 절차도 간단합니다.

A_driver_probe()
{
register_mtd_user(&A_notifier);
}


예 참 쉽죠잉~

잘 이해가 안가시면 댓글이나 메일 날리시기 바랍니다.