主题 : 又是一个痛苦的问题,设备文件无法打开,open返回-1。各位路过麻烦讨论讨论,给点思路..急! 复制链接 | 浏览器收藏 | 打印
个人主页:http://www.lkphy.cn/uchome/space.php?do=home
级别: 新手上路
UID: 15936
精华: 0
发帖: 4
金钱: 20 两
威望: 4 点
贡献值: 0 点
综合积分: 8 分
注册时间: 2010-03-11
最后登录: 2011-10-16
楼主  发表于: 2010-05-04 01:11

 又是一个痛苦的问题,设备文件无法打开,open返回-1。各位路过麻烦讨论讨论,给点思路..急!

问题:
     这是一个为我自己的硬件设备(device)写的一个设备驱动程序,驱动实现了read(),write(),open(),release(),ioctrl()函数,并且驱动程序会自动在加载时,在dev下创建设备文件。代码编译通过,警告也没有。然后,本人写了个简单的用户测试程序,其中重点:fd=open("/dev/device", O_WRONLY);然后要求打印fd信息。 通过交叉编译后,把驱动module,*.ko文件和用户测试程序拷到板上,使用insmod能够成功注册设备,并且在/proc/device和/proc/interrupt下能够找到设备、主设备号(动态分配)和设备中断的注册信息(设备初始化时我顺便也注册了中断),到这里看似没问题。但是,当我启动用户程序时,总是无法打开设备,fd返回-1,open设备失败。另外,我进入/dev下,使用cat 查看设备时返回cat: can't open '/dev/device': No such device or address,无语。使用rmmod卸载驱动,也没问题。这样看来,驱动的注册应该是没问题的。但就是不知道为什么设备怎么也打不开。搞了一天,我能想到办法都试过了,还是不行。/dev下设备文件权限是644,不过我把它改为777也是不行。在驱动中,open函数现在我注释掉很多东西,只剩下一个return 0,看书本上的简单设备驱动的例子,都是使用一个return 0,好像就没问题了。不过即使我改成这样,还是同样问题。有点崩溃啦。希望哪位仁兄帮忙看看,到底还会有什么原因会导致open失败,我现在有点束手无策啦。谢谢!
    本以为,在书本能有所介绍,不过,那些书上说的代码明显调试不会通过,但是,他们就直接说,编译通过,打开成功啦。真不知道他们是怎么想的。实在无语!希望这里能有好心高人相助。问题有点急,希望大家能顶顶!...thanks!...
[ 此帖被lkrocksthone在2010-05-05 01:10重新编辑 ]
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
贡献值: 71 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
1楼  发表于: 2010-05-04 09:44
说那么多还不如把你的驱动和测试程序贴出来。
"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."
个人主页:http://www.lkphy.cn/uchome/space.php?do=home
级别: 新手上路
UID: 15936
精华: 0
发帖: 4
金钱: 20 两
威望: 4 点
贡献值: 0 点
综合积分: 8 分
注册时间: 2010-03-11
最后登录: 2011-10-16
2楼  发表于: 2010-05-04 14:53

 回 1楼(kasim) 的帖子

又好,驱动太长,只贴关键位,如下:
......
static int ccd_stepper_open(struct inode *inode, struct file *file);
static int ccd_stepper_release(struct inode *inode, struct file *file);
static ssize_t ccd_stepper_read(struct file *filp, char *buff, size_t count, loff_t *offp);
static ssize_t ccd_stepper_write(struct file *filp, const char __user *buff, size_t count, loff_t *offp);
static int ccd_stepper_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long msg);
static irqreturn_t ccd_stepper_reading(int irq, void *dev_id);

static struct file_operations ccd_stepper_fops= {
        .owner  =    THIS_MODULE,
        .read   =    ccd_stepper_read,
        .write  =    ccd_stepper_write,
        .ioctl  =    ccd_stepper_ioctl,
        .open     =    ccd_stepper_open,
        .release  =    ccd_stepper_release,
};

struct ccd_stepper_dev {
    struct ccd_stepper_qset *data;
    int quantum;
    int qset;
    unsigned long size;
    unsigned int access_key;
    struct semaphore sem;
    struct cdev cdev;
};

struct ccd_stepper_dev devs;

static void ccd_stepper_setup_dev(struct ccd_stepper_dev *dev, int minor)
{
    int err, devno = MKDEV(CCD_STEPPER_MAJOR, minor);
    cdev_init(&dev->cdev, &ccd_stepper_fops);
    dev->cdev.owner = THIS_MODULE;
    dev->cdev.ops = &ccd_stepper_fops;
    err = cdev_add(&dev->cdev,devno,1);
    if (err) {
        printk(KERN_NOTICE "Error %d adding ccd_stepper %d \n",err,minor);
        printk(KERN_WARNING "Error: unable to add ccd_stepper_dev \n");
    }
    printk(KERN_WARNING "OK: Be able to add cdev! \n");
}

