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
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "cx25821-video.h"
26 MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
27 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
28 MODULE_LICENSE("GPL");
30 static unsigned int video_nr
[] = {[0 ... (CX25821_MAXBOARDS
- 1)] = UNSET
};
31 static unsigned int radio_nr
[] = {[0 ... (CX25821_MAXBOARDS
- 1)] = UNSET
};
33 module_param_array(video_nr
, int, NULL
, 0444);
34 module_param_array(radio_nr
, int, NULL
, 0444);
36 MODULE_PARM_DESC(video_nr
, "video device numbers");
37 MODULE_PARM_DESC(radio_nr
, "radio device numbers");
39 static unsigned int video_debug
=VIDEO_DEBUG
;
40 module_param(video_debug
, int, 0644);
41 MODULE_PARM_DESC(video_debug
, "enable debug messages [video]");
43 static unsigned int irq_debug
;
44 module_param(irq_debug
, int, 0644);
45 MODULE_PARM_DESC(irq_debug
, "enable debug messages [IRQ handler]");
47 unsigned int vid_limit
= 16;
48 module_param(vid_limit
, int, 0644);
49 MODULE_PARM_DESC(vid_limit
, "capture memory limit in megabytes");
51 static void init_controls(struct cx25821_dev
*dev
, int chan_num
);
53 #define FORMAT_FLAGS_PACKED 0x01
55 struct cx25821_fmt formats
[] = {
57 .name
= "8 bpp, gray",
58 .fourcc
= V4L2_PIX_FMT_GREY
,
60 .flags
= FORMAT_FLAGS_PACKED
,
62 .name
= "4:1:1, packed, Y41P",
63 .fourcc
= V4L2_PIX_FMT_Y41P
,
65 .flags
= FORMAT_FLAGS_PACKED
,
67 .name
= "4:2:2, packed, YUYV",
68 .fourcc
= V4L2_PIX_FMT_YUYV
,
70 .flags
= FORMAT_FLAGS_PACKED
,
72 .name
= "4:2:2, packed, UYVY",
73 .fourcc
= V4L2_PIX_FMT_UYVY
,
75 .flags
= FORMAT_FLAGS_PACKED
,
78 .fourcc
= V4L2_PIX_FMT_YUV420
,
80 .flags
= FORMAT_FLAGS_PACKED
,
85 int get_format_size(void)
87 return ARRAY_SIZE(formats
);
91 struct cx25821_fmt
*format_by_fourcc(unsigned int fourcc
)
95 if( fourcc
== V4L2_PIX_FMT_Y41P
|| fourcc
== V4L2_PIX_FMT_YUV411P
)
100 for (i
= 0; i
< ARRAY_SIZE(formats
); i
++)
101 if (formats
[i
].fourcc
== fourcc
)
104 printk(KERN_ERR
"%s(0x%08x) NOT FOUND\n", __func__
, fourcc
);
108 void dump_video_queue(struct cx25821_dev
*dev
, struct cx25821_dmaqueue
*q
)
110 struct cx25821_buffer
*buf
;
111 struct list_head
*item
;
112 dprintk(1, "%s()\n", __func__
);
114 if (!list_empty(&q
->active
)) {
115 list_for_each(item
, &q
->active
)
116 buf
= list_entry(item
, struct cx25821_buffer
, vb
.queue
);
119 if (!list_empty(&q
->queued
))
121 list_for_each(item
, &q
->queued
)
122 buf
= list_entry(item
, struct cx25821_buffer
, vb
.queue
);
128 void cx25821_video_wakeup(struct cx25821_dev
*dev
, struct cx25821_dmaqueue
*q
, u32 count
)
130 struct cx25821_buffer
*buf
;
133 for (bc
= 0;; bc
++) {
134 if (list_empty(&q
->active
))
136 dprintk(1, "bc=%d (=0: active empty)\n", bc
);
140 buf
= list_entry(q
->active
.next
, struct cx25821_buffer
, vb
.queue
);
142 /* count comes from the hw and it is 16bit wide --
143 * this trick handles wrap-arounds correctly for
144 * up to 32767 buffers in flight... */
145 if ((s16
) (count
- buf
->count
) < 0)
150 do_gettimeofday(&buf
->vb
.ts
);
151 buf
->vb
.state
= VIDEOBUF_DONE
;
152 list_del(&buf
->vb
.queue
);
153 wake_up(&buf
->vb
.done
);
156 if (list_empty(&q
->active
))
157 del_timer(&q
->timeout
);
159 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
161 printk(KERN_ERR
"%s: %d buffers handled (should be 1)\n", __func__
, bc
);
165 int cx25821_set_tvnorm(struct cx25821_dev
*dev
, v4l2_std_id norm
)
167 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n", __func__
,
169 v4l2_norm_to_name(norm
));
173 /* Tell the internal A/V decoder */
174 cx25821_call_all(dev
, core
, s_std
, norm
);
180 struct video_device
*cx25821_vdev_init(struct cx25821_dev
*dev
,
182 struct video_device
*template,
185 struct video_device
*vfd
;
186 dprintk(1, "%s()\n", __func__
);
188 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
, cx25821_boards
[dev
->board
].name
);
200 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
204 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
206 for (i = 0; i < CX25821_CTLS; i++)
207 if (cx25821_ctls[i].v.id == qctrl->id)
209 if (i == CX25821_CTLS) {
213 *qctrl = cx25821_ctls[i].v;
218 // resource management
219 int res_get(struct cx25821_dev
*dev
, struct cx25821_fh
*fh
, unsigned int bit
)
221 dprintk(1, "%s()\n", __func__
);
222 if (fh
->resources
& bit
)
223 /* have it already allocated */
227 mutex_lock(&dev
->lock
);
228 if (dev
->resources
& bit
) {
229 /* no, someone else uses it */
230 mutex_unlock(&dev
->lock
);
233 /* it's free, grab it */
234 fh
->resources
|= bit
;
235 dev
->resources
|= bit
;
236 dprintk(1, "res: get %d\n", bit
);
237 mutex_unlock(&dev
->lock
);
241 int res_check(struct cx25821_fh
*fh
, unsigned int bit
)
243 return fh
->resources
& bit
;
246 int res_locked(struct cx25821_dev
*dev
, unsigned int bit
)
248 return dev
->resources
& bit
;
251 void res_free(struct cx25821_dev
*dev
, struct cx25821_fh
*fh
, unsigned int bits
)
253 BUG_ON((fh
->resources
& bits
) != bits
);
254 dprintk(1, "%s()\n", __func__
);
256 mutex_lock(&dev
->lock
);
257 fh
->resources
&= ~bits
;
258 dev
->resources
&= ~bits
;
259 dprintk(1, "res: put %d\n", bits
);
260 mutex_unlock(&dev
->lock
);
263 int cx25821_video_mux(struct cx25821_dev
*dev
, unsigned int input
)
265 struct v4l2_routing route
;
266 memset(&route
, 0, sizeof(route
));
268 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n", __func__
,
269 input
, INPUT(input
)->vmux
,
270 INPUT(input
)->gpio0
, INPUT(input
)->gpio1
,
271 INPUT(input
)->gpio2
, INPUT(input
)->gpio3
);
274 route
.input
= INPUT(input
)->vmux
;
276 /* Tell the internal A/V decoder */
277 cx25821_call_all(dev
, video
, s_routing
, INPUT(input
)->vmux
, 0, 0);
282 int cx25821_start_video_dma(struct cx25821_dev
*dev
,
283 struct cx25821_dmaqueue
*q
,
284 struct cx25821_buffer
*buf
,
285 struct sram_channel
*channel
)
289 /* setup fifo + format */
290 cx25821_sram_channel_setup(dev
, channel
, buf
->bpl
, buf
->risc
.dma
);
293 cx_write(channel
->gpcnt_ctl
, 3);
297 cx_set(PCI_INT_MSK
, cx_read(PCI_INT_MSK
) | (1<<channel
->i
));
298 cx_set(channel
->int_msk
, 0x11);
301 cx_write(channel
->dma_ctl
, 0x11); /* FIFO and RISC enable */
303 /* make sure upstream setting if any is reversed */
304 tmp
= cx_read( VID_CH_MODE_SEL
);
305 cx_write( VID_CH_MODE_SEL
, tmp
& 0xFFFFFE00);
311 int cx25821_restart_video_queue(struct cx25821_dev
*dev
, struct cx25821_dmaqueue
*q
, struct sram_channel
*channel
)
313 struct cx25821_buffer
*buf
, *prev
;
314 struct list_head
*item
;
316 if (!list_empty(&q
->active
)) {
317 buf
= list_entry(q
->active
.next
, struct cx25821_buffer
, vb
.queue
);
319 cx25821_start_video_dma(dev
, q
, buf
, channel
);
321 list_for_each(item
, &q
->active
) {
322 buf
= list_entry(item
, struct cx25821_buffer
, vb
.queue
);
323 buf
->count
= q
->count
++;
326 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
332 if (list_empty(&q
->queued
))
335 buf
= list_entry(q
->queued
.next
, struct cx25821_buffer
, vb
.queue
);
338 list_move_tail(&buf
->vb
.queue
, &q
->active
);
339 cx25821_start_video_dma(dev
, q
, buf
, channel
);
340 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
341 buf
->count
= q
->count
++;
342 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
343 } else if (prev
->vb
.width
== buf
->vb
.width
&&
344 prev
->vb
.height
== buf
->vb
.height
&&
345 prev
->fmt
== buf
->fmt
) {
346 list_move_tail(&buf
->vb
.queue
, &q
->active
);
347 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
348 buf
->count
= q
->count
++;
349 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
350 prev
->risc
.jmp
[2] = cpu_to_le32(0); /* Bits 63 - 32 */
358 void cx25821_vid_timeout(unsigned long data
)
360 struct cx25821_data
*timeout_data
= (struct cx25821_data
*)data
;
361 struct cx25821_dev
*dev
= timeout_data
->dev
;
362 struct sram_channel
*channel
= timeout_data
->channel
;
363 struct cx25821_dmaqueue
*q
= &dev
->vidq
[channel
->i
];
364 struct cx25821_buffer
*buf
;
367 //cx25821_sram_channel_dump(dev, channel);
368 cx_clear(channel
->dma_ctl
, 0x11);
370 spin_lock_irqsave(&dev
->slock
, flags
);
371 while (!list_empty(&q
->active
)) {
372 buf
= list_entry(q
->active
.next
, struct cx25821_buffer
, vb
.queue
);
373 list_del(&buf
->vb
.queue
);
375 buf
->vb
.state
= VIDEOBUF_ERROR
;
376 wake_up(&buf
->vb
.done
);
379 cx25821_restart_video_queue(dev
, q
, channel
);
380 spin_unlock_irqrestore(&dev
->slock
, flags
);
383 int cx25821_video_irq(struct cx25821_dev
*dev
, int chan_num
, u32 status
)
388 struct sram_channel
*channel
= &dev
->sram_channels
[chan_num
];
390 mask
= cx_read(channel
->int_msk
);
391 if (0 == (status
& mask
))
394 cx_write(channel
->int_stat
, status
);
396 /* risc op code error */
397 if (status
& (1 << 16)) {
398 printk(KERN_WARNING
"%s, %s: video risc op code error\n", dev
->name
, channel
->name
);
399 cx_clear(channel
->dma_ctl
, 0x11);
400 cx25821_sram_channel_dump(dev
, channel
);
404 if (status
& FLD_VID_DST_RISC1
) {
405 spin_lock(&dev
->slock
);
406 count
= cx_read(channel
->gpcnt
);
407 cx25821_video_wakeup(dev
, &dev
->vidq
[channel
->i
], count
);
408 spin_unlock(&dev
->slock
);
414 dprintk(2, "stopper video\n");
415 spin_lock(&dev
->slock
);
416 cx25821_restart_video_queue(dev
, &dev
->vidq
[channel
->i
], channel
);
417 spin_unlock(&dev
->slock
);
423 void cx25821_videoioctl_unregister(struct cx25821_dev
*dev
)
427 if (dev
->ioctl_dev
->minor
!= -1)
428 video_unregister_device(dev
->ioctl_dev
);
430 video_device_release(dev
->ioctl_dev
);
432 dev
->ioctl_dev
= NULL
;
436 void cx25821_video_unregister(struct cx25821_dev
*dev
, int chan_num
)
438 cx_clear(PCI_INT_MSK
, 1);
440 if (dev
->video_dev
[chan_num
]) {
441 if (-1 != dev
->video_dev
[chan_num
]->minor
)
442 video_unregister_device(dev
->video_dev
[chan_num
]);
444 video_device_release(dev
->video_dev
[chan_num
]);
446 dev
->video_dev
[chan_num
] = NULL
;
448 btcx_riscmem_free(dev
->pci
, &dev
->vidq
[chan_num
].stopper
);
450 printk(KERN_WARNING
"device %d released!\n", chan_num
);
456 int cx25821_video_register(struct cx25821_dev
*dev
, int chan_num
, struct video_device
*video_template
)
460 spin_lock_init(&dev
->slock
);
462 //printk(KERN_WARNING "Channel %d\n", chan_num);
465 dev
->tvnorm
= video_template
->current_norm
;
468 /* init video dma queues */
469 dev
->timeout_data
[chan_num
].dev
= dev
;
470 dev
->timeout_data
[chan_num
].channel
= &dev
->sram_channels
[chan_num
];
471 INIT_LIST_HEAD(&dev
->vidq
[chan_num
].active
);
472 INIT_LIST_HEAD(&dev
->vidq
[chan_num
].queued
);
473 dev
->vidq
[chan_num
].timeout
.function
= cx25821_vid_timeout
;
474 dev
->vidq
[chan_num
].timeout
.data
= (unsigned long)&dev
->timeout_data
[chan_num
];
475 init_timer(&dev
->vidq
[chan_num
].timeout
);
476 cx25821_risc_stopper(dev
->pci
, &dev
->vidq
[chan_num
].stopper
, dev
->sram_channels
[chan_num
].dma_ctl
, 0x11, 0);
479 /* register v4l devices */
480 dev
->video_dev
[chan_num
] = cx25821_vdev_init(dev
, dev
->pci
, video_template
, "video");
481 err
= video_register_device(dev
->video_dev
[chan_num
], VFL_TYPE_GRABBER
, video_nr
[dev
->nr
]);
488 cx_set(PCI_INT_MSK
, 0xff);
491 /* initial device configuration */
492 mutex_lock(&dev
->lock
);
494 cx25821_set_tvnorm(dev
, dev
->tvnorm
);
496 mutex_unlock(&dev
->lock
);
498 init_controls(dev
, chan_num
);
503 cx25821_video_unregister(dev
, chan_num
);
507 int buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
509 struct cx25821_fh
*fh
= q
->priv_data
;
511 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
517 while (*size
* *count
> vid_limit
* 1024 * 1024)
523 int buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
, enum v4l2_field field
)
525 struct cx25821_fh
*fh
= q
->priv_data
;
526 struct cx25821_dev
*dev
= fh
->dev
;
527 struct cx25821_buffer
*buf
= container_of(vb
, struct cx25821_buffer
, vb
);
528 int rc
, init_buffer
= 0;
529 u32 line0_offset
, line1_offset
;
530 struct videobuf_dmabuf
*dma
= videobuf_to_dma(&buf
->vb
);
531 int bpl_local
= LINE_SIZE_D1
;
532 int channel_opened
= 0;
535 BUG_ON(NULL
== fh
->fmt
);
536 if (fh
->width
< 48 || fh
->width
> 720 ||
537 fh
->height
< 32 || fh
->height
> 576)
540 buf
->vb
.size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
542 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
546 if (buf
->fmt
!= fh
->fmt
||
547 buf
->vb
.width
!= fh
->width
||
548 buf
->vb
.height
!= fh
->height
||
549 buf
->vb
.field
!= field
) {
551 buf
->vb
.width
= fh
->width
;
552 buf
->vb
.height
= fh
->height
;
553 buf
->vb
.field
= field
;
557 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
559 rc
= videobuf_iolock(q
, &buf
->vb
, NULL
);
562 printk(KERN_DEBUG
"videobuf_iolock failed!\n");
567 dprintk(1, "init_buffer=%d\n", init_buffer
);
571 channel_opened
= dev
->channel_opened
;
572 channel_opened
= (channel_opened
< 0 || channel_opened
> 7) ? 7 : channel_opened
;
574 if( dev
->pixel_formats
[channel_opened
] == PIXEL_FRMT_411
)
575 buf
->bpl
= (buf
->fmt
->depth
* buf
->vb
.width
) >> 3;
577 buf
->bpl
= (buf
->fmt
->depth
>> 3) * (buf
->vb
.width
);
580 if( dev
->pixel_formats
[channel_opened
] == PIXEL_FRMT_411
)
582 bpl_local
= buf
->bpl
;
586 bpl_local
= buf
->bpl
; //Default
588 if( channel_opened
>= 0 && channel_opened
<= 7 )
590 if( dev
->use_cif_resolution
[channel_opened
] )
592 if( dev
->tvnorm
& V4L2_STD_PAL_BG
|| dev
->tvnorm
& V4L2_STD_PAL_DK
)
593 bpl_local
= 352 << 1;
595 bpl_local
= dev
->cif_width
[channel_opened
] << 1;
601 switch (buf
->vb
.field
) {
603 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
604 dma
->sglist
, 0, UNSET
,
605 buf
->bpl
, 0, buf
->vb
.height
);
607 case V4L2_FIELD_BOTTOM
:
608 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
609 dma
->sglist
, UNSET
, 0,
610 buf
->bpl
, 0, buf
->vb
.height
);
612 case V4L2_FIELD_INTERLACED
:
613 /* All other formats are top field first */
615 line1_offset
= buf
->bpl
;
616 dprintk(1, "top field first\n");
618 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
619 dma
->sglist
, line0_offset
,
620 bpl_local
, bpl_local
, bpl_local
,
621 buf
->vb
.height
>> 1);
623 case V4L2_FIELD_SEQ_TB
:
624 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
626 0, buf
->bpl
* (buf
->vb
.height
>> 1),
628 buf
->vb
.height
>> 1);
630 case V4L2_FIELD_SEQ_BT
:
631 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
633 buf
->bpl
* (buf
->vb
.height
>> 1), 0,
635 buf
->vb
.height
>> 1);
642 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
643 buf
, buf
->vb
.i
, fh
->width
, fh
->height
, fh
->fmt
->depth
, fh
->fmt
->name
,
644 (unsigned long)buf
->risc
.dma
);
646 buf
->vb
.state
= VIDEOBUF_PREPARED
;
651 cx25821_free_buffer(q
, buf
);
656 void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
658 struct cx25821_buffer
*buf
= container_of(vb
, struct cx25821_buffer
, vb
);
660 cx25821_free_buffer(q
, buf
);
664 struct videobuf_queue
*get_queue(struct cx25821_fh
*fh
)
667 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
675 int get_resource(struct cx25821_fh
*fh
, int resource
)
678 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
687 int video_mmap(struct file
*file
, struct vm_area_struct
*vma
)
689 struct cx25821_fh
*fh
= file
->private_data
;
691 return videobuf_mmap_mapper(get_queue(fh
), vma
);
695 int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*f
)
697 struct cx25821_fh
*fh
= priv
;
699 f
->fmt
.pix
.width
= fh
->width
;
700 f
->fmt
.pix
.height
= fh
->height
;
701 f
->fmt
.pix
.field
= fh
->vidq
.field
;
702 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
703 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
704 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
709 int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*f
)
711 struct cx25821_fmt
*fmt
;
712 enum v4l2_field field
;
713 unsigned int maxw
, maxh
;
715 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
719 field
= f
->fmt
.pix
.field
;
723 if (V4L2_FIELD_ANY
== field
) {
724 field
= (f
->fmt
.pix
.height
> maxh
/2)
725 ? V4L2_FIELD_INTERLACED
731 case V4L2_FIELD_BOTTOM
:
734 case V4L2_FIELD_INTERLACED
:
740 f
->fmt
.pix
.field
= field
;
741 if (f
->fmt
.pix
.height
< 32)
742 f
->fmt
.pix
.height
= 32;
743 if (f
->fmt
.pix
.height
> maxh
)
744 f
->fmt
.pix
.height
= maxh
;
745 if (f
->fmt
.pix
.width
< 48)
746 f
->fmt
.pix
.width
= 48;
747 if (f
->fmt
.pix
.width
> maxw
)
748 f
->fmt
.pix
.width
= maxw
;
749 f
->fmt
.pix
.width
&= ~0x03;
750 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
751 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
758 int vidioc_querycap(struct file
*file
, void *priv
, struct v4l2_capability
*cap
)
760 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
762 strcpy(cap
->driver
, "cx25821");
763 strlcpy(cap
->card
, cx25821_boards
[dev
->board
].name
, sizeof(cap
->card
));
764 sprintf(cap
->bus_info
, "PCIe:%s", pci_name(dev
->pci
));
765 cap
->version
= CX25821_VERSION_CODE
;
767 V4L2_CAP_VIDEO_CAPTURE
|
770 if (UNSET
!= dev
->tuner_type
)
771 cap
->capabilities
|= V4L2_CAP_TUNER
;
775 int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_fmtdesc
*f
)
777 if (unlikely(f
->index
>= ARRAY_SIZE(formats
)))
780 strlcpy(f
->description
, formats
[f
->index
].name
, sizeof(f
->description
));
781 f
->pixelformat
= formats
[f
->index
].fourcc
;
786 #ifdef CONFIG_VIDEO_V4L1_COMPAT
787 int vidiocgmbuf(struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
789 struct cx25821_fh
*fh
= priv
;
790 struct videobuf_queue
*q
;
791 struct v4l2_requestbuffers req
;
796 memset(&req
, 0, sizeof(req
));
799 req
.memory
= V4L2_MEMORY_MMAP
;
800 err
= videobuf_reqbufs(q
, &req
);
804 mbuf
->frames
= req
.count
;
806 for (i
= 0; i
< mbuf
->frames
; i
++) {
807 mbuf
->offsets
[i
] = q
->bufs
[i
]->boff
;
808 mbuf
->size
+= q
->bufs
[i
]->bsize
;
814 int vidioc_reqbufs(struct file
*file
, void *priv
, struct v4l2_requestbuffers
*p
)
816 struct cx25821_fh
*fh
= priv
;
817 return videobuf_reqbufs(get_queue(fh
), p
);
820 int vidioc_querybuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
822 struct cx25821_fh
*fh
= priv
;
823 return videobuf_querybuf(get_queue(fh
), p
);
826 int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
828 struct cx25821_fh
*fh
= priv
;
829 return videobuf_qbuf(get_queue(fh
), p
);
832 int vidioc_g_priority(struct file
*file
, void *f
, enum v4l2_priority
*p
)
834 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)f
)->dev
;
836 *p
= v4l2_prio_max(&dev
->prio
);
841 int vidioc_s_priority(struct file
*file
, void *f
,
842 enum v4l2_priority prio
)
844 struct cx25821_fh
*fh
= f
;
845 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)f
)->dev
;
847 return v4l2_prio_change(&dev
->prio
, &fh
->prio
, prio
);
852 int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*tvnorms
)
854 struct cx25821_fh
*fh
= priv
;
855 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
858 dprintk(1, "%s()\n", __func__
);
862 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
867 if( dev
->tvnorm
== *tvnorms
)
872 mutex_lock(&dev
->lock
);
873 cx25821_set_tvnorm(dev
, *tvnorms
);
874 mutex_unlock(&dev
->lock
);
876 medusa_set_videostandard(dev
);
882 int cx25821_enum_input(struct cx25821_dev
*dev
, struct v4l2_input
*i
)
884 static const char *iname
[] = {
885 [CX25821_VMUX_COMPOSITE
] = "Composite",
886 [CX25821_VMUX_SVIDEO
] = "S-Video",
887 [CX25821_VMUX_DEBUG
] = "for debug only",
890 dprintk(1, "%s()\n", __func__
);
896 if (0 == INPUT(n
)->type
)
899 memset(i
, 0, sizeof(*i
));
901 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
902 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
904 i
->std
= CX25821_NORMS
;
908 int vidioc_enum_input(struct file
*file
, void *priv
, struct v4l2_input
*i
)
910 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
911 dprintk(1, "%s()\n", __func__
);
912 return cx25821_enum_input(dev
, i
);
915 int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
917 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
920 dprintk(1, "%s() returns %d\n", __func__
, *i
);
925 int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
927 struct cx25821_fh
*fh
= priv
;
928 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
931 dprintk(1, "%s(%d)\n", __func__
, i
);
935 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
941 dprintk(1, "%s() -EINVAL\n", __func__
);
945 mutex_lock(&dev
->lock
);
946 cx25821_video_mux(dev
, i
);
947 mutex_unlock(&dev
->lock
);
952 int vidioc_g_frequency(struct file
*file
, void *priv
, struct v4l2_frequency
*f
)
954 struct cx25821_fh
*fh
= priv
;
955 struct cx25821_dev
*dev
= fh
->dev
;
957 f
->frequency
= dev
->freq
;
959 cx25821_call_all(dev
, tuner
, g_frequency
, f
);
964 int cx25821_set_freq(struct cx25821_dev
*dev
, struct v4l2_frequency
*f
)
966 mutex_lock(&dev
->lock
);
967 dev
->freq
= f
->frequency
;
969 cx25821_call_all(dev
, tuner
, s_frequency
, f
);
971 /* When changing channels it is required to reset TVAUDIO */
974 mutex_unlock(&dev
->lock
);
979 int vidioc_s_frequency(struct file
*file
, void *priv
, struct v4l2_frequency
*f
)
981 struct cx25821_fh
*fh
= priv
;
982 struct cx25821_dev
*dev
= fh
->dev
;
987 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
992 return cx25821_set_freq(dev
, f
);
996 #ifdef CONFIG_VIDEO_ADV_DEBUG
997 int vidioc_g_register(struct file
*file
, void *fh
,
998 struct v4l2_dbg_register
*reg
)
1000 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)fh
)->dev
;
1002 if (!v4l2_chip_match_host(®
->match
))
1005 cx25821_call_all(dev
, core
, g_register
, reg
);
1010 int vidioc_s_register(struct file
*file
, void *fh
,
1011 struct v4l2_dbg_register
*reg
)
1013 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)fh
)->dev
;
1015 if (!v4l2_chip_match_host(®
->match
))
1018 cx25821_call_all(dev
, core
, s_register
, reg
);
1027 int vidioc_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
1029 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1031 if (unlikely(UNSET
== dev
->tuner_type
))
1036 strcpy(t
->name
, "Television");
1037 t
->type
= V4L2_TUNER_ANALOG_TV
;
1038 t
->capability
= V4L2_TUNER_CAP_NORM
;
1039 t
->rangehigh
= 0xffffffffUL
;
1041 t
->signal
= 0xffff ; /* LOCKED */
1045 int vidioc_s_tuner(struct file
*file
, void *priv
,
1046 struct v4l2_tuner
*t
)
1048 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1049 struct cx25821_fh
*fh
= priv
;
1054 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
1059 dprintk(1, "%s()\n", __func__
);
1060 if (UNSET
== dev
->tuner_type
)
1069 // ******************************************************************************************
1070 static const struct v4l2_queryctrl no_ctl
= {
1072 .flags
= V4L2_CTRL_FLAG_DISABLED
,
1075 static struct v4l2_queryctrl cx25821_ctls
[] = {
1078 .id
= V4L2_CID_BRIGHTNESS
,
1079 .name
= "Brightness",
1083 .default_value
= 6200,
1084 .type
= V4L2_CTRL_TYPE_INTEGER
,
1086 .id
= V4L2_CID_CONTRAST
,
1091 .default_value
= 5000,
1092 .type
= V4L2_CTRL_TYPE_INTEGER
,
1094 .id
= V4L2_CID_SATURATION
,
1095 .name
= "Saturation",
1099 .default_value
= 5000,
1100 .type
= V4L2_CTRL_TYPE_INTEGER
,
1107 .default_value
= 5000,
1108 .type
= V4L2_CTRL_TYPE_INTEGER
,
1111 static const int CX25821_CTLS
= ARRAY_SIZE(cx25821_ctls
);
1113 static int cx25821_ctrl_query(struct v4l2_queryctrl
*qctrl
)
1117 if (qctrl
->id
< V4L2_CID_BASE
||
1118 qctrl
->id
>= V4L2_CID_LASTP1
)
1120 for (i
= 0; i
< CX25821_CTLS
; i
++)
1121 if (cx25821_ctls
[i
].id
== qctrl
->id
)
1123 if (i
== CX25821_CTLS
) {
1127 *qctrl
= cx25821_ctls
[i
];
1131 int vidioc_queryctrl(struct file
*file
, void *priv
,
1132 struct v4l2_queryctrl
*qctrl
)
1134 return cx25821_ctrl_query(qctrl
);
1137 /* ------------------------------------------------------------------ */
1138 /* VIDEO CTRL IOCTLS */
1140 static const struct v4l2_queryctrl
* ctrl_by_id(unsigned int id
)
1144 for (i
= 0; i
< CX25821_CTLS
; i
++)
1145 if (cx25821_ctls
[i
].id
== id
)
1146 return cx25821_ctls
+i
;
1150 int vidioc_g_ctrl(struct file
*file
,
1152 struct v4l2_control
*ctl
)
1154 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1156 const struct v4l2_queryctrl
* ctrl
;
1158 ctrl
= ctrl_by_id(ctl
->id
);
1164 case V4L2_CID_BRIGHTNESS
:
1165 ctl
->value
= dev
->ctl_bright
;
1168 ctl
->value
= dev
->ctl_hue
;
1170 case V4L2_CID_CONTRAST
:
1171 ctl
->value
= dev
->ctl_contrast
;
1173 case V4L2_CID_SATURATION
:
1174 ctl
->value
= dev
->ctl_saturation
;
1180 int cx25821_set_control(struct cx25821_dev
*dev
,
1181 struct v4l2_control
*ctl
, int chan_num
)
1184 const struct v4l2_queryctrl
* ctrl
;
1188 ctrl
= ctrl_by_id(ctl
->id
);
1195 case V4L2_CTRL_TYPE_BOOLEAN
:
1196 case V4L2_CTRL_TYPE_MENU
:
1197 case V4L2_CTRL_TYPE_INTEGER
:
1198 if (ctl
->value
< ctrl
->minimum
)
1199 ctl
->value
= ctrl
->minimum
;
1200 if (ctl
->value
> ctrl
->maximum
)
1201 ctl
->value
= ctrl
->maximum
;
1209 case V4L2_CID_BRIGHTNESS
:
1210 dev
->ctl_bright
= ctl
->value
;
1211 medusa_set_brightness(dev
, ctl
->value
, chan_num
);
1214 dev
->ctl_hue
= ctl
->value
;
1215 medusa_set_hue(dev
, ctl
->value
, chan_num
);
1217 case V4L2_CID_CONTRAST
:
1218 dev
->ctl_contrast
= ctl
->value
;
1219 medusa_set_contrast(dev
, ctl
->value
, chan_num
);
1221 case V4L2_CID_SATURATION
:
1222 dev
->ctl_saturation
= ctl
->value
;
1223 medusa_set_saturation(dev
, ctl
->value
, chan_num
);
1232 static void init_controls(struct cx25821_dev
*dev
, int chan_num
)
1234 struct v4l2_control ctrl
;
1236 for (i
= 0; i
< CX25821_CTLS
; i
++) {
1237 ctrl
.id
= cx25821_ctls
[i
].id
;
1238 ctrl
.value
= cx25821_ctls
[i
].default_value
;
1240 cx25821_set_control(dev
, &ctrl
, chan_num
);
1244 int vidioc_cropcap(struct file
*file
,
1246 struct v4l2_cropcap
*cropcap
)
1248 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1250 if (cropcap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1252 cropcap
->bounds
.top
= cropcap
->bounds
.left
= 0;
1253 cropcap
->bounds
.width
= 720;
1254 cropcap
->bounds
.height
= dev
->tvnorm
== V4L2_STD_PAL_BG
? 576 : 480;
1255 cropcap
->pixelaspect
.numerator
= dev
->tvnorm
== V4L2_STD_PAL_BG
? 59 : 10;
1256 cropcap
->pixelaspect
.denominator
= dev
->tvnorm
== V4L2_STD_PAL_BG
? 54 : 11;
1257 cropcap
->defrect
= cropcap
->bounds
;
1261 int vidioc_s_crop(struct file
*file
,
1263 struct v4l2_crop
*crop
)
1265 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1266 struct cx25821_fh
*fh
= priv
;
1271 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
1275 // vidioc_s_crop not supported
1279 int vidioc_g_crop(struct file
*file
,
1281 struct v4l2_crop
*crop
)
1283 // vidioc_g_crop not supported
1287 int vidioc_querystd(struct file
*file
,
1291 // medusa does not support video standard sensing of current input
1292 *norm
= CX25821_NORMS
;
1297 int is_valid_width(u32 width
, v4l2_std_id tvnorm
)
1299 if(tvnorm
== V4L2_STD_PAL_BG
)
1301 if (width
== 352 || width
== 720)
1307 if(tvnorm
== V4L2_STD_NTSC_M
)
1309 if (width
== 320 || width
== 352 || width
== 720)
1317 int is_valid_height(u32 height
, v4l2_std_id tvnorm
)
1319 if(tvnorm
== V4L2_STD_PAL_BG
)
1321 if (height
== 576 || height
== 288)
1327 if(tvnorm
== V4L2_STD_NTSC_M
)
1329 if (height
== 480 || height
== 240)