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
7 * Parts adapted/taken from Eduardo Moscoso Rubino
8 * Copyright (C) 2009 Eduardo Moscoso Rubino <moscoso@TopoLogica.com>
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.
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
20 * GNU General Public License for more details.
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.
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 #include "cx25821-video.h"
31 MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
32 MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
33 MODULE_LICENSE("GPL");
35 static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
37 module_param_array(video_nr, int, NULL, 0444);
39 MODULE_PARM_DESC(video_nr, "video device numbers");
41 static unsigned int video_debug = VIDEO_DEBUG;
42 module_param(video_debug, int, 0644);
43 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
45 static unsigned int irq_debug;
46 module_param(irq_debug, int, 0644);
47 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
49 static unsigned int vid_limit = 16;
50 module_param(vid_limit, int, 0644);
51 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
53 #define FORMAT_FLAGS_PACKED 0x01
55 static const struct cx25821_fmt formats[] = {
57 .name = "4:1:1, packed, Y41P",
58 .fourcc = V4L2_PIX_FMT_Y41P,
60 .flags = FORMAT_FLAGS_PACKED,
62 .name = "4:2:2, packed, YUYV",
63 .fourcc = V4L2_PIX_FMT_YUYV,
65 .flags = FORMAT_FLAGS_PACKED,
69 static const struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
73 for (i = 0; i < ARRAY_SIZE(formats); i++)
74 if (formats[i].fourcc == fourcc)
79 void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
82 struct cx25821_buffer *buf;
86 if (list_empty(&q->active)) {
87 dprintk(1, "bc=%d (=0: active empty)\n", bc);
91 buf = list_entry(q->active.next, struct cx25821_buffer,
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... */
97 if ((s16) (count - buf->count) < 0)
100 v4l2_get_timestamp(&buf->vb.ts);
101 buf->vb.state = VIDEOBUF_DONE;
102 list_del(&buf->vb.queue);
103 wake_up(&buf->vb.done);
106 if (list_empty(&q->active))
107 del_timer(&q->timeout);
109 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
111 pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
114 int cx25821_start_video_dma(struct cx25821_dev *dev,
115 struct cx25821_dmaqueue *q,
116 struct cx25821_buffer *buf,
117 const struct sram_channel *channel)
121 /* setup fifo + format */
122 cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
125 cx_write(channel->gpcnt_ctl, 3);
129 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
130 cx_set(channel->int_msk, 0x11);
133 cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
135 /* 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);
142 static int cx25821_restart_video_queue(struct cx25821_dev *dev,
143 struct cx25821_dmaqueue *q,
144 const struct sram_channel *channel)
146 struct cx25821_buffer *buf, *prev;
147 struct list_head *item;
149 if (!list_empty(&q->active)) {
150 buf = list_entry(q->active.next, struct cx25821_buffer,
153 cx25821_start_video_dma(dev, q, buf, channel);
155 list_for_each(item, &q->active) {
156 buf = list_entry(item, struct cx25821_buffer, vb.queue);
157 buf->count = q->count++;
160 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
166 if (list_empty(&q->queued))
169 buf = list_entry(q->queued.next, struct cx25821_buffer,
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);
185 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
193 static void cx25821_vid_timeout(unsigned long data)
195 struct cx25821_data *timeout_data = (struct cx25821_data *)data;
196 struct cx25821_dev *dev = timeout_data->dev;
197 const struct sram_channel *channel = timeout_data->channel;
198 struct cx25821_dmaqueue *q = &dev->channels[channel->i].dma_vidq;
199 struct cx25821_buffer *buf;
202 /* cx25821_sram_channel_dump(dev, channel); */
203 cx_clear(channel->dma_ctl, 0x11);
205 spin_lock_irqsave(&dev->slock, flags);
206 while (!list_empty(&q->active)) {
207 buf = list_entry(q->active.next, struct cx25821_buffer,
209 list_del(&buf->vb.queue);
211 buf->vb.state = VIDEOBUF_ERROR;
212 wake_up(&buf->vb.done);
215 cx25821_restart_video_queue(dev, q, channel);
216 spin_unlock_irqrestore(&dev->slock, flags);
219 int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
224 const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
226 mask = cx_read(channel->int_msk);
227 if (0 == (status & mask))
230 cx_write(channel->int_stat, status);
232 /* risc op code error */
233 if (status & (1 << 16)) {
234 pr_warn("%s, %s: video risc op code error\n",
235 dev->name, channel->name);
236 cx_clear(channel->dma_ctl, 0x11);
237 cx25821_sram_channel_dump(dev, channel);
241 if (status & FLD_VID_DST_RISC1) {
242 spin_lock(&dev->slock);
243 count = cx_read(channel->gpcnt);
244 cx25821_video_wakeup(dev, &dev->channels[channel->i].dma_vidq,
246 spin_unlock(&dev->slock);
252 dprintk(2, "stopper video\n");
253 spin_lock(&dev->slock);
254 cx25821_restart_video_queue(dev,
255 &dev->channels[channel->i].dma_vidq, channel);
256 spin_unlock(&dev->slock);
262 static int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
265 struct cx25821_channel *chan = q->priv_data;
267 *size = chan->fmt->depth * chan->width * chan->height >> 3;
272 if (*size * *count > vid_limit * 1024 * 1024)
273 *count = (vid_limit * 1024 * 1024) / *size;
278 static int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
279 enum v4l2_field field)
281 struct cx25821_channel *chan = q->priv_data;
282 struct cx25821_dev *dev = chan->dev;
283 struct cx25821_buffer *buf =
284 container_of(vb, struct cx25821_buffer, vb);
285 int rc, init_buffer = 0;
287 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
288 int bpl_local = LINE_SIZE_D1;
290 BUG_ON(NULL == chan->fmt);
291 if (chan->width < 48 || chan->width > 720 ||
292 chan->height < 32 || chan->height > 576)
295 buf->vb.size = (chan->width * chan->height * chan->fmt->depth) >> 3;
297 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
300 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;
306 buf->vb.field = field;
310 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
312 rc = videobuf_iolock(q, &buf->vb, NULL);
314 printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
319 dprintk(1, "init_buffer=%d\n", init_buffer);
322 if (chan->pixel_formats == PIXEL_FRMT_411)
323 buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
325 buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
327 if (chan->pixel_formats == PIXEL_FRMT_411) {
328 bpl_local = buf->bpl;
330 bpl_local = buf->bpl; /* Default */
332 if (chan->use_cif_resolution) {
333 if (dev->tvnorm & V4L2_STD_625_50)
334 bpl_local = 352 << 1;
336 bpl_local = chan->cif_width << 1;
340 switch (buf->vb.field) {
342 cx25821_risc_buffer(dev->pci, &buf->risc,
343 dma->sglist, 0, UNSET,
344 buf->bpl, 0, buf->vb.height);
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);
351 case V4L2_FIELD_INTERLACED:
352 /* All other formats are top field first */
354 dprintk(1, "top field first\n");
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);
361 case V4L2_FIELD_SEQ_TB:
362 cx25821_risc_buffer(dev->pci, &buf->risc,
364 0, buf->bpl * (buf->vb.height >> 1),
365 buf->bpl, 0, buf->vb.height >> 1);
367 case V4L2_FIELD_SEQ_BT:
368 cx25821_risc_buffer(dev->pci, &buf->risc,
370 buf->bpl * (buf->vb.height >> 1), 0,
371 buf->bpl, 0, buf->vb.height >> 1);
378 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
379 buf, buf->vb.i, chan->width, chan->height, chan->fmt->depth,
380 chan->fmt->name, (unsigned long)buf->risc.dma);
382 buf->vb.state = VIDEOBUF_PREPARED;
387 cx25821_free_buffer(q, buf);
391 static void cx25821_buffer_release(struct videobuf_queue *q,
392 struct videobuf_buffer *vb)
394 struct cx25821_buffer *buf =
395 container_of(vb, struct cx25821_buffer, vb);
397 cx25821_free_buffer(q, buf);
400 static int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
402 struct cx25821_channel *chan = video_drvdata(file);
404 return videobuf_mmap_mapper(&chan->vidq, vma);
408 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
410 struct cx25821_buffer *buf =
411 container_of(vb, struct cx25821_buffer, vb);
412 struct cx25821_buffer *prev;
413 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;
417 /* 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 */
422 dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
424 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,
430 } else if (list_empty(&q->active)) {
431 list_add_tail(&buf->vb.queue, &q->active);
432 cx25821_start_video_dma(dev, q, buf, chan->sram_channels);
433 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);
439 prev = list_entry(q->active.prev, struct cx25821_buffer,
441 if (prev->vb.width == buf->vb.width
442 && prev->vb.height == buf->vb.height
443 && prev->fmt == buf->fmt) {
444 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);
449 /* 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);
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,
462 if (list_empty(&q->active))
463 dprintk(2, "active queue empty!\n");
466 static struct videobuf_queue_ops cx25821_video_qops = {
467 .buf_setup = cx25821_buffer_setup,
468 .buf_prepare = cx25821_buffer_prepare,
469 .buf_queue = buffer_queue,
470 .buf_release = cx25821_buffer_release,
473 static ssize_t video_read(struct file *file, char __user * data, size_t count,
476 struct v4l2_fh *fh = file->private_data;
477 struct cx25821_channel *chan = video_drvdata(file);
478 struct cx25821_dev *dev = chan->dev;
481 if (mutex_lock_interruptible(&dev->lock))
483 if (chan->streaming_fh && chan->streaming_fh != fh) {
487 chan->streaming_fh = fh;
489 err = videobuf_read_one(&chan->vidq, data, count, ppos,
490 file->f_flags & O_NONBLOCK);
492 mutex_unlock(&dev->lock);
496 static unsigned int video_poll(struct file *file,
497 struct poll_table_struct *wait)
499 struct cx25821_channel *chan = video_drvdata(file);
500 unsigned long req_events = poll_requested_events(wait);
501 unsigned int res = v4l2_ctrl_poll(file, wait);
503 if (req_events & (POLLIN | POLLRDNORM))
504 res |= videobuf_poll_stream(file, &chan->vidq, wait);
507 /* This doesn't belong in poll(). This can be done
508 * much better with vb2. We keep this code here as a
510 if ((res & POLLIN) && buf->vb.state == VIDEOBUF_DONE) {
511 struct cx25821_dev *dev = chan->dev;
513 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),
518 *((char *)buf->vb.baddr + 3) = cam_id;
524 static int video_release(struct file *file)
526 struct cx25821_channel *chan = video_drvdata(file);
527 struct v4l2_fh *fh = file->private_data;
528 struct cx25821_dev *dev = chan->dev;
529 const struct sram_channel *sram_ch =
530 dev->channels[0].sram_channels;
532 mutex_lock(&dev->lock);
533 /* stop the risc engine and fifo */
534 cx_write(sram_ch->dma_ctl, 0); /* FIFO and RISC disable */
536 /* stop video capture */
537 if (chan->streaming_fh == fh) {
538 videobuf_queue_cancel(&chan->vidq);
539 chan->streaming_fh = NULL;
542 if (chan->vidq.read_buf) {
543 cx25821_buffer_release(&chan->vidq, chan->vidq.read_buf);
544 kfree(chan->vidq.read_buf);
547 videobuf_mmap_free(&chan->vidq);
548 mutex_unlock(&dev->lock);
550 return v4l2_fh_release(file);
554 static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
555 struct v4l2_format *f)
557 struct cx25821_channel *chan = video_drvdata(file);
559 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;
563 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;
571 static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
572 struct v4l2_format *f)
574 struct cx25821_channel *chan = video_drvdata(file);
575 struct cx25821_dev *dev = chan->dev;
576 const struct cx25821_fmt *fmt;
577 enum v4l2_field field = f->fmt.pix.field;
578 unsigned int maxw, maxh;
581 fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
585 maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
587 w = f->fmt.pix.width;
588 if (field != V4L2_FIELD_BOTTOM)
589 field = V4L2_FIELD_TOP;
592 f->fmt.pix.height = maxh / 4;
593 } else if (w < 720) {
595 f->fmt.pix.height = maxh / 2;
598 f->fmt.pix.height = maxh;
599 field = V4L2_FIELD_INTERLACED;
601 f->fmt.pix.field = field;
602 f->fmt.pix.width = w;
603 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
604 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
605 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
610 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
612 struct cx25821_channel *chan = video_drvdata(file);
614 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
617 if (chan->streaming_fh && chan->streaming_fh != priv)
619 chan->streaming_fh = priv;
621 return videobuf_streamon(&chan->vidq);
624 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
626 struct cx25821_channel *chan = video_drvdata(file);
628 if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
631 if (chan->streaming_fh && chan->streaming_fh != priv)
633 if (chan->streaming_fh == NULL)
636 chan->streaming_fh = NULL;
637 return videobuf_streamoff(&chan->vidq);
640 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
641 struct v4l2_format *f)
643 struct cx25821_channel *chan = video_drvdata(file);
644 struct cx25821_dev *dev = chan->dev;
645 int pix_format = PIXEL_FRMT_422;
648 err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
653 chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
654 chan->vidq.field = f->fmt.pix.field;
655 chan->width = f->fmt.pix.width;
656 chan->height = f->fmt.pix.height;
658 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
659 pix_format = PIXEL_FRMT_411;
661 pix_format = PIXEL_FRMT_422;
663 cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
665 /* check if cif resolution */
666 if (chan->width == 320 || chan->width == 352)
667 chan->use_cif_resolution = 1;
669 chan->use_cif_resolution = 0;
671 chan->cif_width = chan->width;
672 medusa_set_resolution(dev, chan->width, SRAM_CH00);
676 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
679 struct cx25821_channel *chan = video_drvdata(file);
681 ret_val = videobuf_dqbuf(&chan->vidq, p, file->f_flags & O_NONBLOCK);
682 p->sequence = chan->dma_vidq.count;
687 static int vidioc_log_status(struct file *file, void *priv)
689 struct cx25821_channel *chan = video_drvdata(file);
690 struct cx25821_dev *dev = chan->dev;
691 const struct sram_channel *sram_ch = chan->sram_channels;
694 tmp = cx_read(sram_ch->dma_ctl);
695 pr_info("Video input 0 is %s\n",
696 (tmp & 0x11) ? "streaming" : "stopped");
701 static int cx25821_vidioc_querycap(struct file *file, void *priv,
702 struct v4l2_capability *cap)
704 struct cx25821_channel *chan = video_drvdata(file);
705 struct cx25821_dev *dev = chan->dev;
706 const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
707 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
708 const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
710 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));
713 if (chan->id >= VID_CHANNEL_NUM)
714 cap->device_caps = cap_output;
716 cap->device_caps = cap_input;
717 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
721 static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
722 struct v4l2_fmtdesc *f)
724 if (unlikely(f->index >= ARRAY_SIZE(formats)))
727 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
728 f->pixelformat = formats[f->index].fourcc;
733 static int cx25821_vidioc_reqbufs(struct file *file, void *priv,
734 struct v4l2_requestbuffers *p)
736 struct cx25821_channel *chan = video_drvdata(file);
738 return videobuf_reqbufs(&chan->vidq, p);
741 static int cx25821_vidioc_querybuf(struct file *file, void *priv,
742 struct v4l2_buffer *p)
744 struct cx25821_channel *chan = video_drvdata(file);
746 return videobuf_querybuf(&chan->vidq, p);
749 static int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
751 struct cx25821_channel *chan = video_drvdata(file);
753 return videobuf_qbuf(&chan->vidq, p);
756 static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
758 struct cx25821_channel *chan = video_drvdata(file);
760 *tvnorms = chan->dev->tvnorm;
764 int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
766 struct cx25821_channel *chan = video_drvdata(file);
767 struct cx25821_dev *dev = chan->dev;
769 if (dev->tvnorm == tvnorms)
772 dev->tvnorm = tvnorms;
774 chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
776 medusa_set_videostandard(dev);
781 static int cx25821_vidioc_enum_input(struct file *file, void *priv,
782 struct v4l2_input *i)
787 i->type = V4L2_INPUT_TYPE_CAMERA;
788 i->std = CX25821_NORMS;
789 strcpy(i->name, "Composite");
793 static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
799 static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
801 return i ? -EINVAL : 0;
804 static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
806 struct cx25821_channel *chan =
807 container_of(ctrl->handler, struct cx25821_channel, hdl);
808 struct cx25821_dev *dev = chan->dev;
811 case V4L2_CID_BRIGHTNESS:
812 medusa_set_brightness(dev, ctrl->val, chan->id);
815 medusa_set_hue(dev, ctrl->val, chan->id);
817 case V4L2_CID_CONTRAST:
818 medusa_set_contrast(dev, ctrl->val, chan->id);
820 case V4L2_CID_SATURATION:
821 medusa_set_saturation(dev, ctrl->val, chan->id);
829 static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
832 struct cx25821_channel *chan = video_drvdata(file);
833 struct cx25821_dev *dev = chan->dev;
835 struct upstream_user_struct *data_from_user;
837 data_from_user = (struct upstream_user_struct *)arg;
839 if (!data_from_user) {
840 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
844 command = data_from_user->command;
846 if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
849 dev->input_filename = data_from_user->input_filename;
850 dev->input_audiofilename = data_from_user->input_filename;
851 dev->vid_stdname = data_from_user->vid_stdname;
852 dev->pixel_format = data_from_user->pixel_format;
853 dev->channel_select = data_from_user->channel_select;
854 dev->command = data_from_user->command;
857 case UPSTREAM_START_VIDEO:
858 cx25821_start_upstream_video_ch1(dev, data_from_user);
861 case UPSTREAM_STOP_VIDEO:
862 cx25821_stop_upstream_video_ch1(dev);
869 static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
872 struct cx25821_channel *chan = video_drvdata(file);
873 struct cx25821_dev *dev = chan->dev;
875 struct upstream_user_struct *data_from_user;
877 data_from_user = (struct upstream_user_struct *)arg;
879 if (!data_from_user) {
880 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
884 command = data_from_user->command;
886 if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
889 dev->input_filename_ch2 = data_from_user->input_filename;
890 dev->input_audiofilename = data_from_user->input_filename;
891 dev->vid_stdname_ch2 = data_from_user->vid_stdname;
892 dev->pixel_format_ch2 = data_from_user->pixel_format;
893 dev->channel_select_ch2 = data_from_user->channel_select;
894 dev->command_ch2 = data_from_user->command;
897 case UPSTREAM_START_VIDEO:
898 cx25821_start_upstream_video_ch2(dev, data_from_user);
901 case UPSTREAM_STOP_VIDEO:
902 cx25821_stop_upstream_video_ch2(dev);
909 static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
912 struct cx25821_channel *chan = video_drvdata(file);
913 struct cx25821_dev *dev = chan->dev;
915 struct upstream_user_struct *data_from_user;
917 data_from_user = (struct upstream_user_struct *)arg;
919 if (!data_from_user) {
920 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
924 command = data_from_user->command;
926 if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
929 dev->input_filename = data_from_user->input_filename;
930 dev->input_audiofilename = data_from_user->input_filename;
931 dev->vid_stdname = data_from_user->vid_stdname;
932 dev->pixel_format = data_from_user->pixel_format;
933 dev->channel_select = data_from_user->channel_select;
934 dev->command = data_from_user->command;
937 case UPSTREAM_START_AUDIO:
938 cx25821_start_upstream_audio(dev, data_from_user);
941 case UPSTREAM_STOP_AUDIO:
942 cx25821_stop_upstream_audio(dev);
949 static long cx25821_video_ioctl(struct file *file,
950 unsigned int cmd, unsigned long arg)
952 struct cx25821_channel *chan = video_drvdata(file);
954 /* check to see if it's the video upstream */
955 if (chan->id == SRAM_CH09)
956 return video_ioctl_upstream9(file, cmd, arg);
957 if (chan->id == SRAM_CH10)
958 return video_ioctl_upstream10(file, cmd, arg);
959 if (chan->id == SRAM_CH11)
960 return video_ioctl_upstream11(file, cmd, arg);
962 return video_ioctl2(file, cmd, arg);
965 static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
966 .s_ctrl = cx25821_s_ctrl,
969 static const struct v4l2_file_operations video_fops = {
970 .owner = THIS_MODULE,
971 .open = v4l2_fh_open,
972 .release = video_release,
975 .mmap = cx25821_video_mmap,
976 .unlocked_ioctl = cx25821_video_ioctl,
979 static const struct v4l2_ioctl_ops video_ioctl_ops = {
980 .vidioc_querycap = cx25821_vidioc_querycap,
981 .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
982 .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
983 .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
984 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
985 .vidioc_reqbufs = cx25821_vidioc_reqbufs,
986 .vidioc_querybuf = cx25821_vidioc_querybuf,
987 .vidioc_qbuf = cx25821_vidioc_qbuf,
988 .vidioc_dqbuf = vidioc_dqbuf,
989 .vidioc_g_std = cx25821_vidioc_g_std,
990 .vidioc_s_std = cx25821_vidioc_s_std,
991 .vidioc_enum_input = cx25821_vidioc_enum_input,
992 .vidioc_g_input = cx25821_vidioc_g_input,
993 .vidioc_s_input = cx25821_vidioc_s_input,
994 .vidioc_streamon = vidioc_streamon,
995 .vidioc_streamoff = vidioc_streamoff,
996 .vidioc_log_status = vidioc_log_status,
997 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
998 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1001 static const struct video_device cx25821_video_device = {
1002 .name = "cx25821-video",
1003 .fops = &video_fops,
1004 .release = video_device_release_empty,
1006 .ioctl_ops = &video_ioctl_ops,
1007 .tvnorms = CX25821_NORMS,
1010 void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
1012 cx_clear(PCI_INT_MSK, 1);
1014 if (video_is_registered(&dev->channels[chan_num].vdev)) {
1015 video_unregister_device(&dev->channels[chan_num].vdev);
1016 v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
1018 btcx_riscmem_free(dev->pci,
1019 &dev->channels[chan_num].dma_vidq.stopper);
1023 int cx25821_video_register(struct cx25821_dev *dev)
1028 /* initial device configuration */
1029 dev->tvnorm = V4L2_STD_NTSC_M;
1031 spin_lock_init(&dev->slock);
1033 for (i = 0; i < VID_CHANNEL_NUM; ++i) {
1034 struct cx25821_channel *chan = &dev->channels[i];
1035 struct video_device *vdev = &chan->vdev;
1036 struct v4l2_ctrl_handler *hdl = &chan->hdl;
1038 if (i == SRAM_CH08) /* audio channel */
1041 v4l2_ctrl_handler_init(hdl, 4);
1042 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1043 V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
1044 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1045 V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
1046 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1047 V4L2_CID_SATURATION, 0, 10000, 1, 5000);
1048 v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
1049 V4L2_CID_HUE, 0, 10000, 1, 5000);
1054 err = v4l2_ctrl_handler_setup(hdl);
1058 cx25821_risc_stopper(dev->pci, &chan->dma_vidq.stopper,
1059 chan->sram_channels->dma_ctl, 0x11, 0);
1061 chan->sram_channels = &cx25821_sram_channels[i];
1063 if (dev->tvnorm & V4L2_STD_625_50)
1068 if (chan->pixel_formats == PIXEL_FRMT_411)
1069 chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
1071 chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
1073 cx_write(chan->sram_channels->int_stat, 0xffffffff);
1075 INIT_LIST_HEAD(&chan->dma_vidq.active);
1076 INIT_LIST_HEAD(&chan->dma_vidq.queued);
1078 chan->timeout_data.dev = dev;
1079 chan->timeout_data.channel = &cx25821_sram_channels[i];
1080 chan->dma_vidq.timeout.function = cx25821_vid_timeout;
1081 chan->dma_vidq.timeout.data = (unsigned long)&chan->timeout_data;
1082 init_timer(&chan->dma_vidq.timeout);
1084 videobuf_queue_sg_init(&chan->vidq, &cx25821_video_qops, &dev->pci->dev,
1085 &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
1086 V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
1089 /* register v4l devices */
1090 *vdev = cx25821_video_device;
1091 vdev->v4l2_dev = &dev->v4l2_dev;
1092 vdev->ctrl_handler = hdl;
1093 vdev->lock = &dev->lock;
1094 set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
1095 snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
1096 video_set_drvdata(vdev, chan);
1098 err = video_register_device(vdev, VFL_TYPE_GRABBER,
1105 /* set PCI interrupt */
1106 cx_set(PCI_INT_MSK, 0xff);
1112 cx25821_video_unregister(dev, i--);