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