4f3271cf2171d7baad8424db7756fd2423101afc
[media-ctl.git] / src / main.c
1 /*
2  * Media controller test application
3  *
4  * Copyright (C) 2010-2011 Ideas on board SPRL
5  *
6  * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 #include <sys/mman.h>
23 #include <sys/ioctl.h>
24 #include <sys/time.h>
25 #include <sys/stat.h>
26
27 #include <ctype.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 #include <linux/types.h>
37 #include <linux/media.h>
38 #include <linux/v4l2-mediabus.h>
39 #include <linux/v4l2-subdev.h>
40 #include <linux/videodev2.h>
41
42 #include "mediactl.h"
43 #include "options.h"
44 #include "v4l2subdev.h"
45 #include "tools.h"
46
47 /* -----------------------------------------------------------------------------
48  * Printing
49  */
50
51 static void v4l2_subdev_print_format(struct media_entity *entity,
52         unsigned int pad, enum v4l2_subdev_format_whence which)
53 {
54         struct v4l2_mbus_framefmt format;
55         struct v4l2_rect rect;
56         int ret;
57
58         ret = v4l2_subdev_get_format(entity, &format, pad, which);
59         if (ret != 0)
60                 return;
61
62         printf("[%s %ux%u", v4l2_subdev_pixelcode_to_string(format.code),
63                format.width, format.height);
64
65         ret = v4l2_subdev_get_crop(entity, &rect, pad, which);
66         if (ret == 0)
67                 printf(" (%u,%u)/%ux%u", rect.left, rect.top,
68                        rect.width, rect.height);
69         printf("]");
70 }
71
72 static const char *media_entity_type_to_string(unsigned type)
73 {
74         static const struct {
75                 __u32 type;
76                 const char *name;
77         } types[] = {
78                 { MEDIA_ENT_T_DEVNODE, "Node" },
79                 { MEDIA_ENT_T_V4L2_SUBDEV, "V4L2 subdev" },
80         };
81
82         unsigned int i;
83
84         type &= MEDIA_ENT_TYPE_MASK;
85
86         for (i = 0; i < ARRAY_SIZE(types); i++) {
87                 if (types[i].type == type)
88                         return types[i].name;
89         }
90
91         return "Unknown";
92 }
93
94 static const char *media_entity_subtype_to_string(unsigned type)
95 {
96         static const char *node_types[] = {
97                 "Unknown",
98                 "V4L",
99                 "FB",
100                 "ALSA",
101                 "DVB",
102         };
103         static const char *subdev_types[] = {
104                 "Unknown",
105                 "Sensor",
106                 "Flash",
107                 "Lens",
108         };
109
110         unsigned int subtype = type & MEDIA_ENT_SUBTYPE_MASK;
111
112         switch (type & MEDIA_ENT_TYPE_MASK) {
113         case MEDIA_ENT_T_DEVNODE:
114                 if (subtype >= ARRAY_SIZE(node_types))
115                         subtype = 0;
116                 return node_types[subtype];
117
118         case MEDIA_ENT_T_V4L2_SUBDEV:
119                 if (subtype >= ARRAY_SIZE(subdev_types))
120                         subtype = 0;
121                 return subdev_types[subtype];
122         default:
123                 return node_types[0];
124         }
125 }
126
127 static const char *media_pad_type_to_string(unsigned flag)
128 {
129         static const struct {
130                 __u32 flag;
131                 const char *name;
132         } flags[] = {
133                 { MEDIA_PAD_FL_SINK, "Sink" },
134                 { MEDIA_PAD_FL_SOURCE, "Source" },
135         };
136
137         unsigned int i;
138
139         for (i = 0; i < ARRAY_SIZE(flags); i++) {
140                 if (flags[i].flag & flag)
141                         return flags[i].name;
142         }
143
144         return "Unknown";
145 }
146
147 static void media_print_topology_dot(struct media_device *media)
148 {
149         unsigned int i, j;
150
151         printf("digraph board {\n");
152         printf("\trankdir=TB\n");
153
154         for (i = 0; i < media->entities_count; ++i) {
155                 struct media_entity *entity = &media->entities[i];
156                 unsigned int npads;
157
158                 switch (media_entity_type(entity)) {
159                 case MEDIA_ENT_T_DEVNODE:
160                         printf("\tn%08x [label=\"%s\\n%s\", shape=box, style=filled, "
161                                "fillcolor=yellow]\n",
162                                entity->info.id, entity->info.name, entity->devname);
163                         break;
164
165                 case MEDIA_ENT_T_V4L2_SUBDEV:
166                         printf("\tn%08x [label=\"{{", entity->info.id);
167
168                         for (j = 0, npads = 0; j < entity->info.pads; ++j) {
169                                 if (!(entity->pads[j].flags & MEDIA_PAD_FL_SINK))
170                                         continue;
171
172                                 printf("%s<port%u> %u", npads ? " | " : "", j, j);
173                                 npads++;
174                         }
175
176                         printf("} | %s", entity->info.name);
177                         if (entity->devname)
178                                 printf("\\n%s", entity->devname);
179                         printf(" | {");
180
181                         for (j = 0, npads = 0; j < entity->info.pads; ++j) {
182                                 if (!(entity->pads[j].flags & MEDIA_PAD_FL_SOURCE))
183                                         continue;
184
185                                 printf("%s<port%u> %u", npads ? " | " : "", j, j);
186                                 npads++;
187                         }
188
189                         printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n");
190                         break;
191
192                 default:
193                         continue;
194                 }
195
196                 for (j = 0; j < entity->num_links; j++) {
197                         struct media_link *link = &entity->links[j];
198
199                         if (link->source->entity != entity)
200                                 continue;
201
202                         printf("\tn%08x", link->source->entity->info.id);
203                         if (media_entity_type(link->source->entity) == MEDIA_ENT_T_V4L2_SUBDEV)
204                                 printf(":port%u", link->source->index);
205                         printf(" -> ");
206                         printf("n%08x", link->sink->entity->info.id);
207                         if (media_entity_type(link->sink->entity) == MEDIA_ENT_T_V4L2_SUBDEV)
208                                 printf(":port%u", link->sink->index);
209
210                         if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
211                                 printf(" [style=bold]");
212                         else if (!(link->flags & MEDIA_LNK_FL_ENABLED))
213                                 printf(" [style=dashed]");
214                         printf("\n");
215                 }
216         }
217
218         printf("}\n");
219 }
220
221 static void media_print_topology_text(struct media_device *media)
222 {
223         static const struct {
224                 __u32 flag;
225                 char *name;
226         } link_flags[] = {
227                 { MEDIA_LNK_FL_ENABLED, "ENABLED" },
228                 { MEDIA_LNK_FL_IMMUTABLE, "IMMUTABLE" },
229                 { MEDIA_LNK_FL_DYNAMIC, "DYNAMIC" },
230         };
231
232         unsigned int i, j, k;
233         unsigned int padding;
234
235         printf("Device topology\n");
236
237         for (i = 0; i < media->entities_count; ++i) {
238                 struct media_entity *entity = &media->entities[i];
239
240                 padding = printf("- entity %u: ", entity->info.id);
241                 printf("%s (%u pad%s, %u link%s)\n", entity->info.name,
242                         entity->info.pads, entity->info.pads > 1 ? "s" : "",
243                         entity->num_links, entity->num_links > 1 ? "s" : "");
244                 printf("%*ctype %s subtype %s\n", padding, ' ',
245                         media_entity_type_to_string(entity->info.type),
246                         media_entity_subtype_to_string(entity->info.type));
247                 if (entity->devname[0])
248                         printf("%*cdevice node name %s\n", padding, ' ', entity->devname);
249
250                 for (j = 0; j < entity->info.pads; j++) {
251                         struct media_pad *pad = &entity->pads[j];
252
253                         printf("\tpad%u: %s ", j, media_pad_type_to_string(pad->flags));
254
255                         if (media_entity_type(entity) == MEDIA_ENT_T_V4L2_SUBDEV)
256                                 v4l2_subdev_print_format(entity, j, V4L2_SUBDEV_FORMAT_ACTIVE);
257
258                         printf("\n");
259
260                         for (k = 0; k < entity->num_links; k++) {
261                                 struct media_link *link = &entity->links[k];
262                                 struct media_pad *source = link->source;
263                                 struct media_pad *sink = link->sink;
264                                 bool first = true;
265                                 unsigned int i;
266
267                                 if (source->entity == entity && source->index == j)
268                                         printf("\t\t-> \"%s\":%u [",
269                                                 sink->entity->info.name, sink->index);
270                                 else if (sink->entity == entity && sink->index == j)
271                                         printf("\t\t<- \"%s\":%u [",
272                                                 source->entity->info.name, source->index);
273                                 else
274                                         continue;
275
276                                 for (i = 0; i < ARRAY_SIZE(link_flags); i++) {
277                                         if (!(link->flags & link_flags[i].flag))
278                                                 continue;
279                                         if (!first)
280                                                 printf(",");
281                                         printf("%s", link_flags[i].name);
282                                         first = false;
283                                 }
284
285                                 printf("]\n");
286                         }
287                 }
288                 printf("\n");
289         }
290 }
291
292 void media_print_topology(struct media_device *media, int dot)
293 {
294         if (dot)
295                 media_print_topology_dot(media);
296         else
297                 media_print_topology_text(media);
298 }
299
300 int main(int argc, char **argv)
301 {
302         struct media_device *media;
303         int ret = -1;
304
305         if (parse_cmdline(argc, argv))
306                 return EXIT_FAILURE;
307
308         /* Open the media device and enumerate entities, pads and links. */
309         if (media_opts.verbose)
310                 media = media_open_debug(
311                         media_opts.devname,
312                         (void (*)(void *, ...))fprintf, stdout);
313         else
314                 media = media_open(media_opts.devname);
315         if (media == NULL) {
316                 printf("Failed to open %s\n", media_opts.devname);
317                 goto out;
318         }
319
320         if (media_opts.print) {
321                 printf("Media controller API version %u.%u.%u\n\n",
322                        (media->info.media_version << 16) & 0xff,
323                        (media->info.media_version << 8) & 0xff,
324                        (media->info.media_version << 0) & 0xff);
325                 printf("Media device information\n"
326                        "------------------------\n"
327                        "driver          %s\n"
328                        "model           %s\n"
329                        "serial          %s\n"
330                        "bus info        %s\n"
331                        "hw revision     0x%x\n"
332                        "driver version  %u.%u.%u\n\n",
333                        media->info.driver, media->info.model,
334                        media->info.serial, media->info.bus_info,
335                        media->info.hw_revision,
336                        (media->info.driver_version << 16) & 0xff,
337                        (media->info.driver_version << 8) & 0xff,
338                        (media->info.driver_version << 0) & 0xff);
339         }
340
341         if (media_opts.entity) {
342                 struct media_entity *entity;
343
344                 entity = media_get_entity_by_name(media, media_opts.entity,
345                                                   strlen(media_opts.entity));
346                 if (entity == NULL) {
347                         printf("Entity '%s' not found\n", media_opts.entity);
348                         goto out;
349                 }
350
351                 printf("%s\n", entity->devname);
352         }
353
354         if (media_opts.pad) {
355                 struct media_pad *pad;
356
357                 pad = media_parse_pad(media, media_opts.pad, NULL);
358                 if (pad == NULL) {
359                         printf("Pad '%s' not found\n", media_opts.pad);
360                         goto out;
361                 }
362
363                 v4l2_subdev_print_format(pad->entity, pad->index,
364                                          V4L2_SUBDEV_FORMAT_ACTIVE);
365                 printf("\n");
366         }
367
368         if (media_opts.print || media_opts.print_dot) {
369                 media_print_topology(media, media_opts.print_dot);
370                 printf("\n");
371         }
372
373         if (media_opts.reset) {
374                 if (media_opts.verbose)
375                         printf("Resetting all links to inactive\n");
376                 ret = media_reset_links(media);
377                 if (ret) {
378                         printf("Unable to reset links: %s (%d)\n",
379                                strerror(-ret), -ret);
380                         goto out;
381                 }
382         }
383
384         if (media_opts.links) {
385                 ret = media_parse_setup_links(media, media_opts.links);
386                 if (ret) {
387                         printf("Unable to parse link: %s (%d)\n",
388                                strerror(-ret), -ret);
389                         goto out;
390                 }
391         }
392
393         if (media_opts.formats) {
394                 ret = v4l2_subdev_parse_setup_formats(media,
395                                                       media_opts.formats);
396                 if (ret) {
397                         printf("Unable to parse format: %s (%d)\n",
398                                strerror(-ret), -ret);
399                         goto out;
400                 }
401         }
402
403         if (media_opts.interactive) {
404                 while (1) {
405                         char buffer[32];
406                         char *end;
407
408                         printf("Enter a link to modify or enter to stop\n");
409                         if (fgets(buffer, sizeof buffer, stdin) == NULL)
410                                 break;
411
412                         if (buffer[0] == '\n')
413                                 break;
414
415                         ret = media_parse_setup_link(media, buffer, &end);
416                         if (ret)
417                                 printf("Unable to parse link: %s (%d)\n",
418                                        strerror(-ret), -ret);
419                 }
420         }
421
422         ret = 0;
423
424 out:
425         if (media)
426                 media_close(media);
427
428         return ret ? EXIT_FAILURE : EXIT_SUCCESS;
429 }
430