xref: /linux/drivers/media/pci/cx23885/cx23885-video.c (revision 082c0576d62d7732528d195792f7c80cae5360a1)
1b285192aSMauro Carvalho Chehab /*
2b285192aSMauro Carvalho Chehab  *  Driver for the Conexant CX23885 PCIe bridge
3b285192aSMauro Carvalho Chehab  *
4b285192aSMauro Carvalho Chehab  *  Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
5b285192aSMauro Carvalho Chehab  *
6b285192aSMauro Carvalho Chehab  *  This program is free software; you can redistribute it and/or modify
7b285192aSMauro Carvalho Chehab  *  it under the terms of the GNU General Public License as published by
8b285192aSMauro Carvalho Chehab  *  the Free Software Foundation; either version 2 of the License, or
9b285192aSMauro Carvalho Chehab  *  (at your option) any later version.
10b285192aSMauro Carvalho Chehab  *
11b285192aSMauro Carvalho Chehab  *  This program is distributed in the hope that it will be useful,
12b285192aSMauro Carvalho Chehab  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13b285192aSMauro Carvalho Chehab  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14b285192aSMauro Carvalho Chehab  *
15b285192aSMauro Carvalho Chehab  *  GNU General Public License for more details.
16b285192aSMauro Carvalho Chehab  *
17b285192aSMauro Carvalho Chehab  *  You should have received a copy of the GNU General Public License
18b285192aSMauro Carvalho Chehab  *  along with this program; if not, write to the Free Software
19b285192aSMauro Carvalho Chehab  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20b285192aSMauro Carvalho Chehab  */
21b285192aSMauro Carvalho Chehab 
22b285192aSMauro Carvalho Chehab #include <linux/init.h>
23b285192aSMauro Carvalho Chehab #include <linux/list.h>
24b285192aSMauro Carvalho Chehab #include <linux/module.h>
25b285192aSMauro Carvalho Chehab #include <linux/moduleparam.h>
26b285192aSMauro Carvalho Chehab #include <linux/kmod.h>
27b285192aSMauro Carvalho Chehab #include <linux/kernel.h>
28b285192aSMauro Carvalho Chehab #include <linux/slab.h>
29b285192aSMauro Carvalho Chehab #include <linux/interrupt.h>
30b285192aSMauro Carvalho Chehab #include <linux/delay.h>
31b285192aSMauro Carvalho Chehab #include <linux/kthread.h>
32b285192aSMauro Carvalho Chehab #include <asm/div64.h>
33b285192aSMauro Carvalho Chehab 
34b285192aSMauro Carvalho Chehab #include "cx23885.h"
35b285192aSMauro Carvalho Chehab #include <media/v4l2-common.h>
36b285192aSMauro Carvalho Chehab #include <media/v4l2-ioctl.h>
37b285192aSMauro Carvalho Chehab #include "cx23885-ioctl.h"
38b285192aSMauro Carvalho Chehab #include "tuner-xc2028.h"
39b285192aSMauro Carvalho Chehab 
40b285192aSMauro Carvalho Chehab #include <media/cx25840.h>
41b285192aSMauro Carvalho Chehab 
42b285192aSMauro Carvalho Chehab MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
43b285192aSMauro Carvalho Chehab MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
44b285192aSMauro Carvalho Chehab MODULE_LICENSE("GPL");
45b285192aSMauro Carvalho Chehab 
46b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
47b285192aSMauro Carvalho Chehab 
48b285192aSMauro Carvalho Chehab static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
49b285192aSMauro Carvalho Chehab static unsigned int vbi_nr[]   = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
50b285192aSMauro Carvalho Chehab static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
51b285192aSMauro Carvalho Chehab 
52b285192aSMauro Carvalho Chehab module_param_array(video_nr, int, NULL, 0444);
53b285192aSMauro Carvalho Chehab module_param_array(vbi_nr,   int, NULL, 0444);
54b285192aSMauro Carvalho Chehab module_param_array(radio_nr, int, NULL, 0444);
55b285192aSMauro Carvalho Chehab 
56b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_nr, "video device numbers");
57b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
58b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(radio_nr, "radio device numbers");
59b285192aSMauro Carvalho Chehab 
60b285192aSMauro Carvalho Chehab static unsigned int video_debug;
61b285192aSMauro Carvalho Chehab module_param(video_debug, int, 0644);
62b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
63b285192aSMauro Carvalho Chehab 
64b285192aSMauro Carvalho Chehab static unsigned int irq_debug;
65b285192aSMauro Carvalho Chehab module_param(irq_debug, int, 0644);
66b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
67b285192aSMauro Carvalho Chehab 
68b285192aSMauro Carvalho Chehab static unsigned int vid_limit = 16;
69b285192aSMauro Carvalho Chehab module_param(vid_limit, int, 0644);
70b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
71b285192aSMauro Carvalho Chehab 
72b285192aSMauro Carvalho Chehab #define dprintk(level, fmt, arg...)\
73b285192aSMauro Carvalho Chehab 	do { if (video_debug >= level)\
74b285192aSMauro Carvalho Chehab 		printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
75b285192aSMauro Carvalho Chehab 	} while (0)
76b285192aSMauro Carvalho Chehab 
77b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------- */
78b285192aSMauro Carvalho Chehab /* static data                                                         */
79b285192aSMauro Carvalho Chehab 
80b285192aSMauro Carvalho Chehab #define FORMAT_FLAGS_PACKED       0x01
81b285192aSMauro Carvalho Chehab #if 0
82b285192aSMauro Carvalho Chehab static struct cx23885_fmt formats[] = {
83b285192aSMauro Carvalho Chehab 	{
84b285192aSMauro Carvalho Chehab 		.name     = "8 bpp, gray",
85b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_GREY,
86b285192aSMauro Carvalho Chehab 		.depth    = 8,
87b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
88b285192aSMauro Carvalho Chehab 	}, {
89b285192aSMauro Carvalho Chehab 		.name     = "15 bpp RGB, le",
90b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_RGB555,
91b285192aSMauro Carvalho Chehab 		.depth    = 16,
92b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
93b285192aSMauro Carvalho Chehab 	}, {
94b285192aSMauro Carvalho Chehab 		.name     = "15 bpp RGB, be",
95b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_RGB555X,
96b285192aSMauro Carvalho Chehab 		.depth    = 16,
97b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
98b285192aSMauro Carvalho Chehab 	}, {
99b285192aSMauro Carvalho Chehab 		.name     = "16 bpp RGB, le",
100b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_RGB565,
101b285192aSMauro Carvalho Chehab 		.depth    = 16,
102b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
103b285192aSMauro Carvalho Chehab 	}, {
104b285192aSMauro Carvalho Chehab 		.name     = "16 bpp RGB, be",
105b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_RGB565X,
106b285192aSMauro Carvalho Chehab 		.depth    = 16,
107b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
108b285192aSMauro Carvalho Chehab 	}, {
109b285192aSMauro Carvalho Chehab 		.name     = "24 bpp RGB, le",
110b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_BGR24,
111b285192aSMauro Carvalho Chehab 		.depth    = 24,
112b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
113b285192aSMauro Carvalho Chehab 	}, {
114b285192aSMauro Carvalho Chehab 		.name     = "32 bpp RGB, le",
115b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_BGR32,
116b285192aSMauro Carvalho Chehab 		.depth    = 32,
117b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
118b285192aSMauro Carvalho Chehab 	}, {
119b285192aSMauro Carvalho Chehab 		.name     = "32 bpp RGB, be",
120b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_RGB32,
121b285192aSMauro Carvalho Chehab 		.depth    = 32,
122b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
123b285192aSMauro Carvalho Chehab 	}, {
124b285192aSMauro Carvalho Chehab 		.name     = "4:2:2, packed, YUYV",
125b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_YUYV,
126b285192aSMauro Carvalho Chehab 		.depth    = 16,
127b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
128b285192aSMauro Carvalho Chehab 	}, {
129b285192aSMauro Carvalho Chehab 		.name     = "4:2:2, packed, UYVY",
130b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_UYVY,
131b285192aSMauro Carvalho Chehab 		.depth    = 16,
132b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
133b285192aSMauro Carvalho Chehab 	},
134b285192aSMauro Carvalho Chehab };
135b285192aSMauro Carvalho Chehab #else
136b285192aSMauro Carvalho Chehab static struct cx23885_fmt formats[] = {
137b285192aSMauro Carvalho Chehab 	{
138b285192aSMauro Carvalho Chehab #if 0
139b285192aSMauro Carvalho Chehab 		.name     = "4:2:2, packed, UYVY",
140b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_UYVY,
141b285192aSMauro Carvalho Chehab 		.depth    = 16,
142b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
143b285192aSMauro Carvalho Chehab 	}, {
144b285192aSMauro Carvalho Chehab #endif
145b285192aSMauro Carvalho Chehab 		.name     = "4:2:2, packed, YUYV",
146b285192aSMauro Carvalho Chehab 		.fourcc   = V4L2_PIX_FMT_YUYV,
147b285192aSMauro Carvalho Chehab 		.depth    = 16,
148b285192aSMauro Carvalho Chehab 		.flags    = FORMAT_FLAGS_PACKED,
149b285192aSMauro Carvalho Chehab 	}
150b285192aSMauro Carvalho Chehab };
151b285192aSMauro Carvalho Chehab #endif
152b285192aSMauro Carvalho Chehab 
153b285192aSMauro Carvalho Chehab static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
154b285192aSMauro Carvalho Chehab {
155b285192aSMauro Carvalho Chehab 	unsigned int i;
156b285192aSMauro Carvalho Chehab 
157b285192aSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(formats); i++)
158b285192aSMauro Carvalho Chehab 		if (formats[i].fourcc == fourcc)
159b285192aSMauro Carvalho Chehab 			return formats+i;
160b285192aSMauro Carvalho Chehab 
161b285192aSMauro Carvalho Chehab 	printk(KERN_ERR "%s(%c%c%c%c) NOT FOUND\n", __func__,
162b285192aSMauro Carvalho Chehab 		(fourcc & 0xff),
163b285192aSMauro Carvalho Chehab 		((fourcc >> 8) & 0xff),
164b285192aSMauro Carvalho Chehab 		((fourcc >> 16) & 0xff),
165b285192aSMauro Carvalho Chehab 		((fourcc >> 24) & 0xff)
166b285192aSMauro Carvalho Chehab 		);
167b285192aSMauro Carvalho Chehab 	return NULL;
168b285192aSMauro Carvalho Chehab }
169b285192aSMauro Carvalho Chehab 
170b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------- */
171b285192aSMauro Carvalho Chehab 
172b285192aSMauro Carvalho Chehab static const struct v4l2_queryctrl no_ctl = {
173b285192aSMauro Carvalho Chehab 	.name  = "42",
174b285192aSMauro Carvalho Chehab 	.flags = V4L2_CTRL_FLAG_DISABLED,
175b285192aSMauro Carvalho Chehab };
176b285192aSMauro Carvalho Chehab 
177b285192aSMauro Carvalho Chehab static struct cx23885_ctrl cx23885_ctls[] = {
178b285192aSMauro Carvalho Chehab 	/* --- video --- */
179b285192aSMauro Carvalho Chehab 	{
180b285192aSMauro Carvalho Chehab 		.v = {
181b285192aSMauro Carvalho Chehab 			.id            = V4L2_CID_BRIGHTNESS,
182b285192aSMauro Carvalho Chehab 			.name          = "Brightness",
183b285192aSMauro Carvalho Chehab 			.minimum       = 0x00,
184b285192aSMauro Carvalho Chehab 			.maximum       = 0xff,
185b285192aSMauro Carvalho Chehab 			.step          = 1,
186b285192aSMauro Carvalho Chehab 			.default_value = 0x7f,
187b285192aSMauro Carvalho Chehab 			.type          = V4L2_CTRL_TYPE_INTEGER,
188b285192aSMauro Carvalho Chehab 		},
189b285192aSMauro Carvalho Chehab 		.off                   = 128,
190b285192aSMauro Carvalho Chehab 		.reg                   = LUMA_CTRL,
191b285192aSMauro Carvalho Chehab 		.mask                  = 0x00ff,
192b285192aSMauro Carvalho Chehab 		.shift                 = 0,
193b285192aSMauro Carvalho Chehab 	}, {
194b285192aSMauro Carvalho Chehab 		.v = {
195b285192aSMauro Carvalho Chehab 			.id            = V4L2_CID_CONTRAST,
196b285192aSMauro Carvalho Chehab 			.name          = "Contrast",
197b285192aSMauro Carvalho Chehab 			.minimum       = 0,
198b285192aSMauro Carvalho Chehab 			.maximum       = 0x7f,
199b285192aSMauro Carvalho Chehab 			.step          = 1,
200b285192aSMauro Carvalho Chehab 			.default_value = 0x3f,
201b285192aSMauro Carvalho Chehab 			.type          = V4L2_CTRL_TYPE_INTEGER,
202b285192aSMauro Carvalho Chehab 		},
203b285192aSMauro Carvalho Chehab 		.off                   = 0,
204b285192aSMauro Carvalho Chehab 		.reg                   = LUMA_CTRL,
205b285192aSMauro Carvalho Chehab 		.mask                  = 0xff00,
206b285192aSMauro Carvalho Chehab 		.shift                 = 8,
207b285192aSMauro Carvalho Chehab 	}, {
208b285192aSMauro Carvalho Chehab 		.v = {
209b285192aSMauro Carvalho Chehab 			.id            = V4L2_CID_HUE,
210b285192aSMauro Carvalho Chehab 			.name          = "Hue",
211b285192aSMauro Carvalho Chehab 			.minimum       = -127,
212b285192aSMauro Carvalho Chehab 			.maximum       = 128,
213b285192aSMauro Carvalho Chehab 			.step          = 1,
214b285192aSMauro Carvalho Chehab 			.default_value = 0x0,
215b285192aSMauro Carvalho Chehab 			.type          = V4L2_CTRL_TYPE_INTEGER,
216b285192aSMauro Carvalho Chehab 		},
217b285192aSMauro Carvalho Chehab 		.off                   = 128,
218b285192aSMauro Carvalho Chehab 		.reg                   = CHROMA_CTRL,
219b285192aSMauro Carvalho Chehab 		.mask                  = 0xff0000,
220b285192aSMauro Carvalho Chehab 		.shift                 = 16,
221b285192aSMauro Carvalho Chehab 	}, {
222b285192aSMauro Carvalho Chehab 		/* strictly, this only describes only U saturation.
223b285192aSMauro Carvalho Chehab 		 * V saturation is handled specially through code.
224b285192aSMauro Carvalho Chehab 		 */
225b285192aSMauro Carvalho Chehab 		.v = {
226b285192aSMauro Carvalho Chehab 			.id            = V4L2_CID_SATURATION,
227b285192aSMauro Carvalho Chehab 			.name          = "Saturation",
228b285192aSMauro Carvalho Chehab 			.minimum       = 0,
229b285192aSMauro Carvalho Chehab 			.maximum       = 0x7f,
230b285192aSMauro Carvalho Chehab 			.step          = 1,
231b285192aSMauro Carvalho Chehab 			.default_value = 0x3f,
232b285192aSMauro Carvalho Chehab 			.type          = V4L2_CTRL_TYPE_INTEGER,
233b285192aSMauro Carvalho Chehab 		},
234b285192aSMauro Carvalho Chehab 		.off                   = 0,
235b285192aSMauro Carvalho Chehab 		.reg                   = CHROMA_CTRL,
236b285192aSMauro Carvalho Chehab 		.mask                  = 0x00ff,
237b285192aSMauro Carvalho Chehab 		.shift                 = 0,
238b285192aSMauro Carvalho Chehab 	}, {
239b285192aSMauro Carvalho Chehab 	/* --- audio --- */
240b285192aSMauro Carvalho Chehab 		.v = {
241b285192aSMauro Carvalho Chehab 			.id            = V4L2_CID_AUDIO_MUTE,
242b285192aSMauro Carvalho Chehab 			.name          = "Mute",
243b285192aSMauro Carvalho Chehab 			.minimum       = 0,
244b285192aSMauro Carvalho Chehab 			.maximum       = 1,
245b285192aSMauro Carvalho Chehab 			.default_value = 1,
246b285192aSMauro Carvalho Chehab 			.type          = V4L2_CTRL_TYPE_BOOLEAN,
247b285192aSMauro Carvalho Chehab 		},
248b285192aSMauro Carvalho Chehab 		.reg                   = PATH1_CTL1,
249b285192aSMauro Carvalho Chehab 		.mask                  = (0x1f << 24),
250b285192aSMauro Carvalho Chehab 		.shift                 = 24,
251b285192aSMauro Carvalho Chehab 	}, {
252b285192aSMauro Carvalho Chehab 		.v = {
253b285192aSMauro Carvalho Chehab 			.id            = V4L2_CID_AUDIO_VOLUME,
254b285192aSMauro Carvalho Chehab 			.name          = "Volume",
255b285192aSMauro Carvalho Chehab 			.minimum       = 0,
256b285192aSMauro Carvalho Chehab 			.maximum       = 65535,
257b285192aSMauro Carvalho Chehab 			.step          = 65535 / 100,
258b285192aSMauro Carvalho Chehab 			.default_value = 65535,
259b285192aSMauro Carvalho Chehab 			.type          = V4L2_CTRL_TYPE_INTEGER,
260b285192aSMauro Carvalho Chehab 		},
261b285192aSMauro Carvalho Chehab 		.reg                   = PATH1_VOL_CTL,
262b285192aSMauro Carvalho Chehab 		.mask                  = 0xff,
263b285192aSMauro Carvalho Chehab 		.shift                 = 0,
264b285192aSMauro Carvalho Chehab 	}
265b285192aSMauro Carvalho Chehab };
266b285192aSMauro Carvalho Chehab static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
267b285192aSMauro Carvalho Chehab 
268b285192aSMauro Carvalho Chehab /* Must be sorted from low to high control ID! */
269b285192aSMauro Carvalho Chehab static const u32 cx23885_user_ctrls[] = {
270b285192aSMauro Carvalho Chehab 	V4L2_CID_USER_CLASS,
271b285192aSMauro Carvalho Chehab 	V4L2_CID_BRIGHTNESS,
272b285192aSMauro Carvalho Chehab 	V4L2_CID_CONTRAST,
273b285192aSMauro Carvalho Chehab 	V4L2_CID_SATURATION,
274b285192aSMauro Carvalho Chehab 	V4L2_CID_HUE,
275b285192aSMauro Carvalho Chehab 	V4L2_CID_AUDIO_VOLUME,
276b285192aSMauro Carvalho Chehab 	V4L2_CID_AUDIO_MUTE,
277b285192aSMauro Carvalho Chehab 	0
278b285192aSMauro Carvalho Chehab };
279b285192aSMauro Carvalho Chehab 
280b285192aSMauro Carvalho Chehab static const u32 *ctrl_classes[] = {
281b285192aSMauro Carvalho Chehab 	cx23885_user_ctrls,
282b285192aSMauro Carvalho Chehab 	NULL
283b285192aSMauro Carvalho Chehab };
284b285192aSMauro Carvalho Chehab 
285b285192aSMauro Carvalho Chehab void cx23885_video_wakeup(struct cx23885_dev *dev,
286b285192aSMauro Carvalho Chehab 	struct cx23885_dmaqueue *q, u32 count)
287b285192aSMauro Carvalho Chehab {
288b285192aSMauro Carvalho Chehab 	struct cx23885_buffer *buf;
289b285192aSMauro Carvalho Chehab 	int bc;
290b285192aSMauro Carvalho Chehab 
291b285192aSMauro Carvalho Chehab 	for (bc = 0;; bc++) {
292b285192aSMauro Carvalho Chehab 		if (list_empty(&q->active))
293b285192aSMauro Carvalho Chehab 			break;
294b285192aSMauro Carvalho Chehab 		buf = list_entry(q->active.next,
295b285192aSMauro Carvalho Chehab 				 struct cx23885_buffer, vb.queue);
296b285192aSMauro Carvalho Chehab 
297b285192aSMauro Carvalho Chehab 		/* count comes from the hw and is is 16bit wide --
298b285192aSMauro Carvalho Chehab 		 * this trick handles wrap-arounds correctly for
299b285192aSMauro Carvalho Chehab 		 * up to 32767 buffers in flight... */
300b285192aSMauro Carvalho Chehab 		if ((s16) (count - buf->count) < 0)
301b285192aSMauro Carvalho Chehab 			break;
302b285192aSMauro Carvalho Chehab 
303b285192aSMauro Carvalho Chehab 		do_gettimeofday(&buf->vb.ts);
304b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
305b285192aSMauro Carvalho Chehab 			count, buf->count);
306b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_DONE;
307b285192aSMauro Carvalho Chehab 		list_del(&buf->vb.queue);
308b285192aSMauro Carvalho Chehab 		wake_up(&buf->vb.done);
309b285192aSMauro Carvalho Chehab 	}
310b285192aSMauro Carvalho Chehab 	if (list_empty(&q->active))
311b285192aSMauro Carvalho Chehab 		del_timer(&q->timeout);
312b285192aSMauro Carvalho Chehab 	else
313b285192aSMauro Carvalho Chehab 		mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
314b285192aSMauro Carvalho Chehab 	if (bc != 1)
315b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
316b285192aSMauro Carvalho Chehab 			__func__, bc);
317b285192aSMauro Carvalho Chehab }
318b285192aSMauro Carvalho Chehab 
319b285192aSMauro Carvalho Chehab int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
320b285192aSMauro Carvalho Chehab {
321b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
322b285192aSMauro Carvalho Chehab 		__func__,
323b285192aSMauro Carvalho Chehab 		(unsigned int)norm,
324b285192aSMauro Carvalho Chehab 		v4l2_norm_to_name(norm));
325b285192aSMauro Carvalho Chehab 
326b285192aSMauro Carvalho Chehab 	dev->tvnorm = norm;
327b285192aSMauro Carvalho Chehab 
328b285192aSMauro Carvalho Chehab 	call_all(dev, core, s_std, norm);
329b285192aSMauro Carvalho Chehab 
330b285192aSMauro Carvalho Chehab 	return 0;
331b285192aSMauro Carvalho Chehab }
332b285192aSMauro Carvalho Chehab 
333b285192aSMauro Carvalho Chehab static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
334b285192aSMauro Carvalho Chehab 				    struct pci_dev *pci,
335b285192aSMauro Carvalho Chehab 				    struct video_device *template,
336b285192aSMauro Carvalho Chehab 				    char *type)
337b285192aSMauro Carvalho Chehab {
338b285192aSMauro Carvalho Chehab 	struct video_device *vfd;
339b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
340b285192aSMauro Carvalho Chehab 
341b285192aSMauro Carvalho Chehab 	vfd = video_device_alloc();
342b285192aSMauro Carvalho Chehab 	if (NULL == vfd)
343b285192aSMauro Carvalho Chehab 		return NULL;
344b285192aSMauro Carvalho Chehab 	*vfd = *template;
345b285192aSMauro Carvalho Chehab 	vfd->v4l2_dev = &dev->v4l2_dev;
346b285192aSMauro Carvalho Chehab 	vfd->release = video_device_release;
347b285192aSMauro Carvalho Chehab 	snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
348b285192aSMauro Carvalho Chehab 		 cx23885_boards[dev->board].name, type);
349b285192aSMauro Carvalho Chehab 	video_set_drvdata(vfd, dev);
350b285192aSMauro Carvalho Chehab 	return vfd;
351b285192aSMauro Carvalho Chehab }
352b285192aSMauro Carvalho Chehab 
353b285192aSMauro Carvalho Chehab static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
354b285192aSMauro Carvalho Chehab {
355b285192aSMauro Carvalho Chehab 	int i;
356b285192aSMauro Carvalho Chehab 
357b285192aSMauro Carvalho Chehab 	if (qctrl->id < V4L2_CID_BASE ||
358b285192aSMauro Carvalho Chehab 	    qctrl->id >= V4L2_CID_LASTP1)
359b285192aSMauro Carvalho Chehab 		return -EINVAL;
360b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX23885_CTLS; i++)
361b285192aSMauro Carvalho Chehab 		if (cx23885_ctls[i].v.id == qctrl->id)
362b285192aSMauro Carvalho Chehab 			break;
363b285192aSMauro Carvalho Chehab 	if (i == CX23885_CTLS) {
364b285192aSMauro Carvalho Chehab 		*qctrl = no_ctl;
365b285192aSMauro Carvalho Chehab 		return 0;
366b285192aSMauro Carvalho Chehab 	}
367b285192aSMauro Carvalho Chehab 	*qctrl = cx23885_ctls[i].v;
368b285192aSMauro Carvalho Chehab 	return 0;
369b285192aSMauro Carvalho Chehab }
370b285192aSMauro Carvalho Chehab 
371b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------- */
372b285192aSMauro Carvalho Chehab /* resource management                                                 */
373b285192aSMauro Carvalho Chehab 
374b285192aSMauro Carvalho Chehab static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
375b285192aSMauro Carvalho Chehab 	unsigned int bit)
376b285192aSMauro Carvalho Chehab {
377b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
378b285192aSMauro Carvalho Chehab 	if (fh->resources & bit)
379b285192aSMauro Carvalho Chehab 		/* have it already allocated */
380b285192aSMauro Carvalho Chehab 		return 1;
381b285192aSMauro Carvalho Chehab 
382b285192aSMauro Carvalho Chehab 	/* is it free? */
383b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
384b285192aSMauro Carvalho Chehab 	if (dev->resources & bit) {
385b285192aSMauro Carvalho Chehab 		/* no, someone else uses it */
386b285192aSMauro Carvalho Chehab 		mutex_unlock(&dev->lock);
387b285192aSMauro Carvalho Chehab 		return 0;
388b285192aSMauro Carvalho Chehab 	}
389b285192aSMauro Carvalho Chehab 	/* it's free, grab it */
390b285192aSMauro Carvalho Chehab 	fh->resources  |= bit;
391b285192aSMauro Carvalho Chehab 	dev->resources |= bit;
392b285192aSMauro Carvalho Chehab 	dprintk(1, "res: get %d\n", bit);
393b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
394b285192aSMauro Carvalho Chehab 	return 1;
395b285192aSMauro Carvalho Chehab }
396b285192aSMauro Carvalho Chehab 
397b285192aSMauro Carvalho Chehab static int res_check(struct cx23885_fh *fh, unsigned int bit)
398b285192aSMauro Carvalho Chehab {
399b285192aSMauro Carvalho Chehab 	return fh->resources & bit;
400b285192aSMauro Carvalho Chehab }
401b285192aSMauro Carvalho Chehab 
402b285192aSMauro Carvalho Chehab static int res_locked(struct cx23885_dev *dev, unsigned int bit)
403b285192aSMauro Carvalho Chehab {
404b285192aSMauro Carvalho Chehab 	return dev->resources & bit;
405b285192aSMauro Carvalho Chehab }
406b285192aSMauro Carvalho Chehab 
407b285192aSMauro Carvalho Chehab static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
408b285192aSMauro Carvalho Chehab 	unsigned int bits)
409b285192aSMauro Carvalho Chehab {
410b285192aSMauro Carvalho Chehab 	BUG_ON((fh->resources & bits) != bits);
411b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
412b285192aSMauro Carvalho Chehab 
413b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
414b285192aSMauro Carvalho Chehab 	fh->resources  &= ~bits;
415b285192aSMauro Carvalho Chehab 	dev->resources &= ~bits;
416b285192aSMauro Carvalho Chehab 	dprintk(1, "res: put %d\n", bits);
417b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
418b285192aSMauro Carvalho Chehab }
419b285192aSMauro Carvalho Chehab 
420b285192aSMauro Carvalho Chehab static int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
421b285192aSMauro Carvalho Chehab {
422b285192aSMauro Carvalho Chehab 	/* 8 bit registers, 8 bit values */
423b285192aSMauro Carvalho Chehab 	u8 buf[] = { reg, data };
424b285192aSMauro Carvalho Chehab 
425b285192aSMauro Carvalho Chehab 	struct i2c_msg msg = { .addr = 0x98 >> 1,
426b285192aSMauro Carvalho Chehab 		.flags = 0, .buf = buf, .len = 2 };
427b285192aSMauro Carvalho Chehab 
428b285192aSMauro Carvalho Chehab 	return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
429b285192aSMauro Carvalho Chehab }
430b285192aSMauro Carvalho Chehab 
431b285192aSMauro Carvalho Chehab static u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
432b285192aSMauro Carvalho Chehab {
433b285192aSMauro Carvalho Chehab 	/* 8 bit registers, 8 bit values */
434b285192aSMauro Carvalho Chehab 	int ret;
435b285192aSMauro Carvalho Chehab 	u8 b0[] = { reg };
436b285192aSMauro Carvalho Chehab 	u8 b1[] = { 0 };
437b285192aSMauro Carvalho Chehab 
438b285192aSMauro Carvalho Chehab 	struct i2c_msg msg[] = {
439b285192aSMauro Carvalho Chehab 		{ .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
440b285192aSMauro Carvalho Chehab 		{ .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
441b285192aSMauro Carvalho Chehab 	};
442b285192aSMauro Carvalho Chehab 
443b285192aSMauro Carvalho Chehab 	ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
444b285192aSMauro Carvalho Chehab 	if (ret != 2)
445b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() error\n", __func__);
446b285192aSMauro Carvalho Chehab 
447b285192aSMauro Carvalho Chehab 	return b1[0];
448b285192aSMauro Carvalho Chehab }
449b285192aSMauro Carvalho Chehab 
450b285192aSMauro Carvalho Chehab static void cx23885_flatiron_dump(struct cx23885_dev *dev)
451b285192aSMauro Carvalho Chehab {
452b285192aSMauro Carvalho Chehab 	int i;
453b285192aSMauro Carvalho Chehab 	dprintk(1, "Flatiron dump\n");
454b285192aSMauro Carvalho Chehab 	for (i = 0; i < 0x24; i++) {
455b285192aSMauro Carvalho Chehab 		dprintk(1, "FI[%02x] = %02x\n", i,
456b285192aSMauro Carvalho Chehab 			cx23885_flatiron_read(dev, i));
457b285192aSMauro Carvalho Chehab 	}
458b285192aSMauro Carvalho Chehab }
459b285192aSMauro Carvalho Chehab 
460b285192aSMauro Carvalho Chehab static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
461b285192aSMauro Carvalho Chehab {
462b285192aSMauro Carvalho Chehab 	u8 val;
463b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(input = %d)\n", __func__, input);
464b285192aSMauro Carvalho Chehab 
465b285192aSMauro Carvalho Chehab 	if (input == 1)
466b285192aSMauro Carvalho Chehab 		val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
467b285192aSMauro Carvalho Chehab 	else if (input == 2)
468b285192aSMauro Carvalho Chehab 		val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
469b285192aSMauro Carvalho Chehab 	else
470b285192aSMauro Carvalho Chehab 		return -EINVAL;
471b285192aSMauro Carvalho Chehab 
472b285192aSMauro Carvalho Chehab 	val |= 0x20; /* Enable clock to delta-sigma and dec filter */
473b285192aSMauro Carvalho Chehab 
474b285192aSMauro Carvalho Chehab 	cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
475b285192aSMauro Carvalho Chehab 
476b285192aSMauro Carvalho Chehab 	/* Wake up */
477b285192aSMauro Carvalho Chehab 	cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
478b285192aSMauro Carvalho Chehab 
479b285192aSMauro Carvalho Chehab 	if (video_debug)
480b285192aSMauro Carvalho Chehab 		cx23885_flatiron_dump(dev);
481b285192aSMauro Carvalho Chehab 
482b285192aSMauro Carvalho Chehab 	return 0;
483b285192aSMauro Carvalho Chehab }
484b285192aSMauro Carvalho Chehab 
485b285192aSMauro Carvalho Chehab static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
486b285192aSMauro Carvalho Chehab {
487b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
488b285192aSMauro Carvalho Chehab 		__func__,
489b285192aSMauro Carvalho Chehab 		input, INPUT(input)->vmux,
490b285192aSMauro Carvalho Chehab 		INPUT(input)->gpio0, INPUT(input)->gpio1,
491b285192aSMauro Carvalho Chehab 		INPUT(input)->gpio2, INPUT(input)->gpio3);
492b285192aSMauro Carvalho Chehab 	dev->input = input;
493b285192aSMauro Carvalho Chehab 
494b285192aSMauro Carvalho Chehab 	if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
495b285192aSMauro Carvalho Chehab 		dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
496b285192aSMauro Carvalho Chehab 		dev->board == CX23885_BOARD_MYGICA_X8507) {
497b285192aSMauro Carvalho Chehab 		/* Select Analog TV */
498b285192aSMauro Carvalho Chehab 		if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
499b285192aSMauro Carvalho Chehab 			cx23885_gpio_clear(dev, GPIO_0);
500b285192aSMauro Carvalho Chehab 	}
501b285192aSMauro Carvalho Chehab 
502b285192aSMauro Carvalho Chehab 	/* Tell the internal A/V decoder */
503b285192aSMauro Carvalho Chehab 	v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
504b285192aSMauro Carvalho Chehab 			INPUT(input)->vmux, 0, 0);
505b285192aSMauro Carvalho Chehab 
506b285192aSMauro Carvalho Chehab 	if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
507b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_MPX885) ||
508b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
509b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
510b285192aSMauro Carvalho Chehab 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
511*082c0576SAlfredo Jesús Delaiti 		(dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
512*082c0576SAlfredo Jesús Delaiti 		(dev->board == CX23885_BOARD_MYGICA_X8507)) {
513b285192aSMauro Carvalho Chehab 		/* Configure audio routing */
514b285192aSMauro Carvalho Chehab 		v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
515b285192aSMauro Carvalho Chehab 			INPUT(input)->amux, 0, 0);
516b285192aSMauro Carvalho Chehab 
517b285192aSMauro Carvalho Chehab 		if (INPUT(input)->amux == CX25840_AUDIO7)
518b285192aSMauro Carvalho Chehab 			cx23885_flatiron_mux(dev, 1);
519b285192aSMauro Carvalho Chehab 		else if (INPUT(input)->amux == CX25840_AUDIO6)
520b285192aSMauro Carvalho Chehab 			cx23885_flatiron_mux(dev, 2);
521b285192aSMauro Carvalho Chehab 	}
522b285192aSMauro Carvalho Chehab 
523b285192aSMauro Carvalho Chehab 	return 0;
524b285192aSMauro Carvalho Chehab }
525b285192aSMauro Carvalho Chehab 
526b285192aSMauro Carvalho Chehab static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
527b285192aSMauro Carvalho Chehab {
528b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(input=%d)\n", __func__, input);
529b285192aSMauro Carvalho Chehab 
530b285192aSMauro Carvalho Chehab 	/* The baseband video core of the cx23885 has two audio inputs.
531b285192aSMauro Carvalho Chehab 	 * LR1 and LR2. In almost every single case so far only HVR1xxx
532b285192aSMauro Carvalho Chehab 	 * cards we've only ever supported LR1. Time to support LR2,
533b285192aSMauro Carvalho Chehab 	 * which is available via the optional white breakout header on
534b285192aSMauro Carvalho Chehab 	 * the board.
535b285192aSMauro Carvalho Chehab 	 * We'll use a could of existing enums in the card struct to allow
536b285192aSMauro Carvalho Chehab 	 * devs to specify which baseband input they need, or just default
537b285192aSMauro Carvalho Chehab 	 * to what we've always used.
538b285192aSMauro Carvalho Chehab 	 */
539b285192aSMauro Carvalho Chehab 	if (INPUT(input)->amux == CX25840_AUDIO7)
540b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 1);
541b285192aSMauro Carvalho Chehab 	else if (INPUT(input)->amux == CX25840_AUDIO6)
542b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 2);
543b285192aSMauro Carvalho Chehab 	else {
544b285192aSMauro Carvalho Chehab 		/* Not specifically defined, assume the default. */
545b285192aSMauro Carvalho Chehab 		cx23885_flatiron_mux(dev, 1);
546b285192aSMauro Carvalho Chehab 	}
547b285192aSMauro Carvalho Chehab 
548b285192aSMauro Carvalho Chehab 	return 0;
549b285192aSMauro Carvalho Chehab }
550b285192aSMauro Carvalho Chehab 
551b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
552b285192aSMauro Carvalho Chehab static int cx23885_start_video_dma(struct cx23885_dev *dev,
553b285192aSMauro Carvalho Chehab 			   struct cx23885_dmaqueue *q,
554b285192aSMauro Carvalho Chehab 			   struct cx23885_buffer *buf)
555b285192aSMauro Carvalho Chehab {
556b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
557b285192aSMauro Carvalho Chehab 
558b285192aSMauro Carvalho Chehab 	/* Stop the dma/fifo before we tamper with it's risc programs */
559b285192aSMauro Carvalho Chehab 	cx_clear(VID_A_DMA_CTL, 0x11);
560b285192aSMauro Carvalho Chehab 
561b285192aSMauro Carvalho Chehab 	/* setup fifo + format */
562b285192aSMauro Carvalho Chehab 	cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
563b285192aSMauro Carvalho Chehab 				buf->bpl, buf->risc.dma);
564b285192aSMauro Carvalho Chehab 
565b285192aSMauro Carvalho Chehab 	/* reset counter */
566b285192aSMauro Carvalho Chehab 	cx_write(VID_A_GPCNT_CTL, 3);
567b285192aSMauro Carvalho Chehab 	q->count = 1;
568b285192aSMauro Carvalho Chehab 
569b285192aSMauro Carvalho Chehab 	/* enable irq */
570b285192aSMauro Carvalho Chehab 	cx23885_irq_add_enable(dev, 0x01);
571b285192aSMauro Carvalho Chehab 	cx_set(VID_A_INT_MSK, 0x000011);
572b285192aSMauro Carvalho Chehab 
573b285192aSMauro Carvalho Chehab 	/* start dma */
574b285192aSMauro Carvalho Chehab 	cx_set(DEV_CNTRL2, (1<<5));
575b285192aSMauro Carvalho Chehab 	cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
576b285192aSMauro Carvalho Chehab 
577b285192aSMauro Carvalho Chehab 	return 0;
578b285192aSMauro Carvalho Chehab }
579b285192aSMauro Carvalho Chehab 
580b285192aSMauro Carvalho Chehab 
581b285192aSMauro Carvalho Chehab static int cx23885_restart_video_queue(struct cx23885_dev *dev,
582b285192aSMauro Carvalho Chehab 			       struct cx23885_dmaqueue *q)
583b285192aSMauro Carvalho Chehab {
584b285192aSMauro Carvalho Chehab 	struct cx23885_buffer *buf, *prev;
585b285192aSMauro Carvalho Chehab 	struct list_head *item;
586b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
587b285192aSMauro Carvalho Chehab 
588b285192aSMauro Carvalho Chehab 	if (!list_empty(&q->active)) {
589b285192aSMauro Carvalho Chehab 		buf = list_entry(q->active.next, struct cx23885_buffer,
590b285192aSMauro Carvalho Chehab 			vb.queue);
591b285192aSMauro Carvalho Chehab 		dprintk(2, "restart_queue [%p/%d]: restart dma\n",
592b285192aSMauro Carvalho Chehab 			buf, buf->vb.i);
593b285192aSMauro Carvalho Chehab 		cx23885_start_video_dma(dev, q, buf);
594b285192aSMauro Carvalho Chehab 		list_for_each(item, &q->active) {
595b285192aSMauro Carvalho Chehab 			buf = list_entry(item, struct cx23885_buffer,
596b285192aSMauro Carvalho Chehab 				vb.queue);
597b285192aSMauro Carvalho Chehab 			buf->count    = q->count++;
598b285192aSMauro Carvalho Chehab 		}
599b285192aSMauro Carvalho Chehab 		mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
600b285192aSMauro Carvalho Chehab 		return 0;
601b285192aSMauro Carvalho Chehab 	}
602b285192aSMauro Carvalho Chehab 
603b285192aSMauro Carvalho Chehab 	prev = NULL;
604b285192aSMauro Carvalho Chehab 	for (;;) {
605b285192aSMauro Carvalho Chehab 		if (list_empty(&q->queued))
606b285192aSMauro Carvalho Chehab 			return 0;
607b285192aSMauro Carvalho Chehab 		buf = list_entry(q->queued.next, struct cx23885_buffer,
608b285192aSMauro Carvalho Chehab 			vb.queue);
609b285192aSMauro Carvalho Chehab 		if (NULL == prev) {
610b285192aSMauro Carvalho Chehab 			list_move_tail(&buf->vb.queue, &q->active);
611b285192aSMauro Carvalho Chehab 			cx23885_start_video_dma(dev, q, buf);
612b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
613b285192aSMauro Carvalho Chehab 			buf->count    = q->count++;
614b285192aSMauro Carvalho Chehab 			mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
615b285192aSMauro Carvalho Chehab 			dprintk(2, "[%p/%d] restart_queue - first active\n",
616b285192aSMauro Carvalho Chehab 				buf, buf->vb.i);
617b285192aSMauro Carvalho Chehab 
618b285192aSMauro Carvalho Chehab 		} else if (prev->vb.width  == buf->vb.width  &&
619b285192aSMauro Carvalho Chehab 			   prev->vb.height == buf->vb.height &&
620b285192aSMauro Carvalho Chehab 			   prev->fmt       == buf->fmt) {
621b285192aSMauro Carvalho Chehab 			list_move_tail(&buf->vb.queue, &q->active);
622b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
623b285192aSMauro Carvalho Chehab 			buf->count    = q->count++;
624b285192aSMauro Carvalho Chehab 			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
625b285192aSMauro Carvalho Chehab 			prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
626b285192aSMauro Carvalho Chehab 			dprintk(2, "[%p/%d] restart_queue - move to active\n",
627b285192aSMauro Carvalho Chehab 				buf, buf->vb.i);
628b285192aSMauro Carvalho Chehab 		} else {
629b285192aSMauro Carvalho Chehab 			return 0;
630b285192aSMauro Carvalho Chehab 		}
631b285192aSMauro Carvalho Chehab 		prev = buf;
632b285192aSMauro Carvalho Chehab 	}
633b285192aSMauro Carvalho Chehab }
634b285192aSMauro Carvalho Chehab 
635b285192aSMauro Carvalho Chehab static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
636b285192aSMauro Carvalho Chehab 	unsigned int *size)
637b285192aSMauro Carvalho Chehab {
638b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh = q->priv_data;
639b285192aSMauro Carvalho Chehab 
640b285192aSMauro Carvalho Chehab 	*size = fh->fmt->depth*fh->width*fh->height >> 3;
641b285192aSMauro Carvalho Chehab 	if (0 == *count)
642b285192aSMauro Carvalho Chehab 		*count = 32;
643b285192aSMauro Carvalho Chehab 	if (*size * *count > vid_limit * 1024 * 1024)
644b285192aSMauro Carvalho Chehab 		*count = (vid_limit * 1024 * 1024) / *size;
645b285192aSMauro Carvalho Chehab 	return 0;
646b285192aSMauro Carvalho Chehab }
647b285192aSMauro Carvalho Chehab 
648b285192aSMauro Carvalho Chehab static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
649b285192aSMauro Carvalho Chehab 	       enum v4l2_field field)
650b285192aSMauro Carvalho Chehab {
651b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh  = q->priv_data;
652b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = fh->dev;
653b285192aSMauro Carvalho Chehab 	struct cx23885_buffer *buf =
654b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx23885_buffer, vb);
655b285192aSMauro Carvalho Chehab 	int rc, init_buffer = 0;
656b285192aSMauro Carvalho Chehab 	u32 line0_offset, line1_offset;
657b285192aSMauro Carvalho Chehab 	struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
658b285192aSMauro Carvalho Chehab 	int field_tff;
659b285192aSMauro Carvalho Chehab 
660b285192aSMauro Carvalho Chehab 	BUG_ON(NULL == fh->fmt);
661b285192aSMauro Carvalho Chehab 	if (fh->width  < 48 || fh->width  > norm_maxw(dev->tvnorm) ||
662b285192aSMauro Carvalho Chehab 	    fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
663b285192aSMauro Carvalho Chehab 		return -EINVAL;
664b285192aSMauro Carvalho Chehab 	buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
665b285192aSMauro Carvalho Chehab 	if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
666b285192aSMauro Carvalho Chehab 		return -EINVAL;
667b285192aSMauro Carvalho Chehab 
668b285192aSMauro Carvalho Chehab 	if (buf->fmt       != fh->fmt    ||
669b285192aSMauro Carvalho Chehab 	    buf->vb.width  != fh->width  ||
670b285192aSMauro Carvalho Chehab 	    buf->vb.height != fh->height ||
671b285192aSMauro Carvalho Chehab 	    buf->vb.field  != field) {
672b285192aSMauro Carvalho Chehab 		buf->fmt       = fh->fmt;
673b285192aSMauro Carvalho Chehab 		buf->vb.width  = fh->width;
674b285192aSMauro Carvalho Chehab 		buf->vb.height = fh->height;
675b285192aSMauro Carvalho Chehab 		buf->vb.field  = field;
676b285192aSMauro Carvalho Chehab 		init_buffer = 1;
677b285192aSMauro Carvalho Chehab 	}
678b285192aSMauro Carvalho Chehab 
679b285192aSMauro Carvalho Chehab 	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
680b285192aSMauro Carvalho Chehab 		init_buffer = 1;
681b285192aSMauro Carvalho Chehab 		rc = videobuf_iolock(q, &buf->vb, NULL);
682b285192aSMauro Carvalho Chehab 		if (0 != rc)
683b285192aSMauro Carvalho Chehab 			goto fail;
684b285192aSMauro Carvalho Chehab 	}
685b285192aSMauro Carvalho Chehab 
686b285192aSMauro Carvalho Chehab 	if (init_buffer) {
687b285192aSMauro Carvalho Chehab 		buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
688b285192aSMauro Carvalho Chehab 		switch (buf->vb.field) {
689b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_TOP:
690b285192aSMauro Carvalho Chehab 			cx23885_risc_buffer(dev->pci, &buf->risc,
691b285192aSMauro Carvalho Chehab 					 dma->sglist, 0, UNSET,
692b285192aSMauro Carvalho Chehab 					 buf->bpl, 0, buf->vb.height);
693b285192aSMauro Carvalho Chehab 			break;
694b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_BOTTOM:
695b285192aSMauro Carvalho Chehab 			cx23885_risc_buffer(dev->pci, &buf->risc,
696b285192aSMauro Carvalho Chehab 					 dma->sglist, UNSET, 0,
697b285192aSMauro Carvalho Chehab 					 buf->bpl, 0, buf->vb.height);
698b285192aSMauro Carvalho Chehab 			break;
699b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_INTERLACED:
700b285192aSMauro Carvalho Chehab 			if (dev->tvnorm & V4L2_STD_NTSC)
701b285192aSMauro Carvalho Chehab 				/* NTSC or  */
702b285192aSMauro Carvalho Chehab 				field_tff = 1;
703b285192aSMauro Carvalho Chehab 			else
704b285192aSMauro Carvalho Chehab 				field_tff = 0;
705b285192aSMauro Carvalho Chehab 
706b285192aSMauro Carvalho Chehab 			if (cx23885_boards[dev->board].force_bff)
707b285192aSMauro Carvalho Chehab 				/* PAL / SECAM OR 888 in NTSC MODE */
708b285192aSMauro Carvalho Chehab 				field_tff = 0;
709b285192aSMauro Carvalho Chehab 
710b285192aSMauro Carvalho Chehab 			if (field_tff) {
711b285192aSMauro Carvalho Chehab 				/* cx25840 transmits NTSC bottom field first */
712b285192aSMauro Carvalho Chehab 				dprintk(1, "%s() Creating TFF/NTSC risc\n",
713b285192aSMauro Carvalho Chehab 					__func__);
714b285192aSMauro Carvalho Chehab 				line0_offset = buf->bpl;
715b285192aSMauro Carvalho Chehab 				line1_offset = 0;
716b285192aSMauro Carvalho Chehab 			} else {
717b285192aSMauro Carvalho Chehab 				/* All other formats are top field first */
718b285192aSMauro Carvalho Chehab 				dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
719b285192aSMauro Carvalho Chehab 					__func__);
720b285192aSMauro Carvalho Chehab 				line0_offset = 0;
721b285192aSMauro Carvalho Chehab 				line1_offset = buf->bpl;
722b285192aSMauro Carvalho Chehab 			}
723b285192aSMauro Carvalho Chehab 			cx23885_risc_buffer(dev->pci, &buf->risc,
724b285192aSMauro Carvalho Chehab 					dma->sglist, line0_offset,
725b285192aSMauro Carvalho Chehab 					line1_offset,
726b285192aSMauro Carvalho Chehab 					buf->bpl, buf->bpl,
727b285192aSMauro Carvalho Chehab 					buf->vb.height >> 1);
728b285192aSMauro Carvalho Chehab 			break;
729b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_SEQ_TB:
730b285192aSMauro Carvalho Chehab 			cx23885_risc_buffer(dev->pci, &buf->risc,
731b285192aSMauro Carvalho Chehab 					 dma->sglist,
732b285192aSMauro Carvalho Chehab 					 0, buf->bpl * (buf->vb.height >> 1),
733b285192aSMauro Carvalho Chehab 					 buf->bpl, 0,
734b285192aSMauro Carvalho Chehab 					 buf->vb.height >> 1);
735b285192aSMauro Carvalho Chehab 			break;
736b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_SEQ_BT:
737b285192aSMauro Carvalho Chehab 			cx23885_risc_buffer(dev->pci, &buf->risc,
738b285192aSMauro Carvalho Chehab 					 dma->sglist,
739b285192aSMauro Carvalho Chehab 					 buf->bpl * (buf->vb.height >> 1), 0,
740b285192aSMauro Carvalho Chehab 					 buf->bpl, 0,
741b285192aSMauro Carvalho Chehab 					 buf->vb.height >> 1);
742b285192aSMauro Carvalho Chehab 			break;
743b285192aSMauro Carvalho Chehab 		default:
744b285192aSMauro Carvalho Chehab 			BUG();
745b285192aSMauro Carvalho Chehab 		}
746b285192aSMauro Carvalho Chehab 	}
747b285192aSMauro Carvalho Chehab 	dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
748b285192aSMauro Carvalho Chehab 		buf, buf->vb.i,
749b285192aSMauro Carvalho Chehab 		fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
750b285192aSMauro Carvalho Chehab 		(unsigned long)buf->risc.dma);
751b285192aSMauro Carvalho Chehab 
752b285192aSMauro Carvalho Chehab 	buf->vb.state = VIDEOBUF_PREPARED;
753b285192aSMauro Carvalho Chehab 	return 0;
754b285192aSMauro Carvalho Chehab 
755b285192aSMauro Carvalho Chehab  fail:
756b285192aSMauro Carvalho Chehab 	cx23885_free_buffer(q, buf);
757b285192aSMauro Carvalho Chehab 	return rc;
758b285192aSMauro Carvalho Chehab }
759b285192aSMauro Carvalho Chehab 
760b285192aSMauro Carvalho Chehab static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
761b285192aSMauro Carvalho Chehab {
762b285192aSMauro Carvalho Chehab 	struct cx23885_buffer   *buf = container_of(vb,
763b285192aSMauro Carvalho Chehab 		struct cx23885_buffer, vb);
764b285192aSMauro Carvalho Chehab 	struct cx23885_buffer   *prev;
765b285192aSMauro Carvalho Chehab 	struct cx23885_fh       *fh   = vq->priv_data;
766b285192aSMauro Carvalho Chehab 	struct cx23885_dev      *dev  = fh->dev;
767b285192aSMauro Carvalho Chehab 	struct cx23885_dmaqueue *q    = &dev->vidq;
768b285192aSMauro Carvalho Chehab 
769b285192aSMauro Carvalho Chehab 	/* add jump to stopper */
770b285192aSMauro Carvalho Chehab 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
771b285192aSMauro Carvalho Chehab 	buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
772b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
773b285192aSMauro Carvalho Chehab 
774b285192aSMauro Carvalho Chehab 	if (!list_empty(&q->queued)) {
775b285192aSMauro Carvalho Chehab 		list_add_tail(&buf->vb.queue, &q->queued);
776b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_QUEUED;
777b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
778b285192aSMauro Carvalho Chehab 			buf, buf->vb.i);
779b285192aSMauro Carvalho Chehab 
780b285192aSMauro Carvalho Chehab 	} else if (list_empty(&q->active)) {
781b285192aSMauro Carvalho Chehab 		list_add_tail(&buf->vb.queue, &q->active);
782b285192aSMauro Carvalho Chehab 		cx23885_start_video_dma(dev, q, buf);
783b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_ACTIVE;
784b285192aSMauro Carvalho Chehab 		buf->count    = q->count++;
785b285192aSMauro Carvalho Chehab 		mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
786b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - first active\n",
787b285192aSMauro Carvalho Chehab 			buf, buf->vb.i);
788b285192aSMauro Carvalho Chehab 
789b285192aSMauro Carvalho Chehab 	} else {
790b285192aSMauro Carvalho Chehab 		prev = list_entry(q->active.prev, struct cx23885_buffer,
791b285192aSMauro Carvalho Chehab 			vb.queue);
792b285192aSMauro Carvalho Chehab 		if (prev->vb.width  == buf->vb.width  &&
793b285192aSMauro Carvalho Chehab 		    prev->vb.height == buf->vb.height &&
794b285192aSMauro Carvalho Chehab 		    prev->fmt       == buf->fmt) {
795b285192aSMauro Carvalho Chehab 			list_add_tail(&buf->vb.queue, &q->active);
796b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
797b285192aSMauro Carvalho Chehab 			buf->count    = q->count++;
798b285192aSMauro Carvalho Chehab 			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
799b285192aSMauro Carvalho Chehab 			/* 64 bit bits 63-32 */
800b285192aSMauro Carvalho Chehab 			prev->risc.jmp[2] = cpu_to_le32(0);
801b285192aSMauro Carvalho Chehab 			dprintk(2, "[%p/%d] buffer_queue - append to active\n",
802b285192aSMauro Carvalho Chehab 				buf, buf->vb.i);
803b285192aSMauro Carvalho Chehab 
804b285192aSMauro Carvalho Chehab 		} else {
805b285192aSMauro Carvalho Chehab 			list_add_tail(&buf->vb.queue, &q->queued);
806b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_QUEUED;
807b285192aSMauro Carvalho Chehab 			dprintk(2, "[%p/%d] buffer_queue - first queued\n",
808b285192aSMauro Carvalho Chehab 				buf, buf->vb.i);
809b285192aSMauro Carvalho Chehab 		}
810b285192aSMauro Carvalho Chehab 	}
811b285192aSMauro Carvalho Chehab }
812b285192aSMauro Carvalho Chehab 
813b285192aSMauro Carvalho Chehab static void buffer_release(struct videobuf_queue *q,
814b285192aSMauro Carvalho Chehab 	struct videobuf_buffer *vb)
815b285192aSMauro Carvalho Chehab {
816b285192aSMauro Carvalho Chehab 	struct cx23885_buffer *buf = container_of(vb,
817b285192aSMauro Carvalho Chehab 		struct cx23885_buffer, vb);
818b285192aSMauro Carvalho Chehab 
819b285192aSMauro Carvalho Chehab 	cx23885_free_buffer(q, buf);
820b285192aSMauro Carvalho Chehab }
821b285192aSMauro Carvalho Chehab 
822b285192aSMauro Carvalho Chehab static struct videobuf_queue_ops cx23885_video_qops = {
823b285192aSMauro Carvalho Chehab 	.buf_setup    = buffer_setup,
824b285192aSMauro Carvalho Chehab 	.buf_prepare  = buffer_prepare,
825b285192aSMauro Carvalho Chehab 	.buf_queue    = buffer_queue,
826b285192aSMauro Carvalho Chehab 	.buf_release  = buffer_release,
827b285192aSMauro Carvalho Chehab };
828b285192aSMauro Carvalho Chehab 
829b285192aSMauro Carvalho Chehab static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
830b285192aSMauro Carvalho Chehab {
831b285192aSMauro Carvalho Chehab 	switch (fh->type) {
832b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
833b285192aSMauro Carvalho Chehab 		return &fh->vidq;
834b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VBI_CAPTURE:
835b285192aSMauro Carvalho Chehab 		return &fh->vbiq;
836b285192aSMauro Carvalho Chehab 	default:
837b285192aSMauro Carvalho Chehab 		BUG();
838b285192aSMauro Carvalho Chehab 		return NULL;
839b285192aSMauro Carvalho Chehab 	}
840b285192aSMauro Carvalho Chehab }
841b285192aSMauro Carvalho Chehab 
842b285192aSMauro Carvalho Chehab static int get_resource(struct cx23885_fh *fh)
843b285192aSMauro Carvalho Chehab {
844b285192aSMauro Carvalho Chehab 	switch (fh->type) {
845b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
846b285192aSMauro Carvalho Chehab 		return RESOURCE_VIDEO;
847b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VBI_CAPTURE:
848b285192aSMauro Carvalho Chehab 		return RESOURCE_VBI;
849b285192aSMauro Carvalho Chehab 	default:
850b285192aSMauro Carvalho Chehab 		BUG();
851b285192aSMauro Carvalho Chehab 		return 0;
852b285192aSMauro Carvalho Chehab 	}
853b285192aSMauro Carvalho Chehab }
854b285192aSMauro Carvalho Chehab 
855b285192aSMauro Carvalho Chehab static int video_open(struct file *file)
856b285192aSMauro Carvalho Chehab {
857b285192aSMauro Carvalho Chehab 	struct video_device *vdev = video_devdata(file);
858b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = video_drvdata(file);
859b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh;
860b285192aSMauro Carvalho Chehab 	enum v4l2_buf_type type = 0;
861b285192aSMauro Carvalho Chehab 	int radio = 0;
862b285192aSMauro Carvalho Chehab 
863b285192aSMauro Carvalho Chehab 	switch (vdev->vfl_type) {
864b285192aSMauro Carvalho Chehab 	case VFL_TYPE_GRABBER:
865b285192aSMauro Carvalho Chehab 		type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
866b285192aSMauro Carvalho Chehab 		break;
867b285192aSMauro Carvalho Chehab 	case VFL_TYPE_VBI:
868b285192aSMauro Carvalho Chehab 		type = V4L2_BUF_TYPE_VBI_CAPTURE;
869b285192aSMauro Carvalho Chehab 		break;
870b285192aSMauro Carvalho Chehab 	case VFL_TYPE_RADIO:
871b285192aSMauro Carvalho Chehab 		radio = 1;
872b285192aSMauro Carvalho Chehab 		break;
873b285192aSMauro Carvalho Chehab 	}
874b285192aSMauro Carvalho Chehab 
875b285192aSMauro Carvalho Chehab 	dprintk(1, "open dev=%s radio=%d type=%s\n",
876b285192aSMauro Carvalho Chehab 		video_device_node_name(vdev), radio, v4l2_type_names[type]);
877b285192aSMauro Carvalho Chehab 
878b285192aSMauro Carvalho Chehab 	/* allocate + initialize per filehandle data */
879b285192aSMauro Carvalho Chehab 	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
880b285192aSMauro Carvalho Chehab 	if (NULL == fh)
881b285192aSMauro Carvalho Chehab 		return -ENOMEM;
882b285192aSMauro Carvalho Chehab 
883b285192aSMauro Carvalho Chehab 	file->private_data = fh;
884b285192aSMauro Carvalho Chehab 	fh->dev      = dev;
885b285192aSMauro Carvalho Chehab 	fh->radio    = radio;
886b285192aSMauro Carvalho Chehab 	fh->type     = type;
887b285192aSMauro Carvalho Chehab 	fh->width    = 320;
888b285192aSMauro Carvalho Chehab 	fh->height   = 240;
889b285192aSMauro Carvalho Chehab 	fh->fmt      = format_by_fourcc(V4L2_PIX_FMT_YUYV);
890b285192aSMauro Carvalho Chehab 
891b285192aSMauro Carvalho Chehab 	videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
892b285192aSMauro Carvalho Chehab 			    &dev->pci->dev, &dev->slock,
893b285192aSMauro Carvalho Chehab 			    V4L2_BUF_TYPE_VIDEO_CAPTURE,
894b285192aSMauro Carvalho Chehab 			    V4L2_FIELD_INTERLACED,
895b285192aSMauro Carvalho Chehab 			    sizeof(struct cx23885_buffer),
896b285192aSMauro Carvalho Chehab 			    fh, NULL);
897b285192aSMauro Carvalho Chehab 
898b285192aSMauro Carvalho Chehab 	videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops,
899b285192aSMauro Carvalho Chehab 		&dev->pci->dev, &dev->slock,
900b285192aSMauro Carvalho Chehab 		V4L2_BUF_TYPE_VBI_CAPTURE,
901b285192aSMauro Carvalho Chehab 		V4L2_FIELD_SEQ_TB,
902b285192aSMauro Carvalho Chehab 		sizeof(struct cx23885_buffer),
903b285192aSMauro Carvalho Chehab 		fh, NULL);
904b285192aSMauro Carvalho Chehab 
905b285192aSMauro Carvalho Chehab 
906b285192aSMauro Carvalho Chehab 	dprintk(1, "post videobuf_queue_init()\n");
907b285192aSMauro Carvalho Chehab 
908b285192aSMauro Carvalho Chehab 	return 0;
909b285192aSMauro Carvalho Chehab }
910b285192aSMauro Carvalho Chehab 
911b285192aSMauro Carvalho Chehab static ssize_t video_read(struct file *file, char __user *data,
912b285192aSMauro Carvalho Chehab 	size_t count, loff_t *ppos)
913b285192aSMauro Carvalho Chehab {
914b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh = file->private_data;
915b285192aSMauro Carvalho Chehab 
916b285192aSMauro Carvalho Chehab 	switch (fh->type) {
917b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
918b285192aSMauro Carvalho Chehab 		if (res_locked(fh->dev, RESOURCE_VIDEO))
919b285192aSMauro Carvalho Chehab 			return -EBUSY;
920b285192aSMauro Carvalho Chehab 		return videobuf_read_one(&fh->vidq, data, count, ppos,
921b285192aSMauro Carvalho Chehab 					 file->f_flags & O_NONBLOCK);
922b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VBI_CAPTURE:
923b285192aSMauro Carvalho Chehab 		if (!res_get(fh->dev, fh, RESOURCE_VBI))
924b285192aSMauro Carvalho Chehab 			return -EBUSY;
925b285192aSMauro Carvalho Chehab 		return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
926b285192aSMauro Carvalho Chehab 					    file->f_flags & O_NONBLOCK);
927b285192aSMauro Carvalho Chehab 	default:
928b285192aSMauro Carvalho Chehab 		BUG();
929b285192aSMauro Carvalho Chehab 		return 0;
930b285192aSMauro Carvalho Chehab 	}
931b285192aSMauro Carvalho Chehab }
932b285192aSMauro Carvalho Chehab 
933b285192aSMauro Carvalho Chehab static unsigned int video_poll(struct file *file,
934b285192aSMauro Carvalho Chehab 	struct poll_table_struct *wait)
935b285192aSMauro Carvalho Chehab {
936b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh = file->private_data;
937b285192aSMauro Carvalho Chehab 	struct cx23885_buffer *buf;
938b285192aSMauro Carvalho Chehab 	unsigned int rc = POLLERR;
939b285192aSMauro Carvalho Chehab 
940b285192aSMauro Carvalho Chehab 	if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
941b285192aSMauro Carvalho Chehab 		if (!res_get(fh->dev, fh, RESOURCE_VBI))
942b285192aSMauro Carvalho Chehab 			return POLLERR;
943b285192aSMauro Carvalho Chehab 		return videobuf_poll_stream(file, &fh->vbiq, wait);
944b285192aSMauro Carvalho Chehab 	}
945b285192aSMauro Carvalho Chehab 
946b285192aSMauro Carvalho Chehab 	mutex_lock(&fh->vidq.vb_lock);
947b285192aSMauro Carvalho Chehab 	if (res_check(fh, RESOURCE_VIDEO)) {
948b285192aSMauro Carvalho Chehab 		/* streaming capture */
949b285192aSMauro Carvalho Chehab 		if (list_empty(&fh->vidq.stream))
950b285192aSMauro Carvalho Chehab 			goto done;
951b285192aSMauro Carvalho Chehab 		buf = list_entry(fh->vidq.stream.next,
952b285192aSMauro Carvalho Chehab 			struct cx23885_buffer, vb.stream);
953b285192aSMauro Carvalho Chehab 	} else {
954b285192aSMauro Carvalho Chehab 		/* read() capture */
955b285192aSMauro Carvalho Chehab 		buf = (struct cx23885_buffer *)fh->vidq.read_buf;
956b285192aSMauro Carvalho Chehab 		if (NULL == buf)
957b285192aSMauro Carvalho Chehab 			goto done;
958b285192aSMauro Carvalho Chehab 	}
959b285192aSMauro Carvalho Chehab 	poll_wait(file, &buf->vb.done, wait);
960b285192aSMauro Carvalho Chehab 	if (buf->vb.state == VIDEOBUF_DONE ||
961b285192aSMauro Carvalho Chehab 	    buf->vb.state == VIDEOBUF_ERROR)
962b285192aSMauro Carvalho Chehab 		rc =  POLLIN|POLLRDNORM;
963b285192aSMauro Carvalho Chehab 	else
964b285192aSMauro Carvalho Chehab 		rc = 0;
965b285192aSMauro Carvalho Chehab done:
966b285192aSMauro Carvalho Chehab 	mutex_unlock(&fh->vidq.vb_lock);
967b285192aSMauro Carvalho Chehab 	return rc;
968b285192aSMauro Carvalho Chehab }
969b285192aSMauro Carvalho Chehab 
970b285192aSMauro Carvalho Chehab static int video_release(struct file *file)
971b285192aSMauro Carvalho Chehab {
972b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh = file->private_data;
973b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = fh->dev;
974b285192aSMauro Carvalho Chehab 
975b285192aSMauro Carvalho Chehab 	/* turn off overlay */
976b285192aSMauro Carvalho Chehab 	if (res_check(fh, RESOURCE_OVERLAY)) {
977b285192aSMauro Carvalho Chehab 		/* FIXME */
978b285192aSMauro Carvalho Chehab 		res_free(dev, fh, RESOURCE_OVERLAY);
979b285192aSMauro Carvalho Chehab 	}
980b285192aSMauro Carvalho Chehab 
981b285192aSMauro Carvalho Chehab 	/* stop video capture */
982b285192aSMauro Carvalho Chehab 	if (res_check(fh, RESOURCE_VIDEO)) {
983b285192aSMauro Carvalho Chehab 		videobuf_queue_cancel(&fh->vidq);
984b285192aSMauro Carvalho Chehab 		res_free(dev, fh, RESOURCE_VIDEO);
985b285192aSMauro Carvalho Chehab 	}
986b285192aSMauro Carvalho Chehab 	if (fh->vidq.read_buf) {
987b285192aSMauro Carvalho Chehab 		buffer_release(&fh->vidq, fh->vidq.read_buf);
988b285192aSMauro Carvalho Chehab 		kfree(fh->vidq.read_buf);
989b285192aSMauro Carvalho Chehab 	}
990b285192aSMauro Carvalho Chehab 
991b285192aSMauro Carvalho Chehab 	/* stop vbi capture */
992b285192aSMauro Carvalho Chehab 	if (res_check(fh, RESOURCE_VBI)) {
993b285192aSMauro Carvalho Chehab 		if (fh->vbiq.streaming)
994b285192aSMauro Carvalho Chehab 			videobuf_streamoff(&fh->vbiq);
995b285192aSMauro Carvalho Chehab 		if (fh->vbiq.reading)
996b285192aSMauro Carvalho Chehab 			videobuf_read_stop(&fh->vbiq);
997b285192aSMauro Carvalho Chehab 		res_free(dev, fh, RESOURCE_VBI);
998b285192aSMauro Carvalho Chehab 	}
999b285192aSMauro Carvalho Chehab 
1000b285192aSMauro Carvalho Chehab 	videobuf_mmap_free(&fh->vidq);
1001b285192aSMauro Carvalho Chehab 	videobuf_mmap_free(&fh->vbiq);
1002b285192aSMauro Carvalho Chehab 
1003b285192aSMauro Carvalho Chehab 	file->private_data = NULL;
1004b285192aSMauro Carvalho Chehab 	kfree(fh);
1005b285192aSMauro Carvalho Chehab 
1006b285192aSMauro Carvalho Chehab 	/* We are not putting the tuner to sleep here on exit, because
1007b285192aSMauro Carvalho Chehab 	 * we want to use the mpeg encoder in another session to capture
1008b285192aSMauro Carvalho Chehab 	 * tuner video. Closing this will result in no video to the encoder.
1009b285192aSMauro Carvalho Chehab 	 */
1010b285192aSMauro Carvalho Chehab 
1011b285192aSMauro Carvalho Chehab 	return 0;
1012b285192aSMauro Carvalho Chehab }
1013b285192aSMauro Carvalho Chehab 
1014b285192aSMauro Carvalho Chehab static int video_mmap(struct file *file, struct vm_area_struct *vma)
1015b285192aSMauro Carvalho Chehab {
1016b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh = file->private_data;
1017b285192aSMauro Carvalho Chehab 
1018b285192aSMauro Carvalho Chehab 	return videobuf_mmap_mapper(get_queue(fh), vma);
1019b285192aSMauro Carvalho Chehab }
1020b285192aSMauro Carvalho Chehab 
1021b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
1022b285192aSMauro Carvalho Chehab /* VIDEO CTRL IOCTLS                                                  */
1023b285192aSMauro Carvalho Chehab 
1024b285192aSMauro Carvalho Chehab int cx23885_get_control(struct cx23885_dev *dev,
1025b285192aSMauro Carvalho Chehab 	struct v4l2_control *ctl)
1026b285192aSMauro Carvalho Chehab {
1027b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
1028b285192aSMauro Carvalho Chehab 	call_all(dev, core, g_ctrl, ctl);
1029b285192aSMauro Carvalho Chehab 	return 0;
1030b285192aSMauro Carvalho Chehab }
1031b285192aSMauro Carvalho Chehab 
1032b285192aSMauro Carvalho Chehab int cx23885_set_control(struct cx23885_dev *dev,
1033b285192aSMauro Carvalho Chehab 	struct v4l2_control *ctl)
1034b285192aSMauro Carvalho Chehab {
1035b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
1036b285192aSMauro Carvalho Chehab 	call_all(dev, core, s_ctrl, ctl);
1037b285192aSMauro Carvalho Chehab 
1038b285192aSMauro Carvalho Chehab 	return 0;
1039b285192aSMauro Carvalho Chehab }
1040b285192aSMauro Carvalho Chehab 
1041b285192aSMauro Carvalho Chehab static void init_controls(struct cx23885_dev *dev)
1042b285192aSMauro Carvalho Chehab {
1043b285192aSMauro Carvalho Chehab 	struct v4l2_control ctrl;
1044b285192aSMauro Carvalho Chehab 	int i;
1045b285192aSMauro Carvalho Chehab 
1046b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX23885_CTLS; i++) {
1047b285192aSMauro Carvalho Chehab 		ctrl.id = cx23885_ctls[i].v.id;
1048b285192aSMauro Carvalho Chehab 		ctrl.value = cx23885_ctls[i].v.default_value;
1049b285192aSMauro Carvalho Chehab 
1050b285192aSMauro Carvalho Chehab 		cx23885_set_control(dev, &ctrl);
1051b285192aSMauro Carvalho Chehab 	}
1052b285192aSMauro Carvalho Chehab }
1053b285192aSMauro Carvalho Chehab 
1054b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
1055b285192aSMauro Carvalho Chehab /* VIDEO IOCTLS                                                       */
1056b285192aSMauro Carvalho Chehab 
1057b285192aSMauro Carvalho Chehab static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1058b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
1059b285192aSMauro Carvalho Chehab {
1060b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh   = priv;
1061b285192aSMauro Carvalho Chehab 
1062b285192aSMauro Carvalho Chehab 	f->fmt.pix.width        = fh->width;
1063b285192aSMauro Carvalho Chehab 	f->fmt.pix.height       = fh->height;
1064b285192aSMauro Carvalho Chehab 	f->fmt.pix.field        = fh->vidq.field;
1065b285192aSMauro Carvalho Chehab 	f->fmt.pix.pixelformat  = fh->fmt->fourcc;
1066b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline =
1067b285192aSMauro Carvalho Chehab 		(f->fmt.pix.width * fh->fmt->depth) >> 3;
1068b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage =
1069b285192aSMauro Carvalho Chehab 		f->fmt.pix.height * f->fmt.pix.bytesperline;
1070b285192aSMauro Carvalho Chehab 
1071b285192aSMauro Carvalho Chehab 	return 0;
1072b285192aSMauro Carvalho Chehab }
1073b285192aSMauro Carvalho Chehab 
1074b285192aSMauro Carvalho Chehab static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1075b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
1076b285192aSMauro Carvalho Chehab {
1077b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1078b285192aSMauro Carvalho Chehab 	struct cx23885_fmt *fmt;
1079b285192aSMauro Carvalho Chehab 	enum v4l2_field   field;
1080b285192aSMauro Carvalho Chehab 	unsigned int      maxw, maxh;
1081b285192aSMauro Carvalho Chehab 
1082b285192aSMauro Carvalho Chehab 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1083b285192aSMauro Carvalho Chehab 	if (NULL == fmt)
1084b285192aSMauro Carvalho Chehab 		return -EINVAL;
1085b285192aSMauro Carvalho Chehab 
1086b285192aSMauro Carvalho Chehab 	field = f->fmt.pix.field;
1087b285192aSMauro Carvalho Chehab 	maxw  = norm_maxw(dev->tvnorm);
1088b285192aSMauro Carvalho Chehab 	maxh  = norm_maxh(dev->tvnorm);
1089b285192aSMauro Carvalho Chehab 
1090b285192aSMauro Carvalho Chehab 	if (V4L2_FIELD_ANY == field) {
1091b285192aSMauro Carvalho Chehab 		field = (f->fmt.pix.height > maxh/2)
1092b285192aSMauro Carvalho Chehab 			? V4L2_FIELD_INTERLACED
1093b285192aSMauro Carvalho Chehab 			: V4L2_FIELD_BOTTOM;
1094b285192aSMauro Carvalho Chehab 	}
1095b285192aSMauro Carvalho Chehab 
1096b285192aSMauro Carvalho Chehab 	switch (field) {
1097b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_TOP:
1098b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_BOTTOM:
1099b285192aSMauro Carvalho Chehab 		maxh = maxh / 2;
1100b285192aSMauro Carvalho Chehab 		break;
1101b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_INTERLACED:
1102b285192aSMauro Carvalho Chehab 		break;
1103b285192aSMauro Carvalho Chehab 	default:
1104b285192aSMauro Carvalho Chehab 		return -EINVAL;
1105b285192aSMauro Carvalho Chehab 	}
1106b285192aSMauro Carvalho Chehab 
1107b285192aSMauro Carvalho Chehab 	f->fmt.pix.field = field;
1108b285192aSMauro Carvalho Chehab 	v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
1109b285192aSMauro Carvalho Chehab 			      &f->fmt.pix.height, 32, maxh, 0, 0);
1110b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline =
1111b285192aSMauro Carvalho Chehab 		(f->fmt.pix.width * fmt->depth) >> 3;
1112b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage =
1113b285192aSMauro Carvalho Chehab 		f->fmt.pix.height * f->fmt.pix.bytesperline;
1114b285192aSMauro Carvalho Chehab 
1115b285192aSMauro Carvalho Chehab 	return 0;
1116b285192aSMauro Carvalho Chehab }
1117b285192aSMauro Carvalho Chehab 
1118b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1119b285192aSMauro Carvalho Chehab 	struct v4l2_format *f)
1120b285192aSMauro Carvalho Chehab {
1121b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh = priv;
1122b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev  = ((struct cx23885_fh *)priv)->dev;
1123b285192aSMauro Carvalho Chehab 	struct v4l2_mbus_framefmt mbus_fmt;
1124b285192aSMauro Carvalho Chehab 	int err;
1125b285192aSMauro Carvalho Chehab 
1126b285192aSMauro Carvalho Chehab 	dprintk(2, "%s()\n", __func__);
1127b285192aSMauro Carvalho Chehab 	err = vidioc_try_fmt_vid_cap(file, priv, f);
1128b285192aSMauro Carvalho Chehab 
1129b285192aSMauro Carvalho Chehab 	if (0 != err)
1130b285192aSMauro Carvalho Chehab 		return err;
1131b285192aSMauro Carvalho Chehab 	fh->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
1132b285192aSMauro Carvalho Chehab 	fh->width      = f->fmt.pix.width;
1133b285192aSMauro Carvalho Chehab 	fh->height     = f->fmt.pix.height;
1134b285192aSMauro Carvalho Chehab 	fh->vidq.field = f->fmt.pix.field;
1135b285192aSMauro Carvalho Chehab 	dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
1136b285192aSMauro Carvalho Chehab 		fh->width, fh->height, fh->vidq.field);
1137b285192aSMauro Carvalho Chehab 	v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1138b285192aSMauro Carvalho Chehab 	call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1139b285192aSMauro Carvalho Chehab 	v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
1140b285192aSMauro Carvalho Chehab 	return 0;
1141b285192aSMauro Carvalho Chehab }
1142b285192aSMauro Carvalho Chehab 
1143b285192aSMauro Carvalho Chehab static int vidioc_querycap(struct file *file, void  *priv,
1144b285192aSMauro Carvalho Chehab 	struct v4l2_capability *cap)
1145b285192aSMauro Carvalho Chehab {
1146b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev  = ((struct cx23885_fh *)priv)->dev;
1147b285192aSMauro Carvalho Chehab 
1148b285192aSMauro Carvalho Chehab 	strcpy(cap->driver, "cx23885");
1149b285192aSMauro Carvalho Chehab 	strlcpy(cap->card, cx23885_boards[dev->board].name,
1150b285192aSMauro Carvalho Chehab 		sizeof(cap->card));
1151b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1152b285192aSMauro Carvalho Chehab 	cap->capabilities =
1153b285192aSMauro Carvalho Chehab 		V4L2_CAP_VIDEO_CAPTURE |
1154b285192aSMauro Carvalho Chehab 		V4L2_CAP_READWRITE     |
1155b285192aSMauro Carvalho Chehab 		V4L2_CAP_STREAMING     |
1156b285192aSMauro Carvalho Chehab 		V4L2_CAP_VBI_CAPTURE;
1157b285192aSMauro Carvalho Chehab 	if (UNSET != dev->tuner_type)
1158b285192aSMauro Carvalho Chehab 		cap->capabilities |= V4L2_CAP_TUNER;
1159b285192aSMauro Carvalho Chehab 	return 0;
1160b285192aSMauro Carvalho Chehab }
1161b285192aSMauro Carvalho Chehab 
1162b285192aSMauro Carvalho Chehab static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
1163b285192aSMauro Carvalho Chehab 	struct v4l2_fmtdesc *f)
1164b285192aSMauro Carvalho Chehab {
1165b285192aSMauro Carvalho Chehab 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
1166b285192aSMauro Carvalho Chehab 		return -EINVAL;
1167b285192aSMauro Carvalho Chehab 
1168b285192aSMauro Carvalho Chehab 	strlcpy(f->description, formats[f->index].name,
1169b285192aSMauro Carvalho Chehab 		sizeof(f->description));
1170b285192aSMauro Carvalho Chehab 	f->pixelformat = formats[f->index].fourcc;
1171b285192aSMauro Carvalho Chehab 
1172b285192aSMauro Carvalho Chehab 	return 0;
1173b285192aSMauro Carvalho Chehab }
1174b285192aSMauro Carvalho Chehab 
1175b285192aSMauro Carvalho Chehab static int vidioc_reqbufs(struct file *file, void *priv,
1176b285192aSMauro Carvalho Chehab 	struct v4l2_requestbuffers *p)
1177b285192aSMauro Carvalho Chehab {
1178b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh = priv;
1179b285192aSMauro Carvalho Chehab 	return videobuf_reqbufs(get_queue(fh), p);
1180b285192aSMauro Carvalho Chehab }
1181b285192aSMauro Carvalho Chehab 
1182b285192aSMauro Carvalho Chehab static int vidioc_querybuf(struct file *file, void *priv,
1183b285192aSMauro Carvalho Chehab 	struct v4l2_buffer *p)
1184b285192aSMauro Carvalho Chehab {
1185b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh = priv;
1186b285192aSMauro Carvalho Chehab 	return videobuf_querybuf(get_queue(fh), p);
1187b285192aSMauro Carvalho Chehab }
1188b285192aSMauro Carvalho Chehab 
1189b285192aSMauro Carvalho Chehab static int vidioc_qbuf(struct file *file, void *priv,
1190b285192aSMauro Carvalho Chehab 	struct v4l2_buffer *p)
1191b285192aSMauro Carvalho Chehab {
1192b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh = priv;
1193b285192aSMauro Carvalho Chehab 	return videobuf_qbuf(get_queue(fh), p);
1194b285192aSMauro Carvalho Chehab }
1195b285192aSMauro Carvalho Chehab 
1196b285192aSMauro Carvalho Chehab static int vidioc_dqbuf(struct file *file, void *priv,
1197b285192aSMauro Carvalho Chehab 	struct v4l2_buffer *p)
1198b285192aSMauro Carvalho Chehab {
1199b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh = priv;
1200b285192aSMauro Carvalho Chehab 	return videobuf_dqbuf(get_queue(fh), p,
1201b285192aSMauro Carvalho Chehab 				file->f_flags & O_NONBLOCK);
1202b285192aSMauro Carvalho Chehab }
1203b285192aSMauro Carvalho Chehab 
1204b285192aSMauro Carvalho Chehab static int vidioc_streamon(struct file *file, void *priv,
1205b285192aSMauro Carvalho Chehab 	enum v4l2_buf_type i)
1206b285192aSMauro Carvalho Chehab {
1207b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh = priv;
1208b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = fh->dev;
1209b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1210b285192aSMauro Carvalho Chehab 
1211b285192aSMauro Carvalho Chehab 	if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1212b285192aSMauro Carvalho Chehab 		(fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1213b285192aSMauro Carvalho Chehab 		return -EINVAL;
1214b285192aSMauro Carvalho Chehab 	if (unlikely(i != fh->type))
1215b285192aSMauro Carvalho Chehab 		return -EINVAL;
1216b285192aSMauro Carvalho Chehab 
1217b285192aSMauro Carvalho Chehab 	if (unlikely(!res_get(dev, fh, get_resource(fh))))
1218b285192aSMauro Carvalho Chehab 		return -EBUSY;
1219b285192aSMauro Carvalho Chehab 
1220b285192aSMauro Carvalho Chehab 	/* Don't start VBI streaming unless vida streaming
1221b285192aSMauro Carvalho Chehab 	 * has already started.
1222b285192aSMauro Carvalho Chehab 	 */
1223b285192aSMauro Carvalho Chehab 	if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) &&
1224b285192aSMauro Carvalho Chehab 		((cx_read(VID_A_DMA_CTL) & 0x11) == 0))
1225b285192aSMauro Carvalho Chehab 		return -EINVAL;
1226b285192aSMauro Carvalho Chehab 
1227b285192aSMauro Carvalho Chehab 	return videobuf_streamon(get_queue(fh));
1228b285192aSMauro Carvalho Chehab }
1229b285192aSMauro Carvalho Chehab 
1230b285192aSMauro Carvalho Chehab static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1231b285192aSMauro Carvalho Chehab {
1232b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh = priv;
1233b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = fh->dev;
1234b285192aSMauro Carvalho Chehab 	int err, res;
1235b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1236b285192aSMauro Carvalho Chehab 
1237b285192aSMauro Carvalho Chehab 	if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1238b285192aSMauro Carvalho Chehab 		(fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1239b285192aSMauro Carvalho Chehab 		return -EINVAL;
1240b285192aSMauro Carvalho Chehab 	if (i != fh->type)
1241b285192aSMauro Carvalho Chehab 		return -EINVAL;
1242b285192aSMauro Carvalho Chehab 
1243b285192aSMauro Carvalho Chehab 	res = get_resource(fh);
1244b285192aSMauro Carvalho Chehab 	err = videobuf_streamoff(get_queue(fh));
1245b285192aSMauro Carvalho Chehab 	if (err < 0)
1246b285192aSMauro Carvalho Chehab 		return err;
1247b285192aSMauro Carvalho Chehab 	res_free(dev, fh, res);
1248b285192aSMauro Carvalho Chehab 	return 0;
1249b285192aSMauro Carvalho Chehab }
1250b285192aSMauro Carvalho Chehab 
1251b285192aSMauro Carvalho Chehab static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
1252b285192aSMauro Carvalho Chehab {
1253b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1254b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1255b285192aSMauro Carvalho Chehab 
1256b285192aSMauro Carvalho Chehab 	call_all(dev, core, g_std, id);
1257b285192aSMauro Carvalho Chehab 
1258b285192aSMauro Carvalho Chehab 	return 0;
1259b285192aSMauro Carvalho Chehab }
1260b285192aSMauro Carvalho Chehab 
1261b285192aSMauro Carvalho Chehab static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1262b285192aSMauro Carvalho Chehab {
1263b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1264b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1265b285192aSMauro Carvalho Chehab 
1266b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
1267b285192aSMauro Carvalho Chehab 	cx23885_set_tvnorm(dev, *tvnorms);
1268b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
1269b285192aSMauro Carvalho Chehab 
1270b285192aSMauro Carvalho Chehab 	return 0;
1271b285192aSMauro Carvalho Chehab }
1272b285192aSMauro Carvalho Chehab 
1273b285192aSMauro Carvalho Chehab int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1274b285192aSMauro Carvalho Chehab {
1275b285192aSMauro Carvalho Chehab 	static const char *iname[] = {
1276b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE1] = "Composite1",
1277b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE2] = "Composite2",
1278b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE3] = "Composite3",
1279b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPOSITE4] = "Composite4",
1280b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_SVIDEO]     = "S-Video",
1281b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_COMPONENT]  = "Component",
1282b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_TELEVISION] = "Television",
1283b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_CABLE]      = "Cable TV",
1284b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_DVB]        = "DVB",
1285b285192aSMauro Carvalho Chehab 		[CX23885_VMUX_DEBUG]      = "for debug only",
1286b285192aSMauro Carvalho Chehab 	};
1287b285192aSMauro Carvalho Chehab 	unsigned int n;
1288b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1289b285192aSMauro Carvalho Chehab 
1290b285192aSMauro Carvalho Chehab 	n = i->index;
1291b285192aSMauro Carvalho Chehab 	if (n >= MAX_CX23885_INPUT)
1292b285192aSMauro Carvalho Chehab 		return -EINVAL;
1293b285192aSMauro Carvalho Chehab 
1294b285192aSMauro Carvalho Chehab 	if (0 == INPUT(n)->type)
1295b285192aSMauro Carvalho Chehab 		return -EINVAL;
1296b285192aSMauro Carvalho Chehab 
1297b285192aSMauro Carvalho Chehab 	i->index = n;
1298b285192aSMauro Carvalho Chehab 	i->type  = V4L2_INPUT_TYPE_CAMERA;
1299b285192aSMauro Carvalho Chehab 	strcpy(i->name, iname[INPUT(n)->type]);
1300b285192aSMauro Carvalho Chehab 	if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1301b285192aSMauro Carvalho Chehab 		(CX23885_VMUX_CABLE == INPUT(n)->type)) {
1302b285192aSMauro Carvalho Chehab 		i->type = V4L2_INPUT_TYPE_TUNER;
1303b285192aSMauro Carvalho Chehab 		i->std = CX23885_NORMS;
1304b285192aSMauro Carvalho Chehab 	}
1305b285192aSMauro Carvalho Chehab 
1306b285192aSMauro Carvalho Chehab 	/* Two selectable audio inputs for non-tv inputs */
1307b285192aSMauro Carvalho Chehab 	if (INPUT(n)->type != CX23885_VMUX_TELEVISION)
1308b285192aSMauro Carvalho Chehab 		i->audioset = 0x3;
1309b285192aSMauro Carvalho Chehab 
1310b285192aSMauro Carvalho Chehab 	if (dev->input == n) {
1311b285192aSMauro Carvalho Chehab 		/* enum'd input matches our configured input.
1312b285192aSMauro Carvalho Chehab 		 * Ask the video decoder to process the call
1313b285192aSMauro Carvalho Chehab 		 * and give it an oppertunity to update the
1314b285192aSMauro Carvalho Chehab 		 * status field.
1315b285192aSMauro Carvalho Chehab 		 */
1316b285192aSMauro Carvalho Chehab 		call_all(dev, video, g_input_status, &i->status);
1317b285192aSMauro Carvalho Chehab 	}
1318b285192aSMauro Carvalho Chehab 
1319b285192aSMauro Carvalho Chehab 	return 0;
1320b285192aSMauro Carvalho Chehab }
1321b285192aSMauro Carvalho Chehab 
1322b285192aSMauro Carvalho Chehab static int vidioc_enum_input(struct file *file, void *priv,
1323b285192aSMauro Carvalho Chehab 				struct v4l2_input *i)
1324b285192aSMauro Carvalho Chehab {
1325b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1326b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1327b285192aSMauro Carvalho Chehab 	return cx23885_enum_input(dev, i);
1328b285192aSMauro Carvalho Chehab }
1329b285192aSMauro Carvalho Chehab 
1330b285192aSMauro Carvalho Chehab int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
1331b285192aSMauro Carvalho Chehab {
1332b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1333b285192aSMauro Carvalho Chehab 
1334b285192aSMauro Carvalho Chehab 	*i = dev->input;
1335b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() returns %d\n", __func__, *i);
1336b285192aSMauro Carvalho Chehab 	return 0;
1337b285192aSMauro Carvalho Chehab }
1338b285192aSMauro Carvalho Chehab 
1339b285192aSMauro Carvalho Chehab static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1340b285192aSMauro Carvalho Chehab {
1341b285192aSMauro Carvalho Chehab 	return cx23885_get_input(file, priv, i);
1342b285192aSMauro Carvalho Chehab }
1343b285192aSMauro Carvalho Chehab 
1344b285192aSMauro Carvalho Chehab int cx23885_set_input(struct file *file, void *priv, unsigned int i)
1345b285192aSMauro Carvalho Chehab {
1346b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1347b285192aSMauro Carvalho Chehab 
1348b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(%d)\n", __func__, i);
1349b285192aSMauro Carvalho Chehab 
1350b285192aSMauro Carvalho Chehab 	if (i >= MAX_CX23885_INPUT) {
1351b285192aSMauro Carvalho Chehab 		dprintk(1, "%s() -EINVAL\n", __func__);
1352b285192aSMauro Carvalho Chehab 		return -EINVAL;
1353b285192aSMauro Carvalho Chehab 	}
1354b285192aSMauro Carvalho Chehab 
1355b285192aSMauro Carvalho Chehab 	if (INPUT(i)->type == 0)
1356b285192aSMauro Carvalho Chehab 		return -EINVAL;
1357b285192aSMauro Carvalho Chehab 
1358b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
1359b285192aSMauro Carvalho Chehab 	cx23885_video_mux(dev, i);
1360b285192aSMauro Carvalho Chehab 
1361b285192aSMauro Carvalho Chehab 	/* By default establish the default audio input for the card also */
1362b285192aSMauro Carvalho Chehab 	/* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
1363b285192aSMauro Carvalho Chehab 	cx23885_audio_mux(dev, i);
1364b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
1365b285192aSMauro Carvalho Chehab 	return 0;
1366b285192aSMauro Carvalho Chehab }
1367b285192aSMauro Carvalho Chehab 
1368b285192aSMauro Carvalho Chehab static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1369b285192aSMauro Carvalho Chehab {
1370b285192aSMauro Carvalho Chehab 	return cx23885_set_input(file, priv, i);
1371b285192aSMauro Carvalho Chehab }
1372b285192aSMauro Carvalho Chehab 
1373b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
1374b285192aSMauro Carvalho Chehab {
1375b285192aSMauro Carvalho Chehab 	struct cx23885_fh  *fh  = priv;
1376b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = fh->dev;
1377b285192aSMauro Carvalho Chehab 
1378b285192aSMauro Carvalho Chehab 	printk(KERN_INFO
1379b285192aSMauro Carvalho Chehab 		"%s/0: ============  START LOG STATUS  ============\n",
1380b285192aSMauro Carvalho Chehab 		dev->name);
1381b285192aSMauro Carvalho Chehab 	call_all(dev, core, log_status);
1382b285192aSMauro Carvalho Chehab 	printk(KERN_INFO
1383b285192aSMauro Carvalho Chehab 		"%s/0: =============  END LOG STATUS  =============\n",
1384b285192aSMauro Carvalho Chehab 		dev->name);
1385b285192aSMauro Carvalho Chehab 	return 0;
1386b285192aSMauro Carvalho Chehab }
1387b285192aSMauro Carvalho Chehab 
1388b285192aSMauro Carvalho Chehab static int cx23885_query_audinput(struct file *file, void *priv,
1389b285192aSMauro Carvalho Chehab 	struct v4l2_audio *i)
1390b285192aSMauro Carvalho Chehab {
1391b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1392b285192aSMauro Carvalho Chehab 	static const char *iname[] = {
1393b285192aSMauro Carvalho Chehab 		[0] = "Baseband L/R 1",
1394b285192aSMauro Carvalho Chehab 		[1] = "Baseband L/R 2",
1395b285192aSMauro Carvalho Chehab 	};
1396b285192aSMauro Carvalho Chehab 	unsigned int n;
1397b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1398b285192aSMauro Carvalho Chehab 
1399b285192aSMauro Carvalho Chehab 	n = i->index;
1400b285192aSMauro Carvalho Chehab 	if (n >= 2)
1401b285192aSMauro Carvalho Chehab 		return -EINVAL;
1402b285192aSMauro Carvalho Chehab 
1403b285192aSMauro Carvalho Chehab 	memset(i, 0, sizeof(*i));
1404b285192aSMauro Carvalho Chehab 	i->index = n;
1405b285192aSMauro Carvalho Chehab 	strcpy(i->name, iname[n]);
1406b285192aSMauro Carvalho Chehab 	i->capability  = V4L2_AUDCAP_STEREO;
1407b285192aSMauro Carvalho Chehab 	i->mode  = V4L2_AUDMODE_AVL;
1408b285192aSMauro Carvalho Chehab 	return 0;
1409b285192aSMauro Carvalho Chehab 
1410b285192aSMauro Carvalho Chehab }
1411b285192aSMauro Carvalho Chehab 
1412b285192aSMauro Carvalho Chehab static int vidioc_enum_audinput(struct file *file, void *priv,
1413b285192aSMauro Carvalho Chehab 				struct v4l2_audio *i)
1414b285192aSMauro Carvalho Chehab {
1415b285192aSMauro Carvalho Chehab 	return cx23885_query_audinput(file, priv, i);
1416b285192aSMauro Carvalho Chehab }
1417b285192aSMauro Carvalho Chehab 
1418b285192aSMauro Carvalho Chehab static int vidioc_g_audinput(struct file *file, void *priv,
1419b285192aSMauro Carvalho Chehab 	struct v4l2_audio *i)
1420b285192aSMauro Carvalho Chehab {
1421b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1422b285192aSMauro Carvalho Chehab 
1423b285192aSMauro Carvalho Chehab 	i->index = dev->audinput;
1424b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(input=%d)\n", __func__, i->index);
1425b285192aSMauro Carvalho Chehab 
1426b285192aSMauro Carvalho Chehab 	return cx23885_query_audinput(file, priv, i);
1427b285192aSMauro Carvalho Chehab }
1428b285192aSMauro Carvalho Chehab 
1429b285192aSMauro Carvalho Chehab static int vidioc_s_audinput(struct file *file, void *priv,
14300e8025b9SHans Verkuil 	const struct v4l2_audio *i)
1431b285192aSMauro Carvalho Chehab {
1432b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1433b285192aSMauro Carvalho Chehab 	if (i->index >= 2)
1434b285192aSMauro Carvalho Chehab 		return -EINVAL;
1435b285192aSMauro Carvalho Chehab 
1436b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(%d)\n", __func__, i->index);
1437b285192aSMauro Carvalho Chehab 
1438b285192aSMauro Carvalho Chehab 	dev->audinput = i->index;
1439b285192aSMauro Carvalho Chehab 
1440b285192aSMauro Carvalho Chehab 	/* Skip the audio defaults from the cards struct, caller wants
1441b285192aSMauro Carvalho Chehab 	 * directly touch the audio mux hardware. */
1442b285192aSMauro Carvalho Chehab 	cx23885_flatiron_mux(dev, dev->audinput + 1);
1443b285192aSMauro Carvalho Chehab 	return 0;
1444b285192aSMauro Carvalho Chehab }
1445b285192aSMauro Carvalho Chehab 
1446b285192aSMauro Carvalho Chehab static int vidioc_queryctrl(struct file *file, void *priv,
1447b285192aSMauro Carvalho Chehab 				struct v4l2_queryctrl *qctrl)
1448b285192aSMauro Carvalho Chehab {
1449b285192aSMauro Carvalho Chehab 	qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1450b285192aSMauro Carvalho Chehab 	if (unlikely(qctrl->id == 0))
1451b285192aSMauro Carvalho Chehab 		return -EINVAL;
1452b285192aSMauro Carvalho Chehab 	return cx23885_ctrl_query(qctrl);
1453b285192aSMauro Carvalho Chehab }
1454b285192aSMauro Carvalho Chehab 
1455b285192aSMauro Carvalho Chehab static int vidioc_g_ctrl(struct file *file, void *priv,
1456b285192aSMauro Carvalho Chehab 				struct v4l2_control *ctl)
1457b285192aSMauro Carvalho Chehab {
1458b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1459b285192aSMauro Carvalho Chehab 
1460b285192aSMauro Carvalho Chehab 	return cx23885_get_control(dev, ctl);
1461b285192aSMauro Carvalho Chehab }
1462b285192aSMauro Carvalho Chehab 
1463b285192aSMauro Carvalho Chehab static int vidioc_s_ctrl(struct file *file, void *priv,
1464b285192aSMauro Carvalho Chehab 				struct v4l2_control *ctl)
1465b285192aSMauro Carvalho Chehab {
1466b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1467b285192aSMauro Carvalho Chehab 
1468b285192aSMauro Carvalho Chehab 	return cx23885_set_control(dev, ctl);
1469b285192aSMauro Carvalho Chehab }
1470b285192aSMauro Carvalho Chehab 
1471b285192aSMauro Carvalho Chehab static int vidioc_g_tuner(struct file *file, void *priv,
1472b285192aSMauro Carvalho Chehab 				struct v4l2_tuner *t)
1473b285192aSMauro Carvalho Chehab {
1474b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1475b285192aSMauro Carvalho Chehab 
1476b285192aSMauro Carvalho Chehab 	if (unlikely(UNSET == dev->tuner_type))
1477b285192aSMauro Carvalho Chehab 		return -EINVAL;
1478b285192aSMauro Carvalho Chehab 	if (0 != t->index)
1479b285192aSMauro Carvalho Chehab 		return -EINVAL;
1480b285192aSMauro Carvalho Chehab 
1481b285192aSMauro Carvalho Chehab 	strcpy(t->name, "Television");
1482b285192aSMauro Carvalho Chehab 
1483b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, g_tuner, t);
1484b285192aSMauro Carvalho Chehab 	return 0;
1485b285192aSMauro Carvalho Chehab }
1486b285192aSMauro Carvalho Chehab 
1487b285192aSMauro Carvalho Chehab static int vidioc_s_tuner(struct file *file, void *priv,
1488b285192aSMauro Carvalho Chehab 				struct v4l2_tuner *t)
1489b285192aSMauro Carvalho Chehab {
1490b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1491b285192aSMauro Carvalho Chehab 
1492b285192aSMauro Carvalho Chehab 	if (UNSET == dev->tuner_type)
1493b285192aSMauro Carvalho Chehab 		return -EINVAL;
1494b285192aSMauro Carvalho Chehab 	if (0 != t->index)
1495b285192aSMauro Carvalho Chehab 		return -EINVAL;
1496b285192aSMauro Carvalho Chehab 	/* Update the A/V core */
1497b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, s_tuner, t);
1498b285192aSMauro Carvalho Chehab 
1499b285192aSMauro Carvalho Chehab 	return 0;
1500b285192aSMauro Carvalho Chehab }
1501b285192aSMauro Carvalho Chehab 
1502b285192aSMauro Carvalho Chehab static int vidioc_g_frequency(struct file *file, void *priv,
1503b285192aSMauro Carvalho Chehab 				struct v4l2_frequency *f)
1504b285192aSMauro Carvalho Chehab {
1505b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh = priv;
1506b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = fh->dev;
1507b285192aSMauro Carvalho Chehab 
1508b285192aSMauro Carvalho Chehab 	if (unlikely(UNSET == dev->tuner_type))
1509b285192aSMauro Carvalho Chehab 		return -EINVAL;
1510b285192aSMauro Carvalho Chehab 
1511b285192aSMauro Carvalho Chehab 	/* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1512b285192aSMauro Carvalho Chehab 	f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1513b285192aSMauro Carvalho Chehab 	f->frequency = dev->freq;
1514b285192aSMauro Carvalho Chehab 
1515b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, g_frequency, f);
1516b285192aSMauro Carvalho Chehab 
1517b285192aSMauro Carvalho Chehab 	return 0;
1518b285192aSMauro Carvalho Chehab }
1519b285192aSMauro Carvalho Chehab 
1520b285192aSMauro Carvalho Chehab static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
1521b285192aSMauro Carvalho Chehab {
1522b285192aSMauro Carvalho Chehab 	struct v4l2_control ctrl;
1523b285192aSMauro Carvalho Chehab 
1524b285192aSMauro Carvalho Chehab 	if (unlikely(UNSET == dev->tuner_type))
1525b285192aSMauro Carvalho Chehab 		return -EINVAL;
1526b285192aSMauro Carvalho Chehab 	if (unlikely(f->tuner != 0))
1527b285192aSMauro Carvalho Chehab 		return -EINVAL;
1528b285192aSMauro Carvalho Chehab 
1529b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
1530b285192aSMauro Carvalho Chehab 	dev->freq = f->frequency;
1531b285192aSMauro Carvalho Chehab 
1532b285192aSMauro Carvalho Chehab 	/* I need to mute audio here */
1533b285192aSMauro Carvalho Chehab 	ctrl.id = V4L2_CID_AUDIO_MUTE;
1534b285192aSMauro Carvalho Chehab 	ctrl.value = 1;
1535b285192aSMauro Carvalho Chehab 	cx23885_set_control(dev, &ctrl);
1536b285192aSMauro Carvalho Chehab 
1537b285192aSMauro Carvalho Chehab 	call_all(dev, tuner, s_frequency, f);
1538b285192aSMauro Carvalho Chehab 
1539b285192aSMauro Carvalho Chehab 	/* When changing channels it is required to reset TVAUDIO */
1540b285192aSMauro Carvalho Chehab 	msleep(100);
1541b285192aSMauro Carvalho Chehab 
1542b285192aSMauro Carvalho Chehab 	/* I need to unmute audio here */
1543b285192aSMauro Carvalho Chehab 	ctrl.value = 0;
1544b285192aSMauro Carvalho Chehab 	cx23885_set_control(dev, &ctrl);
1545b285192aSMauro Carvalho Chehab 
1546b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
1547b285192aSMauro Carvalho Chehab 
1548b285192aSMauro Carvalho Chehab 	return 0;
1549b285192aSMauro Carvalho Chehab }
1550b285192aSMauro Carvalho Chehab 
1551b285192aSMauro Carvalho Chehab static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
1552b285192aSMauro Carvalho Chehab 	struct v4l2_frequency *f)
1553b285192aSMauro Carvalho Chehab {
1554b285192aSMauro Carvalho Chehab 	struct v4l2_control ctrl;
1555b285192aSMauro Carvalho Chehab 	struct videobuf_dvb_frontend *vfe;
1556b285192aSMauro Carvalho Chehab 	struct dvb_frontend *fe;
1557b285192aSMauro Carvalho Chehab 
1558b285192aSMauro Carvalho Chehab 	struct analog_parameters params = {
1559b285192aSMauro Carvalho Chehab 		.mode      = V4L2_TUNER_ANALOG_TV,
1560b285192aSMauro Carvalho Chehab 		.audmode   = V4L2_TUNER_MODE_STEREO,
1561b285192aSMauro Carvalho Chehab 		.std       = dev->tvnorm,
1562b285192aSMauro Carvalho Chehab 		.frequency = f->frequency
1563b285192aSMauro Carvalho Chehab 	};
1564b285192aSMauro Carvalho Chehab 
1565b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
1566b285192aSMauro Carvalho Chehab 	dev->freq = f->frequency;
1567b285192aSMauro Carvalho Chehab 
1568b285192aSMauro Carvalho Chehab 	/* I need to mute audio here */
1569b285192aSMauro Carvalho Chehab 	ctrl.id = V4L2_CID_AUDIO_MUTE;
1570b285192aSMauro Carvalho Chehab 	ctrl.value = 1;
1571b285192aSMauro Carvalho Chehab 	cx23885_set_control(dev, &ctrl);
1572b285192aSMauro Carvalho Chehab 
1573b285192aSMauro Carvalho Chehab 	/* If HVR1850 */
1574b285192aSMauro Carvalho Chehab 	dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
1575b285192aSMauro Carvalho Chehab 		params.frequency, f->tuner, params.std);
1576b285192aSMauro Carvalho Chehab 
1577b285192aSMauro Carvalho Chehab 	vfe = videobuf_dvb_get_frontend(&dev->ts2.frontends, 1);
1578b285192aSMauro Carvalho Chehab 	if (!vfe) {
1579b285192aSMauro Carvalho Chehab 		mutex_unlock(&dev->lock);
1580b285192aSMauro Carvalho Chehab 		return -EINVAL;
1581b285192aSMauro Carvalho Chehab 	}
1582b285192aSMauro Carvalho Chehab 
1583b285192aSMauro Carvalho Chehab 	fe = vfe->dvb.frontend;
1584b285192aSMauro Carvalho Chehab 
1585b285192aSMauro Carvalho Chehab 	if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
1586b285192aSMauro Carvalho Chehab 	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
1587b285192aSMauro Carvalho Chehab 	    (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111))
1588b285192aSMauro Carvalho Chehab 		fe = &dev->ts1.analog_fe;
1589b285192aSMauro Carvalho Chehab 
1590b285192aSMauro Carvalho Chehab 	if (fe && fe->ops.tuner_ops.set_analog_params) {
1591b285192aSMauro Carvalho Chehab 		call_all(dev, core, s_std, dev->tvnorm);
1592b285192aSMauro Carvalho Chehab 		fe->ops.tuner_ops.set_analog_params(fe, &params);
1593b285192aSMauro Carvalho Chehab 	}
1594b285192aSMauro Carvalho Chehab 	else
1595b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
1596b285192aSMauro Carvalho Chehab 
1597b285192aSMauro Carvalho Chehab 	/* When changing channels it is required to reset TVAUDIO */
1598b285192aSMauro Carvalho Chehab 	msleep(100);
1599b285192aSMauro Carvalho Chehab 
1600b285192aSMauro Carvalho Chehab 	/* I need to unmute audio here */
1601b285192aSMauro Carvalho Chehab 	ctrl.value = 0;
1602b285192aSMauro Carvalho Chehab 	cx23885_set_control(dev, &ctrl);
1603b285192aSMauro Carvalho Chehab 
1604b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
1605b285192aSMauro Carvalho Chehab 
1606b285192aSMauro Carvalho Chehab 	return 0;
1607b285192aSMauro Carvalho Chehab }
1608b285192aSMauro Carvalho Chehab 
1609b285192aSMauro Carvalho Chehab int cx23885_set_frequency(struct file *file, void *priv,
1610b285192aSMauro Carvalho Chehab 	struct v4l2_frequency *f)
1611b285192aSMauro Carvalho Chehab {
1612b285192aSMauro Carvalho Chehab 	struct cx23885_fh *fh = priv;
1613b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = fh->dev;
1614b285192aSMauro Carvalho Chehab 	int ret;
1615b285192aSMauro Carvalho Chehab 
1616b285192aSMauro Carvalho Chehab 	switch (dev->board) {
1617b285192aSMauro Carvalho Chehab 	case CX23885_BOARD_HAUPPAUGE_HVR1255:
1618b285192aSMauro Carvalho Chehab 	case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1619b285192aSMauro Carvalho Chehab 	case CX23885_BOARD_HAUPPAUGE_HVR1850:
1620b285192aSMauro Carvalho Chehab 		ret = cx23885_set_freq_via_ops(dev, f);
1621b285192aSMauro Carvalho Chehab 		break;
1622b285192aSMauro Carvalho Chehab 	default:
1623b285192aSMauro Carvalho Chehab 		ret = cx23885_set_freq(dev, f);
1624b285192aSMauro Carvalho Chehab 	}
1625b285192aSMauro Carvalho Chehab 
1626b285192aSMauro Carvalho Chehab 	return ret;
1627b285192aSMauro Carvalho Chehab }
1628b285192aSMauro Carvalho Chehab 
1629b285192aSMauro Carvalho Chehab static int vidioc_s_frequency(struct file *file, void *priv,
1630b285192aSMauro Carvalho Chehab 	struct v4l2_frequency *f)
1631b285192aSMauro Carvalho Chehab {
1632b285192aSMauro Carvalho Chehab 	return cx23885_set_frequency(file, priv, f);
1633b285192aSMauro Carvalho Chehab }
1634b285192aSMauro Carvalho Chehab 
1635b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------- */
1636b285192aSMauro Carvalho Chehab 
1637b285192aSMauro Carvalho Chehab static void cx23885_vid_timeout(unsigned long data)
1638b285192aSMauro Carvalho Chehab {
1639b285192aSMauro Carvalho Chehab 	struct cx23885_dev *dev = (struct cx23885_dev *)data;
1640b285192aSMauro Carvalho Chehab 	struct cx23885_dmaqueue *q = &dev->vidq;
1641b285192aSMauro Carvalho Chehab 	struct cx23885_buffer *buf;
1642b285192aSMauro Carvalho Chehab 	unsigned long flags;
1643b285192aSMauro Carvalho Chehab 
1644b285192aSMauro Carvalho Chehab 	spin_lock_irqsave(&dev->slock, flags);
1645b285192aSMauro Carvalho Chehab 	while (!list_empty(&q->active)) {
1646b285192aSMauro Carvalho Chehab 		buf = list_entry(q->active.next,
1647b285192aSMauro Carvalho Chehab 			struct cx23885_buffer, vb.queue);
1648b285192aSMauro Carvalho Chehab 		list_del(&buf->vb.queue);
1649b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_ERROR;
1650b285192aSMauro Carvalho Chehab 		wake_up(&buf->vb.done);
1651b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n",
1652b285192aSMauro Carvalho Chehab 			dev->name, buf, buf->vb.i,
1653b285192aSMauro Carvalho Chehab 			(unsigned long)buf->risc.dma);
1654b285192aSMauro Carvalho Chehab 	}
1655b285192aSMauro Carvalho Chehab 	cx23885_restart_video_queue(dev, q);
1656b285192aSMauro Carvalho Chehab 	spin_unlock_irqrestore(&dev->slock, flags);
1657b285192aSMauro Carvalho Chehab }
1658b285192aSMauro Carvalho Chehab 
1659b285192aSMauro Carvalho Chehab int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1660b285192aSMauro Carvalho Chehab {
1661b285192aSMauro Carvalho Chehab 	u32 mask, count;
1662b285192aSMauro Carvalho Chehab 	int handled = 0;
1663b285192aSMauro Carvalho Chehab 
1664b285192aSMauro Carvalho Chehab 	mask   = cx_read(VID_A_INT_MSK);
1665b285192aSMauro Carvalho Chehab 	if (0 == (status & mask))
1666b285192aSMauro Carvalho Chehab 		return handled;
1667b285192aSMauro Carvalho Chehab 
1668b285192aSMauro Carvalho Chehab 	cx_write(VID_A_INT_STAT, status);
1669b285192aSMauro Carvalho Chehab 
1670b285192aSMauro Carvalho Chehab 	/* risc op code error, fifo overflow or line sync detection error */
1671b285192aSMauro Carvalho Chehab 	if ((status & VID_BC_MSK_OPC_ERR) ||
1672b285192aSMauro Carvalho Chehab 		(status & VID_BC_MSK_SYNC) ||
1673b285192aSMauro Carvalho Chehab 		(status & VID_BC_MSK_OF)) {
1674b285192aSMauro Carvalho Chehab 
1675b285192aSMauro Carvalho Chehab 		if (status & VID_BC_MSK_OPC_ERR) {
1676b285192aSMauro Carvalho Chehab 			dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1677b285192aSMauro Carvalho Chehab 				VID_BC_MSK_OPC_ERR);
1678b285192aSMauro Carvalho Chehab 			printk(KERN_WARNING "%s: video risc op code error\n",
1679b285192aSMauro Carvalho Chehab 				dev->name);
1680b285192aSMauro Carvalho Chehab 			cx23885_sram_channel_dump(dev,
1681b285192aSMauro Carvalho Chehab 				&dev->sram_channels[SRAM_CH01]);
1682b285192aSMauro Carvalho Chehab 		}
1683b285192aSMauro Carvalho Chehab 
1684b285192aSMauro Carvalho Chehab 		if (status & VID_BC_MSK_SYNC)
1685b285192aSMauro Carvalho Chehab 			dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
1686b285192aSMauro Carvalho Chehab 				"video lines miss-match\n",
1687b285192aSMauro Carvalho Chehab 				VID_BC_MSK_SYNC);
1688b285192aSMauro Carvalho Chehab 
1689b285192aSMauro Carvalho Chehab 		if (status & VID_BC_MSK_OF)
1690b285192aSMauro Carvalho Chehab 			dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1691b285192aSMauro Carvalho Chehab 				VID_BC_MSK_OF);
1692b285192aSMauro Carvalho Chehab 
1693b285192aSMauro Carvalho Chehab 	}
1694b285192aSMauro Carvalho Chehab 
1695b285192aSMauro Carvalho Chehab 	/* Video */
1696b285192aSMauro Carvalho Chehab 	if (status & VID_BC_MSK_RISCI1) {
1697b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
1698b285192aSMauro Carvalho Chehab 		count = cx_read(VID_A_GPCNT);
1699b285192aSMauro Carvalho Chehab 		cx23885_video_wakeup(dev, &dev->vidq, count);
1700b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
1701b285192aSMauro Carvalho Chehab 		handled++;
1702b285192aSMauro Carvalho Chehab 	}
1703b285192aSMauro Carvalho Chehab 	if (status & VID_BC_MSK_RISCI2) {
1704b285192aSMauro Carvalho Chehab 		dprintk(2, "stopper video\n");
1705b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
1706b285192aSMauro Carvalho Chehab 		cx23885_restart_video_queue(dev, &dev->vidq);
1707b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
1708b285192aSMauro Carvalho Chehab 		handled++;
1709b285192aSMauro Carvalho Chehab 	}
1710b285192aSMauro Carvalho Chehab 
1711b285192aSMauro Carvalho Chehab 	/* Allow the VBI framework to process it's payload */
1712b285192aSMauro Carvalho Chehab 	handled += cx23885_vbi_irq(dev, status);
1713b285192aSMauro Carvalho Chehab 
1714b285192aSMauro Carvalho Chehab 	return handled;
1715b285192aSMauro Carvalho Chehab }
1716b285192aSMauro Carvalho Chehab 
1717b285192aSMauro Carvalho Chehab /* ----------------------------------------------------------- */
1718b285192aSMauro Carvalho Chehab /* exported stuff                                              */
1719b285192aSMauro Carvalho Chehab 
1720b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops = {
1721b285192aSMauro Carvalho Chehab 	.owner	       = THIS_MODULE,
1722b285192aSMauro Carvalho Chehab 	.open	       = video_open,
1723b285192aSMauro Carvalho Chehab 	.release       = video_release,
1724b285192aSMauro Carvalho Chehab 	.read	       = video_read,
1725b285192aSMauro Carvalho Chehab 	.poll          = video_poll,
1726b285192aSMauro Carvalho Chehab 	.mmap	       = video_mmap,
1727b285192aSMauro Carvalho Chehab 	.ioctl	       = video_ioctl2,
1728b285192aSMauro Carvalho Chehab };
1729b285192aSMauro Carvalho Chehab 
1730b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops = {
1731b285192aSMauro Carvalho Chehab 	.vidioc_querycap      = vidioc_querycap,
1732b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1733b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1734b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1735b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1736b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vbi_cap     = cx23885_vbi_fmt,
1737b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vbi_cap   = cx23885_vbi_fmt,
1738b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vbi_cap     = cx23885_vbi_fmt,
1739b285192aSMauro Carvalho Chehab 	.vidioc_reqbufs       = vidioc_reqbufs,
1740b285192aSMauro Carvalho Chehab 	.vidioc_querybuf      = vidioc_querybuf,
1741b285192aSMauro Carvalho Chehab 	.vidioc_qbuf          = vidioc_qbuf,
1742b285192aSMauro Carvalho Chehab 	.vidioc_dqbuf         = vidioc_dqbuf,
1743b285192aSMauro Carvalho Chehab 	.vidioc_s_std         = vidioc_s_std,
1744b285192aSMauro Carvalho Chehab 	.vidioc_g_std         = vidioc_g_std,
1745b285192aSMauro Carvalho Chehab 	.vidioc_querystd      = vidioc_g_std,
1746b285192aSMauro Carvalho Chehab 	.vidioc_enum_input    = vidioc_enum_input,
1747b285192aSMauro Carvalho Chehab 	.vidioc_g_input       = vidioc_g_input,
1748b285192aSMauro Carvalho Chehab 	.vidioc_s_input       = vidioc_s_input,
1749b285192aSMauro Carvalho Chehab 	.vidioc_log_status    = vidioc_log_status,
1750b285192aSMauro Carvalho Chehab 	.vidioc_queryctrl     = vidioc_queryctrl,
1751b285192aSMauro Carvalho Chehab 	.vidioc_g_ctrl        = vidioc_g_ctrl,
1752b285192aSMauro Carvalho Chehab 	.vidioc_s_ctrl        = vidioc_s_ctrl,
1753b285192aSMauro Carvalho Chehab 	.vidioc_streamon      = vidioc_streamon,
1754b285192aSMauro Carvalho Chehab 	.vidioc_streamoff     = vidioc_streamoff,
1755b285192aSMauro Carvalho Chehab 	.vidioc_g_tuner       = vidioc_g_tuner,
1756b285192aSMauro Carvalho Chehab 	.vidioc_s_tuner       = vidioc_s_tuner,
1757b285192aSMauro Carvalho Chehab 	.vidioc_g_frequency   = vidioc_g_frequency,
1758b285192aSMauro Carvalho Chehab 	.vidioc_s_frequency   = vidioc_s_frequency,
1759b285192aSMauro Carvalho Chehab 	.vidioc_g_chip_ident  = cx23885_g_chip_ident,
1760b285192aSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
1761b285192aSMauro Carvalho Chehab 	.vidioc_g_register    = cx23885_g_register,
1762b285192aSMauro Carvalho Chehab 	.vidioc_s_register    = cx23885_s_register,
1763b285192aSMauro Carvalho Chehab #endif
1764b285192aSMauro Carvalho Chehab 	.vidioc_enumaudio     = vidioc_enum_audinput,
1765b285192aSMauro Carvalho Chehab 	.vidioc_g_audio       = vidioc_g_audinput,
1766b285192aSMauro Carvalho Chehab 	.vidioc_s_audio       = vidioc_s_audinput,
1767b285192aSMauro Carvalho Chehab };
1768b285192aSMauro Carvalho Chehab 
1769b285192aSMauro Carvalho Chehab static struct video_device cx23885_vbi_template;
1770b285192aSMauro Carvalho Chehab static struct video_device cx23885_video_template = {
1771b285192aSMauro Carvalho Chehab 	.name                 = "cx23885-video",
1772b285192aSMauro Carvalho Chehab 	.fops                 = &video_fops,
1773b285192aSMauro Carvalho Chehab 	.ioctl_ops 	      = &video_ioctl_ops,
1774b285192aSMauro Carvalho Chehab 	.tvnorms              = CX23885_NORMS,
1775b285192aSMauro Carvalho Chehab 	.current_norm         = V4L2_STD_NTSC_M,
1776b285192aSMauro Carvalho Chehab };
1777b285192aSMauro Carvalho Chehab 
1778b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations radio_fops = {
1779b285192aSMauro Carvalho Chehab 	.owner         = THIS_MODULE,
1780b285192aSMauro Carvalho Chehab 	.open          = video_open,
1781b285192aSMauro Carvalho Chehab 	.release       = video_release,
1782b285192aSMauro Carvalho Chehab 	.ioctl         = video_ioctl2,
1783b285192aSMauro Carvalho Chehab };
1784b285192aSMauro Carvalho Chehab 
1785b285192aSMauro Carvalho Chehab 
1786b285192aSMauro Carvalho Chehab void cx23885_video_unregister(struct cx23885_dev *dev)
1787b285192aSMauro Carvalho Chehab {
1788b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1789b285192aSMauro Carvalho Chehab 	cx23885_irq_remove(dev, 0x01);
1790b285192aSMauro Carvalho Chehab 
1791b285192aSMauro Carvalho Chehab 	if (dev->vbi_dev) {
1792b285192aSMauro Carvalho Chehab 		if (video_is_registered(dev->vbi_dev))
1793b285192aSMauro Carvalho Chehab 			video_unregister_device(dev->vbi_dev);
1794b285192aSMauro Carvalho Chehab 		else
1795b285192aSMauro Carvalho Chehab 			video_device_release(dev->vbi_dev);
1796b285192aSMauro Carvalho Chehab 		dev->vbi_dev = NULL;
1797b285192aSMauro Carvalho Chehab 		btcx_riscmem_free(dev->pci, &dev->vbiq.stopper);
1798b285192aSMauro Carvalho Chehab 	}
1799b285192aSMauro Carvalho Chehab 	if (dev->video_dev) {
1800b285192aSMauro Carvalho Chehab 		if (video_is_registered(dev->video_dev))
1801b285192aSMauro Carvalho Chehab 			video_unregister_device(dev->video_dev);
1802b285192aSMauro Carvalho Chehab 		else
1803b285192aSMauro Carvalho Chehab 			video_device_release(dev->video_dev);
1804b285192aSMauro Carvalho Chehab 		dev->video_dev = NULL;
1805b285192aSMauro Carvalho Chehab 
1806b285192aSMauro Carvalho Chehab 		btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1807b285192aSMauro Carvalho Chehab 	}
1808b285192aSMauro Carvalho Chehab 
1809b285192aSMauro Carvalho Chehab 	if (dev->audio_dev)
1810b285192aSMauro Carvalho Chehab 		cx23885_audio_unregister(dev);
1811b285192aSMauro Carvalho Chehab }
1812b285192aSMauro Carvalho Chehab 
1813b285192aSMauro Carvalho Chehab int cx23885_video_register(struct cx23885_dev *dev)
1814b285192aSMauro Carvalho Chehab {
1815b285192aSMauro Carvalho Chehab 	int err;
1816b285192aSMauro Carvalho Chehab 
1817b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1818b285192aSMauro Carvalho Chehab 	spin_lock_init(&dev->slock);
1819b285192aSMauro Carvalho Chehab 
1820b285192aSMauro Carvalho Chehab 	/* Initialize VBI template */
1821b285192aSMauro Carvalho Chehab 	memcpy(&cx23885_vbi_template, &cx23885_video_template,
1822b285192aSMauro Carvalho Chehab 		sizeof(cx23885_vbi_template));
1823b285192aSMauro Carvalho Chehab 	strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1824b285192aSMauro Carvalho Chehab 
1825b285192aSMauro Carvalho Chehab 	dev->tvnorm = cx23885_video_template.current_norm;
1826b285192aSMauro Carvalho Chehab 
1827b285192aSMauro Carvalho Chehab 	/* init video dma queues */
1828b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->vidq.active);
1829b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->vidq.queued);
1830b285192aSMauro Carvalho Chehab 	dev->vidq.timeout.function = cx23885_vid_timeout;
1831b285192aSMauro Carvalho Chehab 	dev->vidq.timeout.data = (unsigned long)dev;
1832b285192aSMauro Carvalho Chehab 	init_timer(&dev->vidq.timeout);
1833b285192aSMauro Carvalho Chehab 	cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1834b285192aSMauro Carvalho Chehab 		VID_A_DMA_CTL, 0x11, 0x00);
1835b285192aSMauro Carvalho Chehab 
1836b285192aSMauro Carvalho Chehab 	/* init vbi dma queues */
1837b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->vbiq.active);
1838b285192aSMauro Carvalho Chehab 	INIT_LIST_HEAD(&dev->vbiq.queued);
1839b285192aSMauro Carvalho Chehab 	dev->vbiq.timeout.function = cx23885_vbi_timeout;
1840b285192aSMauro Carvalho Chehab 	dev->vbiq.timeout.data = (unsigned long)dev;
1841b285192aSMauro Carvalho Chehab 	init_timer(&dev->vbiq.timeout);
1842b285192aSMauro Carvalho Chehab 	cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper,
1843b285192aSMauro Carvalho Chehab 		VID_A_DMA_CTL, 0x22, 0x00);
1844b285192aSMauro Carvalho Chehab 
1845b285192aSMauro Carvalho Chehab 	cx23885_irq_add_enable(dev, 0x01);
1846b285192aSMauro Carvalho Chehab 
1847b285192aSMauro Carvalho Chehab 	if ((TUNER_ABSENT != dev->tuner_type) &&
1848b285192aSMauro Carvalho Chehab 			((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1849b285192aSMauro Carvalho Chehab 		struct v4l2_subdev *sd = NULL;
1850b285192aSMauro Carvalho Chehab 
1851b285192aSMauro Carvalho Chehab 		if (dev->tuner_addr)
1852b285192aSMauro Carvalho Chehab 			sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1853b285192aSMauro Carvalho Chehab 				&dev->i2c_bus[dev->tuner_bus].i2c_adap,
1854b285192aSMauro Carvalho Chehab 				"tuner", dev->tuner_addr, NULL);
1855b285192aSMauro Carvalho Chehab 		else
1856b285192aSMauro Carvalho Chehab 			sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1857b285192aSMauro Carvalho Chehab 				&dev->i2c_bus[dev->tuner_bus].i2c_adap,
1858b285192aSMauro Carvalho Chehab 				"tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1859b285192aSMauro Carvalho Chehab 		if (sd) {
1860b285192aSMauro Carvalho Chehab 			struct tuner_setup tun_setup;
1861b285192aSMauro Carvalho Chehab 
1862b285192aSMauro Carvalho Chehab 			memset(&tun_setup, 0, sizeof(tun_setup));
1863b285192aSMauro Carvalho Chehab 			tun_setup.mode_mask = T_ANALOG_TV;
1864b285192aSMauro Carvalho Chehab 			tun_setup.type = dev->tuner_type;
1865b285192aSMauro Carvalho Chehab 			tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1866b285192aSMauro Carvalho Chehab 			tun_setup.tuner_callback = cx23885_tuner_callback;
1867b285192aSMauro Carvalho Chehab 
1868b285192aSMauro Carvalho Chehab 			v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1869b285192aSMauro Carvalho Chehab 
1870b285192aSMauro Carvalho Chehab 			if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
1871b285192aSMauro Carvalho Chehab 				struct xc2028_ctrl ctrl = {
1872b285192aSMauro Carvalho Chehab 					.fname = XC2028_DEFAULT_FIRMWARE,
1873b285192aSMauro Carvalho Chehab 					.max_len = 64
1874b285192aSMauro Carvalho Chehab 				};
1875b285192aSMauro Carvalho Chehab 				struct v4l2_priv_tun_config cfg = {
1876b285192aSMauro Carvalho Chehab 					.tuner = dev->tuner_type,
1877b285192aSMauro Carvalho Chehab 					.priv = &ctrl
1878b285192aSMauro Carvalho Chehab 				};
1879b285192aSMauro Carvalho Chehab 				v4l2_subdev_call(sd, tuner, s_config, &cfg);
1880b285192aSMauro Carvalho Chehab 			}
1881b285192aSMauro Carvalho Chehab 		}
1882b285192aSMauro Carvalho Chehab 	}
1883b285192aSMauro Carvalho Chehab 
1884b285192aSMauro Carvalho Chehab 	/* register Video device */
1885b285192aSMauro Carvalho Chehab 	dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1886b285192aSMauro Carvalho Chehab 		&cx23885_video_template, "video");
1887b285192aSMauro Carvalho Chehab 	err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1888b285192aSMauro Carvalho Chehab 				    video_nr[dev->nr]);
1889b285192aSMauro Carvalho Chehab 	if (err < 0) {
1890b285192aSMauro Carvalho Chehab 		printk(KERN_INFO "%s: can't register video device\n",
1891b285192aSMauro Carvalho Chehab 			dev->name);
1892b285192aSMauro Carvalho Chehab 		goto fail_unreg;
1893b285192aSMauro Carvalho Chehab 	}
1894b285192aSMauro Carvalho Chehab 	printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1895b285192aSMauro Carvalho Chehab 	       dev->name, video_device_node_name(dev->video_dev));
1896b285192aSMauro Carvalho Chehab 
1897b285192aSMauro Carvalho Chehab 	/* register VBI device */
1898b285192aSMauro Carvalho Chehab 	dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1899b285192aSMauro Carvalho Chehab 		&cx23885_vbi_template, "vbi");
1900b285192aSMauro Carvalho Chehab 	err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1901b285192aSMauro Carvalho Chehab 				    vbi_nr[dev->nr]);
1902b285192aSMauro Carvalho Chehab 	if (err < 0) {
1903b285192aSMauro Carvalho Chehab 		printk(KERN_INFO "%s: can't register vbi device\n",
1904b285192aSMauro Carvalho Chehab 			dev->name);
1905b285192aSMauro Carvalho Chehab 		goto fail_unreg;
1906b285192aSMauro Carvalho Chehab 	}
1907b285192aSMauro Carvalho Chehab 	printk(KERN_INFO "%s: registered device %s\n",
1908b285192aSMauro Carvalho Chehab 	       dev->name, video_device_node_name(dev->vbi_dev));
1909b285192aSMauro Carvalho Chehab 
1910b285192aSMauro Carvalho Chehab 	/* Register ALSA audio device */
1911b285192aSMauro Carvalho Chehab 	dev->audio_dev = cx23885_audio_register(dev);
1912b285192aSMauro Carvalho Chehab 
1913b285192aSMauro Carvalho Chehab 	/* initial device configuration */
1914b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
1915b285192aSMauro Carvalho Chehab 	cx23885_set_tvnorm(dev, dev->tvnorm);
1916b285192aSMauro Carvalho Chehab 	init_controls(dev);
1917b285192aSMauro Carvalho Chehab 	cx23885_video_mux(dev, 0);
1918b285192aSMauro Carvalho Chehab 	cx23885_audio_mux(dev, 0);
1919b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
1920b285192aSMauro Carvalho Chehab 
1921b285192aSMauro Carvalho Chehab 	return 0;
1922b285192aSMauro Carvalho Chehab 
1923b285192aSMauro Carvalho Chehab fail_unreg:
1924b285192aSMauro Carvalho Chehab 	cx23885_video_unregister(dev);
1925b285192aSMauro Carvalho Chehab 	return err;
1926b285192aSMauro Carvalho Chehab }
1927b285192aSMauro Carvalho Chehab 
1928