blob: d3cf259f19f91038cff9e845eeeb627dceba6797 [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 */
Hans Verkuil4c1d0f72013-04-14 07:13:02 -0300554
555static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
556 struct v4l2_fmtdesc *f)
557{
558 if (unlikely(f->index >= ARRAY_SIZE(formats)))
559 return -EINVAL;
560
561 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
562 f->pixelformat = formats[f->index].fourcc;
563
564 return 0;
565}
566
Hans Verkuil95c232a2013-04-13 07:41:29 -0300567static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
568 struct v4l2_format *f)
569{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300570 struct cx25821_channel *chan = video_drvdata(file);
Hans Verkuil95c232a2013-04-13 07:41:29 -0300571
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300572 f->fmt.pix.width = chan->width;
573 f->fmt.pix.height = chan->height;
574 f->fmt.pix.field = chan->vidq.field;
575 f->fmt.pix.pixelformat = chan->fmt->fourcc;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300576 f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
577 f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
578 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
579 f->fmt.pix.priv = 0;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300580
581 return 0;
582}
583
584static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
585 struct v4l2_format *f)
586{
Hans Verkuil988f7b82013-04-13 13:06:00 -0300587 struct cx25821_channel *chan = video_drvdata(file);
588 struct cx25821_dev *dev = chan->dev;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300589 const struct cx25821_fmt *fmt;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300590 enum v4l2_field field = f->fmt.pix.field;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300591 unsigned int maxw, maxh;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300592 unsigned w;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300593
594 fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
595 if (NULL == fmt)
596 return -EINVAL;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300597 maxw = 720;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300598 maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300599
Hans Verkuil988f7b82013-04-13 13:06:00 -0300600 w = f->fmt.pix.width;
601 if (field != V4L2_FIELD_BOTTOM)
602 field = V4L2_FIELD_TOP;
603 if (w < 352) {
604 w = 176;
605 f->fmt.pix.height = maxh / 4;
606 } else if (w < 720) {
607 w = 352;
608 f->fmt.pix.height = maxh / 2;
609 } else {
610 w = 720;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300611 f->fmt.pix.height = maxh;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300612 field = V4L2_FIELD_INTERLACED;
613 }
614 f->fmt.pix.field = field;
615 f->fmt.pix.width = w;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300616 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
617 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300618 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
619 f->fmt.pix.priv = 0;
Hans Verkuil95c232a2013-04-13 07:41:29 -0300620
621 return 0;
622}
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300623
624static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Leonid V. Fedorenchikc1e6e242011-09-16 14:14:35 +0800625 struct v4l2_format *f)
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300626{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300627 struct cx25821_channel *chan = video_drvdata(file);
Hans Verkuil8d125c52013-04-14 11:57:18 -0300628 struct cx25821_dev *dev = chan->dev;
Leonid V. Fedorenchika39bea32011-09-16 14:14:54 +0800629 int pix_format = PIXEL_FRMT_422;
Hans Verkuila6aa0dc2013-04-13 13:34:34 -0300630 int err;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300631
Leonid V. Fedorenchik255c0402011-09-16 14:14:55 +0800632 err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300633
Leonid V. Fedorenchik255c0402011-09-16 14:14:55 +0800634 if (0 != err)
635 return err;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300636
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300637 chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
638 chan->vidq.field = f->fmt.pix.field;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300639 chan->width = f->fmt.pix.width;
640 chan->height = f->fmt.pix.height;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300641
Leonid V. Fedorenchik66787622011-09-16 14:14:56 +0800642 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
643 pix_format = PIXEL_FRMT_411;
Leonid V. Fedorenchik66787622011-09-16 14:14:56 +0800644 else
Hans Verkuil988f7b82013-04-13 13:06:00 -0300645 pix_format = PIXEL_FRMT_422;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300646
Leonid V. Fedorenchik66787622011-09-16 14:14:56 +0800647 cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300648
Leonid V. Fedorenchik66787622011-09-16 14:14:56 +0800649 /* check if cif resolution */
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300650 if (chan->width == 320 || chan->width == 352)
651 chan->use_cif_resolution = 1;
Leonid V. Fedorenchik66787622011-09-16 14:14:56 +0800652 else
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300653 chan->use_cif_resolution = 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300654
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300655 chan->cif_width = chan->width;
656 medusa_set_resolution(dev, chan->width, SRAM_CH00);
Leonid V. Fedorenchik66787622011-09-16 14:14:56 +0800657 return 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300658}
659
Hans Verkuil4c1d0f72013-04-14 07:13:02 -0300660static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
661{
662 struct cx25821_channel *chan = video_drvdata(file);
663
664 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
665 return -EINVAL;
666
667 if (chan->streaming_fh && chan->streaming_fh != priv)
668 return -EBUSY;
669 chan->streaming_fh = priv;
670
671 return videobuf_streamon(&chan->vidq);
672}
673
674static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
675{
676 struct cx25821_channel *chan = video_drvdata(file);
677
678 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
679 return -EINVAL;
680
681 if (chan->streaming_fh && chan->streaming_fh != priv)
682 return -EBUSY;
683 if (chan->streaming_fh == NULL)
684 return 0;
685
686 chan->streaming_fh = NULL;
687 return videobuf_streamoff(&chan->vidq);
688}
689
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300690static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
691{
Leonid V. Fedorenchik02859b62011-09-16 14:14:57 +0800692 int ret_val = 0;
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300693 struct cx25821_channel *chan = video_drvdata(file);
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300694
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300695 ret_val = videobuf_dqbuf(&chan->vidq, p, file->f_flags & O_NONBLOCK);
696 p->sequence = chan->dma_vidq.count;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300697
Leonid V. Fedorenchik02859b62011-09-16 14:14:57 +0800698 return ret_val;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300699}
700
701static int vidioc_log_status(struct file *file, void *priv)
702{
Hans Verkuil8d125c52013-04-14 11:57:18 -0300703 struct cx25821_channel *chan = video_drvdata(file);
704 struct cx25821_dev *dev = chan->dev;
705 const struct sram_channel *sram_ch = chan->sram_channels;
Leonid V. Fedorenchik02859b62011-09-16 14:14:57 +0800706 u32 tmp = 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300707
Leonid V. Fedorenchik02859b62011-09-16 14:14:57 +0800708 tmp = cx_read(sram_ch->dma_ctl);
Joe Perches36d89f72010-11-07 17:48:21 -0300709 pr_info("Video input 0 is %s\n",
710 (tmp & 0x11) ? "streaming" : "stopped");
Leonid V. Fedorenchik02859b62011-09-16 14:14:57 +0800711 return 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300712}
713
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300714
Hans Verkuil95c232a2013-04-13 07:41:29 -0300715static int cx25821_vidioc_querycap(struct file *file, void *priv,
Leonid V. Fedorenchikc1e6e242011-09-16 14:14:35 +0800716 struct v4l2_capability *cap)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300717{
Hans Verkuil8d125c52013-04-14 11:57:18 -0300718 struct cx25821_channel *chan = video_drvdata(file);
719 struct cx25821_dev *dev = chan->dev;
Hans Verkuil3dd473c2013-04-13 06:06:18 -0300720 const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
721 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
722 const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300723
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300724 strcpy(cap->driver, "cx25821");
725 strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
726 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
Hans Verkuil8d125c52013-04-14 11:57:18 -0300727 if (chan->id >= VID_CHANNEL_NUM)
Hans Verkuil3dd473c2013-04-13 06:06:18 -0300728 cap->device_caps = cap_output;
729 else
730 cap->device_caps = cap_input;
731 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300732 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300733}
734
Hans Verkuil95c232a2013-04-13 07:41:29 -0300735static int cx25821_vidioc_reqbufs(struct file *file, void *priv,
Leonid V. Fedorenchikc1e6e242011-09-16 14:14:35 +0800736 struct v4l2_requestbuffers *p)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300737{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300738 struct cx25821_channel *chan = video_drvdata(file);
739
740 return videobuf_reqbufs(&chan->vidq, p);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300741}
742
Hans Verkuil95c232a2013-04-13 07:41:29 -0300743static int cx25821_vidioc_querybuf(struct file *file, void *priv,
Leonid V. Fedorenchikc1e6e242011-09-16 14:14:35 +0800744 struct v4l2_buffer *p)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300745{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300746 struct cx25821_channel *chan = video_drvdata(file);
747
748 return videobuf_querybuf(&chan->vidq, p);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300749}
750
Hans Verkuil95c232a2013-04-13 07:41:29 -0300751static int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300752{
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300753 struct cx25821_channel *chan = video_drvdata(file);
754
755 return videobuf_qbuf(&chan->vidq, p);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300756}
757
Hans Verkuil95c232a2013-04-13 07:41:29 -0300758static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
Hans Verkuil18c73af2013-04-13 05:50:18 -0300759{
Hans Verkuil8d125c52013-04-14 11:57:18 -0300760 struct cx25821_channel *chan = video_drvdata(file);
Hans Verkuil18c73af2013-04-13 05:50:18 -0300761
Hans Verkuil8d125c52013-04-14 11:57:18 -0300762 *tvnorms = chan->dev->tvnorm;
Hans Verkuil18c73af2013-04-13 05:50:18 -0300763 return 0;
764}
765
Hans Verkuil314527a2013-03-15 06:10:40 -0300766int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300767{
Hans Verkuil8d125c52013-04-14 11:57:18 -0300768 struct cx25821_channel *chan = video_drvdata(file);
769 struct cx25821_dev *dev = chan->dev;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300770
Hans Verkuil314527a2013-03-15 06:10:40 -0300771 if (dev->tvnorm == tvnorms)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300772 return 0;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300773
Hans Verkuila6aa0dc2013-04-13 13:34:34 -0300774 dev->tvnorm = tvnorms;
Hans Verkuil988f7b82013-04-13 13:06:00 -0300775 chan->width = 720;
776 chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300777
778 medusa_set_videostandard(dev);
779
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300780 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300781}
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300782
Hans Verkuil95c232a2013-04-13 07:41:29 -0300783static int cx25821_vidioc_enum_input(struct file *file, void *priv,
784 struct v4l2_input *i)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300785{
Hans Verkuila6aa0dc2013-04-13 13:34:34 -0300786 if (i->index)
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300787 return -EINVAL;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300788
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300789 i->type = V4L2_INPUT_TYPE_CAMERA;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300790 i->std = CX25821_NORMS;
Hans Verkuila6aa0dc2013-04-13 13:34:34 -0300791 strcpy(i->name, "Composite");
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300792 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300793}
794
Hans Verkuil95c232a2013-04-13 07:41:29 -0300795static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
Mauro Carvalho Chehabbb4c9a72009-09-13 11:25:45 -0300796{
Hans Verkuila6aa0dc2013-04-13 13:34:34 -0300797 *i = 0;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300798 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300799}
800
Hans Verkuil95c232a2013-04-13 07:41:29 -0300801static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300802{
Hans Verkuila6aa0dc2013-04-13 13:34:34 -0300803 return i ? -EINVAL : 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300804}
805
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300806static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300807{
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300808 struct cx25821_channel *chan =
809 container_of(ctrl->handler, struct cx25821_channel, hdl);
810 struct cx25821_dev *dev = chan->dev;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300811
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300812 switch (ctrl->id) {
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300813 case V4L2_CID_BRIGHTNESS:
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300814 medusa_set_brightness(dev, ctrl->val, chan->id);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300815 break;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300816 case V4L2_CID_HUE:
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300817 medusa_set_hue(dev, ctrl->val, chan->id);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300818 break;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300819 case V4L2_CID_CONTRAST:
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300820 medusa_set_contrast(dev, ctrl->val, chan->id);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300821 break;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300822 case V4L2_CID_SATURATION:
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300823 medusa_set_saturation(dev, ctrl->val, chan->id);
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300824 break;
Mauro Carvalho Chehab1a9fc852009-09-13 11:30:11 -0300825 default:
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300826 return -EINVAL;
Joe Perches95cd17c2011-04-10 14:31:35 -0700827 }
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300828 return 0;
Mauro Carvalho Chehab02b20b02009-09-15 11:33:54 -0300829}
830
Hans Verkuil1f198872013-04-14 12:07:13 -0300831static int cx25821_vidioc_enum_output(struct file *file, void *priv,
832 struct v4l2_output *o)
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300833{
Hans Verkuil1f198872013-04-14 12:07:13 -0300834 if (o->index)
835 return -EINVAL;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300836
Hans Verkuil1f198872013-04-14 12:07:13 -0300837 o->type = V4L2_INPUT_TYPE_CAMERA;
838 o->std = CX25821_NORMS;
839 strcpy(o->name, "Composite");
Leonid V. Fedorenchik6f87cc62011-09-16 14:15:03 +0800840 return 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300841}
842
Hans Verkuil1f198872013-04-14 12:07:13 -0300843static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300844{
Hans Verkuil1f198872013-04-14 12:07:13 -0300845 *o = 0;
Leonid V. Fedorenchikf9ef6be2011-09-16 14:15:05 +0800846 return 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300847}
848
Hans Verkuil1f198872013-04-14 12:07:13 -0300849static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300850{
Hans Verkuil1f198872013-04-14 12:07:13 -0300851 return o ? -EINVAL : 0;
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300852}
853
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300854static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
855 .s_ctrl = cx25821_s_ctrl,
856};
857
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300858static const struct v4l2_file_operations video_fops = {
Leonid V. Fedorenchikfa7ce1f2011-09-16 14:15:11 +0800859 .owner = THIS_MODULE,
Hans Verkuil8d125c52013-04-14 11:57:18 -0300860 .open = v4l2_fh_open,
Leonid V. Fedorenchikfa7ce1f2011-09-16 14:15:11 +0800861 .release = video_release,
862 .read = video_read,
863 .poll = video_poll,
864 .mmap = cx25821_video_mmap,
Hans Verkuil1f198872013-04-14 12:07:13 -0300865 .unlocked_ioctl = video_ioctl2,
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300866};
867
868static const struct v4l2_ioctl_ops video_ioctl_ops = {
Leonid V. Fedorenchikfa7ce1f2011-09-16 14:15:11 +0800869 .vidioc_querycap = cx25821_vidioc_querycap,
870 .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
871 .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
872 .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
873 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
874 .vidioc_reqbufs = cx25821_vidioc_reqbufs,
875 .vidioc_querybuf = cx25821_vidioc_querybuf,
876 .vidioc_qbuf = cx25821_vidioc_qbuf,
877 .vidioc_dqbuf = vidioc_dqbuf,
Hans Verkuil18c73af2013-04-13 05:50:18 -0300878 .vidioc_g_std = cx25821_vidioc_g_std,
Leonid V. Fedorenchikfa7ce1f2011-09-16 14:15:11 +0800879 .vidioc_s_std = cx25821_vidioc_s_std,
Leonid V. Fedorenchikfa7ce1f2011-09-16 14:15:11 +0800880 .vidioc_enum_input = cx25821_vidioc_enum_input,
881 .vidioc_g_input = cx25821_vidioc_g_input,
882 .vidioc_s_input = cx25821_vidioc_s_input,
Leonid V. Fedorenchikfa7ce1f2011-09-16 14:15:11 +0800883 .vidioc_streamon = vidioc_streamon,
884 .vidioc_streamoff = vidioc_streamoff,
885 .vidioc_log_status = vidioc_log_status,
Hans Verkuil8d125c52013-04-14 11:57:18 -0300886 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
887 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300888};
889
Hans Verkuilffd3c232013-04-13 05:30:48 -0300890static const struct video_device cx25821_video_device = {
891 .name = "cx25821-video",
Leonid V. Fedorenchik527db492011-09-16 14:15:12 +0800892 .fops = &video_fops,
Hans Verkuil467870c2013-04-13 08:18:00 -0300893 .release = video_device_release_empty,
Hans Verkuilffd3c232013-04-13 05:30:48 -0300894 .minor = -1,
Leonid V. Fedorenchik527db492011-09-16 14:15:12 +0800895 .ioctl_ops = &video_ioctl_ops,
896 .tvnorms = CX25821_NORMS,
Palash Bandyopadhyay6d8c2ba2010-07-04 14:15:38 -0300897};
Hans Verkuilffd3c232013-04-13 05:30:48 -0300898
Hans Verkuil1f198872013-04-14 12:07:13 -0300899static const struct v4l2_file_operations video_out_fops = {
900 .owner = THIS_MODULE,
901 .open = v4l2_fh_open,
902 .release = v4l2_fh_release,
903 .unlocked_ioctl = video_ioctl2,
904};
905
906static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
907 .vidioc_querycap = cx25821_vidioc_querycap,
908 .vidioc_g_std = cx25821_vidioc_g_std,
909 .vidioc_s_std = cx25821_vidioc_s_std,
910 .vidioc_enum_output = cx25821_vidioc_enum_output,
911 .vidioc_g_output = cx25821_vidioc_g_output,
912 .vidioc_s_output = cx25821_vidioc_s_output,
913 .vidioc_log_status = vidioc_log_status,
914};
915
916static const struct video_device cx25821_video_out_device = {
917 .name = "cx25821-video",
918 .fops = &video_out_fops,
919 .release = video_device_release_empty,
920 .minor = -1,
921 .ioctl_ops = &video_out_ioctl_ops,
922 .tvnorms = CX25821_NORMS,
923};
924
Hans Verkuilffd3c232013-04-13 05:30:48 -0300925void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
926{
927 cx_clear(PCI_INT_MSK, 1);
928
Hans Verkuil467870c2013-04-13 08:18:00 -0300929 if (video_is_registered(&dev->channels[chan_num].vdev)) {
930 video_unregister_device(&dev->channels[chan_num].vdev);
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300931 v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
Hans Verkuilffd3c232013-04-13 05:30:48 -0300932
933 btcx_riscmem_free(dev->pci,
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300934 &dev->channels[chan_num].dma_vidq.stopper);
Hans Verkuilffd3c232013-04-13 05:30:48 -0300935 }
Hans Verkuilffd3c232013-04-13 05:30:48 -0300936}
937
938int cx25821_video_register(struct cx25821_dev *dev)
939{
940 int err;
941 int i;
942
Hans Verkuilbe178cb2013-04-14 11:53:35 -0300943 /* initial device configuration */
Hans Verkuila6aa0dc2013-04-13 13:34:34 -0300944 dev->tvnorm = V4L2_STD_NTSC_M;
Hans Verkuilbe178cb2013-04-14 11:53:35 -0300945
Hans Verkuilffd3c232013-04-13 05:30:48 -0300946 spin_lock_init(&dev->slock);
947
Hans Verkuil1f198872013-04-14 12:07:13 -0300948 for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; ++i) {
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300949 struct cx25821_channel *chan = &dev->channels[i];
950 struct video_device *vdev = &chan->vdev;
951 struct v4l2_ctrl_handler *hdl = &chan->hdl;
Hans Verkuil1f198872013-04-14 12:07:13 -0300952 bool is_output = i > SRAM_CH08;
Hans Verkuil467870c2013-04-13 08:18:00 -0300953
Hans Verkuilffd3c232013-04-13 05:30:48 -0300954 if (i == SRAM_CH08) /* audio channel */
955 continue;
956
Hans Verkuil1f198872013-04-14 12:07:13 -0300957 if (!is_output) {
958 v4l2_ctrl_handler_init(hdl, 4);
959 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
960 V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
961 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
962 V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
963 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
964 V4L2_CID_SATURATION, 0, 10000, 1, 5000);
965 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
966 V4L2_CID_HUE, 0, 10000, 1, 5000);
967 if (hdl->error) {
968 err = hdl->error;
969 goto fail_unreg;
970 }
971 err = v4l2_ctrl_handler_setup(hdl);
972 if (err)
973 goto fail_unreg;
Hans Verkuilf8d7ee72013-04-13 08:38:14 -0300974 }
Hans Verkuilffd3c232013-04-13 05:30:48 -0300975
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300976 cx25821_risc_stopper(dev->pci, &chan->dma_vidq.stopper,
977 chan->sram_channels->dma_ctl, 0x11, 0);
Hans Verkuilffd3c232013-04-13 05:30:48 -0300978
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300979 chan->sram_channels = &cx25821_sram_channels[i];
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300980 chan->width = 720;
981 if (dev->tvnorm & V4L2_STD_625_50)
982 chan->height = 576;
983 else
984 chan->height = 480;
Hans Verkuilffd3c232013-04-13 05:30:48 -0300985
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300986 if (chan->pixel_formats == PIXEL_FRMT_411)
987 chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
988 else
989 chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
Hans Verkuilffd3c232013-04-13 05:30:48 -0300990
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300991 cx_write(chan->sram_channels->int_stat, 0xffffffff);
Hans Verkuilffd3c232013-04-13 05:30:48 -0300992
Hans Verkuil2efe2cc2013-04-13 10:00:52 -0300993 INIT_LIST_HEAD(&chan->dma_vidq.active);
994 INIT_LIST_HEAD(&chan->dma_vidq.queued);
995
996 chan->timeout_data.dev = dev;
997 chan->timeout_data.channel = &cx25821_sram_channels[i];
998 chan->dma_vidq.timeout.function = cx25821_vid_timeout;
999 chan->dma_vidq.timeout.data = (unsigned long)&chan->timeout_data;
1000 init_timer(&chan->dma_vidq.timeout);
1001
Hans Verkuil1f198872013-04-14 12:07:13 -03001002 if (!is_output)
1003 videobuf_queue_sg_init(&chan->vidq, &cx25821_video_qops, &dev->pci->dev,
1004 &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
1005 V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
1006 chan, &dev->lock);
Hans Verkuilffd3c232013-04-13 05:30:48 -03001007
1008 /* register v4l devices */
Hans Verkuil1f198872013-04-14 12:07:13 -03001009 *vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
Hans Verkuil467870c2013-04-13 08:18:00 -03001010 vdev->v4l2_dev = &dev->v4l2_dev;
Hans Verkuil1f198872013-04-14 12:07:13 -03001011 if (!is_output)
1012 vdev->ctrl_handler = hdl;
1013 else
1014 vdev->vfl_dir = VFL_DIR_TX;
Hans Verkuilbe178cb2013-04-14 11:53:35 -03001015 vdev->lock = &dev->lock;
Hans Verkuil8d125c52013-04-14 11:57:18 -03001016 set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
Hans Verkuil467870c2013-04-13 08:18:00 -03001017 snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
Hans Verkuil2efe2cc2013-04-13 10:00:52 -03001018 video_set_drvdata(vdev, chan);
Hans Verkuilffd3c232013-04-13 05:30:48 -03001019
Hans Verkuil467870c2013-04-13 08:18:00 -03001020 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1021 video_nr[dev->nr]);
Hans Verkuilffd3c232013-04-13 05:30:48 -03001022
1023 if (err < 0)
1024 goto fail_unreg;
Hans Verkuilffd3c232013-04-13 05:30:48 -03001025 }
1026
1027 /* set PCI interrupt */
1028 cx_set(PCI_INT_MSK, 0xff);
1029
Hans Verkuilffd3c232013-04-13 05:30:48 -03001030 return 0;
1031
1032fail_unreg:
Hans Verkuil467870c2013-04-13 08:18:00 -03001033 while (i >= 0)
1034 cx25821_video_unregister(dev, i--);
Hans Verkuilffd3c232013-04-13 05:30:48 -03001035 return err;
1036}