]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/cx25821/cx25821-videoioctl.c
V4L/DVB (13556): v4l: Remove unneeded video_device::minor assignments
[net-next-2.6.git] / drivers / staging / cx25821 / cx25821-videoioctl.c
CommitLineData
02b20b0b
MCC
1/*
2 * Driver for the Conexant CX25821 PCIe bridge
3 *
bb4c9a74 4 * Copyright (C) 2009 Conexant Systems Inc.
02b20b0b
MCC
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
02b20b0b
MCC
26static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
27{
1a9fc855
MCC
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);
bb4c9a74 58 } else {
1a9fc855
MCC
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 }
bb4c9a74 81 }
02b20b0b 82
1a9fc855
MCC
83 if (list_empty(&q->active)) {
84 dprintk(2, "active queue empty!\n");
85 }
02b20b0b
MCC
86}
87
02b20b0b 88static struct videobuf_queue_ops cx25821_video_qops = {
1a9fc855
MCC
89 .buf_setup = buffer_setup,
90 .buf_prepare = buffer_prepare,
91 .buf_queue = buffer_queue,
92 .buf_release = buffer_release,
02b20b0b
MCC
93};
94
02b20b0b
MCC
95static int video_open(struct file *file)
96{
50462eb0 97 struct video_device *vdev = video_devdata(file);
63b0d5ad 98 struct cx25821_dev *dev = video_drvdata(file);
1a9fc855 99 struct cx25821_fh *fh;
63b0d5ad 100 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1a9fc855
MCC
101 u32 pix_format;
102
50462eb0
LP
103 printk("open dev=%s type=%s\n", video_device_node_name(vdev),
104 v4l2_type_names[type]);
02b20b0b 105
1a9fc855
MCC
106 /* allocate + initialize per filehandle data */
107 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
63b0d5ad 108 if (NULL == fh)
1a9fc855 109 return -ENOMEM;
63b0d5ad
LP
110
111 lock_kernel();
02b20b0b 112
1a9fc855
MCC
113 file->private_data = fh;
114 fh->dev = dev;
115 fh->type = type;
116 fh->width = 720;
02b20b0b 117
1a9fc855
MCC
118 if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
119 fh->height = 576;
120 else
121 fh->height = 480;
02b20b0b 122
1a9fc855
MCC
123 dev->channel_opened = VIDEO_IOCTL_CH;
124 pix_format = V4L2_PIX_FMT_YUYV;
125 fh->fmt = format_by_fourcc(pix_format);
bb4c9a74 126
1a9fc855 127 v4l2_prio_open(&dev->prio, &fh->prio);
02b20b0b 128
1a9fc855
MCC
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);
02b20b0b 134
1a9fc855
MCC
135 dprintk(1, "post videobuf_queue_init()\n");
136 unlock_kernel();
02b20b0b 137
1a9fc855 138 return 0;
02b20b0b
MCC
139}
140
1a9fc855
MCC
141static ssize_t video_read(struct file *file, char __user * data, size_t count,
142 loff_t * ppos)
02b20b0b 143{
1a9fc855 144 struct cx25821_fh *fh = file->private_data;
02b20b0b 145
1a9fc855 146 switch (fh->type) {
bb4c9a74 147 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1a9fc855
MCC
148 if (res_locked(fh->dev, RESOURCE_VIDEO_IOCTL))
149 return -EBUSY;
02b20b0b 150
1a9fc855
MCC
151 return videobuf_read_one(&fh->vidq, data, count, ppos,
152 file->f_flags & O_NONBLOCK);
02b20b0b 153
bb4c9a74 154 default:
1a9fc855
MCC
155 BUG();
156 return 0;
157 }
02b20b0b
MCC
158}
159
1a9fc855
MCC
160static unsigned int video_poll(struct file *file,
161 struct poll_table_struct *wait)
02b20b0b 162{
1a9fc855
MCC
163 struct cx25821_fh *fh = file->private_data;
164 struct cx25821_buffer *buf;
165
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;
177 }
02b20b0b 178
1a9fc855
MCC
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}
02b20b0b
MCC
185
186static int video_release(struct file *file)
187{
1a9fc855
MCC
188 struct cx25821_fh *fh = file->private_data;
189 struct cx25821_dev *dev = fh->dev;
02b20b0b 190
1a9fc855
MCC
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);
195 }
02b20b0b 196
1a9fc855
MCC
197 if (fh->vidq.read_buf) {
198 buffer_release(&fh->vidq, fh->vidq.read_buf);
199 kfree(fh->vidq.read_buf);
200 }
02b20b0b 201
1a9fc855 202 videobuf_mmap_free(&fh->vidq);
02b20b0b 203
1a9fc855 204 v4l2_prio_close(&dev->prio, &fh->prio);
02b20b0b 205
1a9fc855
MCC
206 file->private_data = NULL;
207 kfree(fh);
02b20b0b 208
1a9fc855 209 return 0;
02b20b0b
MCC
210}
211
02b20b0b
MCC
212static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
213{
1a9fc855
MCC
214 struct cx25821_fh *fh = priv;
215 struct cx25821_dev *dev = fh->dev;
02b20b0b 216
1a9fc855
MCC
217 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)) {
218 return -EINVAL;
219 }
02b20b0b 220
1a9fc855
MCC
221 if (unlikely(i != fh->type)) {
222 return -EINVAL;
223 }
02b20b0b 224
1a9fc855
MCC
225 if (unlikely(!res_get(dev, fh, get_resource(fh, RESOURCE_VIDEO_IOCTL)))) {
226 return -EBUSY;
227 }
02b20b0b 228
1a9fc855 229 return videobuf_streamon(get_queue(fh));
02b20b0b
MCC
230}
231
232static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
233{
1a9fc855
MCC
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 = 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;
02b20b0b
MCC
249}
250
1a9fc855
MCC
251static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
252 struct v4l2_format *f)
02b20b0b 253{
1a9fc855
MCC
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 = vidioc_try_fmt_vid_cap(file, priv, f);
02b20b0b 266
bb4c9a74 267 if (0 != err)
1a9fc855
MCC
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;
02b20b0b
MCC
277}
278
279static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
280{
1a9fc855
MCC
281 struct cx25821_fh *fh = priv;
282 return videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
02b20b0b
MCC
283}
284
1a9fc855
MCC
285static long video_ioctl_set(struct file *file, unsigned int cmd,
286 unsigned long arg)
bb4c9a74 287{
1a9fc855
MCC
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 }
bb4c9a74 304
1a9fc855 305 command = data_from_user->command;
bb4c9a74 306
1a9fc855
MCC
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 }
bb4c9a74 313
1a9fc855 314 switch (command) {
bb4c9a74 315 case SET_VIDEO_STD:
1a9fc855
MCC
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;
bb4c9a74
MCC
321
322 case SET_PIXEL_FORMAT:
1a9fc855
MCC
323 selected_channel = data_from_user->decoder_select;
324 pix_format = data_from_user->pixel_format;
bb4c9a74 325
1a9fc855
MCC
326 if (!(selected_channel <= 7 && selected_channel >= 0)) {
327 selected_channel -= 4;
328 selected_channel = selected_channel % 8;
329 }
bb4c9a74 330
1a9fc855
MCC
331 if (selected_channel >= 0)
332 cx25821_set_pixel_format(dev, selected_channel,
333 pix_format);
bb4c9a74 334
1a9fc855 335 break;
bb4c9a74
MCC
336
337 case ENABLE_CIF_RESOLUTION:
1a9fc855
MCC
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;
bb4c9a74 349 }
bb4c9a74 350
1a9fc855
MCC
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;
bb4c9a74
MCC
368 case REG_READ:
369 data_from_user->reg_data = cx_read(data_from_user->reg_address);
1a9fc855 370 break;
bb4c9a74
MCC
371 case REG_WRITE:
372 cx_write(data_from_user->reg_address, data_from_user->reg_data);
1a9fc855 373 break;
bb4c9a74 374 case MEDUSA_READ:
1a9fc855
MCC
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;
bb4c9a74 380 case MEDUSA_WRITE:
1a9fc855
MCC
381 cx25821_i2c_write(&dev->i2c_bus[0],
382 (u16) data_from_user->reg_address,
383 data_from_user->reg_data);
384 break;
385 }
bb4c9a74 386
1a9fc855 387 return 0;
02b20b0b
MCC
388}
389
1a9fc855 390static int vidioc_log_status(struct file *file, void *priv)
02b20b0b 391{
1a9fc855
MCC
392 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
393 char name[32 + 2];
02b20b0b 394
1a9fc855
MCC
395 snprintf(name, sizeof(name), "%s/2", dev->name);
396 printk(KERN_INFO "%s/2: ============ START LOG STATUS ============\n",
02b20b0b 397 dev->name);
1a9fc855
MCC
398 cx25821_call_all(dev, core, log_status);
399 printk(KERN_INFO "%s/2: ============= END LOG STATUS =============\n",
02b20b0b 400 dev->name);
1a9fc855 401 return 0;
02b20b0b
MCC
402}
403
404static int vidioc_s_ctrl(struct file *file, void *priv,
1a9fc855 405 struct v4l2_control *ctl)
02b20b0b 406{
1a9fc855
MCC
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 }
02b20b0b
MCC
416
417 return 0;
418}
1a9fc855 419
02b20b0b
MCC
420// exported stuff
421static const struct v4l2_file_operations video_fops = {
1a9fc855
MCC
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,
02b20b0b
MCC
429};
430
431static const struct v4l2_ioctl_ops video_ioctl_ops = {
1a9fc855
MCC
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,
02b20b0b 441#ifdef TUNER_FLAG
1a9fc855
MCC
442 .vidioc_s_std = vidioc_s_std,
443 .vidioc_querystd = vidioc_querystd,
02b20b0b 444#endif
1a9fc855
MCC
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,
02b20b0b 459#ifdef CONFIG_VIDEO_V4L1_COMPAT
1a9fc855 460 .vidiocgmbuf = vidiocgmbuf,
02b20b0b
MCC
461#endif
462#ifdef TUNER_FLAG
1a9fc855
MCC
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,
02b20b0b
MCC
467#endif
468#ifdef CONFIG_VIDEO_ADV_DEBUG
1a9fc855
MCC
469 .vidioc_g_register = vidioc_g_register,
470 .vidioc_s_register = vidioc_s_register,
02b20b0b
MCC
471#endif
472};
473
474struct video_device cx25821_videoioctl_template = {
1a9fc855
MCC
475 .name = "cx25821-videoioctl",
476 .fops = &video_fops,
1a9fc855
MCC
477 .ioctl_ops = &video_ioctl_ops,
478 .tvnorms = CX25821_NORMS,
479 .current_norm = V4L2_STD_NTSC_M,
02b20b0b 480};