2 * Media controller test application
4 * Copyright (C) 2010 Ideas on board SPRL <laurent.pinchart@ideasonboard.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
21 #include <sys/ioctl.h>
33 #include <linux/types.h>
34 #include <linux/media.h>
35 #include <linux/v4l2-mediabus.h>
36 #include <linux/v4l2-subdev.h>
37 #include <linux/videodev2.h>
43 /* -----------------------------------------------------------------------------
47 static struct media_entity_pad *parse_pad(struct media_device *media, const char *p, char **endp)
49 unsigned int entity_id, pad;
50 struct media_entity *entity;
53 for (; isspace(*p); ++p);
56 for (end = (char *)p + 1; *end && *end != '"'; ++end);
60 entity = media_get_entity_by_name(media, p + 1, end - p - 1);
64 for (++end; isspace(*end); ++end);
66 entity_id = strtoul(p, &end, 10);
67 entity = media_get_entity_by_id(media, entity_id);
74 for (p = end + 1; isspace(*p); ++p);
76 pad = strtoul(p, &end, 10);
77 for (p = end; isspace(*p); ++p);
79 if (pad >= entity->info.pads)
82 for (p = end; isspace(*p); ++p);
85 return &entity->pads[pad];
88 static struct media_entity_link *parse_link(struct media_device *media, const char *p, char **endp)
90 struct media_entity_link *link;
91 struct media_entity_pad *source;
92 struct media_entity_pad *sink;
96 source = parse_pad(media, p, &end);
100 if (end[0] != '-' || end[1] != '>')
104 sink = parse_pad(media, p, &end);
110 for (i = 0; i < source->entity->info.links; i++) {
111 link = &source->entity->links[i];
113 if (link->source == source && link->sink == sink)
120 static int setup_link(struct media_device *media, const char *p, char **endp)
122 struct media_entity_link *link;
126 link = parse_link(media, p, &end);
128 printf("Unable to parse link\n");
134 printf("Unable to parse link flags\n");
138 flags = strtoul(p, &end, 10);
139 for (p = end; isspace(*p); p++);
141 printf("Unable to parse link flags\n");
145 for (; isspace(*p); p++);
148 printf("Setting up link %u:%u -> %u:%u [%u]\n",
149 link->source->entity->info.id, link->source->index,
150 link->sink->entity->info.id, link->sink->index,
153 return media_setup_link(media, link->source, link->sink, flags);
156 static int setup_links(struct media_device *media, const char *p)
162 ret = setup_link(media, p, &end);
167 } while (*end == ',');
169 return *end ? -EINVAL : 0;
172 /* -----------------------------------------------------------------------------
176 static int parse_format(struct v4l2_mbus_framefmt *format, const char *p, char **endp)
178 enum v4l2_mbus_pixelcode code;
179 unsigned int width, height;
182 for (; isspace(*p); ++p);
183 for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
185 code = string_to_pixelcode(p, end - p);
186 if (code == (enum v4l2_mbus_pixelcode)-1)
189 for (p = end; isspace(*p); ++p);
190 width = strtoul(p, &end, 10);
195 height = strtoul(p, &end, 10);
198 memset(format, 0, sizeof(*format));
199 format->width = width;
200 format->height = height;
206 static int parse_crop(struct v4l2_rect *crop, const char *p, char **endp)
210 for (; isspace(*p); ++p);
212 crop->left = strtoul(p, &end, 10);
217 crop->top = strtoul(p, &end, 10);
222 crop->width = strtoul(p, &end, 10);
227 crop->height = strtoul(p, &end, 10);
233 static int parse_frame_interval(struct v4l2_fract *interval, const char *p, char **endp)
237 for (; isspace(*p); ++p);
239 interval->numerator = strtoul(p, &end, 10);
241 for (p = end; isspace(*p); ++p);
245 for (; isspace(*p); ++p);
246 interval->denominator = strtoul(p, &end, 10);
252 static struct media_entity_pad *parse_pad_format(struct media_device *media,
253 struct v4l2_mbus_framefmt *format, struct v4l2_rect *crop,
254 struct v4l2_fract *interval, const char *p, char **endp)
256 struct media_entity_pad *pad;
260 for (; isspace(*p); ++p);
262 pad = parse_pad(media, p, &end);
266 for (p = end; isspace(*p); ++p);
270 ret = parse_format(format, p, &end);
274 for (p = end; isspace(*p); p++);
276 ret = parse_crop(crop, p, &end);
280 for (p = end; isspace(*p); p++);
284 ret = parse_frame_interval(interval, ++p, &end);
288 for (p = end; isspace(*p); p++);
294 *endp = (char *)p + 1;
298 static int set_format(struct media_entity_pad *pad, struct v4l2_mbus_framefmt *format)
302 printf("Setting up format %s %ux%u on pad %s/%u\n",
303 pixelcode_to_string(format->code), format->width, format->height,
304 pad->entity->info.name, pad->index);
306 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
307 V4L2_SUBDEV_FORMAT_ACTIVE);
309 printf("Unable to set format: %s (%d)\n", strerror(-ret), ret);
313 printf("Format set: %s %ux%u\n",
314 pixelcode_to_string(format->code), format->width, format->height);
319 static int set_crop(struct media_entity_pad *pad, struct v4l2_rect *crop)
323 printf("Setting up crop rectangle %u,%u/%ux%u on pad %s/%u\n",
324 crop->left, crop->top, crop->width, crop->height,
325 pad->entity->info.name, pad->index);
327 ret = v4l2_subdev_set_crop(pad->entity, crop, pad->index,
328 V4L2_SUBDEV_FORMAT_ACTIVE);
330 printf("Unable to set crop rectangle: %s (%d)", strerror(-ret), ret);
334 printf("Crop rectangle set: %u,%u/%ux%u\n",
335 crop->left, crop->top, crop->width, crop->height);
340 static int set_frame_interval(struct media_entity *entity, struct v4l2_fract *interval)
344 printf("Setting up frame interval %u/%u on entity %s\n",
345 interval->numerator, interval->denominator, entity->info.name);
347 ret = v4l2_subdev_set_frame_interval(entity, interval);
349 printf("Unable to set frame interval: %s (%d)", strerror(-ret), ret);
353 printf("Frame interval set: %u/%u\n",
354 interval->numerator, interval->denominator);
360 static int setup_format(struct media_device *media, const char *p, char **endp)
362 struct v4l2_mbus_framefmt format;
363 struct media_entity_pad *pad;
364 struct v4l2_rect crop = { -1, -1, -1, -1 };
365 struct v4l2_fract interval = { 0, 0 };
370 pad = parse_pad_format(media, &format, &crop, &interval, p, &end);
372 printf("Unable to parse format\n");
376 ret = set_format(pad, &format);
378 printf("Unable to set format\n");
382 if (crop.left != -1 && crop.top != -1) {
383 ret = set_crop(pad, &crop);
385 printf("Unable to set crop rectangle\n");
390 if (interval.numerator != 0) {
391 ret = set_frame_interval(pad->entity, &interval);
393 printf("Unable to set frame interval\n");
398 /* If the pad is an output pad, automatically set the same format on
399 * the remote subdev input pads, if any.
401 if (pad->type == MEDIA_PAD_TYPE_OUTPUT) {
402 for (i = 0; i < pad->entity->info.links; ++i) {
403 struct media_entity_link *link = &pad->entity->links[i];
404 struct v4l2_mbus_framefmt remote_format;
406 if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
409 if (link->source == pad &&
410 link->sink->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV) {
411 remote_format = format;
412 set_format(link->sink, &remote_format);
421 static int setup_formats(struct media_device *media, const char *p)
427 ret = setup_format(media, p, &end);
432 } while (*end == ',');
434 return *end ? -EINVAL : 0;
437 int main(int argc, char **argv)
439 struct media_device *media;
441 if (parse_cmdline(argc, argv))
444 /* Open the media device and enumerate entities, pads and links. */
445 media = media_open(media_opts.devname, media_opts.verbose);
449 if (media_opts.entity) {
450 struct media_entity *entity;
452 entity = media_get_entity_by_name(media, media_opts.entity,
453 strlen(media_opts.entity));
455 printf("%s\n", entity->devname);
457 printf("Entity '%s' not found\n", media_opts.entity);
460 if (media_opts.print || media_opts.print_dot) {
461 media_print_topology(media, media_opts.print_dot);
465 if (media_opts.reset) {
466 printf("Resetting all links to inactive\n");
467 media_reset_links(media);
470 if (media_opts.links)
471 setup_links(media, media_opts.links);
473 if (media_opts.formats)
474 setup_formats(media, media_opts.formats);
476 if (media_opts.interactive) {
481 printf("Enter a link to modify or enter to stop\n");
482 if (fgets(buffer, sizeof buffer, stdin) == NULL)
485 if (buffer[0] == '\n')
488 setup_link(media, buffer, &end);