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