Linux 2.6.33-rc6
[cris-mirror.git] / drivers / staging / cx25821 / cx25821-videoioctl.c
blob1da52b54a454dcafaebfbac8f28ead1cc3fa90b8
1 /*
2 * Driver for the Conexant CX25821 PCIe bridge
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
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.
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
17 * GNU General Public License for more details.
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.
24 #include "cx25821-video.h"
26 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
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];
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 */
40 dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
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);
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);
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);
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);
83 if (list_empty(&q->active)) {
84 dprintk(2, "active queue empty!\n");
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,
95 static int video_open(struct file *file)
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;
103 printk("open dev=%s type=%s\n", video_device_node_name(vdev),
104 v4l2_type_names[type]);
106 /* allocate + initialize per filehandle data */
107 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
108 if (NULL == fh)
109 return -ENOMEM;
111 lock_kernel();
113 file->private_data = fh;
114 fh->dev = dev;
115 fh->type = type;
116 fh->width = 720;
118 if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
119 fh->height = 576;
120 else
121 fh->height = 480;
123 dev->channel_opened = VIDEO_IOCTL_CH;
124 pix_format = V4L2_PIX_FMT_YUYV;
125 fh->fmt = format_by_fourcc(pix_format);
127 v4l2_prio_open(&dev->prio, &fh->prio);
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);
135 dprintk(1, "post videobuf_queue_init()\n");
136 unlock_kernel();
138 return 0;
141 static ssize_t video_read(struct file *file, char __user * data, size_t count,
142 loff_t * ppos)
144 struct cx25821_fh *fh = file->private_data;
146 switch (fh->type) {
147 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
148 if (res_locked(fh->dev, RESOURCE_VIDEO_IOCTL))
149 return -EBUSY;
151 return videobuf_read_one(&fh->vidq, data, count, ppos,
152 file->f_flags & O_NONBLOCK);
154 default:
155 BUG();
156 return 0;
160 static unsigned int video_poll(struct file *file,
161 struct poll_table_struct *wait)
163 struct cx25821_fh *fh = file->private_data;
164 struct cx25821_buffer *buf;
166 if (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;
179 poll_wait(file, &buf->vb.done, wait);
180 if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR)
181 return POLLIN | POLLRDNORM;
183 return 0;
186 static int video_release(struct file *file)
188 struct cx25821_fh *fh = file->private_data;
189 struct cx25821_dev *dev = fh->dev;
191 /* stop video capture */
192 if (res_check(fh, RESOURCE_VIDEO_IOCTL)) {
193 videobuf_queue_cancel(&fh->vidq);
194 res_free(dev, fh, RESOURCE_VIDEO_IOCTL);
197 if (fh->vidq.read_buf) {
198 buffer_release(&fh->vidq, fh->vidq.read_buf);
199 kfree(fh->vidq.read_buf);
202 videobuf_mmap_free(&fh->vidq);
204 v4l2_prio_close(&dev->prio, &fh->prio);
206 file->private_data = NULL;
207 kfree(fh);
209 return 0;
212 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
214 struct cx25821_fh *fh = priv;
215 struct cx25821_dev *dev = fh->dev;
217 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)) {
218 return -EINVAL;
221 if (unlikely(i != fh->type)) {
222 return -EINVAL;
225 if (unlikely(!res_get(dev, fh, get_resource(fh, RESOURCE_VIDEO_IOCTL)))) {
226 return -EBUSY;
229 return videobuf_streamon(get_queue(fh));
232 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
234 struct cx25821_fh *fh = priv;
235 struct cx25821_dev *dev = fh->dev;
236 int err, res;
238 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
239 return -EINVAL;
240 if (i != fh->type)
241 return -EINVAL;
243 res = get_resource(fh, RESOURCE_VIDEO_IOCTL);
244 err = videobuf_streamoff(get_queue(fh));
245 if (err < 0)
246 return err;
247 res_free(dev, fh, res);
248 return 0;
251 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
252 struct v4l2_format *f)
254 struct cx25821_fh *fh = priv;
255 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
256 int err;
258 if (fh) {
259 err = v4l2_prio_check(&dev->prio, &fh->prio);
260 if (0 != err)
261 return err;
264 dprintk(2, "%s()\n", __func__);
265 err = vidioc_try_fmt_vid_cap(file, priv, f);
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;
279 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
281 struct cx25821_fh *fh = priv;
282 return videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
285 static long video_ioctl_set(struct file *file, unsigned int cmd,
286 unsigned long arg)
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;
297 data_from_user = (struct downstream_user_struct *)arg;
299 if (!data_from_user) {
300 printk("cx25821 in %s(): User data is INVALID. Returning.\n",
301 __func__);
302 return 0;
305 command = data_from_user->command;
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;
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;
322 case SET_PIXEL_FORMAT:
323 selected_channel = data_from_user->decoder_select;
324 pix_format = data_from_user->pixel_format;
326 if (!(selected_channel <= 7 && selected_channel >= 0)) {
327 selected_channel -= 4;
328 selected_channel = selected_channel % 8;
331 if (selected_channel >= 0)
332 cx25821_set_pixel_format(dev, selected_channel,
333 pix_format);
335 break;
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;
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;
351 if (!(selected_channel <= 7 && selected_channel >= 0)) {
352 selected_channel -= 4;
353 selected_channel = selected_channel % 8;
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;
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;
387 return 0;
390 static int vidioc_log_status(struct file *file, void *priv)
392 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
393 char name[32 + 2];
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;
404 static int vidioc_s_ctrl(struct file *file, void *priv,
405 struct v4l2_control *ctl)
407 struct cx25821_fh *fh = priv;
408 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
409 int err;
411 if (fh) {
412 err = v4l2_prio_check(&dev->prio, &fh->prio);
413 if (0 != err)
414 return err;
417 return 0;
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 = video_mmap,
428 .ioctl = video_ioctl_set,
431 static const struct v4l2_ioctl_ops video_ioctl_ops = {
432 .vidioc_querycap = vidioc_querycap,
433 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
434 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
435 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
436 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
437 .vidioc_reqbufs = vidioc_reqbufs,
438 .vidioc_querybuf = vidioc_querybuf,
439 .vidioc_qbuf = vidioc_qbuf,
440 .vidioc_dqbuf = vidioc_dqbuf,
441 #ifdef TUNER_FLAG
442 .vidioc_s_std = vidioc_s_std,
443 .vidioc_querystd = vidioc_querystd,
444 #endif
445 .vidioc_cropcap = vidioc_cropcap,
446 .vidioc_s_crop = vidioc_s_crop,
447 .vidioc_g_crop = vidioc_g_crop,
448 .vidioc_enum_input = vidioc_enum_input,
449 .vidioc_g_input = vidioc_g_input,
450 .vidioc_s_input = vidioc_s_input,
451 .vidioc_g_ctrl = vidioc_g_ctrl,
452 .vidioc_s_ctrl = vidioc_s_ctrl,
453 .vidioc_queryctrl = vidioc_queryctrl,
454 .vidioc_streamon = vidioc_streamon,
455 .vidioc_streamoff = vidioc_streamoff,
456 .vidioc_log_status = vidioc_log_status,
457 .vidioc_g_priority = vidioc_g_priority,
458 .vidioc_s_priority = vidioc_s_priority,
459 #ifdef CONFIG_VIDEO_V4L1_COMPAT
460 .vidiocgmbuf = vidiocgmbuf,
461 #endif
462 #ifdef TUNER_FLAG
463 .vidioc_g_tuner = vidioc_g_tuner,
464 .vidioc_s_tuner = vidioc_s_tuner,
465 .vidioc_g_frequency = vidioc_g_frequency,
466 .vidioc_s_frequency = vidioc_s_frequency,
467 #endif
468 #ifdef CONFIG_VIDEO_ADV_DEBUG
469 .vidioc_g_register = vidioc_g_register,
470 .vidioc_s_register = vidioc_s_register,
471 #endif
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,