Debian Codegraph @

utils/media-ctl/libv4l2subdev.c

raw
1 /*
2 * V4L2 subdev interface library
3 *
4 * Copyright (C) 2010-2014 Ideas on board SPRL
5 *
6 * Contact: Laurent Pinchart <[email protected]>
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/ioctl.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25
26 #include <ctype.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 #include <linux/v4l2-subdev.h>
37
38 #include "mediactl.h"
39 #include "mediactl-priv.h"
40 #include "tools.h"
41 #include "v4l2subdev.h"
42
43 /*
44 * The max value comes from a check in the kernel source code
45 * drivers/media/v4l2-core/v4l2-ioctl.c check_array_args()
46 */
47 #define NUM_ROUTES_MAX 256
48
49 int v4l2_subdev_open(struct media_entity *entity)
50 {
51 struct v4l2_subdev_client_capability clientcap = {};
52 struct v4l2_subdev_capability subdevcap = {};
53 bool subdev_streams;
54 bool client_streams;
55 int ret;
56
57 if (entity->fd != -1)
58 return 0;
59
60 entity->fd = open(entity->devname, O_RDWR);
61 if (entity->fd == -1) {
62 int ret = -errno;
63 media_dbg(entity->media,
64 "%s: Failed to open subdev device node %s\n", __func__,
65 entity->devname);
66 return ret;
67 }
68
69 ret = ioctl(entity->fd, VIDIOC_SUBDEV_QUERYCAP, &subdevcap);
70 subdev_streams = !ret && (subdevcap.capabilities & V4L2_SUBDEV_CAP_STREAMS);
71
72 clientcap.capabilities = V4L2_SUBDEV_CLIENT_CAP_STREAMS
73 | V4L2_SUBDEV_CLIENT_CAP_INTERVAL_USES_WHICH;
74
75 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CLIENT_CAP, &clientcap);
76 client_streams = !ret && (clientcap.capabilities & V4L2_SUBDEV_CLIENT_CAP_STREAMS);
77
78 entity->supports_streams = subdev_streams && client_streams;
79
80 return 0;
81 }
82
83 void v4l2_subdev_close(struct media_entity *entity)
84 {
85 close(entity->fd);
86 entity->fd = -1;
87 }
88
89 int v4l2_subdev_get_format(struct media_entity *entity,
90 struct v4l2_mbus_framefmt *format, unsigned int pad, unsigned int stream,
91 enum v4l2_subdev_format_whence which)
92 {
93 struct v4l2_subdev_format fmt;
94 int ret;
95
96 ret = v4l2_subdev_open(entity);
97 if (ret < 0)
98 return ret;
99
100 if (!entity->supports_streams && stream) {
101 media_dbg(entity->media, "Streams API not supported\n");
102 return -ENOTSUP;
103 }
104
105 memset(&fmt, 0, sizeof(fmt));
106 fmt.pad = pad;
107 fmt.stream = stream;
108 fmt.which = which;
109
110 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FMT, &fmt);
111 if (ret < 0)
112 return -errno;
113
114 *format = fmt.format;
115 return 0;
116 }
117
118 int v4l2_subdev_set_format(struct media_entity *entity,
119 struct v4l2_mbus_framefmt *format, unsigned int pad,
120 unsigned int stream,
121 enum v4l2_subdev_format_whence which)
122 {
123 struct v4l2_subdev_format fmt;
124 int ret;
125
126 ret = v4l2_subdev_open(entity);
127 if (ret < 0)
128 return ret;
129
130 if (!entity->supports_streams && stream) {
131 media_dbg(entity->media, "Streams API not supported\n");
132 return -ENOTSUP;
133 }
134
135 memset(&fmt, 0, sizeof(fmt));
136 fmt.pad = pad;
137 fmt.stream = stream;
138 fmt.which = which;
139 fmt.format = *format;
140
141 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FMT, &fmt);
142 if (ret < 0)
143 return -errno;
144
145 *format = fmt.format;
146 return 0;
147 }
148
149 int v4l2_subdev_get_selection(struct media_entity *entity,
150 struct v4l2_rect *rect, unsigned int pad, unsigned int stream,
151 unsigned int target, enum v4l2_subdev_format_whence which)
152 {
153 union {
154 struct v4l2_subdev_selection sel;
155 struct v4l2_subdev_crop crop;
156 } u;
157 int ret;
158
159 ret = v4l2_subdev_open(entity);
160 if (ret < 0)
161 return ret;
162
163 if (!entity->supports_streams && stream) {
164 media_dbg(entity->media, "Streams API not supported\n");
165 return -ENOTSUP;
166 }
167
168 memset(&u.sel, 0, sizeof(u.sel));
169 u.sel.pad = pad;
170 u.sel.stream = stream;
171 u.sel.target = target;
172 u.sel.which = which;
173
174 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_SELECTION, &u.sel);
175 if (ret >= 0) {
176 *rect = u.sel.r;
177 return 0;
178 }
179 if (errno != ENOTTY || target != V4L2_SEL_TGT_CROP)
180 return -errno;
181
182 memset(&u.crop, 0, sizeof(u.crop));
183 u.crop.pad = pad;
184 u.crop.which = which;
185
186 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_CROP, &u.crop);
187 if (ret < 0)
188 return -errno;
189
190 *rect = u.crop.rect;
191 return 0;
192 }
193
194 int v4l2_subdev_set_selection(struct media_entity *entity,
195 struct v4l2_rect *rect, unsigned int pad, unsigned int stream,
196 unsigned int target, enum v4l2_subdev_format_whence which)
197 {
198 union {
199 struct v4l2_subdev_selection sel;
200 struct v4l2_subdev_crop crop;
201 } u;
202 int ret;
203
204 ret = v4l2_subdev_open(entity);
205 if (ret < 0)
206 return ret;
207
208 if (!entity->supports_streams && stream) {
209 media_dbg(entity->media, "Streams API not supported\n");
210 return -ENOTSUP;
211 }
212
213 memset(&u.sel, 0, sizeof(u.sel));
214 u.sel.pad = pad;
215 u.sel.stream = stream;
216 u.sel.target = target;
217 u.sel.which = which;
218 u.sel.r = *rect;
219
220 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_SELECTION, &u.sel);
221 if (ret >= 0) {
222 *rect = u.sel.r;
223 return 0;
224 }
225 if (errno != ENOTTY || target != V4L2_SEL_TGT_CROP)
226 return -errno;
227
228 memset(&u.crop, 0, sizeof(u.crop));
229 u.crop.pad = pad;
230 u.crop.stream = stream;
231 u.crop.which = which;
232 u.crop.rect = *rect;
233
234 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CROP, &u.crop);
235 if (ret < 0)
236 return -errno;
237
238 *rect = u.crop.rect;
239 return 0;
240 }
241
242 int v4l2_subdev_get_routing(struct media_entity *entity,
243 struct v4l2_subdev_route **routes,
244 unsigned int *num_routes,
245 enum v4l2_subdev_format_whence which)
246 {
247 struct v4l2_subdev_routing routing = {
248 .which = which,
249 };
250 struct v4l2_subdev_route *r;
251 int ret;
252
253 ret = v4l2_subdev_open(entity);
254 if (ret < 0)
255 return ret;
256
257 if (!entity->supports_streams) {
258 media_dbg(entity->media, "Streams API not supported\n");
259 return -ENOTSUP;
260 }
261
262 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_ROUTING, &routing);
263 if (ret == -1)
264 return -errno;
265
266 if (!routing.num_routes) {
267 *routes = NULL;
268 *num_routes = 0;
269 return 0;
270 }
271
272 r = calloc(routing.num_routes, sizeof(*r));
273 if (!r)
274 return -ENOMEM;
275
276 routing.routes = (uintptr_t)r;
277 routing.len_routes = routing.num_routes;
278 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_ROUTING, &routing);
279 if (ret) {
280 free(r);
281 return ret;
282 }
283
284 *num_routes = routing.num_routes;
285 *routes = r;
286
287 return 0;
288 }
289
290 int v4l2_subdev_set_routing(struct media_entity *entity,
291 struct v4l2_subdev_route *routes,
292 unsigned int num_routes,
293 enum v4l2_subdev_format_whence which)
294 {
295 struct v4l2_subdev_routing routing = {
296 .which = which,
297 .routes = (uintptr_t)routes,
298 .num_routes = num_routes,
299 .len_routes = num_routes,
300 };
301 int ret;
302
303 ret = v4l2_subdev_open(entity);
304 if (ret < 0)
305 return ret;
306
307 if (!entity->supports_streams) {
308 media_dbg(entity->media, "Streams API not supported\n");
309 return -ENOTSUP;
310 }
311
312 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_ROUTING, &routing);
313 if (ret == -1)
314 return -errno;
315
316 return 0;
317 }
318
319 int v4l2_subdev_get_dv_timings_caps(struct media_entity *entity,
320 struct v4l2_dv_timings_cap *caps)
321 {
322 unsigned int pad = caps->pad;
323 int ret;
324
325 ret = v4l2_subdev_open(entity);
326 if (ret < 0)
327 return ret;
328
329 memset(caps, 0, sizeof(*caps));
330 caps->pad = pad;
331
332 ret = ioctl(entity->fd, VIDIOC_SUBDEV_DV_TIMINGS_CAP, caps);
333 if (ret < 0)
334 return -errno;
335
336 return 0;
337 }
338
339 int v4l2_subdev_query_dv_timings(struct media_entity *entity,
340 struct v4l2_dv_timings *timings)
341 {
342 int ret;
343
344 ret = v4l2_subdev_open(entity);
345 if (ret < 0)
346 return ret;
347
348 memset(timings, 0, sizeof(*timings));
349
350 ret = ioctl(entity->fd, VIDIOC_SUBDEV_QUERY_DV_TIMINGS, timings);
351 if (ret < 0)
352 return -errno;
353
354 return 0;
355 }
356
357 int v4l2_subdev_get_dv_timings(struct media_entity *entity,
358 struct v4l2_dv_timings *timings)
359 {
360 int ret;
361
362 ret = v4l2_subdev_open(entity);
363 if (ret < 0)
364 return ret;
365
366 memset(timings, 0, sizeof(*timings));
367
368 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_DV_TIMINGS, timings);
369 if (ret < 0)
370 return -errno;
371
372 return 0;
373 }
374
375 int v4l2_subdev_set_dv_timings(struct media_entity *entity,
376 struct v4l2_dv_timings *timings)
377 {
378 int ret;
379
380 ret = v4l2_subdev_open(entity);
381 if (ret < 0)
382 return ret;
383
384 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_DV_TIMINGS, timings);
385 if (ret < 0)
386 return -errno;
387
388 return 0;
389 }
390
391 int v4l2_subdev_get_frame_interval(struct media_entity *entity,
392 struct v4l2_fract *interval,
393 unsigned int pad, unsigned int stream,
394 enum v4l2_subdev_format_whence which)
395 {
396 struct v4l2_subdev_frame_interval ival;
397 int ret;
398
399 ret = v4l2_subdev_open(entity);
400 if (ret < 0)
401 return ret;
402
403 if (!entity->supports_streams && stream) {
404 media_dbg(entity->media, "Streams API not supported\n");
405 return -ENOTSUP;
406 }
407
408 memset(&ival, 0, sizeof(ival));
409 ival.pad = pad;
410 ival.stream = stream;
411 ival.which = which;
412
413 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FRAME_INTERVAL, &ival);
414 if (ret < 0)
415 return -errno;
416
417 *interval = ival.interval;
418 return 0;
419 }
420
421 int v4l2_subdev_set_frame_interval(struct media_entity *entity,
422 struct v4l2_fract *interval,
423 unsigned int pad, unsigned int stream,
424 enum v4l2_subdev_format_whence which)
425 {
426 struct v4l2_subdev_frame_interval ival;
427 int ret;
428
429 ret = v4l2_subdev_open(entity);
430 if (ret < 0)
431 return ret;
432
433 if (!entity->supports_streams && stream) {
434 media_dbg(entity->media, "Streams API not supported\n");
435 return -ENOTSUP;
436 }
437
438 memset(&ival, 0, sizeof(ival));
439 ival.pad = pad;
440 ival.stream = stream;
441 ival.interval = *interval;
442 ival.which = which;
443
444 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &ival);
445 if (ret < 0)
446 return -errno;
447
448 *interval = ival.interval;
449 return 0;
450 }
451
452 static int v4l2_subdev_parse_setup_route(struct media_device *media,
453 struct v4l2_subdev_route *r,
454 const char *p, char **endp)
455 {
456 char *end;
457
458 /* sink pad/stream */
459
460 r->sink_pad = strtoul(p, &end, 10);
461 if (*end != '/') {
462 media_dbg(media, "Expected '/'\n");
463 return -EINVAL;
464 }
465
466 p = end + 1;
467
468 r->sink_stream = strtoul(p, &end, 10);
469
470 for (; isspace(*end); ++end);
471
472 if (end[0] != '-' || end[1] != '>') {
473 media_dbg(media, "Expected '->'\n");
474 return -EINVAL;
475 }
476 p = end + 2;
477
478 /* source pad/stream */
479
480 r->source_pad = strtoul(p, &end, 10);
481 if (*end != '/') {
482 media_dbg(media, "Expected '/'\n");
483 return -EINVAL;
484 }
485
486 p = end + 1;
487
488 r->source_stream = strtoul(p, &end, 10);
489
490 /* flags */
491
492 for (; isspace(*end); ++end);
493
494 if (*end != '[') {
495 media_dbg(media, "Expected '['\n");
496 return -EINVAL;
497 }
498
499 for (end++; isspace(*end); ++end);
500
501 p = end;
502
503 r->flags = strtoul(p, &end, 0);
504 if (r->flags & ~(V4L2_SUBDEV_ROUTE_FL_ACTIVE)) {
505 media_dbg(media, "Bad route flags %#x\n", r->flags);
506 return -EINVAL;
507 }
508
509 for (; isspace(*end); ++end);
510
511 if (*end != ']') {
512 media_dbg(media, "Expected ']'\n");
513 return -EINVAL;
514 }
515 end++;
516
517 for (; isspace(*end); ++end);
518
519 *endp = end;
520
521 return 0;
522 }
523
524 int v4l2_subdev_parse_setup_routes(struct media_device *media,
525 enum v4l2_subdev_format_whence which,
526 const char *p)
527 {
528 struct media_entity *entity;
529 struct v4l2_subdev_route *routes;
530 unsigned int num_routes;
531 unsigned int i;
532 char *end;
533 int ret;
534
535 entity = media_parse_entity(media, p, &end);
536 if (!entity)
537 return -EINVAL;
538
539 p = end;
540
541 if (*p != '[') {
542 media_dbg(media, "Expected '['\n");
543 return -EINVAL;
544 }
545
546 p++;
547
548 routes = calloc(NUM_ROUTES_MAX, sizeof(routes[0]));
549 if (!routes)
550 return -ENOMEM;
551
552 num_routes = 0;
553
554 while (*p != 0) {
555 struct v4l2_subdev_route *r = &routes[num_routes];
556
557 ret = v4l2_subdev_parse_setup_route(media, r, p, &end);
558 if (ret)
559 goto out;
560
561 p = end;
562
563 num_routes++;
564
565 if (*p == ',') {
566 p++;
567 continue;
568 }
569
570 break;
571 }
572
573 if (*p != ']') {
574 media_dbg(media, "Expected ']'\n");
575 ret = -EINVAL;
576 goto out;
577 }
578
579 for (i = 0; i < num_routes; ++i) {
580 struct v4l2_subdev_route *r = &routes[i];
581
582 media_dbg(entity->media,
583 "Setting up route %s : %u/%u -> %u/%u [0x%08x]\n",
584 entity->info.name,
585 r->sink_pad, r->sink_stream,
586 r->source_pad, r->source_stream,
587 r->flags);
588 }
589
590 ret = v4l2_subdev_set_routing(entity, routes, num_routes, which);
591 if (ret) {
592 media_dbg(entity->media, "VIDIOC_SUBDEV_S_ROUTING failed: %d\n",
593 ret);
594 goto out;
595 }
596
597 out:
598 free(routes);
599
600 return ret;
601 }
602
603 static int v4l2_subdev_parse_format(struct media_device *media,
604 struct v4l2_mbus_framefmt *format,
605 const char *p, char **endp)
606 {
607 enum v4l2_mbus_pixelcode code;
608 unsigned int width, height;
609 char *fmt;
610 char *end;
611
612 /*
613 * Compatibility with the old syntax: consider space as valid
614 * separator between the media bus pixel code and the size.
615 */
616 for (; isspace(*p); ++p);
617 for (end = (char *)p;
618 *end != '/' && *end != ' ' && *end != '\0'; ++end);
619
620 fmt = strndup(p, end - p);
621 if (!fmt)
622 return -ENOMEM;
623
624 code = v4l2_subdev_string_to_pixelcode(fmt);
625 free(fmt);
626 if (code == (enum v4l2_mbus_pixelcode)-1) {
627 media_dbg(media, "Invalid pixel code '%.*s'\n", end - p, p);
628 return -EINVAL;
629 }
630
631 p = end + 1;
632 width = strtoul(p, &end, 10);
633 if (*end != 'x') {
634 media_dbg(media, "Expected 'x'\n");
635 return -EINVAL;
636 }
637
638 p = end + 1;
639 height = strtoul(p, &end, 10);
640 *endp = end;
641
642 memset(format, 0, sizeof(*format));
643 format->width = width;
644 format->height = height;
645 format->code = code;
646
647 return 0;
648 }
649
650 static int v4l2_subdev_parse_rectangle(struct media_device *media,
651 struct v4l2_rect *r, const char *p,
652 char **endp)
653 {
654 char *end;
655
656 if (*p++ != '(') {
657 media_dbg(media, "Expected '('\n");
658 *endp = (char *)p - 1;
659 return -EINVAL;
660 }
661
662 r->left = strtoul(p, &end, 10);
663 if (*end != ',') {
664 media_dbg(media, "Expected ','\n");
665 *endp = end;
666 return -EINVAL;
667 }
668
669 p = end + 1;
670 r->top = strtoul(p, &end, 10);
671 if (*end++ != ')') {
672 media_dbg(media, "Expected ')'\n");
673 *endp = end - 1;
674 return -EINVAL;
675 }
676 if (*end != '/') {
677 media_dbg(media, "Expected '/'\n");
678 *endp = end;
679 return -EINVAL;
680 }
681
682 p = end + 1;
683 r->width = strtoul(p, &end, 10);
684 if (*end != 'x') {
685 media_dbg(media, "Expected 'x'\n");
686 *endp = end;
687 return -EINVAL;
688 }
689
690 p = end + 1;
691 r->height = strtoul(p, &end, 10);
692 *endp = end;
693
694 return 0;
695 }
696
697 static int v4l2_subdev_parse_frame_interval(struct media_device *media,
698 struct v4l2_fract *interval,
699 const char *p, char **endp)
700 {
701 char *end;
702
703 for (; isspace(*p); ++p);
704
705 interval->numerator = strtoul(p, &end, 10);
706
707 for (p = end; isspace(*p); ++p);
708 if (*p++ != '/') {
709 media_dbg(media, "Expected '/'\n");
710 *endp = (char *)p - 1;
711 return -EINVAL;
712 }
713
714 for (; isspace(*p); ++p);
715 interval->denominator = strtoul(p, &end, 10);
716
717 *endp = end;
718 return 0;
719 }
720
721 /*
722 * The debate over whether this function should be named icanhasstr() instead
723 * has been strong and heated. If you feel like this would be an important
724 * change, patches are welcome (or not).
725 */
726 static bool strhazit(const char *str, const char **p)
727 {
728 int len = strlen(str);
729
730 if (strncmp(str, *p, len))
731 return false;
732
733 for (*p += len; isspace(**p); ++*p);
734 return true;
735 }
736
737 static struct media_pad *v4l2_subdev_parse_pad_format(
738 struct media_device *media, unsigned int *stream,
739 struct v4l2_mbus_framefmt *format,
740 struct v4l2_rect *crop, struct v4l2_rect *compose,
741 struct v4l2_fract *interval, const char *p, char **endp)
742 {
743 struct media_pad *pad;
744 bool first;
745 char *end;
746 int ret;
747
748 for (; isspace(*p); ++p);
749
750 pad = media_parse_pad_stream(media, p, stream, &end);
751 if (pad == NULL) {
752 *endp = end;
753 return NULL;
754 }
755
756 for (p = end; isspace(*p); ++p);
757 if (*p++ != '[') {
758 media_dbg(media, "Expected '['\n");
759 *endp = (char *)p - 1;
760 return NULL;
761 }
762
763 for (first = true; ; first = false) {
764 for (; isspace(*p); p++);
765
766 /*
767 * Backward compatibility: if the first property starts with an
768 * uppercase later, process it as a format description.
769 */
770 if (strhazit("fmt:", &p) || (first && isupper(*p))) {
771 ret = v4l2_subdev_parse_format(media, format, p, &end);
772 if (ret < 0) {
773 *endp = end;
774 return NULL;
775 }
776
777 p = end;
778 continue;
779 }
780
781 if (strhazit("field:", &p)) {
782 enum v4l2_field field;
783 char *strfield;
784
785 for (end = (char *)p; isalpha(*end) || *end == '-';
786 ++end);
787
788 strfield = strndup(p, end - p);
789 if (!strfield) {
790 *endp = (char *)p;
791 return NULL;
792 }
793
794 field = v4l2_subdev_string_to_field(strfield);
795 free(strfield);
796 if (field == (enum v4l2_field)-1) {
797 media_dbg(media, "Invalid field value '%*s'\n",
798 end - p, p);
799 *endp = (char *)p;
800 return NULL;
801 }
802
803 format->field = field;
804
805 p = end;
806 continue;
807 }
808
809 if (strhazit("colorspace:", &p)) {
810 enum v4l2_colorspace colorspace;
811 char *strfield;
812
813 for (end = (char *)p; isalnum(*end) || *end == '-';
814 ++end);
815
816 strfield = strndup(p, end - p);
817 if (!strfield) {
818 *endp = (char *)p;
819 return NULL;
820 }
821
822 colorspace = v4l2_subdev_string_to_colorspace(strfield);
823 free(strfield);
824 if (colorspace == (enum v4l2_colorspace)-1) {
825 media_dbg(media, "Invalid colorspace value '%*s'\n",
826 end - p, p);
827 *endp = (char *)p;
828 return NULL;
829 }
830
831 format->colorspace = colorspace;
832 if (pad->flags & MEDIA_PAD_FL_SOURCE)
833 format->flags |= V4L2_MBUS_FRAMEFMT_SET_CSC;
834
835 p = end;
836 continue;
837 }
838
839 if (strhazit("xfer:", &p)) {
840 enum v4l2_xfer_func xfer_func;
841 char *strfield;
842
843 for (end = (char *)p; isalnum(*end) || *end == '-';
844 ++end);
845
846 strfield = strndup(p, end - p);
847 if (!strfield) {
848 *endp = (char *)p;
849 return NULL;
850 }
851
852 xfer_func = v4l2_subdev_string_to_xfer_func(strfield);
853 free(strfield);
854 if (xfer_func == (enum v4l2_xfer_func)-1) {
855 media_dbg(media, "Invalid transfer function value '%*s'\n",
856 end - p, p);
857 *endp = (char *)p;
858 return NULL;
859 }
860
861 format->xfer_func = xfer_func;
862 if (pad->flags & MEDIA_PAD_FL_SOURCE)
863 format->flags |= V4L2_MBUS_FRAMEFMT_SET_CSC;
864
865 p = end;
866 continue;
867 }
868
869 if (strhazit("ycbcr:", &p)) {
870 enum v4l2_ycbcr_encoding ycbcr_enc;
871 char *strfield;
872
873 for (end = (char *)p; isalnum(*end) || *end == '-';
874 ++end);
875
876 strfield = strndup(p, end - p);
877 if (!strfield) {
878 *endp = (char *)p;
879 return NULL;
880 }
881
882 ycbcr_enc = v4l2_subdev_string_to_ycbcr_encoding(strfield);
883 free(strfield);
884 if (ycbcr_enc == (enum v4l2_ycbcr_encoding)-1) {
885 media_dbg(media, "Invalid YCbCr encoding value '%*s'\n",
886 end - p, p);
887 *endp = (char *)p;
888 return NULL;
889 }
890
891 format->ycbcr_enc = ycbcr_enc;
892 if (pad->flags & MEDIA_PAD_FL_SOURCE)
893 format->flags |= V4L2_MBUS_FRAMEFMT_SET_CSC;
894
895 p = end;
896 continue;
897 }
898
899 if (strhazit("quantization:", &p)) {
900 enum v4l2_quantization quantization;
901 char *strfield;
902
903 for (end = (char *)p; isalnum(*end) || *end == '-';
904 ++end);
905
906 strfield = strndup(p, end - p);
907 if (!strfield) {
908 *endp = (char *)p;
909 return NULL;
910 }
911
912 quantization = v4l2_subdev_string_to_quantization(strfield);
913 free(strfield);
914 if (quantization == (enum v4l2_quantization)-1) {
915 media_dbg(media, "Invalid quantization value '%*s'\n",
916 end - p, p);
917 *endp = (char *)p;
918 return NULL;
919 }
920
921 format->quantization = quantization;
922 if (pad->flags & MEDIA_PAD_FL_SOURCE)
923 format->flags |= V4L2_MBUS_FRAMEFMT_SET_CSC;
924
925 p = end;
926 continue;
927 }
928
929 /*
930 * Backward compatibility: crop rectangles can be specified
931 * implicitly without the 'crop:' property name.
932 */
933 if (strhazit("crop:", &p) || *p == '(') {
934 ret = v4l2_subdev_parse_rectangle(media, crop, p, &end);
935 if (ret < 0) {
936 *endp = end;
937 return NULL;
938 }
939
940 p = end;
941 continue;
942 }
943
944 if (strhazit("compose:", &p)) {
945 ret = v4l2_subdev_parse_rectangle(media, compose, p, &end);
946 if (ret < 0) {
947 *endp = end;
948 return NULL;
949 }
950
951 for (p = end; isspace(*p); p++);
952 continue;
953 }
954
955 if (*p == '@') {
956 ret = v4l2_subdev_parse_frame_interval(media, interval, ++p, &end);
957 if (ret < 0) {
958 *endp = end;
959 return NULL;
960 }
961
962 p = end;
963 continue;
964 }
965
966 break;
967 }
968
969 if (*p != ']') {
970 media_dbg(media, "Expected ']'\n");
971 *endp = (char *)p;
972 return NULL;
973 }
974
975 *endp = (char *)p + 1;
976 return pad;
977 }
978
979 static int set_format(struct media_pad *pad,
980 unsigned int stream,
981 struct v4l2_mbus_framefmt *format,
982 enum v4l2_subdev_format_whence which)
983 {
984 int ret;
985
986 if (format->width == 0 || format->height == 0)
987 return 0;
988
989 media_dbg(pad->entity->media,
990 "Setting up format %s %ux%u on pad %s/%u/%u\n",
991 v4l2_subdev_pixelcode_to_string(format->code),
992 format->width, format->height,
993 pad->entity->info.name, pad->index, stream);
994
995 ret = v4l2_subdev_set_format(pad->entity, format, pad->index, stream,
996 which);
997 if (ret < 0) {
998 media_dbg(pad->entity->media,
999 "Unable to set format: %s (%d)\n",
1000 strerror(-ret), ret);
1001 return ret;
1002 }
1003
1004 media_dbg(pad->entity->media,
1005 "Format set: %s %ux%u\n",
1006 v4l2_subdev_pixelcode_to_string(format->code),
1007 format->width, format->height);
1008
1009 return 0;
1010 }
1011
1012 static int set_selection(struct media_pad *pad, unsigned int stream,
1013 unsigned int target, struct v4l2_rect *rect,
1014 enum v4l2_subdev_format_whence which)
1015 {
1016 int ret;
1017
1018 if (rect->left == -1 || rect->top == -1)
1019 return 0;
1020
1021 media_dbg(pad->entity->media,
1022 "Setting up selection target %u rectangle (%d,%d)/%ux%u on pad %s/%u/%u\n",
1023 target, rect->left, rect->top, rect->width, rect->height,
1024 pad->entity->info.name, pad->index, stream);
1025
1026 ret = v4l2_subdev_set_selection(pad->entity, rect, pad->index, stream,
1027 target, which);
1028 if (ret < 0) {
1029 media_dbg(pad->entity->media,
1030 "Unable to set selection rectangle: %s (%d)\n",
1031 strerror(-ret), ret);
1032 return ret;
1033 }
1034
1035 media_dbg(pad->entity->media,
1036 "Selection rectangle set: (%d,%d)/%ux%u\n",
1037 rect->left, rect->top, rect->width, rect->height);
1038
1039 return 0;
1040 }
1041
1042 static int set_frame_interval(struct media_pad *pad, unsigned int stream,
1043 struct v4l2_fract *interval,
1044 enum v4l2_subdev_format_whence which)
1045 {
1046 int ret;
1047
1048 if (interval->numerator == 0)
1049 return 0;
1050
1051 media_dbg(pad->entity->media,
1052 "Setting up frame interval %u/%u on pad %s/%u/%u\n",
1053 interval->numerator, interval->denominator,
1054 pad->entity->info.name, pad->index, stream);
1055
1056 ret = v4l2_subdev_set_frame_interval(pad->entity, interval, pad->index,
1057 stream, which);
1058 if (ret < 0) {
1059 media_dbg(pad->entity->media,
1060 "Unable to set frame interval: %s (%d)",
1061 strerror(-ret), ret);
1062 return ret;
1063 }
1064
1065 media_dbg(pad->entity->media, "Frame interval set: %u/%u\n",
1066 interval->numerator, interval->denominator);
1067
1068 return 0;
1069 }
1070
1071
1072 static int v4l2_subdev_parse_setup_format(struct media_device *media,
1073 enum v4l2_subdev_format_whence which,
1074 const char *p, char **endp)
1075 {
1076 struct v4l2_mbus_framefmt format = { 0, 0, 0 };
1077 struct media_pad *pad;
1078 struct v4l2_rect crop = { -1, -1, -1, -1 };
1079 struct v4l2_rect compose = crop;
1080 struct v4l2_fract interval = { 0, 0 };
1081 unsigned int stream;
1082 unsigned int i;
1083 char *end;
1084 int ret;
1085
1086 pad = v4l2_subdev_parse_pad_format(media, &stream,
1087 &format, &crop, &compose,
1088 &interval, p, &end);
1089 if (pad == NULL) {
1090 media_print_streampos(media, p, end);
1091 media_dbg(media, "Unable to parse format\n");
1092 return -EINVAL;
1093 }
1094
1095 if (pad->flags & MEDIA_PAD_FL_SINK) {
1096 ret = set_format(pad, stream, &format, which);
1097 if (ret < 0)
1098 return ret;
1099 }
1100
1101 ret = set_selection(pad, stream, V4L2_SEL_TGT_CROP, &crop, which);
1102 if (ret < 0)
1103 return ret;
1104
1105 ret = set_selection(pad, stream, V4L2_SEL_TGT_COMPOSE, &compose, which);
1106 if (ret < 0)
1107 return ret;
1108
1109 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
1110 ret = set_format(pad, stream, &format, which);
1111 if (ret < 0)
1112 return ret;
1113 }
1114
1115 ret = set_frame_interval(pad, stream, &interval, which);
1116 if (ret < 0)
1117 return ret;
1118
1119 /* If the pad is an output pad, automatically set the same format and
1120 * frame interval on the remote subdev input pads, if any.
1121 */
1122 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
1123 for (i = 0; i < pad->entity->num_links; ++i) {
1124 struct media_link *link = &pad->entity->links[i];
1125 struct v4l2_mbus_framefmt remote_format;
1126
1127 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
1128 continue;
1129
1130 if (link->source == pad &&
1131 link->sink->entity->info.type == MEDIA_ENT_T_V4L2_SUBDEV) {
1132 remote_format = format;
1133 set_format(link->sink, stream, &remote_format,
1134 which);
1135
1136 ret = set_frame_interval(link->sink, stream,
1137 &interval, which);
1138 if (ret < 0 && ret != -EINVAL && ret != -ENOTTY)
1139 return ret;
1140 }
1141 }
1142 }
1143
1144 *endp = end;
1145 return 0;
1146 }
1147
1148 int v4l2_subdev_parse_setup_formats(struct media_device *media,
1149 enum v4l2_subdev_format_whence which,
1150 const char *p)
1151 {
1152 char *end;
1153 int ret;
1154
1155 do {
1156 ret = v4l2_subdev_parse_setup_format(media, which, p, &end);
1157 if (ret < 0)
1158 return ret;
1159
1160 for (; isspace(*end); end++);
1161 p = end + 1;
1162 } while (*end == ',');
1163
1164 return *end ? -EINVAL : 0;
1165 }
1166
1167 static const struct {
1168 const char *name;
1169 enum v4l2_mbus_pixelcode code;
1170 } mbus_formats[] = {
1171 #include "media-bus-format-names.h"
1172 { "FIXED", MEDIA_BUS_FMT_FIXED },
1173 { "Y8", MEDIA_BUS_FMT_Y8_1X8 },
1174 { "Y10", MEDIA_BUS_FMT_Y10_1X10 },
1175 { "Y12", MEDIA_BUS_FMT_Y12_1X12 },
1176 { "Y16", MEDIA_BUS_FMT_Y16_1X16 },
1177 { "YUYV", MEDIA_BUS_FMT_YUYV8_1X16 },
1178 { "YUYV1_5X8", MEDIA_BUS_FMT_YUYV8_1_5X8 },
1179 { "YUYV2X8", MEDIA_BUS_FMT_YUYV8_2X8 },
1180 { "UYVY", MEDIA_BUS_FMT_UYVY8_1X16 },
1181 { "UYVY1_5X8", MEDIA_BUS_FMT_UYVY8_1_5X8 },
1182 { "UYVY2X8", MEDIA_BUS_FMT_UYVY8_2X8 },
1183 { "VUY24", MEDIA_BUS_FMT_VUY8_1X24 },
1184 { "SBGGR8", MEDIA_BUS_FMT_SBGGR8_1X8 },
1185 { "SGBRG8", MEDIA_BUS_FMT_SGBRG8_1X8 },
1186 { "SGRBG8", MEDIA_BUS_FMT_SGRBG8_1X8 },
1187 { "SRGGB8", MEDIA_BUS_FMT_SRGGB8_1X8 },
1188 { "SBGGR10", MEDIA_BUS_FMT_SBGGR10_1X10 },
1189 { "SGBRG10", MEDIA_BUS_FMT_SGBRG10_1X10 },
1190 { "SGRBG10", MEDIA_BUS_FMT_SGRBG10_1X10 },
1191 { "SRGGB10", MEDIA_BUS_FMT_SRGGB10_1X10 },
1192 { "SBGGR10_DPCM8", MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8 },
1193 { "SGBRG10_DPCM8", MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8 },
1194 { "SGRBG10_DPCM8", MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8 },
1195 { "SRGGB10_DPCM8", MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8 },
1196 { "SBGGR12", MEDIA_BUS_FMT_SBGGR12_1X12 },
1197 { "SGBRG12", MEDIA_BUS_FMT_SGBRG12_1X12 },
1198 { "SGRBG12", MEDIA_BUS_FMT_SGRBG12_1X12 },
1199 { "SRGGB12", MEDIA_BUS_FMT_SRGGB12_1X12 },
1200 { "AYUV32", MEDIA_BUS_FMT_AYUV8_1X32 },
1201 { "RBG24", MEDIA_BUS_FMT_RBG888_1X24 },
1202 { "RGB32", MEDIA_BUS_FMT_RGB888_1X32_PADHI },
1203 { "ARGB32", MEDIA_BUS_FMT_ARGB8888_1X32 },
1204 };
1205
1206 const char *v4l2_subdev_pixelcode_to_string(enum v4l2_mbus_pixelcode code)
1207 {
1208 unsigned int i;
1209
1210 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
1211 if (mbus_formats[i].code == code)
1212 return mbus_formats[i].name;
1213 }
1214
1215 return "unknown";
1216 }
1217
1218 enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string)
1219 {
1220 unsigned int i;
1221
1222 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
1223 if (strcmp(mbus_formats[i].name, string) == 0)
1224 return mbus_formats[i].code;
1225 }
1226
1227 return (enum v4l2_mbus_pixelcode)-1;
1228 }
1229
1230 static struct {
1231 const char *name;
1232 enum v4l2_field field;
1233 } fields[] = {
1234 { "any", V4L2_FIELD_ANY },
1235 { "none", V4L2_FIELD_NONE },
1236 { "top", V4L2_FIELD_TOP },
1237 { "bottom", V4L2_FIELD_BOTTOM },
1238 { "interlaced", V4L2_FIELD_INTERLACED },
1239 { "seq-tb", V4L2_FIELD_SEQ_TB },
1240 { "seq-bt", V4L2_FIELD_SEQ_BT },
1241 { "alternate", V4L2_FIELD_ALTERNATE },
1242 { "interlaced-tb", V4L2_FIELD_INTERLACED_TB },
1243 { "interlaced-bt", V4L2_FIELD_INTERLACED_BT },
1244 };
1245
1246 const char *v4l2_subdev_field_to_string(enum v4l2_field field)
1247 {
1248 unsigned int i;
1249
1250 for (i = 0; i < ARRAY_SIZE(fields); ++i) {
1251 if (fields[i].field == field)
1252 return fields[i].name;
1253 }
1254
1255 return "unknown";
1256 }
1257
1258 enum v4l2_field v4l2_subdev_string_to_field(const char *string)
1259 {
1260 unsigned int i;
1261
1262 for (i = 0; i < ARRAY_SIZE(fields); ++i) {
1263 if (strcasecmp(fields[i].name, string) == 0)
1264 return fields[i].field;
1265 }
1266
1267 return (enum v4l2_field)-1;
1268 }
1269
1270 static struct {
1271 const char *name;
1272 enum v4l2_colorspace colorspace;
1273 } colorspaces[] = {
1274 { "default", V4L2_COLORSPACE_DEFAULT },
1275 { "smpte170m", V4L2_COLORSPACE_SMPTE170M },
1276 { "smpte240m", V4L2_COLORSPACE_SMPTE240M },
1277 { "rec709", V4L2_COLORSPACE_REC709 },
1278 { "470m", V4L2_COLORSPACE_470_SYSTEM_M },
1279 { "470bg", V4L2_COLORSPACE_470_SYSTEM_BG },
1280 { "jpeg", V4L2_COLORSPACE_JPEG },
1281 { "srgb", V4L2_COLORSPACE_SRGB },
1282 { "oprgb", V4L2_COLORSPACE_OPRGB },
1283 { "bt2020", V4L2_COLORSPACE_BT2020 },
1284 { "raw", V4L2_COLORSPACE_RAW },
1285 { "dcip3", V4L2_COLORSPACE_DCI_P3 },
1286 };
1287
1288 const char *v4l2_subdev_colorspace_to_string(enum v4l2_colorspace colorspace)
1289 {
1290 unsigned int i;
1291
1292 for (i = 0; i < ARRAY_SIZE(colorspaces); ++i) {
1293 if (colorspaces[i].colorspace == colorspace)
1294 return colorspaces[i].name;
1295 }
1296
1297 return "unknown";
1298 }
1299
1300 enum v4l2_colorspace v4l2_subdev_string_to_colorspace(const char *string)
1301 {
1302 unsigned int i;
1303
1304 for (i = 0; i < ARRAY_SIZE(colorspaces); ++i) {
1305 if (strcasecmp(colorspaces[i].name, string) == 0)
1306 return colorspaces[i].colorspace;
1307 }
1308
1309 return (enum v4l2_colorspace)-1;
1310 }
1311
1312 static struct {
1313 const char *name;
1314 enum v4l2_xfer_func xfer_func;
1315 } xfer_funcs[] = {
1316 { "default", V4L2_XFER_FUNC_DEFAULT },
1317 { "709", V4L2_XFER_FUNC_709 },
1318 { "srgb", V4L2_XFER_FUNC_SRGB },
1319 { "oprgb", V4L2_XFER_FUNC_OPRGB },
1320 { "smpte240m", V4L2_XFER_FUNC_SMPTE240M },
1321 { "smpte2084", V4L2_XFER_FUNC_SMPTE2084 },
1322 { "dcip3", V4L2_XFER_FUNC_DCI_P3 },
1323 { "none", V4L2_XFER_FUNC_NONE },
1324 };
1325
1326 const char *v4l2_subdev_xfer_func_to_string(enum v4l2_xfer_func xfer_func)
1327 {
1328 unsigned int i;
1329
1330 for (i = 0; i < ARRAY_SIZE(xfer_funcs); ++i) {
1331 if (xfer_funcs[i].xfer_func == xfer_func)
1332 return xfer_funcs[i].name;
1333 }
1334
1335 return "unknown";
1336 }
1337
1338 enum v4l2_xfer_func v4l2_subdev_string_to_xfer_func(const char *string)
1339 {
1340 unsigned int i;
1341
1342 for (i = 0; i < ARRAY_SIZE(xfer_funcs); ++i) {
1343 if (strcasecmp(xfer_funcs[i].name, string) == 0)
1344 return xfer_funcs[i].xfer_func;
1345 }
1346
1347 return (enum v4l2_xfer_func)-1;
1348 }
1349
1350 static struct {
1351 const char *name;
1352 enum v4l2_ycbcr_encoding ycbcr_enc;
1353 } ycbcr_encs[] = {
1354 { "default", V4L2_YCBCR_ENC_DEFAULT },
1355 { "601", V4L2_YCBCR_ENC_601 },
1356 { "709", V4L2_YCBCR_ENC_709 },
1357 { "xv601", V4L2_YCBCR_ENC_XV601 },
1358 { "xv709", V4L2_YCBCR_ENC_XV709 },
1359 { "bt2020", V4L2_YCBCR_ENC_BT2020 },
1360 { "bt2020c", V4L2_YCBCR_ENC_BT2020_CONST_LUM },
1361 { "smpte240m", V4L2_YCBCR_ENC_SMPTE240M },
1362 };
1363
1364 const char *v4l2_subdev_ycbcr_encoding_to_string(enum v4l2_ycbcr_encoding ycbcr_enc)
1365 {
1366 unsigned int i;
1367
1368 for (i = 0; i < ARRAY_SIZE(ycbcr_encs); ++i) {
1369 if (ycbcr_encs[i].ycbcr_enc == ycbcr_enc)
1370 return ycbcr_encs[i].name;
1371 }
1372
1373 return "unknown";
1374 }
1375
1376 enum v4l2_ycbcr_encoding v4l2_subdev_string_to_ycbcr_encoding(const char *string)
1377 {
1378 unsigned int i;
1379
1380 for (i = 0; i < ARRAY_SIZE(ycbcr_encs); ++i) {
1381 if (strcasecmp(ycbcr_encs[i].name, string) == 0)
1382 return ycbcr_encs[i].ycbcr_enc;
1383 }
1384
1385 return (enum v4l2_ycbcr_encoding)-1;
1386 }
1387
1388 static struct {
1389 const char *name;
1390 enum v4l2_quantization quantization;
1391 } quantizations[] = {
1392 { "default", V4L2_QUANTIZATION_DEFAULT },
1393 { "full-range", V4L2_QUANTIZATION_FULL_RANGE },
1394 { "lim-range", V4L2_QUANTIZATION_LIM_RANGE },
1395 };
1396
1397 const char *v4l2_subdev_quantization_to_string(enum v4l2_quantization quantization)
1398 {
1399 unsigned int i;
1400
1401 for (i = 0; i < ARRAY_SIZE(quantizations); ++i) {
1402 if (quantizations[i].quantization == quantization)
1403 return quantizations[i].name;
1404 }
1405
1406 return "unknown";
1407 }
1408
1409 enum v4l2_quantization v4l2_subdev_string_to_quantization(const char *string)
1410 {
1411 unsigned int i;
1412
1413 for (i = 0; i < ARRAY_SIZE(quantizations); ++i) {
1414 if (strcasecmp(quantizations[i].name, string) == 0)
1415 return quantizations[i].quantization;
1416 }
1417
1418 return (enum v4l2_quantization)-1;
1419 }
1420
1421 static const enum v4l2_mbus_pixelcode mbus_codes[] = {
1422 #include "media-bus-format-codes.h"
1423 };
1424
1425 const enum v4l2_mbus_pixelcode *v4l2_subdev_pixelcode_list(unsigned int *length)
1426 {
1427 *length = ARRAY_SIZE(mbus_codes);
1428
1429 return mbus_codes;
1430 }