lelee007 发表于 2009-12-19 17:20

2.6.26.8 的内核里边,device_create()为什么会失败?

前面那个驱动刚开始没注意条件判断反了,错以为是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
*pgd=00000000
Internal error: Oops: 5 [#1]


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

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

lelee007 发表于 2009-12-19 18:25

周末难道都出去high了么?

accessory 发表于 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怎么来的?可以检查下。

lelee007 发表于 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,后面紧接着的打印没有出来

emmoblin 发表于 2009-12-20 20:58

if(IS_ERR(my_class))

IS_ERR如何实现的?

@sky 发表于 2009-12-20 21:19

"DEVICENAME%d" 把%d去掉试试

lelee007 发表于 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我看网上有人这样用,我就也这样用了

lelee007 发表于 2009-12-20 21:39

原帖由 @sky 于 2009-12-20 21:19 发表 http://linux.chinaunix.net/bbs/images/common/back.gif
"DEVICENAME%d" 把%d去掉试试


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

readkernel 发表于 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:

readkernel 发表于 2009-12-21 09:34

原帖由 lelee007 于 2009-12-20 21:39 发表 http://linux.chinaunix.net/bbs/images/common/back.gif



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


你LED_MINOR怎么定义的?
页: [1] 2
查看完整版本: 2.6.26.8 的内核里边,device_create()为什么会失败?