Debian Codegraph @

utils/libv4l2util/v4l2_driver.c

raw
1 /*
2 Copyright (C) 2006 Mauro Carvalho Chehab <[email protected]>
3
4 The libv4l2util Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The libv4l2util Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 */
14
15 #include <assert.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <sys/ioctl.h>
23 #include <sys/mman.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26
27 #include <stdlib.h>
28
29 #include "v4l2_driver.h"
30
31 /****************************************************************************
32 Auxiliary routines
33 ****************************************************************************/
34 static int xioctl (int fd, unsigned long int request, void *arg)
35 {
36 int r;
37
38 do r = ioctl (fd, request, arg);
39 while (-1 == r && EINTR == errno);
40
41 return r;
42 }
43
44 static void free_list(struct drv_list **list_ptr)
45 {
46 struct drv_list *prev,*cur;
47
48 if (list_ptr==NULL)
49 return;
50
51 prev=*list_ptr;
52 if (prev==NULL)
53 return;
54
55 do {
56 cur=prev->next;
57 if (prev->curr)
58 free (prev->curr); // Free data
59 free (prev); // Free list
60 prev=cur;
61 } while (prev);
62
63 *list_ptr=NULL;
64 }
65
66 /****************************************************************************
67 Auxiliary Arrays to aid debug messages
68 ****************************************************************************/
69 char *v4l2_field_names[] = {
70 [V4L2_FIELD_ANY] = "any",
71 [V4L2_FIELD_NONE] = "none",
72 [V4L2_FIELD_TOP] = "top",
73 [V4L2_FIELD_BOTTOM] = "bottom",
74 [V4L2_FIELD_INTERLACED] = "interlaced",
75 [V4L2_FIELD_SEQ_TB] = "seq-tb",
76 [V4L2_FIELD_SEQ_BT] = "seq-bt",
77 [V4L2_FIELD_ALTERNATE] = "alternate",
78 };
79
80 char *v4l2_type_names[] = {
81 [V4L2_BUF_TYPE_VIDEO_CAPTURE] = "video-cap",
82 [V4L2_BUF_TYPE_VIDEO_OVERLAY] = "video-over",
83 [V4L2_BUF_TYPE_VIDEO_OUTPUT] = "video-out",
84 [V4L2_BUF_TYPE_VBI_CAPTURE] = "vbi-cap",
85 [V4L2_BUF_TYPE_VBI_OUTPUT] = "vbi-out",
86 [V4L2_BUF_TYPE_SLICED_VBI_CAPTURE] = "sliced-vbi-cap",
87 [V4L2_BUF_TYPE_SLICED_VBI_OUTPUT] = "slicec-vbi-out",
88 };
89
90 static char *v4l2_memory_names[] = {
91 [V4L2_MEMORY_MMAP] = "mmap",
92 [V4L2_MEMORY_USERPTR] = "userptr",
93 [V4L2_MEMORY_OVERLAY] = "overlay",
94 };
95
96 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(*arr))
97 #define prt_names(a,arr) (((a)<ARRAY_SIZE(arr))?arr[a]:"unknown")
98
99 static char *prt_caps(uint32_t caps)
100 {
101 static char s[4096]="";
102
103 s[0] = '\0';
104
105 if (V4L2_CAP_VIDEO_CAPTURE & caps)
106 strcat (s,"CAPTURE ");
107 if (V4L2_CAP_VIDEO_CAPTURE_MPLANE & caps)
108 strcat (s,"CAPTURE_MPLANE ");
109 if (V4L2_CAP_VIDEO_OUTPUT & caps)
110 strcat (s,"OUTPUT ");
111 if (V4L2_CAP_VIDEO_OUTPUT_MPLANE & caps)
112 strcat (s,"OUTPUT_MPLANE ");
113 if (V4L2_CAP_VIDEO_M2M & caps)
114 strcat (s,"M2M ");
115 if (V4L2_CAP_VIDEO_M2M_MPLANE & caps)
116 strcat (s,"M2M_MPLANE ");
117 if (V4L2_CAP_VIDEO_OVERLAY & caps)
118 strcat (s,"OVERLAY ");
119 if (V4L2_CAP_VBI_CAPTURE & caps)
120 strcat (s,"VBI_CAPTURE ");
121 if (V4L2_CAP_VBI_OUTPUT & caps)
122 strcat (s,"VBI_OUTPUT ");
123 if (V4L2_CAP_SLICED_VBI_CAPTURE & caps)
124 strcat (s,"SLICED_VBI_CAPTURE ");
125 if (V4L2_CAP_SLICED_VBI_OUTPUT & caps)
126 strcat (s,"SLICED_VBI_OUTPUT ");
127 if (V4L2_CAP_RDS_CAPTURE & caps)
128 strcat (s,"RDS_CAPTURE ");
129 if (V4L2_CAP_RDS_OUTPUT & caps)
130 strcat (s,"RDS_OUTPUT ");
131 if (V4L2_CAP_SDR_CAPTURE & caps)
132 strcat (s,"SDR_CAPTURE ");
133 if (V4L2_CAP_TUNER & caps)
134 strcat (s,"TUNER ");
135 if (V4L2_CAP_HW_FREQ_SEEK & caps)
136 strcat (s,"HW_FREQ_SEEK ");
137 if (V4L2_CAP_MODULATOR & caps)
138 strcat (s,"MODULATOR ");
139 if (V4L2_CAP_AUDIO & caps)
140 strcat (s,"AUDIO ");
141 if (V4L2_CAP_RADIO & caps)
142 strcat (s,"RADIO ");
143 if (V4L2_CAP_READWRITE & caps)
144 strcat (s,"READWRITE ");
145 if (V4L2_CAP_STREAMING & caps)
146 strcat (s,"STREAMING ");
147 if (V4L2_CAP_EXT_PIX_FORMAT & caps)
148 strcat (s,"EXT_PIX_FORMAT ");
149 if (V4L2_CAP_DEVICE_CAPS & caps)
150 strcat (s,"DEVICE_CAPS ");
151 if(V4L2_CAP_VIDEO_OUTPUT_OVERLAY & caps)
152 strcat (s,"VIDEO_OUTPUT_OVERLAY ");
153 if(V4L2_CAP_HW_FREQ_SEEK & caps)
154 strcat (s,"HW_FREQ_SEEK ");
155 if(V4L2_CAP_VIDEO_M2M_MPLANE & caps)
156 strcat (s,"VIDEO_M2M_MPLANE ");
157 if(V4L2_CAP_VIDEO_M2M & caps)
158 strcat (s,"VIDEO_M2M ");
159 if(V4L2_CAP_SDR_OUTPUT & caps)
160 strcat (s,"SDR_OUTPUT ");
161 if(V4L2_CAP_META_CAPTURE & caps)
162 strcat (s,"META_CAPTURE ");
163 if(V4L2_CAP_META_OUTPUT & caps)
164 strcat (s,"META_OUTPUT ");
165 if(V4L2_CAP_TOUCH & caps)
166 strcat (s,"TOUCH ");
167 if(V4L2_CAP_IO_MC & caps)
168 strcat (s,"IO_MC ");
169
170 return s;
171 }
172
173 static void prt_buf_info(char *name,struct v4l2_buffer *p)
174 {
175 struct v4l2_timecode *tc=&p->timecode;
176
177 printf ("%s: %02llu:%02d:%02d.%08llu index=%d, type=%s, "
178 "bytesused=%d, flags=0x%08x, "
179 "field=%s, sequence=%d, memory=%s, offset=0x%08x, length=%d\n",
180 name, (__u64)(p->timestamp.tv_sec/3600),
181 (int)(p->timestamp.tv_sec/60)%60,
182 (int)(p->timestamp.tv_sec%60),
183 (__u64)p->timestamp.tv_usec,
184 p->index,
185 prt_names(p->type,v4l2_type_names),
186 p->bytesused,p->flags,
187 prt_names(p->field,v4l2_field_names),
188 p->sequence,
189 prt_names(p->memory,v4l2_memory_names),
190 p->m.offset,
191 p->length);
192 tc=&p->timecode;
193 printf ("\tTIMECODE: %02d:%02d:%02d type=%d, "
194 "flags=0x%08x, frames=%d, userbits=0x%02x%02x%02x%02x\n",
195 tc->hours,tc->minutes,tc->seconds,
196 tc->type, tc->flags, tc->frames,
197 tc->userbits[0],
198 tc->userbits[1],
199 tc->userbits[2],
200 tc->userbits[3]);
201 }
202
203 /****************************************************************************
204 Open V4L2 devices
205 ****************************************************************************/
206 int v4l2_open (char *device, int debug, struct v4l2_driver *drv)
207 {
208 int ret;
209
210 memset(drv,0,sizeof(*drv));
211
212 drv->debug=debug;
213
214 if ((drv->fd = open(device, O_RDWR )) < 0) {
215 return(-errno);
216 }
217
218 ret=xioctl(drv->fd,VIDIOC_QUERYCAP,(void *) &drv->cap);
219 if (!ret && drv->debug) {
220 printf ("driver=%s, card=%s, bus=%s, version=%d.%d.%d, "
221 "capabilities=%s, device_caps=%s\n",
222 drv->cap.driver,drv->cap.card,drv->cap.bus_info,
223 (drv->cap.version >> 16) & 0xff,
224 (drv->cap.version >> 8) & 0xff,
225 drv->cap.version & 0xff,
226 prt_caps(drv->cap.capabilities),
227 (drv->cap.capabilities & V4L2_CAP_DEVICE_CAPS) ?
228 prt_caps(drv->cap.device_caps) : "N/A");
229
230
231 }
232 return ret;
233 }
234
235
236 /****************************************************************************
237 V4L2 Eumberations
238 ****************************************************************************/
239 int v4l2_enum_stds (struct v4l2_driver *drv)
240 {
241 struct v4l2_standard *p=NULL;
242 struct drv_list *list;
243 int ok=0,i;
244
245 free_list(&drv->stds);
246
247 list=drv->stds=calloc(1,sizeof(*drv->stds));
248 assert (list!=NULL);
249
250 for (i=0; ok==0; i++) {
251 p=calloc(1,sizeof(*p));
252 assert (p);
253
254 p->index=i;
255 ok=xioctl(drv->fd,VIDIOC_ENUMSTD,p);
256 if (ok<0) {
257 ok=-errno;
258 free(p);
259 break;
260 }
261 if (drv->debug) {
262 printf ("STANDARD: index=%d, id=0x%08x, name=%s, fps=%.3f, "
263 "framelines=%d\n", p->index,
264 (unsigned int)p->id, p->name,
265 1.*p->frameperiod.denominator/p->frameperiod.numerator,
266 p->framelines);
267 }
268 if (list->curr) {
269 list->next=calloc(1,sizeof(*list->next));
270 list=list->next;
271 assert (list!=NULL);
272 }
273 list->curr=p;
274 }
275 if (i>0 && ok==-EINVAL)
276 return 0;
277
278 return ok;
279 }
280
281 int v4l2_enum_input (struct v4l2_driver *drv)
282 {
283 struct v4l2_input *p=NULL;
284 struct drv_list *list;
285 int ok=0,i;
286
287 free_list(&drv->inputs);
288
289 list=drv->inputs=calloc(1,sizeof(*drv->inputs));
290 assert (list!=NULL);
291
292 for (i=0; ok==0; i++) {
293 p=calloc(1,sizeof(*p));
294 assert (p);
295 p->index=i;
296 ok=xioctl(drv->fd,VIDIOC_ENUMINPUT,p);
297 if (ok<0) {
298 ok=errno;
299 free(p);
300 break;
301 }
302 if (drv->debug) {
303 printf ("INPUT: index=%d, name=%s, type=%d, audioset=%d, "
304 "tuner=%d, std=%08x, status=%d\n",
305 p->index,p->name,p->type,p->audioset, p->tuner,
306 (unsigned int)p->std, p->status);
307 }
308 if (list->curr) {
309 list->next=calloc(1,sizeof(*list->next));
310 list=list->next;
311 assert (list!=NULL);
312 }
313 list->curr=p;
314 }
315 if (i>0 && ok==-EINVAL)
316 return 0;
317 return ok;
318 }
319
320 int v4l2_enum_fmt (struct v4l2_driver *drv, enum v4l2_buf_type type)
321 {
322 struct v4l2_fmtdesc *p=NULL;
323 struct drv_list *list;
324 int ok=0,i;
325
326 free_list(&drv->fmt_caps);
327
328 list=drv->fmt_caps=calloc(1,sizeof(*drv->fmt_caps));
329 assert (list!=NULL);
330
331 for (i=0; ok==0; i++) {
332 p=calloc(1,sizeof(*p));
333 assert (p!=NULL);
334
335 p->index=i;
336 p->type =type;
337
338 ok=xioctl(drv->fd,VIDIOC_ENUM_FMT,p);
339 if (ok<0) {
340 ok=errno;
341 free(p);
342 break;
343 }
344 if (drv->debug) {
345 printf ("FORMAT: index=%d, type=%d, flags=%d, description='%s'\n\t"
346 "fourcc=%c%c%c%c\n",
347 p->index, p->type, p->flags,p->description,
348 p->pixelformat & 0xff,
349 (p->pixelformat >> 8) & 0xff,
350 (p->pixelformat >> 16) & 0xff,
351 (p->pixelformat >> 24) & 0xff
352 );
353 }
354 if (list->curr) {
355 list->next=calloc(1,sizeof(*list->next));
356 list=list->next;
357 assert (list!=NULL);
358 }
359 list->curr=p;
360 }
361 if (i>0 && ok==-EINVAL)
362 return 0;
363 return ok;
364 }
365
366 /****************************************************************************
367 Set routines - currently, it also checks results with Get
368 ****************************************************************************/
369 int v4l2_setget_std (struct v4l2_driver *drv, enum v4l2_direction dir, v4l2_std_id *id)
370 {
371 v4l2_std_id s_id=*id;
372 int ret=0;
373 char s[256];
374
375 if (dir & V4L2_SET) {
376 ret=xioctl(drv->fd,VIDIOC_S_STD,&s_id);
377 if (ret<0) {
378 ret=errno;
379
380 sprintf (s,"while trying to set STD to %08x",
381 (unsigned int) *id);
382 perror(s);
383 }
384 }
385
386 if (dir & V4L2_GET) {
387 ret=xioctl(drv->fd,VIDIOC_G_STD,&s_id);
388 if (ret<0) {
389 ret=errno;
390 perror ("while trying to get STD id");
391 }
392 }
393
394 if (dir == V4L2_SET_GET) {
395 if (*id & s_id) {
396 if (*id != s_id) {
397 printf ("Warning: Received a std subset (%08x"
398 " std) while trying to adjust to %08x\n",
399 (unsigned int) s_id,(unsigned int) *id);
400 }
401 } else {
402 fprintf (stderr,"Error: Received %08x std while trying"
403 " to adjust to %08x\n",
404 (unsigned int) s_id,(unsigned int) *id);
405 }
406 }
407 return ret;
408 }
409
410 int v4l2_setget_input (struct v4l2_driver *drv, enum v4l2_direction dir, struct v4l2_input *input)
411 {
412 int ok=0,ret;
413 unsigned int inp=input->index;
414 char s[256];
415
416 if (dir & V4L2_SET) {
417 ret=xioctl(drv->fd,VIDIOC_S_INPUT,input);
418 if (ret<0) {
419 ret=errno;
420 sprintf (s,"while trying to set INPUT to %d\n", inp);
421 perror(s);
422 }
423 }
424
425 if (dir & V4L2_GET) {
426 ret=xioctl(drv->fd,VIDIOC_G_INPUT,input);
427 if (ret<0) {
428 perror ("while trying to get INPUT id\n");
429 }
430 }
431
432 if (dir & V4L2_SET_GET) {
433 if (input->index != inp) {
434 printf ("Input is different than expected (received %i, set %i)\n",
435 inp, input->index);
436 }
437 }
438
439 return ok;
440 }
441
442 int v4l2_gettryset_fmt_cap (struct v4l2_driver *drv, enum v4l2_direction dir,
443 struct v4l2_format *fmt,uint32_t width, uint32_t height,
444 uint32_t pixelformat, enum v4l2_field field)
445 {
446 struct v4l2_pix_format *pix=&(fmt->fmt.pix);
447 int ret=0;
448
449 fmt->type=V4L2_BUF_TYPE_VIDEO_CAPTURE;
450 if (dir == V4L2_GET) {
451 ret=xioctl(drv->fd,VIDIOC_G_FMT,fmt);
452 if (ret < 0) {
453 ret=errno;
454 perror("VIDIOC_G_FMT failed\n");
455 }
456 return ret;
457 }
458 if (dir & (~(V4L2_TRY | V4L2_SET)) ) {
459 perror ("Invalid direction\n");
460 return EINVAL;
461 }
462
463 if (dir & (V4L2_TRY|V4L2_SET)) {
464 pix->width = width;
465 pix->height = height;
466 pix->pixelformat = pixelformat;
467 pix->field = field;
468 /*
469 enum v4l2_colorspace colorspace;
470 */
471
472 if (dir & V4L2_TRY) {
473 ret=xioctl(drv->fd,VIDIOC_TRY_FMT,fmt);
474 if (ret < 0) {
475 perror("VIDIOC_TRY_FMT failed\n");
476 }
477 }
478
479 if (dir & V4L2_SET) {
480 ret=xioctl(drv->fd,VIDIOC_S_FMT,fmt);
481 if (ret < 0) {
482 perror("VIDIOC_S_FMT failed\n");
483 }
484 drv->sizeimage=pix->sizeimage;
485 }
486
487 if (pix->pixelformat != pixelformat) {
488 fprintf(stderr,"Error: asked for format %d, received %d",pixelformat,
489 pix->pixelformat);
490 }
491
492 if (pix->width != width) {
493 fprintf(stderr,"Error: asked for format %d, received %d\n",width,
494 pix->width);
495 }
496
497 if (pix->height != height) {
498 fprintf(stderr,"Error: asked for format %d, received %d\n",height,
499 pix->height);
500 }
501
502 if (pix->bytesperline == 0 ) {
503 fprintf(stderr,"Error: bytesperline = 0\n");
504 }
505
506 if (pix->sizeimage == 0 ) {
507 fprintf(stderr,"Error: sizeimage = 0\n");
508 }
509 }
510
511 if (drv->debug)
512 printf( "FMT SET: %ux%u, fourcc=%c%c%c%c, %u bytes/line,"
513 " %u bytes/frame, colorspace=0x%08x\n",
514 pix->width, pix->height,
515 pix->pixelformat & 0xff,
516 (pix->pixelformat >> 8) & 0xff,
517 (pix->pixelformat >> 16) & 0xff,
518 (pix->pixelformat >> 24) & 0xff,
519 pix->bytesperline,
520 pix->sizeimage,
521 pix->colorspace);
522
523 return 0;
524 }
525
526 /****************************************************************************
527 Get routines
528 ****************************************************************************/
529 int v4l2_get_parm (struct v4l2_driver *drv)
530 {
531 int ret;
532 struct v4l2_captureparm *c;
533
534 drv->parm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
535 if ((ret=xioctl(drv->fd,VIDIOC_G_PARM,&drv->parm))>=0) {
536 c=&drv->parm.parm.capture;
537 printf ("PARM: capability=%d, capturemode=%d, %.3f fps "
538 "ext=%x, readbuf=%d\n",
539 c->capability,
540 c->capturemode,
541 c->timeperframe.denominator*1./c->timeperframe.numerator,
542 c->extendedmode, c->readbuffers);
543 } else {
544 ret=errno;
545
546 perror ("VIDIOC_G_PARM");
547 }
548
549 return ret;
550 }
551
552 /****************************************************************************
553 Queue and stream control
554 ****************************************************************************/
555
556 int v4l2_free_bufs(struct v4l2_driver *drv)
557 {
558 unsigned int i;
559
560 if (!drv->n_bufs)
561 return 0;
562
563 /* Requests the driver to free all buffers */
564 drv->reqbuf.count = 0;
565 drv->reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
566 drv->reqbuf.memory = V4L2_MEMORY_MMAP;
567
568 /* stop capture */
569 if (xioctl(drv->fd,VIDIOC_STREAMOFF,&drv->reqbuf.type)<0)
570 return errno;
571
572 sleep (1); // FIXME: Should check if all buffers are stopped
573
574 /* V4L2 API says REQBUFS with count=0 should be used to release buffer.
575 However, video-buf.c doesn't implement it.
576 */
577 #if 0
578 if (xioctl(drv->fd,VIDIOC_REQBUFS,&drv->reqbuf)<0) {
579 perror("reqbufs while freeing buffers");
580 return errno;
581 }
582 #endif
583
584 if (drv->reqbuf.count != 0) {
585 fprintf(stderr,"REQBUFS returned %d buffers while asking for freeing it!\n",
586 drv->reqbuf.count);
587 }
588
589 for (i = 0; i < drv->n_bufs; i++) {
590 if (drv->bufs[i].length)
591 munmap(drv->bufs[i].start, drv->bufs[i].length);
592 if (drv->v4l2_bufs[i])
593 free (drv->v4l2_bufs[i]);
594 }
595
596 free(drv->v4l2_bufs);
597 free(drv->bufs);
598
599 drv->v4l2_bufs=NULL;
600 drv->bufs=NULL;
601 drv->n_bufs=0;
602
603 return 0;
604 }
605
606 int v4l2_mmap_bufs(struct v4l2_driver *drv, unsigned int num_buffers)
607 {
608 /* Frees previous allocations, if required */
609 v4l2_free_bufs(drv);
610
611 if (drv->sizeimage==0) {
612 fprintf(stderr,"Image size is zero! Can't proceed\n");
613 return -1;
614 }
615 /* Requests the specified number of buffers */
616 drv->reqbuf.count = num_buffers;
617 drv->reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
618 drv->reqbuf.memory = V4L2_MEMORY_MMAP;
619
620 if (xioctl(drv->fd,VIDIOC_REQBUFS,&drv->reqbuf)<0) {
621 perror("reqbufs");
622 return errno;
623 }
624
625 if (drv->debug)
626 printf ("REQBUFS: count=%d, type=%s, memory=%s\n",
627 drv->reqbuf.count,
628 prt_names(drv->reqbuf.type,v4l2_type_names),
629 prt_names(drv->reqbuf.memory,v4l2_memory_names));
630
631 /* Allocates the required number of buffers */
632 drv->v4l2_bufs=calloc(drv->reqbuf.count, sizeof(*drv->v4l2_bufs));
633 assert(drv->v4l2_bufs!=NULL);
634 drv->bufs=calloc(drv->reqbuf.count, sizeof(*drv->bufs));
635 assert(drv->bufs!=NULL);
636
637 for (drv->n_bufs = 0; drv->n_bufs < drv->reqbuf.count; drv->n_bufs++) {
638 struct v4l2_buffer *p;
639
640 /* Requests kernel buffers to be mmapped */
641 p=drv->v4l2_bufs[drv->n_bufs]=calloc(1,sizeof(*p));
642 assert (p!=NULL);
643 p->index = drv->n_bufs;
644 p->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
645 p->memory = V4L2_MEMORY_MMAP;
646 if (xioctl(drv->fd,VIDIOC_QUERYBUF,p)<0) {
647 int ret=errno;
648 perror("querybuf");
649
650 free (drv->v4l2_bufs[drv->n_bufs]);
651
652 v4l2_free_bufs(drv);
653 return ret;
654 }
655
656 if (drv->debug)
657 prt_buf_info("QUERYBUF",p);
658
659 if (drv->sizeimage != p->length) {
660 if (drv->sizeimage < p->length) {
661 fprintf (stderr, "QUERYBUF: ERROR: VIDIOC_S_FMT said buffer should have %d size, but received %d from QUERYBUF!\n",
662 drv->sizeimage, p->length);
663 } else {
664 fprintf (stderr, "QUERYBUF: Expecting %d size, received %d buff length\n",
665 drv->sizeimage, p->length);
666 }
667 }
668
669 drv->bufs[drv->n_bufs].length = p->length;
670 drv->bufs[drv->n_bufs].start = mmap (NULL, /* start anywhere */
671 p->length,
672 PROT_READ | PROT_WRITE, /* required */
673 MAP_SHARED, /* recommended */
674 drv->fd, p->m.offset);
675
676
677 if (MAP_FAILED == drv->bufs[drv->n_bufs].start) {
678 perror("mmap");
679
680 free (drv->v4l2_bufs[drv->n_bufs]);
681 v4l2_free_bufs(drv);
682 return errno;
683 }
684 }
685
686 return 0;
687 }
688
689 /* Returns <0, if error, 0 if nothing to read and <size>, if something
690 read
691 */
692 int v4l2_rcvbuf(struct v4l2_driver *drv, v4l2_recebe_buffer *rec_buf)
693 {
694 int ret;
695
696 struct v4l2_buffer buf;
697 memset (&buf, 0, sizeof(buf));
698
699 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
700 buf.memory = V4L2_MEMORY_MMAP;
701
702 if (-1 == xioctl (drv->fd, VIDIOC_DQBUF, &buf)) {
703 switch (errno) {
704 case EAGAIN:
705 return 0;
706
707 case EIO:
708 /* Could ignore EIO, see spec. */
709
710 /* fall through */
711
712 default:
713 perror ("dqbuf");
714 return -errno;
715 }
716 }
717 prt_buf_info("DQBUF",&buf);
718
719 assert (buf.index < drv->n_bufs);
720
721 ret = rec_buf (&buf,&drv->bufs[buf.index]);
722
723 if (ret<0) {
724 v4l2_free_bufs(drv);
725 return ret;
726 }
727
728 if (-1 == xioctl (drv->fd, VIDIOC_QBUF, &buf)) {
729 perror ("qbuf");
730 return -errno;
731 }
732 return ret;
733 }
734
735 int v4l2_start_streaming(struct v4l2_driver *drv)
736 {
737 uint32_t i;
738 struct v4l2_buffer buf;
739
740 if (drv->debug)
741 printf("Activating %d queues\n", drv->n_bufs);
742 for (i = 0; i < drv->n_bufs; i++) {
743 int res;
744
745 memset(&buf, 0, sizeof(buf));
746 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
747 buf.memory = V4L2_MEMORY_MMAP;
748 buf.index = i;
749
750 res = xioctl (drv->fd, VIDIOC_QBUF, &buf);
751
752 if (!res)
753 prt_buf_info("QBUF",&buf);
754 else {
755 perror("qbuf");
756 return errno;
757 }
758 }
759
760 /* Activates stream */
761 if (drv->debug)
762 printf("Enabling streaming\n");
763
764 if (xioctl(drv->fd,VIDIOC_STREAMON,&drv->reqbuf.type)<0)
765 return errno;
766
767 return 0;
768 }
769
770 int v4l2_stop_streaming(struct v4l2_driver *drv)
771 {
772 v4l2_free_bufs(drv);
773
774 return 0;
775 }
776
777 /****************************************************************************
778 Close V4L2, disallocating all structs
779 ****************************************************************************/
780 int v4l2_close (struct v4l2_driver *drv)
781 {
782 v4l2_free_bufs(drv);
783
784 free_list(&drv->stds);
785 free_list(&drv->inputs);
786 free_list(&drv->fmt_caps);
787
788 return (close(drv->fd));
789 }
790
791 /****************************************************************************
792 Get/Set frequency
793 ****************************************************************************/
794
795 int v4l2_getset_freq (struct v4l2_driver *drv, enum v4l2_direction dir,
796 double *freq)
797 {
798 struct v4l2_tuner tun;
799 struct v4l2_frequency frq;
800 double d = 62500;
801
802 memset(&tun, 0, sizeof(tun));
803
804 if (-1 == xioctl (drv->fd, VIDIOC_G_TUNER, &tun)) {
805 perror ("g_tuner");
806 printf("Assuming 62.5 kHz step\n");
807 } else {
808 if (tun.capability & V4L2_TUNER_CAP_LOW)
809 d=62.5;
810 }
811
812 if (drv->debug) {
813 if (tun.capability & V4L2_TUNER_CAP_LOW)
814 printf("62.5 Hz step\n");
815 else
816 printf("62.5 kHz step\n");
817 }
818
819 memset(&frq, 0, sizeof(frq));
820
821 frq.type = V4L2_TUNER_ANALOG_TV;
822
823 if (dir & V4L2_GET) {
824 if (-1 == xioctl (drv->fd, VIDIOC_G_FREQUENCY, &frq)) {
825 perror ("s_frequency");
826 return errno;
827 }
828 *freq = frq.frequency * d;
829 if (drv->debug)
830 printf("board is at freq %4.3f MHz (%d)\n",
831 *freq/1000000, frq.frequency);
832 } else {
833 frq.frequency = (uint32_t)(((*freq)+d/2) / d);
834
835 if (-1 == xioctl (drv->fd, VIDIOC_S_FREQUENCY, &frq)) {
836 perror ("s_frequency");
837 return errno;
838 }
839 if (drv->debug)
840 printf("board set to freq %4.3f MHz (%d)\n",
841 *freq/1000000, frq.frequency);
842
843 }
844 return 0;
845 }