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