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 unsigned int vid_limit = 16;
50 module_param(vid_limit, int, 0644);
51 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
53 static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num);
55 #define FORMAT_FLAGS_PACKED 0x01
57 struct cx25821_fmt formats[] = {
59 .name = "8 bpp, gray",
60 .fourcc = V4L2_PIX_FMT_GREY,
62 .flags = FORMAT_FLAGS_PACKED,
64 .name = "4:1:1, packed, Y41P",
65 .fourcc = V4L2_PIX_FMT_Y41P,
67 .flags = FORMAT_FLAGS_PACKED,
69 .name = "4:2:2, packed, YUYV",
70 .fourcc = V4L2_PIX_FMT_YUYV,
72 .flags = FORMAT_FLAGS_PACKED,
74 .name = "4:2:2, packed, UYVY",
75 .fourcc = V4L2_PIX_FMT_UYVY,
77 .flags = FORMAT_FLAGS_PACKED,
80 .fourcc = V4L2_PIX_FMT_YUV420,
82 .flags = FORMAT_FLAGS_PACKED,
86 int cx25821_get_format_size(void)
88 return ARRAY_SIZE(formats);
91 struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
95 if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P)
98 for (i = 0; i < ARRAY_SIZE(formats); i++)
99 if (formats[i].fourcc == fourcc)
102 pr_err("%s(0x%08x) NOT FOUND\n", __func__, fourcc);
106 void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
109 struct cx25821_buffer *buf;
112 for (bc = 0;; bc++) {
113 if (list_empty(&q->active)) {
114 dprintk(1, "bc=%d (=0: active empty)\n", bc);
118 buf = list_entry(q->active.next, struct cx25821_buffer,
121 /* count comes from the hw and it is 16bit wide --
122 * this trick handles wrap-arounds correctly for
123 * up to 32767 buffers in flight... */
124 if ((s16) (count - buf->count) < 0)
127 v4l2_get_timestamp(&buf->vb.ts);
128 buf->vb.state = VIDEOBUF_DONE;
129 list_del(&buf->vb.queue);
130 wake_up(&buf->vb.done);
133 if (list_empty(&q->active))
134 del_timer(&q->timeout);
136 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
138 pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
141 int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
143 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
144 __func__, (unsigned int)norm, v4l2_norm_to_name(norm));
148 /* Tell the internal A/V decoder */
149 cx25821_call_all(dev, core, s_std, norm);
154 struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
156 const struct video_device *template,
159 struct video_device *vfd;
160 dprintk(1, "%s()\n", __func__);
162 vfd = video_device_alloc();
166 vfd->v4l2_dev = &dev->v4l2_dev;
167 vfd->release = video_device_release;
168 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, type,
169 cx25821_boards[dev->board].name);
170 video_set_drvdata(vfd, dev);
175 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
179 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
181 for (i = 0; i < CX25821_CTLS; i++)
182 if (cx25821_ctls[i].v.id == qctrl->id)
184 if (i == CX25821_CTLS) {
188 *qctrl = cx25821_ctls[i].v;
193 /* resource management */
194 int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh,
197 dprintk(1, "%s()\n", __func__);
198 if (fh->resources & bit)
199 /* have it already allocated */
203 mutex_lock(&dev->lock);
204 if (dev->channels[fh->channel_id].resources & bit) {
205 /* no, someone else uses it */
206 mutex_unlock(&dev->lock);
209 /* it's free, grab it */
210 fh->resources |= bit;
211 dev->channels[fh->channel_id].resources |= bit;
212 dprintk(1, "res: get %d\n", bit);
213 mutex_unlock(&dev->lock);
217 int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
219 return fh->resources & bit;
222 int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
224 return fh->dev->channels[fh->channel_id].resources & bit;
227 void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh,
230 BUG_ON((fh->resources & bits) != bits);
231 dprintk(1, "%s()\n", __func__);
233 mutex_lock(&dev->lock);
234 fh->resources &= ~bits;
235 dev->channels[fh->channel_id].resources &= ~bits;
236 dprintk(1, "res: put %d\n", bits);
237 mutex_unlock(&dev->lock);
240 int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
242 struct v4l2_routing route;
243 memset(&route, 0, sizeof(route));
245 dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
246 __func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
247 INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
250 route.input = INPUT(input)->vmux;
252 /* Tell the internal A/V decoder */
253 cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
258 int cx25821_start_video_dma(struct cx25821_dev *dev,
259 struct cx25821_dmaqueue *q,
260 struct cx25821_buffer *buf,
261 struct sram_channel *channel)
265 /* setup fifo + format */
266 cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
269 cx_write(channel->gpcnt_ctl, 3);
273 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
274 cx_set(channel->int_msk, 0x11);
277 cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
279 /* make sure upstream setting if any is reversed */
280 tmp = cx_read(VID_CH_MODE_SEL);
281 cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
286 static int cx25821_restart_video_queue(struct cx25821_dev *dev,
287 struct cx25821_dmaqueue *q,
288 struct sram_channel *channel)
290 struct cx25821_buffer *buf, *prev;
291 struct list_head *item;
293 if (!list_empty(&q->active)) {
294 buf = list_entry(q->active.next, struct cx25821_buffer,
297 cx25821_start_video_dma(dev, q, buf, channel);
299 list_for_each(item, &q->active) {
300 buf = list_entry(item, struct cx25821_buffer, vb.queue);
301 buf->count = q->count++;
304 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
310 if (list_empty(&q->queued))
313 buf = list_entry(q->queued.next, struct cx25821_buffer,
317 list_move_tail(&buf->vb.queue, &q->active);
318 cx25821_start_video_dma(dev, q, buf, channel);
319 buf->vb.state = VIDEOBUF_ACTIVE;
320 buf->count = q->count++;
321 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
322 } else if (prev->vb.width == buf->vb.width &&
323 prev->vb.height == buf->vb.height &&
324 prev->fmt == buf->fmt) {
325 list_move_tail(&buf->vb.queue, &q->active);
326 buf->vb.state = VIDEOBUF_ACTIVE;
327 buf->count = q->count++;
328 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
329 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
337 static void cx25821_vid_timeout(unsigned long data)
339 struct cx25821_data *timeout_data = (struct cx25821_data *)data;
340 struct cx25821_dev *dev = timeout_data->dev;
341 struct sram_channel *channel = timeout_data->channel;
342 struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq;
343 struct cx25821_buffer *buf;
346 /* cx25821_sram_channel_dump(dev, channel); */
347 cx_clear(channel->dma_ctl, 0x11);
349 spin_lock_irqsave(&dev->slock, flags);
350 while (!list_empty(&q->active)) {
351 buf = list_entry(q->active.next, struct cx25821_buffer,
353 list_del(&buf->vb.queue);
355 buf->vb.state = VIDEOBUF_ERROR;
356 wake_up(&buf->vb.done);
359 cx25821_restart_video_queue(dev, q, channel);
360 spin_unlock_irqrestore(&dev->slock, flags);
363 int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
368 struct sram_channel *channel = dev->channels[chan_num].sram_channels;
370 mask = cx_read(channel->int_msk);
371 if (0 == (status & mask))
374 cx_write(channel->int_stat, status);
376 /* risc op code error */
377 if (status & (1 << 16)) {
378 pr_warn("%s, %s: video risc op code error\n",
379 dev->name, channel->name);
380 cx_clear(channel->dma_ctl, 0x11);
381 cx25821_sram_channel_dump(dev, channel);
385 if (status & FLD_VID_DST_RISC1) {
386 spin_lock(&dev->slock);
387 count = cx_read(channel->gpcnt);
388 cx25821_video_wakeup(dev, &dev->channels[channel->i].vidq,
390 spin_unlock(&dev->slock);
396 dprintk(2, "stopper video\n");
397 spin_lock(&dev->slock);
398 cx25821_restart_video_queue(dev,
399 &dev->channels[channel->i].vidq, channel);
400 spin_unlock(&dev->slock);
406 int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
409 struct cx25821_fh *fh = q->priv_data;
411 *size = fh->fmt->depth * fh->width * fh->height >> 3;
416 if (*size * *count > vid_limit * 1024 * 1024)
417 *count = (vid_limit * 1024 * 1024) / *size;
422 int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
423 enum v4l2_field field)
425 struct cx25821_fh *fh = q->priv_data;
426 struct cx25821_dev *dev = fh->dev;
427 struct cx25821_buffer *buf =
428 container_of(vb, struct cx25821_buffer, vb);
429 int rc, init_buffer = 0;
431 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
432 int bpl_local = LINE_SIZE_D1;
433 int channel_opened = fh->channel_id;
435 BUG_ON(NULL == fh->fmt);
436 if (fh->width < 48 || fh->width > 720 ||
437 fh->height < 32 || fh->height > 576)
440 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
442 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
445 if (buf->fmt != fh->fmt ||
446 buf->vb.width != fh->width ||
447 buf->vb.height != fh->height || buf->vb.field != field) {
449 buf->vb.width = fh->width;
450 buf->vb.height = fh->height;
451 buf->vb.field = field;
455 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
457 rc = videobuf_iolock(q, &buf->vb, NULL);
459 printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
464 dprintk(1, "init_buffer=%d\n", init_buffer);
468 channel_opened = dev->channel_opened;
469 if (channel_opened < 0 || channel_opened > 7)
472 if (dev->channels[channel_opened].pixel_formats ==
474 buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
476 buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
478 if (dev->channels[channel_opened].pixel_formats ==
480 bpl_local = buf->bpl;
482 bpl_local = buf->bpl; /* Default */
484 if (channel_opened >= 0 && channel_opened <= 7) {
485 if (dev->channels[channel_opened]
486 .use_cif_resolution) {
487 if (dev->tvnorm & V4L2_STD_PAL_BG ||
488 dev->tvnorm & V4L2_STD_PAL_DK)
489 bpl_local = 352 << 1;
491 bpl_local = dev->channels[
498 switch (buf->vb.field) {
500 cx25821_risc_buffer(dev->pci, &buf->risc,
501 dma->sglist, 0, UNSET,
502 buf->bpl, 0, buf->vb.height);
504 case V4L2_FIELD_BOTTOM:
505 cx25821_risc_buffer(dev->pci, &buf->risc,
506 dma->sglist, UNSET, 0,
507 buf->bpl, 0, buf->vb.height);
509 case V4L2_FIELD_INTERLACED:
510 /* All other formats are top field first */
512 dprintk(1, "top field first\n");
514 cx25821_risc_buffer(dev->pci, &buf->risc,
515 dma->sglist, line0_offset,
516 bpl_local, bpl_local, bpl_local,
517 buf->vb.height >> 1);
519 case V4L2_FIELD_SEQ_TB:
520 cx25821_risc_buffer(dev->pci, &buf->risc,
522 0, buf->bpl * (buf->vb.height >> 1),
523 buf->bpl, 0, buf->vb.height >> 1);
525 case V4L2_FIELD_SEQ_BT:
526 cx25821_risc_buffer(dev->pci, &buf->risc,
528 buf->bpl * (buf->vb.height >> 1), 0,
529 buf->bpl, 0, buf->vb.height >> 1);
536 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
537 buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
538 fh->fmt->name, (unsigned long)buf->risc.dma);
540 buf->vb.state = VIDEOBUF_PREPARED;
545 cx25821_free_buffer(q, buf);
549 void cx25821_buffer_release(struct videobuf_queue *q,
550 struct videobuf_buffer *vb)
552 struct cx25821_buffer *buf =
553 container_of(vb, struct cx25821_buffer, vb);
555 cx25821_free_buffer(q, buf);
558 struct videobuf_queue *get_queue(struct cx25821_fh *fh)
561 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
569 int cx25821_get_resource(struct cx25821_fh *fh, int resource)
572 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
580 int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
582 struct cx25821_fh *fh = file->private_data;
584 return videobuf_mmap_mapper(get_queue(fh), vma);
588 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
590 struct cx25821_buffer *buf =
591 container_of(vb, struct cx25821_buffer, vb);
592 struct cx25821_buffer *prev;
593 struct cx25821_fh *fh = vq->priv_data;
594 struct cx25821_dev *dev = fh->dev;
595 struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq;
597 /* add jump to stopper */
598 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
599 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
600 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
602 dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
604 if (!list_empty(&q->queued)) {
605 list_add_tail(&buf->vb.queue, &q->queued);
606 buf->vb.state = VIDEOBUF_QUEUED;
607 dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
610 } else if (list_empty(&q->active)) {
611 list_add_tail(&buf->vb.queue, &q->active);
612 cx25821_start_video_dma(dev, q, buf,
613 dev->channels[fh->channel_id].sram_channels);
614 buf->vb.state = VIDEOBUF_ACTIVE;
615 buf->count = q->count++;
616 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
617 dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
618 buf, buf->vb.i, buf->count, q->count);
620 prev = list_entry(q->active.prev, struct cx25821_buffer,
622 if (prev->vb.width == buf->vb.width
623 && prev->vb.height == buf->vb.height
624 && prev->fmt == buf->fmt) {
625 list_add_tail(&buf->vb.queue, &q->active);
626 buf->vb.state = VIDEOBUF_ACTIVE;
627 buf->count = q->count++;
628 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
630 /* 64 bit bits 63-32 */
631 prev->risc.jmp[2] = cpu_to_le32(0);
632 dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
633 buf, buf->vb.i, buf->count);
636 list_add_tail(&buf->vb.queue, &q->queued);
637 buf->vb.state = VIDEOBUF_QUEUED;
638 dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
643 if (list_empty(&q->active))
644 dprintk(2, "active queue empty!\n");
647 static struct videobuf_queue_ops cx25821_video_qops = {
648 .buf_setup = cx25821_buffer_setup,
649 .buf_prepare = cx25821_buffer_prepare,
650 .buf_queue = buffer_queue,
651 .buf_release = cx25821_buffer_release,
654 static int video_open(struct file *file)
656 struct video_device *vdev = video_devdata(file);
657 struct cx25821_dev *h, *dev = video_drvdata(file);
658 struct cx25821_fh *fh;
659 struct list_head *list;
660 int minor = video_devdata(file)->minor;
661 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
666 dprintk(1, "open dev=%s type=%s\n", video_device_node_name(vdev),
667 v4l2_type_names[type]);
669 /* allocate + initialize per filehandle data */
670 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
674 mutex_lock(&cx25821_devlist_mutex);
676 list_for_each(list, &cx25821_devlist)
678 h = list_entry(list, struct cx25821_dev, devlist);
680 for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; i++) {
681 if (h->channels[i].video_dev &&
682 h->channels[i].video_dev->minor == minor) {
685 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
691 mutex_unlock(&cx25821_devlist_mutex);
696 file->private_data = fh;
700 fh->channel_id = ch_id;
702 if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
707 dev->channel_opened = fh->channel_id;
708 if (dev->channels[ch_id].pixel_formats == PIXEL_FRMT_411)
709 pix_format = V4L2_PIX_FMT_Y41P;
711 pix_format = V4L2_PIX_FMT_YUYV;
712 fh->fmt = cx25821_format_by_fourcc(pix_format);
714 v4l2_prio_open(&dev->channels[ch_id].prio, &fh->prio);
716 videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops, &dev->pci->dev,
717 &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
718 V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
721 dprintk(1, "post videobuf_queue_init()\n");
722 mutex_unlock(&cx25821_devlist_mutex);
727 static ssize_t video_read(struct file *file, char __user * data, size_t count,
730 struct cx25821_fh *fh = file->private_data;
733 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
734 if (cx25821_res_locked(fh, RESOURCE_VIDEO0))
737 return videobuf_read_one(&fh->vidq, data, count, ppos,
738 file->f_flags & O_NONBLOCK);
746 static unsigned int video_poll(struct file *file,
747 struct poll_table_struct *wait)
749 struct cx25821_fh *fh = file->private_data;
750 struct cx25821_buffer *buf;
752 if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
753 /* streaming capture */
754 if (list_empty(&fh->vidq.stream))
756 buf = list_entry(fh->vidq.stream.next,
757 struct cx25821_buffer, vb.stream);
760 buf = (struct cx25821_buffer *)fh->vidq.read_buf;
765 poll_wait(file, &buf->vb.done, wait);
766 if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
767 if (buf->vb.state == VIDEOBUF_DONE) {
768 struct cx25821_dev *dev = fh->dev;
770 if (dev && dev->channels[fh->channel_id]
771 .use_cif_resolution) {
772 u8 cam_id = *((char *)buf->vb.baddr + 3);
773 memcpy((char *)buf->vb.baddr,
774 (char *)buf->vb.baddr + (fh->width * 2),
776 *((char *)buf->vb.baddr + 3) = cam_id;
780 return POLLIN | POLLRDNORM;
786 static int video_release(struct file *file)
788 struct cx25821_fh *fh = file->private_data;
789 struct cx25821_dev *dev = fh->dev;
791 /* stop the risc engine and fifo */
792 cx_write(channel0->dma_ctl, 0); /* FIFO and RISC disable */
794 /* stop video capture */
795 if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
796 videobuf_queue_cancel(&fh->vidq);
797 cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
800 if (fh->vidq.read_buf) {
801 cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
802 kfree(fh->vidq.read_buf);
805 videobuf_mmap_free(&fh->vidq);
807 v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
808 file->private_data = NULL;
814 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
816 struct cx25821_fh *fh = priv;
817 struct cx25821_dev *dev = fh->dev;
819 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
822 if (unlikely(i != fh->type))
825 if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh,
829 return videobuf_streamon(get_queue(fh));
832 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
834 struct cx25821_fh *fh = priv;
835 struct cx25821_dev *dev = fh->dev;
838 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
843 res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
844 err = videobuf_streamoff(get_queue(fh));
847 cx25821_res_free(dev, fh, res);
851 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
852 struct v4l2_format *f)
854 struct cx25821_fh *fh = priv;
855 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
856 struct v4l2_mbus_framefmt mbus_fmt;
858 int pix_format = PIXEL_FRMT_422;
861 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
867 dprintk(2, "%s()\n", __func__);
868 err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
873 fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
874 fh->vidq.field = f->fmt.pix.field;
876 /* check if width and height is valid based on set standard */
877 if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
878 fh->width = f->fmt.pix.width;
880 if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
881 fh->height = f->fmt.pix.height;
883 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
884 pix_format = PIXEL_FRMT_411;
885 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
886 pix_format = PIXEL_FRMT_422;
890 cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
892 /* check if cif resolution */
893 if (fh->width == 320 || fh->width == 352)
894 dev->channels[fh->channel_id].use_cif_resolution = 1;
896 dev->channels[fh->channel_id].use_cif_resolution = 0;
898 dev->channels[fh->channel_id].cif_width = fh->width;
899 medusa_set_resolution(dev, fh->width, SRAM_CH00);
901 dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__, fh->width,
902 fh->height, fh->vidq.field);
903 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
904 cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
909 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
912 struct cx25821_fh *fh = priv;
913 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
915 ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
917 p->sequence = dev->channels[fh->channel_id].vidq.count;
922 static int vidioc_log_status(struct file *file, void *priv)
924 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
925 struct cx25821_fh *fh = priv;
928 struct sram_channel *sram_ch = dev->channels[fh->channel_id]
932 snprintf(name, sizeof(name), "%s/2", dev->name);
933 pr_info("%s/2: ============ START LOG STATUS ============\n",
935 cx25821_call_all(dev, core, log_status);
936 tmp = cx_read(sram_ch->dma_ctl);
937 pr_info("Video input 0 is %s\n",
938 (tmp & 0x11) ? "streaming" : "stopped");
939 pr_info("%s/2: ============= END LOG STATUS =============\n",
944 static int vidioc_s_ctrl(struct file *file, void *priv,
945 struct v4l2_control *ctl)
947 struct cx25821_fh *fh = priv;
948 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
952 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
958 return cx25821_set_control(dev, ctl, fh->channel_id);
962 int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
963 struct v4l2_format *f)
965 struct cx25821_fh *fh = priv;
967 f->fmt.pix.width = fh->width;
968 f->fmt.pix.height = fh->height;
969 f->fmt.pix.field = fh->vidq.field;
970 f->fmt.pix.pixelformat = fh->fmt->fourcc;
971 f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
972 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
977 int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
978 struct v4l2_format *f)
980 struct cx25821_fmt *fmt;
981 enum v4l2_field field;
982 unsigned int maxw, maxh;
984 fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
988 field = f->fmt.pix.field;
992 if (V4L2_FIELD_ANY == field) {
993 if (f->fmt.pix.height > maxh / 2)
994 field = V4L2_FIELD_INTERLACED;
996 field = V4L2_FIELD_TOP;
1000 case V4L2_FIELD_TOP:
1001 case V4L2_FIELD_BOTTOM:
1004 case V4L2_FIELD_INTERLACED:
1010 f->fmt.pix.field = field;
1011 if (f->fmt.pix.height < 32)
1012 f->fmt.pix.height = 32;
1013 if (f->fmt.pix.height > maxh)
1014 f->fmt.pix.height = maxh;
1015 if (f->fmt.pix.width < 48)
1016 f->fmt.pix.width = 48;
1017 if (f->fmt.pix.width > maxw)
1018 f->fmt.pix.width = maxw;
1019 f->fmt.pix.width &= ~0x03;
1020 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
1021 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
1026 int cx25821_vidioc_querycap(struct file *file, void *priv,
1027 struct v4l2_capability *cap)
1029 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1031 strcpy(cap->driver, "cx25821");
1032 strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
1033 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1034 cap->version = CX25821_VERSION_CODE;
1035 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1040 int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1041 struct v4l2_fmtdesc *f)
1043 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1046 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1047 f->pixelformat = formats[f->index].fourcc;
1052 int cx25821_vidioc_reqbufs(struct file *file, void *priv,
1053 struct v4l2_requestbuffers *p)
1055 struct cx25821_fh *fh = priv;
1056 return videobuf_reqbufs(get_queue(fh), p);
1059 int cx25821_vidioc_querybuf(struct file *file, void *priv,
1060 struct v4l2_buffer *p)
1062 struct cx25821_fh *fh = priv;
1063 return videobuf_querybuf(get_queue(fh), p);
1066 int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1068 struct cx25821_fh *fh = priv;
1069 return videobuf_qbuf(get_queue(fh), p);
1072 int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
1074 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1075 struct cx25821_fh *fh = f;
1077 *p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
1082 int cx25821_vidioc_s_priority(struct file *file, void *f,
1083 enum v4l2_priority prio)
1085 struct cx25821_fh *fh = f;
1086 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1088 return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
1092 int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1094 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1096 *tvnorms = dev->tvnorm;
1100 int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
1102 struct cx25821_fh *fh = priv;
1103 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1106 dprintk(1, "%s()\n", __func__);
1109 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1115 if (dev->tvnorm == tvnorms)
1118 mutex_lock(&dev->lock);
1119 cx25821_set_tvnorm(dev, tvnorms);
1120 mutex_unlock(&dev->lock);
1122 medusa_set_videostandard(dev);
1127 int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
1129 static const char * const iname[] = {
1130 [CX25821_VMUX_COMPOSITE] = "Composite",
1131 [CX25821_VMUX_SVIDEO] = "S-Video",
1132 [CX25821_VMUX_DEBUG] = "for debug only",
1135 dprintk(1, "%s()\n", __func__);
1141 if (0 == INPUT(n)->type)
1144 i->type = V4L2_INPUT_TYPE_CAMERA;
1145 strcpy(i->name, iname[INPUT(n)->type]);
1147 i->std = CX25821_NORMS;
1151 int cx25821_vidioc_enum_input(struct file *file, void *priv,
1152 struct v4l2_input *i)
1154 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1155 dprintk(1, "%s()\n", __func__);
1156 return cx25821_enum_input(dev, i);
1159 int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1161 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1164 dprintk(1, "%s(): returns %d\n", __func__, *i);
1168 int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1170 struct cx25821_fh *fh = priv;
1171 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1174 dprintk(1, "%s(%d)\n", __func__, i);
1177 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1183 if (i >= CX25821_NR_INPUT) {
1184 dprintk(1, "%s(): -EINVAL\n", __func__);
1188 mutex_lock(&dev->lock);
1189 cx25821_video_mux(dev, i);
1190 mutex_unlock(&dev->lock);
1194 #ifdef CONFIG_VIDEO_ADV_DEBUG
1195 int cx25821_vidioc_g_register(struct file *file, void *fh,
1196 struct v4l2_dbg_register *reg)
1198 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1200 if (!v4l2_chip_match_host(®->match))
1203 cx25821_call_all(dev, core, g_register, reg);
1208 int cx25821_vidioc_s_register(struct file *file, void *fh,
1209 const struct v4l2_dbg_register *reg)
1211 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1213 if (!v4l2_chip_match_host(®->match))
1216 cx25821_call_all(dev, core, s_register, reg);
1223 /*****************************************************************************/
1224 static const struct v4l2_queryctrl no_ctl = {
1226 .flags = V4L2_CTRL_FLAG_DISABLED,
1229 static struct v4l2_queryctrl cx25821_ctls[] = {
1232 .id = V4L2_CID_BRIGHTNESS,
1233 .name = "Brightness",
1237 .default_value = 6200,
1238 .type = V4L2_CTRL_TYPE_INTEGER,
1240 .id = V4L2_CID_CONTRAST,
1245 .default_value = 5000,
1246 .type = V4L2_CTRL_TYPE_INTEGER,
1248 .id = V4L2_CID_SATURATION,
1249 .name = "Saturation",
1253 .default_value = 5000,
1254 .type = V4L2_CTRL_TYPE_INTEGER,
1261 .default_value = 5000,
1262 .type = V4L2_CTRL_TYPE_INTEGER,
1265 static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1267 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1271 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
1273 for (i = 0; i < CX25821_CTLS; i++)
1274 if (cx25821_ctls[i].id == qctrl->id)
1276 if (i == CX25821_CTLS) {
1280 *qctrl = cx25821_ctls[i];
1284 int cx25821_vidioc_queryctrl(struct file *file, void *priv,
1285 struct v4l2_queryctrl *qctrl)
1287 return cx25821_ctrl_query(qctrl);
1290 /* ------------------------------------------------------------------ */
1291 /* VIDEO CTRL IOCTLS */
1293 static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
1297 for (i = 0; i < CX25821_CTLS; i++)
1298 if (cx25821_ctls[i].id == id)
1299 return cx25821_ctls + i;
1303 int cx25821_vidioc_g_ctrl(struct file *file, void *priv,
1304 struct v4l2_control *ctl)
1306 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1307 struct cx25821_fh *fh = priv;
1309 const struct v4l2_queryctrl *ctrl;
1311 ctrl = ctrl_by_id(ctl->id);
1316 case V4L2_CID_BRIGHTNESS:
1317 ctl->value = dev->channels[fh->channel_id].ctl_bright;
1320 ctl->value = dev->channels[fh->channel_id].ctl_hue;
1322 case V4L2_CID_CONTRAST:
1323 ctl->value = dev->channels[fh->channel_id].ctl_contrast;
1325 case V4L2_CID_SATURATION:
1326 ctl->value = dev->channels[fh->channel_id].ctl_saturation;
1332 int cx25821_set_control(struct cx25821_dev *dev,
1333 struct v4l2_control *ctl, int chan_num)
1336 const struct v4l2_queryctrl *ctrl;
1340 ctrl = ctrl_by_id(ctl->id);
1345 switch (ctrl->type) {
1346 case V4L2_CTRL_TYPE_BOOLEAN:
1347 case V4L2_CTRL_TYPE_MENU:
1348 case V4L2_CTRL_TYPE_INTEGER:
1349 if (ctl->value < ctrl->minimum)
1350 ctl->value = ctrl->minimum;
1351 if (ctl->value > ctrl->maximum)
1352 ctl->value = ctrl->maximum;
1359 case V4L2_CID_BRIGHTNESS:
1360 dev->channels[chan_num].ctl_bright = ctl->value;
1361 medusa_set_brightness(dev, ctl->value, chan_num);
1364 dev->channels[chan_num].ctl_hue = ctl->value;
1365 medusa_set_hue(dev, ctl->value, chan_num);
1367 case V4L2_CID_CONTRAST:
1368 dev->channels[chan_num].ctl_contrast = ctl->value;
1369 medusa_set_contrast(dev, ctl->value, chan_num);
1371 case V4L2_CID_SATURATION:
1372 dev->channels[chan_num].ctl_saturation = ctl->value;
1373 medusa_set_saturation(dev, ctl->value, chan_num);
1382 static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num)
1384 struct v4l2_control ctrl;
1386 for (i = 0; i < CX25821_CTLS; i++) {
1387 ctrl.id = cx25821_ctls[i].id;
1388 ctrl.value = cx25821_ctls[i].default_value;
1390 cx25821_set_control(dev, &ctrl, chan_num);
1394 int cx25821_vidioc_cropcap(struct file *file, void *priv,
1395 struct v4l2_cropcap *cropcap)
1397 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1399 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1401 cropcap->bounds.top = 0;
1402 cropcap->bounds.left = 0;
1403 cropcap->bounds.width = 720;
1404 cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1405 cropcap->pixelaspect.numerator =
1406 dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1407 cropcap->pixelaspect.denominator =
1408 dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1409 cropcap->defrect = cropcap->bounds;
1413 int cx25821_vidioc_s_crop(struct file *file, void *priv, const struct v4l2_crop *crop)
1415 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1416 struct cx25821_fh *fh = priv;
1420 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1425 /* cx25821_vidioc_s_crop not supported */
1429 int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1431 /* cx25821_vidioc_g_crop not supported */
1435 int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
1437 if (tvnorm == V4L2_STD_PAL_BG) {
1438 if (width == 352 || width == 720)
1444 if (tvnorm == V4L2_STD_NTSC_M) {
1445 if (width == 320 || width == 352 || width == 720)
1453 int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
1455 if (tvnorm == V4L2_STD_PAL_BG) {
1456 if (height == 576 || height == 288)
1462 if (tvnorm == V4L2_STD_NTSC_M) {
1463 if (height == 480 || height == 240)
1472 static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
1475 struct cx25821_fh *fh = file->private_data;
1476 struct cx25821_dev *dev = fh->dev;
1478 struct upstream_user_struct *data_from_user;
1480 data_from_user = (struct upstream_user_struct *)arg;
1482 if (!data_from_user) {
1483 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1487 command = data_from_user->command;
1489 if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1492 dev->input_filename = data_from_user->input_filename;
1493 dev->input_audiofilename = data_from_user->input_filename;
1494 dev->vid_stdname = data_from_user->vid_stdname;
1495 dev->pixel_format = data_from_user->pixel_format;
1496 dev->channel_select = data_from_user->channel_select;
1497 dev->command = data_from_user->command;
1500 case UPSTREAM_START_VIDEO:
1501 cx25821_start_upstream_video_ch1(dev, data_from_user);
1504 case UPSTREAM_STOP_VIDEO:
1505 cx25821_stop_upstream_video_ch1(dev);
1512 static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
1515 struct cx25821_fh *fh = file->private_data;
1516 struct cx25821_dev *dev = fh->dev;
1518 struct upstream_user_struct *data_from_user;
1520 data_from_user = (struct upstream_user_struct *)arg;
1522 if (!data_from_user) {
1523 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1527 command = data_from_user->command;
1529 if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1532 dev->input_filename_ch2 = data_from_user->input_filename;
1533 dev->input_audiofilename = data_from_user->input_filename;
1534 dev->vid_stdname_ch2 = data_from_user->vid_stdname;
1535 dev->pixel_format_ch2 = data_from_user->pixel_format;
1536 dev->channel_select_ch2 = data_from_user->channel_select;
1537 dev->command_ch2 = data_from_user->command;
1540 case UPSTREAM_START_VIDEO:
1541 cx25821_start_upstream_video_ch2(dev, data_from_user);
1544 case UPSTREAM_STOP_VIDEO:
1545 cx25821_stop_upstream_video_ch2(dev);
1552 static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
1555 struct cx25821_fh *fh = file->private_data;
1556 struct cx25821_dev *dev = fh->dev;
1558 struct upstream_user_struct *data_from_user;
1560 data_from_user = (struct upstream_user_struct *)arg;
1562 if (!data_from_user) {
1563 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1567 command = data_from_user->command;
1569 if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
1572 dev->input_filename = data_from_user->input_filename;
1573 dev->input_audiofilename = data_from_user->input_filename;
1574 dev->vid_stdname = data_from_user->vid_stdname;
1575 dev->pixel_format = data_from_user->pixel_format;
1576 dev->channel_select = data_from_user->channel_select;
1577 dev->command = data_from_user->command;
1580 case UPSTREAM_START_AUDIO:
1581 cx25821_start_upstream_audio(dev, data_from_user);
1584 case UPSTREAM_STOP_AUDIO:
1585 cx25821_stop_upstream_audio(dev);
1592 static long video_ioctl_set(struct file *file, unsigned int cmd,
1595 struct cx25821_fh *fh = file->private_data;
1596 struct cx25821_dev *dev = fh->dev;
1597 struct downstream_user_struct *data_from_user;
1600 int selected_channel = 0;
1606 data_from_user = (struct downstream_user_struct *)arg;
1608 if (!data_from_user) {
1609 pr_err("%s(): User data is INVALID. Returning\n", __func__);
1613 command = data_from_user->command;
1615 if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
1616 && command != ENABLE_CIF_RESOLUTION && command != REG_READ
1617 && command != REG_WRITE && command != MEDUSA_READ
1618 && command != MEDUSA_WRITE) {
1624 if (!strcmp(data_from_user->vid_stdname, "PAL"))
1625 dev->tvnorm = V4L2_STD_PAL_BG;
1627 dev->tvnorm = V4L2_STD_NTSC_M;
1628 medusa_set_videostandard(dev);
1631 case SET_PIXEL_FORMAT:
1632 selected_channel = data_from_user->decoder_select;
1633 pix_format = data_from_user->pixel_format;
1635 if (!(selected_channel <= 7 && selected_channel >= 0)) {
1636 selected_channel -= 4;
1637 selected_channel = selected_channel % 8;
1640 if (selected_channel >= 0)
1641 cx25821_set_pixel_format(dev, selected_channel,
1646 case ENABLE_CIF_RESOLUTION:
1647 selected_channel = data_from_user->decoder_select;
1648 cif_enable = data_from_user->cif_resolution_enable;
1649 cif_width = data_from_user->cif_width;
1652 if (dev->tvnorm & V4L2_STD_PAL_BG
1653 || dev->tvnorm & V4L2_STD_PAL_DK) {
1657 if (cif_width != 320 && cif_width != 352)
1662 if (!(selected_channel <= 7 && selected_channel >= 0)) {
1663 selected_channel -= 4;
1664 selected_channel = selected_channel % 8;
1667 if (selected_channel <= 7 && selected_channel >= 0) {
1668 dev->channels[selected_channel].use_cif_resolution =
1670 dev->channels[selected_channel].cif_width = width;
1672 for (i = 0; i < VID_CHANNEL_NUM; i++) {
1673 dev->channels[i].use_cif_resolution =
1675 dev->channels[i].cif_width = width;
1679 medusa_set_resolution(dev, width, selected_channel);
1682 data_from_user->reg_data = cx_read(data_from_user->reg_address);
1685 cx_write(data_from_user->reg_address, data_from_user->reg_data);
1688 cx25821_i2c_read(&dev->i2c_bus[0],
1689 (u16) data_from_user->reg_address,
1690 &data_from_user->reg_data);
1693 cx25821_i2c_write(&dev->i2c_bus[0],
1694 (u16) data_from_user->reg_address,
1695 data_from_user->reg_data);
1702 static long cx25821_video_ioctl(struct file *file,
1703 unsigned int cmd, unsigned long arg)
1707 struct cx25821_fh *fh = file->private_data;
1709 /* check to see if it's the video upstream */
1710 if (fh->channel_id == SRAM_CH09) {
1711 ret = video_ioctl_upstream9(file, cmd, arg);
1713 } else if (fh->channel_id == SRAM_CH10) {
1714 ret = video_ioctl_upstream10(file, cmd, arg);
1716 } else if (fh->channel_id == SRAM_CH11) {
1717 ret = video_ioctl_upstream11(file, cmd, arg);
1718 ret = video_ioctl_set(file, cmd, arg);
1722 return video_ioctl2(file, cmd, arg);
1725 /* exported stuff */
1726 static const struct v4l2_file_operations video_fops = {
1727 .owner = THIS_MODULE,
1729 .release = video_release,
1732 .mmap = cx25821_video_mmap,
1733 .ioctl = cx25821_video_ioctl,
1736 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1737 .vidioc_querycap = cx25821_vidioc_querycap,
1738 .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
1739 .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
1740 .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
1741 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1742 .vidioc_reqbufs = cx25821_vidioc_reqbufs,
1743 .vidioc_querybuf = cx25821_vidioc_querybuf,
1744 .vidioc_qbuf = cx25821_vidioc_qbuf,
1745 .vidioc_dqbuf = vidioc_dqbuf,
1746 .vidioc_g_std = cx25821_vidioc_g_std,
1747 .vidioc_s_std = cx25821_vidioc_s_std,
1748 .vidioc_cropcap = cx25821_vidioc_cropcap,
1749 .vidioc_s_crop = cx25821_vidioc_s_crop,
1750 .vidioc_g_crop = cx25821_vidioc_g_crop,
1751 .vidioc_enum_input = cx25821_vidioc_enum_input,
1752 .vidioc_g_input = cx25821_vidioc_g_input,
1753 .vidioc_s_input = cx25821_vidioc_s_input,
1754 .vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
1755 .vidioc_s_ctrl = vidioc_s_ctrl,
1756 .vidioc_queryctrl = cx25821_vidioc_queryctrl,
1757 .vidioc_streamon = vidioc_streamon,
1758 .vidioc_streamoff = vidioc_streamoff,
1759 .vidioc_log_status = vidioc_log_status,
1760 .vidioc_g_priority = cx25821_vidioc_g_priority,
1761 .vidioc_s_priority = cx25821_vidioc_s_priority,
1762 #ifdef CONFIG_VIDEO_ADV_DEBUG
1763 .vidioc_g_register = cx25821_vidioc_g_register,
1764 .vidioc_s_register = cx25821_vidioc_s_register,
1768 static const struct video_device cx25821_video_device = {
1769 .name = "cx25821-video",
1770 .fops = &video_fops,
1772 .ioctl_ops = &video_ioctl_ops,
1773 .tvnorms = CX25821_NORMS,
1776 void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
1778 cx_clear(PCI_INT_MSK, 1);
1780 if (dev->channels[chan_num].video_dev) {
1781 if (video_is_registered(dev->channels[chan_num].video_dev))
1782 video_unregister_device(
1783 dev->channels[chan_num].video_dev);
1785 video_device_release(
1786 dev->channels[chan_num].video_dev);
1788 dev->channels[chan_num].video_dev = NULL;
1790 btcx_riscmem_free(dev->pci,
1791 &dev->channels[chan_num].vidq.stopper);
1793 pr_warn("device %d released!\n", chan_num);
1798 int cx25821_video_register(struct cx25821_dev *dev)
1803 spin_lock_init(&dev->slock);
1805 for (i = 0; i < VID_CHANNEL_NUM; ++i) {
1806 if (i == SRAM_CH08) /* audio channel */
1809 cx25821_init_controls(dev, i);
1811 cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
1812 dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
1814 dev->channels[i].sram_channels = &cx25821_sram_channels[i];
1815 dev->channels[i].video_dev = NULL;
1816 dev->channels[i].resources = 0;
1818 cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
1820 INIT_LIST_HEAD(&dev->channels[i].vidq.active);
1821 INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
1823 dev->channels[i].timeout_data.dev = dev;
1824 dev->channels[i].timeout_data.channel =
1825 &cx25821_sram_channels[i];
1826 dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
1827 dev->channels[i].vidq.timeout.data =
1828 (unsigned long)&dev->channels[i].timeout_data;
1829 init_timer(&dev->channels[i].vidq.timeout);
1831 /* register v4l devices */
1832 dev->channels[i].video_dev = cx25821_vdev_init(dev, dev->pci,
1833 &cx25821_video_device, "video");
1835 err = video_register_device(dev->channels[i].video_dev,
1836 VFL_TYPE_GRABBER, video_nr[dev->nr]);
1843 /* set PCI interrupt */
1844 cx_set(PCI_INT_MSK, 0xff);
1846 /* initial device configuration */
1847 mutex_lock(&dev->lock);
1848 dev->tvnorm = V4L2_STD_NTSC_M,
1849 cx25821_set_tvnorm(dev, dev->tvnorm);
1850 mutex_unlock(&dev->lock);
1855 cx25821_video_unregister(dev, i);