aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/video4linux/v4l2-framework.txt
diff options
context:
space:
mode:
Diffstat (limited to 'Documentation/video4linux/v4l2-framework.txt')
-rw-r--r--Documentation/video4linux/v4l2-framework.txt249
1 files changed, 147 insertions, 102 deletions
diff --git a/Documentation/video4linux/v4l2-framework.txt b/Documentation/video4linux/v4l2-framework.txt
index 74d677c8b036..e831aaca66f8 100644
--- a/Documentation/video4linux/v4l2-framework.txt
+++ b/Documentation/video4linux/v4l2-framework.txt
@@ -545,12 +545,11 @@ unregister them:
545This will remove the device nodes from sysfs (causing udev to remove them 545This will remove the device nodes from sysfs (causing udev to remove them
546from /dev). 546from /dev).
547 547
548After video_unregister_device() returns no new opens can be done. 548After video_unregister_device() returns no new opens can be done. However,
549 549in the case of USB devices some application might still have one of these
550However, in the case of USB devices some application might still have one 550device nodes open. So after the unregister all file operations will return
551of these device nodes open. You should block all new accesses to read, 551an error as well, except for the ioctl and unlocked_ioctl file operations:
552write, poll, etc. except possibly for certain ioctl operations like 552those will still be passed on since some buffer ioctls may still be needed.
553queueing buffers.
554 553
555When the last user of the video device node exits, then the vdev->release() 554When the last user of the video device node exits, then the vdev->release()
556callback is called and you can do the final cleanup there. 555callback is called and you can do the final cleanup there.
@@ -599,99 +598,145 @@ video_device::minor fields.
599video buffer helper functions 598video buffer helper functions
600----------------------------- 599-----------------------------
601 600
602The v4l2 core API provides a standard method for dealing with video 601The v4l2 core API provides a set of standard methods (called "videobuf")
603buffers. Those methods allow a driver to implement read(), mmap() and 602for dealing with video buffers. Those methods allow a driver to implement
604overlay() on a consistent way. 603read(), mmap() and overlay() in a consistent way. There are currently
605 604methods for using video buffers on devices that supports DMA with
606There are currently methods for using video buffers on devices that 605scatter/gather method (videobuf-dma-sg), DMA with linear access
607supports DMA with scatter/gather method (videobuf-dma-sg), DMA with 606(videobuf-dma-contig), and vmalloced buffers, mostly used on USB drivers
608linear access (videobuf-dma-contig), and vmalloced buffers, mostly 607(videobuf-vmalloc).
609used on USB drivers (videobuf-vmalloc). 608
610 609Please see Documentation/video4linux/videobuf for more information on how
611Any driver using videobuf should provide operations (callbacks) for 610to use the videobuf layer.
612four handlers: 611
613 612struct v4l2_fh
614ops->buf_setup - calculates the size of the video buffers and avoid they 613--------------
615 to waste more than some maximum limit of RAM; 614
616ops->buf_prepare - fills the video buffer structs and calls 615struct v4l2_fh provides a way to easily keep file handle specific data
617 videobuf_iolock() to alloc and prepare mmaped memory; 616that is used by the V4L2 framework. Using v4l2_fh is optional for
618ops->buf_queue - advices the driver that another buffer were 617drivers.
619 requested (by read() or by QBUF); 618
620ops->buf_release - frees any buffer that were allocated. 619The users of v4l2_fh (in the V4L2 framework, not the driver) know
621 620whether a driver uses v4l2_fh as its file->private_data pointer by
622In order to use it, the driver need to have a code (generally called at 621testing the V4L2_FL_USES_V4L2_FH bit in video_device->flags.
623interrupt context) that will properly handle the buffer request lists, 622
624announcing that a new buffer were filled. 623Useful functions:
625 624
626The irq handling code should handle the videobuf task lists, in order 625- v4l2_fh_init()
627to advice videobuf that a new frame were filled, in order to honor to a 626
628request. The code is generally like this one: 627 Initialise the file handle. This *MUST* be performed in the driver's
629 if (list_empty(&dma_q->active)) 628 v4l2_file_operations->open() handler.
630 return; 629
631 630- v4l2_fh_add()
632 buf = list_entry(dma_q->active.next, struct vbuffer, vb.queue); 631
633 632 Add a v4l2_fh to video_device file handle list. May be called after
634 if (!waitqueue_active(&buf->vb.done)) 633 initialising the file handle.
635 return; 634
636 635- v4l2_fh_del()
637 /* Some logic to handle the buf may be needed here */ 636
638 637 Unassociate the file handle from video_device(). The file handle
639 list_del(&buf->vb.queue); 638 exit function may now be called.
640 do_gettimeofday(&buf->vb.ts); 639
641 wake_up(&buf->vb.done); 640- v4l2_fh_exit()
642 641
643Those are the videobuffer functions used on drivers, implemented on 642 Uninitialise the file handle. After uninitialisation the v4l2_fh
644videobuf-core: 643 memory can be freed.
645 644
646- Videobuf init functions 645struct v4l2_fh is allocated as a part of the driver's own file handle
647 videobuf_queue_sg_init() 646structure and is set to file->private_data in the driver's open
648 Initializes the videobuf infrastructure. This function should be 647function by the driver. Drivers can extract their own file handle
649 called before any other videobuf function on drivers that uses DMA 648structure by using the container_of macro. Example:
650 Scatter/Gather buffers. 649
651 650struct my_fh {
652 videobuf_queue_dma_contig_init 651 int blah;
653 Initializes the videobuf infrastructure. This function should be 652 struct v4l2_fh fh;
654 called before any other videobuf function on drivers that need DMA 653};
655 contiguous buffers. 654
656 655...
657 videobuf_queue_vmalloc_init() 656
658 Initializes the videobuf infrastructure. This function should be 657int my_open(struct file *file)
659 called before any other videobuf function on USB (and other drivers) 658{
660 that need a vmalloced type of videobuf. 659 struct my_fh *my_fh;
661 660 struct video_device *vfd;
662- videobuf_iolock() 661 int ret;
663 Prepares the videobuf memory for the proper method (read, mmap, overlay). 662
664 663 ...
665- videobuf_queue_is_busy() 664
666 Checks if a videobuf is streaming. 665 ret = v4l2_fh_init(&my_fh->fh, vfd);
667 666 if (ret)
668- videobuf_queue_cancel() 667 return ret;
669 Stops video handling. 668
670 669 v4l2_fh_add(&my_fh->fh);
671- videobuf_mmap_free() 670
672 frees mmap buffers. 671 file->private_data = &my_fh->fh;
673 672
674- videobuf_stop() 673 ...
675 Stops video handling, ends mmap and frees mmap and other buffers. 674}
676 675
677- V4L2 api functions. Those functions correspond to VIDIOC_foo ioctls: 676int my_release(struct file *file)
678 videobuf_reqbufs(), videobuf_querybuf(), videobuf_qbuf(), 677{
679 videobuf_dqbuf(), videobuf_streamon(), videobuf_streamoff(). 678 struct v4l2_fh *fh = file->private_data;
680 679 struct my_fh *my_fh = container_of(fh, struct my_fh, fh);
681- V4L1 api function (corresponds to VIDIOCMBUF ioctl): 680
682 videobuf_cgmbuf() 681 ...
683 This function is used to provide backward compatibility with V4L1 682}
684 API. 683
685 684V4L2 events
686- Some help functions for read()/poll() operations: 685-----------
687 videobuf_read_stream() 686
688 For continuous stream read() 687The V4L2 events provide a generic way to pass events to user space.
689 videobuf_read_one() 688The driver must use v4l2_fh to be able to support V4L2 events.
690 For snapshot read() 689
691 videobuf_poll_stream() 690Useful functions:
692 polling help function 691
693 692- v4l2_event_alloc()
694The better way to understand it is to take a look at vivi driver. One 693
695of the main reasons for vivi is to be a videobuf usage example. the 694 To use events, the driver must allocate events for the file handle. By
696vivi_thread_tick() does the task that the IRQ callback would do on PCI 695 calling the function more than once, the driver may assure that at least n
697drivers (or the irq callback on USB). 696 events in total have been allocated. The function may not be called in
697 atomic context.
698
699- v4l2_event_queue()
700
701 Queue events to video device. The driver's only responsibility is to fill
702 in the type and the data fields. The other fields will be filled in by
703 V4L2.
704
705- v4l2_event_subscribe()
706
707 The video_device->ioctl_ops->vidioc_subscribe_event must check the driver
708 is able to produce events with specified event id. Then it calls
709 v4l2_event_subscribe() to subscribe the event.
710
711- v4l2_event_unsubscribe()
712
713 vidioc_unsubscribe_event in struct v4l2_ioctl_ops. A driver may use
714 v4l2_event_unsubscribe() directly unless it wants to be involved in
715 unsubscription process.
716
717 The special type V4L2_EVENT_ALL may be used to unsubscribe all events. The
718 drivers may want to handle this in a special way.
719
720- v4l2_event_pending()
721
722 Returns the number of pending events. Useful when implementing poll.
723
724Drivers do not initialise events directly. The events are initialised
725through v4l2_fh_init() if video_device->ioctl_ops->vidioc_subscribe_event is
726non-NULL. This *MUST* be performed in the driver's
727v4l2_file_operations->open() handler.
728
729Events are delivered to user space through the poll system call. The driver
730can use v4l2_fh->events->wait wait_queue_head_t as the argument for
731poll_wait().
732
733There are standard and private events. New standard events must use the
734smallest available event type. The drivers must allocate their events from
735their own class starting from class base. Class base is
736V4L2_EVENT_PRIVATE_START + n * 1000 where n is the lowest available number.
737The first event type in the class is reserved for future use, so the first
738available event type is 'class base + 1'.
739
740An example on how the V4L2 events may be used can be found in the OMAP
7413 ISP driver available at <URL:http://gitorious.org/omap3camera> as of
742writing this.