* [patch] ioctl-test.c
@ 2008-03-02 6:42 Carl Karsten
2008-03-02 22:10 ` hermann pitton
0 siblings, 1 reply; 4+ messages in thread
From: Carl Karsten @ 2008-03-02 6:42 UTC (permalink / raw)
To: video4linux-list
I copied the command line parameter support from test/pixfmt-test.c, and used
the prt_caps() func from lib/v4l2_driver.c.
I am hoping to merge all of the test code into one big test, and put all the
generic code into one lib.
Carl K
diff -r 127f67dea087 v4l2-apps/lib/v4l2_driver.h
--- a/v4l2-apps/lib/v4l2_driver.h Tue Feb 26 20:43:56 2008 +0000
+++ b/v4l2-apps/lib/v4l2_driver.h Sun Mar 02 00:37:59 2008 -0600
@@ -12,6 +12,7 @@
Lesser General Public License for more details.
*/
+#include <stddef.h>
#include <stdint.h>
#include <sys/time.h>
#include <linux/videodev2.h>
diff -r 127f67dea087 v4l2-apps/test/ioctl-test.c
--- a/v4l2-apps/test/ioctl-test.c Tue Feb 26 20:43:56 2008 +0000
+++ b/v4l2-apps/test/ioctl-test.c Sun Mar 02 00:37:59 2008 -0600
@@ -29,14 +29,24 @@
*/
//#define INTERNAL 1 /* meant for testing ioctl debug msgs */
+#include <errno.h>
+#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
+#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
+#include <assert.h>
+
+#include <getopt.h> /* getopt_long() */
+
#include <fcntl.h>
-#include "linux/videodev.h"
+// #include "linux/videodev.h"
+#include <linux/videodev2.h>
+
+#include <lib/v4l2_driver.h>
#ifdef INTERNAL
typedef __u8 u8;
@@ -47,6 +57,25 @@ typedef __u32 u32;
#else
typedef u_int32_t u32;
#endif
+
+static const char * my_name;
+#define VERSION "1.1"
+static const char * dev_name = "/dev/video0";
+
+#define CLEAR(var) memset (&(var), 0, sizeof (var))
+
+typedef enum {
+ IO_METHOD_READ = 1,
+ IO_METHOD_MMAP,
+} io_methods;
+
+
+static int fd;
+static v4l2_std_id std_id;
+static io_methods io_method;
+static struct v4l2_format fmt;
+//static io_buffer * buffers;
+static unsigned int n_buffers;
/* All possible parameters used on v4l ioctls */
union v4l_parms {
@@ -209,26 +238,253 @@ int ioctls[] = {
#define S_IOCTLS sizeof(ioctls)/sizeof(ioctls[0])
/********************************************************************/
+static int
+xioctl (int fd,
+ int request,
+ void * arg)
+{
+ int r;
-int main (void)
+ do r = ioctl (fd, request, arg);
+ while (-1 == r && EINTR == errno);
+
+ return r;
+}
+
+/********************************************************************/
+static void
+error_exit (const char * templ,
+ ...)
{
- int fd=0, ret=0;
+ va_list ap;
+
+ fprintf (stderr, "%s: ", my_name);
+ va_start (ap, templ);
+ vfprintf (stderr, templ, ap);
+ va_end (ap);
+
+ exit (EXIT_FAILURE);
+}
+
+/********************************************************************/
+static void
+errno_exit (const char * s)
+{
+ error_exit ("%s error %d, %s\n",
+ s, errno, strerror (errno));
+}
+
+
+/********************************************************************/
+static void
+open_device (void)
+{
+ struct stat st;
+
+ if (-1 == stat (dev_name, &st)) {
+ error_exit ("Cannot identify '%s'. %s.\n",
+ dev_name, strerror (errno));
+ }
+
+ if (!S_ISCHR (st.st_mode)) {
+ error_exit ("%s is not a device file.\n", dev_name);
+ }
+
+ fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
+
+ if (-1 == fd) {
+ error_exit ("Cannot open %s. %s.\n",
+ dev_name, strerror (errno));
+ }
+}
+
+/********************************************************************/
+static void
+printstat (struct v4l2_capability cap)
+{
+ printf ("driver=%s\n" "card=%s\n" "bus=%s\n" "version=%d.%d.%d\n"
+ "capabilities=%s\n",
+ cap.driver,cap.card,cap.bus_info,
+ (cap.version >> 16) & 0xff,
+ (cap.version >> 8) & 0xff,
+ cap.version & 0xff,
+ prt_caps(cap.capabilities));
+}
+
+/********************************************************************/
+
+static void
+init_device (void)
+{
+ struct v4l2_capability cap;
+ struct v4l2_cropcap cropcap;
+ struct v4l2_crop crop;
+
+ if (-1 == xioctl (fd, VIDIOC_QUERYCAP, &cap)) {
+ if (EINVAL == errno) {
+ error_exit ("%s is not a V4L2 device.\n");
+ } else {
+ errno_exit ("VIDIOC_QUERYCAP");
+ }
+ }
+
+ printstat(cap);
+
+ switch (io_method) {
+ case 0:
+ if (cap.capabilities & V4L2_CAP_STREAMING) {
+ io_method = IO_METHOD_MMAP;
+ } else if (cap.capabilities & V4L2_CAP_READWRITE) {
+ io_method = IO_METHOD_READ;
+ } else {
+ error_exit ("%s does not support reading or "
+ "streaming.\n");
+ }
+
+ break;
+
+ case IO_METHOD_READ:
+ if (0 == (cap.capabilities & V4L2_CAP_READWRITE)) {
+ error_exit ("%s does not support read i/o.\n");
+ }
+
+ break;
+
+ case IO_METHOD_MMAP:
+ if (0 == (cap.capabilities & V4L2_CAP_STREAMING)) {
+ error_exit ("%s does not support streaming i/o.\n");
+ }
+
+ break;
+
+ default:
+ assert (0);
+ break;
+ }
+
+ CLEAR (cropcap);
+
+ cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+
+ if (0 == xioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
+ crop.type = cropcap.type;
+ crop.c = cropcap.defrect; /* reset to default */
+
+ /* Errors ignored. */
+ xioctl (fd, VIDIOC_S_CROP, &crop);
+ } else {
+ /* Errors ignored. */
+ }
+
+ if (-1 == xioctl (fd, VIDIOC_G_STD, &std_id))
+ errno_exit ("VIDIOC_G_STD");
+}
+
+
+/********************************************************************/
+static void
+mainloop (void)
+{
+
+ int ret=0;
unsigned i;
- char *device="/dev/video0";
union v4l_parms p;
- if ((fd = open(device, O_RDONLY)) < 0) {
- perror("Couldn't open video0");
- return(-1);
- }
for (i=0;i<S_IOCTLS;i++) {
memset(&p,0,sizeof(p));
ret=ioctl(fd,ioctls[i], (void *) &p);
printf("%i: ioctl=0x%08x, return=%d\n",i, ioctls[i], ret);
}
+}
+
+/********************************************************************/
+
+static void
+usage (FILE * fp,
+ int argc,
+ char ** argv)
+{
+ fprintf (fp, "\
+V4L2 test " VERSION "\n\
+Copyright (C) 2007 Michael H. Schimek\n\
+This program is licensed under GPL 2 or later. NO WARRANTIES.\n\n\
+Usage: %s [options]\n\n\
+Options:\n\
+-d | --device name Video device name [%s]\n\
+-h | --help Print this message\n\
+-m | --mmap Use memory mapped buffers (auto)\n\
+-r | --read Use read() calls (auto)\n\
+",
+ my_name, dev_name);
+}
+
+static const char short_options [] = "d:hmr";
+
+static const struct option
+long_options [] = {
+ { "device", required_argument, NULL, 'd' },
+ { "help", no_argument, NULL, 'h' },
+ { "mmap", no_argument, NULL, 'm' },
+ { "read", no_argument, NULL, 'r' },
+ { "usage", no_argument, NULL, 'h' },
+ { 0, 0, 0, 0 }
+};
+
+
+
+int
+main (int argc,
+ char ** argv)
+{
+ my_name = argv[0];
+
+ for (;;) {
+ int index;
+ int c;
+
+ c = getopt_long (argc, argv,
+ short_options, long_options,
+ &index);
+
+ if (-1 == c)
+ break;
+
+ switch (c) {
+ case 0: /* getopt_long() flag */
+ break;
+
+ case 'd':
+ dev_name = optarg;
+ break;
+
+ case 'h':
+ usage (stdout, argc, argv);
+ exit (EXIT_SUCCESS);
+
+ case 'm':
+ io_method = IO_METHOD_MMAP;
+ break;
+
+ case 'r':
+ io_method = IO_METHOD_READ;
+ break;
+
+ default:
+ usage (stderr, argc, argv);
+ exit (EXIT_FAILURE);
+ }
+ }
+
+ open_device ();
+
+ init_device ();
+
+ mainloop ();
close (fd);
+ exit (EXIT_SUCCESS);
+
return (0);
}
--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [patch] ioctl-test.c
2008-03-02 6:42 [patch] ioctl-test.c Carl Karsten
@ 2008-03-02 22:10 ` hermann pitton
2008-03-02 23:02 ` Carl Karsten
0 siblings, 1 reply; 4+ messages in thread
From: hermann pitton @ 2008-03-02 22:10 UTC (permalink / raw)
To: Carl Karsten; +Cc: video4linux-list
Hi Carl,
Am Sonntag, den 02.03.2008, 00:42 -0600 schrieb Carl Karsten:
> I copied the command line parameter support from test/pixfmt-test.c, and used
> the prt_caps() func from lib/v4l2_driver.c.
>
> I am hoping to merge all of the test code into one big test, and put all the
> generic code into one lib.
>
> Carl K
>
that looks interesting and should save time looking up the ioctls in the
code etc.
But please don't post any patches inline with thunderbird, try as
attachment or use another mail client.
Thunderbird adds to any unchanged line one space of ident mismatch at
the beginning and also fuzz. Also try to keep the tabbing.
Concerning newly included headers, at what kernel environment you get
this to compile? My stuff still seems to be too old.
Cheers,
Hermann
> diff -r 127f67dea087 v4l2-apps/lib/v4l2_driver.h
> --- a/v4l2-apps/lib/v4l2_driver.h Tue Feb 26 20:43:56 2008 +0000
> +++ b/v4l2-apps/lib/v4l2_driver.h Sun Mar 02 00:37:59 2008 -0600
> @@ -12,6 +12,7 @@
> Lesser General Public License for more details.
> */
>
> +#include <stddef.h>
> #include <stdint.h>
> #include <sys/time.h>
> #include <linux/videodev2.h>
> diff -r 127f67dea087 v4l2-apps/test/ioctl-test.c
> --- a/v4l2-apps/test/ioctl-test.c Tue Feb 26 20:43:56 2008 +0000
> +++ b/v4l2-apps/test/ioctl-test.c Sun Mar 02 00:37:59 2008 -0600
> @@ -29,14 +29,24 @@
> */
> //#define INTERNAL 1 /* meant for testing ioctl debug msgs */
>
> +#include <errno.h>
> +#include <stdlib.h>
> #include <stdio.h>
> #include <unistd.h>
> +#include <stdarg.h>
> #include <string.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <sys/ioctl.h>
> +#include <assert.h>
> +
> +#include <getopt.h> /* getopt_long() */
> +
> #include <fcntl.h>
> -#include "linux/videodev.h"
> +// #include "linux/videodev.h"
> +#include <linux/videodev2.h>
> +
> +#include <lib/v4l2_driver.h>
>
> #ifdef INTERNAL
> typedef __u8 u8;
> @@ -47,6 +57,25 @@ typedef __u32 u32;
> #else
> typedef u_int32_t u32;
> #endif
> +
> +static const char * my_name;
> +#define VERSION "1.1"
> +static const char * dev_name = "/dev/video0";
> +
> +#define CLEAR(var) memset (&(var), 0, sizeof (var))
> +
> +typedef enum {
> + IO_METHOD_READ = 1,
> + IO_METHOD_MMAP,
> +} io_methods;
> +
> +
> +static int fd;
> +static v4l2_std_id std_id;
> +static io_methods io_method;
> +static struct v4l2_format fmt;
> +//static io_buffer * buffers;
> +static unsigned int n_buffers;
>
> /* All possible parameters used on v4l ioctls */
> union v4l_parms {
> @@ -209,26 +238,253 @@ int ioctls[] = {
> #define S_IOCTLS sizeof(ioctls)/sizeof(ioctls[0])
>
> /********************************************************************/
> +static int
> +xioctl (int fd,
> + int request,
> + void * arg)
> +{
> + int r;
>
> -int main (void)
> + do r = ioctl (fd, request, arg);
> + while (-1 == r && EINTR == errno);
> +
> + return r;
> +}
> +
> +/********************************************************************/
> +static void
> +error_exit (const char * templ,
> + ...)
> {
> - int fd=0, ret=0;
> + va_list ap;
> +
> + fprintf (stderr, "%s: ", my_name);
> + va_start (ap, templ);
> + vfprintf (stderr, templ, ap);
> + va_end (ap);
> +
> + exit (EXIT_FAILURE);
> +}
> +
> +/********************************************************************/
> +static void
> +errno_exit (const char * s)
> +{
> + error_exit ("%s error %d, %s\n",
> + s, errno, strerror (errno));
> +}
> +
> +
> +/********************************************************************/
> +static void
> +open_device (void)
> +{
> + struct stat st;
> +
> + if (-1 == stat (dev_name, &st)) {
> + error_exit ("Cannot identify '%s'. %s.\n",
> + dev_name, strerror (errno));
> + }
> +
> + if (!S_ISCHR (st.st_mode)) {
> + error_exit ("%s is not a device file.\n", dev_name);
> + }
> +
> + fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
> +
> + if (-1 == fd) {
> + error_exit ("Cannot open %s. %s.\n",
> + dev_name, strerror (errno));
> + }
> +}
> +
> +/********************************************************************/
> +static void
> +printstat (struct v4l2_capability cap)
> +{
> + printf ("driver=%s\n" "card=%s\n" "bus=%s\n" "version=%d.%d.%d\n"
> + "capabilities=%s\n",
> + cap.driver,cap.card,cap.bus_info,
> + (cap.version >> 16) & 0xff,
> + (cap.version >> 8) & 0xff,
> + cap.version & 0xff,
> + prt_caps(cap.capabilities));
> +}
> +
> +/********************************************************************/
> +
> +static void
> +init_device (void)
> +{
> + struct v4l2_capability cap;
> + struct v4l2_cropcap cropcap;
> + struct v4l2_crop crop;
> +
> + if (-1 == xioctl (fd, VIDIOC_QUERYCAP, &cap)) {
> + if (EINVAL == errno) {
> + error_exit ("%s is not a V4L2 device.\n");
> + } else {
> + errno_exit ("VIDIOC_QUERYCAP");
> + }
> + }
> +
> + printstat(cap);
> +
> + switch (io_method) {
> + case 0:
> + if (cap.capabilities & V4L2_CAP_STREAMING) {
> + io_method = IO_METHOD_MMAP;
> + } else if (cap.capabilities & V4L2_CAP_READWRITE) {
> + io_method = IO_METHOD_READ;
> + } else {
> + error_exit ("%s does not support reading or "
> + "streaming.\n");
> + }
> +
> + break;
> +
> + case IO_METHOD_READ:
> + if (0 == (cap.capabilities & V4L2_CAP_READWRITE)) {
> + error_exit ("%s does not support read i/o.\n");
> + }
> +
> + break;
> +
> + case IO_METHOD_MMAP:
> + if (0 == (cap.capabilities & V4L2_CAP_STREAMING)) {
> + error_exit ("%s does not support streaming i/o.\n");
> + }
> +
> + break;
> +
> + default:
> + assert (0);
> + break;
> + }
> +
> + CLEAR (cropcap);
> +
> + cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
> +
> + if (0 == xioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
> + crop.type = cropcap.type;
> + crop.c = cropcap.defrect; /* reset to default */
> +
> + /* Errors ignored. */
> + xioctl (fd, VIDIOC_S_CROP, &crop);
> + } else {
> + /* Errors ignored. */
> + }
> +
> + if (-1 == xioctl (fd, VIDIOC_G_STD, &std_id))
> + errno_exit ("VIDIOC_G_STD");
> +}
> +
> +
> +/********************************************************************/
> +static void
> +mainloop (void)
> +{
> +
> + int ret=0;
> unsigned i;
> - char *device="/dev/video0";
> union v4l_parms p;
>
> - if ((fd = open(device, O_RDONLY)) < 0) {
> - perror("Couldn't open video0");
> - return(-1);
> - }
>
> for (i=0;i<S_IOCTLS;i++) {
> memset(&p,0,sizeof(p));
> ret=ioctl(fd,ioctls[i], (void *) &p);
> printf("%i: ioctl=0x%08x, return=%d\n",i, ioctls[i], ret);
> }
> +}
> +
> +/********************************************************************/
> +
> +static void
> +usage (FILE * fp,
> + int argc,
> + char ** argv)
> +{
> + fprintf (fp, "\
> +V4L2 test " VERSION "\n\
> +Copyright (C) 2007 Michael H. Schimek\n\
> +This program is licensed under GPL 2 or later. NO WARRANTIES.\n\n\
> +Usage: %s [options]\n\n\
> +Options:\n\
> +-d | --device name Video device name [%s]\n\
> +-h | --help Print this message\n\
> +-m | --mmap Use memory mapped buffers (auto)\n\
> +-r | --read Use read() calls (auto)\n\
> +",
> + my_name, dev_name);
> +}
> +
> +static const char short_options [] = "d:hmr";
> +
> +static const struct option
> +long_options [] = {
> + { "device", required_argument, NULL, 'd' },
> + { "help", no_argument, NULL, 'h' },
> + { "mmap", no_argument, NULL, 'm' },
> + { "read", no_argument, NULL, 'r' },
> + { "usage", no_argument, NULL, 'h' },
> + { 0, 0, 0, 0 }
> +};
> +
> +
> +
> +int
> +main (int argc,
> + char ** argv)
> +{
> + my_name = argv[0];
> +
> + for (;;) {
> + int index;
> + int c;
> +
> + c = getopt_long (argc, argv,
> + short_options, long_options,
> + &index);
> +
> + if (-1 == c)
> + break;
> +
> + switch (c) {
> + case 0: /* getopt_long() flag */
> + break;
> +
> + case 'd':
> + dev_name = optarg;
> + break;
> +
> + case 'h':
> + usage (stdout, argc, argv);
> + exit (EXIT_SUCCESS);
> +
> + case 'm':
> + io_method = IO_METHOD_MMAP;
> + break;
> +
> + case 'r':
> + io_method = IO_METHOD_READ;
> + break;
> +
> + default:
> + usage (stderr, argc, argv);
> + exit (EXIT_FAILURE);
> + }
> + }
> +
> + open_device ();
> +
> + init_device ();
> +
> + mainloop ();
>
> close (fd);
>
> + exit (EXIT_SUCCESS);
> +
> return (0);
> }
>
--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [patch] ioctl-test.c
2008-03-02 22:10 ` hermann pitton
@ 2008-03-02 23:02 ` Carl Karsten
2008-03-02 23:15 ` hermann pitton
0 siblings, 1 reply; 4+ messages in thread
From: Carl Karsten @ 2008-03-02 23:02 UTC (permalink / raw)
To: hermann pitton; +Cc: video4linux-list
[-- Attachment #1: Type: text/plain, Size: 850 bytes --]
hermann pitton wrote:
> Hi Carl,
>
> Am Sonntag, den 02.03.2008, 00:42 -0600 schrieb Carl Karsten:
>> I copied the command line parameter support from test/pixfmt-test.c, and used
>> the prt_caps() func from lib/v4l2_driver.c.
>>
>> I am hoping to merge all of the test code into one big test, and put all the
>> generic code into one lib.
>>
>> Carl K
>>
>
> that looks interesting and should save time looking up the ioctls in the
> code etc.
There is more to come :)
>
> But please don't post any patches inline with thunderbird, try as
> attachment or use another mail client.
better?
> Concerning newly included headers, at what kernel environment you get
> this to compile? My stuff still seems to be too old.
Oh, didn't include the Makefile, probably what is causing you problems. I tried
to keep my vivi patch separate.
Carl K
[-- Attachment #2: ioctl-test.patch --]
[-- Type: text/x-patch, Size: 10690 bytes --]
diff -r 127f67dea087 linux/drivers/media/video/vivi.c
--- a/linux/drivers/media/video/vivi.c Tue Feb 26 20:43:56 2008 +0000
+++ b/linux/drivers/media/video/vivi.c Sun Mar 02 17:00:24 2008 -0600
@@ -48,6 +48,8 @@
#include <linux/freezer.h>
#endif
+#define MODULE_NAME "vivi"
+
/* Wake up at about 30 fps */
#define WAKE_NUMERATOR 30
#define WAKE_DENOMINATOR 1001
@@ -56,7 +58,7 @@
#include "font.h"
#define VIVI_MAJOR_VERSION 0
-#define VIVI_MINOR_VERSION 4
+#define VIVI_MINOR_VERSION 5
#define VIVI_RELEASE 0
#define VIVI_VERSION \
KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
@@ -1220,10 +1222,14 @@ static int vivi_release(void)
list_del(list);
dev = list_entry(list, struct vivi_dev, vivi_devlist);
- if (-1 != dev->vfd->minor)
+ if (-1 != dev->vfd->minor) {
video_unregister_device(dev->vfd);
- else
+ printk(KERN_INFO "%s: /dev/video%d unregistered.\n", MODULE_NAME, dev->vfd->minor);
+ }
+ else {
video_device_release(dev->vfd);
+ printk(KERN_INFO "%s: /dev/video%d released.\n", MODULE_NAME, dev->vfd->minor);
+ }
kfree(dev);
}
@@ -1338,6 +1344,7 @@ static int __init vivi_init(void)
video_nr++;
dev->vfd = vfd;
+ printk(KERN_INFO "%s: V4L2 device registered as /dev/video%d\n", MODULE_NAME, vfd->minor);
}
if (ret < 0) {
@@ -1345,7 +1352,8 @@ static int __init vivi_init(void)
printk(KERN_INFO "Error %d while loading vivi driver\n", ret);
} else
printk(KERN_INFO "Video Technology Magazine Virtual Video "
- "Capture Board successfully loaded.\n");
+ "Capture Board ver %u.%u.%u successfully loaded.\n",
+ (VIVI_VERSION >> 16) & 0xFF, (VIVI_VERSION >> 8) & 0xFF, VIVI_VERSION & 0xFF);
return ret;
}
diff -r 127f67dea087 v4l2-apps/lib/v4l2_driver.h
--- a/v4l2-apps/lib/v4l2_driver.h Tue Feb 26 20:43:56 2008 +0000
+++ b/v4l2-apps/lib/v4l2_driver.h Sun Mar 02 17:00:24 2008 -0600
@@ -12,6 +12,7 @@
Lesser General Public License for more details.
*/
+#include <stddef.h>
#include <stdint.h>
#include <sys/time.h>
#include <linux/videodev2.h>
diff -r 127f67dea087 v4l2-apps/test/Makefile
--- a/v4l2-apps/test/Makefile Tue Feb 26 20:43:56 2008 +0000
+++ b/v4l2-apps/test/Makefile Sun Mar 02 17:00:24 2008 -0600
@@ -25,6 +25,8 @@ install:
driver-test: driver-test.o ../lib/libv4l2.a
+ioctl-test: ioctl-test.o ../lib/libv4l2.a
+
pixfmt-test: pixfmt-test.o
$(CC) $(LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@ -lX11
diff -r 127f67dea087 v4l2-apps/test/ioctl-test.c
--- a/v4l2-apps/test/ioctl-test.c Tue Feb 26 20:43:56 2008 +0000
+++ b/v4l2-apps/test/ioctl-test.c Sun Mar 02 17:00:24 2008 -0600
@@ -29,14 +29,23 @@
*/
//#define INTERNAL 1 /* meant for testing ioctl debug msgs */
+#include <errno.h>
+#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
+#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
+#include <assert.h>
+
+#include <getopt.h> /* getopt_long() */
+
#include <fcntl.h>
-#include "linux/videodev.h"
+#include <linux/videodev2.h>
+
+#include <lib/v4l2_driver.h>
#ifdef INTERNAL
typedef __u8 u8;
@@ -47,6 +56,24 @@ typedef __u32 u32;
#else
typedef u_int32_t u32;
#endif
+
+static const char * my_name;
+#define VERSION "1.1"
+static const char * dev_name = "/dev/video0";
+
+#define CLEAR(var) memset (&(var), 0, sizeof (var))
+
+typedef enum {
+ IO_METHOD_READ = 1,
+ IO_METHOD_MMAP,
+} io_methods;
+
+
+static int fd;
+static v4l2_std_id std_id;
+static io_methods io_method;
+static struct v4l2_format fmt;
+static unsigned int n_buffers;
/* All possible parameters used on v4l ioctls */
union v4l_parms {
@@ -209,26 +236,253 @@ int ioctls[] = {
#define S_IOCTLS sizeof(ioctls)/sizeof(ioctls[0])
/********************************************************************/
+static int
+xioctl (int fd,
+ int request,
+ void * arg)
+{
+ int r;
-int main (void)
+ do r = ioctl (fd, request, arg);
+ while (-1 == r && EINTR == errno);
+
+ return r;
+}
+
+/********************************************************************/
+static void
+error_exit (const char * templ,
+ ...)
{
- int fd=0, ret=0;
+ va_list ap;
+
+ fprintf (stderr, "%s: ", my_name);
+ va_start (ap, templ);
+ vfprintf (stderr, templ, ap);
+ va_end (ap);
+
+ exit (EXIT_FAILURE);
+}
+
+/********************************************************************/
+static void
+errno_exit (const char * s)
+{
+ error_exit ("%s error %d, %s\n",
+ s, errno, strerror (errno));
+}
+
+
+/********************************************************************/
+static void
+open_device (void)
+{
+ struct stat st;
+
+ if (-1 == stat (dev_name, &st)) {
+ error_exit ("Cannot identify '%s'. %s.\n",
+ dev_name, strerror (errno));
+ }
+
+ if (!S_ISCHR (st.st_mode)) {
+ error_exit ("%s is not a device file.\n", dev_name);
+ }
+
+ fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
+
+ if (-1 == fd) {
+ error_exit ("Cannot open %s. %s.\n",
+ dev_name, strerror (errno));
+ }
+}
+
+/********************************************************************/
+static void
+printstat (struct v4l2_capability cap)
+{
+ printf ("driver=%s\n" "card=%s\n" "bus=%s\n" "version=%d.%d.%d\n"
+ "capabilities=%s\n",
+ cap.driver,cap.card,cap.bus_info,
+ (cap.version >> 16) & 0xff,
+ (cap.version >> 8) & 0xff,
+ cap.version & 0xff,
+ prt_caps(cap.capabilities));
+}
+
+/********************************************************************/
+
+static void
+init_device (void)
+{
+ struct v4l2_capability cap;
+ struct v4l2_cropcap cropcap;
+ struct v4l2_crop crop;
+
+ if (-1 == xioctl (fd, VIDIOC_QUERYCAP, &cap)) {
+ if (EINVAL == errno) {
+ error_exit ("%s is not a V4L2 device.\n");
+ } else {
+ errno_exit ("VIDIOC_QUERYCAP");
+ }
+ }
+
+ printstat(cap);
+
+ switch (io_method) {
+ case 0:
+ if (cap.capabilities & V4L2_CAP_STREAMING) {
+ io_method = IO_METHOD_MMAP;
+ } else if (cap.capabilities & V4L2_CAP_READWRITE) {
+ io_method = IO_METHOD_READ;
+ } else {
+ error_exit ("%s does not support reading or "
+ "streaming.\n");
+ }
+
+ break;
+
+ case IO_METHOD_READ:
+ if (0 == (cap.capabilities & V4L2_CAP_READWRITE)) {
+ error_exit ("%s does not support read i/o.\n");
+ }
+
+ break;
+
+ case IO_METHOD_MMAP:
+ if (0 == (cap.capabilities & V4L2_CAP_STREAMING)) {
+ error_exit ("%s does not support streaming i/o.\n");
+ }
+
+ break;
+
+ default:
+ assert (0);
+ break;
+ }
+
+ CLEAR (cropcap);
+
+ cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+
+ if (0 == xioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
+ crop.type = cropcap.type;
+ crop.c = cropcap.defrect; /* reset to default */
+
+ /* Errors ignored. */
+ xioctl (fd, VIDIOC_S_CROP, &crop);
+ } else {
+ /* Errors ignored. */
+ }
+
+ if (-1 == xioctl (fd, VIDIOC_G_STD, &std_id))
+ errno_exit ("VIDIOC_G_STD");
+}
+
+
+/********************************************************************/
+static void
+mainloop (void)
+{
+
+ int ret=0;
unsigned i;
- char *device="/dev/video0";
union v4l_parms p;
- if ((fd = open(device, O_RDONLY)) < 0) {
- perror("Couldn't open video0");
- return(-1);
- }
for (i=0;i<S_IOCTLS;i++) {
memset(&p,0,sizeof(p));
ret=ioctl(fd,ioctls[i], (void *) &p);
printf("%i: ioctl=0x%08x, return=%d\n",i, ioctls[i], ret);
}
+}
+
+/********************************************************************/
+
+static void
+usage (FILE * fp,
+ int argc,
+ char ** argv)
+{
+ fprintf (fp, "\
+V4L2 test " VERSION "\n\
+Copyright (C) 2007 Michael H. Schimek\n\
+This program is licensed under GPL 2 or later. NO WARRANTIES.\n\n\
+Usage: %s [options]\n\n\
+Options:\n\
+-d | --device name Video device name [%s]\n\
+-h | --help Print this message\n\
+-m | --mmap Use memory mapped buffers (auto)\n\
+-r | --read Use read() calls (auto)\n\
+",
+ my_name, dev_name);
+}
+
+static const char short_options [] = "d:hmr";
+
+static const struct option
+long_options [] = {
+ { "device", required_argument, NULL, 'd' },
+ { "help", no_argument, NULL, 'h' },
+ { "mmap", no_argument, NULL, 'm' },
+ { "read", no_argument, NULL, 'r' },
+ { "usage", no_argument, NULL, 'h' },
+ { 0, 0, 0, 0 }
+};
+
+
+
+int
+main (int argc,
+ char ** argv)
+{
+ my_name = argv[0];
+
+ for (;;) {
+ int index;
+ int c;
+
+ c = getopt_long (argc, argv,
+ short_options, long_options,
+ &index);
+
+ if (-1 == c)
+ break;
+
+ switch (c) {
+ case 0: /* getopt_long() flag */
+ break;
+
+ case 'd':
+ dev_name = optarg;
+ break;
+
+ case 'h':
+ usage (stdout, argc, argv);
+ exit (EXIT_SUCCESS);
+
+ case 'm':
+ io_method = IO_METHOD_MMAP;
+ break;
+
+ case 'r':
+ io_method = IO_METHOD_READ;
+ break;
+
+ default:
+ usage (stderr, argc, argv);
+ exit (EXIT_FAILURE);
+ }
+ }
+
+ open_device ();
+
+ init_device ();
+
+ mainloop ();
close (fd);
+ exit (EXIT_SUCCESS);
+
return (0);
}
[-- Attachment #3: Type: text/plain, Size: 164 bytes --]
--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [patch] ioctl-test.c
2008-03-02 23:02 ` Carl Karsten
@ 2008-03-02 23:15 ` hermann pitton
0 siblings, 0 replies; 4+ messages in thread
From: hermann pitton @ 2008-03-02 23:15 UTC (permalink / raw)
To: Carl Karsten; +Cc: video4linux-list
Am Sonntag, den 02.03.2008, 17:02 -0600 schrieb Carl Karsten:
> hermann pitton wrote:
> > Hi Carl,
> >
> > Am Sonntag, den 02.03.2008, 00:42 -0600 schrieb Carl Karsten:
> >> I copied the command line parameter support from test/pixfmt-test.c, and used
> >> the prt_caps() func from lib/v4l2_driver.c.
> >>
> >> I am hoping to merge all of the test code into one big test, and put all the
> >> generic code into one lib.
> >>
> >> Carl K
> >>
> >
> > that looks interesting and should save time looking up the ioctls in the
> > code etc.
>
> There is more to come :)
>
> >
> > But please don't post any patches inline with thunderbird, try as
> > attachment or use another mail client.
>
> better?
at least functional ...
> > Concerning newly included headers, at what kernel environment you get
> > this to compile? My stuff still seems to be too old.
>
> Oh, didn't include the Makefile, probably what is causing you problems. I tried
> to keep my vivi patch separate.
>
> Carl K
>
Ah, also without line breakage at vivi now.
Should be worth another try :)
Cheers,
Hermann
--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2008-03-02 23:23 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-03-02 6:42 [patch] ioctl-test.c Carl Karsten
2008-03-02 22:10 ` hermann pitton
2008-03-02 23:02 ` Carl Karsten
2008-03-02 23:15 ` hermann pitton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox