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