主题 : 求救,关于mini2440USB万能驱动的问题!! 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 7012
精华: 0
发帖: 17
金钱: 160 两
威望: 82 点
综合积分: 34 分
注册时间: 2009-06-28
最后登录: 2011-09-24
楼主  发表于: 2009-10-09 08:57

 求救,关于mini2440USB万能驱动的问题!!

管理提醒: 本帖被 kasim 从 Qtopia技术交流专区 移动到本区(2009-10-09)
最近放假在研究V4l采集图片,我用V4l2的函数 if (-1 == xioctl (fd, VIDIOC_QUERYCAP, &cap)) {
                if (EINVAL == errno) {
                        fprintf (stderr, "%s is no V4L2 device\n",
                                 dev_name);
                        exit (EXIT_FAILURE);
                } else {
                        errno_exit ("VIDIOC_QUERYCAP");
                }
        }
结果什么都没返回,比如cap.driver和cap.capabilities。
然后我用V4l测试,
int v4l_open(char *dev, v4l_device *vd)
{
   if (!dev)
      dev = DEFAULT_DEVICE;

   if ((vd->fd = open(dev, O_RDWR)) < 0) {
      perror("v4l_open:");
      return -1;
   }

   if (v4l_get_capability(vd))
      return -1;

   if (v4l_get_picture(vd))
      return -1;

   return 0;
}

这些完全没问题,可以返回设备信息,但是下面这个函数一直得不到返回
int v4l_get_mbuf(v4l_device *vd)
{
   if (ioctl(vd->fd, VIDIOCGMBUF, &(vd->mbuf)) < 0) {      perror("v4l_get_mbuf");
      return -1;
   }
   return 0;
}
现在我搞不清到底万能驱动是支持v4l2还是v4l了,希望高手帮帮忙,万分感谢。
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
1楼  发表于: 2009-10-09 10:38
这是Mini2440默认配置(config_mini2440_n35)中关于V4L的配置:
复制代码
  1. #
  2. # Multimedia devices
  3. #
  4. #
  5. # Multimedia core support
  6. #
  7. CONFIG_VIDEO_DEV=y
  8. CONFIG_VIDEO_V4L2_COMMON=y
  9. # CONFIG_VIDEO_ALLOW_V4L1 is not set
  10. CONFIG_VIDEO_V4L1_COMPAT=y
  11. # CONFIG_DVB_CORE is not set
  12. CONFIG_VIDEO_MEDIA=y

