Update to the latest media kernel API
[media-ctl.git] / main.c
1 /*
2  * Media controller test application
3  *
4  * Copyright (C) 2010 Ideas on board SPRL <laurent.pinchart@ideasonboard.com>
5  *
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.
10  *
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.
15  *
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.,
18  */
19
20 #include <sys/mman.h>
21 #include <sys/ioctl.h>
22 #include <sys/time.h>
23 #include <sys/stat.h>
24
25 #include <ctype.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
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>
38
39 #include "media.h"
40 #include "options.h"
41 #include "subdev.h"
42
43 /* -----------------------------------------------------------------------------
44  * Links setup
45  */
46
47 static struct media_entity_pad *parse_pad(struct media_device *media, const char *p, char **endp)
48 {
49         unsigned int entity_id, pad;
50         struct media_entity *entity;
51         char *end;
52
53         for (; isspace(*p); ++p);
54
55         if (*p == '"') {
56                 for (end = (char *)p + 1; *end && *end != '"'; ++end);
57                 if (*end != '"')
58                         return NULL;
59
60                 entity = media_get_entity_by_name(media, p + 1, end - p - 1);
61                 if (entity == NULL)
62                         return NULL;
63
64                 ++end;
65         } else {
66                 entity_id = strtoul(p, &end, 10);
67                 entity = media_get_entity_by_id(media, entity_id);
68                 if (entity == NULL)
69                         return NULL;
70         }
71         for (; isspace(*end); ++end);
72
73         if (*end != ':')
74                 return NULL;
75         for (p = end + 1; isspace(*p); ++p);
76
77         pad = strtoul(p, &end, 10);
78         for (p = end; isspace(*p); ++p);
79
80         if (pad >= entity->info.pads)
81                 return NULL;
82
83         for (p = end; isspace(*p); ++p);
84         if (endp)
85                 *endp = (char *)p;
86
87         return &entity->pads[pad];
88 }
89
90 static struct media_entity_link *parse_link(struct media_device *media, const char *p, char **endp)
91 {
92         struct media_entity_link *link;
93         struct media_entity_pad *source;
94         struct media_entity_pad *sink;
95         unsigned int i;
96         char *end;
97
98         source = parse_pad(media, p, &end);
99         if (source == NULL)
100                 return NULL;
101
102         if (end[0] != '-' || end[1] != '>')
103                 return NULL;
104         p = end + 2;
105
106         sink = parse_pad(media, p, &end);
107         if (sink == NULL)
108                 return NULL;
109
110         *endp = end;
111
112         for (i = 0; i < source->entity->info.links; i++) {
113                 link = &source->entity->links[i];
114
115                 if (link->source == source && link->sink == sink)
116                         return link;
117         }
118
119         return NULL;
120 }
121
122 static int setup_link(struct media_device *media, const char *p, char **endp)
123 {
124         struct media_entity_link *link;
125         __u32 flags;
126         char *end;
127
128         link = parse_link(media, p, &end);
129         if (link == NULL) {
130                 printf("Unable to parse link\n");
131                 return -EINVAL;
132         }
133
134         p = end;
135         if (*p++ != '[') {
136                 printf("Unable to parse link flags\n");
137                 return -EINVAL;
138         }
139
140         flags = strtoul(p, &end, 10);
141         for (p = end; isspace(*p); p++);
142         if (*p++ != ']') {
143                 printf("Unable to parse link flags\n");
144                 return -EINVAL;
145         }
146
147         for (; isspace(*p); p++);
148         *endp = (char *)p;
149
150         printf("Setting up link %u:%u -> %u:%u [%u]\n",
151                 link->source->entity->info.id, link->source->index,
152                 link->sink->entity->info.id, link->sink->index,
153                 flags);
154
155         return media_setup_link(media, link->source, link->sink, flags);
156 }
157
158 static int setup_links(struct media_device *media, const char *p)
159 {
160         char *end;
161         int ret;
162
163         do {
164                 ret = setup_link(media, p, &end);
165                 if (ret < 0)
166                         return ret;
167
168                 p = end + 1;
169         } while (*end == ',');
170
171         return *end ? -EINVAL : 0;
172 }
173
174 /* -----------------------------------------------------------------------------
175  * Formats setup
176  */
177
178 static int parse_format(struct v4l2_mbus_framefmt *format, const char *p, char **endp)
179 {
180         enum v4l2_mbus_pixelcode code;
181         unsigned int width, height;
182         char *end;
183
184         for (; isspace(*p); ++p);
185         for (end = (char *)p; !isspace(*end) && *end != '\0'; ++end);
186
187         code = string_to_pixelcode(p, end - p);
188         if (code == (enum v4l2_mbus_pixelcode)-1)
189                 return -EINVAL;
190
191         for (p = end; isspace(*p); ++p);
192         width = strtoul(p, &end, 10);
193         if (*end != 'x')
194                 return -EINVAL;
195
196         p = end + 1;
197         height = strtoul(p, &end, 10);
198         *endp = end;
199
200         memset(format, 0, sizeof(*format));
201         format->width = width;
202         format->height = height;
203         format->code = code;
204
205         return 0;
206 }
207
208 static int parse_crop(struct v4l2_rect *crop, const char *p, char **endp)
209 {
210         char *end;
211
212         if (*p++ != '(')
213                 return -EINVAL;
214
215         crop->left = strtoul(p, &end, 10);
216         if (*end != ',')
217                 return -EINVAL;
218
219         p = end + 1;
220         crop->top = strtoul(p, &end, 10);
221         if (*end++ != ')')
222                 return -EINVAL;
223         if (*end != '/')
224                 return -EINVAL;
225
226         p = end + 1;
227         crop->width = strtoul(p, &end, 10);
228         if (*end != 'x')
229                 return -EINVAL;
230
231         p = end + 1;
232         crop->height = strtoul(p, &end, 10);
233         *endp = end;
234
235         return 0;
236 }
237
238 static int parse_frame_interval(struct v4l2_fract *interval, const char *p, char **endp)
239 {
240         char *end;
241
242         for (; isspace(*p); ++p);
243
244         interval->numerator = strtoul(p, &end, 10);
245
246         for (p = end; isspace(*p); ++p);
247         if (*p++ != '/')
248                 return -EINVAL;
249
250         for (; isspace(*p); ++p);
251         interval->denominator = strtoul(p, &end, 10);
252
253         *endp = end;
254         return 0;
255 }
256
257 static struct media_entity_pad *parse_pad_format(struct media_device *media,
258         struct v4l2_mbus_framefmt *format, struct v4l2_rect *crop,
259         struct v4l2_fract *interval, const char *p, char **endp)
260 {
261         struct media_entity_pad *pad;
262         char *end;
263         int ret;
264
265         for (; isspace(*p); ++p);
266
267         pad = parse_pad(media, p, &end);
268         if (pad == NULL)
269                 return NULL;
270
271         for (p = end; isspace(*p); ++p);
272         if (*p++ != '[')
273                 return NULL;
274
275         if (isalnum(*p)) {
276                 ret = parse_format(format, p, &end);
277                 if (ret < 0)
278                         return NULL;
279
280                 for (p = end; isspace(*p); p++);
281         }
282
283         if (*p == '(') {
284                 ret = parse_crop(crop, p, &end);
285                 if (ret < 0)
286                         return NULL;
287
288                 for (p = end; isspace(*p); p++);
289         }
290
291         if (*p == '@') {
292                 ret = parse_frame_interval(interval, ++p, &end);
293                 if (ret < 0)
294                         return NULL;
295
296                 for (p = end; isspace(*p); p++);
297         }
298
299         if (*p != ']')
300                 return NULL;
301
302         *endp = (char *)p + 1;
303         return pad;
304 }
305
306 static int set_format(struct media_entity_pad *pad, struct v4l2_mbus_framefmt *format)
307 {
308         int ret;
309
310         if (format->width == 0 || format->height == 0)
311                 return 0;
312
313         printf("Setting up format %s %ux%u on pad %s/%u\n",
314                 pixelcode_to_string(format->code), format->width, format->height,
315                 pad->entity->info.name, pad->index);
316
317         ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
318                                      V4L2_SUBDEV_FORMAT_ACTIVE);
319         if (ret < 0) {
320                 printf("Unable to set format: %s (%d)\n", strerror(-ret), ret);
321                 return ret;
322         }
323
324         printf("Format set: %s %ux%u\n",
325                 pixelcode_to_string(format->code), format->width, format->height);
326
327         return 0;
328 }
329
330 static int set_crop(struct media_entity_pad *pad, struct v4l2_rect *crop)
331 {
332         int ret;
333
334         if (crop->left == -1 || crop->top == -1)
335                 return 0;
336
337         printf("Setting up crop rectangle (%u,%u)/%ux%u on pad %s/%u\n",
338                 crop->left, crop->top, crop->width, crop->height,
339                 pad->entity->info.name, pad->index);
340
341         ret = v4l2_subdev_set_crop(pad->entity, crop, pad->index,
342                                    V4L2_SUBDEV_FORMAT_ACTIVE);
343         if (ret < 0) {
344                 printf("Unable to set crop rectangle: %s (%d)", strerror(-ret), ret);
345                 return ret;
346         }
347
348         printf("Crop rectangle set: (%u,%u)/%ux%u\n",
349                 crop->left, crop->top, crop->width, crop->height);
350
351         return 0;
352 }
353
354 static int set_frame_interval(struct media_entity *entity, struct v4l2_fract *interval)
355 {
356         int ret;
357
358         if (interval->numerator == 0)
359                 return 0;
360
361         printf("Setting up frame interval %u/%u on entity %s\n",
362                 interval->numerator, interval->denominator, entity->info.name);
363
364         ret = v4l2_subdev_set_frame_interval(entity, interval);
365         if (ret < 0) {
366                 printf("Unable to set frame interval: %s (%d)", strerror(-ret), ret);
367                 return ret;
368         }
369
370         printf("Frame interval set: %u/%u\n",
371                 interval->numerator, interval->denominator);
372
373         return 0;
374 }
375
376
377 static int setup_format(struct media_device *media, const char *p, char **endp)
378 {
379         struct v4l2_mbus_framefmt format = { 0, 0, 0 };
380         struct media_entity_pad *pad;
381         struct v4l2_rect crop = { -1, -1, -1, -1 };
382         struct v4l2_fract interval = { 0, 0 };
383         unsigned int i;
384         char *end;
385         int ret;
386
387         pad = parse_pad_format(media, &format, &crop, &interval, p, &end);
388         if (pad == NULL) {
389                 printf("Unable to parse format\n");
390                 return -EINVAL;
391         }
392
393         if (pad->flags & MEDIA_PAD_FLAG_OUTPUT) {
394                 ret = set_crop(pad, &crop);
395                 if (ret < 0)
396                         return ret;
397         }
398
399         ret = set_format(pad, &format);
400         if (ret < 0)
401                 return ret;
402
403         if (pad->flags & MEDIA_PAD_FLAG_INPUT) {
404                 ret = set_crop(pad, &crop);
405                 if (ret < 0)
406                         return ret;
407         }
408
409         ret = set_frame_interval(pad->entity, &interval);
410         if (ret < 0)
411                 return ret;
412
413
414         /* If the pad is an output pad, automatically set the same format on
415          * the remote subdev input pads, if any.
416          */
417         if (pad->flags & MEDIA_PAD_FLAG_OUTPUT) {
418                 for (i = 0; i < pad->entity->info.links; ++i) {
419                         struct media_entity_link *link = &pad->entity->links[i];
420                         struct v4l2_mbus_framefmt remote_format;
421
422                         if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
423                                 continue;
424
425                         if (link->source == pad &&
426                             link->sink->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV) {
427                                 remote_format = format;
428                                 set_format(link->sink, &remote_format);
429                         }
430                 }
431         }
432
433         *endp = end;
434         return 0;
435 }
436
437 static int setup_formats(struct media_device *media, const char *p)
438 {
439         char *end;
440         int ret;
441
442         do {
443                 ret = setup_format(media, p, &end);
444                 if (ret < 0)
445                         return ret;
446
447                 p = end + 1;
448         } while (*end == ',');
449
450         return *end ? -EINVAL : 0;
451 }
452
453 int main(int argc, char **argv)
454 {
455         struct media_device *media;
456         int ret = -1;
457
458         if (parse_cmdline(argc, argv))
459                 return EXIT_FAILURE;
460
461         /* Open the media device and enumerate entities, pads and links. */
462         media = media_open(media_opts.devname, media_opts.verbose);
463         if (media == NULL)
464                 goto out;
465
466         if (media_opts.entity) {
467                 struct media_entity *entity;
468
469                 entity = media_get_entity_by_name(media, media_opts.entity,
470                                                   strlen(media_opts.entity));
471                 if (entity == NULL) {
472                         printf("Entity '%s' not found\n", media_opts.entity);
473                         goto out;
474                 }
475
476                 printf("%s\n", entity->devname);
477         }
478
479         if (media_opts.pad) {
480                 struct media_entity_pad *pad;
481
482                 pad = parse_pad(media, media_opts.pad, NULL);
483                 if (pad == NULL) {
484                         printf("Pad '%s' not found\n", media_opts.pad);
485                         goto out;
486                 }
487
488                 v4l2_subdev_print_format(pad->entity, pad->index,
489                                          V4L2_SUBDEV_FORMAT_ACTIVE);
490                 printf("\n");
491         }
492
493         if (media_opts.print || media_opts.print_dot) {
494                 media_print_topology(media, media_opts.print_dot);
495                 printf("\n");
496         }
497
498         if (media_opts.reset) {
499                 printf("Resetting all links to inactive\n");
500                 media_reset_links(media);
501         }
502
503         if (media_opts.links)
504                 setup_links(media, media_opts.links);
505
506         if (media_opts.formats)
507                 setup_formats(media, media_opts.formats);
508
509         if (media_opts.interactive) {
510                 while (1) {
511                         char buffer[32];
512                         char *end;
513
514                         printf("Enter a link to modify or enter to stop\n");
515                         if (fgets(buffer, sizeof buffer, stdin) == NULL)
516                                 break;
517
518                         if (buffer[0] == '\n')
519                                 break;
520
521                         setup_link(media, buffer, &end);
522                 }
523         }
524
525         ret = 0;
526
527 out:
528         if (media)
529                 media_close(media);
530
531         return ret ? EXIT_FAILURE : EXIT_SUCCESS;
532 }
533