static int __init ccd_stepper_init(void)
{
    int result;
    struct class *ccd_stepper_class;
    dev_t dev = MKDEV(CCD_STEPPER_MAJOR, CCD_STEPPER_MINOR);
    if (CCD_STEPPER_MAJOR) {
        result = register_chrdev_region(dev,1,DEVICE_NAME);
    }
    else {
        result = alloc_chrdev_region(&dev,CCD_STEPPER_MINOR,1,DEVICE_NAME);
        CCD_STEPPER_MAJOR=MAJOR(dev);
    }
    if (result < 0) {
        printk(KERN_WARNING "CCD_STEPPER: unable to registe registe major %d !\n", CCD_STEPPER_MAJOR);
        return result;
    }
    ccd_stepper_setup_dev(&devs,CCD_STEPPER_MINOR);
    printk(KERN_WARNING "Successfully registed! \nThe major of ccd_stepper device is %d !\n", CCD_STEPPER_MAJOR);
    ccd_stepper_class = class_create(THIS_MODULE,"ccd_stepper");
    device_create(ccd_stepper_class,NULL,MKDEV(CCD_STEPPER_MAJOR, 0),NULL,"ccd_stepper");
    printk(KERN_WARNING "Device ccd_stepper is automatically created!\n");
    return 0;
}
static int ccd_stepper_open(struct inode *inode, struct file *file)
{
    return 0;
}
.....
下面是我的测试程序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <fcntl.h>
//#include <sys/stat.h>
int main (int argc, char **argv)
{
    int d;
    int p;
    int fd;
    int i;
    int j;
    if (argc !=3 || sscanf(argv[1], "%d", &d)!=1 || sscanf(argv[2],"%d",&p) !=1 || d < 0 || d > 2 || d < 0 || p > 900) {
    fprintf(stderr, "Usage: stpmov 0(left)|1(right)|2(down) p(distance)\n");
    exit(1);
    }
    fd = open("/dev/ccd_stepper",O_WRONLY);
    printf("fd = %d \n",fd);
    if (fd < 0) {
        fprintf(stderr, "Open device ccd_stepper error!\n");
        exit(1);
    }
    else {
        printf("Open device ccd_stepper success!\n");
        printf("fd = %d \n",fd);
    }
....
    close(fd);
    return 0;
}
这是今天重新修改了的代码,open返回不是-1啦,不过出现了段错误,也不是什么原因?结果如下:
[root@SmartDPY /root]# insmod ccd_stepper.ko
OK: Be able to adding cdev!
Successfully registed!
The major of ccd_stepper device is 252 !
Device ccd_stepper is automatically created!
运行测试程序如下(很多,开始部分没有复制到):
......
5ec0:                                     c3a56300 00000014 c3a85f04 c3a85ee8
5ee0: c009507c bf000088 00000003 c3a56300 00000014 c3a56300 c3a85f7c c3a85f08
5f00: c0095330 c0095014 4001f000 00000008 c39d2100 c3a85f78 c3a84000 00000000
5f20: c3a85f54 c3a85f30 c014b1a0 c014aee0 c39d2100 4001f000 c3a85f78 00000004
5f40: c0029008 c3a84000 c3a85f74 c3a85f58 c008950c 00000003 00000014 00000001
5f60: c3a56300 c0029008 c3a84000 00000000 c3a85fa4 c3a85f80 c0095664 c00950bc
5f80: ffffffff 00000000 000086b0 00000000 00008440 00000036 00000000 c3a85fa8
5fa0: c0028e60 c0095634 000086b0 00000000 00000003 00000001 00000014 00000001
5fc0: 000086b0 00000000 00008440 00000036 00000000 00000000 40025000 be98fb84
5fe0: 00000000 be98fb60 00008630 400e4cac 60000010 00000003 00000000 00000000
Backtrace:
[<bf000078>] (ccd_stepper_ioctl+0x0/0xa8 [ccd_stepper]) from [<c009507c>] (vfs_i
octl+0x78/0x80)
r5:00000014 r4:c3a56300
[<c0095004>] (vfs_ioctl+0x0/0x80) from [<c0095330>] (do_vfs_ioctl+0x284/0x578)
r7:c3a56300 r6:00000014 r5:c3a56300 r4:00000003
[<c00950ac>] (do_vfs_ioctl+0x0/0x578) from [<c0095664>] (sys_ioctl+0x40/0x68)
[<c0095624>] (sys_ioctl+0x0/0x68) from [<c0028e60>] (ret_fast_syscall+0x0/0x2c)
r7:00000036 r6:00008440 r5:00000000 r4:000086b0
Code: e59f202c e3a03412 e3a0000d e3a01000 (e1c320b0)
---[ end trace c9a7dd5c6bfd5e9b ]---
Trying to free already-free IRQ 16
fd = 3                  // 这里好像跳过了本应该有点的输出
Segmentation fault

好像是个普通问题,不过本人没经验,搞不懂...麻烦看看,是哪里有问题..谢谢!
个人主页:http://www.lkphy.cn/uchome/space.php?do=home
级别: 新手上路
UID: 15936
精华: 0
发帖: 4
金钱: 20 两
威望: 4 点
贡献值: 0 点
综合积分: 8 分
注册时间: 2010-03-11
最后登录: 2011-10-16
3楼  发表于: 2010-05-05 01:09
ok,问题解决,指针没有管好,还是比较明显的...虽然没人回帖,不过还是做个交代吧。感谢一楼的关注。谢谢!...松了口气...
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
贡献值: 71 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
4楼  发表于: 2010-05-05 11:55
不客气,不过还是希望你下次能发点更有价值的帖子。
"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."
级别: 新手上路
UID: 33601
精华: 0
发帖: 17
金钱: 85 两
威望: 17 点
贡献值: 0 点
综合积分: 34 分
注册时间: 2010-12-03
最后登录: 2011-12-01
5楼  发表于: 2011-08-10 19:19
老兄,你的这个问题是怎么解决的呢?
级别: 新手上路
UID: 42941
精华: 0
发帖: 13
金钱: 65 两
威望: 13 点
贡献值: 0 点
综合积分: 26 分
注册时间: 2011-04-12
最后登录: 2014-07-26
6楼  发表于: 2011-10-19 20:54
遇到相同的情况,楼主你是怎么解决的啊??!请指教啊
级别: 新手上路
UID: 60266
精华: 0
发帖: 3
金钱: 15 两
威望: 3 点
贡献值: 0 点
综合积分: 6 分
注册时间: 2011-12-07
最后登录: 2012-09-11
7楼  发表于: 2011-12-08 11:00
求答案。。。