1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * driver for the SAA7146 based AV110 cards (like the Fujitsu-Siemens DVB)
4  * av7110.c: initialization and demux stuff
5  *
6  * Copyright (C) 1999-2002 Ralph  Metzler
7  *                       & Marcus Metzler for convergence integrated media GmbH
8  *
9  * originally based on code by:
10  * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
11  *
12  * the project's page is at https://linuxtv.org
13  */
14 
15 
16 #include <linux/module.h>
17 #include <linux/kmod.h>
18 #include <linux/delay.h>
19 #include <linux/fs.h>
20 #include <linux/timer.h>
21 #include <linux/poll.h>
22 
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/types.h>
26 #include <linux/fcntl.h>
27 #include <linux/interrupt.h>
28 #include <linux/string.h>
29 #include <linux/pci.h>
30 #include <linux/vmalloc.h>
31 #include <linux/firmware.h>
32 #include <linux/crc32.h>
33 #include <linux/i2c.h>
34 #include <linux/kthread.h>
35 #include <linux/slab.h>
36 #include <asm/unaligned.h>
37 #include <asm/byteorder.h>
38 
39 
40 #include <linux/dvb/frontend.h>
41 
42 #include <media/dvb_frontend.h>
43 
44 #include "ttpci-eeprom.h"
45 #include "av7110.h"
46 #include "av7110_hw.h"
47 #include "av7110_av.h"
48 #include "av7110_ca.h"
49 #include "av7110_ipack.h"
50 
51 #include "bsbe1.h"
52 #include "lnbp21.h"
53 #include "bsru6.h"
54 
55 #define TS_WIDTH  376
56 #define TS_HEIGHT 512
57 #define TS_BUFLEN (TS_WIDTH*TS_HEIGHT)
58 #define TS_MAX_PACKETS (TS_BUFLEN/TS_SIZE)
59 
60 
61 int av7110_debug;
62 
63 static int vidmode = CVBS_RGB_OUT;
64 static int pids_off;
65 static int adac = DVB_ADAC_TI;
66 static int hw_sections;
67 static int rgb_on;
68 static int volume = 255;
69 static int budgetpatch;
70 static int wss_cfg_4_3 = 0x4008;
71 static int wss_cfg_16_9 = 0x0007;
72 static int tv_standard;
73 static int full_ts;
74 
75 module_param_named(debug, av7110_debug, int, 0644);
76 MODULE_PARM_DESC(debug, "debug level (bitmask, default 0)");
77 module_param(vidmode, int, 0444);
78 MODULE_PARM_DESC(vidmode,"analog video out: 0 off, 1 CVBS+RGB (default), 2 CVBS+YC, 3 YC");
79 module_param(pids_off, int, 0444);
80 MODULE_PARM_DESC(pids_off,"clear video/audio/PCR PID filters when demux is closed");
81 module_param(adac, int, 0444);
82 MODULE_PARM_DESC(adac,"audio DAC type: 0 TI, 1 CRYSTAL, 2 MSP (use if autodetection fails)");
83 module_param(hw_sections, int, 0444);
84 MODULE_PARM_DESC(hw_sections, "0 use software section filter, 1 use hardware");
85 module_param(rgb_on, int, 0444);
86 MODULE_PARM_DESC(rgb_on, "For Siemens DVB-C cards only: Enable RGB control signal on SCART pin 16 to switch SCART video mode from CVBS to RGB");
87 module_param(volume, int, 0444);
88 MODULE_PARM_DESC(volume, "initial volume: default 255 (range 0-255)");
89 module_param(budgetpatch, int, 0444);
90 MODULE_PARM_DESC(budgetpatch, "use budget-patch hardware modification: default 0 (0 no, 1 autodetect, 2 always)");
91 module_param(full_ts, int, 0444);
92 MODULE_PARM_DESC(full_ts, "enable code for full-ts hardware modification: 0 disable (default), 1 enable");
93 module_param(wss_cfg_4_3, int, 0444);
94 MODULE_PARM_DESC(wss_cfg_4_3, "WSS 4:3 - default 0x4008 - bit 15: disable, 14: burst mode, 13..0: wss data");
95 module_param(wss_cfg_16_9, int, 0444);
96 MODULE_PARM_DESC(wss_cfg_16_9, "WSS 16:9 - default 0x0007 - bit 15: disable, 14: burst mode, 13..0: wss data");
97 module_param(tv_standard, int, 0444);
98 MODULE_PARM_DESC(tv_standard, "TV standard: 0 PAL (default), 1 NTSC");
99 
100 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
101 
102 static void restart_feeds(struct av7110 *av7110);
103 static int budget_start_feed(struct dvb_demux_feed *feed);
104 static int budget_stop_feed(struct dvb_demux_feed *feed);
105 
106 static int av7110_num;
107 
108 #define FE_FUNC_OVERRIDE(fe_func, av7110_copy, av7110_func) \
109 {\
110 	if (fe_func != NULL) { \
111 		av7110_copy = fe_func; \
112 		fe_func = av7110_func; \
113 	} \
114 }
115 
116 
init_av7110_av(struct av7110 * av7110)117 static void init_av7110_av(struct av7110 *av7110)
118 {
119 	int ret;
120 	struct saa7146_dev *dev = av7110->dev;
121 
122 	/* set internal volume control to maximum */
123 	av7110->adac_type = DVB_ADAC_TI;
124 	ret = av7110_set_volume(av7110, av7110->mixer.volume_left, av7110->mixer.volume_right);
125 	if (ret < 0)
126 		printk("dvb-ttpci:cannot set internal volume to maximum:%d\n",ret);
127 
128 	ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetMonitorType,
129 			    1, (u16) av7110->display_ar);
130 	if (ret < 0)
131 		printk("dvb-ttpci: unable to set aspect ratio\n");
132 	ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetPanScanType,
133 			    1, av7110->display_panscan);
134 	if (ret < 0)
135 		printk("dvb-ttpci: unable to set pan scan\n");
136 
137 	ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 2, wss_cfg_4_3);
138 	if (ret < 0)
139 		printk("dvb-ttpci: unable to configure 4:3 wss\n");
140 	ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetWSSConfig, 2, 3, wss_cfg_16_9);
141 	if (ret < 0)
142 		printk("dvb-ttpci: unable to configure 16:9 wss\n");
143 
144 	ret = av7710_set_video_mode(av7110, vidmode);
145 	if (ret < 0)
146 		printk("dvb-ttpci:cannot set video mode:%d\n",ret);
147 
148 	/* handle different card types */
149 	/* remaining inits according to card and frontend type */
150 	av7110->analog_tuner_flags = 0;
151 	av7110->current_input = 0;
152 	if (dev->pci->subsystem_vendor == 0x13c2 && dev->pci->subsystem_device == 0x000a)
153 		av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, ADSwitch, 1, 0); // SPDIF on
154 	if (i2c_writereg(av7110, 0x20, 0x00, 0x00) == 1) {
155 		printk ("dvb-ttpci: Crystal audio DAC @ card %d detected\n",
156 			av7110->dvb_adapter.num);
157 		av7110->adac_type = DVB_ADAC_CRYSTAL;
158 		i2c_writereg(av7110, 0x20, 0x01, 0xd2);
159 		i2c_writereg(av7110, 0x20, 0x02, 0x49);
160 		i2c_writereg(av7110, 0x20, 0x03, 0x00);
161 		i2c_writereg(av7110, 0x20, 0x04, 0x00);
162 
163 		/**
164 		 * some special handling for the Siemens DVB-C cards...
165 		 */
166 	} else if (0 == av7110_init_analog_module(av7110)) {
167 		/* done. */
168 	}
169 	else if (dev->pci->subsystem_vendor == 0x110a) {
170 		printk("dvb-ttpci: DVB-C w/o analog module @ card %d detected\n",
171 			av7110->dvb_adapter.num);
172 		av7110->adac_type = DVB_ADAC_NONE;
173 	}
174 	else {
175 		av7110->adac_type = adac;
176 		printk("dvb-ttpci: adac type set to %d @ card %d\n",
177 			av7110->adac_type, av7110->dvb_adapter.num);
178 	}
179 
180 	if (av7110->adac_type == DVB_ADAC_NONE || av7110->adac_type == DVB_ADAC_MSP34x0) {
181 		// switch DVB SCART on
182 		ret = av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, MainSwitch, 1, 0);
183 		if (ret < 0)
184 			printk("dvb-ttpci:cannot switch on SCART(Main):%d\n",ret);
185 		ret = av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, ADSwitch, 1, 1);
186 		if (ret < 0)
187 			printk("dvb-ttpci:cannot switch on SCART(AD):%d\n",ret);
188 		if (rgb_on &&
189 		    ((av7110->dev->pci->subsystem_vendor == 0x110a) ||
190 		     (av7110->dev->pci->subsystem_vendor == 0x13c2)) &&
191 		     (av7110->dev->pci->subsystem_device == 0x0000)) {
192 			saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI); // RGB on, SCART pin 16
193 			//saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO); // SCARTpin 8
194 		}
195 	}
196 
197 	if (dev->pci->subsystem_vendor == 0x13c2 && dev->pci->subsystem_device == 0x000e)
198 		av7110_fw_cmd(av7110, COMTYPE_AUDIODAC, SpdifSwitch, 1, 0); // SPDIF on
199 
200 	ret = av7110_set_volume(av7110, av7110->mixer.volume_left, av7110->mixer.volume_right);
201 	if (ret < 0)
202 		printk("dvb-ttpci:cannot set volume :%d\n",ret);
203 }
204 
recover_arm(struct av7110 * av7110)205 static void recover_arm(struct av7110 *av7110)
206 {
207 	dprintk(4, "%p\n",av7110);
208 
209 	av7110_bootarm(av7110);
210 	msleep(100);
211 
212 	init_av7110_av(av7110);
213 
214 	/* card-specific recovery */
215 	if (av7110->recover)
216 		av7110->recover(av7110);
217 
218 	restart_feeds(av7110);
219 
220 #if IS_ENABLED(CONFIG_DVB_AV7110_IR)
221 	av7110_set_ir_config(av7110);
222 #endif
223 }
224 
av7110_arm_sync(struct av7110 * av7110)225 static void av7110_arm_sync(struct av7110 *av7110)
226 {
227 	if (av7110->arm_thread)
228 		kthread_stop(av7110->arm_thread);
229 
230 	av7110->arm_thread = NULL;
231 }
232 
arm_thread(void * data)233 static int arm_thread(void *data)
234 {
235 	struct av7110 *av7110 = data;
236 	u16 newloops = 0;
237 	int timeout;
238 
239 	dprintk(4, "%p\n",av7110);
240 
241 	for (;;) {
242 		timeout = wait_event_interruptible_timeout(av7110->arm_wait,
243 			kthread_should_stop(), 5 * HZ);
244 
245 		if (-ERESTARTSYS == timeout || kthread_should_stop()) {
246 			/* got signal or told to quit*/
247 			break;
248 		}
249 
250 		if (!av7110->arm_ready)
251 			continue;
252 
253 		if (mutex_lock_interruptible(&av7110->dcomlock))
254 			break;
255 		newloops = rdebi(av7110, DEBINOSWAP, STATUS_LOOPS, 0, 2);
256 		mutex_unlock(&av7110->dcomlock);
257 
258 		if (newloops == av7110->arm_loops || av7110->arm_errors > 3) {
259 			printk(KERN_ERR "dvb-ttpci: ARM crashed @ card %d\n",
260 			       av7110->dvb_adapter.num);
261 
262 			recover_arm(av7110);
263 
264 			if (mutex_lock_interruptible(&av7110->dcomlock))
265 				break;
266 			newloops = rdebi(av7110, DEBINOSWAP, STATUS_LOOPS, 0, 2) - 1;
267 			mutex_unlock(&av7110->dcomlock);
268 		}
269 		av7110->arm_loops = newloops;
270 		av7110->arm_errors = 0;
271 	}
272 
273 	return 0;
274 }
275 
276 
277 /****************************************************************************
278  * IRQ handling
279  ****************************************************************************/
280 
DvbDmxFilterCallback(u8 * buffer1,size_t buffer1_len,u8 * buffer2,size_t buffer2_len,struct dvb_demux_filter * dvbdmxfilter,struct av7110 * av7110)281 static int DvbDmxFilterCallback(u8 *buffer1, size_t buffer1_len,
282 				u8 *buffer2, size_t buffer2_len,
283 				struct dvb_demux_filter *dvbdmxfilter,
284 				struct av7110 *av7110)
285 {
286 	if (!dvbdmxfilter->feed->demux->dmx.frontend)
287 		return 0;
288 	if (dvbdmxfilter->feed->demux->dmx.frontend->source == DMX_MEMORY_FE)
289 		return 0;
290 
291 	switch (dvbdmxfilter->type) {
292 	case DMX_TYPE_SEC:
293 		if ((((buffer1[1] << 8) | buffer1[2]) & 0xfff) + 3 != buffer1_len)
294 			return 0;
295 		if (dvbdmxfilter->doneq) {
296 			struct dmx_section_filter *filter = &dvbdmxfilter->filter;
297 			int i;
298 			u8 xor, neq = 0;
299 
300 			for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
301 				xor = filter->filter_value[i] ^ buffer1[i];
302 				neq |= dvbdmxfilter->maskandnotmode[i] & xor;
303 			}
304 			if (!neq)
305 				return 0;
306 		}
307 		return dvbdmxfilter->feed->cb.sec(buffer1, buffer1_len,
308 						  buffer2, buffer2_len,
309 						  &dvbdmxfilter->filter, NULL);
310 	case DMX_TYPE_TS:
311 		if (!(dvbdmxfilter->feed->ts_type & TS_PACKET))
312 			return 0;
313 		if (dvbdmxfilter->feed->ts_type & TS_PAYLOAD_ONLY)
314 			return dvbdmxfilter->feed->cb.ts(buffer1, buffer1_len,
315 							 buffer2, buffer2_len,
316 							 &dvbdmxfilter->feed->feed.ts,
317 							 NULL);
318 		else
319 			av7110_p2t_write(buffer1, buffer1_len,
320 					 dvbdmxfilter->feed->pid,
321 					 &av7110->p2t_filter[dvbdmxfilter->index]);
322 		return 0;
323 	default:
324 		return 0;
325 	}
326 }
327 
328 
329 //#define DEBUG_TIMING
print_time(char * s)330 static inline void print_time(char *s)
331 {
332 #ifdef DEBUG_TIMING
333 	struct timespec64 ts;
334 	ktime_get_real_ts64(&ts);
335 	printk("%s: %lld.%09ld\n", s, (s64)ts.tv_sec, ts.tv_nsec);
336 #endif
337 }
338 
339 #define DEBI_READ 0
340 #define DEBI_WRITE 1
start_debi_dma(struct av7110 * av7110,int dir,unsigned long addr,unsigned int len)341 static inline void start_debi_dma(struct av7110 *av7110, int dir,
342 				  unsigned long addr, unsigned int len)
343 {
344 	dprintk(8, "%c %08lx %u\n", dir == DEBI_READ ? 'R' : 'W', addr, len);
345 	if (saa7146_wait_for_debi_done(av7110->dev, 0)) {
346 		printk(KERN_ERR "%s: saa7146_wait_for_debi_done timed out\n", __func__);
347 		return;
348 	}
349 
350 	SAA7146_ISR_CLEAR(av7110->dev, MASK_19); /* for good measure */
351 	SAA7146_IER_ENABLE(av7110->dev, MASK_19);
352 	if (len < 5)
353 		len = 5; /* we want a real DEBI DMA */
354 	if (dir == DEBI_WRITE)
355 		iwdebi(av7110, DEBISWAB, addr, 0, (len + 3) & ~3);
356 	else
357 		irdebi(av7110, DEBISWAB, addr, 0, len);
358 }
359 
debiirq(struct tasklet_struct * t)360 static void debiirq(struct tasklet_struct *t)
361 {
362 	struct av7110 *av7110 = from_tasklet(av7110, t, debi_tasklet);
363 	int type = av7110->debitype;
364 	int handle = (type >> 8) & 0x1f;
365 	unsigned int xfer = 0;
366 
367 	print_time("debi");
368 	dprintk(4, "type 0x%04x\n", type);
369 
370 	if (type == -1) {
371 		printk("DEBI irq oops @ %ld, psr:0x%08x, ssr:0x%08x\n",
372 		       jiffies, saa7146_read(av7110->dev, PSR),
373 		       saa7146_read(av7110->dev, SSR));
374 		goto debi_done;
375 	}
376 	av7110->debitype = -1;
377 
378 	switch (type & 0xff) {
379 
380 	case DATA_TS_RECORD:
381 		dvb_dmx_swfilter_packets(&av7110->demux,
382 					 (const u8 *) av7110->debi_virt,
383 					 av7110->debilen / 188);
384 		xfer = RX_BUFF;
385 		break;
386 
387 	case DATA_PES_RECORD:
388 		if (av7110->demux.recording)
389 			av7110_record_cb(&av7110->p2t[handle],
390 					 (u8 *) av7110->debi_virt,
391 					 av7110->debilen);
392 		xfer = RX_BUFF;
393 		break;
394 
395 	case DATA_IPMPE:
396 	case DATA_FSECTION:
397 	case DATA_PIPING:
398 		if (av7110->handle2filter[handle])
399 			DvbDmxFilterCallback((u8 *)av7110->debi_virt,
400 					     av7110->debilen, NULL, 0,
401 					     av7110->handle2filter[handle],
402 					     av7110);
403 		xfer = RX_BUFF;
404 		break;
405 
406 	case DATA_CI_GET:
407 	{
408 		u8 *data = av7110->debi_virt;
409 		u8 data_0 = data[0];
410 
411 		if (data_0 < 2 && data[2] == 0xff) {
412 			int flags = 0;
413 			if (data[5] > 0)
414 				flags |= CA_CI_MODULE_PRESENT;
415 			if (data[5] > 5)
416 				flags |= CA_CI_MODULE_READY;
417 			av7110->ci_slot[data_0].flags = flags;
418 		} else
419 			ci_get_data(&av7110->ci_rbuffer,
420 				    av7110->debi_virt,
421 				    av7110->debilen);
422 		xfer = RX_BUFF;
423 		break;
424 	}
425 
426 	case DATA_COMMON_INTERFACE:
427 		CI_handle(av7110, (u8 *)av7110->debi_virt, av7110->debilen);
428 		xfer = RX_BUFF;
429 		break;
430 
431 	case DATA_DEBUG_MESSAGE:
432 		((s8*)av7110->debi_virt)[Reserved_SIZE - 1] = 0;
433 		printk("%s\n", (s8 *) av7110->debi_virt);
434 		xfer = RX_BUFF;
435 		break;
436 
437 	case DATA_CI_PUT:
438 		dprintk(4, "debi DATA_CI_PUT\n");
439 		xfer = TX_BUFF;
440 		break;
441 	case DATA_MPEG_PLAY:
442 		dprintk(4, "debi DATA_MPEG_PLAY\n");
443 		xfer = TX_BUFF;
444 		break;
445 	case DATA_BMP_LOAD:
446 		dprintk(4, "debi DATA_BMP_LOAD\n");
447 		xfer = TX_BUFF;
448 		break;
449 	default:
450 		break;
451 	}
452 debi_done:
453 	spin_lock(&av7110->debilock);
454 	if (xfer)
455 		iwdebi(av7110, DEBINOSWAP, xfer, 0, 2);
456 	ARM_ClearMailBox(av7110);
457 	spin_unlock(&av7110->debilock);
458 }
459 
460 /* irq from av7110 firmware writing the mailbox register in the DPRAM */
gpioirq(struct tasklet_struct * t)461 static void gpioirq(struct tasklet_struct *t)
462 {
463 	struct av7110 *av7110 = from_tasklet(av7110, t, gpio_tasklet);
464 	u32 rxbuf, txbuf;
465 	int len;
466 
467 	if (av7110->debitype != -1)
468 		/* we shouldn't get any irq while a debi xfer is running */
469 		printk("dvb-ttpci: GPIO0 irq oops @ %ld, psr:0x%08x, ssr:0x%08x\n",
470 		       jiffies, saa7146_read(av7110->dev, PSR),
471 		       saa7146_read(av7110->dev, SSR));
472 
473 	if (saa7146_wait_for_debi_done(av7110->dev, 0)) {
474 		printk(KERN_ERR "%s: saa7146_wait_for_debi_done timed out\n", __func__);
475 		BUG(); /* maybe we should try resetting the debi? */
476 	}
477 
478 	spin_lock(&av7110->debilock);
479 	ARM_ClearIrq(av7110);
480 
481 	/* see what the av7110 wants */
482 	av7110->debitype = irdebi(av7110, DEBINOSWAP, IRQ_STATE, 0, 2);
483 	av7110->debilen  = irdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
484 	rxbuf = irdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
485 	txbuf = irdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
486 	len = (av7110->debilen + 3) & ~3;
487 
488 	print_time("gpio");
489 	dprintk(8, "GPIO0 irq 0x%04x %d\n", av7110->debitype, av7110->debilen);
490 
491 	switch (av7110->debitype & 0xff) {
492 
493 	case DATA_TS_PLAY:
494 	case DATA_PES_PLAY:
495 		break;
496 
497 	case DATA_MPEG_VIDEO_EVENT:
498 	{
499 		u32 h_ar;
500 		struct video_event event;
501 
502 		av7110->video_size.w = irdebi(av7110, DEBINOSWAP, STATUS_MPEG_WIDTH, 0, 2);
503 		h_ar = irdebi(av7110, DEBINOSWAP, STATUS_MPEG_HEIGHT_AR, 0, 2);
504 
505 		iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
506 		iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
507 
508 		av7110->video_size.h = h_ar & 0xfff;
509 
510 		event.type = VIDEO_EVENT_SIZE_CHANGED;
511 		event.u.size.w = av7110->video_size.w;
512 		event.u.size.h = av7110->video_size.h;
513 		switch ((h_ar >> 12) & 0xf)
514 		{
515 		case 3:
516 			av7110->video_size.aspect_ratio = VIDEO_FORMAT_16_9;
517 			event.u.size.aspect_ratio = VIDEO_FORMAT_16_9;
518 			av7110->videostate.video_format = VIDEO_FORMAT_16_9;
519 			break;
520 		case 4:
521 			av7110->video_size.aspect_ratio = VIDEO_FORMAT_221_1;
522 			event.u.size.aspect_ratio = VIDEO_FORMAT_221_1;
523 			av7110->videostate.video_format = VIDEO_FORMAT_221_1;
524 			break;
525 		default:
526 			av7110->video_size.aspect_ratio = VIDEO_FORMAT_4_3;
527 			event.u.size.aspect_ratio = VIDEO_FORMAT_4_3;
528 			av7110->videostate.video_format = VIDEO_FORMAT_4_3;
529 		}
530 
531 		dprintk(8, "GPIO0 irq: DATA_MPEG_VIDEO_EVENT: w/h/ar = %u/%u/%u\n",
532 			av7110->video_size.w, av7110->video_size.h,
533 			av7110->video_size.aspect_ratio);
534 
535 		dvb_video_add_event(av7110, &event);
536 		break;
537 	}
538 
539 	case DATA_CI_PUT:
540 	{
541 		int avail;
542 		struct dvb_ringbuffer *cibuf = &av7110->ci_wbuffer;
543 
544 		avail = dvb_ringbuffer_avail(cibuf);
545 		if (avail <= 2) {
546 			iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
547 			iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
548 			iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
549 			break;
550 		}
551 		len = DVB_RINGBUFFER_PEEK(cibuf, 0) << 8;
552 		len |= DVB_RINGBUFFER_PEEK(cibuf, 1);
553 		if (avail < len + 2) {
554 			iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
555 			iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
556 			iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
557 			break;
558 		}
559 		DVB_RINGBUFFER_SKIP(cibuf, 2);
560 
561 		dvb_ringbuffer_read(cibuf, av7110->debi_virt, len);
562 
563 		iwdebi(av7110, DEBINOSWAP, TX_LEN, len, 2);
564 		iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, len, 2);
565 		dprintk(8, "DMA: CI\n");
566 		start_debi_dma(av7110, DEBI_WRITE, DPRAM_BASE + txbuf, len);
567 		spin_unlock(&av7110->debilock);
568 		wake_up(&cibuf->queue);
569 		return;
570 	}
571 
572 	case DATA_MPEG_PLAY:
573 		if (!av7110->playing) {
574 			iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
575 			iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
576 			iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
577 			break;
578 		}
579 		len = 0;
580 		if (av7110->debitype & 0x100) {
581 			spin_lock(&av7110->aout.lock);
582 			len = av7110_pes_play(av7110->debi_virt, &av7110->aout, 2048);
583 			spin_unlock(&av7110->aout.lock);
584 		}
585 		if (len <= 0 && (av7110->debitype & 0x200)
586 		    &&av7110->videostate.play_state != VIDEO_FREEZED) {
587 			spin_lock(&av7110->avout.lock);
588 			len = av7110_pes_play(av7110->debi_virt, &av7110->avout, 2048);
589 			spin_unlock(&av7110->avout.lock);
590 		}
591 		if (len <= 0) {
592 			iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
593 			iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
594 			iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
595 			break;
596 		}
597 		dprintk(8, "GPIO0 PES_PLAY len=%04x\n", len);
598 		iwdebi(av7110, DEBINOSWAP, TX_LEN, len, 2);
599 		iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, len, 2);
600 		dprintk(8, "DMA: MPEG_PLAY\n");
601 		start_debi_dma(av7110, DEBI_WRITE, DPRAM_BASE + txbuf, len);
602 		spin_unlock(&av7110->debilock);
603 		return;
604 
605 	case DATA_BMP_LOAD:
606 		len = av7110->debilen;
607 		dprintk(8, "gpio DATA_BMP_LOAD len %d\n", len);
608 		if (!len) {
609 			av7110->bmp_state = BMP_LOADED;
610 			iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, 0, 2);
611 			iwdebi(av7110, DEBINOSWAP, TX_LEN, 0, 2);
612 			iwdebi(av7110, DEBINOSWAP, TX_BUFF, 0, 2);
613 			wake_up(&av7110->bmpq);
614 			dprintk(8, "gpio DATA_BMP_LOAD done\n");
615 			break;
616 		}
617 		if (len > av7110->bmplen)
618 			len = av7110->bmplen;
619 		if (len > 2 * 1024)
620 			len = 2 * 1024;
621 		iwdebi(av7110, DEBINOSWAP, TX_LEN, len, 2);
622 		iwdebi(av7110, DEBINOSWAP, IRQ_STATE_EXT, len, 2);
623 		memcpy(av7110->debi_virt, av7110->bmpbuf+av7110->bmpp, len);
624 		av7110->bmpp += len;
625 		av7110->bmplen -= len;
626 		dprintk(8, "gpio DATA_BMP_LOAD DMA len %d\n", len);
627 		start_debi_dma(av7110, DEBI_WRITE, DPRAM_BASE+txbuf, len);
628 		spin_unlock(&av7110->debilock);
629 		return;
630 
631 	case DATA_CI_GET:
632 	case DATA_COMMON_INTERFACE:
633 	case DATA_FSECTION:
634 	case DATA_IPMPE:
635 	case DATA_PIPING:
636 		if (!len || len > 4 * 1024) {
637 			iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
638 			break;
639 		}
640 		fallthrough;
641 
642 	case DATA_TS_RECORD:
643 	case DATA_PES_RECORD:
644 		dprintk(8, "DMA: TS_REC etc.\n");
645 		start_debi_dma(av7110, DEBI_READ, DPRAM_BASE+rxbuf, len);
646 		spin_unlock(&av7110->debilock);
647 		return;
648 
649 	case DATA_DEBUG_MESSAGE:
650 		if (!len || len > 0xff) {
651 			iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
652 			break;
653 		}
654 		start_debi_dma(av7110, DEBI_READ, Reserved, len);
655 		spin_unlock(&av7110->debilock);
656 		return;
657 
658 	case DATA_IRCOMMAND:
659 #if IS_ENABLED(CONFIG_DVB_AV7110_IR)
660 		av7110_ir_handler(av7110,
661 				  swahw32(irdebi(av7110, DEBINOSWAP, Reserved,
662 						 0, 4)));
663 #endif
664 		iwdebi(av7110, DEBINOSWAP, RX_BUFF, 0, 2);
665 		break;
666 
667 	default:
668 		printk("dvb-ttpci: gpioirq unknown type=%d len=%d\n",
669 		       av7110->debitype, av7110->debilen);
670 		break;
671 	}
672 	av7110->debitype = -1;
673 	ARM_ClearMailBox(av7110);
674 	spin_unlock(&av7110->debilock);
675 }
676 
677 
678 #ifdef CONFIG_DVB_AV7110_OSD
dvb_osd_ioctl(struct file * file,unsigned int cmd,void * parg)679 static int dvb_osd_ioctl(struct file *file,
680 			 unsigned int cmd, void *parg)
681 {
682 	struct dvb_device *dvbdev = file->private_data;
683 	struct av7110 *av7110 = dvbdev->priv;
684 
685 	dprintk(4, "%p\n", av7110);
686 
687 	if (cmd == OSD_SEND_CMD)
688 		return av7110_osd_cmd(av7110, (osd_cmd_t *) parg);
689 	if (cmd == OSD_GET_CAPABILITY)
690 		return av7110_osd_capability(av7110, (osd_cap_t *) parg);
691 
692 	return -EINVAL;
693 }
694 
695 
696 static const struct file_operations dvb_osd_fops = {
697 	.owner		= THIS_MODULE,
698 	.unlocked_ioctl	= dvb_generic_ioctl,
699 	.open		= dvb_generic_open,
700 	.release	= dvb_generic_release,
701 	.llseek		= noop_llseek,
702 };
703 
704 static struct dvb_device dvbdev_osd = {
705 	.priv		= NULL,
706 	.users		= 1,
707 	.writers	= 1,
708 	.fops		= &dvb_osd_fops,
709 	.kernel_ioctl	= dvb_osd_ioctl,
710 };
711 #endif /* CONFIG_DVB_AV7110_OSD */
712 
713 
SetPIDs(struct av7110 * av7110,u16 vpid,u16 apid,u16 ttpid,u16 subpid,u16 pcrpid)714 static inline int SetPIDs(struct av7110 *av7110, u16 vpid, u16 apid, u16 ttpid,
715 			  u16 subpid, u16 pcrpid)
716 {
717 	u16 aflags = 0;
718 
719 	dprintk(4, "%p\n", av7110);
720 
721 	if (vpid == 0x1fff || apid == 0x1fff ||
722 	    ttpid == 0x1fff || subpid == 0x1fff || pcrpid == 0x1fff) {
723 		vpid = apid = ttpid = subpid = pcrpid = 0;
724 		av7110->pids[DMX_PES_VIDEO] = 0;
725 		av7110->pids[DMX_PES_AUDIO] = 0;
726 		av7110->pids[DMX_PES_TELETEXT] = 0;
727 		av7110->pids[DMX_PES_PCR] = 0;
728 	}
729 
730 	if (av7110->audiostate.bypass_mode)
731 		aflags |= 0x8000;
732 
733 	return av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, MultiPID, 6,
734 			     pcrpid, vpid, apid, ttpid, subpid, aflags);
735 }
736 
ChangePIDs(struct av7110 * av7110,u16 vpid,u16 apid,u16 ttpid,u16 subpid,u16 pcrpid)737 int ChangePIDs(struct av7110 *av7110, u16 vpid, u16 apid, u16 ttpid,
738 		u16 subpid, u16 pcrpid)
739 {
740 	int ret = 0;
741 	dprintk(4, "%p\n", av7110);
742 
743 	if (mutex_lock_interruptible(&av7110->pid_mutex))
744 		return -ERESTARTSYS;
745 
746 	if (!(vpid & 0x8000))
747 		av7110->pids[DMX_PES_VIDEO] = vpid;
748 	if (!(apid & 0x8000))
749 		av7110->pids[DMX_PES_AUDIO] = apid;
750 	if (!(ttpid & 0x8000))
751 		av7110->pids[DMX_PES_TELETEXT] = ttpid;
752 	if (!(pcrpid & 0x8000))
753 		av7110->pids[DMX_PES_PCR] = pcrpid;
754 
755 	av7110->pids[DMX_PES_SUBTITLE] = 0;
756 
757 	if (av7110->fe_synced) {
758 		pcrpid = av7110->pids[DMX_PES_PCR];
759 		ret = SetPIDs(av7110, vpid, apid, ttpid, subpid, pcrpid);
760 	}
761 
762 	mutex_unlock(&av7110->pid_mutex);
763 	return ret;
764 }
765 
766 
767 /******************************************************************************
768  * hardware filter functions
769  ******************************************************************************/
770 
StartHWFilter(struct dvb_demux_filter * dvbdmxfilter)771 static int StartHWFilter(struct dvb_demux_filter *dvbdmxfilter)
772 {
773 	struct dvb_demux_feed *dvbdmxfeed = dvbdmxfilter->feed;
774 	struct av7110 *av7110 = dvbdmxfeed->demux->priv;
775 	u16 buf[20];
776 	int ret, i;
777 	u16 handle;
778 //	u16 mode = 0x0320;
779 	u16 mode = 0xb96a;
780 
781 	dprintk(4, "%p\n", av7110);
782 
783 	if (av7110->full_ts)
784 		return 0;
785 
786 	if (dvbdmxfilter->type == DMX_TYPE_SEC) {
787 		if (hw_sections) {
788 			buf[4] = (dvbdmxfilter->filter.filter_value[0] << 8) |
789 				dvbdmxfilter->maskandmode[0];
790 			for (i = 3; i < 18; i++)
791 				buf[i + 4 - 2] =
792 					(dvbdmxfilter->filter.filter_value[i] << 8) |
793 					dvbdmxfilter->maskandmode[i];
794 			mode = 4;
795 		}
796 	} else if ((dvbdmxfeed->ts_type & TS_PACKET) &&
797 		   !(dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)) {
798 		av7110_p2t_init(&av7110->p2t_filter[dvbdmxfilter->index], dvbdmxfeed);
799 	}
800 
801 	buf[0] = (COMTYPE_PID_FILTER << 8) + AddPIDFilter;
802 	buf[1] = 16;
803 	buf[2] = dvbdmxfeed->pid;
804 	buf[3] = mode;
805 
806 	ret = av7110_fw_request(av7110, buf, 20, &handle, 1);
807 	if (ret != 0 || handle >= 32) {
808 		printk(KERN_ERR "dvb-ttpci: %s error  buf %04x %04x %04x %04x  ret %d  handle %04x\n",
809 				__func__, buf[0], buf[1], buf[2], buf[3],
810 				ret, handle);
811 		dvbdmxfilter->hw_handle = 0xffff;
812 		if (!ret)
813 			ret = -1;
814 		return ret;
815 	}
816 
817 	av7110->handle2filter[handle] = dvbdmxfilter;
818 	dvbdmxfilter->hw_handle = handle;
819 
820 	return ret;
821 }
822 
StopHWFilter(struct dvb_demux_filter * dvbdmxfilter)823 static int StopHWFilter(struct dvb_demux_filter *dvbdmxfilter)
824 {
825 	struct av7110 *av7110 = dvbdmxfilter->feed->demux->priv;
826 	u16 buf[3];
827 	u16 answ[2];
828 	int ret;
829 	u16 handle;
830 
831 	dprintk(4, "%p\n", av7110);
832 
833 	if (av7110->full_ts)
834 		return 0;
835 
836 	handle = dvbdmxfilter->hw_handle;
837 	if (handle >= 32) {
838 		printk("%s tried to stop invalid filter %04x, filter type = %x\n",
839 				__func__, handle, dvbdmxfilter->type);
840 		return -EINVAL;
841 	}
842 
843 	av7110->handle2filter[handle] = NULL;
844 
845 	buf[0] = (COMTYPE_PID_FILTER << 8) + DelPIDFilter;
846 	buf[1] = 1;
847 	buf[2] = handle;
848 	ret = av7110_fw_request(av7110, buf, 3, answ, 2);
849 	if (ret != 0 || answ[1] != handle) {
850 		printk(KERN_ERR "dvb-ttpci: %s error  cmd %04x %04x %04x  ret %x  resp %04x %04x  pid %d\n",
851 				__func__, buf[0], buf[1], buf[2], ret,
852 				answ[0], answ[1], dvbdmxfilter->feed->pid);
853 		if (!ret)
854 			ret = -1;
855 	}
856 	return ret;
857 }
858 
859 
dvb_feed_start_pid(struct dvb_demux_feed * dvbdmxfeed)860 static int dvb_feed_start_pid(struct dvb_demux_feed *dvbdmxfeed)
861 {
862 	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
863 	struct av7110 *av7110 = dvbdmx->priv;
864 	u16 *pid = dvbdmx->pids, npids[5];
865 	int i;
866 	int ret = 0;
867 
868 	dprintk(4, "%p\n", av7110);
869 
870 	npids[0] = npids[1] = npids[2] = npids[3] = npids[4] = 0xffff;
871 	i = dvbdmxfeed->pes_type;
872 	npids[i] = (pid[i]&0x8000) ? 0 : pid[i];
873 	if ((i == 2) && npids[i] && (dvbdmxfeed->ts_type & TS_PACKET)) {
874 		npids[i] = 0;
875 		ret = ChangePIDs(av7110, npids[1], npids[0], npids[2], npids[3], npids[4]);
876 		if (!ret)
877 			ret = StartHWFilter(dvbdmxfeed->filter);
878 		return ret;
879 	}
880 	if (dvbdmxfeed->pes_type <= 2 || dvbdmxfeed->pes_type == 4) {
881 		ret = ChangePIDs(av7110, npids[1], npids[0], npids[2], npids[3], npids[4]);
882 		if (ret)
883 			return ret;
884 	}
885 
886 	if (dvbdmxfeed->pes_type < 2 && npids[0])
887 		if (av7110->fe_synced)
888 		{
889 			ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
890 			if (ret)
891 				return ret;
892 		}
893 
894 	if ((dvbdmxfeed->ts_type & TS_PACKET) && !av7110->full_ts) {
895 		if (dvbdmxfeed->pes_type == 0 && !(dvbdmx->pids[0] & 0x8000))
896 			ret = av7110_av_start_record(av7110, RP_AUDIO, dvbdmxfeed);
897 		if (dvbdmxfeed->pes_type == 1 && !(dvbdmx->pids[1] & 0x8000))
898 			ret = av7110_av_start_record(av7110, RP_VIDEO, dvbdmxfeed);
899 	}
900 	return ret;
901 }
902 
dvb_feed_stop_pid(struct dvb_demux_feed * dvbdmxfeed)903 static int dvb_feed_stop_pid(struct dvb_demux_feed *dvbdmxfeed)
904 {
905 	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
906 	struct av7110 *av7110 = dvbdmx->priv;
907 	u16 *pid = dvbdmx->pids, npids[5];
908 	int i;
909 
910 	int ret = 0;
911 
912 	dprintk(4, "%p\n", av7110);
913 
914 	if (dvbdmxfeed->pes_type <= 1) {
915 		ret = av7110_av_stop(av7110, dvbdmxfeed->pes_type ?  RP_VIDEO : RP_AUDIO);
916 		if (ret)
917 			return ret;
918 		if (!av7110->rec_mode)
919 			dvbdmx->recording = 0;
920 		if (!av7110->playing)
921 			dvbdmx->playing = 0;
922 	}
923 	npids[0] = npids[1] = npids[2] = npids[3] = npids[4] = 0xffff;
924 	i = dvbdmxfeed->pes_type;
925 	switch (i) {
926 	case 2: //teletext
927 		if (dvbdmxfeed->ts_type & TS_PACKET)
928 			ret = StopHWFilter(dvbdmxfeed->filter);
929 		npids[2] = 0;
930 		break;
931 	case 0:
932 	case 1:
933 	case 4:
934 		if (!pids_off)
935 			return 0;
936 		npids[i] = (pid[i]&0x8000) ? 0 : pid[i];
937 		break;
938 	}
939 	if (!ret)
940 		ret = ChangePIDs(av7110, npids[1], npids[0], npids[2], npids[3], npids[4]);
941 	return ret;
942 }
943 
av7110_start_feed(struct dvb_demux_feed * feed)944 static int av7110_start_feed(struct dvb_demux_feed *feed)
945 {
946 	struct dvb_demux *demux = feed->demux;
947 	struct av7110 *av7110 = demux->priv;
948 	int ret = 0;
949 
950 	dprintk(4, "%p\n", av7110);
951 
952 	if (!demux->dmx.frontend)
953 		return -EINVAL;
954 
955 	if (!av7110->full_ts && feed->pid > 0x1fff)
956 		return -EINVAL;
957 
958 	if (feed->type == DMX_TYPE_TS) {
959 		if ((feed->ts_type & TS_DECODER) &&
960 		    (feed->pes_type <= DMX_PES_PCR)) {
961 			switch (demux->dmx.frontend->source) {
962 			case DMX_MEMORY_FE:
963 				if (feed->ts_type & TS_DECODER)
964 				       if (feed->pes_type < 2 &&
965 					   !(demux->pids[0] & 0x8000) &&
966 					   !(demux->pids[1] & 0x8000)) {
967 					       dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
968 					       dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
969 					       ret = av7110_av_start_play(av7110,RP_AV);
970 					       if (!ret)
971 						       demux->playing = 1;
972 					}
973 				break;
974 			default:
975 				ret = dvb_feed_start_pid(feed);
976 				break;
977 			}
978 		} else if ((feed->ts_type & TS_PACKET) &&
979 			   (demux->dmx.frontend->source != DMX_MEMORY_FE)) {
980 			ret = StartHWFilter(feed->filter);
981 		}
982 	}
983 
984 	if (av7110->full_ts) {
985 		budget_start_feed(feed);
986 		return ret;
987 	}
988 
989 	if (feed->type == DMX_TYPE_SEC) {
990 		int i;
991 
992 		for (i = 0; i < demux->filternum; i++) {
993 			if (demux->filter[i].state != DMX_STATE_READY)
994 				continue;
995 			if (demux->filter[i].type != DMX_TYPE_SEC)
996 				continue;
997 			if (demux->filter[i].filter.parent != &feed->feed.sec)
998 				continue;
999 			demux->filter[i].state = DMX_STATE_GO;
1000 			if (demux->dmx.frontend->source != DMX_MEMORY_FE) {
1001 				ret = StartHWFilter(&demux->filter[i]);
1002 				if (ret)
1003 					break;
1004 			}
1005 		}
1006 	}
1007 
1008 	return ret;
1009 }
1010 
1011 
av7110_stop_feed(struct dvb_demux_feed * feed)1012 static int av7110_stop_feed(struct dvb_demux_feed *feed)
1013 {
1014 	struct dvb_demux *demux = feed->demux;
1015 	struct av7110 *av7110 = demux->priv;
1016 	int i, rc, ret = 0;
1017 	dprintk(4, "%p\n", av7110);
1018 
1019 	if (feed->type == DMX_TYPE_TS) {
1020 		if (feed->ts_type & TS_DECODER) {
1021 			if (feed->pes_type >= DMX_PES_OTHER ||
1022 			    !demux->pesfilter[feed->pes_type])
1023 				return -EINVAL;
1024 			demux->pids[feed->pes_type] |= 0x8000;
1025 			demux->pesfilter[feed->pes_type] = NULL;
1026 		}
1027 		if (feed->ts_type & TS_DECODER &&
1028 		    feed->pes_type < DMX_PES_OTHER) {
1029 			ret = dvb_feed_stop_pid(feed);
1030 		} else
1031 			if ((feed->ts_type & TS_PACKET) &&
1032 			    (demux->dmx.frontend->source != DMX_MEMORY_FE))
1033 				ret = StopHWFilter(feed->filter);
1034 	}
1035 
1036 	if (av7110->full_ts) {
1037 		budget_stop_feed(feed);
1038 		return ret;
1039 	}
1040 
1041 	if (feed->type == DMX_TYPE_SEC) {
1042 		for (i = 0; i<demux->filternum; i++) {
1043 			if (demux->filter[i].state == DMX_STATE_GO &&
1044 			    demux->filter[i].filter.parent == &feed->feed.sec) {
1045 				demux->filter[i].state = DMX_STATE_READY;
1046 				if (demux->dmx.frontend->source != DMX_MEMORY_FE) {
1047 					rc = StopHWFilter(&demux->filter[i]);
1048 					if (!ret)
1049 						ret = rc;
1050 					/* keep going, stop as many filters as possible */
1051 				}
1052 			}
1053 		}
1054 	}
1055 
1056 	return ret;
1057 }
1058 
1059 
restart_feeds(struct av7110 * av7110)1060 static void restart_feeds(struct av7110 *av7110)
1061 {
1062 	struct dvb_demux *dvbdmx = &av7110->demux;
1063 	struct dvb_demux_feed *feed;
1064 	int mode;
1065 	int feeding;
1066 	int i, j;
1067 
1068 	dprintk(4, "%p\n", av7110);
1069 
1070 	mode = av7110->playing;
1071 	av7110->playing = 0;
1072 	av7110->rec_mode = 0;
1073 
1074 	feeding = av7110->feeding1; /* full_ts mod */
1075 
1076 	for (i = 0; i < dvbdmx->feednum; i++) {
1077 		feed = &dvbdmx->feed[i];
1078 		if (feed->state == DMX_STATE_GO) {
1079 			if (feed->type == DMX_TYPE_SEC) {
1080 				for (j = 0; j < dvbdmx->filternum; j++) {
1081 					if (dvbdmx->filter[j].type != DMX_TYPE_SEC)
1082 						continue;
1083 					if (dvbdmx->filter[j].filter.parent != &feed->feed.sec)
1084 						continue;
1085 					if (dvbdmx->filter[j].state == DMX_STATE_GO)
1086 						dvbdmx->filter[j].state = DMX_STATE_READY;
1087 				}
1088 			}
1089 			av7110_start_feed(feed);
1090 		}
1091 	}
1092 
1093 	av7110->feeding1 = feeding; /* full_ts mod */
1094 
1095 	if (mode)
1096 		av7110_av_start_play(av7110, mode);
1097 }
1098 
dvb_get_stc(struct dmx_demux * demux,unsigned int num,uint64_t * stc,unsigned int * base)1099 static int dvb_get_stc(struct dmx_demux *demux, unsigned int num,
1100 		       uint64_t *stc, unsigned int *base)
1101 {
1102 	int ret;
1103 	u16 fwstc[4];
1104 	u16 tag = ((COMTYPE_REQUEST << 8) + ReqSTC);
1105 	struct dvb_demux *dvbdemux;
1106 	struct av7110 *av7110;
1107 
1108 	/* pointer casting paranoia... */
1109 	if (WARN_ON(!demux))
1110 		return -EIO;
1111 	dvbdemux = demux->priv;
1112 	if (WARN_ON(!dvbdemux))
1113 		return -EIO;
1114 	av7110 = dvbdemux->priv;
1115 
1116 	dprintk(4, "%p\n", av7110);
1117 
1118 	if (num != 0)
1119 		return -EINVAL;
1120 
1121 	ret = av7110_fw_request(av7110, &tag, 0, fwstc, 4);
1122 	if (ret) {
1123 		printk(KERN_ERR "%s: av7110_fw_request error\n", __func__);
1124 		return ret;
1125 	}
1126 	dprintk(2, "fwstc = %04hx %04hx %04hx %04hx\n",
1127 		fwstc[0], fwstc[1], fwstc[2], fwstc[3]);
1128 
1129 	*stc =	(((uint64_t) ((fwstc[3] & 0x8000) >> 15)) << 32) |
1130 		(((uint64_t)  fwstc[1]) << 16) | ((uint64_t) fwstc[0]);
1131 	*base = 1;
1132 
1133 	dprintk(4, "stc = %lu\n", (unsigned long)*stc);
1134 
1135 	return 0;
1136 }
1137 
1138 
1139 /******************************************************************************
1140  * SEC device file operations
1141  ******************************************************************************/
1142 
1143 
av7110_set_tone(struct dvb_frontend * fe,enum fe_sec_tone_mode tone)1144 static int av7110_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
1145 {
1146 	struct av7110* av7110 = fe->dvb->priv;
1147 
1148 	switch (tone) {
1149 	case SEC_TONE_ON:
1150 		return Set22K(av7110, 1);
1151 
1152 	case SEC_TONE_OFF:
1153 		return Set22K(av7110, 0);
1154 
1155 	default:
1156 		return -EINVAL;
1157 	}
1158 }
1159 
av7110_diseqc_send_master_cmd(struct dvb_frontend * fe,struct dvb_diseqc_master_cmd * cmd)1160 static int av7110_diseqc_send_master_cmd(struct dvb_frontend* fe,
1161 					 struct dvb_diseqc_master_cmd* cmd)
1162 {
1163 	struct av7110* av7110 = fe->dvb->priv;
1164 
1165 	return av7110_diseqc_send(av7110, cmd->msg_len, cmd->msg, -1);
1166 }
1167 
av7110_diseqc_send_burst(struct dvb_frontend * fe,enum fe_sec_mini_cmd minicmd)1168 static int av7110_diseqc_send_burst(struct dvb_frontend* fe,
1169 				    enum fe_sec_mini_cmd minicmd)
1170 {
1171 	struct av7110* av7110 = fe->dvb->priv;
1172 
1173 	return av7110_diseqc_send(av7110, 0, NULL, minicmd);
1174 }
1175 
1176 /* simplified code from budget-core.c */
stop_ts_capture(struct av7110 * budget)1177 static int stop_ts_capture(struct av7110 *budget)
1178 {
1179 	dprintk(2, "budget: %p\n", budget);
1180 
1181 	if (--budget->feeding1)
1182 		return budget->feeding1;
1183 	saa7146_write(budget->dev, MC1, MASK_20);	/* DMA3 off */
1184 	SAA7146_IER_DISABLE(budget->dev, MASK_10);
1185 	SAA7146_ISR_CLEAR(budget->dev, MASK_10);
1186 	return 0;
1187 }
1188 
start_ts_capture(struct av7110 * budget)1189 static int start_ts_capture(struct av7110 *budget)
1190 {
1191 	unsigned y;
1192 
1193 	dprintk(2, "budget: %p\n", budget);
1194 
1195 	if (budget->feeding1)
1196 		return ++budget->feeding1;
1197 	for (y = 0; y < TS_HEIGHT; y++)
1198 		memset(budget->grabbing + y * TS_WIDTH, 0x00, TS_WIDTH);
1199 	budget->ttbp = 0;
1200 	SAA7146_ISR_CLEAR(budget->dev, MASK_10);  /* VPE */
1201 	SAA7146_IER_ENABLE(budget->dev, MASK_10); /* VPE */
1202 	saa7146_write(budget->dev, MC1, (MASK_04 | MASK_20)); /* DMA3 on */
1203 	return ++budget->feeding1;
1204 }
1205 
budget_start_feed(struct dvb_demux_feed * feed)1206 static int budget_start_feed(struct dvb_demux_feed *feed)
1207 {
1208 	struct dvb_demux *demux = feed->demux;
1209 	struct av7110 *budget = demux->priv;
1210 	int status;
1211 
1212 	dprintk(2, "av7110: %p\n", budget);
1213 
1214 	spin_lock(&budget->feedlock1);
1215 	feed->pusi_seen = false; /* have a clean section start */
1216 	status = start_ts_capture(budget);
1217 	spin_unlock(&budget->feedlock1);
1218 	return status;
1219 }
1220 
budget_stop_feed(struct dvb_demux_feed * feed)1221 static int budget_stop_feed(struct dvb_demux_feed *feed)
1222 {
1223 	struct dvb_demux *demux = feed->demux;
1224 	struct av7110 *budget = demux->priv;
1225 	int status;
1226 
1227 	dprintk(2, "budget: %p\n", budget);
1228 
1229 	spin_lock(&budget->feedlock1);
1230 	status = stop_ts_capture(budget);
1231 	spin_unlock(&budget->feedlock1);
1232 	return status;
1233 }
1234 
vpeirq(struct tasklet_struct * t)1235 static void vpeirq(struct tasklet_struct *t)
1236 {
1237 	struct av7110 *budget = from_tasklet(budget, t, vpe_tasklet);
1238 	u8 *mem = (u8 *) (budget->grabbing);
1239 	u32 olddma = budget->ttbp;
1240 	u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
1241 	struct dvb_demux *demux = budget->full_ts ? &budget->demux : &budget->demux1;
1242 
1243 	/* nearest lower position divisible by 188 */
1244 	newdma -= newdma % 188;
1245 
1246 	if (newdma >= TS_BUFLEN)
1247 		return;
1248 
1249 	budget->ttbp = newdma;
1250 
1251 	if (!budget->feeding1 || (newdma == olddma))
1252 		return;
1253 
1254 	/* Ensure streamed PCI data is synced to CPU */
1255 	dma_sync_sg_for_cpu(&budget->dev->pci->dev, budget->pt.slist,
1256 			    budget->pt.nents, DMA_FROM_DEVICE);
1257 
1258 #if 0
1259 	/* track rps1 activity */
1260 	printk("vpeirq: %02x Event Counter 1 0x%04x\n",
1261 	       mem[olddma],
1262 	       saa7146_read(budget->dev, EC1R) & 0x3fff);
1263 #endif
1264 
1265 	if (newdma > olddma)
1266 		/* no wraparound, dump olddma..newdma */
1267 		dvb_dmx_swfilter_packets(demux, mem + olddma, (newdma - olddma) / 188);
1268 	else {
1269 		/* wraparound, dump olddma..buflen and 0..newdma */
1270 		dvb_dmx_swfilter_packets(demux, mem + olddma, (TS_BUFLEN - olddma) / 188);
1271 		dvb_dmx_swfilter_packets(demux, mem, newdma / 188);
1272 	}
1273 }
1274 
av7110_register(struct av7110 * av7110)1275 static int av7110_register(struct av7110 *av7110)
1276 {
1277 	int ret, i;
1278 	struct dvb_demux *dvbdemux = &av7110->demux;
1279 	struct dvb_demux *dvbdemux1 = &av7110->demux1;
1280 
1281 	dprintk(4, "%p\n", av7110);
1282 
1283 	if (av7110->registered)
1284 		return -1;
1285 
1286 	av7110->registered = 1;
1287 
1288 	dvbdemux->priv = (void *) av7110;
1289 
1290 	for (i = 0; i < 32; i++)
1291 		av7110->handle2filter[i] = NULL;
1292 
1293 	dvbdemux->filternum = (av7110->full_ts) ? 256 : 32;
1294 	dvbdemux->feednum = (av7110->full_ts) ? 256 : 32;
1295 	dvbdemux->start_feed = av7110_start_feed;
1296 	dvbdemux->stop_feed = av7110_stop_feed;
1297 	dvbdemux->write_to_decoder = av7110_write_to_decoder;
1298 	dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
1299 				      DMX_MEMORY_BASED_FILTERING);
1300 
1301 	dvb_dmx_init(&av7110->demux);
1302 	av7110->demux.dmx.get_stc = dvb_get_stc;
1303 
1304 	av7110->dmxdev.filternum = (av7110->full_ts) ? 256 : 32;
1305 	av7110->dmxdev.demux = &dvbdemux->dmx;
1306 	av7110->dmxdev.capabilities = 0;
1307 
1308 	dvb_dmxdev_init(&av7110->dmxdev, &av7110->dvb_adapter);
1309 
1310 	av7110->hw_frontend.source = DMX_FRONTEND_0;
1311 
1312 	ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &av7110->hw_frontend);
1313 
1314 	if (ret < 0)
1315 		return ret;
1316 
1317 	av7110->mem_frontend.source = DMX_MEMORY_FE;
1318 
1319 	ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &av7110->mem_frontend);
1320 
1321 	if (ret < 0)
1322 		return ret;
1323 
1324 	ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx,
1325 					     &av7110->hw_frontend);
1326 	if (ret < 0)
1327 		return ret;
1328 
1329 	av7110_av_register(av7110);
1330 	av7110_ca_register(av7110);
1331 
1332 #ifdef CONFIG_DVB_AV7110_OSD
1333 	dvb_register_device(&av7110->dvb_adapter, &av7110->osd_dev,
1334 			    &dvbdev_osd, av7110, DVB_DEVICE_OSD, 0);
1335 #endif
1336 
1337 	dvb_net_init(&av7110->dvb_adapter, &av7110->dvb_net, &dvbdemux->dmx);
1338 
1339 	if (budgetpatch) {
1340 		/* initialize software demux1 without its own frontend
1341 		 * demux1 hardware is connected to frontend0 of demux0
1342 		 */
1343 		dvbdemux1->priv = (void *) av7110;
1344 
1345 		dvbdemux1->filternum = 256;
1346 		dvbdemux1->feednum = 256;
1347 		dvbdemux1->start_feed = budget_start_feed;
1348 		dvbdemux1->stop_feed = budget_stop_feed;
1349 		dvbdemux1->write_to_decoder = NULL;
1350 
1351 		dvbdemux1->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
1352 					       DMX_MEMORY_BASED_FILTERING);
1353 
1354 		dvb_dmx_init(&av7110->demux1);
1355 
1356 		av7110->dmxdev1.filternum = 256;
1357 		av7110->dmxdev1.demux = &dvbdemux1->dmx;
1358 		av7110->dmxdev1.capabilities = 0;
1359 
1360 		dvb_dmxdev_init(&av7110->dmxdev1, &av7110->dvb_adapter);
1361 
1362 		dvb_net_init(&av7110->dvb_adapter, &av7110->dvb_net1, &dvbdemux1->dmx);
1363 		printk("dvb-ttpci: additional demux1 for budget-patch registered\n");
1364 	}
1365 	return 0;
1366 }
1367 
1368 
dvb_unregister(struct av7110 * av7110)1369 static void dvb_unregister(struct av7110 *av7110)
1370 {
1371 	struct dvb_demux *dvbdemux = &av7110->demux;
1372 	struct dvb_demux *dvbdemux1 = &av7110->demux1;
1373 
1374 	dprintk(4, "%p\n", av7110);
1375 
1376 	if (!av7110->registered)
1377 		return;
1378 
1379 	if (budgetpatch) {
1380 		dvb_net_release(&av7110->dvb_net1);
1381 		dvbdemux->dmx.close(&dvbdemux1->dmx);
1382 		dvb_dmxdev_release(&av7110->dmxdev1);
1383 		dvb_dmx_release(&av7110->demux1);
1384 	}
1385 
1386 	dvb_net_release(&av7110->dvb_net);
1387 
1388 	dvbdemux->dmx.close(&dvbdemux->dmx);
1389 	dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &av7110->hw_frontend);
1390 	dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &av7110->mem_frontend);
1391 
1392 	dvb_dmxdev_release(&av7110->dmxdev);
1393 	dvb_dmx_release(&av7110->demux);
1394 
1395 	if (av7110->fe != NULL) {
1396 		dvb_unregister_frontend(av7110->fe);
1397 		dvb_frontend_detach(av7110->fe);
1398 	}
1399 	dvb_unregister_device(av7110->osd_dev);
1400 	av7110_av_unregister(av7110);
1401 	av7110_ca_unregister(av7110);
1402 }
1403 
1404 
1405 /****************************************************************************
1406  * I2C client commands
1407  ****************************************************************************/
1408 
i2c_writereg(struct av7110 * av7110,u8 id,u8 reg,u8 val)1409 int i2c_writereg(struct av7110 *av7110, u8 id, u8 reg, u8 val)
1410 {
1411 	u8 msg[2] = { reg, val };
1412 	struct i2c_msg msgs;
1413 
1414 	msgs.flags = 0;
1415 	msgs.addr = id / 2;
1416 	msgs.len = 2;
1417 	msgs.buf = msg;
1418 	return i2c_transfer(&av7110->i2c_adap, &msgs, 1);
1419 }
1420 
i2c_readreg(struct av7110 * av7110,u8 id,u8 reg)1421 u8 i2c_readreg(struct av7110 *av7110, u8 id, u8 reg)
1422 {
1423 	u8 mm1[] = {0x00};
1424 	u8 mm2[] = {0x00};
1425 	struct i2c_msg msgs[2];
1426 
1427 	msgs[0].flags = 0;
1428 	msgs[1].flags = I2C_M_RD;
1429 	msgs[0].addr = msgs[1].addr = id / 2;
1430 	mm1[0] = reg;
1431 	msgs[0].len = 1; msgs[1].len = 1;
1432 	msgs[0].buf = mm1; msgs[1].buf = mm2;
1433 	i2c_transfer(&av7110->i2c_adap, msgs, 2);
1434 
1435 	return mm2[0];
1436 }
1437 
1438 /****************************************************************************
1439  * INITIALIZATION
1440  ****************************************************************************/
1441 
1442 
check_firmware(struct av7110 * av7110)1443 static int check_firmware(struct av7110* av7110)
1444 {
1445 	u32 crc = 0, len = 0;
1446 	unsigned char *ptr;
1447 
1448 	/* check for firmware magic */
1449 	ptr = av7110->bin_fw;
1450 	if (ptr[0] != 'A' || ptr[1] != 'V' ||
1451 	    ptr[2] != 'F' || ptr[3] != 'W') {
1452 		printk("dvb-ttpci: this is not an av7110 firmware\n");
1453 		return -EINVAL;
1454 	}
1455 	ptr += 4;
1456 
1457 	/* check dpram file */
1458 	crc = get_unaligned_be32(ptr);
1459 	ptr += 4;
1460 	len = get_unaligned_be32(ptr);
1461 	ptr += 4;
1462 	if (len >= 512) {
1463 		printk("dvb-ttpci: dpram file is way too big.\n");
1464 		return -EINVAL;
1465 	}
1466 	if (crc != crc32_le(0, ptr, len)) {
1467 		printk("dvb-ttpci: crc32 of dpram file does not match.\n");
1468 		return -EINVAL;
1469 	}
1470 	av7110->bin_dpram = ptr;
1471 	av7110->size_dpram = len;
1472 	ptr += len;
1473 
1474 	/* check root file */
1475 	crc = get_unaligned_be32(ptr);
1476 	ptr += 4;
1477 	len = get_unaligned_be32(ptr);
1478 	ptr += 4;
1479 
1480 	if (len <= 200000 || len >= 300000 ||
1481 	    len > ((av7110->bin_fw + av7110->size_fw) - ptr)) {
1482 		printk("dvb-ttpci: root file has strange size (%d). aborting.\n", len);
1483 		return -EINVAL;
1484 	}
1485 	if( crc != crc32_le(0, ptr, len)) {
1486 		printk("dvb-ttpci: crc32 of root file does not match.\n");
1487 		return -EINVAL;
1488 	}
1489 	av7110->bin_root = ptr;
1490 	av7110->size_root = len;
1491 	return 0;
1492 }
1493 
put_firmware(struct av7110 * av7110)1494 static void put_firmware(struct av7110* av7110)
1495 {
1496 	vfree(av7110->bin_fw);
1497 }
1498 
get_firmware(struct av7110 * av7110)1499 static int get_firmware(struct av7110* av7110)
1500 {
1501 	int ret;
1502 	const struct firmware *fw;
1503 
1504 	/* request the av7110 firmware, this will block until someone uploads it */
1505 	ret = request_firmware(&fw, "dvb-ttpci-01.fw", &av7110->dev->pci->dev);
1506 	if (ret) {
1507 		if (ret == -ENOENT) {
1508 			printk(KERN_ERR "dvb-ttpci: could not load firmware, file not found: dvb-ttpci-01.fw\n");
1509 			printk(KERN_ERR "dvb-ttpci: usually this should be in /usr/lib/hotplug/firmware or /lib/firmware\n");
1510 			printk(KERN_ERR "dvb-ttpci: and can be downloaded from https://linuxtv.org/download/dvb/firmware/\n");
1511 		} else
1512 			printk(KERN_ERR "dvb-ttpci: cannot request firmware (error %i)\n",
1513 			       ret);
1514 		return -EINVAL;
1515 	}
1516 
1517 	if (fw->size <= 200000) {
1518 		printk("dvb-ttpci: this firmware is way too small.\n");
1519 		release_firmware(fw);
1520 		return -EINVAL;
1521 	}
1522 
1523 	/* check if the firmware is available */
1524 	av7110->bin_fw = vmalloc(fw->size);
1525 	if (NULL == av7110->bin_fw) {
1526 		dprintk(1, "out of memory\n");
1527 		release_firmware(fw);
1528 		return -ENOMEM;
1529 	}
1530 
1531 	memcpy(av7110->bin_fw, fw->data, fw->size);
1532 	av7110->size_fw = fw->size;
1533 	if ((ret = check_firmware(av7110)))
1534 		vfree(av7110->bin_fw);
1535 
1536 	release_firmware(fw);
1537 	return ret;
1538 }
1539 
alps_bsrv2_tuner_set_params(struct dvb_frontend * fe)1540 static int alps_bsrv2_tuner_set_params(struct dvb_frontend *fe)
1541 {
1542 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1543 	struct av7110* av7110 = fe->dvb->priv;
1544 	u8 pwr = 0;
1545 	u8 buf[4];
1546 	struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = buf, .len = sizeof(buf) };
1547 	u32 div = (p->frequency + 479500) / 125;
1548 
1549 	if (p->frequency > 2000000)
1550 		pwr = 3;
1551 	else if (p->frequency > 1800000)
1552 		pwr = 2;
1553 	else if (p->frequency > 1600000)
1554 		pwr = 1;
1555 	else if (p->frequency > 1200000)
1556 		pwr = 0;
1557 	else if (p->frequency >= 1100000)
1558 		pwr = 1;
1559 	else
1560 		pwr = 2;
1561 
1562 	buf[0] = (div >> 8) & 0x7f;
1563 	buf[1] = div & 0xff;
1564 	buf[2] = ((div & 0x18000) >> 10) | 0x95;
1565 	buf[3] = (pwr << 6) | 0x30;
1566 
1567 	// NOTE: since we're using a prescaler of 2, we set the
1568 	// divisor frequency to 62.5kHz and divide by 125 above
1569 
1570 	if (fe->ops.i2c_gate_ctrl)
1571 		fe->ops.i2c_gate_ctrl(fe, 1);
1572 	if (i2c_transfer (&av7110->i2c_adap, &msg, 1) != 1)
1573 		return -EIO;
1574 	return 0;
1575 }
1576 
1577 static struct ves1x93_config alps_bsrv2_config = {
1578 	.demod_address = 0x08,
1579 	.xin = 90100000UL,
1580 	.invert_pwm = 0,
1581 };
1582 
alps_tdbe2_tuner_set_params(struct dvb_frontend * fe)1583 static int alps_tdbe2_tuner_set_params(struct dvb_frontend *fe)
1584 {
1585 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1586 	struct av7110* av7110 = fe->dvb->priv;
1587 	u32 div;
1588 	u8 data[4];
1589 	struct i2c_msg msg = { .addr = 0x62, .flags = 0, .buf = data, .len = sizeof(data) };
1590 
1591 	div = (p->frequency + 35937500 + 31250) / 62500;
1592 
1593 	data[0] = (div >> 8) & 0x7f;
1594 	data[1] = div & 0xff;
1595 	data[2] = 0x85 | ((div >> 10) & 0x60);
1596 	data[3] = (p->frequency < 174000000 ? 0x88 : p->frequency < 470000000 ? 0x84 : 0x81);
1597 
1598 	if (fe->ops.i2c_gate_ctrl)
1599 		fe->ops.i2c_gate_ctrl(fe, 1);
1600 	if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1601 		return -EIO;
1602 	return 0;
1603 }
1604 
1605 static struct ves1820_config alps_tdbe2_config = {
1606 	.demod_address = 0x09,
1607 	.xin = 57840000UL,
1608 	.invert = 1,
1609 	.selagc = VES1820_SELAGC_SIGNAMPERR,
1610 };
1611 
1612 
1613 
1614 
grundig_29504_451_tuner_set_params(struct dvb_frontend * fe)1615 static int grundig_29504_451_tuner_set_params(struct dvb_frontend *fe)
1616 {
1617 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1618 	struct av7110* av7110 = fe->dvb->priv;
1619 	u32 div;
1620 	u8 data[4];
1621 	struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
1622 
1623 	div = p->frequency / 125;
1624 	data[0] = (div >> 8) & 0x7f;
1625 	data[1] = div & 0xff;
1626 	data[2] = 0x8e;
1627 	data[3] = 0x00;
1628 
1629 	if (fe->ops.i2c_gate_ctrl)
1630 		fe->ops.i2c_gate_ctrl(fe, 1);
1631 	if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1632 		return -EIO;
1633 	return 0;
1634 }
1635 
1636 static struct tda8083_config grundig_29504_451_config = {
1637 	.demod_address = 0x68,
1638 };
1639 
1640 
1641 
philips_cd1516_tuner_set_params(struct dvb_frontend * fe)1642 static int philips_cd1516_tuner_set_params(struct dvb_frontend *fe)
1643 {
1644 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1645 	struct av7110* av7110 = fe->dvb->priv;
1646 	u32 div;
1647 	u32 f = p->frequency;
1648 	u8 data[4];
1649 	struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
1650 
1651 	div = (f + 36125000 + 31250) / 62500;
1652 
1653 	data[0] = (div >> 8) & 0x7f;
1654 	data[1] = div & 0xff;
1655 	data[2] = 0x8e;
1656 	data[3] = (f < 174000000 ? 0xa1 : f < 470000000 ? 0x92 : 0x34);
1657 
1658 	if (fe->ops.i2c_gate_ctrl)
1659 		fe->ops.i2c_gate_ctrl(fe, 1);
1660 	if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1661 		return -EIO;
1662 	return 0;
1663 }
1664 
1665 static struct ves1820_config philips_cd1516_config = {
1666 	.demod_address = 0x09,
1667 	.xin = 57840000UL,
1668 	.invert = 1,
1669 	.selagc = VES1820_SELAGC_SIGNAMPERR,
1670 };
1671 
1672 
1673 
alps_tdlb7_tuner_set_params(struct dvb_frontend * fe)1674 static int alps_tdlb7_tuner_set_params(struct dvb_frontend *fe)
1675 {
1676 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1677 	struct av7110* av7110 = fe->dvb->priv;
1678 	u32 div, pwr;
1679 	u8 data[4];
1680 	struct i2c_msg msg = { .addr = 0x60, .flags = 0, .buf = data, .len = sizeof(data) };
1681 
1682 	div = (p->frequency + 36200000) / 166666;
1683 
1684 	if (p->frequency <= 782000000)
1685 		pwr = 1;
1686 	else
1687 		pwr = 2;
1688 
1689 	data[0] = (div >> 8) & 0x7f;
1690 	data[1] = div & 0xff;
1691 	data[2] = 0x85;
1692 	data[3] = pwr << 6;
1693 
1694 	if (fe->ops.i2c_gate_ctrl)
1695 		fe->ops.i2c_gate_ctrl(fe, 1);
1696 	if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1)
1697 		return -EIO;
1698 	return 0;
1699 }
1700 
alps_tdlb7_request_firmware(struct dvb_frontend * fe,const struct firmware ** fw,char * name)1701 static int alps_tdlb7_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
1702 {
1703 #if IS_ENABLED(CONFIG_DVB_SP8870)
1704 	struct av7110* av7110 = fe->dvb->priv;
1705 
1706 	return request_firmware(fw, name, &av7110->dev->pci->dev);
1707 #else
1708 	return -EINVAL;
1709 #endif
1710 }
1711 
1712 static const struct sp8870_config alps_tdlb7_config = {
1713 
1714 	.demod_address = 0x71,
1715 	.request_firmware = alps_tdlb7_request_firmware,
1716 };
1717 
1718 
1719 static u8 nexusca_stv0297_inittab[] = {
1720 	0x80, 0x01,
1721 	0x80, 0x00,
1722 	0x81, 0x01,
1723 	0x81, 0x00,
1724 	0x00, 0x09,
1725 	0x01, 0x69,
1726 	0x03, 0x00,
1727 	0x04, 0x00,
1728 	0x07, 0x00,
1729 	0x08, 0x00,
1730 	0x20, 0x00,
1731 	0x21, 0x40,
1732 	0x22, 0x00,
1733 	0x23, 0x00,
1734 	0x24, 0x40,
1735 	0x25, 0x88,
1736 	0x30, 0xff,
1737 	0x31, 0x00,
1738 	0x32, 0xff,
1739 	0x33, 0x00,
1740 	0x34, 0x50,
1741 	0x35, 0x7f,
1742 	0x36, 0x00,
1743 	0x37, 0x20,
1744 	0x38, 0x00,
1745 	0x40, 0x1c,
1746 	0x41, 0xff,
1747 	0x42, 0x29,
1748 	0x43, 0x00,
1749 	0x44, 0xff,
1750 	0x45, 0x00,
1751 	0x46, 0x00,
1752 	0x49, 0x04,
1753 	0x4a, 0x00,
1754 	0x4b, 0x7b,
1755 	0x52, 0x30,
1756 	0x55, 0xae,
1757 	0x56, 0x47,
1758 	0x57, 0xe1,
1759 	0x58, 0x3a,
1760 	0x5a, 0x1e,
1761 	0x5b, 0x34,
1762 	0x60, 0x00,
1763 	0x63, 0x00,
1764 	0x64, 0x00,
1765 	0x65, 0x00,
1766 	0x66, 0x00,
1767 	0x67, 0x00,
1768 	0x68, 0x00,
1769 	0x69, 0x00,
1770 	0x6a, 0x02,
1771 	0x6b, 0x00,
1772 	0x70, 0xff,
1773 	0x71, 0x00,
1774 	0x72, 0x00,
1775 	0x73, 0x00,
1776 	0x74, 0x0c,
1777 	0x80, 0x00,
1778 	0x81, 0x00,
1779 	0x82, 0x00,
1780 	0x83, 0x00,
1781 	0x84, 0x04,
1782 	0x85, 0x80,
1783 	0x86, 0x24,
1784 	0x87, 0x78,
1785 	0x88, 0x10,
1786 	0x89, 0x00,
1787 	0x90, 0x01,
1788 	0x91, 0x01,
1789 	0xa0, 0x04,
1790 	0xa1, 0x00,
1791 	0xa2, 0x00,
1792 	0xb0, 0x91,
1793 	0xb1, 0x0b,
1794 	0xc0, 0x53,
1795 	0xc1, 0x70,
1796 	0xc2, 0x12,
1797 	0xd0, 0x00,
1798 	0xd1, 0x00,
1799 	0xd2, 0x00,
1800 	0xd3, 0x00,
1801 	0xd4, 0x00,
1802 	0xd5, 0x00,
1803 	0xde, 0x00,
1804 	0xdf, 0x00,
1805 	0x61, 0x49,
1806 	0x62, 0x0b,
1807 	0x53, 0x08,
1808 	0x59, 0x08,
1809 	0xff, 0xff,
1810 };
1811 
nexusca_stv0297_tuner_set_params(struct dvb_frontend * fe)1812 static int nexusca_stv0297_tuner_set_params(struct dvb_frontend *fe)
1813 {
1814 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1815 	struct av7110* av7110 = fe->dvb->priv;
1816 	u32 div;
1817 	u8 data[4];
1818 	struct i2c_msg msg = { .addr = 0x63, .flags = 0, .buf = data, .len = sizeof(data) };
1819 	struct i2c_msg readmsg = { .addr = 0x63, .flags = I2C_M_RD, .buf = data, .len = 1 };
1820 	int i;
1821 
1822 	div = (p->frequency + 36150000 + 31250) / 62500;
1823 
1824 	data[0] = (div >> 8) & 0x7f;
1825 	data[1] = div & 0xff;
1826 	data[2] = 0xce;
1827 
1828 	if (p->frequency < 45000000)
1829 		return -EINVAL;
1830 	else if (p->frequency < 137000000)
1831 		data[3] = 0x01;
1832 	else if (p->frequency < 403000000)
1833 		data[3] = 0x02;
1834 	else if (p->frequency < 860000000)
1835 		data[3] = 0x04;
1836 	else
1837 		return -EINVAL;
1838 
1839 	if (fe->ops.i2c_gate_ctrl)
1840 		fe->ops.i2c_gate_ctrl(fe, 1);
1841 	if (i2c_transfer(&av7110->i2c_adap, &msg, 1) != 1) {
1842 		printk("nexusca: pll transfer failed!\n");
1843 		return -EIO;
1844 	}
1845 
1846 	// wait for PLL lock
1847 	for(i = 0; i < 20; i++) {
1848 		if (fe->ops.i2c_gate_ctrl)
1849 			fe->ops.i2c_gate_ctrl(fe, 1);
1850 		if (i2c_transfer(&av7110->i2c_adap, &readmsg, 1) == 1)
1851 			if (data[0] & 0x40) break;
1852 		msleep(10);
1853 	}
1854 
1855 	return 0;
1856 }
1857 
1858 static struct stv0297_config nexusca_stv0297_config = {
1859 
1860 	.demod_address = 0x1C,
1861 	.inittab = nexusca_stv0297_inittab,
1862 	.invert = 1,
1863 	.stop_during_read = 1,
1864 };
1865 
1866 
1867 
grundig_29504_401_tuner_set_params(struct dvb_frontend * fe)1868 static int grundig_29504_401_tuner_set_params(struct dvb_frontend *fe)
1869 {
1870 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1871 	struct av7110* av7110 = fe->dvb->priv;
1872 	u32 div;
1873 	u8 cfg, cpump, band_select;
1874 	u8 data[4];
1875 	struct i2c_msg msg = { .addr = 0x61, .flags = 0, .buf = data, .len = sizeof(data) };
1876 
1877 	div = (36125000 + p->frequency) / 166666;
1878 
1879 	cfg = 0x88;
1880 
1881 	if (p->frequency < 175000000)
1882 		cpump = 2;
1883 	else if (p->frequency < 390000000)
1884 		cpump = 1;
1885 	else if (p->frequency < 470000000)
1886 		cpump = 2;
1887 	else if (p->frequency < 750000000)
1888 		cpump = 1;
1889 	else
1890 		cpump = 3;
1891 
1892 	if (p->frequency < 175000000)
1893 		band_select = 0x0e;
1894 	else if (p->frequency < 470000000)
1895 		band_select = 0x05;
1896 	else
1897 		band_select = 0x03;
1898 
1899 	data[0] = (div >> 8) & 0x7f;
1900 	data[1] = div & 0xff;
1901 	data[2] = ((div >> 10) & 0x60) | cfg;
1902 	data[3] = (cpump << 6) | band_select;
1903 
1904 	if (fe->ops.i2c_gate_ctrl)
1905 		fe->ops.i2c_gate_ctrl(fe, 1);
1906 	if (i2c_transfer (&av7110->i2c_adap, &msg, 1) != 1) return -EIO;
1907 	return 0;
1908 }
1909 
1910 static struct l64781_config grundig_29504_401_config = {
1911 	.demod_address = 0x55,
1912 };
1913 
1914 
1915 
av7110_fe_lock_fix(struct av7110 * av7110,enum fe_status status)1916 static int av7110_fe_lock_fix(struct av7110 *av7110, enum fe_status status)
1917 {
1918 	int ret = 0;
1919 	int synced = (status & FE_HAS_LOCK) ? 1 : 0;
1920 
1921 	av7110->fe_status = status;
1922 
1923 	if (av7110->fe_synced == synced)
1924 		return 0;
1925 
1926 	if (av7110->playing) {
1927 		av7110->fe_synced = synced;
1928 		return 0;
1929 	}
1930 
1931 	if (mutex_lock_interruptible(&av7110->pid_mutex))
1932 		return -ERESTARTSYS;
1933 
1934 	if (synced) {
1935 		ret = SetPIDs(av7110, av7110->pids[DMX_PES_VIDEO],
1936 			av7110->pids[DMX_PES_AUDIO],
1937 			av7110->pids[DMX_PES_TELETEXT], 0,
1938 			av7110->pids[DMX_PES_PCR]);
1939 		if (!ret)
1940 			ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
1941 	} else {
1942 		ret = SetPIDs(av7110, 0, 0, 0, 0, 0);
1943 		if (!ret) {
1944 			ret = av7110_fw_cmd(av7110, COMTYPE_PID_FILTER, FlushTSQueue, 0);
1945 			if (!ret)
1946 				ret = av7110_wait_msgstate(av7110, GPMQBusy);
1947 		}
1948 	}
1949 
1950 	if (!ret)
1951 		av7110->fe_synced = synced;
1952 
1953 	mutex_unlock(&av7110->pid_mutex);
1954 	return ret;
1955 }
1956 
av7110_fe_set_frontend(struct dvb_frontend * fe)1957 static int av7110_fe_set_frontend(struct dvb_frontend *fe)
1958 {
1959 	struct av7110* av7110 = fe->dvb->priv;
1960 
1961 	int ret = av7110_fe_lock_fix(av7110, 0);
1962 	if (!ret)
1963 		ret = av7110->fe_set_frontend(fe);
1964 
1965 	return ret;
1966 }
1967 
av7110_fe_init(struct dvb_frontend * fe)1968 static int av7110_fe_init(struct dvb_frontend* fe)
1969 {
1970 	struct av7110* av7110 = fe->dvb->priv;
1971 
1972 	int ret = av7110_fe_lock_fix(av7110, 0);
1973 	if (!ret)
1974 		ret = av7110->fe_init(fe);
1975 	return ret;
1976 }
1977 
av7110_fe_read_status(struct dvb_frontend * fe,enum fe_status * status)1978 static int av7110_fe_read_status(struct dvb_frontend *fe,
1979 				 enum fe_status *status)
1980 {
1981 	struct av7110* av7110 = fe->dvb->priv;
1982 
1983 	/* call the real implementation */
1984 	int ret = av7110->fe_read_status(fe, status);
1985 	if (!ret)
1986 		if (((*status ^ av7110->fe_status) & FE_HAS_LOCK) && (*status & FE_HAS_LOCK))
1987 			ret = av7110_fe_lock_fix(av7110, *status);
1988 	return ret;
1989 }
1990 
av7110_fe_diseqc_reset_overload(struct dvb_frontend * fe)1991 static int av7110_fe_diseqc_reset_overload(struct dvb_frontend* fe)
1992 {
1993 	struct av7110* av7110 = fe->dvb->priv;
1994 
1995 	int ret = av7110_fe_lock_fix(av7110, 0);
1996 	if (!ret)
1997 		ret = av7110->fe_diseqc_reset_overload(fe);
1998 	return ret;
1999 }
2000 
av7110_fe_diseqc_send_master_cmd(struct dvb_frontend * fe,struct dvb_diseqc_master_cmd * cmd)2001 static int av7110_fe_diseqc_send_master_cmd(struct dvb_frontend* fe,
2002 					    struct dvb_diseqc_master_cmd* cmd)
2003 {
2004 	struct av7110* av7110 = fe->dvb->priv;
2005 
2006 	int ret = av7110_fe_lock_fix(av7110, 0);
2007 	if (!ret) {
2008 		av7110->saved_master_cmd = *cmd;
2009 		ret = av7110->fe_diseqc_send_master_cmd(fe, cmd);
2010 	}
2011 	return ret;
2012 }
2013 
av7110_fe_diseqc_send_burst(struct dvb_frontend * fe,enum fe_sec_mini_cmd minicmd)2014 static int av7110_fe_diseqc_send_burst(struct dvb_frontend *fe,
2015 				       enum fe_sec_mini_cmd minicmd)
2016 {
2017 	struct av7110* av7110 = fe->dvb->priv;
2018 
2019 	int ret = av7110_fe_lock_fix(av7110, 0);
2020 	if (!ret) {
2021 		av7110->saved_minicmd = minicmd;
2022 		ret = av7110->fe_diseqc_send_burst(fe, minicmd);
2023 	}
2024 	return ret;
2025 }
2026 
av7110_fe_set_tone(struct dvb_frontend * fe,enum fe_sec_tone_mode tone)2027 static int av7110_fe_set_tone(struct dvb_frontend *fe,
2028 			      enum fe_sec_tone_mode tone)
2029 {
2030 	struct av7110* av7110 = fe->dvb->priv;
2031 
2032 	int ret = av7110_fe_lock_fix(av7110, 0);
2033 	if (!ret) {
2034 		av7110->saved_tone = tone;
2035 		ret = av7110->fe_set_tone(fe, tone);
2036 	}
2037 	return ret;
2038 }
2039 
av7110_fe_set_voltage(struct dvb_frontend * fe,enum fe_sec_voltage voltage)2040 static int av7110_fe_set_voltage(struct dvb_frontend *fe,
2041 				 enum fe_sec_voltage voltage)
2042 {
2043 	struct av7110* av7110 = fe->dvb->priv;
2044 
2045 	int ret = av7110_fe_lock_fix(av7110, 0);
2046 	if (!ret) {
2047 		av7110->saved_voltage = voltage;
2048 		ret = av7110->fe_set_voltage(fe, voltage);
2049 	}
2050 	return ret;
2051 }
2052 
av7110_fe_dishnetwork_send_legacy_command(struct dvb_frontend * fe,unsigned long cmd)2053 static int av7110_fe_dishnetwork_send_legacy_command(struct dvb_frontend* fe, unsigned long cmd)
2054 {
2055 	struct av7110* av7110 = fe->dvb->priv;
2056 
2057 	int ret = av7110_fe_lock_fix(av7110, 0);
2058 	if (!ret)
2059 		ret = av7110->fe_dishnetwork_send_legacy_command(fe, cmd);
2060 	return ret;
2061 }
2062 
dvb_s_recover(struct av7110 * av7110)2063 static void dvb_s_recover(struct av7110* av7110)
2064 {
2065 	av7110_fe_init(av7110->fe);
2066 
2067 	av7110_fe_set_voltage(av7110->fe, av7110->saved_voltage);
2068 	if (av7110->saved_master_cmd.msg_len) {
2069 		msleep(20);
2070 		av7110_fe_diseqc_send_master_cmd(av7110->fe, &av7110->saved_master_cmd);
2071 	}
2072 	msleep(20);
2073 	av7110_fe_diseqc_send_burst(av7110->fe, av7110->saved_minicmd);
2074 	msleep(20);
2075 	av7110_fe_set_tone(av7110->fe, av7110->saved_tone);
2076 
2077 	av7110_fe_set_frontend(av7110->fe);
2078 }
2079 
read_pwm(struct av7110 * av7110)2080 static u8 read_pwm(struct av7110* av7110)
2081 {
2082 	u8 b = 0xff;
2083 	u8 pwm;
2084 	struct i2c_msg msg[] = { { .addr = 0x50,.flags = 0,.buf = &b,.len = 1 },
2085 				 { .addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1} };
2086 
2087 	if ((i2c_transfer(&av7110->i2c_adap, msg, 2) != 2) || (pwm == 0xff))
2088 		pwm = 0x48;
2089 
2090 	return pwm;
2091 }
2092 
frontend_init(struct av7110 * av7110)2093 static int frontend_init(struct av7110 *av7110)
2094 {
2095 	int ret;
2096 
2097 	if (av7110->dev->pci->subsystem_vendor == 0x110a) {
2098 		switch(av7110->dev->pci->subsystem_device) {
2099 		case 0x0000: // Fujitsu/Siemens DVB-Cable (ves1820/Philips CD1516(??))
2100 			av7110->fe = dvb_attach(ves1820_attach, &philips_cd1516_config,
2101 						    &av7110->i2c_adap, read_pwm(av7110));
2102 			if (av7110->fe) {
2103 				av7110->fe->ops.tuner_ops.set_params = philips_cd1516_tuner_set_params;
2104 			}
2105 			break;
2106 		}
2107 
2108 	} else if (av7110->dev->pci->subsystem_vendor == 0x13c2) {
2109 		switch(av7110->dev->pci->subsystem_device) {
2110 		case 0x0000: // Hauppauge/TT WinTV DVB-S rev1.X
2111 		case 0x0003: // Hauppauge/TT WinTV Nexus-S Rev 2.X
2112 		case 0x1002: // Hauppauge/TT WinTV DVB-S rev1.3SE
2113 
2114 			// try the ALPS BSRV2 first of all
2115 			av7110->fe = dvb_attach(ves1x93_attach, &alps_bsrv2_config, &av7110->i2c_adap);
2116 			if (av7110->fe) {
2117 				av7110->fe->ops.tuner_ops.set_params = alps_bsrv2_tuner_set_params;
2118 				av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2119 				av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2120 				av7110->fe->ops.set_tone = av7110_set_tone;
2121 				av7110->recover = dvb_s_recover;
2122 				break;
2123 			}
2124 
2125 			// try the ALPS BSRU6 now
2126 			av7110->fe = dvb_attach(stv0299_attach, &alps_bsru6_config, &av7110->i2c_adap);
2127 			if (av7110->fe) {
2128 				av7110->fe->ops.tuner_ops.set_params = alps_bsru6_tuner_set_params;
2129 				av7110->fe->tuner_priv = &av7110->i2c_adap;
2130 
2131 				av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2132 				av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2133 				av7110->fe->ops.set_tone = av7110_set_tone;
2134 				av7110->recover = dvb_s_recover;
2135 				break;
2136 			}
2137 
2138 			// Try the grundig 29504-451
2139 			av7110->fe = dvb_attach(tda8083_attach, &grundig_29504_451_config, &av7110->i2c_adap);
2140 			if (av7110->fe) {
2141 				av7110->fe->ops.tuner_ops.set_params = grundig_29504_451_tuner_set_params;
2142 				av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2143 				av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2144 				av7110->fe->ops.set_tone = av7110_set_tone;
2145 				av7110->recover = dvb_s_recover;
2146 				break;
2147 			}
2148 
2149 			/* Try DVB-C cards */
2150 			switch(av7110->dev->pci->subsystem_device) {
2151 			case 0x0000:
2152 				/* Siemens DVB-C (full-length card) VES1820/Philips CD1516 */
2153 				av7110->fe = dvb_attach(ves1820_attach, &philips_cd1516_config, &av7110->i2c_adap,
2154 							read_pwm(av7110));
2155 				if (av7110->fe) {
2156 					av7110->fe->ops.tuner_ops.set_params = philips_cd1516_tuner_set_params;
2157 				}
2158 				break;
2159 			case 0x0003:
2160 				/* Hauppauge DVB-C 2.1 VES1820/ALPS TDBE2 */
2161 				av7110->fe = dvb_attach(ves1820_attach, &alps_tdbe2_config, &av7110->i2c_adap,
2162 							read_pwm(av7110));
2163 				if (av7110->fe) {
2164 					av7110->fe->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
2165 				}
2166 				break;
2167 			}
2168 			break;
2169 
2170 		case 0x0001: // Hauppauge/TT Nexus-T premium rev1.X
2171 		{
2172 			struct dvb_frontend *fe;
2173 
2174 			// try ALPS TDLB7 first, then Grundig 29504-401
2175 			fe = dvb_attach(sp8870_attach, &alps_tdlb7_config, &av7110->i2c_adap);
2176 			if (fe) {
2177 				fe->ops.tuner_ops.set_params = alps_tdlb7_tuner_set_params;
2178 				av7110->fe = fe;
2179 				break;
2180 			}
2181 		}
2182 			fallthrough;
2183 
2184 		case 0x0008: // Hauppauge/TT DVB-T
2185 			// Grundig 29504-401
2186 			av7110->fe = dvb_attach(l64781_attach, &grundig_29504_401_config, &av7110->i2c_adap);
2187 			if (av7110->fe)
2188 				av7110->fe->ops.tuner_ops.set_params = grundig_29504_401_tuner_set_params;
2189 			break;
2190 
2191 		case 0x0002: // Hauppauge/TT DVB-C premium rev2.X
2192 
2193 			av7110->fe = dvb_attach(ves1820_attach, &alps_tdbe2_config, &av7110->i2c_adap, read_pwm(av7110));
2194 			if (av7110->fe) {
2195 				av7110->fe->ops.tuner_ops.set_params = alps_tdbe2_tuner_set_params;
2196 			}
2197 			break;
2198 
2199 		case 0x0004: // Galaxis DVB-S rev1.3
2200 			/* ALPS BSRV2 */
2201 			av7110->fe = dvb_attach(ves1x93_attach, &alps_bsrv2_config, &av7110->i2c_adap);
2202 			if (av7110->fe) {
2203 				av7110->fe->ops.tuner_ops.set_params = alps_bsrv2_tuner_set_params;
2204 				av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2205 				av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2206 				av7110->fe->ops.set_tone = av7110_set_tone;
2207 				av7110->recover = dvb_s_recover;
2208 			}
2209 			break;
2210 
2211 		case 0x0006: /* Fujitsu-Siemens DVB-S rev 1.6 */
2212 			/* Grundig 29504-451 */
2213 			av7110->fe = dvb_attach(tda8083_attach, &grundig_29504_451_config, &av7110->i2c_adap);
2214 			if (av7110->fe) {
2215 				av7110->fe->ops.tuner_ops.set_params = grundig_29504_451_tuner_set_params;
2216 				av7110->fe->ops.diseqc_send_master_cmd = av7110_diseqc_send_master_cmd;
2217 				av7110->fe->ops.diseqc_send_burst = av7110_diseqc_send_burst;
2218 				av7110->fe->ops.set_tone = av7110_set_tone;
2219 				av7110->recover = dvb_s_recover;
2220 			}
2221 			break;
2222 
2223 		case 0x000A: // Hauppauge/TT Nexus-CA rev1.X
2224 
2225 			av7110->fe = dvb_attach(stv0297_attach, &nexusca_stv0297_config, &av7110->i2c_adap);
2226 			if (av7110->fe) {
2227 				av7110->fe->ops.tuner_ops.set_params = nexusca_stv0297_tuner_set_params;
2228 
2229 				/* set TDA9819 into DVB mode */
2230 				saa7146_setgpio(av7110->dev, 1, SAA7146_GPIO_OUTLO); // TDA9819 pin9(STD)
2231 				saa7146_setgpio(av7110->dev, 3, SAA7146_GPIO_OUTLO); // TDA9819 pin30(VIF)
2232 
2233 				/* tuner on this needs a slower i2c bus speed */
2234 				av7110->dev->i2c_bitrate = SAA7146_I2C_BUS_BIT_RATE_240;
2235 				break;
2236 			}
2237 			break;
2238 
2239 		case 0x000E: /* Hauppauge/TT Nexus-S rev 2.3 */
2240 			/* ALPS BSBE1 */
2241 			av7110->fe = dvb_attach(stv0299_attach, &alps_bsbe1_config, &av7110->i2c_adap);
2242 			if (av7110->fe) {
2243 				av7110->fe->ops.tuner_ops.set_params = alps_bsbe1_tuner_set_params;
2244 				av7110->fe->tuner_priv = &av7110->i2c_adap;
2245 
2246 				if (dvb_attach(lnbp21_attach, av7110->fe, &av7110->i2c_adap, 0, 0) == NULL) {
2247 					printk("dvb-ttpci: LNBP21 not found!\n");
2248 					if (av7110->fe->ops.release)
2249 						av7110->fe->ops.release(av7110->fe);
2250 					av7110->fe = NULL;
2251 				} else {
2252 					av7110->fe->ops.dishnetwork_send_legacy_command = NULL;
2253 					av7110->recover = dvb_s_recover;
2254 				}
2255 			}
2256 			break;
2257 		}
2258 	}
2259 
2260 	if (!av7110->fe) {
2261 		/* FIXME: propagate the failure code from the lower layers */
2262 		ret = -ENOMEM;
2263 		printk("dvb-ttpci: A frontend driver was not found for device [%04x:%04x] subsystem [%04x:%04x]\n",
2264 		       av7110->dev->pci->vendor,
2265 		       av7110->dev->pci->device,
2266 		       av7110->dev->pci->subsystem_vendor,
2267 		       av7110->dev->pci->subsystem_device);
2268 	} else {
2269 		FE_FUNC_OVERRIDE(av7110->fe->ops.init, av7110->fe_init, av7110_fe_init);
2270 		FE_FUNC_OVERRIDE(av7110->fe->ops.read_status, av7110->fe_read_status, av7110_fe_read_status);
2271 		FE_FUNC_OVERRIDE(av7110->fe->ops.diseqc_reset_overload, av7110->fe_diseqc_reset_overload, av7110_fe_diseqc_reset_overload);
2272 		FE_FUNC_OVERRIDE(av7110->fe->ops.diseqc_send_master_cmd, av7110->fe_diseqc_send_master_cmd, av7110_fe_diseqc_send_master_cmd);
2273 		FE_FUNC_OVERRIDE(av7110->fe->ops.diseqc_send_burst, av7110->fe_diseqc_send_burst, av7110_fe_diseqc_send_burst);
2274 		FE_FUNC_OVERRIDE(av7110->fe->ops.set_tone, av7110->fe_set_tone, av7110_fe_set_tone);
2275 		FE_FUNC_OVERRIDE(av7110->fe->ops.set_voltage, av7110->fe_set_voltage, av7110_fe_set_voltage);
2276 		FE_FUNC_OVERRIDE(av7110->fe->ops.dishnetwork_send_legacy_command, av7110->fe_dishnetwork_send_legacy_command, av7110_fe_dishnetwork_send_legacy_command);
2277 		FE_FUNC_OVERRIDE(av7110->fe->ops.set_frontend, av7110->fe_set_frontend, av7110_fe_set_frontend);
2278 
2279 		ret = dvb_register_frontend(&av7110->dvb_adapter, av7110->fe);
2280 		if (ret < 0) {
2281 			printk("av7110: Frontend registration failed!\n");
2282 			dvb_frontend_detach(av7110->fe);
2283 			av7110->fe = NULL;
2284 		}
2285 	}
2286 	return ret;
2287 }
2288 
2289 /* Budgetpatch note:
2290  * Original hardware design by Roberto Deza:
2291  * There is a DVB_Wiki at
2292  * https://linuxtv.org
2293  *
2294  * New software triggering design by Emard that works on
2295  * original Roberto Deza's hardware:
2296  *
2297  * rps1 code for budgetpatch will copy internal HS event to GPIO3 pin.
2298  * GPIO3 is in budget-patch hardware connectd to port B VSYNC
2299  * HS is an internal event of 7146, accessible with RPS
2300  * and temporarily raised high every n lines
2301  * (n in defined in the RPS_THRESH1 counter threshold)
2302  * I think HS is raised high on the beginning of the n-th line
2303  * and remains high until this n-th line that triggered
2304  * it is completely received. When the reception of n-th line
2305  * ends, HS is lowered.
2306  *
2307  * To transmit data over DMA, 7146 needs changing state at
2308  * port B VSYNC pin. Any changing of port B VSYNC will
2309  * cause some DMA data transfer, with more or less packets loss.
2310  * It depends on the phase and frequency of VSYNC and
2311  * the way of 7146 is instructed to trigger on port B (defined
2312  * in DD1_INIT register, 3rd nibble from the right valid
2313  * numbers are 0-7, see datasheet)
2314  *
2315  * The correct triggering can minimize packet loss,
2316  * dvbtraffic should give this stable bandwidths:
2317  *   22k transponder = 33814 kbit/s
2318  * 27.5k transponder = 38045 kbit/s
2319  * by experiment it is found that the best results
2320  * (stable bandwidths and almost no packet loss)
2321  * are obtained using DD1_INIT triggering number 2
2322  * (Va at rising edge of VS Fa = HS x VS-failing forced toggle)
2323  * and a VSYNC phase that occurs in the middle of DMA transfer
2324  * (about byte 188*512=96256 in the DMA window).
2325  *
2326  * Phase of HS is still not clear to me how to control,
2327  * It just happens to be so. It can be seen if one enables
2328  * RPS_IRQ and print Event Counter 1 in vpeirq(). Every
2329  * time RPS_INTERRUPT is called, the Event Counter 1 will
2330  * increment. That's how the 7146 is programmed to do event
2331  * counting in this budget-patch.c
2332  * I *think* HPS setting has something to do with the phase
2333  * of HS but I can't be 100% sure in that.
2334  *
2335  * hardware debug note: a working budget card (including budget patch)
2336  * with vpeirq() interrupt setup in mode "0x90" (every 64K) will
2337  * generate 3 interrupts per 25-Hz DMA frame of 2*188*512 bytes
2338  * and that means 3*25=75 Hz of interrupt frequency, as seen by
2339  * watch cat /proc/interrupts
2340  *
2341  * If this frequency is 3x lower (and data received in the DMA
2342  * buffer don't start with 0x47, but in the middle of packets,
2343  * whose lengths appear to be like 188 292 188 104 etc.
2344  * this means VSYNC line is not connected in the hardware.
2345  * (check soldering pcb and pins)
2346  * The same behaviour of missing VSYNC can be duplicated on budget
2347  * cards, by setting DD1_INIT trigger mode 7 in 3rd nibble.
2348  */
av7110_attach(struct saa7146_dev * dev,struct saa7146_pci_extension_data * pci_ext)2349 static int av7110_attach(struct saa7146_dev* dev,
2350 			 struct saa7146_pci_extension_data *pci_ext)
2351 {
2352 	const int length = TS_WIDTH * TS_HEIGHT;
2353 	struct pci_dev *pdev = dev->pci;
2354 	struct av7110 *av7110;
2355 	struct task_struct *thread;
2356 	int ret, count = 0;
2357 
2358 	dprintk(4, "dev: %p\n", dev);
2359 
2360 	/* Set RPS_IRQ to 1 to track rps1 activity.
2361 	 * Enabling this won't send any interrupt to PC CPU.
2362 	 */
2363 #define RPS_IRQ 0
2364 
2365 	if (budgetpatch == 1) {
2366 		budgetpatch = 0;
2367 		/* autodetect the presence of budget patch
2368 		 * this only works if saa7146 has been recently
2369 		 * reset with MASK_31 to MC1
2370 		 *
2371 		 * will wait for VBI_B event (vertical blank at port B)
2372 		 * and will reset GPIO3 after VBI_B is detected.
2373 		 * (GPIO3 should be raised high by CPU to
2374 		 * test if GPIO3 will generate vertical blank signal
2375 		 * in budget patch GPIO3 is connected to VSYNC_B
2376 		 */
2377 
2378 		/* RESET SAA7146 */
2379 		saa7146_write(dev, MC1, MASK_31);
2380 		/* autodetection success seems to be time-dependend after reset */
2381 
2382 		/* Fix VSYNC level */
2383 		saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
2384 		/* set vsync_b triggering */
2385 		saa7146_write(dev, DD1_STREAM_B, 0);
2386 		/* port B VSYNC at rising edge */
2387 		saa7146_write(dev, DD1_INIT, 0x00000200);
2388 		saa7146_write(dev, BRS_CTRL, 0x00000000);  // VBI
2389 		saa7146_write(dev, MC2,
2390 			      1 * (MASK_08 | MASK_24)  |   // BRS control
2391 			      0 * (MASK_09 | MASK_25)  |   // a
2392 			      1 * (MASK_10 | MASK_26)  |   // b
2393 			      0 * (MASK_06 | MASK_22)  |   // HPS_CTRL1
2394 			      0 * (MASK_05 | MASK_21)  |   // HPS_CTRL2
2395 			      0 * (MASK_01 | MASK_15)      // DEBI
2396 		);
2397 
2398 		/* start writing RPS1 code from beginning */
2399 		count = 0;
2400 		/* Disable RPS1 */
2401 		saa7146_write(dev, MC1, MASK_29);
2402 		/* RPS1 timeout disable */
2403 		saa7146_write(dev, RPS_TOV1, 0);
2404 		WRITE_RPS1(CMD_PAUSE | EVT_VBI_B);
2405 		WRITE_RPS1(CMD_WR_REG_MASK | (GPIO_CTRL>>2));
2406 		WRITE_RPS1(GPIO3_MSK);
2407 		WRITE_RPS1(SAA7146_GPIO_OUTLO<<24);
2408 #if RPS_IRQ
2409 		/* issue RPS1 interrupt to increment counter */
2410 		WRITE_RPS1(CMD_INTERRUPT);
2411 #endif
2412 		WRITE_RPS1(CMD_STOP);
2413 		/* Jump to begin of RPS program as safety measure               (p37) */
2414 		WRITE_RPS1(CMD_JUMP);
2415 		WRITE_RPS1(dev->d_rps1.dma_handle);
2416 
2417 #if RPS_IRQ
2418 		/* set event counter 1 source as RPS1 interrupt (0x03)          (rE4 p53)
2419 		 * use 0x03 to track RPS1 interrupts - increase by 1 every gpio3 is toggled
2420 		 * use 0x15 to track VPE  interrupts - increase by 1 every vpeirq() is called
2421 		 */
2422 		saa7146_write(dev, EC1SSR, (0x03<<2) | 3 );
2423 		/* set event counter 1 threshold to maximum allowed value        (rEC p55) */
2424 		saa7146_write(dev, ECT1R,  0x3fff );
2425 #endif
2426 		/* Set RPS1 Address register to point to RPS code               (r108 p42) */
2427 		saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
2428 		/* Enable RPS1,                                                 (rFC p33) */
2429 		saa7146_write(dev, MC1, (MASK_13 | MASK_29 ));
2430 
2431 		mdelay(10);
2432 		/* now send VSYNC_B to rps1 by rising GPIO3 */
2433 		saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTHI);
2434 		mdelay(10);
2435 		/* if rps1 responded by lowering the GPIO3,
2436 		 * then we have budgetpatch hardware
2437 		 */
2438 		if ((saa7146_read(dev, GPIO_CTRL) & 0x10000000) == 0) {
2439 			budgetpatch = 1;
2440 			printk("dvb-ttpci: BUDGET-PATCH DETECTED.\n");
2441 		}
2442 		/* Disable RPS1 */
2443 		saa7146_write(dev, MC1, ( MASK_29 ));
2444 #if RPS_IRQ
2445 		printk("dvb-ttpci: Event Counter 1 0x%04x\n", saa7146_read(dev, EC1R) & 0x3fff );
2446 #endif
2447 	}
2448 
2449 	/* prepare the av7110 device struct */
2450 	av7110 = kzalloc(sizeof(struct av7110), GFP_KERNEL);
2451 	if (!av7110) {
2452 		dprintk(1, "out of memory\n");
2453 		return -ENOMEM;
2454 	}
2455 
2456 	av7110->card_name = (char*) pci_ext->ext_priv;
2457 	av7110->dev = dev;
2458 	dev->ext_priv = av7110;
2459 
2460 	ret = get_firmware(av7110);
2461 	if (ret < 0)
2462 		goto err_kfree_0;
2463 
2464 	ret = dvb_register_adapter(&av7110->dvb_adapter, av7110->card_name,
2465 				   THIS_MODULE, &dev->pci->dev, adapter_nr);
2466 	if (ret < 0)
2467 		goto err_put_firmware_1;
2468 
2469 	/* the Siemens DVB needs this if you want to have the i2c chips
2470 	   get recognized before the main driver is fully loaded */
2471 	saa7146_write(dev, GPIO_CTRL, 0x500000);
2472 
2473 	strscpy(av7110->i2c_adap.name, pci_ext->ext_priv,
2474 		sizeof(av7110->i2c_adap.name));
2475 
2476 	saa7146_i2c_adapter_prepare(dev, &av7110->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120); /* 275 kHz */
2477 
2478 	ret = i2c_add_adapter(&av7110->i2c_adap);
2479 	if (ret < 0)
2480 		goto err_dvb_unregister_adapter_2;
2481 
2482 	ttpci_eeprom_parse_mac(&av7110->i2c_adap,
2483 			       av7110->dvb_adapter.proposed_mac);
2484 	ret = -ENOMEM;
2485 
2486 	/* full-ts mod? */
2487 	if (full_ts)
2488 		av7110->full_ts = true;
2489 
2490 	/* check for full-ts flag in eeprom */
2491 	if (i2c_readreg(av7110, 0xaa, 0) == 0x4f && i2c_readreg(av7110, 0xaa, 1) == 0x45) {
2492 		u8 flags = i2c_readreg(av7110, 0xaa, 2);
2493 		if (flags != 0xff && (flags & 0x01))
2494 			av7110->full_ts = true;
2495 	}
2496 
2497 	if (av7110->full_ts) {
2498 		printk(KERN_INFO "dvb-ttpci: full-ts mode enabled for saa7146 port B\n");
2499 		spin_lock_init(&av7110->feedlock1);
2500 		av7110->grabbing = saa7146_vmalloc_build_pgtable(pdev, length,
2501 								 &av7110->pt);
2502 		if (!av7110->grabbing)
2503 			goto err_i2c_del_3;
2504 
2505 		saa7146_write(dev, DD1_STREAM_B, 0x00000000);
2506 		saa7146_write(dev, MC2, (MASK_10 | MASK_26));
2507 
2508 		saa7146_write(dev, DD1_INIT, 0x00000600);
2509 		saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
2510 
2511 		saa7146_write(dev, BRS_CTRL, 0x60000000);
2512 		saa7146_write(dev, MC2, MASK_08 | MASK_24);
2513 
2514 		/* dma3 */
2515 		saa7146_write(dev, PCI_BT_V1, 0x001c0000 | (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
2516 		saa7146_write(dev, BASE_ODD3, 0);
2517 		saa7146_write(dev, BASE_EVEN3, 0);
2518 		saa7146_write(dev, PROT_ADDR3, TS_WIDTH * TS_HEIGHT);
2519 		saa7146_write(dev, PITCH3, TS_WIDTH);
2520 		saa7146_write(dev, BASE_PAGE3, av7110->pt.dma | ME1 | 0x90);
2521 		saa7146_write(dev, NUM_LINE_BYTE3, (TS_HEIGHT << 16) | TS_WIDTH);
2522 		saa7146_write(dev, MC2, MASK_04 | MASK_20);
2523 
2524 		tasklet_setup(&av7110->vpe_tasklet, vpeirq);
2525 
2526 	} else if (budgetpatch) {
2527 		spin_lock_init(&av7110->feedlock1);
2528 		av7110->grabbing = saa7146_vmalloc_build_pgtable(pdev, length,
2529 								 &av7110->pt);
2530 		if (!av7110->grabbing)
2531 			goto err_i2c_del_3;
2532 
2533 		saa7146_write(dev, PCI_BT_V1, 0x1c1f101f);
2534 		saa7146_write(dev, BCS_CTRL, 0x80400040);
2535 		/* set dd1 stream a & b */
2536 		saa7146_write(dev, DD1_STREAM_B, 0x00000000);
2537 		saa7146_write(dev, DD1_INIT, 0x03000200);
2538 		saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
2539 		saa7146_write(dev, BRS_CTRL, 0x60000000);
2540 		saa7146_write(dev, BASE_ODD3, 0);
2541 		saa7146_write(dev, BASE_EVEN3, 0);
2542 		saa7146_write(dev, PROT_ADDR3, TS_WIDTH * TS_HEIGHT);
2543 		saa7146_write(dev, BASE_PAGE3, av7110->pt.dma | ME1 | 0x90);
2544 
2545 		saa7146_write(dev, PITCH3, TS_WIDTH);
2546 		saa7146_write(dev, NUM_LINE_BYTE3, (TS_HEIGHT << 16) | TS_WIDTH);
2547 
2548 		/* upload all */
2549 		saa7146_write(dev, MC2, 0x077c077c);
2550 		saa7146_write(dev, GPIO_CTRL, 0x000000);
2551 #if RPS_IRQ
2552 		/* set event counter 1 source as RPS1 interrupt (0x03)          (rE4 p53)
2553 		 * use 0x03 to track RPS1 interrupts - increase by 1 every gpio3 is toggled
2554 		 * use 0x15 to track VPE  interrupts - increase by 1 every vpeirq() is called
2555 		 */
2556 		saa7146_write(dev, EC1SSR, (0x03<<2) | 3 );
2557 		/* set event counter 1 threshold to maximum allowed value        (rEC p55) */
2558 		saa7146_write(dev, ECT1R,  0x3fff );
2559 #endif
2560 		/* Setup BUDGETPATCH MAIN RPS1 "program" (p35) */
2561 		count = 0;
2562 
2563 		/* Wait Source Line Counter Threshold                           (p36) */
2564 		WRITE_RPS1(CMD_PAUSE | EVT_HS);
2565 		/* Set GPIO3=1                                                  (p42) */
2566 		WRITE_RPS1(CMD_WR_REG_MASK | (GPIO_CTRL>>2));
2567 		WRITE_RPS1(GPIO3_MSK);
2568 		WRITE_RPS1(SAA7146_GPIO_OUTHI<<24);
2569 #if RPS_IRQ
2570 		/* issue RPS1 interrupt */
2571 		WRITE_RPS1(CMD_INTERRUPT);
2572 #endif
2573 		/* Wait reset Source Line Counter Threshold                     (p36) */
2574 		WRITE_RPS1(CMD_PAUSE | RPS_INV | EVT_HS);
2575 		/* Set GPIO3=0                                                  (p42) */
2576 		WRITE_RPS1(CMD_WR_REG_MASK | (GPIO_CTRL>>2));
2577 		WRITE_RPS1(GPIO3_MSK);
2578 		WRITE_RPS1(SAA7146_GPIO_OUTLO<<24);
2579 #if RPS_IRQ
2580 		/* issue RPS1 interrupt */
2581 		WRITE_RPS1(CMD_INTERRUPT);
2582 #endif
2583 		/* Jump to begin of RPS program                                 (p37) */
2584 		WRITE_RPS1(CMD_JUMP);
2585 		WRITE_RPS1(dev->d_rps1.dma_handle);
2586 
2587 		/* Fix VSYNC level */
2588 		saa7146_setgpio(dev, 3, SAA7146_GPIO_OUTLO);
2589 		/* Set RPS1 Address register to point to RPS code               (r108 p42) */
2590 		saa7146_write(dev, RPS_ADDR1, dev->d_rps1.dma_handle);
2591 		/* Set Source Line Counter Threshold, using BRS                 (rCC p43)
2592 		 * It generates HS event every TS_HEIGHT lines
2593 		 * this is related to TS_WIDTH set in register
2594 		 * NUM_LINE_BYTE3. If NUM_LINE_BYTE low 16 bits
2595 		 * are set to TS_WIDTH bytes (TS_WIDTH=2*188),
2596 		 * then RPS_THRESH1 should be set to trigger
2597 		 * every TS_HEIGHT (512) lines.
2598 		 */
2599 		saa7146_write(dev, RPS_THRESH1, (TS_HEIGHT*1) | MASK_12 );
2600 
2601 		/* Enable RPS1                                                  (rFC p33) */
2602 		saa7146_write(dev, MC1, (MASK_13 | MASK_29));
2603 
2604 		/* end of budgetpatch register initialization */
2605 		tasklet_setup(&av7110->vpe_tasklet,  vpeirq);
2606 	} else {
2607 		saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
2608 		saa7146_write(dev, BCS_CTRL, 0x80400040);
2609 
2610 		/* set dd1 stream a & b */
2611 		saa7146_write(dev, DD1_STREAM_B, 0x00000000);
2612 		saa7146_write(dev, DD1_INIT, 0x03000000);
2613 		saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
2614 
2615 		/* upload all */
2616 		saa7146_write(dev, MC2, 0x077c077c);
2617 		saa7146_write(dev, GPIO_CTRL, 0x000000);
2618 	}
2619 
2620 	tasklet_setup(&av7110->debi_tasklet, debiirq);
2621 	tasklet_setup(&av7110->gpio_tasklet, gpioirq);
2622 
2623 	mutex_init(&av7110->pid_mutex);
2624 
2625 	/* locks for data transfers from/to AV7110 */
2626 	spin_lock_init(&av7110->debilock);
2627 	mutex_init(&av7110->dcomlock);
2628 	av7110->debitype = -1;
2629 
2630 	/* default OSD window */
2631 	av7110->osdwin = 1;
2632 	mutex_init(&av7110->osd_mutex);
2633 
2634 	/* TV standard */
2635 	av7110->vidmode = tv_standard == 1 ? AV7110_VIDEO_MODE_NTSC
2636 					   : AV7110_VIDEO_MODE_PAL;
2637 
2638 	/* ARM "watchdog" */
2639 	init_waitqueue_head(&av7110->arm_wait);
2640 	av7110->arm_thread = NULL;
2641 
2642 	/* allocate and init buffers */
2643 	av7110->debi_virt = dma_alloc_coherent(&pdev->dev, 8192,
2644 					       &av7110->debi_bus, GFP_KERNEL);
2645 	if (!av7110->debi_virt)
2646 		goto err_saa71466_vfree_4;
2647 
2648 
2649 	av7110->iobuf = vmalloc(AVOUTLEN+AOUTLEN+BMPLEN+4*IPACKS);
2650 	if (!av7110->iobuf)
2651 		goto err_pci_free_5;
2652 
2653 	ret = av7110_av_init(av7110);
2654 	if (ret < 0)
2655 		goto err_iobuf_vfree_6;
2656 
2657 	/* init BMP buffer */
2658 	av7110->bmpbuf = av7110->iobuf+AVOUTLEN+AOUTLEN;
2659 	init_waitqueue_head(&av7110->bmpq);
2660 
2661 	ret = av7110_ca_init(av7110);
2662 	if (ret < 0)
2663 		goto err_av7110_av_exit_7;
2664 
2665 	/* load firmware into AV7110 cards */
2666 	ret = av7110_bootarm(av7110);
2667 	if (ret < 0)
2668 		goto err_av7110_ca_exit_8;
2669 
2670 	ret = av7110_firmversion(av7110);
2671 	if (ret < 0)
2672 		goto err_stop_arm_9;
2673 
2674 	if (FW_VERSION(av7110->arm_app)<0x2501)
2675 		printk(KERN_WARNING
2676 		       "dvb-ttpci: Warning, firmware version 0x%04x is too old. System might be unstable!\n",
2677 		       FW_VERSION(av7110->arm_app));
2678 
2679 	thread = kthread_run(arm_thread, (void *) av7110, "arm_mon");
2680 	if (IS_ERR(thread)) {
2681 		ret = PTR_ERR(thread);
2682 		goto err_stop_arm_9;
2683 	}
2684 	av7110->arm_thread = thread;
2685 
2686 	/* set initial volume in mixer struct */
2687 	av7110->mixer.volume_left  = volume;
2688 	av7110->mixer.volume_right = volume;
2689 
2690 	ret = av7110_register(av7110);
2691 	if (ret < 0)
2692 		goto err_arm_thread_stop_10;
2693 
2694 	init_av7110_av(av7110);
2695 
2696 	/* special case DVB-C: these cards have an analog tuner
2697 	   plus need some special handling, so we have separate
2698 	   saa7146_ext_vv data for these... */
2699 	ret = av7110_init_v4l(av7110);
2700 	if (ret < 0)
2701 		goto err_av7110_unregister_11;
2702 
2703 	av7110->dvb_adapter.priv = av7110;
2704 	ret = frontend_init(av7110);
2705 	if (ret < 0)
2706 		goto err_av7110_exit_v4l_12;
2707 
2708 	mutex_init(&av7110->ioctl_mutex);
2709 
2710 #if IS_ENABLED(CONFIG_DVB_AV7110_IR)
2711 	av7110_ir_init(av7110);
2712 #endif
2713 	printk(KERN_INFO "dvb-ttpci: found av7110-%d.\n", av7110_num);
2714 	av7110_num++;
2715 out:
2716 	return ret;
2717 
2718 err_av7110_exit_v4l_12:
2719 	av7110_exit_v4l(av7110);
2720 err_av7110_unregister_11:
2721 	dvb_unregister(av7110);
2722 err_arm_thread_stop_10:
2723 	av7110_arm_sync(av7110);
2724 err_stop_arm_9:
2725 	/* Nothing to do. Rejoice. */
2726 err_av7110_ca_exit_8:
2727 	av7110_ca_exit(av7110);
2728 err_av7110_av_exit_7:
2729 	av7110_av_exit(av7110);
2730 err_iobuf_vfree_6:
2731 	vfree(av7110->iobuf);
2732 err_pci_free_5:
2733 	dma_free_coherent(&pdev->dev, 8192, av7110->debi_virt,
2734 			  av7110->debi_bus);
2735 err_saa71466_vfree_4:
2736 	if (av7110->grabbing)
2737 		saa7146_vfree_destroy_pgtable(pdev, av7110->grabbing, &av7110->pt);
2738 err_i2c_del_3:
2739 	i2c_del_adapter(&av7110->i2c_adap);
2740 err_dvb_unregister_adapter_2:
2741 	dvb_unregister_adapter(&av7110->dvb_adapter);
2742 err_put_firmware_1:
2743 	put_firmware(av7110);
2744 err_kfree_0:
2745 	kfree(av7110);
2746 	goto out;
2747 }
2748 
av7110_detach(struct saa7146_dev * saa)2749 static int av7110_detach(struct saa7146_dev* saa)
2750 {
2751 	struct av7110 *av7110 = saa->ext_priv;
2752 	dprintk(4, "%p\n", av7110);
2753 
2754 #if IS_ENABLED(CONFIG_DVB_AV7110_IR)
2755 	av7110_ir_exit(av7110);
2756 #endif
2757 	if (budgetpatch || av7110->full_ts) {
2758 		if (budgetpatch) {
2759 			/* Disable RPS1 */
2760 			saa7146_write(saa, MC1, MASK_29);
2761 			/* VSYNC LOW (inactive) */
2762 			saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
2763 		}
2764 		saa7146_write(saa, MC1, MASK_20);	/* DMA3 off */
2765 		SAA7146_IER_DISABLE(saa, MASK_10);
2766 		SAA7146_ISR_CLEAR(saa, MASK_10);
2767 		msleep(50);
2768 		tasklet_kill(&av7110->vpe_tasklet);
2769 		saa7146_vfree_destroy_pgtable(saa->pci, av7110->grabbing, &av7110->pt);
2770 	}
2771 	av7110_exit_v4l(av7110);
2772 
2773 	av7110_arm_sync(av7110);
2774 
2775 	tasklet_kill(&av7110->debi_tasklet);
2776 	tasklet_kill(&av7110->gpio_tasklet);
2777 
2778 	dvb_unregister(av7110);
2779 
2780 	SAA7146_IER_DISABLE(saa, MASK_19 | MASK_03);
2781 	SAA7146_ISR_CLEAR(saa, MASK_19 | MASK_03);
2782 
2783 	av7110_ca_exit(av7110);
2784 	av7110_av_exit(av7110);
2785 
2786 	vfree(av7110->iobuf);
2787 	dma_free_coherent(&saa->pci->dev, 8192, av7110->debi_virt,
2788 			  av7110->debi_bus);
2789 
2790 	i2c_del_adapter(&av7110->i2c_adap);
2791 
2792 	dvb_unregister_adapter (&av7110->dvb_adapter);
2793 
2794 	av7110_num--;
2795 
2796 	put_firmware(av7110);
2797 
2798 	kfree(av7110);
2799 
2800 	saa->ext_priv = NULL;
2801 
2802 	return 0;
2803 }
2804 
2805 
av7110_irq(struct saa7146_dev * dev,u32 * isr)2806 static void av7110_irq(struct saa7146_dev* dev, u32 *isr)
2807 {
2808 	struct av7110 *av7110 = dev->ext_priv;
2809 
2810 	//print_time("av7110_irq");
2811 
2812 	/* Note: Don't try to handle the DEBI error irq (MASK_18), in
2813 	 * intel mode the timeout is asserted all the time...
2814 	 */
2815 
2816 	if (*isr & MASK_19) {
2817 		//printk("av7110_irq: DEBI\n");
2818 		/* Note 1: The DEBI irq is level triggered: We must enable it
2819 		 * only after we started a DMA xfer, and disable it here
2820 		 * immediately, or it will be signalled all the time while
2821 		 * DEBI is idle.
2822 		 * Note 2: You would think that an irq which is masked is
2823 		 * not signalled by the hardware. Not so for the SAA7146:
2824 		 * An irq is signalled as long as the corresponding bit
2825 		 * in the ISR is set, and disabling irqs just prevents the
2826 		 * hardware from setting the ISR bit. This means a) that we
2827 		 * must clear the ISR *after* disabling the irq (which is why
2828 		 * we must do it here even though saa7146_core did it already),
2829 		 * and b) that if we were to disable an edge triggered irq
2830 		 * (like the gpio irqs sadly are) temporarily we would likely
2831 		 * loose some. This sucks :-(
2832 		 */
2833 		SAA7146_IER_DISABLE(av7110->dev, MASK_19);
2834 		SAA7146_ISR_CLEAR(av7110->dev, MASK_19);
2835 		tasklet_schedule(&av7110->debi_tasklet);
2836 	}
2837 
2838 	if (*isr & MASK_03) {
2839 		//printk("av7110_irq: GPIO\n");
2840 		tasklet_schedule(&av7110->gpio_tasklet);
2841 	}
2842 
2843 	if (*isr & MASK_10)
2844 		tasklet_schedule(&av7110->vpe_tasklet);
2845 }
2846 
2847 
2848 static struct saa7146_extension av7110_extension_driver;
2849 
2850 #define MAKE_AV7110_INFO(x_var,x_name) \
2851 static struct saa7146_pci_extension_data x_var = { \
2852 	.ext_priv = x_name, \
2853 	.ext = &av7110_extension_driver }
2854 
2855 MAKE_AV7110_INFO(tts_1_X_fsc,"Technotrend/Hauppauge WinTV DVB-S rev1.X or Fujitsu Siemens DVB-C");
2856 MAKE_AV7110_INFO(ttt_1_X,    "Technotrend/Hauppauge WinTV DVB-T rev1.X");
2857 MAKE_AV7110_INFO(ttc_1_X,    "Technotrend/Hauppauge WinTV Nexus-CA rev1.X");
2858 MAKE_AV7110_INFO(ttc_2_X,    "Technotrend/Hauppauge WinTV DVB-C rev2.X");
2859 MAKE_AV7110_INFO(tts_2_X,    "Technotrend/Hauppauge WinTV Nexus-S rev2.X");
2860 MAKE_AV7110_INFO(tts_2_3,    "Technotrend/Hauppauge WinTV Nexus-S rev2.3");
2861 MAKE_AV7110_INFO(tts_1_3se,  "Technotrend/Hauppauge WinTV DVB-S rev1.3 SE");
2862 MAKE_AV7110_INFO(ttt,        "Technotrend/Hauppauge DVB-T");
2863 MAKE_AV7110_INFO(fsc,        "Fujitsu Siemens DVB-C");
2864 MAKE_AV7110_INFO(fss,        "Fujitsu Siemens DVB-S rev1.6");
2865 MAKE_AV7110_INFO(gxs_1_3,    "Galaxis DVB-S rev1.3");
2866 
2867 static const struct pci_device_id pci_tbl[] = {
2868 	MAKE_EXTENSION_PCI(fsc,         0x110a, 0x0000),
2869 	MAKE_EXTENSION_PCI(tts_1_X_fsc, 0x13c2, 0x0000),
2870 	MAKE_EXTENSION_PCI(ttt_1_X,     0x13c2, 0x0001),
2871 	MAKE_EXTENSION_PCI(ttc_2_X,     0x13c2, 0x0002),
2872 	MAKE_EXTENSION_PCI(tts_2_X,     0x13c2, 0x0003),
2873 	MAKE_EXTENSION_PCI(gxs_1_3,     0x13c2, 0x0004),
2874 	MAKE_EXTENSION_PCI(fss,         0x13c2, 0x0006),
2875 	MAKE_EXTENSION_PCI(ttt,         0x13c2, 0x0008),
2876 	MAKE_EXTENSION_PCI(ttc_1_X,     0x13c2, 0x000a),
2877 	MAKE_EXTENSION_PCI(tts_2_3,     0x13c2, 0x000e),
2878 	MAKE_EXTENSION_PCI(tts_1_3se,   0x13c2, 0x1002),
2879 
2880 /*	MAKE_EXTENSION_PCI(???, 0x13c2, 0x0005), UNDEFINED CARD */ // Technisat SkyStar1
2881 /*	MAKE_EXTENSION_PCI(???, 0x13c2, 0x0009), UNDEFINED CARD */ // TT/Hauppauge WinTV Nexus-CA v????
2882 
2883 	{
2884 		.vendor    = 0,
2885 	}
2886 };
2887 
2888 MODULE_DEVICE_TABLE(pci, pci_tbl);
2889 
2890 
2891 static struct saa7146_extension av7110_extension_driver = {
2892 	.name		= "av7110",
2893 	.flags		= SAA7146_USE_I2C_IRQ,
2894 
2895 	.module		= THIS_MODULE,
2896 	.pci_tbl	= &pci_tbl[0],
2897 	.attach		= av7110_attach,
2898 	.detach		= av7110_detach,
2899 
2900 	.irq_mask	= MASK_19 | MASK_03 | MASK_10,
2901 	.irq_func	= av7110_irq,
2902 };
2903 
2904 
av7110_init(void)2905 static int __init av7110_init(void)
2906 {
2907 	return saa7146_register_extension(&av7110_extension_driver);
2908 }
2909 
2910 
av7110_exit(void)2911 static void __exit av7110_exit(void)
2912 {
2913 	saa7146_unregister_extension(&av7110_extension_driver);
2914 }
2915 
2916 module_init(av7110_init);
2917 module_exit(av7110_exit);
2918 
2919 MODULE_DESCRIPTION("driver for the SAA7146 based AV110 PCI DVB cards by Siemens, Technotrend, Hauppauge");
2920 MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, others");
2921 MODULE_LICENSE("GPL");
2922