[PATCH] v4l: saa7134 ntsc vbi fix
[linux-2.6.git] / drivers / media / video / saa7134 / saa7134.h
1 /*
2  * $Id: saa7134.h,v 1.38 2005/03/07 12:01:51 kraxel Exp $
3  *
4  * v4l2 device driver for philips saa7134 based TV cards
5  *
6  * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
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  *  GNU General Public License for more details.
17  *
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.
21  */
22
23 #include <linux/version.h>
24 #define SAA7134_VERSION_CODE KERNEL_VERSION(0,2,13)
25
26 #include <linux/pci.h>
27 #include <linux/i2c.h>
28 #include <linux/videodev.h>
29 #include <linux/kdev_t.h>
30 #include <linux/input.h>
31
32 #include <asm/io.h>
33
34 #include <media/tuner.h>
35 #include <media/audiochip.h>
36 #include <media/id.h>
37 #include <media/ir-common.h>
38 #include <media/video-buf.h>
39 #include <media/video-buf-dvb.h>
40
41 #ifndef TRUE
42 # define TRUE (1==1)
43 #endif
44 #ifndef FALSE
45 # define FALSE (1==0)
46 #endif
47 #define UNSET (-1U)
48
49 /* 2.4 / 2.5 driver compatibility stuff */
50
51 /* ----------------------------------------------------------- */
52 /* enums                                                       */
53
54 enum saa7134_tvaudio_mode {
55         TVAUDIO_FM_MONO       = 1,
56         TVAUDIO_FM_BG_STEREO  = 2,
57         TVAUDIO_FM_SAT_STEREO = 3,
58         TVAUDIO_FM_K_STEREO   = 4,
59         TVAUDIO_NICAM_AM      = 5,
60         TVAUDIO_NICAM_FM      = 6,
61 };
62
63 enum saa7134_audio_in {
64         TV    = 1,
65         LINE1 = 2,
66         LINE2 = 3,
67         LINE2_LEFT,
68 };
69
70 enum saa7134_video_out {
71         CCIR656 = 1,
72 };
73
74 /* ----------------------------------------------------------- */
75 /* static data                                                 */
76
77 struct saa7134_tvnorm {
78         char          *name;
79         v4l2_std_id   id;
80
81         /* video decoder */
82         unsigned int  sync_control;
83         unsigned int  luma_control;
84         unsigned int  chroma_ctrl1;
85         unsigned int  chroma_gain;
86         unsigned int  chroma_ctrl2;
87         unsigned int  vgate_misc;
88
89         /* video scaler */
90         unsigned int  h_start;
91         unsigned int  h_stop;
92         unsigned int  video_v_start;
93         unsigned int  video_v_stop;
94         unsigned int  vbi_v_start_0;
95         unsigned int  vbi_v_stop_0;
96         unsigned int  src_timing;
97         unsigned int  vbi_v_start_1;
98 };
99
100 struct saa7134_tvaudio {
101         char         *name;
102         v4l2_std_id  std;
103         enum         saa7134_tvaudio_mode mode;
104         int          carr1;
105         int          carr2;
106 };
107
108 struct saa7134_format {
109         char           *name;
110         unsigned int   fourcc;
111         unsigned int   depth;
112         unsigned int   pm;
113         unsigned int   vshift;   /* vertical downsampling (for planar yuv) */
114         unsigned int   hshift;   /* horizontal downsampling (for planar yuv) */
115         unsigned int   bswap:1;
116         unsigned int   wswap:1;
117         unsigned int   yuv:1;
118         unsigned int   planar:1;
119         unsigned int   uvswap:1;
120 };
121
122 /* ----------------------------------------------------------- */
123 /* card configuration                                          */
124
125 #define SAA7134_BOARD_NOAUTO        UNSET
126 #define SAA7134_BOARD_UNKNOWN           0
127 #define SAA7134_BOARD_PROTEUS_PRO       1
128 #define SAA7134_BOARD_FLYVIDEO3000      2
129 #define SAA7134_BOARD_FLYVIDEO2000      3
130 #define SAA7134_BOARD_EMPRESS           4
131 #define SAA7134_BOARD_MONSTERTV         5
132 #define SAA7134_BOARD_MD9717            6
133 #define SAA7134_BOARD_TVSTATION_RDS     7
134 #define SAA7134_BOARD_CINERGY400        8
135 #define SAA7134_BOARD_MD5044            9
136 #define SAA7134_BOARD_KWORLD           10
137 #define SAA7134_BOARD_CINERGY600       11
138 #define SAA7134_BOARD_MD7134           12
139 #define SAA7134_BOARD_TYPHOON_90031    13
140 #define SAA7134_BOARD_ELSA             14
141 #define SAA7134_BOARD_ELSA_500TV       15
142 #define SAA7134_BOARD_ASUSTeK_TVFM7134 16
143 #define SAA7134_BOARD_VA1000POWER      17
144 #define SAA7134_BOARD_BMK_MPEX_NOTUNER 18
145 #define SAA7134_BOARD_VIDEOMATE_TV     19
146 #define SAA7134_BOARD_CRONOS_PLUS      20
147 #define SAA7134_BOARD_10MOONSTVMASTER  21
148 #define SAA7134_BOARD_MD2819           22
149 #define SAA7134_BOARD_BMK_MPEX_TUNER   23
150 #define SAA7134_BOARD_TVSTATION_DVR    24
151 #define SAA7134_BOARD_ASUSTEK_TVFM7133  25
152 #define SAA7134_BOARD_PINNACLE_PCTV_STEREO 26
153 #define SAA7134_BOARD_MANLI_MTV002     27
154 #define SAA7134_BOARD_MANLI_MTV001     28
155 #define SAA7134_BOARD_TG3000TV         29
156 #define SAA7134_BOARD_ECS_TVP3XP       30
157 #define SAA7134_BOARD_ECS_TVP3XP_4CB5  31
158 #define SAA7134_BOARD_AVACSSMARTTV     32
159 #define SAA7134_BOARD_AVERMEDIA_DVD_EZMAKER 33
160 #define SAA7134_BOARD_NOVAC_PRIMETV7133 34
161 #define SAA7134_BOARD_AVERMEDIA_STUDIO_305 35
162 #define SAA7133_BOARD_UPMOST_PURPLE_TV 36
163 #define SAA7134_BOARD_ITEMS_MTV005     37
164 #define SAA7134_BOARD_CINERGY200       38
165 #define SAA7134_BOARD_FLYTVPLATINUM_MINI 39
166 #define SAA7134_BOARD_VIDEOMATE_TV_PVR 40
167 #define SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUS 41
168 #define SAA7134_BOARD_SABRENT_SBTTVFM  42
169 #define SAA7134_BOARD_ZOLID_XPERT_TV7134 43
170 #define SAA7134_BOARD_EMPIRE_PCI_TV_RADIO_LE 44
171 #define SAA7134_BOARD_AVERMEDIA_307    45
172 #define SAA7134_BOARD_AVERMEDIA_CARDBUS 46
173 #define SAA7134_BOARD_CINERGY400_CARDBUS 47
174 #define SAA7134_BOARD_CINERGY600_MK3   48
175 #define SAA7134_BOARD_VIDEOMATE_GOLD_PLUS 49
176 #define SAA7134_BOARD_PINNACLE_300I_DVBT_PAL 50
177 #define SAA7134_BOARD_PROVIDEO_PV952   51
178 #define SAA7134_BOARD_AVERMEDIA_305    52
179 #define SAA7135_BOARD_ASUSTeK_TVFM7135 53
180 #define SAA7134_BOARD_FLYTVPLATINUM_FM 54
181 #define SAA7134_BOARD_FLYDVBTDUO 55
182
183 #define SAA7134_MAXBOARDS 8
184 #define SAA7134_INPUT_MAX 8
185
186 struct saa7134_input {
187         char                    *name;
188         unsigned int            vmux;
189         enum saa7134_audio_in   amux;
190         unsigned int            gpio;
191         unsigned int            tv:1;
192 };
193
194 enum saa7134_mpeg_type {
195         SAA7134_MPEG_UNUSED,
196         SAA7134_MPEG_EMPRESS,
197         SAA7134_MPEG_DVB,
198 };
199
200 struct saa7134_board {
201         char                    *name;
202         unsigned int            audio_clock;
203
204         /* input switching */
205         unsigned int            gpiomask;
206         struct saa7134_input    inputs[SAA7134_INPUT_MAX];
207         struct saa7134_input    radio;
208         struct saa7134_input    mute;
209
210         /* i2c chip info */
211         unsigned int            tuner_type;
212         unsigned int            tda9887_conf;
213
214         /* peripheral I/O */
215         enum saa7134_video_out  video_out;
216         enum saa7134_mpeg_type  mpeg;
217 };
218
219 #define card_has_radio(dev)   (NULL != saa7134_boards[dev->board].radio.name)
220 #define card_is_empress(dev)  (SAA7134_MPEG_EMPRESS == saa7134_boards[dev->board].mpeg)
221 #define card_is_dvb(dev)      (SAA7134_MPEG_DVB     == saa7134_boards[dev->board].mpeg)
222 #define card_has_mpeg(dev)    (SAA7134_MPEG_UNUSED  != saa7134_boards[dev->board].mpeg)
223 #define card(dev)             (saa7134_boards[dev->board])
224 #define card_in(dev,n)        (saa7134_boards[dev->board].inputs[n])
225
226 /* ----------------------------------------------------------- */
227 /* device / file handle status                                 */
228
229 #define RESOURCE_OVERLAY       1
230 #define RESOURCE_VIDEO         2
231 #define RESOURCE_VBI           4
232
233 #define INTERLACE_AUTO         0
234 #define INTERLACE_ON           1
235 #define INTERLACE_OFF          2
236
237 #define BUFFER_TIMEOUT     (HZ/2)  /* 0.5 seconds */
238
239 struct saa7134_dev;
240 struct saa7134_dma;
241
242 /* saa7134 page table */
243 struct saa7134_pgtable {
244         unsigned int               size;
245         __le32                     *cpu;
246         dma_addr_t                 dma;
247 };
248
249 /* tvaudio thread status */
250 struct saa7134_thread {
251         pid_t                      pid;
252         struct completion          exit;
253         wait_queue_head_t          wq;
254         unsigned int               shutdown;
255         unsigned int               scan1;
256         unsigned int               scan2;
257         unsigned int               mode;
258 };
259
260 /* buffer for one video/vbi/ts frame */
261 struct saa7134_buf {
262         /* common v4l buffer stuff -- must be first */
263         struct videobuf_buffer vb;
264
265         /* saa7134 specific */
266         struct saa7134_format   *fmt;
267         unsigned int            top_seen;
268         int (*activate)(struct saa7134_dev *dev,
269                         struct saa7134_buf *buf,
270                         struct saa7134_buf *next);
271
272         /* page tables */
273         struct saa7134_pgtable  *pt;
274 };
275
276 struct saa7134_dmaqueue {
277         struct saa7134_dev         *dev;
278         struct saa7134_buf         *curr;
279         struct list_head           queue;
280         struct timer_list          timeout;
281         unsigned int               need_two;
282 };
283
284 /* video filehandle status */
285 struct saa7134_fh {
286         struct saa7134_dev         *dev;
287         unsigned int               radio;
288         enum v4l2_buf_type         type;
289         unsigned int               resources;
290 #ifdef VIDIOC_G_PRIORITY
291         enum v4l2_priority         prio;
292 #endif
293
294         /* video overlay */
295         struct v4l2_window         win;
296         struct v4l2_clip           clips[8];
297         unsigned int               nclips;
298
299         /* video capture */
300         struct saa7134_format      *fmt;
301         unsigned int               width,height;
302         struct videobuf_queue      cap;
303         struct saa7134_pgtable     pt_cap;
304
305         /* vbi capture */
306         struct videobuf_queue      vbi;
307         struct saa7134_pgtable     pt_vbi;
308 };
309
310 /* oss dsp status */
311 struct saa7134_oss {
312         struct semaphore           lock;
313         int                        minor_mixer;
314         int                        minor_dsp;
315         unsigned int               users_dsp;
316
317         /* mixer */
318         enum saa7134_audio_in      input;
319         unsigned int               count;
320         unsigned int               line1;
321         unsigned int               line2;
322
323         /* dsp */
324         unsigned int               afmt;
325         unsigned int               rate;
326         unsigned int               channels;
327         unsigned int               recording_on;
328         unsigned int               dma_running;
329         unsigned int               blocks;
330         unsigned int               blksize;
331         unsigned int               bufsize;
332         struct saa7134_pgtable     pt;
333         struct videobuf_dmabuf     dma;
334         wait_queue_head_t          wq;
335         unsigned int               dma_blk;
336         unsigned int               read_offset;
337         unsigned int               read_count;
338 };
339
340 /* IR input */
341 struct saa7134_ir {
342         struct input_dev           dev;
343         struct ir_input_state      ir;
344         char                       name[32];
345         char                       phys[32];
346         u32                        mask_keycode;
347         u32                        mask_keydown;
348         u32                        mask_keyup;
349         int                        polling;
350         u32                        last_gpio;
351         struct timer_list          timer;
352 };
353
354 /* ts/mpeg status */
355 struct saa7134_ts {
356         /* TS capture */
357         struct saa7134_pgtable     pt_ts;
358         int                        nr_packets;
359         int                        nr_bufs;
360 };
361
362 /* ts/mpeg ops */
363 struct saa7134_mpeg_ops {
364         enum saa7134_mpeg_type     type;
365         struct list_head           next;
366         int                        (*init)(struct saa7134_dev *dev);
367         int                        (*fini)(struct saa7134_dev *dev);
368         void                       (*signal_change)(struct saa7134_dev *dev);
369 };
370
371 /* global device status */
372 struct saa7134_dev {
373         struct list_head           devlist;
374         struct semaphore           lock;
375         spinlock_t                 slock;
376 #ifdef VIDIOC_G_PRIORITY
377         struct v4l2_prio_state     prio;
378 #endif
379
380         /* various device info */
381         unsigned int               resources;
382         struct video_device        *video_dev;
383         struct video_device        *radio_dev;
384         struct video_device        *vbi_dev;
385         struct saa7134_oss         oss;
386
387         /* infrared remote */
388         int                        has_remote;
389         struct saa7134_ir          *remote;
390
391         /* pci i/o */
392         char                       name[32];
393         int                        nr;
394         struct pci_dev             *pci;
395         unsigned char              pci_rev,pci_lat;
396         __u32                      __iomem *lmmio;
397         __u8                       __iomem *bmmio;
398
399         /* config info */
400         unsigned int               board;
401         unsigned int               tuner_type;
402         unsigned int               tda9887_conf;
403         unsigned int               gpio_value;
404         unsigned int               irq2_mask;
405
406         /* i2c i/o */
407         struct i2c_adapter         i2c_adap;
408         struct i2c_client          i2c_client;
409         unsigned char              eedata[64];
410
411         /* video overlay */
412         struct v4l2_framebuffer    ovbuf;
413         struct saa7134_format      *ovfmt;
414         unsigned int               ovenable;
415         enum v4l2_field            ovfield;
416
417         /* video+ts+vbi capture */
418         struct saa7134_dmaqueue    video_q;
419         struct saa7134_dmaqueue    vbi_q;
420         unsigned int               video_fieldcount;
421         unsigned int               vbi_fieldcount;
422
423         /* various v4l controls */
424         struct saa7134_tvnorm      *tvnorm;              /* video */
425         struct saa7134_tvaudio     *tvaudio;
426         unsigned int               ctl_input;
427         int                        ctl_bright;
428         int                        ctl_contrast;
429         int                        ctl_hue;
430         int                        ctl_saturation;
431         int                        ctl_freq;
432         int                        ctl_mute;             /* audio */
433         int                        ctl_volume;
434         int                        ctl_invert;           /* private */
435         int                        ctl_mirror;
436         int                        ctl_y_odd;
437         int                        ctl_y_even;
438         int                        ctl_automute;
439
440         /* crop */
441         struct v4l2_rect           crop_bounds;
442         struct v4l2_rect           crop_defrect;
443         struct v4l2_rect           crop_current;
444
445         /* other global state info */
446         unsigned int               automute;
447         struct saa7134_thread      thread;
448         struct saa7134_input       *input;
449         struct saa7134_input       *hw_input;
450         unsigned int               hw_mute;
451         int                        last_carrier;
452         int                        nosignal;
453
454         /* SAA7134_MPEG_* */
455         struct saa7134_ts          ts;
456         struct saa7134_dmaqueue    ts_q;
457         struct saa7134_mpeg_ops    *mops;
458
459         /* SAA7134_MPEG_EMPRESS only */
460         struct video_device        *empress_dev;
461         struct videobuf_queue      empress_tsq;
462         unsigned int               empress_users;
463         struct work_struct         empress_workqueue;
464         int                        empress_started;
465
466         /* SAA7134_MPEG_DVB only */
467         struct videobuf_dvb        dvb;
468 };
469
470 /* ----------------------------------------------------------- */
471
472 #define saa_readl(reg)             readl(dev->lmmio + (reg))
473 #define saa_writel(reg,value)      writel((value), dev->lmmio + (reg));
474 #define saa_andorl(reg,mask,value) \
475   writel((readl(dev->lmmio+(reg)) & ~(mask)) |\
476   ((value) & (mask)), dev->lmmio+(reg))
477 #define saa_setl(reg,bit)          saa_andorl((reg),(bit),(bit))
478 #define saa_clearl(reg,bit)        saa_andorl((reg),(bit),0)
479
480 #define saa_readb(reg)             readb(dev->bmmio + (reg))
481 #define saa_writeb(reg,value)      writeb((value), dev->bmmio + (reg));
482 #define saa_andorb(reg,mask,value) \
483   writeb((readb(dev->bmmio+(reg)) & ~(mask)) |\
484   ((value) & (mask)), dev->bmmio+(reg))
485 #define saa_setb(reg,bit)          saa_andorb((reg),(bit),(bit))
486 #define saa_clearb(reg,bit)        saa_andorb((reg),(bit),0)
487
488 #define saa_wait(us) { udelay(us); }
489
490 /* ----------------------------------------------------------- */
491 /* saa7134-core.c                                              */
492
493 extern struct list_head  saa7134_devlist;
494
495 void saa7134_print_ioctl(char *name, unsigned int cmd);
496 void saa7134_track_gpio(struct saa7134_dev *dev, char *msg);
497
498 #define SAA7134_PGTABLE_SIZE 4096
499
500 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt);
501 int  saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
502                            struct scatterlist *list, unsigned int length,
503                            unsigned int startpage);
504 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt);
505
506 int saa7134_buffer_count(unsigned int size, unsigned int count);
507 int saa7134_buffer_startpage(struct saa7134_buf *buf);
508 unsigned long saa7134_buffer_base(struct saa7134_buf *buf);
509
510 int saa7134_buffer_queue(struct saa7134_dev *dev, struct saa7134_dmaqueue *q,
511                          struct saa7134_buf *buf);
512 void saa7134_buffer_finish(struct saa7134_dev *dev, struct saa7134_dmaqueue *q,
513                            unsigned int state);
514 void saa7134_buffer_next(struct saa7134_dev *dev, struct saa7134_dmaqueue *q);
515 void saa7134_buffer_timeout(unsigned long data);
516 void saa7134_dma_free(struct saa7134_dev *dev,struct saa7134_buf *buf);
517
518 int saa7134_set_dmabits(struct saa7134_dev *dev);
519
520 /* ----------------------------------------------------------- */
521 /* saa7134-cards.c                                             */
522
523 extern struct saa7134_board saa7134_boards[];
524 extern const unsigned int saa7134_bcount;
525 extern struct pci_device_id __devinitdata saa7134_pci_tbl[];
526
527 extern int saa7134_board_init1(struct saa7134_dev *dev);
528 extern int saa7134_board_init2(struct saa7134_dev *dev);
529
530
531 /* ----------------------------------------------------------- */
532 /* saa7134-i2c.c                                               */
533
534 int saa7134_i2c_register(struct saa7134_dev *dev);
535 int saa7134_i2c_unregister(struct saa7134_dev *dev);
536 void saa7134_i2c_call_clients(struct saa7134_dev *dev,
537                               unsigned int cmd, void *arg);
538
539
540 /* ----------------------------------------------------------- */
541 /* saa7134-video.c                                             */
542
543 extern struct video_device saa7134_video_template;
544 extern struct video_device saa7134_radio_template;
545
546 int saa7134_common_ioctl(struct saa7134_dev *dev,
547                          unsigned int cmd, void *arg);
548
549 int saa7134_video_init1(struct saa7134_dev *dev);
550 int saa7134_video_init2(struct saa7134_dev *dev);
551 int saa7134_video_fini(struct saa7134_dev *dev);
552 void saa7134_irq_video_intl(struct saa7134_dev *dev);
553 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status);
554
555
556 /* ----------------------------------------------------------- */
557 /* saa7134-ts.c                                                */
558
559 #define TS_PACKET_SIZE 188 /* TS packets 188 bytes */
560
561 extern struct videobuf_queue_ops saa7134_ts_qops;
562
563 int saa7134_ts_init1(struct saa7134_dev *dev);
564 int saa7134_ts_fini(struct saa7134_dev *dev);
565 void saa7134_irq_ts_done(struct saa7134_dev *dev, unsigned long status);
566
567 int saa7134_ts_register(struct saa7134_mpeg_ops *ops);
568 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops);
569
570 /* ----------------------------------------------------------- */
571 /* saa7134-vbi.c                                               */
572
573 extern struct videobuf_queue_ops saa7134_vbi_qops;
574 extern struct video_device saa7134_vbi_template;
575
576 int saa7134_vbi_init1(struct saa7134_dev *dev);
577 int saa7134_vbi_fini(struct saa7134_dev *dev);
578 void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status);
579
580
581 /* ----------------------------------------------------------- */
582 /* saa7134-tvaudio.c                                           */
583
584 int saa7134_tvaudio_rx2mode(u32 rx);
585
586 void saa7134_tvaudio_setmute(struct saa7134_dev *dev);
587 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
588                               struct saa7134_input *in);
589 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level);
590 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev);
591
592 int saa7134_tvaudio_init2(struct saa7134_dev *dev);
593 int saa7134_tvaudio_fini(struct saa7134_dev *dev);
594 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev);
595
596 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value);
597
598 /* ----------------------------------------------------------- */
599 /* saa7134-oss.c                                               */
600
601 extern struct file_operations saa7134_dsp_fops;
602 extern struct file_operations saa7134_mixer_fops;
603
604 int saa7134_oss_init1(struct saa7134_dev *dev);
605 int saa7134_oss_fini(struct saa7134_dev *dev);
606 void saa7134_irq_oss_done(struct saa7134_dev *dev, unsigned long status);
607
608 /* ----------------------------------------------------------- */
609 /* saa7134-input.c                                             */
610
611 int  saa7134_input_init1(struct saa7134_dev *dev);
612 void saa7134_input_fini(struct saa7134_dev *dev);
613 void saa7134_input_irq(struct saa7134_dev *dev);
614
615 /*
616  * Local variables:
617  * c-basic-offset: 8
618  * End:
619  */