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