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 <linux/slab.h>
26 #include "cx25821-video.h"
28 static void buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
30 struct cx25821_buffer
*buf
=
31 container_of(vb
, struct cx25821_buffer
, vb
);
32 struct cx25821_buffer
*prev
;
33 struct cx25821_fh
*fh
= vq
->priv_data
;
34 struct cx25821_dev
*dev
= fh
->dev
;
35 struct cx25821_dmaqueue
*q
= &dev
->vidq
[SRAM_CH11
];
37 /* add jump to stopper */
38 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| RISC_CNT_INC
);
39 buf
->risc
.jmp
[1] = cpu_to_le32(q
->stopper
.dma
);
40 buf
->risc
.jmp
[2] = cpu_to_le32(0); /* bits 63-32 */
42 dprintk(2, "jmp to stopper (0x%x)\n", buf
->risc
.jmp
[1]);
44 if (!list_empty(&q
->queued
)) {
45 list_add_tail(&buf
->vb
.queue
, &q
->queued
);
46 buf
->vb
.state
= VIDEOBUF_QUEUED
;
47 dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf
,
50 } else if (list_empty(&q
->active
)) {
51 list_add_tail(&buf
->vb
.queue
, &q
->active
);
52 cx25821_start_video_dma(dev
, q
, buf
,
53 &dev
->sram_channels
[SRAM_CH11
]);
54 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
55 buf
->count
= q
->count
++;
56 mod_timer(&q
->timeout
, jiffies
+ BUFFER_TIMEOUT
);
58 "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
59 buf
, buf
->vb
.i
, buf
->count
, q
->count
);
62 list_entry(q
->active
.prev
, struct cx25821_buffer
, vb
.queue
);
63 if (prev
->vb
.width
== buf
->vb
.width
64 && prev
->vb
.height
== buf
->vb
.height
65 && prev
->fmt
== buf
->fmt
) {
66 list_add_tail(&buf
->vb
.queue
, &q
->active
);
67 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
68 buf
->count
= q
->count
++;
69 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
71 /* 64 bit bits 63-32 */
72 prev
->risc
.jmp
[2] = cpu_to_le32(0);
74 "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
75 buf
, buf
->vb
.i
, buf
->count
);
78 list_add_tail(&buf
->vb
.queue
, &q
->queued
);
79 buf
->vb
.state
= VIDEOBUF_QUEUED
;
80 dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf
,
85 if (list_empty(&q
->active
)) {
86 dprintk(2, "active queue empty!\n");
90 static struct videobuf_queue_ops cx25821_video_qops
= {
91 .buf_setup
= buffer_setup
,
92 .buf_prepare
= buffer_prepare
,
93 .buf_queue
= buffer_queue
,
94 .buf_release
= buffer_release
,
97 static int video_open(struct file
*file
)
99 struct video_device
*vdev
= video_devdata(file
);
100 struct cx25821_dev
*dev
= video_drvdata(file
);
101 struct cx25821_fh
*fh
;
102 enum v4l2_buf_type type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
104 printk("open dev=%s type=%s\n", video_device_node_name(vdev
),
105 v4l2_type_names
[type
]);
107 /* allocate + initialize per filehandle data */
108 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
114 file
->private_data
= fh
;
119 if (dev
->tvnorm
& V4L2_STD_PAL_BG
|| dev
->tvnorm
& V4L2_STD_PAL_DK
)
124 dev
->channel_opened
= 10;
125 fh
->fmt
= format_by_fourcc(V4L2_PIX_FMT_YUYV
);
127 v4l2_prio_open(&dev
->prio
, &fh
->prio
);
129 videobuf_queue_sg_init(&fh
->vidq
, &cx25821_video_qops
,
130 &dev
->pci
->dev
, &dev
->slock
,
131 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
132 V4L2_FIELD_INTERLACED
,
133 sizeof(struct cx25821_buffer
), fh
);
135 dprintk(1, "post videobuf_queue_init()\n");
141 static ssize_t
video_read(struct file
*file
, char __user
* data
, size_t count
,
144 struct cx25821_fh
*fh
= file
->private_data
;
147 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
148 if (res_locked(fh
->dev
, RESOURCE_VIDEO11
))
151 return videobuf_read_one(&fh
->vidq
, data
, count
, ppos
,
152 file
->f_flags
& O_NONBLOCK
);
160 static unsigned int video_poll(struct file
*file
,
161 struct poll_table_struct
*wait
)
163 struct cx25821_fh
*fh
= file
->private_data
;
164 struct cx25821_buffer
*buf
;
166 if (res_check(fh
, RESOURCE_VIDEO11
)) {
167 /* streaming capture */
168 if (list_empty(&fh
->vidq
.stream
))
170 buf
= list_entry(fh
->vidq
.stream
.next
,
171 struct cx25821_buffer
, vb
.stream
);
174 buf
= (struct cx25821_buffer
*)fh
->vidq
.read_buf
;
179 poll_wait(file
, &buf
->vb
.done
, wait
);
180 if (buf
->vb
.state
== VIDEOBUF_DONE
|| buf
->vb
.state
== VIDEOBUF_ERROR
)
181 return POLLIN
| POLLRDNORM
;
185 static int video_release(struct file
*file
)
187 struct cx25821_fh
*fh
= file
->private_data
;
188 struct cx25821_dev
*dev
= fh
->dev
;
190 //stop the risc engine and fifo
191 //cx_write(channel11->dma_ctl, 0);
193 /* stop video capture */
194 if (res_check(fh
, RESOURCE_VIDEO11
)) {
195 videobuf_queue_cancel(&fh
->vidq
);
196 res_free(dev
, fh
, RESOURCE_VIDEO11
);
199 if (fh
->vidq
.read_buf
) {
200 buffer_release(&fh
->vidq
, fh
->vidq
.read_buf
);
201 kfree(fh
->vidq
.read_buf
);
204 videobuf_mmap_free(&fh
->vidq
);
206 v4l2_prio_close(&dev
->prio
, &fh
->prio
);
208 file
->private_data
= NULL
;
214 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
216 struct cx25821_fh
*fh
= priv
;
217 struct cx25821_dev
*dev
= fh
->dev
;
219 if (unlikely(fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)) {
223 if (unlikely(i
!= fh
->type
)) {
227 if (unlikely(!res_get(dev
, fh
, get_resource(fh
, RESOURCE_VIDEO11
)))) {
231 return videobuf_streamon(get_queue(fh
));
234 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
236 struct cx25821_fh
*fh
= priv
;
237 struct cx25821_dev
*dev
= fh
->dev
;
240 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
245 res
= get_resource(fh
, RESOURCE_VIDEO11
);
246 err
= videobuf_streamoff(get_queue(fh
));
249 res_free(dev
, fh
, res
);
253 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
254 struct v4l2_format
*f
)
256 struct cx25821_fh
*fh
= priv
;
257 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
261 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
266 dprintk(2, "%s()\n", __func__
);
267 err
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
271 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
272 fh
->width
= f
->fmt
.pix
.width
;
273 fh
->height
= f
->fmt
.pix
.height
;
274 fh
->vidq
.field
= f
->fmt
.pix
.field
;
275 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__
, fh
->width
,
276 fh
->height
, fh
->vidq
.field
);
277 cx25821_call_all(dev
, video
, s_fmt
, f
);
281 static long video_ioctl_upstream11(struct file
*file
, unsigned int cmd
,
284 struct cx25821_fh
*fh
= file
->private_data
;
285 struct cx25821_dev
*dev
= fh
->dev
;
287 struct upstream_user_struct
*data_from_user
;
289 data_from_user
= (struct upstream_user_struct
*)arg
;
291 if (!data_from_user
) {
293 ("cx25821 in %s(): Upstream data is INVALID. Returning.\n",
298 command
= data_from_user
->command
;
300 if (command
!= UPSTREAM_START_AUDIO
&& command
!= UPSTREAM_STOP_AUDIO
) {
304 dev
->input_filename
= data_from_user
->input_filename
;
305 dev
->input_audiofilename
= data_from_user
->input_filename
;
306 dev
->vid_stdname
= data_from_user
->vid_stdname
;
307 dev
->pixel_format
= data_from_user
->pixel_format
;
308 dev
->channel_select
= data_from_user
->channel_select
;
309 dev
->command
= data_from_user
->command
;
312 case UPSTREAM_START_AUDIO
:
313 cx25821_start_upstream_audio(dev
, data_from_user
);
316 case UPSTREAM_STOP_AUDIO
:
317 cx25821_stop_upstream_audio(dev
);
324 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
326 struct cx25821_fh
*fh
= priv
;
327 return videobuf_dqbuf(get_queue(fh
), p
, file
->f_flags
& O_NONBLOCK
);
330 static int vidioc_log_status(struct file
*file
, void *priv
)
332 struct cx25821_dev
*dev
= ((struct cx25821_fh
*)priv
)->dev
;
335 snprintf(name
, sizeof(name
), "%s/2", dev
->name
);
336 printk(KERN_INFO
"%s/2: ============ START LOG STATUS ============\n",
338 cx25821_call_all(dev
, core
, log_status
);
339 printk(KERN_INFO
"%s/2: ============= END LOG STATUS =============\n",
344 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
345 struct v4l2_control
*ctl
)
347 struct cx25821_fh
*fh
= priv
;
348 struct cx25821_dev
*dev
;
353 err
= v4l2_prio_check(&dev
->prio
, &fh
->prio
);
361 static const struct v4l2_file_operations video_fops
= {
362 .owner
= THIS_MODULE
,
364 .release
= video_release
,
368 .ioctl
= video_ioctl_upstream11
,
371 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
372 .vidioc_querycap
= vidioc_querycap
,
373 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
374 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
375 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
376 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
377 .vidioc_reqbufs
= vidioc_reqbufs
,
378 .vidioc_querybuf
= vidioc_querybuf
,
379 .vidioc_qbuf
= vidioc_qbuf
,
380 .vidioc_dqbuf
= vidioc_dqbuf
,
382 .vidioc_s_std
= vidioc_s_std
,
383 .vidioc_querystd
= vidioc_querystd
,
385 .vidioc_cropcap
= vidioc_cropcap
,
386 .vidioc_s_crop
= vidioc_s_crop
,
387 .vidioc_g_crop
= vidioc_g_crop
,
388 .vidioc_enum_input
= vidioc_enum_input
,
389 .vidioc_g_input
= vidioc_g_input
,
390 .vidioc_s_input
= vidioc_s_input
,
391 .vidioc_g_ctrl
= vidioc_g_ctrl
,
392 .vidioc_s_ctrl
= vidioc_s_ctrl
,
393 .vidioc_queryctrl
= vidioc_queryctrl
,
394 .vidioc_streamon
= vidioc_streamon
,
395 .vidioc_streamoff
= vidioc_streamoff
,
396 .vidioc_log_status
= vidioc_log_status
,
397 .vidioc_g_priority
= vidioc_g_priority
,
398 .vidioc_s_priority
= vidioc_s_priority
,
399 #ifdef CONFIG_VIDEO_V4L1_COMPAT
400 .vidiocgmbuf
= vidiocgmbuf
,
403 .vidioc_g_tuner
= vidioc_g_tuner
,
404 .vidioc_s_tuner
= vidioc_s_tuner
,
405 .vidioc_g_frequency
= vidioc_g_frequency
,
406 .vidioc_s_frequency
= vidioc_s_frequency
,
408 #ifdef CONFIG_VIDEO_ADV_DEBUG
409 .vidioc_g_register
= vidioc_g_register
,
410 .vidioc_s_register
= vidioc_s_register
,
414 struct video_device cx25821_video_template11
= {
415 .name
= "cx25821-audioupstream",
417 .ioctl_ops
= &video_ioctl_ops
,
418 .tvnorms
= CX25821_NORMS
,
419 .current_norm
= V4L2_STD_NTSC_M
,