3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
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
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
30 #include "saa7134-reg.h"
33 /* ------------------------------------------------------------------ */
35 static unsigned int video_debug = 0;
36 static unsigned int gbuffers = 8;
37 static unsigned int noninterlaced = 0;
38 static unsigned int gbufsize = 720*576*4;
39 static unsigned int gbufsize_max = 720*576*4;
40 module_param(video_debug, int, 0644);
41 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
42 module_param(gbuffers, int, 0444);
43 MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
44 module_param(noninterlaced, int, 0644);
45 MODULE_PARM_DESC(noninterlaced,"video input is noninterlaced");
47 #define dprintk(fmt, arg...) if (video_debug) \
48 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
50 /* ------------------------------------------------------------------ */
51 /* Defines for Video Output Port Register at address 0x191 */
53 /* Bit 0: VIP code T bit polarity */
55 #define VP_T_CODE_P_NON_INVERTED 0x00
56 #define VP_T_CODE_P_INVERTED 0x01
58 /* ------------------------------------------------------------------ */
59 /* Defines for Video Output Port Register at address 0x195 */
61 /* Bit 2: Video output clock delay control */
63 #define VP_CLK_CTRL2_NOT_DELAYED 0x00
64 #define VP_CLK_CTRL2_DELAYED 0x04
66 /* Bit 1: Video output clock invert control */
68 #define VP_CLK_CTRL1_NON_INVERTED 0x00
69 #define VP_CLK_CTRL1_INVERTED 0x02
71 /* ------------------------------------------------------------------ */
72 /* Defines for Video Output Port Register at address 0x196 */
74 /* Bits 2 to 0: VSYNC pin video vertical sync type */
76 #define VP_VS_TYPE_MASK 0x07
78 #define VP_VS_TYPE_OFF 0x00
79 #define VP_VS_TYPE_V123 0x01
80 #define VP_VS_TYPE_V_ITU 0x02
81 #define VP_VS_TYPE_VGATE_L 0x03
82 #define VP_VS_TYPE_RESERVED1 0x04
83 #define VP_VS_TYPE_RESERVED2 0x05
84 #define VP_VS_TYPE_F_ITU 0x06
85 #define VP_VS_TYPE_SC_FID 0x07
87 /* ------------------------------------------------------------------ */
88 /* data structs for video */
90 static int video_out[][9] = {
91 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
94 static struct saa7134_format formats[] = {
97 .fourcc = V4L2_PIX_FMT_GREY,
101 .name = "15 bpp RGB, le",
102 .fourcc = V4L2_PIX_FMT_RGB555,
106 .name = "15 bpp RGB, be",
107 .fourcc = V4L2_PIX_FMT_RGB555X,
112 .name = "16 bpp RGB, le",
113 .fourcc = V4L2_PIX_FMT_RGB565,
117 .name = "16 bpp RGB, be",
118 .fourcc = V4L2_PIX_FMT_RGB565X,
123 .name = "24 bpp RGB, le",
124 .fourcc = V4L2_PIX_FMT_BGR24,
128 .name = "24 bpp RGB, be",
129 .fourcc = V4L2_PIX_FMT_RGB24,
134 .name = "32 bpp RGB, le",
135 .fourcc = V4L2_PIX_FMT_BGR32,
139 .name = "32 bpp RGB, be",
140 .fourcc = V4L2_PIX_FMT_RGB32,
146 .name = "4:2:2 packed, YUYV",
147 .fourcc = V4L2_PIX_FMT_YUYV,
153 .name = "4:2:2 packed, UYVY",
154 .fourcc = V4L2_PIX_FMT_UYVY,
159 .name = "4:2:2 planar, Y-Cb-Cr",
160 .fourcc = V4L2_PIX_FMT_YUV422P,
168 .name = "4:2:0 planar, Y-Cb-Cr",
169 .fourcc = V4L2_PIX_FMT_YUV420,
177 .name = "4:2:0 planar, Y-Cb-Cr",
178 .fourcc = V4L2_PIX_FMT_YVU420,
188 #define FORMATS ARRAY_SIZE(formats)
190 #define NORM_625_50 \
193 .video_v_start = 24, \
194 .video_v_stop = 311, \
195 .vbi_v_start_0 = 7, \
196 .vbi_v_stop_0 = 22, \
197 .vbi_v_start_1 = 319, \
200 #define NORM_525_60 \
203 .video_v_start = 23, \
204 .video_v_stop = 262, \
205 .vbi_v_start_0 = 10, \
206 .vbi_v_stop_0 = 21, \
207 .vbi_v_start_1 = 273, \
210 static struct saa7134_tvnorm tvnorms[] = {
212 .name = "PAL", /* autodetect */
216 .sync_control = 0x18,
217 .luma_control = 0x40,
218 .chroma_ctrl1 = 0x81,
220 .chroma_ctrl2 = 0x06,
225 .id = V4L2_STD_PAL_BG,
228 .sync_control = 0x18,
229 .luma_control = 0x40,
230 .chroma_ctrl1 = 0x81,
232 .chroma_ctrl2 = 0x06,
237 .id = V4L2_STD_PAL_I,
240 .sync_control = 0x18,
241 .luma_control = 0x40,
242 .chroma_ctrl1 = 0x81,
244 .chroma_ctrl2 = 0x06,
249 .id = V4L2_STD_PAL_DK,
252 .sync_control = 0x18,
253 .luma_control = 0x40,
254 .chroma_ctrl1 = 0x81,
256 .chroma_ctrl2 = 0x06,
264 .sync_control = 0x59,
265 .luma_control = 0x40,
266 .chroma_ctrl1 = 0x89,
268 .chroma_ctrl2 = 0x0e,
273 .id = V4L2_STD_SECAM,
276 .sync_control = 0x18, /* old: 0x58, */
277 .luma_control = 0x1b,
278 .chroma_ctrl1 = 0xd1,
280 .chroma_ctrl2 = 0x00,
285 .id = V4L2_STD_PAL_M,
288 .sync_control = 0x59,
289 .luma_control = 0x40,
290 .chroma_ctrl1 = 0xb9,
292 .chroma_ctrl2 = 0x0e,
297 .id = V4L2_STD_PAL_Nc,
300 .sync_control = 0x18,
301 .luma_control = 0x40,
302 .chroma_ctrl1 = 0xa1,
304 .chroma_ctrl2 = 0x06,
309 .id = V4L2_STD_PAL_60,
317 .vbi_v_start_1 = 273,
320 .sync_control = 0x18,
321 .luma_control = 0x40,
322 .chroma_ctrl1 = 0x81,
324 .chroma_ctrl2 = 0x06,
328 #define TVNORMS ARRAY_SIZE(tvnorms)
330 #define V4L2_CID_PRIVATE_INVERT (V4L2_CID_PRIVATE_BASE + 0)
331 #define V4L2_CID_PRIVATE_Y_ODD (V4L2_CID_PRIVATE_BASE + 1)
332 #define V4L2_CID_PRIVATE_Y_EVEN (V4L2_CID_PRIVATE_BASE + 2)
333 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 3)
334 #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 4)
336 static const struct v4l2_queryctrl no_ctrl = {
338 .flags = V4L2_CTRL_FLAG_DISABLED,
340 static const struct v4l2_queryctrl video_ctrls[] = {
343 .id = V4L2_CID_BRIGHTNESS,
344 .name = "Brightness",
348 .default_value = 128,
349 .type = V4L2_CTRL_TYPE_INTEGER,
351 .id = V4L2_CID_CONTRAST,
357 .type = V4L2_CTRL_TYPE_INTEGER,
359 .id = V4L2_CID_SATURATION,
360 .name = "Saturation",
365 .type = V4L2_CTRL_TYPE_INTEGER,
373 .type = V4L2_CTRL_TYPE_INTEGER,
375 .id = V4L2_CID_HFLIP,
379 .type = V4L2_CTRL_TYPE_BOOLEAN,
383 .id = V4L2_CID_AUDIO_MUTE,
387 .type = V4L2_CTRL_TYPE_BOOLEAN,
389 .id = V4L2_CID_AUDIO_VOLUME,
395 .type = V4L2_CTRL_TYPE_INTEGER,
397 /* --- private --- */
399 .id = V4L2_CID_PRIVATE_INVERT,
403 .type = V4L2_CTRL_TYPE_BOOLEAN,
405 .id = V4L2_CID_PRIVATE_Y_ODD,
406 .name = "y offset odd field",
410 .type = V4L2_CTRL_TYPE_INTEGER,
412 .id = V4L2_CID_PRIVATE_Y_EVEN,
413 .name = "y offset even field",
417 .type = V4L2_CTRL_TYPE_INTEGER,
419 .id = V4L2_CID_PRIVATE_AUTOMUTE,
424 .type = V4L2_CTRL_TYPE_BOOLEAN,
427 static const unsigned int CTRLS = ARRAY_SIZE(video_ctrls);
429 static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
433 for (i = 0; i < CTRLS; i++)
434 if (video_ctrls[i].id == id)
435 return video_ctrls+i;
439 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
443 for (i = 0; i < FORMATS; i++)
444 if (formats[i].fourcc == fourcc)
449 /* ----------------------------------------------------------------------- */
450 /* resource management */
452 static int res_get(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bit)
454 if (fh->resources & bit)
455 /* have it already allocated */
460 if (dev->resources & bit) {
461 /* no, someone else uses it */
465 /* it's free, grab it */
466 fh->resources |= bit;
467 dev->resources |= bit;
468 dprintk("res: get %d\n",bit);
474 int res_check(struct saa7134_fh *fh, unsigned int bit)
476 return (fh->resources & bit);
480 int res_locked(struct saa7134_dev *dev, unsigned int bit)
482 return (dev->resources & bit);
486 void res_free(struct saa7134_dev *dev, struct saa7134_fh *fh, unsigned int bits)
488 if ((fh->resources & bits) != bits)
492 fh->resources &= ~bits;
493 dev->resources &= ~bits;
494 dprintk("res: put %d\n",bits);
498 /* ------------------------------------------------------------------ */
500 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
502 int luma_control,sync_control,mux;
504 dprintk("set tv norm = %s\n",norm->name);
507 mux = card_in(dev,dev->ctl_input).vmux;
508 luma_control = norm->luma_control;
509 sync_control = norm->sync_control;
512 luma_control |= 0x80; /* svideo */
513 if (noninterlaced || dev->nosignal)
514 sync_control |= 0x20;
517 dev->crop_bounds.left = norm->h_start;
518 dev->crop_defrect.left = norm->h_start;
519 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
520 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
522 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
523 dev->crop_defrect.top = norm->video_v_start*2;
524 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
525 - dev->crop_bounds.top;
526 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
528 dev->crop_current = dev->crop_defrect;
530 /* setup video decoder */
531 saa_writeb(SAA7134_INCR_DELAY, 0x08);
532 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
533 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
535 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
536 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
537 saa_writeb(SAA7134_HSYNC_START, 0xeb);
538 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
539 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
541 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
542 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
543 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
544 saa_writeb(SAA7134_DEC_LUMA_CONTRAST, dev->ctl_contrast);
546 saa_writeb(SAA7134_DEC_CHROMA_SATURATION, dev->ctl_saturation);
547 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
548 saa_writeb(SAA7134_CHROMA_CTRL1, norm->chroma_ctrl1);
549 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
551 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
552 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
554 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
555 saa_writeb(SAA7134_VGATE_START, 0x11);
556 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
557 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
558 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
559 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
561 saa7134_i2c_call_clients(dev,VIDIOC_S_STD,&norm->id);
564 static void video_mux(struct saa7134_dev *dev, int input)
566 dprintk("video input = %d [%s]\n",input,card_in(dev,input).name);
567 dev->ctl_input = input;
568 set_tvnorm(dev,dev->tvnorm);
569 saa7134_tvaudio_setinput(dev,&card_in(dev,input));
572 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
574 static const struct {
581 /* XPSC XACL XC2_1 XDCG VPFY */
593 static const int count = ARRAY_SIZE(vals);
596 for (i = 0; i < count; i++)
597 if (vals[i].xpsc == prescale)
602 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
603 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
604 saa_writeb(SAA7134_LEVEL_CTRL(task),
605 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
606 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
607 (vals[i].vpfy << 2) | vals[i].vpfy);
610 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
614 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
615 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
617 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
620 dprintk("yscale LPI yscale=%d\n",yscale);
621 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
622 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
623 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
626 val = 0x40 * 1024 / yscale;
627 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
628 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
629 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
630 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
632 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
635 static void set_size(struct saa7134_dev *dev, int task,
636 int width, int height, int interlace)
638 int prescale,xscale,yscale,y_even,y_odd;
639 int h_start, h_stop, v_start, v_stop;
640 int div = interlace ? 2 : 1;
642 /* setup video scaler */
643 h_start = dev->crop_current.left;
644 v_start = dev->crop_current.top/2;
645 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
646 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
648 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
649 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
650 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
651 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
652 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
653 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
654 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
655 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
657 prescale = dev->crop_current.width / width;
660 xscale = 1024 * dev->crop_current.width / prescale / width;
661 yscale = 512 * div * dev->crop_current.height / height;
662 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
663 set_h_prescale(dev,task,prescale);
664 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
665 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
666 set_v_scale(dev,task,yscale);
668 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
669 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
670 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
671 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
673 /* deinterlace y offsets */
674 y_odd = dev->ctl_y_odd;
675 y_even = dev->ctl_y_even;
676 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
677 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
678 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
679 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
682 /* ------------------------------------------------------------------ */
690 static void sort_cliplist(struct cliplist *cl, int entries)
692 struct cliplist swap;
695 for (i = entries-2; i >= 0; i--) {
696 for (n = 0, j = 0; j <= i; j++) {
697 if (cl[j].position > cl[j+1].position) {
709 static void set_cliplist(struct saa7134_dev *dev, int reg,
710 struct cliplist *cl, int entries, char *name)
715 for (i = 0; i < entries; i++) {
716 winbits |= cl[i].enable;
717 winbits &= ~cl[i].disable;
718 if (i < 15 && cl[i].position == cl[i+1].position)
720 saa_writeb(reg + 0, winbits);
721 saa_writeb(reg + 2, cl[i].position & 0xff);
722 saa_writeb(reg + 3, cl[i].position >> 8);
723 dprintk("clip: %s winbits=%02x pos=%d\n",
724 name,winbits,cl[i].position);
727 for (; reg < 0x400; reg += 8) {
728 saa_writeb(reg+ 0, 0);
729 saa_writeb(reg + 1, 0);
730 saa_writeb(reg + 2, 0);
731 saa_writeb(reg + 3, 0);
735 static int clip_range(int val)
742 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
743 int nclips, int interlace)
745 struct cliplist col[16], row[16];
747 int div = interlace ? 2 : 1;
749 memset(col,0,sizeof(col)); cols = 0;
750 memset(row,0,sizeof(row)); rows = 0;
751 for (i = 0; i < nclips && i < 8; i++) {
752 col[cols].position = clip_range(clips[i].c.left);
753 col[cols].enable = (1 << i);
755 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
756 col[cols].disable = (1 << i);
758 row[rows].position = clip_range(clips[i].c.top / div);
759 row[rows].enable = (1 << i);
761 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
763 row[rows].disable = (1 << i);
766 sort_cliplist(col,cols);
767 sort_cliplist(row,rows);
768 set_cliplist(dev,0x380,col,cols,"cols");
769 set_cliplist(dev,0x384,row,rows,"rows");
773 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win)
775 enum v4l2_field field;
778 if (NULL == dev->ovbuf.base)
780 if (NULL == dev->ovfmt)
782 if (win->w.width < 48 || win->w.height < 32)
784 if (win->clipcount > 2048)
788 maxw = dev->crop_current.width;
789 maxh = dev->crop_current.height;
791 if (V4L2_FIELD_ANY == field) {
792 field = (win->w.height > maxh/2)
793 ? V4L2_FIELD_INTERLACED
798 case V4L2_FIELD_BOTTOM:
801 case V4L2_FIELD_INTERLACED:
808 if (win->w.width > maxw)
810 if (win->w.height > maxh)
811 win->w.height = maxh;
815 static int start_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
817 unsigned long base,control,bpl;
820 err = verify_preview(dev,&fh->win);
824 dev->ovfield = fh->win.field;
825 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
826 fh->win.w.width,fh->win.w.height,
827 fh->win.w.left,fh->win.w.top,
828 dev->ovfmt->name,v4l2_field_names[dev->ovfield]);
830 /* setup window + clipping */
831 set_size(dev,TASK_B,fh->win.w.width,fh->win.w.height,
832 V4L2_FIELD_HAS_BOTH(dev->ovfield));
833 setup_clipping(dev,fh->clips,fh->nclips,
834 V4L2_FIELD_HAS_BOTH(dev->ovfield));
836 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
838 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
839 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
841 /* dma: setup channel 1 (= Video Task B) */
842 base = (unsigned long)dev->ovbuf.base;
843 base += dev->ovbuf.fmt.bytesperline * fh->win.w.top;
844 base += dev->ovfmt->depth/8 * fh->win.w.left;
845 bpl = dev->ovbuf.fmt.bytesperline;
846 control = SAA7134_RS_CONTROL_BURST_16;
847 if (dev->ovfmt->bswap)
848 control |= SAA7134_RS_CONTROL_BSWAP;
849 if (dev->ovfmt->wswap)
850 control |= SAA7134_RS_CONTROL_WSWAP;
851 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
852 saa_writel(SAA7134_RS_BA1(1),base);
853 saa_writel(SAA7134_RS_BA2(1),base+bpl);
854 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
855 saa_writel(SAA7134_RS_CONTROL(1),control);
857 saa_writel(SAA7134_RS_BA1(1),base);
858 saa_writel(SAA7134_RS_BA2(1),base);
859 saa_writel(SAA7134_RS_PITCH(1),bpl);
860 saa_writel(SAA7134_RS_CONTROL(1),control);
865 saa7134_set_dmabits(dev);
870 static int stop_preview(struct saa7134_dev *dev, struct saa7134_fh *fh)
873 saa7134_set_dmabits(dev);
877 /* ------------------------------------------------------------------ */
879 static int buffer_activate(struct saa7134_dev *dev,
880 struct saa7134_buf *buf,
881 struct saa7134_buf *next)
883 unsigned long base,control,bpl;
884 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
886 dprintk("buffer_activate buf=%p\n",buf);
887 buf->vb.state = STATE_ACTIVE;
890 set_size(dev,TASK_A,buf->vb.width,buf->vb.height,
891 V4L2_FIELD_HAS_BOTH(buf->vb.field));
893 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
895 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
896 saa_writeb(SAA7134_OFMT_VIDEO_A, buf->fmt->pm);
898 /* DMA: setup channel 0 (= Video Task A0) */
899 base = saa7134_buffer_base(buf);
900 if (buf->fmt->planar)
903 bpl = (buf->vb.width * buf->fmt->depth) / 8;
904 control = SAA7134_RS_CONTROL_BURST_16 |
905 SAA7134_RS_CONTROL_ME |
906 (buf->pt->dma >> 12);
908 control |= SAA7134_RS_CONTROL_BSWAP;
910 control |= SAA7134_RS_CONTROL_WSWAP;
911 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
913 saa_writel(SAA7134_RS_BA1(0),base);
914 saa_writel(SAA7134_RS_BA2(0),base+bpl);
915 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
918 saa_writel(SAA7134_RS_BA1(0),base);
919 saa_writel(SAA7134_RS_BA2(0),base);
920 saa_writel(SAA7134_RS_PITCH(0),bpl);
922 saa_writel(SAA7134_RS_CONTROL(0),control);
924 if (buf->fmt->planar) {
925 /* DMA: setup channel 4+5 (= planar task A) */
926 bpl_uv = bpl >> buf->fmt->hshift;
927 lines_uv = buf->vb.height >> buf->fmt->vshift;
928 base2 = base + bpl * buf->vb.height;
929 base3 = base2 + bpl_uv * lines_uv;
930 if (buf->fmt->uvswap)
931 tmp = base2, base2 = base3, base3 = tmp;
932 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
933 bpl_uv,lines_uv,base2,base3);
934 if (V4L2_FIELD_HAS_BOTH(buf->vb.field)) {
936 saa_writel(SAA7134_RS_BA1(4),base2);
937 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
938 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
939 saa_writel(SAA7134_RS_BA1(5),base3);
940 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
941 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
944 saa_writel(SAA7134_RS_BA1(4),base2);
945 saa_writel(SAA7134_RS_BA2(4),base2);
946 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
947 saa_writel(SAA7134_RS_BA1(5),base3);
948 saa_writel(SAA7134_RS_BA2(5),base3);
949 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
951 saa_writel(SAA7134_RS_CONTROL(4),control);
952 saa_writel(SAA7134_RS_CONTROL(5),control);
956 saa7134_set_dmabits(dev);
957 mod_timer(&dev->video_q.timeout, jiffies+BUFFER_TIMEOUT);
961 static int buffer_prepare(struct videobuf_queue *q,
962 struct videobuf_buffer *vb,
963 enum v4l2_field field)
965 struct saa7134_fh *fh = q->priv_data;
966 struct saa7134_dev *dev = fh->dev;
967 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
974 if (fh->width < 48 ||
976 fh->width/4 > dev->crop_current.width ||
977 fh->height/4 > dev->crop_current.height ||
978 fh->width > dev->crop_bounds.width ||
979 fh->height > dev->crop_bounds.height)
981 size = (fh->width * fh->height * fh->fmt->depth) >> 3;
982 if (0 != buf->vb.baddr && buf->vb.bsize < size)
985 dprintk("buffer_prepare [%d,size=%dx%d,bytes=%d,fields=%s,%s]\n",
986 vb->i,fh->width,fh->height,size,v4l2_field_names[field],
988 if (buf->vb.width != fh->width ||
989 buf->vb.height != fh->height ||
990 buf->vb.size != size ||
991 buf->vb.field != field ||
992 buf->fmt != fh->fmt) {
993 saa7134_dma_free(dev,buf);
996 if (STATE_NEEDS_INIT == buf->vb.state) {
997 buf->vb.width = fh->width;
998 buf->vb.height = fh->height;
1000 buf->vb.field = field;
1002 buf->pt = &fh->pt_cap;
1004 err = videobuf_iolock(dev->pci,&buf->vb,&dev->ovbuf);
1007 err = saa7134_pgtable_build(dev->pci,buf->pt,
1010 saa7134_buffer_startpage(buf));
1014 buf->vb.state = STATE_PREPARED;
1015 buf->activate = buffer_activate;
1019 saa7134_dma_free(dev,buf);
1024 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
1026 struct saa7134_fh *fh = q->priv_data;
1028 *size = fh->fmt->depth * fh->width * fh->height >> 3;
1031 *count = saa7134_buffer_count(*size,*count);
1035 static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
1037 struct saa7134_fh *fh = q->priv_data;
1038 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1040 saa7134_buffer_queue(fh->dev,&fh->dev->video_q,buf);
1043 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
1045 struct saa7134_fh *fh = q->priv_data;
1046 struct saa7134_buf *buf = container_of(vb,struct saa7134_buf,vb);
1048 saa7134_dma_free(fh->dev,buf);
1051 static struct videobuf_queue_ops video_qops = {
1052 .buf_setup = buffer_setup,
1053 .buf_prepare = buffer_prepare,
1054 .buf_queue = buffer_queue,
1055 .buf_release = buffer_release,
1058 /* ------------------------------------------------------------------ */
1060 static int get_control(struct saa7134_dev *dev, struct v4l2_control *c)
1062 const struct v4l2_queryctrl* ctrl;
1064 ctrl = ctrl_by_id(c->id);
1068 case V4L2_CID_BRIGHTNESS:
1069 c->value = dev->ctl_bright;
1072 c->value = dev->ctl_hue;
1074 case V4L2_CID_CONTRAST:
1075 c->value = dev->ctl_contrast;
1077 case V4L2_CID_SATURATION:
1078 c->value = dev->ctl_saturation;
1080 case V4L2_CID_AUDIO_MUTE:
1081 c->value = dev->ctl_mute;
1083 case V4L2_CID_AUDIO_VOLUME:
1084 c->value = dev->ctl_volume;
1086 case V4L2_CID_PRIVATE_INVERT:
1087 c->value = dev->ctl_invert;
1089 case V4L2_CID_HFLIP:
1090 c->value = dev->ctl_mirror;
1092 case V4L2_CID_PRIVATE_Y_EVEN:
1093 c->value = dev->ctl_y_even;
1095 case V4L2_CID_PRIVATE_Y_ODD:
1096 c->value = dev->ctl_y_odd;
1098 case V4L2_CID_PRIVATE_AUTOMUTE:
1099 c->value = dev->ctl_automute;
1107 static int set_control(struct saa7134_dev *dev, struct saa7134_fh *fh,
1108 struct v4l2_control *c)
1110 const struct v4l2_queryctrl* ctrl;
1111 unsigned long flags;
1112 int restart_overlay = 0;
1114 ctrl = ctrl_by_id(c->id);
1117 dprintk("set_control name=%s val=%d\n",ctrl->name,c->value);
1118 switch (ctrl->type) {
1119 case V4L2_CTRL_TYPE_BOOLEAN:
1120 case V4L2_CTRL_TYPE_MENU:
1121 case V4L2_CTRL_TYPE_INTEGER:
1122 if (c->value < ctrl->minimum)
1123 c->value = ctrl->minimum;
1124 if (c->value > ctrl->maximum)
1125 c->value = ctrl->maximum;
1131 case V4L2_CID_BRIGHTNESS:
1132 dev->ctl_bright = c->value;
1133 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
1136 dev->ctl_hue = c->value;
1137 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
1139 case V4L2_CID_CONTRAST:
1140 dev->ctl_contrast = c->value;
1141 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1142 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1144 case V4L2_CID_SATURATION:
1145 dev->ctl_saturation = c->value;
1146 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1147 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1149 case V4L2_CID_AUDIO_MUTE:
1150 dev->ctl_mute = c->value;
1151 saa7134_tvaudio_setmute(dev);
1153 case V4L2_CID_AUDIO_VOLUME:
1154 dev->ctl_volume = c->value;
1155 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1157 case V4L2_CID_PRIVATE_INVERT:
1158 dev->ctl_invert = c->value;
1159 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1160 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1161 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1162 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1164 case V4L2_CID_HFLIP:
1165 dev->ctl_mirror = c->value;
1166 restart_overlay = 1;
1168 case V4L2_CID_PRIVATE_Y_EVEN:
1169 dev->ctl_y_even = c->value;
1170 restart_overlay = 1;
1172 case V4L2_CID_PRIVATE_Y_ODD:
1173 dev->ctl_y_odd = c->value;
1174 restart_overlay = 1;
1176 case V4L2_CID_PRIVATE_AUTOMUTE:
1177 dev->ctl_automute = c->value;
1178 if (dev->tda9887_conf) {
1179 if (dev->ctl_automute)
1180 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1182 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1183 saa7134_i2c_call_clients(dev, TDA9887_SET_CONFIG,
1184 &dev->tda9887_conf);
1190 if (restart_overlay && fh && res_check(fh, RESOURCE_OVERLAY)) {
1191 spin_lock_irqsave(&dev->slock,flags);
1192 stop_preview(dev,fh);
1193 start_preview(dev,fh);
1194 spin_unlock_irqrestore(&dev->slock,flags);
1199 /* ------------------------------------------------------------------ */
1201 static struct videobuf_queue* saa7134_queue(struct saa7134_fh *fh)
1203 struct videobuf_queue* q = NULL;
1206 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1209 case V4L2_BUF_TYPE_VBI_CAPTURE:
1218 static int saa7134_resource(struct saa7134_fh *fh)
1223 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1224 res = RESOURCE_VIDEO;
1226 case V4L2_BUF_TYPE_VBI_CAPTURE:
1235 static int video_open(struct inode *inode, struct file *file)
1237 int minor = iminor(inode);
1238 struct saa7134_dev *h,*dev = NULL;
1239 struct saa7134_fh *fh;
1240 struct list_head *list;
1241 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1243 list_for_each(list,&saa7134_devlist) {
1244 h = list_entry(list, struct saa7134_dev, devlist);
1245 if (h->video_dev && (h->video_dev->minor == minor))
1247 if (h->radio_dev && (h->radio_dev->minor == minor)) {
1251 if (h->vbi_dev && (h->vbi_dev->minor == minor)) {
1252 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1259 dprintk("open minor=%d radio=%d type=%s\n",minor,radio,
1260 v4l2_type_names[type]);
1262 /* allocate + initialize per filehandle data */
1263 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
1266 memset(fh,0,sizeof(*fh));
1267 file->private_data = fh;
1271 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1274 v4l2_prio_open(&dev->prio,&fh->prio);
1276 videobuf_queue_init(&fh->cap, &video_qops,
1277 dev->pci, &dev->slock,
1278 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1279 V4L2_FIELD_INTERLACED,
1280 sizeof(struct saa7134_buf),
1282 videobuf_queue_init(&fh->vbi, &saa7134_vbi_qops,
1283 dev->pci, &dev->slock,
1284 V4L2_BUF_TYPE_VBI_CAPTURE,
1286 sizeof(struct saa7134_buf),
1288 saa7134_pgtable_alloc(dev->pci,&fh->pt_cap);
1289 saa7134_pgtable_alloc(dev->pci,&fh->pt_vbi);
1292 /* switch to radio mode */
1293 saa7134_tvaudio_setinput(dev,&card(dev).radio);
1294 saa7134_i2c_call_clients(dev,AUDC_SET_RADIO, NULL);
1296 /* switch to video/vbi mode */
1297 video_mux(dev,dev->ctl_input);
1303 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
1305 struct saa7134_fh *fh = file->private_data;
1308 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1309 if (res_locked(fh->dev,RESOURCE_VIDEO))
1311 return videobuf_read_one(saa7134_queue(fh),
1313 file->f_flags & O_NONBLOCK);
1314 case V4L2_BUF_TYPE_VBI_CAPTURE:
1315 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1317 return videobuf_read_stream(saa7134_queue(fh),
1318 data, count, ppos, 1,
1319 file->f_flags & O_NONBLOCK);
1328 video_poll(struct file *file, struct poll_table_struct *wait)
1330 struct saa7134_fh *fh = file->private_data;
1331 struct videobuf_buffer *buf = NULL;
1333 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1334 return videobuf_poll_stream(file, &fh->vbi, wait);
1336 if (res_check(fh,RESOURCE_VIDEO)) {
1337 if (!list_empty(&fh->cap.stream))
1338 buf = list_entry(fh->cap.stream.next, struct videobuf_buffer, stream);
1340 down(&fh->cap.lock);
1341 if (UNSET == fh->cap.read_off) {
1342 /* need to capture a new frame */
1343 if (res_locked(fh->dev,RESOURCE_VIDEO)) {
1347 if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,fh->cap.field)) {
1351 fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
1352 fh->cap.read_off = 0;
1355 buf = fh->cap.read_buf;
1361 poll_wait(file, &buf->done, wait);
1362 if (buf->state == STATE_DONE ||
1363 buf->state == STATE_ERROR)
1364 return POLLIN|POLLRDNORM;
1368 static int video_release(struct inode *inode, struct file *file)
1370 struct saa7134_fh *fh = file->private_data;
1371 struct saa7134_dev *dev = fh->dev;
1372 unsigned long flags;
1374 /* turn off overlay */
1375 if (res_check(fh, RESOURCE_OVERLAY)) {
1376 spin_lock_irqsave(&dev->slock,flags);
1377 stop_preview(dev,fh);
1378 spin_unlock_irqrestore(&dev->slock,flags);
1379 res_free(dev,fh,RESOURCE_OVERLAY);
1382 /* stop video capture */
1383 if (res_check(fh, RESOURCE_VIDEO)) {
1384 videobuf_streamoff(&fh->cap);
1385 res_free(dev,fh,RESOURCE_VIDEO);
1387 if (fh->cap.read_buf) {
1388 buffer_release(&fh->cap,fh->cap.read_buf);
1389 kfree(fh->cap.read_buf);
1392 /* stop vbi capture */
1393 if (res_check(fh, RESOURCE_VBI)) {
1394 if (fh->vbi.streaming)
1395 videobuf_streamoff(&fh->vbi);
1396 if (fh->vbi.reading)
1397 videobuf_read_stop(&fh->vbi);
1398 res_free(dev,fh,RESOURCE_VBI);
1401 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1402 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1403 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1404 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1405 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1407 saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1410 videobuf_mmap_free(&fh->cap);
1411 videobuf_mmap_free(&fh->vbi);
1412 saa7134_pgtable_free(dev->pci,&fh->pt_cap);
1413 saa7134_pgtable_free(dev->pci,&fh->pt_vbi);
1415 v4l2_prio_close(&dev->prio,&fh->prio);
1416 file->private_data = NULL;
1422 video_mmap(struct file *file, struct vm_area_struct * vma)
1424 struct saa7134_fh *fh = file->private_data;
1426 return videobuf_mmap_mapper(saa7134_queue(fh), vma);
1429 /* ------------------------------------------------------------------ */
1431 static void saa7134_vbi_fmt(struct saa7134_dev *dev, struct v4l2_format *f)
1433 struct saa7134_tvnorm *norm = dev->tvnorm;
1435 f->fmt.vbi.sampling_rate = 6750000 * 4;
1436 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1437 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1438 f->fmt.vbi.offset = 64 * 4;
1439 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1440 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1441 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1442 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1443 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1447 static int saa7134_g_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1448 struct v4l2_format *f)
1451 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1452 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1453 f->fmt.pix.width = fh->width;
1454 f->fmt.pix.height = fh->height;
1455 f->fmt.pix.field = fh->cap.field;
1456 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1457 f->fmt.pix.bytesperline =
1458 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1459 f->fmt.pix.sizeimage =
1460 f->fmt.pix.height * f->fmt.pix.bytesperline;
1462 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1463 f->fmt.win = fh->win;
1465 case V4L2_BUF_TYPE_VBI_CAPTURE:
1466 saa7134_vbi_fmt(dev,f);
1473 static int saa7134_try_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1474 struct v4l2_format *f)
1479 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1481 struct saa7134_format *fmt;
1482 enum v4l2_field field;
1483 unsigned int maxw, maxh;
1485 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1489 field = f->fmt.pix.field;
1490 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1491 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1493 if (V4L2_FIELD_ANY == field) {
1494 field = (f->fmt.pix.height > maxh/2)
1495 ? V4L2_FIELD_INTERLACED
1496 : V4L2_FIELD_BOTTOM;
1499 case V4L2_FIELD_TOP:
1500 case V4L2_FIELD_BOTTOM:
1503 case V4L2_FIELD_INTERLACED:
1509 f->fmt.pix.field = field;
1510 if (f->fmt.pix.width < 48)
1511 f->fmt.pix.width = 48;
1512 if (f->fmt.pix.height < 32)
1513 f->fmt.pix.height = 32;
1514 if (f->fmt.pix.width > maxw)
1515 f->fmt.pix.width = maxw;
1516 if (f->fmt.pix.height > maxh)
1517 f->fmt.pix.height = maxh;
1518 f->fmt.pix.width &= ~0x03;
1519 f->fmt.pix.bytesperline =
1520 (f->fmt.pix.width * fmt->depth) >> 3;
1521 f->fmt.pix.sizeimage =
1522 f->fmt.pix.height * f->fmt.pix.bytesperline;
1526 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1527 err = verify_preview(dev,&f->fmt.win);
1531 case V4L2_BUF_TYPE_VBI_CAPTURE:
1532 saa7134_vbi_fmt(dev,f);
1539 static int saa7134_s_fmt(struct saa7134_dev *dev, struct saa7134_fh *fh,
1540 struct v4l2_format *f)
1542 unsigned long flags;
1546 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1547 err = saa7134_try_fmt(dev,fh,f);
1551 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1552 fh->width = f->fmt.pix.width;
1553 fh->height = f->fmt.pix.height;
1554 fh->cap.field = f->fmt.pix.field;
1556 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1557 err = verify_preview(dev,&f->fmt.win);
1562 fh->win = f->fmt.win;
1563 fh->nclips = f->fmt.win.clipcount;
1566 if (copy_from_user(fh->clips,f->fmt.win.clips,
1567 sizeof(struct v4l2_clip)*fh->nclips)) {
1572 if (res_check(fh, RESOURCE_OVERLAY)) {
1573 spin_lock_irqsave(&dev->slock,flags);
1574 stop_preview(dev,fh);
1575 start_preview(dev,fh);
1576 spin_unlock_irqrestore(&dev->slock,flags);
1580 case V4L2_BUF_TYPE_VBI_CAPTURE:
1581 saa7134_vbi_fmt(dev,f);
1588 int saa7134_common_ioctl(struct saa7134_dev *dev,
1589 unsigned int cmd, void *arg)
1594 case VIDIOC_QUERYCTRL:
1596 const struct v4l2_queryctrl *ctrl;
1597 struct v4l2_queryctrl *c = arg;
1599 if ((c->id < V4L2_CID_BASE ||
1600 c->id >= V4L2_CID_LASTP1) &&
1601 (c->id < V4L2_CID_PRIVATE_BASE ||
1602 c->id >= V4L2_CID_PRIVATE_LASTP1))
1604 ctrl = ctrl_by_id(c->id);
1605 *c = (NULL != ctrl) ? *ctrl : no_ctrl;
1609 return get_control(dev,arg);
1613 err = set_control(dev,NULL,arg);
1617 /* --- input switching --------------------------------------- */
1618 case VIDIOC_ENUMINPUT:
1620 struct v4l2_input *i = arg;
1624 if (n >= SAA7134_INPUT_MAX)
1626 if (NULL == card_in(dev,i->index).name)
1628 memset(i,0,sizeof(*i));
1630 i->type = V4L2_INPUT_TYPE_CAMERA;
1631 strcpy(i->name,card_in(dev,n).name);
1632 if (card_in(dev,n).tv)
1633 i->type = V4L2_INPUT_TYPE_TUNER;
1635 if (n == dev->ctl_input) {
1636 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1637 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1639 if (0 != (v1 & 0x40))
1640 i->status |= V4L2_IN_ST_NO_H_LOCK;
1641 if (0 != (v2 & 0x40))
1642 i->status |= V4L2_IN_ST_NO_SYNC;
1643 if (0 != (v2 & 0x0e))
1644 i->status |= V4L2_IN_ST_MACROVISION;
1646 for (n = 0; n < TVNORMS; n++)
1647 i->std |= tvnorms[n].id;
1650 case VIDIOC_G_INPUT:
1653 *i = dev->ctl_input;
1656 case VIDIOC_S_INPUT:
1660 if (*i < 0 || *i >= SAA7134_INPUT_MAX)
1662 if (NULL == card_in(dev,*i).name)
1673 EXPORT_SYMBOL(saa7134_common_ioctl);
1676 * This function is _not_ called directly, but from
1677 * video_generic_ioctl (and maybe others). userspace
1678 * copying is done already, arg is a kernel pointer.
1680 static int video_do_ioctl(struct inode *inode, struct file *file,
1681 unsigned int cmd, void *arg)
1683 struct saa7134_fh *fh = file->private_data;
1684 struct saa7134_dev *dev = fh->dev;
1685 unsigned long flags;
1688 if (video_debug > 1)
1689 saa7134_print_ioctl(dev->name,cmd);
1694 case VIDIOC_S_INPUT:
1695 case VIDIOC_S_TUNER:
1696 case VIDIOC_S_FREQUENCY:
1697 err = v4l2_prio_check(&dev->prio,&fh->prio);
1703 case VIDIOC_QUERYCAP:
1705 struct v4l2_capability *cap = arg;
1706 unsigned int tuner_type = dev->tuner_type;
1708 memset(cap,0,sizeof(*cap));
1709 strcpy(cap->driver, "saa7134");
1710 strlcpy(cap->card, saa7134_boards[dev->board].name,
1712 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1713 cap->version = SAA7134_VERSION_CODE;
1715 V4L2_CAP_VIDEO_CAPTURE |
1716 V4L2_CAP_VIDEO_OVERLAY |
1717 V4L2_CAP_VBI_CAPTURE |
1718 V4L2_CAP_READWRITE |
1719 V4L2_CAP_STREAMING |
1722 if ((tuner_type == TUNER_ABSENT) || (tuner_type == UNSET))
1723 cap->capabilities &= ~V4L2_CAP_TUNER;
1728 /* --- tv standards ------------------------------------------ */
1729 case VIDIOC_ENUMSTD:
1731 struct v4l2_standard *e = arg;
1737 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1738 tvnorms[e->index].name);
1746 v4l2_std_id *id = arg;
1748 *id = dev->tvnorm->id;
1753 v4l2_std_id *id = arg;
1756 for (i = 0; i < TVNORMS; i++)
1757 if (*id == tvnorms[i].id)
1760 for (i = 0; i < TVNORMS; i++)
1761 if (*id & tvnorms[i].id)
1767 if (res_check(fh, RESOURCE_OVERLAY)) {
1768 spin_lock_irqsave(&dev->slock,flags);
1769 stop_preview(dev,fh);
1770 set_tvnorm(dev,&tvnorms[i]);
1771 start_preview(dev,fh);
1772 spin_unlock_irqrestore(&dev->slock,flags);
1774 set_tvnorm(dev,&tvnorms[i]);
1775 saa7134_tvaudio_do_scan(dev);
1780 case VIDIOC_CROPCAP:
1782 struct v4l2_cropcap *cap = arg;
1784 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1785 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1787 cap->bounds = dev->crop_bounds;
1788 cap->defrect = dev->crop_defrect;
1789 cap->pixelaspect.numerator = 1;
1790 cap->pixelaspect.denominator = 1;
1791 if (dev->tvnorm->id & V4L2_STD_525_60) {
1792 cap->pixelaspect.numerator = 11;
1793 cap->pixelaspect.denominator = 10;
1795 if (dev->tvnorm->id & V4L2_STD_625_50) {
1796 cap->pixelaspect.numerator = 54;
1797 cap->pixelaspect.denominator = 59;
1804 struct v4l2_crop * crop = arg;
1806 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1807 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1809 crop->c = dev->crop_current;
1814 struct v4l2_crop *crop = arg;
1815 struct v4l2_rect *b = &dev->crop_bounds;
1817 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1818 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1820 if (crop->c.height < 0)
1822 if (crop->c.width < 0)
1825 if (res_locked(fh->dev,RESOURCE_OVERLAY))
1827 if (res_locked(fh->dev,RESOURCE_VIDEO))
1830 if (crop->c.top < b->top)
1831 crop->c.top = b->top;
1832 if (crop->c.top > b->top + b->height)
1833 crop->c.top = b->top + b->height;
1834 if (crop->c.height > b->top - crop->c.top + b->height)
1835 crop->c.height = b->top - crop->c.top + b->height;
1837 if (crop->c.left < b->left)
1838 crop->c.left = b->left;
1839 if (crop->c.left > b->left + b->width)
1840 crop->c.left = b->left + b->width;
1841 if (crop->c.width > b->left - crop->c.left + b->width)
1842 crop->c.width = b->left - crop->c.left + b->width;
1844 dev->crop_current = crop->c;
1848 /* --- tuner ioctls ------------------------------------------ */
1849 case VIDIOC_G_TUNER:
1851 struct v4l2_tuner *t = arg;
1856 memset(t,0,sizeof(*t));
1857 for (n = 0; n < SAA7134_INPUT_MAX; n++)
1858 if (card_in(dev,n).tv)
1860 if (NULL != card_in(dev,n).name) {
1861 strcpy(t->name, "Television");
1862 t->capability = V4L2_TUNER_CAP_NORM |
1863 V4L2_TUNER_CAP_STEREO |
1864 V4L2_TUNER_CAP_LANG1 |
1865 V4L2_TUNER_CAP_LANG2;
1866 t->rangehigh = 0xffffffffUL;
1867 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1868 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1870 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1874 case VIDIOC_S_TUNER:
1876 struct v4l2_tuner *t = arg;
1879 mode = dev->thread.mode;
1880 if (UNSET == mode) {
1881 rx = saa7134_tvaudio_getstereo(dev);
1882 mode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1884 if (mode != t->audmode) {
1885 dev->thread.mode = t->audmode;
1889 case VIDIOC_G_FREQUENCY:
1891 struct v4l2_frequency *f = arg;
1893 memset(f,0,sizeof(*f));
1894 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1895 f->frequency = dev->ctl_freq;
1898 case VIDIOC_S_FREQUENCY:
1900 struct v4l2_frequency *f = arg;
1904 if (0 == fh->radio && V4L2_TUNER_ANALOG_TV != f->type)
1906 if (1 == fh->radio && V4L2_TUNER_RADIO != f->type)
1909 dev->ctl_freq = f->frequency;
1911 saa7134_i2c_call_clients(dev,VIDIOC_S_FREQUENCY,f);
1913 saa7134_tvaudio_do_scan(dev);
1918 /* --- control ioctls ---------------------------------------- */
1919 case VIDIOC_ENUMINPUT:
1920 case VIDIOC_G_INPUT:
1921 case VIDIOC_S_INPUT:
1922 case VIDIOC_QUERYCTRL:
1925 return saa7134_common_ioctl(dev, cmd, arg);
1927 case VIDIOC_G_AUDIO:
1929 struct v4l2_audio *a = arg;
1931 memset(a,0,sizeof(*a));
1932 strcpy(a->name,"audio");
1935 case VIDIOC_S_AUDIO:
1939 struct v4l2_captureparm *parm = arg;
1940 memset(parm,0,sizeof(*parm));
1944 case VIDIOC_G_PRIORITY:
1946 enum v4l2_priority *p = arg;
1948 *p = v4l2_prio_max(&dev->prio);
1951 case VIDIOC_S_PRIORITY:
1953 enum v4l2_priority *prio = arg;
1955 return v4l2_prio_change(&dev->prio, &fh->prio, *prio);
1958 /* --- preview ioctls ---------------------------------------- */
1959 case VIDIOC_ENUM_FMT:
1961 struct v4l2_fmtdesc *f = arg;
1962 enum v4l2_buf_type type;
1968 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1969 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
1970 if (index >= FORMATS)
1972 if (f->type == V4L2_BUF_TYPE_VIDEO_OVERLAY &&
1973 formats[index].planar)
1975 memset(f,0,sizeof(*f));
1978 strlcpy(f->description,formats[index].name,sizeof(f->description));
1979 f->pixelformat = formats[index].fourcc;
1981 case V4L2_BUF_TYPE_VBI_CAPTURE:
1984 memset(f,0,sizeof(*f));
1987 f->pixelformat = V4L2_PIX_FMT_GREY;
1988 strcpy(f->description,"vbi data");
1997 struct v4l2_framebuffer *fb = arg;
2000 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
2005 struct v4l2_framebuffer *fb = arg;
2006 struct saa7134_format *fmt;
2008 if(!capable(CAP_SYS_ADMIN) &&
2009 !capable(CAP_SYS_RAWIO))
2013 fmt = format_by_fourcc(fb->fmt.pixelformat);
2020 if (0 == dev->ovbuf.fmt.bytesperline)
2021 dev->ovbuf.fmt.bytesperline =
2022 dev->ovbuf.fmt.width*fmt->depth/8;
2025 case VIDIOC_OVERLAY:
2030 if (!res_get(dev,fh,RESOURCE_OVERLAY))
2032 spin_lock_irqsave(&dev->slock,flags);
2033 start_preview(dev,fh);
2034 spin_unlock_irqrestore(&dev->slock,flags);
2037 if (!res_check(fh, RESOURCE_OVERLAY))
2039 spin_lock_irqsave(&dev->slock,flags);
2040 stop_preview(dev,fh);
2041 spin_unlock_irqrestore(&dev->slock,flags);
2042 res_free(dev,fh,RESOURCE_OVERLAY);
2047 /* --- capture ioctls ---------------------------------------- */
2050 struct v4l2_format *f = arg;
2051 return saa7134_g_fmt(dev,fh,f);
2055 struct v4l2_format *f = arg;
2056 return saa7134_s_fmt(dev,fh,f);
2058 case VIDIOC_TRY_FMT:
2060 struct v4l2_format *f = arg;
2061 return saa7134_try_fmt(dev,fh,f);
2066 struct video_mbuf *mbuf = arg;
2067 struct videobuf_queue *q;
2068 struct v4l2_requestbuffers req;
2071 q = saa7134_queue(fh);
2072 memset(&req,0,sizeof(req));
2074 req.count = gbuffers;
2075 req.memory = V4L2_MEMORY_MMAP;
2076 err = videobuf_reqbufs(q,&req);
2079 memset(mbuf,0,sizeof(*mbuf));
2080 mbuf->frames = req.count;
2082 for (i = 0; i < mbuf->frames; i++) {
2083 mbuf->offsets[i] = q->bufs[i]->boff;
2084 mbuf->size += q->bufs[i]->bsize;
2088 case VIDIOC_REQBUFS:
2089 return videobuf_reqbufs(saa7134_queue(fh),arg);
2091 case VIDIOC_QUERYBUF:
2092 return videobuf_querybuf(saa7134_queue(fh),arg);
2095 return videobuf_qbuf(saa7134_queue(fh),arg);
2098 return videobuf_dqbuf(saa7134_queue(fh),arg,
2099 file->f_flags & O_NONBLOCK);
2101 case VIDIOC_STREAMON:
2103 int res = saa7134_resource(fh);
2105 if (!res_get(dev,fh,res))
2107 return videobuf_streamon(saa7134_queue(fh));
2109 case VIDIOC_STREAMOFF:
2111 int res = saa7134_resource(fh);
2113 err = videobuf_streamoff(saa7134_queue(fh));
2116 res_free(dev,fh,res);
2121 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2127 static int video_ioctl(struct inode *inode, struct file *file,
2128 unsigned int cmd, unsigned long arg)
2130 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
2133 static int radio_do_ioctl(struct inode *inode, struct file *file,
2134 unsigned int cmd, void *arg)
2136 struct saa7134_fh *fh = file->private_data;
2137 struct saa7134_dev *dev = fh->dev;
2139 if (video_debug > 1)
2140 saa7134_print_ioctl(dev->name,cmd);
2142 case VIDIOC_QUERYCAP:
2144 struct v4l2_capability *cap = arg;
2146 memset(cap,0,sizeof(*cap));
2147 strcpy(cap->driver, "saa7134");
2148 strlcpy(cap->card, saa7134_boards[dev->board].name,
2150 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
2151 cap->version = SAA7134_VERSION_CODE;
2152 cap->capabilities = V4L2_CAP_TUNER;
2155 case VIDIOC_G_TUNER:
2157 struct v4l2_tuner *t = arg;
2162 memset(t,0,sizeof(*t));
2163 strcpy(t->name, "Radio");
2165 saa7134_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
2169 case VIDIOC_S_TUNER:
2171 struct v4l2_tuner *t = arg;
2176 saa7134_i2c_call_clients(dev,VIDIOC_S_TUNER,t);
2180 case VIDIOC_ENUMINPUT:
2182 struct v4l2_input *i = arg;
2186 strcpy(i->name,"Radio");
2187 i->type = V4L2_INPUT_TYPE_TUNER;
2190 case VIDIOC_G_INPUT:
2196 case VIDIOC_G_AUDIO:
2198 struct v4l2_audio *a = arg;
2200 memset(a,0,sizeof(*a));
2201 strcpy(a->name,"Radio");
2206 v4l2_std_id *id = arg;
2210 case VIDIOC_S_AUDIO:
2211 case VIDIOC_S_INPUT:
2215 case VIDIOC_QUERYCTRL:
2217 const struct v4l2_queryctrl *ctrl;
2218 struct v4l2_queryctrl *c = arg;
2220 if (c->id < V4L2_CID_BASE ||
2221 c->id >= V4L2_CID_LASTP1)
2223 if (c->id == V4L2_CID_AUDIO_MUTE) {
2224 ctrl = ctrl_by_id(c->id);
2233 case VIDIOC_G_FREQUENCY:
2234 case VIDIOC_S_FREQUENCY:
2235 return video_do_ioctl(inode,file,cmd,arg);
2238 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
2244 static int radio_ioctl(struct inode *inode, struct file *file,
2245 unsigned int cmd, unsigned long arg)
2247 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
2250 static struct file_operations video_fops =
2252 .owner = THIS_MODULE,
2254 .release = video_release,
2258 .ioctl = video_ioctl,
2259 .llseek = no_llseek,
2262 static struct file_operations radio_fops =
2264 .owner = THIS_MODULE,
2266 .release = video_release,
2267 .ioctl = radio_ioctl,
2268 .llseek = no_llseek,
2271 /* ----------------------------------------------------------- */
2272 /* exported stuff */
2274 struct video_device saa7134_video_template =
2276 .name = "saa7134-video",
2277 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_OVERLAY|
2278 VID_TYPE_CLIPPING|VID_TYPE_SCALES,
2280 .fops = &video_fops,
2284 struct video_device saa7134_vbi_template =
2286 .name = "saa7134-vbi",
2287 .type = VID_TYPE_TUNER|VID_TYPE_TELETEXT,
2289 .fops = &video_fops,
2293 struct video_device saa7134_radio_template =
2295 .name = "saa7134-radio",
2296 .type = VID_TYPE_TUNER,
2298 .fops = &radio_fops,
2302 int saa7134_video_init1(struct saa7134_dev *dev)
2304 /* sanitycheck insmod options */
2305 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2307 if (gbufsize < 0 || gbufsize > gbufsize_max)
2308 gbufsize = gbufsize_max;
2309 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2311 /* put some sensible defaults into the data structures ... */
2312 dev->ctl_bright = ctrl_by_id(V4L2_CID_BRIGHTNESS)->default_value;
2313 dev->ctl_contrast = ctrl_by_id(V4L2_CID_CONTRAST)->default_value;
2314 dev->ctl_hue = ctrl_by_id(V4L2_CID_HUE)->default_value;
2315 dev->ctl_saturation = ctrl_by_id(V4L2_CID_SATURATION)->default_value;
2316 dev->ctl_volume = ctrl_by_id(V4L2_CID_AUDIO_VOLUME)->default_value;
2317 dev->ctl_mute = 1; // ctrl_by_id(V4L2_CID_AUDIO_MUTE)->default_value;
2318 dev->ctl_invert = ctrl_by_id(V4L2_CID_PRIVATE_INVERT)->default_value;
2319 dev->ctl_automute = ctrl_by_id(V4L2_CID_PRIVATE_AUTOMUTE)->default_value;
2321 if (dev->tda9887_conf && dev->ctl_automute)
2322 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2325 INIT_LIST_HEAD(&dev->video_q.queue);
2326 init_timer(&dev->video_q.timeout);
2327 dev->video_q.timeout.function = saa7134_buffer_timeout;
2328 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2329 dev->video_q.dev = dev;
2331 if (saa7134_boards[dev->board].video_out) {
2332 /* enable video output */
2333 int vo = saa7134_boards[dev->board].video_out;
2335 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2336 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2337 video_reg = video_out[vo][1];
2338 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2339 video_reg &= ~VP_T_CODE_P_INVERTED;
2340 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2341 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2342 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2343 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2344 video_reg = video_out[vo][5];
2345 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2346 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2347 if (vid_port_opts & SET_CLOCK_INVERTED)
2348 video_reg |= VP_CLK_CTRL1_INVERTED;
2349 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2350 video_reg = video_out[vo][6];
2351 if (vid_port_opts & SET_VSYNC_OFF) {
2352 video_reg &= ~VP_VS_TYPE_MASK;
2353 video_reg |= VP_VS_TYPE_OFF;
2355 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2356 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2357 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2363 int saa7134_video_init2(struct saa7134_dev *dev)
2366 set_tvnorm(dev,&tvnorms[0]);
2368 saa7134_tvaudio_setmute(dev);
2369 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2373 int saa7134_video_fini(struct saa7134_dev *dev)
2379 void saa7134_irq_video_intl(struct saa7134_dev *dev)
2381 static const char *st[] = {
2382 "(no signal)", "NTSC", "PAL", "SECAM" };
2385 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2386 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2387 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2388 (st1 & 0x40) ? "not locked" : "locked",
2389 (st2 & 0x40) ? "no" : "yes",
2391 dev->nosignal = (st1 & 0x40) || (st2 & 0x40);
2393 if (dev->nosignal) {
2394 /* no video signal -> mute audio */
2395 if (dev->ctl_automute)
2397 saa7134_tvaudio_setmute(dev);
2398 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2400 /* wake up tvaudio audio carrier scan thread */
2401 saa7134_tvaudio_do_scan(dev);
2403 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2405 if (dev->mops && dev->mops->signal_change)
2406 dev->mops->signal_change(dev);
2409 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2411 enum v4l2_field field;
2413 spin_lock(&dev->slock);
2414 if (dev->video_q.curr) {
2415 dev->video_fieldcount++;
2416 field = dev->video_q.curr->vb.field;
2417 if (V4L2_FIELD_HAS_BOTH(field)) {
2418 /* make sure we have seen both fields */
2419 if ((status & 0x10) == 0x00) {
2420 dev->video_q.curr->top_seen = 1;
2423 if (!dev->video_q.curr->top_seen)
2425 } else if (field == V4L2_FIELD_TOP) {
2426 if ((status & 0x10) != 0x10)
2428 } else if (field == V4L2_FIELD_BOTTOM) {
2429 if ((status & 0x10) != 0x00)
2432 dev->video_q.curr->vb.field_count = dev->video_fieldcount;
2433 saa7134_buffer_finish(dev,&dev->video_q,STATE_DONE);
2435 saa7134_buffer_next(dev,&dev->video_q);
2438 spin_unlock(&dev->slock);
2441 /* ----------------------------------------------------------- */