---
 Makefile   |    5 ++-
 luvcview.c |    1 
 uvcvideo.h |    1 
 v4l2uvc.c  |   86 +++++++++++++++++++++++++++++++------------------------------
 4 files changed, 48 insertions(+), 45 deletions(-)

--- a/Makefile
+++ b/Makefile
@@ -12,6 +12,8 @@
 MATH_LIB = -lm
 SDLLIBS = $(shell sdl-config --libs) 
 SDLFLAGS = $(shell sdl-config --cflags)
+V4L2LIBS = $(shell pkg-config --libs libv4l2)
+V4L2FLAGS = $(shell pkg-config --cflags libv4l2)
 #LIBX11FLAGS= -I/usr/X11R6/include -L/usr/X11R6/lib
 VERSION = 0.2.6
 
@@ -23,7 +25,7 @@
 #           -Wno-unused
 #           -Wunused
 
-CFLAGS += -DUSE_SDL -O2 -DLINUX -DVERSION=\"$(VERSION)\" -I$(SDLFLAGS) $(WARNINGS)
+CFLAGS += -DUSE_SDL -O2 -DLINUX -DVERSION=\"$(VERSION)\" -I$(SDLFLAGS) $(V4L2FLAGS) $(WARNINGS)
 CPPFLAGS = $(CFLAGS)
 
 OBJECTS= luvcview.o color.o utils.o v4l2uvc.o gui.o avilib.o
@@ -40,6 +42,7 @@
 	$(CC)	$(CFLAGS) $(OBJECTS) $(X11_LIB) $(XPM_LIB)\
 		$(MATH_LIB) \
 		$(SDLLIBS)\
+		$(V4L2LIBS)\
 		-o $(APP_BINARY)
 	chmod 755 $(APP_BINARY)
 
--- a/v4l2uvc.c
+++ b/v4l2uvc.c
@@ -25,6 +25,8 @@
 #include <math.h>
 #include <float.h>
 
+#include <libv4l2.h>
+
 #include "v4l2uvc.h"
 #include "utils.h"
 
@@ -72,12 +74,12 @@
 	snprintf(vd->videodevice, 12, "%s", device);
 	printf("Device information:\n");
 	printf("  Device path:  %s\n", vd->videodevice);
-	if ((vd->fd = open(vd->videodevice, O_RDWR)) == -1) {
+	if ((vd->fd = v4l2_open(vd->videodevice, O_RDWR)) == -1) {
 		perror("ERROR opening V4L interface");
 		exit(1);
 	}
 	memset(&vd->cap, 0, sizeof(struct v4l2_capability));
-	ret = ioctl(vd->fd, VIDIOC_QUERYCAP, &vd->cap);
+	ret = v4l2_ioctl(vd->fd, VIDIOC_QUERYCAP, &vd->cap);
 	if (ret < 0) {
 		printf("Error opening device %s: unable to query device.\n",
 				vd->videodevice);
@@ -95,7 +97,7 @@
 	}
 	enum_frame_formats(vd->fd, NULL, 0);
 fatal:    
-	close(vd->fd);
+	v4l2_close(vd->fd);
 	free(vd->videodevice);
 	return 0;
 }
@@ -174,7 +176,7 @@
     free(vd->videodevice);
     free(vd->status);
     free(vd->pictName);
-    close(vd->fd);
+    v4l2_close(vd->fd);
     return -1;
 }
 int enum_controls(int vd) //struct vdIn *vd)
@@ -188,7 +190,7 @@
   getinput=(struct v4l2_input *) calloc(1, sizeof(struct v4l2_input));
   memset(getinput, 0, sizeof(struct v4l2_input));
   getinput->index=0;
-  ioctl(vd,VIDIOC_ENUMINPUT , getinput);
+  v4l2_ioctl(vd,VIDIOC_ENUMINPUT , getinput);
   printf ("Available controls of device '%s' (Type 1=Integer 2=Boolean 3=Menu 4=Button)\n", getinput->name);
 
   //subroutine to read menu items of controls with type 3
@@ -219,7 +221,7 @@
       if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
         continue;
       control_s.id=queryctrl.id;
