2 * Media controller test application
4 * Copyright (C) 2010-2011 Ideas on board SPRL
6 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published
10 * by the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/ioctl.h>
36 #include <linux/media.h>
37 #include <linux/types.h>
38 #include <linux/v4l2-mediabus.h>
39 #include <linux/v4l2-subdev.h>
40 #include <linux/videodev2.h>
45 #include "v4l2subdev.h"
47 /* -----------------------------------------------------------------------------
51 static void v4l2_subdev_print_format(struct media_entity *entity,
52 unsigned int pad, enum v4l2_subdev_format_whence which)
54 struct v4l2_mbus_framefmt format;
55 struct v4l2_rect rect;
58 ret = v4l2_subdev_get_format(entity, &format, pad, which);
62 printf("\t\t[fmt:%s/%ux%u",
63 v4l2_subdev_pixelcode_to_string(format.code),
64 format.width, format.height);
66 ret = v4l2_subdev_get_selection(entity, &rect, pad,
67 V4L2_SEL_TGT_CROP_BOUNDS,
70 printf("\n\t\t crop.bounds:(%u,%u)/%ux%u", rect.left, rect.top,
71 rect.width, rect.height);
73 ret = v4l2_subdev_get_selection(entity, &rect, pad,
77 printf("\n\t\t crop:(%u,%u)/%ux%u", rect.left, rect.top,
78 rect.width, rect.height);
80 ret = v4l2_subdev_get_selection(entity, &rect, pad,
81 V4L2_SEL_TGT_COMPOSE_BOUNDS,
84 printf("\n\t\t compose.bounds:(%u,%u)/%ux%u",
85 rect.left, rect.top, rect.width, rect.height);
87 ret = v4l2_subdev_get_selection(entity, &rect, pad,
91 printf("\n\t\t compose:(%u,%u)/%ux%u",
92 rect.left, rect.top, rect.width, rect.height);
97 static const char *media_entity_type_to_string(unsigned type)
103 { MEDIA_ENT_T_DEVNODE, "Node" },
104 { MEDIA_ENT_T_V4L2_SUBDEV, "V4L2 subdev" },
109 type &= MEDIA_ENT_TYPE_MASK;
111 for (i = 0; i < ARRAY_SIZE(types); i++) {
112 if (types[i].type == type)
113 return types[i].name;
119 static const char *media_entity_subtype_to_string(unsigned type)
121 static const char *node_types[] = {
128 static const char *subdev_types[] = {
135 unsigned int subtype = type & MEDIA_ENT_SUBTYPE_MASK;
137 switch (type & MEDIA_ENT_TYPE_MASK) {
138 case MEDIA_ENT_T_DEVNODE:
139 if (subtype >= ARRAY_SIZE(node_types))
141 return node_types[subtype];
143 case MEDIA_ENT_T_V4L2_SUBDEV:
144 if (subtype >= ARRAY_SIZE(subdev_types))
146 return subdev_types[subtype];
148 return node_types[0];
152 static const char *media_pad_type_to_string(unsigned flag)
154 static const struct {
158 { MEDIA_PAD_FL_SINK, "Sink" },
159 { MEDIA_PAD_FL_SOURCE, "Source" },
164 for (i = 0; i < ARRAY_SIZE(flags); i++) {
165 if (flags[i].flag & flag)
166 return flags[i].name;
172 static void media_print_topology_dot(struct media_device *media)
174 unsigned int nents = media_get_entities_count(media);
177 printf("digraph board {\n");
178 printf("\trankdir=TB\n");
180 for (i = 0; i < nents; ++i) {
181 struct media_entity *entity = media_get_entity(media, i);
182 const struct media_entity_desc *info = media_entity_get_info(entity);
183 const char *devname = media_entity_get_devname(entity);
184 unsigned int num_links = media_entity_get_links_count(entity);
187 switch (media_entity_type(entity)) {
188 case MEDIA_ENT_T_DEVNODE:
189 printf("\tn%08x [label=\"%s\\n%s\", shape=box, style=filled, "
190 "fillcolor=yellow]\n",
191 info->id, info->name, devname);
194 case MEDIA_ENT_T_V4L2_SUBDEV:
195 printf("\tn%08x [label=\"{{", info->id);
197 for (j = 0, npads = 0; j < info->pads; ++j) {
198 const struct media_pad *pad = media_entity_get_pad(entity, j);
200 if (!(pad->flags & MEDIA_PAD_FL_SINK))
203 printf("%s<port%u> %u", npads ? " | " : "", j, j);
207 printf("} | %s", info->name);
209 printf("\\n%s", devname);
212 for (j = 0, npads = 0; j < info->pads; ++j) {
213 const struct media_pad *pad = media_entity_get_pad(entity, j);
215 if (!(pad->flags & MEDIA_PAD_FL_SOURCE))
218 printf("%s<port%u> %u", npads ? " | " : "", j, j);
222 printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n");
229 for (j = 0; j < num_links; j++) {
230 const struct media_link *link = media_entity_get_link(entity, j);
231 const struct media_pad *source = link->source;
232 const struct media_pad *sink = link->sink;
234 if (source->entity != entity)
237 printf("\tn%08x", media_entity_get_info(source->entity)->id);
238 if (media_entity_type(source->entity) == MEDIA_ENT_T_V4L2_SUBDEV)
239 printf(":port%u", source->index);
241 printf("n%08x", media_entity_get_info(sink->entity)->id);
242 if (media_entity_type(sink->entity) == MEDIA_ENT_T_V4L2_SUBDEV)
243 printf(":port%u", sink->index);
245 if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
246 printf(" [style=bold]");
247 else if (!(link->flags & MEDIA_LNK_FL_ENABLED))
248 printf(" [style=dashed]");
256 static void media_print_topology_text(struct media_device *media)
258 static const struct {
262 { MEDIA_LNK_FL_ENABLED, "ENABLED" },
263 { MEDIA_LNK_FL_IMMUTABLE, "IMMUTABLE" },
264 { MEDIA_LNK_FL_DYNAMIC, "DYNAMIC" },
267 unsigned int nents = media_get_entities_count(media);
268 unsigned int i, j, k;
269 unsigned int padding;
271 printf("Device topology\n");
273 for (i = 0; i < nents; ++i) {
274 struct media_entity *entity = media_get_entity(media, i);
275 const struct media_entity_desc *info = media_entity_get_info(entity);
276 const char *devname = media_entity_get_devname(entity);
277 unsigned int num_links = media_entity_get_links_count(entity);
279 padding = printf("- entity %u: ", info->id);
280 printf("%s (%u pad%s, %u link%s)\n", info->name,
281 info->pads, info->pads > 1 ? "s" : "",
282 num_links, num_links > 1 ? "s" : "");
283 printf("%*ctype %s subtype %s flags %x\n", padding, ' ',
284 media_entity_type_to_string(info->type),
285 media_entity_subtype_to_string(info->type),
288 printf("%*cdevice node name %s\n", padding, ' ', devname);
290 for (j = 0; j < info->pads; j++) {
291 const struct media_pad *pad = media_entity_get_pad(entity, j);
293 printf("\tpad%u: %s\n", j, media_pad_type_to_string(pad->flags));
295 if (media_entity_type(entity) == MEDIA_ENT_T_V4L2_SUBDEV)
296 v4l2_subdev_print_format(entity, j, V4L2_SUBDEV_FORMAT_ACTIVE);
298 for (k = 0; k < num_links; k++) {
299 const struct media_link *link = media_entity_get_link(entity, k);
300 const struct media_pad *source = link->source;
301 const struct media_pad *sink = link->sink;
305 if (source->entity == entity && source->index == j)
306 printf("\t\t-> \"%s\":%u [",
307 media_entity_get_info(sink->entity)->name,
309 else if (sink->entity == entity && sink->index == j)
310 printf("\t\t<- \"%s\":%u [",
311 media_entity_get_info(source->entity)->name,
316 for (i = 0; i < ARRAY_SIZE(link_flags); i++) {
317 if (!(link->flags & link_flags[i].flag))
321 printf("%s", link_flags[i].name);
332 void media_print_topology(struct media_device *media, int dot)
335 media_print_topology_dot(media);
337 media_print_topology_text(media);
340 int main(int argc, char **argv)
342 struct media_device *media;
345 if (parse_cmdline(argc, argv))
348 media = media_device_new(media_opts.devname);
350 printf("Failed to create media device\n");
354 if (media_opts.verbose)
355 media_debug_set_handler(media,
356 (void (*)(void *, ...))fprintf, stdout);
358 /* Enumerate entities, pads and links. */
359 ret = media_device_enumerate(media);
361 printf("Failed to enumerate %s (%d)\n", media_opts.devname, ret);
365 if (media_opts.print) {
366 const struct media_device_info *info = media_get_info(media);
368 printf("Media controller API version %u.%u.%u\n\n",
369 (info->media_version << 16) & 0xff,
370 (info->media_version << 8) & 0xff,
371 (info->media_version << 0) & 0xff);
372 printf("Media device information\n"
373 "------------------------\n"
379 "driver version %u.%u.%u\n\n",
380 info->driver, info->model,
381 info->serial, info->bus_info,
383 (info->driver_version << 16) & 0xff,
384 (info->driver_version << 8) & 0xff,
385 (info->driver_version << 0) & 0xff);
388 if (media_opts.entity) {
389 struct media_entity *entity;
391 entity = media_get_entity_by_name(media, media_opts.entity,
392 strlen(media_opts.entity));
393 if (entity == NULL) {
394 printf("Entity '%s' not found\n", media_opts.entity);
398 printf("%s\n", media_entity_get_devname(entity));
401 if (media_opts.pad) {
402 struct media_pad *pad;
404 pad = media_parse_pad(media, media_opts.pad, NULL);
406 printf("Pad '%s' not found\n", media_opts.pad);
410 v4l2_subdev_print_format(pad->entity, pad->index,
411 V4L2_SUBDEV_FORMAT_ACTIVE);
414 if (media_opts.print || media_opts.print_dot) {
415 media_print_topology(media, media_opts.print_dot);
419 if (media_opts.reset) {
420 if (media_opts.verbose)
421 printf("Resetting all links to inactive\n");
422 ret = media_reset_links(media);
424 printf("Unable to reset links: %s (%d)\n",
425 strerror(-ret), -ret);
430 if (media_opts.links) {
431 ret = media_parse_setup_links(media, media_opts.links);
433 printf("Unable to parse link: %s (%d)\n",
434 strerror(-ret), -ret);
439 if (media_opts.formats) {
440 ret = v4l2_subdev_parse_setup_formats(media,
443 printf("Unable to setup formats: %s (%d)\n",
444 strerror(-ret), -ret);
449 if (media_opts.interactive) {
454 printf("Enter a link to modify or enter to stop\n");
455 if (fgets(buffer, sizeof buffer, stdin) == NULL)
458 if (buffer[0] == '\n')
461 ret = media_parse_setup_link(media, buffer, &end);
463 printf("Unable to parse link: %s (%d)\n",
464 strerror(-ret), -ret);
472 media_device_unref(media);
474 return ret ? EXIT_FAILURE : EXIT_SUCCESS;