可见采用的是V4L2兼容V4L接口。在这样的设置下v4l_get_capability()和VIDIOC_QUERYCAP事实上是一样的。我想你的问题取决于USB摄像头的驱动支持哪种接口。
"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: 7012
精华: 0
发帖: 17
金钱: 160 两
威望: 82 点
综合积分: 34 分
注册时间: 2009-06-28
最后登录: 2011-09-24
2楼  发表于: 2009-10-09 13:13
(kasim: 贴代码的时候用
复制代码
  1. [/code]标记)
  2. 谢谢版主的解释,明白了不少,刚才又试了一个v4l2的例子;
  3. 把代码贴一下吧:
  4. [code]
  5. /* Simple Video4Linux image grabber. */
  6. /*
  7. *    Video4Linux Driver Test/Example Framegrabbing Program
  8. *
  9. *    Compile with:
  10. *        gcc -s -Wall -Wstrict-prototypes v4lgrab.c -o v4lgrab
  11. *    Use as:
  12. *        v4lgrab >image.ppm
  13. *
  14. *    Copyright (C) 1998-05-03, Phil Blundell <[email]philb@gnu.org[/email]>
  15. *    Copied from [url]http://www.tazenda.demon.co.uk/phil/vgrabber.c[/url]
  16. *    with minor modifications (Dave Forrest, [email]drf5n@virginia.edu[/email]).
  17. *
  18. *
  19. *    For some cameras you may need to pre-load libv4l to perform
  20. *    the necessary decompression, e.g.:
  21. *[color=#FF0000]
  22. *    export LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so
  23. *    ./v4lgrab >image.ppm
  24. *[/color] *    see [url]http://hansdegoede.livejournal.com/3636.html[/url] for details.
  25. *
  26. */
  27. #include <unistd.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <stdio.h>
  32. #include <sys/ioctl.h>
  33. #include <stdlib.h>
  34. #include <linux/types.h>
  35. #include <linux/videodev.h>
  36. #define VIDEO_DEV "/dev/video0"
  37. /* Stole this from tvset.c */
  38. #define READ_VIDEO_PIXEL(buf, format, depth, r, g, b)                   \
  39. {                                                                       \
  40.     switch (format)                                                 \
  41.     {                                                               \
  42.         case VIDEO_PALETTE_GREY:                                \
  43.             switch (depth)                                  \
  44.             {                                               \
  45.                 case 4:                                 \
  46.                 case 6:                                 \
  47.                 case 8:                                 \
  48.                     (r) = (g) = (b) = (*buf++ << 8);\
  49.                     break;                          \
  50.                                     \
  51.                 case 16:                                \
  52.                     (r) = (g) = (b) =               \
  53.                         *((unsigned short *) buf);      \
  54.                     buf += 2;                       \
  55.                     break;                          \
  56.             }                                               \
  57.             break;                                          \
  58.                                     \
  59.                                     \
  60.         case VIDEO_PALETTE_RGB565:                              \
  61.         {                                                       \
  62.             unsigned short tmp = *(unsigned short *)buf;    \
  63.             (r) = tmp&0xF800;                               \
  64.             (g) = (tmp<<5)&0xFC00;                          \
  65.             (b) = (tmp<<11)&0xF800;                         \
  66.             buf += 2;                                       \
  67.         }                                                       \
  68.         break;                                                  \
  69.                                     \
  70.         case VIDEO_PALETTE_RGB555:                              \
  71.             (r) = (buf[0]&0xF8)<<8;                         \
  72.             (g) = ((buf[0] << 5 | buf[1] >> 3)&0xF8)<<8;    \
  73.             (b) = ((buf[1] << 2 ) & 0xF8)<<8;               \
  74.             buf += 2;                                       \
  75.             break;                                          \
  76.                                     \
  77.         case VIDEO_PALETTE_RGB24:                               \
  78.             (r) = buf[0] << 8; (g) = buf[1] << 8;           \
  79.             (b) = buf[2] << 8;                              \
  80.             buf += 3;                                       \
  81.             break;                                          \
  82.                                     \
  83.         default:                                                \
  84.             fprintf(stderr,                                 \
  85.                 "Format %d not yet supported\n",        \
  86.                 format);                                \
  87.     }                                                               \
  88. }
  89. int get_brightness_adj(unsigned char *image, long size, int *brightness) {
  90.   long i, tot = 0;
  91.   for (i=0;i<size*3;i++)
  92.     tot += image[i];
  93.   *brightness = (128 - tot/(size*3))/3;
  94.   return !((tot/(size*3)) >= 126 && (tot/(size*3)) <= 130);
  95. }
  96. int main(int argc, char ** argv)
  97. {
  98.   int fd = open(VIDEO_DEV, O_RDONLY), f;
  99.   struct video_capability cap;
  100.   struct video_window win;
  101.   struct video_picture vpic;
  102.   unsigned char *buffer, *src;
  103.   int bpp = 24, r, g, b;
  104.   unsigned int i, src_depth;
  105.   if (fd < 0) {
  106.     perror(VIDEO_DEV);
  107.     exit(1);
  108.   }
  109.   if (ioctl(fd, VIDIOCGCAP, &cap) < 0) {
  110.     perror("VIDIOGCAP");
  111.     fprintf(stderr, "(" VIDEO_DEV " not a video4linux device?)\n");
  112.     close(fd);
  113.     exit(1);
  114.   }
  115.   if (ioctl(fd, VIDIOCGWIN, &win) < 0) {
  116.     perror("VIDIOCGWIN");
  117.     close(fd);
  118.     exit(1);
  119.   }
  120.   if (ioctl(fd, VIDIOCGPICT, &vpic) < 0) {
  121.     perror("VIDIOCGPICT");
  122.     close(fd);
  123.     exit(1);
  124.   }
  125.   if (cap.type & VID_TYPE_MONOCHROME) {
  126.     vpic.depth=8;
  127.     vpic.palette=VIDEO_PALETTE_GREY;    /* 8bit grey */
  128.     if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
  129.       vpic.depth=6;
  130.       if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
  131.     vpic.depth=4;
  132.     if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
  133.       fprintf(stderr, "Unable to find a supported capture format.\n");
  134.       close(fd);
  135.       exit(1);
  136.     }
  137.       }
  138.     }
  139.   } else {
  140.     vpic.depth=24;
  141.     vpic.palette=VIDEO_PALETTE_RGB24;
  142.     if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
  143.       vpic.palette=VIDEO_PALETTE_RGB565;
  144.       vpic.depth=16;
  145.       if(ioctl(fd, VIDIOCSPICT, &vpic)==-1) {
  146.     vpic.palette=VIDEO_PALETTE_RGB555;
  147.     vpic.depth=15;
  148.     if(ioctl(fd, VIDIOCSPICT, &vpic)==-1) {
  149.       fprintf(stderr, "Unable to find a supported capture format.\n");
  150.       return -1;
  151.     }
  152.       }
  153.     }
  154.   }
  155.   buffer = malloc(win.width * win.height * bpp);
  156.   if (!buffer) {
  157.     fprintf(stderr, "Out of memory.\n");
  158.     exit(1);
  159.   }
  160.   do {
  161.     int newbright;
  162.     read(fd, buffer, win.width * win.height * bpp);
  163.     f = get_brightness_adj(buffer, win.width * win.height, &newbright);
  164.     if (f) {
  165.       vpic.brightness += (newbright << 8);
  166.       if(ioctl(fd, VIDIOCSPICT, &vpic)==-1) {
  167.     perror("VIDIOSPICT");
  168.     break;
  169.       }
  170.     }
  171.   } while (f);
  172.   fprintf(stdout, "P6\n%d %d 255\n", win.width, win.height);
  173.   src = buffer;
  174.   for (i = 0; i < win.width * win.height; i++) {
  175.     READ_VIDEO_PIXEL(src, vpic.palette, src_depth, r, g, b);
  176.     fputc(r>>8, stdout);
  177.     fputc(g>>8, stdout);
  178.     fputc(b>>8, stdout);
  179.   }
  180.   close(fd);
  181.   return 0;
  182. }

