2.6. V4L2 檔案控制代碼

struct v4l2_fh 提供了一種輕鬆保留 V4L2 框架使用的檔案控制代碼特定資料的方法。

注意

新的驅動程式必須使用 struct v4l2_fh,因為它也用於實現優先順序處理(ioctl VIDIOC_G_PRIORITY, VIDIOC_S_PRIORITY)。

v4l2_fh 的使用者(在 V4L2 框架中,而不是驅動程式中)透過測試 video_device->flags 中的 V4L2_FL_USES_V4L2_FH 位來了解驅動程式是否將 v4l2_fh 用作其 file->private_data 指標。每當呼叫 v4l2_fh_init() 時,都會設定此位。

struct v4l2_fh 作為驅動程式自己的檔案控制代碼結構的一部分分配,並且 file->private_data 在驅動程式的 open() 函式中由驅動程式設定為它。

在許多情況下,struct v4l2_fh 將嵌入到更大的結構中。 在這種情況下,您應該在 open() 中呼叫

  1. v4l2_fh_init()v4l2_fh_add()

  2. release() 中呼叫 v4l2_fh_del()v4l2_fh_exit()

驅動程式可以使用 container_of 宏提取自己的檔案控制代碼結構。

示例

struct my_fh {
        int blah;
        struct v4l2_fh fh;
};

...

int my_open(struct file *file)
{
        struct my_fh *my_fh;
        struct video_device *vfd;
        int ret;

        ...

        my_fh = kzalloc(sizeof(*my_fh), GFP_KERNEL);

        ...

        v4l2_fh_init(&my_fh->fh, vfd);

        ...

        file->private_data = &my_fh->fh;
        v4l2_fh_add(&my_fh->fh);
        return 0;
}

int my_release(struct file *file)
{
        struct v4l2_fh *fh = file->private_data;
        struct my_fh *my_fh = container_of(fh, struct my_fh, fh);

        ...
        v4l2_fh_del(&my_fh->fh);
        v4l2_fh_exit(&my_fh->fh);
        kfree(my_fh);
        return 0;
}

下面是對使用的 v4l2_fh 函式的簡短描述

v4l2_fh_init (fh, vdev)

  • 初始化檔案控制代碼。 這必須在驅動程式的 v4l2_file_operations->open() 處理程式中執行。

v4l2_fh_add (fh)

  • v4l2_fh 新增到 video_device 檔案控制代碼列表。 必須在檔案控制代碼完全初始化後呼叫。

v4l2_fh_del (fh)

  • video_device 取消關聯檔案控制代碼。 現在可以呼叫檔案控制代碼退出函式。

v4l2_fh_exit (fh)

  • 取消初始化檔案控制代碼。 取消初始化後,可以釋放 v4l2_fh 記憶體。

如果未嵌入 struct v4l2_fh,則可以使用這些輔助函式

v4l2_fh_open (struct file *filp)

v4l2_fh_release (struct file *filp)

這兩個函式可以插入到 v4l2_file_operation 的 open()release() 操作中。

當第一個檔案控制代碼開啟和最後一個檔案控制代碼關閉時,一些驅動程式需要執行某些操作。 添加了兩個輔助函式來檢查 v4l2_fh 結構是否是關聯裝置節點的唯一開啟的檔案控制代碼

v4l2_fh_is_singular (fh)

  • 如果檔案控制代碼是唯一開啟的檔案控制代碼,則返回 1,否則返回 0。

v4l2_fh_is_singular_file (struct file *filp)

  • 相同,但它使用 filp->private_data 呼叫 v4l2_fh_is_singular。

2.6.1. V4L2 fh 函式和資料結構

struct v4l2_fh

描述 V4L2 檔案控制代碼

定義:

