v4l capturing for usb web cam

rajasekar
I connected the iball usb webcam(640x480) 32bpp web cam to friendly ARM
with linux 2.6 qutopia 
then the device detected ib dev with camera0 
the i want to capture using V4L capture program given bellow but am not
able to capture using this read is failing 
I am new to linux 
please any one help me whether i use this are not and to solve this 
problem one more doubt is is the capture program change with respective to
camera resolution 
  
#include <sys/time.h>
#include <sys/types.h>
#include <asm/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>


#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/videodev.h>
#include <linux/videodev2.h>
#include <linux/fb.h>

#define V4L2_DEV_NODE      "/dev/video0"
#define FB_DEV_NODE      "/dev/fb0"



typedef struct fb_var_screeninfo F_VINFO;

static unsigned int fb_grab(int fd, char **fbmem)
{
  
F_VINFO modeinfo;
unsigned int length;

if (ioctl(fd, FBIOGET_VSCREENINFO, &modeinfo) < 0) 
{
printf("\n Cannot get screen information ");
}
length = modeinfo.xres * modeinfo.yres * (modeinfo.bits_per_pixel >> 3);
printf("fb memory info=xres (%d) x yres (%d), %d bpp\n",modeinfo.xres,
modeinfo.yres, modeinfo.bits_per_pixel);

*fbmem = mmap(0, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (*fbmem < 0) 
{
printf("\n Still couldn't allocate memory ");
length = 0;
}
 printf("length of buffer : %d \n ", length );
return length;
}


static void v4l2_show_on_fb(int fd, char *fbmem, int frames)
{
int i;
int ret;
char preview_buf[240*320*2];
FILE *img=NULL;
img=fopen("pic.bin","w");
printf("\n Opened file for writing ");
    
if ((ret = read (fd, &preview_buf, 240*320*2)) < 0)
printf("\n Can't read from file ");   
if(fwrite(preview_buf,1,240*320*2,img)!=(240*320*2))
printf("\n Error writing to file ");
printf("\n Before memory copy ");
memcpy(fbmem, &preview_buf, 240*320*2);
printf("\n After memory copy ");
fflush(stdout);
fclose(img) ;
printf("\n");
}
static void fb_ungrab(char **fbmem, unsigned int length)
{
if (*fbmem)
munmap(*fbmem, length);
}
int main()
{
 int v4l2_fd=-1,fb_fd=-1;
 char *fbmem = NULL;
 unsigned int fb_length = 0;
 int preview_frames = 180;
 
 v4l2_fd=open(V4L2_DEV_NODE, O_RDWR);
 printf("\n Camera opened successfully in read/write mode ");
 if (v4l2_fd < 0) 
{
printf("\n Camera opening failed ");
exit(0);
}
 fb_fd=open(FB_DEV_NODE, O_RDWR);
 printf("\n Frame Buffer opened successfully in read/write mode ");
  if (fb_fd < 0) 
{
printf("\n Frame Buffer opening failed ");
exit(0);
}
fflush(stdout);
 if ((fb_length = fb_grab(fb_fd, &fbmem)) == 0)
{
printf("\n Cannot allocate memory for frame buffer ");
exit(0);
    }
memset(fbmem, 0, fb_length);
printf("Press Ctrl+C to stop !\n");
fflush(stdout);
v4l2_show_on_fb(v4l2_fd, fbmem, preview_frames);
printf("\n");
printf("\n Before closing ");
if (v4l2_fd > 0)
close(v4l2_fd);
 printf("\n Closing capture operation \n"); 
fb_ungrab(&fbmem, fb_length);
return 0;
}