diff options
Diffstat (limited to 'Documentation/video4linux/v4l2-framework.txt')
-rw-r--r-- | Documentation/video4linux/v4l2-framework.txt | 249 |
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: | |||
545 | This will remove the device nodes from sysfs (causing udev to remove them | 545 | This will remove the device nodes from sysfs (causing udev to remove them |
546 | from /dev). | 546 | from /dev). |
547 | 547 | ||
548 | After video_unregister_device() returns no new opens can be done. | 548 | After video_unregister_device() returns no new opens can be done. However, |
549 | 549 | in the case of USB devices some application might still have one of these | |
550 | However, in the case of USB devices some application might still have one | 550 | device nodes open. So after the unregister all file operations will return |
551 | of these device nodes open. You should block all new accesses to read, | 551 | an error as well, except for the ioctl and unlocked_ioctl file operations: |
552 | write, poll, etc. except possibly for certain ioctl operations like | 552 | those will still be passed on since some buffer ioctls may still be needed. |
553 | queueing buffers. | ||
554 | 553 | ||
555 | When the last user of the video device node exits, then the vdev->release() | 554 | When the last user of the video device node exits, then the vdev->release() |
556 | callback is called and you can do the final cleanup there. | 555 | callback is called and you can do the final cleanup there. |
@@ -599,99 +598,145 @@ video_device::minor fields. | |||
599 | video buffer helper functions | 598 | video buffer helper functions |
600 | ----------------------------- | 599 | ----------------------------- |
601 | 600 | ||
602 | The v4l2 core API provides a standard method for dealing with video | 601 | The v4l2 core API provides a set of standard methods (called "videobuf") |
603 | buffers. Those methods allow a driver to implement read(), mmap() and | 602 | for dealing with video buffers. Those methods allow a driver to implement |
604 | overlay() on a consistent way. | 603 | read(), mmap() and overlay() in a consistent way. There are currently |
605 | 604 | methods for using video buffers on devices that supports DMA with | |
606 | There are currently methods for using video buffers on devices that | 605 | scatter/gather method (videobuf-dma-sg), DMA with linear access |
607 | supports DMA with scatter/gather method (videobuf-dma-sg), DMA with | 606 | (videobuf-dma-contig), and vmalloced buffers, mostly used on USB drivers |
608 | linear access (videobuf-dma-contig), and vmalloced buffers, mostly | 607 | (videobuf-vmalloc). |
609 | used on USB drivers (videobuf-vmalloc). | 608 | |
610 | 609 | Please see Documentation/video4linux/videobuf for more information on how | |
611 | Any driver using videobuf should provide operations (callbacks) for | 610 | to use the videobuf layer. |
612 | four handlers: | 611 | |
613 | 612 | struct v4l2_fh | |
614 | ops->buf_setup - calculates the size of the video buffers and avoid they | 613 | -------------- |
615 | to waste more than some maximum limit of RAM; | 614 | |
616 | ops->buf_prepare - fills the video buffer structs and calls | 615 | struct v4l2_fh provides a way to easily keep file handle specific data |
617 | videobuf_iolock() to alloc and prepare mmaped memory; | 616 | that is used by the V4L2 framework. Using v4l2_fh is optional for |
618 | ops->buf_queue - advices the driver that another buffer were | 617 | drivers. |
619 | requested (by read() or by QBUF); | 618 | |
620 | ops->buf_release - frees any buffer that were allocated. | 619 | The users of v4l2_fh (in the V4L2 framework, not the driver) know |
621 | 620 | whether a driver uses v4l2_fh as its file->private_data pointer by | |
622 | In order to use it, the driver need to have a code (generally called at | 621 | testing the V4L2_FL_USES_V4L2_FH bit in video_device->flags. |
623 | interrupt context) that will properly handle the buffer request lists, | 622 | |
624 | announcing that a new buffer were filled. | 623 | Useful functions: |
625 | 624 | ||
626 | The irq handling code should handle the videobuf task lists, in order | 625 | - v4l2_fh_init() |
627 | to advice videobuf that a new frame were filled, in order to honor to a | 626 | |
628 | request. 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 | ||
643 | Those are the videobuffer functions used on drivers, implemented on | 642 | Uninitialise the file handle. After uninitialisation the v4l2_fh |
644 | videobuf-core: | 643 | memory can be freed. |
645 | 644 | ||
646 | - Videobuf init functions | 645 | struct v4l2_fh is allocated as a part of the driver's own file handle |
647 | videobuf_queue_sg_init() | 646 | structure and is set to file->private_data in the driver's open |
648 | Initializes the videobuf infrastructure. This function should be | 647 | function by the driver. Drivers can extract their own file handle |
649 | called before any other videobuf function on drivers that uses DMA | 648 | structure by using the container_of macro. Example: |
650 | Scatter/Gather buffers. | 649 | |
651 | 650 | struct 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 | 657 | int 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: | 676 | int 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 | 684 | V4L2 events | |
686 | - Some help functions for read()/poll() operations: | 685 | ----------- |
687 | videobuf_read_stream() | 686 | |
688 | For continuous stream read() | 687 | The V4L2 events provide a generic way to pass events to user space. |
689 | videobuf_read_one() | 688 | The driver must use v4l2_fh to be able to support V4L2 events. |
690 | For snapshot read() | 689 | |
691 | videobuf_poll_stream() | 690 | Useful functions: |
692 | polling help function | 691 | |
693 | 692 | - v4l2_event_alloc() | |
694 | The better way to understand it is to take a look at vivi driver. One | 693 | |
695 | of 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 |
696 | vivi_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 |
697 | drivers (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 | |||
724 | Drivers do not initialise events directly. The events are initialised | ||
725 | through v4l2_fh_init() if video_device->ioctl_ops->vidioc_subscribe_event is | ||
726 | non-NULL. This *MUST* be performed in the driver's | ||
727 | v4l2_file_operations->open() handler. | ||
728 | |||
729 | Events are delivered to user space through the poll system call. The driver | ||
730 | can use v4l2_fh->events->wait wait_queue_head_t as the argument for | ||
731 | poll_wait(). | ||
732 | |||
733 | There are standard and private events. New standard events must use the | ||
734 | smallest available event type. The drivers must allocate their events from | ||
735 | their own class starting from class base. Class base is | ||
736 | V4L2_EVENT_PRIVATE_START + n * 1000 where n is the lowest available number. | ||
737 | The first event type in the class is reserved for future use, so the first | ||
738 | available event type is 'class base + 1'. | ||
739 | |||
740 | An example on how the V4L2 events may be used can be found in the OMAP | ||
741 | 3 ISP driver available at <URL:http://gitorious.org/omap3camera> as of | ||
742 | writing this. | ||