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