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() 中呼叫
在
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新增到video_device檔案控制代碼列表。 必須在檔案控制代碼完全初始化後呼叫。
從
video_device取消關聯檔案控制代碼。 現在可以呼叫檔案控制代碼退出函式。
取消初始化檔案控制代碼。 取消初始化後,可以釋放
v4l2_fh記憶體。
如果未嵌入 struct v4l2_fh,則可以使用這些輔助函式
v4l2_fh_open (struct file *filp)
這將分配一個
struct v4l2_fh,對其進行初始化並將其新增到與檔案結構關聯的struct video_device。
v4l2_fh_release (struct file *filp)
這會從與檔案結構關聯的
struct video_device中刪除它,取消初始化v4l2_fh並釋放它。
這兩個函式可以插入到 v4l2_file_operation 的 open() 和 release() 操作中。
當第一個檔案控制代碼開啟和最後一個檔案控制代碼關閉時,一些驅動程式需要執行某些操作。 添加了兩個輔助函式來檢查 v4l2_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等待出隊的事件列表
navailableavailable 列表中可用事件的數量
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() 處理程式中呼叫。
引數
struct v4l2_fh *fh指向
struct v4l2_fh的指標
描述
發生錯誤時,filp->private_data 將為 NULL,否則它將指向 struct v4l2_fh。
注意
如果驅動程式使用 struct v4l2_fh,則必須在 v4l2_file_operations->release() 處理程式中呼叫。
引數
struct v4l2_fh *fh指向
struct v4l2_fh的指標
描述
使用 v4l2_fh 的 V4L2 框架的各個部分也必須在此處釋放其資源。
注意
如果驅動程式使用 struct v4l2_fh,則必須在 v4l2_file_operations->release() 處理程式中呼叫。
引數
struct file *filp指向
struct file的指標
描述
它刪除並退出與檔案指標關聯的 v4l2_fh 並釋放它。 如果 filp->private_data(指向 v4l2_fh 結構的指標)為 NULL,它將不執行任何操作。
此函式始終返回 0。
引數
struct file *filp指向
struct file的指標
描述
這是 v4l2_fh_is_singular() 的輔助函式變體,它使用 struct file 作為引數。
如果 filp->private_data 為 NULL,則它將返回 0。