免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 10154 | 回复: 13
打印 上一主题 下一主题

2.6.26.8 的内核里边,device_create()为什么会失败? [复制链接]

论坛徽章:
3
卯兔
日期:2013-08-26 22:14:57未羊
日期:2013-09-08 19:16:05未羊
日期:2014-10-23 10:34:12
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-19 17:20 |只看该作者 |倒序浏览
前面那个驱动刚开始没注意条件判断反了,错以为是kmalloc没成功,下午调试发现是device_create失败了


        printk("pre-create device_class!!!\n");
        //device_create(my_class, NULL, dev, NULL, DEVICE_NAME "%d", LED_MINOR );//dev为开始处MKDEV产生的,其类型为dev_t
        device_create(my_class, NULL, dev, NULL, "DEVICENAME%d" );// , LED_MINOR );
        printk("finished to create device_class!!!\n");

device_create,第一个printk打印了,然后内核就挂了:
Unable to handle kernel NULL pointer dereference at virtual address 00000000
pgd = c0004000
[00000000] *pgd=00000000
Internal error: Oops: 5 [#1]


请教一下大大们,这个device_create到底该怎么用哇?

看了下她的接口,感觉没有用错的哇,为什么会create失败了?

论坛徽章:
3
卯兔
日期:2013-08-26 22:14:57未羊
日期:2013-09-08 19:16:05未羊
日期:2014-10-23 10:34:12
2 [报告]
发表于 2009-12-19 18:25 |只看该作者
周末难道都出去high了么?

论坛徽章:
0
3 [报告]
发表于 2009-12-20 00:33 |只看该作者
Note

the struct class passed to this function must have previously been created with a call to class_create.

LZ的 MY_CLASS怎么来的?可以检查下。

论坛徽章:
3
卯兔
日期:2013-08-26 22:14:57未羊
日期:2013-09-08 19:16:05未羊
日期:2014-10-23 10:34:12
4 [报告]
发表于 2009-12-20 10:03 |只看该作者
#include <linux/device.h> /*  struct class *my_class;

struct class *my_class;

my_class = class_create(THIS_MODULE , "led-class");

if(IS_ERR(my_class))
{
      printk("failed to creat class\n ");
      return -1;
}

这个地方class_create成功了,那个if里边的没有打印,更没有返回

接着往下就是device_create,之前还打印了条消息,然后就挂在device_create,后面紧接着的打印没有出来

论坛徽章:
0
5 [报告]
发表于 2009-12-20 20:58 |只看该作者
if(IS_ERR(my_class))

IS_ERR如何实现的?

论坛徽章:
0
6 [报告]
发表于 2009-12-20 21:19 |只看该作者
"DEVICENAME%d" 把%d去掉试试

论坛徽章:
3
卯兔
日期:2013-08-26 22:14:57未羊
日期:2013-09-08 19:16:05未羊
日期:2014-10-23 10:34:12
7 [报告]
发表于 2009-12-20 21:39 |只看该作者
include/linux/err.h

static inline long IS_ERR(const void *ptr)
{
        return IS_ERR_VALUE((unsigned long)ptr);
}

IS_ERR我看网上有人这样用,我就也这样用了

论坛徽章:
3
卯兔
日期:2013-08-26 22:14:57未羊
日期:2013-09-08 19:16:05未羊
日期:2014-10-23 10:34:12
8 [报告]
发表于 2009-12-20 21:39 |只看该作者
原帖由 @sky 于 2009-12-20 21:19 发表
"DEVICENAME%d" 把%d去掉试试



%d去掉也试过,同样会挂在device_create

论坛徽章:
0
9 [报告]
发表于 2009-12-21 09:33 |只看该作者
device_create(my_class, NULL, dev, NULL, "DEVICENAME%d" );// , LED_MINOR );

:em12:

你第五个参数"DEVICENAME%d"是个格式化的字符串,既然带了%d,后面的参数就不可省略。
device_create(my_class, NULL, dev, NULL, "DEVICENAME%d" , LED_MINOR);
与printf类似..
你省略掉后,device_create里面调用的vsnprintf不出错才怪了.内核当然认为%d对应的变量地址是NULL
Unable to handle kernel NULL pointer dereference at virtual address 00000000

你甚至可以这样写这个函数

char * str = "abcde";
int inta = 10;
device_create(my_class, NULL, dev, NULL, "DEVICENAME%d%s%d", LED_MINOR, str, inta);

记住:格式化串必须对应参数.
:em12:

论坛徽章:
0
10 [报告]
发表于 2009-12-21 09:34 |只看该作者
原帖由 lelee007 于 2009-12-20 21:39 发表



%d去掉也试过,同样会挂在device_create



你LED_MINOR怎么定义的?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP