1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * vsp1_video.c -- R-Car VSP1 Video Node
4 *
5 * Copyright (C) 2013-2015 Renesas Electronics Corporation
6 *
7 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8 */
9
10 #include <linux/list.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/slab.h>
14 #include <linux/v4l2-mediabus.h>
15 #include <linux/videodev2.h>
16 #include <linux/wait.h>
17
18 #include <media/media-entity.h>
19 #include <media/v4l2-dev.h>
20 #include <media/v4l2-fh.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/v4l2-subdev.h>
23 #include <media/videobuf2-v4l2.h>
24 #include <media/videobuf2-dma-contig.h>
25
26 #include "vsp1.h"
27 #include "vsp1_brx.h"
28 #include "vsp1_dl.h"
29 #include "vsp1_entity.h"
30 #include "vsp1_hgo.h"
31 #include "vsp1_hgt.h"
32 #include "vsp1_pipe.h"
33 #include "vsp1_rwpf.h"
34 #include "vsp1_uds.h"
35 #include "vsp1_video.h"
36
37 #define VSP1_VIDEO_DEF_FORMAT V4L2_PIX_FMT_YUYV
38 #define VSP1_VIDEO_DEF_WIDTH 1024
39 #define VSP1_VIDEO_DEF_HEIGHT 768
40
41 #define VSP1_VIDEO_MAX_WIDTH 8190U
42 #define VSP1_VIDEO_MAX_HEIGHT 8190U
43
44 /* -----------------------------------------------------------------------------
45 * Helper functions
46 */
47
48 static struct v4l2_subdev *
vsp1_video_remote_subdev(struct media_pad * local,u32 * pad)49 vsp1_video_remote_subdev(struct media_pad *local, u32 *pad)
50 {
51 struct media_pad *remote;
52
53 remote = media_pad_remote_pad_first(local);
54 if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
55 return NULL;
56
57 if (pad)
58 *pad = remote->index;
59
60 return media_entity_to_v4l2_subdev(remote->entity);
61 }
62
vsp1_video_verify_format(struct vsp1_video * video)63 static int vsp1_video_verify_format(struct vsp1_video *video)
64 {
65 struct v4l2_subdev_format fmt = {
66 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
67 };
68 struct v4l2_subdev *subdev;
69 int ret;
70
71 subdev = vsp1_video_remote_subdev(&video->pad, &fmt.pad);
72 if (subdev == NULL)
73 return -EINVAL;
74
75 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
76 if (ret < 0)
77 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
78
79 if (video->rwpf->fmtinfo->mbus != fmt.format.code ||
80 video->rwpf->format.height != fmt.format.height ||
81 video->rwpf->format.width != fmt.format.width) {
82 dev_dbg(video->vsp1->dev,
83 "Format mismatch: 0x%04x/%ux%u != 0x%04x/%ux%u\n",
84 video->rwpf->fmtinfo->mbus, video->rwpf->format.width,
85 video->rwpf->format.height, fmt.format.code,
86 fmt.format.width, fmt.format.height);
87 return -EPIPE;
88 }
89
90 return 0;
91 }
92
__vsp1_video_try_format(struct vsp1_video * video,struct v4l2_pix_format_mplane * pix,const struct vsp1_format_info ** fmtinfo)93 static int __vsp1_video_try_format(struct vsp1_video *video,
94 struct v4l2_pix_format_mplane *pix,
95 const struct vsp1_format_info **fmtinfo)
96 {
97 static const u32 xrgb_formats[][2] = {
98 { V4L2_PIX_FMT_RGB444, V4L2_PIX_FMT_XRGB444 },
99 { V4L2_PIX_FMT_RGB555, V4L2_PIX_FMT_XRGB555 },
100 { V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_XBGR32 },
101 { V4L2_PIX_FMT_RGB32, V4L2_PIX_FMT_XRGB32 },
102 };
103
104 const struct vsp1_format_info *info;
105 unsigned int width = pix->width;
106 unsigned int height = pix->height;
107 unsigned int i;
108
109 /*
110 * Backward compatibility: replace deprecated RGB formats by their XRGB
111 * equivalent. This selects the format older userspace applications want
112 * while still exposing the new format.
113 */
114 for (i = 0; i < ARRAY_SIZE(xrgb_formats); ++i) {
115 if (xrgb_formats[i][0] == pix->pixelformat) {
116 pix->pixelformat = xrgb_formats[i][1];
117 break;
118 }
119 }
120
121 /*
122 * Retrieve format information and select the default format if the
123 * requested format isn't supported.
124 */
125 info = vsp1_get_format_info(video->vsp1, pix->pixelformat);
126 if (info == NULL)
127 info = vsp1_get_format_info(video->vsp1, VSP1_VIDEO_DEF_FORMAT);
128
129 pix->pixelformat = info->fourcc;
130 pix->field = V4L2_FIELD_NONE;
131
132 /*
133 * Adjust the colour space fields. On capture devices, userspace needs
134 * to set the V4L2_PIX_FMT_FLAG_SET_CSC to override the defaults. Reset
135 * all fields to *_DEFAULT if the flag isn't set, to then handle
136 * capture and output devices in the same way.
137 */
138 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
139 !(pix->flags & V4L2_PIX_FMT_FLAG_SET_CSC)) {
140 pix->colorspace = V4L2_COLORSPACE_DEFAULT;
141 pix->xfer_func = V4L2_XFER_FUNC_DEFAULT;
142 pix->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
143 pix->quantization = V4L2_QUANTIZATION_DEFAULT;
144 }
145
146 vsp1_adjust_color_space(info->mbus, &pix->colorspace, &pix->xfer_func,
147 &pix->ycbcr_enc, &pix->quantization);
148
149 memset(pix->reserved, 0, sizeof(pix->reserved));
150
151 /* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */
152 width = round_down(width, info->hsub);
153 height = round_down(height, info->vsub);
154
155 /* Clamp the width and height. */
156 pix->width = clamp(width, info->hsub, VSP1_VIDEO_MAX_WIDTH);
157 pix->height = clamp(height, info->vsub, VSP1_VIDEO_MAX_HEIGHT);
158
159 /*
160 * Compute and clamp the stride and image size. While not documented in
161 * the datasheet, strides not aligned to a multiple of 128 bytes result
162 * in image corruption.
163 */
164 for (i = 0; i < min(info->planes, 2U); ++i) {
165 unsigned int hsub = i > 0 ? info->hsub : 1;
166 unsigned int vsub = i > 0 ? info->vsub : 1;
167 unsigned int align = 128;
168 unsigned int bpl;
169
170 bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline,
171 pix->width / hsub * info->bpp[i] / 8,
172 round_down(65535U, align));
173
174 pix->plane_fmt[i].bytesperline = round_up(bpl, align);
175 pix->plane_fmt[i].sizeimage = pix->plane_fmt[i].bytesperline
176 * pix->height / vsub;
177 }
178
179 if (info->planes == 3) {
180 /* The second and third planes must have the same stride. */
181 pix->plane_fmt[2].bytesperline = pix->plane_fmt[1].bytesperline;
182 pix->plane_fmt[2].sizeimage = pix->plane_fmt[1].sizeimage;
183 }
184
185 pix->num_planes = info->planes;
186
187 if (fmtinfo)
188 *fmtinfo = info;
189
190 return 0;
191 }
192
193 /* -----------------------------------------------------------------------------
194 * Pipeline Management
195 */
196
197 /*
198 * vsp1_video_complete_buffer - Complete the current buffer
199 * @video: the video node
200 *
201 * This function completes the current buffer by filling its sequence number,
202 * time stamp and payload size, and hands it back to the vb2 core.
203 *
204 * Return the next queued buffer or NULL if the queue is empty.
205 */
206 static struct vsp1_vb2_buffer *
vsp1_video_complete_buffer(struct vsp1_video * video)207 vsp1_video_complete_buffer(struct vsp1_video *video)
208 {
209 struct vsp1_pipeline *pipe = video->rwpf->entity.pipe;
210 struct vsp1_vb2_buffer *next = NULL;
211 struct vsp1_vb2_buffer *done;
212 unsigned long flags;
213 unsigned int i;
214
215 spin_lock_irqsave(&video->irqlock, flags);
216
217 if (list_empty(&video->irqqueue)) {
218 spin_unlock_irqrestore(&video->irqlock, flags);
219 return NULL;
220 }
221
222 done = list_first_entry(&video->irqqueue,
223 struct vsp1_vb2_buffer, queue);
224
225 list_del(&done->queue);
226
227 if (!list_empty(&video->irqqueue))
228 next = list_first_entry(&video->irqqueue,
229 struct vsp1_vb2_buffer, queue);
230
231 spin_unlock_irqrestore(&video->irqlock, flags);
232
233 done->buf.sequence = pipe->sequence;
234 done->buf.vb2_buf.timestamp = ktime_get_ns();
235 for (i = 0; i < done->buf.vb2_buf.num_planes; ++i)
236 vb2_set_plane_payload(&done->buf.vb2_buf, i,
237 vb2_plane_size(&done->buf.vb2_buf, i));
238 vb2_buffer_done(&done->buf.vb2_buf, VB2_BUF_STATE_DONE);
239
240 return next;
241 }
242
vsp1_video_frame_end(struct vsp1_pipeline * pipe,struct vsp1_rwpf * rwpf)243 static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
244 struct vsp1_rwpf *rwpf)
245 {
246 struct vsp1_video *video = rwpf->video;
247 struct vsp1_vb2_buffer *buf;
248
249 buf = vsp1_video_complete_buffer(video);
250 if (buf == NULL)
251 return;
252
253 video->rwpf->mem = buf->mem;
254 pipe->buffers_ready |= 1 << video->pipe_index;
255 }
256
vsp1_video_pipeline_run_partition(struct vsp1_pipeline * pipe,struct vsp1_dl_list * dl,unsigned int partition)257 static void vsp1_video_pipeline_run_partition(struct vsp1_pipeline *pipe,
258 struct vsp1_dl_list *dl,
259 unsigned int partition)
260 {
261 struct vsp1_partition *part = &pipe->part_table[partition];
262 struct vsp1_dl_body *dlb = vsp1_dl_list_get_body0(dl);
263 struct vsp1_entity *entity;
264
265 list_for_each_entry(entity, &pipe->entities, list_pipe)
266 vsp1_entity_configure_partition(entity, pipe, part, dl, dlb);
267 }
268
vsp1_video_pipeline_run(struct vsp1_pipeline * pipe)269 static void vsp1_video_pipeline_run(struct vsp1_pipeline *pipe)
270 {
271 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
272 struct vsp1_entity *entity;
273 struct vsp1_dl_body *dlb;
274 struct vsp1_dl_list *dl;
275 unsigned int partition;
276
277 dl = vsp1_dl_list_get(pipe->output->dlm);
278
279 /*
280 * If the VSP hardware isn't configured yet (which occurs either when
281 * processing the first frame or after a system suspend/resume), add the
282 * cached stream configuration to the display list to perform a full
283 * initialisation.
284 */
285 if (!pipe->configured)
286 vsp1_dl_list_add_body(dl, pipe->stream_config);
287
288 dlb = vsp1_dl_list_get_body0(dl);
289
290 list_for_each_entry(entity, &pipe->entities, list_pipe)
291 vsp1_entity_configure_frame(entity, pipe, dl, dlb);
292
293 /* Run the first partition. */
294 vsp1_video_pipeline_run_partition(pipe, dl, 0);
295
296 /* Process consecutive partitions as necessary. */
297 for (partition = 1; partition < pipe->partitions; ++partition) {
298 struct vsp1_dl_list *dl_next;
299
300 dl_next = vsp1_dl_list_get(pipe->output->dlm);
301
302 /*
303 * An incomplete chain will still function, but output only
304 * the partitions that had a dl available. The frame end
305 * interrupt will be marked on the last dl in the chain.
306 */
307 if (!dl_next) {
308 dev_err(vsp1->dev, "Failed to obtain a dl list. Frame will be incomplete\n");
309 break;
310 }
311
312 vsp1_video_pipeline_run_partition(pipe, dl_next, partition);
313 vsp1_dl_list_add_chain(dl, dl_next);
314 }
315
316 /* Complete, and commit the head display list. */
317 vsp1_dl_list_commit(dl, 0);
318 pipe->configured = true;
319
320 vsp1_pipeline_run(pipe);
321 }
322
vsp1_video_pipeline_frame_end(struct vsp1_pipeline * pipe,unsigned int completion)323 static void vsp1_video_pipeline_frame_end(struct vsp1_pipeline *pipe,
324 unsigned int completion)
325 {
326 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
327 enum vsp1_pipeline_state state;
328 unsigned long flags;
329 unsigned int i;
330
331 /* M2M Pipelines should never call here with an incomplete frame. */
332 WARN_ON_ONCE(!(completion & VSP1_DL_FRAME_END_COMPLETED));
333
334 spin_lock_irqsave(&pipe->irqlock, flags);
335
336 /* Complete buffers on all video nodes. */
337 for (i = 0; i < vsp1->info->rpf_count; ++i) {
338 if (!pipe->inputs[i])
339 continue;
340
341 vsp1_video_frame_end(pipe, pipe->inputs[i]);
342 }
343
344 vsp1_video_frame_end(pipe, pipe->output);
345
346 state = pipe->state;
347 pipe->state = VSP1_PIPELINE_STOPPED;
348
349 /*
350 * If a stop has been requested, mark the pipeline as stopped and
351 * return. Otherwise restart the pipeline if ready.
352 */
353 if (state == VSP1_PIPELINE_STOPPING)
354 wake_up(&pipe->wq);
355 else if (vsp1_pipeline_ready(pipe))
356 vsp1_video_pipeline_run(pipe);
357
358 spin_unlock_irqrestore(&pipe->irqlock, flags);
359 }
360
vsp1_video_pipeline_build_branch(struct vsp1_pipeline * pipe,struct vsp1_rwpf * input,struct vsp1_rwpf * output)361 static int vsp1_video_pipeline_build_branch(struct vsp1_pipeline *pipe,
362 struct vsp1_rwpf *input,
363 struct vsp1_rwpf *output)
364 {
365 struct media_entity_enum ent_enum;
366 struct vsp1_entity *entity;
367 struct media_pad *pad;
368 struct vsp1_brx *brx = NULL;
369 int ret;
370
371 ret = media_entity_enum_init(&ent_enum, &input->entity.vsp1->media_dev);
372 if (ret < 0)
373 return ret;
374
375 /*
376 * The main data path doesn't include the HGO or HGT, use
377 * vsp1_entity_remote_pad() to traverse the graph.
378 */
379
380 pad = vsp1_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
381
382 while (1) {
383 if (pad == NULL) {
384 ret = -EPIPE;
385 goto out;
386 }
387
388 /* We've reached a video node, that shouldn't have happened. */
389 if (!is_media_entity_v4l2_subdev(pad->entity)) {
390 ret = -EPIPE;
391 goto out;
392 }
393
394 entity = to_vsp1_entity(
395 media_entity_to_v4l2_subdev(pad->entity));
396
397 /*
398 * A BRU or BRS is present in the pipeline, store its input pad
399 * number in the input RPF for use when configuring the RPF.
400 */
401 if (entity->type == VSP1_ENTITY_BRU ||
402 entity->type == VSP1_ENTITY_BRS) {
403 /* BRU and BRS can't be chained. */
404 if (brx) {
405 ret = -EPIPE;
406 goto out;
407 }
408
409 brx = to_brx(&entity->subdev);
410 brx->inputs[pad->index].rpf = input;
411 input->brx_input = pad->index;
412 }
413
414 /* We've reached the WPF, we're done. */
415 if (entity->type == VSP1_ENTITY_WPF)
416 break;
417
418 /* Ensure the branch has no loop. */
419 if (media_entity_enum_test_and_set(&ent_enum,
420 &entity->subdev.entity)) {
421 ret = -EPIPE;
422 goto out;
423 }
424
425 /* UDS can't be chained. */
426 if (entity->type == VSP1_ENTITY_UDS) {
427 if (pipe->uds) {
428 ret = -EPIPE;
429 goto out;
430 }
431
432 pipe->uds = entity;
433 pipe->uds_input = brx ? &brx->entity : &input->entity;
434 }
435
436 /* Follow the source link, ignoring any HGO or HGT. */
437 pad = &entity->pads[entity->source_pad];
438 pad = vsp1_entity_remote_pad(pad);
439 }
440
441 /* The last entity must be the output WPF. */
442 if (entity != &output->entity)
443 ret = -EPIPE;
444
445 out:
446 media_entity_enum_cleanup(&ent_enum);
447
448 return ret;
449 }
450
vsp1_video_pipeline_build(struct vsp1_pipeline * pipe,struct vsp1_video * video)451 static int vsp1_video_pipeline_build(struct vsp1_pipeline *pipe,
452 struct vsp1_video *video)
453 {
454 struct media_graph graph;
455 struct media_entity *entity = &video->video.entity;
456 struct media_device *mdev = entity->graph_obj.mdev;
457 unsigned int i;
458 int ret;
459
460 /* Walk the graph to locate the entities and video nodes. */
461 ret = media_graph_walk_init(&graph, mdev);
462 if (ret)
463 return ret;
464
465 media_graph_walk_start(&graph, entity);
466
467 while ((entity = media_graph_walk_next(&graph))) {
468 struct v4l2_subdev *subdev;
469 struct vsp1_rwpf *rwpf;
470 struct vsp1_entity *e;
471
472 if (!is_media_entity_v4l2_subdev(entity))
473 continue;
474
475 subdev = media_entity_to_v4l2_subdev(entity);
476 e = to_vsp1_entity(subdev);
477 list_add_tail(&e->list_pipe, &pipe->entities);
478 e->pipe = pipe;
479
480 switch (e->type) {
481 case VSP1_ENTITY_RPF:
482 rwpf = to_rwpf(subdev);
483 pipe->inputs[rwpf->entity.index] = rwpf;
484 rwpf->video->pipe_index = ++pipe->num_inputs;
485 break;
486
487 case VSP1_ENTITY_WPF:
488 rwpf = to_rwpf(subdev);
489 pipe->output = rwpf;
490 rwpf->video->pipe_index = 0;
491 break;
492
493 case VSP1_ENTITY_LIF:
494 pipe->lif = e;
495 break;
496
497 case VSP1_ENTITY_BRU:
498 case VSP1_ENTITY_BRS:
499 pipe->brx = e;
500 break;
501
502 case VSP1_ENTITY_HGO:
503 pipe->hgo = e;
504 break;
505
506 case VSP1_ENTITY_HGT:
507 pipe->hgt = e;
508 break;
509
510 default:
511 break;
512 }
513 }
514
515 media_graph_walk_cleanup(&graph);
516
517 /* We need one output and at least one input. */
518 if (pipe->num_inputs == 0 || !pipe->output)
519 return -EPIPE;
520
521 /*
522 * Follow links downstream for each input and make sure the graph
523 * contains no loop and that all branches end at the output WPF.
524 */
525 for (i = 0; i < video->vsp1->info->rpf_count; ++i) {
526 if (!pipe->inputs[i])
527 continue;
528
529 ret = vsp1_video_pipeline_build_branch(pipe, pipe->inputs[i],
530 pipe->output);
531 if (ret < 0)
532 return ret;
533 }
534
535 return 0;
536 }
537
vsp1_video_pipeline_init(struct vsp1_pipeline * pipe,struct vsp1_video * video)538 static int vsp1_video_pipeline_init(struct vsp1_pipeline *pipe,
539 struct vsp1_video *video)
540 {
541 int ret;
542
543 vsp1_pipeline_init(pipe);
544
545 pipe->frame_end = vsp1_video_pipeline_frame_end;
546
547 ret = vsp1_video_pipeline_build(pipe, video);
548 if (ret)
549 return ret;
550
551 vsp1_pipeline_dump(pipe, "video");
552
553 return 0;
554 }
555
vsp1_video_pipeline_get(struct vsp1_video * video)556 static struct vsp1_pipeline *vsp1_video_pipeline_get(struct vsp1_video *video)
557 {
558 struct vsp1_pipeline *pipe;
559 int ret;
560
561 /*
562 * Get a pipeline object for the video node. If a pipeline has already
563 * been allocated just increment its reference count and return it.
564 * Otherwise allocate a new pipeline and initialize it, it will be freed
565 * when the last reference is released.
566 */
567 if (!video->rwpf->entity.pipe) {
568 pipe = kzalloc_obj(*pipe);
569 if (!pipe)
570 return ERR_PTR(-ENOMEM);
571
572 ret = vsp1_video_pipeline_init(pipe, video);
573 if (ret < 0) {
574 vsp1_pipeline_reset(pipe);
575 kfree(pipe);
576 return ERR_PTR(ret);
577 }
578 } else {
579 pipe = video->rwpf->entity.pipe;
580 kref_get(&pipe->kref);
581 }
582
583 return pipe;
584 }
585
vsp1_video_pipeline_release(struct kref * kref)586 static void vsp1_video_pipeline_release(struct kref *kref)
587 {
588 struct vsp1_pipeline *pipe = container_of(kref, typeof(*pipe), kref);
589
590 vsp1_pipeline_reset(pipe);
591 kfree(pipe);
592 }
593
vsp1_video_pipeline_put(struct vsp1_pipeline * pipe)594 static void vsp1_video_pipeline_put(struct vsp1_pipeline *pipe)
595 {
596 struct media_device *mdev = &pipe->output->entity.vsp1->media_dev;
597
598 mutex_lock(&mdev->graph_mutex);
599 kref_put(&pipe->kref, vsp1_video_pipeline_release);
600 mutex_unlock(&mdev->graph_mutex);
601 }
602
603 /* -----------------------------------------------------------------------------
604 * videobuf2 Queue Operations
605 */
606
607 static int
vsp1_video_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])608 vsp1_video_queue_setup(struct vb2_queue *vq,
609 unsigned int *nbuffers, unsigned int *nplanes,
610 unsigned int sizes[], struct device *alloc_devs[])
611 {
612 struct vsp1_video *video = vb2_get_drv_priv(vq);
613 const struct v4l2_pix_format_mplane *format = &video->rwpf->format;
614 unsigned int i;
615
616 if (*nplanes) {
617 if (*nplanes != format->num_planes)
618 return -EINVAL;
619
620 for (i = 0; i < *nplanes; i++)
621 if (sizes[i] < format->plane_fmt[i].sizeimage)
622 return -EINVAL;
623 return 0;
624 }
625
626 *nplanes = format->num_planes;
627
628 for (i = 0; i < format->num_planes; ++i)
629 sizes[i] = format->plane_fmt[i].sizeimage;
630
631 return 0;
632 }
633
vsp1_video_buffer_prepare(struct vb2_buffer * vb)634 static int vsp1_video_buffer_prepare(struct vb2_buffer *vb)
635 {
636 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
637 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
638 struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf);
639 const struct v4l2_pix_format_mplane *format = &video->rwpf->format;
640 unsigned int i;
641
642 if (vb->num_planes < format->num_planes)
643 return -EINVAL;
644
645 for (i = 0; i < vb->num_planes; ++i) {
646 buf->mem.addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
647
648 if (vb2_plane_size(vb, i) < format->plane_fmt[i].sizeimage)
649 return -EINVAL;
650 }
651
652 for ( ; i < 3; ++i)
653 buf->mem.addr[i] = 0;
654
655 return 0;
656 }
657
vsp1_video_buffer_queue(struct vb2_buffer * vb)658 static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
659 {
660 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
661 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
662 struct vsp1_pipeline *pipe = video->rwpf->entity.pipe;
663 struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf);
664 unsigned long flags;
665 bool empty;
666
667 spin_lock_irqsave(&video->irqlock, flags);
668 empty = list_empty(&video->irqqueue);
669 list_add_tail(&buf->queue, &video->irqqueue);
670 spin_unlock_irqrestore(&video->irqlock, flags);
671
672 if (!empty)
673 return;
674
675 spin_lock_irqsave(&pipe->irqlock, flags);
676
677 video->rwpf->mem = buf->mem;
678 pipe->buffers_ready |= 1 << video->pipe_index;
679
680 if (vb2_start_streaming_called(&video->queue) &&
681 vsp1_pipeline_ready(pipe))
682 vsp1_video_pipeline_run(pipe);
683
684 spin_unlock_irqrestore(&pipe->irqlock, flags);
685 }
686
vsp1_video_pipeline_setup_partitions(struct vsp1_pipeline * pipe)687 static int vsp1_video_pipeline_setup_partitions(struct vsp1_pipeline *pipe)
688 {
689 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
690 const struct v4l2_mbus_framefmt *format;
691 struct vsp1_entity *entity;
692 unsigned int div_size;
693 unsigned int i;
694
695 /*
696 * Partitions are computed on the size before rotation, use the format
697 * at the WPF sink.
698 */
699 format = v4l2_subdev_state_get_format(pipe->output->entity.state,
700 RWPF_PAD_SINK);
701 div_size = format->width;
702
703 /*
704 * Only Gen3+ hardware requires image partitioning, Gen2 will operate
705 * with a single partition that covers the whole output.
706 */
707 if (vsp1->info->gen >= 3) {
708 list_for_each_entry(entity, &pipe->entities, list_pipe) {
709 unsigned int entity_max;
710
711 if (!entity->ops->max_width)
712 continue;
713
714 entity_max = entity->ops->max_width(entity,
715 entity->state,
716 pipe);
717 if (entity_max)
718 div_size = min(div_size, entity_max);
719 }
720 }
721
722 pipe->partitions = DIV_ROUND_UP(format->width, div_size);
723 pipe->part_table = kzalloc_objs(*pipe->part_table, pipe->partitions);
724 if (!pipe->part_table)
725 return -ENOMEM;
726
727 for (i = 0; i < pipe->partitions; ++i)
728 vsp1_pipeline_calculate_partition(pipe, &pipe->part_table[i],
729 div_size, i);
730
731 return 0;
732 }
733
vsp1_video_setup_pipeline(struct vsp1_pipeline * pipe)734 static int vsp1_video_setup_pipeline(struct vsp1_pipeline *pipe)
735 {
736 struct vsp1_entity *entity;
737 int ret;
738
739 /* Determine this pipelines sizes for image partitioning support. */
740 ret = vsp1_video_pipeline_setup_partitions(pipe);
741 if (ret < 0)
742 return ret;
743
744 if (pipe->uds) {
745 struct vsp1_uds *uds = to_uds(&pipe->uds->subdev);
746
747 /*
748 * If a BRU or BRS is present in the pipeline before the UDS,
749 * the alpha component doesn't need to be scaled as the BRU and
750 * BRS output alpha value is fixed to 255. Otherwise we need to
751 * scale the alpha component only when available at the input
752 * RPF.
753 */
754 if (pipe->uds_input->type == VSP1_ENTITY_BRU ||
755 pipe->uds_input->type == VSP1_ENTITY_BRS) {
756 uds->scale_alpha = false;
757 } else {
758 struct vsp1_rwpf *rpf =
759 to_rwpf(&pipe->uds_input->subdev);
760
761 uds->scale_alpha = rpf->fmtinfo->alpha;
762 }
763 }
764
765 /*
766 * Compute and cache the stream configuration into a body. The cached
767 * body will be added to the display list by vsp1_video_pipeline_run()
768 * whenever the pipeline needs to be fully reconfigured.
769 */
770 pipe->stream_config = vsp1_dlm_dl_body_get(pipe->output->dlm);
771 if (!pipe->stream_config)
772 return -ENOMEM;
773
774 list_for_each_entry(entity, &pipe->entities, list_pipe) {
775 vsp1_entity_route_setup(entity, pipe, pipe->stream_config);
776 vsp1_entity_configure_stream(entity, entity->state, pipe, NULL,
777 pipe->stream_config);
778 }
779
780 return 0;
781 }
782
vsp1_video_release_buffers(struct vsp1_video * video)783 static void vsp1_video_release_buffers(struct vsp1_video *video)
784 {
785 struct vsp1_vb2_buffer *buffer;
786 unsigned long flags;
787
788 /* Remove all buffers from the IRQ queue. */
789 spin_lock_irqsave(&video->irqlock, flags);
790 list_for_each_entry(buffer, &video->irqqueue, queue)
791 vb2_buffer_done(&buffer->buf.vb2_buf, VB2_BUF_STATE_ERROR);
792 INIT_LIST_HEAD(&video->irqqueue);
793 spin_unlock_irqrestore(&video->irqlock, flags);
794 }
795
vsp1_video_cleanup_pipeline(struct vsp1_pipeline * pipe)796 static void vsp1_video_cleanup_pipeline(struct vsp1_pipeline *pipe)
797 {
798 lockdep_assert_held(&pipe->lock);
799
800 /* Release any cached configuration from our output video. */
801 vsp1_dl_body_put(pipe->stream_config);
802 pipe->stream_config = NULL;
803 pipe->configured = false;
804
805 /* Release our partition table allocation. */
806 kfree(pipe->part_table);
807 pipe->part_table = NULL;
808 }
809
vsp1_video_start_streaming(struct vb2_queue * vq,unsigned int count)810 static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
811 {
812 struct vsp1_video *video = vb2_get_drv_priv(vq);
813 struct vsp1_pipeline *pipe = video->rwpf->entity.pipe;
814 bool start_pipeline = false;
815 unsigned long flags;
816 int ret;
817
818 mutex_lock(&pipe->lock);
819 if (pipe->stream_count == pipe->num_inputs) {
820 ret = vsp1_video_setup_pipeline(pipe);
821 if (ret < 0) {
822 vsp1_video_release_buffers(video);
823 vsp1_video_cleanup_pipeline(pipe);
824 mutex_unlock(&pipe->lock);
825 return ret;
826 }
827
828 start_pipeline = true;
829 }
830
831 pipe->stream_count++;
832 mutex_unlock(&pipe->lock);
833
834 /*
835 * vsp1_pipeline_ready() is not sufficient to establish that all streams
836 * are prepared and the pipeline is configured, as multiple streams
837 * can race through streamon with buffers already queued; Therefore we
838 * don't even attempt to start the pipeline until the last stream has
839 * called through here.
840 */
841 if (!start_pipeline)
842 return 0;
843
844 spin_lock_irqsave(&pipe->irqlock, flags);
845 if (vsp1_pipeline_ready(pipe))
846 vsp1_video_pipeline_run(pipe);
847 spin_unlock_irqrestore(&pipe->irqlock, flags);
848
849 return 0;
850 }
851
vsp1_video_stop_streaming(struct vb2_queue * vq)852 static void vsp1_video_stop_streaming(struct vb2_queue *vq)
853 {
854 struct vsp1_video *video = vb2_get_drv_priv(vq);
855 struct vsp1_pipeline *pipe = video->rwpf->entity.pipe;
856 unsigned long flags;
857 int ret;
858
859 /*
860 * Clear the buffers ready flag to make sure the device won't be started
861 * by a QBUF on the video node on the other side of the pipeline.
862 */
863 spin_lock_irqsave(&video->irqlock, flags);
864 pipe->buffers_ready &= ~(1 << video->pipe_index);
865 spin_unlock_irqrestore(&video->irqlock, flags);
866
867 mutex_lock(&pipe->lock);
868 if (--pipe->stream_count == pipe->num_inputs) {
869 /* Stop the pipeline. */
870 ret = vsp1_pipeline_stop(pipe);
871 if (ret == -ETIMEDOUT)
872 dev_err(video->vsp1->dev, "pipeline stop timeout\n");
873
874 vsp1_video_cleanup_pipeline(pipe);
875 }
876 mutex_unlock(&pipe->lock);
877
878 video_device_pipeline_stop(&video->video);
879 vsp1_video_release_buffers(video);
880 vsp1_video_pipeline_put(pipe);
881 }
882
883 static const struct vb2_ops vsp1_video_queue_qops = {
884 .queue_setup = vsp1_video_queue_setup,
885 .buf_prepare = vsp1_video_buffer_prepare,
886 .buf_queue = vsp1_video_buffer_queue,
887 .start_streaming = vsp1_video_start_streaming,
888 .stop_streaming = vsp1_video_stop_streaming,
889 };
890
891 /* -----------------------------------------------------------------------------
892 * V4L2 ioctls
893 */
894
895 static int
vsp1_video_querycap(struct file * file,void * fh,struct v4l2_capability * cap)896 vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
897 {
898 struct v4l2_fh *vfh = file_to_v4l2_fh(file);
899 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
900
901 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
902 | V4L2_CAP_IO_MC | V4L2_CAP_VIDEO_CAPTURE_MPLANE
903 | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
904
905 strscpy(cap->driver, "vsp1", sizeof(cap->driver));
906 strscpy(cap->card, video->video.name, sizeof(cap->card));
907
908 return 0;
909 }
910
vsp1_video_enum_format(struct file * file,void * fh,struct v4l2_fmtdesc * f)911 static int vsp1_video_enum_format(struct file *file, void *fh,
912 struct v4l2_fmtdesc *f)
913 {
914 struct v4l2_fh *vfh = file_to_v4l2_fh(file);
915 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
916 const struct vsp1_format_info *info;
917
918 info = vsp1_get_format_info_by_index(video->vsp1, f->index, f->mbus_code);
919 if (!info)
920 return -EINVAL;
921
922 f->pixelformat = info->fourcc;
923
924 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
925 info->mbus == MEDIA_BUS_FMT_AYUV8_1X32)
926 f->flags = V4L2_FMT_FLAG_CSC_YCBCR_ENC
927 | V4L2_FMT_FLAG_CSC_QUANTIZATION;
928
929 return 0;
930 }
931
932 static int
vsp1_video_get_format(struct file * file,void * fh,struct v4l2_format * format)933 vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
934 {
935 struct v4l2_fh *vfh = file_to_v4l2_fh(file);
936 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
937
938 if (format->type != video->queue.type)
939 return -EINVAL;
940
941 mutex_lock(&video->lock);
942 format->fmt.pix_mp = video->rwpf->format;
943 mutex_unlock(&video->lock);
944
945 return 0;
946 }
947
948 static int
vsp1_video_try_format(struct file * file,void * fh,struct v4l2_format * format)949 vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
950 {
951 struct v4l2_fh *vfh = file_to_v4l2_fh(file);
952 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
953
954 if (format->type != video->queue.type)
955 return -EINVAL;
956
957 return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
958 }
959
960 static int
vsp1_video_set_format(struct file * file,void * fh,struct v4l2_format * format)961 vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
962 {
963 struct v4l2_fh *vfh = file_to_v4l2_fh(file);
964 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
965 const struct vsp1_format_info *info;
966 int ret;
967
968 if (format->type != video->queue.type)
969 return -EINVAL;
970
971 ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
972 if (ret < 0)
973 return ret;
974
975 mutex_lock(&video->lock);
976
977 if (vb2_is_busy(&video->queue)) {
978 ret = -EBUSY;
979 goto done;
980 }
981
982 video->rwpf->format = format->fmt.pix_mp;
983 video->rwpf->fmtinfo = info;
984
985 done:
986 mutex_unlock(&video->lock);
987 return ret;
988 }
989
990 static int
vsp1_video_streamon(struct file * file,void * fh,enum v4l2_buf_type type)991 vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
992 {
993 struct v4l2_fh *vfh = file_to_v4l2_fh(file);
994 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
995 struct media_device *mdev = &video->vsp1->media_dev;
996 struct vsp1_pipeline *pipe;
997 int ret;
998
999 if (vb2_queue_is_busy(&video->queue, file))
1000 return -EBUSY;
1001
1002 /*
1003 * Get a pipeline for the video node and start streaming on it. No link
1004 * touching an entity in the pipeline can be activated or deactivated
1005 * once streaming is started.
1006 */
1007 mutex_lock(&mdev->graph_mutex);
1008
1009 pipe = vsp1_video_pipeline_get(video);
1010 if (IS_ERR(pipe)) {
1011 mutex_unlock(&mdev->graph_mutex);
1012 return PTR_ERR(pipe);
1013 }
1014
1015 ret = __video_device_pipeline_start(&video->video, &pipe->pipe);
1016 if (ret < 0) {
1017 mutex_unlock(&mdev->graph_mutex);
1018 goto err_pipe;
1019 }
1020
1021 mutex_unlock(&mdev->graph_mutex);
1022
1023 /*
1024 * Verify that the configured format matches the output of the connected
1025 * subdev.
1026 */
1027 ret = vsp1_video_verify_format(video);
1028 if (ret < 0)
1029 goto err_stop;
1030
1031 /* Start the queue. */
1032 ret = vb2_streamon(&video->queue, type);
1033 if (ret < 0)
1034 goto err_stop;
1035
1036 return 0;
1037
1038 err_stop:
1039 video_device_pipeline_stop(&video->video);
1040 err_pipe:
1041 vsp1_video_pipeline_put(pipe);
1042 return ret;
1043 }
1044
1045 static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
1046 .vidioc_querycap = vsp1_video_querycap,
1047 .vidioc_enum_fmt_vid_cap = vsp1_video_enum_format,
1048 .vidioc_enum_fmt_vid_out = vsp1_video_enum_format,
1049 .vidioc_g_fmt_vid_cap_mplane = vsp1_video_get_format,
1050 .vidioc_s_fmt_vid_cap_mplane = vsp1_video_set_format,
1051 .vidioc_try_fmt_vid_cap_mplane = vsp1_video_try_format,
1052 .vidioc_g_fmt_vid_out_mplane = vsp1_video_get_format,
1053 .vidioc_s_fmt_vid_out_mplane = vsp1_video_set_format,
1054 .vidioc_try_fmt_vid_out_mplane = vsp1_video_try_format,
1055 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1056 .vidioc_querybuf = vb2_ioctl_querybuf,
1057 .vidioc_qbuf = vb2_ioctl_qbuf,
1058 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1059 .vidioc_expbuf = vb2_ioctl_expbuf,
1060 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1061 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1062 .vidioc_streamon = vsp1_video_streamon,
1063 .vidioc_streamoff = vb2_ioctl_streamoff,
1064 };
1065
1066 /* -----------------------------------------------------------------------------
1067 * V4L2 File Operations
1068 */
1069
vsp1_video_open(struct file * file)1070 static int vsp1_video_open(struct file *file)
1071 {
1072 struct vsp1_video *video = video_drvdata(file);
1073 struct v4l2_fh *vfh;
1074 int ret = 0;
1075
1076 vfh = kzalloc_obj(*vfh);
1077 if (vfh == NULL)
1078 return -ENOMEM;
1079
1080 v4l2_fh_init(vfh, &video->video);
1081 v4l2_fh_add(vfh, file);
1082
1083 ret = vsp1_device_get(video->vsp1);
1084 if (ret < 0) {
1085 v4l2_fh_del(vfh, file);
1086 v4l2_fh_exit(vfh);
1087 kfree(vfh);
1088 }
1089
1090 return ret;
1091 }
1092
vsp1_video_release(struct file * file)1093 static int vsp1_video_release(struct file *file)
1094 {
1095 struct vsp1_video *video = video_drvdata(file);
1096
1097 vb2_fop_release(file);
1098
1099 vsp1_device_put(video->vsp1);
1100
1101 return 0;
1102 }
1103
1104 static const struct v4l2_file_operations vsp1_video_fops = {
1105 .owner = THIS_MODULE,
1106 .unlocked_ioctl = video_ioctl2,
1107 .open = vsp1_video_open,
1108 .release = vsp1_video_release,
1109 .poll = vb2_fop_poll,
1110 .mmap = vb2_fop_mmap,
1111 };
1112
1113 /* -----------------------------------------------------------------------------
1114 * Media entity operations
1115 */
1116
vsp1_video_link_validate(struct media_link * link)1117 static int vsp1_video_link_validate(struct media_link *link)
1118 {
1119 /*
1120 * Ideally, link validation should be implemented here instead of
1121 * calling vsp1_video_verify_format() in vsp1_video_streamon()
1122 * manually. That would however break userspace that start one video
1123 * device before configures formats on other video devices in the
1124 * pipeline. This operation is just a no-op to silence the warnings
1125 * from v4l2_subdev_link_validate().
1126 */
1127 return 0;
1128 }
1129
1130 static const struct media_entity_operations vsp1_video_media_ops = {
1131 .link_validate = vsp1_video_link_validate,
1132 };
1133
1134 /* -----------------------------------------------------------------------------
1135 * Suspend and Resume
1136 */
1137
vsp1_video_suspend(struct vsp1_device * vsp1)1138 void vsp1_video_suspend(struct vsp1_device *vsp1)
1139 {
1140 unsigned long flags;
1141 unsigned int i;
1142 int ret;
1143
1144 /*
1145 * To avoid increasing the system suspend time needlessly, loop over the
1146 * pipelines twice, first to set them all to the stopping state, and
1147 * then to wait for the stop to complete.
1148 */
1149 for (i = 0; i < vsp1->info->wpf_count; ++i) {
1150 struct vsp1_rwpf *wpf = vsp1->wpf[i];
1151 struct vsp1_pipeline *pipe;
1152
1153 if (wpf == NULL)
1154 continue;
1155
1156 pipe = wpf->entity.pipe;
1157 if (pipe == NULL)
1158 continue;
1159
1160 spin_lock_irqsave(&pipe->irqlock, flags);
1161 if (pipe->state == VSP1_PIPELINE_RUNNING)
1162 pipe->state = VSP1_PIPELINE_STOPPING;
1163 spin_unlock_irqrestore(&pipe->irqlock, flags);
1164 }
1165
1166 for (i = 0; i < vsp1->info->wpf_count; ++i) {
1167 struct vsp1_rwpf *wpf = vsp1->wpf[i];
1168 struct vsp1_pipeline *pipe;
1169
1170 if (wpf == NULL)
1171 continue;
1172
1173 pipe = wpf->entity.pipe;
1174 if (pipe == NULL)
1175 continue;
1176
1177 ret = wait_event_timeout(pipe->wq, vsp1_pipeline_stopped(pipe),
1178 msecs_to_jiffies(500));
1179 if (ret == 0)
1180 dev_warn(vsp1->dev, "pipeline %u stop timeout\n",
1181 wpf->entity.index);
1182 }
1183 }
1184
vsp1_video_resume(struct vsp1_device * vsp1)1185 void vsp1_video_resume(struct vsp1_device *vsp1)
1186 {
1187 unsigned long flags;
1188 unsigned int i;
1189
1190 /* Resume all running pipelines. */
1191 for (i = 0; i < vsp1->info->wpf_count; ++i) {
1192 struct vsp1_rwpf *wpf = vsp1->wpf[i];
1193 struct vsp1_pipeline *pipe;
1194
1195 if (wpf == NULL)
1196 continue;
1197
1198 pipe = wpf->entity.pipe;
1199 if (pipe == NULL)
1200 continue;
1201
1202 /*
1203 * The hardware may have been reset during a suspend and will
1204 * need a full reconfiguration.
1205 */
1206 pipe->configured = false;
1207
1208 spin_lock_irqsave(&pipe->irqlock, flags);
1209 if (vsp1_pipeline_ready(pipe))
1210 vsp1_video_pipeline_run(pipe);
1211 spin_unlock_irqrestore(&pipe->irqlock, flags);
1212 }
1213 }
1214
1215 /* -----------------------------------------------------------------------------
1216 * Initialization and Cleanup
1217 */
1218
vsp1_video_create(struct vsp1_device * vsp1,struct vsp1_rwpf * rwpf)1219 struct vsp1_video *vsp1_video_create(struct vsp1_device *vsp1,
1220 struct vsp1_rwpf *rwpf)
1221 {
1222 struct vsp1_video *video;
1223 const char *direction;
1224 int ret;
1225
1226 video = devm_kzalloc(vsp1->dev, sizeof(*video), GFP_KERNEL);
1227 if (!video)
1228 return ERR_PTR(-ENOMEM);
1229
1230 rwpf->video = video;
1231
1232 video->vsp1 = vsp1;
1233 video->rwpf = rwpf;
1234
1235 if (rwpf->entity.type == VSP1_ENTITY_RPF) {
1236 direction = "input";
1237 video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1238 video->pad.flags = MEDIA_PAD_FL_SOURCE;
1239 video->video.vfl_dir = VFL_DIR_TX;
1240 video->video.device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE |
1241 V4L2_CAP_STREAMING | V4L2_CAP_IO_MC;
1242 } else {
1243 direction = "output";
1244 video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1245 video->pad.flags = MEDIA_PAD_FL_SINK;
1246 video->video.vfl_dir = VFL_DIR_RX;
1247 video->video.device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
1248 V4L2_CAP_STREAMING | V4L2_CAP_IO_MC;
1249 }
1250
1251 mutex_init(&video->lock);
1252 spin_lock_init(&video->irqlock);
1253 INIT_LIST_HEAD(&video->irqqueue);
1254
1255 /* Initialize the media entity... */
1256 ret = media_entity_pads_init(&video->video.entity, 1, &video->pad);
1257 if (ret < 0)
1258 return ERR_PTR(ret);
1259
1260 /* ... and the format ... */
1261 rwpf->format.pixelformat = VSP1_VIDEO_DEF_FORMAT;
1262 rwpf->format.width = VSP1_VIDEO_DEF_WIDTH;
1263 rwpf->format.height = VSP1_VIDEO_DEF_HEIGHT;
1264 __vsp1_video_try_format(video, &rwpf->format, &rwpf->fmtinfo);
1265
1266 /* ... and the video node... */
1267 video->video.v4l2_dev = &video->vsp1->v4l2_dev;
1268 video->video.entity.ops = &vsp1_video_media_ops;
1269 video->video.fops = &vsp1_video_fops;
1270 snprintf(video->video.name, sizeof(video->video.name), "%s %s",
1271 rwpf->entity.subdev.name, direction);
1272 video->video.vfl_type = VFL_TYPE_VIDEO;
1273 video->video.release = video_device_release_empty;
1274 video->video.ioctl_ops = &vsp1_video_ioctl_ops;
1275
1276 video_set_drvdata(&video->video, video);
1277
1278 video->queue.type = video->type;
1279 video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1280 video->queue.lock = &video->lock;
1281 video->queue.drv_priv = video;
1282 video->queue.buf_struct_size = sizeof(struct vsp1_vb2_buffer);
1283 video->queue.ops = &vsp1_video_queue_qops;
1284 video->queue.mem_ops = &vb2_dma_contig_memops;
1285 video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1286 video->queue.dev = video->vsp1->bus_master;
1287 ret = vb2_queue_init(&video->queue);
1288 if (ret < 0) {
1289 dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
1290 goto error;
1291 }
1292
1293 /* ... and register the video device. */
1294 video->video.queue = &video->queue;
1295 ret = video_register_device(&video->video, VFL_TYPE_VIDEO, -1);
1296 if (ret < 0) {
1297 dev_err(video->vsp1->dev, "failed to register video device\n");
1298 goto error;
1299 }
1300
1301 return video;
1302
1303 error:
1304 vsp1_video_cleanup(video);
1305 return ERR_PTR(ret);
1306 }
1307
vsp1_video_cleanup(struct vsp1_video * video)1308 void vsp1_video_cleanup(struct vsp1_video *video)
1309 {
1310 if (video_is_registered(&video->video))
1311 video_unregister_device(&video->video);
1312
1313 media_entity_cleanup(&video->video.entity);
1314 }
1315