Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[deliverable/linux.git] / drivers / staging / cx25821 / cx25821-video0.c
CommitLineData
02b20b0b
MCC
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
bb4c9a74 4 * Copyright (C) 2009 Conexant Systems Inc.
02b20b0b
MCC
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
7 *
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.
12 *
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
16 *
17 * GNU General Public License for more details.
18 *
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.
22 */
23
24#include "cx25821-video.h"
25
02b20b0b
MCC
26static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
27{
1a9fc855
MCC
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_CH00];
34
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 */
39
40 dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
41
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,
46 buf->vb.i);
47
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_CH00]);
52 buf->vb.state = VIDEOBUF_ACTIVE;
53 buf->count = q->count++;
54 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
55 dprintk(2,
56 "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
57 buf, buf->vb.i, buf->count, q->count);
bb4c9a74 58 } else {
1a9fc855
MCC
59 prev =
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);
68
69 /* 64 bit bits 63-32 */
70 prev->risc.jmp[2] = cpu_to_le32(0);
71 dprintk(2,
72 "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
73 buf, buf->vb.i, buf->count);
74
75 } else {
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,
79 buf->vb.i);
80 }
bb4c9a74 81 }
02b20b0b 82
1a9fc855
MCC
83 if (list_empty(&q->active)) {
84 dprintk(2, "active queue empty!\n");
85 }
02b20b0b
MCC
86}
87
02b20b0b 88static struct videobuf_queue_ops cx25821_video_qops = {
1a9fc855
MCC
89 .buf_setup = buffer_setup,
90 .buf_prepare = buffer_prepare,
91 .buf_queue = buffer_queue,
92 .buf_release = buffer_release,
02b20b0b
MCC
93};
94
02b20b0b
MCC
95static int video_open(struct file *file)
96{
1a9fc855
MCC
97 int minor = video_devdata(file)->minor;
98 struct cx25821_dev *h, *dev = NULL;
99 struct cx25821_fh *fh;
100 struct list_head *list;
101 enum v4l2_buf_type type = 0;
102 u32 pix_format;
103
104 lock_kernel();
105 list_for_each(list, &cx25821_devlist) {
106 h = list_entry(list, struct cx25821_dev, devlist);
107
108 if (h->video_dev[SRAM_CH00]
109 && h->video_dev[SRAM_CH00]->minor == minor) {
110 dev = h;
111 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
112 }
bb4c9a74 113 }
02b20b0b 114
1a9fc855
MCC
115 if (NULL == dev) {
116 unlock_kernel();
117 return -ENODEV;
118 }
02b20b0b 119
1a9fc855 120 printk("open minor=%d type=%s\n", minor, v4l2_type_names[type]);
02b20b0b 121
1a9fc855
MCC
122 /* allocate + initialize per filehandle data */
123 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
124 if (NULL == fh) {
125 unlock_kernel();
126 return -ENOMEM;
127 }
02b20b0b 128
1a9fc855
MCC
129 file->private_data = fh;
130 fh->dev = dev;
131 fh->type = type;
132 fh->width = 720;
02b20b0b 133
1a9fc855
MCC
134 if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
135 fh->height = 576;
136 else
137 fh->height = 480;
02b20b0b 138
1a9fc855
MCC
139 dev->channel_opened = SRAM_CH00;
140 pix_format =
141 (dev->pixel_formats[dev->channel_opened] ==
142 PIXEL_FRMT_411) ? V4L2_PIX_FMT_Y41P : V4L2_PIX_FMT_YUYV;
143 fh->fmt = format_by_fourcc(pix_format);
bb4c9a74 144
1a9fc855 145 v4l2_prio_open(&dev->prio, &fh->prio);
02b20b0b 146
1a9fc855
MCC
147 videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops,
148 &dev->pci->dev, &dev->slock,
149 V4L2_BUF_TYPE_VIDEO_CAPTURE,
150 V4L2_FIELD_INTERLACED,
151 sizeof(struct cx25821_buffer), fh);
02b20b0b 152
1a9fc855
MCC
153 dprintk(1, "post videobuf_queue_init()\n");
154 unlock_kernel();
02b20b0b 155
1a9fc855 156 return 0;
02b20b0b
MCC
157}
158
1a9fc855
MCC
159static ssize_t video_read(struct file *file, char __user * data, size_t count,
160 loff_t * ppos)
02b20b0b 161{
1a9fc855 162 struct cx25821_fh *fh = file->private_data;
02b20b0b 163
1a9fc855 164 switch (fh->type) {
bb4c9a74 165 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1a9fc855
MCC
166 if (res_locked(fh->dev, RESOURCE_VIDEO0))
167 return -EBUSY;
02b20b0b 168
1a9fc855
MCC
169 return videobuf_read_one(&fh->vidq, data, count, ppos,
170 file->f_flags & O_NONBLOCK);
02b20b0b 171
bb4c9a74 172 default:
1a9fc855
MCC
173 BUG();
174 return 0;
175 }
02b20b0b
MCC
176}
177
1a9fc855
MCC
178static unsigned int video_poll(struct file *file,
179 struct poll_table_struct *wait)
02b20b0b 180{
1a9fc855
MCC
181 struct cx25821_fh *fh = file->private_data;
182 struct cx25821_buffer *buf;
183
184 if (res_check(fh, RESOURCE_VIDEO0)) {
185 /* streaming capture */
186 if (list_empty(&fh->vidq.stream))
187 return POLLERR;
188 buf = list_entry(fh->vidq.stream.next,
189 struct cx25821_buffer, vb.stream);
190 } else {
191 /* read() capture */
192 buf = (struct cx25821_buffer *)fh->vidq.read_buf;
193 if (NULL == buf)
194 return POLLERR;
bb4c9a74
MCC
195 }
196
1a9fc855
MCC
197 poll_wait(file, &buf->vb.done, wait);
198 if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
199 if (buf->vb.state == VIDEOBUF_DONE) {
200 struct cx25821_dev *dev = fh->dev;
201
202 if (dev && dev->use_cif_resolution[SRAM_CH00]) {
203 u8 cam_id = *((char *)buf->vb.baddr + 3);
204 memcpy((char *)buf->vb.baddr,
205 (char *)buf->vb.baddr + (fh->width * 2),
206 (fh->width * 2));
207 *((char *)buf->vb.baddr + 3) = cam_id;
208 }
209 }
210
211 return POLLIN | POLLRDNORM;
212 }
bb4c9a74 213
1a9fc855 214 return 0;
02b20b0b
MCC
215}
216
02b20b0b
MCC
217static int video_release(struct file *file)
218{
1a9fc855
MCC
219 struct cx25821_fh *fh = file->private_data;
220 struct cx25821_dev *dev = fh->dev;
02b20b0b 221
1a9fc855
MCC
222 //stop the risc engine and fifo
223 cx_write(channel0->dma_ctl, 0); /* FIFO and RISC disable */
02b20b0b 224
1a9fc855
MCC
225 /* stop video capture */
226 if (res_check(fh, RESOURCE_VIDEO0)) {
227 videobuf_queue_cancel(&fh->vidq);
228 res_free(dev, fh, RESOURCE_VIDEO0);
229 }
02b20b0b 230
1a9fc855
MCC
231 if (fh->vidq.read_buf) {
232 buffer_release(&fh->vidq, fh->vidq.read_buf);
233 kfree(fh->vidq.read_buf);
234 }
02b20b0b 235
1a9fc855 236 videobuf_mmap_free(&fh->vidq);
02b20b0b 237
1a9fc855
MCC
238 v4l2_prio_close(&dev->prio, &fh->prio);
239 file->private_data = NULL;
240 kfree(fh);
02b20b0b 241
1a9fc855 242 return 0;
02b20b0b
MCC
243}
244
02b20b0b
MCC
245static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
246{
1a9fc855
MCC
247 struct cx25821_fh *fh = priv;
248 struct cx25821_dev *dev = fh->dev;
02b20b0b 249
1a9fc855
MCC
250 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)) {
251 return -EINVAL;
252 }
02b20b0b 253
1a9fc855
MCC
254 if (unlikely(i != fh->type)) {
255 return -EINVAL;
256 }
02b20b0b 257
1a9fc855
MCC
258 if (unlikely(!res_get(dev, fh, get_resource(fh, RESOURCE_VIDEO0)))) {
259 return -EBUSY;
260 }
02b20b0b 261
1a9fc855 262 return videobuf_streamon(get_queue(fh));
02b20b0b
MCC
263}
264
265static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
266{
1a9fc855
MCC
267 struct cx25821_fh *fh = priv;
268 struct cx25821_dev *dev = fh->dev;
269 int err, res;
270
271 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
272 return -EINVAL;
273 if (i != fh->type)
274 return -EINVAL;
275
276 res = get_resource(fh, RESOURCE_VIDEO0);
277 err = videobuf_streamoff(get_queue(fh));
278 if (err < 0)
279 return err;
280 res_free(dev, fh, res);
281 return 0;
02b20b0b
MCC
282}
283
1a9fc855
MCC
284static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
285 struct v4l2_format *f)
02b20b0b 286{
1a9fc855
MCC
287 struct cx25821_fh *fh = priv;
288 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
289 int err;
290 int pix_format = PIXEL_FRMT_422;
291
292 if (fh) {
293 err = v4l2_prio_check(&dev->prio, &fh->prio);
294 if (0 != err)
295 return err;
296 }
297
298 dprintk(2, "%s()\n", __func__);
299 err = vidioc_try_fmt_vid_cap(file, priv, f);
300
bb4c9a74 301 if (0 != err)
1a9fc855
MCC
302 return err;
303
304 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
305 fh->vidq.field = f->fmt.pix.field;
306
307 // check if width and height is valid based on set standard
308 if (is_valid_width(f->fmt.pix.width, dev->tvnorm)) {
309 fh->width = f->fmt.pix.width;
310 }
311
312 if (is_valid_height(f->fmt.pix.height, dev->tvnorm)) {
313 fh->height = f->fmt.pix.height;
314 }
315
316 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
317 pix_format = PIXEL_FRMT_411;
318 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
319 pix_format = PIXEL_FRMT_422;
320 else
321 return -EINVAL;
322
323 cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
324
325 // check if cif resolution
326 if (fh->width == 320 || fh->width == 352) {
327 dev->use_cif_resolution[SRAM_CH00] = 1;
328 } else {
329 dev->use_cif_resolution[SRAM_CH00] = 0;
330 }
331 dev->cif_width[SRAM_CH00] = fh->width;
332 medusa_set_resolution(dev, fh->width, SRAM_CH00);
333
334 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__, fh->width,
335 fh->height, fh->vidq.field);
336 cx25821_call_all(dev, video, s_fmt, f);
337
338 return 0;
02b20b0b
MCC
339}
340
341static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
342{
1a9fc855
MCC
343 int ret_val = 0;
344 struct cx25821_fh *fh = priv;
345 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
02b20b0b 346
1a9fc855 347 ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
02b20b0b 348
1a9fc855 349 p->sequence = dev->vidq[SRAM_CH00].count;
02b20b0b 350
1a9fc855 351 return ret_val;
02b20b0b
MCC
352}
353
1a9fc855 354static int vidioc_log_status(struct file *file, void *priv)
02b20b0b 355{
1a9fc855
MCC
356 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
357 char name[32 + 2];
02b20b0b 358
1a9fc855
MCC
359 struct sram_channel *sram_ch = &dev->sram_channels[SRAM_CH00];
360 u32 tmp = 0;
02b20b0b 361
1a9fc855
MCC
362 snprintf(name, sizeof(name), "%s/2", dev->name);
363 printk(KERN_INFO "%s/2: ============ START LOG STATUS ============\n",
02b20b0b 364 dev->name);
1a9fc855
MCC
365 cx25821_call_all(dev, core, log_status);
366 tmp = cx_read(sram_ch->dma_ctl);
367 printk(KERN_INFO "Video input 0 is %s\n",
368 (tmp & 0x11) ? "streaming" : "stopped");
369 printk(KERN_INFO "%s/2: ============= END LOG STATUS =============\n",
02b20b0b 370 dev->name);
1a9fc855 371 return 0;
02b20b0b
MCC
372}
373
374static int vidioc_s_ctrl(struct file *file, void *priv,
1a9fc855 375 struct v4l2_control *ctl)
02b20b0b
MCC
376{
377 struct cx25821_fh *fh = priv;
bb4c9a74
MCC
378 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
379 int err;
02b20b0b 380
bb4c9a74 381 if (fh) {
02b20b0b
MCC
382 err = v4l2_prio_check(&dev->prio, &fh->prio);
383 if (0 != err)
384 return err;
385 }
386
387 return cx25821_set_control(dev, ctl, SRAM_CH00);
388}
389
390// exported stuff
391static const struct v4l2_file_operations video_fops = {
1a9fc855
MCC
392 .owner = THIS_MODULE,
393 .open = video_open,
394 .release = video_release,
395 .read = video_read,
396 .poll = video_poll,
397 .mmap = video_mmap,
398 .ioctl = video_ioctl2,
02b20b0b
MCC
399};
400
401static const struct v4l2_ioctl_ops video_ioctl_ops = {
1a9fc855
MCC
402 .vidioc_querycap = vidioc_querycap,
403 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
404 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
405 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
406 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
407 .vidioc_reqbufs = vidioc_reqbufs,
408 .vidioc_querybuf = vidioc_querybuf,
409 .vidioc_qbuf = vidioc_qbuf,
410 .vidioc_dqbuf = vidioc_dqbuf,
02b20b0b 411#ifdef TUNER_FLAG
1a9fc855
MCC
412 .vidioc_s_std = vidioc_s_std,
413 .vidioc_querystd = vidioc_querystd,
02b20b0b 414#endif
1a9fc855
MCC
415 .vidioc_cropcap = vidioc_cropcap,
416 .vidioc_s_crop = vidioc_s_crop,
417 .vidioc_g_crop = vidioc_g_crop,
418 .vidioc_enum_input = vidioc_enum_input,
419 .vidioc_g_input = vidioc_g_input,
420 .vidioc_s_input = vidioc_s_input,
421 .vidioc_g_ctrl = vidioc_g_ctrl,
422 .vidioc_s_ctrl = vidioc_s_ctrl,
423 .vidioc_queryctrl = vidioc_queryctrl,
424 .vidioc_streamon = vidioc_streamon,
425 .vidioc_streamoff = vidioc_streamoff,
426 .vidioc_log_status = vidioc_log_status,
427 .vidioc_g_priority = vidioc_g_priority,
428 .vidioc_s_priority = vidioc_s_priority,
02b20b0b 429#ifdef CONFIG_VIDEO_V4L1_COMPAT
1a9fc855 430 .vidiocgmbuf = vidiocgmbuf,
02b20b0b
MCC
431#endif
432#ifdef TUNER_FLAG
1a9fc855
MCC
433 .vidioc_g_tuner = vidioc_g_tuner,
434 .vidioc_s_tuner = vidioc_s_tuner,
435 .vidioc_g_frequency = vidioc_g_frequency,
436 .vidioc_s_frequency = vidioc_s_frequency,
02b20b0b
MCC
437#endif
438#ifdef CONFIG_VIDEO_ADV_DEBUG
1a9fc855
MCC
439 .vidioc_g_register = vidioc_g_register,
440 .vidioc_s_register = vidioc_s_register,
02b20b0b
MCC
441#endif
442};
443
444struct video_device cx25821_video_template0 = {
1a9fc855
MCC
445 .name = "cx25821-video",
446 .fops = &video_fops,
447 .minor = -1,
448 .ioctl_ops = &video_ioctl_ops,
449 .tvnorms = CX25821_NORMS,
450 .current_norm = V4L2_STD_NTSC_M,
02b20b0b 451};
This page took 0.10028 seconds and 5 git commands to generate.