1 // SPDX-License-Identifier: GPL-2.0+
2 /* Driver for virtio video device.
4 * Copyright 2020 OpenSynergy GmbH.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <media/v4l2-ioctl.h>
21 #include <media/videobuf2-dma-sg.h>
23 #include "virtio_video.h"
25 static void virtio_video_free_frame_rates(struct video_format_frame *frame)
27 kfree(frame->frame_rates);
30 static void virtio_video_free_frames(struct video_format *fmt)
34 for (idx = 0; idx < fmt->desc.num_frames; idx++)
35 virtio_video_free_frame_rates(&fmt->frames[idx]);
39 static void virtio_video_free_fmt(struct list_head *fmts_list)
41 struct video_format *fmt, *tmp;
43 list_for_each_entry_safe(fmt, tmp, fmts_list, formats_list_entry) {
44 list_del(&fmt->formats_list_entry);
45 virtio_video_free_frames(fmt);
50 static void virtio_video_free_fmts(struct virtio_video_device *vvd)
52 virtio_video_free_fmt(&vvd->input_fmt_list);
53 virtio_video_free_fmt(&vvd->output_fmt_list);
56 static void virtio_video_copy_fmt_range(struct virtio_video_format_range *d_rge,
57 struct virtio_video_format_range *s_rge)
59 d_rge->min = le32_to_cpu(s_rge->min);
60 d_rge->max = le32_to_cpu(s_rge->max);
61 d_rge->step = le32_to_cpu(s_rge->step);
65 virtio_video_parse_virtio_frame_rate(struct virtio_video_device *vvd,
66 struct virtio_video_format_range *f_rate,
69 struct virtio_video_format_range *virtio_frame_rate;
71 virtio_frame_rate = buf;
72 virtio_video_copy_fmt_range(f_rate, virtio_frame_rate);
74 return sizeof(struct virtio_video_format_range);
77 static size_t virtio_video_parse_virtio_frame(struct virtio_video_device *vvd,
78 struct video_format_frame *frm,
81 struct virtio_video_format_frame *virtio_frame;
82 struct virtio_video_format_frame *frame = &frm->frame;
83 struct virtio_video_format_range *rate;
84 size_t idx, offset, extra_size;
88 virtio_video_copy_fmt_range(&frame->width, &virtio_frame->width);
89 virtio_video_copy_fmt_range(&frame->height, &virtio_frame->height);
91 frame->num_rates = le32_to_cpu(virtio_frame->num_rates);
92 frm->frame_rates = kcalloc(frame->num_rates,
93 sizeof(struct virtio_video_format_range),
96 offset = sizeof(struct virtio_video_format_frame);
97 for (idx = 0; idx < frame->num_rates; idx++) {
98 rate = &frm->frame_rates[idx];
100 virtio_video_parse_virtio_frame_rate(vvd, rate,
102 if (extra_size == 0) {
103 kfree(frm->frame_rates);
104 v4l2_err(&vvd->v4l2_dev,
105 "failed to parse frame rate\n");
108 offset += extra_size;
114 static size_t virtio_video_parse_virtio_fmt(struct virtio_video_device *vvd,
115 struct video_format *fmt, void *buf)
117 struct virtio_video_format_desc *virtio_fmt_desc;
118 struct virtio_video_format_desc *fmt_desc;
119 struct video_format_frame *frame;
120 size_t idx, offset, extra_size;
122 virtio_fmt_desc = buf;
123 fmt_desc = &fmt->desc;
126 virtio_video_format_to_v4l2
127 (le32_to_cpu(virtio_fmt_desc->format));
128 fmt_desc->mask = le64_to_cpu(virtio_fmt_desc->mask);
129 fmt_desc->planes_layout = le32_to_cpu(virtio_fmt_desc->planes_layout);
131 fmt_desc->num_frames = le32_to_cpu(virtio_fmt_desc->num_frames);
132 fmt->frames = kcalloc(fmt_desc->num_frames,
133 sizeof(struct video_format_frame),
136 offset = sizeof(struct virtio_video_format_desc);
137 for (idx = 0; idx < fmt_desc->num_frames; idx++) {
138 frame = &fmt->frames[idx];
140 virtio_video_parse_virtio_frame(vvd, frame,
142 if (extra_size == 0) {
144 v4l2_err(&vvd->v4l2_dev, "failed to parse frame\n");
147 offset += extra_size;
153 int virtio_video_parse_virtio_capability(struct virtio_video_device *vvd,
155 struct list_head *ret_fmt_list,
156 uint32_t *ret_num_fmts)
158 struct virtio_video_query_capability_resp *resp = resp_buf;
159 struct video_format *fmt;
165 if (!resp || ret_fmt_list == NULL || ret_num_fmts == NULL) {
166 v4l2_err(&vvd->v4l2_dev, "invalid arguments!\n");
170 if (le32_to_cpu(resp->num_descs) <= 0) {
171 v4l2_err(&vvd->v4l2_dev, "invalid capability response\n");
175 fmt_count = le32_to_cpu(resp->num_descs);
176 offset = sizeof(struct virtio_video_query_capability_resp);
178 for (fmt_idx = 0; fmt_idx < fmt_count; fmt_idx++) {
181 fmt = kzalloc(sizeof(*fmt), GFP_KERNEL);
187 fmt_size = virtio_video_parse_virtio_fmt(vvd, fmt,
190 v4l2_err(&vvd->v4l2_dev, "failed to parse fmt\n");
195 list_add(&fmt->formats_list_entry, ret_fmt_list);
198 *ret_num_fmts = fmt_count;
204 virtio_video_free_fmts(vvd);
208 int virtio_video_parse_virtio_capabilities(struct virtio_video_device *vvd,
209 void *input_buf, void *output_buf)
214 ret = virtio_video_parse_virtio_capability(vvd, input_buf,
215 &vvd->input_fmt_list,
216 &vvd->num_input_fmts);
218 v4l2_err(&vvd->v4l2_dev,
219 "Failed to parse input capability: %d\n",
226 ret = virtio_video_parse_virtio_capability(vvd, output_buf,
227 &vvd->output_fmt_list,
228 &vvd->num_output_fmts);
230 v4l2_err(&vvd->v4l2_dev,
231 "Failed to parse output capability: %d\n",
240 void virtio_video_clean_capability(struct virtio_video_device *vvd)
242 virtio_video_free_fmts(vvd);
246 virtio_video_free_control_fmt_data(struct video_control_fmt_data *data)
248 kfree(data->entries);
252 static void virtio_video_free_control_formats(struct virtio_video_device *vvd)
254 struct video_control_format *c_fmt, *tmp;
256 list_for_each_entry_safe(c_fmt, tmp, &vvd->controls_fmt_list,
257 controls_list_entry) {
258 list_del(&c_fmt->controls_list_entry);
259 virtio_video_free_control_fmt_data(c_fmt->profile);
260 virtio_video_free_control_fmt_data(c_fmt->level);
265 static int virtio_video_parse_control_levels(struct virtio_video_device *vvd,
266 struct video_control_format *fmt)
269 struct virtio_video_query_control_resp *resp_buf;
270 struct virtio_video_query_control_resp_level *l_resp_buf;
271 struct video_control_fmt_data *level;
272 enum virtio_video_format virtio_format;
273 uint32_t *virtio_levels;
274 uint32_t num_levels, mask = 0;
275 int max = 0, min = UINT_MAX;
278 resp_size = vvd->max_resp_len;
280 virtio_format = virtio_video_v4l2_format_to_virtio(fmt->format);
282 resp_buf = kzalloc(resp_size, GFP_KERNEL);
283 if (IS_ERR(resp_buf)) {
284 ret = PTR_ERR(resp_buf);
288 ret = virtio_video_query_control_level(vvd, resp_buf, resp_size,
291 v4l2_err(&vvd->v4l2_dev, "failed to query level\n");
295 l_resp_buf = (void *)((char *)resp_buf + sizeof(*resp_buf));
296 num_levels = le32_to_cpu(l_resp_buf->num);
300 fmt->level = kzalloc(sizeof(*level), GFP_KERNEL);
307 level->entries = kcalloc(num_levels, sizeof(uint32_t), GFP_KERNEL);
308 if (!level->entries) {
314 virtio_levels = (void *)((char *)l_resp_buf + sizeof(*l_resp_buf));
316 for (idx = 0; idx < num_levels; idx++) {
317 level->entries[idx] =
318 virtio_video_level_to_v4l2
319 (le32_to_cpu(virtio_levels[idx]));
321 mask = mask | (1 << level->entries[idx]);
322 if (level->entries[idx] > max)
323 max = level->entries[idx];
324 if (level->entries[idx] < min)
325 min = level->entries[idx];
329 level->num = num_levels;
330 level->skip_mask = ~mask;
338 static int virtio_video_parse_control_profiles(struct virtio_video_device *vvd,
339 struct video_control_format *fmt)
342 struct virtio_video_query_control_resp *resp_buf;
343 struct virtio_video_query_control_resp_profile *p_resp_buf;
344 struct video_control_fmt_data *profile;
345 uint32_t virtio_format, num_profiles, mask = 0;
346 uint32_t *virtio_profiles;
347 int max = 0, min = UINT_MAX;
350 resp_size = vvd->max_resp_len;
351 virtio_format = virtio_video_v4l2_format_to_virtio(fmt->format);
352 resp_buf = kzalloc(resp_size, GFP_KERNEL);
353 if (IS_ERR(resp_buf)) {
354 ret = PTR_ERR(resp_buf);
358 ret = virtio_video_query_control_profile(vvd, resp_buf, resp_size,
361 v4l2_err(&vvd->v4l2_dev, "failed to query profile\n");
365 p_resp_buf = (void *)((char *)resp_buf + sizeof(*resp_buf));
366 num_profiles = le32_to_cpu(p_resp_buf->num);
367 if (num_profiles == 0)
370 fmt->profile = kzalloc(sizeof(*profile), GFP_KERNEL);
376 profile = fmt->profile;
377 profile->entries = kcalloc(num_profiles, sizeof(uint32_t), GFP_KERNEL);
378 if (!profile->entries) {
384 virtio_profiles = (void *)((char *)p_resp_buf + sizeof(*p_resp_buf));
386 for (idx = 0; idx < num_profiles; idx++) {
387 profile->entries[idx] =
388 virtio_video_profile_to_v4l2
389 (le32_to_cpu(virtio_profiles[idx]));
391 mask = mask | (1 << profile->entries[idx]);
392 if (profile->entries[idx] > max)
393 max = profile->entries[idx];
394 if (profile->entries[idx] < min)
395 min = profile->entries[idx];
399 profile->num = num_profiles;
400 profile->skip_mask = ~mask;
408 int virtio_video_parse_virtio_control(struct virtio_video_device *vvd)
410 struct video_format *fmt;
411 struct video_control_format *c_fmt;
412 uint32_t virtio_format;
415 list_for_each_entry(fmt, &vvd->output_fmt_list, formats_list_entry) {
417 virtio_video_v4l2_format_to_virtio(fmt->desc.format);
418 if (virtio_format < VIRTIO_VIDEO_FORMAT_CODED_MIN ||
419 virtio_format > VIRTIO_VIDEO_FORMAT_CODED_MAX)
422 c_fmt = kzalloc(sizeof(*c_fmt), GFP_KERNEL);
425 goto parse_ctrl_alloc_err;
428 c_fmt->format = fmt->desc.format;
430 ret = virtio_video_parse_control_profiles(vvd, c_fmt);
432 v4l2_err(&vvd->v4l2_dev,
433 "failed to parse control profile\n");
434 goto parse_ctrl_prf_err;
437 ret = virtio_video_parse_control_levels(vvd, c_fmt);
439 v4l2_err(&vvd->v4l2_dev,
440 "failed to parse control level\n");
441 goto parse_ctrl_lvl_err;
443 list_add(&c_fmt->controls_list_entry, &vvd->controls_fmt_list);
448 virtio_video_free_control_fmt_data(c_fmt->profile);
451 parse_ctrl_alloc_err:
452 virtio_video_free_control_formats(vvd);
457 void virtio_video_clean_control(struct virtio_video_device *vvd)
459 virtio_video_free_control_formats(vvd);