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_dump_video_queue(struct cx25821_dev
*dev
,
113 struct cx25821_dmaqueue
*q
)
115 struct cx25821_buffer
*buf
;
116 struct list_head
*item
;
117 dprintk(1, "%s()\n", __func__
);
119 if (!list_empty(&q
->active
)) {
120 list_for_each(item
, &q
->active
)
121 buf
= list_entry(item
, struct cx25821_buffer
, vb
.queue
);
124 if (!list_empty(&q
->queued
)) {
125 list_for_each(item
, &q
->queued
)
126 buf
= list_entry(item
, struct cx25821_buffer
, vb
.queue
);
131 void cx25821_video_wakeup(struct cx25821_dev
*dev
, struct cx25821_dmaqueue
*q
,
134 struct cx25821_buffer
*buf
;
137 for (bc
= 0;; bc
++) {
138 if (list_empty(&q
->active
)) {
139 dprintk(1, "bc=%d (=0: active empty)\n", bc
);
144 list_entry(q
->active
.next
, struct cx25821_buffer
, vb
.queue
);
146 /* count comes from the hw and it is 16bit wide --
147 * this trick handles wrap-arounds correctly for
148 * up to 32767 buffers in flight... */
149 if ((s16
) (count
- buf
->count
) < 0)
152 do_gettimeofday(&buf
->vb
.ts
);
153 buf
->vb
.state
= VIDEOBUF_DONE
;
154 list_del(&buf
->vb
.queue
);
155 wake_up(&buf
->vb
.done
);
158 if (list_empty(&q
->active
))
159 del_timer(&q
->timeout
);
161 mod_timer(&q
->timeout
, jiffies
+ BUFFER_TIMEOUT
);
163 pr_err("%s: %d buffers handled (should be 1)\n", __func__
, bc
);
167 int cx25821_set_tvnorm(struct cx25821_dev
*dev
, v4l2_std_id norm
)
169 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
170 __func__
, (unsigned int)norm
, v4l2_norm_to_name(norm
));
174 /* Tell the internal A/V decoder */
175 cx25821_call_all(dev
, core
, s_std
, norm
);
181 struct video_device
*cx25821_vdev_init(struct cx25821_dev
*dev
,
183 struct video_device
*template,
186 struct video_device
*vfd
;
187 dprintk(1, "%s()\n", __func__
);
189 vfd
= video_device_alloc();
193 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
194 vfd
->release
= video_device_release
;
195 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s (%s)", dev
->name
, type
,
196 cx25821_boards
[dev
->board
].name
);
197 video_set_drvdata(vfd
, dev
);
202 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
206 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
208 for (i = 0; i < CX25821_CTLS; i++)
209 if (cx25821_ctls[i].v.id == qctrl->id)
211 if (i == CX25821_CTLS) {
215 *qctrl = cx25821_ctls[i].v;
220 /* resource management */
221 int cx25821_res_get(struct cx25821_dev
*dev
, struct cx25821_fh
*fh
,
224 dprintk(1, "%s()\n", __func__
);
225 if (fh
->resources
& bit
)
226 /* have it already allocated */
230 mutex_lock(&dev
->lock
);
231 if (dev
->channels
[fh
->channel_id
].resources
& bit
) {
232 /* no, someone else uses it */
233 mutex_unlock(&dev
->lock
);
236 /* it's free, grab it */
237 fh
->resources
|= bit
;
238 dev
->channels
[fh
->channel_id
].resources
|= bit
;
239 dprintk(1, "res: get %d\n", bit
);
240 mutex_unlock(&dev
->lock
);
244 int cx25821_res_check(struct cx25821_fh
*fh
, unsigned int bit
)
246 return fh
->resources
& bit
;
249 int cx25821_res_locked(struct cx25821_fh
*fh
, unsigned int bit
)
251 return fh
->dev
->channels
[fh
->channel_id
].resources
& bit
;
254 void cx25821_res_free(struct cx25821_dev
*dev
, struct cx25821_fh
*fh
,
257 BUG_ON((fh
->resources
& bits
) != bits
);
258 dprintk(1, "%s()\n", __func__
);
260 mutex_lock(&dev
->lock
);
261 fh
->resources
&= ~bits
;
262 dev
->channels
[fh
->channel_id
].resources
&= ~bits
;
263 dprintk(1, "res: put %d\n", bits
);
264 mutex_unlock(&dev
->lock
);
267 int cx25821_video_mux(struct cx25821_dev
*dev
, unsigned int input
)
269 struct v4l2_routing route
;
270 memset(&route
, 0, sizeof(route
));
272 dprintk(1, "%s(): video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
273 __func__
, input
, INPUT(input
)->vmux
, INPUT(input
)->gpio0
,
274 INPUT(input
)->gpio1
, INPUT(input
)->gpio2
, INPUT(input
)->gpio3
);
277 route
.input
= INPUT(input
)->vmux
;
279 /* Tell the internal A/V decoder */
280 cx25821_call_all(dev
, video
, s_routing
, INPUT(input
)->vmux
, 0, 0);
285 int cx25821_start_video_dma(struct cx25821_dev
*dev
,
286 struct cx25821_dmaqueue
*q
,
287 struct cx25821_buffer
*buf
,
288 struct sram_channel
*channel
)
292 /* setup fifo + format */
293 cx25821_sram_channel_setup(dev
, channel
, buf
->bpl
, buf
->risc
.dma
);
296 cx_write(channel
->gpcnt_ctl
, 3);
300 cx_set(PCI_INT_MSK
, cx_read(PCI_INT_MSK
) | (1 << channel
->i
));
301 cx_set(channel
->int_msk
, 0x11);
304 cx_write(channel
->dma_ctl
, 0x11); /* FIFO and RISC enable */
306 /* make sure upstream setting if any is reversed */
307 tmp
= cx_read(VID_CH_MODE_SEL
);
308 cx_write(VID_CH_MODE_SEL
, tmp
& 0xFFFFFE00);
313 int cx25821_restart_video_queue(struct cx25821_dev
*dev
,
314 struct cx25821_dmaqueue
*q
,
315 struct sram_channel
*channel
)
317 struct cx25821_buffer
*buf
, *prev
;
318 struct list_head
*item
;
320 if (!list_empty(&q
->active
)) {
322 list_entry(q
->active
.next
, struct cx25821_buffer
, vb
.queue
);
324 cx25821_start_video_dma(dev
, q
, buf
, channel
);
326 list_for_each(item
, &q
->active
) {
327 buf
= list_entry(item
, struct cx25821_buffer
, vb
.queue
);
328 buf
->count
= q
->count
++;
331 mod_timer(&q
->timeout
, jiffies
+ BUFFER_TIMEOUT
);
337 if (list_empty(&q
->queued
))
341 list_entry(q
->queued
.next
, struct cx25821_buffer
, vb
.queue
);
344 list_move_tail(&buf
->vb
.queue
, &q
->active
);
345 cx25821_start_video_dma(dev
, q
, buf
, channel
);
346 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
347 buf
->count
= q
->count
++;
348 mod_timer(&q
->timeout
, jiffies
+ BUFFER_TIMEOUT
);
349 } else if (prev
->vb
.width
== buf
->vb
.width
&&
350 prev
->vb
.height
== buf
->vb
.height
&&
351 prev
->fmt
== buf
->fmt
) {
352 list_move_tail(&buf
->vb
.queue
, &q
->active
);
353 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
354 buf
->count
= q
->count
++;
355 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
356 prev
->risc
.jmp
[2] = cpu_to_le32(0); /* Bits 63 - 32 */
364 void cx25821_vid_timeout(unsigned long data
)
366 struct cx25821_data
*timeout_data
= (struct cx25821_data
*)data
;
367 struct cx25821_dev
*dev
= timeout_data
->dev
;
368 struct sram_channel
*channel
= timeout_data
->channel
;
369 struct cx25821_dmaqueue
*q
= &dev
->channels
[channel
->i
].vidq
;
370 struct cx25821_buffer
*buf
;
373 /* cx25821_sram_channel_dump(dev, channel); */
374 cx_clear(channel
->dma_ctl
, 0x11);
376 spin_lock_irqsave(&dev
->slock
, flags
);
377 while (!list_empty(&q
->active
)) {
379 list_entry(q
->active
.next
, struct cx25821_buffer
, vb
.queue
);
380 list_del(&buf
->vb
.queue
);
382 buf
->vb
.state
= VIDEOBUF_ERROR
;
383 wake_up(&buf
->vb
.done
);
386 cx25821_restart_video_queue(dev
, q
, channel
);
387 spin_unlock_irqrestore(&dev
->slock
, flags
);
390 int cx25821_video_irq(struct cx25821_dev
*dev
, int chan_num
, u32 status
)
395 struct sram_channel
*channel
= dev
->channels
[chan_num
].sram_channels
;
397 mask
= cx_read(channel
->int_msk
);
398 if (0 == (status
& mask
))
401 cx_write(channel
->int_stat
, status
);
403 /* risc op code error */
404 if (status
& (1 << 16)) {
405 pr_warn("%s, %s: video risc op code error\n",
406 dev
->name
, channel
->name
);
407 cx_clear(channel
->dma_ctl
, 0x11);
408 cx25821_sram_channel_dump(dev
, channel
);
412 if (status
& FLD_VID_DST_RISC1
) {
413 spin_lock(&dev
->slock
);
414 count
= cx_read(channel
->gpcnt
);
415 cx25821_video_wakeup(dev
, &dev
->channels
[channel
->i
].vidq
,
417 spin_unlock(&dev
->slock
);
423 dprintk(2, "stopper video\n");
424 spin_lock(&dev
->slock
);
425 cx25821_restart_video_queue(dev
,
426 &dev
->channels
[channel
->i
].vidq
, channel
);
427 spin_unlock(&dev
->slock
);
433 void cx25821_videoioctl_unregister(struct cx25821_dev
*dev
)
435 if (dev
->ioctl_dev
) {
436 if (video_is_registered(dev
->ioctl_dev
))
437 video_unregister_device(dev
->ioctl_dev
);
439 video_device_release(dev
->ioctl_dev
);
441 dev
->ioctl_dev
= NULL
;
445 void cx25821_video_unregister(struct cx25821_dev
*dev
, int chan_num
)
447 cx_clear(PCI_INT_MSK
, 1);
449 if (dev
->channels
[chan_num
].video_dev
) {
450 if (video_is_registered(dev
->channels
[chan_num
].video_dev
))
451 video_unregister_device(
452 dev
->channels
[chan_num
].video_dev
);
454 video_device_release(
455 dev
->channels
[chan_num
].video_dev
);
457 dev
->channels
[chan_num
].video_dev
= NULL
;
459 btcx_riscmem_free(dev
->pci
,
460 &dev
->channels
[chan_num
].vidq
.stopper
);
462 pr_warn("device %d released!\n", chan_num
);
467 int cx25821_video_register(struct cx25821_dev
*dev
)
472 struct video_device cx25821_video_device
= {
473 .name
= "cx25821-video",
476 .ioctl_ops
= &video_ioctl_ops
,
477 .tvnorms
= CX25821_NORMS
,
478 .current_norm
= V4L2_STD_NTSC_M
,
481 spin_lock_init(&dev
->slock
);
483 for (i
= 0; i
< MAX_VID_CHANNEL_NUM
- 1; ++i
) {
484 cx25821_init_controls(dev
, i
);
486 cx25821_risc_stopper(dev
->pci
, &dev
->channels
[i
].vidq
.stopper
,
487 dev
->channels
[i
].sram_channels
->dma_ctl
,
490 dev
->channels
[i
].sram_channels
= &cx25821_sram_channels
[i
];
491 dev
->channels
[i
].video_dev
= NULL
;
492 dev
->channels
[i
].resources
= 0;
494 cx_write(dev
->channels
[i
].sram_channels
->int_stat
, 0xffffffff);
496 INIT_LIST_HEAD(&dev
->channels
[i
].vidq
.active
);
497 INIT_LIST_HEAD(&dev
->channels
[i
].vidq
.queued
);
499 dev
->channels
[i
].timeout_data
.dev
= dev
;
500 dev
->channels
[i
].timeout_data
.channel
=
501 &cx25821_sram_channels
[i
];
502 dev
->channels
[i
].vidq
.timeout
.function
=
504 dev
->channels
[i
].vidq
.timeout
.data
=
505 (unsigned long)&dev
->channels
[i
].timeout_data
;
506 init_timer(&dev
->channels
[i
].vidq
.timeout
);
508 /* register v4l devices */
509 dev
->channels
[i
].video_dev
= cx25821_vdev_init(dev
,
510 dev
->pci
, &cx25821_video_device
, "video");
512 err
= video_register_device(dev
->channels
[i
].video_dev
,
513 VFL_TYPE_GRABBER
, video_nr
[dev
->nr
]);
520 /* set PCI interrupt */
521 cx_set(PCI_INT_MSK
, 0xff);
523 /* initial device configuration */
524 mutex_lock(&dev
->lock
);
526 dev
->tvnorm
= cx25821_video_device
.current_norm
;
527 cx25821_set_tvnorm(dev
, dev
->tvnorm
);
529 mutex_unlock(&dev
->lock
);
535 cx25821_video_unregister(dev
, i
);
539 int cx25821_buffer_setup(struct videobuf_queue
*q
, unsigned int *count
,
542 struct cx25821_fh
*fh
= q
->priv_data
;
544 *size
= fh
->fmt
->depth
* fh
->width
* fh
->height
>> 3;
549 if (*size
* *count
> vid_limit
* 1024 * 1024)
550 *count
= (vid_limit
* 1024 * 1024) / *size
;
555 int cx25821_buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
556 enum v4l2_field field
)
558 struct cx25821_fh
*fh
= q
->priv_data
;
559 struct cx25821_dev
*dev
= fh
->dev
;
560 struct cx25821_buffer
*buf
=
561 container_of(vb
, struct cx25821_buffer
, vb
);
562 int rc
, init_buffer
= 0;
563 u32 line0_offset
, line1_offset
;
564 struct videobuf_dmabuf
*dma
= videobuf_to_dma(&buf
->vb
);
565 int bpl_local
= LINE_SIZE_D1
;
566 int channel_opened
= fh
->channel_id
;
568 BUG_ON(NULL
== fh
->fmt
);
569 if (fh
->width
< 48 || fh
->width
> 720 ||
570 fh
->height
< 32 || fh
->height
> 576)
573 buf
->vb
.size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
575 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
578 if (buf
->fmt
!= fh
->fmt
||
579 buf
->vb
.width
!= fh
->width
||
580 buf
->vb
.height
!= fh
->height
|| buf
->vb
.field
!= field
) {
582 buf
->vb
.width
= fh
->width
;
583 buf
->vb
.height
= fh
->height
;
584 buf
->vb
.field
= field
;
588 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
590 rc
= videobuf_iolock(q
, &buf
->vb
, NULL
);
592 printk(KERN_DEBUG
pr_fmt("videobuf_iolock failed!\n"));
597 dprintk(1, "init_buffer=%d\n", init_buffer
);
601 channel_opened
= dev
->channel_opened
;
602 if (channel_opened
< 0 || channel_opened
> 7)
605 if (dev
->channels
[channel_opened
].pixel_formats
==
607 buf
->bpl
= (buf
->fmt
->depth
* buf
->vb
.width
) >> 3;
609 buf
->bpl
= (buf
->fmt
->depth
>> 3) * (buf
->vb
.width
);
611 if (dev
->channels
[channel_opened
].pixel_formats
==
613 bpl_local
= buf
->bpl
;
615 bpl_local
= buf
->bpl
; /* Default */
617 if (channel_opened
>= 0 && channel_opened
<= 7) {
618 if (dev
->channels
[channel_opened
]
619 .use_cif_resolution
) {
620 if (dev
->tvnorm
& V4L2_STD_PAL_BG
621 || dev
->tvnorm
& V4L2_STD_PAL_DK
)
622 bpl_local
= 352 << 1;
625 dev
->channels
[channel_opened
].
632 switch (buf
->vb
.field
) {
634 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
635 dma
->sglist
, 0, UNSET
,
636 buf
->bpl
, 0, buf
->vb
.height
);
638 case V4L2_FIELD_BOTTOM
:
639 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
640 dma
->sglist
, UNSET
, 0,
641 buf
->bpl
, 0, buf
->vb
.height
);
643 case V4L2_FIELD_INTERLACED
:
644 /* All other formats are top field first */
646 line1_offset
= buf
->bpl
;
647 dprintk(1, "top field first\n");
649 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
650 dma
->sglist
, line0_offset
,
651 bpl_local
, bpl_local
, bpl_local
,
652 buf
->vb
.height
>> 1);
654 case V4L2_FIELD_SEQ_TB
:
655 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
657 0, buf
->bpl
* (buf
->vb
.height
>> 1),
658 buf
->bpl
, 0, buf
->vb
.height
>> 1);
660 case V4L2_FIELD_SEQ_BT
:
661 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
663 buf
->bpl
* (buf
->vb
.height
>> 1), 0,
664 buf
->bpl
, 0, buf
->vb
.height
>> 1);
671 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
672 buf
, buf
->vb
.i
, fh
->width
, fh
->height
, fh
->fmt
->depth
,
673 fh
->fmt
->name
, (unsigned long)buf
->risc
.dma
);
675 buf
->vb
.state
= VIDEOBUF_PREPARED
;
680 cx25821_free_buffer(q
, buf
);
684 void cx25821_buffer_release(struct videobuf_queue
*q
,
685 struct videobuf_buffer
*vb
)
687 struct cx25821_buffer
*buf
=
688 container_of(vb
, struct cx25821_buffer
, vb
);
690 cx25821_free_buffer(q
, buf
);
693 struct videobuf_queue
*get_queue(struct cx25821_fh
*fh
)
696 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
704 int cx25821_get_resource(struct cx25821_fh
*fh
, int resource
)
707 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
715 int cx25821_video_mmap(struct file
*file
, struct vm_area_struct
*vma
)
717 struct cx25821_fh
*fh
= file
->private_data
;
719 return videobuf_mmap_mapper(get_queue(fh
), vma
);
723 static void buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
725 struct cx25821_buffer
*buf
=
726 container_of(vb
, struct cx25821_buffer
, vb
);
727 struct cx25821_buffer
*prev
;
728 struct cx25821_fh
*fh
= vq
->priv_data
;
729 struct cx25821_dev
*dev
= fh
->dev
;
730 struct cx25821_dmaqueue
*q
= &dev
->channels
[fh
->channel_id
].vidq
;
732 /* add jump to stopper */
733 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| RISC_CNT_INC
);
734 buf
->risc
.jmp
[1] = cpu_to_le32(q
->stopper
.dma
);
735 buf
->risc
.jmp
[2] = cpu_to_le32(0); /* bits 63-32 */
737 dprintk(2, "jmp to stopper (0x%x)\n", buf
->risc
.jmp
[1]);
739 if (!list_empty(&q
->queued
)) {
740 list_add_tail(&buf
->vb
.queue
, &q
->queued
);
741 buf
->vb
.state
= VIDEOBUF_QUEUED
;
742 dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf
,
745 } else if (list_empty(&q
->active
)) {
746 list_add_tail(&buf
->vb
.queue
, &q
->active
);
747 cx25821_start_video_dma(dev
, q
, buf
,
748 dev
->channels
[fh
->channel_id
].sram_channels
);
749 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
750 buf
->count
= q
->count
++;
751 mod_timer(&q
->timeout
, jiffies
+ BUFFER_TIMEOUT
);
752 dprintk(2, "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
753 buf
, buf
->vb
.i
, buf
->count
, q
->count
);
755 prev
= list_entry(q
->active
.prev
, struct cx25821_buffer
,
757 if (prev
->vb
.width
== buf
->vb
.width
758 && prev
->vb
.height
== buf
->vb
.height
759 && prev
->fmt
== buf
->fmt
) {
760 list_add_tail(&buf
->vb
.queue
, &q
->active
);
761 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
762 buf
->count
= q
->count
++;
763 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
765 /* 64 bit bits 63-32 */
766 prev
->risc
.jmp
[2] = cpu_to_le32(0);
767 dprintk(2, "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
768 buf
, buf
->vb
.i
, buf
->count
);
771 list_add_tail(&buf
->vb
.queue
, &q
->queued
);
772 buf
->vb
.state
= VIDEOBUF_QUEUED
;
773 dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf
,
778 if (list_empty(&q
->active
))
779 dprintk(2, "active queue empty!\n");
782 static struct videobuf_queue_ops cx25821_video_qops
= {
783 .buf_setup
= cx25821_buffer_setup
,
784 .buf_prepare
= cx25821_buffer_prepare
,
785 .buf_queue
= buffer_queue
,
786 .buf_release
= cx25821_buffer_release
,
789 static int video_open(struct file
*file
)
791 struct video_device
*vdev
= video_devdata(file
);
792 struct cx25821_dev
*h
, *dev
= video_drvdata(file
);
793 struct cx25821_fh
*fh
;
794 struct list_head
*list
;
795 int minor
= video_devdata(file
)->minor
;
796 enum v4l2_buf_type type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
801 dprintk(1, "open dev=%s type=%s\n", video_device_node_name(vdev
),
802 v4l2_type_names
[type
]);
804 /* allocate + initialize per filehandle data */
805 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
809 mutex_lock(&cx25821_devlist_mutex
);
811 list_for_each(list
, &cx25821_devlist
)
813 h
= list_entry(list
, struct cx25821_dev
, devlist
);
815 for (i
= 0; i
< MAX_VID_CHANNEL_NUM
; i
++) {
816 if (h
->channels
[i
].video_dev
&&
817 h
->channels
[i
].video_dev
->minor
== minor
) {
820 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
826 mutex_unlock(&cx25821_devlist_mutex
);
831 file
->private_data
= fh
;
835 fh
->channel_id
= ch_id
;
837 if (dev
->tvnorm
& V4L2_STD_PAL_BG
|| dev
->tvnorm
& V4L2_STD_PAL_DK
)
842 dev
->channel_opened
= fh
->channel_id
;
843 if (dev
->channels
[ch_id
].pixel_formats
== PIXEL_FRMT_411
)
844 pix_format
= V4L2_PIX_FMT_Y41P
;
846 pix_format
= V4L2_PIX_FMT_YUYV
;
847 fh
->fmt
= cx25821_format_by_fourcc(pix_format
);
849 v4l2_prio_open(&dev
->channels
[ch_id
].prio
, &fh
->prio
);
851 videobuf_queue_sg_init(&fh
->vidq
, &cx25821_video_qops
,
852 &dev
->pci
->dev
, &dev
->slock
,
853 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
854 V4L2_FIELD_INTERLACED
,
855 sizeof(struct cx25821_buffer
), fh
, NULL
);
857 dprintk(1, "post videobuf_queue_init()\n");
858 mutex_unlock(&cx25821_devlist_mutex
);
863 static ssize_t
video_read(struct file
*file
, char __user
* data
, size_t count
,
866 struct cx25821_fh
*fh
= file
->private_data
;
869 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
870 if (cx25821_res_locked(fh
, RESOURCE_VIDEO0
))
873 return videobuf_read_one(&fh
->vidq
, data
, count
, ppos
,
874 file
->f_flags
& O_NONBLOCK
);
882 static unsigned int video_poll(struct file
*file
,
883 struct poll_table_struct
*wait
)
885 struct cx25821_fh
*fh
= file
->private_data
;
886 struct cx25821_buffer
*buf
;
888 if (cx25821_res_check(fh
, RESOURCE_VIDEO0
)) {
889 /* streaming capture */
890 if (list_empty(&fh
->vidq
.stream
))
892 buf
= list_entry(fh
->vidq
.stream
.next
,
893 struct cx25821_buffer
, vb
.stream
);
896 buf
= (struct cx25821_buffer
*)fh
->vidq
.read_buf
;
901 poll_wait(file
, &buf
->vb
.done
, wait
);
902 if (buf
->vb
.state
== VIDEOBUF_DONE
|| buf
->vb
.state
== VIDEOBUF_ERROR
) {
903 if (buf
->vb
.state
== VIDEOBUF_DONE
) {
904 struct cx25821_dev
*dev
= fh
->dev
;
906 if (dev
&& dev
->channels
[fh
->channel_id
]
907 .use_cif_resolution
) {
908 u8 cam_id
= *((char *)buf
->vb
.baddr
+ 3);
909 memcpy((char *)buf
->vb
.baddr
,
910 (char *)buf
->vb
.baddr
+ (fh
->width
* 2),
912 *((char *)buf
->vb
.baddr
+ 3) = cam_id
;
916 return POLLIN
| POLLRDNORM
;
922 static int video_release(struct file
*file
)
924 struct cx25821_fh
*fh
= file
->private_data
;
925 struct cx25821_dev
*dev
= fh
->dev
;
927 /* stop the risc engine and fifo */
928 cx_write(channel0
->dma_ctl
, 0); /* FIFO and RISC disable */
930 /* stop video capture */
931 if (cx25821_res_check(fh
, RESOURCE_VIDEO0
)) {
932 videobuf_queue_cancel(&fh
->vidq
);
933 cx25821_res_free(dev
, fh
, RESOURCE_VIDEO0
);
936 if (fh
->vidq
.read_buf
) {
937 cx25821_buffer_release(&fh
->vidq
, fh
->vidq
.read_buf
);
938 kfree(fh
->vidq
.read_buf
);
941 videobuf_mmap_free(&fh
->vidq
);
943 v4l2_prio_close(&dev
->channels
[fh
->channel_id
].prio
, fh
->prio
);
944 file
->private_data
= NULL
;
950 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
952 struct cx25821_fh
*fh
= priv
;
953 struct cx25821_dev
*dev
= fh
->dev
;
955 if (unlikely(fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
))
958 if (unlikely(i
!= fh
->type
))
961 if (unlikely(!cx25821_res_get(dev
, fh
, cx25821_get_resource(fh
,
965 return videobuf_streamon(get_queue(fh
));
968 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
970 struct cx25821_fh
*fh
= priv
;
971 struct cx25821_dev
*dev
= fh
->dev
;
974 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
979 res
= cx25821_get_resource(fh
, RESOURCE_VIDEO0
);
980 err
= videobuf_streamoff(get_queue(fh
));
983 cx25821_res_free(dev
, fh
, res
);
987 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
988 struct v4l2_format
*f
)
990 struct cx25821_fh
*fh
= priv
;
991 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
992 struct v4l2_mbus_framefmt mbus_fmt
;
994 int pix_format
= PIXEL_FRMT_422
;
997 err
= v4l2_prio_check(&dev
->channels
[fh
->channel_id
].prio
,
1003 dprintk(2, "%s()\n", __func__
);
1004 err
= cx25821_vidioc_try_fmt_vid_cap(file
, priv
, f
);
1009 fh
->fmt
= cx25821_format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1010 fh
->vidq
.field
= f
->fmt
.pix
.field
;
1012 /* check if width and height is valid based on set standard */
1013 if (cx25821_is_valid_width(f
->fmt
.pix
.width
, dev
->tvnorm
))
1014 fh
->width
= f
->fmt
.pix
.width
;
1016 if (cx25821_is_valid_height(f
->fmt
.pix
.height
, dev
->tvnorm
))
1017 fh
->height
= f
->fmt
.pix
.height
;
1019 if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_Y41P
)
1020 pix_format
= PIXEL_FRMT_411
;
1021 else if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_YUYV
)
1022 pix_format
= PIXEL_FRMT_422
;
1026 cx25821_set_pixel_format(dev
, SRAM_CH00
, pix_format
);
1028 /* check if cif resolution */
1029 if (fh
->width
== 320 || fh
->width
== 352)
1030 dev
->channels
[fh
->channel_id
].use_cif_resolution
= 1;
1032 dev
->channels
[fh
->channel_id
].use_cif_resolution
= 0;
1034 dev
->channels
[fh
->channel_id
].cif_width
= fh
->width
;
1035 medusa_set_resolution(dev
, fh
->width
, SRAM_CH00
);
1037 dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__
, fh
->width
,
1038 fh
->height
, fh
->vidq
.field
);
1039 v4l2_fill_mbus_format(&mbus_fmt
, &f
->fmt
.pix
, V4L2_MBUS_FMT_FIXED
);
1040 cx25821_call_all(dev
, video
, s_mbus_fmt
, &mbus_fmt
);
1045 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1048 struct cx25821_fh
*fh
= priv
;
1049 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1051 ret_val
= videobuf_dqbuf(get_queue(fh
), p
, file
->f_flags
& O_NONBLOCK
);
1053 p
->sequence
= dev
->channels
[fh
->channel_id
].vidq
.count
;
1058 static int vidioc_log_status(struct file
*file
, void *priv
)
1060 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1061 struct cx25821_fh
*fh
= priv
;
1064 struct sram_channel
*sram_ch
= dev
->channels
[fh
->channel_id
]
1068 snprintf(name
, sizeof(name
), "%s/2", dev
->name
);
1069 pr_info("%s/2: ============ START LOG STATUS ============\n",
1071 cx25821_call_all(dev
, core
, log_status
);
1072 tmp
= cx_read(sram_ch
->dma_ctl
);
1073 pr_info("Video input 0 is %s\n",
1074 (tmp
& 0x11) ? "streaming" : "stopped");
1075 pr_info("%s/2: ============= END LOG STATUS =============\n",
1080 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1081 struct v4l2_control
*ctl
)
1083 struct cx25821_fh
*fh
= priv
;
1084 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1088 err
= v4l2_prio_check(&dev
->channels
[fh
->channel_id
].prio
,
1094 return cx25821_set_control(dev
, ctl
, fh
->channel_id
);
1098 int cx25821_vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1099 struct v4l2_format
*f
)
1101 struct cx25821_fh
*fh
= priv
;
1103 f
->fmt
.pix
.width
= fh
->width
;
1104 f
->fmt
.pix
.height
= fh
->height
;
1105 f
->fmt
.pix
.field
= fh
->vidq
.field
;
1106 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
1107 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
1108 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1113 int cx25821_vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1114 struct v4l2_format
*f
)
1116 struct cx25821_fmt
*fmt
;
1117 enum v4l2_field field
;
1118 unsigned int maxw
, maxh
;
1120 fmt
= cx25821_format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1124 field
= f
->fmt
.pix
.field
;
1128 if (V4L2_FIELD_ANY
== field
) {
1129 if (f
->fmt
.pix
.height
> maxh
/ 2)
1130 field
= V4L2_FIELD_INTERLACED
;
1132 field
= V4L2_FIELD_TOP
;
1136 case V4L2_FIELD_TOP
:
1137 case V4L2_FIELD_BOTTOM
:
1140 case V4L2_FIELD_INTERLACED
:
1146 f
->fmt
.pix
.field
= field
;
1147 if (f
->fmt
.pix
.height
< 32)
1148 f
->fmt
.pix
.height
= 32;
1149 if (f
->fmt
.pix
.height
> maxh
)
1150 f
->fmt
.pix
.height
= maxh
;
1151 if (f
->fmt
.pix
.width
< 48)
1152 f
->fmt
.pix
.width
= 48;
1153 if (f
->fmt
.pix
.width
> maxw
)
1154 f
->fmt
.pix
.width
= maxw
;
1155 f
->fmt
.pix
.width
&= ~0x03;
1156 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1157 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1162 int cx25821_vidioc_querycap(struct file
*file
, void *priv
,
1163 struct v4l2_capability
*cap
)
1165 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1167 strcpy(cap
->driver
, "cx25821");
1168 strlcpy(cap
->card
, cx25821_boards
[dev
->board
].name
, sizeof(cap
->card
));
1169 sprintf(cap
->bus_info
, "PCIe:%s", pci_name(dev
->pci
));
1170 cap
->version
= CX25821_VERSION_CODE
;
1172 V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1173 if (UNSET
!= dev
->tuner_type
)
1174 cap
->capabilities
|= V4L2_CAP_TUNER
;
1178 int cx25821_vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1179 struct v4l2_fmtdesc
*f
)
1181 if (unlikely(f
->index
>= ARRAY_SIZE(formats
)))
1184 strlcpy(f
->description
, formats
[f
->index
].name
, sizeof(f
->description
));
1185 f
->pixelformat
= formats
[f
->index
].fourcc
;
1190 int cx25821_vidioc_reqbufs(struct file
*file
, void *priv
,
1191 struct v4l2_requestbuffers
*p
)
1193 struct cx25821_fh
*fh
= priv
;
1194 return videobuf_reqbufs(get_queue(fh
), p
);
1197 int cx25821_vidioc_querybuf(struct file
*file
, void *priv
,
1198 struct v4l2_buffer
*p
)
1200 struct cx25821_fh
*fh
= priv
;
1201 return videobuf_querybuf(get_queue(fh
), p
);
1204 int cx25821_vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1206 struct cx25821_fh
*fh
= priv
;
1207 return videobuf_qbuf(get_queue(fh
), p
);
1210 int cx25821_vidioc_g_priority(struct file
*file
, void *f
, enum v4l2_priority
*p
)
1212 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)f
)->dev
;
1213 struct cx25821_fh
*fh
= f
;
1215 *p
= v4l2_prio_max(&dev
->channels
[fh
->channel_id
].prio
);
1220 int cx25821_vidioc_s_priority(struct file
*file
, void *f
,
1221 enum v4l2_priority prio
)
1223 struct cx25821_fh
*fh
= f
;
1224 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)f
)->dev
;
1226 return v4l2_prio_change(&dev
->channels
[fh
->channel_id
].prio
, &fh
->prio
,
1231 int cx25821_vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
* tvnorms
)
1233 struct cx25821_fh
*fh
= priv
;
1234 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1237 dprintk(1, "%s()\n", __func__
);
1240 err
= v4l2_prio_check(&dev
->channels
[fh
->channel_id
].prio
,
1246 if (dev
->tvnorm
== *tvnorms
)
1249 mutex_lock(&dev
->lock
);
1250 cx25821_set_tvnorm(dev
, *tvnorms
);
1251 mutex_unlock(&dev
->lock
);
1253 medusa_set_videostandard(dev
);
1259 int cx25821_enum_input(struct cx25821_dev
*dev
, struct v4l2_input
*i
)
1261 static const char * const iname
[] = {
1262 [CX25821_VMUX_COMPOSITE
] = "Composite",
1263 [CX25821_VMUX_SVIDEO
] = "S-Video",
1264 [CX25821_VMUX_DEBUG
] = "for debug only",
1267 dprintk(1, "%s()\n", __func__
);
1273 if (0 == INPUT(n
)->type
)
1276 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1277 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
1279 i
->std
= CX25821_NORMS
;
1283 int cx25821_vidioc_enum_input(struct file
*file
, void *priv
,
1284 struct v4l2_input
*i
)
1286 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1287 dprintk(1, "%s()\n", __func__
);
1288 return cx25821_enum_input(dev
, i
);
1291 int cx25821_vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1293 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1296 dprintk(1, "%s(): returns %d\n", __func__
, *i
);
1300 int cx25821_vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1302 struct cx25821_fh
*fh
= priv
;
1303 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1306 dprintk(1, "%s(%d)\n", __func__
, i
);
1309 err
= v4l2_prio_check(&dev
->channels
[fh
->channel_id
].prio
,
1316 dprintk(1, "%s(): -EINVAL\n", __func__
);
1320 mutex_lock(&dev
->lock
);
1321 cx25821_video_mux(dev
, i
);
1322 mutex_unlock(&dev
->lock
);
1327 int cx25821_vidioc_g_frequency(struct file
*file
, void *priv
,
1328 struct v4l2_frequency
*f
)
1330 struct cx25821_fh
*fh
= priv
;
1331 struct cx25821_dev
*dev
= fh
->dev
;
1333 f
->frequency
= dev
->freq
;
1335 cx25821_call_all(dev
, tuner
, g_frequency
, f
);
1340 int cx25821_set_freq(struct cx25821_dev
*dev
, struct v4l2_frequency
*f
)
1342 mutex_lock(&dev
->lock
);
1343 dev
->freq
= f
->frequency
;
1345 cx25821_call_all(dev
, tuner
, s_frequency
, f
);
1347 /* When changing channels it is required to reset TVAUDIO */
1350 mutex_unlock(&dev
->lock
);
1355 int cx25821_vidioc_s_frequency(struct file
*file
, void *priv
,
1356 struct v4l2_frequency
*f
)
1358 struct cx25821_fh
*fh
= priv
;
1359 struct cx25821_dev
*dev
;
1364 err
= v4l2_prio_check(&dev
->channels
[fh
->channel_id
].prio
,
1369 pr_err("Invalid fh pointer!\n");
1373 return cx25821_set_freq(dev
, f
);
1377 #ifdef CONFIG_VIDEO_ADV_DEBUG
1378 int cx25821_vidioc_g_register(struct file
*file
, void *fh
,
1379 struct v4l2_dbg_register
*reg
)
1381 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)fh
)->dev
;
1383 if (!v4l2_chip_match_host(®
->match
))
1386 cx25821_call_all(dev
, core
, g_register
, reg
);
1391 int cx25821_vidioc_s_register(struct file
*file
, void *fh
,
1392 struct v4l2_dbg_register
*reg
)
1394 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)fh
)->dev
;
1396 if (!v4l2_chip_match_host(®
->match
))
1399 cx25821_call_all(dev
, core
, s_register
, reg
);
1407 int cx25821_vidioc_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
1409 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1411 if (unlikely(UNSET
== dev
->tuner_type
))
1416 strcpy(t
->name
, "Television");
1417 t
->type
= V4L2_TUNER_ANALOG_TV
;
1418 t
->capability
= V4L2_TUNER_CAP_NORM
;
1419 t
->rangehigh
= 0xffffffffUL
;
1421 t
->signal
= 0xffff; /* LOCKED */
1425 int cx25821_vidioc_s_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
1427 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1428 struct cx25821_fh
*fh
= priv
;
1432 err
= v4l2_prio_check(&dev
->channels
[fh
->channel_id
].prio
,
1438 dprintk(1, "%s()\n", __func__
);
1439 if (UNSET
== dev
->tuner_type
)
1448 /*****************************************************************************/
1449 static const struct v4l2_queryctrl no_ctl
= {
1451 .flags
= V4L2_CTRL_FLAG_DISABLED
,
1454 static struct v4l2_queryctrl cx25821_ctls
[] = {
1457 .id
= V4L2_CID_BRIGHTNESS
,
1458 .name
= "Brightness",
1462 .default_value
= 6200,
1463 .type
= V4L2_CTRL_TYPE_INTEGER
,
1465 .id
= V4L2_CID_CONTRAST
,
1470 .default_value
= 5000,
1471 .type
= V4L2_CTRL_TYPE_INTEGER
,
1473 .id
= V4L2_CID_SATURATION
,
1474 .name
= "Saturation",
1478 .default_value
= 5000,
1479 .type
= V4L2_CTRL_TYPE_INTEGER
,
1486 .default_value
= 5000,
1487 .type
= V4L2_CTRL_TYPE_INTEGER
,
1490 static const int CX25821_CTLS
= ARRAY_SIZE(cx25821_ctls
);
1492 static int cx25821_ctrl_query(struct v4l2_queryctrl
*qctrl
)
1496 if (qctrl
->id
< V4L2_CID_BASE
|| qctrl
->id
>= V4L2_CID_LASTP1
)
1498 for (i
= 0; i
< CX25821_CTLS
; i
++)
1499 if (cx25821_ctls
[i
].id
== qctrl
->id
)
1501 if (i
== CX25821_CTLS
) {
1505 *qctrl
= cx25821_ctls
[i
];
1509 int cx25821_vidioc_queryctrl(struct file
*file
, void *priv
,
1510 struct v4l2_queryctrl
*qctrl
)
1512 return cx25821_ctrl_query(qctrl
);
1515 /* ------------------------------------------------------------------ */
1516 /* VIDEO CTRL IOCTLS */
1518 static const struct v4l2_queryctrl
*ctrl_by_id(unsigned int id
)
1522 for (i
= 0; i
< CX25821_CTLS
; i
++)
1523 if (cx25821_ctls
[i
].id
== id
)
1524 return cx25821_ctls
+ i
;
1528 int cx25821_vidioc_g_ctrl(struct file
*file
, void *priv
,
1529 struct v4l2_control
*ctl
)
1531 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1532 struct cx25821_fh
*fh
= priv
;
1534 const struct v4l2_queryctrl
*ctrl
;
1536 ctrl
= ctrl_by_id(ctl
->id
);
1541 case V4L2_CID_BRIGHTNESS
:
1542 ctl
->value
= dev
->channels
[fh
->channel_id
].ctl_bright
;
1545 ctl
->value
= dev
->channels
[fh
->channel_id
].ctl_hue
;
1547 case V4L2_CID_CONTRAST
:
1548 ctl
->value
= dev
->channels
[fh
->channel_id
].ctl_contrast
;
1550 case V4L2_CID_SATURATION
:
1551 ctl
->value
= dev
->channels
[fh
->channel_id
].ctl_saturation
;
1557 int cx25821_set_control(struct cx25821_dev
*dev
,
1558 struct v4l2_control
*ctl
, int chan_num
)
1561 const struct v4l2_queryctrl
*ctrl
;
1565 ctrl
= ctrl_by_id(ctl
->id
);
1570 switch (ctrl
->type
) {
1571 case V4L2_CTRL_TYPE_BOOLEAN
:
1572 case V4L2_CTRL_TYPE_MENU
:
1573 case V4L2_CTRL_TYPE_INTEGER
:
1574 if (ctl
->value
< ctrl
->minimum
)
1575 ctl
->value
= ctrl
->minimum
;
1576 if (ctl
->value
> ctrl
->maximum
)
1577 ctl
->value
= ctrl
->maximum
;
1584 case V4L2_CID_BRIGHTNESS
:
1585 dev
->channels
[chan_num
].ctl_bright
= ctl
->value
;
1586 medusa_set_brightness(dev
, ctl
->value
, chan_num
);
1589 dev
->channels
[chan_num
].ctl_hue
= ctl
->value
;
1590 medusa_set_hue(dev
, ctl
->value
, chan_num
);
1592 case V4L2_CID_CONTRAST
:
1593 dev
->channels
[chan_num
].ctl_contrast
= ctl
->value
;
1594 medusa_set_contrast(dev
, ctl
->value
, chan_num
);
1596 case V4L2_CID_SATURATION
:
1597 dev
->channels
[chan_num
].ctl_saturation
= ctl
->value
;
1598 medusa_set_saturation(dev
, ctl
->value
, chan_num
);
1607 static void cx25821_init_controls(struct cx25821_dev
*dev
, int chan_num
)
1609 struct v4l2_control ctrl
;
1611 for (i
= 0; i
< CX25821_CTLS
; i
++) {
1612 ctrl
.id
= cx25821_ctls
[i
].id
;
1613 ctrl
.value
= cx25821_ctls
[i
].default_value
;
1615 cx25821_set_control(dev
, &ctrl
, chan_num
);
1619 int cx25821_vidioc_cropcap(struct file
*file
, void *priv
,
1620 struct v4l2_cropcap
*cropcap
)
1622 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1624 if (cropcap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1626 cropcap
->bounds
.top
= cropcap
->bounds
.left
= 0;
1627 cropcap
->bounds
.width
= 720;
1628 cropcap
->bounds
.height
= dev
->tvnorm
== V4L2_STD_PAL_BG
? 576 : 480;
1629 cropcap
->pixelaspect
.numerator
=
1630 dev
->tvnorm
== V4L2_STD_PAL_BG
? 59 : 10;
1631 cropcap
->pixelaspect
.denominator
=
1632 dev
->tvnorm
== V4L2_STD_PAL_BG
? 54 : 11;
1633 cropcap
->defrect
= cropcap
->bounds
;
1637 int cx25821_vidioc_s_crop(struct file
*file
, void *priv
, struct v4l2_crop
*crop
)
1639 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1640 struct cx25821_fh
*fh
= priv
;
1644 err
= v4l2_prio_check(&dev
->channels
[fh
->channel_id
].prio
,
1649 /* cx25821_vidioc_s_crop not supported */
1653 int cx25821_vidioc_g_crop(struct file
*file
, void *priv
, struct v4l2_crop
*crop
)
1655 /* cx25821_vidioc_g_crop not supported */
1659 int cx25821_vidioc_querystd(struct file
*file
, void *priv
, v4l2_std_id
* norm
)
1661 /* medusa does not support video standard sensing of current input */
1662 *norm
= CX25821_NORMS
;
1667 int cx25821_is_valid_width(u32 width
, v4l2_std_id tvnorm
)
1669 if (tvnorm
== V4L2_STD_PAL_BG
) {
1670 if (width
== 352 || width
== 720)
1676 if (tvnorm
== V4L2_STD_NTSC_M
) {
1677 if (width
== 320 || width
== 352 || width
== 720)
1685 int cx25821_is_valid_height(u32 height
, v4l2_std_id tvnorm
)
1687 if (tvnorm
== V4L2_STD_PAL_BG
) {
1688 if (height
== 576 || height
== 288)
1694 if (tvnorm
== V4L2_STD_NTSC_M
) {
1695 if (height
== 480 || height
== 240)
1704 static long video_ioctl_upstream9(struct file
*file
, unsigned int cmd
,
1707 struct cx25821_fh
*fh
= file
->private_data
;
1708 struct cx25821_dev
*dev
= fh
->dev
;
1710 struct upstream_user_struct
*data_from_user
;
1712 data_from_user
= (struct upstream_user_struct
*)arg
;
1714 if (!data_from_user
) {
1715 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__
);
1719 command
= data_from_user
->command
;
1721 if (command
!= UPSTREAM_START_VIDEO
&& command
!= UPSTREAM_STOP_VIDEO
)
1724 dev
->input_filename
= data_from_user
->input_filename
;
1725 dev
->input_audiofilename
= data_from_user
->input_filename
;
1726 dev
->vid_stdname
= data_from_user
->vid_stdname
;
1727 dev
->pixel_format
= data_from_user
->pixel_format
;
1728 dev
->channel_select
= data_from_user
->channel_select
;
1729 dev
->command
= data_from_user
->command
;
1732 case UPSTREAM_START_VIDEO
:
1733 cx25821_start_upstream_video_ch1(dev
, data_from_user
);
1736 case UPSTREAM_STOP_VIDEO
:
1737 cx25821_stop_upstream_video_ch1(dev
);
1744 static long video_ioctl_upstream10(struct file
*file
, unsigned int cmd
,
1747 struct cx25821_fh
*fh
= file
->private_data
;
1748 struct cx25821_dev
*dev
= fh
->dev
;
1750 struct upstream_user_struct
*data_from_user
;
1752 data_from_user
= (struct upstream_user_struct
*)arg
;
1754 if (!data_from_user
) {
1755 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__
);
1759 command
= data_from_user
->command
;
1761 if (command
!= UPSTREAM_START_VIDEO
&& command
!= UPSTREAM_STOP_VIDEO
)
1764 dev
->input_filename_ch2
= data_from_user
->input_filename
;
1765 dev
->input_audiofilename
= data_from_user
->input_filename
;
1766 dev
->vid_stdname_ch2
= data_from_user
->vid_stdname
;
1767 dev
->pixel_format_ch2
= data_from_user
->pixel_format
;
1768 dev
->channel_select_ch2
= data_from_user
->channel_select
;
1769 dev
->command_ch2
= data_from_user
->command
;
1772 case UPSTREAM_START_VIDEO
:
1773 cx25821_start_upstream_video_ch2(dev
, data_from_user
);
1776 case UPSTREAM_STOP_VIDEO
:
1777 cx25821_stop_upstream_video_ch2(dev
);
1784 static long video_ioctl_upstream11(struct file
*file
, unsigned int cmd
,
1787 struct cx25821_fh
*fh
= file
->private_data
;
1788 struct cx25821_dev
*dev
= fh
->dev
;
1790 struct upstream_user_struct
*data_from_user
;
1792 data_from_user
= (struct upstream_user_struct
*)arg
;
1794 if (!data_from_user
) {
1795 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__
);
1799 command
= data_from_user
->command
;
1801 if (command
!= UPSTREAM_START_AUDIO
&& command
!= UPSTREAM_STOP_AUDIO
)
1804 dev
->input_filename
= data_from_user
->input_filename
;
1805 dev
->input_audiofilename
= data_from_user
->input_filename
;
1806 dev
->vid_stdname
= data_from_user
->vid_stdname
;
1807 dev
->pixel_format
= data_from_user
->pixel_format
;
1808 dev
->channel_select
= data_from_user
->channel_select
;
1809 dev
->command
= data_from_user
->command
;
1812 case UPSTREAM_START_AUDIO
:
1813 cx25821_start_upstream_audio(dev
, data_from_user
);
1816 case UPSTREAM_STOP_AUDIO
:
1817 cx25821_stop_upstream_audio(dev
);
1824 static long video_ioctl_set(struct file
*file
, unsigned int cmd
,
1827 struct cx25821_fh
*fh
= file
->private_data
;
1828 struct cx25821_dev
*dev
= fh
->dev
;
1829 struct downstream_user_struct
*data_from_user
;
1832 int selected_channel
= 0, pix_format
= 0, i
= 0;
1833 int cif_enable
= 0, cif_width
= 0;
1836 data_from_user
= (struct downstream_user_struct
*)arg
;
1838 if (!data_from_user
) {
1839 pr_err("%s(): User data is INVALID. Returning\n", __func__
);
1843 command
= data_from_user
->command
;
1845 if (command
!= SET_VIDEO_STD
&& command
!= SET_PIXEL_FORMAT
1846 && command
!= ENABLE_CIF_RESOLUTION
&& command
!= REG_READ
1847 && command
!= REG_WRITE
&& command
!= MEDUSA_READ
1848 && command
!= MEDUSA_WRITE
) {
1854 if (!strcmp(data_from_user
->vid_stdname
, "PAL"))
1855 dev
->tvnorm
= V4L2_STD_PAL_BG
;
1857 dev
->tvnorm
= V4L2_STD_NTSC_M
;
1858 medusa_set_videostandard(dev
);
1861 case SET_PIXEL_FORMAT
:
1862 selected_channel
= data_from_user
->decoder_select
;
1863 pix_format
= data_from_user
->pixel_format
;
1865 if (!(selected_channel
<= 7 && selected_channel
>= 0)) {
1866 selected_channel
-= 4;
1867 selected_channel
= selected_channel
% 8;
1870 if (selected_channel
>= 0)
1871 cx25821_set_pixel_format(dev
, selected_channel
,
1876 case ENABLE_CIF_RESOLUTION
:
1877 selected_channel
= data_from_user
->decoder_select
;
1878 cif_enable
= data_from_user
->cif_resolution_enable
;
1879 cif_width
= data_from_user
->cif_width
;
1882 if (dev
->tvnorm
& V4L2_STD_PAL_BG
1883 || dev
->tvnorm
& V4L2_STD_PAL_DK
) {
1887 if (cif_width
!= 320 && cif_width
!= 352)
1892 if (!(selected_channel
<= 7 && selected_channel
>= 0)) {
1893 selected_channel
-= 4;
1894 selected_channel
= selected_channel
% 8;
1897 if (selected_channel
<= 7 && selected_channel
>= 0) {
1898 dev
->channels
[selected_channel
].
1899 use_cif_resolution
= cif_enable
;
1900 dev
->channels
[selected_channel
].cif_width
= width
;
1902 for (i
= 0; i
< VID_CHANNEL_NUM
; i
++) {
1903 dev
->channels
[i
].use_cif_resolution
=
1905 dev
->channels
[i
].cif_width
= width
;
1909 medusa_set_resolution(dev
, width
, selected_channel
);
1912 data_from_user
->reg_data
= cx_read(data_from_user
->reg_address
);
1915 cx_write(data_from_user
->reg_address
, data_from_user
->reg_data
);
1918 value
= cx25821_i2c_read(&dev
->i2c_bus
[0],
1919 (u16
) data_from_user
->reg_address
,
1920 &data_from_user
->reg_data
);
1923 cx25821_i2c_write(&dev
->i2c_bus
[0],
1924 (u16
) data_from_user
->reg_address
,
1925 data_from_user
->reg_data
);
1932 static long cx25821_video_ioctl(struct file
*file
,
1933 unsigned int cmd
, unsigned long arg
)
1937 struct cx25821_fh
*fh
= file
->private_data
;
1939 /* check to see if it's the video upstream */
1940 if (fh
->channel_id
== SRAM_CH09
) {
1941 ret
= video_ioctl_upstream9(file
, cmd
, arg
);
1943 } else if (fh
->channel_id
== SRAM_CH10
) {
1944 ret
= video_ioctl_upstream10(file
, cmd
, arg
);
1946 } else if (fh
->channel_id
== SRAM_CH11
) {
1947 ret
= video_ioctl_upstream11(file
, cmd
, arg
);
1948 ret
= video_ioctl_set(file
, cmd
, arg
);
1952 return video_ioctl2(file
, cmd
, arg
);
1955 /* exported stuff */
1956 static const struct v4l2_file_operations video_fops
= {
1957 .owner
= THIS_MODULE
,
1959 .release
= video_release
,
1962 .mmap
= cx25821_video_mmap
,
1963 .ioctl
= cx25821_video_ioctl
,
1966 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1967 .vidioc_querycap
= cx25821_vidioc_querycap
,
1968 .vidioc_enum_fmt_vid_cap
= cx25821_vidioc_enum_fmt_vid_cap
,
1969 .vidioc_g_fmt_vid_cap
= cx25821_vidioc_g_fmt_vid_cap
,
1970 .vidioc_try_fmt_vid_cap
= cx25821_vidioc_try_fmt_vid_cap
,
1971 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1972 .vidioc_reqbufs
= cx25821_vidioc_reqbufs
,
1973 .vidioc_querybuf
= cx25821_vidioc_querybuf
,
1974 .vidioc_qbuf
= cx25821_vidioc_qbuf
,
1975 .vidioc_dqbuf
= vidioc_dqbuf
,
1977 .vidioc_s_std
= cx25821_vidioc_s_std
,
1978 .vidioc_querystd
= cx25821_vidioc_querystd
,
1980 .vidioc_cropcap
= cx25821_vidioc_cropcap
,
1981 .vidioc_s_crop
= cx25821_vidioc_s_crop
,
1982 .vidioc_g_crop
= cx25821_vidioc_g_crop
,
1983 .vidioc_enum_input
= cx25821_vidioc_enum_input
,
1984 .vidioc_g_input
= cx25821_vidioc_g_input
,
1985 .vidioc_s_input
= cx25821_vidioc_s_input
,
1986 .vidioc_g_ctrl
= cx25821_vidioc_g_ctrl
,
1987 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1988 .vidioc_queryctrl
= cx25821_vidioc_queryctrl
,
1989 .vidioc_streamon
= vidioc_streamon
,
1990 .vidioc_streamoff
= vidioc_streamoff
,
1991 .vidioc_log_status
= vidioc_log_status
,
1992 .vidioc_g_priority
= cx25821_vidioc_g_priority
,
1993 .vidioc_s_priority
= cx25821_vidioc_s_priority
,
1995 .vidioc_g_tuner
= cx25821_vidioc_g_tuner
,
1996 .vidioc_s_tuner
= cx25821_vidioc_s_tuner
,
1997 .vidioc_g_frequency
= cx25821_vidioc_g_frequency
,
1998 .vidioc_s_frequency
= cx25821_vidioc_s_frequency
,
2000 #ifdef CONFIG_VIDEO_ADV_DEBUG
2001 .vidioc_g_register
= cx25821_vidioc_g_register
,
2002 .vidioc_s_register
= cx25821_vidioc_s_register
,
2006 struct video_device cx25821_videoioctl_template
= {
2007 .name
= "cx25821-videoioctl",
2008 .fops
= &video_fops
,
2009 .ioctl_ops
= &video_ioctl_ops
,
2010 .tvnorms
= CX25821_NORMS
,
2011 .current_norm
= V4L2_STD_NTSC_M
,