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