1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/module.h>
16#include <linux/delay.h>
17#include <linux/errno.h>
18#include <linux/fs.h>
19#include <linux/kernel.h>
20#include <linux/slab.h>
21#include <linux/mm.h>
22#include <linux/ioport.h>
23#include <linux/init.h>
24#include <linux/sched.h>
25#include <linux/pci.h>
26#include <linux/random.h>
27#include <linux/version.h>
28#include <linux/mutex.h>
29#include <linux/videodev2.h>
30#include <linux/dma-mapping.h>
31#include <linux/interrupt.h>
32#include <linux/kthread.h>
33#include <linux/highmem.h>
34#include <linux/freezer.h>
35#include <media/videobuf-vmalloc.h>
36#include <media/v4l2-device.h>
37#include <media/v4l2-ioctl.h>
38#include "font.h"
39
40#define VIVI_MODULE_NAME "vivi"
41
42
43#define WAKE_NUMERATOR 30
44#define WAKE_DENOMINATOR 1001
45#define BUFFER_TIMEOUT msecs_to_jiffies(500)
46
47#define VIVI_MAJOR_VERSION 0
48#define VIVI_MINOR_VERSION 6
49#define VIVI_RELEASE 0
50#define VIVI_VERSION \
51 KERNEL_VERSION(VIVI_MAJOR_VERSION, VIVI_MINOR_VERSION, VIVI_RELEASE)
52
53MODULE_DESCRIPTION("Video Technology Magazine Virtual Video Capture Board");
54MODULE_AUTHOR("Mauro Carvalho Chehab, Ted Walther and John Sokol");
55MODULE_LICENSE("Dual BSD/GPL");
56
57static unsigned video_nr = -1;
58module_param(video_nr, uint, 0644);
59MODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect");
60
61static unsigned n_devs = 1;
62module_param(n_devs, uint, 0644);
63MODULE_PARM_DESC(n_devs, "number of video devices to create");
64
65static unsigned debug;
66module_param(debug, uint, 0644);
67MODULE_PARM_DESC(debug, "activates debug info");
68
69static unsigned int vid_limit = 16;
70module_param(vid_limit, uint, 0644);
71MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
72
73
74
75static struct v4l2_queryctrl vivi_qctrl[] = {
76 {
77 .id = V4L2_CID_AUDIO_VOLUME,
78 .name = "Volume",
79 .minimum = 0,
80 .maximum = 65535,
81 .step = 65535/100,
82 .default_value = 65535,
83 .flags = V4L2_CTRL_FLAG_SLIDER,
84 .type = V4L2_CTRL_TYPE_INTEGER,
85 }, {
86 .id = V4L2_CID_BRIGHTNESS,
87 .type = V4L2_CTRL_TYPE_INTEGER,
88 .name = "Brightness",
89 .minimum = 0,
90 .maximum = 255,
91 .step = 1,
92 .default_value = 127,
93 .flags = V4L2_CTRL_FLAG_SLIDER,
94 }, {
95 .id = V4L2_CID_CONTRAST,
96 .type = V4L2_CTRL_TYPE_INTEGER,
97 .name = "Contrast",
98 .minimum = 0,
99 .maximum = 255,
100 .step = 0x1,
101 .default_value = 0x10,
102 .flags = V4L2_CTRL_FLAG_SLIDER,
103 }, {
104 .id = V4L2_CID_SATURATION,
105 .type = V4L2_CTRL_TYPE_INTEGER,
106 .name = "Saturation",
107 .minimum = 0,
108 .maximum = 255,
109 .step = 0x1,
110 .default_value = 127,
111 .flags = V4L2_CTRL_FLAG_SLIDER,
112 }, {
113 .id = V4L2_CID_HUE,
114 .type = V4L2_CTRL_TYPE_INTEGER,
115 .name = "Hue",
116 .minimum = -128,
117 .maximum = 127,
118 .step = 0x1,
119 .default_value = 0,
120 .flags = V4L2_CTRL_FLAG_SLIDER,
121 }
122};
123
124#define dprintk(dev, level, fmt, arg...) \
125 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
126
127
128
129
130
131struct vivi_fmt {
132 char *name;
133 u32 fourcc;
134 int depth;
135};
136
137static struct vivi_fmt formats[] = {
138 {
139 .name = "4:2:2, packed, YUYV",
140 .fourcc = V4L2_PIX_FMT_YUYV,
141 .depth = 16,
142 },
143 {
144 .name = "4:2:2, packed, UYVY",
145 .fourcc = V4L2_PIX_FMT_UYVY,
146 .depth = 16,
147 },
148 {
149 .name = "RGB565 (LE)",
150 .fourcc = V4L2_PIX_FMT_RGB565,
151 .depth = 16,
152 },
153 {
154 .name = "RGB565 (BE)",
155 .fourcc = V4L2_PIX_FMT_RGB565X,
156 .depth = 16,
157 },
158 {
159 .name = "RGB555 (LE)",
160 .fourcc = V4L2_PIX_FMT_RGB555,
161 .depth = 16,
162 },
163 {
164 .name = "RGB555 (BE)",
165 .fourcc = V4L2_PIX_FMT_RGB555X,
166 .depth = 16,
167 },
168};
169
170static struct vivi_fmt *get_format(struct v4l2_format *f)
171{
172 struct vivi_fmt *fmt;
173 unsigned int k;
174
175 for (k = 0; k < ARRAY_SIZE(formats); k++) {
176 fmt = &formats[k];
177 if (fmt->fourcc == f->fmt.pix.pixelformat)
178 break;
179 }
180
181 if (k == ARRAY_SIZE(formats))
182 return NULL;
183
184 return &formats[k];
185}
186
187struct sg_to_addr {
188 int pos;
189 struct scatterlist *sg;
190};
191
192
193struct vivi_buffer {
194
195 struct videobuf_buffer vb;
196
197 struct vivi_fmt *fmt;
198};
199
200struct vivi_dmaqueue {
201 struct list_head active;
202
203
204 struct task_struct *kthread;
205 wait_queue_head_t wq;
206
207 int frame;
208 int ini_jiffies;
209};
210
211static LIST_HEAD(vivi_devlist);
212
213struct vivi_dev {
214 struct list_head vivi_devlist;
215 struct v4l2_device v4l2_dev;
216
217 spinlock_t slock;
218 struct mutex mutex;
219
220 int users;
221
222
223 struct video_device *vfd;
224
225 struct vivi_dmaqueue vidq;
226
227
228 int h, m, s, ms;
229 unsigned long jiffies;
230 char timestr[13];
231
232 int mv_count;
233
234
235 int input;
236
237
238 int qctl_regs[ARRAY_SIZE(vivi_qctrl)];
239};
240
241struct vivi_fh {
242 struct vivi_dev *dev;
243
244
245 struct vivi_fmt *fmt;
246 unsigned int width, height;
247 struct videobuf_queue vb_vidq;
248
249 enum v4l2_buf_type type;
250 unsigned char bars[8][3];
251 int input;
252};
253
254
255
256
257
258
259
260enum colors {
261 WHITE,
262 AMBAR,
263 CYAN,
264 GREEN,
265 MAGENTA,
266 RED,
267 BLUE,
268 BLACK,
269};
270
271
272#define COLOR_WHITE {204, 204, 204}
273#define COLOR_AMBAR {208, 208, 0}
274#define COLOR_CIAN { 0, 206, 206}
275#define COLOR_GREEN { 0, 239, 0}
276#define COLOR_MAGENTA {239, 0, 239}
277#define COLOR_RED {205, 0, 0}
278#define COLOR_BLUE { 0, 0, 255}
279#define COLOR_BLACK { 0, 0, 0}
280
281struct bar_std {
282 u8 bar[8][3];
283};
284
285
286
287static struct bar_std bars[] = {
288 {
289 {
290 COLOR_WHITE,
291 COLOR_AMBAR,
292 COLOR_CIAN,
293 COLOR_GREEN,
294 COLOR_MAGENTA,
295 COLOR_RED,
296 COLOR_BLUE,
297 COLOR_BLACK,
298 }
299 }, {
300 {
301 COLOR_WHITE,
302 COLOR_AMBAR,
303 COLOR_BLACK,
304 COLOR_WHITE,
305 COLOR_AMBAR,
306 COLOR_BLACK,
307 COLOR_WHITE,
308 COLOR_AMBAR,
309 }
310 }, {
311 {
312 COLOR_WHITE,
313 COLOR_CIAN,
314 COLOR_BLACK,
315 COLOR_WHITE,
316 COLOR_CIAN,
317 COLOR_BLACK,
318 COLOR_WHITE,
319 COLOR_CIAN,
320 }
321 }, {
322 {
323 COLOR_WHITE,
324 COLOR_GREEN,
325 COLOR_BLACK,
326 COLOR_WHITE,
327 COLOR_GREEN,
328 COLOR_BLACK,
329 COLOR_WHITE,
330 COLOR_GREEN,
331 }
332 },
333};
334
335#define NUM_INPUTS ARRAY_SIZE(bars)
336
337#define TO_Y(r, g, b) \
338 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
339
340#define TO_V(r, g, b) \
341 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
342
343#define TO_U(r, g, b) \
344 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
345
346
347static void precalculate_bars(struct vivi_fh *fh)
348{
349 struct vivi_dev *dev = fh->dev;
350 unsigned char r, g, b;
351 int k, is_yuv;
352
353 fh->input = dev->input;
354
355 for (k = 0; k < 8; k++) {
356 r = bars[fh->input].bar[k][0];
357 g = bars[fh->input].bar[k][1];
358 b = bars[fh->input].bar[k][2];
359 is_yuv = 0;
360
361 switch (fh->fmt->fourcc) {
362 case V4L2_PIX_FMT_YUYV:
363 case V4L2_PIX_FMT_UYVY:
364 is_yuv = 1;
365 break;
366 case V4L2_PIX_FMT_RGB565:
367 case V4L2_PIX_FMT_RGB565X:
368 r >>= 3;
369 g >>= 2;
370 b >>= 3;
371 break;
372 case V4L2_PIX_FMT_RGB555:
373 case V4L2_PIX_FMT_RGB555X:
374 r >>= 3;
375 g >>= 3;
376 b >>= 3;
377 break;
378 }
379
380 if (is_yuv) {
381 fh->bars[k][0] = TO_Y(r, g, b);
382 fh->bars[k][1] = TO_U(r, g, b);
383 fh->bars[k][2] = TO_V(r, g, b);
384 } else {
385 fh->bars[k][0] = r;
386 fh->bars[k][1] = g;
387 fh->bars[k][2] = b;
388 }
389 }
390
391}
392
393#define TSTAMP_MIN_Y 24
394#define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15)
395#define TSTAMP_INPUT_X 10
396#define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X)
397
398static void gen_twopix(struct vivi_fh *fh, unsigned char *buf, int colorpos)
399{
400 unsigned char r_y, g_u, b_v;
401 unsigned char *p;
402 int color;
403
404 r_y = fh->bars[colorpos][0];
405 g_u = fh->bars[colorpos][1];
406 b_v = fh->bars[colorpos][2];
407
408 for (color = 0; color < 4; color++) {
409 p = buf + color;
410
411 switch (fh->fmt->fourcc) {
412 case V4L2_PIX_FMT_YUYV:
413 switch (color) {
414 case 0:
415 case 2:
416 *p = r_y;
417 break;
418 case 1:
419 *p = g_u;
420 break;
421 case 3:
422 *p = b_v;
423 break;
424 }
425 break;
426 case V4L2_PIX_FMT_UYVY:
427 switch (color) {
428 case 1:
429 case 3:
430 *p = r_y;
431 break;
432 case 0:
433 *p = g_u;
434 break;
435 case 2:
436 *p = b_v;
437 break;
438 }
439 break;
440 case V4L2_PIX_FMT_RGB565:
441 switch (color) {
442 case 0:
443 case 2:
444 *p = (g_u << 5) | b_v;
445 break;
446 case 1:
447 case 3:
448 *p = (r_y << 3) | (g_u >> 3);
449 break;
450 }
451 break;
452 case V4L2_PIX_FMT_RGB565X:
453 switch (color) {
454 case 0:
455 case 2:
456 *p = (r_y << 3) | (g_u >> 3);
457 break;
458 case 1:
459 case 3:
460 *p = (g_u << 5) | b_v;
461 break;
462 }
463 break;
464 case V4L2_PIX_FMT_RGB555:
465 switch (color) {
466 case 0:
467 case 2:
468 *p = (g_u << 5) | b_v;
469 break;
470 case 1:
471 case 3:
472 *p = (r_y << 2) | (g_u >> 3);
473 break;
474 }
475 break;
476 case V4L2_PIX_FMT_RGB555X:
477 switch (color) {
478 case 0:
479 case 2:
480 *p = (r_y << 2) | (g_u >> 3);
481 break;
482 case 1:
483 case 3:
484 *p = (g_u << 5) | b_v;
485 break;
486 }
487 break;
488 }
489 }
490}
491
492static void gen_line(struct vivi_fh *fh, char *basep, int inipos, int wmax,
493 int hmax, int line, int count, char *timestr)
494{
495 int w, i, j;
496 int pos = inipos;
497 char *s;
498 u8 chr;
499
500
501 wmax /= 2;
502
503
504 for (w = 0; w < wmax; w++) {
505 int colorpos = ((w + count) * 8/(wmax + 1)) % 8;
506
507 gen_twopix(fh, basep + pos, colorpos);
508 pos += 4;
509 }
510
511
512
513
514 if (TSTAMP_MAX_Y >= hmax)
515 goto end;
516
517 if (TSTAMP_INPUT_X + strlen(timestr) >= wmax)
518 goto end;
519
520 if (line >= TSTAMP_MIN_Y && line <= TSTAMP_MAX_Y) {
521 chr = rom8x16_bits[fh->input * 16 + line - TSTAMP_MIN_Y];
522 pos = TSTAMP_INPUT_X;
523 for (i = 0; i < 7; i++) {
524
525 if (chr & 1 << (7 - i))
526 gen_twopix(fh, basep + pos, WHITE);
527 else
528 gen_twopix(fh, basep + pos, BLACK);
529 pos += 2;
530 }
531 }
532
533
534 if (TSTAMP_MIN_X + strlen(timestr) >= wmax)
535 goto end;
536
537
538 if (line >= TSTAMP_MIN_Y && line <= TSTAMP_MAX_Y) {
539 j = TSTAMP_MIN_X;
540 for (s = timestr; *s; s++) {
541 chr = rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
542 for (i = 0; i < 7; i++) {
543 pos = inipos + j * 2;
544
545 if (chr & 1 << (7 - i))
546 gen_twopix(fh, basep + pos, WHITE);
547 else
548 gen_twopix(fh, basep + pos, BLACK);
549 j++;
550 }
551 }
552 }
553
554end:
555 return;
556}
557
558static void vivi_fillbuff(struct vivi_fh *fh, struct vivi_buffer *buf)
559{
560 struct vivi_dev *dev = fh->dev;
561 int h , pos = 0;
562 int hmax = buf->vb.height;
563 int wmax = buf->vb.width;
564 struct timeval ts;
565 char *tmpbuf;
566 void *vbuf = videobuf_to_vmalloc(&buf->vb);
567
568 if (!vbuf)
569 return;
570
571 tmpbuf = kmalloc(wmax * 2, GFP_ATOMIC);
572 if (!tmpbuf)
573 return;
574
575 for (h = 0; h < hmax; h++) {
576 gen_line(fh, tmpbuf, 0, wmax, hmax, h, dev->mv_count,
577 dev->timestr);
578 memcpy(vbuf + pos, tmpbuf, wmax * 2);
579 pos += wmax*2;
580 }
581
582 dev->mv_count++;
583
584 kfree(tmpbuf);
585
586
587
588 dev->ms += jiffies_to_msecs(jiffies-dev->jiffies);
589 dev->jiffies = jiffies;
590 if (dev->ms >= 1000) {
591 dev->ms -= 1000;
592 dev->s++;
593 if (dev->s >= 60) {
594 dev->s -= 60;
595 dev->m++;
596 if (dev->m > 60) {
597 dev->m -= 60;
598 dev->h++;
599 if (dev->h > 24)
600 dev->h -= 24;
601 }
602 }
603 }
604 sprintf(dev->timestr, "%02d:%02d:%02d:%03d",
605 dev->h, dev->m, dev->s, dev->ms);
606
607 dprintk(dev, 2, "vivifill at %s: Buffer 0x%08lx size= %d\n",
608 dev->timestr, (unsigned long)tmpbuf, pos);
609
610
611 buf->vb.field_count++;
612 do_gettimeofday(&ts);
613 buf->vb.ts = ts;
614 buf->vb.state = VIDEOBUF_DONE;
615}
616
617static void vivi_thread_tick(struct vivi_fh *fh)
618{
619 struct vivi_buffer *buf;
620 struct vivi_dev *dev = fh->dev;
621 struct vivi_dmaqueue *dma_q = &dev->vidq;
622
623 unsigned long flags = 0;
624
625 dprintk(dev, 1, "Thread tick\n");
626
627 spin_lock_irqsave(&dev->slock, flags);
628 if (list_empty(&dma_q->active)) {
629 dprintk(dev, 1, "No active queue to serve\n");
630 goto unlock;
631 }
632
633 buf = list_entry(dma_q->active.next,
634 struct vivi_buffer, vb.queue);
635
636
637 if (!waitqueue_active(&buf->vb.done))
638 goto unlock;
639
640 list_del(&buf->vb.queue);
641
642 do_gettimeofday(&buf->vb.ts);
643
644
645 vivi_fillbuff(fh, buf);
646 dprintk(dev, 1, "filled buffer %p\n", buf);
647
648 wake_up(&buf->vb.done);
649 dprintk(dev, 2, "[%p/%d] wakeup\n", buf, buf->vb. i);
650unlock:
651 spin_unlock_irqrestore(&dev->slock, flags);
652 return;
653}
654
655#define frames_to_ms(frames) \
656 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
657
658static void vivi_sleep(struct vivi_fh *fh)
659{
660 struct vivi_dev *dev = fh->dev;
661 struct vivi_dmaqueue *dma_q = &dev->vidq;
662 int timeout;
663 DECLARE_WAITQUEUE(wait, current);
664
665 dprintk(dev, 1, "%s dma_q=0x%08lx\n", __func__,
666 (unsigned long)dma_q);
667
668 add_wait_queue(&dma_q->wq, &wait);
669 if (kthread_should_stop())
670 goto stop_task;
671
672
673 timeout = msecs_to_jiffies(frames_to_ms(1));
674
675 vivi_thread_tick(fh);
676
677 schedule_timeout_interruptible(timeout);
678
679stop_task:
680 remove_wait_queue(&dma_q->wq, &wait);
681 try_to_freeze();
682}
683
684static int vivi_thread(void *data)
685{
686 struct vivi_fh *fh = data;
687 struct vivi_dev *dev = fh->dev;
688
689 dprintk(dev, 1, "thread started\n");
690
691 set_freezable();
692
693 for (;;) {
694 vivi_sleep(fh);
695
696 if (kthread_should_stop())
697 break;
698 }
699 dprintk(dev, 1, "thread: exit\n");
700 return 0;
701}
702
703static int vivi_start_thread(struct vivi_fh *fh)
704{
705 struct vivi_dev *dev = fh->dev;
706 struct vivi_dmaqueue *dma_q = &dev->vidq;
707
708 dma_q->frame = 0;
709 dma_q->ini_jiffies = jiffies;
710
711 dprintk(dev, 1, "%s\n", __func__);
712
713 dma_q->kthread = kthread_run(vivi_thread, fh, "vivi");
714
715 if (IS_ERR(dma_q->kthread)) {
716 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
717 return PTR_ERR(dma_q->kthread);
718 }
719
720 wake_up_interruptible(&dma_q->wq);
721
722 dprintk(dev, 1, "returning from %s\n", __func__);
723 return 0;
724}
725
726static void vivi_stop_thread(struct vivi_dmaqueue *dma_q)
727{
728 struct vivi_dev *dev = container_of(dma_q, struct vivi_dev, vidq);
729
730 dprintk(dev, 1, "%s\n", __func__);
731
732 if (dma_q->kthread) {
733 kthread_stop(dma_q->kthread);
734 dma_q->kthread = NULL;
735 }
736}
737
738
739
740
741static int
742buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
743{
744 struct vivi_fh *fh = vq->priv_data;
745 struct vivi_dev *dev = fh->dev;
746
747 *size = fh->width*fh->height*2;
748
749 if (0 == *count)
750 *count = 32;
751
752 while (*size * *count > vid_limit * 1024 * 1024)
753 (*count)--;
754
755 dprintk(dev, 1, "%s, count=%d, size=%d\n", __func__,
756 *count, *size);
757
758 return 0;
759}
760
761static void free_buffer(struct videobuf_queue *vq, struct vivi_buffer *buf)
762{
763 struct vivi_fh *fh = vq->priv_data;
764 struct vivi_dev *dev = fh->dev;
765
766 dprintk(dev, 1, "%s, state: %i\n", __func__, buf->vb.state);
767
768 if (in_interrupt())
769 BUG();
770
771 videobuf_vmalloc_free(&buf->vb);
772 dprintk(dev, 1, "free_buffer: freed\n");
773 buf->vb.state = VIDEOBUF_NEEDS_INIT;
774}
775
776#define norm_maxw() 1024
777#define norm_maxh() 768
778static int
779buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
780 enum v4l2_field field)
781{
782 struct vivi_fh *fh = vq->priv_data;
783 struct vivi_dev *dev = fh->dev;
784 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
785 int rc;
786
787 dprintk(dev, 1, "%s, field=%d\n", __func__, field);
788
789 BUG_ON(NULL == fh->fmt);
790
791 if (fh->width < 48 || fh->width > norm_maxw() ||
792 fh->height < 32 || fh->height > norm_maxh())
793 return -EINVAL;
794
795 buf->vb.size = fh->width*fh->height*2;
796 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
797 return -EINVAL;
798
799
800 buf->fmt = fh->fmt;
801 buf->vb.width = fh->width;
802 buf->vb.height = fh->height;
803 buf->vb.field = field;
804
805 precalculate_bars(fh);
806
807 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
808 rc = videobuf_iolock(vq, &buf->vb, NULL);
809 if (rc < 0)
810 goto fail;
811 }
812
813 buf->vb.state = VIDEOBUF_PREPARED;
814
815 return 0;
816
817fail:
818 free_buffer(vq, buf);
819 return rc;
820}
821
822static void
823buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
824{
825 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
826 struct vivi_fh *fh = vq->priv_data;
827 struct vivi_dev *dev = fh->dev;
828 struct vivi_dmaqueue *vidq = &dev->vidq;
829
830 dprintk(dev, 1, "%s\n", __func__);
831
832 buf->vb.state = VIDEOBUF_QUEUED;
833 list_add_tail(&buf->vb.queue, &vidq->active);
834}
835
836static void buffer_release(struct videobuf_queue *vq,
837 struct videobuf_buffer *vb)
838{
839 struct vivi_buffer *buf = container_of(vb, struct vivi_buffer, vb);
840 struct vivi_fh *fh = vq->priv_data;
841 struct vivi_dev *dev = (struct vivi_dev *)fh->dev;
842
843 dprintk(dev, 1, "%s\n", __func__);
844
845 free_buffer(vq, buf);
846}
847
848static struct videobuf_queue_ops vivi_video_qops = {
849 .buf_setup = buffer_setup,
850 .buf_prepare = buffer_prepare,
851 .buf_queue = buffer_queue,
852 .buf_release = buffer_release,
853};
854
855
856
857
858static int vidioc_querycap(struct file *file, void *priv,
859 struct v4l2_capability *cap)
860{
861 struct vivi_fh *fh = priv;
862 struct vivi_dev *dev = fh->dev;
863
864 strcpy(cap->driver, "vivi");
865 strcpy(cap->card, "vivi");
866 strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
867 cap->version = VIVI_VERSION;
868 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
869 V4L2_CAP_STREAMING |
870 V4L2_CAP_READWRITE;
871 return 0;
872}
873
874static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
875 struct v4l2_fmtdesc *f)
876{
877 struct vivi_fmt *fmt;
878
879 if (f->index >= ARRAY_SIZE(formats))
880 return -EINVAL;
881
882 fmt = &formats[f->index];
883
884 strlcpy(f->description, fmt->name, sizeof(f->description));
885 f->pixelformat = fmt->fourcc;
886 return 0;
887}
888
889static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
890 struct v4l2_format *f)
891{
892 struct vivi_fh *fh = priv;
893
894 f->fmt.pix.width = fh->width;
895 f->fmt.pix.height = fh->height;
896 f->fmt.pix.field = fh->vb_vidq.field;
897 f->fmt.pix.pixelformat = fh->fmt->fourcc;
898 f->fmt.pix.bytesperline =
899 (f->fmt.pix.width * fh->fmt->depth) >> 3;
900 f->fmt.pix.sizeimage =
901 f->fmt.pix.height * f->fmt.pix.bytesperline;
902
903 return (0);
904}
905
906static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
907 struct v4l2_format *f)
908{
909 struct vivi_fh *fh = priv;
910 struct vivi_dev *dev = fh->dev;
911 struct vivi_fmt *fmt;
912 enum v4l2_field field;
913 unsigned int maxw, maxh;
914
915 fmt = get_format(f);
916 if (!fmt) {
917 dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n",
918 f->fmt.pix.pixelformat);
919 return -EINVAL;
920 }
921
922 field = f->fmt.pix.field;
923
924 if (field == V4L2_FIELD_ANY) {
925 field = V4L2_FIELD_INTERLACED;
926 } else if (V4L2_FIELD_INTERLACED != field) {
927 dprintk(dev, 1, "Field type invalid.\n");
928 return -EINVAL;
929 }
930
931 maxw = norm_maxw();
932 maxh = norm_maxh();
933
934 f->fmt.pix.field = field;
935 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
936 &f->fmt.pix.height, 32, maxh, 0, 0);
937 f->fmt.pix.bytesperline =
938 (f->fmt.pix.width * fmt->depth) >> 3;
939 f->fmt.pix.sizeimage =
940 f->fmt.pix.height * f->fmt.pix.bytesperline;
941
942 return 0;
943}
944
945
946static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
947 struct v4l2_format *f)
948{
949 struct vivi_fh *fh = priv;
950 struct videobuf_queue *q = &fh->vb_vidq;
951
952 int ret = vidioc_try_fmt_vid_cap(file, fh, f);
953 if (ret < 0)
954 return ret;
955
956 mutex_lock(&q->vb_lock);
957
958 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
959 dprintk(fh->dev, 1, "%s queue busy\n", __func__);
960 ret = -EBUSY;
961 goto out;
962 }
963
964 fh->fmt = get_format(f);
965 fh->width = f->fmt.pix.width;
966 fh->height = f->fmt.pix.height;
967 fh->vb_vidq.field = f->fmt.pix.field;
968 fh->type = f->type;
969
970 ret = 0;
971out:
972 mutex_unlock(&q->vb_lock);
973
974 return ret;
975}
976
977static int vidioc_reqbufs(struct file *file, void *priv,
978 struct v4l2_requestbuffers *p)
979{
980 struct vivi_fh *fh = priv;
981
982 return (videobuf_reqbufs(&fh->vb_vidq, p));
983}
984
985static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
986{
987 struct vivi_fh *fh = priv;
988
989 return (videobuf_querybuf(&fh->vb_vidq, p));
990}
991
992static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
993{
994 struct vivi_fh *fh = priv;
995
996 return (videobuf_qbuf(&fh->vb_vidq, p));
997}
998
999static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1000{
1001 struct vivi_fh *fh = priv;
1002
1003 return (videobuf_dqbuf(&fh->vb_vidq, p,
1004 file->f_flags & O_NONBLOCK));
1005}
1006
1007#ifdef CONFIG_VIDEO_V4L1_COMPAT
1008static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1009{
1010 struct vivi_fh *fh = priv;
1011
1012 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1013}
1014#endif
1015
1016static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1017{
1018 struct vivi_fh *fh = priv;
1019
1020 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1021 return -EINVAL;
1022 if (i != fh->type)
1023 return -EINVAL;
1024
1025 return videobuf_streamon(&fh->vb_vidq);
1026}
1027
1028static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1029{
1030 struct vivi_fh *fh = priv;
1031
1032 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1033 return -EINVAL;
1034 if (i != fh->type)
1035 return -EINVAL;
1036
1037 return videobuf_streamoff(&fh->vb_vidq);
1038}
1039
1040static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *i)
1041{
1042 return 0;
1043}
1044
1045
1046static int vidioc_enum_input(struct file *file, void *priv,
1047 struct v4l2_input *inp)
1048{
1049 if (inp->index >= NUM_INPUTS)
1050 return -EINVAL;
1051
1052 inp->type = V4L2_INPUT_TYPE_CAMERA;
1053 inp->std = V4L2_STD_525_60;
1054 sprintf(inp->name, "Camera %u", inp->index);
1055
1056 return (0);
1057}
1058
1059static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1060{
1061 struct vivi_fh *fh = priv;
1062 struct vivi_dev *dev = fh->dev;
1063
1064 *i = dev->input;
1065
1066 return (0);
1067}
1068static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1069{
1070 struct vivi_fh *fh = priv;
1071 struct vivi_dev *dev = fh->dev;
1072
1073 if (i >= NUM_INPUTS)
1074 return -EINVAL;
1075
1076 dev->input = i;
1077 precalculate_bars(fh);
1078
1079 return (0);
1080}
1081
1082
1083static int vidioc_queryctrl(struct file *file, void *priv,
1084 struct v4l2_queryctrl *qc)
1085{
1086 int i;
1087
1088 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1089 if (qc->id && qc->id == vivi_qctrl[i].id) {
1090 memcpy(qc, &(vivi_qctrl[i]),
1091 sizeof(*qc));
1092 return (0);
1093 }
1094
1095 return -EINVAL;
1096}
1097
1098static int vidioc_g_ctrl(struct file *file, void *priv,
1099 struct v4l2_control *ctrl)
1100{
1101 struct vivi_fh *fh = priv;
1102 struct vivi_dev *dev = fh->dev;
1103 int i;
1104
1105 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1106 if (ctrl->id == vivi_qctrl[i].id) {
1107 ctrl->value = dev->qctl_regs[i];
1108 return 0;
1109 }
1110
1111 return -EINVAL;
1112}
1113static int vidioc_s_ctrl(struct file *file, void *priv,
1114 struct v4l2_control *ctrl)
1115{
1116 struct vivi_fh *fh = priv;
1117 struct vivi_dev *dev = fh->dev;
1118 int i;
1119
1120 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1121 if (ctrl->id == vivi_qctrl[i].id) {
1122 if (ctrl->value < vivi_qctrl[i].minimum ||
1123 ctrl->value > vivi_qctrl[i].maximum) {
1124 return -ERANGE;
1125 }
1126 dev->qctl_regs[i] = ctrl->value;
1127 return 0;
1128 }
1129 return -EINVAL;
1130}
1131
1132
1133
1134
1135
1136static int vivi_open(struct file *file)
1137{
1138 struct vivi_dev *dev = video_drvdata(file);
1139 struct vivi_fh *fh = NULL;
1140 int retval = 0;
1141
1142 mutex_lock(&dev->mutex);
1143 dev->users++;
1144
1145 if (dev->users > 1) {
1146 dev->users--;
1147 mutex_unlock(&dev->mutex);
1148 return -EBUSY;
1149 }
1150
1151 dprintk(dev, 1, "open /dev/video%d type=%s users=%d\n", dev->vfd->num,
1152 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1153
1154
1155 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1156 if (NULL == fh) {
1157 dev->users--;
1158 retval = -ENOMEM;
1159 }
1160 mutex_unlock(&dev->mutex);
1161
1162 if (retval)
1163 return retval;
1164
1165 file->private_data = fh;
1166 fh->dev = dev;
1167
1168 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1169 fh->fmt = &formats[0];
1170 fh->width = 640;
1171 fh->height = 480;
1172
1173
1174 dev->h = 0;
1175 dev->m = 0;
1176 dev->s = 0;
1177 dev->ms = 0;
1178 dev->mv_count = 0;
1179 dev->jiffies = jiffies;
1180 sprintf(dev->timestr, "%02d:%02d:%02d:%03d",
1181 dev->h, dev->m, dev->s, dev->ms);
1182
1183 videobuf_queue_vmalloc_init(&fh->vb_vidq, &vivi_video_qops,
1184 NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
1185 sizeof(struct vivi_buffer), fh);
1186
1187 vivi_start_thread(fh);
1188
1189 return 0;
1190}
1191
1192static ssize_t
1193vivi_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1194{
1195 struct vivi_fh *fh = file->private_data;
1196
1197 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1198 return videobuf_read_stream(&fh->vb_vidq, data, count, ppos, 0,
1199 file->f_flags & O_NONBLOCK);
1200 }
1201 return 0;
1202}
1203
1204static unsigned int
1205vivi_poll(struct file *file, struct poll_table_struct *wait)
1206{
1207 struct vivi_fh *fh = file->private_data;
1208 struct vivi_dev *dev = fh->dev;
1209 struct videobuf_queue *q = &fh->vb_vidq;
1210
1211 dprintk(dev, 1, "%s\n", __func__);
1212
1213 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1214 return POLLERR;
1215
1216 return videobuf_poll_stream(file, q, wait);
1217}
1218
1219static int vivi_close(struct file *file)
1220{
1221 struct vivi_fh *fh = file->private_data;
1222 struct vivi_dev *dev = fh->dev;
1223 struct vivi_dmaqueue *vidq = &dev->vidq;
1224
1225 int minor = video_devdata(file)->minor;
1226
1227 vivi_stop_thread(vidq);
1228 videobuf_stop(&fh->vb_vidq);
1229 videobuf_mmap_free(&fh->vb_vidq);
1230
1231 kfree(fh);
1232
1233 mutex_lock(&dev->mutex);
1234 dev->users--;
1235 mutex_unlock(&dev->mutex);
1236
1237 dprintk(dev, 1, "close called (minor=%d, users=%d)\n",
1238 minor, dev->users);
1239
1240 return 0;
1241}
1242
1243static int vivi_mmap(struct file *file, struct vm_area_struct *vma)
1244{
1245 struct vivi_fh *fh = file->private_data;
1246 struct vivi_dev *dev = fh->dev;
1247 int ret;
1248
1249 dprintk(dev, 1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1250
1251 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1252
1253 dprintk(dev, 1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1254 (unsigned long)vma->vm_start,
1255 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1256 ret);
1257
1258 return ret;
1259}
1260
1261static const struct v4l2_file_operations vivi_fops = {
1262 .owner = THIS_MODULE,
1263 .open = vivi_open,
1264 .release = vivi_close,
1265 .read = vivi_read,
1266 .poll = vivi_poll,
1267 .ioctl = video_ioctl2,
1268 .mmap = vivi_mmap,
1269};
1270
1271static const struct v4l2_ioctl_ops vivi_ioctl_ops = {
1272 .vidioc_querycap = vidioc_querycap,
1273 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1274 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1275 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1276 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1277 .vidioc_reqbufs = vidioc_reqbufs,
1278 .vidioc_querybuf = vidioc_querybuf,
1279 .vidioc_qbuf = vidioc_qbuf,
1280 .vidioc_dqbuf = vidioc_dqbuf,
1281 .vidioc_s_std = vidioc_s_std,
1282 .vidioc_enum_input = vidioc_enum_input,
1283 .vidioc_g_input = vidioc_g_input,
1284 .vidioc_s_input = vidioc_s_input,
1285 .vidioc_queryctrl = vidioc_queryctrl,
1286 .vidioc_g_ctrl = vidioc_g_ctrl,
1287 .vidioc_s_ctrl = vidioc_s_ctrl,
1288 .vidioc_streamon = vidioc_streamon,
1289 .vidioc_streamoff = vidioc_streamoff,
1290#ifdef CONFIG_VIDEO_V4L1_COMPAT
1291 .vidiocgmbuf = vidiocgmbuf,
1292#endif
1293};
1294
1295static struct video_device vivi_template = {
1296 .name = "vivi",
1297 .fops = &vivi_fops,
1298 .ioctl_ops = &vivi_ioctl_ops,
1299 .minor = -1,
1300 .release = video_device_release,
1301
1302 .tvnorms = V4L2_STD_525_60,
1303 .current_norm = V4L2_STD_NTSC_M,
1304};
1305
1306
1307
1308
1309
1310static int vivi_release(void)
1311{
1312 struct vivi_dev *dev;
1313 struct list_head *list;
1314
1315 while (!list_empty(&vivi_devlist)) {
1316 list = vivi_devlist.next;
1317 list_del(list);
1318 dev = list_entry(list, struct vivi_dev, vivi_devlist);
1319
1320 v4l2_info(&dev->v4l2_dev, "unregistering /dev/video%d\n",
1321 dev->vfd->num);
1322 video_unregister_device(dev->vfd);
1323 v4l2_device_unregister(&dev->v4l2_dev);
1324 kfree(dev);
1325 }
1326
1327 return 0;
1328}
1329
1330static int __init vivi_create_instance(int inst)
1331{
1332 struct vivi_dev *dev;
1333 struct video_device *vfd;
1334 int ret, i;
1335
1336 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1337 if (!dev)
1338 return -ENOMEM;
1339
1340 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1341 "%s-%03d", VIVI_MODULE_NAME, inst);
1342 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
1343 if (ret)
1344 goto free_dev;
1345
1346
1347 INIT_LIST_HEAD(&dev->vidq.active);
1348 init_waitqueue_head(&dev->vidq.wq);
1349
1350
1351 spin_lock_init(&dev->slock);
1352 mutex_init(&dev->mutex);
1353
1354 ret = -ENOMEM;
1355 vfd = video_device_alloc();
1356 if (!vfd)
1357 goto unreg_dev;
1358
1359 *vfd = vivi_template;
1360 vfd->debug = debug;
1361
1362 ret = video_register_device(vfd, VFL_TYPE_GRABBER, video_nr);
1363 if (ret < 0)
1364 goto rel_vdev;
1365
1366 video_set_drvdata(vfd, dev);
1367
1368
1369 for (i = 0; i < ARRAY_SIZE(vivi_qctrl); i++)
1370 dev->qctl_regs[i] = vivi_qctrl[i].default_value;
1371
1372
1373 list_add_tail(&dev->vivi_devlist, &vivi_devlist);
1374
1375 snprintf(vfd->name, sizeof(vfd->name), "%s (%i)",
1376 vivi_template.name, vfd->num);
1377
1378 if (video_nr >= 0)
1379 video_nr++;
1380
1381 dev->vfd = vfd;
1382 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as /dev/video%d\n",
1383 vfd->num);
1384 return 0;
1385
1386rel_vdev:
1387 video_device_release(vfd);
1388unreg_dev:
1389 v4l2_device_unregister(&dev->v4l2_dev);
1390free_dev:
1391 kfree(dev);
1392 return ret;
1393}
1394
1395
1396
1397
1398
1399
1400
1401static int __init vivi_init(void)
1402{
1403 int ret = 0, i;
1404
1405 if (n_devs <= 0)
1406 n_devs = 1;
1407
1408 for (i = 0; i < n_devs; i++) {
1409 ret = vivi_create_instance(i);
1410 if (ret) {
1411
1412 if (i)
1413 ret = 0;
1414 break;
1415 }
1416 }
1417
1418 if (ret < 0) {
1419 printk(KERN_INFO "Error %d while loading vivi driver\n", ret);
1420 return ret;
1421 }
1422
1423 printk(KERN_INFO "Video Technology Magazine Virtual Video "
1424 "Capture Board ver %u.%u.%u successfully loaded.\n",
1425 (VIVI_VERSION >> 16) & 0xFF, (VIVI_VERSION >> 8) & 0xFF,
1426 VIVI_VERSION & 0xFF);
1427
1428
1429 n_devs = i;
1430
1431 return ret;
1432}
1433
1434static void __exit vivi_exit(void)
1435{
1436 vivi_release();
1437}
1438
1439module_init(vivi_init);
1440module_exit(vivi_exit);
1441