-      ioctl(vd, VIDIOC_G_CTRL, &control_s);
+      v4l2_ioctl(vd, VIDIOC_G_CTRL, &control_s);
       SDL_Delay(10);
       printf (" index:%-10d name:%-32s type:%d min:%-5d max:%-5d step:%-5d def:%-5d now:%d\n",
               queryctrl.id, queryctrl.name, queryctrl.type, queryctrl.minimum,
@@ -242,7 +244,7 @@
       if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
         continue;
       control_s.id=queryctrl.id;
-      ioctl(vd, VIDIOC_G_CTRL, &control_s);
+      v4l2_ioctl(vd, VIDIOC_G_CTRL, &control_s);
       SDL_Delay(20);
       printf (" index:%-10d name:%-32s type:%d min:%-5d max:%-5d step:%-5d def:%-5d now:%d\n",
               queryctrl.id, queryctrl.name, queryctrl.type, queryctrl.minimum,
@@ -280,7 +282,7 @@
         if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
           continue;
         control_s.id=queryctrl.id;
-        ioctl(vd, VIDIOC_G_CTRL, &control_s);
+        v4l2_ioctl(vd, VIDIOC_G_CTRL, &control_s);
         SDL_Delay(10);
         fprintf (configfile, "%-10d %-10d # name:%-32s type:%d min:%-5d max:%-5d step:%-5d def:%d\n",
                  queryctrl.id, control_s.value, queryctrl.name, queryctrl.type, queryctrl.minimum,
@@ -299,7 +301,7 @@
         if ((queryctrl.id==134217735) || (queryctrl.id==134217736))
           continue;
         control_s.id=queryctrl.id;
-        ioctl(vd, VIDIOC_G_CTRL, &control_s);
+        v4l2_ioctl(vd, VIDIOC_G_CTRL, &control_s);
         SDL_Delay(10);
         fprintf (configfile, "%-10d %-10d # name:%-32s type:%d min:%-5d max:%-5d step:%-5d def:%d\n",
                  queryctrl.id, control_s.value, queryctrl.name, queryctrl.type, queryctrl.minimum,
@@ -335,7 +337,7 @@
     while (NULL !=fgets(buffer, sizeof(buffer), configfile) )
       {
         sscanf(buffer, "%i%i", &control.id, &control.value);
-        if (ioctl(vd, VIDIOC_S_CTRL, &control))
+        if (v4l2_ioctl(vd, VIDIOC_S_CTRL, &control))
           printf("ERROR id:%d val:%d\n", control.id, control.value);
         else
           printf("OK    id:%d val:%d\n", control.id, control.value);
@@ -350,12 +352,12 @@
 	int i;
 	int ret = 0;
 
-	if ((vd->fd = open(vd->videodevice, O_RDWR)) == -1) {
+	if ((vd->fd = v4l2_open(vd->videodevice, O_RDWR)) == -1) {
 		perror("ERROR opening V4L interface");
 		exit(1);
 	}
 	memset(&vd->cap, 0, sizeof(struct v4l2_capability));
-	ret = ioctl(vd->fd, VIDIOC_QUERYCAP, &vd->cap);
+	ret = v4l2_ioctl(vd->fd, VIDIOC_QUERYCAP, &vd->cap);
 	if (ret < 0) {
 		printf("Error opening device %s: unable to query device.\n",
 				vd->videodevice);
@@ -424,7 +426,7 @@
 	vd->fmt.fmt.pix.height = vd->height;
 	vd->fmt.fmt.pix.pixelformat = vd->formatIn;
 	vd->fmt.fmt.pix.field = V4L2_FIELD_ANY;
-	ret = ioctl(vd->fd, VIDIOC_S_FMT, &vd->fmt);
+	ret = v4l2_ioctl(vd->fd, VIDIOC_S_FMT, &vd->fmt);
 	if (ret < 0) {
 		perror("Unable to set format");
 		goto fatal;
@@ -454,12 +456,12 @@
 	setfps->parm.capture.timeperframe.numerator = d;
 	setfps->parm.capture.timeperframe.denominator = n;
 
-	ret = ioctl(vd->fd, VIDIOC_S_PARM, setfps);
+	ret = v4l2_ioctl(vd->fd, VIDIOC_S_PARM, setfps);
 	if(ret == -1) {
 		perror("Unable to set frame rate");
 		goto fatal;
 	}
-	ret = ioctl(vd->fd, VIDIOC_G_PARM, setfps); 
+	ret = v4l2_ioctl(vd->fd, VIDIOC_G_PARM, setfps);
 	if(ret == 0) {
 		float confirmed_fps = (float)setfps->parm.capture.timeperframe.denominator / (float)setfps->parm.capture.timeperframe.numerator;
 		if (confirmed_fps != (float)n / (float)d) {
@@ -484,7 +486,7 @@
 	vd->rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 	vd->rb.memory = V4L2_MEMORY_MMAP;
 
-	ret = ioctl(vd->fd, VIDIOC_REQBUFS, &vd->rb);
+	ret = v4l2_ioctl(vd->fd, VIDIOC_REQBUFS, &vd->rb);
 	if (ret < 0) {
 		perror("Unable to allocate buffers");
 		goto fatal;
@@ -495,7 +497,7 @@
 		vd->buf.index = i;
 		vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 		vd->buf.memory = V4L2_MEMORY_MMAP;
-		ret = ioctl(vd->fd, VIDIOC_QUERYBUF, &vd->buf);
+		ret = v4l2_ioctl(vd->fd, VIDIOC_QUERYBUF, &vd->buf);
 		if (ret < 0) {
 			perror("Unable to query buffer");
 			goto fatal;
@@ -503,7 +505,7 @@
 		if (debug)
 			printf("length: %u offset: %u\n", vd->buf.length,
 					vd->buf.m.offset);
-		vd->mem[i] = mmap(0 /* start anywhere */ ,
+		vd->mem[i] = v4l2_mmap(0 /* start anywhere */ ,
 				vd->buf.length, PROT_READ, MAP_SHARED, vd->fd,
 				vd->buf.m.offset);
 		if (vd->mem[i] == MAP_FAILED) {
@@ -519,7 +521,7 @@
 		vd->buf.index = i;
 		vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 		vd->buf.memory = V4L2_MEMORY_MMAP;
-		ret = ioctl(vd->fd, VIDIOC_QBUF, &vd->buf);
+		ret = v4l2_ioctl(vd->fd, VIDIOC_QBUF, &vd->buf);
 		if (ret < 0) {
 			perror("Unable to queue buffer");
 			goto fatal;;
@@ -536,7 +538,7 @@
     int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
     int ret;
 
-    ret = ioctl(vd->fd, VIDIOC_STREAMON, &type);
+    ret = v4l2_ioctl(vd->fd, VIDIOC_STREAMON, &type);
     if (ret < 0) {
 	perror("Unable to start capture");
 	return ret;
@@ -550,7 +552,7 @@
     int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
     int ret;
 
-    ret = ioctl(vd->fd, VIDIOC_STREAMOFF, &type);
+    ret = v4l2_ioctl(vd->fd, VIDIOC_STREAMOFF, &type);
     if (ret < 0) {
 	perror("Unable to stop capture");
 	return ret;
@@ -571,7 +573,7 @@
     memset(&vd->buf, 0, sizeof(struct v4l2_buffer));
     vd->buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
     vd->buf.memory = V4L2_MEMORY_MMAP;
-    ret = ioctl(vd->fd, VIDIOC_DQBUF, &vd->buf);
+    ret = v4l2_ioctl(vd->fd, VIDIOC_DQBUF, &vd->buf);
     if (ret < 0) {
 	perror("Unable to dequeue buffer");
 	goto err;
@@ -687,7 +689,7 @@
 	goto err;
 	break;
     }
-    ret = ioctl(vd->fd, VIDIOC_QBUF, &vd->buf);
+    ret = v4l2_ioctl(vd->fd, VIDIOC_QBUF, &vd->buf);
     if (ret < 0) {
 	perror("Unable to requeue buffer");
 	goto err;
@@ -721,7 +723,7 @@
 {
 int err =0;
     queryctrl->id = control;
-    if ((err= ioctl(vd->fd, VIDIOC_QUERYCTRL, queryctrl)) < 0) {
+    if ((err= v4l2_ioctl(vd->fd, VIDIOC_QUERYCTRL, queryctrl)) < 0) {
 	perror("ioctl querycontrol error");
     } else if (queryctrl->flags & V4L2_CTRL_FLAG_DISABLED) {
 	printf("control %s disabled\n", (char *) queryctrl->name);
@@ -743,7 +745,7 @@
     if (isv4l2Control(vd, control, &queryctrl) < 0)
 	return -1;
     control_s.id = control;
-    if ((err = ioctl(vd->fd, VIDIOC_G_CTRL, &control_s)) < 0) {
+    if ((err = v4l2_ioctl(vd->fd, VIDIOC_G_CTRL, &control_s)) < 0) {
 	printf("ioctl get control error\n");
 	return -1;
     }
@@ -765,7 +767,7 @@
     if ((value >= min) && (value <= max)) {
 	control_s.id = control;
 	control_s.value = value;
-	if ((err = ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
+	if ((err = v4l2_ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
 	    printf("ioctl set control error\n");
 	    return -1;
 	}
@@ -791,7 +793,7 @@
     if (current <= max) {
 	control_s.id = control;
 	control_s.value = current;
-	if ((err = ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
+	if ((err = v4l2_ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
 	    printf("ioctl set control error\n");
 	    return -1;
 	}            
@@ -819,7 +821,7 @@
     if (current >= min) {
 	control_s.id = control;
 	control_s.value = current;
-	if ((err = ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
+	if ((err = v4l2_ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
 	    printf("ioctl set control error\n");
 	    return -1;
 	}
@@ -841,7 +843,7 @@
     current = v4l2GetControl(vd, control);
     control_s.id = control;
     control_s.value = !current;
-    if ((err = ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
+    if ((err = v4l2_ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
 	printf("ioctl toggle control error\n");
 	return -1;
     }
@@ -858,7 +860,7 @@
     val_def = queryctrl.default_value;
     control_s.id = control;
     control_s.value = val_def;
-    if ((err = ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
+    if ((err = v4l2_ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
 	printf("ioctl reset control error\n");
 	return -1;
     }
@@ -878,7 +880,7 @@
 
 	control_s.id = control;
 	control_s.value = 1;
-	if ((err = ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
+	if ((err = v4l2_ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
 		printf("ERROR: Unable to reset pan (error = %d)\n", errno);
 		return -1;
 	}
@@ -898,7 +900,7 @@
 
 	control_s.id = control;
 	control_s.value = 1;
-	if ((err = ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
+	if ((err = v4l2_ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
 		printf("ERROR: Unable to reset tilt (error = %d)\n", errno);
 		return -1;
 	}
@@ -919,7 +921,7 @@
 
 	control_s.id = control;
 	control_s.value = 3;
-	if ((err = ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
+	if ((err = v4l2_ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
 		printf("ERROR: Unable to reset pan/tilt (error = %d)\n", errno);
 		return -1;
 	}
@@ -938,7 +940,7 @@
 		return -1;
 	control_s.id = control;
 	control_s.value = inc;
-	if ((err = ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
+	if ((err = v4l2_ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
 		printf("ioctl pan updown control error\n");
 		return -1;
 	}
@@ -955,7 +957,7 @@
 	return -1;  
     control_s.id = control;
     control_s.value = inc;
-    if ((err = ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
+    if ((err = v4l2_ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
 	printf("ioctl tiltupdown control error\n");
 	return -1;
     }
@@ -984,7 +986,7 @@
   control_s[1].id = t_control;
   control_s[1].value = inc_t;
 
-  if ((err = ioctl(vd->fd, VIDIOC_S_EXT_CTRLS, &control_s_array)) < 0) {
+  if ((err = v4l2_ioctl(vd->fd, VIDIOC_S_EXT_CTRLS, &control_s_array)) < 0) {
     printf("ioctl pan-tilt updown control error\n");
     return -1;
   }
@@ -1017,7 +1019,7 @@
   pan.s16.tilt = 0;
  
 	control_s.value = pan.value ;
-    if ((err = ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
+    if ((err = v4l2_ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
 	printf("ioctl pan updown control error\n");
 	return -1;
 	}
@@ -1038,7 +1040,7 @@
     pan.s16.tilt = inc;
   
 	control_s.value = pan.value;
-    if ((err = ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
+    if ((err = v4l2_ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
 	printf("ioctl tiltupdown control error\n");
 	return -1;
 	}
@@ -1057,7 +1059,7 @@
 
        control_s.value = flt;
 
-    if ((err = ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
+    if ((err = v4l2_ioctl(vd->fd, VIDIOC_S_CTRL, &control_s)) < 0) {
 	printf("ioctl set_light_frequency_filter error\n");
 	return -1;
 	}
@@ -1074,7 +1076,7 @@
 	fival.width = width;
 	fival.height = height;
 	printf("\tTime interval between frame: ");
-	while ((ret = ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &fival)) == 0) {
+	while ((ret = v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &fival)) == 0) {
 		if (fival.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
 				printf("%u/%u, ",
 						fival.discrete.numerator, fival.discrete.denominator);
@@ -1109,7 +1111,7 @@
 	memset(&fsize, 0, sizeof(fsize));
 	fsize.index = 0;
 	fsize.pixel_format = pixfmt;
-	while ((ret = ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &fsize)) == 0) {
+	while ((ret = v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &fsize)) == 0) {
 		if (fsize.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
 			printf("{ discrete: width = %u, height = %u }\n",
 					fsize.discrete.width, fsize.discrete.height);
@@ -1152,7 +1154,7 @@
 	memset(&fmt, 0, sizeof(fmt));
 	fmt.index = 0;
 	fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-	while ((ret = ioctl(dev, VIDIOC_ENUM_FMT, &fmt)) == 0) {
+	while ((ret = v4l2_ioctl(dev, VIDIOC_ENUM_FMT, &fmt)) == 0) {
 		if(supported_formats == NULL) {
 			printf("{ pixelformat = '%c%c%c%c', description = '%s' }\n",
 					fmt.pixelformat & 0xFF, (fmt.pixelformat >> 8) & 0xFF,
--- a/luvcview.c
+++ b/luvcview.c
@@ -33,7 +33,6 @@
 #include <SDL/SDL_thread.h>
 #include <SDL/SDL_audio.h>
 #include <SDL/SDL_timer.h>
-#include <linux/videodev.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <errno.h>
--- a/uvcvideo.h
+++ b/uvcvideo.h
@@ -2,7 +2,6 @@
 #define _USB_VIDEO_H_
 
 #include <linux/kernel.h>
-#include <linux/videodev.h>
 
 #include "uvc_compat.h"
 
