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