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