platform s3c2440_led驱动分析<2>

第二天再对个驱动研究的时候发现了里面有个自动创建设备节点的“功能”。实现函数在 s3c_led_probe函数中。


led_device.dev_class = class_create(THIS_MODULE, DEV_NAME); 
    if(IS_ERR(led_device.dev_class)) 
    { 
        printk("%s driver create class failture\n",DEV_NAME); 
        result =  -ENOMEM; 
        goto ERROR; 
    }

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)     
   
    device_create(led_device.dev_class, NULL, MKDEV(dev_major, 0), NULL,DEV_NAME);

#else
    device_create (led_device.dev_class, NULL, MKDEV(dev_major, 0), DEV_NAME);
#endif


想实现自动创建设备节点,即在驱动中实现对mdev的支持。

在驱动初始化的代码里调用class_create(...)为该设备创建一个class,再为每个设备调用device_create(...)( 在2.6较早的内核中用class_device_create)创建对应的设备。 内核中定义的struct class结构体,顾名思义,一个struct class结构体类型变量对应一个类,内核同时提供了class_create(…)函数,可以用它来创建一个类,这个类存放于sys/class下面,一旦创建好了这个类,再调用 device_create(…)函数来在/dev目录下创建相应的设备节点。这样,加载模块的时候,用户空间中的mdev会自动响应 device_create(…)函数,去/sys/class下寻找对应的类从而创建设备节点。

insmod 驱动后

节点也创建好了


如果想创建多个设备节点则需要修改的地方,不过有一点小bug,我不知道什么原因。


for(i=0; i<pdata->nleds; i++)
	{
       device_create(led_device.dev_class, NULL, MKDEV(dev_major, i), NULL, "led%d",i);
	}
当然前面的设备注册号里面的数量也要改成4即pdata->nleds,add_cdev里面也要改成4即pdata->nleds 后面注销函数里面的数量也要改一下。

11月15号更新:(学弟问到了这个宏然后回顾一下)
关于container_of这个宏的使用:
container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址。

例子:申明一个与init_test_struct同类型的test_struct变量,然后通过前者其中一个变量的地址来获取整个init_test_struct结构体的地址;
struct test_struct {  
    int num;  
    char ch;  
    float fl;  
};  
  
int main(void)  
{  
    struct test_struct init_test_struct = { 99, 'C', 59.12 };  
  
    char *char_ptr = &init_test_struct.ch;  
  
    struct test_struct *test_struct = container_of(char_ptr, struct test_struct, ch);  
      
    printf(" test_struct->num = %d\n test_struct->ch = %c\n test_struct->fl = %f\n",   
        test_struct->num, test_struct->ch, test_struct->fl);  
      
    return 0;  
}  

输出结果:

test_struct->num = 99  
test_struct->ch = C  
test_struct->fl = 59.119999 








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值