关闭

字符设备驱动开发

335人阅读 评论(0) 收藏 举报
分类:
 

1、外部设备映射到字符设备

/* 宏定义 */

#define CHAR_MAJOR 90

#define CHAR_DEV_NAME "ssd_chr"

 

add_mtd_device(struct mtd_dev *mtd_dev,int dev_idx)
{

     int idx;

    int dev_id;

   struct mtd_infor mtd_info = mtd_dev->mtd;

    spin_lock(mtd_table_lock);

     for(idx = 0;idx < MTD_DEV_COUNT;idx++)

   {

          if(NULL == mtd_table[idx])

          {

                      mtd_table[idx] = (struct mtd_dev)kmalloc(sizeof(struct mtd_dev),GFP_KERNEL);

                      memset(mtd_table[idx],0,sizeof(struct mtd_dev));

                      dev_id = MKDEV(MTD_CHAR_MAJOR,i * 2);

                       memcpy(mtd_table[idx],sizeof(struct mtd_dev));

                       mtd_table[idx] ->mtd.index = i ;

                       mtd_table[idx]->mtd.usecount++;

                       mtd_table[idx]->mtd.dev.class = g_mtdclass;

                       mtd_table[idx]-.>mtd.dev.devt = j;

                       dev_set_name(mtd_table[idx]->mtd_dev,"mtd%d",i);

                       dev_set_drvdata(mtd_table[idx]->dev,&mtd_info);

                       if(dev_id)

                    {

                                device_create(&mtd_table[idx]->dev.parent,j,NULL,"mtd_%d",i;

                     }

                  spin_unlock(mtd_table_lock);

                 __module_get(THIS_MODULE);

              return 0;

            }

     

   }

  spin_unlock(mtd_table_lock);

}

2、字符设备的注册

 
/* 字符设备IO操作接口 */
static struct file_operations g_chr_fops = 
{
	.open = chr_open,
	.close = chr_close,
	.ioctl = chr_ioctl
};
 
static struct class g_mtd_class = 
{
	.name = "mtd“,
	.ower = THIS_MODULE,
	.suspend = mtd_suspend,
	.resume = mtd_resume
};
 
/* 模块初始化 */
static int __init module_charDrv_init()
{
	int idx; 
	if(register_chrdev(CHAR_MAJOR,CHAR_DEV_NAME,g_chr_fops))
	{
		printk(KERN_ERR“register char device failed !\n”);
		return -1;
	}
	
	/* MTD设备表初始化 */
	for(idx = 0;idx < MTD_DEV_COUNT)
	{
		g_mtd_table[idx] = NULL;
		printk(“register char device failed !\n”);
	}
	
	/* 注册MTD设备Class */
	if(class_register(&g_mtd_class))
	{
		printk(KERN_ERR“register class failed !\n”);
		return -1;
	}
	
	printk(KERN_INFO"register char device success.\n");
}
 


 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4169次
    • 积分:155
    • 等级:
    • 排名:千里之外
    • 原创:9篇
    • 转载:3篇
    • 译文:1篇
    • 评论:1条
    文章分类
    最新评论