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 = "8 bpp, gray",
58 .fourcc = V4L2_PIX_FMT_GREY,
60 .flags = FORMAT_FLAGS_PACKED,
62 .name = "4:1:1, packed, Y41P",
63 .fourcc = V4L2_PIX_FMT_Y41P,
65 .flags = FORMAT_FLAGS_PACKED,
67 .name = "4:2:2, packed, YUYV",
68 .fourcc = V4L2_PIX_FMT_YUYV,
70 .flags = FORMAT_FLAGS_PACKED,
72 .name = "4:2:2, packed, UYVY",
73 .fourcc = V4L2_PIX_FMT_UYVY,
75 .flags = FORMAT_FLAGS_PACKED,
78 .fourcc = V4L2_PIX_FMT_YUV420,
80 .flags = FORMAT_FLAGS_PACKED,
84 static const struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
88 if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P)
91 for (i = 0; i < ARRAY_SIZE(formats); i++)
92 if (formats[i].fourcc == fourcc)
95 pr_err("%s(0x%08x) NOT FOUND\n", __func__, fourcc);
99 void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
102 struct cx25821_buffer *buf;
105 for (bc = 0;; bc++) {
106 if (list_empty(&q->active)) {
107 dprintk(1, "bc=%d (=0: active empty)\n", bc);
111 buf = list_entry(q->active.next, struct cx25821_buffer,
114 /* count comes from the hw and it is 16bit wide --
115 * this trick handles wrap-arounds correctly for
116 * up to 32767 buffers in flight... */
117 if ((s16) (count - buf->count) < 0)
120 v4l2_get_timestamp(&buf->vb.ts);
121 buf->vb.state = VIDEOBUF_DONE;
122 list_del(&buf->vb.queue);
123 wake_up(&buf->vb.done);
126 if (list_empty(&q->active))
127 del_timer(&q->timeout);
129 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
131 pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
134 static int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
136 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
137 __func__, (unsigned int)norm, v4l2_norm_to_name(norm));
141 /* Tell the internal A/V decoder */
142 cx25821_call_all(dev, core, s_std, norm);
147 static struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
149 const struct video_device *template,
152 struct video_device *vfd;
153 dprintk(1, "%s()\n", __func__);
155 vfd = video_device_alloc();
159 vfd->v4l2_dev = &dev->v4l2_dev;
160 vfd->release = video_device_release;
161 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, type,
162 cx25821_boards[dev->board].name);
163 video_set_drvdata(vfd, dev);
168 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
172 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
174 for (i = 0; i < CX25821_CTLS; i++)
175 if (cx25821_ctls[i].v.id == qctrl->id)
177 if (i == CX25821_CTLS) {
181 *qctrl = cx25821_ctls[i].v;
186 /* resource management */
187 int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh,
190 dprintk(1, "%s()\n", __func__);
191 if (fh->resources & bit)
192 /* have it already allocated */
196 mutex_lock(&dev->lock);
197 if (dev->channels[fh->channel_id].resources & bit) {
198 /* no, someone else uses it */
199 mutex_unlock(&dev->lock);
202 /* it's free, grab it */
203 fh->resources |= bit;
204 dev->channels[fh->channel_id].resources |= bit;
205 dprintk(1, "res: get %d\n", bit);
206 mutex_unlock(&dev->lock);
210 int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
212 return fh->resources & bit;
215 int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
217 return fh->dev->channels[fh->channel_id].resources & bit;
220 void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh,
223 BUG_ON((fh->resources & bits) != bits);
224 dprintk(1, "%s()\n", __func__);
226 mutex_lock(&dev->lock);
227 fh->resources &= ~bits;
228 dev->channels[fh->channel_id].resources &= ~bits;
229 dprintk(1, "res: put %d\n", bits);
230 mutex_unlock(&dev->lock);
233 static int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
235 struct v4l2_routing route;
236 memset(&route, 0, sizeof(route));
238 dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
239 __func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
240 INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
243 route.input = INPUT(input)->vmux;
245 /* Tell the internal A/V decoder */
246 cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
251 int cx25821_start_video_dma(struct cx25821_dev *dev,
252 struct cx25821_dmaqueue *q,
253 struct cx25821_buffer *buf,
254 const struct sram_channel *channel)
258 /* setup fifo + format */
259 cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
262 cx_write(channel->gpcnt_ctl, 3);
266 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
267 cx_set(channel->int_msk, 0x11);
270 cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
272 /* make sure upstream setting if any is reversed */
273 tmp = cx_read(VID_CH_MODE_SEL);
274 cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
279 static int cx25821_restart_video_queue(struct cx25821_dev *dev,
280 struct cx25821_dmaqueue *q,
281 const struct sram_channel *channel)
283 struct cx25821_buffer *buf, *prev;
284 struct list_head *item;
286 if (!list_empty(&q->active)) {
287 buf = list_entry(q->active.next, struct cx25821_buffer,
290 cx25821_start_video_dma(dev, q, buf, channel);
292 list_for_each(item, &q->active) {
293 buf = list_entry(item, struct cx25821_buffer, vb.queue);
294 buf->count = q->count++;
297 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
303 if (list_empty(&q->queued))
306 buf = list_entry(q->queued.next, struct cx25821_buffer,
310 list_move_tail(&buf->vb.queue, &q->active);
311 cx25821_start_video_dma(dev, q, buf, channel);
312 buf->vb.state = VIDEOBUF_ACTIVE;
313 buf->count = q->count++;
314 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
315 } else if (prev->vb.width == buf->vb.width &&
316 prev->vb.height == buf->vb.height &&
317 prev->fmt == buf->fmt) {
318 list_move_tail(&buf->vb.queue, &q->active);
319 buf->vb.state = VIDEOBUF_ACTIVE;
320 buf->count = q->count++;
321 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
322 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
330 static void cx25821_vid_timeout(unsigned long data)
332 struct cx25821_data *timeout_data = (struct cx25821_data *)data;
333 struct cx25821_dev *dev = timeout_data->dev;
334 const struct sram_channel *channel = timeout_data->channel;
335 struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq;
336 struct cx25821_buffer *buf;
339 /* cx25821_sram_channel_dump(dev, channel); */
340 cx_clear(channel->dma_ctl, 0x11);
342 spin_lock_irqsave(&dev->slock, flags);
343 while (!list_empty(&q->active)) {
344 buf = list_entry(q->active.next, struct cx25821_buffer,
346 list_del(&buf->vb.queue);
348 buf->vb.state = VIDEOBUF_ERROR;
349 wake_up(&buf->vb.done);
352 cx25821_restart_video_queue(dev, q, channel);
353 spin_unlock_irqrestore(&dev->slock, flags);
356 int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
361 const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
363 mask = cx_read(channel->int_msk);
364 if (0 == (status & mask))
367 cx_write(channel->int_stat, status);
369 /* risc op code error */
370 if (status & (1 << 16)) {
371 pr_warn("%s, %s: video risc op code error\n",
372 dev->name, channel->name);
373 cx_clear(channel->dma_ctl, 0x11);
374 cx25821_sram_channel_dump(dev, channel);
378 if (status & FLD_VID_DST_RISC1) {
379 spin_lock(&dev->slock);
380 count = cx_read(channel->gpcnt);
381 cx25821_video_wakeup(dev, &dev->channels[channel->i].vidq,
383 spin_unlock(&dev->slock);
389 dprintk(2, "stopper video\n");
390 spin_lock(&dev->slock);
391 cx25821_restart_video_queue(dev,
392 &dev->channels[channel->i].vidq, channel);
393 spin_unlock(&dev->slock);
399 static int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
402 struct cx25821_fh *fh = q->priv_data;
404 *size = fh->fmt->depth * fh->width * fh->height >> 3;
409 if (*size * *count > vid_limit * 1024 * 1024)
410 *count = (vid_limit * 1024 * 1024) / *size;
415 static int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
416 enum v4l2_field field)
418 struct cx25821_fh *fh = q->priv_data;
419 struct cx25821_dev *dev = fh->dev;
420 struct cx25821_buffer *buf =
421 container_of(vb, struct cx25821_buffer, vb);
422 int rc, init_buffer = 0;
424 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
425 int bpl_local = LINE_SIZE_D1;
426 int channel_opened = fh->channel_id;
428 BUG_ON(NULL == fh->fmt);
429 if (fh->width < 48 || fh->width > 720 ||
430 fh->height < 32 || fh->height > 576)
433 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
435 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
438 if (buf->fmt != fh->fmt ||
439 buf->vb.width != fh->width ||
440 buf->vb.height != fh->height || buf->vb.field != field) {
442 buf->vb.width = fh->width;
443 buf->vb.height = fh->height;
444 buf->vb.field = field;
448 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
450 rc = videobuf_iolock(q, &buf->vb, NULL);
452 printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
457 dprintk(1, "init_buffer=%d\n", init_buffer);
461 channel_opened = dev->channel_opened;
462 if (channel_opened < 0 || channel_opened > 7)
465 if (dev->channels[channel_opened].pixel_formats ==
467 buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
469 buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
471 if (dev->channels[channel_opened].pixel_formats ==
473 bpl_local = buf->bpl;
475 bpl_local = buf->bpl; /* Default */
477 if (channel_opened >= 0 && channel_opened <= 7) {
478 if (dev->channels[channel_opened]
479 .use_cif_resolution) {
480 if (dev->tvnorm & V4L2_STD_PAL_BG ||
481 dev->tvnorm & V4L2_STD_PAL_DK)
482 bpl_local = 352 << 1;
484 bpl_local = dev->channels[
491 switch (buf->vb.field) {
493 cx25821_risc_buffer(dev->pci, &buf->risc,
494 dma->sglist, 0, UNSET,
495 buf->bpl, 0, buf->vb.height);
497 case V4L2_FIELD_BOTTOM:
498 cx25821_risc_buffer(dev->pci, &buf->risc,
499 dma->sglist, UNSET, 0,
500 buf->bpl, 0, buf->vb.height);
502 case V4L2_FIELD_INTERLACED:
503 /* All other formats are top field first */
505 dprintk(1, "top field first\n");
507 cx25821_risc_buffer(dev->pci, &buf->risc,
508 dma->sglist, line0_offset,
509 bpl_local, bpl_local, bpl_local,
510 buf->vb.height >> 1);
512 case V4L2_FIELD_SEQ_TB:
513 cx25821_risc_buffer(dev->pci, &buf->risc,
515 0, buf->bpl * (buf->vb.height >> 1),
516 buf->bpl, 0, buf->vb.height >> 1);
518 case V4L2_FIELD_SEQ_BT:
519 cx25821_risc_buffer(dev->pci, &buf->risc,
521 buf->bpl * (buf->vb.height >> 1), 0,
522 buf->bpl, 0, buf->vb.height >> 1);
529 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
530 buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
531 fh->fmt->name, (unsigned long)buf->risc.dma);
533 buf->vb.state = VIDEOBUF_PREPARED;
538 cx25821_free_buffer(q, buf);
542 static void cx25821_buffer_release(struct videobuf_queue *q,
543 struct videobuf_buffer *vb)
545 struct cx25821_buffer *buf =
546 container_of(vb, struct cx25821_buffer, vb);
548 cx25821_free_buffer(q, buf);
551 static struct videobuf_queue *get_queue(struct cx25821_fh *fh)
554 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
562 static int cx25821_get_resource(struct cx25821_fh *fh, int resource)
565 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
573 static int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
575 struct cx25821_fh *fh = file->private_data;
577 return videobuf_mmap_mapper(get_queue(fh), vma);
581 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
583 struct cx25821_buffer *buf =
584 container_of(vb, struct cx25821_buffer, vb);
585 struct cx25821_buffer *prev;
586 struct cx25821_fh *fh = vq->priv_data;
587 struct cx25821_dev *dev = fh->dev;
588 struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq;
590 /* add jump to stopper */
591 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
592 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
593 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
595 dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
597 if (!list_empty(&q->queued)) {
598 list_add_tail(&buf->vb.queue, &q->queued);
599 buf->vb.state = VIDEOBUF_QUEUED;
600 dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
603 } else if (list_empty(&q->active)) {
604 list_add_tail(&buf->vb.queue, &q->active);
605 cx25821_start_video_dma(dev, q, buf,
606 dev->channels[fh->channel_id].sram_channels);
607 buf->vb.state = VIDEOBUF_ACTIVE;
608 buf->count = q->count++;
609 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
610 dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
611 buf, buf->vb.i, buf->count, q->count);
613 prev = list_entry(q->active.prev, struct cx25821_buffer,
615 if (prev->vb.width == buf->vb.width
616 && prev->vb.height == buf->vb.height
617 && prev->fmt == buf->fmt) {
618 list_add_tail(&buf->vb.queue, &q->active);
619 buf->vb.state = VIDEOBUF_ACTIVE;
620 buf->count = q->count++;
621 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
623 /* 64 bit bits 63-32 */
624 prev->risc.jmp[2] = cpu_to_le32(0);
625 dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
626 buf, buf->vb.i, buf->count);
629 list_add_tail(&buf->vb.queue, &q->queued);
630 buf->vb.state = VIDEOBUF_QUEUED;
631 dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
636 if (list_empty(&q->active))
637 dprintk(2, "active queue empty!\n");
640 static struct videobuf_queue_ops cx25821_video_qops = {
641 .buf_setup = cx25821_buffer_setup,
642 .buf_prepare = cx25821_buffer_prepare,
643 .buf_queue = buffer_queue,
644 .buf_release = cx25821_buffer_release,
647 static int video_open(struct file *file)
649 struct video_device *vdev = video_devdata(file);
650 struct cx25821_dev *dev = video_drvdata(file);
651 struct cx25821_fh *fh;
652 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
656 dprintk(1, "open dev=%s type=%s\n", video_device_node_name(vdev),
657 v4l2_type_names[type]);
659 for (ch_id = 0; ch_id < MAX_VID_CHANNEL_NUM - 1; ch_id++)
660 if (dev->channels[ch_id].video_dev == vdev)
664 if (ch_id >= MAX_VID_CHANNEL_NUM - 1)
667 /* allocate + initialize per filehandle data */
668 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
672 file->private_data = fh;
676 fh->channel_id = ch_id;
678 if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
683 dev->channel_opened = fh->channel_id;
684 if (dev->channels[ch_id].pixel_formats == PIXEL_FRMT_411)
685 pix_format = V4L2_PIX_FMT_Y41P;
687 pix_format = V4L2_PIX_FMT_YUYV;
688 fh->fmt = cx25821_format_by_fourcc(pix_format);
690 v4l2_prio_open(&dev->channels[ch_id].prio, &fh->prio);
692 videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops, &dev->pci->dev,
693 &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
694 V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
697 dprintk(1, "post videobuf_queue_init()\n");
702 static ssize_t video_read(struct file *file, char __user * data, size_t count,
705 struct cx25821_fh *fh = file->private_data;
708 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
709 if (cx25821_res_locked(fh, RESOURCE_VIDEO0))
712 return videobuf_read_one(&fh->vidq, data, count, ppos,
713 file->f_flags & O_NONBLOCK);
721 static unsigned int video_poll(struct file *file,
722 struct poll_table_struct *wait)
724 struct cx25821_fh *fh = file->private_data;
725 struct cx25821_buffer *buf;
727 if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
728 /* streaming capture */
729 if (list_empty(&fh->vidq.stream))
731 buf = list_entry(fh->vidq.stream.next,
732 struct cx25821_buffer, vb.stream);
735 buf = (struct cx25821_buffer *)fh->vidq.read_buf;
740 poll_wait(file, &buf->vb.done, wait);
741 if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
742 if (buf->vb.state == VIDEOBUF_DONE) {
743 struct cx25821_dev *dev = fh->dev;
745 if (dev && dev->channels[fh->channel_id]
746 .use_cif_resolution) {
747 u8 cam_id = *((char *)buf->vb.baddr + 3);
748 memcpy((char *)buf->vb.baddr,
749 (char *)buf->vb.baddr + (fh->width * 2),
751 *((char *)buf->vb.baddr + 3) = cam_id;
755 return POLLIN | POLLRDNORM;
761 static int video_release(struct file *file)
763 struct cx25821_fh *fh = file->private_data;
764 struct cx25821_dev *dev = fh->dev;
765 const struct sram_channel *sram_ch =
766 dev->channels[0].sram_channels;
768 /* stop the risc engine and fifo */
769 cx_write(sram_ch->dma_ctl, 0); /* FIFO and RISC disable */
771 /* stop video capture */
772 if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
773 videobuf_queue_cancel(&fh->vidq);
774 cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
777 if (fh->vidq.read_buf) {
778 cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
779 kfree(fh->vidq.read_buf);
782 videobuf_mmap_free(&fh->vidq);
784 v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
785 file->private_data = NULL;
792 static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
793 struct v4l2_format *f)
795 struct cx25821_fh *fh = priv;
797 f->fmt.pix.width = fh->width;
798 f->fmt.pix.height = fh->height;
799 f->fmt.pix.field = fh->vidq.field;
800 f->fmt.pix.pixelformat = fh->fmt->fourcc;
801 f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
802 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
807 static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
808 struct v4l2_format *f)
810 const struct cx25821_fmt *fmt;
811 enum v4l2_field field;
812 unsigned int maxw, maxh;
814 fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
818 field = f->fmt.pix.field;
822 if (V4L2_FIELD_ANY == field) {
823 if (f->fmt.pix.height > maxh / 2)
824 field = V4L2_FIELD_INTERLACED;
826 field = V4L2_FIELD_TOP;
831 case V4L2_FIELD_BOTTOM:
834 case V4L2_FIELD_INTERLACED:
840 f->fmt.pix.field = field;
841 if (f->fmt.pix.height < 32)
842 f->fmt.pix.height = 32;
843 if (f->fmt.pix.height > maxh)
844 f->fmt.pix.height = maxh;
845 if (f->fmt.pix.width < 48)
846 f->fmt.pix.width = 48;
847 if (f->fmt.pix.width > maxw)
848 f->fmt.pix.width = maxw;
849 f->fmt.pix.width &= ~0x03;
850 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
851 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
855 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
857 struct cx25821_fh *fh = priv;
858 struct cx25821_dev *dev = fh->dev;
860 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
863 if (unlikely(i != fh->type))
866 if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh,
870 return videobuf_streamon(get_queue(fh));
873 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
875 struct cx25821_fh *fh = priv;
876 struct cx25821_dev *dev = fh->dev;
879 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
884 res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
885 err = videobuf_streamoff(get_queue(fh));
888 cx25821_res_free(dev, fh, res);
892 static int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
894 if (tvnorm == V4L2_STD_PAL_BG) {
895 if (width == 352 || width == 720)
901 if (tvnorm == V4L2_STD_NTSC_M) {
902 if (width == 320 || width == 352 || width == 720)
910 static int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
912 if (tvnorm == V4L2_STD_PAL_BG) {
913 if (height == 576 || height == 288)
919 if (tvnorm == V4L2_STD_NTSC_M) {
920 if (height == 480 || height == 240)
929 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
930 struct v4l2_format *f)
932 struct cx25821_fh *fh = priv;
933 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
934 struct v4l2_mbus_framefmt mbus_fmt;
936 int pix_format = PIXEL_FRMT_422;
939 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
945 dprintk(2, "%s()\n", __func__);
946 err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
951 fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
952 fh->vidq.field = f->fmt.pix.field;
954 /* check if width and height is valid based on set standard */
955 if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
956 fh->width = f->fmt.pix.width;
958 if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
959 fh->height = f->fmt.pix.height;
961 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
962 pix_format = PIXEL_FRMT_411;
963 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
964 pix_format = PIXEL_FRMT_422;
968 cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
970 /* check if cif resolution */
971 if (fh->width == 320 || fh->width == 352)
972 dev->channels[fh->channel_id].use_cif_resolution = 1;
974 dev->channels[fh->channel_id].use_cif_resolution = 0;
976 dev->channels[fh->channel_id].cif_width = fh->width;
977 medusa_set_resolution(dev, fh->width, SRAM_CH00);
979 dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__, fh->width,
980 fh->height, fh->vidq.field);
981 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
982 cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
987 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
990 struct cx25821_fh *fh = priv;
991 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
993 ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
995 p->sequence = dev->channels[fh->channel_id].vidq.count;
1000 static int vidioc_log_status(struct file *file, void *priv)
1002 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1003 struct cx25821_fh *fh = priv;
1004 const struct sram_channel *sram_ch =
1005 dev->channels[fh->channel_id].sram_channels;
1008 cx25821_call_all(dev, core, log_status);
1009 tmp = cx_read(sram_ch->dma_ctl);
1010 pr_info("Video input 0 is %s\n",
1011 (tmp & 0x11) ? "streaming" : "stopped");
1016 static int cx25821_vidioc_querycap(struct file *file, void *priv,
1017 struct v4l2_capability *cap)
1019 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1020 struct cx25821_fh *fh = priv;
1021 const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
1022 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1023 const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
1025 strcpy(cap->driver, "cx25821");
1026 strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
1027 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1028 if (fh->channel_id >= VID_CHANNEL_NUM)
1029 cap->device_caps = cap_output;
1031 cap->device_caps = cap_input;
1032 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1036 static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1037 struct v4l2_fmtdesc *f)
1039 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1042 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1043 f->pixelformat = formats[f->index].fourcc;
1048 static int cx25821_vidioc_reqbufs(struct file *file, void *priv,
1049 struct v4l2_requestbuffers *p)
1051 struct cx25821_fh *fh = priv;
1052 return videobuf_reqbufs(get_queue(fh), p);
1055 static int cx25821_vidioc_querybuf(struct file *file, void *priv,
1056 struct v4l2_buffer *p)
1058 struct cx25821_fh *fh = priv;
1059 return videobuf_querybuf(get_queue(fh), p);
1062 static int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1064 struct cx25821_fh *fh = priv;
1065 return videobuf_qbuf(get_queue(fh), p);
1068 static int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
1070 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1071 struct cx25821_fh *fh = f;
1073 *p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
1078 static int cx25821_vidioc_s_priority(struct file *file, void *f,
1079 enum v4l2_priority prio)
1081 struct cx25821_fh *fh = f;
1082 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1084 return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
1088 static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1090 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1092 *tvnorms = dev->tvnorm;
1096 int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
1098 struct cx25821_fh *fh = priv;
1099 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1102 dprintk(1, "%s()\n", __func__);
1105 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1111 if (dev->tvnorm == tvnorms)
1114 mutex_lock(&dev->lock);
1115 cx25821_set_tvnorm(dev, tvnorms);
1116 mutex_unlock(&dev->lock);
1118 medusa_set_videostandard(dev);
1123 static int cx25821_vidioc_enum_input(struct file *file, void *priv,
1124 struct v4l2_input *i)
1126 static const char * const iname[] = {
1127 [CX25821_VMUX_COMPOSITE] = "Composite",
1128 [CX25821_VMUX_SVIDEO] = "S-Video",
1129 [CX25821_VMUX_DEBUG] = "for debug only",
1131 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1133 dprintk(1, "%s()\n", __func__);
1136 if (n >= CX25821_NR_INPUT)
1139 if (0 == INPUT(n)->type)
1142 i->type = V4L2_INPUT_TYPE_CAMERA;
1143 strcpy(i->name, iname[INPUT(n)->type]);
1145 i->std = CX25821_NORMS;
1149 static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1151 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1154 dprintk(1, "%s(): returns %d\n", __func__, *i);
1158 static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1160 struct cx25821_fh *fh = priv;
1161 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1164 dprintk(1, "%s(%d)\n", __func__, i);
1167 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1173 if (i >= CX25821_NR_INPUT || INPUT(i)->type == 0)
1176 mutex_lock(&dev->lock);
1177 cx25821_video_mux(dev, i);
1178 mutex_unlock(&dev->lock);
1182 #ifdef CONFIG_VIDEO_ADV_DEBUG
1183 int cx25821_vidioc_g_register(struct file *file, void *fh,
1184 struct v4l2_dbg_register *reg)
1186 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1188 if (!v4l2_chip_match_host(®->match))
1191 cx25821_call_all(dev, core, g_register, reg);
1196 int cx25821_vidioc_s_register(struct file *file, void *fh,
1197 const struct v4l2_dbg_register *reg)
1199 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1201 if (!v4l2_chip_match_host(®->match))
1204 cx25821_call_all(dev, core, s_register, reg);
1211 /*****************************************************************************/
1212 static const struct v4l2_queryctrl no_ctl = {
1214 .flags = V4L2_CTRL_FLAG_DISABLED,
1217 static struct v4l2_queryctrl cx25821_ctls[] = {
1220 .id = V4L2_CID_BRIGHTNESS,
1221 .name = "Brightness",
1225 .default_value = 6200,
1226 .type = V4L2_CTRL_TYPE_INTEGER,
1228 .id = V4L2_CID_CONTRAST,
1233 .default_value = 5000,
1234 .type = V4L2_CTRL_TYPE_INTEGER,
1236 .id = V4L2_CID_SATURATION,
1237 .name = "Saturation",
1241 .default_value = 5000,
1242 .type = V4L2_CTRL_TYPE_INTEGER,
1249 .default_value = 5000,
1250 .type = V4L2_CTRL_TYPE_INTEGER,
1253 static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1255 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1259 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
1261 for (i = 0; i < CX25821_CTLS; i++)
1262 if (cx25821_ctls[i].id == qctrl->id)
1264 if (i == CX25821_CTLS) {
1268 *qctrl = cx25821_ctls[i];
1272 static int cx25821_vidioc_queryctrl(struct file *file, void *priv,
1273 struct v4l2_queryctrl *qctrl)
1275 return cx25821_ctrl_query(qctrl);
1278 /* ------------------------------------------------------------------ */
1279 /* VIDEO CTRL IOCTLS */
1281 static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
1285 for (i = 0; i < CX25821_CTLS; i++)
1286 if (cx25821_ctls[i].id == id)
1287 return cx25821_ctls + i;
1291 static int cx25821_vidioc_g_ctrl(struct file *file, void *priv,
1292 struct v4l2_control *ctl)
1294 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1295 struct cx25821_fh *fh = priv;
1297 const struct v4l2_queryctrl *ctrl;
1299 ctrl = ctrl_by_id(ctl->id);
1304 case V4L2_CID_BRIGHTNESS:
1305 ctl->value = dev->channels[fh->channel_id].ctl_bright;
1308 ctl->value = dev->channels[fh->channel_id].ctl_hue;
1310 case V4L2_CID_CONTRAST:
1311 ctl->value = dev->channels[fh->channel_id].ctl_contrast;
1313 case V4L2_CID_SATURATION:
1314 ctl->value = dev->channels[fh->channel_id].ctl_saturation;
1320 static int cx25821_set_control(struct cx25821_dev *dev,
1321 struct v4l2_control *ctl, int chan_num)
1324 const struct v4l2_queryctrl *ctrl;
1328 ctrl = ctrl_by_id(ctl->id);
1333 switch (ctrl->type) {
1334 case V4L2_CTRL_TYPE_BOOLEAN:
1335 case V4L2_CTRL_TYPE_MENU:
1336 case V4L2_CTRL_TYPE_INTEGER:
1337 if (ctl->value < ctrl->minimum)
1338 ctl->value = ctrl->minimum;
1339 if (ctl->value > ctrl->maximum)
1340 ctl->value = ctrl->maximum;
1347 case V4L2_CID_BRIGHTNESS:
1348 dev->channels[chan_num].ctl_bright = ctl->value;
1349 medusa_set_brightness(dev, ctl->value, chan_num);
1352 dev->channels[chan_num].ctl_hue = ctl->value;
1353 medusa_set_hue(dev, ctl->value, chan_num);
1355 case V4L2_CID_CONTRAST:
1356 dev->channels[chan_num].ctl_contrast = ctl->value;
1357 medusa_set_contrast(dev, ctl->value, chan_num);
1359 case V4L2_CID_SATURATION:
1360 dev->channels[chan_num].ctl_saturation = ctl->value;
1361 medusa_set_saturation(dev, ctl->value, chan_num);
1370 static int vidioc_s_ctrl(struct file *file, void *priv,
1371 struct v4l2_control *ctl)
1373 struct cx25821_fh *fh = priv;
1374 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1378 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1384 return cx25821_set_control(dev, ctl, fh->channel_id);
1387 static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num)
1389 struct v4l2_control ctrl;
1391 for (i = 0; i < CX25821_CTLS; i++) {
1392 ctrl.id = cx25821_ctls[i].id;
1393 ctrl.value = cx25821_ctls[i].default_value;
1395 cx25821_set_control(dev, &ctrl, chan_num);
1399 static int cx25821_vidioc_cropcap(struct file *file, void *priv,
1400 struct v4l2_cropcap *cropcap)
1402 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1404 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1406 cropcap->bounds.top = 0;
1407 cropcap->bounds.left = 0;
1408 cropcap->bounds.width = 720;
1409 cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1410 cropcap->pixelaspect.numerator =
1411 dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1412 cropcap->pixelaspect.denominator =
1413 dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1414 cropcap->defrect = cropcap->bounds;
1418 static int cx25821_vidioc_s_crop(struct file *file, void *priv, const struct v4l2_crop *crop)
1420 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1421 struct cx25821_fh *fh = priv;
1425 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1430 /* cx25821_vidioc_s_crop not supported */
1434 static int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1436 /* cx25821_vidioc_g_crop not supported */
1440 static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
1443 struct cx25821_fh *fh = file->private_data;
1444 struct cx25821_dev *dev = fh->dev;
1446 struct upstream_user_struct *data_from_user;
1448 data_from_user = (struct upstream_user_struct *)arg;
1450 if (!data_from_user) {
1451 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1455 command = data_from_user->command;
1457 if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1460 dev->input_filename = data_from_user->input_filename;
1461 dev->input_audiofilename = data_from_user->input_filename;
1462 dev->vid_stdname = data_from_user->vid_stdname;
1463 dev->pixel_format = data_from_user->pixel_format;
1464 dev->channel_select = data_from_user->channel_select;
1465 dev->command = data_from_user->command;
1468 case UPSTREAM_START_VIDEO:
1469 cx25821_start_upstream_video_ch1(dev, data_from_user);
1472 case UPSTREAM_STOP_VIDEO:
1473 cx25821_stop_upstream_video_ch1(dev);
1480 static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
1483 struct cx25821_fh *fh = file->private_data;
1484 struct cx25821_dev *dev = fh->dev;
1486 struct upstream_user_struct *data_from_user;
1488 data_from_user = (struct upstream_user_struct *)arg;
1490 if (!data_from_user) {
1491 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1495 command = data_from_user->command;
1497 if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1500 dev->input_filename_ch2 = data_from_user->input_filename;
1501 dev->input_audiofilename = data_from_user->input_filename;
1502 dev->vid_stdname_ch2 = data_from_user->vid_stdname;
1503 dev->pixel_format_ch2 = data_from_user->pixel_format;
1504 dev->channel_select_ch2 = data_from_user->channel_select;
1505 dev->command_ch2 = data_from_user->command;
1508 case UPSTREAM_START_VIDEO:
1509 cx25821_start_upstream_video_ch2(dev, data_from_user);
1512 case UPSTREAM_STOP_VIDEO:
1513 cx25821_stop_upstream_video_ch2(dev);
1520 static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
1523 struct cx25821_fh *fh = file->private_data;
1524 struct cx25821_dev *dev = fh->dev;
1526 struct upstream_user_struct *data_from_user;
1528 data_from_user = (struct upstream_user_struct *)arg;
1530 if (!data_from_user) {
1531 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1535 command = data_from_user->command;
1537 if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
1540 dev->input_filename = data_from_user->input_filename;
1541 dev->input_audiofilename = data_from_user->input_filename;
1542 dev->vid_stdname = data_from_user->vid_stdname;
1543 dev->pixel_format = data_from_user->pixel_format;
1544 dev->channel_select = data_from_user->channel_select;
1545 dev->command = data_from_user->command;
1548 case UPSTREAM_START_AUDIO:
1549 cx25821_start_upstream_audio(dev, data_from_user);
1552 case UPSTREAM_STOP_AUDIO:
1553 cx25821_stop_upstream_audio(dev);
1560 static long video_ioctl_set(struct file *file, unsigned int cmd,
1563 struct cx25821_fh *fh = file->private_data;
1564 struct cx25821_dev *dev = fh->dev;
1565 struct downstream_user_struct *data_from_user;
1568 int selected_channel = 0;
1574 data_from_user = (struct downstream_user_struct *)arg;
1576 if (!data_from_user) {
1577 pr_err("%s(): User data is INVALID. Returning\n", __func__);
1581 command = data_from_user->command;
1583 if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
1584 && command != ENABLE_CIF_RESOLUTION && command != REG_READ
1585 && command != REG_WRITE && command != MEDUSA_READ
1586 && command != MEDUSA_WRITE) {
1592 if (!strcmp(data_from_user->vid_stdname, "PAL"))
1593 dev->tvnorm = V4L2_STD_PAL_BG;
1595 dev->tvnorm = V4L2_STD_NTSC_M;
1596 medusa_set_videostandard(dev);
1599 case SET_PIXEL_FORMAT:
1600 selected_channel = data_from_user->decoder_select;
1601 pix_format = data_from_user->pixel_format;
1603 if (!(selected_channel <= 7 && selected_channel >= 0)) {
1604 selected_channel -= 4;
1605 selected_channel = selected_channel % 8;
1608 if (selected_channel >= 0)
1609 cx25821_set_pixel_format(dev, selected_channel,
1614 case ENABLE_CIF_RESOLUTION:
1615 selected_channel = data_from_user->decoder_select;
1616 cif_enable = data_from_user->cif_resolution_enable;
1617 cif_width = data_from_user->cif_width;
1620 if (dev->tvnorm & V4L2_STD_PAL_BG
1621 || dev->tvnorm & V4L2_STD_PAL_DK) {
1625 if (cif_width != 320 && cif_width != 352)
1630 if (!(selected_channel <= 7 && selected_channel >= 0)) {
1631 selected_channel -= 4;
1632 selected_channel = selected_channel % 8;
1635 if (selected_channel <= 7 && selected_channel >= 0) {
1636 dev->channels[selected_channel].use_cif_resolution =
1638 dev->channels[selected_channel].cif_width = width;
1640 for (i = 0; i < VID_CHANNEL_NUM; i++) {
1641 dev->channels[i].use_cif_resolution =
1643 dev->channels[i].cif_width = width;
1647 medusa_set_resolution(dev, width, selected_channel);
1650 data_from_user->reg_data = cx_read(data_from_user->reg_address);
1653 cx_write(data_from_user->reg_address, data_from_user->reg_data);
1656 cx25821_i2c_read(&dev->i2c_bus[0],
1657 (u16) data_from_user->reg_address,
1658 &data_from_user->reg_data);
1661 cx25821_i2c_write(&dev->i2c_bus[0],
1662 (u16) data_from_user->reg_address,
1663 data_from_user->reg_data);
1670 static long cx25821_video_ioctl(struct file *file,
1671 unsigned int cmd, unsigned long arg)
1675 struct cx25821_fh *fh = file->private_data;
1677 /* check to see if it's the video upstream */
1678 if (fh->channel_id == SRAM_CH09) {
1679 ret = video_ioctl_upstream9(file, cmd, arg);
1681 } else if (fh->channel_id == SRAM_CH10) {
1682 ret = video_ioctl_upstream10(file, cmd, arg);
1684 } else if (fh->channel_id == SRAM_CH11) {
1685 ret = video_ioctl_upstream11(file, cmd, arg);
1686 ret = video_ioctl_set(file, cmd, arg);
1690 return video_ioctl2(file, cmd, arg);
1693 /* exported stuff */
1694 static const struct v4l2_file_operations video_fops = {
1695 .owner = THIS_MODULE,
1697 .release = video_release,
1700 .mmap = cx25821_video_mmap,
1701 .ioctl = cx25821_video_ioctl,
1704 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1705 .vidioc_querycap = cx25821_vidioc_querycap,
1706 .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
1707 .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
1708 .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
1709 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1710 .vidioc_reqbufs = cx25821_vidioc_reqbufs,
1711 .vidioc_querybuf = cx25821_vidioc_querybuf,
1712 .vidioc_qbuf = cx25821_vidioc_qbuf,
1713 .vidioc_dqbuf = vidioc_dqbuf,
1714 .vidioc_g_std = cx25821_vidioc_g_std,
1715 .vidioc_s_std = cx25821_vidioc_s_std,
1716 .vidioc_cropcap = cx25821_vidioc_cropcap,
1717 .vidioc_s_crop = cx25821_vidioc_s_crop,
1718 .vidioc_g_crop = cx25821_vidioc_g_crop,
1719 .vidioc_enum_input = cx25821_vidioc_enum_input,
1720 .vidioc_g_input = cx25821_vidioc_g_input,
1721 .vidioc_s_input = cx25821_vidioc_s_input,
1722 .vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
1723 .vidioc_s_ctrl = vidioc_s_ctrl,
1724 .vidioc_queryctrl = cx25821_vidioc_queryctrl,
1725 .vidioc_streamon = vidioc_streamon,
1726 .vidioc_streamoff = vidioc_streamoff,
1727 .vidioc_log_status = vidioc_log_status,
1728 .vidioc_g_priority = cx25821_vidioc_g_priority,
1729 .vidioc_s_priority = cx25821_vidioc_s_priority,
1730 #ifdef CONFIG_VIDEO_ADV_DEBUG
1731 .vidioc_g_register = cx25821_vidioc_g_register,
1732 .vidioc_s_register = cx25821_vidioc_s_register,
1736 static const struct video_device cx25821_video_device = {
1737 .name = "cx25821-video",
1738 .fops = &video_fops,
1740 .ioctl_ops = &video_ioctl_ops,
1741 .tvnorms = CX25821_NORMS,
1744 void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
1746 cx_clear(PCI_INT_MSK, 1);
1748 if (dev->channels[chan_num].video_dev) {
1749 if (video_is_registered(dev->channels[chan_num].video_dev))
1750 video_unregister_device(
1751 dev->channels[chan_num].video_dev);
1753 video_device_release(
1754 dev->channels[chan_num].video_dev);
1756 dev->channels[chan_num].video_dev = NULL;
1758 btcx_riscmem_free(dev->pci,
1759 &dev->channels[chan_num].vidq.stopper);
1761 pr_warn("device %d released!\n", chan_num);
1766 int cx25821_video_register(struct cx25821_dev *dev)
1771 spin_lock_init(&dev->slock);
1773 for (i = 0; i < VID_CHANNEL_NUM; ++i) {
1774 if (i == SRAM_CH08) /* audio channel */
1777 cx25821_init_controls(dev, i);
1779 cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
1780 dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
1782 dev->channels[i].sram_channels = &cx25821_sram_channels[i];
1783 dev->channels[i].video_dev = NULL;
1784 dev->channels[i].resources = 0;
1786 cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
1788 INIT_LIST_HEAD(&dev->channels[i].vidq.active);
1789 INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
1791 dev->channels[i].timeout_data.dev = dev;
1792 dev->channels[i].timeout_data.channel =
1793 &cx25821_sram_channels[i];
1794 dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
1795 dev->channels[i].vidq.timeout.data =
1796 (unsigned long)&dev->channels[i].timeout_data;
1797 init_timer(&dev->channels[i].vidq.timeout);
1799 /* register v4l devices */
1800 dev->channels[i].video_dev = cx25821_vdev_init(dev, dev->pci,
1801 &cx25821_video_device, "video");
1803 err = video_register_device(dev->channels[i].video_dev,
1804 VFL_TYPE_GRABBER, video_nr[dev->nr]);
1811 /* set PCI interrupt */
1812 cx_set(PCI_INT_MSK, 0xff);
1814 /* initial device configuration */
1815 mutex_lock(&dev->lock);
1816 dev->tvnorm = V4L2_STD_NTSC_M,
1817 cx25821_set_tvnorm(dev, dev->tvnorm);
1818 mutex_unlock(&dev->lock);
1823 cx25821_video_unregister(dev, i);