- patches.arch/x86_mce_intel_decode_physical_address.patch:
[linux-flexiantxendom0-3.2.10.git] / drivers / staging / cx25821 / cx25821-videoioctl.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[VIDEO_IOCTL_CH];
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[VIDEO_IOCTL_CH]);
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 = cx25821_buffer_setup,
90         .buf_prepare = cx25821_buffer_prepare,
91         .buf_queue = buffer_queue,
92         .buf_release = cx25821_buffer_release,
93 };
94
95 static int video_open(struct file *file)
96 {
97         struct video_device *vdev = video_devdata(file);
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         u32 pix_format;
102
103         printk("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 = VIDEO_IOCTL_CH;
124         pix_format = V4L2_PIX_FMT_YUYV;
125         fh->fmt = format_by_fourcc(pix_format);
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 (cx25821_res_locked(fh->dev, RESOURCE_VIDEO_IOCTL))
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 (cx25821_res_check(fh, RESOURCE_VIDEO_IOCTL)) {
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
183         return 0;
184 }
185
186 static int video_release(struct file *file)
187 {
188         struct cx25821_fh *fh = file->private_data;
189         struct cx25821_dev *dev = fh->dev;
190
191         /* stop video capture */
192         if (cx25821_res_check(fh, RESOURCE_VIDEO_IOCTL)) {
193                 videobuf_queue_cancel(&fh->vidq);
194                 cx25821_res_free(dev, fh, RESOURCE_VIDEO_IOCTL);
195         }
196
197         if (fh->vidq.read_buf) {
198                 cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
199                 kfree(fh->vidq.read_buf);
200         }
201
202         videobuf_mmap_free(&fh->vidq);
203
204         v4l2_prio_close(&dev->prio, fh->prio);
205
206         file->private_data = NULL;
207         kfree(fh);
208
209         return 0;
210 }
211
212 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
213 {
214         struct cx25821_fh *fh = priv;
215         struct cx25821_dev *dev = fh->dev;
216
217         if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)) {
218                 return -EINVAL;
219         }
220
221         if (unlikely(i != fh->type)) {
222                 return -EINVAL;
223         }
224
225         if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh, RESOURCE_VIDEO_IOCTL)))) {
226                 return -EBUSY;
227         }
228
229         return videobuf_streamon(get_queue(fh));
230 }
231
232 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
233 {
234         struct cx25821_fh *fh = priv;
235         struct cx25821_dev *dev = fh->dev;
236         int err, res;
237
238         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
239                 return -EINVAL;
240         if (i != fh->type)
241                 return -EINVAL;
242
243         res = cx25821_get_resource(fh, RESOURCE_VIDEO_IOCTL);
244         err = videobuf_streamoff(get_queue(fh));
245         if (err < 0)
246                 return err;
247         cx25821_res_free(dev, fh, res);
248         return 0;
249 }
250
251 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
252                                 struct v4l2_format *f)
253 {
254         struct cx25821_fh *fh = priv;
255         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
256         int err;
257
258         if (fh) {
259                 err = v4l2_prio_check(&dev->prio, fh->prio);
260                 if (0 != err)
261                         return err;
262         }
263
264         dprintk(2, "%s()\n", __func__);
265         err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
266
267         if (0 != err)
268                 return err;
269         fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
270         fh->width = f->fmt.pix.width;
271         fh->height = f->fmt.pix.height;
272         fh->vidq.field = f->fmt.pix.field;
273         dprintk(2, "%s() width=%d height=%d field=%d\n", __func__, fh->width,
274                 fh->height, fh->vidq.field);
275         cx25821_call_all(dev, video, s_fmt, f);
276         return 0;
277 }
278
279 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
280 {
281         struct cx25821_fh *fh = priv;
282         return videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
283 }
284
285 static long video_ioctl_set(struct file *file, unsigned int cmd,
286                             unsigned long arg)
287 {
288         struct cx25821_fh *fh = file->private_data;
289         struct cx25821_dev *dev = fh->dev;
290         struct downstream_user_struct *data_from_user;
291         int command;
292         int width = 720;
293         int selected_channel = 0, pix_format = 0, i = 0;
294         int cif_enable = 0, cif_width = 0;
295         u32 value = 0;
296
297         data_from_user = (struct downstream_user_struct *)arg;
298
299         if (!data_from_user) {
300                 printk("cx25821 in %s(): User data is INVALID. Returning.\n",
301                        __func__);
302                 return 0;
303         }
304
305         command = data_from_user->command;
306
307         if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
308             && command != ENABLE_CIF_RESOLUTION && command != REG_READ
309             && command != REG_WRITE && command != MEDUSA_READ
310             && command != MEDUSA_WRITE) {
311                 return 0;
312         }
313
314         switch (command) {
315         case SET_VIDEO_STD:
316                 dev->tvnorm =
317                     !strcmp(data_from_user->vid_stdname,
318                             "PAL") ? V4L2_STD_PAL_BG : V4L2_STD_NTSC_M;
319                 medusa_set_videostandard(dev);
320                 break;
321
322         case SET_PIXEL_FORMAT:
323                 selected_channel = data_from_user->decoder_select;
324                 pix_format = data_from_user->pixel_format;
325
326                 if (!(selected_channel <= 7 && selected_channel >= 0)) {
327                         selected_channel -= 4;
328                         selected_channel = selected_channel % 8;
329                 }
330
331                 if (selected_channel >= 0)
332                         cx25821_set_pixel_format(dev, selected_channel,
333                                                  pix_format);
334
335                 break;
336
337         case ENABLE_CIF_RESOLUTION:
338                 selected_channel = data_from_user->decoder_select;
339                 cif_enable = data_from_user->cif_resolution_enable;
340                 cif_width = data_from_user->cif_width;
341
342                 if (cif_enable) {
343                         if (dev->tvnorm & V4L2_STD_PAL_BG
344                             || dev->tvnorm & V4L2_STD_PAL_DK)
345                                 width = 352;
346                         else
347                                 width = (cif_width == 320
348                                          || cif_width == 352) ? cif_width : 320;
349                 }
350
351                 if (!(selected_channel <= 7 && selected_channel >= 0)) {
352                         selected_channel -= 4;
353                         selected_channel = selected_channel % 8;
354                 }
355
356                 if (selected_channel <= 7 && selected_channel >= 0) {
357                         dev->use_cif_resolution[selected_channel] = cif_enable;
358                         dev->cif_width[selected_channel] = width;
359                 } else {
360                         for (i = 0; i < VID_CHANNEL_NUM; i++) {
361                                 dev->use_cif_resolution[i] = cif_enable;
362                                 dev->cif_width[i] = width;
363                         }
364                 }
365
366                 medusa_set_resolution(dev, width, selected_channel);
367                 break;
368         case REG_READ:
369                 data_from_user->reg_data = cx_read(data_from_user->reg_address);
370                 break;
371         case REG_WRITE:
372                 cx_write(data_from_user->reg_address, data_from_user->reg_data);
373                 break;
374         case MEDUSA_READ:
375                 value =
376                     cx25821_i2c_read(&dev->i2c_bus[0],
377                                      (u16) data_from_user->reg_address,
378                                      &data_from_user->reg_data);
379                 break;
380         case MEDUSA_WRITE:
381                 cx25821_i2c_write(&dev->i2c_bus[0],
382                                   (u16) data_from_user->reg_address,
383                                   data_from_user->reg_data);
384                 break;
385         }
386
387         return 0;
388 }
389
390 static int vidioc_log_status(struct file *file, void *priv)
391 {
392         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
393         char name[32 + 2];
394
395         snprintf(name, sizeof(name), "%s/2", dev->name);
396         printk(KERN_INFO "%s/2: ============  START LOG STATUS  ============\n",
397                dev->name);
398         cx25821_call_all(dev, core, log_status);
399         printk(KERN_INFO "%s/2: =============  END LOG STATUS  =============\n",
400                dev->name);
401         return 0;
402 }
403
404 static int vidioc_s_ctrl(struct file *file, void *priv,
405                          struct v4l2_control *ctl)
406 {
407         struct cx25821_fh *fh = priv;
408         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
409         int err;
410
411         if (fh) {
412                 err = v4l2_prio_check(&dev->prio, fh->prio);
413                 if (0 != err)
414                         return err;
415         }
416
417         return 0;
418 }
419
420 // exported stuff
421 static const struct v4l2_file_operations video_fops = {
422         .owner = THIS_MODULE,
423         .open = video_open,
424         .release = video_release,
425         .read = video_read,
426         .poll = video_poll,
427         .mmap = cx25821_video_mmap,
428         .ioctl = video_ioctl_set,
429 };
430
431 static const struct v4l2_ioctl_ops video_ioctl_ops = {
432         .vidioc_querycap = cx25821_vidioc_querycap,
433         .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
434         .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
435         .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
436         .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
437         .vidioc_reqbufs = cx25821_vidioc_reqbufs,
438         .vidioc_querybuf = cx25821_vidioc_querybuf,
439         .vidioc_qbuf = cx25821_vidioc_qbuf,
440         .vidioc_dqbuf = vidioc_dqbuf,
441 #ifdef TUNER_FLAG
442         .vidioc_s_std = cx25821_vidioc_s_std,
443         .vidioc_querystd = cx25821_vidioc_querystd,
444 #endif
445         .vidioc_cropcap = cx25821_vidioc_cropcap,
446         .vidioc_s_crop = cx25821_vidioc_s_crop,
447         .vidioc_g_crop = cx25821_vidioc_g_crop,
448         .vidioc_enum_input = cx25821_vidioc_enum_input,
449         .vidioc_g_input = cx25821_vidioc_g_input,
450         .vidioc_s_input = cx25821_vidioc_s_input,
451         .vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
452         .vidioc_s_ctrl = vidioc_s_ctrl,
453         .vidioc_queryctrl = cx25821_vidioc_queryctrl,
454         .vidioc_streamon = vidioc_streamon,
455         .vidioc_streamoff = vidioc_streamoff,
456         .vidioc_log_status = vidioc_log_status,
457         .vidioc_g_priority = cx25821_vidioc_g_priority,
458         .vidioc_s_priority = cx25821_vidioc_s_priority,
459 #ifdef CONFIG_VIDEO_V4L1_COMPAT
460         .vidiocgmbuf = cx25821_vidiocgmbuf,
461 #endif
462 #ifdef TUNER_FLAG
463         .vidioc_g_tuner = cx25821_vidioc_g_tuner,
464         .vidioc_s_tuner = cx25821_vidioc_s_tuner,
465         .vidioc_g_frequency = cx25821_vidioc_g_frequency,
466         .vidioc_s_frequency = cx25821_vidioc_s_frequency,
467 #endif
468 #ifdef CONFIG_VIDEO_ADV_DEBUG
469         .vidioc_g_register = cx25821_vidioc_g_register,
470         .vidioc_s_register = cx25821_vidioc_s_register,
471 #endif
472 };
473
474 struct video_device cx25821_videoioctl_template = {
475         .name = "cx25821-videoioctl",
476         .fops = &video_fops,
477         .ioctl_ops = &video_ioctl_ops,
478         .tvnorms = CX25821_NORMS,
479         .current_norm = V4L2_STD_NTSC_M,
480 };