Commit | Line | Data |
---|---|---|
96d8eab5 PO |
1 | /* |
2 | * A virtual v4l2-mem2mem example device. | |
3 | * | |
4 | * This is a virtual device driver for testing mem-to-mem videobuf framework. | |
5 | * It simulates a device that uses memory buffers for both source and | |
144bd0ee HV |
6 | * destination, processes the data and issues an "irq" (simulated by a delayed |
7 | * workqueue). | |
96d8eab5 PO |
8 | * The device is capable of multi-instance, multi-buffer-per-transaction |
9 | * operation (via the mem2mem framework). | |
10 | * | |
11 | * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd. | |
95072084 | 12 | * Pawel Osciak, <pawel@osciak.com> |
96d8eab5 PO |
13 | * Marek Szyprowski, <m.szyprowski@samsung.com> |
14 | * | |
15 | * This program is free software; you can redistribute it and/or modify | |
16 | * it under the terms of the GNU General Public License as published by the | |
17 | * Free Software Foundation; either version 2 of the | |
18 | * License, or (at your option) any later version | |
19 | */ | |
20 | #include <linux/module.h> | |
21 | #include <linux/delay.h> | |
22 | #include <linux/fs.h> | |
96d8eab5 | 23 | #include <linux/sched.h> |
6b46c397 | 24 | #include <linux/slab.h> |
96d8eab5 PO |
25 | |
26 | #include <linux/platform_device.h> | |
27 | #include <media/v4l2-mem2mem.h> | |
28 | #include <media/v4l2-device.h> | |
29 | #include <media/v4l2-ioctl.h> | |
d3dd59c2 | 30 | #include <media/v4l2-ctrls.h> |
97a3c902 | 31 | #include <media/v4l2-event.h> |
d80ee38c | 32 | #include <media/videobuf2-vmalloc.h> |
96d8eab5 | 33 | |
96d8eab5 | 34 | MODULE_DESCRIPTION("Virtual device for mem2mem framework testing"); |
95072084 | 35 | MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>"); |
96d8eab5 | 36 | MODULE_LICENSE("GPL"); |
1990d50b | 37 | MODULE_VERSION("0.1.1"); |
1f923a42 | 38 | MODULE_ALIAS("mem2mem_testdev"); |
96d8eab5 | 39 | |
d95d7c64 HV |
40 | static unsigned debug; |
41 | module_param(debug, uint, 0644); | |
42 | MODULE_PARM_DESC(debug, "activates debug info"); | |
43 | ||
96d8eab5 PO |
44 | #define MIN_W 32 |
45 | #define MIN_H 32 | |
46 | #define MAX_W 640 | |
47 | #define MAX_H 480 | |
9ce3ce4d | 48 | #define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */ |
96d8eab5 PO |
49 | |
50 | /* Flags that indicate a format can be used for capture/output */ | |
51 | #define MEM2MEM_CAPTURE (1 << 0) | |
52 | #define MEM2MEM_OUTPUT (1 << 1) | |
53 | ||
1f923a42 | 54 | #define MEM2MEM_NAME "vim2m" |
96d8eab5 PO |
55 | |
56 | /* Per queue */ | |
57 | #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME | |
58 | /* In bytes, per queue */ | |
59 | #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024) | |
60 | ||
61 | /* Default transaction time in msec */ | |
43405831 | 62 | #define MEM2MEM_DEF_TRANSTIME 40 |
96d8eab5 PO |
63 | #define MEM2MEM_COLOR_STEP (0xff >> 4) |
64 | #define MEM2MEM_NUM_TILES 8 | |
65 | ||
80072871 TM |
66 | /* Flags that indicate processing mode */ |
67 | #define MEM2MEM_HFLIP (1 << 0) | |
68 | #define MEM2MEM_VFLIP (1 << 1) | |
69 | ||
96d8eab5 | 70 | #define dprintk(dev, fmt, arg...) \ |
d95d7c64 | 71 | v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg) |
96d8eab5 PO |
72 | |
73 | ||
1f923a42 | 74 | static void vim2m_dev_release(struct device *dev) |
96d8eab5 PO |
75 | {} |
76 | ||
1f923a42 | 77 | static struct platform_device vim2m_pdev = { |
96d8eab5 | 78 | .name = MEM2MEM_NAME, |
1f923a42 | 79 | .dev.release = vim2m_dev_release, |
96d8eab5 PO |
80 | }; |
81 | ||
1f923a42 | 82 | struct vim2m_fmt { |
96d8eab5 PO |
83 | u32 fourcc; |
84 | int depth; | |
85 | /* Types the format can be used for */ | |
86 | u32 types; | |
87 | }; | |
88 | ||
1f923a42 | 89 | static struct vim2m_fmt formats[] = { |
96d8eab5 | 90 | { |
96d8eab5 PO |
91 | .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */ |
92 | .depth = 16, | |
93 | /* Both capture and output format */ | |
94 | .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT, | |
95 | }, | |
96 | { | |
96d8eab5 PO |
97 | .fourcc = V4L2_PIX_FMT_YUYV, |
98 | .depth = 16, | |
99 | /* Output-only format */ | |
100 | .types = MEM2MEM_OUTPUT, | |
101 | }, | |
102 | }; | |
103 | ||
d3dd59c2 HV |
104 | #define NUM_FORMATS ARRAY_SIZE(formats) |
105 | ||
96d8eab5 | 106 | /* Per-queue, driver-specific private data */ |
1f923a42 | 107 | struct vim2m_q_data { |
96d8eab5 PO |
108 | unsigned int width; |
109 | unsigned int height; | |
110 | unsigned int sizeimage; | |
ca5f5fdb | 111 | unsigned int sequence; |
1f923a42 | 112 | struct vim2m_fmt *fmt; |
96d8eab5 PO |
113 | }; |
114 | ||
115 | enum { | |
116 | V4L2_M2M_SRC = 0, | |
117 | V4L2_M2M_DST = 1, | |
118 | }; | |
119 | ||
d3dd59c2 HV |
120 | #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000) |
121 | #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001) | |
96d8eab5 | 122 | |
1f923a42 | 123 | static struct vim2m_fmt *find_format(struct v4l2_format *f) |
96d8eab5 | 124 | { |
1f923a42 | 125 | struct vim2m_fmt *fmt; |
96d8eab5 PO |
126 | unsigned int k; |
127 | ||
128 | for (k = 0; k < NUM_FORMATS; k++) { | |
129 | fmt = &formats[k]; | |
130 | if (fmt->fourcc == f->fmt.pix.pixelformat) | |
131 | break; | |
132 | } | |
133 | ||
134 | if (k == NUM_FORMATS) | |
135 | return NULL; | |
136 | ||
137 | return &formats[k]; | |
138 | } | |
139 | ||
1f923a42 | 140 | struct vim2m_dev { |
96d8eab5 | 141 | struct v4l2_device v4l2_dev; |
09365420 | 142 | struct video_device vfd; |
e35f7021 HV |
143 | #ifdef CONFIG_MEDIA_CONTROLLER |
144 | struct media_device mdev; | |
145 | #endif | |
96d8eab5 PO |
146 | |
147 | atomic_t num_inst; | |
148 | struct mutex dev_mutex; | |
149 | spinlock_t irqlock; | |
150 | ||
144bd0ee | 151 | struct delayed_work work_run; |
96d8eab5 PO |
152 | |
153 | struct v4l2_m2m_dev *m2m_dev; | |
154 | }; | |
155 | ||
1f923a42 | 156 | struct vim2m_ctx { |
d3dd59c2 | 157 | struct v4l2_fh fh; |
1f923a42 | 158 | struct vim2m_dev *dev; |
96d8eab5 | 159 | |
d3dd59c2 HV |
160 | struct v4l2_ctrl_handler hdl; |
161 | ||
96d8eab5 PO |
162 | /* Processed buffers in this transaction */ |
163 | u8 num_processed; | |
164 | ||
165 | /* Transaction length (i.e. how many buffers per transaction) */ | |
166 | u32 translen; | |
167 | /* Transaction time (i.e. simulated processing time) in milliseconds */ | |
168 | u32 transtime; | |
169 | ||
170 | /* Abort requested by m2m */ | |
171 | int aborting; | |
172 | ||
80072871 TM |
173 | /* Processing mode */ |
174 | int mode; | |
175 | ||
47556ffa | 176 | enum v4l2_colorspace colorspace; |
9f8b3333 HV |
177 | enum v4l2_ycbcr_encoding ycbcr_enc; |
178 | enum v4l2_xfer_func xfer_func; | |
179 | enum v4l2_quantization quant; | |
47556ffa | 180 | |
9f4161a6 | 181 | /* Source and destination queue data */ |
1f923a42 | 182 | struct vim2m_q_data q_data[2]; |
96d8eab5 PO |
183 | }; |
184 | ||
1f923a42 | 185 | static inline struct vim2m_ctx *file2ctx(struct file *file) |
d3dd59c2 | 186 | { |
1f923a42 | 187 | return container_of(file->private_data, struct vim2m_ctx, fh); |
d3dd59c2 HV |
188 | } |
189 | ||
1f923a42 | 190 | static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx, |
9f4161a6 TM |
191 | enum v4l2_buf_type type) |
192 | { | |
193 | switch (type) { | |
194 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: | |
195 | return &ctx->q_data[V4L2_M2M_SRC]; | |
196 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: | |
197 | return &ctx->q_data[V4L2_M2M_DST]; | |
198 | default: | |
199 | BUG(); | |
200 | } | |
201 | return NULL; | |
202 | } | |
203 | ||
204 | ||
1f923a42 | 205 | static int device_process(struct vim2m_ctx *ctx, |
2d700715 JS |
206 | struct vb2_v4l2_buffer *in_vb, |
207 | struct vb2_v4l2_buffer *out_vb) | |
96d8eab5 | 208 | { |
1f923a42 HV |
209 | struct vim2m_dev *dev = ctx->dev; |
210 | struct vim2m_q_data *q_data; | |
96d8eab5 PO |
211 | u8 *p_in, *p_out; |
212 | int x, y, t, w; | |
213 | int tile_w, bytes_left; | |
d80ee38c | 214 | int width, height, bytesperline; |
96d8eab5 | 215 | |
9f4161a6 | 216 | q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
d80ee38c MS |
217 | |
218 | width = q_data->width; | |
219 | height = q_data->height; | |
220 | bytesperline = (q_data->width * q_data->fmt->depth) >> 3; | |
221 | ||
2d700715 JS |
222 | p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0); |
223 | p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0); | |
96d8eab5 PO |
224 | if (!p_in || !p_out) { |
225 | v4l2_err(&dev->v4l2_dev, | |
226 | "Acquiring kernel pointers to buffers failed\n"); | |
227 | return -EFAULT; | |
228 | } | |
229 | ||
2d700715 JS |
230 | if (vb2_plane_size(&in_vb->vb2_buf, 0) > |
231 | vb2_plane_size(&out_vb->vb2_buf, 0)) { | |
96d8eab5 PO |
232 | v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n"); |
233 | return -EINVAL; | |
234 | } | |
235 | ||
d80ee38c | 236 | tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3)) |
96d8eab5 | 237 | / MEM2MEM_NUM_TILES; |
d80ee38c | 238 | bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES; |
96d8eab5 PO |
239 | w = 0; |
240 | ||
2d700715 JS |
241 | out_vb->sequence = |
242 | get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++; | |
243 | in_vb->sequence = q_data->sequence++; | |
d6dd645e | 244 | out_vb->vb2_buf.timestamp = in_vb->vb2_buf.timestamp; |
2d700715 JS |
245 | |
246 | if (in_vb->flags & V4L2_BUF_FLAG_TIMECODE) | |
247 | out_vb->timecode = in_vb->timecode; | |
248 | out_vb->field = in_vb->field; | |
249 | out_vb->flags = in_vb->flags & | |
ca5f5fdb HV |
250 | (V4L2_BUF_FLAG_TIMECODE | |
251 | V4L2_BUF_FLAG_KEYFRAME | | |
252 | V4L2_BUF_FLAG_PFRAME | | |
253 | V4L2_BUF_FLAG_BFRAME | | |
254 | V4L2_BUF_FLAG_TSTAMP_SRC_MASK); | |
d95d7c64 | 255 | |
80072871 TM |
256 | switch (ctx->mode) { |
257 | case MEM2MEM_HFLIP | MEM2MEM_VFLIP: | |
258 | p_out += bytesperline * height - bytes_left; | |
259 | for (y = 0; y < height; ++y) { | |
260 | for (t = 0; t < MEM2MEM_NUM_TILES; ++t) { | |
261 | if (w & 0x1) { | |
262 | for (x = 0; x < tile_w; ++x) | |
263 | *--p_out = *p_in++ + | |
264 | MEM2MEM_COLOR_STEP; | |
265 | } else { | |
266 | for (x = 0; x < tile_w; ++x) | |
267 | *--p_out = *p_in++ - | |
268 | MEM2MEM_COLOR_STEP; | |
269 | } | |
270 | ++w; | |
96d8eab5 | 271 | } |
80072871 TM |
272 | p_in += bytes_left; |
273 | p_out -= bytes_left; | |
274 | } | |
275 | break; | |
276 | ||
277 | case MEM2MEM_HFLIP: | |
278 | for (y = 0; y < height; ++y) { | |
279 | p_out += MEM2MEM_NUM_TILES * tile_w; | |
280 | for (t = 0; t < MEM2MEM_NUM_TILES; ++t) { | |
281 | if (w & 0x01) { | |
282 | for (x = 0; x < tile_w; ++x) | |
283 | *--p_out = *p_in++ + | |
284 | MEM2MEM_COLOR_STEP; | |
285 | } else { | |
286 | for (x = 0; x < tile_w; ++x) | |
287 | *--p_out = *p_in++ - | |
288 | MEM2MEM_COLOR_STEP; | |
289 | } | |
290 | ++w; | |
291 | } | |
292 | p_in += bytes_left; | |
293 | p_out += bytesperline; | |
294 | } | |
295 | break; | |
296 | ||
297 | case MEM2MEM_VFLIP: | |
298 | p_out += bytesperline * (height - 1); | |
299 | for (y = 0; y < height; ++y) { | |
300 | for (t = 0; t < MEM2MEM_NUM_TILES; ++t) { | |
301 | if (w & 0x1) { | |
302 | for (x = 0; x < tile_w; ++x) | |
303 | *p_out++ = *p_in++ + | |
304 | MEM2MEM_COLOR_STEP; | |
305 | } else { | |
306 | for (x = 0; x < tile_w; ++x) | |
307 | *p_out++ = *p_in++ - | |
308 | MEM2MEM_COLOR_STEP; | |
309 | } | |
310 | ++w; | |
311 | } | |
312 | p_in += bytes_left; | |
313 | p_out += bytes_left - 2 * bytesperline; | |
314 | } | |
315 | break; | |
316 | ||
317 | default: | |
318 | for (y = 0; y < height; ++y) { | |
319 | for (t = 0; t < MEM2MEM_NUM_TILES; ++t) { | |
320 | if (w & 0x1) { | |
321 | for (x = 0; x < tile_w; ++x) | |
322 | *p_out++ = *p_in++ + | |
323 | MEM2MEM_COLOR_STEP; | |
324 | } else { | |
325 | for (x = 0; x < tile_w; ++x) | |
326 | *p_out++ = *p_in++ - | |
327 | MEM2MEM_COLOR_STEP; | |
328 | } | |
329 | ++w; | |
330 | } | |
331 | p_in += bytes_left; | |
332 | p_out += bytes_left; | |
96d8eab5 | 333 | } |
96d8eab5 PO |
334 | } |
335 | ||
336 | return 0; | |
337 | } | |
338 | ||
96d8eab5 PO |
339 | /* |
340 | * mem2mem callbacks | |
341 | */ | |
342 | ||
cba862dc | 343 | /* |
96d8eab5 PO |
344 | * job_ready() - check whether an instance is ready to be scheduled to run |
345 | */ | |
346 | static int job_ready(void *priv) | |
347 | { | |
1f923a42 | 348 | struct vim2m_ctx *ctx = priv; |
96d8eab5 | 349 | |
20702ebc SN |
350 | if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen |
351 | || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) { | |
96d8eab5 PO |
352 | dprintk(ctx->dev, "Not enough buffers available\n"); |
353 | return 0; | |
354 | } | |
355 | ||
356 | return 1; | |
357 | } | |
358 | ||
359 | static void job_abort(void *priv) | |
360 | { | |
1f923a42 | 361 | struct vim2m_ctx *ctx = priv; |
96d8eab5 PO |
362 | |
363 | /* Will cancel the transaction in the next interrupt handler */ | |
364 | ctx->aborting = 1; | |
365 | } | |
366 | ||
367 | /* device_run() - prepares and starts the device | |
368 | * | |
369 | * This simulates all the immediate preparations required before starting | |
370 | * a device. This will be called by the framework when it decides to schedule | |
371 | * a particular instance. | |
372 | */ | |
373 | static void device_run(void *priv) | |
374 | { | |
1f923a42 HV |
375 | struct vim2m_ctx *ctx = priv; |
376 | struct vim2m_dev *dev = ctx->dev; | |
2d700715 | 377 | struct vb2_v4l2_buffer *src_buf, *dst_buf; |
96d8eab5 | 378 | |
20702ebc SN |
379 | src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); |
380 | dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); | |
96d8eab5 | 381 | |
86b93b2c HV |
382 | /* Apply request controls if any */ |
383 | v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req, | |
384 | &ctx->hdl); | |
385 | ||
96d8eab5 PO |
386 | device_process(ctx, src_buf, dst_buf); |
387 | ||
86b93b2c HV |
388 | /* Complete request controls if any */ |
389 | v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req, | |
390 | &ctx->hdl); | |
391 | ||
144bd0ee HV |
392 | /* Run delayed work, which simulates a hardware irq */ |
393 | schedule_delayed_work(&dev->work_run, msecs_to_jiffies(ctx->transtime)); | |
96d8eab5 PO |
394 | } |
395 | ||
144bd0ee | 396 | static void device_work(struct work_struct *w) |
96d8eab5 | 397 | { |
144bd0ee HV |
398 | struct vim2m_dev *vim2m_dev = |
399 | container_of(w, struct vim2m_dev, work_run.work); | |
1f923a42 | 400 | struct vim2m_ctx *curr_ctx; |
2d700715 | 401 | struct vb2_v4l2_buffer *src_vb, *dst_vb; |
96d8eab5 PO |
402 | unsigned long flags; |
403 | ||
1f923a42 | 404 | curr_ctx = v4l2_m2m_get_curr_priv(vim2m_dev->m2m_dev); |
96d8eab5 PO |
405 | |
406 | if (NULL == curr_ctx) { | |
26fdcf09 | 407 | pr_err("Instance released before the end of transaction\n"); |
96d8eab5 PO |
408 | return; |
409 | } | |
410 | ||
20702ebc SN |
411 | src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx); |
412 | dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx); | |
d80ee38c | 413 | |
96d8eab5 PO |
414 | curr_ctx->num_processed++; |
415 | ||
1f923a42 | 416 | spin_lock_irqsave(&vim2m_dev->irqlock, flags); |
d80ee38c MS |
417 | v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE); |
418 | v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE); | |
1f923a42 | 419 | spin_unlock_irqrestore(&vim2m_dev->irqlock, flags); |
d80ee38c | 420 | |
96d8eab5 PO |
421 | if (curr_ctx->num_processed == curr_ctx->translen |
422 | || curr_ctx->aborting) { | |
423 | dprintk(curr_ctx->dev, "Finishing transaction\n"); | |
424 | curr_ctx->num_processed = 0; | |
1f923a42 | 425 | v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx); |
96d8eab5 | 426 | } else { |
96d8eab5 PO |
427 | device_run(curr_ctx); |
428 | } | |
429 | } | |
430 | ||
96d8eab5 PO |
431 | /* |
432 | * video ioctls | |
433 | */ | |
434 | static int vidioc_querycap(struct file *file, void *priv, | |
435 | struct v4l2_capability *cap) | |
436 | { | |
437 | strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1); | |
438 | strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1); | |
72c2af6e HV |
439 | snprintf(cap->bus_info, sizeof(cap->bus_info), |
440 | "platform:%s", MEM2MEM_NAME); | |
00424c7e | 441 | cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING; |
b0d14996 | 442 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
96d8eab5 PO |
443 | return 0; |
444 | } | |
445 | ||
446 | static int enum_fmt(struct v4l2_fmtdesc *f, u32 type) | |
447 | { | |
448 | int i, num; | |
1f923a42 | 449 | struct vim2m_fmt *fmt; |
96d8eab5 PO |
450 | |
451 | num = 0; | |
452 | ||
453 | for (i = 0; i < NUM_FORMATS; ++i) { | |
454 | if (formats[i].types & type) { | |
455 | /* index-th format of type type found ? */ | |
456 | if (num == f->index) | |
457 | break; | |
458 | /* Correct type but haven't reached our index yet, | |
459 | * just increment per-type index */ | |
460 | ++num; | |
461 | } | |
462 | } | |
463 | ||
464 | if (i < NUM_FORMATS) { | |
465 | /* Format found */ | |
466 | fmt = &formats[i]; | |
96d8eab5 PO |
467 | f->pixelformat = fmt->fourcc; |
468 | return 0; | |
469 | } | |
470 | ||
471 | /* Format not found */ | |
472 | return -EINVAL; | |
473 | } | |
474 | ||
475 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, | |
476 | struct v4l2_fmtdesc *f) | |
477 | { | |
478 | return enum_fmt(f, MEM2MEM_CAPTURE); | |
479 | } | |
480 | ||
481 | static int vidioc_enum_fmt_vid_out(struct file *file, void *priv, | |
482 | struct v4l2_fmtdesc *f) | |
483 | { | |
484 | return enum_fmt(f, MEM2MEM_OUTPUT); | |
485 | } | |
486 | ||
1f923a42 | 487 | static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f) |
96d8eab5 | 488 | { |
d80ee38c | 489 | struct vb2_queue *vq; |
1f923a42 | 490 | struct vim2m_q_data *q_data; |
96d8eab5 | 491 | |
20702ebc | 492 | vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); |
96d8eab5 PO |
493 | if (!vq) |
494 | return -EINVAL; | |
495 | ||
9f4161a6 | 496 | q_data = get_q_data(ctx, f->type); |
96d8eab5 PO |
497 | |
498 | f->fmt.pix.width = q_data->width; | |
499 | f->fmt.pix.height = q_data->height; | |
d80ee38c | 500 | f->fmt.pix.field = V4L2_FIELD_NONE; |
96d8eab5 PO |
501 | f->fmt.pix.pixelformat = q_data->fmt->fourcc; |
502 | f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3; | |
503 | f->fmt.pix.sizeimage = q_data->sizeimage; | |
47556ffa | 504 | f->fmt.pix.colorspace = ctx->colorspace; |
9f8b3333 HV |
505 | f->fmt.pix.xfer_func = ctx->xfer_func; |
506 | f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc; | |
507 | f->fmt.pix.quantization = ctx->quant; | |
96d8eab5 PO |
508 | |
509 | return 0; | |
510 | } | |
511 | ||
512 | static int vidioc_g_fmt_vid_out(struct file *file, void *priv, | |
513 | struct v4l2_format *f) | |
514 | { | |
d3dd59c2 | 515 | return vidioc_g_fmt(file2ctx(file), f); |
96d8eab5 PO |
516 | } |
517 | ||
518 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, | |
519 | struct v4l2_format *f) | |
520 | { | |
d3dd59c2 | 521 | return vidioc_g_fmt(file2ctx(file), f); |
96d8eab5 PO |
522 | } |
523 | ||
1f923a42 | 524 | static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt) |
96d8eab5 | 525 | { |
96d8eab5 PO |
526 | /* V4L2 specification suggests the driver corrects the format struct |
527 | * if any of the dimensions is unsupported */ | |
96d8eab5 PO |
528 | if (f->fmt.pix.height < MIN_H) |
529 | f->fmt.pix.height = MIN_H; | |
530 | else if (f->fmt.pix.height > MAX_H) | |
531 | f->fmt.pix.height = MAX_H; | |
532 | ||
533 | if (f->fmt.pix.width < MIN_W) | |
534 | f->fmt.pix.width = MIN_W; | |
535 | else if (f->fmt.pix.width > MAX_W) | |
536 | f->fmt.pix.width = MAX_W; | |
537 | ||
538 | f->fmt.pix.width &= ~DIM_ALIGN_MASK; | |
539 | f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3; | |
540 | f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; | |
5c3112b5 | 541 | f->fmt.pix.field = V4L2_FIELD_NONE; |
96d8eab5 PO |
542 | |
543 | return 0; | |
544 | } | |
545 | ||
546 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, | |
547 | struct v4l2_format *f) | |
548 | { | |
1f923a42 HV |
549 | struct vim2m_fmt *fmt; |
550 | struct vim2m_ctx *ctx = file2ctx(file); | |
96d8eab5 PO |
551 | |
552 | fmt = find_format(f); | |
4e8ec0a4 HV |
553 | if (!fmt) { |
554 | f->fmt.pix.pixelformat = formats[0].fourcc; | |
555 | fmt = find_format(f); | |
556 | } | |
557 | if (!(fmt->types & MEM2MEM_CAPTURE)) { | |
96d8eab5 PO |
558 | v4l2_err(&ctx->dev->v4l2_dev, |
559 | "Fourcc format (0x%08x) invalid.\n", | |
560 | f->fmt.pix.pixelformat); | |
561 | return -EINVAL; | |
562 | } | |
47556ffa | 563 | f->fmt.pix.colorspace = ctx->colorspace; |
9f8b3333 HV |
564 | f->fmt.pix.xfer_func = ctx->xfer_func; |
565 | f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc; | |
566 | f->fmt.pix.quantization = ctx->quant; | |
96d8eab5 PO |
567 | |
568 | return vidioc_try_fmt(f, fmt); | |
569 | } | |
570 | ||
571 | static int vidioc_try_fmt_vid_out(struct file *file, void *priv, | |
572 | struct v4l2_format *f) | |
573 | { | |
1f923a42 HV |
574 | struct vim2m_fmt *fmt; |
575 | struct vim2m_ctx *ctx = file2ctx(file); | |
96d8eab5 PO |
576 | |
577 | fmt = find_format(f); | |
4e8ec0a4 HV |
578 | if (!fmt) { |
579 | f->fmt.pix.pixelformat = formats[0].fourcc; | |
580 | fmt = find_format(f); | |
581 | } | |
582 | if (!(fmt->types & MEM2MEM_OUTPUT)) { | |
96d8eab5 PO |
583 | v4l2_err(&ctx->dev->v4l2_dev, |
584 | "Fourcc format (0x%08x) invalid.\n", | |
585 | f->fmt.pix.pixelformat); | |
586 | return -EINVAL; | |
587 | } | |
47556ffa HV |
588 | if (!f->fmt.pix.colorspace) |
589 | f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709; | |
96d8eab5 PO |
590 | |
591 | return vidioc_try_fmt(f, fmt); | |
592 | } | |
593 | ||
1f923a42 | 594 | static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f) |
96d8eab5 | 595 | { |
1f923a42 | 596 | struct vim2m_q_data *q_data; |
d80ee38c | 597 | struct vb2_queue *vq; |
96d8eab5 | 598 | |
20702ebc | 599 | vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); |
96d8eab5 PO |
600 | if (!vq) |
601 | return -EINVAL; | |
602 | ||
9f4161a6 | 603 | q_data = get_q_data(ctx, f->type); |
96d8eab5 PO |
604 | if (!q_data) |
605 | return -EINVAL; | |
606 | ||
d80ee38c | 607 | if (vb2_is_busy(vq)) { |
96d8eab5 | 608 | v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__); |
07e80305 | 609 | return -EBUSY; |
96d8eab5 PO |
610 | } |
611 | ||
612 | q_data->fmt = find_format(f); | |
613 | q_data->width = f->fmt.pix.width; | |
614 | q_data->height = f->fmt.pix.height; | |
615 | q_data->sizeimage = q_data->width * q_data->height | |
616 | * q_data->fmt->depth >> 3; | |
96d8eab5 PO |
617 | |
618 | dprintk(ctx->dev, | |
619 | "Setting format for type %d, wxh: %dx%d, fmt: %d\n", | |
620 | f->type, q_data->width, q_data->height, q_data->fmt->fourcc); | |
621 | ||
07e80305 | 622 | return 0; |
96d8eab5 PO |
623 | } |
624 | ||
625 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, | |
626 | struct v4l2_format *f) | |
627 | { | |
628 | int ret; | |
629 | ||
630 | ret = vidioc_try_fmt_vid_cap(file, priv, f); | |
631 | if (ret) | |
632 | return ret; | |
633 | ||
d3dd59c2 | 634 | return vidioc_s_fmt(file2ctx(file), f); |
96d8eab5 PO |
635 | } |
636 | ||
637 | static int vidioc_s_fmt_vid_out(struct file *file, void *priv, | |
638 | struct v4l2_format *f) | |
639 | { | |
1f923a42 | 640 | struct vim2m_ctx *ctx = file2ctx(file); |
96d8eab5 PO |
641 | int ret; |
642 | ||
643 | ret = vidioc_try_fmt_vid_out(file, priv, f); | |
644 | if (ret) | |
645 | return ret; | |
646 | ||
47556ffa | 647 | ret = vidioc_s_fmt(file2ctx(file), f); |
9f8b3333 | 648 | if (!ret) { |
47556ffa | 649 | ctx->colorspace = f->fmt.pix.colorspace; |
9f8b3333 HV |
650 | ctx->xfer_func = f->fmt.pix.xfer_func; |
651 | ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc; | |
652 | ctx->quant = f->fmt.pix.quantization; | |
653 | } | |
47556ffa | 654 | return ret; |
96d8eab5 PO |
655 | } |
656 | ||
1f923a42 | 657 | static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl) |
96d8eab5 | 658 | { |
1f923a42 HV |
659 | struct vim2m_ctx *ctx = |
660 | container_of(ctrl->handler, struct vim2m_ctx, hdl); | |
96d8eab5 PO |
661 | |
662 | switch (ctrl->id) { | |
80072871 | 663 | case V4L2_CID_HFLIP: |
d3dd59c2 | 664 | if (ctrl->val) |
80072871 TM |
665 | ctx->mode |= MEM2MEM_HFLIP; |
666 | else | |
667 | ctx->mode &= ~MEM2MEM_HFLIP; | |
668 | break; | |
669 | ||
670 | case V4L2_CID_VFLIP: | |
d3dd59c2 | 671 | if (ctrl->val) |
80072871 TM |
672 | ctx->mode |= MEM2MEM_VFLIP; |
673 | else | |
674 | ctx->mode &= ~MEM2MEM_VFLIP; | |
675 | break; | |
676 | ||
96d8eab5 | 677 | case V4L2_CID_TRANS_TIME_MSEC: |
d3dd59c2 | 678 | ctx->transtime = ctrl->val; |
96d8eab5 PO |
679 | break; |
680 | ||
681 | case V4L2_CID_TRANS_NUM_BUFS: | |
d3dd59c2 | 682 | ctx->translen = ctrl->val; |
96d8eab5 PO |
683 | break; |
684 | ||
685 | default: | |
686 | v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n"); | |
687 | return -EINVAL; | |
688 | } | |
689 | ||
690 | return 0; | |
691 | } | |
692 | ||
1f923a42 HV |
693 | static const struct v4l2_ctrl_ops vim2m_ctrl_ops = { |
694 | .s_ctrl = vim2m_s_ctrl, | |
d3dd59c2 HV |
695 | }; |
696 | ||
96d8eab5 | 697 | |
1f923a42 | 698 | static const struct v4l2_ioctl_ops vim2m_ioctl_ops = { |
96d8eab5 PO |
699 | .vidioc_querycap = vidioc_querycap, |
700 | ||
701 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, | |
702 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, | |
703 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, | |
704 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, | |
705 | ||
706 | .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out, | |
707 | .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out, | |
708 | .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out, | |
709 | .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out, | |
710 | ||
20702ebc SN |
711 | .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, |
712 | .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, | |
713 | .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, | |
714 | .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, | |
7bcff1c2 HV |
715 | .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, |
716 | .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, | |
f5294f45 | 717 | .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, |
96d8eab5 | 718 | |
20702ebc SN |
719 | .vidioc_streamon = v4l2_m2m_ioctl_streamon, |
720 | .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, | |
96d8eab5 | 721 | |
97a3c902 HV |
722 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
723 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
96d8eab5 PO |
724 | }; |
725 | ||
726 | ||
727 | /* | |
728 | * Queue operations | |
729 | */ | |
730 | ||
1f923a42 | 731 | static int vim2m_queue_setup(struct vb2_queue *vq, |
fc714e70 | 732 | unsigned int *nbuffers, unsigned int *nplanes, |
36c0f8b3 | 733 | unsigned int sizes[], struct device *alloc_devs[]) |
96d8eab5 | 734 | { |
1f923a42 HV |
735 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vq); |
736 | struct vim2m_q_data *q_data; | |
d80ee38c | 737 | unsigned int size, count = *nbuffers; |
96d8eab5 | 738 | |
9f4161a6 | 739 | q_data = get_q_data(ctx, vq->type); |
96d8eab5 | 740 | |
d80ee38c MS |
741 | size = q_data->width * q_data->height * q_data->fmt->depth >> 3; |
742 | ||
743 | while (size * count > MEM2MEM_VID_MEM_LIMIT) | |
744 | (count)--; | |
df9ecb0c HV |
745 | *nbuffers = count; |
746 | ||
747 | if (*nplanes) | |
748 | return sizes[0] < size ? -EINVAL : 0; | |
96d8eab5 | 749 | |
d80ee38c | 750 | *nplanes = 1; |
d80ee38c | 751 | sizes[0] = size; |
96d8eab5 | 752 | |
d80ee38c | 753 | dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size); |
96d8eab5 PO |
754 | |
755 | return 0; | |
756 | } | |
757 | ||
1f923a42 | 758 | static int vim2m_buf_prepare(struct vb2_buffer *vb) |
96d8eab5 | 759 | { |
2d700715 | 760 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
1f923a42 HV |
761 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
762 | struct vim2m_q_data *q_data; | |
96d8eab5 | 763 | |
d80ee38c | 764 | dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type); |
96d8eab5 | 765 | |
9f4161a6 | 766 | q_data = get_q_data(ctx, vb->vb2_queue->type); |
5c3112b5 | 767 | if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) { |
2d700715 JS |
768 | if (vbuf->field == V4L2_FIELD_ANY) |
769 | vbuf->field = V4L2_FIELD_NONE; | |
770 | if (vbuf->field != V4L2_FIELD_NONE) { | |
5c3112b5 HV |
771 | dprintk(ctx->dev, "%s field isn't supported\n", |
772 | __func__); | |
773 | return -EINVAL; | |
774 | } | |
775 | } | |
96d8eab5 | 776 | |
d80ee38c MS |
777 | if (vb2_plane_size(vb, 0) < q_data->sizeimage) { |
778 | dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n", | |
779 | __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage); | |
96d8eab5 PO |
780 | return -EINVAL; |
781 | } | |
782 | ||
d80ee38c | 783 | vb2_set_plane_payload(vb, 0, q_data->sizeimage); |
96d8eab5 PO |
784 | |
785 | return 0; | |
96d8eab5 PO |
786 | } |
787 | ||
1f923a42 | 788 | static void vim2m_buf_queue(struct vb2_buffer *vb) |
96d8eab5 | 789 | { |
2d700715 | 790 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
1f923a42 | 791 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
ca5f5fdb | 792 | |
2d700715 | 793 | v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); |
0f910bf0 MO |
794 | } |
795 | ||
1f923a42 | 796 | static int vim2m_start_streaming(struct vb2_queue *q, unsigned count) |
ca5f5fdb | 797 | { |
1f923a42 HV |
798 | struct vim2m_ctx *ctx = vb2_get_drv_priv(q); |
799 | struct vim2m_q_data *q_data = get_q_data(ctx, q->type); | |
ca5f5fdb HV |
800 | |
801 | q_data->sequence = 0; | |
802 | return 0; | |
803 | } | |
804 | ||
1f923a42 | 805 | static void vim2m_stop_streaming(struct vb2_queue *q) |
a773632c | 806 | { |
1f923a42 | 807 | struct vim2m_ctx *ctx = vb2_get_drv_priv(q); |
2d700715 | 808 | struct vb2_v4l2_buffer *vbuf; |
a773632c HV |
809 | unsigned long flags; |
810 | ||
144bd0ee | 811 | flush_scheduled_work(); |
a773632c HV |
812 | for (;;) { |
813 | if (V4L2_TYPE_IS_OUTPUT(q->type)) | |
2d700715 | 814 | vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); |
a773632c | 815 | else |
2d700715 JS |
816 | vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); |
817 | if (vbuf == NULL) | |
e37559b2 | 818 | return; |
86b93b2c HV |
819 | v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req, |
820 | &ctx->hdl); | |
a773632c | 821 | spin_lock_irqsave(&ctx->dev->irqlock, flags); |
2d700715 | 822 | v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); |
a773632c HV |
823 | spin_unlock_irqrestore(&ctx->dev->irqlock, flags); |
824 | } | |
a773632c HV |
825 | } |
826 | ||
86b93b2c HV |
827 | static void vim2m_buf_request_complete(struct vb2_buffer *vb) |
828 | { | |
829 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); | |
830 | ||
831 | v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl); | |
832 | } | |
833 | ||
b7b361f0 | 834 | static const struct vb2_ops vim2m_qops = { |
1f923a42 HV |
835 | .queue_setup = vim2m_queue_setup, |
836 | .buf_prepare = vim2m_buf_prepare, | |
837 | .buf_queue = vim2m_buf_queue, | |
838 | .start_streaming = vim2m_start_streaming, | |
839 | .stop_streaming = vim2m_stop_streaming, | |
20702ebc SN |
840 | .wait_prepare = vb2_ops_wait_prepare, |
841 | .wait_finish = vb2_ops_wait_finish, | |
86b93b2c | 842 | .buf_request_complete = vim2m_buf_request_complete, |
96d8eab5 PO |
843 | }; |
844 | ||
d80ee38c | 845 | static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq) |
96d8eab5 | 846 | { |
1f923a42 | 847 | struct vim2m_ctx *ctx = priv; |
d80ee38c | 848 | int ret; |
96d8eab5 | 849 | |
d80ee38c | 850 | src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; |
782f36ca | 851 | src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; |
d80ee38c MS |
852 | src_vq->drv_priv = ctx; |
853 | src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); | |
1f923a42 | 854 | src_vq->ops = &vim2m_qops; |
d80ee38c | 855 | src_vq->mem_ops = &vb2_vmalloc_memops; |
ade48681 | 856 | src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
20702ebc | 857 | src_vq->lock = &ctx->dev->dev_mutex; |
e5079cf1 | 858 | src_vq->supports_requests = true; |
d80ee38c MS |
859 | |
860 | ret = vb2_queue_init(src_vq); | |
861 | if (ret) | |
862 | return ret; | |
96d8eab5 | 863 | |
d80ee38c | 864 | dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
782f36ca | 865 | dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; |
d80ee38c MS |
866 | dst_vq->drv_priv = ctx; |
867 | dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); | |
1f923a42 | 868 | dst_vq->ops = &vim2m_qops; |
d80ee38c | 869 | dst_vq->mem_ops = &vb2_vmalloc_memops; |
ade48681 | 870 | dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
20702ebc | 871 | dst_vq->lock = &ctx->dev->dev_mutex; |
d80ee38c MS |
872 | |
873 | return vb2_queue_init(dst_vq); | |
874 | } | |
96d8eab5 | 875 | |
1f923a42 HV |
876 | static const struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = { |
877 | .ops = &vim2m_ctrl_ops, | |
d3dd59c2 HV |
878 | .id = V4L2_CID_TRANS_TIME_MSEC, |
879 | .name = "Transaction Time (msec)", | |
880 | .type = V4L2_CTRL_TYPE_INTEGER, | |
43405831 | 881 | .def = MEM2MEM_DEF_TRANSTIME, |
d3dd59c2 HV |
882 | .min = 1, |
883 | .max = 10001, | |
43405831 | 884 | .step = 1, |
d3dd59c2 HV |
885 | }; |
886 | ||
1f923a42 HV |
887 | static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = { |
888 | .ops = &vim2m_ctrl_ops, | |
d3dd59c2 HV |
889 | .id = V4L2_CID_TRANS_NUM_BUFS, |
890 | .name = "Buffers Per Transaction", | |
891 | .type = V4L2_CTRL_TYPE_INTEGER, | |
892 | .def = 1, | |
893 | .min = 1, | |
894 | .max = MEM2MEM_DEF_NUM_BUFS, | |
895 | .step = 1, | |
896 | }; | |
897 | ||
96d8eab5 PO |
898 | /* |
899 | * File operations | |
900 | */ | |
1f923a42 | 901 | static int vim2m_open(struct file *file) |
96d8eab5 | 902 | { |
1f923a42 HV |
903 | struct vim2m_dev *dev = video_drvdata(file); |
904 | struct vim2m_ctx *ctx = NULL; | |
d3dd59c2 | 905 | struct v4l2_ctrl_handler *hdl; |
f4694039 | 906 | int rc = 0; |
96d8eab5 | 907 | |
f4694039 HV |
908 | if (mutex_lock_interruptible(&dev->dev_mutex)) |
909 | return -ERESTARTSYS; | |
56ed221f | 910 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
f4694039 HV |
911 | if (!ctx) { |
912 | rc = -ENOMEM; | |
913 | goto open_unlock; | |
914 | } | |
96d8eab5 | 915 | |
d3dd59c2 HV |
916 | v4l2_fh_init(&ctx->fh, video_devdata(file)); |
917 | file->private_data = &ctx->fh; | |
96d8eab5 | 918 | ctx->dev = dev; |
d3dd59c2 HV |
919 | hdl = &ctx->hdl; |
920 | v4l2_ctrl_handler_init(hdl, 4); | |
1f923a42 HV |
921 | v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0); |
922 | v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0); | |
923 | v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL); | |
924 | v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL); | |
d3dd59c2 | 925 | if (hdl->error) { |
a7bd7756 | 926 | rc = hdl->error; |
d3dd59c2 | 927 | v4l2_ctrl_handler_free(hdl); |
7c13a4d6 | 928 | kfree(ctx); |
a7bd7756 | 929 | goto open_unlock; |
d3dd59c2 HV |
930 | } |
931 | ctx->fh.ctrl_handler = hdl; | |
932 | v4l2_ctrl_handler_setup(hdl); | |
96d8eab5 | 933 | |
9f4161a6 | 934 | ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0]; |
47556ffa HV |
935 | ctx->q_data[V4L2_M2M_SRC].width = 640; |
936 | ctx->q_data[V4L2_M2M_SRC].height = 480; | |
937 | ctx->q_data[V4L2_M2M_SRC].sizeimage = | |
938 | ctx->q_data[V4L2_M2M_SRC].width * | |
939 | ctx->q_data[V4L2_M2M_SRC].height * | |
940 | (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3); | |
941 | ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC]; | |
942 | ctx->colorspace = V4L2_COLORSPACE_REC709; | |
9f4161a6 | 943 | |
20702ebc | 944 | ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init); |
d80ee38c | 945 | |
20702ebc SN |
946 | if (IS_ERR(ctx->fh.m2m_ctx)) { |
947 | rc = PTR_ERR(ctx->fh.m2m_ctx); | |
16ee9bb1 | 948 | |
d3dd59c2 | 949 | v4l2_ctrl_handler_free(hdl); |
7c13a4d6 | 950 | v4l2_fh_exit(&ctx->fh); |
96d8eab5 | 951 | kfree(ctx); |
f4694039 | 952 | goto open_unlock; |
96d8eab5 PO |
953 | } |
954 | ||
d3dd59c2 | 955 | v4l2_fh_add(&ctx->fh); |
96d8eab5 PO |
956 | atomic_inc(&dev->num_inst); |
957 | ||
20702ebc SN |
958 | dprintk(dev, "Created instance: %p, m2m_ctx: %p\n", |
959 | ctx, ctx->fh.m2m_ctx); | |
96d8eab5 | 960 | |
f4694039 HV |
961 | open_unlock: |
962 | mutex_unlock(&dev->dev_mutex); | |
a7bd7756 | 963 | return rc; |
96d8eab5 PO |
964 | } |
965 | ||
1f923a42 | 966 | static int vim2m_release(struct file *file) |
96d8eab5 | 967 | { |
1f923a42 HV |
968 | struct vim2m_dev *dev = video_drvdata(file); |
969 | struct vim2m_ctx *ctx = file2ctx(file); | |
96d8eab5 PO |
970 | |
971 | dprintk(dev, "Releasing instance %p\n", ctx); | |
972 | ||
d3dd59c2 HV |
973 | v4l2_fh_del(&ctx->fh); |
974 | v4l2_fh_exit(&ctx->fh); | |
975 | v4l2_ctrl_handler_free(&ctx->hdl); | |
f4694039 | 976 | mutex_lock(&dev->dev_mutex); |
20702ebc | 977 | v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); |
f4694039 | 978 | mutex_unlock(&dev->dev_mutex); |
96d8eab5 PO |
979 | kfree(ctx); |
980 | ||
981 | atomic_dec(&dev->num_inst); | |
982 | ||
983 | return 0; | |
984 | } | |
985 | ||
1f923a42 | 986 | static const struct v4l2_file_operations vim2m_fops = { |
96d8eab5 | 987 | .owner = THIS_MODULE, |
1f923a42 HV |
988 | .open = vim2m_open, |
989 | .release = vim2m_release, | |
20702ebc | 990 | .poll = v4l2_m2m_fop_poll, |
07e80305 | 991 | .unlocked_ioctl = video_ioctl2, |
20702ebc | 992 | .mmap = v4l2_m2m_fop_mmap, |
96d8eab5 PO |
993 | }; |
994 | ||
5303135c | 995 | static const struct video_device vim2m_videodev = { |
96d8eab5 | 996 | .name = MEM2MEM_NAME, |
954f340f | 997 | .vfl_dir = VFL_DIR_M2M, |
1f923a42 HV |
998 | .fops = &vim2m_fops, |
999 | .ioctl_ops = &vim2m_ioctl_ops, | |
96d8eab5 | 1000 | .minor = -1, |
09365420 | 1001 | .release = video_device_release_empty, |
96d8eab5 PO |
1002 | }; |
1003 | ||
86ab0b1c | 1004 | static const struct v4l2_m2m_ops m2m_ops = { |
96d8eab5 PO |
1005 | .device_run = device_run, |
1006 | .job_ready = job_ready, | |
1007 | .job_abort = job_abort, | |
1008 | }; | |
1009 | ||
86b93b2c HV |
1010 | static const struct media_device_ops m2m_media_ops = { |
1011 | .req_validate = vb2_request_validate, | |
ef86eaf9 | 1012 | .req_queue = v4l2_m2m_request_queue, |
86b93b2c HV |
1013 | }; |
1014 | ||
1f923a42 | 1015 | static int vim2m_probe(struct platform_device *pdev) |
96d8eab5 | 1016 | { |
1f923a42 | 1017 | struct vim2m_dev *dev; |
96d8eab5 PO |
1018 | struct video_device *vfd; |
1019 | int ret; | |
1020 | ||
38a7996c | 1021 | dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); |
96d8eab5 PO |
1022 | if (!dev) |
1023 | return -ENOMEM; | |
1024 | ||
1025 | spin_lock_init(&dev->irqlock); | |
1026 | ||
1027 | ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); | |
1028 | if (ret) | |
38a7996c | 1029 | return ret; |
96d8eab5 PO |
1030 | |
1031 | atomic_set(&dev->num_inst, 0); | |
1032 | mutex_init(&dev->dev_mutex); | |
1033 | ||
09365420 HV |
1034 | dev->vfd = vim2m_videodev; |
1035 | vfd = &dev->vfd; | |
07e80305 | 1036 | vfd->lock = &dev->dev_mutex; |
8f484d87 | 1037 | vfd->v4l2_dev = &dev->v4l2_dev; |
144bd0ee | 1038 | INIT_DELAYED_WORK(&dev->work_run, device_work); |
96d8eab5 PO |
1039 | |
1040 | ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0); | |
1041 | if (ret) { | |
1042 | v4l2_err(&dev->v4l2_dev, "Failed to register video device\n"); | |
e35f7021 | 1043 | goto unreg_v4l2; |
96d8eab5 PO |
1044 | } |
1045 | ||
1046 | video_set_drvdata(vfd, dev); | |
8f484d87 | 1047 | v4l2_info(&dev->v4l2_dev, |
96d8eab5 PO |
1048 | "Device registered as /dev/video%d\n", vfd->num); |
1049 | ||
96d8eab5 PO |
1050 | platform_set_drvdata(pdev, dev); |
1051 | ||
1052 | dev->m2m_dev = v4l2_m2m_init(&m2m_ops); | |
1053 | if (IS_ERR(dev->m2m_dev)) { | |
1054 | v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n"); | |
1055 | ret = PTR_ERR(dev->m2m_dev); | |
e35f7021 HV |
1056 | goto unreg_dev; |
1057 | } | |
1058 | ||
1059 | #ifdef CONFIG_MEDIA_CONTROLLER | |
1060 | dev->mdev.dev = &pdev->dev; | |
c0decac1 | 1061 | strscpy(dev->mdev.model, "vim2m", sizeof(dev->mdev.model)); |
e35f7021 | 1062 | media_device_init(&dev->mdev); |
86b93b2c | 1063 | dev->mdev.ops = &m2m_media_ops; |
e35f7021 HV |
1064 | dev->v4l2_dev.mdev = &dev->mdev; |
1065 | ||
1066 | ret = v4l2_m2m_register_media_controller(dev->m2m_dev, | |
1067 | vfd, MEDIA_ENT_F_PROC_VIDEO_SCALER); | |
1068 | if (ret) { | |
1069 | v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n"); | |
1070 | goto unreg_m2m; | |
96d8eab5 PO |
1071 | } |
1072 | ||
e35f7021 HV |
1073 | ret = media_device_register(&dev->mdev); |
1074 | if (ret) { | |
1075 | v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n"); | |
1076 | goto unreg_m2m_mc; | |
1077 | } | |
1078 | #endif | |
96d8eab5 PO |
1079 | return 0; |
1080 | ||
e35f7021 HV |
1081 | #ifdef CONFIG_MEDIA_CONTROLLER |
1082 | unreg_m2m_mc: | |
1083 | v4l2_m2m_unregister_media_controller(dev->m2m_dev); | |
1084 | unreg_m2m: | |
b7e13ef7 | 1085 | v4l2_m2m_release(dev->m2m_dev); |
e35f7021 | 1086 | #endif |
96d8eab5 | 1087 | unreg_dev: |
e35f7021 HV |
1088 | video_unregister_device(&dev->vfd); |
1089 | unreg_v4l2: | |
96d8eab5 | 1090 | v4l2_device_unregister(&dev->v4l2_dev); |
96d8eab5 PO |
1091 | |
1092 | return ret; | |
1093 | } | |
1094 | ||
1f923a42 | 1095 | static int vim2m_remove(struct platform_device *pdev) |
96d8eab5 | 1096 | { |
1f923a42 | 1097 | struct vim2m_dev *dev = platform_get_drvdata(pdev); |
96d8eab5 | 1098 | |
1f923a42 | 1099 | v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME); |
e35f7021 HV |
1100 | |
1101 | #ifdef CONFIG_MEDIA_CONTROLLER | |
1102 | media_device_unregister(&dev->mdev); | |
1103 | v4l2_m2m_unregister_media_controller(dev->m2m_dev); | |
1104 | media_device_cleanup(&dev->mdev); | |
1105 | #endif | |
96d8eab5 | 1106 | v4l2_m2m_release(dev->m2m_dev); |
09365420 | 1107 | video_unregister_device(&dev->vfd); |
96d8eab5 | 1108 | v4l2_device_unregister(&dev->v4l2_dev); |
96d8eab5 PO |
1109 | |
1110 | return 0; | |
1111 | } | |
1112 | ||
1f923a42 HV |
1113 | static struct platform_driver vim2m_pdrv = { |
1114 | .probe = vim2m_probe, | |
1115 | .remove = vim2m_remove, | |
96d8eab5 PO |
1116 | .driver = { |
1117 | .name = MEM2MEM_NAME, | |
96d8eab5 PO |
1118 | }, |
1119 | }; | |
1120 | ||
1f923a42 | 1121 | static void __exit vim2m_exit(void) |
96d8eab5 | 1122 | { |
1f923a42 HV |
1123 | platform_driver_unregister(&vim2m_pdrv); |
1124 | platform_device_unregister(&vim2m_pdev); | |
96d8eab5 PO |
1125 | } |
1126 | ||
1f923a42 | 1127 | static int __init vim2m_init(void) |
96d8eab5 PO |
1128 | { |
1129 | int ret; | |
1130 | ||
1f923a42 | 1131 | ret = platform_device_register(&vim2m_pdev); |
96d8eab5 PO |
1132 | if (ret) |
1133 | return ret; | |
1134 | ||
1f923a42 | 1135 | ret = platform_driver_register(&vim2m_pdrv); |
96d8eab5 | 1136 | if (ret) |
1f923a42 | 1137 | platform_device_unregister(&vim2m_pdev); |
96d8eab5 | 1138 | |
b20b51f0 | 1139 | return ret; |
96d8eab5 PO |
1140 | } |
1141 | ||
1f923a42 HV |
1142 | module_init(vim2m_init); |
1143 | module_exit(vim2m_exit); |