]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/cx25821/cx25821-vidups10.c
V4L/DVB (13554a): v4l: Use the video_drvdata function in drivers
[net-next-2.6.git] / drivers / staging / cx25821 / cx25821-vidups10.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_CH10];
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_CH10]);
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
102         printk("open minor=%d type=%s\n", minor, v4l2_type_names[type]);
103
104         /* allocate + initialize per filehandle data */
105         fh = kzalloc(sizeof(*fh), GFP_KERNEL);
106         if (NULL == fh)
107                 return -ENOMEM;
108
109         lock_kernel();
110
111         file->private_data = fh;
112         fh->dev = dev;
113         fh->type = type;
114         fh->width = 720;
115
116         if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
117                 fh->height = 576;
118         else
119                 fh->height = 480;
120
121         dev->channel_opened = 9;
122         fh->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
123
124         v4l2_prio_open(&dev->prio, &fh->prio);
125
126         videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops,
127                                &dev->pci->dev, &dev->slock,
128                                V4L2_BUF_TYPE_VIDEO_CAPTURE,
129                                V4L2_FIELD_INTERLACED,
130                                sizeof(struct cx25821_buffer), fh);
131
132         dprintk(1, "post videobuf_queue_init()\n");
133         unlock_kernel();
134
135         return 0;
136 }
137
138 static ssize_t video_read(struct file *file, char __user * data, size_t count,
139                           loff_t * ppos)
140 {
141         struct cx25821_fh *fh = file->private_data;
142
143         switch (fh->type) {
144         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
145                 if (res_locked(fh->dev, RESOURCE_VIDEO10))
146                         return -EBUSY;
147
148                 return videobuf_read_one(&fh->vidq, data, count, ppos,
149                                          file->f_flags & O_NONBLOCK);
150
151         default:
152                 BUG();
153                 return 0;
154         }
155 }
156
157 static unsigned int video_poll(struct file *file,
158                                struct poll_table_struct *wait)
159 {
160         struct cx25821_fh *fh = file->private_data;
161         struct cx25821_buffer *buf;
162
163         if (res_check(fh, RESOURCE_VIDEO10)) {
164                 /* streaming capture */
165                 if (list_empty(&fh->vidq.stream))
166                         return POLLERR;
167                 buf = list_entry(fh->vidq.stream.next,
168                                  struct cx25821_buffer, vb.stream);
169         } else {
170                 /* read() capture */
171                 buf = (struct cx25821_buffer *)fh->vidq.read_buf;
172                 if (NULL == buf)
173                         return POLLERR;
174         }
175
176         poll_wait(file, &buf->vb.done, wait);
177         if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR)
178                 return POLLIN | POLLRDNORM;
179         return 0;
180 }
181
182 static int video_release(struct file *file)
183 {
184         struct cx25821_fh *fh = file->private_data;
185         struct cx25821_dev *dev = fh->dev;
186
187         //stop the risc engine and fifo
188         //cx_write(channel10->dma_ctl, 0);
189
190         /* stop video capture */
191         if (res_check(fh, RESOURCE_VIDEO10)) {
192                 videobuf_queue_cancel(&fh->vidq);
193                 res_free(dev, fh, RESOURCE_VIDEO10);
194         }
195
196         if (fh->vidq.read_buf) {
197                 buffer_release(&fh->vidq, fh->vidq.read_buf);
198                 kfree(fh->vidq.read_buf);
199         }
200
201         videobuf_mmap_free(&fh->vidq);
202
203         v4l2_prio_close(&dev->prio, &fh->prio);
204
205         file->private_data = NULL;
206         kfree(fh);
207
208         return 0;
209 }
210
211 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
212 {
213         struct cx25821_fh *fh = priv;
214         struct cx25821_dev *dev = fh->dev;
215
216         if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)) {
217                 return -EINVAL;
218         }
219
220         if (unlikely(i != fh->type)) {
221                 return -EINVAL;
222         }
223
224         if (unlikely(!res_get(dev, fh, get_resource(fh, RESOURCE_VIDEO10)))) {
225                 return -EBUSY;
226         }
227
228         return videobuf_streamon(get_queue(fh));
229 }
230
231 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
232 {
233         struct cx25821_fh *fh = priv;
234         struct cx25821_dev *dev = fh->dev;
235         int err, res;
236
237         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
238                 return -EINVAL;
239         if (i != fh->type)
240                 return -EINVAL;
241
242         res = get_resource(fh, RESOURCE_VIDEO10);
243         err = videobuf_streamoff(get_queue(fh));
244         if (err < 0)
245                 return err;
246         res_free(dev, fh, res);
247         return 0;
248 }
249
250 static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
251                                    unsigned long arg)
252 {
253         struct cx25821_fh *fh = file->private_data;
254         struct cx25821_dev *dev = fh->dev;
255         int command = 0;
256         struct upstream_user_struct *data_from_user;
257
258         data_from_user = (struct upstream_user_struct *)arg;
259
260         if (!data_from_user) {
261                 printk
262                     ("cx25821 in %s(): Upstream data is INVALID. Returning.\n",
263                      __func__);
264                 return 0;
265         }
266
267         command = data_from_user->command;
268
269         if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO) {
270                 return 0;
271         }
272
273         dev->input_filename_ch2 = data_from_user->input_filename;
274         dev->input_audiofilename = data_from_user->input_filename;
275         dev->vid_stdname_ch2 = data_from_user->vid_stdname;
276         dev->pixel_format_ch2 = data_from_user->pixel_format;
277         dev->channel_select_ch2 = data_from_user->channel_select;
278         dev->command_ch2 = data_from_user->command;
279
280         switch (command) {
281         case UPSTREAM_START_VIDEO:
282                 cx25821_start_upstream_video_ch2(dev, data_from_user);
283                 break;
284
285         case UPSTREAM_STOP_VIDEO:
286                 cx25821_stop_upstream_video_ch2(dev);
287                 break;
288         }
289
290         return 0;
291 }
292
293 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
294                                 struct v4l2_format *f)
295 {
296         struct cx25821_fh *fh = priv;
297         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
298         int err;
299
300         if (fh) {
301                 err = v4l2_prio_check(&dev->prio, &fh->prio);
302                 if (0 != err)
303                         return err;
304         }
305
306         dprintk(2, "%s()\n", __func__);
307         err = vidioc_try_fmt_vid_cap(file, priv, f);
308
309         if (0 != err)
310                 return err;
311         fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
312         fh->width = f->fmt.pix.width;
313         fh->height = f->fmt.pix.height;
314         fh->vidq.field = f->fmt.pix.field;
315         dprintk(2, "%s() width=%d height=%d field=%d\n", __func__, fh->width,
316                 fh->height, fh->vidq.field);
317         cx25821_call_all(dev, video, s_fmt, f);
318         return 0;
319 }
320
321 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
322 {
323         struct cx25821_fh *fh = priv;
324         return videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
325 }
326
327 static int vidioc_log_status(struct file *file, void *priv)
328 {
329         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
330         char name[32 + 2];
331
332         snprintf(name, sizeof(name), "%s/2", dev->name);
333         printk(KERN_INFO "%s/2: ============  START LOG STATUS  ============\n",
334                dev->name);
335         cx25821_call_all(dev, core, log_status);
336         printk(KERN_INFO "%s/2: =============  END LOG STATUS  =============\n",
337                dev->name);
338         return 0;
339 }
340
341 static int vidioc_s_ctrl(struct file *file, void *priv,
342                          struct v4l2_control *ctl)
343 {
344         struct cx25821_fh *fh = priv;
345         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
346         int err;
347
348         if (fh) {
349                 err = v4l2_prio_check(&dev->prio, &fh->prio);
350                 if (0 != err)
351                         return err;
352         }
353
354         return 0;
355 }
356
357 //exported stuff
358 static const struct v4l2_file_operations video_fops = {
359         .owner = THIS_MODULE,
360         .open = video_open,
361         .release = video_release,
362         .read = video_read,
363         .poll = video_poll,
364         .mmap = video_mmap,
365         .ioctl = video_ioctl_upstream10,
366 };
367
368 static const struct v4l2_ioctl_ops video_ioctl_ops = {
369         .vidioc_querycap = vidioc_querycap,
370         .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
371         .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
372         .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
373         .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
374         .vidioc_reqbufs = vidioc_reqbufs,
375         .vidioc_querybuf = vidioc_querybuf,
376         .vidioc_qbuf = vidioc_qbuf,
377         .vidioc_dqbuf = vidioc_dqbuf,
378 #ifdef TUNER_FLAG
379         .vidioc_s_std = vidioc_s_std,
380         .vidioc_querystd = vidioc_querystd,
381 #endif
382         .vidioc_cropcap = vidioc_cropcap,
383         .vidioc_s_crop = vidioc_s_crop,
384         .vidioc_g_crop = vidioc_g_crop,
385         .vidioc_enum_input = vidioc_enum_input,
386         .vidioc_g_input = vidioc_g_input,
387         .vidioc_s_input = vidioc_s_input,
388         .vidioc_g_ctrl = vidioc_g_ctrl,
389         .vidioc_s_ctrl = vidioc_s_ctrl,
390         .vidioc_queryctrl = vidioc_queryctrl,
391         .vidioc_streamon = vidioc_streamon,
392         .vidioc_streamoff = vidioc_streamoff,
393         .vidioc_log_status = vidioc_log_status,
394         .vidioc_g_priority = vidioc_g_priority,
395         .vidioc_s_priority = vidioc_s_priority,
396 #ifdef CONFIG_VIDEO_V4L1_COMPAT
397         .vidiocgmbuf = vidiocgmbuf,
398 #endif
399 #ifdef TUNER_FLAG
400         .vidioc_g_tuner = vidioc_g_tuner,
401         .vidioc_s_tuner = vidioc_s_tuner,
402         .vidioc_g_frequency = vidioc_g_frequency,
403         .vidioc_s_frequency = vidioc_s_frequency,
404 #endif
405 #ifdef CONFIG_VIDEO_ADV_DEBUG
406         .vidioc_g_register = vidioc_g_register,
407         .vidioc_s_register = vidioc_s_register,
408 #endif
409 };
410
411 struct video_device cx25821_video_template10 = {
412         .name = "cx25821-upstream10",
413         .fops = &video_fops,
414         .minor = -1,
415         .ioctl_ops = &video_ioctl_ops,
416         .tvnorms = CX25821_NORMS,
417         .current_norm = V4L2_STD_NTSC_M,
418 };