23 #include <linux/list.h>
24 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
32 #include <asm/div64.h>
60 static unsigned int video_debug;
64 static unsigned int irq_debug;
68 static unsigned int vid_limit = 16;
72 #define dprintk(level, fmt, arg...)\
73 do { if (video_debug >= level)\
74 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
80 #define FORMAT_FLAGS_PACKED 0x01
84 .name =
"8 bpp, gray",
89 .name =
"15 bpp RGB, le",
94 .name =
"15 bpp RGB, be",
99 .name =
"16 bpp RGB, le",
104 .name =
"16 bpp RGB, be",
109 .name =
"24 bpp RGB, le",
114 .name =
"32 bpp RGB, le",
119 .name =
"32 bpp RGB, be",
124 .name =
"4:2:2, packed, YUYV",
129 .name =
"4:2:2, packed, UYVY",
139 .
name =
"4:2:2, packed, UYVY",
145 .name =
"4:2:2, packed, YUYV",
158 if (formats[i].fourcc == fourcc)
163 ((fourcc >> 8) & 0xff),
164 ((fourcc >> 16) & 0xff),
165 ((fourcc >> 24) & 0xff)
182 .name =
"Brightness",
186 .default_value = 0x7f,
200 .default_value = 0x3f,
214 .default_value = 0x0,
227 .name =
"Saturation",
231 .default_value = 0x3f,
249 .mask = (0x1f << 24),
258 .default_value = 65535,
266 static const int CX23885_CTLS =
ARRAY_SIZE(cx23885_ctls);
269 static const u32 cx23885_user_ctrls[] = {
280 static const u32 *ctrl_classes[] = {
291 for (bc = 0;; bc++) {
292 if (list_empty(&q->
active))
304 dprintk(2,
"[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->
vb.i,
310 if (list_empty(&q->
active))
321 dprintk(1,
"%s(norm = 0x%08x) name: [%s]\n",
339 dprintk(1,
"%s()\n", __func__);
349 video_set_drvdata(vfd, dev);
360 for (i = 0; i < CX23885_CTLS; i++)
361 if (cx23885_ctls[i].
v.id == qctrl->
id)
363 if (i == CX23885_CTLS) {
367 *qctrl = cx23885_ctls[
i].
v;
377 dprintk(1,
"%s()\n", __func__);
392 dprintk(1,
"res: get %d\n", bit);
397 static int res_check(
struct cx23885_fh *fh,
unsigned int bit)
402 static int res_locked(
struct cx23885_dev *dev,
unsigned int bit)
411 dprintk(1,
"%s()\n", __func__);
416 dprintk(1,
"res: put %d\n", bits);
426 .flags = 0, .buf =
buf, .len = 2 };
439 { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
440 { .addr = 0x98 >> 1, .flags =
I2C_M_RD, .buf = b1, .len = 1 }
450 static void cx23885_flatiron_dump(
struct cx23885_dev *dev)
454 for (i = 0; i < 0x24; i++) {
455 dprintk(1,
"FI[%02x] = %02x\n", i,
456 cx23885_flatiron_read(dev, i));
463 dprintk(1,
"%s(input = %d)\n", __func__, input);
480 cx23885_flatiron_dump(dev);
485 static int cx23885_video_mux(
struct cx23885_dev *dev,
unsigned int input)
487 dprintk(1,
"%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
489 input,
INPUT(input)->vmux,
504 INPUT(input)->vmux, 0, 0);
515 INPUT(input)->amux, 0, 0);
518 cx23885_flatiron_mux(dev, 1);
520 cx23885_flatiron_mux(dev, 2);
526 static int cx23885_audio_mux(
struct cx23885_dev *dev,
unsigned int input)
528 dprintk(1,
"%s(input=%d)\n", __func__, input);
540 cx23885_flatiron_mux(dev, 1);
542 cx23885_flatiron_mux(dev, 2);
545 cx23885_flatiron_mux(dev, 1);
552 static int cx23885_start_video_dma(
struct cx23885_dev *dev,
556 dprintk(1,
"%s()\n", __func__);
581 static int cx23885_restart_video_queue(
struct cx23885_dev *dev,
586 dprintk(1,
"%s()\n", __func__);
588 if (!list_empty(&q->
active)) {
591 dprintk(2,
"restart_queue [%p/%d]: restart dma\n",
593 cx23885_start_video_dma(dev, q, buf);
605 if (list_empty(&q->
queued))
610 list_move_tail(&buf->
vb.queue, &q->
active);
611 cx23885_start_video_dma(dev, q, buf);
615 dprintk(2,
"[%p/%d] restart_queue - first active\n",
618 }
else if (prev->
vb.width == buf->
vb.width &&
619 prev->
vb.height == buf->
vb.height &&
621 list_move_tail(&buf->
vb.queue, &q->
active);
626 dprintk(2,
"[%p/%d] restart_queue - move to active\n",
643 if (*size * *count > vid_limit * 1024 * 1024)
644 *count = (vid_limit * 1024 * 1024) / *size;
656 u32 line0_offset, line1_offset;
665 if (0 != buf->
vb.baddr && buf->
vb.bsize < buf->
vb.size)
668 if (buf->
fmt != fh->
fmt ||
671 buf->
vb.field != field) {
687 buf->
bpl = buf->
vb.width * buf->
fmt->depth >> 3;
688 switch (buf->
vb.field) {
692 buf->
bpl, 0, buf->
vb.height);
697 buf->
bpl, 0, buf->
vb.height);
712 dprintk(1,
"%s() Creating TFF/NTSC risc\n",
714 line0_offset = buf->
bpl;
718 dprintk(1,
"%s() Creating BFF/PAL/SECAM risc\n",
721 line1_offset = buf->
bpl;
724 dma->
sglist, line0_offset,
727 buf->
vb.height >> 1);
732 0, buf->
bpl * (buf->
vb.height >> 1),
734 buf->
vb.height >> 1);
739 buf->
bpl * (buf->
vb.height >> 1), 0,
741 buf->
vb.height >> 1);
747 dprintk(2,
"[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
750 (
unsigned long)buf->
risc.dma);
774 if (!list_empty(&q->
queued)) {
777 dprintk(2,
"[%p/%d] buffer_queue - append to queued\n",
780 }
else if (list_empty(&q->
active)) {
782 cx23885_start_video_dma(dev, q, buf);
786 dprintk(2,
"[%p/%d] buffer_queue - first active\n",
792 if (prev->
vb.width == buf->
vb.width &&
793 prev->
vb.height == buf->
vb.height &&
801 dprintk(2,
"[%p/%d] buffer_queue - append to active\n",
807 dprintk(2,
"[%p/%d] buffer_queue - first queued\n",
823 .buf_setup = buffer_setup,
824 .buf_prepare = buffer_prepare,
825 .buf_queue = buffer_queue,
826 .buf_release = buffer_release,
842 static int get_resource(
struct cx23885_fh *fh)
855 static int video_open(
struct file *
file)
875 dprintk(1,
"open dev=%s radio=%d type=%s\n",
906 dprintk(1,
"post videobuf_queue_init()\n");
911 static ssize_t video_read(
struct file *file,
char __user *
data,
912 size_t count, loff_t *ppos)
933 static unsigned int video_poll(
struct file *file,
949 if (list_empty(&fh->
vidq.stream))
959 poll_wait(file, &buf->
vb.done, wait);
970 static int video_release(
struct file *file)
986 if (fh->
vidq.read_buf) {
987 buffer_release(&fh->
vidq, fh->
vidq.read_buf);
993 if (fh->
vbiq.streaming)
995 if (fh->
vbiq.reading)
1014 static int video_mmap(
struct file *file,
struct vm_area_struct *vma)
1027 dprintk(1,
"%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
1035 dprintk(1,
"%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
1041 static void init_controls(
struct cx23885_dev *dev)
1046 for (i = 0; i < CX23885_CTLS; i++) {
1047 ctrl.id = cx23885_ctls[
i].
v.id;
1048 ctrl.value = cx23885_ctls[
i].
v.default_value;
1057 static int vidioc_g_fmt_vid_cap(
struct file *file,
void *
priv,
1065 f->
fmt.
pix.pixelformat = fh->
fmt->fourcc;
1066 f->
fmt.
pix.bytesperline =
1067 (f->
fmt.
pix.width * fh->
fmt->depth) >> 3;
1074 static int vidioc_try_fmt_vid_cap(
struct file *file,
void *
priv,
1080 unsigned int maxw, maxh;
1086 field = f->
fmt.
pix.field;
1091 field = (f->
fmt.
pix.height > maxh/2)
1109 &f->
fmt.
pix.height, 32, maxh, 0, 0);
1110 f->
fmt.
pix.bytesperline =
1118 static int vidioc_s_fmt_vid_cap(
struct file *file,
void *priv,
1123 struct v4l2_mbus_framefmt mbus_fmt;
1126 dprintk(2,
"%s()\n", __func__);
1127 err = vidioc_try_fmt_vid_cap(file, priv, f);
1135 dprintk(2,
"%s() width=%d height=%d field=%d\n", __func__,
1138 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1139 v4l2_fill_pix_format(&f->
fmt.
pix, &mbus_fmt);
1143 static int vidioc_querycap(
struct file *file,
void *priv,
1162 static int vidioc_enum_fmt_vid_cap(
struct file *file,
void *priv,
1175 static int vidioc_reqbufs(
struct file *file,
void *priv,
1182 static int vidioc_querybuf(
struct file *file,
void *priv,
1189 static int vidioc_qbuf(
struct file *file,
void *priv,
1196 static int vidioc_dqbuf(
struct file *file,
void *priv,
1204 static int vidioc_streamon(
struct file *file,
void *priv,
1209 dprintk(1,
"%s()\n", __func__);
1217 if (
unlikely(!res_get(dev, fh, get_resource(fh))))
1230 static int vidioc_streamoff(
struct file *file,
void *priv,
enum v4l2_buf_type i)
1235 dprintk(1,
"%s()\n", __func__);
1243 res = get_resource(fh);
1247 res_free(dev, fh, res);
1251 static int vidioc_g_std(
struct file *file,
void *priv,
v4l2_std_id *
id)
1254 dprintk(1,
"%s()\n", __func__);
1261 static int vidioc_s_std(
struct file *file,
void *priv,
v4l2_std_id *tvnorms)
1264 dprintk(1,
"%s()\n", __func__);
1275 static const char *iname[] = {
1288 dprintk(1,
"%s()\n", __func__);
1294 if (0 ==
INPUT(n)->type)
1310 if (dev->
input == n) {
1322 static int vidioc_enum_input(
struct file *file,
void *priv,
1326 dprintk(1,
"%s()\n", __func__);
1335 dprintk(1,
"%s() returns %d\n", __func__, *i);
1339 static int vidioc_g_input(
struct file *file,
void *priv,
unsigned int *i)
1348 dprintk(1,
"%s(%d)\n", __func__, i);
1351 dprintk(1,
"%s() -EINVAL\n", __func__);
1355 if (
INPUT(i)->type == 0)
1359 cx23885_video_mux(dev, i);
1363 cx23885_audio_mux(dev, i);
1368 static int vidioc_s_input(
struct file *file,
void *priv,
unsigned int i)
1373 static int vidioc_log_status(
struct file *file,
void *priv)
1379 "%s/0: ============ START LOG STATUS ============\n",
1383 "%s/0: ============= END LOG STATUS =============\n",
1388 static int cx23885_query_audinput(
struct file *file,
void *priv,
1392 static const char *iname[] = {
1393 [0] =
"Baseband L/R 1",
1394 [1] =
"Baseband L/R 2",
1397 dprintk(1,
"%s()\n", __func__);
1403 memset(i, 0,
sizeof(*i));
1412 static int vidioc_enum_audinput(
struct file *file,
void *priv,
1415 return cx23885_query_audinput(file, priv, i);
1418 static int vidioc_g_audinput(
struct file *file,
void *priv,
1426 return cx23885_query_audinput(file, priv, i);
1429 static int vidioc_s_audinput(
struct file *file,
void *priv,
1442 cx23885_flatiron_mux(dev, dev->
audinput + 1);
1446 static int vidioc_queryctrl(
struct file *file,
void *priv,
1452 return cx23885_ctrl_query(qctrl);
1455 static int vidioc_g_ctrl(
struct file *file,
void *priv,
1463 static int vidioc_s_ctrl(
struct file *file,
void *priv,
1471 static int vidioc_g_tuner(
struct file *file,
void *priv,
1487 static int vidioc_s_tuner(
struct file *file,
void *priv,
1502 static int vidioc_g_frequency(
struct file *file,
void *priv,
1551 static int cx23885_set_freq_via_ops(
struct cx23885_dev *dev,
1574 dprintk(1,
"%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
1583 fe = vfe->
dvb.frontend;
1588 fe = &dev->ts1.analog_fe;
1590 if (fe && fe->
ops.tuner_ops.set_analog_params) {
1592 fe->
ops.tuner_ops.set_analog_params(fe, ¶ms);
1616 switch (dev->
board) {
1620 ret = cx23885_set_freq_via_ops(dev, f);
1623 ret = cx23885_set_freq(dev, f);
1629 static int vidioc_s_frequency(
struct file *file,
void *priv,
1637 static void cx23885_vid_timeout(
unsigned long data)
1642 unsigned long flags;
1645 while (!list_empty(&q->
active)) {
1652 dev->
name, buf, buf->
vb.i,
1653 (
unsigned long)buf->
risc.dma);
1655 cx23885_restart_video_queue(dev, q);
1656 spin_unlock_irqrestore(&dev->
slock, flags);
1665 if (0 == (status & mask))
1675 if (status & VID_BC_MSK_OPC_ERR) {
1676 dprintk(7,
" (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1677 VID_BC_MSK_OPC_ERR);
1684 if (status & VID_BC_MSK_SYNC)
1685 dprintk(7,
" (VID_BC_MSK_SYNC 0x%08x) "
1686 "video lines miss-match\n",
1689 if (status & VID_BC_MSK_OF)
1690 dprintk(7,
" (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1697 spin_lock(&dev->
slock);
1700 spin_unlock(&dev->
slock);
1704 dprintk(2,
"stopper video\n");
1705 spin_lock(&dev->
slock);
1706 cx23885_restart_video_queue(dev, &dev->
vidq);
1707 spin_unlock(&dev->
slock);
1723 .release = video_release,
1760 #ifdef CONFIG_VIDEO_ADV_DEBUG
1761 .vidioc_g_register = cx23885_g_register,
1762 .vidioc_s_register = cx23885_s_register,
1764 .vidioc_enumaudio = vidioc_enum_audinput,
1765 .vidioc_g_audio = vidioc_g_audinput,
1766 .vidioc_s_audio = vidioc_s_audinput,
1771 .name =
"cx23885-video",
1772 .fops = &video_fops,
1773 .ioctl_ops = &video_ioctl_ops,
1781 .release = video_release,
1788 dprintk(1,
"%s()\n", __func__);
1792 if (video_is_registered(dev->
vbi_dev))
1800 if (video_is_registered(dev->
video_dev))
1817 dprintk(1,
"%s()\n", __func__);
1821 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1822 sizeof(cx23885_vbi_template));
1823 strcpy(cx23885_vbi_template.name,
"cx23885-vbi");
1828 INIT_LIST_HEAD(&dev->
vidq.active);
1829 INIT_LIST_HEAD(&dev->
vidq.queued);
1830 dev->
vidq.timeout.function = cx23885_vid_timeout;
1831 dev->
vidq.timeout.data = (
unsigned long)dev;
1837 INIT_LIST_HEAD(&dev->
vbiq.active);
1838 INIT_LIST_HEAD(&dev->
vbiq.queued);
1840 dev->
vbiq.timeout.data = (
unsigned long)dev;
1860 struct tuner_setup tun_setup;
1862 memset(&tun_setup, 0,
sizeof(tun_setup));
1863 tun_setup.mode_mask = T_ANALOG_TV;
1886 &cx23885_video_template,
"video");
1898 dev->
vbi_dev = cx23885_vdev_init(dev, dev->
pci,
1899 &cx23885_vbi_template,
"vbi");
1917 cx23885_video_mux(dev, 0);
1918 cx23885_audio_mux(dev, 0);