1
2
3
4
5
6
7
8
9
10
11
12
13
14#define pr_fmt(fmt) "s5p-tv (mixer): " fmt
15
16#include "mixer.h"
17
18#include <media/v4l2-ioctl.h>
19#include <linux/videodev2.h>
20#include <linux/mm.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/timer.h>
24#include <media/videobuf2-dma-contig.h>
25
26static int find_reg_callback(struct device *dev, void *p)
27{
28 struct v4l2_subdev **sd = p;
29
30 *sd = dev_get_drvdata(dev);
31
32 return 1;
33}
34
35static struct v4l2_subdev *find_and_register_subdev(
36 struct mxr_device *mdev, char *module_name)
37{
38 struct device_driver *drv;
39 struct v4l2_subdev *sd = NULL;
40 int ret;
41
42
43 drv = driver_find(module_name, &platform_bus_type);
44 if (!drv) {
45 mxr_warn(mdev, "module %s is missing\n", module_name);
46 return NULL;
47 }
48
49 ret = driver_for_each_device(drv, NULL, &sd, find_reg_callback);
50
51 if (sd == NULL) {
52 mxr_warn(mdev, "module %s provides no subdev!\n", module_name);
53 goto done;
54 }
55
56 ret = v4l2_device_register_subdev(&mdev->v4l2_dev, sd);
57 if (ret) {
58 mxr_warn(mdev, "failed to register subdev %s\n", sd->name);
59 sd = NULL;
60 }
61
62done:
63 return sd;
64}
65
66int mxr_acquire_video(struct mxr_device *mdev,
67 struct mxr_output_conf *output_conf, int output_count)
68{
69 struct device *dev = mdev->dev;
70 struct v4l2_device *v4l2_dev = &mdev->v4l2_dev;
71 int i;
72 int ret = 0;
73 struct v4l2_subdev *sd;
74
75 strlcpy(v4l2_dev->name, dev_name(mdev->dev), sizeof(v4l2_dev->name));
76
77 ret = v4l2_device_register(dev, v4l2_dev);
78 if (ret) {
79 mxr_err(mdev, "could not register v4l2 device.\n");
80 goto fail;
81 }
82
83 mdev->alloc_ctx = vb2_dma_contig_init_ctx(mdev->dev);
84 if (IS_ERR_OR_NULL(mdev->alloc_ctx)) {
85 mxr_err(mdev, "could not acquire vb2 allocator\n");
86 goto fail_v4l2_dev;
87 }
88
89
90 mdev->output_cnt = 0;
91 for (i = 0; i < output_count; ++i) {
92 struct mxr_output_conf *conf = &output_conf[i];
93 struct mxr_output *out;
94
95 sd = find_and_register_subdev(mdev, conf->module_name);
96
97 if (sd == NULL)
98 continue;
99 out = kzalloc(sizeof(*out), GFP_KERNEL);
100 if (out == NULL) {
101 mxr_err(mdev, "no memory for '%s'\n",
102 conf->output_name);
103 ret = -ENOMEM;
104
105 goto fail_output;
106 }
107 strlcpy(out->name, conf->output_name, sizeof(out->name));
108 out->sd = sd;
109 out->cookie = conf->cookie;
110 mdev->output[mdev->output_cnt++] = out;
111 mxr_info(mdev, "added output '%s' from module '%s'\n",
112 conf->output_name, conf->module_name);
113
114 if (mdev->output_cnt >= MXR_MAX_OUTPUTS)
115 break;
116 }
117
118 if (mdev->output_cnt == 0) {
119 mxr_err(mdev, "failed to register any output\n");
120 ret = -ENODEV;
121
122 goto fail_vb2_allocator;
123 }
124
125 return 0;
126
127fail_output:
128
129 for (i = 0; i < mdev->output_cnt; ++i)
130 kfree(mdev->output[i]);
131 memset(mdev->output, 0, sizeof(mdev->output));
132
133fail_vb2_allocator:
134
135 vb2_dma_contig_cleanup_ctx(mdev->alloc_ctx);
136
137fail_v4l2_dev:
138
139 v4l2_device_unregister(v4l2_dev);
140
141fail:
142 return ret;
143}
144
145void mxr_release_video(struct mxr_device *mdev)
146{
147 int i;
148
149
150 for (i = 0; i < mdev->output_cnt; ++i)
151 kfree(mdev->output[i]);
152
153 vb2_dma_contig_cleanup_ctx(mdev->alloc_ctx);
154 v4l2_device_unregister(&mdev->v4l2_dev);
155}
156
157static int mxr_querycap(struct file *file, void *priv,
158 struct v4l2_capability *cap)
159{
160 struct mxr_layer *layer = video_drvdata(file);
161
162 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
163
164 strlcpy(cap->driver, MXR_DRIVER_NAME, sizeof(cap->driver));
165 strlcpy(cap->card, layer->vfd.name, sizeof(cap->card));
166 sprintf(cap->bus_info, "%d", layer->idx);
167 cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
168 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
169
170 return 0;
171}
172
173static void mxr_geometry_dump(struct mxr_device *mdev, struct mxr_geometry *geo)
174{
175 mxr_dbg(mdev, "src.full_size = (%u, %u)\n",
176 geo->src.full_width, geo->src.full_height);
177 mxr_dbg(mdev, "src.size = (%u, %u)\n",
178 geo->src.width, geo->src.height);
179 mxr_dbg(mdev, "src.offset = (%u, %u)\n",
180 geo->src.x_offset, geo->src.y_offset);
181 mxr_dbg(mdev, "dst.full_size = (%u, %u)\n",
182 geo->dst.full_width, geo->dst.full_height);
183 mxr_dbg(mdev, "dst.size = (%u, %u)\n",
184 geo->dst.width, geo->dst.height);
185 mxr_dbg(mdev, "dst.offset = (%u, %u)\n",
186 geo->dst.x_offset, geo->dst.y_offset);
187 mxr_dbg(mdev, "ratio = (%u, %u)\n",
188 geo->x_ratio, geo->y_ratio);
189}
190
191static void mxr_layer_default_geo(struct mxr_layer *layer)
192{
193 struct mxr_device *mdev = layer->mdev;
194 struct v4l2_mbus_framefmt mbus_fmt;
195
196 memset(&layer->geo, 0, sizeof(layer->geo));
197
198 mxr_get_mbus_fmt(mdev, &mbus_fmt);
199
200 layer->geo.dst.full_width = mbus_fmt.width;
201 layer->geo.dst.full_height = mbus_fmt.height;
202 layer->geo.dst.width = layer->geo.dst.full_width;
203 layer->geo.dst.height = layer->geo.dst.full_height;
204 layer->geo.dst.field = mbus_fmt.field;
205
206 layer->geo.src.full_width = mbus_fmt.width;
207 layer->geo.src.full_height = mbus_fmt.height;
208 layer->geo.src.width = layer->geo.src.full_width;
209 layer->geo.src.height = layer->geo.src.full_height;
210
211 mxr_geometry_dump(mdev, &layer->geo);
212 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SINK, 0);
213 mxr_geometry_dump(mdev, &layer->geo);
214}
215
216static void mxr_layer_update_output(struct mxr_layer *layer)
217{
218 struct mxr_device *mdev = layer->mdev;
219 struct v4l2_mbus_framefmt mbus_fmt;
220
221 mxr_get_mbus_fmt(mdev, &mbus_fmt);
222
223 if (layer->geo.dst.full_width == mbus_fmt.width &&
224 layer->geo.dst.full_height == mbus_fmt.width)
225 return;
226
227 layer->geo.dst.full_width = mbus_fmt.width;
228 layer->geo.dst.full_height = mbus_fmt.height;
229 layer->geo.dst.field = mbus_fmt.field;
230 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SINK, 0);
231
232 mxr_geometry_dump(mdev, &layer->geo);
233}
234
235static const struct mxr_format *find_format_by_fourcc(
236 struct mxr_layer *layer, unsigned long fourcc);
237static const struct mxr_format *find_format_by_index(
238 struct mxr_layer *layer, unsigned long index);
239
240static int mxr_enum_fmt(struct file *file, void *priv,
241 struct v4l2_fmtdesc *f)
242{
243 struct mxr_layer *layer = video_drvdata(file);
244 struct mxr_device *mdev = layer->mdev;
245 const struct mxr_format *fmt;
246
247 mxr_dbg(mdev, "%s\n", __func__);
248 fmt = find_format_by_index(layer, f->index);
249 if (fmt == NULL)
250 return -EINVAL;
251
252 strlcpy(f->description, fmt->name, sizeof(f->description));
253 f->pixelformat = fmt->fourcc;
254
255 return 0;
256}
257
258static unsigned int divup(unsigned int divident, unsigned int divisor)
259{
260 return (divident + divisor - 1) / divisor;
261}
262
263unsigned long mxr_get_plane_size(const struct mxr_block *blk,
264 unsigned int width, unsigned int height)
265{
266 unsigned int bl_width = divup(width, blk->width);
267 unsigned int bl_height = divup(height, blk->height);
268
269 return bl_width * bl_height * blk->size;
270}
271
272static void mxr_mplane_fill(struct v4l2_plane_pix_format *planes,
273 const struct mxr_format *fmt, u32 width, u32 height)
274{
275 int i;
276
277
278 if (!planes)
279 return;
280
281 memset(planes, 0, sizeof(*planes) * fmt->num_subframes);
282 for (i = 0; i < fmt->num_planes; ++i) {
283 struct v4l2_plane_pix_format *plane = planes
284 + fmt->plane2subframe[i];
285 const struct mxr_block *blk = &fmt->plane[i];
286 u32 bl_width = divup(width, blk->width);
287 u32 bl_height = divup(height, blk->height);
288 u32 sizeimage = bl_width * bl_height * blk->size;
289 u16 bytesperline = bl_width * blk->size / blk->height;
290
291 plane->sizeimage += sizeimage;
292 plane->bytesperline = max(plane->bytesperline, bytesperline);
293 }
294}
295
296static int mxr_g_fmt(struct file *file, void *priv,
297 struct v4l2_format *f)
298{
299 struct mxr_layer *layer = video_drvdata(file);
300 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
301
302 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
303
304 pix->width = layer->geo.src.full_width;
305 pix->height = layer->geo.src.full_height;
306 pix->field = V4L2_FIELD_NONE;
307 pix->pixelformat = layer->fmt->fourcc;
308 pix->colorspace = layer->fmt->colorspace;
309 mxr_mplane_fill(pix->plane_fmt, layer->fmt, pix->width, pix->height);
310
311 return 0;
312}
313
314static int mxr_s_fmt(struct file *file, void *priv,
315 struct v4l2_format *f)
316{
317 struct mxr_layer *layer = video_drvdata(file);
318 const struct mxr_format *fmt;
319 struct v4l2_pix_format_mplane *pix;
320 struct mxr_device *mdev = layer->mdev;
321 struct mxr_geometry *geo = &layer->geo;
322
323 mxr_dbg(mdev, "%s:%d\n", __func__, __LINE__);
324
325 pix = &f->fmt.pix_mp;
326 fmt = find_format_by_fourcc(layer, pix->pixelformat);
327 if (fmt == NULL) {
328 mxr_warn(mdev, "not recognized fourcc: %08x\n",
329 pix->pixelformat);
330 return -EINVAL;
331 }
332 layer->fmt = fmt;
333
334 geo->src.full_width = max(geo->dst.full_width, pix->width);
335 geo->src.full_height = max(geo->dst.full_height, pix->height);
336 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SOURCE, 0);
337 mxr_geometry_dump(mdev, &layer->geo);
338
339 geo->src.width = pix->width;
340 geo->src.height = pix->height;
341 geo->src.x_offset = 0;
342 geo->src.y_offset = 0;
343
344 layer->ops.fix_geometry(layer, MXR_GEOMETRY_CROP, MXR_NO_OFFSET);
345 mxr_geometry_dump(mdev, &layer->geo);
346
347 geo->src.full_width = 0;
348 geo->src.full_height = 0;
349 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SOURCE, 0);
350 mxr_geometry_dump(mdev, &layer->geo);
351
352
353 mxr_g_fmt(file, priv, f);
354
355 return 0;
356}
357
358static int mxr_g_selection(struct file *file, void *fh,
359 struct v4l2_selection *s)
360{
361 struct mxr_layer *layer = video_drvdata(file);
362 struct mxr_geometry *geo = &layer->geo;
363
364 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
365
366 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
367 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
368 return -EINVAL;
369
370 switch (s->target) {
371 case V4L2_SEL_TGT_CROP:
372 s->r.left = geo->src.x_offset;
373 s->r.top = geo->src.y_offset;
374 s->r.width = geo->src.width;
375 s->r.height = geo->src.height;
376 break;
377 case V4L2_SEL_TGT_CROP_DEFAULT:
378 case V4L2_SEL_TGT_CROP_BOUNDS:
379 s->r.left = 0;
380 s->r.top = 0;
381 s->r.width = geo->src.full_width;
382 s->r.height = geo->src.full_height;
383 break;
384 case V4L2_SEL_TGT_COMPOSE:
385 case V4L2_SEL_TGT_COMPOSE_PADDED:
386 s->r.left = geo->dst.x_offset;
387 s->r.top = geo->dst.y_offset;
388 s->r.width = geo->dst.width;
389 s->r.height = geo->dst.height;
390 break;
391 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
392 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
393 s->r.left = 0;
394 s->r.top = 0;
395 s->r.width = geo->dst.full_width;
396 s->r.height = geo->dst.full_height;
397 break;
398 default:
399 return -EINVAL;
400 }
401
402 return 0;
403}
404
405
406static int mxr_is_rect_inside(struct v4l2_rect *a, struct v4l2_rect *b)
407{
408 if (a->left < b->left)
409 return 0;
410 if (a->top < b->top)
411 return 0;
412 if (a->left + a->width > b->left + b->width)
413 return 0;
414 if (a->top + a->height > b->top + b->height)
415 return 0;
416 return 1;
417}
418
419static int mxr_s_selection(struct file *file, void *fh,
420 struct v4l2_selection *s)
421{
422 struct mxr_layer *layer = video_drvdata(file);
423 struct mxr_geometry *geo = &layer->geo;
424 struct mxr_crop *target = NULL;
425 enum mxr_geometry_stage stage;
426 struct mxr_geometry tmp;
427 struct v4l2_rect res;
428
429 memset(&res, 0, sizeof(res));
430
431 mxr_dbg(layer->mdev, "%s: rect: %dx%d@%d,%d\n", __func__,
432 s->r.width, s->r.height, s->r.left, s->r.top);
433
434 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
435 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
436 return -EINVAL;
437
438 switch (s->target) {
439
440 case V4L2_SEL_TGT_CROP_DEFAULT:
441 case V4L2_SEL_TGT_CROP_BOUNDS:
442 res.width = geo->src.full_width;
443 res.height = geo->src.full_height;
444 break;
445
446
447 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
448 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
449 res.width = geo->dst.full_width;
450 res.height = geo->dst.full_height;
451 break;
452
453 case V4L2_SEL_TGT_CROP:
454 target = &geo->src;
455 stage = MXR_GEOMETRY_CROP;
456 break;
457 case V4L2_SEL_TGT_COMPOSE:
458 case V4L2_SEL_TGT_COMPOSE_PADDED:
459 target = &geo->dst;
460 stage = MXR_GEOMETRY_COMPOSE;
461 break;
462 default:
463 return -EINVAL;
464 }
465
466 if (target) {
467
468 memcpy(&tmp, geo, sizeof(tmp));
469
470
471 target->x_offset = s->r.left;
472 target->y_offset = s->r.top;
473 target->width = s->r.width;
474 target->height = s->r.height;
475
476 layer->ops.fix_geometry(layer, stage, s->flags);
477
478
479 res.left = target->x_offset;
480 res.top = target->y_offset;
481 res.width = target->width;
482 res.height = target->height;
483
484 mxr_geometry_dump(layer->mdev, &layer->geo);
485 }
486
487
488 if ((s->flags & V4L2_SEL_FLAG_LE) && !mxr_is_rect_inside(&res, &s->r))
489 goto fail;
490 if ((s->flags & V4L2_SEL_FLAG_GE) && !mxr_is_rect_inside(&s->r, &res))
491 goto fail;
492
493
494 s->r = res;
495
496 return 0;
497fail:
498
499 if (target)
500 memcpy(geo, &tmp, sizeof(tmp));
501 return -ERANGE;
502}
503
504static int mxr_enum_dv_timings(struct file *file, void *fh,
505 struct v4l2_enum_dv_timings *timings)
506{
507 struct mxr_layer *layer = video_drvdata(file);
508 struct mxr_device *mdev = layer->mdev;
509 int ret;
510
511
512 mutex_lock(&mdev->mutex);
513 ret = v4l2_subdev_call(to_outsd(mdev), video, enum_dv_timings, timings);
514 mutex_unlock(&mdev->mutex);
515
516 return ret ? -EINVAL : 0;
517}
518
519static int mxr_s_dv_timings(struct file *file, void *fh,
520 struct v4l2_dv_timings *timings)
521{
522 struct mxr_layer *layer = video_drvdata(file);
523 struct mxr_device *mdev = layer->mdev;
524 int ret;
525
526
527 mutex_lock(&mdev->mutex);
528
529
530
531
532 if (mdev->n_output > 0) {
533 mutex_unlock(&mdev->mutex);
534 return -EBUSY;
535 }
536
537 ret = v4l2_subdev_call(to_outsd(mdev), video, s_dv_timings, timings);
538
539 mutex_unlock(&mdev->mutex);
540
541 mxr_layer_update_output(layer);
542
543
544 return ret ? -EINVAL : 0;
545}
546
547static int mxr_g_dv_timings(struct file *file, void *fh,
548 struct v4l2_dv_timings *timings)
549{
550 struct mxr_layer *layer = video_drvdata(file);
551 struct mxr_device *mdev = layer->mdev;
552 int ret;
553
554
555 mutex_lock(&mdev->mutex);
556 ret = v4l2_subdev_call(to_outsd(mdev), video, g_dv_timings, timings);
557 mutex_unlock(&mdev->mutex);
558
559 return ret ? -EINVAL : 0;
560}
561
562static int mxr_dv_timings_cap(struct file *file, void *fh,
563 struct v4l2_dv_timings_cap *cap)
564{
565 struct mxr_layer *layer = video_drvdata(file);
566 struct mxr_device *mdev = layer->mdev;
567 int ret;
568
569
570 mutex_lock(&mdev->mutex);
571 ret = v4l2_subdev_call(to_outsd(mdev), video, dv_timings_cap, cap);
572 mutex_unlock(&mdev->mutex);
573
574 return ret ? -EINVAL : 0;
575}
576
577static int mxr_s_std(struct file *file, void *fh, v4l2_std_id norm)
578{
579 struct mxr_layer *layer = video_drvdata(file);
580 struct mxr_device *mdev = layer->mdev;
581 int ret;
582
583
584 mutex_lock(&mdev->mutex);
585
586
587
588
589 if (mdev->n_output > 0) {
590 mutex_unlock(&mdev->mutex);
591 return -EBUSY;
592 }
593
594 ret = v4l2_subdev_call(to_outsd(mdev), video, s_std_output, norm);
595
596 mutex_unlock(&mdev->mutex);
597
598 mxr_layer_update_output(layer);
599
600 return ret ? -EINVAL : 0;
601}
602
603static int mxr_g_std(struct file *file, void *fh, v4l2_std_id *norm)
604{
605 struct mxr_layer *layer = video_drvdata(file);
606 struct mxr_device *mdev = layer->mdev;
607 int ret;
608
609
610 mutex_lock(&mdev->mutex);
611 ret = v4l2_subdev_call(to_outsd(mdev), video, g_std_output, norm);
612 mutex_unlock(&mdev->mutex);
613
614 return ret ? -EINVAL : 0;
615}
616
617static int mxr_enum_output(struct file *file, void *fh, struct v4l2_output *a)
618{
619 struct mxr_layer *layer = video_drvdata(file);
620 struct mxr_device *mdev = layer->mdev;
621 struct mxr_output *out;
622 struct v4l2_subdev *sd;
623
624 if (a->index >= mdev->output_cnt)
625 return -EINVAL;
626 out = mdev->output[a->index];
627 BUG_ON(out == NULL);
628 sd = out->sd;
629 strlcpy(a->name, out->name, sizeof(a->name));
630
631
632 v4l2_subdev_call(sd, video, g_tvnorms_output, &a->std);
633 a->capabilities = 0;
634 if (sd->ops->video && sd->ops->video->s_dv_timings)
635 a->capabilities |= V4L2_OUT_CAP_DV_TIMINGS;
636 if (sd->ops->video && sd->ops->video->s_std_output)
637 a->capabilities |= V4L2_OUT_CAP_STD;
638 a->type = V4L2_OUTPUT_TYPE_ANALOG;
639
640 return 0;
641}
642
643static int mxr_s_output(struct file *file, void *fh, unsigned int i)
644{
645 struct video_device *vfd = video_devdata(file);
646 struct mxr_layer *layer = video_drvdata(file);
647 struct mxr_device *mdev = layer->mdev;
648
649 if (i >= mdev->output_cnt || mdev->output[i] == NULL)
650 return -EINVAL;
651
652 mutex_lock(&mdev->mutex);
653 if (mdev->n_output > 0) {
654 mutex_unlock(&mdev->mutex);
655 return -EBUSY;
656 }
657 mdev->current_output = i;
658 vfd->tvnorms = 0;
659 v4l2_subdev_call(to_outsd(mdev), video, g_tvnorms_output,
660 &vfd->tvnorms);
661 mutex_unlock(&mdev->mutex);
662
663
664 mxr_layer_update_output(layer);
665
666 mxr_dbg(mdev, "tvnorms = %08llx\n", vfd->tvnorms);
667
668 return 0;
669}
670
671static int mxr_g_output(struct file *file, void *fh, unsigned int *p)
672{
673 struct mxr_layer *layer = video_drvdata(file);
674 struct mxr_device *mdev = layer->mdev;
675
676 mutex_lock(&mdev->mutex);
677 *p = mdev->current_output;
678 mutex_unlock(&mdev->mutex);
679
680 return 0;
681}
682
683static int mxr_reqbufs(struct file *file, void *priv,
684 struct v4l2_requestbuffers *p)
685{
686 struct mxr_layer *layer = video_drvdata(file);
687
688 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
689 return vb2_reqbufs(&layer->vb_queue, p);
690}
691
692static int mxr_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
693{
694 struct mxr_layer *layer = video_drvdata(file);
695
696 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
697 return vb2_querybuf(&layer->vb_queue, p);
698}
699
700static int mxr_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
701{
702 struct mxr_layer *layer = video_drvdata(file);
703
704 mxr_dbg(layer->mdev, "%s:%d(%d)\n", __func__, __LINE__, p->index);
705 return vb2_qbuf(&layer->vb_queue, p);
706}
707
708static int mxr_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
709{
710 struct mxr_layer *layer = video_drvdata(file);
711
712 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
713 return vb2_dqbuf(&layer->vb_queue, p, file->f_flags & O_NONBLOCK);
714}
715
716static int mxr_expbuf(struct file *file, void *priv,
717 struct v4l2_exportbuffer *eb)
718{
719 struct mxr_layer *layer = video_drvdata(file);
720
721 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
722 return vb2_expbuf(&layer->vb_queue, eb);
723}
724
725static int mxr_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
726{
727 struct mxr_layer *layer = video_drvdata(file);
728
729 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
730 return vb2_streamon(&layer->vb_queue, i);
731}
732
733static int mxr_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
734{
735 struct mxr_layer *layer = video_drvdata(file);
736
737 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
738 return vb2_streamoff(&layer->vb_queue, i);
739}
740
741static const struct v4l2_ioctl_ops mxr_ioctl_ops = {
742 .vidioc_querycap = mxr_querycap,
743
744 .vidioc_enum_fmt_vid_out_mplane = mxr_enum_fmt,
745 .vidioc_s_fmt_vid_out_mplane = mxr_s_fmt,
746 .vidioc_g_fmt_vid_out_mplane = mxr_g_fmt,
747
748 .vidioc_reqbufs = mxr_reqbufs,
749 .vidioc_querybuf = mxr_querybuf,
750 .vidioc_qbuf = mxr_qbuf,
751 .vidioc_dqbuf = mxr_dqbuf,
752 .vidioc_expbuf = mxr_expbuf,
753
754 .vidioc_streamon = mxr_streamon,
755 .vidioc_streamoff = mxr_streamoff,
756
757 .vidioc_enum_dv_timings = mxr_enum_dv_timings,
758 .vidioc_s_dv_timings = mxr_s_dv_timings,
759 .vidioc_g_dv_timings = mxr_g_dv_timings,
760 .vidioc_dv_timings_cap = mxr_dv_timings_cap,
761
762 .vidioc_s_std = mxr_s_std,
763 .vidioc_g_std = mxr_g_std,
764
765 .vidioc_enum_output = mxr_enum_output,
766 .vidioc_s_output = mxr_s_output,
767 .vidioc_g_output = mxr_g_output,
768
769 .vidioc_g_selection = mxr_g_selection,
770 .vidioc_s_selection = mxr_s_selection,
771};
772
773static int mxr_video_open(struct file *file)
774{
775 struct mxr_layer *layer = video_drvdata(file);
776 struct mxr_device *mdev = layer->mdev;
777 int ret = 0;
778
779 mxr_dbg(mdev, "%s:%d\n", __func__, __LINE__);
780 if (mutex_lock_interruptible(&layer->mutex))
781 return -ERESTARTSYS;
782
783 wait_for_device_probe();
784
785 ret = v4l2_fh_open(file);
786 if (ret) {
787 mxr_err(mdev, "v4l2_fh_open failed\n");
788 goto unlock;
789 }
790
791
792 if (!v4l2_fh_is_singular_file(file))
793 goto unlock;
794
795
796 ret = mxr_power_get(mdev);
797 if (ret) {
798 mxr_err(mdev, "power on failed\n");
799 goto fail_fh_open;
800 }
801
802 ret = vb2_queue_init(&layer->vb_queue);
803 if (ret != 0) {
804 mxr_err(mdev, "failed to initialize vb2 queue\n");
805 goto fail_power;
806 }
807
808 layer->fmt = layer->fmt_array[0];
809
810 mxr_layer_default_geo(layer);
811 mutex_unlock(&layer->mutex);
812
813 return 0;
814
815fail_power:
816 mxr_power_put(mdev);
817
818fail_fh_open:
819 v4l2_fh_release(file);
820
821unlock:
822 mutex_unlock(&layer->mutex);
823
824 return ret;
825}
826
827static unsigned int
828mxr_video_poll(struct file *file, struct poll_table_struct *wait)
829{
830 struct mxr_layer *layer = video_drvdata(file);
831 unsigned int res;
832
833 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
834
835 mutex_lock(&layer->mutex);
836 res = vb2_poll(&layer->vb_queue, file, wait);
837 mutex_unlock(&layer->mutex);
838 return res;
839}
840
841static int mxr_video_mmap(struct file *file, struct vm_area_struct *vma)
842{
843 struct mxr_layer *layer = video_drvdata(file);
844 int ret;
845
846 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
847
848 if (mutex_lock_interruptible(&layer->mutex))
849 return -ERESTARTSYS;
850 ret = vb2_mmap(&layer->vb_queue, vma);
851 mutex_unlock(&layer->mutex);
852 return ret;
853}
854
855static int mxr_video_release(struct file *file)
856{
857 struct mxr_layer *layer = video_drvdata(file);
858
859 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
860 mutex_lock(&layer->mutex);
861 if (v4l2_fh_is_singular_file(file)) {
862 vb2_queue_release(&layer->vb_queue);
863 mxr_power_put(layer->mdev);
864 }
865 v4l2_fh_release(file);
866 mutex_unlock(&layer->mutex);
867 return 0;
868}
869
870static const struct v4l2_file_operations mxr_fops = {
871 .owner = THIS_MODULE,
872 .open = mxr_video_open,
873 .poll = mxr_video_poll,
874 .mmap = mxr_video_mmap,
875 .release = mxr_video_release,
876 .unlocked_ioctl = video_ioctl2,
877};
878
879static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
880 unsigned int *nbuffers, unsigned int *nplanes, unsigned int sizes[],
881 void *alloc_ctxs[])
882{
883 struct mxr_layer *layer = vb2_get_drv_priv(vq);
884 const struct mxr_format *fmt = layer->fmt;
885 int i;
886 struct mxr_device *mdev = layer->mdev;
887 struct v4l2_plane_pix_format planes[3];
888
889 mxr_dbg(mdev, "%s\n", __func__);
890
891 if (fmt == NULL)
892 return -EINVAL;
893 mxr_dbg(mdev, "fmt = %s\n", fmt->name);
894 mxr_mplane_fill(planes, fmt, layer->geo.src.full_width,
895 layer->geo.src.full_height);
896
897 *nplanes = fmt->num_subframes;
898 for (i = 0; i < fmt->num_subframes; ++i) {
899 alloc_ctxs[i] = layer->mdev->alloc_ctx;
900 sizes[i] = planes[i].sizeimage;
901 mxr_dbg(mdev, "size[%d] = %08x\n", i, sizes[i]);
902 }
903
904 if (*nbuffers == 0)
905 *nbuffers = 1;
906
907 return 0;
908}
909
910static void buf_queue(struct vb2_buffer *vb)
911{
912 struct mxr_buffer *buffer = container_of(vb, struct mxr_buffer, vb);
913 struct mxr_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
914 struct mxr_device *mdev = layer->mdev;
915 unsigned long flags;
916
917 spin_lock_irqsave(&layer->enq_slock, flags);
918 list_add_tail(&buffer->list, &layer->enq_list);
919 spin_unlock_irqrestore(&layer->enq_slock, flags);
920
921 mxr_dbg(mdev, "queuing buffer\n");
922}
923
924static void wait_lock(struct vb2_queue *vq)
925{
926 struct mxr_layer *layer = vb2_get_drv_priv(vq);
927
928 mxr_dbg(layer->mdev, "%s\n", __func__);
929 mutex_lock(&layer->mutex);
930}
931
932static void wait_unlock(struct vb2_queue *vq)
933{
934 struct mxr_layer *layer = vb2_get_drv_priv(vq);
935
936 mxr_dbg(layer->mdev, "%s\n", __func__);
937 mutex_unlock(&layer->mutex);
938}
939
940static int start_streaming(struct vb2_queue *vq, unsigned int count)
941{
942 struct mxr_layer *layer = vb2_get_drv_priv(vq);
943 struct mxr_device *mdev = layer->mdev;
944 unsigned long flags;
945
946 mxr_dbg(mdev, "%s\n", __func__);
947
948 if (count == 0) {
949 mxr_dbg(mdev, "no output buffers queued\n");
950 return -EINVAL;
951 }
952
953
954 mxr_output_get(mdev);
955
956 mxr_layer_update_output(layer);
957 layer->ops.format_set(layer);
958
959 spin_lock_irqsave(&layer->enq_slock, flags);
960 layer->state = MXR_LAYER_STREAMING;
961 spin_unlock_irqrestore(&layer->enq_slock, flags);
962
963 layer->ops.stream_set(layer, MXR_ENABLE);
964 mxr_streamer_get(mdev);
965
966 return 0;
967}
968
969static void mxr_watchdog(unsigned long arg)
970{
971 struct mxr_layer *layer = (struct mxr_layer *) arg;
972 struct mxr_device *mdev = layer->mdev;
973 unsigned long flags;
974
975 mxr_err(mdev, "watchdog fired for layer %s\n", layer->vfd.name);
976
977 spin_lock_irqsave(&layer->enq_slock, flags);
978
979 if (layer->update_buf == layer->shadow_buf)
980 layer->update_buf = NULL;
981 if (layer->update_buf) {
982 vb2_buffer_done(&layer->update_buf->vb, VB2_BUF_STATE_ERROR);
983 layer->update_buf = NULL;
984 }
985 if (layer->shadow_buf) {
986 vb2_buffer_done(&layer->shadow_buf->vb, VB2_BUF_STATE_ERROR);
987 layer->shadow_buf = NULL;
988 }
989 spin_unlock_irqrestore(&layer->enq_slock, flags);
990}
991
992static int stop_streaming(struct vb2_queue *vq)
993{
994 struct mxr_layer *layer = vb2_get_drv_priv(vq);
995 struct mxr_device *mdev = layer->mdev;
996 unsigned long flags;
997 struct timer_list watchdog;
998 struct mxr_buffer *buf, *buf_tmp;
999
1000 mxr_dbg(mdev, "%s\n", __func__);
1001
1002 spin_lock_irqsave(&layer->enq_slock, flags);
1003
1004
1005 layer->state = MXR_LAYER_STREAMING_FINISH;
1006
1007
1008 list_for_each_entry_safe(buf, buf_tmp, &layer->enq_list, list) {
1009 list_del(&buf->list);
1010 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
1011 }
1012
1013 spin_unlock_irqrestore(&layer->enq_slock, flags);
1014
1015
1016 setup_timer_on_stack(&watchdog, mxr_watchdog,
1017 (unsigned long)layer);
1018 mod_timer(&watchdog, jiffies + msecs_to_jiffies(1000));
1019
1020
1021 vb2_wait_for_all_buffers(vq);
1022
1023
1024 del_timer_sync(&watchdog);
1025 destroy_timer_on_stack(&watchdog);
1026
1027
1028 spin_lock_irqsave(&layer->enq_slock, flags);
1029 layer->state = MXR_LAYER_IDLE;
1030 spin_unlock_irqrestore(&layer->enq_slock, flags);
1031
1032
1033 layer->ops.stream_set(layer, MXR_DISABLE);
1034
1035 mxr_streamer_put(mdev);
1036
1037 mxr_output_put(mdev);
1038 return 0;
1039}
1040
1041static struct vb2_ops mxr_video_qops = {
1042 .queue_setup = queue_setup,
1043 .buf_queue = buf_queue,
1044 .wait_prepare = wait_unlock,
1045 .wait_finish = wait_lock,
1046 .start_streaming = start_streaming,
1047 .stop_streaming = stop_streaming,
1048};
1049
1050
1051int mxr_base_layer_register(struct mxr_layer *layer)
1052{
1053 struct mxr_device *mdev = layer->mdev;
1054 int ret;
1055
1056 ret = video_register_device(&layer->vfd, VFL_TYPE_GRABBER, -1);
1057 if (ret)
1058 mxr_err(mdev, "failed to register video device\n");
1059 else
1060 mxr_info(mdev, "registered layer %s as /dev/video%d\n",
1061 layer->vfd.name, layer->vfd.num);
1062 return ret;
1063}
1064
1065void mxr_base_layer_unregister(struct mxr_layer *layer)
1066{
1067 video_unregister_device(&layer->vfd);
1068}
1069
1070void mxr_layer_release(struct mxr_layer *layer)
1071{
1072 if (layer->ops.release)
1073 layer->ops.release(layer);
1074}
1075
1076void mxr_base_layer_release(struct mxr_layer *layer)
1077{
1078 kfree(layer);
1079}
1080
1081static void mxr_vfd_release(struct video_device *vdev)
1082{
1083 pr_info("video device release\n");
1084}
1085
1086struct mxr_layer *mxr_base_layer_create(struct mxr_device *mdev,
1087 int idx, char *name, struct mxr_layer_ops *ops)
1088{
1089 struct mxr_layer *layer;
1090
1091 layer = kzalloc(sizeof(*layer), GFP_KERNEL);
1092 if (layer == NULL) {
1093 mxr_err(mdev, "not enough memory for layer.\n");
1094 goto fail;
1095 }
1096
1097 layer->mdev = mdev;
1098 layer->idx = idx;
1099 layer->ops = *ops;
1100
1101 spin_lock_init(&layer->enq_slock);
1102 INIT_LIST_HEAD(&layer->enq_list);
1103 mutex_init(&layer->mutex);
1104
1105 layer->vfd = (struct video_device) {
1106 .minor = -1,
1107 .release = mxr_vfd_release,
1108 .fops = &mxr_fops,
1109 .vfl_dir = VFL_DIR_TX,
1110 .ioctl_ops = &mxr_ioctl_ops,
1111 };
1112 strlcpy(layer->vfd.name, name, sizeof(layer->vfd.name));
1113
1114 set_bit(V4L2_FL_USE_FH_PRIO, &layer->vfd.flags);
1115
1116 video_set_drvdata(&layer->vfd, layer);
1117 layer->vfd.lock = &layer->mutex;
1118 layer->vfd.v4l2_dev = &mdev->v4l2_dev;
1119
1120 layer->vb_queue = (struct vb2_queue) {
1121 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
1122 .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF,
1123 .drv_priv = layer,
1124 .buf_struct_size = sizeof(struct mxr_buffer),
1125 .ops = &mxr_video_qops,
1126 .mem_ops = &vb2_dma_contig_memops,
1127 };
1128
1129 return layer;
1130
1131fail:
1132 return NULL;
1133}
1134
1135static const struct mxr_format *find_format_by_fourcc(
1136 struct mxr_layer *layer, unsigned long fourcc)
1137{
1138 int i;
1139
1140 for (i = 0; i < layer->fmt_array_size; ++i)
1141 if (layer->fmt_array[i]->fourcc == fourcc)
1142 return layer->fmt_array[i];
1143 return NULL;
1144}
1145
1146static const struct mxr_format *find_format_by_index(
1147 struct mxr_layer *layer, unsigned long index)
1148{
1149 if (index >= layer->fmt_array_size)
1150 return NULL;
1151 return layer->fmt_array[index];
1152}
1153
1154