xref: /linux/drivers/media/pci/cx25821/cx25821-video.c (revision f8d7ee70919d44ef4f01f3c9bc49af54fdc433bc)
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 
37b285192aSMauro Carvalho Chehab module_param_array(video_nr, int, NULL, 0444);
38b285192aSMauro Carvalho Chehab 
39b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_nr, "video device numbers");
40b285192aSMauro Carvalho Chehab 
41b285192aSMauro Carvalho Chehab static unsigned int video_debug = VIDEO_DEBUG;
42b285192aSMauro Carvalho Chehab module_param(video_debug, int, 0644);
43b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
44b285192aSMauro Carvalho Chehab 
45b285192aSMauro Carvalho Chehab static unsigned int irq_debug;
46b285192aSMauro Carvalho Chehab module_param(irq_debug, int, 0644);
47b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
48b285192aSMauro Carvalho Chehab 
4995c232a2SHans Verkuil static unsigned int vid_limit = 16;
50b285192aSMauro Carvalho Chehab module_param(vid_limit, int, 0644);
51b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
52b285192aSMauro Carvalho Chehab 
53b285192aSMauro Carvalho Chehab #define FORMAT_FLAGS_PACKED       0x01
54b285192aSMauro Carvalho Chehab 
5595c232a2SHans Verkuil static const struct cx25821_fmt formats[] = {
56b285192aSMauro Carvalho Chehab 	{
57b285192aSMauro Carvalho Chehab 		.name = "8 bpp, gray",
58b285192aSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_GREY,
59b285192aSMauro Carvalho Chehab 		.depth = 8,
60b285192aSMauro Carvalho Chehab 		.flags = FORMAT_FLAGS_PACKED,
61b285192aSMauro Carvalho Chehab 	 }, {
62b285192aSMauro Carvalho Chehab 		.name = "4:1:1, packed, Y41P",
63b285192aSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_Y41P,
64b285192aSMauro Carvalho Chehab 		.depth = 12,
65b285192aSMauro Carvalho Chehab 		.flags = FORMAT_FLAGS_PACKED,
66b285192aSMauro Carvalho Chehab 	}, {
67b285192aSMauro Carvalho Chehab 		.name = "4:2:2, packed, YUYV",
68b285192aSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_YUYV,
69b285192aSMauro Carvalho Chehab 		.depth = 16,
70b285192aSMauro Carvalho Chehab 		.flags = FORMAT_FLAGS_PACKED,
71b285192aSMauro Carvalho Chehab 	}, {
72b285192aSMauro Carvalho Chehab 		.name = "4:2:2, packed, UYVY",
73b285192aSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_UYVY,
74b285192aSMauro Carvalho Chehab 		.depth = 16,
75b285192aSMauro Carvalho Chehab 		.flags = FORMAT_FLAGS_PACKED,
76b285192aSMauro Carvalho Chehab 	}, {
77b285192aSMauro Carvalho Chehab 		.name = "4:2:0, YUV",
78b285192aSMauro Carvalho Chehab 		.fourcc = V4L2_PIX_FMT_YUV420,
79b285192aSMauro Carvalho Chehab 		.depth = 12,
80b285192aSMauro Carvalho Chehab 		.flags = FORMAT_FLAGS_PACKED,
81b285192aSMauro Carvalho Chehab 	},
82b285192aSMauro Carvalho Chehab };
83b285192aSMauro Carvalho Chehab 
8495c232a2SHans Verkuil static const struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
85b285192aSMauro Carvalho Chehab {
86b285192aSMauro Carvalho Chehab 	unsigned int i;
87b285192aSMauro Carvalho Chehab 
88b285192aSMauro Carvalho Chehab 	if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P)
89b285192aSMauro Carvalho Chehab 		return formats + 1;
90b285192aSMauro Carvalho Chehab 
91b285192aSMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(formats); i++)
92b285192aSMauro Carvalho Chehab 		if (formats[i].fourcc == fourcc)
93b285192aSMauro Carvalho Chehab 			return formats + i;
94b285192aSMauro Carvalho Chehab 
95b285192aSMauro Carvalho Chehab 	pr_err("%s(0x%08x) NOT FOUND\n", __func__, fourcc);
96b285192aSMauro Carvalho Chehab 	return NULL;
97b285192aSMauro Carvalho Chehab }
98b285192aSMauro Carvalho Chehab 
99b285192aSMauro Carvalho Chehab void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
100b285192aSMauro Carvalho Chehab 			  u32 count)
101b285192aSMauro Carvalho Chehab {
102b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf;
103b285192aSMauro Carvalho Chehab 	int bc;
104b285192aSMauro Carvalho Chehab 
105b285192aSMauro Carvalho Chehab 	for (bc = 0;; bc++) {
106b285192aSMauro Carvalho Chehab 		if (list_empty(&q->active)) {
107b285192aSMauro Carvalho Chehab 			dprintk(1, "bc=%d (=0: active empty)\n", bc);
108b285192aSMauro Carvalho Chehab 			break;
109b285192aSMauro Carvalho Chehab 		}
110b285192aSMauro Carvalho Chehab 
111b285192aSMauro Carvalho Chehab 		buf = list_entry(q->active.next, struct cx25821_buffer,
112b285192aSMauro Carvalho Chehab 				vb.queue);
113b285192aSMauro Carvalho Chehab 
114b285192aSMauro Carvalho Chehab 		/* count comes from the hw and it is 16bit wide --
115b285192aSMauro Carvalho Chehab 		 * this trick handles wrap-arounds correctly for
116b285192aSMauro Carvalho Chehab 		 * up to 32767 buffers in flight... */
117b285192aSMauro Carvalho Chehab 		if ((s16) (count - buf->count) < 0)
118b285192aSMauro Carvalho Chehab 			break;
119b285192aSMauro Carvalho Chehab 
1208e6057b5SSakari Ailus 		v4l2_get_timestamp(&buf->vb.ts);
121b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_DONE;
122b285192aSMauro Carvalho Chehab 		list_del(&buf->vb.queue);
123b285192aSMauro Carvalho Chehab 		wake_up(&buf->vb.done);
124b285192aSMauro Carvalho Chehab 	}
125b285192aSMauro Carvalho Chehab 
126b285192aSMauro Carvalho Chehab 	if (list_empty(&q->active))
127b285192aSMauro Carvalho Chehab 		del_timer(&q->timeout);
128b285192aSMauro Carvalho Chehab 	else
129b285192aSMauro Carvalho Chehab 		mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
130b285192aSMauro Carvalho Chehab 	if (bc != 1)
131b285192aSMauro Carvalho Chehab 		pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
132b285192aSMauro Carvalho Chehab }
133b285192aSMauro Carvalho Chehab 
13495c232a2SHans Verkuil static int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
135b285192aSMauro Carvalho Chehab {
136b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
137b285192aSMauro Carvalho Chehab 		__func__, (unsigned int)norm, v4l2_norm_to_name(norm));
138b285192aSMauro Carvalho Chehab 
139b285192aSMauro Carvalho Chehab 	dev->tvnorm = norm;
140b285192aSMauro Carvalho Chehab 
141b285192aSMauro Carvalho Chehab 	/* Tell the internal A/V decoder */
142b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, core, s_std, norm);
143b285192aSMauro Carvalho Chehab 
144b285192aSMauro Carvalho Chehab 	return 0;
145b285192aSMauro Carvalho Chehab }
146b285192aSMauro Carvalho Chehab 
147b285192aSMauro Carvalho Chehab /*
148b285192aSMauro Carvalho Chehab static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
149b285192aSMauro Carvalho Chehab {
150b285192aSMauro Carvalho Chehab 	int i;
151b285192aSMauro Carvalho Chehab 
152b285192aSMauro Carvalho Chehab 	if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
153b285192aSMauro Carvalho Chehab 		return -EINVAL;
154b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX25821_CTLS; i++)
155b285192aSMauro Carvalho Chehab 		if (cx25821_ctls[i].v.id == qctrl->id)
156b285192aSMauro Carvalho Chehab 			break;
157b285192aSMauro Carvalho Chehab 	if (i == CX25821_CTLS) {
158b285192aSMauro Carvalho Chehab 		*qctrl = no_ctl;
159b285192aSMauro Carvalho Chehab 		return 0;
160b285192aSMauro Carvalho Chehab 	}
161b285192aSMauro Carvalho Chehab 	*qctrl = cx25821_ctls[i].v;
162b285192aSMauro Carvalho Chehab 	return 0;
163b285192aSMauro Carvalho Chehab }
164b285192aSMauro Carvalho Chehab */
165b285192aSMauro Carvalho Chehab 
166b285192aSMauro Carvalho Chehab /* resource management */
167b285192aSMauro Carvalho Chehab int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh,
168b285192aSMauro Carvalho Chehab 		    unsigned int bit)
169b285192aSMauro Carvalho Chehab {
170b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
171b285192aSMauro Carvalho Chehab 	if (fh->resources & bit)
172b285192aSMauro Carvalho Chehab 		/* have it already allocated */
173b285192aSMauro Carvalho Chehab 		return 1;
174b285192aSMauro Carvalho Chehab 
175b285192aSMauro Carvalho Chehab 	/* is it free? */
176b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
177b285192aSMauro Carvalho Chehab 	if (dev->channels[fh->channel_id].resources & bit) {
178b285192aSMauro Carvalho Chehab 		/* no, someone else uses it */
179b285192aSMauro Carvalho Chehab 		mutex_unlock(&dev->lock);
180b285192aSMauro Carvalho Chehab 		return 0;
181b285192aSMauro Carvalho Chehab 	}
182b285192aSMauro Carvalho Chehab 	/* it's free, grab it */
183b285192aSMauro Carvalho Chehab 	fh->resources |= bit;
184b285192aSMauro Carvalho Chehab 	dev->channels[fh->channel_id].resources |= bit;
185b285192aSMauro Carvalho Chehab 	dprintk(1, "res: get %d\n", bit);
186b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
187b285192aSMauro Carvalho Chehab 	return 1;
188b285192aSMauro Carvalho Chehab }
189b285192aSMauro Carvalho Chehab 
190b285192aSMauro Carvalho Chehab int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
191b285192aSMauro Carvalho Chehab {
192b285192aSMauro Carvalho Chehab 	return fh->resources & bit;
193b285192aSMauro Carvalho Chehab }
194b285192aSMauro Carvalho Chehab 
195b285192aSMauro Carvalho Chehab int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
196b285192aSMauro Carvalho Chehab {
197b285192aSMauro Carvalho Chehab 	return fh->dev->channels[fh->channel_id].resources & bit;
198b285192aSMauro Carvalho Chehab }
199b285192aSMauro Carvalho Chehab 
200b285192aSMauro Carvalho Chehab void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh,
201b285192aSMauro Carvalho Chehab 		      unsigned int bits)
202b285192aSMauro Carvalho Chehab {
203b285192aSMauro Carvalho Chehab 	BUG_ON((fh->resources & bits) != bits);
204b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
205b285192aSMauro Carvalho Chehab 
206b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
207b285192aSMauro Carvalho Chehab 	fh->resources &= ~bits;
208b285192aSMauro Carvalho Chehab 	dev->channels[fh->channel_id].resources &= ~bits;
209b285192aSMauro Carvalho Chehab 	dprintk(1, "res: put %d\n", bits);
210b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
211b285192aSMauro Carvalho Chehab }
212b285192aSMauro Carvalho Chehab 
21395c232a2SHans Verkuil static int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
214b285192aSMauro Carvalho Chehab {
215b285192aSMauro Carvalho Chehab 	struct v4l2_routing route;
216b285192aSMauro Carvalho Chehab 	memset(&route, 0, sizeof(route));
217b285192aSMauro Carvalho Chehab 
218b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
219b285192aSMauro Carvalho Chehab 		__func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
220b285192aSMauro Carvalho Chehab 		INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
221b285192aSMauro Carvalho Chehab 	dev->input = input;
222b285192aSMauro Carvalho Chehab 
223b285192aSMauro Carvalho Chehab 	route.input = INPUT(input)->vmux;
224b285192aSMauro Carvalho Chehab 
225b285192aSMauro Carvalho Chehab 	/* Tell the internal A/V decoder */
226b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
227b285192aSMauro Carvalho Chehab 
228b285192aSMauro Carvalho Chehab 	return 0;
229b285192aSMauro Carvalho Chehab }
230b285192aSMauro Carvalho Chehab 
231b285192aSMauro Carvalho Chehab int cx25821_start_video_dma(struct cx25821_dev *dev,
232b285192aSMauro Carvalho Chehab 			    struct cx25821_dmaqueue *q,
233b285192aSMauro Carvalho Chehab 			    struct cx25821_buffer *buf,
234bfef0d35SHans Verkuil 			    const struct sram_channel *channel)
235b285192aSMauro Carvalho Chehab {
236b285192aSMauro Carvalho Chehab 	int tmp = 0;
237b285192aSMauro Carvalho Chehab 
238b285192aSMauro Carvalho Chehab 	/* setup fifo + format */
239b285192aSMauro Carvalho Chehab 	cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
240b285192aSMauro Carvalho Chehab 
241b285192aSMauro Carvalho Chehab 	/* reset counter */
242b285192aSMauro Carvalho Chehab 	cx_write(channel->gpcnt_ctl, 3);
243b285192aSMauro Carvalho Chehab 	q->count = 1;
244b285192aSMauro Carvalho Chehab 
245b285192aSMauro Carvalho Chehab 	/* enable irq */
246b285192aSMauro Carvalho Chehab 	cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
247b285192aSMauro Carvalho Chehab 	cx_set(channel->int_msk, 0x11);
248b285192aSMauro Carvalho Chehab 
249b285192aSMauro Carvalho Chehab 	/* start dma */
250b285192aSMauro Carvalho Chehab 	cx_write(channel->dma_ctl, 0x11);	/* FIFO and RISC enable */
251b285192aSMauro Carvalho Chehab 
252b285192aSMauro Carvalho Chehab 	/* make sure upstream setting if any is reversed */
253b285192aSMauro Carvalho Chehab 	tmp = cx_read(VID_CH_MODE_SEL);
254b285192aSMauro Carvalho Chehab 	cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
255b285192aSMauro Carvalho Chehab 
256b285192aSMauro Carvalho Chehab 	return 0;
257b285192aSMauro Carvalho Chehab }
258b285192aSMauro Carvalho Chehab 
259dafc456cSMauro Carvalho Chehab static int cx25821_restart_video_queue(struct cx25821_dev *dev,
260b285192aSMauro Carvalho Chehab 				       struct cx25821_dmaqueue *q,
261bfef0d35SHans Verkuil 				       const struct sram_channel *channel)
262b285192aSMauro Carvalho Chehab {
263b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf, *prev;
264b285192aSMauro Carvalho Chehab 	struct list_head *item;
265b285192aSMauro Carvalho Chehab 
266b285192aSMauro Carvalho Chehab 	if (!list_empty(&q->active)) {
267b285192aSMauro Carvalho Chehab 		buf = list_entry(q->active.next, struct cx25821_buffer,
268b285192aSMauro Carvalho Chehab 				vb.queue);
269b285192aSMauro Carvalho Chehab 
270b285192aSMauro Carvalho Chehab 		cx25821_start_video_dma(dev, q, buf, channel);
271b285192aSMauro Carvalho Chehab 
272b285192aSMauro Carvalho Chehab 		list_for_each(item, &q->active) {
273b285192aSMauro Carvalho Chehab 			buf = list_entry(item, struct cx25821_buffer, vb.queue);
274b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
275b285192aSMauro Carvalho Chehab 		}
276b285192aSMauro Carvalho Chehab 
277b285192aSMauro Carvalho Chehab 		mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
278b285192aSMauro Carvalho Chehab 		return 0;
279b285192aSMauro Carvalho Chehab 	}
280b285192aSMauro Carvalho Chehab 
281b285192aSMauro Carvalho Chehab 	prev = NULL;
282b285192aSMauro Carvalho Chehab 	for (;;) {
283b285192aSMauro Carvalho Chehab 		if (list_empty(&q->queued))
284b285192aSMauro Carvalho Chehab 			return 0;
285b285192aSMauro Carvalho Chehab 
286b285192aSMauro Carvalho Chehab 		buf = list_entry(q->queued.next, struct cx25821_buffer,
287b285192aSMauro Carvalho Chehab 				vb.queue);
288b285192aSMauro Carvalho Chehab 
289b285192aSMauro Carvalho Chehab 		if (NULL == prev) {
290b285192aSMauro Carvalho Chehab 			list_move_tail(&buf->vb.queue, &q->active);
291b285192aSMauro Carvalho Chehab 			cx25821_start_video_dma(dev, q, buf, channel);
292b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
293b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
294b285192aSMauro Carvalho Chehab 			mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
295b285192aSMauro Carvalho Chehab 		} else if (prev->vb.width == buf->vb.width &&
296b285192aSMauro Carvalho Chehab 			   prev->vb.height == buf->vb.height &&
297b285192aSMauro Carvalho Chehab 			   prev->fmt == buf->fmt) {
298b285192aSMauro Carvalho Chehab 			list_move_tail(&buf->vb.queue, &q->active);
299b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
300b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
301b285192aSMauro Carvalho Chehab 			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
302b285192aSMauro Carvalho Chehab 			prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
303b285192aSMauro Carvalho Chehab 		} else {
304b285192aSMauro Carvalho Chehab 			return 0;
305b285192aSMauro Carvalho Chehab 		}
306b285192aSMauro Carvalho Chehab 		prev = buf;
307b285192aSMauro Carvalho Chehab 	}
308b285192aSMauro Carvalho Chehab }
309b285192aSMauro Carvalho Chehab 
310dafc456cSMauro Carvalho Chehab static void cx25821_vid_timeout(unsigned long data)
311b285192aSMauro Carvalho Chehab {
312b285192aSMauro Carvalho Chehab 	struct cx25821_data *timeout_data = (struct cx25821_data *)data;
313b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = timeout_data->dev;
314bfef0d35SHans Verkuil 	const struct sram_channel *channel = timeout_data->channel;
315b285192aSMauro Carvalho Chehab 	struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq;
316b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf;
317b285192aSMauro Carvalho Chehab 	unsigned long flags;
318b285192aSMauro Carvalho Chehab 
319b285192aSMauro Carvalho Chehab 	/* cx25821_sram_channel_dump(dev, channel); */
320b285192aSMauro Carvalho Chehab 	cx_clear(channel->dma_ctl, 0x11);
321b285192aSMauro Carvalho Chehab 
322b285192aSMauro Carvalho Chehab 	spin_lock_irqsave(&dev->slock, flags);
323b285192aSMauro Carvalho Chehab 	while (!list_empty(&q->active)) {
324b285192aSMauro Carvalho Chehab 		buf = list_entry(q->active.next, struct cx25821_buffer,
325b285192aSMauro Carvalho Chehab 				vb.queue);
326b285192aSMauro Carvalho Chehab 		list_del(&buf->vb.queue);
327b285192aSMauro Carvalho Chehab 
328b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_ERROR;
329b285192aSMauro Carvalho Chehab 		wake_up(&buf->vb.done);
330b285192aSMauro Carvalho Chehab 	}
331b285192aSMauro Carvalho Chehab 
332b285192aSMauro Carvalho Chehab 	cx25821_restart_video_queue(dev, q, channel);
333b285192aSMauro Carvalho Chehab 	spin_unlock_irqrestore(&dev->slock, flags);
334b285192aSMauro Carvalho Chehab }
335b285192aSMauro Carvalho Chehab 
336b285192aSMauro Carvalho Chehab int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
337b285192aSMauro Carvalho Chehab {
338b285192aSMauro Carvalho Chehab 	u32 count = 0;
339b285192aSMauro Carvalho Chehab 	int handled = 0;
340b285192aSMauro Carvalho Chehab 	u32 mask;
341bfef0d35SHans Verkuil 	const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
342b285192aSMauro Carvalho Chehab 
343b285192aSMauro Carvalho Chehab 	mask = cx_read(channel->int_msk);
344b285192aSMauro Carvalho Chehab 	if (0 == (status & mask))
345b285192aSMauro Carvalho Chehab 		return handled;
346b285192aSMauro Carvalho Chehab 
347b285192aSMauro Carvalho Chehab 	cx_write(channel->int_stat, status);
348b285192aSMauro Carvalho Chehab 
349b285192aSMauro Carvalho Chehab 	/* risc op code error */
350b285192aSMauro Carvalho Chehab 	if (status & (1 << 16)) {
351b285192aSMauro Carvalho Chehab 		pr_warn("%s, %s: video risc op code error\n",
352b285192aSMauro Carvalho Chehab 			dev->name, channel->name);
353b285192aSMauro Carvalho Chehab 		cx_clear(channel->dma_ctl, 0x11);
354b285192aSMauro Carvalho Chehab 		cx25821_sram_channel_dump(dev, channel);
355b285192aSMauro Carvalho Chehab 	}
356b285192aSMauro Carvalho Chehab 
357b285192aSMauro Carvalho Chehab 	/* risc1 y */
358b285192aSMauro Carvalho Chehab 	if (status & FLD_VID_DST_RISC1) {
359b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
360b285192aSMauro Carvalho Chehab 		count = cx_read(channel->gpcnt);
361b285192aSMauro Carvalho Chehab 		cx25821_video_wakeup(dev, &dev->channels[channel->i].vidq,
362b285192aSMauro Carvalho Chehab 				count);
363b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
364b285192aSMauro Carvalho Chehab 		handled++;
365b285192aSMauro Carvalho Chehab 	}
366b285192aSMauro Carvalho Chehab 
367b285192aSMauro Carvalho Chehab 	/* risc2 y */
368b285192aSMauro Carvalho Chehab 	if (status & 0x10) {
369b285192aSMauro Carvalho Chehab 		dprintk(2, "stopper video\n");
370b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
371b285192aSMauro Carvalho Chehab 		cx25821_restart_video_queue(dev,
372b285192aSMauro Carvalho Chehab 				&dev->channels[channel->i].vidq, channel);
373b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
374b285192aSMauro Carvalho Chehab 		handled++;
375b285192aSMauro Carvalho Chehab 	}
376b285192aSMauro Carvalho Chehab 	return handled;
377b285192aSMauro Carvalho Chehab }
378b285192aSMauro Carvalho Chehab 
37995c232a2SHans Verkuil static int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
380b285192aSMauro Carvalho Chehab 		 unsigned int *size)
381b285192aSMauro Carvalho Chehab {
382b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = q->priv_data;
383b285192aSMauro Carvalho Chehab 
384b285192aSMauro Carvalho Chehab 	*size = fh->fmt->depth * fh->width * fh->height >> 3;
385b285192aSMauro Carvalho Chehab 
386b285192aSMauro Carvalho Chehab 	if (0 == *count)
387b285192aSMauro Carvalho Chehab 		*count = 32;
388b285192aSMauro Carvalho Chehab 
389b285192aSMauro Carvalho Chehab 	if (*size * *count > vid_limit * 1024 * 1024)
390b285192aSMauro Carvalho Chehab 		*count = (vid_limit * 1024 * 1024) / *size;
391b285192aSMauro Carvalho Chehab 
392b285192aSMauro Carvalho Chehab 	return 0;
393b285192aSMauro Carvalho Chehab }
394b285192aSMauro Carvalho Chehab 
39595c232a2SHans Verkuil static int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
396b285192aSMauro Carvalho Chehab 		   enum v4l2_field field)
397b285192aSMauro Carvalho Chehab {
398b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = q->priv_data;
399b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
400b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
401b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
402b285192aSMauro Carvalho Chehab 	int rc, init_buffer = 0;
403b285192aSMauro Carvalho Chehab 	u32 line0_offset;
404b285192aSMauro Carvalho Chehab 	struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
405b285192aSMauro Carvalho Chehab 	int bpl_local = LINE_SIZE_D1;
406b285192aSMauro Carvalho Chehab 	int channel_opened = fh->channel_id;
407b285192aSMauro Carvalho Chehab 
408b285192aSMauro Carvalho Chehab 	BUG_ON(NULL == fh->fmt);
409b285192aSMauro Carvalho Chehab 	if (fh->width < 48 || fh->width > 720 ||
410b285192aSMauro Carvalho Chehab 	    fh->height < 32 || fh->height > 576)
411b285192aSMauro Carvalho Chehab 		return -EINVAL;
412b285192aSMauro Carvalho Chehab 
413b285192aSMauro Carvalho Chehab 	buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
414b285192aSMauro Carvalho Chehab 
415b285192aSMauro Carvalho Chehab 	if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
416b285192aSMauro Carvalho Chehab 		return -EINVAL;
417b285192aSMauro Carvalho Chehab 
418b285192aSMauro Carvalho Chehab 	if (buf->fmt != fh->fmt ||
419b285192aSMauro Carvalho Chehab 	    buf->vb.width != fh->width ||
420b285192aSMauro Carvalho Chehab 	    buf->vb.height != fh->height || buf->vb.field != field) {
421b285192aSMauro Carvalho Chehab 		buf->fmt = fh->fmt;
422b285192aSMauro Carvalho Chehab 		buf->vb.width = fh->width;
423b285192aSMauro Carvalho Chehab 		buf->vb.height = fh->height;
424b285192aSMauro Carvalho Chehab 		buf->vb.field = field;
425b285192aSMauro Carvalho Chehab 		init_buffer = 1;
426b285192aSMauro Carvalho Chehab 	}
427b285192aSMauro Carvalho Chehab 
428b285192aSMauro Carvalho Chehab 	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
429b285192aSMauro Carvalho Chehab 		init_buffer = 1;
430b285192aSMauro Carvalho Chehab 		rc = videobuf_iolock(q, &buf->vb, NULL);
431b285192aSMauro Carvalho Chehab 		if (0 != rc) {
432b285192aSMauro Carvalho Chehab 			printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
433b285192aSMauro Carvalho Chehab 			goto fail;
434b285192aSMauro Carvalho Chehab 		}
435b285192aSMauro Carvalho Chehab 	}
436b285192aSMauro Carvalho Chehab 
437b285192aSMauro Carvalho Chehab 	dprintk(1, "init_buffer=%d\n", init_buffer);
438b285192aSMauro Carvalho Chehab 
439b285192aSMauro Carvalho Chehab 	if (init_buffer) {
440b285192aSMauro Carvalho Chehab 
441b285192aSMauro Carvalho Chehab 		channel_opened = dev->channel_opened;
442b285192aSMauro Carvalho Chehab 		if (channel_opened < 0 || channel_opened > 7)
443b285192aSMauro Carvalho Chehab 			channel_opened = 7;
444b285192aSMauro Carvalho Chehab 
445b285192aSMauro Carvalho Chehab 		if (dev->channels[channel_opened].pixel_formats ==
446b285192aSMauro Carvalho Chehab 				PIXEL_FRMT_411)
447b285192aSMauro Carvalho Chehab 			buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
448b285192aSMauro Carvalho Chehab 		else
449b285192aSMauro Carvalho Chehab 			buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
450b285192aSMauro Carvalho Chehab 
451b285192aSMauro Carvalho Chehab 		if (dev->channels[channel_opened].pixel_formats ==
452b285192aSMauro Carvalho Chehab 				PIXEL_FRMT_411) {
453b285192aSMauro Carvalho Chehab 			bpl_local = buf->bpl;
454b285192aSMauro Carvalho Chehab 		} else {
455b285192aSMauro Carvalho Chehab 			bpl_local = buf->bpl;   /* Default */
456b285192aSMauro Carvalho Chehab 
457b285192aSMauro Carvalho Chehab 			if (channel_opened >= 0 && channel_opened <= 7) {
458b285192aSMauro Carvalho Chehab 				if (dev->channels[channel_opened]
459b285192aSMauro Carvalho Chehab 						.use_cif_resolution) {
460b285192aSMauro Carvalho Chehab 					if (dev->tvnorm & V4L2_STD_PAL_BG ||
461b285192aSMauro Carvalho Chehab 					    dev->tvnorm & V4L2_STD_PAL_DK)
462b285192aSMauro Carvalho Chehab 						bpl_local = 352 << 1;
463b285192aSMauro Carvalho Chehab 					else
464b285192aSMauro Carvalho Chehab 						bpl_local = dev->channels[
465b285192aSMauro Carvalho Chehab 							channel_opened].
466b285192aSMauro Carvalho Chehab 							cif_width << 1;
467b285192aSMauro Carvalho Chehab 				}
468b285192aSMauro Carvalho Chehab 			}
469b285192aSMauro Carvalho Chehab 		}
470b285192aSMauro Carvalho Chehab 
471b285192aSMauro Carvalho Chehab 		switch (buf->vb.field) {
472b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_TOP:
473b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
474b285192aSMauro Carvalho Chehab 					    dma->sglist, 0, UNSET,
475b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height);
476b285192aSMauro Carvalho Chehab 			break;
477b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_BOTTOM:
478b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
479b285192aSMauro Carvalho Chehab 					    dma->sglist, UNSET, 0,
480b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height);
481b285192aSMauro Carvalho Chehab 			break;
482b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_INTERLACED:
483b285192aSMauro Carvalho Chehab 			/* All other formats are top field first */
484b285192aSMauro Carvalho Chehab 			line0_offset = 0;
485b285192aSMauro Carvalho Chehab 			dprintk(1, "top field first\n");
486b285192aSMauro Carvalho Chehab 
487b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
488b285192aSMauro Carvalho Chehab 					    dma->sglist, line0_offset,
489b285192aSMauro Carvalho Chehab 					    bpl_local, bpl_local, bpl_local,
490b285192aSMauro Carvalho Chehab 					    buf->vb.height >> 1);
491b285192aSMauro Carvalho Chehab 			break;
492b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_SEQ_TB:
493b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
494b285192aSMauro Carvalho Chehab 					    dma->sglist,
495b285192aSMauro Carvalho Chehab 					    0, buf->bpl * (buf->vb.height >> 1),
496b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height >> 1);
497b285192aSMauro Carvalho Chehab 			break;
498b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_SEQ_BT:
499b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
500b285192aSMauro Carvalho Chehab 					    dma->sglist,
501b285192aSMauro Carvalho Chehab 					    buf->bpl * (buf->vb.height >> 1), 0,
502b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height >> 1);
503b285192aSMauro Carvalho Chehab 			break;
504b285192aSMauro Carvalho Chehab 		default:
505b285192aSMauro Carvalho Chehab 			BUG();
506b285192aSMauro Carvalho Chehab 		}
507b285192aSMauro Carvalho Chehab 	}
508b285192aSMauro Carvalho Chehab 
509b285192aSMauro Carvalho Chehab 	dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
510b285192aSMauro Carvalho Chehab 		buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
511b285192aSMauro Carvalho Chehab 		fh->fmt->name, (unsigned long)buf->risc.dma);
512b285192aSMauro Carvalho Chehab 
513b285192aSMauro Carvalho Chehab 	buf->vb.state = VIDEOBUF_PREPARED;
514b285192aSMauro Carvalho Chehab 
515b285192aSMauro Carvalho Chehab 	return 0;
516b285192aSMauro Carvalho Chehab 
517b285192aSMauro Carvalho Chehab fail:
518b285192aSMauro Carvalho Chehab 	cx25821_free_buffer(q, buf);
519b285192aSMauro Carvalho Chehab 	return rc;
520b285192aSMauro Carvalho Chehab }
521b285192aSMauro Carvalho Chehab 
52295c232a2SHans Verkuil static void cx25821_buffer_release(struct videobuf_queue *q,
523b285192aSMauro Carvalho Chehab 			    struct videobuf_buffer *vb)
524b285192aSMauro Carvalho Chehab {
525b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
526b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
527b285192aSMauro Carvalho Chehab 
528b285192aSMauro Carvalho Chehab 	cx25821_free_buffer(q, buf);
529b285192aSMauro Carvalho Chehab }
530b285192aSMauro Carvalho Chehab 
53195c232a2SHans Verkuil static struct videobuf_queue *get_queue(struct cx25821_fh *fh)
532b285192aSMauro Carvalho Chehab {
533b285192aSMauro Carvalho Chehab 	switch (fh->type) {
534b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
535b285192aSMauro Carvalho Chehab 		return &fh->vidq;
536b285192aSMauro Carvalho Chehab 	default:
537b285192aSMauro Carvalho Chehab 		BUG();
538b285192aSMauro Carvalho Chehab 		return NULL;
539b285192aSMauro Carvalho Chehab 	}
540b285192aSMauro Carvalho Chehab }
541b285192aSMauro Carvalho Chehab 
54295c232a2SHans Verkuil static int cx25821_get_resource(struct cx25821_fh *fh, int resource)
543b285192aSMauro Carvalho Chehab {
544b285192aSMauro Carvalho Chehab 	switch (fh->type) {
545b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
546b285192aSMauro Carvalho Chehab 		return resource;
547b285192aSMauro Carvalho Chehab 	default:
548b285192aSMauro Carvalho Chehab 		BUG();
549b285192aSMauro Carvalho Chehab 		return 0;
550b285192aSMauro Carvalho Chehab 	}
551b285192aSMauro Carvalho Chehab }
552b285192aSMauro Carvalho Chehab 
55395c232a2SHans Verkuil static int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
554b285192aSMauro Carvalho Chehab {
555b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
556b285192aSMauro Carvalho Chehab 
557b285192aSMauro Carvalho Chehab 	return videobuf_mmap_mapper(get_queue(fh), vma);
558b285192aSMauro Carvalho Chehab }
559b285192aSMauro Carvalho Chehab 
560b285192aSMauro Carvalho Chehab 
561b285192aSMauro Carvalho Chehab static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
562b285192aSMauro Carvalho Chehab {
563b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
564b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
565b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *prev;
566b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = vq->priv_data;
567b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
568b285192aSMauro Carvalho Chehab 	struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq;
569b285192aSMauro Carvalho Chehab 
570b285192aSMauro Carvalho Chehab 	/* add jump to stopper */
571b285192aSMauro Carvalho Chehab 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
572b285192aSMauro Carvalho Chehab 	buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
573b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0);      /* bits 63-32 */
574b285192aSMauro Carvalho Chehab 
575b285192aSMauro Carvalho Chehab 	dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
576b285192aSMauro Carvalho Chehab 
577b285192aSMauro Carvalho Chehab 	if (!list_empty(&q->queued)) {
578b285192aSMauro Carvalho Chehab 		list_add_tail(&buf->vb.queue, &q->queued);
579b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_QUEUED;
580b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
581b285192aSMauro Carvalho Chehab 				buf->vb.i);
582b285192aSMauro Carvalho Chehab 
583b285192aSMauro Carvalho Chehab 	} else if (list_empty(&q->active)) {
584b285192aSMauro Carvalho Chehab 		list_add_tail(&buf->vb.queue, &q->active);
585b285192aSMauro Carvalho Chehab 		cx25821_start_video_dma(dev, q, buf,
586b285192aSMauro Carvalho Chehab 				dev->channels[fh->channel_id].sram_channels);
587b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_ACTIVE;
588b285192aSMauro Carvalho Chehab 		buf->count = q->count++;
589b285192aSMauro Carvalho Chehab 		mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
590b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
591b285192aSMauro Carvalho Chehab 				buf, buf->vb.i, buf->count, q->count);
592b285192aSMauro Carvalho Chehab 	} else {
593b285192aSMauro Carvalho Chehab 		prev = list_entry(q->active.prev, struct cx25821_buffer,
594b285192aSMauro Carvalho Chehab 				vb.queue);
595b285192aSMauro Carvalho Chehab 		if (prev->vb.width == buf->vb.width
596b285192aSMauro Carvalho Chehab 		   && prev->vb.height == buf->vb.height
597b285192aSMauro Carvalho Chehab 		   && prev->fmt == buf->fmt) {
598b285192aSMauro Carvalho Chehab 			list_add_tail(&buf->vb.queue, &q->active);
599b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
600b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
601b285192aSMauro Carvalho Chehab 			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
602b285192aSMauro Carvalho Chehab 
603b285192aSMauro Carvalho Chehab 			/* 64 bit bits 63-32 */
604b285192aSMauro Carvalho Chehab 			prev->risc.jmp[2] = cpu_to_le32(0);
605b285192aSMauro Carvalho Chehab 			dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
606b285192aSMauro Carvalho Chehab 					buf, buf->vb.i, buf->count);
607b285192aSMauro Carvalho Chehab 
608b285192aSMauro Carvalho Chehab 		} else {
609b285192aSMauro Carvalho Chehab 			list_add_tail(&buf->vb.queue, &q->queued);
610b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_QUEUED;
611b285192aSMauro Carvalho Chehab 			dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
612b285192aSMauro Carvalho Chehab 					buf->vb.i);
613b285192aSMauro Carvalho Chehab 		}
614b285192aSMauro Carvalho Chehab 	}
615b285192aSMauro Carvalho Chehab 
616b285192aSMauro Carvalho Chehab 	if (list_empty(&q->active))
617b285192aSMauro Carvalho Chehab 		dprintk(2, "active queue empty!\n");
618b285192aSMauro Carvalho Chehab }
619b285192aSMauro Carvalho Chehab 
620b285192aSMauro Carvalho Chehab static struct videobuf_queue_ops cx25821_video_qops = {
621b285192aSMauro Carvalho Chehab 	.buf_setup = cx25821_buffer_setup,
622b285192aSMauro Carvalho Chehab 	.buf_prepare = cx25821_buffer_prepare,
623b285192aSMauro Carvalho Chehab 	.buf_queue = buffer_queue,
624b285192aSMauro Carvalho Chehab 	.buf_release = cx25821_buffer_release,
625b285192aSMauro Carvalho Chehab };
626b285192aSMauro Carvalho Chehab 
627b285192aSMauro Carvalho Chehab static int video_open(struct file *file)
628b285192aSMauro Carvalho Chehab {
629b285192aSMauro Carvalho Chehab 	struct video_device *vdev = video_devdata(file);
630a8f35ce3SHans Verkuil 	struct cx25821_dev *dev = video_drvdata(file);
631b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh;
632b285192aSMauro Carvalho Chehab 	enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
633b285192aSMauro Carvalho Chehab 	u32 pix_format;
634a8f35ce3SHans Verkuil 	int ch_id;
635b285192aSMauro Carvalho Chehab 
636b285192aSMauro Carvalho Chehab 	dprintk(1, "open dev=%s type=%s\n", video_device_node_name(vdev),
637b285192aSMauro Carvalho Chehab 			v4l2_type_names[type]);
638b285192aSMauro Carvalho Chehab 
639a8f35ce3SHans Verkuil 	for (ch_id = 0; ch_id < MAX_VID_CHANNEL_NUM - 1; ch_id++)
640467870caSHans Verkuil 		if (&dev->channels[ch_id].vdev == vdev)
641a8f35ce3SHans Verkuil 			break;
642a8f35ce3SHans Verkuil 
643a8f35ce3SHans Verkuil 	/* Can't happen */
644a8f35ce3SHans Verkuil 	if (ch_id >= MAX_VID_CHANNEL_NUM - 1)
645a8f35ce3SHans Verkuil 		return -ENODEV;
646a8f35ce3SHans Verkuil 
647b285192aSMauro Carvalho Chehab 	/* allocate + initialize per filehandle data */
648b285192aSMauro Carvalho Chehab 	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
649b285192aSMauro Carvalho Chehab 	if (NULL == fh)
650b285192aSMauro Carvalho Chehab 		return -ENOMEM;
651b285192aSMauro Carvalho Chehab 
652b285192aSMauro Carvalho Chehab 	file->private_data = fh;
653b285192aSMauro Carvalho Chehab 	fh->dev = dev;
654b285192aSMauro Carvalho Chehab 	fh->type = type;
655b285192aSMauro Carvalho Chehab 	fh->width = 720;
656b285192aSMauro Carvalho Chehab 	fh->channel_id = ch_id;
657b285192aSMauro Carvalho Chehab 
658b285192aSMauro Carvalho Chehab 	if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
659b285192aSMauro Carvalho Chehab 		fh->height = 576;
660b285192aSMauro Carvalho Chehab 	else
661b285192aSMauro Carvalho Chehab 		fh->height = 480;
662b285192aSMauro Carvalho Chehab 
663b285192aSMauro Carvalho Chehab 	dev->channel_opened = fh->channel_id;
664b285192aSMauro Carvalho Chehab 	if (dev->channels[ch_id].pixel_formats == PIXEL_FRMT_411)
665b285192aSMauro Carvalho Chehab 		pix_format = V4L2_PIX_FMT_Y41P;
666b285192aSMauro Carvalho Chehab 	else
667b285192aSMauro Carvalho Chehab 		pix_format = V4L2_PIX_FMT_YUYV;
668b285192aSMauro Carvalho Chehab 	fh->fmt = cx25821_format_by_fourcc(pix_format);
669b285192aSMauro Carvalho Chehab 
670b285192aSMauro Carvalho Chehab 	v4l2_prio_open(&dev->channels[ch_id].prio, &fh->prio);
671b285192aSMauro Carvalho Chehab 
672b285192aSMauro Carvalho Chehab 	videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops, &dev->pci->dev,
673b285192aSMauro Carvalho Chehab 			&dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
674b285192aSMauro Carvalho Chehab 			V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
675b285192aSMauro Carvalho Chehab 			fh, NULL);
676b285192aSMauro Carvalho Chehab 
677b285192aSMauro Carvalho Chehab 	dprintk(1, "post videobuf_queue_init()\n");
678b285192aSMauro Carvalho Chehab 
679b285192aSMauro Carvalho Chehab 	return 0;
680b285192aSMauro Carvalho Chehab }
681b285192aSMauro Carvalho Chehab 
682b285192aSMauro Carvalho Chehab static ssize_t video_read(struct file *file, char __user * data, size_t count,
683b285192aSMauro Carvalho Chehab 			 loff_t *ppos)
684b285192aSMauro Carvalho Chehab {
685b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
686b285192aSMauro Carvalho Chehab 
687b285192aSMauro Carvalho Chehab 	switch (fh->type) {
688b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
689b285192aSMauro Carvalho Chehab 		if (cx25821_res_locked(fh, RESOURCE_VIDEO0))
690b285192aSMauro Carvalho Chehab 			return -EBUSY;
691b285192aSMauro Carvalho Chehab 
692b285192aSMauro Carvalho Chehab 		return videobuf_read_one(&fh->vidq, data, count, ppos,
693b285192aSMauro Carvalho Chehab 					file->f_flags & O_NONBLOCK);
694b285192aSMauro Carvalho Chehab 
695b285192aSMauro Carvalho Chehab 	default:
696b285192aSMauro Carvalho Chehab 		BUG();
697b285192aSMauro Carvalho Chehab 		return 0;
698b285192aSMauro Carvalho Chehab 	}
699b285192aSMauro Carvalho Chehab }
700b285192aSMauro Carvalho Chehab 
701b285192aSMauro Carvalho Chehab static unsigned int video_poll(struct file *file,
702b285192aSMauro Carvalho Chehab 			      struct poll_table_struct *wait)
703b285192aSMauro Carvalho Chehab {
704b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
705b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf;
706b285192aSMauro Carvalho Chehab 
707b285192aSMauro Carvalho Chehab 	if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
708b285192aSMauro Carvalho Chehab 		/* streaming capture */
709b285192aSMauro Carvalho Chehab 		if (list_empty(&fh->vidq.stream))
710b285192aSMauro Carvalho Chehab 			return POLLERR;
711b285192aSMauro Carvalho Chehab 		buf = list_entry(fh->vidq.stream.next,
712b285192aSMauro Carvalho Chehab 				struct cx25821_buffer, vb.stream);
713b285192aSMauro Carvalho Chehab 	} else {
714b285192aSMauro Carvalho Chehab 		/* read() capture */
715b285192aSMauro Carvalho Chehab 		buf = (struct cx25821_buffer *)fh->vidq.read_buf;
716b285192aSMauro Carvalho Chehab 		if (NULL == buf)
717b285192aSMauro Carvalho Chehab 			return POLLERR;
718b285192aSMauro Carvalho Chehab 	}
719b285192aSMauro Carvalho Chehab 
720b285192aSMauro Carvalho Chehab 	poll_wait(file, &buf->vb.done, wait);
721b285192aSMauro Carvalho Chehab 	if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
722b285192aSMauro Carvalho Chehab 		if (buf->vb.state == VIDEOBUF_DONE) {
723b285192aSMauro Carvalho Chehab 			struct cx25821_dev *dev = fh->dev;
724b285192aSMauro Carvalho Chehab 
725b285192aSMauro Carvalho Chehab 			if (dev && dev->channels[fh->channel_id]
726b285192aSMauro Carvalho Chehab 					.use_cif_resolution) {
727b285192aSMauro Carvalho Chehab 				u8 cam_id = *((char *)buf->vb.baddr + 3);
728b285192aSMauro Carvalho Chehab 				memcpy((char *)buf->vb.baddr,
729b285192aSMauro Carvalho Chehab 				      (char *)buf->vb.baddr + (fh->width * 2),
730b285192aSMauro Carvalho Chehab 				      (fh->width * 2));
731b285192aSMauro Carvalho Chehab 				*((char *)buf->vb.baddr + 3) = cam_id;
732b285192aSMauro Carvalho Chehab 			}
733b285192aSMauro Carvalho Chehab 		}
734b285192aSMauro Carvalho Chehab 
735b285192aSMauro Carvalho Chehab 		return POLLIN | POLLRDNORM;
736b285192aSMauro Carvalho Chehab 	}
737b285192aSMauro Carvalho Chehab 
738b285192aSMauro Carvalho Chehab 	return 0;
739b285192aSMauro Carvalho Chehab }
740b285192aSMauro Carvalho Chehab 
741b285192aSMauro Carvalho Chehab static int video_release(struct file *file)
742b285192aSMauro Carvalho Chehab {
743b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
744b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
745bfef0d35SHans Verkuil 	const struct sram_channel *sram_ch =
746bfef0d35SHans Verkuil 		dev->channels[0].sram_channels;
747b285192aSMauro Carvalho Chehab 
748b285192aSMauro Carvalho Chehab 	/* stop the risc engine and fifo */
749bfef0d35SHans Verkuil 	cx_write(sram_ch->dma_ctl, 0); /* FIFO and RISC disable */
750b285192aSMauro Carvalho Chehab 
751b285192aSMauro Carvalho Chehab 	/* stop video capture */
752b285192aSMauro Carvalho Chehab 	if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
753b285192aSMauro Carvalho Chehab 		videobuf_queue_cancel(&fh->vidq);
754b285192aSMauro Carvalho Chehab 		cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
755b285192aSMauro Carvalho Chehab 	}
756b285192aSMauro Carvalho Chehab 
757b285192aSMauro Carvalho Chehab 	if (fh->vidq.read_buf) {
758b285192aSMauro Carvalho Chehab 		cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
759b285192aSMauro Carvalho Chehab 		kfree(fh->vidq.read_buf);
760b285192aSMauro Carvalho Chehab 	}
761b285192aSMauro Carvalho Chehab 
762b285192aSMauro Carvalho Chehab 	videobuf_mmap_free(&fh->vidq);
763b285192aSMauro Carvalho Chehab 
764b285192aSMauro Carvalho Chehab 	v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
765b285192aSMauro Carvalho Chehab 	file->private_data = NULL;
766b285192aSMauro Carvalho Chehab 	kfree(fh);
767b285192aSMauro Carvalho Chehab 
768b285192aSMauro Carvalho Chehab 	return 0;
769b285192aSMauro Carvalho Chehab }
770b285192aSMauro Carvalho Chehab 
77195c232a2SHans Verkuil /* VIDEO IOCTLS */
77295c232a2SHans Verkuil static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
77395c232a2SHans Verkuil 				 struct v4l2_format *f)
77495c232a2SHans Verkuil {
77595c232a2SHans Verkuil 	struct cx25821_fh *fh = priv;
77695c232a2SHans Verkuil 
77795c232a2SHans Verkuil 	f->fmt.pix.width = fh->width;
77895c232a2SHans Verkuil 	f->fmt.pix.height = fh->height;
77995c232a2SHans Verkuil 	f->fmt.pix.field = fh->vidq.field;
78095c232a2SHans Verkuil 	f->fmt.pix.pixelformat = fh->fmt->fourcc;
78195c232a2SHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
78295c232a2SHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
78395c232a2SHans Verkuil 
78495c232a2SHans Verkuil 	return 0;
78595c232a2SHans Verkuil }
78695c232a2SHans Verkuil 
78795c232a2SHans Verkuil static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
78895c232a2SHans Verkuil 				   struct v4l2_format *f)
78995c232a2SHans Verkuil {
79095c232a2SHans Verkuil 	const struct cx25821_fmt *fmt;
79195c232a2SHans Verkuil 	enum v4l2_field field;
79295c232a2SHans Verkuil 	unsigned int maxw, maxh;
79395c232a2SHans Verkuil 
79495c232a2SHans Verkuil 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
79595c232a2SHans Verkuil 	if (NULL == fmt)
79695c232a2SHans Verkuil 		return -EINVAL;
79795c232a2SHans Verkuil 
79895c232a2SHans Verkuil 	field = f->fmt.pix.field;
79995c232a2SHans Verkuil 	maxw = 720;
80095c232a2SHans Verkuil 	maxh = 576;
80195c232a2SHans Verkuil 
80295c232a2SHans Verkuil 	if (V4L2_FIELD_ANY == field) {
80395c232a2SHans Verkuil 		if (f->fmt.pix.height > maxh / 2)
80495c232a2SHans Verkuil 			field = V4L2_FIELD_INTERLACED;
80595c232a2SHans Verkuil 		else
80695c232a2SHans Verkuil 			field = V4L2_FIELD_TOP;
80795c232a2SHans Verkuil 	}
80895c232a2SHans Verkuil 
80995c232a2SHans Verkuil 	switch (field) {
81095c232a2SHans Verkuil 	case V4L2_FIELD_TOP:
81195c232a2SHans Verkuil 	case V4L2_FIELD_BOTTOM:
81295c232a2SHans Verkuil 		maxh = maxh / 2;
81395c232a2SHans Verkuil 		break;
81495c232a2SHans Verkuil 	case V4L2_FIELD_INTERLACED:
81595c232a2SHans Verkuil 		break;
81695c232a2SHans Verkuil 	default:
81795c232a2SHans Verkuil 		return -EINVAL;
81895c232a2SHans Verkuil 	}
81995c232a2SHans Verkuil 
82095c232a2SHans Verkuil 	f->fmt.pix.field = field;
82195c232a2SHans Verkuil 	if (f->fmt.pix.height < 32)
82295c232a2SHans Verkuil 		f->fmt.pix.height = 32;
82395c232a2SHans Verkuil 	if (f->fmt.pix.height > maxh)
82495c232a2SHans Verkuil 		f->fmt.pix.height = maxh;
82595c232a2SHans Verkuil 	if (f->fmt.pix.width < 48)
82695c232a2SHans Verkuil 		f->fmt.pix.width = 48;
82795c232a2SHans Verkuil 	if (f->fmt.pix.width > maxw)
82895c232a2SHans Verkuil 		f->fmt.pix.width = maxw;
82995c232a2SHans Verkuil 	f->fmt.pix.width &= ~0x03;
83095c232a2SHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
83195c232a2SHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
83295c232a2SHans Verkuil 
83395c232a2SHans Verkuil 	return 0;
83495c232a2SHans Verkuil }
835b285192aSMauro Carvalho Chehab static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
836b285192aSMauro Carvalho Chehab {
837b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
838b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
839b285192aSMauro Carvalho Chehab 
840b285192aSMauro Carvalho Chehab 	if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
841b285192aSMauro Carvalho Chehab 		return -EINVAL;
842b285192aSMauro Carvalho Chehab 
843b285192aSMauro Carvalho Chehab 	if (unlikely(i != fh->type))
844b285192aSMauro Carvalho Chehab 		return -EINVAL;
845b285192aSMauro Carvalho Chehab 
846b285192aSMauro Carvalho Chehab 	if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh,
847b285192aSMauro Carvalho Chehab 						RESOURCE_VIDEO0))))
848b285192aSMauro Carvalho Chehab 		return -EBUSY;
849b285192aSMauro Carvalho Chehab 
850b285192aSMauro Carvalho Chehab 	return videobuf_streamon(get_queue(fh));
851b285192aSMauro Carvalho Chehab }
852b285192aSMauro Carvalho Chehab 
853b285192aSMauro Carvalho Chehab static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
854b285192aSMauro Carvalho Chehab {
855b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
856b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
857b285192aSMauro Carvalho Chehab 	int err, res;
858b285192aSMauro Carvalho Chehab 
859b285192aSMauro Carvalho Chehab 	if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
860b285192aSMauro Carvalho Chehab 		return -EINVAL;
861b285192aSMauro Carvalho Chehab 	if (i != fh->type)
862b285192aSMauro Carvalho Chehab 		return -EINVAL;
863b285192aSMauro Carvalho Chehab 
864b285192aSMauro Carvalho Chehab 	res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
865b285192aSMauro Carvalho Chehab 	err = videobuf_streamoff(get_queue(fh));
866b285192aSMauro Carvalho Chehab 	if (err < 0)
867b285192aSMauro Carvalho Chehab 		return err;
868b285192aSMauro Carvalho Chehab 	cx25821_res_free(dev, fh, res);
869b285192aSMauro Carvalho Chehab 	return 0;
870b285192aSMauro Carvalho Chehab }
871b285192aSMauro Carvalho Chehab 
87295c232a2SHans Verkuil static int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
87395c232a2SHans Verkuil {
87495c232a2SHans Verkuil 	if (tvnorm == V4L2_STD_PAL_BG) {
87595c232a2SHans Verkuil 		if (width == 352 || width == 720)
87695c232a2SHans Verkuil 			return 1;
87795c232a2SHans Verkuil 		else
87895c232a2SHans Verkuil 			return 0;
87995c232a2SHans Verkuil 	}
88095c232a2SHans Verkuil 
88195c232a2SHans Verkuil 	if (tvnorm == V4L2_STD_NTSC_M) {
88295c232a2SHans Verkuil 		if (width == 320 || width == 352 || width == 720)
88395c232a2SHans Verkuil 			return 1;
88495c232a2SHans Verkuil 		else
88595c232a2SHans Verkuil 			return 0;
88695c232a2SHans Verkuil 	}
88795c232a2SHans Verkuil 	return 0;
88895c232a2SHans Verkuil }
88995c232a2SHans Verkuil 
89095c232a2SHans Verkuil static int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
89195c232a2SHans Verkuil {
89295c232a2SHans Verkuil 	if (tvnorm == V4L2_STD_PAL_BG) {
89395c232a2SHans Verkuil 		if (height == 576 || height == 288)
89495c232a2SHans Verkuil 			return 1;
89595c232a2SHans Verkuil 		else
89695c232a2SHans Verkuil 			return 0;
89795c232a2SHans Verkuil 	}
89895c232a2SHans Verkuil 
89995c232a2SHans Verkuil 	if (tvnorm == V4L2_STD_NTSC_M) {
90095c232a2SHans Verkuil 		if (height == 480 || height == 240)
90195c232a2SHans Verkuil 			return 1;
90295c232a2SHans Verkuil 		else
90395c232a2SHans Verkuil 			return 0;
90495c232a2SHans Verkuil 	}
90595c232a2SHans Verkuil 
90695c232a2SHans Verkuil 	return 0;
90795c232a2SHans Verkuil }
90895c232a2SHans Verkuil 
909b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
910b285192aSMauro Carvalho Chehab 				struct v4l2_format *f)
911b285192aSMauro Carvalho Chehab {
912b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
913b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
914b285192aSMauro Carvalho Chehab 	struct v4l2_mbus_framefmt mbus_fmt;
915b285192aSMauro Carvalho Chehab 	int err;
916b285192aSMauro Carvalho Chehab 	int pix_format = PIXEL_FRMT_422;
917b285192aSMauro Carvalho Chehab 
918b285192aSMauro Carvalho Chehab 	if (fh) {
919b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
920b285192aSMauro Carvalho Chehab 				      fh->prio);
921b285192aSMauro Carvalho Chehab 		if (0 != err)
922b285192aSMauro Carvalho Chehab 			return err;
923b285192aSMauro Carvalho Chehab 	}
924b285192aSMauro Carvalho Chehab 
925b285192aSMauro Carvalho Chehab 	dprintk(2, "%s()\n", __func__);
926b285192aSMauro Carvalho Chehab 	err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
927b285192aSMauro Carvalho Chehab 
928b285192aSMauro Carvalho Chehab 	if (0 != err)
929b285192aSMauro Carvalho Chehab 		return err;
930b285192aSMauro Carvalho Chehab 
931b285192aSMauro Carvalho Chehab 	fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
932b285192aSMauro Carvalho Chehab 	fh->vidq.field = f->fmt.pix.field;
933b285192aSMauro Carvalho Chehab 
934b285192aSMauro Carvalho Chehab 	/* check if width and height is valid based on set standard */
935b285192aSMauro Carvalho Chehab 	if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
936b285192aSMauro Carvalho Chehab 		fh->width = f->fmt.pix.width;
937b285192aSMauro Carvalho Chehab 
938b285192aSMauro Carvalho Chehab 	if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
939b285192aSMauro Carvalho Chehab 		fh->height = f->fmt.pix.height;
940b285192aSMauro Carvalho Chehab 
941b285192aSMauro Carvalho Chehab 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
942b285192aSMauro Carvalho Chehab 		pix_format = PIXEL_FRMT_411;
943b285192aSMauro Carvalho Chehab 	else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
944b285192aSMauro Carvalho Chehab 		pix_format = PIXEL_FRMT_422;
945b285192aSMauro Carvalho Chehab 	else
946b285192aSMauro Carvalho Chehab 		return -EINVAL;
947b285192aSMauro Carvalho Chehab 
948b285192aSMauro Carvalho Chehab 	cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
949b285192aSMauro Carvalho Chehab 
950b285192aSMauro Carvalho Chehab 	/* check if cif resolution */
951b285192aSMauro Carvalho Chehab 	if (fh->width == 320 || fh->width == 352)
952b285192aSMauro Carvalho Chehab 		dev->channels[fh->channel_id].use_cif_resolution = 1;
953b285192aSMauro Carvalho Chehab 	else
954b285192aSMauro Carvalho Chehab 		dev->channels[fh->channel_id].use_cif_resolution = 0;
955b285192aSMauro Carvalho Chehab 
956b285192aSMauro Carvalho Chehab 	dev->channels[fh->channel_id].cif_width = fh->width;
957b285192aSMauro Carvalho Chehab 	medusa_set_resolution(dev, fh->width, SRAM_CH00);
958b285192aSMauro Carvalho Chehab 
959b285192aSMauro Carvalho Chehab 	dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__, fh->width,
960b285192aSMauro Carvalho Chehab 		fh->height, fh->vidq.field);
961b285192aSMauro Carvalho Chehab 	v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
962b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
963b285192aSMauro Carvalho Chehab 
964b285192aSMauro Carvalho Chehab 	return 0;
965b285192aSMauro Carvalho Chehab }
966b285192aSMauro Carvalho Chehab 
967b285192aSMauro Carvalho Chehab static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
968b285192aSMauro Carvalho Chehab {
969b285192aSMauro Carvalho Chehab 	int ret_val = 0;
970b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
971b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
972b285192aSMauro Carvalho Chehab 
973b285192aSMauro Carvalho Chehab 	ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
974b285192aSMauro Carvalho Chehab 
975b285192aSMauro Carvalho Chehab 	p->sequence = dev->channels[fh->channel_id].vidq.count;
976b285192aSMauro Carvalho Chehab 
977b285192aSMauro Carvalho Chehab 	return ret_val;
978b285192aSMauro Carvalho Chehab }
979b285192aSMauro Carvalho Chehab 
980b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
981b285192aSMauro Carvalho Chehab {
982b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
983b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
984bfef0d35SHans Verkuil 	const struct sram_channel *sram_ch =
9853dd473caSHans Verkuil 		dev->channels[fh->channel_id].sram_channels;
986b285192aSMauro Carvalho Chehab 	u32 tmp = 0;
987b285192aSMauro Carvalho Chehab 
988b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, core, log_status);
989b285192aSMauro Carvalho Chehab 	tmp = cx_read(sram_ch->dma_ctl);
990b285192aSMauro Carvalho Chehab 	pr_info("Video input 0 is %s\n",
991b285192aSMauro Carvalho Chehab 		(tmp & 0x11) ? "streaming" : "stopped");
992b285192aSMauro Carvalho Chehab 	return 0;
993b285192aSMauro Carvalho Chehab }
994b285192aSMauro Carvalho Chehab 
995b285192aSMauro Carvalho Chehab 
99695c232a2SHans Verkuil static int cx25821_vidioc_querycap(struct file *file, void *priv,
997b285192aSMauro Carvalho Chehab 			    struct v4l2_capability *cap)
998b285192aSMauro Carvalho Chehab {
999b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
10003dd473caSHans Verkuil 	struct cx25821_fh *fh = priv;
10013dd473caSHans Verkuil 	const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
10023dd473caSHans Verkuil 			V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
10033dd473caSHans Verkuil 	const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
1004b285192aSMauro Carvalho Chehab 
1005b285192aSMauro Carvalho Chehab 	strcpy(cap->driver, "cx25821");
1006b285192aSMauro Carvalho Chehab 	strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
1007b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
10083dd473caSHans Verkuil 	if (fh->channel_id >= VID_CHANNEL_NUM)
10093dd473caSHans Verkuil 		cap->device_caps = cap_output;
10103dd473caSHans Verkuil 	else
10113dd473caSHans Verkuil 		cap->device_caps = cap_input;
10123dd473caSHans Verkuil 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1013b285192aSMauro Carvalho Chehab 	return 0;
1014b285192aSMauro Carvalho Chehab }
1015b285192aSMauro Carvalho Chehab 
101695c232a2SHans Verkuil static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1017b285192aSMauro Carvalho Chehab 			    struct v4l2_fmtdesc *f)
1018b285192aSMauro Carvalho Chehab {
1019b285192aSMauro Carvalho Chehab 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
1020b285192aSMauro Carvalho Chehab 		return -EINVAL;
1021b285192aSMauro Carvalho Chehab 
1022b285192aSMauro Carvalho Chehab 	strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1023b285192aSMauro Carvalho Chehab 	f->pixelformat = formats[f->index].fourcc;
1024b285192aSMauro Carvalho Chehab 
1025b285192aSMauro Carvalho Chehab 	return 0;
1026b285192aSMauro Carvalho Chehab }
1027b285192aSMauro Carvalho Chehab 
102895c232a2SHans Verkuil static int cx25821_vidioc_reqbufs(struct file *file, void *priv,
1029b285192aSMauro Carvalho Chehab 			   struct v4l2_requestbuffers *p)
1030b285192aSMauro Carvalho Chehab {
1031b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1032b285192aSMauro Carvalho Chehab 	return videobuf_reqbufs(get_queue(fh), p);
1033b285192aSMauro Carvalho Chehab }
1034b285192aSMauro Carvalho Chehab 
103595c232a2SHans Verkuil static int cx25821_vidioc_querybuf(struct file *file, void *priv,
1036b285192aSMauro Carvalho Chehab 			    struct v4l2_buffer *p)
1037b285192aSMauro Carvalho Chehab {
1038b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1039b285192aSMauro Carvalho Chehab 	return videobuf_querybuf(get_queue(fh), p);
1040b285192aSMauro Carvalho Chehab }
1041b285192aSMauro Carvalho Chehab 
104295c232a2SHans Verkuil static int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1043b285192aSMauro Carvalho Chehab {
1044b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1045b285192aSMauro Carvalho Chehab 	return videobuf_qbuf(get_queue(fh), p);
1046b285192aSMauro Carvalho Chehab }
1047b285192aSMauro Carvalho Chehab 
104895c232a2SHans Verkuil static int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
1049b285192aSMauro Carvalho Chehab {
1050b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1051b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = f;
1052b285192aSMauro Carvalho Chehab 
1053b285192aSMauro Carvalho Chehab 	*p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
1054b285192aSMauro Carvalho Chehab 
1055b285192aSMauro Carvalho Chehab 	return 0;
1056b285192aSMauro Carvalho Chehab }
1057b285192aSMauro Carvalho Chehab 
105895c232a2SHans Verkuil static int cx25821_vidioc_s_priority(struct file *file, void *f,
1059b285192aSMauro Carvalho Chehab 			      enum v4l2_priority prio)
1060b285192aSMauro Carvalho Chehab {
1061b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = f;
1062b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1063b285192aSMauro Carvalho Chehab 
1064b285192aSMauro Carvalho Chehab 	return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
1065b285192aSMauro Carvalho Chehab 			prio);
1066b285192aSMauro Carvalho Chehab }
1067b285192aSMauro Carvalho Chehab 
106895c232a2SHans Verkuil static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
106918c73af6SHans Verkuil {
107018c73af6SHans Verkuil 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
107118c73af6SHans Verkuil 
107218c73af6SHans Verkuil 	*tvnorms = dev->tvnorm;
107318c73af6SHans Verkuil 	return 0;
107418c73af6SHans Verkuil }
107518c73af6SHans Verkuil 
1076314527acSHans Verkuil int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
1077b285192aSMauro Carvalho Chehab {
1078b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1079b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1080b285192aSMauro Carvalho Chehab 	int err;
1081b285192aSMauro Carvalho Chehab 
1082b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1083b285192aSMauro Carvalho Chehab 
1084b285192aSMauro Carvalho Chehab 	if (fh) {
1085b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1086b285192aSMauro Carvalho Chehab 				      fh->prio);
1087b285192aSMauro Carvalho Chehab 		if (0 != err)
1088b285192aSMauro Carvalho Chehab 			return err;
1089b285192aSMauro Carvalho Chehab 	}
1090b285192aSMauro Carvalho Chehab 
1091314527acSHans Verkuil 	if (dev->tvnorm == tvnorms)
1092b285192aSMauro Carvalho Chehab 		return 0;
1093b285192aSMauro Carvalho Chehab 
1094b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
1095314527acSHans Verkuil 	cx25821_set_tvnorm(dev, tvnorms);
1096b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
1097b285192aSMauro Carvalho Chehab 
1098b285192aSMauro Carvalho Chehab 	medusa_set_videostandard(dev);
1099b285192aSMauro Carvalho Chehab 
1100b285192aSMauro Carvalho Chehab 	return 0;
1101b285192aSMauro Carvalho Chehab }
1102b285192aSMauro Carvalho Chehab 
110395c232a2SHans Verkuil static int cx25821_vidioc_enum_input(struct file *file, void *priv,
110495c232a2SHans Verkuil 			      struct v4l2_input *i)
1105b285192aSMauro Carvalho Chehab {
1106b285192aSMauro Carvalho Chehab 	static const char * const iname[] = {
1107b285192aSMauro Carvalho Chehab 		[CX25821_VMUX_COMPOSITE] = "Composite",
1108b285192aSMauro Carvalho Chehab 		[CX25821_VMUX_SVIDEO] = "S-Video",
1109b285192aSMauro Carvalho Chehab 		[CX25821_VMUX_DEBUG] = "for debug only",
1110b285192aSMauro Carvalho Chehab 	};
111195c232a2SHans Verkuil 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1112b285192aSMauro Carvalho Chehab 	unsigned int n;
1113b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1114b285192aSMauro Carvalho Chehab 
1115b285192aSMauro Carvalho Chehab 	n = i->index;
111695c232a2SHans Verkuil 	if (n >= CX25821_NR_INPUT)
1117b285192aSMauro Carvalho Chehab 		return -EINVAL;
1118b285192aSMauro Carvalho Chehab 
1119b285192aSMauro Carvalho Chehab 	if (0 == INPUT(n)->type)
1120b285192aSMauro Carvalho Chehab 		return -EINVAL;
1121b285192aSMauro Carvalho Chehab 
1122b285192aSMauro Carvalho Chehab 	i->type = V4L2_INPUT_TYPE_CAMERA;
1123b285192aSMauro Carvalho Chehab 	strcpy(i->name, iname[INPUT(n)->type]);
1124b285192aSMauro Carvalho Chehab 
1125b285192aSMauro Carvalho Chehab 	i->std = CX25821_NORMS;
1126b285192aSMauro Carvalho Chehab 	return 0;
1127b285192aSMauro Carvalho Chehab }
1128b285192aSMauro Carvalho Chehab 
112995c232a2SHans Verkuil static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1130b285192aSMauro Carvalho Chehab {
1131b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1132b285192aSMauro Carvalho Chehab 
1133b285192aSMauro Carvalho Chehab 	*i = dev->input;
1134b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(): returns %d\n", __func__, *i);
1135b285192aSMauro Carvalho Chehab 	return 0;
1136b285192aSMauro Carvalho Chehab }
1137b285192aSMauro Carvalho Chehab 
113895c232a2SHans Verkuil static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1139b285192aSMauro Carvalho Chehab {
1140b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1141b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1142b285192aSMauro Carvalho Chehab 	int err;
1143b285192aSMauro Carvalho Chehab 
1144b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(%d)\n", __func__, i);
1145b285192aSMauro Carvalho Chehab 
1146b285192aSMauro Carvalho Chehab 	if (fh) {
1147b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1148b285192aSMauro Carvalho Chehab 				      fh->prio);
1149b285192aSMauro Carvalho Chehab 		if (0 != err)
1150b285192aSMauro Carvalho Chehab 			return err;
1151b285192aSMauro Carvalho Chehab 	}
1152b285192aSMauro Carvalho Chehab 
11536b1dce25SHans Verkuil 	if (i >= CX25821_NR_INPUT || INPUT(i)->type == 0)
1154b285192aSMauro Carvalho Chehab 		return -EINVAL;
1155b285192aSMauro Carvalho Chehab 
1156b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
1157b285192aSMauro Carvalho Chehab 	cx25821_video_mux(dev, i);
1158b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
1159b285192aSMauro Carvalho Chehab 	return 0;
1160b285192aSMauro Carvalho Chehab }
1161b285192aSMauro Carvalho Chehab 
1162b285192aSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
1163b285192aSMauro Carvalho Chehab int cx25821_vidioc_g_register(struct file *file, void *fh,
1164b285192aSMauro Carvalho Chehab 		      struct v4l2_dbg_register *reg)
1165b285192aSMauro Carvalho Chehab {
1166b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1167b285192aSMauro Carvalho Chehab 
1168b285192aSMauro Carvalho Chehab 	if (!v4l2_chip_match_host(&reg->match))
1169b285192aSMauro Carvalho Chehab 		return -EINVAL;
1170b285192aSMauro Carvalho Chehab 
1171b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, core, g_register, reg);
1172b285192aSMauro Carvalho Chehab 
1173b285192aSMauro Carvalho Chehab 	return 0;
1174b285192aSMauro Carvalho Chehab }
1175b285192aSMauro Carvalho Chehab 
1176b285192aSMauro Carvalho Chehab int cx25821_vidioc_s_register(struct file *file, void *fh,
1177977ba3b1SHans Verkuil 		      const struct v4l2_dbg_register *reg)
1178b285192aSMauro Carvalho Chehab {
1179b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1180b285192aSMauro Carvalho Chehab 
1181b285192aSMauro Carvalho Chehab 	if (!v4l2_chip_match_host(&reg->match))
1182b285192aSMauro Carvalho Chehab 		return -EINVAL;
1183b285192aSMauro Carvalho Chehab 
1184b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, core, s_register, reg);
1185b285192aSMauro Carvalho Chehab 
1186b285192aSMauro Carvalho Chehab 	return 0;
1187b285192aSMauro Carvalho Chehab }
1188b285192aSMauro Carvalho Chehab 
1189b285192aSMauro Carvalho Chehab #endif
1190b285192aSMauro Carvalho Chehab 
1191*f8d7ee70SHans Verkuil static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
1192b285192aSMauro Carvalho Chehab {
1193*f8d7ee70SHans Verkuil 	struct cx25821_channel *chan =
1194*f8d7ee70SHans Verkuil 		container_of(ctrl->handler, struct cx25821_channel, hdl);
1195*f8d7ee70SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
1196b285192aSMauro Carvalho Chehab 
1197*f8d7ee70SHans Verkuil 	switch (ctrl->id) {
1198b285192aSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
1199*f8d7ee70SHans Verkuil 		medusa_set_brightness(dev, ctrl->val, chan->id);
1200b285192aSMauro Carvalho Chehab 		break;
1201b285192aSMauro Carvalho Chehab 	case V4L2_CID_HUE:
1202*f8d7ee70SHans Verkuil 		medusa_set_hue(dev, ctrl->val, chan->id);
1203b285192aSMauro Carvalho Chehab 		break;
1204b285192aSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
1205*f8d7ee70SHans Verkuil 		medusa_set_contrast(dev, ctrl->val, chan->id);
1206b285192aSMauro Carvalho Chehab 		break;
1207b285192aSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
1208*f8d7ee70SHans Verkuil 		medusa_set_saturation(dev, ctrl->val, chan->id);
1209b285192aSMauro Carvalho Chehab 		break;
1210b285192aSMauro Carvalho Chehab 	default:
1211*f8d7ee70SHans Verkuil 		return -EINVAL;
1212b285192aSMauro Carvalho Chehab 	}
1213*f8d7ee70SHans Verkuil 	return 0;
1214b285192aSMauro Carvalho Chehab }
1215b285192aSMauro Carvalho Chehab 
1216b285192aSMauro Carvalho Chehab static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
1217b285192aSMauro Carvalho Chehab 				 unsigned long arg)
1218b285192aSMauro Carvalho Chehab {
1219b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1220b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1221b285192aSMauro Carvalho Chehab 	int command = 0;
1222b285192aSMauro Carvalho Chehab 	struct upstream_user_struct *data_from_user;
1223b285192aSMauro Carvalho Chehab 
1224b285192aSMauro Carvalho Chehab 	data_from_user = (struct upstream_user_struct *)arg;
1225b285192aSMauro Carvalho Chehab 
1226b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1227b285192aSMauro Carvalho Chehab 		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1228b285192aSMauro Carvalho Chehab 		return 0;
1229b285192aSMauro Carvalho Chehab 	}
1230b285192aSMauro Carvalho Chehab 
1231b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1232b285192aSMauro Carvalho Chehab 
1233b285192aSMauro Carvalho Chehab 	if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1234b285192aSMauro Carvalho Chehab 		return 0;
1235b285192aSMauro Carvalho Chehab 
1236b285192aSMauro Carvalho Chehab 	dev->input_filename = data_from_user->input_filename;
1237b285192aSMauro Carvalho Chehab 	dev->input_audiofilename = data_from_user->input_filename;
1238b285192aSMauro Carvalho Chehab 	dev->vid_stdname = data_from_user->vid_stdname;
1239b285192aSMauro Carvalho Chehab 	dev->pixel_format = data_from_user->pixel_format;
1240b285192aSMauro Carvalho Chehab 	dev->channel_select = data_from_user->channel_select;
1241b285192aSMauro Carvalho Chehab 	dev->command = data_from_user->command;
1242b285192aSMauro Carvalho Chehab 
1243b285192aSMauro Carvalho Chehab 	switch (command) {
1244b285192aSMauro Carvalho Chehab 	case UPSTREAM_START_VIDEO:
1245b285192aSMauro Carvalho Chehab 		cx25821_start_upstream_video_ch1(dev, data_from_user);
1246b285192aSMauro Carvalho Chehab 		break;
1247b285192aSMauro Carvalho Chehab 
1248b285192aSMauro Carvalho Chehab 	case UPSTREAM_STOP_VIDEO:
1249b285192aSMauro Carvalho Chehab 		cx25821_stop_upstream_video_ch1(dev);
1250b285192aSMauro Carvalho Chehab 		break;
1251b285192aSMauro Carvalho Chehab 	}
1252b285192aSMauro Carvalho Chehab 
1253b285192aSMauro Carvalho Chehab 	return 0;
1254b285192aSMauro Carvalho Chehab }
1255b285192aSMauro Carvalho Chehab 
1256b285192aSMauro Carvalho Chehab static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
1257b285192aSMauro Carvalho Chehab 				  unsigned long arg)
1258b285192aSMauro Carvalho Chehab {
1259b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1260b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1261b285192aSMauro Carvalho Chehab 	int command = 0;
1262b285192aSMauro Carvalho Chehab 	struct upstream_user_struct *data_from_user;
1263b285192aSMauro Carvalho Chehab 
1264b285192aSMauro Carvalho Chehab 	data_from_user = (struct upstream_user_struct *)arg;
1265b285192aSMauro Carvalho Chehab 
1266b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1267b285192aSMauro Carvalho Chehab 		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1268b285192aSMauro Carvalho Chehab 		return 0;
1269b285192aSMauro Carvalho Chehab 	}
1270b285192aSMauro Carvalho Chehab 
1271b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1272b285192aSMauro Carvalho Chehab 
1273b285192aSMauro Carvalho Chehab 	if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1274b285192aSMauro Carvalho Chehab 		return 0;
1275b285192aSMauro Carvalho Chehab 
1276b285192aSMauro Carvalho Chehab 	dev->input_filename_ch2 = data_from_user->input_filename;
1277b285192aSMauro Carvalho Chehab 	dev->input_audiofilename = data_from_user->input_filename;
1278b285192aSMauro Carvalho Chehab 	dev->vid_stdname_ch2 = data_from_user->vid_stdname;
1279b285192aSMauro Carvalho Chehab 	dev->pixel_format_ch2 = data_from_user->pixel_format;
1280b285192aSMauro Carvalho Chehab 	dev->channel_select_ch2 = data_from_user->channel_select;
1281b285192aSMauro Carvalho Chehab 	dev->command_ch2 = data_from_user->command;
1282b285192aSMauro Carvalho Chehab 
1283b285192aSMauro Carvalho Chehab 	switch (command) {
1284b285192aSMauro Carvalho Chehab 	case UPSTREAM_START_VIDEO:
1285b285192aSMauro Carvalho Chehab 		cx25821_start_upstream_video_ch2(dev, data_from_user);
1286b285192aSMauro Carvalho Chehab 		break;
1287b285192aSMauro Carvalho Chehab 
1288b285192aSMauro Carvalho Chehab 	case UPSTREAM_STOP_VIDEO:
1289b285192aSMauro Carvalho Chehab 		cx25821_stop_upstream_video_ch2(dev);
1290b285192aSMauro Carvalho Chehab 		break;
1291b285192aSMauro Carvalho Chehab 	}
1292b285192aSMauro Carvalho Chehab 
1293b285192aSMauro Carvalho Chehab 	return 0;
1294b285192aSMauro Carvalho Chehab }
1295b285192aSMauro Carvalho Chehab 
1296b285192aSMauro Carvalho Chehab static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
1297b285192aSMauro Carvalho Chehab 				  unsigned long arg)
1298b285192aSMauro Carvalho Chehab {
1299b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1300b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1301b285192aSMauro Carvalho Chehab 	int command = 0;
1302b285192aSMauro Carvalho Chehab 	struct upstream_user_struct *data_from_user;
1303b285192aSMauro Carvalho Chehab 
1304b285192aSMauro Carvalho Chehab 	data_from_user = (struct upstream_user_struct *)arg;
1305b285192aSMauro Carvalho Chehab 
1306b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1307b285192aSMauro Carvalho Chehab 		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1308b285192aSMauro Carvalho Chehab 		return 0;
1309b285192aSMauro Carvalho Chehab 	}
1310b285192aSMauro Carvalho Chehab 
1311b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1312b285192aSMauro Carvalho Chehab 
1313b285192aSMauro Carvalho Chehab 	if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
1314b285192aSMauro Carvalho Chehab 		return 0;
1315b285192aSMauro Carvalho Chehab 
1316b285192aSMauro Carvalho Chehab 	dev->input_filename = data_from_user->input_filename;
1317b285192aSMauro Carvalho Chehab 	dev->input_audiofilename = data_from_user->input_filename;
1318b285192aSMauro Carvalho Chehab 	dev->vid_stdname = data_from_user->vid_stdname;
1319b285192aSMauro Carvalho Chehab 	dev->pixel_format = data_from_user->pixel_format;
1320b285192aSMauro Carvalho Chehab 	dev->channel_select = data_from_user->channel_select;
1321b285192aSMauro Carvalho Chehab 	dev->command = data_from_user->command;
1322b285192aSMauro Carvalho Chehab 
1323b285192aSMauro Carvalho Chehab 	switch (command) {
1324b285192aSMauro Carvalho Chehab 	case UPSTREAM_START_AUDIO:
1325b285192aSMauro Carvalho Chehab 		cx25821_start_upstream_audio(dev, data_from_user);
1326b285192aSMauro Carvalho Chehab 		break;
1327b285192aSMauro Carvalho Chehab 
1328b285192aSMauro Carvalho Chehab 	case UPSTREAM_STOP_AUDIO:
1329b285192aSMauro Carvalho Chehab 		cx25821_stop_upstream_audio(dev);
1330b285192aSMauro Carvalho Chehab 		break;
1331b285192aSMauro Carvalho Chehab 	}
1332b285192aSMauro Carvalho Chehab 
1333b285192aSMauro Carvalho Chehab 	return 0;
1334b285192aSMauro Carvalho Chehab }
1335b285192aSMauro Carvalho Chehab 
1336b285192aSMauro Carvalho Chehab static long video_ioctl_set(struct file *file, unsigned int cmd,
1337b285192aSMauro Carvalho Chehab 			   unsigned long arg)
1338b285192aSMauro Carvalho Chehab {
1339b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1340b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1341b285192aSMauro Carvalho Chehab 	struct downstream_user_struct *data_from_user;
1342b285192aSMauro Carvalho Chehab 	int command;
1343b285192aSMauro Carvalho Chehab 	int width = 720;
1344b285192aSMauro Carvalho Chehab 	int selected_channel = 0;
1345b285192aSMauro Carvalho Chehab 	int pix_format = 0;
1346b285192aSMauro Carvalho Chehab 	int i = 0;
1347b285192aSMauro Carvalho Chehab 	int cif_enable = 0;
1348b285192aSMauro Carvalho Chehab 	int cif_width = 0;
1349b285192aSMauro Carvalho Chehab 
1350b285192aSMauro Carvalho Chehab 	data_from_user = (struct downstream_user_struct *)arg;
1351b285192aSMauro Carvalho Chehab 
1352b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1353b285192aSMauro Carvalho Chehab 		pr_err("%s(): User data is INVALID. Returning\n", __func__);
1354b285192aSMauro Carvalho Chehab 		return 0;
1355b285192aSMauro Carvalho Chehab 	}
1356b285192aSMauro Carvalho Chehab 
1357b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1358b285192aSMauro Carvalho Chehab 
1359b285192aSMauro Carvalho Chehab 	if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
1360b285192aSMauro Carvalho Chehab 	   && command != ENABLE_CIF_RESOLUTION && command != REG_READ
1361b285192aSMauro Carvalho Chehab 	   && command != REG_WRITE && command != MEDUSA_READ
1362b285192aSMauro Carvalho Chehab 	   && command != MEDUSA_WRITE) {
1363b285192aSMauro Carvalho Chehab 		return 0;
1364b285192aSMauro Carvalho Chehab 	}
1365b285192aSMauro Carvalho Chehab 
1366b285192aSMauro Carvalho Chehab 	switch (command) {
1367b285192aSMauro Carvalho Chehab 	case SET_VIDEO_STD:
1368b285192aSMauro Carvalho Chehab 		if (!strcmp(data_from_user->vid_stdname, "PAL"))
1369b285192aSMauro Carvalho Chehab 			dev->tvnorm = V4L2_STD_PAL_BG;
1370b285192aSMauro Carvalho Chehab 		else
1371b285192aSMauro Carvalho Chehab 			dev->tvnorm = V4L2_STD_NTSC_M;
1372b285192aSMauro Carvalho Chehab 		medusa_set_videostandard(dev);
1373b285192aSMauro Carvalho Chehab 		break;
1374b285192aSMauro Carvalho Chehab 
1375b285192aSMauro Carvalho Chehab 	case SET_PIXEL_FORMAT:
1376b285192aSMauro Carvalho Chehab 		selected_channel = data_from_user->decoder_select;
1377b285192aSMauro Carvalho Chehab 		pix_format = data_from_user->pixel_format;
1378b285192aSMauro Carvalho Chehab 
1379b285192aSMauro Carvalho Chehab 		if (!(selected_channel <= 7 && selected_channel >= 0)) {
1380b285192aSMauro Carvalho Chehab 			selected_channel -= 4;
1381b285192aSMauro Carvalho Chehab 			selected_channel = selected_channel % 8;
1382b285192aSMauro Carvalho Chehab 		}
1383b285192aSMauro Carvalho Chehab 
1384b285192aSMauro Carvalho Chehab 		if (selected_channel >= 0)
1385b285192aSMauro Carvalho Chehab 			cx25821_set_pixel_format(dev, selected_channel,
1386b285192aSMauro Carvalho Chehab 						pix_format);
1387b285192aSMauro Carvalho Chehab 
1388b285192aSMauro Carvalho Chehab 		break;
1389b285192aSMauro Carvalho Chehab 
1390b285192aSMauro Carvalho Chehab 	case ENABLE_CIF_RESOLUTION:
1391b285192aSMauro Carvalho Chehab 		selected_channel = data_from_user->decoder_select;
1392b285192aSMauro Carvalho Chehab 		cif_enable = data_from_user->cif_resolution_enable;
1393b285192aSMauro Carvalho Chehab 		cif_width = data_from_user->cif_width;
1394b285192aSMauro Carvalho Chehab 
1395b285192aSMauro Carvalho Chehab 		if (cif_enable) {
1396b285192aSMauro Carvalho Chehab 			if (dev->tvnorm & V4L2_STD_PAL_BG
1397b285192aSMauro Carvalho Chehab 			    || dev->tvnorm & V4L2_STD_PAL_DK) {
1398b285192aSMauro Carvalho Chehab 				width = 352;
1399b285192aSMauro Carvalho Chehab 			} else {
1400b285192aSMauro Carvalho Chehab 				width = cif_width;
1401b285192aSMauro Carvalho Chehab 				if (cif_width != 320 && cif_width != 352)
1402b285192aSMauro Carvalho Chehab 					width = 320;
1403b285192aSMauro Carvalho Chehab 			}
1404b285192aSMauro Carvalho Chehab 		}
1405b285192aSMauro Carvalho Chehab 
1406b285192aSMauro Carvalho Chehab 		if (!(selected_channel <= 7 && selected_channel >= 0)) {
1407b285192aSMauro Carvalho Chehab 			selected_channel -= 4;
1408b285192aSMauro Carvalho Chehab 			selected_channel = selected_channel % 8;
1409b285192aSMauro Carvalho Chehab 		}
1410b285192aSMauro Carvalho Chehab 
1411b285192aSMauro Carvalho Chehab 		if (selected_channel <= 7 && selected_channel >= 0) {
1412b285192aSMauro Carvalho Chehab 			dev->channels[selected_channel].use_cif_resolution =
1413b285192aSMauro Carvalho Chehab 				cif_enable;
1414b285192aSMauro Carvalho Chehab 			dev->channels[selected_channel].cif_width = width;
1415b285192aSMauro Carvalho Chehab 		} else {
1416b285192aSMauro Carvalho Chehab 			for (i = 0; i < VID_CHANNEL_NUM; i++) {
1417b285192aSMauro Carvalho Chehab 				dev->channels[i].use_cif_resolution =
1418b285192aSMauro Carvalho Chehab 					cif_enable;
1419b285192aSMauro Carvalho Chehab 				dev->channels[i].cif_width = width;
1420b285192aSMauro Carvalho Chehab 			}
1421b285192aSMauro Carvalho Chehab 		}
1422b285192aSMauro Carvalho Chehab 
1423b285192aSMauro Carvalho Chehab 		medusa_set_resolution(dev, width, selected_channel);
1424b285192aSMauro Carvalho Chehab 		break;
1425b285192aSMauro Carvalho Chehab 	case REG_READ:
1426b285192aSMauro Carvalho Chehab 		data_from_user->reg_data = cx_read(data_from_user->reg_address);
1427b285192aSMauro Carvalho Chehab 		break;
1428b285192aSMauro Carvalho Chehab 	case REG_WRITE:
1429b285192aSMauro Carvalho Chehab 		cx_write(data_from_user->reg_address, data_from_user->reg_data);
1430b285192aSMauro Carvalho Chehab 		break;
1431b285192aSMauro Carvalho Chehab 	case MEDUSA_READ:
1432b285192aSMauro Carvalho Chehab 		cx25821_i2c_read(&dev->i2c_bus[0],
1433b285192aSMauro Carvalho Chehab 					 (u16) data_from_user->reg_address,
1434b285192aSMauro Carvalho Chehab 					 &data_from_user->reg_data);
1435b285192aSMauro Carvalho Chehab 		break;
1436b285192aSMauro Carvalho Chehab 	case MEDUSA_WRITE:
1437b285192aSMauro Carvalho Chehab 		cx25821_i2c_write(&dev->i2c_bus[0],
1438b285192aSMauro Carvalho Chehab 				  (u16) data_from_user->reg_address,
1439b285192aSMauro Carvalho Chehab 				  data_from_user->reg_data);
1440b285192aSMauro Carvalho Chehab 		break;
1441b285192aSMauro Carvalho Chehab 	}
1442b285192aSMauro Carvalho Chehab 
1443b285192aSMauro Carvalho Chehab 	return 0;
1444b285192aSMauro Carvalho Chehab }
1445b285192aSMauro Carvalho Chehab 
1446b285192aSMauro Carvalho Chehab static long cx25821_video_ioctl(struct file *file,
1447b285192aSMauro Carvalho Chehab 				unsigned int cmd, unsigned long arg)
1448b285192aSMauro Carvalho Chehab {
1449b285192aSMauro Carvalho Chehab 	int ret = 0;
1450b285192aSMauro Carvalho Chehab 
1451b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1452b285192aSMauro Carvalho Chehab 
1453b285192aSMauro Carvalho Chehab 	/* check to see if it's the video upstream */
1454b285192aSMauro Carvalho Chehab 	if (fh->channel_id == SRAM_CH09) {
1455b285192aSMauro Carvalho Chehab 		ret = video_ioctl_upstream9(file, cmd, arg);
1456b285192aSMauro Carvalho Chehab 		return ret;
1457b285192aSMauro Carvalho Chehab 	} else if (fh->channel_id == SRAM_CH10) {
1458b285192aSMauro Carvalho Chehab 		ret = video_ioctl_upstream10(file, cmd, arg);
1459b285192aSMauro Carvalho Chehab 		return ret;
1460b285192aSMauro Carvalho Chehab 	} else if (fh->channel_id == SRAM_CH11) {
1461b285192aSMauro Carvalho Chehab 		ret = video_ioctl_upstream11(file, cmd, arg);
1462b285192aSMauro Carvalho Chehab 		ret = video_ioctl_set(file, cmd, arg);
1463b285192aSMauro Carvalho Chehab 		return ret;
1464b285192aSMauro Carvalho Chehab 	}
1465b285192aSMauro Carvalho Chehab 
1466b285192aSMauro Carvalho Chehab 	return video_ioctl2(file, cmd, arg);
1467b285192aSMauro Carvalho Chehab }
1468b285192aSMauro Carvalho Chehab 
1469*f8d7ee70SHans Verkuil static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
1470*f8d7ee70SHans Verkuil 	.s_ctrl = cx25821_s_ctrl,
1471*f8d7ee70SHans Verkuil };
1472*f8d7ee70SHans Verkuil 
1473b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops = {
1474b285192aSMauro Carvalho Chehab 	.owner = THIS_MODULE,
1475b285192aSMauro Carvalho Chehab 	.open = video_open,
1476b285192aSMauro Carvalho Chehab 	.release = video_release,
1477b285192aSMauro Carvalho Chehab 	.read = video_read,
1478b285192aSMauro Carvalho Chehab 	.poll = video_poll,
1479b285192aSMauro Carvalho Chehab 	.mmap = cx25821_video_mmap,
1480b285192aSMauro Carvalho Chehab 	.ioctl = cx25821_video_ioctl,
1481b285192aSMauro Carvalho Chehab };
1482b285192aSMauro Carvalho Chehab 
1483b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops = {
1484b285192aSMauro Carvalho Chehab 	.vidioc_querycap = cx25821_vidioc_querycap,
1485b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
1486b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
1487b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
1488b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1489b285192aSMauro Carvalho Chehab 	.vidioc_reqbufs = cx25821_vidioc_reqbufs,
1490b285192aSMauro Carvalho Chehab 	.vidioc_querybuf = cx25821_vidioc_querybuf,
1491b285192aSMauro Carvalho Chehab 	.vidioc_qbuf = cx25821_vidioc_qbuf,
1492b285192aSMauro Carvalho Chehab 	.vidioc_dqbuf = vidioc_dqbuf,
149318c73af6SHans Verkuil 	.vidioc_g_std = cx25821_vidioc_g_std,
1494b285192aSMauro Carvalho Chehab 	.vidioc_s_std = cx25821_vidioc_s_std,
1495b285192aSMauro Carvalho Chehab 	.vidioc_enum_input = cx25821_vidioc_enum_input,
1496b285192aSMauro Carvalho Chehab 	.vidioc_g_input = cx25821_vidioc_g_input,
1497b285192aSMauro Carvalho Chehab 	.vidioc_s_input = cx25821_vidioc_s_input,
1498b285192aSMauro Carvalho Chehab 	.vidioc_streamon = vidioc_streamon,
1499b285192aSMauro Carvalho Chehab 	.vidioc_streamoff = vidioc_streamoff,
1500b285192aSMauro Carvalho Chehab 	.vidioc_log_status = vidioc_log_status,
1501b285192aSMauro Carvalho Chehab 	.vidioc_g_priority = cx25821_vidioc_g_priority,
1502b285192aSMauro Carvalho Chehab 	.vidioc_s_priority = cx25821_vidioc_s_priority,
1503b285192aSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
1504b285192aSMauro Carvalho Chehab 	.vidioc_g_register = cx25821_vidioc_g_register,
1505b285192aSMauro Carvalho Chehab 	.vidioc_s_register = cx25821_vidioc_s_register,
1506b285192aSMauro Carvalho Chehab #endif
1507b285192aSMauro Carvalho Chehab };
1508b285192aSMauro Carvalho Chehab 
1509ffd3c233SHans Verkuil static const struct video_device cx25821_video_device = {
1510ffd3c233SHans Verkuil 	.name = "cx25821-video",
1511b285192aSMauro Carvalho Chehab 	.fops = &video_fops,
1512467870caSHans Verkuil 	.release = video_device_release_empty,
1513ffd3c233SHans Verkuil 	.minor = -1,
1514b285192aSMauro Carvalho Chehab 	.ioctl_ops = &video_ioctl_ops,
1515b285192aSMauro Carvalho Chehab 	.tvnorms = CX25821_NORMS,
1516b285192aSMauro Carvalho Chehab };
1517ffd3c233SHans Verkuil 
1518ffd3c233SHans Verkuil void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
1519ffd3c233SHans Verkuil {
1520ffd3c233SHans Verkuil 	cx_clear(PCI_INT_MSK, 1);
1521ffd3c233SHans Verkuil 
1522467870caSHans Verkuil 	if (video_is_registered(&dev->channels[chan_num].vdev)) {
1523467870caSHans Verkuil 		video_unregister_device(&dev->channels[chan_num].vdev);
1524*f8d7ee70SHans Verkuil 		v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
1525ffd3c233SHans Verkuil 
1526ffd3c233SHans Verkuil 		btcx_riscmem_free(dev->pci,
1527ffd3c233SHans Verkuil 				&dev->channels[chan_num].vidq.stopper);
1528ffd3c233SHans Verkuil 	}
1529ffd3c233SHans Verkuil }
1530ffd3c233SHans Verkuil 
1531ffd3c233SHans Verkuil int cx25821_video_register(struct cx25821_dev *dev)
1532ffd3c233SHans Verkuil {
1533ffd3c233SHans Verkuil 	int err;
1534ffd3c233SHans Verkuil 	int i;
1535ffd3c233SHans Verkuil 
1536ffd3c233SHans Verkuil 	spin_lock_init(&dev->slock);
1537ffd3c233SHans Verkuil 
1538ffd3c233SHans Verkuil 	for (i = 0; i < VID_CHANNEL_NUM; ++i) {
1539467870caSHans Verkuil 		struct video_device *vdev = &dev->channels[i].vdev;
1540*f8d7ee70SHans Verkuil 		struct v4l2_ctrl_handler *hdl = &dev->channels[i].hdl;
1541467870caSHans Verkuil 
1542ffd3c233SHans Verkuil 		if (i == SRAM_CH08) /* audio channel */
1543ffd3c233SHans Verkuil 			continue;
1544ffd3c233SHans Verkuil 
1545*f8d7ee70SHans Verkuil 		v4l2_ctrl_handler_init(hdl, 4);
1546*f8d7ee70SHans Verkuil 		v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1547*f8d7ee70SHans Verkuil 			V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
1548*f8d7ee70SHans Verkuil 		v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1549*f8d7ee70SHans Verkuil 			V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
1550*f8d7ee70SHans Verkuil 		v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1551*f8d7ee70SHans Verkuil 			V4L2_CID_SATURATION, 0, 10000, 1, 5000);
1552*f8d7ee70SHans Verkuil 		v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1553*f8d7ee70SHans Verkuil 			V4L2_CID_HUE, 0, 10000, 1, 5000);
1554*f8d7ee70SHans Verkuil 		if (hdl->error) {
1555*f8d7ee70SHans Verkuil 			err = hdl->error;
1556*f8d7ee70SHans Verkuil 			goto fail_unreg;
1557*f8d7ee70SHans Verkuil 		}
1558ffd3c233SHans Verkuil 
1559ffd3c233SHans Verkuil 		cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
1560ffd3c233SHans Verkuil 			dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
1561ffd3c233SHans Verkuil 
1562ffd3c233SHans Verkuil 		dev->channels[i].sram_channels = &cx25821_sram_channels[i];
1563ffd3c233SHans Verkuil 		dev->channels[i].resources = 0;
1564ffd3c233SHans Verkuil 
1565ffd3c233SHans Verkuil 		cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
1566ffd3c233SHans Verkuil 
1567ffd3c233SHans Verkuil 		INIT_LIST_HEAD(&dev->channels[i].vidq.active);
1568ffd3c233SHans Verkuil 		INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
1569ffd3c233SHans Verkuil 
1570ffd3c233SHans Verkuil 		dev->channels[i].timeout_data.dev = dev;
1571ffd3c233SHans Verkuil 		dev->channels[i].timeout_data.channel =
1572ffd3c233SHans Verkuil 			&cx25821_sram_channels[i];
1573ffd3c233SHans Verkuil 		dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
1574ffd3c233SHans Verkuil 		dev->channels[i].vidq.timeout.data =
1575ffd3c233SHans Verkuil 			(unsigned long)&dev->channels[i].timeout_data;
1576ffd3c233SHans Verkuil 		init_timer(&dev->channels[i].vidq.timeout);
1577ffd3c233SHans Verkuil 
1578ffd3c233SHans Verkuil 		/* register v4l devices */
1579467870caSHans Verkuil 		*vdev = cx25821_video_device;
1580467870caSHans Verkuil 		vdev->v4l2_dev = &dev->v4l2_dev;
1581*f8d7ee70SHans Verkuil 		vdev->ctrl_handler = hdl;
1582467870caSHans Verkuil 		snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
1583467870caSHans Verkuil 		video_set_drvdata(vdev, dev);
1584ffd3c233SHans Verkuil 
1585467870caSHans Verkuil 		err = video_register_device(vdev, VFL_TYPE_GRABBER,
1586467870caSHans Verkuil 					    video_nr[dev->nr]);
1587ffd3c233SHans Verkuil 
1588ffd3c233SHans Verkuil 		if (err < 0)
1589ffd3c233SHans Verkuil 			goto fail_unreg;
1590ffd3c233SHans Verkuil 	}
1591ffd3c233SHans Verkuil 
1592ffd3c233SHans Verkuil 	/* set PCI interrupt */
1593ffd3c233SHans Verkuil 	cx_set(PCI_INT_MSK, 0xff);
1594ffd3c233SHans Verkuil 
1595ffd3c233SHans Verkuil 	/* initial device configuration */
1596ffd3c233SHans Verkuil 	mutex_lock(&dev->lock);
159718c73af6SHans Verkuil 	dev->tvnorm = V4L2_STD_NTSC_M,
1598ffd3c233SHans Verkuil 	cx25821_set_tvnorm(dev, dev->tvnorm);
1599ffd3c233SHans Verkuil 	mutex_unlock(&dev->lock);
1600ffd3c233SHans Verkuil 
1601ffd3c233SHans Verkuil 	return 0;
1602ffd3c233SHans Verkuil 
1603ffd3c233SHans Verkuil fail_unreg:
1604467870caSHans Verkuil 	while (i >= 0)
1605467870caSHans Verkuil 		cx25821_video_unregister(dev, i--);
1606ffd3c233SHans Verkuil 	return err;
1607ffd3c233SHans Verkuil }
1608