2 * Samsung TV Mixer driver
4 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
6 * Tomasz Stanislawski, <t.stanislaws@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation. either version 2 of the License,
11 * or (at your option) any later version
16 #include <media/v4l2-ioctl.h>
17 #include <linux/videodev2.h>
19 #include <linux/module.h>
20 #include <linux/version.h>
21 #include <linux/timer.h>
22 #include <media/videobuf2-dma-contig.h>
24 static int find_reg_callback(struct device *dev, void *p)
26 struct v4l2_subdev **sd = p;
28 *sd = dev_get_drvdata(dev);
29 /* non-zero value stops iteration */
33 static struct v4l2_subdev *find_and_register_subdev(
34 struct mxr_device *mdev, char *module_name)
36 struct device_driver *drv;
37 struct v4l2_subdev *sd = NULL;
40 /* TODO: add waiting until probe is finished */
41 drv = driver_find(module_name, &platform_bus_type);
43 mxr_warn(mdev, "module %s is missing\n", module_name);
46 /* driver refcnt is increased, it is safe to iterate over devices */
47 ret = driver_for_each_device(drv, NULL, &sd, find_reg_callback);
48 /* ret == 0 means that find_reg_callback was never executed */
50 mxr_warn(mdev, "module %s provides no subdev!\n", module_name);
53 /* v4l2_device_register_subdev detects if sd is NULL */
54 ret = v4l2_device_register_subdev(&mdev->v4l2_dev, sd);
56 mxr_warn(mdev, "failed to register subdev %s\n", sd->name);
64 int __devinit mxr_acquire_video(struct mxr_device *mdev,
65 struct mxr_output_conf *output_conf, int output_count)
67 struct device *dev = mdev->dev;
68 struct v4l2_device *v4l2_dev = &mdev->v4l2_dev;
71 struct v4l2_subdev *sd;
73 strlcpy(v4l2_dev->name, dev_name(mdev->dev), sizeof(v4l2_dev->name));
74 /* prepare context for V4L2 device */
75 ret = v4l2_device_register(dev, v4l2_dev);
77 mxr_err(mdev, "could not register v4l2 device.\n");
81 mdev->alloc_ctx = vb2_dma_contig_init_ctx(mdev->dev);
82 if (IS_ERR_OR_NULL(mdev->alloc_ctx)) {
83 mxr_err(mdev, "could not acquire vb2 allocator\n");
87 /* registering outputs */
89 for (i = 0; i < output_count; ++i) {
90 struct mxr_output_conf *conf = &output_conf[i];
91 struct mxr_output *out;
93 sd = find_and_register_subdev(mdev, conf->module_name);
94 /* trying to register next output */
97 out = kzalloc(sizeof *out, GFP_KERNEL);
99 mxr_err(mdev, "no memory for '%s'\n",
102 /* registered subdevs are removed in fail_v4l2_dev */
105 strlcpy(out->name, conf->output_name, sizeof(out->name));
107 out->cookie = conf->cookie;
108 mdev->output[mdev->output_cnt++] = out;
109 mxr_info(mdev, "added output '%s' from module '%s'\n",
110 conf->output_name, conf->module_name);
111 /* checking if maximal number of outputs is reached */
112 if (mdev->output_cnt >= MXR_MAX_OUTPUTS)
116 if (mdev->output_cnt == 0) {
117 mxr_err(mdev, "failed to register any output\n");
119 /* skipping fail_output because there is nothing to free */
120 goto fail_vb2_allocator;
126 /* kfree is NULL-safe */
127 for (i = 0; i < mdev->output_cnt; ++i)
128 kfree(mdev->output[i]);
129 memset(mdev->output, 0, sizeof mdev->output);
132 /* freeing allocator context */
133 vb2_dma_contig_cleanup_ctx(mdev->alloc_ctx);
136 /* NOTE: automatically unregister all subdevs */
137 v4l2_device_unregister(v4l2_dev);
143 void mxr_release_video(struct mxr_device *mdev)
147 /* kfree is NULL-safe */
148 for (i = 0; i < mdev->output_cnt; ++i)
149 kfree(mdev->output[i]);
151 vb2_dma_contig_cleanup_ctx(mdev->alloc_ctx);
152 v4l2_device_unregister(&mdev->v4l2_dev);
155 static int mxr_querycap(struct file *file, void *priv,
156 struct v4l2_capability *cap)
158 struct mxr_layer *layer = video_drvdata(file);
160 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
162 strlcpy(cap->driver, MXR_DRIVER_NAME, sizeof cap->driver);
163 strlcpy(cap->card, layer->vfd.name, sizeof cap->card);
164 sprintf(cap->bus_info, "%d", layer->idx);
165 cap->version = KERNEL_VERSION(0, 1, 0);
166 cap->capabilities = V4L2_CAP_STREAMING |
167 V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
172 static void mxr_geometry_dump(struct mxr_device *mdev, struct mxr_geometry *geo)
174 mxr_dbg(mdev, "src.full_size = (%u, %u)\n",
175 geo->src.full_width, geo->src.full_height);
176 mxr_dbg(mdev, "src.size = (%u, %u)\n",
177 geo->src.width, geo->src.height);
178 mxr_dbg(mdev, "src.offset = (%u, %u)\n",
179 geo->src.x_offset, geo->src.y_offset);
180 mxr_dbg(mdev, "dst.full_size = (%u, %u)\n",
181 geo->dst.full_width, geo->dst.full_height);
182 mxr_dbg(mdev, "dst.size = (%u, %u)\n",
183 geo->dst.width, geo->dst.height);
184 mxr_dbg(mdev, "dst.offset = (%u, %u)\n",
185 geo->dst.x_offset, geo->dst.y_offset);
186 mxr_dbg(mdev, "ratio = (%u, %u)\n",
187 geo->x_ratio, geo->y_ratio);
190 static void mxr_layer_default_geo(struct mxr_layer *layer)
192 struct mxr_device *mdev = layer->mdev;
193 struct v4l2_mbus_framefmt mbus_fmt;
195 memset(&layer->geo, 0, sizeof layer->geo);
197 mxr_get_mbus_fmt(mdev, &mbus_fmt);
199 layer->geo.dst.full_width = mbus_fmt.width;
200 layer->geo.dst.full_height = mbus_fmt.height;
201 layer->geo.dst.width = layer->geo.dst.full_width;
202 layer->geo.dst.height = layer->geo.dst.full_height;
203 layer->geo.dst.field = mbus_fmt.field;
205 layer->geo.src.full_width = mbus_fmt.width;
206 layer->geo.src.full_height = mbus_fmt.height;
207 layer->geo.src.width = layer->geo.src.full_width;
208 layer->geo.src.height = layer->geo.src.full_height;
210 mxr_geometry_dump(mdev, &layer->geo);
211 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SINK, 0);
212 mxr_geometry_dump(mdev, &layer->geo);
215 static void mxr_layer_update_output(struct mxr_layer *layer)
217 struct mxr_device *mdev = layer->mdev;
218 struct v4l2_mbus_framefmt mbus_fmt;
220 mxr_get_mbus_fmt(mdev, &mbus_fmt);
221 /* checking if update is needed */
222 if (layer->geo.dst.full_width == mbus_fmt.width &&
223 layer->geo.dst.full_height == mbus_fmt.width)
226 layer->geo.dst.full_width = mbus_fmt.width;
227 layer->geo.dst.full_height = mbus_fmt.height;
228 layer->geo.dst.field = mbus_fmt.field;
229 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SINK, 0);
231 mxr_geometry_dump(mdev, &layer->geo);
234 static const struct mxr_format *find_format_by_fourcc(
235 struct mxr_layer *layer, unsigned long fourcc);
236 static const struct mxr_format *find_format_by_index(
237 struct mxr_layer *layer, unsigned long index);
239 static int mxr_enum_fmt(struct file *file, void *priv,
240 struct v4l2_fmtdesc *f)
242 struct mxr_layer *layer = video_drvdata(file);
243 struct mxr_device *mdev = layer->mdev;
244 const struct mxr_format *fmt;
246 mxr_dbg(mdev, "%s\n", __func__);
247 fmt = find_format_by_index(layer, f->index);
251 strlcpy(f->description, fmt->name, sizeof(f->description));
252 f->pixelformat = fmt->fourcc;
257 static unsigned int divup(unsigned int divident, unsigned int divisor)
259 return (divident + divisor - 1) / divisor;
262 unsigned long mxr_get_plane_size(const struct mxr_block *blk,
263 unsigned int width, unsigned int height)
265 unsigned int bl_width = divup(width, blk->width);
266 unsigned int bl_height = divup(height, blk->height);
268 return bl_width * bl_height * blk->size;
271 static void mxr_mplane_fill(struct v4l2_plane_pix_format *planes,
272 const struct mxr_format *fmt, u32 width, u32 height)
276 /* checking if nothing to fill */
280 memset(planes, 0, sizeof(*planes) * fmt->num_subframes);
281 for (i = 0; i < fmt->num_planes; ++i) {
282 struct v4l2_plane_pix_format *plane = planes
283 + fmt->plane2subframe[i];
284 const struct mxr_block *blk = &fmt->plane[i];
285 u32 bl_width = divup(width, blk->width);
286 u32 bl_height = divup(height, blk->height);
287 u32 sizeimage = bl_width * bl_height * blk->size;
288 u16 bytesperline = bl_width * blk->size / blk->height;
290 plane->sizeimage += sizeimage;
291 plane->bytesperline = max(plane->bytesperline, bytesperline);
295 static int mxr_g_fmt(struct file *file, void *priv,
296 struct v4l2_format *f)
298 struct mxr_layer *layer = video_drvdata(file);
299 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
301 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
303 pix->width = layer->geo.src.full_width;
304 pix->height = layer->geo.src.full_height;
305 pix->field = V4L2_FIELD_NONE;
306 pix->pixelformat = layer->fmt->fourcc;
307 pix->colorspace = layer->fmt->colorspace;
308 mxr_mplane_fill(pix->plane_fmt, layer->fmt, pix->width, pix->height);
313 static int mxr_s_fmt(struct file *file, void *priv,
314 struct v4l2_format *f)
316 struct mxr_layer *layer = video_drvdata(file);
317 const struct mxr_format *fmt;
318 struct v4l2_pix_format_mplane *pix;
319 struct mxr_device *mdev = layer->mdev;
320 struct mxr_geometry *geo = &layer->geo;
322 mxr_dbg(mdev, "%s:%d\n", __func__, __LINE__);
324 pix = &f->fmt.pix_mp;
325 fmt = find_format_by_fourcc(layer, pix->pixelformat);
327 mxr_warn(mdev, "not recognized fourcc: %08x\n",
332 /* set source size to highest accepted value */
333 geo->src.full_width = max(geo->dst.full_width, pix->width);
334 geo->src.full_height = max(geo->dst.full_height, pix->height);
335 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SOURCE, 0);
336 mxr_geometry_dump(mdev, &layer->geo);
337 /* set cropping to total visible screen */
338 geo->src.width = pix->width;
339 geo->src.height = pix->height;
340 geo->src.x_offset = 0;
341 geo->src.y_offset = 0;
342 /* assure consistency of geometry */
343 layer->ops.fix_geometry(layer, MXR_GEOMETRY_CROP, MXR_NO_OFFSET);
344 mxr_geometry_dump(mdev, &layer->geo);
345 /* set full size to lowest possible value */
346 geo->src.full_width = 0;
347 geo->src.full_height = 0;
348 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SOURCE, 0);
349 mxr_geometry_dump(mdev, &layer->geo);
351 /* returning results */
352 mxr_g_fmt(file, priv, f);
357 static int mxr_g_selection(struct file *file, void *fh,
358 struct v4l2_selection *s)
360 struct mxr_layer *layer = video_drvdata(file);
361 struct mxr_geometry *geo = &layer->geo;
363 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
365 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
366 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
370 case V4L2_SEL_TGT_CROP_ACTIVE:
371 s->r.left = geo->src.x_offset;
372 s->r.top = geo->src.y_offset;
373 s->r.width = geo->src.width;
374 s->r.height = geo->src.height;
376 case V4L2_SEL_TGT_CROP_DEFAULT:
377 case V4L2_SEL_TGT_CROP_BOUNDS:
380 s->r.width = geo->src.full_width;
381 s->r.height = geo->src.full_height;
383 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
384 case V4L2_SEL_TGT_COMPOSE_PADDED:
385 s->r.left = geo->dst.x_offset;
386 s->r.top = geo->dst.y_offset;
387 s->r.width = geo->dst.width;
388 s->r.height = geo->dst.height;
390 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
391 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
394 s->r.width = geo->dst.full_width;
395 s->r.height = geo->dst.full_height;
404 /* returns 1 if rectangle 'a' is inside 'b' */
405 static int mxr_is_rect_inside(struct v4l2_rect *a, struct v4l2_rect *b)
407 if (a->left < b->left)
411 if (a->left + a->width > b->left + b->width)
413 if (a->top + a->height > b->top + b->height)
418 static int mxr_s_selection(struct file *file, void *fh,
419 struct v4l2_selection *s)
421 struct mxr_layer *layer = video_drvdata(file);
422 struct mxr_geometry *geo = &layer->geo;
423 struct mxr_crop *target = NULL;
424 enum mxr_geometry_stage stage;
425 struct mxr_geometry tmp;
426 struct v4l2_rect res;
428 memset(&res, 0, sizeof res);
430 mxr_dbg(layer->mdev, "%s: rect: %dx%d@%d,%d\n", __func__,
431 s->r.width, s->r.height, s->r.left, s->r.top);
433 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
434 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
438 /* ignore read-only targets */
439 case V4L2_SEL_TGT_CROP_DEFAULT:
440 case V4L2_SEL_TGT_CROP_BOUNDS:
441 res.width = geo->src.full_width;
442 res.height = geo->src.full_height;
445 /* ignore read-only targets */
446 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
447 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
448 res.width = geo->dst.full_width;
449 res.height = geo->dst.full_height;
452 case V4L2_SEL_TGT_CROP_ACTIVE:
454 stage = MXR_GEOMETRY_CROP;
456 case V4L2_SEL_TGT_COMPOSE_ACTIVE:
457 case V4L2_SEL_TGT_COMPOSE_PADDED:
459 stage = MXR_GEOMETRY_COMPOSE;
464 /* apply change and update geometry if needed */
466 /* backup current geometry if setup fails */
467 memcpy(&tmp, geo, sizeof tmp);
469 /* apply requested selection */
470 target->x_offset = s->r.left;
471 target->y_offset = s->r.top;
472 target->width = s->r.width;
473 target->height = s->r.height;
475 layer->ops.fix_geometry(layer, stage, s->flags);
477 /* retrieve update selection rectangle */
478 res.left = target->x_offset;
479 res.top = target->y_offset;
480 res.width = target->width;
481 res.height = target->height;
483 mxr_geometry_dump(layer->mdev, &layer->geo);
486 /* checking if the rectangle satisfies constraints */
487 if ((s->flags & V4L2_SEL_FLAG_LE) && !mxr_is_rect_inside(&res, &s->r))
489 if ((s->flags & V4L2_SEL_FLAG_GE) && !mxr_is_rect_inside(&s->r, &res))
492 /* return result rectangle */
497 /* restore old geometry, which is not touched if target is NULL */
499 memcpy(geo, &tmp, sizeof tmp);
503 static int mxr_enum_dv_presets(struct file *file, void *fh,
504 struct v4l2_dv_enum_preset *preset)
506 struct mxr_layer *layer = video_drvdata(file);
507 struct mxr_device *mdev = layer->mdev;
510 /* lock protects from changing sd_out */
511 mutex_lock(&mdev->mutex);
512 ret = v4l2_subdev_call(to_outsd(mdev), video, enum_dv_presets, preset);
513 mutex_unlock(&mdev->mutex);
515 return ret ? -EINVAL : 0;
518 static int mxr_s_dv_preset(struct file *file, void *fh,
519 struct v4l2_dv_preset *preset)
521 struct mxr_layer *layer = video_drvdata(file);
522 struct mxr_device *mdev = layer->mdev;
525 /* lock protects from changing sd_out */
526 mutex_lock(&mdev->mutex);
528 /* preset change cannot be done while there is an entity
529 * dependant on output configuration
531 if (mdev->n_output > 0) {
532 mutex_unlock(&mdev->mutex);
536 ret = v4l2_subdev_call(to_outsd(mdev), video, s_dv_preset, preset);
538 mutex_unlock(&mdev->mutex);
540 mxr_layer_update_output(layer);
542 /* any failure should return EINVAL according to V4L2 doc */
543 return ret ? -EINVAL : 0;
546 static int mxr_g_dv_preset(struct file *file, void *fh,
547 struct v4l2_dv_preset *preset)
549 struct mxr_layer *layer = video_drvdata(file);
550 struct mxr_device *mdev = layer->mdev;
553 /* lock protects from changing sd_out */
554 mutex_lock(&mdev->mutex);
555 ret = v4l2_subdev_call(to_outsd(mdev), video, g_dv_preset, preset);
556 mutex_unlock(&mdev->mutex);
558 return ret ? -EINVAL : 0;
561 static int mxr_s_std(struct file *file, void *fh, v4l2_std_id *norm)
563 struct mxr_layer *layer = video_drvdata(file);
564 struct mxr_device *mdev = layer->mdev;
567 /* lock protects from changing sd_out */
568 mutex_lock(&mdev->mutex);
570 /* standard change cannot be done while there is an entity
571 * dependant on output configuration
573 if (mdev->n_output > 0) {
574 mutex_unlock(&mdev->mutex);
578 ret = v4l2_subdev_call(to_outsd(mdev), video, s_std_output, *norm);
580 mutex_unlock(&mdev->mutex);
582 mxr_layer_update_output(layer);
584 return ret ? -EINVAL : 0;
587 static int mxr_g_std(struct file *file, void *fh, v4l2_std_id *norm)
589 struct mxr_layer *layer = video_drvdata(file);
590 struct mxr_device *mdev = layer->mdev;
593 /* lock protects from changing sd_out */
594 mutex_lock(&mdev->mutex);
595 ret = v4l2_subdev_call(to_outsd(mdev), video, g_std_output, norm);
596 mutex_unlock(&mdev->mutex);
598 return ret ? -EINVAL : 0;
601 static int mxr_enum_output(struct file *file, void *fh, struct v4l2_output *a)
603 struct mxr_layer *layer = video_drvdata(file);
604 struct mxr_device *mdev = layer->mdev;
605 struct mxr_output *out;
606 struct v4l2_subdev *sd;
608 if (a->index >= mdev->output_cnt)
610 out = mdev->output[a->index];
613 strlcpy(a->name, out->name, sizeof(a->name));
615 /* try to obtain supported tv norms */
616 v4l2_subdev_call(sd, video, g_tvnorms_output, &a->std);
618 if (sd->ops->video && sd->ops->video->s_dv_preset)
619 a->capabilities |= V4L2_OUT_CAP_PRESETS;
620 if (sd->ops->video && sd->ops->video->s_std_output)
621 a->capabilities |= V4L2_OUT_CAP_STD;
622 a->type = V4L2_OUTPUT_TYPE_ANALOG;
627 static int mxr_s_output(struct file *file, void *fh, unsigned int i)
629 struct video_device *vfd = video_devdata(file);
630 struct mxr_layer *layer = video_drvdata(file);
631 struct mxr_device *mdev = layer->mdev;
633 if (i >= mdev->output_cnt || mdev->output[i] == NULL)
636 mutex_lock(&mdev->mutex);
637 if (mdev->n_output > 0) {
638 mutex_unlock(&mdev->mutex);
641 mdev->current_output = i;
643 v4l2_subdev_call(to_outsd(mdev), video, g_tvnorms_output,
645 mutex_unlock(&mdev->mutex);
647 /* update layers geometry */
648 mxr_layer_update_output(layer);
650 mxr_dbg(mdev, "tvnorms = %08llx\n", vfd->tvnorms);
655 static int mxr_g_output(struct file *file, void *fh, unsigned int *p)
657 struct mxr_layer *layer = video_drvdata(file);
658 struct mxr_device *mdev = layer->mdev;
660 mutex_lock(&mdev->mutex);
661 *p = mdev->current_output;
662 mutex_unlock(&mdev->mutex);
667 static int mxr_reqbufs(struct file *file, void *priv,
668 struct v4l2_requestbuffers *p)
670 struct mxr_layer *layer = video_drvdata(file);
672 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
673 return vb2_reqbufs(&layer->vb_queue, p);
676 static int mxr_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
678 struct mxr_layer *layer = video_drvdata(file);
680 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
681 return vb2_querybuf(&layer->vb_queue, p);
684 static int mxr_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
686 struct mxr_layer *layer = video_drvdata(file);
688 mxr_dbg(layer->mdev, "%s:%d(%d)\n", __func__, __LINE__, p->index);
689 return vb2_qbuf(&layer->vb_queue, p);
692 static int mxr_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
694 struct mxr_layer *layer = video_drvdata(file);
696 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
697 return vb2_dqbuf(&layer->vb_queue, p, file->f_flags & O_NONBLOCK);
700 static int mxr_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
702 struct mxr_layer *layer = video_drvdata(file);
704 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
705 return vb2_streamon(&layer->vb_queue, i);
708 static int mxr_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
710 struct mxr_layer *layer = video_drvdata(file);
712 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
713 return vb2_streamoff(&layer->vb_queue, i);
716 static const struct v4l2_ioctl_ops mxr_ioctl_ops = {
717 .vidioc_querycap = mxr_querycap,
718 /* format handling */
719 .vidioc_enum_fmt_vid_out = mxr_enum_fmt,
720 .vidioc_s_fmt_vid_out_mplane = mxr_s_fmt,
721 .vidioc_g_fmt_vid_out_mplane = mxr_g_fmt,
723 .vidioc_reqbufs = mxr_reqbufs,
724 .vidioc_querybuf = mxr_querybuf,
725 .vidioc_qbuf = mxr_qbuf,
726 .vidioc_dqbuf = mxr_dqbuf,
727 /* Streaming control */
728 .vidioc_streamon = mxr_streamon,
729 .vidioc_streamoff = mxr_streamoff,
730 /* Preset functions */
731 .vidioc_enum_dv_presets = mxr_enum_dv_presets,
732 .vidioc_s_dv_preset = mxr_s_dv_preset,
733 .vidioc_g_dv_preset = mxr_g_dv_preset,
734 /* analog TV standard functions */
735 .vidioc_s_std = mxr_s_std,
736 .vidioc_g_std = mxr_g_std,
737 /* Output handling */
738 .vidioc_enum_output = mxr_enum_output,
739 .vidioc_s_output = mxr_s_output,
740 .vidioc_g_output = mxr_g_output,
741 /* selection ioctls */
742 .vidioc_g_selection = mxr_g_selection,
743 .vidioc_s_selection = mxr_s_selection,
746 static int mxr_video_open(struct file *file)
748 struct mxr_layer *layer = video_drvdata(file);
749 struct mxr_device *mdev = layer->mdev;
752 mxr_dbg(mdev, "%s:%d\n", __func__, __LINE__);
753 /* assure device probe is finished */
754 wait_for_device_probe();
755 /* creating context for file descriptor */
756 ret = v4l2_fh_open(file);
758 mxr_err(mdev, "v4l2_fh_open failed\n");
762 /* leaving if layer is already initialized */
763 if (!v4l2_fh_is_singular_file(file))
766 /* FIXME: should power be enabled on open? */
767 ret = mxr_power_get(mdev);
769 mxr_err(mdev, "power on failed\n");
773 ret = vb2_queue_init(&layer->vb_queue);
775 mxr_err(mdev, "failed to initialize vb2 queue\n");
778 /* set default format, first on the list */
779 layer->fmt = layer->fmt_array[0];
780 /* setup default geometry */
781 mxr_layer_default_geo(layer);
789 v4l2_fh_release(file);
795 mxr_video_poll(struct file *file, struct poll_table_struct *wait)
797 struct mxr_layer *layer = video_drvdata(file);
799 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
801 return vb2_poll(&layer->vb_queue, file, wait);
804 static int mxr_video_mmap(struct file *file, struct vm_area_struct *vma)
806 struct mxr_layer *layer = video_drvdata(file);
808 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
810 return vb2_mmap(&layer->vb_queue, vma);
813 static int mxr_video_release(struct file *file)
815 struct mxr_layer *layer = video_drvdata(file);
817 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
818 if (v4l2_fh_is_singular_file(file)) {
819 vb2_queue_release(&layer->vb_queue);
820 mxr_power_put(layer->mdev);
822 v4l2_fh_release(file);
826 static const struct v4l2_file_operations mxr_fops = {
827 .owner = THIS_MODULE,
828 .open = mxr_video_open,
829 .poll = mxr_video_poll,
830 .mmap = mxr_video_mmap,
831 .release = mxr_video_release,
832 .unlocked_ioctl = video_ioctl2,
835 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
836 unsigned int *nbuffers, unsigned int *nplanes, unsigned int sizes[],
839 struct mxr_layer *layer = vb2_get_drv_priv(vq);
840 const struct mxr_format *fmt = layer->fmt;
842 struct mxr_device *mdev = layer->mdev;
843 struct v4l2_plane_pix_format planes[3];
845 mxr_dbg(mdev, "%s\n", __func__);
846 /* checking if format was configured */
849 mxr_dbg(mdev, "fmt = %s\n", fmt->name);
850 mxr_mplane_fill(planes, fmt, layer->geo.src.full_width,
851 layer->geo.src.full_height);
853 *nplanes = fmt->num_subframes;
854 for (i = 0; i < fmt->num_subframes; ++i) {
855 alloc_ctxs[i] = layer->mdev->alloc_ctx;
856 sizes[i] = planes[i].sizeimage;
857 mxr_dbg(mdev, "size[%d] = %08x\n", i, sizes[i]);
866 static void buf_queue(struct vb2_buffer *vb)
868 struct mxr_buffer *buffer = container_of(vb, struct mxr_buffer, vb);
869 struct mxr_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
870 struct mxr_device *mdev = layer->mdev;
873 spin_lock_irqsave(&layer->enq_slock, flags);
874 list_add_tail(&buffer->list, &layer->enq_list);
875 spin_unlock_irqrestore(&layer->enq_slock, flags);
877 mxr_dbg(mdev, "queuing buffer\n");
880 static void wait_lock(struct vb2_queue *vq)
882 struct mxr_layer *layer = vb2_get_drv_priv(vq);
884 mxr_dbg(layer->mdev, "%s\n", __func__);
885 mutex_lock(&layer->mutex);
888 static void wait_unlock(struct vb2_queue *vq)
890 struct mxr_layer *layer = vb2_get_drv_priv(vq);
892 mxr_dbg(layer->mdev, "%s\n", __func__);
893 mutex_unlock(&layer->mutex);
896 static int start_streaming(struct vb2_queue *vq, unsigned int count)
898 struct mxr_layer *layer = vb2_get_drv_priv(vq);
899 struct mxr_device *mdev = layer->mdev;
902 mxr_dbg(mdev, "%s\n", __func__);
905 mxr_dbg(mdev, "no output buffers queued\n");
909 /* block any changes in output configuration */
910 mxr_output_get(mdev);
912 mxr_layer_update_output(layer);
913 layer->ops.format_set(layer);
914 /* enabling layer in hardware */
915 spin_lock_irqsave(&layer->enq_slock, flags);
916 layer->state = MXR_LAYER_STREAMING;
917 spin_unlock_irqrestore(&layer->enq_slock, flags);
919 layer->ops.stream_set(layer, MXR_ENABLE);
920 mxr_streamer_get(mdev);
925 static void mxr_watchdog(unsigned long arg)
927 struct mxr_layer *layer = (struct mxr_layer *) arg;
928 struct mxr_device *mdev = layer->mdev;
931 mxr_err(mdev, "watchdog fired for layer %s\n", layer->vfd.name);
933 spin_lock_irqsave(&layer->enq_slock, flags);
935 if (layer->update_buf == layer->shadow_buf)
936 layer->update_buf = NULL;
937 if (layer->update_buf) {
938 vb2_buffer_done(&layer->update_buf->vb, VB2_BUF_STATE_ERROR);
939 layer->update_buf = NULL;
941 if (layer->shadow_buf) {
942 vb2_buffer_done(&layer->shadow_buf->vb, VB2_BUF_STATE_ERROR);
943 layer->shadow_buf = NULL;
945 spin_unlock_irqrestore(&layer->enq_slock, flags);
948 static int stop_streaming(struct vb2_queue *vq)
950 struct mxr_layer *layer = vb2_get_drv_priv(vq);
951 struct mxr_device *mdev = layer->mdev;
953 struct timer_list watchdog;
954 struct mxr_buffer *buf, *buf_tmp;
956 mxr_dbg(mdev, "%s\n", __func__);
958 spin_lock_irqsave(&layer->enq_slock, flags);
961 layer->state = MXR_LAYER_STREAMING_FINISH;
963 /* set all buffer to be done */
964 list_for_each_entry_safe(buf, buf_tmp, &layer->enq_list, list) {
965 list_del(&buf->list);
966 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
969 spin_unlock_irqrestore(&layer->enq_slock, flags);
971 /* give 1 seconds to complete to complete last buffers */
972 setup_timer_on_stack(&watchdog, mxr_watchdog,
973 (unsigned long)layer);
974 mod_timer(&watchdog, jiffies + msecs_to_jiffies(1000));
976 /* wait until all buffers are goes to done state */
977 vb2_wait_for_all_buffers(vq);
979 /* stop timer if all synchronization is done */
980 del_timer_sync(&watchdog);
981 destroy_timer_on_stack(&watchdog);
983 /* stopping hardware */
984 spin_lock_irqsave(&layer->enq_slock, flags);
985 layer->state = MXR_LAYER_IDLE;
986 spin_unlock_irqrestore(&layer->enq_slock, flags);
988 /* disabling layer in hardware */
989 layer->ops.stream_set(layer, MXR_DISABLE);
990 /* remove one streamer */
991 mxr_streamer_put(mdev);
992 /* allow changes in output configuration */
993 mxr_output_put(mdev);
997 static struct vb2_ops mxr_video_qops = {
998 .queue_setup = queue_setup,
999 .buf_queue = buf_queue,
1000 .wait_prepare = wait_unlock,
1001 .wait_finish = wait_lock,
1002 .start_streaming = start_streaming,
1003 .stop_streaming = stop_streaming,
1006 /* FIXME: try to put this functions to mxr_base_layer_create */
1007 int mxr_base_layer_register(struct mxr_layer *layer)
1009 struct mxr_device *mdev = layer->mdev;
1012 ret = video_register_device(&layer->vfd, VFL_TYPE_GRABBER, -1);
1014 mxr_err(mdev, "failed to register video device\n");
1016 mxr_info(mdev, "registered layer %s as /dev/video%d\n",
1017 layer->vfd.name, layer->vfd.num);
1021 void mxr_base_layer_unregister(struct mxr_layer *layer)
1023 video_unregister_device(&layer->vfd);
1026 void mxr_layer_release(struct mxr_layer *layer)
1028 if (layer->ops.release)
1029 layer->ops.release(layer);
1032 void mxr_base_layer_release(struct mxr_layer *layer)
1037 static void mxr_vfd_release(struct video_device *vdev)
1039 printk(KERN_INFO "video device release\n");
1042 struct mxr_layer *mxr_base_layer_create(struct mxr_device *mdev,
1043 int idx, char *name, struct mxr_layer_ops *ops)
1045 struct mxr_layer *layer;
1047 layer = kzalloc(sizeof *layer, GFP_KERNEL);
1048 if (layer == NULL) {
1049 mxr_err(mdev, "not enough memory for layer.\n");
1057 spin_lock_init(&layer->enq_slock);
1058 INIT_LIST_HEAD(&layer->enq_list);
1059 mutex_init(&layer->mutex);
1061 layer->vfd = (struct video_device) {
1063 .release = mxr_vfd_release,
1065 .ioctl_ops = &mxr_ioctl_ops,
1067 strlcpy(layer->vfd.name, name, sizeof(layer->vfd.name));
1068 /* let framework control PRIORITY */
1069 set_bit(V4L2_FL_USE_FH_PRIO, &layer->vfd.flags);
1071 video_set_drvdata(&layer->vfd, layer);
1072 /* Locking in file operations other than ioctl should be done
1073 by the driver, not the V4L2 core.
1074 This driver needs auditing so that this flag can be removed. */
1075 set_bit(V4L2_FL_LOCK_ALL_FOPS, &layer->vfd.flags);
1076 layer->vfd.lock = &layer->mutex;
1077 layer->vfd.v4l2_dev = &mdev->v4l2_dev;
1079 layer->vb_queue = (struct vb2_queue) {
1080 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
1081 .io_modes = VB2_MMAP | VB2_USERPTR,
1083 .buf_struct_size = sizeof(struct mxr_buffer),
1084 .ops = &mxr_video_qops,
1085 .mem_ops = &vb2_dma_contig_memops,
1094 static const struct mxr_format *find_format_by_fourcc(
1095 struct mxr_layer *layer, unsigned long fourcc)
1099 for (i = 0; i < layer->fmt_array_size; ++i)
1100 if (layer->fmt_array[i]->fourcc == fourcc)
1101 return layer->fmt_array[i];
1105 static const struct mxr_format *find_format_by_index(
1106 struct mxr_layer *layer, unsigned long index)
1108 if (index >= layer->fmt_array_size)
1110 return layer->fmt_array[index];