1 /*
2 * V4L2 subdev 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 <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 <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #include <linux/v4l2-subdev.h>
36
37 #include "mediactl.h"
38 #include "mediactl-priv.h"
39 #include "tools.h"
40 #include "v4l2subdev.h"
41
v4l2_subdev_open(struct media_entity * entity)42 int v4l2_subdev_open(struct media_entity *entity)
43 {
44 if (entity->fd != -1)
45 return 0;
46
47 entity->fd = open(entity->devname, O_RDWR);
48 if (entity->fd == -1) {
49 int ret = -errno;
50 media_dbg(entity->media,
51 "%s: Failed to open subdev device node %s\n", __func__,
52 entity->devname);
53 return ret;
54 }
55
56 return 0;
57 }
58
v4l2_subdev_close(struct media_entity * entity)59 void v4l2_subdev_close(struct media_entity *entity)
60 {
61 close(entity->fd);
62 entity->fd = -1;
63 }
64
v4l2_subdev_get_format(struct media_entity * entity,struct v4l2_mbus_framefmt * format,unsigned int pad,enum v4l2_subdev_format_whence which)65 int v4l2_subdev_get_format(struct media_entity *entity,
66 struct v4l2_mbus_framefmt *format, unsigned int pad,
67 enum v4l2_subdev_format_whence which)
68 {
69 struct v4l2_subdev_format fmt;
70 int ret;
71
72 ret = v4l2_subdev_open(entity);
73 if (ret < 0)
74 return ret;
75
76 memset(&fmt, 0, sizeof(fmt));
77 fmt.pad = pad;
78 fmt.which = which;
79
80 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FMT, &fmt);
81 if (ret < 0)
82 return -errno;
83
84 *format = fmt.format;
85 return 0;
86 }
87
v4l2_subdev_set_format(struct media_entity * entity,struct v4l2_mbus_framefmt * format,unsigned int pad,enum v4l2_subdev_format_whence which)88 int v4l2_subdev_set_format(struct media_entity *entity,
89 struct v4l2_mbus_framefmt *format, unsigned int pad,
90 enum v4l2_subdev_format_whence which)
91 {
92 struct v4l2_subdev_format fmt;
93 int ret;
94
95 ret = v4l2_subdev_open(entity);
96 if (ret < 0)
97 return ret;
98
99 memset(&fmt, 0, sizeof(fmt));
100 fmt.pad = pad;
101 fmt.which = which;
102 fmt.format = *format;
103
104 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FMT, &fmt);
105 if (ret < 0)
106 return -errno;
107
108 *format = fmt.format;
109 return 0;
110 }
111
v4l2_subdev_get_selection(struct media_entity * entity,struct v4l2_rect * rect,unsigned int pad,unsigned int target,enum v4l2_subdev_format_whence which)112 int v4l2_subdev_get_selection(struct media_entity *entity,
113 struct v4l2_rect *rect, unsigned int pad, unsigned int target,
114 enum v4l2_subdev_format_whence which)
115 {
116 union {
117 struct v4l2_subdev_selection sel;
118 struct v4l2_subdev_crop crop;
119 } u;
120 int ret;
121
122 ret = v4l2_subdev_open(entity);
123 if (ret < 0)
124 return ret;
125
126 memset(&u.sel, 0, sizeof(u.sel));
127 u.sel.pad = pad;
128 u.sel.target = target;
129 u.sel.which = which;
130
131 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_SELECTION, &u.sel);
132 if (ret >= 0) {
133 *rect = u.sel.r;
134 return 0;
135 }
136 if (errno != ENOTTY || target != V4L2_SEL_TGT_CROP)
137 return -errno;
138
139 memset(&u.crop, 0, sizeof(u.crop));
140 u.crop.pad = pad;
141 u.crop.which = which;
142
143 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_CROP, &u.crop);
144 if (ret < 0)
145 return -errno;
146
147 *rect = u.crop.rect;
148 return 0;
149 }
150
v4l2_subdev_set_selection(struct media_entity * entity,struct v4l2_rect * rect,unsigned int pad,unsigned int target,enum v4l2_subdev_format_whence which)151 int v4l2_subdev_set_selection(struct media_entity *entity,
152 struct v4l2_rect *rect, unsigned int pad, unsigned int target,
153 enum v4l2_subdev_format_whence which)
154 {
155 union {
156 struct v4l2_subdev_selection sel;
157 struct v4l2_subdev_crop crop;
158 } u;
159 int ret;
160
161 ret = v4l2_subdev_open(entity);
162 if (ret < 0)
163 return ret;
164
165 memset(&u.sel, 0, sizeof(u.sel));
166 u.sel.pad = pad;
167 u.sel.target = target;
168 u.sel.which = which;
169 u.sel.r = *rect;
170
171 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_SELECTION, &u.sel);
172 if (ret >= 0) {
173 *rect = u.sel.r;
174 return 0;
175 }
176 if (errno != ENOTTY || target != V4L2_SEL_TGT_CROP)
177 return -errno;
178
179 memset(&u.crop, 0, sizeof(u.crop));
180 u.crop.pad = pad;
181 u.crop.which = which;
182 u.crop.rect = *rect;
183
184 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_CROP, &u.crop);
185 if (ret < 0)
186 return -errno;
187
188 *rect = u.crop.rect;
189 return 0;
190 }
191
v4l2_subdev_get_frame_interval(struct media_entity * entity,struct v4l2_fract * interval)192 int v4l2_subdev_get_frame_interval(struct media_entity *entity,
193 struct v4l2_fract *interval)
194 {
195 struct v4l2_subdev_frame_interval ival;
196 int ret;
197
198 ret = v4l2_subdev_open(entity);
199 if (ret < 0)
200 return ret;
201
202 memset(&ival, 0, sizeof(ival));
203
204 ret = ioctl(entity->fd, VIDIOC_SUBDEV_G_FRAME_INTERVAL, &ival);
205 if (ret < 0)
206 return -errno;
207
208 *interval = ival.interval;
209 return 0;
210 }
211
v4l2_subdev_set_frame_interval(struct media_entity * entity,struct v4l2_fract * interval)212 int v4l2_subdev_set_frame_interval(struct media_entity *entity,
213 struct v4l2_fract *interval)
214 {
215 struct v4l2_subdev_frame_interval ival;
216 int ret;
217
218 ret = v4l2_subdev_open(entity);
219 if (ret < 0)
220 return ret;
221
222 memset(&ival, 0, sizeof(ival));
223 ival.interval = *interval;
224
225 ret = ioctl(entity->fd, VIDIOC_SUBDEV_S_FRAME_INTERVAL, &ival);
226 if (ret < 0)
227 return -errno;
228
229 *interval = ival.interval;
230 return 0;
231 }
232
v4l2_subdev_parse_format(struct media_device * media,struct v4l2_mbus_framefmt * format,const char * p,char ** endp)233 static int v4l2_subdev_parse_format(struct media_device *media,
234 struct v4l2_mbus_framefmt *format,
235 const char *p, char **endp)
236 {
237 enum v4l2_mbus_pixelcode code;
238 unsigned int width, height;
239 char *end;
240
241 /*
242 * Compatibility with the old syntax: consider space as valid
243 * separator between the media bus pixel code and the size.
244 */
245 for (; isspace(*p); ++p);
246 for (end = (char *)p;
247 *end != '/' && *end != ' ' && *end != '\0'; ++end);
248
249 code = v4l2_subdev_string_to_pixelcode(p, end - p);
250 if (code == (enum v4l2_mbus_pixelcode)-1) {
251 media_dbg(media, "Invalid pixel code '%.*s'\n", end - p, p);
252 return -EINVAL;
253 }
254
255 p = end + 1;
256 width = strtoul(p, &end, 10);
257 if (*end != 'x') {
258 media_dbg(media, "Expected 'x'\n");
259 return -EINVAL;
260 }
261
262 p = end + 1;
263 height = strtoul(p, &end, 10);
264 *endp = end;
265
266 memset(format, 0, sizeof(*format));
267 format->width = width;
268 format->height = height;
269 format->code = code;
270
271 return 0;
272 }
273
v4l2_subdev_parse_rectangle(struct media_device * media,struct v4l2_rect * r,const char * p,char ** endp)274 static int v4l2_subdev_parse_rectangle(struct media_device *media,
275 struct v4l2_rect *r, const char *p,
276 char **endp)
277 {
278 char *end;
279
280 if (*p++ != '(') {
281 media_dbg(media, "Expected '('\n");
282 *endp = (char *)p - 1;
283 return -EINVAL;
284 }
285
286 r->left = strtoul(p, &end, 10);
287 if (*end != ',') {
288 media_dbg(media, "Expected ','\n");
289 *endp = end;
290 return -EINVAL;
291 }
292
293 p = end + 1;
294 r->top = strtoul(p, &end, 10);
295 if (*end++ != ')') {
296 media_dbg(media, "Expected ')'\n");
297 *endp = end - 1;
298 return -EINVAL;
299 }
300 if (*end != '/') {
301 media_dbg(media, "Expected '/'\n");
302 *endp = end;
303 return -EINVAL;
304 }
305
306 p = end + 1;
307 r->width = strtoul(p, &end, 10);
308 if (*end != 'x') {
309 media_dbg(media, "Expected 'x'\n");
310 *endp = end;
311 return -EINVAL;
312 }
313
314 p = end + 1;
315 r->height = strtoul(p, &end, 10);
316 *endp = end;
317
318 return 0;
319 }
320
v4l2_subdev_parse_frame_interval(struct media_device * media,struct v4l2_fract * interval,const char * p,char ** endp)321 static int v4l2_subdev_parse_frame_interval(struct media_device *media,
322 struct v4l2_fract *interval,
323 const char *p, char **endp)
324 {
325 char *end;
326
327 for (; isspace(*p); ++p);
328
329 interval->numerator = strtoul(p, &end, 10);
330
331 for (p = end; isspace(*p); ++p);
332 if (*p++ != '/') {
333 media_dbg(media, "Expected '/'\n");
334 *endp = (char *)p - 1;
335 return -EINVAL;
336 }
337
338 for (; isspace(*p); ++p);
339 interval->denominator = strtoul(p, &end, 10);
340
341 *endp = end;
342 return 0;
343 }
344
345 /*
346 * The debate over whether this function should be named icanhasstr() instead
347 * has been strong and heated. If you feel like this would be an important
348 * change, patches are welcome (or not).
349 */
strhazit(const char * str,const char ** p)350 static bool strhazit(const char *str, const char **p)
351 {
352 int len = strlen(str);
353
354 if (strncmp(str, *p, len))
355 return false;
356
357 for (*p += len; isspace(**p); ++*p);
358 return true;
359 }
360
v4l2_subdev_parse_pad_format(struct media_device * media,struct v4l2_mbus_framefmt * format,struct v4l2_rect * crop,struct v4l2_rect * compose,struct v4l2_fract * interval,const char * p,char ** endp)361 static struct media_pad *v4l2_subdev_parse_pad_format(
362 struct media_device *media, struct v4l2_mbus_framefmt *format,
363 struct v4l2_rect *crop, struct v4l2_rect *compose,
364 struct v4l2_fract *interval, const char *p, char **endp)
365 {
366 struct media_pad *pad;
367 bool first;
368 char *end;
369 int ret;
370
371 for (; isspace(*p); ++p);
372
373 pad = media_parse_pad(media, p, &end);
374 if (pad == NULL) {
375 *endp = end;
376 return NULL;
377 }
378
379 for (p = end; isspace(*p); ++p);
380 if (*p++ != '[') {
381 media_dbg(media, "Expected '['\n");
382 *endp = (char *)p - 1;
383 return NULL;
384 }
385
386 for (first = true; ; first = false) {
387 for (; isspace(*p); p++);
388
389 /*
390 * Backward compatibility: if the first property starts with an
391 * uppercase later, process it as a format description.
392 */
393 if (strhazit("fmt:", &p) || (first && isupper(*p))) {
394 ret = v4l2_subdev_parse_format(media, format, p, &end);
395 if (ret < 0) {
396 *endp = end;
397 return NULL;
398 }
399
400 p = end;
401 continue;
402 }
403
404 /*
405 * Backward compatibility: crop rectangles can be specified
406 * implicitly without the 'crop:' property name.
407 */
408 if (strhazit("crop:", &p) || *p == '(') {
409 ret = v4l2_subdev_parse_rectangle(media, crop, p, &end);
410 if (ret < 0) {
411 *endp = end;
412 return NULL;
413 }
414
415 p = end;
416 continue;
417 }
418
419 if (strhazit("compose:", &p)) {
420 ret = v4l2_subdev_parse_rectangle(media, compose, p, &end);
421 if (ret < 0) {
422 *endp = end;
423 return NULL;
424 }
425
426 for (p = end; isspace(*p); p++);
427 continue;
428 }
429
430 if (*p == '@') {
431 ret = v4l2_subdev_parse_frame_interval(media, interval, ++p, &end);
432 if (ret < 0) {
433 *endp = end;
434 return NULL;
435 }
436
437 p = end;
438 continue;
439 }
440
441 break;
442 }
443
444 if (*p != ']') {
445 media_dbg(media, "Expected ']'\n");
446 *endp = (char *)p;
447 return NULL;
448 }
449
450 *endp = (char *)p + 1;
451 return pad;
452 }
453
set_format(struct media_pad * pad,struct v4l2_mbus_framefmt * format)454 static int set_format(struct media_pad *pad,
455 struct v4l2_mbus_framefmt *format)
456 {
457 int ret;
458
459 if (format->width == 0 || format->height == 0)
460 return 0;
461
462 media_dbg(pad->entity->media,
463 "Setting up format %s %ux%u on pad %s/%u\n",
464 v4l2_subdev_pixelcode_to_string(format->code),
465 format->width, format->height,
466 pad->entity->info.name, pad->index);
467
468 ret = v4l2_subdev_set_format(pad->entity, format, pad->index,
469 V4L2_SUBDEV_FORMAT_ACTIVE);
470 if (ret < 0) {
471 media_dbg(pad->entity->media,
472 "Unable to set format: %s (%d)\n",
473 strerror(-ret), ret);
474 return ret;
475 }
476
477 media_dbg(pad->entity->media,
478 "Format set: %s %ux%u\n",
479 v4l2_subdev_pixelcode_to_string(format->code),
480 format->width, format->height);
481
482 return 0;
483 }
484
set_selection(struct media_pad * pad,unsigned int target,struct v4l2_rect * rect)485 static int set_selection(struct media_pad *pad, unsigned int target,
486 struct v4l2_rect *rect)
487 {
488 int ret;
489
490 if (rect->left == -1 || rect->top == -1)
491 return 0;
492
493 media_dbg(pad->entity->media,
494 "Setting up selection target %u rectangle (%u,%u)/%ux%u on pad %s/%u\n",
495 target, rect->left, rect->top, rect->width, rect->height,
496 pad->entity->info.name, pad->index);
497
498 ret = v4l2_subdev_set_selection(pad->entity, rect, pad->index,
499 target, V4L2_SUBDEV_FORMAT_ACTIVE);
500 if (ret < 0) {
501 media_dbg(pad->entity->media,
502 "Unable to set selection rectangle: %s (%d)\n",
503 strerror(-ret), ret);
504 return ret;
505 }
506
507 media_dbg(pad->entity->media,
508 "Selection rectangle set: (%u,%u)/%ux%u\n",
509 rect->left, rect->top, rect->width, rect->height);
510
511 return 0;
512 }
513
set_frame_interval(struct media_entity * entity,struct v4l2_fract * interval)514 static int set_frame_interval(struct media_entity *entity,
515 struct v4l2_fract *interval)
516 {
517 int ret;
518
519 if (interval->numerator == 0)
520 return 0;
521
522 media_dbg(entity->media,
523 "Setting up frame interval %u/%u on entity %s\n",
524 interval->numerator, interval->denominator,
525 entity->info.name);
526
527 ret = v4l2_subdev_set_frame_interval(entity, interval);
528 if (ret < 0) {
529 media_dbg(entity->media,
530 "Unable to set frame interval: %s (%d)",
531 strerror(-ret), ret);
532 return ret;
533 }
534
535 media_dbg(entity->media, "Frame interval set: %u/%u\n",
536 interval->numerator, interval->denominator);
537
538 return 0;
539 }
540
541
v4l2_subdev_parse_setup_format(struct media_device * media,const char * p,char ** endp)542 static int v4l2_subdev_parse_setup_format(struct media_device *media,
543 const char *p, char **endp)
544 {
545 struct v4l2_mbus_framefmt format = { 0, 0, 0 };
546 struct media_pad *pad;
547 struct v4l2_rect crop = { -1, -1, -1, -1 };
548 struct v4l2_rect compose = crop;
549 struct v4l2_fract interval = { 0, 0 };
550 unsigned int i;
551 char *end;
552 int ret;
553
554 pad = v4l2_subdev_parse_pad_format(media, &format, &crop, &compose,
555 &interval, p, &end);
556 if (pad == NULL) {
557 media_print_streampos(media, p, end);
558 media_dbg(media, "Unable to parse format\n");
559 return -EINVAL;
560 }
561
562 if (pad->flags & MEDIA_PAD_FL_SINK) {
563 ret = set_format(pad, &format);
564 if (ret < 0)
565 return ret;
566 }
567
568 ret = set_selection(pad, V4L2_SEL_TGT_CROP, &crop);
569 if (ret < 0)
570 return ret;
571
572 ret = set_selection(pad, V4L2_SEL_TGT_COMPOSE, &compose);
573 if (ret < 0)
574 return ret;
575
576 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
577 ret = set_format(pad, &format);
578 if (ret < 0)
579 return ret;
580 }
581
582 ret = set_frame_interval(pad->entity, &interval);
583 if (ret < 0)
584 return ret;
585
586
587 /* If the pad is an output pad, automatically set the same format on
588 * the remote subdev input pads, if any.
589 */
590 if (pad->flags & MEDIA_PAD_FL_SOURCE) {
591 for (i = 0; i < pad->entity->num_links; ++i) {
592 struct media_link *link = &pad->entity->links[i];
593 struct v4l2_mbus_framefmt remote_format;
594
595 if (!(link->flags & MEDIA_LNK_FL_ENABLED))
596 continue;
597
598 if (link->source == pad &&
599 link->sink->entity->info.type == MEDIA_ENT_T_V4L2_SUBDEV) {
600 remote_format = format;
601 set_format(link->sink, &remote_format);
602 }
603 }
604 }
605
606 *endp = end;
607 return 0;
608 }
609
v4l2_subdev_parse_setup_formats(struct media_device * media,const char * p)610 int v4l2_subdev_parse_setup_formats(struct media_device *media, const char *p)
611 {
612 char *end;
613 int ret;
614
615 do {
616 ret = v4l2_subdev_parse_setup_format(media, p, &end);
617 if (ret < 0)
618 return ret;
619
620 p = end + 1;
621 } while (*end == ',');
622
623 return *end ? -EINVAL : 0;
624 }
625
626 static struct {
627 const char *name;
628 enum v4l2_mbus_pixelcode code;
629 } mbus_formats[] = {
630 { "Y8", V4L2_MBUS_FMT_Y8_1X8},
631 { "Y10", V4L2_MBUS_FMT_Y10_1X10 },
632 { "Y12", V4L2_MBUS_FMT_Y12_1X12 },
633 { "YUYV", V4L2_MBUS_FMT_YUYV8_1X16 },
634 { "YUYV1_5X8", V4L2_MBUS_FMT_YUYV8_1_5X8 },
635 { "YUYV2X8", V4L2_MBUS_FMT_YUYV8_2X8 },
636 { "UYVY", V4L2_MBUS_FMT_UYVY8_1X16 },
637 { "UYVY1_5X8", V4L2_MBUS_FMT_UYVY8_1_5X8 },
638 { "UYVY2X8", V4L2_MBUS_FMT_UYVY8_2X8 },
639 { "SBGGR8", V4L2_MBUS_FMT_SBGGR8_1X8 },
640 { "SGBRG8", V4L2_MBUS_FMT_SGBRG8_1X8 },
641 { "SGRBG8", V4L2_MBUS_FMT_SGRBG8_1X8 },
642 { "SRGGB8", V4L2_MBUS_FMT_SRGGB8_1X8 },
643 { "SBGGR10", V4L2_MBUS_FMT_SBGGR10_1X10 },
644 { "SGBRG10", V4L2_MBUS_FMT_SGBRG10_1X10 },
645 { "SGRBG10", V4L2_MBUS_FMT_SGRBG10_1X10 },
646 { "SRGGB10", V4L2_MBUS_FMT_SRGGB10_1X10 },
647 { "SBGGR10_DPCM8", V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8 },
648 { "SGBRG10_DPCM8", V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8 },
649 { "SGRBG10_DPCM8", V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8 },
650 { "SRGGB10_DPCM8", V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8 },
651 { "SBGGR12", V4L2_MBUS_FMT_SBGGR12_1X12 },
652 { "SGBRG12", V4L2_MBUS_FMT_SGBRG12_1X12 },
653 { "SGRBG12", V4L2_MBUS_FMT_SGRBG12_1X12 },
654 { "SRGGB12", V4L2_MBUS_FMT_SRGGB12_1X12 },
655 { "AYUV32", V4L2_MBUS_FMT_AYUV8_1X32 },
656 { "ARGB32", V4L2_MBUS_FMT_ARGB8888_1X32 },
657 };
658
v4l2_subdev_pixelcode_to_string(enum v4l2_mbus_pixelcode code)659 const char *v4l2_subdev_pixelcode_to_string(enum v4l2_mbus_pixelcode code)
660 {
661 unsigned int i;
662
663 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
664 if (mbus_formats[i].code == code)
665 return mbus_formats[i].name;
666 }
667
668 return "unknown";
669 }
670
v4l2_subdev_string_to_pixelcode(const char * string,unsigned int length)671 enum v4l2_mbus_pixelcode v4l2_subdev_string_to_pixelcode(const char *string,
672 unsigned int length)
673 {
674 unsigned int i;
675
676 for (i = 0; i < ARRAY_SIZE(mbus_formats); ++i) {
677 if (strncmp(mbus_formats[i].name, string, length) == 0)
678 break;
679 }
680
681 if (i == ARRAY_SIZE(mbus_formats))
682 return (enum v4l2_mbus_pixelcode)-1;
683
684 return mbus_formats[i].code;
685 }