18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
26 #include <linux/videodev2.h>
37 #define VIVI_MODULE_NAME "vivi"
40 #define WAKE_NUMERATOR 30
41 #define WAKE_DENOMINATOR 1001
42 #define BUFFER_TIMEOUT msecs_to_jiffies(500)
44 #define MAX_WIDTH 1920
45 #define MAX_HEIGHT 1200
47 #define VIVI_VERSION "0.8.1"
50 MODULE_AUTHOR(
"Mauro Carvalho Chehab, Ted Walther and John Sokol");
54 static unsigned video_nr = -1;
58 static unsigned n_devs = 1;
62 static unsigned debug;
71 static const u8 *font8x16;
73 #define dprintk(dev, level, fmt, arg...) \
74 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ## arg)
89 .name =
"4:2:2, packed, YUYV",
95 .name =
"4:2:2, packed, UYVY",
101 .name =
"4:2:2, packed, YVYU",
107 .name =
"4:2:2, packed, VYUY",
113 .name =
"RGB565 (LE)",
118 .name =
"RGB565 (BE)",
123 .name =
"RGB555 (LE)",
128 .name =
"RGB555 (BE)",
133 .name =
"RGB24 (LE)",
138 .name =
"RGB24 (BE)",
143 .name =
"RGB32 (LE)",
148 .name =
"RGB32 (BE)",
265 #define COLOR_WHITE {204, 204, 204}
266 #define COLOR_AMBER {208, 208, 0}
267 #define COLOR_CYAN { 0, 206, 206}
268 #define COLOR_GREEN { 0, 239, 0}
269 #define COLOR_MAGENTA {239, 0, 239}
270 #define COLOR_RED {205, 0, 0}
271 #define COLOR_BLUE { 0, 0, 255}
272 #define COLOR_BLACK { 0, 0, 0}
280 static struct bar_std bars[] = {
296 #define NUM_INPUTS ARRAY_SIZE(bars)
298 #define TO_Y(r, g, b) \
299 (((16829 * r + 33039 * g + 6416 * b + 32768) >> 16) + 16)
301 #define TO_V(r, g, b) \
302 (((28784 * r - 24103 * g - 4681 * b + 32768) >> 16) + 128)
304 #define TO_U(r, g, b) \
305 (((-9714 * r - 19070 * g + 28784 * b + 32768) >> 16) + 128)
308 static void precalculate_bars(
struct vivi_dev *
dev)
313 for (k = 0; k < 9; k++) {
317 is_yuv = dev->
fmt->is_yuv;
319 switch (dev->
fmt->fourcc) {
355 #define TSTAMP_MIN_Y 24
356 #define TSTAMP_MAX_Y (TSTAMP_MIN_Y + 15)
357 #define TSTAMP_INPUT_X 10
358 #define TSTAMP_MIN_X (54 + TSTAMP_INPUT_X)
361 static void gen_twopix(
struct vivi_dev *
dev,
u8 *
buf,
int colorpos,
bool odd)
368 r_y = dev->
bars[colorpos][0];
369 g_u = dev->
bars[colorpos][1];
370 b_v = dev->
bars[colorpos][2];
372 for (color = 0; color < dev->
pixelsize; color++) {
375 switch (dev->
fmt->fourcc) {
382 *p = odd ? b_v : g_u;
389 *p = odd ? b_v : g_u;
402 *p = odd ? g_u : b_v;
409 *p = odd ? g_u : b_v;
419 *p = (g_u << 5) | b_v;
422 *p = (r_y << 3) | (g_u >> 3);
429 *p = (r_y << 3) | (g_u >> 3);
432 *p = (g_u << 5) | b_v;
439 *p = (g_u << 5) | b_v;
442 *p = (alpha & 0x80) | (r_y << 2) | (g_u >> 3);
449 *p = (alpha & 0x80) | (r_y << 2) | (g_u >> 3);
452 *p = (g_u << 5) | b_v;
518 static void precalculate_line(
struct vivi_dev *
dev)
522 for (w = 0; w < dev->
width * 2; w++) {
523 int colorpos = w / (dev->
width / 8) % 8;
525 gen_twopix(dev, dev->
line + w * dev->
pixelsize, colorpos, w & 1);
529 static void gen_text(
struct vivi_dev *dev,
char *basep,
539 for (line = y; line < y + 16; line++) {
544 for (s = text; *
s; s++) {
545 u8 chr = font8x16[*s * 16 + line -
y];
548 for (i = 0; i < 7; i++, j++) {
550 if (chr & (1 << (7 - i)))
561 int wmax = dev->
width;
573 for (h = 0; h < hmax; h++)
583 snprintf(str,
sizeof(str),
" %02d:%02d:%02d:%03d ",
584 (ms / (60 * 60 * 1000)) % 24,
585 (ms / (60 * 1000)) % 60,
588 gen_text(dev, vbuf, line++ * 16, 16, str);
589 snprintf(str,
sizeof(str),
" %dx%d, input %d ",
591 gen_text(dev, vbuf, line++ * 16, 16, str);
595 snprintf(str,
sizeof(str),
" brightness %3d, contrast %3d, saturation %3d, hue %d ",
600 gen_text(dev, vbuf, line++ * 16, 16, str);
601 snprintf(str,
sizeof(str),
" autogain %d, gain %3d, volume %3d, alpha 0x%02x ",
603 dev->
alpha->cur.val);
604 gen_text(dev, vbuf, line++ * 16, 16, str);
605 snprintf(str,
sizeof(str),
" int32 %d, int64 %lld, bitmask %08x ",
607 dev->
int64->cur.val64,
609 gen_text(dev, vbuf, line++ * 16, 16, str);
610 snprintf(str,
sizeof(str),
" boolean %d, menu %s, string \"%s\" ",
612 dev->
menu->qmenu[dev->
menu->cur.val],
614 gen_text(dev, vbuf, line++ * 16, 16, str);
615 snprintf(str,
sizeof(str),
" integer_menu %lld, value %d ",
618 gen_text(dev, vbuf, line++ * 16, 16, str);
622 snprintf(str,
sizeof(str),
" button pressed!");
623 gen_text(dev, vbuf, line++ * 16, 16, str);
632 buf->
vb.v4l2_buf.timestamp =
ts;
635 static void vivi_thread_tick(
struct vivi_dev *dev)
639 unsigned long flags = 0;
641 dprintk(dev, 1,
"Thread tick\n");
644 if (list_empty(&dma_q->
active)) {
645 dprintk(dev, 1,
"No active queue to serve\n");
646 spin_unlock_irqrestore(&dev->
slock, flags);
652 spin_unlock_irqrestore(&dev->
slock, flags);
657 vivi_fillbuff(dev, buf);
658 dprintk(dev, 1,
"filled buffer %p\n", buf);
661 dprintk(dev, 2,
"[%p/%d] done\n", buf, buf->
vb.v4l2_buf.index);
664 #define frames_to_ms(frames) \
665 ((frames * WAKE_NUMERATOR * 1000) / WAKE_DENOMINATOR)
667 static void vivi_sleep(
struct vivi_dev *dev)
673 dprintk(dev, 1,
"%s dma_q=0x%08lx\n", __func__,
674 (
unsigned long)dma_q);
683 vivi_thread_tick(dev);
692 static int vivi_thread(
void *
data)
696 dprintk(dev, 1,
"thread started\n");
706 dprintk(dev, 1,
"thread: exit\n");
710 static int vivi_start_generating(
struct vivi_dev *dev)
714 dprintk(dev, 1,
"%s\n", __func__);
727 return PTR_ERR(dma_q->
kthread);
732 dprintk(dev, 1,
"returning from %s\n", __func__);
736 static void vivi_stop_generating(
struct vivi_dev *dev)
740 dprintk(dev, 1,
"%s\n", __func__);
754 while (!list_empty(&dma_q->
active)) {
759 dprintk(dev, 2,
"[%p/%d] done\n", buf, buf->
vb.v4l2_buf.index);
766 unsigned int *nbuffers,
unsigned int *nplanes,
767 unsigned int sizes[],
void *alloc_ctxs[])
769 struct vivi_dev *dev = vb2_get_drv_priv(vq);
773 size = fmt->
fmt.
pix.sizeimage;
783 while (size * *nbuffers > vid_limit * 1024 * 1024)
795 dprintk(dev, 1,
"%s, count=%d, size=%ld\n", __func__,
801 static int buffer_prepare(
struct vb2_buffer *vb)
822 if (vb2_plane_size(vb, 0) < size) {
823 dprintk(dev, 1,
"%s data will not fit into plane (%lu < %lu)\n",
824 __func__, vb2_plane_size(vb, 0), size);
828 vb2_set_plane_payload(&buf->
vb, 0, size);
832 precalculate_bars(dev);
833 precalculate_line(dev);
838 static void buffer_queue(
struct vb2_buffer *vb)
843 unsigned long flags = 0;
845 dprintk(dev, 1,
"%s\n", __func__);
849 spin_unlock_irqrestore(&dev->
slock, flags);
852 static int start_streaming(
struct vb2_queue *vq,
unsigned int count)
854 struct vivi_dev *dev = vb2_get_drv_priv(vq);
855 dprintk(dev, 1,
"%s\n", __func__);
856 return vivi_start_generating(dev);
860 static int stop_streaming(
struct vb2_queue *vq)
862 struct vivi_dev *dev = vb2_get_drv_priv(vq);
863 dprintk(dev, 1,
"%s\n", __func__);
864 vivi_stop_generating(dev);
868 static void vivi_lock(
struct vb2_queue *vq)
870 struct vivi_dev *dev = vb2_get_drv_priv(vq);
874 static void vivi_unlock(
struct vb2_queue *vq)
876 struct vivi_dev *dev = vb2_get_drv_priv(vq);
881 static struct vb2_ops vivi_video_qops = {
883 .buf_prepare = buffer_prepare,
884 .buf_queue = buffer_queue,
887 .wait_prepare = vivi_unlock,
888 .wait_finish = vivi_lock,
894 static int vidioc_querycap(
struct file *
file,
void *
priv,
897 struct vivi_dev *dev = video_drvdata(file);
909 static int vidioc_enum_fmt_vid_cap(
struct file *
file,
void *
priv,
917 fmt = &formats[f->
index];
924 static int vidioc_g_fmt_vid_cap(
struct file *file,
void *priv,
927 struct vivi_dev *dev = video_drvdata(file);
932 f->
fmt.
pix.pixelformat = dev->
fmt->fourcc;
934 (f->
fmt.
pix.width * dev->
fmt->depth) >> 3;
937 if (dev->
fmt->is_yuv)
944 static int vidioc_try_fmt_vid_cap(
struct file *file,
void *priv,
947 struct vivi_dev *dev = video_drvdata(file);
952 dprintk(dev, 1,
"Fourcc format (0x%08x) unknown.\n",
973 static int vidioc_s_fmt_vid_cap(
struct file *file,
void *priv,
976 struct vivi_dev *dev = video_drvdata(file);
979 int ret = vidioc_try_fmt_vid_cap(file, priv, f);
983 if (vb2_is_busy(q)) {
984 dprintk(dev, 1,
"%s device busy\n", __func__);
996 static int vidioc_enum_framesizes(
struct file *file,
void *fh,
1018 static int vidioc_enum_input(
struct file *file,
void *priv,
1029 static int vidioc_g_input(
struct file *file,
void *priv,
unsigned int *i)
1031 struct vivi_dev *dev = video_drvdata(file);
1037 static int vidioc_s_input(
struct file *file,
void *priv,
unsigned int i)
1039 struct vivi_dev *dev = video_drvdata(file);
1044 if (i == dev->
input)
1048 precalculate_bars(dev);
1049 precalculate_line(dev);
1064 static int vivi_s_ctrl(
struct v4l2_ctrl *ctrl)
1085 .g_volatile_ctrl = vivi_g_volatile_ctrl,
1086 .s_ctrl = vivi_s_ctrl,
1089 #define VIVI_CID_CUSTOM_BASE (V4L2_CID_USER_BASE | 0xf000)
1092 .ops = &vivi_ctrl_ops,
1099 .ops = &vivi_ctrl_ops,
1110 .ops = &vivi_ctrl_ops,
1112 .name =
"Integer 32 Bits",
1120 .ops = &vivi_ctrl_ops,
1122 .name =
"Integer 64 Bits",
1126 static const char *
const vivi_ctrl_menu_strings[] = {
1127 "Menu Item 0 (Skipped)",
1129 "Menu Item 2 (Skipped)",
1132 "Menu Item 5 (Skipped)",
1137 .ops = &vivi_ctrl_ops,
1144 .menu_skip_mask = 0x04,
1145 .qmenu = vivi_ctrl_menu_strings,
1149 .ops = &vivi_ctrl_ops,
1159 .ops = &vivi_ctrl_ops,
1169 static const s64 vivi_ctrl_int_menu_values[] = {
1170 1, 1, 2, 3, 5, 8, 13, 21, 42,
1174 .ops = &vivi_ctrl_ops,
1176 .name =
"Integer menu",
1181 .menu_skip_mask = 0x02,
1182 .qmenu_int = vivi_ctrl_int_menu_values,
1221 .ioctl_ops = &vivi_ioctl_ops,
1229 static int vivi_release(
void)
1234 while (!list_empty(&vivi_devlist)) {
1235 list = vivi_devlist.
next;
1240 video_device_node_name(&dev->
vdev));
1250 static int __init vivi_create_instance(
int inst)
1268 dev->
fmt = &formats[0];
1314 q->
ops = &vivi_video_qops;
1324 INIT_LIST_HEAD(&dev->
vidq.active);
1328 *vfd = vivi_template;
1339 video_set_drvdata(vfd, dev);
1349 video_device_node_name(vfd));
1366 static int __init vivi_init(
void)
1375 font8x16 = font->
data;
1380 for (i = 0; i < n_devs; i++) {
1381 ret = vivi_create_instance(i);
1396 "Capture Board ver %s successfully loaded.\n",
1405 static void __exit vivi_exit(
void)