xref: /linux/drivers/media/pci/cx25821/cx25821-video.c (revision 95c232a24b73199f464e3234510ae4729196f38d)
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 
49*95c232a2SHans 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 
55*95c232a2SHans 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 
84*95c232a2SHans 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 
134*95c232a2SHans 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 
147*95c232a2SHans Verkuil static struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
148b285192aSMauro Carvalho Chehab 				       struct pci_dev *pci,
149170bd533SHans Verkuil 				       const struct video_device *template,
150b285192aSMauro Carvalho Chehab 				       char *type)
151b285192aSMauro Carvalho Chehab {
152b285192aSMauro Carvalho Chehab 	struct video_device *vfd;
153b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
154b285192aSMauro Carvalho Chehab 
155b285192aSMauro Carvalho Chehab 	vfd = video_device_alloc();
156b285192aSMauro Carvalho Chehab 	if (NULL == vfd)
157b285192aSMauro Carvalho Chehab 		return NULL;
158b285192aSMauro Carvalho Chehab 	*vfd = *template;
159b285192aSMauro Carvalho Chehab 	vfd->v4l2_dev = &dev->v4l2_dev;
160b285192aSMauro Carvalho Chehab 	vfd->release = video_device_release;
161b285192aSMauro Carvalho Chehab 	snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, type,
162b285192aSMauro Carvalho Chehab 		 cx25821_boards[dev->board].name);
163b285192aSMauro Carvalho Chehab 	video_set_drvdata(vfd, dev);
164b285192aSMauro Carvalho Chehab 	return vfd;
165b285192aSMauro Carvalho Chehab }
166b285192aSMauro Carvalho Chehab 
167b285192aSMauro Carvalho Chehab /*
168b285192aSMauro Carvalho Chehab static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
169b285192aSMauro Carvalho Chehab {
170b285192aSMauro Carvalho Chehab 	int i;
171b285192aSMauro Carvalho Chehab 
172b285192aSMauro Carvalho Chehab 	if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
173b285192aSMauro Carvalho Chehab 		return -EINVAL;
174b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX25821_CTLS; i++)
175b285192aSMauro Carvalho Chehab 		if (cx25821_ctls[i].v.id == qctrl->id)
176b285192aSMauro Carvalho Chehab 			break;
177b285192aSMauro Carvalho Chehab 	if (i == CX25821_CTLS) {
178b285192aSMauro Carvalho Chehab 		*qctrl = no_ctl;
179b285192aSMauro Carvalho Chehab 		return 0;
180b285192aSMauro Carvalho Chehab 	}
181b285192aSMauro Carvalho Chehab 	*qctrl = cx25821_ctls[i].v;
182b285192aSMauro Carvalho Chehab 	return 0;
183b285192aSMauro Carvalho Chehab }
184b285192aSMauro Carvalho Chehab */
185b285192aSMauro Carvalho Chehab 
186b285192aSMauro Carvalho Chehab /* resource management */
187b285192aSMauro Carvalho Chehab int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh,
188b285192aSMauro Carvalho Chehab 		    unsigned int bit)
189b285192aSMauro Carvalho Chehab {
190b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
191b285192aSMauro Carvalho Chehab 	if (fh->resources & bit)
192b285192aSMauro Carvalho Chehab 		/* have it already allocated */
193b285192aSMauro Carvalho Chehab 		return 1;
194b285192aSMauro Carvalho Chehab 
195b285192aSMauro Carvalho Chehab 	/* is it free? */
196b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
197b285192aSMauro Carvalho Chehab 	if (dev->channels[fh->channel_id].resources & bit) {
198b285192aSMauro Carvalho Chehab 		/* no, someone else uses it */
199b285192aSMauro Carvalho Chehab 		mutex_unlock(&dev->lock);
200b285192aSMauro Carvalho Chehab 		return 0;
201b285192aSMauro Carvalho Chehab 	}
202b285192aSMauro Carvalho Chehab 	/* it's free, grab it */
203b285192aSMauro Carvalho Chehab 	fh->resources |= bit;
204b285192aSMauro Carvalho Chehab 	dev->channels[fh->channel_id].resources |= bit;
205b285192aSMauro Carvalho Chehab 	dprintk(1, "res: get %d\n", bit);
206b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
207b285192aSMauro Carvalho Chehab 	return 1;
208b285192aSMauro Carvalho Chehab }
209b285192aSMauro Carvalho Chehab 
210b285192aSMauro Carvalho Chehab int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
211b285192aSMauro Carvalho Chehab {
212b285192aSMauro Carvalho Chehab 	return fh->resources & bit;
213b285192aSMauro Carvalho Chehab }
214b285192aSMauro Carvalho Chehab 
215b285192aSMauro Carvalho Chehab int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
216b285192aSMauro Carvalho Chehab {
217b285192aSMauro Carvalho Chehab 	return fh->dev->channels[fh->channel_id].resources & bit;
218b285192aSMauro Carvalho Chehab }
219b285192aSMauro Carvalho Chehab 
220b285192aSMauro Carvalho Chehab void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh,
221b285192aSMauro Carvalho Chehab 		      unsigned int bits)
222b285192aSMauro Carvalho Chehab {
223b285192aSMauro Carvalho Chehab 	BUG_ON((fh->resources & bits) != bits);
224b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
225b285192aSMauro Carvalho Chehab 
226b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
227b285192aSMauro Carvalho Chehab 	fh->resources &= ~bits;
228b285192aSMauro Carvalho Chehab 	dev->channels[fh->channel_id].resources &= ~bits;
229b285192aSMauro Carvalho Chehab 	dprintk(1, "res: put %d\n", bits);
230b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
231b285192aSMauro Carvalho Chehab }
232b285192aSMauro Carvalho Chehab 
233*95c232a2SHans Verkuil static int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
234b285192aSMauro Carvalho Chehab {
235b285192aSMauro Carvalho Chehab 	struct v4l2_routing route;
236b285192aSMauro Carvalho Chehab 	memset(&route, 0, sizeof(route));
237b285192aSMauro Carvalho Chehab 
238b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
239b285192aSMauro Carvalho Chehab 		__func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
240b285192aSMauro Carvalho Chehab 		INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
241b285192aSMauro Carvalho Chehab 	dev->input = input;
242b285192aSMauro Carvalho Chehab 
243b285192aSMauro Carvalho Chehab 	route.input = INPUT(input)->vmux;
244b285192aSMauro Carvalho Chehab 
245b285192aSMauro Carvalho Chehab 	/* Tell the internal A/V decoder */
246b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
247b285192aSMauro Carvalho Chehab 
248b285192aSMauro Carvalho Chehab 	return 0;
249b285192aSMauro Carvalho Chehab }
250b285192aSMauro Carvalho Chehab 
251b285192aSMauro Carvalho Chehab int cx25821_start_video_dma(struct cx25821_dev *dev,
252b285192aSMauro Carvalho Chehab 			    struct cx25821_dmaqueue *q,
253b285192aSMauro Carvalho Chehab 			    struct cx25821_buffer *buf,
254bfef0d35SHans Verkuil 			    const struct sram_channel *channel)
255b285192aSMauro Carvalho Chehab {
256b285192aSMauro Carvalho Chehab 	int tmp = 0;
257b285192aSMauro Carvalho Chehab 
258b285192aSMauro Carvalho Chehab 	/* setup fifo + format */
259b285192aSMauro Carvalho Chehab 	cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
260b285192aSMauro Carvalho Chehab 
261b285192aSMauro Carvalho Chehab 	/* reset counter */
262b285192aSMauro Carvalho Chehab 	cx_write(channel->gpcnt_ctl, 3);
263b285192aSMauro Carvalho Chehab 	q->count = 1;
264b285192aSMauro Carvalho Chehab 
265b285192aSMauro Carvalho Chehab 	/* enable irq */
266b285192aSMauro Carvalho Chehab 	cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
267b285192aSMauro Carvalho Chehab 	cx_set(channel->int_msk, 0x11);
268b285192aSMauro Carvalho Chehab 
269b285192aSMauro Carvalho Chehab 	/* start dma */
270b285192aSMauro Carvalho Chehab 	cx_write(channel->dma_ctl, 0x11);	/* FIFO and RISC enable */
271b285192aSMauro Carvalho Chehab 
272b285192aSMauro Carvalho Chehab 	/* make sure upstream setting if any is reversed */
273b285192aSMauro Carvalho Chehab 	tmp = cx_read(VID_CH_MODE_SEL);
274b285192aSMauro Carvalho Chehab 	cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
275b285192aSMauro Carvalho Chehab 
276b285192aSMauro Carvalho Chehab 	return 0;
277b285192aSMauro Carvalho Chehab }
278b285192aSMauro Carvalho Chehab 
279dafc456cSMauro Carvalho Chehab static int cx25821_restart_video_queue(struct cx25821_dev *dev,
280b285192aSMauro Carvalho Chehab 				       struct cx25821_dmaqueue *q,
281bfef0d35SHans Verkuil 				       const struct sram_channel *channel)
282b285192aSMauro Carvalho Chehab {
283b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf, *prev;
284b285192aSMauro Carvalho Chehab 	struct list_head *item;
285b285192aSMauro Carvalho Chehab 
286b285192aSMauro Carvalho Chehab 	if (!list_empty(&q->active)) {
287b285192aSMauro Carvalho Chehab 		buf = list_entry(q->active.next, struct cx25821_buffer,
288b285192aSMauro Carvalho Chehab 				vb.queue);
289b285192aSMauro Carvalho Chehab 
290b285192aSMauro Carvalho Chehab 		cx25821_start_video_dma(dev, q, buf, channel);
291b285192aSMauro Carvalho Chehab 
292b285192aSMauro Carvalho Chehab 		list_for_each(item, &q->active) {
293b285192aSMauro Carvalho Chehab 			buf = list_entry(item, struct cx25821_buffer, vb.queue);
294b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
295b285192aSMauro Carvalho Chehab 		}
296b285192aSMauro Carvalho Chehab 
297b285192aSMauro Carvalho Chehab 		mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
298b285192aSMauro Carvalho Chehab 		return 0;
299b285192aSMauro Carvalho Chehab 	}
300b285192aSMauro Carvalho Chehab 
301b285192aSMauro Carvalho Chehab 	prev = NULL;
302b285192aSMauro Carvalho Chehab 	for (;;) {
303b285192aSMauro Carvalho Chehab 		if (list_empty(&q->queued))
304b285192aSMauro Carvalho Chehab 			return 0;
305b285192aSMauro Carvalho Chehab 
306b285192aSMauro Carvalho Chehab 		buf = list_entry(q->queued.next, struct cx25821_buffer,
307b285192aSMauro Carvalho Chehab 				vb.queue);
308b285192aSMauro Carvalho Chehab 
309b285192aSMauro Carvalho Chehab 		if (NULL == prev) {
310b285192aSMauro Carvalho Chehab 			list_move_tail(&buf->vb.queue, &q->active);
311b285192aSMauro Carvalho Chehab 			cx25821_start_video_dma(dev, q, buf, channel);
312b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
313b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
314b285192aSMauro Carvalho Chehab 			mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
315b285192aSMauro Carvalho Chehab 		} else if (prev->vb.width == buf->vb.width &&
316b285192aSMauro Carvalho Chehab 			   prev->vb.height == buf->vb.height &&
317b285192aSMauro Carvalho Chehab 			   prev->fmt == buf->fmt) {
318b285192aSMauro Carvalho Chehab 			list_move_tail(&buf->vb.queue, &q->active);
319b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
320b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
321b285192aSMauro Carvalho Chehab 			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
322b285192aSMauro Carvalho Chehab 			prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
323b285192aSMauro Carvalho Chehab 		} else {
324b285192aSMauro Carvalho Chehab 			return 0;
325b285192aSMauro Carvalho Chehab 		}
326b285192aSMauro Carvalho Chehab 		prev = buf;
327b285192aSMauro Carvalho Chehab 	}
328b285192aSMauro Carvalho Chehab }
329b285192aSMauro Carvalho Chehab 
330dafc456cSMauro Carvalho Chehab static void cx25821_vid_timeout(unsigned long data)
331b285192aSMauro Carvalho Chehab {
332b285192aSMauro Carvalho Chehab 	struct cx25821_data *timeout_data = (struct cx25821_data *)data;
333b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = timeout_data->dev;
334bfef0d35SHans Verkuil 	const struct sram_channel *channel = timeout_data->channel;
335b285192aSMauro Carvalho Chehab 	struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq;
336b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf;
337b285192aSMauro Carvalho Chehab 	unsigned long flags;
338b285192aSMauro Carvalho Chehab 
339b285192aSMauro Carvalho Chehab 	/* cx25821_sram_channel_dump(dev, channel); */
340b285192aSMauro Carvalho Chehab 	cx_clear(channel->dma_ctl, 0x11);
341b285192aSMauro Carvalho Chehab 
342b285192aSMauro Carvalho Chehab 	spin_lock_irqsave(&dev->slock, flags);
343b285192aSMauro Carvalho Chehab 	while (!list_empty(&q->active)) {
344b285192aSMauro Carvalho Chehab 		buf = list_entry(q->active.next, struct cx25821_buffer,
345b285192aSMauro Carvalho Chehab 				vb.queue);
346b285192aSMauro Carvalho Chehab 		list_del(&buf->vb.queue);
347b285192aSMauro Carvalho Chehab 
348b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_ERROR;
349b285192aSMauro Carvalho Chehab 		wake_up(&buf->vb.done);
350b285192aSMauro Carvalho Chehab 	}
351b285192aSMauro Carvalho Chehab 
352b285192aSMauro Carvalho Chehab 	cx25821_restart_video_queue(dev, q, channel);
353b285192aSMauro Carvalho Chehab 	spin_unlock_irqrestore(&dev->slock, flags);
354b285192aSMauro Carvalho Chehab }
355b285192aSMauro Carvalho Chehab 
356b285192aSMauro Carvalho Chehab int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
357b285192aSMauro Carvalho Chehab {
358b285192aSMauro Carvalho Chehab 	u32 count = 0;
359b285192aSMauro Carvalho Chehab 	int handled = 0;
360b285192aSMauro Carvalho Chehab 	u32 mask;
361bfef0d35SHans Verkuil 	const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
362b285192aSMauro Carvalho Chehab 
363b285192aSMauro Carvalho Chehab 	mask = cx_read(channel->int_msk);
364b285192aSMauro Carvalho Chehab 	if (0 == (status & mask))
365b285192aSMauro Carvalho Chehab 		return handled;
366b285192aSMauro Carvalho Chehab 
367b285192aSMauro Carvalho Chehab 	cx_write(channel->int_stat, status);
368b285192aSMauro Carvalho Chehab 
369b285192aSMauro Carvalho Chehab 	/* risc op code error */
370b285192aSMauro Carvalho Chehab 	if (status & (1 << 16)) {
371b285192aSMauro Carvalho Chehab 		pr_warn("%s, %s: video risc op code error\n",
372b285192aSMauro Carvalho Chehab 			dev->name, channel->name);
373b285192aSMauro Carvalho Chehab 		cx_clear(channel->dma_ctl, 0x11);
374b285192aSMauro Carvalho Chehab 		cx25821_sram_channel_dump(dev, channel);
375b285192aSMauro Carvalho Chehab 	}
376b285192aSMauro Carvalho Chehab 
377b285192aSMauro Carvalho Chehab 	/* risc1 y */
378b285192aSMauro Carvalho Chehab 	if (status & FLD_VID_DST_RISC1) {
379b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
380b285192aSMauro Carvalho Chehab 		count = cx_read(channel->gpcnt);
381b285192aSMauro Carvalho Chehab 		cx25821_video_wakeup(dev, &dev->channels[channel->i].vidq,
382b285192aSMauro Carvalho Chehab 				count);
383b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
384b285192aSMauro Carvalho Chehab 		handled++;
385b285192aSMauro Carvalho Chehab 	}
386b285192aSMauro Carvalho Chehab 
387b285192aSMauro Carvalho Chehab 	/* risc2 y */
388b285192aSMauro Carvalho Chehab 	if (status & 0x10) {
389b285192aSMauro Carvalho Chehab 		dprintk(2, "stopper video\n");
390b285192aSMauro Carvalho Chehab 		spin_lock(&dev->slock);
391b285192aSMauro Carvalho Chehab 		cx25821_restart_video_queue(dev,
392b285192aSMauro Carvalho Chehab 				&dev->channels[channel->i].vidq, channel);
393b285192aSMauro Carvalho Chehab 		spin_unlock(&dev->slock);
394b285192aSMauro Carvalho Chehab 		handled++;
395b285192aSMauro Carvalho Chehab 	}
396b285192aSMauro Carvalho Chehab 	return handled;
397b285192aSMauro Carvalho Chehab }
398b285192aSMauro Carvalho Chehab 
399*95c232a2SHans Verkuil static int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
400b285192aSMauro Carvalho Chehab 		 unsigned int *size)
401b285192aSMauro Carvalho Chehab {
402b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = q->priv_data;
403b285192aSMauro Carvalho Chehab 
404b285192aSMauro Carvalho Chehab 	*size = fh->fmt->depth * fh->width * fh->height >> 3;
405b285192aSMauro Carvalho Chehab 
406b285192aSMauro Carvalho Chehab 	if (0 == *count)
407b285192aSMauro Carvalho Chehab 		*count = 32;
408b285192aSMauro Carvalho Chehab 
409b285192aSMauro Carvalho Chehab 	if (*size * *count > vid_limit * 1024 * 1024)
410b285192aSMauro Carvalho Chehab 		*count = (vid_limit * 1024 * 1024) / *size;
411b285192aSMauro Carvalho Chehab 
412b285192aSMauro Carvalho Chehab 	return 0;
413b285192aSMauro Carvalho Chehab }
414b285192aSMauro Carvalho Chehab 
415*95c232a2SHans Verkuil static int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
416b285192aSMauro Carvalho Chehab 		   enum v4l2_field field)
417b285192aSMauro Carvalho Chehab {
418b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = q->priv_data;
419b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
420b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
421b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
422b285192aSMauro Carvalho Chehab 	int rc, init_buffer = 0;
423b285192aSMauro Carvalho Chehab 	u32 line0_offset;
424b285192aSMauro Carvalho Chehab 	struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
425b285192aSMauro Carvalho Chehab 	int bpl_local = LINE_SIZE_D1;
426b285192aSMauro Carvalho Chehab 	int channel_opened = fh->channel_id;
427b285192aSMauro Carvalho Chehab 
428b285192aSMauro Carvalho Chehab 	BUG_ON(NULL == fh->fmt);
429b285192aSMauro Carvalho Chehab 	if (fh->width < 48 || fh->width > 720 ||
430b285192aSMauro Carvalho Chehab 	    fh->height < 32 || fh->height > 576)
431b285192aSMauro Carvalho Chehab 		return -EINVAL;
432b285192aSMauro Carvalho Chehab 
433b285192aSMauro Carvalho Chehab 	buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
434b285192aSMauro Carvalho Chehab 
435b285192aSMauro Carvalho Chehab 	if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
436b285192aSMauro Carvalho Chehab 		return -EINVAL;
437b285192aSMauro Carvalho Chehab 
438b285192aSMauro Carvalho Chehab 	if (buf->fmt != fh->fmt ||
439b285192aSMauro Carvalho Chehab 	    buf->vb.width != fh->width ||
440b285192aSMauro Carvalho Chehab 	    buf->vb.height != fh->height || buf->vb.field != field) {
441b285192aSMauro Carvalho Chehab 		buf->fmt = fh->fmt;
442b285192aSMauro Carvalho Chehab 		buf->vb.width = fh->width;
443b285192aSMauro Carvalho Chehab 		buf->vb.height = fh->height;
444b285192aSMauro Carvalho Chehab 		buf->vb.field = field;
445b285192aSMauro Carvalho Chehab 		init_buffer = 1;
446b285192aSMauro Carvalho Chehab 	}
447b285192aSMauro Carvalho Chehab 
448b285192aSMauro Carvalho Chehab 	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
449b285192aSMauro Carvalho Chehab 		init_buffer = 1;
450b285192aSMauro Carvalho Chehab 		rc = videobuf_iolock(q, &buf->vb, NULL);
451b285192aSMauro Carvalho Chehab 		if (0 != rc) {
452b285192aSMauro Carvalho Chehab 			printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
453b285192aSMauro Carvalho Chehab 			goto fail;
454b285192aSMauro Carvalho Chehab 		}
455b285192aSMauro Carvalho Chehab 	}
456b285192aSMauro Carvalho Chehab 
457b285192aSMauro Carvalho Chehab 	dprintk(1, "init_buffer=%d\n", init_buffer);
458b285192aSMauro Carvalho Chehab 
459b285192aSMauro Carvalho Chehab 	if (init_buffer) {
460b285192aSMauro Carvalho Chehab 
461b285192aSMauro Carvalho Chehab 		channel_opened = dev->channel_opened;
462b285192aSMauro Carvalho Chehab 		if (channel_opened < 0 || channel_opened > 7)
463b285192aSMauro Carvalho Chehab 			channel_opened = 7;
464b285192aSMauro Carvalho Chehab 
465b285192aSMauro Carvalho Chehab 		if (dev->channels[channel_opened].pixel_formats ==
466b285192aSMauro Carvalho Chehab 				PIXEL_FRMT_411)
467b285192aSMauro Carvalho Chehab 			buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
468b285192aSMauro Carvalho Chehab 		else
469b285192aSMauro Carvalho Chehab 			buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
470b285192aSMauro Carvalho Chehab 
471b285192aSMauro Carvalho Chehab 		if (dev->channels[channel_opened].pixel_formats ==
472b285192aSMauro Carvalho Chehab 				PIXEL_FRMT_411) {
473b285192aSMauro Carvalho Chehab 			bpl_local = buf->bpl;
474b285192aSMauro Carvalho Chehab 		} else {
475b285192aSMauro Carvalho Chehab 			bpl_local = buf->bpl;   /* Default */
476b285192aSMauro Carvalho Chehab 
477b285192aSMauro Carvalho Chehab 			if (channel_opened >= 0 && channel_opened <= 7) {
478b285192aSMauro Carvalho Chehab 				if (dev->channels[channel_opened]
479b285192aSMauro Carvalho Chehab 						.use_cif_resolution) {
480b285192aSMauro Carvalho Chehab 					if (dev->tvnorm & V4L2_STD_PAL_BG ||
481b285192aSMauro Carvalho Chehab 					    dev->tvnorm & V4L2_STD_PAL_DK)
482b285192aSMauro Carvalho Chehab 						bpl_local = 352 << 1;
483b285192aSMauro Carvalho Chehab 					else
484b285192aSMauro Carvalho Chehab 						bpl_local = dev->channels[
485b285192aSMauro Carvalho Chehab 							channel_opened].
486b285192aSMauro Carvalho Chehab 							cif_width << 1;
487b285192aSMauro Carvalho Chehab 				}
488b285192aSMauro Carvalho Chehab 			}
489b285192aSMauro Carvalho Chehab 		}
490b285192aSMauro Carvalho Chehab 
491b285192aSMauro Carvalho Chehab 		switch (buf->vb.field) {
492b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_TOP:
493b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
494b285192aSMauro Carvalho Chehab 					    dma->sglist, 0, UNSET,
495b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height);
496b285192aSMauro Carvalho Chehab 			break;
497b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_BOTTOM:
498b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
499b285192aSMauro Carvalho Chehab 					    dma->sglist, UNSET, 0,
500b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height);
501b285192aSMauro Carvalho Chehab 			break;
502b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_INTERLACED:
503b285192aSMauro Carvalho Chehab 			/* All other formats are top field first */
504b285192aSMauro Carvalho Chehab 			line0_offset = 0;
505b285192aSMauro Carvalho Chehab 			dprintk(1, "top field first\n");
506b285192aSMauro Carvalho Chehab 
507b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
508b285192aSMauro Carvalho Chehab 					    dma->sglist, line0_offset,
509b285192aSMauro Carvalho Chehab 					    bpl_local, bpl_local, bpl_local,
510b285192aSMauro Carvalho Chehab 					    buf->vb.height >> 1);
511b285192aSMauro Carvalho Chehab 			break;
512b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_SEQ_TB:
513b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
514b285192aSMauro Carvalho Chehab 					    dma->sglist,
515b285192aSMauro Carvalho Chehab 					    0, buf->bpl * (buf->vb.height >> 1),
516b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height >> 1);
517b285192aSMauro Carvalho Chehab 			break;
518b285192aSMauro Carvalho Chehab 		case V4L2_FIELD_SEQ_BT:
519b285192aSMauro Carvalho Chehab 			cx25821_risc_buffer(dev->pci, &buf->risc,
520b285192aSMauro Carvalho Chehab 					    dma->sglist,
521b285192aSMauro Carvalho Chehab 					    buf->bpl * (buf->vb.height >> 1), 0,
522b285192aSMauro Carvalho Chehab 					    buf->bpl, 0, buf->vb.height >> 1);
523b285192aSMauro Carvalho Chehab 			break;
524b285192aSMauro Carvalho Chehab 		default:
525b285192aSMauro Carvalho Chehab 			BUG();
526b285192aSMauro Carvalho Chehab 		}
527b285192aSMauro Carvalho Chehab 	}
528b285192aSMauro Carvalho Chehab 
529b285192aSMauro Carvalho Chehab 	dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
530b285192aSMauro Carvalho Chehab 		buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
531b285192aSMauro Carvalho Chehab 		fh->fmt->name, (unsigned long)buf->risc.dma);
532b285192aSMauro Carvalho Chehab 
533b285192aSMauro Carvalho Chehab 	buf->vb.state = VIDEOBUF_PREPARED;
534b285192aSMauro Carvalho Chehab 
535b285192aSMauro Carvalho Chehab 	return 0;
536b285192aSMauro Carvalho Chehab 
537b285192aSMauro Carvalho Chehab fail:
538b285192aSMauro Carvalho Chehab 	cx25821_free_buffer(q, buf);
539b285192aSMauro Carvalho Chehab 	return rc;
540b285192aSMauro Carvalho Chehab }
541b285192aSMauro Carvalho Chehab 
542*95c232a2SHans Verkuil static void cx25821_buffer_release(struct videobuf_queue *q,
543b285192aSMauro Carvalho Chehab 			    struct videobuf_buffer *vb)
544b285192aSMauro Carvalho Chehab {
545b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
546b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
547b285192aSMauro Carvalho Chehab 
548b285192aSMauro Carvalho Chehab 	cx25821_free_buffer(q, buf);
549b285192aSMauro Carvalho Chehab }
550b285192aSMauro Carvalho Chehab 
551*95c232a2SHans Verkuil static struct videobuf_queue *get_queue(struct cx25821_fh *fh)
552b285192aSMauro Carvalho Chehab {
553b285192aSMauro Carvalho Chehab 	switch (fh->type) {
554b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
555b285192aSMauro Carvalho Chehab 		return &fh->vidq;
556b285192aSMauro Carvalho Chehab 	default:
557b285192aSMauro Carvalho Chehab 		BUG();
558b285192aSMauro Carvalho Chehab 		return NULL;
559b285192aSMauro Carvalho Chehab 	}
560b285192aSMauro Carvalho Chehab }
561b285192aSMauro Carvalho Chehab 
562*95c232a2SHans Verkuil static int cx25821_get_resource(struct cx25821_fh *fh, int resource)
563b285192aSMauro Carvalho Chehab {
564b285192aSMauro Carvalho Chehab 	switch (fh->type) {
565b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
566b285192aSMauro Carvalho Chehab 		return resource;
567b285192aSMauro Carvalho Chehab 	default:
568b285192aSMauro Carvalho Chehab 		BUG();
569b285192aSMauro Carvalho Chehab 		return 0;
570b285192aSMauro Carvalho Chehab 	}
571b285192aSMauro Carvalho Chehab }
572b285192aSMauro Carvalho Chehab 
573*95c232a2SHans Verkuil static int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
574b285192aSMauro Carvalho Chehab {
575b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
576b285192aSMauro Carvalho Chehab 
577b285192aSMauro Carvalho Chehab 	return videobuf_mmap_mapper(get_queue(fh), vma);
578b285192aSMauro Carvalho Chehab }
579b285192aSMauro Carvalho Chehab 
580b285192aSMauro Carvalho Chehab 
581b285192aSMauro Carvalho Chehab static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
582b285192aSMauro Carvalho Chehab {
583b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf =
584b285192aSMauro Carvalho Chehab 		container_of(vb, struct cx25821_buffer, vb);
585b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *prev;
586b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = vq->priv_data;
587b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
588b285192aSMauro Carvalho Chehab 	struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq;
589b285192aSMauro Carvalho Chehab 
590b285192aSMauro Carvalho Chehab 	/* add jump to stopper */
591b285192aSMauro Carvalho Chehab 	buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
592b285192aSMauro Carvalho Chehab 	buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
593b285192aSMauro Carvalho Chehab 	buf->risc.jmp[2] = cpu_to_le32(0);      /* bits 63-32 */
594b285192aSMauro Carvalho Chehab 
595b285192aSMauro Carvalho Chehab 	dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
596b285192aSMauro Carvalho Chehab 
597b285192aSMauro Carvalho Chehab 	if (!list_empty(&q->queued)) {
598b285192aSMauro Carvalho Chehab 		list_add_tail(&buf->vb.queue, &q->queued);
599b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_QUEUED;
600b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
601b285192aSMauro Carvalho Chehab 				buf->vb.i);
602b285192aSMauro Carvalho Chehab 
603b285192aSMauro Carvalho Chehab 	} else if (list_empty(&q->active)) {
604b285192aSMauro Carvalho Chehab 		list_add_tail(&buf->vb.queue, &q->active);
605b285192aSMauro Carvalho Chehab 		cx25821_start_video_dma(dev, q, buf,
606b285192aSMauro Carvalho Chehab 				dev->channels[fh->channel_id].sram_channels);
607b285192aSMauro Carvalho Chehab 		buf->vb.state = VIDEOBUF_ACTIVE;
608b285192aSMauro Carvalho Chehab 		buf->count = q->count++;
609b285192aSMauro Carvalho Chehab 		mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
610b285192aSMauro Carvalho Chehab 		dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
611b285192aSMauro Carvalho Chehab 				buf, buf->vb.i, buf->count, q->count);
612b285192aSMauro Carvalho Chehab 	} else {
613b285192aSMauro Carvalho Chehab 		prev = list_entry(q->active.prev, struct cx25821_buffer,
614b285192aSMauro Carvalho Chehab 				vb.queue);
615b285192aSMauro Carvalho Chehab 		if (prev->vb.width == buf->vb.width
616b285192aSMauro Carvalho Chehab 		   && prev->vb.height == buf->vb.height
617b285192aSMauro Carvalho Chehab 		   && prev->fmt == buf->fmt) {
618b285192aSMauro Carvalho Chehab 			list_add_tail(&buf->vb.queue, &q->active);
619b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_ACTIVE;
620b285192aSMauro Carvalho Chehab 			buf->count = q->count++;
621b285192aSMauro Carvalho Chehab 			prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
622b285192aSMauro Carvalho Chehab 
623b285192aSMauro Carvalho Chehab 			/* 64 bit bits 63-32 */
624b285192aSMauro Carvalho Chehab 			prev->risc.jmp[2] = cpu_to_le32(0);
625b285192aSMauro Carvalho Chehab 			dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
626b285192aSMauro Carvalho Chehab 					buf, buf->vb.i, buf->count);
627b285192aSMauro Carvalho Chehab 
628b285192aSMauro Carvalho Chehab 		} else {
629b285192aSMauro Carvalho Chehab 			list_add_tail(&buf->vb.queue, &q->queued);
630b285192aSMauro Carvalho Chehab 			buf->vb.state = VIDEOBUF_QUEUED;
631b285192aSMauro Carvalho Chehab 			dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
632b285192aSMauro Carvalho Chehab 					buf->vb.i);
633b285192aSMauro Carvalho Chehab 		}
634b285192aSMauro Carvalho Chehab 	}
635b285192aSMauro Carvalho Chehab 
636b285192aSMauro Carvalho Chehab 	if (list_empty(&q->active))
637b285192aSMauro Carvalho Chehab 		dprintk(2, "active queue empty!\n");
638b285192aSMauro Carvalho Chehab }
639b285192aSMauro Carvalho Chehab 
640b285192aSMauro Carvalho Chehab static struct videobuf_queue_ops cx25821_video_qops = {
641b285192aSMauro Carvalho Chehab 	.buf_setup = cx25821_buffer_setup,
642b285192aSMauro Carvalho Chehab 	.buf_prepare = cx25821_buffer_prepare,
643b285192aSMauro Carvalho Chehab 	.buf_queue = buffer_queue,
644b285192aSMauro Carvalho Chehab 	.buf_release = cx25821_buffer_release,
645b285192aSMauro Carvalho Chehab };
646b285192aSMauro Carvalho Chehab 
647b285192aSMauro Carvalho Chehab static int video_open(struct file *file)
648b285192aSMauro Carvalho Chehab {
649b285192aSMauro Carvalho Chehab 	struct video_device *vdev = video_devdata(file);
650a8f35ce3SHans Verkuil 	struct cx25821_dev *dev = video_drvdata(file);
651b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh;
652b285192aSMauro Carvalho Chehab 	enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
653b285192aSMauro Carvalho Chehab 	u32 pix_format;
654a8f35ce3SHans Verkuil 	int ch_id;
655b285192aSMauro Carvalho Chehab 
656b285192aSMauro Carvalho Chehab 	dprintk(1, "open dev=%s type=%s\n", video_device_node_name(vdev),
657b285192aSMauro Carvalho Chehab 			v4l2_type_names[type]);
658b285192aSMauro Carvalho Chehab 
659a8f35ce3SHans Verkuil 	for (ch_id = 0; ch_id < MAX_VID_CHANNEL_NUM - 1; ch_id++)
660a8f35ce3SHans Verkuil 		if (dev->channels[ch_id].video_dev == vdev)
661a8f35ce3SHans Verkuil 			break;
662a8f35ce3SHans Verkuil 
663a8f35ce3SHans Verkuil 	/* Can't happen */
664a8f35ce3SHans Verkuil 	if (ch_id >= MAX_VID_CHANNEL_NUM - 1)
665a8f35ce3SHans Verkuil 		return -ENODEV;
666a8f35ce3SHans Verkuil 
667b285192aSMauro Carvalho Chehab 	/* allocate + initialize per filehandle data */
668b285192aSMauro Carvalho Chehab 	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
669b285192aSMauro Carvalho Chehab 	if (NULL == fh)
670b285192aSMauro Carvalho Chehab 		return -ENOMEM;
671b285192aSMauro Carvalho Chehab 
672b285192aSMauro Carvalho Chehab 	file->private_data = fh;
673b285192aSMauro Carvalho Chehab 	fh->dev = dev;
674b285192aSMauro Carvalho Chehab 	fh->type = type;
675b285192aSMauro Carvalho Chehab 	fh->width = 720;
676b285192aSMauro Carvalho Chehab 	fh->channel_id = ch_id;
677b285192aSMauro Carvalho Chehab 
678b285192aSMauro Carvalho Chehab 	if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
679b285192aSMauro Carvalho Chehab 		fh->height = 576;
680b285192aSMauro Carvalho Chehab 	else
681b285192aSMauro Carvalho Chehab 		fh->height = 480;
682b285192aSMauro Carvalho Chehab 
683b285192aSMauro Carvalho Chehab 	dev->channel_opened = fh->channel_id;
684b285192aSMauro Carvalho Chehab 	if (dev->channels[ch_id].pixel_formats == PIXEL_FRMT_411)
685b285192aSMauro Carvalho Chehab 		pix_format = V4L2_PIX_FMT_Y41P;
686b285192aSMauro Carvalho Chehab 	else
687b285192aSMauro Carvalho Chehab 		pix_format = V4L2_PIX_FMT_YUYV;
688b285192aSMauro Carvalho Chehab 	fh->fmt = cx25821_format_by_fourcc(pix_format);
689b285192aSMauro Carvalho Chehab 
690b285192aSMauro Carvalho Chehab 	v4l2_prio_open(&dev->channels[ch_id].prio, &fh->prio);
691b285192aSMauro Carvalho Chehab 
692b285192aSMauro Carvalho Chehab 	videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops, &dev->pci->dev,
693b285192aSMauro Carvalho Chehab 			&dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
694b285192aSMauro Carvalho Chehab 			V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
695b285192aSMauro Carvalho Chehab 			fh, NULL);
696b285192aSMauro Carvalho Chehab 
697b285192aSMauro Carvalho Chehab 	dprintk(1, "post videobuf_queue_init()\n");
698b285192aSMauro Carvalho Chehab 
699b285192aSMauro Carvalho Chehab 	return 0;
700b285192aSMauro Carvalho Chehab }
701b285192aSMauro Carvalho Chehab 
702b285192aSMauro Carvalho Chehab static ssize_t video_read(struct file *file, char __user * data, size_t count,
703b285192aSMauro Carvalho Chehab 			 loff_t *ppos)
704b285192aSMauro Carvalho Chehab {
705b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
706b285192aSMauro Carvalho Chehab 
707b285192aSMauro Carvalho Chehab 	switch (fh->type) {
708b285192aSMauro Carvalho Chehab 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
709b285192aSMauro Carvalho Chehab 		if (cx25821_res_locked(fh, RESOURCE_VIDEO0))
710b285192aSMauro Carvalho Chehab 			return -EBUSY;
711b285192aSMauro Carvalho Chehab 
712b285192aSMauro Carvalho Chehab 		return videobuf_read_one(&fh->vidq, data, count, ppos,
713b285192aSMauro Carvalho Chehab 					file->f_flags & O_NONBLOCK);
714b285192aSMauro Carvalho Chehab 
715b285192aSMauro Carvalho Chehab 	default:
716b285192aSMauro Carvalho Chehab 		BUG();
717b285192aSMauro Carvalho Chehab 		return 0;
718b285192aSMauro Carvalho Chehab 	}
719b285192aSMauro Carvalho Chehab }
720b285192aSMauro Carvalho Chehab 
721b285192aSMauro Carvalho Chehab static unsigned int video_poll(struct file *file,
722b285192aSMauro Carvalho Chehab 			      struct poll_table_struct *wait)
723b285192aSMauro Carvalho Chehab {
724b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
725b285192aSMauro Carvalho Chehab 	struct cx25821_buffer *buf;
726b285192aSMauro Carvalho Chehab 
727b285192aSMauro Carvalho Chehab 	if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
728b285192aSMauro Carvalho Chehab 		/* streaming capture */
729b285192aSMauro Carvalho Chehab 		if (list_empty(&fh->vidq.stream))
730b285192aSMauro Carvalho Chehab 			return POLLERR;
731b285192aSMauro Carvalho Chehab 		buf = list_entry(fh->vidq.stream.next,
732b285192aSMauro Carvalho Chehab 				struct cx25821_buffer, vb.stream);
733b285192aSMauro Carvalho Chehab 	} else {
734b285192aSMauro Carvalho Chehab 		/* read() capture */
735b285192aSMauro Carvalho Chehab 		buf = (struct cx25821_buffer *)fh->vidq.read_buf;
736b285192aSMauro Carvalho Chehab 		if (NULL == buf)
737b285192aSMauro Carvalho Chehab 			return POLLERR;
738b285192aSMauro Carvalho Chehab 	}
739b285192aSMauro Carvalho Chehab 
740b285192aSMauro Carvalho Chehab 	poll_wait(file, &buf->vb.done, wait);
741b285192aSMauro Carvalho Chehab 	if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
742b285192aSMauro Carvalho Chehab 		if (buf->vb.state == VIDEOBUF_DONE) {
743b285192aSMauro Carvalho Chehab 			struct cx25821_dev *dev = fh->dev;
744b285192aSMauro Carvalho Chehab 
745b285192aSMauro Carvalho Chehab 			if (dev && dev->channels[fh->channel_id]
746b285192aSMauro Carvalho Chehab 					.use_cif_resolution) {
747b285192aSMauro Carvalho Chehab 				u8 cam_id = *((char *)buf->vb.baddr + 3);
748b285192aSMauro Carvalho Chehab 				memcpy((char *)buf->vb.baddr,
749b285192aSMauro Carvalho Chehab 				      (char *)buf->vb.baddr + (fh->width * 2),
750b285192aSMauro Carvalho Chehab 				      (fh->width * 2));
751b285192aSMauro Carvalho Chehab 				*((char *)buf->vb.baddr + 3) = cam_id;
752b285192aSMauro Carvalho Chehab 			}
753b285192aSMauro Carvalho Chehab 		}
754b285192aSMauro Carvalho Chehab 
755b285192aSMauro Carvalho Chehab 		return POLLIN | POLLRDNORM;
756b285192aSMauro Carvalho Chehab 	}
757b285192aSMauro Carvalho Chehab 
758b285192aSMauro Carvalho Chehab 	return 0;
759b285192aSMauro Carvalho Chehab }
760b285192aSMauro Carvalho Chehab 
761b285192aSMauro Carvalho Chehab static int video_release(struct file *file)
762b285192aSMauro Carvalho Chehab {
763b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
764b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
765bfef0d35SHans Verkuil 	const struct sram_channel *sram_ch =
766bfef0d35SHans Verkuil 		dev->channels[0].sram_channels;
767b285192aSMauro Carvalho Chehab 
768b285192aSMauro Carvalho Chehab 	/* stop the risc engine and fifo */
769bfef0d35SHans Verkuil 	cx_write(sram_ch->dma_ctl, 0); /* FIFO and RISC disable */
770b285192aSMauro Carvalho Chehab 
771b285192aSMauro Carvalho Chehab 	/* stop video capture */
772b285192aSMauro Carvalho Chehab 	if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
773b285192aSMauro Carvalho Chehab 		videobuf_queue_cancel(&fh->vidq);
774b285192aSMauro Carvalho Chehab 		cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
775b285192aSMauro Carvalho Chehab 	}
776b285192aSMauro Carvalho Chehab 
777b285192aSMauro Carvalho Chehab 	if (fh->vidq.read_buf) {
778b285192aSMauro Carvalho Chehab 		cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
779b285192aSMauro Carvalho Chehab 		kfree(fh->vidq.read_buf);
780b285192aSMauro Carvalho Chehab 	}
781b285192aSMauro Carvalho Chehab 
782b285192aSMauro Carvalho Chehab 	videobuf_mmap_free(&fh->vidq);
783b285192aSMauro Carvalho Chehab 
784b285192aSMauro Carvalho Chehab 	v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
785b285192aSMauro Carvalho Chehab 	file->private_data = NULL;
786b285192aSMauro Carvalho Chehab 	kfree(fh);
787b285192aSMauro Carvalho Chehab 
788b285192aSMauro Carvalho Chehab 	return 0;
789b285192aSMauro Carvalho Chehab }
790b285192aSMauro Carvalho Chehab 
791*95c232a2SHans Verkuil /* VIDEO IOCTLS */
792*95c232a2SHans Verkuil static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
793*95c232a2SHans Verkuil 				 struct v4l2_format *f)
794*95c232a2SHans Verkuil {
795*95c232a2SHans Verkuil 	struct cx25821_fh *fh = priv;
796*95c232a2SHans Verkuil 
797*95c232a2SHans Verkuil 	f->fmt.pix.width = fh->width;
798*95c232a2SHans Verkuil 	f->fmt.pix.height = fh->height;
799*95c232a2SHans Verkuil 	f->fmt.pix.field = fh->vidq.field;
800*95c232a2SHans Verkuil 	f->fmt.pix.pixelformat = fh->fmt->fourcc;
801*95c232a2SHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
802*95c232a2SHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
803*95c232a2SHans Verkuil 
804*95c232a2SHans Verkuil 	return 0;
805*95c232a2SHans Verkuil }
806*95c232a2SHans Verkuil 
807*95c232a2SHans Verkuil static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
808*95c232a2SHans Verkuil 				   struct v4l2_format *f)
809*95c232a2SHans Verkuil {
810*95c232a2SHans Verkuil 	const struct cx25821_fmt *fmt;
811*95c232a2SHans Verkuil 	enum v4l2_field field;
812*95c232a2SHans Verkuil 	unsigned int maxw, maxh;
813*95c232a2SHans Verkuil 
814*95c232a2SHans Verkuil 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
815*95c232a2SHans Verkuil 	if (NULL == fmt)
816*95c232a2SHans Verkuil 		return -EINVAL;
817*95c232a2SHans Verkuil 
818*95c232a2SHans Verkuil 	field = f->fmt.pix.field;
819*95c232a2SHans Verkuil 	maxw = 720;
820*95c232a2SHans Verkuil 	maxh = 576;
821*95c232a2SHans Verkuil 
822*95c232a2SHans Verkuil 	if (V4L2_FIELD_ANY == field) {
823*95c232a2SHans Verkuil 		if (f->fmt.pix.height > maxh / 2)
824*95c232a2SHans Verkuil 			field = V4L2_FIELD_INTERLACED;
825*95c232a2SHans Verkuil 		else
826*95c232a2SHans Verkuil 			field = V4L2_FIELD_TOP;
827*95c232a2SHans Verkuil 	}
828*95c232a2SHans Verkuil 
829*95c232a2SHans Verkuil 	switch (field) {
830*95c232a2SHans Verkuil 	case V4L2_FIELD_TOP:
831*95c232a2SHans Verkuil 	case V4L2_FIELD_BOTTOM:
832*95c232a2SHans Verkuil 		maxh = maxh / 2;
833*95c232a2SHans Verkuil 		break;
834*95c232a2SHans Verkuil 	case V4L2_FIELD_INTERLACED:
835*95c232a2SHans Verkuil 		break;
836*95c232a2SHans Verkuil 	default:
837*95c232a2SHans Verkuil 		return -EINVAL;
838*95c232a2SHans Verkuil 	}
839*95c232a2SHans Verkuil 
840*95c232a2SHans Verkuil 	f->fmt.pix.field = field;
841*95c232a2SHans Verkuil 	if (f->fmt.pix.height < 32)
842*95c232a2SHans Verkuil 		f->fmt.pix.height = 32;
843*95c232a2SHans Verkuil 	if (f->fmt.pix.height > maxh)
844*95c232a2SHans Verkuil 		f->fmt.pix.height = maxh;
845*95c232a2SHans Verkuil 	if (f->fmt.pix.width < 48)
846*95c232a2SHans Verkuil 		f->fmt.pix.width = 48;
847*95c232a2SHans Verkuil 	if (f->fmt.pix.width > maxw)
848*95c232a2SHans Verkuil 		f->fmt.pix.width = maxw;
849*95c232a2SHans Verkuil 	f->fmt.pix.width &= ~0x03;
850*95c232a2SHans Verkuil 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
851*95c232a2SHans Verkuil 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
852*95c232a2SHans Verkuil 
853*95c232a2SHans Verkuil 	return 0;
854*95c232a2SHans Verkuil }
855b285192aSMauro Carvalho Chehab static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
856b285192aSMauro Carvalho Chehab {
857b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
858b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
859b285192aSMauro Carvalho Chehab 
860b285192aSMauro Carvalho Chehab 	if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
861b285192aSMauro Carvalho Chehab 		return -EINVAL;
862b285192aSMauro Carvalho Chehab 
863b285192aSMauro Carvalho Chehab 	if (unlikely(i != fh->type))
864b285192aSMauro Carvalho Chehab 		return -EINVAL;
865b285192aSMauro Carvalho Chehab 
866b285192aSMauro Carvalho Chehab 	if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh,
867b285192aSMauro Carvalho Chehab 						RESOURCE_VIDEO0))))
868b285192aSMauro Carvalho Chehab 		return -EBUSY;
869b285192aSMauro Carvalho Chehab 
870b285192aSMauro Carvalho Chehab 	return videobuf_streamon(get_queue(fh));
871b285192aSMauro Carvalho Chehab }
872b285192aSMauro Carvalho Chehab 
873b285192aSMauro Carvalho Chehab static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
874b285192aSMauro Carvalho Chehab {
875b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
876b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
877b285192aSMauro Carvalho Chehab 	int err, res;
878b285192aSMauro Carvalho Chehab 
879b285192aSMauro Carvalho Chehab 	if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
880b285192aSMauro Carvalho Chehab 		return -EINVAL;
881b285192aSMauro Carvalho Chehab 	if (i != fh->type)
882b285192aSMauro Carvalho Chehab 		return -EINVAL;
883b285192aSMauro Carvalho Chehab 
884b285192aSMauro Carvalho Chehab 	res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
885b285192aSMauro Carvalho Chehab 	err = videobuf_streamoff(get_queue(fh));
886b285192aSMauro Carvalho Chehab 	if (err < 0)
887b285192aSMauro Carvalho Chehab 		return err;
888b285192aSMauro Carvalho Chehab 	cx25821_res_free(dev, fh, res);
889b285192aSMauro Carvalho Chehab 	return 0;
890b285192aSMauro Carvalho Chehab }
891b285192aSMauro Carvalho Chehab 
892*95c232a2SHans Verkuil static int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
893*95c232a2SHans Verkuil {
894*95c232a2SHans Verkuil 	if (tvnorm == V4L2_STD_PAL_BG) {
895*95c232a2SHans Verkuil 		if (width == 352 || width == 720)
896*95c232a2SHans Verkuil 			return 1;
897*95c232a2SHans Verkuil 		else
898*95c232a2SHans Verkuil 			return 0;
899*95c232a2SHans Verkuil 	}
900*95c232a2SHans Verkuil 
901*95c232a2SHans Verkuil 	if (tvnorm == V4L2_STD_NTSC_M) {
902*95c232a2SHans Verkuil 		if (width == 320 || width == 352 || width == 720)
903*95c232a2SHans Verkuil 			return 1;
904*95c232a2SHans Verkuil 		else
905*95c232a2SHans Verkuil 			return 0;
906*95c232a2SHans Verkuil 	}
907*95c232a2SHans Verkuil 	return 0;
908*95c232a2SHans Verkuil }
909*95c232a2SHans Verkuil 
910*95c232a2SHans Verkuil static int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
911*95c232a2SHans Verkuil {
912*95c232a2SHans Verkuil 	if (tvnorm == V4L2_STD_PAL_BG) {
913*95c232a2SHans Verkuil 		if (height == 576 || height == 288)
914*95c232a2SHans Verkuil 			return 1;
915*95c232a2SHans Verkuil 		else
916*95c232a2SHans Verkuil 			return 0;
917*95c232a2SHans Verkuil 	}
918*95c232a2SHans Verkuil 
919*95c232a2SHans Verkuil 	if (tvnorm == V4L2_STD_NTSC_M) {
920*95c232a2SHans Verkuil 		if (height == 480 || height == 240)
921*95c232a2SHans Verkuil 			return 1;
922*95c232a2SHans Verkuil 		else
923*95c232a2SHans Verkuil 			return 0;
924*95c232a2SHans Verkuil 	}
925*95c232a2SHans Verkuil 
926*95c232a2SHans Verkuil 	return 0;
927*95c232a2SHans Verkuil }
928*95c232a2SHans Verkuil 
929b285192aSMauro Carvalho Chehab static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
930b285192aSMauro Carvalho Chehab 				struct v4l2_format *f)
931b285192aSMauro Carvalho Chehab {
932b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
933b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
934b285192aSMauro Carvalho Chehab 	struct v4l2_mbus_framefmt mbus_fmt;
935b285192aSMauro Carvalho Chehab 	int err;
936b285192aSMauro Carvalho Chehab 	int pix_format = PIXEL_FRMT_422;
937b285192aSMauro Carvalho Chehab 
938b285192aSMauro Carvalho Chehab 	if (fh) {
939b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
940b285192aSMauro Carvalho Chehab 				      fh->prio);
941b285192aSMauro Carvalho Chehab 		if (0 != err)
942b285192aSMauro Carvalho Chehab 			return err;
943b285192aSMauro Carvalho Chehab 	}
944b285192aSMauro Carvalho Chehab 
945b285192aSMauro Carvalho Chehab 	dprintk(2, "%s()\n", __func__);
946b285192aSMauro Carvalho Chehab 	err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
947b285192aSMauro Carvalho Chehab 
948b285192aSMauro Carvalho Chehab 	if (0 != err)
949b285192aSMauro Carvalho Chehab 		return err;
950b285192aSMauro Carvalho Chehab 
951b285192aSMauro Carvalho Chehab 	fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
952b285192aSMauro Carvalho Chehab 	fh->vidq.field = f->fmt.pix.field;
953b285192aSMauro Carvalho Chehab 
954b285192aSMauro Carvalho Chehab 	/* check if width and height is valid based on set standard */
955b285192aSMauro Carvalho Chehab 	if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
956b285192aSMauro Carvalho Chehab 		fh->width = f->fmt.pix.width;
957b285192aSMauro Carvalho Chehab 
958b285192aSMauro Carvalho Chehab 	if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
959b285192aSMauro Carvalho Chehab 		fh->height = f->fmt.pix.height;
960b285192aSMauro Carvalho Chehab 
961b285192aSMauro Carvalho Chehab 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
962b285192aSMauro Carvalho Chehab 		pix_format = PIXEL_FRMT_411;
963b285192aSMauro Carvalho Chehab 	else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
964b285192aSMauro Carvalho Chehab 		pix_format = PIXEL_FRMT_422;
965b285192aSMauro Carvalho Chehab 	else
966b285192aSMauro Carvalho Chehab 		return -EINVAL;
967b285192aSMauro Carvalho Chehab 
968b285192aSMauro Carvalho Chehab 	cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
969b285192aSMauro Carvalho Chehab 
970b285192aSMauro Carvalho Chehab 	/* check if cif resolution */
971b285192aSMauro Carvalho Chehab 	if (fh->width == 320 || fh->width == 352)
972b285192aSMauro Carvalho Chehab 		dev->channels[fh->channel_id].use_cif_resolution = 1;
973b285192aSMauro Carvalho Chehab 	else
974b285192aSMauro Carvalho Chehab 		dev->channels[fh->channel_id].use_cif_resolution = 0;
975b285192aSMauro Carvalho Chehab 
976b285192aSMauro Carvalho Chehab 	dev->channels[fh->channel_id].cif_width = fh->width;
977b285192aSMauro Carvalho Chehab 	medusa_set_resolution(dev, fh->width, SRAM_CH00);
978b285192aSMauro Carvalho Chehab 
979b285192aSMauro Carvalho Chehab 	dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__, fh->width,
980b285192aSMauro Carvalho Chehab 		fh->height, fh->vidq.field);
981b285192aSMauro Carvalho Chehab 	v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
982b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
983b285192aSMauro Carvalho Chehab 
984b285192aSMauro Carvalho Chehab 	return 0;
985b285192aSMauro Carvalho Chehab }
986b285192aSMauro Carvalho Chehab 
987b285192aSMauro Carvalho Chehab static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
988b285192aSMauro Carvalho Chehab {
989b285192aSMauro Carvalho Chehab 	int ret_val = 0;
990b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
991b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
992b285192aSMauro Carvalho Chehab 
993b285192aSMauro Carvalho Chehab 	ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
994b285192aSMauro Carvalho Chehab 
995b285192aSMauro Carvalho Chehab 	p->sequence = dev->channels[fh->channel_id].vidq.count;
996b285192aSMauro Carvalho Chehab 
997b285192aSMauro Carvalho Chehab 	return ret_val;
998b285192aSMauro Carvalho Chehab }
999b285192aSMauro Carvalho Chehab 
1000b285192aSMauro Carvalho Chehab static int vidioc_log_status(struct file *file, void *priv)
1001b285192aSMauro Carvalho Chehab {
1002b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1003b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1004bfef0d35SHans Verkuil 	const struct sram_channel *sram_ch =
10053dd473caSHans Verkuil 		dev->channels[fh->channel_id].sram_channels;
1006b285192aSMauro Carvalho Chehab 	u32 tmp = 0;
1007b285192aSMauro Carvalho Chehab 
1008b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, core, log_status);
1009b285192aSMauro Carvalho Chehab 	tmp = cx_read(sram_ch->dma_ctl);
1010b285192aSMauro Carvalho Chehab 	pr_info("Video input 0 is %s\n",
1011b285192aSMauro Carvalho Chehab 		(tmp & 0x11) ? "streaming" : "stopped");
1012b285192aSMauro Carvalho Chehab 	return 0;
1013b285192aSMauro Carvalho Chehab }
1014b285192aSMauro Carvalho Chehab 
1015b285192aSMauro Carvalho Chehab 
1016*95c232a2SHans Verkuil static int cx25821_vidioc_querycap(struct file *file, void *priv,
1017b285192aSMauro Carvalho Chehab 			    struct v4l2_capability *cap)
1018b285192aSMauro Carvalho Chehab {
1019b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
10203dd473caSHans Verkuil 	struct cx25821_fh *fh = priv;
10213dd473caSHans Verkuil 	const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
10223dd473caSHans Verkuil 			V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
10233dd473caSHans Verkuil 	const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
1024b285192aSMauro Carvalho Chehab 
1025b285192aSMauro Carvalho Chehab 	strcpy(cap->driver, "cx25821");
1026b285192aSMauro Carvalho Chehab 	strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
1027b285192aSMauro Carvalho Chehab 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
10283dd473caSHans Verkuil 	if (fh->channel_id >= VID_CHANNEL_NUM)
10293dd473caSHans Verkuil 		cap->device_caps = cap_output;
10303dd473caSHans Verkuil 	else
10313dd473caSHans Verkuil 		cap->device_caps = cap_input;
10323dd473caSHans Verkuil 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1033b285192aSMauro Carvalho Chehab 	return 0;
1034b285192aSMauro Carvalho Chehab }
1035b285192aSMauro Carvalho Chehab 
1036*95c232a2SHans Verkuil static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1037b285192aSMauro Carvalho Chehab 			    struct v4l2_fmtdesc *f)
1038b285192aSMauro Carvalho Chehab {
1039b285192aSMauro Carvalho Chehab 	if (unlikely(f->index >= ARRAY_SIZE(formats)))
1040b285192aSMauro Carvalho Chehab 		return -EINVAL;
1041b285192aSMauro Carvalho Chehab 
1042b285192aSMauro Carvalho Chehab 	strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1043b285192aSMauro Carvalho Chehab 	f->pixelformat = formats[f->index].fourcc;
1044b285192aSMauro Carvalho Chehab 
1045b285192aSMauro Carvalho Chehab 	return 0;
1046b285192aSMauro Carvalho Chehab }
1047b285192aSMauro Carvalho Chehab 
1048*95c232a2SHans Verkuil static int cx25821_vidioc_reqbufs(struct file *file, void *priv,
1049b285192aSMauro Carvalho Chehab 			   struct v4l2_requestbuffers *p)
1050b285192aSMauro Carvalho Chehab {
1051b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1052b285192aSMauro Carvalho Chehab 	return videobuf_reqbufs(get_queue(fh), p);
1053b285192aSMauro Carvalho Chehab }
1054b285192aSMauro Carvalho Chehab 
1055*95c232a2SHans Verkuil static int cx25821_vidioc_querybuf(struct file *file, void *priv,
1056b285192aSMauro Carvalho Chehab 			    struct v4l2_buffer *p)
1057b285192aSMauro Carvalho Chehab {
1058b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1059b285192aSMauro Carvalho Chehab 	return videobuf_querybuf(get_queue(fh), p);
1060b285192aSMauro Carvalho Chehab }
1061b285192aSMauro Carvalho Chehab 
1062*95c232a2SHans Verkuil static int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1063b285192aSMauro Carvalho Chehab {
1064b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1065b285192aSMauro Carvalho Chehab 	return videobuf_qbuf(get_queue(fh), p);
1066b285192aSMauro Carvalho Chehab }
1067b285192aSMauro Carvalho Chehab 
1068*95c232a2SHans Verkuil static int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
1069b285192aSMauro Carvalho Chehab {
1070b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1071b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = f;
1072b285192aSMauro Carvalho Chehab 
1073b285192aSMauro Carvalho Chehab 	*p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
1074b285192aSMauro Carvalho Chehab 
1075b285192aSMauro Carvalho Chehab 	return 0;
1076b285192aSMauro Carvalho Chehab }
1077b285192aSMauro Carvalho Chehab 
1078*95c232a2SHans Verkuil static int cx25821_vidioc_s_priority(struct file *file, void *f,
1079b285192aSMauro Carvalho Chehab 			      enum v4l2_priority prio)
1080b285192aSMauro Carvalho Chehab {
1081b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = f;
1082b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1083b285192aSMauro Carvalho Chehab 
1084b285192aSMauro Carvalho Chehab 	return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
1085b285192aSMauro Carvalho Chehab 			prio);
1086b285192aSMauro Carvalho Chehab }
1087b285192aSMauro Carvalho Chehab 
1088*95c232a2SHans Verkuil static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
108918c73af6SHans Verkuil {
109018c73af6SHans Verkuil 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
109118c73af6SHans Verkuil 
109218c73af6SHans Verkuil 	*tvnorms = dev->tvnorm;
109318c73af6SHans Verkuil 	return 0;
109418c73af6SHans Verkuil }
109518c73af6SHans Verkuil 
1096314527acSHans Verkuil int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
1097b285192aSMauro Carvalho Chehab {
1098b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1099b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1100b285192aSMauro Carvalho Chehab 	int err;
1101b285192aSMauro Carvalho Chehab 
1102b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1103b285192aSMauro Carvalho Chehab 
1104b285192aSMauro Carvalho Chehab 	if (fh) {
1105b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1106b285192aSMauro Carvalho Chehab 				      fh->prio);
1107b285192aSMauro Carvalho Chehab 		if (0 != err)
1108b285192aSMauro Carvalho Chehab 			return err;
1109b285192aSMauro Carvalho Chehab 	}
1110b285192aSMauro Carvalho Chehab 
1111314527acSHans Verkuil 	if (dev->tvnorm == tvnorms)
1112b285192aSMauro Carvalho Chehab 		return 0;
1113b285192aSMauro Carvalho Chehab 
1114b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
1115314527acSHans Verkuil 	cx25821_set_tvnorm(dev, tvnorms);
1116b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
1117b285192aSMauro Carvalho Chehab 
1118b285192aSMauro Carvalho Chehab 	medusa_set_videostandard(dev);
1119b285192aSMauro Carvalho Chehab 
1120b285192aSMauro Carvalho Chehab 	return 0;
1121b285192aSMauro Carvalho Chehab }
1122b285192aSMauro Carvalho Chehab 
1123*95c232a2SHans Verkuil static int cx25821_vidioc_enum_input(struct file *file, void *priv,
1124*95c232a2SHans Verkuil 			      struct v4l2_input *i)
1125b285192aSMauro Carvalho Chehab {
1126b285192aSMauro Carvalho Chehab 	static const char * const iname[] = {
1127b285192aSMauro Carvalho Chehab 		[CX25821_VMUX_COMPOSITE] = "Composite",
1128b285192aSMauro Carvalho Chehab 		[CX25821_VMUX_SVIDEO] = "S-Video",
1129b285192aSMauro Carvalho Chehab 		[CX25821_VMUX_DEBUG] = "for debug only",
1130b285192aSMauro Carvalho Chehab 	};
1131*95c232a2SHans Verkuil 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1132b285192aSMauro Carvalho Chehab 	unsigned int n;
1133b285192aSMauro Carvalho Chehab 	dprintk(1, "%s()\n", __func__);
1134b285192aSMauro Carvalho Chehab 
1135b285192aSMauro Carvalho Chehab 	n = i->index;
1136*95c232a2SHans Verkuil 	if (n >= CX25821_NR_INPUT)
1137b285192aSMauro Carvalho Chehab 		return -EINVAL;
1138b285192aSMauro Carvalho Chehab 
1139b285192aSMauro Carvalho Chehab 	if (0 == INPUT(n)->type)
1140b285192aSMauro Carvalho Chehab 		return -EINVAL;
1141b285192aSMauro Carvalho Chehab 
1142b285192aSMauro Carvalho Chehab 	i->type = V4L2_INPUT_TYPE_CAMERA;
1143b285192aSMauro Carvalho Chehab 	strcpy(i->name, iname[INPUT(n)->type]);
1144b285192aSMauro Carvalho Chehab 
1145b285192aSMauro Carvalho Chehab 	i->std = CX25821_NORMS;
1146b285192aSMauro Carvalho Chehab 	return 0;
1147b285192aSMauro Carvalho Chehab }
1148b285192aSMauro Carvalho Chehab 
1149*95c232a2SHans Verkuil static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1150b285192aSMauro Carvalho Chehab {
1151b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1152b285192aSMauro Carvalho Chehab 
1153b285192aSMauro Carvalho Chehab 	*i = dev->input;
1154b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(): returns %d\n", __func__, *i);
1155b285192aSMauro Carvalho Chehab 	return 0;
1156b285192aSMauro Carvalho Chehab }
1157b285192aSMauro Carvalho Chehab 
1158*95c232a2SHans Verkuil static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1159b285192aSMauro Carvalho Chehab {
1160b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1161b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1162b285192aSMauro Carvalho Chehab 	int err;
1163b285192aSMauro Carvalho Chehab 
1164b285192aSMauro Carvalho Chehab 	dprintk(1, "%s(%d)\n", __func__, i);
1165b285192aSMauro Carvalho Chehab 
1166b285192aSMauro Carvalho Chehab 	if (fh) {
1167b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1168b285192aSMauro Carvalho Chehab 				      fh->prio);
1169b285192aSMauro Carvalho Chehab 		if (0 != err)
1170b285192aSMauro Carvalho Chehab 			return err;
1171b285192aSMauro Carvalho Chehab 	}
1172b285192aSMauro Carvalho Chehab 
11736b1dce25SHans Verkuil 	if (i >= CX25821_NR_INPUT || INPUT(i)->type == 0)
1174b285192aSMauro Carvalho Chehab 		return -EINVAL;
1175b285192aSMauro Carvalho Chehab 
1176b285192aSMauro Carvalho Chehab 	mutex_lock(&dev->lock);
1177b285192aSMauro Carvalho Chehab 	cx25821_video_mux(dev, i);
1178b285192aSMauro Carvalho Chehab 	mutex_unlock(&dev->lock);
1179b285192aSMauro Carvalho Chehab 	return 0;
1180b285192aSMauro Carvalho Chehab }
1181b285192aSMauro Carvalho Chehab 
1182b285192aSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
1183b285192aSMauro Carvalho Chehab int cx25821_vidioc_g_register(struct file *file, void *fh,
1184b285192aSMauro Carvalho Chehab 		      struct v4l2_dbg_register *reg)
1185b285192aSMauro Carvalho Chehab {
1186b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1187b285192aSMauro Carvalho Chehab 
1188b285192aSMauro Carvalho Chehab 	if (!v4l2_chip_match_host(&reg->match))
1189b285192aSMauro Carvalho Chehab 		return -EINVAL;
1190b285192aSMauro Carvalho Chehab 
1191b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, core, g_register, reg);
1192b285192aSMauro Carvalho Chehab 
1193b285192aSMauro Carvalho Chehab 	return 0;
1194b285192aSMauro Carvalho Chehab }
1195b285192aSMauro Carvalho Chehab 
1196b285192aSMauro Carvalho Chehab int cx25821_vidioc_s_register(struct file *file, void *fh,
1197977ba3b1SHans Verkuil 		      const struct v4l2_dbg_register *reg)
1198b285192aSMauro Carvalho Chehab {
1199b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1200b285192aSMauro Carvalho Chehab 
1201b285192aSMauro Carvalho Chehab 	if (!v4l2_chip_match_host(&reg->match))
1202b285192aSMauro Carvalho Chehab 		return -EINVAL;
1203b285192aSMauro Carvalho Chehab 
1204b285192aSMauro Carvalho Chehab 	cx25821_call_all(dev, core, s_register, reg);
1205b285192aSMauro Carvalho Chehab 
1206b285192aSMauro Carvalho Chehab 	return 0;
1207b285192aSMauro Carvalho Chehab }
1208b285192aSMauro Carvalho Chehab 
1209b285192aSMauro Carvalho Chehab #endif
1210b285192aSMauro Carvalho Chehab 
1211b285192aSMauro Carvalho Chehab /*****************************************************************************/
1212b285192aSMauro Carvalho Chehab static const struct v4l2_queryctrl no_ctl = {
1213b285192aSMauro Carvalho Chehab 	.name = "42",
1214b285192aSMauro Carvalho Chehab 	.flags = V4L2_CTRL_FLAG_DISABLED,
1215b285192aSMauro Carvalho Chehab };
1216b285192aSMauro Carvalho Chehab 
1217b285192aSMauro Carvalho Chehab static struct v4l2_queryctrl cx25821_ctls[] = {
1218b285192aSMauro Carvalho Chehab 	/* --- video --- */
1219b285192aSMauro Carvalho Chehab 	{
1220b285192aSMauro Carvalho Chehab 		.id = V4L2_CID_BRIGHTNESS,
1221b285192aSMauro Carvalho Chehab 		.name = "Brightness",
1222b285192aSMauro Carvalho Chehab 		.minimum = 0,
1223b285192aSMauro Carvalho Chehab 		.maximum = 10000,
1224b285192aSMauro Carvalho Chehab 		.step = 1,
1225b285192aSMauro Carvalho Chehab 		.default_value = 6200,
1226b285192aSMauro Carvalho Chehab 		.type = V4L2_CTRL_TYPE_INTEGER,
1227b285192aSMauro Carvalho Chehab 	}, {
1228b285192aSMauro Carvalho Chehab 		.id = V4L2_CID_CONTRAST,
1229b285192aSMauro Carvalho Chehab 		.name = "Contrast",
1230b285192aSMauro Carvalho Chehab 		.minimum = 0,
1231b285192aSMauro Carvalho Chehab 		.maximum = 10000,
1232b285192aSMauro Carvalho Chehab 		.step = 1,
1233b285192aSMauro Carvalho Chehab 		.default_value = 5000,
1234b285192aSMauro Carvalho Chehab 		.type = V4L2_CTRL_TYPE_INTEGER,
1235b285192aSMauro Carvalho Chehab 	}, {
1236b285192aSMauro Carvalho Chehab 		.id = V4L2_CID_SATURATION,
1237b285192aSMauro Carvalho Chehab 		.name = "Saturation",
1238b285192aSMauro Carvalho Chehab 		.minimum = 0,
1239b285192aSMauro Carvalho Chehab 		.maximum = 10000,
1240b285192aSMauro Carvalho Chehab 		.step = 1,
1241b285192aSMauro Carvalho Chehab 		.default_value = 5000,
1242b285192aSMauro Carvalho Chehab 		.type = V4L2_CTRL_TYPE_INTEGER,
1243b285192aSMauro Carvalho Chehab 	}, {
1244b285192aSMauro Carvalho Chehab 		.id = V4L2_CID_HUE,
1245b285192aSMauro Carvalho Chehab 		.name = "Hue",
1246b285192aSMauro Carvalho Chehab 		.minimum = 0,
1247b285192aSMauro Carvalho Chehab 		.maximum = 10000,
1248b285192aSMauro Carvalho Chehab 		.step = 1,
1249b285192aSMauro Carvalho Chehab 		.default_value = 5000,
1250b285192aSMauro Carvalho Chehab 		.type = V4L2_CTRL_TYPE_INTEGER,
1251b285192aSMauro Carvalho Chehab 	}
1252b285192aSMauro Carvalho Chehab };
1253b285192aSMauro Carvalho Chehab static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1254b285192aSMauro Carvalho Chehab 
1255b285192aSMauro Carvalho Chehab static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1256b285192aSMauro Carvalho Chehab {
1257b285192aSMauro Carvalho Chehab 	int i;
1258b285192aSMauro Carvalho Chehab 
1259b285192aSMauro Carvalho Chehab 	if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
1260b285192aSMauro Carvalho Chehab 		return -EINVAL;
1261b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX25821_CTLS; i++)
1262b285192aSMauro Carvalho Chehab 		if (cx25821_ctls[i].id == qctrl->id)
1263b285192aSMauro Carvalho Chehab 			break;
1264b285192aSMauro Carvalho Chehab 	if (i == CX25821_CTLS) {
1265b285192aSMauro Carvalho Chehab 		*qctrl = no_ctl;
1266b285192aSMauro Carvalho Chehab 		return 0;
1267b285192aSMauro Carvalho Chehab 	}
1268b285192aSMauro Carvalho Chehab 	*qctrl = cx25821_ctls[i];
1269b285192aSMauro Carvalho Chehab 	return 0;
1270b285192aSMauro Carvalho Chehab }
1271b285192aSMauro Carvalho Chehab 
1272*95c232a2SHans Verkuil static int cx25821_vidioc_queryctrl(struct file *file, void *priv,
1273b285192aSMauro Carvalho Chehab 		     struct v4l2_queryctrl *qctrl)
1274b285192aSMauro Carvalho Chehab {
1275b285192aSMauro Carvalho Chehab 	return cx25821_ctrl_query(qctrl);
1276b285192aSMauro Carvalho Chehab }
1277b285192aSMauro Carvalho Chehab 
1278b285192aSMauro Carvalho Chehab /* ------------------------------------------------------------------ */
1279b285192aSMauro Carvalho Chehab /* VIDEO CTRL IOCTLS                                                  */
1280b285192aSMauro Carvalho Chehab 
1281b285192aSMauro Carvalho Chehab static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
1282b285192aSMauro Carvalho Chehab {
1283b285192aSMauro Carvalho Chehab 	unsigned int i;
1284b285192aSMauro Carvalho Chehab 
1285b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX25821_CTLS; i++)
1286b285192aSMauro Carvalho Chehab 		if (cx25821_ctls[i].id == id)
1287b285192aSMauro Carvalho Chehab 			return cx25821_ctls + i;
1288b285192aSMauro Carvalho Chehab 	return NULL;
1289b285192aSMauro Carvalho Chehab }
1290b285192aSMauro Carvalho Chehab 
1291*95c232a2SHans Verkuil static int cx25821_vidioc_g_ctrl(struct file *file, void *priv,
1292b285192aSMauro Carvalho Chehab 			  struct v4l2_control *ctl)
1293b285192aSMauro Carvalho Chehab {
1294b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1295b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1296b285192aSMauro Carvalho Chehab 
1297b285192aSMauro Carvalho Chehab 	const struct v4l2_queryctrl *ctrl;
1298b285192aSMauro Carvalho Chehab 
1299b285192aSMauro Carvalho Chehab 	ctrl = ctrl_by_id(ctl->id);
1300b285192aSMauro Carvalho Chehab 
1301b285192aSMauro Carvalho Chehab 	if (NULL == ctrl)
1302b285192aSMauro Carvalho Chehab 		return -EINVAL;
1303b285192aSMauro Carvalho Chehab 	switch (ctl->id) {
1304b285192aSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
1305b285192aSMauro Carvalho Chehab 		ctl->value = dev->channels[fh->channel_id].ctl_bright;
1306b285192aSMauro Carvalho Chehab 		break;
1307b285192aSMauro Carvalho Chehab 	case V4L2_CID_HUE:
1308b285192aSMauro Carvalho Chehab 		ctl->value = dev->channels[fh->channel_id].ctl_hue;
1309b285192aSMauro Carvalho Chehab 		break;
1310b285192aSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
1311b285192aSMauro Carvalho Chehab 		ctl->value = dev->channels[fh->channel_id].ctl_contrast;
1312b285192aSMauro Carvalho Chehab 		break;
1313b285192aSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
1314b285192aSMauro Carvalho Chehab 		ctl->value = dev->channels[fh->channel_id].ctl_saturation;
1315b285192aSMauro Carvalho Chehab 		break;
1316b285192aSMauro Carvalho Chehab 	}
1317b285192aSMauro Carvalho Chehab 	return 0;
1318b285192aSMauro Carvalho Chehab }
1319b285192aSMauro Carvalho Chehab 
1320*95c232a2SHans Verkuil static int cx25821_set_control(struct cx25821_dev *dev,
1321b285192aSMauro Carvalho Chehab 			struct v4l2_control *ctl, int chan_num)
1322b285192aSMauro Carvalho Chehab {
1323b285192aSMauro Carvalho Chehab 	int err;
1324b285192aSMauro Carvalho Chehab 	const struct v4l2_queryctrl *ctrl;
1325b285192aSMauro Carvalho Chehab 
1326b285192aSMauro Carvalho Chehab 	err = -EINVAL;
1327b285192aSMauro Carvalho Chehab 
1328b285192aSMauro Carvalho Chehab 	ctrl = ctrl_by_id(ctl->id);
1329b285192aSMauro Carvalho Chehab 
1330b285192aSMauro Carvalho Chehab 	if (NULL == ctrl)
1331b285192aSMauro Carvalho Chehab 		return err;
1332b285192aSMauro Carvalho Chehab 
1333b285192aSMauro Carvalho Chehab 	switch (ctrl->type) {
1334b285192aSMauro Carvalho Chehab 	case V4L2_CTRL_TYPE_BOOLEAN:
1335b285192aSMauro Carvalho Chehab 	case V4L2_CTRL_TYPE_MENU:
1336b285192aSMauro Carvalho Chehab 	case V4L2_CTRL_TYPE_INTEGER:
1337b285192aSMauro Carvalho Chehab 		if (ctl->value < ctrl->minimum)
1338b285192aSMauro Carvalho Chehab 			ctl->value = ctrl->minimum;
1339b285192aSMauro Carvalho Chehab 		if (ctl->value > ctrl->maximum)
1340b285192aSMauro Carvalho Chehab 			ctl->value = ctrl->maximum;
1341b285192aSMauro Carvalho Chehab 		break;
1342b285192aSMauro Carvalho Chehab 	default:
1343b285192aSMauro Carvalho Chehab 		/* nothing */ ;
1344b285192aSMauro Carvalho Chehab 	}
1345b285192aSMauro Carvalho Chehab 
1346b285192aSMauro Carvalho Chehab 	switch (ctl->id) {
1347b285192aSMauro Carvalho Chehab 	case V4L2_CID_BRIGHTNESS:
1348b285192aSMauro Carvalho Chehab 		dev->channels[chan_num].ctl_bright = ctl->value;
1349b285192aSMauro Carvalho Chehab 		medusa_set_brightness(dev, ctl->value, chan_num);
1350b285192aSMauro Carvalho Chehab 		break;
1351b285192aSMauro Carvalho Chehab 	case V4L2_CID_HUE:
1352b285192aSMauro Carvalho Chehab 		dev->channels[chan_num].ctl_hue = ctl->value;
1353b285192aSMauro Carvalho Chehab 		medusa_set_hue(dev, ctl->value, chan_num);
1354b285192aSMauro Carvalho Chehab 		break;
1355b285192aSMauro Carvalho Chehab 	case V4L2_CID_CONTRAST:
1356b285192aSMauro Carvalho Chehab 		dev->channels[chan_num].ctl_contrast = ctl->value;
1357b285192aSMauro Carvalho Chehab 		medusa_set_contrast(dev, ctl->value, chan_num);
1358b285192aSMauro Carvalho Chehab 		break;
1359b285192aSMauro Carvalho Chehab 	case V4L2_CID_SATURATION:
1360b285192aSMauro Carvalho Chehab 		dev->channels[chan_num].ctl_saturation = ctl->value;
1361b285192aSMauro Carvalho Chehab 		medusa_set_saturation(dev, ctl->value, chan_num);
1362b285192aSMauro Carvalho Chehab 		break;
1363b285192aSMauro Carvalho Chehab 	}
1364b285192aSMauro Carvalho Chehab 
1365b285192aSMauro Carvalho Chehab 	err = 0;
1366b285192aSMauro Carvalho Chehab 
1367b285192aSMauro Carvalho Chehab 	return err;
1368b285192aSMauro Carvalho Chehab }
1369b285192aSMauro Carvalho Chehab 
1370*95c232a2SHans Verkuil static int vidioc_s_ctrl(struct file *file, void *priv,
1371*95c232a2SHans Verkuil 			struct v4l2_control *ctl)
1372*95c232a2SHans Verkuil {
1373*95c232a2SHans Verkuil 	struct cx25821_fh *fh = priv;
1374*95c232a2SHans Verkuil 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1375*95c232a2SHans Verkuil 	int err;
1376*95c232a2SHans Verkuil 
1377*95c232a2SHans Verkuil 	if (fh) {
1378*95c232a2SHans Verkuil 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1379*95c232a2SHans Verkuil 				      fh->prio);
1380*95c232a2SHans Verkuil 		if (0 != err)
1381*95c232a2SHans Verkuil 			return err;
1382*95c232a2SHans Verkuil 	}
1383*95c232a2SHans Verkuil 
1384*95c232a2SHans Verkuil 	return cx25821_set_control(dev, ctl, fh->channel_id);
1385*95c232a2SHans Verkuil }
1386*95c232a2SHans Verkuil 
1387b285192aSMauro Carvalho Chehab static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num)
1388b285192aSMauro Carvalho Chehab {
1389b285192aSMauro Carvalho Chehab 	struct v4l2_control ctrl;
1390b285192aSMauro Carvalho Chehab 	int i;
1391b285192aSMauro Carvalho Chehab 	for (i = 0; i < CX25821_CTLS; i++) {
1392b285192aSMauro Carvalho Chehab 		ctrl.id = cx25821_ctls[i].id;
1393b285192aSMauro Carvalho Chehab 		ctrl.value = cx25821_ctls[i].default_value;
1394b285192aSMauro Carvalho Chehab 
1395b285192aSMauro Carvalho Chehab 		cx25821_set_control(dev, &ctrl, chan_num);
1396b285192aSMauro Carvalho Chehab 	}
1397b285192aSMauro Carvalho Chehab }
1398b285192aSMauro Carvalho Chehab 
1399*95c232a2SHans Verkuil static int cx25821_vidioc_cropcap(struct file *file, void *priv,
1400b285192aSMauro Carvalho Chehab 			   struct v4l2_cropcap *cropcap)
1401b285192aSMauro Carvalho Chehab {
1402b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1403b285192aSMauro Carvalho Chehab 
1404b285192aSMauro Carvalho Chehab 	if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1405b285192aSMauro Carvalho Chehab 		return -EINVAL;
1406b285192aSMauro Carvalho Chehab 	cropcap->bounds.top = 0;
1407b285192aSMauro Carvalho Chehab 	cropcap->bounds.left = 0;
1408b285192aSMauro Carvalho Chehab 	cropcap->bounds.width = 720;
1409b285192aSMauro Carvalho Chehab 	cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1410b285192aSMauro Carvalho Chehab 	cropcap->pixelaspect.numerator =
1411b285192aSMauro Carvalho Chehab 		dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1412b285192aSMauro Carvalho Chehab 	cropcap->pixelaspect.denominator =
1413b285192aSMauro Carvalho Chehab 		dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1414b285192aSMauro Carvalho Chehab 	cropcap->defrect = cropcap->bounds;
1415b285192aSMauro Carvalho Chehab 	return 0;
1416b285192aSMauro Carvalho Chehab }
1417b285192aSMauro Carvalho Chehab 
1418*95c232a2SHans Verkuil static int cx25821_vidioc_s_crop(struct file *file, void *priv, const struct v4l2_crop *crop)
1419b285192aSMauro Carvalho Chehab {
1420b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1421b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = priv;
1422b285192aSMauro Carvalho Chehab 	int err;
1423b285192aSMauro Carvalho Chehab 
1424b285192aSMauro Carvalho Chehab 	if (fh) {
1425b285192aSMauro Carvalho Chehab 		err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1426b285192aSMauro Carvalho Chehab 				      fh->prio);
1427b285192aSMauro Carvalho Chehab 		if (0 != err)
1428b285192aSMauro Carvalho Chehab 			return err;
1429b285192aSMauro Carvalho Chehab 	}
1430b285192aSMauro Carvalho Chehab 	/* cx25821_vidioc_s_crop not supported */
1431b285192aSMauro Carvalho Chehab 	return -EINVAL;
1432b285192aSMauro Carvalho Chehab }
1433b285192aSMauro Carvalho Chehab 
1434*95c232a2SHans Verkuil static int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1435b285192aSMauro Carvalho Chehab {
1436b285192aSMauro Carvalho Chehab 	/* cx25821_vidioc_g_crop not supported */
1437b285192aSMauro Carvalho Chehab 	return -EINVAL;
1438b285192aSMauro Carvalho Chehab }
1439b285192aSMauro Carvalho Chehab 
1440b285192aSMauro Carvalho Chehab static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
1441b285192aSMauro Carvalho Chehab 				 unsigned long arg)
1442b285192aSMauro Carvalho Chehab {
1443b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1444b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1445b285192aSMauro Carvalho Chehab 	int command = 0;
1446b285192aSMauro Carvalho Chehab 	struct upstream_user_struct *data_from_user;
1447b285192aSMauro Carvalho Chehab 
1448b285192aSMauro Carvalho Chehab 	data_from_user = (struct upstream_user_struct *)arg;
1449b285192aSMauro Carvalho Chehab 
1450b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1451b285192aSMauro Carvalho Chehab 		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1452b285192aSMauro Carvalho Chehab 		return 0;
1453b285192aSMauro Carvalho Chehab 	}
1454b285192aSMauro Carvalho Chehab 
1455b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1456b285192aSMauro Carvalho Chehab 
1457b285192aSMauro Carvalho Chehab 	if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1458b285192aSMauro Carvalho Chehab 		return 0;
1459b285192aSMauro Carvalho Chehab 
1460b285192aSMauro Carvalho Chehab 	dev->input_filename = data_from_user->input_filename;
1461b285192aSMauro Carvalho Chehab 	dev->input_audiofilename = data_from_user->input_filename;
1462b285192aSMauro Carvalho Chehab 	dev->vid_stdname = data_from_user->vid_stdname;
1463b285192aSMauro Carvalho Chehab 	dev->pixel_format = data_from_user->pixel_format;
1464b285192aSMauro Carvalho Chehab 	dev->channel_select = data_from_user->channel_select;
1465b285192aSMauro Carvalho Chehab 	dev->command = data_from_user->command;
1466b285192aSMauro Carvalho Chehab 
1467b285192aSMauro Carvalho Chehab 	switch (command) {
1468b285192aSMauro Carvalho Chehab 	case UPSTREAM_START_VIDEO:
1469b285192aSMauro Carvalho Chehab 		cx25821_start_upstream_video_ch1(dev, data_from_user);
1470b285192aSMauro Carvalho Chehab 		break;
1471b285192aSMauro Carvalho Chehab 
1472b285192aSMauro Carvalho Chehab 	case UPSTREAM_STOP_VIDEO:
1473b285192aSMauro Carvalho Chehab 		cx25821_stop_upstream_video_ch1(dev);
1474b285192aSMauro Carvalho Chehab 		break;
1475b285192aSMauro Carvalho Chehab 	}
1476b285192aSMauro Carvalho Chehab 
1477b285192aSMauro Carvalho Chehab 	return 0;
1478b285192aSMauro Carvalho Chehab }
1479b285192aSMauro Carvalho Chehab 
1480b285192aSMauro Carvalho Chehab static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
1481b285192aSMauro Carvalho Chehab 				  unsigned long arg)
1482b285192aSMauro Carvalho Chehab {
1483b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1484b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1485b285192aSMauro Carvalho Chehab 	int command = 0;
1486b285192aSMauro Carvalho Chehab 	struct upstream_user_struct *data_from_user;
1487b285192aSMauro Carvalho Chehab 
1488b285192aSMauro Carvalho Chehab 	data_from_user = (struct upstream_user_struct *)arg;
1489b285192aSMauro Carvalho Chehab 
1490b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1491b285192aSMauro Carvalho Chehab 		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1492b285192aSMauro Carvalho Chehab 		return 0;
1493b285192aSMauro Carvalho Chehab 	}
1494b285192aSMauro Carvalho Chehab 
1495b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1496b285192aSMauro Carvalho Chehab 
1497b285192aSMauro Carvalho Chehab 	if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1498b285192aSMauro Carvalho Chehab 		return 0;
1499b285192aSMauro Carvalho Chehab 
1500b285192aSMauro Carvalho Chehab 	dev->input_filename_ch2 = data_from_user->input_filename;
1501b285192aSMauro Carvalho Chehab 	dev->input_audiofilename = data_from_user->input_filename;
1502b285192aSMauro Carvalho Chehab 	dev->vid_stdname_ch2 = data_from_user->vid_stdname;
1503b285192aSMauro Carvalho Chehab 	dev->pixel_format_ch2 = data_from_user->pixel_format;
1504b285192aSMauro Carvalho Chehab 	dev->channel_select_ch2 = data_from_user->channel_select;
1505b285192aSMauro Carvalho Chehab 	dev->command_ch2 = data_from_user->command;
1506b285192aSMauro Carvalho Chehab 
1507b285192aSMauro Carvalho Chehab 	switch (command) {
1508b285192aSMauro Carvalho Chehab 	case UPSTREAM_START_VIDEO:
1509b285192aSMauro Carvalho Chehab 		cx25821_start_upstream_video_ch2(dev, data_from_user);
1510b285192aSMauro Carvalho Chehab 		break;
1511b285192aSMauro Carvalho Chehab 
1512b285192aSMauro Carvalho Chehab 	case UPSTREAM_STOP_VIDEO:
1513b285192aSMauro Carvalho Chehab 		cx25821_stop_upstream_video_ch2(dev);
1514b285192aSMauro Carvalho Chehab 		break;
1515b285192aSMauro Carvalho Chehab 	}
1516b285192aSMauro Carvalho Chehab 
1517b285192aSMauro Carvalho Chehab 	return 0;
1518b285192aSMauro Carvalho Chehab }
1519b285192aSMauro Carvalho Chehab 
1520b285192aSMauro Carvalho Chehab static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
1521b285192aSMauro Carvalho Chehab 				  unsigned long arg)
1522b285192aSMauro Carvalho Chehab {
1523b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1524b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1525b285192aSMauro Carvalho Chehab 	int command = 0;
1526b285192aSMauro Carvalho Chehab 	struct upstream_user_struct *data_from_user;
1527b285192aSMauro Carvalho Chehab 
1528b285192aSMauro Carvalho Chehab 	data_from_user = (struct upstream_user_struct *)arg;
1529b285192aSMauro Carvalho Chehab 
1530b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1531b285192aSMauro Carvalho Chehab 		pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1532b285192aSMauro Carvalho Chehab 		return 0;
1533b285192aSMauro Carvalho Chehab 	}
1534b285192aSMauro Carvalho Chehab 
1535b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1536b285192aSMauro Carvalho Chehab 
1537b285192aSMauro Carvalho Chehab 	if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
1538b285192aSMauro Carvalho Chehab 		return 0;
1539b285192aSMauro Carvalho Chehab 
1540b285192aSMauro Carvalho Chehab 	dev->input_filename = data_from_user->input_filename;
1541b285192aSMauro Carvalho Chehab 	dev->input_audiofilename = data_from_user->input_filename;
1542b285192aSMauro Carvalho Chehab 	dev->vid_stdname = data_from_user->vid_stdname;
1543b285192aSMauro Carvalho Chehab 	dev->pixel_format = data_from_user->pixel_format;
1544b285192aSMauro Carvalho Chehab 	dev->channel_select = data_from_user->channel_select;
1545b285192aSMauro Carvalho Chehab 	dev->command = data_from_user->command;
1546b285192aSMauro Carvalho Chehab 
1547b285192aSMauro Carvalho Chehab 	switch (command) {
1548b285192aSMauro Carvalho Chehab 	case UPSTREAM_START_AUDIO:
1549b285192aSMauro Carvalho Chehab 		cx25821_start_upstream_audio(dev, data_from_user);
1550b285192aSMauro Carvalho Chehab 		break;
1551b285192aSMauro Carvalho Chehab 
1552b285192aSMauro Carvalho Chehab 	case UPSTREAM_STOP_AUDIO:
1553b285192aSMauro Carvalho Chehab 		cx25821_stop_upstream_audio(dev);
1554b285192aSMauro Carvalho Chehab 		break;
1555b285192aSMauro Carvalho Chehab 	}
1556b285192aSMauro Carvalho Chehab 
1557b285192aSMauro Carvalho Chehab 	return 0;
1558b285192aSMauro Carvalho Chehab }
1559b285192aSMauro Carvalho Chehab 
1560b285192aSMauro Carvalho Chehab static long video_ioctl_set(struct file *file, unsigned int cmd,
1561b285192aSMauro Carvalho Chehab 			   unsigned long arg)
1562b285192aSMauro Carvalho Chehab {
1563b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1564b285192aSMauro Carvalho Chehab 	struct cx25821_dev *dev = fh->dev;
1565b285192aSMauro Carvalho Chehab 	struct downstream_user_struct *data_from_user;
1566b285192aSMauro Carvalho Chehab 	int command;
1567b285192aSMauro Carvalho Chehab 	int width = 720;
1568b285192aSMauro Carvalho Chehab 	int selected_channel = 0;
1569b285192aSMauro Carvalho Chehab 	int pix_format = 0;
1570b285192aSMauro Carvalho Chehab 	int i = 0;
1571b285192aSMauro Carvalho Chehab 	int cif_enable = 0;
1572b285192aSMauro Carvalho Chehab 	int cif_width = 0;
1573b285192aSMauro Carvalho Chehab 
1574b285192aSMauro Carvalho Chehab 	data_from_user = (struct downstream_user_struct *)arg;
1575b285192aSMauro Carvalho Chehab 
1576b285192aSMauro Carvalho Chehab 	if (!data_from_user) {
1577b285192aSMauro Carvalho Chehab 		pr_err("%s(): User data is INVALID. Returning\n", __func__);
1578b285192aSMauro Carvalho Chehab 		return 0;
1579b285192aSMauro Carvalho Chehab 	}
1580b285192aSMauro Carvalho Chehab 
1581b285192aSMauro Carvalho Chehab 	command = data_from_user->command;
1582b285192aSMauro Carvalho Chehab 
1583b285192aSMauro Carvalho Chehab 	if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
1584b285192aSMauro Carvalho Chehab 	   && command != ENABLE_CIF_RESOLUTION && command != REG_READ
1585b285192aSMauro Carvalho Chehab 	   && command != REG_WRITE && command != MEDUSA_READ
1586b285192aSMauro Carvalho Chehab 	   && command != MEDUSA_WRITE) {
1587b285192aSMauro Carvalho Chehab 		return 0;
1588b285192aSMauro Carvalho Chehab 	}
1589b285192aSMauro Carvalho Chehab 
1590b285192aSMauro Carvalho Chehab 	switch (command) {
1591b285192aSMauro Carvalho Chehab 	case SET_VIDEO_STD:
1592b285192aSMauro Carvalho Chehab 		if (!strcmp(data_from_user->vid_stdname, "PAL"))
1593b285192aSMauro Carvalho Chehab 			dev->tvnorm = V4L2_STD_PAL_BG;
1594b285192aSMauro Carvalho Chehab 		else
1595b285192aSMauro Carvalho Chehab 			dev->tvnorm = V4L2_STD_NTSC_M;
1596b285192aSMauro Carvalho Chehab 		medusa_set_videostandard(dev);
1597b285192aSMauro Carvalho Chehab 		break;
1598b285192aSMauro Carvalho Chehab 
1599b285192aSMauro Carvalho Chehab 	case SET_PIXEL_FORMAT:
1600b285192aSMauro Carvalho Chehab 		selected_channel = data_from_user->decoder_select;
1601b285192aSMauro Carvalho Chehab 		pix_format = data_from_user->pixel_format;
1602b285192aSMauro Carvalho Chehab 
1603b285192aSMauro Carvalho Chehab 		if (!(selected_channel <= 7 && selected_channel >= 0)) {
1604b285192aSMauro Carvalho Chehab 			selected_channel -= 4;
1605b285192aSMauro Carvalho Chehab 			selected_channel = selected_channel % 8;
1606b285192aSMauro Carvalho Chehab 		}
1607b285192aSMauro Carvalho Chehab 
1608b285192aSMauro Carvalho Chehab 		if (selected_channel >= 0)
1609b285192aSMauro Carvalho Chehab 			cx25821_set_pixel_format(dev, selected_channel,
1610b285192aSMauro Carvalho Chehab 						pix_format);
1611b285192aSMauro Carvalho Chehab 
1612b285192aSMauro Carvalho Chehab 		break;
1613b285192aSMauro Carvalho Chehab 
1614b285192aSMauro Carvalho Chehab 	case ENABLE_CIF_RESOLUTION:
1615b285192aSMauro Carvalho Chehab 		selected_channel = data_from_user->decoder_select;
1616b285192aSMauro Carvalho Chehab 		cif_enable = data_from_user->cif_resolution_enable;
1617b285192aSMauro Carvalho Chehab 		cif_width = data_from_user->cif_width;
1618b285192aSMauro Carvalho Chehab 
1619b285192aSMauro Carvalho Chehab 		if (cif_enable) {
1620b285192aSMauro Carvalho Chehab 			if (dev->tvnorm & V4L2_STD_PAL_BG
1621b285192aSMauro Carvalho Chehab 			    || dev->tvnorm & V4L2_STD_PAL_DK) {
1622b285192aSMauro Carvalho Chehab 				width = 352;
1623b285192aSMauro Carvalho Chehab 			} else {
1624b285192aSMauro Carvalho Chehab 				width = cif_width;
1625b285192aSMauro Carvalho Chehab 				if (cif_width != 320 && cif_width != 352)
1626b285192aSMauro Carvalho Chehab 					width = 320;
1627b285192aSMauro Carvalho Chehab 			}
1628b285192aSMauro Carvalho Chehab 		}
1629b285192aSMauro Carvalho Chehab 
1630b285192aSMauro Carvalho Chehab 		if (!(selected_channel <= 7 && selected_channel >= 0)) {
1631b285192aSMauro Carvalho Chehab 			selected_channel -= 4;
1632b285192aSMauro Carvalho Chehab 			selected_channel = selected_channel % 8;
1633b285192aSMauro Carvalho Chehab 		}
1634b285192aSMauro Carvalho Chehab 
1635b285192aSMauro Carvalho Chehab 		if (selected_channel <= 7 && selected_channel >= 0) {
1636b285192aSMauro Carvalho Chehab 			dev->channels[selected_channel].use_cif_resolution =
1637b285192aSMauro Carvalho Chehab 				cif_enable;
1638b285192aSMauro Carvalho Chehab 			dev->channels[selected_channel].cif_width = width;
1639b285192aSMauro Carvalho Chehab 		} else {
1640b285192aSMauro Carvalho Chehab 			for (i = 0; i < VID_CHANNEL_NUM; i++) {
1641b285192aSMauro Carvalho Chehab 				dev->channels[i].use_cif_resolution =
1642b285192aSMauro Carvalho Chehab 					cif_enable;
1643b285192aSMauro Carvalho Chehab 				dev->channels[i].cif_width = width;
1644b285192aSMauro Carvalho Chehab 			}
1645b285192aSMauro Carvalho Chehab 		}
1646b285192aSMauro Carvalho Chehab 
1647b285192aSMauro Carvalho Chehab 		medusa_set_resolution(dev, width, selected_channel);
1648b285192aSMauro Carvalho Chehab 		break;
1649b285192aSMauro Carvalho Chehab 	case REG_READ:
1650b285192aSMauro Carvalho Chehab 		data_from_user->reg_data = cx_read(data_from_user->reg_address);
1651b285192aSMauro Carvalho Chehab 		break;
1652b285192aSMauro Carvalho Chehab 	case REG_WRITE:
1653b285192aSMauro Carvalho Chehab 		cx_write(data_from_user->reg_address, data_from_user->reg_data);
1654b285192aSMauro Carvalho Chehab 		break;
1655b285192aSMauro Carvalho Chehab 	case MEDUSA_READ:
1656b285192aSMauro Carvalho Chehab 		cx25821_i2c_read(&dev->i2c_bus[0],
1657b285192aSMauro Carvalho Chehab 					 (u16) data_from_user->reg_address,
1658b285192aSMauro Carvalho Chehab 					 &data_from_user->reg_data);
1659b285192aSMauro Carvalho Chehab 		break;
1660b285192aSMauro Carvalho Chehab 	case MEDUSA_WRITE:
1661b285192aSMauro Carvalho Chehab 		cx25821_i2c_write(&dev->i2c_bus[0],
1662b285192aSMauro Carvalho Chehab 				  (u16) data_from_user->reg_address,
1663b285192aSMauro Carvalho Chehab 				  data_from_user->reg_data);
1664b285192aSMauro Carvalho Chehab 		break;
1665b285192aSMauro Carvalho Chehab 	}
1666b285192aSMauro Carvalho Chehab 
1667b285192aSMauro Carvalho Chehab 	return 0;
1668b285192aSMauro Carvalho Chehab }
1669b285192aSMauro Carvalho Chehab 
1670b285192aSMauro Carvalho Chehab static long cx25821_video_ioctl(struct file *file,
1671b285192aSMauro Carvalho Chehab 				unsigned int cmd, unsigned long arg)
1672b285192aSMauro Carvalho Chehab {
1673b285192aSMauro Carvalho Chehab 	int ret = 0;
1674b285192aSMauro Carvalho Chehab 
1675b285192aSMauro Carvalho Chehab 	struct cx25821_fh *fh = file->private_data;
1676b285192aSMauro Carvalho Chehab 
1677b285192aSMauro Carvalho Chehab 	/* check to see if it's the video upstream */
1678b285192aSMauro Carvalho Chehab 	if (fh->channel_id == SRAM_CH09) {
1679b285192aSMauro Carvalho Chehab 		ret = video_ioctl_upstream9(file, cmd, arg);
1680b285192aSMauro Carvalho Chehab 		return ret;
1681b285192aSMauro Carvalho Chehab 	} else if (fh->channel_id == SRAM_CH10) {
1682b285192aSMauro Carvalho Chehab 		ret = video_ioctl_upstream10(file, cmd, arg);
1683b285192aSMauro Carvalho Chehab 		return ret;
1684b285192aSMauro Carvalho Chehab 	} else if (fh->channel_id == SRAM_CH11) {
1685b285192aSMauro Carvalho Chehab 		ret = video_ioctl_upstream11(file, cmd, arg);
1686b285192aSMauro Carvalho Chehab 		ret = video_ioctl_set(file, cmd, arg);
1687b285192aSMauro Carvalho Chehab 		return ret;
1688b285192aSMauro Carvalho Chehab 	}
1689b285192aSMauro Carvalho Chehab 
1690b285192aSMauro Carvalho Chehab 	return video_ioctl2(file, cmd, arg);
1691b285192aSMauro Carvalho Chehab }
1692b285192aSMauro Carvalho Chehab 
1693b285192aSMauro Carvalho Chehab /* exported stuff */
1694b285192aSMauro Carvalho Chehab static const struct v4l2_file_operations video_fops = {
1695b285192aSMauro Carvalho Chehab 	.owner = THIS_MODULE,
1696b285192aSMauro Carvalho Chehab 	.open = video_open,
1697b285192aSMauro Carvalho Chehab 	.release = video_release,
1698b285192aSMauro Carvalho Chehab 	.read = video_read,
1699b285192aSMauro Carvalho Chehab 	.poll = video_poll,
1700b285192aSMauro Carvalho Chehab 	.mmap = cx25821_video_mmap,
1701b285192aSMauro Carvalho Chehab 	.ioctl = cx25821_video_ioctl,
1702b285192aSMauro Carvalho Chehab };
1703b285192aSMauro Carvalho Chehab 
1704b285192aSMauro Carvalho Chehab static const struct v4l2_ioctl_ops video_ioctl_ops = {
1705b285192aSMauro Carvalho Chehab 	.vidioc_querycap = cx25821_vidioc_querycap,
1706b285192aSMauro Carvalho Chehab 	.vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
1707b285192aSMauro Carvalho Chehab 	.vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
1708b285192aSMauro Carvalho Chehab 	.vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
1709b285192aSMauro Carvalho Chehab 	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1710b285192aSMauro Carvalho Chehab 	.vidioc_reqbufs = cx25821_vidioc_reqbufs,
1711b285192aSMauro Carvalho Chehab 	.vidioc_querybuf = cx25821_vidioc_querybuf,
1712b285192aSMauro Carvalho Chehab 	.vidioc_qbuf = cx25821_vidioc_qbuf,
1713b285192aSMauro Carvalho Chehab 	.vidioc_dqbuf = vidioc_dqbuf,
171418c73af6SHans Verkuil 	.vidioc_g_std = cx25821_vidioc_g_std,
1715b285192aSMauro Carvalho Chehab 	.vidioc_s_std = cx25821_vidioc_s_std,
1716b285192aSMauro Carvalho Chehab 	.vidioc_cropcap = cx25821_vidioc_cropcap,
1717b285192aSMauro Carvalho Chehab 	.vidioc_s_crop = cx25821_vidioc_s_crop,
1718b285192aSMauro Carvalho Chehab 	.vidioc_g_crop = cx25821_vidioc_g_crop,
1719b285192aSMauro Carvalho Chehab 	.vidioc_enum_input = cx25821_vidioc_enum_input,
1720b285192aSMauro Carvalho Chehab 	.vidioc_g_input = cx25821_vidioc_g_input,
1721b285192aSMauro Carvalho Chehab 	.vidioc_s_input = cx25821_vidioc_s_input,
1722b285192aSMauro Carvalho Chehab 	.vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
1723b285192aSMauro Carvalho Chehab 	.vidioc_s_ctrl = vidioc_s_ctrl,
1724b285192aSMauro Carvalho Chehab 	.vidioc_queryctrl = cx25821_vidioc_queryctrl,
1725b285192aSMauro Carvalho Chehab 	.vidioc_streamon = vidioc_streamon,
1726b285192aSMauro Carvalho Chehab 	.vidioc_streamoff = vidioc_streamoff,
1727b285192aSMauro Carvalho Chehab 	.vidioc_log_status = vidioc_log_status,
1728b285192aSMauro Carvalho Chehab 	.vidioc_g_priority = cx25821_vidioc_g_priority,
1729b285192aSMauro Carvalho Chehab 	.vidioc_s_priority = cx25821_vidioc_s_priority,
1730b285192aSMauro Carvalho Chehab #ifdef CONFIG_VIDEO_ADV_DEBUG
1731b285192aSMauro Carvalho Chehab 	.vidioc_g_register = cx25821_vidioc_g_register,
1732b285192aSMauro Carvalho Chehab 	.vidioc_s_register = cx25821_vidioc_s_register,
1733b285192aSMauro Carvalho Chehab #endif
1734b285192aSMauro Carvalho Chehab };
1735b285192aSMauro Carvalho Chehab 
1736ffd3c233SHans Verkuil static const struct video_device cx25821_video_device = {
1737ffd3c233SHans Verkuil 	.name = "cx25821-video",
1738b285192aSMauro Carvalho Chehab 	.fops = &video_fops,
1739ffd3c233SHans Verkuil 	.minor = -1,
1740b285192aSMauro Carvalho Chehab 	.ioctl_ops = &video_ioctl_ops,
1741b285192aSMauro Carvalho Chehab 	.tvnorms = CX25821_NORMS,
1742b285192aSMauro Carvalho Chehab };
1743ffd3c233SHans Verkuil 
1744ffd3c233SHans Verkuil void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
1745ffd3c233SHans Verkuil {
1746ffd3c233SHans Verkuil 	cx_clear(PCI_INT_MSK, 1);
1747ffd3c233SHans Verkuil 
1748ffd3c233SHans Verkuil 	if (dev->channels[chan_num].video_dev) {
1749ffd3c233SHans Verkuil 		if (video_is_registered(dev->channels[chan_num].video_dev))
1750ffd3c233SHans Verkuil 			video_unregister_device(
1751ffd3c233SHans Verkuil 					dev->channels[chan_num].video_dev);
1752ffd3c233SHans Verkuil 		else
1753ffd3c233SHans Verkuil 			video_device_release(
1754ffd3c233SHans Verkuil 					dev->channels[chan_num].video_dev);
1755ffd3c233SHans Verkuil 
1756ffd3c233SHans Verkuil 		dev->channels[chan_num].video_dev = NULL;
1757ffd3c233SHans Verkuil 
1758ffd3c233SHans Verkuil 		btcx_riscmem_free(dev->pci,
1759ffd3c233SHans Verkuil 				&dev->channels[chan_num].vidq.stopper);
1760ffd3c233SHans Verkuil 
1761ffd3c233SHans Verkuil 		pr_warn("device %d released!\n", chan_num);
1762ffd3c233SHans Verkuil 	}
1763ffd3c233SHans Verkuil 
1764ffd3c233SHans Verkuil }
1765ffd3c233SHans Verkuil 
1766ffd3c233SHans Verkuil int cx25821_video_register(struct cx25821_dev *dev)
1767ffd3c233SHans Verkuil {
1768ffd3c233SHans Verkuil 	int err;
1769ffd3c233SHans Verkuil 	int i;
1770ffd3c233SHans Verkuil 
1771ffd3c233SHans Verkuil 	spin_lock_init(&dev->slock);
1772ffd3c233SHans Verkuil 
1773ffd3c233SHans Verkuil 	for (i = 0; i < VID_CHANNEL_NUM; ++i) {
1774ffd3c233SHans Verkuil 		if (i == SRAM_CH08) /* audio channel */
1775ffd3c233SHans Verkuil 			continue;
1776ffd3c233SHans Verkuil 
1777ffd3c233SHans Verkuil 		cx25821_init_controls(dev, i);
1778ffd3c233SHans Verkuil 
1779ffd3c233SHans Verkuil 		cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
1780ffd3c233SHans Verkuil 			dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
1781ffd3c233SHans Verkuil 
1782ffd3c233SHans Verkuil 		dev->channels[i].sram_channels = &cx25821_sram_channels[i];
1783ffd3c233SHans Verkuil 		dev->channels[i].video_dev = NULL;
1784ffd3c233SHans Verkuil 		dev->channels[i].resources = 0;
1785ffd3c233SHans Verkuil 
1786ffd3c233SHans Verkuil 		cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
1787ffd3c233SHans Verkuil 
1788ffd3c233SHans Verkuil 		INIT_LIST_HEAD(&dev->channels[i].vidq.active);
1789ffd3c233SHans Verkuil 		INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
1790ffd3c233SHans Verkuil 
1791ffd3c233SHans Verkuil 		dev->channels[i].timeout_data.dev = dev;
1792ffd3c233SHans Verkuil 		dev->channels[i].timeout_data.channel =
1793ffd3c233SHans Verkuil 			&cx25821_sram_channels[i];
1794ffd3c233SHans Verkuil 		dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
1795ffd3c233SHans Verkuil 		dev->channels[i].vidq.timeout.data =
1796ffd3c233SHans Verkuil 			(unsigned long)&dev->channels[i].timeout_data;
1797ffd3c233SHans Verkuil 		init_timer(&dev->channels[i].vidq.timeout);
1798ffd3c233SHans Verkuil 
1799ffd3c233SHans Verkuil 		/* register v4l devices */
1800ffd3c233SHans Verkuil 		dev->channels[i].video_dev = cx25821_vdev_init(dev, dev->pci,
1801ffd3c233SHans Verkuil 				&cx25821_video_device, "video");
1802ffd3c233SHans Verkuil 
1803ffd3c233SHans Verkuil 		err = video_register_device(dev->channels[i].video_dev,
1804ffd3c233SHans Verkuil 				VFL_TYPE_GRABBER, video_nr[dev->nr]);
1805ffd3c233SHans Verkuil 
1806ffd3c233SHans Verkuil 		if (err < 0)
1807ffd3c233SHans Verkuil 			goto fail_unreg;
1808ffd3c233SHans Verkuil 
1809ffd3c233SHans Verkuil 	}
1810ffd3c233SHans Verkuil 
1811ffd3c233SHans Verkuil 	/* set PCI interrupt */
1812ffd3c233SHans Verkuil 	cx_set(PCI_INT_MSK, 0xff);
1813ffd3c233SHans Verkuil 
1814ffd3c233SHans Verkuil 	/* initial device configuration */
1815ffd3c233SHans Verkuil 	mutex_lock(&dev->lock);
181618c73af6SHans Verkuil 	dev->tvnorm = V4L2_STD_NTSC_M,
1817ffd3c233SHans Verkuil 	cx25821_set_tvnorm(dev, dev->tvnorm);
1818ffd3c233SHans Verkuil 	mutex_unlock(&dev->lock);
1819ffd3c233SHans Verkuil 
1820ffd3c233SHans Verkuil 	return 0;
1821ffd3c233SHans Verkuil 
1822ffd3c233SHans Verkuil fail_unreg:
1823ffd3c233SHans Verkuil 	cx25821_video_unregister(dev, i);
1824ffd3c233SHans Verkuil 	return err;
1825ffd3c233SHans Verkuil }
1826