xref: /linux/drivers/media/pci/cx25821/cx25821-video.c (revision be178cb4f41f70e29108ce4eb5a8a77a62f1922f)
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 /* resource management */
148*be178cb4SHans Verkuil static int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh,
149b285192aSMauro Carvalho Chehab 		    unsigned int bit)
150b285192aSMauro Carvalho Chehab {
151b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
152b285192aSMauro Carvalho Chehab 	if (fh->resources & bit)
153b285192aSMauro Carvalho Chehab 		/* have it already allocated */
154b285192aSMauro Carvalho Chehab 		return 1;
155b285192aSMauro Carvalho Chehab 
156b285192aSMauro Carvalho Chehab 	/* is it free? */
157b285192aSMauro Carvalho Chehab 	if (dev->channels[fh->channel_id].resources & bit) {
158b285192aSMauro Carvalho Chehab 		/* no, someone else uses it */
159b285192aSMauro Carvalho Chehab 		return 0;
160b285192aSMauro Carvalho Chehab 	}
161b285192aSMauro Carvalho Chehab 	/* it's free, grab it */
162b285192aSMauro Carvalho Chehab 	fh->resources |= bit;
163b285192aSMauro Carvalho Chehab 	dev->channels[fh->channel_id].resources |= bit;
164b285192aSMauro Carvalho Chehab 	dprintk(1, "res: get %d\n", bit);
165b285192aSMauro Carvalho Chehab 	return 1;
166b285192aSMauro Carvalho Chehab }
167b285192aSMauro Carvalho Chehab 
168*be178cb4SHans Verkuil static int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
169b285192aSMauro Carvalho Chehab {
170b285192aSMauro Carvalho Chehab 	return fh->resources & bit;
171b285192aSMauro Carvalho Chehab }
172b285192aSMauro Carvalho Chehab 
173*be178cb4SHans Verkuil static int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
174b285192aSMauro Carvalho Chehab {
175b285192aSMauro Carvalho Chehab 	return fh->dev->channels[fh->channel_id].resources & bit;
176b285192aSMauro Carvalho Chehab }
177b285192aSMauro Carvalho Chehab 
178*be178cb4SHans Verkuil static void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh,
179b285192aSMauro Carvalho Chehab 		      unsigned int bits)
180b285192aSMauro Carvalho Chehab {
181b285192aSMauro Carvalho Chehab 	BUG_ON((fh->resources & bits) != bits);
182b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
183b285192aSMauro Carvalho Chehab 
184b285192aSMauro Carvalho Chehab 	fh->resources &= ~bits;
185b285192aSMauro Carvalho Chehab 	dev->channels[fh->channel_id].resources &= ~bits;
186b285192aSMauro Carvalho Chehab 	dprintk(1, "res: put %d\n", bits);
187b285192aSMauro Carvalho Chehab }
188b285192aSMauro Carvalho Chehab 
18995c232a2SHans Verkuil static int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
190b285192aSMauro Carvalho Chehab {
191b285192aSMauro Carvalho Chehab 	struct v4l2_routing route;
192b285192aSMauro Carvalho Chehab 	memset(&route, 0, sizeof(route));
193b285192aSMauro Carvalho Chehab 
194b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
195b285192aSMauro Carvalho Chehab 		__func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
196b285192aSMauro Carvalho Chehab 		INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
197b285192aSMauro Carvalho Chehab 	dev->input = input;
198b285192aSMauro Carvalho Chehab 
199b285192aSMauro Carvalho Chehab 	route.input = INPUT(input)->vmux;
200b285192aSMauro Carvalho Chehab 
201b285192aSMauro Carvalho Chehab 	/* Tell the internal A/V decoder */
202b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
203b285192aSMauro Carvalho Chehab 
204b285192aSMauro Carvalho Chehab 	return 0;
205b285192aSMauro Carvalho Chehab }
206b285192aSMauro Carvalho Chehab 
207b285192aSMauro Carvalho Chehab int cx25821_start_video_dma(struct cx25821_dev *dev,
208b285192aSMauro Carvalho Chehab 			    struct cx25821_dmaqueue *q,
209b285192aSMauro Carvalho Chehab 			    struct cx25821_buffer *buf,
210bfef0d35SHans Verkuil 			    const struct sram_channel *channel)
211b285192aSMauro Carvalho Chehab {
212b285192aSMauro Carvalho Chehab 	int tmp = 0;
213b285192aSMauro Carvalho Chehab 
214b285192aSMauro Carvalho Chehab 	/* setup fifo + format */
215b285192aSMauro Carvalho Chehab 	cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
216b285192aSMauro Carvalho Chehab 
217b285192aSMauro Carvalho Chehab 	/* reset counter */
218b285192aSMauro Carvalho Chehab 	cx_write(channel->gpcnt_ctl, 3);
219b285192aSMauro Carvalho Chehab 	q->count = 1;
220b285192aSMauro Carvalho Chehab 
221b285192aSMauro Carvalho Chehab 	/* enable irq */
222b285192aSMauro Carvalho Chehab 	cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
223b285192aSMauro Carvalho Chehab 	cx_set(channel->int_msk, 0x11);
224b285192aSMauro Carvalho Chehab 
225b285192aSMauro Carvalho Chehab 	/* start dma */
226b285192aSMauro Carvalho Chehab 	cx_write(channel->dma_ctl, 0x11);	/* FIFO and RISC enable */
227b285192aSMauro Carvalho Chehab 
228b285192aSMauro Carvalho Chehab 	/* make sure upstream setting if any is reversed */
229b285192aSMauro Carvalho Chehab 	tmp = cx_read(VID_CH_MODE_SEL);
230b285192aSMauro Carvalho Chehab 	cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
231b285192aSMauro Carvalho Chehab 
232b285192aSMauro Carvalho Chehab 	return 0;
233b285192aSMauro Carvalho Chehab }
234b285192aSMauro Carvalho Chehab 
235dafc456cSMauro Carvalho Chehab static int cx25821_restart_video_queue(struct cx25821_dev *dev,
236b285192aSMauro Carvalho Chehab 				       struct cx25821_dmaqueue *q,
237bfef0d35SHans Verkuil 				       const struct sram_channel *channel)
238b285192aSMauro Carvalho Chehab {
239b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf, *prev;
240b285192aSMauro Carvalho Chehab 	struct list_head *item;
241b285192aSMauro Carvalho Chehab 
242b285192aSMauro Carvalho Chehab 	if (!list_empty(&q->active)) {
243b285192aSMauro Carvalho Chehab 		buf = list_entry(q->active.next, struct cx25821_buffer,
244b285192aSMauro Carvalho Chehab 				vb.queue);
245b285192aSMauro Carvalho Chehab 
246b285192aSMauro Carvalho Chehab 		cx25821_start_video_dma(dev, q, buf, channel);
247b285192aSMauro Carvalho Chehab 
248b285192aSMauro Carvalho Chehab 		list_for_each(item, &q->active) {
249b285192aSMauro Carvalho Chehab 			buf = list_entry(item, struct cx25821_buffer, vb.queue);
250b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
251b285192aSMauro Carvalho Chehab 		}
252b285192aSMauro Carvalho Chehab 
253b285192aSMauro Carvalho Chehab 		mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
254b285192aSMauro Carvalho Chehab 		return 0;
255b285192aSMauro Carvalho Chehab 	}
256b285192aSMauro Carvalho Chehab 
257b285192aSMauro Carvalho Chehab 	prev = NULL;
258b285192aSMauro Carvalho Chehab 	for (;;) {
259b285192aSMauro Carvalho Chehab 		if (list_empty(&q->queued))
260b285192aSMauro Carvalho Chehab 			return 0;
261b285192aSMauro Carvalho Chehab 
262b285192aSMauro Carvalho Chehab 		buf = list_entry(q->queued.next, struct cx25821_buffer,
263b285192aSMauro Carvalho Chehab 				vb.queue);
264b285192aSMauro Carvalho Chehab 
265b285192aSMauro Carvalho Chehab 		if (NULL == prev) {
266b285192aSMauro Carvalho Chehab 			list_move_tail(&buf->vb.queue, &q->active);
267b285192aSMauro Carvalho Chehab 			cx25821_start_video_dma(dev, q, buf, channel);
268b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
269b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
270b285192aSMauro Carvalho Chehab 			mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
271b285192aSMauro Carvalho Chehab 		} else if (prev->vb.width == buf->vb.width &&
272b285192aSMauro Carvalho Chehab 			   prev->vb.height == buf->vb.height &&
273b285192aSMauro Carvalho Chehab 			   prev->fmt == buf->fmt) {
274b285192aSMauro Carvalho Chehab 			list_move_tail(&buf->vb.queue, &q->active);
275b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
276b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
277b285192aSMauro Carvalho Chehab 			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
278b285192aSMauro Carvalho Chehab 			prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
279b285192aSMauro Carvalho Chehab 		} else {
280b285192aSMauro Carvalho Chehab 			return 0;
281b285192aSMauro Carvalho Chehab 		}
282b285192aSMauro Carvalho Chehab 		prev = buf;
283b285192aSMauro Carvalho Chehab 	}
284b285192aSMauro Carvalho Chehab }
285b285192aSMauro Carvalho Chehab 
286dafc456cSMauro Carvalho Chehab static void cx25821_vid_timeout(unsigned long data)
287b285192aSMauro Carvalho Chehab {
288b285192aSMauro Carvalho Chehab 	struct cx25821_data *timeout_data = (struct cx25821_data *)data;
289b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = timeout_data->dev;
290bfef0d35SHans Verkuil 	const struct sram_channel *channel = timeout_data->channel;
291b285192aSMauro Carvalho Chehab 	struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq;
292b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf;
293b285192aSMauro Carvalho Chehab 	unsigned long flags;
294b285192aSMauro Carvalho Chehab 
295b285192aSMauro Carvalho Chehab 	/* cx25821_sram_channel_dump(dev, channel); */
296b285192aSMauro Carvalho Chehab 	cx_clear(channel->dma_ctl, 0x11);
297b285192aSMauro Carvalho Chehab 
298b285192aSMauro Carvalho Chehab 	spin_lock_irqsave(&dev->slock, flags);
299b285192aSMauro Carvalho Chehab 	while (!list_empty(&q->active)) {
300b285192aSMauro Carvalho Chehab 		buf = list_entry(q->active.next, struct cx25821_buffer,
301b285192aSMauro Carvalho Chehab 				vb.queue);
302b285192aSMauro Carvalho Chehab 		list_del(&buf->vb.queue);
303b285192aSMauro Carvalho Chehab 
304b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_ERROR;
305b285192aSMauro Carvalho Chehab 		wake_up(&buf->vb.done);
306b285192aSMauro Carvalho Chehab 	}
307b285192aSMauro Carvalho Chehab 
308b285192aSMauro Carvalho Chehab 	cx25821_restart_video_queue(dev, q, channel);
309b285192aSMauro Carvalho Chehab 	spin_unlock_irqrestore(&dev->slock, flags);
310b285192aSMauro Carvalho Chehab }
311b285192aSMauro Carvalho Chehab 
312b285192aSMauro Carvalho Chehab int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
313b285192aSMauro Carvalho Chehab {
314b285192aSMauro Carvalho Chehab 	u32 count = 0;
315b285192aSMauro Carvalho Chehab 	int handled = 0;
316b285192aSMauro Carvalho Chehab 	u32 mask;
317bfef0d35SHans Verkuil 	const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
318b285192aSMauro Carvalho Chehab 
319b285192aSMauro Carvalho Chehab 	mask = cx_read(channel->int_msk);
320b285192aSMauro Carvalho Chehab 	if (0 == (status & mask))
321b285192aSMauro Carvalho Chehab 		return handled;
322b285192aSMauro Carvalho Chehab 
323b285192aSMauro Carvalho Chehab 	cx_write(channel->int_stat, status);
324b285192aSMauro Carvalho Chehab 
325b285192aSMauro Carvalho Chehab 	/* risc op code error */
326b285192aSMauro Carvalho Chehab 	if (status & (1 << 16)) {
327b285192aSMauro Carvalho Chehab 		pr_warn("%s, %s: video risc op code error\n",
328b285192aSMauro Carvalho Chehab 			dev->name, channel->name);
329b285192aSMauro Carvalho Chehab 		cx_clear(channel->dma_ctl, 0x11);
330b285192aSMauro Carvalho Chehab 		cx25821_sram_channel_dump(dev, channel);
331b285192aSMauro Carvalho Chehab 	}
332b285192aSMauro Carvalho Chehab 
333b285192aSMauro Carvalho Chehab 	/* risc1 y */
334b285192aSMauro Carvalho Chehab 	if (status & FLD_VID_DST_RISC1) {
335b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
336b285192aSMauro Carvalho Chehab 		count = cx_read(channel->gpcnt);
337b285192aSMauro Carvalho Chehab 		cx25821_video_wakeup(dev, &dev->channels[channel->i].vidq,
338b285192aSMauro Carvalho Chehab 				count);
339b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
340b285192aSMauro Carvalho Chehab 		handled++;
341b285192aSMauro Carvalho Chehab 	}
342b285192aSMauro Carvalho Chehab 
343b285192aSMauro Carvalho Chehab 	/* risc2 y */
344b285192aSMauro Carvalho Chehab 	if (status & 0x10) {
345b285192aSMauro Carvalho Chehab 		dprintk(2, "stopper video\n");
346b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
347b285192aSMauro Carvalho Chehab 		cx25821_restart_video_queue(dev,
348b285192aSMauro Carvalho Chehab 				&dev->channels[channel->i].vidq, channel);
349b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
350b285192aSMauro Carvalho Chehab 		handled++;
351b285192aSMauro Carvalho Chehab 	}
352b285192aSMauro Carvalho Chehab 	return handled;
353b285192aSMauro Carvalho Chehab }
354b285192aSMauro Carvalho Chehab 
35595c232a2SHans Verkuil static int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
356b285192aSMauro Carvalho Chehab 		 unsigned int *size)
357b285192aSMauro Carvalho Chehab {
358b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = q->priv_data;
359b285192aSMauro Carvalho Chehab 
360b285192aSMauro Carvalho Chehab 	*size = fh->fmt->depth * fh->width * fh->height >> 3;
361b285192aSMauro Carvalho Chehab 
362b285192aSMauro Carvalho Chehab 	if (0 == *count)
363b285192aSMauro Carvalho Chehab 		*count = 32;
364b285192aSMauro Carvalho Chehab 
365b285192aSMauro Carvalho Chehab 	if (*size * *count > vid_limit * 1024 * 1024)
366b285192aSMauro Carvalho Chehab 		*count = (vid_limit * 1024 * 1024) / *size;
367b285192aSMauro Carvalho Chehab 
368b285192aSMauro Carvalho Chehab 	return 0;
369b285192aSMauro Carvalho Chehab }
370b285192aSMauro Carvalho Chehab 
37195c232a2SHans Verkuil static int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
372b285192aSMauro Carvalho Chehab 		   enum v4l2_field field)
373b285192aSMauro Carvalho Chehab {
374b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = q->priv_data;
375b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
376b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
377b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
378b285192aSMauro Carvalho Chehab 	int rc, init_buffer = 0;
379b285192aSMauro Carvalho Chehab 	u32 line0_offset;
380b285192aSMauro Carvalho Chehab 	struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
381b285192aSMauro Carvalho Chehab 	int bpl_local = LINE_SIZE_D1;
382b285192aSMauro Carvalho Chehab 	int channel_opened = fh->channel_id;
383b285192aSMauro Carvalho Chehab 
384b285192aSMauro Carvalho Chehab 	BUG_ON(NULL == fh->fmt);
385b285192aSMauro Carvalho Chehab 	if (fh->width < 48 || fh->width > 720 ||
386b285192aSMauro Carvalho Chehab 	    fh->height < 32 || fh->height > 576)
387b285192aSMauro Carvalho Chehab 		return -EINVAL;
388b285192aSMauro Carvalho Chehab 
389b285192aSMauro Carvalho Chehab 	buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
390b285192aSMauro Carvalho Chehab 
391b285192aSMauro Carvalho Chehab 	if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
392b285192aSMauro Carvalho Chehab 		return -EINVAL;
393b285192aSMauro Carvalho Chehab 
394b285192aSMauro Carvalho Chehab 	if (buf->fmt != fh->fmt ||
395b285192aSMauro Carvalho Chehab 	    buf->vb.width != fh->width ||
396b285192aSMauro Carvalho Chehab 	    buf->vb.height != fh->height || buf->vb.field != field) {
397b285192aSMauro Carvalho Chehab 		buf->fmt = fh->fmt;
398b285192aSMauro Carvalho Chehab 		buf->vb.width = fh->width;
399b285192aSMauro Carvalho Chehab 		buf->vb.height = fh->height;
400b285192aSMauro Carvalho Chehab 		buf->vb.field = field;
401b285192aSMauro Carvalho Chehab 		init_buffer = 1;
402b285192aSMauro Carvalho Chehab 	}
403b285192aSMauro Carvalho Chehab 
404b285192aSMauro Carvalho Chehab 	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
405b285192aSMauro Carvalho Chehab 		init_buffer = 1;
406b285192aSMauro Carvalho Chehab 		rc = videobuf_iolock(q, &buf->vb, NULL);
407b285192aSMauro Carvalho Chehab 		if (0 != rc) {
408b285192aSMauro Carvalho Chehab 			printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
409b285192aSMauro Carvalho Chehab 			goto fail;
410b285192aSMauro Carvalho Chehab 		}
411b285192aSMauro Carvalho Chehab 	}
412b285192aSMauro Carvalho Chehab 
413b285192aSMauro Carvalho Chehab 	dprintk(1, "init_buffer=%d\n", init_buffer);
414b285192aSMauro Carvalho Chehab 
415b285192aSMauro Carvalho Chehab 	if (init_buffer) {
416b285192aSMauro Carvalho Chehab 
417b285192aSMauro Carvalho Chehab 		channel_opened = dev->channel_opened;
418b285192aSMauro Carvalho Chehab 		if (channel_opened < 0 || channel_opened > 7)
419b285192aSMauro Carvalho Chehab 			channel_opened = 7;
420b285192aSMauro Carvalho Chehab 
421b285192aSMauro Carvalho Chehab 		if (dev->channels[channel_opened].pixel_formats ==
422b285192aSMauro Carvalho Chehab 				PIXEL_FRMT_411)
423b285192aSMauro Carvalho Chehab 			buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
424b285192aSMauro Carvalho Chehab 		else
425b285192aSMauro Carvalho Chehab 			buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
426b285192aSMauro Carvalho Chehab 
427b285192aSMauro Carvalho Chehab 		if (dev->channels[channel_opened].pixel_formats ==
428b285192aSMauro Carvalho Chehab 				PIXEL_FRMT_411) {
429b285192aSMauro Carvalho Chehab 			bpl_local = buf->bpl;
430b285192aSMauro Carvalho Chehab 		} else {
431b285192aSMauro Carvalho Chehab 			bpl_local = buf->bpl;   /* Default */
432b285192aSMauro Carvalho Chehab 
433b285192aSMauro Carvalho Chehab 			if (channel_opened >= 0 && channel_opened <= 7) {
434b285192aSMauro Carvalho Chehab 				if (dev->channels[channel_opened]
435b285192aSMauro Carvalho Chehab 						.use_cif_resolution) {
436b285192aSMauro Carvalho Chehab 					if (dev->tvnorm & V4L2_STD_PAL_BG ||
437b285192aSMauro Carvalho Chehab 					    dev->tvnorm & V4L2_STD_PAL_DK)
438b285192aSMauro Carvalho Chehab 						bpl_local = 352 << 1;
439b285192aSMauro Carvalho Chehab 					else
440b285192aSMauro Carvalho Chehab 						bpl_local = dev->channels[
441b285192aSMauro Carvalho Chehab 							channel_opened].
442b285192aSMauro Carvalho Chehab 							cif_width << 1;
443b285192aSMauro Carvalho Chehab 				}
444b285192aSMauro Carvalho Chehab 			}
445b285192aSMauro Carvalho Chehab 		}
446b285192aSMauro Carvalho Chehab 
447b285192aSMauro Carvalho Chehab 		switch (buf->vb.field) {
448b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_TOP:
449b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
450b285192aSMauro Carvalho Chehab 					    dma->sglist, 0, UNSET,
451b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height);
452b285192aSMauro Carvalho Chehab 			break;
453b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_BOTTOM:
454b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
455b285192aSMauro Carvalho Chehab 					    dma->sglist, UNSET, 0,
456b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height);
457b285192aSMauro Carvalho Chehab 			break;
458b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_INTERLACED:
459b285192aSMauro Carvalho Chehab 			/* All other formats are top field first */
460b285192aSMauro Carvalho Chehab 			line0_offset = 0;
461b285192aSMauro Carvalho Chehab 			dprintk(1, "top field first\n");
462b285192aSMauro Carvalho Chehab 
463b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
464b285192aSMauro Carvalho Chehab 					    dma->sglist, line0_offset,
465b285192aSMauro Carvalho Chehab 					    bpl_local, bpl_local, bpl_local,
466b285192aSMauro Carvalho Chehab 					    buf->vb.height >> 1);
467b285192aSMauro Carvalho Chehab 			break;
468b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_SEQ_TB:
469b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
470b285192aSMauro Carvalho Chehab 					    dma->sglist,
471b285192aSMauro Carvalho Chehab 					    0, buf->bpl * (buf->vb.height >> 1),
472b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height >> 1);
473b285192aSMauro Carvalho Chehab 			break;
474b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_SEQ_BT:
475b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
476b285192aSMauro Carvalho Chehab 					    dma->sglist,
477b285192aSMauro Carvalho Chehab 					    buf->bpl * (buf->vb.height >> 1), 0,
478b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height >> 1);
479b285192aSMauro Carvalho Chehab 			break;
480b285192aSMauro Carvalho Chehab 		default:
481b285192aSMauro Carvalho Chehab 			BUG();
482b285192aSMauro Carvalho Chehab 		}
483b285192aSMauro Carvalho Chehab 	}
484b285192aSMauro Carvalho Chehab 
485b285192aSMauro Carvalho Chehab 	dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
486b285192aSMauro Carvalho Chehab 		buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
487b285192aSMauro Carvalho Chehab 		fh->fmt->name, (unsigned long)buf->risc.dma);
488b285192aSMauro Carvalho Chehab 
489b285192aSMauro Carvalho Chehab 	buf->vb.state = VIDEOBUF_PREPARED;
490b285192aSMauro Carvalho Chehab 
491b285192aSMauro Carvalho Chehab 	return 0;
492b285192aSMauro Carvalho Chehab 
493b285192aSMauro Carvalho Chehab fail:
494b285192aSMauro Carvalho Chehab 	cx25821_free_buffer(q, buf);
495b285192aSMauro Carvalho Chehab 	return rc;
496b285192aSMauro Carvalho Chehab }
497b285192aSMauro Carvalho Chehab 
49895c232a2SHans Verkuil static void cx25821_buffer_release(struct videobuf_queue *q,
499b285192aSMauro Carvalho Chehab 			    struct videobuf_buffer *vb)
500b285192aSMauro Carvalho Chehab {
501b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
502b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
503b285192aSMauro Carvalho Chehab 
504b285192aSMauro Carvalho Chehab 	cx25821_free_buffer(q, buf);
505b285192aSMauro Carvalho Chehab }
506b285192aSMauro Carvalho Chehab 
50795c232a2SHans Verkuil static struct videobuf_queue *get_queue(struct cx25821_fh *fh)
508b285192aSMauro Carvalho Chehab {
509b285192aSMauro Carvalho Chehab 	switch (fh->type) {
510b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
511b285192aSMauro Carvalho Chehab 		return &fh->vidq;
512b285192aSMauro Carvalho Chehab 	default:
513b285192aSMauro Carvalho Chehab 		BUG();
514b285192aSMauro Carvalho Chehab 		return NULL;
515b285192aSMauro Carvalho Chehab 	}
516b285192aSMauro Carvalho Chehab }
517b285192aSMauro Carvalho Chehab 
51895c232a2SHans Verkuil static int cx25821_get_resource(struct cx25821_fh *fh, int resource)
519b285192aSMauro Carvalho Chehab {
520b285192aSMauro Carvalho Chehab 	switch (fh->type) {
521b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
522b285192aSMauro Carvalho Chehab 		return resource;
523b285192aSMauro Carvalho Chehab 	default:
524b285192aSMauro Carvalho Chehab 		BUG();
525b285192aSMauro Carvalho Chehab 		return 0;
526b285192aSMauro Carvalho Chehab 	}
527b285192aSMauro Carvalho Chehab }
528b285192aSMauro Carvalho Chehab 
52995c232a2SHans Verkuil static int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
530b285192aSMauro Carvalho Chehab {
531b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
532b285192aSMauro Carvalho Chehab 
533b285192aSMauro Carvalho Chehab 	return videobuf_mmap_mapper(get_queue(fh), vma);
534b285192aSMauro Carvalho Chehab }
535b285192aSMauro Carvalho Chehab 
536b285192aSMauro Carvalho Chehab 
537b285192aSMauro Carvalho Chehab static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
538b285192aSMauro Carvalho Chehab {
539b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
540b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
541b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *prev;
542b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = vq->priv_data;
543b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
544b285192aSMauro Carvalho Chehab 	struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq;
545b285192aSMauro Carvalho Chehab 
546b285192aSMauro Carvalho Chehab 	/* add jump to stopper */
547b285192aSMauro Carvalho Chehab 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
548b285192aSMauro Carvalho Chehab 	buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
549b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0);      /* bits 63-32 */
550b285192aSMauro Carvalho Chehab 
551b285192aSMauro Carvalho Chehab 	dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
552b285192aSMauro Carvalho Chehab 
553b285192aSMauro Carvalho Chehab 	if (!list_empty(&q->queued)) {
554b285192aSMauro Carvalho Chehab 		list_add_tail(&buf->vb.queue, &q->queued);
555b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_QUEUED;
556b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
557b285192aSMauro Carvalho Chehab 				buf->vb.i);
558b285192aSMauro Carvalho Chehab 
559b285192aSMauro Carvalho Chehab 	} else if (list_empty(&q->active)) {
560b285192aSMauro Carvalho Chehab 		list_add_tail(&buf->vb.queue, &q->active);
561b285192aSMauro Carvalho Chehab 		cx25821_start_video_dma(dev, q, buf,
562b285192aSMauro Carvalho Chehab 				dev->channels[fh->channel_id].sram_channels);
563b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_ACTIVE;
564b285192aSMauro Carvalho Chehab 		buf->count = q->count++;
565b285192aSMauro Carvalho Chehab 		mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
566b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
567b285192aSMauro Carvalho Chehab 				buf, buf->vb.i, buf->count, q->count);
568b285192aSMauro Carvalho Chehab 	} else {
569b285192aSMauro Carvalho Chehab 		prev = list_entry(q->active.prev, struct cx25821_buffer,
570b285192aSMauro Carvalho Chehab 				vb.queue);
571b285192aSMauro Carvalho Chehab 		if (prev->vb.width == buf->vb.width
572b285192aSMauro Carvalho Chehab 		   && prev->vb.height == buf->vb.height
573b285192aSMauro Carvalho Chehab 		   && prev->fmt == buf->fmt) {
574b285192aSMauro Carvalho Chehab 			list_add_tail(&buf->vb.queue, &q->active);
575b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
576b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
577b285192aSMauro Carvalho Chehab 			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
578b285192aSMauro Carvalho Chehab 
579b285192aSMauro Carvalho Chehab 			/* 64 bit bits 63-32 */
580b285192aSMauro Carvalho Chehab 			prev->risc.jmp[2] = cpu_to_le32(0);
581b285192aSMauro Carvalho Chehab 			dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
582b285192aSMauro Carvalho Chehab 					buf, buf->vb.i, buf->count);
583b285192aSMauro Carvalho Chehab 
584b285192aSMauro Carvalho Chehab 		} else {
585b285192aSMauro Carvalho Chehab 			list_add_tail(&buf->vb.queue, &q->queued);
586b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_QUEUED;
587b285192aSMauro Carvalho Chehab 			dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
588b285192aSMauro Carvalho Chehab 					buf->vb.i);
589b285192aSMauro Carvalho Chehab 		}
590b285192aSMauro Carvalho Chehab 	}
591b285192aSMauro Carvalho Chehab 
592b285192aSMauro Carvalho Chehab 	if (list_empty(&q->active))
593b285192aSMauro Carvalho Chehab 		dprintk(2, "active queue empty!\n");
594b285192aSMauro Carvalho Chehab }
595b285192aSMauro Carvalho Chehab 
596b285192aSMauro Carvalho Chehab static struct videobuf_queue_ops cx25821_video_qops = {
597b285192aSMauro Carvalho Chehab 	.buf_setup = cx25821_buffer_setup,
598b285192aSMauro Carvalho Chehab 	.buf_prepare = cx25821_buffer_prepare,
599b285192aSMauro Carvalho Chehab 	.buf_queue = buffer_queue,
600b285192aSMauro Carvalho Chehab 	.buf_release = cx25821_buffer_release,
601b285192aSMauro Carvalho Chehab };
602b285192aSMauro Carvalho Chehab 
603b285192aSMauro Carvalho Chehab static int video_open(struct file *file)
604b285192aSMauro Carvalho Chehab {
605b285192aSMauro Carvalho Chehab 	struct video_device *vdev = video_devdata(file);
606a8f35ce3SHans Verkuil 	struct cx25821_dev *dev = video_drvdata(file);
607b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh;
608b285192aSMauro Carvalho Chehab 	enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
609b285192aSMauro Carvalho Chehab 	u32 pix_format;
610a8f35ce3SHans Verkuil 	int ch_id;
611b285192aSMauro Carvalho Chehab 
612a8f35ce3SHans Verkuil 	for (ch_id = 0; ch_id < MAX_VID_CHANNEL_NUM - 1; ch_id++)
613467870caSHans Verkuil 		if (&dev->channels[ch_id].vdev == vdev)
614a8f35ce3SHans Verkuil 			break;
615a8f35ce3SHans Verkuil 
616a8f35ce3SHans Verkuil 	/* Can't happen */
617a8f35ce3SHans Verkuil 	if (ch_id >= MAX_VID_CHANNEL_NUM - 1)
618a8f35ce3SHans Verkuil 		return -ENODEV;
619a8f35ce3SHans Verkuil 
620b285192aSMauro Carvalho Chehab 	/* allocate + initialize per filehandle data */
621b285192aSMauro Carvalho Chehab 	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
622b285192aSMauro Carvalho Chehab 	if (NULL == fh)
623b285192aSMauro Carvalho Chehab 		return -ENOMEM;
624b285192aSMauro Carvalho Chehab 
625b285192aSMauro Carvalho Chehab 	file->private_data = fh;
626b285192aSMauro Carvalho Chehab 	fh->dev = dev;
627b285192aSMauro Carvalho Chehab 	fh->type = type;
628b285192aSMauro Carvalho Chehab 	fh->width = 720;
629b285192aSMauro Carvalho Chehab 	fh->channel_id = ch_id;
630b285192aSMauro Carvalho Chehab 
631b285192aSMauro Carvalho Chehab 	if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
632b285192aSMauro Carvalho Chehab 		fh->height = 576;
633b285192aSMauro Carvalho Chehab 	else
634b285192aSMauro Carvalho Chehab 		fh->height = 480;
635b285192aSMauro Carvalho Chehab 
636b285192aSMauro Carvalho Chehab 	dev->channel_opened = fh->channel_id;
637b285192aSMauro Carvalho Chehab 	if (dev->channels[ch_id].pixel_formats == PIXEL_FRMT_411)
638b285192aSMauro Carvalho Chehab 		pix_format = V4L2_PIX_FMT_Y41P;
639b285192aSMauro Carvalho Chehab 	else
640b285192aSMauro Carvalho Chehab 		pix_format = V4L2_PIX_FMT_YUYV;
641b285192aSMauro Carvalho Chehab 	fh->fmt = cx25821_format_by_fourcc(pix_format);
642b285192aSMauro Carvalho Chehab 
643b285192aSMauro Carvalho Chehab 	v4l2_prio_open(&dev->channels[ch_id].prio, &fh->prio);
644b285192aSMauro Carvalho Chehab 
645b285192aSMauro Carvalho Chehab 	videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops, &dev->pci->dev,
646b285192aSMauro Carvalho Chehab 			&dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
647b285192aSMauro Carvalho Chehab 			V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
648*be178cb4SHans Verkuil 			fh, &dev->lock);
649b285192aSMauro Carvalho Chehab 
650b285192aSMauro Carvalho Chehab 	dprintk(1, "post videobuf_queue_init()\n");
651b285192aSMauro Carvalho Chehab 
652b285192aSMauro Carvalho Chehab 	return 0;
653b285192aSMauro Carvalho Chehab }
654b285192aSMauro Carvalho Chehab 
655b285192aSMauro Carvalho Chehab static ssize_t video_read(struct file *file, char __user * data, size_t count,
656b285192aSMauro Carvalho Chehab 			 loff_t *ppos)
657b285192aSMauro Carvalho Chehab {
658b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
659*be178cb4SHans Verkuil 	struct cx25821_dev *dev = fh->dev;
660*be178cb4SHans Verkuil 	int err;
661b285192aSMauro Carvalho Chehab 
662b285192aSMauro Carvalho Chehab 	switch (fh->type) {
663b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
664*be178cb4SHans Verkuil 		if (mutex_lock_interruptible(&dev->lock))
665*be178cb4SHans Verkuil 			return -ERESTARTSYS;
666b285192aSMauro Carvalho Chehab 		if (cx25821_res_locked(fh, RESOURCE_VIDEO0))
667*be178cb4SHans Verkuil 			err = -EBUSY;
668*be178cb4SHans Verkuil 		else
669*be178cb4SHans Verkuil 			err = videobuf_read_one(&fh->vidq, data, count, ppos,
670b285192aSMauro Carvalho Chehab 					file->f_flags & O_NONBLOCK);
671*be178cb4SHans Verkuil 		mutex_unlock(&dev->lock);
672*be178cb4SHans Verkuil 		return err;
673b285192aSMauro Carvalho Chehab 
674b285192aSMauro Carvalho Chehab 	default:
675*be178cb4SHans Verkuil 		return -ENODEV;
676b285192aSMauro Carvalho Chehab 	}
677*be178cb4SHans Verkuil 
678b285192aSMauro Carvalho Chehab }
679b285192aSMauro Carvalho Chehab 
680b285192aSMauro Carvalho Chehab static unsigned int video_poll(struct file *file,
681b285192aSMauro Carvalho Chehab 			      struct poll_table_struct *wait)
682b285192aSMauro Carvalho Chehab {
683b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
684b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf;
685b285192aSMauro Carvalho Chehab 
686b285192aSMauro Carvalho Chehab 	if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
687b285192aSMauro Carvalho Chehab 		/* streaming capture */
688b285192aSMauro Carvalho Chehab 		if (list_empty(&fh->vidq.stream))
689b285192aSMauro Carvalho Chehab 			return POLLERR;
690b285192aSMauro Carvalho Chehab 		buf = list_entry(fh->vidq.stream.next,
691b285192aSMauro Carvalho Chehab 				struct cx25821_buffer, vb.stream);
692b285192aSMauro Carvalho Chehab 	} else {
693b285192aSMauro Carvalho Chehab 		/* read() capture */
694b285192aSMauro Carvalho Chehab 		buf = (struct cx25821_buffer *)fh->vidq.read_buf;
695b285192aSMauro Carvalho Chehab 		if (NULL == buf)
696b285192aSMauro Carvalho Chehab 			return POLLERR;
697b285192aSMauro Carvalho Chehab 	}
698b285192aSMauro Carvalho Chehab 
699b285192aSMauro Carvalho Chehab 	poll_wait(file, &buf->vb.done, wait);
700b285192aSMauro Carvalho Chehab 	if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
701b285192aSMauro Carvalho Chehab 		if (buf->vb.state == VIDEOBUF_DONE) {
702b285192aSMauro Carvalho Chehab 			struct cx25821_dev *dev = fh->dev;
703b285192aSMauro Carvalho Chehab 
704b285192aSMauro Carvalho Chehab 			if (dev && dev->channels[fh->channel_id]
705b285192aSMauro Carvalho Chehab 					.use_cif_resolution) {
706b285192aSMauro Carvalho Chehab 				u8 cam_id = *((char *)buf->vb.baddr + 3);
707b285192aSMauro Carvalho Chehab 				memcpy((char *)buf->vb.baddr,
708b285192aSMauro Carvalho Chehab 				      (char *)buf->vb.baddr + (fh->width * 2),
709b285192aSMauro Carvalho Chehab 				      (fh->width * 2));
710b285192aSMauro Carvalho Chehab 				*((char *)buf->vb.baddr + 3) = cam_id;
711b285192aSMauro Carvalho Chehab 			}
712b285192aSMauro Carvalho Chehab 		}
713b285192aSMauro Carvalho Chehab 
714b285192aSMauro Carvalho Chehab 		return POLLIN | POLLRDNORM;
715b285192aSMauro Carvalho Chehab 	}
716b285192aSMauro Carvalho Chehab 
717b285192aSMauro Carvalho Chehab 	return 0;
718b285192aSMauro Carvalho Chehab }
719b285192aSMauro Carvalho Chehab 
720b285192aSMauro Carvalho Chehab static int video_release(struct file *file)
721b285192aSMauro Carvalho Chehab {
722b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
723b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
724bfef0d35SHans Verkuil 	const struct sram_channel *sram_ch =
725bfef0d35SHans Verkuil 		dev->channels[0].sram_channels;
726b285192aSMauro Carvalho Chehab 
727*be178cb4SHans Verkuil 	mutex_lock(&dev->lock);
728b285192aSMauro Carvalho Chehab 	/* stop the risc engine and fifo */
729bfef0d35SHans Verkuil 	cx_write(sram_ch->dma_ctl, 0); /* FIFO and RISC disable */
730b285192aSMauro Carvalho Chehab 
731b285192aSMauro Carvalho Chehab 	/* stop video capture */
732b285192aSMauro Carvalho Chehab 	if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
733b285192aSMauro Carvalho Chehab 		videobuf_queue_cancel(&fh->vidq);
734b285192aSMauro Carvalho Chehab 		cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
735b285192aSMauro Carvalho Chehab 	}
736*be178cb4SHans Verkuil 	mutex_unlock(&dev->lock);
737b285192aSMauro Carvalho Chehab 
738b285192aSMauro Carvalho Chehab 	if (fh->vidq.read_buf) {
739b285192aSMauro Carvalho Chehab 		cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
740b285192aSMauro Carvalho Chehab 		kfree(fh->vidq.read_buf);
741b285192aSMauro Carvalho Chehab 	}
742b285192aSMauro Carvalho Chehab 
743b285192aSMauro Carvalho Chehab 	videobuf_mmap_free(&fh->vidq);
744b285192aSMauro Carvalho Chehab 
745b285192aSMauro Carvalho Chehab 	v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
746b285192aSMauro Carvalho Chehab 	file->private_data = NULL;
747b285192aSMauro Carvalho Chehab 	kfree(fh);
748b285192aSMauro Carvalho Chehab 
749b285192aSMauro Carvalho Chehab 	return 0;
750b285192aSMauro Carvalho Chehab }
751b285192aSMauro Carvalho Chehab 
75295c232a2SHans Verkuil /* VIDEO IOCTLS */
75395c232a2SHans Verkuil static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
75495c232a2SHans Verkuil 				 struct v4l2_format *f)
75595c232a2SHans Verkuil {
75695c232a2SHans Verkuil 	struct cx25821_fh *fh = priv;
75795c232a2SHans Verkuil 
75895c232a2SHans Verkuil 	f->fmt.pix.width = fh->width;
75995c232a2SHans Verkuil 	f->fmt.pix.height = fh->height;
76095c232a2SHans Verkuil 	f->fmt.pix.field = fh->vidq.field;
76195c232a2SHans Verkuil 	f->fmt.pix.pixelformat = fh->fmt->fourcc;
76295c232a2SHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
76395c232a2SHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
76495c232a2SHans Verkuil 
76595c232a2SHans Verkuil 	return 0;
76695c232a2SHans Verkuil }
76795c232a2SHans Verkuil 
76895c232a2SHans Verkuil static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
76995c232a2SHans Verkuil 				   struct v4l2_format *f)
77095c232a2SHans Verkuil {
77195c232a2SHans Verkuil 	const struct cx25821_fmt *fmt;
77295c232a2SHans Verkuil 	enum v4l2_field field;
77395c232a2SHans Verkuil 	unsigned int maxw, maxh;
77495c232a2SHans Verkuil 
77595c232a2SHans Verkuil 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
77695c232a2SHans Verkuil 	if (NULL == fmt)
77795c232a2SHans Verkuil 		return -EINVAL;
77895c232a2SHans Verkuil 
77995c232a2SHans Verkuil 	field = f->fmt.pix.field;
78095c232a2SHans Verkuil 	maxw = 720;
78195c232a2SHans Verkuil 	maxh = 576;
78295c232a2SHans Verkuil 
78395c232a2SHans Verkuil 	if (V4L2_FIELD_ANY == field) {
78495c232a2SHans Verkuil 		if (f->fmt.pix.height > maxh / 2)
78595c232a2SHans Verkuil 			field = V4L2_FIELD_INTERLACED;
78695c232a2SHans Verkuil 		else
78795c232a2SHans Verkuil 			field = V4L2_FIELD_TOP;
78895c232a2SHans Verkuil 	}
78995c232a2SHans Verkuil 
79095c232a2SHans Verkuil 	switch (field) {
79195c232a2SHans Verkuil 	case V4L2_FIELD_TOP:
79295c232a2SHans Verkuil 	case V4L2_FIELD_BOTTOM:
79395c232a2SHans Verkuil 		maxh = maxh / 2;
79495c232a2SHans Verkuil 		break;
79595c232a2SHans Verkuil 	case V4L2_FIELD_INTERLACED:
79695c232a2SHans Verkuil 		break;
79795c232a2SHans Verkuil 	default:
79895c232a2SHans Verkuil 		return -EINVAL;
79995c232a2SHans Verkuil 	}
80095c232a2SHans Verkuil 
80195c232a2SHans Verkuil 	f->fmt.pix.field = field;
80295c232a2SHans Verkuil 	if (f->fmt.pix.height < 32)
80395c232a2SHans Verkuil 		f->fmt.pix.height = 32;
80495c232a2SHans Verkuil 	if (f->fmt.pix.height > maxh)
80595c232a2SHans Verkuil 		f->fmt.pix.height = maxh;
80695c232a2SHans Verkuil 	if (f->fmt.pix.width < 48)
80795c232a2SHans Verkuil 		f->fmt.pix.width = 48;
80895c232a2SHans Verkuil 	if (f->fmt.pix.width > maxw)
80995c232a2SHans Verkuil 		f->fmt.pix.width = maxw;
81095c232a2SHans Verkuil 	f->fmt.pix.width &= ~0x03;
81195c232a2SHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
81295c232a2SHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
81395c232a2SHans Verkuil 
81495c232a2SHans Verkuil 	return 0;
81595c232a2SHans Verkuil }
816b285192aSMauro Carvalho Chehab static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
817b285192aSMauro Carvalho Chehab {
818b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
819b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
820b285192aSMauro Carvalho Chehab 
821b285192aSMauro Carvalho Chehab 	if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
822b285192aSMauro Carvalho Chehab 		return -EINVAL;
823b285192aSMauro Carvalho Chehab 
824b285192aSMauro Carvalho Chehab 	if (unlikely(i != fh->type))
825b285192aSMauro Carvalho Chehab 		return -EINVAL;
826b285192aSMauro Carvalho Chehab 
827b285192aSMauro Carvalho Chehab 	if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh,
828b285192aSMauro Carvalho Chehab 						RESOURCE_VIDEO0))))
829b285192aSMauro Carvalho Chehab 		return -EBUSY;
830b285192aSMauro Carvalho Chehab 
831b285192aSMauro Carvalho Chehab 	return videobuf_streamon(get_queue(fh));
832b285192aSMauro Carvalho Chehab }
833b285192aSMauro Carvalho Chehab 
834b285192aSMauro Carvalho Chehab static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
835b285192aSMauro Carvalho Chehab {
836b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
837b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
838b285192aSMauro Carvalho Chehab 	int err, res;
839b285192aSMauro Carvalho Chehab 
840b285192aSMauro Carvalho Chehab 	if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
841b285192aSMauro Carvalho Chehab 		return -EINVAL;
842b285192aSMauro Carvalho Chehab 	if (i != fh->type)
843b285192aSMauro Carvalho Chehab 		return -EINVAL;
844b285192aSMauro Carvalho Chehab 
845b285192aSMauro Carvalho Chehab 	res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
846b285192aSMauro Carvalho Chehab 	err = videobuf_streamoff(get_queue(fh));
847b285192aSMauro Carvalho Chehab 	if (err < 0)
848b285192aSMauro Carvalho Chehab 		return err;
849b285192aSMauro Carvalho Chehab 	cx25821_res_free(dev, fh, res);
850b285192aSMauro Carvalho Chehab 	return 0;
851b285192aSMauro Carvalho Chehab }
852b285192aSMauro Carvalho Chehab 
85395c232a2SHans Verkuil static int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
85495c232a2SHans Verkuil {
85595c232a2SHans Verkuil 	if (tvnorm == V4L2_STD_PAL_BG) {
85695c232a2SHans Verkuil 		if (width == 352 || width == 720)
85795c232a2SHans Verkuil 			return 1;
85895c232a2SHans Verkuil 		else
85995c232a2SHans Verkuil 			return 0;
86095c232a2SHans Verkuil 	}
86195c232a2SHans Verkuil 
86295c232a2SHans Verkuil 	if (tvnorm == V4L2_STD_NTSC_M) {
86395c232a2SHans Verkuil 		if (width == 320 || width == 352 || width == 720)
86495c232a2SHans Verkuil 			return 1;
86595c232a2SHans Verkuil 		else
86695c232a2SHans Verkuil 			return 0;
86795c232a2SHans Verkuil 	}
86895c232a2SHans Verkuil 	return 0;
86995c232a2SHans Verkuil }
87095c232a2SHans Verkuil 
87195c232a2SHans Verkuil static int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
87295c232a2SHans Verkuil {
87395c232a2SHans Verkuil 	if (tvnorm == V4L2_STD_PAL_BG) {
87495c232a2SHans Verkuil 		if (height == 576 || height == 288)
87595c232a2SHans Verkuil 			return 1;
87695c232a2SHans Verkuil 		else
87795c232a2SHans Verkuil 			return 0;
87895c232a2SHans Verkuil 	}
87995c232a2SHans Verkuil 
88095c232a2SHans Verkuil 	if (tvnorm == V4L2_STD_NTSC_M) {
88195c232a2SHans Verkuil 		if (height == 480 || height == 240)
88295c232a2SHans Verkuil 			return 1;
88395c232a2SHans Verkuil 		else
88495c232a2SHans Verkuil 			return 0;
88595c232a2SHans Verkuil 	}
88695c232a2SHans Verkuil 
88795c232a2SHans Verkuil 	return 0;
88895c232a2SHans Verkuil }
88995c232a2SHans Verkuil 
890b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
891b285192aSMauro Carvalho Chehab 				struct v4l2_format *f)
892b285192aSMauro Carvalho Chehab {
893b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
894b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
895b285192aSMauro Carvalho Chehab 	struct v4l2_mbus_framefmt mbus_fmt;
896b285192aSMauro Carvalho Chehab 	int err;
897b285192aSMauro Carvalho Chehab 	int pix_format = PIXEL_FRMT_422;
898b285192aSMauro Carvalho Chehab 
899b285192aSMauro Carvalho Chehab 	if (fh) {
900b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
901b285192aSMauro Carvalho Chehab 				      fh->prio);
902b285192aSMauro Carvalho Chehab 		if (0 != err)
903b285192aSMauro Carvalho Chehab 			return err;
904b285192aSMauro Carvalho Chehab 	}
905b285192aSMauro Carvalho Chehab 
906b285192aSMauro Carvalho Chehab 	err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
907b285192aSMauro Carvalho Chehab 
908b285192aSMauro Carvalho Chehab 	if (0 != err)
909b285192aSMauro Carvalho Chehab 		return err;
910b285192aSMauro Carvalho Chehab 
911b285192aSMauro Carvalho Chehab 	fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
912b285192aSMauro Carvalho Chehab 	fh->vidq.field = f->fmt.pix.field;
913b285192aSMauro Carvalho Chehab 
914b285192aSMauro Carvalho Chehab 	/* check if width and height is valid based on set standard */
915b285192aSMauro Carvalho Chehab 	if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
916b285192aSMauro Carvalho Chehab 		fh->width = f->fmt.pix.width;
917b285192aSMauro Carvalho Chehab 
918b285192aSMauro Carvalho Chehab 	if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
919b285192aSMauro Carvalho Chehab 		fh->height = f->fmt.pix.height;
920b285192aSMauro Carvalho Chehab 
921b285192aSMauro Carvalho Chehab 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
922b285192aSMauro Carvalho Chehab 		pix_format = PIXEL_FRMT_411;
923b285192aSMauro Carvalho Chehab 	else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
924b285192aSMauro Carvalho Chehab 		pix_format = PIXEL_FRMT_422;
925b285192aSMauro Carvalho Chehab 	else
926b285192aSMauro Carvalho Chehab 		return -EINVAL;
927b285192aSMauro Carvalho Chehab 
928b285192aSMauro Carvalho Chehab 	cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
929b285192aSMauro Carvalho Chehab 
930b285192aSMauro Carvalho Chehab 	/* check if cif resolution */
931b285192aSMauro Carvalho Chehab 	if (fh->width == 320 || fh->width == 352)
932b285192aSMauro Carvalho Chehab 		dev->channels[fh->channel_id].use_cif_resolution = 1;
933b285192aSMauro Carvalho Chehab 	else
934b285192aSMauro Carvalho Chehab 		dev->channels[fh->channel_id].use_cif_resolution = 0;
935b285192aSMauro Carvalho Chehab 
936b285192aSMauro Carvalho Chehab 	dev->channels[fh->channel_id].cif_width = fh->width;
937b285192aSMauro Carvalho Chehab 	medusa_set_resolution(dev, fh->width, SRAM_CH00);
938b285192aSMauro Carvalho Chehab 
939b285192aSMauro Carvalho Chehab 	v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
940b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
941b285192aSMauro Carvalho Chehab 
942b285192aSMauro Carvalho Chehab 	return 0;
943b285192aSMauro Carvalho Chehab }
944b285192aSMauro Carvalho Chehab 
945b285192aSMauro Carvalho Chehab static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
946b285192aSMauro Carvalho Chehab {
947b285192aSMauro Carvalho Chehab 	int ret_val = 0;
948b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
949b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
950b285192aSMauro Carvalho Chehab 
951b285192aSMauro Carvalho Chehab 	ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
952b285192aSMauro Carvalho Chehab 
953b285192aSMauro Carvalho Chehab 	p->sequence = dev->channels[fh->channel_id].vidq.count;
954b285192aSMauro Carvalho Chehab 
955b285192aSMauro Carvalho Chehab 	return ret_val;
956b285192aSMauro Carvalho Chehab }
957b285192aSMauro Carvalho Chehab 
958b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
959b285192aSMauro Carvalho Chehab {
960b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
961b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
962bfef0d35SHans Verkuil 	const struct sram_channel *sram_ch =
9633dd473caSHans Verkuil 		dev->channels[fh->channel_id].sram_channels;
964b285192aSMauro Carvalho Chehab 	u32 tmp = 0;
965b285192aSMauro Carvalho Chehab 
966b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, core, log_status);
967b285192aSMauro Carvalho Chehab 	tmp = cx_read(sram_ch->dma_ctl);
968b285192aSMauro Carvalho Chehab 	pr_info("Video input 0 is %s\n",
969b285192aSMauro Carvalho Chehab 		(tmp & 0x11) ? "streaming" : "stopped");
970b285192aSMauro Carvalho Chehab 	return 0;
971b285192aSMauro Carvalho Chehab }
972b285192aSMauro Carvalho Chehab 
973b285192aSMauro Carvalho Chehab 
97495c232a2SHans Verkuil static int cx25821_vidioc_querycap(struct file *file, void *priv,
975b285192aSMauro Carvalho Chehab 			    struct v4l2_capability *cap)
976b285192aSMauro Carvalho Chehab {
977b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
9783dd473caSHans Verkuil 	struct cx25821_fh *fh = priv;
9793dd473caSHans Verkuil 	const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
9803dd473caSHans Verkuil 			V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
9813dd473caSHans Verkuil 	const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
982b285192aSMauro Carvalho Chehab 
983b285192aSMauro Carvalho Chehab 	strcpy(cap->driver, "cx25821");
984b285192aSMauro Carvalho Chehab 	strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
985b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
9863dd473caSHans Verkuil 	if (fh->channel_id >= VID_CHANNEL_NUM)
9873dd473caSHans Verkuil 		cap->device_caps = cap_output;
9883dd473caSHans Verkuil 	else
9893dd473caSHans Verkuil 		cap->device_caps = cap_input;
9903dd473caSHans Verkuil 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
991b285192aSMauro Carvalho Chehab 	return 0;
992b285192aSMauro Carvalho Chehab }
993b285192aSMauro Carvalho Chehab 
99495c232a2SHans Verkuil static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
995b285192aSMauro Carvalho Chehab 			    struct v4l2_fmtdesc *f)
996b285192aSMauro Carvalho Chehab {
997b285192aSMauro Carvalho Chehab 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
998b285192aSMauro Carvalho Chehab 		return -EINVAL;
999b285192aSMauro Carvalho Chehab 
1000b285192aSMauro Carvalho Chehab 	strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1001b285192aSMauro Carvalho Chehab 	f->pixelformat = formats[f->index].fourcc;
1002b285192aSMauro Carvalho Chehab 
1003b285192aSMauro Carvalho Chehab 	return 0;
1004b285192aSMauro Carvalho Chehab }
1005b285192aSMauro Carvalho Chehab 
100695c232a2SHans Verkuil static int cx25821_vidioc_reqbufs(struct file *file, void *priv,
1007b285192aSMauro Carvalho Chehab 			   struct v4l2_requestbuffers *p)
1008b285192aSMauro Carvalho Chehab {
1009b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1010b285192aSMauro Carvalho Chehab 	return videobuf_reqbufs(get_queue(fh), p);
1011b285192aSMauro Carvalho Chehab }
1012b285192aSMauro Carvalho Chehab 
101395c232a2SHans Verkuil static int cx25821_vidioc_querybuf(struct file *file, void *priv,
1014b285192aSMauro Carvalho Chehab 			    struct v4l2_buffer *p)
1015b285192aSMauro Carvalho Chehab {
1016b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1017b285192aSMauro Carvalho Chehab 	return videobuf_querybuf(get_queue(fh), p);
1018b285192aSMauro Carvalho Chehab }
1019b285192aSMauro Carvalho Chehab 
102095c232a2SHans Verkuil static int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1021b285192aSMauro Carvalho Chehab {
1022b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1023b285192aSMauro Carvalho Chehab 	return videobuf_qbuf(get_queue(fh), p);
1024b285192aSMauro Carvalho Chehab }
1025b285192aSMauro Carvalho Chehab 
102695c232a2SHans Verkuil static int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
1027b285192aSMauro Carvalho Chehab {
1028b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1029b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = f;
1030b285192aSMauro Carvalho Chehab 
1031b285192aSMauro Carvalho Chehab 	*p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
1032b285192aSMauro Carvalho Chehab 
1033b285192aSMauro Carvalho Chehab 	return 0;
1034b285192aSMauro Carvalho Chehab }
1035b285192aSMauro Carvalho Chehab 
103695c232a2SHans Verkuil static int cx25821_vidioc_s_priority(struct file *file, void *f,
1037b285192aSMauro Carvalho Chehab 			      enum v4l2_priority prio)
1038b285192aSMauro Carvalho Chehab {
1039b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = f;
1040b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1041b285192aSMauro Carvalho Chehab 
1042b285192aSMauro Carvalho Chehab 	return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
1043b285192aSMauro Carvalho Chehab 			prio);
1044b285192aSMauro Carvalho Chehab }
1045b285192aSMauro Carvalho Chehab 
104695c232a2SHans Verkuil static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
104718c73af6SHans Verkuil {
104818c73af6SHans Verkuil 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
104918c73af6SHans Verkuil 
105018c73af6SHans Verkuil 	*tvnorms = dev->tvnorm;
105118c73af6SHans Verkuil 	return 0;
105218c73af6SHans Verkuil }
105318c73af6SHans Verkuil 
1054314527acSHans Verkuil int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
1055b285192aSMauro Carvalho Chehab {
1056b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1057b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1058b285192aSMauro Carvalho Chehab 	int err;
1059b285192aSMauro Carvalho Chehab 
1060b285192aSMauro Carvalho Chehab 	if (fh) {
1061b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1062b285192aSMauro Carvalho Chehab 				      fh->prio);
1063b285192aSMauro Carvalho Chehab 		if (0 != err)
1064b285192aSMauro Carvalho Chehab 			return err;
1065b285192aSMauro Carvalho Chehab 	}
1066b285192aSMauro Carvalho Chehab 
1067314527acSHans Verkuil 	if (dev->tvnorm == tvnorms)
1068b285192aSMauro Carvalho Chehab 		return 0;
1069b285192aSMauro Carvalho Chehab 
1070314527acSHans Verkuil 	cx25821_set_tvnorm(dev, tvnorms);
1071b285192aSMauro Carvalho Chehab 
1072b285192aSMauro Carvalho Chehab 	medusa_set_videostandard(dev);
1073b285192aSMauro Carvalho Chehab 
1074b285192aSMauro Carvalho Chehab 	return 0;
1075b285192aSMauro Carvalho Chehab }
1076b285192aSMauro Carvalho Chehab 
107795c232a2SHans Verkuil static int cx25821_vidioc_enum_input(struct file *file, void *priv,
107895c232a2SHans Verkuil 			      struct v4l2_input *i)
1079b285192aSMauro Carvalho Chehab {
1080b285192aSMauro Carvalho Chehab 	static const char * const iname[] = {
1081b285192aSMauro Carvalho Chehab 		[CX25821_VMUX_COMPOSITE] = "Composite",
1082b285192aSMauro Carvalho Chehab 		[CX25821_VMUX_SVIDEO] = "S-Video",
1083b285192aSMauro Carvalho Chehab 		[CX25821_VMUX_DEBUG] = "for debug only",
1084b285192aSMauro Carvalho Chehab 	};
108595c232a2SHans Verkuil 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1086b285192aSMauro Carvalho Chehab 	unsigned int n;
1087b285192aSMauro Carvalho Chehab 
1088b285192aSMauro Carvalho Chehab 	n = i->index;
108995c232a2SHans Verkuil 	if (n >= CX25821_NR_INPUT)
1090b285192aSMauro Carvalho Chehab 		return -EINVAL;
1091b285192aSMauro Carvalho Chehab 
1092b285192aSMauro Carvalho Chehab 	if (0 == INPUT(n)->type)
1093b285192aSMauro Carvalho Chehab 		return -EINVAL;
1094b285192aSMauro Carvalho Chehab 
1095b285192aSMauro Carvalho Chehab 	i->type = V4L2_INPUT_TYPE_CAMERA;
1096b285192aSMauro Carvalho Chehab 	strcpy(i->name, iname[INPUT(n)->type]);
1097b285192aSMauro Carvalho Chehab 
1098b285192aSMauro Carvalho Chehab 	i->std = CX25821_NORMS;
1099b285192aSMauro Carvalho Chehab 	return 0;
1100b285192aSMauro Carvalho Chehab }
1101b285192aSMauro Carvalho Chehab 
110295c232a2SHans Verkuil static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1103b285192aSMauro Carvalho Chehab {
1104b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1105b285192aSMauro Carvalho Chehab 
1106b285192aSMauro Carvalho Chehab 	*i = dev->input;
1107b285192aSMauro Carvalho Chehab 	return 0;
1108b285192aSMauro Carvalho Chehab }
1109b285192aSMauro Carvalho Chehab 
111095c232a2SHans Verkuil static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1111b285192aSMauro Carvalho Chehab {
1112b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1113b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1114b285192aSMauro Carvalho Chehab 	int err;
1115b285192aSMauro Carvalho Chehab 
1116b285192aSMauro Carvalho Chehab 	if (fh) {
1117b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1118b285192aSMauro Carvalho Chehab 				      fh->prio);
1119b285192aSMauro Carvalho Chehab 		if (0 != err)
1120b285192aSMauro Carvalho Chehab 			return err;
1121b285192aSMauro Carvalho Chehab 	}
1122b285192aSMauro Carvalho Chehab 
11236b1dce25SHans Verkuil 	if (i >= CX25821_NR_INPUT || INPUT(i)->type == 0)
1124b285192aSMauro Carvalho Chehab 		return -EINVAL;
1125b285192aSMauro Carvalho Chehab 
1126b285192aSMauro Carvalho Chehab 	cx25821_video_mux(dev, i);
1127b285192aSMauro Carvalho Chehab 	return 0;
1128b285192aSMauro Carvalho Chehab }
1129b285192aSMauro Carvalho Chehab 
1130b285192aSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
1131b285192aSMauro Carvalho Chehab int cx25821_vidioc_g_register(struct file *file, void *fh,
1132b285192aSMauro Carvalho Chehab 		      struct v4l2_dbg_register *reg)
1133b285192aSMauro Carvalho Chehab {
1134b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1135b285192aSMauro Carvalho Chehab 
1136b285192aSMauro Carvalho Chehab 	if (!v4l2_chip_match_host(&reg->match))
1137b285192aSMauro Carvalho Chehab 		return -EINVAL;
1138b285192aSMauro Carvalho Chehab 
1139b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, core, g_register, reg);
1140b285192aSMauro Carvalho Chehab 
1141b285192aSMauro Carvalho Chehab 	return 0;
1142b285192aSMauro Carvalho Chehab }
1143b285192aSMauro Carvalho Chehab 
1144b285192aSMauro Carvalho Chehab int cx25821_vidioc_s_register(struct file *file, void *fh,
1145977ba3b1SHans Verkuil 		      const struct v4l2_dbg_register *reg)
1146b285192aSMauro Carvalho Chehab {
1147b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1148b285192aSMauro Carvalho Chehab 
1149b285192aSMauro Carvalho Chehab 	if (!v4l2_chip_match_host(&reg->match))
1150b285192aSMauro Carvalho Chehab 		return -EINVAL;
1151b285192aSMauro Carvalho Chehab 
1152b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, core, s_register, reg);
1153b285192aSMauro Carvalho Chehab 
1154b285192aSMauro Carvalho Chehab 	return 0;
1155b285192aSMauro Carvalho Chehab }
1156b285192aSMauro Carvalho Chehab 
1157b285192aSMauro Carvalho Chehab #endif
1158b285192aSMauro Carvalho Chehab 
1159f8d7ee70SHans Verkuil static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
1160b285192aSMauro Carvalho Chehab {
1161f8d7ee70SHans Verkuil 	struct cx25821_channel *chan =
1162f8d7ee70SHans Verkuil 		container_of(ctrl->handler, struct cx25821_channel, hdl);
1163f8d7ee70SHans Verkuil 	struct cx25821_dev *dev = chan->dev;
1164b285192aSMauro Carvalho Chehab 
1165f8d7ee70SHans Verkuil 	switch (ctrl->id) {
1166b285192aSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
1167f8d7ee70SHans Verkuil 		medusa_set_brightness(dev, ctrl->val, chan->id);
1168b285192aSMauro Carvalho Chehab 		break;
1169b285192aSMauro Carvalho Chehab 	case V4L2_CID_HUE:
1170f8d7ee70SHans Verkuil 		medusa_set_hue(dev, ctrl->val, chan->id);
1171b285192aSMauro Carvalho Chehab 		break;
1172b285192aSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
1173f8d7ee70SHans Verkuil 		medusa_set_contrast(dev, ctrl->val, chan->id);
1174b285192aSMauro Carvalho Chehab 		break;
1175b285192aSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
1176f8d7ee70SHans Verkuil 		medusa_set_saturation(dev, ctrl->val, chan->id);
1177b285192aSMauro Carvalho Chehab 		break;
1178b285192aSMauro Carvalho Chehab 	default:
1179f8d7ee70SHans Verkuil 		return -EINVAL;
1180b285192aSMauro Carvalho Chehab 	}
1181f8d7ee70SHans Verkuil 	return 0;
1182b285192aSMauro Carvalho Chehab }
1183b285192aSMauro Carvalho Chehab 
1184b285192aSMauro Carvalho Chehab static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
1185b285192aSMauro Carvalho Chehab 				 unsigned long arg)
1186b285192aSMauro Carvalho Chehab {
1187b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1188b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1189b285192aSMauro Carvalho Chehab 	int command = 0;
1190b285192aSMauro Carvalho Chehab 	struct upstream_user_struct *data_from_user;
1191b285192aSMauro Carvalho Chehab 
1192b285192aSMauro Carvalho Chehab 	data_from_user = (struct upstream_user_struct *)arg;
1193b285192aSMauro Carvalho Chehab 
1194b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1195b285192aSMauro Carvalho Chehab 		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1196b285192aSMauro Carvalho Chehab 		return 0;
1197b285192aSMauro Carvalho Chehab 	}
1198b285192aSMauro Carvalho Chehab 
1199b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1200b285192aSMauro Carvalho Chehab 
1201b285192aSMauro Carvalho Chehab 	if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1202b285192aSMauro Carvalho Chehab 		return 0;
1203b285192aSMauro Carvalho Chehab 
1204b285192aSMauro Carvalho Chehab 	dev->input_filename = data_from_user->input_filename;
1205b285192aSMauro Carvalho Chehab 	dev->input_audiofilename = data_from_user->input_filename;
1206b285192aSMauro Carvalho Chehab 	dev->vid_stdname = data_from_user->vid_stdname;
1207b285192aSMauro Carvalho Chehab 	dev->pixel_format = data_from_user->pixel_format;
1208b285192aSMauro Carvalho Chehab 	dev->channel_select = data_from_user->channel_select;
1209b285192aSMauro Carvalho Chehab 	dev->command = data_from_user->command;
1210b285192aSMauro Carvalho Chehab 
1211b285192aSMauro Carvalho Chehab 	switch (command) {
1212b285192aSMauro Carvalho Chehab 	case UPSTREAM_START_VIDEO:
1213b285192aSMauro Carvalho Chehab 		cx25821_start_upstream_video_ch1(dev, data_from_user);
1214b285192aSMauro Carvalho Chehab 		break;
1215b285192aSMauro Carvalho Chehab 
1216b285192aSMauro Carvalho Chehab 	case UPSTREAM_STOP_VIDEO:
1217b285192aSMauro Carvalho Chehab 		cx25821_stop_upstream_video_ch1(dev);
1218b285192aSMauro Carvalho Chehab 		break;
1219b285192aSMauro Carvalho Chehab 	}
1220b285192aSMauro Carvalho Chehab 
1221b285192aSMauro Carvalho Chehab 	return 0;
1222b285192aSMauro Carvalho Chehab }
1223b285192aSMauro Carvalho Chehab 
1224b285192aSMauro Carvalho Chehab static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
1225b285192aSMauro Carvalho Chehab 				  unsigned long arg)
1226b285192aSMauro Carvalho Chehab {
1227b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1228b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1229b285192aSMauro Carvalho Chehab 	int command = 0;
1230b285192aSMauro Carvalho Chehab 	struct upstream_user_struct *data_from_user;
1231b285192aSMauro Carvalho Chehab 
1232b285192aSMauro Carvalho Chehab 	data_from_user = (struct upstream_user_struct *)arg;
1233b285192aSMauro Carvalho Chehab 
1234b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1235b285192aSMauro Carvalho Chehab 		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1236b285192aSMauro Carvalho Chehab 		return 0;
1237b285192aSMauro Carvalho Chehab 	}
1238b285192aSMauro Carvalho Chehab 
1239b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1240b285192aSMauro Carvalho Chehab 
1241b285192aSMauro Carvalho Chehab 	if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1242b285192aSMauro Carvalho Chehab 		return 0;
1243b285192aSMauro Carvalho Chehab 
1244b285192aSMauro Carvalho Chehab 	dev->input_filename_ch2 = data_from_user->input_filename;
1245b285192aSMauro Carvalho Chehab 	dev->input_audiofilename = data_from_user->input_filename;
1246b285192aSMauro Carvalho Chehab 	dev->vid_stdname_ch2 = data_from_user->vid_stdname;
1247b285192aSMauro Carvalho Chehab 	dev->pixel_format_ch2 = data_from_user->pixel_format;
1248b285192aSMauro Carvalho Chehab 	dev->channel_select_ch2 = data_from_user->channel_select;
1249b285192aSMauro Carvalho Chehab 	dev->command_ch2 = data_from_user->command;
1250b285192aSMauro Carvalho Chehab 
1251b285192aSMauro Carvalho Chehab 	switch (command) {
1252b285192aSMauro Carvalho Chehab 	case UPSTREAM_START_VIDEO:
1253b285192aSMauro Carvalho Chehab 		cx25821_start_upstream_video_ch2(dev, data_from_user);
1254b285192aSMauro Carvalho Chehab 		break;
1255b285192aSMauro Carvalho Chehab 
1256b285192aSMauro Carvalho Chehab 	case UPSTREAM_STOP_VIDEO:
1257b285192aSMauro Carvalho Chehab 		cx25821_stop_upstream_video_ch2(dev);
1258b285192aSMauro Carvalho Chehab 		break;
1259b285192aSMauro Carvalho Chehab 	}
1260b285192aSMauro Carvalho Chehab 
1261b285192aSMauro Carvalho Chehab 	return 0;
1262b285192aSMauro Carvalho Chehab }
1263b285192aSMauro Carvalho Chehab 
1264b285192aSMauro Carvalho Chehab static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
1265b285192aSMauro Carvalho Chehab 				  unsigned long arg)
1266b285192aSMauro Carvalho Chehab {
1267b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1268b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1269b285192aSMauro Carvalho Chehab 	int command = 0;
1270b285192aSMauro Carvalho Chehab 	struct upstream_user_struct *data_from_user;
1271b285192aSMauro Carvalho Chehab 
1272b285192aSMauro Carvalho Chehab 	data_from_user = (struct upstream_user_struct *)arg;
1273b285192aSMauro Carvalho Chehab 
1274b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1275b285192aSMauro Carvalho Chehab 		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1276b285192aSMauro Carvalho Chehab 		return 0;
1277b285192aSMauro Carvalho Chehab 	}
1278b285192aSMauro Carvalho Chehab 
1279b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1280b285192aSMauro Carvalho Chehab 
1281b285192aSMauro Carvalho Chehab 	if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
1282b285192aSMauro Carvalho Chehab 		return 0;
1283b285192aSMauro Carvalho Chehab 
1284b285192aSMauro Carvalho Chehab 	dev->input_filename = data_from_user->input_filename;
1285b285192aSMauro Carvalho Chehab 	dev->input_audiofilename = data_from_user->input_filename;
1286b285192aSMauro Carvalho Chehab 	dev->vid_stdname = data_from_user->vid_stdname;
1287b285192aSMauro Carvalho Chehab 	dev->pixel_format = data_from_user->pixel_format;
1288b285192aSMauro Carvalho Chehab 	dev->channel_select = data_from_user->channel_select;
1289b285192aSMauro Carvalho Chehab 	dev->command = data_from_user->command;
1290b285192aSMauro Carvalho Chehab 
1291b285192aSMauro Carvalho Chehab 	switch (command) {
1292b285192aSMauro Carvalho Chehab 	case UPSTREAM_START_AUDIO:
1293b285192aSMauro Carvalho Chehab 		cx25821_start_upstream_audio(dev, data_from_user);
1294b285192aSMauro Carvalho Chehab 		break;
1295b285192aSMauro Carvalho Chehab 
1296b285192aSMauro Carvalho Chehab 	case UPSTREAM_STOP_AUDIO:
1297b285192aSMauro Carvalho Chehab 		cx25821_stop_upstream_audio(dev);
1298b285192aSMauro Carvalho Chehab 		break;
1299b285192aSMauro Carvalho Chehab 	}
1300b285192aSMauro Carvalho Chehab 
1301b285192aSMauro Carvalho Chehab 	return 0;
1302b285192aSMauro Carvalho Chehab }
1303b285192aSMauro Carvalho Chehab 
1304b285192aSMauro Carvalho Chehab static long video_ioctl_set(struct file *file, unsigned int cmd,
1305b285192aSMauro Carvalho Chehab 			   unsigned long arg)
1306b285192aSMauro Carvalho Chehab {
1307b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1308b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1309b285192aSMauro Carvalho Chehab 	struct downstream_user_struct *data_from_user;
1310b285192aSMauro Carvalho Chehab 	int command;
1311b285192aSMauro Carvalho Chehab 	int width = 720;
1312b285192aSMauro Carvalho Chehab 	int selected_channel = 0;
1313b285192aSMauro Carvalho Chehab 	int pix_format = 0;
1314b285192aSMauro Carvalho Chehab 	int i = 0;
1315b285192aSMauro Carvalho Chehab 	int cif_enable = 0;
1316b285192aSMauro Carvalho Chehab 	int cif_width = 0;
1317b285192aSMauro Carvalho Chehab 
1318b285192aSMauro Carvalho Chehab 	data_from_user = (struct downstream_user_struct *)arg;
1319b285192aSMauro Carvalho Chehab 
1320b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1321b285192aSMauro Carvalho Chehab 		pr_err("%s(): User data is INVALID. Returning\n", __func__);
1322b285192aSMauro Carvalho Chehab 		return 0;
1323b285192aSMauro Carvalho Chehab 	}
1324b285192aSMauro Carvalho Chehab 
1325b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1326b285192aSMauro Carvalho Chehab 
1327b285192aSMauro Carvalho Chehab 	if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
1328b285192aSMauro Carvalho Chehab 	   && command != ENABLE_CIF_RESOLUTION && command != REG_READ
1329b285192aSMauro Carvalho Chehab 	   && command != REG_WRITE && command != MEDUSA_READ
1330b285192aSMauro Carvalho Chehab 	   && command != MEDUSA_WRITE) {
1331b285192aSMauro Carvalho Chehab 		return 0;
1332b285192aSMauro Carvalho Chehab 	}
1333b285192aSMauro Carvalho Chehab 
1334b285192aSMauro Carvalho Chehab 	switch (command) {
1335b285192aSMauro Carvalho Chehab 	case SET_VIDEO_STD:
1336b285192aSMauro Carvalho Chehab 		if (!strcmp(data_from_user->vid_stdname, "PAL"))
1337b285192aSMauro Carvalho Chehab 			dev->tvnorm = V4L2_STD_PAL_BG;
1338b285192aSMauro Carvalho Chehab 		else
1339b285192aSMauro Carvalho Chehab 			dev->tvnorm = V4L2_STD_NTSC_M;
1340b285192aSMauro Carvalho Chehab 		medusa_set_videostandard(dev);
1341b285192aSMauro Carvalho Chehab 		break;
1342b285192aSMauro Carvalho Chehab 
1343b285192aSMauro Carvalho Chehab 	case SET_PIXEL_FORMAT:
1344b285192aSMauro Carvalho Chehab 		selected_channel = data_from_user->decoder_select;
1345b285192aSMauro Carvalho Chehab 		pix_format = data_from_user->pixel_format;
1346b285192aSMauro Carvalho Chehab 
1347b285192aSMauro Carvalho Chehab 		if (!(selected_channel <= 7 && selected_channel >= 0)) {
1348b285192aSMauro Carvalho Chehab 			selected_channel -= 4;
1349b285192aSMauro Carvalho Chehab 			selected_channel = selected_channel % 8;
1350b285192aSMauro Carvalho Chehab 		}
1351b285192aSMauro Carvalho Chehab 
1352b285192aSMauro Carvalho Chehab 		if (selected_channel >= 0)
1353b285192aSMauro Carvalho Chehab 			cx25821_set_pixel_format(dev, selected_channel,
1354b285192aSMauro Carvalho Chehab 						pix_format);
1355b285192aSMauro Carvalho Chehab 
1356b285192aSMauro Carvalho Chehab 		break;
1357b285192aSMauro Carvalho Chehab 
1358b285192aSMauro Carvalho Chehab 	case ENABLE_CIF_RESOLUTION:
1359b285192aSMauro Carvalho Chehab 		selected_channel = data_from_user->decoder_select;
1360b285192aSMauro Carvalho Chehab 		cif_enable = data_from_user->cif_resolution_enable;
1361b285192aSMauro Carvalho Chehab 		cif_width = data_from_user->cif_width;
1362b285192aSMauro Carvalho Chehab 
1363b285192aSMauro Carvalho Chehab 		if (cif_enable) {
1364b285192aSMauro Carvalho Chehab 			if (dev->tvnorm & V4L2_STD_PAL_BG
1365b285192aSMauro Carvalho Chehab 			    || dev->tvnorm & V4L2_STD_PAL_DK) {
1366b285192aSMauro Carvalho Chehab 				width = 352;
1367b285192aSMauro Carvalho Chehab 			} else {
1368b285192aSMauro Carvalho Chehab 				width = cif_width;
1369b285192aSMauro Carvalho Chehab 				if (cif_width != 320 && cif_width != 352)
1370b285192aSMauro Carvalho Chehab 					width = 320;
1371b285192aSMauro Carvalho Chehab 			}
1372b285192aSMauro Carvalho Chehab 		}
1373b285192aSMauro Carvalho Chehab 
1374b285192aSMauro Carvalho Chehab 		if (!(selected_channel <= 7 && selected_channel >= 0)) {
1375b285192aSMauro Carvalho Chehab 			selected_channel -= 4;
1376b285192aSMauro Carvalho Chehab 			selected_channel = selected_channel % 8;
1377b285192aSMauro Carvalho Chehab 		}
1378b285192aSMauro Carvalho Chehab 
1379b285192aSMauro Carvalho Chehab 		if (selected_channel <= 7 && selected_channel >= 0) {
1380b285192aSMauro Carvalho Chehab 			dev->channels[selected_channel].use_cif_resolution =
1381b285192aSMauro Carvalho Chehab 				cif_enable;
1382b285192aSMauro Carvalho Chehab 			dev->channels[selected_channel].cif_width = width;
1383b285192aSMauro Carvalho Chehab 		} else {
1384b285192aSMauro Carvalho Chehab 			for (i = 0; i < VID_CHANNEL_NUM; i++) {
1385b285192aSMauro Carvalho Chehab 				dev->channels[i].use_cif_resolution =
1386b285192aSMauro Carvalho Chehab 					cif_enable;
1387b285192aSMauro Carvalho Chehab 				dev->channels[i].cif_width = width;
1388b285192aSMauro Carvalho Chehab 			}
1389b285192aSMauro Carvalho Chehab 		}
1390b285192aSMauro Carvalho Chehab 
1391b285192aSMauro Carvalho Chehab 		medusa_set_resolution(dev, width, selected_channel);
1392b285192aSMauro Carvalho Chehab 		break;
1393b285192aSMauro Carvalho Chehab 	case REG_READ:
1394b285192aSMauro Carvalho Chehab 		data_from_user->reg_data = cx_read(data_from_user->reg_address);
1395b285192aSMauro Carvalho Chehab 		break;
1396b285192aSMauro Carvalho Chehab 	case REG_WRITE:
1397b285192aSMauro Carvalho Chehab 		cx_write(data_from_user->reg_address, data_from_user->reg_data);
1398b285192aSMauro Carvalho Chehab 		break;
1399b285192aSMauro Carvalho Chehab 	case MEDUSA_READ:
1400b285192aSMauro Carvalho Chehab 		cx25821_i2c_read(&dev->i2c_bus[0],
1401b285192aSMauro Carvalho Chehab 					 (u16) data_from_user->reg_address,
1402b285192aSMauro Carvalho Chehab 					 &data_from_user->reg_data);
1403b285192aSMauro Carvalho Chehab 		break;
1404b285192aSMauro Carvalho Chehab 	case MEDUSA_WRITE:
1405b285192aSMauro Carvalho Chehab 		cx25821_i2c_write(&dev->i2c_bus[0],
1406b285192aSMauro Carvalho Chehab 				  (u16) data_from_user->reg_address,
1407b285192aSMauro Carvalho Chehab 				  data_from_user->reg_data);
1408b285192aSMauro Carvalho Chehab 		break;
1409b285192aSMauro Carvalho Chehab 	}
1410b285192aSMauro Carvalho Chehab 
1411b285192aSMauro Carvalho Chehab 	return 0;
1412b285192aSMauro Carvalho Chehab }
1413b285192aSMauro Carvalho Chehab 
1414b285192aSMauro Carvalho Chehab static long cx25821_video_ioctl(struct file *file,
1415b285192aSMauro Carvalho Chehab 				unsigned int cmd, unsigned long arg)
1416b285192aSMauro Carvalho Chehab {
1417b285192aSMauro Carvalho Chehab 	int ret = 0;
1418b285192aSMauro Carvalho Chehab 
1419b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1420b285192aSMauro Carvalho Chehab 
1421b285192aSMauro Carvalho Chehab 	/* check to see if it's the video upstream */
1422b285192aSMauro Carvalho Chehab 	if (fh->channel_id == SRAM_CH09) {
1423b285192aSMauro Carvalho Chehab 		ret = video_ioctl_upstream9(file, cmd, arg);
1424b285192aSMauro Carvalho Chehab 		return ret;
1425b285192aSMauro Carvalho Chehab 	} else if (fh->channel_id == SRAM_CH10) {
1426b285192aSMauro Carvalho Chehab 		ret = video_ioctl_upstream10(file, cmd, arg);
1427b285192aSMauro Carvalho Chehab 		return ret;
1428b285192aSMauro Carvalho Chehab 	} else if (fh->channel_id == SRAM_CH11) {
1429b285192aSMauro Carvalho Chehab 		ret = video_ioctl_upstream11(file, cmd, arg);
1430b285192aSMauro Carvalho Chehab 		ret = video_ioctl_set(file, cmd, arg);
1431b285192aSMauro Carvalho Chehab 		return ret;
1432b285192aSMauro Carvalho Chehab 	}
1433b285192aSMauro Carvalho Chehab 
1434b285192aSMauro Carvalho Chehab 	return video_ioctl2(file, cmd, arg);
1435b285192aSMauro Carvalho Chehab }
1436b285192aSMauro Carvalho Chehab 
1437f8d7ee70SHans Verkuil static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
1438f8d7ee70SHans Verkuil 	.s_ctrl = cx25821_s_ctrl,
1439f8d7ee70SHans Verkuil };
1440f8d7ee70SHans Verkuil 
1441b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops = {
1442b285192aSMauro Carvalho Chehab 	.owner = THIS_MODULE,
1443b285192aSMauro Carvalho Chehab 	.open = video_open,
1444b285192aSMauro Carvalho Chehab 	.release = video_release,
1445b285192aSMauro Carvalho Chehab 	.read = video_read,
1446b285192aSMauro Carvalho Chehab 	.poll = video_poll,
1447b285192aSMauro Carvalho Chehab 	.mmap = cx25821_video_mmap,
1448*be178cb4SHans Verkuil 	.unlocked_ioctl = cx25821_video_ioctl,
1449b285192aSMauro Carvalho Chehab };
1450b285192aSMauro Carvalho Chehab 
1451b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops = {
1452b285192aSMauro Carvalho Chehab 	.vidioc_querycap = cx25821_vidioc_querycap,
1453b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
1454b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
1455b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
1456b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1457b285192aSMauro Carvalho Chehab 	.vidioc_reqbufs = cx25821_vidioc_reqbufs,
1458b285192aSMauro Carvalho Chehab 	.vidioc_querybuf = cx25821_vidioc_querybuf,
1459b285192aSMauro Carvalho Chehab 	.vidioc_qbuf = cx25821_vidioc_qbuf,
1460b285192aSMauro Carvalho Chehab 	.vidioc_dqbuf = vidioc_dqbuf,
146118c73af6SHans Verkuil 	.vidioc_g_std = cx25821_vidioc_g_std,
1462b285192aSMauro Carvalho Chehab 	.vidioc_s_std = cx25821_vidioc_s_std,
1463b285192aSMauro Carvalho Chehab 	.vidioc_enum_input = cx25821_vidioc_enum_input,
1464b285192aSMauro Carvalho Chehab 	.vidioc_g_input = cx25821_vidioc_g_input,
1465b285192aSMauro Carvalho Chehab 	.vidioc_s_input = cx25821_vidioc_s_input,
1466b285192aSMauro Carvalho Chehab 	.vidioc_streamon = vidioc_streamon,
1467b285192aSMauro Carvalho Chehab 	.vidioc_streamoff = vidioc_streamoff,
1468b285192aSMauro Carvalho Chehab 	.vidioc_log_status = vidioc_log_status,
1469b285192aSMauro Carvalho Chehab 	.vidioc_g_priority = cx25821_vidioc_g_priority,
1470b285192aSMauro Carvalho Chehab 	.vidioc_s_priority = cx25821_vidioc_s_priority,
1471b285192aSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
1472b285192aSMauro Carvalho Chehab 	.vidioc_g_register = cx25821_vidioc_g_register,
1473b285192aSMauro Carvalho Chehab 	.vidioc_s_register = cx25821_vidioc_s_register,
1474b285192aSMauro Carvalho Chehab #endif
1475b285192aSMauro Carvalho Chehab };
1476b285192aSMauro Carvalho Chehab 
1477ffd3c233SHans Verkuil static const struct video_device cx25821_video_device = {
1478ffd3c233SHans Verkuil 	.name = "cx25821-video",
1479b285192aSMauro Carvalho Chehab 	.fops = &video_fops,
1480467870caSHans Verkuil 	.release = video_device_release_empty,
1481ffd3c233SHans Verkuil 	.minor = -1,
1482b285192aSMauro Carvalho Chehab 	.ioctl_ops = &video_ioctl_ops,
1483b285192aSMauro Carvalho Chehab 	.tvnorms = CX25821_NORMS,
1484b285192aSMauro Carvalho Chehab };
1485ffd3c233SHans Verkuil 
1486ffd3c233SHans Verkuil void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
1487ffd3c233SHans Verkuil {
1488ffd3c233SHans Verkuil 	cx_clear(PCI_INT_MSK, 1);
1489ffd3c233SHans Verkuil 
1490467870caSHans Verkuil 	if (video_is_registered(&dev->channels[chan_num].vdev)) {
1491467870caSHans Verkuil 		video_unregister_device(&dev->channels[chan_num].vdev);
1492f8d7ee70SHans Verkuil 		v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
1493ffd3c233SHans Verkuil 
1494ffd3c233SHans Verkuil 		btcx_riscmem_free(dev->pci,
1495ffd3c233SHans Verkuil 				&dev->channels[chan_num].vidq.stopper);
1496ffd3c233SHans Verkuil 	}
1497ffd3c233SHans Verkuil }
1498ffd3c233SHans Verkuil 
1499ffd3c233SHans Verkuil int cx25821_video_register(struct cx25821_dev *dev)
1500ffd3c233SHans Verkuil {
1501ffd3c233SHans Verkuil 	int err;
1502ffd3c233SHans Verkuil 	int i;
1503ffd3c233SHans Verkuil 
1504*be178cb4SHans Verkuil 	/* initial device configuration */
1505*be178cb4SHans Verkuil 	dev->tvnorm = V4L2_STD_NTSC_M,
1506*be178cb4SHans Verkuil 	cx25821_set_tvnorm(dev, dev->tvnorm);
1507*be178cb4SHans Verkuil 
1508ffd3c233SHans Verkuil 	spin_lock_init(&dev->slock);
1509ffd3c233SHans Verkuil 
1510ffd3c233SHans Verkuil 	for (i = 0; i < VID_CHANNEL_NUM; ++i) {
1511467870caSHans Verkuil 		struct video_device *vdev = &dev->channels[i].vdev;
1512f8d7ee70SHans Verkuil 		struct v4l2_ctrl_handler *hdl = &dev->channels[i].hdl;
1513467870caSHans Verkuil 
1514ffd3c233SHans Verkuil 		if (i == SRAM_CH08) /* audio channel */
1515ffd3c233SHans Verkuil 			continue;
1516ffd3c233SHans Verkuil 
1517f8d7ee70SHans Verkuil 		v4l2_ctrl_handler_init(hdl, 4);
1518f8d7ee70SHans Verkuil 		v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1519f8d7ee70SHans Verkuil 			V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
1520f8d7ee70SHans Verkuil 		v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1521f8d7ee70SHans Verkuil 			V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
1522f8d7ee70SHans Verkuil 		v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1523f8d7ee70SHans Verkuil 			V4L2_CID_SATURATION, 0, 10000, 1, 5000);
1524f8d7ee70SHans Verkuil 		v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1525f8d7ee70SHans Verkuil 			V4L2_CID_HUE, 0, 10000, 1, 5000);
1526f8d7ee70SHans Verkuil 		if (hdl->error) {
1527f8d7ee70SHans Verkuil 			err = hdl->error;
1528f8d7ee70SHans Verkuil 			goto fail_unreg;
1529f8d7ee70SHans Verkuil 		}
1530*be178cb4SHans Verkuil 		err = v4l2_ctrl_handler_setup(hdl);
1531*be178cb4SHans Verkuil 		if (err)
1532*be178cb4SHans Verkuil 			goto fail_unreg;
1533ffd3c233SHans Verkuil 
1534ffd3c233SHans Verkuil 		cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
1535ffd3c233SHans Verkuil 			dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
1536ffd3c233SHans Verkuil 
1537ffd3c233SHans Verkuil 		dev->channels[i].sram_channels = &cx25821_sram_channels[i];
1538ffd3c233SHans Verkuil 		dev->channels[i].resources = 0;
1539ffd3c233SHans Verkuil 
1540ffd3c233SHans Verkuil 		cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
1541ffd3c233SHans Verkuil 
1542ffd3c233SHans Verkuil 		INIT_LIST_HEAD(&dev->channels[i].vidq.active);
1543ffd3c233SHans Verkuil 		INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
1544ffd3c233SHans Verkuil 
1545ffd3c233SHans Verkuil 		dev->channels[i].timeout_data.dev = dev;
1546ffd3c233SHans Verkuil 		dev->channels[i].timeout_data.channel =
1547ffd3c233SHans Verkuil 			&cx25821_sram_channels[i];
1548ffd3c233SHans Verkuil 		dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
1549ffd3c233SHans Verkuil 		dev->channels[i].vidq.timeout.data =
1550ffd3c233SHans Verkuil 			(unsigned long)&dev->channels[i].timeout_data;
1551ffd3c233SHans Verkuil 		init_timer(&dev->channels[i].vidq.timeout);
1552ffd3c233SHans Verkuil 
1553ffd3c233SHans Verkuil 		/* register v4l devices */
1554467870caSHans Verkuil 		*vdev = cx25821_video_device;
1555467870caSHans Verkuil 		vdev->v4l2_dev = &dev->v4l2_dev;
1556f8d7ee70SHans Verkuil 		vdev->ctrl_handler = hdl;
1557*be178cb4SHans Verkuil 		vdev->lock = &dev->lock;
1558467870caSHans Verkuil 		snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
1559467870caSHans Verkuil 		video_set_drvdata(vdev, dev);
1560ffd3c233SHans Verkuil 
1561467870caSHans Verkuil 		err = video_register_device(vdev, VFL_TYPE_GRABBER,
1562467870caSHans Verkuil 					    video_nr[dev->nr]);
1563ffd3c233SHans Verkuil 
1564ffd3c233SHans Verkuil 		if (err < 0)
1565ffd3c233SHans Verkuil 			goto fail_unreg;
1566ffd3c233SHans Verkuil 	}
1567ffd3c233SHans Verkuil 
1568ffd3c233SHans Verkuil 	/* set PCI interrupt */
1569ffd3c233SHans Verkuil 	cx_set(PCI_INT_MSK, 0xff);
1570ffd3c233SHans Verkuil 
1571ffd3c233SHans Verkuil 	return 0;
1572ffd3c233SHans Verkuil 
1573ffd3c233SHans Verkuil fail_unreg:
1574467870caSHans Verkuil 	while (i >= 0)
1575467870caSHans Verkuil 		cx25821_video_unregister(dev, i--);
1576ffd3c233SHans Verkuil 	return err;
1577ffd3c233SHans Verkuil }
1578