我正在为ARM平台上的GPIO引脚编写驱动程序。 我的司机工作正常,直到我避免了这个问题 现在通过手动mknod'ing设备文件。
我的初始化代码:
static int __init gpio_init (void)
{
void *ptr_error;
if (register_chrdev(249, "gpio_device", &fops) < 0){
printk(KERN_INFO "Registering device failed\n");
return -EINVAL;
}
if ((device_class = class_create(THIS_MODULE, "gpio_device"))
== NULL){
unregister_chrdev_region(DEV_T, 1);
printk(KERN_INFO "Class creation failed\n");
return -EINVAL;
}
ptr_error = device_create(device_class, NULL, DEV_T, NULL, "gpio_device");
if (IS_ERR(ptr_error)){
class_destroy(device_class);
unregister_chrdev_region(DEV_T, 1);
printk(KERN_INFO "Device creation failed\n");
return -EINVAL;
}
cdev_init(&c_dev, &fops);
if (cdev_add(&c_dev, DEV_T, 1)){
device_destroy(device_class, DEV_T);
class_destroy(device_class);
unregister_chrdev_region(DEV_T, 1);
printk(KERN_INFO "Cdev add failed\n");
return -EINVAL;
}
printk(KERN_INFO "Guten tag, GPIO driver initialized\n");
return SUCCESS;
void *ptr_error;
if (register_chrdev(249, "gpio_device", &fops) < 0){
printk(KERN_INFO "Registering device failed\n");
return -EINVAL;
}
if ((device_class = class_create(THIS_MODULE, "gpio_device"))
== NULL){
unregister_chrdev_region(DEV_T, 1);
printk(KERN_INFO "Class creation failed\n");
return -EINVAL;
}
ptr_error = device_create(device_class, NULL, DEV_T, NULL, "gpio_device");
if (IS_ERR(ptr_error)){
class_destroy(device_class);
unregister_chrdev_region(DEV_T, 1);
printk(KERN_INFO "Device creation failed\n");
return -EINVAL;
}
cdev_init(&c_dev, &fops);
if (cdev_add(&c_dev, DEV_T, 1)){
device_destroy(device_class, DEV_T);
class_destroy(device_class);
unregister_chrdev_region(DEV_T, 1);
printk(KERN_INFO "Cdev add failed\n");
return -EINVAL;
}
printk(KERN_INFO "Guten tag, GPIO driver initialized\n");
return SUCCESS;
除了在/ dev中没有创建文件“gpio_device”外,它没有错误运行。
我正在为ARM编译内核2.6.39.4。 (使用arm-linux-gcc)
据我了解,device_create 应该创建/ dev文件。
答案 0 :(得分:3)
我尝试运行你的代码并发现了一些错误:
当您注册register_chrdev()
时,您应该注销unregister_chrdev()
。 unregister_chrdev_region()
用于取消注册使用alloc_chrdev_region()
或register_chrdev_region()
完成的注册。
对register_chrdev()
的调用会为给定的专业注册次要编号0-255,并为每个编号设置默认的cdev
结构,因此,您无需处理{ {1}}&amp; cdev_init()
。
您应该使用cdev_add()
&amp; IS_ERR
&amp; PTR_ERR
class_create()
device_create()
将使用强制转换将返回指针转换为错误代码。
您可以在此处阅读更多内容:Char Device Registration。
应用我提到的修改后,PTR_ERR
创建时没有/dev/gpio_device
:
mknod
答案 1 :(得分:1)
我只知道这里发生了什么。 我们正在使用BuildRoot来创建我们的自定义 linux,结果我们编译了 udev设备文件管理系统。
这就是为什么这不起作用的原因。