主题 : 驱动加载后打开失败,请帮忙看看,谢谢! 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 7830
精华: 0
发帖: 45
金钱: 335 两
威望: 183 点
综合积分: 90 分
注册时间: 2009-07-30
最后登录: 2017-09-13
楼主  发表于: 2010-04-23 22:20

 驱动加载后打开失败,请帮忙看看,谢谢!

这几天在写了一个按键驱动,在开发板加载后用cat /proc/device 能看到有我加载的驱动,然后用mknod命令在/dev/下添加设备,也成功了,但是用测试程序测试的时候提示是打开设备失败,请大家帮忙看看,附上程序如下(也可下载附件: keydrv.rar (2 K) 下载次数:5 ),先谢谢大家了。
驱动程序:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
dev_t devno;
struct KEY_DEV
{
 struct cdev cdev;
};
struct KEY_DEV *g_pkey_dev;

static int key_open(struct inode *inode, struct file *filp)
{
 printk(KERN_NOTICE "key opened\n"); 
 return 0;
}
static int key_release(struct inode *inode, struct file *filp)
{
 return 0;
}
static ssize_t key_read(struct file *filp, char *buf, size_t count, loff_t *ppos)
{
 return 0;
}
static int key_ioctl(struct inode *inodep, struct file *filp, unsigned int cmd, unsigned long arg)
{
 return 0;
}
static struct file_operations g_tkey_fops =
{
 .owner   = THIS_MODULE,
 .open    = key_open,  //打开设备
 .release = key_release,  //关闭设备
 .read  = key_read,  //读取按键的键值
 .ioctl  = key_ioctl,  //清除缓冲区
};

static int __devinit key_dev_probe(struct device *dev)
{
 struct platform_device *pdev;
 int err;
 
 if ( dev == NULL )
 {
  printk(KERN_NOTICE "null device pointer!\n");
  return -ENODEV;
 }
 
 pdev = to_platform_device(dev);
 g_pkey_dev = kcalloc(1, sizeof(struct KEY_DEV), GFP_KERNEL);
 if (unlikely(g_pkey_dev == NULL))
 {
  printk(KERN_NOTICE "Can't allocate memory for device!\n");
  return -ENOMEM;
 }
 memset(g_pkey_dev, 0, sizeof(struct KEY_DEV));
 
 cdev_init(&g_pkey_dev->cdev, &g_tkey_fops);
 g_pkey_dev->cdev.owner = THIS_MODULE;
 g_pkey_dev->cdev.ops = &g_tkey_fops;
 
 err = cdev_add(&g_pkey_dev->cdev, devno, 1);
 
 if(err)
 {
  printk(KERN_NOTICE "Error in adding dev");
 }
 return 0;
}
static int __devexit key_dev_remove(struct device * dev)
{
 return 0;
}

static struct device_driver key_driver =
{
    .name     = "Mini2440_Key",
    .bus      = &platform_bus_type,
    .probe    = key_dev_probe,
    .remove   = key_dev_remove,
    .owner    = THIS_MODULE,
};
static int mini2440_key_init(void)
{
 int ret;
 ret = alloc_chrdev_region(&devno, 0, 1,"Mini2440_Key");
 if(ret < 0)
 {
  return ret;
 }
     ret = driver_register(&key_driver);
 if(unlikely(ret))
 {
     printk(KERN_NOTICE "driver registration failed: %d\n", ret);
     return -ret;
 }
 printk(KERN_NOTICE "Init finish!\n");
 return 0;
}
static void key_exit(void)
{
 return;
}
MODULE_AUTHOR("Benson");
MODULE_LICENSE("Dual BSD/GPL");
module_init(mini2440_key_init);
module_exit(key_exit);
级别: 新手上路
UID: 7830
精华: 0
发帖: 45
金钱: 335 两
威望: 183 点
综合积分: 90 分
注册时间: 2009-07-30
最后登录: 2017-09-13
1楼  发表于: 2010-04-23 22:58
没人回我 斑竹老大帮帮忙啊
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
2楼  发表于: 2010-04-24 11:09
但是用测试程序测试的时候提示是打开设备失败

为什么不自己尝试从Linux获得更多的出错信息呢?Linux下有很多方法可以获取错误信息,perror()就是其中最简单的一个。
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."