c5410c1558e4ba8e9c0ece4e48ee80eccfb8b2f4
[safe/jmp/linux-2.6] / drivers / staging / cx25821 / cx25821-video6.c
1 /*
2  *  Driver for the Conexant CX25821 PCIe bridge
3  *
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  *
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
26 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
27 {
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_CH06];
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_CH06]);
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);
58         } else {
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                 }
81         }
82
83         if (list_empty(&q->active)) {
84                 dprintk(2, "active queue empty!\n");
85         }
86 }
87
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,
93 };
94
95 static int video_open(struct file *file)
96 {
97         int minor = video_devdata(file)->minor;
98         struct cx25821_dev *dev = video_drvdata(file);
99         struct cx25821_fh *fh;
100         enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
101         u32 pix_format;
102
103         printk("open minor=%d type=%s\n", minor, v4l2_type_names[type]);
104
105         /* allocate + initialize per filehandle data */
106         fh = kzalloc(sizeof(*fh), GFP_KERNEL);
107         if (NULL == fh)
108                 return -ENOMEM;
109
110         lock_kernel();
111
112         file->private_data = fh;
113         fh->dev = dev;
114         fh->type = type;
115         fh->width = 720;
116
117         if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
118                 fh->height = 576;
119         else
120                 fh->height = 480;
121
122         dev->channel_opened = SRAM_CH06;
123         pix_format =
124             (dev->pixel_formats[dev->channel_opened] ==
125              PIXEL_FRMT_411) ? V4L2_PIX_FMT_Y41P : V4L2_PIX_FMT_YUYV;
126         fh->fmt = format_by_fourcc(pix_format);
127
128         v4l2_prio_open(&dev->prio, &fh->prio);
129
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);
135
136         dprintk(1, "post videobuf_queue_init()\n");
137         unlock_kernel();
138
139         return 0;
140 }
141
142 static ssize_t video_read(struct file *file, char __user * data, size_t count,
143                           loff_t * ppos)
144 {
145         struct cx25821_fh *fh = file->private_data;
146
147         switch (fh->type) {
148         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
149                 if (res_locked(fh->dev, RESOURCE_VIDEO6))
150                         return -EBUSY;
151
152                 return videobuf_read_one(&fh->vidq, data, count, ppos,
153                                          file->f_flags & O_NONBLOCK);
154
155         default:
156                 BUG();
157                 return 0;
158         }
159 }
160
161 static unsigned int video_poll(struct file *file,
162                                struct poll_table_struct *wait)
163 {
164         struct cx25821_fh *fh = file->private_data;
165         struct cx25821_buffer *buf;
166
167         if (res_check(fh, RESOURCE_VIDEO6)) {
168                 /* streaming capture */
169                 if (list_empty(&fh->vidq.stream))
170                         return POLLERR;
171                 buf = list_entry(fh->vidq.stream.next,
172                                  struct cx25821_buffer, vb.stream);
173         } else {
174                 /* read() capture */
175                 buf = (struct cx25821_buffer *)fh->vidq.read_buf;
176                 if (NULL == buf)
177                         return POLLERR;
178         }
179
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;
184
185                         if (dev && dev->use_cif_resolution[SRAM_CH06]) {
186                                 u8 cam_id = *((char *)buf->vb.baddr + 3);
187                                 memcpy((char *)buf->vb.baddr,
188                                        (char *)buf->vb.baddr + (fh->width * 2),
189                                        (fh->width * 2));
190                                 *((char *)buf->vb.baddr + 3) = cam_id;
191                         }
192                 }
193
194                 return POLLIN | POLLRDNORM;
195         }
196
197         return 0;
198 }
199
200 static int video_release(struct file *file)
201 {
202         struct cx25821_fh *fh = file->private_data;
203         struct cx25821_dev *dev = fh->dev;
204
205         //stop the risc engine and fifo
206         cx_write(channel6->dma_ctl, 0); /* FIFO and RISC disable */
207
208         /* stop video capture */
209         if (res_check(fh, RESOURCE_VIDEO6)) {
210                 videobuf_queue_cancel(&fh->vidq);
211                 res_free(dev, fh, RESOURCE_VIDEO6);
212         }
213         if (fh->vidq.read_buf) {
214                 buffer_release(&fh->vidq, fh->vidq.read_buf);
215                 kfree(fh->vidq.read_buf);
216         }
217
218         videobuf_mmap_free(&fh->vidq);
219
220         v4l2_prio_close(&dev->prio, &fh->prio);
221         file->private_data = NULL;
222         kfree(fh);
223
224         return 0;
225 }
226
227 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
228 {
229         struct cx25821_fh *fh = priv;
230         struct cx25821_dev *dev = fh->dev;
231
232         if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)) {
233                 return -EINVAL;
234         }
235
236         if (unlikely(i != fh->type)) {
237                 return -EINVAL;
238         }
239
240         if (unlikely(!res_get(dev, fh, get_resource(fh, RESOURCE_VIDEO6)))) {
241                 return -EBUSY;
242         }
243
244         return videobuf_streamon(get_queue(fh));
245 }
246
247 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
248 {
249         struct cx25821_fh *fh = priv;
250         struct cx25821_dev *dev = fh->dev;
251         int err, res;
252
253         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
254                 return -EINVAL;
255         if (i != fh->type)
256                 return -EINVAL;
257
258         res = get_resource(fh, RESOURCE_VIDEO6);
259         err = videobuf_streamoff(get_queue(fh));
260         if (err < 0)
261                 return err;
262         res_free(dev, fh, res);
263         return 0;
264 }
265
266 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
267                                 struct v4l2_format *f)
268 {
269         struct cx25821_fh *fh = priv;
270         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
271         int err;
272         int pix_format = 0;
273
274         if (fh) {
275                 err = v4l2_prio_check(&dev->prio, &fh->prio);
276                 if (0 != err)
277                         return err;
278         }
279
280         dprintk(2, "%s()\n", __func__);
281         err = vidioc_try_fmt_vid_cap(file, priv, f);
282
283         if (0 != err)
284                 return err;
285
286         fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
287         fh->vidq.field = f->fmt.pix.field;
288
289         // check if width and height is valid based on set standard
290         if (is_valid_width(f->fmt.pix.width, dev->tvnorm)) {
291                 fh->width = f->fmt.pix.width;
292         }
293
294         if (is_valid_height(f->fmt.pix.height, dev->tvnorm)) {
295                 fh->height = f->fmt.pix.height;
296         }
297
298         if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
299                 pix_format = PIXEL_FRMT_411;
300         else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
301                 pix_format = PIXEL_FRMT_422;
302         else
303                 return -EINVAL;
304
305         cx25821_set_pixel_format(dev, SRAM_CH06, pix_format);
306
307         // check if cif resolution
308         if (fh->width == 320 || fh->width == 352) {
309                 dev->use_cif_resolution[SRAM_CH06] = 1;
310         } else {
311                 dev->use_cif_resolution[SRAM_CH06] = 0;
312         }
313         dev->cif_width[SRAM_CH06] = fh->width;
314         medusa_set_resolution(dev, fh->width, SRAM_CH06);
315
316         dprintk(2, "%s() width=%d height=%d field=%d\n", __func__, fh->width,
317                 fh->height, fh->vidq.field);
318         cx25821_call_all(dev, video, s_fmt, f);
319
320         return 0;
321 }
322
323 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
324 {
325         int ret_val = 0;
326         struct cx25821_fh *fh = priv;
327         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
328
329         ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
330
331         p->sequence = dev->vidq[SRAM_CH06].count;
332
333         return ret_val;
334 }
335
336 static int vidioc_log_status(struct file *file, void *priv)
337 {
338         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
339         char name[32 + 2];
340
341         struct sram_channel *sram_ch = &dev->sram_channels[SRAM_CH06];
342         u32 tmp = 0;
343
344         snprintf(name, sizeof(name), "%s/2", dev->name);
345         printk(KERN_INFO "%s/2: ============  START LOG STATUS  ============\n",
346                dev->name);
347         cx25821_call_all(dev, core, log_status);
348
349         tmp = cx_read(sram_ch->dma_ctl);
350         printk(KERN_INFO "Video input 6 is %s\n",
351                (tmp & 0x11) ? "streaming" : "stopped");
352         printk(KERN_INFO "%s/2: =============  END LOG STATUS  =============\n",
353                dev->name);
354         return 0;
355 }
356
357 static int vidioc_s_ctrl(struct file *file, void *priv,
358                          struct v4l2_control *ctl)
359 {
360         struct cx25821_fh *fh = priv;
361         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
362         int err;
363
364         if (fh) {
365                 err = v4l2_prio_check(&dev->prio, &fh->prio);
366                 if (0 != err)
367                         return err;
368         }
369
370         return cx25821_set_control(dev, ctl, SRAM_CH06);
371 }
372
373 // exported stuff
374 static const struct v4l2_file_operations video_fops = {
375         .owner = THIS_MODULE,
376         .open = video_open,
377         .release = video_release,
378         .read = video_read,
379         .poll = video_poll,
380         .mmap = video_mmap,
381         .ioctl = video_ioctl2,
382 };
383
384 static const struct v4l2_ioctl_ops video_ioctl_ops = {
385         .vidioc_querycap = vidioc_querycap,
386         .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
387         .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
388         .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
389         .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
390         .vidioc_reqbufs = vidioc_reqbufs,
391         .vidioc_querybuf = vidioc_querybuf,
392         .vidioc_qbuf = vidioc_qbuf,
393         .vidioc_dqbuf = vidioc_dqbuf,
394 #ifdef TUNER_FLAG
395         .vidioc_s_std = vidioc_s_std,
396         .vidioc_querystd = vidioc_querystd,
397 #endif
398         .vidioc_cropcap = vidioc_cropcap,
399         .vidioc_s_crop = vidioc_s_crop,
400         .vidioc_g_crop = vidioc_g_crop,
401         .vidioc_enum_input = vidioc_enum_input,
402         .vidioc_g_input = vidioc_g_input,
403         .vidioc_s_input = vidioc_s_input,
404         .vidioc_g_ctrl = vidioc_g_ctrl,
405         .vidioc_s_ctrl = vidioc_s_ctrl,
406         .vidioc_queryctrl = vidioc_queryctrl,
407         .vidioc_streamon = vidioc_streamon,
408         .vidioc_streamoff = vidioc_streamoff,
409         .vidioc_log_status = vidioc_log_status,
410         .vidioc_g_priority = vidioc_g_priority,
411         .vidioc_s_priority = vidioc_s_priority,
412 #ifdef CONFIG_VIDEO_V4L1_COMPAT
413         .vidiocgmbuf = vidiocgmbuf,
414 #endif
415 #ifdef TUNER_FLAG
416         .vidioc_g_tuner = vidioc_g_tuner,
417         .vidioc_s_tuner = vidioc_s_tuner,
418         .vidioc_g_frequency = vidioc_g_frequency,
419         .vidioc_s_frequency = vidioc_s_frequency,
420 #endif
421 #ifdef CONFIG_VIDEO_ADV_DEBUG
422         .vidioc_g_register = vidioc_g_register,
423         .vidioc_s_register = vidioc_s_register,
424 #endif
425 };
426
427 struct video_device cx25821_video_template6 = {
428         .name = "cx25821-video",
429         .fops = &video_fops,
430         .minor = -1,
431         .ioctl_ops = &video_ioctl_ops,
432         .tvnorms = CX25821_NORMS,
433         .current_norm = V4L2_STD_NTSC_M,
434 };