]>
Commit | Line | Data |
---|---|---|
1e6dd65e MCC |
1 | /* |
2 | * Virtual Video driver - This code emulates a real video device with v4l2 api | |
3 | * | |
4 | * Copyright (c) 2006 by: | |
5 | * Mauro Carvalho Chehab <mchehab--a.t--infradead.org> | |
6 | * Ted Walther <ted--a.t--enumera.com> | |
7 | * John Sokol <sokol--a.t--videotechnology.com> | |
8 | * http://v4l.videotechnology.com/ | |
9 | * | |
e007a325 PO |
10 | * Conversion to videobuf2 by Pawel Osciak & Marek Szyprowski |
11 | * Copyright (c) 2010 Samsung Electronics | |
12 | * | |
1e6dd65e MCC |
13 | * This program is free software; you can redistribute it and/or modify |
14 | * it under the terms of the BSD Licence, GNU General Public License | |
15 | * as published by the Free Software Foundation; either version 2 of the | |
16 | * License, or (at your option) any later version | |
17 | */ | |
18 | #include <linux/module.h> | |
1e6dd65e | 19 | #include <linux/errno.h> |
1e6dd65e | 20 | #include <linux/kernel.h> |
1e6dd65e MCC |
21 | #include <linux/init.h> |
22 | #include <linux/sched.h> | |
6b46c397 | 23 | #include <linux/slab.h> |
730947bc | 24 | #include <linux/font.h> |
51b54029 | 25 | #include <linux/mutex.h> |
1e6dd65e | 26 | #include <linux/videodev2.h> |
1e6dd65e | 27 | #include <linux/kthread.h> |
7dfb7103 | 28 | #include <linux/freezer.h> |
e007a325 | 29 | #include <media/videobuf2-vmalloc.h> |
5ab6c9af HV |
30 | #include <media/v4l2-device.h> |
31 | #include <media/v4l2-ioctl.h> | |
7e996afa | 32 | #include <media/v4l2-ctrls.h> |
2e4784d0 | 33 | #include <media/v4l2-fh.h> |
730947bc | 34 | #include <media/v4l2-common.h> |
1e6dd65e | 35 | |
584ce48d | 36 | #define VIVI_MODULE_NAME "vivi" |
745271ae | 37 | |
1e6dd65e MCC |
38 | /* Wake up at about 30 fps */ |
39 | #define WAKE_NUMERATOR 30 | |
40 | #define WAKE_DENOMINATOR 1001 | |
41 | #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */ | |
42 | ||
730947bc HV |
43 | #define MAX_WIDTH 1920 |
44 | #define MAX_HEIGHT 1200 | |
45 | ||
1990d50b | 46 | #define VIVI_VERSION "0.8.1" |
1e6dd65e | 47 | |
5ab6c9af HV |
48 | MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board"); |
49 | MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol"); | |
50 | MODULE_LICENSE("Dual BSD/GPL"); | |
1990d50b | 51 | MODULE_VERSION(VIVI_VERSION); |
5ab6c9af HV |
52 | |
53 | static unsigned video_nr = -1; | |
54 | module_param(video_nr, uint, 0644); | |
55 | MODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect"); | |
56 | ||
57 | static unsigned n_devs = 1; | |
58 | module_param(n_devs, uint, 0644); | |
59 | MODULE_PARM_DESC(n_devs, "number of video devices to create"); | |
60 | ||
61 | static unsigned debug; | |
62 | module_param(debug, uint, 0644); | |
63 | MODULE_PARM_DESC(debug, "activates debug info"); | |
64 | ||
65 | static unsigned int vid_limit = 16; | |
66 | module_param(vid_limit, uint, 0644); | |
67 | MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes"); | |
68 | ||
730947bc HV |
69 | /* Global font descriptor */ |
70 | static const u8 *font8x16; | |
1e6dd65e | 71 | |
5ab6c9af HV |
72 | #define dprintk(dev, level, fmt, arg...) \ |
73 | v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg) | |
1e6dd65e MCC |
74 | |
75 | /* ------------------------------------------------------------------ | |
76 | Basic structures | |
77 | ------------------------------------------------------------------*/ | |
78 | ||
79 | struct vivi_fmt { | |
80 | char *name; | |
81 | u32 fourcc; /* v4l2 format id */ | |
82 | int depth; | |
83 | }; | |
84 | ||
d891f475 MD |
85 | static struct vivi_fmt formats[] = { |
86 | { | |
87 | .name = "4:2:2, packed, YUYV", | |
88 | .fourcc = V4L2_PIX_FMT_YUYV, | |
89 | .depth = 16, | |
90 | }, | |
fca36bab MD |
91 | { |
92 | .name = "4:2:2, packed, UYVY", | |
93 | .fourcc = V4L2_PIX_FMT_UYVY, | |
94 | .depth = 16, | |
95 | }, | |
aeadb5d4 MD |
96 | { |
97 | .name = "RGB565 (LE)", | |
98 | .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */ | |
99 | .depth = 16, | |
100 | }, | |
101 | { | |
102 | .name = "RGB565 (BE)", | |
103 | .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */ | |
104 | .depth = 16, | |
105 | }, | |
def52393 MD |
106 | { |
107 | .name = "RGB555 (LE)", | |
108 | .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */ | |
109 | .depth = 16, | |
110 | }, | |
111 | { | |
112 | .name = "RGB555 (BE)", | |
113 | .fourcc = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */ | |
114 | .depth = 16, | |
115 | }, | |
1e6dd65e MCC |
116 | }; |
117 | ||
d891f475 MD |
118 | static struct vivi_fmt *get_format(struct v4l2_format *f) |
119 | { | |
120 | struct vivi_fmt *fmt; | |
121 | unsigned int k; | |
122 | ||
123 | for (k = 0; k < ARRAY_SIZE(formats); k++) { | |
124 | fmt = &formats[k]; | |
125 | if (fmt->fourcc == f->fmt.pix.pixelformat) | |
126 | break; | |
127 | } | |
128 | ||
129 | if (k == ARRAY_SIZE(formats)) | |
130 | return NULL; | |
131 | ||
132 | return &formats[k]; | |
133 | } | |
134 | ||
1e6dd65e MCC |
135 | /* buffer for one video frame */ |
136 | struct vivi_buffer { | |
137 | /* common v4l buffer stuff -- must be first */ | |
e007a325 PO |
138 | struct vb2_buffer vb; |
139 | struct list_head list; | |
1e6dd65e | 140 | struct vivi_fmt *fmt; |
1e6dd65e MCC |
141 | }; |
142 | ||
143 | struct vivi_dmaqueue { | |
144 | struct list_head active; | |
1e6dd65e MCC |
145 | |
146 | /* thread for generating video stream*/ | |
147 | struct task_struct *kthread; | |
148 | wait_queue_head_t wq; | |
149 | /* Counters to control fps rate */ | |
150 | int frame; | |
151 | int ini_jiffies; | |
152 | }; | |
153 | ||
154 | static LIST_HEAD(vivi_devlist); | |
155 | ||
156 | struct vivi_dev { | |
157 | struct list_head vivi_devlist; | |
5ab6c9af | 158 | struct v4l2_device v4l2_dev; |
7e996afa | 159 | struct v4l2_ctrl_handler ctrl_handler; |
1e6dd65e | 160 | |
730947bc | 161 | /* controls */ |
7e996afa HV |
162 | struct v4l2_ctrl *brightness; |
163 | struct v4l2_ctrl *contrast; | |
164 | struct v4l2_ctrl *saturation; | |
165 | struct v4l2_ctrl *hue; | |
166 | struct v4l2_ctrl *volume; | |
167 | struct v4l2_ctrl *button; | |
168 | struct v4l2_ctrl *boolean; | |
169 | struct v4l2_ctrl *int32; | |
170 | struct v4l2_ctrl *int64; | |
171 | struct v4l2_ctrl *menu; | |
172 | struct v4l2_ctrl *string; | |
730947bc | 173 | |
55862ac9 | 174 | spinlock_t slock; |
aa9dbac4 | 175 | struct mutex mutex; |
1e6dd65e | 176 | |
1e6dd65e | 177 | /* various device info */ |
f905c442 | 178 | struct video_device *vfd; |
1e6dd65e MCC |
179 | |
180 | struct vivi_dmaqueue vidq; | |
181 | ||
182 | /* Several counters */ | |
730947bc | 183 | unsigned ms; |
dfd8c04e | 184 | unsigned long jiffies; |
7e996afa | 185 | unsigned button_pressed; |
025341d4 MCC |
186 | |
187 | int mv_count; /* Controls bars movement */ | |
e164b58a MCC |
188 | |
189 | /* Input Number */ | |
190 | int input; | |
c41ee24b | 191 | |
1e6dd65e MCC |
192 | /* video capture */ |
193 | struct vivi_fmt *fmt; | |
543323bc | 194 | unsigned int width, height; |
e007a325 PO |
195 | struct vb2_queue vb_vidq; |
196 | enum v4l2_field field; | |
197 | unsigned int field_count; | |
1e6dd65e | 198 | |
730947bc HV |
199 | u8 bars[9][3]; |
200 | u8 line[MAX_WIDTH * 4]; | |
1e6dd65e MCC |
201 | }; |
202 | ||
203 | /* ------------------------------------------------------------------ | |
204 | DMA and thread functions | |
205 | ------------------------------------------------------------------*/ | |
206 | ||
207 | /* Bars and Colors should match positions */ | |
208 | ||
209 | enum colors { | |
210 | WHITE, | |
730947bc | 211 | AMBER, |
1e6dd65e MCC |
212 | CYAN, |
213 | GREEN, | |
214 | MAGENTA, | |
215 | RED, | |
543323bc MCC |
216 | BLUE, |
217 | BLACK, | |
730947bc | 218 | TEXT_BLACK, |
1e6dd65e MCC |
219 | }; |
220 | ||
730947bc | 221 | /* R G B */ |
e164b58a | 222 | #define COLOR_WHITE {204, 204, 204} |
730947bc HV |
223 | #define COLOR_AMBER {208, 208, 0} |
224 | #define COLOR_CYAN { 0, 206, 206} | |
e164b58a MCC |
225 | #define COLOR_GREEN { 0, 239, 0} |
226 | #define COLOR_MAGENTA {239, 0, 239} | |
227 | #define COLOR_RED {205, 0, 0} | |
228 | #define COLOR_BLUE { 0, 0, 255} | |
229 | #define COLOR_BLACK { 0, 0, 0} | |
230 | ||
231 | struct bar_std { | |
730947bc | 232 | u8 bar[9][3]; |
e164b58a MCC |
233 | }; |
234 | ||
235 | /* Maximum number of bars are 10 - otherwise, the input print code | |
236 | should be modified */ | |
237 | static struct bar_std bars[] = { | |
238 | { /* Standard ITU-R color bar sequence */ | |
730947bc HV |
239 | { COLOR_WHITE, COLOR_AMBER, COLOR_CYAN, COLOR_GREEN, |
240 | COLOR_MAGENTA, COLOR_RED, COLOR_BLUE, COLOR_BLACK, COLOR_BLACK } | |
e164b58a | 241 | }, { |
730947bc HV |
242 | { COLOR_WHITE, COLOR_AMBER, COLOR_BLACK, COLOR_WHITE, |
243 | COLOR_AMBER, COLOR_BLACK, COLOR_WHITE, COLOR_AMBER, COLOR_BLACK } | |
e164b58a | 244 | }, { |
730947bc HV |
245 | { COLOR_WHITE, COLOR_CYAN, COLOR_BLACK, COLOR_WHITE, |
246 | COLOR_CYAN, COLOR_BLACK, COLOR_WHITE, COLOR_CYAN, COLOR_BLACK } | |
e164b58a | 247 | }, { |
730947bc HV |
248 | { COLOR_WHITE, COLOR_GREEN, COLOR_BLACK, COLOR_WHITE, |
249 | COLOR_GREEN, COLOR_BLACK, COLOR_WHITE, COLOR_GREEN, COLOR_BLACK } | |
e164b58a | 250 | }, |
1e6dd65e MCC |
251 | }; |
252 | ||
e164b58a MCC |
253 | #define NUM_INPUTS ARRAY_SIZE(bars) |
254 | ||
543323bc MCC |
255 | #define TO_Y(r, g, b) \ |
256 | (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16) | |
1e6dd65e | 257 | /* RGB to V(Cr) Color transform */ |
543323bc MCC |
258 | #define TO_V(r, g, b) \ |
259 | (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128) | |
1e6dd65e | 260 | /* RGB to U(Cb) Color transform */ |
543323bc MCC |
261 | #define TO_U(r, g, b) \ |
262 | (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128) | |
1e6dd65e | 263 | |
c285addb | 264 | /* precalculate color bar values to speed up rendering */ |
730947bc | 265 | static void precalculate_bars(struct vivi_dev *dev) |
c285addb | 266 | { |
730947bc | 267 | u8 r, g, b; |
c285addb MCC |
268 | int k, is_yuv; |
269 | ||
730947bc HV |
270 | for (k = 0; k < 9; k++) { |
271 | r = bars[dev->input].bar[k][0]; | |
272 | g = bars[dev->input].bar[k][1]; | |
273 | b = bars[dev->input].bar[k][2]; | |
c285addb MCC |
274 | is_yuv = 0; |
275 | ||
730947bc | 276 | switch (dev->fmt->fourcc) { |
c285addb MCC |
277 | case V4L2_PIX_FMT_YUYV: |
278 | case V4L2_PIX_FMT_UYVY: | |
279 | is_yuv = 1; | |
280 | break; | |
281 | case V4L2_PIX_FMT_RGB565: | |
282 | case V4L2_PIX_FMT_RGB565X: | |
283 | r >>= 3; | |
284 | g >>= 2; | |
285 | b >>= 3; | |
286 | break; | |
287 | case V4L2_PIX_FMT_RGB555: | |
288 | case V4L2_PIX_FMT_RGB555X: | |
289 | r >>= 3; | |
290 | g >>= 3; | |
291 | b >>= 3; | |
292 | break; | |
293 | } | |
294 | ||
295 | if (is_yuv) { | |
730947bc HV |
296 | dev->bars[k][0] = TO_Y(r, g, b); /* Luma */ |
297 | dev->bars[k][1] = TO_U(r, g, b); /* Cb */ | |
298 | dev->bars[k][2] = TO_V(r, g, b); /* Cr */ | |
c285addb | 299 | } else { |
730947bc HV |
300 | dev->bars[k][0] = r; |
301 | dev->bars[k][1] = g; | |
302 | dev->bars[k][2] = b; | |
c285addb MCC |
303 | } |
304 | } | |
c285addb MCC |
305 | } |
306 | ||
e164b58a MCC |
307 | #define TSTAMP_MIN_Y 24 |
308 | #define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15) | |
309 | #define TSTAMP_INPUT_X 10 | |
310 | #define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X) | |
1e6dd65e | 311 | |
730947bc | 312 | static void gen_twopix(struct vivi_dev *dev, u8 *buf, int colorpos) |
74d7c5af | 313 | { |
730947bc | 314 | u8 r_y, g_u, b_v; |
74d7c5af | 315 | int color; |
730947bc | 316 | u8 *p; |
74d7c5af | 317 | |
730947bc HV |
318 | r_y = dev->bars[colorpos][0]; /* R or precalculated Y */ |
319 | g_u = dev->bars[colorpos][1]; /* G or precalculated U */ | |
320 | b_v = dev->bars[colorpos][2]; /* B or precalculated V */ | |
74d7c5af MD |
321 | |
322 | for (color = 0; color < 4; color++) { | |
323 | p = buf + color; | |
324 | ||
730947bc | 325 | switch (dev->fmt->fourcc) { |
d891f475 MD |
326 | case V4L2_PIX_FMT_YUYV: |
327 | switch (color) { | |
328 | case 0: | |
329 | case 2: | |
330 | *p = r_y; | |
331 | break; | |
332 | case 1: | |
333 | *p = g_u; | |
334 | break; | |
335 | case 3: | |
336 | *p = b_v; | |
337 | break; | |
338 | } | |
74d7c5af | 339 | break; |
fca36bab MD |
340 | case V4L2_PIX_FMT_UYVY: |
341 | switch (color) { | |
342 | case 1: | |
343 | case 3: | |
344 | *p = r_y; | |
345 | break; | |
346 | case 0: | |
347 | *p = g_u; | |
348 | break; | |
349 | case 2: | |
350 | *p = b_v; | |
351 | break; | |
352 | } | |
353 | break; | |
aeadb5d4 MD |
354 | case V4L2_PIX_FMT_RGB565: |
355 | switch (color) { | |
356 | case 0: | |
357 | case 2: | |
358 | *p = (g_u << 5) | b_v; | |
359 | break; | |
360 | case 1: | |
361 | case 3: | |
362 | *p = (r_y << 3) | (g_u >> 3); | |
363 | break; | |
364 | } | |
365 | break; | |
366 | case V4L2_PIX_FMT_RGB565X: | |
367 | switch (color) { | |
368 | case 0: | |
369 | case 2: | |
370 | *p = (r_y << 3) | (g_u >> 3); | |
371 | break; | |
372 | case 1: | |
373 | case 3: | |
374 | *p = (g_u << 5) | b_v; | |
375 | break; | |
376 | } | |
377 | break; | |
def52393 MD |
378 | case V4L2_PIX_FMT_RGB555: |
379 | switch (color) { | |
380 | case 0: | |
381 | case 2: | |
382 | *p = (g_u << 5) | b_v; | |
383 | break; | |
384 | case 1: | |
385 | case 3: | |
386 | *p = (r_y << 2) | (g_u >> 3); | |
387 | break; | |
388 | } | |
389 | break; | |
390 | case V4L2_PIX_FMT_RGB555X: | |
391 | switch (color) { | |
392 | case 0: | |
393 | case 2: | |
394 | *p = (r_y << 2) | (g_u >> 3); | |
395 | break; | |
396 | case 1: | |
397 | case 3: | |
398 | *p = (g_u << 5) | b_v; | |
399 | break; | |
400 | } | |
401 | break; | |
74d7c5af MD |
402 | } |
403 | } | |
404 | } | |
405 | ||
730947bc | 406 | static void precalculate_line(struct vivi_dev *dev) |
1e6dd65e | 407 | { |
730947bc | 408 | int w; |
1e6dd65e | 409 | |
730947bc HV |
410 | for (w = 0; w < dev->width * 2; w += 2) { |
411 | int colorpos = (w / (dev->width / 8) % 8); | |
74d7c5af | 412 | |
730947bc | 413 | gen_twopix(dev, dev->line + w * 2, colorpos); |
1e6dd65e | 414 | } |
730947bc | 415 | } |
1e6dd65e | 416 | |
730947bc HV |
417 | static void gen_text(struct vivi_dev *dev, char *basep, |
418 | int y, int x, char *text) | |
419 | { | |
420 | int line; | |
e164b58a | 421 | |
730947bc HV |
422 | /* Checks if it is possible to show string */ |
423 | if (y + 16 >= dev->height || x + strlen(text) * 8 >= dev->width) | |
424 | return; | |
1e6dd65e MCC |
425 | |
426 | /* Print stream time */ | |
730947bc HV |
427 | for (line = y; line < y + 16; line++) { |
428 | int j = 0; | |
429 | char *pos = basep + line * dev->width * 2 + x * 2; | |
430 | char *s; | |
431 | ||
432 | for (s = text; *s; s++) { | |
433 | u8 chr = font8x16[*s * 16 + line - y]; | |
434 | int i; | |
435 | ||
436 | for (i = 0; i < 7; i++, j++) { | |
74d7c5af | 437 | /* Draw white font on black background */ |
730947bc HV |
438 | if (chr & (1 << (7 - i))) |
439 | gen_twopix(dev, pos + j * 2, WHITE); | |
74d7c5af | 440 | else |
730947bc | 441 | gen_twopix(dev, pos + j * 2, TEXT_BLACK); |
1e6dd65e MCC |
442 | } |
443 | } | |
444 | } | |
1e6dd65e | 445 | } |
78718e5d | 446 | |
730947bc | 447 | static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf) |
1e6dd65e | 448 | { |
e007a325 PO |
449 | int wmax = dev->width; |
450 | int hmax = dev->height; | |
1e6dd65e | 451 | struct timeval ts; |
e007a325 | 452 | void *vbuf = vb2_plane_vaddr(&buf->vb, 0); |
730947bc HV |
453 | unsigned ms; |
454 | char str[100]; | |
455 | int h, line = 1; | |
b50e7fe9 | 456 | |
5c554e6b | 457 | if (!vbuf) |
5a037706 | 458 | return; |
1e6dd65e | 459 | |
730947bc HV |
460 | for (h = 0; h < hmax; h++) |
461 | memcpy(vbuf + h * wmax * 2, dev->line + (dev->mv_count % wmax) * 2, wmax * 2); | |
5a037706 | 462 | |
1e6dd65e MCC |
463 | /* Updates stream time */ |
464 | ||
730947bc | 465 | dev->ms += jiffies_to_msecs(jiffies - dev->jiffies); |
543323bc | 466 | dev->jiffies = jiffies; |
730947bc HV |
467 | ms = dev->ms; |
468 | snprintf(str, sizeof(str), " %02d:%02d:%02d:%03d ", | |
469 | (ms / (60 * 60 * 1000)) % 24, | |
470 | (ms / (60 * 1000)) % 60, | |
471 | (ms / 1000) % 60, | |
472 | ms % 1000); | |
473 | gen_text(dev, vbuf, line++ * 16, 16, str); | |
474 | snprintf(str, sizeof(str), " %dx%d, input %d ", | |
475 | dev->width, dev->height, dev->input); | |
476 | gen_text(dev, vbuf, line++ * 16, 16, str); | |
477 | ||
7e996afa | 478 | mutex_lock(&dev->ctrl_handler.lock); |
730947bc | 479 | snprintf(str, sizeof(str), " brightness %3d, contrast %3d, saturation %3d, hue %d ", |
7e996afa HV |
480 | dev->brightness->cur.val, |
481 | dev->contrast->cur.val, | |
482 | dev->saturation->cur.val, | |
483 | dev->hue->cur.val); | |
730947bc | 484 | gen_text(dev, vbuf, line++ * 16, 16, str); |
7e996afa | 485 | snprintf(str, sizeof(str), " volume %3d ", dev->volume->cur.val); |
730947bc | 486 | gen_text(dev, vbuf, line++ * 16, 16, str); |
7e996afa HV |
487 | snprintf(str, sizeof(str), " int32 %d, int64 %lld ", |
488 | dev->int32->cur.val, | |
489 | dev->int64->cur.val64); | |
490 | gen_text(dev, vbuf, line++ * 16, 16, str); | |
491 | snprintf(str, sizeof(str), " boolean %d, menu %s, string \"%s\" ", | |
492 | dev->boolean->cur.val, | |
493 | dev->menu->qmenu[dev->menu->cur.val], | |
494 | dev->string->cur.string); | |
495 | mutex_unlock(&dev->ctrl_handler.lock); | |
496 | gen_text(dev, vbuf, line++ * 16, 16, str); | |
497 | if (dev->button_pressed) { | |
498 | dev->button_pressed--; | |
499 | snprintf(str, sizeof(str), " button pressed!"); | |
500 | gen_text(dev, vbuf, line++ * 16, 16, str); | |
501 | } | |
730947bc HV |
502 | |
503 | dev->mv_count += 2; | |
1e6dd65e | 504 | |
e007a325 PO |
505 | buf->vb.v4l2_buf.field = dev->field; |
506 | dev->field_count++; | |
507 | buf->vb.v4l2_buf.sequence = dev->field_count >> 1; | |
1e6dd65e | 508 | do_gettimeofday(&ts); |
e007a325 | 509 | buf->vb.v4l2_buf.timestamp = ts; |
1e6dd65e MCC |
510 | } |
511 | ||
730947bc | 512 | static void vivi_thread_tick(struct vivi_dev *dev) |
1e6dd65e | 513 | { |
78718e5d | 514 | struct vivi_dmaqueue *dma_q = &dev->vidq; |
730947bc | 515 | struct vivi_buffer *buf; |
78718e5d | 516 | unsigned long flags = 0; |
1e6dd65e | 517 | |
78718e5d | 518 | dprintk(dev, 1, "Thread tick\n"); |
1e6dd65e | 519 | |
78718e5d BP |
520 | spin_lock_irqsave(&dev->slock, flags); |
521 | if (list_empty(&dma_q->active)) { | |
522 | dprintk(dev, 1, "No active queue to serve\n"); | |
523 | goto unlock; | |
524 | } | |
1e6dd65e | 525 | |
e007a325 PO |
526 | buf = list_entry(dma_q->active.next, struct vivi_buffer, list); |
527 | list_del(&buf->list); | |
1e6dd65e | 528 | |
e007a325 | 529 | do_gettimeofday(&buf->vb.v4l2_buf.timestamp); |
78718e5d BP |
530 | |
531 | /* Fill buffer */ | |
730947bc | 532 | vivi_fillbuff(dev, buf); |
78718e5d BP |
533 | dprintk(dev, 1, "filled buffer %p\n", buf); |
534 | ||
e007a325 PO |
535 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE); |
536 | dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index); | |
78718e5d BP |
537 | unlock: |
538 | spin_unlock_irqrestore(&dev->slock, flags); | |
1e6dd65e MCC |
539 | } |
540 | ||
6594ad82 MCC |
541 | #define frames_to_ms(frames) \ |
542 | ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR) | |
543 | ||
730947bc | 544 | static void vivi_sleep(struct vivi_dev *dev) |
1e6dd65e | 545 | { |
78718e5d BP |
546 | struct vivi_dmaqueue *dma_q = &dev->vidq; |
547 | int timeout; | |
1e6dd65e MCC |
548 | DECLARE_WAITQUEUE(wait, current); |
549 | ||
7e28adb2 | 550 | dprintk(dev, 1, "%s dma_q=0x%08lx\n", __func__, |
6c2f9901 | 551 | (unsigned long)dma_q); |
1e6dd65e MCC |
552 | |
553 | add_wait_queue(&dma_q->wq, &wait); | |
6594ad82 MCC |
554 | if (kthread_should_stop()) |
555 | goto stop_task; | |
556 | ||
6594ad82 | 557 | /* Calculate time to wake up */ |
78718e5d | 558 | timeout = msecs_to_jiffies(frames_to_ms(1)); |
6594ad82 | 559 | |
730947bc | 560 | vivi_thread_tick(dev); |
6594ad82 MCC |
561 | |
562 | schedule_timeout_interruptible(timeout); | |
1e6dd65e | 563 | |
6594ad82 | 564 | stop_task: |
1e6dd65e MCC |
565 | remove_wait_queue(&dma_q->wq, &wait); |
566 | try_to_freeze(); | |
567 | } | |
568 | ||
972c3517 | 569 | static int vivi_thread(void *data) |
1e6dd65e | 570 | { |
730947bc | 571 | struct vivi_dev *dev = data; |
1e6dd65e | 572 | |
6c2f9901 | 573 | dprintk(dev, 1, "thread started\n"); |
1e6dd65e | 574 | |
83144186 | 575 | set_freezable(); |
0b600512 | 576 | |
1e6dd65e | 577 | for (;;) { |
730947bc | 578 | vivi_sleep(dev); |
1e6dd65e MCC |
579 | |
580 | if (kthread_should_stop()) | |
581 | break; | |
582 | } | |
6c2f9901 | 583 | dprintk(dev, 1, "thread: exit\n"); |
1e6dd65e MCC |
584 | return 0; |
585 | } | |
586 | ||
e007a325 | 587 | static int vivi_start_generating(struct vivi_dev *dev) |
1e6dd65e | 588 | { |
78718e5d | 589 | struct vivi_dmaqueue *dma_q = &dev->vidq; |
6c2f9901 | 590 | |
7e28adb2 | 591 | dprintk(dev, 1, "%s\n", __func__); |
1e6dd65e | 592 | |
730947bc HV |
593 | /* Resets frame counters */ |
594 | dev->ms = 0; | |
595 | dev->mv_count = 0; | |
596 | dev->jiffies = jiffies; | |
597 | ||
598 | dma_q->frame = 0; | |
599 | dma_q->ini_jiffies = jiffies; | |
600 | dma_q->kthread = kthread_run(vivi_thread, dev, dev->v4l2_dev.name); | |
1e6dd65e | 601 | |
054afee4 | 602 | if (IS_ERR(dma_q->kthread)) { |
5ab6c9af | 603 | v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n"); |
e007a325 | 604 | return PTR_ERR(dma_q->kthread); |
1e6dd65e | 605 | } |
0b600512 MCC |
606 | /* Wakes thread */ |
607 | wake_up_interruptible(&dma_q->wq); | |
608 | ||
7e28adb2 | 609 | dprintk(dev, 1, "returning from %s\n", __func__); |
e007a325 | 610 | return 0; |
1e6dd65e MCC |
611 | } |
612 | ||
e007a325 | 613 | static void vivi_stop_generating(struct vivi_dev *dev) |
1e6dd65e | 614 | { |
730947bc | 615 | struct vivi_dmaqueue *dma_q = &dev->vidq; |
6c2f9901 | 616 | |
7e28adb2 | 617 | dprintk(dev, 1, "%s\n", __func__); |
730947bc | 618 | |
1e6dd65e MCC |
619 | /* shutdown control thread */ |
620 | if (dma_q->kthread) { | |
621 | kthread_stop(dma_q->kthread); | |
543323bc | 622 | dma_q->kthread = NULL; |
1e6dd65e | 623 | } |
730947bc | 624 | |
e007a325 PO |
625 | /* |
626 | * Typical driver might need to wait here until dma engine stops. | |
627 | * In this case we can abort imiedetly, so it's just a noop. | |
628 | */ | |
629 | ||
630 | /* Release all active buffers */ | |
631 | while (!list_empty(&dma_q->active)) { | |
632 | struct vivi_buffer *buf; | |
633 | buf = list_entry(dma_q->active.next, struct vivi_buffer, list); | |
634 | list_del(&buf->list); | |
635 | vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); | |
636 | dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index); | |
637 | } | |
1e6dd65e | 638 | } |
1e6dd65e MCC |
639 | /* ------------------------------------------------------------------ |
640 | Videobuf operations | |
641 | ------------------------------------------------------------------*/ | |
e007a325 PO |
642 | static int queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, |
643 | unsigned int *nplanes, unsigned long sizes[], | |
644 | void *alloc_ctxs[]) | |
1e6dd65e | 645 | { |
e007a325 PO |
646 | struct vivi_dev *dev = vb2_get_drv_priv(vq); |
647 | unsigned long size; | |
648 | ||
649 | size = dev->width * dev->height * 2; | |
1e6dd65e | 650 | |
e007a325 PO |
651 | if (0 == *nbuffers) |
652 | *nbuffers = 32; | |
1e6dd65e | 653 | |
e007a325 PO |
654 | while (size * *nbuffers > vid_limit * 1024 * 1024) |
655 | (*nbuffers)--; | |
6bb2790f | 656 | |
e007a325 | 657 | *nplanes = 1; |
6bb2790f | 658 | |
e007a325 PO |
659 | sizes[0] = size; |
660 | ||
661 | /* | |
662 | * videobuf2-vmalloc allocator is context-less so no need to set | |
663 | * alloc_ctxs array. | |
664 | */ | |
665 | ||
666 | dprintk(dev, 1, "%s, count=%d, size=%ld\n", __func__, | |
667 | *nbuffers, size); | |
6bb2790f | 668 | |
1e6dd65e MCC |
669 | return 0; |
670 | } | |
671 | ||
e007a325 | 672 | static int buffer_init(struct vb2_buffer *vb) |
1e6dd65e | 673 | { |
e007a325 | 674 | struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue); |
6c2f9901 | 675 | |
e007a325 | 676 | BUG_ON(NULL == dev->fmt); |
1e6dd65e | 677 | |
e007a325 PO |
678 | /* |
679 | * This callback is called once per buffer, after its allocation. | |
680 | * | |
681 | * Vivi does not allow changing format during streaming, but it is | |
682 | * possible to do so when streaming is paused (i.e. in streamoff state). | |
683 | * Buffers however are not freed when going into streamoff and so | |
684 | * buffer size verification has to be done in buffer_prepare, on each | |
685 | * qbuf. | |
686 | * It would be best to move verification code here to buf_init and | |
687 | * s_fmt though. | |
688 | */ | |
689 | ||
690 | return 0; | |
1e6dd65e MCC |
691 | } |
692 | ||
e007a325 | 693 | static int buffer_prepare(struct vb2_buffer *vb) |
1e6dd65e | 694 | { |
e007a325 | 695 | struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue); |
543323bc | 696 | struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb); |
e007a325 | 697 | unsigned long size; |
1e6dd65e | 698 | |
e007a325 | 699 | dprintk(dev, 1, "%s, field=%d\n", __func__, vb->v4l2_buf.field); |
1e6dd65e | 700 | |
730947bc | 701 | BUG_ON(NULL == dev->fmt); |
78718e5d | 702 | |
e007a325 PO |
703 | /* |
704 | * Theses properties only change when queue is idle, see s_fmt. | |
705 | * The below checks should not be performed here, on each | |
706 | * buffer_prepare (i.e. on each qbuf). Most of the code in this function | |
707 | * should thus be moved to buffer_init and s_fmt. | |
708 | */ | |
730947bc HV |
709 | if (dev->width < 48 || dev->width > MAX_WIDTH || |
710 | dev->height < 32 || dev->height > MAX_HEIGHT) | |
1e6dd65e | 711 | return -EINVAL; |
78718e5d | 712 | |
e007a325 PO |
713 | size = dev->width * dev->height * 2; |
714 | if (vb2_plane_size(vb, 0) < size) { | |
715 | dprintk(dev, 1, "%s data will not fit into plane (%lu < %lu)\n", | |
716 | __func__, vb2_plane_size(vb, 0), size); | |
1e6dd65e | 717 | return -EINVAL; |
e007a325 PO |
718 | } |
719 | ||
720 | vb2_set_plane_payload(&buf->vb, 0, size); | |
1e6dd65e | 721 | |
e007a325 | 722 | buf->fmt = dev->fmt; |
1e6dd65e | 723 | |
730947bc HV |
724 | precalculate_bars(dev); |
725 | precalculate_line(dev); | |
c285addb | 726 | |
e007a325 PO |
727 | return 0; |
728 | } | |
1e6dd65e | 729 | |
e007a325 PO |
730 | static int buffer_finish(struct vb2_buffer *vb) |
731 | { | |
732 | struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue); | |
733 | dprintk(dev, 1, "%s\n", __func__); | |
1e6dd65e | 734 | return 0; |
e007a325 PO |
735 | } |
736 | ||
737 | static void buffer_cleanup(struct vb2_buffer *vb) | |
738 | { | |
739 | struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue); | |
740 | dprintk(dev, 1, "%s\n", __func__); | |
1e6dd65e | 741 | |
1e6dd65e MCC |
742 | } |
743 | ||
e007a325 | 744 | static void buffer_queue(struct vb2_buffer *vb) |
1e6dd65e | 745 | { |
e007a325 | 746 | struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue); |
730947bc | 747 | struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb); |
78718e5d | 748 | struct vivi_dmaqueue *vidq = &dev->vidq; |
e007a325 | 749 | unsigned long flags = 0; |
78718e5d | 750 | |
7e28adb2 | 751 | dprintk(dev, 1, "%s\n", __func__); |
78718e5d | 752 | |
e007a325 PO |
753 | spin_lock_irqsave(&dev->slock, flags); |
754 | list_add_tail(&buf->list, &vidq->active); | |
755 | spin_unlock_irqrestore(&dev->slock, flags); | |
1e6dd65e MCC |
756 | } |
757 | ||
e007a325 | 758 | static int start_streaming(struct vb2_queue *vq) |
1e6dd65e | 759 | { |
e007a325 PO |
760 | struct vivi_dev *dev = vb2_get_drv_priv(vq); |
761 | dprintk(dev, 1, "%s\n", __func__); | |
762 | return vivi_start_generating(dev); | |
763 | } | |
1e6dd65e | 764 | |
e007a325 PO |
765 | /* abort streaming and wait for last buffer */ |
766 | static int stop_streaming(struct vb2_queue *vq) | |
767 | { | |
768 | struct vivi_dev *dev = vb2_get_drv_priv(vq); | |
7e28adb2 | 769 | dprintk(dev, 1, "%s\n", __func__); |
e007a325 PO |
770 | vivi_stop_generating(dev); |
771 | return 0; | |
772 | } | |
773 | ||
774 | static void vivi_lock(struct vb2_queue *vq) | |
775 | { | |
776 | struct vivi_dev *dev = vb2_get_drv_priv(vq); | |
777 | mutex_lock(&dev->mutex); | |
778 | } | |
1e6dd65e | 779 | |
e007a325 PO |
780 | static void vivi_unlock(struct vb2_queue *vq) |
781 | { | |
782 | struct vivi_dev *dev = vb2_get_drv_priv(vq); | |
783 | mutex_unlock(&dev->mutex); | |
1e6dd65e MCC |
784 | } |
785 | ||
e007a325 PO |
786 | |
787 | static struct vb2_ops vivi_video_qops = { | |
788 | .queue_setup = queue_setup, | |
789 | .buf_init = buffer_init, | |
790 | .buf_prepare = buffer_prepare, | |
791 | .buf_finish = buffer_finish, | |
792 | .buf_cleanup = buffer_cleanup, | |
793 | .buf_queue = buffer_queue, | |
794 | .start_streaming = start_streaming, | |
795 | .stop_streaming = stop_streaming, | |
796 | .wait_prepare = vivi_unlock, | |
797 | .wait_finish = vivi_lock, | |
1e6dd65e MCC |
798 | }; |
799 | ||
c820cc45 MCC |
800 | /* ------------------------------------------------------------------ |
801 | IOCTL vidioc handling | |
802 | ------------------------------------------------------------------*/ | |
543323bc | 803 | static int vidioc_querycap(struct file *file, void *priv, |
c820cc45 MCC |
804 | struct v4l2_capability *cap) |
805 | { | |
730947bc | 806 | struct vivi_dev *dev = video_drvdata(file); |
5ab6c9af | 807 | |
c820cc45 MCC |
808 | strcpy(cap->driver, "vivi"); |
809 | strcpy(cap->card, "vivi"); | |
5ab6c9af | 810 | strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info)); |
730947bc HV |
811 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | \ |
812 | V4L2_CAP_READWRITE; | |
c820cc45 MCC |
813 | return 0; |
814 | } | |
815 | ||
78b526a4 | 816 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
c820cc45 MCC |
817 | struct v4l2_fmtdesc *f) |
818 | { | |
d891f475 MD |
819 | struct vivi_fmt *fmt; |
820 | ||
821 | if (f->index >= ARRAY_SIZE(formats)) | |
c820cc45 MCC |
822 | return -EINVAL; |
823 | ||
d891f475 MD |
824 | fmt = &formats[f->index]; |
825 | ||
826 | strlcpy(f->description, fmt->name, sizeof(f->description)); | |
827 | f->pixelformat = fmt->fourcc; | |
c820cc45 MCC |
828 | return 0; |
829 | } | |
830 | ||
78b526a4 | 831 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, |
c820cc45 MCC |
832 | struct v4l2_format *f) |
833 | { | |
730947bc | 834 | struct vivi_dev *dev = video_drvdata(file); |
c820cc45 | 835 | |
730947bc HV |
836 | f->fmt.pix.width = dev->width; |
837 | f->fmt.pix.height = dev->height; | |
e007a325 | 838 | f->fmt.pix.field = dev->field; |
730947bc | 839 | f->fmt.pix.pixelformat = dev->fmt->fourcc; |
c820cc45 | 840 | f->fmt.pix.bytesperline = |
730947bc | 841 | (f->fmt.pix.width * dev->fmt->depth) >> 3; |
c820cc45 MCC |
842 | f->fmt.pix.sizeimage = |
843 | f->fmt.pix.height * f->fmt.pix.bytesperline; | |
730947bc | 844 | return 0; |
c820cc45 MCC |
845 | } |
846 | ||
78b526a4 | 847 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
1e6dd65e MCC |
848 | struct v4l2_format *f) |
849 | { | |
730947bc | 850 | struct vivi_dev *dev = video_drvdata(file); |
1e6dd65e MCC |
851 | struct vivi_fmt *fmt; |
852 | enum v4l2_field field; | |
1e6dd65e | 853 | |
d891f475 MD |
854 | fmt = get_format(f); |
855 | if (!fmt) { | |
856 | dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n", | |
857 | f->fmt.pix.pixelformat); | |
1e6dd65e MCC |
858 | return -EINVAL; |
859 | } | |
1e6dd65e MCC |
860 | |
861 | field = f->fmt.pix.field; | |
862 | ||
863 | if (field == V4L2_FIELD_ANY) { | |
543323bc | 864 | field = V4L2_FIELD_INTERLACED; |
1e6dd65e | 865 | } else if (V4L2_FIELD_INTERLACED != field) { |
6c2f9901 | 866 | dprintk(dev, 1, "Field type invalid.\n"); |
1e6dd65e MCC |
867 | return -EINVAL; |
868 | } | |
869 | ||
1e6dd65e | 870 | f->fmt.pix.field = field; |
730947bc HV |
871 | v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 2, |
872 | &f->fmt.pix.height, 32, MAX_HEIGHT, 0, 0); | |
1e6dd65e MCC |
873 | f->fmt.pix.bytesperline = |
874 | (f->fmt.pix.width * fmt->depth) >> 3; | |
875 | f->fmt.pix.sizeimage = | |
876 | f->fmt.pix.height * f->fmt.pix.bytesperline; | |
1e6dd65e MCC |
877 | return 0; |
878 | } | |
879 | ||
e164b58a MCC |
880 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
881 | struct v4l2_format *f) | |
882 | { | |
730947bc | 883 | struct vivi_dev *dev = video_drvdata(file); |
e007a325 | 884 | struct vb2_queue *q = &dev->vb_vidq; |
e164b58a | 885 | |
730947bc | 886 | int ret = vidioc_try_fmt_vid_cap(file, priv, f); |
e164b58a MCC |
887 | if (ret < 0) |
888 | return ret; | |
889 | ||
e007a325 | 890 | if (vb2_is_streaming(q)) { |
730947bc | 891 | dprintk(dev, 1, "%s device busy\n", __func__); |
e007a325 | 892 | return -EBUSY; |
e164b58a MCC |
893 | } |
894 | ||
730947bc HV |
895 | dev->fmt = get_format(f); |
896 | dev->width = f->fmt.pix.width; | |
897 | dev->height = f->fmt.pix.height; | |
e007a325 PO |
898 | dev->field = f->fmt.pix.field; |
899 | ||
900 | return 0; | |
1e6dd65e MCC |
901 | } |
902 | ||
543323bc MCC |
903 | static int vidioc_reqbufs(struct file *file, void *priv, |
904 | struct v4l2_requestbuffers *p) | |
1e6dd65e | 905 | { |
730947bc | 906 | struct vivi_dev *dev = video_drvdata(file); |
e007a325 | 907 | return vb2_reqbufs(&dev->vb_vidq, p); |
1e6dd65e MCC |
908 | } |
909 | ||
543323bc | 910 | static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p) |
1e6dd65e | 911 | { |
730947bc | 912 | struct vivi_dev *dev = video_drvdata(file); |
e007a325 | 913 | return vb2_querybuf(&dev->vb_vidq, p); |
c820cc45 | 914 | } |
1e6dd65e | 915 | |
543323bc | 916 | static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p) |
c820cc45 | 917 | { |
730947bc | 918 | struct vivi_dev *dev = video_drvdata(file); |
e007a325 | 919 | return vb2_qbuf(&dev->vb_vidq, p); |
c820cc45 | 920 | } |
1e6dd65e | 921 | |
543323bc | 922 | static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p) |
c820cc45 | 923 | { |
730947bc | 924 | struct vivi_dev *dev = video_drvdata(file); |
e007a325 | 925 | return vb2_dqbuf(&dev->vb_vidq, p, file->f_flags & O_NONBLOCK); |
c820cc45 | 926 | } |
1e6dd65e | 927 | |
dc46ace1 | 928 | static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i) |
c820cc45 | 929 | { |
730947bc | 930 | struct vivi_dev *dev = video_drvdata(file); |
e007a325 | 931 | return vb2_streamon(&dev->vb_vidq, i); |
c820cc45 | 932 | } |
1e6dd65e | 933 | |
dc46ace1 | 934 | static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) |
c820cc45 | 935 | { |
730947bc | 936 | struct vivi_dev *dev = video_drvdata(file); |
e007a325 | 937 | return vb2_streamoff(&dev->vb_vidq, i); |
c820cc45 MCC |
938 | } |
939 | ||
543323bc | 940 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i) |
c820cc45 | 941 | { |
c820cc45 MCC |
942 | return 0; |
943 | } | |
1e6dd65e | 944 | |
c820cc45 | 945 | /* only one input in this sample driver */ |
543323bc | 946 | static int vidioc_enum_input(struct file *file, void *priv, |
c820cc45 MCC |
947 | struct v4l2_input *inp) |
948 | { | |
e164b58a | 949 | if (inp->index >= NUM_INPUTS) |
c820cc45 | 950 | return -EINVAL; |
1e6dd65e | 951 | |
c820cc45 | 952 | inp->type = V4L2_INPUT_TYPE_CAMERA; |
784c668b | 953 | inp->std = V4L2_STD_525_60; |
e164b58a | 954 | sprintf(inp->name, "Camera %u", inp->index); |
730947bc | 955 | return 0; |
c820cc45 | 956 | } |
1e6dd65e | 957 | |
543323bc | 958 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
c820cc45 | 959 | { |
730947bc | 960 | struct vivi_dev *dev = video_drvdata(file); |
e164b58a MCC |
961 | |
962 | *i = dev->input; | |
730947bc | 963 | return 0; |
c820cc45 | 964 | } |
730947bc | 965 | |
543323bc | 966 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) |
c820cc45 | 967 | { |
730947bc | 968 | struct vivi_dev *dev = video_drvdata(file); |
e164b58a MCC |
969 | |
970 | if (i >= NUM_INPUTS) | |
c820cc45 | 971 | return -EINVAL; |
1e6dd65e | 972 | |
e164b58a | 973 | dev->input = i; |
730947bc HV |
974 | precalculate_bars(dev); |
975 | precalculate_line(dev); | |
976 | return 0; | |
c820cc45 | 977 | } |
1e6dd65e | 978 | |
730947bc | 979 | /* --- controls ---------------------------------------------- */ |
1e6dd65e | 980 | |
7e996afa | 981 | static int vivi_s_ctrl(struct v4l2_ctrl *ctrl) |
c820cc45 | 982 | { |
7e996afa | 983 | struct vivi_dev *dev = container_of(ctrl->handler, struct vivi_dev, ctrl_handler); |
1e6dd65e | 984 | |
7e996afa HV |
985 | if (ctrl == dev->button) |
986 | dev->button_pressed = 30; | |
987 | return 0; | |
1e6dd65e MCC |
988 | } |
989 | ||
990 | /* ------------------------------------------------------------------ | |
991 | File operations for the device | |
992 | ------------------------------------------------------------------*/ | |
993 | ||
1e6dd65e MCC |
994 | static ssize_t |
995 | vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos) | |
996 | { | |
730947bc | 997 | struct vivi_dev *dev = video_drvdata(file); |
1e6dd65e | 998 | |
e007a325 PO |
999 | dprintk(dev, 1, "read called\n"); |
1000 | return vb2_read(&dev->vb_vidq, data, count, ppos, | |
1001 | file->f_flags & O_NONBLOCK); | |
1e6dd65e MCC |
1002 | } |
1003 | ||
1004 | static unsigned int | |
1005 | vivi_poll(struct file *file, struct poll_table_struct *wait) | |
1006 | { | |
730947bc | 1007 | struct vivi_dev *dev = video_drvdata(file); |
e007a325 | 1008 | struct vb2_queue *q = &dev->vb_vidq; |
1e6dd65e | 1009 | |
7e28adb2 | 1010 | dprintk(dev, 1, "%s\n", __func__); |
e007a325 | 1011 | return vb2_poll(q, file, wait); |
1e6dd65e MCC |
1012 | } |
1013 | ||
bec43661 | 1014 | static int vivi_close(struct file *file) |
1e6dd65e | 1015 | { |
50462eb0 | 1016 | struct video_device *vdev = video_devdata(file); |
730947bc | 1017 | struct vivi_dev *dev = video_drvdata(file); |
1e6dd65e | 1018 | |
e007a325 PO |
1019 | dprintk(dev, 1, "close called (dev=%s), file %p\n", |
1020 | video_device_node_name(vdev), file); | |
1e6dd65e | 1021 | |
2e4784d0 | 1022 | if (v4l2_fh_is_singular_file(file)) |
e007a325 | 1023 | vb2_queue_release(&dev->vb_vidq); |
2e4784d0 | 1024 | return v4l2_fh_release(file); |
1e6dd65e MCC |
1025 | } |
1026 | ||
543323bc | 1027 | static int vivi_mmap(struct file *file, struct vm_area_struct *vma) |
1e6dd65e | 1028 | { |
730947bc | 1029 | struct vivi_dev *dev = video_drvdata(file); |
1e6dd65e MCC |
1030 | int ret; |
1031 | ||
6c2f9901 | 1032 | dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma); |
1e6dd65e | 1033 | |
e007a325 | 1034 | ret = vb2_mmap(&dev->vb_vidq, vma); |
6c2f9901 | 1035 | dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n", |
1e6dd65e | 1036 | (unsigned long)vma->vm_start, |
730947bc | 1037 | (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, |
1e6dd65e | 1038 | ret); |
1e6dd65e MCC |
1039 | return ret; |
1040 | } | |
1041 | ||
7e996afa HV |
1042 | static const struct v4l2_ctrl_ops vivi_ctrl_ops = { |
1043 | .s_ctrl = vivi_s_ctrl, | |
1044 | }; | |
1045 | ||
1046 | #define VIVI_CID_CUSTOM_BASE (V4L2_CID_USER_BASE | 0xf000) | |
1047 | ||
1048 | static const struct v4l2_ctrl_config vivi_ctrl_button = { | |
1049 | .ops = &vivi_ctrl_ops, | |
1050 | .id = VIVI_CID_CUSTOM_BASE + 0, | |
1051 | .name = "Button", | |
1052 | .type = V4L2_CTRL_TYPE_BUTTON, | |
1053 | }; | |
1054 | ||
1055 | static const struct v4l2_ctrl_config vivi_ctrl_boolean = { | |
1056 | .ops = &vivi_ctrl_ops, | |
1057 | .id = VIVI_CID_CUSTOM_BASE + 1, | |
1058 | .name = "Boolean", | |
1059 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
1060 | .min = 0, | |
1061 | .max = 1, | |
1062 | .step = 1, | |
1063 | .def = 1, | |
1064 | }; | |
1065 | ||
1066 | static const struct v4l2_ctrl_config vivi_ctrl_int32 = { | |
1067 | .ops = &vivi_ctrl_ops, | |
1068 | .id = VIVI_CID_CUSTOM_BASE + 2, | |
1069 | .name = "Integer 32 Bits", | |
1070 | .type = V4L2_CTRL_TYPE_INTEGER, | |
5b283029 HV |
1071 | .min = 0x80000000, |
1072 | .max = 0x7fffffff, | |
7e996afa HV |
1073 | .step = 1, |
1074 | }; | |
1075 | ||
1076 | static const struct v4l2_ctrl_config vivi_ctrl_int64 = { | |
1077 | .ops = &vivi_ctrl_ops, | |
1078 | .id = VIVI_CID_CUSTOM_BASE + 3, | |
1079 | .name = "Integer 64 Bits", | |
1080 | .type = V4L2_CTRL_TYPE_INTEGER64, | |
1081 | }; | |
1082 | ||
1083 | static const char * const vivi_ctrl_menu_strings[] = { | |
1084 | "Menu Item 0 (Skipped)", | |
1085 | "Menu Item 1", | |
1086 | "Menu Item 2 (Skipped)", | |
1087 | "Menu Item 3", | |
1088 | "Menu Item 4", | |
1089 | "Menu Item 5 (Skipped)", | |
1090 | NULL, | |
1091 | }; | |
1092 | ||
1093 | static const struct v4l2_ctrl_config vivi_ctrl_menu = { | |
1094 | .ops = &vivi_ctrl_ops, | |
1095 | .id = VIVI_CID_CUSTOM_BASE + 4, | |
1096 | .name = "Menu", | |
1097 | .type = V4L2_CTRL_TYPE_MENU, | |
1098 | .min = 1, | |
1099 | .max = 4, | |
1100 | .def = 3, | |
1101 | .menu_skip_mask = 0x04, | |
1102 | .qmenu = vivi_ctrl_menu_strings, | |
1103 | }; | |
1104 | ||
1105 | static const struct v4l2_ctrl_config vivi_ctrl_string = { | |
1106 | .ops = &vivi_ctrl_ops, | |
1107 | .id = VIVI_CID_CUSTOM_BASE + 5, | |
1108 | .name = "String", | |
1109 | .type = V4L2_CTRL_TYPE_STRING, | |
1110 | .min = 2, | |
1111 | .max = 4, | |
1112 | .step = 1, | |
1113 | }; | |
1114 | ||
bec43661 | 1115 | static const struct v4l2_file_operations vivi_fops = { |
1e6dd65e | 1116 | .owner = THIS_MODULE, |
2e4784d0 | 1117 | .open = v4l2_fh_open, |
f905c442 | 1118 | .release = vivi_close, |
1e6dd65e MCC |
1119 | .read = vivi_read, |
1120 | .poll = vivi_poll, | |
fedc6c81 | 1121 | .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */ |
5a037706 | 1122 | .mmap = vivi_mmap, |
1e6dd65e MCC |
1123 | }; |
1124 | ||
a399810c | 1125 | static const struct v4l2_ioctl_ops vivi_ioctl_ops = { |
c820cc45 | 1126 | .vidioc_querycap = vidioc_querycap, |
78b526a4 HV |
1127 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
1128 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, | |
1129 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, | |
1130 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, | |
c820cc45 MCC |
1131 | .vidioc_reqbufs = vidioc_reqbufs, |
1132 | .vidioc_querybuf = vidioc_querybuf, | |
1133 | .vidioc_qbuf = vidioc_qbuf, | |
1134 | .vidioc_dqbuf = vidioc_dqbuf, | |
1135 | .vidioc_s_std = vidioc_s_std, | |
1136 | .vidioc_enum_input = vidioc_enum_input, | |
1137 | .vidioc_g_input = vidioc_g_input, | |
1138 | .vidioc_s_input = vidioc_s_input, | |
730947bc HV |
1139 | .vidioc_streamon = vidioc_streamon, |
1140 | .vidioc_streamoff = vidioc_streamoff, | |
a399810c HV |
1141 | }; |
1142 | ||
1143 | static struct video_device vivi_template = { | |
1144 | .name = "vivi", | |
a399810c HV |
1145 | .fops = &vivi_fops, |
1146 | .ioctl_ops = &vivi_ioctl_ops, | |
a399810c HV |
1147 | .release = video_device_release, |
1148 | ||
784c668b | 1149 | .tvnorms = V4L2_STD_525_60, |
e75f9cee | 1150 | .current_norm = V4L2_STD_NTSC_M, |
1e6dd65e | 1151 | }; |
5ab6c9af | 1152 | |
c820cc45 | 1153 | /* ----------------------------------------------------------------- |
1e6dd65e MCC |
1154 | Initialization and module stuff |
1155 | ------------------------------------------------------------------*/ | |
1156 | ||
5ab6c9af HV |
1157 | static int vivi_release(void) |
1158 | { | |
1159 | struct vivi_dev *dev; | |
1160 | struct list_head *list; | |
980d4f17 | 1161 | |
5ab6c9af HV |
1162 | while (!list_empty(&vivi_devlist)) { |
1163 | list = vivi_devlist.next; | |
1164 | list_del(list); | |
1165 | dev = list_entry(list, struct vivi_dev, vivi_devlist); | |
1166 | ||
38c7c036 LP |
1167 | v4l2_info(&dev->v4l2_dev, "unregistering %s\n", |
1168 | video_device_node_name(dev->vfd)); | |
5ab6c9af HV |
1169 | video_unregister_device(dev->vfd); |
1170 | v4l2_device_unregister(&dev->v4l2_dev); | |
7e996afa | 1171 | v4l2_ctrl_handler_free(&dev->ctrl_handler); |
5ab6c9af HV |
1172 | kfree(dev); |
1173 | } | |
1174 | ||
1175 | return 0; | |
1176 | } | |
1177 | ||
c41ee24b | 1178 | static int __init vivi_create_instance(int inst) |
1e6dd65e | 1179 | { |
1e6dd65e | 1180 | struct vivi_dev *dev; |
f905c442 | 1181 | struct video_device *vfd; |
7e996afa | 1182 | struct v4l2_ctrl_handler *hdl; |
e007a325 | 1183 | struct vb2_queue *q; |
730947bc | 1184 | int ret; |
1e6dd65e | 1185 | |
5ab6c9af HV |
1186 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
1187 | if (!dev) | |
1188 | return -ENOMEM; | |
980d4f17 | 1189 | |
5ab6c9af | 1190 | snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name), |
c41ee24b | 1191 | "%s-%03d", VIVI_MODULE_NAME, inst); |
5ab6c9af HV |
1192 | ret = v4l2_device_register(NULL, &dev->v4l2_dev); |
1193 | if (ret) | |
1194 | goto free_dev; | |
1e6dd65e | 1195 | |
730947bc HV |
1196 | dev->fmt = &formats[0]; |
1197 | dev->width = 640; | |
1198 | dev->height = 480; | |
7e996afa HV |
1199 | hdl = &dev->ctrl_handler; |
1200 | v4l2_ctrl_handler_init(hdl, 11); | |
1201 | dev->volume = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops, | |
1202 | V4L2_CID_AUDIO_VOLUME, 0, 255, 1, 200); | |
1203 | dev->brightness = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops, | |
1204 | V4L2_CID_BRIGHTNESS, 0, 255, 1, 127); | |
1205 | dev->contrast = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops, | |
1206 | V4L2_CID_CONTRAST, 0, 255, 1, 16); | |
1207 | dev->saturation = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops, | |
1208 | V4L2_CID_SATURATION, 0, 255, 1, 127); | |
1209 | dev->hue = v4l2_ctrl_new_std(hdl, &vivi_ctrl_ops, | |
1210 | V4L2_CID_HUE, -128, 127, 1, 0); | |
1211 | dev->button = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_button, NULL); | |
1212 | dev->int32 = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_int32, NULL); | |
1213 | dev->int64 = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_int64, NULL); | |
1214 | dev->boolean = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_boolean, NULL); | |
1215 | dev->menu = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_menu, NULL); | |
1216 | dev->string = v4l2_ctrl_new_custom(hdl, &vivi_ctrl_string, NULL); | |
1217 | if (hdl->error) { | |
1218 | ret = hdl->error; | |
1219 | goto unreg_dev; | |
1220 | } | |
1221 | dev->v4l2_dev.ctrl_handler = hdl; | |
730947bc | 1222 | |
fedc6c81 HV |
1223 | /* initialize locks */ |
1224 | spin_lock_init(&dev->slock); | |
fedc6c81 | 1225 | |
e007a325 PO |
1226 | /* initialize queue */ |
1227 | q = &dev->vb_vidq; | |
1228 | memset(q, 0, sizeof(dev->vb_vidq)); | |
1229 | q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1230 | q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; | |
1231 | q->drv_priv = dev; | |
1232 | q->buf_struct_size = sizeof(struct vivi_buffer); | |
1233 | q->ops = &vivi_video_qops; | |
1234 | q->mem_ops = &vb2_vmalloc_memops; | |
1235 | ||
1236 | vb2_queue_init(q); | |
1237 | ||
1238 | mutex_init(&dev->mutex); | |
730947bc | 1239 | |
5ab6c9af HV |
1240 | /* init video dma queues */ |
1241 | INIT_LIST_HEAD(&dev->vidq.active); | |
1242 | init_waitqueue_head(&dev->vidq.wq); | |
1e6dd65e | 1243 | |
5ab6c9af HV |
1244 | ret = -ENOMEM; |
1245 | vfd = video_device_alloc(); | |
1246 | if (!vfd) | |
1247 | goto unreg_dev; | |
55712ff7 | 1248 | |
5ab6c9af | 1249 | *vfd = vivi_template; |
c285addb | 1250 | vfd->debug = debug; |
730947bc | 1251 | vfd->v4l2_dev = &dev->v4l2_dev; |
b1a873a3 | 1252 | set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags); |
e007a325 PO |
1253 | |
1254 | /* | |
1255 | * Provide a mutex to v4l2 core. It will be used to protect | |
1256 | * all fops and v4l2 ioctls. | |
1257 | */ | |
fedc6c81 | 1258 | vfd->lock = &dev->mutex; |
55712ff7 | 1259 | |
5ab6c9af HV |
1260 | ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr); |
1261 | if (ret < 0) | |
1262 | goto rel_vdev; | |
980d4f17 | 1263 | |
5ab6c9af | 1264 | video_set_drvdata(vfd, dev); |
980d4f17 | 1265 | |
5ab6c9af HV |
1266 | /* Now that everything is fine, let's add it to device list */ |
1267 | list_add_tail(&dev->vivi_devlist, &vivi_devlist); | |
980d4f17 | 1268 | |
7de0b873 | 1269 | if (video_nr != -1) |
5ab6c9af | 1270 | video_nr++; |
f905c442 | 1271 | |
5ab6c9af | 1272 | dev->vfd = vfd; |
38c7c036 LP |
1273 | v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n", |
1274 | video_device_node_name(vfd)); | |
5ab6c9af HV |
1275 | return 0; |
1276 | ||
1277 | rel_vdev: | |
1278 | video_device_release(vfd); | |
1279 | unreg_dev: | |
7e996afa | 1280 | v4l2_ctrl_handler_free(hdl); |
5ab6c9af HV |
1281 | v4l2_device_unregister(&dev->v4l2_dev); |
1282 | free_dev: | |
1283 | kfree(dev); | |
1284 | return ret; | |
1285 | } | |
f905c442 | 1286 | |
5ab6c9af HV |
1287 | /* This routine allocates from 1 to n_devs virtual drivers. |
1288 | ||
1289 | The real maximum number of virtual drivers will depend on how many drivers | |
1290 | will succeed. This is limited to the maximum number of devices that | |
1291 | videodev supports, which is equal to VIDEO_NUM_DEVICES. | |
1292 | */ | |
1293 | static int __init vivi_init(void) | |
1294 | { | |
730947bc | 1295 | const struct font_desc *font = find_font("VGA8x16"); |
9185cbfc | 1296 | int ret = 0, i; |
5ab6c9af | 1297 | |
730947bc HV |
1298 | if (font == NULL) { |
1299 | printk(KERN_ERR "vivi: could not find font\n"); | |
1300 | return -ENODEV; | |
1301 | } | |
1302 | font8x16 = font->data; | |
1303 | ||
5ab6c9af HV |
1304 | if (n_devs <= 0) |
1305 | n_devs = 1; | |
1306 | ||
1307 | for (i = 0; i < n_devs; i++) { | |
1308 | ret = vivi_create_instance(i); | |
1309 | if (ret) { | |
1310 | /* If some instantiations succeeded, keep driver */ | |
1311 | if (i) | |
1312 | ret = 0; | |
1313 | break; | |
1314 | } | |
55712ff7 | 1315 | } |
f905c442 | 1316 | |
55712ff7 | 1317 | if (ret < 0) { |
730947bc | 1318 | printk(KERN_ERR "vivi: error %d while loading driver\n", ret); |
5ab6c9af HV |
1319 | return ret; |
1320 | } | |
1321 | ||
1322 | printk(KERN_INFO "Video Technology Magazine Virtual Video " | |
1990d50b MCC |
1323 | "Capture Board ver %s successfully loaded.\n", |
1324 | VIVI_VERSION); | |
980d4f17 | 1325 | |
5ab6c9af HV |
1326 | /* n_devs will reflect the actual number of allocated devices */ |
1327 | n_devs = i; | |
980d4f17 | 1328 | |
1e6dd65e MCC |
1329 | return ret; |
1330 | } | |
1331 | ||
1332 | static void __exit vivi_exit(void) | |
1333 | { | |
55712ff7 | 1334 | vivi_release(); |
1e6dd65e MCC |
1335 | } |
1336 | ||
1337 | module_init(vivi_init); | |
1338 | module_exit(vivi_exit); |