V4L/DVB (4213): Cx88: cleanups
[linux-2.6.git] / drivers / media / video / cx88 / cx88.h
1 /*
2  *
3  * v4l2 device driver for cx2388x based TV cards
4  *
5  * (c) 2003,04 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/pci.h>
23 #include <linux/i2c.h>
24 #include <linux/i2c-algo-bit.h>
25 #include <linux/videodev2.h>
26 #include <linux/kdev_t.h>
27
28 #include <media/v4l2-common.h>
29 #include <media/tuner.h>
30 #include <media/tveeprom.h>
31 #include <media/video-buf.h>
32 #include <media/cx2341x.h>
33 #include <media/video-buf-dvb.h>
34
35 #include "btcx-risc.h"
36 #include "cx88-reg.h"
37
38 #include <linux/version.h>
39 #include <linux/mutex.h>
40 #define CX88_VERSION_CODE KERNEL_VERSION(0,0,6)
41
42 #ifndef TRUE
43 # define TRUE (1==1)
44 #endif
45 #ifndef FALSE
46 # define FALSE (1==0)
47 #endif
48 #define UNSET (-1U)
49
50 #define CX88_MAXBOARDS 8
51
52 /* Max number of inputs by card */
53 #define MAX_CX88_INPUT 8
54
55 /* ----------------------------------------------------------- */
56 /* defines and enums                                           */
57
58 #define FORMAT_FLAGS_PACKED       0x01
59 #define FORMAT_FLAGS_PLANAR       0x02
60
61 #define VBI_LINE_COUNT              17
62 #define VBI_LINE_LENGTH           2048
63
64 /* need "shadow" registers for some write-only ones ... */
65 #define SHADOW_AUD_VOL_CTL           1
66 #define SHADOW_AUD_BAL_CTL           2
67 #define SHADOW_MAX                   3
68
69 /* FM Radio deemphasis type */
70 enum cx88_deemph_type {
71         FM_NO_DEEMPH = 0,
72         FM_DEEMPH_50,
73         FM_DEEMPH_75
74 };
75
76 /* ----------------------------------------------------------- */
77 /* tv norms                                                    */
78
79 struct cx88_tvnorm {
80         char                   *name;
81         v4l2_std_id            id;
82         u32                    cxiformat;
83         u32                    cxoformat;
84 };
85
86 static unsigned int inline norm_maxw(struct cx88_tvnorm *norm)
87 {
88         return (norm->id & V4L2_STD_625_50) ? 768 : 640;
89 }
90
91
92 static unsigned int inline norm_maxh(struct cx88_tvnorm *norm)
93 {
94         return (norm->id & V4L2_STD_625_50) ? 576 : 480;
95 }
96
97 /* ----------------------------------------------------------- */
98 /* static data                                                 */
99
100 struct cx8800_fmt {
101         char  *name;
102         u32   fourcc;          /* v4l2 format id */
103         int   depth;
104         int   flags;
105         u32   cxformat;
106 };
107
108 struct cx88_ctrl {
109         struct v4l2_queryctrl  v;
110         u32                    off;
111         u32                    reg;
112         u32                    sreg;
113         u32                    mask;
114         u32                    shift;
115 };
116
117 /* ----------------------------------------------------------- */
118 /* SRAM memory management data (see cx88-core.c)               */
119
120 #define SRAM_CH21 0   /* video */
121 #define SRAM_CH22 1
122 #define SRAM_CH23 2
123 #define SRAM_CH24 3   /* vbi   */
124 #define SRAM_CH25 4   /* audio */
125 #define SRAM_CH26 5
126 #define SRAM_CH28 6   /* mpeg */
127 /* more */
128
129 struct sram_channel {
130         char *name;
131         u32  cmds_start;
132         u32  ctrl_start;
133         u32  cdt;
134         u32  fifo_start;
135         u32  fifo_size;
136         u32  ptr1_reg;
137         u32  ptr2_reg;
138         u32  cnt1_reg;
139         u32  cnt2_reg;
140 };
141 extern struct sram_channel cx88_sram_channels[];
142
143 /* ----------------------------------------------------------- */
144 /* card configuration                                          */
145
146 #define CX88_BOARD_NOAUTO               UNSET
147 #define CX88_BOARD_UNKNOWN                  0
148 #define CX88_BOARD_HAUPPAUGE                1
149 #define CX88_BOARD_GDI                      2
150 #define CX88_BOARD_PIXELVIEW                3
151 #define CX88_BOARD_ATI_WONDER_PRO           4
152 #define CX88_BOARD_WINFAST2000XP_EXPERT     5
153 #define CX88_BOARD_AVERTV_STUDIO_303        6
154 #define CX88_BOARD_MSI_TVANYWHERE_MASTER    7
155 #define CX88_BOARD_WINFAST_DV2000           8
156 #define CX88_BOARD_LEADTEK_PVR2000          9
157 #define CX88_BOARD_IODATA_GVVCP3PCI        10
158 #define CX88_BOARD_PROLINK_PLAYTVPVR       11
159 #define CX88_BOARD_ASUS_PVR_416            12
160 #define CX88_BOARD_MSI_TVANYWHERE          13
161 #define CX88_BOARD_KWORLD_DVB_T            14
162 #define CX88_BOARD_DVICO_FUSIONHDTV_DVB_T1 15
163 #define CX88_BOARD_KWORLD_LTV883           16
164 #define CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q  17
165 #define CX88_BOARD_HAUPPAUGE_DVB_T1        18
166 #define CX88_BOARD_CONEXANT_DVB_T1         19
167 #define CX88_BOARD_PROVIDEO_PV259          20
168 #define CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_PLUS 21
169 #define CX88_BOARD_PCHDTV_HD3000           22
170 #define CX88_BOARD_DNTV_LIVE_DVB_T         23
171 #define CX88_BOARD_HAUPPAUGE_ROSLYN        24
172 #define CX88_BOARD_DIGITALLOGIC_MEC        25
173 #define CX88_BOARD_IODATA_GVBCTV7E         26
174 #define CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO 27
175 #define CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T  28
176 #define CX88_BOARD_ADSTECH_DVB_T_PCI          29
177 #define CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1  30
178 #define CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD 31
179 #define CX88_BOARD_AVERMEDIA_ULTRATV_MC_550 32
180 #define CX88_BOARD_KWORLD_VSTREAM_EXPERT_DVD 33
181 #define CX88_BOARD_ATI_HDTVWONDER          34
182 #define CX88_BOARD_WINFAST_DTV1000         35
183 #define CX88_BOARD_AVERTV_303              36
184 #define CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1  37
185 #define CX88_BOARD_HAUPPAUGE_NOVASE2_S1    38
186 #define CX88_BOARD_KWORLD_DVBS_100         39
187 #define CX88_BOARD_HAUPPAUGE_HVR1100       40
188 #define CX88_BOARD_HAUPPAUGE_HVR1100LP     41
189 #define CX88_BOARD_DNTV_LIVE_DVB_T_PRO     42
190 #define CX88_BOARD_KWORLD_DVB_T_CX22702    43
191 #define CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_DUAL 44
192 #define CX88_BOARD_KWORLD_HARDWARE_MPEG_TV_XPERT 45
193 #define CX88_BOARD_DVICO_FUSIONHDTV_DVB_T_HYBRID 46
194 #define CX88_BOARD_PCHDTV_HD5500           47
195 #define CX88_BOARD_KWORLD_MCE200_DELUXE    48
196 #define CX88_BOARD_PIXELVIEW_PLAYTV_P7000  49
197 #define CX88_BOARD_NPGTECH_REALTV_TOP10FM  50
198 #define CX88_BOARD_WINFAST_DTV2000H        51
199
200 enum cx88_itype {
201         CX88_VMUX_COMPOSITE1 = 1,
202         CX88_VMUX_COMPOSITE2,
203         CX88_VMUX_COMPOSITE3,
204         CX88_VMUX_COMPOSITE4,
205         CX88_VMUX_SVIDEO,
206         CX88_VMUX_TELEVISION,
207         CX88_VMUX_CABLE,
208         CX88_VMUX_DVB,
209         CX88_VMUX_DEBUG,
210         CX88_RADIO,
211 };
212
213 struct cx88_input {
214         enum cx88_itype type;
215         unsigned int    vmux;
216         u32             gpio0, gpio1, gpio2, gpio3;
217 };
218
219 struct cx88_board {
220         char                    *name;
221         unsigned int            tuner_type;
222         unsigned int            radio_type;
223         unsigned char           tuner_addr;
224         unsigned char           radio_addr;
225         int                     tda9887_conf;
226         struct cx88_input       input[MAX_CX88_INPUT];
227         struct cx88_input       radio;
228         unsigned int            blackbird:1;
229         unsigned int            dvb:1;
230 };
231
232 struct cx88_subid {
233         u16     subvendor;
234         u16     subdevice;
235         u32     card;
236 };
237
238 #define INPUT(nr) (&cx88_boards[core->board].input[nr])
239
240 /* ----------------------------------------------------------- */
241 /* device / file handle status                                 */
242
243 #define RESOURCE_OVERLAY       1
244 #define RESOURCE_VIDEO         2
245 #define RESOURCE_VBI           4
246
247 #define BUFFER_TIMEOUT     (HZ/2)  /* 0.5 seconds */
248
249 /* buffer for one video frame */
250 struct cx88_buffer {
251         /* common v4l buffer stuff -- must be first */
252         struct videobuf_buffer vb;
253
254         /* cx88 specific */
255         unsigned int           bpl;
256         struct btcx_riscmem    risc;
257         struct cx8800_fmt      *fmt;
258         u32                    count;
259 };
260
261 struct cx88_dmaqueue {
262         struct list_head       active;
263         struct list_head       queued;
264         struct timer_list      timeout;
265         struct btcx_riscmem    stopper;
266         u32                    count;
267 };
268
269 struct cx88_core {
270         struct list_head           devlist;
271         atomic_t                   refcount;
272
273         /* board name */
274         int                        nr;
275         char                       name[32];
276
277         /* pci stuff */
278         int                        pci_bus;
279         int                        pci_slot;
280         u32                        __iomem *lmmio;
281         u8                         __iomem *bmmio;
282         u32                        shadow[SHADOW_MAX];
283         int                        pci_irqmask;
284
285         /* i2c i/o */
286         struct i2c_adapter         i2c_adap;
287         struct i2c_algo_bit_data   i2c_algo;
288         struct i2c_client          i2c_client;
289         u32                        i2c_state, i2c_rc;
290
291         /* config info -- analog */
292         unsigned int               board;
293         unsigned int               tuner_type;
294         unsigned int               radio_type;
295         unsigned char              tuner_addr;
296         unsigned char              radio_addr;
297         unsigned int               tda9887_conf;
298         unsigned int               has_radio;
299
300         /* Supported V4L _STD_ tuner formats */
301         unsigned int               tuner_formats;
302
303         /* config info -- dvb */
304         struct dvb_pll_desc        *pll_desc;
305         unsigned int               pll_addr;
306         int                        (*prev_set_voltage)(struct dvb_frontend* fe, fe_sec_voltage_t voltage);
307
308         /* state info */
309         struct task_struct         *kthread;
310         struct cx88_tvnorm         *tvnorm;
311         u32                        tvaudio;
312         u32                        audiomode_manual;
313         u32                        audiomode_current;
314         u32                        input;
315         u32                        astat;
316         u32                        use_nicam;
317
318         /* IR remote control state */
319         struct cx88_IR             *ir;
320
321         struct mutex               lock;
322         /* various v4l controls */
323         u32                        freq;
324
325         /* cx88-video needs to access cx8802 for hybrid tuner pll access. */
326         struct cx8802_dev          *dvbdev;
327 };
328
329 struct cx8800_dev;
330 struct cx8802_dev;
331
332 /* ----------------------------------------------------------- */
333 /* function 0: video stuff                                     */
334
335 struct cx8800_fh {
336         struct cx8800_dev          *dev;
337         enum v4l2_buf_type         type;
338         int                        radio;
339         unsigned int               resources;
340
341         /* video overlay */
342         struct v4l2_window         win;
343         struct v4l2_clip           *clips;
344         unsigned int               nclips;
345
346         /* video capture */
347         struct cx8800_fmt          *fmt;
348         unsigned int               width,height;
349         struct videobuf_queue      vidq;
350
351         /* vbi capture */
352         struct videobuf_queue      vbiq;
353 };
354
355 struct cx8800_suspend_state {
356         int                        disabled;
357 };
358
359 struct cx8800_dev {
360         struct cx88_core           *core;
361         struct list_head           devlist;
362         spinlock_t                 slock;
363
364         /* various device info */
365         unsigned int               resources;
366         struct video_device        *video_dev;
367         struct video_device        *vbi_dev;
368         struct video_device        *radio_dev;
369
370         /* pci i/o */
371         struct pci_dev             *pci;
372         unsigned char              pci_rev,pci_lat;
373
374
375         /* capture queues */
376         struct cx88_dmaqueue       vidq;
377         struct cx88_dmaqueue       vbiq;
378
379         /* various v4l controls */
380
381         /* other global state info */
382         struct cx8800_suspend_state state;
383 };
384
385 /* ----------------------------------------------------------- */
386 /* function 1: audio/alsa stuff                                */
387 /* =============> moved to cx88-alsa.c <====================== */
388
389
390 /* ----------------------------------------------------------- */
391 /* function 2: mpeg stuff                                      */
392
393 struct cx8802_fh {
394         struct cx8802_dev          *dev;
395         struct videobuf_queue      mpegq;
396 };
397
398 struct cx8802_suspend_state {
399         int                        disabled;
400 };
401
402 struct cx8802_dev {
403         struct cx88_core           *core;
404         spinlock_t                 slock;
405
406         /* pci i/o */
407         struct pci_dev             *pci;
408         unsigned char              pci_rev,pci_lat;
409
410         /* dma queues */
411         struct cx88_dmaqueue       mpegq;
412         u32                        ts_packet_size;
413         u32                        ts_packet_count;
414
415         /* other global state info */
416         struct cx8802_suspend_state state;
417
418         /* for blackbird only */
419         struct list_head           devlist;
420         struct video_device        *mpeg_dev;
421         u32                        mailbox;
422         int                        width;
423         int                        height;
424
425         /* for dvb only */
426         struct videobuf_dvb        dvb;
427         void*                      fe_handle;
428         int                        (*fe_release)(void *handle);
429
430         void                       *card_priv;
431         /* for switching modulation types */
432         unsigned char              ts_gen_cntrl;
433
434         /* mpeg params */
435         struct cx2341x_mpeg_params params;
436 };
437
438 /* ----------------------------------------------------------- */
439
440 #define cx_read(reg)             readl(core->lmmio + ((reg)>>2))
441 #define cx_write(reg,value)      writel((value), core->lmmio + ((reg)>>2))
442 #define cx_writeb(reg,value)     writeb((value), core->bmmio + (reg))
443
444 #define cx_andor(reg,mask,value) \
445   writel((readl(core->lmmio+((reg)>>2)) & ~(mask)) |\
446   ((value) & (mask)), core->lmmio+((reg)>>2))
447 #define cx_set(reg,bit)          cx_andor((reg),(bit),(bit))
448 #define cx_clear(reg,bit)        cx_andor((reg),(bit),0)
449
450 #define cx_wait(d) { if (need_resched()) schedule(); else udelay(d); }
451
452 /* shadow registers */
453 #define cx_sread(sreg)              (core->shadow[sreg])
454 #define cx_swrite(sreg,reg,value) \
455   (core->shadow[sreg] = value, \
456    writel(core->shadow[sreg], core->lmmio + ((reg)>>2)))
457 #define cx_sandor(sreg,reg,mask,value) \
458   (core->shadow[sreg] = (core->shadow[sreg] & ~(mask)) | ((value) & (mask)), \
459    writel(core->shadow[sreg], core->lmmio + ((reg)>>2)))
460
461 /* ----------------------------------------------------------- */
462 /* cx88-core.c                                                 */
463
464 extern void cx88_print_irqbits(char *name, char *tag, char **strings,
465                                u32 bits, u32 mask);
466
467 extern int cx88_core_irq(struct cx88_core *core, u32 status);
468 extern void cx88_wakeup(struct cx88_core *core,
469                         struct cx88_dmaqueue *q, u32 count);
470 extern void cx88_shutdown(struct cx88_core *core);
471 extern int cx88_reset(struct cx88_core *core);
472
473 extern int
474 cx88_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,
475                  struct scatterlist *sglist,
476                  unsigned int top_offset, unsigned int bottom_offset,
477                  unsigned int bpl, unsigned int padding, unsigned int lines);
478 extern int
479 cx88_risc_databuffer(struct pci_dev *pci, struct btcx_riscmem *risc,
480                      struct scatterlist *sglist, unsigned int bpl,
481                      unsigned int lines);
482 extern int
483 cx88_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,
484                   u32 reg, u32 mask, u32 value);
485 extern void
486 cx88_free_buffer(struct videobuf_queue *q, struct cx88_buffer *buf);
487
488 extern void cx88_risc_disasm(struct cx88_core *core,
489                              struct btcx_riscmem *risc);
490 extern int cx88_sram_channel_setup(struct cx88_core *core,
491                                    struct sram_channel *ch,
492                                    unsigned int bpl, u32 risc);
493 extern void cx88_sram_channel_dump(struct cx88_core *core,
494                                    struct sram_channel *ch);
495
496 extern int cx88_set_scale(struct cx88_core *core, unsigned int width,
497                           unsigned int height, enum v4l2_field field);
498 extern int cx88_set_tvnorm(struct cx88_core *core, struct cx88_tvnorm *norm);
499
500 extern struct video_device *cx88_vdev_init(struct cx88_core *core,
501                                            struct pci_dev *pci,
502                                            struct video_device *template,
503                                            char *type);
504 extern struct cx88_core* cx88_core_get(struct pci_dev *pci);
505 extern void cx88_core_put(struct cx88_core *core,
506                           struct pci_dev *pci);
507
508 extern int cx88_start_audio_dma(struct cx88_core *core);
509 extern int cx88_stop_audio_dma(struct cx88_core *core);
510
511
512 /* ----------------------------------------------------------- */
513 /* cx88-vbi.c                                                  */
514
515 void cx8800_vbi_fmt(struct cx8800_dev *dev, struct v4l2_format *f);
516 /*
517 int cx8800_start_vbi_dma(struct cx8800_dev    *dev,
518                          struct cx88_dmaqueue *q,
519                          struct cx88_buffer   *buf);
520 */
521 int cx8800_stop_vbi_dma(struct cx8800_dev *dev);
522 int cx8800_restart_vbi_queue(struct cx8800_dev    *dev,
523                              struct cx88_dmaqueue *q);
524 void cx8800_vbi_timeout(unsigned long data);
525
526 extern struct videobuf_queue_ops cx8800_vbi_qops;
527
528 /* ----------------------------------------------------------- */
529 /* cx88-i2c.c                                                  */
530
531 extern int cx88_i2c_init(struct cx88_core *core, struct pci_dev *pci);
532 extern void cx88_call_i2c_clients(struct cx88_core *core,
533                                   unsigned int cmd, void *arg);
534
535
536 /* ----------------------------------------------------------- */
537 /* cx88-cards.c                                                */
538
539 extern struct cx88_board cx88_boards[];
540 extern const unsigned int cx88_bcount;
541
542 extern struct cx88_subid cx88_subids[];
543 extern const unsigned int cx88_idcount;
544
545 extern void cx88_card_list(struct cx88_core *core, struct pci_dev *pci);
546 extern void cx88_card_setup(struct cx88_core *core);
547
548 /* ----------------------------------------------------------- */
549 /* cx88-tvaudio.c                                              */
550
551 #define WW_NONE          1
552 #define WW_BTSC          2
553 #define WW_BG            3
554 #define WW_DK            4
555 #define WW_I             5
556 #define WW_L             6
557 #define WW_EIAJ          7
558 #define WW_I2SPT         8
559 #define WW_FM            9
560
561 void cx88_set_tvaudio(struct cx88_core *core);
562 void cx88_newstation(struct cx88_core *core);
563 void cx88_get_stereo(struct cx88_core *core, struct v4l2_tuner *t);
564 void cx88_set_stereo(struct cx88_core *core, u32 mode, int manual);
565 int cx88_audio_thread(void *data);
566
567 /* ----------------------------------------------------------- */
568 /* cx88-input.c                                                */
569
570 int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci);
571 int cx88_ir_fini(struct cx88_core *core);
572 void cx88_ir_irq(struct cx88_core *core);
573
574 /* ----------------------------------------------------------- */
575 /* cx88-mpeg.c                                                 */
576
577 int cx8802_buf_prepare(struct videobuf_queue *q,struct cx8802_dev *dev,
578                         struct cx88_buffer *buf, enum v4l2_field field);
579 void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf);
580 void cx8802_cancel_buffers(struct cx8802_dev *dev);
581
582 int cx8802_init_common(struct cx8802_dev *dev);
583 void cx8802_fini_common(struct cx8802_dev *dev);
584
585 int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state);
586 int cx8802_resume_common(struct pci_dev *pci_dev);
587
588 /* ----------------------------------------------------------- */
589 /* cx88-video.c                                                */
590 extern int cx88_do_ioctl(struct inode *inode, struct file *file, int radio,
591                                 struct cx88_core *core, unsigned int cmd,
592                                 void *arg, v4l2_kioctl driver_ioctl);
593
594 /*
595  * Local variables:
596  * c-basic-offset: 8
597  * End:
598  * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
599  */