xref: /linux/drivers/media/pci/cx25821/cx25821-video.c (revision b530a447bb588fdf43fdf4eb909e4ee1921d47ac)
1b285192aSMauro Carvalho Chehab /*
2b285192aSMauro Carvalho Chehab  *  Driver for the Conexant CX25821 PCIe bridge
3b285192aSMauro Carvalho Chehab  *
4b285192aSMauro Carvalho Chehab  *  Copyright (C) 2009 Conexant Systems Inc.
5b285192aSMauro Carvalho Chehab  *  Authors  <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6b285192aSMauro Carvalho Chehab  *  Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
7b285192aSMauro Carvalho Chehab  *  Parts adapted/taken from Eduardo Moscoso Rubino
8b285192aSMauro Carvalho Chehab  *  Copyright (C) 2009 Eduardo Moscoso Rubino <moscoso@TopoLogica.com>
9b285192aSMauro Carvalho Chehab  *
10b285192aSMauro Carvalho Chehab  *
11b285192aSMauro Carvalho Chehab  *  This program is free software; you can redistribute it and/or modify
12b285192aSMauro Carvalho Chehab  *  it under the terms of the GNU General Public License as published by
13b285192aSMauro Carvalho Chehab  *  the Free Software Foundation; either version 2 of the License, or
14b285192aSMauro Carvalho Chehab  *  (at your option) any later version.
15b285192aSMauro Carvalho Chehab  *
16b285192aSMauro Carvalho Chehab  *  This program is distributed in the hope that it will be useful,
17b285192aSMauro Carvalho Chehab  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18b285192aSMauro Carvalho Chehab  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19b285192aSMauro Carvalho Chehab  *
20b285192aSMauro Carvalho Chehab  *  GNU General Public License for more details.
21b285192aSMauro Carvalho Chehab  *
22b285192aSMauro Carvalho Chehab  *  You should have received a copy of the GNU General Public License
23b285192aSMauro Carvalho Chehab  *  along with this program; if not, write to the Free Software
24b285192aSMauro Carvalho Chehab  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25b285192aSMauro Carvalho Chehab  */
26b285192aSMauro Carvalho Chehab 
27b285192aSMauro Carvalho Chehab #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28b285192aSMauro Carvalho Chehab 
29b285192aSMauro Carvalho Chehab #include "cx25821-video.h"
30b285192aSMauro Carvalho Chehab 
31b285192aSMauro Carvalho Chehab MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
32b285192aSMauro Carvalho Chehab MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
33b285192aSMauro Carvalho Chehab MODULE_LICENSE("GPL");
34b285192aSMauro Carvalho Chehab 
35b285192aSMauro Carvalho Chehab static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
36b285192aSMauro Carvalho Chehab static unsigned int radio_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
37b285192aSMauro Carvalho Chehab 
38b285192aSMauro Carvalho Chehab module_param_array(video_nr, int, NULL, 0444);
39b285192aSMauro Carvalho Chehab module_param_array(radio_nr, int, NULL, 0444);
40b285192aSMauro Carvalho Chehab 
41b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_nr, "video device numbers");
42b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(radio_nr, "radio device numbers");
43b285192aSMauro Carvalho Chehab 
44b285192aSMauro Carvalho Chehab static unsigned int video_debug = VIDEO_DEBUG;
45b285192aSMauro Carvalho Chehab module_param(video_debug, int, 0644);
46b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
47b285192aSMauro Carvalho Chehab 
48b285192aSMauro Carvalho Chehab static unsigned int irq_debug;
49b285192aSMauro Carvalho Chehab module_param(irq_debug, int, 0644);
50b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
51b285192aSMauro Carvalho Chehab 
52b285192aSMauro Carvalho Chehab unsigned int vid_limit = 16;
53b285192aSMauro Carvalho Chehab module_param(vid_limit, int, 0644);
54b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
55b285192aSMauro Carvalho Chehab 
56b285192aSMauro Carvalho Chehab static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num);
57b285192aSMauro Carvalho Chehab 
58b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops;
59b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops;
60b285192aSMauro Carvalho Chehab 
61b285192aSMauro Carvalho Chehab #define FORMAT_FLAGS_PACKED       0x01
62b285192aSMauro Carvalho Chehab 
63b285192aSMauro Carvalho Chehab struct cx25821_fmt formats[] = {
64b285192aSMauro Carvalho Chehab 	{
65b285192aSMauro Carvalho Chehab 		.name = "8 bpp, gray",
66b285192aSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_GREY,
67b285192aSMauro Carvalho Chehab 		.depth = 8,
68b285192aSMauro Carvalho Chehab 		.flags = FORMAT_FLAGS_PACKED,
69b285192aSMauro Carvalho Chehab 	 }, {
70b285192aSMauro Carvalho Chehab 		.name = "4:1:1, packed, Y41P",
71b285192aSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_Y41P,
72b285192aSMauro Carvalho Chehab 		.depth = 12,
73b285192aSMauro Carvalho Chehab 		.flags = FORMAT_FLAGS_PACKED,
74b285192aSMauro Carvalho Chehab 	}, {
75b285192aSMauro Carvalho Chehab 		.name = "4:2:2, packed, YUYV",
76b285192aSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_YUYV,
77b285192aSMauro Carvalho Chehab 		.depth = 16,
78b285192aSMauro Carvalho Chehab 		.flags = FORMAT_FLAGS_PACKED,
79b285192aSMauro Carvalho Chehab 	}, {
80b285192aSMauro Carvalho Chehab 		.name = "4:2:2, packed, UYVY",
81b285192aSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_UYVY,
82b285192aSMauro Carvalho Chehab 		.depth = 16,
83b285192aSMauro Carvalho Chehab 		.flags = FORMAT_FLAGS_PACKED,
84b285192aSMauro Carvalho Chehab 	}, {
85b285192aSMauro Carvalho Chehab 		.name = "4:2:0, YUV",
86b285192aSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_YUV420,
87b285192aSMauro Carvalho Chehab 		.depth = 12,
88b285192aSMauro Carvalho Chehab 		.flags = FORMAT_FLAGS_PACKED,
89b285192aSMauro Carvalho Chehab 	},
90b285192aSMauro Carvalho Chehab };
91b285192aSMauro Carvalho Chehab 
92b285192aSMauro Carvalho Chehab int cx25821_get_format_size(void)
93b285192aSMauro Carvalho Chehab {
94b285192aSMauro Carvalho Chehab 	return ARRAY_SIZE(formats);
95b285192aSMauro Carvalho Chehab }
96b285192aSMauro Carvalho Chehab 
97b285192aSMauro Carvalho Chehab struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
98b285192aSMauro Carvalho Chehab {
99b285192aSMauro Carvalho Chehab 	unsigned int i;
100b285192aSMauro Carvalho Chehab 
101b285192aSMauro Carvalho Chehab 	if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P)
102b285192aSMauro Carvalho Chehab 		return formats + 1;
103b285192aSMauro Carvalho Chehab 
104b285192aSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(formats); i++)
105b285192aSMauro Carvalho Chehab 		if (formats[i].fourcc == fourcc)
106b285192aSMauro Carvalho Chehab 			return formats + i;
107b285192aSMauro Carvalho Chehab 
108b285192aSMauro Carvalho Chehab 	pr_err("%s(0x%08x) NOT FOUND\n", __func__, fourcc);
109b285192aSMauro Carvalho Chehab 	return NULL;
110b285192aSMauro Carvalho Chehab }
111b285192aSMauro Carvalho Chehab 
112b285192aSMauro Carvalho Chehab void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
113b285192aSMauro Carvalho Chehab 			  u32 count)
114b285192aSMauro Carvalho Chehab {
115b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf;
116b285192aSMauro Carvalho Chehab 	int bc;
117b285192aSMauro Carvalho Chehab 
118b285192aSMauro Carvalho Chehab 	for (bc = 0;; bc++) {
119b285192aSMauro Carvalho Chehab 		if (list_empty(&q->active)) {
120b285192aSMauro Carvalho Chehab 			dprintk(1, "bc=%d (=0: active empty)\n", bc);
121b285192aSMauro Carvalho Chehab 			break;
122b285192aSMauro Carvalho Chehab 		}
123b285192aSMauro Carvalho Chehab 
124b285192aSMauro Carvalho Chehab 		buf = list_entry(q->active.next, struct cx25821_buffer,
125b285192aSMauro Carvalho Chehab 				vb.queue);
126b285192aSMauro Carvalho Chehab 
127b285192aSMauro Carvalho Chehab 		/* count comes from the hw and it is 16bit wide --
128b285192aSMauro Carvalho Chehab 		 * this trick handles wrap-arounds correctly for
129b285192aSMauro Carvalho Chehab 		 * up to 32767 buffers in flight... */
130b285192aSMauro Carvalho Chehab 		if ((s16) (count - buf->count) < 0)
131b285192aSMauro Carvalho Chehab 			break;
132b285192aSMauro Carvalho Chehab 
1338e6057b5SSakari Ailus 		v4l2_get_timestamp(&buf->vb.ts);
134b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_DONE;
135b285192aSMauro Carvalho Chehab 		list_del(&buf->vb.queue);
136b285192aSMauro Carvalho Chehab 		wake_up(&buf->vb.done);
137b285192aSMauro Carvalho Chehab 	}
138b285192aSMauro Carvalho Chehab 
139b285192aSMauro Carvalho Chehab 	if (list_empty(&q->active))
140b285192aSMauro Carvalho Chehab 		del_timer(&q->timeout);
141b285192aSMauro Carvalho Chehab 	else
142b285192aSMauro Carvalho Chehab 		mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
143b285192aSMauro Carvalho Chehab 	if (bc != 1)
144b285192aSMauro Carvalho Chehab 		pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
145b285192aSMauro Carvalho Chehab }
146b285192aSMauro Carvalho Chehab 
147b285192aSMauro Carvalho Chehab #ifdef TUNER_FLAG
148b285192aSMauro Carvalho Chehab int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
149b285192aSMauro Carvalho Chehab {
150b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
151b285192aSMauro Carvalho Chehab 		__func__, (unsigned int)norm, v4l2_norm_to_name(norm));
152b285192aSMauro Carvalho Chehab 
153b285192aSMauro Carvalho Chehab 	dev->tvnorm = norm;
154b285192aSMauro Carvalho Chehab 
155b285192aSMauro Carvalho Chehab 	/* Tell the internal A/V decoder */
156b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, core, s_std, norm);
157b285192aSMauro Carvalho Chehab 
158b285192aSMauro Carvalho Chehab 	return 0;
159b285192aSMauro Carvalho Chehab }
160b285192aSMauro Carvalho Chehab #endif
161b285192aSMauro Carvalho Chehab 
162b285192aSMauro Carvalho Chehab struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
163b285192aSMauro Carvalho Chehab 				       struct pci_dev *pci,
164b285192aSMauro Carvalho Chehab 				       struct video_device *template,
165b285192aSMauro Carvalho Chehab 				       char *type)
166b285192aSMauro Carvalho Chehab {
167b285192aSMauro Carvalho Chehab 	struct video_device *vfd;
168b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
169b285192aSMauro Carvalho Chehab 
170b285192aSMauro Carvalho Chehab 	vfd = video_device_alloc();
171b285192aSMauro Carvalho Chehab 	if (NULL == vfd)
172b285192aSMauro Carvalho Chehab 		return NULL;
173b285192aSMauro Carvalho Chehab 	*vfd = *template;
174b285192aSMauro Carvalho Chehab 	vfd->v4l2_dev = &dev->v4l2_dev;
175b285192aSMauro Carvalho Chehab 	vfd->release = video_device_release;
176b285192aSMauro Carvalho Chehab 	snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, type,
177b285192aSMauro Carvalho Chehab 		 cx25821_boards[dev->board].name);
178b285192aSMauro Carvalho Chehab 	video_set_drvdata(vfd, dev);
179b285192aSMauro Carvalho Chehab 	return vfd;
180b285192aSMauro Carvalho Chehab }
181b285192aSMauro Carvalho Chehab 
182b285192aSMauro Carvalho Chehab /*
183b285192aSMauro Carvalho Chehab static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
184b285192aSMauro Carvalho Chehab {
185b285192aSMauro Carvalho Chehab 	int i;
186b285192aSMauro Carvalho Chehab 
187b285192aSMauro Carvalho Chehab 	if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
188b285192aSMauro Carvalho Chehab 		return -EINVAL;
189b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX25821_CTLS; i++)
190b285192aSMauro Carvalho Chehab 		if (cx25821_ctls[i].v.id == qctrl->id)
191b285192aSMauro Carvalho Chehab 			break;
192b285192aSMauro Carvalho Chehab 	if (i == CX25821_CTLS) {
193b285192aSMauro Carvalho Chehab 		*qctrl = no_ctl;
194b285192aSMauro Carvalho Chehab 		return 0;
195b285192aSMauro Carvalho Chehab 	}
196b285192aSMauro Carvalho Chehab 	*qctrl = cx25821_ctls[i].v;
197b285192aSMauro Carvalho Chehab 	return 0;
198b285192aSMauro Carvalho Chehab }
199b285192aSMauro Carvalho Chehab */
200b285192aSMauro Carvalho Chehab 
201b285192aSMauro Carvalho Chehab /* resource management */
202b285192aSMauro Carvalho Chehab int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh,
203b285192aSMauro Carvalho Chehab 		    unsigned int bit)
204b285192aSMauro Carvalho Chehab {
205b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
206b285192aSMauro Carvalho Chehab 	if (fh->resources & bit)
207b285192aSMauro Carvalho Chehab 		/* have it already allocated */
208b285192aSMauro Carvalho Chehab 		return 1;
209b285192aSMauro Carvalho Chehab 
210b285192aSMauro Carvalho Chehab 	/* is it free? */
211b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
212b285192aSMauro Carvalho Chehab 	if (dev->channels[fh->channel_id].resources & bit) {
213b285192aSMauro Carvalho Chehab 		/* no, someone else uses it */
214b285192aSMauro Carvalho Chehab 		mutex_unlock(&dev->lock);
215b285192aSMauro Carvalho Chehab 		return 0;
216b285192aSMauro Carvalho Chehab 	}
217b285192aSMauro Carvalho Chehab 	/* it's free, grab it */
218b285192aSMauro Carvalho Chehab 	fh->resources |= bit;
219b285192aSMauro Carvalho Chehab 	dev->channels[fh->channel_id].resources |= bit;
220b285192aSMauro Carvalho Chehab 	dprintk(1, "res: get %d\n", bit);
221b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
222b285192aSMauro Carvalho Chehab 	return 1;
223b285192aSMauro Carvalho Chehab }
224b285192aSMauro Carvalho Chehab 
225b285192aSMauro Carvalho Chehab int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
226b285192aSMauro Carvalho Chehab {
227b285192aSMauro Carvalho Chehab 	return fh->resources & bit;
228b285192aSMauro Carvalho Chehab }
229b285192aSMauro Carvalho Chehab 
230b285192aSMauro Carvalho Chehab int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
231b285192aSMauro Carvalho Chehab {
232b285192aSMauro Carvalho Chehab 	return fh->dev->channels[fh->channel_id].resources & bit;
233b285192aSMauro Carvalho Chehab }
234b285192aSMauro Carvalho Chehab 
235b285192aSMauro Carvalho Chehab void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh,
236b285192aSMauro Carvalho Chehab 		      unsigned int bits)
237b285192aSMauro Carvalho Chehab {
238b285192aSMauro Carvalho Chehab 	BUG_ON((fh->resources & bits) != bits);
239b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
240b285192aSMauro Carvalho Chehab 
241b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
242b285192aSMauro Carvalho Chehab 	fh->resources &= ~bits;
243b285192aSMauro Carvalho Chehab 	dev->channels[fh->channel_id].resources &= ~bits;
244b285192aSMauro Carvalho Chehab 	dprintk(1, "res: put %d\n", bits);
245b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
246b285192aSMauro Carvalho Chehab }
247b285192aSMauro Carvalho Chehab 
248b285192aSMauro Carvalho Chehab int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
249b285192aSMauro Carvalho Chehab {
250b285192aSMauro Carvalho Chehab 	struct v4l2_routing route;
251b285192aSMauro Carvalho Chehab 	memset(&route, 0, sizeof(route));
252b285192aSMauro Carvalho Chehab 
253b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
254b285192aSMauro Carvalho Chehab 		__func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
255b285192aSMauro Carvalho Chehab 		INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
256b285192aSMauro Carvalho Chehab 	dev->input = input;
257b285192aSMauro Carvalho Chehab 
258b285192aSMauro Carvalho Chehab 	route.input = INPUT(input)->vmux;
259b285192aSMauro Carvalho Chehab 
260b285192aSMauro Carvalho Chehab 	/* Tell the internal A/V decoder */
261b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
262b285192aSMauro Carvalho Chehab 
263b285192aSMauro Carvalho Chehab 	return 0;
264b285192aSMauro Carvalho Chehab }
265b285192aSMauro Carvalho Chehab 
266b285192aSMauro Carvalho Chehab int cx25821_start_video_dma(struct cx25821_dev *dev,
267b285192aSMauro Carvalho Chehab 			    struct cx25821_dmaqueue *q,
268b285192aSMauro Carvalho Chehab 			    struct cx25821_buffer *buf,
269b285192aSMauro Carvalho Chehab 			    struct sram_channel *channel)
270b285192aSMauro Carvalho Chehab {
271b285192aSMauro Carvalho Chehab 	int tmp = 0;
272b285192aSMauro Carvalho Chehab 
273b285192aSMauro Carvalho Chehab 	/* setup fifo + format */
274b285192aSMauro Carvalho Chehab 	cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
275b285192aSMauro Carvalho Chehab 
276b285192aSMauro Carvalho Chehab 	/* reset counter */
277b285192aSMauro Carvalho Chehab 	cx_write(channel->gpcnt_ctl, 3);
278b285192aSMauro Carvalho Chehab 	q->count = 1;
279b285192aSMauro Carvalho Chehab 
280b285192aSMauro Carvalho Chehab 	/* enable irq */
281b285192aSMauro Carvalho Chehab 	cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
282b285192aSMauro Carvalho Chehab 	cx_set(channel->int_msk, 0x11);
283b285192aSMauro Carvalho Chehab 
284b285192aSMauro Carvalho Chehab 	/* start dma */
285b285192aSMauro Carvalho Chehab 	cx_write(channel->dma_ctl, 0x11);	/* FIFO and RISC enable */
286b285192aSMauro Carvalho Chehab 
287b285192aSMauro Carvalho Chehab 	/* make sure upstream setting if any is reversed */
288b285192aSMauro Carvalho Chehab 	tmp = cx_read(VID_CH_MODE_SEL);
289b285192aSMauro Carvalho Chehab 	cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
290b285192aSMauro Carvalho Chehab 
291b285192aSMauro Carvalho Chehab 	return 0;
292b285192aSMauro Carvalho Chehab }
293b285192aSMauro Carvalho Chehab 
294dafc456cSMauro Carvalho Chehab static int cx25821_restart_video_queue(struct cx25821_dev *dev,
295b285192aSMauro Carvalho Chehab 				       struct cx25821_dmaqueue *q,
296b285192aSMauro Carvalho Chehab 				       struct sram_channel *channel)
297b285192aSMauro Carvalho Chehab {
298b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf, *prev;
299b285192aSMauro Carvalho Chehab 	struct list_head *item;
300b285192aSMauro Carvalho Chehab 
301b285192aSMauro Carvalho Chehab 	if (!list_empty(&q->active)) {
302b285192aSMauro Carvalho Chehab 		buf = list_entry(q->active.next, struct cx25821_buffer,
303b285192aSMauro Carvalho Chehab 				vb.queue);
304b285192aSMauro Carvalho Chehab 
305b285192aSMauro Carvalho Chehab 		cx25821_start_video_dma(dev, q, buf, channel);
306b285192aSMauro Carvalho Chehab 
307b285192aSMauro Carvalho Chehab 		list_for_each(item, &q->active) {
308b285192aSMauro Carvalho Chehab 			buf = list_entry(item, struct cx25821_buffer, vb.queue);
309b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
310b285192aSMauro Carvalho Chehab 		}
311b285192aSMauro Carvalho Chehab 
312b285192aSMauro Carvalho Chehab 		mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
313b285192aSMauro Carvalho Chehab 		return 0;
314b285192aSMauro Carvalho Chehab 	}
315b285192aSMauro Carvalho Chehab 
316b285192aSMauro Carvalho Chehab 	prev = NULL;
317b285192aSMauro Carvalho Chehab 	for (;;) {
318b285192aSMauro Carvalho Chehab 		if (list_empty(&q->queued))
319b285192aSMauro Carvalho Chehab 			return 0;
320b285192aSMauro Carvalho Chehab 
321b285192aSMauro Carvalho Chehab 		buf = list_entry(q->queued.next, struct cx25821_buffer,
322b285192aSMauro Carvalho Chehab 				vb.queue);
323b285192aSMauro Carvalho Chehab 
324b285192aSMauro Carvalho Chehab 		if (NULL == prev) {
325b285192aSMauro Carvalho Chehab 			list_move_tail(&buf->vb.queue, &q->active);
326b285192aSMauro Carvalho Chehab 			cx25821_start_video_dma(dev, q, buf, channel);
327b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
328b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
329b285192aSMauro Carvalho Chehab 			mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
330b285192aSMauro Carvalho Chehab 		} else if (prev->vb.width == buf->vb.width &&
331b285192aSMauro Carvalho Chehab 			   prev->vb.height == buf->vb.height &&
332b285192aSMauro Carvalho Chehab 			   prev->fmt == buf->fmt) {
333b285192aSMauro Carvalho Chehab 			list_move_tail(&buf->vb.queue, &q->active);
334b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
335b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
336b285192aSMauro Carvalho Chehab 			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
337b285192aSMauro Carvalho Chehab 			prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
338b285192aSMauro Carvalho Chehab 		} else {
339b285192aSMauro Carvalho Chehab 			return 0;
340b285192aSMauro Carvalho Chehab 		}
341b285192aSMauro Carvalho Chehab 		prev = buf;
342b285192aSMauro Carvalho Chehab 	}
343b285192aSMauro Carvalho Chehab }
344b285192aSMauro Carvalho Chehab 
345dafc456cSMauro Carvalho Chehab static void cx25821_vid_timeout(unsigned long data)
346b285192aSMauro Carvalho Chehab {
347b285192aSMauro Carvalho Chehab 	struct cx25821_data *timeout_data = (struct cx25821_data *)data;
348b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = timeout_data->dev;
349b285192aSMauro Carvalho Chehab 	struct sram_channel *channel = timeout_data->channel;
350b285192aSMauro Carvalho Chehab 	struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq;
351b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf;
352b285192aSMauro Carvalho Chehab 	unsigned long flags;
353b285192aSMauro Carvalho Chehab 
354b285192aSMauro Carvalho Chehab 	/* cx25821_sram_channel_dump(dev, channel); */
355b285192aSMauro Carvalho Chehab 	cx_clear(channel->dma_ctl, 0x11);
356b285192aSMauro Carvalho Chehab 
357b285192aSMauro Carvalho Chehab 	spin_lock_irqsave(&dev->slock, flags);
358b285192aSMauro Carvalho Chehab 	while (!list_empty(&q->active)) {
359b285192aSMauro Carvalho Chehab 		buf = list_entry(q->active.next, struct cx25821_buffer,
360b285192aSMauro Carvalho Chehab 				vb.queue);
361b285192aSMauro Carvalho Chehab 		list_del(&buf->vb.queue);
362b285192aSMauro Carvalho Chehab 
363b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_ERROR;
364b285192aSMauro Carvalho Chehab 		wake_up(&buf->vb.done);
365b285192aSMauro Carvalho Chehab 	}
366b285192aSMauro Carvalho Chehab 
367b285192aSMauro Carvalho Chehab 	cx25821_restart_video_queue(dev, q, channel);
368b285192aSMauro Carvalho Chehab 	spin_unlock_irqrestore(&dev->slock, flags);
369b285192aSMauro Carvalho Chehab }
370b285192aSMauro Carvalho Chehab 
371b285192aSMauro Carvalho Chehab int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
372b285192aSMauro Carvalho Chehab {
373b285192aSMauro Carvalho Chehab 	u32 count = 0;
374b285192aSMauro Carvalho Chehab 	int handled = 0;
375b285192aSMauro Carvalho Chehab 	u32 mask;
376b285192aSMauro Carvalho Chehab 	struct sram_channel *channel = dev->channels[chan_num].sram_channels;
377b285192aSMauro Carvalho Chehab 
378b285192aSMauro Carvalho Chehab 	mask = cx_read(channel->int_msk);
379b285192aSMauro Carvalho Chehab 	if (0 == (status & mask))
380b285192aSMauro Carvalho Chehab 		return handled;
381b285192aSMauro Carvalho Chehab 
382b285192aSMauro Carvalho Chehab 	cx_write(channel->int_stat, status);
383b285192aSMauro Carvalho Chehab 
384b285192aSMauro Carvalho Chehab 	/* risc op code error */
385b285192aSMauro Carvalho Chehab 	if (status & (1 << 16)) {
386b285192aSMauro Carvalho Chehab 		pr_warn("%s, %s: video risc op code error\n",
387b285192aSMauro Carvalho Chehab 			dev->name, channel->name);
388b285192aSMauro Carvalho Chehab 		cx_clear(channel->dma_ctl, 0x11);
389b285192aSMauro Carvalho Chehab 		cx25821_sram_channel_dump(dev, channel);
390b285192aSMauro Carvalho Chehab 	}
391b285192aSMauro Carvalho Chehab 
392b285192aSMauro Carvalho Chehab 	/* risc1 y */
393b285192aSMauro Carvalho Chehab 	if (status & FLD_VID_DST_RISC1) {
394b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
395b285192aSMauro Carvalho Chehab 		count = cx_read(channel->gpcnt);
396b285192aSMauro Carvalho Chehab 		cx25821_video_wakeup(dev, &dev->channels[channel->i].vidq,
397b285192aSMauro Carvalho Chehab 				count);
398b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
399b285192aSMauro Carvalho Chehab 		handled++;
400b285192aSMauro Carvalho Chehab 	}
401b285192aSMauro Carvalho Chehab 
402b285192aSMauro Carvalho Chehab 	/* risc2 y */
403b285192aSMauro Carvalho Chehab 	if (status & 0x10) {
404b285192aSMauro Carvalho Chehab 		dprintk(2, "stopper video\n");
405b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
406b285192aSMauro Carvalho Chehab 		cx25821_restart_video_queue(dev,
407b285192aSMauro Carvalho Chehab 				&dev->channels[channel->i].vidq, channel);
408b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
409b285192aSMauro Carvalho Chehab 		handled++;
410b285192aSMauro Carvalho Chehab 	}
411b285192aSMauro Carvalho Chehab 	return handled;
412b285192aSMauro Carvalho Chehab }
413b285192aSMauro Carvalho Chehab 
414b285192aSMauro Carvalho Chehab void cx25821_videoioctl_unregister(struct cx25821_dev *dev)
415b285192aSMauro Carvalho Chehab {
416b285192aSMauro Carvalho Chehab 	if (dev->ioctl_dev) {
417b285192aSMauro Carvalho Chehab 		if (video_is_registered(dev->ioctl_dev))
418b285192aSMauro Carvalho Chehab 			video_unregister_device(dev->ioctl_dev);
419b285192aSMauro Carvalho Chehab 		else
420b285192aSMauro Carvalho Chehab 			video_device_release(dev->ioctl_dev);
421b285192aSMauro Carvalho Chehab 
422b285192aSMauro Carvalho Chehab 		dev->ioctl_dev = NULL;
423b285192aSMauro Carvalho Chehab 	}
424b285192aSMauro Carvalho Chehab }
425b285192aSMauro Carvalho Chehab 
426b285192aSMauro Carvalho Chehab void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
427b285192aSMauro Carvalho Chehab {
428b285192aSMauro Carvalho Chehab 	cx_clear(PCI_INT_MSK, 1);
429b285192aSMauro Carvalho Chehab 
430b285192aSMauro Carvalho Chehab 	if (dev->channels[chan_num].video_dev) {
431b285192aSMauro Carvalho Chehab 		if (video_is_registered(dev->channels[chan_num].video_dev))
432b285192aSMauro Carvalho Chehab 			video_unregister_device(
433b285192aSMauro Carvalho Chehab 					dev->channels[chan_num].video_dev);
434b285192aSMauro Carvalho Chehab 		else
435b285192aSMauro Carvalho Chehab 			video_device_release(
436b285192aSMauro Carvalho Chehab 					dev->channels[chan_num].video_dev);
437b285192aSMauro Carvalho Chehab 
438b285192aSMauro Carvalho Chehab 		dev->channels[chan_num].video_dev = NULL;
439b285192aSMauro Carvalho Chehab 
440b285192aSMauro Carvalho Chehab 		btcx_riscmem_free(dev->pci,
441b285192aSMauro Carvalho Chehab 				&dev->channels[chan_num].vidq.stopper);
442b285192aSMauro Carvalho Chehab 
443b285192aSMauro Carvalho Chehab 		pr_warn("device %d released!\n", chan_num);
444b285192aSMauro Carvalho Chehab 	}
445b285192aSMauro Carvalho Chehab 
446b285192aSMauro Carvalho Chehab }
447b285192aSMauro Carvalho Chehab 
448b285192aSMauro Carvalho Chehab int cx25821_video_register(struct cx25821_dev *dev)
449b285192aSMauro Carvalho Chehab {
450b285192aSMauro Carvalho Chehab 	int err;
451b285192aSMauro Carvalho Chehab 	int i;
452b285192aSMauro Carvalho Chehab 
453b285192aSMauro Carvalho Chehab 	struct video_device cx25821_video_device = {
454b285192aSMauro Carvalho Chehab 		.name = "cx25821-video",
455b285192aSMauro Carvalho Chehab 		.fops = &video_fops,
456b285192aSMauro Carvalho Chehab 		.minor = -1,
457b285192aSMauro Carvalho Chehab 		.ioctl_ops = &video_ioctl_ops,
458b285192aSMauro Carvalho Chehab 		.tvnorms = CX25821_NORMS,
459b285192aSMauro Carvalho Chehab 		.current_norm = V4L2_STD_NTSC_M,
460b285192aSMauro Carvalho Chehab 	};
461b285192aSMauro Carvalho Chehab 
462b285192aSMauro Carvalho Chehab 	spin_lock_init(&dev->slock);
463b285192aSMauro Carvalho Chehab 
464b285192aSMauro Carvalho Chehab 	for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; ++i) {
465b285192aSMauro Carvalho Chehab 		cx25821_init_controls(dev, i);
466b285192aSMauro Carvalho Chehab 
467b285192aSMauro Carvalho Chehab 		cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
468b285192aSMauro Carvalho Chehab 			dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
469b285192aSMauro Carvalho Chehab 
470b285192aSMauro Carvalho Chehab 		dev->channels[i].sram_channels = &cx25821_sram_channels[i];
471b285192aSMauro Carvalho Chehab 		dev->channels[i].video_dev = NULL;
472b285192aSMauro Carvalho Chehab 		dev->channels[i].resources = 0;
473b285192aSMauro Carvalho Chehab 
474b285192aSMauro Carvalho Chehab 		cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
475b285192aSMauro Carvalho Chehab 
476b285192aSMauro Carvalho Chehab 		INIT_LIST_HEAD(&dev->channels[i].vidq.active);
477b285192aSMauro Carvalho Chehab 		INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
478b285192aSMauro Carvalho Chehab 
479b285192aSMauro Carvalho Chehab 		dev->channels[i].timeout_data.dev = dev;
480b285192aSMauro Carvalho Chehab 		dev->channels[i].timeout_data.channel =
481b285192aSMauro Carvalho Chehab 			&cx25821_sram_channels[i];
482b285192aSMauro Carvalho Chehab 		dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
483b285192aSMauro Carvalho Chehab 		dev->channels[i].vidq.timeout.data =
484b285192aSMauro Carvalho Chehab 			(unsigned long)&dev->channels[i].timeout_data;
485b285192aSMauro Carvalho Chehab 		init_timer(&dev->channels[i].vidq.timeout);
486b285192aSMauro Carvalho Chehab 
487b285192aSMauro Carvalho Chehab 		/* register v4l devices */
488b285192aSMauro Carvalho Chehab 		dev->channels[i].video_dev = cx25821_vdev_init(dev, dev->pci,
489b285192aSMauro Carvalho Chehab 				&cx25821_video_device, "video");
490b285192aSMauro Carvalho Chehab 
491b285192aSMauro Carvalho Chehab 		err = video_register_device(dev->channels[i].video_dev,
492b285192aSMauro Carvalho Chehab 				VFL_TYPE_GRABBER, video_nr[dev->nr]);
493b285192aSMauro Carvalho Chehab 
494b285192aSMauro Carvalho Chehab 		if (err < 0)
495b285192aSMauro Carvalho Chehab 			goto fail_unreg;
496b285192aSMauro Carvalho Chehab 
497b285192aSMauro Carvalho Chehab 	}
498b285192aSMauro Carvalho Chehab 
499b285192aSMauro Carvalho Chehab 	/* set PCI interrupt */
500b285192aSMauro Carvalho Chehab 	cx_set(PCI_INT_MSK, 0xff);
501b285192aSMauro Carvalho Chehab 
502b285192aSMauro Carvalho Chehab 	/* initial device configuration */
503b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
504b285192aSMauro Carvalho Chehab #ifdef TUNER_FLAG
505b285192aSMauro Carvalho Chehab 	dev->tvnorm = cx25821_video_device.current_norm;
506b285192aSMauro Carvalho Chehab 	cx25821_set_tvnorm(dev, dev->tvnorm);
507b285192aSMauro Carvalho Chehab #endif
508b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
509b285192aSMauro Carvalho Chehab 
510b285192aSMauro Carvalho Chehab 	return 0;
511b285192aSMauro Carvalho Chehab 
512b285192aSMauro Carvalho Chehab fail_unreg:
513b285192aSMauro Carvalho Chehab 	cx25821_video_unregister(dev, i);
514b285192aSMauro Carvalho Chehab 	return err;
515b285192aSMauro Carvalho Chehab }
516b285192aSMauro Carvalho Chehab 
517b285192aSMauro Carvalho Chehab int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
518b285192aSMauro Carvalho Chehab 		 unsigned int *size)
519b285192aSMauro Carvalho Chehab {
520b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = q->priv_data;
521b285192aSMauro Carvalho Chehab 
522b285192aSMauro Carvalho Chehab 	*size = fh->fmt->depth * fh->width * fh->height >> 3;
523b285192aSMauro Carvalho Chehab 
524b285192aSMauro Carvalho Chehab 	if (0 == *count)
525b285192aSMauro Carvalho Chehab 		*count = 32;
526b285192aSMauro Carvalho Chehab 
527b285192aSMauro Carvalho Chehab 	if (*size * *count > vid_limit * 1024 * 1024)
528b285192aSMauro Carvalho Chehab 		*count = (vid_limit * 1024 * 1024) / *size;
529b285192aSMauro Carvalho Chehab 
530b285192aSMauro Carvalho Chehab 	return 0;
531b285192aSMauro Carvalho Chehab }
532b285192aSMauro Carvalho Chehab 
533b285192aSMauro Carvalho Chehab int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
534b285192aSMauro Carvalho Chehab 		   enum v4l2_field field)
535b285192aSMauro Carvalho Chehab {
536b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = q->priv_data;
537b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
538b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
539b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
540b285192aSMauro Carvalho Chehab 	int rc, init_buffer = 0;
541b285192aSMauro Carvalho Chehab 	u32 line0_offset;
542b285192aSMauro Carvalho Chehab 	struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
543b285192aSMauro Carvalho Chehab 	int bpl_local = LINE_SIZE_D1;
544b285192aSMauro Carvalho Chehab 	int channel_opened = fh->channel_id;
545b285192aSMauro Carvalho Chehab 
546b285192aSMauro Carvalho Chehab 	BUG_ON(NULL == fh->fmt);
547b285192aSMauro Carvalho Chehab 	if (fh->width < 48 || fh->width > 720 ||
548b285192aSMauro Carvalho Chehab 	    fh->height < 32 || fh->height > 576)
549b285192aSMauro Carvalho Chehab 		return -EINVAL;
550b285192aSMauro Carvalho Chehab 
551b285192aSMauro Carvalho Chehab 	buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
552b285192aSMauro Carvalho Chehab 
553b285192aSMauro Carvalho Chehab 	if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
554b285192aSMauro Carvalho Chehab 		return -EINVAL;
555b285192aSMauro Carvalho Chehab 
556b285192aSMauro Carvalho Chehab 	if (buf->fmt != fh->fmt ||
557b285192aSMauro Carvalho Chehab 	    buf->vb.width != fh->width ||
558b285192aSMauro Carvalho Chehab 	    buf->vb.height != fh->height || buf->vb.field != field) {
559b285192aSMauro Carvalho Chehab 		buf->fmt = fh->fmt;
560b285192aSMauro Carvalho Chehab 		buf->vb.width = fh->width;
561b285192aSMauro Carvalho Chehab 		buf->vb.height = fh->height;
562b285192aSMauro Carvalho Chehab 		buf->vb.field = field;
563b285192aSMauro Carvalho Chehab 		init_buffer = 1;
564b285192aSMauro Carvalho Chehab 	}
565b285192aSMauro Carvalho Chehab 
566b285192aSMauro Carvalho Chehab 	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
567b285192aSMauro Carvalho Chehab 		init_buffer = 1;
568b285192aSMauro Carvalho Chehab 		rc = videobuf_iolock(q, &buf->vb, NULL);
569b285192aSMauro Carvalho Chehab 		if (0 != rc) {
570b285192aSMauro Carvalho Chehab 			printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
571b285192aSMauro Carvalho Chehab 			goto fail;
572b285192aSMauro Carvalho Chehab 		}
573b285192aSMauro Carvalho Chehab 	}
574b285192aSMauro Carvalho Chehab 
575b285192aSMauro Carvalho Chehab 	dprintk(1, "init_buffer=%d\n", init_buffer);
576b285192aSMauro Carvalho Chehab 
577b285192aSMauro Carvalho Chehab 	if (init_buffer) {
578b285192aSMauro Carvalho Chehab 
579b285192aSMauro Carvalho Chehab 		channel_opened = dev->channel_opened;
580b285192aSMauro Carvalho Chehab 		if (channel_opened < 0 || channel_opened > 7)
581b285192aSMauro Carvalho Chehab 			channel_opened = 7;
582b285192aSMauro Carvalho Chehab 
583b285192aSMauro Carvalho Chehab 		if (dev->channels[channel_opened].pixel_formats ==
584b285192aSMauro Carvalho Chehab 				PIXEL_FRMT_411)
585b285192aSMauro Carvalho Chehab 			buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
586b285192aSMauro Carvalho Chehab 		else
587b285192aSMauro Carvalho Chehab 			buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
588b285192aSMauro Carvalho Chehab 
589b285192aSMauro Carvalho Chehab 		if (dev->channels[channel_opened].pixel_formats ==
590b285192aSMauro Carvalho Chehab 				PIXEL_FRMT_411) {
591b285192aSMauro Carvalho Chehab 			bpl_local = buf->bpl;
592b285192aSMauro Carvalho Chehab 		} else {
593b285192aSMauro Carvalho Chehab 			bpl_local = buf->bpl;   /* Default */
594b285192aSMauro Carvalho Chehab 
595b285192aSMauro Carvalho Chehab 			if (channel_opened >= 0 && channel_opened <= 7) {
596b285192aSMauro Carvalho Chehab 				if (dev->channels[channel_opened]
597b285192aSMauro Carvalho Chehab 						.use_cif_resolution) {
598b285192aSMauro Carvalho Chehab 					if (dev->tvnorm & V4L2_STD_PAL_BG ||
599b285192aSMauro Carvalho Chehab 					    dev->tvnorm & V4L2_STD_PAL_DK)
600b285192aSMauro Carvalho Chehab 						bpl_local = 352 << 1;
601b285192aSMauro Carvalho Chehab 					else
602b285192aSMauro Carvalho Chehab 						bpl_local = dev->channels[
603b285192aSMauro Carvalho Chehab 							channel_opened].
604b285192aSMauro Carvalho Chehab 							cif_width << 1;
605b285192aSMauro Carvalho Chehab 				}
606b285192aSMauro Carvalho Chehab 			}
607b285192aSMauro Carvalho Chehab 		}
608b285192aSMauro Carvalho Chehab 
609b285192aSMauro Carvalho Chehab 		switch (buf->vb.field) {
610b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_TOP:
611b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
612b285192aSMauro Carvalho Chehab 					    dma->sglist, 0, UNSET,
613b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height);
614b285192aSMauro Carvalho Chehab 			break;
615b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_BOTTOM:
616b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
617b285192aSMauro Carvalho Chehab 					    dma->sglist, UNSET, 0,
618b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height);
619b285192aSMauro Carvalho Chehab 			break;
620b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_INTERLACED:
621b285192aSMauro Carvalho Chehab 			/* All other formats are top field first */
622b285192aSMauro Carvalho Chehab 			line0_offset = 0;
623b285192aSMauro Carvalho Chehab 			dprintk(1, "top field first\n");
624b285192aSMauro Carvalho Chehab 
625b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
626b285192aSMauro Carvalho Chehab 					    dma->sglist, line0_offset,
627b285192aSMauro Carvalho Chehab 					    bpl_local, bpl_local, bpl_local,
628b285192aSMauro Carvalho Chehab 					    buf->vb.height >> 1);
629b285192aSMauro Carvalho Chehab 			break;
630b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_SEQ_TB:
631b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
632b285192aSMauro Carvalho Chehab 					    dma->sglist,
633b285192aSMauro Carvalho Chehab 					    0, buf->bpl * (buf->vb.height >> 1),
634b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height >> 1);
635b285192aSMauro Carvalho Chehab 			break;
636b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_SEQ_BT:
637b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
638b285192aSMauro Carvalho Chehab 					    dma->sglist,
639b285192aSMauro Carvalho Chehab 					    buf->bpl * (buf->vb.height >> 1), 0,
640b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height >> 1);
641b285192aSMauro Carvalho Chehab 			break;
642b285192aSMauro Carvalho Chehab 		default:
643b285192aSMauro Carvalho Chehab 			BUG();
644b285192aSMauro Carvalho Chehab 		}
645b285192aSMauro Carvalho Chehab 	}
646b285192aSMauro Carvalho Chehab 
647b285192aSMauro Carvalho Chehab 	dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
648b285192aSMauro Carvalho Chehab 		buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
649b285192aSMauro Carvalho Chehab 		fh->fmt->name, (unsigned long)buf->risc.dma);
650b285192aSMauro Carvalho Chehab 
651b285192aSMauro Carvalho Chehab 	buf->vb.state = VIDEOBUF_PREPARED;
652b285192aSMauro Carvalho Chehab 
653b285192aSMauro Carvalho Chehab 	return 0;
654b285192aSMauro Carvalho Chehab 
655b285192aSMauro Carvalho Chehab fail:
656b285192aSMauro Carvalho Chehab 	cx25821_free_buffer(q, buf);
657b285192aSMauro Carvalho Chehab 	return rc;
658b285192aSMauro Carvalho Chehab }
659b285192aSMauro Carvalho Chehab 
660b285192aSMauro Carvalho Chehab void cx25821_buffer_release(struct videobuf_queue *q,
661b285192aSMauro Carvalho Chehab 			    struct videobuf_buffer *vb)
662b285192aSMauro Carvalho Chehab {
663b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
664b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
665b285192aSMauro Carvalho Chehab 
666b285192aSMauro Carvalho Chehab 	cx25821_free_buffer(q, buf);
667b285192aSMauro Carvalho Chehab }
668b285192aSMauro Carvalho Chehab 
669b285192aSMauro Carvalho Chehab struct videobuf_queue *get_queue(struct cx25821_fh *fh)
670b285192aSMauro Carvalho Chehab {
671b285192aSMauro Carvalho Chehab 	switch (fh->type) {
672b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
673b285192aSMauro Carvalho Chehab 		return &fh->vidq;
674b285192aSMauro Carvalho Chehab 	default:
675b285192aSMauro Carvalho Chehab 		BUG();
676b285192aSMauro Carvalho Chehab 		return NULL;
677b285192aSMauro Carvalho Chehab 	}
678b285192aSMauro Carvalho Chehab }
679b285192aSMauro Carvalho Chehab 
680b285192aSMauro Carvalho Chehab int cx25821_get_resource(struct cx25821_fh *fh, int resource)
681b285192aSMauro Carvalho Chehab {
682b285192aSMauro Carvalho Chehab 	switch (fh->type) {
683b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
684b285192aSMauro Carvalho Chehab 		return resource;
685b285192aSMauro Carvalho Chehab 	default:
686b285192aSMauro Carvalho Chehab 		BUG();
687b285192aSMauro Carvalho Chehab 		return 0;
688b285192aSMauro Carvalho Chehab 	}
689b285192aSMauro Carvalho Chehab }
690b285192aSMauro Carvalho Chehab 
691b285192aSMauro Carvalho Chehab int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
692b285192aSMauro Carvalho Chehab {
693b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
694b285192aSMauro Carvalho Chehab 
695b285192aSMauro Carvalho Chehab 	return videobuf_mmap_mapper(get_queue(fh), vma);
696b285192aSMauro Carvalho Chehab }
697b285192aSMauro Carvalho Chehab 
698b285192aSMauro Carvalho Chehab 
699b285192aSMauro Carvalho Chehab static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
700b285192aSMauro Carvalho Chehab {
701b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
702b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
703b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *prev;
704b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = vq->priv_data;
705b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
706b285192aSMauro Carvalho Chehab 	struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq;
707b285192aSMauro Carvalho Chehab 
708b285192aSMauro Carvalho Chehab 	/* add jump to stopper */
709b285192aSMauro Carvalho Chehab 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
710b285192aSMauro Carvalho Chehab 	buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
711b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0);      /* bits 63-32 */
712b285192aSMauro Carvalho Chehab 
713b285192aSMauro Carvalho Chehab 	dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
714b285192aSMauro Carvalho Chehab 
715b285192aSMauro Carvalho Chehab 	if (!list_empty(&q->queued)) {
716b285192aSMauro Carvalho Chehab 		list_add_tail(&buf->vb.queue, &q->queued);
717b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_QUEUED;
718b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
719b285192aSMauro Carvalho Chehab 				buf->vb.i);
720b285192aSMauro Carvalho Chehab 
721b285192aSMauro Carvalho Chehab 	} else if (list_empty(&q->active)) {
722b285192aSMauro Carvalho Chehab 		list_add_tail(&buf->vb.queue, &q->active);
723b285192aSMauro Carvalho Chehab 		cx25821_start_video_dma(dev, q, buf,
724b285192aSMauro Carvalho Chehab 				dev->channels[fh->channel_id].sram_channels);
725b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_ACTIVE;
726b285192aSMauro Carvalho Chehab 		buf->count = q->count++;
727b285192aSMauro Carvalho Chehab 		mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
728b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
729b285192aSMauro Carvalho Chehab 				buf, buf->vb.i, buf->count, q->count);
730b285192aSMauro Carvalho Chehab 	} else {
731b285192aSMauro Carvalho Chehab 		prev = list_entry(q->active.prev, struct cx25821_buffer,
732b285192aSMauro Carvalho Chehab 				vb.queue);
733b285192aSMauro Carvalho Chehab 		if (prev->vb.width == buf->vb.width
734b285192aSMauro Carvalho Chehab 		   && prev->vb.height == buf->vb.height
735b285192aSMauro Carvalho Chehab 		   && prev->fmt == buf->fmt) {
736b285192aSMauro Carvalho Chehab 			list_add_tail(&buf->vb.queue, &q->active);
737b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
738b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
739b285192aSMauro Carvalho Chehab 			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
740b285192aSMauro Carvalho Chehab 
741b285192aSMauro Carvalho Chehab 			/* 64 bit bits 63-32 */
742b285192aSMauro Carvalho Chehab 			prev->risc.jmp[2] = cpu_to_le32(0);
743b285192aSMauro Carvalho Chehab 			dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
744b285192aSMauro Carvalho Chehab 					buf, buf->vb.i, buf->count);
745b285192aSMauro Carvalho Chehab 
746b285192aSMauro Carvalho Chehab 		} else {
747b285192aSMauro Carvalho Chehab 			list_add_tail(&buf->vb.queue, &q->queued);
748b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_QUEUED;
749b285192aSMauro Carvalho Chehab 			dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
750b285192aSMauro Carvalho Chehab 					buf->vb.i);
751b285192aSMauro Carvalho Chehab 		}
752b285192aSMauro Carvalho Chehab 	}
753b285192aSMauro Carvalho Chehab 
754b285192aSMauro Carvalho Chehab 	if (list_empty(&q->active))
755b285192aSMauro Carvalho Chehab 		dprintk(2, "active queue empty!\n");
756b285192aSMauro Carvalho Chehab }
757b285192aSMauro Carvalho Chehab 
758b285192aSMauro Carvalho Chehab static struct videobuf_queue_ops cx25821_video_qops = {
759b285192aSMauro Carvalho Chehab 	.buf_setup = cx25821_buffer_setup,
760b285192aSMauro Carvalho Chehab 	.buf_prepare = cx25821_buffer_prepare,
761b285192aSMauro Carvalho Chehab 	.buf_queue = buffer_queue,
762b285192aSMauro Carvalho Chehab 	.buf_release = cx25821_buffer_release,
763b285192aSMauro Carvalho Chehab };
764b285192aSMauro Carvalho Chehab 
765b285192aSMauro Carvalho Chehab static int video_open(struct file *file)
766b285192aSMauro Carvalho Chehab {
767b285192aSMauro Carvalho Chehab 	struct video_device *vdev = video_devdata(file);
768b285192aSMauro Carvalho Chehab 	struct cx25821_dev *h, *dev = video_drvdata(file);
769b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh;
770b285192aSMauro Carvalho Chehab 	struct list_head *list;
771b285192aSMauro Carvalho Chehab 	int minor = video_devdata(file)->minor;
772b285192aSMauro Carvalho Chehab 	enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
773b285192aSMauro Carvalho Chehab 	u32 pix_format;
774b285192aSMauro Carvalho Chehab 	int ch_id = 0;
775b285192aSMauro Carvalho Chehab 	int i;
776b285192aSMauro Carvalho Chehab 
777b285192aSMauro Carvalho Chehab 	dprintk(1, "open dev=%s type=%s\n", video_device_node_name(vdev),
778b285192aSMauro Carvalho Chehab 			v4l2_type_names[type]);
779b285192aSMauro Carvalho Chehab 
780b285192aSMauro Carvalho Chehab 	/* allocate + initialize per filehandle data */
781b285192aSMauro Carvalho Chehab 	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
782b285192aSMauro Carvalho Chehab 	if (NULL == fh)
783b285192aSMauro Carvalho Chehab 		return -ENOMEM;
784b285192aSMauro Carvalho Chehab 
785b285192aSMauro Carvalho Chehab 	mutex_lock(&cx25821_devlist_mutex);
786b285192aSMauro Carvalho Chehab 
787b285192aSMauro Carvalho Chehab 	list_for_each(list, &cx25821_devlist)
788b285192aSMauro Carvalho Chehab 	{
789b285192aSMauro Carvalho Chehab 		h = list_entry(list, struct cx25821_dev, devlist);
790b285192aSMauro Carvalho Chehab 
791b285192aSMauro Carvalho Chehab 		for (i = 0; i < MAX_VID_CHANNEL_NUM; i++) {
792b285192aSMauro Carvalho Chehab 			if (h->channels[i].video_dev &&
793b285192aSMauro Carvalho Chehab 			    h->channels[i].video_dev->minor == minor) {
794b285192aSMauro Carvalho Chehab 				dev = h;
795b285192aSMauro Carvalho Chehab 				ch_id = i;
796b285192aSMauro Carvalho Chehab 				type  = V4L2_BUF_TYPE_VIDEO_CAPTURE;
797b285192aSMauro Carvalho Chehab 			}
798b285192aSMauro Carvalho Chehab 		}
799b285192aSMauro Carvalho Chehab 	}
800b285192aSMauro Carvalho Chehab 
801b285192aSMauro Carvalho Chehab 	if (NULL == dev) {
802b285192aSMauro Carvalho Chehab 		mutex_unlock(&cx25821_devlist_mutex);
803b285192aSMauro Carvalho Chehab 		kfree(fh);
804b285192aSMauro Carvalho Chehab 		return -ENODEV;
805b285192aSMauro Carvalho Chehab 	}
806b285192aSMauro Carvalho Chehab 
807b285192aSMauro Carvalho Chehab 	file->private_data = fh;
808b285192aSMauro Carvalho Chehab 	fh->dev = dev;
809b285192aSMauro Carvalho Chehab 	fh->type = type;
810b285192aSMauro Carvalho Chehab 	fh->width = 720;
811b285192aSMauro Carvalho Chehab 	fh->channel_id = ch_id;
812b285192aSMauro Carvalho Chehab 
813b285192aSMauro Carvalho Chehab 	if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
814b285192aSMauro Carvalho Chehab 		fh->height = 576;
815b285192aSMauro Carvalho Chehab 	else
816b285192aSMauro Carvalho Chehab 		fh->height = 480;
817b285192aSMauro Carvalho Chehab 
818b285192aSMauro Carvalho Chehab 	dev->channel_opened = fh->channel_id;
819b285192aSMauro Carvalho Chehab 	if (dev->channels[ch_id].pixel_formats == PIXEL_FRMT_411)
820b285192aSMauro Carvalho Chehab 		pix_format = V4L2_PIX_FMT_Y41P;
821b285192aSMauro Carvalho Chehab 	else
822b285192aSMauro Carvalho Chehab 		pix_format = V4L2_PIX_FMT_YUYV;
823b285192aSMauro Carvalho Chehab 	fh->fmt = cx25821_format_by_fourcc(pix_format);
824b285192aSMauro Carvalho Chehab 
825b285192aSMauro Carvalho Chehab 	v4l2_prio_open(&dev->channels[ch_id].prio, &fh->prio);
826b285192aSMauro Carvalho Chehab 
827b285192aSMauro Carvalho Chehab 	videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops, &dev->pci->dev,
828b285192aSMauro Carvalho Chehab 			&dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
829b285192aSMauro Carvalho Chehab 			V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
830b285192aSMauro Carvalho Chehab 			fh, NULL);
831b285192aSMauro Carvalho Chehab 
832b285192aSMauro Carvalho Chehab 	dprintk(1, "post videobuf_queue_init()\n");
833b285192aSMauro Carvalho Chehab 	mutex_unlock(&cx25821_devlist_mutex);
834b285192aSMauro Carvalho Chehab 
835b285192aSMauro Carvalho Chehab 	return 0;
836b285192aSMauro Carvalho Chehab }
837b285192aSMauro Carvalho Chehab 
838b285192aSMauro Carvalho Chehab static ssize_t video_read(struct file *file, char __user * data, size_t count,
839b285192aSMauro Carvalho Chehab 			 loff_t *ppos)
840b285192aSMauro Carvalho Chehab {
841b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
842b285192aSMauro Carvalho Chehab 
843b285192aSMauro Carvalho Chehab 	switch (fh->type) {
844b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
845b285192aSMauro Carvalho Chehab 		if (cx25821_res_locked(fh, RESOURCE_VIDEO0))
846b285192aSMauro Carvalho Chehab 			return -EBUSY;
847b285192aSMauro Carvalho Chehab 
848b285192aSMauro Carvalho Chehab 		return videobuf_read_one(&fh->vidq, data, count, ppos,
849b285192aSMauro Carvalho Chehab 					file->f_flags & O_NONBLOCK);
850b285192aSMauro Carvalho Chehab 
851b285192aSMauro Carvalho Chehab 	default:
852b285192aSMauro Carvalho Chehab 		BUG();
853b285192aSMauro Carvalho Chehab 		return 0;
854b285192aSMauro Carvalho Chehab 	}
855b285192aSMauro Carvalho Chehab }
856b285192aSMauro Carvalho Chehab 
857b285192aSMauro Carvalho Chehab static unsigned int video_poll(struct file *file,
858b285192aSMauro Carvalho Chehab 			      struct poll_table_struct *wait)
859b285192aSMauro Carvalho Chehab {
860b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
861b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf;
862b285192aSMauro Carvalho Chehab 
863b285192aSMauro Carvalho Chehab 	if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
864b285192aSMauro Carvalho Chehab 		/* streaming capture */
865b285192aSMauro Carvalho Chehab 		if (list_empty(&fh->vidq.stream))
866b285192aSMauro Carvalho Chehab 			return POLLERR;
867b285192aSMauro Carvalho Chehab 		buf = list_entry(fh->vidq.stream.next,
868b285192aSMauro Carvalho Chehab 				struct cx25821_buffer, vb.stream);
869b285192aSMauro Carvalho Chehab 	} else {
870b285192aSMauro Carvalho Chehab 		/* read() capture */
871b285192aSMauro Carvalho Chehab 		buf = (struct cx25821_buffer *)fh->vidq.read_buf;
872b285192aSMauro Carvalho Chehab 		if (NULL == buf)
873b285192aSMauro Carvalho Chehab 			return POLLERR;
874b285192aSMauro Carvalho Chehab 	}
875b285192aSMauro Carvalho Chehab 
876b285192aSMauro Carvalho Chehab 	poll_wait(file, &buf->vb.done, wait);
877b285192aSMauro Carvalho Chehab 	if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
878b285192aSMauro Carvalho Chehab 		if (buf->vb.state == VIDEOBUF_DONE) {
879b285192aSMauro Carvalho Chehab 			struct cx25821_dev *dev = fh->dev;
880b285192aSMauro Carvalho Chehab 
881b285192aSMauro Carvalho Chehab 			if (dev && dev->channels[fh->channel_id]
882b285192aSMauro Carvalho Chehab 					.use_cif_resolution) {
883b285192aSMauro Carvalho Chehab 				u8 cam_id = *((char *)buf->vb.baddr + 3);
884b285192aSMauro Carvalho Chehab 				memcpy((char *)buf->vb.baddr,
885b285192aSMauro Carvalho Chehab 				      (char *)buf->vb.baddr + (fh->width * 2),
886b285192aSMauro Carvalho Chehab 				      (fh->width * 2));
887b285192aSMauro Carvalho Chehab 				*((char *)buf->vb.baddr + 3) = cam_id;
888b285192aSMauro Carvalho Chehab 			}
889b285192aSMauro Carvalho Chehab 		}
890b285192aSMauro Carvalho Chehab 
891b285192aSMauro Carvalho Chehab 		return POLLIN | POLLRDNORM;
892b285192aSMauro Carvalho Chehab 	}
893b285192aSMauro Carvalho Chehab 
894b285192aSMauro Carvalho Chehab 	return 0;
895b285192aSMauro Carvalho Chehab }
896b285192aSMauro Carvalho Chehab 
897b285192aSMauro Carvalho Chehab static int video_release(struct file *file)
898b285192aSMauro Carvalho Chehab {
899b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
900b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
901b285192aSMauro Carvalho Chehab 
902b285192aSMauro Carvalho Chehab 	/* stop the risc engine and fifo */
903b285192aSMauro Carvalho Chehab 	cx_write(channel0->dma_ctl, 0); /* FIFO and RISC disable */
904b285192aSMauro Carvalho Chehab 
905b285192aSMauro Carvalho Chehab 	/* stop video capture */
906b285192aSMauro Carvalho Chehab 	if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
907b285192aSMauro Carvalho Chehab 		videobuf_queue_cancel(&fh->vidq);
908b285192aSMauro Carvalho Chehab 		cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
909b285192aSMauro Carvalho Chehab 	}
910b285192aSMauro Carvalho Chehab 
911b285192aSMauro Carvalho Chehab 	if (fh->vidq.read_buf) {
912b285192aSMauro Carvalho Chehab 		cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
913b285192aSMauro Carvalho Chehab 		kfree(fh->vidq.read_buf);
914b285192aSMauro Carvalho Chehab 	}
915b285192aSMauro Carvalho Chehab 
916b285192aSMauro Carvalho Chehab 	videobuf_mmap_free(&fh->vidq);
917b285192aSMauro Carvalho Chehab 
918b285192aSMauro Carvalho Chehab 	v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
919b285192aSMauro Carvalho Chehab 	file->private_data = NULL;
920b285192aSMauro Carvalho Chehab 	kfree(fh);
921b285192aSMauro Carvalho Chehab 
922b285192aSMauro Carvalho Chehab 	return 0;
923b285192aSMauro Carvalho Chehab }
924b285192aSMauro Carvalho Chehab 
925b285192aSMauro Carvalho Chehab static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
926b285192aSMauro Carvalho Chehab {
927b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
928b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
929b285192aSMauro Carvalho Chehab 
930b285192aSMauro Carvalho Chehab 	if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
931b285192aSMauro Carvalho Chehab 		return -EINVAL;
932b285192aSMauro Carvalho Chehab 
933b285192aSMauro Carvalho Chehab 	if (unlikely(i != fh->type))
934b285192aSMauro Carvalho Chehab 		return -EINVAL;
935b285192aSMauro Carvalho Chehab 
936b285192aSMauro Carvalho Chehab 	if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh,
937b285192aSMauro Carvalho Chehab 						RESOURCE_VIDEO0))))
938b285192aSMauro Carvalho Chehab 		return -EBUSY;
939b285192aSMauro Carvalho Chehab 
940b285192aSMauro Carvalho Chehab 	return videobuf_streamon(get_queue(fh));
941b285192aSMauro Carvalho Chehab }
942b285192aSMauro Carvalho Chehab 
943b285192aSMauro Carvalho Chehab static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
944b285192aSMauro Carvalho Chehab {
945b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
946b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
947b285192aSMauro Carvalho Chehab 	int err, res;
948b285192aSMauro Carvalho Chehab 
949b285192aSMauro Carvalho Chehab 	if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
950b285192aSMauro Carvalho Chehab 		return -EINVAL;
951b285192aSMauro Carvalho Chehab 	if (i != fh->type)
952b285192aSMauro Carvalho Chehab 		return -EINVAL;
953b285192aSMauro Carvalho Chehab 
954b285192aSMauro Carvalho Chehab 	res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
955b285192aSMauro Carvalho Chehab 	err = videobuf_streamoff(get_queue(fh));
956b285192aSMauro Carvalho Chehab 	if (err < 0)
957b285192aSMauro Carvalho Chehab 		return err;
958b285192aSMauro Carvalho Chehab 	cx25821_res_free(dev, fh, res);
959b285192aSMauro Carvalho Chehab 	return 0;
960b285192aSMauro Carvalho Chehab }
961b285192aSMauro Carvalho Chehab 
962b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
963b285192aSMauro Carvalho Chehab 				struct v4l2_format *f)
964b285192aSMauro Carvalho Chehab {
965b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
966b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
967b285192aSMauro Carvalho Chehab 	struct v4l2_mbus_framefmt mbus_fmt;
968b285192aSMauro Carvalho Chehab 	int err;
969b285192aSMauro Carvalho Chehab 	int pix_format = PIXEL_FRMT_422;
970b285192aSMauro Carvalho Chehab 
971b285192aSMauro Carvalho Chehab 	if (fh) {
972b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
973b285192aSMauro Carvalho Chehab 				      fh->prio);
974b285192aSMauro Carvalho Chehab 		if (0 != err)
975b285192aSMauro Carvalho Chehab 			return err;
976b285192aSMauro Carvalho Chehab 	}
977b285192aSMauro Carvalho Chehab 
978b285192aSMauro Carvalho Chehab 	dprintk(2, "%s()\n", __func__);
979b285192aSMauro Carvalho Chehab 	err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
980b285192aSMauro Carvalho Chehab 
981b285192aSMauro Carvalho Chehab 	if (0 != err)
982b285192aSMauro Carvalho Chehab 		return err;
983b285192aSMauro Carvalho Chehab 
984b285192aSMauro Carvalho Chehab 	fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
985b285192aSMauro Carvalho Chehab 	fh->vidq.field = f->fmt.pix.field;
986b285192aSMauro Carvalho Chehab 
987b285192aSMauro Carvalho Chehab 	/* check if width and height is valid based on set standard */
988b285192aSMauro Carvalho Chehab 	if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
989b285192aSMauro Carvalho Chehab 		fh->width = f->fmt.pix.width;
990b285192aSMauro Carvalho Chehab 
991b285192aSMauro Carvalho Chehab 	if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
992b285192aSMauro Carvalho Chehab 		fh->height = f->fmt.pix.height;
993b285192aSMauro Carvalho Chehab 
994b285192aSMauro Carvalho Chehab 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
995b285192aSMauro Carvalho Chehab 		pix_format = PIXEL_FRMT_411;
996b285192aSMauro Carvalho Chehab 	else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
997b285192aSMauro Carvalho Chehab 		pix_format = PIXEL_FRMT_422;
998b285192aSMauro Carvalho Chehab 	else
999b285192aSMauro Carvalho Chehab 		return -EINVAL;
1000b285192aSMauro Carvalho Chehab 
1001b285192aSMauro Carvalho Chehab 	cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
1002b285192aSMauro Carvalho Chehab 
1003b285192aSMauro Carvalho Chehab 	/* check if cif resolution */
1004b285192aSMauro Carvalho Chehab 	if (fh->width == 320 || fh->width == 352)
1005b285192aSMauro Carvalho Chehab 		dev->channels[fh->channel_id].use_cif_resolution = 1;
1006b285192aSMauro Carvalho Chehab 	else
1007b285192aSMauro Carvalho Chehab 		dev->channels[fh->channel_id].use_cif_resolution = 0;
1008b285192aSMauro Carvalho Chehab 
1009b285192aSMauro Carvalho Chehab 	dev->channels[fh->channel_id].cif_width = fh->width;
1010b285192aSMauro Carvalho Chehab 	medusa_set_resolution(dev, fh->width, SRAM_CH00);
1011b285192aSMauro Carvalho Chehab 
1012b285192aSMauro Carvalho Chehab 	dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__, fh->width,
1013b285192aSMauro Carvalho Chehab 		fh->height, fh->vidq.field);
1014b285192aSMauro Carvalho Chehab 	v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1015b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1016b285192aSMauro Carvalho Chehab 
1017b285192aSMauro Carvalho Chehab 	return 0;
1018b285192aSMauro Carvalho Chehab }
1019b285192aSMauro Carvalho Chehab 
1020b285192aSMauro Carvalho Chehab static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1021b285192aSMauro Carvalho Chehab {
1022b285192aSMauro Carvalho Chehab 	int ret_val = 0;
1023b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1024b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1025b285192aSMauro Carvalho Chehab 
1026b285192aSMauro Carvalho Chehab 	ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
1027b285192aSMauro Carvalho Chehab 
1028b285192aSMauro Carvalho Chehab 	p->sequence = dev->channels[fh->channel_id].vidq.count;
1029b285192aSMauro Carvalho Chehab 
1030b285192aSMauro Carvalho Chehab 	return ret_val;
1031b285192aSMauro Carvalho Chehab }
1032b285192aSMauro Carvalho Chehab 
1033b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
1034b285192aSMauro Carvalho Chehab {
1035b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1036b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1037b285192aSMauro Carvalho Chehab 	char name[32 + 2];
1038b285192aSMauro Carvalho Chehab 
1039b285192aSMauro Carvalho Chehab 	struct sram_channel *sram_ch = dev->channels[fh->channel_id]
1040b285192aSMauro Carvalho Chehab 								.sram_channels;
1041b285192aSMauro Carvalho Chehab 	u32 tmp = 0;
1042b285192aSMauro Carvalho Chehab 
1043b285192aSMauro Carvalho Chehab 	snprintf(name, sizeof(name), "%s/2", dev->name);
1044b285192aSMauro Carvalho Chehab 	pr_info("%s/2: ============  START LOG STATUS  ============\n",
1045b285192aSMauro Carvalho Chehab 		dev->name);
1046b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, core, log_status);
1047b285192aSMauro Carvalho Chehab 	tmp = cx_read(sram_ch->dma_ctl);
1048b285192aSMauro Carvalho Chehab 	pr_info("Video input 0 is %s\n",
1049b285192aSMauro Carvalho Chehab 		(tmp & 0x11) ? "streaming" : "stopped");
1050b285192aSMauro Carvalho Chehab 	pr_info("%s/2: =============  END LOG STATUS  =============\n",
1051b285192aSMauro Carvalho Chehab 		dev->name);
1052b285192aSMauro Carvalho Chehab 	return 0;
1053b285192aSMauro Carvalho Chehab }
1054b285192aSMauro Carvalho Chehab 
1055b285192aSMauro Carvalho Chehab static int vidioc_s_ctrl(struct file *file, void *priv,
1056b285192aSMauro Carvalho Chehab 			struct v4l2_control *ctl)
1057b285192aSMauro Carvalho Chehab {
1058b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1059b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1060b285192aSMauro Carvalho Chehab 	int err;
1061b285192aSMauro Carvalho Chehab 
1062b285192aSMauro Carvalho Chehab 	if (fh) {
1063b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1064b285192aSMauro Carvalho Chehab 				      fh->prio);
1065b285192aSMauro Carvalho Chehab 		if (0 != err)
1066b285192aSMauro Carvalho Chehab 			return err;
1067b285192aSMauro Carvalho Chehab 	}
1068b285192aSMauro Carvalho Chehab 
1069b285192aSMauro Carvalho Chehab 	return cx25821_set_control(dev, ctl, fh->channel_id);
1070b285192aSMauro Carvalho Chehab }
1071b285192aSMauro Carvalho Chehab 
1072b285192aSMauro Carvalho Chehab /* VIDEO IOCTLS */
1073b285192aSMauro Carvalho Chehab int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1074b285192aSMauro Carvalho Chehab 				 struct v4l2_format *f)
1075b285192aSMauro Carvalho Chehab {
1076b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1077b285192aSMauro Carvalho Chehab 
1078b285192aSMauro Carvalho Chehab 	f->fmt.pix.width = fh->width;
1079b285192aSMauro Carvalho Chehab 	f->fmt.pix.height = fh->height;
1080b285192aSMauro Carvalho Chehab 	f->fmt.pix.field = fh->vidq.field;
1081b285192aSMauro Carvalho Chehab 	f->fmt.pix.pixelformat = fh->fmt->fourcc;
1082b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
1083b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
1084b285192aSMauro Carvalho Chehab 
1085b285192aSMauro Carvalho Chehab 	return 0;
1086b285192aSMauro Carvalho Chehab }
1087b285192aSMauro Carvalho Chehab 
1088b285192aSMauro Carvalho Chehab int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1089b285192aSMauro Carvalho Chehab 				   struct v4l2_format *f)
1090b285192aSMauro Carvalho Chehab {
1091b285192aSMauro Carvalho Chehab 	struct cx25821_fmt *fmt;
1092b285192aSMauro Carvalho Chehab 	enum v4l2_field field;
1093b285192aSMauro Carvalho Chehab 	unsigned int maxw, maxh;
1094b285192aSMauro Carvalho Chehab 
1095b285192aSMauro Carvalho Chehab 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
1096b285192aSMauro Carvalho Chehab 	if (NULL == fmt)
1097b285192aSMauro Carvalho Chehab 		return -EINVAL;
1098b285192aSMauro Carvalho Chehab 
1099b285192aSMauro Carvalho Chehab 	field = f->fmt.pix.field;
1100b285192aSMauro Carvalho Chehab 	maxw = 720;
1101b285192aSMauro Carvalho Chehab 	maxh = 576;
1102b285192aSMauro Carvalho Chehab 
1103b285192aSMauro Carvalho Chehab 	if (V4L2_FIELD_ANY == field) {
1104b285192aSMauro Carvalho Chehab 		if (f->fmt.pix.height > maxh / 2)
1105b285192aSMauro Carvalho Chehab 			field = V4L2_FIELD_INTERLACED;
1106b285192aSMauro Carvalho Chehab 		else
1107b285192aSMauro Carvalho Chehab 			field = V4L2_FIELD_TOP;
1108b285192aSMauro Carvalho Chehab 	}
1109b285192aSMauro Carvalho Chehab 
1110b285192aSMauro Carvalho Chehab 	switch (field) {
1111b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_TOP:
1112b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_BOTTOM:
1113b285192aSMauro Carvalho Chehab 		maxh = maxh / 2;
1114b285192aSMauro Carvalho Chehab 		break;
1115b285192aSMauro Carvalho Chehab 	case V4L2_FIELD_INTERLACED:
1116b285192aSMauro Carvalho Chehab 		break;
1117b285192aSMauro Carvalho Chehab 	default:
1118b285192aSMauro Carvalho Chehab 		return -EINVAL;
1119b285192aSMauro Carvalho Chehab 	}
1120b285192aSMauro Carvalho Chehab 
1121b285192aSMauro Carvalho Chehab 	f->fmt.pix.field = field;
1122b285192aSMauro Carvalho Chehab 	if (f->fmt.pix.height < 32)
1123b285192aSMauro Carvalho Chehab 		f->fmt.pix.height = 32;
1124b285192aSMauro Carvalho Chehab 	if (f->fmt.pix.height > maxh)
1125b285192aSMauro Carvalho Chehab 		f->fmt.pix.height = maxh;
1126b285192aSMauro Carvalho Chehab 	if (f->fmt.pix.width < 48)
1127b285192aSMauro Carvalho Chehab 		f->fmt.pix.width = 48;
1128b285192aSMauro Carvalho Chehab 	if (f->fmt.pix.width > maxw)
1129b285192aSMauro Carvalho Chehab 		f->fmt.pix.width = maxw;
1130b285192aSMauro Carvalho Chehab 	f->fmt.pix.width &= ~0x03;
1131b285192aSMauro Carvalho Chehab 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
1132b285192aSMauro Carvalho Chehab 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
1133b285192aSMauro Carvalho Chehab 
1134b285192aSMauro Carvalho Chehab 	return 0;
1135b285192aSMauro Carvalho Chehab }
1136b285192aSMauro Carvalho Chehab 
1137b285192aSMauro Carvalho Chehab int cx25821_vidioc_querycap(struct file *file, void *priv,
1138b285192aSMauro Carvalho Chehab 			    struct v4l2_capability *cap)
1139b285192aSMauro Carvalho Chehab {
1140b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1141b285192aSMauro Carvalho Chehab 
1142b285192aSMauro Carvalho Chehab 	strcpy(cap->driver, "cx25821");
1143b285192aSMauro Carvalho Chehab 	strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
1144b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1145b285192aSMauro Carvalho Chehab 	cap->version = CX25821_VERSION_CODE;
1146b285192aSMauro Carvalho Chehab 	cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1147b285192aSMauro Carvalho Chehab 		V4L2_CAP_STREAMING;
1148b285192aSMauro Carvalho Chehab 	if (UNSET != dev->tuner_type)
1149b285192aSMauro Carvalho Chehab 		cap->capabilities |= V4L2_CAP_TUNER;
1150b285192aSMauro Carvalho Chehab 	return 0;
1151b285192aSMauro Carvalho Chehab }
1152b285192aSMauro Carvalho Chehab 
1153b285192aSMauro Carvalho Chehab int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1154b285192aSMauro Carvalho Chehab 			    struct v4l2_fmtdesc *f)
1155b285192aSMauro Carvalho Chehab {
1156b285192aSMauro Carvalho Chehab 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
1157b285192aSMauro Carvalho Chehab 		return -EINVAL;
1158b285192aSMauro Carvalho Chehab 
1159b285192aSMauro Carvalho Chehab 	strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1160b285192aSMauro Carvalho Chehab 	f->pixelformat = formats[f->index].fourcc;
1161b285192aSMauro Carvalho Chehab 
1162b285192aSMauro Carvalho Chehab 	return 0;
1163b285192aSMauro Carvalho Chehab }
1164b285192aSMauro Carvalho Chehab 
1165b285192aSMauro Carvalho Chehab int cx25821_vidioc_reqbufs(struct file *file, void *priv,
1166b285192aSMauro Carvalho Chehab 			   struct v4l2_requestbuffers *p)
1167b285192aSMauro Carvalho Chehab {
1168b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1169b285192aSMauro Carvalho Chehab 	return videobuf_reqbufs(get_queue(fh), p);
1170b285192aSMauro Carvalho Chehab }
1171b285192aSMauro Carvalho Chehab 
1172b285192aSMauro Carvalho Chehab int cx25821_vidioc_querybuf(struct file *file, void *priv,
1173b285192aSMauro Carvalho Chehab 			    struct v4l2_buffer *p)
1174b285192aSMauro Carvalho Chehab {
1175b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1176b285192aSMauro Carvalho Chehab 	return videobuf_querybuf(get_queue(fh), p);
1177b285192aSMauro Carvalho Chehab }
1178b285192aSMauro Carvalho Chehab 
1179b285192aSMauro Carvalho Chehab int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1180b285192aSMauro Carvalho Chehab {
1181b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1182b285192aSMauro Carvalho Chehab 	return videobuf_qbuf(get_queue(fh), p);
1183b285192aSMauro Carvalho Chehab }
1184b285192aSMauro Carvalho Chehab 
1185b285192aSMauro Carvalho Chehab int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
1186b285192aSMauro Carvalho Chehab {
1187b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1188b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = f;
1189b285192aSMauro Carvalho Chehab 
1190b285192aSMauro Carvalho Chehab 	*p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
1191b285192aSMauro Carvalho Chehab 
1192b285192aSMauro Carvalho Chehab 	return 0;
1193b285192aSMauro Carvalho Chehab }
1194b285192aSMauro Carvalho Chehab 
1195b285192aSMauro Carvalho Chehab int cx25821_vidioc_s_priority(struct file *file, void *f,
1196b285192aSMauro Carvalho Chehab 			      enum v4l2_priority prio)
1197b285192aSMauro Carvalho Chehab {
1198b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = f;
1199b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1200b285192aSMauro Carvalho Chehab 
1201b285192aSMauro Carvalho Chehab 	return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
1202b285192aSMauro Carvalho Chehab 			prio);
1203b285192aSMauro Carvalho Chehab }
1204b285192aSMauro Carvalho Chehab 
1205b285192aSMauro Carvalho Chehab #ifdef TUNER_FLAG
1206b285192aSMauro Carvalho Chehab int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id * tvnorms)
1207b285192aSMauro Carvalho Chehab {
1208b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1209b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1210b285192aSMauro Carvalho Chehab 	int err;
1211b285192aSMauro Carvalho Chehab 
1212b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1213b285192aSMauro Carvalho Chehab 
1214b285192aSMauro Carvalho Chehab 	if (fh) {
1215b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1216b285192aSMauro Carvalho Chehab 				      fh->prio);
1217b285192aSMauro Carvalho Chehab 		if (0 != err)
1218b285192aSMauro Carvalho Chehab 			return err;
1219b285192aSMauro Carvalho Chehab 	}
1220b285192aSMauro Carvalho Chehab 
1221b285192aSMauro Carvalho Chehab 	if (dev->tvnorm == *tvnorms)
1222b285192aSMauro Carvalho Chehab 		return 0;
1223b285192aSMauro Carvalho Chehab 
1224b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
1225b285192aSMauro Carvalho Chehab 	cx25821_set_tvnorm(dev, *tvnorms);
1226b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
1227b285192aSMauro Carvalho Chehab 
1228b285192aSMauro Carvalho Chehab 	medusa_set_videostandard(dev);
1229b285192aSMauro Carvalho Chehab 
1230b285192aSMauro Carvalho Chehab 	return 0;
1231b285192aSMauro Carvalho Chehab }
1232b285192aSMauro Carvalho Chehab #endif
1233b285192aSMauro Carvalho Chehab 
1234b285192aSMauro Carvalho Chehab int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
1235b285192aSMauro Carvalho Chehab {
1236b285192aSMauro Carvalho Chehab 	static const char * const iname[] = {
1237b285192aSMauro Carvalho Chehab 		[CX25821_VMUX_COMPOSITE] = "Composite",
1238b285192aSMauro Carvalho Chehab 		[CX25821_VMUX_SVIDEO] = "S-Video",
1239b285192aSMauro Carvalho Chehab 		[CX25821_VMUX_DEBUG] = "for debug only",
1240b285192aSMauro Carvalho Chehab 	};
1241b285192aSMauro Carvalho Chehab 	unsigned int n;
1242b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1243b285192aSMauro Carvalho Chehab 
1244b285192aSMauro Carvalho Chehab 	n = i->index;
1245b285192aSMauro Carvalho Chehab 	if (n >= 2)
1246b285192aSMauro Carvalho Chehab 		return -EINVAL;
1247b285192aSMauro Carvalho Chehab 
1248b285192aSMauro Carvalho Chehab 	if (0 == INPUT(n)->type)
1249b285192aSMauro Carvalho Chehab 		return -EINVAL;
1250b285192aSMauro Carvalho Chehab 
1251b285192aSMauro Carvalho Chehab 	i->type = V4L2_INPUT_TYPE_CAMERA;
1252b285192aSMauro Carvalho Chehab 	strcpy(i->name, iname[INPUT(n)->type]);
1253b285192aSMauro Carvalho Chehab 
1254b285192aSMauro Carvalho Chehab 	i->std = CX25821_NORMS;
1255b285192aSMauro Carvalho Chehab 	return 0;
1256b285192aSMauro Carvalho Chehab }
1257b285192aSMauro Carvalho Chehab 
1258b285192aSMauro Carvalho Chehab int cx25821_vidioc_enum_input(struct file *file, void *priv,
1259b285192aSMauro Carvalho Chehab 			      struct v4l2_input *i)
1260b285192aSMauro Carvalho Chehab {
1261b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1262b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1263b285192aSMauro Carvalho Chehab 	return cx25821_enum_input(dev, i);
1264b285192aSMauro Carvalho Chehab }
1265b285192aSMauro Carvalho Chehab 
1266b285192aSMauro Carvalho Chehab int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1267b285192aSMauro Carvalho Chehab {
1268b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1269b285192aSMauro Carvalho Chehab 
1270b285192aSMauro Carvalho Chehab 	*i = dev->input;
1271b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(): returns %d\n", __func__, *i);
1272b285192aSMauro Carvalho Chehab 	return 0;
1273b285192aSMauro Carvalho Chehab }
1274b285192aSMauro Carvalho Chehab 
1275b285192aSMauro Carvalho Chehab int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1276b285192aSMauro Carvalho Chehab {
1277b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1278b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1279b285192aSMauro Carvalho Chehab 	int err;
1280b285192aSMauro Carvalho Chehab 
1281b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(%d)\n", __func__, i);
1282b285192aSMauro Carvalho Chehab 
1283b285192aSMauro Carvalho Chehab 	if (fh) {
1284b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1285b285192aSMauro Carvalho Chehab 				      fh->prio);
1286b285192aSMauro Carvalho Chehab 		if (0 != err)
1287b285192aSMauro Carvalho Chehab 			return err;
1288b285192aSMauro Carvalho Chehab 	}
1289b285192aSMauro Carvalho Chehab 
1290b285192aSMauro Carvalho Chehab 	if (i >= CX25821_NR_INPUT) {
1291b285192aSMauro Carvalho Chehab 		dprintk(1, "%s(): -EINVAL\n", __func__);
1292b285192aSMauro Carvalho Chehab 		return -EINVAL;
1293b285192aSMauro Carvalho Chehab 	}
1294b285192aSMauro Carvalho Chehab 
1295b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
1296b285192aSMauro Carvalho Chehab 	cx25821_video_mux(dev, i);
1297b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
1298b285192aSMauro Carvalho Chehab 	return 0;
1299b285192aSMauro Carvalho Chehab }
1300b285192aSMauro Carvalho Chehab 
1301b285192aSMauro Carvalho Chehab #ifdef TUNER_FLAG
1302b285192aSMauro Carvalho Chehab int cx25821_vidioc_g_frequency(struct file *file, void *priv,
1303b285192aSMauro Carvalho Chehab 			       struct v4l2_frequency *f)
1304b285192aSMauro Carvalho Chehab {
1305b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1306b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1307b285192aSMauro Carvalho Chehab 
1308b285192aSMauro Carvalho Chehab 	f->frequency = dev->freq;
1309b285192aSMauro Carvalho Chehab 
1310b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, tuner, g_frequency, f);
1311b285192aSMauro Carvalho Chehab 
1312b285192aSMauro Carvalho Chehab 	return 0;
1313b285192aSMauro Carvalho Chehab }
1314b285192aSMauro Carvalho Chehab 
1315*b530a447SHans Verkuil int cx25821_set_freq(struct cx25821_dev *dev, const struct v4l2_frequency *f)
1316b285192aSMauro Carvalho Chehab {
1317b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
1318b285192aSMauro Carvalho Chehab 	dev->freq = f->frequency;
1319b285192aSMauro Carvalho Chehab 
1320b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, tuner, s_frequency, f);
1321b285192aSMauro Carvalho Chehab 
1322b285192aSMauro Carvalho Chehab 	/* When changing channels it is required to reset TVAUDIO */
1323b285192aSMauro Carvalho Chehab 	msleep(10);
1324b285192aSMauro Carvalho Chehab 
1325b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
1326b285192aSMauro Carvalho Chehab 
1327b285192aSMauro Carvalho Chehab 	return 0;
1328b285192aSMauro Carvalho Chehab }
1329b285192aSMauro Carvalho Chehab 
1330b285192aSMauro Carvalho Chehab int cx25821_vidioc_s_frequency(struct file *file, void *priv,
1331*b530a447SHans Verkuil 			       const struct v4l2_frequency *f)
1332b285192aSMauro Carvalho Chehab {
1333b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1334b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev;
1335b285192aSMauro Carvalho Chehab 	int err;
1336b285192aSMauro Carvalho Chehab 
1337b285192aSMauro Carvalho Chehab 	if (fh) {
1338b285192aSMauro Carvalho Chehab 		dev = fh->dev;
1339b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1340b285192aSMauro Carvalho Chehab 				      fh->prio);
1341b285192aSMauro Carvalho Chehab 		if (0 != err)
1342b285192aSMauro Carvalho Chehab 			return err;
1343b285192aSMauro Carvalho Chehab 	} else {
1344b285192aSMauro Carvalho Chehab 		pr_err("Invalid fh pointer!\n");
1345b285192aSMauro Carvalho Chehab 		return -EINVAL;
1346b285192aSMauro Carvalho Chehab 	}
1347b285192aSMauro Carvalho Chehab 
1348b285192aSMauro Carvalho Chehab 	return cx25821_set_freq(dev, f);
1349b285192aSMauro Carvalho Chehab }
1350b285192aSMauro Carvalho Chehab #endif
1351b285192aSMauro Carvalho Chehab 
1352b285192aSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
1353b285192aSMauro Carvalho Chehab int cx25821_vidioc_g_register(struct file *file, void *fh,
1354b285192aSMauro Carvalho Chehab 		      struct v4l2_dbg_register *reg)
1355b285192aSMauro Carvalho Chehab {
1356b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1357b285192aSMauro Carvalho Chehab 
1358b285192aSMauro Carvalho Chehab 	if (!v4l2_chip_match_host(&reg->match))
1359b285192aSMauro Carvalho Chehab 		return -EINVAL;
1360b285192aSMauro Carvalho Chehab 
1361b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, core, g_register, reg);
1362b285192aSMauro Carvalho Chehab 
1363b285192aSMauro Carvalho Chehab 	return 0;
1364b285192aSMauro Carvalho Chehab }
1365b285192aSMauro Carvalho Chehab 
1366b285192aSMauro Carvalho Chehab int cx25821_vidioc_s_register(struct file *file, void *fh,
1367b285192aSMauro Carvalho Chehab 		      struct v4l2_dbg_register *reg)
1368b285192aSMauro Carvalho Chehab {
1369b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1370b285192aSMauro Carvalho Chehab 
1371b285192aSMauro Carvalho Chehab 	if (!v4l2_chip_match_host(&reg->match))
1372b285192aSMauro Carvalho Chehab 		return -EINVAL;
1373b285192aSMauro Carvalho Chehab 
1374b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, core, s_register, reg);
1375b285192aSMauro Carvalho Chehab 
1376b285192aSMauro Carvalho Chehab 	return 0;
1377b285192aSMauro Carvalho Chehab }
1378b285192aSMauro Carvalho Chehab 
1379b285192aSMauro Carvalho Chehab #endif
1380b285192aSMauro Carvalho Chehab 
1381b285192aSMauro Carvalho Chehab #ifdef TUNER_FLAG
1382b285192aSMauro Carvalho Chehab int cx25821_vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1383b285192aSMauro Carvalho Chehab {
1384b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1385b285192aSMauro Carvalho Chehab 
1386b285192aSMauro Carvalho Chehab 	if (unlikely(UNSET == dev->tuner_type))
1387b285192aSMauro Carvalho Chehab 		return -EINVAL;
1388b285192aSMauro Carvalho Chehab 	if (0 != t->index)
1389b285192aSMauro Carvalho Chehab 		return -EINVAL;
1390b285192aSMauro Carvalho Chehab 
1391b285192aSMauro Carvalho Chehab 	strcpy(t->name, "Television");
1392b285192aSMauro Carvalho Chehab 	t->type = V4L2_TUNER_ANALOG_TV;
1393b285192aSMauro Carvalho Chehab 	t->capability = V4L2_TUNER_CAP_NORM;
1394b285192aSMauro Carvalho Chehab 	t->rangehigh = 0xffffffffUL;
1395b285192aSMauro Carvalho Chehab 
1396b285192aSMauro Carvalho Chehab 	t->signal = 0xffff;	/* LOCKED */
1397b285192aSMauro Carvalho Chehab 	return 0;
1398b285192aSMauro Carvalho Chehab }
1399b285192aSMauro Carvalho Chehab 
1400b285192aSMauro Carvalho Chehab int cx25821_vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1401b285192aSMauro Carvalho Chehab {
1402b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1403b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1404b285192aSMauro Carvalho Chehab 	int err;
1405b285192aSMauro Carvalho Chehab 
1406b285192aSMauro Carvalho Chehab 	if (fh) {
1407b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1408b285192aSMauro Carvalho Chehab 				      fh->prio);
1409b285192aSMauro Carvalho Chehab 		if (0 != err)
1410b285192aSMauro Carvalho Chehab 			return err;
1411b285192aSMauro Carvalho Chehab 	}
1412b285192aSMauro Carvalho Chehab 
1413b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1414b285192aSMauro Carvalho Chehab 	if (UNSET == dev->tuner_type)
1415b285192aSMauro Carvalho Chehab 		return -EINVAL;
1416b285192aSMauro Carvalho Chehab 	if (0 != t->index)
1417b285192aSMauro Carvalho Chehab 		return -EINVAL;
1418b285192aSMauro Carvalho Chehab 
1419b285192aSMauro Carvalho Chehab 	return 0;
1420b285192aSMauro Carvalho Chehab }
1421b285192aSMauro Carvalho Chehab 
1422b285192aSMauro Carvalho Chehab #endif
1423b285192aSMauro Carvalho Chehab /*****************************************************************************/
1424b285192aSMauro Carvalho Chehab static const struct v4l2_queryctrl no_ctl = {
1425b285192aSMauro Carvalho Chehab 	.name = "42",
1426b285192aSMauro Carvalho Chehab 	.flags = V4L2_CTRL_FLAG_DISABLED,
1427b285192aSMauro Carvalho Chehab };
1428b285192aSMauro Carvalho Chehab 
1429b285192aSMauro Carvalho Chehab static struct v4l2_queryctrl cx25821_ctls[] = {
1430b285192aSMauro Carvalho Chehab 	/* --- video --- */
1431b285192aSMauro Carvalho Chehab 	{
1432b285192aSMauro Carvalho Chehab 		.id = V4L2_CID_BRIGHTNESS,
1433b285192aSMauro Carvalho Chehab 		.name = "Brightness",
1434b285192aSMauro Carvalho Chehab 		.minimum = 0,
1435b285192aSMauro Carvalho Chehab 		.maximum = 10000,
1436b285192aSMauro Carvalho Chehab 		.step = 1,
1437b285192aSMauro Carvalho Chehab 		.default_value = 6200,
1438b285192aSMauro Carvalho Chehab 		.type = V4L2_CTRL_TYPE_INTEGER,
1439b285192aSMauro Carvalho Chehab 	}, {
1440b285192aSMauro Carvalho Chehab 		.id = V4L2_CID_CONTRAST,
1441b285192aSMauro Carvalho Chehab 		.name = "Contrast",
1442b285192aSMauro Carvalho Chehab 		.minimum = 0,
1443b285192aSMauro Carvalho Chehab 		.maximum = 10000,
1444b285192aSMauro Carvalho Chehab 		.step = 1,
1445b285192aSMauro Carvalho Chehab 		.default_value = 5000,
1446b285192aSMauro Carvalho Chehab 		.type = V4L2_CTRL_TYPE_INTEGER,
1447b285192aSMauro Carvalho Chehab 	}, {
1448b285192aSMauro Carvalho Chehab 		.id = V4L2_CID_SATURATION,
1449b285192aSMauro Carvalho Chehab 		.name = "Saturation",
1450b285192aSMauro Carvalho Chehab 		.minimum = 0,
1451b285192aSMauro Carvalho Chehab 		.maximum = 10000,
1452b285192aSMauro Carvalho Chehab 		.step = 1,
1453b285192aSMauro Carvalho Chehab 		.default_value = 5000,
1454b285192aSMauro Carvalho Chehab 		.type = V4L2_CTRL_TYPE_INTEGER,
1455b285192aSMauro Carvalho Chehab 	}, {
1456b285192aSMauro Carvalho Chehab 		.id = V4L2_CID_HUE,
1457b285192aSMauro Carvalho Chehab 		.name = "Hue",
1458b285192aSMauro Carvalho Chehab 		.minimum = 0,
1459b285192aSMauro Carvalho Chehab 		.maximum = 10000,
1460b285192aSMauro Carvalho Chehab 		.step = 1,
1461b285192aSMauro Carvalho Chehab 		.default_value = 5000,
1462b285192aSMauro Carvalho Chehab 		.type = V4L2_CTRL_TYPE_INTEGER,
1463b285192aSMauro Carvalho Chehab 	}
1464b285192aSMauro Carvalho Chehab };
1465b285192aSMauro Carvalho Chehab static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1466b285192aSMauro Carvalho Chehab 
1467b285192aSMauro Carvalho Chehab static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1468b285192aSMauro Carvalho Chehab {
1469b285192aSMauro Carvalho Chehab 	int i;
1470b285192aSMauro Carvalho Chehab 
1471b285192aSMauro Carvalho Chehab 	if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
1472b285192aSMauro Carvalho Chehab 		return -EINVAL;
1473b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX25821_CTLS; i++)
1474b285192aSMauro Carvalho Chehab 		if (cx25821_ctls[i].id == qctrl->id)
1475b285192aSMauro Carvalho Chehab 			break;
1476b285192aSMauro Carvalho Chehab 	if (i == CX25821_CTLS) {
1477b285192aSMauro Carvalho Chehab 		*qctrl = no_ctl;
1478b285192aSMauro Carvalho Chehab 		return 0;
1479b285192aSMauro Carvalho Chehab 	}
1480b285192aSMauro Carvalho Chehab 	*qctrl = cx25821_ctls[i];
1481b285192aSMauro Carvalho Chehab 	return 0;
1482b285192aSMauro Carvalho Chehab }
1483b285192aSMauro Carvalho Chehab 
1484b285192aSMauro Carvalho Chehab int cx25821_vidioc_queryctrl(struct file *file, void *priv,
1485b285192aSMauro Carvalho Chehab 		     struct v4l2_queryctrl *qctrl)
1486b285192aSMauro Carvalho Chehab {
1487b285192aSMauro Carvalho Chehab 	return cx25821_ctrl_query(qctrl);
1488b285192aSMauro Carvalho Chehab }
1489b285192aSMauro Carvalho Chehab 
1490b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
1491b285192aSMauro Carvalho Chehab /* VIDEO CTRL IOCTLS                                                  */
1492b285192aSMauro Carvalho Chehab 
1493b285192aSMauro Carvalho Chehab static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
1494b285192aSMauro Carvalho Chehab {
1495b285192aSMauro Carvalho Chehab 	unsigned int i;
1496b285192aSMauro Carvalho Chehab 
1497b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX25821_CTLS; i++)
1498b285192aSMauro Carvalho Chehab 		if (cx25821_ctls[i].id == id)
1499b285192aSMauro Carvalho Chehab 			return cx25821_ctls + i;
1500b285192aSMauro Carvalho Chehab 	return NULL;
1501b285192aSMauro Carvalho Chehab }
1502b285192aSMauro Carvalho Chehab 
1503b285192aSMauro Carvalho Chehab int cx25821_vidioc_g_ctrl(struct file *file, void *priv,
1504b285192aSMauro Carvalho Chehab 			  struct v4l2_control *ctl)
1505b285192aSMauro Carvalho Chehab {
1506b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1507b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1508b285192aSMauro Carvalho Chehab 
1509b285192aSMauro Carvalho Chehab 	const struct v4l2_queryctrl *ctrl;
1510b285192aSMauro Carvalho Chehab 
1511b285192aSMauro Carvalho Chehab 	ctrl = ctrl_by_id(ctl->id);
1512b285192aSMauro Carvalho Chehab 
1513b285192aSMauro Carvalho Chehab 	if (NULL == ctrl)
1514b285192aSMauro Carvalho Chehab 		return -EINVAL;
1515b285192aSMauro Carvalho Chehab 	switch (ctl->id) {
1516b285192aSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
1517b285192aSMauro Carvalho Chehab 		ctl->value = dev->channels[fh->channel_id].ctl_bright;
1518b285192aSMauro Carvalho Chehab 		break;
1519b285192aSMauro Carvalho Chehab 	case V4L2_CID_HUE:
1520b285192aSMauro Carvalho Chehab 		ctl->value = dev->channels[fh->channel_id].ctl_hue;
1521b285192aSMauro Carvalho Chehab 		break;
1522b285192aSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
1523b285192aSMauro Carvalho Chehab 		ctl->value = dev->channels[fh->channel_id].ctl_contrast;
1524b285192aSMauro Carvalho Chehab 		break;
1525b285192aSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
1526b285192aSMauro Carvalho Chehab 		ctl->value = dev->channels[fh->channel_id].ctl_saturation;
1527b285192aSMauro Carvalho Chehab 		break;
1528b285192aSMauro Carvalho Chehab 	}
1529b285192aSMauro Carvalho Chehab 	return 0;
1530b285192aSMauro Carvalho Chehab }
1531b285192aSMauro Carvalho Chehab 
1532b285192aSMauro Carvalho Chehab int cx25821_set_control(struct cx25821_dev *dev,
1533b285192aSMauro Carvalho Chehab 			struct v4l2_control *ctl, int chan_num)
1534b285192aSMauro Carvalho Chehab {
1535b285192aSMauro Carvalho Chehab 	int err;
1536b285192aSMauro Carvalho Chehab 	const struct v4l2_queryctrl *ctrl;
1537b285192aSMauro Carvalho Chehab 
1538b285192aSMauro Carvalho Chehab 	err = -EINVAL;
1539b285192aSMauro Carvalho Chehab 
1540b285192aSMauro Carvalho Chehab 	ctrl = ctrl_by_id(ctl->id);
1541b285192aSMauro Carvalho Chehab 
1542b285192aSMauro Carvalho Chehab 	if (NULL == ctrl)
1543b285192aSMauro Carvalho Chehab 		return err;
1544b285192aSMauro Carvalho Chehab 
1545b285192aSMauro Carvalho Chehab 	switch (ctrl->type) {
1546b285192aSMauro Carvalho Chehab 	case V4L2_CTRL_TYPE_BOOLEAN:
1547b285192aSMauro Carvalho Chehab 	case V4L2_CTRL_TYPE_MENU:
1548b285192aSMauro Carvalho Chehab 	case V4L2_CTRL_TYPE_INTEGER:
1549b285192aSMauro Carvalho Chehab 		if (ctl->value < ctrl->minimum)
1550b285192aSMauro Carvalho Chehab 			ctl->value = ctrl->minimum;
1551b285192aSMauro Carvalho Chehab 		if (ctl->value > ctrl->maximum)
1552b285192aSMauro Carvalho Chehab 			ctl->value = ctrl->maximum;
1553b285192aSMauro Carvalho Chehab 		break;
1554b285192aSMauro Carvalho Chehab 	default:
1555b285192aSMauro Carvalho Chehab 		/* nothing */ ;
1556b285192aSMauro Carvalho Chehab 	}
1557b285192aSMauro Carvalho Chehab 
1558b285192aSMauro Carvalho Chehab 	switch (ctl->id) {
1559b285192aSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
1560b285192aSMauro Carvalho Chehab 		dev->channels[chan_num].ctl_bright = ctl->value;
1561b285192aSMauro Carvalho Chehab 		medusa_set_brightness(dev, ctl->value, chan_num);
1562b285192aSMauro Carvalho Chehab 		break;
1563b285192aSMauro Carvalho Chehab 	case V4L2_CID_HUE:
1564b285192aSMauro Carvalho Chehab 		dev->channels[chan_num].ctl_hue = ctl->value;
1565b285192aSMauro Carvalho Chehab 		medusa_set_hue(dev, ctl->value, chan_num);
1566b285192aSMauro Carvalho Chehab 		break;
1567b285192aSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
1568b285192aSMauro Carvalho Chehab 		dev->channels[chan_num].ctl_contrast = ctl->value;
1569b285192aSMauro Carvalho Chehab 		medusa_set_contrast(dev, ctl->value, chan_num);
1570b285192aSMauro Carvalho Chehab 		break;
1571b285192aSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
1572b285192aSMauro Carvalho Chehab 		dev->channels[chan_num].ctl_saturation = ctl->value;
1573b285192aSMauro Carvalho Chehab 		medusa_set_saturation(dev, ctl->value, chan_num);
1574b285192aSMauro Carvalho Chehab 		break;
1575b285192aSMauro Carvalho Chehab 	}
1576b285192aSMauro Carvalho Chehab 
1577b285192aSMauro Carvalho Chehab 	err = 0;
1578b285192aSMauro Carvalho Chehab 
1579b285192aSMauro Carvalho Chehab 	return err;
1580b285192aSMauro Carvalho Chehab }
1581b285192aSMauro Carvalho Chehab 
1582b285192aSMauro Carvalho Chehab static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num)
1583b285192aSMauro Carvalho Chehab {
1584b285192aSMauro Carvalho Chehab 	struct v4l2_control ctrl;
1585b285192aSMauro Carvalho Chehab 	int i;
1586b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX25821_CTLS; i++) {
1587b285192aSMauro Carvalho Chehab 		ctrl.id = cx25821_ctls[i].id;
1588b285192aSMauro Carvalho Chehab 		ctrl.value = cx25821_ctls[i].default_value;
1589b285192aSMauro Carvalho Chehab 
1590b285192aSMauro Carvalho Chehab 		cx25821_set_control(dev, &ctrl, chan_num);
1591b285192aSMauro Carvalho Chehab 	}
1592b285192aSMauro Carvalho Chehab }
1593b285192aSMauro Carvalho Chehab 
1594b285192aSMauro Carvalho Chehab int cx25821_vidioc_cropcap(struct file *file, void *priv,
1595b285192aSMauro Carvalho Chehab 			   struct v4l2_cropcap *cropcap)
1596b285192aSMauro Carvalho Chehab {
1597b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1598b285192aSMauro Carvalho Chehab 
1599b285192aSMauro Carvalho Chehab 	if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1600b285192aSMauro Carvalho Chehab 		return -EINVAL;
1601b285192aSMauro Carvalho Chehab 	cropcap->bounds.top = 0;
1602b285192aSMauro Carvalho Chehab 	cropcap->bounds.left = 0;
1603b285192aSMauro Carvalho Chehab 	cropcap->bounds.width = 720;
1604b285192aSMauro Carvalho Chehab 	cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1605b285192aSMauro Carvalho Chehab 	cropcap->pixelaspect.numerator =
1606b285192aSMauro Carvalho Chehab 		dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1607b285192aSMauro Carvalho Chehab 	cropcap->pixelaspect.denominator =
1608b285192aSMauro Carvalho Chehab 		dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1609b285192aSMauro Carvalho Chehab 	cropcap->defrect = cropcap->bounds;
1610b285192aSMauro Carvalho Chehab 	return 0;
1611b285192aSMauro Carvalho Chehab }
1612b285192aSMauro Carvalho Chehab 
16134f996594SHans Verkuil int cx25821_vidioc_s_crop(struct file *file, void *priv, const struct v4l2_crop *crop)
1614b285192aSMauro Carvalho Chehab {
1615b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1616b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1617b285192aSMauro Carvalho Chehab 	int err;
1618b285192aSMauro Carvalho Chehab 
1619b285192aSMauro Carvalho Chehab 	if (fh) {
1620b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1621b285192aSMauro Carvalho Chehab 				      fh->prio);
1622b285192aSMauro Carvalho Chehab 		if (0 != err)
1623b285192aSMauro Carvalho Chehab 			return err;
1624b285192aSMauro Carvalho Chehab 	}
1625b285192aSMauro Carvalho Chehab 	/* cx25821_vidioc_s_crop not supported */
1626b285192aSMauro Carvalho Chehab 	return -EINVAL;
1627b285192aSMauro Carvalho Chehab }
1628b285192aSMauro Carvalho Chehab 
1629b285192aSMauro Carvalho Chehab int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1630b285192aSMauro Carvalho Chehab {
1631b285192aSMauro Carvalho Chehab 	/* cx25821_vidioc_g_crop not supported */
1632b285192aSMauro Carvalho Chehab 	return -EINVAL;
1633b285192aSMauro Carvalho Chehab }
1634b285192aSMauro Carvalho Chehab 
1635b285192aSMauro Carvalho Chehab int cx25821_vidioc_querystd(struct file *file, void *priv, v4l2_std_id * norm)
1636b285192aSMauro Carvalho Chehab {
1637b285192aSMauro Carvalho Chehab 	/* medusa does not support video standard sensing of current input */
1638b285192aSMauro Carvalho Chehab 	*norm = CX25821_NORMS;
1639b285192aSMauro Carvalho Chehab 
1640b285192aSMauro Carvalho Chehab 	return 0;
1641b285192aSMauro Carvalho Chehab }
1642b285192aSMauro Carvalho Chehab 
1643b285192aSMauro Carvalho Chehab int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
1644b285192aSMauro Carvalho Chehab {
1645b285192aSMauro Carvalho Chehab 	if (tvnorm == V4L2_STD_PAL_BG) {
1646b285192aSMauro Carvalho Chehab 		if (width == 352 || width == 720)
1647b285192aSMauro Carvalho Chehab 			return 1;
1648b285192aSMauro Carvalho Chehab 		else
1649b285192aSMauro Carvalho Chehab 			return 0;
1650b285192aSMauro Carvalho Chehab 	}
1651b285192aSMauro Carvalho Chehab 
1652b285192aSMauro Carvalho Chehab 	if (tvnorm == V4L2_STD_NTSC_M) {
1653b285192aSMauro Carvalho Chehab 		if (width == 320 || width == 352 || width == 720)
1654b285192aSMauro Carvalho Chehab 			return 1;
1655b285192aSMauro Carvalho Chehab 		else
1656b285192aSMauro Carvalho Chehab 			return 0;
1657b285192aSMauro Carvalho Chehab 	}
1658b285192aSMauro Carvalho Chehab 	return 0;
1659b285192aSMauro Carvalho Chehab }
1660b285192aSMauro Carvalho Chehab 
1661b285192aSMauro Carvalho Chehab int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
1662b285192aSMauro Carvalho Chehab {
1663b285192aSMauro Carvalho Chehab 	if (tvnorm == V4L2_STD_PAL_BG) {
1664b285192aSMauro Carvalho Chehab 		if (height == 576 || height == 288)
1665b285192aSMauro Carvalho Chehab 			return 1;
1666b285192aSMauro Carvalho Chehab 		else
1667b285192aSMauro Carvalho Chehab 			return 0;
1668b285192aSMauro Carvalho Chehab 	}
1669b285192aSMauro Carvalho Chehab 
1670b285192aSMauro Carvalho Chehab 	if (tvnorm == V4L2_STD_NTSC_M) {
1671b285192aSMauro Carvalho Chehab 		if (height == 480 || height == 240)
1672b285192aSMauro Carvalho Chehab 			return 1;
1673b285192aSMauro Carvalho Chehab 		else
1674b285192aSMauro Carvalho Chehab 			return 0;
1675b285192aSMauro Carvalho Chehab 	}
1676b285192aSMauro Carvalho Chehab 
1677b285192aSMauro Carvalho Chehab 	return 0;
1678b285192aSMauro Carvalho Chehab }
1679b285192aSMauro Carvalho Chehab 
1680b285192aSMauro Carvalho Chehab static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
1681b285192aSMauro Carvalho Chehab 				 unsigned long arg)
1682b285192aSMauro Carvalho Chehab {
1683b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1684b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1685b285192aSMauro Carvalho Chehab 	int command = 0;
1686b285192aSMauro Carvalho Chehab 	struct upstream_user_struct *data_from_user;
1687b285192aSMauro Carvalho Chehab 
1688b285192aSMauro Carvalho Chehab 	data_from_user = (struct upstream_user_struct *)arg;
1689b285192aSMauro Carvalho Chehab 
1690b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1691b285192aSMauro Carvalho Chehab 		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1692b285192aSMauro Carvalho Chehab 		return 0;
1693b285192aSMauro Carvalho Chehab 	}
1694b285192aSMauro Carvalho Chehab 
1695b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1696b285192aSMauro Carvalho Chehab 
1697b285192aSMauro Carvalho Chehab 	if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1698b285192aSMauro Carvalho Chehab 		return 0;
1699b285192aSMauro Carvalho Chehab 
1700b285192aSMauro Carvalho Chehab 	dev->input_filename = data_from_user->input_filename;
1701b285192aSMauro Carvalho Chehab 	dev->input_audiofilename = data_from_user->input_filename;
1702b285192aSMauro Carvalho Chehab 	dev->vid_stdname = data_from_user->vid_stdname;
1703b285192aSMauro Carvalho Chehab 	dev->pixel_format = data_from_user->pixel_format;
1704b285192aSMauro Carvalho Chehab 	dev->channel_select = data_from_user->channel_select;
1705b285192aSMauro Carvalho Chehab 	dev->command = data_from_user->command;
1706b285192aSMauro Carvalho Chehab 
1707b285192aSMauro Carvalho Chehab 	switch (command) {
1708b285192aSMauro Carvalho Chehab 	case UPSTREAM_START_VIDEO:
1709b285192aSMauro Carvalho Chehab 		cx25821_start_upstream_video_ch1(dev, data_from_user);
1710b285192aSMauro Carvalho Chehab 		break;
1711b285192aSMauro Carvalho Chehab 
1712b285192aSMauro Carvalho Chehab 	case UPSTREAM_STOP_VIDEO:
1713b285192aSMauro Carvalho Chehab 		cx25821_stop_upstream_video_ch1(dev);
1714b285192aSMauro Carvalho Chehab 		break;
1715b285192aSMauro Carvalho Chehab 	}
1716b285192aSMauro Carvalho Chehab 
1717b285192aSMauro Carvalho Chehab 	return 0;
1718b285192aSMauro Carvalho Chehab }
1719b285192aSMauro Carvalho Chehab 
1720b285192aSMauro Carvalho Chehab static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
1721b285192aSMauro Carvalho Chehab 				  unsigned long arg)
1722b285192aSMauro Carvalho Chehab {
1723b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1724b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1725b285192aSMauro Carvalho Chehab 	int command = 0;
1726b285192aSMauro Carvalho Chehab 	struct upstream_user_struct *data_from_user;
1727b285192aSMauro Carvalho Chehab 
1728b285192aSMauro Carvalho Chehab 	data_from_user = (struct upstream_user_struct *)arg;
1729b285192aSMauro Carvalho Chehab 
1730b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1731b285192aSMauro Carvalho Chehab 		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1732b285192aSMauro Carvalho Chehab 		return 0;
1733b285192aSMauro Carvalho Chehab 	}
1734b285192aSMauro Carvalho Chehab 
1735b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1736b285192aSMauro Carvalho Chehab 
1737b285192aSMauro Carvalho Chehab 	if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1738b285192aSMauro Carvalho Chehab 		return 0;
1739b285192aSMauro Carvalho Chehab 
1740b285192aSMauro Carvalho Chehab 	dev->input_filename_ch2 = data_from_user->input_filename;
1741b285192aSMauro Carvalho Chehab 	dev->input_audiofilename = data_from_user->input_filename;
1742b285192aSMauro Carvalho Chehab 	dev->vid_stdname_ch2 = data_from_user->vid_stdname;
1743b285192aSMauro Carvalho Chehab 	dev->pixel_format_ch2 = data_from_user->pixel_format;
1744b285192aSMauro Carvalho Chehab 	dev->channel_select_ch2 = data_from_user->channel_select;
1745b285192aSMauro Carvalho Chehab 	dev->command_ch2 = data_from_user->command;
1746b285192aSMauro Carvalho Chehab 
1747b285192aSMauro Carvalho Chehab 	switch (command) {
1748b285192aSMauro Carvalho Chehab 	case UPSTREAM_START_VIDEO:
1749b285192aSMauro Carvalho Chehab 		cx25821_start_upstream_video_ch2(dev, data_from_user);
1750b285192aSMauro Carvalho Chehab 		break;
1751b285192aSMauro Carvalho Chehab 
1752b285192aSMauro Carvalho Chehab 	case UPSTREAM_STOP_VIDEO:
1753b285192aSMauro Carvalho Chehab 		cx25821_stop_upstream_video_ch2(dev);
1754b285192aSMauro Carvalho Chehab 		break;
1755b285192aSMauro Carvalho Chehab 	}
1756b285192aSMauro Carvalho Chehab 
1757b285192aSMauro Carvalho Chehab 	return 0;
1758b285192aSMauro Carvalho Chehab }
1759b285192aSMauro Carvalho Chehab 
1760b285192aSMauro Carvalho Chehab static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
1761b285192aSMauro Carvalho Chehab 				  unsigned long arg)
1762b285192aSMauro Carvalho Chehab {
1763b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1764b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1765b285192aSMauro Carvalho Chehab 	int command = 0;
1766b285192aSMauro Carvalho Chehab 	struct upstream_user_struct *data_from_user;
1767b285192aSMauro Carvalho Chehab 
1768b285192aSMauro Carvalho Chehab 	data_from_user = (struct upstream_user_struct *)arg;
1769b285192aSMauro Carvalho Chehab 
1770b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1771b285192aSMauro Carvalho Chehab 		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1772b285192aSMauro Carvalho Chehab 		return 0;
1773b285192aSMauro Carvalho Chehab 	}
1774b285192aSMauro Carvalho Chehab 
1775b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1776b285192aSMauro Carvalho Chehab 
1777b285192aSMauro Carvalho Chehab 	if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
1778b285192aSMauro Carvalho Chehab 		return 0;
1779b285192aSMauro Carvalho Chehab 
1780b285192aSMauro Carvalho Chehab 	dev->input_filename = data_from_user->input_filename;
1781b285192aSMauro Carvalho Chehab 	dev->input_audiofilename = data_from_user->input_filename;
1782b285192aSMauro Carvalho Chehab 	dev->vid_stdname = data_from_user->vid_stdname;
1783b285192aSMauro Carvalho Chehab 	dev->pixel_format = data_from_user->pixel_format;
1784b285192aSMauro Carvalho Chehab 	dev->channel_select = data_from_user->channel_select;
1785b285192aSMauro Carvalho Chehab 	dev->command = data_from_user->command;
1786b285192aSMauro Carvalho Chehab 
1787b285192aSMauro Carvalho Chehab 	switch (command) {
1788b285192aSMauro Carvalho Chehab 	case UPSTREAM_START_AUDIO:
1789b285192aSMauro Carvalho Chehab 		cx25821_start_upstream_audio(dev, data_from_user);
1790b285192aSMauro Carvalho Chehab 		break;
1791b285192aSMauro Carvalho Chehab 
1792b285192aSMauro Carvalho Chehab 	case UPSTREAM_STOP_AUDIO:
1793b285192aSMauro Carvalho Chehab 		cx25821_stop_upstream_audio(dev);
1794b285192aSMauro Carvalho Chehab 		break;
1795b285192aSMauro Carvalho Chehab 	}
1796b285192aSMauro Carvalho Chehab 
1797b285192aSMauro Carvalho Chehab 	return 0;
1798b285192aSMauro Carvalho Chehab }
1799b285192aSMauro Carvalho Chehab 
1800b285192aSMauro Carvalho Chehab static long video_ioctl_set(struct file *file, unsigned int cmd,
1801b285192aSMauro Carvalho Chehab 			   unsigned long arg)
1802b285192aSMauro Carvalho Chehab {
1803b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1804b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1805b285192aSMauro Carvalho Chehab 	struct downstream_user_struct *data_from_user;
1806b285192aSMauro Carvalho Chehab 	int command;
1807b285192aSMauro Carvalho Chehab 	int width = 720;
1808b285192aSMauro Carvalho Chehab 	int selected_channel = 0;
1809b285192aSMauro Carvalho Chehab 	int pix_format = 0;
1810b285192aSMauro Carvalho Chehab 	int i = 0;
1811b285192aSMauro Carvalho Chehab 	int cif_enable = 0;
1812b285192aSMauro Carvalho Chehab 	int cif_width = 0;
1813b285192aSMauro Carvalho Chehab 
1814b285192aSMauro Carvalho Chehab 	data_from_user = (struct downstream_user_struct *)arg;
1815b285192aSMauro Carvalho Chehab 
1816b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1817b285192aSMauro Carvalho Chehab 		pr_err("%s(): User data is INVALID. Returning\n", __func__);
1818b285192aSMauro Carvalho Chehab 		return 0;
1819b285192aSMauro Carvalho Chehab 	}
1820b285192aSMauro Carvalho Chehab 
1821b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1822b285192aSMauro Carvalho Chehab 
1823b285192aSMauro Carvalho Chehab 	if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
1824b285192aSMauro Carvalho Chehab 	   && command != ENABLE_CIF_RESOLUTION && command != REG_READ
1825b285192aSMauro Carvalho Chehab 	   && command != REG_WRITE && command != MEDUSA_READ
1826b285192aSMauro Carvalho Chehab 	   && command != MEDUSA_WRITE) {
1827b285192aSMauro Carvalho Chehab 		return 0;
1828b285192aSMauro Carvalho Chehab 	}
1829b285192aSMauro Carvalho Chehab 
1830b285192aSMauro Carvalho Chehab 	switch (command) {
1831b285192aSMauro Carvalho Chehab 	case SET_VIDEO_STD:
1832b285192aSMauro Carvalho Chehab 		if (!strcmp(data_from_user->vid_stdname, "PAL"))
1833b285192aSMauro Carvalho Chehab 			dev->tvnorm = V4L2_STD_PAL_BG;
1834b285192aSMauro Carvalho Chehab 		else
1835b285192aSMauro Carvalho Chehab 			dev->tvnorm = V4L2_STD_NTSC_M;
1836b285192aSMauro Carvalho Chehab 		medusa_set_videostandard(dev);
1837b285192aSMauro Carvalho Chehab 		break;
1838b285192aSMauro Carvalho Chehab 
1839b285192aSMauro Carvalho Chehab 	case SET_PIXEL_FORMAT:
1840b285192aSMauro Carvalho Chehab 		selected_channel = data_from_user->decoder_select;
1841b285192aSMauro Carvalho Chehab 		pix_format = data_from_user->pixel_format;
1842b285192aSMauro Carvalho Chehab 
1843b285192aSMauro Carvalho Chehab 		if (!(selected_channel <= 7 && selected_channel >= 0)) {
1844b285192aSMauro Carvalho Chehab 			selected_channel -= 4;
1845b285192aSMauro Carvalho Chehab 			selected_channel = selected_channel % 8;
1846b285192aSMauro Carvalho Chehab 		}
1847b285192aSMauro Carvalho Chehab 
1848b285192aSMauro Carvalho Chehab 		if (selected_channel >= 0)
1849b285192aSMauro Carvalho Chehab 			cx25821_set_pixel_format(dev, selected_channel,
1850b285192aSMauro Carvalho Chehab 						pix_format);
1851b285192aSMauro Carvalho Chehab 
1852b285192aSMauro Carvalho Chehab 		break;
1853b285192aSMauro Carvalho Chehab 
1854b285192aSMauro Carvalho Chehab 	case ENABLE_CIF_RESOLUTION:
1855b285192aSMauro Carvalho Chehab 		selected_channel = data_from_user->decoder_select;
1856b285192aSMauro Carvalho Chehab 		cif_enable = data_from_user->cif_resolution_enable;
1857b285192aSMauro Carvalho Chehab 		cif_width = data_from_user->cif_width;
1858b285192aSMauro Carvalho Chehab 
1859b285192aSMauro Carvalho Chehab 		if (cif_enable) {
1860b285192aSMauro Carvalho Chehab 			if (dev->tvnorm & V4L2_STD_PAL_BG
1861b285192aSMauro Carvalho Chehab 			    || dev->tvnorm & V4L2_STD_PAL_DK) {
1862b285192aSMauro Carvalho Chehab 				width = 352;
1863b285192aSMauro Carvalho Chehab 			} else {
1864b285192aSMauro Carvalho Chehab 				width = cif_width;
1865b285192aSMauro Carvalho Chehab 				if (cif_width != 320 && cif_width != 352)
1866b285192aSMauro Carvalho Chehab 					width = 320;
1867b285192aSMauro Carvalho Chehab 			}
1868b285192aSMauro Carvalho Chehab 		}
1869b285192aSMauro Carvalho Chehab 
1870b285192aSMauro Carvalho Chehab 		if (!(selected_channel <= 7 && selected_channel >= 0)) {
1871b285192aSMauro Carvalho Chehab 			selected_channel -= 4;
1872b285192aSMauro Carvalho Chehab 			selected_channel = selected_channel % 8;
1873b285192aSMauro Carvalho Chehab 		}
1874b285192aSMauro Carvalho Chehab 
1875b285192aSMauro Carvalho Chehab 		if (selected_channel <= 7 && selected_channel >= 0) {
1876b285192aSMauro Carvalho Chehab 			dev->channels[selected_channel].use_cif_resolution =
1877b285192aSMauro Carvalho Chehab 				cif_enable;
1878b285192aSMauro Carvalho Chehab 			dev->channels[selected_channel].cif_width = width;
1879b285192aSMauro Carvalho Chehab 		} else {
1880b285192aSMauro Carvalho Chehab 			for (i = 0; i < VID_CHANNEL_NUM; i++) {
1881b285192aSMauro Carvalho Chehab 				dev->channels[i].use_cif_resolution =
1882b285192aSMauro Carvalho Chehab 					cif_enable;
1883b285192aSMauro Carvalho Chehab 				dev->channels[i].cif_width = width;
1884b285192aSMauro Carvalho Chehab 			}
1885b285192aSMauro Carvalho Chehab 		}
1886b285192aSMauro Carvalho Chehab 
1887b285192aSMauro Carvalho Chehab 		medusa_set_resolution(dev, width, selected_channel);
1888b285192aSMauro Carvalho Chehab 		break;
1889b285192aSMauro Carvalho Chehab 	case REG_READ:
1890b285192aSMauro Carvalho Chehab 		data_from_user->reg_data = cx_read(data_from_user->reg_address);
1891b285192aSMauro Carvalho Chehab 		break;
1892b285192aSMauro Carvalho Chehab 	case REG_WRITE:
1893b285192aSMauro Carvalho Chehab 		cx_write(data_from_user->reg_address, data_from_user->reg_data);
1894b285192aSMauro Carvalho Chehab 		break;
1895b285192aSMauro Carvalho Chehab 	case MEDUSA_READ:
1896b285192aSMauro Carvalho Chehab 		cx25821_i2c_read(&dev->i2c_bus[0],
1897b285192aSMauro Carvalho Chehab 					 (u16) data_from_user->reg_address,
1898b285192aSMauro Carvalho Chehab 					 &data_from_user->reg_data);
1899b285192aSMauro Carvalho Chehab 		break;
1900b285192aSMauro Carvalho Chehab 	case MEDUSA_WRITE:
1901b285192aSMauro Carvalho Chehab 		cx25821_i2c_write(&dev->i2c_bus[0],
1902b285192aSMauro Carvalho Chehab 				  (u16) data_from_user->reg_address,
1903b285192aSMauro Carvalho Chehab 				  data_from_user->reg_data);
1904b285192aSMauro Carvalho Chehab 		break;
1905b285192aSMauro Carvalho Chehab 	}
1906b285192aSMauro Carvalho Chehab 
1907b285192aSMauro Carvalho Chehab 	return 0;
1908b285192aSMauro Carvalho Chehab }
1909b285192aSMauro Carvalho Chehab 
1910b285192aSMauro Carvalho Chehab static long cx25821_video_ioctl(struct file *file,
1911b285192aSMauro Carvalho Chehab 				unsigned int cmd, unsigned long arg)
1912b285192aSMauro Carvalho Chehab {
1913b285192aSMauro Carvalho Chehab 	int ret = 0;
1914b285192aSMauro Carvalho Chehab 
1915b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1916b285192aSMauro Carvalho Chehab 
1917b285192aSMauro Carvalho Chehab 	/* check to see if it's the video upstream */
1918b285192aSMauro Carvalho Chehab 	if (fh->channel_id == SRAM_CH09) {
1919b285192aSMauro Carvalho Chehab 		ret = video_ioctl_upstream9(file, cmd, arg);
1920b285192aSMauro Carvalho Chehab 		return ret;
1921b285192aSMauro Carvalho Chehab 	} else if (fh->channel_id == SRAM_CH10) {
1922b285192aSMauro Carvalho Chehab 		ret = video_ioctl_upstream10(file, cmd, arg);
1923b285192aSMauro Carvalho Chehab 		return ret;
1924b285192aSMauro Carvalho Chehab 	} else if (fh->channel_id == SRAM_CH11) {
1925b285192aSMauro Carvalho Chehab 		ret = video_ioctl_upstream11(file, cmd, arg);
1926b285192aSMauro Carvalho Chehab 		ret = video_ioctl_set(file, cmd, arg);
1927b285192aSMauro Carvalho Chehab 		return ret;
1928b285192aSMauro Carvalho Chehab 	}
1929b285192aSMauro Carvalho Chehab 
1930b285192aSMauro Carvalho Chehab 	return video_ioctl2(file, cmd, arg);
1931b285192aSMauro Carvalho Chehab }
1932b285192aSMauro Carvalho Chehab 
1933b285192aSMauro Carvalho Chehab /* exported stuff */
1934b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops = {
1935b285192aSMauro Carvalho Chehab 	.owner = THIS_MODULE,
1936b285192aSMauro Carvalho Chehab 	.open = video_open,
1937b285192aSMauro Carvalho Chehab 	.release = video_release,
1938b285192aSMauro Carvalho Chehab 	.read = video_read,
1939b285192aSMauro Carvalho Chehab 	.poll = video_poll,
1940b285192aSMauro Carvalho Chehab 	.mmap = cx25821_video_mmap,
1941b285192aSMauro Carvalho Chehab 	.ioctl = cx25821_video_ioctl,
1942b285192aSMauro Carvalho Chehab };
1943b285192aSMauro Carvalho Chehab 
1944b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops = {
1945b285192aSMauro Carvalho Chehab 	.vidioc_querycap = cx25821_vidioc_querycap,
1946b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
1947b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
1948b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
1949b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1950b285192aSMauro Carvalho Chehab 	.vidioc_reqbufs = cx25821_vidioc_reqbufs,
1951b285192aSMauro Carvalho Chehab 	.vidioc_querybuf = cx25821_vidioc_querybuf,
1952b285192aSMauro Carvalho Chehab 	.vidioc_qbuf = cx25821_vidioc_qbuf,
1953b285192aSMauro Carvalho Chehab 	.vidioc_dqbuf = vidioc_dqbuf,
1954b285192aSMauro Carvalho Chehab #ifdef TUNER_FLAG
1955b285192aSMauro Carvalho Chehab 	.vidioc_s_std = cx25821_vidioc_s_std,
1956b285192aSMauro Carvalho Chehab 	.vidioc_querystd = cx25821_vidioc_querystd,
1957b285192aSMauro Carvalho Chehab #endif
1958b285192aSMauro Carvalho Chehab 	.vidioc_cropcap = cx25821_vidioc_cropcap,
1959b285192aSMauro Carvalho Chehab 	.vidioc_s_crop = cx25821_vidioc_s_crop,
1960b285192aSMauro Carvalho Chehab 	.vidioc_g_crop = cx25821_vidioc_g_crop,
1961b285192aSMauro Carvalho Chehab 	.vidioc_enum_input = cx25821_vidioc_enum_input,
1962b285192aSMauro Carvalho Chehab 	.vidioc_g_input = cx25821_vidioc_g_input,
1963b285192aSMauro Carvalho Chehab 	.vidioc_s_input = cx25821_vidioc_s_input,
1964b285192aSMauro Carvalho Chehab 	.vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
1965b285192aSMauro Carvalho Chehab 	.vidioc_s_ctrl = vidioc_s_ctrl,
1966b285192aSMauro Carvalho Chehab 	.vidioc_queryctrl = cx25821_vidioc_queryctrl,
1967b285192aSMauro Carvalho Chehab 	.vidioc_streamon = vidioc_streamon,
1968b285192aSMauro Carvalho Chehab 	.vidioc_streamoff = vidioc_streamoff,
1969b285192aSMauro Carvalho Chehab 	.vidioc_log_status = vidioc_log_status,
1970b285192aSMauro Carvalho Chehab 	.vidioc_g_priority = cx25821_vidioc_g_priority,
1971b285192aSMauro Carvalho Chehab 	.vidioc_s_priority = cx25821_vidioc_s_priority,
1972b285192aSMauro Carvalho Chehab #ifdef TUNER_FLAG
1973b285192aSMauro Carvalho Chehab 	.vidioc_g_tuner = cx25821_vidioc_g_tuner,
1974b285192aSMauro Carvalho Chehab 	.vidioc_s_tuner = cx25821_vidioc_s_tuner,
1975b285192aSMauro Carvalho Chehab 	.vidioc_g_frequency = cx25821_vidioc_g_frequency,
1976b285192aSMauro Carvalho Chehab 	.vidioc_s_frequency = cx25821_vidioc_s_frequency,
1977b285192aSMauro Carvalho Chehab #endif
1978b285192aSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
1979b285192aSMauro Carvalho Chehab 	.vidioc_g_register = cx25821_vidioc_g_register,
1980b285192aSMauro Carvalho Chehab 	.vidioc_s_register = cx25821_vidioc_s_register,
1981b285192aSMauro Carvalho Chehab #endif
1982b285192aSMauro Carvalho Chehab };
1983b285192aSMauro Carvalho Chehab 
1984b285192aSMauro Carvalho Chehab struct video_device cx25821_videoioctl_template = {
1985b285192aSMauro Carvalho Chehab 	.name = "cx25821-videoioctl",
1986b285192aSMauro Carvalho Chehab 	.fops = &video_fops,
1987b285192aSMauro Carvalho Chehab 	.ioctl_ops = &video_ioctl_ops,
1988b285192aSMauro Carvalho Chehab 	.tvnorms = CX25821_NORMS,
1989b285192aSMauro Carvalho Chehab 	.current_norm = V4L2_STD_NTSC_M,
1990b285192aSMauro Carvalho Chehab };
1991