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