主题 : 测试测序中的open无法打开设备问件 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 42941
精华: 0
发帖: 13
金钱: 65 两
威望: 13 点
综合积分: 26 分
注册时间: 2011-04-12
最后登录: 2014-07-26
楼主  发表于: 2011-10-19 21:09

 测试测序中的open无法打开设备问件

dev目录下有我创建的设备文件,/proc/devices里能看见。sys/class中也看得见我创建的类,驱动加载成功,make驱动时没有错误。我的驱动和源码如下:
复制代码
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/miscdevice.h>
  6. #include <linux/delay.h>
  7. #include <linux/device.h>
  8. #include <linux/cdev.h>
  9. #include <asm/irq.h>
  10. #include <mach/gpio.h>
  11. #include <plat/regs-gpio.h>
  12. #include <plat/gpio-cfg.h>
  13. #include <mach/hardware.h>
  14. #include <linux/io.h>
  15. #include <asm/uaccess.h>
  16. MODULE_LICENSE("Dual BSD/GPL");
  17. MODULE_DESCRIPTION("OK6410_V 0.1");
  18. dev_t devno;
  19. static struct cdev cdev_first;
  20. static struct class *firstdrv_class;
  21. volatile unsigned long *gpmcon = NULL;
  22. volatile unsigned long *gpmdat = NULL;
  23. static int first_drv_open(struct inode *inode, struct file *file)
  24. {
  25.     /* 配置gpm1-4引脚为输出*/
  26.     *gpmcon &= 0xffff0000;
  27.     *gpmcon |= 0x00001111;
  28.      return 0;
  29.     
  30. }
  31. static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  32. {
  33.     int val;
  34.     int r;
  35.     //printk("first_drv_write\n");
  36.     r=copy_from_user(&val, buf, count); //    copy_to_user();
  37.     if (val == 1)
  38.     {
  39.         // 点灯
  40.         *gpmdat &= 0xfffffff0;
  41.     }
  42.     else
  43.     {
  44.         // 灭灯
  45.         *gpmdat |= 0xf;
  46.     }
  47.     
  48.     return 0;
  49. }
  50. static struct file_operations first_drv_fops ={
  51.     .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
  52.     .open   =   first_drv_open,    
  53.     .write  =   first_drv_write,      
  54. };
  55. static int __init first_drv_init(void)
  56. {
  57.     
  58.     int result;
  59.     
  60.     gpmcon = (volatile unsigned long *)ioremap(0x7f008820, 16);
  61.     gpmdat = gpmcon + 1;
  62.     
  63.     printk(KERN_NOTICE "MODULE IS INITING\n");
  64.         
  65.     result=alloc_chrdev_region(&devno,0,1,"myled");
  66.     if(result<0)
  67.     printk(KERN_NOTICE "region wrong!");
  68.     cdev_init(&cdev_first,&first_drv_fops);
  69.     cdev_first.owner=THIS_MODULE;
  70.     cdev_first.ops=&first_drv_fops;
  71.     cdev_add(&cdev_first,devno,1);
  72.     
  73.     
  74.     firstdrv_class = class_create(THIS_MODULE, "mydrv");  //sys/class
  75.     device_create(firstdrv_class, NULL, devno, NULL, "myled"); /* /dev/xyz */
  76.     return 0;
  77. }
  78. static void __exit first_drv_exit(void)
  79. {
  80.     
  81.     cdev_del(&cdev_first);
  82.     unregister_chrdev_region(devno,1);
  83.     
  84.     device_destroy(firstdrv_class,devno);
  85.     class_destroy(firstdrv_class);
  86.     
  87.     iounmap(gpmcon);
  88.     printk(KERN_NOTICE "my driver exit\n");
  89. }
  90. module_init(first_drv_init);
  91. module_exit(first_drv_exit);


测试程序:

复制代码
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. /* firstdrvtest on
  6.   * firstdrvtest off
  7.   */
  8. int main(int argc, char **argv)
  9. {
  10.     int fd;
  11.     int val = 1;
  12.     char *filename="/dev/myled";
  13.     fd = open("filename", O_RDWR);
  14.     if (fd < 0)
  15.     {
  16.         printf("can't open!\n");
  17.     }
  18.     if (argc != 2)
  19.     {
  20.         printf("Usage :\n");
  21.         printf("%s <on|off>\n", argv[0]);
  22.         return 0;
  23.     }
  24.     if (strcmp(argv[1], "on") == 0)
  25.     {
  26.         val  = 1;
  27.     }
  28.     else
  29.     {
  30.         val = 0;
  31.     }
  32.     
  33.     write(fd, &val, 4);
  34.     return 0;
  35. }



请高手解答,非常感谢对新手的帮助。
^很多问题的背后都是简单的原因......
级别: 荣誉会员
UID: 34780
精华: 0
发帖: 1219
金钱: 6230 两
威望: 1246 点
综合积分: 2438 分
注册时间: 2010-12-21
最后登录: 2017-09-18
1楼  发表于: 2011-10-20 09:37
下面这一行错了
fd = open("filename", O_RDWR);
级别: 新手上路
UID: 42941
精华: 0
发帖: 13
金钱: 65 两
威望: 13 点
综合积分: 26 分
注册时间: 2011-04-12
最后登录: 2014-07-26
2楼  发表于: 2011-10-20 11:05

 回 1楼(911gt3) 的帖子

请问我应该怎么改啊?谢谢你的回答了。
我改成fd = open("/dev/myled", O_RDWR);
或fd = open(filename, O_RDWR);
错误都一样啊。
^很多问题的背后都是简单的原因......
级别: 荣誉会员
UID: 34780
精华: 0
发帖: 1219
金钱: 6230 两
威望: 1246 点
综合积分: 2438 分
注册时间: 2010-12-21
最后登录: 2017-09-18
3楼  发表于: 2011-10-20 11:48
1) 加载你的驱动后, 请检查一下 /dev/myled 这个文件是否存在, 不存在的话肯定有问题
2) 你也使用 strerror 这个函数获得open失败时具体的错误原因