struct v4l2_fh {
    struct list_head        list;
    struct video_device     *vdev;
    struct v4l2_ctrl_handler *ctrl_handler;
    enum v4l2_priority      prio;
    wait_queue_head_t wait;
    struct mutex            subscribe_lock;
    struct list_head        subscribed;
    struct list_head        available;
    unsigned int            navailable;
    u32 sequence;
    struct v4l2_m2m_ctx     *m2m_ctx;
};

成員

list

檔案控制代碼列表

vdev

指向 struct video_device 的指標

ctrl_handler

指向 struct v4l2_ctrl_handler 的指標

prio

檔案控制代碼的優先順序,如 enum v4l2_priority 定義

wait

事件的等待佇列

subscribe_lock

序列化已訂閱列表的更改; 保證按順序呼叫新增和刪除事件回撥

subscribed

已訂閱事件列表

available

等待出隊的事件列表

navailable

available 列表中可用事件的數量

sequence

事件序列號

m2m_ctx

指向 struct v4l2_m2m_ctx 的指標

void v4l2_fh_init(struct v4l2_fh *fh, struct video_device *vdev)

初始化檔案控制代碼。

引數

struct v4l2_fh *fh

指向 struct v4l2_fh 的指標

struct video_device *vdev

指向 struct video_device 的指標

描述

應在此函式中初始化使用檔案控制代碼的 V4L2 框架的各個部分。 如果驅動程式使用 struct v4l2_fh,則必須從驅動程式的 v4l2_file_operations->open() 處理程式中呼叫。

void v4l2_fh_add(struct v4l2_fh *fh)

將 fh 新增到影片裝置上的檔案控制代碼列表。

引數

struct v4l2_fh *fh

指向 struct v4l2_fh 的指標

描述

注意

必須首先初始化 fh 檔案控制代碼。

int v4l2_fh_open(struct file *filp)

可以用作 v4l2_file_operations 的 open() 操作的輔助例程。

引數

struct file *filp

指向 struct file 的指標

描述

它分配一個 v4l2_fh 並初始化它並將其新增到與檔案指標關聯的 struct video_device

void v4l2_fh_del(struct v4l2_fh *fh)

從檔案控制代碼列表中刪除檔案控制代碼。

引數

struct v4l2_fh *fh

指向 struct v4l2_fh 的指標

描述

發生錯誤時,filp->private_data 將為 NULL,否則它將指向 struct v4l2_fh

注意

如果驅動程式使用 struct v4l2_fh,則必須在 v4l2_file_operations->release() 處理程式中呼叫。

void v4l2_fh_exit(struct v4l2_fh *fh)

釋放與檔案控制代碼相關的資源。

引數

struct v4l2_fh *fh

指向 struct v4l2_fh 的指標

描述

使用 v4l2_fh 的 V4L2 框架的各個部分也必須在此處釋放其資源。

注意

如果驅動程式使用 struct v4l2_fh,則必須在 v4l2_file_operations->release() 處理程式中呼叫。

int v4l2_fh_release(struct file *filp)

可以用作 v4l2_file_operations 的 release() 操作的輔助例程。

引數

struct file *filp

指向 struct file 的指標

描述

它刪除並退出與檔案指標關聯的 v4l2_fh 並釋放它。 如果 filp->private_data(指向 v4l2_fh 結構的指標)為 NULL,它將不執行任何操作。

此函式始終返回 0。

int v4l2_fh_is_singular(struct v4l2_fh *fh)

如果此檔案控制代碼是為關聯的影片裝置開啟的唯一檔案控制代碼,則返回 1。

引數

struct v4l2_fh *fh

指向 struct v4l2_fh 的指標

描述

如果 fh 為 NULL,則返回 0。

int v4l2_fh_is_singular_file(struct file *filp)

如果此檔案控制代碼是為關聯的影片裝置開啟的唯一檔案控制代碼,則返回 1。

引數

struct file *filp

指向 struct file 的指標

描述

這是 v4l2_fh_is_singular() 的輔助函式變體,它使用 struct file 作為引數。

如果 filp->private_data 為 NULL,則它將返回 0。