summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2010-05-19 00:15:52 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2010-05-19 00:15:52 +0200
commitabbd1a3b585c685426196f066ad7fd417ed730a0 (patch)
tree1c02e17f5f44470552ae22ab9fcb3a031c37f530
Initial import
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
-rw-r--r--COPYING.GPL339
-rw-r--r--Makefile19
-rw-r--r--main.c402
-rw-r--r--media.c433
-rw-r--r--media.h66
-rw-r--r--options.c117
-rw-r--r--options.h40
-rw-r--r--subdev.c96
-rw-r--r--subdev.h35
-rw-r--r--tools.h27
10 files changed, 1574 insertions, 0 deletions
diff --git a/COPYING.GPL b/COPYING.GPL
new file mode 100644
index 0000000..d511905
--- /dev/null
+++ b/COPYING.GPL
@@ -0,0 +1,339 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users. This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it. (Some other Free Software Foundation software is covered by
+the GNU Lesser General Public License instead.) You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+ To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have. You must make sure that they, too, receive or can get the
+source code. And you must show them these terms so they know their
+rights.
+
+ We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+ Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software. If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+ Finally, any free program is threatened constantly by software
+patents. We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary. To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ GNU GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License. The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language. (Hereinafter, translation is included without limitation in
+the term "modification".) Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+ 1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+ 2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) You must cause the modified files to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ b) You must cause any work that you distribute or publish, that in
+ whole or in part contains or is derived from the Program or any
+ part thereof, to be licensed as a whole at no charge to all third
+ parties under the terms of this License.
+
+ c) If the modified program normally reads commands interactively
+ when run, you must cause it, when started running for such
+ interactive use in the most ordinary way, to print or display an
+ announcement including an appropriate copyright notice and a
+ notice that there is no warranty (or else, saying that you provide
+ a warranty) and that users may redistribute the program under
+ these conditions, and telling the user how to view a copy of this
+ License. (Exception: if the Program itself is interactive but
+ does not normally print such an announcement, your work based on
+ the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+ a) Accompany it with the complete corresponding machine-readable
+ source code, which must be distributed under the terms of Sections
+ 1 and 2 above on a medium customarily used for software interchange; or,
+
+ b) Accompany it with a written offer, valid for at least three
+ years, to give any third party, for a charge no more than your
+ cost of physically performing source distribution, a complete
+ machine-readable copy of the corresponding source code, to be
+ distributed under the terms of Sections 1 and 2 above on a medium
+ customarily used for software interchange; or,
+
+ c) Accompany it with the information you received as to the offer
+ to distribute corresponding source code. (This alternative is
+ allowed only for noncommercial distribution and only if you
+ received the program in object code or executable form with such
+ an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it. For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable. However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License. Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+ 5. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Program or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+ 6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+ 7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all. For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded. In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+ 9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation. If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+ 10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission. For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this. Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+ NO WARRANTY
+
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+ Gnomovision version 69, Copyright (C) year name of author
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+ <signature of Ty Coon>, 1 April 1989
+ Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs. If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library. If this is what you want to do, use the GNU Lesser General
+Public License instead of this License.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..bf4cf55
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,19 @@
+SRCARCH ?= arm
+CROSS_COMPILE ?= arm-none-linux-gnueabi-
+KDIR ?= /usr/src/linux
+
+KINC := -I$(KDIR)/include -I$(KDIR)/arch/$(SRCARCH)/include
+CC := $(CROSS_COMPILE)gcc
+
+CFLAGS = -O2 -Wall -fpic -I. $(KINC)
+OBJS = media.o main.o options.o subdev.o
+
+all: media-ctl
+
+media-ctl: $(OBJS)
+ $(CC) $(CFLAGS) -o media-ctl $(OBJS)
+ $(CROSS_COMPILE)strip media-ctl
+clean:
+ rm -f $(OBJS) media-ctl
+
+.PHONY: clean all
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..ab76c84
--- /dev/null
+++ b/main.c
@@ -0,0 +1,402 @@
+/*
+ * Media controller test application
+ *
+ * Copyright (C) 2010 Ideas on board SPRL <laurent.pinchart@ideasonboard.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ */
+
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <linux/types.h>
+#include <linux/media.h>
+#include <linux/v4l2-mediabus.h>
+#include <linux/v4l2-subdev.h>
+#include <linux/videodev2.h>
+
+#include "media.h"
+#include "options.h"
+#include "subdev.h"
+#include "tools.h"
+
+/* -----------------------------------------------------------------------------
+ * Links setup
+ */
+
+static struct media_entity_pad *parse_pad(struct media_device *media, const char *p, char **endp)
+{
+ unsigned int entity_id, pad;
+ struct media_entity *entity;
+ char *end;
+
+ for (; isspace(*p); ++p);
+
+ if (*p == '"') {
+ for (end = (char *)p + 1; *end && *end != '"'; ++end);
+ if (*end != '"')
+ return NULL;
+
+ entity = media_get_entity_by_name(media, p + 1, end - p - 1);
+ if (entity == NULL)
+ return NULL;
+
+ for (++end; isspace(*end); ++end);
+ } else {
+ entity_id = strtoul(p, &end, 10) - 1;
+ if (entity_id >= media->entities_count)
+ return NULL;
+
+ entity = &media->entities[entity_id];
+ }
+
+ if (*end != ':')
+ return NULL;
+ for (p = end + 1; isspace(*p); ++p);
+
+ pad = strtoul(p, &end, 10);
+ for (p = end; isspace(*p); ++p);
+
+ if (pad >= entity->info.pads)
+ return NULL;
+
+ for (p = end; isspace(*p); ++p);
+ *endp = (char *)p;
+
+ return &entity->pads[pad];
+}
+
+static struct media_entity_link *parse_link(struct media_device *media, const char *p, char **endp)
+{
+ struct media_entity_link *link;
+ struct media_entity_pad *source;
+ struct media_entity_pad *sink;
+ unsigned int i;
+ char *end;
+
+ source = parse_pad(media, p, &end);
+ if (source == NULL)
+ return NULL;
+
+ if (end[0] != '-' || end[1] != '>')
+ return NULL;
+ p = end + 2;
+
+ sink = parse_pad(media, p, &end);
+ if (sink == NULL)
+ return NULL;
+
+ *endp = end;
+
+ for (i = 0; i < source->entity->info.links; i++) {
+ link = &source->entity->links[i];
+
+ if (link->source == source && link->sink == sink)
+ return link;
+ }
+
+ return NULL;
+}
+
+static int setup_link(struct media_device *media, const char *p, char **endp)
+{
+ struct media_entity_link *link;
+ __u32 flags;
+ char *end;
+
+ link = parse_link(media, p, &end);
+ if (link == NULL) {
+ printf("Unable to parse link\n");
+ return -EINVAL;
+ }
+
+ p = end;
+ if (*p++ != '[') {
+ printf("Unable to parse link flags\n");
+ return -EINVAL;
+ }
+
+ flags = strtoul(p, &end, 10);
+ for (p = end; isspace(*p); p++);
+ if (*p++ != ']') {
+ printf("Unable to parse link flags\n");
+ return -EINVAL;
+ }
+
+ for (; isspace(*p); p++);
+ *endp = (char *)p;
+
+ printf("Setting up link %u:%u -> %u:%u [%u]\n",
+ link->source->entity->info.id, link->source->index,
+ link->sink->entity->info.id, link->sink->index,
+ flags);
+
+ return media_setup_link(media, link->source, link->sink, flags);
+}
+
+static int setup_links(struct media_device *media, const char *p)
+{
+ char *end;
+ int ret;
+
+ do {
+ ret = setup_link(media, p, &end);
+ if (ret < 0)
+ return ret;
+
+ p = end + 1;
+ } while (*end == ',');
+
+ return *end ? -EINVAL : 0;
+}
+
+/* -----------------------------------------------------------------------------
+ * Formats setup
+ */
+
+static struct {
+ const char *name;
+ enum v4l2_mbus_pixelcode code;
+} mbus_formats[] = {
+ { "YUYV", V4L2_MBUS_FMT_YUYV16_1X16 },
+ { "UYVY", V4L2_MBUS_FMT_UYVY16_1X16 },
+ { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
+ { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
+};
+
+static const char *pixelcode_to_string(enum v4l2_mbus_pixelcode code)
+{
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
+ if (mbus_formats[i].code == code)
+ return mbus_formats[i].name;
+ }
+
+ return "unknown";
+}
+
+static int parse_format(struct v4l2_mbus_framefmt *format, const char *p, char **endp)
+{
+ unsigned int width, height;
+ unsigned int i;
+ char *end;
+
+ for (; isspace(*p); ++p);
+ for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
+
+ for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
+ if (strncmp(mbus_formats[i].name, p, end - p) == 0)
+ break;
+ }
+
+ if (i == ARRAY_SIZE(mbus_formats))
+ return -EINVAL;
+
+ for (p = end; isspace(*p); ++p);
+ width = strtoul(p, &end, 10);
+ if (*end != 'x')
+ return -EINVAL;
+
+ p = end + 1;
+ height = strtoul(p, &end, 10);
+ *endp = end;
+
+ format->width = width;
+ format->height = height;
+ format->code = mbus_formats[i].code;
+
+ return 0;
+}
+
+static struct media_entity_pad *parse_pad_format(struct media_device *media,
+ struct v4l2_mbus_framefmt *format, const char *p, char **endp)
+{
+ struct media_entity_pad *pad;
+ char *end;
+ int ret;
+
+ for (; isspace(*p); ++p);
+
+ pad = parse_pad(media, p, &end);
+ if (pad == NULL)
+ return NULL;
+
+ for (p = end; isspace(*p); ++p);
+ if (*p++ != '[')
+ return NULL;
+
+ ret = parse_format(format, p, &end);
+ if (ret < 0)
+ return NULL;
+
+ for (p = end; isspace(*p); p++);
+ if (*p++ != ']')
+ return NULL;
+
+ *endp = (char *)p;
+ return pad;
+}
+
+static int set_format(struct media_entity_pad *pad, struct v4l2_mbus_framefmt *format)
+{
+ int ret;
+
+ printf("Setting up format %s %ux%u on pad %s/%u\n",
+ pixelcode_to_string(format->code), format->width, format->height,
+ pad->entity->info.name, pad->index);
+
+ ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
+ V4L2_SUBDEV_FORMAT_ACTIVE);
+ if (ret < 0) {
+ printf("Unable to set format: %s(%u)", strerror(ret), ret);
+ return ret;
+ }
+
+ printf("Format set: %s %ux%u\n",
+ pixelcode_to_string(format->code), format->width, format->height);
+
+ return 0;
+}
+
+static int setup_format(struct media_device *media, const char *p, char **endp)
+{
+ struct v4l2_mbus_framefmt format;
+ struct media_entity_pad *pad;
+ unsigned int i;
+ char *end;
+ int ret;
+
+ pad = parse_pad_format(media, &format, p, &end);
+ if (pad == NULL) {
+ printf("Unable to parse format\n");
+ return -EINVAL;
+ }
+
+ ret = set_format(pad, &format);
+ if (ret < 0)
+ return ret;
+
+ /* If the pad is an output pad, automatically set the same format on
+ * the remote inputs, if any.
+ */
+ if (pad->type == MEDIA_PAD_TYPE_OUTPUT) {
+ for (i = 0; i < pad->entity->info.links; ++i) {
+ struct media_entity_link *link = &pad->entity->links[i];
+ struct v4l2_mbus_framefmt remote_format;
+
+ if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
+ continue;
+
+ if (link->source == pad) {
+ remote_format = format;
+ set_format(link->sink, &remote_format);
+ }
+ }
+ }
+
+ *endp = end;
+ return 0;
+}
+
+static int setup_formats(struct media_device *media, const char *p)
+{
+ char *end;
+ int ret;
+
+ do {
+ ret = setup_format(media, p, &end);
+ if (ret < 0) {
+ printf("Unable to parse format\n");
+ return ret;
+ }
+
+ p = end + 1;
+ } while (*end == ',');
+
+ return *end ? -EINVAL : 0;
+}
+
+int main(int argc, char **argv)
+{
+ struct media_device *media;
+
+ if (parse_cmdline(argc, argv))
+ return EXIT_FAILURE;
+
+ /* Open the media device and enumerate entities, pads and links. */
+ media = media_open(media_opts.devname, media_opts.verbose);
+ if (media == NULL)
+ goto out;
+
+ if (media_opts.entity) {
+ struct media_entity *entity;
+
+ entity = media_get_entity_by_name(media, media_opts.entity,
+ strlen(media_opts.entity));
+ if (entity != NULL)
+ printf("%s\n", entity->devname);
+ else
+ printf("Entity '%s' not found\n", media_opts.entity);
+ }
+
+ if (media_opts.print) {
+ media_print_topology(media);
+ printf("\n");
+ }
+
+ if (media_opts.reset) {
+ printf("Resetting all links to inactive\n");
+ media_reset_links(media);
+ }
+
+ if (media_opts.links)
+ setup_links(media, media_opts.links);
+
+ if (media_opts.formats)
+ setup_formats(media, media_opts.formats);
+
+ if (media_opts.interactive) {
+ while (1) {
+ char buffer[32];
+ char *end;
+
+ printf("Enter a link to modify or enter to stop\n");
+ if (fgets(buffer, sizeof buffer, stdin) == NULL)
+ break;
+
+ if (buffer[0] == '\n')
+ break;
+
+ setup_link(media, buffer, &end);
+ }
+ }
+
+out:
+ if (media)
+ media_close(media);
+
+ exit(EXIT_SUCCESS);
+}
+
diff --git a/media.c b/media.c
new file mode 100644
index 0000000..81c730c
--- /dev/null
+++ b/media.c
@@ -0,0 +1,433 @@
+/*
+ * Media controller test application
+ *
+ * Copyright (C) 2010 Ideas on board SPRL <laurent.pinchart@ideasonboard.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ */
+
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include <linux/videodev2.h>
+#include <linux/media.h>
+
+#include "media.h"
+#include "tools.h"
+
+static const char *media_entity_type_to_string(unsigned type)
+{
+ static const struct {
+ __u32 type;
+ const char *name;
+ } types[] = {
+ { MEDIA_ENTITY_TYPE_NODE, "Node" },
+ { MEDIA_ENTITY_TYPE_SUBDEV, "V4L2 subdev" },
+ };
+
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(types); i++) {
+ if (types[i].type == type)
+ return types[i].name;
+ }
+
+ return "Unknown";
+}
+
+static const char *media_entity_subtype_to_string(unsigned type, unsigned subtype)
+{
+ static const char *node_types[] = {
+ "Unknown",
+ "V4L",
+ "FB",
+ "ALSA",
+ "DVB",
+ };
+ static const char *subdev_types[] = {
+ "Unknown",
+ "Video Decoder",
+ "Video Encoder",
+ "Miscellaneous",
+ };
+
+ switch (type) {
+ case MEDIA_ENTITY_TYPE_NODE:
+ if (subtype > 4)
+ subtype = 0;
+ return node_types[subtype];
+
+ case MEDIA_ENTITY_TYPE_SUBDEV:
+ if (subtype > 3)
+ subtype = 0;
+ return subdev_types[subtype];
+ default:
+ return node_types[0];
+ }
+}
+
+/*
+ * media_entity_remote_pad -
+ */
+struct media_entity_pad *media_entity_remote_pad(struct media_entity_pad *pad)
+{
+ unsigned int i;
+
+ for (i = 0; i < pad->entity->info.links; ++i) {
+ struct media_entity_link *link = &pad->entity->links[i];
+
+ if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
+ continue;
+
+ if (link->source == pad)
+ return link->sink;
+
+ if (link->sink == pad)
+ return link->source;
+ }
+
+ return NULL;
+}
+
+/*
+ * media_entity_by_name -
+ */
+struct media_entity *media_get_entity_by_name(struct media_device *media,
+ const char *name, size_t length)
+{
+ unsigned int i;
+
+ for (i = 0; i < media->entities_count; ++i) {
+ struct media_entity *entity = &media->entities[i];
+
+ if (strncmp(entity->info.name, name, length) == 0)
+ return entity;
+ }
+
+ return NULL;
+}
+
+/*
+ * media_setup_link -
+ */
+int media_setup_link(struct media_device *media,
+ struct media_entity_pad *source,
+ struct media_entity_pad *sink,
+ __u32 flags)
+{
+ struct media_entity_link *link;
+ struct media_user_link ulink;
+ unsigned int i;
+ int ret;
+
+ for (i = 0; i < source->entity->info.links; i++) {
+ link = &source->entity->links[i];
+
+ if (link->source->entity == source->entity &&
+ link->source->index == source->index &&
+ link->sink->entity == sink->entity &&
+ link->sink->index == sink->index)
+ break;
+ }
+
+ if (i == source->entity->info.links) {
+ printf("%s: Link not found\n", __func__);
+ return -EINVAL;
+ }
+
+ /* source pad */
+ ulink.source.entity = source->entity->info.id;
+ ulink.source.index = source->index;
+ ulink.source.type = MEDIA_PAD_TYPE_OUTPUT;
+
+ /* sink pad */
+ ulink.sink.entity = sink->entity->info.id;
+ ulink.sink.index = sink->index;
+ ulink.sink.type = MEDIA_PAD_TYPE_INPUT;
+
+ ulink.flags = flags | (link->flags & MEDIA_LINK_FLAG_IMMUTABLE);
+
+ ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
+ if (ret < 0) {
+ printf("%s: Unable to setup link (%s)\n", __func__,
+ strerror(errno));
+ return ret;
+ }
+
+ link->flags = flags;
+ return 0;
+}
+
+int media_reset_links(struct media_device *media)
+{
+ unsigned int i, j;
+ int ret;
+
+ for (i = 0; i < media->entities_count; ++i) {
+ struct media_entity *entity = &media->entities[i];
+
+ for (j = 0; j < entity->info.links; j++) {
+ struct media_entity_link *link = &entity->links[j];
+
+ if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
+ continue;
+
+ ret = media_setup_link(media, link->source, link->sink,
+ link->flags & ~MEDIA_LINK_FLAG_ACTIVE);
+ if (ret < 0)
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+void media_print_topology(struct media_device *media)
+{
+ unsigned int i, j, k;
+ unsigned int padding;
+
+ printf("Device topology\n");
+
+ for (i = 0; i < media->entities_count; ++i) {
+ struct media_entity *entity = &media->entities[i];
+
+ padding = printf("- entity %u: ", entity->info.id);
+ printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
+ entity->info.pads, entity->info.pads > 1 ? "s" : "",
+ entity->info.links, entity->info.links > 1 ? "s" : "");
+ printf("%*ctype %s subtype %s\n", padding, ' ',
+ media_entity_type_to_string(entity->info.type),
+ media_entity_subtype_to_string(entity->info.type, entity->info.subtype));
+ if (entity->devname)
+ printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
+
+ for (j = 0; j < entity->info.pads; j++) {
+ for (k = 0; k < entity->info.links; k++) {
+ struct media_entity_link *link = &entity->links[k];
+
+ if (link->source->entity != entity ||
+ link->source->index != j)
+ continue;
+
+ printf("\tpad%u -> '%s':pad%u [", link->source->index,
+ link->sink->entity->info.name, link->sink->index);
+
+ if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
+ printf("IMMUTABLE,");
+ if (link->flags & MEDIA_LINK_FLAG_ACTIVE)
+ printf("ACTIVE");
+
+ printf("]\n");
+ }
+ }
+ printf("\n");
+ }
+}
+
+static int media_enum_links(struct media_device *media)
+{
+ __u32 id;
+ int ret = 0;
+
+ for (id = 1; id <= media->entities_count; id++) {
+ struct media_entity *entity = &media->entities[id - 1];
+ struct media_user_links links;
+ unsigned int i;
+
+ links.entity = entity->info.id;
+ links.pads = malloc(entity->info.pads * sizeof(struct media_user_pad));
+ links.links = malloc(entity->info.links * sizeof(struct media_user_link));
+
+ if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
+ printf("%s: Unable to enumerate pads and links (%s).\n",
+ __func__, strerror(errno));
+ free(links.pads);
+ free(links.links);
+ return -errno;
+ }
+
+ for (i = 0; i < entity->info.pads; ++i) {
+ entity->pads[i].entity = entity;
+ entity->pads[i].type = links.pads[i].type;
+ entity->pads[i].index = links.pads[i].index;
+ }
+
+ for (i = 0; i < entity->info.links; ++i) {
+ struct media_user_link *link = &links.links[i];
+ struct media_entity *source;
+ struct media_entity *sink;
+
+ if (link->source.entity > media->entities_count ||
+ link->sink.entity > media->entities_count) {
+ printf("WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
+ id, i, link->source.entity, link->source.index,
+ link->sink.entity, link->sink.index);
+ ret = -EINVAL;
+ }
+
+ source = &media->entities[link->source.entity - 1];
+ sink = &media->entities[link->sink.entity - 1];
+ entity->links[i].source = &source->pads[link->source.index];
+ entity->links[i].sink = &sink->pads[link->sink.index];
+ entity->links[i].flags = links.links[i].flags;
+ }
+
+ free(links.pads);
+ free(links.links);
+ }
+
+ return ret;
+}
+
+static int media_enum_entities(struct media_device *media)
+{
+ struct media_entity *entity;
+ struct stat devstat;
+ char devname[32];
+ unsigned int i;
+ __u32 id;
+ int ret;
+
+ for (id = 1; ; id++) {
+ media->entities =
+ realloc(media->entities, id * sizeof(*media->entities));
+ entity = &media->entities[id - 1];
+ memset(entity, 0, sizeof(*entity));
+ entity->fd = -1;
+ entity->info.id = id;
+
+ ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
+ if (ret < 0) {
+ if (errno == EINVAL)
+ break;
+ return -errno;
+ }
+
+ entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
+ entity->links = malloc(entity->info.links * sizeof(*entity->links));
+ if (entity->pads == NULL || entity->links == NULL)
+ return -ENOMEM;
+
+ media->entities_count++;
+
+ /* Find the corresponding device name. */
+ if ((entity->info.type != MEDIA_ENTITY_TYPE_NODE ||
+ entity->info.type != MEDIA_NODE_TYPE_V4L) &&
+ (entity->info.type != MEDIA_ENTITY_TYPE_SUBDEV))
+ continue;
+
+ for (i = 0; i < 256; ++i) {
+ if (entity->info.type == MEDIA_ENTITY_TYPE_NODE)
+ sprintf(devname, "/dev/video%u", i);
+ else
+ sprintf(devname, "/dev/subdev%u", i);
+
+ ret = stat(devname, &devstat);
+ if (ret < 0)
+ continue;
+
+ if (major(devstat.st_rdev) == entity->info.v4l.major &&
+ minor(devstat.st_rdev) == entity->info.v4l.minor) {
+ strcpy(entity->devname, devname);
+ break;
+ }
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * media_open -
+ */
+struct media_device *media_open(const char *name, int verbose)
+{
+ struct media_device *media;
+ int ret;
+
+ media = malloc(sizeof(*media));
+ if (media == NULL) {
+ printf("%s: unable to allocate memory\n", __func__);
+ return NULL;
+ }
+ memset(media, 0, sizeof(*media));
+
+ if (verbose)
+ printf("Opening media device %s\n", name);
+ media->fd = open(name, O_RDWR);
+ if (media->fd < 0) {
+ media_close(media);
+ printf("%s: Can't open media device %s\n", __func__, name);
+ return NULL;
+ }
+
+ if (verbose)
+ printf("Enumerating entities\n");
+
+ ret = media_enum_entities(media);
+ if (ret < 0) {
+ printf("%s: Unable to enumerate entities for device %s (%s)\n",
+ __func__, name, strerror(ret));
+ media_close(media);
+ return NULL;
+ }
+
+ if (verbose) {
+ printf("Found %u entities\n", media->entities_count);
+ printf("Enumerating pads and links\n");
+ }
+
+ ret = media_enum_links(media);
+ if (ret < 0) {
+ printf("%s: Unable to enumerate pads and linksfor device %s\n",
+ __func__, name);
+ media_close(media);
+ return NULL;
+ }
+
+ return media;
+}
+
+/*
+ * media_close -
+ */
+void media_close(struct media_device *media)
+{
+ unsigned int i;
+
+ if (media->fd != -1)
+ close(media->fd);
+
+ for (i = 0; i < media->entities_count; ++i) {
+ struct media_entity *entity = &media->entities[i];
+
+ free(entity->pads);
+ free(entity->links);
+ if (entity->fd != -1)
+ close(entity->fd);
+ }
+
+ free(media->entities);
+ free(media);
+}
+
diff --git a/media.h b/media.h
new file mode 100644
index 0000000..7217403
--- /dev/null
+++ b/media.h
@@ -0,0 +1,66 @@
+/*
+ * Media controller test application
+ *
+ * Copyright (C) 2010 Ideas on board SPRL <laurent.pinchart@ideasonboard.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ */
+
+#ifndef __MEDIA_H__
+#define __MEDIA_H__
+
+#include <linux/media.h>
+
+struct media_entity_link {
+ struct media_entity_pad *source;
+ struct media_entity_pad *sink;
+ __u32 flags;
+};
+
+struct media_entity_pad {
+ struct media_entity *entity;
+ __u32 type;
+ __u32 index;
+};
+
+struct media_entity {
+ struct media_user_entity info;
+ struct media_entity_pad *pads;
+ struct media_entity_link *links;
+
+ char devname[32];
+ int fd;
+};
+
+struct media_device {
+ int fd;
+ struct media_entity *entities;
+ unsigned int entities_count;
+};
+
+struct media_device *media_open(const char *name, int verbose);
+void media_close(struct media_device *media);
+
+struct media_entity_pad *media_entity_remote_pad(struct media_entity_pad *pad);
+
+struct media_entity *media_get_entity_by_name(struct media_device *media,
+ const char *name, size_t length);
+void media_print_topology(struct media_device *media);
+int media_setup_link(struct media_device *media,
+ struct media_entity_pad *source, struct media_entity_pad *sink,
+ __u32 flags);
+int media_reset_links(struct media_device *media);
+
+#endif
+
diff --git a/options.c b/options.c
new file mode 100644
index 0000000..c607dee
--- /dev/null
+++ b/options.c
@@ -0,0 +1,117 @@
+/*
+ * Media controller test application
+ *
+ * Copyright (C) 2010 Ideas on board SPRL <laurent.pinchart@ideasonboard.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ */
+
+#include <stdlib.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "options.h"
+
+#define MEDIA_DEVNAME_DEFAULT "/dev/media0"
+
+struct media_options media_opts = {
+ .devname = MEDIA_DEVNAME_DEFAULT,
+};
+
+static void usage(const char *argv0)
+{
+ printf("%s [options] device\n", argv0);
+ printf("-d, --device dev Media device name (default: %s)\n", MEDIA_DEVNAME_DEFAULT);
+ printf("-e, --entity name Print the device name associated with the given entity\n");
+ printf("-f, --formats Comma-separated list of formats to setup\n");
+ printf("-h, --help Show this help screen\n");
+ printf("-i, --interactive Modify links interactively\n");
+ printf("-l, --links Comma-separated list of links descriptors to setup\n");
+ printf("-p, --print-topology Print the device topology (implies -v)\n");
+ printf("-r, --reset Reset all links to inactive\n");
+ printf("-v, --verbose Be verbose\n");
+}
+
+static struct option opts[] = {
+ {"device", 1, 0, 'd'},
+ {"entity", 1, 0, 'e'},
+ {"formats", 1, 0, 'f'},
+ {"help", 0, 0, 'h'},
+ {"interactive", 0, 0, 'i'},
+ {"links", 1, 0, 'l'},
+ {"print-topology", 0, 0, 'p'},
+ {"reset", 0, 0, 'r'},
+ {"verbose", 0, 0, 'v'},
+};
+
+int parse_cmdline(int argc, char **argv)
+{
+ int opt;
+
+ if (argc == 1) {
+ usage(argv[0]);
+ return 1;
+ }
+
+ /* parse options */
+ while ((opt = getopt_long(argc, argv, "d:e:f:hil:prv", opts, NULL)) != -1) {
+ switch (opt) {
+ case 'd':
+ media_opts.devname = optarg;
+ break;
+
+ case 'e':
+ media_opts.entity = optarg;
+ break;
+
+ case 'f':
+ media_opts.formats = optarg;
+ break;
+
+ case 'h':
+ usage(argv[0]);
+ return 1;
+
+ case 'i':
+ media_opts.interactive = 1;
+ break;
+
+ case 'l':
+ media_opts.links = optarg;
+ break;
+
+ case 'p':
+ media_opts.print = 1;
+ media_opts.verbose = 1;
+ break;
+
+ case 'r':
+ media_opts.reset = 1;
+ break;
+
+ case 'v':
+ media_opts.verbose = 1;
+ break;
+
+ default:
+ printf("Invalid option -%c\n", opt);
+ printf("Run %s -h for help.\n", argv[0]);
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
diff --git a/options.h b/options.h
new file mode 100644
index 0000000..a490a65
--- /dev/null
+++ b/options.h
@@ -0,0 +1,40 @@
+/*
+ * Media controller test application
+ *
+ * Copyright (C) 2010 Ideas on board SPRL <laurent.pinchart@ideasonboard.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ */
+
+#ifndef __OPTIONS_H
+#define __OPTIONS_H
+
+struct media_options
+{
+ const char *devname;
+ unsigned int interactive:1,
+ print:1,
+ reset:1,
+ verbose:1;
+ const char *entity;
+ const char *formats;
+ const char *links;
+};
+
+extern struct media_options media_opts;
+
+extern int parse_cmdline(int argc, char **argv);
+
+#endif /* __OPTIONS_H */
+
diff --git a/subdev.c b/subdev.c
new file mode 100644
index 0000000..224b066
--- /dev/null
+++ b/subdev.c
@@ -0,0 +1,96 @@
+/*
+ * Media controller test application
+ *
+ * Copyright (C) 2010 Ideas on board SPRL <laurent.pinchart@ideasonboard.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ */
+
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <linux/v4l2-subdev.h>
+
+#include "media.h"
+#include "subdev.h"
+
+static int v4l2_subdev_open(struct media_entity *entity)
+{
+ if (entity->fd != -1)
+ return 0;
+
+ entity->fd = open(entity->devname, O_RDWR);
+ if (entity->fd == -1) {
+ printf("%s: Failed to open subdev device node %s\n", __func__,
+ entity->devname);
+ return errno;
+ }
+
+ return 0;
+}
+
+int v4l2_subdev_get_format(struct media_entity *entity,
+ struct v4l2_mbus_framefmt *format, unsigned int pad,
+ enum v4l2_subdev_format which)
+{
+ struct v4l2_subdev_pad_format fmt;
+ int ret;
+
+ ret = v4l2_subdev_open(entity);
+ if (ret < 0)
+ return ret;
+
+ memset(&fmt, 0, sizeof(fmt));
+ fmt.pad = pad;
+ fmt.which = which;
+
+ ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FMT, &fmt);
+ if (ret < 0)
+ return errno;
+
+ *format = fmt.format;
+ return 0;
+}
+
+int v4l2_subdev_set_format(struct media_entity *entity,
+ struct v4l2_mbus_framefmt *format, unsigned int pad,
+ enum v4l2_subdev_format which)
+{
+ struct v4l2_subdev_pad_format fmt;
+ int ret;
+
+ ret = v4l2_subdev_open(entity);
+ if (ret < 0)
+ return ret;
+
+ memset(&fmt, 0, sizeof(fmt));
+ fmt.pad = pad;
+ fmt.which = which;
+ fmt.format = *format;
+
+ ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FMT, &fmt);
+ if (ret < 0)
+ return errno;
+
+ *format = fmt.format;
+ return 0;
+}
+
diff --git a/subdev.h b/subdev.h
new file mode 100644
index 0000000..aeb92d3
--- /dev/null
+++ b/subdev.h
@@ -0,0 +1,35 @@
+/*
+ * Media controller test application
+ *
+ * Copyright (C) 2010 Ideas on board SPRL <laurent.pinchart@ideasonboard.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ */
+
+#ifndef __SUBDEV_H__
+#define __SUBDEV_H__
+
+#include <linux/v4l2-subdev.h>
+
+struct media_entity;
+
+int v4l2_subdev_get_format(struct media_entity *entity,
+ struct v4l2_mbus_framefmt *format, unsigned int pad,
+ enum v4l2_subdev_format which);
+int v4l2_subdev_set_format(struct media_entity *entity,
+ struct v4l2_mbus_framefmt *format, unsigned int pad,
+ enum v4l2_subdev_format which);
+
+#endif
+
diff --git a/tools.h b/tools.h
new file mode 100644
index 0000000..0a0a948
--- /dev/null
+++ b/tools.h
@@ -0,0 +1,27 @@
+/*
+ * Media controller test application
+ *
+ * Copyright (C) 2010 Ideas on board SPRL <laurent.pinchart@ideasonboard.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef __TOOLS_H__
+#define __TOOLS_H__
+
+#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
+
+#endif /* __TOOLS_H__ */
+