v4l2: Fix white-space issue and compile warning
[glsdk/omapdrmtest.git] / util / v4l2.c
1 /*
2  * Copyright (C) 2011 Texas Instruments
3  * Author: Rob Clark <rob.clark@linaro.org>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
22 #include <linux/videodev2.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <sys/ioctl.h>
27 #include <pthread.h>
28 #include <time.h>
30 #include "util.h"
32 struct v4l2 {
33         int fd;
34         int nbufs;
35         struct v4l2_buffer *v4l2bufs;
36         struct buffer **bufs;
37 };
39 void v4l2_usage(void)
40 {
41         MSG("V4L2 Capture Options:");
42         MSG("\t-c WxH@fourcc\tset capture dimensions/format");
43         MSG("\t-m\t\tdo MCF setup");
44 }
46 /* media_* helpers to do the MCF dance to get things configured properly
47  * so that we can set the specified format on the output device.  For
48  * non-MCF cameras this can just be skipped.
49  */
50 #include <linux/media.h>
51 #include <linux/v4l2-subdev.h>
53 static int
54 media_open_entity(struct media_entity_desc *entity)
55 {
56         struct stat devstat;
57         char devname[32];
58         char sysname[32];
59         char target[1024];
60         char *p;
61         int ret;
63         sprintf(sysname, "/sys/dev/char/%u:%u", entity->v4l.major,
64                         entity->v4l.minor);
65         ret = readlink(sysname, target, sizeof(target));
66         if (ret < 0)
67                 return -errno;
70         target[ret] = '\0';
71         p = strrchr(target, '/');
72         if (p == NULL)
73                 return -EINVAL;
75         sprintf(devname, "/dev/%s", p + 1);
76 MSG("\t%s -> %s -> %s", sysname, target, devname);
77         ret = stat(devname, &devstat);
78         if (ret < 0)
79                 return -errno;
81         /* Sanity check: udev might have reordered the device nodes.
82          * Make sure the major/minor match. We should really use
83          * libudev.
84          */
85         if (major(devstat.st_rdev) == entity->v4l.major &&
86             minor(devstat.st_rdev) == entity->v4l.minor) {
87                 return open(devname, O_RDWR);
88         }
90         return -1;
91 }
93 static int
94 media_find_entity(int fd, struct media_entity_desc *entity,
95                 uint32_t type, uint32_t entity_id)
96 {
97         int id, ret = 0;
99         for (id = 0; ; id = entity->id) {
100                 memset(entity, 0, sizeof(*entity));
101                 entity->id = id | MEDIA_ENT_ID_FLAG_NEXT;
103                 ret = ioctl(fd, MEDIA_IOC_ENUM_ENTITIES, entity);
104                 if (ret) {
105                         ERROR("MEDIA_IOC_ENUM_ENTITIES failed: %s (%d)",
106                                         strerror(errno), ret);
107                         break;
108                 }
110 MSG("\tfound entity: %s type=%08x, flags=%08x, group_id=%d, pads=%d, links=%d",
111 entity->name, entity->type, entity->flags, entity->group_id,
112 entity->pads, entity->links);
114                 if ((entity->type == type) || (entity->id == entity_id))
115                         return 0;
116         }
118         return ret;
121 static void
122 media_configure(struct media_entity_desc *entity,
123                 struct v4l2_format *format, int pad)
125         struct v4l2_subdev_format ent_format = {
126                         .pad = pad,
127                         .which = V4L2_SUBDEV_FORMAT_ACTIVE,
128                         .format = {
129                                         .width  = format->fmt.pix.width,
130                                         .height = format->fmt.pix.height,
131                                         .code   = V4L2_MBUS_FMT_UYVY8_1X16,
132                                         .field  = V4L2_FIELD_NONE,
133                                         .colorspace = V4L2_COLORSPACE_JPEG,
134                         },
135         };
136         int fd, ret;
138         fd = media_open_entity(entity);
139         if (fd < 0) {
140                 ERROR("could not open media device: \"%s\" (%d:%d)",
141                         entity->name,
142                         entity->v4l.major, entity->v4l.minor);
143                 return;
144         }
146         MSG("Setting format for: \"%s\" (%d)", entity->name, pad);
147         ret = ioctl(fd, VIDIOC_SUBDEV_S_FMT, &ent_format);
148         if (ret)
149                 MSG("Could not configure: %s (%d)", strerror(errno), ret);
152 /* walk the graph and attempt to configure all the nodes to the same settings.
153  * This works for smart-sensor with no element in between that can convert/
154  * scale..  If the sensor can't handle the settings, then S_FMT just fails
155  * and hopefully some element in between can pick up the slack.
156  */
157 static int
158 media_setup(struct v4l2_format *format)
160         struct media_entity_desc entity;
161         int fd, ret;
163         fd = open("/dev/media0", O_RDWR);
164         if (fd < 0) {
165                 ERROR("could not open MCF: %s (%d)", strerror(errno), errno);
166                 return fd;
167         }
169         ret = media_find_entity(fd, &entity,
170                                 MEDIA_ENT_T_V4L2_SUBDEV_SENSOR, ~0);
171         if (ret)
172                 return ret;
174         /* now walk the graph to the output, configure everything on the way: */
175         do {
176                 struct media_link_desc links[10];
177                 struct media_links_enum link_enum = {
178                                 .entity = entity.id,
179                                 .links = links,
180                 };
181                 int i;
183                 ret = ioctl(fd, MEDIA_IOC_ENUM_LINKS, &link_enum);
184                 if (ret) {
185                         ERROR("MEDIA_IOC_ENUM_LINKS failed: %s (%d)",
186                                         strerror(errno), ret);
187                         return ret;
188                 }
190                 for (i = 0; i < entity.links; i++) {
191                         if (links[i].source.entity == entity.id) {
192                                 /*
193                                  * XXX maybe if there are multiple links, we
194                                  * should prefer an enabled link, otherwise
195                                  * just pick one..
196                                  */
197                                 media_configure(&entity, format,
198                                                 links[i].source.index);
199                                 media_configure(&entity, format,
200                                                 links[i].sink.index);
202                                 /* lets take this link.. */
203                                 if (!(links[i].flags & MEDIA_LNK_FL_ENABLED)) {
204                                         links[i].flags |= MEDIA_LNK_FL_ENABLED;
205                                         ret = ioctl(fd, MEDIA_IOC_SETUP_LINK,
206                                                     &links[i]);
207                                         if (ret)
208                                                 ERROR("MEDIA_IOC_SETUP_LINK failed: %s (%d)",
209                                                         strerror(errno), errno);
210                                 }
212                                 ret = media_find_entity(fd, &entity, ~0,
213                                                         links[i].sink.entity);
214                                 if (ret)
215                                         return ret;
217                                 break;
218                         }
219                 }
220         } while (entity.type != MEDIA_ENT_T_DEVNODE_V4L);
222         return 0;
225 /* Open v4l2 (and media0??) XXX */
226 struct v4l2 *
227 v4l2_open(int argc, char **argv, uint32_t *fourcc,
228                 uint32_t *width, uint32_t *height)
230         struct v4l2_format format = {
231                         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
232         };
233         struct v4l2 *v4l2;
234         int i, ret;
235         bool mcf = false, user_format = false, user_dev = false;
236         char devname[20] = "/dev/video1";
237         char fourccstr[5];
239         v4l2 = calloc(1, sizeof(*v4l2));
241         /* note: set args to NULL after we've parsed them so other modules know
242          * that it is already parsed (since the arg parsing is decentralized)
243          */
244         for (i = 1; i < argc; i++) {
245                 if (!argv[i])
246                         continue;
247                 if (!strcmp("-d", argv[i])) {
248                         if (user_dev)
249                                 continue;
251                         argv[i++] = NULL;
252                         if (sscanf(argv[i], "%s", devname) != 1) {
253                                 ERROR("invalid device node: %s", argv[i]);
254                                 goto fail;
255                         }
256                         user_dev = true;
257                 } else if (!strcmp("-c", argv[i])) {
258                         if (user_format)
259                                 continue;
261                         argv[i++] = NULL;
262                         if (sscanf(argv[i], "%ux%u@%4s",
263                                         width,
264                                         height,
265                                         fourccstr) != 3) {
266                                 ERROR("invalid arg: %s", argv[i]);
267                                 goto fail;
268                         }
269                         *fourcc = FOURCC_STR(fourccstr);
270                         user_format = true;
271                 } else if (!strcmp(argv[i], "-m")) {
272                         mcf = true;
273                 } else {
274                         continue;
275                 }
276                 argv[i] = NULL;
277         }
279         v4l2->fd = open(devname, O_RDWR);
281         ret = ioctl(v4l2->fd, VIDIOC_G_FMT, &format);
282         if (ret < 0) {
283                 ERROR("VIDIOC_G_FMT failed: %s (%d)", strerror(errno), ret);
284                 goto fail;
285         }
287         if (user_format) {
288                 format.fmt.pix.pixelformat = *fourcc;
289                 format.fmt.pix.width = *width;
290                 format.fmt.pix.height = *height;
291                 if (*fourcc == FOURCC_STR("AR24"))
292                         format.fmt.pix.pixelformat = FOURCC_STR("RGB4");
294         }
296         if ((format.fmt.pix.width == 0) || (format.fmt.pix.height == 0) ||
297                         (format.fmt.pix.pixelformat == 0)) {
299                 ERROR("invalid capture settings '%dx%d@%4s' (did you not use '-c'?)",
300                         format.fmt.pix.width, format.fmt.pix.height,
301                         (char *)&format.fmt.pix.pixelformat);
302                 goto fail;
303         }
305         if (mcf) {
306                 ret = media_setup(&format);
307                 if (ret < 0)
308                         goto fail;
309         }
311         ret = ioctl(v4l2->fd, VIDIOC_S_FMT, &format);
312         if (ret < 0) {
313                 ERROR("VIDIOC_S_FMT failed: %s (%d)", strerror(errno), ret);
314                 goto fail;
315         }
317         return v4l2;
319 fail:
320         return NULL;
323 int
324 v4l2_reqbufs(struct v4l2 *v4l2, struct buffer **bufs, uint32_t n)
326         struct v4l2_requestbuffers reqbuf = {
327                         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
328                         .memory = V4L2_MEMORY_DMABUF,
329                         .count = n,
330         };
331         uint32_t i;
332         int ret, dmafd;
334         if (v4l2->v4l2bufs) {
335                 /* maybe eventually need to support this? */
336                 ERROR("already reqbuf'd");
337                 return -1;
338         }
340         ret = ioctl(v4l2->fd, VIDIOC_REQBUFS, &reqbuf);
341         if (ret < 0) {
342                 ERROR("VIDIOC_REQBUFS failed: %s (%d)", strerror(errno), ret);
343                 return ret;
344         }
346         if ((reqbuf.count != n) ||
347                         (reqbuf.type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
348                         (reqbuf.memory != V4L2_MEMORY_DMABUF)) {
349                 ERROR("unsupported..");
350                 return -1;
351         }
353         v4l2->nbufs = reqbuf.count;
354         v4l2->bufs = bufs;
355         v4l2->v4l2bufs = calloc(v4l2->nbufs, sizeof(*v4l2->v4l2bufs));
356         if (!v4l2->v4l2bufs) {
357                 ERROR("allocation failed");
358                 return -1;
359         }
361         for (i = 0; i < reqbuf.count; i++) {
362                 assert(bufs[i]->nbo == 1); /* TODO add multi-planar support */
363                 /* Call omap_bo_dmabuf only once, to export only once
364                  * Otherwise, each call will return duplicated fds
365                  * This way, every call to omap_bo_dmabuf will return a new fd
366                  * Which won't match with any previously exported fds
367                  * Instead, store dma fd in buf->fd[] */
368                 dmafd = omap_bo_dmabuf(bufs[i]->bo[0]);
369                 bufs[i]->fd[0] = dmafd;
370                 v4l2->v4l2bufs[i] = (struct v4l2_buffer){
371                         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
372                                         .memory = V4L2_MEMORY_DMABUF,
373                                         .index = i,
374                                         .m.fd = dmafd,
375                 };
376                 MSG("Exported buffer fd = %d\n", dmafd);
377                 ret = ioctl(v4l2->fd, VIDIOC_QUERYBUF, &v4l2->v4l2bufs[i]);
378                 v4l2->v4l2bufs[i].m.fd = dmafd;
379                 if (ret) {
380                         ERROR("VIDIOC_QUERYBUF failed: %s (%d)",
381                               strerror(errno), ret);
382                         return ret;
383                 }
384         }
386         return 0;
389 int
390 v4l2_streamon(struct v4l2 *v4l2)
392         enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
393         int ret;
395         ret = ioctl(v4l2->fd, VIDIOC_STREAMON, &type);
397         if (ret)
398                 ERROR("VIDIOC_STREAMON failed: %s (%d)", strerror(errno), ret);
400         return ret;
403 int
404 v4l2_streamoff(struct v4l2 *v4l2)
406         enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
407         int ret;
409             ret = ioctl(v4l2->fd, VIDIOC_STREAMOFF, &type);
411             if (ret)
412                 ERROR("VIDIOC_STREAMOFF failed: %s (%d)", strerror(errno), ret);
414             return ret;
417 int
418 v4l2_qbuf(struct v4l2 *v4l2, struct buffer *buf)
420         struct v4l2_buffer *v4l2buf = NULL;
421         int i, ret, fd;
423         struct timeval disp_time;
424         struct timeval disp_lat;
425         struct timespec now;
426         pthread_t tid;
428         assert(buf->nbo == 1); /* TODO add multi-planar support */
430         fd = buf->fd[0];
432         for (i = 0; i < v4l2->nbufs; i++) {
433                 if (v4l2->v4l2bufs[i].m.fd == fd)
434                         v4l2buf = &v4l2->v4l2bufs[i];
435         }
437         tid = pthread_self();
438         clock_gettime(CLOCK_MONOTONIC, &now);
439         disp_time.tv_sec = now.tv_sec;
440         disp_time.tv_usec = now.tv_nsec / 1000;
441         timersub(&disp_time, &v4l2buf->timestamp, &disp_lat);
442         if(v4l2buf->timestamp.tv_sec)
443                 DBG("%lx: DISP latency = %ld.%6ld", tid, disp_lat.tv_sec, disp_lat.tv_usec);
445         if (!v4l2buf) {
446                 ERROR("invalid buffer");
447                 return -1;
448         }
450         ret = ioctl(v4l2->fd, VIDIOC_QBUF, v4l2buf);
451         v4l2buf->m.fd = buf->fd[0];
452         if (ret)
453                 ERROR("VIDIOC_QBUF failed: %s (%d)", strerror(errno), ret);
455         return ret;
458 struct buffer *
459 v4l2_dqbuf(struct v4l2 *v4l2)
461         struct timeval dq_time;
462         struct timeval dq_lat;
463         struct timespec now;
464         pthread_t tid;
466         struct buffer *buf;
467         struct v4l2_buffer v4l2buf = {
468                         .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
469                         .memory = V4L2_MEMORY_DMABUF,
470         };
471         int ret;
473         ret = ioctl(v4l2->fd, VIDIOC_DQBUF, &v4l2buf);
474         if (ret)
475                 ERROR("VIDIOC_DQBUF failed: %s (%d)", strerror(errno), ret);
477         buf = v4l2->bufs[v4l2buf.index];
478         v4l2->v4l2bufs[v4l2buf.index].timestamp = v4l2buf.timestamp;
480         assert(buf->nbo == 1); /* TODO add multi-planar support */
482         tid = pthread_self();
483         clock_gettime(CLOCK_MONOTONIC, &now);
484         dq_time.tv_sec = now.tv_sec;
485         dq_time.tv_usec = now.tv_nsec / 1000;
486         timersub(&dq_time, &v4l2buf.timestamp, &dq_lat);
487         DBG("%lx: DQ latency = %ld.%6ld", tid, dq_lat.tv_sec, dq_lat.tv_usec);
489         return buf;