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 static void buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
28 struct cx25821_buffer
*buf
=
29 container_of(vb
, struct cx25821_buffer
, vb
);
30 struct cx25821_buffer
*prev
;
31 struct cx25821_fh
*fh
= vq
->priv_data
;
32 struct cx25821_dev
*dev
= fh
->dev
;
33 struct cx25821_dmaqueue
*q
= &dev
->vidq
[SRAM_CH04
];
35 /* add jump to stopper */
36 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| RISC_CNT_INC
);
37 buf
->risc
.jmp
[1] = cpu_to_le32(q
->stopper
.dma
);
38 buf
->risc
.jmp
[2] = cpu_to_le32(0); /* bits 63-32 */
40 dprintk(2, "jmp to stopper (0x%x)\n", buf
->risc
.jmp
[1]);
42 if (!list_empty(&q
->queued
)) {
43 list_add_tail(&buf
->vb
.queue
, &q
->queued
);
44 buf
->vb
.state
= VIDEOBUF_QUEUED
;
45 dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf
,
48 } else if (list_empty(&q
->active
)) {
49 list_add_tail(&buf
->vb
.queue
, &q
->active
);
50 cx25821_start_video_dma(dev
, q
, buf
,
51 &dev
->sram_channels
[SRAM_CH04
]);
52 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
53 buf
->count
= q
->count
++;
54 mod_timer(&q
->timeout
, jiffies
+ BUFFER_TIMEOUT
);
56 "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
57 buf
, buf
->vb
.i
, buf
->count
, q
->count
);
60 list_entry(q
->active
.prev
, struct cx25821_buffer
, vb
.queue
);
61 if (prev
->vb
.width
== buf
->vb
.width
62 && prev
->vb
.height
== buf
->vb
.height
63 && prev
->fmt
== buf
->fmt
) {
64 list_add_tail(&buf
->vb
.queue
, &q
->active
);
65 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
66 buf
->count
= q
->count
++;
67 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
69 /* 64 bit bits 63-32 */
70 prev
->risc
.jmp
[2] = cpu_to_le32(0);
72 "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
73 buf
, buf
->vb
.i
, buf
->count
);
76 list_add_tail(&buf
->vb
.queue
, &q
->queued
);
77 buf
->vb
.state
= VIDEOBUF_QUEUED
;
78 dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf
,
83 if (list_empty(&q
->active
)) {
84 dprintk(2, "active queue empty!\n");
88 static struct videobuf_queue_ops cx25821_video_qops
= {
89 .buf_setup
= buffer_setup
,
90 .buf_prepare
= buffer_prepare
,
91 .buf_queue
= buffer_queue
,
92 .buf_release
= buffer_release
,
95 static int video_open(struct file
*file
)
97 struct video_device
*vdev
= video_devdata(file
);
98 struct cx25821_dev
*dev
= video_drvdata(file
);
99 struct cx25821_fh
*fh
;
100 enum v4l2_buf_type type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
103 printk("open dev=%s type=%s\n", video_device_node_name(vdev
),
104 v4l2_type_names
[type
]);
106 /* allocate + initialize per filehandle data */
107 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
113 file
->private_data
= fh
;
118 if (dev
->tvnorm
& V4L2_STD_PAL_BG
|| dev
->tvnorm
& V4L2_STD_PAL_DK
)
123 dev
->channel_opened
= SRAM_CH04
;
125 (dev
->pixel_formats
[dev
->channel_opened
] ==
126 PIXEL_FRMT_411
) ? V4L2_PIX_FMT_Y41P
: V4L2_PIX_FMT_YUYV
;
127 fh
->fmt
= format_by_fourcc(pix_format
);
129 v4l2_prio_open(&dev
->prio
, &fh
->prio
);
130 videobuf_queue_sg_init(&fh
->vidq
, &cx25821_video_qops
,
131 &dev
->pci
->dev
, &dev
->slock
,
132 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
133 V4L2_FIELD_INTERLACED
,
134 sizeof(struct cx25821_buffer
), fh
);
136 dprintk(1, "post videobuf_queue_init()\n");
142 static ssize_t
video_read(struct file
*file
, char __user
* data
, size_t count
,
145 struct cx25821_fh
*fh
= file
->private_data
;
148 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
149 if (res_locked(fh
->dev
, RESOURCE_VIDEO4
))
152 return videobuf_read_one(&fh
->vidq
, data
, count
, ppos
,
153 file
->f_flags
& O_NONBLOCK
);
161 static unsigned int video_poll(struct file
*file
,
162 struct poll_table_struct
*wait
)
164 struct cx25821_fh
*fh
= file
->private_data
;
165 struct cx25821_buffer
*buf
;
167 if (res_check(fh
, RESOURCE_VIDEO4
)) {
168 /* streaming capture */
169 if (list_empty(&fh
->vidq
.stream
))
171 buf
= list_entry(fh
->vidq
.stream
.next
,
172 struct cx25821_buffer
, vb
.stream
);
175 buf
= (struct cx25821_buffer
*)fh
->vidq
.read_buf
;
180 poll_wait(file
, &buf
->vb
.done
, wait
);
181 if (buf
->vb
.state
== VIDEOBUF_DONE
|| buf
->vb
.state
== VIDEOBUF_ERROR
) {
182 if (buf
->vb
.state
== VIDEOBUF_DONE
) {
183 struct cx25821_dev
*dev
= fh
->dev
;
185 if (dev
&& dev
->use_cif_resolution
[SRAM_CH04
]) {
186 u8 cam_id
= *((char *)buf
->vb
.baddr
+ 3);
187 memcpy((char *)buf
->vb
.baddr
,
188 (char *)buf
->vb
.baddr
+ (fh
->width
* 2),
190 *((char *)buf
->vb
.baddr
+ 3) = cam_id
;
194 return POLLIN
| POLLRDNORM
;
200 static int video_release(struct file
*file
)
202 struct cx25821_fh
*fh
= file
->private_data
;
203 struct cx25821_dev
*dev
= fh
->dev
;
205 //stop the risc engine and fifo
206 cx_write(channel4
->dma_ctl
, 0); /* FIFO and RISC disable */
208 /* stop video capture */
209 if (res_check(fh
, RESOURCE_VIDEO4
)) {
210 videobuf_queue_cancel(&fh
->vidq
);
211 res_free(dev
, fh
, RESOURCE_VIDEO4
);
214 if (fh
->vidq
.read_buf
) {
215 buffer_release(&fh
->vidq
, fh
->vidq
.read_buf
);
216 kfree(fh
->vidq
.read_buf
);
219 videobuf_mmap_free(&fh
->vidq
);
221 v4l2_prio_close(&dev
->prio
, &fh
->prio
);
222 file
->private_data
= NULL
;
228 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
230 struct cx25821_fh
*fh
= priv
;
231 struct cx25821_dev
*dev
= fh
->dev
;
233 if (unlikely(fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)) {
237 if (unlikely(i
!= fh
->type
)) {
241 if (unlikely(!res_get(dev
, fh
, get_resource(fh
, RESOURCE_VIDEO4
)))) {
245 return videobuf_streamon(get_queue(fh
));
248 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
250 struct cx25821_fh
*fh
= priv
;
251 struct cx25821_dev
*dev
= fh
->dev
;
254 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
259 res
= get_resource(fh
, RESOURCE_VIDEO4
);
260 err
= videobuf_streamoff(get_queue(fh
));
263 res_free(dev
, fh
, res
);
267 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
268 struct v4l2_format
*f
)
270 struct cx25821_fh
*fh
= priv
;
271 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
277 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
281 dprintk(2, "%s()\n", __func__
);
282 err
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
287 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
288 fh
->vidq
.field
= f
->fmt
.pix
.field
;
290 // check if width and height is valid based on set standard
291 if (is_valid_width(f
->fmt
.pix
.width
, dev
->tvnorm
)) {
292 fh
->width
= f
->fmt
.pix
.width
;
295 if (is_valid_height(f
->fmt
.pix
.height
, dev
->tvnorm
)) {
296 fh
->height
= f
->fmt
.pix
.height
;
299 if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_Y41P
)
300 pix_format
= PIXEL_FRMT_411
;
301 else if (f
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_YUYV
)
302 pix_format
= PIXEL_FRMT_422
;
306 cx25821_set_pixel_format(dev
, SRAM_CH04
, pix_format
);
308 // check if cif resolution
309 if (fh
->width
== 320 || fh
->width
== 352) {
310 dev
->use_cif_resolution
[SRAM_CH04
] = 1;
312 dev
->use_cif_resolution
[SRAM_CH04
] = 0;
314 dev
->cif_width
[SRAM_CH04
] = fh
->width
;
315 medusa_set_resolution(dev
, fh
->width
, SRAM_CH04
);
317 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__
, fh
->width
,
318 fh
->height
, fh
->vidq
.field
);
319 cx25821_call_all(dev
, video
, s_fmt
, f
);
324 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
327 struct cx25821_fh
*fh
= priv
;
328 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
330 ret_val
= videobuf_dqbuf(get_queue(fh
), p
, file
->f_flags
& O_NONBLOCK
);
332 p
->sequence
= dev
->vidq
[SRAM_CH04
].count
;
337 static int vidioc_log_status(struct file
*file
, void *priv
)
339 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
342 struct sram_channel
*sram_ch
= &dev
->sram_channels
[SRAM_CH04
];
345 snprintf(name
, sizeof(name
), "%s/2", dev
->name
);
346 printk(KERN_INFO
"%s/2: ============ START LOG STATUS ============\n",
348 cx25821_call_all(dev
, core
, log_status
);
350 tmp
= cx_read(sram_ch
->dma_ctl
);
351 printk(KERN_INFO
"Video input 4 is %s\n",
352 (tmp
& 0x11) ? "streaming" : "stopped");
353 printk(KERN_INFO
"%s/2: ============= END LOG STATUS =============\n",
358 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
359 struct v4l2_control
*ctl
)
361 struct cx25821_fh
*fh
= priv
;
362 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
366 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
371 return cx25821_set_control(dev
, ctl
, SRAM_CH04
);
375 static const struct v4l2_file_operations video_fops
= {
376 .owner
= THIS_MODULE
,
378 .release
= video_release
,
382 .ioctl
= video_ioctl2
,
385 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
386 .vidioc_querycap
= vidioc_querycap
,
387 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
388 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
389 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
390 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
391 .vidioc_reqbufs
= vidioc_reqbufs
,
392 .vidioc_querybuf
= vidioc_querybuf
,
393 .vidioc_qbuf
= vidioc_qbuf
,
394 .vidioc_dqbuf
= vidioc_dqbuf
,
396 .vidioc_s_std
= vidioc_s_std
,
397 .vidioc_querystd
= vidioc_querystd
,
399 .vidioc_cropcap
= vidioc_cropcap
,
400 .vidioc_s_crop
= vidioc_s_crop
,
401 .vidioc_g_crop
= vidioc_g_crop
,
402 .vidioc_enum_input
= vidioc_enum_input
,
403 .vidioc_g_input
= vidioc_g_input
,
404 .vidioc_s_input
= vidioc_s_input
,
405 .vidioc_g_ctrl
= vidioc_g_ctrl
,
406 .vidioc_s_ctrl
= vidioc_s_ctrl
,
407 .vidioc_queryctrl
= vidioc_queryctrl
,
408 .vidioc_streamon
= vidioc_streamon
,
409 .vidioc_streamoff
= vidioc_streamoff
,
410 .vidioc_log_status
= vidioc_log_status
,
411 .vidioc_g_priority
= vidioc_g_priority
,
412 .vidioc_s_priority
= vidioc_s_priority
,
413 #ifdef CONFIG_VIDEO_V4L1_COMPAT
414 .vidiocgmbuf
= vidiocgmbuf
,
417 .vidioc_g_tuner
= vidioc_g_tuner
,
418 .vidioc_s_tuner
= vidioc_s_tuner
,
419 .vidioc_g_frequency
= vidioc_g_frequency
,
420 .vidioc_s_frequency
= vidioc_s_frequency
,
422 #ifdef CONFIG_VIDEO_ADV_DEBUG
423 .vidioc_g_register
= vidioc_g_register
,
424 .vidioc_s_register
= vidioc_s_register
,
428 struct video_device cx25821_video_template4
= {
429 .name
= "cx25821-video",
431 .ioctl_ops
= &video_ioctl_ops
,
432 .tvnorms
= CX25821_NORMS
,
433 .current_norm
= V4L2_STD_NTSC_M
,