1b285192aSMauro Carvalho Chehab /*
2b285192aSMauro Carvalho Chehab  *  Driver for the NXP SAA7164 PCIe bridge
3b285192aSMauro Carvalho Chehab  *
4b285192aSMauro Carvalho Chehab  *  Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
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 "saa7164.h"
23b285192aSMauro Carvalho Chehab 
24b285192aSMauro Carvalho Chehab #define ENCODER_MAX_BITRATE 6500000
25b285192aSMauro Carvalho Chehab #define ENCODER_MIN_BITRATE 1000000
26b285192aSMauro Carvalho Chehab #define ENCODER_DEF_BITRATE 5000000
27b285192aSMauro Carvalho Chehab 
28b285192aSMauro Carvalho Chehab static struct saa7164_tvnorm saa7164_tvnorms[] = {
29b285192aSMauro Carvalho Chehab 	{
30b285192aSMauro Carvalho Chehab 		.name      = "NTSC-M",
31b285192aSMauro Carvalho Chehab 		.id        = V4L2_STD_NTSC_M,
32b285192aSMauro Carvalho Chehab 	}, {
33b285192aSMauro Carvalho Chehab 		.name      = "NTSC-JP",
34b285192aSMauro Carvalho Chehab 		.id        = V4L2_STD_NTSC_M_JP,
35b285192aSMauro Carvalho Chehab 	}
36b285192aSMauro Carvalho Chehab };
37b285192aSMauro Carvalho Chehab 
38b285192aSMauro Carvalho Chehab static const u32 saa7164_v4l2_ctrls[] = {
39b285192aSMauro Carvalho Chehab 	V4L2_CID_BRIGHTNESS,
40b285192aSMauro Carvalho Chehab 	V4L2_CID_CONTRAST,
41b285192aSMauro Carvalho Chehab 	V4L2_CID_SATURATION,
42b285192aSMauro Carvalho Chehab 	V4L2_CID_HUE,
43b285192aSMauro Carvalho Chehab 	V4L2_CID_AUDIO_VOLUME,
44b285192aSMauro Carvalho Chehab 	V4L2_CID_SHARPNESS,
45b285192aSMauro Carvalho Chehab 	V4L2_CID_MPEG_STREAM_TYPE,
46b285192aSMauro Carvalho Chehab 	V4L2_CID_MPEG_VIDEO_ASPECT,
47b285192aSMauro Carvalho Chehab 	V4L2_CID_MPEG_VIDEO_B_FRAMES,
48b285192aSMauro Carvalho Chehab 	V4L2_CID_MPEG_VIDEO_GOP_SIZE,
49b285192aSMauro Carvalho Chehab 	V4L2_CID_MPEG_AUDIO_MUTE,
50b285192aSMauro Carvalho Chehab 	V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
51b285192aSMauro Carvalho Chehab 	V4L2_CID_MPEG_VIDEO_BITRATE,
52b285192aSMauro Carvalho Chehab 	V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
53b285192aSMauro Carvalho Chehab 	0
54b285192aSMauro Carvalho Chehab };
55b285192aSMauro Carvalho Chehab 
56b285192aSMauro Carvalho Chehab /* Take the encoder configuration form the port struct and
57b285192aSMauro Carvalho Chehab  * flush it to the hardware.
58b285192aSMauro Carvalho Chehab  */
59b285192aSMauro Carvalho Chehab static void saa7164_encoder_configure(struct saa7164_port *port)
60b285192aSMauro Carvalho Chehab {
61b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
62b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
63b285192aSMauro Carvalho Chehab 
64b285192aSMauro Carvalho Chehab 	port->encoder_params.width = port->width;
65b285192aSMauro Carvalho Chehab 	port->encoder_params.height = port->height;
66b285192aSMauro Carvalho Chehab 	port->encoder_params.is_50hz =
67b285192aSMauro Carvalho Chehab 		(port->encodernorm.id & V4L2_STD_625_50) != 0;
68b285192aSMauro Carvalho Chehab 
69b285192aSMauro Carvalho Chehab 	/* Set up the DIF (enable it) for analog mode by default */
70b285192aSMauro Carvalho Chehab 	saa7164_api_initialize_dif(port);
71b285192aSMauro Carvalho Chehab 
72b285192aSMauro Carvalho Chehab 	/* Configure the correct video standard */
73b285192aSMauro Carvalho Chehab 	saa7164_api_configure_dif(port, port->encodernorm.id);
74b285192aSMauro Carvalho Chehab 
75b285192aSMauro Carvalho Chehab 	/* Ensure the audio decoder is correct configured */
76b285192aSMauro Carvalho Chehab 	saa7164_api_set_audio_std(port);
77b285192aSMauro Carvalho Chehab }
78b285192aSMauro Carvalho Chehab 
79b285192aSMauro Carvalho Chehab static int saa7164_encoder_buffers_dealloc(struct saa7164_port *port)
80b285192aSMauro Carvalho Chehab {
81b285192aSMauro Carvalho Chehab 	struct list_head *c, *n, *p, *q, *l, *v;
82b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
83b285192aSMauro Carvalho Chehab 	struct saa7164_buffer *buf;
84b285192aSMauro Carvalho Chehab 	struct saa7164_user_buffer *ubuf;
85b285192aSMauro Carvalho Chehab 
86b285192aSMauro Carvalho Chehab 	/* Remove any allocated buffers */
87b285192aSMauro Carvalho Chehab 	mutex_lock(&port->dmaqueue_lock);
88b285192aSMauro Carvalho Chehab 
89b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s(port=%d) dmaqueue\n", __func__, port->nr);
90b285192aSMauro Carvalho Chehab 	list_for_each_safe(c, n, &port->dmaqueue.list) {
91b285192aSMauro Carvalho Chehab 		buf = list_entry(c, struct saa7164_buffer, list);
92b285192aSMauro Carvalho Chehab 		list_del(c);
93b285192aSMauro Carvalho Chehab 		saa7164_buffer_dealloc(buf);
94b285192aSMauro Carvalho Chehab 	}
95b285192aSMauro Carvalho Chehab 
96b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s(port=%d) used\n", __func__, port->nr);
97b285192aSMauro Carvalho Chehab 	list_for_each_safe(p, q, &port->list_buf_used.list) {
98b285192aSMauro Carvalho Chehab 		ubuf = list_entry(p, struct saa7164_user_buffer, list);
99b285192aSMauro Carvalho Chehab 		list_del(p);
100b285192aSMauro Carvalho Chehab 		saa7164_buffer_dealloc_user(ubuf);
101b285192aSMauro Carvalho Chehab 	}
102b285192aSMauro Carvalho Chehab 
103b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s(port=%d) free\n", __func__, port->nr);
104b285192aSMauro Carvalho Chehab 	list_for_each_safe(l, v, &port->list_buf_free.list) {
105b285192aSMauro Carvalho Chehab 		ubuf = list_entry(l, struct saa7164_user_buffer, list);
106b285192aSMauro Carvalho Chehab 		list_del(l);
107b285192aSMauro Carvalho Chehab 		saa7164_buffer_dealloc_user(ubuf);
108b285192aSMauro Carvalho Chehab 	}
109b285192aSMauro Carvalho Chehab 
110b285192aSMauro Carvalho Chehab 	mutex_unlock(&port->dmaqueue_lock);
111b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
112b285192aSMauro Carvalho Chehab 
113b285192aSMauro Carvalho Chehab 	return 0;
114b285192aSMauro Carvalho Chehab }
115b285192aSMauro Carvalho Chehab 
116b285192aSMauro Carvalho Chehab /* Dynamic buffer switch at encoder start time */
117b285192aSMauro Carvalho Chehab static int saa7164_encoder_buffers_alloc(struct saa7164_port *port)
118b285192aSMauro Carvalho Chehab {
119b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
120b285192aSMauro Carvalho Chehab 	struct saa7164_buffer *buf;
121b285192aSMauro Carvalho Chehab 	struct saa7164_user_buffer *ubuf;
122b285192aSMauro Carvalho Chehab 	struct tmHWStreamParameters *params = &port->hw_streamingparams;
123b285192aSMauro Carvalho Chehab 	int result = -ENODEV, i;
124b285192aSMauro Carvalho Chehab 	int len = 0;
125b285192aSMauro Carvalho Chehab 
126b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
127b285192aSMauro Carvalho Chehab 
128b285192aSMauro Carvalho Chehab 	if (port->encoder_params.stream_type ==
129b285192aSMauro Carvalho Chehab 		V4L2_MPEG_STREAM_TYPE_MPEG2_PS) {
130b285192aSMauro Carvalho Chehab 		dprintk(DBGLVL_ENC,
131b285192aSMauro Carvalho Chehab 			"%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_PS\n",
132b285192aSMauro Carvalho Chehab 			__func__);
133b285192aSMauro Carvalho Chehab 		params->samplesperline = 128;
134b285192aSMauro Carvalho Chehab 		params->numberoflines = 256;
135b285192aSMauro Carvalho Chehab 		params->pitch = 128;
136b285192aSMauro Carvalho Chehab 		params->numpagetables = 2 +
137b285192aSMauro Carvalho Chehab 			((SAA7164_PS_NUMBER_OF_LINES * 128) / PAGE_SIZE);
138b285192aSMauro Carvalho Chehab 	} else
139b285192aSMauro Carvalho Chehab 	if (port->encoder_params.stream_type ==
140b285192aSMauro Carvalho Chehab 		V4L2_MPEG_STREAM_TYPE_MPEG2_TS) {
141b285192aSMauro Carvalho Chehab 		dprintk(DBGLVL_ENC,
142b285192aSMauro Carvalho Chehab 			"%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_TS\n",
143b285192aSMauro Carvalho Chehab 			__func__);
144b285192aSMauro Carvalho Chehab 		params->samplesperline = 188;
145b285192aSMauro Carvalho Chehab 		params->numberoflines = 312;
146b285192aSMauro Carvalho Chehab 		params->pitch = 188;
147b285192aSMauro Carvalho Chehab 		params->numpagetables = 2 +
148b285192aSMauro Carvalho Chehab 			((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
149b285192aSMauro Carvalho Chehab 	} else
150b285192aSMauro Carvalho Chehab 		BUG();
151b285192aSMauro Carvalho Chehab 
152b285192aSMauro Carvalho Chehab 	/* Init and establish defaults */
153b285192aSMauro Carvalho Chehab 	params->bitspersample = 8;
154b285192aSMauro Carvalho Chehab 	params->linethreshold = 0;
155b285192aSMauro Carvalho Chehab 	params->pagetablelistvirt = NULL;
156b285192aSMauro Carvalho Chehab 	params->pagetablelistphys = NULL;
157b285192aSMauro Carvalho Chehab 	params->numpagetableentries = port->hwcfg.buffercount;
158b285192aSMauro Carvalho Chehab 
159b285192aSMauro Carvalho Chehab 	/* Allocate the PCI resources, buffers (hard) */
160b285192aSMauro Carvalho Chehab 	for (i = 0; i < port->hwcfg.buffercount; i++) {
161b285192aSMauro Carvalho Chehab 		buf = saa7164_buffer_alloc(port,
162b285192aSMauro Carvalho Chehab 			params->numberoflines *
163b285192aSMauro Carvalho Chehab 			params->pitch);
164b285192aSMauro Carvalho Chehab 
165b285192aSMauro Carvalho Chehab 		if (!buf) {
166b285192aSMauro Carvalho Chehab 			printk(KERN_ERR "%s() failed "
167b285192aSMauro Carvalho Chehab 			       "(errno = %d), unable to allocate buffer\n",
168b285192aSMauro Carvalho Chehab 				__func__, result);
169b285192aSMauro Carvalho Chehab 			result = -ENOMEM;
170b285192aSMauro Carvalho Chehab 			goto failed;
171b285192aSMauro Carvalho Chehab 		} else {
172b285192aSMauro Carvalho Chehab 
173b285192aSMauro Carvalho Chehab 			mutex_lock(&port->dmaqueue_lock);
174b285192aSMauro Carvalho Chehab 			list_add_tail(&buf->list, &port->dmaqueue.list);
175b285192aSMauro Carvalho Chehab 			mutex_unlock(&port->dmaqueue_lock);
176b285192aSMauro Carvalho Chehab 
177b285192aSMauro Carvalho Chehab 		}
178b285192aSMauro Carvalho Chehab 	}
179b285192aSMauro Carvalho Chehab 
180b285192aSMauro Carvalho Chehab 	/* Allocate some kernel buffers for copying
181b285192aSMauro Carvalho Chehab 	 * to userpsace.
182b285192aSMauro Carvalho Chehab 	 */
183b285192aSMauro Carvalho Chehab 	len = params->numberoflines * params->pitch;
184b285192aSMauro Carvalho Chehab 
185b285192aSMauro Carvalho Chehab 	if (encoder_buffers < 16)
186b285192aSMauro Carvalho Chehab 		encoder_buffers = 16;
187b285192aSMauro Carvalho Chehab 	if (encoder_buffers > 512)
188b285192aSMauro Carvalho Chehab 		encoder_buffers = 512;
189b285192aSMauro Carvalho Chehab 
190b285192aSMauro Carvalho Chehab 	for (i = 0; i < encoder_buffers; i++) {
191b285192aSMauro Carvalho Chehab 
192b285192aSMauro Carvalho Chehab 		ubuf = saa7164_buffer_alloc_user(dev, len);
193b285192aSMauro Carvalho Chehab 		if (ubuf) {
194b285192aSMauro Carvalho Chehab 			mutex_lock(&port->dmaqueue_lock);
195b285192aSMauro Carvalho Chehab 			list_add_tail(&ubuf->list, &port->list_buf_free.list);
196b285192aSMauro Carvalho Chehab 			mutex_unlock(&port->dmaqueue_lock);
197b285192aSMauro Carvalho Chehab 		}
198b285192aSMauro Carvalho Chehab 
199b285192aSMauro Carvalho Chehab 	}
200b285192aSMauro Carvalho Chehab 
201b285192aSMauro Carvalho Chehab 	result = 0;
202b285192aSMauro Carvalho Chehab 
203b285192aSMauro Carvalho Chehab failed:
204b285192aSMauro Carvalho Chehab 	return result;
205b285192aSMauro Carvalho Chehab }
206b285192aSMauro Carvalho Chehab 
207b285192aSMauro Carvalho Chehab static int saa7164_encoder_initialize(struct saa7164_port *port)
208b285192aSMauro Carvalho Chehab {
209b285192aSMauro Carvalho Chehab 	saa7164_encoder_configure(port);
210b285192aSMauro Carvalho Chehab 	return 0;
211b285192aSMauro Carvalho Chehab }
212b285192aSMauro Carvalho Chehab 
213b285192aSMauro Carvalho Chehab /* -- V4L2 --------------------------------------------------------- */
214b285192aSMauro Carvalho Chehab static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
215b285192aSMauro Carvalho Chehab {
216b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
217b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
218b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
219b285192aSMauro Carvalho Chehab 	unsigned int i;
220b285192aSMauro Carvalho Chehab 
221b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s(id=0x%x)\n", __func__, (u32)*id);
222b285192aSMauro Carvalho Chehab 
223b285192aSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) {
224b285192aSMauro Carvalho Chehab 		if (*id & saa7164_tvnorms[i].id)
225b285192aSMauro Carvalho Chehab 			break;
226b285192aSMauro Carvalho Chehab 	}
227b285192aSMauro Carvalho Chehab 	if (i == ARRAY_SIZE(saa7164_tvnorms))
228b285192aSMauro Carvalho Chehab 		return -EINVAL;
229b285192aSMauro Carvalho Chehab 
230b285192aSMauro Carvalho Chehab 	port->encodernorm = saa7164_tvnorms[i];
231b285192aSMauro Carvalho Chehab 
232b285192aSMauro Carvalho Chehab 	/* Update the audio decoder while is not running in
233b285192aSMauro Carvalho Chehab 	 * auto detect mode.
234b285192aSMauro Carvalho Chehab 	 */
235b285192aSMauro Carvalho Chehab 	saa7164_api_set_audio_std(port);
236b285192aSMauro Carvalho Chehab 
237b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s(id=0x%x) OK\n", __func__, (u32)*id);
238b285192aSMauro Carvalho Chehab 
239b285192aSMauro Carvalho Chehab 	return 0;
240b285192aSMauro Carvalho Chehab }
241b285192aSMauro Carvalho Chehab 
242b285192aSMauro Carvalho Chehab static int vidioc_enum_input(struct file *file, void *priv,
243b285192aSMauro Carvalho Chehab 	struct v4l2_input *i)
244b285192aSMauro Carvalho Chehab {
245b285192aSMauro Carvalho Chehab 	int n;
246b285192aSMauro Carvalho Chehab 
247b285192aSMauro Carvalho Chehab 	char *inputs[] = { "tuner", "composite", "svideo", "aux",
248b285192aSMauro Carvalho Chehab 		"composite 2", "svideo 2", "aux 2" };
249b285192aSMauro Carvalho Chehab 
250b285192aSMauro Carvalho Chehab 	if (i->index >= 7)
251b285192aSMauro Carvalho Chehab 		return -EINVAL;
252b285192aSMauro Carvalho Chehab 
253b285192aSMauro Carvalho Chehab 	strcpy(i->name, inputs[i->index]);
254b285192aSMauro Carvalho Chehab 
255b285192aSMauro Carvalho Chehab 	if (i->index == 0)
256b285192aSMauro Carvalho Chehab 		i->type = V4L2_INPUT_TYPE_TUNER;
257b285192aSMauro Carvalho Chehab 	else
258b285192aSMauro Carvalho Chehab 		i->type  = V4L2_INPUT_TYPE_CAMERA;
259b285192aSMauro Carvalho Chehab 
260b285192aSMauro Carvalho Chehab 	for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++)
261b285192aSMauro Carvalho Chehab 		i->std |= saa7164_tvnorms[n].id;
262b285192aSMauro Carvalho Chehab 
263b285192aSMauro Carvalho Chehab 	return 0;
264b285192aSMauro Carvalho Chehab }
265b285192aSMauro Carvalho Chehab 
266b285192aSMauro Carvalho Chehab static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
267b285192aSMauro Carvalho Chehab {
268b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
269b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
270b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
271b285192aSMauro Carvalho Chehab 
272b285192aSMauro Carvalho Chehab 	if (saa7164_api_get_videomux(port) != SAA_OK)
273b285192aSMauro Carvalho Chehab 		return -EIO;
274b285192aSMauro Carvalho Chehab 
275b285192aSMauro Carvalho Chehab 	*i = (port->mux_input - 1);
276b285192aSMauro Carvalho Chehab 
277b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, *i);
278b285192aSMauro Carvalho Chehab 
279b285192aSMauro Carvalho Chehab 	return 0;
280b285192aSMauro Carvalho Chehab }
281b285192aSMauro Carvalho Chehab 
282b285192aSMauro Carvalho Chehab static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
283b285192aSMauro Carvalho Chehab {
284b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
285b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
286b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
287b285192aSMauro Carvalho Chehab 
288b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, i);
289b285192aSMauro Carvalho Chehab 
290b285192aSMauro Carvalho Chehab 	if (i >= 7)
291b285192aSMauro Carvalho Chehab 		return -EINVAL;
292b285192aSMauro Carvalho Chehab 
293b285192aSMauro Carvalho Chehab 	port->mux_input = i + 1;
294b285192aSMauro Carvalho Chehab 
295b285192aSMauro Carvalho Chehab 	if (saa7164_api_set_videomux(port) != SAA_OK)
296b285192aSMauro Carvalho Chehab 		return -EIO;
297b285192aSMauro Carvalho Chehab 
298b285192aSMauro Carvalho Chehab 	return 0;
299b285192aSMauro Carvalho Chehab }
300b285192aSMauro Carvalho Chehab 
301b285192aSMauro Carvalho Chehab static int vidioc_g_tuner(struct file *file, void *priv,
302b285192aSMauro Carvalho Chehab 	struct v4l2_tuner *t)
303b285192aSMauro Carvalho Chehab {
304b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
305b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
306b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
307b285192aSMauro Carvalho Chehab 
308b285192aSMauro Carvalho Chehab 	if (0 != t->index)
309b285192aSMauro Carvalho Chehab 		return -EINVAL;
310b285192aSMauro Carvalho Chehab 
311b285192aSMauro Carvalho Chehab 	strcpy(t->name, "tuner");
312b285192aSMauro Carvalho Chehab 	t->type = V4L2_TUNER_ANALOG_TV;
313b285192aSMauro Carvalho Chehab 	t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
314b285192aSMauro Carvalho Chehab 
315b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "VIDIOC_G_TUNER: tuner type %d\n", t->type);
316b285192aSMauro Carvalho Chehab 
317b285192aSMauro Carvalho Chehab 	return 0;
318b285192aSMauro Carvalho Chehab }
319b285192aSMauro Carvalho Chehab 
320b285192aSMauro Carvalho Chehab static int vidioc_s_tuner(struct file *file, void *priv,
321b285192aSMauro Carvalho Chehab 	struct v4l2_tuner *t)
322b285192aSMauro Carvalho Chehab {
323b285192aSMauro Carvalho Chehab 	/* Update the A/V core */
324b285192aSMauro Carvalho Chehab 	return 0;
325b285192aSMauro Carvalho Chehab }
326b285192aSMauro Carvalho Chehab 
327b285192aSMauro Carvalho Chehab static int vidioc_g_frequency(struct file *file, void *priv,
328b285192aSMauro Carvalho Chehab 	struct v4l2_frequency *f)
329b285192aSMauro Carvalho Chehab {
330b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
331b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
332b285192aSMauro Carvalho Chehab 
333b285192aSMauro Carvalho Chehab 	f->type = V4L2_TUNER_ANALOG_TV;
334b285192aSMauro Carvalho Chehab 	f->frequency = port->freq;
335b285192aSMauro Carvalho Chehab 
336b285192aSMauro Carvalho Chehab 	return 0;
337b285192aSMauro Carvalho Chehab }
338b285192aSMauro Carvalho Chehab 
339b285192aSMauro Carvalho Chehab static int vidioc_s_frequency(struct file *file, void *priv,
340b285192aSMauro Carvalho Chehab 	struct v4l2_frequency *f)
341b285192aSMauro Carvalho Chehab {
342b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
343b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
344b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
345b285192aSMauro Carvalho Chehab 	struct saa7164_port *tsport;
346b285192aSMauro Carvalho Chehab 	struct dvb_frontend *fe;
347b285192aSMauro Carvalho Chehab 
348b285192aSMauro Carvalho Chehab 	/* TODO: Pull this for the std */
349b285192aSMauro Carvalho Chehab 	struct analog_parameters params = {
350b285192aSMauro Carvalho Chehab 		.mode      = V4L2_TUNER_ANALOG_TV,
351b285192aSMauro Carvalho Chehab 		.audmode   = V4L2_TUNER_MODE_STEREO,
352b285192aSMauro Carvalho Chehab 		.std       = port->encodernorm.id,
353b285192aSMauro Carvalho Chehab 		.frequency = f->frequency
354b285192aSMauro Carvalho Chehab 	};
355b285192aSMauro Carvalho Chehab 
356b285192aSMauro Carvalho Chehab 	/* Stop the encoder */
357b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s() frequency=%d tuner=%d\n", __func__,
358b285192aSMauro Carvalho Chehab 		f->frequency, f->tuner);
359b285192aSMauro Carvalho Chehab 
360b285192aSMauro Carvalho Chehab 	if (f->tuner != 0)
361b285192aSMauro Carvalho Chehab 		return -EINVAL;
362b285192aSMauro Carvalho Chehab 
363b285192aSMauro Carvalho Chehab 	if (f->type != V4L2_TUNER_ANALOG_TV)
364b285192aSMauro Carvalho Chehab 		return -EINVAL;
365b285192aSMauro Carvalho Chehab 
366b285192aSMauro Carvalho Chehab 	port->freq = f->frequency;
367b285192aSMauro Carvalho Chehab 
368b285192aSMauro Carvalho Chehab 	/* Update the hardware */
369b285192aSMauro Carvalho Chehab 	if (port->nr == SAA7164_PORT_ENC1)
370b285192aSMauro Carvalho Chehab 		tsport = &dev->ports[SAA7164_PORT_TS1];
371b285192aSMauro Carvalho Chehab 	else
372b285192aSMauro Carvalho Chehab 	if (port->nr == SAA7164_PORT_ENC2)
373b285192aSMauro Carvalho Chehab 		tsport = &dev->ports[SAA7164_PORT_TS2];
374b285192aSMauro Carvalho Chehab 	else
375b285192aSMauro Carvalho Chehab 		BUG();
376b285192aSMauro Carvalho Chehab 
377b285192aSMauro Carvalho Chehab 	fe = tsport->dvb.frontend;
378b285192aSMauro Carvalho Chehab 
379b285192aSMauro Carvalho Chehab 	if (fe && fe->ops.tuner_ops.set_analog_params)
380b285192aSMauro Carvalho Chehab 		fe->ops.tuner_ops.set_analog_params(fe, &params);
381b285192aSMauro Carvalho Chehab 	else
382b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
383b285192aSMauro Carvalho Chehab 
384b285192aSMauro Carvalho Chehab 	saa7164_encoder_initialize(port);
385b285192aSMauro Carvalho Chehab 
386b285192aSMauro Carvalho Chehab 	return 0;
387b285192aSMauro Carvalho Chehab }
388b285192aSMauro Carvalho Chehab 
389b285192aSMauro Carvalho Chehab static int vidioc_g_ctrl(struct file *file, void *priv,
390b285192aSMauro Carvalho Chehab 	struct v4l2_control *ctl)
391b285192aSMauro Carvalho Chehab {
392b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
393b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
394b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
395b285192aSMauro Carvalho Chehab 
396b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s(id=%d, value=%d)\n", __func__,
397b285192aSMauro Carvalho Chehab 		ctl->id, ctl->value);
398b285192aSMauro Carvalho Chehab 
399b285192aSMauro Carvalho Chehab 	switch (ctl->id) {
400b285192aSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
401b285192aSMauro Carvalho Chehab 		ctl->value = port->ctl_brightness;
402b285192aSMauro Carvalho Chehab 		break;
403b285192aSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
404b285192aSMauro Carvalho Chehab 		ctl->value = port->ctl_contrast;
405b285192aSMauro Carvalho Chehab 		break;
406b285192aSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
407b285192aSMauro Carvalho Chehab 		ctl->value = port->ctl_saturation;
408b285192aSMauro Carvalho Chehab 		break;
409b285192aSMauro Carvalho Chehab 	case V4L2_CID_HUE:
410b285192aSMauro Carvalho Chehab 		ctl->value = port->ctl_hue;
411b285192aSMauro Carvalho Chehab 		break;
412b285192aSMauro Carvalho Chehab 	case V4L2_CID_SHARPNESS:
413b285192aSMauro Carvalho Chehab 		ctl->value = port->ctl_sharpness;
414b285192aSMauro Carvalho Chehab 		break;
415b285192aSMauro Carvalho Chehab 	case V4L2_CID_AUDIO_VOLUME:
416b285192aSMauro Carvalho Chehab 		ctl->value = port->ctl_volume;
417b285192aSMauro Carvalho Chehab 		break;
418b285192aSMauro Carvalho Chehab 	default:
419b285192aSMauro Carvalho Chehab 		return -EINVAL;
420b285192aSMauro Carvalho Chehab 	}
421b285192aSMauro Carvalho Chehab 
422b285192aSMauro Carvalho Chehab 	return 0;
423b285192aSMauro Carvalho Chehab }
424b285192aSMauro Carvalho Chehab 
425b285192aSMauro Carvalho Chehab static int vidioc_s_ctrl(struct file *file, void *priv,
426b285192aSMauro Carvalho Chehab 	struct v4l2_control *ctl)
427b285192aSMauro Carvalho Chehab {
428b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
429b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
430b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
431b285192aSMauro Carvalho Chehab 	int ret = 0;
432b285192aSMauro Carvalho Chehab 
433b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s(id=%d, value=%d)\n", __func__,
434b285192aSMauro Carvalho Chehab 		ctl->id, ctl->value);
435b285192aSMauro Carvalho Chehab 
436b285192aSMauro Carvalho Chehab 	switch (ctl->id) {
437b285192aSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
438b285192aSMauro Carvalho Chehab 		if ((ctl->value >= 0) && (ctl->value <= 255)) {
439b285192aSMauro Carvalho Chehab 			port->ctl_brightness = ctl->value;
440b285192aSMauro Carvalho Chehab 			saa7164_api_set_usercontrol(port,
441b285192aSMauro Carvalho Chehab 				PU_BRIGHTNESS_CONTROL);
442b285192aSMauro Carvalho Chehab 		} else
443b285192aSMauro Carvalho Chehab 			ret = -EINVAL;
444b285192aSMauro Carvalho Chehab 		break;
445b285192aSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
446b285192aSMauro Carvalho Chehab 		if ((ctl->value >= 0) && (ctl->value <= 255)) {
447b285192aSMauro Carvalho Chehab 			port->ctl_contrast = ctl->value;
448b285192aSMauro Carvalho Chehab 			saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
449b285192aSMauro Carvalho Chehab 		} else
450b285192aSMauro Carvalho Chehab 			ret = -EINVAL;
451b285192aSMauro Carvalho Chehab 		break;
452b285192aSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
453b285192aSMauro Carvalho Chehab 		if ((ctl->value >= 0) && (ctl->value <= 255)) {
454b285192aSMauro Carvalho Chehab 			port->ctl_saturation = ctl->value;
455b285192aSMauro Carvalho Chehab 			saa7164_api_set_usercontrol(port,
456b285192aSMauro Carvalho Chehab 				PU_SATURATION_CONTROL);
457b285192aSMauro Carvalho Chehab 		} else
458b285192aSMauro Carvalho Chehab 			ret = -EINVAL;
459b285192aSMauro Carvalho Chehab 		break;
460b285192aSMauro Carvalho Chehab 	case V4L2_CID_HUE:
461b285192aSMauro Carvalho Chehab 		if ((ctl->value >= 0) && (ctl->value <= 255)) {
462b285192aSMauro Carvalho Chehab 			port->ctl_hue = ctl->value;
463b285192aSMauro Carvalho Chehab 			saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
464b285192aSMauro Carvalho Chehab 		} else
465b285192aSMauro Carvalho Chehab 			ret = -EINVAL;
466b285192aSMauro Carvalho Chehab 		break;
467b285192aSMauro Carvalho Chehab 	case V4L2_CID_SHARPNESS:
468b285192aSMauro Carvalho Chehab 		if ((ctl->value >= 0) && (ctl->value <= 255)) {
469b285192aSMauro Carvalho Chehab 			port->ctl_sharpness = ctl->value;
470b285192aSMauro Carvalho Chehab 			saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
471b285192aSMauro Carvalho Chehab 		} else
472b285192aSMauro Carvalho Chehab 			ret = -EINVAL;
473b285192aSMauro Carvalho Chehab 		break;
474b285192aSMauro Carvalho Chehab 	case V4L2_CID_AUDIO_VOLUME:
475b285192aSMauro Carvalho Chehab 		if ((ctl->value >= -83) && (ctl->value <= 24)) {
476b285192aSMauro Carvalho Chehab 			port->ctl_volume = ctl->value;
477b285192aSMauro Carvalho Chehab 			saa7164_api_set_audio_volume(port, port->ctl_volume);
478b285192aSMauro Carvalho Chehab 		} else
479b285192aSMauro Carvalho Chehab 			ret = -EINVAL;
480b285192aSMauro Carvalho Chehab 		break;
481b285192aSMauro Carvalho Chehab 	default:
482b285192aSMauro Carvalho Chehab 		ret = -EINVAL;
483b285192aSMauro Carvalho Chehab 	}
484b285192aSMauro Carvalho Chehab 
485b285192aSMauro Carvalho Chehab 	return ret;
486b285192aSMauro Carvalho Chehab }
487b285192aSMauro Carvalho Chehab 
488b285192aSMauro Carvalho Chehab static int saa7164_get_ctrl(struct saa7164_port *port,
489b285192aSMauro Carvalho Chehab 	struct v4l2_ext_control *ctrl)
490b285192aSMauro Carvalho Chehab {
491b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_params *params = &port->encoder_params;
492b285192aSMauro Carvalho Chehab 
493b285192aSMauro Carvalho Chehab 	switch (ctrl->id) {
494b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_BITRATE:
495b285192aSMauro Carvalho Chehab 		ctrl->value = params->bitrate;
496b285192aSMauro Carvalho Chehab 		break;
497b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_STREAM_TYPE:
498b285192aSMauro Carvalho Chehab 		ctrl->value = params->stream_type;
499b285192aSMauro Carvalho Chehab 		break;
500b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_AUDIO_MUTE:
501b285192aSMauro Carvalho Chehab 		ctrl->value = params->ctl_mute;
502b285192aSMauro Carvalho Chehab 		break;
503b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_ASPECT:
504b285192aSMauro Carvalho Chehab 		ctrl->value = params->ctl_aspect;
505b285192aSMauro Carvalho Chehab 		break;
506b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
507b285192aSMauro Carvalho Chehab 		ctrl->value = params->bitrate_mode;
508b285192aSMauro Carvalho Chehab 		break;
509b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
510b285192aSMauro Carvalho Chehab 		ctrl->value = params->refdist;
511b285192aSMauro Carvalho Chehab 		break;
512b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
513b285192aSMauro Carvalho Chehab 		ctrl->value = params->bitrate_peak;
514b285192aSMauro Carvalho Chehab 		break;
515b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
516b285192aSMauro Carvalho Chehab 		ctrl->value = params->gop_size;
517b285192aSMauro Carvalho Chehab 		break;
518b285192aSMauro Carvalho Chehab 	default:
519b285192aSMauro Carvalho Chehab 		return -EINVAL;
520b285192aSMauro Carvalho Chehab 	}
521b285192aSMauro Carvalho Chehab 	return 0;
522b285192aSMauro Carvalho Chehab }
523b285192aSMauro Carvalho Chehab 
524b285192aSMauro Carvalho Chehab static int vidioc_g_ext_ctrls(struct file *file, void *priv,
525b285192aSMauro Carvalho Chehab 	struct v4l2_ext_controls *ctrls)
526b285192aSMauro Carvalho Chehab {
527b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
528b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
529b285192aSMauro Carvalho Chehab 	int i, err = 0;
530b285192aSMauro Carvalho Chehab 
531b285192aSMauro Carvalho Chehab 	if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
532b285192aSMauro Carvalho Chehab 		for (i = 0; i < ctrls->count; i++) {
533b285192aSMauro Carvalho Chehab 			struct v4l2_ext_control *ctrl = ctrls->controls + i;
534b285192aSMauro Carvalho Chehab 
535b285192aSMauro Carvalho Chehab 			err = saa7164_get_ctrl(port, ctrl);
536b285192aSMauro Carvalho Chehab 			if (err) {
537b285192aSMauro Carvalho Chehab 				ctrls->error_idx = i;
538b285192aSMauro Carvalho Chehab 				break;
539b285192aSMauro Carvalho Chehab 			}
540b285192aSMauro Carvalho Chehab 		}
541b285192aSMauro Carvalho Chehab 		return err;
542b285192aSMauro Carvalho Chehab 
543b285192aSMauro Carvalho Chehab 	}
544b285192aSMauro Carvalho Chehab 
545b285192aSMauro Carvalho Chehab 	return -EINVAL;
546b285192aSMauro Carvalho Chehab }
547b285192aSMauro Carvalho Chehab 
548b285192aSMauro Carvalho Chehab static int saa7164_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
549b285192aSMauro Carvalho Chehab {
550b285192aSMauro Carvalho Chehab 	int ret = -EINVAL;
551b285192aSMauro Carvalho Chehab 
552b285192aSMauro Carvalho Chehab 	switch (ctrl->id) {
553b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_BITRATE:
554b285192aSMauro Carvalho Chehab 		if ((ctrl->value >= ENCODER_MIN_BITRATE) &&
555b285192aSMauro Carvalho Chehab 			(ctrl->value <= ENCODER_MAX_BITRATE))
556b285192aSMauro Carvalho Chehab 			ret = 0;
557b285192aSMauro Carvalho Chehab 		break;
558b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_STREAM_TYPE:
559b285192aSMauro Carvalho Chehab 		if ((ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_PS) ||
560b285192aSMauro Carvalho Chehab 			(ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS))
561b285192aSMauro Carvalho Chehab 			ret = 0;
562b285192aSMauro Carvalho Chehab 		break;
563b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_AUDIO_MUTE:
564b285192aSMauro Carvalho Chehab 		if ((ctrl->value >= 0) &&
565b285192aSMauro Carvalho Chehab 			(ctrl->value <= 1))
566b285192aSMauro Carvalho Chehab 			ret = 0;
567b285192aSMauro Carvalho Chehab 		break;
568b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_ASPECT:
569b285192aSMauro Carvalho Chehab 		if ((ctrl->value >= V4L2_MPEG_VIDEO_ASPECT_1x1) &&
570b285192aSMauro Carvalho Chehab 			(ctrl->value <= V4L2_MPEG_VIDEO_ASPECT_221x100))
571b285192aSMauro Carvalho Chehab 			ret = 0;
572b285192aSMauro Carvalho Chehab 		break;
573b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
574b285192aSMauro Carvalho Chehab 		if ((ctrl->value >= 0) &&
575b285192aSMauro Carvalho Chehab 			(ctrl->value <= 255))
576b285192aSMauro Carvalho Chehab 			ret = 0;
577b285192aSMauro Carvalho Chehab 		break;
578b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
579b285192aSMauro Carvalho Chehab 		if ((ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ||
580b285192aSMauro Carvalho Chehab 			(ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR))
581b285192aSMauro Carvalho Chehab 			ret = 0;
582b285192aSMauro Carvalho Chehab 		break;
583b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
584b285192aSMauro Carvalho Chehab 		if ((ctrl->value >= 1) &&
585b285192aSMauro Carvalho Chehab 			(ctrl->value <= 3))
586b285192aSMauro Carvalho Chehab 			ret = 0;
587b285192aSMauro Carvalho Chehab 		break;
588b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
589b285192aSMauro Carvalho Chehab 		if ((ctrl->value >= ENCODER_MIN_BITRATE) &&
590b285192aSMauro Carvalho Chehab 			(ctrl->value <= ENCODER_MAX_BITRATE))
591b285192aSMauro Carvalho Chehab 			ret = 0;
592b285192aSMauro Carvalho Chehab 		break;
593b285192aSMauro Carvalho Chehab 	default:
594b285192aSMauro Carvalho Chehab 		ret = -EINVAL;
595b285192aSMauro Carvalho Chehab 	}
596b285192aSMauro Carvalho Chehab 
597b285192aSMauro Carvalho Chehab 	return ret;
598b285192aSMauro Carvalho Chehab }
599b285192aSMauro Carvalho Chehab 
600b285192aSMauro Carvalho Chehab static int vidioc_try_ext_ctrls(struct file *file, void *priv,
601b285192aSMauro Carvalho Chehab 	struct v4l2_ext_controls *ctrls)
602b285192aSMauro Carvalho Chehab {
603b285192aSMauro Carvalho Chehab 	int i, err = 0;
604b285192aSMauro Carvalho Chehab 
605b285192aSMauro Carvalho Chehab 	if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
606b285192aSMauro Carvalho Chehab 		for (i = 0; i < ctrls->count; i++) {
607b285192aSMauro Carvalho Chehab 			struct v4l2_ext_control *ctrl = ctrls->controls + i;
608b285192aSMauro Carvalho Chehab 
609b285192aSMauro Carvalho Chehab 			err = saa7164_try_ctrl(ctrl, 0);
610b285192aSMauro Carvalho Chehab 			if (err) {
611b285192aSMauro Carvalho Chehab 				ctrls->error_idx = i;
612b285192aSMauro Carvalho Chehab 				break;
613b285192aSMauro Carvalho Chehab 			}
614b285192aSMauro Carvalho Chehab 		}
615b285192aSMauro Carvalho Chehab 		return err;
616b285192aSMauro Carvalho Chehab 	}
617b285192aSMauro Carvalho Chehab 
618b285192aSMauro Carvalho Chehab 	return -EINVAL;
619b285192aSMauro Carvalho Chehab }
620b285192aSMauro Carvalho Chehab 
621b285192aSMauro Carvalho Chehab static int saa7164_set_ctrl(struct saa7164_port *port,
622b285192aSMauro Carvalho Chehab 	struct v4l2_ext_control *ctrl)
623b285192aSMauro Carvalho Chehab {
624b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_params *params = &port->encoder_params;
625b285192aSMauro Carvalho Chehab 	int ret = 0;
626b285192aSMauro Carvalho Chehab 
627b285192aSMauro Carvalho Chehab 	switch (ctrl->id) {
628b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_BITRATE:
629b285192aSMauro Carvalho Chehab 		params->bitrate = ctrl->value;
630b285192aSMauro Carvalho Chehab 		break;
631b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_STREAM_TYPE:
632b285192aSMauro Carvalho Chehab 		params->stream_type = ctrl->value;
633b285192aSMauro Carvalho Chehab 		break;
634b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_AUDIO_MUTE:
635b285192aSMauro Carvalho Chehab 		params->ctl_mute = ctrl->value;
636b285192aSMauro Carvalho Chehab 		ret = saa7164_api_audio_mute(port, params->ctl_mute);
637b285192aSMauro Carvalho Chehab 		if (ret != SAA_OK) {
638b285192aSMauro Carvalho Chehab 			printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
639b285192aSMauro Carvalho Chehab 				ret);
640b285192aSMauro Carvalho Chehab 			ret = -EIO;
641b285192aSMauro Carvalho Chehab 		}
642b285192aSMauro Carvalho Chehab 		break;
643b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_ASPECT:
644b285192aSMauro Carvalho Chehab 		params->ctl_aspect = ctrl->value;
645b285192aSMauro Carvalho Chehab 		ret = saa7164_api_set_aspect_ratio(port);
646b285192aSMauro Carvalho Chehab 		if (ret != SAA_OK) {
647b285192aSMauro Carvalho Chehab 			printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
648b285192aSMauro Carvalho Chehab 				ret);
649b285192aSMauro Carvalho Chehab 			ret = -EIO;
650b285192aSMauro Carvalho Chehab 		}
651b285192aSMauro Carvalho Chehab 		break;
652b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
653b285192aSMauro Carvalho Chehab 		params->bitrate_mode = ctrl->value;
654b285192aSMauro Carvalho Chehab 		break;
655b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
656b285192aSMauro Carvalho Chehab 		params->refdist = ctrl->value;
657b285192aSMauro Carvalho Chehab 		break;
658b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
659b285192aSMauro Carvalho Chehab 		params->bitrate_peak = ctrl->value;
660b285192aSMauro Carvalho Chehab 		break;
661b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
662b285192aSMauro Carvalho Chehab 		params->gop_size = ctrl->value;
663b285192aSMauro Carvalho Chehab 		break;
664b285192aSMauro Carvalho Chehab 	default:
665b285192aSMauro Carvalho Chehab 		return -EINVAL;
666b285192aSMauro Carvalho Chehab 	}
667b285192aSMauro Carvalho Chehab 
668b285192aSMauro Carvalho Chehab 	/* TODO: Update the hardware */
669b285192aSMauro Carvalho Chehab 
670b285192aSMauro Carvalho Chehab 	return ret;
671b285192aSMauro Carvalho Chehab }
672b285192aSMauro Carvalho Chehab 
673b285192aSMauro Carvalho Chehab static int vidioc_s_ext_ctrls(struct file *file, void *priv,
674b285192aSMauro Carvalho Chehab 	struct v4l2_ext_controls *ctrls)
675b285192aSMauro Carvalho Chehab {
676b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
677b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
678b285192aSMauro Carvalho Chehab 	int i, err = 0;
679b285192aSMauro Carvalho Chehab 
680b285192aSMauro Carvalho Chehab 	if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
681b285192aSMauro Carvalho Chehab 		for (i = 0; i < ctrls->count; i++) {
682b285192aSMauro Carvalho Chehab 			struct v4l2_ext_control *ctrl = ctrls->controls + i;
683b285192aSMauro Carvalho Chehab 
684b285192aSMauro Carvalho Chehab 			err = saa7164_try_ctrl(ctrl, 0);
685b285192aSMauro Carvalho Chehab 			if (err) {
686b285192aSMauro Carvalho Chehab 				ctrls->error_idx = i;
687b285192aSMauro Carvalho Chehab 				break;
688b285192aSMauro Carvalho Chehab 			}
689b285192aSMauro Carvalho Chehab 			err = saa7164_set_ctrl(port, ctrl);
690b285192aSMauro Carvalho Chehab 			if (err) {
691b285192aSMauro Carvalho Chehab 				ctrls->error_idx = i;
692b285192aSMauro Carvalho Chehab 				break;
693b285192aSMauro Carvalho Chehab 			}
694b285192aSMauro Carvalho Chehab 		}
695b285192aSMauro Carvalho Chehab 		return err;
696b285192aSMauro Carvalho Chehab 
697b285192aSMauro Carvalho Chehab 	}
698b285192aSMauro Carvalho Chehab 
699b285192aSMauro Carvalho Chehab 	return -EINVAL;
700b285192aSMauro Carvalho Chehab }
701b285192aSMauro Carvalho Chehab 
702b285192aSMauro Carvalho Chehab static int vidioc_querycap(struct file *file, void  *priv,
703b285192aSMauro Carvalho Chehab 	struct v4l2_capability *cap)
704b285192aSMauro Carvalho Chehab {
705b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
706b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
707b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
708b285192aSMauro Carvalho Chehab 
709b285192aSMauro Carvalho Chehab 	strcpy(cap->driver, dev->name);
710b285192aSMauro Carvalho Chehab 	strlcpy(cap->card, saa7164_boards[dev->board].name,
711b285192aSMauro Carvalho Chehab 		sizeof(cap->card));
712b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
713b285192aSMauro Carvalho Chehab 
714b285192aSMauro Carvalho Chehab 	cap->capabilities =
715b285192aSMauro Carvalho Chehab 		V4L2_CAP_VIDEO_CAPTURE |
716b285192aSMauro Carvalho Chehab 		V4L2_CAP_READWRITE     |
717b285192aSMauro Carvalho Chehab 		0;
718b285192aSMauro Carvalho Chehab 
719b285192aSMauro Carvalho Chehab 	cap->capabilities |= V4L2_CAP_TUNER;
720b285192aSMauro Carvalho Chehab 	cap->version = 0;
721b285192aSMauro Carvalho Chehab 
722b285192aSMauro Carvalho Chehab 	return 0;
723b285192aSMauro Carvalho Chehab }
724b285192aSMauro Carvalho Chehab 
725b285192aSMauro Carvalho Chehab static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
726b285192aSMauro Carvalho Chehab 	struct v4l2_fmtdesc *f)
727b285192aSMauro Carvalho Chehab {
728b285192aSMauro Carvalho Chehab 	if (f->index != 0)
729b285192aSMauro Carvalho Chehab 		return -EINVAL;
730b285192aSMauro Carvalho Chehab 
731b285192aSMauro Carvalho Chehab 	strlcpy(f->description, "MPEG", sizeof(f->description));
732b285192aSMauro Carvalho Chehab 	f->pixelformat = V4L2_PIX_FMT_MPEG;
733b285192aSMauro Carvalho Chehab 
734b285192aSMauro Carvalho Chehab 	return 0;
735b285192aSMauro Carvalho Chehab }
736b285192aSMauro Carvalho Chehab 
737b285192aSMauro Carvalho Chehab static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
738b285192aSMauro Carvalho Chehab 				struct v4l2_format *f)
739b285192aSMauro Carvalho Chehab {
740b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
741b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
742b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
743b285192aSMauro Carvalho Chehab 
744b285192aSMauro Carvalho Chehab 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
745b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline = 0;
746b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage    =
747b285192aSMauro Carvalho Chehab 		port->ts_packet_size * port->ts_packet_count;
748b285192aSMauro Carvalho Chehab 	f->fmt.pix.colorspace   = 0;
749b285192aSMauro Carvalho Chehab 	f->fmt.pix.width        = port->width;
750b285192aSMauro Carvalho Chehab 	f->fmt.pix.height       = port->height;
751b285192aSMauro Carvalho Chehab 
752b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "VIDIOC_G_FMT: w: %d, h: %d\n",
753b285192aSMauro Carvalho Chehab 		port->width, port->height);
754b285192aSMauro Carvalho Chehab 
755b285192aSMauro Carvalho Chehab 	return 0;
756b285192aSMauro Carvalho Chehab }
757b285192aSMauro Carvalho Chehab 
758b285192aSMauro Carvalho Chehab static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
759b285192aSMauro Carvalho Chehab 				struct v4l2_format *f)
760b285192aSMauro Carvalho Chehab {
761b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
762b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
763b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
764b285192aSMauro Carvalho Chehab 
765b285192aSMauro Carvalho Chehab 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
766b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline = 0;
767b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage    =
768b285192aSMauro Carvalho Chehab 		port->ts_packet_size * port->ts_packet_count;
769b285192aSMauro Carvalho Chehab 	f->fmt.pix.colorspace   = 0;
770b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
771b285192aSMauro Carvalho Chehab 		port->width, port->height);
772b285192aSMauro Carvalho Chehab 	return 0;
773b285192aSMauro Carvalho Chehab }
774b285192aSMauro Carvalho Chehab 
775b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
776b285192aSMauro Carvalho Chehab 				struct v4l2_format *f)
777b285192aSMauro Carvalho Chehab {
778b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
779b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
780b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
781b285192aSMauro Carvalho Chehab 
782b285192aSMauro Carvalho Chehab 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
783b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline = 0;
784b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage    =
785b285192aSMauro Carvalho Chehab 		port->ts_packet_size * port->ts_packet_count;
786b285192aSMauro Carvalho Chehab 	f->fmt.pix.colorspace   = 0;
787b285192aSMauro Carvalho Chehab 
788b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
789b285192aSMauro Carvalho Chehab 		f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
790b285192aSMauro Carvalho Chehab 
791b285192aSMauro Carvalho Chehab 	return 0;
792b285192aSMauro Carvalho Chehab }
793b285192aSMauro Carvalho Chehab 
794b285192aSMauro Carvalho Chehab static int fill_queryctrl(struct saa7164_encoder_params *params,
795b285192aSMauro Carvalho Chehab 	struct v4l2_queryctrl *c)
796b285192aSMauro Carvalho Chehab {
797b285192aSMauro Carvalho Chehab 	switch (c->id) {
798b285192aSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
799b285192aSMauro Carvalho Chehab 		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 127);
800b285192aSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
801b285192aSMauro Carvalho Chehab 		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 66);
802b285192aSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
803b285192aSMauro Carvalho Chehab 		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 62);
804b285192aSMauro Carvalho Chehab 	case V4L2_CID_HUE:
805b285192aSMauro Carvalho Chehab 		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 128);
806b285192aSMauro Carvalho Chehab 	case V4L2_CID_SHARPNESS:
807b285192aSMauro Carvalho Chehab 		return v4l2_ctrl_query_fill(c, 0x0, 0x0f, 1, 8);
808b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_AUDIO_MUTE:
809b285192aSMauro Carvalho Chehab 		return v4l2_ctrl_query_fill(c, 0x0, 0x01, 1, 0);
810b285192aSMauro Carvalho Chehab 	case V4L2_CID_AUDIO_VOLUME:
811b285192aSMauro Carvalho Chehab 		return v4l2_ctrl_query_fill(c, -83, 24, 1, 20);
812b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_BITRATE:
813b285192aSMauro Carvalho Chehab 		return v4l2_ctrl_query_fill(c,
814b285192aSMauro Carvalho Chehab 			ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
815b285192aSMauro Carvalho Chehab 			100000, ENCODER_DEF_BITRATE);
816b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_STREAM_TYPE:
817b285192aSMauro Carvalho Chehab 		return v4l2_ctrl_query_fill(c,
818b285192aSMauro Carvalho Chehab 			V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
819b285192aSMauro Carvalho Chehab 			V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
820b285192aSMauro Carvalho Chehab 			1, V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
821b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_ASPECT:
822b285192aSMauro Carvalho Chehab 		return v4l2_ctrl_query_fill(c,
823b285192aSMauro Carvalho Chehab 			V4L2_MPEG_VIDEO_ASPECT_1x1,
824b285192aSMauro Carvalho Chehab 			V4L2_MPEG_VIDEO_ASPECT_221x100,
825b285192aSMauro Carvalho Chehab 			1, V4L2_MPEG_VIDEO_ASPECT_4x3);
826b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
827b285192aSMauro Carvalho Chehab 		return v4l2_ctrl_query_fill(c, 1, 255, 1, 15);
828b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
829b285192aSMauro Carvalho Chehab 		return v4l2_ctrl_query_fill(c,
830b285192aSMauro Carvalho Chehab 			V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
831b285192aSMauro Carvalho Chehab 			V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
832b285192aSMauro Carvalho Chehab 			1, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
833b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
834b285192aSMauro Carvalho Chehab 		return v4l2_ctrl_query_fill(c,
835b285192aSMauro Carvalho Chehab 			1, 3, 1, 1);
836b285192aSMauro Carvalho Chehab 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
837b285192aSMauro Carvalho Chehab 		return v4l2_ctrl_query_fill(c,
838b285192aSMauro Carvalho Chehab 			ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
839b285192aSMauro Carvalho Chehab 			100000, ENCODER_DEF_BITRATE);
840b285192aSMauro Carvalho Chehab 	default:
841b285192aSMauro Carvalho Chehab 		return -EINVAL;
842b285192aSMauro Carvalho Chehab 	}
843b285192aSMauro Carvalho Chehab }
844b285192aSMauro Carvalho Chehab 
845b285192aSMauro Carvalho Chehab static int vidioc_queryctrl(struct file *file, void *priv,
846b285192aSMauro Carvalho Chehab 	struct v4l2_queryctrl *c)
847b285192aSMauro Carvalho Chehab {
848b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = priv;
849b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
850b285192aSMauro Carvalho Chehab 	int i, next;
851b285192aSMauro Carvalho Chehab 	u32 id = c->id;
852b285192aSMauro Carvalho Chehab 
853b285192aSMauro Carvalho Chehab 	memset(c, 0, sizeof(*c));
854b285192aSMauro Carvalho Chehab 
855b285192aSMauro Carvalho Chehab 	next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
856b285192aSMauro Carvalho Chehab 	c->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
857b285192aSMauro Carvalho Chehab 
858b285192aSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(saa7164_v4l2_ctrls); i++) {
859b285192aSMauro Carvalho Chehab 		if (next) {
860b285192aSMauro Carvalho Chehab 			if (c->id < saa7164_v4l2_ctrls[i])
861b285192aSMauro Carvalho Chehab 				c->id = saa7164_v4l2_ctrls[i];
862b285192aSMauro Carvalho Chehab 			else
863b285192aSMauro Carvalho Chehab 				continue;
864b285192aSMauro Carvalho Chehab 		}
865b285192aSMauro Carvalho Chehab 
866b285192aSMauro Carvalho Chehab 		if (c->id == saa7164_v4l2_ctrls[i])
867b285192aSMauro Carvalho Chehab 			return fill_queryctrl(&port->encoder_params, c);
868b285192aSMauro Carvalho Chehab 
869b285192aSMauro Carvalho Chehab 		if (c->id < saa7164_v4l2_ctrls[i])
870b285192aSMauro Carvalho Chehab 			break;
871b285192aSMauro Carvalho Chehab 	}
872b285192aSMauro Carvalho Chehab 
873b285192aSMauro Carvalho Chehab 	return -EINVAL;
874b285192aSMauro Carvalho Chehab }
875b285192aSMauro Carvalho Chehab 
876b285192aSMauro Carvalho Chehab static int saa7164_encoder_stop_port(struct saa7164_port *port)
877b285192aSMauro Carvalho Chehab {
878b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
879b285192aSMauro Carvalho Chehab 	int ret;
880b285192aSMauro Carvalho Chehab 
881b285192aSMauro Carvalho Chehab 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
882b285192aSMauro Carvalho Chehab 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
883b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
884b285192aSMauro Carvalho Chehab 			__func__, ret);
885b285192aSMauro Carvalho Chehab 		ret = -EIO;
886b285192aSMauro Carvalho Chehab 	} else {
887b285192aSMauro Carvalho Chehab 		dprintk(DBGLVL_ENC, "%s()    Stopped\n", __func__);
888b285192aSMauro Carvalho Chehab 		ret = 0;
889b285192aSMauro Carvalho Chehab 	}
890b285192aSMauro Carvalho Chehab 
891b285192aSMauro Carvalho Chehab 	return ret;
892b285192aSMauro Carvalho Chehab }
893b285192aSMauro Carvalho Chehab 
894b285192aSMauro Carvalho Chehab static int saa7164_encoder_acquire_port(struct saa7164_port *port)
895b285192aSMauro Carvalho Chehab {
896b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
897b285192aSMauro Carvalho Chehab 	int ret;
898b285192aSMauro Carvalho Chehab 
899b285192aSMauro Carvalho Chehab 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
900b285192aSMauro Carvalho Chehab 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
901b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
902b285192aSMauro Carvalho Chehab 			__func__, ret);
903b285192aSMauro Carvalho Chehab 		ret = -EIO;
904b285192aSMauro Carvalho Chehab 	} else {
905b285192aSMauro Carvalho Chehab 		dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
906b285192aSMauro Carvalho Chehab 		ret = 0;
907b285192aSMauro Carvalho Chehab 	}
908b285192aSMauro Carvalho Chehab 
909b285192aSMauro Carvalho Chehab 	return ret;
910b285192aSMauro Carvalho Chehab }
911b285192aSMauro Carvalho Chehab 
912b285192aSMauro Carvalho Chehab static int saa7164_encoder_pause_port(struct saa7164_port *port)
913b285192aSMauro Carvalho Chehab {
914b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
915b285192aSMauro Carvalho Chehab 	int ret;
916b285192aSMauro Carvalho Chehab 
917b285192aSMauro Carvalho Chehab 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
918b285192aSMauro Carvalho Chehab 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
919b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
920b285192aSMauro Carvalho Chehab 			__func__, ret);
921b285192aSMauro Carvalho Chehab 		ret = -EIO;
922b285192aSMauro Carvalho Chehab 	} else {
923b285192aSMauro Carvalho Chehab 		dprintk(DBGLVL_ENC, "%s()   Paused\n", __func__);
924b285192aSMauro Carvalho Chehab 		ret = 0;
925b285192aSMauro Carvalho Chehab 	}
926b285192aSMauro Carvalho Chehab 
927b285192aSMauro Carvalho Chehab 	return ret;
928b285192aSMauro Carvalho Chehab }
929b285192aSMauro Carvalho Chehab 
930b285192aSMauro Carvalho Chehab /* Firmware is very windows centric, meaning you have to transition
931b285192aSMauro Carvalho Chehab  * the part through AVStream / KS Windows stages, forwards or backwards.
932b285192aSMauro Carvalho Chehab  * States are: stopped, acquired (h/w), paused, started.
933b285192aSMauro Carvalho Chehab  * We have to leave here will all of the soft buffers on the free list,
934b285192aSMauro Carvalho Chehab  * else the cfg_post() func won't have soft buffers to correctly configure.
935b285192aSMauro Carvalho Chehab  */
936b285192aSMauro Carvalho Chehab static int saa7164_encoder_stop_streaming(struct saa7164_port *port)
937b285192aSMauro Carvalho Chehab {
938b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
939b285192aSMauro Carvalho Chehab 	struct saa7164_buffer *buf;
940b285192aSMauro Carvalho Chehab 	struct saa7164_user_buffer *ubuf;
941b285192aSMauro Carvalho Chehab 	struct list_head *c, *n;
942b285192aSMauro Carvalho Chehab 	int ret;
943b285192aSMauro Carvalho Chehab 
944b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
945b285192aSMauro Carvalho Chehab 
946b285192aSMauro Carvalho Chehab 	ret = saa7164_encoder_pause_port(port);
947b285192aSMauro Carvalho Chehab 	ret = saa7164_encoder_acquire_port(port);
948b285192aSMauro Carvalho Chehab 	ret = saa7164_encoder_stop_port(port);
949b285192aSMauro Carvalho Chehab 
950b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s(port=%d) Hardware stopped\n", __func__,
951b285192aSMauro Carvalho Chehab 		port->nr);
952b285192aSMauro Carvalho Chehab 
953b285192aSMauro Carvalho Chehab 	/* Reset the state of any allocated buffer resources */
954b285192aSMauro Carvalho Chehab 	mutex_lock(&port->dmaqueue_lock);
955b285192aSMauro Carvalho Chehab 
956b285192aSMauro Carvalho Chehab 	/* Reset the hard and soft buffer state */
957b285192aSMauro Carvalho Chehab 	list_for_each_safe(c, n, &port->dmaqueue.list) {
958b285192aSMauro Carvalho Chehab 		buf = list_entry(c, struct saa7164_buffer, list);
959b285192aSMauro Carvalho Chehab 		buf->flags = SAA7164_BUFFER_FREE;
960b285192aSMauro Carvalho Chehab 		buf->pos = 0;
961b285192aSMauro Carvalho Chehab 	}
962b285192aSMauro Carvalho Chehab 
963b285192aSMauro Carvalho Chehab 	list_for_each_safe(c, n, &port->list_buf_used.list) {
964b285192aSMauro Carvalho Chehab 		ubuf = list_entry(c, struct saa7164_user_buffer, list);
965b285192aSMauro Carvalho Chehab 		ubuf->pos = 0;
966b285192aSMauro Carvalho Chehab 		list_move_tail(&ubuf->list, &port->list_buf_free.list);
967b285192aSMauro Carvalho Chehab 	}
968b285192aSMauro Carvalho Chehab 
969b285192aSMauro Carvalho Chehab 	mutex_unlock(&port->dmaqueue_lock);
970b285192aSMauro Carvalho Chehab 
971b285192aSMauro Carvalho Chehab 	/* Free any allocated resources */
972b285192aSMauro Carvalho Chehab 	saa7164_encoder_buffers_dealloc(port);
973b285192aSMauro Carvalho Chehab 
974b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s(port=%d) Released\n", __func__, port->nr);
975b285192aSMauro Carvalho Chehab 
976b285192aSMauro Carvalho Chehab 	return ret;
977b285192aSMauro Carvalho Chehab }
978b285192aSMauro Carvalho Chehab 
979b285192aSMauro Carvalho Chehab static int saa7164_encoder_start_streaming(struct saa7164_port *port)
980b285192aSMauro Carvalho Chehab {
981b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
982b285192aSMauro Carvalho Chehab 	int result, ret = 0;
983b285192aSMauro Carvalho Chehab 
984b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
985b285192aSMauro Carvalho Chehab 
986b285192aSMauro Carvalho Chehab 	port->done_first_interrupt = 0;
987b285192aSMauro Carvalho Chehab 
988b285192aSMauro Carvalho Chehab 	/* allocate all of the PCIe DMA buffer resources on the fly,
989b285192aSMauro Carvalho Chehab 	 * allowing switching between TS and PS payloads without
990b285192aSMauro Carvalho Chehab 	 * requiring a complete driver reload.
991b285192aSMauro Carvalho Chehab 	 */
992b285192aSMauro Carvalho Chehab 	saa7164_encoder_buffers_alloc(port);
993b285192aSMauro Carvalho Chehab 
994b285192aSMauro Carvalho Chehab 	/* Configure the encoder with any cache values */
995b285192aSMauro Carvalho Chehab 	saa7164_api_set_encoder(port);
996b285192aSMauro Carvalho Chehab 	saa7164_api_get_encoder(port);
997b285192aSMauro Carvalho Chehab 
998b285192aSMauro Carvalho Chehab 	/* Place the empty buffers on the hardware */
999b285192aSMauro Carvalho Chehab 	saa7164_buffer_cfg_port(port);
1000b285192aSMauro Carvalho Chehab 
1001b285192aSMauro Carvalho Chehab 	/* Acquire the hardware */
1002b285192aSMauro Carvalho Chehab 	result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
1003b285192aSMauro Carvalho Chehab 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1004b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
1005b285192aSMauro Carvalho Chehab 			__func__, result);
1006b285192aSMauro Carvalho Chehab 
1007b285192aSMauro Carvalho Chehab 		/* Stop the hardware, regardless */
1008b285192aSMauro Carvalho Chehab 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1009b285192aSMauro Carvalho Chehab 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1010b285192aSMauro Carvalho Chehab 			printk(KERN_ERR "%s() acquire/forced stop transition "
1011b285192aSMauro Carvalho Chehab 				"failed, res = 0x%x\n", __func__, result);
1012b285192aSMauro Carvalho Chehab 		}
1013b285192aSMauro Carvalho Chehab 		ret = -EIO;
1014b285192aSMauro Carvalho Chehab 		goto out;
1015b285192aSMauro Carvalho Chehab 	} else
1016b285192aSMauro Carvalho Chehab 		dprintk(DBGLVL_ENC, "%s()   Acquired\n", __func__);
1017b285192aSMauro Carvalho Chehab 
1018b285192aSMauro Carvalho Chehab 	/* Pause the hardware */
1019b285192aSMauro Carvalho Chehab 	result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
1020b285192aSMauro Carvalho Chehab 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1021b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
1022b285192aSMauro Carvalho Chehab 				__func__, result);
1023b285192aSMauro Carvalho Chehab 
1024b285192aSMauro Carvalho Chehab 		/* Stop the hardware, regardless */
1025b285192aSMauro Carvalho Chehab 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1026b285192aSMauro Carvalho Chehab 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1027b285192aSMauro Carvalho Chehab 			printk(KERN_ERR "%s() pause/forced stop transition "
1028b285192aSMauro Carvalho Chehab 				"failed, res = 0x%x\n", __func__, result);
1029b285192aSMauro Carvalho Chehab 		}
1030b285192aSMauro Carvalho Chehab 
1031b285192aSMauro Carvalho Chehab 		ret = -EIO;
1032b285192aSMauro Carvalho Chehab 		goto out;
1033b285192aSMauro Carvalho Chehab 	} else
1034b285192aSMauro Carvalho Chehab 		dprintk(DBGLVL_ENC, "%s()   Paused\n", __func__);
1035b285192aSMauro Carvalho Chehab 
1036b285192aSMauro Carvalho Chehab 	/* Start the hardware */
1037b285192aSMauro Carvalho Chehab 	result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
1038b285192aSMauro Carvalho Chehab 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1039b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
1040b285192aSMauro Carvalho Chehab 				__func__, result);
1041b285192aSMauro Carvalho Chehab 
1042b285192aSMauro Carvalho Chehab 		/* Stop the hardware, regardless */
1043b285192aSMauro Carvalho Chehab 		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
1044b285192aSMauro Carvalho Chehab 		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
1045b285192aSMauro Carvalho Chehab 			printk(KERN_ERR "%s() run/forced stop transition "
1046b285192aSMauro Carvalho Chehab 				"failed, res = 0x%x\n", __func__, result);
1047b285192aSMauro Carvalho Chehab 		}
1048b285192aSMauro Carvalho Chehab 
1049b285192aSMauro Carvalho Chehab 		ret = -EIO;
1050b285192aSMauro Carvalho Chehab 	} else
1051b285192aSMauro Carvalho Chehab 		dprintk(DBGLVL_ENC, "%s()   Running\n", __func__);
1052b285192aSMauro Carvalho Chehab 
1053b285192aSMauro Carvalho Chehab out:
1054b285192aSMauro Carvalho Chehab 	return ret;
1055b285192aSMauro Carvalho Chehab }
1056b285192aSMauro Carvalho Chehab 
1057b285192aSMauro Carvalho Chehab static int fops_open(struct file *file)
1058b285192aSMauro Carvalho Chehab {
1059b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev;
1060b285192aSMauro Carvalho Chehab 	struct saa7164_port *port;
1061b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh;
1062b285192aSMauro Carvalho Chehab 
1063b285192aSMauro Carvalho Chehab 	port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
1064b285192aSMauro Carvalho Chehab 	if (!port)
1065b285192aSMauro Carvalho Chehab 		return -ENODEV;
1066b285192aSMauro Carvalho Chehab 
1067b285192aSMauro Carvalho Chehab 	dev = port->dev;
1068b285192aSMauro Carvalho Chehab 
1069b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1070b285192aSMauro Carvalho Chehab 
1071b285192aSMauro Carvalho Chehab 	/* allocate + initialize per filehandle data */
1072b285192aSMauro Carvalho Chehab 	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1073b285192aSMauro Carvalho Chehab 	if (NULL == fh)
1074b285192aSMauro Carvalho Chehab 		return -ENOMEM;
1075b285192aSMauro Carvalho Chehab 
1076b285192aSMauro Carvalho Chehab 	file->private_data = fh;
1077b285192aSMauro Carvalho Chehab 	fh->port = port;
1078b285192aSMauro Carvalho Chehab 
1079b285192aSMauro Carvalho Chehab 	return 0;
1080b285192aSMauro Carvalho Chehab }
1081b285192aSMauro Carvalho Chehab 
1082b285192aSMauro Carvalho Chehab static int fops_release(struct file *file)
1083b285192aSMauro Carvalho Chehab {
1084b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
1085b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
1086b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
1087b285192aSMauro Carvalho Chehab 
1088b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1089b285192aSMauro Carvalho Chehab 
1090b285192aSMauro Carvalho Chehab 	/* Shut device down on last close */
1091b285192aSMauro Carvalho Chehab 	if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
1092b285192aSMauro Carvalho Chehab 		if (atomic_dec_return(&port->v4l_reader_count) == 0) {
1093b285192aSMauro Carvalho Chehab 			/* stop mpeg capture then cancel buffers */
1094b285192aSMauro Carvalho Chehab 			saa7164_encoder_stop_streaming(port);
1095b285192aSMauro Carvalho Chehab 		}
1096b285192aSMauro Carvalho Chehab 	}
1097b285192aSMauro Carvalho Chehab 
1098b285192aSMauro Carvalho Chehab 	file->private_data = NULL;
1099b285192aSMauro Carvalho Chehab 	kfree(fh);
1100b285192aSMauro Carvalho Chehab 
1101b285192aSMauro Carvalho Chehab 	return 0;
1102b285192aSMauro Carvalho Chehab }
1103b285192aSMauro Carvalho Chehab 
11045faf7db8SMauro Carvalho Chehab static struct
11055faf7db8SMauro Carvalho Chehab saa7164_user_buffer *saa7164_enc_next_buf(struct saa7164_port *port)
1106b285192aSMauro Carvalho Chehab {
1107b285192aSMauro Carvalho Chehab 	struct saa7164_user_buffer *ubuf = NULL;
1108b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
1109b285192aSMauro Carvalho Chehab 	u32 crc;
1110b285192aSMauro Carvalho Chehab 
1111b285192aSMauro Carvalho Chehab 	mutex_lock(&port->dmaqueue_lock);
1112b285192aSMauro Carvalho Chehab 	if (!list_empty(&port->list_buf_used.list)) {
1113b285192aSMauro Carvalho Chehab 		ubuf = list_first_entry(&port->list_buf_used.list,
1114b285192aSMauro Carvalho Chehab 			struct saa7164_user_buffer, list);
1115b285192aSMauro Carvalho Chehab 
1116b285192aSMauro Carvalho Chehab 		if (crc_checking) {
1117b285192aSMauro Carvalho Chehab 			crc = crc32(0, ubuf->data, ubuf->actual_size);
1118b285192aSMauro Carvalho Chehab 			if (crc != ubuf->crc) {
1119b285192aSMauro Carvalho Chehab 				printk(KERN_ERR
1120b285192aSMauro Carvalho Chehab 		"%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
1121b285192aSMauro Carvalho Chehab 					__func__,
1122b285192aSMauro Carvalho Chehab 					ubuf, ubuf->crc, crc);
1123b285192aSMauro Carvalho Chehab 			}
1124b285192aSMauro Carvalho Chehab 		}
1125b285192aSMauro Carvalho Chehab 
1126b285192aSMauro Carvalho Chehab 	}
1127b285192aSMauro Carvalho Chehab 	mutex_unlock(&port->dmaqueue_lock);
1128b285192aSMauro Carvalho Chehab 
1129b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s() returns %p\n", __func__, ubuf);
1130b285192aSMauro Carvalho Chehab 
1131b285192aSMauro Carvalho Chehab 	return ubuf;
1132b285192aSMauro Carvalho Chehab }
1133b285192aSMauro Carvalho Chehab 
1134b285192aSMauro Carvalho Chehab static ssize_t fops_read(struct file *file, char __user *buffer,
1135b285192aSMauro Carvalho Chehab 	size_t count, loff_t *pos)
1136b285192aSMauro Carvalho Chehab {
1137b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh = file->private_data;
1138b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
1139b285192aSMauro Carvalho Chehab 	struct saa7164_user_buffer *ubuf = NULL;
1140b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
1141b285192aSMauro Carvalho Chehab 	int ret = 0;
1142b285192aSMauro Carvalho Chehab 	int rem, cnt;
1143b285192aSMauro Carvalho Chehab 	u8 *p;
1144b285192aSMauro Carvalho Chehab 
1145b285192aSMauro Carvalho Chehab 	port->last_read_msecs_diff = port->last_read_msecs;
1146b285192aSMauro Carvalho Chehab 	port->last_read_msecs = jiffies_to_msecs(jiffies);
1147b285192aSMauro Carvalho Chehab 	port->last_read_msecs_diff = port->last_read_msecs -
1148b285192aSMauro Carvalho Chehab 		port->last_read_msecs_diff;
1149b285192aSMauro Carvalho Chehab 
1150b285192aSMauro Carvalho Chehab 	saa7164_histogram_update(&port->read_interval,
1151b285192aSMauro Carvalho Chehab 		port->last_read_msecs_diff);
1152b285192aSMauro Carvalho Chehab 
1153b285192aSMauro Carvalho Chehab 	if (*pos) {
1154b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() ESPIPE\n", __func__);
1155b285192aSMauro Carvalho Chehab 		return -ESPIPE;
1156b285192aSMauro Carvalho Chehab 	}
1157b285192aSMauro Carvalho Chehab 
1158b285192aSMauro Carvalho Chehab 	if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1159b285192aSMauro Carvalho Chehab 		if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1160b285192aSMauro Carvalho Chehab 
1161b285192aSMauro Carvalho Chehab 			if (saa7164_encoder_initialize(port) < 0) {
1162b285192aSMauro Carvalho Chehab 				printk(KERN_ERR "%s() EINVAL\n", __func__);
1163b285192aSMauro Carvalho Chehab 				return -EINVAL;
1164b285192aSMauro Carvalho Chehab 			}
1165b285192aSMauro Carvalho Chehab 
1166b285192aSMauro Carvalho Chehab 			saa7164_encoder_start_streaming(port);
1167b285192aSMauro Carvalho Chehab 			msleep(200);
1168b285192aSMauro Carvalho Chehab 		}
1169b285192aSMauro Carvalho Chehab 	}
1170b285192aSMauro Carvalho Chehab 
1171b285192aSMauro Carvalho Chehab 	/* blocking wait for buffer */
1172b285192aSMauro Carvalho Chehab 	if ((file->f_flags & O_NONBLOCK) == 0) {
1173b285192aSMauro Carvalho Chehab 		if (wait_event_interruptible(port->wait_read,
1174b285192aSMauro Carvalho Chehab 			saa7164_enc_next_buf(port))) {
1175b285192aSMauro Carvalho Chehab 				printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
1176b285192aSMauro Carvalho Chehab 				return -ERESTARTSYS;
1177b285192aSMauro Carvalho Chehab 		}
1178b285192aSMauro Carvalho Chehab 	}
1179b285192aSMauro Carvalho Chehab 
1180b285192aSMauro Carvalho Chehab 	/* Pull the first buffer from the used list */
1181b285192aSMauro Carvalho Chehab 	ubuf = saa7164_enc_next_buf(port);
1182b285192aSMauro Carvalho Chehab 
1183b285192aSMauro Carvalho Chehab 	while ((count > 0) && ubuf) {
1184b285192aSMauro Carvalho Chehab 
1185b285192aSMauro Carvalho Chehab 		/* set remaining bytes to copy */
1186b285192aSMauro Carvalho Chehab 		rem = ubuf->actual_size - ubuf->pos;
1187b285192aSMauro Carvalho Chehab 		cnt = rem > count ? count : rem;
1188b285192aSMauro Carvalho Chehab 
1189b285192aSMauro Carvalho Chehab 		p = ubuf->data + ubuf->pos;
1190b285192aSMauro Carvalho Chehab 
1191b285192aSMauro Carvalho Chehab 		dprintk(DBGLVL_ENC,
1192b285192aSMauro Carvalho Chehab 			"%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
1193b285192aSMauro Carvalho Chehab 			__func__, (int)count, cnt, rem, ubuf, ubuf->pos);
1194b285192aSMauro Carvalho Chehab 
1195b285192aSMauro Carvalho Chehab 		if (copy_to_user(buffer, p, cnt)) {
1196b285192aSMauro Carvalho Chehab 			printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
1197b285192aSMauro Carvalho Chehab 			if (!ret) {
1198b285192aSMauro Carvalho Chehab 				printk(KERN_ERR "%s() EFAULT\n", __func__);
1199b285192aSMauro Carvalho Chehab 				ret = -EFAULT;
1200b285192aSMauro Carvalho Chehab 			}
1201b285192aSMauro Carvalho Chehab 			goto err;
1202b285192aSMauro Carvalho Chehab 		}
1203b285192aSMauro Carvalho Chehab 
1204b285192aSMauro Carvalho Chehab 		ubuf->pos += cnt;
1205b285192aSMauro Carvalho Chehab 		count -= cnt;
1206b285192aSMauro Carvalho Chehab 		buffer += cnt;
1207b285192aSMauro Carvalho Chehab 		ret += cnt;
1208b285192aSMauro Carvalho Chehab 
1209b285192aSMauro Carvalho Chehab 		if (ubuf->pos > ubuf->actual_size)
1210b285192aSMauro Carvalho Chehab 			printk(KERN_ERR "read() pos > actual, huh?\n");
1211b285192aSMauro Carvalho Chehab 
1212b285192aSMauro Carvalho Chehab 		if (ubuf->pos == ubuf->actual_size) {
1213b285192aSMauro Carvalho Chehab 
1214b285192aSMauro Carvalho Chehab 			/* finished with current buffer, take next buffer */
1215b285192aSMauro Carvalho Chehab 
1216b285192aSMauro Carvalho Chehab 			/* Requeue the buffer on the free list */
1217b285192aSMauro Carvalho Chehab 			ubuf->pos = 0;
1218b285192aSMauro Carvalho Chehab 
1219b285192aSMauro Carvalho Chehab 			mutex_lock(&port->dmaqueue_lock);
1220b285192aSMauro Carvalho Chehab 			list_move_tail(&ubuf->list, &port->list_buf_free.list);
1221b285192aSMauro Carvalho Chehab 			mutex_unlock(&port->dmaqueue_lock);
1222b285192aSMauro Carvalho Chehab 
1223b285192aSMauro Carvalho Chehab 			/* Dequeue next */
1224b285192aSMauro Carvalho Chehab 			if ((file->f_flags & O_NONBLOCK) == 0) {
1225b285192aSMauro Carvalho Chehab 				if (wait_event_interruptible(port->wait_read,
1226b285192aSMauro Carvalho Chehab 					saa7164_enc_next_buf(port))) {
1227b285192aSMauro Carvalho Chehab 						break;
1228b285192aSMauro Carvalho Chehab 				}
1229b285192aSMauro Carvalho Chehab 			}
1230b285192aSMauro Carvalho Chehab 			ubuf = saa7164_enc_next_buf(port);
1231b285192aSMauro Carvalho Chehab 		}
1232b285192aSMauro Carvalho Chehab 	}
1233b285192aSMauro Carvalho Chehab err:
1234b285192aSMauro Carvalho Chehab 	if (!ret && !ubuf)
1235b285192aSMauro Carvalho Chehab 		ret = -EAGAIN;
1236b285192aSMauro Carvalho Chehab 
1237b285192aSMauro Carvalho Chehab 	return ret;
1238b285192aSMauro Carvalho Chehab }
1239b285192aSMauro Carvalho Chehab 
1240b285192aSMauro Carvalho Chehab static unsigned int fops_poll(struct file *file, poll_table *wait)
1241b285192aSMauro Carvalho Chehab {
1242b285192aSMauro Carvalho Chehab 	struct saa7164_encoder_fh *fh =
1243b285192aSMauro Carvalho Chehab 		(struct saa7164_encoder_fh *)file->private_data;
1244b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = fh->port;
1245b285192aSMauro Carvalho Chehab 	unsigned int mask = 0;
1246b285192aSMauro Carvalho Chehab 
1247b285192aSMauro Carvalho Chehab 	port->last_poll_msecs_diff = port->last_poll_msecs;
1248b285192aSMauro Carvalho Chehab 	port->last_poll_msecs = jiffies_to_msecs(jiffies);
1249b285192aSMauro Carvalho Chehab 	port->last_poll_msecs_diff = port->last_poll_msecs -
1250b285192aSMauro Carvalho Chehab 		port->last_poll_msecs_diff;
1251b285192aSMauro Carvalho Chehab 
1252b285192aSMauro Carvalho Chehab 	saa7164_histogram_update(&port->poll_interval,
1253b285192aSMauro Carvalho Chehab 		port->last_poll_msecs_diff);
1254b285192aSMauro Carvalho Chehab 
1255b285192aSMauro Carvalho Chehab 	if (!video_is_registered(port->v4l_device))
1256b285192aSMauro Carvalho Chehab 		return -EIO;
1257b285192aSMauro Carvalho Chehab 
1258b285192aSMauro Carvalho Chehab 	if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1259b285192aSMauro Carvalho Chehab 		if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1260b285192aSMauro Carvalho Chehab 			if (saa7164_encoder_initialize(port) < 0)
1261b285192aSMauro Carvalho Chehab 				return -EINVAL;
1262b285192aSMauro Carvalho Chehab 			saa7164_encoder_start_streaming(port);
1263b285192aSMauro Carvalho Chehab 			msleep(200);
1264b285192aSMauro Carvalho Chehab 		}
1265b285192aSMauro Carvalho Chehab 	}
1266b285192aSMauro Carvalho Chehab 
1267b285192aSMauro Carvalho Chehab 	/* blocking wait for buffer */
1268b285192aSMauro Carvalho Chehab 	if ((file->f_flags & O_NONBLOCK) == 0) {
1269b285192aSMauro Carvalho Chehab 		if (wait_event_interruptible(port->wait_read,
1270b285192aSMauro Carvalho Chehab 			saa7164_enc_next_buf(port))) {
1271b285192aSMauro Carvalho Chehab 				return -ERESTARTSYS;
1272b285192aSMauro Carvalho Chehab 		}
1273b285192aSMauro Carvalho Chehab 	}
1274b285192aSMauro Carvalho Chehab 
1275b285192aSMauro Carvalho Chehab 	/* Pull the first buffer from the used list */
1276b285192aSMauro Carvalho Chehab 	if (!list_empty(&port->list_buf_used.list))
1277b285192aSMauro Carvalho Chehab 		mask |= POLLIN | POLLRDNORM;
1278b285192aSMauro Carvalho Chehab 
1279b285192aSMauro Carvalho Chehab 	return mask;
1280b285192aSMauro Carvalho Chehab }
1281b285192aSMauro Carvalho Chehab 
1282b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations mpeg_fops = {
1283b285192aSMauro Carvalho Chehab 	.owner		= THIS_MODULE,
1284b285192aSMauro Carvalho Chehab 	.open		= fops_open,
1285b285192aSMauro Carvalho Chehab 	.release	= fops_release,
1286b285192aSMauro Carvalho Chehab 	.read		= fops_read,
1287b285192aSMauro Carvalho Chehab 	.poll		= fops_poll,
1288b285192aSMauro Carvalho Chehab 	.unlocked_ioctl	= video_ioctl2,
1289b285192aSMauro Carvalho Chehab };
1290b285192aSMauro Carvalho Chehab 
12915faf7db8SMauro Carvalho Chehab static int saa7164_g_chip_ident(struct file *file, void *fh,
1292b285192aSMauro Carvalho Chehab 				struct v4l2_dbg_chip_ident *chip)
1293b285192aSMauro Carvalho Chehab {
1294b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port;
1295b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
1296b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1297b285192aSMauro Carvalho Chehab 
1298b285192aSMauro Carvalho Chehab 	return 0;
1299b285192aSMauro Carvalho Chehab }
1300b285192aSMauro Carvalho Chehab 
1301b4c13d3dSPaul Bolle #ifdef CONFIG_VIDEO_ADV_DEBUG
13025faf7db8SMauro Carvalho Chehab static int saa7164_g_register(struct file *file, void *fh,
1303b285192aSMauro Carvalho Chehab 			      struct v4l2_dbg_register *reg)
1304b285192aSMauro Carvalho Chehab {
1305b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port;
1306b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
1307b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1308b285192aSMauro Carvalho Chehab 
1309b285192aSMauro Carvalho Chehab 	if (!capable(CAP_SYS_ADMIN))
1310b285192aSMauro Carvalho Chehab 		return -EPERM;
1311b285192aSMauro Carvalho Chehab 
1312b285192aSMauro Carvalho Chehab 	return 0;
1313b285192aSMauro Carvalho Chehab }
1314b285192aSMauro Carvalho Chehab 
13155faf7db8SMauro Carvalho Chehab static int saa7164_s_register(struct file *file, void *fh,
1316b285192aSMauro Carvalho Chehab 			      struct v4l2_dbg_register *reg)
1317b285192aSMauro Carvalho Chehab {
1318b285192aSMauro Carvalho Chehab 	struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port;
1319b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
1320b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1321b285192aSMauro Carvalho Chehab 
1322b285192aSMauro Carvalho Chehab 	if (!capable(CAP_SYS_ADMIN))
1323b285192aSMauro Carvalho Chehab 		return -EPERM;
1324b285192aSMauro Carvalho Chehab 
1325b285192aSMauro Carvalho Chehab 	return 0;
1326b285192aSMauro Carvalho Chehab }
1327b4c13d3dSPaul Bolle #endif
1328b285192aSMauro Carvalho Chehab 
1329b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
1330b285192aSMauro Carvalho Chehab 	.vidioc_s_std		 = vidioc_s_std,
1331b285192aSMauro Carvalho Chehab 	.vidioc_enum_input	 = vidioc_enum_input,
1332b285192aSMauro Carvalho Chehab 	.vidioc_g_input		 = vidioc_g_input,
1333b285192aSMauro Carvalho Chehab 	.vidioc_s_input		 = vidioc_s_input,
1334b285192aSMauro Carvalho Chehab 	.vidioc_g_tuner		 = vidioc_g_tuner,
1335b285192aSMauro Carvalho Chehab 	.vidioc_s_tuner		 = vidioc_s_tuner,
1336b285192aSMauro Carvalho Chehab 	.vidioc_g_frequency	 = vidioc_g_frequency,
1337b285192aSMauro Carvalho Chehab 	.vidioc_s_frequency	 = vidioc_s_frequency,
1338b285192aSMauro Carvalho Chehab 	.vidioc_s_ctrl		 = vidioc_s_ctrl,
1339b285192aSMauro Carvalho Chehab 	.vidioc_g_ctrl		 = vidioc_g_ctrl,
1340b285192aSMauro Carvalho Chehab 	.vidioc_querycap	 = vidioc_querycap,
1341b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1342b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap	 = vidioc_g_fmt_vid_cap,
1343b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap	 = vidioc_try_fmt_vid_cap,
1344b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap	 = vidioc_s_fmt_vid_cap,
1345b285192aSMauro Carvalho Chehab 	.vidioc_g_ext_ctrls	 = vidioc_g_ext_ctrls,
1346b285192aSMauro Carvalho Chehab 	.vidioc_s_ext_ctrls	 = vidioc_s_ext_ctrls,
1347b285192aSMauro Carvalho Chehab 	.vidioc_try_ext_ctrls	 = vidioc_try_ext_ctrls,
1348b285192aSMauro Carvalho Chehab 	.vidioc_queryctrl	 = vidioc_queryctrl,
1349b285192aSMauro Carvalho Chehab 	.vidioc_g_chip_ident	 = saa7164_g_chip_ident,
1350b285192aSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
1351b285192aSMauro Carvalho Chehab 	.vidioc_g_register	 = saa7164_g_register,
1352b285192aSMauro Carvalho Chehab 	.vidioc_s_register	 = saa7164_s_register,
1353b285192aSMauro Carvalho Chehab #endif
1354b285192aSMauro Carvalho Chehab };
1355b285192aSMauro Carvalho Chehab 
1356b285192aSMauro Carvalho Chehab static struct video_device saa7164_mpeg_template = {
1357b285192aSMauro Carvalho Chehab 	.name          = "saa7164",
1358b285192aSMauro Carvalho Chehab 	.fops          = &mpeg_fops,
1359b285192aSMauro Carvalho Chehab 	.ioctl_ops     = &mpeg_ioctl_ops,
1360b285192aSMauro Carvalho Chehab 	.minor         = -1,
1361b285192aSMauro Carvalho Chehab 	.tvnorms       = SAA7164_NORMS,
1362b285192aSMauro Carvalho Chehab 	.current_norm  = V4L2_STD_NTSC_M,
1363b285192aSMauro Carvalho Chehab };
1364b285192aSMauro Carvalho Chehab 
1365b285192aSMauro Carvalho Chehab static struct video_device *saa7164_encoder_alloc(
1366b285192aSMauro Carvalho Chehab 	struct saa7164_port *port,
1367b285192aSMauro Carvalho Chehab 	struct pci_dev *pci,
1368b285192aSMauro Carvalho Chehab 	struct video_device *template,
1369b285192aSMauro Carvalho Chehab 	char *type)
1370b285192aSMauro Carvalho Chehab {
1371b285192aSMauro Carvalho Chehab 	struct video_device *vfd;
1372b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
1373b285192aSMauro Carvalho Chehab 
1374b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1375b285192aSMauro Carvalho Chehab 
1376b285192aSMauro Carvalho Chehab 	vfd = video_device_alloc();
1377b285192aSMauro Carvalho Chehab 	if (NULL == vfd)
1378b285192aSMauro Carvalho Chehab 		return NULL;
1379b285192aSMauro Carvalho Chehab 
1380b285192aSMauro Carvalho Chehab 	*vfd = *template;
1381b285192aSMauro Carvalho Chehab 	snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
1382b285192aSMauro Carvalho Chehab 		type, saa7164_boards[dev->board].name);
1383b285192aSMauro Carvalho Chehab 
1384b285192aSMauro Carvalho Chehab 	vfd->parent  = &pci->dev;
1385b285192aSMauro Carvalho Chehab 	vfd->release = video_device_release;
1386b285192aSMauro Carvalho Chehab 	return vfd;
1387b285192aSMauro Carvalho Chehab }
1388b285192aSMauro Carvalho Chehab 
1389b285192aSMauro Carvalho Chehab int saa7164_encoder_register(struct saa7164_port *port)
1390b285192aSMauro Carvalho Chehab {
1391b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
1392b285192aSMauro Carvalho Chehab 	int result = -ENODEV;
1393b285192aSMauro Carvalho Chehab 
1394b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s()\n", __func__);
1395b285192aSMauro Carvalho Chehab 
1396b285192aSMauro Carvalho Chehab 	if (port->type != SAA7164_MPEG_ENCODER)
1397b285192aSMauro Carvalho Chehab 		BUG();
1398b285192aSMauro Carvalho Chehab 
1399b285192aSMauro Carvalho Chehab 	/* Sanity check that the PCI configuration space is active */
1400b285192aSMauro Carvalho Chehab 	if (port->hwcfg.BARLocation == 0) {
1401b285192aSMauro Carvalho Chehab 		printk(KERN_ERR "%s() failed "
1402b285192aSMauro Carvalho Chehab 		       "(errno = %d), NO PCI configuration\n",
1403b285192aSMauro Carvalho Chehab 			__func__, result);
1404b285192aSMauro Carvalho Chehab 		result = -ENOMEM;
1405b285192aSMauro Carvalho Chehab 		goto failed;
1406b285192aSMauro Carvalho Chehab 	}
1407b285192aSMauro Carvalho Chehab 
1408b285192aSMauro Carvalho Chehab 	/* Establish encoder defaults here */
1409b285192aSMauro Carvalho Chehab 	/* Set default TV standard */
1410b285192aSMauro Carvalho Chehab 	port->encodernorm = saa7164_tvnorms[0];
1411b285192aSMauro Carvalho Chehab 	port->width = 720;
1412b285192aSMauro Carvalho Chehab 	port->mux_input = 1; /* Composite */
1413b285192aSMauro Carvalho Chehab 	port->video_format = EU_VIDEO_FORMAT_MPEG_2;
1414b285192aSMauro Carvalho Chehab 	port->audio_format = 0;
1415b285192aSMauro Carvalho Chehab 	port->video_resolution = 0;
1416b285192aSMauro Carvalho Chehab 	port->ctl_brightness = 127;
1417b285192aSMauro Carvalho Chehab 	port->ctl_contrast = 66;
1418b285192aSMauro Carvalho Chehab 	port->ctl_hue = 128;
1419b285192aSMauro Carvalho Chehab 	port->ctl_saturation = 62;
1420b285192aSMauro Carvalho Chehab 	port->ctl_sharpness = 8;
1421b285192aSMauro Carvalho Chehab 	port->encoder_params.bitrate = ENCODER_DEF_BITRATE;
1422b285192aSMauro Carvalho Chehab 	port->encoder_params.bitrate_peak = ENCODER_DEF_BITRATE;
1423b285192aSMauro Carvalho Chehab 	port->encoder_params.bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
1424b285192aSMauro Carvalho Chehab 	port->encoder_params.stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
1425b285192aSMauro Carvalho Chehab 	port->encoder_params.ctl_mute = 0;
1426b285192aSMauro Carvalho Chehab 	port->encoder_params.ctl_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
1427b285192aSMauro Carvalho Chehab 	port->encoder_params.refdist = 1;
1428b285192aSMauro Carvalho Chehab 	port->encoder_params.gop_size = SAA7164_ENCODER_DEFAULT_GOP_SIZE;
1429b285192aSMauro Carvalho Chehab 
1430b285192aSMauro Carvalho Chehab 	if (port->encodernorm.id & V4L2_STD_525_60)
1431b285192aSMauro Carvalho Chehab 		port->height = 480;
1432b285192aSMauro Carvalho Chehab 	else
1433b285192aSMauro Carvalho Chehab 		port->height = 576;
1434b285192aSMauro Carvalho Chehab 
1435b285192aSMauro Carvalho Chehab 	/* Allocate and register the video device node */
1436b285192aSMauro Carvalho Chehab 	port->v4l_device = saa7164_encoder_alloc(port,
1437b285192aSMauro Carvalho Chehab 		dev->pci, &saa7164_mpeg_template, "mpeg");
1438b285192aSMauro Carvalho Chehab 
1439b285192aSMauro Carvalho Chehab 	if (!port->v4l_device) {
1440b285192aSMauro Carvalho Chehab 		printk(KERN_INFO "%s: can't allocate mpeg device\n",
1441b285192aSMauro Carvalho Chehab 			dev->name);
1442b285192aSMauro Carvalho Chehab 		result = -ENOMEM;
1443b285192aSMauro Carvalho Chehab 		goto failed;
1444b285192aSMauro Carvalho Chehab 	}
1445b285192aSMauro Carvalho Chehab 
1446b285192aSMauro Carvalho Chehab 	video_set_drvdata(port->v4l_device, port);
1447b285192aSMauro Carvalho Chehab 	result = video_register_device(port->v4l_device,
1448b285192aSMauro Carvalho Chehab 		VFL_TYPE_GRABBER, -1);
1449b285192aSMauro Carvalho Chehab 	if (result < 0) {
1450b285192aSMauro Carvalho Chehab 		printk(KERN_INFO "%s: can't register mpeg device\n",
1451b285192aSMauro Carvalho Chehab 			dev->name);
1452b285192aSMauro Carvalho Chehab 		/* TODO: We're going to leak here if we don't dealloc
1453b285192aSMauro Carvalho Chehab 		 The buffers above. The unreg function can't deal wit it.
1454b285192aSMauro Carvalho Chehab 		*/
1455b285192aSMauro Carvalho Chehab 		goto failed;
1456b285192aSMauro Carvalho Chehab 	}
1457b285192aSMauro Carvalho Chehab 
1458b285192aSMauro Carvalho Chehab 	printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
1459b285192aSMauro Carvalho Chehab 		dev->name, port->v4l_device->num);
1460b285192aSMauro Carvalho Chehab 
1461b285192aSMauro Carvalho Chehab 	/* Configure the hardware defaults */
1462b285192aSMauro Carvalho Chehab 	saa7164_api_set_videomux(port);
1463b285192aSMauro Carvalho Chehab 	saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
1464b285192aSMauro Carvalho Chehab 	saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
1465b285192aSMauro Carvalho Chehab 	saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
1466b285192aSMauro Carvalho Chehab 	saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
1467b285192aSMauro Carvalho Chehab 	saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
1468b285192aSMauro Carvalho Chehab 	saa7164_api_audio_mute(port, 0);
1469b285192aSMauro Carvalho Chehab 	saa7164_api_set_audio_volume(port, 20);
1470b285192aSMauro Carvalho Chehab 	saa7164_api_set_aspect_ratio(port);
1471b285192aSMauro Carvalho Chehab 
1472b285192aSMauro Carvalho Chehab 	/* Disable audio standard detection, it's buggy */
1473b285192aSMauro Carvalho Chehab 	saa7164_api_set_audio_detection(port, 0);
1474b285192aSMauro Carvalho Chehab 
1475b285192aSMauro Carvalho Chehab 	saa7164_api_set_encoder(port);
1476b285192aSMauro Carvalho Chehab 	saa7164_api_get_encoder(port);
1477b285192aSMauro Carvalho Chehab 
1478b285192aSMauro Carvalho Chehab 	result = 0;
1479b285192aSMauro Carvalho Chehab failed:
1480b285192aSMauro Carvalho Chehab 	return result;
1481b285192aSMauro Carvalho Chehab }
1482b285192aSMauro Carvalho Chehab 
1483b285192aSMauro Carvalho Chehab void saa7164_encoder_unregister(struct saa7164_port *port)
1484b285192aSMauro Carvalho Chehab {
1485b285192aSMauro Carvalho Chehab 	struct saa7164_dev *dev = port->dev;
1486b285192aSMauro Carvalho Chehab 
1487b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);
1488b285192aSMauro Carvalho Chehab 
1489b285192aSMauro Carvalho Chehab 	if (port->type != SAA7164_MPEG_ENCODER)
1490b285192aSMauro Carvalho Chehab 		BUG();
1491b285192aSMauro Carvalho Chehab 
1492b285192aSMauro Carvalho Chehab 	if (port->v4l_device) {
1493b285192aSMauro Carvalho Chehab 		if (port->v4l_device->minor != -1)
1494b285192aSMauro Carvalho Chehab 			video_unregister_device(port->v4l_device);
1495b285192aSMauro Carvalho Chehab 		else
1496b285192aSMauro Carvalho Chehab 			video_device_release(port->v4l_device);
1497b285192aSMauro Carvalho Chehab 
1498b285192aSMauro Carvalho Chehab 		port->v4l_device = NULL;
1499b285192aSMauro Carvalho Chehab 	}
1500b285192aSMauro Carvalho Chehab 
1501b285192aSMauro Carvalho Chehab 	dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
1502b285192aSMauro Carvalho Chehab }
1503b285192aSMauro Carvalho Chehab 
1504