3 * v4l2 device driver for philips saa7134 based TV cards
5 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
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.
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.
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.
22 #include <linux/version.h>
23 #define SAA7134_VERSION_CODE KERNEL_VERSION(0,2,14)
25 #include <linux/pci.h>
26 #include <linux/i2c.h>
27 #include <linux/videodev.h>
28 #include <linux/kdev_t.h>
29 #include <linux/input.h>
33 #include <media/tuner.h>
34 #include <media/audiochip.h>
36 #include <media/ir-common.h>
37 #include <media/video-buf.h>
38 #include <media/video-buf-dvb.h>
48 /* ----------------------------------------------------------- */
51 enum saa7134_tvaudio_mode {
53 TVAUDIO_FM_BG_STEREO = 2,
54 TVAUDIO_FM_SAT_STEREO = 3,
55 TVAUDIO_FM_K_STEREO = 4,
60 enum saa7134_audio_in {
67 enum saa7134_video_out {
71 /* ----------------------------------------------------------- */
74 struct saa7134_tvnorm {
79 unsigned int sync_control;
80 unsigned int luma_control;
81 unsigned int chroma_ctrl1;
82 unsigned int chroma_gain;
83 unsigned int chroma_ctrl2;
84 unsigned int vgate_misc;
89 unsigned int video_v_start;
90 unsigned int video_v_stop;
91 unsigned int vbi_v_start_0;
92 unsigned int vbi_v_stop_0;
93 unsigned int src_timing;
94 unsigned int vbi_v_start_1;
97 struct saa7134_tvaudio {
100 enum saa7134_tvaudio_mode mode;
105 struct saa7134_format {
110 unsigned int vshift; /* vertical downsampling (for planar yuv) */
111 unsigned int hshift; /* horizontal downsampling (for planar yuv) */
112 unsigned int bswap:1;
113 unsigned int wswap:1;
115 unsigned int planar:1;
116 unsigned int uvswap:1;
119 /* ----------------------------------------------------------- */
120 /* card configuration */
122 #define SAA7134_BOARD_NOAUTO UNSET
123 #define SAA7134_BOARD_UNKNOWN 0
124 #define SAA7134_BOARD_PROTEUS_PRO 1
125 #define SAA7134_BOARD_FLYVIDEO3000 2
126 #define SAA7134_BOARD_FLYVIDEO2000 3
127 #define SAA7134_BOARD_EMPRESS 4
128 #define SAA7134_BOARD_MONSTERTV 5
129 #define SAA7134_BOARD_MD9717 6
130 #define SAA7134_BOARD_TVSTATION_RDS 7
131 #define SAA7134_BOARD_CINERGY400 8
132 #define SAA7134_BOARD_MD5044 9
133 #define SAA7134_BOARD_KWORLD 10
134 #define SAA7134_BOARD_CINERGY600 11
135 #define SAA7134_BOARD_MD7134 12
136 #define SAA7134_BOARD_TYPHOON_90031 13
137 #define SAA7134_BOARD_ELSA 14
138 #define SAA7134_BOARD_ELSA_500TV 15
139 #define SAA7134_BOARD_ASUSTeK_TVFM7134 16
140 #define SAA7134_BOARD_VA1000POWER 17
141 #define SAA7134_BOARD_BMK_MPEX_NOTUNER 18
142 #define SAA7134_BOARD_VIDEOMATE_TV 19
143 #define SAA7134_BOARD_CRONOS_PLUS 20
144 #define SAA7134_BOARD_10MOONSTVMASTER 21
145 #define SAA7134_BOARD_MD2819 22
146 #define SAA7134_BOARD_BMK_MPEX_TUNER 23
147 #define SAA7134_BOARD_TVSTATION_DVR 24
148 #define SAA7134_BOARD_ASUSTEK_TVFM7133 25
149 #define SAA7134_BOARD_PINNACLE_PCTV_STEREO 26
150 #define SAA7134_BOARD_MANLI_MTV002 27
151 #define SAA7134_BOARD_MANLI_MTV001 28
152 #define SAA7134_BOARD_TG3000TV 29
153 #define SAA7134_BOARD_ECS_TVP3XP 30
154 #define SAA7134_BOARD_ECS_TVP3XP_4CB5 31
155 #define SAA7134_BOARD_AVACSSMARTTV 32
156 #define SAA7134_BOARD_AVERMEDIA_DVD_EZMAKER 33
157 #define SAA7134_BOARD_NOVAC_PRIMETV7133 34
158 #define SAA7134_BOARD_AVERMEDIA_STUDIO_305 35
159 #define SAA7134_BOARD_UPMOST_PURPLE_TV 36
160 #define SAA7134_BOARD_ITEMS_MTV005 37
161 #define SAA7134_BOARD_CINERGY200 38
162 #define SAA7134_BOARD_FLYTVPLATINUM_MINI 39
163 #define SAA7134_BOARD_VIDEOMATE_TV_PVR 40
164 #define SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUS 41
165 #define SAA7134_BOARD_SABRENT_SBTTVFM 42
166 #define SAA7134_BOARD_ZOLID_XPERT_TV7134 43
167 #define SAA7134_BOARD_EMPIRE_PCI_TV_RADIO_LE 44
168 #define SAA7134_BOARD_AVERMEDIA_STUDIO_307 45
169 #define SAA7134_BOARD_AVERMEDIA_CARDBUS 46
170 #define SAA7134_BOARD_CINERGY400_CARDBUS 47
171 #define SAA7134_BOARD_CINERGY600_MK3 48
172 #define SAA7134_BOARD_VIDEOMATE_GOLD_PLUS 49
173 #define SAA7134_BOARD_PINNACLE_300I_DVBT_PAL 50
174 #define SAA7134_BOARD_PROVIDEO_PV952 51
175 #define SAA7134_BOARD_AVERMEDIA_305 52
176 #define SAA7134_BOARD_ASUSTeK_TVFM7135 53
177 #define SAA7134_BOARD_FLYTVPLATINUM_FM 54
178 #define SAA7134_BOARD_FLYDVBTDUO 55
179 #define SAA7134_BOARD_AVERMEDIA_307 56
180 #define SAA7134_BOARD_AVERMEDIA_GO_007_FM 57
181 #define SAA7134_BOARD_ADS_INSTANT_TV 58
182 #define SAA7134_BOARD_KWORLD_VSTREAM_XPERT 59
183 #define SAA7134_BOARD_THYPHOON_DVBT_DUO_CARDBUS 60
184 #define SAA7134_BOARD_PHILIPS_TOUGH 61
185 #define SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII 62
186 #define SAA7134_BOARD_KWORLD_XPERT 63
187 #define SAA7134_BOARD_FLYTV_DIGIMATRIX 64
188 #define SAA7134_BOARD_KWORLD_TERMINATOR 65
189 #define SAA7134_BOARD_YUAN_TUN900 66
191 #define SAA7134_MAXBOARDS 8
192 #define SAA7134_INPUT_MAX 8
194 struct saa7134_input {
197 enum saa7134_audio_in amux;
202 enum saa7134_mpeg_type {
204 SAA7134_MPEG_EMPRESS,
208 struct saa7134_board {
210 unsigned int audio_clock;
212 /* input switching */
213 unsigned int gpiomask;
214 struct saa7134_input inputs[SAA7134_INPUT_MAX];
215 struct saa7134_input radio;
216 struct saa7134_input mute;
219 unsigned int tuner_type;
220 unsigned int radio_type;
221 unsigned char tuner_addr;
222 unsigned char radio_addr;
224 unsigned int tda9887_conf;
227 enum saa7134_video_out video_out;
228 enum saa7134_mpeg_type mpeg;
231 #define card_has_radio(dev) (NULL != saa7134_boards[dev->board].radio.name)
232 #define card_is_empress(dev) (SAA7134_MPEG_EMPRESS == saa7134_boards[dev->board].mpeg)
233 #define card_is_dvb(dev) (SAA7134_MPEG_DVB == saa7134_boards[dev->board].mpeg)
234 #define card_has_mpeg(dev) (SAA7134_MPEG_UNUSED != saa7134_boards[dev->board].mpeg)
235 #define card(dev) (saa7134_boards[dev->board])
236 #define card_in(dev,n) (saa7134_boards[dev->board].inputs[n])
238 /* ----------------------------------------------------------- */
239 /* device / file handle status */
241 #define RESOURCE_OVERLAY 1
242 #define RESOURCE_VIDEO 2
243 #define RESOURCE_VBI 4
245 #define INTERLACE_AUTO 0
246 #define INTERLACE_ON 1
247 #define INTERLACE_OFF 2
249 #define BUFFER_TIMEOUT (HZ/2) /* 0.5 seconds */
254 /* saa7134 page table */
255 struct saa7134_pgtable {
261 /* tvaudio thread status */
262 struct saa7134_thread {
264 struct completion exit;
265 wait_queue_head_t wq;
266 unsigned int shutdown;
272 /* buffer for one video/vbi/ts frame */
274 /* common v4l buffer stuff -- must be first */
275 struct videobuf_buffer vb;
277 /* saa7134 specific */
278 struct saa7134_format *fmt;
279 unsigned int top_seen;
280 int (*activate)(struct saa7134_dev *dev,
281 struct saa7134_buf *buf,
282 struct saa7134_buf *next);
285 struct saa7134_pgtable *pt;
288 struct saa7134_dmaqueue {
289 struct saa7134_dev *dev;
290 struct saa7134_buf *curr;
291 struct list_head queue;
292 struct timer_list timeout;
293 unsigned int need_two;
296 /* video filehandle status */
298 struct saa7134_dev *dev;
300 enum v4l2_buf_type type;
301 unsigned int resources;
302 #ifdef VIDIOC_G_PRIORITY
303 enum v4l2_priority prio;
307 struct v4l2_window win;
308 struct v4l2_clip clips[8];
312 struct saa7134_format *fmt;
313 unsigned int width,height;
314 struct videobuf_queue cap;
315 struct saa7134_pgtable pt_cap;
318 struct videobuf_queue vbi;
319 struct saa7134_pgtable pt_vbi;
324 struct semaphore lock;
327 unsigned int users_dsp;
330 enum saa7134_audio_in input;
338 unsigned int channels;
339 unsigned int recording_on;
340 unsigned int dma_running;
342 unsigned int blksize;
343 unsigned int bufsize;
344 struct saa7134_pgtable pt;
345 struct videobuf_dmabuf dma;
346 wait_queue_head_t wq;
347 unsigned int dma_blk;
348 unsigned int read_offset;
349 unsigned int read_count;
354 struct input_dev dev;
355 struct ir_input_state ir;
363 struct timer_list timer;
369 struct saa7134_pgtable pt_ts;
375 struct saa7134_mpeg_ops {
376 enum saa7134_mpeg_type type;
377 struct list_head next;
378 int (*init)(struct saa7134_dev *dev);
379 int (*fini)(struct saa7134_dev *dev);
380 void (*signal_change)(struct saa7134_dev *dev);
383 /* global device status */
385 struct list_head devlist;
386 struct semaphore lock;
388 #ifdef VIDIOC_G_PRIORITY
389 struct v4l2_prio_state prio;
392 /* various device info */
393 unsigned int resources;
394 struct video_device *video_dev;
395 struct video_device *radio_dev;
396 struct video_device *vbi_dev;
397 struct saa7134_oss oss;
399 /* infrared remote */
401 struct saa7134_ir *remote;
407 unsigned char pci_rev,pci_lat;
408 __u32 __iomem *lmmio;
413 unsigned int tuner_type;
414 unsigned int radio_type;
415 unsigned char tuner_addr;
416 unsigned char radio_addr;
418 unsigned int tda9887_conf;
419 unsigned int gpio_value;
422 struct i2c_adapter i2c_adap;
423 struct i2c_client i2c_client;
424 unsigned char eedata[64];
427 struct v4l2_framebuffer ovbuf;
428 struct saa7134_format *ovfmt;
429 unsigned int ovenable;
430 enum v4l2_field ovfield;
432 /* video+ts+vbi capture */
433 struct saa7134_dmaqueue video_q;
434 struct saa7134_dmaqueue vbi_q;
435 unsigned int video_fieldcount;
436 unsigned int vbi_fieldcount;
438 /* various v4l controls */
439 struct saa7134_tvnorm *tvnorm; /* video */
440 struct saa7134_tvaudio *tvaudio;
441 unsigned int ctl_input;
447 int ctl_mute; /* audio */
449 int ctl_invert; /* private */
456 struct v4l2_rect crop_bounds;
457 struct v4l2_rect crop_defrect;
458 struct v4l2_rect crop_current;
460 /* other global state info */
461 unsigned int automute;
462 struct saa7134_thread thread;
463 struct saa7134_input *input;
464 struct saa7134_input *hw_input;
465 unsigned int hw_mute;
470 struct saa7134_ts ts;
471 struct saa7134_dmaqueue ts_q;
472 struct saa7134_mpeg_ops *mops;
474 /* SAA7134_MPEG_EMPRESS only */
475 struct video_device *empress_dev;
476 struct videobuf_queue empress_tsq;
477 unsigned int empress_users;
478 struct work_struct empress_workqueue;
481 /* SAA7134_MPEG_DVB only */
482 struct videobuf_dvb dvb;
485 /* ----------------------------------------------------------- */
487 #define saa_readl(reg) readl(dev->lmmio + (reg))
488 #define saa_writel(reg,value) writel((value), dev->lmmio + (reg));
489 #define saa_andorl(reg,mask,value) \
490 writel((readl(dev->lmmio+(reg)) & ~(mask)) |\
491 ((value) & (mask)), dev->lmmio+(reg))
492 #define saa_setl(reg,bit) saa_andorl((reg),(bit),(bit))
493 #define saa_clearl(reg,bit) saa_andorl((reg),(bit),0)
495 #define saa_readb(reg) readb(dev->bmmio + (reg))
496 #define saa_writeb(reg,value) writeb((value), dev->bmmio + (reg));
497 #define saa_andorb(reg,mask,value) \
498 writeb((readb(dev->bmmio+(reg)) & ~(mask)) |\
499 ((value) & (mask)), dev->bmmio+(reg))
500 #define saa_setb(reg,bit) saa_andorb((reg),(bit),(bit))
501 #define saa_clearb(reg,bit) saa_andorb((reg),(bit),0)
503 #define saa_wait(us) { udelay(us); }
505 /* ----------------------------------------------------------- */
508 extern struct list_head saa7134_devlist;
510 void saa7134_print_ioctl(char *name, unsigned int cmd);
511 void saa7134_track_gpio(struct saa7134_dev *dev, char *msg);
513 #define SAA7134_PGTABLE_SIZE 4096
515 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt);
516 int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
517 struct scatterlist *list, unsigned int length,
518 unsigned int startpage);
519 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt);
521 int saa7134_buffer_count(unsigned int size, unsigned int count);
522 int saa7134_buffer_startpage(struct saa7134_buf *buf);
523 unsigned long saa7134_buffer_base(struct saa7134_buf *buf);
525 int saa7134_buffer_queue(struct saa7134_dev *dev, struct saa7134_dmaqueue *q,
526 struct saa7134_buf *buf);
527 void saa7134_buffer_finish(struct saa7134_dev *dev, struct saa7134_dmaqueue *q,
529 void saa7134_buffer_next(struct saa7134_dev *dev, struct saa7134_dmaqueue *q);
530 void saa7134_buffer_timeout(unsigned long data);
531 void saa7134_dma_free(struct saa7134_dev *dev,struct saa7134_buf *buf);
533 int saa7134_set_dmabits(struct saa7134_dev *dev);
535 /* ----------------------------------------------------------- */
536 /* saa7134-cards.c */
538 extern struct saa7134_board saa7134_boards[];
539 extern const unsigned int saa7134_bcount;
540 extern struct pci_device_id __devinitdata saa7134_pci_tbl[];
542 extern int saa7134_board_init1(struct saa7134_dev *dev);
543 extern int saa7134_board_init2(struct saa7134_dev *dev);
546 /* ----------------------------------------------------------- */
549 int saa7134_i2c_register(struct saa7134_dev *dev);
550 int saa7134_i2c_unregister(struct saa7134_dev *dev);
551 void saa7134_i2c_call_clients(struct saa7134_dev *dev,
552 unsigned int cmd, void *arg);
555 /* ----------------------------------------------------------- */
556 /* saa7134-video.c */
558 extern struct video_device saa7134_video_template;
559 extern struct video_device saa7134_radio_template;
561 int saa7134_common_ioctl(struct saa7134_dev *dev,
562 unsigned int cmd, void *arg);
564 int saa7134_video_init1(struct saa7134_dev *dev);
565 int saa7134_video_init2(struct saa7134_dev *dev);
566 int saa7134_video_fini(struct saa7134_dev *dev);
567 void saa7134_irq_video_intl(struct saa7134_dev *dev);
568 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status);
571 /* ----------------------------------------------------------- */
574 #define TS_PACKET_SIZE 188 /* TS packets 188 bytes */
576 extern struct videobuf_queue_ops saa7134_ts_qops;
578 int saa7134_ts_init1(struct saa7134_dev *dev);
579 int saa7134_ts_fini(struct saa7134_dev *dev);
580 void saa7134_irq_ts_done(struct saa7134_dev *dev, unsigned long status);
582 int saa7134_ts_register(struct saa7134_mpeg_ops *ops);
583 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops);
585 /* ----------------------------------------------------------- */
588 extern struct videobuf_queue_ops saa7134_vbi_qops;
589 extern struct video_device saa7134_vbi_template;
591 int saa7134_vbi_init1(struct saa7134_dev *dev);
592 int saa7134_vbi_fini(struct saa7134_dev *dev);
593 void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status);
596 /* ----------------------------------------------------------- */
597 /* saa7134-tvaudio.c */
599 int saa7134_tvaudio_rx2mode(u32 rx);
601 void saa7134_tvaudio_setmute(struct saa7134_dev *dev);
602 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
603 struct saa7134_input *in);
604 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level);
605 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev);
607 int saa7134_tvaudio_init2(struct saa7134_dev *dev);
608 int saa7134_tvaudio_fini(struct saa7134_dev *dev);
609 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev);
611 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value);
613 /* ----------------------------------------------------------- */
616 extern struct file_operations saa7134_dsp_fops;
617 extern struct file_operations saa7134_mixer_fops;
619 int saa7134_oss_init1(struct saa7134_dev *dev);
620 int saa7134_oss_fini(struct saa7134_dev *dev);
621 void saa7134_irq_oss_done(struct saa7134_dev *dev, unsigned long status);
623 /* ----------------------------------------------------------- */
624 /* saa7134-input.c */
626 int saa7134_input_init1(struct saa7134_dev *dev);
627 void saa7134_input_fini(struct saa7134_dev *dev);
628 void saa7134_input_irq(struct saa7134_dev *dev);