1 /*
2  *
3  * v4l2 device driver for philips saa7134 based TV cards
4  *
5  * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
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 #define SAA7134_VERSION "0, 2, 17"
23 
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 
26 #include <linux/pci.h>
27 #include <linux/i2c.h>
28 #include <linux/videodev2.h>
29 #include <linux/kdev_t.h>
30 #include <linux/input.h>
31 #include <linux/notifier.h>
32 #include <linux/delay.h>
33 #include <linux/mutex.h>
34 #include <linux/pm_qos.h>
35 
36 #include <asm/io.h>
37 
38 #include <media/v4l2-common.h>
39 #include <media/v4l2-ioctl.h>
40 #include <media/v4l2-device.h>
41 #include <media/v4l2-fh.h>
42 #include <media/v4l2-ctrls.h>
43 #include <media/tuner.h>
44 #include <media/rc-core.h>
45 #include <media/i2c/ir-kbd-i2c.h>
46 #include <media/videobuf2-dma-sg.h>
47 #include <sound/core.h>
48 #include <sound/pcm.h>
49 #if IS_ENABLED(CONFIG_VIDEO_SAA7134_DVB)
50 #include <media/videobuf2-dvb.h>
51 #endif
52 #include "tda8290.h"
53 
54 #define UNSET (-1U)
55 
56 /* ----------------------------------------------------------- */
57 /* enums                                                       */
58 
59 enum saa7134_tvaudio_mode {
60 	TVAUDIO_FM_MONO       = 1,
61 	TVAUDIO_FM_BG_STEREO  = 2,
62 	TVAUDIO_FM_SAT_STEREO = 3,
63 	TVAUDIO_FM_K_STEREO   = 4,
64 	TVAUDIO_NICAM_AM      = 5,
65 	TVAUDIO_NICAM_FM      = 6,
66 };
67 
68 enum saa7134_audio_in {
69 	TV    = 1,
70 	LINE1 = 2,
71 	LINE2 = 3,
72 	LINE2_LEFT,
73 };
74 
75 enum saa7134_video_out {
76 	CCIR656 = 1,
77 };
78 
79 /* ----------------------------------------------------------- */
80 /* static data                                                 */
81 
82 struct saa7134_tvnorm {
83 	char          *name;
84 	v4l2_std_id   id;
85 
86 	/* video decoder */
87 	unsigned int  sync_control;
88 	unsigned int  luma_control;
89 	unsigned int  chroma_ctrl1;
90 	unsigned int  chroma_gain;
91 	unsigned int  chroma_ctrl2;
92 	unsigned int  vgate_misc;
93 
94 	/* video scaler */
95 	unsigned int  h_start;
96 	unsigned int  h_stop;
97 	unsigned int  video_v_start;
98 	unsigned int  video_v_stop;
99 	unsigned int  vbi_v_start_0;
100 	unsigned int  vbi_v_stop_0;
101 	unsigned int  src_timing;
102 	unsigned int  vbi_v_start_1;
103 };
104 
105 struct saa7134_tvaudio {
106 	char         *name;
107 	v4l2_std_id  std;
108 	enum         saa7134_tvaudio_mode mode;
109 	int          carr1;
110 	int          carr2;
111 };
112 
113 struct saa7134_format {
114 	char           *name;
115 	unsigned int   fourcc;
116 	unsigned int   depth;
117 	unsigned int   pm;
118 	unsigned int   vshift;   /* vertical downsampling (for planar yuv) */
119 	unsigned int   hshift;   /* horizontal downsampling (for planar yuv) */
120 	unsigned int   bswap:1;
121 	unsigned int   wswap:1;
122 	unsigned int   yuv:1;
123 	unsigned int   planar:1;
124 	unsigned int   uvswap:1;
125 };
126 
127 struct saa7134_card_ir {
128 	struct rc_dev		*dev;
129 
130 	char                    name[32];
131 	char                    phys[32];
132 	unsigned                users;
133 
134 	u32			polling;
135 	u32			last_gpio;
136 	u32			mask_keycode, mask_keydown, mask_keyup;
137 
138 	bool                    running;
139 
140 	struct timer_list       timer;
141 
142 	/* IR core raw decoding */
143 	u32                     raw_decode;
144 };
145 
146 /* ----------------------------------------------------------- */
147 /* card configuration                                          */
148 
149 #define SAA7134_BOARD_NOAUTO        UNSET
150 #define SAA7134_BOARD_UNKNOWN           0
151 #define SAA7134_BOARD_PROTEUS_PRO       1
152 #define SAA7134_BOARD_FLYVIDEO3000      2
153 #define SAA7134_BOARD_FLYVIDEO2000      3
154 #define SAA7134_BOARD_EMPRESS           4
155 #define SAA7134_BOARD_MONSTERTV         5
156 #define SAA7134_BOARD_MD9717            6
157 #define SAA7134_BOARD_TVSTATION_RDS     7
158 #define SAA7134_BOARD_CINERGY400	8
159 #define SAA7134_BOARD_MD5044		9
160 #define SAA7134_BOARD_KWORLD           10
161 #define SAA7134_BOARD_CINERGY600       11
162 #define SAA7134_BOARD_MD7134           12
163 #define SAA7134_BOARD_TYPHOON_90031    13
164 #define SAA7134_BOARD_ELSA             14
165 #define SAA7134_BOARD_ELSA_500TV       15
166 #define SAA7134_BOARD_ASUSTeK_TVFM7134 16
167 #define SAA7134_BOARD_VA1000POWER      17
168 #define SAA7134_BOARD_BMK_MPEX_NOTUNER 18
169 #define SAA7134_BOARD_VIDEOMATE_TV     19
170 #define SAA7134_BOARD_CRONOS_PLUS      20
171 #define SAA7134_BOARD_10MOONSTVMASTER  21
172 #define SAA7134_BOARD_MD2819           22
173 #define SAA7134_BOARD_BMK_MPEX_TUNER   23
174 #define SAA7134_BOARD_TVSTATION_DVR    24
175 #define SAA7134_BOARD_ASUSTEK_TVFM7133	25
176 #define SAA7134_BOARD_PINNACLE_PCTV_STEREO 26
177 #define SAA7134_BOARD_MANLI_MTV002     27
178 #define SAA7134_BOARD_MANLI_MTV001     28
179 #define SAA7134_BOARD_TG3000TV         29
180 #define SAA7134_BOARD_ECS_TVP3XP       30
181 #define SAA7134_BOARD_ECS_TVP3XP_4CB5  31
182 #define SAA7134_BOARD_AVACSSMARTTV     32
183 #define SAA7134_BOARD_AVERMEDIA_DVD_EZMAKER 33
184 #define SAA7134_BOARD_NOVAC_PRIMETV7133 34
185 #define SAA7134_BOARD_AVERMEDIA_STUDIO_305 35
186 #define SAA7134_BOARD_UPMOST_PURPLE_TV 36
187 #define SAA7134_BOARD_ITEMS_MTV005     37
188 #define SAA7134_BOARD_CINERGY200       38
189 #define SAA7134_BOARD_FLYTVPLATINUM_MINI 39
190 #define SAA7134_BOARD_VIDEOMATE_TV_PVR 40
191 #define SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUS 41
192 #define SAA7134_BOARD_SABRENT_SBTTVFM  42
193 #define SAA7134_BOARD_ZOLID_XPERT_TV7134 43
194 #define SAA7134_BOARD_EMPIRE_PCI_TV_RADIO_LE 44
195 #define SAA7134_BOARD_AVERMEDIA_STUDIO_307    45
196 #define SAA7134_BOARD_AVERMEDIA_CARDBUS 46
197 #define SAA7134_BOARD_CINERGY400_CARDBUS 47
198 #define SAA7134_BOARD_CINERGY600_MK3   48
199 #define SAA7134_BOARD_VIDEOMATE_GOLD_PLUS 49
200 #define SAA7134_BOARD_PINNACLE_300I_DVBT_PAL 50
201 #define SAA7134_BOARD_PROVIDEO_PV952   51
202 #define SAA7134_BOARD_AVERMEDIA_305    52
203 #define SAA7134_BOARD_ASUSTeK_TVFM7135 53
204 #define SAA7134_BOARD_FLYTVPLATINUM_FM 54
205 #define SAA7134_BOARD_FLYDVBTDUO 55
206 #define SAA7134_BOARD_AVERMEDIA_307    56
207 #define SAA7134_BOARD_AVERMEDIA_GO_007_FM 57
208 #define SAA7134_BOARD_ADS_INSTANT_TV 58
209 #define SAA7134_BOARD_KWORLD_VSTREAM_XPERT 59
210 #define SAA7134_BOARD_FLYDVBT_DUO_CARDBUS 60
211 #define SAA7134_BOARD_PHILIPS_TOUGH 61
212 #define SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII 62
213 #define SAA7134_BOARD_KWORLD_XPERT 63
214 #define SAA7134_BOARD_FLYTV_DIGIMATRIX 64
215 #define SAA7134_BOARD_KWORLD_TERMINATOR 65
216 #define SAA7134_BOARD_YUAN_TUN900 66
217 #define SAA7134_BOARD_BEHOLD_409FM 67
218 #define SAA7134_BOARD_GOTVIEW_7135 68
219 #define SAA7134_BOARD_PHILIPS_EUROPA  69
220 #define SAA7134_BOARD_VIDEOMATE_DVBT_300 70
221 #define SAA7134_BOARD_VIDEOMATE_DVBT_200 71
222 #define SAA7134_BOARD_RTD_VFG7350 72
223 #define SAA7134_BOARD_RTD_VFG7330 73
224 #define SAA7134_BOARD_FLYTVPLATINUM_MINI2 74
225 #define SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180 75
226 #define SAA7134_BOARD_MONSTERTV_MOBILE 76
227 #define SAA7134_BOARD_PINNACLE_PCTV_110i 77
228 #define SAA7134_BOARD_ASUSTeK_P7131_DUAL 78
229 #define SAA7134_BOARD_SEDNA_PC_TV_CARDBUS     79
230 #define SAA7134_BOARD_ASUSTEK_DIGIMATRIX_TV 80
231 #define SAA7134_BOARD_PHILIPS_TIGER  81
232 #define SAA7134_BOARD_MSI_TVATANYWHERE_PLUS  82
233 #define SAA7134_BOARD_CINERGY250PCI 83
234 #define SAA7134_BOARD_FLYDVB_TRIO 84
235 #define SAA7134_BOARD_AVERMEDIA_777 85
236 #define SAA7134_BOARD_FLYDVBT_LR301 86
237 #define SAA7134_BOARD_ADS_DUO_CARDBUS_PTV331 87
238 #define SAA7134_BOARD_TEVION_DVBT_220RF 88
239 #define SAA7134_BOARD_ELSA_700TV       89
240 #define SAA7134_BOARD_KWORLD_ATSC110   90
241 #define SAA7134_BOARD_AVERMEDIA_A169_B 91
242 #define SAA7134_BOARD_AVERMEDIA_A169_B1 92
243 #define SAA7134_BOARD_MD7134_BRIDGE_2     93
244 #define SAA7134_BOARD_FLYDVBT_HYBRID_CARDBUS 94
245 #define SAA7134_BOARD_FLYVIDEO3000_NTSC 95
246 #define SAA7134_BOARD_MEDION_MD8800_QUADRO 96
247 #define SAA7134_BOARD_FLYDVBS_LR300 97
248 #define SAA7134_BOARD_PROTEUS_2309 98
249 #define SAA7134_BOARD_AVERMEDIA_A16AR   99
250 #define SAA7134_BOARD_ASUS_EUROPA2_HYBRID 100
251 #define SAA7134_BOARD_PINNACLE_PCTV_310i  101
252 #define SAA7134_BOARD_AVERMEDIA_STUDIO_507 102
253 #define SAA7134_BOARD_VIDEOMATE_DVBT_200A  103
254 #define SAA7134_BOARD_HAUPPAUGE_HVR1110    104
255 #define SAA7134_BOARD_CINERGY_HT_PCMCIA    105
256 #define SAA7134_BOARD_ENCORE_ENLTV         106
257 #define SAA7134_BOARD_ENCORE_ENLTV_FM      107
258 #define SAA7134_BOARD_CINERGY_HT_PCI       108
259 #define SAA7134_BOARD_PHILIPS_TIGER_S      109
260 #define SAA7134_BOARD_AVERMEDIA_M102	   110
261 #define SAA7134_BOARD_ASUS_P7131_4871	   111
262 #define SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA 112
263 #define SAA7134_BOARD_ECS_TVP3XP_4CB6  113
264 #define SAA7134_BOARD_KWORLD_DVBT_210 114
265 #define SAA7134_BOARD_SABRENT_TV_PCB05     115
266 #define SAA7134_BOARD_10MOONSTVMASTER3     116
267 #define SAA7134_BOARD_AVERMEDIA_SUPER_007  117
268 #define SAA7134_BOARD_BEHOLD_401  	118
269 #define SAA7134_BOARD_BEHOLD_403  	119
270 #define SAA7134_BOARD_BEHOLD_403FM	120
271 #define SAA7134_BOARD_BEHOLD_405	121
272 #define SAA7134_BOARD_BEHOLD_405FM	122
273 #define SAA7134_BOARD_BEHOLD_407	123
274 #define SAA7134_BOARD_BEHOLD_407FM	124
275 #define SAA7134_BOARD_BEHOLD_409	125
276 #define SAA7134_BOARD_BEHOLD_505FM	126
277 #define SAA7134_BOARD_BEHOLD_507_9FM	127
278 #define SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM 128
279 #define SAA7134_BOARD_BEHOLD_607FM_MK3	129
280 #define SAA7134_BOARD_BEHOLD_M6		130
281 #define SAA7134_BOARD_TWINHAN_DTV_DVB_3056 131
282 #define SAA7134_BOARD_GENIUS_TVGO_A11MCE   132
283 #define SAA7134_BOARD_PHILIPS_SNAKE        133
284 #define SAA7134_BOARD_CREATIX_CTX953       134
285 #define SAA7134_BOARD_MSI_TVANYWHERE_AD11  135
286 #define SAA7134_BOARD_AVERMEDIA_CARDBUS_506 136
287 #define SAA7134_BOARD_AVERMEDIA_A16D       137
288 #define SAA7134_BOARD_AVERMEDIA_M115       138
289 #define SAA7134_BOARD_VIDEOMATE_T750       139
290 #define SAA7134_BOARD_AVERMEDIA_A700_PRO    140
291 #define SAA7134_BOARD_AVERMEDIA_A700_HYBRID 141
292 #define SAA7134_BOARD_BEHOLD_H6      142
293 #define SAA7134_BOARD_BEHOLD_M63      143
294 #define SAA7134_BOARD_BEHOLD_M6_EXTRA    144
295 #define SAA7134_BOARD_AVERMEDIA_M103    145
296 #define SAA7134_BOARD_ASUSTeK_P7131_ANALOG 146
297 #define SAA7134_BOARD_ASUSTeK_TIGER_3IN1   147
298 #define SAA7134_BOARD_ENCORE_ENLTV_FM53 148
299 #define SAA7134_BOARD_AVERMEDIA_M135A    149
300 #define SAA7134_BOARD_REAL_ANGEL_220     150
301 #define SAA7134_BOARD_ADS_INSTANT_HDTV_PCI  151
302 #define SAA7134_BOARD_ASUSTeK_TIGER         152
303 #define SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG 153
304 #define SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS 154
305 #define SAA7134_BOARD_HAUPPAUGE_HVR1150     155
306 #define SAA7134_BOARD_HAUPPAUGE_HVR1120   156
307 #define SAA7134_BOARD_AVERMEDIA_STUDIO_507UA 157
308 #define SAA7134_BOARD_AVERMEDIA_CARDBUS_501 158
309 #define SAA7134_BOARD_BEHOLD_505RDS_MK5     159
310 #define SAA7134_BOARD_BEHOLD_507RDS_MK3     160
311 #define SAA7134_BOARD_BEHOLD_507RDS_MK5     161
312 #define SAA7134_BOARD_BEHOLD_607FM_MK5      162
313 #define SAA7134_BOARD_BEHOLD_609FM_MK3      163
314 #define SAA7134_BOARD_BEHOLD_609FM_MK5      164
315 #define SAA7134_BOARD_BEHOLD_607RDS_MK3     165
316 #define SAA7134_BOARD_BEHOLD_607RDS_MK5     166
317 #define SAA7134_BOARD_BEHOLD_609RDS_MK3     167
318 #define SAA7134_BOARD_BEHOLD_609RDS_MK5     168
319 #define SAA7134_BOARD_VIDEOMATE_S350        169
320 #define SAA7134_BOARD_AVERMEDIA_STUDIO_505  170
321 #define SAA7134_BOARD_BEHOLD_X7             171
322 #define SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM 172
323 #define SAA7134_BOARD_ZOLID_HYBRID_PCI		173
324 #define SAA7134_BOARD_ASUS_EUROPA_HYBRID	174
325 #define SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S 175
326 #define SAA7134_BOARD_BEHOLD_505RDS_MK3     176
327 #define SAA7134_BOARD_HAWELL_HW_404M7		177
328 #define SAA7134_BOARD_BEHOLD_H7             178
329 #define SAA7134_BOARD_BEHOLD_A7             179
330 #define SAA7134_BOARD_AVERMEDIA_M733A       180
331 #define SAA7134_BOARD_TECHNOTREND_BUDGET_T3000 181
332 #define SAA7134_BOARD_KWORLD_PCI_SBTVD_FULLSEG 182
333 #define SAA7134_BOARD_VIDEOMATE_M1F         183
334 #define SAA7134_BOARD_ENCORE_ENLTV_FM3      184
335 #define SAA7134_BOARD_MAGICPRO_PROHDTV_PRO2 185
336 #define SAA7134_BOARD_BEHOLD_501            186
337 #define SAA7134_BOARD_BEHOLD_503FM          187
338 #define SAA7134_BOARD_SENSORAY811_911       188
339 #define SAA7134_BOARD_KWORLD_PC150U         189
340 #define SAA7134_BOARD_ASUSTeK_PS3_100      190
341 #define SAA7134_BOARD_HAWELL_HW_9004V1      191
342 #define SAA7134_BOARD_AVERMEDIA_A706		192
343 #define SAA7134_BOARD_WIS_VOYAGER           193
344 #define SAA7134_BOARD_AVERMEDIA_505         194
345 #define SAA7134_BOARD_LEADTEK_WINFAST_TV2100_FM 195
346 #define SAA7134_BOARD_SNAZIO_TVPVR_PRO      196
347 
348 #define SAA7134_MAXBOARDS 32
349 #define SAA7134_INPUT_MAX 8
350 
351 /* ----------------------------------------------------------- */
352 /* Since we support 2 remote types, lets tell them apart       */
353 
354 #define SAA7134_REMOTE_GPIO  1
355 #define SAA7134_REMOTE_I2C   2
356 
357 /* ----------------------------------------------------------- */
358 /* Video Output Port Register Initialization Options           */
359 
360 #define SET_T_CODE_POLARITY_NON_INVERTED	(1 << 0)
361 #define SET_CLOCK_NOT_DELAYED			(1 << 1)
362 #define SET_CLOCK_INVERTED			(1 << 2)
363 #define SET_VSYNC_OFF				(1 << 3)
364 
365 enum saa7134_input_types {
366 	SAA7134_NO_INPUT = 0,
367 	SAA7134_INPUT_MUTE,
368 	SAA7134_INPUT_RADIO,
369 	SAA7134_INPUT_TV,
370 	SAA7134_INPUT_TV_MONO,
371 	SAA7134_INPUT_COMPOSITE,
372 	SAA7134_INPUT_COMPOSITE0,
373 	SAA7134_INPUT_COMPOSITE1,
374 	SAA7134_INPUT_COMPOSITE2,
375 	SAA7134_INPUT_COMPOSITE3,
376 	SAA7134_INPUT_COMPOSITE4,
377 	SAA7134_INPUT_SVIDEO,
378 	SAA7134_INPUT_SVIDEO0,
379 	SAA7134_INPUT_SVIDEO1,
380 	SAA7134_INPUT_COMPOSITE_OVER_SVIDEO,
381 };
382 
383 struct saa7134_input {
384 	enum saa7134_input_types type;
385 	unsigned int             vmux;
386 	enum saa7134_audio_in    amux;
387 	unsigned int             gpio;
388 };
389 
390 enum saa7134_mpeg_type {
391 	SAA7134_MPEG_UNUSED,
392 	SAA7134_MPEG_EMPRESS,
393 	SAA7134_MPEG_DVB,
394 	SAA7134_MPEG_GO7007,
395 };
396 
397 enum saa7134_mpeg_ts_type {
398 	SAA7134_MPEG_TS_PARALLEL = 0,
399 	SAA7134_MPEG_TS_SERIAL,
400 };
401 
402 struct saa7134_board {
403 	char                    *name;
404 	unsigned int            audio_clock;
405 
406 	/* input switching */
407 	unsigned int            gpiomask;
408 	struct saa7134_input    inputs[SAA7134_INPUT_MAX];
409 	struct saa7134_input    radio;
410 	struct saa7134_input    mute;
411 
412 	/* i2c chip info */
413 	unsigned int            tuner_type;
414 	unsigned int		radio_type;
415 	unsigned char		tuner_addr;
416 	unsigned char		radio_addr;
417 	unsigned char		empress_addr;
418 	unsigned char		rds_addr;
419 
420 	unsigned int            tda9887_conf;
421 	struct tda829x_config   tda829x_conf;
422 
423 	/* peripheral I/O */
424 	enum saa7134_video_out  video_out;
425 	enum saa7134_mpeg_type  mpeg;
426 	enum saa7134_mpeg_ts_type ts_type;
427 	unsigned int            vid_port_opts;
428 	unsigned int            ts_force_val:1;
429 };
430 
431 #define card_has_radio(dev)   (SAA7134_NO_INPUT != saa7134_boards[dev->board].radio.type)
432 #define card_is_empress(dev)  (SAA7134_MPEG_EMPRESS == saa7134_boards[dev->board].mpeg)
433 #define card_is_dvb(dev)      (SAA7134_MPEG_DVB     == saa7134_boards[dev->board].mpeg)
434 #define card_is_go7007(dev)   (SAA7134_MPEG_GO7007  == saa7134_boards[dev->board].mpeg)
435 #define card_has_mpeg(dev)    (SAA7134_MPEG_UNUSED  != saa7134_boards[dev->board].mpeg)
436 #define card(dev)             (saa7134_boards[dev->board])
437 #define card_in(dev,n)        (saa7134_boards[dev->board].inputs[n])
438 
439 #define V4L2_CID_PRIVATE_INVERT      (V4L2_CID_USER_SAA7134_BASE + 0)
440 #define V4L2_CID_PRIVATE_Y_ODD       (V4L2_CID_USER_SAA7134_BASE + 1)
441 #define V4L2_CID_PRIVATE_Y_EVEN      (V4L2_CID_USER_SAA7134_BASE + 2)
442 #define V4L2_CID_PRIVATE_AUTOMUTE    (V4L2_CID_USER_SAA7134_BASE + 3)
443 
444 /* ----------------------------------------------------------- */
445 /* device / file handle status                                 */
446 
447 #define RESOURCE_OVERLAY       1
448 #define RESOURCE_VIDEO         2
449 #define RESOURCE_VBI           4
450 #define RESOURCE_EMPRESS       8
451 
452 #define INTERLACE_AUTO         0
453 #define INTERLACE_ON           1
454 #define INTERLACE_OFF          2
455 
456 #define BUFFER_TIMEOUT     msecs_to_jiffies(500)  /* 0.5 seconds */
457 #define TS_BUFFER_TIMEOUT  msecs_to_jiffies(1000)  /* 1 second */
458 
459 struct saa7134_dev;
460 struct saa7134_dma;
461 
462 /* saa7134 page table */
463 struct saa7134_pgtable {
464 	unsigned int               size;
465 	__le32                     *cpu;
466 	dma_addr_t                 dma;
467 };
468 
469 /* tvaudio thread status */
470 struct saa7134_thread {
471 	struct task_struct         *thread;
472 	unsigned int               scan1;
473 	unsigned int               scan2;
474 	unsigned int               mode;
475 	unsigned int		   stopped;
476 };
477 
478 /* buffer for one video/vbi/ts frame */
479 struct saa7134_buf {
480 	/* common v4l buffer stuff -- must be first */
481 	struct vb2_v4l2_buffer vb2;
482 
483 	/* saa7134 specific */
484 	unsigned int            top_seen;
485 	int (*activate)(struct saa7134_dev *dev,
486 			struct saa7134_buf *buf,
487 			struct saa7134_buf *next);
488 
489 	struct list_head	entry;
490 };
491 
492 struct saa7134_dmaqueue {
493 	struct saa7134_dev         *dev;
494 	struct saa7134_buf         *curr;
495 	struct list_head           queue;
496 	struct timer_list          timeout;
497 	unsigned int               need_two;
498 	unsigned int               seq_nr;
499 	struct saa7134_pgtable     pt;
500 };
501 
502 /* dmasound dsp status */
503 struct saa7134_dmasound {
504 	struct mutex               lock;
505 	int                        minor_mixer;
506 	int                        minor_dsp;
507 	unsigned int               users_dsp;
508 
509 	/* mixer */
510 	enum saa7134_audio_in      input;
511 	unsigned int               count;
512 	unsigned int               line1;
513 	unsigned int               line2;
514 
515 	/* dsp */
516 	unsigned int               afmt;
517 	unsigned int               rate;
518 	unsigned int               channels;
519 	unsigned int               recording_on;
520 	unsigned int               dma_running;
521 	unsigned int               blocks;
522 	unsigned int               blksize;
523 	unsigned int               bufsize;
524 	struct saa7134_pgtable     pt;
525 	void			   *vaddr;
526 	struct scatterlist	   *sglist;
527 	int                        sglen;
528 	int                        nr_pages;
529 	unsigned int               dma_blk;
530 	unsigned int               read_offset;
531 	unsigned int               read_count;
532 	void *			   priv_data;
533 	struct snd_pcm_substream   *substream;
534 };
535 
536 /* ts/mpeg status */
537 struct saa7134_ts {
538 	/* TS capture */
539 	int                        nr_packets;
540 	int                        nr_bufs;
541 };
542 
543 /* ts/mpeg ops */
544 struct saa7134_mpeg_ops {
545 	enum saa7134_mpeg_type     type;
546 	struct list_head           next;
547 	int                        (*init)(struct saa7134_dev *dev);
548 	int                        (*fini)(struct saa7134_dev *dev);
549 	void                       (*signal_change)(struct saa7134_dev *dev);
550 	void                       (*irq_ts_done)(struct saa7134_dev *dev,
551 						  unsigned long status);
552 };
553 
554 /* global device status */
555 struct saa7134_dev {
556 	struct list_head           devlist;
557 	struct mutex               lock;
558 	spinlock_t                 slock;
559 	struct v4l2_device         v4l2_dev;
560 	/* workstruct for loading modules */
561 	struct work_struct request_module_wk;
562 
563 	/* insmod option/autodetected */
564 	int                        autodetected;
565 
566 	/* various device info */
567 	unsigned int               resources;
568 	struct video_device        *video_dev;
569 	struct video_device        *radio_dev;
570 	struct video_device        *vbi_dev;
571 	struct saa7134_dmasound    dmasound;
572 
573 	/* infrared remote */
574 	int                        has_remote;
575 	struct saa7134_card_ir     *remote;
576 
577 	/* pci i/o */
578 	char                       name[32];
579 	int                        nr;
580 	struct pci_dev             *pci;
581 	unsigned char              pci_rev,pci_lat;
582 	__u32                      __iomem *lmmio;
583 	__u8                       __iomem *bmmio;
584 
585 	/* config info */
586 	unsigned int               board;
587 	unsigned int               tuner_type;
588 	unsigned int 		   radio_type;
589 	unsigned char		   tuner_addr;
590 	unsigned char		   radio_addr;
591 
592 	unsigned int               tda9887_conf;
593 	unsigned int               gpio_value;
594 
595 	/* i2c i/o */
596 	struct i2c_adapter         i2c_adap;
597 	struct i2c_client          i2c_client;
598 	unsigned char              eedata[256];
599 	int 			   has_rds;
600 
601 	/* video overlay */
602 	struct v4l2_framebuffer    ovbuf;
603 	struct saa7134_format      *ovfmt;
604 	unsigned int               ovenable;
605 	enum v4l2_field            ovfield;
606 	struct v4l2_window         win;
607 	struct v4l2_clip           clips[8];
608 	unsigned int               nclips;
609 	struct v4l2_fh		   *overlay_owner;
610 
611 
612 	/* video+ts+vbi capture */
613 	void			   *alloc_ctx;
614 	struct saa7134_dmaqueue    video_q;
615 	struct vb2_queue           video_vbq;
616 	struct saa7134_dmaqueue    vbi_q;
617 	struct vb2_queue           vbi_vbq;
618 	enum v4l2_field		   field;
619 	struct saa7134_format      *fmt;
620 	unsigned int               width, height;
621 	unsigned int               vbi_hlen, vbi_vlen;
622 	struct pm_qos_request	   qos_request;
623 
624 	/* SAA7134_MPEG_* */
625 	struct saa7134_ts          ts;
626 	struct saa7134_dmaqueue    ts_q;
627 	enum v4l2_field		   ts_field;
628 	int                        ts_started;
629 	struct saa7134_mpeg_ops    *mops;
630 
631 	/* SAA7134_MPEG_EMPRESS only */
632 	struct video_device        *empress_dev;
633 	struct v4l2_subdev	   *empress_sd;
634 	struct vb2_queue           empress_vbq;
635 	struct work_struct         empress_workqueue;
636 	int                        empress_started;
637 	struct v4l2_ctrl_handler   empress_ctrl_handler;
638 
639 	/* various v4l controls */
640 	struct saa7134_tvnorm      *tvnorm;              /* video */
641 	struct saa7134_tvaudio     *tvaudio;
642 	struct v4l2_ctrl_handler   ctrl_handler;
643 	unsigned int               ctl_input;
644 	int                        ctl_bright;
645 	int                        ctl_contrast;
646 	int                        ctl_hue;
647 	int                        ctl_saturation;
648 	int                        ctl_mute;             /* audio */
649 	int                        ctl_volume;
650 	int                        ctl_invert;           /* private */
651 	int                        ctl_mirror;
652 	int                        ctl_y_odd;
653 	int                        ctl_y_even;
654 	int                        ctl_automute;
655 
656 	/* crop */
657 	struct v4l2_rect           crop_bounds;
658 	struct v4l2_rect           crop_defrect;
659 	struct v4l2_rect           crop_current;
660 
661 	/* other global state info */
662 	unsigned int               automute;
663 	struct saa7134_thread      thread;
664 	struct saa7134_input       *input;
665 	struct saa7134_input       *hw_input;
666 	unsigned int               hw_mute;
667 	int                        last_carrier;
668 	int                        nosignal;
669 	unsigned int               insuspend;
670 	struct v4l2_ctrl_handler   radio_ctrl_handler;
671 
672 	/* I2C keyboard data */
673 	struct IR_i2c_init_data    init_data;
674 
675 #ifdef CONFIG_MEDIA_CONTROLLER
676 	struct media_device *media_dev;
677 
678 	struct media_entity input_ent[SAA7134_INPUT_MAX + 1];
679 	struct media_pad input_pad[SAA7134_INPUT_MAX + 1];
680 
681 	struct media_entity demod;
682 	struct media_pad demod_pad[DEMOD_NUM_PADS];
683 
684 	struct media_pad video_pad, vbi_pad;
685 	struct media_entity *decoder;
686 #endif
687 
688 #if IS_ENABLED(CONFIG_VIDEO_SAA7134_DVB)
689 	/* SAA7134_MPEG_DVB only */
690 	struct vb2_dvb_frontends frontends;
691 	int (*original_demod_sleep)(struct dvb_frontend *fe);
692 	int (*original_set_voltage)(struct dvb_frontend *fe,
693 				    enum fe_sec_voltage voltage);
694 	int (*original_set_high_voltage)(struct dvb_frontend *fe, long arg);
695 #endif
696 	void (*gate_ctrl)(struct saa7134_dev *dev, int open);
697 };
698 
699 /* ----------------------------------------------------------- */
700 
701 #define saa_readl(reg)             readl(dev->lmmio + (reg))
702 #define saa_writel(reg,value)      writel((value), dev->lmmio + (reg));
703 #define saa_andorl(reg,mask,value) \
704   writel((readl(dev->lmmio+(reg)) & ~(mask)) |\
705   ((value) & (mask)), dev->lmmio+(reg))
706 #define saa_setl(reg,bit)          saa_andorl((reg),(bit),(bit))
707 #define saa_clearl(reg,bit)        saa_andorl((reg),(bit),0)
708 
709 #define saa_readb(reg)             readb(dev->bmmio + (reg))
710 #define saa_writeb(reg,value)      writeb((value), dev->bmmio + (reg));
711 #define saa_andorb(reg,mask,value) \
712   writeb((readb(dev->bmmio+(reg)) & ~(mask)) |\
713   ((value) & (mask)), dev->bmmio+(reg))
714 #define saa_setb(reg,bit)          saa_andorb((reg),(bit),(bit))
715 #define saa_clearb(reg,bit)        saa_andorb((reg),(bit),0)
716 
717 #define saa_wait(us) { udelay(us); }
718 
719 #define SAA7134_NORMS	(\
720 		V4L2_STD_PAL    | V4L2_STD_PAL_N | \
721 		V4L2_STD_PAL_Nc | V4L2_STD_SECAM | \
722 		V4L2_STD_NTSC   | V4L2_STD_PAL_M | \
723 		V4L2_STD_PAL_60)
724 
725 #define GRP_EMPRESS (1)
726 #define saa_call_all(dev, o, f, args...) do {				\
727 	if (dev->gate_ctrl)						\
728 		dev->gate_ctrl(dev, 1);					\
729 	v4l2_device_call_all(&(dev)->v4l2_dev, 0, o, f , ##args);	\
730 	if (dev->gate_ctrl)						\
731 		dev->gate_ctrl(dev, 0);					\
732 } while (0)
733 
734 #define saa_call_empress(dev, o, f, args...) ({				\
735 	long _rc;							\
736 	if (dev->gate_ctrl)						\
737 		dev->gate_ctrl(dev, 1);					\
738 	_rc = v4l2_device_call_until_err(&(dev)->v4l2_dev,		\
739 					 GRP_EMPRESS, o, f , ##args);	\
740 	if (dev->gate_ctrl)						\
741 		dev->gate_ctrl(dev, 0);					\
742 	_rc;								\
743 })
744 
745 static inline bool is_empress(struct file *file)
746 {
747 	struct video_device *vdev = video_devdata(file);
748 	struct saa7134_dev *dev = video_get_drvdata(vdev);
749 
750 	return vdev->queue == &dev->empress_vbq;
751 }
752 
753 /* ----------------------------------------------------------- */
754 /* saa7134-core.c                                              */
755 
756 extern struct list_head  saa7134_devlist;
757 extern struct mutex saa7134_devlist_lock;
758 extern int saa7134_no_overlay;
759 extern bool saa7134_userptr;
760 
761 void saa7134_track_gpio(struct saa7134_dev *dev, const char *msg);
762 void saa7134_set_gpio(struct saa7134_dev *dev, int bit_no, int value);
763 
764 #define SAA7134_PGTABLE_SIZE 4096
765 
766 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt);
767 int  saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
768 			   struct scatterlist *list, unsigned int length,
769 			   unsigned int startpage);
770 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt);
771 
772 int saa7134_buffer_count(unsigned int size, unsigned int count);
773 int saa7134_buffer_startpage(struct saa7134_buf *buf);
774 unsigned long saa7134_buffer_base(struct saa7134_buf *buf);
775 
776 int saa7134_buffer_queue(struct saa7134_dev *dev, struct saa7134_dmaqueue *q,
777 			 struct saa7134_buf *buf);
778 void saa7134_buffer_finish(struct saa7134_dev *dev, struct saa7134_dmaqueue *q,
779 			   unsigned int state);
780 void saa7134_buffer_next(struct saa7134_dev *dev, struct saa7134_dmaqueue *q);
781 void saa7134_buffer_timeout(unsigned long data);
782 void saa7134_stop_streaming(struct saa7134_dev *dev, struct saa7134_dmaqueue *q);
783 
784 int saa7134_set_dmabits(struct saa7134_dev *dev);
785 
786 extern int (*saa7134_dmasound_init)(struct saa7134_dev *dev);
787 extern int (*saa7134_dmasound_exit)(struct saa7134_dev *dev);
788 
789 
790 /* ----------------------------------------------------------- */
791 /* saa7134-cards.c                                             */
792 
793 extern struct saa7134_board saa7134_boards[];
794 extern const char * const saa7134_input_name[];
795 extern const unsigned int saa7134_bcount;
796 extern struct pci_device_id saa7134_pci_tbl[];
797 
798 extern int saa7134_board_init1(struct saa7134_dev *dev);
799 extern int saa7134_board_init2(struct saa7134_dev *dev);
800 int saa7134_tuner_callback(void *priv, int component, int command, int arg);
801 
802 
803 /* ----------------------------------------------------------- */
804 /* saa7134-i2c.c                                               */
805 
806 int saa7134_i2c_register(struct saa7134_dev *dev);
807 int saa7134_i2c_unregister(struct saa7134_dev *dev);
808 
809 
810 /* ----------------------------------------------------------- */
811 /* saa7134-video.c                                             */
812 
813 extern unsigned int video_debug;
814 extern struct video_device saa7134_video_template;
815 extern struct video_device saa7134_radio_template;
816 
817 void saa7134_vb2_buffer_queue(struct vb2_buffer *vb);
818 int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count);
819 void saa7134_vb2_stop_streaming(struct vb2_queue *vq);
820 
821 int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id);
822 int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id);
823 int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std);
824 int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i);
825 int saa7134_g_input(struct file *file, void *priv, unsigned int *i);
826 int saa7134_s_input(struct file *file, void *priv, unsigned int i);
827 int saa7134_querycap(struct file *file, void  *priv,
828 					struct v4l2_capability *cap);
829 int saa7134_g_tuner(struct file *file, void *priv,
830 					struct v4l2_tuner *t);
831 int saa7134_s_tuner(struct file *file, void *priv,
832 					const struct v4l2_tuner *t);
833 int saa7134_g_frequency(struct file *file, void *priv,
834 					struct v4l2_frequency *f);
835 int saa7134_s_frequency(struct file *file, void *priv,
836 					const struct v4l2_frequency *f);
837 
838 int saa7134_videoport_init(struct saa7134_dev *dev);
839 void saa7134_set_tvnorm_hw(struct saa7134_dev *dev);
840 
841 int saa7134_video_init1(struct saa7134_dev *dev);
842 int saa7134_video_init2(struct saa7134_dev *dev);
843 void saa7134_irq_video_signalchange(struct saa7134_dev *dev);
844 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status);
845 void saa7134_video_fini(struct saa7134_dev *dev);
846 
847 
848 /* ----------------------------------------------------------- */
849 /* saa7134-ts.c                                                */
850 
851 #define TS_PACKET_SIZE 188 /* TS packets 188 bytes */
852 
853 int saa7134_ts_buffer_init(struct vb2_buffer *vb2);
854 int saa7134_ts_buffer_prepare(struct vb2_buffer *vb2);
855 int saa7134_ts_queue_setup(struct vb2_queue *q,
856 			   unsigned int *nbuffers, unsigned int *nplanes,
857 			   unsigned int sizes[], void *alloc_ctxs[]);
858 int saa7134_ts_start_streaming(struct vb2_queue *vq, unsigned int count);
859 void saa7134_ts_stop_streaming(struct vb2_queue *vq);
860 
861 extern struct vb2_ops saa7134_ts_qops;
862 
863 int saa7134_ts_init1(struct saa7134_dev *dev);
864 int saa7134_ts_fini(struct saa7134_dev *dev);
865 void saa7134_irq_ts_done(struct saa7134_dev *dev, unsigned long status);
866 
867 int saa7134_ts_register(struct saa7134_mpeg_ops *ops);
868 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops);
869 
870 int saa7134_ts_init_hw(struct saa7134_dev *dev);
871 
872 int saa7134_ts_start(struct saa7134_dev *dev);
873 int saa7134_ts_stop(struct saa7134_dev *dev);
874 
875 /* ----------------------------------------------------------- */
876 /* saa7134-vbi.c                                               */
877 
878 extern struct vb2_ops saa7134_vbi_qops;
879 extern struct video_device saa7134_vbi_template;
880 
881 int saa7134_vbi_init1(struct saa7134_dev *dev);
882 int saa7134_vbi_fini(struct saa7134_dev *dev);
883 void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status);
884 
885 
886 /* ----------------------------------------------------------- */
887 /* saa7134-tvaudio.c                                           */
888 
889 int saa7134_tvaudio_rx2mode(u32 rx);
890 
891 void saa7134_tvaudio_setmute(struct saa7134_dev *dev);
892 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
893 			      struct saa7134_input *in);
894 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level);
895 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev);
896 
897 void saa7134_tvaudio_init(struct saa7134_dev *dev);
898 int saa7134_tvaudio_init2(struct saa7134_dev *dev);
899 int saa7134_tvaudio_fini(struct saa7134_dev *dev);
900 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev);
901 int saa7134_tvaudio_close(struct saa7134_dev *dev);
902 
903 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value);
904 
905 void saa7134_enable_i2s(struct saa7134_dev *dev);
906 
907 /* ----------------------------------------------------------- */
908 /* saa7134-oss.c                                               */
909 
910 extern const struct file_operations saa7134_dsp_fops;
911 extern const struct file_operations saa7134_mixer_fops;
912 
913 int saa7134_oss_init1(struct saa7134_dev *dev);
914 int saa7134_oss_fini(struct saa7134_dev *dev);
915 void saa7134_irq_oss_done(struct saa7134_dev *dev, unsigned long status);
916 
917 /* ----------------------------------------------------------- */
918 /* saa7134-input.c                                             */
919 
920 #if defined(CONFIG_VIDEO_SAA7134_RC)
921 int  saa7134_input_init1(struct saa7134_dev *dev);
922 void saa7134_input_fini(struct saa7134_dev *dev);
923 void saa7134_input_irq(struct saa7134_dev *dev);
924 void saa7134_probe_i2c_ir(struct saa7134_dev *dev);
925 int saa7134_ir_start(struct saa7134_dev *dev);
926 void saa7134_ir_stop(struct saa7134_dev *dev);
927 #else
928 #define saa7134_input_init1(dev)	((void)0)
929 #define saa7134_input_fini(dev)		((void)0)
930 #define saa7134_input_irq(dev)		((void)0)
931 #define saa7134_probe_i2c_ir(dev)	((void)0)
932 #define saa7134_ir_start(dev)		((void)0)
933 #define saa7134_ir_stop(dev)		((void)0)
934 #endif
935