V4L/DVB (12730): Add conexant cx25821 driver
[deliverable/linux.git] / drivers / staging / cx25821 / cx25821-video.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 MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
27 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
28 MODULE_LICENSE("GPL");
29
30 static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
31 static unsigned int radio_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
32
33 module_param_array(video_nr, int, NULL, 0444);
34 module_param_array(radio_nr, int, NULL, 0444);
35
36 MODULE_PARM_DESC(video_nr, "video device numbers");
37 MODULE_PARM_DESC(radio_nr, "radio device numbers");
38
39 static unsigned int video_debug=VIDEO_DEBUG;
40 module_param(video_debug, int, 0644);
41 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
42
43 static unsigned int irq_debug;
44 module_param(irq_debug, int, 0644);
45 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
46
47 unsigned int vid_limit = 16;
48 module_param(vid_limit, int, 0644);
49 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
50
51 static void init_controls(struct cx25821_dev *dev, int chan_num);
52
53 #define FORMAT_FLAGS_PACKED 0x01
54
55 struct cx25821_fmt formats[] = {
56 {
57 .name = "8 bpp, gray",
58 .fourcc = V4L2_PIX_FMT_GREY,
59 .depth = 8,
60 .flags = FORMAT_FLAGS_PACKED,
61 }, {
62 .name = "4:1:1, packed, Y41P",
63 .fourcc = V4L2_PIX_FMT_Y41P,
64 .depth = 12,
65 .flags = FORMAT_FLAGS_PACKED,
66 }, {
67 .name = "4:2:2, packed, YUYV",
68 .fourcc = V4L2_PIX_FMT_YUYV,
69 .depth = 16,
70 .flags = FORMAT_FLAGS_PACKED,
71 }, {
72 .name = "4:2:2, packed, UYVY",
73 .fourcc = V4L2_PIX_FMT_UYVY,
74 .depth = 16,
75 .flags = FORMAT_FLAGS_PACKED,
76 },{
77 .name = "4:2:0, YUV",
78 .fourcc = V4L2_PIX_FMT_YUV420,
79 .depth = 12,
80 .flags = FORMAT_FLAGS_PACKED,
81 },
82 };
83
84
85 int get_format_size(void)
86 {
87 return ARRAY_SIZE(formats);
88 }
89
90
91 struct cx25821_fmt *format_by_fourcc(unsigned int fourcc)
92 {
93 unsigned int i;
94
95 if( fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P )
96 {
97 return formats+1;
98 }
99
100 for (i = 0; i < ARRAY_SIZE(formats); i++)
101 if (formats[i].fourcc == fourcc)
102 return formats+i;
103
104 printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
105 return NULL;
106 }
107
108 void dump_video_queue(struct cx25821_dev *dev, struct cx25821_dmaqueue *q)
109 {
110 struct cx25821_buffer *buf;
111 struct list_head *item;
112 dprintk(1, "%s()\n", __func__);
113
114 if (!list_empty(&q->active)) {
115 list_for_each(item, &q->active)
116 buf = list_entry(item, struct cx25821_buffer, vb.queue);
117 }
118
119 if (!list_empty(&q->queued))
120 {
121 list_for_each(item, &q->queued)
122 buf = list_entry(item, struct cx25821_buffer, vb.queue);
123 }
124
125 }
126
127
128 void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q, u32 count)
129 {
130 struct cx25821_buffer *buf;
131 int bc;
132
133 for (bc = 0;; bc++) {
134 if (list_empty(&q->active))
135 {
136 dprintk(1, "bc=%d (=0: active empty)\n", bc);
137 break;
138 }
139
140 buf = list_entry(q->active.next, struct cx25821_buffer, vb.queue);
141
142 /* count comes from the hw and it is 16bit wide --
143 * this trick handles wrap-arounds correctly for
144 * up to 32767 buffers in flight... */
145 if ((s16) (count - buf->count) < 0)
146 {
147 break;
148 }
149
150 do_gettimeofday(&buf->vb.ts);
151 buf->vb.state = VIDEOBUF_DONE;
152 list_del(&buf->vb.queue);
153 wake_up(&buf->vb.done);
154 }
155
156 if (list_empty(&q->active))
157 del_timer(&q->timeout);
158 else
159 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
160 if (bc != 1)
161 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n", __func__, bc);
162 }
163
164 #ifdef TUNER_FLAG
165 int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
166 {
167 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n", __func__,
168 (unsigned int)norm,
169 v4l2_norm_to_name(norm));
170
171 dev->tvnorm = norm;
172
173 /* Tell the internal A/V decoder */
174 cx25821_call_all(dev, core, s_std, norm);
175
176 return 0;
177 }
178 #endif
179
180 struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
181 struct pci_dev *pci,
182 struct video_device *template,
183 char *type)
184 {
185 struct video_device *vfd;
186 dprintk(1, "%s()\n", __func__);
187
188 vfd = video_device_alloc();
189 if (NULL == vfd)
190 return NULL;
191 *vfd = *template;
192 vfd->minor = -1;
193 vfd->v4l2_dev = &dev->v4l2_dev;
194 vfd->release = video_device_release;
195 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, type, cx25821_boards[dev->board].name);
196 return vfd;
197 }
198
199 /*
200 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
201 {
202 int i;
203
204 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
205 return -EINVAL;
206 for (i = 0; i < CX25821_CTLS; i++)
207 if (cx25821_ctls[i].v.id == qctrl->id)
208 break;
209 if (i == CX25821_CTLS) {
210 *qctrl = no_ctl;
211 return 0;
212 }
213 *qctrl = cx25821_ctls[i].v;
214 return 0;
215 }
216 */
217
218 // resource management
219 int res_get(struct cx25821_dev *dev, struct cx25821_fh *fh, unsigned int bit)
220 {
221 dprintk(1, "%s()\n", __func__);
222 if (fh->resources & bit)
223 /* have it already allocated */
224 return 1;
225
226 /* is it free? */
227 mutex_lock(&dev->lock);
228 if (dev->resources & bit) {
229 /* no, someone else uses it */
230 mutex_unlock(&dev->lock);
231 return 0;
232 }
233 /* it's free, grab it */
234 fh->resources |= bit;
235 dev->resources |= bit;
236 dprintk(1, "res: get %d\n", bit);
237 mutex_unlock(&dev->lock);
238 return 1;
239 }
240
241 int res_check(struct cx25821_fh *fh, unsigned int bit)
242 {
243 return fh->resources & bit;
244 }
245
246 int res_locked(struct cx25821_dev *dev, unsigned int bit)
247 {
248 return dev->resources & bit;
249 }
250
251 void res_free(struct cx25821_dev *dev, struct cx25821_fh *fh, unsigned int bits)
252 {
253 BUG_ON((fh->resources & bits) != bits);
254 dprintk(1, "%s()\n", __func__);
255
256 mutex_lock(&dev->lock);
257 fh->resources &= ~bits;
258 dev->resources &= ~bits;
259 dprintk(1, "res: put %d\n", bits);
260 mutex_unlock(&dev->lock);
261 }
262
263 int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
264 {
265 struct v4l2_routing route;
266 memset(&route, 0, sizeof(route));
267
268 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n", __func__,
269 input, INPUT(input)->vmux,
270 INPUT(input)->gpio0, INPUT(input)->gpio1,
271 INPUT(input)->gpio2, INPUT(input)->gpio3);
272 dev->input = input;
273
274 route.input = INPUT(input)->vmux;
275
276 /* Tell the internal A/V decoder */
277 cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
278
279 return 0;
280 }
281
282 int cx25821_start_video_dma(struct cx25821_dev *dev,
283 struct cx25821_dmaqueue *q,
284 struct cx25821_buffer *buf,
285 struct sram_channel *channel)
286 {
287 int tmp = 0;
288
289 /* setup fifo + format */
290 cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
291
292 /* reset counter */
293 cx_write(channel->gpcnt_ctl, 3);
294 q->count = 1;
295
296 /* enable irq */
297 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1<<channel->i));
298 cx_set(channel->int_msk, 0x11);
299
300 /* start dma */
301 cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
302
303 /* make sure upstream setting if any is reversed */
304 tmp = cx_read( VID_CH_MODE_SEL );
305 cx_write( VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
306
307 return 0;
308 }
309
310
311 int cx25821_restart_video_queue(struct cx25821_dev *dev, struct cx25821_dmaqueue *q, struct sram_channel *channel)
312 {
313 struct cx25821_buffer *buf, *prev;
314 struct list_head *item;
315
316 if (!list_empty(&q->active)) {
317 buf = list_entry(q->active.next, struct cx25821_buffer, vb.queue);
318
319 cx25821_start_video_dma(dev, q, buf, channel);
320
321 list_for_each(item, &q->active) {
322 buf = list_entry(item, struct cx25821_buffer, vb.queue);
323 buf->count = q->count++;
324 }
325
326 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
327 return 0;
328 }
329
330 prev = NULL;
331 for (;;) {
332 if (list_empty(&q->queued))
333 return 0;
334
335 buf = list_entry(q->queued.next, struct cx25821_buffer, vb.queue);
336
337 if (NULL == prev) {
338 list_move_tail(&buf->vb.queue, &q->active);
339 cx25821_start_video_dma(dev, q, buf, channel);
340 buf->vb.state = VIDEOBUF_ACTIVE;
341 buf->count = q->count++;
342 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
343 } else if (prev->vb.width == buf->vb.width &&
344 prev->vb.height == buf->vb.height &&
345 prev->fmt == buf->fmt) {
346 list_move_tail(&buf->vb.queue, &q->active);
347 buf->vb.state = VIDEOBUF_ACTIVE;
348 buf->count = q->count++;
349 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
350 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
351 } else {
352 return 0;
353 }
354 prev = buf;
355 }
356 }
357
358 void cx25821_vid_timeout(unsigned long data)
359 {
360 struct cx25821_data *timeout_data = (struct cx25821_data *)data;
361 struct cx25821_dev *dev = timeout_data->dev;
362 struct sram_channel *channel = timeout_data->channel;
363 struct cx25821_dmaqueue *q = &dev->vidq[channel->i];
364 struct cx25821_buffer *buf;
365 unsigned long flags;
366
367 //cx25821_sram_channel_dump(dev, channel);
368 cx_clear(channel->dma_ctl, 0x11);
369
370 spin_lock_irqsave(&dev->slock, flags);
371 while (!list_empty(&q->active)) {
372 buf = list_entry(q->active.next, struct cx25821_buffer, vb.queue);
373 list_del(&buf->vb.queue);
374
375 buf->vb.state = VIDEOBUF_ERROR;
376 wake_up(&buf->vb.done);
377 }
378
379 cx25821_restart_video_queue(dev, q, channel);
380 spin_unlock_irqrestore(&dev->slock, flags);
381 }
382
383 int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
384 {
385 u32 count=0;
386 int handled = 0;
387 u32 mask;
388 struct sram_channel *channel = &dev->sram_channels[chan_num];
389
390 mask = cx_read(channel->int_msk);
391 if (0 == (status & mask))
392 return handled;
393
394 cx_write(channel->int_stat, status);
395
396 /* risc op code error */
397 if (status & (1 << 16)) {
398 printk(KERN_WARNING "%s, %s: video risc op code error\n", dev->name, channel->name);
399 cx_clear(channel->dma_ctl, 0x11);
400 cx25821_sram_channel_dump(dev, channel);
401 }
402
403 /* risc1 y */
404 if (status & FLD_VID_DST_RISC1) {
405 spin_lock(&dev->slock);
406 count = cx_read(channel->gpcnt);
407 cx25821_video_wakeup(dev, &dev->vidq[channel->i], count);
408 spin_unlock(&dev->slock);
409 handled++;
410 }
411
412 /* risc2 y */
413 if (status & 0x10) {
414 dprintk(2, "stopper video\n");
415 spin_lock(&dev->slock);
416 cx25821_restart_video_queue(dev, &dev->vidq[channel->i], channel);
417 spin_unlock(&dev->slock);
418 handled++;
419 }
420 return handled;
421 }
422
423 void cx25821_videoioctl_unregister(struct cx25821_dev *dev)
424 {
425 if( dev->ioctl_dev )
426 {
427 if (dev->ioctl_dev->minor != -1)
428 video_unregister_device(dev->ioctl_dev);
429 else
430 video_device_release(dev->ioctl_dev);
431
432 dev->ioctl_dev = NULL;
433 }
434 }
435
436 void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
437 {
438 cx_clear(PCI_INT_MSK, 1);
439
440 if (dev->video_dev[chan_num]) {
441 if (-1 != dev->video_dev[chan_num]->minor)
442 video_unregister_device(dev->video_dev[chan_num]);
443 else
444 video_device_release(dev->video_dev[chan_num]);
445
446 dev->video_dev[chan_num] = NULL;
447
448 btcx_riscmem_free(dev->pci, &dev->vidq[chan_num].stopper);
449
450 printk(KERN_WARNING "device %d released!\n", chan_num);
451 }
452
453 }
454
455
456 int cx25821_video_register(struct cx25821_dev *dev, int chan_num, struct video_device *video_template)
457 {
458 int err;
459
460 spin_lock_init(&dev->slock);
461
462 //printk(KERN_WARNING "Channel %d\n", chan_num);
463
464 #ifdef TUNER_FLAG
465 dev->tvnorm = video_template->current_norm;
466 #endif
467
468 /* init video dma queues */
469 dev->timeout_data[chan_num].dev = dev;
470 dev->timeout_data[chan_num].channel = &dev->sram_channels[chan_num];
471 INIT_LIST_HEAD(&dev->vidq[chan_num].active);
472 INIT_LIST_HEAD(&dev->vidq[chan_num].queued);
473 dev->vidq[chan_num].timeout.function = cx25821_vid_timeout;
474 dev->vidq[chan_num].timeout.data = (unsigned long)&dev->timeout_data[chan_num];
475 init_timer(&dev->vidq[chan_num].timeout);
476 cx25821_risc_stopper(dev->pci, &dev->vidq[chan_num].stopper, dev->sram_channels[chan_num].dma_ctl, 0x11, 0);
477
478
479 /* register v4l devices */
480 dev->video_dev[chan_num] = cx25821_vdev_init(dev, dev->pci, video_template, "video");
481 err = video_register_device(dev->video_dev[chan_num], VFL_TYPE_GRABBER, video_nr[dev->nr]);
482
483 if (err < 0) {
484 goto fail_unreg;
485 }
486
487 //set PCI interrupt
488 cx_set(PCI_INT_MSK, 0xff);
489
490
491 /* initial device configuration */
492 mutex_lock(&dev->lock);
493 #ifdef TUNER_FLAG
494 cx25821_set_tvnorm(dev, dev->tvnorm);
495 #endif
496 mutex_unlock(&dev->lock);
497
498 init_controls(dev, chan_num);
499
500 return 0;
501
502 fail_unreg:
503 cx25821_video_unregister(dev, chan_num);
504 return err;
505 }
506
507 int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
508 {
509 struct cx25821_fh *fh = q->priv_data;
510
511 *size = fh->fmt->depth*fh->width*fh->height >> 3;
512
513
514 if (0 == *count)
515 *count = 32;
516
517 while (*size * *count > vid_limit * 1024 * 1024)
518 (*count)--;
519
520 return 0;
521 }
522
523 int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb, enum v4l2_field field)
524 {
525 struct cx25821_fh *fh = q->priv_data;
526 struct cx25821_dev *dev = fh->dev;
527 struct cx25821_buffer *buf = container_of(vb, struct cx25821_buffer, vb);
528 int rc, init_buffer = 0;
529 u32 line0_offset, line1_offset;
530 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
531 int bpl_local = LINE_SIZE_D1;
532 int channel_opened = 0;
533
534
535 BUG_ON(NULL == fh->fmt);
536 if (fh->width < 48 || fh->width > 720 ||
537 fh->height < 32 || fh->height > 576)
538 return -EINVAL;
539
540 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
541
542 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
543 return -EINVAL;
544
545
546 if (buf->fmt != fh->fmt ||
547 buf->vb.width != fh->width ||
548 buf->vb.height != fh->height ||
549 buf->vb.field != field) {
550 buf->fmt = fh->fmt;
551 buf->vb.width = fh->width;
552 buf->vb.height = fh->height;
553 buf->vb.field = field;
554 init_buffer = 1;
555 }
556
557 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
558 init_buffer = 1;
559 rc = videobuf_iolock(q, &buf->vb, NULL);
560 if (0 != rc)
561 {
562 printk(KERN_DEBUG "videobuf_iolock failed!\n");
563 goto fail;
564 }
565 }
566
567 dprintk(1, "init_buffer=%d\n", init_buffer);
568
569 if (init_buffer) {
570
571 channel_opened = dev->channel_opened;
572 channel_opened = (channel_opened < 0 || channel_opened > 7) ? 7 : channel_opened;
573
574 if( dev->pixel_formats[channel_opened] == PIXEL_FRMT_411 )
575 buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
576 else
577 buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
578
579
580 if( dev->pixel_formats[channel_opened] == PIXEL_FRMT_411 )
581 {
582 bpl_local = buf->bpl;
583 }
584 else
585 {
586 bpl_local = buf->bpl; //Default
587
588 if( channel_opened >= 0 && channel_opened <= 7 )
589 {
590 if( dev->use_cif_resolution[channel_opened] )
591 {
592 if( dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK )
593 bpl_local = 352 << 1;
594 else
595 bpl_local = dev->cif_width[channel_opened] << 1;
596 }
597 }
598 }
599
600
601 switch (buf->vb.field) {
602 case V4L2_FIELD_TOP:
603 cx25821_risc_buffer(dev->pci, &buf->risc,
604 dma->sglist, 0, UNSET,
605 buf->bpl, 0, buf->vb.height);
606 break;
607 case V4L2_FIELD_BOTTOM:
608 cx25821_risc_buffer(dev->pci, &buf->risc,
609 dma->sglist, UNSET, 0,
610 buf->bpl, 0, buf->vb.height);
611 break;
612 case V4L2_FIELD_INTERLACED:
613 /* All other formats are top field first */
614 line0_offset = 0;
615 line1_offset = buf->bpl;
616 dprintk(1, "top field first\n");
617
618 cx25821_risc_buffer(dev->pci, &buf->risc,
619 dma->sglist, line0_offset,
620 bpl_local, bpl_local, bpl_local,
621 buf->vb.height >> 1);
622 break;
623 case V4L2_FIELD_SEQ_TB:
624 cx25821_risc_buffer(dev->pci, &buf->risc,
625 dma->sglist,
626 0, buf->bpl * (buf->vb.height >> 1),
627 buf->bpl, 0,
628 buf->vb.height >> 1);
629 break;
630 case V4L2_FIELD_SEQ_BT:
631 cx25821_risc_buffer(dev->pci, &buf->risc,
632 dma->sglist,
633 buf->bpl * (buf->vb.height >> 1), 0,
634 buf->bpl, 0,
635 buf->vb.height >> 1);
636 break;
637 default:
638 BUG();
639 }
640 }
641
642 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
643 buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
644 (unsigned long)buf->risc.dma);
645
646 buf->vb.state = VIDEOBUF_PREPARED;
647
648 return 0;
649
650 fail:
651 cx25821_free_buffer(q, buf);
652 return rc;
653 }
654
655
656 void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
657 {
658 struct cx25821_buffer *buf = container_of(vb, struct cx25821_buffer, vb);
659
660 cx25821_free_buffer(q, buf);
661 }
662
663
664 struct videobuf_queue *get_queue(struct cx25821_fh *fh)
665 {
666 switch (fh->type) {
667 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
668 return &fh->vidq;
669 default:
670 BUG();
671 return NULL;
672 }
673 }
674
675 int get_resource(struct cx25821_fh *fh, int resource)
676 {
677 switch (fh->type) {
678 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
679 return resource;
680 default:
681 BUG();
682 return 0;
683 }
684 }
685
686
687 int video_mmap(struct file *file, struct vm_area_struct *vma)
688 {
689 struct cx25821_fh *fh = file->private_data;
690
691 return videobuf_mmap_mapper(get_queue(fh), vma);
692 }
693
694 /* VIDEO IOCTLS */
695 int vidioc_g_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f)
696 {
697 struct cx25821_fh *fh = priv;
698
699 f->fmt.pix.width = fh->width;
700 f->fmt.pix.height = fh->height;
701 f->fmt.pix.field = fh->vidq.field;
702 f->fmt.pix.pixelformat = fh->fmt->fourcc;
703 f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
704 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
705
706 return 0;
707 }
708
709 int vidioc_try_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f)
710 {
711 struct cx25821_fmt *fmt;
712 enum v4l2_field field;
713 unsigned int maxw, maxh;
714
715 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
716 if (NULL == fmt)
717 return -EINVAL;
718
719 field = f->fmt.pix.field;
720 maxw = 720;
721 maxh = 576;
722
723 if (V4L2_FIELD_ANY == field) {
724 field = (f->fmt.pix.height > maxh/2)
725 ? V4L2_FIELD_INTERLACED
726 : V4L2_FIELD_TOP;
727 }
728
729 switch (field) {
730 case V4L2_FIELD_TOP:
731 case V4L2_FIELD_BOTTOM:
732 maxh = maxh / 2;
733 break;
734 case V4L2_FIELD_INTERLACED:
735 break;
736 default:
737 return -EINVAL;
738 }
739
740 f->fmt.pix.field = field;
741 if (f->fmt.pix.height < 32)
742 f->fmt.pix.height = 32;
743 if (f->fmt.pix.height > maxh)
744 f->fmt.pix.height = maxh;
745 if (f->fmt.pix.width < 48)
746 f->fmt.pix.width = 48;
747 if (f->fmt.pix.width > maxw)
748 f->fmt.pix.width = maxw;
749 f->fmt.pix.width &= ~0x03;
750 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
751 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
752
753 return 0;
754 }
755
756
757
758 int vidioc_querycap(struct file *file, void *priv, struct v4l2_capability *cap)
759 {
760 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
761
762 strcpy(cap->driver, "cx25821");
763 strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
764 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
765 cap->version = CX25821_VERSION_CODE;
766 cap->capabilities =
767 V4L2_CAP_VIDEO_CAPTURE |
768 V4L2_CAP_READWRITE |
769 V4L2_CAP_STREAMING;
770 if (UNSET != dev->tuner_type)
771 cap->capabilities |= V4L2_CAP_TUNER;
772 return 0;
773 }
774
775 int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, struct v4l2_fmtdesc *f)
776 {
777 if (unlikely(f->index >= ARRAY_SIZE(formats)))
778 return -EINVAL;
779
780 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
781 f->pixelformat = formats[f->index].fourcc;
782
783 return 0;
784 }
785
786 #ifdef CONFIG_VIDEO_V4L1_COMPAT
787 int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
788 {
789 struct cx25821_fh *fh = priv;
790 struct videobuf_queue *q;
791 struct v4l2_requestbuffers req;
792 unsigned int i;
793 int err;
794
795 q = get_queue(fh);
796 memset(&req, 0, sizeof(req));
797 req.type = q->type;
798 req.count = 8;
799 req.memory = V4L2_MEMORY_MMAP;
800 err = videobuf_reqbufs(q, &req);
801 if (err < 0)
802 return err;
803
804 mbuf->frames = req.count;
805 mbuf->size = 0;
806 for (i = 0; i < mbuf->frames; i++) {
807 mbuf->offsets[i] = q->bufs[i]->boff;
808 mbuf->size += q->bufs[i]->bsize;
809 }
810 return 0;
811 }
812 #endif
813
814 int vidioc_reqbufs(struct file *file, void *priv, struct v4l2_requestbuffers *p)
815 {
816 struct cx25821_fh *fh = priv;
817 return videobuf_reqbufs(get_queue(fh), p);
818 }
819
820 int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
821 {
822 struct cx25821_fh *fh = priv;
823 return videobuf_querybuf(get_queue(fh), p);
824 }
825
826 int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
827 {
828 struct cx25821_fh *fh = priv;
829 return videobuf_qbuf(get_queue(fh), p);
830 }
831
832 int vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
833 {
834 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
835
836 *p = v4l2_prio_max(&dev->prio);
837
838 return 0;
839 }
840
841 int vidioc_s_priority(struct file *file, void *f,
842 enum v4l2_priority prio)
843 {
844 struct cx25821_fh *fh = f;
845 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
846
847 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
848 }
849
850
851 #ifdef TUNER_FLAG
852 int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
853 {
854 struct cx25821_fh *fh = priv;
855 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
856 int err;
857
858 dprintk(1, "%s()\n", __func__);
859
860 if (fh)
861 {
862 err = v4l2_prio_check(&dev->prio, &fh->prio);
863 if (0 != err)
864 return err;
865 }
866
867 if( dev->tvnorm == *tvnorms )
868 {
869 return 0;
870 }
871
872 mutex_lock(&dev->lock);
873 cx25821_set_tvnorm(dev, *tvnorms);
874 mutex_unlock(&dev->lock);
875
876 medusa_set_videostandard(dev);
877
878 return 0;
879 }
880 #endif
881
882 int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
883 {
884 static const char *iname[] = {
885 [CX25821_VMUX_COMPOSITE] = "Composite",
886 [CX25821_VMUX_SVIDEO] = "S-Video",
887 [CX25821_VMUX_DEBUG] = "for debug only",
888 };
889 unsigned int n;
890 dprintk(1, "%s()\n", __func__);
891
892 n = i->index;
893 if (n > 2)
894 return -EINVAL;
895
896 if (0 == INPUT(n)->type)
897 return -EINVAL;
898
899 memset(i, 0, sizeof(*i));
900 i->index = n;
901 i->type = V4L2_INPUT_TYPE_CAMERA;
902 strcpy(i->name, iname[INPUT(n)->type]);
903
904 i->std = CX25821_NORMS;
905 return 0;
906 }
907
908 int vidioc_enum_input(struct file *file, void *priv, struct v4l2_input *i)
909 {
910 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
911 dprintk(1, "%s()\n", __func__);
912 return cx25821_enum_input(dev, i);
913 }
914
915 int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
916 {
917 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
918
919 *i = dev->input;
920 dprintk(1, "%s() returns %d\n", __func__, *i);
921 return 0;
922 }
923
924
925 int vidioc_s_input(struct file *file, void *priv, unsigned int i)
926 {
927 struct cx25821_fh *fh = priv;
928 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
929 int err;
930
931 dprintk(1, "%s(%d)\n", __func__, i);
932
933 if (fh)
934 {
935 err = v4l2_prio_check(&dev->prio, &fh->prio);
936 if (0 != err)
937 return err;
938 }
939
940 if (i > 2) {
941 dprintk(1, "%s() -EINVAL\n", __func__);
942 return -EINVAL;
943 }
944
945 mutex_lock(&dev->lock);
946 cx25821_video_mux(dev, i);
947 mutex_unlock(&dev->lock);
948 return 0;
949 }
950
951 #ifdef TUNER_FLAG
952 int vidioc_g_frequency(struct file *file, void *priv, struct v4l2_frequency *f)
953 {
954 struct cx25821_fh *fh = priv;
955 struct cx25821_dev *dev = fh->dev;
956
957 f->frequency = dev->freq;
958
959 cx25821_call_all(dev, tuner, g_frequency, f);
960
961 return 0;
962 }
963
964 int cx25821_set_freq(struct cx25821_dev *dev, struct v4l2_frequency *f)
965 {
966 mutex_lock(&dev->lock);
967 dev->freq = f->frequency;
968
969 cx25821_call_all(dev, tuner, s_frequency, f);
970
971 /* When changing channels it is required to reset TVAUDIO */
972 msleep(10);
973
974 mutex_unlock(&dev->lock);
975
976 return 0;
977 }
978
979 int vidioc_s_frequency(struct file *file, void *priv, struct v4l2_frequency *f)
980 {
981 struct cx25821_fh *fh = priv;
982 struct cx25821_dev *dev = fh->dev;
983 int err;
984
985 if (fh)
986 {
987 err = v4l2_prio_check(&dev->prio, &fh->prio);
988 if (0 != err)
989 return err;
990 }
991
992 return cx25821_set_freq(dev, f);
993 }
994 #endif
995
996 #ifdef CONFIG_VIDEO_ADV_DEBUG
997 int vidioc_g_register(struct file *file, void *fh,
998 struct v4l2_dbg_register *reg)
999 {
1000 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1001
1002 if (!v4l2_chip_match_host(&reg->match))
1003 return -EINVAL;
1004
1005 cx25821_call_all(dev, core, g_register, reg);
1006
1007 return 0;
1008 }
1009
1010 int vidioc_s_register(struct file *file, void *fh,
1011 struct v4l2_dbg_register *reg)
1012 {
1013 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1014
1015 if (!v4l2_chip_match_host(&reg->match))
1016 return -EINVAL;
1017
1018 cx25821_call_all(dev, core, s_register, reg);
1019
1020 return 0;
1021 }
1022
1023 #endif
1024
1025
1026 #ifdef TUNER_FLAG
1027 int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1028 {
1029 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1030
1031 if (unlikely(UNSET == dev->tuner_type))
1032 return -EINVAL;
1033 if (0 != t->index)
1034 return -EINVAL;
1035
1036 strcpy(t->name, "Television");
1037 t->type = V4L2_TUNER_ANALOG_TV;
1038 t->capability = V4L2_TUNER_CAP_NORM;
1039 t->rangehigh = 0xffffffffUL;
1040
1041 t->signal = 0xffff ; /* LOCKED */
1042 return 0;
1043 }
1044
1045 int vidioc_s_tuner(struct file *file, void *priv,
1046 struct v4l2_tuner *t)
1047 {
1048 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1049 struct cx25821_fh *fh = priv;
1050 int err;
1051
1052 if (fh)
1053 {
1054 err = v4l2_prio_check(&dev->prio, &fh->prio);
1055 if (0 != err)
1056 return err;
1057 }
1058
1059 dprintk(1, "%s()\n", __func__);
1060 if (UNSET == dev->tuner_type)
1061 return -EINVAL;
1062 if (0 != t->index)
1063 return -EINVAL;
1064
1065 return 0;
1066 }
1067
1068 #endif
1069 // ******************************************************************************************
1070 static const struct v4l2_queryctrl no_ctl = {
1071 .name = "42",
1072 .flags = V4L2_CTRL_FLAG_DISABLED,
1073 };
1074
1075 static struct v4l2_queryctrl cx25821_ctls[] = {
1076 /* --- video --- */
1077 {
1078 .id = V4L2_CID_BRIGHTNESS,
1079 .name = "Brightness",
1080 .minimum = 0,
1081 .maximum = 10000,
1082 .step = 1,
1083 .default_value = 6200,
1084 .type = V4L2_CTRL_TYPE_INTEGER,
1085 },{
1086 .id = V4L2_CID_CONTRAST,
1087 .name = "Contrast",
1088 .minimum = 0,
1089 .maximum = 10000,
1090 .step = 1,
1091 .default_value = 5000,
1092 .type = V4L2_CTRL_TYPE_INTEGER,
1093 },{
1094 .id = V4L2_CID_SATURATION,
1095 .name = "Saturation",
1096 .minimum = 0,
1097 .maximum = 10000,
1098 .step = 1,
1099 .default_value = 5000,
1100 .type = V4L2_CTRL_TYPE_INTEGER,
1101 },{
1102 .id = V4L2_CID_HUE,
1103 .name = "Hue",
1104 .minimum = 0,
1105 .maximum = 10000,
1106 .step = 1,
1107 .default_value = 5000,
1108 .type = V4L2_CTRL_TYPE_INTEGER,
1109 }
1110 };
1111 static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1112
1113 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1114 {
1115 int i;
1116
1117 if (qctrl->id < V4L2_CID_BASE ||
1118 qctrl->id >= V4L2_CID_LASTP1)
1119 return -EINVAL;
1120 for (i = 0; i < CX25821_CTLS; i++)
1121 if (cx25821_ctls[i].id == qctrl->id)
1122 break;
1123 if (i == CX25821_CTLS) {
1124 *qctrl = no_ctl;
1125 return 0;
1126 }
1127 *qctrl = cx25821_ctls[i];
1128 return 0;
1129 }
1130
1131 int vidioc_queryctrl(struct file *file, void *priv,
1132 struct v4l2_queryctrl *qctrl)
1133 {
1134 return cx25821_ctrl_query(qctrl);
1135 }
1136
1137 /* ------------------------------------------------------------------ */
1138 /* VIDEO CTRL IOCTLS */
1139
1140 static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
1141 {
1142 unsigned int i;
1143
1144 for (i = 0; i < CX25821_CTLS; i++)
1145 if (cx25821_ctls[i].id == id)
1146 return cx25821_ctls+i;
1147 return NULL;
1148 }
1149
1150 int vidioc_g_ctrl(struct file *file,
1151 void *priv,
1152 struct v4l2_control *ctl)
1153 {
1154 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1155
1156 const struct v4l2_queryctrl* ctrl;
1157
1158 ctrl = ctrl_by_id(ctl->id);
1159
1160 if (NULL == ctrl)
1161 return -EINVAL;
1162 switch (ctl->id)
1163 {
1164 case V4L2_CID_BRIGHTNESS:
1165 ctl->value = dev->ctl_bright;
1166 break;
1167 case V4L2_CID_HUE:
1168 ctl->value = dev->ctl_hue;
1169 break;
1170 case V4L2_CID_CONTRAST:
1171 ctl->value = dev->ctl_contrast;
1172 break;
1173 case V4L2_CID_SATURATION:
1174 ctl->value = dev->ctl_saturation;
1175 break;
1176 }
1177 return 0;
1178 }
1179
1180 int cx25821_set_control(struct cx25821_dev *dev,
1181 struct v4l2_control *ctl, int chan_num)
1182 {
1183 int err;
1184 const struct v4l2_queryctrl* ctrl;
1185
1186 err = -EINVAL;
1187
1188 ctrl = ctrl_by_id(ctl->id);
1189
1190 if (NULL == ctrl)
1191 return err;
1192
1193 switch (ctrl->type)
1194 {
1195 case V4L2_CTRL_TYPE_BOOLEAN:
1196 case V4L2_CTRL_TYPE_MENU:
1197 case V4L2_CTRL_TYPE_INTEGER:
1198 if (ctl->value < ctrl->minimum)
1199 ctl->value = ctrl->minimum;
1200 if (ctl->value > ctrl->maximum)
1201 ctl->value = ctrl->maximum;
1202 break;
1203 default:
1204 /* nothing */;
1205 };
1206
1207 switch (ctl->id)
1208 {
1209 case V4L2_CID_BRIGHTNESS:
1210 dev->ctl_bright = ctl->value;
1211 medusa_set_brightness(dev, ctl->value, chan_num);
1212 break;
1213 case V4L2_CID_HUE:
1214 dev->ctl_hue = ctl->value;
1215 medusa_set_hue(dev, ctl->value, chan_num);
1216 break;
1217 case V4L2_CID_CONTRAST:
1218 dev->ctl_contrast = ctl->value;
1219 medusa_set_contrast(dev, ctl->value, chan_num);
1220 break;
1221 case V4L2_CID_SATURATION:
1222 dev->ctl_saturation = ctl->value;
1223 medusa_set_saturation(dev, ctl->value, chan_num);
1224 break;
1225 }
1226
1227 err = 0;
1228
1229 return err;
1230 }
1231
1232 static void init_controls(struct cx25821_dev *dev, int chan_num)
1233 {
1234 struct v4l2_control ctrl;
1235 int i;
1236 for (i = 0; i < CX25821_CTLS; i++) {
1237 ctrl.id = cx25821_ctls[i].id;
1238 ctrl.value = cx25821_ctls[i].default_value;
1239
1240 cx25821_set_control(dev, &ctrl, chan_num);
1241 }
1242 }
1243
1244 int vidioc_cropcap(struct file *file,
1245 void *priv,
1246 struct v4l2_cropcap *cropcap)
1247 {
1248 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1249
1250 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1251 return -EINVAL;
1252 cropcap->bounds.top = cropcap->bounds.left = 0;
1253 cropcap->bounds.width = 720;
1254 cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1255 cropcap->pixelaspect.numerator = dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1256 cropcap->pixelaspect.denominator = dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1257 cropcap->defrect = cropcap->bounds;
1258 return 0;
1259 }
1260
1261 int vidioc_s_crop(struct file *file,
1262 void *priv,
1263 struct v4l2_crop *crop)
1264 {
1265 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1266 struct cx25821_fh *fh = priv;
1267 int err;
1268
1269 if (fh)
1270 {
1271 err = v4l2_prio_check(&dev->prio, &fh->prio);
1272 if (0 != err)
1273 return err;
1274 }
1275 // vidioc_s_crop not supported
1276 return -EINVAL;
1277 }
1278
1279 int vidioc_g_crop(struct file *file,
1280 void *priv,
1281 struct v4l2_crop *crop)
1282 {
1283 // vidioc_g_crop not supported
1284 return -EINVAL;
1285 }
1286
1287 int vidioc_querystd(struct file *file,
1288 void *priv,
1289 v4l2_std_id *norm)
1290 {
1291 // medusa does not support video standard sensing of current input
1292 *norm = CX25821_NORMS;
1293
1294 return 0;
1295 }
1296
1297 int is_valid_width(u32 width, v4l2_std_id tvnorm)
1298 {
1299 if(tvnorm == V4L2_STD_PAL_BG)
1300 {
1301 if (width == 352 || width == 720)
1302 return 1;
1303 else
1304 return 0;
1305 }
1306
1307 if(tvnorm == V4L2_STD_NTSC_M)
1308 {
1309 if (width == 320 || width == 352 || width == 720)
1310 return 1;
1311 else
1312 return 0;
1313 }
1314 return 0;
1315 }
1316
1317 int is_valid_height(u32 height, v4l2_std_id tvnorm)
1318 {
1319 if(tvnorm == V4L2_STD_PAL_BG)
1320 {
1321 if (height == 576 || height == 288)
1322 return 1;
1323 else
1324 return 0;
1325 }
1326
1327 if(tvnorm == V4L2_STD_NTSC_M)
1328 {
1329 if (height == 480 || height == 240)
1330 return 1;
1331 else
1332 return 0;
1333 }
1334
1335 return 0;
1336 }
1337
This page took 0.06497 seconds and 5 git commands to generate.