xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/common/mediactl/mediactl.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Media controller interface library
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 "config.h"
23 
24 #include <sys/ioctl.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/sysmacros.h>
28 
29 #include <ctype.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <stdbool.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 #include <linux/media.h>
39 #include <linux/videodev2.h>
40 
41 #include "mediactl.h"
42 #include "mediactl-priv.h"
43 #include "tools.h"
44 
45 #include "xcam_log.h"
46 
47 /* -----------------------------------------------------------------------------
48  * Graph access
49  */
50 
media_entity_remote_source(struct media_pad * pad)51 struct media_pad *media_entity_remote_source(struct media_pad *pad)
52 {
53 	unsigned int i;
54 
55 	if (!(pad->flags & MEDIA_PAD_FL_SINK))
56 		return NULL;
57 
58 	for (i = 0; i < pad->entity->num_links; ++i) {
59 		struct media_link *link = &pad->entity->links[i];
60 
61 		if (!(link->flags & MEDIA_LNK_FL_ENABLED))
62 			continue;
63 
64 		if (link->sink == pad)
65 			return link->source;
66 	}
67 
68 	return NULL;
69 }
70 
media_get_entity_by_name(struct media_device * media,const char * name,size_t length)71 struct media_entity *media_get_entity_by_name(struct media_device *media,
72 					      const char *name, size_t length)
73 {
74 	unsigned int i;
75 
76 	/* A match is impossible if the entity name is longer than the maximum
77 	 * size we can get from the kernel.
78 	 */
79 	if (length >= FIELD_SIZEOF(struct media_entity_desc, name))
80 		return NULL;
81 
82 	for (i = 0; i < media->entities_count; ++i) {
83 		struct media_entity *entity = &media->entities[i];
84 
85 		if (strncmp(entity->info.name, name, length) == 0 &&
86 		    entity->info.name[length] == '\0') {
87 			return entity;
88         }
89 	}
90 
91     LOGW("media get entity by name: %s is null\n", name);
92 	return NULL;
93 }
94 
media_get_entity_by_id(struct media_device * media,__u32 id)95 struct media_entity *media_get_entity_by_id(struct media_device *media,
96 					    __u32 id)
97 {
98 	bool next = id & MEDIA_ENT_ID_FLAG_NEXT;
99 	unsigned int i;
100 
101 	id &= ~MEDIA_ENT_ID_FLAG_NEXT;
102 
103 	for (i = 0; i < media->entities_count; ++i) {
104 		struct media_entity *entity = &media->entities[i];
105 
106 		if ((entity->info.id == id && !next) ||
107 		    (entity->info.id > id && next))
108 			return entity;
109 	}
110 
111 	return NULL;
112 }
113 
media_get_entities_count(struct media_device * media)114 unsigned int media_get_entities_count(struct media_device *media)
115 {
116 	return media->entities_count;
117 }
118 
media_get_entity(struct media_device * media,unsigned int index)119 struct media_entity *media_get_entity(struct media_device *media, unsigned int index)
120 {
121 	if (index >= media->entities_count)
122 		return NULL;
123 
124 	return &media->entities[index];
125 }
126 
media_entity_get_pad(struct media_entity * entity,unsigned int index)127 const struct media_pad *media_entity_get_pad(struct media_entity *entity, unsigned int index)
128 {
129 	if (index >= entity->info.pads)
130 		return NULL;
131 
132 	return &entity->pads[index];
133 }
134 
media_entity_get_links_count(struct media_entity * entity)135 unsigned int media_entity_get_links_count(struct media_entity *entity)
136 {
137 	return entity->num_links;
138 }
139 
media_entity_get_link(struct media_entity * entity,unsigned int index)140 const struct media_link *media_entity_get_link(struct media_entity *entity, unsigned int index)
141 {
142 	if (index >= entity->num_links)
143 		return NULL;
144 
145 	return &entity->links[index];
146 }
147 
media_entity_get_devname(struct media_entity * entity)148 const char *media_entity_get_devname(struct media_entity *entity)
149 {
150     if (entity == NULL) {
151         return NULL;
152     }
153 
154 	return entity->devname[0] ? entity->devname : NULL;
155 }
156 
media_get_default_entity(struct media_device * media,unsigned int type)157 struct media_entity *media_get_default_entity(struct media_device *media,
158 					      unsigned int type)
159 {
160 	switch (type) {
161 	case MEDIA_ENT_T_DEVNODE_V4L:
162 		return media->def.v4l;
163 	case MEDIA_ENT_T_DEVNODE_FB:
164 		return media->def.fb;
165 	case MEDIA_ENT_T_DEVNODE_ALSA:
166 		return media->def.alsa;
167 	case MEDIA_ENT_T_DEVNODE_DVB:
168 		return media->def.dvb;
169 	}
170 
171 	return NULL;
172 }
173 
media_get_info(struct media_device * media)174 const struct media_device_info *media_get_info(struct media_device *media)
175 {
176 	return &media->info;
177 }
178 
media_get_devnode(struct media_device * media)179 const char *media_get_devnode(struct media_device *media)
180 {
181 	return media->devnode;
182 }
183 
media_entity_get_info(struct media_entity * entity)184 const struct media_entity_desc *media_entity_get_info(struct media_entity *entity)
185 {
186 	return &entity->info;
187 }
188 
189 /* -----------------------------------------------------------------------------
190  * Open/close
191  */
192 
media_device_open(struct media_device * media)193 static int media_device_open(struct media_device *media)
194 {
195 	int ret;
196 
197 	if (media->fd != -1)
198 		return 0;
199 
200 	media_dbg(media, "Opening media device %s\n", media->devnode);
201 
202 	media->fd = open(media->devnode, O_RDWR);
203 	if (media->fd < 0) {
204 		ret = -errno;
205 		media_dbg(media, "%s: Can't open media device %s\n",
206 			  __func__, media->devnode);
207 		return ret;
208 	}
209 
210 	return 0;
211 }
212 
media_device_close(struct media_device * media)213 static void media_device_close(struct media_device *media)
214 {
215 	if (media->fd != -1) {
216 		close(media->fd);
217 		media->fd = -1;
218 	}
219 }
220 
221 /* -----------------------------------------------------------------------------
222  * Link setup
223  */
224 
media_setup_link(struct media_device * media,struct media_pad * source,struct media_pad * sink,__u32 flags)225 int media_setup_link(struct media_device *media,
226 		     struct media_pad *source,
227 		     struct media_pad *sink,
228 		     __u32 flags)
229 {
230 	struct media_link *link = 0;
231 	struct media_link_desc ulink;
232 	unsigned int i;
233 	int ret;
234 
235 	ret = media_device_open(media);
236 	if (ret < 0)
237 		goto done;
238 
239 	for (i = 0; i < source->entity->num_links; i++) {
240 		link = &source->entity->links[i];
241 
242 		if (link->source->entity == source->entity &&
243 		    link->source->index == source->index &&
244 		    link->sink->entity == sink->entity &&
245 		    link->sink->index == sink->index)
246 			break;
247 	}
248 
249 	if (i == source->entity->num_links) {
250 		media_dbg(media, "%s: Link not found\n", __func__);
251 		ret = -ENOENT;
252 		goto done;
253 	}
254 
255 	/* source pad */
256 	memset(&ulink, 0, sizeof(ulink));
257 	ulink.source.entity = source->entity->info.id;
258 	ulink.source.index = source->index;
259 	ulink.source.flags = MEDIA_PAD_FL_SOURCE;
260 
261 	/* sink pad */
262 	ulink.sink.entity = sink->entity->info.id;
263 	ulink.sink.index = sink->index;
264 	ulink.sink.flags = MEDIA_PAD_FL_SINK;
265 
266 	ulink.flags = flags | (link->flags & MEDIA_LNK_FL_IMMUTABLE);
267 
268 	ret = ioctl(media->fd, MEDIA_IOC_SETUP_LINK, &ulink);
269 	if (ret == -1) {
270 		ret = -errno;
271 		media_dbg(media, "%s: Unable to setup link (%s)\n",
272 			  __func__, strerror(errno));
273 		goto done;
274 	}
275 
276 	if(0 != link) {
277 		link->flags = ulink.flags;
278 		link->twin->flags = ulink.flags;
279 		ret = 0;
280 	}
281 done:
282 	media_device_close(media);
283 	return ret;
284 }
285 
media_reset_links(struct media_device * media)286 int media_reset_links(struct media_device *media)
287 {
288 	unsigned int i, j;
289 	int ret;
290 
291 	for (i = 0; i < media->entities_count; ++i) {
292 		struct media_entity *entity = &media->entities[i];
293 
294 		for (j = 0; j < entity->num_links; j++) {
295 			struct media_link *link = &entity->links[j];
296 
297 			if (link->flags & MEDIA_LNK_FL_IMMUTABLE ||
298 			    link->source->entity != entity)
299 				continue;
300 
301 			ret = media_setup_link(media, link->source, link->sink,
302 					       link->flags & ~MEDIA_LNK_FL_ENABLED);
303 			if (ret < 0)
304 				return ret;
305 		}
306 	}
307 
308 	return 0;
309 }
310 
311 /* -----------------------------------------------------------------------------
312  * Entities, pads and links enumeration
313  */
314 
media_entity_add_link(struct media_entity * entity)315 static struct media_link *media_entity_add_link(struct media_entity *entity)
316 {
317 	if (entity->num_links >= entity->max_links) {
318 		struct media_link *links = entity->links;
319 		unsigned int max_links = entity->max_links * 2;
320 		unsigned int i;
321 
322 		links = realloc(links, max_links * sizeof *links);
323 		if (links == NULL)
324 			return NULL;
325 
326 		for (i = 0; i < entity->num_links; ++i)
327 			links[i].twin->twin = &links[i];
328 
329 		entity->max_links = max_links;
330 		entity->links = links;
331 	}
332 
333 	return &entity->links[entity->num_links++];
334 }
335 
media_enum_links(struct media_device * media)336 static int media_enum_links(struct media_device *media)
337 {
338 	__u32 id;
339 	int ret = 0;
340 
341 	for (id = 1; id <= media->entities_count; id++) {
342 		struct media_entity *entity = &media->entities[id - 1];
343 		struct media_links_enum links;
344 		unsigned int i;
345 
346 		memset(&links, 0, sizeof(links));
347 		links.entity = entity->info.id;
348 		links.pads = calloc(entity->info.pads, sizeof(struct media_pad_desc));
349 		links.links = calloc(entity->info.links, sizeof(struct media_link_desc));
350 
351 		if (ioctl(media->fd, MEDIA_IOC_ENUM_LINKS, &links) < 0) {
352 			ret = -errno;
353 			media_dbg(media,
354 				  "%s: Unable to enumerate pads and links (%s).\n",
355 				  __func__, strerror(errno));
356 			free(links.pads);
357 			free(links.links);
358 			return ret;
359 		}
360 
361 		for (i = 0; i < entity->info.pads; ++i) {
362 			entity->pads[i].entity = entity;
363 			entity->pads[i].index = links.pads[i].index;
364 			entity->pads[i].flags = links.pads[i].flags;
365 		}
366 
367 		for (i = 0; i < entity->info.links; ++i) {
368 			struct media_link_desc *link = &links.links[i];
369 			struct media_link *fwdlink;
370 			struct media_link *backlink;
371 			struct media_entity *source;
372 			struct media_entity *sink;
373 
374 			source = media_get_entity_by_id(media, link->source.entity);
375 			sink = media_get_entity_by_id(media, link->sink.entity);
376 
377 			if (source == NULL || sink == NULL) {
378 				media_dbg(media,
379 					  "WARNING entity %u link %u from %u/%u to %u/%u is invalid!\n",
380 					  id, i, link->source.entity,
381 					  link->source.index,
382 					  link->sink.entity,
383 					  link->sink.index);
384 				ret = -EINVAL;
385 			} else {
386 				fwdlink = media_entity_add_link(source);
387 				fwdlink->source = &source->pads[link->source.index];
388 				fwdlink->sink = &sink->pads[link->sink.index];
389 				fwdlink->flags = link->flags;
390 
391 				backlink = media_entity_add_link(sink);
392 				backlink->source = &source->pads[link->source.index];
393 				backlink->sink = &sink->pads[link->sink.index];
394 				backlink->flags = link->flags;
395 
396 				fwdlink->twin = backlink;
397 				backlink->twin = fwdlink;
398 			}
399 		}
400 
401 		free(links.pads);
402 		free(links.links);
403 	}
404 
405 	return ret;
406 }
407 
408 #ifdef HAVE_LIBUDEV
409 
410 #include <libudev.h>
411 
media_udev_open(struct udev ** udev)412 static inline int media_udev_open(struct udev **udev)
413 {
414 	*udev = udev_new();
415 	if (*udev == NULL)
416 		return -ENOMEM;
417 	return 0;
418 }
419 
media_udev_close(struct udev * udev)420 static inline void media_udev_close(struct udev *udev)
421 {
422 	if (udev != NULL)
423 		udev_unref(udev);
424 }
425 
media_get_devname_udev(struct udev * udev,struct media_entity * entity)426 static int media_get_devname_udev(struct udev *udev,
427 		struct media_entity *entity)
428 {
429 	struct udev_device *device;
430 	dev_t devnum;
431 	const char *p;
432 	int ret = -ENODEV;
433 
434 	if (udev == NULL)
435 		return -EINVAL;
436 
437 	devnum = makedev(entity->info.v4l.major, entity->info.v4l.minor);
438 	media_dbg(entity->media, "looking up device: %u:%u\n",
439 		  major(devnum), minor(devnum));
440 	device = udev_device_new_from_devnum(udev, 'c', devnum);
441 	if (device) {
442 		p = udev_device_get_devnode(device);
443 		if (p) {
444 			strncpy(entity->devname, p, sizeof(entity->devname));
445 			entity->devname[sizeof(entity->devname) - 1] = '\0';
446 		}
447 		ret = 0;
448 	}
449 
450 	udev_device_unref(device);
451 
452 	return ret;
453 }
454 
455 #else	/* HAVE_LIBUDEV */
456 
457 struct udev;
458 
media_udev_open(struct udev ** udev)459 static inline int media_udev_open(struct udev **udev) { return 0; }
460 
media_udev_close(struct udev * udev)461 static inline void media_udev_close(struct udev *udev) { }
462 
media_get_devname_udev(struct udev * udev,struct media_entity * entity)463 static inline int media_get_devname_udev(struct udev *udev,
464 		struct media_entity *entity)
465 {
466 	return -ENOTSUP;
467 }
468 
469 #endif	/* HAVE_LIBUDEV */
470 
media_get_devname_sysfs(struct media_entity * entity)471 static int media_get_devname_sysfs(struct media_entity *entity)
472 {
473 	struct stat devstat;
474 	char devname[32];
475 	char sysname[32];
476 	char target[1024];
477 	char *p;
478 	int ret;
479 
480 	sprintf(sysname, "/sys/dev/char/%u:%u", entity->info.v4l.major,
481 		entity->info.v4l.minor);
482 	ret = readlink(sysname, target, sizeof(target));
483 	if (ret < 0)
484 		return -errno;
485 
486 	target[ret] = '\0';
487 	p = strrchr(target, '/');
488 	if (p == NULL)
489 		return -EINVAL;
490 
491 	sprintf(devname, "/dev/%s", p + 1);
492 	ret = stat(devname, &devstat);
493 	if (ret < 0)
494 		return -errno;
495 
496 	/* Sanity check: udev might have reordered the device nodes.
497 	 * Make sure the major/minor match. We should really use
498 	 * libudev.
499 	 */
500 	if (major(devstat.st_rdev) == entity->info.v4l.major &&
501 	    minor(devstat.st_rdev) == entity->info.v4l.minor)
502 		strcpy(entity->devname, devname);
503 
504 	return 0;
505 }
506 
media_enum_entities(struct media_device * media)507 static int media_enum_entities(struct media_device *media)
508 {
509 	struct media_entity *entity;
510 	struct udev *udev;
511 	unsigned int size;
512 	__u32 id;
513 	int ret;
514 
515 	ret = media_udev_open(&udev);
516 	if (ret < 0)
517 		media_dbg(media, "Can't get udev context\n");
518 
519 	for (id = 0, ret = 0; ; id = entity->info.id) {
520 		size = (media->entities_count + 1) * sizeof(*media->entities);
521 		media->entities = realloc(media->entities, size);
522 
523 		entity = &media->entities[media->entities_count];
524 		memset(entity, 0, sizeof(*entity));
525 		entity->fd = -1;
526 		entity->info.id = id | MEDIA_ENT_ID_FLAG_NEXT;
527 		entity->media = media;
528 
529 		ret = ioctl(media->fd, MEDIA_IOC_ENUM_ENTITIES, &entity->info);
530 		if (ret < 0) {
531 			ret = errno != EINVAL ? -errno : 0;
532 			break;
533 		}
534 
535 		/* Number of links (for outbound links) plus number of pads (for
536 		 * inbound links) is a good safe initial estimate of the total
537 		 * number of links.
538 		 */
539 		entity->max_links = entity->info.pads + entity->info.links;
540 
541 		entity->pads = malloc(entity->info.pads * sizeof(*entity->pads));
542 		entity->links = malloc(entity->max_links * sizeof(*entity->links));
543 		if (entity->pads == NULL || entity->links == NULL) {
544 			ret = -ENOMEM;
545 			break;
546 		}
547 
548 		media->entities_count++;
549 
550 		if (entity->info.flags & MEDIA_ENT_FL_DEFAULT) {
551 			switch (entity->info.type) {
552 			case MEDIA_ENT_T_DEVNODE_V4L:
553 				media->def.v4l = entity;
554 				break;
555 			case MEDIA_ENT_T_DEVNODE_FB:
556 				media->def.fb = entity;
557 				break;
558 			case MEDIA_ENT_T_DEVNODE_ALSA:
559 				media->def.alsa = entity;
560 				break;
561 			case MEDIA_ENT_T_DEVNODE_DVB:
562 				media->def.dvb = entity;
563 				break;
564 			}
565 		}
566 
567 		/* Find the corresponding device name. */
568 		if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE &&
569 		    media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV)
570 			continue;
571 
572 		/* Try to get the device name via udev */
573 		if (!media_get_devname_udev(udev, entity))
574 			continue;
575 
576 		/* Fall back to get the device name via sysfs */
577 		media_get_devname_sysfs(entity);
578 	}
579 
580 	media_udev_close(udev);
581 	return ret;
582 }
583 
media_device_enumerate(struct media_device * media)584 int media_device_enumerate(struct media_device *media)
585 {
586 	int ret;
587 
588 	if (media->entities)
589 		return 0;
590 
591 	ret = media_device_open(media);
592 	if (ret < 0)
593 		return ret;
594 
595 	ret = ioctl(media->fd, MEDIA_IOC_DEVICE_INFO, &media->info);
596 	if (ret < 0) {
597 		ret = -errno;
598 		media_dbg(media, "%s: Unable to retrieve media device "
599 			  "information for device %s (%s)\n", __func__,
600 			  media->devnode, strerror(errno));
601 		goto done;
602 	}
603 
604 	media_dbg(media, "Enumerating entities\n");
605 
606 	ret = media_enum_entities(media);
607 	if (ret < 0) {
608 		media_dbg(media,
609 			  "%s: Unable to enumerate entities for device %s (%s)\n",
610 			  __func__, media->devnode, strerror(-ret));
611 		goto done;
612 	}
613 
614 	media_dbg(media, "Found %u entities\n", media->entities_count);
615 	media_dbg(media, "Enumerating pads and links\n");
616 
617 	ret = media_enum_links(media);
618 	if (ret < 0) {
619 		media_dbg(media,
620 			  "%s: Unable to enumerate pads and linksfor device %s\n",
621 			  __func__, media->devnode);
622 		goto done;
623 	}
624 
625 	ret = 0;
626 
627 done:
628 	media_device_close(media);
629 	return ret;
630 }
631 
632 /* -----------------------------------------------------------------------------
633  * Create/destroy
634  */
635 
media_debug_default(void * ptr,...)636 static void media_debug_default(void *ptr, ...)
637 {
638 }
639 
media_debug_set_handler(struct media_device * media,void (* debug_handler)(void *,...),void * debug_priv)640 void media_debug_set_handler(struct media_device *media,
641 			     void (*debug_handler)(void *, ...),
642 			     void *debug_priv)
643 {
644 	if (debug_handler) {
645 		media->debug_handler = debug_handler;
646 		media->debug_priv = debug_priv;
647 	} else {
648 		media->debug_handler = media_debug_default;
649 		media->debug_priv = NULL;
650 	}
651 }
652 
__media_device_new(void)653 static struct media_device *__media_device_new(void)
654 {
655 	struct media_device *media;
656 
657 	media = calloc(1, sizeof(*media));
658 	if (media == NULL)
659 		return NULL;
660 
661 	media->fd = -1;
662 	media->refcount = 1;
663 
664 	media_debug_set_handler(media, NULL, NULL);
665 
666 	return media;
667 }
668 
media_device_new(const char * devnode)669 struct media_device *media_device_new(const char *devnode)
670 {
671 	struct media_device *media;
672 
673 	media = __media_device_new();
674 	if (media == NULL)
675 		return NULL;
676 
677 	media->devnode = strdup(devnode);
678 	if (media->devnode == NULL) {
679 		media_device_unref(media);
680 		return NULL;
681 	}
682 
683 	return media;
684 }
685 
media_device_new_emulated(struct media_device_info * info)686 struct media_device *media_device_new_emulated(struct media_device_info *info)
687 {
688 	struct media_device *media;
689 
690 	media = __media_device_new();
691 	if (media == NULL)
692 		return NULL;
693 
694 	media->info = *info;
695 
696 	return media;
697 }
698 
media_device_ref(struct media_device * media)699 struct media_device *media_device_ref(struct media_device *media)
700 {
701 	media->refcount++;
702 	return media;
703 }
704 
media_device_unref(struct media_device * media)705 void media_device_unref(struct media_device *media)
706 {
707 	unsigned int i;
708 
709 	media->refcount--;
710 	if (media->refcount > 0)
711 		return;
712 
713 	for (i = 0; i < media->entities_count; ++i) {
714 		struct media_entity *entity = &media->entities[i];
715 
716 		free(entity->pads);
717 		free(entity->links);
718 		if (entity->fd != -1)
719 			close(entity->fd);
720 	}
721 
722 	free(media->entities);
723 	free(media->devnode);
724 	free(media);
725 }
726 
media_device_add_entity(struct media_device * media,const struct media_entity_desc * desc,const char * devnode)727 int media_device_add_entity(struct media_device *media,
728 			    const struct media_entity_desc *desc,
729 			    const char *devnode)
730 {
731 	struct media_entity **defent = NULL;
732 	struct media_entity *entity;
733 	unsigned int size;
734 
735 	size = (media->entities_count + 1) * sizeof(*media->entities);
736 	entity = realloc(media->entities, size);
737 	if (entity == NULL)
738 		return -ENOMEM;
739 
740 	media->entities = entity;
741 	media->entities_count++;
742 
743 	entity = &media->entities[media->entities_count - 1];
744 	memset(entity, 0, sizeof *entity);
745 
746 	entity->fd = -1;
747 	entity->media = media;
748 	strncpy(entity->devname, devnode, sizeof(entity->devname) - 1);
749 	entity->devname[sizeof entity->devname - 1] = '\0';
750 
751 	entity->info.id = 0;
752 	entity->info.type = desc->type;
753 	entity->info.flags = 0;
754 	memcpy(entity->info.name, desc->name, sizeof entity->info.name);
755 
756 	switch (entity->info.type) {
757 	case MEDIA_ENT_T_DEVNODE_V4L:
758 		defent = &media->def.v4l;
759 		entity->info.v4l = desc->v4l;
760 		break;
761 	case MEDIA_ENT_T_DEVNODE_FB:
762 		defent = &media->def.fb;
763 		entity->info.fb = desc->fb;
764 		break;
765 	case MEDIA_ENT_T_DEVNODE_ALSA:
766 		defent = &media->def.alsa;
767 		entity->info.alsa = desc->alsa;
768 		break;
769 	case MEDIA_ENT_T_DEVNODE_DVB:
770 		defent = &media->def.dvb;
771 		entity->info.dvb = desc->dvb;
772 		break;
773 	}
774 
775 	if (desc->flags & MEDIA_ENT_FL_DEFAULT) {
776 		entity->info.flags |= MEDIA_ENT_FL_DEFAULT;
777 		if (defent)
778 			*defent = entity;
779 	}
780 
781 	return 0;
782 }
783 
media_parse_pad(struct media_device * media,const char * p,char ** endp)784 struct media_pad *media_parse_pad(struct media_device *media,
785 				  const char *p, char **endp)
786 {
787 	unsigned int entity_id, pad;
788 	struct media_entity *entity;
789 	char *end;
790 
791 	/* endp can be NULL. To avoid spreading NULL checks across the function,
792 	 * set endp to &end in that case.
793 	 */
794 	if (endp == NULL)
795 		endp = &end;
796 
797 	for (; isspace(*p); ++p);
798 
799 	if (*p == '"' || *p == '\'') {
800 		for (end = (char *)p + 1; *end && *end != '"' && *end != '\''; ++end);
801 		if (*end != '"' && *end != '\'') {
802 			media_dbg(media, "missing matching '\"'\n");
803 			*endp = end;
804 			return NULL;
805 		}
806 
807 		entity = media_get_entity_by_name(media, p + 1, end - p - 1);
808 		if (entity == NULL) {
809 			media_dbg(media, "no such entity \"%.*s\"\n", end - p - 1, p + 1);
810 			*endp = (char *)p + 1;
811 			return NULL;
812 		}
813 
814 		++end;
815 	} else {
816 		entity_id = strtoul(p, &end, 10);
817 		entity = media_get_entity_by_id(media, entity_id);
818 		if (entity == NULL) {
819 			media_dbg(media, "no such entity %d\n", entity_id);
820 			*endp = (char *)p;
821 			return NULL;
822 		}
823 	}
824 	for (; isspace(*end); ++end);
825 
826 	if (*end != ':') {
827 		media_dbg(media, "Expected ':'\n", *end);
828 		*endp = end;
829 		return NULL;
830 	}
831 
832 	for (p = end + 1; isspace(*p); ++p);
833 
834 	pad = strtoul(p, &end, 10);
835 
836 	if (pad >= entity->info.pads) {
837 		media_dbg(media, "No pad '%d' on entity \"%s\". Maximum pad number is %d\n",
838 				pad, entity->info.name, entity->info.pads - 1);
839 		*endp = (char *)p;
840 		return NULL;
841 	}
842 
843 	for (p = end; isspace(*p); ++p);
844 	*endp = (char *)p;
845 
846 	return &entity->pads[pad];
847 }
848 
media_parse_link(struct media_device * media,const char * p,char ** endp)849 struct media_link *media_parse_link(struct media_device *media,
850 				    const char *p, char **endp)
851 {
852 	struct media_link *link;
853 	struct media_pad *source;
854 	struct media_pad *sink;
855 	unsigned int i;
856 	char *end;
857 
858 	source = media_parse_pad(media, p, &end);
859 	if (source == NULL) {
860 		*endp = end;
861 		return NULL;
862 	}
863 
864 	if (end[0] != '-' || end[1] != '>') {
865 		*endp = end;
866 		media_dbg(media, "Expected '->'\n");
867 		return NULL;
868 	}
869 
870 	p = end + 2;
871 
872 	sink = media_parse_pad(media, p, &end);
873 	if (sink == NULL) {
874 		*endp = end;
875 		return NULL;
876 	}
877 
878 	*endp = end;
879 
880 	for (i = 0; i < source->entity->num_links; i++) {
881 		link = &source->entity->links[i];
882 
883 		if (link->source == source && link->sink == sink)
884 			return link;
885 	}
886 
887 	media_dbg(media, "No link between \"%s\":%d and \"%s\":%d\n",
888 			source->entity->info.name, source->index,
889 			sink->entity->info.name, sink->index);
890 	return NULL;
891 }
892 
media_parse_setup_link(struct media_device * media,const char * p,char ** endp)893 int media_parse_setup_link(struct media_device *media,
894 			   const char *p, char **endp)
895 {
896 	struct media_link *link;
897 	__u32 flags;
898 	char *end;
899 
900 	link = media_parse_link(media, p, &end);
901 	if (link == NULL) {
902 		media_dbg(media,
903 			  "%s: Unable to parse link\n", __func__);
904 		*endp = end;
905 		return -EINVAL;
906 	}
907 
908 	p = end;
909 	if (*p++ != '[') {
910 		media_dbg(media, "Unable to parse link flags: expected '['.\n");
911 		*endp = (char *)p - 1;
912 		return -EINVAL;
913 	}
914 
915 	flags = strtoul(p, &end, 10);
916 	for (p = end; isspace(*p); p++);
917 	if (*p++ != ']') {
918 		media_dbg(media, "Unable to parse link flags: expected ']'.\n");
919 		*endp = (char *)p - 1;
920 		return -EINVAL;
921 	}
922 
923 	for (; isspace(*p); p++);
924 	*endp = (char *)p;
925 
926 	media_dbg(media,
927 		  "Setting up link %u:%u -> %u:%u [%u]\n",
928 		  link->source->entity->info.id, link->source->index,
929 		  link->sink->entity->info.id, link->sink->index,
930 		  flags);
931 
932 	return media_setup_link(media, link->source, link->sink, flags);
933 }
934 
media_print_streampos(struct media_device * media,const char * p,const char * end)935 void media_print_streampos(struct media_device *media, const char *p,
936 			   const char *end)
937 {
938 	size_t pos;
939 
940 	pos = end - p + 1;
941 
942 	if ((ssize_t)pos < 0)
943 		pos = 0;
944 	if (pos > strlen(p))
945 		pos = strlen(p);
946 
947 	media_dbg(media, "\n");
948 	media_dbg(media, " %s\n", p);
949 	media_dbg(media, " %*s\n", pos, "^");
950 }
951 
media_parse_setup_links(struct media_device * media,const char * p)952 int media_parse_setup_links(struct media_device *media, const char *p)
953 {
954 	char *end;
955 	int ret;
956 
957 	do {
958 		ret = media_parse_setup_link(media, p, &end);
959 		if (ret < 0) {
960 			media_print_streampos(media, p, end);
961 			return ret;
962 		}
963 
964 		p = end + 1;
965 	} while (*end == ',');
966 
967 	return *end ? -EINVAL : 0;
968 }
969