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 };
36 static unsigned int radio_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
38 module_param_array(video_nr, int, NULL, 0444);
39 module_param_array(radio_nr, int, NULL, 0444);
41 MODULE_PARM_DESC(video_nr, "video device numbers");
42 MODULE_PARM_DESC(radio_nr, "radio device numbers");
44 static unsigned int video_debug = VIDEO_DEBUG;
45 module_param(video_debug, int, 0644);
46 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
48 static unsigned int irq_debug;
49 module_param(irq_debug, int, 0644);
50 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
52 unsigned int vid_limit = 16;
53 module_param(vid_limit, int, 0644);
54 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
56 static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num);
58 static const struct v4l2_file_operations video_fops;
59 static const struct v4l2_ioctl_ops video_ioctl_ops;
61 #define FORMAT_FLAGS_PACKED 0x01
63 struct cx25821_fmt formats[] = {
65 .name = "8 bpp, gray",
66 .fourcc = V4L2_PIX_FMT_GREY,
68 .flags = FORMAT_FLAGS_PACKED,
70 .name = "4:1:1, packed, Y41P",
71 .fourcc = V4L2_PIX_FMT_Y41P,
73 .flags = FORMAT_FLAGS_PACKED,
75 .name = "4:2:2, packed, YUYV",
76 .fourcc = V4L2_PIX_FMT_YUYV,
78 .flags = FORMAT_FLAGS_PACKED,
80 .name = "4:2:2, packed, UYVY",
81 .fourcc = V4L2_PIX_FMT_UYVY,
83 .flags = FORMAT_FLAGS_PACKED,
86 .fourcc = V4L2_PIX_FMT_YUV420,
88 .flags = FORMAT_FLAGS_PACKED,
92 int cx25821_get_format_size(void)
94 return ARRAY_SIZE(formats);
97 struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
101 if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P)
104 for (i = 0; i < ARRAY_SIZE(formats); i++)
105 if (formats[i].fourcc == fourcc)
108 pr_err("%s(0x%08x) NOT FOUND\n", __func__, fourcc);
112 void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
115 struct cx25821_buffer *buf;
118 for (bc = 0;; bc++) {
119 if (list_empty(&q->active)) {
120 dprintk(1, "bc=%d (=0: active empty)\n", bc);
124 buf = list_entry(q->active.next, struct cx25821_buffer,
127 /* count comes from the hw and it is 16bit wide --
128 * this trick handles wrap-arounds correctly for
129 * up to 32767 buffers in flight... */
130 if ((s16) (count - buf->count) < 0)
133 v4l2_get_timestamp(&buf->vb.ts);
134 buf->vb.state = VIDEOBUF_DONE;
135 list_del(&buf->vb.queue);
136 wake_up(&buf->vb.done);
139 if (list_empty(&q->active))
140 del_timer(&q->timeout);
142 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
144 pr_err("%s: %d buffers handled (should be 1)\n", __func__, bc);
148 int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
150 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
151 __func__, (unsigned int)norm, v4l2_norm_to_name(norm));
155 /* Tell the internal A/V decoder */
156 cx25821_call_all(dev, core, s_std, norm);
162 struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
164 struct video_device *template,
167 struct video_device *vfd;
168 dprintk(1, "%s()\n", __func__);
170 vfd = video_device_alloc();
174 vfd->v4l2_dev = &dev->v4l2_dev;
175 vfd->release = video_device_release;
176 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, type,
177 cx25821_boards[dev->board].name);
178 video_set_drvdata(vfd, dev);
183 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
187 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
189 for (i = 0; i < CX25821_CTLS; i++)
190 if (cx25821_ctls[i].v.id == qctrl->id)
192 if (i == CX25821_CTLS) {
196 *qctrl = cx25821_ctls[i].v;
201 /* resource management */
202 int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh,
205 dprintk(1, "%s()\n", __func__);
206 if (fh->resources & bit)
207 /* have it already allocated */
211 mutex_lock(&dev->lock);
212 if (dev->channels[fh->channel_id].resources & bit) {
213 /* no, someone else uses it */
214 mutex_unlock(&dev->lock);
217 /* it's free, grab it */
218 fh->resources |= bit;
219 dev->channels[fh->channel_id].resources |= bit;
220 dprintk(1, "res: get %d\n", bit);
221 mutex_unlock(&dev->lock);
225 int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
227 return fh->resources & bit;
230 int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
232 return fh->dev->channels[fh->channel_id].resources & bit;
235 void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh,
238 BUG_ON((fh->resources & bits) != bits);
239 dprintk(1, "%s()\n", __func__);
241 mutex_lock(&dev->lock);
242 fh->resources &= ~bits;
243 dev->channels[fh->channel_id].resources &= ~bits;
244 dprintk(1, "res: put %d\n", bits);
245 mutex_unlock(&dev->lock);
248 int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
250 struct v4l2_routing route;
251 memset(&route, 0, sizeof(route));
253 dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
254 __func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
255 INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
258 route.input = INPUT(input)->vmux;
260 /* Tell the internal A/V decoder */
261 cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
266 int cx25821_start_video_dma(struct cx25821_dev *dev,
267 struct cx25821_dmaqueue *q,
268 struct cx25821_buffer *buf,
269 struct sram_channel *channel)
273 /* setup fifo + format */
274 cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
277 cx_write(channel->gpcnt_ctl, 3);
281 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
282 cx_set(channel->int_msk, 0x11);
285 cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
287 /* make sure upstream setting if any is reversed */
288 tmp = cx_read(VID_CH_MODE_SEL);
289 cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
294 static int cx25821_restart_video_queue(struct cx25821_dev *dev,
295 struct cx25821_dmaqueue *q,
296 struct sram_channel *channel)
298 struct cx25821_buffer *buf, *prev;
299 struct list_head *item;
301 if (!list_empty(&q->active)) {
302 buf = list_entry(q->active.next, struct cx25821_buffer,
305 cx25821_start_video_dma(dev, q, buf, channel);
307 list_for_each(item, &q->active) {
308 buf = list_entry(item, struct cx25821_buffer, vb.queue);
309 buf->count = q->count++;
312 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
318 if (list_empty(&q->queued))
321 buf = list_entry(q->queued.next, struct cx25821_buffer,
325 list_move_tail(&buf->vb.queue, &q->active);
326 cx25821_start_video_dma(dev, q, buf, channel);
327 buf->vb.state = VIDEOBUF_ACTIVE;
328 buf->count = q->count++;
329 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
330 } else if (prev->vb.width == buf->vb.width &&
331 prev->vb.height == buf->vb.height &&
332 prev->fmt == buf->fmt) {
333 list_move_tail(&buf->vb.queue, &q->active);
334 buf->vb.state = VIDEOBUF_ACTIVE;
335 buf->count = q->count++;
336 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
337 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
345 static void cx25821_vid_timeout(unsigned long data)
347 struct cx25821_data *timeout_data = (struct cx25821_data *)data;
348 struct cx25821_dev *dev = timeout_data->dev;
349 struct sram_channel *channel = timeout_data->channel;
350 struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq;
351 struct cx25821_buffer *buf;
354 /* cx25821_sram_channel_dump(dev, channel); */
355 cx_clear(channel->dma_ctl, 0x11);
357 spin_lock_irqsave(&dev->slock, flags);
358 while (!list_empty(&q->active)) {
359 buf = list_entry(q->active.next, struct cx25821_buffer,
361 list_del(&buf->vb.queue);
363 buf->vb.state = VIDEOBUF_ERROR;
364 wake_up(&buf->vb.done);
367 cx25821_restart_video_queue(dev, q, channel);
368 spin_unlock_irqrestore(&dev->slock, flags);
371 int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
376 struct sram_channel *channel = dev->channels[chan_num].sram_channels;
378 mask = cx_read(channel->int_msk);
379 if (0 == (status & mask))
382 cx_write(channel->int_stat, status);
384 /* risc op code error */
385 if (status & (1 << 16)) {
386 pr_warn("%s, %s: video risc op code error\n",
387 dev->name, channel->name);
388 cx_clear(channel->dma_ctl, 0x11);
389 cx25821_sram_channel_dump(dev, channel);
393 if (status & FLD_VID_DST_RISC1) {
394 spin_lock(&dev->slock);
395 count = cx_read(channel->gpcnt);
396 cx25821_video_wakeup(dev, &dev->channels[channel->i].vidq,
398 spin_unlock(&dev->slock);
404 dprintk(2, "stopper video\n");
405 spin_lock(&dev->slock);
406 cx25821_restart_video_queue(dev,
407 &dev->channels[channel->i].vidq, channel);
408 spin_unlock(&dev->slock);
414 void cx25821_videoioctl_unregister(struct cx25821_dev *dev)
416 if (dev->ioctl_dev) {
417 if (video_is_registered(dev->ioctl_dev))
418 video_unregister_device(dev->ioctl_dev);
420 video_device_release(dev->ioctl_dev);
422 dev->ioctl_dev = NULL;
426 void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
428 cx_clear(PCI_INT_MSK, 1);
430 if (dev->channels[chan_num].video_dev) {
431 if (video_is_registered(dev->channels[chan_num].video_dev))
432 video_unregister_device(
433 dev->channels[chan_num].video_dev);
435 video_device_release(
436 dev->channels[chan_num].video_dev);
438 dev->channels[chan_num].video_dev = NULL;
440 btcx_riscmem_free(dev->pci,
441 &dev->channels[chan_num].vidq.stopper);
443 pr_warn("device %d released!\n", chan_num);
448 int cx25821_video_register(struct cx25821_dev *dev)
453 struct video_device cx25821_video_device = {
454 .name = "cx25821-video",
457 .ioctl_ops = &video_ioctl_ops,
458 .tvnorms = CX25821_NORMS,
459 .current_norm = V4L2_STD_NTSC_M,
462 spin_lock_init(&dev->slock);
464 for (i = 0; i < VID_CHANNEL_NUM; ++i) {
465 cx25821_init_controls(dev, i);
467 cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
468 dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
470 dev->channels[i].sram_channels = &cx25821_sram_channels[i];
471 dev->channels[i].video_dev = NULL;
472 dev->channels[i].resources = 0;
474 cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
476 INIT_LIST_HEAD(&dev->channels[i].vidq.active);
477 INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
479 dev->channels[i].timeout_data.dev = dev;
480 dev->channels[i].timeout_data.channel =
481 &cx25821_sram_channels[i];
482 dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
483 dev->channels[i].vidq.timeout.data =
484 (unsigned long)&dev->channels[i].timeout_data;
485 init_timer(&dev->channels[i].vidq.timeout);
487 /* register v4l devices */
488 dev->channels[i].video_dev = cx25821_vdev_init(dev, dev->pci,
489 &cx25821_video_device, "video");
491 err = video_register_device(dev->channels[i].video_dev,
492 VFL_TYPE_GRABBER, video_nr[dev->nr]);
499 /* set PCI interrupt */
500 cx_set(PCI_INT_MSK, 0xff);
502 /* initial device configuration */
503 mutex_lock(&dev->lock);
505 dev->tvnorm = cx25821_video_device.current_norm;
506 cx25821_set_tvnorm(dev, dev->tvnorm);
508 mutex_unlock(&dev->lock);
513 cx25821_video_unregister(dev, i);
517 int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
520 struct cx25821_fh *fh = q->priv_data;
522 *size = fh->fmt->depth * fh->width * fh->height >> 3;
527 if (*size * *count > vid_limit * 1024 * 1024)
528 *count = (vid_limit * 1024 * 1024) / *size;
533 int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
534 enum v4l2_field field)
536 struct cx25821_fh *fh = q->priv_data;
537 struct cx25821_dev *dev = fh->dev;
538 struct cx25821_buffer *buf =
539 container_of(vb, struct cx25821_buffer, vb);
540 int rc, init_buffer = 0;
542 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
543 int bpl_local = LINE_SIZE_D1;
544 int channel_opened = fh->channel_id;
546 BUG_ON(NULL == fh->fmt);
547 if (fh->width < 48 || fh->width > 720 ||
548 fh->height < 32 || fh->height > 576)
551 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
553 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
556 if (buf->fmt != fh->fmt ||
557 buf->vb.width != fh->width ||
558 buf->vb.height != fh->height || buf->vb.field != field) {
560 buf->vb.width = fh->width;
561 buf->vb.height = fh->height;
562 buf->vb.field = field;
566 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
568 rc = videobuf_iolock(q, &buf->vb, NULL);
570 printk(KERN_DEBUG pr_fmt("videobuf_iolock failed!\n"));
575 dprintk(1, "init_buffer=%d\n", init_buffer);
579 channel_opened = dev->channel_opened;
580 if (channel_opened < 0 || channel_opened > 7)
583 if (dev->channels[channel_opened].pixel_formats ==
585 buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
587 buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
589 if (dev->channels[channel_opened].pixel_formats ==
591 bpl_local = buf->bpl;
593 bpl_local = buf->bpl; /* Default */
595 if (channel_opened >= 0 && channel_opened <= 7) {
596 if (dev->channels[channel_opened]
597 .use_cif_resolution) {
598 if (dev->tvnorm & V4L2_STD_PAL_BG ||
599 dev->tvnorm & V4L2_STD_PAL_DK)
600 bpl_local = 352 << 1;
602 bpl_local = dev->channels[
609 switch (buf->vb.field) {
611 cx25821_risc_buffer(dev->pci, &buf->risc,
612 dma->sglist, 0, UNSET,
613 buf->bpl, 0, buf->vb.height);
615 case V4L2_FIELD_BOTTOM:
616 cx25821_risc_buffer(dev->pci, &buf->risc,
617 dma->sglist, UNSET, 0,
618 buf->bpl, 0, buf->vb.height);
620 case V4L2_FIELD_INTERLACED:
621 /* All other formats are top field first */
623 dprintk(1, "top field first\n");
625 cx25821_risc_buffer(dev->pci, &buf->risc,
626 dma->sglist, line0_offset,
627 bpl_local, bpl_local, bpl_local,
628 buf->vb.height >> 1);
630 case V4L2_FIELD_SEQ_TB:
631 cx25821_risc_buffer(dev->pci, &buf->risc,
633 0, buf->bpl * (buf->vb.height >> 1),
634 buf->bpl, 0, buf->vb.height >> 1);
636 case V4L2_FIELD_SEQ_BT:
637 cx25821_risc_buffer(dev->pci, &buf->risc,
639 buf->bpl * (buf->vb.height >> 1), 0,
640 buf->bpl, 0, buf->vb.height >> 1);
647 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
648 buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
649 fh->fmt->name, (unsigned long)buf->risc.dma);
651 buf->vb.state = VIDEOBUF_PREPARED;
656 cx25821_free_buffer(q, buf);
660 void cx25821_buffer_release(struct videobuf_queue *q,
661 struct videobuf_buffer *vb)
663 struct cx25821_buffer *buf =
664 container_of(vb, struct cx25821_buffer, vb);
666 cx25821_free_buffer(q, buf);
669 struct videobuf_queue *get_queue(struct cx25821_fh *fh)
672 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
680 int cx25821_get_resource(struct cx25821_fh *fh, int resource)
683 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
691 int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
693 struct cx25821_fh *fh = file->private_data;
695 return videobuf_mmap_mapper(get_queue(fh), vma);
699 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
701 struct cx25821_buffer *buf =
702 container_of(vb, struct cx25821_buffer, vb);
703 struct cx25821_buffer *prev;
704 struct cx25821_fh *fh = vq->priv_data;
705 struct cx25821_dev *dev = fh->dev;
706 struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq;
708 /* add jump to stopper */
709 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
710 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
711 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
713 dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
715 if (!list_empty(&q->queued)) {
716 list_add_tail(&buf->vb.queue, &q->queued);
717 buf->vb.state = VIDEOBUF_QUEUED;
718 dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
721 } else if (list_empty(&q->active)) {
722 list_add_tail(&buf->vb.queue, &q->active);
723 cx25821_start_video_dma(dev, q, buf,
724 dev->channels[fh->channel_id].sram_channels);
725 buf->vb.state = VIDEOBUF_ACTIVE;
726 buf->count = q->count++;
727 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
728 dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
729 buf, buf->vb.i, buf->count, q->count);
731 prev = list_entry(q->active.prev, struct cx25821_buffer,
733 if (prev->vb.width == buf->vb.width
734 && prev->vb.height == buf->vb.height
735 && prev->fmt == buf->fmt) {
736 list_add_tail(&buf->vb.queue, &q->active);
737 buf->vb.state = VIDEOBUF_ACTIVE;
738 buf->count = q->count++;
739 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
741 /* 64 bit bits 63-32 */
742 prev->risc.jmp[2] = cpu_to_le32(0);
743 dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
744 buf, buf->vb.i, buf->count);
747 list_add_tail(&buf->vb.queue, &q->queued);
748 buf->vb.state = VIDEOBUF_QUEUED;
749 dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
754 if (list_empty(&q->active))
755 dprintk(2, "active queue empty!\n");
758 static struct videobuf_queue_ops cx25821_video_qops = {
759 .buf_setup = cx25821_buffer_setup,
760 .buf_prepare = cx25821_buffer_prepare,
761 .buf_queue = buffer_queue,
762 .buf_release = cx25821_buffer_release,
765 static int video_open(struct file *file)
767 struct video_device *vdev = video_devdata(file);
768 struct cx25821_dev *h, *dev = video_drvdata(file);
769 struct cx25821_fh *fh;
770 struct list_head *list;
771 int minor = video_devdata(file)->minor;
772 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
777 dprintk(1, "open dev=%s type=%s\n", video_device_node_name(vdev),
778 v4l2_type_names[type]);
780 /* allocate + initialize per filehandle data */
781 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
785 mutex_lock(&cx25821_devlist_mutex);
787 list_for_each(list, &cx25821_devlist)
789 h = list_entry(list, struct cx25821_dev, devlist);
791 for (i = 0; i < MAX_VID_CHANNEL_NUM; i++) {
792 if (h->channels[i].video_dev &&
793 h->channels[i].video_dev->minor == minor) {
796 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
802 mutex_unlock(&cx25821_devlist_mutex);
807 file->private_data = fh;
811 fh->channel_id = ch_id;
813 if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
818 dev->channel_opened = fh->channel_id;
819 if (dev->channels[ch_id].pixel_formats == PIXEL_FRMT_411)
820 pix_format = V4L2_PIX_FMT_Y41P;
822 pix_format = V4L2_PIX_FMT_YUYV;
823 fh->fmt = cx25821_format_by_fourcc(pix_format);
825 v4l2_prio_open(&dev->channels[ch_id].prio, &fh->prio);
827 videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops, &dev->pci->dev,
828 &dev->slock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
829 V4L2_FIELD_INTERLACED, sizeof(struct cx25821_buffer),
832 dprintk(1, "post videobuf_queue_init()\n");
833 mutex_unlock(&cx25821_devlist_mutex);
838 static ssize_t video_read(struct file *file, char __user * data, size_t count,
841 struct cx25821_fh *fh = file->private_data;
844 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
845 if (cx25821_res_locked(fh, RESOURCE_VIDEO0))
848 return videobuf_read_one(&fh->vidq, data, count, ppos,
849 file->f_flags & O_NONBLOCK);
857 static unsigned int video_poll(struct file *file,
858 struct poll_table_struct *wait)
860 struct cx25821_fh *fh = file->private_data;
861 struct cx25821_buffer *buf;
863 if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
864 /* streaming capture */
865 if (list_empty(&fh->vidq.stream))
867 buf = list_entry(fh->vidq.stream.next,
868 struct cx25821_buffer, vb.stream);
871 buf = (struct cx25821_buffer *)fh->vidq.read_buf;
876 poll_wait(file, &buf->vb.done, wait);
877 if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
878 if (buf->vb.state == VIDEOBUF_DONE) {
879 struct cx25821_dev *dev = fh->dev;
881 if (dev && dev->channels[fh->channel_id]
882 .use_cif_resolution) {
883 u8 cam_id = *((char *)buf->vb.baddr + 3);
884 memcpy((char *)buf->vb.baddr,
885 (char *)buf->vb.baddr + (fh->width * 2),
887 *((char *)buf->vb.baddr + 3) = cam_id;
891 return POLLIN | POLLRDNORM;
897 static int video_release(struct file *file)
899 struct cx25821_fh *fh = file->private_data;
900 struct cx25821_dev *dev = fh->dev;
902 /* stop the risc engine and fifo */
903 cx_write(channel0->dma_ctl, 0); /* FIFO and RISC disable */
905 /* stop video capture */
906 if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
907 videobuf_queue_cancel(&fh->vidq);
908 cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
911 if (fh->vidq.read_buf) {
912 cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
913 kfree(fh->vidq.read_buf);
916 videobuf_mmap_free(&fh->vidq);
918 v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
919 file->private_data = NULL;
925 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
927 struct cx25821_fh *fh = priv;
928 struct cx25821_dev *dev = fh->dev;
930 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
933 if (unlikely(i != fh->type))
936 if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh,
940 return videobuf_streamon(get_queue(fh));
943 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
945 struct cx25821_fh *fh = priv;
946 struct cx25821_dev *dev = fh->dev;
949 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
954 res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
955 err = videobuf_streamoff(get_queue(fh));
958 cx25821_res_free(dev, fh, res);
962 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
963 struct v4l2_format *f)
965 struct cx25821_fh *fh = priv;
966 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
967 struct v4l2_mbus_framefmt mbus_fmt;
969 int pix_format = PIXEL_FRMT_422;
972 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
978 dprintk(2, "%s()\n", __func__);
979 err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
984 fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
985 fh->vidq.field = f->fmt.pix.field;
987 /* check if width and height is valid based on set standard */
988 if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
989 fh->width = f->fmt.pix.width;
991 if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
992 fh->height = f->fmt.pix.height;
994 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
995 pix_format = PIXEL_FRMT_411;
996 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
997 pix_format = PIXEL_FRMT_422;
1001 cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
1003 /* check if cif resolution */
1004 if (fh->width == 320 || fh->width == 352)
1005 dev->channels[fh->channel_id].use_cif_resolution = 1;
1007 dev->channels[fh->channel_id].use_cif_resolution = 0;
1009 dev->channels[fh->channel_id].cif_width = fh->width;
1010 medusa_set_resolution(dev, fh->width, SRAM_CH00);
1012 dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__, fh->width,
1013 fh->height, fh->vidq.field);
1014 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1015 cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1020 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1023 struct cx25821_fh *fh = priv;
1024 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1026 ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
1028 p->sequence = dev->channels[fh->channel_id].vidq.count;
1033 static int vidioc_log_status(struct file *file, void *priv)
1035 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1036 struct cx25821_fh *fh = priv;
1039 struct sram_channel *sram_ch = dev->channels[fh->channel_id]
1043 snprintf(name, sizeof(name), "%s/2", dev->name);
1044 pr_info("%s/2: ============ START LOG STATUS ============\n",
1046 cx25821_call_all(dev, core, log_status);
1047 tmp = cx_read(sram_ch->dma_ctl);
1048 pr_info("Video input 0 is %s\n",
1049 (tmp & 0x11) ? "streaming" : "stopped");
1050 pr_info("%s/2: ============= END LOG STATUS =============\n",
1055 static int vidioc_s_ctrl(struct file *file, void *priv,
1056 struct v4l2_control *ctl)
1058 struct cx25821_fh *fh = priv;
1059 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1063 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1069 return cx25821_set_control(dev, ctl, fh->channel_id);
1073 int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1074 struct v4l2_format *f)
1076 struct cx25821_fh *fh = priv;
1078 f->fmt.pix.width = fh->width;
1079 f->fmt.pix.height = fh->height;
1080 f->fmt.pix.field = fh->vidq.field;
1081 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1082 f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
1083 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
1088 int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1089 struct v4l2_format *f)
1091 struct cx25821_fmt *fmt;
1092 enum v4l2_field field;
1093 unsigned int maxw, maxh;
1095 fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
1099 field = f->fmt.pix.field;
1103 if (V4L2_FIELD_ANY == field) {
1104 if (f->fmt.pix.height > maxh / 2)
1105 field = V4L2_FIELD_INTERLACED;
1107 field = V4L2_FIELD_TOP;
1111 case V4L2_FIELD_TOP:
1112 case V4L2_FIELD_BOTTOM:
1115 case V4L2_FIELD_INTERLACED:
1121 f->fmt.pix.field = field;
1122 if (f->fmt.pix.height < 32)
1123 f->fmt.pix.height = 32;
1124 if (f->fmt.pix.height > maxh)
1125 f->fmt.pix.height = maxh;
1126 if (f->fmt.pix.width < 48)
1127 f->fmt.pix.width = 48;
1128 if (f->fmt.pix.width > maxw)
1129 f->fmt.pix.width = maxw;
1130 f->fmt.pix.width &= ~0x03;
1131 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
1132 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
1137 int cx25821_vidioc_querycap(struct file *file, void *priv,
1138 struct v4l2_capability *cap)
1140 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1142 strcpy(cap->driver, "cx25821");
1143 strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
1144 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1145 cap->version = CX25821_VERSION_CODE;
1146 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1148 if (UNSET != dev->tuner_type)
1149 cap->capabilities |= V4L2_CAP_TUNER;
1153 int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1154 struct v4l2_fmtdesc *f)
1156 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1159 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1160 f->pixelformat = formats[f->index].fourcc;
1165 int cx25821_vidioc_reqbufs(struct file *file, void *priv,
1166 struct v4l2_requestbuffers *p)
1168 struct cx25821_fh *fh = priv;
1169 return videobuf_reqbufs(get_queue(fh), p);
1172 int cx25821_vidioc_querybuf(struct file *file, void *priv,
1173 struct v4l2_buffer *p)
1175 struct cx25821_fh *fh = priv;
1176 return videobuf_querybuf(get_queue(fh), p);
1179 int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1181 struct cx25821_fh *fh = priv;
1182 return videobuf_qbuf(get_queue(fh), p);
1185 int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
1187 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1188 struct cx25821_fh *fh = f;
1190 *p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
1195 int cx25821_vidioc_s_priority(struct file *file, void *f,
1196 enum v4l2_priority prio)
1198 struct cx25821_fh *fh = f;
1199 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1201 return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
1206 int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id * tvnorms)
1208 struct cx25821_fh *fh = priv;
1209 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1212 dprintk(1, "%s()\n", __func__);
1215 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1221 if (dev->tvnorm == *tvnorms)
1224 mutex_lock(&dev->lock);
1225 cx25821_set_tvnorm(dev, *tvnorms);
1226 mutex_unlock(&dev->lock);
1228 medusa_set_videostandard(dev);
1234 int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
1236 static const char * const iname[] = {
1237 [CX25821_VMUX_COMPOSITE] = "Composite",
1238 [CX25821_VMUX_SVIDEO] = "S-Video",
1239 [CX25821_VMUX_DEBUG] = "for debug only",
1242 dprintk(1, "%s()\n", __func__);
1248 if (0 == INPUT(n)->type)
1251 i->type = V4L2_INPUT_TYPE_CAMERA;
1252 strcpy(i->name, iname[INPUT(n)->type]);
1254 i->std = CX25821_NORMS;
1258 int cx25821_vidioc_enum_input(struct file *file, void *priv,
1259 struct v4l2_input *i)
1261 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1262 dprintk(1, "%s()\n", __func__);
1263 return cx25821_enum_input(dev, i);
1266 int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1268 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1271 dprintk(1, "%s(): returns %d\n", __func__, *i);
1275 int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1277 struct cx25821_fh *fh = priv;
1278 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1281 dprintk(1, "%s(%d)\n", __func__, i);
1284 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1290 if (i >= CX25821_NR_INPUT) {
1291 dprintk(1, "%s(): -EINVAL\n", __func__);
1295 mutex_lock(&dev->lock);
1296 cx25821_video_mux(dev, i);
1297 mutex_unlock(&dev->lock);
1302 int cx25821_vidioc_g_frequency(struct file *file, void *priv,
1303 struct v4l2_frequency *f)
1305 struct cx25821_fh *fh = priv;
1306 struct cx25821_dev *dev = fh->dev;
1308 f->frequency = dev->freq;
1310 cx25821_call_all(dev, tuner, g_frequency, f);
1315 int cx25821_set_freq(struct cx25821_dev *dev, struct v4l2_frequency *f)
1317 mutex_lock(&dev->lock);
1318 dev->freq = f->frequency;
1320 cx25821_call_all(dev, tuner, s_frequency, f);
1322 /* When changing channels it is required to reset TVAUDIO */
1325 mutex_unlock(&dev->lock);
1330 int cx25821_vidioc_s_frequency(struct file *file, void *priv,
1331 struct v4l2_frequency *f)
1333 struct cx25821_fh *fh = priv;
1334 struct cx25821_dev *dev;
1339 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1344 pr_err("Invalid fh pointer!\n");
1348 return cx25821_set_freq(dev, f);
1352 #ifdef CONFIG_VIDEO_ADV_DEBUG
1353 int cx25821_vidioc_g_register(struct file *file, void *fh,
1354 struct v4l2_dbg_register *reg)
1356 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1358 if (!v4l2_chip_match_host(®->match))
1361 cx25821_call_all(dev, core, g_register, reg);
1366 int cx25821_vidioc_s_register(struct file *file, void *fh,
1367 struct v4l2_dbg_register *reg)
1369 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1371 if (!v4l2_chip_match_host(®->match))
1374 cx25821_call_all(dev, core, s_register, reg);
1382 int cx25821_vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1384 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1386 if (unlikely(UNSET == dev->tuner_type))
1391 strcpy(t->name, "Television");
1392 t->type = V4L2_TUNER_ANALOG_TV;
1393 t->capability = V4L2_TUNER_CAP_NORM;
1394 t->rangehigh = 0xffffffffUL;
1396 t->signal = 0xffff; /* LOCKED */
1400 int cx25821_vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1402 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1403 struct cx25821_fh *fh = priv;
1407 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1413 dprintk(1, "%s()\n", __func__);
1414 if (UNSET == dev->tuner_type)
1423 /*****************************************************************************/
1424 static const struct v4l2_queryctrl no_ctl = {
1426 .flags = V4L2_CTRL_FLAG_DISABLED,
1429 static struct v4l2_queryctrl cx25821_ctls[] = {
1432 .id = V4L2_CID_BRIGHTNESS,
1433 .name = "Brightness",
1437 .default_value = 6200,
1438 .type = V4L2_CTRL_TYPE_INTEGER,
1440 .id = V4L2_CID_CONTRAST,
1445 .default_value = 5000,
1446 .type = V4L2_CTRL_TYPE_INTEGER,
1448 .id = V4L2_CID_SATURATION,
1449 .name = "Saturation",
1453 .default_value = 5000,
1454 .type = V4L2_CTRL_TYPE_INTEGER,
1461 .default_value = 5000,
1462 .type = V4L2_CTRL_TYPE_INTEGER,
1465 static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1467 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1471 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
1473 for (i = 0; i < CX25821_CTLS; i++)
1474 if (cx25821_ctls[i].id == qctrl->id)
1476 if (i == CX25821_CTLS) {
1480 *qctrl = cx25821_ctls[i];
1484 int cx25821_vidioc_queryctrl(struct file *file, void *priv,
1485 struct v4l2_queryctrl *qctrl)
1487 return cx25821_ctrl_query(qctrl);
1490 /* ------------------------------------------------------------------ */
1491 /* VIDEO CTRL IOCTLS */
1493 static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
1497 for (i = 0; i < CX25821_CTLS; i++)
1498 if (cx25821_ctls[i].id == id)
1499 return cx25821_ctls + i;
1503 int cx25821_vidioc_g_ctrl(struct file *file, void *priv,
1504 struct v4l2_control *ctl)
1506 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1507 struct cx25821_fh *fh = priv;
1509 const struct v4l2_queryctrl *ctrl;
1511 ctrl = ctrl_by_id(ctl->id);
1516 case V4L2_CID_BRIGHTNESS:
1517 ctl->value = dev->channels[fh->channel_id].ctl_bright;
1520 ctl->value = dev->channels[fh->channel_id].ctl_hue;
1522 case V4L2_CID_CONTRAST:
1523 ctl->value = dev->channels[fh->channel_id].ctl_contrast;
1525 case V4L2_CID_SATURATION:
1526 ctl->value = dev->channels[fh->channel_id].ctl_saturation;
1532 int cx25821_set_control(struct cx25821_dev *dev,
1533 struct v4l2_control *ctl, int chan_num)
1536 const struct v4l2_queryctrl *ctrl;
1540 ctrl = ctrl_by_id(ctl->id);
1545 switch (ctrl->type) {
1546 case V4L2_CTRL_TYPE_BOOLEAN:
1547 case V4L2_CTRL_TYPE_MENU:
1548 case V4L2_CTRL_TYPE_INTEGER:
1549 if (ctl->value < ctrl->minimum)
1550 ctl->value = ctrl->minimum;
1551 if (ctl->value > ctrl->maximum)
1552 ctl->value = ctrl->maximum;
1559 case V4L2_CID_BRIGHTNESS:
1560 dev->channels[chan_num].ctl_bright = ctl->value;
1561 medusa_set_brightness(dev, ctl->value, chan_num);
1564 dev->channels[chan_num].ctl_hue = ctl->value;
1565 medusa_set_hue(dev, ctl->value, chan_num);
1567 case V4L2_CID_CONTRAST:
1568 dev->channels[chan_num].ctl_contrast = ctl->value;
1569 medusa_set_contrast(dev, ctl->value, chan_num);
1571 case V4L2_CID_SATURATION:
1572 dev->channels[chan_num].ctl_saturation = ctl->value;
1573 medusa_set_saturation(dev, ctl->value, chan_num);
1582 static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num)
1584 struct v4l2_control ctrl;
1586 for (i = 0; i < CX25821_CTLS; i++) {
1587 ctrl.id = cx25821_ctls[i].id;
1588 ctrl.value = cx25821_ctls[i].default_value;
1590 cx25821_set_control(dev, &ctrl, chan_num);
1594 int cx25821_vidioc_cropcap(struct file *file, void *priv,
1595 struct v4l2_cropcap *cropcap)
1597 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1599 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1601 cropcap->bounds.top = 0;
1602 cropcap->bounds.left = 0;
1603 cropcap->bounds.width = 720;
1604 cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1605 cropcap->pixelaspect.numerator =
1606 dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1607 cropcap->pixelaspect.denominator =
1608 dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1609 cropcap->defrect = cropcap->bounds;
1613 int cx25821_vidioc_s_crop(struct file *file, void *priv, const struct v4l2_crop *crop)
1615 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1616 struct cx25821_fh *fh = priv;
1620 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1625 /* cx25821_vidioc_s_crop not supported */
1629 int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1631 /* cx25821_vidioc_g_crop not supported */
1635 int cx25821_vidioc_querystd(struct file *file, void *priv, v4l2_std_id * norm)
1637 /* medusa does not support video standard sensing of current input */
1638 *norm = CX25821_NORMS;
1643 int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
1645 if (tvnorm == V4L2_STD_PAL_BG) {
1646 if (width == 352 || width == 720)
1652 if (tvnorm == V4L2_STD_NTSC_M) {
1653 if (width == 320 || width == 352 || width == 720)
1661 int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
1663 if (tvnorm == V4L2_STD_PAL_BG) {
1664 if (height == 576 || height == 288)
1670 if (tvnorm == V4L2_STD_NTSC_M) {
1671 if (height == 480 || height == 240)
1680 static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
1683 struct cx25821_fh *fh = file->private_data;
1684 struct cx25821_dev *dev = fh->dev;
1686 struct upstream_user_struct *data_from_user;
1688 data_from_user = (struct upstream_user_struct *)arg;
1690 if (!data_from_user) {
1691 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1695 command = data_from_user->command;
1697 if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1700 dev->input_filename = data_from_user->input_filename;
1701 dev->input_audiofilename = data_from_user->input_filename;
1702 dev->vid_stdname = data_from_user->vid_stdname;
1703 dev->pixel_format = data_from_user->pixel_format;
1704 dev->channel_select = data_from_user->channel_select;
1705 dev->command = data_from_user->command;
1708 case UPSTREAM_START_VIDEO:
1709 cx25821_start_upstream_video_ch1(dev, data_from_user);
1712 case UPSTREAM_STOP_VIDEO:
1713 cx25821_stop_upstream_video_ch1(dev);
1720 static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
1723 struct cx25821_fh *fh = file->private_data;
1724 struct cx25821_dev *dev = fh->dev;
1726 struct upstream_user_struct *data_from_user;
1728 data_from_user = (struct upstream_user_struct *)arg;
1730 if (!data_from_user) {
1731 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1735 command = data_from_user->command;
1737 if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1740 dev->input_filename_ch2 = data_from_user->input_filename;
1741 dev->input_audiofilename = data_from_user->input_filename;
1742 dev->vid_stdname_ch2 = data_from_user->vid_stdname;
1743 dev->pixel_format_ch2 = data_from_user->pixel_format;
1744 dev->channel_select_ch2 = data_from_user->channel_select;
1745 dev->command_ch2 = data_from_user->command;
1748 case UPSTREAM_START_VIDEO:
1749 cx25821_start_upstream_video_ch2(dev, data_from_user);
1752 case UPSTREAM_STOP_VIDEO:
1753 cx25821_stop_upstream_video_ch2(dev);
1760 static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
1763 struct cx25821_fh *fh = file->private_data;
1764 struct cx25821_dev *dev = fh->dev;
1766 struct upstream_user_struct *data_from_user;
1768 data_from_user = (struct upstream_user_struct *)arg;
1770 if (!data_from_user) {
1771 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1775 command = data_from_user->command;
1777 if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
1780 dev->input_filename = data_from_user->input_filename;
1781 dev->input_audiofilename = data_from_user->input_filename;
1782 dev->vid_stdname = data_from_user->vid_stdname;
1783 dev->pixel_format = data_from_user->pixel_format;
1784 dev->channel_select = data_from_user->channel_select;
1785 dev->command = data_from_user->command;
1788 case UPSTREAM_START_AUDIO:
1789 cx25821_start_upstream_audio(dev, data_from_user);
1792 case UPSTREAM_STOP_AUDIO:
1793 cx25821_stop_upstream_audio(dev);
1800 static long video_ioctl_set(struct file *file, unsigned int cmd,
1803 struct cx25821_fh *fh = file->private_data;
1804 struct cx25821_dev *dev = fh->dev;
1805 struct downstream_user_struct *data_from_user;
1808 int selected_channel = 0;
1814 data_from_user = (struct downstream_user_struct *)arg;
1816 if (!data_from_user) {
1817 pr_err("%s(): User data is INVALID. Returning\n", __func__);
1821 command = data_from_user->command;
1823 if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
1824 && command != ENABLE_CIF_RESOLUTION && command != REG_READ
1825 && command != REG_WRITE && command != MEDUSA_READ
1826 && command != MEDUSA_WRITE) {
1832 if (!strcmp(data_from_user->vid_stdname, "PAL"))
1833 dev->tvnorm = V4L2_STD_PAL_BG;
1835 dev->tvnorm = V4L2_STD_NTSC_M;
1836 medusa_set_videostandard(dev);
1839 case SET_PIXEL_FORMAT:
1840 selected_channel = data_from_user->decoder_select;
1841 pix_format = data_from_user->pixel_format;
1843 if (!(selected_channel <= 7 && selected_channel >= 0)) {
1844 selected_channel -= 4;
1845 selected_channel = selected_channel % 8;
1848 if (selected_channel >= 0)
1849 cx25821_set_pixel_format(dev, selected_channel,
1854 case ENABLE_CIF_RESOLUTION:
1855 selected_channel = data_from_user->decoder_select;
1856 cif_enable = data_from_user->cif_resolution_enable;
1857 cif_width = data_from_user->cif_width;
1860 if (dev->tvnorm & V4L2_STD_PAL_BG
1861 || dev->tvnorm & V4L2_STD_PAL_DK) {
1865 if (cif_width != 320 && cif_width != 352)
1870 if (!(selected_channel <= 7 && selected_channel >= 0)) {
1871 selected_channel -= 4;
1872 selected_channel = selected_channel % 8;
1875 if (selected_channel <= 7 && selected_channel >= 0) {
1876 dev->channels[selected_channel].use_cif_resolution =
1878 dev->channels[selected_channel].cif_width = width;
1880 for (i = 0; i < VID_CHANNEL_NUM; i++) {
1881 dev->channels[i].use_cif_resolution =
1883 dev->channels[i].cif_width = width;
1887 medusa_set_resolution(dev, width, selected_channel);
1890 data_from_user->reg_data = cx_read(data_from_user->reg_address);
1893 cx_write(data_from_user->reg_address, data_from_user->reg_data);
1896 cx25821_i2c_read(&dev->i2c_bus[0],
1897 (u16) data_from_user->reg_address,
1898 &data_from_user->reg_data);
1901 cx25821_i2c_write(&dev->i2c_bus[0],
1902 (u16) data_from_user->reg_address,
1903 data_from_user->reg_data);
1910 static long cx25821_video_ioctl(struct file *file,
1911 unsigned int cmd, unsigned long arg)
1915 struct cx25821_fh *fh = file->private_data;
1917 /* check to see if it's the video upstream */
1918 if (fh->channel_id == SRAM_CH09) {
1919 ret = video_ioctl_upstream9(file, cmd, arg);
1921 } else if (fh->channel_id == SRAM_CH10) {
1922 ret = video_ioctl_upstream10(file, cmd, arg);
1924 } else if (fh->channel_id == SRAM_CH11) {
1925 ret = video_ioctl_upstream11(file, cmd, arg);
1926 ret = video_ioctl_set(file, cmd, arg);
1930 return video_ioctl2(file, cmd, arg);
1933 /* exported stuff */
1934 static const struct v4l2_file_operations video_fops = {
1935 .owner = THIS_MODULE,
1937 .release = video_release,
1940 .mmap = cx25821_video_mmap,
1941 .ioctl = cx25821_video_ioctl,
1944 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1945 .vidioc_querycap = cx25821_vidioc_querycap,
1946 .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
1947 .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
1948 .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
1949 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1950 .vidioc_reqbufs = cx25821_vidioc_reqbufs,
1951 .vidioc_querybuf = cx25821_vidioc_querybuf,
1952 .vidioc_qbuf = cx25821_vidioc_qbuf,
1953 .vidioc_dqbuf = vidioc_dqbuf,
1955 .vidioc_s_std = cx25821_vidioc_s_std,
1956 .vidioc_querystd = cx25821_vidioc_querystd,
1958 .vidioc_cropcap = cx25821_vidioc_cropcap,
1959 .vidioc_s_crop = cx25821_vidioc_s_crop,
1960 .vidioc_g_crop = cx25821_vidioc_g_crop,
1961 .vidioc_enum_input = cx25821_vidioc_enum_input,
1962 .vidioc_g_input = cx25821_vidioc_g_input,
1963 .vidioc_s_input = cx25821_vidioc_s_input,
1964 .vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
1965 .vidioc_s_ctrl = vidioc_s_ctrl,
1966 .vidioc_queryctrl = cx25821_vidioc_queryctrl,
1967 .vidioc_streamon = vidioc_streamon,
1968 .vidioc_streamoff = vidioc_streamoff,
1969 .vidioc_log_status = vidioc_log_status,
1970 .vidioc_g_priority = cx25821_vidioc_g_priority,
1971 .vidioc_s_priority = cx25821_vidioc_s_priority,
1973 .vidioc_g_tuner = cx25821_vidioc_g_tuner,
1974 .vidioc_s_tuner = cx25821_vidioc_s_tuner,
1975 .vidioc_g_frequency = cx25821_vidioc_g_frequency,
1976 .vidioc_s_frequency = cx25821_vidioc_s_frequency,
1978 #ifdef CONFIG_VIDEO_ADV_DEBUG
1979 .vidioc_g_register = cx25821_vidioc_g_register,
1980 .vidioc_s_register = cx25821_vidioc_s_register,
1984 struct video_device cx25821_videoioctl_template = {
1985 .name = "cx25821-videoioctl",
1986 .fops = &video_fops,
1987 .ioctl_ops = &video_ioctl_ops,
1988 .tvnorms = CX25821_NORMS,
1989 .current_norm = V4L2_STD_NTSC_M,