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