]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/cx25821/cx25821-video2.c
V4L/DVB: cx25821: prepend cx25821_ to video exported symbols
[net-next-2.6.git] / drivers / staging / cx25821 / cx25821-video2.c
CommitLineData
02b20b0b
MCC
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
bb4c9a74 4 * Copyright (C) 2009 Conexant Systems Inc.
02b20b0b
MCC
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 *
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include "cx25821-video.h"
25
02b20b0b
MCC
26static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
27{
1a9fc855
MCC
28 struct cx25821_buffer *buf =
29 container_of(vb, struct cx25821_buffer, vb);
30 struct cx25821_buffer *prev;
31 struct cx25821_fh *fh = vq->priv_data;
32 struct cx25821_dev *dev = fh->dev;
33 struct cx25821_dmaqueue *q = &dev->vidq[SRAM_CH02];
34
35 /* add jump to stopper */
36 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
37 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
38 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
39
40 dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
41
42 if (!list_empty(&q->queued)) {
43 list_add_tail(&buf->vb.queue, &q->queued);
44 buf->vb.state = VIDEOBUF_QUEUED;
45 dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
46 buf->vb.i);
47
48 } else if (list_empty(&q->active)) {
49 list_add_tail(&buf->vb.queue, &q->active);
50 cx25821_start_video_dma(dev, q, buf,
51 &dev->sram_channels[SRAM_CH02]);
52 buf->vb.state = VIDEOBUF_ACTIVE;
53 buf->count = q->count++;
54 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
55 dprintk(2,
56 "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
57 buf, buf->vb.i, buf->count, q->count);
bb4c9a74 58 } else {
1a9fc855
MCC
59 prev =
60 list_entry(q->active.prev, struct cx25821_buffer, vb.queue);
61 if (prev->vb.width == buf->vb.width
62 && prev->vb.height == buf->vb.height
63 && prev->fmt == buf->fmt) {
64 list_add_tail(&buf->vb.queue, &q->active);
65 buf->vb.state = VIDEOBUF_ACTIVE;
66 buf->count = q->count++;
67 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
68
69 /* 64 bit bits 63-32 */
70 prev->risc.jmp[2] = cpu_to_le32(0);
71 dprintk(2,
72 "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
73 buf, buf->vb.i, buf->count);
74
75 } else {
76 list_add_tail(&buf->vb.queue, &q->queued);
77 buf->vb.state = VIDEOBUF_QUEUED;
78 dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
79 buf->vb.i);
80 }
bb4c9a74 81 }
02b20b0b 82
1a9fc855
MCC
83 if (list_empty(&q->active)) {
84 dprintk(2, "active queue empty!\n");
85 }
02b20b0b
MCC
86}
87
02b20b0b 88static struct videobuf_queue_ops cx25821_video_qops = {
f2466d63
MCC
89 .buf_setup = cx25821_buffer_setup,
90 .buf_prepare = cx25821_buffer_prepare,
1a9fc855 91 .buf_queue = buffer_queue,
f2466d63 92 .buf_release = cx25821_buffer_release,
02b20b0b
MCC
93};
94
02b20b0b
MCC
95static int video_open(struct file *file)
96{
50462eb0 97 struct video_device *vdev = video_devdata(file);
63b0d5ad 98 struct cx25821_dev *dev = video_drvdata(file);
1a9fc855 99 struct cx25821_fh *fh;
63b0d5ad 100 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1a9fc855
MCC
101 u32 pix_format;
102
50462eb0
LP
103 printk("open dev=%s type=%s\n", video_device_node_name(vdev),
104 v4l2_type_names[type]);
02b20b0b 105
1a9fc855
MCC
106 /* allocate + initialize per filehandle data */
107 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
63b0d5ad 108 if (NULL == fh)
1a9fc855 109 return -ENOMEM;
63b0d5ad
LP
110
111 lock_kernel();
112
1a9fc855
MCC
113 file->private_data = fh;
114 fh->dev = dev;
115 fh->type = type;
116 fh->width = 720;
117
118 if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
119 fh->height = 576;
120 else
121 fh->height = 480;
122
123 dev->channel_opened = SRAM_CH02;
124 pix_format =
125 (dev->pixel_formats[dev->channel_opened] ==
126 PIXEL_FRMT_411) ? V4L2_PIX_FMT_Y41P : V4L2_PIX_FMT_YUYV;
127 fh->fmt = format_by_fourcc(pix_format);
128
129 v4l2_prio_open(&dev->prio, &fh->prio);
130
131 videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops,
132 &dev->pci->dev, &dev->slock,
133 V4L2_BUF_TYPE_VIDEO_CAPTURE,
134 V4L2_FIELD_INTERLACED,
135 sizeof(struct cx25821_buffer), fh);
136
137 dprintk(1, "post videobuf_queue_init()\n");
bb4c9a74 138 unlock_kernel();
1a9fc855
MCC
139
140 return 0;
02b20b0b
MCC
141}
142
1a9fc855
MCC
143static ssize_t video_read(struct file *file, char __user * data, size_t count,
144 loff_t * ppos)
02b20b0b 145{
1a9fc855 146 struct cx25821_fh *fh = file->private_data;
02b20b0b 147
1a9fc855 148 switch (fh->type) {
bb4c9a74 149 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
f2466d63 150 if (cx25821_res_locked(fh->dev, RESOURCE_VIDEO2))
1a9fc855 151 return -EBUSY;
02b20b0b 152
1a9fc855
MCC
153 return videobuf_read_one(&fh->vidq, data, count, ppos,
154 file->f_flags & O_NONBLOCK);
02b20b0b 155
bb4c9a74 156 default:
1a9fc855
MCC
157 BUG();
158 return 0;
159 }
02b20b0b
MCC
160}
161
1a9fc855
MCC
162static unsigned int video_poll(struct file *file,
163 struct poll_table_struct *wait)
02b20b0b 164{
1a9fc855
MCC
165 struct cx25821_fh *fh = file->private_data;
166 struct cx25821_buffer *buf;
167
f2466d63 168 if (cx25821_res_check(fh, RESOURCE_VIDEO2)) {
1a9fc855
MCC
169 /* streaming capture */
170 if (list_empty(&fh->vidq.stream))
171 return POLLERR;
172 buf = list_entry(fh->vidq.stream.next,
173 struct cx25821_buffer, vb.stream);
174 } else {
175 /* read() capture */
176 buf = (struct cx25821_buffer *)fh->vidq.read_buf;
177 if (NULL == buf)
178 return POLLERR;
bb4c9a74
MCC
179 }
180
1a9fc855
MCC
181 poll_wait(file, &buf->vb.done, wait);
182 if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
183 if (buf->vb.state == VIDEOBUF_DONE) {
184 struct cx25821_dev *dev = fh->dev;
185
186 if (dev && dev->use_cif_resolution[SRAM_CH02]) {
187 u8 cam_id = *((char *)buf->vb.baddr + 3);
188 memcpy((char *)buf->vb.baddr,
189 (char *)buf->vb.baddr + (fh->width * 2),
190 (fh->width * 2));
191 *((char *)buf->vb.baddr + 3) = cam_id;
192 }
193 }
194
195 return POLLIN | POLLRDNORM;
196 }
bb4c9a74 197
1a9fc855 198 return 0;
02b20b0b
MCC
199}
200
02b20b0b
MCC
201static int video_release(struct file *file)
202{
1a9fc855
MCC
203 struct cx25821_fh *fh = file->private_data;
204 struct cx25821_dev *dev = fh->dev;
02b20b0b 205
1a9fc855
MCC
206 //stop the risc engine and fifo
207 cx_write(channel2->dma_ctl, 0); /* FIFO and RISC disable */
02b20b0b 208
1a9fc855 209 /* stop video capture */
f2466d63 210 if (cx25821_res_check(fh, RESOURCE_VIDEO2)) {
1a9fc855 211 videobuf_queue_cancel(&fh->vidq);
f2466d63 212 cx25821_res_free(dev, fh, RESOURCE_VIDEO2);
1a9fc855 213 }
02b20b0b 214
1a9fc855 215 if (fh->vidq.read_buf) {
f2466d63 216 cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
1a9fc855
MCC
217 kfree(fh->vidq.read_buf);
218 }
02b20b0b 219
1a9fc855 220 videobuf_mmap_free(&fh->vidq);
02b20b0b 221
1a9fc855
MCC
222 v4l2_prio_close(&dev->prio, &fh->prio);
223 file->private_data = NULL;
224 kfree(fh);
02b20b0b 225
1a9fc855 226 return 0;
02b20b0b
MCC
227}
228
02b20b0b
MCC
229static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
230{
1a9fc855
MCC
231 struct cx25821_fh *fh = priv;
232 struct cx25821_dev *dev = fh->dev;
02b20b0b 233
1a9fc855
MCC
234 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)) {
235 return -EINVAL;
236 }
02b20b0b 237
1a9fc855
MCC
238 if (unlikely(i != fh->type)) {
239 return -EINVAL;
240 }
02b20b0b 241
f2466d63 242 if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh, RESOURCE_VIDEO2)))) {
1a9fc855
MCC
243 return -EBUSY;
244 }
02b20b0b 245
1a9fc855 246 return videobuf_streamon(get_queue(fh));
02b20b0b
MCC
247}
248
249static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
250{
1a9fc855
MCC
251 struct cx25821_fh *fh = priv;
252 struct cx25821_dev *dev = fh->dev;
253 int err, res;
254
255 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
256 return -EINVAL;
257 if (i != fh->type)
258 return -EINVAL;
259
f2466d63 260 res = cx25821_get_resource(fh, RESOURCE_VIDEO2);
1a9fc855
MCC
261 err = videobuf_streamoff(get_queue(fh));
262 if (err < 0)
263 return err;
f2466d63 264 cx25821_res_free(dev, fh, res);
1a9fc855 265 return 0;
02b20b0b
MCC
266}
267
1a9fc855
MCC
268static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
269 struct v4l2_format *f)
270{
271 struct cx25821_fh *fh = priv;
272 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
273 int err;
274 int pix_format = 0;
02b20b0b 275
1a9fc855
MCC
276 if (fh) {
277 err = v4l2_prio_check(&dev->prio, &fh->prio);
278 if (0 != err)
279 return err;
280 }
281
282 dprintk(2, "%s()\n", __func__);
f2466d63 283 err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
02b20b0b 284
bb4c9a74 285 if (0 != err)
1a9fc855
MCC
286 return err;
287
288 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
289 fh->vidq.field = f->fmt.pix.field;
290
291 // check if width and height is valid based on set standard
f2466d63 292 if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm)) {
1a9fc855
MCC
293 fh->width = f->fmt.pix.width;
294 }
295
f2466d63 296 if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm)) {
1a9fc855
MCC
297 fh->height = f->fmt.pix.height;
298 }
299
300 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
301 pix_format = PIXEL_FRMT_411;
302 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
303 pix_format = PIXEL_FRMT_422;
304 else
305 return -EINVAL;
306
307 cx25821_set_pixel_format(dev, SRAM_CH02, pix_format);
308
309 // check if cif resolution
310 if (fh->width == 320 || fh->width == 352) {
311 dev->use_cif_resolution[SRAM_CH02] = 1;
312 } else {
313 dev->use_cif_resolution[SRAM_CH02] = 0;
314 }
315 dev->cif_width[SRAM_CH02] = fh->width;
316 medusa_set_resolution(dev, fh->width, SRAM_CH02);
317
318 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__, fh->width,
319 fh->height, fh->vidq.field);
320 cx25821_call_all(dev, video, s_fmt, f);
321
322 return 0;
02b20b0b
MCC
323}
324
325static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
326{
1a9fc855
MCC
327 int ret_val = 0;
328 struct cx25821_fh *fh = priv;
329 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
02b20b0b 330
1a9fc855 331 ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
02b20b0b 332
1a9fc855 333 p->sequence = dev->vidq[SRAM_CH02].count;
02b20b0b 334
1a9fc855 335 return ret_val;
02b20b0b
MCC
336}
337
1a9fc855 338static int vidioc_log_status(struct file *file, void *priv)
02b20b0b 339{
1a9fc855
MCC
340 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
341 char name[32 + 2];
02b20b0b 342
1a9fc855
MCC
343 struct sram_channel *sram_ch = &dev->sram_channels[SRAM_CH02];
344 u32 tmp = 0;
02b20b0b 345
1a9fc855
MCC
346 snprintf(name, sizeof(name), "%s/2", dev->name);
347 printk(KERN_INFO "%s/2: ============ START LOG STATUS ============\n",
02b20b0b 348 dev->name);
bb4c9a74 349
1a9fc855 350 cx25821_call_all(dev, core, log_status);
bb4c9a74 351
1a9fc855
MCC
352 tmp = cx_read(sram_ch->dma_ctl);
353 printk(KERN_INFO "Video input 2 is %s\n",
354 (tmp & 0x11) ? "streaming" : "stopped");
355 printk(KERN_INFO "%s/2: ============= END LOG STATUS =============\n",
02b20b0b 356 dev->name);
1a9fc855 357 return 0;
02b20b0b
MCC
358}
359
360static int vidioc_s_ctrl(struct file *file, void *priv,
1a9fc855 361 struct v4l2_control *ctl)
02b20b0b
MCC
362{
363 struct cx25821_fh *fh = priv;
bb4c9a74
MCC
364 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
365 int err;
02b20b0b 366
bb4c9a74 367 if (fh) {
02b20b0b
MCC
368 err = v4l2_prio_check(&dev->prio, &fh->prio);
369 if (0 != err)
370 return err;
371 }
372
373 return cx25821_set_control(dev, ctl, SRAM_CH02);
374}
1a9fc855 375
02b20b0b
MCC
376// exported stuff
377static const struct v4l2_file_operations video_fops = {
1a9fc855
MCC
378 .owner = THIS_MODULE,
379 .open = video_open,
380 .release = video_release,
381 .read = video_read,
382 .poll = video_poll,
f2466d63 383 .mmap = cx25821_video_mmap,
1a9fc855 384 .ioctl = video_ioctl2,
02b20b0b
MCC
385};
386
387static const struct v4l2_ioctl_ops video_ioctl_ops = {
f2466d63
MCC
388 .vidioc_querycap = cx25821_vidioc_querycap,
389 .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
390 .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
391 .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
1a9fc855 392 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
f2466d63
MCC
393 .vidioc_reqbufs = cx25821_vidioc_reqbufs,
394 .vidioc_querybuf = cx25821_vidioc_querybuf,
395 .vidioc_qbuf = cx25821_vidioc_qbuf,
1a9fc855 396 .vidioc_dqbuf = vidioc_dqbuf,
02b20b0b 397#ifdef TUNER_FLAG
f2466d63
MCC
398 .vidioc_s_std = cx25821_vidioc_s_std,
399 .vidioc_querystd = cx25821_vidioc_querystd,
02b20b0b 400#endif
f2466d63
MCC
401 .vidioc_cropcap = cx25821_vidioc_cropcap,
402 .vidioc_s_crop = cx25821_vidioc_s_crop,
403 .vidioc_g_crop = cx25821_vidioc_g_crop,
404 .vidioc_enum_input = cx25821_vidioc_enum_input,
405 .vidioc_g_input = cx25821_vidioc_g_input,
406 .vidioc_s_input = cx25821_vidioc_s_input,
407 .vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
1a9fc855 408 .vidioc_s_ctrl = vidioc_s_ctrl,
f2466d63 409 .vidioc_queryctrl = cx25821_vidioc_queryctrl,
1a9fc855
MCC
410 .vidioc_streamon = vidioc_streamon,
411 .vidioc_streamoff = vidioc_streamoff,
412 .vidioc_log_status = vidioc_log_status,
f2466d63
MCC
413 .vidioc_g_priority = cx25821_vidioc_g_priority,
414 .vidioc_s_priority = cx25821_vidioc_s_priority,
02b20b0b 415#ifdef CONFIG_VIDEO_V4L1_COMPAT
f2466d63 416 .vidiocgmbuf = cx25821_vidiocgmbuf,
02b20b0b
MCC
417#endif
418#ifdef TUNER_FLAG
f2466d63
MCC
419 .vidioc_g_tuner = cx25821_vidioc_g_tuner,
420 .vidioc_s_tuner = cx25821_vidioc_s_tuner,
421 .vidioc_g_frequency = cx25821_vidioc_g_frequency,
422 .vidioc_s_frequency = cx25821_vidioc_s_frequency,
02b20b0b
MCC
423#endif
424#ifdef CONFIG_VIDEO_ADV_DEBUG
f2466d63
MCC
425 .vidioc_g_register = cx25821_vidioc_g_register,
426 .vidioc_s_register = cx25821_vidioc_s_register,
02b20b0b
MCC
427#endif
428};
429
430struct video_device cx25821_video_template2 = {
1a9fc855
MCC
431 .name = "cx25821-video",
432 .fops = &video_fops,
1a9fc855
MCC
433 .ioctl_ops = &video_ioctl_ops,
434 .tvnorms = CX25821_NORMS,
435 .current_norm = V4L2_STD_NTSC_M,
02b20b0b 436};