[media] Stop using linux/version.h on most video drivers
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / vivi.c
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 *
10 * Conversion to videobuf2 by Pawel Osciak & Marek Szyprowski
11 * Copyright (c) 2010 Samsung Electronics
12 *
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>
19 #include <linux/errno.h>
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/font.h>
25 #include <linux/mutex.h>
26 #include <linux/videodev2.h>
27 #include <linux/kthread.h>
28 #include <linux/freezer.h>
29 #include <media/videobuf2-vmalloc.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/v4l2-ctrls.h>
33 #include <media/v4l2-fh.h>
34 #include <media/v4l2-common.h>
35
36 #define VIVI_MODULE_NAME "vivi"
37
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
43 #define MAX_WIDTH 1920
44 #define MAX_HEIGHT 1200
45
46 #define VIVI_VERSION "0.8.1"
47
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");
51 MODULE_VERSION(VIVI_VERSION);
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
69 /* Global font descriptor */
70 static const u8 *font8x16;
71
72 #define dprintk(dev, level, fmt, arg...) \
73 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
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
85 static struct vivi_fmt formats[] = {
86 {
87 .name = "4:2:2, packed, YUYV",
88 .fourcc = V4L2_PIX_FMT_YUYV,
89 .depth = 16,
90 },
91 {
92 .name = "4:2:2, packed, UYVY",
93 .fourcc = V4L2_PIX_FMT_UYVY,
94 .depth = 16,
95 },
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 },
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 },
116 };
117
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
135 /* buffer for one video frame */
136 struct vivi_buffer {
137 /* common v4l buffer stuff -- must be first */
138 struct vb2_buffer vb;
139 struct list_head list;
140 struct vivi_fmt *fmt;
141 };
142
143 struct vivi_dmaqueue {
144 struct list_head active;
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;
158 struct v4l2_device v4l2_dev;
159 struct v4l2_ctrl_handler ctrl_handler;
160
161 /* controls */
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;
173
174 spinlock_t slock;
175 struct mutex mutex;
176
177 /* various device info */
178 struct video_device *vfd;
179
180 struct vivi_dmaqueue vidq;
181
182 /* Several counters */
183 unsigned ms;
184 unsigned long jiffies;
185 unsigned button_pressed;
186
187 int mv_count; /* Controls bars movement */
188
189 /* Input Number */
190 int input;
191
192 /* video capture */
193 struct vivi_fmt *fmt;
194 unsigned int width, height;
195 struct vb2_queue vb_vidq;
196 enum v4l2_field field;
197 unsigned int field_count;
198
199 u8 bars[9][3];
200 u8 line[MAX_WIDTH * 4];
201 };
202
203 /* ------------------------------------------------------------------
204 DMA and thread functions
205 ------------------------------------------------------------------*/
206
207 /* Bars and Colors should match positions */
208
209 enum colors {
210 WHITE,
211 AMBER,
212 CYAN,
213 GREEN,
214 MAGENTA,
215 RED,
216 BLUE,
217 BLACK,
218 TEXT_BLACK,
219 };
220
221 /* R G B */
222 #define COLOR_WHITE {204, 204, 204}
223 #define COLOR_AMBER {208, 208, 0}
224 #define COLOR_CYAN { 0, 206, 206}
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 {
232 u8 bar[9][3];
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 */
239 { COLOR_WHITE, COLOR_AMBER, COLOR_CYAN, COLOR_GREEN,
240 COLOR_MAGENTA, COLOR_RED, COLOR_BLUE, COLOR_BLACK, COLOR_BLACK }
241 }, {
242 { COLOR_WHITE, COLOR_AMBER, COLOR_BLACK, COLOR_WHITE,
243 COLOR_AMBER, COLOR_BLACK, COLOR_WHITE, COLOR_AMBER, COLOR_BLACK }
244 }, {
245 { COLOR_WHITE, COLOR_CYAN, COLOR_BLACK, COLOR_WHITE,
246 COLOR_CYAN, COLOR_BLACK, COLOR_WHITE, COLOR_CYAN, COLOR_BLACK }
247 }, {
248 { COLOR_WHITE, COLOR_GREEN, COLOR_BLACK, COLOR_WHITE,
249 COLOR_GREEN, COLOR_BLACK, COLOR_WHITE, COLOR_GREEN, COLOR_BLACK }
250 },
251 };
252
253 #define NUM_INPUTS ARRAY_SIZE(bars)
254
255 #define TO_Y(r, g, b) \
256 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
257 /* RGB to V(Cr) Color transform */
258 #define TO_V(r, g, b) \
259 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
260 /* RGB to U(Cb) Color transform */
261 #define TO_U(r, g, b) \
262 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
263
264 /* precalculate color bar values to speed up rendering */
265 static void precalculate_bars(struct vivi_dev *dev)
266 {
267 u8 r, g, b;
268 int k, is_yuv;
269
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];
274 is_yuv = 0;
275
276 switch (dev->fmt->fourcc) {
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) {
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 */
299 } else {
300 dev->bars[k][0] = r;
301 dev->bars[k][1] = g;
302 dev->bars[k][2] = b;
303 }
304 }
305 }
306
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)
311
312 static void gen_twopix(struct vivi_dev *dev, u8 *buf, int colorpos)
313 {
314 u8 r_y, g_u, b_v;
315 int color;
316 u8 *p;
317
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 */
321
322 for (color = 0; color < 4; color++) {
323 p = buf + color;
324
325 switch (dev->fmt->fourcc) {
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 }
339 break;
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;
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;
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;
402 }
403 }
404 }
405
406 static void precalculate_line(struct vivi_dev *dev)
407 {
408 int w;
409
410 for (w = 0; w < dev->width * 2; w += 2) {
411 int colorpos = (w / (dev->width / 8) % 8);
412
413 gen_twopix(dev, dev->line + w * 2, colorpos);
414 }
415 }
416
417 static void gen_text(struct vivi_dev *dev, char *basep,
418 int y, int x, char *text)
419 {
420 int line;
421
422 /* Checks if it is possible to show string */
423 if (y + 16 >= dev->height || x + strlen(text) * 8 >= dev->width)
424 return;
425
426 /* Print stream time */
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++) {
437 /* Draw white font on black background */
438 if (chr & (1 << (7 - i)))
439 gen_twopix(dev, pos + j * 2, WHITE);
440 else
441 gen_twopix(dev, pos + j * 2, TEXT_BLACK);
442 }
443 }
444 }
445 }
446
447 static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf)
448 {
449 int wmax = dev->width;
450 int hmax = dev->height;
451 struct timeval ts;
452 void *vbuf = vb2_plane_vaddr(&buf->vb, 0);
453 unsigned ms;
454 char str[100];
455 int h, line = 1;
456
457 if (!vbuf)
458 return;
459
460 for (h = 0; h < hmax; h++)
461 memcpy(vbuf + h * wmax * 2, dev->line + (dev->mv_count % wmax) * 2, wmax * 2);
462
463 /* Updates stream time */
464
465 dev->ms += jiffies_to_msecs(jiffies - dev->jiffies);
466 dev->jiffies = jiffies;
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
478 mutex_lock(&dev->ctrl_handler.lock);
479 snprintf(str, sizeof(str), " brightness %3d, contrast %3d, saturation %3d, hue %d ",
480 dev->brightness->cur.val,
481 dev->contrast->cur.val,
482 dev->saturation->cur.val,
483 dev->hue->cur.val);
484 gen_text(dev, vbuf, line++ * 16, 16, str);
485 snprintf(str, sizeof(str), " volume %3d ", dev->volume->cur.val);
486 gen_text(dev, vbuf, line++ * 16, 16, str);
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 }
502
503 dev->mv_count += 2;
504
505 buf->vb.v4l2_buf.field = dev->field;
506 dev->field_count++;
507 buf->vb.v4l2_buf.sequence = dev->field_count >> 1;
508 do_gettimeofday(&ts);
509 buf->vb.v4l2_buf.timestamp = ts;
510 }
511
512 static void vivi_thread_tick(struct vivi_dev *dev)
513 {
514 struct vivi_dmaqueue *dma_q = &dev->vidq;
515 struct vivi_buffer *buf;
516 unsigned long flags = 0;
517
518 dprintk(dev, 1, "Thread tick\n");
519
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 }
525
526 buf = list_entry(dma_q->active.next, struct vivi_buffer, list);
527 list_del(&buf->list);
528
529 do_gettimeofday(&buf->vb.v4l2_buf.timestamp);
530
531 /* Fill buffer */
532 vivi_fillbuff(dev, buf);
533 dprintk(dev, 1, "filled buffer %p\n", buf);
534
535 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
536 dprintk(dev, 2, "[%p/%d] done\n", buf, buf->vb.v4l2_buf.index);
537 unlock:
538 spin_unlock_irqrestore(&dev->slock, flags);
539 }
540
541 #define frames_to_ms(frames) \
542 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
543
544 static void vivi_sleep(struct vivi_dev *dev)
545 {
546 struct vivi_dmaqueue *dma_q = &dev->vidq;
547 int timeout;
548 DECLARE_WAITQUEUE(wait, current);
549
550 dprintk(dev, 1, "%s dma_q=0x%08lx\n", __func__,
551 (unsigned long)dma_q);
552
553 add_wait_queue(&dma_q->wq, &wait);
554 if (kthread_should_stop())
555 goto stop_task;
556
557 /* Calculate time to wake up */
558 timeout = msecs_to_jiffies(frames_to_ms(1));
559
560 vivi_thread_tick(dev);
561
562 schedule_timeout_interruptible(timeout);
563
564 stop_task:
565 remove_wait_queue(&dma_q->wq, &wait);
566 try_to_freeze();
567 }
568
569 static int vivi_thread(void *data)
570 {
571 struct vivi_dev *dev = data;
572
573 dprintk(dev, 1, "thread started\n");
574
575 set_freezable();
576
577 for (;;) {
578 vivi_sleep(dev);
579
580 if (kthread_should_stop())
581 break;
582 }
583 dprintk(dev, 1, "thread: exit\n");
584 return 0;
585 }
586
587 static int vivi_start_generating(struct vivi_dev *dev)
588 {
589 struct vivi_dmaqueue *dma_q = &dev->vidq;
590
591 dprintk(dev, 1, "%s\n", __func__);
592
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);
601
602 if (IS_ERR(dma_q->kthread)) {
603 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
604 return PTR_ERR(dma_q->kthread);
605 }
606 /* Wakes thread */
607 wake_up_interruptible(&dma_q->wq);
608
609 dprintk(dev, 1, "returning from %s\n", __func__);
610 return 0;
611 }
612
613 static void vivi_stop_generating(struct vivi_dev *dev)
614 {
615 struct vivi_dmaqueue *dma_q = &dev->vidq;
616
617 dprintk(dev, 1, "%s\n", __func__);
618
619 /* shutdown control thread */
620 if (dma_q->kthread) {
621 kthread_stop(dma_q->kthread);
622 dma_q->kthread = NULL;
623 }
624
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 }
638 }
639 /* ------------------------------------------------------------------
640 Videobuf operations
641 ------------------------------------------------------------------*/
642 static int queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
643 unsigned int *nplanes, unsigned long sizes[],
644 void *alloc_ctxs[])
645 {
646 struct vivi_dev *dev = vb2_get_drv_priv(vq);
647 unsigned long size;
648
649 size = dev->width * dev->height * 2;
650
651 if (0 == *nbuffers)
652 *nbuffers = 32;
653
654 while (size * *nbuffers > vid_limit * 1024 * 1024)
655 (*nbuffers)--;
656
657 *nplanes = 1;
658
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);
668
669 return 0;
670 }
671
672 static int buffer_init(struct vb2_buffer *vb)
673 {
674 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
675
676 BUG_ON(NULL == dev->fmt);
677
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;
691 }
692
693 static int buffer_prepare(struct vb2_buffer *vb)
694 {
695 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
696 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
697 unsigned long size;
698
699 dprintk(dev, 1, "%s, field=%d\n", __func__, vb->v4l2_buf.field);
700
701 BUG_ON(NULL == dev->fmt);
702
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 */
709 if (dev->width < 48 || dev->width > MAX_WIDTH ||
710 dev->height < 32 || dev->height > MAX_HEIGHT)
711 return -EINVAL;
712
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);
717 return -EINVAL;
718 }
719
720 vb2_set_plane_payload(&buf->vb, 0, size);
721
722 buf->fmt = dev->fmt;
723
724 precalculate_bars(dev);
725 precalculate_line(dev);
726
727 return 0;
728 }
729
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__);
734 return 0;
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__);
741
742 }
743
744 static void buffer_queue(struct vb2_buffer *vb)
745 {
746 struct vivi_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
747 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
748 struct vivi_dmaqueue *vidq = &dev->vidq;
749 unsigned long flags = 0;
750
751 dprintk(dev, 1, "%s\n", __func__);
752
753 spin_lock_irqsave(&dev->slock, flags);
754 list_add_tail(&buf->list, &vidq->active);
755 spin_unlock_irqrestore(&dev->slock, flags);
756 }
757
758 static int start_streaming(struct vb2_queue *vq)
759 {
760 struct vivi_dev *dev = vb2_get_drv_priv(vq);
761 dprintk(dev, 1, "%s\n", __func__);
762 return vivi_start_generating(dev);
763 }
764
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);
769 dprintk(dev, 1, "%s\n", __func__);
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 }
779
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);
784 }
785
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,
798 };
799
800 /* ------------------------------------------------------------------
801 IOCTL vidioc handling
802 ------------------------------------------------------------------*/
803 static int vidioc_querycap(struct file *file, void *priv,
804 struct v4l2_capability *cap)
805 {
806 struct vivi_dev *dev = video_drvdata(file);
807
808 strcpy(cap->driver, "vivi");
809 strcpy(cap->card, "vivi");
810 strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
811 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | \
812 V4L2_CAP_READWRITE;
813 return 0;
814 }
815
816 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
817 struct v4l2_fmtdesc *f)
818 {
819 struct vivi_fmt *fmt;
820
821 if (f->index >= ARRAY_SIZE(formats))
822 return -EINVAL;
823
824 fmt = &formats[f->index];
825
826 strlcpy(f->description, fmt->name, sizeof(f->description));
827 f->pixelformat = fmt->fourcc;
828 return 0;
829 }
830
831 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
832 struct v4l2_format *f)
833 {
834 struct vivi_dev *dev = video_drvdata(file);
835
836 f->fmt.pix.width = dev->width;
837 f->fmt.pix.height = dev->height;
838 f->fmt.pix.field = dev->field;
839 f->fmt.pix.pixelformat = dev->fmt->fourcc;
840 f->fmt.pix.bytesperline =
841 (f->fmt.pix.width * dev->fmt->depth) >> 3;
842 f->fmt.pix.sizeimage =
843 f->fmt.pix.height * f->fmt.pix.bytesperline;
844 return 0;
845 }
846
847 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
848 struct v4l2_format *f)
849 {
850 struct vivi_dev *dev = video_drvdata(file);
851 struct vivi_fmt *fmt;
852 enum v4l2_field field;
853
854 fmt = get_format(f);
855 if (!fmt) {
856 dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n",
857 f->fmt.pix.pixelformat);
858 return -EINVAL;
859 }
860
861 field = f->fmt.pix.field;
862
863 if (field == V4L2_FIELD_ANY) {
864 field = V4L2_FIELD_INTERLACED;
865 } else if (V4L2_FIELD_INTERLACED != field) {
866 dprintk(dev, 1, "Field type invalid.\n");
867 return -EINVAL;
868 }
869
870 f->fmt.pix.field = field;
871 v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 2,
872 &f->fmt.pix.height, 32, MAX_HEIGHT, 0, 0);
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;
877 return 0;
878 }
879
880 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
881 struct v4l2_format *f)
882 {
883 struct vivi_dev *dev = video_drvdata(file);
884 struct vb2_queue *q = &dev->vb_vidq;
885
886 int ret = vidioc_try_fmt_vid_cap(file, priv, f);
887 if (ret < 0)
888 return ret;
889
890 if (vb2_is_streaming(q)) {
891 dprintk(dev, 1, "%s device busy\n", __func__);
892 return -EBUSY;
893 }
894
895 dev->fmt = get_format(f);
896 dev->width = f->fmt.pix.width;
897 dev->height = f->fmt.pix.height;
898 dev->field = f->fmt.pix.field;
899
900 return 0;
901 }
902
903 static int vidioc_reqbufs(struct file *file, void *priv,
904 struct v4l2_requestbuffers *p)
905 {
906 struct vivi_dev *dev = video_drvdata(file);
907 return vb2_reqbufs(&dev->vb_vidq, p);
908 }
909
910 static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
911 {
912 struct vivi_dev *dev = video_drvdata(file);
913 return vb2_querybuf(&dev->vb_vidq, p);
914 }
915
916 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
917 {
918 struct vivi_dev *dev = video_drvdata(file);
919 return vb2_qbuf(&dev->vb_vidq, p);
920 }
921
922 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
923 {
924 struct vivi_dev *dev = video_drvdata(file);
925 return vb2_dqbuf(&dev->vb_vidq, p, file->f_flags & O_NONBLOCK);
926 }
927
928 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
929 {
930 struct vivi_dev *dev = video_drvdata(file);
931 return vb2_streamon(&dev->vb_vidq, i);
932 }
933
934 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
935 {
936 struct vivi_dev *dev = video_drvdata(file);
937 return vb2_streamoff(&dev->vb_vidq, i);
938 }
939
940 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
941 {
942 return 0;
943 }
944
945 /* only one input in this sample driver */
946 static int vidioc_enum_input(struct file *file, void *priv,
947 struct v4l2_input *inp)
948 {
949 if (inp->index >= NUM_INPUTS)
950 return -EINVAL;
951
952 inp->type = V4L2_INPUT_TYPE_CAMERA;
953 inp->std = V4L2_STD_525_60;
954 sprintf(inp->name, "Camera %u", inp->index);
955 return 0;
956 }
957
958 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
959 {
960 struct vivi_dev *dev = video_drvdata(file);
961
962 *i = dev->input;
963 return 0;
964 }
965
966 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
967 {
968 struct vivi_dev *dev = video_drvdata(file);
969
970 if (i >= NUM_INPUTS)
971 return -EINVAL;
972
973 dev->input = i;
974 precalculate_bars(dev);
975 precalculate_line(dev);
976 return 0;
977 }
978
979 /* --- controls ---------------------------------------------- */
980
981 static int vivi_s_ctrl(struct v4l2_ctrl *ctrl)
982 {
983 struct vivi_dev *dev = container_of(ctrl->handler, struct vivi_dev, ctrl_handler);
984
985 if (ctrl == dev->button)
986 dev->button_pressed = 30;
987 return 0;
988 }
989
990 /* ------------------------------------------------------------------
991 File operations for the device
992 ------------------------------------------------------------------*/
993
994 static ssize_t
995 vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
996 {
997 struct vivi_dev *dev = video_drvdata(file);
998
999 dprintk(dev, 1, "read called\n");
1000 return vb2_read(&dev->vb_vidq, data, count, ppos,
1001 file->f_flags & O_NONBLOCK);
1002 }
1003
1004 static unsigned int
1005 vivi_poll(struct file *file, struct poll_table_struct *wait)
1006 {
1007 struct vivi_dev *dev = video_drvdata(file);
1008 struct vb2_queue *q = &dev->vb_vidq;
1009
1010 dprintk(dev, 1, "%s\n", __func__);
1011 return vb2_poll(q, file, wait);
1012 }
1013
1014 static int vivi_close(struct file *file)
1015 {
1016 struct video_device *vdev = video_devdata(file);
1017 struct vivi_dev *dev = video_drvdata(file);
1018
1019 dprintk(dev, 1, "close called (dev=%s), file %p\n",
1020 video_device_node_name(vdev), file);
1021
1022 if (v4l2_fh_is_singular_file(file))
1023 vb2_queue_release(&dev->vb_vidq);
1024 return v4l2_fh_release(file);
1025 }
1026
1027 static int vivi_mmap(struct file *file, struct vm_area_struct *vma)
1028 {
1029 struct vivi_dev *dev = video_drvdata(file);
1030 int ret;
1031
1032 dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1033
1034 ret = vb2_mmap(&dev->vb_vidq, vma);
1035 dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1036 (unsigned long)vma->vm_start,
1037 (unsigned long)vma->vm_end - (unsigned long)vma->vm_start,
1038 ret);
1039 return ret;
1040 }
1041
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,
1071 .min = 0x80000000,
1072 .max = 0x7fffffff,
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
1115 static const struct v4l2_file_operations vivi_fops = {
1116 .owner = THIS_MODULE,
1117 .open = v4l2_fh_open,
1118 .release = vivi_close,
1119 .read = vivi_read,
1120 .poll = vivi_poll,
1121 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
1122 .mmap = vivi_mmap,
1123 };
1124
1125 static const struct v4l2_ioctl_ops vivi_ioctl_ops = {
1126 .vidioc_querycap = vidioc_querycap,
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,
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,
1139 .vidioc_streamon = vidioc_streamon,
1140 .vidioc_streamoff = vidioc_streamoff,
1141 };
1142
1143 static struct video_device vivi_template = {
1144 .name = "vivi",
1145 .fops = &vivi_fops,
1146 .ioctl_ops = &vivi_ioctl_ops,
1147 .release = video_device_release,
1148
1149 .tvnorms = V4L2_STD_525_60,
1150 .current_norm = V4L2_STD_NTSC_M,
1151 };
1152
1153 /* -----------------------------------------------------------------
1154 Initialization and module stuff
1155 ------------------------------------------------------------------*/
1156
1157 static int vivi_release(void)
1158 {
1159 struct vivi_dev *dev;
1160 struct list_head *list;
1161
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
1167 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1168 video_device_node_name(dev->vfd));
1169 video_unregister_device(dev->vfd);
1170 v4l2_device_unregister(&dev->v4l2_dev);
1171 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1172 kfree(dev);
1173 }
1174
1175 return 0;
1176 }
1177
1178 static int __init vivi_create_instance(int inst)
1179 {
1180 struct vivi_dev *dev;
1181 struct video_device *vfd;
1182 struct v4l2_ctrl_handler *hdl;
1183 struct vb2_queue *q;
1184 int ret;
1185
1186 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1187 if (!dev)
1188 return -ENOMEM;
1189
1190 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1191 "%s-%03d", VIVI_MODULE_NAME, inst);
1192 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1193 if (ret)
1194 goto free_dev;
1195
1196 dev->fmt = &formats[0];
1197 dev->width = 640;
1198 dev->height = 480;
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;
1222
1223 /* initialize locks */
1224 spin_lock_init(&dev->slock);
1225
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);
1239
1240 /* init video dma queues */
1241 INIT_LIST_HEAD(&dev->vidq.active);
1242 init_waitqueue_head(&dev->vidq.wq);
1243
1244 ret = -ENOMEM;
1245 vfd = video_device_alloc();
1246 if (!vfd)
1247 goto unreg_dev;
1248
1249 *vfd = vivi_template;
1250 vfd->debug = debug;
1251 vfd->v4l2_dev = &dev->v4l2_dev;
1252 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
1253
1254 /*
1255 * Provide a mutex to v4l2 core. It will be used to protect
1256 * all fops and v4l2 ioctls.
1257 */
1258 vfd->lock = &dev->mutex;
1259
1260 ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
1261 if (ret < 0)
1262 goto rel_vdev;
1263
1264 video_set_drvdata(vfd, dev);
1265
1266 /* Now that everything is fine, let's add it to device list */
1267 list_add_tail(&dev->vivi_devlist, &vivi_devlist);
1268
1269 if (video_nr != -1)
1270 video_nr++;
1271
1272 dev->vfd = vfd;
1273 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
1274 video_device_node_name(vfd));
1275 return 0;
1276
1277 rel_vdev:
1278 video_device_release(vfd);
1279 unreg_dev:
1280 v4l2_ctrl_handler_free(hdl);
1281 v4l2_device_unregister(&dev->v4l2_dev);
1282 free_dev:
1283 kfree(dev);
1284 return ret;
1285 }
1286
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 {
1295 const struct font_desc *font = find_font("VGA8x16");
1296 int ret = 0, i;
1297
1298 if (font == NULL) {
1299 printk(KERN_ERR "vivi: could not find font\n");
1300 return -ENODEV;
1301 }
1302 font8x16 = font->data;
1303
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 }
1315 }
1316
1317 if (ret < 0) {
1318 printk(KERN_ERR "vivi: error %d while loading driver\n", ret);
1319 return ret;
1320 }
1321
1322 printk(KERN_INFO "Video Technology Magazine Virtual Video "
1323 "Capture Board ver %s successfully loaded.\n",
1324 VIVI_VERSION);
1325
1326 /* n_devs will reflect the actual number of allocated devices */
1327 n_devs = i;
1328
1329 return ret;
1330 }
1331
1332 static void __exit vivi_exit(void)
1333 {
1334 vivi_release();
1335 }
1336
1337 module_init(vivi_init);
1338 module_exit(vivi_exit);