blob: 8d5d13bb5f09f84f7380772f2a1bc8a3ce4ac4a5 [file] [log] [blame]
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -03001/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -03004 * Copyright (C) 2009 Conexant Systems Inc.
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -03005 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -03007 * Parts adapted/taken from Eduardo Moscoso Rubino
8 * Copyright (C) 2009 Eduardo Moscoso Rubino <moscoso@TopoLogica.com>
9 *
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 *
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
Joe Perches36d89f72010-11-07 17:48:21 -030027#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030029#include "cx25821-video.h"
30
31MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -030032MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030033MODULE_LICENSE("GPL");
34
Mauro Carvalho Chehab53e712d2009-09-13 11:39:12 -030035static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030036
37module_param_array(video_nr, int, NULL, 0444);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030038
39MODULE_PARM_DESC(video_nr, "video device numbers");
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030040
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030041static unsigned int video_debug = VIDEO_DEBUG;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030042module_param(video_debug, int, 0644);
43MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
44
45static unsigned int irq_debug;
46module_param(irq_debug, int, 0644);
47MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
48
Hans Verkuil95c232a2013-04-13 07:41:29 -030049static unsigned int vid_limit = 16;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030050module_param(vid_limit, int, 0644);
51MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
52
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030053#define FORMAT_FLAGS_PACKED 0x01
54
Hans Verkuil95c232a2013-04-13 07:41:29 -030055static const struct cx25821_fmt formats[] = {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030056 {
Mauro Carvalho Chehabd7d93382010-03-24 14:23:25 -030057 .name = "4:1:1, packed, Y41P",
58 .fourcc = V4L2_PIX_FMT_Y41P,
59 .depth = 12,
60 .flags = FORMAT_FLAGS_PACKED,
61 }, {
62 .name = "4:2:2, packed, YUYV",
63 .fourcc = V4L2_PIX_FMT_YUYV,
64 .depth = 16,
65 .flags = FORMAT_FLAGS_PACKED,
Mauro Carvalho Chehabd7d93382010-03-24 14:23:25 -030066 },
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030067};
68
Hans Verkuil95c232a2013-04-13 07:41:29 -030069static const struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030070{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030071 unsigned int i;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030072
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030073 for (i = 0; i < ARRAY_SIZE(formats); i++)
74 if (formats[i].fourcc == fourcc)
75 return formats + i;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030076 return NULL;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030077}
78
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030079void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
80 u32 count)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030081{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030082 struct cx25821_buffer *buf;
83 int bc;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -030084
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030085 for (bc = 0;; bc++) {
86 if (list_empty(&q->active)) {
87 dprintk(1, "bc=%d (=0: active empty)\n", bc);
88 break;
89 }
90
Leonid V. Fedorenchik0abfefb2011-10-22 01:43:55 -030091 buf = list_entry(q->active.next, struct cx25821_buffer,
92 vb.queue);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030093
94 /* count comes from the hw and it is 16bit wide --
95 * this trick handles wrap-arounds correctly for
96 * up to 32767 buffers in flight... */
Leonid V. Fedorenchike313e1f2011-09-16 14:15:13 +080097 if ((s16) (count - buf->count) < 0)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030098 break;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -030099
Sakari Ailus8e6057b2012-09-15 15:14:42 -0300100 v4l2_get_timestamp(&buf->vb.ts);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300101 buf->vb.state = VIDEOBUF_DONE;
102 list_del(&buf->vb.queue);
103 wake_up(&buf->vb.done);
104 }
105
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300106 if (list_empty(&q->active))
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300107 del_timer(&q->timeout);
108 else
109 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
110 if (bc != 1)
Leonid V. Fedorenchik692cfb92011-09-16 14:14:36 +0800111 pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300112}
113
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300114int cx25821_start_video_dma(struct cx25821_dev *dev,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300115 struct cx25821_dmaqueue *q,
116 struct cx25821_buffer *buf,
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300117 const struct sram_channel *channel)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300118{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300119 int tmp = 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300120
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300121 /* setup fifo + format */
122 cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300123
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300124 /* reset counter */
125 cx_write(channel->gpcnt_ctl, 3);
126 q->count = 1;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300127
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300128 /* enable irq */
129 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
130 cx_set(channel->int_msk, 0x11);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300131
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300132 /* start dma */
133 cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300134
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300135 /* make sure upstream setting if any is reversed */
136 tmp = cx_read(VID_CH_MODE_SEL);
137 cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300138
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300139 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300140}
141
Mauro Carvalho Chehabdafc4562012-10-27 12:42:59 -0300142static int cx25821_restart_video_queue(struct cx25821_dev *dev,
143 struct cx25821_dmaqueue *q,
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300144 const struct sram_channel *channel)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300145{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300146 struct cx25821_buffer *buf, *prev;
147 struct list_head *item;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300148
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300149 if (!list_empty(&q->active)) {
Leonid V. Fedorenchik0abfefb2011-10-22 01:43:55 -0300150 buf = list_entry(q->active.next, struct cx25821_buffer,
151 vb.queue);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300152
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300153 cx25821_start_video_dma(dev, q, buf, channel);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300154
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300155 list_for_each(item, &q->active) {
156 buf = list_entry(item, struct cx25821_buffer, vb.queue);
157 buf->count = q->count++;
158 }
159
160 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
161 return 0;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300162 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300163
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300164 prev = NULL;
165 for (;;) {
166 if (list_empty(&q->queued))
167 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300168
Leonid V. Fedorenchik0abfefb2011-10-22 01:43:55 -0300169 buf = list_entry(q->queued.next, struct cx25821_buffer,
170 vb.queue);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300171
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300172 if (NULL == prev) {
173 list_move_tail(&buf->vb.queue, &q->active);
174 cx25821_start_video_dma(dev, q, buf, channel);
175 buf->vb.state = VIDEOBUF_ACTIVE;
176 buf->count = q->count++;
177 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
178 } else if (prev->vb.width == buf->vb.width &&
179 prev->vb.height == buf->vb.height &&
180 prev->fmt == buf->fmt) {
181 list_move_tail(&buf->vb.queue, &q->active);
182 buf->vb.state = VIDEOBUF_ACTIVE;
183 buf->count = q->count++;
184 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
Leonid V. Fedorenchik69dfe452011-09-16 14:14:38 +0800185 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300186 } else {
187 return 0;
188 }
189 prev = buf;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300190 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300191}
192
Mauro Carvalho Chehabdafc4562012-10-27 12:42:59 -0300193static void cx25821_vid_timeout(unsigned long data)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300194{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300195 struct cx25821_data *timeout_data = (struct cx25821_data *)data;
196 struct cx25821_dev *dev = timeout_data->dev;
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300197 const struct sram_channel *channel = timeout_data->channel;
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300198 struct cx25821_dmaqueue *q = &dev->channels[channel->i].dma_vidq;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300199 struct cx25821_buffer *buf;
200 unsigned long flags;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300201
Leonid V. Fedorenchik69dfe452011-09-16 14:14:38 +0800202 /* cx25821_sram_channel_dump(dev, channel); */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300203 cx_clear(channel->dma_ctl, 0x11);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300204
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300205 spin_lock_irqsave(&dev->slock, flags);
206 while (!list_empty(&q->active)) {
Leonid V. Fedorenchik0abfefb2011-10-22 01:43:55 -0300207 buf = list_entry(q->active.next, struct cx25821_buffer,
208 vb.queue);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300209 list_del(&buf->vb.queue);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300210
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300211 buf->vb.state = VIDEOBUF_ERROR;
212 wake_up(&buf->vb.done);
213 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300214
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300215 cx25821_restart_video_queue(dev, q, channel);
216 spin_unlock_irqrestore(&dev->slock, flags);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300217}
218
219int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
220{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300221 u32 count = 0;
222 int handled = 0;
223 u32 mask;
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300224 const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300225
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300226 mask = cx_read(channel->int_msk);
227 if (0 == (status & mask))
228 return handled;
229
230 cx_write(channel->int_stat, status);
231
232 /* risc op code error */
233 if (status & (1 << 16)) {
Joe Perches36d89f72010-11-07 17:48:21 -0300234 pr_warn("%s, %s: video risc op code error\n",
235 dev->name, channel->name);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300236 cx_clear(channel->dma_ctl, 0x11);
237 cx25821_sram_channel_dump(dev, channel);
238 }
239
240 /* risc1 y */
241 if (status & FLD_VID_DST_RISC1) {
242 spin_lock(&dev->slock);
243 count = cx_read(channel->gpcnt);
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300244 cx25821_video_wakeup(dev, &dev->channels[channel->i].dma_vidq,
Leonid V. Fedorenchik69dfe452011-09-16 14:14:38 +0800245 count);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300246 spin_unlock(&dev->slock);
247 handled++;
248 }
249
250 /* risc2 y */
251 if (status & 0x10) {
252 dprintk(2, "stopper video\n");
253 spin_lock(&dev->slock);
Leonid V. Fedorenchik788d0f32011-09-16 14:14:39 +0800254 cx25821_restart_video_queue(dev,
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300255 &dev->channels[channel->i].dma_vidq, channel);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300256 spin_unlock(&dev->slock);
257 handled++;
258 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300259 return handled;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300260}
261
Hans Verkuil95c232a2013-04-13 07:41:29 -0300262static int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300263 unsigned int *size)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300264{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300265 struct cx25821_channel *chan = q->priv_data;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300266
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300267 *size = chan->fmt->depth * chan->width * chan->height >> 3;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300268
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300269 if (0 == *count)
270 *count = 32;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300271
Andreas Bombedab7e312010-03-21 16:02:45 -0300272 if (*size * *count > vid_limit * 1024 * 1024)
273 *count = (vid_limit * 1024 * 1024) / *size;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300274
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300275 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300276}
277
Hans Verkuil95c232a2013-04-13 07:41:29 -0300278static int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300279 enum v4l2_field field)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300280{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300281 struct cx25821_channel *chan = q->priv_data;
282 struct cx25821_dev *dev = chan->dev;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300283 struct cx25821_buffer *buf =
Leonid V. Fedorenchikf2539812011-10-22 01:43:54 -0300284 container_of(vb, struct cx25821_buffer, vb);
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300285 int rc, init_buffer = 0;
Hans Verkuil30fdf032012-04-20 06:26:19 -0300286 u32 line0_offset;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300287 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
288 int bpl_local = LINE_SIZE_D1;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300289
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300290 BUG_ON(NULL == chan->fmt);
291 if (chan->width < 48 || chan->width > 720 ||
292 chan->height < 32 || chan->height > 576)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300293 return -EINVAL;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300294
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300295 buf->vb.size = (chan->width * chan->height * chan->fmt->depth) >> 3;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300296
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300297 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
298 return -EINVAL;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300299
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300300 if (buf->fmt != chan->fmt ||
301 buf->vb.width != chan->width ||
302 buf->vb.height != chan->height || buf->vb.field != field) {
303 buf->fmt = chan->fmt;
304 buf->vb.width = chan->width;
305 buf->vb.height = chan->height;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300306 buf->vb.field = field;
307 init_buffer = 1;
308 }
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300309
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300310 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
311 init_buffer = 1;
312 rc = videobuf_iolock(q, &buf->vb, NULL);
313 if (0 != rc) {
Joe Perches36d89f72010-11-07 17:48:21 -0300314 printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300315 goto fail;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300316 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300317 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300318
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300319 dprintk(1, "init_buffer=%d\n", init_buffer);
320
321 if (init_buffer) {
Hans Verkuil8d125c52013-04-14 11:57:18 -0300322 if (chan->pixel_formats == PIXEL_FRMT_411)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300323 buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
324 else
325 buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
326
Hans Verkuil8d125c52013-04-14 11:57:18 -0300327 if (chan->pixel_formats == PIXEL_FRMT_411) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300328 bpl_local = buf->bpl;
329 } else {
Leonid V. Fedorenchik8e4ac072011-09-16 14:14:43 +0800330 bpl_local = buf->bpl; /* Default */
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300331
Hans Verkuil8d125c52013-04-14 11:57:18 -0300332 if (chan->use_cif_resolution) {
Hans Verkuil988f7b82013-04-13 13:06:00 -0300333 if (dev->tvnorm & V4L2_STD_625_50)
Hans Verkuil8d125c52013-04-14 11:57:18 -0300334 bpl_local = 352 << 1;
335 else
336 bpl_local = chan->cif_width << 1;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300337 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300338 }
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300339
340 switch (buf->vb.field) {
341 case V4L2_FIELD_TOP:
342 cx25821_risc_buffer(dev->pci, &buf->risc,
343 dma->sglist, 0, UNSET,
344 buf->bpl, 0, buf->vb.height);
345 break;
346 case V4L2_FIELD_BOTTOM:
347 cx25821_risc_buffer(dev->pci, &buf->risc,
348 dma->sglist, UNSET, 0,
349 buf->bpl, 0, buf->vb.height);
350 break;
351 case V4L2_FIELD_INTERLACED:
352 /* All other formats are top field first */
353 line0_offset = 0;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300354 dprintk(1, "top field first\n");
355
356 cx25821_risc_buffer(dev->pci, &buf->risc,
357 dma->sglist, line0_offset,
358 bpl_local, bpl_local, bpl_local,
359 buf->vb.height >> 1);
360 break;
361 case V4L2_FIELD_SEQ_TB:
362 cx25821_risc_buffer(dev->pci, &buf->risc,
363 dma->sglist,
364 0, buf->bpl * (buf->vb.height >> 1),
365 buf->bpl, 0, buf->vb.height >> 1);
366 break;
367 case V4L2_FIELD_SEQ_BT:
368 cx25821_risc_buffer(dev->pci, &buf->risc,
369 dma->sglist,
370 buf->bpl * (buf->vb.height >> 1), 0,
371 buf->bpl, 0, buf->vb.height >> 1);
372 break;
373 default:
374 BUG();
375 }
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300376 }
377
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300378 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300379 buf, buf->vb.i, chan->width, chan->height, chan->fmt->depth,
380 chan->fmt->name, (unsigned long)buf->risc.dma);
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300381
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300382 buf->vb.state = VIDEOBUF_PREPARED;
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300383
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300384 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300385
Leonid V. Fedorenchik2748f262011-09-16 14:14:34 +0800386fail:
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300387 cx25821_free_buffer(q, buf);
388 return rc;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300389}
390
Hans Verkuil95c232a2013-04-13 07:41:29 -0300391static void cx25821_buffer_release(struct videobuf_queue *q,
Leonid V. Fedorenchikc1e6e242011-09-16 14:14:35 +0800392 struct videobuf_buffer *vb)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300393{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300394 struct cx25821_buffer *buf =
Leonid V. Fedorenchikf2539812011-10-22 01:43:54 -0300395 container_of(vb, struct cx25821_buffer, vb);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300396
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300397 cx25821_free_buffer(q, buf);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300398}
399
Hans Verkuil95c232a2013-04-13 07:41:29 -0300400static int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300401{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300402 struct cx25821_channel *chan = video_drvdata(file);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300403
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300404 return videobuf_mmap_mapper(&chan->vidq, vma);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300405}
406
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300407
408static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
409{
Leonid V. Fedorenchik8e4ac072011-09-16 14:14:43 +0800410 struct cx25821_buffer *buf =
Leonid V. Fedorenchikf2539812011-10-22 01:43:54 -0300411 container_of(vb, struct cx25821_buffer, vb);
Leonid V. Fedorenchike6cf66c2011-09-16 14:14:44 +0800412 struct cx25821_buffer *prev;
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300413 struct cx25821_channel *chan = vq->priv_data;
414 struct cx25821_dev *dev = chan->dev;
415 struct cx25821_dmaqueue *q = &dev->channels[chan->id].dma_vidq;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300416
Leonid V. Fedorenchike6cf66c2011-09-16 14:14:44 +0800417 /* add jump to stopper */
418 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
419 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
420 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300421
Leonid V. Fedorenchike6cf66c2011-09-16 14:14:44 +0800422 dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300423
Leonid V. Fedorenchike6cf66c2011-09-16 14:14:44 +0800424 if (!list_empty(&q->queued)) {
425 list_add_tail(&buf->vb.queue, &q->queued);
426 buf->vb.state = VIDEOBUF_QUEUED;
427 dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
428 buf->vb.i);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300429
Leonid V. Fedorenchike6cf66c2011-09-16 14:14:44 +0800430 } else if (list_empty(&q->active)) {
431 list_add_tail(&buf->vb.queue, &q->active);
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300432 cx25821_start_video_dma(dev, q, buf, chan->sram_channels);
Leonid V. Fedorenchike6cf66c2011-09-16 14:14:44 +0800433 buf->vb.state = VIDEOBUF_ACTIVE;
434 buf->count = q->count++;
435 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
436 dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
437 buf, buf->vb.i, buf->count, q->count);
438 } else {
439 prev = list_entry(q->active.prev, struct cx25821_buffer,
440 vb.queue);
441 if (prev->vb.width == buf->vb.width
Mauro Carvalho Chehab3e9442c2010-07-04 15:37:05 -0300442 && prev->vb.height == buf->vb.height
443 && prev->fmt == buf->fmt) {
Leonid V. Fedorenchik8ebbda42011-09-16 14:14:45 +0800444 list_add_tail(&buf->vb.queue, &q->active);
445 buf->vb.state = VIDEOBUF_ACTIVE;
446 buf->count = q->count++;
447 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300448
Leonid V. Fedorenchik8ebbda42011-09-16 14:14:45 +0800449 /* 64 bit bits 63-32 */
450 prev->risc.jmp[2] = cpu_to_le32(0);
451 dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
452 buf, buf->vb.i, buf->count);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300453
Leonid V. Fedorenchik8ebbda42011-09-16 14:14:45 +0800454 } else {
455 list_add_tail(&buf->vb.queue, &q->queued);
456 buf->vb.state = VIDEOBUF_QUEUED;
457 dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
458 buf->vb.i);
459 }
460 }
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300461
Leonid V. Fedorenchik8ebbda42011-09-16 14:14:45 +0800462 if (list_empty(&q->active))
463 dprintk(2, "active queue empty!\n");
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300464}
465
466static struct videobuf_queue_ops cx25821_video_qops = {
Leonid V. Fedorenchikfb5f2c82011-09-16 14:14:46 +0800467 .buf_setup = cx25821_buffer_setup,
468 .buf_prepare = cx25821_buffer_prepare,
469 .buf_queue = buffer_queue,
470 .buf_release = cx25821_buffer_release,
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300471};
472
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300473static ssize_t video_read(struct file *file, char __user * data, size_t count,
Mauro Carvalho Chehab3e9442c2010-07-04 15:37:05 -0300474 loff_t *ppos)
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300475{
Hans Verkuil8d125c52013-04-14 11:57:18 -0300476 struct v4l2_fh *fh = file->private_data;
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300477 struct cx25821_channel *chan = video_drvdata(file);
Hans Verkuil8d125c52013-04-14 11:57:18 -0300478 struct cx25821_dev *dev = chan->dev;
Hans Verkuil84293f02013-04-14 11:56:39 -0300479 int err = 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300480
Hans Verkuil11f095a2013-04-14 11:54:56 -0300481 if (mutex_lock_interruptible(&dev->lock))
482 return -ERESTARTSYS;
Hans Verkuil84293f02013-04-14 11:56:39 -0300483 if (chan->streaming_fh && chan->streaming_fh != fh) {
Hans Verkuil11f095a2013-04-14 11:54:56 -0300484 err = -EBUSY;
Hans Verkuil84293f02013-04-14 11:56:39 -0300485 goto unlock;
486 }
487 chan->streaming_fh = fh;
488
489 err = videobuf_read_one(&chan->vidq, data, count, ppos,
Hans Verkuil11f095a2013-04-14 11:54:56 -0300490 file->f_flags & O_NONBLOCK);
Hans Verkuil84293f02013-04-14 11:56:39 -0300491unlock:
Hans Verkuil11f095a2013-04-14 11:54:56 -0300492 mutex_unlock(&dev->lock);
493 return err;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300494}
495
496static unsigned int video_poll(struct file *file,
Mauro Carvalho Chehab3e9442c2010-07-04 15:37:05 -0300497 struct poll_table_struct *wait)
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300498{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300499 struct cx25821_channel *chan = video_drvdata(file);
Hans Verkuil8d125c52013-04-14 11:57:18 -0300500 unsigned long req_events = poll_requested_events(wait);
501 unsigned int res = v4l2_ctrl_poll(file, wait);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300502
Hans Verkuil8d125c52013-04-14 11:57:18 -0300503 if (req_events & (POLLIN | POLLRDNORM))
504 res |= videobuf_poll_stream(file, &chan->vidq, wait);
505 return res;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300506
Hans Verkuil84293f02013-04-14 11:56:39 -0300507 /* This doesn't belong in poll(). This can be done
508 * much better with vb2. We keep this code here as a
509 * reminder.
510 if ((res & POLLIN) && buf->vb.state == VIDEOBUF_DONE) {
511 struct cx25821_dev *dev = chan->dev;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300512
Hans Verkuil84293f02013-04-14 11:56:39 -0300513 if (dev && chan->use_cif_resolution) {
514 u8 cam_id = *((char *)buf->vb.baddr + 3);
515 memcpy((char *)buf->vb.baddr,
516 (char *)buf->vb.baddr + (chan->width * 2),
517 (chan->width * 2));
518 *((char *)buf->vb.baddr + 3) = cam_id;
Leonid V. Fedorenchik55c37c02011-09-16 14:14:50 +0800519 }
Leonid V. Fedorenchik55c37c02011-09-16 14:14:50 +0800520 }
Hans Verkuil84293f02013-04-14 11:56:39 -0300521 */
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300522}
523
524static int video_release(struct file *file)
525{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300526 struct cx25821_channel *chan = video_drvdata(file);
Hans Verkuil8d125c52013-04-14 11:57:18 -0300527 struct v4l2_fh *fh = file->private_data;
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300528 struct cx25821_dev *dev = chan->dev;
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300529 const struct sram_channel *sram_ch =
530 dev->channels[0].sram_channels;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300531
Hans Verkuilbe178cb2013-04-14 11:53:35 -0300532 mutex_lock(&dev->lock);
Leonid V. Fedorenchik21377cd2011-09-16 14:14:51 +0800533 /* stop the risc engine and fifo */
Hans Verkuilbfef0d32013-04-13 06:28:54 -0300534 cx_write(sram_ch->dma_ctl, 0); /* FIFO and RISC disable */
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300535
Leonid V. Fedorenchik21377cd2011-09-16 14:14:51 +0800536 /* stop video capture */
Hans Verkuil84293f02013-04-14 11:56:39 -0300537 if (chan->streaming_fh == fh) {
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300538 videobuf_queue_cancel(&chan->vidq);
Hans Verkuil84293f02013-04-14 11:56:39 -0300539 chan->streaming_fh = NULL;
Leonid V. Fedorenchik21377cd2011-09-16 14:14:51 +0800540 }
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300541
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300542 if (chan->vidq.read_buf) {
543 cx25821_buffer_release(&chan->vidq, chan->vidq.read_buf);
544 kfree(chan->vidq.read_buf);
Leonid V. Fedorenchik21377cd2011-09-16 14:14:51 +0800545 }
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300546
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300547 videobuf_mmap_free(&chan->vidq);
Hans Verkuil84293f02013-04-14 11:56:39 -0300548 mutex_unlock(&dev->lock);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300549
Hans Verkuil8d125c52013-04-14 11:57:18 -0300550 return v4l2_fh_release(file);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300551}
552
Hans Verkuil95c232a2013-04-13 07:41:29 -0300553/* VIDEO IOCTLS */
554static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
555 struct v4l2_format *f)
556{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300557 struct cx25821_channel *chan = video_drvdata(file);
Hans Verkuil95c232a2013-04-13 07:41:29 -0300558
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300559 f->fmt.pix.width = chan->width;
560 f->fmt.pix.height = chan->height;
561 f->fmt.pix.field = chan->vidq.field;
562 f->fmt.pix.pixelformat = chan->fmt->fourcc;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300563 f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
564 f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
565 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
566 f->fmt.pix.priv = 0;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300567
568 return 0;
569}
570
571static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
572 struct v4l2_format *f)
573{
Hans Verkuil988f7b82013-04-13 13:06:00 -0300574 struct cx25821_channel *chan = video_drvdata(file);
575 struct cx25821_dev *dev = chan->dev;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300576 const struct cx25821_fmt *fmt;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300577 enum v4l2_field field = f->fmt.pix.field;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300578 unsigned int maxw, maxh;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300579 unsigned w;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300580
581 fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
582 if (NULL == fmt)
583 return -EINVAL;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300584 maxw = 720;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300585 maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300586
Hans Verkuil988f7b82013-04-13 13:06:00 -0300587 w = f->fmt.pix.width;
588 if (field != V4L2_FIELD_BOTTOM)
589 field = V4L2_FIELD_TOP;
590 if (w < 352) {
591 w = 176;
592 f->fmt.pix.height = maxh / 4;
593 } else if (w < 720) {
594 w = 352;
595 f->fmt.pix.height = maxh / 2;
596 } else {
597 w = 720;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300598 f->fmt.pix.height = maxh;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300599 field = V4L2_FIELD_INTERLACED;
600 }
601 f->fmt.pix.field = field;
602 f->fmt.pix.width = w;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300603 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
604 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300605 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
606 f->fmt.pix.priv = 0;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300607
608 return 0;
609}
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300610static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
611{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300612 struct cx25821_channel *chan = video_drvdata(file);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300613
Hans Verkuil11f095a2013-04-14 11:54:56 -0300614 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Leonid V. Fedorenchik3f3d9e42011-09-16 14:14:52 +0800615 return -EINVAL;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300616
Hans Verkuil8d125c52013-04-14 11:57:18 -0300617 if (chan->streaming_fh && chan->streaming_fh != priv)
Leonid V. Fedorenchik3f3d9e42011-09-16 14:14:52 +0800618 return -EBUSY;
Hans Verkuil8d125c52013-04-14 11:57:18 -0300619 chan->streaming_fh = priv;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300620
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300621 return videobuf_streamon(&chan->vidq);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300622}
623
624static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
625{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300626 struct cx25821_channel *chan = video_drvdata(file);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300627
Hans Verkuil11f095a2013-04-14 11:54:56 -0300628 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
Leonid V. Fedorenchikeda59eb2011-09-16 14:14:53 +0800629 return -EINVAL;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300630
Hans Verkuil8d125c52013-04-14 11:57:18 -0300631 if (chan->streaming_fh && chan->streaming_fh != priv)
Hans Verkuil84293f02013-04-14 11:56:39 -0300632 return -EBUSY;
633 if (chan->streaming_fh == NULL)
634 return 0;
635
636 chan->streaming_fh = NULL;
637 return videobuf_streamoff(&chan->vidq);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300638}
639
640static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Leonid V. Fedorenchikc1e6e242011-09-16 14:14:35 +0800641 struct v4l2_format *f)
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300642{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300643 struct cx25821_channel *chan = video_drvdata(file);
Hans Verkuil8d125c52013-04-14 11:57:18 -0300644 struct cx25821_dev *dev = chan->dev;
Leonid V. Fedorenchika39bea32011-09-16 14:14:54 +0800645 int pix_format = PIXEL_FRMT_422;
Hans Verkuila6aa0dc2013-04-13 13:34:34 -0300646 int err;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300647
Leonid V. Fedorenchik255c0402011-09-16 14:14:55 +0800648 err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300649
Leonid V. Fedorenchik255c0402011-09-16 14:14:55 +0800650 if (0 != err)
651 return err;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300652
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300653 chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
654 chan->vidq.field = f->fmt.pix.field;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300655 chan->width = f->fmt.pix.width;
656 chan->height = f->fmt.pix.height;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300657
Leonid V. Fedorenchik66787622011-09-16 14:14:56 +0800658 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
659 pix_format = PIXEL_FRMT_411;
Leonid V. Fedorenchik66787622011-09-16 14:14:56 +0800660 else
Hans Verkuil988f7b82013-04-13 13:06:00 -0300661 pix_format = PIXEL_FRMT_422;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300662
Leonid V. Fedorenchik66787622011-09-16 14:14:56 +0800663 cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300664
Leonid V. Fedorenchik66787622011-09-16 14:14:56 +0800665 /* check if cif resolution */
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300666 if (chan->width == 320 || chan->width == 352)
667 chan->use_cif_resolution = 1;
Leonid V. Fedorenchik66787622011-09-16 14:14:56 +0800668 else
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300669 chan->use_cif_resolution = 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300670
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300671 chan->cif_width = chan->width;
672 medusa_set_resolution(dev, chan->width, SRAM_CH00);
Leonid V. Fedorenchik66787622011-09-16 14:14:56 +0800673 return 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300674}
675
676static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
677{
Leonid V. Fedorenchik02859b62011-09-16 14:14:57 +0800678 int ret_val = 0;
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300679 struct cx25821_channel *chan = video_drvdata(file);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300680
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300681 ret_val = videobuf_dqbuf(&chan->vidq, p, file->f_flags & O_NONBLOCK);
682 p->sequence = chan->dma_vidq.count;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300683
Leonid V. Fedorenchik02859b62011-09-16 14:14:57 +0800684 return ret_val;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300685}
686
687static int vidioc_log_status(struct file *file, void *priv)
688{
Hans Verkuil8d125c52013-04-14 11:57:18 -0300689 struct cx25821_channel *chan = video_drvdata(file);
690 struct cx25821_dev *dev = chan->dev;
691 const struct sram_channel *sram_ch = chan->sram_channels;
Leonid V. Fedorenchik02859b62011-09-16 14:14:57 +0800692 u32 tmp = 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300693
Leonid V. Fedorenchik02859b62011-09-16 14:14:57 +0800694 tmp = cx_read(sram_ch->dma_ctl);
Joe Perches36d89f72010-11-07 17:48:21 -0300695 pr_info("Video input 0 is %s\n",
696 (tmp & 0x11) ? "streaming" : "stopped");
Leonid V. Fedorenchik02859b62011-09-16 14:14:57 +0800697 return 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300698}
699
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300700
Hans Verkuil95c232a2013-04-13 07:41:29 -0300701static int cx25821_vidioc_querycap(struct file *file, void *priv,
Leonid V. Fedorenchikc1e6e242011-09-16 14:14:35 +0800702 struct v4l2_capability *cap)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300703{
Hans Verkuil8d125c52013-04-14 11:57:18 -0300704 struct cx25821_channel *chan = video_drvdata(file);
705 struct cx25821_dev *dev = chan->dev;
Hans Verkuil3dd473c2013-04-13 06:06:18 -0300706 const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
707 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
708 const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300709
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300710 strcpy(cap->driver, "cx25821");
711 strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
712 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
Hans Verkuil8d125c52013-04-14 11:57:18 -0300713 if (chan->id >= VID_CHANNEL_NUM)
Hans Verkuil3dd473c2013-04-13 06:06:18 -0300714 cap->device_caps = cap_output;
715 else
716 cap->device_caps = cap_input;
717 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300718 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300719}
720
Hans Verkuil95c232a2013-04-13 07:41:29 -0300721static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300722 struct v4l2_fmtdesc *f)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300723{
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300724 if (unlikely(f->index >= ARRAY_SIZE(formats)))
725 return -EINVAL;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300726
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300727 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
728 f->pixelformat = formats[f->index].fourcc;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300729
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300730 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300731}
732
Hans Verkuil95c232a2013-04-13 07:41:29 -0300733static int cx25821_vidioc_reqbufs(struct file *file, void *priv,
Leonid V. Fedorenchikc1e6e242011-09-16 14:14:35 +0800734 struct v4l2_requestbuffers *p)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300735{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300736 struct cx25821_channel *chan = video_drvdata(file);
737
738 return videobuf_reqbufs(&chan->vidq, p);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300739}
740
Hans Verkuil95c232a2013-04-13 07:41:29 -0300741static int cx25821_vidioc_querybuf(struct file *file, void *priv,
Leonid V. Fedorenchikc1e6e242011-09-16 14:14:35 +0800742 struct v4l2_buffer *p)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300743{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300744 struct cx25821_channel *chan = video_drvdata(file);
745
746 return videobuf_querybuf(&chan->vidq, p);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300747}
748
Hans Verkuil95c232a2013-04-13 07:41:29 -0300749static int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300750{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300751 struct cx25821_channel *chan = video_drvdata(file);
752
753 return videobuf_qbuf(&chan->vidq, p);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300754}
755
Hans Verkuil95c232a2013-04-13 07:41:29 -0300756static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
Hans Verkuil18c73af2013-04-13 05:50:18 -0300757{
Hans Verkuil8d125c52013-04-14 11:57:18 -0300758 struct cx25821_channel *chan = video_drvdata(file);
Hans Verkuil18c73af2013-04-13 05:50:18 -0300759
Hans Verkuil8d125c52013-04-14 11:57:18 -0300760 *tvnorms = chan->dev->tvnorm;
Hans Verkuil18c73af2013-04-13 05:50:18 -0300761 return 0;
762}
763
Hans Verkuil314527a2013-03-15 06:10:40 -0300764int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300765{
Hans Verkuil8d125c52013-04-14 11:57:18 -0300766 struct cx25821_channel *chan = video_drvdata(file);
767 struct cx25821_dev *dev = chan->dev;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300768
Hans Verkuil314527a2013-03-15 06:10:40 -0300769 if (dev->tvnorm == tvnorms)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300770 return 0;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300771
Hans Verkuila6aa0dc2013-04-13 13:34:34 -0300772 dev->tvnorm = tvnorms;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300773 chan->width = 720;
774 chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300775
776 medusa_set_videostandard(dev);
777
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300778 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300779}
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300780
Hans Verkuil95c232a2013-04-13 07:41:29 -0300781static int cx25821_vidioc_enum_input(struct file *file, void *priv,
782 struct v4l2_input *i)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300783{
Hans Verkuila6aa0dc2013-04-13 13:34:34 -0300784 if (i->index)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300785 return -EINVAL;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300786
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300787 i->type = V4L2_INPUT_TYPE_CAMERA;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300788 i->std = CX25821_NORMS;
Hans Verkuila6aa0dc2013-04-13 13:34:34 -0300789 strcpy(i->name, "Composite");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300790 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300791}
792
Hans Verkuil95c232a2013-04-13 07:41:29 -0300793static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300794{
Hans Verkuila6aa0dc2013-04-13 13:34:34 -0300795 *i = 0;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300796 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300797}
798
Hans Verkuil95c232a2013-04-13 07:41:29 -0300799static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300800{
Hans Verkuila6aa0dc2013-04-13 13:34:34 -0300801 return i ? -EINVAL : 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300802}
803
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300804static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300805{
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300806 struct cx25821_channel *chan =
807 container_of(ctrl->handler, struct cx25821_channel, hdl);
808 struct cx25821_dev *dev = chan->dev;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300809
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300810 switch (ctrl->id) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300811 case V4L2_CID_BRIGHTNESS:
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300812 medusa_set_brightness(dev, ctrl->val, chan->id);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300813 break;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300814 case V4L2_CID_HUE:
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300815 medusa_set_hue(dev, ctrl->val, chan->id);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300816 break;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300817 case V4L2_CID_CONTRAST:
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300818 medusa_set_contrast(dev, ctrl->val, chan->id);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300819 break;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300820 case V4L2_CID_SATURATION:
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300821 medusa_set_saturation(dev, ctrl->val, chan->id);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300822 break;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300823 default:
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300824 return -EINVAL;
Joe Perches95cd17c2011-04-10 14:31:35 -0700825 }
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300826 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300827}
828
Hans Verkuil1f198872013-04-14 12:07:13 -0300829static int cx25821_vidioc_enum_output(struct file *file, void *priv,
830 struct v4l2_output *o)
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300831{
Hans Verkuil1f198872013-04-14 12:07:13 -0300832 if (o->index)
833 return -EINVAL;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300834
Hans Verkuil1f198872013-04-14 12:07:13 -0300835 o->type = V4L2_INPUT_TYPE_CAMERA;
836 o->std = CX25821_NORMS;
837 strcpy(o->name, "Composite");
Leonid V. Fedorenchik6f87cc62011-09-16 14:15:03 +0800838 return 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300839}
840
Hans Verkuil1f198872013-04-14 12:07:13 -0300841static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300842{
Hans Verkuil1f198872013-04-14 12:07:13 -0300843 *o = 0;
Leonid V. Fedorenchikf9ef6be2011-09-16 14:15:05 +0800844 return 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300845}
846
Hans Verkuil1f198872013-04-14 12:07:13 -0300847static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300848{
Hans Verkuil1f198872013-04-14 12:07:13 -0300849 return o ? -EINVAL : 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300850}
851
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300852static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
853 .s_ctrl = cx25821_s_ctrl,
854};
855
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300856static const struct v4l2_file_operations video_fops = {
Leonid V. Fedorenchikfa7ce1f2011-09-16 14:15:11 +0800857 .owner = THIS_MODULE,
Hans Verkuil8d125c52013-04-14 11:57:18 -0300858 .open = v4l2_fh_open,
Leonid V. Fedorenchikfa7ce1f2011-09-16 14:15:11 +0800859 .release = video_release,
860 .read = video_read,
861 .poll = video_poll,
862 .mmap = cx25821_video_mmap,
Hans Verkuil1f198872013-04-14 12:07:13 -0300863 .unlocked_ioctl = video_ioctl2,
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300864};
865
866static const struct v4l2_ioctl_ops video_ioctl_ops = {
Leonid V. Fedorenchikfa7ce1f2011-09-16 14:15:11 +0800867 .vidioc_querycap = cx25821_vidioc_querycap,
868 .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
869 .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
870 .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
871 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
872 .vidioc_reqbufs = cx25821_vidioc_reqbufs,
873 .vidioc_querybuf = cx25821_vidioc_querybuf,
874 .vidioc_qbuf = cx25821_vidioc_qbuf,
875 .vidioc_dqbuf = vidioc_dqbuf,
Hans Verkuil18c73af2013-04-13 05:50:18 -0300876 .vidioc_g_std = cx25821_vidioc_g_std,
Leonid V. Fedorenchikfa7ce1f2011-09-16 14:15:11 +0800877 .vidioc_s_std = cx25821_vidioc_s_std,
Leonid V. Fedorenchikfa7ce1f2011-09-16 14:15:11 +0800878 .vidioc_enum_input = cx25821_vidioc_enum_input,
879 .vidioc_g_input = cx25821_vidioc_g_input,
880 .vidioc_s_input = cx25821_vidioc_s_input,
Leonid V. Fedorenchikfa7ce1f2011-09-16 14:15:11 +0800881 .vidioc_streamon = vidioc_streamon,
882 .vidioc_streamoff = vidioc_streamoff,
883 .vidioc_log_status = vidioc_log_status,
Hans Verkuil8d125c52013-04-14 11:57:18 -0300884 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
885 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300886};
887
Hans Verkuilffd3c232013-04-13 05:30:48 -0300888static const struct video_device cx25821_video_device = {
889 .name = "cx25821-video",
Leonid V. Fedorenchik527db492011-09-16 14:15:12 +0800890 .fops = &video_fops,
Hans Verkuil467870c2013-04-13 08:18:00 -0300891 .release = video_device_release_empty,
Hans Verkuilffd3c232013-04-13 05:30:48 -0300892 .minor = -1,
Leonid V. Fedorenchik527db492011-09-16 14:15:12 +0800893 .ioctl_ops = &video_ioctl_ops,
894 .tvnorms = CX25821_NORMS,
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300895};
Hans Verkuilffd3c232013-04-13 05:30:48 -0300896
Hans Verkuil1f198872013-04-14 12:07:13 -0300897static const struct v4l2_file_operations video_out_fops = {
898 .owner = THIS_MODULE,
899 .open = v4l2_fh_open,
900 .release = v4l2_fh_release,
901 .unlocked_ioctl = video_ioctl2,
902};
903
904static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
905 .vidioc_querycap = cx25821_vidioc_querycap,
906 .vidioc_g_std = cx25821_vidioc_g_std,
907 .vidioc_s_std = cx25821_vidioc_s_std,
908 .vidioc_enum_output = cx25821_vidioc_enum_output,
909 .vidioc_g_output = cx25821_vidioc_g_output,
910 .vidioc_s_output = cx25821_vidioc_s_output,
911 .vidioc_log_status = vidioc_log_status,
912};
913
914static const struct video_device cx25821_video_out_device = {
915 .name = "cx25821-video",
916 .fops = &video_out_fops,
917 .release = video_device_release_empty,
918 .minor = -1,
919 .ioctl_ops = &video_out_ioctl_ops,
920 .tvnorms = CX25821_NORMS,
921};
922
Hans Verkuilffd3c232013-04-13 05:30:48 -0300923void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
924{
925 cx_clear(PCI_INT_MSK, 1);
926
Hans Verkuil467870c2013-04-13 08:18:00 -0300927 if (video_is_registered(&dev->channels[chan_num].vdev)) {
928 video_unregister_device(&dev->channels[chan_num].vdev);
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300929 v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
Hans Verkuilffd3c232013-04-13 05:30:48 -0300930
931 btcx_riscmem_free(dev->pci,
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300932 &dev->channels[chan_num].dma_vidq.stopper);
Hans Verkuilffd3c232013-04-13 05:30:48 -0300933 }
Hans Verkuilffd3c232013-04-13 05:30:48 -0300934}
935
936int cx25821_video_register(struct cx25821_dev *dev)
937{
938 int err;
939 int i;
940
Hans Verkuilbe178cb2013-04-14 11:53:35 -0300941 /* initial device configuration */
Hans Verkuila6aa0dc2013-04-13 13:34:34 -0300942 dev->tvnorm = V4L2_STD_NTSC_M;
Hans Verkuilbe178cb2013-04-14 11:53:35 -0300943
Hans Verkuilffd3c232013-04-13 05:30:48 -0300944 spin_lock_init(&dev->slock);
945
Hans Verkuil1f198872013-04-14 12:07:13 -0300946 for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; ++i) {
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300947 struct cx25821_channel *chan = &dev->channels[i];
948 struct video_device *vdev = &chan->vdev;
949 struct v4l2_ctrl_handler *hdl = &chan->hdl;
Hans Verkuil1f198872013-04-14 12:07:13 -0300950 bool is_output = i > SRAM_CH08;
Hans Verkuil467870c2013-04-13 08:18:00 -0300951
Hans Verkuilffd3c232013-04-13 05:30:48 -0300952 if (i == SRAM_CH08) /* audio channel */
953 continue;
954
Hans Verkuil1f198872013-04-14 12:07:13 -0300955 if (!is_output) {
956 v4l2_ctrl_handler_init(hdl, 4);
957 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
958 V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
959 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
960 V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
961 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
962 V4L2_CID_SATURATION, 0, 10000, 1, 5000);
963 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
964 V4L2_CID_HUE, 0, 10000, 1, 5000);
965 if (hdl->error) {
966 err = hdl->error;
967 goto fail_unreg;
968 }
969 err = v4l2_ctrl_handler_setup(hdl);
970 if (err)
971 goto fail_unreg;
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300972 }
Hans Verkuilffd3c232013-04-13 05:30:48 -0300973
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300974 cx25821_risc_stopper(dev->pci, &chan->dma_vidq.stopper,
975 chan->sram_channels->dma_ctl, 0x11, 0);
Hans Verkuilffd3c232013-04-13 05:30:48 -0300976
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300977 chan->sram_channels = &cx25821_sram_channels[i];
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300978 chan->width = 720;
979 if (dev->tvnorm & V4L2_STD_625_50)
980 chan->height = 576;
981 else
982 chan->height = 480;
Hans Verkuilffd3c232013-04-13 05:30:48 -0300983
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300984 if (chan->pixel_formats == PIXEL_FRMT_411)
985 chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
986 else
987 chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
Hans Verkuilffd3c232013-04-13 05:30:48 -0300988
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300989 cx_write(chan->sram_channels->int_stat, 0xffffffff);
Hans Verkuilffd3c232013-04-13 05:30:48 -0300990
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300991 INIT_LIST_HEAD(&chan->dma_vidq.active);
992 INIT_LIST_HEAD(&chan->dma_vidq.queued);
993
994 chan->timeout_data.dev = dev;
995 chan->timeout_data.channel = &cx25821_sram_channels[i];
996 chan->dma_vidq.timeout.function = cx25821_vid_timeout;
997 chan->dma_vidq.timeout.data = (unsigned long)&chan->timeout_data;
998 init_timer(&chan->dma_vidq.timeout);
999
Hans Verkuil1f198872013-04-14 12:07:13 -03001000 if (!is_output)
1001 videobuf_queue_sg_init(&chan->vidq, &cx25821_video_qops, &dev->pci->dev,
1002 &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
1003 V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
1004 chan, &dev->lock);
Hans Verkuilffd3c232013-04-13 05:30:48 -03001005
1006 /* register v4l devices */
Hans Verkuil1f198872013-04-14 12:07:13 -03001007 *vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
Hans Verkuil467870c2013-04-13 08:18:00 -03001008 vdev->v4l2_dev = &dev->v4l2_dev;
Hans Verkuil1f198872013-04-14 12:07:13 -03001009 if (!is_output)
1010 vdev->ctrl_handler = hdl;
1011 else
1012 vdev->vfl_dir = VFL_DIR_TX;
Hans Verkuilbe178cb2013-04-14 11:53:35 -03001013 vdev->lock = &dev->lock;
Hans Verkuil8d125c52013-04-14 11:57:18 -03001014 set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
Hans Verkuil467870c2013-04-13 08:18:00 -03001015 snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
Hans Verkuil2efe2cc2013-04-13 10:00:52 -03001016 video_set_drvdata(vdev, chan);
Hans Verkuilffd3c232013-04-13 05:30:48 -03001017
Hans Verkuil467870c2013-04-13 08:18:00 -03001018 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1019 video_nr[dev->nr]);
Hans Verkuilffd3c232013-04-13 05:30:48 -03001020
1021 if (err < 0)
1022 goto fail_unreg;
Hans Verkuilffd3c232013-04-13 05:30:48 -03001023 }
1024
1025 /* set PCI interrupt */
1026 cx_set(PCI_INT_MSK, 0xff);
1027
Hans Verkuilffd3c232013-04-13 05:30:48 -03001028 return 0;
1029
1030fail_unreg:
Hans Verkuil467870c2013-04-13 08:18:00 -03001031 while (i >= 0)
1032 cx25821_video_unregister(dev, i--);
Hans Verkuilffd3c232013-04-13 05:30:48 -03001033 return err;
1034}