出现错误如下,也找不到上面红色部分说的export LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so
[root@FriendlyARM bin]# ./v4lgrab   >image.ppm
Unable to find a supported capture format.
VIDIOSPICT: Invalid argument
[root@FriendlyARM bin]#

实在没啥头绪了,友善附带的USB摄像头程序可以正常工作,希望友善可以点拔一下,不给源码给点提示也行,指明一下方向,谢啦。
[ 此帖被kasim在2009-10-09 15:47重新编辑 ]
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
3楼  发表于: 2009-10-09 15:18
当设备驱动的ioctl出错时,第一件事情是把errno打印出来。
[ 此帖被kasim在2009-10-09 15:30重新编辑 ]
"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: 7012
精华: 0
发帖: 17
金钱: 160 两
威望: 82 点
综合积分: 34 分
注册时间: 2009-06-28
最后登录: 2011-09-24
4楼  发表于: 2009-10-09 15:49
当设备驱动的ioctl出错时,第一件事情是把errno打印出来。

嗯,这我知道,
Unable to find a supported capture format.//本来输出这句的时候就应该退出了,我屏蔽了一下,之后才到下面那句
VIDIOSPICT: Invalid argument


版主可以发些友善那个演示程序的代码或者思路给我吗?谢谢了,我邮箱是08zthuang@stu.edu.cn,QQ是502116852
级别: 新手上路
UID: 7012
精华: 0
发帖: 17
金钱: 160 两
威望: 82 点
综合积分: 34 分
注册时间: 2009-06-28
最后登录: 2011-09-24
5楼  发表于: 2009-10-09 15:51

 回 2楼(monet) 的帖子

OK,没问题,多谢提醒。
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
6楼  发表于: 2009-10-09 16:19
友善之臂的摄像头演示程序源代码是不公开的,我也没有源代码。理论上只要你用的摄像头USB驱动支持V4L2,那所有符合V4L(通过V4L2兼容模式)或者V4L2标准的测试程序应该都是OK的。
"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: 7012
精华: 0
发帖: 17
金钱: 160 两
威望: 82 点
综合积分: 34 分
注册时间: 2009-06-28
最后登录: 2011-09-24
7楼  发表于: 2009-10-09 17:30
刚才查了一下我的摄像头型号,中微星的VID_0AC8  PID_0332,竟然是免驱的!!!!!
在万能驱动里对应这个文件zc0301 ,演示程序使用这个摄像头正常工作,那到底该摄像头支持v4l2吗?请教一下版主。
级别: 新手上路
UID: 7012
精华: 0
发帖: 17
金钱: 160 两
威望: 82 点
综合积分: 34 分
注册时间: 2009-06-28
最后登录: 2011-09-24
8楼  发表于: 2009-10-10 12:36
试了十几个摄像头,还是不行,这次打击太大了。可能是驱动接口问题,哪位高手可以告诉我定义万能驱的ioctl()的文件时那个啊?

ioctl(vd->fd, VIDIOCGMBUF, &(vd->mbuf)) 为什么这个函数一直不行呢?