2 * Driver for the Conexant CX25821 PCIe bridge
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
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.
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
17 * GNU General Public License for more details.
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.
24 #include "cx25821-video.h"
27 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
29 struct cx25821_buffer *buf = 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_CH00];
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 */
40 dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
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, buf->vb.i);
47 } else if (list_empty(&q->active)) {
48 list_add_tail(&buf->vb.queue, &q->active);
49 cx25821_start_video_dma(dev, q, buf, &dev->sram_channels[SRAM_CH00]);
50 buf->vb.state = VIDEOBUF_ACTIVE;
51 buf->count = q->count++;
52 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
53 dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
54 buf, buf->vb. i, buf->count, q->count);
56 prev = list_entry(q->active.prev, struct cx25821_buffer, vb.queue);
57 if (prev->vb.width == buf->vb.width &&
58 prev->vb.height == buf->vb.height &&
59 prev->fmt == buf->fmt) {
60 list_add_tail(&buf->vb.queue, &q->active);
61 buf->vb.state = VIDEOBUF_ACTIVE;
62 buf->count = q->count++;
63 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
65 /* 64 bit bits 63-32 */
66 prev->risc.jmp[2] = cpu_to_le32(0);
67 dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n", buf, buf->vb.i, buf->count);
70 list_add_tail(&buf->vb.queue, &q->queued);
71 buf->vb.state = VIDEOBUF_QUEUED;
72 dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf, buf->vb.i);
76 if (list_empty(&q->active))
78 dprintk(2, "active queue empty!\n");
83 static struct videobuf_queue_ops cx25821_video_qops = {
84 .buf_setup = buffer_setup,
85 .buf_prepare = buffer_prepare,
86 .buf_queue = buffer_queue,
87 .buf_release = buffer_release,
91 static int video_open(struct file *file)
93 int minor = video_devdata(file)->minor;
94 struct cx25821_dev *h, *dev = NULL;
95 struct cx25821_fh *fh;
96 struct list_head *list;
97 enum v4l2_buf_type type = 0;
101 list_for_each(list, &cx25821_devlist)
103 h = list_entry(list, struct cx25821_dev, devlist);
105 if (h->video_dev[SRAM_CH00] && h->video_dev[SRAM_CH00]->minor == minor)
108 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
117 printk("open minor=%d type=%s\n", minor, v4l2_type_names[type]);
119 /* allocate + initialize per filehandle data */
120 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
126 file->private_data = fh;
131 if(dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
136 dev->channel_opened = SRAM_CH00;
137 pix_format = (dev->pixel_formats[dev->channel_opened] == PIXEL_FRMT_411) ? V4L2_PIX_FMT_Y41P : V4L2_PIX_FMT_YUYV;
138 fh->fmt = format_by_fourcc(pix_format);
140 v4l2_prio_open(&dev->prio,&fh->prio);
142 videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops,
143 &dev->pci->dev, &dev->slock,
144 V4L2_BUF_TYPE_VIDEO_CAPTURE,
145 V4L2_FIELD_INTERLACED,
146 sizeof(struct cx25821_buffer),
149 dprintk(1, "post videobuf_queue_init()\n");
155 static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
157 struct cx25821_fh *fh = file->private_data;
161 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
162 if (res_locked(fh->dev, RESOURCE_VIDEO0))
165 return videobuf_read_one(&fh->vidq, data, count, ppos, file->f_flags & O_NONBLOCK);
173 static unsigned int video_poll(struct file *file, struct poll_table_struct *wait)
175 struct cx25821_fh *fh = file->private_data;
176 struct cx25821_buffer *buf;
178 if (res_check(fh, RESOURCE_VIDEO0)) {
179 /* streaming capture */
180 if (list_empty(&fh->vidq.stream))
182 buf = list_entry(fh->vidq.stream.next,
183 struct cx25821_buffer, vb.stream);
186 buf = (struct cx25821_buffer *)fh->vidq.read_buf;
191 poll_wait(file, &buf->vb.done, wait);
192 if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR)
194 if( buf->vb.state == VIDEOBUF_DONE )
196 struct cx25821_dev *dev = fh->dev;
198 if( dev && dev->use_cif_resolution[SRAM_CH00] )
200 u8 cam_id = *((char*)buf->vb.baddr+3);
201 memcpy((char*)buf->vb.baddr, (char*)buf->vb.baddr + (fh->width * 2), (fh->width * 2));
202 *((char*)buf->vb.baddr+3) = cam_id;
206 return POLLIN|POLLRDNORM;
213 static int video_release(struct file *file)
215 struct cx25821_fh *fh = file->private_data;
216 struct cx25821_dev *dev = fh->dev;
218 //stop the risc engine and fifo
219 cx_write(channel0->dma_ctl, 0); /* FIFO and RISC disable */
221 /* stop video capture */
222 if (res_check(fh, RESOURCE_VIDEO0)) {
223 videobuf_queue_cancel(&fh->vidq);
224 res_free(dev, fh, RESOURCE_VIDEO0);
227 if (fh->vidq.read_buf) {
228 buffer_release(&fh->vidq, fh->vidq.read_buf);
229 kfree(fh->vidq.read_buf);
232 videobuf_mmap_free(&fh->vidq);
234 v4l2_prio_close(&dev->prio,&fh->prio);
235 file->private_data = NULL;
242 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
244 struct cx25821_fh *fh = priv;
245 struct cx25821_dev *dev = fh->dev;
247 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
252 if (unlikely(i != fh->type))
257 if (unlikely(!res_get(dev, fh, get_resource(fh, RESOURCE_VIDEO0))))
262 return videobuf_streamon(get_queue(fh));
265 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
267 struct cx25821_fh *fh = priv;
268 struct cx25821_dev *dev = fh->dev;
271 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
276 res = get_resource(fh, RESOURCE_VIDEO0);
277 err = videobuf_streamoff(get_queue(fh));
280 res_free(dev, fh, res);
285 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f)
287 struct cx25821_fh *fh = priv;
288 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
290 int pix_format = PIXEL_FRMT_422;
294 err = v4l2_prio_check(&dev->prio, &fh->prio);
299 dprintk(2, "%s()\n", __func__);
300 err = vidioc_try_fmt_vid_cap(file, priv, f);
305 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
306 fh->vidq.field = f->fmt.pix.field;
308 // check if width and height is valid based on set standard
309 if (is_valid_width(f->fmt.pix.width, dev->tvnorm))
311 fh->width = f->fmt.pix.width;
314 if (is_valid_height(f->fmt.pix.height, dev->tvnorm))
316 fh->height = f->fmt.pix.height;
319 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
320 pix_format = PIXEL_FRMT_411;
321 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
322 pix_format = PIXEL_FRMT_422;
326 cx25821_set_pixel_format( dev, SRAM_CH00, pix_format );
328 // check if cif resolution
329 if (fh->width == 320 || fh->width == 352)
331 dev->use_cif_resolution[SRAM_CH00] = 1;
334 dev->use_cif_resolution[SRAM_CH00] = 0;
336 dev->cif_width[SRAM_CH00] = fh->width;
337 medusa_set_resolution( dev, fh->width, SRAM_CH00 );
339 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__, fh->width, fh->height, fh->vidq.field);
340 cx25821_call_all(dev, video, s_fmt, f);
345 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
348 struct cx25821_fh *fh = priv;
349 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
351 ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
353 p->sequence = dev->vidq[SRAM_CH00].count;
358 static int vidioc_log_status (struct file *file, void *priv)
360 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
363 struct sram_channel *sram_ch = &dev->sram_channels[SRAM_CH00];
366 snprintf(name, sizeof(name), "%s/2", dev->name);
367 printk(KERN_INFO "%s/2: ============ START LOG STATUS ============\n",
369 cx25821_call_all(dev, core, log_status);
370 tmp = cx_read(sram_ch->dma_ctl);
371 printk(KERN_INFO "Video input 0 is %s\n", (tmp & 0x11)?"streaming" : "stopped");
372 printk(KERN_INFO "%s/2: ============= END LOG STATUS =============\n",
377 static int vidioc_s_ctrl(struct file *file, void *priv,
378 struct v4l2_control *ctl)
380 struct cx25821_fh *fh = priv;
381 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
385 err = v4l2_prio_check(&dev->prio, &fh->prio);
390 return cx25821_set_control(dev, ctl, SRAM_CH00);
394 static const struct v4l2_file_operations video_fops = {
395 .owner = THIS_MODULE,
397 .release = video_release,
401 .ioctl = video_ioctl2,
404 static const struct v4l2_ioctl_ops video_ioctl_ops = {
405 .vidioc_querycap = vidioc_querycap,
406 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
407 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
408 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
409 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
410 .vidioc_reqbufs = vidioc_reqbufs,
411 .vidioc_querybuf = vidioc_querybuf,
412 .vidioc_qbuf = vidioc_qbuf,
413 .vidioc_dqbuf = vidioc_dqbuf,
415 .vidioc_s_std = vidioc_s_std,
416 .vidioc_querystd = vidioc_querystd,
418 .vidioc_cropcap = vidioc_cropcap,
419 .vidioc_s_crop = vidioc_s_crop,
420 .vidioc_g_crop = vidioc_g_crop,
421 .vidioc_enum_input = vidioc_enum_input,
422 .vidioc_g_input = vidioc_g_input,
423 .vidioc_s_input = vidioc_s_input,
424 .vidioc_g_ctrl = vidioc_g_ctrl,
425 .vidioc_s_ctrl = vidioc_s_ctrl,
426 .vidioc_queryctrl = vidioc_queryctrl,
427 .vidioc_streamon = vidioc_streamon,
428 .vidioc_streamoff = vidioc_streamoff,
429 .vidioc_log_status = vidioc_log_status,
430 .vidioc_g_priority = vidioc_g_priority,
431 .vidioc_s_priority = vidioc_s_priority,
432 #ifdef CONFIG_VIDEO_V4L1_COMPAT
433 .vidiocgmbuf = vidiocgmbuf,
436 .vidioc_g_tuner = vidioc_g_tuner,
437 .vidioc_s_tuner = vidioc_s_tuner,
438 .vidioc_g_frequency = vidioc_g_frequency,
439 .vidioc_s_frequency = vidioc_s_frequency,
441 #ifdef CONFIG_VIDEO_ADV_DEBUG
442 .vidioc_g_register = vidioc_g_register,
443 .vidioc_s_register = vidioc_s_register,
447 struct video_device cx25821_video_template0 = {
448 .name = "cx25821-video",
451 .ioctl_ops = &video_ioctl_ops,
452 .tvnorms = CX25821_NORMS,
453 .current_norm = V4L2_STD_NTSC_M,