1 /*
2  * Copyright (C) 2012 Texas Instruments Inc
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation version 2.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contributors:
18  *      Manjunath Hadli <manjunath.hadli@ti.com>
19  *      Prabhakar Lad <prabhakar.lad@ti.com>
20  */
21 
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 
25 #include <media/v4l2-ioctl.h>
26 
27 #include "vpfe.h"
28 #include "vpfe_mc_capture.h"
29 
30 /* minimum number of buffers needed in cont-mode */
31 #define MIN_NUM_BUFFERS			3
32 
33 static int debug;
34 
35 /* get v4l2 subdev pointer to external subdev which is active */
vpfe_get_input_entity(struct vpfe_video_device * video)36 static struct media_entity *vpfe_get_input_entity
37 			(struct vpfe_video_device *video)
38 {
39 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
40 	struct media_pad *remote;
41 
42 	remote = media_entity_remote_pad(&vpfe_dev->vpfe_isif.pads[0]);
43 	if (remote == NULL) {
44 		pr_err("Invalid media connection to isif/ccdc\n");
45 		return NULL;
46 	}
47 	return remote->entity;
48 }
49 
50 /* updates external subdev(sensor/decoder) which is active */
vpfe_update_current_ext_subdev(struct vpfe_video_device * video)51 static int vpfe_update_current_ext_subdev(struct vpfe_video_device *video)
52 {
53 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
54 	struct vpfe_config *vpfe_cfg;
55 	struct v4l2_subdev *subdev;
56 	struct media_pad *remote;
57 	int i;
58 
59 	remote = media_entity_remote_pad(&vpfe_dev->vpfe_isif.pads[0]);
60 	if (remote == NULL) {
61 		pr_err("Invalid media connection to isif/ccdc\n");
62 		return -EINVAL;
63 	}
64 
65 	subdev = media_entity_to_v4l2_subdev(remote->entity);
66 	vpfe_cfg = vpfe_dev->pdev->platform_data;
67 	for (i = 0; i < vpfe_cfg->num_subdevs; i++) {
68 		if (!strcmp(vpfe_cfg->sub_devs[i].module_name, subdev->name)) {
69 			video->current_ext_subdev = &vpfe_cfg->sub_devs[i];
70 			break;
71 		}
72 	}
73 
74 	/* if user not linked decoder/sensor to isif/ccdc */
75 	if (i == vpfe_cfg->num_subdevs) {
76 		pr_err("Invalid media chain connection to isif/ccdc\n");
77 		return -EINVAL;
78 	}
79 	/* find the v4l2 subdev pointer */
80 	for (i = 0; i < vpfe_dev->num_ext_subdevs; i++) {
81 		if (!strcmp(video->current_ext_subdev->module_name,
82 			vpfe_dev->sd[i]->name))
83 			video->current_ext_subdev->subdev = vpfe_dev->sd[i];
84 	}
85 	return 0;
86 }
87 
88 /* get the subdev which is connected to the output video node */
89 static struct v4l2_subdev *
vpfe_video_remote_subdev(struct vpfe_video_device * video,u32 * pad)90 vpfe_video_remote_subdev(struct vpfe_video_device *video, u32 *pad)
91 {
92 	struct media_pad *remote = media_entity_remote_pad(&video->pad);
93 
94 	if (remote == NULL || remote->entity->type != MEDIA_ENT_T_V4L2_SUBDEV)
95 		return NULL;
96 	if (pad)
97 		*pad = remote->index;
98 	return media_entity_to_v4l2_subdev(remote->entity);
99 }
100 
101 /* get the format set at output pad of the adjacent subdev */
102 static int
__vpfe_video_get_format(struct vpfe_video_device * video,struct v4l2_format * format)103 __vpfe_video_get_format(struct vpfe_video_device *video,
104 			struct v4l2_format *format)
105 {
106 	struct v4l2_subdev_format fmt;
107 	struct v4l2_subdev *subdev;
108 	struct media_pad *remote;
109 	u32 pad;
110 	int ret;
111 
112 	subdev = vpfe_video_remote_subdev(video, &pad);
113 	if (subdev == NULL)
114 		return -EINVAL;
115 
116 	fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
117 	remote = media_entity_remote_pad(&video->pad);
118 	fmt.pad = remote->index;
119 
120 	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
121 	if (ret == -ENOIOCTLCMD)
122 		return -EINVAL;
123 
124 	format->type = video->type;
125 	/* convert mbus_format to v4l2_format */
126 	v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
127 	mbus_to_pix(&fmt.format, &format->fmt.pix);
128 
129 	return 0;
130 }
131 
132 /* make a note of pipeline details */
vpfe_prepare_pipeline(struct vpfe_video_device * video)133 static void vpfe_prepare_pipeline(struct vpfe_video_device *video)
134 {
135 	struct media_entity *entity = &video->video_dev.entity;
136 	struct media_device *mdev = entity->parent;
137 	struct vpfe_pipeline *pipe = &video->pipe;
138 	struct vpfe_video_device *far_end = NULL;
139 	struct media_entity_graph graph;
140 
141 	pipe->input_num = 0;
142 	pipe->output_num = 0;
143 
144 	if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
145 		pipe->inputs[pipe->input_num++] = video;
146 	else
147 		pipe->outputs[pipe->output_num++] = video;
148 
149 	mutex_lock(&mdev->graph_mutex);
150 	media_entity_graph_walk_start(&graph, entity);
151 	while ((entity = media_entity_graph_walk_next(&graph))) {
152 		if (entity == &video->video_dev.entity)
153 			continue;
154 		if (media_entity_type(entity) != MEDIA_ENT_T_DEVNODE)
155 			continue;
156 		far_end = to_vpfe_video(media_entity_to_video_device(entity));
157 		if (far_end->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
158 			pipe->inputs[pipe->input_num++] = far_end;
159 		else
160 			pipe->outputs[pipe->output_num++] = far_end;
161 	}
162 	mutex_unlock(&mdev->graph_mutex);
163 }
164 
165 /* update pipe state selected by user */
vpfe_update_pipe_state(struct vpfe_video_device * video)166 static int vpfe_update_pipe_state(struct vpfe_video_device *video)
167 {
168 	struct vpfe_pipeline *pipe = &video->pipe;
169 	int ret;
170 
171 	vpfe_prepare_pipeline(video);
172 
173 	/* Find out if there is any input video
174 	  if yes, it is single shot.
175 	*/
176 	if (pipe->input_num == 0) {
177 		pipe->state = VPFE_PIPELINE_STREAM_CONTINUOUS;
178 		ret = vpfe_update_current_ext_subdev(video);
179 		if (ret) {
180 			pr_err("Invalid external subdev\n");
181 			return ret;
182 		}
183 	} else {
184 		pipe->state = VPFE_PIPELINE_STREAM_SINGLESHOT;
185 	}
186 	video->initialized = 1;
187 	video->skip_frame_count = 1;
188 	video->skip_frame_count_init = 1;
189 	return 0;
190 }
191 
192 /* checks wether pipeline is ready for enabling */
vpfe_video_is_pipe_ready(struct vpfe_pipeline * pipe)193 int vpfe_video_is_pipe_ready(struct vpfe_pipeline *pipe)
194 {
195 	int i;
196 
197 	for (i = 0; i < pipe->input_num; i++)
198 		if (!pipe->inputs[i]->started ||
199 			pipe->inputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
200 			return 0;
201 	for (i = 0; i < pipe->output_num; i++)
202 		if (!pipe->outputs[i]->started ||
203 			pipe->outputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
204 			return 0;
205 	return 1;
206 }
207 
208 /**
209  * Validate a pipeline by checking both ends of all links for format
210  * discrepancies.
211  *
212  * Return 0 if all formats match, or -EPIPE if at least one link is found with
213  * different formats on its two ends.
214  */
vpfe_video_validate_pipeline(struct vpfe_pipeline * pipe)215 static int vpfe_video_validate_pipeline(struct vpfe_pipeline *pipe)
216 {
217 	struct v4l2_subdev_format fmt_source;
218 	struct v4l2_subdev_format fmt_sink;
219 	struct v4l2_subdev *subdev;
220 	struct media_pad *pad;
221 	int ret;
222 
223 	/*
224 	 * Should not matter if it is output[0] or 1 as
225 	 * the general ideas is to traverse backwards and
226 	 * the fact that the out video node always has the
227 	 * format of the connected pad.
228 	 */
229 	subdev = vpfe_video_remote_subdev(pipe->outputs[0], NULL);
230 	if (subdev == NULL)
231 		return -EPIPE;
232 
233 	while (1) {
234 		/* Retrieve the sink format */
235 		pad = &subdev->entity.pads[0];
236 		if (!(pad->flags & MEDIA_PAD_FL_SINK))
237 			break;
238 
239 		fmt_sink.which = V4L2_SUBDEV_FORMAT_ACTIVE;
240 		fmt_sink.pad = pad->index;
241 		ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL,
242 				       &fmt_sink);
243 
244 		if (ret < 0 && ret != -ENOIOCTLCMD)
245 			return -EPIPE;
246 
247 		/* Retrieve the source format */
248 		pad = media_entity_remote_pad(pad);
249 		if (pad == NULL ||
250 			pad->entity->type != MEDIA_ENT_T_V4L2_SUBDEV)
251 			break;
252 
253 		subdev = media_entity_to_v4l2_subdev(pad->entity);
254 
255 		fmt_source.which = V4L2_SUBDEV_FORMAT_ACTIVE;
256 		fmt_source.pad = pad->index;
257 		ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_source);
258 		if (ret < 0 && ret != -ENOIOCTLCMD)
259 			return -EPIPE;
260 
261 		/* Check if the two ends match */
262 		if (fmt_source.format.code != fmt_sink.format.code ||
263 		    fmt_source.format.width != fmt_sink.format.width ||
264 		    fmt_source.format.height != fmt_sink.format.height)
265 			return -EPIPE;
266 	}
267 	return 0;
268 }
269 
270 /*
271  * vpfe_pipeline_enable() - Enable streaming on a pipeline
272  * @vpfe_dev: vpfe device
273  * @pipe: vpfe pipeline
274  *
275  * Walk the entities chain starting at the pipeline output video node and start
276  * all modules in the chain in the given mode.
277  *
278  * Return 0 if successful, or the return value of the failed video::s_stream
279  * operation otherwise.
280  */
vpfe_pipeline_enable(struct vpfe_pipeline * pipe)281 static int vpfe_pipeline_enable(struct vpfe_pipeline *pipe)
282 {
283 	struct media_entity_graph graph;
284 	struct media_entity *entity;
285 	struct v4l2_subdev *subdev;
286 	struct media_device *mdev;
287 	int ret = 0;
288 
289 	if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
290 		entity = vpfe_get_input_entity(pipe->outputs[0]);
291 	else
292 		entity = &pipe->inputs[0]->video_dev.entity;
293 
294 	mdev = entity->parent;
295 	mutex_lock(&mdev->graph_mutex);
296 	media_entity_graph_walk_start(&graph, entity);
297 	while ((entity = media_entity_graph_walk_next(&graph))) {
298 
299 		if (media_entity_type(entity) == MEDIA_ENT_T_DEVNODE)
300 			continue;
301 		subdev = media_entity_to_v4l2_subdev(entity);
302 		ret = v4l2_subdev_call(subdev, video, s_stream, 1);
303 		if (ret < 0 && ret != -ENOIOCTLCMD)
304 			break;
305 	}
306 	mutex_unlock(&mdev->graph_mutex);
307 	return ret;
308 }
309 
310 /*
311  * vpfe_pipeline_disable() - Disable streaming on a pipeline
312  * @vpfe_dev: vpfe device
313  * @pipe: VPFE pipeline
314  *
315  * Walk the entities chain starting at the pipeline output video node and stop
316  * all modules in the chain.
317  *
318  * Return 0 if all modules have been properly stopped, or -ETIMEDOUT if a module
319  * can't be stopped.
320  */
vpfe_pipeline_disable(struct vpfe_pipeline * pipe)321 static int vpfe_pipeline_disable(struct vpfe_pipeline *pipe)
322 {
323 	struct media_entity_graph graph;
324 	struct media_entity *entity;
325 	struct v4l2_subdev *subdev;
326 	struct media_device *mdev;
327 	int ret = 0;
328 
329 	if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
330 		entity = vpfe_get_input_entity(pipe->outputs[0]);
331 	else
332 		entity = &pipe->inputs[0]->video_dev.entity;
333 
334 	mdev = entity->parent;
335 	mutex_lock(&mdev->graph_mutex);
336 	media_entity_graph_walk_start(&graph, entity);
337 
338 	while ((entity = media_entity_graph_walk_next(&graph))) {
339 
340 		if (media_entity_type(entity) == MEDIA_ENT_T_DEVNODE)
341 			continue;
342 		subdev = media_entity_to_v4l2_subdev(entity);
343 		ret = v4l2_subdev_call(subdev, video, s_stream, 0);
344 		if (ret < 0 && ret != -ENOIOCTLCMD)
345 			break;
346 	}
347 	mutex_unlock(&mdev->graph_mutex);
348 
349 	return ret ? -ETIMEDOUT : 0;
350 }
351 
352 /*
353  * vpfe_pipeline_set_stream() - Enable/disable streaming on a pipeline
354  * @vpfe_dev: VPFE device
355  * @pipe: VPFE pipeline
356  * @state: Stream state (stopped or active)
357  *
358  * Set the pipeline to the given stream state.
359  *
360  * Return 0 if successful, or the return value of the failed video::s_stream
361  * operation otherwise.
362  */
vpfe_pipeline_set_stream(struct vpfe_pipeline * pipe,enum vpfe_pipeline_stream_state state)363 static int vpfe_pipeline_set_stream(struct vpfe_pipeline *pipe,
364 			    enum vpfe_pipeline_stream_state state)
365 {
366 	if (state == VPFE_PIPELINE_STREAM_STOPPED)
367 		return vpfe_pipeline_disable(pipe);
368 
369 	return vpfe_pipeline_enable(pipe);
370 }
371 
all_videos_stopped(struct vpfe_video_device * video)372 static int all_videos_stopped(struct vpfe_video_device *video)
373 {
374 	struct vpfe_pipeline *pipe = &video->pipe;
375 	int i;
376 
377 	for (i = 0; i < pipe->input_num; i++)
378 		if (pipe->inputs[i]->started)
379 			return 0;
380 	for (i = 0; i < pipe->output_num; i++)
381 		if (pipe->outputs[i]->started)
382 			return 0;
383 	return 1;
384 }
385 
386 /*
387  * vpfe_open() - open video device
388  * @file: file pointer
389  *
390  * initialize media pipeline state, allocate memory for file handle
391  *
392  * Return 0 if successful, or the return -ENODEV otherwise.
393  */
vpfe_open(struct file * file)394 static int vpfe_open(struct file *file)
395 {
396 	struct vpfe_video_device *video = video_drvdata(file);
397 	struct vpfe_fh *handle;
398 
399 	/* Allocate memory for the file handle object */
400 	handle = kzalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
401 
402 	if (handle == NULL)
403 		return -ENOMEM;
404 
405 	v4l2_fh_init(&handle->vfh, &video->video_dev);
406 	v4l2_fh_add(&handle->vfh);
407 
408 	mutex_lock(&video->lock);
409 	/* If decoder is not initialized. initialize it */
410 	if (!video->initialized && vpfe_update_pipe_state(video)) {
411 		mutex_unlock(&video->lock);
412 		return -ENODEV;
413 	}
414 	/* Increment device users counter */
415 	video->usrs++;
416 	/* Set io_allowed member to false */
417 	handle->io_allowed = 0;
418 	handle->video = video;
419 	file->private_data = &handle->vfh;
420 	mutex_unlock(&video->lock);
421 
422 	return 0;
423 }
424 
425 /* get the next buffer available from dma queue */
426 static unsigned long
vpfe_video_get_next_buffer(struct vpfe_video_device * video)427 vpfe_video_get_next_buffer(struct vpfe_video_device *video)
428 {
429 	video->cur_frm = video->next_frm =
430 		list_entry(video->dma_queue.next,
431 			   struct vpfe_cap_buffer, list);
432 
433 	list_del(&video->next_frm->list);
434 	video->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
435 	return vb2_dma_contig_plane_dma_addr(&video->next_frm->vb, 0);
436 }
437 
438 /* schedule the next buffer which is available on dma queue */
vpfe_video_schedule_next_buffer(struct vpfe_video_device * video)439 void vpfe_video_schedule_next_buffer(struct vpfe_video_device *video)
440 {
441 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
442 	unsigned long addr;
443 
444 	if (list_empty(&video->dma_queue))
445 		return;
446 
447 	video->next_frm = list_entry(video->dma_queue.next,
448 					struct vpfe_cap_buffer, list);
449 
450 	if (VPFE_PIPELINE_STREAM_SINGLESHOT == video->pipe.state)
451 		video->cur_frm = video->next_frm;
452 
453 	list_del(&video->next_frm->list);
454 	video->next_frm->vb.state = VB2_BUF_STATE_ACTIVE;
455 	addr = vb2_dma_contig_plane_dma_addr(&video->next_frm->vb, 0);
456 	video->ops->queue(vpfe_dev, addr);
457 	video->state = VPFE_VIDEO_BUFFER_QUEUED;
458 }
459 
460 /* schedule the buffer for capturing bottom field */
vpfe_video_schedule_bottom_field(struct vpfe_video_device * video)461 void vpfe_video_schedule_bottom_field(struct vpfe_video_device *video)
462 {
463 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
464 	unsigned long addr;
465 
466 	addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb, 0);
467 	addr += video->field_off;
468 	video->ops->queue(vpfe_dev, addr);
469 }
470 
471 /* make buffer available for dequeue */
vpfe_video_process_buffer_complete(struct vpfe_video_device * video)472 void vpfe_video_process_buffer_complete(struct vpfe_video_device *video)
473 {
474 	struct vpfe_pipeline *pipe = &video->pipe;
475 
476 	do_gettimeofday(&video->cur_frm->vb.v4l2_buf.timestamp);
477 	vb2_buffer_done(&video->cur_frm->vb, VB2_BUF_STATE_DONE);
478 	if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
479 		video->cur_frm = video->next_frm;
480 }
481 
482 /* vpfe_stop_capture() - stop streaming */
vpfe_stop_capture(struct vpfe_video_device * video)483 static void vpfe_stop_capture(struct vpfe_video_device *video)
484 {
485 	struct vpfe_pipeline *pipe = &video->pipe;
486 
487 	video->started = 0;
488 
489 	if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
490 		return;
491 	if (all_videos_stopped(video))
492 		vpfe_pipeline_set_stream(pipe,
493 					 VPFE_PIPELINE_STREAM_STOPPED);
494 }
495 
496 /*
497  * vpfe_release() - release video device
498  * @file: file pointer
499  *
500  * deletes buffer queue, frees the buffers and the vpfe file handle
501  *
502  * Return 0
503  */
vpfe_release(struct file * file)504 static int vpfe_release(struct file *file)
505 {
506 	struct vpfe_video_device *video = video_drvdata(file);
507 	struct v4l2_fh *vfh = file->private_data;
508 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
509 	struct vpfe_fh *fh = container_of(vfh, struct vpfe_fh, vfh);
510 
511 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
512 
513 	/* Get the device lock */
514 	mutex_lock(&video->lock);
515 	/* if this instance is doing IO */
516 	if (fh->io_allowed) {
517 		if (video->started) {
518 			vpfe_stop_capture(video);
519 			/* mark pipe state as stopped in vpfe_release(),
520 			   as app might call streamon() after streamoff()
521 			   in which case driver has to start streaming.
522 			*/
523 			video->pipe.state = VPFE_PIPELINE_STREAM_STOPPED;
524 			vb2_streamoff(&video->buffer_queue,
525 				      video->buffer_queue.type);
526 		}
527 		video->io_usrs = 0;
528 		/* Free buffers allocated */
529 		vb2_queue_release(&video->buffer_queue);
530 		vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
531 	}
532 	/* Decrement device users counter */
533 	video->usrs--;
534 	v4l2_fh_del(&fh->vfh);
535 	v4l2_fh_exit(&fh->vfh);
536 	/* If this is the last file handle */
537 	if (!video->usrs)
538 		video->initialized = 0;
539 	mutex_unlock(&video->lock);
540 	file->private_data = NULL;
541 	/* Free memory allocated to file handle object */
542 	v4l2_fh_del(vfh);
543 	kzfree(fh);
544 	return 0;
545 }
546 
547 /*
548  * vpfe_mmap() - It is used to map kernel space buffers
549  * into user spaces
550  */
vpfe_mmap(struct file * file,struct vm_area_struct * vma)551 static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
552 {
553 	struct vpfe_video_device *video = video_drvdata(file);
554 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
555 
556 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
557 	return vb2_mmap(&video->buffer_queue, vma);
558 }
559 
560 /*
561  * vpfe_poll() - It is used for select/poll system call
562  */
vpfe_poll(struct file * file,poll_table * wait)563 static unsigned int vpfe_poll(struct file *file, poll_table *wait)
564 {
565 	struct vpfe_video_device *video = video_drvdata(file);
566 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
567 
568 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
569 	if (video->started)
570 		return vb2_poll(&video->buffer_queue, file, wait);
571 	return 0;
572 }
573 
574 /* vpfe capture driver file operations */
575 static const struct v4l2_file_operations vpfe_fops = {
576 	.owner = THIS_MODULE,
577 	.open = vpfe_open,
578 	.release = vpfe_release,
579 	.unlocked_ioctl = video_ioctl2,
580 	.mmap = vpfe_mmap,
581 	.poll = vpfe_poll
582 };
583 
584 /*
585  * vpfe_querycap() - query capabilities of video device
586  * @file: file pointer
587  * @priv: void pointer
588  * @cap: pointer to v4l2_capability structure
589  *
590  * fills v4l2 capabilities structure
591  *
592  * Return 0
593  */
vpfe_querycap(struct file * file,void * priv,struct v4l2_capability * cap)594 static int vpfe_querycap(struct file *file, void  *priv,
595 			       struct v4l2_capability *cap)
596 {
597 	struct vpfe_video_device *video = video_drvdata(file);
598 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
599 
600 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
601 
602 	if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
603 		cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
604 	else
605 		cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
606 	cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
607 			    V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
608 	strlcpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
609 	strlcpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
610 	strlcpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
611 
612 	return 0;
613 }
614 
615 /*
616  * vpfe_g_fmt() - get the format which is active on video device
617  * @file: file pointer
618  * @priv: void pointer
619  * @fmt: pointer to v4l2_format structure
620  *
621  * fills v4l2 format structure with active format
622  *
623  * Return 0
624  */
vpfe_g_fmt(struct file * file,void * priv,struct v4l2_format * fmt)625 static int vpfe_g_fmt(struct file *file, void *priv,
626 				struct v4l2_format *fmt)
627 {
628 	struct vpfe_video_device *video = video_drvdata(file);
629 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
630 
631 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt\n");
632 	/* Fill in the information about format */
633 	*fmt = video->fmt;
634 	return 0;
635 }
636 
637 /*
638  * vpfe_enum_fmt() - enum formats supported on media chain
639  * @file: file pointer
640  * @priv: void pointer
641  * @fmt: pointer to v4l2_fmtdesc structure
642  *
643  * fills v4l2_fmtdesc structure with output format set on adjacent subdev,
644  * only one format is enumearted as subdevs are already configured
645  *
646  * Return 0 if successful, error code otherwise
647  */
vpfe_enum_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * fmt)648 static int vpfe_enum_fmt(struct file *file, void  *priv,
649 				   struct v4l2_fmtdesc *fmt)
650 {
651 	struct vpfe_video_device *video = video_drvdata(file);
652 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
653 	struct v4l2_subdev_format sd_fmt;
654 	struct v4l2_mbus_framefmt mbus;
655 	struct v4l2_subdev *subdev;
656 	struct v4l2_format format;
657 	struct media_pad *remote;
658 	int ret;
659 
660 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt\n");
661 
662 	/* since already subdev pad format is set,
663 	only one pixel format is available */
664 	if (fmt->index > 0) {
665 		v4l2_err(&vpfe_dev->v4l2_dev, "Invalid index\n");
666 		return -EINVAL;
667 	}
668 	/* get the remote pad */
669 	remote = media_entity_remote_pad(&video->pad);
670 	if (remote == NULL) {
671 		v4l2_err(&vpfe_dev->v4l2_dev,
672 			 "invalid remote pad for video node\n");
673 		return -EINVAL;
674 	}
675 	/* get the remote subdev */
676 	subdev = vpfe_video_remote_subdev(video, NULL);
677 	if (subdev == NULL) {
678 		v4l2_err(&vpfe_dev->v4l2_dev,
679 			 "invalid remote subdev for video node\n");
680 		return -EINVAL;
681 	}
682 	sd_fmt.pad = remote->index;
683 	sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
684 	/* get output format of remote subdev */
685 	ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &sd_fmt);
686 	if (ret) {
687 		v4l2_err(&vpfe_dev->v4l2_dev,
688 			 "invalid remote subdev for video node\n");
689 		return ret;
690 	}
691 	/* convert to pix format */
692 	mbus.code = sd_fmt.format.code;
693 	mbus_to_pix(&mbus, &format.fmt.pix);
694 	/* copy the result */
695 	fmt->pixelformat = format.fmt.pix.pixelformat;
696 
697 	return 0;
698 }
699 
700 /*
701  * vpfe_s_fmt() - set the format on video device
702  * @file: file pointer
703  * @priv: void pointer
704  * @fmt: pointer to v4l2_format structure
705  *
706  * validate and set the format on video device
707  *
708  * Return 0 on success, error code otherwise
709  */
vpfe_s_fmt(struct file * file,void * priv,struct v4l2_format * fmt)710 static int vpfe_s_fmt(struct file *file, void *priv,
711 				struct v4l2_format *fmt)
712 {
713 	struct vpfe_video_device *video = video_drvdata(file);
714 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
715 	struct v4l2_format format;
716 	int ret;
717 
718 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt\n");
719 	/* If streaming is started, return error */
720 	if (video->started) {
721 		v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
722 		return -EBUSY;
723 	}
724 	/* get adjacent subdev's output pad format */
725 	ret = __vpfe_video_get_format(video, &format);
726 	if (ret)
727 		return ret;
728 	*fmt = format;
729 	video->fmt = *fmt;
730 	return 0;
731 }
732 
733 /*
734  * vpfe_try_fmt() - try the format on video device
735  * @file: file pointer
736  * @priv: void pointer
737  * @fmt: pointer to v4l2_format structure
738  *
739  * validate the format, update with correct format
740  * based on output format set on adjacent subdev
741  *
742  * Return 0 on success, error code otherwise
743  */
vpfe_try_fmt(struct file * file,void * priv,struct v4l2_format * fmt)744 static int vpfe_try_fmt(struct file *file, void *priv,
745 				  struct v4l2_format *fmt)
746 {
747 	struct vpfe_video_device *video = video_drvdata(file);
748 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
749 	struct v4l2_format format;
750 	int ret;
751 
752 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt\n");
753 	/* get adjacent subdev's output pad format */
754 	ret = __vpfe_video_get_format(video, &format);
755 	if (ret)
756 		return ret;
757 
758 	*fmt = format;
759 	return 0;
760 }
761 
762 /*
763  * vpfe_enum_input() - enum inputs supported on media chain
764  * @file: file pointer
765  * @priv: void pointer
766  * @fmt: pointer to v4l2_fmtdesc structure
767  *
768  * fills v4l2_input structure with input available on media chain,
769  * only one input is enumearted as media chain is setup by this time
770  *
771  * Return 0 if successful, -EINVAL is media chain is invalid
772  */
vpfe_enum_input(struct file * file,void * priv,struct v4l2_input * inp)773 static int vpfe_enum_input(struct file *file, void *priv,
774 				 struct v4l2_input *inp)
775 {
776 	struct vpfe_video_device *video = video_drvdata(file);
777 	struct vpfe_ext_subdev_info *sdinfo = video->current_ext_subdev;
778 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
779 
780 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
781 	/* enumerate from the subdev user has chosen through mc */
782 	if (inp->index < sdinfo->num_inputs) {
783 		memcpy(inp, &sdinfo->inputs[inp->index],
784 		       sizeof(struct v4l2_input));
785 		return 0;
786 	}
787 	return -EINVAL;
788 }
789 
790 /*
791  * vpfe_g_input() - get index of the input which is active
792  * @file: file pointer
793  * @priv: void pointer
794  * @index: pointer to unsigned int
795  *
796  * set index with input index which is active
797  */
vpfe_g_input(struct file * file,void * priv,unsigned int * index)798 static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
799 {
800 	struct vpfe_video_device *video = video_drvdata(file);
801 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
802 
803 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
804 
805 	*index = video->current_input;
806 	return 0;
807 }
808 
809 /*
810  * vpfe_s_input() - set input which is pointed by input index
811  * @file: file pointer
812  * @priv: void pointer
813  * @index: pointer to unsigned int
814  *
815  * set input on external subdev
816  *
817  * Return 0 on success, error code otherwise
818  */
vpfe_s_input(struct file * file,void * priv,unsigned int index)819 static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
820 {
821 	struct vpfe_video_device *video = video_drvdata(file);
822 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
823 	struct vpfe_ext_subdev_info *sdinfo;
824 	struct vpfe_route *route;
825 	struct v4l2_input *inps;
826 	u32 output;
827 	u32 input;
828 	int ret;
829 	int i;
830 
831 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
832 
833 	ret = mutex_lock_interruptible(&video->lock);
834 	if (ret)
835 		return ret;
836 	/*
837 	 * If streaming is started return device busy
838 	 * error
839 	 */
840 	if (video->started) {
841 		v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
842 		ret = -EBUSY;
843 		goto unlock_out;
844 	}
845 
846 	sdinfo = video->current_ext_subdev;
847 	if (!sdinfo->registered) {
848 		ret = -EINVAL;
849 		goto unlock_out;
850 	}
851 	if (vpfe_dev->cfg->setup_input &&
852 		vpfe_dev->cfg->setup_input(sdinfo->grp_id) < 0) {
853 		ret = -EFAULT;
854 		v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
855 			  "couldn't setup input for %s\n",
856 			  sdinfo->module_name);
857 		goto unlock_out;
858 	}
859 	route = &sdinfo->routes[index];
860 	if (route && sdinfo->can_route) {
861 		input = route->input;
862 		output = route->output;
863 		ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
864 						 sdinfo->grp_id, video,
865 						 s_routing, input, output, 0);
866 		if (ret) {
867 			v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
868 				"s_input:error in setting input in decoder\n");
869 			ret = -EINVAL;
870 			goto unlock_out;
871 		}
872 	}
873 	/* set standards set by subdev in video device */
874 	for (i = 0; i < sdinfo->num_inputs; i++) {
875 		inps = &sdinfo->inputs[i];
876 		video->video_dev.tvnorms |= inps->std;
877 	}
878 	video->current_input = index;
879 unlock_out:
880 	mutex_unlock(&video->lock);
881 	return ret;
882 }
883 
884 /*
885  * vpfe_querystd() - query std which is being input on external subdev
886  * @file: file pointer
887  * @priv: void pointer
888  * @std_id: pointer to v4l2_std_id structure
889  *
890  * call external subdev through v4l2_device_call_until_err to
891  * get the std that is being active.
892  *
893  * Return 0 on success, error code otherwise
894  */
vpfe_querystd(struct file * file,void * priv,v4l2_std_id * std_id)895 static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
896 {
897 	struct vpfe_video_device *video = video_drvdata(file);
898 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
899 	struct vpfe_ext_subdev_info *sdinfo;
900 	int ret;
901 
902 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
903 
904 	ret = mutex_lock_interruptible(&video->lock);
905 	sdinfo = video->current_ext_subdev;
906 	if (ret)
907 		return ret;
908 	/* Call querystd function of decoder device */
909 	ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
910 					 video, querystd, std_id);
911 	mutex_unlock(&video->lock);
912 	return ret;
913 }
914 
915 /*
916  * vpfe_s_std() - set std on external subdev
917  * @file: file pointer
918  * @priv: void pointer
919  * @std_id: pointer to v4l2_std_id structure
920  *
921  * set std pointed by std_id on external subdev by calling it using
922  * v4l2_device_call_until_err
923  *
924  * Return 0 on success, error code otherwise
925  */
vpfe_s_std(struct file * file,void * priv,v4l2_std_id std_id)926 static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
927 {
928 	struct vpfe_video_device *video = video_drvdata(file);
929 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
930 	struct vpfe_ext_subdev_info *sdinfo;
931 	int ret;
932 
933 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
934 
935 	/* Call decoder driver function to set the standard */
936 	ret = mutex_lock_interruptible(&video->lock);
937 	if (ret)
938 		return ret;
939 	sdinfo = video->current_ext_subdev;
940 	/* If streaming is started, return device busy error */
941 	if (video->started) {
942 		v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
943 		ret = -EBUSY;
944 		goto unlock_out;
945 	}
946 	ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
947 					 video, s_std, std_id);
948 	if (ret < 0) {
949 		v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
950 		video->stdid = V4L2_STD_UNKNOWN;
951 		goto unlock_out;
952 	}
953 	video->stdid = std_id;
954 unlock_out:
955 	mutex_unlock(&video->lock);
956 	return ret;
957 }
958 
vpfe_g_std(struct file * file,void * priv,v4l2_std_id * tvnorm)959 static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
960 {
961 	struct vpfe_video_device *video = video_drvdata(file);
962 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
963 
964 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
965 	*tvnorm = video->stdid;
966 	return 0;
967 }
968 
969 /*
970  * vpfe_enum_dv_timings() - enumerate dv_timings which are supported by
971  *			to external subdev
972  * @file: file pointer
973  * @priv: void pointer
974  * @timings: pointer to v4l2_enum_dv_timings structure
975  *
976  * enum dv_timings's which are supported by external subdev through
977  * v4l2_subdev_call
978  *
979  * Return 0 on success, error code otherwise
980  */
981 static int
vpfe_enum_dv_timings(struct file * file,void * fh,struct v4l2_enum_dv_timings * timings)982 vpfe_enum_dv_timings(struct file *file, void *fh,
983 		  struct v4l2_enum_dv_timings *timings)
984 {
985 	struct vpfe_video_device *video = video_drvdata(file);
986 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
987 	struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
988 
989 	timings->pad = 0;
990 
991 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_dv_timings\n");
992 	return v4l2_subdev_call(subdev, pad, enum_dv_timings, timings);
993 }
994 
995 /*
996  * vpfe_query_dv_timings() - query the dv_timings which is being input
997  *			to external subdev
998  * @file: file pointer
999  * @priv: void pointer
1000  * @timings: pointer to v4l2_dv_timings structure
1001  *
1002  * get dv_timings which is being input on external subdev through
1003  * v4l2_subdev_call
1004  *
1005  * Return 0 on success, error code otherwise
1006  */
1007 static int
vpfe_query_dv_timings(struct file * file,void * fh,struct v4l2_dv_timings * timings)1008 vpfe_query_dv_timings(struct file *file, void *fh,
1009 		   struct v4l2_dv_timings *timings)
1010 {
1011 	struct vpfe_video_device *video = video_drvdata(file);
1012 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
1013 	struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1014 
1015 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_query_dv_timings\n");
1016 	return v4l2_subdev_call(subdev, video, query_dv_timings, timings);
1017 }
1018 
1019 /*
1020  * vpfe_s_dv_timings() - set dv_timings on external subdev
1021  * @file: file pointer
1022  * @priv: void pointer
1023  * @timings: pointer to v4l2_dv_timings structure
1024  *
1025  * set dv_timings pointed by timings on external subdev through
1026  * v4l2_device_call_until_err, this configures amplifier also
1027  *
1028  * Return 0 on success, error code otherwise
1029  */
1030 static int
vpfe_s_dv_timings(struct file * file,void * fh,struct v4l2_dv_timings * timings)1031 vpfe_s_dv_timings(struct file *file, void *fh,
1032 		  struct v4l2_dv_timings *timings)
1033 {
1034 	struct vpfe_video_device *video = video_drvdata(file);
1035 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
1036 
1037 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_dv_timings\n");
1038 
1039 	video->stdid = V4L2_STD_UNKNOWN;
1040 	return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
1041 					  video->current_ext_subdev->grp_id,
1042 					  video, s_dv_timings, timings);
1043 }
1044 
1045 /*
1046  * vpfe_g_dv_timings() - get dv_timings which is set on external subdev
1047  * @file: file pointer
1048  * @priv: void pointer
1049  * @timings: pointer to v4l2_dv_timings structure
1050  *
1051  * get dv_timings which is set on external subdev through
1052  * v4l2_subdev_call
1053  *
1054  * Return 0 on success, error code otherwise
1055  */
1056 static int
vpfe_g_dv_timings(struct file * file,void * fh,struct v4l2_dv_timings * timings)1057 vpfe_g_dv_timings(struct file *file, void *fh,
1058 	      struct v4l2_dv_timings *timings)
1059 {
1060 	struct vpfe_video_device *video = video_drvdata(file);
1061 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
1062 	struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1063 
1064 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_dv_timings\n");
1065 	return v4l2_subdev_call(subdev, video, g_dv_timings, timings);
1066 }
1067 
1068 /*
1069  *  Videobuf operations
1070  */
1071 /*
1072  * vpfe_buffer_queue_setup : Callback function for buffer setup.
1073  * @vq: vb2_queue ptr
1074  * @fmt: v4l2 format
1075  * @nbuffers: ptr to number of buffers requested by application
1076  * @nplanes:: contains number of distinct video planes needed to hold a frame
1077  * @sizes[]: contains the size (in bytes) of each plane.
1078  * @alloc_ctxs: ptr to allocation context
1079  *
1080  * This callback function is called when reqbuf() is called to adjust
1081  * the buffer nbuffers and buffer size
1082  */
1083 static int
vpfe_buffer_queue_setup(struct vb2_queue * vq,const struct v4l2_format * fmt,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],void * alloc_ctxs[])1084 vpfe_buffer_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
1085 			unsigned int *nbuffers, unsigned int *nplanes,
1086 			unsigned int sizes[], void *alloc_ctxs[])
1087 {
1088 	struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1089 	struct vpfe_video_device *video = fh->video;
1090 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
1091 	struct vpfe_pipeline *pipe = &video->pipe;
1092 	unsigned long size;
1093 
1094 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue_setup\n");
1095 	size = video->fmt.fmt.pix.sizeimage;
1096 
1097 	if (vpfe_dev->video_limit) {
1098 		while (size * *nbuffers > vpfe_dev->video_limit)
1099 			(*nbuffers)--;
1100 	}
1101 	if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS) {
1102 		if (*nbuffers < MIN_NUM_BUFFERS)
1103 			*nbuffers = MIN_NUM_BUFFERS;
1104 	}
1105 	*nplanes = 1;
1106 	sizes[0] = size;
1107 	alloc_ctxs[0] = video->alloc_ctx;
1108 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1109 		 "nbuffers=%d, size=%lu\n", *nbuffers, size);
1110 	return 0;
1111 }
1112 
1113 /*
1114  * vpfe_buffer_prepare : callback function for buffer prepare
1115  * @vb: ptr to vb2_buffer
1116  *
1117  * This is the callback function for buffer prepare when vb2_qbuf()
1118  * function is called. The buffer is prepared and user space virtual address
1119  * or user address is converted into  physical address
1120  */
vpfe_buffer_prepare(struct vb2_buffer * vb)1121 static int vpfe_buffer_prepare(struct vb2_buffer *vb)
1122 {
1123 	struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1124 	struct vpfe_video_device *video = fh->video;
1125 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
1126 	unsigned long addr;
1127 
1128 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1129 
1130 	if (vb->state != VB2_BUF_STATE_ACTIVE &&
1131 	    vb->state != VB2_BUF_STATE_PREPARED)
1132 		return 0;
1133 
1134 	/* Initialize buffer */
1135 	vb2_set_plane_payload(vb, 0, video->fmt.fmt.pix.sizeimage);
1136 	if (vb2_plane_vaddr(vb, 0) &&
1137 		vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
1138 			return -EINVAL;
1139 
1140 	addr = vb2_dma_contig_plane_dma_addr(vb, 0);
1141 	/* Make sure user addresses are aligned to 32 bytes */
1142 	if (!ALIGN(addr, 32))
1143 		return -EINVAL;
1144 
1145 	return 0;
1146 }
1147 
vpfe_buffer_queue(struct vb2_buffer * vb)1148 static void vpfe_buffer_queue(struct vb2_buffer *vb)
1149 {
1150 	/* Get the file handle object and device object */
1151 	struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1152 	struct vpfe_video_device *video = fh->video;
1153 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
1154 	struct vpfe_pipeline *pipe = &video->pipe;
1155 	struct vpfe_cap_buffer *buf = container_of(vb,
1156 				struct vpfe_cap_buffer, vb);
1157 	unsigned long flags;
1158 	unsigned long empty;
1159 	unsigned long addr;
1160 
1161 	spin_lock_irqsave(&video->dma_queue_lock, flags);
1162 	empty = list_empty(&video->dma_queue);
1163 	/* add the buffer to the DMA queue */
1164 	list_add_tail(&buf->list, &video->dma_queue);
1165 	spin_unlock_irqrestore(&video->dma_queue_lock, flags);
1166 	/* this case happens in case of single shot */
1167 	if (empty && video->started && pipe->state ==
1168 		VPFE_PIPELINE_STREAM_SINGLESHOT &&
1169 		video->state == VPFE_VIDEO_BUFFER_NOT_QUEUED) {
1170 		spin_lock(&video->dma_queue_lock);
1171 		addr = vpfe_video_get_next_buffer(video);
1172 		video->ops->queue(vpfe_dev, addr);
1173 
1174 		video->state = VPFE_VIDEO_BUFFER_QUEUED;
1175 		spin_unlock(&video->dma_queue_lock);
1176 
1177 		/* enable h/w each time in single shot */
1178 		if (vpfe_video_is_pipe_ready(pipe))
1179 			vpfe_pipeline_set_stream(pipe,
1180 					VPFE_PIPELINE_STREAM_SINGLESHOT);
1181 	}
1182 }
1183 
1184 /* vpfe_start_capture() - start streaming on all the subdevs */
vpfe_start_capture(struct vpfe_video_device * video)1185 static int vpfe_start_capture(struct vpfe_video_device *video)
1186 {
1187 	struct vpfe_pipeline *pipe = &video->pipe;
1188 	int ret = 0;
1189 
1190 	video->started = 1;
1191 	if (vpfe_video_is_pipe_ready(pipe))
1192 		ret = vpfe_pipeline_set_stream(pipe, pipe->state);
1193 
1194 	return ret;
1195 }
1196 
vpfe_start_streaming(struct vb2_queue * vq,unsigned int count)1197 static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
1198 {
1199 	struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1200 	struct vpfe_video_device *video = fh->video;
1201 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
1202 	unsigned long addr;
1203 	int ret;
1204 
1205 	ret = mutex_lock_interruptible(&video->lock);
1206 	if (ret)
1207 		goto streamoff;
1208 
1209 	/* Get the next frame from the buffer queue */
1210 	video->cur_frm = video->next_frm =
1211 		list_entry(video->dma_queue.next, struct vpfe_cap_buffer, list);
1212 	/* Remove buffer from the buffer queue */
1213 	list_del(&video->cur_frm->list);
1214 	/* Mark state of the current frame to active */
1215 	video->cur_frm->vb.state = VB2_BUF_STATE_ACTIVE;
1216 	/* Initialize field_id and started member */
1217 	video->field_id = 0;
1218 	addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb, 0);
1219 	video->ops->queue(vpfe_dev, addr);
1220 	video->state = VPFE_VIDEO_BUFFER_QUEUED;
1221 
1222 	ret = vpfe_start_capture(video);
1223 	if (ret) {
1224 		struct vpfe_cap_buffer *buf, *tmp;
1225 
1226 		vb2_buffer_done(&video->cur_frm->vb, VB2_BUF_STATE_QUEUED);
1227 		list_for_each_entry_safe(buf, tmp, &video->dma_queue, list) {
1228 			list_del(&buf->list);
1229 			vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
1230 		}
1231 		goto unlock_out;
1232 	}
1233 
1234 	mutex_unlock(&video->lock);
1235 
1236 	return ret;
1237 unlock_out:
1238 	mutex_unlock(&video->lock);
1239 streamoff:
1240 	ret = vb2_streamoff(&video->buffer_queue, video->buffer_queue.type);
1241 	return 0;
1242 }
1243 
vpfe_buffer_init(struct vb2_buffer * vb)1244 static int vpfe_buffer_init(struct vb2_buffer *vb)
1245 {
1246 	struct vpfe_cap_buffer *buf = container_of(vb,
1247 						   struct vpfe_cap_buffer, vb);
1248 
1249 	INIT_LIST_HEAD(&buf->list);
1250 	return 0;
1251 }
1252 
1253 /* abort streaming and wait for last buffer */
vpfe_stop_streaming(struct vb2_queue * vq)1254 static void vpfe_stop_streaming(struct vb2_queue *vq)
1255 {
1256 	struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1257 	struct vpfe_video_device *video = fh->video;
1258 
1259 	/* release all active buffers */
1260 	if (video->cur_frm == video->next_frm) {
1261 		vb2_buffer_done(&video->cur_frm->vb, VB2_BUF_STATE_ERROR);
1262 	} else {
1263 		if (video->cur_frm != NULL)
1264 			vb2_buffer_done(&video->cur_frm->vb,
1265 					VB2_BUF_STATE_ERROR);
1266 		if (video->next_frm != NULL)
1267 			vb2_buffer_done(&video->next_frm->vb,
1268 					VB2_BUF_STATE_ERROR);
1269 	}
1270 
1271 	while (!list_empty(&video->dma_queue)) {
1272 		video->next_frm = list_entry(video->dma_queue.next,
1273 						struct vpfe_cap_buffer, list);
1274 		list_del(&video->next_frm->list);
1275 		vb2_buffer_done(&video->next_frm->vb, VB2_BUF_STATE_ERROR);
1276 	}
1277 }
1278 
vpfe_buf_cleanup(struct vb2_buffer * vb)1279 static void vpfe_buf_cleanup(struct vb2_buffer *vb)
1280 {
1281 	struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1282 	struct vpfe_video_device *video = fh->video;
1283 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
1284 	struct vpfe_cap_buffer *buf = container_of(vb,
1285 					struct vpfe_cap_buffer, vb);
1286 
1287 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buf_cleanup\n");
1288 	if (vb->state == VB2_BUF_STATE_ACTIVE)
1289 		list_del_init(&buf->list);
1290 }
1291 
1292 static struct vb2_ops video_qops = {
1293 	.queue_setup		= vpfe_buffer_queue_setup,
1294 	.buf_init		= vpfe_buffer_init,
1295 	.buf_prepare		= vpfe_buffer_prepare,
1296 	.start_streaming	= vpfe_start_streaming,
1297 	.stop_streaming		= vpfe_stop_streaming,
1298 	.buf_cleanup		= vpfe_buf_cleanup,
1299 	.buf_queue		= vpfe_buffer_queue,
1300 };
1301 
1302 /*
1303  * vpfe_reqbufs() - supported REQBUF only once opening
1304  * the device.
1305  */
vpfe_reqbufs(struct file * file,void * priv,struct v4l2_requestbuffers * req_buf)1306 static int vpfe_reqbufs(struct file *file, void *priv,
1307 			struct v4l2_requestbuffers *req_buf)
1308 {
1309 	struct vpfe_video_device *video = video_drvdata(file);
1310 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
1311 	struct vpfe_fh *fh = file->private_data;
1312 	struct vb2_queue *q;
1313 	int ret;
1314 
1315 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1316 
1317 	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != req_buf->type &&
1318 	    V4L2_BUF_TYPE_VIDEO_OUTPUT != req_buf->type) {
1319 		v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1320 		return -EINVAL;
1321 	}
1322 
1323 	ret = mutex_lock_interruptible(&video->lock);
1324 	if (ret)
1325 		return ret;
1326 
1327 	if (video->io_usrs != 0) {
1328 		v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1329 		ret = -EBUSY;
1330 		goto unlock_out;
1331 	}
1332 	video->memory = req_buf->memory;
1333 
1334 	/* Initialize videobuf2 queue as per the buffer type */
1335 	video->alloc_ctx = vb2_dma_contig_init_ctx(vpfe_dev->pdev);
1336 	if (IS_ERR(video->alloc_ctx)) {
1337 		v4l2_err(&vpfe_dev->v4l2_dev, "Failed to get the context\n");
1338 		return PTR_ERR(video->alloc_ctx);
1339 	}
1340 
1341 	q = &video->buffer_queue;
1342 	q->type = req_buf->type;
1343 	q->io_modes = VB2_MMAP | VB2_USERPTR;
1344 	q->drv_priv = fh;
1345 	q->min_buffers_needed = 1;
1346 	q->ops = &video_qops;
1347 	q->mem_ops = &vb2_dma_contig_memops;
1348 	q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
1349 
1350 	ret = vb2_queue_init(q);
1351 	if (ret) {
1352 		v4l2_err(&vpfe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1353 		vb2_dma_contig_cleanup_ctx(vpfe_dev->pdev);
1354 		return ret;
1355 	}
1356 
1357 	fh->io_allowed = 1;
1358 	video->io_usrs = 1;
1359 	INIT_LIST_HEAD(&video->dma_queue);
1360 	ret = vb2_reqbufs(&video->buffer_queue, req_buf);
1361 
1362 unlock_out:
1363 	mutex_unlock(&video->lock);
1364 	return ret;
1365 }
1366 
1367 /*
1368  * vpfe_querybuf() - query buffers for exchange
1369  */
vpfe_querybuf(struct file * file,void * priv,struct v4l2_buffer * buf)1370 static int vpfe_querybuf(struct file *file, void *priv,
1371 			 struct v4l2_buffer *buf)
1372 {
1373 	struct vpfe_video_device *video = video_drvdata(file);
1374 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
1375 
1376 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1377 
1378 	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type &&
1379 	    V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1380 		v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1381 		return  -EINVAL;
1382 	}
1383 
1384 	if (video->memory != V4L2_MEMORY_MMAP) {
1385 		v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1386 		return -EINVAL;
1387 	}
1388 
1389 	/* Call vb2_querybuf to get information */
1390 	return vb2_querybuf(&video->buffer_queue, buf);
1391 }
1392 
1393 /*
1394  * vpfe_qbuf() - queue buffers for capture or processing
1395  */
vpfe_qbuf(struct file * file,void * priv,struct v4l2_buffer * p)1396 static int vpfe_qbuf(struct file *file, void *priv,
1397 		     struct v4l2_buffer *p)
1398 {
1399 	struct vpfe_video_device *video = video_drvdata(file);
1400 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
1401 	struct vpfe_fh *fh = file->private_data;
1402 
1403 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1404 
1405 	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != p->type &&
1406 	    V4L2_BUF_TYPE_VIDEO_OUTPUT != p->type) {
1407 		v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1408 		return -EINVAL;
1409 	}
1410 	/*
1411 	 * If this file handle is not allowed to do IO,
1412 	 * return error
1413 	 */
1414 	if (!fh->io_allowed) {
1415 		v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1416 		return -EACCES;
1417 	}
1418 
1419 	return vb2_qbuf(&video->buffer_queue, p);
1420 }
1421 
1422 /*
1423  * vpfe_dqbuf() - deque buffer which is done with processing
1424  */
vpfe_dqbuf(struct file * file,void * priv,struct v4l2_buffer * buf)1425 static int vpfe_dqbuf(struct file *file, void *priv,
1426 		      struct v4l2_buffer *buf)
1427 {
1428 	struct vpfe_video_device *video = video_drvdata(file);
1429 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
1430 
1431 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1432 
1433 	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf->type &&
1434 	    V4L2_BUF_TYPE_VIDEO_OUTPUT != buf->type) {
1435 		v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1436 		return -EINVAL;
1437 	}
1438 
1439 	return vb2_dqbuf(&video->buffer_queue,
1440 			 buf, (file->f_flags & O_NONBLOCK));
1441 }
1442 
1443 /*
1444  * vpfe_streamon() - start streaming
1445  * @file: file pointer
1446  * @priv: void pointer
1447  * @buf_type: enum v4l2_buf_type
1448  *
1449  * queue buffer onto hardware for capture/processing and
1450  * start all the subdevs which are in media chain
1451  *
1452  * Return 0 on success, error code otherwise
1453  */
vpfe_streamon(struct file * file,void * priv,enum v4l2_buf_type buf_type)1454 static int vpfe_streamon(struct file *file, void *priv,
1455 			 enum v4l2_buf_type buf_type)
1456 {
1457 	struct vpfe_video_device *video = video_drvdata(file);
1458 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
1459 	struct vpfe_pipeline *pipe = &video->pipe;
1460 	struct vpfe_fh *fh = file->private_data;
1461 	struct vpfe_ext_subdev_info *sdinfo;
1462 	int ret = -EINVAL;
1463 
1464 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1465 
1466 	if (V4L2_BUF_TYPE_VIDEO_CAPTURE != buf_type &&
1467 	    V4L2_BUF_TYPE_VIDEO_OUTPUT != buf_type) {
1468 		v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1469 		return ret;
1470 	}
1471 	/* If file handle is not allowed IO, return error */
1472 	if (!fh->io_allowed) {
1473 		v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1474 		return -EACCES;
1475 	}
1476 	sdinfo = video->current_ext_subdev;
1477 	/* If buffer queue is empty, return error */
1478 	if (list_empty(&video->buffer_queue.queued_list)) {
1479 		v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1480 		return -EIO;
1481 	}
1482 	/* Validate the pipeline */
1483 	if (V4L2_BUF_TYPE_VIDEO_CAPTURE == buf_type) {
1484 		ret = vpfe_video_validate_pipeline(pipe);
1485 		if (ret < 0)
1486 			return ret;
1487 	}
1488 	/* Call vb2_streamon to start streaming */
1489 	return vb2_streamon(&video->buffer_queue, buf_type);
1490 }
1491 
1492 /*
1493  * vpfe_streamoff() - stop streaming
1494  * @file: file pointer
1495  * @priv: void pointer
1496  * @buf_type: enum v4l2_buf_type
1497  *
1498  * stop all the subdevs which are in media chain
1499  *
1500  * Return 0 on success, error code otherwise
1501  */
vpfe_streamoff(struct file * file,void * priv,enum v4l2_buf_type buf_type)1502 static int vpfe_streamoff(struct file *file, void *priv,
1503 			  enum v4l2_buf_type buf_type)
1504 {
1505 	struct vpfe_video_device *video = video_drvdata(file);
1506 	struct vpfe_device *vpfe_dev = video->vpfe_dev;
1507 	struct vpfe_fh *fh = file->private_data;
1508 	int ret = 0;
1509 
1510 	v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1511 
1512 	if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1513 	    buf_type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1514 		v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "Invalid buf type\n");
1515 		return -EINVAL;
1516 	}
1517 
1518 	/* If io is allowed for this file handle, return error */
1519 	if (!fh->io_allowed) {
1520 		v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1521 		return -EACCES;
1522 	}
1523 
1524 	/* If streaming is not started, return error */
1525 	if (!video->started) {
1526 		v4l2_err(&vpfe_dev->v4l2_dev, "device is not started\n");
1527 		return -EINVAL;
1528 	}
1529 
1530 	ret = mutex_lock_interruptible(&video->lock);
1531 	if (ret)
1532 		return ret;
1533 
1534 	vpfe_stop_capture(video);
1535 	ret = vb2_streamoff(&video->buffer_queue, buf_type);
1536 	mutex_unlock(&video->lock);
1537 
1538 	return ret;
1539 }
1540 
1541 /* vpfe capture ioctl operations */
1542 static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1543 	.vidioc_querycap	 = vpfe_querycap,
1544 	.vidioc_g_fmt_vid_cap    = vpfe_g_fmt,
1545 	.vidioc_s_fmt_vid_cap    = vpfe_s_fmt,
1546 	.vidioc_try_fmt_vid_cap  = vpfe_try_fmt,
1547 	.vidioc_enum_fmt_vid_cap = vpfe_enum_fmt,
1548 	.vidioc_g_fmt_vid_out    = vpfe_g_fmt,
1549 	.vidioc_s_fmt_vid_out    = vpfe_s_fmt,
1550 	.vidioc_try_fmt_vid_out  = vpfe_try_fmt,
1551 	.vidioc_enum_fmt_vid_out = vpfe_enum_fmt,
1552 	.vidioc_enum_input	 = vpfe_enum_input,
1553 	.vidioc_g_input		 = vpfe_g_input,
1554 	.vidioc_s_input		 = vpfe_s_input,
1555 	.vidioc_querystd	 = vpfe_querystd,
1556 	.vidioc_s_std		 = vpfe_s_std,
1557 	.vidioc_g_std		 = vpfe_g_std,
1558 	.vidioc_enum_dv_timings	 = vpfe_enum_dv_timings,
1559 	.vidioc_query_dv_timings = vpfe_query_dv_timings,
1560 	.vidioc_s_dv_timings	 = vpfe_s_dv_timings,
1561 	.vidioc_g_dv_timings	 = vpfe_g_dv_timings,
1562 	.vidioc_reqbufs		 = vpfe_reqbufs,
1563 	.vidioc_querybuf	 = vpfe_querybuf,
1564 	.vidioc_qbuf		 = vpfe_qbuf,
1565 	.vidioc_dqbuf		 = vpfe_dqbuf,
1566 	.vidioc_streamon	 = vpfe_streamon,
1567 	.vidioc_streamoff	 = vpfe_streamoff,
1568 };
1569 
1570 /* VPFE video init function */
vpfe_video_init(struct vpfe_video_device * video,const char * name)1571 int vpfe_video_init(struct vpfe_video_device *video, const char *name)
1572 {
1573 	const char *direction;
1574 	int ret;
1575 
1576 	switch (video->type) {
1577 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1578 		direction = "output";
1579 		video->pad.flags = MEDIA_PAD_FL_SINK;
1580 		video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1581 		break;
1582 
1583 	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1584 		direction = "input";
1585 		video->pad.flags = MEDIA_PAD_FL_SOURCE;
1586 		video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1587 		break;
1588 
1589 	default:
1590 		return -EINVAL;
1591 	}
1592 	/* Initialize field of video device */
1593 	video->video_dev.release = video_device_release;
1594 	video->video_dev.fops = &vpfe_fops;
1595 	video->video_dev.ioctl_ops = &vpfe_ioctl_ops;
1596 	video->video_dev.minor = -1;
1597 	video->video_dev.tvnorms = 0;
1598 	snprintf(video->video_dev.name, sizeof(video->video_dev.name),
1599 		 "DAVINCI VIDEO %s %s", name, direction);
1600 
1601 	spin_lock_init(&video->irqlock);
1602 	spin_lock_init(&video->dma_queue_lock);
1603 	mutex_init(&video->lock);
1604 	ret = media_entity_init(&video->video_dev.entity,
1605 				1, &video->pad, 0);
1606 	if (ret < 0)
1607 		return ret;
1608 
1609 	video_set_drvdata(&video->video_dev, video);
1610 
1611 	return 0;
1612 }
1613 
1614 /* vpfe video device register function */
vpfe_video_register(struct vpfe_video_device * video,struct v4l2_device * vdev)1615 int vpfe_video_register(struct vpfe_video_device *video,
1616 			struct v4l2_device *vdev)
1617 {
1618 	int ret;
1619 
1620 	video->video_dev.v4l2_dev = vdev;
1621 
1622 	ret = video_register_device(&video->video_dev, VFL_TYPE_GRABBER, -1);
1623 	if (ret < 0)
1624 		pr_err("%s: could not register video device (%d)\n",
1625 		       __func__, ret);
1626 	return ret;
1627 }
1628 
1629 /* vpfe video device unregister function */
vpfe_video_unregister(struct vpfe_video_device * video)1630 void vpfe_video_unregister(struct vpfe_video_device *video)
1631 {
1632 	if (video_is_registered(&video->video_dev)) {
1633 		video_unregister_device(&video->video_dev);
1634 		media_entity_cleanup(&video->video_dev.entity);
1635 	}
1636 }
1637