summaryrefslogtreecommitdiff
path: root/media.c
blob: f3840266e4fa9b59501b9f7f847f32c299b6d28d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
 * 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 "subdev.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];
	}
}

static const char *media_pad_type_to_string(unsigned type)
{
	static const struct {
		__u32 type;
		const char *name;
	} types[] = {
		{ MEDIA_PAD_TYPE_INPUT, "Input" },
		{ MEDIA_PAD_TYPE_OUTPUT, "Output" },
	};

	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(types); i++) {
		if (types[i].type == type)
			return types[i].name;
	}

	return "Unknown";
}

/*
 * 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_get_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_get_entity_by_id -
 */
struct media_entity *media_get_entity_by_id(struct media_device *media,
					    __u32 id)
{
	unsigned int i;

	for (i = 0; i < media->entities_count; ++i) {
		struct media_entity *entity = &media->entities[i];

		if (entity->info.id == id)
			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;
}

static void media_print_topology_dot(struct media_device *media)
{
	unsigned int i, j;

	printf("digraph board {\n");
	printf("\trankdir=TB\n");

	for (i = 0; i < media->entities_count; ++i) {
		struct media_entity *entity = &media->entities[i];
		unsigned int npads;

		switch (entity->info.type) {
		case MEDIA_ENTITY_TYPE_NODE:
			printf("\tn%08x [label=\"%s\\n%s\", shape=box, style=filled, "
			       "fillcolor=yellow]\n",
			       entity->info.id, entity->info.name, entity->devname);
			break;

		case MEDIA_ENTITY_TYPE_SUBDEV:
			printf("\tn%08x [label=\"{{", entity->info.id);

			for (j = 0, npads = 0; j < entity->info.pads; ++j) {
				if (entity->pads[j].type != MEDIA_PAD_TYPE_INPUT)
					continue;

				printf("%s<port%u> %u", npads ? " | " : "", j, j);
				npads++;
			}

			printf("} | %s", entity->info.name);
			if (entity->devname)
				printf("\\n%s", entity->devname);
			printf(" | {");

			for (j = 0, npads = 0; j < entity->info.pads; ++j) {
				if (entity->pads[j].type != MEDIA_PAD_TYPE_OUTPUT)
					continue;

				printf("%s<port%u> %u", npads ? " | " : "", j, j);
				npads++;
			}

			printf("}}\", shape=Mrecord, style=filled, fillcolor=green]\n");
			break;

		default:
			continue;
		}

		for (j = 0; j < entity->info.links; j++) {
			struct media_entity_link *link = &entity->links[j];

			if (link->source->entity != entity)
				continue;

			printf("\tn%08x", link->source->entity->info.id);
			if (link->source->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV)
				printf(":port%u", link->source->index);
			printf(" -> ");
			printf("n%08x", link->sink->entity->info.id);
			if (link->sink->entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV)
				printf(":port%u", link->sink->index);

			if (link->flags & MEDIA_LINK_FLAG_IMMUTABLE)
				printf(" [style=bold]");
			else if (!(link->flags & MEDIA_LINK_FLAG_ACTIVE))
				printf(" [style=dashed]");
			printf("\n");
		}
	}

	printf("}\n");
}

static void media_print_topology_text(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[0])
			printf("%*cdevice node name %s\n", padding, ' ', entity->devname);

		for (j = 0; j < entity->info.pads; j++) {
			struct media_entity_pad *pad = &entity->pads[j];

			printf("\tpad%u: %s ", j, media_pad_type_to_string(pad->type));

			if (entity->info.type == MEDIA_ENTITY_TYPE_SUBDEV)
				v4l2_subdev_print_format(entity, j, V4L2_SUBDEV_FORMAT_ACTIVE);

			printf("\n");

			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("\t\t-> '%s':pad%u [",
					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");
	}
}

void media_print_topology(struct media_device *media, int dot)
{
	if (dot)
		media_print_topology_dot(media);
	else
		media_print_topology_text(media);
}

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;

			source = media_get_entity_by_id(media, link->source.entity);
			sink = media_get_entity_by_id(media, link->sink.entity);

			if (source == NULL || sink == NULL) {
				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;
			}

			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;
	unsigned int size;
	char devname[32];
	char sysname[32];
	char target[1024];
	char *p;
	__u32 id;
	int ret;

	for (id = 0; ; id = entity->info.id) {
		size = (media->entities_count + 1) * sizeof(*media->entities);
		media->entities = realloc(media->entities, size);

		entity = &media->entities[media->entities_count];
		memset(entity, 0, sizeof(*entity));
		entity->fd = -1;
		entity->info.id = id | MEDIA_ENTITY_ID_FLAG_NEXT;

		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;

		sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
			entity->info.v4l.minor);
		ret = readlink(sysname, target, sizeof(target));
		if (ret < 0)
			continue;

		target[ret] = '\0';
		p = strrchr(target, '/');
		if (p == NULL)
			continue;

		sprintf(devname, "/dev/%s", p + 1);
		ret = stat(devname, &devstat);
		if (ret < 0)
			continue;

		/* Sanity check: udev might have reordered the device nodes.
		 * Make sure the major/minor match. We should really use
		 * libudev.
		 */
		if (major(devstat.st_rdev) == entity->info.v4l.major &&
		    minor(devstat.st_rdev) == entity->info.v4l.minor)
			strcpy(entity->devname, devname);
	}

	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);
}