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
) {
105 for (i
= 0; i
< ARRAY_SIZE(formats
); i
++)
106 if (formats
[i
].fourcc
== fourcc
)
109 pr_err("%s(0x%08x) NOT FOUND\n", __func__
, fourcc
);
113 void cx25821_dump_video_queue(struct cx25821_dev
*dev
, 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) {
153 do_gettimeofday(&buf
->vb
.ts
);
154 buf
->vb
.state
= VIDEOBUF_DONE
;
155 list_del(&buf
->vb
.queue
);
156 wake_up(&buf
->vb
.done
);
159 if (list_empty(&q
->active
))
160 del_timer(&q
->timeout
);
162 mod_timer(&q
->timeout
, jiffies
+ BUFFER_TIMEOUT
);
164 pr_err("%s: %d buffers handled (should be 1)\n",
169 int cx25821_set_tvnorm(struct cx25821_dev
*dev
, v4l2_std_id norm
)
171 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
172 __func__
, (unsigned int)norm
, v4l2_norm_to_name(norm
));
176 /* Tell the internal A/V decoder */
177 cx25821_call_all(dev
, core
, s_std
, norm
);
183 struct video_device
*cx25821_vdev_init(struct cx25821_dev
*dev
,
185 struct video_device
*template,
188 struct video_device
*vfd
;
189 dprintk(1, "%s()\n", __func__
);
191 vfd
= video_device_alloc();
195 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
196 vfd
->release
= video_device_release
;
197 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s (%s)", dev
->name
, type
,
198 cx25821_boards
[dev
->board
].name
);
199 video_set_drvdata(vfd
, dev
);
204 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
208 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
210 for (i = 0; i < CX25821_CTLS; i++)
211 if (cx25821_ctls[i].v.id == qctrl->id)
213 if (i == CX25821_CTLS) {
217 *qctrl = cx25821_ctls[i].v;
222 /* resource management */
223 int cx25821_res_get(struct cx25821_dev
*dev
, struct cx25821_fh
*fh
, unsigned int bit
)
225 dprintk(1, "%s()\n", __func__
);
226 if (fh
->resources
& bit
)
227 /* have it already allocated */
231 mutex_lock(&dev
->lock
);
232 if (dev
->channels
[fh
->channel_id
].resources
& bit
) {
233 /* no, someone else uses it */
234 mutex_unlock(&dev
->lock
);
237 /* it's free, grab it */
238 fh
->resources
|= bit
;
239 dev
->channels
[fh
->channel_id
].resources
|= bit
;
240 dprintk(1, "res: get %d\n", bit
);
241 mutex_unlock(&dev
->lock
);
245 int cx25821_res_check(struct cx25821_fh
*fh
, unsigned int bit
)
247 return fh
->resources
& bit
;
250 int cx25821_res_locked(struct cx25821_fh
*fh
, unsigned int bit
)
252 return fh
->dev
->channels
[fh
->channel_id
].resources
& bit
;
255 void cx25821_res_free(struct cx25821_dev
*dev
, struct cx25821_fh
*fh
, unsigned int bits
)
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
,
416 &dev
->channels
[channel
->i
].vidq
, count
);
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
,
428 spin_unlock(&dev
->slock
);
434 void cx25821_videoioctl_unregister(struct cx25821_dev
*dev
)
436 if (dev
->ioctl_dev
) {
437 if (video_is_registered(dev
->ioctl_dev
))
438 video_unregister_device(dev
->ioctl_dev
);
440 video_device_release(dev
->ioctl_dev
);
442 dev
->ioctl_dev
= NULL
;
446 void cx25821_video_unregister(struct cx25821_dev
*dev
, int chan_num
)
448 cx_clear(PCI_INT_MSK
, 1);
450 if (dev
->channels
[chan_num
].video_dev
) {
451 if (video_is_registered(dev
->channels
[chan_num
].video_dev
))
452 video_unregister_device(
453 dev
->channels
[chan_num
].video_dev
);
455 video_device_release(
456 dev
->channels
[chan_num
].video_dev
);
458 dev
->channels
[chan_num
].video_dev
= NULL
;
460 btcx_riscmem_free(dev
->pci
,
461 &dev
->channels
[chan_num
].vidq
.stopper
);
463 pr_warn("device %d released!\n", chan_num
);
468 int cx25821_video_register(struct cx25821_dev
*dev
)
473 struct video_device cx25821_video_device
= {
474 .name
= "cx25821-video",
477 .ioctl_ops
= &video_ioctl_ops
,
478 .tvnorms
= CX25821_NORMS
,
479 .current_norm
= V4L2_STD_NTSC_M
,
482 spin_lock_init(&dev
->slock
);
484 for (i
= 0; i
< MAX_VID_CHANNEL_NUM
- 1; ++i
) {
485 cx25821_init_controls(dev
, i
);
487 cx25821_risc_stopper(dev
->pci
,
488 &dev
->channels
[i
].vidq
.stopper
,
489 dev
->channels
[i
].sram_channels
->dma_ctl
,
492 dev
->channels
[i
].sram_channels
= &cx25821_sram_channels
[i
];
493 dev
->channels
[i
].video_dev
= NULL
;
494 dev
->channels
[i
].resources
= 0;
496 cx_write(dev
->channels
[i
].sram_channels
->int_stat
,
499 INIT_LIST_HEAD(&dev
->channels
[i
].vidq
.active
);
500 INIT_LIST_HEAD(&dev
->channels
[i
].vidq
.queued
);
502 dev
->channels
[i
].timeout_data
.dev
= dev
;
503 dev
->channels
[i
].timeout_data
.channel
=
504 &cx25821_sram_channels
[i
];
505 dev
->channels
[i
].vidq
.timeout
.function
=
507 dev
->channels
[i
].vidq
.timeout
.data
=
508 (unsigned long)&dev
->channels
[i
].timeout_data
;
509 init_timer(&dev
->channels
[i
].vidq
.timeout
);
511 /* register v4l devices */
512 dev
->channels
[i
].video_dev
= cx25821_vdev_init(dev
,
513 dev
->pci
, &cx25821_video_device
, "video");
515 err
= video_register_device(dev
->channels
[i
].video_dev
,
516 VFL_TYPE_GRABBER
, video_nr
[dev
->nr
]);
523 /* set PCI interrupt */
524 cx_set(PCI_INT_MSK
, 0xff);
526 /* initial device configuration */
527 mutex_lock(&dev
->lock
);
529 dev
->tvnorm
= cx25821_video_device
.current_norm
;
530 cx25821_set_tvnorm(dev
, dev
->tvnorm
);
532 mutex_unlock(&dev
->lock
);
538 cx25821_video_unregister(dev
, i
);
542 int cx25821_buffer_setup(struct videobuf_queue
*q
, unsigned int *count
,
545 struct cx25821_fh
*fh
= q
->priv_data
;
547 *size
= fh
->fmt
->depth
* fh
->width
* fh
->height
>> 3;
552 if (*size
* *count
> vid_limit
* 1024 * 1024)
553 *count
= (vid_limit
* 1024 * 1024) / *size
;
558 int cx25821_buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
559 enum v4l2_field field
)
561 struct cx25821_fh
*fh
= q
->priv_data
;
562 struct cx25821_dev
*dev
= fh
->dev
;
563 struct cx25821_buffer
*buf
=
564 container_of(vb
, struct cx25821_buffer
, vb
);
565 int rc
, init_buffer
= 0;
566 u32 line0_offset
, line1_offset
;
567 struct videobuf_dmabuf
*dma
= videobuf_to_dma(&buf
->vb
);
568 int bpl_local
= LINE_SIZE_D1
;
569 int channel_opened
= fh
->channel_id
;
571 BUG_ON(NULL
== fh
->fmt
);
572 if (fh
->width
< 48 || fh
->width
> 720 ||
573 fh
->height
< 32 || fh
->height
> 576)
576 buf
->vb
.size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
578 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
581 if (buf
->fmt
!= fh
->fmt
||
582 buf
->vb
.width
!= fh
->width
||
583 buf
->vb
.height
!= fh
->height
|| buf
->vb
.field
!= field
) {
585 buf
->vb
.width
= fh
->width
;
586 buf
->vb
.height
= fh
->height
;
587 buf
->vb
.field
= field
;
591 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
593 rc
= videobuf_iolock(q
, &buf
->vb
, NULL
);
595 printk(KERN_DEBUG
pr_fmt("videobuf_iolock failed!\n"));
600 dprintk(1, "init_buffer=%d\n", init_buffer
);
604 channel_opened
= dev
->channel_opened
;
605 channel_opened
= (channel_opened
< 0
606 || channel_opened
> 7) ? 7 : channel_opened
;
608 if (dev
->channels
[channel_opened
]
609 .pixel_formats
== PIXEL_FRMT_411
)
610 buf
->bpl
= (buf
->fmt
->depth
* buf
->vb
.width
) >> 3;
612 buf
->bpl
= (buf
->fmt
->depth
>> 3) * (buf
->vb
.width
);
614 if (dev
->channels
[channel_opened
]
615 .pixel_formats
== PIXEL_FRMT_411
) {
616 bpl_local
= buf
->bpl
;
618 bpl_local
= buf
->bpl
; /* Default */
620 if (channel_opened
>= 0 && channel_opened
<= 7) {
621 if (dev
->channels
[channel_opened
]
622 .use_cif_resolution
) {
623 if (dev
->tvnorm
& V4L2_STD_PAL_BG
624 || dev
->tvnorm
& V4L2_STD_PAL_DK
)
625 bpl_local
= 352 << 1;
628 dev
->channels
[channel_opened
].
635 switch (buf
->vb
.field
) {
637 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
638 dma
->sglist
, 0, UNSET
,
639 buf
->bpl
, 0, buf
->vb
.height
);
641 case V4L2_FIELD_BOTTOM
:
642 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
643 dma
->sglist
, UNSET
, 0,
644 buf
->bpl
, 0, buf
->vb
.height
);
646 case V4L2_FIELD_INTERLACED
:
647 /* All other formats are top field first */
649 line1_offset
= buf
->bpl
;
650 dprintk(1, "top field first\n");
652 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
653 dma
->sglist
, line0_offset
,
654 bpl_local
, bpl_local
, bpl_local
,
655 buf
->vb
.height
>> 1);
657 case V4L2_FIELD_SEQ_TB
:
658 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
660 0, buf
->bpl
* (buf
->vb
.height
>> 1),
661 buf
->bpl
, 0, buf
->vb
.height
>> 1);
663 case V4L2_FIELD_SEQ_BT
:
664 cx25821_risc_buffer(dev
->pci
, &buf
->risc
,
666 buf
->bpl
* (buf
->vb
.height
>> 1), 0,
667 buf
->bpl
, 0, buf
->vb
.height
>> 1);
674 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
675 buf
, buf
->vb
.i
, fh
->width
, fh
->height
, fh
->fmt
->depth
,
676 fh
->fmt
->name
, (unsigned long)buf
->risc
.dma
);
678 buf
->vb
.state
= VIDEOBUF_PREPARED
;
683 cx25821_free_buffer(q
, buf
);
687 void cx25821_buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
689 struct cx25821_buffer
*buf
=
690 container_of(vb
, struct cx25821_buffer
, vb
);
692 cx25821_free_buffer(q
, buf
);
695 struct videobuf_queue
*get_queue(struct cx25821_fh
*fh
)
698 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
706 int cx25821_get_resource(struct cx25821_fh
*fh
, int resource
)
709 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
717 int cx25821_video_mmap(struct file
*file
, struct vm_area_struct
*vma
)
719 struct cx25821_fh
*fh
= file
->private_data
;
721 return videobuf_mmap_mapper(get_queue(fh
), vma
);
725 static void buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
727 struct cx25821_buffer
*buf
=
728 container_of(vb
, struct cx25821_buffer
, vb
);
729 struct cx25821_buffer
*prev
;
730 struct cx25821_fh
*fh
= vq
->priv_data
;
731 struct cx25821_dev
*dev
= fh
->dev
;
732 struct cx25821_dmaqueue
*q
= &dev
->channels
[fh
->channel_id
].vidq
;
734 /* add jump to stopper */
735 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| RISC_CNT_INC
);
736 buf
->risc
.jmp
[1] = cpu_to_le32(q
->stopper
.dma
);
737 buf
->risc
.jmp
[2] = cpu_to_le32(0); /* bits 63-32 */
739 dprintk(2, "jmp to stopper (0x%x)\n", buf
->risc
.jmp
[1]);
741 if (!list_empty(&q
->queued
)) {
742 list_add_tail(&buf
->vb
.queue
, &q
->queued
);
743 buf
->vb
.state
= VIDEOBUF_QUEUED
;
744 dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf
,
747 } else if (list_empty(&q
->active
)) {
748 list_add_tail(&buf
->vb
.queue
, &q
->active
);
749 cx25821_start_video_dma(dev
, q
, buf
,
750 dev
->channels
[fh
->channel_id
].
752 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
753 buf
->count
= q
->count
++;
754 mod_timer(&q
->timeout
, jiffies
+ BUFFER_TIMEOUT
);
756 "[%p/%d] buffer_queue - first active, buf cnt = %d, \
758 buf
, buf
->vb
.i
, buf
->count
, q
->count
);
761 list_entry(q
->active
.prev
, struct cx25821_buffer
, vb
.queue
);
762 if (prev
->vb
.width
== buf
->vb
.width
763 && prev
->vb
.height
== buf
->vb
.height
764 && prev
->fmt
== buf
->fmt
) {
765 list_add_tail(&buf
->vb
.queue
, &q
->active
);
766 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
767 buf
->count
= q
->count
++;
768 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
770 /* 64 bit bits 63-32 */
771 prev
->risc
.jmp
[2] = cpu_to_le32(0);
773 "[%p/%d] buffer_queue - append to active, \
775 buf
, buf
->vb
.i
, buf
->count
);
778 list_add_tail(&buf
->vb
.queue
, &q
->queued
);
779 buf
->vb
.state
= VIDEOBUF_QUEUED
;
780 dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf
,
785 if (list_empty(&q
->active
))
786 dprintk(2, "active queue empty!\n");
789 static struct videobuf_queue_ops cx25821_video_qops
= {
790 .buf_setup
= cx25821_buffer_setup
,
791 .buf_prepare
= cx25821_buffer_prepare
,
792 .buf_queue
= buffer_queue
,
793 .buf_release
= cx25821_buffer_release
,
796 static int video_open(struct file
*file
)
798 struct video_device
*vdev
= video_devdata(file
);
799 struct cx25821_dev
*h
, *dev
= video_drvdata(file
);
800 struct cx25821_fh
*fh
;
801 struct list_head
*list
;
802 int minor
= video_devdata(file
)->minor
;
803 enum v4l2_buf_type type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
808 dprintk(1, "open dev=%s type=%s\n",
809 video_device_node_name(vdev
),
810 v4l2_type_names
[type
]);
812 /* allocate + initialize per filehandle data */
813 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
817 mutex_lock(&cx25821_devlist_mutex
);
819 list_for_each(list
, &cx25821_devlist
)
821 h
= list_entry(list
, struct cx25821_dev
, devlist
);
823 for (i
= 0; i
< MAX_VID_CHANNEL_NUM
; i
++) {
824 if (h
->channels
[i
].video_dev
&&
825 h
->channels
[i
].video_dev
->minor
== minor
) {
828 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
834 mutex_unlock(&cx25821_devlist_mutex
);
838 file
->private_data
= fh
;
842 fh
->channel_id
= ch_id
;
844 if (dev
->tvnorm
& V4L2_STD_PAL_BG
|| dev
->tvnorm
& V4L2_STD_PAL_DK
)
849 dev
->channel_opened
= fh
->channel_id
;
851 (dev
->channels
[ch_id
].pixel_formats
==
852 PIXEL_FRMT_411
) ? V4L2_PIX_FMT_Y41P
: V4L2_PIX_FMT_YUYV
;
853 fh
->fmt
= cx25821_format_by_fourcc(pix_format
);
855 v4l2_prio_open(&dev
->channels
[ch_id
].prio
, &fh
->prio
);
857 videobuf_queue_sg_init(&fh
->vidq
, &cx25821_video_qops
,
858 &dev
->pci
->dev
, &dev
->slock
,
859 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
860 V4L2_FIELD_INTERLACED
,
861 sizeof(struct cx25821_buffer
), fh
, NULL
);
863 dprintk(1, "post videobuf_queue_init()\n");
864 mutex_unlock(&cx25821_devlist_mutex
);
869 static ssize_t
video_read(struct file
*file
, char __user
* data
, size_t count
,
872 struct cx25821_fh
*fh
= file
->private_data
;
875 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
876 if (cx25821_res_locked(fh
, RESOURCE_VIDEO0
))
879 return videobuf_read_one(&fh
->vidq
, data
, count
, ppos
,
880 file
->f_flags
& O_NONBLOCK
);
888 static unsigned int video_poll(struct file
*file
,
889 struct poll_table_struct
*wait
)
891 struct cx25821_fh
*fh
= file
->private_data
;
892 struct cx25821_buffer
*buf
;
894 if (cx25821_res_check(fh
, RESOURCE_VIDEO0
)) {
895 /* streaming capture */
896 if (list_empty(&fh
->vidq
.stream
))
898 buf
= list_entry(fh
->vidq
.stream
.next
,
899 struct cx25821_buffer
, vb
.stream
);
902 buf
= (struct cx25821_buffer
*)fh
->vidq
.read_buf
;
907 poll_wait(file
, &buf
->vb
.done
, wait
);
908 if (buf
->vb
.state
== VIDEOBUF_DONE
|| buf
->vb
.state
== VIDEOBUF_ERROR
) {
909 if (buf
->vb
.state
== VIDEOBUF_DONE
) {
910 struct cx25821_dev
*dev
= fh
->dev
;
912 if (dev
&& dev
->channels
[fh
->channel_id
]
913 .use_cif_resolution
) {
914 u8 cam_id
= *((char *)buf
->vb
.baddr
+ 3);
915 memcpy((char *)buf
->vb
.baddr
,
916 (char *)buf
->vb
.baddr
+ (fh
->width
* 2),
918 *((char *)buf
->vb
.baddr
+ 3) = cam_id
;
922 return POLLIN
| POLLRDNORM
;
928 static int video_release(struct file
*file
)
930 struct cx25821_fh
*fh
= file
->private_data
;
931 struct cx25821_dev
*dev
= fh
->dev
;
933 /* stop the risc engine and fifo */
934 cx_write(channel0
->dma_ctl
, 0); /* FIFO and RISC disable */
936 /* stop video capture */
937 if (cx25821_res_check(fh
, RESOURCE_VIDEO0
)) {
938 videobuf_queue_cancel(&fh
->vidq
);
939 cx25821_res_free(dev
, fh
, RESOURCE_VIDEO0
);
942 if (fh
->vidq
.read_buf
) {
943 cx25821_buffer_release(&fh
->vidq
, fh
->vidq
.read_buf
);
944 kfree(fh
->vidq
.read_buf
);
947 videobuf_mmap_free(&fh
->vidq
);
949 v4l2_prio_close(&dev
->channels
[fh
->channel_id
].prio
, fh
->prio
);
950 file
->private_data
= NULL
;
956 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
958 struct cx25821_fh
*fh
= priv
;
959 struct cx25821_dev
*dev
= fh
->dev
;
961 if (unlikely(fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
))
964 if (unlikely(i
!= fh
->type
))
967 if (unlikely(!cx25821_res_get(dev
, fh
,
968 cx25821_get_resource(fh
, RESOURCE_VIDEO0
))))
971 return videobuf_streamon(get_queue(fh
));
974 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
976 struct cx25821_fh
*fh
= priv
;
977 struct cx25821_dev
*dev
= fh
->dev
;
980 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
985 res
= cx25821_get_resource(fh
, RESOURCE_VIDEO0
);
986 err
= videobuf_streamoff(get_queue(fh
));
989 cx25821_res_free(dev
, fh
, res
);
993 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
994 struct v4l2_format
*f
)
996 struct cx25821_fh
*fh
= priv
;
997 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
998 struct v4l2_mbus_framefmt mbus_fmt
;
1000 int pix_format
= PIXEL_FRMT_422
;
1003 err
= v4l2_prio_check(&dev
->channels
[fh
->channel_id
]
1009 dprintk(2, "%s()\n", __func__
);
1010 err
= cx25821_vidioc_try_fmt_vid_cap(file
, priv
, f
);
1015 fh
->fmt
= cx25821_format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1016 fh
->vidq
.field
= f
->fmt
.pix
.field
;
1018 /* check if width and height is valid based on set standard */
1019 if (cx25821_is_valid_width(f
->fmt
.pix
.width
, dev
->tvnorm
))
1020 fh
->width
= f
->fmt
.pix
.width
;
1022 if (cx25821_is_valid_height(f
->fmt
.pix
.height
, dev
->tvnorm
))
1023 fh
->height
= f
->fmt
.pix
.height
;
1025 if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_Y41P
)
1026 pix_format
= PIXEL_FRMT_411
;
1027 else if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_YUYV
)
1028 pix_format
= PIXEL_FRMT_422
;
1032 cx25821_set_pixel_format(dev
, SRAM_CH00
, pix_format
);
1034 /* check if cif resolution */
1035 if (fh
->width
== 320 || fh
->width
== 352)
1036 dev
->channels
[fh
->channel_id
].use_cif_resolution
= 1;
1038 dev
->channels
[fh
->channel_id
].use_cif_resolution
= 0;
1040 dev
->channels
[fh
->channel_id
].cif_width
= fh
->width
;
1041 medusa_set_resolution(dev
, fh
->width
, SRAM_CH00
);
1043 dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__
, fh
->width
,
1044 fh
->height
, fh
->vidq
.field
);
1045 v4l2_fill_mbus_format(&mbus_fmt
, &f
->fmt
.pix
, V4L2_MBUS_FMT_FIXED
);
1046 cx25821_call_all(dev
, video
, s_mbus_fmt
, &mbus_fmt
);
1051 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1054 struct cx25821_fh
*fh
= priv
;
1055 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1057 ret_val
= videobuf_dqbuf(get_queue(fh
), p
, file
->f_flags
& O_NONBLOCK
);
1059 p
->sequence
= dev
->channels
[fh
->channel_id
].vidq
.count
;
1064 static int vidioc_log_status(struct file
*file
, void *priv
)
1066 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1067 struct cx25821_fh
*fh
= priv
;
1070 struct sram_channel
*sram_ch
= dev
->channels
[fh
->channel_id
]
1074 snprintf(name
, sizeof(name
), "%s/2", dev
->name
);
1075 pr_info("%s/2: ============ START LOG STATUS ============\n",
1077 cx25821_call_all(dev
, core
, log_status
);
1078 tmp
= cx_read(sram_ch
->dma_ctl
);
1079 pr_info("Video input 0 is %s\n",
1080 (tmp
& 0x11) ? "streaming" : "stopped");
1081 pr_info("%s/2: ============= END LOG STATUS =============\n",
1086 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1087 struct v4l2_control
*ctl
)
1089 struct cx25821_fh
*fh
= priv
;
1090 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1094 err
= v4l2_prio_check(&dev
->channels
[fh
->channel_id
]
1100 return cx25821_set_control(dev
, ctl
, fh
->channel_id
);
1104 int cx25821_vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*f
)
1106 struct cx25821_fh
*fh
= priv
;
1108 f
->fmt
.pix
.width
= fh
->width
;
1109 f
->fmt
.pix
.height
= fh
->height
;
1110 f
->fmt
.pix
.field
= fh
->vidq
.field
;
1111 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
1112 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
1113 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1118 int cx25821_vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
, struct v4l2_format
*f
)
1120 struct cx25821_fmt
*fmt
;
1121 enum v4l2_field field
;
1122 unsigned int maxw
, maxh
;
1124 fmt
= cx25821_format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1128 field
= f
->fmt
.pix
.field
;
1132 if (V4L2_FIELD_ANY
== field
) {
1133 field
= (f
->fmt
.pix
.height
> maxh
/ 2)
1134 ? V4L2_FIELD_INTERLACED
: V4L2_FIELD_TOP
;
1138 case V4L2_FIELD_TOP
:
1139 case V4L2_FIELD_BOTTOM
:
1142 case V4L2_FIELD_INTERLACED
:
1148 f
->fmt
.pix
.field
= field
;
1149 if (f
->fmt
.pix
.height
< 32)
1150 f
->fmt
.pix
.height
= 32;
1151 if (f
->fmt
.pix
.height
> maxh
)
1152 f
->fmt
.pix
.height
= maxh
;
1153 if (f
->fmt
.pix
.width
< 48)
1154 f
->fmt
.pix
.width
= 48;
1155 if (f
->fmt
.pix
.width
> maxw
)
1156 f
->fmt
.pix
.width
= maxw
;
1157 f
->fmt
.pix
.width
&= ~0x03;
1158 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1159 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1164 int cx25821_vidioc_querycap(struct file
*file
, void *priv
, struct v4l2_capability
*cap
)
1166 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1168 strcpy(cap
->driver
, "cx25821");
1169 strlcpy(cap
->card
, cx25821_boards
[dev
->board
].name
, sizeof(cap
->card
));
1170 sprintf(cap
->bus_info
, "PCIe:%s", pci_name(dev
->pci
));
1171 cap
->version
= CX25821_VERSION_CODE
;
1173 V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
;
1174 if (UNSET
!= dev
->tuner_type
)
1175 cap
->capabilities
|= V4L2_CAP_TUNER
;
1179 int cx25821_vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1180 struct v4l2_fmtdesc
*f
)
1182 if (unlikely(f
->index
>= ARRAY_SIZE(formats
)))
1185 strlcpy(f
->description
, formats
[f
->index
].name
, sizeof(f
->description
));
1186 f
->pixelformat
= formats
[f
->index
].fourcc
;
1191 int cx25821_vidioc_reqbufs(struct file
*file
, void *priv
, 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
, struct v4l2_buffer
*p
)
1199 struct cx25821_fh
*fh
= priv
;
1200 return videobuf_querybuf(get_queue(fh
), p
);
1203 int cx25821_vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1205 struct cx25821_fh
*fh
= priv
;
1206 return videobuf_qbuf(get_queue(fh
), p
);
1209 int cx25821_vidioc_g_priority(struct file
*file
, void *f
, enum v4l2_priority
*p
)
1211 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)f
)->dev
;
1212 struct cx25821_fh
*fh
= f
;
1214 *p
= v4l2_prio_max(&dev
->channels
[fh
->channel_id
].prio
);
1219 int cx25821_vidioc_s_priority(struct file
*file
, void *f
, enum v4l2_priority prio
)
1221 struct cx25821_fh
*fh
= f
;
1222 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)f
)->dev
;
1224 return v4l2_prio_change(&dev
->channels
[fh
->channel_id
]
1225 .prio
, &fh
->prio
, prio
);
1229 int cx25821_vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
* tvnorms
)
1231 struct cx25821_fh
*fh
= priv
;
1232 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1235 dprintk(1, "%s()\n", __func__
);
1238 err
= v4l2_prio_check(&dev
->channels
[fh
->channel_id
]
1244 if (dev
->tvnorm
== *tvnorms
) {
1248 mutex_lock(&dev
->lock
);
1249 cx25821_set_tvnorm(dev
, *tvnorms
);
1250 mutex_unlock(&dev
->lock
);
1252 medusa_set_videostandard(dev
);
1258 int cx25821_enum_input(struct cx25821_dev
*dev
, struct v4l2_input
*i
)
1260 static const char *iname
[] = {
1261 [CX25821_VMUX_COMPOSITE
] = "Composite",
1262 [CX25821_VMUX_SVIDEO
] = "S-Video",
1263 [CX25821_VMUX_DEBUG
] = "for debug only",
1266 dprintk(1, "%s()\n", __func__
);
1272 if (0 == INPUT(n
)->type
)
1275 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1276 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
1278 i
->std
= CX25821_NORMS
;
1282 int cx25821_vidioc_enum_input(struct file
*file
, void *priv
, struct v4l2_input
*i
)
1284 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1285 dprintk(1, "%s()\n", __func__
);
1286 return cx25821_enum_input(dev
, i
);
1289 int cx25821_vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1291 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1294 dprintk(1, "%s(): returns %d\n", __func__
, *i
);
1298 int cx25821_vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1300 struct cx25821_fh
*fh
= priv
;
1301 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1304 dprintk(1, "%s(%d)\n", __func__
, i
);
1307 err
= v4l2_prio_check(&dev
->channels
[fh
->channel_id
]
1314 dprintk(1, "%s(): -EINVAL\n", __func__
);
1318 mutex_lock(&dev
->lock
);
1319 cx25821_video_mux(dev
, i
);
1320 mutex_unlock(&dev
->lock
);
1325 int cx25821_vidioc_g_frequency(struct file
*file
, void *priv
, struct v4l2_frequency
*f
)
1327 struct cx25821_fh
*fh
= priv
;
1328 struct cx25821_dev
*dev
= fh
->dev
;
1330 f
->frequency
= dev
->freq
;
1332 cx25821_call_all(dev
, tuner
, g_frequency
, f
);
1337 int cx25821_set_freq(struct cx25821_dev
*dev
, struct v4l2_frequency
*f
)
1339 mutex_lock(&dev
->lock
);
1340 dev
->freq
= f
->frequency
;
1342 cx25821_call_all(dev
, tuner
, s_frequency
, f
);
1344 /* When changing channels it is required to reset TVAUDIO */
1347 mutex_unlock(&dev
->lock
);
1352 int cx25821_vidioc_s_frequency(struct file
*file
, void *priv
, struct v4l2_frequency
*f
)
1354 struct cx25821_fh
*fh
= priv
;
1355 struct cx25821_dev
*dev
;
1360 err
= v4l2_prio_check(&dev
->channels
[fh
->channel_id
]
1365 pr_err("Invalid fh pointer!\n");
1369 return cx25821_set_freq(dev
, f
);
1373 #ifdef CONFIG_VIDEO_ADV_DEBUG
1374 int cx25821_vidioc_g_register(struct file
*file
, void *fh
,
1375 struct v4l2_dbg_register
*reg
)
1377 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)fh
)->dev
;
1379 if (!v4l2_chip_match_host(®
->match
))
1382 cx25821_call_all(dev
, core
, g_register
, reg
);
1387 int cx25821_vidioc_s_register(struct file
*file
, void *fh
,
1388 struct v4l2_dbg_register
*reg
)
1390 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)fh
)->dev
;
1392 if (!v4l2_chip_match_host(®
->match
))
1395 cx25821_call_all(dev
, core
, s_register
, reg
);
1403 int cx25821_vidioc_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
1405 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1407 if (unlikely(UNSET
== dev
->tuner_type
))
1412 strcpy(t
->name
, "Television");
1413 t
->type
= V4L2_TUNER_ANALOG_TV
;
1414 t
->capability
= V4L2_TUNER_CAP_NORM
;
1415 t
->rangehigh
= 0xffffffffUL
;
1417 t
->signal
= 0xffff; /* LOCKED */
1421 int cx25821_vidioc_s_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
1423 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1424 struct cx25821_fh
*fh
= priv
;
1428 err
= v4l2_prio_check(&dev
->channels
[fh
->channel_id
]
1434 dprintk(1, "%s()\n", __func__
);
1435 if (UNSET
== dev
->tuner_type
)
1444 /*****************************************************************************/
1445 static const struct v4l2_queryctrl no_ctl
= {
1447 .flags
= V4L2_CTRL_FLAG_DISABLED
,
1450 static struct v4l2_queryctrl cx25821_ctls
[] = {
1453 .id
= V4L2_CID_BRIGHTNESS
,
1454 .name
= "Brightness",
1458 .default_value
= 6200,
1459 .type
= V4L2_CTRL_TYPE_INTEGER
,
1461 .id
= V4L2_CID_CONTRAST
,
1466 .default_value
= 5000,
1467 .type
= V4L2_CTRL_TYPE_INTEGER
,
1469 .id
= V4L2_CID_SATURATION
,
1470 .name
= "Saturation",
1474 .default_value
= 5000,
1475 .type
= V4L2_CTRL_TYPE_INTEGER
,
1482 .default_value
= 5000,
1483 .type
= V4L2_CTRL_TYPE_INTEGER
,
1486 static const int CX25821_CTLS
= ARRAY_SIZE(cx25821_ctls
);
1488 static int cx25821_ctrl_query(struct v4l2_queryctrl
*qctrl
)
1492 if (qctrl
->id
< V4L2_CID_BASE
|| qctrl
->id
>= V4L2_CID_LASTP1
)
1494 for (i
= 0; i
< CX25821_CTLS
; i
++)
1495 if (cx25821_ctls
[i
].id
== qctrl
->id
)
1497 if (i
== CX25821_CTLS
) {
1501 *qctrl
= cx25821_ctls
[i
];
1505 int cx25821_vidioc_queryctrl(struct file
*file
, void *priv
,
1506 struct v4l2_queryctrl
*qctrl
)
1508 return cx25821_ctrl_query(qctrl
);
1511 /* ------------------------------------------------------------------ */
1512 /* VIDEO CTRL IOCTLS */
1514 static const struct v4l2_queryctrl
*ctrl_by_id(unsigned int id
)
1518 for (i
= 0; i
< CX25821_CTLS
; i
++)
1519 if (cx25821_ctls
[i
].id
== id
)
1520 return cx25821_ctls
+ i
;
1524 int cx25821_vidioc_g_ctrl(struct file
*file
, void *priv
, struct v4l2_control
*ctl
)
1526 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1527 struct cx25821_fh
*fh
= priv
;
1529 const struct v4l2_queryctrl
*ctrl
;
1531 ctrl
= ctrl_by_id(ctl
->id
);
1536 case V4L2_CID_BRIGHTNESS
:
1537 ctl
->value
= dev
->channels
[fh
->channel_id
].ctl_bright
;
1540 ctl
->value
= dev
->channels
[fh
->channel_id
].ctl_hue
;
1542 case V4L2_CID_CONTRAST
:
1543 ctl
->value
= dev
->channels
[fh
->channel_id
].ctl_contrast
;
1545 case V4L2_CID_SATURATION
:
1546 ctl
->value
= dev
->channels
[fh
->channel_id
].ctl_saturation
;
1552 int cx25821_set_control(struct cx25821_dev
*dev
,
1553 struct v4l2_control
*ctl
, int chan_num
)
1556 const struct v4l2_queryctrl
*ctrl
;
1560 ctrl
= ctrl_by_id(ctl
->id
);
1565 switch (ctrl
->type
) {
1566 case V4L2_CTRL_TYPE_BOOLEAN
:
1567 case V4L2_CTRL_TYPE_MENU
:
1568 case V4L2_CTRL_TYPE_INTEGER
:
1569 if (ctl
->value
< ctrl
->minimum
)
1570 ctl
->value
= ctrl
->minimum
;
1571 if (ctl
->value
> ctrl
->maximum
)
1572 ctl
->value
= ctrl
->maximum
;
1579 case V4L2_CID_BRIGHTNESS
:
1580 dev
->channels
[chan_num
].ctl_bright
= ctl
->value
;
1581 medusa_set_brightness(dev
, ctl
->value
, chan_num
);
1584 dev
->channels
[chan_num
].ctl_hue
= ctl
->value
;
1585 medusa_set_hue(dev
, ctl
->value
, chan_num
);
1587 case V4L2_CID_CONTRAST
:
1588 dev
->channels
[chan_num
].ctl_contrast
= ctl
->value
;
1589 medusa_set_contrast(dev
, ctl
->value
, chan_num
);
1591 case V4L2_CID_SATURATION
:
1592 dev
->channels
[chan_num
].ctl_saturation
= ctl
->value
;
1593 medusa_set_saturation(dev
, ctl
->value
, chan_num
);
1602 static void cx25821_init_controls(struct cx25821_dev
*dev
, int chan_num
)
1604 struct v4l2_control ctrl
;
1606 for (i
= 0; i
< CX25821_CTLS
; i
++) {
1607 ctrl
.id
= cx25821_ctls
[i
].id
;
1608 ctrl
.value
= cx25821_ctls
[i
].default_value
;
1610 cx25821_set_control(dev
, &ctrl
, chan_num
);
1614 int cx25821_vidioc_cropcap(struct file
*file
, void *priv
, struct v4l2_cropcap
*cropcap
)
1616 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1618 if (cropcap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1620 cropcap
->bounds
.top
= cropcap
->bounds
.left
= 0;
1621 cropcap
->bounds
.width
= 720;
1622 cropcap
->bounds
.height
= dev
->tvnorm
== V4L2_STD_PAL_BG
? 576 : 480;
1623 cropcap
->pixelaspect
.numerator
=
1624 dev
->tvnorm
== V4L2_STD_PAL_BG
? 59 : 10;
1625 cropcap
->pixelaspect
.denominator
=
1626 dev
->tvnorm
== V4L2_STD_PAL_BG
? 54 : 11;
1627 cropcap
->defrect
= cropcap
->bounds
;
1631 int cx25821_vidioc_s_crop(struct file
*file
, void *priv
, struct v4l2_crop
*crop
)
1633 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
1634 struct cx25821_fh
*fh
= priv
;
1638 err
= v4l2_prio_check(&dev
->channels
[fh
->channel_id
].
1643 /* cx25821_vidioc_s_crop not supported */
1647 int cx25821_vidioc_g_crop(struct file
*file
, void *priv
, struct v4l2_crop
*crop
)
1649 /* cx25821_vidioc_g_crop not supported */
1653 int cx25821_vidioc_querystd(struct file
*file
, void *priv
, v4l2_std_id
* norm
)
1655 /* medusa does not support video standard sensing of current input */
1656 *norm
= CX25821_NORMS
;
1661 int cx25821_is_valid_width(u32 width
, v4l2_std_id tvnorm
)
1663 if (tvnorm
== V4L2_STD_PAL_BG
) {
1664 if (width
== 352 || width
== 720)
1670 if (tvnorm
== V4L2_STD_NTSC_M
) {
1671 if (width
== 320 || width
== 352 || width
== 720)
1679 int cx25821_is_valid_height(u32 height
, v4l2_std_id tvnorm
)
1681 if (tvnorm
== V4L2_STD_PAL_BG
) {
1682 if (height
== 576 || height
== 288)
1688 if (tvnorm
== V4L2_STD_NTSC_M
) {
1689 if (height
== 480 || height
== 240)
1698 static long video_ioctl_upstream9(struct file
*file
, unsigned int cmd
,
1701 struct cx25821_fh
*fh
= file
->private_data
;
1702 struct cx25821_dev
*dev
= fh
->dev
;
1704 struct upstream_user_struct
*data_from_user
;
1706 data_from_user
= (struct upstream_user_struct
*)arg
;
1708 if (!data_from_user
) {
1709 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__
);
1713 command
= data_from_user
->command
;
1715 if (command
!= UPSTREAM_START_VIDEO
&&
1716 command
!= UPSTREAM_STOP_VIDEO
)
1719 dev
->input_filename
= data_from_user
->input_filename
;
1720 dev
->input_audiofilename
= data_from_user
->input_filename
;
1721 dev
->vid_stdname
= data_from_user
->vid_stdname
;
1722 dev
->pixel_format
= data_from_user
->pixel_format
;
1723 dev
->channel_select
= data_from_user
->channel_select
;
1724 dev
->command
= data_from_user
->command
;
1727 case UPSTREAM_START_VIDEO
:
1728 cx25821_start_upstream_video_ch1(dev
, data_from_user
);
1731 case UPSTREAM_STOP_VIDEO
:
1732 cx25821_stop_upstream_video_ch1(dev
);
1739 static long video_ioctl_upstream10(struct file
*file
, unsigned int cmd
,
1742 struct cx25821_fh
*fh
= file
->private_data
;
1743 struct cx25821_dev
*dev
= fh
->dev
;
1745 struct upstream_user_struct
*data_from_user
;
1747 data_from_user
= (struct upstream_user_struct
*)arg
;
1749 if (!data_from_user
) {
1750 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__
);
1754 command
= data_from_user
->command
;
1756 if (command
!= UPSTREAM_START_VIDEO
&&
1757 command
!= UPSTREAM_STOP_VIDEO
)
1760 dev
->input_filename_ch2
= data_from_user
->input_filename
;
1761 dev
->input_audiofilename
= data_from_user
->input_filename
;
1762 dev
->vid_stdname_ch2
= data_from_user
->vid_stdname
;
1763 dev
->pixel_format_ch2
= data_from_user
->pixel_format
;
1764 dev
->channel_select_ch2
= data_from_user
->channel_select
;
1765 dev
->command_ch2
= data_from_user
->command
;
1768 case UPSTREAM_START_VIDEO
:
1769 cx25821_start_upstream_video_ch2(dev
, data_from_user
);
1772 case UPSTREAM_STOP_VIDEO
:
1773 cx25821_stop_upstream_video_ch2(dev
);
1780 static long video_ioctl_upstream11(struct file
*file
, unsigned int cmd
,
1783 struct cx25821_fh
*fh
= file
->private_data
;
1784 struct cx25821_dev
*dev
= fh
->dev
;
1786 struct upstream_user_struct
*data_from_user
;
1788 data_from_user
= (struct upstream_user_struct
*)arg
;
1790 if (!data_from_user
) {
1791 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__
);
1795 command
= data_from_user
->command
;
1797 if (command
!= UPSTREAM_START_AUDIO
&&
1798 command
!= UPSTREAM_STOP_AUDIO
)
1801 dev
->input_filename
= data_from_user
->input_filename
;
1802 dev
->input_audiofilename
= data_from_user
->input_filename
;
1803 dev
->vid_stdname
= data_from_user
->vid_stdname
;
1804 dev
->pixel_format
= data_from_user
->pixel_format
;
1805 dev
->channel_select
= data_from_user
->channel_select
;
1806 dev
->command
= data_from_user
->command
;
1809 case UPSTREAM_START_AUDIO
:
1810 cx25821_start_upstream_audio(dev
, data_from_user
);
1813 case UPSTREAM_STOP_AUDIO
:
1814 cx25821_stop_upstream_audio(dev
);
1821 static long video_ioctl_set(struct file
*file
, unsigned int cmd
,
1824 struct cx25821_fh
*fh
= file
->private_data
;
1825 struct cx25821_dev
*dev
= fh
->dev
;
1826 struct downstream_user_struct
*data_from_user
;
1829 int selected_channel
= 0, pix_format
= 0, i
= 0;
1830 int cif_enable
= 0, cif_width
= 0;
1833 data_from_user
= (struct downstream_user_struct
*)arg
;
1835 if (!data_from_user
) {
1836 pr_err("%s(): User data is INVALID. Returning\n", __func__
);
1840 command
= data_from_user
->command
;
1842 if (command
!= SET_VIDEO_STD
&& command
!= SET_PIXEL_FORMAT
1843 && command
!= ENABLE_CIF_RESOLUTION
&& command
!= REG_READ
1844 && command
!= REG_WRITE
&& command
!= MEDUSA_READ
1845 && command
!= MEDUSA_WRITE
) {
1852 !strcmp(data_from_user
->vid_stdname
,
1853 "PAL") ? V4L2_STD_PAL_BG
: V4L2_STD_NTSC_M
;
1854 medusa_set_videostandard(dev
);
1857 case SET_PIXEL_FORMAT
:
1858 selected_channel
= data_from_user
->decoder_select
;
1859 pix_format
= data_from_user
->pixel_format
;
1861 if (!(selected_channel
<= 7 && selected_channel
>= 0)) {
1862 selected_channel
-= 4;
1863 selected_channel
= selected_channel
% 8;
1866 if (selected_channel
>= 0)
1867 cx25821_set_pixel_format(dev
, selected_channel
,
1872 case ENABLE_CIF_RESOLUTION
:
1873 selected_channel
= data_from_user
->decoder_select
;
1874 cif_enable
= data_from_user
->cif_resolution_enable
;
1875 cif_width
= data_from_user
->cif_width
;
1878 if (dev
->tvnorm
& V4L2_STD_PAL_BG
1879 || dev
->tvnorm
& V4L2_STD_PAL_DK
)
1882 width
= (cif_width
== 320
1883 || cif_width
== 352) ? cif_width
: 320;
1886 if (!(selected_channel
<= 7 && selected_channel
>= 0)) {
1887 selected_channel
-= 4;
1888 selected_channel
= selected_channel
% 8;
1891 if (selected_channel
<= 7 && selected_channel
>= 0) {
1892 dev
->channels
[selected_channel
].
1893 use_cif_resolution
= cif_enable
;
1894 dev
->channels
[selected_channel
].cif_width
= width
;
1896 for (i
= 0; i
< VID_CHANNEL_NUM
; i
++) {
1897 dev
->channels
[i
].use_cif_resolution
=
1899 dev
->channels
[i
].cif_width
= width
;
1903 medusa_set_resolution(dev
, width
, selected_channel
);
1906 data_from_user
->reg_data
= cx_read(data_from_user
->reg_address
);
1909 cx_write(data_from_user
->reg_address
, data_from_user
->reg_data
);
1913 cx25821_i2c_read(&dev
->i2c_bus
[0],
1914 (u16
) data_from_user
->reg_address
,
1915 &data_from_user
->reg_data
);
1918 cx25821_i2c_write(&dev
->i2c_bus
[0],
1919 (u16
) data_from_user
->reg_address
,
1920 data_from_user
->reg_data
);
1927 static long cx25821_video_ioctl(struct file
*file
,
1928 unsigned int cmd
, unsigned long arg
)
1932 struct cx25821_fh
*fh
= file
->private_data
;
1934 /* check to see if it's the video upstream */
1935 if (fh
->channel_id
== SRAM_CH09
) {
1936 ret
= video_ioctl_upstream9(file
, cmd
, arg
);
1938 } else if (fh
->channel_id
== SRAM_CH10
) {
1939 ret
= video_ioctl_upstream10(file
, cmd
, arg
);
1941 } else if (fh
->channel_id
== SRAM_CH11
) {
1942 ret
= video_ioctl_upstream11(file
, cmd
, arg
);
1943 ret
= video_ioctl_set(file
, cmd
, arg
);
1947 return video_ioctl2(file
, cmd
, arg
);
1950 /* exported stuff */
1951 static const struct v4l2_file_operations video_fops
= {
1952 .owner
= THIS_MODULE
,
1954 .release
= video_release
,
1957 .mmap
= cx25821_video_mmap
,
1958 .ioctl
= cx25821_video_ioctl
,
1961 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1962 .vidioc_querycap
= cx25821_vidioc_querycap
,
1963 .vidioc_enum_fmt_vid_cap
= cx25821_vidioc_enum_fmt_vid_cap
,
1964 .vidioc_g_fmt_vid_cap
= cx25821_vidioc_g_fmt_vid_cap
,
1965 .vidioc_try_fmt_vid_cap
= cx25821_vidioc_try_fmt_vid_cap
,
1966 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1967 .vidioc_reqbufs
= cx25821_vidioc_reqbufs
,
1968 .vidioc_querybuf
= cx25821_vidioc_querybuf
,
1969 .vidioc_qbuf
= cx25821_vidioc_qbuf
,
1970 .vidioc_dqbuf
= vidioc_dqbuf
,
1972 .vidioc_s_std
= cx25821_vidioc_s_std
,
1973 .vidioc_querystd
= cx25821_vidioc_querystd
,
1975 .vidioc_cropcap
= cx25821_vidioc_cropcap
,
1976 .vidioc_s_crop
= cx25821_vidioc_s_crop
,
1977 .vidioc_g_crop
= cx25821_vidioc_g_crop
,
1978 .vidioc_enum_input
= cx25821_vidioc_enum_input
,
1979 .vidioc_g_input
= cx25821_vidioc_g_input
,
1980 .vidioc_s_input
= cx25821_vidioc_s_input
,
1981 .vidioc_g_ctrl
= cx25821_vidioc_g_ctrl
,
1982 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1983 .vidioc_queryctrl
= cx25821_vidioc_queryctrl
,
1984 .vidioc_streamon
= vidioc_streamon
,
1985 .vidioc_streamoff
= vidioc_streamoff
,
1986 .vidioc_log_status
= vidioc_log_status
,
1987 .vidioc_g_priority
= cx25821_vidioc_g_priority
,
1988 .vidioc_s_priority
= cx25821_vidioc_s_priority
,
1990 .vidioc_g_tuner
= cx25821_vidioc_g_tuner
,
1991 .vidioc_s_tuner
= cx25821_vidioc_s_tuner
,
1992 .vidioc_g_frequency
= cx25821_vidioc_g_frequency
,
1993 .vidioc_s_frequency
= cx25821_vidioc_s_frequency
,
1995 #ifdef CONFIG_VIDEO_ADV_DEBUG
1996 .vidioc_g_register
= cx25821_vidioc_g_register
,
1997 .vidioc_s_register
= cx25821_vidioc_s_register
,
2001 struct video_device cx25821_videoioctl_template
= {
2002 .name
= "cx25821-videoioctl",
2003 .fops
= &video_fops
,
2004 .ioctl_ops
= &video_ioctl_ops
,
2005 .tvnorms
= CX25821_NORMS
,
2006 .current_norm
= V4L2_STD_NTSC_M
,