Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * A virtual v4l2-mem2mem example device.
4 *
5 * This is a virtual device driver for testing mem-to-mem videobuf framework.
6 * It simulates a device that uses memory buffers for both source and
7 * destination, processes the data and issues an "irq" (simulated by a delayed
8 * workqueue).
9 * The device is capable of multi-instance, multi-buffer-per-transaction
10 * operation (via the mem2mem framework).
11 *
12 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
13 * Pawel Osciak, <pawel@osciak.com>
14 * Marek Szyprowski, <m.szyprowski@samsung.com>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2 of the
19 * License, or (at your option) any later version
20 */
21#include <linux/module.h>
22#include <linux/delay.h>
23#include <linux/fs.h>
24#include <linux/sched.h>
25#include <linux/slab.h>
26
27#include <linux/platform_device.h>
28#include <media/v4l2-mem2mem.h>
29#include <media/v4l2-device.h>
30#include <media/v4l2-ioctl.h>
31#include <media/v4l2-ctrls.h>
32#include <media/v4l2-event.h>
33#include <media/videobuf2-vmalloc.h>
34
35MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
36MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
37MODULE_LICENSE("GPL");
38MODULE_VERSION("0.2");
39MODULE_ALIAS("mem2mem_testdev");
40
41static unsigned int debug;
42module_param(debug, uint, 0644);
43MODULE_PARM_DESC(debug, "debug level");
44
45/* Default transaction time in msec */
46static unsigned int default_transtime = 40; /* Max 25 fps */
47module_param(default_transtime, uint, 0644);
48MODULE_PARM_DESC(default_transtime, "default transaction time in ms");
49
50#define MIN_W 32
51#define MIN_H 32
52#define MAX_W 640
53#define MAX_H 480
54
55/* Pixel alignment for non-bayer formats */
56#define WIDTH_ALIGN 2
57#define HEIGHT_ALIGN 1
58
59/* Pixel alignment for bayer formats */
60#define BAYER_WIDTH_ALIGN 2
61#define BAYER_HEIGHT_ALIGN 2
62
63/* Flags that indicate a format can be used for capture/output */
64#define MEM2MEM_CAPTURE BIT(0)
65#define MEM2MEM_OUTPUT BIT(1)
66
67#define MEM2MEM_NAME "vim2m"
68
69/* Per queue */
70#define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
71/* In bytes, per queue */
72#define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
73
74/* Flags that indicate processing mode */
75#define MEM2MEM_HFLIP BIT(0)
76#define MEM2MEM_VFLIP BIT(1)
77
78#define dprintk(dev, lvl, fmt, arg...) \
79 v4l2_dbg(lvl, debug, &(dev)->v4l2_dev, "%s: " fmt, __func__, ## arg)
80
81static void vim2m_dev_release(struct device *dev)
82{}
83
84static struct platform_device vim2m_pdev = {
85 .name = MEM2MEM_NAME,
86 .dev.release = vim2m_dev_release,
87};
88
89struct vim2m_fmt {
90 u32 fourcc;
91 int depth;
92 /* Types the format can be used for */
93 u32 types;
94};
95
96static struct vim2m_fmt formats[] = {
97 {
98 .fourcc = V4L2_PIX_FMT_RGB565, /* rrrrrggg gggbbbbb */
99 .depth = 16,
100 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
101 }, {
102 .fourcc = V4L2_PIX_FMT_RGB565X, /* gggbbbbb rrrrrggg */
103 .depth = 16,
104 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
105 }, {
106 .fourcc = V4L2_PIX_FMT_RGB24,
107 .depth = 24,
108 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
109 }, {
110 .fourcc = V4L2_PIX_FMT_BGR24,
111 .depth = 24,
112 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
113 }, {
114 .fourcc = V4L2_PIX_FMT_YUYV,
115 .depth = 16,
116 .types = MEM2MEM_CAPTURE,
117 }, {
118 .fourcc = V4L2_PIX_FMT_SBGGR8,
119 .depth = 8,
120 .types = MEM2MEM_CAPTURE,
121 }, {
122 .fourcc = V4L2_PIX_FMT_SGBRG8,
123 .depth = 8,
124 .types = MEM2MEM_CAPTURE,
125 }, {
126 .fourcc = V4L2_PIX_FMT_SGRBG8,
127 .depth = 8,
128 .types = MEM2MEM_CAPTURE,
129 }, {
130 .fourcc = V4L2_PIX_FMT_SRGGB8,
131 .depth = 8,
132 .types = MEM2MEM_CAPTURE,
133 },
134};
135
136#define NUM_FORMATS ARRAY_SIZE(formats)
137
138/* Per-queue, driver-specific private data */
139struct vim2m_q_data {
140 unsigned int width;
141 unsigned int height;
142 unsigned int sizeimage;
143 unsigned int sequence;
144 struct vim2m_fmt *fmt;
145};
146
147enum {
148 V4L2_M2M_SRC = 0,
149 V4L2_M2M_DST = 1,
150};
151
152#define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
153#define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
154
155static struct vim2m_fmt *find_format(u32 fourcc)
156{
157 struct vim2m_fmt *fmt;
158 unsigned int k;
159
160 for (k = 0; k < NUM_FORMATS; k++) {
161 fmt = &formats[k];
162 if (fmt->fourcc == fourcc)
163 break;
164 }
165
166 if (k == NUM_FORMATS)
167 return NULL;
168
169 return &formats[k];
170}
171
172static void get_alignment(u32 fourcc,
173 unsigned int *walign, unsigned int *halign)
174{
175 switch (fourcc) {
176 case V4L2_PIX_FMT_SBGGR8:
177 case V4L2_PIX_FMT_SGBRG8:
178 case V4L2_PIX_FMT_SGRBG8:
179 case V4L2_PIX_FMT_SRGGB8:
180 *walign = BAYER_WIDTH_ALIGN;
181 *halign = BAYER_HEIGHT_ALIGN;
182 return;
183 default:
184 *walign = WIDTH_ALIGN;
185 *halign = HEIGHT_ALIGN;
186 return;
187 }
188}
189
190struct vim2m_dev {
191 struct v4l2_device v4l2_dev;
192 struct video_device vfd;
193#ifdef CONFIG_MEDIA_CONTROLLER
194 struct media_device mdev;
195#endif
196
197 atomic_t num_inst;
198 struct mutex dev_mutex;
199
200 struct v4l2_m2m_dev *m2m_dev;
201};
202
203struct vim2m_ctx {
204 struct v4l2_fh fh;
205 struct vim2m_dev *dev;
206
207 struct v4l2_ctrl_handler hdl;
208
209 /* Processed buffers in this transaction */
210 u8 num_processed;
211
212 /* Transaction length (i.e. how many buffers per transaction) */
213 u32 translen;
214 /* Transaction time (i.e. simulated processing time) in milliseconds */
215 u32 transtime;
216
217 struct mutex vb_mutex;
218 struct delayed_work work_run;
219 spinlock_t irqlock;
220
221 /* Abort requested by m2m */
222 int aborting;
223
224 /* Processing mode */
225 int mode;
226
227 enum v4l2_colorspace colorspace;
228 enum v4l2_ycbcr_encoding ycbcr_enc;
229 enum v4l2_xfer_func xfer_func;
230 enum v4l2_quantization quant;
231
232 /* Source and destination queue data */
233 struct vim2m_q_data q_data[2];
234};
235
236static inline struct vim2m_ctx *file2ctx(struct file *file)
237{
238 return container_of(file->private_data, struct vim2m_ctx, fh);
239}
240
241static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx,
242 enum v4l2_buf_type type)
243{
244 switch (type) {
245 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
246 return &ctx->q_data[V4L2_M2M_SRC];
247 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
248 return &ctx->q_data[V4L2_M2M_DST];
249 default:
250 return NULL;
251 }
252}
253
254static const char *type_name(enum v4l2_buf_type type)
255{
256 switch (type) {
257 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
258 return "Output";
259 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
260 return "Capture";
261 default:
262 return "Invalid";
263 }
264}
265
266#define CLIP(__color) \
267 (u8)(((__color) > 0xff) ? 0xff : (((__color) < 0) ? 0 : (__color)))
268
269static void copy_line(struct vim2m_q_data *q_data_out,
270 u8 *src, u8 *dst, bool reverse)
271{
272 int x, depth = q_data_out->fmt->depth >> 3;
273
274 if (!reverse) {
275 memcpy(dst, src, q_data_out->width * depth);
276 } else {
277 for (x = 0; x < q_data_out->width >> 1; x++) {
278 memcpy(dst, src, depth);
279 memcpy(dst + depth, src - depth, depth);
280 src -= depth << 1;
281 dst += depth << 1;
282 }
283 return;
284 }
285}
286
287static void copy_two_pixels(struct vim2m_q_data *q_data_in,
288 struct vim2m_q_data *q_data_out,
289 u8 *src[2], u8 **dst, int ypos, bool reverse)
290{
291 struct vim2m_fmt *out = q_data_out->fmt;
292 struct vim2m_fmt *in = q_data_in->fmt;
293 u8 _r[2], _g[2], _b[2], *r, *g, *b;
294 int i;
295
296 /* Step 1: read two consecutive pixels from src pointer */
297
298 r = _r;
299 g = _g;
300 b = _b;
301
302 switch (in->fourcc) {
303 case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
304 for (i = 0; i < 2; i++) {
305 u16 pix = *(u16 *)(src[i]);
306
307 *r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07;
308 *g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03;
309 *b++ = (u8)((pix & 0x1f) << 3) | 0x07;
310 }
311 break;
312 case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
313 for (i = 0; i < 2; i++) {
314 u16 pix = *(u16 *)(src[i]);
315
316 *r++ = (u8)(((0x00f8 & pix) >> 3) << 3) | 0x07;
317 *g++ = (u8)(((pix & 0x7) << 2) |
318 ((pix & 0xe000) >> 5)) | 0x03;
319 *b++ = (u8)(((pix & 0x1f00) >> 8) << 3) | 0x07;
320 }
321 break;
322 default:
323 case V4L2_PIX_FMT_RGB24:
324 for (i = 0; i < 2; i++) {
325 *r++ = src[i][0];
326 *g++ = src[i][1];
327 *b++ = src[i][2];
328 }
329 break;
330 case V4L2_PIX_FMT_BGR24:
331 for (i = 0; i < 2; i++) {
332 *b++ = src[i][0];
333 *g++ = src[i][1];
334 *r++ = src[i][2];
335 }
336 break;
337 }
338
339 /* Step 2: store two consecutive points, reversing them if needed */
340
341 r = _r;
342 g = _g;
343 b = _b;
344
345 switch (out->fourcc) {
346 case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
347 for (i = 0; i < 2; i++) {
348 u16 *pix = (u16 *)*dst;
349
350 *pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) |
351 (*b >> 3);
352
353 *dst += 2;
354 }
355 return;
356 case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
357 for (i = 0; i < 2; i++) {
358 u16 *pix = (u16 *)*dst;
359 u8 green = *g++ >> 2;
360
361 *pix = ((green << 8) & 0xe000) | (green & 0x07) |
362 ((*b++ << 5) & 0x1f00) | ((*r++ & 0xf8));
363
364 *dst += 2;
365 }
366 return;
367 case V4L2_PIX_FMT_RGB24:
368 for (i = 0; i < 2; i++) {
369 *(*dst)++ = *r++;
370 *(*dst)++ = *g++;
371 *(*dst)++ = *b++;
372 }
373 return;
374 case V4L2_PIX_FMT_BGR24:
375 for (i = 0; i < 2; i++) {
376 *(*dst)++ = *b++;
377 *(*dst)++ = *g++;
378 *(*dst)++ = *r++;
379 }
380 return;
381 case V4L2_PIX_FMT_YUYV:
382 default:
383 {
384 u8 y, y1, u, v;
385
386 y = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b)
387 + 524288) >> 15);
388 u = ((-4878 * (*r) - 9578 * (*g) + 14456 * (*b)
389 + 4210688) >> 15);
390 v = ((14456 * (*r++) - 12105 * (*g++) - 2351 * (*b++)
391 + 4210688) >> 15);
392 y1 = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b)
393 + 524288) >> 15);
394
395 *(*dst)++ = y;
396 *(*dst)++ = u;
397
398 *(*dst)++ = y1;
399 *(*dst)++ = v;
400 return;
401 }
402 case V4L2_PIX_FMT_SBGGR8:
403 if (!(ypos & 1)) {
404 *(*dst)++ = *b;
405 *(*dst)++ = *++g;
406 } else {
407 *(*dst)++ = *g;
408 *(*dst)++ = *++r;
409 }
410 return;
411 case V4L2_PIX_FMT_SGBRG8:
412 if (!(ypos & 1)) {
413 *(*dst)++ = *g;
414 *(*dst)++ = *++b;
415 } else {
416 *(*dst)++ = *r;
417 *(*dst)++ = *++g;
418 }
419 return;
420 case V4L2_PIX_FMT_SGRBG8:
421 if (!(ypos & 1)) {
422 *(*dst)++ = *g;
423 *(*dst)++ = *++r;
424 } else {
425 *(*dst)++ = *b;
426 *(*dst)++ = *++g;
427 }
428 return;
429 case V4L2_PIX_FMT_SRGGB8:
430 if (!(ypos & 1)) {
431 *(*dst)++ = *r;
432 *(*dst)++ = *++g;
433 } else {
434 *(*dst)++ = *g;
435 *(*dst)++ = *++b;
436 }
437 return;
438 }
439}
440
441static int device_process(struct vim2m_ctx *ctx,
442 struct vb2_v4l2_buffer *in_vb,
443 struct vb2_v4l2_buffer *out_vb)
444{
445 struct vim2m_dev *dev = ctx->dev;
446 struct vim2m_q_data *q_data_in, *q_data_out;
447 u8 *p_in, *p_line, *p_in_x[2], *p, *p_out;
448 unsigned int width, height, bytesperline, bytes_per_pixel;
449 unsigned int x, y, y_in, y_out, x_int, x_fract, x_err, x_offset;
450 int start, end, step;
451
452 q_data_in = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
453 if (!q_data_in)
454 return 0;
455 bytesperline = (q_data_in->width * q_data_in->fmt->depth) >> 3;
456 bytes_per_pixel = q_data_in->fmt->depth >> 3;
457
458 q_data_out = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
459 if (!q_data_out)
460 return 0;
461
462 /* As we're doing scaling, use the output dimensions here */
463 height = q_data_out->height;
464 width = q_data_out->width;
465
466 p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0);
467 p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0);
468 if (!p_in || !p_out) {
469 v4l2_err(&dev->v4l2_dev,
470 "Acquiring kernel pointers to buffers failed\n");
471 return -EFAULT;
472 }
473
474 out_vb->sequence = q_data_out->sequence++;
475 in_vb->sequence = q_data_in->sequence++;
476 v4l2_m2m_buf_copy_metadata(in_vb, out_vb, true);
477
478 if (ctx->mode & MEM2MEM_VFLIP) {
479 start = height - 1;
480 end = -1;
481 step = -1;
482 } else {
483 start = 0;
484 end = height;
485 step = 1;
486 }
487 y_out = 0;
488
489 /*
490 * When format and resolution are identical,
491 * we can use a faster copy logic
492 */
493 if (q_data_in->fmt->fourcc == q_data_out->fmt->fourcc &&
494 q_data_in->width == q_data_out->width &&
495 q_data_in->height == q_data_out->height) {
496 for (y = start; y != end; y += step, y_out++) {
497 p = p_in + (y * bytesperline);
498 if (ctx->mode & MEM2MEM_HFLIP)
499 p += bytesperline - (q_data_in->fmt->depth >> 3);
500
501 copy_line(q_data_out, p, p_out,
502 ctx->mode & MEM2MEM_HFLIP);
503
504 p_out += bytesperline;
505 }
506 return 0;
507 }
508
509 /* Slower algorithm with format conversion, hflip, vflip and scaler */
510
511 /* To speed scaler up, use Bresenham for X dimension */
512 x_int = q_data_in->width / q_data_out->width;
513 x_fract = q_data_in->width % q_data_out->width;
514
515 for (y = start; y != end; y += step, y_out++) {
516 y_in = (y * q_data_in->height) / q_data_out->height;
517 x_offset = 0;
518 x_err = 0;
519
520 p_line = p_in + (y_in * bytesperline);
521 if (ctx->mode & MEM2MEM_HFLIP)
522 p_line += bytesperline - (q_data_in->fmt->depth >> 3);
523 p_in_x[0] = p_line;
524
525 for (x = 0; x < width >> 1; x++) {
526 x_offset += x_int;
527 x_err += x_fract;
528 if (x_err > width) {
529 x_offset++;
530 x_err -= width;
531 }
532
533 if (ctx->mode & MEM2MEM_HFLIP)
534 p_in_x[1] = p_line - x_offset * bytes_per_pixel;
535 else
536 p_in_x[1] = p_line + x_offset * bytes_per_pixel;
537
538 copy_two_pixels(q_data_in, q_data_out,
539 p_in_x, &p_out, y_out,
540 ctx->mode & MEM2MEM_HFLIP);
541
542 /* Calculate the next p_in_x0 */
543 x_offset += x_int;
544 x_err += x_fract;
545 if (x_err > width) {
546 x_offset++;
547 x_err -= width;
548 }
549
550 if (ctx->mode & MEM2MEM_HFLIP)
551 p_in_x[0] = p_line - x_offset * bytes_per_pixel;
552 else
553 p_in_x[0] = p_line + x_offset * bytes_per_pixel;
554 }
555 }
556
557 return 0;
558}
559
560/*
561 * mem2mem callbacks
562 */
563
564/*
565 * job_ready() - check whether an instance is ready to be scheduled to run
566 */
567static int job_ready(void *priv)
568{
569 struct vim2m_ctx *ctx = priv;
570
571 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
572 || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
573 dprintk(ctx->dev, 1, "Not enough buffers available\n");
574 return 0;
575 }
576
577 return 1;
578}
579
580static void job_abort(void *priv)
581{
582 struct vim2m_ctx *ctx = priv;
583
584 /* Will cancel the transaction in the next interrupt handler */
585 ctx->aborting = 1;
586}
587
588/* device_run() - prepares and starts the device
589 *
590 * This simulates all the immediate preparations required before starting
591 * a device. This will be called by the framework when it decides to schedule
592 * a particular instance.
593 */
594static void device_run(void *priv)
595{
596 struct vim2m_ctx *ctx = priv;
597 struct vb2_v4l2_buffer *src_buf, *dst_buf;
598
599 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
600 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
601
602 /* Apply request controls if any */
603 v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req,
604 &ctx->hdl);
605
606 device_process(ctx, src_buf, dst_buf);
607
608 /* Complete request controls if any */
609 v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req,
610 &ctx->hdl);
611
612 /* Run delayed work, which simulates a hardware irq */
613 schedule_delayed_work(&ctx->work_run, msecs_to_jiffies(ctx->transtime));
614}
615
616static void device_work(struct work_struct *w)
617{
618 struct vim2m_ctx *curr_ctx;
619 struct vim2m_dev *vim2m_dev;
620 struct vb2_v4l2_buffer *src_vb, *dst_vb;
621 unsigned long flags;
622
623 curr_ctx = container_of(w, struct vim2m_ctx, work_run.work);
624
625 if (!curr_ctx) {
626 pr_err("Instance released before the end of transaction\n");
627 return;
628 }
629
630 vim2m_dev = curr_ctx->dev;
631
632 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
633 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
634
635 curr_ctx->num_processed++;
636
637 spin_lock_irqsave(&curr_ctx->irqlock, flags);
638 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
639 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
640 spin_unlock_irqrestore(&curr_ctx->irqlock, flags);
641
642 if (curr_ctx->num_processed == curr_ctx->translen
643 || curr_ctx->aborting) {
644 dprintk(curr_ctx->dev, 2, "Finishing capture buffer fill\n");
645 curr_ctx->num_processed = 0;
646 v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
647 } else {
648 device_run(curr_ctx);
649 }
650}
651
652/*
653 * video ioctls
654 */
655static int vidioc_querycap(struct file *file, void *priv,
656 struct v4l2_capability *cap)
657{
658 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
659 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
660 snprintf(cap->bus_info, sizeof(cap->bus_info),
661 "platform:%s", MEM2MEM_NAME);
662 return 0;
663}
664
665static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
666{
667 int i, num;
668 struct vim2m_fmt *fmt;
669
670 num = 0;
671
672 for (i = 0; i < NUM_FORMATS; ++i) {
673 if (formats[i].types & type) {
674 /* index-th format of type type found ? */
675 if (num == f->index)
676 break;
677 /*
678 * Correct type but haven't reached our index yet,
679 * just increment per-type index
680 */
681 ++num;
682 }
683 }
684
685 if (i < NUM_FORMATS) {
686 /* Format found */
687 fmt = &formats[i];
688 f->pixelformat = fmt->fourcc;
689 return 0;
690 }
691
692 /* Format not found */
693 return -EINVAL;
694}
695
696static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
697 struct v4l2_fmtdesc *f)
698{
699 return enum_fmt(f, MEM2MEM_CAPTURE);
700}
701
702static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
703 struct v4l2_fmtdesc *f)
704{
705 return enum_fmt(f, MEM2MEM_OUTPUT);
706}
707
708static int vidioc_enum_framesizes(struct file *file, void *priv,
709 struct v4l2_frmsizeenum *fsize)
710{
711 if (fsize->index != 0)
712 return -EINVAL;
713
714 if (!find_format(fsize->pixel_format))
715 return -EINVAL;
716
717 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
718 fsize->stepwise.min_width = MIN_W;
719 fsize->stepwise.min_height = MIN_H;
720 fsize->stepwise.max_width = MAX_W;
721 fsize->stepwise.max_height = MAX_H;
722
723 get_alignment(fsize->pixel_format,
724 &fsize->stepwise.step_width,
725 &fsize->stepwise.step_height);
726 return 0;
727}
728
729static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
730{
731 struct vb2_queue *vq;
732 struct vim2m_q_data *q_data;
733
734 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
735 if (!vq)
736 return -EINVAL;
737
738 q_data = get_q_data(ctx, f->type);
739 if (!q_data)
740 return -EINVAL;
741
742 f->fmt.pix.width = q_data->width;
743 f->fmt.pix.height = q_data->height;
744 f->fmt.pix.field = V4L2_FIELD_NONE;
745 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
746 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
747 f->fmt.pix.sizeimage = q_data->sizeimage;
748 f->fmt.pix.colorspace = ctx->colorspace;
749 f->fmt.pix.xfer_func = ctx->xfer_func;
750 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
751 f->fmt.pix.quantization = ctx->quant;
752
753 return 0;
754}
755
756static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
757 struct v4l2_format *f)
758{
759 return vidioc_g_fmt(file2ctx(file), f);
760}
761
762static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
763 struct v4l2_format *f)
764{
765 return vidioc_g_fmt(file2ctx(file), f);
766}
767
768static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt)
769{
770 int walign, halign;
771 /*
772 * V4L2 specification specifies the driver corrects the
773 * format struct if any of the dimensions is unsupported
774 */
775 if (f->fmt.pix.height < MIN_H)
776 f->fmt.pix.height = MIN_H;
777 else if (f->fmt.pix.height > MAX_H)
778 f->fmt.pix.height = MAX_H;
779
780 if (f->fmt.pix.width < MIN_W)
781 f->fmt.pix.width = MIN_W;
782 else if (f->fmt.pix.width > MAX_W)
783 f->fmt.pix.width = MAX_W;
784
785 get_alignment(f->fmt.pix.pixelformat, &walign, &halign);
786 f->fmt.pix.width &= ~(walign - 1);
787 f->fmt.pix.height &= ~(halign - 1);
788 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
789 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
790 f->fmt.pix.field = V4L2_FIELD_NONE;
791
792 return 0;
793}
794
795static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
796 struct v4l2_format *f)
797{
798 struct vim2m_fmt *fmt;
799 struct vim2m_ctx *ctx = file2ctx(file);
800
801 fmt = find_format(f->fmt.pix.pixelformat);
802 if (!fmt) {
803 f->fmt.pix.pixelformat = formats[0].fourcc;
804 fmt = find_format(f->fmt.pix.pixelformat);
805 }
806 if (!(fmt->types & MEM2MEM_CAPTURE)) {
807 v4l2_err(&ctx->dev->v4l2_dev,
808 "Fourcc format (0x%08x) invalid.\n",
809 f->fmt.pix.pixelformat);
810 return -EINVAL;
811 }
812 f->fmt.pix.colorspace = ctx->colorspace;
813 f->fmt.pix.xfer_func = ctx->xfer_func;
814 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
815 f->fmt.pix.quantization = ctx->quant;
816
817 return vidioc_try_fmt(f, fmt);
818}
819
820static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
821 struct v4l2_format *f)
822{
823 struct vim2m_fmt *fmt;
824 struct vim2m_ctx *ctx = file2ctx(file);
825
826 fmt = find_format(f->fmt.pix.pixelformat);
827 if (!fmt) {
828 f->fmt.pix.pixelformat = formats[0].fourcc;
829 fmt = find_format(f->fmt.pix.pixelformat);
830 }
831 if (!(fmt->types & MEM2MEM_OUTPUT)) {
832 v4l2_err(&ctx->dev->v4l2_dev,
833 "Fourcc format (0x%08x) invalid.\n",
834 f->fmt.pix.pixelformat);
835 return -EINVAL;
836 }
837 if (!f->fmt.pix.colorspace)
838 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
839
840 return vidioc_try_fmt(f, fmt);
841}
842
843static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
844{
845 struct vim2m_q_data *q_data;
846 struct vb2_queue *vq;
847
848 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
849 if (!vq)
850 return -EINVAL;
851
852 q_data = get_q_data(ctx, f->type);
853 if (!q_data)
854 return -EINVAL;
855
856 if (vb2_is_busy(vq)) {
857 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
858 return -EBUSY;
859 }
860
861 q_data->fmt = find_format(f->fmt.pix.pixelformat);
862 q_data->width = f->fmt.pix.width;
863 q_data->height = f->fmt.pix.height;
864 q_data->sizeimage = q_data->width * q_data->height
865 * q_data->fmt->depth >> 3;
866
867 dprintk(ctx->dev, 1,
868 "Format for type %s: %dx%d (%d bpp), fmt: %c%c%c%c\n",
869 type_name(f->type), q_data->width, q_data->height,
870 q_data->fmt->depth,
871 (q_data->fmt->fourcc & 0xff),
872 (q_data->fmt->fourcc >> 8) & 0xff,
873 (q_data->fmt->fourcc >> 16) & 0xff,
874 (q_data->fmt->fourcc >> 24) & 0xff);
875
876 return 0;
877}
878
879static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
880 struct v4l2_format *f)
881{
882 int ret;
883
884 ret = vidioc_try_fmt_vid_cap(file, priv, f);
885 if (ret)
886 return ret;
887
888 return vidioc_s_fmt(file2ctx(file), f);
889}
890
891static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
892 struct v4l2_format *f)
893{
894 struct vim2m_ctx *ctx = file2ctx(file);
895 int ret;
896
897 ret = vidioc_try_fmt_vid_out(file, priv, f);
898 if (ret)
899 return ret;
900
901 ret = vidioc_s_fmt(file2ctx(file), f);
902 if (!ret) {
903 ctx->colorspace = f->fmt.pix.colorspace;
904 ctx->xfer_func = f->fmt.pix.xfer_func;
905 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
906 ctx->quant = f->fmt.pix.quantization;
907 }
908 return ret;
909}
910
911static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl)
912{
913 struct vim2m_ctx *ctx =
914 container_of(ctrl->handler, struct vim2m_ctx, hdl);
915
916 switch (ctrl->id) {
917 case V4L2_CID_HFLIP:
918 if (ctrl->val)
919 ctx->mode |= MEM2MEM_HFLIP;
920 else
921 ctx->mode &= ~MEM2MEM_HFLIP;
922 break;
923
924 case V4L2_CID_VFLIP:
925 if (ctrl->val)
926 ctx->mode |= MEM2MEM_VFLIP;
927 else
928 ctx->mode &= ~MEM2MEM_VFLIP;
929 break;
930
931 case V4L2_CID_TRANS_TIME_MSEC:
932 ctx->transtime = ctrl->val;
933 if (ctx->transtime < 1)
934 ctx->transtime = 1;
935 break;
936
937 case V4L2_CID_TRANS_NUM_BUFS:
938 ctx->translen = ctrl->val;
939 break;
940
941 default:
942 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
943 return -EINVAL;
944 }
945
946 return 0;
947}
948
949static const struct v4l2_ctrl_ops vim2m_ctrl_ops = {
950 .s_ctrl = vim2m_s_ctrl,
951};
952
953static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
954 .vidioc_querycap = vidioc_querycap,
955
956 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
957 .vidioc_enum_framesizes = vidioc_enum_framesizes,
958 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
959 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
960 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
961
962 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
963 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
964 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
965 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
966
967 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
968 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
969 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
970 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
971 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
972 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
973 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
974
975 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
976 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
977
978 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
979 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
980};
981
982/*
983 * Queue operations
984 */
985
986static int vim2m_queue_setup(struct vb2_queue *vq,
987 unsigned int *nbuffers,
988 unsigned int *nplanes,
989 unsigned int sizes[],
990 struct device *alloc_devs[])
991{
992 struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
993 struct vim2m_q_data *q_data;
994 unsigned int size, count = *nbuffers;
995
996 q_data = get_q_data(ctx, vq->type);
997 if (!q_data)
998 return -EINVAL;
999
1000 size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
1001
1002 while (size * count > MEM2MEM_VID_MEM_LIMIT)
1003 (count)--;
1004 *nbuffers = count;
1005
1006 if (*nplanes)
1007 return sizes[0] < size ? -EINVAL : 0;
1008
1009 *nplanes = 1;
1010 sizes[0] = size;
1011
1012 dprintk(ctx->dev, 1, "%s: get %d buffer(s) of size %d each.\n",
1013 type_name(vq->type), count, size);
1014
1015 return 0;
1016}
1017
1018static int vim2m_buf_out_validate(struct vb2_buffer *vb)
1019{
1020 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1021 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1022
1023 if (vbuf->field == V4L2_FIELD_ANY)
1024 vbuf->field = V4L2_FIELD_NONE;
1025 if (vbuf->field != V4L2_FIELD_NONE) {
1026 dprintk(ctx->dev, 1, "%s field isn't supported\n", __func__);
1027 return -EINVAL;
1028 }
1029
1030 return 0;
1031}
1032
1033static int vim2m_buf_prepare(struct vb2_buffer *vb)
1034{
1035 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1036 struct vim2m_q_data *q_data;
1037
1038 dprintk(ctx->dev, 2, "type: %s\n", type_name(vb->vb2_queue->type));
1039
1040 q_data = get_q_data(ctx, vb->vb2_queue->type);
1041 if (!q_data)
1042 return -EINVAL;
1043 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
1044 dprintk(ctx->dev, 1,
1045 "%s data will not fit into plane (%lu < %lu)\n",
1046 __func__, vb2_plane_size(vb, 0),
1047 (long)q_data->sizeimage);
1048 return -EINVAL;
1049 }
1050
1051 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
1052
1053 return 0;
1054}
1055
1056static void vim2m_buf_queue(struct vb2_buffer *vb)
1057{
1058 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1059 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1060
1061 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1062}
1063
1064static int vim2m_start_streaming(struct vb2_queue *q, unsigned int count)
1065{
1066 struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
1067 struct vim2m_q_data *q_data = get_q_data(ctx, q->type);
1068
1069 if (!q_data)
1070 return -EINVAL;
1071
1072 q_data->sequence = 0;
1073 return 0;
1074}
1075
1076static void vim2m_stop_streaming(struct vb2_queue *q)
1077{
1078 struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
1079 struct vb2_v4l2_buffer *vbuf;
1080 unsigned long flags;
1081
1082 cancel_delayed_work_sync(&ctx->work_run);
1083
1084 for (;;) {
1085 if (V4L2_TYPE_IS_OUTPUT(q->type))
1086 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1087 else
1088 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1089 if (!vbuf)
1090 return;
1091 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
1092 &ctx->hdl);
1093 spin_lock_irqsave(&ctx->irqlock, flags);
1094 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
1095 spin_unlock_irqrestore(&ctx->irqlock, flags);
1096 }
1097}
1098
1099static void vim2m_buf_request_complete(struct vb2_buffer *vb)
1100{
1101 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1102
1103 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
1104}
1105
1106static const struct vb2_ops vim2m_qops = {
1107 .queue_setup = vim2m_queue_setup,
1108 .buf_out_validate = vim2m_buf_out_validate,
1109 .buf_prepare = vim2m_buf_prepare,
1110 .buf_queue = vim2m_buf_queue,
1111 .start_streaming = vim2m_start_streaming,
1112 .stop_streaming = vim2m_stop_streaming,
1113 .wait_prepare = vb2_ops_wait_prepare,
1114 .wait_finish = vb2_ops_wait_finish,
1115 .buf_request_complete = vim2m_buf_request_complete,
1116};
1117
1118static int queue_init(void *priv, struct vb2_queue *src_vq,
1119 struct vb2_queue *dst_vq)
1120{
1121 struct vim2m_ctx *ctx = priv;
1122 int ret;
1123
1124 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1125 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1126 src_vq->drv_priv = ctx;
1127 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1128 src_vq->ops = &vim2m_qops;
1129 src_vq->mem_ops = &vb2_vmalloc_memops;
1130 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1131 src_vq->lock = &ctx->vb_mutex;
1132 src_vq->supports_requests = true;
1133
1134 ret = vb2_queue_init(src_vq);
1135 if (ret)
1136 return ret;
1137
1138 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1139 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1140 dst_vq->drv_priv = ctx;
1141 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1142 dst_vq->ops = &vim2m_qops;
1143 dst_vq->mem_ops = &vb2_vmalloc_memops;
1144 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1145 dst_vq->lock = &ctx->vb_mutex;
1146
1147 return vb2_queue_init(dst_vq);
1148}
1149
1150static struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = {
1151 .ops = &vim2m_ctrl_ops,
1152 .id = V4L2_CID_TRANS_TIME_MSEC,
1153 .name = "Transaction Time (msec)",
1154 .type = V4L2_CTRL_TYPE_INTEGER,
1155 .min = 1,
1156 .max = 10001,
1157 .step = 1,
1158};
1159
1160static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = {
1161 .ops = &vim2m_ctrl_ops,
1162 .id = V4L2_CID_TRANS_NUM_BUFS,
1163 .name = "Buffers Per Transaction",
1164 .type = V4L2_CTRL_TYPE_INTEGER,
1165 .def = 1,
1166 .min = 1,
1167 .max = MEM2MEM_DEF_NUM_BUFS,
1168 .step = 1,
1169};
1170
1171/*
1172 * File operations
1173 */
1174static int vim2m_open(struct file *file)
1175{
1176 struct vim2m_dev *dev = video_drvdata(file);
1177 struct vim2m_ctx *ctx = NULL;
1178 struct v4l2_ctrl_handler *hdl;
1179 int rc = 0;
1180
1181 if (mutex_lock_interruptible(&dev->dev_mutex))
1182 return -ERESTARTSYS;
1183 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1184 if (!ctx) {
1185 rc = -ENOMEM;
1186 goto open_unlock;
1187 }
1188
1189 v4l2_fh_init(&ctx->fh, video_devdata(file));
1190 file->private_data = &ctx->fh;
1191 ctx->dev = dev;
1192 hdl = &ctx->hdl;
1193 v4l2_ctrl_handler_init(hdl, 4);
1194 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
1195 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
1196
1197 vim2m_ctrl_trans_time_msec.def = default_transtime;
1198 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
1199 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL);
1200 if (hdl->error) {
1201 rc = hdl->error;
1202 v4l2_ctrl_handler_free(hdl);
1203 kfree(ctx);
1204 goto open_unlock;
1205 }
1206 ctx->fh.ctrl_handler = hdl;
1207 v4l2_ctrl_handler_setup(hdl);
1208
1209 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
1210 ctx->q_data[V4L2_M2M_SRC].width = 640;
1211 ctx->q_data[V4L2_M2M_SRC].height = 480;
1212 ctx->q_data[V4L2_M2M_SRC].sizeimage =
1213 ctx->q_data[V4L2_M2M_SRC].width *
1214 ctx->q_data[V4L2_M2M_SRC].height *
1215 (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
1216 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
1217 ctx->colorspace = V4L2_COLORSPACE_REC709;
1218
1219 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
1220
1221 mutex_init(&ctx->vb_mutex);
1222 spin_lock_init(&ctx->irqlock);
1223 INIT_DELAYED_WORK(&ctx->work_run, device_work);
1224
1225 if (IS_ERR(ctx->fh.m2m_ctx)) {
1226 rc = PTR_ERR(ctx->fh.m2m_ctx);
1227
1228 v4l2_ctrl_handler_free(hdl);
1229 v4l2_fh_exit(&ctx->fh);
1230 kfree(ctx);
1231 goto open_unlock;
1232 }
1233
1234 v4l2_fh_add(&ctx->fh);
1235 atomic_inc(&dev->num_inst);
1236
1237 dprintk(dev, 1, "Created instance: %p, m2m_ctx: %p\n",
1238 ctx, ctx->fh.m2m_ctx);
1239
1240open_unlock:
1241 mutex_unlock(&dev->dev_mutex);
1242 return rc;
1243}
1244
1245static int vim2m_release(struct file *file)
1246{
1247 struct vim2m_dev *dev = video_drvdata(file);
1248 struct vim2m_ctx *ctx = file2ctx(file);
1249
1250 dprintk(dev, 1, "Releasing instance %p\n", ctx);
1251
1252 v4l2_fh_del(&ctx->fh);
1253 v4l2_fh_exit(&ctx->fh);
1254 v4l2_ctrl_handler_free(&ctx->hdl);
1255 mutex_lock(&dev->dev_mutex);
1256 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1257 mutex_unlock(&dev->dev_mutex);
1258 kfree(ctx);
1259
1260 atomic_dec(&dev->num_inst);
1261
1262 return 0;
1263}
1264
1265static const struct v4l2_file_operations vim2m_fops = {
1266 .owner = THIS_MODULE,
1267 .open = vim2m_open,
1268 .release = vim2m_release,
1269 .poll = v4l2_m2m_fop_poll,
1270 .unlocked_ioctl = video_ioctl2,
1271 .mmap = v4l2_m2m_fop_mmap,
1272};
1273
1274static const struct video_device vim2m_videodev = {
1275 .name = MEM2MEM_NAME,
1276 .vfl_dir = VFL_DIR_M2M,
1277 .fops = &vim2m_fops,
1278 .ioctl_ops = &vim2m_ioctl_ops,
1279 .minor = -1,
1280 .release = video_device_release_empty,
1281 .device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
1282};
1283
1284static const struct v4l2_m2m_ops m2m_ops = {
1285 .device_run = device_run,
1286 .job_ready = job_ready,
1287 .job_abort = job_abort,
1288};
1289
1290static const struct media_device_ops m2m_media_ops = {
1291 .req_validate = vb2_request_validate,
1292 .req_queue = v4l2_m2m_request_queue,
1293};
1294
1295static int vim2m_probe(struct platform_device *pdev)
1296{
1297 struct vim2m_dev *dev;
1298 struct video_device *vfd;
1299 int ret;
1300
1301 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1302 if (!dev)
1303 return -ENOMEM;
1304
1305 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1306 if (ret)
1307 return ret;
1308
1309 atomic_set(&dev->num_inst, 0);
1310 mutex_init(&dev->dev_mutex);
1311
1312 dev->vfd = vim2m_videodev;
1313 vfd = &dev->vfd;
1314 vfd->lock = &dev->dev_mutex;
1315 vfd->v4l2_dev = &dev->v4l2_dev;
1316
1317 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1318 if (ret) {
1319 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1320 goto unreg_v4l2;
1321 }
1322
1323 video_set_drvdata(vfd, dev);
1324 v4l2_info(&dev->v4l2_dev,
1325 "Device registered as /dev/video%d\n", vfd->num);
1326
1327 platform_set_drvdata(pdev, dev);
1328
1329 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1330 if (IS_ERR(dev->m2m_dev)) {
1331 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1332 ret = PTR_ERR(dev->m2m_dev);
1333 goto unreg_dev;
1334 }
1335
1336#ifdef CONFIG_MEDIA_CONTROLLER
1337 dev->mdev.dev = &pdev->dev;
1338 strscpy(dev->mdev.model, "vim2m", sizeof(dev->mdev.model));
1339 strscpy(dev->mdev.bus_info, "platform:vim2m",
1340 sizeof(dev->mdev.bus_info));
1341 media_device_init(&dev->mdev);
1342 dev->mdev.ops = &m2m_media_ops;
1343 dev->v4l2_dev.mdev = &dev->mdev;
1344
1345 ret = v4l2_m2m_register_media_controller(dev->m2m_dev, vfd,
1346 MEDIA_ENT_F_PROC_VIDEO_SCALER);
1347 if (ret) {
1348 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n");
1349 goto unreg_m2m;
1350 }
1351
1352 ret = media_device_register(&dev->mdev);
1353 if (ret) {
1354 v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n");
1355 goto unreg_m2m_mc;
1356 }
1357#endif
1358 return 0;
1359
1360#ifdef CONFIG_MEDIA_CONTROLLER
1361unreg_m2m_mc:
1362 v4l2_m2m_unregister_media_controller(dev->m2m_dev);
1363unreg_m2m:
1364 v4l2_m2m_release(dev->m2m_dev);
1365#endif
1366unreg_dev:
1367 video_unregister_device(&dev->vfd);
1368unreg_v4l2:
1369 v4l2_device_unregister(&dev->v4l2_dev);
1370
1371 return ret;
1372}
1373
1374static int vim2m_remove(struct platform_device *pdev)
1375{
1376 struct vim2m_dev *dev = platform_get_drvdata(pdev);
1377
1378 v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
1379
1380#ifdef CONFIG_MEDIA_CONTROLLER
1381 media_device_unregister(&dev->mdev);
1382 v4l2_m2m_unregister_media_controller(dev->m2m_dev);
1383 media_device_cleanup(&dev->mdev);
1384#endif
1385 v4l2_m2m_release(dev->m2m_dev);
1386 video_unregister_device(&dev->vfd);
1387 v4l2_device_unregister(&dev->v4l2_dev);
1388
1389 return 0;
1390}
1391
1392static struct platform_driver vim2m_pdrv = {
1393 .probe = vim2m_probe,
1394 .remove = vim2m_remove,
1395 .driver = {
1396 .name = MEM2MEM_NAME,
1397 },
1398};
1399
1400static void __exit vim2m_exit(void)
1401{
1402 platform_driver_unregister(&vim2m_pdrv);
1403 platform_device_unregister(&vim2m_pdev);
1404}
1405
1406static int __init vim2m_init(void)
1407{
1408 int ret;
1409
1410 ret = platform_device_register(&vim2m_pdev);
1411 if (ret)
1412 return ret;
1413
1414 ret = platform_driver_register(&vim2m_pdrv);
1415 if (ret)
1416 platform_device_unregister(&vim2m_pdev);
1417
1418 return ret;
1419}
1420
1421module_init(vim2m_init);
1422module_exit(vim2m_exit);